virtio-uclass.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. #define LOG_CATEGORY UCLASS_VIRTIO
  18. #include <common.h>
  19. #include <bootdev.h>
  20. #include <dm.h>
  21. #include <log.h>
  22. #include <malloc.h>
  23. #include <virtio_types.h>
  24. #include <virtio.h>
  25. #include <dm/lists.h>
  26. #include <linux/bug.h>
  27. static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = {
  28. [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME,
  29. [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME,
  30. [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME,
  31. };
  32. int virtio_get_config(struct udevice *vdev, unsigned int offset,
  33. void *buf, unsigned int len)
  34. {
  35. struct dm_virtio_ops *ops;
  36. ops = virtio_get_ops(vdev->parent);
  37. return ops->get_config(vdev->parent, offset, buf, len);
  38. }
  39. int virtio_set_config(struct udevice *vdev, unsigned int offset,
  40. void *buf, unsigned int len)
  41. {
  42. struct dm_virtio_ops *ops;
  43. ops = virtio_get_ops(vdev->parent);
  44. return ops->set_config(vdev->parent, offset, buf, len);
  45. }
  46. int virtio_generation(struct udevice *vdev, u32 *counter)
  47. {
  48. struct dm_virtio_ops *ops;
  49. ops = virtio_get_ops(vdev->parent);
  50. if (!ops->generation)
  51. return -ENOSYS;
  52. return ops->generation(vdev->parent, counter);
  53. }
  54. int virtio_get_status(struct udevice *vdev, u8 *status)
  55. {
  56. struct dm_virtio_ops *ops;
  57. ops = virtio_get_ops(vdev->parent);
  58. return ops->get_status(vdev->parent, status);
  59. }
  60. int virtio_set_status(struct udevice *vdev, u8 status)
  61. {
  62. struct dm_virtio_ops *ops;
  63. ops = virtio_get_ops(vdev->parent);
  64. return ops->set_status(vdev->parent, status);
  65. }
  66. int virtio_reset(struct udevice *vdev)
  67. {
  68. struct dm_virtio_ops *ops;
  69. ops = virtio_get_ops(vdev->parent);
  70. return ops->reset(vdev->parent);
  71. }
  72. int virtio_get_features(struct udevice *vdev, u64 *features)
  73. {
  74. struct dm_virtio_ops *ops;
  75. ops = virtio_get_ops(vdev->parent);
  76. return ops->get_features(vdev->parent, features);
  77. }
  78. int virtio_set_features(struct udevice *vdev)
  79. {
  80. struct dm_virtio_ops *ops;
  81. ops = virtio_get_ops(vdev->parent);
  82. return ops->set_features(vdev->parent);
  83. }
  84. int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
  85. struct virtqueue *vqs[])
  86. {
  87. struct dm_virtio_ops *ops;
  88. ops = virtio_get_ops(vdev->parent);
  89. return ops->find_vqs(vdev->parent, nvqs, vqs);
  90. }
  91. int virtio_del_vqs(struct udevice *vdev)
  92. {
  93. struct dm_virtio_ops *ops;
  94. ops = virtio_get_ops(vdev->parent);
  95. return ops->del_vqs(vdev->parent);
  96. }
  97. int virtio_notify(struct udevice *vdev, struct virtqueue *vq)
  98. {
  99. struct dm_virtio_ops *ops;
  100. ops = virtio_get_ops(vdev->parent);
  101. return ops->notify(vdev->parent, vq);
  102. }
  103. void virtio_add_status(struct udevice *vdev, u8 status)
  104. {
  105. u8 old;
  106. if (!virtio_get_status(vdev, &old))
  107. virtio_set_status(vdev, old | status);
  108. }
  109. int virtio_finalize_features(struct udevice *vdev)
  110. {
  111. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
  112. u8 status;
  113. int ret;
  114. ret = virtio_set_features(vdev);
  115. if (ret)
  116. return ret;
  117. if (uc_priv->legacy)
  118. return 0;
  119. virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK);
  120. ret = virtio_get_status(vdev, &status);
  121. if (ret)
  122. return ret;
  123. if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
  124. debug("(%s): device refuses features %x\n", vdev->name, status);
  125. return -EINVAL;
  126. }
  127. return 0;
  128. }
  129. void virtio_driver_features_init(struct virtio_dev_priv *priv,
  130. const u32 *feature,
  131. u32 feature_size,
  132. const u32 *feature_legacy,
  133. u32 feature_legacy_size)
  134. {
  135. priv->feature_table = feature;
  136. priv->feature_table_size = feature_size;
  137. priv->feature_table_legacy = feature_legacy;
  138. priv->feature_table_size_legacy = feature_legacy_size;
  139. }
  140. int virtio_init(void)
  141. {
  142. /* Enumerate all known virtio devices */
  143. return uclass_probe_all(UCLASS_VIRTIO);
  144. }
  145. static int virtio_uclass_pre_probe(struct udevice *udev)
  146. {
  147. struct dm_virtio_ops *ops;
  148. ops = (struct dm_virtio_ops *)(udev->driver->ops);
  149. /*
  150. * Check virtio transport driver ops here so that we don't need
  151. * check these ops each time when the virtio_xxx APIs are called.
  152. *
  153. * Only generation op is optional. All other ops are must-have.
  154. */
  155. if (!ops->get_config || !ops->set_config ||
  156. !ops->get_status || !ops->set_status ||
  157. !ops->get_features || !ops->set_features ||
  158. !ops->find_vqs || !ops->del_vqs ||
  159. !ops->reset || !ops->notify)
  160. return -ENOENT;
  161. return 0;
  162. }
  163. static int virtio_uclass_post_probe(struct udevice *udev)
  164. {
  165. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
  166. char dev_name[30], *str;
  167. struct udevice *vdev;
  168. const char *name;
  169. int ret;
  170. if (uc_priv->device >= VIRTIO_ID_MAX_NUM) {
  171. debug("(%s): virtio device ID %d exceeds maximum num\n",
  172. udev->name, uc_priv->device);
  173. return 0;
  174. }
  175. name = virtio_drv_name[uc_priv->device];
  176. if (!name) {
  177. debug("(%s): underlying virtio device driver unavailable\n",
  178. udev->name);
  179. return 0;
  180. }
  181. snprintf(dev_name, sizeof(dev_name), "%s#%d", name, dev_seq(udev));
  182. str = strdup(dev_name);
  183. if (!str)
  184. return -ENOMEM;
  185. ret = device_bind_driver(udev, name, str, &vdev);
  186. if (ret == -ENOENT) {
  187. debug("(%s): %s driver not configured\n", udev->name, name);
  188. return 0;
  189. }
  190. if (ret) {
  191. free(str);
  192. return ret;
  193. }
  194. device_set_name_alloced(vdev);
  195. if (uc_priv->device == VIRTIO_ID_BLOCK && !IS_ENABLED(CONFIG_SANDBOX)) {
  196. ret = bootdev_setup_for_sibling_blk(vdev, "virtio_bootdev");
  197. if (ret)
  198. return log_msg_ret("bootdev", ret);
  199. }
  200. INIT_LIST_HEAD(&uc_priv->vqs);
  201. return 0;
  202. }
  203. static int virtio_uclass_child_post_bind(struct udevice *vdev)
  204. {
  205. /* Acknowledge that we've seen the device */
  206. virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
  207. return 0;
  208. }
  209. static int virtio_uclass_child_pre_probe(struct udevice *vdev)
  210. {
  211. struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
  212. u64 device_features;
  213. u64 driver_features;
  214. u64 driver_features_legacy;
  215. int i;
  216. int ret;
  217. /* bootdevs are not virtio devices */
  218. if (device_get_uclass_id(vdev) == UCLASS_BOOTDEV)
  219. return 0;
  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 || i == VIRTIO_F_IOMMU_PLATFORM))
  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. static int virtio_bootdev_bind(struct udevice *dev)
  287. {
  288. struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);
  289. ucp->prio = BOOTDEVP_4_SCAN_FAST;
  290. return 0;
  291. }
  292. static int virtio_bootdev_hunt(struct bootdev_hunter *info, bool show)
  293. {
  294. int ret;
  295. if (IS_ENABLED(CONFIG_PCI)) {
  296. ret = uclass_probe_all(UCLASS_PCI);
  297. if (ret && ret != -ENOENT)
  298. return log_msg_ret("pci", ret);
  299. }
  300. ret = uclass_probe_all(UCLASS_VIRTIO);
  301. if (ret && ret != -ENOENT)
  302. return log_msg_ret("vir", ret);
  303. return 0;
  304. }
  305. UCLASS_DRIVER(virtio) = {
  306. .name = "virtio",
  307. .id = UCLASS_VIRTIO,
  308. .flags = DM_UC_FLAG_SEQ_ALIAS,
  309. .pre_probe = virtio_uclass_pre_probe,
  310. .post_probe = virtio_uclass_post_probe,
  311. .child_post_bind = virtio_uclass_child_post_bind,
  312. .child_pre_probe = virtio_uclass_child_pre_probe,
  313. .child_post_probe = virtio_uclass_child_post_probe,
  314. .per_device_auto = sizeof(struct virtio_dev_priv),
  315. };
  316. struct bootdev_ops virtio_bootdev_ops = {
  317. };
  318. static const struct udevice_id virtio_bootdev_ids[] = {
  319. { .compatible = "u-boot,bootdev-virtio" },
  320. { }
  321. };
  322. U_BOOT_DRIVER(virtio_bootdev) = {
  323. .name = "virtio_bootdev",
  324. .id = UCLASS_BOOTDEV,
  325. .ops = &virtio_bootdev_ops,
  326. .bind = virtio_bootdev_bind,
  327. .of_match = virtio_bootdev_ids,
  328. };
  329. BOOTDEV_HUNTER(virtio_bootdev_hunter) = {
  330. .prio = BOOTDEVP_4_SCAN_FAST,
  331. .uclass = UCLASS_VIRTIO,
  332. .hunt = virtio_bootdev_hunt,
  333. .drv = DM_DRIVER_REF(virtio_bootdev),
  334. };