iommu.c 18 KB

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