s390-iommu.c 9.5 KB

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