iommu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
  3. *
  4. * Rewrite, cleanup, new allocation schemes, virtual merging:
  5. * Copyright (C) 2004 Olof Johansson, IBM Corporation
  6. * and Ben. Herrenschmidt, IBM Corporation
  7. *
  8. * Dynamic DMA mapping support, bus-independent parts.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/slab.h>
  27. #include <linux/mm.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/string.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/bitmap.h>
  32. #include <linux/iommu-helper.h>
  33. #include <linux/crash_dump.h>
  34. #include <asm/io.h>
  35. #include <asm/prom.h>
  36. #include <asm/iommu.h>
  37. #include <asm/pci-bridge.h>
  38. #include <asm/machdep.h>
  39. #include <asm/kdump.h>
  40. #include <asm/fadump.h>
  41. #define DBG(...)
  42. static int novmerge;
  43. static void __iommu_free(struct iommu_table *, dma_addr_t, unsigned int);
  44. static int __init setup_iommu(char *str)
  45. {
  46. if (!strcmp(str, "novmerge"))
  47. novmerge = 1;
  48. else if (!strcmp(str, "vmerge"))
  49. novmerge = 0;
  50. return 1;
  51. }
  52. __setup("iommu=", setup_iommu);
  53. static unsigned long iommu_range_alloc(struct device *dev,
  54. struct iommu_table *tbl,
  55. unsigned long npages,
  56. unsigned long *handle,
  57. unsigned long mask,
  58. unsigned int align_order)
  59. {
  60. unsigned long n, end, start;
  61. unsigned long limit;
  62. int largealloc = npages > 15;
  63. int pass = 0;
  64. unsigned long align_mask;
  65. unsigned long boundary_size;
  66. align_mask = 0xffffffffffffffffl >> (64 - align_order);
  67. /* This allocator was derived from x86_64's bit string search */
  68. /* Sanity check */
  69. if (unlikely(npages == 0)) {
  70. if (printk_ratelimit())
  71. WARN_ON(1);
  72. return DMA_ERROR_CODE;
  73. }
  74. if (handle && *handle)
  75. start = *handle;
  76. else
  77. start = largealloc ? tbl->it_largehint : tbl->it_hint;
  78. /* Use only half of the table for small allocs (15 pages or less) */
  79. limit = largealloc ? tbl->it_size : tbl->it_halfpoint;
  80. if (largealloc && start < tbl->it_halfpoint)
  81. start = tbl->it_halfpoint;
  82. /* The case below can happen if we have a small segment appended
  83. * to a large, or when the previous alloc was at the very end of
  84. * the available space. If so, go back to the initial start.
  85. */
  86. if (start >= limit)
  87. start = largealloc ? tbl->it_largehint : tbl->it_hint;
  88. again:
  89. if (limit + tbl->it_offset > mask) {
  90. limit = mask - tbl->it_offset + 1;
  91. /* If we're constrained on address range, first try
  92. * at the masked hint to avoid O(n) search complexity,
  93. * but on second pass, start at 0.
  94. */
  95. if ((start & mask) >= limit || pass > 0)
  96. start = 0;
  97. else
  98. start &= mask;
  99. }
  100. if (dev)
  101. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  102. 1 << IOMMU_PAGE_SHIFT);
  103. else
  104. boundary_size = ALIGN(1UL << 32, 1 << IOMMU_PAGE_SHIFT);
  105. /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
  106. n = iommu_area_alloc(tbl->it_map, limit, start, npages,
  107. tbl->it_offset, boundary_size >> IOMMU_PAGE_SHIFT,
  108. align_mask);
  109. if (n == -1) {
  110. if (likely(pass < 2)) {
  111. /* First failure, just rescan the half of the table.
  112. * Second failure, rescan the other half of the table.
  113. */
  114. start = (largealloc ^ pass) ? tbl->it_halfpoint : 0;
  115. limit = pass ? tbl->it_size : limit;
  116. pass++;
  117. goto again;
  118. } else {
  119. /* Third failure, give up */
  120. return DMA_ERROR_CODE;
  121. }
  122. }
  123. end = n + npages;
  124. /* Bump the hint to a new block for small allocs. */
  125. if (largealloc) {
  126. /* Don't bump to new block to avoid fragmentation */
  127. tbl->it_largehint = end;
  128. } else {
  129. /* Overflow will be taken care of at the next allocation */
  130. tbl->it_hint = (end + tbl->it_blocksize - 1) &
  131. ~(tbl->it_blocksize - 1);
  132. }
  133. /* Update handle for SG allocations */
  134. if (handle)
  135. *handle = end;
  136. return n;
  137. }
  138. static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
  139. void *page, unsigned int npages,
  140. enum dma_data_direction direction,
  141. unsigned long mask, unsigned int align_order,
  142. struct dma_attrs *attrs)
  143. {
  144. unsigned long entry, flags;
  145. dma_addr_t ret = DMA_ERROR_CODE;
  146. int build_fail;
  147. spin_lock_irqsave(&(tbl->it_lock), flags);
  148. entry = iommu_range_alloc(dev, tbl, npages, NULL, mask, align_order);
  149. if (unlikely(entry == DMA_ERROR_CODE)) {
  150. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  151. return DMA_ERROR_CODE;
  152. }
  153. entry += tbl->it_offset; /* Offset into real TCE table */
  154. ret = entry << IOMMU_PAGE_SHIFT; /* Set the return dma address */
  155. /* Put the TCEs in the HW table */
  156. build_fail = ppc_md.tce_build(tbl, entry, npages,
  157. (unsigned long)page & IOMMU_PAGE_MASK,
  158. direction, attrs);
  159. /* ppc_md.tce_build() only returns non-zero for transient errors.
  160. * Clean up the table bitmap in this case and return
  161. * DMA_ERROR_CODE. For all other errors the functionality is
  162. * not altered.
  163. */
  164. if (unlikely(build_fail)) {
  165. __iommu_free(tbl, ret, npages);
  166. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  167. return DMA_ERROR_CODE;
  168. }
  169. /* Flush/invalidate TLB caches if necessary */
  170. if (ppc_md.tce_flush)
  171. ppc_md.tce_flush(tbl);
  172. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  173. /* Make sure updates are seen by hardware */
  174. mb();
  175. return ret;
  176. }
  177. static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  178. unsigned int npages)
  179. {
  180. unsigned long entry, free_entry;
  181. entry = dma_addr >> IOMMU_PAGE_SHIFT;
  182. free_entry = entry - tbl->it_offset;
  183. if (((free_entry + npages) > tbl->it_size) ||
  184. (entry < tbl->it_offset)) {
  185. if (printk_ratelimit()) {
  186. printk(KERN_INFO "iommu_free: invalid entry\n");
  187. printk(KERN_INFO "\tentry = 0x%lx\n", entry);
  188. printk(KERN_INFO "\tdma_addr = 0x%llx\n", (u64)dma_addr);
  189. printk(KERN_INFO "\tTable = 0x%llx\n", (u64)tbl);
  190. printk(KERN_INFO "\tbus# = 0x%llx\n", (u64)tbl->it_busno);
  191. printk(KERN_INFO "\tsize = 0x%llx\n", (u64)tbl->it_size);
  192. printk(KERN_INFO "\tstartOff = 0x%llx\n", (u64)tbl->it_offset);
  193. printk(KERN_INFO "\tindex = 0x%llx\n", (u64)tbl->it_index);
  194. WARN_ON(1);
  195. }
  196. return;
  197. }
  198. ppc_md.tce_free(tbl, entry, npages);
  199. bitmap_clear(tbl->it_map, free_entry, npages);
  200. }
  201. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  202. unsigned int npages)
  203. {
  204. unsigned long flags;
  205. spin_lock_irqsave(&(tbl->it_lock), flags);
  206. __iommu_free(tbl, dma_addr, npages);
  207. /* Make sure TLB cache is flushed if the HW needs it. We do
  208. * not do an mb() here on purpose, it is not needed on any of
  209. * the current platforms.
  210. */
  211. if (ppc_md.tce_flush)
  212. ppc_md.tce_flush(tbl);
  213. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  214. }
  215. int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
  216. struct scatterlist *sglist, int nelems,
  217. unsigned long mask, enum dma_data_direction direction,
  218. struct dma_attrs *attrs)
  219. {
  220. dma_addr_t dma_next = 0, dma_addr;
  221. unsigned long flags;
  222. struct scatterlist *s, *outs, *segstart;
  223. int outcount, incount, i, build_fail = 0;
  224. unsigned int align;
  225. unsigned long handle;
  226. unsigned int max_seg_size;
  227. BUG_ON(direction == DMA_NONE);
  228. if ((nelems == 0) || !tbl)
  229. return 0;
  230. outs = s = segstart = &sglist[0];
  231. outcount = 1;
  232. incount = nelems;
  233. handle = 0;
  234. /* Init first segment length for backout at failure */
  235. outs->dma_length = 0;
  236. DBG("sg mapping %d elements:\n", nelems);
  237. spin_lock_irqsave(&(tbl->it_lock), flags);
  238. max_seg_size = dma_get_max_seg_size(dev);
  239. for_each_sg(sglist, s, nelems, i) {
  240. unsigned long vaddr, npages, entry, slen;
  241. slen = s->length;
  242. /* Sanity check */
  243. if (slen == 0) {
  244. dma_next = 0;
  245. continue;
  246. }
  247. /* Allocate iommu entries for that segment */
  248. vaddr = (unsigned long) sg_virt(s);
  249. npages = iommu_num_pages(vaddr, slen, IOMMU_PAGE_SIZE);
  250. align = 0;
  251. if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && slen >= PAGE_SIZE &&
  252. (vaddr & ~PAGE_MASK) == 0)
  253. align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
  254. entry = iommu_range_alloc(dev, tbl, npages, &handle,
  255. mask >> IOMMU_PAGE_SHIFT, align);
  256. DBG(" - vaddr: %lx, size: %lx\n", vaddr, slen);
  257. /* Handle failure */
  258. if (unlikely(entry == DMA_ERROR_CODE)) {
  259. if (printk_ratelimit())
  260. dev_info(dev, "iommu_alloc failed, tbl %p "
  261. "vaddr %lx npages %lu\n", tbl, vaddr,
  262. npages);
  263. goto failure;
  264. }
  265. /* Convert entry to a dma_addr_t */
  266. entry += tbl->it_offset;
  267. dma_addr = entry << IOMMU_PAGE_SHIFT;
  268. dma_addr |= (s->offset & ~IOMMU_PAGE_MASK);
  269. DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n",
  270. npages, entry, dma_addr);
  271. /* Insert into HW table */
  272. build_fail = ppc_md.tce_build(tbl, entry, npages,
  273. vaddr & IOMMU_PAGE_MASK,
  274. direction, attrs);
  275. if(unlikely(build_fail))
  276. goto failure;
  277. /* If we are in an open segment, try merging */
  278. if (segstart != s) {
  279. DBG(" - trying merge...\n");
  280. /* We cannot merge if:
  281. * - allocated dma_addr isn't contiguous to previous allocation
  282. */
  283. if (novmerge || (dma_addr != dma_next) ||
  284. (outs->dma_length + s->length > max_seg_size)) {
  285. /* Can't merge: create a new segment */
  286. segstart = s;
  287. outcount++;
  288. outs = sg_next(outs);
  289. DBG(" can't merge, new segment.\n");
  290. } else {
  291. outs->dma_length += s->length;
  292. DBG(" merged, new len: %ux\n", outs->dma_length);
  293. }
  294. }
  295. if (segstart == s) {
  296. /* This is a new segment, fill entries */
  297. DBG(" - filling new segment.\n");
  298. outs->dma_address = dma_addr;
  299. outs->dma_length = slen;
  300. }
  301. /* Calculate next page pointer for contiguous check */
  302. dma_next = dma_addr + slen;
  303. DBG(" - dma next is: %lx\n", dma_next);
  304. }
  305. /* Flush/invalidate TLB caches if necessary */
  306. if (ppc_md.tce_flush)
  307. ppc_md.tce_flush(tbl);
  308. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  309. DBG("mapped %d elements:\n", outcount);
  310. /* For the sake of iommu_unmap_sg, we clear out the length in the
  311. * next entry of the sglist if we didn't fill the list completely
  312. */
  313. if (outcount < incount) {
  314. outs = sg_next(outs);
  315. outs->dma_address = DMA_ERROR_CODE;
  316. outs->dma_length = 0;
  317. }
  318. /* Make sure updates are seen by hardware */
  319. mb();
  320. return outcount;
  321. failure:
  322. for_each_sg(sglist, s, nelems, i) {
  323. if (s->dma_length != 0) {
  324. unsigned long vaddr, npages;
  325. vaddr = s->dma_address & IOMMU_PAGE_MASK;
  326. npages = iommu_num_pages(s->dma_address, s->dma_length,
  327. IOMMU_PAGE_SIZE);
  328. __iommu_free(tbl, vaddr, npages);
  329. s->dma_address = DMA_ERROR_CODE;
  330. s->dma_length = 0;
  331. }
  332. if (s == outs)
  333. break;
  334. }
  335. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  336. return 0;
  337. }
  338. void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
  339. int nelems, enum dma_data_direction direction,
  340. struct dma_attrs *attrs)
  341. {
  342. struct scatterlist *sg;
  343. unsigned long flags;
  344. BUG_ON(direction == DMA_NONE);
  345. if (!tbl)
  346. return;
  347. spin_lock_irqsave(&(tbl->it_lock), flags);
  348. sg = sglist;
  349. while (nelems--) {
  350. unsigned int npages;
  351. dma_addr_t dma_handle = sg->dma_address;
  352. if (sg->dma_length == 0)
  353. break;
  354. npages = iommu_num_pages(dma_handle, sg->dma_length,
  355. IOMMU_PAGE_SIZE);
  356. __iommu_free(tbl, dma_handle, npages);
  357. sg = sg_next(sg);
  358. }
  359. /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
  360. * do not do an mb() here, the affected platforms do not need it
  361. * when freeing.
  362. */
  363. if (ppc_md.tce_flush)
  364. ppc_md.tce_flush(tbl);
  365. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  366. }
  367. static void iommu_table_clear(struct iommu_table *tbl)
  368. {
  369. /*
  370. * In case of firmware assisted dump system goes through clean
  371. * reboot process at the time of system crash. Hence it's safe to
  372. * clear the TCE entries if firmware assisted dump is active.
  373. */
  374. if (!is_kdump_kernel() || is_fadump_active()) {
  375. /* Clear the table in case firmware left allocations in it */
  376. ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
  377. return;
  378. }
  379. #ifdef CONFIG_CRASH_DUMP
  380. if (ppc_md.tce_get) {
  381. unsigned long index, tceval, tcecount = 0;
  382. /* Reserve the existing mappings left by the first kernel. */
  383. for (index = 0; index < tbl->it_size; index++) {
  384. tceval = ppc_md.tce_get(tbl, index + tbl->it_offset);
  385. /*
  386. * Freed TCE entry contains 0x7fffffffffffffff on JS20
  387. */
  388. if (tceval && (tceval != 0x7fffffffffffffffUL)) {
  389. __set_bit(index, tbl->it_map);
  390. tcecount++;
  391. }
  392. }
  393. if ((tbl->it_size - tcecount) < KDUMP_MIN_TCE_ENTRIES) {
  394. printk(KERN_WARNING "TCE table is full; freeing ");
  395. printk(KERN_WARNING "%d entries for the kdump boot\n",
  396. KDUMP_MIN_TCE_ENTRIES);
  397. for (index = tbl->it_size - KDUMP_MIN_TCE_ENTRIES;
  398. index < tbl->it_size; index++)
  399. __clear_bit(index, tbl->it_map);
  400. }
  401. }
  402. #endif
  403. }
  404. /*
  405. * Build a iommu_table structure. This contains a bit map which
  406. * is used to manage allocation of the tce space.
  407. */
  408. struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
  409. {
  410. unsigned long sz;
  411. static int welcomed = 0;
  412. struct page *page;
  413. /* Set aside 1/4 of the table for large allocations. */
  414. tbl->it_halfpoint = tbl->it_size * 3 / 4;
  415. /* number of bytes needed for the bitmap */
  416. sz = (tbl->it_size + 7) >> 3;
  417. page = alloc_pages_node(nid, GFP_KERNEL, get_order(sz));
  418. if (!page)
  419. panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
  420. tbl->it_map = page_address(page);
  421. memset(tbl->it_map, 0, sz);
  422. /*
  423. * Reserve page 0 so it will not be used for any mappings.
  424. * This avoids buggy drivers that consider page 0 to be invalid
  425. * to crash the machine or even lose data.
  426. */
  427. if (tbl->it_offset == 0)
  428. set_bit(0, tbl->it_map);
  429. tbl->it_hint = 0;
  430. tbl->it_largehint = tbl->it_halfpoint;
  431. spin_lock_init(&tbl->it_lock);
  432. iommu_table_clear(tbl);
  433. if (!welcomed) {
  434. printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",
  435. novmerge ? "disabled" : "enabled");
  436. welcomed = 1;
  437. }
  438. return tbl;
  439. }
  440. void iommu_free_table(struct iommu_table *tbl, const char *node_name)
  441. {
  442. unsigned long bitmap_sz, i;
  443. unsigned int order;
  444. if (!tbl || !tbl->it_map) {
  445. printk(KERN_ERR "%s: expected TCE map for %s\n", __func__,
  446. node_name);
  447. return;
  448. }
  449. /* verify that table contains no entries */
  450. /* it_size is in entries, and we're examining 64 at a time */
  451. for (i = 0; i < (tbl->it_size/64); i++) {
  452. if (tbl->it_map[i] != 0) {
  453. printk(KERN_WARNING "%s: Unexpected TCEs for %s\n",
  454. __func__, node_name);
  455. break;
  456. }
  457. }
  458. /* calculate bitmap size in bytes */
  459. bitmap_sz = (tbl->it_size + 7) / 8;
  460. /* free bitmap */
  461. order = get_order(bitmap_sz);
  462. free_pages((unsigned long) tbl->it_map, order);
  463. /* free table */
  464. kfree(tbl);
  465. }
  466. /* Creates TCEs for a user provided buffer. The user buffer must be
  467. * contiguous real kernel storage (not vmalloc). The address passed here
  468. * comprises a page address and offset into that page. The dma_addr_t
  469. * returned will point to the same byte within the page as was passed in.
  470. */
  471. dma_addr_t iommu_map_page(struct device *dev, struct iommu_table *tbl,
  472. struct page *page, unsigned long offset, size_t size,
  473. unsigned long mask, enum dma_data_direction direction,
  474. struct dma_attrs *attrs)
  475. {
  476. dma_addr_t dma_handle = DMA_ERROR_CODE;
  477. void *vaddr;
  478. unsigned long uaddr;
  479. unsigned int npages, align;
  480. BUG_ON(direction == DMA_NONE);
  481. vaddr = page_address(page) + offset;
  482. uaddr = (unsigned long)vaddr;
  483. npages = iommu_num_pages(uaddr, size, IOMMU_PAGE_SIZE);
  484. if (tbl) {
  485. align = 0;
  486. if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && size >= PAGE_SIZE &&
  487. ((unsigned long)vaddr & ~PAGE_MASK) == 0)
  488. align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
  489. dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction,
  490. mask >> IOMMU_PAGE_SHIFT, align,
  491. attrs);
  492. if (dma_handle == DMA_ERROR_CODE) {
  493. if (printk_ratelimit()) {
  494. dev_info(dev, "iommu_alloc failed, tbl %p "
  495. "vaddr %p npages %d\n", tbl, vaddr,
  496. npages);
  497. }
  498. } else
  499. dma_handle |= (uaddr & ~IOMMU_PAGE_MASK);
  500. }
  501. return dma_handle;
  502. }
  503. void iommu_unmap_page(struct iommu_table *tbl, dma_addr_t dma_handle,
  504. size_t size, enum dma_data_direction direction,
  505. struct dma_attrs *attrs)
  506. {
  507. unsigned int npages;
  508. BUG_ON(direction == DMA_NONE);
  509. if (tbl) {
  510. npages = iommu_num_pages(dma_handle, size, IOMMU_PAGE_SIZE);
  511. iommu_free(tbl, dma_handle, npages);
  512. }
  513. }
  514. /* Allocates a contiguous real buffer and creates mappings over it.
  515. * Returns the virtual address of the buffer and sets dma_handle
  516. * to the dma address (mapping) of the first page.
  517. */
  518. void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,
  519. size_t size, dma_addr_t *dma_handle,
  520. unsigned long mask, gfp_t flag, int node)
  521. {
  522. void *ret = NULL;
  523. dma_addr_t mapping;
  524. unsigned int order;
  525. unsigned int nio_pages, io_order;
  526. struct page *page;
  527. size = PAGE_ALIGN(size);
  528. order = get_order(size);
  529. /*
  530. * Client asked for way too much space. This is checked later
  531. * anyway. It is easier to debug here for the drivers than in
  532. * the tce tables.
  533. */
  534. if (order >= IOMAP_MAX_ORDER) {
  535. dev_info(dev, "iommu_alloc_consistent size too large: 0x%lx\n",
  536. size);
  537. return NULL;
  538. }
  539. if (!tbl)
  540. return NULL;
  541. /* Alloc enough pages (and possibly more) */
  542. page = alloc_pages_node(node, flag, order);
  543. if (!page)
  544. return NULL;
  545. ret = page_address(page);
  546. memset(ret, 0, size);
  547. /* Set up tces to cover the allocated range */
  548. nio_pages = size >> IOMMU_PAGE_SHIFT;
  549. io_order = get_iommu_order(size);
  550. mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL,
  551. mask >> IOMMU_PAGE_SHIFT, io_order, NULL);
  552. if (mapping == DMA_ERROR_CODE) {
  553. free_pages((unsigned long)ret, order);
  554. return NULL;
  555. }
  556. *dma_handle = mapping;
  557. return ret;
  558. }
  559. void iommu_free_coherent(struct iommu_table *tbl, size_t size,
  560. void *vaddr, dma_addr_t dma_handle)
  561. {
  562. if (tbl) {
  563. unsigned int nio_pages;
  564. size = PAGE_ALIGN(size);
  565. nio_pages = size >> IOMMU_PAGE_SHIFT;
  566. iommu_free(tbl, dma_handle, nio_pages);
  567. size = PAGE_ALIGN(size);
  568. free_pages((unsigned long)vaddr, get_order(size));
  569. }
  570. }