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