virtio-uclass.c 8.8 KB

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