pci_dma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. * Copyright IBM Corp. 2012
  3. *
  4. * Author(s):
  5. * Jan Glauber <jang@linux.vnet.ibm.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/slab.h>
  9. #include <linux/export.h>
  10. #include <linux/iommu-helper.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/pci.h>
  14. #include <asm/pci_dma.h>
  15. static struct kmem_cache *dma_region_table_cache;
  16. static struct kmem_cache *dma_page_table_cache;
  17. static int s390_iommu_strict;
  18. static int zpci_refresh_global(struct zpci_dev *zdev)
  19. {
  20. return zpci_refresh_trans((u64) zdev->fh << 32, zdev->start_dma,
  21. zdev->iommu_pages * PAGE_SIZE);
  22. }
  23. unsigned long *dma_alloc_cpu_table(void)
  24. {
  25. unsigned long *table, *entry;
  26. table = kmem_cache_alloc(dma_region_table_cache, GFP_ATOMIC);
  27. if (!table)
  28. return NULL;
  29. for (entry = table; entry < table + ZPCI_TABLE_ENTRIES; entry++)
  30. *entry = ZPCI_TABLE_INVALID;
  31. return table;
  32. }
  33. static void dma_free_cpu_table(void *table)
  34. {
  35. kmem_cache_free(dma_region_table_cache, table);
  36. }
  37. static unsigned long *dma_alloc_page_table(void)
  38. {
  39. unsigned long *table, *entry;
  40. table = kmem_cache_alloc(dma_page_table_cache, GFP_ATOMIC);
  41. if (!table)
  42. return NULL;
  43. for (entry = table; entry < table + ZPCI_PT_ENTRIES; entry++)
  44. *entry = ZPCI_PTE_INVALID;
  45. return table;
  46. }
  47. static void dma_free_page_table(void *table)
  48. {
  49. kmem_cache_free(dma_page_table_cache, table);
  50. }
  51. static unsigned long *dma_get_seg_table_origin(unsigned long *entry)
  52. {
  53. unsigned long *sto;
  54. if (reg_entry_isvalid(*entry))
  55. sto = get_rt_sto(*entry);
  56. else {
  57. sto = dma_alloc_cpu_table();
  58. if (!sto)
  59. return NULL;
  60. set_rt_sto(entry, sto);
  61. validate_rt_entry(entry);
  62. entry_clr_protected(entry);
  63. }
  64. return sto;
  65. }
  66. static unsigned long *dma_get_page_table_origin(unsigned long *entry)
  67. {
  68. unsigned long *pto;
  69. if (reg_entry_isvalid(*entry))
  70. pto = get_st_pto(*entry);
  71. else {
  72. pto = dma_alloc_page_table();
  73. if (!pto)
  74. return NULL;
  75. set_st_pto(entry, pto);
  76. validate_st_entry(entry);
  77. entry_clr_protected(entry);
  78. }
  79. return pto;
  80. }
  81. unsigned long *dma_walk_cpu_trans(unsigned long *rto, dma_addr_t dma_addr)
  82. {
  83. unsigned long *sto, *pto;
  84. unsigned int rtx, sx, px;
  85. rtx = calc_rtx(dma_addr);
  86. sto = dma_get_seg_table_origin(&rto[rtx]);
  87. if (!sto)
  88. return NULL;
  89. sx = calc_sx(dma_addr);
  90. pto = dma_get_page_table_origin(&sto[sx]);
  91. if (!pto)
  92. return NULL;
  93. px = calc_px(dma_addr);
  94. return &pto[px];
  95. }
  96. void dma_update_cpu_trans(unsigned long *entry, void *page_addr, int flags)
  97. {
  98. if (flags & ZPCI_PTE_INVALID) {
  99. invalidate_pt_entry(entry);
  100. } else {
  101. set_pt_pfaa(entry, page_addr);
  102. validate_pt_entry(entry);
  103. }
  104. if (flags & ZPCI_TABLE_PROTECTED)
  105. entry_set_protected(entry);
  106. else
  107. entry_clr_protected(entry);
  108. }
  109. static int dma_update_trans(struct zpci_dev *zdev, unsigned long pa,
  110. dma_addr_t dma_addr, size_t size, int flags)
  111. {
  112. unsigned int nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  113. u8 *page_addr = (u8 *) (pa & PAGE_MASK);
  114. dma_addr_t start_dma_addr = dma_addr;
  115. unsigned long irq_flags;
  116. unsigned long *entry;
  117. int i, rc = 0;
  118. if (!nr_pages)
  119. return -EINVAL;
  120. spin_lock_irqsave(&zdev->dma_table_lock, irq_flags);
  121. if (!zdev->dma_table) {
  122. rc = -EINVAL;
  123. goto no_refresh;
  124. }
  125. for (i = 0; i < nr_pages; i++) {
  126. entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr);
  127. if (!entry) {
  128. rc = -ENOMEM;
  129. goto undo_cpu_trans;
  130. }
  131. dma_update_cpu_trans(entry, page_addr, flags);
  132. page_addr += PAGE_SIZE;
  133. dma_addr += PAGE_SIZE;
  134. }
  135. /*
  136. * With zdev->tlb_refresh == 0, rpcit is not required to establish new
  137. * translations when previously invalid translation-table entries are
  138. * validated. With lazy unmap, it also is skipped for previously valid
  139. * entries, but a global rpcit is then required before any address can
  140. * be re-used, i.e. after each iommu bitmap wrap-around.
  141. */
  142. if (!zdev->tlb_refresh &&
  143. (!s390_iommu_strict ||
  144. ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)))
  145. goto no_refresh;
  146. rc = zpci_refresh_trans((u64) zdev->fh << 32, start_dma_addr,
  147. nr_pages * PAGE_SIZE);
  148. undo_cpu_trans:
  149. if (rc && ((flags & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID)) {
  150. flags = ZPCI_PTE_INVALID;
  151. while (i-- > 0) {
  152. page_addr -= PAGE_SIZE;
  153. dma_addr -= PAGE_SIZE;
  154. entry = dma_walk_cpu_trans(zdev->dma_table, dma_addr);
  155. if (!entry)
  156. break;
  157. dma_update_cpu_trans(entry, page_addr, flags);
  158. }
  159. }
  160. no_refresh:
  161. spin_unlock_irqrestore(&zdev->dma_table_lock, irq_flags);
  162. return rc;
  163. }
  164. void dma_free_seg_table(unsigned long entry)
  165. {
  166. unsigned long *sto = get_rt_sto(entry);
  167. int sx;
  168. for (sx = 0; sx < ZPCI_TABLE_ENTRIES; sx++)
  169. if (reg_entry_isvalid(sto[sx]))
  170. dma_free_page_table(get_st_pto(sto[sx]));
  171. dma_free_cpu_table(sto);
  172. }
  173. void dma_cleanup_tables(unsigned long *table)
  174. {
  175. int rtx;
  176. if (!table)
  177. return;
  178. for (rtx = 0; rtx < ZPCI_TABLE_ENTRIES; rtx++)
  179. if (reg_entry_isvalid(table[rtx]))
  180. dma_free_seg_table(table[rtx]);
  181. dma_free_cpu_table(table);
  182. }
  183. static unsigned long __dma_alloc_iommu(struct zpci_dev *zdev,
  184. unsigned long start, int size)
  185. {
  186. unsigned long boundary_size;
  187. boundary_size = ALIGN(dma_get_seg_boundary(&zdev->pdev->dev) + 1,
  188. PAGE_SIZE) >> PAGE_SHIFT;
  189. return iommu_area_alloc(zdev->iommu_bitmap, zdev->iommu_pages,
  190. start, size, 0, boundary_size, 0);
  191. }
  192. static unsigned long dma_alloc_iommu(struct zpci_dev *zdev, int size)
  193. {
  194. unsigned long offset, flags;
  195. int wrap = 0;
  196. spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
  197. offset = __dma_alloc_iommu(zdev, zdev->next_bit, size);
  198. if (offset == -1) {
  199. /* wrap-around */
  200. offset = __dma_alloc_iommu(zdev, 0, size);
  201. wrap = 1;
  202. }
  203. if (offset != -1) {
  204. zdev->next_bit = offset + size;
  205. if (!zdev->tlb_refresh && !s390_iommu_strict && wrap)
  206. /* global flush after wrap-around with lazy unmap */
  207. zpci_refresh_global(zdev);
  208. }
  209. spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
  210. return offset;
  211. }
  212. static void dma_free_iommu(struct zpci_dev *zdev, unsigned long offset, int size)
  213. {
  214. unsigned long flags;
  215. spin_lock_irqsave(&zdev->iommu_bitmap_lock, flags);
  216. if (!zdev->iommu_bitmap)
  217. goto out;
  218. bitmap_clear(zdev->iommu_bitmap, offset, size);
  219. /*
  220. * Lazy flush for unmap: need to move next_bit to avoid address re-use
  221. * until wrap-around.
  222. */
  223. if (!s390_iommu_strict && offset >= zdev->next_bit)
  224. zdev->next_bit = offset + size;
  225. out:
  226. spin_unlock_irqrestore(&zdev->iommu_bitmap_lock, flags);
  227. }
  228. static inline void zpci_err_dma(unsigned long rc, unsigned long addr)
  229. {
  230. struct {
  231. unsigned long rc;
  232. unsigned long addr;
  233. } __packed data = {rc, addr};
  234. zpci_err_hex(&data, sizeof(data));
  235. }
  236. static dma_addr_t s390_dma_map_pages(struct device *dev, struct page *page,
  237. unsigned long offset, size_t size,
  238. enum dma_data_direction direction,
  239. struct dma_attrs *attrs)
  240. {
  241. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
  242. unsigned long nr_pages, iommu_page_index;
  243. unsigned long pa = page_to_phys(page) + offset;
  244. int flags = ZPCI_PTE_VALID;
  245. dma_addr_t dma_addr;
  246. int ret;
  247. /* This rounds up number of pages based on size and offset */
  248. nr_pages = iommu_num_pages(pa, size, PAGE_SIZE);
  249. iommu_page_index = dma_alloc_iommu(zdev, nr_pages);
  250. if (iommu_page_index == -1) {
  251. ret = -ENOSPC;
  252. goto out_err;
  253. }
  254. /* Use rounded up size */
  255. size = nr_pages * PAGE_SIZE;
  256. dma_addr = zdev->start_dma + iommu_page_index * PAGE_SIZE;
  257. if (dma_addr + size > zdev->end_dma) {
  258. ret = -ERANGE;
  259. goto out_free;
  260. }
  261. if (direction == DMA_NONE || direction == DMA_TO_DEVICE)
  262. flags |= ZPCI_TABLE_PROTECTED;
  263. ret = dma_update_trans(zdev, pa, dma_addr, size, flags);
  264. if (ret)
  265. goto out_free;
  266. atomic64_add(nr_pages, &zdev->mapped_pages);
  267. return dma_addr + (offset & ~PAGE_MASK);
  268. out_free:
  269. dma_free_iommu(zdev, iommu_page_index, nr_pages);
  270. out_err:
  271. zpci_err("map error:\n");
  272. zpci_err_dma(ret, pa);
  273. return DMA_ERROR_CODE;
  274. }
  275. static void s390_dma_unmap_pages(struct device *dev, dma_addr_t dma_addr,
  276. size_t size, enum dma_data_direction direction,
  277. struct dma_attrs *attrs)
  278. {
  279. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
  280. unsigned long iommu_page_index;
  281. int npages, ret;
  282. npages = iommu_num_pages(dma_addr, size, PAGE_SIZE);
  283. dma_addr = dma_addr & PAGE_MASK;
  284. ret = dma_update_trans(zdev, 0, dma_addr, npages * PAGE_SIZE,
  285. ZPCI_PTE_INVALID);
  286. if (ret) {
  287. zpci_err("unmap error:\n");
  288. zpci_err_dma(ret, dma_addr);
  289. return;
  290. }
  291. atomic64_add(npages, &zdev->unmapped_pages);
  292. iommu_page_index = (dma_addr - zdev->start_dma) >> PAGE_SHIFT;
  293. dma_free_iommu(zdev, iommu_page_index, npages);
  294. }
  295. static void *s390_dma_alloc(struct device *dev, size_t size,
  296. dma_addr_t *dma_handle, gfp_t flag,
  297. struct dma_attrs *attrs)
  298. {
  299. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
  300. struct page *page;
  301. unsigned long pa;
  302. dma_addr_t map;
  303. size = PAGE_ALIGN(size);
  304. page = alloc_pages(flag, get_order(size));
  305. if (!page)
  306. return NULL;
  307. pa = page_to_phys(page);
  308. memset((void *) pa, 0, size);
  309. map = s390_dma_map_pages(dev, page, pa % PAGE_SIZE,
  310. size, DMA_BIDIRECTIONAL, NULL);
  311. if (dma_mapping_error(dev, map)) {
  312. free_pages(pa, get_order(size));
  313. return NULL;
  314. }
  315. atomic64_add(size / PAGE_SIZE, &zdev->allocated_pages);
  316. if (dma_handle)
  317. *dma_handle = map;
  318. return (void *) pa;
  319. }
  320. static void s390_dma_free(struct device *dev, size_t size,
  321. void *pa, dma_addr_t dma_handle,
  322. struct dma_attrs *attrs)
  323. {
  324. struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
  325. size = PAGE_ALIGN(size);
  326. atomic64_sub(size / PAGE_SIZE, &zdev->allocated_pages);
  327. s390_dma_unmap_pages(dev, dma_handle, size, DMA_BIDIRECTIONAL, NULL);
  328. free_pages((unsigned long) pa, get_order(size));
  329. }
  330. static int s390_dma_map_sg(struct device *dev, struct scatterlist *sg,
  331. int nr_elements, enum dma_data_direction dir,
  332. struct dma_attrs *attrs)
  333. {
  334. int mapped_elements = 0;
  335. struct scatterlist *s;
  336. int i;
  337. for_each_sg(sg, s, nr_elements, i) {
  338. struct page *page = sg_page(s);
  339. s->dma_address = s390_dma_map_pages(dev, page, s->offset,
  340. s->length, dir, NULL);
  341. if (!dma_mapping_error(dev, s->dma_address)) {
  342. s->dma_length = s->length;
  343. mapped_elements++;
  344. } else
  345. goto unmap;
  346. }
  347. out:
  348. return mapped_elements;
  349. unmap:
  350. for_each_sg(sg, s, mapped_elements, i) {
  351. if (s->dma_address)
  352. s390_dma_unmap_pages(dev, s->dma_address, s->dma_length,
  353. dir, NULL);
  354. s->dma_address = 0;
  355. s->dma_length = 0;
  356. }
  357. mapped_elements = 0;
  358. goto out;
  359. }
  360. static void s390_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  361. int nr_elements, enum dma_data_direction dir,
  362. struct dma_attrs *attrs)
  363. {
  364. struct scatterlist *s;
  365. int i;
  366. for_each_sg(sg, s, nr_elements, i) {
  367. s390_dma_unmap_pages(dev, s->dma_address, s->dma_length, dir, NULL);
  368. s->dma_address = 0;
  369. s->dma_length = 0;
  370. }
  371. }
  372. int zpci_dma_init_device(struct zpci_dev *zdev)
  373. {
  374. int rc;
  375. /*
  376. * At this point, if the device is part of an IOMMU domain, this would
  377. * be a strong hint towards a bug in the IOMMU API (common) code and/or
  378. * simultaneous access via IOMMU and DMA API. So let's issue a warning.
  379. */
  380. WARN_ON(zdev->s390_domain);
  381. spin_lock_init(&zdev->iommu_bitmap_lock);
  382. spin_lock_init(&zdev->dma_table_lock);
  383. zdev->dma_table = dma_alloc_cpu_table();
  384. if (!zdev->dma_table) {
  385. rc = -ENOMEM;
  386. goto out;
  387. }
  388. /*
  389. * Restrict the iommu bitmap size to the minimum of the following:
  390. * - main memory size
  391. * - 3-level pagetable address limit minus start_dma offset
  392. * - DMA address range allowed by the hardware (clp query pci fn)
  393. *
  394. * Also set zdev->end_dma to the actual end address of the usable
  395. * range, instead of the theoretical maximum as reported by hardware.
  396. */
  397. zdev->iommu_size = min3((u64) high_memory,
  398. ZPCI_TABLE_SIZE_RT - zdev->start_dma,
  399. zdev->end_dma - zdev->start_dma + 1);
  400. zdev->end_dma = zdev->start_dma + zdev->iommu_size - 1;
  401. zdev->iommu_pages = zdev->iommu_size >> PAGE_SHIFT;
  402. zdev->iommu_bitmap = vzalloc(zdev->iommu_pages / 8);
  403. if (!zdev->iommu_bitmap) {
  404. rc = -ENOMEM;
  405. goto free_dma_table;
  406. }
  407. rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
  408. (u64) zdev->dma_table);
  409. if (rc)
  410. goto free_bitmap;
  411. return 0;
  412. free_bitmap:
  413. vfree(zdev->iommu_bitmap);
  414. zdev->iommu_bitmap = NULL;
  415. free_dma_table:
  416. dma_free_cpu_table(zdev->dma_table);
  417. zdev->dma_table = NULL;
  418. out:
  419. return rc;
  420. }
  421. void zpci_dma_exit_device(struct zpci_dev *zdev)
  422. {
  423. /*
  424. * At this point, if the device is part of an IOMMU domain, this would
  425. * be a strong hint towards a bug in the IOMMU API (common) code and/or
  426. * simultaneous access via IOMMU and DMA API. So let's issue a warning.
  427. */
  428. WARN_ON(zdev->s390_domain);
  429. zpci_unregister_ioat(zdev, 0);
  430. dma_cleanup_tables(zdev->dma_table);
  431. zdev->dma_table = NULL;
  432. vfree(zdev->iommu_bitmap);
  433. zdev->iommu_bitmap = NULL;
  434. zdev->next_bit = 0;
  435. }
  436. static int __init dma_alloc_cpu_table_caches(void)
  437. {
  438. dma_region_table_cache = kmem_cache_create("PCI_DMA_region_tables",
  439. ZPCI_TABLE_SIZE, ZPCI_TABLE_ALIGN,
  440. 0, NULL);
  441. if (!dma_region_table_cache)
  442. return -ENOMEM;
  443. dma_page_table_cache = kmem_cache_create("PCI_DMA_page_tables",
  444. ZPCI_PT_SIZE, ZPCI_PT_ALIGN,
  445. 0, NULL);
  446. if (!dma_page_table_cache) {
  447. kmem_cache_destroy(dma_region_table_cache);
  448. return -ENOMEM;
  449. }
  450. return 0;
  451. }
  452. int __init zpci_dma_init(void)
  453. {
  454. return dma_alloc_cpu_table_caches();
  455. }
  456. void zpci_dma_exit(void)
  457. {
  458. kmem_cache_destroy(dma_page_table_cache);
  459. kmem_cache_destroy(dma_region_table_cache);
  460. }
  461. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  462. static int __init dma_debug_do_init(void)
  463. {
  464. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  465. return 0;
  466. }
  467. fs_initcall(dma_debug_do_init);
  468. struct dma_map_ops s390_dma_ops = {
  469. .alloc = s390_dma_alloc,
  470. .free = s390_dma_free,
  471. .map_sg = s390_dma_map_sg,
  472. .unmap_sg = s390_dma_unmap_sg,
  473. .map_page = s390_dma_map_pages,
  474. .unmap_page = s390_dma_unmap_pages,
  475. /* if we support direct DMA this must be conditional */
  476. .is_phys = 0,
  477. /* dma_supported is unconditionally true without a callback */
  478. };
  479. EXPORT_SYMBOL_GPL(s390_dma_ops);
  480. static int __init s390_iommu_setup(char *str)
  481. {
  482. if (!strncmp(str, "strict", 6))
  483. s390_iommu_strict = 1;
  484. return 0;
  485. }
  486. __setup("s390_iommu=", s390_iommu_setup);