iommu.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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 <linux/iommu.h>
  38. #include <linux/sched.h>
  39. #include <asm/io.h>
  40. #include <asm/prom.h>
  41. #include <asm/iommu.h>
  42. #include <asm/pci-bridge.h>
  43. #include <asm/machdep.h>
  44. #include <asm/kdump.h>
  45. #include <asm/fadump.h>
  46. #include <asm/vio.h>
  47. #include <asm/tce.h>
  48. #define DBG(...)
  49. static int novmerge;
  50. static void __iommu_free(struct iommu_table *, dma_addr_t, unsigned int);
  51. static int __init setup_iommu(char *str)
  52. {
  53. if (!strcmp(str, "novmerge"))
  54. novmerge = 1;
  55. else if (!strcmp(str, "vmerge"))
  56. novmerge = 0;
  57. return 1;
  58. }
  59. __setup("iommu=", setup_iommu);
  60. static DEFINE_PER_CPU(unsigned int, iommu_pool_hash);
  61. /*
  62. * We precalculate the hash to avoid doing it on every allocation.
  63. *
  64. * The hash is important to spread CPUs across all the pools. For example,
  65. * on a POWER7 with 4 way SMT we want interrupts on the primary threads and
  66. * with 4 pools all primary threads would map to the same pool.
  67. */
  68. static int __init setup_iommu_pool_hash(void)
  69. {
  70. unsigned int i;
  71. for_each_possible_cpu(i)
  72. per_cpu(iommu_pool_hash, i) = hash_32(i, IOMMU_POOL_HASHBITS);
  73. return 0;
  74. }
  75. subsys_initcall(setup_iommu_pool_hash);
  76. #ifdef CONFIG_FAIL_IOMMU
  77. static DECLARE_FAULT_ATTR(fail_iommu);
  78. static int __init setup_fail_iommu(char *str)
  79. {
  80. return setup_fault_attr(&fail_iommu, str);
  81. }
  82. __setup("fail_iommu=", setup_fail_iommu);
  83. static bool should_fail_iommu(struct device *dev)
  84. {
  85. return dev->archdata.fail_iommu && should_fail(&fail_iommu, 1);
  86. }
  87. static int __init fail_iommu_debugfs(void)
  88. {
  89. struct dentry *dir = fault_create_debugfs_attr("fail_iommu",
  90. NULL, &fail_iommu);
  91. return PTR_ERR_OR_ZERO(dir);
  92. }
  93. late_initcall(fail_iommu_debugfs);
  94. static ssize_t fail_iommu_show(struct device *dev,
  95. struct device_attribute *attr, char *buf)
  96. {
  97. return sprintf(buf, "%d\n", dev->archdata.fail_iommu);
  98. }
  99. static ssize_t fail_iommu_store(struct device *dev,
  100. struct device_attribute *attr, const char *buf,
  101. size_t count)
  102. {
  103. int i;
  104. if (count > 0 && sscanf(buf, "%d", &i) > 0)
  105. dev->archdata.fail_iommu = (i == 0) ? 0 : 1;
  106. return count;
  107. }
  108. static DEVICE_ATTR_RW(fail_iommu);
  109. static int fail_iommu_bus_notify(struct notifier_block *nb,
  110. unsigned long action, void *data)
  111. {
  112. struct device *dev = data;
  113. if (action == BUS_NOTIFY_ADD_DEVICE) {
  114. if (device_create_file(dev, &dev_attr_fail_iommu))
  115. pr_warn("Unable to create IOMMU fault injection sysfs "
  116. "entries\n");
  117. } else if (action == BUS_NOTIFY_DEL_DEVICE) {
  118. device_remove_file(dev, &dev_attr_fail_iommu);
  119. }
  120. return 0;
  121. }
  122. static struct notifier_block fail_iommu_bus_notifier = {
  123. .notifier_call = fail_iommu_bus_notify
  124. };
  125. static int __init fail_iommu_setup(void)
  126. {
  127. #ifdef CONFIG_PCI
  128. bus_register_notifier(&pci_bus_type, &fail_iommu_bus_notifier);
  129. #endif
  130. #ifdef CONFIG_IBMVIO
  131. bus_register_notifier(&vio_bus_type, &fail_iommu_bus_notifier);
  132. #endif
  133. return 0;
  134. }
  135. /*
  136. * Must execute after PCI and VIO subsystem have initialised but before
  137. * devices are probed.
  138. */
  139. arch_initcall(fail_iommu_setup);
  140. #else
  141. static inline bool should_fail_iommu(struct device *dev)
  142. {
  143. return false;
  144. }
  145. #endif
  146. static unsigned long iommu_range_alloc(struct device *dev,
  147. struct iommu_table *tbl,
  148. unsigned long npages,
  149. unsigned long *handle,
  150. unsigned long mask,
  151. unsigned int align_order)
  152. {
  153. unsigned long n, end, start;
  154. unsigned long limit;
  155. int largealloc = npages > 15;
  156. int pass = 0;
  157. unsigned long align_mask;
  158. unsigned long boundary_size;
  159. unsigned long flags;
  160. unsigned int pool_nr;
  161. struct iommu_pool *pool;
  162. align_mask = (1ull << align_order) - 1;
  163. /* This allocator was derived from x86_64's bit string search */
  164. /* Sanity check */
  165. if (unlikely(npages == 0)) {
  166. if (printk_ratelimit())
  167. WARN_ON(1);
  168. return IOMMU_MAPPING_ERROR;
  169. }
  170. if (should_fail_iommu(dev))
  171. return IOMMU_MAPPING_ERROR;
  172. /*
  173. * We don't need to disable preemption here because any CPU can
  174. * safely use any IOMMU pool.
  175. */
  176. pool_nr = raw_cpu_read(iommu_pool_hash) & (tbl->nr_pools - 1);
  177. if (largealloc)
  178. pool = &(tbl->large_pool);
  179. else
  180. pool = &(tbl->pools[pool_nr]);
  181. spin_lock_irqsave(&(pool->lock), flags);
  182. again:
  183. if ((pass == 0) && handle && *handle &&
  184. (*handle >= pool->start) && (*handle < pool->end))
  185. start = *handle;
  186. else
  187. start = pool->hint;
  188. limit = pool->end;
  189. /* The case below can happen if we have a small segment appended
  190. * to a large, or when the previous alloc was at the very end of
  191. * the available space. If so, go back to the initial start.
  192. */
  193. if (start >= limit)
  194. start = pool->start;
  195. if (limit + tbl->it_offset > mask) {
  196. limit = mask - tbl->it_offset + 1;
  197. /* If we're constrained on address range, first try
  198. * at the masked hint to avoid O(n) search complexity,
  199. * but on second pass, start at 0 in pool 0.
  200. */
  201. if ((start & mask) >= limit || pass > 0) {
  202. spin_unlock(&(pool->lock));
  203. pool = &(tbl->pools[0]);
  204. spin_lock(&(pool->lock));
  205. start = pool->start;
  206. } else {
  207. start &= mask;
  208. }
  209. }
  210. if (dev)
  211. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  212. 1 << tbl->it_page_shift);
  213. else
  214. boundary_size = ALIGN(1UL << 32, 1 << tbl->it_page_shift);
  215. /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
  216. n = iommu_area_alloc(tbl->it_map, limit, start, npages, tbl->it_offset,
  217. boundary_size >> tbl->it_page_shift, align_mask);
  218. if (n == -1) {
  219. if (likely(pass == 0)) {
  220. /* First try the pool from the start */
  221. pool->hint = pool->start;
  222. pass++;
  223. goto again;
  224. } else if (pass <= tbl->nr_pools) {
  225. /* Now try scanning all the other pools */
  226. spin_unlock(&(pool->lock));
  227. pool_nr = (pool_nr + 1) & (tbl->nr_pools - 1);
  228. pool = &tbl->pools[pool_nr];
  229. spin_lock(&(pool->lock));
  230. pool->hint = pool->start;
  231. pass++;
  232. goto again;
  233. } else {
  234. /* Give up */
  235. spin_unlock_irqrestore(&(pool->lock), flags);
  236. return IOMMU_MAPPING_ERROR;
  237. }
  238. }
  239. end = n + npages;
  240. /* Bump the hint to a new block for small allocs. */
  241. if (largealloc) {
  242. /* Don't bump to new block to avoid fragmentation */
  243. pool->hint = end;
  244. } else {
  245. /* Overflow will be taken care of at the next allocation */
  246. pool->hint = (end + tbl->it_blocksize - 1) &
  247. ~(tbl->it_blocksize - 1);
  248. }
  249. /* Update handle for SG allocations */
  250. if (handle)
  251. *handle = end;
  252. spin_unlock_irqrestore(&(pool->lock), flags);
  253. return n;
  254. }
  255. static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
  256. void *page, unsigned int npages,
  257. enum dma_data_direction direction,
  258. unsigned long mask, unsigned int align_order,
  259. unsigned long attrs)
  260. {
  261. unsigned long entry;
  262. dma_addr_t ret = IOMMU_MAPPING_ERROR;
  263. int build_fail;
  264. entry = iommu_range_alloc(dev, tbl, npages, NULL, mask, align_order);
  265. if (unlikely(entry == IOMMU_MAPPING_ERROR))
  266. return IOMMU_MAPPING_ERROR;
  267. entry += tbl->it_offset; /* Offset into real TCE table */
  268. ret = entry << tbl->it_page_shift; /* Set the return dma address */
  269. /* Put the TCEs in the HW table */
  270. build_fail = tbl->it_ops->set(tbl, entry, npages,
  271. (unsigned long)page &
  272. IOMMU_PAGE_MASK(tbl), direction, attrs);
  273. /* tbl->it_ops->set() only returns non-zero for transient errors.
  274. * Clean up the table bitmap in this case and return
  275. * IOMMU_MAPPING_ERROR. For all other errors the functionality is
  276. * not altered.
  277. */
  278. if (unlikely(build_fail)) {
  279. __iommu_free(tbl, ret, npages);
  280. return IOMMU_MAPPING_ERROR;
  281. }
  282. /* Flush/invalidate TLB caches if necessary */
  283. if (tbl->it_ops->flush)
  284. tbl->it_ops->flush(tbl);
  285. /* Make sure updates are seen by hardware */
  286. mb();
  287. return ret;
  288. }
  289. static bool iommu_free_check(struct iommu_table *tbl, dma_addr_t dma_addr,
  290. unsigned int npages)
  291. {
  292. unsigned long entry, free_entry;
  293. entry = dma_addr >> tbl->it_page_shift;
  294. free_entry = entry - tbl->it_offset;
  295. if (((free_entry + npages) > tbl->it_size) ||
  296. (entry < tbl->it_offset)) {
  297. if (printk_ratelimit()) {
  298. printk(KERN_INFO "iommu_free: invalid entry\n");
  299. printk(KERN_INFO "\tentry = 0x%lx\n", entry);
  300. printk(KERN_INFO "\tdma_addr = 0x%llx\n", (u64)dma_addr);
  301. printk(KERN_INFO "\tTable = 0x%llx\n", (u64)tbl);
  302. printk(KERN_INFO "\tbus# = 0x%llx\n", (u64)tbl->it_busno);
  303. printk(KERN_INFO "\tsize = 0x%llx\n", (u64)tbl->it_size);
  304. printk(KERN_INFO "\tstartOff = 0x%llx\n", (u64)tbl->it_offset);
  305. printk(KERN_INFO "\tindex = 0x%llx\n", (u64)tbl->it_index);
  306. WARN_ON(1);
  307. }
  308. return false;
  309. }
  310. return true;
  311. }
  312. static struct iommu_pool *get_pool(struct iommu_table *tbl,
  313. unsigned long entry)
  314. {
  315. struct iommu_pool *p;
  316. unsigned long largepool_start = tbl->large_pool.start;
  317. /* The large pool is the last pool at the top of the table */
  318. if (entry >= largepool_start) {
  319. p = &tbl->large_pool;
  320. } else {
  321. unsigned int pool_nr = entry / tbl->poolsize;
  322. BUG_ON(pool_nr > tbl->nr_pools);
  323. p = &tbl->pools[pool_nr];
  324. }
  325. return p;
  326. }
  327. static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  328. unsigned int npages)
  329. {
  330. unsigned long entry, free_entry;
  331. unsigned long flags;
  332. struct iommu_pool *pool;
  333. entry = dma_addr >> tbl->it_page_shift;
  334. free_entry = entry - tbl->it_offset;
  335. pool = get_pool(tbl, free_entry);
  336. if (!iommu_free_check(tbl, dma_addr, npages))
  337. return;
  338. tbl->it_ops->clear(tbl, entry, npages);
  339. spin_lock_irqsave(&(pool->lock), flags);
  340. bitmap_clear(tbl->it_map, free_entry, npages);
  341. spin_unlock_irqrestore(&(pool->lock), flags);
  342. }
  343. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  344. unsigned int npages)
  345. {
  346. __iommu_free(tbl, dma_addr, npages);
  347. /* Make sure TLB cache is flushed if the HW needs it. We do
  348. * not do an mb() here on purpose, it is not needed on any of
  349. * the current platforms.
  350. */
  351. if (tbl->it_ops->flush)
  352. tbl->it_ops->flush(tbl);
  353. }
  354. int ppc_iommu_map_sg(struct device *dev, struct iommu_table *tbl,
  355. struct scatterlist *sglist, int nelems,
  356. unsigned long mask, enum dma_data_direction direction,
  357. unsigned long attrs)
  358. {
  359. dma_addr_t dma_next = 0, dma_addr;
  360. struct scatterlist *s, *outs, *segstart;
  361. int outcount, incount, i, build_fail = 0;
  362. unsigned int align;
  363. unsigned long handle;
  364. unsigned int max_seg_size;
  365. BUG_ON(direction == DMA_NONE);
  366. if ((nelems == 0) || !tbl)
  367. return 0;
  368. outs = s = segstart = &sglist[0];
  369. outcount = 1;
  370. incount = nelems;
  371. handle = 0;
  372. /* Init first segment length for backout at failure */
  373. outs->dma_length = 0;
  374. DBG("sg mapping %d elements:\n", nelems);
  375. max_seg_size = dma_get_max_seg_size(dev);
  376. for_each_sg(sglist, s, nelems, i) {
  377. unsigned long vaddr, npages, entry, slen;
  378. slen = s->length;
  379. /* Sanity check */
  380. if (slen == 0) {
  381. dma_next = 0;
  382. continue;
  383. }
  384. /* Allocate iommu entries for that segment */
  385. vaddr = (unsigned long) sg_virt(s);
  386. npages = iommu_num_pages(vaddr, slen, IOMMU_PAGE_SIZE(tbl));
  387. align = 0;
  388. if (tbl->it_page_shift < PAGE_SHIFT && slen >= PAGE_SIZE &&
  389. (vaddr & ~PAGE_MASK) == 0)
  390. align = PAGE_SHIFT - tbl->it_page_shift;
  391. entry = iommu_range_alloc(dev, tbl, npages, &handle,
  392. mask >> tbl->it_page_shift, align);
  393. DBG(" - vaddr: %lx, size: %lx\n", vaddr, slen);
  394. /* Handle failure */
  395. if (unlikely(entry == IOMMU_MAPPING_ERROR)) {
  396. if (!(attrs & DMA_ATTR_NO_WARN) &&
  397. printk_ratelimit())
  398. dev_info(dev, "iommu_alloc failed, tbl %p "
  399. "vaddr %lx npages %lu\n", tbl, vaddr,
  400. npages);
  401. goto failure;
  402. }
  403. /* Convert entry to a dma_addr_t */
  404. entry += tbl->it_offset;
  405. dma_addr = entry << tbl->it_page_shift;
  406. dma_addr |= (s->offset & ~IOMMU_PAGE_MASK(tbl));
  407. DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n",
  408. npages, entry, dma_addr);
  409. /* Insert into HW table */
  410. build_fail = tbl->it_ops->set(tbl, entry, npages,
  411. vaddr & IOMMU_PAGE_MASK(tbl),
  412. direction, attrs);
  413. if(unlikely(build_fail))
  414. goto failure;
  415. /* If we are in an open segment, try merging */
  416. if (segstart != s) {
  417. DBG(" - trying merge...\n");
  418. /* We cannot merge if:
  419. * - allocated dma_addr isn't contiguous to previous allocation
  420. */
  421. if (novmerge || (dma_addr != dma_next) ||
  422. (outs->dma_length + s->length > max_seg_size)) {
  423. /* Can't merge: create a new segment */
  424. segstart = s;
  425. outcount++;
  426. outs = sg_next(outs);
  427. DBG(" can't merge, new segment.\n");
  428. } else {
  429. outs->dma_length += s->length;
  430. DBG(" merged, new len: %ux\n", outs->dma_length);
  431. }
  432. }
  433. if (segstart == s) {
  434. /* This is a new segment, fill entries */
  435. DBG(" - filling new segment.\n");
  436. outs->dma_address = dma_addr;
  437. outs->dma_length = slen;
  438. }
  439. /* Calculate next page pointer for contiguous check */
  440. dma_next = dma_addr + slen;
  441. DBG(" - dma next is: %lx\n", dma_next);
  442. }
  443. /* Flush/invalidate TLB caches if necessary */
  444. if (tbl->it_ops->flush)
  445. tbl->it_ops->flush(tbl);
  446. DBG("mapped %d elements:\n", outcount);
  447. /* For the sake of ppc_iommu_unmap_sg, we clear out the length in the
  448. * next entry of the sglist if we didn't fill the list completely
  449. */
  450. if (outcount < incount) {
  451. outs = sg_next(outs);
  452. outs->dma_address = IOMMU_MAPPING_ERROR;
  453. outs->dma_length = 0;
  454. }
  455. /* Make sure updates are seen by hardware */
  456. mb();
  457. return outcount;
  458. failure:
  459. for_each_sg(sglist, s, nelems, i) {
  460. if (s->dma_length != 0) {
  461. unsigned long vaddr, npages;
  462. vaddr = s->dma_address & IOMMU_PAGE_MASK(tbl);
  463. npages = iommu_num_pages(s->dma_address, s->dma_length,
  464. IOMMU_PAGE_SIZE(tbl));
  465. __iommu_free(tbl, vaddr, npages);
  466. s->dma_address = IOMMU_MAPPING_ERROR;
  467. s->dma_length = 0;
  468. }
  469. if (s == outs)
  470. break;
  471. }
  472. return 0;
  473. }
  474. void ppc_iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
  475. int nelems, enum dma_data_direction direction,
  476. unsigned long attrs)
  477. {
  478. struct scatterlist *sg;
  479. BUG_ON(direction == DMA_NONE);
  480. if (!tbl)
  481. return;
  482. sg = sglist;
  483. while (nelems--) {
  484. unsigned int npages;
  485. dma_addr_t dma_handle = sg->dma_address;
  486. if (sg->dma_length == 0)
  487. break;
  488. npages = iommu_num_pages(dma_handle, sg->dma_length,
  489. IOMMU_PAGE_SIZE(tbl));
  490. __iommu_free(tbl, dma_handle, npages);
  491. sg = sg_next(sg);
  492. }
  493. /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
  494. * do not do an mb() here, the affected platforms do not need it
  495. * when freeing.
  496. */
  497. if (tbl->it_ops->flush)
  498. tbl->it_ops->flush(tbl);
  499. }
  500. static void iommu_table_clear(struct iommu_table *tbl)
  501. {
  502. /*
  503. * In case of firmware assisted dump system goes through clean
  504. * reboot process at the time of system crash. Hence it's safe to
  505. * clear the TCE entries if firmware assisted dump is active.
  506. */
  507. if (!is_kdump_kernel() || is_fadump_active()) {
  508. /* Clear the table in case firmware left allocations in it */
  509. tbl->it_ops->clear(tbl, tbl->it_offset, tbl->it_size);
  510. return;
  511. }
  512. #ifdef CONFIG_CRASH_DUMP
  513. if (tbl->it_ops->get) {
  514. unsigned long index, tceval, tcecount = 0;
  515. /* Reserve the existing mappings left by the first kernel. */
  516. for (index = 0; index < tbl->it_size; index++) {
  517. tceval = tbl->it_ops->get(tbl, index + tbl->it_offset);
  518. /*
  519. * Freed TCE entry contains 0x7fffffffffffffff on JS20
  520. */
  521. if (tceval && (tceval != 0x7fffffffffffffffUL)) {
  522. __set_bit(index, tbl->it_map);
  523. tcecount++;
  524. }
  525. }
  526. if ((tbl->it_size - tcecount) < KDUMP_MIN_TCE_ENTRIES) {
  527. printk(KERN_WARNING "TCE table is full; freeing ");
  528. printk(KERN_WARNING "%d entries for the kdump boot\n",
  529. KDUMP_MIN_TCE_ENTRIES);
  530. for (index = tbl->it_size - KDUMP_MIN_TCE_ENTRIES;
  531. index < tbl->it_size; index++)
  532. __clear_bit(index, tbl->it_map);
  533. }
  534. }
  535. #endif
  536. }
  537. /*
  538. * Build a iommu_table structure. This contains a bit map which
  539. * is used to manage allocation of the tce space.
  540. */
  541. struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
  542. {
  543. unsigned long sz;
  544. static int welcomed = 0;
  545. struct page *page;
  546. unsigned int i;
  547. struct iommu_pool *p;
  548. BUG_ON(!tbl->it_ops);
  549. /* number of bytes needed for the bitmap */
  550. sz = BITS_TO_LONGS(tbl->it_size) * sizeof(unsigned long);
  551. page = alloc_pages_node(nid, GFP_KERNEL, get_order(sz));
  552. if (!page)
  553. panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
  554. tbl->it_map = page_address(page);
  555. memset(tbl->it_map, 0, sz);
  556. /*
  557. * Reserve page 0 so it will not be used for any mappings.
  558. * This avoids buggy drivers that consider page 0 to be invalid
  559. * to crash the machine or even lose data.
  560. */
  561. if (tbl->it_offset == 0)
  562. set_bit(0, tbl->it_map);
  563. /* We only split the IOMMU table if we have 1GB or more of space */
  564. if ((tbl->it_size << tbl->it_page_shift) >= (1UL * 1024 * 1024 * 1024))
  565. tbl->nr_pools = IOMMU_NR_POOLS;
  566. else
  567. tbl->nr_pools = 1;
  568. /* We reserve the top 1/4 of the table for large allocations */
  569. tbl->poolsize = (tbl->it_size * 3 / 4) / tbl->nr_pools;
  570. for (i = 0; i < tbl->nr_pools; i++) {
  571. p = &tbl->pools[i];
  572. spin_lock_init(&(p->lock));
  573. p->start = tbl->poolsize * i;
  574. p->hint = p->start;
  575. p->end = p->start + tbl->poolsize;
  576. }
  577. p = &tbl->large_pool;
  578. spin_lock_init(&(p->lock));
  579. p->start = tbl->poolsize * i;
  580. p->hint = p->start;
  581. p->end = tbl->it_size;
  582. iommu_table_clear(tbl);
  583. if (!welcomed) {
  584. printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",
  585. novmerge ? "disabled" : "enabled");
  586. welcomed = 1;
  587. }
  588. return tbl;
  589. }
  590. static void iommu_table_free(struct kref *kref)
  591. {
  592. unsigned long bitmap_sz;
  593. unsigned int order;
  594. struct iommu_table *tbl;
  595. tbl = container_of(kref, struct iommu_table, it_kref);
  596. if (tbl->it_ops->free)
  597. tbl->it_ops->free(tbl);
  598. if (!tbl->it_map) {
  599. kfree(tbl);
  600. return;
  601. }
  602. /*
  603. * In case we have reserved the first bit, we should not emit
  604. * the warning below.
  605. */
  606. if (tbl->it_offset == 0)
  607. clear_bit(0, tbl->it_map);
  608. /* verify that table contains no entries */
  609. if (!bitmap_empty(tbl->it_map, tbl->it_size))
  610. pr_warn("%s: Unexpected TCEs\n", __func__);
  611. /* calculate bitmap size in bytes */
  612. bitmap_sz = BITS_TO_LONGS(tbl->it_size) * sizeof(unsigned long);
  613. /* free bitmap */
  614. order = get_order(bitmap_sz);
  615. free_pages((unsigned long) tbl->it_map, order);
  616. /* free table */
  617. kfree(tbl);
  618. }
  619. struct iommu_table *iommu_tce_table_get(struct iommu_table *tbl)
  620. {
  621. if (kref_get_unless_zero(&tbl->it_kref))
  622. return tbl;
  623. return NULL;
  624. }
  625. EXPORT_SYMBOL_GPL(iommu_tce_table_get);
  626. int iommu_tce_table_put(struct iommu_table *tbl)
  627. {
  628. if (WARN_ON(!tbl))
  629. return 0;
  630. return kref_put(&tbl->it_kref, iommu_table_free);
  631. }
  632. EXPORT_SYMBOL_GPL(iommu_tce_table_put);
  633. /* Creates TCEs for a user provided buffer. The user buffer must be
  634. * contiguous real kernel storage (not vmalloc). The address passed here
  635. * comprises a page address and offset into that page. The dma_addr_t
  636. * returned will point to the same byte within the page as was passed in.
  637. */
  638. dma_addr_t iommu_map_page(struct device *dev, struct iommu_table *tbl,
  639. struct page *page, unsigned long offset, size_t size,
  640. unsigned long mask, enum dma_data_direction direction,
  641. unsigned long attrs)
  642. {
  643. dma_addr_t dma_handle = IOMMU_MAPPING_ERROR;
  644. void *vaddr;
  645. unsigned long uaddr;
  646. unsigned int npages, align;
  647. BUG_ON(direction == DMA_NONE);
  648. vaddr = page_address(page) + offset;
  649. uaddr = (unsigned long)vaddr;
  650. if (tbl) {
  651. npages = iommu_num_pages(uaddr, size, IOMMU_PAGE_SIZE(tbl));
  652. align = 0;
  653. if (tbl->it_page_shift < PAGE_SHIFT && size >= PAGE_SIZE &&
  654. ((unsigned long)vaddr & ~PAGE_MASK) == 0)
  655. align = PAGE_SHIFT - tbl->it_page_shift;
  656. dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction,
  657. mask >> tbl->it_page_shift, align,
  658. attrs);
  659. if (dma_handle == IOMMU_MAPPING_ERROR) {
  660. if (!(attrs & DMA_ATTR_NO_WARN) &&
  661. printk_ratelimit()) {
  662. dev_info(dev, "iommu_alloc failed, tbl %p "
  663. "vaddr %p npages %d\n", tbl, vaddr,
  664. npages);
  665. }
  666. } else
  667. dma_handle |= (uaddr & ~IOMMU_PAGE_MASK(tbl));
  668. }
  669. return dma_handle;
  670. }
  671. void iommu_unmap_page(struct iommu_table *tbl, dma_addr_t dma_handle,
  672. size_t size, enum dma_data_direction direction,
  673. unsigned long attrs)
  674. {
  675. unsigned int npages;
  676. BUG_ON(direction == DMA_NONE);
  677. if (tbl) {
  678. npages = iommu_num_pages(dma_handle, size,
  679. IOMMU_PAGE_SIZE(tbl));
  680. iommu_free(tbl, dma_handle, npages);
  681. }
  682. }
  683. /* Allocates a contiguous real buffer and creates mappings over it.
  684. * Returns the virtual address of the buffer and sets dma_handle
  685. * to the dma address (mapping) of the first page.
  686. */
  687. void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,
  688. size_t size, dma_addr_t *dma_handle,
  689. unsigned long mask, gfp_t flag, int node)
  690. {
  691. void *ret = NULL;
  692. dma_addr_t mapping;
  693. unsigned int order;
  694. unsigned int nio_pages, io_order;
  695. struct page *page;
  696. size = PAGE_ALIGN(size);
  697. order = get_order(size);
  698. /*
  699. * Client asked for way too much space. This is checked later
  700. * anyway. It is easier to debug here for the drivers than in
  701. * the tce tables.
  702. */
  703. if (order >= IOMAP_MAX_ORDER) {
  704. dev_info(dev, "iommu_alloc_consistent size too large: 0x%lx\n",
  705. size);
  706. return NULL;
  707. }
  708. if (!tbl)
  709. return NULL;
  710. /* Alloc enough pages (and possibly more) */
  711. page = alloc_pages_node(node, flag, order);
  712. if (!page)
  713. return NULL;
  714. ret = page_address(page);
  715. memset(ret, 0, size);
  716. /* Set up tces to cover the allocated range */
  717. nio_pages = size >> tbl->it_page_shift;
  718. io_order = get_iommu_order(size, tbl);
  719. mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL,
  720. mask >> tbl->it_page_shift, io_order, 0);
  721. if (mapping == IOMMU_MAPPING_ERROR) {
  722. free_pages((unsigned long)ret, order);
  723. return NULL;
  724. }
  725. *dma_handle = mapping;
  726. return ret;
  727. }
  728. void iommu_free_coherent(struct iommu_table *tbl, size_t size,
  729. void *vaddr, dma_addr_t dma_handle)
  730. {
  731. if (tbl) {
  732. unsigned int nio_pages;
  733. size = PAGE_ALIGN(size);
  734. nio_pages = size >> tbl->it_page_shift;
  735. iommu_free(tbl, dma_handle, nio_pages);
  736. size = PAGE_ALIGN(size);
  737. free_pages((unsigned long)vaddr, get_order(size));
  738. }
  739. }
  740. unsigned long iommu_direction_to_tce_perm(enum dma_data_direction dir)
  741. {
  742. switch (dir) {
  743. case DMA_BIDIRECTIONAL:
  744. return TCE_PCI_READ | TCE_PCI_WRITE;
  745. case DMA_FROM_DEVICE:
  746. return TCE_PCI_WRITE;
  747. case DMA_TO_DEVICE:
  748. return TCE_PCI_READ;
  749. default:
  750. return 0;
  751. }
  752. }
  753. EXPORT_SYMBOL_GPL(iommu_direction_to_tce_perm);
  754. #ifdef CONFIG_IOMMU_API
  755. /*
  756. * SPAPR TCE API
  757. */
  758. static void group_release(void *iommu_data)
  759. {
  760. struct iommu_table_group *table_group = iommu_data;
  761. table_group->group = NULL;
  762. }
  763. void iommu_register_group(struct iommu_table_group *table_group,
  764. int pci_domain_number, unsigned long pe_num)
  765. {
  766. struct iommu_group *grp;
  767. char *name;
  768. grp = iommu_group_alloc();
  769. if (IS_ERR(grp)) {
  770. pr_warn("powerpc iommu api: cannot create new group, err=%ld\n",
  771. PTR_ERR(grp));
  772. return;
  773. }
  774. table_group->group = grp;
  775. iommu_group_set_iommudata(grp, table_group, group_release);
  776. name = kasprintf(GFP_KERNEL, "domain%d-pe%lx",
  777. pci_domain_number, pe_num);
  778. if (!name)
  779. return;
  780. iommu_group_set_name(grp, name);
  781. kfree(name);
  782. }
  783. enum dma_data_direction iommu_tce_direction(unsigned long tce)
  784. {
  785. if ((tce & TCE_PCI_READ) && (tce & TCE_PCI_WRITE))
  786. return DMA_BIDIRECTIONAL;
  787. else if (tce & TCE_PCI_READ)
  788. return DMA_TO_DEVICE;
  789. else if (tce & TCE_PCI_WRITE)
  790. return DMA_FROM_DEVICE;
  791. else
  792. return DMA_NONE;
  793. }
  794. EXPORT_SYMBOL_GPL(iommu_tce_direction);
  795. void iommu_flush_tce(struct iommu_table *tbl)
  796. {
  797. /* Flush/invalidate TLB caches if necessary */
  798. if (tbl->it_ops->flush)
  799. tbl->it_ops->flush(tbl);
  800. /* Make sure updates are seen by hardware */
  801. mb();
  802. }
  803. EXPORT_SYMBOL_GPL(iommu_flush_tce);
  804. int iommu_tce_check_ioba(unsigned long page_shift,
  805. unsigned long offset, unsigned long size,
  806. unsigned long ioba, unsigned long npages)
  807. {
  808. unsigned long mask = (1UL << page_shift) - 1;
  809. if (ioba & mask)
  810. return -EINVAL;
  811. ioba >>= page_shift;
  812. if (ioba < offset)
  813. return -EINVAL;
  814. if ((ioba + 1) > (offset + size))
  815. return -EINVAL;
  816. return 0;
  817. }
  818. EXPORT_SYMBOL_GPL(iommu_tce_check_ioba);
  819. int iommu_tce_check_gpa(unsigned long page_shift, unsigned long gpa)
  820. {
  821. unsigned long mask = (1UL << page_shift) - 1;
  822. if (gpa & mask)
  823. return -EINVAL;
  824. return 0;
  825. }
  826. EXPORT_SYMBOL_GPL(iommu_tce_check_gpa);
  827. long iommu_tce_xchg(struct iommu_table *tbl, unsigned long entry,
  828. unsigned long *hpa, enum dma_data_direction *direction)
  829. {
  830. long ret;
  831. ret = tbl->it_ops->exchange(tbl, entry, hpa, direction);
  832. if (!ret && ((*direction == DMA_FROM_DEVICE) ||
  833. (*direction == DMA_BIDIRECTIONAL)))
  834. SetPageDirty(pfn_to_page(*hpa >> PAGE_SHIFT));
  835. /* if (unlikely(ret))
  836. pr_err("iommu_tce: %s failed on hwaddr=%lx ioba=%lx kva=%lx ret=%d\n",
  837. __func__, hwaddr, entry << tbl->it_page_shift,
  838. hwaddr, ret); */
  839. return ret;
  840. }
  841. EXPORT_SYMBOL_GPL(iommu_tce_xchg);
  842. int iommu_take_ownership(struct iommu_table *tbl)
  843. {
  844. unsigned long flags, i, sz = (tbl->it_size + 7) >> 3;
  845. int ret = 0;
  846. /*
  847. * VFIO does not control TCE entries allocation and the guest
  848. * can write new TCEs on top of existing ones so iommu_tce_build()
  849. * must be able to release old pages. This functionality
  850. * requires exchange() callback defined so if it is not
  851. * implemented, we disallow taking ownership over the table.
  852. */
  853. if (!tbl->it_ops->exchange)
  854. return -EINVAL;
  855. spin_lock_irqsave(&tbl->large_pool.lock, flags);
  856. for (i = 0; i < tbl->nr_pools; i++)
  857. spin_lock_nest_lock(&tbl->pools[i].lock, &tbl->large_pool.lock);
  858. if (tbl->it_offset == 0)
  859. clear_bit(0, tbl->it_map);
  860. if (!bitmap_empty(tbl->it_map, tbl->it_size)) {
  861. pr_err("iommu_tce: it_map is not empty");
  862. ret = -EBUSY;
  863. /* Restore bit#0 set by iommu_init_table() */
  864. if (tbl->it_offset == 0)
  865. set_bit(0, tbl->it_map);
  866. } else {
  867. memset(tbl->it_map, 0xff, sz);
  868. }
  869. for (i = 0; i < tbl->nr_pools; i++)
  870. spin_unlock(&tbl->pools[i].lock);
  871. spin_unlock_irqrestore(&tbl->large_pool.lock, flags);
  872. return ret;
  873. }
  874. EXPORT_SYMBOL_GPL(iommu_take_ownership);
  875. void iommu_release_ownership(struct iommu_table *tbl)
  876. {
  877. unsigned long flags, i, sz = (tbl->it_size + 7) >> 3;
  878. spin_lock_irqsave(&tbl->large_pool.lock, flags);
  879. for (i = 0; i < tbl->nr_pools; i++)
  880. spin_lock_nest_lock(&tbl->pools[i].lock, &tbl->large_pool.lock);
  881. memset(tbl->it_map, 0, sz);
  882. /* Restore bit#0 set by iommu_init_table() */
  883. if (tbl->it_offset == 0)
  884. set_bit(0, tbl->it_map);
  885. for (i = 0; i < tbl->nr_pools; i++)
  886. spin_unlock(&tbl->pools[i].lock);
  887. spin_unlock_irqrestore(&tbl->large_pool.lock, flags);
  888. }
  889. EXPORT_SYMBOL_GPL(iommu_release_ownership);
  890. int iommu_add_device(struct device *dev)
  891. {
  892. struct iommu_table *tbl;
  893. struct iommu_table_group_link *tgl;
  894. /*
  895. * The sysfs entries should be populated before
  896. * binding IOMMU group. If sysfs entries isn't
  897. * ready, we simply bail.
  898. */
  899. if (!device_is_registered(dev))
  900. return -ENOENT;
  901. if (dev->iommu_group) {
  902. pr_debug("%s: Skipping device %s with iommu group %d\n",
  903. __func__, dev_name(dev),
  904. iommu_group_id(dev->iommu_group));
  905. return -EBUSY;
  906. }
  907. tbl = get_iommu_table_base(dev);
  908. if (!tbl) {
  909. pr_debug("%s: Skipping device %s with no tbl\n",
  910. __func__, dev_name(dev));
  911. return 0;
  912. }
  913. tgl = list_first_entry_or_null(&tbl->it_group_list,
  914. struct iommu_table_group_link, next);
  915. if (!tgl) {
  916. pr_debug("%s: Skipping device %s with no group\n",
  917. __func__, dev_name(dev));
  918. return 0;
  919. }
  920. pr_debug("%s: Adding %s to iommu group %d\n",
  921. __func__, dev_name(dev),
  922. iommu_group_id(tgl->table_group->group));
  923. if (PAGE_SIZE < IOMMU_PAGE_SIZE(tbl)) {
  924. pr_err("%s: Invalid IOMMU page size %lx (%lx) on %s\n",
  925. __func__, IOMMU_PAGE_SIZE(tbl),
  926. PAGE_SIZE, dev_name(dev));
  927. return -EINVAL;
  928. }
  929. return iommu_group_add_device(tgl->table_group->group, dev);
  930. }
  931. EXPORT_SYMBOL_GPL(iommu_add_device);
  932. void iommu_del_device(struct device *dev)
  933. {
  934. /*
  935. * Some devices might not have IOMMU table and group
  936. * and we needn't detach them from the associated
  937. * IOMMU groups
  938. */
  939. if (!dev->iommu_group) {
  940. pr_debug("iommu_tce: skipping device %s with no tbl\n",
  941. dev_name(dev));
  942. return;
  943. }
  944. iommu_group_remove_device(dev);
  945. }
  946. EXPORT_SYMBOL_GPL(iommu_del_device);
  947. static int tce_iommu_bus_notifier(struct notifier_block *nb,
  948. unsigned long action, void *data)
  949. {
  950. struct device *dev = data;
  951. switch (action) {
  952. case BUS_NOTIFY_ADD_DEVICE:
  953. return iommu_add_device(dev);
  954. case BUS_NOTIFY_DEL_DEVICE:
  955. if (dev->iommu_group)
  956. iommu_del_device(dev);
  957. return 0;
  958. default:
  959. return 0;
  960. }
  961. }
  962. static struct notifier_block tce_iommu_bus_nb = {
  963. .notifier_call = tce_iommu_bus_notifier,
  964. };
  965. int __init tce_iommu_bus_notifier_init(void)
  966. {
  967. bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb);
  968. return 0;
  969. }
  970. #endif /* CONFIG_IOMMU_API */