iommu.c 37 KB

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