iommu.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. if (len < sizeof(struct dynamic_dma_window_prop)) {
  595. remove_ddw(pdn);
  596. } else {
  597. window = kzalloc(sizeof(*window), GFP_KERNEL);
  598. if (!window) {
  599. remove_ddw(pdn);
  600. } else {
  601. window->device = pdn;
  602. window->prop = direct64;
  603. spin_lock(&direct_window_list_lock);
  604. list_add(&window->list, &direct_window_list);
  605. spin_unlock(&direct_window_list_lock);
  606. dma_addr = direct64->dma_base;
  607. }
  608. }
  609. }
  610. return dma_addr;
  611. }
  612. static int query_ddw(struct pci_dev *dev, const u32 *ddr_avail,
  613. struct ddw_query_response *query)
  614. {
  615. struct device_node *dn;
  616. struct pci_dn *pcidn;
  617. u32 cfg_addr;
  618. u64 buid;
  619. int ret;
  620. /*
  621. * Get the config address and phb buid of the PE window.
  622. * Rely on eeh to retrieve this for us.
  623. * Retrieve them from the pci device, not the node with the
  624. * dma-window property
  625. */
  626. dn = pci_device_to_OF_node(dev);
  627. pcidn = PCI_DN(dn);
  628. cfg_addr = pcidn->eeh_config_addr;
  629. if (pcidn->eeh_pe_config_addr)
  630. cfg_addr = pcidn->eeh_pe_config_addr;
  631. buid = pcidn->phb->buid;
  632. ret = rtas_call(ddr_avail[0], 3, 5, (u32 *)query,
  633. cfg_addr, BUID_HI(buid), BUID_LO(buid));
  634. dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
  635. " returned %d\n", ddr_avail[0], cfg_addr, BUID_HI(buid),
  636. BUID_LO(buid), ret);
  637. return ret;
  638. }
  639. static int create_ddw(struct pci_dev *dev, const u32 *ddr_avail,
  640. struct ddw_create_response *create, int page_shift,
  641. int window_shift)
  642. {
  643. struct device_node *dn;
  644. struct pci_dn *pcidn;
  645. u32 cfg_addr;
  646. u64 buid;
  647. int ret;
  648. /*
  649. * Get the config address and phb buid of the PE window.
  650. * Rely on eeh to retrieve this for us.
  651. * Retrieve them from the pci device, not the node with the
  652. * dma-window property
  653. */
  654. dn = pci_device_to_OF_node(dev);
  655. pcidn = PCI_DN(dn);
  656. cfg_addr = pcidn->eeh_config_addr;
  657. if (pcidn->eeh_pe_config_addr)
  658. cfg_addr = pcidn->eeh_pe_config_addr;
  659. buid = pcidn->phb->buid;
  660. do {
  661. /* extra outputs are LIOBN and dma-addr (hi, lo) */
  662. ret = rtas_call(ddr_avail[1], 5, 4, (u32 *)create, cfg_addr,
  663. BUID_HI(buid), BUID_LO(buid), page_shift, window_shift);
  664. } while (rtas_busy_delay(ret));
  665. dev_info(&dev->dev,
  666. "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
  667. "(liobn = 0x%x starting addr = %x %x)\n", ddr_avail[1],
  668. cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
  669. window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
  670. return ret;
  671. }
  672. /*
  673. * If the PE supports dynamic dma windows, and there is space for a table
  674. * that can map all pages in a linear offset, then setup such a table,
  675. * and record the dma-offset in the struct device.
  676. *
  677. * dev: the pci device we are checking
  678. * pdn: the parent pe node with the ibm,dma_window property
  679. * Future: also check if we can remap the base window for our base page size
  680. *
  681. * returns the dma offset for use by dma_set_mask
  682. */
  683. static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
  684. {
  685. int len, ret;
  686. struct ddw_query_response query;
  687. struct ddw_create_response create;
  688. int page_shift;
  689. u64 dma_addr, max_addr;
  690. struct device_node *dn;
  691. const u32 *uninitialized_var(ddr_avail);
  692. struct direct_window *window;
  693. struct property *win64;
  694. struct dynamic_dma_window_prop *ddwprop;
  695. mutex_lock(&direct_window_init_mutex);
  696. dma_addr = dupe_ddw_if_already_created(dev, pdn);
  697. if (dma_addr != 0)
  698. goto out_unlock;
  699. dma_addr = dupe_ddw_if_kexec(dev, pdn);
  700. if (dma_addr != 0)
  701. goto out_unlock;
  702. /*
  703. * the ibm,ddw-applicable property holds the tokens for:
  704. * ibm,query-pe-dma-window
  705. * ibm,create-pe-dma-window
  706. * ibm,remove-pe-dma-window
  707. * for the given node in that order.
  708. * the property is actually in the parent, not the PE
  709. */
  710. ddr_avail = of_get_property(pdn, "ibm,ddw-applicable", &len);
  711. if (!ddr_avail || len < 3 * sizeof(u32))
  712. goto out_unlock;
  713. /*
  714. * Query if there is a second window of size to map the
  715. * whole partition. Query returns number of windows, largest
  716. * block assigned to PE (partition endpoint), and two bitmasks
  717. * of page sizes: supported and supported for migrate-dma.
  718. */
  719. dn = pci_device_to_OF_node(dev);
  720. ret = query_ddw(dev, ddr_avail, &query);
  721. if (ret != 0)
  722. goto out_unlock;
  723. if (query.windows_available == 0) {
  724. /*
  725. * no additional windows are available for this device.
  726. * We might be able to reallocate the existing window,
  727. * trading in for a larger page size.
  728. */
  729. dev_dbg(&dev->dev, "no free dynamic windows");
  730. goto out_unlock;
  731. }
  732. if (query.page_size & 4) {
  733. page_shift = 24; /* 16MB */
  734. } else if (query.page_size & 2) {
  735. page_shift = 16; /* 64kB */
  736. } else if (query.page_size & 1) {
  737. page_shift = 12; /* 4kB */
  738. } else {
  739. dev_dbg(&dev->dev, "no supported direct page size in mask %x",
  740. query.page_size);
  741. goto out_unlock;
  742. }
  743. /* verify the window * number of ptes will map the partition */
  744. /* check largest block * page size > max memory hotplug addr */
  745. max_addr = memory_hotplug_max();
  746. if (query.largest_available_block < (max_addr >> page_shift)) {
  747. dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
  748. "%llu-sized pages\n", max_addr, query.largest_available_block,
  749. 1ULL << page_shift);
  750. goto out_unlock;
  751. }
  752. len = order_base_2(max_addr);
  753. win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
  754. if (!win64) {
  755. dev_info(&dev->dev,
  756. "couldn't allocate property for 64bit dma window\n");
  757. goto out_unlock;
  758. }
  759. win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
  760. win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
  761. win64->length = sizeof(*ddwprop);
  762. if (!win64->name || !win64->value) {
  763. dev_info(&dev->dev,
  764. "couldn't allocate property name and value\n");
  765. goto out_free_prop;
  766. }
  767. ret = create_ddw(dev, ddr_avail, &create, page_shift, len);
  768. if (ret != 0)
  769. goto out_free_prop;
  770. ddwprop->liobn = cpu_to_be32(create.liobn);
  771. ddwprop->dma_base = cpu_to_be64(of_read_number(&create.addr_hi, 2));
  772. ddwprop->tce_shift = cpu_to_be32(page_shift);
  773. ddwprop->window_shift = cpu_to_be32(len);
  774. dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
  775. create.liobn, dn->full_name);
  776. window = kzalloc(sizeof(*window), GFP_KERNEL);
  777. if (!window)
  778. goto out_clear_window;
  779. ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
  780. win64->value, tce_setrange_multi_pSeriesLP_walk);
  781. if (ret) {
  782. dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
  783. dn->full_name, ret);
  784. goto out_clear_window;
  785. }
  786. ret = prom_add_property(pdn, win64);
  787. if (ret) {
  788. dev_err(&dev->dev, "unable to add dma window property for %s: %d",
  789. pdn->full_name, ret);
  790. goto out_clear_window;
  791. }
  792. window->device = pdn;
  793. window->prop = ddwprop;
  794. spin_lock(&direct_window_list_lock);
  795. list_add(&window->list, &direct_window_list);
  796. spin_unlock(&direct_window_list_lock);
  797. dma_addr = of_read_number(&create.addr_hi, 2);
  798. goto out_unlock;
  799. out_clear_window:
  800. remove_ddw(pdn);
  801. out_free_prop:
  802. kfree(win64->name);
  803. kfree(win64->value);
  804. kfree(win64);
  805. out_unlock:
  806. mutex_unlock(&direct_window_init_mutex);
  807. return dma_addr;
  808. }
  809. static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
  810. {
  811. struct device_node *pdn, *dn;
  812. struct iommu_table *tbl;
  813. const void *dma_window = NULL;
  814. struct pci_dn *pci;
  815. pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
  816. /* dev setup for LPAR is a little tricky, since the device tree might
  817. * contain the dma-window properties per-device and not necessarily
  818. * for the bus. So we need to search upwards in the tree until we
  819. * either hit a dma-window property, OR find a parent with a table
  820. * already allocated.
  821. */
  822. dn = pci_device_to_OF_node(dev);
  823. pr_debug(" node is %s\n", dn->full_name);
  824. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  825. pdn = pdn->parent) {
  826. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  827. if (dma_window)
  828. break;
  829. }
  830. if (!pdn || !PCI_DN(pdn)) {
  831. printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
  832. "no DMA window found for pci dev=%s dn=%s\n",
  833. pci_name(dev), dn? dn->full_name : "<null>");
  834. return;
  835. }
  836. pr_debug(" parent is %s\n", pdn->full_name);
  837. pci = PCI_DN(pdn);
  838. if (!pci->iommu_table) {
  839. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  840. pci->phb->node);
  841. iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
  842. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  843. pr_debug(" created table: %p\n", pci->iommu_table);
  844. } else {
  845. pr_debug(" found DMA window, table: %p\n", pci->iommu_table);
  846. }
  847. set_iommu_table_base(&dev->dev, pci->iommu_table);
  848. }
  849. static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
  850. {
  851. bool ddw_enabled = false;
  852. struct device_node *pdn, *dn;
  853. struct pci_dev *pdev;
  854. const void *dma_window = NULL;
  855. u64 dma_offset;
  856. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  857. return -EIO;
  858. /* only attempt to use a new window if 64-bit DMA is requested */
  859. if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
  860. pdev = to_pci_dev(dev);
  861. dn = pci_device_to_OF_node(pdev);
  862. dev_dbg(dev, "node is %s\n", dn->full_name);
  863. /*
  864. * the device tree might contain the dma-window properties
  865. * per-device and not necessarily for the bus. So we need to
  866. * search upwards in the tree until we either hit a dma-window
  867. * property, OR find a parent with a table already allocated.
  868. */
  869. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  870. pdn = pdn->parent) {
  871. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  872. if (dma_window)
  873. break;
  874. }
  875. if (pdn && PCI_DN(pdn)) {
  876. dma_offset = enable_ddw(pdev, pdn);
  877. if (dma_offset != 0) {
  878. dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
  879. set_dma_offset(dev, dma_offset);
  880. set_dma_ops(dev, &dma_direct_ops);
  881. ddw_enabled = true;
  882. }
  883. }
  884. }
  885. /* fall-through to iommu ops */
  886. if (!ddw_enabled) {
  887. dev_info(dev, "Using 32-bit DMA via iommu\n");
  888. set_dma_ops(dev, &dma_iommu_ops);
  889. }
  890. *dev->dma_mask = dma_mask;
  891. return 0;
  892. }
  893. #else /* CONFIG_PCI */
  894. #define pci_dma_bus_setup_pSeries NULL
  895. #define pci_dma_dev_setup_pSeries NULL
  896. #define pci_dma_bus_setup_pSeriesLP NULL
  897. #define pci_dma_dev_setup_pSeriesLP NULL
  898. #define dma_set_mask_pSeriesLP NULL
  899. #endif /* !CONFIG_PCI */
  900. static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
  901. void *data)
  902. {
  903. struct direct_window *window;
  904. struct memory_notify *arg = data;
  905. int ret = 0;
  906. switch (action) {
  907. case MEM_GOING_ONLINE:
  908. spin_lock(&direct_window_list_lock);
  909. list_for_each_entry(window, &direct_window_list, list) {
  910. ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
  911. arg->nr_pages, window->prop);
  912. /* XXX log error */
  913. }
  914. spin_unlock(&direct_window_list_lock);
  915. break;
  916. case MEM_CANCEL_ONLINE:
  917. case MEM_OFFLINE:
  918. spin_lock(&direct_window_list_lock);
  919. list_for_each_entry(window, &direct_window_list, list) {
  920. ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
  921. arg->nr_pages, window->prop);
  922. /* XXX log error */
  923. }
  924. spin_unlock(&direct_window_list_lock);
  925. break;
  926. default:
  927. break;
  928. }
  929. if (ret && action != MEM_CANCEL_ONLINE)
  930. return NOTIFY_BAD;
  931. return NOTIFY_OK;
  932. }
  933. static struct notifier_block iommu_mem_nb = {
  934. .notifier_call = iommu_mem_notifier,
  935. };
  936. static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  937. {
  938. int err = NOTIFY_OK;
  939. struct device_node *np = node;
  940. struct pci_dn *pci = PCI_DN(np);
  941. struct direct_window *window;
  942. switch (action) {
  943. case PSERIES_RECONFIG_REMOVE:
  944. if (pci && pci->iommu_table)
  945. iommu_free_table(pci->iommu_table, np->full_name);
  946. spin_lock(&direct_window_list_lock);
  947. list_for_each_entry(window, &direct_window_list, list) {
  948. if (window->device == np) {
  949. list_del(&window->list);
  950. kfree(window);
  951. break;
  952. }
  953. }
  954. spin_unlock(&direct_window_list_lock);
  955. /*
  956. * Because the notifier runs after isolation of the
  957. * slot, we are guaranteed any DMA window has already
  958. * been revoked and the TCEs have been marked invalid,
  959. * so we don't need a call to remove_ddw(np). However,
  960. * if an additional notifier action is added before the
  961. * isolate call, we should update this code for
  962. * completeness with such a call.
  963. */
  964. break;
  965. default:
  966. err = NOTIFY_DONE;
  967. break;
  968. }
  969. return err;
  970. }
  971. static struct notifier_block iommu_reconfig_nb = {
  972. .notifier_call = iommu_reconfig_notifier,
  973. };
  974. /* These are called very early. */
  975. void iommu_init_early_pSeries(void)
  976. {
  977. if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
  978. return;
  979. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  980. if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
  981. ppc_md.tce_build = tce_buildmulti_pSeriesLP;
  982. ppc_md.tce_free = tce_freemulti_pSeriesLP;
  983. } else {
  984. ppc_md.tce_build = tce_build_pSeriesLP;
  985. ppc_md.tce_free = tce_free_pSeriesLP;
  986. }
  987. ppc_md.tce_get = tce_get_pSeriesLP;
  988. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
  989. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
  990. ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
  991. } else {
  992. ppc_md.tce_build = tce_build_pSeries;
  993. ppc_md.tce_free = tce_free_pSeries;
  994. ppc_md.tce_get = tce_get_pseries;
  995. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeries;
  996. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeries;
  997. }
  998. pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
  999. register_memory_notifier(&iommu_mem_nb);
  1000. set_pci_dma_ops(&dma_iommu_ops);
  1001. }
  1002. static int __init disable_multitce(char *str)
  1003. {
  1004. if (strcmp(str, "off") == 0 &&
  1005. firmware_has_feature(FW_FEATURE_LPAR) &&
  1006. firmware_has_feature(FW_FEATURE_MULTITCE)) {
  1007. printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
  1008. ppc_md.tce_build = tce_build_pSeriesLP;
  1009. ppc_md.tce_free = tce_free_pSeriesLP;
  1010. powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
  1011. }
  1012. return 1;
  1013. }
  1014. __setup("multitce=", disable_multitce);