iommu.c 18 KB

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