You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

virtio-pci.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /* virtio-pci.c - pci interface for virtio interface
  2. *
  3. * (c) Copyright 2008 Bull S.A.S.
  4. *
  5. * Author: Laurent Vivier <Laurent.Vivier@bull.net>
  6. *
  7. * some parts from Linux Virtio PCI driver
  8. *
  9. * Copyright IBM Corp. 2007
  10. * Authors: Anthony Liguori <aliguori@us.ibm.com>
  11. *
  12. */
  13. #include "errno.h"
  14. #include "byteswap.h"
  15. #include "etherboot.h"
  16. #include "ipxe/io.h"
  17. #include "ipxe/iomap.h"
  18. #include "ipxe/pci.h"
  19. #include "ipxe/reboot.h"
  20. #include "ipxe/virtio-pci.h"
  21. #include "ipxe/virtio-ring.h"
  22. static int vp_alloc_vq(struct vring_virtqueue *vq, u16 num)
  23. {
  24. size_t queue_size = PAGE_MASK + vring_size(num);
  25. size_t vdata_size = num * sizeof(void *);
  26. vq->queue = zalloc(queue_size + vdata_size);
  27. if (!vq->queue) {
  28. return -ENOMEM;
  29. }
  30. /* vdata immediately follows the ring */
  31. vq->vdata = (void **)(vq->queue + queue_size);
  32. return 0;
  33. }
  34. void vp_free_vq(struct vring_virtqueue *vq)
  35. {
  36. if (vq->queue) {
  37. free(vq->queue);
  38. vq->queue = NULL;
  39. vq->vdata = NULL;
  40. }
  41. }
  42. int vp_find_vq(unsigned int ioaddr, int queue_index,
  43. struct vring_virtqueue *vq)
  44. {
  45. struct vring * vr = &vq->vring;
  46. u16 num;
  47. int rc;
  48. /* select the queue */
  49. outw(queue_index, ioaddr + VIRTIO_PCI_QUEUE_SEL);
  50. /* check if the queue is available */
  51. num = inw(ioaddr + VIRTIO_PCI_QUEUE_NUM);
  52. if (!num) {
  53. DBG("VIRTIO-PCI ERROR: queue size is 0\n");
  54. return -1;
  55. }
  56. /* check if the queue is already active */
  57. if (inl(ioaddr + VIRTIO_PCI_QUEUE_PFN)) {
  58. DBG("VIRTIO-PCI ERROR: queue already active\n");
  59. return -1;
  60. }
  61. vq->queue_index = queue_index;
  62. /* initialize the queue */
  63. rc = vp_alloc_vq(vq, num);
  64. if (rc) {
  65. DBG("VIRTIO-PCI ERROR: failed to allocate queue memory\n");
  66. return rc;
  67. }
  68. vring_init(vr, num, vq->queue);
  69. /* activate the queue
  70. *
  71. * NOTE: vr->desc is initialized by vring_init()
  72. */
  73. outl((unsigned long)virt_to_phys(vr->desc) >> PAGE_SHIFT,
  74. ioaddr + VIRTIO_PCI_QUEUE_PFN);
  75. return num;
  76. }
  77. #define CFG_POS(vdev, field) \
  78. (vdev->cfg_cap_pos + offsetof(struct virtio_pci_cfg_cap, field))
  79. static void prep_pci_cfg_cap(struct virtio_pci_modern_device *vdev,
  80. struct virtio_pci_region *region,
  81. size_t offset, u32 length)
  82. {
  83. pci_write_config_byte(vdev->pci, CFG_POS(vdev, cap.bar), region->bar);
  84. pci_write_config_dword(vdev->pci, CFG_POS(vdev, cap.length), length);
  85. pci_write_config_dword(vdev->pci, CFG_POS(vdev, cap.offset),
  86. (intptr_t)(region->base + offset));
  87. }
  88. void vpm_iowrite8(struct virtio_pci_modern_device *vdev,
  89. struct virtio_pci_region *region, u8 data, size_t offset)
  90. {
  91. switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
  92. case VIRTIO_PCI_REGION_MEMORY:
  93. writeb(data, region->base + offset);
  94. break;
  95. case VIRTIO_PCI_REGION_PORT:
  96. outb(data, region->base + offset);
  97. break;
  98. case VIRTIO_PCI_REGION_PCI_CONFIG:
  99. prep_pci_cfg_cap(vdev, region, offset, 1);
  100. pci_write_config_byte(vdev->pci, CFG_POS(vdev, pci_cfg_data), data);
  101. break;
  102. default:
  103. assert(0);
  104. break;
  105. }
  106. }
  107. void vpm_iowrite16(struct virtio_pci_modern_device *vdev,
  108. struct virtio_pci_region *region, u16 data, size_t offset)
  109. {
  110. data = cpu_to_le16(data);
  111. switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
  112. case VIRTIO_PCI_REGION_MEMORY:
  113. writew(data, region->base + offset);
  114. break;
  115. case VIRTIO_PCI_REGION_PORT:
  116. outw(data, region->base + offset);
  117. break;
  118. case VIRTIO_PCI_REGION_PCI_CONFIG:
  119. prep_pci_cfg_cap(vdev, region, offset, 2);
  120. pci_write_config_word(vdev->pci, CFG_POS(vdev, pci_cfg_data), data);
  121. break;
  122. default:
  123. assert(0);
  124. break;
  125. }
  126. }
  127. void vpm_iowrite32(struct virtio_pci_modern_device *vdev,
  128. struct virtio_pci_region *region, u32 data, size_t offset)
  129. {
  130. data = cpu_to_le32(data);
  131. switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
  132. case VIRTIO_PCI_REGION_MEMORY:
  133. writel(data, region->base + offset);
  134. break;
  135. case VIRTIO_PCI_REGION_PORT:
  136. outl(data, region->base + offset);
  137. break;
  138. case VIRTIO_PCI_REGION_PCI_CONFIG:
  139. prep_pci_cfg_cap(vdev, region, offset, 4);
  140. pci_write_config_dword(vdev->pci, CFG_POS(vdev, pci_cfg_data), data);
  141. break;
  142. default:
  143. assert(0);
  144. break;
  145. }
  146. }
  147. u8 vpm_ioread8(struct virtio_pci_modern_device *vdev,
  148. struct virtio_pci_region *region, size_t offset)
  149. {
  150. uint8_t data;
  151. switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
  152. case VIRTIO_PCI_REGION_MEMORY:
  153. data = readb(region->base + offset);
  154. break;
  155. case VIRTIO_PCI_REGION_PORT:
  156. data = inb(region->base + offset);
  157. break;
  158. case VIRTIO_PCI_REGION_PCI_CONFIG:
  159. prep_pci_cfg_cap(vdev, region, offset, 1);
  160. pci_read_config_byte(vdev->pci, CFG_POS(vdev, pci_cfg_data), &data);
  161. break;
  162. default:
  163. assert(0);
  164. data = 0;
  165. break;
  166. }
  167. return data;
  168. }
  169. u16 vpm_ioread16(struct virtio_pci_modern_device *vdev,
  170. struct virtio_pci_region *region, size_t offset)
  171. {
  172. uint16_t data;
  173. switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
  174. case VIRTIO_PCI_REGION_MEMORY:
  175. data = readw(region->base + offset);
  176. break;
  177. case VIRTIO_PCI_REGION_PORT:
  178. data = inw(region->base + offset);
  179. break;
  180. case VIRTIO_PCI_REGION_PCI_CONFIG:
  181. prep_pci_cfg_cap(vdev, region, offset, 2);
  182. pci_read_config_word(vdev->pci, CFG_POS(vdev, pci_cfg_data), &data);
  183. break;
  184. default:
  185. assert(0);
  186. data = 0;
  187. break;
  188. }
  189. return le16_to_cpu(data);
  190. }
  191. u32 vpm_ioread32(struct virtio_pci_modern_device *vdev,
  192. struct virtio_pci_region *region, size_t offset)
  193. {
  194. uint32_t data;
  195. switch (region->flags & VIRTIO_PCI_REGION_TYPE_MASK) {
  196. case VIRTIO_PCI_REGION_MEMORY:
  197. data = readw(region->base + offset);
  198. break;
  199. case VIRTIO_PCI_REGION_PORT:
  200. data = inw(region->base + offset);
  201. break;
  202. case VIRTIO_PCI_REGION_PCI_CONFIG:
  203. prep_pci_cfg_cap(vdev, region, offset, 4);
  204. pci_read_config_dword(vdev->pci, CFG_POS(vdev, pci_cfg_data), &data);
  205. break;
  206. default:
  207. assert(0);
  208. data = 0;
  209. break;
  210. }
  211. return le32_to_cpu(data);
  212. }
  213. int virtio_pci_find_capability(struct pci_device *pci, uint8_t cfg_type)
  214. {
  215. int pos;
  216. uint8_t type, bar;
  217. for (pos = pci_find_capability(pci, PCI_CAP_ID_VNDR);
  218. pos > 0;
  219. pos = pci_find_next_capability(pci, pos, PCI_CAP_ID_VNDR)) {
  220. pci_read_config_byte(pci, pos + offsetof(struct virtio_pci_cap,
  221. cfg_type), &type);
  222. pci_read_config_byte(pci, pos + offsetof(struct virtio_pci_cap,
  223. bar), &bar);
  224. /* Ignore structures with reserved BAR values */
  225. if (bar > 0x5) {
  226. continue;
  227. }
  228. if (type == cfg_type) {
  229. return pos;
  230. }
  231. }
  232. return 0;
  233. }
  234. int virtio_pci_map_capability(struct pci_device *pci, int cap, size_t minlen,
  235. u32 align, u32 start, u32 size,
  236. struct virtio_pci_region *region)
  237. {
  238. u8 bar;
  239. u32 offset, length, base_raw;
  240. unsigned long base;
  241. pci_read_config_byte(pci, cap + offsetof(struct virtio_pci_cap, bar), &bar);
  242. pci_read_config_dword(pci, cap + offsetof(struct virtio_pci_cap, offset),
  243. &offset);
  244. pci_read_config_dword(pci, cap + offsetof(struct virtio_pci_cap, length),
  245. &length);
  246. if (length <= start) {
  247. DBG("VIRTIO-PCI bad capability len %d (>%d expected)\n", length, start);
  248. return -EINVAL;
  249. }
  250. if (length - start < minlen) {
  251. DBG("VIRTIO-PCI bad capability len %d (>=%zd expected)\n", length, minlen);
  252. return -EINVAL;
  253. }
  254. length -= start;
  255. if (start + offset < offset) {
  256. DBG("VIRTIO-PCI map wrap-around %d+%d\n", start, offset);
  257. return -EINVAL;
  258. }
  259. offset += start;
  260. if (offset & (align - 1)) {
  261. DBG("VIRTIO-PCI offset %d not aligned to %d\n", offset, align);
  262. return -EINVAL;
  263. }
  264. if (length > size) {
  265. length = size;
  266. }
  267. if (minlen + offset < minlen ||
  268. minlen + offset > pci_bar_size(pci, PCI_BASE_ADDRESS(bar))) {
  269. DBG("VIRTIO-PCI map virtio %zd@%d out of range on bar %i length %ld\n",
  270. minlen, offset,
  271. bar, pci_bar_size(pci, PCI_BASE_ADDRESS(bar)));
  272. return -EINVAL;
  273. }
  274. region->base = NULL;
  275. region->length = length;
  276. region->bar = bar;
  277. base = pci_bar_start(pci, PCI_BASE_ADDRESS(bar));
  278. if (base) {
  279. pci_read_config_dword(pci, PCI_BASE_ADDRESS(bar), &base_raw);
  280. if (base_raw & PCI_BASE_ADDRESS_SPACE_IO) {
  281. /* Region accessed using port I/O */
  282. region->base = (void *)(base + offset);
  283. region->flags = VIRTIO_PCI_REGION_PORT;
  284. } else {
  285. /* Region mapped into memory space */
  286. region->base = ioremap(base + offset, length);
  287. region->flags = VIRTIO_PCI_REGION_MEMORY;
  288. }
  289. }
  290. if (!region->base) {
  291. /* Region accessed via PCI config space window */
  292. region->base = (void *)(intptr_t)offset;
  293. region->flags = VIRTIO_PCI_REGION_PCI_CONFIG;
  294. }
  295. return 0;
  296. }
  297. void virtio_pci_unmap_capability(struct virtio_pci_region *region)
  298. {
  299. unsigned region_type = region->flags & VIRTIO_PCI_REGION_TYPE_MASK;
  300. if (region_type == VIRTIO_PCI_REGION_MEMORY) {
  301. iounmap(region->base);
  302. }
  303. }
  304. void vpm_notify(struct virtio_pci_modern_device *vdev,
  305. struct vring_virtqueue *vq)
  306. {
  307. vpm_iowrite16(vdev, &vq->notification, (u16)vq->queue_index, 0);
  308. }
  309. int vpm_find_vqs(struct virtio_pci_modern_device *vdev,
  310. unsigned nvqs, struct vring_virtqueue *vqs)
  311. {
  312. unsigned i;
  313. struct vring_virtqueue *vq;
  314. u16 size, off;
  315. u32 notify_offset_multiplier;
  316. int err;
  317. if (nvqs > vpm_ioread16(vdev, &vdev->common, COMMON_OFFSET(num_queues))) {
  318. return -ENOENT;
  319. }
  320. /* Read notify_off_multiplier from config space. */
  321. pci_read_config_dword(vdev->pci,
  322. vdev->notify_cap_pos + offsetof(struct virtio_pci_notify_cap,
  323. notify_off_multiplier),
  324. &notify_offset_multiplier);
  325. for (i = 0; i < nvqs; i++) {
  326. /* Select the queue we're interested in */
  327. vpm_iowrite16(vdev, &vdev->common, (u16)i, COMMON_OFFSET(queue_select));
  328. /* Check if queue is either not available or already active. */
  329. size = vpm_ioread16(vdev, &vdev->common, COMMON_OFFSET(queue_size));
  330. /* QEMU has a bug where queues don't revert to inactive on device
  331. * reset. Skip checking the queue_enable field until it is fixed.
  332. */
  333. if (!size /*|| vpm_ioread16(vdev, &vdev->common.queue_enable)*/)
  334. return -ENOENT;
  335. if (size & (size - 1)) {
  336. DBG("VIRTIO-PCI %p: bad queue size %d\n", vdev, size);
  337. return -EINVAL;
  338. }
  339. if (size > MAX_QUEUE_NUM) {
  340. /* iPXE networking tends to be not perf critical so there's no
  341. * need to accept large queue sizes.
  342. */
  343. size = MAX_QUEUE_NUM;
  344. }
  345. vq = &vqs[i];
  346. vq->queue_index = i;
  347. /* get offset of notification word for this vq */
  348. off = vpm_ioread16(vdev, &vdev->common, COMMON_OFFSET(queue_notify_off));
  349. err = vp_alloc_vq(vq, size);
  350. if (err) {
  351. DBG("VIRTIO-PCI %p: failed to allocate queue memory\n", vdev);
  352. return err;
  353. }
  354. vring_init(&vq->vring, size, vq->queue);
  355. /* activate the queue */
  356. vpm_iowrite16(vdev, &vdev->common, size, COMMON_OFFSET(queue_size));
  357. vpm_iowrite64(vdev, &vdev->common, virt_to_phys(vq->vring.desc),
  358. COMMON_OFFSET(queue_desc_lo),
  359. COMMON_OFFSET(queue_desc_hi));
  360. vpm_iowrite64(vdev, &vdev->common, virt_to_phys(vq->vring.avail),
  361. COMMON_OFFSET(queue_avail_lo),
  362. COMMON_OFFSET(queue_avail_hi));
  363. vpm_iowrite64(vdev, &vdev->common, virt_to_phys(vq->vring.used),
  364. COMMON_OFFSET(queue_used_lo),
  365. COMMON_OFFSET(queue_used_hi));
  366. err = virtio_pci_map_capability(vdev->pci,
  367. vdev->notify_cap_pos, 2, 2,
  368. off * notify_offset_multiplier, 2,
  369. &vq->notification);
  370. if (err) {
  371. return err;
  372. }
  373. }
  374. /* Select and activate all queues. Has to be done last: once we do
  375. * this, there's no way to go back except reset.
  376. */
  377. for (i = 0; i < nvqs; i++) {
  378. vq = &vqs[i];
  379. vpm_iowrite16(vdev, &vdev->common, (u16)vq->queue_index,
  380. COMMON_OFFSET(queue_select));
  381. vpm_iowrite16(vdev, &vdev->common, 1, COMMON_OFFSET(queue_enable));
  382. }
  383. return 0;
  384. }