iommu.c 37 KB

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