iommu.c 30 KB

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