vfio-helpers.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * VFIO utility
  3. *
  4. * Copyright 2016 - 2018 Red Hat, Inc.
  5. *
  6. * Authors:
  7. * Fam Zheng <famz@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include <sys/ioctl.h>
  14. #include <linux/vfio.h>
  15. #include "qapi/error.h"
  16. #include "exec/ramlist.h"
  17. #include "exec/cpu-common.h"
  18. #include "trace.h"
  19. #include "qemu/error-report.h"
  20. #include "standard-headers/linux/pci_regs.h"
  21. #include "qemu/event_notifier.h"
  22. #include "qemu/vfio-helpers.h"
  23. #include "trace.h"
  24. #define QEMU_VFIO_DEBUG 0
  25. #define QEMU_VFIO_IOVA_MIN 0x10000ULL
  26. /* XXX: Once VFIO exposes the iova bit width in the IOMMU capability interface,
  27. * we can use a runtime limit; alternatively it's also possible to do platform
  28. * specific detection by reading sysfs entries. Until then, 39 is a safe bet.
  29. **/
  30. #define QEMU_VFIO_IOVA_MAX (1ULL << 39)
  31. typedef struct {
  32. /* Page aligned addr. */
  33. void *host;
  34. size_t size;
  35. uint64_t iova;
  36. } IOVAMapping;
  37. struct QEMUVFIOState {
  38. QemuMutex lock;
  39. /* These fields are protected by BQL */
  40. int container;
  41. int group;
  42. int device;
  43. RAMBlockNotifier ram_notifier;
  44. struct vfio_region_info config_region_info, bar_region_info[6];
  45. /* These fields are protected by @lock */
  46. /* VFIO's IO virtual address space is managed by splitting into a few
  47. * sections:
  48. *
  49. * --------------- <= 0
  50. * |xxxxxxxxxxxxx|
  51. * |-------------| <= QEMU_VFIO_IOVA_MIN
  52. * | |
  53. * | Fixed |
  54. * | |
  55. * |-------------| <= low_water_mark
  56. * | |
  57. * | Free |
  58. * | |
  59. * |-------------| <= high_water_mark
  60. * | |
  61. * | Temp |
  62. * | |
  63. * |-------------| <= QEMU_VFIO_IOVA_MAX
  64. * |xxxxxxxxxxxxx|
  65. * |xxxxxxxxxxxxx|
  66. * ---------------
  67. *
  68. * - Addresses lower than QEMU_VFIO_IOVA_MIN are reserved as invalid;
  69. *
  70. * - Fixed mappings of HVAs are assigned "low" IOVAs in the range of
  71. * [QEMU_VFIO_IOVA_MIN, low_water_mark). Once allocated they will not be
  72. * reclaimed - low_water_mark never shrinks;
  73. *
  74. * - IOVAs in range [low_water_mark, high_water_mark) are free;
  75. *
  76. * - IOVAs in range [high_water_mark, QEMU_VFIO_IOVA_MAX) are volatile
  77. * mappings. At each qemu_vfio_dma_reset_temporary() call, the whole area
  78. * is recycled. The caller should make sure I/O's depending on these
  79. * mappings are completed before calling.
  80. **/
  81. uint64_t low_water_mark;
  82. uint64_t high_water_mark;
  83. IOVAMapping *mappings;
  84. int nr_mappings;
  85. };
  86. /**
  87. * Find group file by PCI device address as specified @device, and return the
  88. * path. The returned string is owned by caller and should be g_free'ed later.
  89. */
  90. static char *sysfs_find_group_file(const char *device, Error **errp)
  91. {
  92. char *sysfs_link;
  93. char *sysfs_group;
  94. char *p;
  95. char *path = NULL;
  96. sysfs_link = g_strdup_printf("/sys/bus/pci/devices/%s/iommu_group", device);
  97. sysfs_group = g_malloc0(PATH_MAX);
  98. if (readlink(sysfs_link, sysfs_group, PATH_MAX - 1) == -1) {
  99. error_setg_errno(errp, errno, "Failed to find iommu group sysfs path");
  100. goto out;
  101. }
  102. p = strrchr(sysfs_group, '/');
  103. if (!p) {
  104. error_setg(errp, "Failed to find iommu group number");
  105. goto out;
  106. }
  107. path = g_strdup_printf("/dev/vfio/%s", p + 1);
  108. out:
  109. g_free(sysfs_link);
  110. g_free(sysfs_group);
  111. return path;
  112. }
  113. static inline void assert_bar_index_valid(QEMUVFIOState *s, int index)
  114. {
  115. assert(index >= 0 && index < ARRAY_SIZE(s->bar_region_info));
  116. }
  117. static int qemu_vfio_pci_init_bar(QEMUVFIOState *s, int index, Error **errp)
  118. {
  119. assert_bar_index_valid(s, index);
  120. s->bar_region_info[index] = (struct vfio_region_info) {
  121. .index = VFIO_PCI_BAR0_REGION_INDEX + index,
  122. .argsz = sizeof(struct vfio_region_info),
  123. };
  124. if (ioctl(s->device, VFIO_DEVICE_GET_REGION_INFO, &s->bar_region_info[index])) {
  125. error_setg_errno(errp, errno, "Failed to get BAR region info");
  126. return -errno;
  127. }
  128. return 0;
  129. }
  130. /**
  131. * Map a PCI bar area.
  132. */
  133. void *qemu_vfio_pci_map_bar(QEMUVFIOState *s, int index,
  134. uint64_t offset, uint64_t size,
  135. Error **errp)
  136. {
  137. void *p;
  138. assert_bar_index_valid(s, index);
  139. p = mmap(NULL, MIN(size, s->bar_region_info[index].size - offset),
  140. PROT_READ | PROT_WRITE, MAP_SHARED,
  141. s->device, s->bar_region_info[index].offset + offset);
  142. if (p == MAP_FAILED) {
  143. error_setg_errno(errp, errno, "Failed to map BAR region");
  144. p = NULL;
  145. }
  146. return p;
  147. }
  148. /**
  149. * Unmap a PCI bar area.
  150. */
  151. void qemu_vfio_pci_unmap_bar(QEMUVFIOState *s, int index, void *bar,
  152. uint64_t offset, uint64_t size)
  153. {
  154. if (bar) {
  155. munmap(bar, MIN(size, s->bar_region_info[index].size - offset));
  156. }
  157. }
  158. /**
  159. * Initialize device IRQ with @irq_type and and register an event notifier.
  160. */
  161. int qemu_vfio_pci_init_irq(QEMUVFIOState *s, EventNotifier *e,
  162. int irq_type, Error **errp)
  163. {
  164. int r;
  165. struct vfio_irq_set *irq_set;
  166. size_t irq_set_size;
  167. struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) };
  168. irq_info.index = irq_type;
  169. if (ioctl(s->device, VFIO_DEVICE_GET_IRQ_INFO, &irq_info)) {
  170. error_setg_errno(errp, errno, "Failed to get device interrupt info");
  171. return -errno;
  172. }
  173. if (!(irq_info.flags & VFIO_IRQ_INFO_EVENTFD)) {
  174. error_setg(errp, "Device interrupt doesn't support eventfd");
  175. return -EINVAL;
  176. }
  177. irq_set_size = sizeof(*irq_set) + sizeof(int);
  178. irq_set = g_malloc0(irq_set_size);
  179. /* Get to a known IRQ state */
  180. *irq_set = (struct vfio_irq_set) {
  181. .argsz = irq_set_size,
  182. .flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER,
  183. .index = irq_info.index,
  184. .start = 0,
  185. .count = 1,
  186. };
  187. *(int *)&irq_set->data = event_notifier_get_fd(e);
  188. r = ioctl(s->device, VFIO_DEVICE_SET_IRQS, irq_set);
  189. g_free(irq_set);
  190. if (r) {
  191. error_setg_errno(errp, errno, "Failed to setup device interrupt");
  192. return -errno;
  193. }
  194. return 0;
  195. }
  196. static int qemu_vfio_pci_read_config(QEMUVFIOState *s, void *buf,
  197. int size, int ofs)
  198. {
  199. int ret;
  200. do {
  201. ret = pread(s->device, buf, size, s->config_region_info.offset + ofs);
  202. } while (ret == -1 && errno == EINTR);
  203. return ret == size ? 0 : -errno;
  204. }
  205. static int qemu_vfio_pci_write_config(QEMUVFIOState *s, void *buf, int size, int ofs)
  206. {
  207. int ret;
  208. do {
  209. ret = pwrite(s->device, buf, size, s->config_region_info.offset + ofs);
  210. } while (ret == -1 && errno == EINTR);
  211. return ret == size ? 0 : -errno;
  212. }
  213. static int qemu_vfio_init_pci(QEMUVFIOState *s, const char *device,
  214. Error **errp)
  215. {
  216. int ret;
  217. int i;
  218. uint16_t pci_cmd;
  219. struct vfio_group_status group_status = { .argsz = sizeof(group_status) };
  220. struct vfio_iommu_type1_info iommu_info = { .argsz = sizeof(iommu_info) };
  221. struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
  222. char *group_file = NULL;
  223. /* Create a new container */
  224. s->container = open("/dev/vfio/vfio", O_RDWR);
  225. if (s->container == -1) {
  226. error_setg_errno(errp, errno, "Failed to open /dev/vfio/vfio");
  227. return -errno;
  228. }
  229. if (ioctl(s->container, VFIO_GET_API_VERSION) != VFIO_API_VERSION) {
  230. error_setg(errp, "Invalid VFIO version");
  231. ret = -EINVAL;
  232. goto fail_container;
  233. }
  234. if (!ioctl(s->container, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
  235. error_setg_errno(errp, errno, "VFIO IOMMU check failed");
  236. ret = -EINVAL;
  237. goto fail_container;
  238. }
  239. /* Open the group */
  240. group_file = sysfs_find_group_file(device, errp);
  241. if (!group_file) {
  242. ret = -EINVAL;
  243. goto fail_container;
  244. }
  245. s->group = open(group_file, O_RDWR);
  246. if (s->group == -1) {
  247. error_setg_errno(errp, errno, "Failed to open VFIO group file: %s",
  248. group_file);
  249. g_free(group_file);
  250. ret = -errno;
  251. goto fail_container;
  252. }
  253. g_free(group_file);
  254. /* Test the group is viable and available */
  255. if (ioctl(s->group, VFIO_GROUP_GET_STATUS, &group_status)) {
  256. error_setg_errno(errp, errno, "Failed to get VFIO group status");
  257. ret = -errno;
  258. goto fail;
  259. }
  260. if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
  261. error_setg(errp, "VFIO group is not viable");
  262. ret = -EINVAL;
  263. goto fail;
  264. }
  265. /* Add the group to the container */
  266. if (ioctl(s->group, VFIO_GROUP_SET_CONTAINER, &s->container)) {
  267. error_setg_errno(errp, errno, "Failed to add group to VFIO container");
  268. ret = -errno;
  269. goto fail;
  270. }
  271. /* Enable the IOMMU model we want */
  272. if (ioctl(s->container, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU)) {
  273. error_setg_errno(errp, errno, "Failed to set VFIO IOMMU type");
  274. ret = -errno;
  275. goto fail;
  276. }
  277. /* Get additional IOMMU info */
  278. if (ioctl(s->container, VFIO_IOMMU_GET_INFO, &iommu_info)) {
  279. error_setg_errno(errp, errno, "Failed to get IOMMU info");
  280. ret = -errno;
  281. goto fail;
  282. }
  283. s->device = ioctl(s->group, VFIO_GROUP_GET_DEVICE_FD, device);
  284. if (s->device < 0) {
  285. error_setg_errno(errp, errno, "Failed to get device fd");
  286. ret = -errno;
  287. goto fail;
  288. }
  289. /* Test and setup the device */
  290. if (ioctl(s->device, VFIO_DEVICE_GET_INFO, &device_info)) {
  291. error_setg_errno(errp, errno, "Failed to get device info");
  292. ret = -errno;
  293. goto fail;
  294. }
  295. if (device_info.num_regions < VFIO_PCI_CONFIG_REGION_INDEX) {
  296. error_setg(errp, "Invalid device regions");
  297. ret = -EINVAL;
  298. goto fail;
  299. }
  300. s->config_region_info = (struct vfio_region_info) {
  301. .index = VFIO_PCI_CONFIG_REGION_INDEX,
  302. .argsz = sizeof(struct vfio_region_info),
  303. };
  304. if (ioctl(s->device, VFIO_DEVICE_GET_REGION_INFO, &s->config_region_info)) {
  305. error_setg_errno(errp, errno, "Failed to get config region info");
  306. ret = -errno;
  307. goto fail;
  308. }
  309. for (i = 0; i < ARRAY_SIZE(s->bar_region_info); i++) {
  310. ret = qemu_vfio_pci_init_bar(s, i, errp);
  311. if (ret) {
  312. goto fail;
  313. }
  314. }
  315. /* Enable bus master */
  316. ret = qemu_vfio_pci_read_config(s, &pci_cmd, sizeof(pci_cmd), PCI_COMMAND);
  317. if (ret) {
  318. goto fail;
  319. }
  320. pci_cmd |= PCI_COMMAND_MASTER;
  321. ret = qemu_vfio_pci_write_config(s, &pci_cmd, sizeof(pci_cmd), PCI_COMMAND);
  322. if (ret) {
  323. goto fail;
  324. }
  325. return 0;
  326. fail:
  327. close(s->group);
  328. fail_container:
  329. close(s->container);
  330. return ret;
  331. }
  332. static void qemu_vfio_ram_block_added(RAMBlockNotifier *n,
  333. void *host, size_t size)
  334. {
  335. QEMUVFIOState *s = container_of(n, QEMUVFIOState, ram_notifier);
  336. trace_qemu_vfio_ram_block_added(s, host, size);
  337. qemu_vfio_dma_map(s, host, size, false, NULL);
  338. }
  339. static void qemu_vfio_ram_block_removed(RAMBlockNotifier *n,
  340. void *host, size_t size)
  341. {
  342. QEMUVFIOState *s = container_of(n, QEMUVFIOState, ram_notifier);
  343. if (host) {
  344. trace_qemu_vfio_ram_block_removed(s, host, size);
  345. qemu_vfio_dma_unmap(s, host);
  346. }
  347. }
  348. static int qemu_vfio_init_ramblock(RAMBlock *rb, void *opaque)
  349. {
  350. void *host_addr = qemu_ram_get_host_addr(rb);
  351. ram_addr_t length = qemu_ram_get_used_length(rb);
  352. int ret;
  353. QEMUVFIOState *s = opaque;
  354. if (!host_addr) {
  355. return 0;
  356. }
  357. ret = qemu_vfio_dma_map(s, host_addr, length, false, NULL);
  358. if (ret) {
  359. fprintf(stderr, "qemu_vfio_init_ramblock: failed %p %" PRId64 "\n",
  360. host_addr, (uint64_t)length);
  361. }
  362. return 0;
  363. }
  364. static void qemu_vfio_open_common(QEMUVFIOState *s)
  365. {
  366. qemu_mutex_init(&s->lock);
  367. s->ram_notifier.ram_block_added = qemu_vfio_ram_block_added;
  368. s->ram_notifier.ram_block_removed = qemu_vfio_ram_block_removed;
  369. ram_block_notifier_add(&s->ram_notifier);
  370. s->low_water_mark = QEMU_VFIO_IOVA_MIN;
  371. s->high_water_mark = QEMU_VFIO_IOVA_MAX;
  372. qemu_ram_foreach_block(qemu_vfio_init_ramblock, s);
  373. }
  374. /**
  375. * Open a PCI device, e.g. "0000:00:01.0".
  376. */
  377. QEMUVFIOState *qemu_vfio_open_pci(const char *device, Error **errp)
  378. {
  379. int r;
  380. QEMUVFIOState *s = g_new0(QEMUVFIOState, 1);
  381. r = qemu_vfio_init_pci(s, device, errp);
  382. if (r) {
  383. g_free(s);
  384. return NULL;
  385. }
  386. qemu_vfio_open_common(s);
  387. return s;
  388. }
  389. static void qemu_vfio_dump_mapping(IOVAMapping *m)
  390. {
  391. if (QEMU_VFIO_DEBUG) {
  392. printf(" vfio mapping %p %" PRIx64 " to %" PRIx64 "\n", m->host,
  393. (uint64_t)m->size, (uint64_t)m->iova);
  394. }
  395. }
  396. static void qemu_vfio_dump_mappings(QEMUVFIOState *s)
  397. {
  398. int i;
  399. if (QEMU_VFIO_DEBUG) {
  400. printf("vfio mappings\n");
  401. for (i = 0; i < s->nr_mappings; ++i) {
  402. qemu_vfio_dump_mapping(&s->mappings[i]);
  403. }
  404. }
  405. }
  406. /**
  407. * Find the mapping entry that contains [host, host + size) and set @index to
  408. * the position. If no entry contains it, @index is the position _after_ which
  409. * to insert the new mapping. IOW, it is the index of the largest element that
  410. * is smaller than @host, or -1 if no entry is.
  411. */
  412. static IOVAMapping *qemu_vfio_find_mapping(QEMUVFIOState *s, void *host,
  413. int *index)
  414. {
  415. IOVAMapping *p = s->mappings;
  416. IOVAMapping *q = p ? p + s->nr_mappings - 1 : NULL;
  417. IOVAMapping *mid;
  418. trace_qemu_vfio_find_mapping(s, host);
  419. if (!p) {
  420. *index = -1;
  421. return NULL;
  422. }
  423. while (true) {
  424. mid = p + (q - p) / 2;
  425. if (mid == p) {
  426. break;
  427. }
  428. if (mid->host > host) {
  429. q = mid;
  430. } else if (mid->host < host) {
  431. p = mid;
  432. } else {
  433. break;
  434. }
  435. }
  436. if (mid->host > host) {
  437. mid--;
  438. } else if (mid < &s->mappings[s->nr_mappings - 1]
  439. && (mid + 1)->host <= host) {
  440. mid++;
  441. }
  442. *index = mid - &s->mappings[0];
  443. if (mid >= &s->mappings[0] &&
  444. mid->host <= host && mid->host + mid->size > host) {
  445. assert(mid < &s->mappings[s->nr_mappings]);
  446. return mid;
  447. }
  448. /* At this point *index + 1 is the right position to insert the new
  449. * mapping.*/
  450. return NULL;
  451. }
  452. /**
  453. * Allocate IOVA and and create a new mapping record and insert it in @s.
  454. */
  455. static IOVAMapping *qemu_vfio_add_mapping(QEMUVFIOState *s,
  456. void *host, size_t size,
  457. int index, uint64_t iova)
  458. {
  459. int shift;
  460. IOVAMapping m = {.host = host, .size = size, .iova = iova};
  461. IOVAMapping *insert;
  462. assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size));
  463. assert(QEMU_IS_ALIGNED(s->low_water_mark, qemu_real_host_page_size));
  464. assert(QEMU_IS_ALIGNED(s->high_water_mark, qemu_real_host_page_size));
  465. trace_qemu_vfio_new_mapping(s, host, size, index, iova);
  466. assert(index >= 0);
  467. s->nr_mappings++;
  468. s->mappings = g_renew(IOVAMapping, s->mappings, s->nr_mappings);
  469. insert = &s->mappings[index];
  470. shift = s->nr_mappings - index - 1;
  471. if (shift) {
  472. memmove(insert + 1, insert, shift * sizeof(s->mappings[0]));
  473. }
  474. *insert = m;
  475. return insert;
  476. }
  477. /* Do the DMA mapping with VFIO. */
  478. static int qemu_vfio_do_mapping(QEMUVFIOState *s, void *host, size_t size,
  479. uint64_t iova)
  480. {
  481. struct vfio_iommu_type1_dma_map dma_map = {
  482. .argsz = sizeof(dma_map),
  483. .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
  484. .iova = iova,
  485. .vaddr = (uintptr_t)host,
  486. .size = size,
  487. };
  488. trace_qemu_vfio_do_mapping(s, host, size, iova);
  489. if (ioctl(s->container, VFIO_IOMMU_MAP_DMA, &dma_map)) {
  490. error_report("VFIO_MAP_DMA: %d", -errno);
  491. return -errno;
  492. }
  493. return 0;
  494. }
  495. /**
  496. * Undo the DMA mapping from @s with VFIO, and remove from mapping list.
  497. */
  498. static void qemu_vfio_undo_mapping(QEMUVFIOState *s, IOVAMapping *mapping,
  499. Error **errp)
  500. {
  501. int index;
  502. struct vfio_iommu_type1_dma_unmap unmap = {
  503. .argsz = sizeof(unmap),
  504. .flags = 0,
  505. .iova = mapping->iova,
  506. .size = mapping->size,
  507. };
  508. index = mapping - s->mappings;
  509. assert(mapping->size > 0);
  510. assert(QEMU_IS_ALIGNED(mapping->size, qemu_real_host_page_size));
  511. assert(index >= 0 && index < s->nr_mappings);
  512. if (ioctl(s->container, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
  513. error_setg(errp, "VFIO_UNMAP_DMA failed: %d", -errno);
  514. }
  515. memmove(mapping, &s->mappings[index + 1],
  516. sizeof(s->mappings[0]) * (s->nr_mappings - index - 1));
  517. s->nr_mappings--;
  518. s->mappings = g_renew(IOVAMapping, s->mappings, s->nr_mappings);
  519. }
  520. /* Check if the mapping list is (ascending) ordered. */
  521. static bool qemu_vfio_verify_mappings(QEMUVFIOState *s)
  522. {
  523. int i;
  524. if (QEMU_VFIO_DEBUG) {
  525. for (i = 0; i < s->nr_mappings - 1; ++i) {
  526. if (!(s->mappings[i].host < s->mappings[i + 1].host)) {
  527. fprintf(stderr, "item %d not sorted!\n", i);
  528. qemu_vfio_dump_mappings(s);
  529. return false;
  530. }
  531. if (!(s->mappings[i].host + s->mappings[i].size <=
  532. s->mappings[i + 1].host)) {
  533. fprintf(stderr, "item %d overlap with next!\n", i);
  534. qemu_vfio_dump_mappings(s);
  535. return false;
  536. }
  537. }
  538. }
  539. return true;
  540. }
  541. /* Map [host, host + size) area into a contiguous IOVA address space, and store
  542. * the result in @iova if not NULL. The caller need to make sure the area is
  543. * aligned to page size, and mustn't overlap with existing mapping areas (split
  544. * mapping status within this area is not allowed).
  545. */
  546. int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, size_t size,
  547. bool temporary, uint64_t *iova)
  548. {
  549. int ret = 0;
  550. int index;
  551. IOVAMapping *mapping;
  552. uint64_t iova0;
  553. assert(QEMU_PTR_IS_ALIGNED(host, qemu_real_host_page_size));
  554. assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size));
  555. trace_qemu_vfio_dma_map(s, host, size, temporary, iova);
  556. qemu_mutex_lock(&s->lock);
  557. mapping = qemu_vfio_find_mapping(s, host, &index);
  558. if (mapping) {
  559. iova0 = mapping->iova + ((uint8_t *)host - (uint8_t *)mapping->host);
  560. } else {
  561. if (s->high_water_mark - s->low_water_mark + 1 < size) {
  562. ret = -ENOMEM;
  563. goto out;
  564. }
  565. if (!temporary) {
  566. iova0 = s->low_water_mark;
  567. mapping = qemu_vfio_add_mapping(s, host, size, index + 1, iova0);
  568. if (!mapping) {
  569. ret = -ENOMEM;
  570. goto out;
  571. }
  572. assert(qemu_vfio_verify_mappings(s));
  573. ret = qemu_vfio_do_mapping(s, host, size, iova0);
  574. if (ret) {
  575. qemu_vfio_undo_mapping(s, mapping, NULL);
  576. goto out;
  577. }
  578. s->low_water_mark += size;
  579. qemu_vfio_dump_mappings(s);
  580. } else {
  581. iova0 = s->high_water_mark - size;
  582. ret = qemu_vfio_do_mapping(s, host, size, iova0);
  583. if (ret) {
  584. goto out;
  585. }
  586. s->high_water_mark -= size;
  587. }
  588. }
  589. if (iova) {
  590. *iova = iova0;
  591. }
  592. out:
  593. qemu_mutex_unlock(&s->lock);
  594. return ret;
  595. }
  596. /* Reset the high watermark and free all "temporary" mappings. */
  597. int qemu_vfio_dma_reset_temporary(QEMUVFIOState *s)
  598. {
  599. struct vfio_iommu_type1_dma_unmap unmap = {
  600. .argsz = sizeof(unmap),
  601. .flags = 0,
  602. .iova = s->high_water_mark,
  603. .size = QEMU_VFIO_IOVA_MAX - s->high_water_mark,
  604. };
  605. trace_qemu_vfio_dma_reset_temporary(s);
  606. qemu_mutex_lock(&s->lock);
  607. if (ioctl(s->container, VFIO_IOMMU_UNMAP_DMA, &unmap)) {
  608. error_report("VFIO_UNMAP_DMA: %d", -errno);
  609. qemu_mutex_unlock(&s->lock);
  610. return -errno;
  611. }
  612. s->high_water_mark = QEMU_VFIO_IOVA_MAX;
  613. qemu_mutex_unlock(&s->lock);
  614. return 0;
  615. }
  616. /* Unmapping the whole area that was previously mapped with
  617. * qemu_vfio_dma_map(). */
  618. void qemu_vfio_dma_unmap(QEMUVFIOState *s, void *host)
  619. {
  620. int index = 0;
  621. IOVAMapping *m;
  622. if (!host) {
  623. return;
  624. }
  625. trace_qemu_vfio_dma_unmap(s, host);
  626. qemu_mutex_lock(&s->lock);
  627. m = qemu_vfio_find_mapping(s, host, &index);
  628. if (!m) {
  629. goto out;
  630. }
  631. qemu_vfio_undo_mapping(s, m, NULL);
  632. out:
  633. qemu_mutex_unlock(&s->lock);
  634. }
  635. static void qemu_vfio_reset(QEMUVFIOState *s)
  636. {
  637. ioctl(s->device, VFIO_DEVICE_RESET);
  638. }
  639. /* Close and free the VFIO resources. */
  640. void qemu_vfio_close(QEMUVFIOState *s)
  641. {
  642. int i;
  643. if (!s) {
  644. return;
  645. }
  646. for (i = 0; i < s->nr_mappings; ++i) {
  647. qemu_vfio_undo_mapping(s, &s->mappings[i], NULL);
  648. }
  649. ram_block_notifier_remove(&s->ram_notifier);
  650. qemu_vfio_reset(s);
  651. close(s->device);
  652. close(s->group);
  653. close(s->container);
  654. }