iommu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 <linux/hash.h>
  35. #include <linux/fault-inject.h>
  36. #include <linux/pci.h>
  37. #include <asm/io.h>
  38. #include <asm/prom.h>
  39. #include <asm/iommu.h>
  40. #include <asm/pci-bridge.h>
  41. #include <asm/machdep.h>
  42. #include <asm/kdump.h>
  43. #include <asm/fadump.h>
  44. #include <asm/vio.h>
  45. #define DBG(...)
  46. static int novmerge;
  47. static void __iommu_free(struct iommu_table *, dma_addr_t, unsigned int);
  48. static int __init setup_iommu(char *str)
  49. {
  50. if (!strcmp(str, "novmerge"))
  51. novmerge = 1;
  52. else if (!strcmp(str, "vmerge"))
  53. novmerge = 0;
  54. return 1;
  55. }
  56. __setup("iommu=", setup_iommu);
  57. static DEFINE_PER_CPU(unsigned int, iommu_pool_hash);
  58. /*
  59. * We precalculate the hash to avoid doing it on every allocation.
  60. *
  61. * The hash is important to spread CPUs across all the pools. For example,
  62. * on a POWER7 with 4 way SMT we want interrupts on the primary threads and
  63. * with 4 pools all primary threads would map to the same pool.
  64. */
  65. static int __init setup_iommu_pool_hash(void)
  66. {
  67. unsigned int i;
  68. for_each_possible_cpu(i)
  69. per_cpu(iommu_pool_hash, i) = hash_32(i, IOMMU_POOL_HASHBITS);
  70. return 0;
  71. }
  72. subsys_initcall(setup_iommu_pool_hash);
  73. #ifdef CONFIG_FAIL_IOMMU
  74. static DECLARE_FAULT_ATTR(fail_iommu);
  75. static int __init setup_fail_iommu(char *str)
  76. {
  77. return setup_fault_attr(&fail_iommu, str);
  78. }
  79. __setup("fail_iommu=", setup_fail_iommu);
  80. static bool should_fail_iommu(struct device *dev)
  81. {
  82. return dev->archdata.fail_iommu && should_fail(&fail_iommu, 1);
  83. }
  84. static int __init fail_iommu_debugfs(void)
  85. {
  86. struct dentry *dir = fault_create_debugfs_attr("fail_iommu",
  87. NULL, &fail_iommu);
  88. return IS_ERR(dir) ? PTR_ERR(dir) : 0;
  89. }
  90. late_initcall(fail_iommu_debugfs);
  91. static ssize_t fail_iommu_show(struct device *dev,
  92. struct device_attribute *attr, char *buf)
  93. {
  94. return sprintf(buf, "%d\n", dev->archdata.fail_iommu);
  95. }
  96. static ssize_t fail_iommu_store(struct device *dev,
  97. struct device_attribute *attr, const char *buf,
  98. size_t count)
  99. {
  100. int i;
  101. if (count > 0 && sscanf(buf, "%d", &i) > 0)
  102. dev->archdata.fail_iommu = (i == 0) ? 0 : 1;
  103. return count;
  104. }
  105. static DEVICE_ATTR(fail_iommu, S_IRUGO|S_IWUSR, fail_iommu_show,
  106. fail_iommu_store);
  107. static int fail_iommu_bus_notify(struct notifier_block *nb,
  108. unsigned long action, void *data)
  109. {
  110. struct device *dev = data;
  111. if (action == BUS_NOTIFY_ADD_DEVICE) {
  112. if (device_create_file(dev, &dev_attr_fail_iommu))
  113. pr_warn("Unable to create IOMMU fault injection sysfs "
  114. "entries\n");
  115. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  116. device_remove_file(dev, &dev_attr_fail_iommu);
  117. }
  118. return 0;
  119. }
  120. static struct notifier_block fail_iommu_bus_notifier = {
  121. .notifier_call = fail_iommu_bus_notify
  122. };
  123. static int __init fail_iommu_setup(void)
  124. {
  125. #ifdef CONFIG_PCI
  126. bus_register_notifier(&pci_bus_type, &fail_iommu_bus_notifier);
  127. #endif
  128. #ifdef CONFIG_IBMVIO
  129. bus_register_notifier(&vio_bus_type, &fail_iommu_bus_notifier);
  130. #endif
  131. return 0;
  132. }
  133. /*
  134. * Must execute after PCI and VIO subsystem have initialised but before
  135. * devices are probed.
  136. */
  137. arch_initcall(fail_iommu_setup);
  138. #else
  139. static inline bool should_fail_iommu(struct device *dev)
  140. {
  141. return false;
  142. }
  143. #endif
  144. static unsigned long iommu_range_alloc(struct device *dev,
  145. struct iommu_table *tbl,
  146. unsigned long npages,
  147. unsigned long *handle,
  148. unsigned long mask,
  149. unsigned int align_order)
  150. {
  151. unsigned long n, end, start;
  152. unsigned long limit;
  153. int largealloc = npages > 15;
  154. int pass = 0;
  155. unsigned long align_mask;
  156. unsigned long boundary_size;
  157. unsigned long flags;
  158. unsigned int pool_nr;
  159. struct iommu_pool *pool;
  160. align_mask = 0xffffffffffffffffl >> (64 - align_order);
  161. /* This allocator was derived from x86_64's bit string search */
  162. /* Sanity check */
  163. if (unlikely(npages == 0)) {
  164. if (printk_ratelimit())
  165. WARN_ON(1);
  166. return DMA_ERROR_CODE;
  167. }
  168. if (should_fail_iommu(dev))
  169. return DMA_ERROR_CODE;
  170. /*
  171. * We don't need to disable preemption here because any CPU can
  172. * safely use any IOMMU pool.
  173. */
  174. pool_nr = __raw_get_cpu_var(iommu_pool_hash) & (tbl->nr_pools - 1);
  175. if (largealloc)
  176. pool = &(tbl->large_pool);
  177. else
  178. pool = &(tbl->pools[pool_nr]);
  179. spin_lock_irqsave(&(pool->lock), flags);
  180. again:
  181. if ((pass == 0) && handle && *handle &&
  182. (*handle >= pool->start) && (*handle < pool->end))
  183. start = *handle;
  184. else
  185. start = pool->hint;
  186. limit = pool->end;
  187. /* The case below can happen if we have a small segment appended
  188. * to a large, or when the previous alloc was at the very end of
  189. * the available space. If so, go back to the initial start.
  190. */
  191. if (start >= limit)
  192. start = pool->start;
  193. if (limit + tbl->it_offset > mask) {
  194. limit = mask - tbl->it_offset + 1;
  195. /* If we're constrained on address range, first try
  196. * at the masked hint to avoid O(n) search complexity,
  197. * but on second pass, start at 0 in pool 0.
  198. */
  199. if ((start & mask) >= limit || pass > 0) {
  200. spin_unlock(&(pool->lock));
  201. pool = &(tbl->pools[0]);
  202. spin_lock(&(pool->lock));
  203. start = pool->start;
  204. } else {
  205. start &= mask;
  206. }
  207. }
  208. if (dev)
  209. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  210. 1 << IOMMU_PAGE_SHIFT);
  211. else
  212. boundary_size = ALIGN(1UL << 32, 1 << IOMMU_PAGE_SHIFT);
  213. /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
  214. n = iommu_area_alloc(tbl->it_map, limit, start, npages,
  215. tbl->it_offset, boundary_size >> IOMMU_PAGE_SHIFT,
  216. align_mask);
  217. if (n == -1) {
  218. if (likely(pass == 0)) {
  219. /* First try the pool from the start */
  220. pool->hint = pool->start;
  221. pass++;
  222. goto again;
  223. } else if (pass <= tbl->nr_pools) {
  224. /* Now try scanning all the other pools */
  225. spin_unlock(&(pool->lock));
  226. pool_nr = (pool_nr + 1) & (tbl->nr_pools - 1);
  227. pool = &tbl->pools[pool_nr];
  228. spin_lock(&(pool->lock));
  229. pool->hint = pool->start;
  230. pass++;
  231. goto again;
  232. } else {
  233. /* Give up */
  234. spin_unlock_irqrestore(&(pool->lock), flags);
  235. return DMA_ERROR_CODE;
  236. }
  237. }
  238. end = n + npages;
  239. /* Bump the hint to a new block for small allocs. */
  240. if (largealloc) {
  241. /* Don't bump to new block to avoid fragmentation */
  242. pool->hint = end;
  243. } else {
  244. /* Overflow will be taken care of at the next allocation */
  245. pool->hint = (end + tbl->it_blocksize - 1) &
  246. ~(tbl->it_blocksize - 1);
  247. }
  248. /* Update handle for SG allocations */
  249. if (handle)
  250. *handle = end;
  251. spin_unlock_irqrestore(&(pool->lock), flags);
  252. return n;
  253. }
  254. static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
  255. void *page, unsigned int npages,
  256. enum dma_data_direction direction,
  257. unsigned long mask, unsigned int align_order,
  258. struct dma_attrs *attrs)
  259. {
  260. unsigned long entry;
  261. dma_addr_t ret = DMA_ERROR_CODE;
  262. int build_fail;
  263. entry = iommu_range_alloc(dev, tbl, npages, NULL, mask, align_order);
  264. if (unlikely(entry == DMA_ERROR_CODE))
  265. return DMA_ERROR_CODE;
  266. entry += tbl->it_offset; /* Offset into real TCE table */
  267. ret = entry << IOMMU_PAGE_SHIFT; /* Set the return dma address */
  268. /* Put the TCEs in the HW table */
  269. build_fail = ppc_md.tce_build(tbl, entry, npages,
  270. (unsigned long)page & IOMMU_PAGE_MASK,
  271. direction, attrs);
  272. /* ppc_md.tce_build() only returns non-zero for transient errors.
  273. * Clean up the table bitmap in this case and return
  274. * DMA_ERROR_CODE. For all other errors the functionality is
  275. * not altered.
  276. */
  277. if (unlikely(build_fail)) {
  278. __iommu_free(tbl, ret, npages);
  279. return DMA_ERROR_CODE;
  280. }
  281. /* Flush/invalidate TLB caches if necessary */
  282. if (ppc_md.tce_flush)
  283. ppc_md.tce_flush(tbl);
  284. /* Make sure updates are seen by hardware */
  285. mb();
  286. return ret;
  287. }
  288. static bool iommu_free_check(struct iommu_table *tbl, dma_addr_t dma_addr,
  289. unsigned int npages)
  290. {
  291. unsigned long entry, free_entry;
  292. entry = dma_addr >> IOMMU_PAGE_SHIFT;
  293. free_entry = entry - tbl->it_offset;
  294. if (((free_entry + npages) > tbl->it_size) ||
  295. (entry < tbl->it_offset)) {
  296. if (printk_ratelimit()) {
  297. printk(KERN_INFO "iommu_free: invalid entry\n");
  298. printk(KERN_INFO "\tentry = 0x%lx\n", entry);
  299. printk(KERN_INFO "\tdma_addr = 0x%llx\n", (u64)dma_addr);
  300. printk(KERN_INFO "\tTable = 0x%llx\n", (u64)tbl);
  301. printk(KERN_INFO "\tbus# = 0x%llx\n", (u64)tbl->it_busno);
  302. printk(KERN_INFO "\tsize = 0x%llx\n", (u64)tbl->it_size);
  303. printk(KERN_INFO "\tstartOff = 0x%llx\n", (u64)tbl->it_offset);
  304. printk(KERN_INFO "\tindex = 0x%llx\n", (u64)tbl->it_index);
  305. WARN_ON(1);
  306. }
  307. return false;
  308. }
  309. return true;
  310. }
  311. static struct iommu_pool *get_pool(struct iommu_table *tbl,
  312. unsigned long entry)
  313. {
  314. struct iommu_pool *p;
  315. unsigned long largepool_start = tbl->large_pool.start;
  316. /* The large pool is the last pool at the top of the table */
  317. if (entry >= largepool_start) {
  318. p = &tbl->large_pool;
  319. } else {
  320. unsigned int pool_nr = entry / tbl->poolsize;
  321. BUG_ON(pool_nr > tbl->nr_pools);
  322. p = &tbl->pools[pool_nr];
  323. }
  324. return p;
  325. }
  326. static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  327. unsigned int npages)
  328. {
  329. unsigned long entry, free_entry;
  330. unsigned long flags;
  331. struct iommu_pool *pool;
  332. entry = dma_addr >> IOMMU_PAGE_SHIFT;
  333. free_entry = entry - tbl->it_offset;
  334. pool = get_pool(tbl, free_entry);
  335. if (!iommu_free_check(tbl, dma_addr, npages))
  336. return;
  337. ppc_md.tce_free(tbl, entry, npages);
  338. spin_lock_irqsave(&(pool->lock), flags);
  339. bitmap_clear(tbl->it_map, free_entry, npages);
  340. spin_unlock_irqrestore(&(pool->lock), flags);
  341. }
  342. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  343. unsigned int npages)
  344. {
  345. __iommu_free(tbl, dma_addr, npages);
  346. /* Make sure TLB cache is flushed if the HW needs it. We do
  347. * not do an mb() here on purpose, it is not needed on any of
  348. * the current platforms.
  349. */
  350. if (ppc_md.tce_flush)
  351. ppc_md.tce_flush(tbl);
  352. }
  353. int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
  354. struct scatterlist *sglist, int nelems,
  355. unsigned long mask, enum dma_data_direction direction,
  356. struct dma_attrs *attrs)
  357. {
  358. dma_addr_t dma_next = 0, dma_addr;
  359. struct scatterlist *s, *outs, *segstart;
  360. int outcount, incount, i, build_fail = 0;
  361. unsigned int align;
  362. unsigned long handle;
  363. unsigned int max_seg_size;
  364. BUG_ON(direction == DMA_NONE);
  365. if ((nelems == 0) || !tbl)
  366. return 0;
  367. outs = s = segstart = &sglist[0];
  368. outcount = 1;
  369. incount = nelems;
  370. handle = 0;
  371. /* Init first segment length for backout at failure */
  372. outs->dma_length = 0;
  373. DBG("sg mapping %d elements:\n", nelems);
  374. max_seg_size = dma_get_max_seg_size(dev);
  375. for_each_sg(sglist, s, nelems, i) {
  376. unsigned long vaddr, npages, entry, slen;
  377. slen = s->length;
  378. /* Sanity check */
  379. if (slen == 0) {
  380. dma_next = 0;
  381. continue;
  382. }
  383. /* Allocate iommu entries for that segment */
  384. vaddr = (unsigned long) sg_virt(s);
  385. npages = iommu_num_pages(vaddr, slen, IOMMU_PAGE_SIZE);
  386. align = 0;
  387. if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && slen >= PAGE_SIZE &&
  388. (vaddr & ~PAGE_MASK) == 0)
  389. align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
  390. entry = iommu_range_alloc(dev, tbl, npages, &handle,
  391. mask >> IOMMU_PAGE_SHIFT, align);
  392. DBG(" - vaddr: %lx, size: %lx\n", vaddr, slen);
  393. /* Handle failure */
  394. if (unlikely(entry == DMA_ERROR_CODE)) {
  395. if (printk_ratelimit())
  396. dev_info(dev, "iommu_alloc failed, tbl %p "
  397. "vaddr %lx npages %lu\n", tbl, vaddr,
  398. npages);
  399. goto failure;
  400. }
  401. /* Convert entry to a dma_addr_t */
  402. entry += tbl->it_offset;
  403. dma_addr = entry << IOMMU_PAGE_SHIFT;
  404. dma_addr |= (s->offset & ~IOMMU_PAGE_MASK);
  405. DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n",
  406. npages, entry, dma_addr);
  407. /* Insert into HW table */
  408. build_fail = ppc_md.tce_build(tbl, entry, npages,
  409. vaddr & IOMMU_PAGE_MASK,
  410. direction, attrs);
  411. if(unlikely(build_fail))
  412. goto failure;
  413. /* If we are in an open segment, try merging */
  414. if (segstart != s) {
  415. DBG(" - trying merge...\n");
  416. /* We cannot merge if:
  417. * - allocated dma_addr isn't contiguous to previous allocation
  418. */
  419. if (novmerge || (dma_addr != dma_next) ||
  420. (outs->dma_length + s->length > max_seg_size)) {
  421. /* Can't merge: create a new segment */
  422. segstart = s;
  423. outcount++;
  424. outs = sg_next(outs);
  425. DBG(" can't merge, new segment.\n");
  426. } else {
  427. outs->dma_length += s->length;
  428. DBG(" merged, new len: %ux\n", outs->dma_length);
  429. }
  430. }
  431. if (segstart == s) {
  432. /* This is a new segment, fill entries */
  433. DBG(" - filling new segment.\n");
  434. outs->dma_address = dma_addr;
  435. outs->dma_length = slen;
  436. }
  437. /* Calculate next page pointer for contiguous check */
  438. dma_next = dma_addr + slen;
  439. DBG(" - dma next is: %lx\n", dma_next);
  440. }
  441. /* Flush/invalidate TLB caches if necessary */
  442. if (ppc_md.tce_flush)
  443. ppc_md.tce_flush(tbl);
  444. DBG("mapped %d elements:\n", outcount);
  445. /* For the sake of iommu_unmap_sg, we clear out the length in the
  446. * next entry of the sglist if we didn't fill the list completely
  447. */
  448. if (outcount < incount) {
  449. outs = sg_next(outs);
  450. outs->dma_address = DMA_ERROR_CODE;
  451. outs->dma_length = 0;
  452. }
  453. /* Make sure updates are seen by hardware */
  454. mb();
  455. return outcount;
  456. failure:
  457. for_each_sg(sglist, s, nelems, i) {
  458. if (s->dma_length != 0) {
  459. unsigned long vaddr, npages;
  460. vaddr = s->dma_address & IOMMU_PAGE_MASK;
  461. npages = iommu_num_pages(s->dma_address, s->dma_length,
  462. IOMMU_PAGE_SIZE);
  463. __iommu_free(tbl, vaddr, npages);
  464. s->dma_address = DMA_ERROR_CODE;
  465. s->dma_length = 0;
  466. }
  467. if (s == outs)
  468. break;
  469. }
  470. return 0;
  471. }
  472. void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
  473. int nelems, enum dma_data_direction direction,
  474. struct dma_attrs *attrs)
  475. {
  476. struct scatterlist *sg;
  477. BUG_ON(direction == DMA_NONE);
  478. if (!tbl)
  479. return;
  480. sg = sglist;
  481. while (nelems--) {
  482. unsigned int npages;
  483. dma_addr_t dma_handle = sg->dma_address;
  484. if (sg->dma_length == 0)
  485. break;
  486. npages = iommu_num_pages(dma_handle, sg->dma_length,
  487. IOMMU_PAGE_SIZE);
  488. __iommu_free(tbl, dma_handle, npages);
  489. sg = sg_next(sg);
  490. }
  491. /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
  492. * do not do an mb() here, the affected platforms do not need it
  493. * when freeing.
  494. */
  495. if (ppc_md.tce_flush)
  496. ppc_md.tce_flush(tbl);
  497. }
  498. static void iommu_table_clear(struct iommu_table *tbl)
  499. {
  500. /*
  501. * In case of firmware assisted dump system goes through clean
  502. * reboot process at the time of system crash. Hence it's safe to
  503. * clear the TCE entries if firmware assisted dump is active.
  504. */
  505. if (!is_kdump_kernel() || is_fadump_active()) {
  506. /* Clear the table in case firmware left allocations in it */
  507. ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
  508. return;
  509. }
  510. #ifdef CONFIG_CRASH_DUMP
  511. if (ppc_md.tce_get) {
  512. unsigned long index, tceval, tcecount = 0;
  513. /* Reserve the existing mappings left by the first kernel. */
  514. for (index = 0; index < tbl->it_size; index++) {
  515. tceval = ppc_md.tce_get(tbl, index + tbl->it_offset);
  516. /*
  517. * Freed TCE entry contains 0x7fffffffffffffff on JS20
  518. */
  519. if (tceval && (tceval != 0x7fffffffffffffffUL)) {
  520. __set_bit(index, tbl->it_map);
  521. tcecount++;
  522. }
  523. }
  524. if ((tbl->it_size - tcecount) < KDUMP_MIN_TCE_ENTRIES) {
  525. printk(KERN_WARNING "TCE table is full; freeing ");
  526. printk(KERN_WARNING "%d entries for the kdump boot\n",
  527. KDUMP_MIN_TCE_ENTRIES);
  528. for (index = tbl->it_size - KDUMP_MIN_TCE_ENTRIES;
  529. index < tbl->it_size; index++)
  530. __clear_bit(index, tbl->it_map);
  531. }
  532. }
  533. #endif
  534. }
  535. /*
  536. * Build a iommu_table structure. This contains a bit map which
  537. * is used to manage allocation of the tce space.
  538. */
  539. struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
  540. {
  541. unsigned long sz;
  542. static int welcomed = 0;
  543. struct page *page;
  544. unsigned int i;
  545. struct iommu_pool *p;
  546. /* number of bytes needed for the bitmap */
  547. sz = BITS_TO_LONGS(tbl->it_size) * sizeof(unsigned long);
  548. page = alloc_pages_node(nid, GFP_ATOMIC, get_order(sz));
  549. if (!page)
  550. panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
  551. tbl->it_map = page_address(page);
  552. memset(tbl->it_map, 0, sz);
  553. /*
  554. * Reserve page 0 so it will not be used for any mappings.
  555. * This avoids buggy drivers that consider page 0 to be invalid
  556. * to crash the machine or even lose data.
  557. */
  558. if (tbl->it_offset == 0)
  559. set_bit(0, tbl->it_map);
  560. /* We only split the IOMMU table if we have 1GB or more of space */
  561. if ((tbl->it_size << IOMMU_PAGE_SHIFT) >= (1UL * 1024 * 1024 * 1024))
  562. tbl->nr_pools = IOMMU_NR_POOLS;
  563. else
  564. tbl->nr_pools = 1;
  565. /* We reserve the top 1/4 of the table for large allocations */
  566. tbl->poolsize = (tbl->it_size * 3 / 4) / tbl->nr_pools;
  567. for (i = 0; i < tbl->nr_pools; i++) {
  568. p = &tbl->pools[i];
  569. spin_lock_init(&(p->lock));
  570. p->start = tbl->poolsize * i;
  571. p->hint = p->start;
  572. p->end = p->start + tbl->poolsize;
  573. }
  574. p = &tbl->large_pool;
  575. spin_lock_init(&(p->lock));
  576. p->start = tbl->poolsize * i;
  577. p->hint = p->start;
  578. p->end = tbl->it_size;
  579. iommu_table_clear(tbl);
  580. if (!welcomed) {
  581. printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",
  582. novmerge ? "disabled" : "enabled");
  583. welcomed = 1;
  584. }
  585. return tbl;
  586. }
  587. void iommu_free_table(struct iommu_table *tbl, const char *node_name)
  588. {
  589. unsigned long bitmap_sz;
  590. unsigned int order;
  591. if (!tbl || !tbl->it_map) {
  592. printk(KERN_ERR "%s: expected TCE map for %s\n", __func__,
  593. node_name);
  594. return;
  595. }
  596. /*
  597. * In case we have reserved the first bit, we should not emit
  598. * the warning below.
  599. */
  600. if (tbl->it_offset == 0)
  601. clear_bit(0, tbl->it_map);
  602. /* verify that table contains no entries */
  603. if (!bitmap_empty(tbl->it_map, tbl->it_size))
  604. pr_warn("%s: Unexpected TCEs for %s\n", __func__, node_name);
  605. /* calculate bitmap size in bytes */
  606. bitmap_sz = BITS_TO_LONGS(tbl->it_size) * sizeof(unsigned long);
  607. /* free bitmap */
  608. order = get_order(bitmap_sz);
  609. free_pages((unsigned long) tbl->it_map, order);
  610. /* free table */
  611. kfree(tbl);
  612. }
  613. /* Creates TCEs for a user provided buffer. The user buffer must be
  614. * contiguous real kernel storage (not vmalloc). The address passed here
  615. * comprises a page address and offset into that page. The dma_addr_t
  616. * returned will point to the same byte within the page as was passed in.
  617. */
  618. dma_addr_t iommu_map_page(struct device *dev, struct iommu_table *tbl,
  619. struct page *page, unsigned long offset, size_t size,
  620. unsigned long mask, enum dma_data_direction direction,
  621. struct dma_attrs *attrs)
  622. {
  623. dma_addr_t dma_handle = DMA_ERROR_CODE;
  624. void *vaddr;
  625. unsigned long uaddr;
  626. unsigned int npages, align;
  627. BUG_ON(direction == DMA_NONE);
  628. vaddr = page_address(page) + offset;
  629. uaddr = (unsigned long)vaddr;
  630. npages = iommu_num_pages(uaddr, size, IOMMU_PAGE_SIZE);
  631. if (tbl) {
  632. align = 0;
  633. if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && size >= PAGE_SIZE &&
  634. ((unsigned long)vaddr & ~PAGE_MASK) == 0)
  635. align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
  636. dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction,
  637. mask >> IOMMU_PAGE_SHIFT, align,
  638. attrs);
  639. if (dma_handle == DMA_ERROR_CODE) {
  640. if (printk_ratelimit()) {
  641. dev_info(dev, "iommu_alloc failed, tbl %p "
  642. "vaddr %p npages %d\n", tbl, vaddr,
  643. npages);
  644. }
  645. } else
  646. dma_handle |= (uaddr & ~IOMMU_PAGE_MASK);
  647. }
  648. return dma_handle;
  649. }
  650. void iommu_unmap_page(struct iommu_table *tbl, dma_addr_t dma_handle,
  651. size_t size, enum dma_data_direction direction,
  652. struct dma_attrs *attrs)
  653. {
  654. unsigned int npages;
  655. BUG_ON(direction == DMA_NONE);
  656. if (tbl) {
  657. npages = iommu_num_pages(dma_handle, size, IOMMU_PAGE_SIZE);
  658. iommu_free(tbl, dma_handle, npages);
  659. }
  660. }
  661. /* Allocates a contiguous real buffer and creates mappings over it.
  662. * Returns the virtual address of the buffer and sets dma_handle
  663. * to the dma address (mapping) of the first page.
  664. */
  665. void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,
  666. size_t size, dma_addr_t *dma_handle,
  667. unsigned long mask, gfp_t flag, int node)
  668. {
  669. void *ret = NULL;
  670. dma_addr_t mapping;
  671. unsigned int order;
  672. unsigned int nio_pages, io_order;
  673. struct page *page;
  674. size = PAGE_ALIGN(size);
  675. order = get_order(size);
  676. /*
  677. * Client asked for way too much space. This is checked later
  678. * anyway. It is easier to debug here for the drivers than in
  679. * the tce tables.
  680. */
  681. if (order >= IOMAP_MAX_ORDER) {
  682. dev_info(dev, "iommu_alloc_consistent size too large: 0x%lx\n",
  683. size);
  684. return NULL;
  685. }
  686. if (!tbl)
  687. return NULL;
  688. /* Alloc enough pages (and possibly more) */
  689. page = alloc_pages_node(node, flag, order);
  690. if (!page)
  691. return NULL;
  692. ret = page_address(page);
  693. memset(ret, 0, size);
  694. /* Set up tces to cover the allocated range */
  695. nio_pages = size >> IOMMU_PAGE_SHIFT;
  696. io_order = get_iommu_order(size);
  697. mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL,
  698. mask >> IOMMU_PAGE_SHIFT, io_order, NULL);
  699. if (mapping == DMA_ERROR_CODE) {
  700. free_pages((unsigned long)ret, order);
  701. return NULL;
  702. }
  703. *dma_handle = mapping;
  704. return ret;
  705. }
  706. void iommu_free_coherent(struct iommu_table *tbl, size_t size,
  707. void *vaddr, dma_addr_t dma_handle)
  708. {
  709. if (tbl) {
  710. unsigned int nio_pages;
  711. size = PAGE_ALIGN(size);
  712. nio_pages = size >> IOMMU_PAGE_SHIFT;
  713. iommu_free(tbl, dma_handle, nio_pages);
  714. size = PAGE_ALIGN(size);
  715. free_pages((unsigned long)vaddr, get_order(size));
  716. }
  717. }