iommu.c 36 KB

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