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/bitops.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. #define DBG(...)
  41. #ifdef CONFIG_IOMMU_VMERGE
  42. static int novmerge = 0;
  43. #else
  44. static int novmerge = 1;
  45. #endif
  46. static int protect4gb = 1;
  47. static void __iommu_free(struct iommu_table *, dma_addr_t, unsigned int);
  48. static int __init setup_protect4gb(char *str)
  49. {
  50. if (strcmp(str, "on") == 0)
  51. protect4gb = 1;
  52. else if (strcmp(str, "off") == 0)
  53. protect4gb = 0;
  54. return 1;
  55. }
  56. static int __init setup_iommu(char *str)
  57. {
  58. if (!strcmp(str, "novmerge"))
  59. novmerge = 1;
  60. else if (!strcmp(str, "vmerge"))
  61. novmerge = 0;
  62. return 1;
  63. }
  64. __setup("protect4gb=", setup_protect4gb);
  65. __setup("iommu=", setup_iommu);
  66. static unsigned long iommu_range_alloc(struct device *dev,
  67. struct iommu_table *tbl,
  68. unsigned long npages,
  69. unsigned long *handle,
  70. unsigned long mask,
  71. unsigned int align_order)
  72. {
  73. unsigned long n, end, start;
  74. unsigned long limit;
  75. int largealloc = npages > 15;
  76. int pass = 0;
  77. unsigned long align_mask;
  78. unsigned long boundary_size;
  79. align_mask = 0xffffffffffffffffl >> (64 - align_order);
  80. /* This allocator was derived from x86_64's bit string search */
  81. /* Sanity check */
  82. if (unlikely(npages == 0)) {
  83. if (printk_ratelimit())
  84. WARN_ON(1);
  85. return DMA_ERROR_CODE;
  86. }
  87. if (handle && *handle)
  88. start = *handle;
  89. else
  90. start = largealloc ? tbl->it_largehint : tbl->it_hint;
  91. /* Use only half of the table for small allocs (15 pages or less) */
  92. limit = largealloc ? tbl->it_size : tbl->it_halfpoint;
  93. if (largealloc && start < tbl->it_halfpoint)
  94. start = tbl->it_halfpoint;
  95. /* The case below can happen if we have a small segment appended
  96. * to a large, or when the previous alloc was at the very end of
  97. * the available space. If so, go back to the initial start.
  98. */
  99. if (start >= limit)
  100. start = largealloc ? tbl->it_largehint : tbl->it_hint;
  101. again:
  102. if (limit + tbl->it_offset > mask) {
  103. limit = mask - tbl->it_offset + 1;
  104. /* If we're constrained on address range, first try
  105. * at the masked hint to avoid O(n) search complexity,
  106. * but on second pass, start at 0.
  107. */
  108. if ((start & mask) >= limit || pass > 0)
  109. start = 0;
  110. else
  111. start &= mask;
  112. }
  113. if (dev)
  114. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  115. 1 << IOMMU_PAGE_SHIFT);
  116. else
  117. boundary_size = ALIGN(1UL << 32, 1 << IOMMU_PAGE_SHIFT);
  118. /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
  119. n = iommu_area_alloc(tbl->it_map, limit, start, npages,
  120. tbl->it_offset, boundary_size >> IOMMU_PAGE_SHIFT,
  121. align_mask);
  122. if (n == -1) {
  123. if (likely(pass < 2)) {
  124. /* First failure, just rescan the half of the table.
  125. * Second failure, rescan the other half of the table.
  126. */
  127. start = (largealloc ^ pass) ? tbl->it_halfpoint : 0;
  128. limit = pass ? tbl->it_size : limit;
  129. pass++;
  130. goto again;
  131. } else {
  132. /* Third failure, give up */
  133. return DMA_ERROR_CODE;
  134. }
  135. }
  136. end = n + npages;
  137. /* Bump the hint to a new block for small allocs. */
  138. if (largealloc) {
  139. /* Don't bump to new block to avoid fragmentation */
  140. tbl->it_largehint = end;
  141. } else {
  142. /* Overflow will be taken care of at the next allocation */
  143. tbl->it_hint = (end + tbl->it_blocksize - 1) &
  144. ~(tbl->it_blocksize - 1);
  145. }
  146. /* Update handle for SG allocations */
  147. if (handle)
  148. *handle = end;
  149. return n;
  150. }
  151. static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
  152. void *page, unsigned int npages,
  153. enum dma_data_direction direction,
  154. unsigned long mask, unsigned int align_order,
  155. struct dma_attrs *attrs)
  156. {
  157. unsigned long entry, flags;
  158. dma_addr_t ret = DMA_ERROR_CODE;
  159. int build_fail;
  160. spin_lock_irqsave(&(tbl->it_lock), flags);
  161. entry = iommu_range_alloc(dev, tbl, npages, NULL, mask, align_order);
  162. if (unlikely(entry == DMA_ERROR_CODE)) {
  163. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  164. return DMA_ERROR_CODE;
  165. }
  166. entry += tbl->it_offset; /* Offset into real TCE table */
  167. ret = entry << IOMMU_PAGE_SHIFT; /* Set the return dma address */
  168. /* Put the TCEs in the HW table */
  169. build_fail = ppc_md.tce_build(tbl, entry, npages,
  170. (unsigned long)page & IOMMU_PAGE_MASK,
  171. direction, attrs);
  172. /* ppc_md.tce_build() only returns non-zero for transient errors.
  173. * Clean up the table bitmap in this case and return
  174. * DMA_ERROR_CODE. For all other errors the functionality is
  175. * not altered.
  176. */
  177. if (unlikely(build_fail)) {
  178. __iommu_free(tbl, ret, npages);
  179. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  180. return DMA_ERROR_CODE;
  181. }
  182. /* Flush/invalidate TLB caches if necessary */
  183. if (ppc_md.tce_flush)
  184. ppc_md.tce_flush(tbl);
  185. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  186. /* Make sure updates are seen by hardware */
  187. mb();
  188. return ret;
  189. }
  190. static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  191. unsigned int npages)
  192. {
  193. unsigned long entry, free_entry;
  194. entry = dma_addr >> IOMMU_PAGE_SHIFT;
  195. free_entry = entry - tbl->it_offset;
  196. if (((free_entry + npages) > tbl->it_size) ||
  197. (entry < tbl->it_offset)) {
  198. if (printk_ratelimit()) {
  199. printk(KERN_INFO "iommu_free: invalid entry\n");
  200. printk(KERN_INFO "\tentry = 0x%lx\n", entry);
  201. printk(KERN_INFO "\tdma_addr = 0x%llx\n", (u64)dma_addr);
  202. printk(KERN_INFO "\tTable = 0x%llx\n", (u64)tbl);
  203. printk(KERN_INFO "\tbus# = 0x%llx\n", (u64)tbl->it_busno);
  204. printk(KERN_INFO "\tsize = 0x%llx\n", (u64)tbl->it_size);
  205. printk(KERN_INFO "\tstartOff = 0x%llx\n", (u64)tbl->it_offset);
  206. printk(KERN_INFO "\tindex = 0x%llx\n", (u64)tbl->it_index);
  207. WARN_ON(1);
  208. }
  209. return;
  210. }
  211. ppc_md.tce_free(tbl, entry, npages);
  212. iommu_area_free(tbl->it_map, free_entry, npages);
  213. }
  214. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  215. unsigned int npages)
  216. {
  217. unsigned long flags;
  218. spin_lock_irqsave(&(tbl->it_lock), flags);
  219. __iommu_free(tbl, dma_addr, npages);
  220. /* Make sure TLB cache is flushed if the HW needs it. We do
  221. * not do an mb() here on purpose, it is not needed on any of
  222. * the current platforms.
  223. */
  224. if (ppc_md.tce_flush)
  225. ppc_md.tce_flush(tbl);
  226. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  227. }
  228. int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
  229. struct scatterlist *sglist, int nelems,
  230. unsigned long mask, enum dma_data_direction direction,
  231. struct dma_attrs *attrs)
  232. {
  233. dma_addr_t dma_next = 0, dma_addr;
  234. unsigned long flags;
  235. struct scatterlist *s, *outs, *segstart;
  236. int outcount, incount, i, build_fail = 0;
  237. unsigned int align;
  238. unsigned long handle;
  239. unsigned int max_seg_size;
  240. BUG_ON(direction == DMA_NONE);
  241. if ((nelems == 0) || !tbl)
  242. return 0;
  243. outs = s = segstart = &sglist[0];
  244. outcount = 1;
  245. incount = nelems;
  246. handle = 0;
  247. /* Init first segment length for backout at failure */
  248. outs->dma_length = 0;
  249. DBG("sg mapping %d elements:\n", nelems);
  250. spin_lock_irqsave(&(tbl->it_lock), flags);
  251. max_seg_size = dma_get_max_seg_size(dev);
  252. for_each_sg(sglist, s, nelems, i) {
  253. unsigned long vaddr, npages, entry, slen;
  254. slen = s->length;
  255. /* Sanity check */
  256. if (slen == 0) {
  257. dma_next = 0;
  258. continue;
  259. }
  260. /* Allocate iommu entries for that segment */
  261. vaddr = (unsigned long) sg_virt(s);
  262. npages = iommu_num_pages(vaddr, slen, IOMMU_PAGE_SIZE);
  263. align = 0;
  264. if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && slen >= PAGE_SIZE &&
  265. (vaddr & ~PAGE_MASK) == 0)
  266. align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
  267. entry = iommu_range_alloc(dev, tbl, npages, &handle,
  268. mask >> IOMMU_PAGE_SHIFT, align);
  269. DBG(" - vaddr: %lx, size: %lx\n", vaddr, slen);
  270. /* Handle failure */
  271. if (unlikely(entry == DMA_ERROR_CODE)) {
  272. if (printk_ratelimit())
  273. printk(KERN_INFO "iommu_alloc failed, tbl %p vaddr %lx"
  274. " npages %lx\n", tbl, vaddr, npages);
  275. goto failure;
  276. }
  277. /* Convert entry to a dma_addr_t */
  278. entry += tbl->it_offset;
  279. dma_addr = entry << IOMMU_PAGE_SHIFT;
  280. dma_addr |= (s->offset & ~IOMMU_PAGE_MASK);
  281. DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n",
  282. npages, entry, dma_addr);
  283. /* Insert into HW table */
  284. build_fail = ppc_md.tce_build(tbl, entry, npages,
  285. vaddr & IOMMU_PAGE_MASK,
  286. direction, attrs);
  287. if(unlikely(build_fail))
  288. goto failure;
  289. /* If we are in an open segment, try merging */
  290. if (segstart != s) {
  291. DBG(" - trying merge...\n");
  292. /* We cannot merge if:
  293. * - allocated dma_addr isn't contiguous to previous allocation
  294. */
  295. if (novmerge || (dma_addr != dma_next) ||
  296. (outs->dma_length + s->length > max_seg_size)) {
  297. /* Can't merge: create a new segment */
  298. segstart = s;
  299. outcount++;
  300. outs = sg_next(outs);
  301. DBG(" can't merge, new segment.\n");
  302. } else {
  303. outs->dma_length += s->length;
  304. DBG(" merged, new len: %ux\n", outs->dma_length);
  305. }
  306. }
  307. if (segstart == s) {
  308. /* This is a new segment, fill entries */
  309. DBG(" - filling new segment.\n");
  310. outs->dma_address = dma_addr;
  311. outs->dma_length = slen;
  312. }
  313. /* Calculate next page pointer for contiguous check */
  314. dma_next = dma_addr + slen;
  315. DBG(" - dma next is: %lx\n", dma_next);
  316. }
  317. /* Flush/invalidate TLB caches if necessary */
  318. if (ppc_md.tce_flush)
  319. ppc_md.tce_flush(tbl);
  320. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  321. DBG("mapped %d elements:\n", outcount);
  322. /* For the sake of iommu_unmap_sg, we clear out the length in the
  323. * next entry of the sglist if we didn't fill the list completely
  324. */
  325. if (outcount < incount) {
  326. outs = sg_next(outs);
  327. outs->dma_address = DMA_ERROR_CODE;
  328. outs->dma_length = 0;
  329. }
  330. /* Make sure updates are seen by hardware */
  331. mb();
  332. return outcount;
  333. failure:
  334. for_each_sg(sglist, s, nelems, i) {
  335. if (s->dma_length != 0) {
  336. unsigned long vaddr, npages;
  337. vaddr = s->dma_address & IOMMU_PAGE_MASK;
  338. npages = iommu_num_pages(s->dma_address, s->dma_length,
  339. IOMMU_PAGE_SIZE);
  340. __iommu_free(tbl, vaddr, npages);
  341. s->dma_address = DMA_ERROR_CODE;
  342. s->dma_length = 0;
  343. }
  344. if (s == outs)
  345. break;
  346. }
  347. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  348. return 0;
  349. }
  350. void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
  351. int nelems, enum dma_data_direction direction,
  352. struct dma_attrs *attrs)
  353. {
  354. struct scatterlist *sg;
  355. unsigned long flags;
  356. BUG_ON(direction == DMA_NONE);
  357. if (!tbl)
  358. return;
  359. spin_lock_irqsave(&(tbl->it_lock), flags);
  360. sg = sglist;
  361. while (nelems--) {
  362. unsigned int npages;
  363. dma_addr_t dma_handle = sg->dma_address;
  364. if (sg->dma_length == 0)
  365. break;
  366. npages = iommu_num_pages(dma_handle, sg->dma_length,
  367. IOMMU_PAGE_SIZE);
  368. __iommu_free(tbl, dma_handle, npages);
  369. sg = sg_next(sg);
  370. }
  371. /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
  372. * do not do an mb() here, the affected platforms do not need it
  373. * when freeing.
  374. */
  375. if (ppc_md.tce_flush)
  376. ppc_md.tce_flush(tbl);
  377. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  378. }
  379. static void iommu_table_clear(struct iommu_table *tbl)
  380. {
  381. if (!is_kdump_kernel()) {
  382. /* Clear the table in case firmware left allocations in it */
  383. ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
  384. return;
  385. }
  386. #ifdef CONFIG_CRASH_DUMP
  387. if (ppc_md.tce_get) {
  388. unsigned long index, tceval, tcecount = 0;
  389. /* Reserve the existing mappings left by the first kernel. */
  390. for (index = 0; index < tbl->it_size; index++) {
  391. tceval = ppc_md.tce_get(tbl, index + tbl->it_offset);
  392. /*
  393. * Freed TCE entry contains 0x7fffffffffffffff on JS20
  394. */
  395. if (tceval && (tceval != 0x7fffffffffffffffUL)) {
  396. __set_bit(index, tbl->it_map);
  397. tcecount++;
  398. }
  399. }
  400. if ((tbl->it_size - tcecount) < KDUMP_MIN_TCE_ENTRIES) {
  401. printk(KERN_WARNING "TCE table is full; freeing ");
  402. printk(KERN_WARNING "%d entries for the kdump boot\n",
  403. KDUMP_MIN_TCE_ENTRIES);
  404. for (index = tbl->it_size - KDUMP_MIN_TCE_ENTRIES;
  405. index < tbl->it_size; index++)
  406. __clear_bit(index, tbl->it_map);
  407. }
  408. }
  409. #endif
  410. }
  411. /*
  412. * Build a iommu_table structure. This contains a bit map which
  413. * is used to manage allocation of the tce space.
  414. */
  415. struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
  416. {
  417. unsigned long sz;
  418. static int welcomed = 0;
  419. struct page *page;
  420. /* Set aside 1/4 of the table for large allocations. */
  421. tbl->it_halfpoint = tbl->it_size * 3 / 4;
  422. /* number of bytes needed for the bitmap */
  423. sz = (tbl->it_size + 7) >> 3;
  424. page = alloc_pages_node(nid, GFP_ATOMIC, get_order(sz));
  425. if (!page)
  426. panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
  427. tbl->it_map = page_address(page);
  428. memset(tbl->it_map, 0, sz);
  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. printk(KERN_INFO "iommu_alloc failed, "
  495. "tbl %p vaddr %p npages %d\n",
  496. tbl, vaddr, 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. printk("iommu_alloc_consistent size too large: 0x%lx\n", size);
  536. return NULL;
  537. }
  538. if (!tbl)
  539. return NULL;
  540. /* Alloc enough pages (and possibly more) */
  541. page = alloc_pages_node(node, flag, order);
  542. if (!page)
  543. return NULL;
  544. ret = page_address(page);
  545. memset(ret, 0, size);
  546. /* Set up tces to cover the allocated range */
  547. nio_pages = size >> IOMMU_PAGE_SHIFT;
  548. io_order = get_iommu_order(size);
  549. mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL,
  550. mask >> IOMMU_PAGE_SHIFT, io_order, NULL);
  551. if (mapping == DMA_ERROR_CODE) {
  552. free_pages((unsigned long)ret, order);
  553. return NULL;
  554. }
  555. *dma_handle = mapping;
  556. return ret;
  557. }
  558. void iommu_free_coherent(struct iommu_table *tbl, size_t size,
  559. void *vaddr, dma_addr_t dma_handle)
  560. {
  561. if (tbl) {
  562. unsigned int nio_pages;
  563. size = PAGE_ALIGN(size);
  564. nio_pages = size >> IOMMU_PAGE_SHIFT;
  565. iommu_free(tbl, dma_handle, nio_pages);
  566. size = PAGE_ALIGN(size);
  567. free_pages((unsigned long)vaddr, get_order(size));
  568. }
  569. }