iommu.c 37 KB

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