s390-iommu.c 8.8 KB

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