iommu.c 38 KB

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