s390-iommu.c 9.4 KB

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