iommu.c 36 KB

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