iommu.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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/sched.h> /* for show_stack */
  33. #include <linux/string.h>
  34. #include <linux/pci.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/crash_dump.h>
  37. #include <linux/memory.h>
  38. #include <linux/of.h>
  39. #include <asm/io.h>
  40. #include <asm/prom.h>
  41. #include <asm/rtas.h>
  42. #include <asm/iommu.h>
  43. #include <asm/pci-bridge.h>
  44. #include <asm/machdep.h>
  45. #include <asm/firmware.h>
  46. #include <asm/tce.h>
  47. #include <asm/ppc-pci.h>
  48. #include <asm/udbg.h>
  49. #include <asm/mmzone.h>
  50. #include <asm/plpar_wrappers.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. show_stack(current, (unsigned long *)__get_SP());
  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 = __get_cpu_var(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. __get_cpu_var(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. show_stack(current, (unsigned long *)__get_SP());
  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. show_stack(current, (unsigned long *)__get_SP());
  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. show_stack(current, (unsigned long *)__get_SP());
  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. show_stack(current, (unsigned long *)__get_SP());
  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. __be32 windows_available;
  275. __be32 largest_available_block;
  276. __be32 page_size;
  277. __be32 migration_capable;
  278. };
  279. struct ddw_create_response {
  280. __be32 liobn;
  281. __be32 addr_hi;
  282. __be32 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 = __get_cpu_var(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. __get_cpu_var(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. if (isa_dn_orig)
  475. of_node_put(isa_dn_orig);
  476. /* Count number of direct PCI children of the PHB. */
  477. for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
  478. children++;
  479. pr_debug("Children: %d\n", children);
  480. /* Calculate amount of DMA window per slot. Each window must be
  481. * a power of two (due to pci_alloc_consistent requirements).
  482. *
  483. * Keep 256MB aside for PHBs with ISA.
  484. */
  485. if (!isa_dn) {
  486. /* No ISA/IDE - just set window size and return */
  487. pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
  488. while (pci->phb->dma_window_size * children > 0x80000000ul)
  489. pci->phb->dma_window_size >>= 1;
  490. pr_debug("No ISA/IDE, window size is 0x%llx\n",
  491. pci->phb->dma_window_size);
  492. pci->phb->dma_window_base_cur = 0;
  493. return;
  494. }
  495. /* If we have ISA, then we probably have an IDE
  496. * controller too. Allocate a 128MB table but
  497. * skip the first 128MB to avoid stepping on ISA
  498. * space.
  499. */
  500. pci->phb->dma_window_size = 0x8000000ul;
  501. pci->phb->dma_window_base_cur = 0x8000000ul;
  502. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  503. pci->phb->node);
  504. iommu_table_setparms(pci->phb, dn, tbl);
  505. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  506. iommu_register_group(tbl, pci_domain_nr(bus), 0);
  507. /* Divide the rest (1.75GB) among the children */
  508. pci->phb->dma_window_size = 0x80000000ul;
  509. while (pci->phb->dma_window_size * children > 0x70000000ul)
  510. pci->phb->dma_window_size >>= 1;
  511. pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
  512. }
  513. static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
  514. {
  515. struct iommu_table *tbl;
  516. struct device_node *dn, *pdn;
  517. struct pci_dn *ppci;
  518. const __be32 *dma_window = NULL;
  519. dn = pci_bus_to_OF_node(bus);
  520. pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
  521. dn->full_name);
  522. /* Find nearest ibm,dma-window, walking up the device tree */
  523. for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
  524. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  525. if (dma_window != NULL)
  526. break;
  527. }
  528. if (dma_window == NULL) {
  529. pr_debug(" no ibm,dma-window property !\n");
  530. return;
  531. }
  532. ppci = PCI_DN(pdn);
  533. pr_debug(" parent is %s, iommu_table: 0x%p\n",
  534. pdn->full_name, ppci->iommu_table);
  535. if (!ppci->iommu_table) {
  536. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  537. ppci->phb->node);
  538. iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
  539. ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
  540. iommu_register_group(tbl, pci_domain_nr(bus), 0);
  541. pr_debug(" created table: %p\n", ppci->iommu_table);
  542. }
  543. }
  544. static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
  545. {
  546. struct device_node *dn;
  547. struct iommu_table *tbl;
  548. pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
  549. dn = dev->dev.of_node;
  550. /* If we're the direct child of a root bus, then we need to allocate
  551. * an iommu table ourselves. The bus setup code should have setup
  552. * the window sizes already.
  553. */
  554. if (!dev->bus->self) {
  555. struct pci_controller *phb = PCI_DN(dn)->phb;
  556. pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
  557. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  558. phb->node);
  559. iommu_table_setparms(phb, dn, tbl);
  560. PCI_DN(dn)->iommu_table = iommu_init_table(tbl, phb->node);
  561. iommu_register_group(tbl, pci_domain_nr(phb->bus), 0);
  562. set_iommu_table_base_and_group(&dev->dev,
  563. PCI_DN(dn)->iommu_table);
  564. return;
  565. }
  566. /* If this device is further down the bus tree, search upwards until
  567. * an already allocated iommu table is found and use that.
  568. */
  569. while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
  570. dn = dn->parent;
  571. if (dn && PCI_DN(dn))
  572. set_iommu_table_base_and_group(&dev->dev,
  573. PCI_DN(dn)->iommu_table);
  574. else
  575. printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
  576. pci_name(dev));
  577. }
  578. static int __read_mostly disable_ddw;
  579. static int __init disable_ddw_setup(char *str)
  580. {
  581. disable_ddw = 1;
  582. printk(KERN_INFO "ppc iommu: disabling ddw.\n");
  583. return 0;
  584. }
  585. early_param("disable_ddw", disable_ddw_setup);
  586. static void remove_ddw(struct device_node *np)
  587. {
  588. struct dynamic_dma_window_prop *dwp;
  589. struct property *win64;
  590. const u32 *ddw_avail;
  591. u64 liobn;
  592. int len, ret;
  593. ddw_avail = of_get_property(np, "ibm,ddw-applicable", &len);
  594. win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
  595. if (!win64)
  596. return;
  597. if (!ddw_avail || len < 3 * sizeof(u32) || 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. 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);
  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, cfg_addr,
  716. BUID_HI(buid), BUID_LO(buid), page_shift, window_shift);
  717. } while (rtas_busy_delay(ret));
  718. dev_info(&dev->dev,
  719. "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
  720. "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
  721. cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
  722. window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
  723. return ret;
  724. }
  725. struct failed_ddw_pdn {
  726. struct device_node *pdn;
  727. struct list_head list;
  728. };
  729. static LIST_HEAD(failed_ddw_pdn_list);
  730. /*
  731. * If the PE supports dynamic dma windows, and there is space for a table
  732. * that can map all pages in a linear offset, then setup such a table,
  733. * and record the dma-offset in the struct device.
  734. *
  735. * dev: the pci device we are checking
  736. * pdn: the parent pe node with the ibm,dma_window property
  737. * Future: also check if we can remap the base window for our base page size
  738. *
  739. * returns the dma offset for use by dma_set_mask
  740. */
  741. static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
  742. {
  743. int len, ret;
  744. struct ddw_query_response query;
  745. struct ddw_create_response create;
  746. int page_shift;
  747. u64 dma_addr, max_addr;
  748. struct device_node *dn;
  749. const u32 *uninitialized_var(ddw_avail);
  750. struct direct_window *window;
  751. struct property *win64;
  752. struct dynamic_dma_window_prop *ddwprop;
  753. struct failed_ddw_pdn *fpdn;
  754. mutex_lock(&direct_window_init_mutex);
  755. dma_addr = find_existing_ddw(pdn);
  756. if (dma_addr != 0)
  757. goto out_unlock;
  758. /*
  759. * If we already went through this for a previous function of
  760. * the same device and failed, we don't want to muck with the
  761. * DMA window again, as it will race with in-flight operations
  762. * and can lead to EEHs. The above mutex protects access to the
  763. * list.
  764. */
  765. list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) {
  766. if (!strcmp(fpdn->pdn->full_name, pdn->full_name))
  767. goto out_unlock;
  768. }
  769. /*
  770. * the ibm,ddw-applicable property holds the tokens for:
  771. * ibm,query-pe-dma-window
  772. * ibm,create-pe-dma-window
  773. * ibm,remove-pe-dma-window
  774. * for the given node in that order.
  775. * the property is actually in the parent, not the PE
  776. */
  777. ddw_avail = of_get_property(pdn, "ibm,ddw-applicable", &len);
  778. if (!ddw_avail || len < 3 * sizeof(u32))
  779. goto out_failed;
  780. /*
  781. * Query if there is a second window of size to map the
  782. * whole partition. Query returns number of windows, largest
  783. * block assigned to PE (partition endpoint), and two bitmasks
  784. * of page sizes: supported and supported for migrate-dma.
  785. */
  786. dn = pci_device_to_OF_node(dev);
  787. ret = query_ddw(dev, ddw_avail, &query);
  788. if (ret != 0)
  789. goto out_failed;
  790. if (query.windows_available == 0) {
  791. /*
  792. * no additional windows are available for this device.
  793. * We might be able to reallocate the existing window,
  794. * trading in for a larger page size.
  795. */
  796. dev_dbg(&dev->dev, "no free dynamic windows");
  797. goto out_failed;
  798. }
  799. if (be32_to_cpu(query.page_size) & 4) {
  800. page_shift = 24; /* 16MB */
  801. } else if (be32_to_cpu(query.page_size) & 2) {
  802. page_shift = 16; /* 64kB */
  803. } else if (be32_to_cpu(query.page_size) & 1) {
  804. page_shift = 12; /* 4kB */
  805. } else {
  806. dev_dbg(&dev->dev, "no supported direct page size in mask %x",
  807. query.page_size);
  808. goto out_failed;
  809. }
  810. /* verify the window * number of ptes will map the partition */
  811. /* check largest block * page size > max memory hotplug addr */
  812. max_addr = memory_hotplug_max();
  813. if (be32_to_cpu(query.largest_available_block) < (max_addr >> page_shift)) {
  814. dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
  815. "%llu-sized pages\n", max_addr, query.largest_available_block,
  816. 1ULL << page_shift);
  817. goto out_failed;
  818. }
  819. len = order_base_2(max_addr);
  820. win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
  821. if (!win64) {
  822. dev_info(&dev->dev,
  823. "couldn't allocate property for 64bit dma window\n");
  824. goto out_failed;
  825. }
  826. win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
  827. win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
  828. win64->length = sizeof(*ddwprop);
  829. if (!win64->name || !win64->value) {
  830. dev_info(&dev->dev,
  831. "couldn't allocate property name and value\n");
  832. goto out_free_prop;
  833. }
  834. ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
  835. if (ret != 0)
  836. goto out_free_prop;
  837. ddwprop->liobn = create.liobn;
  838. ddwprop->dma_base = cpu_to_be64(of_read_number(&create.addr_hi, 2));
  839. ddwprop->tce_shift = cpu_to_be32(page_shift);
  840. ddwprop->window_shift = cpu_to_be32(len);
  841. dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
  842. create.liobn, dn->full_name);
  843. window = kzalloc(sizeof(*window), GFP_KERNEL);
  844. if (!window)
  845. goto out_clear_window;
  846. ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
  847. win64->value, tce_setrange_multi_pSeriesLP_walk);
  848. if (ret) {
  849. dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
  850. dn->full_name, ret);
  851. goto out_free_window;
  852. }
  853. ret = of_add_property(pdn, win64);
  854. if (ret) {
  855. dev_err(&dev->dev, "unable to add dma window property for %s: %d",
  856. pdn->full_name, ret);
  857. goto out_free_window;
  858. }
  859. window->device = pdn;
  860. window->prop = ddwprop;
  861. spin_lock(&direct_window_list_lock);
  862. list_add(&window->list, &direct_window_list);
  863. spin_unlock(&direct_window_list_lock);
  864. dma_addr = of_read_number(&create.addr_hi, 2);
  865. goto out_unlock;
  866. out_free_window:
  867. kfree(window);
  868. out_clear_window:
  869. remove_ddw(pdn);
  870. out_free_prop:
  871. kfree(win64->name);
  872. kfree(win64->value);
  873. kfree(win64);
  874. out_failed:
  875. fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
  876. if (!fpdn)
  877. goto out_unlock;
  878. fpdn->pdn = pdn;
  879. list_add(&fpdn->list, &failed_ddw_pdn_list);
  880. out_unlock:
  881. mutex_unlock(&direct_window_init_mutex);
  882. return dma_addr;
  883. }
  884. static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
  885. {
  886. struct device_node *pdn, *dn;
  887. struct iommu_table *tbl;
  888. const __be32 *dma_window = NULL;
  889. struct pci_dn *pci;
  890. pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
  891. /* dev setup for LPAR is a little tricky, since the device tree might
  892. * contain the dma-window properties per-device and not necessarily
  893. * for the bus. So we need to search upwards in the tree until we
  894. * either hit a dma-window property, OR find a parent with a table
  895. * already allocated.
  896. */
  897. dn = pci_device_to_OF_node(dev);
  898. pr_debug(" node is %s\n", dn->full_name);
  899. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  900. pdn = pdn->parent) {
  901. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  902. if (dma_window)
  903. break;
  904. }
  905. if (!pdn || !PCI_DN(pdn)) {
  906. printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
  907. "no DMA window found for pci dev=%s dn=%s\n",
  908. pci_name(dev), of_node_full_name(dn));
  909. return;
  910. }
  911. pr_debug(" parent is %s\n", pdn->full_name);
  912. pci = PCI_DN(pdn);
  913. if (!pci->iommu_table) {
  914. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  915. pci->phb->node);
  916. iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
  917. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  918. iommu_register_group(tbl, pci_domain_nr(pci->phb->bus), 0);
  919. pr_debug(" created table: %p\n", pci->iommu_table);
  920. } else {
  921. pr_debug(" found DMA window, table: %p\n", pci->iommu_table);
  922. }
  923. set_iommu_table_base_and_group(&dev->dev, pci->iommu_table);
  924. }
  925. static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
  926. {
  927. bool ddw_enabled = false;
  928. struct device_node *pdn, *dn;
  929. struct pci_dev *pdev;
  930. const __be32 *dma_window = NULL;
  931. u64 dma_offset;
  932. if (!dev->dma_mask)
  933. return -EIO;
  934. if (!dev_is_pci(dev))
  935. goto check_mask;
  936. pdev = to_pci_dev(dev);
  937. /* only attempt to use a new window if 64-bit DMA is requested */
  938. if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
  939. dn = pci_device_to_OF_node(pdev);
  940. dev_dbg(dev, "node is %s\n", dn->full_name);
  941. /*
  942. * the device tree might contain the dma-window properties
  943. * per-device and not necessarily for the bus. So we need to
  944. * search upwards in the tree until we either hit a dma-window
  945. * property, OR find a parent with a table already allocated.
  946. */
  947. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  948. pdn = pdn->parent) {
  949. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  950. if (dma_window)
  951. break;
  952. }
  953. if (pdn && PCI_DN(pdn)) {
  954. dma_offset = enable_ddw(pdev, pdn);
  955. if (dma_offset != 0) {
  956. dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
  957. set_dma_offset(dev, dma_offset);
  958. set_dma_ops(dev, &dma_direct_ops);
  959. ddw_enabled = true;
  960. }
  961. }
  962. }
  963. /* fall back on iommu ops, restore table pointer with ops */
  964. if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
  965. dev_info(dev, "Restoring 32-bit DMA via iommu\n");
  966. set_dma_ops(dev, &dma_iommu_ops);
  967. pci_dma_dev_setup_pSeriesLP(pdev);
  968. }
  969. check_mask:
  970. if (!dma_supported(dev, dma_mask))
  971. return -EIO;
  972. *dev->dma_mask = dma_mask;
  973. return 0;
  974. }
  975. static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
  976. {
  977. if (!dev->dma_mask)
  978. return 0;
  979. if (!disable_ddw && dev_is_pci(dev)) {
  980. struct pci_dev *pdev = to_pci_dev(dev);
  981. struct device_node *dn;
  982. dn = pci_device_to_OF_node(pdev);
  983. /* search upwards for ibm,dma-window */
  984. for (; dn && PCI_DN(dn) && !PCI_DN(dn)->iommu_table;
  985. dn = dn->parent)
  986. if (of_get_property(dn, "ibm,dma-window", NULL))
  987. break;
  988. /* if there is a ibm,ddw-applicable property require 64 bits */
  989. if (dn && PCI_DN(dn) &&
  990. of_get_property(dn, "ibm,ddw-applicable", NULL))
  991. return DMA_BIT_MASK(64);
  992. }
  993. return dma_iommu_ops.get_required_mask(dev);
  994. }
  995. #else /* CONFIG_PCI */
  996. #define pci_dma_bus_setup_pSeries NULL
  997. #define pci_dma_dev_setup_pSeries NULL
  998. #define pci_dma_bus_setup_pSeriesLP NULL
  999. #define pci_dma_dev_setup_pSeriesLP NULL
  1000. #define dma_set_mask_pSeriesLP NULL
  1001. #define dma_get_required_mask_pSeriesLP NULL
  1002. #endif /* !CONFIG_PCI */
  1003. static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
  1004. void *data)
  1005. {
  1006. struct direct_window *window;
  1007. struct memory_notify *arg = data;
  1008. int ret = 0;
  1009. switch (action) {
  1010. case MEM_GOING_ONLINE:
  1011. spin_lock(&direct_window_list_lock);
  1012. list_for_each_entry(window, &direct_window_list, list) {
  1013. ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
  1014. arg->nr_pages, window->prop);
  1015. /* XXX log error */
  1016. }
  1017. spin_unlock(&direct_window_list_lock);
  1018. break;
  1019. case MEM_CANCEL_ONLINE:
  1020. case MEM_OFFLINE:
  1021. spin_lock(&direct_window_list_lock);
  1022. list_for_each_entry(window, &direct_window_list, list) {
  1023. ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
  1024. arg->nr_pages, window->prop);
  1025. /* XXX log error */
  1026. }
  1027. spin_unlock(&direct_window_list_lock);
  1028. break;
  1029. default:
  1030. break;
  1031. }
  1032. if (ret && action != MEM_CANCEL_ONLINE)
  1033. return NOTIFY_BAD;
  1034. return NOTIFY_OK;
  1035. }
  1036. static struct notifier_block iommu_mem_nb = {
  1037. .notifier_call = iommu_mem_notifier,
  1038. };
  1039. static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  1040. {
  1041. int err = NOTIFY_OK;
  1042. struct device_node *np = node;
  1043. struct pci_dn *pci = PCI_DN(np);
  1044. struct direct_window *window;
  1045. switch (action) {
  1046. case OF_RECONFIG_DETACH_NODE:
  1047. remove_ddw(np);
  1048. if (pci && pci->iommu_table)
  1049. iommu_free_table(pci->iommu_table, np->full_name);
  1050. spin_lock(&direct_window_list_lock);
  1051. list_for_each_entry(window, &direct_window_list, list) {
  1052. if (window->device == np) {
  1053. list_del(&window->list);
  1054. kfree(window);
  1055. break;
  1056. }
  1057. }
  1058. spin_unlock(&direct_window_list_lock);
  1059. break;
  1060. default:
  1061. err = NOTIFY_DONE;
  1062. break;
  1063. }
  1064. return err;
  1065. }
  1066. static struct notifier_block iommu_reconfig_nb = {
  1067. .notifier_call = iommu_reconfig_notifier,
  1068. };
  1069. /* These are called very early. */
  1070. void iommu_init_early_pSeries(void)
  1071. {
  1072. if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
  1073. return;
  1074. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  1075. if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
  1076. ppc_md.tce_build = tce_buildmulti_pSeriesLP;
  1077. ppc_md.tce_free = tce_freemulti_pSeriesLP;
  1078. } else {
  1079. ppc_md.tce_build = tce_build_pSeriesLP;
  1080. ppc_md.tce_free = tce_free_pSeriesLP;
  1081. }
  1082. ppc_md.tce_get = tce_get_pSeriesLP;
  1083. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
  1084. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
  1085. ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
  1086. ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
  1087. } else {
  1088. ppc_md.tce_build = tce_build_pSeries;
  1089. ppc_md.tce_free = tce_free_pSeries;
  1090. ppc_md.tce_get = tce_get_pseries;
  1091. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeries;
  1092. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeries;
  1093. }
  1094. of_reconfig_notifier_register(&iommu_reconfig_nb);
  1095. register_memory_notifier(&iommu_mem_nb);
  1096. set_pci_dma_ops(&dma_iommu_ops);
  1097. }
  1098. static int __init disable_multitce(char *str)
  1099. {
  1100. if (strcmp(str, "off") == 0 &&
  1101. firmware_has_feature(FW_FEATURE_LPAR) &&
  1102. firmware_has_feature(FW_FEATURE_MULTITCE)) {
  1103. printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
  1104. ppc_md.tce_build = tce_build_pSeriesLP;
  1105. ppc_md.tce_free = tce_free_pSeriesLP;
  1106. powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
  1107. }
  1108. return 1;
  1109. }
  1110. __setup("multitce=", disable_multitce);