virtio-uclass.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
  4. * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
  5. *
  6. * VirtIO is a virtualization standard for network and disk device drivers
  7. * where just the guest's device driver "knows" it is running in a virtual
  8. * environment, and cooperates with the hypervisor. This enables guests to
  9. * get high performance network and disk operations, and gives most of the
  10. * performance benefits of paravirtualization. In the U-Boot case, the guest
  11. * is U-Boot itself, while the virtual environment are normally QEMU targets
  12. * like ARM, RISC-V and x86.
  13. *
  14. * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for
  15. * the VirtIO specification v1.0.
  16. */
  17. #include <common.h>
  18. #include <dm.h>
  19. #include <virtio_types.h>
  20. #include <virtio.h>
  21. #include <dm/lists.h>
  22. static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = {
  23. [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME,
  24. [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME,
  25. };
  26. int virtio_get_config(struct udevice *vdev, unsigned int offset,
  27. void *buf, unsigned int len)
  28. {
  29. struct dm_virtio_ops *ops;
  30. ops = virtio_get_ops(vdev->parent);
  31. return ops->get_config(vdev->parent, offset, buf, len);
  32. }
  33. int virtio_set_config(struct udevice *vdev, unsigned int offset,
  34. void *buf, unsigned int len)
  35. {
  36. struct dm_virtio_ops *ops;
  37. ops = virtio_get_ops(vdev->parent);
  38. return ops->set_config(vdev->parent, offset, buf, len);
  39. }
  40. int virtio_generation(struct udevice *vdev, u32 *counter)
  41. {
  42. struct dm_virtio_ops *ops;
  43. ops = virtio_get_ops(vdev->parent);
  44. if (!ops->generation)
  45. return -ENOSYS;
  46. return ops->generation(vdev->parent, counter);
  47. }
  48. int virtio_get_status(struct udevice *vdev, u8 *status)
  49. {
  50. struct dm_virtio_ops *ops;
  51. ops = virtio_get_ops(vdev->parent);
  52. return ops->get_status(vdev->parent, status);
  53. }
  54. int virtio_set_status(struct udevice *vdev, u8 status)
  55. {
  56. struct dm_virtio_ops *ops;
  57. ops = virtio_get_ops(vdev->parent);
  58. return ops->set_status(vdev->parent, status);
  59. }
  60. int virtio_reset(struct udevice *vdev)
  61. {
  62. struct dm_virtio_ops *ops;
  63. ops = virtio_get_ops(vdev->parent);
  64. return ops->reset(vdev->parent);
  65. }
  66. int virtio_get_features(struct udevice *vdev, u64 *features)
  67. {
  68. struct dm_virtio_ops *ops;
  69. ops = virtio_get_ops(vdev->parent);
  70. return ops->get_features(vdev->parent, features);
  71. }
  72. int virtio_set_features(struct udevice *vdev)
  73. {
  74. struct dm_virtio_ops *ops;
  75. ops = virtio_get_ops(vdev->parent);
  76. return ops->set_features(vdev->parent);
  77. }
  78. int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
  79. struct virtqueue *vqs[])
  80. {
  81. struct dm_virtio_ops *ops;
  82. ops = virtio_get_ops(vdev->parent);
  83. return ops->find_vqs(vdev->parent, nvqs, vqs);
  84. }
  85. int virtio_del_vqs(struct udevice *vdev)
  86. {
  87. struct dm_virtio_ops *ops;
  88. ops = virtio_get_ops(vdev->parent);
  89. return ops->del_vqs(vdev->parent);
  90. }
  91. int virtio_notify(struct udevice *vdev, struct virtqueue *vq)
  92. {
  93. struct dm_virtio_ops *ops;
  94. ops = virtio_get_ops(vdev->parent);
  95. return ops->notify(vdev->parent, vq);
  96. }
  97. void virtio_add_status(struct udevice *vdev, u8 status)
  98. {
  99. u8 old;
  100. if (!virtio_get_status(vdev, &old))
  101. virtio_set_status(vdev, old | status);
  102. }
  103. int virtio_finalize_features(struct udevice *vdev)
  104. {
  105. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
  106. u8 status;
  107. int ret;
  108. ret = virtio_set_features(vdev);
  109. if (ret)
  110. return ret;
  111. if (uc_priv->legacy)
  112. return 0;
  113. virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK);
  114. ret = virtio_get_status(vdev, &status);
  115. if (ret)
  116. return ret;
  117. if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
  118. debug("(%s): device refuses features %x\n", vdev->name, status);
  119. return -ENODEV;
  120. }
  121. return 0;
  122. }
  123. void virtio_driver_features_init(struct virtio_dev_priv *priv,
  124. const u32 *feature,
  125. u32 feature_size,
  126. const u32 *feature_legacy,
  127. u32 feature_legacy_size)
  128. {
  129. priv->feature_table = feature;
  130. priv->feature_table_size = feature_size;
  131. priv->feature_table_legacy = feature_legacy;
  132. priv->feature_table_size_legacy = feature_legacy_size;
  133. }
  134. int virtio_init(void)
  135. {
  136. struct udevice *bus;
  137. int ret;
  138. /* Enumerate all known virtio devices */
  139. ret = uclass_first_device(UCLASS_VIRTIO, &bus);
  140. if (ret)
  141. return ret;
  142. while (bus) {
  143. ret = uclass_next_device(&bus);
  144. if (ret)
  145. break;
  146. }
  147. return ret;
  148. }
  149. static int virtio_uclass_pre_probe(struct udevice *udev)
  150. {
  151. struct dm_virtio_ops *ops;
  152. ops = (struct dm_virtio_ops *)(udev->driver->ops);
  153. /*
  154. * Check virtio transport driver ops here so that we don't need
  155. * check these ops each time when the virtio_xxx APIs are called.
  156. *
  157. * Only generation op is optional. All other ops are must-have.
  158. */
  159. if (!ops->get_config || !ops->set_config ||
  160. !ops->get_status || !ops->set_status ||
  161. !ops->get_features || !ops->set_features ||
  162. !ops->find_vqs || !ops->del_vqs ||
  163. !ops->reset || !ops->notify)
  164. return -ENOENT;
  165. return 0;
  166. }
  167. static int virtio_uclass_post_probe(struct udevice *udev)
  168. {
  169. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
  170. char dev_name[30], *str;
  171. struct udevice *vdev;
  172. int ret;
  173. if (uc_priv->device > VIRTIO_ID_MAX_NUM) {
  174. debug("(%s): virtio device ID %d exceeds maximum num\n",
  175. udev->name, uc_priv->device);
  176. return 0;
  177. }
  178. if (!virtio_drv_name[uc_priv->device]) {
  179. debug("(%s): underlying virtio device driver unavailable\n",
  180. udev->name);
  181. return 0;
  182. }
  183. snprintf(dev_name, sizeof(dev_name), "%s#%d",
  184. virtio_drv_name[uc_priv->device], udev->seq);
  185. str = strdup(dev_name);
  186. if (!str)
  187. return -ENOMEM;
  188. ret = device_bind_driver(udev, virtio_drv_name[uc_priv->device],
  189. str, &vdev);
  190. if (ret == -ENOENT) {
  191. debug("(%s): no driver configured\n", udev->name);
  192. return 0;
  193. }
  194. if (ret) {
  195. free(str);
  196. return ret;
  197. }
  198. device_set_name_alloced(vdev);
  199. INIT_LIST_HEAD(&uc_priv->vqs);
  200. return 0;
  201. }
  202. static int virtio_uclass_child_post_bind(struct udevice *vdev)
  203. {
  204. /* Acknowledge that we've seen the device */
  205. virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  206. return 0;
  207. }
  208. static int virtio_uclass_child_pre_probe(struct udevice *vdev)
  209. {
  210. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
  211. u64 device_features;
  212. u64 driver_features;
  213. u64 driver_features_legacy;
  214. int i;
  215. int ret;
  216. /*
  217. * Save the real virtio device (eg: virtio-net, virtio-blk) to
  218. * the transport (parent) device's uclass priv for future use.
  219. */
  220. uc_priv->vdev = vdev;
  221. /*
  222. * We always start by resetting the device, in case a previous driver
  223. * messed it up. This also tests that code path a little.
  224. */
  225. ret = virtio_reset(vdev);
  226. if (ret)
  227. goto err;
  228. /* We have a driver! */
  229. virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER);
  230. /* Figure out what features the device supports */
  231. virtio_get_features(vdev, &device_features);
  232. debug("(%s) plain device features supported %016llx\n",
  233. vdev->name, device_features);
  234. if (!(device_features & (1ULL << VIRTIO_F_VERSION_1)))
  235. uc_priv->legacy = true;
  236. /* Figure out what features the driver supports */
  237. driver_features = 0;
  238. for (i = 0; i < uc_priv->feature_table_size; i++) {
  239. unsigned int f = uc_priv->feature_table[i];
  240. WARN_ON(f >= 64);
  241. driver_features |= (1ULL << f);
  242. }
  243. /* Some drivers have a separate feature table for virtio v1.0 */
  244. if (uc_priv->feature_table_legacy) {
  245. driver_features_legacy = 0;
  246. for (i = 0; i < uc_priv->feature_table_size_legacy; i++) {
  247. unsigned int f = uc_priv->feature_table_legacy[i];
  248. WARN_ON(f >= 64);
  249. driver_features_legacy |= (1ULL << f);
  250. }
  251. } else {
  252. driver_features_legacy = driver_features;
  253. }
  254. if (uc_priv->legacy) {
  255. debug("(%s): legacy virtio device\n", vdev->name);
  256. uc_priv->features = driver_features_legacy & device_features;
  257. } else {
  258. debug("(%s): v1.0 complaint virtio device\n", vdev->name);
  259. uc_priv->features = driver_features & device_features;
  260. }
  261. /* Transport features always preserved to pass to finalize_features */
  262. for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
  263. if ((device_features & (1ULL << i)) &&
  264. (i == VIRTIO_F_VERSION_1))
  265. __virtio_set_bit(vdev->parent, i);
  266. debug("(%s) final negotiated features supported %016llx\n",
  267. vdev->name, uc_priv->features);
  268. ret = virtio_finalize_features(vdev);
  269. if (ret)
  270. goto err;
  271. return 0;
  272. err:
  273. virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED);
  274. return ret;
  275. }
  276. static int virtio_uclass_child_post_probe(struct udevice *vdev)
  277. {
  278. /* Indicates that the driver is set up and ready to drive the device */
  279. virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK);
  280. return 0;
  281. }
  282. UCLASS_DRIVER(virtio) = {
  283. .name = "virtio",
  284. .id = UCLASS_VIRTIO,
  285. .flags = DM_UC_FLAG_SEQ_ALIAS,
  286. .pre_probe = virtio_uclass_pre_probe,
  287. .post_probe = virtio_uclass_post_probe,
  288. .child_post_bind = virtio_uclass_child_post_bind,
  289. .child_pre_probe = virtio_uclass_child_pre_probe,
  290. .child_post_probe = virtio_uclass_child_post_probe,
  291. .per_device_auto_alloc_size = sizeof(struct virtio_dev_priv),
  292. };