pci_dma.c 17 KB

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