iommu.c 33 KB

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