virtio-uclass.c 8.8 KB

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