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;
  56. struct iommu_table *tbl;
  57. struct iommu_table_group_link *tgl;
  58. table_group = kzalloc_node(sizeof(struct iommu_table_group), GFP_KERNEL,
  59. node);
  60. if (!table_group)
  61. return NULL;
  62. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, node);
  63. if (!tbl)
  64. goto free_group;
  65. tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
  66. node);
  67. if (!tgl)
  68. goto free_table;
  69. INIT_LIST_HEAD_RCU(&tbl->it_group_list);
  70. kref_init(&tbl->it_kref);
  71. tgl->table_group = table_group;
  72. list_add_rcu(&tgl->next, &tbl->it_group_list);
  73. table_group->tables[0] = tbl;
  74. return table_group;
  75. free_table:
  76. kfree(tbl);
  77. free_group:
  78. kfree(table_group);
  79. return NULL;
  80. }
  81. static void iommu_pseries_free_group(struct iommu_table_group *table_group,
  82. const char *node_name)
  83. {
  84. struct iommu_table *tbl;
  85. #ifdef CONFIG_IOMMU_API
  86. struct iommu_table_group_link *tgl;
  87. #endif
  88. if (!table_group)
  89. return;
  90. tbl = table_group->tables[0];
  91. #ifdef CONFIG_IOMMU_API
  92. tgl = list_first_entry_or_null(&tbl->it_group_list,
  93. struct iommu_table_group_link, next);
  94. WARN_ON_ONCE(!tgl);
  95. if (tgl) {
  96. list_del_rcu(&tgl->next);
  97. kfree(tgl);
  98. }
  99. if (table_group->group) {
  100. iommu_group_put(table_group->group);
  101. BUG_ON(table_group->group);
  102. }
  103. #endif
  104. iommu_tce_table_put(tbl);
  105. kfree(table_group);
  106. }
  107. static int tce_build_pSeries(struct iommu_table *tbl, long index,
  108. long npages, unsigned long uaddr,
  109. enum dma_data_direction direction,
  110. unsigned long attrs)
  111. {
  112. u64 proto_tce;
  113. __be64 *tcep, *tces;
  114. u64 rpn;
  115. proto_tce = TCE_PCI_READ; // Read allowed
  116. if (direction != DMA_TO_DEVICE)
  117. proto_tce |= TCE_PCI_WRITE;
  118. tces = tcep = ((__be64 *)tbl->it_base) + index;
  119. while (npages--) {
  120. /* can't move this out since we might cross MEMBLOCK boundary */
  121. rpn = __pa(uaddr) >> TCE_SHIFT;
  122. *tcep = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
  123. uaddr += TCE_PAGE_SIZE;
  124. tcep++;
  125. }
  126. return 0;
  127. }
  128. static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
  129. {
  130. __be64 *tcep, *tces;
  131. tces = tcep = ((__be64 *)tbl->it_base) + index;
  132. while (npages--)
  133. *(tcep++) = 0;
  134. }
  135. static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
  136. {
  137. __be64 *tcep;
  138. tcep = ((__be64 *)tbl->it_base) + index;
  139. return be64_to_cpu(*tcep);
  140. }
  141. static void tce_free_pSeriesLP(unsigned long liobn, long, long);
  142. static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
  143. static int tce_build_pSeriesLP(unsigned long liobn, long tcenum, long tceshift,
  144. long npages, unsigned long uaddr,
  145. enum dma_data_direction direction,
  146. unsigned long attrs)
  147. {
  148. u64 rc = 0;
  149. u64 proto_tce, tce;
  150. u64 rpn;
  151. int ret = 0;
  152. long tcenum_start = tcenum, npages_start = npages;
  153. rpn = __pa(uaddr) >> tceshift;
  154. proto_tce = TCE_PCI_READ;
  155. if (direction != DMA_TO_DEVICE)
  156. proto_tce |= TCE_PCI_WRITE;
  157. while (npages--) {
  158. tce = proto_tce | (rpn & TCE_RPN_MASK) << tceshift;
  159. rc = plpar_tce_put((u64)liobn, (u64)tcenum << tceshift, tce);
  160. if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
  161. ret = (int)rc;
  162. tce_free_pSeriesLP(liobn, tcenum_start,
  163. (npages_start - (npages + 1)));
  164. break;
  165. }
  166. if (rc && printk_ratelimit()) {
  167. printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
  168. printk("\tindex = 0x%llx\n", (u64)liobn);
  169. printk("\ttcenum = 0x%llx\n", (u64)tcenum);
  170. printk("\ttce val = 0x%llx\n", tce );
  171. dump_stack();
  172. }
  173. tcenum++;
  174. rpn++;
  175. }
  176. return ret;
  177. }
  178. static DEFINE_PER_CPU(__be64 *, tce_page);
  179. static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
  180. long npages, unsigned long uaddr,
  181. enum dma_data_direction direction,
  182. unsigned long attrs)
  183. {
  184. u64 rc = 0;
  185. u64 proto_tce;
  186. __be64 *tcep;
  187. u64 rpn;
  188. long l, limit;
  189. long tcenum_start = tcenum, npages_start = npages;
  190. int ret = 0;
  191. unsigned long flags;
  192. if ((npages == 1) || !firmware_has_feature(FW_FEATURE_MULTITCE)) {
  193. return tce_build_pSeriesLP(tbl->it_index, tcenum,
  194. tbl->it_page_shift, npages, uaddr,
  195. direction, attrs);
  196. }
  197. local_irq_save(flags); /* to protect tcep and the page behind it */
  198. tcep = __this_cpu_read(tce_page);
  199. /* This is safe to do since interrupts are off when we're called
  200. * from iommu_alloc{,_sg}()
  201. */
  202. if (!tcep) {
  203. tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
  204. /* If allocation fails, fall back to the loop implementation */
  205. if (!tcep) {
  206. local_irq_restore(flags);
  207. return tce_build_pSeriesLP(tbl->it_index, tcenum,
  208. tbl->it_page_shift,
  209. npages, uaddr, direction, attrs);
  210. }
  211. __this_cpu_write(tce_page, tcep);
  212. }
  213. rpn = __pa(uaddr) >> TCE_SHIFT;
  214. proto_tce = TCE_PCI_READ;
  215. if (direction != DMA_TO_DEVICE)
  216. proto_tce |= TCE_PCI_WRITE;
  217. /* We can map max one pageful of TCEs at a time */
  218. do {
  219. /*
  220. * Set up the page with TCE data, looping through and setting
  221. * the values.
  222. */
  223. limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
  224. for (l = 0; l < limit; l++) {
  225. tcep[l] = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
  226. rpn++;
  227. }
  228. rc = plpar_tce_put_indirect((u64)tbl->it_index,
  229. (u64)tcenum << 12,
  230. (u64)__pa(tcep),
  231. limit);
  232. npages -= limit;
  233. tcenum += limit;
  234. } while (npages > 0 && !rc);
  235. local_irq_restore(flags);
  236. if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
  237. ret = (int)rc;
  238. tce_freemulti_pSeriesLP(tbl, tcenum_start,
  239. (npages_start - (npages + limit)));
  240. return ret;
  241. }
  242. if (rc && printk_ratelimit()) {
  243. printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
  244. printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
  245. printk("\tnpages = 0x%llx\n", (u64)npages);
  246. printk("\ttce[0] val = 0x%llx\n", tcep[0]);
  247. dump_stack();
  248. }
  249. return ret;
  250. }
  251. static void tce_free_pSeriesLP(unsigned long liobn, long tcenum, long npages)
  252. {
  253. u64 rc;
  254. while (npages--) {
  255. rc = plpar_tce_put((u64)liobn, (u64)tcenum << 12, 0);
  256. if (rc && printk_ratelimit()) {
  257. printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
  258. printk("\tindex = 0x%llx\n", (u64)liobn);
  259. printk("\ttcenum = 0x%llx\n", (u64)tcenum);
  260. dump_stack();
  261. }
  262. tcenum++;
  263. }
  264. }
  265. static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
  266. {
  267. u64 rc;
  268. if (!firmware_has_feature(FW_FEATURE_MULTITCE))
  269. return tce_free_pSeriesLP(tbl->it_index, tcenum, npages);
  270. rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
  271. if (rc && printk_ratelimit()) {
  272. printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
  273. printk("\trc = %lld\n", rc);
  274. printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
  275. printk("\tnpages = 0x%llx\n", (u64)npages);
  276. dump_stack();
  277. }
  278. }
  279. static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
  280. {
  281. u64 rc;
  282. unsigned long tce_ret;
  283. rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
  284. if (rc && printk_ratelimit()) {
  285. printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
  286. printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
  287. printk("\ttcenum = 0x%llx\n", (u64)tcenum);
  288. dump_stack();
  289. }
  290. return tce_ret;
  291. }
  292. /* this is compatible with cells for the device tree property */
  293. struct dynamic_dma_window_prop {
  294. __be32 liobn; /* tce table number */
  295. __be64 dma_base; /* address hi,lo */
  296. __be32 tce_shift; /* ilog2(tce_page_size) */
  297. __be32 window_shift; /* ilog2(tce_window_size) */
  298. };
  299. struct direct_window {
  300. struct device_node *device;
  301. const struct dynamic_dma_window_prop *prop;
  302. struct list_head list;
  303. };
  304. /* Dynamic DMA Window support */
  305. struct ddw_query_response {
  306. u32 windows_available;
  307. u32 largest_available_block;
  308. u32 page_size;
  309. u32 migration_capable;
  310. };
  311. struct ddw_create_response {
  312. u32 liobn;
  313. u32 addr_hi;
  314. u32 addr_lo;
  315. };
  316. static LIST_HEAD(direct_window_list);
  317. /* prevents races between memory on/offline and window creation */
  318. static DEFINE_SPINLOCK(direct_window_list_lock);
  319. /* protects initializing window twice for same device */
  320. static DEFINE_MUTEX(direct_window_init_mutex);
  321. #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
  322. static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,
  323. unsigned long num_pfn, const void *arg)
  324. {
  325. const struct dynamic_dma_window_prop *maprange = arg;
  326. int rc;
  327. u64 tce_size, num_tce, dma_offset, next;
  328. u32 tce_shift;
  329. long limit;
  330. tce_shift = be32_to_cpu(maprange->tce_shift);
  331. tce_size = 1ULL << tce_shift;
  332. next = start_pfn << PAGE_SHIFT;
  333. num_tce = num_pfn << PAGE_SHIFT;
  334. /* round back to the beginning of the tce page size */
  335. num_tce += next & (tce_size - 1);
  336. next &= ~(tce_size - 1);
  337. /* covert to number of tces */
  338. num_tce |= tce_size - 1;
  339. num_tce >>= tce_shift;
  340. do {
  341. /*
  342. * Set up the page with TCE data, looping through and setting
  343. * the values.
  344. */
  345. limit = min_t(long, num_tce, 512);
  346. dma_offset = next + be64_to_cpu(maprange->dma_base);
  347. rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn),
  348. dma_offset,
  349. 0, limit);
  350. next += limit * tce_size;
  351. num_tce -= limit;
  352. } while (num_tce > 0 && !rc);
  353. return rc;
  354. }
  355. static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
  356. unsigned long num_pfn, const void *arg)
  357. {
  358. const struct dynamic_dma_window_prop *maprange = arg;
  359. u64 tce_size, num_tce, dma_offset, next, proto_tce, liobn;
  360. __be64 *tcep;
  361. u32 tce_shift;
  362. u64 rc = 0;
  363. long l, limit;
  364. if (!firmware_has_feature(FW_FEATURE_MULTITCE)) {
  365. unsigned long tceshift = be32_to_cpu(maprange->tce_shift);
  366. unsigned long dmastart = (start_pfn << PAGE_SHIFT) +
  367. be64_to_cpu(maprange->dma_base);
  368. unsigned long tcenum = dmastart >> tceshift;
  369. unsigned long npages = num_pfn << PAGE_SHIFT >> tceshift;
  370. void *uaddr = __va(start_pfn << PAGE_SHIFT);
  371. return tce_build_pSeriesLP(be32_to_cpu(maprange->liobn),
  372. tcenum, tceshift, npages, (unsigned long) uaddr,
  373. DMA_BIDIRECTIONAL, 0);
  374. }
  375. local_irq_disable(); /* to protect tcep and the page behind it */
  376. tcep = __this_cpu_read(tce_page);
  377. if (!tcep) {
  378. tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
  379. if (!tcep) {
  380. local_irq_enable();
  381. return -ENOMEM;
  382. }
  383. __this_cpu_write(tce_page, tcep);
  384. }
  385. proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
  386. liobn = (u64)be32_to_cpu(maprange->liobn);
  387. tce_shift = be32_to_cpu(maprange->tce_shift);
  388. tce_size = 1ULL << tce_shift;
  389. next = start_pfn << PAGE_SHIFT;
  390. num_tce = num_pfn << PAGE_SHIFT;
  391. /* round back to the beginning of the tce page size */
  392. num_tce += next & (tce_size - 1);
  393. next &= ~(tce_size - 1);
  394. /* covert to number of tces */
  395. num_tce |= tce_size - 1;
  396. num_tce >>= tce_shift;
  397. /* We can map max one pageful of TCEs at a time */
  398. do {
  399. /*
  400. * Set up the page with TCE data, looping through and setting
  401. * the values.
  402. */
  403. limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
  404. dma_offset = next + be64_to_cpu(maprange->dma_base);
  405. for (l = 0; l < limit; l++) {
  406. tcep[l] = cpu_to_be64(proto_tce | next);
  407. next += tce_size;
  408. }
  409. rc = plpar_tce_put_indirect(liobn,
  410. dma_offset,
  411. (u64)__pa(tcep),
  412. limit);
  413. num_tce -= limit;
  414. } while (num_tce > 0 && !rc);
  415. /* error cleanup: caller will clear whole range */
  416. local_irq_enable();
  417. return rc;
  418. }
  419. static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
  420. unsigned long num_pfn, void *arg)
  421. {
  422. return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
  423. }
  424. static void iommu_table_setparms(struct pci_controller *phb,
  425. struct device_node *dn,
  426. struct iommu_table *tbl)
  427. {
  428. struct device_node *node;
  429. const unsigned long *basep;
  430. const u32 *sizep;
  431. node = phb->dn;
  432. basep = of_get_property(node, "linux,tce-base", NULL);
  433. sizep = of_get_property(node, "linux,tce-size", NULL);
  434. if (basep == NULL || sizep == NULL) {
  435. printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %pOF has "
  436. "missing tce entries !\n", dn);
  437. return;
  438. }
  439. tbl->it_base = (unsigned long)__va(*basep);
  440. if (!is_kdump_kernel())
  441. memset((void *)tbl->it_base, 0, *sizep);
  442. tbl->it_busno = phb->bus->number;
  443. tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
  444. /* Units of tce entries */
  445. tbl->it_offset = phb->dma_window_base_cur >> tbl->it_page_shift;
  446. /* Test if we are going over 2GB of DMA space */
  447. if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
  448. udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  449. panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  450. }
  451. phb->dma_window_base_cur += phb->dma_window_size;
  452. /* Set the tce table size - measured in entries */
  453. tbl->it_size = phb->dma_window_size >> tbl->it_page_shift;
  454. tbl->it_index = 0;
  455. tbl->it_blocksize = 16;
  456. tbl->it_type = TCE_PCI;
  457. }
  458. /*
  459. * iommu_table_setparms_lpar
  460. *
  461. * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
  462. */
  463. static void iommu_table_setparms_lpar(struct pci_controller *phb,
  464. struct device_node *dn,
  465. struct iommu_table *tbl,
  466. struct iommu_table_group *table_group,
  467. const __be32 *dma_window)
  468. {
  469. unsigned long offset, size;
  470. of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
  471. tbl->it_busno = phb->bus->number;
  472. tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
  473. tbl->it_base = 0;
  474. tbl->it_blocksize = 16;
  475. tbl->it_type = TCE_PCI;
  476. tbl->it_offset = offset >> tbl->it_page_shift;
  477. tbl->it_size = size >> tbl->it_page_shift;
  478. table_group->tce32_start = offset;
  479. table_group->tce32_size = size;
  480. }
  481. struct iommu_table_ops iommu_table_pseries_ops = {
  482. .set = tce_build_pSeries,
  483. .clear = tce_free_pSeries,
  484. .get = tce_get_pseries
  485. };
  486. static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
  487. {
  488. struct device_node *dn;
  489. struct iommu_table *tbl;
  490. struct device_node *isa_dn, *isa_dn_orig;
  491. struct device_node *tmp;
  492. struct pci_dn *pci;
  493. int children;
  494. dn = pci_bus_to_OF_node(bus);
  495. pr_debug("pci_dma_bus_setup_pSeries: setting up bus %pOF\n", dn);
  496. if (bus->self) {
  497. /* This is not a root bus, any setup will be done for the
  498. * device-side of the bridge in iommu_dev_setup_pSeries().
  499. */
  500. return;
  501. }
  502. pci = PCI_DN(dn);
  503. /* Check if the ISA bus on the system is under
  504. * this PHB.
  505. */
  506. isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
  507. while (isa_dn && isa_dn != dn)
  508. isa_dn = isa_dn->parent;
  509. of_node_put(isa_dn_orig);
  510. /* Count number of direct PCI children of the PHB. */
  511. for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
  512. children++;
  513. pr_debug("Children: %d\n", children);
  514. /* Calculate amount of DMA window per slot. Each window must be
  515. * a power of two (due to pci_alloc_consistent requirements).
  516. *
  517. * Keep 256MB aside for PHBs with ISA.
  518. */
  519. if (!isa_dn) {
  520. /* No ISA/IDE - just set window size and return */
  521. pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
  522. while (pci->phb->dma_window_size * children > 0x80000000ul)
  523. pci->phb->dma_window_size >>= 1;
  524. pr_debug("No ISA/IDE, window size is 0x%llx\n",
  525. pci->phb->dma_window_size);
  526. pci->phb->dma_window_base_cur = 0;
  527. return;
  528. }
  529. /* If we have ISA, then we probably have an IDE
  530. * controller too. Allocate a 128MB table but
  531. * skip the first 128MB to avoid stepping on ISA
  532. * space.
  533. */
  534. pci->phb->dma_window_size = 0x8000000ul;
  535. pci->phb->dma_window_base_cur = 0x8000000ul;
  536. pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
  537. tbl = pci->table_group->tables[0];
  538. iommu_table_setparms(pci->phb, dn, tbl);
  539. tbl->it_ops = &iommu_table_pseries_ops;
  540. iommu_init_table(tbl, pci->phb->node);
  541. iommu_register_group(pci->table_group, pci_domain_nr(bus), 0);
  542. /* Divide the rest (1.75GB) among the children */
  543. pci->phb->dma_window_size = 0x80000000ul;
  544. while (pci->phb->dma_window_size * children > 0x70000000ul)
  545. pci->phb->dma_window_size >>= 1;
  546. pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
  547. }
  548. #ifdef CONFIG_IOMMU_API
  549. static int tce_exchange_pseries(struct iommu_table *tbl, long index, unsigned
  550. long *tce, enum dma_data_direction *direction)
  551. {
  552. long rc;
  553. unsigned long ioba = (unsigned long) index << tbl->it_page_shift;
  554. unsigned long flags, oldtce = 0;
  555. u64 proto_tce = iommu_direction_to_tce_perm(*direction);
  556. unsigned long newtce = *tce | proto_tce;
  557. spin_lock_irqsave(&tbl->large_pool.lock, flags);
  558. rc = plpar_tce_get((u64)tbl->it_index, ioba, &oldtce);
  559. if (!rc)
  560. rc = plpar_tce_put((u64)tbl->it_index, ioba, newtce);
  561. if (!rc) {
  562. *direction = iommu_tce_direction(oldtce);
  563. *tce = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
  564. }
  565. spin_unlock_irqrestore(&tbl->large_pool.lock, flags);
  566. return rc;
  567. }
  568. #endif
  569. struct iommu_table_ops iommu_table_lpar_multi_ops = {
  570. .set = tce_buildmulti_pSeriesLP,
  571. #ifdef CONFIG_IOMMU_API
  572. .exchange = tce_exchange_pseries,
  573. #endif
  574. .clear = tce_freemulti_pSeriesLP,
  575. .get = tce_get_pSeriesLP
  576. };
  577. static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
  578. {
  579. struct iommu_table *tbl;
  580. struct device_node *dn, *pdn;
  581. struct pci_dn *ppci;
  582. const __be32 *dma_window = NULL;
  583. dn = pci_bus_to_OF_node(bus);
  584. pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %pOF\n",
  585. dn);
  586. /* Find nearest ibm,dma-window, walking up the device tree */
  587. for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
  588. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  589. if (dma_window != NULL)
  590. break;
  591. }
  592. if (dma_window == NULL) {
  593. pr_debug(" no ibm,dma-window property !\n");
  594. return;
  595. }
  596. ppci = PCI_DN(pdn);
  597. pr_debug(" parent is %pOF, iommu_table: 0x%p\n",
  598. pdn, ppci->table_group);
  599. if (!ppci->table_group) {
  600. ppci->table_group = iommu_pseries_alloc_group(ppci->phb->node);
  601. tbl = ppci->table_group->tables[0];
  602. iommu_table_setparms_lpar(ppci->phb, pdn, tbl,
  603. ppci->table_group, dma_window);
  604. tbl->it_ops = &iommu_table_lpar_multi_ops;
  605. iommu_init_table(tbl, ppci->phb->node);
  606. iommu_register_group(ppci->table_group,
  607. pci_domain_nr(bus), 0);
  608. pr_debug(" created table: %p\n", ppci->table_group);
  609. }
  610. }
  611. static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
  612. {
  613. struct device_node *dn;
  614. struct iommu_table *tbl;
  615. pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
  616. dn = dev->dev.of_node;
  617. /* If we're the direct child of a root bus, then we need to allocate
  618. * an iommu table ourselves. The bus setup code should have setup
  619. * the window sizes already.
  620. */
  621. if (!dev->bus->self) {
  622. struct pci_controller *phb = PCI_DN(dn)->phb;
  623. pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
  624. PCI_DN(dn)->table_group = iommu_pseries_alloc_group(phb->node);
  625. tbl = PCI_DN(dn)->table_group->tables[0];
  626. iommu_table_setparms(phb, dn, tbl);
  627. tbl->it_ops = &iommu_table_pseries_ops;
  628. iommu_init_table(tbl, phb->node);
  629. iommu_register_group(PCI_DN(dn)->table_group,
  630. pci_domain_nr(phb->bus), 0);
  631. set_iommu_table_base(&dev->dev, tbl);
  632. iommu_add_device(&dev->dev);
  633. return;
  634. }
  635. /* If this device is further down the bus tree, search upwards until
  636. * an already allocated iommu table is found and use that.
  637. */
  638. while (dn && PCI_DN(dn) && PCI_DN(dn)->table_group == NULL)
  639. dn = dn->parent;
  640. if (dn && PCI_DN(dn)) {
  641. set_iommu_table_base(&dev->dev,
  642. PCI_DN(dn)->table_group->tables[0]);
  643. iommu_add_device(&dev->dev);
  644. } else
  645. printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
  646. pci_name(dev));
  647. }
  648. static int __read_mostly disable_ddw;
  649. static int __init disable_ddw_setup(char *str)
  650. {
  651. disable_ddw = 1;
  652. printk(KERN_INFO "ppc iommu: disabling ddw.\n");
  653. return 0;
  654. }
  655. early_param("disable_ddw", disable_ddw_setup);
  656. static void remove_ddw(struct device_node *np, bool remove_prop)
  657. {
  658. struct dynamic_dma_window_prop *dwp;
  659. struct property *win64;
  660. u32 ddw_avail[3];
  661. u64 liobn;
  662. int ret = 0;
  663. ret = of_property_read_u32_array(np, "ibm,ddw-applicable",
  664. &ddw_avail[0], 3);
  665. win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
  666. if (!win64)
  667. return;
  668. if (ret || win64->length < sizeof(*dwp))
  669. goto delprop;
  670. dwp = win64->value;
  671. liobn = (u64)be32_to_cpu(dwp->liobn);
  672. /* clear the whole window, note the arg is in kernel pages */
  673. ret = tce_clearrange_multi_pSeriesLP(0,
  674. 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
  675. if (ret)
  676. pr_warn("%pOF failed to clear tces in window.\n",
  677. np);
  678. else
  679. pr_debug("%pOF successfully cleared tces in window.\n",
  680. np);
  681. ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
  682. if (ret)
  683. pr_warn("%pOF: failed to remove direct window: rtas returned "
  684. "%d to ibm,remove-pe-dma-window(%x) %llx\n",
  685. np, ret, ddw_avail[2], liobn);
  686. else
  687. pr_debug("%pOF: successfully removed direct window: rtas returned "
  688. "%d to ibm,remove-pe-dma-window(%x) %llx\n",
  689. np, ret, ddw_avail[2], liobn);
  690. delprop:
  691. if (remove_prop)
  692. ret = of_remove_property(np, win64);
  693. if (ret)
  694. pr_warn("%pOF: failed to remove direct window property: %d\n",
  695. np, ret);
  696. }
  697. static u64 find_existing_ddw(struct device_node *pdn)
  698. {
  699. struct direct_window *window;
  700. const struct dynamic_dma_window_prop *direct64;
  701. u64 dma_addr = 0;
  702. spin_lock(&direct_window_list_lock);
  703. /* check if we already created a window and dupe that config if so */
  704. list_for_each_entry(window, &direct_window_list, list) {
  705. if (window->device == pdn) {
  706. direct64 = window->prop;
  707. dma_addr = be64_to_cpu(direct64->dma_base);
  708. break;
  709. }
  710. }
  711. spin_unlock(&direct_window_list_lock);
  712. return dma_addr;
  713. }
  714. static int find_existing_ddw_windows(void)
  715. {
  716. int len;
  717. struct device_node *pdn;
  718. struct direct_window *window;
  719. const struct dynamic_dma_window_prop *direct64;
  720. if (!firmware_has_feature(FW_FEATURE_LPAR))
  721. return 0;
  722. for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
  723. direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
  724. if (!direct64)
  725. continue;
  726. window = kzalloc(sizeof(*window), GFP_KERNEL);
  727. if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
  728. kfree(window);
  729. remove_ddw(pdn, true);
  730. continue;
  731. }
  732. window->device = pdn;
  733. window->prop = direct64;
  734. spin_lock(&direct_window_list_lock);
  735. list_add(&window->list, &direct_window_list);
  736. spin_unlock(&direct_window_list_lock);
  737. }
  738. return 0;
  739. }
  740. machine_arch_initcall(pseries, find_existing_ddw_windows);
  741. static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
  742. struct ddw_query_response *query)
  743. {
  744. struct device_node *dn;
  745. struct pci_dn *pdn;
  746. u32 cfg_addr;
  747. u64 buid;
  748. int ret;
  749. /*
  750. * Get the config address and phb buid of the PE window.
  751. * Rely on eeh to retrieve this for us.
  752. * Retrieve them from the pci device, not the node with the
  753. * dma-window property
  754. */
  755. dn = pci_device_to_OF_node(dev);
  756. pdn = PCI_DN(dn);
  757. buid = pdn->phb->buid;
  758. cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
  759. ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
  760. cfg_addr, BUID_HI(buid), BUID_LO(buid));
  761. dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
  762. " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
  763. BUID_LO(buid), ret);
  764. return ret;
  765. }
  766. static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
  767. struct ddw_create_response *create, int page_shift,
  768. int window_shift)
  769. {
  770. struct device_node *dn;
  771. struct pci_dn *pdn;
  772. u32 cfg_addr;
  773. u64 buid;
  774. int ret;
  775. /*
  776. * Get the config address and phb buid of the PE window.
  777. * Rely on eeh to retrieve this for us.
  778. * Retrieve them from the pci device, not the node with the
  779. * dma-window property
  780. */
  781. dn = pci_device_to_OF_node(dev);
  782. pdn = PCI_DN(dn);
  783. buid = pdn->phb->buid;
  784. cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
  785. do {
  786. /* extra outputs are LIOBN and dma-addr (hi, lo) */
  787. ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create,
  788. cfg_addr, BUID_HI(buid), BUID_LO(buid),
  789. page_shift, window_shift);
  790. } while (rtas_busy_delay(ret));
  791. dev_info(&dev->dev,
  792. "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
  793. "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
  794. cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
  795. window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
  796. return ret;
  797. }
  798. struct failed_ddw_pdn {
  799. struct device_node *pdn;
  800. struct list_head list;
  801. };
  802. static LIST_HEAD(failed_ddw_pdn_list);
  803. /*
  804. * If the PE supports dynamic dma windows, and there is space for a table
  805. * that can map all pages in a linear offset, then setup such a table,
  806. * and record the dma-offset in the struct device.
  807. *
  808. * dev: the pci device we are checking
  809. * pdn: the parent pe node with the ibm,dma_window property
  810. * Future: also check if we can remap the base window for our base page size
  811. *
  812. * returns the dma offset for use by dma_set_mask
  813. */
  814. static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
  815. {
  816. int len, ret;
  817. struct ddw_query_response query;
  818. struct ddw_create_response create;
  819. int page_shift;
  820. u64 dma_addr, max_addr;
  821. struct device_node *dn;
  822. u32 ddw_avail[3];
  823. struct direct_window *window;
  824. struct property *win64;
  825. struct dynamic_dma_window_prop *ddwprop;
  826. struct failed_ddw_pdn *fpdn;
  827. mutex_lock(&direct_window_init_mutex);
  828. dma_addr = find_existing_ddw(pdn);
  829. if (dma_addr != 0)
  830. goto out_unlock;
  831. /*
  832. * If we already went through this for a previous function of
  833. * the same device and failed, we don't want to muck with the
  834. * DMA window again, as it will race with in-flight operations
  835. * and can lead to EEHs. The above mutex protects access to the
  836. * list.
  837. */
  838. list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) {
  839. if (fpdn->pdn == pdn)
  840. goto out_unlock;
  841. }
  842. /*
  843. * the ibm,ddw-applicable property holds the tokens for:
  844. * ibm,query-pe-dma-window
  845. * ibm,create-pe-dma-window
  846. * ibm,remove-pe-dma-window
  847. * for the given node in that order.
  848. * the property is actually in the parent, not the PE
  849. */
  850. ret = of_property_read_u32_array(pdn, "ibm,ddw-applicable",
  851. &ddw_avail[0], 3);
  852. if (ret)
  853. goto out_failed;
  854. /*
  855. * Query if there is a second window of size to map the
  856. * whole partition. Query returns number of windows, largest
  857. * block assigned to PE (partition endpoint), and two bitmasks
  858. * of page sizes: supported and supported for migrate-dma.
  859. */
  860. dn = pci_device_to_OF_node(dev);
  861. ret = query_ddw(dev, ddw_avail, &query);
  862. if (ret != 0)
  863. goto out_failed;
  864. if (query.windows_available == 0) {
  865. /*
  866. * no additional windows are available for this device.
  867. * We might be able to reallocate the existing window,
  868. * trading in for a larger page size.
  869. */
  870. dev_dbg(&dev->dev, "no free dynamic windows");
  871. goto out_failed;
  872. }
  873. if (query.page_size & 4) {
  874. page_shift = 24; /* 16MB */
  875. } else if (query.page_size & 2) {
  876. page_shift = 16; /* 64kB */
  877. } else if (query.page_size & 1) {
  878. page_shift = 12; /* 4kB */
  879. } else {
  880. dev_dbg(&dev->dev, "no supported direct page size in mask %x",
  881. query.page_size);
  882. goto out_failed;
  883. }
  884. /* verify the window * number of ptes will map the partition */
  885. /* check largest block * page size > max memory hotplug addr */
  886. max_addr = memory_hotplug_max();
  887. if (query.largest_available_block < (max_addr >> page_shift)) {
  888. dev_dbg(&dev->dev, "can't map partition max 0x%llx with %u "
  889. "%llu-sized pages\n", max_addr, query.largest_available_block,
  890. 1ULL << page_shift);
  891. goto out_failed;
  892. }
  893. len = order_base_2(max_addr);
  894. win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
  895. if (!win64) {
  896. dev_info(&dev->dev,
  897. "couldn't allocate property for 64bit dma window\n");
  898. goto out_failed;
  899. }
  900. win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
  901. win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
  902. win64->length = sizeof(*ddwprop);
  903. if (!win64->name || !win64->value) {
  904. dev_info(&dev->dev,
  905. "couldn't allocate property name and value\n");
  906. goto out_free_prop;
  907. }
  908. ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
  909. if (ret != 0)
  910. goto out_free_prop;
  911. ddwprop->liobn = cpu_to_be32(create.liobn);
  912. ddwprop->dma_base = cpu_to_be64(((u64)create.addr_hi << 32) |
  913. create.addr_lo);
  914. ddwprop->tce_shift = cpu_to_be32(page_shift);
  915. ddwprop->window_shift = cpu_to_be32(len);
  916. dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %pOF\n",
  917. create.liobn, dn);
  918. window = kzalloc(sizeof(*window), GFP_KERNEL);
  919. if (!window)
  920. goto out_clear_window;
  921. ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
  922. win64->value, tce_setrange_multi_pSeriesLP_walk);
  923. if (ret) {
  924. dev_info(&dev->dev, "failed to map direct window for %pOF: %d\n",
  925. dn, ret);
  926. goto out_free_window;
  927. }
  928. ret = of_add_property(pdn, win64);
  929. if (ret) {
  930. dev_err(&dev->dev, "unable to add dma window property for %pOF: %d",
  931. pdn, ret);
  932. goto out_free_window;
  933. }
  934. window->device = pdn;
  935. window->prop = ddwprop;
  936. spin_lock(&direct_window_list_lock);
  937. list_add(&window->list, &direct_window_list);
  938. spin_unlock(&direct_window_list_lock);
  939. dma_addr = be64_to_cpu(ddwprop->dma_base);
  940. goto out_unlock;
  941. out_free_window:
  942. kfree(window);
  943. out_clear_window:
  944. remove_ddw(pdn, true);
  945. out_free_prop:
  946. kfree(win64->name);
  947. kfree(win64->value);
  948. kfree(win64);
  949. out_failed:
  950. fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
  951. if (!fpdn)
  952. goto out_unlock;
  953. fpdn->pdn = pdn;
  954. list_add(&fpdn->list, &failed_ddw_pdn_list);
  955. out_unlock:
  956. mutex_unlock(&direct_window_init_mutex);
  957. return dma_addr;
  958. }
  959. static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
  960. {
  961. struct device_node *pdn, *dn;
  962. struct iommu_table *tbl;
  963. const __be32 *dma_window = NULL;
  964. struct pci_dn *pci;
  965. pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
  966. /* dev setup for LPAR is a little tricky, since the device tree might
  967. * contain the dma-window properties per-device and not necessarily
  968. * for the bus. So we need to search upwards in the tree until we
  969. * either hit a dma-window property, OR find a parent with a table
  970. * already allocated.
  971. */
  972. dn = pci_device_to_OF_node(dev);
  973. pr_debug(" node is %pOF\n", dn);
  974. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
  975. pdn = pdn->parent) {
  976. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  977. if (dma_window)
  978. break;
  979. }
  980. if (!pdn || !PCI_DN(pdn)) {
  981. printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
  982. "no DMA window found for pci dev=%s dn=%pOF\n",
  983. pci_name(dev), dn);
  984. return;
  985. }
  986. pr_debug(" parent is %pOF\n", pdn);
  987. pci = PCI_DN(pdn);
  988. if (!pci->table_group) {
  989. pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
  990. tbl = pci->table_group->tables[0];
  991. iommu_table_setparms_lpar(pci->phb, pdn, tbl,
  992. pci->table_group, 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 %pOF\n", dn);
  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_nommu_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);