1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/virtio.h>
3 #include <linux/spinlock.h>
4 #include <linux/virtio_config.h>
5 #include <linux/module.h>
8 #include <uapi/linux/virtio_ids.h>
10 /* Unique numbering for virtio devices. */
11 static DEFINE_IDA(virtio_index_ida);
13 static ssize_t device_show(struct device *_d,
14 struct device_attribute *attr, char *buf)
16 struct virtio_device *dev = dev_to_virtio(_d);
17 return sprintf(buf, "0x%04x\n", dev->id.device);
19 static DEVICE_ATTR_RO(device);
21 static ssize_t vendor_show(struct device *_d,
22 struct device_attribute *attr, char *buf)
24 struct virtio_device *dev = dev_to_virtio(_d);
25 return sprintf(buf, "0x%04x\n", dev->id.vendor);
27 static DEVICE_ATTR_RO(vendor);
29 static ssize_t status_show(struct device *_d,
30 struct device_attribute *attr, char *buf)
32 struct virtio_device *dev = dev_to_virtio(_d);
33 return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
35 static DEVICE_ATTR_RO(status);
37 static ssize_t modalias_show(struct device *_d,
38 struct device_attribute *attr, char *buf)
40 struct virtio_device *dev = dev_to_virtio(_d);
41 return sprintf(buf, "virtio:d%08Xv%08X\n",
42 dev->id.device, dev->id.vendor);
44 static DEVICE_ATTR_RO(modalias);
46 static ssize_t features_show(struct device *_d,
47 struct device_attribute *attr, char *buf)
49 struct virtio_device *dev = dev_to_virtio(_d);
53 /* We actually represent this as a bitstring, as it could be
54 * arbitrary length in future. */
55 for (i = 0; i < sizeof(dev->features)*8; i++)
56 len += sprintf(buf+len, "%c",
57 __virtio_test_bit(dev, i) ? '1' : '0');
58 len += sprintf(buf+len, "\n");
61 static DEVICE_ATTR_RO(features);
63 static struct attribute *virtio_dev_attrs[] = {
64 &dev_attr_device.attr,
65 &dev_attr_vendor.attr,
66 &dev_attr_status.attr,
67 &dev_attr_modalias.attr,
68 &dev_attr_features.attr,
71 ATTRIBUTE_GROUPS(virtio_dev);
73 static inline int virtio_id_match(const struct virtio_device *dev,
74 const struct virtio_device_id *id)
76 if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
79 return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor;
82 /* This looks through all the IDs a driver claims to support. If any of them
83 * match, we return 1 and the kernel will call virtio_dev_probe(). */
84 static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
87 struct virtio_device *dev = dev_to_virtio(_dv);
88 const struct virtio_device_id *ids;
90 ids = drv_to_virtio(_dr)->id_table;
91 for (i = 0; ids[i].device; i++)
92 if (virtio_id_match(dev, &ids[i]))
97 static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
99 struct virtio_device *dev = dev_to_virtio(_dv);
101 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
102 dev->id.device, dev->id.vendor);
105 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
109 struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
111 for (i = 0; i < drv->feature_table_size; i++)
112 if (drv->feature_table[i] == fbit)
115 if (drv->feature_table_legacy) {
116 for (i = 0; i < drv->feature_table_size_legacy; i++)
117 if (drv->feature_table_legacy[i] == fbit)
123 EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
125 static void __virtio_config_changed(struct virtio_device *dev)
127 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
129 if (!dev->config_enabled)
130 dev->config_change_pending = true;
131 else if (drv && drv->config_changed)
132 drv->config_changed(dev);
135 void virtio_config_changed(struct virtio_device *dev)
139 spin_lock_irqsave(&dev->config_lock, flags);
140 __virtio_config_changed(dev);
141 spin_unlock_irqrestore(&dev->config_lock, flags);
143 EXPORT_SYMBOL_GPL(virtio_config_changed);
145 static void virtio_config_disable(struct virtio_device *dev)
147 spin_lock_irq(&dev->config_lock);
148 dev->config_enabled = false;
149 spin_unlock_irq(&dev->config_lock);
152 static void virtio_config_enable(struct virtio_device *dev)
154 spin_lock_irq(&dev->config_lock);
155 dev->config_enabled = true;
156 if (dev->config_change_pending)
157 __virtio_config_changed(dev);
158 dev->config_change_pending = false;
159 spin_unlock_irq(&dev->config_lock);
162 void virtio_add_status(struct virtio_device *dev, unsigned int status)
165 dev->config->set_status(dev, dev->config->get_status(dev) | status);
167 EXPORT_SYMBOL_GPL(virtio_add_status);
169 /* Do some validation, then set FEATURES_OK */
170 static int virtio_features_ok(struct virtio_device *dev)
177 ret = arch_has_restricted_virtio_memory_access();
179 if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) {
181 "device must provide VIRTIO_F_VERSION_1\n");
185 if (!virtio_has_feature(dev, VIRTIO_F_ACCESS_PLATFORM)) {
187 "device must provide VIRTIO_F_ACCESS_PLATFORM\n");
192 if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
195 virtio_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
196 status = dev->config->get_status(dev);
197 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
198 dev_err(&dev->dev, "virtio: device refuses features: %x\n",
206 * virtio_reset_device - quiesce device for removal
207 * @dev: the device to reset
209 * Prevents device from sending interrupts and accessing memory.
211 * Generally used for cleanup during driver / device removal.
213 * Once this has been invoked, caller must ensure that
214 * virtqueue_notify / virtqueue_kick are not in progress.
216 * Note: this guarantees that vq callbacks are not in progress, however caller
217 * is responsible for preventing access from other contexts, such as a system
218 * call/workqueue/bh. Invoking virtio_break_device then flushing any such
219 * contexts is one way to handle that.
221 void virtio_reset_device(struct virtio_device *dev)
223 dev->config->reset(dev);
225 EXPORT_SYMBOL_GPL(virtio_reset_device);
227 static int virtio_dev_probe(struct device *_d)
230 struct virtio_device *dev = dev_to_virtio(_d);
231 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
234 u64 driver_features_legacy;
236 /* We have a driver! */
237 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
239 /* Figure out what features the device supports. */
240 device_features = dev->config->get_features(dev);
242 /* Figure out what features the driver supports. */
244 for (i = 0; i < drv->feature_table_size; i++) {
245 unsigned int f = drv->feature_table[i];
247 driver_features |= (1ULL << f);
250 /* Some drivers have a separate feature table for virtio v1.0 */
251 if (drv->feature_table_legacy) {
252 driver_features_legacy = 0;
253 for (i = 0; i < drv->feature_table_size_legacy; i++) {
254 unsigned int f = drv->feature_table_legacy[i];
256 driver_features_legacy |= (1ULL << f);
259 driver_features_legacy = driver_features;
262 if (device_features & (1ULL << VIRTIO_F_VERSION_1))
263 dev->features = driver_features & device_features;
265 dev->features = driver_features_legacy & device_features;
267 /* Transport features always preserved to pass to finalize_features. */
268 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
269 if (device_features & (1ULL << i))
270 __virtio_set_bit(dev, i);
272 err = dev->config->finalize_features(dev);
277 u64 features = dev->features;
279 err = drv->validate(dev);
283 /* Did validation change any features? Then write them again. */
284 if (features != dev->features) {
285 err = dev->config->finalize_features(dev);
291 err = virtio_features_ok(dev);
295 err = drv->probe(dev);
299 /* If probe didn't do it, mark device DRIVER_OK ourselves. */
300 if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
301 virtio_device_ready(dev);
306 virtio_config_enable(dev);
310 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
315 static void virtio_dev_remove(struct device *_d)
317 struct virtio_device *dev = dev_to_virtio(_d);
318 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
320 virtio_config_disable(dev);
324 /* Driver should have reset device. */
325 WARN_ON_ONCE(dev->config->get_status(dev));
327 /* Acknowledge the device's existence again. */
328 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
330 of_node_put(dev->dev.of_node);
333 static struct bus_type virtio_bus = {
335 .match = virtio_dev_match,
336 .dev_groups = virtio_dev_groups,
337 .uevent = virtio_uevent,
338 .probe = virtio_dev_probe,
339 .remove = virtio_dev_remove,
342 int register_virtio_driver(struct virtio_driver *driver)
344 /* Catch this early. */
345 BUG_ON(driver->feature_table_size && !driver->feature_table);
346 driver->driver.bus = &virtio_bus;
347 return driver_register(&driver->driver);
349 EXPORT_SYMBOL_GPL(register_virtio_driver);
351 void unregister_virtio_driver(struct virtio_driver *driver)
353 driver_unregister(&driver->driver);
355 EXPORT_SYMBOL_GPL(unregister_virtio_driver);
357 static int virtio_device_of_init(struct virtio_device *dev)
359 struct device_node *np, *pnode = dev_of_node(dev->dev.parent);
360 char compat[] = "virtio,deviceXXXXXXXX";
366 count = of_get_available_child_count(pnode);
370 /* There can be only 1 child node */
371 if (WARN_ON(count > 1))
374 np = of_get_next_available_child(pnode, NULL);
378 ret = snprintf(compat, sizeof(compat), "virtio,device%x", dev->id.device);
379 BUG_ON(ret >= sizeof(compat));
382 * On powerpc/pseries virtio devices are PCI devices so PCI
383 * vendor/device ids play the role of the "compatible" property.
384 * Simply don't init of_node in this case.
386 if (!of_device_is_compatible(np, compat)) {
391 dev->dev.of_node = np;
400 * register_virtio_device - register virtio device
401 * @dev : virtio device to be registered
403 * On error, the caller must call put_device on &@dev->dev (and not kfree),
404 * as another code path may have obtained a reference to @dev.
406 * Returns: 0 on suceess, -error on failure
408 int register_virtio_device(struct virtio_device *dev)
412 dev->dev.bus = &virtio_bus;
413 device_initialize(&dev->dev);
415 /* Assign a unique device index and hence name. */
416 err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
421 dev_set_name(&dev->dev, "virtio%u", dev->index);
423 err = virtio_device_of_init(dev);
427 spin_lock_init(&dev->config_lock);
428 dev->config_enabled = false;
429 dev->config_change_pending = false;
431 /* We always start by resetting the device, in case a previous
432 * driver messed it up. This also tests that code path a little. */
433 dev->config->reset(dev);
435 /* Acknowledge that we've seen the device. */
436 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
438 INIT_LIST_HEAD(&dev->vqs);
439 spin_lock_init(&dev->vqs_list_lock);
442 * device_add() causes the bus infrastructure to look for a matching
445 err = device_add(&dev->dev);
447 goto out_of_node_put;
452 of_node_put(dev->dev.of_node);
454 ida_simple_remove(&virtio_index_ida, dev->index);
456 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
459 EXPORT_SYMBOL_GPL(register_virtio_device);
461 bool is_virtio_device(struct device *dev)
463 return dev->bus == &virtio_bus;
465 EXPORT_SYMBOL_GPL(is_virtio_device);
467 void unregister_virtio_device(struct virtio_device *dev)
469 int index = dev->index; /* save for after device release */
471 device_unregister(&dev->dev);
472 ida_simple_remove(&virtio_index_ida, index);
474 EXPORT_SYMBOL_GPL(unregister_virtio_device);
476 #ifdef CONFIG_PM_SLEEP
477 int virtio_device_freeze(struct virtio_device *dev)
479 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
481 virtio_config_disable(dev);
483 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
485 if (drv && drv->freeze)
486 return drv->freeze(dev);
490 EXPORT_SYMBOL_GPL(virtio_device_freeze);
492 int virtio_device_restore(struct virtio_device *dev)
494 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
497 /* We always start by resetting the device, in case a previous
498 * driver messed it up. */
499 dev->config->reset(dev);
501 /* Acknowledge that we've seen the device. */
502 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
504 /* Maybe driver failed before freeze.
505 * Restore the failed status, for debugging. */
507 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
512 /* We have a driver! */
513 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
515 ret = dev->config->finalize_features(dev);
519 ret = virtio_features_ok(dev);
524 ret = drv->restore(dev);
529 /* Finally, tell the device we're all set */
530 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
532 virtio_config_enable(dev);
537 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
540 EXPORT_SYMBOL_GPL(virtio_device_restore);
543 static int virtio_init(void)
545 if (bus_register(&virtio_bus) != 0)
546 panic("virtio bus registration failed");
550 static void __exit virtio_exit(void)
552 bus_unregister(&virtio_bus);
553 ida_destroy(&virtio_index_ida);
555 core_initcall(virtio_init);
556 module_exit(virtio_exit);
558 MODULE_LICENSE("GPL");