iommu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. start = *handle;
  183. else
  184. start = pool->hint;
  185. limit = pool->end;
  186. /* The case below can happen if we have a small segment appended
  187. * to a large, or when the previous alloc was at the very end of
  188. * the available space. If so, go back to the initial start.
  189. */
  190. if (start >= limit)
  191. start = pool->start;
  192. if (limit + tbl->it_offset > mask) {
  193. limit = mask - tbl->it_offset + 1;
  194. /* If we're constrained on address range, first try
  195. * at the masked hint to avoid O(n) search complexity,
  196. * but on second pass, start at 0 in pool 0.
  197. */
  198. if ((start & mask) >= limit || pass > 0) {
  199. pool = &(tbl->pools[0]);
  200. start = pool->start;
  201. } else {
  202. start &= mask;
  203. }
  204. }
  205. if (dev)
  206. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  207. 1 << IOMMU_PAGE_SHIFT);
  208. else
  209. boundary_size = ALIGN(1UL << 32, 1 << IOMMU_PAGE_SHIFT);
  210. /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
  211. n = iommu_area_alloc(tbl->it_map, limit, start, npages,
  212. tbl->it_offset, boundary_size >> IOMMU_PAGE_SHIFT,
  213. align_mask);
  214. if (n == -1) {
  215. if (likely(pass == 0)) {
  216. /* First try the pool from the start */
  217. pool->hint = pool->start;
  218. pass++;
  219. goto again;
  220. } else if (pass <= tbl->nr_pools) {
  221. /* Now try scanning all the other pools */
  222. spin_unlock(&(pool->lock));
  223. pool_nr = (pool_nr + 1) & (tbl->nr_pools - 1);
  224. pool = &tbl->pools[pool_nr];
  225. spin_lock(&(pool->lock));
  226. pool->hint = pool->start;
  227. pass++;
  228. goto again;
  229. } else {
  230. /* Give up */
  231. spin_unlock_irqrestore(&(pool->lock), flags);
  232. return DMA_ERROR_CODE;
  233. }
  234. }
  235. end = n + npages;
  236. /* Bump the hint to a new block for small allocs. */
  237. if (largealloc) {
  238. /* Don't bump to new block to avoid fragmentation */
  239. pool->hint = end;
  240. } else {
  241. /* Overflow will be taken care of at the next allocation */
  242. pool->hint = (end + tbl->it_blocksize - 1) &
  243. ~(tbl->it_blocksize - 1);
  244. }
  245. /* Update handle for SG allocations */
  246. if (handle)
  247. *handle = end;
  248. spin_unlock_irqrestore(&(pool->lock), flags);
  249. return n;
  250. }
  251. static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
  252. void *page, unsigned int npages,
  253. enum dma_data_direction direction,
  254. unsigned long mask, unsigned int align_order,
  255. struct dma_attrs *attrs)
  256. {
  257. unsigned long entry;
  258. dma_addr_t ret = DMA_ERROR_CODE;
  259. int build_fail;
  260. entry = iommu_range_alloc(dev, tbl, npages, NULL, mask, align_order);
  261. if (unlikely(entry == DMA_ERROR_CODE))
  262. return DMA_ERROR_CODE;
  263. entry += tbl->it_offset; /* Offset into real TCE table */
  264. ret = entry << IOMMU_PAGE_SHIFT; /* Set the return dma address */
  265. /* Put the TCEs in the HW table */
  266. build_fail = ppc_md.tce_build(tbl, entry, npages,
  267. (unsigned long)page & IOMMU_PAGE_MASK,
  268. direction, attrs);
  269. /* ppc_md.tce_build() only returns non-zero for transient errors.
  270. * Clean up the table bitmap in this case and return
  271. * DMA_ERROR_CODE. For all other errors the functionality is
  272. * not altered.
  273. */
  274. if (unlikely(build_fail)) {
  275. __iommu_free(tbl, ret, npages);
  276. return DMA_ERROR_CODE;
  277. }
  278. /* Flush/invalidate TLB caches if necessary */
  279. if (ppc_md.tce_flush)
  280. ppc_md.tce_flush(tbl);
  281. /* Make sure updates are seen by hardware */
  282. mb();
  283. return ret;
  284. }
  285. static bool iommu_free_check(struct iommu_table *tbl, dma_addr_t dma_addr,
  286. unsigned int npages)
  287. {
  288. unsigned long entry, free_entry;
  289. entry = dma_addr >> IOMMU_PAGE_SHIFT;
  290. free_entry = entry - tbl->it_offset;
  291. if (((free_entry + npages) > tbl->it_size) ||
  292. (entry < tbl->it_offset)) {
  293. if (printk_ratelimit()) {
  294. printk(KERN_INFO "iommu_free: invalid entry\n");
  295. printk(KERN_INFO "\tentry = 0x%lx\n", entry);
  296. printk(KERN_INFO "\tdma_addr = 0x%llx\n", (u64)dma_addr);
  297. printk(KERN_INFO "\tTable = 0x%llx\n", (u64)tbl);
  298. printk(KERN_INFO "\tbus# = 0x%llx\n", (u64)tbl->it_busno);
  299. printk(KERN_INFO "\tsize = 0x%llx\n", (u64)tbl->it_size);
  300. printk(KERN_INFO "\tstartOff = 0x%llx\n", (u64)tbl->it_offset);
  301. printk(KERN_INFO "\tindex = 0x%llx\n", (u64)tbl->it_index);
  302. WARN_ON(1);
  303. }
  304. return false;
  305. }
  306. return true;
  307. }
  308. static struct iommu_pool *get_pool(struct iommu_table *tbl,
  309. unsigned long entry)
  310. {
  311. struct iommu_pool *p;
  312. unsigned long largepool_start = tbl->large_pool.start;
  313. /* The large pool is the last pool at the top of the table */
  314. if (entry >= largepool_start) {
  315. p = &tbl->large_pool;
  316. } else {
  317. unsigned int pool_nr = entry / tbl->poolsize;
  318. BUG_ON(pool_nr > tbl->nr_pools);
  319. p = &tbl->pools[pool_nr];
  320. }
  321. return p;
  322. }
  323. static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  324. unsigned int npages)
  325. {
  326. unsigned long entry, free_entry;
  327. unsigned long flags;
  328. struct iommu_pool *pool;
  329. entry = dma_addr >> IOMMU_PAGE_SHIFT;
  330. free_entry = entry - tbl->it_offset;
  331. pool = get_pool(tbl, free_entry);
  332. if (!iommu_free_check(tbl, dma_addr, npages))
  333. return;
  334. ppc_md.tce_free(tbl, entry, npages);
  335. spin_lock_irqsave(&(pool->lock), flags);
  336. bitmap_clear(tbl->it_map, free_entry, npages);
  337. spin_unlock_irqrestore(&(pool->lock), flags);
  338. }
  339. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  340. unsigned int npages)
  341. {
  342. __iommu_free(tbl, dma_addr, npages);
  343. /* Make sure TLB cache is flushed if the HW needs it. We do
  344. * not do an mb() here on purpose, it is not needed on any of
  345. * the current platforms.
  346. */
  347. if (ppc_md.tce_flush)
  348. ppc_md.tce_flush(tbl);
  349. }
  350. int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
  351. struct scatterlist *sglist, int nelems,
  352. unsigned long mask, enum dma_data_direction direction,
  353. struct dma_attrs *attrs)
  354. {
  355. dma_addr_t dma_next = 0, dma_addr;
  356. struct scatterlist *s, *outs, *segstart;
  357. int outcount, incount, i, build_fail = 0;
  358. unsigned int align;
  359. unsigned long handle;
  360. unsigned int max_seg_size;
  361. BUG_ON(direction == DMA_NONE);
  362. if ((nelems == 0) || !tbl)
  363. return 0;
  364. outs = s = segstart = &sglist[0];
  365. outcount = 1;
  366. incount = nelems;
  367. handle = 0;
  368. /* Init first segment length for backout at failure */
  369. outs->dma_length = 0;
  370. DBG("sg mapping %d elements:\n", nelems);
  371. max_seg_size = dma_get_max_seg_size(dev);
  372. for_each_sg(sglist, s, nelems, i) {
  373. unsigned long vaddr, npages, entry, slen;
  374. slen = s->length;
  375. /* Sanity check */
  376. if (slen == 0) {
  377. dma_next = 0;
  378. continue;
  379. }
  380. /* Allocate iommu entries for that segment */
  381. vaddr = (unsigned long) sg_virt(s);
  382. npages = iommu_num_pages(vaddr, slen, IOMMU_PAGE_SIZE);
  383. align = 0;
  384. if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && slen >= PAGE_SIZE &&
  385. (vaddr & ~PAGE_MASK) == 0)
  386. align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
  387. entry = iommu_range_alloc(dev, tbl, npages, &handle,
  388. mask >> IOMMU_PAGE_SHIFT, align);
  389. DBG(" - vaddr: %lx, size: %lx\n", vaddr, slen);
  390. /* Handle failure */
  391. if (unlikely(entry == DMA_ERROR_CODE)) {
  392. if (printk_ratelimit())
  393. dev_info(dev, "iommu_alloc failed, tbl %p "
  394. "vaddr %lx npages %lu\n", tbl, vaddr,
  395. npages);
  396. goto failure;
  397. }
  398. /* Convert entry to a dma_addr_t */
  399. entry += tbl->it_offset;
  400. dma_addr = entry << IOMMU_PAGE_SHIFT;
  401. dma_addr |= (s->offset & ~IOMMU_PAGE_MASK);
  402. DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n",
  403. npages, entry, dma_addr);
  404. /* Insert into HW table */
  405. build_fail = ppc_md.tce_build(tbl, entry, npages,
  406. vaddr & IOMMU_PAGE_MASK,
  407. direction, attrs);
  408. if(unlikely(build_fail))
  409. goto failure;
  410. /* If we are in an open segment, try merging */
  411. if (segstart != s) {
  412. DBG(" - trying merge...\n");
  413. /* We cannot merge if:
  414. * - allocated dma_addr isn't contiguous to previous allocation
  415. */
  416. if (novmerge || (dma_addr != dma_next) ||
  417. (outs->dma_length + s->length > max_seg_size)) {
  418. /* Can't merge: create a new segment */
  419. segstart = s;
  420. outcount++;
  421. outs = sg_next(outs);
  422. DBG(" can't merge, new segment.\n");
  423. } else {
  424. outs->dma_length += s->length;
  425. DBG(" merged, new len: %ux\n", outs->dma_length);
  426. }
  427. }
  428. if (segstart == s) {
  429. /* This is a new segment, fill entries */
  430. DBG(" - filling new segment.\n");
  431. outs->dma_address = dma_addr;
  432. outs->dma_length = slen;
  433. }
  434. /* Calculate next page pointer for contiguous check */
  435. dma_next = dma_addr + slen;
  436. DBG(" - dma next is: %lx\n", dma_next);
  437. }
  438. /* Flush/invalidate TLB caches if necessary */
  439. if (ppc_md.tce_flush)
  440. ppc_md.tce_flush(tbl);
  441. DBG("mapped %d elements:\n", outcount);
  442. /* For the sake of iommu_unmap_sg, we clear out the length in the
  443. * next entry of the sglist if we didn't fill the list completely
  444. */
  445. if (outcount < incount) {
  446. outs = sg_next(outs);
  447. outs->dma_address = DMA_ERROR_CODE;
  448. outs->dma_length = 0;
  449. }
  450. /* Make sure updates are seen by hardware */
  451. mb();
  452. return outcount;
  453. failure:
  454. for_each_sg(sglist, s, nelems, i) {
  455. if (s->dma_length != 0) {
  456. unsigned long vaddr, npages;
  457. vaddr = s->dma_address & IOMMU_PAGE_MASK;
  458. npages = iommu_num_pages(s->dma_address, s->dma_length,
  459. IOMMU_PAGE_SIZE);
  460. __iommu_free(tbl, vaddr, npages);
  461. s->dma_address = DMA_ERROR_CODE;
  462. s->dma_length = 0;
  463. }
  464. if (s == outs)
  465. break;
  466. }
  467. return 0;
  468. }
  469. void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
  470. int nelems, enum dma_data_direction direction,
  471. struct dma_attrs *attrs)
  472. {
  473. struct scatterlist *sg;
  474. BUG_ON(direction == DMA_NONE);
  475. if (!tbl)
  476. return;
  477. sg = sglist;
  478. while (nelems--) {
  479. unsigned int npages;
  480. dma_addr_t dma_handle = sg->dma_address;
  481. if (sg->dma_length == 0)
  482. break;
  483. npages = iommu_num_pages(dma_handle, sg->dma_length,
  484. IOMMU_PAGE_SIZE);
  485. __iommu_free(tbl, dma_handle, npages);
  486. sg = sg_next(sg);
  487. }
  488. /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
  489. * do not do an mb() here, the affected platforms do not need it
  490. * when freeing.
  491. */
  492. if (ppc_md.tce_flush)
  493. ppc_md.tce_flush(tbl);
  494. }
  495. static void iommu_table_clear(struct iommu_table *tbl)
  496. {
  497. /*
  498. * In case of firmware assisted dump system goes through clean
  499. * reboot process at the time of system crash. Hence it's safe to
  500. * clear the TCE entries if firmware assisted dump is active.
  501. */
  502. if (!is_kdump_kernel() || is_fadump_active()) {
  503. /* Clear the table in case firmware left allocations in it */
  504. ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
  505. return;
  506. }
  507. #ifdef CONFIG_CRASH_DUMP
  508. if (ppc_md.tce_get) {
  509. unsigned long index, tceval, tcecount = 0;
  510. /* Reserve the existing mappings left by the first kernel. */
  511. for (index = 0; index < tbl->it_size; index++) {
  512. tceval = ppc_md.tce_get(tbl, index + tbl->it_offset);
  513. /*
  514. * Freed TCE entry contains 0x7fffffffffffffff on JS20
  515. */
  516. if (tceval && (tceval != 0x7fffffffffffffffUL)) {
  517. __set_bit(index, tbl->it_map);
  518. tcecount++;
  519. }
  520. }
  521. if ((tbl->it_size - tcecount) < KDUMP_MIN_TCE_ENTRIES) {
  522. printk(KERN_WARNING "TCE table is full; freeing ");
  523. printk(KERN_WARNING "%d entries for the kdump boot\n",
  524. KDUMP_MIN_TCE_ENTRIES);
  525. for (index = tbl->it_size - KDUMP_MIN_TCE_ENTRIES;
  526. index < tbl->it_size; index++)
  527. __clear_bit(index, tbl->it_map);
  528. }
  529. }
  530. #endif
  531. }
  532. /*
  533. * Build a iommu_table structure. This contains a bit map which
  534. * is used to manage allocation of the tce space.
  535. */
  536. struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
  537. {
  538. unsigned long sz;
  539. static int welcomed = 0;
  540. struct page *page;
  541. unsigned int i;
  542. struct iommu_pool *p;
  543. /* number of bytes needed for the bitmap */
  544. sz = (tbl->it_size + 7) >> 3;
  545. page = alloc_pages_node(nid, GFP_ATOMIC, get_order(sz));
  546. if (!page)
  547. panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
  548. tbl->it_map = page_address(page);
  549. memset(tbl->it_map, 0, sz);
  550. /*
  551. * Reserve page 0 so it will not be used for any mappings.
  552. * This avoids buggy drivers that consider page 0 to be invalid
  553. * to crash the machine or even lose data.
  554. */
  555. if (tbl->it_offset == 0)
  556. set_bit(0, tbl->it_map);
  557. /* We only split the IOMMU table if we have 1GB or more of space */
  558. if ((tbl->it_size << IOMMU_PAGE_SHIFT) >= (1UL * 1024 * 1024 * 1024))
  559. tbl->nr_pools = IOMMU_NR_POOLS;
  560. else
  561. tbl->nr_pools = 1;
  562. /* We reserve the top 1/4 of the table for large allocations */
  563. tbl->poolsize = (tbl->it_size * 3 / 4) / tbl->nr_pools;
  564. for (i = 0; i < tbl->nr_pools; i++) {
  565. p = &tbl->pools[i];
  566. spin_lock_init(&(p->lock));
  567. p->start = tbl->poolsize * i;
  568. p->hint = p->start;
  569. p->end = p->start + tbl->poolsize;
  570. }
  571. p = &tbl->large_pool;
  572. spin_lock_init(&(p->lock));
  573. p->start = tbl->poolsize * i;
  574. p->hint = p->start;
  575. p->end = tbl->it_size;
  576. iommu_table_clear(tbl);
  577. if (!welcomed) {
  578. printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",
  579. novmerge ? "disabled" : "enabled");
  580. welcomed = 1;
  581. }
  582. return tbl;
  583. }
  584. void iommu_free_table(struct iommu_table *tbl, const char *node_name)
  585. {
  586. unsigned long bitmap_sz, i;
  587. unsigned int order;
  588. if (!tbl || !tbl->it_map) {
  589. printk(KERN_ERR "%s: expected TCE map for %s\n", __func__,
  590. node_name);
  591. return;
  592. }
  593. /* verify that table contains no entries */
  594. /* it_size is in entries, and we're examining 64 at a time */
  595. for (i = 0; i < (tbl->it_size/64); i++) {
  596. if (tbl->it_map[i] != 0) {
  597. printk(KERN_WARNING "%s: Unexpected TCEs for %s\n",
  598. __func__, node_name);
  599. break;
  600. }
  601. }
  602. /* calculate bitmap size in bytes */
  603. bitmap_sz = (tbl->it_size + 7) / 8;
  604. /* free bitmap */
  605. order = get_order(bitmap_sz);
  606. free_pages((unsigned long) tbl->it_map, order);
  607. /* free table */
  608. kfree(tbl);
  609. }
  610. /* Creates TCEs for a user provided buffer. The user buffer must be
  611. * contiguous real kernel storage (not vmalloc). The address passed here
  612. * comprises a page address and offset into that page. The dma_addr_t
  613. * returned will point to the same byte within the page as was passed in.
  614. */
  615. dma_addr_t iommu_map_page(struct device *dev, struct iommu_table *tbl,
  616. struct page *page, unsigned long offset, size_t size,
  617. unsigned long mask, enum dma_data_direction direction,
  618. struct dma_attrs *attrs)
  619. {
  620. dma_addr_t dma_handle = DMA_ERROR_CODE;
  621. void *vaddr;
  622. unsigned long uaddr;
  623. unsigned int npages, align;
  624. BUG_ON(direction == DMA_NONE);
  625. vaddr = page_address(page) + offset;
  626. uaddr = (unsigned long)vaddr;
  627. npages = iommu_num_pages(uaddr, size, IOMMU_PAGE_SIZE);
  628. if (tbl) {
  629. align = 0;
  630. if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && size >= PAGE_SIZE &&
  631. ((unsigned long)vaddr & ~PAGE_MASK) == 0)
  632. align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
  633. dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction,
  634. mask >> IOMMU_PAGE_SHIFT, align,
  635. attrs);
  636. if (dma_handle == DMA_ERROR_CODE) {
  637. if (printk_ratelimit()) {
  638. dev_info(dev, "iommu_alloc failed, tbl %p "
  639. "vaddr %p npages %d\n", tbl, vaddr,
  640. npages);
  641. }
  642. } else
  643. dma_handle |= (uaddr & ~IOMMU_PAGE_MASK);
  644. }
  645. return dma_handle;
  646. }
  647. void iommu_unmap_page(struct iommu_table *tbl, dma_addr_t dma_handle,
  648. size_t size, enum dma_data_direction direction,
  649. struct dma_attrs *attrs)
  650. {
  651. unsigned int npages;
  652. BUG_ON(direction == DMA_NONE);
  653. if (tbl) {
  654. npages = iommu_num_pages(dma_handle, size, IOMMU_PAGE_SIZE);
  655. iommu_free(tbl, dma_handle, npages);
  656. }
  657. }
  658. /* Allocates a contiguous real buffer and creates mappings over it.
  659. * Returns the virtual address of the buffer and sets dma_handle
  660. * to the dma address (mapping) of the first page.
  661. */
  662. void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,
  663. size_t size, dma_addr_t *dma_handle,
  664. unsigned long mask, gfp_t flag, int node)
  665. {
  666. void *ret = NULL;
  667. dma_addr_t mapping;
  668. unsigned int order;
  669. unsigned int nio_pages, io_order;
  670. struct page *page;
  671. size = PAGE_ALIGN(size);
  672. order = get_order(size);
  673. /*
  674. * Client asked for way too much space. This is checked later
  675. * anyway. It is easier to debug here for the drivers than in
  676. * the tce tables.
  677. */
  678. if (order >= IOMAP_MAX_ORDER) {
  679. dev_info(dev, "iommu_alloc_consistent size too large: 0x%lx\n",
  680. size);
  681. return NULL;
  682. }
  683. if (!tbl)
  684. return NULL;
  685. /* Alloc enough pages (and possibly more) */
  686. page = alloc_pages_node(node, flag, order);
  687. if (!page)
  688. return NULL;
  689. ret = page_address(page);
  690. memset(ret, 0, size);
  691. /* Set up tces to cover the allocated range */
  692. nio_pages = size >> IOMMU_PAGE_SHIFT;
  693. io_order = get_iommu_order(size);
  694. mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL,
  695. mask >> IOMMU_PAGE_SHIFT, io_order, NULL);
  696. if (mapping == DMA_ERROR_CODE) {
  697. free_pages((unsigned long)ret, order);
  698. return NULL;
  699. }
  700. *dma_handle = mapping;
  701. return ret;
  702. }
  703. void iommu_free_coherent(struct iommu_table *tbl, size_t size,
  704. void *vaddr, dma_addr_t dma_handle)
  705. {
  706. if (tbl) {
  707. unsigned int nio_pages;
  708. size = PAGE_ALIGN(size);
  709. nio_pages = size >> IOMMU_PAGE_SHIFT;
  710. iommu_free(tbl, dma_handle, nio_pages);
  711. size = PAGE_ALIGN(size);
  712. free_pages((unsigned long)vaddr, get_order(size));
  713. }
  714. }