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