iommu.c 36 KB

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