clk: vc5: Add support for IDT VersaClock 5P49V5923 and 5P49V5933
[linux-2.6/btrfs-unstable.git] / drivers / vfio / mdev / mdev_core.c
blobbe1ee89ee9172f4408eab231c9b1e6a52a253c88
1 /*
2 * Mediated device Core Driver
4 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
5 * Author: Neo Jia <cjia@nvidia.com>
6 * Kirti Wankhede <kwankhede@nvidia.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/uuid.h>
17 #include <linux/sysfs.h>
18 #include <linux/mdev.h>
20 #include "mdev_private.h"
22 #define DRIVER_VERSION "0.1"
23 #define DRIVER_AUTHOR "NVIDIA Corporation"
24 #define DRIVER_DESC "Mediated device Core Driver"
26 static LIST_HEAD(parent_list);
27 static DEFINE_MUTEX(parent_list_lock);
28 static struct class_compat *mdev_bus_compat_class;
30 static int _find_mdev_device(struct device *dev, void *data)
32 struct mdev_device *mdev;
34 if (!dev_is_mdev(dev))
35 return 0;
37 mdev = to_mdev_device(dev);
39 if (uuid_le_cmp(mdev->uuid, *(uuid_le *)data) == 0)
40 return 1;
42 return 0;
45 static bool mdev_device_exist(struct parent_device *parent, uuid_le uuid)
47 struct device *dev;
49 dev = device_find_child(parent->dev, &uuid, _find_mdev_device);
50 if (dev) {
51 put_device(dev);
52 return true;
55 return false;
58 /* Should be called holding parent_list_lock */
59 static struct parent_device *__find_parent_device(struct device *dev)
61 struct parent_device *parent;
63 list_for_each_entry(parent, &parent_list, next) {
64 if (parent->dev == dev)
65 return parent;
67 return NULL;
70 static void mdev_release_parent(struct kref *kref)
72 struct parent_device *parent = container_of(kref, struct parent_device,
73 ref);
74 struct device *dev = parent->dev;
76 kfree(parent);
77 put_device(dev);
80 static
81 inline struct parent_device *mdev_get_parent(struct parent_device *parent)
83 if (parent)
84 kref_get(&parent->ref);
86 return parent;
89 static inline void mdev_put_parent(struct parent_device *parent)
91 if (parent)
92 kref_put(&parent->ref, mdev_release_parent);
95 static int mdev_device_create_ops(struct kobject *kobj,
96 struct mdev_device *mdev)
98 struct parent_device *parent = mdev->parent;
99 int ret;
101 ret = parent->ops->create(kobj, mdev);
102 if (ret)
103 return ret;
105 ret = sysfs_create_groups(&mdev->dev.kobj,
106 parent->ops->mdev_attr_groups);
107 if (ret)
108 parent->ops->remove(mdev);
110 return ret;
114 * mdev_device_remove_ops gets called from sysfs's 'remove' and when parent
115 * device is being unregistered from mdev device framework.
116 * - 'force_remove' is set to 'false' when called from sysfs's 'remove' which
117 * indicates that if the mdev device is active, used by VMM or userspace
118 * application, vendor driver could return error then don't remove the device.
119 * - 'force_remove' is set to 'true' when called from mdev_unregister_device()
120 * which indicate that parent device is being removed from mdev device
121 * framework so remove mdev device forcefully.
123 static int mdev_device_remove_ops(struct mdev_device *mdev, bool force_remove)
125 struct parent_device *parent = mdev->parent;
126 int ret;
129 * Vendor driver can return error if VMM or userspace application is
130 * using this mdev device.
132 ret = parent->ops->remove(mdev);
133 if (ret && !force_remove)
134 return -EBUSY;
136 sysfs_remove_groups(&mdev->dev.kobj, parent->ops->mdev_attr_groups);
137 return 0;
140 static int mdev_device_remove_cb(struct device *dev, void *data)
142 if (!dev_is_mdev(dev))
143 return 0;
145 return mdev_device_remove(dev, data ? *(bool *)data : true);
149 * mdev_register_device : Register a device
150 * @dev: device structure representing parent device.
151 * @ops: Parent device operation structure to be registered.
153 * Add device to list of registered parent devices.
154 * Returns a negative value on error, otherwise 0.
156 int mdev_register_device(struct device *dev, const struct parent_ops *ops)
158 int ret;
159 struct parent_device *parent;
161 /* check for mandatory ops */
162 if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
163 return -EINVAL;
165 dev = get_device(dev);
166 if (!dev)
167 return -EINVAL;
169 mutex_lock(&parent_list_lock);
171 /* Check for duplicate */
172 parent = __find_parent_device(dev);
173 if (parent) {
174 ret = -EEXIST;
175 goto add_dev_err;
178 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
179 if (!parent) {
180 ret = -ENOMEM;
181 goto add_dev_err;
184 kref_init(&parent->ref);
185 mutex_init(&parent->lock);
187 parent->dev = dev;
188 parent->ops = ops;
190 if (!mdev_bus_compat_class) {
191 mdev_bus_compat_class = class_compat_register("mdev_bus");
192 if (!mdev_bus_compat_class) {
193 ret = -ENOMEM;
194 goto add_dev_err;
198 ret = parent_create_sysfs_files(parent);
199 if (ret)
200 goto add_dev_err;
202 ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
203 if (ret)
204 dev_warn(dev, "Failed to create compatibility class link\n");
206 list_add(&parent->next, &parent_list);
207 mutex_unlock(&parent_list_lock);
209 dev_info(dev, "MDEV: Registered\n");
210 return 0;
212 add_dev_err:
213 mutex_unlock(&parent_list_lock);
214 if (parent)
215 mdev_put_parent(parent);
216 else
217 put_device(dev);
218 return ret;
220 EXPORT_SYMBOL(mdev_register_device);
223 * mdev_unregister_device : Unregister a parent device
224 * @dev: device structure representing parent device.
226 * Remove device from list of registered parent devices. Give a chance to free
227 * existing mediated devices for given device.
230 void mdev_unregister_device(struct device *dev)
232 struct parent_device *parent;
233 bool force_remove = true;
235 mutex_lock(&parent_list_lock);
236 parent = __find_parent_device(dev);
238 if (!parent) {
239 mutex_unlock(&parent_list_lock);
240 return;
242 dev_info(dev, "MDEV: Unregistering\n");
244 list_del(&parent->next);
245 class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
247 device_for_each_child(dev, (void *)&force_remove,
248 mdev_device_remove_cb);
250 parent_remove_sysfs_files(parent);
252 mutex_unlock(&parent_list_lock);
253 mdev_put_parent(parent);
255 EXPORT_SYMBOL(mdev_unregister_device);
257 static void mdev_device_release(struct device *dev)
259 struct mdev_device *mdev = to_mdev_device(dev);
261 dev_dbg(&mdev->dev, "MDEV: destroying\n");
262 kfree(mdev);
265 int mdev_device_create(struct kobject *kobj, struct device *dev, uuid_le uuid)
267 int ret;
268 struct mdev_device *mdev;
269 struct parent_device *parent;
270 struct mdev_type *type = to_mdev_type(kobj);
272 parent = mdev_get_parent(type->parent);
273 if (!parent)
274 return -EINVAL;
276 mutex_lock(&parent->lock);
278 /* Check for duplicate */
279 if (mdev_device_exist(parent, uuid)) {
280 ret = -EEXIST;
281 goto create_err;
284 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
285 if (!mdev) {
286 ret = -ENOMEM;
287 goto create_err;
290 memcpy(&mdev->uuid, &uuid, sizeof(uuid_le));
291 mdev->parent = parent;
292 kref_init(&mdev->ref);
294 mdev->dev.parent = dev;
295 mdev->dev.bus = &mdev_bus_type;
296 mdev->dev.release = mdev_device_release;
297 dev_set_name(&mdev->dev, "%pUl", uuid.b);
299 ret = device_register(&mdev->dev);
300 if (ret) {
301 put_device(&mdev->dev);
302 goto create_err;
305 ret = mdev_device_create_ops(kobj, mdev);
306 if (ret)
307 goto create_failed;
309 ret = mdev_create_sysfs_files(&mdev->dev, type);
310 if (ret) {
311 mdev_device_remove_ops(mdev, true);
312 goto create_failed;
315 mdev->type_kobj = kobj;
316 dev_dbg(&mdev->dev, "MDEV: created\n");
318 mutex_unlock(&parent->lock);
319 return ret;
321 create_failed:
322 device_unregister(&mdev->dev);
324 create_err:
325 mutex_unlock(&parent->lock);
326 mdev_put_parent(parent);
327 return ret;
330 int mdev_device_remove(struct device *dev, bool force_remove)
332 struct mdev_device *mdev;
333 struct parent_device *parent;
334 struct mdev_type *type;
335 int ret;
337 mdev = to_mdev_device(dev);
338 type = to_mdev_type(mdev->type_kobj);
339 parent = mdev->parent;
340 mutex_lock(&parent->lock);
342 ret = mdev_device_remove_ops(mdev, force_remove);
343 if (ret) {
344 mutex_unlock(&parent->lock);
345 return ret;
348 mdev_remove_sysfs_files(dev, type);
349 device_unregister(dev);
350 mutex_unlock(&parent->lock);
351 mdev_put_parent(parent);
352 return ret;
355 static int __init mdev_init(void)
357 int ret;
359 ret = mdev_bus_register();
362 * Attempt to load known vfio_mdev. This gives us a working environment
363 * without the user needing to explicitly load vfio_mdev driver.
365 if (!ret)
366 request_module_nowait("vfio_mdev");
368 return ret;
371 static void __exit mdev_exit(void)
373 if (mdev_bus_compat_class)
374 class_compat_unregister(mdev_bus_compat_class);
376 mdev_bus_unregister();
379 module_init(mdev_init)
380 module_exit(mdev_exit)
382 MODULE_VERSION(DRIVER_VERSION);
383 MODULE_LICENSE("GPL v2");
384 MODULE_AUTHOR(DRIVER_AUTHOR);
385 MODULE_DESCRIPTION(DRIVER_DESC);