iommu.c 36 KB

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