iommu.c 38 KB

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