iommu.c 18 KB

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