2
0

iommu.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * IOMMU for remote device
  3. *
  4. * Copyright © 2022 Oracle and/or its affiliates.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. */
  10. #include "qemu/osdep.h"
  11. #include "hw/remote/iommu.h"
  12. #include "hw/pci/pci_bus.h"
  13. #include "hw/pci/pci.h"
  14. #include "exec/memory.h"
  15. #include "exec/address-spaces.h"
  16. #include "trace.h"
  17. /**
  18. * IOMMU for TYPE_REMOTE_MACHINE - manages DMA address space isolation
  19. * for remote machine. It is used by TYPE_VFIO_USER_SERVER.
  20. *
  21. * - Each TYPE_VFIO_USER_SERVER instance handles one PCIDevice on a PCIBus.
  22. * There is one RemoteIommu per PCIBus, so the RemoteIommu tracks multiple
  23. * PCIDevices by maintaining a ->elem_by_devfn mapping.
  24. *
  25. * - memory_region_init_iommu() is not used because vfio-user MemoryRegions
  26. * will be added to the elem->mr container instead. This is more natural
  27. * than implementing the IOMMUMemoryRegionClass APIs since vfio-user
  28. * provides something that is close to a full-fledged MemoryRegion and
  29. * not like an IOMMU mapping.
  30. *
  31. * - When a device is hot unplugged, the elem->mr reference is dropped so
  32. * all vfio-user MemoryRegions associated with this vfio-user server are
  33. * destroyed.
  34. */
  35. static AddressSpace *remote_iommu_find_add_as(PCIBus *pci_bus,
  36. void *opaque, int devfn)
  37. {
  38. RemoteIommu *iommu = opaque;
  39. RemoteIommuElem *elem = NULL;
  40. qemu_mutex_lock(&iommu->lock);
  41. elem = g_hash_table_lookup(iommu->elem_by_devfn, INT2VOIDP(devfn));
  42. if (!elem) {
  43. elem = g_new0(RemoteIommuElem, 1);
  44. g_hash_table_insert(iommu->elem_by_devfn, INT2VOIDP(devfn), elem);
  45. }
  46. if (!elem->mr) {
  47. elem->mr = MEMORY_REGION(object_new(TYPE_MEMORY_REGION));
  48. memory_region_set_size(elem->mr, UINT64_MAX);
  49. address_space_init(&elem->as, elem->mr, NULL);
  50. }
  51. qemu_mutex_unlock(&iommu->lock);
  52. return &elem->as;
  53. }
  54. void remote_iommu_unplug_dev(PCIDevice *pci_dev)
  55. {
  56. AddressSpace *as = pci_device_iommu_address_space(pci_dev);
  57. RemoteIommuElem *elem = NULL;
  58. if (as == &address_space_memory) {
  59. return;
  60. }
  61. elem = container_of(as, RemoteIommuElem, as);
  62. address_space_destroy(&elem->as);
  63. object_unref(elem->mr);
  64. elem->mr = NULL;
  65. }
  66. static void remote_iommu_init(Object *obj)
  67. {
  68. RemoteIommu *iommu = REMOTE_IOMMU(obj);
  69. iommu->elem_by_devfn = g_hash_table_new_full(NULL, NULL, NULL, g_free);
  70. qemu_mutex_init(&iommu->lock);
  71. }
  72. static void remote_iommu_finalize(Object *obj)
  73. {
  74. RemoteIommu *iommu = REMOTE_IOMMU(obj);
  75. qemu_mutex_destroy(&iommu->lock);
  76. g_hash_table_destroy(iommu->elem_by_devfn);
  77. iommu->elem_by_devfn = NULL;
  78. }
  79. void remote_iommu_setup(PCIBus *pci_bus)
  80. {
  81. RemoteIommu *iommu = NULL;
  82. g_assert(pci_bus);
  83. iommu = REMOTE_IOMMU(object_new(TYPE_REMOTE_IOMMU));
  84. pci_setup_iommu(pci_bus, remote_iommu_find_add_as, iommu);
  85. object_property_add_child(OBJECT(pci_bus), "remote-iommu", OBJECT(iommu));
  86. object_unref(OBJECT(iommu));
  87. }
  88. static const TypeInfo remote_iommu_info = {
  89. .name = TYPE_REMOTE_IOMMU,
  90. .parent = TYPE_OBJECT,
  91. .instance_size = sizeof(RemoteIommu),
  92. .instance_init = remote_iommu_init,
  93. .instance_finalize = remote_iommu_finalize,
  94. };
  95. static void remote_iommu_register_types(void)
  96. {
  97. type_register_static(&remote_iommu_info);
  98. }
  99. type_init(remote_iommu_register_types)