iommu.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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_RET(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 = __raw_get_cpu_var(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 << IOMMU_PAGE_SHIFT);
  214. else
  215. boundary_size = ALIGN(1UL << 32, 1 << IOMMU_PAGE_SHIFT);
  216. /* 4GB boundary for iseries_hv_alloc and iseries_hv_map */
  217. n = iommu_area_alloc(tbl->it_map, limit, start, npages,
  218. tbl->it_offset, boundary_size >> IOMMU_PAGE_SHIFT,
  219. align_mask);
  220. if (n == -1) {
  221. if (likely(pass == 0)) {
  222. /* First try the pool from the start */
  223. pool->hint = pool->start;
  224. pass++;
  225. goto again;
  226. } else if (pass <= tbl->nr_pools) {
  227. /* Now try scanning all the other pools */
  228. spin_unlock(&(pool->lock));
  229. pool_nr = (pool_nr + 1) & (tbl->nr_pools - 1);
  230. pool = &tbl->pools[pool_nr];
  231. spin_lock(&(pool->lock));
  232. pool->hint = pool->start;
  233. pass++;
  234. goto again;
  235. } else {
  236. /* Give up */
  237. spin_unlock_irqrestore(&(pool->lock), flags);
  238. return DMA_ERROR_CODE;
  239. }
  240. }
  241. end = n + npages;
  242. /* Bump the hint to a new block for small allocs. */
  243. if (largealloc) {
  244. /* Don't bump to new block to avoid fragmentation */
  245. pool->hint = end;
  246. } else {
  247. /* Overflow will be taken care of at the next allocation */
  248. pool->hint = (end + tbl->it_blocksize - 1) &
  249. ~(tbl->it_blocksize - 1);
  250. }
  251. /* Update handle for SG allocations */
  252. if (handle)
  253. *handle = end;
  254. spin_unlock_irqrestore(&(pool->lock), flags);
  255. return n;
  256. }
  257. static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
  258. void *page, unsigned int npages,
  259. enum dma_data_direction direction,
  260. unsigned long mask, unsigned int align_order,
  261. struct dma_attrs *attrs)
  262. {
  263. unsigned long entry;
  264. dma_addr_t ret = DMA_ERROR_CODE;
  265. int build_fail;
  266. entry = iommu_range_alloc(dev, tbl, npages, NULL, mask, align_order);
  267. if (unlikely(entry == DMA_ERROR_CODE))
  268. return DMA_ERROR_CODE;
  269. entry += tbl->it_offset; /* Offset into real TCE table */
  270. ret = entry << IOMMU_PAGE_SHIFT; /* Set the return dma address */
  271. /* Put the TCEs in the HW table */
  272. build_fail = ppc_md.tce_build(tbl, entry, npages,
  273. (unsigned long)page & IOMMU_PAGE_MASK,
  274. direction, attrs);
  275. /* ppc_md.tce_build() only returns non-zero for transient errors.
  276. * Clean up the table bitmap in this case and return
  277. * DMA_ERROR_CODE. For all other errors the functionality is
  278. * not altered.
  279. */
  280. if (unlikely(build_fail)) {
  281. __iommu_free(tbl, ret, npages);
  282. return DMA_ERROR_CODE;
  283. }
  284. /* Flush/invalidate TLB caches if necessary */
  285. if (ppc_md.tce_flush)
  286. ppc_md.tce_flush(tbl);
  287. /* Make sure updates are seen by hardware */
  288. mb();
  289. return ret;
  290. }
  291. static bool iommu_free_check(struct iommu_table *tbl, dma_addr_t dma_addr,
  292. unsigned int npages)
  293. {
  294. unsigned long entry, free_entry;
  295. entry = dma_addr >> IOMMU_PAGE_SHIFT;
  296. free_entry = entry - tbl->it_offset;
  297. if (((free_entry + npages) > tbl->it_size) ||
  298. (entry < tbl->it_offset)) {
  299. if (printk_ratelimit()) {
  300. printk(KERN_INFO "iommu_free: invalid entry\n");
  301. printk(KERN_INFO "\tentry = 0x%lx\n", entry);
  302. printk(KERN_INFO "\tdma_addr = 0x%llx\n", (u64)dma_addr);
  303. printk(KERN_INFO "\tTable = 0x%llx\n", (u64)tbl);
  304. printk(KERN_INFO "\tbus# = 0x%llx\n", (u64)tbl->it_busno);
  305. printk(KERN_INFO "\tsize = 0x%llx\n", (u64)tbl->it_size);
  306. printk(KERN_INFO "\tstartOff = 0x%llx\n", (u64)tbl->it_offset);
  307. printk(KERN_INFO "\tindex = 0x%llx\n", (u64)tbl->it_index);
  308. WARN_ON(1);
  309. }
  310. return false;
  311. }
  312. return true;
  313. }
  314. static struct iommu_pool *get_pool(struct iommu_table *tbl,
  315. unsigned long entry)
  316. {
  317. struct iommu_pool *p;
  318. unsigned long largepool_start = tbl->large_pool.start;
  319. /* The large pool is the last pool at the top of the table */
  320. if (entry >= largepool_start) {
  321. p = &tbl->large_pool;
  322. } else {
  323. unsigned int pool_nr = entry / tbl->poolsize;
  324. BUG_ON(pool_nr > tbl->nr_pools);
  325. p = &tbl->pools[pool_nr];
  326. }
  327. return p;
  328. }
  329. static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  330. unsigned int npages)
  331. {
  332. unsigned long entry, free_entry;
  333. unsigned long flags;
  334. struct iommu_pool *pool;
  335. entry = dma_addr >> IOMMU_PAGE_SHIFT;
  336. free_entry = entry - tbl->it_offset;
  337. pool = get_pool(tbl, free_entry);
  338. if (!iommu_free_check(tbl, dma_addr, npages))
  339. return;
  340. ppc_md.tce_free(tbl, entry, npages);
  341. spin_lock_irqsave(&(pool->lock), flags);
  342. bitmap_clear(tbl->it_map, free_entry, npages);
  343. spin_unlock_irqrestore(&(pool->lock), flags);
  344. }
  345. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  346. unsigned int npages)
  347. {
  348. __iommu_free(tbl, dma_addr, npages);
  349. /* Make sure TLB cache is flushed if the HW needs it. We do
  350. * not do an mb() here on purpose, it is not needed on any of
  351. * the current platforms.
  352. */
  353. if (ppc_md.tce_flush)
  354. ppc_md.tce_flush(tbl);
  355. }
  356. int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
  357. struct scatterlist *sglist, int nelems,
  358. unsigned long mask, enum dma_data_direction direction,
  359. struct dma_attrs *attrs)
  360. {
  361. dma_addr_t dma_next = 0, dma_addr;
  362. struct scatterlist *s, *outs, *segstart;
  363. int outcount, incount, i, build_fail = 0;
  364. unsigned int align;
  365. unsigned long handle;
  366. unsigned int max_seg_size;
  367. BUG_ON(direction == DMA_NONE);
  368. if ((nelems == 0) || !tbl)
  369. return 0;
  370. outs = s = segstart = &sglist[0];
  371. outcount = 1;
  372. incount = nelems;
  373. handle = 0;
  374. /* Init first segment length for backout at failure */
  375. outs->dma_length = 0;
  376. DBG("sg mapping %d elements:\n", nelems);
  377. max_seg_size = dma_get_max_seg_size(dev);
  378. for_each_sg(sglist, s, nelems, i) {
  379. unsigned long vaddr, npages, entry, slen;
  380. slen = s->length;
  381. /* Sanity check */
  382. if (slen == 0) {
  383. dma_next = 0;
  384. continue;
  385. }
  386. /* Allocate iommu entries for that segment */
  387. vaddr = (unsigned long) sg_virt(s);
  388. npages = iommu_num_pages(vaddr, slen, IOMMU_PAGE_SIZE);
  389. align = 0;
  390. if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && slen >= PAGE_SIZE &&
  391. (vaddr & ~PAGE_MASK) == 0)
  392. align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
  393. entry = iommu_range_alloc(dev, tbl, npages, &handle,
  394. mask >> IOMMU_PAGE_SHIFT, align);
  395. DBG(" - vaddr: %lx, size: %lx\n", vaddr, slen);
  396. /* Handle failure */
  397. if (unlikely(entry == DMA_ERROR_CODE)) {
  398. if (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 << IOMMU_PAGE_SHIFT;
  407. dma_addr |= (s->offset & ~IOMMU_PAGE_MASK);
  408. DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n",
  409. npages, entry, dma_addr);
  410. /* Insert into HW table */
  411. build_fail = ppc_md.tce_build(tbl, entry, npages,
  412. vaddr & IOMMU_PAGE_MASK,
  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 (ppc_md.tce_flush)
  446. ppc_md.tce_flush(tbl);
  447. DBG("mapped %d elements:\n", outcount);
  448. /* For the sake of 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;
  464. npages = iommu_num_pages(s->dma_address, s->dma_length,
  465. IOMMU_PAGE_SIZE);
  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 iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
  476. int nelems, enum dma_data_direction direction,
  477. struct dma_attrs *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);
  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 (ppc_md.tce_flush)
  499. ppc_md.tce_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. ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
  511. return;
  512. }
  513. #ifdef CONFIG_CRASH_DUMP
  514. if (ppc_md.tce_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 = ppc_md.tce_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. /* 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_ATOMIC, 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 << IOMMU_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. void iommu_free_table(struct iommu_table *tbl, const char *node_name)
  591. {
  592. unsigned long bitmap_sz;
  593. unsigned int order;
  594. if (!tbl || !tbl->it_map) {
  595. printk(KERN_ERR "%s: expected TCE map for %s\n", __func__,
  596. node_name);
  597. return;
  598. }
  599. /*
  600. * In case we have reserved the first bit, we should not emit
  601. * the warning below.
  602. */
  603. if (tbl->it_offset == 0)
  604. clear_bit(0, tbl->it_map);
  605. #ifdef CONFIG_IOMMU_API
  606. if (tbl->it_group) {
  607. iommu_group_put(tbl->it_group);
  608. BUG_ON(tbl->it_group);
  609. }
  610. #endif
  611. /* verify that table contains no entries */
  612. if (!bitmap_empty(tbl->it_map, tbl->it_size))
  613. pr_warn("%s: Unexpected TCEs for %s\n", __func__, node_name);
  614. /* calculate bitmap size in bytes */
  615. bitmap_sz = BITS_TO_LONGS(tbl->it_size) * sizeof(unsigned long);
  616. /* free bitmap */
  617. order = get_order(bitmap_sz);
  618. free_pages((unsigned long) tbl->it_map, order);
  619. /* free table */
  620. kfree(tbl);
  621. }
  622. /* Creates TCEs for a user provided buffer. The user buffer must be
  623. * contiguous real kernel storage (not vmalloc). The address passed here
  624. * comprises a page address and offset into that page. The dma_addr_t
  625. * returned will point to the same byte within the page as was passed in.
  626. */
  627. dma_addr_t iommu_map_page(struct device *dev, struct iommu_table *tbl,
  628. struct page *page, unsigned long offset, size_t size,
  629. unsigned long mask, enum dma_data_direction direction,
  630. struct dma_attrs *attrs)
  631. {
  632. dma_addr_t dma_handle = DMA_ERROR_CODE;
  633. void *vaddr;
  634. unsigned long uaddr;
  635. unsigned int npages, align;
  636. BUG_ON(direction == DMA_NONE);
  637. vaddr = page_address(page) + offset;
  638. uaddr = (unsigned long)vaddr;
  639. npages = iommu_num_pages(uaddr, size, IOMMU_PAGE_SIZE);
  640. if (tbl) {
  641. align = 0;
  642. if (IOMMU_PAGE_SHIFT < PAGE_SHIFT && size >= PAGE_SIZE &&
  643. ((unsigned long)vaddr & ~PAGE_MASK) == 0)
  644. align = PAGE_SHIFT - IOMMU_PAGE_SHIFT;
  645. dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction,
  646. mask >> IOMMU_PAGE_SHIFT, align,
  647. attrs);
  648. if (dma_handle == DMA_ERROR_CODE) {
  649. if (printk_ratelimit()) {
  650. dev_info(dev, "iommu_alloc failed, tbl %p "
  651. "vaddr %p npages %d\n", tbl, vaddr,
  652. npages);
  653. }
  654. } else
  655. dma_handle |= (uaddr & ~IOMMU_PAGE_MASK);
  656. }
  657. return dma_handle;
  658. }
  659. void iommu_unmap_page(struct iommu_table *tbl, dma_addr_t dma_handle,
  660. size_t size, enum dma_data_direction direction,
  661. struct dma_attrs *attrs)
  662. {
  663. unsigned int npages;
  664. BUG_ON(direction == DMA_NONE);
  665. if (tbl) {
  666. npages = iommu_num_pages(dma_handle, size, IOMMU_PAGE_SIZE);
  667. iommu_free(tbl, dma_handle, npages);
  668. }
  669. }
  670. /* Allocates a contiguous real buffer and creates mappings over it.
  671. * Returns the virtual address of the buffer and sets dma_handle
  672. * to the dma address (mapping) of the first page.
  673. */
  674. void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,
  675. size_t size, dma_addr_t *dma_handle,
  676. unsigned long mask, gfp_t flag, int node)
  677. {
  678. void *ret = NULL;
  679. dma_addr_t mapping;
  680. unsigned int order;
  681. unsigned int nio_pages, io_order;
  682. struct page *page;
  683. size = PAGE_ALIGN(size);
  684. order = get_order(size);
  685. /*
  686. * Client asked for way too much space. This is checked later
  687. * anyway. It is easier to debug here for the drivers than in
  688. * the tce tables.
  689. */
  690. if (order >= IOMAP_MAX_ORDER) {
  691. dev_info(dev, "iommu_alloc_consistent size too large: 0x%lx\n",
  692. size);
  693. return NULL;
  694. }
  695. if (!tbl)
  696. return NULL;
  697. /* Alloc enough pages (and possibly more) */
  698. page = alloc_pages_node(node, flag, order);
  699. if (!page)
  700. return NULL;
  701. ret = page_address(page);
  702. memset(ret, 0, size);
  703. /* Set up tces to cover the allocated range */
  704. nio_pages = size >> IOMMU_PAGE_SHIFT;
  705. io_order = get_iommu_order(size);
  706. mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL,
  707. mask >> IOMMU_PAGE_SHIFT, io_order, NULL);
  708. if (mapping == DMA_ERROR_CODE) {
  709. free_pages((unsigned long)ret, order);
  710. return NULL;
  711. }
  712. *dma_handle = mapping;
  713. return ret;
  714. }
  715. void iommu_free_coherent(struct iommu_table *tbl, size_t size,
  716. void *vaddr, dma_addr_t dma_handle)
  717. {
  718. if (tbl) {
  719. unsigned int nio_pages;
  720. size = PAGE_ALIGN(size);
  721. nio_pages = size >> IOMMU_PAGE_SHIFT;
  722. iommu_free(tbl, dma_handle, nio_pages);
  723. size = PAGE_ALIGN(size);
  724. free_pages((unsigned long)vaddr, get_order(size));
  725. }
  726. }
  727. #ifdef CONFIG_IOMMU_API
  728. /*
  729. * SPAPR TCE API
  730. */
  731. static void group_release(void *iommu_data)
  732. {
  733. struct iommu_table *tbl = iommu_data;
  734. tbl->it_group = NULL;
  735. }
  736. void iommu_register_group(struct iommu_table *tbl,
  737. int pci_domain_number, unsigned long pe_num)
  738. {
  739. struct iommu_group *grp;
  740. char *name;
  741. grp = iommu_group_alloc();
  742. if (IS_ERR(grp)) {
  743. pr_warn("powerpc iommu api: cannot create new group, err=%ld\n",
  744. PTR_ERR(grp));
  745. return;
  746. }
  747. tbl->it_group = grp;
  748. iommu_group_set_iommudata(grp, tbl, group_release);
  749. name = kasprintf(GFP_KERNEL, "domain%d-pe%lx",
  750. pci_domain_number, pe_num);
  751. if (!name)
  752. return;
  753. iommu_group_set_name(grp, name);
  754. kfree(name);
  755. }
  756. enum dma_data_direction iommu_tce_direction(unsigned long tce)
  757. {
  758. if ((tce & TCE_PCI_READ) && (tce & TCE_PCI_WRITE))
  759. return DMA_BIDIRECTIONAL;
  760. else if (tce & TCE_PCI_READ)
  761. return DMA_TO_DEVICE;
  762. else if (tce & TCE_PCI_WRITE)
  763. return DMA_FROM_DEVICE;
  764. else
  765. return DMA_NONE;
  766. }
  767. EXPORT_SYMBOL_GPL(iommu_tce_direction);
  768. void iommu_flush_tce(struct iommu_table *tbl)
  769. {
  770. /* Flush/invalidate TLB caches if necessary */
  771. if (ppc_md.tce_flush)
  772. ppc_md.tce_flush(tbl);
  773. /* Make sure updates are seen by hardware */
  774. mb();
  775. }
  776. EXPORT_SYMBOL_GPL(iommu_flush_tce);
  777. int iommu_tce_clear_param_check(struct iommu_table *tbl,
  778. unsigned long ioba, unsigned long tce_value,
  779. unsigned long npages)
  780. {
  781. /* ppc_md.tce_free() does not support any value but 0 */
  782. if (tce_value)
  783. return -EINVAL;
  784. if (ioba & ~IOMMU_PAGE_MASK)
  785. return -EINVAL;
  786. ioba >>= IOMMU_PAGE_SHIFT;
  787. if (ioba < tbl->it_offset)
  788. return -EINVAL;
  789. if ((ioba + npages) > (tbl->it_offset + tbl->it_size))
  790. return -EINVAL;
  791. return 0;
  792. }
  793. EXPORT_SYMBOL_GPL(iommu_tce_clear_param_check);
  794. int iommu_tce_put_param_check(struct iommu_table *tbl,
  795. unsigned long ioba, unsigned long tce)
  796. {
  797. if (!(tce & (TCE_PCI_WRITE | TCE_PCI_READ)))
  798. return -EINVAL;
  799. if (tce & ~(IOMMU_PAGE_MASK | TCE_PCI_WRITE | TCE_PCI_READ))
  800. return -EINVAL;
  801. if (ioba & ~IOMMU_PAGE_MASK)
  802. return -EINVAL;
  803. ioba >>= IOMMU_PAGE_SHIFT;
  804. if (ioba < tbl->it_offset)
  805. return -EINVAL;
  806. if ((ioba + 1) > (tbl->it_offset + tbl->it_size))
  807. return -EINVAL;
  808. return 0;
  809. }
  810. EXPORT_SYMBOL_GPL(iommu_tce_put_param_check);
  811. unsigned long iommu_clear_tce(struct iommu_table *tbl, unsigned long entry)
  812. {
  813. unsigned long oldtce;
  814. struct iommu_pool *pool = get_pool(tbl, entry);
  815. spin_lock(&(pool->lock));
  816. oldtce = ppc_md.tce_get(tbl, entry);
  817. if (oldtce & (TCE_PCI_WRITE | TCE_PCI_READ))
  818. ppc_md.tce_free(tbl, entry, 1);
  819. else
  820. oldtce = 0;
  821. spin_unlock(&(pool->lock));
  822. return oldtce;
  823. }
  824. EXPORT_SYMBOL_GPL(iommu_clear_tce);
  825. int iommu_clear_tces_and_put_pages(struct iommu_table *tbl,
  826. unsigned long entry, unsigned long pages)
  827. {
  828. unsigned long oldtce;
  829. struct page *page;
  830. for ( ; pages; --pages, ++entry) {
  831. oldtce = iommu_clear_tce(tbl, entry);
  832. if (!oldtce)
  833. continue;
  834. page = pfn_to_page(oldtce >> PAGE_SHIFT);
  835. WARN_ON(!page);
  836. if (page) {
  837. if (oldtce & TCE_PCI_WRITE)
  838. SetPageDirty(page);
  839. put_page(page);
  840. }
  841. }
  842. return 0;
  843. }
  844. EXPORT_SYMBOL_GPL(iommu_clear_tces_and_put_pages);
  845. /*
  846. * hwaddr is a kernel virtual address here (0xc... bazillion),
  847. * tce_build converts it to a physical address.
  848. */
  849. int iommu_tce_build(struct iommu_table *tbl, unsigned long entry,
  850. unsigned long hwaddr, enum dma_data_direction direction)
  851. {
  852. int ret = -EBUSY;
  853. unsigned long oldtce;
  854. struct iommu_pool *pool = get_pool(tbl, entry);
  855. spin_lock(&(pool->lock));
  856. oldtce = ppc_md.tce_get(tbl, entry);
  857. /* Add new entry if it is not busy */
  858. if (!(oldtce & (TCE_PCI_WRITE | TCE_PCI_READ)))
  859. ret = ppc_md.tce_build(tbl, entry, 1, hwaddr, direction, NULL);
  860. spin_unlock(&(pool->lock));
  861. /* if (unlikely(ret))
  862. pr_err("iommu_tce: %s failed on hwaddr=%lx ioba=%lx kva=%lx ret=%d\n",
  863. __func__, hwaddr, entry << IOMMU_PAGE_SHIFT,
  864. hwaddr, ret); */
  865. return ret;
  866. }
  867. EXPORT_SYMBOL_GPL(iommu_tce_build);
  868. int iommu_put_tce_user_mode(struct iommu_table *tbl, unsigned long entry,
  869. unsigned long tce)
  870. {
  871. int ret;
  872. struct page *page = NULL;
  873. unsigned long hwaddr, offset = tce & IOMMU_PAGE_MASK & ~PAGE_MASK;
  874. enum dma_data_direction direction = iommu_tce_direction(tce);
  875. ret = get_user_pages_fast(tce & PAGE_MASK, 1,
  876. direction != DMA_TO_DEVICE, &page);
  877. if (unlikely(ret != 1)) {
  878. /* pr_err("iommu_tce: get_user_pages_fast failed tce=%lx ioba=%lx ret=%d\n",
  879. tce, entry << IOMMU_PAGE_SHIFT, ret); */
  880. return -EFAULT;
  881. }
  882. hwaddr = (unsigned long) page_address(page) + offset;
  883. ret = iommu_tce_build(tbl, entry, hwaddr, direction);
  884. if (ret)
  885. put_page(page);
  886. if (ret < 0)
  887. pr_err("iommu_tce: %s failed ioba=%lx, tce=%lx, ret=%d\n",
  888. __func__, entry << IOMMU_PAGE_SHIFT, tce, ret);
  889. return ret;
  890. }
  891. EXPORT_SYMBOL_GPL(iommu_put_tce_user_mode);
  892. int iommu_take_ownership(struct iommu_table *tbl)
  893. {
  894. unsigned long sz = (tbl->it_size + 7) >> 3;
  895. if (tbl->it_offset == 0)
  896. clear_bit(0, tbl->it_map);
  897. if (!bitmap_empty(tbl->it_map, tbl->it_size)) {
  898. pr_err("iommu_tce: it_map is not empty");
  899. return -EBUSY;
  900. }
  901. memset(tbl->it_map, 0xff, sz);
  902. iommu_clear_tces_and_put_pages(tbl, tbl->it_offset, tbl->it_size);
  903. return 0;
  904. }
  905. EXPORT_SYMBOL_GPL(iommu_take_ownership);
  906. void iommu_release_ownership(struct iommu_table *tbl)
  907. {
  908. unsigned long sz = (tbl->it_size + 7) >> 3;
  909. iommu_clear_tces_and_put_pages(tbl, tbl->it_offset, tbl->it_size);
  910. memset(tbl->it_map, 0, sz);
  911. /* Restore bit#0 set by iommu_init_table() */
  912. if (tbl->it_offset == 0)
  913. set_bit(0, tbl->it_map);
  914. }
  915. EXPORT_SYMBOL_GPL(iommu_release_ownership);
  916. static int iommu_add_device(struct device *dev)
  917. {
  918. struct iommu_table *tbl;
  919. int ret = 0;
  920. if (WARN_ON(dev->iommu_group)) {
  921. pr_warn("iommu_tce: device %s is already in iommu group %d, skipping\n",
  922. dev_name(dev),
  923. iommu_group_id(dev->iommu_group));
  924. return -EBUSY;
  925. }
  926. tbl = get_iommu_table_base(dev);
  927. if (!tbl || !tbl->it_group) {
  928. pr_debug("iommu_tce: skipping device %s with no tbl\n",
  929. dev_name(dev));
  930. return 0;
  931. }
  932. pr_debug("iommu_tce: adding %s to iommu group %d\n",
  933. dev_name(dev), iommu_group_id(tbl->it_group));
  934. ret = iommu_group_add_device(tbl->it_group, dev);
  935. if (ret < 0)
  936. pr_err("iommu_tce: %s has not been added, ret=%d\n",
  937. dev_name(dev), ret);
  938. return ret;
  939. }
  940. static void iommu_del_device(struct device *dev)
  941. {
  942. iommu_group_remove_device(dev);
  943. }
  944. static int iommu_bus_notifier(struct notifier_block *nb,
  945. unsigned long action, void *data)
  946. {
  947. struct device *dev = data;
  948. switch (action) {
  949. case BUS_NOTIFY_ADD_DEVICE:
  950. return iommu_add_device(dev);
  951. case BUS_NOTIFY_DEL_DEVICE:
  952. iommu_del_device(dev);
  953. return 0;
  954. default:
  955. return 0;
  956. }
  957. }
  958. static struct notifier_block tce_iommu_bus_nb = {
  959. .notifier_call = iommu_bus_notifier,
  960. };
  961. static int __init tce_iommu_init(void)
  962. {
  963. struct pci_dev *pdev = NULL;
  964. BUILD_BUG_ON(PAGE_SIZE < IOMMU_PAGE_SIZE);
  965. for_each_pci_dev(pdev)
  966. iommu_add_device(&pdev->dev);
  967. bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb);
  968. return 0;
  969. }
  970. subsys_initcall_sync(tce_iommu_init);
  971. #else
  972. void iommu_register_group(struct iommu_table *tbl,
  973. int pci_domain_number, unsigned long pe_num)
  974. {
  975. }
  976. #endif /* CONFIG_IOMMU_API */