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