iommu.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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/spinlock.h>
  31. #include <linux/sched.h> /* for show_stack */
  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 <asm/io.h>
  38. #include <asm/prom.h>
  39. #include <asm/rtas.h>
  40. #include <asm/iommu.h>
  41. #include <asm/pci-bridge.h>
  42. #include <asm/machdep.h>
  43. #include <asm/abs_addr.h>
  44. #include <asm/pSeries_reconfig.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 "plpar_wrappers.h"
  51. static void tce_invalidate_pSeries_sw(struct iommu_table *tbl,
  52. u64 *startp, u64 *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. u64 *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 = ((u64 *)tbl->it_base) + index;
  87. while (npages--) {
  88. /* can't move this out since we might cross MEMBLOCK boundary */
  89. rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
  90. *tcep = 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. u64 *tcep, *tces;
  101. tces = tcep = ((u64 *)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. u64 *tcep;
  110. tcep = ((u64 *)tbl->it_base) + index;
  111. return *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 = (virt_to_abs(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(u64 *, 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. u64 *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 = (u64 *)__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 = (virt_to_abs(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] = 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)virt_to_abs(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. num_tce -= limit;
  319. } while (num_tce > 0 && !rc);
  320. return rc;
  321. }
  322. static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
  323. unsigned long num_pfn, const void *arg)
  324. {
  325. const struct dynamic_dma_window_prop *maprange = arg;
  326. u64 *tcep, tce_size, num_tce, dma_offset, next, proto_tce, liobn;
  327. u32 tce_shift;
  328. u64 rc = 0;
  329. long l, limit;
  330. local_irq_disable(); /* to protect tcep and the page behind it */
  331. tcep = __get_cpu_var(tce_page);
  332. if (!tcep) {
  333. tcep = (u64 *)__get_free_page(GFP_ATOMIC);
  334. if (!tcep) {
  335. local_irq_enable();
  336. return -ENOMEM;
  337. }
  338. __get_cpu_var(tce_page) = tcep;
  339. }
  340. proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
  341. liobn = (u64)be32_to_cpu(maprange->liobn);
  342. tce_shift = be32_to_cpu(maprange->tce_shift);
  343. tce_size = 1ULL << tce_shift;
  344. next = start_pfn << PAGE_SHIFT;
  345. num_tce = num_pfn << PAGE_SHIFT;
  346. /* round back to the beginning of the tce page size */
  347. num_tce += next & (tce_size - 1);
  348. next &= ~(tce_size - 1);
  349. /* covert to number of tces */
  350. num_tce |= tce_size - 1;
  351. num_tce >>= tce_shift;
  352. /* We can map max one pageful of TCEs at a time */
  353. do {
  354. /*
  355. * Set up the page with TCE data, looping through and setting
  356. * the values.
  357. */
  358. limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
  359. dma_offset = next + be64_to_cpu(maprange->dma_base);
  360. for (l = 0; l < limit; l++) {
  361. tcep[l] = proto_tce | next;
  362. next += tce_size;
  363. }
  364. rc = plpar_tce_put_indirect(liobn,
  365. dma_offset,
  366. (u64)virt_to_abs(tcep),
  367. limit);
  368. num_tce -= limit;
  369. } while (num_tce > 0 && !rc);
  370. /* error cleanup: caller will clear whole range */
  371. local_irq_enable();
  372. return rc;
  373. }
  374. static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
  375. unsigned long num_pfn, void *arg)
  376. {
  377. return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
  378. }
  379. #ifdef CONFIG_PCI
  380. static void iommu_table_setparms(struct pci_controller *phb,
  381. struct device_node *dn,
  382. struct iommu_table *tbl)
  383. {
  384. struct device_node *node;
  385. const unsigned long *basep, *sw_inval;
  386. const u32 *sizep;
  387. node = phb->dn;
  388. basep = of_get_property(node, "linux,tce-base", NULL);
  389. sizep = of_get_property(node, "linux,tce-size", NULL);
  390. if (basep == NULL || sizep == NULL) {
  391. printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
  392. "missing tce entries !\n", dn->full_name);
  393. return;
  394. }
  395. tbl->it_base = (unsigned long)__va(*basep);
  396. if (!is_kdump_kernel())
  397. memset((void *)tbl->it_base, 0, *sizep);
  398. tbl->it_busno = phb->bus->number;
  399. /* Units of tce entries */
  400. tbl->it_offset = phb->dma_window_base_cur >> IOMMU_PAGE_SHIFT;
  401. /* Test if we are going over 2GB of DMA space */
  402. if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
  403. udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  404. panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
  405. }
  406. phb->dma_window_base_cur += phb->dma_window_size;
  407. /* Set the tce table size - measured in entries */
  408. tbl->it_size = phb->dma_window_size >> IOMMU_PAGE_SHIFT;
  409. tbl->it_index = 0;
  410. tbl->it_blocksize = 16;
  411. tbl->it_type = TCE_PCI;
  412. sw_inval = of_get_property(node, "linux,tce-sw-invalidate-info", NULL);
  413. if (sw_inval) {
  414. /*
  415. * This property contains information on how to
  416. * invalidate the TCE entry. The first property is
  417. * the base MMIO address used to invalidate entries.
  418. * The second property tells us the format of the TCE
  419. * invalidate (whether it needs to be shifted) and
  420. * some magic routing info to add to our invalidate
  421. * command.
  422. */
  423. tbl->it_index = (unsigned long) ioremap(sw_inval[0], 8);
  424. tbl->it_busno = sw_inval[1]; /* overload this with magic */
  425. tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
  426. }
  427. }
  428. /*
  429. * iommu_table_setparms_lpar
  430. *
  431. * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
  432. */
  433. static void iommu_table_setparms_lpar(struct pci_controller *phb,
  434. struct device_node *dn,
  435. struct iommu_table *tbl,
  436. const void *dma_window)
  437. {
  438. unsigned long offset, size;
  439. of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
  440. tbl->it_busno = phb->bus->number;
  441. tbl->it_base = 0;
  442. tbl->it_blocksize = 16;
  443. tbl->it_type = TCE_PCI;
  444. tbl->it_offset = offset >> IOMMU_PAGE_SHIFT;
  445. tbl->it_size = size >> IOMMU_PAGE_SHIFT;
  446. }
  447. static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
  448. {
  449. struct device_node *dn;
  450. struct iommu_table *tbl;
  451. struct device_node *isa_dn, *isa_dn_orig;
  452. struct device_node *tmp;
  453. struct pci_dn *pci;
  454. int children;
  455. dn = pci_bus_to_OF_node(bus);
  456. pr_debug("pci_dma_bus_setup_pSeries: setting up bus %s\n", dn->full_name);
  457. if (bus->self) {
  458. /* This is not a root bus, any setup will be done for the
  459. * device-side of the bridge in iommu_dev_setup_pSeries().
  460. */
  461. return;
  462. }
  463. pci = PCI_DN(dn);
  464. /* Check if the ISA bus on the system is under
  465. * this PHB.
  466. */
  467. isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
  468. while (isa_dn && isa_dn != dn)
  469. isa_dn = isa_dn->parent;
  470. if (isa_dn_orig)
  471. of_node_put(isa_dn_orig);
  472. /* Count number of direct PCI children of the PHB. */
  473. for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
  474. children++;
  475. pr_debug("Children: %d\n", children);
  476. /* Calculate amount of DMA window per slot. Each window must be
  477. * a power of two (due to pci_alloc_consistent requirements).
  478. *
  479. * Keep 256MB aside for PHBs with ISA.
  480. */
  481. if (!isa_dn) {
  482. /* No ISA/IDE - just set window size and return */
  483. pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
  484. while (pci->phb->dma_window_size * children > 0x80000000ul)
  485. pci->phb->dma_window_size >>= 1;
  486. pr_debug("No ISA/IDE, window size is 0x%llx\n",
  487. pci->phb->dma_window_size);
  488. pci->phb->dma_window_base_cur = 0;
  489. return;
  490. }
  491. /* If we have ISA, then we probably have an IDE
  492. * controller too. Allocate a 128MB table but
  493. * skip the first 128MB to avoid stepping on ISA
  494. * space.
  495. */
  496. pci->phb->dma_window_size = 0x8000000ul;
  497. pci->phb->dma_window_base_cur = 0x8000000ul;
  498. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  499. pci->phb->node);
  500. iommu_table_setparms(pci->phb, dn, tbl);
  501. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  502. /* Divide the rest (1.75GB) among the children */
  503. pci->phb->dma_window_size = 0x80000000ul;
  504. while (pci->phb->dma_window_size * children > 0x70000000ul)
  505. pci->phb->dma_window_size >>= 1;
  506. pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
  507. }
  508. static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
  509. {
  510. struct iommu_table *tbl;
  511. struct device_node *dn, *pdn;
  512. struct pci_dn *ppci;
  513. const void *dma_window = NULL;
  514. dn = pci_bus_to_OF_node(bus);
  515. pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
  516. dn->full_name);
  517. /* Find nearest ibm,dma-window, walking up the device tree */
  518. for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
  519. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  520. if (dma_window != NULL)
  521. break;
  522. }
  523. if (dma_window == NULL) {
  524. pr_debug(" no ibm,dma-window property !\n");
  525. return;
  526. }
  527. ppci = PCI_DN(pdn);
  528. pr_debug(" parent is %s, iommu_table: 0x%p\n",
  529. pdn->full_name, ppci->iommu_table);
  530. if (!ppci->iommu_table) {
  531. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  532. ppci->phb->node);
  533. iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
  534. ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
  535. pr_debug(" created table: %p\n", ppci->iommu_table);
  536. }
  537. }
  538. static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
  539. {
  540. struct device_node *dn;
  541. struct iommu_table *tbl;
  542. pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
  543. dn = dev->dev.of_node;
  544. /* If we're the direct child of a root bus, then we need to allocate
  545. * an iommu table ourselves. The bus setup code should have setup
  546. * the window sizes already.
  547. */
  548. if (!dev->bus->self) {
  549. struct pci_controller *phb = PCI_DN(dn)->phb;
  550. pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
  551. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  552. phb->node);
  553. iommu_table_setparms(phb, dn, tbl);
  554. PCI_DN(dn)->iommu_table = iommu_init_table(tbl, phb->node);
  555. set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
  556. return;
  557. }
  558. /* If this device is further down the bus tree, search upwards until
  559. * an already allocated iommu table is found and use that.
  560. */
  561. while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
  562. dn = dn->parent;
  563. if (dn && PCI_DN(dn))
  564. set_iommu_table_base(&dev->dev, PCI_DN(dn)->iommu_table);
  565. else
  566. printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
  567. pci_name(dev));
  568. }
  569. static int __read_mostly disable_ddw;
  570. static int __init disable_ddw_setup(char *str)
  571. {
  572. disable_ddw = 1;
  573. printk(KERN_INFO "ppc iommu: disabling ddw.\n");
  574. return 0;
  575. }
  576. early_param("disable_ddw", disable_ddw_setup);
  577. static inline void __remove_ddw(struct device_node *np, const u32 *ddw_avail, u64 liobn)
  578. {
  579. int ret;
  580. ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
  581. if (ret)
  582. pr_warning("%s: failed to remove DMA window: rtas returned "
  583. "%d to ibm,remove-pe-dma-window(%x) %llx\n",
  584. np->full_name, ret, ddw_avail[2], liobn);
  585. else
  586. pr_debug("%s: successfully removed DMA window: rtas returned "
  587. "%d to ibm,remove-pe-dma-window(%x) %llx\n",
  588. np->full_name, ret, ddw_avail[2], liobn);
  589. }
  590. static void remove_ddw(struct device_node *np)
  591. {
  592. struct dynamic_dma_window_prop *dwp;
  593. struct property *win64;
  594. const u32 *ddw_avail;
  595. u64 liobn;
  596. int len, ret;
  597. ddw_avail = of_get_property(np, "ibm,ddw-applicable", &len);
  598. win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
  599. if (!win64)
  600. return;
  601. if (!ddw_avail || len < 3 * sizeof(u32) || win64->length < sizeof(*dwp))
  602. goto delprop;
  603. dwp = win64->value;
  604. liobn = (u64)be32_to_cpu(dwp->liobn);
  605. /* clear the whole window, note the arg is in kernel pages */
  606. ret = tce_clearrange_multi_pSeriesLP(0,
  607. 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
  608. if (ret)
  609. pr_warning("%s failed to clear tces in window.\n",
  610. np->full_name);
  611. else
  612. pr_debug("%s successfully cleared tces in window.\n",
  613. np->full_name);
  614. __remove_ddw(np, ddw_avail, liobn);
  615. delprop:
  616. ret = prom_remove_property(np, win64);
  617. if (ret)
  618. pr_warning("%s: failed to remove direct window property: %d\n",
  619. np->full_name, ret);
  620. }
  621. static u64 find_existing_ddw(struct device_node *pdn)
  622. {
  623. struct direct_window *window;
  624. const struct dynamic_dma_window_prop *direct64;
  625. u64 dma_addr = 0;
  626. spin_lock(&direct_window_list_lock);
  627. /* check if we already created a window and dupe that config if so */
  628. list_for_each_entry(window, &direct_window_list, list) {
  629. if (window->device == pdn) {
  630. direct64 = window->prop;
  631. dma_addr = direct64->dma_base;
  632. break;
  633. }
  634. }
  635. spin_unlock(&direct_window_list_lock);
  636. return dma_addr;
  637. }
  638. static int find_existing_ddw_windows(void)
  639. {
  640. int len;
  641. struct device_node *pdn;
  642. struct direct_window *window;
  643. const struct dynamic_dma_window_prop *direct64;
  644. if (!firmware_has_feature(FW_FEATURE_LPAR))
  645. return 0;
  646. for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
  647. direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
  648. if (!direct64)
  649. continue;
  650. window = kzalloc(sizeof(*window), GFP_KERNEL);
  651. if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
  652. kfree(window);
  653. remove_ddw(pdn);
  654. continue;
  655. }
  656. window->device = pdn;
  657. window->prop = direct64;
  658. spin_lock(&direct_window_list_lock);
  659. list_add(&window->list, &direct_window_list);
  660. spin_unlock(&direct_window_list_lock);
  661. }
  662. return 0;
  663. }
  664. machine_arch_initcall(pseries, find_existing_ddw_windows);
  665. static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
  666. struct ddw_query_response *query)
  667. {
  668. struct eeh_dev *edev;
  669. u32 cfg_addr;
  670. u64 buid;
  671. int ret;
  672. /*
  673. * Get the config address and phb buid of the PE window.
  674. * Rely on eeh to retrieve this for us.
  675. * Retrieve them from the pci device, not the node with the
  676. * dma-window property
  677. */
  678. edev = pci_dev_to_eeh_dev(dev);
  679. cfg_addr = edev->config_addr;
  680. if (edev->pe_config_addr)
  681. cfg_addr = edev->pe_config_addr;
  682. buid = edev->phb->buid;
  683. ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
  684. cfg_addr, BUID_HI(buid), BUID_LO(buid));
  685. dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
  686. " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
  687. BUID_LO(buid), ret);
  688. return ret;
  689. }
  690. static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
  691. struct ddw_create_response *create, int page_shift,
  692. int window_shift)
  693. {
  694. struct eeh_dev *edev;
  695. u32 cfg_addr;
  696. u64 buid;
  697. int ret;
  698. /*
  699. * Get the config address and phb buid of the PE window.
  700. * Rely on eeh to retrieve this for us.
  701. * Retrieve them from the pci device, not the node with the
  702. * dma-window property
  703. */
  704. edev = pci_dev_to_eeh_dev(dev);
  705. cfg_addr = edev->config_addr;
  706. if (edev->pe_config_addr)
  707. cfg_addr = edev->pe_config_addr;
  708. buid = edev->phb->buid;
  709. do {
  710. /* extra outputs are LIOBN and dma-addr (hi, lo) */
  711. ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create, cfg_addr,
  712. BUID_HI(buid), BUID_LO(buid), page_shift, window_shift);
  713. } while (rtas_busy_delay(ret));
  714. dev_info(&dev->dev,
  715. "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
  716. "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
  717. cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
  718. window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
  719. return ret;
  720. }
  721. static void restore_default_window(struct pci_dev *dev,
  722. u32 ddw_restore_token, unsigned long liobn)
  723. {
  724. struct eeh_dev *edev;
  725. u32 cfg_addr;
  726. u64 buid;
  727. int ret;
  728. /*
  729. * Get the config address and phb buid of the PE window.
  730. * Rely on eeh to retrieve this for us.
  731. * Retrieve them from the pci device, not the node with the
  732. * dma-window property
  733. */
  734. edev = pci_dev_to_eeh_dev(dev);
  735. cfg_addr = edev->config_addr;
  736. if (edev->pe_config_addr)
  737. cfg_addr = edev->pe_config_addr;
  738. buid = edev->phb->buid;
  739. do {
  740. ret = rtas_call(ddw_restore_token, 3, 1, NULL, cfg_addr,
  741. BUID_HI(buid), BUID_LO(buid));
  742. } while (rtas_busy_delay(ret));
  743. dev_info(&dev->dev,
  744. "ibm,reset-pe-dma-windows(%x) %x %x %x returned %d\n",
  745. ddw_restore_token, cfg_addr, BUID_HI(buid), BUID_LO(buid), ret);
  746. }
  747. /*
  748. * If the PE supports dynamic dma windows, and there is space for a table
  749. * that can map all pages in a linear offset, then setup such a table,
  750. * and record the dma-offset in the struct device.
  751. *
  752. * dev: the pci device we are checking
  753. * pdn: the parent pe node with the ibm,dma_window property
  754. * Future: also check if we can remap the base window for our base page size
  755. *
  756. * returns the dma offset for use by dma_set_mask
  757. */
  758. static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
  759. {
  760. int len, ret;
  761. struct ddw_query_response query;
  762. struct ddw_create_response create;
  763. int page_shift;
  764. u64 dma_addr, max_addr;
  765. struct device_node *dn;
  766. const u32 *uninitialized_var(ddw_avail);
  767. const u32 *uninitialized_var(ddw_extensions);
  768. u32 ddw_restore_token = 0;
  769. struct direct_window *window;
  770. struct property *win64;
  771. struct dynamic_dma_window_prop *ddwprop;
  772. const void *dma_window = NULL;
  773. unsigned long liobn, offset, size;
  774. mutex_lock(&direct_window_init_mutex);
  775. dma_addr = find_existing_ddw(pdn);
  776. if (dma_addr != 0)
  777. goto out_unlock;
  778. /*
  779. * the ibm,ddw-applicable property holds the tokens for:
  780. * ibm,query-pe-dma-window
  781. * ibm,create-pe-dma-window
  782. * ibm,remove-pe-dma-window
  783. * for the given node in that order.
  784. * the property is actually in the parent, not the PE
  785. */
  786. ddw_avail = of_get_property(pdn, "ibm,ddw-applicable", &len);
  787. if (!ddw_avail || len < 3 * sizeof(u32))
  788. goto out_unlock;
  789. /*
  790. * the extensions property is only required to exist in certain
  791. * levels of firmware and later
  792. * the ibm,ddw-extensions property is a list with the first
  793. * element containing the number of extensions and each
  794. * subsequent entry is a value corresponding to that extension
  795. */
  796. ddw_extensions = of_get_property(pdn, "ibm,ddw-extensions", &len);
  797. if (ddw_extensions) {
  798. /*
  799. * each new defined extension length should be added to
  800. * the top of the switch so the "earlier" entries also
  801. * get picked up
  802. */
  803. switch (ddw_extensions[0]) {
  804. /* ibm,reset-pe-dma-windows */
  805. case 1:
  806. ddw_restore_token = ddw_extensions[1];
  807. break;
  808. }
  809. }
  810. /*
  811. * Only remove the existing DMA window if we can restore back to
  812. * the default state. Removing the existing window maximizes the
  813. * resources available to firmware for dynamic window creation.
  814. */
  815. if (ddw_restore_token) {
  816. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  817. of_parse_dma_window(pdn, dma_window, &liobn, &offset, &size);
  818. __remove_ddw(pdn, ddw_avail, liobn);
  819. }
  820. /*
  821. * Query if there is a second window of size to map the
  822. * whole partition. Query returns number of windows, largest
  823. * block assigned to PE (partition endpoint), and two bitmasks
  824. * of page sizes: supported and supported for migrate-dma.
  825. */
  826. dn = pci_device_to_OF_node(dev);
  827. ret = query_ddw(dev, ddw_avail, &query);
  828. if (ret != 0)
  829. goto out_restore_window;
  830. if (query.windows_available == 0) {
  831. /*
  832. * no additional windows are available for this device.
  833. * We might be able to reallocate the existing window,
  834. * trading in for a larger page size.
  835. */
  836. dev_dbg(&dev->dev, "no free dynamic windows");
  837. goto out_restore_window;
  838. }
  839. if (query.page_size & 4) {
  840. page_shift = 24; /* 16MB */
  841. } else if (query.page_size & 2) {
  842. page_shift = 16; /* 64kB */
  843. } else if (query.page_size & 1) {
  844. page_shift = 12; /* 4kB */
  845. } else {
  846. dev_dbg(&dev->dev, "no supported direct page size in mask %x",
  847. query.page_size);
  848. goto out_restore_window;
  849. }
  850. /* verify the window * number of ptes will map the partition */
  851. /* check largest block * page size > max memory hotplug addr */
  852. max_addr = memory_hotplug_max();
  853. if (query.largest_available_block < (max_addr >> page_shift)) {
  854. dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
  855. "%llu-sized pages\n", max_addr, query.largest_available_block,
  856. 1ULL << page_shift);
  857. goto out_restore_window;
  858. }
  859. len = order_base_2(max_addr);
  860. win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
  861. if (!win64) {
  862. dev_info(&dev->dev,
  863. "couldn't allocate property for 64bit dma window\n");
  864. goto out_restore_window;
  865. }
  866. win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
  867. win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
  868. win64->length = sizeof(*ddwprop);
  869. if (!win64->name || !win64->value) {
  870. dev_info(&dev->dev,
  871. "couldn't allocate property name and value\n");
  872. goto out_free_prop;
  873. }
  874. ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
  875. if (ret != 0)
  876. goto out_free_prop;
  877. ddwprop->liobn = cpu_to_be32(create.liobn);
  878. ddwprop->dma_base = cpu_to_be64(of_read_number(&create.addr_hi, 2));
  879. ddwprop->tce_shift = cpu_to_be32(page_shift);
  880. ddwprop->window_shift = cpu_to_be32(len);
  881. dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
  882. create.liobn, dn->full_name);
  883. window = kzalloc(sizeof(*window), GFP_KERNEL);
  884. if (!window)
  885. goto out_clear_window;
  886. ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
  887. win64->value, tce_setrange_multi_pSeriesLP_walk);
  888. if (ret) {
  889. dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
  890. dn->full_name, ret);
  891. goto out_free_window;
  892. }
  893. ret = prom_add_property(pdn, win64);
  894. if (ret) {
  895. dev_err(&dev->dev, "unable to add dma window property for %s: %d",
  896. pdn->full_name, ret);
  897. goto out_free_window;
  898. }
  899. window->device = pdn;
  900. window->prop = ddwprop;
  901. spin_lock(&direct_window_list_lock);
  902. list_add(&window->list, &direct_window_list);
  903. spin_unlock(&direct_window_list_lock);
  904. dma_addr = of_read_number(&create.addr_hi, 2);
  905. goto out_unlock;
  906. out_free_window:
  907. kfree(window);
  908. out_clear_window:
  909. remove_ddw(pdn);
  910. out_free_prop:
  911. kfree(win64->name);
  912. kfree(win64->value);
  913. kfree(win64);
  914. out_restore_window:
  915. if (ddw_restore_token)
  916. restore_default_window(dev, ddw_restore_token, liobn);
  917. out_unlock:
  918. mutex_unlock(&direct_window_init_mutex);
  919. return dma_addr;
  920. }
  921. static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
  922. {
  923. struct device_node *pdn, *dn;
  924. struct iommu_table *tbl;
  925. const void *dma_window = NULL;
  926. struct pci_dn *pci;
  927. pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
  928. /* dev setup for LPAR is a little tricky, since the device tree might
  929. * contain the dma-window properties per-device and not necessarily
  930. * for the bus. So we need to search upwards in the tree until we
  931. * either hit a dma-window property, OR find a parent with a table
  932. * already allocated.
  933. */
  934. dn = pci_device_to_OF_node(dev);
  935. pr_debug(" node is %s\n", dn->full_name);
  936. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  937. pdn = pdn->parent) {
  938. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  939. if (dma_window)
  940. break;
  941. }
  942. if (!pdn || !PCI_DN(pdn)) {
  943. printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
  944. "no DMA window found for pci dev=%s dn=%s\n",
  945. pci_name(dev), of_node_full_name(dn));
  946. return;
  947. }
  948. pr_debug(" parent is %s\n", pdn->full_name);
  949. pci = PCI_DN(pdn);
  950. if (!pci->iommu_table) {
  951. tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
  952. pci->phb->node);
  953. iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
  954. pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
  955. pr_debug(" created table: %p\n", pci->iommu_table);
  956. } else {
  957. pr_debug(" found DMA window, table: %p\n", pci->iommu_table);
  958. }
  959. set_iommu_table_base(&dev->dev, pci->iommu_table);
  960. }
  961. static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
  962. {
  963. bool ddw_enabled = false;
  964. struct device_node *pdn, *dn;
  965. struct pci_dev *pdev;
  966. const void *dma_window = NULL;
  967. u64 dma_offset;
  968. if (!dev->dma_mask)
  969. return -EIO;
  970. if (!dev_is_pci(dev))
  971. goto check_mask;
  972. pdev = to_pci_dev(dev);
  973. /* only attempt to use a new window if 64-bit DMA is requested */
  974. if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
  975. dn = pci_device_to_OF_node(pdev);
  976. dev_dbg(dev, "node is %s\n", dn->full_name);
  977. /*
  978. * the device tree might contain the dma-window properties
  979. * per-device and not necessarily for the bus. So we need to
  980. * search upwards in the tree until we either hit a dma-window
  981. * property, OR find a parent with a table already allocated.
  982. */
  983. for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
  984. pdn = pdn->parent) {
  985. dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
  986. if (dma_window)
  987. break;
  988. }
  989. if (pdn && PCI_DN(pdn)) {
  990. dma_offset = enable_ddw(pdev, pdn);
  991. if (dma_offset != 0) {
  992. dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
  993. set_dma_offset(dev, dma_offset);
  994. set_dma_ops(dev, &dma_direct_ops);
  995. ddw_enabled = true;
  996. }
  997. }
  998. }
  999. /* fall back on iommu ops, restore table pointer with ops */
  1000. if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
  1001. dev_info(dev, "Restoring 32-bit DMA via iommu\n");
  1002. set_dma_ops(dev, &dma_iommu_ops);
  1003. pci_dma_dev_setup_pSeriesLP(pdev);
  1004. }
  1005. check_mask:
  1006. if (!dma_supported(dev, dma_mask))
  1007. return -EIO;
  1008. *dev->dma_mask = dma_mask;
  1009. return 0;
  1010. }
  1011. static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
  1012. {
  1013. if (!dev->dma_mask)
  1014. return 0;
  1015. if (!disable_ddw && dev_is_pci(dev)) {
  1016. struct pci_dev *pdev = to_pci_dev(dev);
  1017. struct device_node *dn;
  1018. dn = pci_device_to_OF_node(pdev);
  1019. /* search upwards for ibm,dma-window */
  1020. for (; dn && PCI_DN(dn) && !PCI_DN(dn)->iommu_table;
  1021. dn = dn->parent)
  1022. if (of_get_property(dn, "ibm,dma-window", NULL))
  1023. break;
  1024. /* if there is a ibm,ddw-applicable property require 64 bits */
  1025. if (dn && PCI_DN(dn) &&
  1026. of_get_property(dn, "ibm,ddw-applicable", NULL))
  1027. return DMA_BIT_MASK(64);
  1028. }
  1029. return dma_iommu_ops.get_required_mask(dev);
  1030. }
  1031. #else /* CONFIG_PCI */
  1032. #define pci_dma_bus_setup_pSeries NULL
  1033. #define pci_dma_dev_setup_pSeries NULL
  1034. #define pci_dma_bus_setup_pSeriesLP NULL
  1035. #define pci_dma_dev_setup_pSeriesLP NULL
  1036. #define dma_set_mask_pSeriesLP NULL
  1037. #define dma_get_required_mask_pSeriesLP NULL
  1038. #endif /* !CONFIG_PCI */
  1039. static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
  1040. void *data)
  1041. {
  1042. struct direct_window *window;
  1043. struct memory_notify *arg = data;
  1044. int ret = 0;
  1045. switch (action) {
  1046. case MEM_GOING_ONLINE:
  1047. spin_lock(&direct_window_list_lock);
  1048. list_for_each_entry(window, &direct_window_list, list) {
  1049. ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
  1050. arg->nr_pages, window->prop);
  1051. /* XXX log error */
  1052. }
  1053. spin_unlock(&direct_window_list_lock);
  1054. break;
  1055. case MEM_CANCEL_ONLINE:
  1056. case MEM_OFFLINE:
  1057. spin_lock(&direct_window_list_lock);
  1058. list_for_each_entry(window, &direct_window_list, list) {
  1059. ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
  1060. arg->nr_pages, window->prop);
  1061. /* XXX log error */
  1062. }
  1063. spin_unlock(&direct_window_list_lock);
  1064. break;
  1065. default:
  1066. break;
  1067. }
  1068. if (ret && action != MEM_CANCEL_ONLINE)
  1069. return NOTIFY_BAD;
  1070. return NOTIFY_OK;
  1071. }
  1072. static struct notifier_block iommu_mem_nb = {
  1073. .notifier_call = iommu_mem_notifier,
  1074. };
  1075. static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  1076. {
  1077. int err = NOTIFY_OK;
  1078. struct device_node *np = node;
  1079. struct pci_dn *pci = PCI_DN(np);
  1080. struct direct_window *window;
  1081. switch (action) {
  1082. case PSERIES_RECONFIG_REMOVE:
  1083. if (pci && pci->iommu_table)
  1084. iommu_free_table(pci->iommu_table, np->full_name);
  1085. spin_lock(&direct_window_list_lock);
  1086. list_for_each_entry(window, &direct_window_list, list) {
  1087. if (window->device == np) {
  1088. list_del(&window->list);
  1089. kfree(window);
  1090. break;
  1091. }
  1092. }
  1093. spin_unlock(&direct_window_list_lock);
  1094. /*
  1095. * Because the notifier runs after isolation of the
  1096. * slot, we are guaranteed any DMA window has already
  1097. * been revoked and the TCEs have been marked invalid,
  1098. * so we don't need a call to remove_ddw(np). However,
  1099. * if an additional notifier action is added before the
  1100. * isolate call, we should update this code for
  1101. * completeness with such a call.
  1102. */
  1103. break;
  1104. default:
  1105. err = NOTIFY_DONE;
  1106. break;
  1107. }
  1108. return err;
  1109. }
  1110. static struct notifier_block iommu_reconfig_nb = {
  1111. .notifier_call = iommu_reconfig_notifier,
  1112. };
  1113. /* These are called very early. */
  1114. void iommu_init_early_pSeries(void)
  1115. {
  1116. if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
  1117. return;
  1118. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  1119. if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
  1120. ppc_md.tce_build = tce_buildmulti_pSeriesLP;
  1121. ppc_md.tce_free = tce_freemulti_pSeriesLP;
  1122. } else {
  1123. ppc_md.tce_build = tce_build_pSeriesLP;
  1124. ppc_md.tce_free = tce_free_pSeriesLP;
  1125. }
  1126. ppc_md.tce_get = tce_get_pSeriesLP;
  1127. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
  1128. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
  1129. ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
  1130. ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
  1131. } else {
  1132. ppc_md.tce_build = tce_build_pSeries;
  1133. ppc_md.tce_free = tce_free_pSeries;
  1134. ppc_md.tce_get = tce_get_pseries;
  1135. ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_pSeries;
  1136. ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_pSeries;
  1137. }
  1138. pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
  1139. register_memory_notifier(&iommu_mem_nb);
  1140. set_pci_dma_ops(&dma_iommu_ops);
  1141. }
  1142. static int __init disable_multitce(char *str)
  1143. {
  1144. if (strcmp(str, "off") == 0 &&
  1145. firmware_has_feature(FW_FEATURE_LPAR) &&
  1146. firmware_has_feature(FW_FEATURE_MULTITCE)) {
  1147. printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
  1148. ppc_md.tce_build = tce_build_pSeriesLP;
  1149. ppc_md.tce_free = tce_free_pSeriesLP;
  1150. powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
  1151. }
  1152. return 1;
  1153. }
  1154. __setup("multitce=", disable_multitce);