iommu.c 32 KB

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