iommu.c 32 KB

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