s390-iommu.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * IOMMU API for s390 PCI devices
  3. *
  4. * Copyright IBM Corp. 2015
  5. * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
  6. */
  7. #include <linux/pci.h>
  8. #include <linux/iommu.h>
  9. #include <linux/iommu-helper.h>
  10. #include <linux/sizes.h>
  11. #include <asm/pci_dma.h>
  12. /*
  13. * Physically contiguous memory regions can be mapped with 4 KiB alignment,
  14. * we allow all page sizes that are an order of 4KiB (no special large page
  15. * support so far).
  16. */
  17. #define S390_IOMMU_PGSIZES (~0xFFFUL)
  18. struct s390_domain {
  19. struct iommu_domain domain;
  20. struct list_head devices;
  21. unsigned long *dma_table;
  22. spinlock_t dma_table_lock;
  23. spinlock_t list_lock;
  24. };
  25. struct s390_domain_device {
  26. struct list_head list;
  27. struct zpci_dev *zdev;
  28. };
  29. static struct s390_domain *to_s390_domain(struct iommu_domain *dom)
  30. {
  31. return container_of(dom, struct s390_domain, domain);
  32. }
  33. static bool s390_iommu_capable(enum iommu_cap cap)
  34. {
  35. switch (cap) {
  36. case IOMMU_CAP_CACHE_COHERENCY:
  37. return true;
  38. case IOMMU_CAP_INTR_REMAP:
  39. return true;
  40. default:
  41. return false;
  42. }
  43. }
  44. static struct iommu_domain *s390_domain_alloc(unsigned domain_type)
  45. {
  46. struct s390_domain *s390_domain;
  47. if (domain_type != IOMMU_DOMAIN_UNMANAGED)
  48. return NULL;
  49. s390_domain = kzalloc(sizeof(*s390_domain), GFP_KERNEL);
  50. if (!s390_domain)
  51. return NULL;
  52. s390_domain->dma_table = dma_alloc_cpu_table();
  53. if (!s390_domain->dma_table) {
  54. kfree(s390_domain);
  55. return NULL;
  56. }
  57. spin_lock_init(&s390_domain->dma_table_lock);
  58. spin_lock_init(&s390_domain->list_lock);
  59. INIT_LIST_HEAD(&s390_domain->devices);
  60. return &s390_domain->domain;
  61. }
  62. static void s390_domain_free(struct iommu_domain *domain)
  63. {
  64. struct s390_domain *s390_domain = to_s390_domain(domain);
  65. dma_cleanup_tables(s390_domain->dma_table);
  66. kfree(s390_domain);
  67. }
  68. static int s390_iommu_attach_device(struct iommu_domain *domain,
  69. struct device *dev)
  70. {
  71. struct s390_domain *s390_domain = to_s390_domain(domain);
  72. struct zpci_dev *zdev = to_pci_dev(dev)->sysdata;
  73. struct s390_domain_device *domain_device;
  74. unsigned long flags;
  75. int rc;
  76. if (!zdev)
  77. return -ENODEV;
  78. domain_device = kzalloc(sizeof(*domain_device), GFP_KERNEL);
  79. if (!domain_device)
  80. return -ENOMEM;
  81. if (zdev->dma_table)
  82. zpci_dma_exit_device(zdev);
  83. zdev->dma_table = s390_domain->dma_table;
  84. rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
  85. (u64) zdev->dma_table);
  86. if (rc)
  87. goto out_restore;
  88. spin_lock_irqsave(&s390_domain->list_lock, flags);
  89. /* First device defines the DMA range limits */
  90. if (list_empty(&s390_domain->devices)) {
  91. domain->geometry.aperture_start = zdev->start_dma;
  92. domain->geometry.aperture_end = zdev->end_dma;
  93. domain->geometry.force_aperture = true;
  94. /* Allow only devices with identical DMA range limits */
  95. } else if (domain->geometry.aperture_start != zdev->start_dma ||
  96. domain->geometry.aperture_end != zdev->end_dma) {
  97. rc = -EINVAL;
  98. spin_unlock_irqrestore(&s390_domain->list_lock, flags);
  99. goto out_restore;
  100. }
  101. domain_device->zdev = zdev;
  102. zdev->s390_domain = s390_domain;
  103. list_add(&domain_device->list, &s390_domain->devices);
  104. spin_unlock_irqrestore(&s390_domain->list_lock, flags);
  105. return 0;
  106. out_restore:
  107. zpci_dma_init_device(zdev);
  108. kfree(domain_device);
  109. return rc;
  110. }
  111. static void s390_iommu_detach_device(struct iommu_domain *domain,
  112. struct device *dev)
  113. {
  114. struct s390_domain *s390_domain = to_s390_domain(domain);
  115. struct zpci_dev *zdev = to_pci_dev(dev)->sysdata;
  116. struct s390_domain_device *domain_device, *tmp;
  117. unsigned long flags;
  118. int found = 0;
  119. if (!zdev)
  120. return;
  121. spin_lock_irqsave(&s390_domain->list_lock, flags);
  122. list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices,
  123. list) {
  124. if (domain_device->zdev == zdev) {
  125. list_del(&domain_device->list);
  126. kfree(domain_device);
  127. found = 1;
  128. break;
  129. }
  130. }
  131. spin_unlock_irqrestore(&s390_domain->list_lock, flags);
  132. if (found) {
  133. zdev->s390_domain = NULL;
  134. zpci_unregister_ioat(zdev, 0);
  135. zpci_dma_init_device(zdev);
  136. }
  137. }
  138. static int s390_iommu_add_device(struct device *dev)
  139. {
  140. struct iommu_group *group;
  141. int rc;
  142. group = iommu_group_get(dev);
  143. if (!group) {
  144. group = iommu_group_alloc();
  145. if (IS_ERR(group))
  146. return PTR_ERR(group);
  147. }
  148. rc = iommu_group_add_device(group, dev);
  149. iommu_group_put(group);
  150. return rc;
  151. }
  152. static void s390_iommu_remove_device(struct device *dev)
  153. {
  154. struct zpci_dev *zdev = to_pci_dev(dev)->sysdata;
  155. struct iommu_domain *domain;
  156. /*
  157. * This is a workaround for a scenario where the IOMMU API common code
  158. * "forgets" to call the detach_dev callback: After binding a device
  159. * to vfio-pci and completing the VFIO_SET_IOMMU ioctl (which triggers
  160. * the attach_dev), removing the device via
  161. * "echo 1 > /sys/bus/pci/devices/.../remove" won't trigger detach_dev,
  162. * only remove_device will be called via the BUS_NOTIFY_REMOVED_DEVICE
  163. * notifier.
  164. *
  165. * So let's call detach_dev from here if it hasn't been called before.
  166. */
  167. if (zdev && zdev->s390_domain) {
  168. domain = iommu_get_domain_for_dev(dev);
  169. if (domain)
  170. s390_iommu_detach_device(domain, dev);
  171. }
  172. iommu_group_remove_device(dev);
  173. }
  174. static int s390_iommu_update_trans(struct s390_domain *s390_domain,
  175. unsigned long pa, dma_addr_t dma_addr,
  176. size_t size, int flags)
  177. {
  178. struct s390_domain_device *domain_device;
  179. u8 *page_addr = (u8 *) (pa & PAGE_MASK);
  180. dma_addr_t start_dma_addr = dma_addr;
  181. unsigned long irq_flags, nr_pages, i;
  182. unsigned long *entry;
  183. int rc = 0;
  184. if (dma_addr < s390_domain->domain.geometry.aperture_start ||
  185. dma_addr + size > s390_domain->domain.geometry.aperture_end)
  186. return -EINVAL;
  187. nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  188. if (!nr_pages)
  189. return 0;
  190. spin_lock_irqsave(&s390_domain->dma_table_lock, irq_flags);
  191. for (i = 0; i < nr_pages; i++) {
  192. entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr);
  193. if (!entry) {
  194. rc = -ENOMEM;
  195. goto undo_cpu_trans;
  196. }
  197. dma_update_cpu_trans(entry, page_addr, flags);
  198. page_addr += PAGE_SIZE;
  199. dma_addr += PAGE_SIZE;
  200. }
  201. spin_lock(&s390_domain->list_lock);
  202. list_for_each_entry(domain_device, &s390_domain->devices, list) {
  203. rc = zpci_refresh_trans((u64) domain_device->zdev->fh << 32,
  204. start_dma_addr, nr_pages * PAGE_SIZE);
  205. if (rc)
  206. break;
  207. }
  208. spin_unlock(&s390_domain->list_lock);
  209. undo_cpu_trans:
  210. if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
  211. flags = ZPCI_PTE_INVALID;
  212. while (i-- > 0) {
  213. page_addr -= PAGE_SIZE;
  214. dma_addr -= PAGE_SIZE;
  215. entry = dma_walk_cpu_trans(s390_domain->dma_table,
  216. dma_addr);
  217. if (!entry)
  218. break;
  219. dma_update_cpu_trans(entry, page_addr, flags);
  220. }
  221. }
  222. spin_unlock_irqrestore(&s390_domain->dma_table_lock, irq_flags);
  223. return rc;
  224. }
  225. static int s390_iommu_map(struct iommu_domain *domain, unsigned long iova,
  226. phys_addr_t paddr, size_t size, int prot)
  227. {
  228. struct s390_domain *s390_domain = to_s390_domain(domain);
  229. int flags = ZPCI_PTE_VALID, rc = 0;
  230. if (!(prot & IOMMU_READ))
  231. return -EINVAL;
  232. if (!(prot & IOMMU_WRITE))
  233. flags |= ZPCI_TABLE_PROTECTED;
  234. rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova,
  235. size, flags);
  236. return rc;
  237. }
  238. static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
  239. dma_addr_t iova)
  240. {
  241. struct s390_domain *s390_domain = to_s390_domain(domain);
  242. unsigned long *sto, *pto, *rto, flags;
  243. unsigned int rtx, sx, px;
  244. phys_addr_t phys = 0;
  245. if (iova < domain->geometry.aperture_start ||
  246. iova > domain->geometry.aperture_end)
  247. return 0;
  248. rtx = calc_rtx(iova);
  249. sx = calc_sx(iova);
  250. px = calc_px(iova);
  251. rto = s390_domain->dma_table;
  252. spin_lock_irqsave(&s390_domain->dma_table_lock, flags);
  253. if (rto && reg_entry_isvalid(rto[rtx])) {
  254. sto = get_rt_sto(rto[rtx]);
  255. if (sto && reg_entry_isvalid(sto[sx])) {
  256. pto = get_st_pto(sto[sx]);
  257. if (pto && pt_entry_isvalid(pto[px]))
  258. phys = pto[px] & ZPCI_PTE_ADDR_MASK;
  259. }
  260. }
  261. spin_unlock_irqrestore(&s390_domain->dma_table_lock, flags);
  262. return phys;
  263. }
  264. static size_t s390_iommu_unmap(struct iommu_domain *domain,
  265. unsigned long iova, size_t size)
  266. {
  267. struct s390_domain *s390_domain = to_s390_domain(domain);
  268. int flags = ZPCI_PTE_INVALID;
  269. phys_addr_t paddr;
  270. int rc;
  271. paddr = s390_iommu_iova_to_phys(domain, iova);
  272. if (!paddr)
  273. return 0;
  274. rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova,
  275. size, flags);
  276. if (rc)
  277. return 0;
  278. return size;
  279. }
  280. static struct iommu_ops s390_iommu_ops = {
  281. .capable = s390_iommu_capable,
  282. .domain_alloc = s390_domain_alloc,
  283. .domain_free = s390_domain_free,
  284. .attach_dev = s390_iommu_attach_device,
  285. .detach_dev = s390_iommu_detach_device,
  286. .map = s390_iommu_map,
  287. .unmap = s390_iommu_unmap,
  288. .iova_to_phys = s390_iommu_iova_to_phys,
  289. .add_device = s390_iommu_add_device,
  290. .remove_device = s390_iommu_remove_device,
  291. .pgsize_bitmap = S390_IOMMU_PGSIZES,
  292. };
  293. static int __init s390_iommu_init(void)
  294. {
  295. return bus_set_iommu(&pci_bus_type, &s390_iommu_ops);
  296. }
  297. subsys_initcall(s390_iommu_init);