iommu.c 30 KB

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