iommu.c 17 KB

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