iommu.c 35 KB

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