iommu.c 18 KB

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