iommu.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /*
  2. * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
  3. *
  4. * Rewrite, cleanup:
  5. *
  6. * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
  7. * Copyright (C) 2006 Olof Johansson <olof@lixom.net>
  8. *
  9. * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR.
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/mm.h>
  30. #include <linux/memblock.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/string.h>
  33. #include <linux/pci.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/crash_dump.h>
  36. #include <linux/memory.h>
  37. #include <linux/of.h>
  38. #include <asm/io.h>
  39. #include <asm/prom.h>
  40. #include <asm/rtas.h>
  41. #include <asm/iommu.h>
  42. #include <asm/pci-bridge.h>
  43. #include <asm/machdep.h>
  44. #include <asm/firmware.h>
  45. #include <asm/tce.h>
  46. #include <asm/ppc-pci.h>
  47. #include <asm/udbg.h>
  48. #include <asm/mmzone.h>
  49. #include <asm/plpar_wrappers.h>
  50. static void tce_invalidate_pSeries_sw(struct iommu_table *tbl,
  51. __be64 *startp, __be64 *endp)
  52. {
  53. u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
  54. unsigned long start, end, inc;
  55. start = __pa(startp);
  56. end = __pa(endp);
  57. inc = L1_CACHE_BYTES; /* invalidate a cacheline of TCEs at a time */
  58. /* If this is non-zero, change the format. We shift the
  59. * address and or in the magic from the device tree. */
  60. if (tbl->it_busno) {
  61. start <<= 12;
  62. end <<= 12;
  63. inc <<= 12;
  64. start |= tbl->it_busno;
  65. end |= tbl->it_busno;
  66. }
  67. end |= inc - 1; /* round up end to be different than start */
  68. mb(); /* Make sure TCEs in memory are written */
  69. while (start <= end) {
  70. out_be64(invalidate, start);
  71. start += inc;
  72. }
  73. }
  74. static int tce_build_pSeries(struct iommu_table *tbl, long index,
  75. long npages, unsigned long uaddr,
  76. enum dma_data_direction direction,
  77. struct dma_attrs *attrs)
  78. {
  79. u64 proto_tce;
  80. __be64 *tcep, *tces;
  81. u64 rpn;
  82. proto_tce = TCE_PCI_READ; // Read allowed
  83. if (direction != DMA_TO_DEVICE)
  84. proto_tce |= TCE_PCI_WRITE;
  85. tces = tcep = ((__be64 *)tbl->it_base) + index;
  86. while (npages--) {
  87. /* can't move this out since we might cross MEMBLOCK boundary */
  88. rpn = __pa(uaddr) >> TCE_SHIFT;
  89. *tcep = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
  90. uaddr += TCE_PAGE_SIZE;
  91. tcep++;
  92. }
  93. if (tbl->it_type & TCE_PCI_SWINV_CREATE)
  94. tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
  95. return 0;
  96. }
  97. static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
  98. {
  99. __be64 *tcep, *tces;
  100. tces = tcep = ((__be64 *)tbl->it_base) + index;
  101. while (npages--)
  102. *(tcep++) = 0;
  103. if (tbl->it_type & TCE_PCI_SWINV_FREE)
  104. tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
  105. }
  106. static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
  107. {
  108. __be64 *tcep;
  109. tcep = ((__be64 *)tbl->it_base) + index;
  110. return be64_to_cpu(*tcep);
  111. }
  112. static void tce_free_pSeriesLP(struct iommu_table*, long, long);
  113. static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
  114. static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
  115. long npages, unsigned long uaddr,
  116. enum dma_data_direction direction,
  117. struct dma_attrs *attrs)
  118. {
  119. u64 rc = 0;
  120. u64 proto_tce, tce;
  121. u64 rpn;
  122. int ret = 0;
  123. long tcenum_start = tcenum, npages_start = npages;
  124. rpn = __pa(uaddr) >> TCE_SHIFT;
  125. proto_tce = TCE_PCI_READ;
  126. if (direction != DMA_TO_DEVICE)
  127. proto_tce |= TCE_PCI_WRITE;
  128. while (npages--) {
  129. tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
  130. rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
  131. if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
  132. ret = (int)rc;
  133. tce_free_pSeriesLP(tbl, tcenum_start,
  134. (npages_start - (npages + 1)));
  135. break;
  136. }
  137. if (rc && printk_ratelimit()) {
  138. printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
  139. printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
  140. printk("\ttcenum = 0x%llx\n", (u64)tcenum);
  141. printk("\ttce val = 0x%llx\n", tce );
  142. dump_stack();
  143. }
  144. tcenum++;
  145. rpn++;
  146. }
  147. return ret;
  148. }
  149. static DEFINE_PER_CPU(__be64 *, tce_page);
  150. static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
  151. long npages, unsigned long uaddr,
  152. enum dma_data_direction direction,
  153. struct dma_attrs *attrs)
  154. {
  155. u64 rc = 0;
  156. u64 proto_tce;
  157. __be64 *tcep;
  158. u64 rpn;
  159. long l, limit;
  160. long tcenum_start = tcenum, npages_start = npages;
  161. int ret = 0;
  162. unsigned long flags;
  163. if (npages == 1) {
  164. return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
  165. direction, attrs);
  166. }
  167. local_irq_save(flags); /* to protect tcep and the page behind it */
  168. tcep = __this_cpu_read(tce_page);
  169. /* This is safe to do since interrupts are off when we're called
  170. * from iommu_alloc{,_sg}()
  171. */
  172. if (!tcep) {
  173. tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
  174. /* If allocation fails, fall back to the loop implementation */
  175. if (!tcep) {
  176. local_irq_restore(flags);
  177. return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
  178. direction, attrs);
  179. }
  180. __this_cpu_write(tce_page, tcep);
  181. }
  182. rpn = __pa(uaddr) >> TCE_SHIFT;
  183. proto_tce = TCE_PCI_READ;
  184. if (direction != DMA_TO_DEVICE)
  185. proto_tce |= TCE_PCI_WRITE;
  186. /* We can map max one pageful of TCEs at a time */
  187. do {
  188. /*
  189. * Set up the page with TCE data, looping through and setting
  190. * the values.
  191. */
  192. limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
  193. for (l = 0; l < limit; l++) {
  194. tcep[l] = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
  195. rpn++;
  196. }
  197. rc = plpar_tce_put_indirect((u64)tbl->it_index,
  198. (u64)tcenum << 12,
  199. (u64)__pa(tcep),
  200. limit);
  201. npages -= limit;
  202. tcenum += limit;
  203. } while (npages > 0 && !rc);
  204. local_irq_restore(flags);
  205. if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
  206. ret = (int)rc;
  207. tce_freemulti_pSeriesLP(tbl, tcenum_start,
  208. (npages_start - (npages + limit)));
  209. return ret;
  210. }
  211. if (rc && printk_ratelimit()) {
  212. printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
  213. printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
  214. printk("\tnpages = 0x%llx\n", (u64)npages);
  215. printk("\ttce[0] val = 0x%llx\n", tcep[0]);
  216. dump_stack();
  217. }
  218. return ret;
  219. }
  220. static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
  221. {
  222. u64 rc;
  223. while (npages--) {
  224. rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
  225. if (rc && printk_ratelimit()) {
  226. printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
  227. printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
  228. printk("\ttcenum = 0x%llx\n", (u64)tcenum);
  229. dump_stack();
  230. }
  231. tcenum++;
  232. }
  233. }
  234. static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
  235. {
  236. u64 rc;
  237. rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
  238. if (rc && printk_ratelimit()) {
  239. printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
  240. printk("\trc = %lld\n", rc);
  241. printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
  242. printk("\tnpages = 0x%llx\n", (u64)npages);
  243. dump_stack();
  244. }
  245. }
  246. static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
  247. {
  248. u64 rc;
  249. unsigned long tce_ret;
  250. rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
  251. if (rc && printk_ratelimit()) {
  252. printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
  253. printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
  254. printk("\ttcenum = 0x%llx\n", (u64)tcenum);
  255. dump_stack();
  256. }
  257. return tce_ret;
  258. }
  259. /* this is compatible with cells for the device tree property */
  260. struct dynamic_dma_window_prop {
  261. __be32 liobn; /* tce table number */
  262. __be64 dma_base; /* address hi,lo */
  263. __be32 tce_shift; /* ilog2(tce_page_size) */
  264. __be32 window_shift; /* ilog2(tce_window_size) */
  265. };
  266. struct direct_window {
  267. struct device_node *device;
  268. const struct dynamic_dma_window_prop *prop;
  269. struct list_head list;
  270. };
  271. /* Dynamic DMA Window support */
  272. struct ddw_query_response {
  273. u32 windows_available;
  274. u32 largest_available_block;
  275. u32 page_size;
  276. u32 migration_capable;
  277. };
  278. struct ddw_create_response {
  279. u32 liobn;
  280. u32 addr_hi;
  281. u32 addr_lo;
  282. };
  283. static LIST_HEAD(direct_window_list);
  284. /* prevents races between memory on/offline and window creation */
  285. static DEFINE_SPINLOCK(direct_window_list_lock);
  286. /* protects initializing window twice for same device */
  287. static DEFINE_MUTEX(direct_window_init_mutex);
  288. #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
  289. static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,
  290. unsigned long num_pfn, const void *arg)
  291. {
  292. const struct dynamic_dma_window_prop *maprange = arg;
  293. int rc;
  294. u64 tce_size, num_tce, dma_offset, next;
  295. u32 tce_shift;
  296. long limit;
  297. tce_shift = be32_to_cpu(maprange->tce_shift);
  298. tce_size = 1ULL << tce_shift;
  299. next = start_pfn << PAGE_SHIFT;
  300. num_tce = num_pfn << PAGE_SHIFT;
  301. /* round back to the beginning of the tce page size */
  302. num_tce += next & (tce_size - 1);
  303. next &= ~(tce_size - 1);
  304. /* covert to number of tces */
  305. num_tce |= tce_size - 1;
  306. num_tce >>= tce_shift;
  307. do {
  308. /*
  309. * Set up the page with TCE data, looping through and setting
  310. * the values.
  311. */
  312. limit = min_t(long, num_tce, 512);
  313. dma_offset = next + be64_to_cpu(maprange->dma_base);
  314. rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn),
  315. dma_offset,
  316. 0, limit);
  317. next += limit * tce_size;
  318. num_tce -= limit;
  319. } while (num_tce > 0 && !rc);
  320. return rc;
  321. }
  322. static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
  323. unsigned long num_pfn, const void *arg)
  324. {
  325. const struct dynamic_dma_window_prop *maprange = arg;
  326. u64 tce_size, num_tce, dma_offset, next, proto_tce, liobn;
  327. __be64 *tcep;
  328. u32 tce_shift;
  329. u64 rc = 0;
  330. long l, limit;
  331. local_irq_disable(); /* to protect tcep and the page behind it */
  332. tcep = __this_cpu_read(tce_page);
  333. if (!tcep) {
  334. tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
  335. if (!tcep) {
  336. local_irq_enable();
  337. return -ENOMEM;
  338. }
  339. __this_cpu_write(tce_page, tcep);
  340. }
  341. proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
  342. liobn = (u64)be32_to_cpu(maprange->liobn);
  343. tce_shift = be32_to_cpu(maprange->tce_shift);
  344. tce_size = 1ULL << tce_shift;
  345. next = start_pfn << PAGE_SHIFT;
  346. num_tce = num_pfn << PAGE_SHIFT;
  347. /* round back to the beginning of the tce page size */
  348. num_tce += next & (tce_size - 1);
  349. next &= ~(tce_size - 1);
  350. /* covert to number of tces */
  351. num_tce |= tce_size - 1;
  352. num_tce >>= tce_shift;
  353. /* We can map max one pageful of TCEs at a time */
  354. do {
  355. /*
  356. * Set up the page with TCE data, looping through and setting
  357. * the values.
  358. */
  359. limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
  360. dma_offset = next + be64_to_cpu(maprange->dma_base);
  361. for (l = 0; l < limit; l++) {
  362. tcep[l] = cpu_to_be64(proto_tce | next);
  363. next += tce_size;
  364. }
  365. rc = plpar_tce_put_indirect(liobn,
  366. dma_offset,
  367. (u64)__pa(tcep),
  368. limit);
  369. num_tce -= limit;
  370. } while (num_tce > 0 && !rc);
  371. /* error cleanup: caller will clear whole range */
  372. local_irq_enable();
  373. return rc;
  374. }
  375. static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
  376. unsigned long num_pfn, void *arg)
  377. {
  378. return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
  379. }
  380. #ifdef CONFIG_PCI
  381. static void iommu_table_setparms(struct pci_controller *phb,
  382. struct device_node *dn,
  383. struct iommu_table *tbl)
  384. {
  385. struct device_node *node;
  386. const unsigned long *basep, *sw_inval;
  387. const u32 *sizep;
  388. node = phb->dn;
  389. basep = of_get_property(node, "linux,tce-base", NULL);
  390. sizep = of_get_property(node, "linux,tce-size", NULL);
  391. if (basep == NULL || sizep == NULL) {
  392. printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
  393. "missing tce entries !\n", dn->full_name);
  394. return;
  395. }
  396. tbl->it_base = (unsigned long)__va(*basep);
  397. if (!is_kdump_kernel())
  398. memset((void *)tbl->it_base, 0, *sizep);
  399. tbl->it_busno = phb->bus->number;
  400. tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
  401. /* Units of tce entries */
  402. tbl->it_offset = phb->dma_window_base_cur >> tbl->it_page_shift;
  403. /* Test if we are going over 2GB of DMA space */
  404. if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
  405. udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  406. panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  407. }
  408. phb->dma_window_base_cur += phb->dma_window_size;
  409. /* Set the tce table size - measured in entries */
  410. tbl->it_size = phb->dma_window_size >> tbl->it_page_shift;
  411. tbl->it_index = 0;
  412. tbl->it_blocksize = 16;
  413. tbl->it_type = TCE_PCI;
  414. sw_inval = of_get_property(node, "linux,tce-sw-invalidate-info", NULL);
  415. if (sw_inval) {
  416. /*
  417. * This property contains information on how to
  418. * invalidate the TCE entry. The first property is
  419. * the base MMIO address used to invalidate entries.
  420. * The second property tells us the format of the TCE
  421. * invalidate (whether it needs to be shifted) and
  422. * some magic routing info to add to our invalidate
  423. * command.
  424. */
  425. tbl->it_index = (unsigned long) ioremap(sw_inval[0], 8);
  426. tbl->it_busno = sw_inval[1]; /* overload this with magic */
  427. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
  428. }
  429. }
  430. /*
  431. * iommu_table_setparms_lpar
  432. *
  433. * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
  434. */
  435. static void iommu_table_setparms_lpar(struct pci_controller *phb,
  436. struct device_node *dn,
  437. struct iommu_table *tbl,
  438. const __be32 *dma_window)
  439. {
  440. unsigned long offset, size;
  441. of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
  442. tbl->it_busno = phb->bus->number;
  443. tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
  444. tbl->it_base = 0;
  445. tbl->it_blocksize = 16;
  446. tbl->it_type = TCE_PCI;
  447. tbl->it_offset = offset >> tbl->it_page_shift;
  448. tbl->it_size = size >> tbl->it_page_shift;
  449. }
  450. static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
  451. {
  452. struct device_node *dn;
  453. struct iommu_table *tbl;
  454. struct device_node *isa_dn, *isa_dn_orig;
  455. struct device_node *tmp;
  456. struct pci_dn *pci;
  457. int children;
  458. dn = pci_bus_to_OF_node(bus);
  459. pr_debug("pci_dma_bus_setup_pSeries: setting up bus %s\n", dn->full_name);
  460. if (bus->self) {
  461. /* This is not a root bus, any setup will be done for the
  462. * device-side of the bridge in iommu_dev_setup_pSeries().
  463. */
  464. return;
  465. }
  466. pci = PCI_DN(dn);
  467. /* Check if the ISA bus on the system is under
  468. * this PHB.
  469. */
  470. isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
  471. while (isa_dn && isa_dn != dn)
  472. isa_dn = isa_dn->parent;
  473. of_node_put(isa_dn_orig);
  474. /* Count number of direct PCI children of the PHB. */
  475. for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
  476. children++;
  477. pr_debug("Children: %d\n", children);
  478. /* Calculate amount of DMA window per slot. Each window must be
  479. * a power of two (due to pci_alloc_consistent requirements).
  480. *
  481. * Keep 256MB aside for PHBs with ISA.
  482. */
  483. if (!isa_dn) {
  484. /* No ISA/IDE - just set window size and return */
  485. pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
  486. while (pci->phb->dma_window_size * children > 0x80000000ul)
  487. pci->phb->dma_window_size >>= 1;
  488. pr_debug("No ISA/IDE, window size is 0x%llx\n",
  489. pci->phb->dma_window_size);
  490. pci->phb->dma_window_base_cur = 0;
  491. return;
  492. }
  493. /* If we have ISA, then we probably have an IDE
  494. * controller too. Allocate a 128MB table but
  495. * skip the first 128MB to avoid stepping on ISA
  496. * space.
  497. */
  498. pci->phb->dma_window_size = 0x8000000ul;
  499. pci->phb->dma_window_base_cur = 0x8000000ul;
  500. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  501. pci->phb->node);
  502. iommu_table_setparms(pci->phb, dn, tbl);
  503. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  504. iommu_register_group(tbl, pci_domain_nr(bus), 0);
  505. /* Divide the rest (1.75GB) among the children */
  506. pci->phb->dma_window_size = 0x80000000ul;
  507. while (pci->phb->dma_window_size * children > 0x70000000ul)
  508. pci->phb->dma_window_size >>= 1;
  509. pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
  510. }
  511. static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
  512. {
  513. struct iommu_table *tbl;
  514. struct device_node *dn, *pdn;
  515. struct pci_dn *ppci;
  516. const __be32 *dma_window = NULL;
  517. dn = pci_bus_to_OF_node(bus);
  518. pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
  519. dn->full_name);
  520. /* Find nearest ibm,dma-window, walking up the device tree */
  521. for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
  522. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  523. if (dma_window != NULL)
  524. break;
  525. }
  526. if (dma_window == NULL) {
  527. pr_debug(" no ibm,dma-window property !\n");
  528. return;
  529. }
  530. ppci = PCI_DN(pdn);
  531. pr_debug(" parent is %s, iommu_table: 0x%p\n",
  532. pdn->full_name, ppci->iommu_table);
  533. if (!ppci->iommu_table) {
  534. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  535. ppci->phb->node);
  536. iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
  537. ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
  538. iommu_register_group(tbl, pci_domain_nr(bus), 0);
  539. pr_debug(" created table: %p\n", ppci->iommu_table);
  540. }
  541. }
  542. static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
  543. {
  544. struct device_node *dn;
  545. struct iommu_table *tbl;
  546. pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
  547. dn = dev->dev.of_node;
  548. /* If we're the direct child of a root bus, then we need to allocate
  549. * an iommu table ourselves. The bus setup code should have setup
  550. * the window sizes already.
  551. */
  552. if (!dev->bus->self) {
  553. struct pci_controller *phb = PCI_DN(dn)->phb;
  554. pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
  555. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  556. phb->node);
  557. iommu_table_setparms(phb, dn, tbl);
  558. PCI_DN(dn)->iommu_table = iommu_init_table(tbl, phb->node);
  559. iommu_register_group(tbl, pci_domain_nr(phb->bus), 0);
  560. set_iommu_table_base_and_group(&dev->dev,
  561. PCI_DN(dn)->iommu_table);
  562. return;
  563. }
  564. /* If this device is further down the bus tree, search upwards until
  565. * an already allocated iommu table is found and use that.
  566. */
  567. while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
  568. dn = dn->parent;
  569. if (dn && PCI_DN(dn))
  570. set_iommu_table_base_and_group(&dev->dev,
  571. PCI_DN(dn)->iommu_table);
  572. else
  573. printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
  574. pci_name(dev));
  575. }
  576. static int __read_mostly disable_ddw;
  577. static int __init disable_ddw_setup(char *str)
  578. {
  579. disable_ddw = 1;
  580. printk(KERN_INFO "ppc iommu: disabling ddw.\n");
  581. return 0;
  582. }
  583. early_param("disable_ddw", disable_ddw_setup);
  584. static void remove_ddw(struct device_node *np, bool remove_prop)
  585. {
  586. struct dynamic_dma_window_prop *dwp;
  587. struct property *win64;
  588. u32 ddw_avail[3];
  589. u64 liobn;
  590. int ret = 0;
  591. ret = of_property_read_u32_array(np, "ibm,ddw-applicable",
  592. &ddw_avail[0], 3);
  593. win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
  594. if (!win64)
  595. return;
  596. if (ret || win64->length < sizeof(*dwp))
  597. goto delprop;
  598. dwp = win64->value;
  599. liobn = (u64)be32_to_cpu(dwp->liobn);
  600. /* clear the whole window, note the arg is in kernel pages */
  601. ret = tce_clearrange_multi_pSeriesLP(0,
  602. 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
  603. if (ret)
  604. pr_warning("%s failed to clear tces in window.\n",
  605. np->full_name);
  606. else
  607. pr_debug("%s successfully cleared tces in window.\n",
  608. np->full_name);
  609. ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
  610. if (ret)
  611. pr_warning("%s: failed to remove direct window: rtas returned "
  612. "%d to ibm,remove-pe-dma-window(%x) %llx\n",
  613. np->full_name, ret, ddw_avail[2], liobn);
  614. else
  615. pr_debug("%s: successfully removed direct window: rtas returned "
  616. "%d to ibm,remove-pe-dma-window(%x) %llx\n",
  617. np->full_name, ret, ddw_avail[2], liobn);
  618. delprop:
  619. if (remove_prop)
  620. ret = of_remove_property(np, win64);
  621. if (ret)
  622. pr_warning("%s: failed to remove direct window property: %d\n",
  623. np->full_name, ret);
  624. }
  625. static u64 find_existing_ddw(struct device_node *pdn)
  626. {
  627. struct direct_window *window;
  628. const struct dynamic_dma_window_prop *direct64;
  629. u64 dma_addr = 0;
  630. spin_lock(&direct_window_list_lock);
  631. /* check if we already created a window and dupe that config if so */
  632. list_for_each_entry(window, &direct_window_list, list) {
  633. if (window->device == pdn) {
  634. direct64 = window->prop;
  635. dma_addr = be64_to_cpu(direct64->dma_base);
  636. break;
  637. }
  638. }
  639. spin_unlock(&direct_window_list_lock);
  640. return dma_addr;
  641. }
  642. static int find_existing_ddw_windows(void)
  643. {
  644. int len;
  645. struct device_node *pdn;
  646. struct direct_window *window;
  647. const struct dynamic_dma_window_prop *direct64;
  648. if (!firmware_has_feature(FW_FEATURE_LPAR))
  649. return 0;
  650. for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
  651. direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
  652. if (!direct64)
  653. continue;
  654. window = kzalloc(sizeof(*window), GFP_KERNEL);
  655. if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
  656. kfree(window);
  657. remove_ddw(pdn, true);
  658. continue;
  659. }
  660. window->device = pdn;
  661. window->prop = direct64;
  662. spin_lock(&direct_window_list_lock);
  663. list_add(&window->list, &direct_window_list);
  664. spin_unlock(&direct_window_list_lock);
  665. }
  666. return 0;
  667. }
  668. machine_arch_initcall(pseries, find_existing_ddw_windows);
  669. static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
  670. struct ddw_query_response *query)
  671. {
  672. struct eeh_dev *edev;
  673. u32 cfg_addr;
  674. u64 buid;
  675. int ret;
  676. /*
  677. * Get the config address and phb buid of the PE window.
  678. * Rely on eeh to retrieve this for us.
  679. * Retrieve them from the pci device, not the node with the
  680. * dma-window property
  681. */
  682. edev = pci_dev_to_eeh_dev(dev);
  683. cfg_addr = edev->config_addr;
  684. if (edev->pe_config_addr)
  685. cfg_addr = edev->pe_config_addr;
  686. buid = edev->phb->buid;
  687. ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
  688. cfg_addr, BUID_HI(buid), BUID_LO(buid));
  689. dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
  690. " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
  691. BUID_LO(buid), ret);
  692. return ret;
  693. }
  694. static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
  695. struct ddw_create_response *create, int page_shift,
  696. int window_shift)
  697. {
  698. struct eeh_dev *edev;
  699. u32 cfg_addr;
  700. u64 buid;
  701. int ret;
  702. /*
  703. * Get the config address and phb buid of the PE window.
  704. * Rely on eeh to retrieve this for us.
  705. * Retrieve them from the pci device, not the node with the
  706. * dma-window property
  707. */
  708. edev = pci_dev_to_eeh_dev(dev);
  709. cfg_addr = edev->config_addr;
  710. if (edev->pe_config_addr)
  711. cfg_addr = edev->pe_config_addr;
  712. buid = edev->phb->buid;
  713. do {
  714. /* extra outputs are LIOBN and dma-addr (hi, lo) */
  715. ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create,
  716. cfg_addr, BUID_HI(buid), BUID_LO(buid),
  717. page_shift, window_shift);
  718. } while (rtas_busy_delay(ret));
  719. dev_info(&dev->dev,
  720. "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
  721. "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
  722. cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
  723. window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
  724. return ret;
  725. }
  726. struct failed_ddw_pdn {
  727. struct device_node *pdn;
  728. struct list_head list;
  729. };
  730. static LIST_HEAD(failed_ddw_pdn_list);
  731. /*
  732. * If the PE supports dynamic dma windows, and there is space for a table
  733. * that can map all pages in a linear offset, then setup such a table,
  734. * and record the dma-offset in the struct device.
  735. *
  736. * dev: the pci device we are checking
  737. * pdn: the parent pe node with the ibm,dma_window property
  738. * Future: also check if we can remap the base window for our base page size
  739. *
  740. * returns the dma offset for use by dma_set_mask
  741. */
  742. static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
  743. {
  744. int len, ret;
  745. struct ddw_query_response query;
  746. struct ddw_create_response create;
  747. int page_shift;
  748. u64 dma_addr, max_addr;
  749. struct device_node *dn;
  750. u32 ddw_avail[3];
  751. struct direct_window *window;
  752. struct property *win64;
  753. struct dynamic_dma_window_prop *ddwprop;
  754. struct failed_ddw_pdn *fpdn;
  755. mutex_lock(&direct_window_init_mutex);
  756. dma_addr = find_existing_ddw(pdn);
  757. if (dma_addr != 0)
  758. goto out_unlock;
  759. /*
  760. * If we already went through this for a previous function of
  761. * the same device and failed, we don't want to muck with the
  762. * DMA window again, as it will race with in-flight operations
  763. * and can lead to EEHs. The above mutex protects access to the
  764. * list.
  765. */
  766. list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) {
  767. if (!strcmp(fpdn->pdn->full_name, pdn->full_name))
  768. goto out_unlock;
  769. }
  770. /*
  771. * the ibm,ddw-applicable property holds the tokens for:
  772. * ibm,query-pe-dma-window
  773. * ibm,create-pe-dma-window
  774. * ibm,remove-pe-dma-window
  775. * for the given node in that order.
  776. * the property is actually in the parent, not the PE
  777. */
  778. ret = of_property_read_u32_array(pdn, "ibm,ddw-applicable",
  779. &ddw_avail[0], 3);
  780. if (ret)
  781. goto out_failed;
  782. /*
  783. * Query if there is a second window of size to map the
  784. * whole partition. Query returns number of windows, largest
  785. * block assigned to PE (partition endpoint), and two bitmasks
  786. * of page sizes: supported and supported for migrate-dma.
  787. */
  788. dn = pci_device_to_OF_node(dev);
  789. ret = query_ddw(dev, ddw_avail, &query);
  790. if (ret != 0)
  791. goto out_failed;
  792. if (query.windows_available == 0) {
  793. /*
  794. * no additional windows are available for this device.
  795. * We might be able to reallocate the existing window,
  796. * trading in for a larger page size.
  797. */
  798. dev_dbg(&dev->dev, "no free dynamic windows");
  799. goto out_failed;
  800. }
  801. if (query.page_size & 4) {
  802. page_shift = 24; /* 16MB */
  803. } else if (query.page_size & 2) {
  804. page_shift = 16; /* 64kB */
  805. } else if (query.page_size & 1) {
  806. page_shift = 12; /* 4kB */
  807. } else {
  808. dev_dbg(&dev->dev, "no supported direct page size in mask %x",
  809. query.page_size);
  810. goto out_failed;
  811. }
  812. /* verify the window * number of ptes will map the partition */
  813. /* check largest block * page size > max memory hotplug addr */
  814. max_addr = memory_hotplug_max();
  815. if (query.largest_available_block < (max_addr >> page_shift)) {
  816. dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
  817. "%llu-sized pages\n", max_addr, query.largest_available_block,
  818. 1ULL << page_shift);
  819. goto out_failed;
  820. }
  821. len = order_base_2(max_addr);
  822. win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
  823. if (!win64) {
  824. dev_info(&dev->dev,
  825. "couldn't allocate property for 64bit dma window\n");
  826. goto out_failed;
  827. }
  828. win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
  829. win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
  830. win64->length = sizeof(*ddwprop);
  831. if (!win64->name || !win64->value) {
  832. dev_info(&dev->dev,
  833. "couldn't allocate property name and value\n");
  834. goto out_free_prop;
  835. }
  836. ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
  837. if (ret != 0)
  838. goto out_free_prop;
  839. ddwprop->liobn = cpu_to_be32(create.liobn);
  840. ddwprop->dma_base = cpu_to_be64(((u64)create.addr_hi << 32) |
  841. create.addr_lo);
  842. ddwprop->tce_shift = cpu_to_be32(page_shift);
  843. ddwprop->window_shift = cpu_to_be32(len);
  844. dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
  845. create.liobn, dn->full_name);
  846. window = kzalloc(sizeof(*window), GFP_KERNEL);
  847. if (!window)
  848. goto out_clear_window;
  849. ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
  850. win64->value, tce_setrange_multi_pSeriesLP_walk);
  851. if (ret) {
  852. dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
  853. dn->full_name, ret);
  854. goto out_free_window;
  855. }
  856. ret = of_add_property(pdn, win64);
  857. if (ret) {
  858. dev_err(&dev->dev, "unable to add dma window property for %s: %d",
  859. pdn->full_name, ret);
  860. goto out_free_window;
  861. }
  862. window->device = pdn;
  863. window->prop = ddwprop;
  864. spin_lock(&direct_window_list_lock);
  865. list_add(&window->list, &direct_window_list);
  866. spin_unlock(&direct_window_list_lock);
  867. dma_addr = be64_to_cpu(ddwprop->dma_base);
  868. goto out_unlock;
  869. out_free_window:
  870. kfree(window);
  871. out_clear_window:
  872. remove_ddw(pdn, true);
  873. out_free_prop:
  874. kfree(win64->name);
  875. kfree(win64->value);
  876. kfree(win64);
  877. out_failed:
  878. fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
  879. if (!fpdn)
  880. goto out_unlock;
  881. fpdn->pdn = pdn;
  882. list_add(&fpdn->list, &failed_ddw_pdn_list);
  883. out_unlock:
  884. mutex_unlock(&direct_window_init_mutex);
  885. return dma_addr;
  886. }
  887. static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
  888. {
  889. struct device_node *pdn, *dn;
  890. struct iommu_table *tbl;
  891. const __be32 *dma_window = NULL;
  892. struct pci_dn *pci;
  893. pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
  894. /* dev setup for LPAR is a little tricky, since the device tree might
  895. * contain the dma-window properties per-device and not necessarily
  896. * for the bus. So we need to search upwards in the tree until we
  897. * either hit a dma-window property, OR find a parent with a table
  898. * already allocated.
  899. */
  900. dn = pci_device_to_OF_node(dev);
  901. pr_debug(" node is %s\n", dn->full_name);
  902. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  903. pdn = pdn->parent) {
  904. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  905. if (dma_window)
  906. break;
  907. }
  908. if (!pdn || !PCI_DN(pdn)) {
  909. printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
  910. "no DMA window found for pci dev=%s dn=%s\n",
  911. pci_name(dev), of_node_full_name(dn));
  912. return;
  913. }
  914. pr_debug(" parent is %s\n", pdn->full_name);
  915. pci = PCI_DN(pdn);
  916. if (!pci->iommu_table) {
  917. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  918. pci->phb->node);
  919. iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
  920. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  921. iommu_register_group(tbl, pci_domain_nr(pci->phb->bus), 0);
  922. pr_debug(" created table: %p\n", pci->iommu_table);
  923. } else {
  924. pr_debug(" found DMA window, table: %p\n", pci->iommu_table);
  925. }
  926. set_iommu_table_base_and_group(&dev->dev, pci->iommu_table);
  927. }
  928. static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
  929. {
  930. bool ddw_enabled = false;
  931. struct device_node *pdn, *dn;
  932. struct pci_dev *pdev;
  933. const __be32 *dma_window = NULL;
  934. u64 dma_offset;
  935. if (!dev->dma_mask)
  936. return -EIO;
  937. if (!dev_is_pci(dev))
  938. goto check_mask;
  939. pdev = to_pci_dev(dev);
  940. /* only attempt to use a new window if 64-bit DMA is requested */
  941. if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
  942. dn = pci_device_to_OF_node(pdev);
  943. dev_dbg(dev, "node is %s\n", dn->full_name);
  944. /*
  945. * the device tree might contain the dma-window properties
  946. * per-device and not necessarily for the bus. So we need to
  947. * search upwards in the tree until we either hit a dma-window
  948. * property, OR find a parent with a table already allocated.
  949. */
  950. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  951. pdn = pdn->parent) {
  952. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  953. if (dma_window)
  954. break;
  955. }
  956. if (pdn && PCI_DN(pdn)) {
  957. dma_offset = enable_ddw(pdev, pdn);
  958. if (dma_offset != 0) {
  959. dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
  960. set_dma_offset(dev, dma_offset);
  961. set_dma_ops(dev, &dma_direct_ops);
  962. ddw_enabled = true;
  963. }
  964. }
  965. }
  966. /* fall back on iommu ops, restore table pointer with ops */
  967. if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
  968. dev_info(dev, "Restoring 32-bit DMA via iommu\n");
  969. set_dma_ops(dev, &dma_iommu_ops);
  970. pci_dma_dev_setup_pSeriesLP(pdev);
  971. }
  972. check_mask:
  973. if (!dma_supported(dev, dma_mask))
  974. return -EIO;
  975. *dev->dma_mask = dma_mask;
  976. return 0;
  977. }
  978. static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
  979. {
  980. if (!dev->dma_mask)
  981. return 0;
  982. if (!disable_ddw && dev_is_pci(dev)) {
  983. struct pci_dev *pdev = to_pci_dev(dev);
  984. struct device_node *dn;
  985. dn = pci_device_to_OF_node(pdev);
  986. /* search upwards for ibm,dma-window */
  987. for (; dn && PCI_DN(dn) && !PCI_DN(dn)->iommu_table;
  988. dn = dn->parent)
  989. if (of_get_property(dn, "ibm,dma-window", NULL))
  990. break;
  991. /* if there is a ibm,ddw-applicable property require 64 bits */
  992. if (dn && PCI_DN(dn) &&
  993. of_get_property(dn, "ibm,ddw-applicable", NULL))
  994. return DMA_BIT_MASK(64);
  995. }
  996. return dma_iommu_ops.get_required_mask(dev);
  997. }
  998. #else /* CONFIG_PCI */
  999. #define pci_dma_bus_setup_pSeries NULL
  1000. #define pci_dma_dev_setup_pSeries NULL
  1001. #define pci_dma_bus_setup_pSeriesLP NULL
  1002. #define pci_dma_dev_setup_pSeriesLP NULL
  1003. #define dma_set_mask_pSeriesLP NULL
  1004. #define dma_get_required_mask_pSeriesLP NULL
  1005. #endif /* !CONFIG_PCI */
  1006. static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
  1007. void *data)
  1008. {
  1009. struct direct_window *window;
  1010. struct memory_notify *arg = data;
  1011. int ret = 0;
  1012. switch (action) {
  1013. case MEM_GOING_ONLINE:
  1014. spin_lock(&direct_window_list_lock);
  1015. list_for_each_entry(window, &direct_window_list, list) {
  1016. ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
  1017. arg->nr_pages, window->prop);
  1018. /* XXX log error */
  1019. }
  1020. spin_unlock(&direct_window_list_lock);
  1021. break;
  1022. case MEM_CANCEL_ONLINE:
  1023. case MEM_OFFLINE:
  1024. spin_lock(&direct_window_list_lock);
  1025. list_for_each_entry(window, &direct_window_list, list) {
  1026. ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
  1027. arg->nr_pages, window->prop);
  1028. /* XXX log error */
  1029. }
  1030. spin_unlock(&direct_window_list_lock);
  1031. break;
  1032. default:
  1033. break;
  1034. }
  1035. if (ret && action != MEM_CANCEL_ONLINE)
  1036. return NOTIFY_BAD;
  1037. return NOTIFY_OK;
  1038. }
  1039. static struct notifier_block iommu_mem_nb = {
  1040. .notifier_call = iommu_mem_notifier,
  1041. };
  1042. static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
  1043. {
  1044. int err = NOTIFY_OK;
  1045. struct of_reconfig_data *rd = data;
  1046. struct device_node *np = rd->dn;
  1047. struct pci_dn *pci = PCI_DN(np);
  1048. struct direct_window *window;
  1049. switch (action) {
  1050. case OF_RECONFIG_DETACH_NODE:
  1051. /*
  1052. * Removing the property will invoke the reconfig
  1053. * notifier again, which causes dead-lock on the
  1054. * read-write semaphore of the notifier chain. So
  1055. * we have to remove the property when releasing
  1056. * the device node.
  1057. */
  1058. remove_ddw(np, false);
  1059. if (pci && pci->iommu_table)
  1060. iommu_free_table(pci->iommu_table, np->full_name);
  1061. spin_lock(&direct_window_list_lock);
  1062. list_for_each_entry(window, &direct_window_list, list) {
  1063. if (window->device == np) {
  1064. list_del(&window->list);
  1065. kfree(window);
  1066. break;
  1067. }
  1068. }
  1069. spin_unlock(&direct_window_list_lock);
  1070. break;
  1071. default:
  1072. err = NOTIFY_DONE;
  1073. break;
  1074. }
  1075. return err;
  1076. }
  1077. static struct notifier_block iommu_reconfig_nb = {
  1078. .notifier_call = iommu_reconfig_notifier,
  1079. };
  1080. /* These are called very early. */
  1081. void iommu_init_early_pSeries(void)
  1082. {
  1083. if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
  1084. return;
  1085. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  1086. if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
  1087. ppc_md.tce_build = tce_buildmulti_pSeriesLP;
  1088. ppc_md.tce_free = tce_freemulti_pSeriesLP;
  1089. } else {
  1090. ppc_md.tce_build = tce_build_pSeriesLP;
  1091. ppc_md.tce_free = tce_free_pSeriesLP;
  1092. }
  1093. ppc_md.tce_get = tce_get_pSeriesLP;
  1094. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
  1095. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
  1096. ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
  1097. ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
  1098. } else {
  1099. ppc_md.tce_build = tce_build_pSeries;
  1100. ppc_md.tce_free = tce_free_pSeries;
  1101. ppc_md.tce_get = tce_get_pseries;
  1102. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeries;
  1103. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeries;
  1104. }
  1105. of_reconfig_notifier_register(&iommu_reconfig_nb);
  1106. register_memory_notifier(&iommu_mem_nb);
  1107. set_pci_dma_ops(&dma_iommu_ops);
  1108. }
  1109. static int __init disable_multitce(char *str)
  1110. {
  1111. if (strcmp(str, "off") == 0 &&
  1112. firmware_has_feature(FW_FEATURE_LPAR) &&
  1113. firmware_has_feature(FW_FEATURE_MULTITCE)) {
  1114. printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
  1115. ppc_md.tce_build = tce_build_pSeriesLP;
  1116. ppc_md.tce_free = tce_free_pSeriesLP;
  1117. powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
  1118. }
  1119. return 1;
  1120. }
  1121. __setup("multitce=", disable_multitce);