s390-iommu.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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_zpci_dev(dev);
  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_zpci_dev(dev);
  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 struct iommu_device *s390_iommu_probe_device(struct device *dev)
  141. {
  142. struct zpci_dev *zdev = to_zpci_dev(dev);
  143. return &zdev->iommu_dev;
  144. }
  145. static void s390_iommu_release_device(struct device *dev)
  146. {
  147. struct zpci_dev *zdev = to_zpci_dev(dev);
  148. struct iommu_domain *domain;
  149. /*
  150. * This is a workaround for a scenario where the IOMMU API common code
  151. * "forgets" to call the detach_dev callback: After binding a device
  152. * to vfio-pci and completing the VFIO_SET_IOMMU ioctl (which triggers
  153. * the attach_dev), removing the device via
  154. * "echo 1 > /sys/bus/pci/devices/.../remove" won't trigger detach_dev,
  155. * only release_device will be called via the BUS_NOTIFY_REMOVED_DEVICE
  156. * notifier.
  157. *
  158. * So let's call detach_dev from here if it hasn't been called before.
  159. */
  160. if (zdev && zdev->s390_domain) {
  161. domain = iommu_get_domain_for_dev(dev);
  162. if (domain)
  163. s390_iommu_detach_device(domain, dev);
  164. }
  165. }
  166. static int s390_iommu_update_trans(struct s390_domain *s390_domain,
  167. unsigned long pa, dma_addr_t dma_addr,
  168. size_t size, int flags)
  169. {
  170. struct s390_domain_device *domain_device;
  171. u8 *page_addr = (u8 *) (pa & PAGE_MASK);
  172. dma_addr_t start_dma_addr = dma_addr;
  173. unsigned long irq_flags, nr_pages, i;
  174. unsigned long *entry;
  175. int rc = 0;
  176. if (dma_addr < s390_domain->domain.geometry.aperture_start ||
  177. dma_addr + size > s390_domain->domain.geometry.aperture_end)
  178. return -EINVAL;
  179. nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  180. if (!nr_pages)
  181. return 0;
  182. spin_lock_irqsave(&s390_domain->dma_table_lock, irq_flags);
  183. for (i = 0; i < nr_pages; i++) {
  184. entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr);
  185. if (!entry) {
  186. rc = -ENOMEM;
  187. goto undo_cpu_trans;
  188. }
  189. dma_update_cpu_trans(entry, page_addr, flags);
  190. page_addr += PAGE_SIZE;
  191. dma_addr += PAGE_SIZE;
  192. }
  193. spin_lock(&s390_domain->list_lock);
  194. list_for_each_entry(domain_device, &s390_domain->devices, list) {
  195. rc = zpci_refresh_trans((u64) domain_device->zdev->fh << 32,
  196. start_dma_addr, nr_pages * PAGE_SIZE);
  197. if (rc)
  198. break;
  199. }
  200. spin_unlock(&s390_domain->list_lock);
  201. undo_cpu_trans:
  202. if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
  203. flags = ZPCI_PTE_INVALID;
  204. while (i-- > 0) {
  205. page_addr -= PAGE_SIZE;
  206. dma_addr -= PAGE_SIZE;
  207. entry = dma_walk_cpu_trans(s390_domain->dma_table,
  208. dma_addr);
  209. if (!entry)
  210. break;
  211. dma_update_cpu_trans(entry, page_addr, flags);
  212. }
  213. }
  214. spin_unlock_irqrestore(&s390_domain->dma_table_lock, irq_flags);
  215. return rc;
  216. }
  217. static int s390_iommu_map(struct iommu_domain *domain, unsigned long iova,
  218. phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
  219. {
  220. struct s390_domain *s390_domain = to_s390_domain(domain);
  221. int flags = ZPCI_PTE_VALID, rc = 0;
  222. if (!(prot & IOMMU_READ))
  223. return -EINVAL;
  224. if (!(prot & IOMMU_WRITE))
  225. flags |= ZPCI_TABLE_PROTECTED;
  226. rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova,
  227. size, flags);
  228. return rc;
  229. }
  230. static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
  231. dma_addr_t iova)
  232. {
  233. struct s390_domain *s390_domain = to_s390_domain(domain);
  234. unsigned long *sto, *pto, *rto, flags;
  235. unsigned int rtx, sx, px;
  236. phys_addr_t phys = 0;
  237. if (iova < domain->geometry.aperture_start ||
  238. iova > domain->geometry.aperture_end)
  239. return 0;
  240. rtx = calc_rtx(iova);
  241. sx = calc_sx(iova);
  242. px = calc_px(iova);
  243. rto = s390_domain->dma_table;
  244. spin_lock_irqsave(&s390_domain->dma_table_lock, flags);
  245. if (rto && reg_entry_isvalid(rto[rtx])) {
  246. sto = get_rt_sto(rto[rtx]);
  247. if (sto && reg_entry_isvalid(sto[sx])) {
  248. pto = get_st_pto(sto[sx]);
  249. if (pto && pt_entry_isvalid(pto[px]))
  250. phys = pto[px] & ZPCI_PTE_ADDR_MASK;
  251. }
  252. }
  253. spin_unlock_irqrestore(&s390_domain->dma_table_lock, flags);
  254. return phys;
  255. }
  256. static size_t s390_iommu_unmap(struct iommu_domain *domain,
  257. unsigned long iova, size_t size,
  258. struct iommu_iotlb_gather *gather)
  259. {
  260. struct s390_domain *s390_domain = to_s390_domain(domain);
  261. int flags = ZPCI_PTE_INVALID;
  262. phys_addr_t paddr;
  263. int rc;
  264. paddr = s390_iommu_iova_to_phys(domain, iova);
  265. if (!paddr)
  266. return 0;
  267. rc = s390_iommu_update_trans(s390_domain, (unsigned long) paddr, iova,
  268. size, flags);
  269. if (rc)
  270. return 0;
  271. return size;
  272. }
  273. int zpci_init_iommu(struct zpci_dev *zdev)
  274. {
  275. int rc = 0;
  276. rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL,
  277. "s390-iommu.%08x", zdev->fid);
  278. if (rc)
  279. goto out_err;
  280. iommu_device_set_ops(&zdev->iommu_dev, &s390_iommu_ops);
  281. rc = iommu_device_register(&zdev->iommu_dev);
  282. if (rc)
  283. goto out_sysfs;
  284. return 0;
  285. out_sysfs:
  286. iommu_device_sysfs_remove(&zdev->iommu_dev);
  287. out_err:
  288. return rc;
  289. }
  290. void zpci_destroy_iommu(struct zpci_dev *zdev)
  291. {
  292. iommu_device_unregister(&zdev->iommu_dev);
  293. iommu_device_sysfs_remove(&zdev->iommu_dev);
  294. }
  295. static const struct iommu_ops s390_iommu_ops = {
  296. .capable = s390_iommu_capable,
  297. .domain_alloc = s390_domain_alloc,
  298. .domain_free = s390_domain_free,
  299. .attach_dev = s390_iommu_attach_device,
  300. .detach_dev = s390_iommu_detach_device,
  301. .map = s390_iommu_map,
  302. .unmap = s390_iommu_unmap,
  303. .iova_to_phys = s390_iommu_iova_to_phys,
  304. .probe_device = s390_iommu_probe_device,
  305. .release_device = s390_iommu_release_device,
  306. .device_group = generic_device_group,
  307. .pgsize_bitmap = S390_IOMMU_PGSIZES,
  308. };
  309. static int __init s390_iommu_init(void)
  310. {
  311. return bus_set_iommu(&pci_bus_type, &s390_iommu_ops);
  312. }
  313. subsys_initcall(s390_iommu_init);