s390-iommu.c 9.1 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 <[email protected]>
  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(struct device *dev, 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. WARN_ON(!list_empty(&s390_domain->devices));
  68. dma_cleanup_tables(s390_domain->dma_table);
  69. kfree(s390_domain);
  70. }
  71. static void __s390_iommu_detach_device(struct zpci_dev *zdev)
  72. {
  73. struct s390_domain *s390_domain = zdev->s390_domain;
  74. struct s390_domain_device *domain_device, *tmp;
  75. unsigned long flags;
  76. if (!s390_domain)
  77. return;
  78. spin_lock_irqsave(&s390_domain->list_lock, flags);
  79. list_for_each_entry_safe(domain_device, tmp, &s390_domain->devices,
  80. list) {
  81. if (domain_device->zdev == zdev) {
  82. list_del(&domain_device->list);
  83. kfree(domain_device);
  84. break;
  85. }
  86. }
  87. spin_unlock_irqrestore(&s390_domain->list_lock, flags);
  88. zpci_unregister_ioat(zdev, 0);
  89. zdev->s390_domain = NULL;
  90. zdev->dma_table = NULL;
  91. }
  92. static int s390_iommu_attach_device(struct iommu_domain *domain,
  93. struct device *dev)
  94. {
  95. struct s390_domain *s390_domain = to_s390_domain(domain);
  96. struct zpci_dev *zdev = to_zpci_dev(dev);
  97. struct s390_domain_device *domain_device;
  98. unsigned long flags;
  99. int cc, rc = 0;
  100. if (!zdev)
  101. return -ENODEV;
  102. domain_device = kzalloc(sizeof(*domain_device), GFP_KERNEL);
  103. if (!domain_device)
  104. return -ENOMEM;
  105. if (zdev->s390_domain)
  106. __s390_iommu_detach_device(zdev);
  107. else if (zdev->dma_table)
  108. zpci_dma_exit_device(zdev);
  109. cc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
  110. virt_to_phys(s390_domain->dma_table));
  111. if (cc) {
  112. rc = -EIO;
  113. goto out_free;
  114. }
  115. zdev->dma_table = s390_domain->dma_table;
  116. spin_lock_irqsave(&s390_domain->list_lock, flags);
  117. /* First device defines the DMA range limits */
  118. if (list_empty(&s390_domain->devices)) {
  119. domain->geometry.aperture_start = zdev->start_dma;
  120. domain->geometry.aperture_end = zdev->end_dma;
  121. domain->geometry.force_aperture = true;
  122. /* Allow only devices with identical DMA range limits */
  123. } else if (domain->geometry.aperture_start != zdev->start_dma ||
  124. domain->geometry.aperture_end != zdev->end_dma) {
  125. spin_unlock_irqrestore(&s390_domain->list_lock, flags);
  126. rc = -EINVAL;
  127. goto out_unregister;
  128. }
  129. domain_device->zdev = zdev;
  130. zdev->s390_domain = s390_domain;
  131. list_add(&domain_device->list, &s390_domain->devices);
  132. spin_unlock_irqrestore(&s390_domain->list_lock, flags);
  133. return 0;
  134. out_unregister:
  135. zpci_unregister_ioat(zdev, 0);
  136. zdev->dma_table = NULL;
  137. out_free:
  138. kfree(domain_device);
  139. return rc;
  140. }
  141. static void s390_iommu_detach_device(struct iommu_domain *domain,
  142. struct device *dev)
  143. {
  144. struct zpci_dev *zdev = to_zpci_dev(dev);
  145. WARN_ON(zdev->s390_domain != to_s390_domain(domain));
  146. __s390_iommu_detach_device(zdev);
  147. zpci_dma_init_device(zdev);
  148. }
  149. static struct iommu_device *s390_iommu_probe_device(struct device *dev)
  150. {
  151. struct zpci_dev *zdev;
  152. if (!dev_is_pci(dev))
  153. return ERR_PTR(-ENODEV);
  154. zdev = to_zpci_dev(dev);
  155. return &zdev->iommu_dev;
  156. }
  157. static void s390_iommu_release_device(struct device *dev)
  158. {
  159. struct zpci_dev *zdev = to_zpci_dev(dev);
  160. /*
  161. * release_device is expected to detach any domain currently attached
  162. * to the device, but keep it attached to other devices in the group.
  163. */
  164. if (zdev)
  165. __s390_iommu_detach_device(zdev);
  166. }
  167. static int s390_iommu_update_trans(struct s390_domain *s390_domain,
  168. phys_addr_t pa, dma_addr_t dma_addr,
  169. size_t size, int flags)
  170. {
  171. struct s390_domain_device *domain_device;
  172. phys_addr_t page_addr = pa & PAGE_MASK;
  173. dma_addr_t start_dma_addr = dma_addr;
  174. unsigned long irq_flags, nr_pages, i;
  175. unsigned long *entry;
  176. int rc = 0;
  177. if (dma_addr < s390_domain->domain.geometry.aperture_start ||
  178. dma_addr + size > s390_domain->domain.geometry.aperture_end)
  179. return -EINVAL;
  180. nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  181. if (!nr_pages)
  182. return 0;
  183. spin_lock_irqsave(&s390_domain->dma_table_lock, irq_flags);
  184. for (i = 0; i < nr_pages; i++) {
  185. entry = dma_walk_cpu_trans(s390_domain->dma_table, dma_addr);
  186. if (!entry) {
  187. rc = -ENOMEM;
  188. goto undo_cpu_trans;
  189. }
  190. dma_update_cpu_trans(entry, page_addr, flags);
  191. page_addr += PAGE_SIZE;
  192. dma_addr += PAGE_SIZE;
  193. }
  194. spin_lock(&s390_domain->list_lock);
  195. list_for_each_entry(domain_device, &s390_domain->devices, list) {
  196. rc = zpci_refresh_trans((u64) domain_device->zdev->fh << 32,
  197. start_dma_addr, nr_pages * PAGE_SIZE);
  198. if (rc)
  199. break;
  200. }
  201. spin_unlock(&s390_domain->list_lock);
  202. undo_cpu_trans:
  203. if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
  204. flags = ZPCI_PTE_INVALID;
  205. while (i-- > 0) {
  206. page_addr -= PAGE_SIZE;
  207. dma_addr -= PAGE_SIZE;
  208. entry = dma_walk_cpu_trans(s390_domain->dma_table,
  209. dma_addr);
  210. if (!entry)
  211. break;
  212. dma_update_cpu_trans(entry, page_addr, flags);
  213. }
  214. }
  215. spin_unlock_irqrestore(&s390_domain->dma_table_lock, irq_flags);
  216. return rc;
  217. }
  218. static int s390_iommu_map(struct iommu_domain *domain, unsigned long iova,
  219. phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
  220. {
  221. struct s390_domain *s390_domain = to_s390_domain(domain);
  222. int flags = ZPCI_PTE_VALID, rc = 0;
  223. if (!(prot & IOMMU_READ))
  224. return -EINVAL;
  225. if (!(prot & IOMMU_WRITE))
  226. flags |= ZPCI_TABLE_PROTECTED;
  227. rc = s390_iommu_update_trans(s390_domain, paddr, iova,
  228. size, flags);
  229. return rc;
  230. }
  231. static phys_addr_t s390_iommu_iova_to_phys(struct iommu_domain *domain,
  232. dma_addr_t iova)
  233. {
  234. struct s390_domain *s390_domain = to_s390_domain(domain);
  235. unsigned long *sto, *pto, *rto, flags;
  236. unsigned int rtx, sx, px;
  237. phys_addr_t phys = 0;
  238. if (iova < domain->geometry.aperture_start ||
  239. iova > domain->geometry.aperture_end)
  240. return 0;
  241. rtx = calc_rtx(iova);
  242. sx = calc_sx(iova);
  243. px = calc_px(iova);
  244. rto = s390_domain->dma_table;
  245. spin_lock_irqsave(&s390_domain->dma_table_lock, flags);
  246. if (rto && reg_entry_isvalid(rto[rtx])) {
  247. sto = get_rt_sto(rto[rtx]);
  248. if (sto && reg_entry_isvalid(sto[sx])) {
  249. pto = get_st_pto(sto[sx]);
  250. if (pto && pt_entry_isvalid(pto[px]))
  251. phys = pto[px] & ZPCI_PTE_ADDR_MASK;
  252. }
  253. }
  254. spin_unlock_irqrestore(&s390_domain->dma_table_lock, flags);
  255. return phys;
  256. }
  257. static size_t s390_iommu_unmap(struct iommu_domain *domain,
  258. unsigned long iova, size_t size,
  259. struct iommu_iotlb_gather *gather)
  260. {
  261. struct s390_domain *s390_domain = to_s390_domain(domain);
  262. int flags = ZPCI_PTE_INVALID;
  263. phys_addr_t paddr;
  264. int rc;
  265. paddr = s390_iommu_iova_to_phys(domain, iova);
  266. if (!paddr)
  267. return 0;
  268. rc = s390_iommu_update_trans(s390_domain, paddr, iova,
  269. size, flags);
  270. if (rc)
  271. return 0;
  272. return size;
  273. }
  274. int zpci_init_iommu(struct zpci_dev *zdev)
  275. {
  276. int rc = 0;
  277. rc = iommu_device_sysfs_add(&zdev->iommu_dev, NULL, NULL,
  278. "s390-iommu.%08x", zdev->fid);
  279. if (rc)
  280. goto out_err;
  281. rc = iommu_device_register(&zdev->iommu_dev, &s390_iommu_ops, NULL);
  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. .probe_device = s390_iommu_probe_device,
  299. .release_device = s390_iommu_release_device,
  300. .device_group = generic_device_group,
  301. .pgsize_bitmap = S390_IOMMU_PGSIZES,
  302. .default_domain_ops = &(const struct iommu_domain_ops) {
  303. .attach_dev = s390_iommu_attach_device,
  304. .detach_dev = s390_iommu_detach_device,
  305. .map = s390_iommu_map,
  306. .unmap = s390_iommu_unmap,
  307. .iova_to_phys = s390_iommu_iova_to_phys,
  308. .free = s390_domain_free,
  309. }
  310. };