iommu.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. /* Divide the rest (1.75GB) among the children */
  504. pci->phb->dma_window_size = 0x80000000ul;
  505. while (pci->phb->dma_window_size * children > 0x70000000ul)
  506. pci->phb->dma_window_size >>= 1;
  507. pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
  508. }
  509. static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
  510. {
  511. struct iommu_table *tbl;
  512. struct device_node *dn, *pdn;
  513. struct pci_dn *ppci;
  514. const void *dma_window = NULL;
  515. dn = pci_bus_to_OF_node(bus);
  516. pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
  517. dn->full_name);
  518. /* Find nearest ibm,dma-window, walking up the device tree */
  519. for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
  520. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  521. if (dma_window != NULL)
  522. break;
  523. }
  524. if (dma_window == NULL) {
  525. pr_debug(" no ibm,dma-window property !\n");
  526. return;
  527. }
  528. ppci = PCI_DN(pdn);
  529. pr_debug(" parent is %s, iommu_table: 0x%p\n",
  530. pdn->full_name, ppci->iommu_table);
  531. if (!ppci->iommu_table) {
  532. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  533. ppci->phb->node);
  534. iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
  535. ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
  536. pr_debug(" created table: %p\n", ppci->iommu_table);
  537. }
  538. }
  539. static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
  540. {
  541. struct device_node *dn;
  542. struct iommu_table *tbl;
  543. pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
  544. dn = dev->dev.of_node;
  545. /* If we're the direct child of a root bus, then we need to allocate
  546. * an iommu table ourselves. The bus setup code should have setup
  547. * the window sizes already.
  548. */
  549. if (!dev->bus->self) {
  550. struct pci_controller *phb = PCI_DN(dn)->phb;
  551. pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
  552. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  553. phb->node);
  554. iommu_table_setparms(phb, dn, tbl);
  555. PCI_DN(dn)->iommu_table = iommu_init_table(tbl, phb->node);
  556. set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
  557. return;
  558. }
  559. /* If this device is further down the bus tree, search upwards until
  560. * an already allocated iommu table is found and use that.
  561. */
  562. while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
  563. dn = dn->parent;
  564. if (dn && PCI_DN(dn))
  565. set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
  566. else
  567. printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
  568. pci_name(dev));
  569. }
  570. static int __read_mostly disable_ddw;
  571. static int __init disable_ddw_setup(char *str)
  572. {
  573. disable_ddw = 1;
  574. printk(KERN_INFO "ppc iommu: disabling ddw.\n");
  575. return 0;
  576. }
  577. early_param("disable_ddw", disable_ddw_setup);
  578. static inline void __remove_ddw(struct device_node *np, const u32 *ddw_avail, u64 liobn)
  579. {
  580. int ret;
  581. ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
  582. if (ret)
  583. pr_warning("%s: failed to remove DMA window: rtas returned "
  584. "%d to ibm,remove-pe-dma-window(%x) %llx\n",
  585. np->full_name, ret, ddw_avail[2], liobn);
  586. else
  587. pr_debug("%s: successfully removed 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. }
  591. static void remove_ddw(struct device_node *np)
  592. {
  593. struct dynamic_dma_window_prop *dwp;
  594. struct property *win64;
  595. const u32 *ddw_avail;
  596. u64 liobn;
  597. int len, ret;
  598. ddw_avail = of_get_property(np, "ibm,ddw-applicable", &len);
  599. win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
  600. if (!win64)
  601. return;
  602. if (!ddw_avail || len < 3 * sizeof(u32) || win64->length < sizeof(*dwp))
  603. goto delprop;
  604. dwp = win64->value;
  605. liobn = (u64)be32_to_cpu(dwp->liobn);
  606. /* clear the whole window, note the arg is in kernel pages */
  607. ret = tce_clearrange_multi_pSeriesLP(0,
  608. 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
  609. if (ret)
  610. pr_warning("%s failed to clear tces in window.\n",
  611. np->full_name);
  612. else
  613. pr_debug("%s successfully cleared tces in window.\n",
  614. np->full_name);
  615. __remove_ddw(np, ddw_avail, liobn);
  616. delprop:
  617. ret = of_remove_property(np, win64);
  618. if (ret)
  619. pr_warning("%s: failed to remove direct window property: %d\n",
  620. np->full_name, ret);
  621. }
  622. static u64 find_existing_ddw(struct device_node *pdn)
  623. {
  624. struct direct_window *window;
  625. const struct dynamic_dma_window_prop *direct64;
  626. u64 dma_addr = 0;
  627. spin_lock(&direct_window_list_lock);
  628. /* check if we already created a window and dupe that config if so */
  629. list_for_each_entry(window, &direct_window_list, list) {
  630. if (window->device == pdn) {
  631. direct64 = window->prop;
  632. dma_addr = direct64->dma_base;
  633. break;
  634. }
  635. }
  636. spin_unlock(&direct_window_list_lock);
  637. return dma_addr;
  638. }
  639. static void __restore_default_window(struct eeh_dev *edev,
  640. u32 ddw_restore_token)
  641. {
  642. u32 cfg_addr;
  643. u64 buid;
  644. int ret;
  645. /*
  646. * Get the config address and phb buid of the PE window.
  647. * Rely on eeh to retrieve this for us.
  648. * Retrieve them from the pci device, not the node with the
  649. * dma-window property
  650. */
  651. cfg_addr = edev->config_addr;
  652. if (edev->pe_config_addr)
  653. cfg_addr = edev->pe_config_addr;
  654. buid = edev->phb->buid;
  655. do {
  656. ret = rtas_call(ddw_restore_token, 3, 1, NULL, cfg_addr,
  657. BUID_HI(buid), BUID_LO(buid));
  658. } while (rtas_busy_delay(ret));
  659. pr_info("ibm,reset-pe-dma-windows(%x) %x %x %x returned %d\n",
  660. ddw_restore_token, cfg_addr, BUID_HI(buid), BUID_LO(buid), ret);
  661. }
  662. static int find_existing_ddw_windows(void)
  663. {
  664. struct device_node *pdn;
  665. const struct dynamic_dma_window_prop *direct64;
  666. const u32 *ddw_extensions;
  667. if (!firmware_has_feature(FW_FEATURE_LPAR))
  668. return 0;
  669. for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
  670. direct64 = of_get_property(pdn, DIRECT64_PROPNAME, NULL);
  671. if (!direct64)
  672. continue;
  673. /*
  674. * We need to ensure the IOMMU table is active when we
  675. * return from the IOMMU setup so that the common code
  676. * can clear the table or find the holes. To that end,
  677. * first, remove any existing DDW configuration.
  678. */
  679. remove_ddw(pdn);
  680. /*
  681. * Second, if we are running on a new enough level of
  682. * firmware where the restore API is present, use it to
  683. * restore the 32-bit window, which was removed in
  684. * create_ddw.
  685. * If the API is not present, then create_ddw couldn't
  686. * have removed the 32-bit window in the first place, so
  687. * removing the DDW configuration should be sufficient.
  688. */
  689. ddw_extensions = of_get_property(pdn, "ibm,ddw-extensions",
  690. NULL);
  691. if (ddw_extensions && ddw_extensions[0] > 0)
  692. __restore_default_window(of_node_to_eeh_dev(pdn),
  693. ddw_extensions[1]);
  694. }
  695. return 0;
  696. }
  697. machine_arch_initcall(pseries, find_existing_ddw_windows);
  698. static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
  699. struct ddw_query_response *query)
  700. {
  701. struct eeh_dev *edev;
  702. u32 cfg_addr;
  703. u64 buid;
  704. int ret;
  705. /*
  706. * Get the config address and phb buid of the PE window.
  707. * Rely on eeh to retrieve this for us.
  708. * Retrieve them from the pci device, not the node with the
  709. * dma-window property
  710. */
  711. edev = pci_dev_to_eeh_dev(dev);
  712. cfg_addr = edev->config_addr;
  713. if (edev->pe_config_addr)
  714. cfg_addr = edev->pe_config_addr;
  715. buid = edev->phb->buid;
  716. ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
  717. cfg_addr, BUID_HI(buid), BUID_LO(buid));
  718. dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
  719. " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
  720. BUID_LO(buid), ret);
  721. return ret;
  722. }
  723. static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
  724. struct ddw_create_response *create, int page_shift,
  725. int window_shift)
  726. {
  727. struct eeh_dev *edev;
  728. u32 cfg_addr;
  729. u64 buid;
  730. int ret;
  731. /*
  732. * Get the config address and phb buid of the PE window.
  733. * Rely on eeh to retrieve this for us.
  734. * Retrieve them from the pci device, not the node with the
  735. * dma-window property
  736. */
  737. edev = pci_dev_to_eeh_dev(dev);
  738. cfg_addr = edev->config_addr;
  739. if (edev->pe_config_addr)
  740. cfg_addr = edev->pe_config_addr;
  741. buid = edev->phb->buid;
  742. do {
  743. /* extra outputs are LIOBN and dma-addr (hi, lo) */
  744. ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create, cfg_addr,
  745. BUID_HI(buid), BUID_LO(buid), page_shift, window_shift);
  746. } while (rtas_busy_delay(ret));
  747. dev_info(&dev->dev,
  748. "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
  749. "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
  750. cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
  751. window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
  752. return ret;
  753. }
  754. static void restore_default_window(struct pci_dev *dev,
  755. u32 ddw_restore_token)
  756. {
  757. __restore_default_window(pci_dev_to_eeh_dev(dev), ddw_restore_token);
  758. }
  759. /*
  760. * If the PE supports dynamic dma windows, and there is space for a table
  761. * that can map all pages in a linear offset, then setup such a table,
  762. * and record the dma-offset in the struct device.
  763. *
  764. * dev: the pci device we are checking
  765. * pdn: the parent pe node with the ibm,dma_window property
  766. * Future: also check if we can remap the base window for our base page size
  767. *
  768. * returns the dma offset for use by dma_set_mask
  769. */
  770. static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
  771. {
  772. int len, ret;
  773. struct ddw_query_response query;
  774. struct ddw_create_response create;
  775. int page_shift;
  776. u64 dma_addr, max_addr;
  777. struct device_node *dn;
  778. const u32 *uninitialized_var(ddw_avail);
  779. const u32 *uninitialized_var(ddw_extensions);
  780. u32 ddw_restore_token = 0;
  781. struct direct_window *window;
  782. struct property *win64;
  783. struct dynamic_dma_window_prop *ddwprop;
  784. const void *dma_window = NULL;
  785. unsigned long liobn, offset, size;
  786. mutex_lock(&direct_window_init_mutex);
  787. dma_addr = find_existing_ddw(pdn);
  788. if (dma_addr != 0)
  789. goto out_unlock;
  790. /*
  791. * the ibm,ddw-applicable property holds the tokens for:
  792. * ibm,query-pe-dma-window
  793. * ibm,create-pe-dma-window
  794. * ibm,remove-pe-dma-window
  795. * for the given node in that order.
  796. * the property is actually in the parent, not the PE
  797. */
  798. ddw_avail = of_get_property(pdn, "ibm,ddw-applicable", &len);
  799. if (!ddw_avail || len < 3 * sizeof(u32))
  800. goto out_unlock;
  801. /*
  802. * the extensions property is only required to exist in certain
  803. * levels of firmware and later
  804. * the ibm,ddw-extensions property is a list with the first
  805. * element containing the number of extensions and each
  806. * subsequent entry is a value corresponding to that extension
  807. */
  808. ddw_extensions = of_get_property(pdn, "ibm,ddw-extensions", &len);
  809. if (ddw_extensions) {
  810. /*
  811. * each new defined extension length should be added to
  812. * the top of the switch so the "earlier" entries also
  813. * get picked up
  814. */
  815. switch (ddw_extensions[0]) {
  816. /* ibm,reset-pe-dma-windows */
  817. case 1:
  818. ddw_restore_token = ddw_extensions[1];
  819. break;
  820. }
  821. }
  822. /*
  823. * Only remove the existing DMA window if we can restore back to
  824. * the default state. Removing the existing window maximizes the
  825. * resources available to firmware for dynamic window creation.
  826. */
  827. if (ddw_restore_token) {
  828. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  829. of_parse_dma_window(pdn, dma_window, &liobn, &offset, &size);
  830. __remove_ddw(pdn, ddw_avail, liobn);
  831. }
  832. /*
  833. * Query if there is a second window of size to map the
  834. * whole partition. Query returns number of windows, largest
  835. * block assigned to PE (partition endpoint), and two bitmasks
  836. * of page sizes: supported and supported for migrate-dma.
  837. */
  838. dn = pci_device_to_OF_node(dev);
  839. ret = query_ddw(dev, ddw_avail, &query);
  840. if (ret != 0)
  841. goto out_restore_window;
  842. if (query.windows_available == 0) {
  843. /*
  844. * no additional windows are available for this device.
  845. * We might be able to reallocate the existing window,
  846. * trading in for a larger page size.
  847. */
  848. dev_dbg(&dev->dev, "no free dynamic windows");
  849. goto out_restore_window;
  850. }
  851. if (query.page_size & 4) {
  852. page_shift = 24; /* 16MB */
  853. } else if (query.page_size & 2) {
  854. page_shift = 16; /* 64kB */
  855. } else if (query.page_size & 1) {
  856. page_shift = 12; /* 4kB */
  857. } else {
  858. dev_dbg(&dev->dev, "no supported direct page size in mask %x",
  859. query.page_size);
  860. goto out_restore_window;
  861. }
  862. /* verify the window * number of ptes will map the partition */
  863. /* check largest block * page size > max memory hotplug addr */
  864. max_addr = memory_hotplug_max();
  865. if (query.largest_available_block < (max_addr >> page_shift)) {
  866. dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
  867. "%llu-sized pages\n", max_addr, query.largest_available_block,
  868. 1ULL << page_shift);
  869. goto out_restore_window;
  870. }
  871. len = order_base_2(max_addr);
  872. win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
  873. if (!win64) {
  874. dev_info(&dev->dev,
  875. "couldn't allocate property for 64bit dma window\n");
  876. goto out_restore_window;
  877. }
  878. win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
  879. win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
  880. win64->length = sizeof(*ddwprop);
  881. if (!win64->name || !win64->value) {
  882. dev_info(&dev->dev,
  883. "couldn't allocate property name and value\n");
  884. goto out_free_prop;
  885. }
  886. ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
  887. if (ret != 0)
  888. goto out_free_prop;
  889. ddwprop->liobn = cpu_to_be32(create.liobn);
  890. ddwprop->dma_base = cpu_to_be64(of_read_number(&create.addr_hi, 2));
  891. ddwprop->tce_shift = cpu_to_be32(page_shift);
  892. ddwprop->window_shift = cpu_to_be32(len);
  893. dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
  894. create.liobn, dn->full_name);
  895. window = kzalloc(sizeof(*window), GFP_KERNEL);
  896. if (!window)
  897. goto out_clear_window;
  898. ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
  899. win64->value, tce_setrange_multi_pSeriesLP_walk);
  900. if (ret) {
  901. dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
  902. dn->full_name, ret);
  903. goto out_free_window;
  904. }
  905. ret = of_add_property(pdn, win64);
  906. if (ret) {
  907. dev_err(&dev->dev, "unable to add dma window property for %s: %d",
  908. pdn->full_name, ret);
  909. goto out_free_window;
  910. }
  911. window->device = pdn;
  912. window->prop = ddwprop;
  913. spin_lock(&direct_window_list_lock);
  914. list_add(&window->list, &direct_window_list);
  915. spin_unlock(&direct_window_list_lock);
  916. dma_addr = of_read_number(&create.addr_hi, 2);
  917. goto out_unlock;
  918. out_free_window:
  919. kfree(window);
  920. out_clear_window:
  921. remove_ddw(pdn);
  922. out_free_prop:
  923. kfree(win64->name);
  924. kfree(win64->value);
  925. kfree(win64);
  926. out_restore_window:
  927. if (ddw_restore_token)
  928. restore_default_window(dev, ddw_restore_token);
  929. out_unlock:
  930. mutex_unlock(&direct_window_init_mutex);
  931. return dma_addr;
  932. }
  933. static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
  934. {
  935. struct device_node *pdn, *dn;
  936. struct iommu_table *tbl;
  937. const void *dma_window = NULL;
  938. struct pci_dn *pci;
  939. pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
  940. /* dev setup for LPAR is a little tricky, since the device tree might
  941. * contain the dma-window properties per-device and not necessarily
  942. * for the bus. So we need to search upwards in the tree until we
  943. * either hit a dma-window property, OR find a parent with a table
  944. * already allocated.
  945. */
  946. dn = pci_device_to_OF_node(dev);
  947. pr_debug(" node is %s\n", dn->full_name);
  948. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  949. pdn = pdn->parent) {
  950. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  951. if (dma_window)
  952. break;
  953. }
  954. if (!pdn || !PCI_DN(pdn)) {
  955. printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
  956. "no DMA window found for pci dev=%s dn=%s\n",
  957. pci_name(dev), of_node_full_name(dn));
  958. return;
  959. }
  960. pr_debug(" parent is %s\n", pdn->full_name);
  961. pci = PCI_DN(pdn);
  962. if (!pci->iommu_table) {
  963. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  964. pci->phb->node);
  965. iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
  966. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  967. pr_debug(" created table: %p\n", pci->iommu_table);
  968. } else {
  969. pr_debug(" found DMA window, table: %p\n", pci->iommu_table);
  970. }
  971. set_iommu_table_base(&dev->dev, pci->iommu_table);
  972. }
  973. static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
  974. {
  975. bool ddw_enabled = false;
  976. struct device_node *pdn, *dn;
  977. struct pci_dev *pdev;
  978. const void *dma_window = NULL;
  979. u64 dma_offset;
  980. if (!dev->dma_mask)
  981. return -EIO;
  982. if (!dev_is_pci(dev))
  983. goto check_mask;
  984. pdev = to_pci_dev(dev);
  985. /* only attempt to use a new window if 64-bit DMA is requested */
  986. if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
  987. dn = pci_device_to_OF_node(pdev);
  988. dev_dbg(dev, "node is %s\n", dn->full_name);
  989. /*
  990. * the device tree might contain the dma-window properties
  991. * per-device and not necessarily for the bus. So we need to
  992. * search upwards in the tree until we either hit a dma-window
  993. * property, OR find a parent with a table already allocated.
  994. */
  995. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  996. pdn = pdn->parent) {
  997. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  998. if (dma_window)
  999. break;
  1000. }
  1001. if (pdn && PCI_DN(pdn)) {
  1002. dma_offset = enable_ddw(pdev, pdn);
  1003. if (dma_offset != 0) {
  1004. dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
  1005. set_dma_offset(dev, dma_offset);
  1006. set_dma_ops(dev, &dma_direct_ops);
  1007. ddw_enabled = true;
  1008. }
  1009. }
  1010. }
  1011. /* fall back on iommu ops, restore table pointer with ops */
  1012. if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
  1013. dev_info(dev, "Restoring 32-bit DMA via iommu\n");
  1014. set_dma_ops(dev, &dma_iommu_ops);
  1015. pci_dma_dev_setup_pSeriesLP(pdev);
  1016. }
  1017. check_mask:
  1018. if (!dma_supported(dev, dma_mask))
  1019. return -EIO;
  1020. *dev->dma_mask = dma_mask;
  1021. return 0;
  1022. }
  1023. static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
  1024. {
  1025. if (!dev->dma_mask)
  1026. return 0;
  1027. if (!disable_ddw && dev_is_pci(dev)) {
  1028. struct pci_dev *pdev = to_pci_dev(dev);
  1029. struct device_node *dn;
  1030. dn = pci_device_to_OF_node(pdev);
  1031. /* search upwards for ibm,dma-window */
  1032. for (; dn && PCI_DN(dn) && !PCI_DN(dn)->iommu_table;
  1033. dn = dn->parent)
  1034. if (of_get_property(dn, "ibm,dma-window", NULL))
  1035. break;
  1036. /* if there is a ibm,ddw-applicable property require 64 bits */
  1037. if (dn && PCI_DN(dn) &&
  1038. of_get_property(dn, "ibm,ddw-applicable", NULL))
  1039. return DMA_BIT_MASK(64);
  1040. }
  1041. return dma_iommu_ops.get_required_mask(dev);
  1042. }
  1043. #else /* CONFIG_PCI */
  1044. #define pci_dma_bus_setup_pSeries NULL
  1045. #define pci_dma_dev_setup_pSeries NULL
  1046. #define pci_dma_bus_setup_pSeriesLP NULL
  1047. #define pci_dma_dev_setup_pSeriesLP NULL
  1048. #define dma_set_mask_pSeriesLP NULL
  1049. #define dma_get_required_mask_pSeriesLP NULL
  1050. #endif /* !CONFIG_PCI */
  1051. static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
  1052. void *data)
  1053. {
  1054. struct direct_window *window;
  1055. struct memory_notify *arg = data;
  1056. int ret = 0;
  1057. switch (action) {
  1058. case MEM_GOING_ONLINE:
  1059. spin_lock(&direct_window_list_lock);
  1060. list_for_each_entry(window, &direct_window_list, list) {
  1061. ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
  1062. arg->nr_pages, window->prop);
  1063. /* XXX log error */
  1064. }
  1065. spin_unlock(&direct_window_list_lock);
  1066. break;
  1067. case MEM_CANCEL_ONLINE:
  1068. case MEM_OFFLINE:
  1069. spin_lock(&direct_window_list_lock);
  1070. list_for_each_entry(window, &direct_window_list, list) {
  1071. ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
  1072. arg->nr_pages, window->prop);
  1073. /* XXX log error */
  1074. }
  1075. spin_unlock(&direct_window_list_lock);
  1076. break;
  1077. default:
  1078. break;
  1079. }
  1080. if (ret && action != MEM_CANCEL_ONLINE)
  1081. return NOTIFY_BAD;
  1082. return NOTIFY_OK;
  1083. }
  1084. static struct notifier_block iommu_mem_nb = {
  1085. .notifier_call = iommu_mem_notifier,
  1086. };
  1087. static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  1088. {
  1089. int err = NOTIFY_OK;
  1090. struct device_node *np = node;
  1091. struct pci_dn *pci = PCI_DN(np);
  1092. struct direct_window *window;
  1093. switch (action) {
  1094. case OF_RECONFIG_DETACH_NODE:
  1095. remove_ddw(np);
  1096. if (pci && pci->iommu_table)
  1097. iommu_free_table(pci->iommu_table, np->full_name);
  1098. spin_lock(&direct_window_list_lock);
  1099. list_for_each_entry(window, &direct_window_list, list) {
  1100. if (window->device == np) {
  1101. list_del(&window->list);
  1102. kfree(window);
  1103. break;
  1104. }
  1105. }
  1106. spin_unlock(&direct_window_list_lock);
  1107. break;
  1108. default:
  1109. err = NOTIFY_DONE;
  1110. break;
  1111. }
  1112. return err;
  1113. }
  1114. static struct notifier_block iommu_reconfig_nb = {
  1115. .notifier_call = iommu_reconfig_notifier,
  1116. };
  1117. /* These are called very early. */
  1118. void iommu_init_early_pSeries(void)
  1119. {
  1120. if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
  1121. return;
  1122. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  1123. if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
  1124. ppc_md.tce_build = tce_buildmulti_pSeriesLP;
  1125. ppc_md.tce_free = tce_freemulti_pSeriesLP;
  1126. } else {
  1127. ppc_md.tce_build = tce_build_pSeriesLP;
  1128. ppc_md.tce_free = tce_free_pSeriesLP;
  1129. }
  1130. ppc_md.tce_get = tce_get_pSeriesLP;
  1131. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
  1132. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
  1133. ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
  1134. ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
  1135. } else {
  1136. ppc_md.tce_build = tce_build_pSeries;
  1137. ppc_md.tce_free = tce_free_pSeries;
  1138. ppc_md.tce_get = tce_get_pseries;
  1139. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeries;
  1140. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeries;
  1141. }
  1142. of_reconfig_notifier_register(&iommu_reconfig_nb);
  1143. register_memory_notifier(&iommu_mem_nb);
  1144. set_pci_dma_ops(&dma_iommu_ops);
  1145. }
  1146. static int __init disable_multitce(char *str)
  1147. {
  1148. if (strcmp(str, "off") == 0 &&
  1149. firmware_has_feature(FW_FEATURE_LPAR) &&
  1150. firmware_has_feature(FW_FEATURE_MULTITCE)) {
  1151. printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
  1152. ppc_md.tce_build = tce_build_pSeriesLP;
  1153. ppc_md.tce_free = tce_free_pSeriesLP;
  1154. powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
  1155. }
  1156. return 1;
  1157. }
  1158. __setup("multitce=", disable_multitce);