iommu.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. /*
  2. * IOMMU implementation for Cell Broadband Processor Architecture
  3. *
  4. * (C) Copyright IBM Corporation 2006-2008
  5. *
  6. * Author: Jeremy Kerr <jk@ozlabs.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #undef DEBUG
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/notifier.h>
  27. #include <linux/of.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/lmb.h>
  30. #include <asm/prom.h>
  31. #include <asm/iommu.h>
  32. #include <asm/machdep.h>
  33. #include <asm/pci-bridge.h>
  34. #include <asm/udbg.h>
  35. #include <asm/firmware.h>
  36. #include <asm/cell-regs.h>
  37. #include "interrupt.h"
  38. /* Define CELL_IOMMU_REAL_UNMAP to actually unmap non-used pages
  39. * instead of leaving them mapped to some dummy page. This can be
  40. * enabled once the appropriate workarounds for spider bugs have
  41. * been enabled
  42. */
  43. #define CELL_IOMMU_REAL_UNMAP
  44. /* Define CELL_IOMMU_STRICT_PROTECTION to enforce protection of
  45. * IO PTEs based on the transfer direction. That can be enabled
  46. * once spider-net has been fixed to pass the correct direction
  47. * to the DMA mapping functions
  48. */
  49. #define CELL_IOMMU_STRICT_PROTECTION
  50. #define NR_IOMMUS 2
  51. /* IOC mmap registers */
  52. #define IOC_Reg_Size 0x2000
  53. #define IOC_IOPT_CacheInvd 0x908
  54. #define IOC_IOPT_CacheInvd_NE_Mask 0xffe0000000000000ul
  55. #define IOC_IOPT_CacheInvd_IOPTE_Mask 0x000003fffffffff8ul
  56. #define IOC_IOPT_CacheInvd_Busy 0x0000000000000001ul
  57. #define IOC_IOST_Origin 0x918
  58. #define IOC_IOST_Origin_E 0x8000000000000000ul
  59. #define IOC_IOST_Origin_HW 0x0000000000000800ul
  60. #define IOC_IOST_Origin_HL 0x0000000000000400ul
  61. #define IOC_IO_ExcpStat 0x920
  62. #define IOC_IO_ExcpStat_V 0x8000000000000000ul
  63. #define IOC_IO_ExcpStat_SPF_Mask 0x6000000000000000ul
  64. #define IOC_IO_ExcpStat_SPF_S 0x6000000000000000ul
  65. #define IOC_IO_ExcpStat_SPF_P 0x4000000000000000ul
  66. #define IOC_IO_ExcpStat_ADDR_Mask 0x00000007fffff000ul
  67. #define IOC_IO_ExcpStat_RW_Mask 0x0000000000000800ul
  68. #define IOC_IO_ExcpStat_IOID_Mask 0x00000000000007fful
  69. #define IOC_IO_ExcpMask 0x928
  70. #define IOC_IO_ExcpMask_SFE 0x4000000000000000ul
  71. #define IOC_IO_ExcpMask_PFE 0x2000000000000000ul
  72. #define IOC_IOCmd_Offset 0x1000
  73. #define IOC_IOCmd_Cfg 0xc00
  74. #define IOC_IOCmd_Cfg_TE 0x0000800000000000ul
  75. /* Segment table entries */
  76. #define IOSTE_V 0x8000000000000000ul /* valid */
  77. #define IOSTE_H 0x4000000000000000ul /* cache hint */
  78. #define IOSTE_PT_Base_RPN_Mask 0x3ffffffffffff000ul /* base RPN of IOPT */
  79. #define IOSTE_NPPT_Mask 0x0000000000000fe0ul /* no. pages in IOPT */
  80. #define IOSTE_PS_Mask 0x0000000000000007ul /* page size */
  81. #define IOSTE_PS_4K 0x0000000000000001ul /* - 4kB */
  82. #define IOSTE_PS_64K 0x0000000000000003ul /* - 64kB */
  83. #define IOSTE_PS_1M 0x0000000000000005ul /* - 1MB */
  84. #define IOSTE_PS_16M 0x0000000000000007ul /* - 16MB */
  85. /* Page table entries */
  86. #define IOPTE_PP_W 0x8000000000000000ul /* protection: write */
  87. #define IOPTE_PP_R 0x4000000000000000ul /* protection: read */
  88. #define IOPTE_M 0x2000000000000000ul /* coherency required */
  89. #define IOPTE_SO_R 0x1000000000000000ul /* ordering: writes */
  90. #define IOPTE_SO_RW 0x1800000000000000ul /* ordering: r & w */
  91. #define IOPTE_RPN_Mask 0x07fffffffffff000ul /* RPN */
  92. #define IOPTE_H 0x0000000000000800ul /* cache hint */
  93. #define IOPTE_IOID_Mask 0x00000000000007fful /* ioid */
  94. /* IOMMU sizing */
  95. #define IO_SEGMENT_SHIFT 28
  96. #define IO_PAGENO_BITS(shift) (IO_SEGMENT_SHIFT - (shift))
  97. /* The high bit needs to be set on every DMA address */
  98. #define SPIDER_DMA_OFFSET 0x80000000ul
  99. struct iommu_window {
  100. struct list_head list;
  101. struct cbe_iommu *iommu;
  102. unsigned long offset;
  103. unsigned long size;
  104. unsigned int ioid;
  105. struct iommu_table table;
  106. };
  107. #define NAMESIZE 8
  108. struct cbe_iommu {
  109. int nid;
  110. char name[NAMESIZE];
  111. void __iomem *xlate_regs;
  112. void __iomem *cmd_regs;
  113. unsigned long *stab;
  114. unsigned long *ptab;
  115. void *pad_page;
  116. struct list_head windows;
  117. };
  118. /* Static array of iommus, one per node
  119. * each contains a list of windows, keyed from dma_window property
  120. * - on bus setup, look for a matching window, or create one
  121. * - on dev setup, assign iommu_table ptr
  122. */
  123. static struct cbe_iommu iommus[NR_IOMMUS];
  124. static int cbe_nr_iommus;
  125. static void invalidate_tce_cache(struct cbe_iommu *iommu, unsigned long *pte,
  126. long n_ptes)
  127. {
  128. unsigned long __iomem *reg;
  129. unsigned long val;
  130. long n;
  131. reg = iommu->xlate_regs + IOC_IOPT_CacheInvd;
  132. while (n_ptes > 0) {
  133. /* we can invalidate up to 1 << 11 PTEs at once */
  134. n = min(n_ptes, 1l << 11);
  135. val = (((n /*- 1*/) << 53) & IOC_IOPT_CacheInvd_NE_Mask)
  136. | (__pa(pte) & IOC_IOPT_CacheInvd_IOPTE_Mask)
  137. | IOC_IOPT_CacheInvd_Busy;
  138. out_be64(reg, val);
  139. while (in_be64(reg) & IOC_IOPT_CacheInvd_Busy)
  140. ;
  141. n_ptes -= n;
  142. pte += n;
  143. }
  144. }
  145. static int tce_build_cell(struct iommu_table *tbl, long index, long npages,
  146. unsigned long uaddr, enum dma_data_direction direction,
  147. struct dma_attrs *attrs)
  148. {
  149. int i;
  150. unsigned long *io_pte, base_pte;
  151. struct iommu_window *window =
  152. container_of(tbl, struct iommu_window, table);
  153. /* implementing proper protection causes problems with the spidernet
  154. * driver - check mapping directions later, but allow read & write by
  155. * default for now.*/
  156. #ifdef CELL_IOMMU_STRICT_PROTECTION
  157. /* to avoid referencing a global, we use a trick here to setup the
  158. * protection bit. "prot" is setup to be 3 fields of 4 bits apprended
  159. * together for each of the 3 supported direction values. It is then
  160. * shifted left so that the fields matching the desired direction
  161. * lands on the appropriate bits, and other bits are masked out.
  162. */
  163. const unsigned long prot = 0xc48;
  164. base_pte =
  165. ((prot << (52 + 4 * direction)) & (IOPTE_PP_W | IOPTE_PP_R))
  166. | IOPTE_M | IOPTE_SO_RW | (window->ioid & IOPTE_IOID_Mask);
  167. #else
  168. base_pte = IOPTE_PP_W | IOPTE_PP_R | IOPTE_M | IOPTE_SO_RW |
  169. (window->ioid & IOPTE_IOID_Mask);
  170. #endif
  171. if (unlikely(dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs)))
  172. base_pte &= ~IOPTE_SO_RW;
  173. io_pte = (unsigned long *)tbl->it_base + (index - tbl->it_offset);
  174. for (i = 0; i < npages; i++, uaddr += IOMMU_PAGE_SIZE)
  175. io_pte[i] = base_pte | (__pa(uaddr) & IOPTE_RPN_Mask);
  176. mb();
  177. invalidate_tce_cache(window->iommu, io_pte, npages);
  178. pr_debug("tce_build_cell(index=%lx,n=%lx,dir=%d,base_pte=%lx)\n",
  179. index, npages, direction, base_pte);
  180. return 0;
  181. }
  182. static void tce_free_cell(struct iommu_table *tbl, long index, long npages)
  183. {
  184. int i;
  185. unsigned long *io_pte, pte;
  186. struct iommu_window *window =
  187. container_of(tbl, struct iommu_window, table);
  188. pr_debug("tce_free_cell(index=%lx,n=%lx)\n", index, npages);
  189. #ifdef CELL_IOMMU_REAL_UNMAP
  190. pte = 0;
  191. #else
  192. /* spider bridge does PCI reads after freeing - insert a mapping
  193. * to a scratch page instead of an invalid entry */
  194. pte = IOPTE_PP_R | IOPTE_M | IOPTE_SO_RW | __pa(window->iommu->pad_page)
  195. | (window->ioid & IOPTE_IOID_Mask);
  196. #endif
  197. io_pte = (unsigned long *)tbl->it_base + (index - tbl->it_offset);
  198. for (i = 0; i < npages; i++)
  199. io_pte[i] = pte;
  200. mb();
  201. invalidate_tce_cache(window->iommu, io_pte, npages);
  202. }
  203. static irqreturn_t ioc_interrupt(int irq, void *data)
  204. {
  205. unsigned long stat;
  206. struct cbe_iommu *iommu = data;
  207. stat = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);
  208. /* Might want to rate limit it */
  209. printk(KERN_ERR "iommu: DMA exception 0x%016lx\n", stat);
  210. printk(KERN_ERR " V=%d, SPF=[%c%c], RW=%s, IOID=0x%04x\n",
  211. !!(stat & IOC_IO_ExcpStat_V),
  212. (stat & IOC_IO_ExcpStat_SPF_S) ? 'S' : ' ',
  213. (stat & IOC_IO_ExcpStat_SPF_P) ? 'P' : ' ',
  214. (stat & IOC_IO_ExcpStat_RW_Mask) ? "Read" : "Write",
  215. (unsigned int)(stat & IOC_IO_ExcpStat_IOID_Mask));
  216. printk(KERN_ERR " page=0x%016lx\n",
  217. stat & IOC_IO_ExcpStat_ADDR_Mask);
  218. /* clear interrupt */
  219. stat &= ~IOC_IO_ExcpStat_V;
  220. out_be64(iommu->xlate_regs + IOC_IO_ExcpStat, stat);
  221. return IRQ_HANDLED;
  222. }
  223. static int cell_iommu_find_ioc(int nid, unsigned long *base)
  224. {
  225. struct device_node *np;
  226. struct resource r;
  227. *base = 0;
  228. /* First look for new style /be nodes */
  229. for_each_node_by_name(np, "ioc") {
  230. if (of_node_to_nid(np) != nid)
  231. continue;
  232. if (of_address_to_resource(np, 0, &r)) {
  233. printk(KERN_ERR "iommu: can't get address for %s\n",
  234. np->full_name);
  235. continue;
  236. }
  237. *base = r.start;
  238. of_node_put(np);
  239. return 0;
  240. }
  241. /* Ok, let's try the old way */
  242. for_each_node_by_type(np, "cpu") {
  243. const unsigned int *nidp;
  244. const unsigned long *tmp;
  245. nidp = of_get_property(np, "node-id", NULL);
  246. if (nidp && *nidp == nid) {
  247. tmp = of_get_property(np, "ioc-translation", NULL);
  248. if (tmp) {
  249. *base = *tmp;
  250. of_node_put(np);
  251. return 0;
  252. }
  253. }
  254. }
  255. return -ENODEV;
  256. }
  257. static void cell_iommu_setup_stab(struct cbe_iommu *iommu,
  258. unsigned long dbase, unsigned long dsize,
  259. unsigned long fbase, unsigned long fsize)
  260. {
  261. struct page *page;
  262. unsigned long segments, stab_size;
  263. segments = max(dbase + dsize, fbase + fsize) >> IO_SEGMENT_SHIFT;
  264. pr_debug("%s: iommu[%d]: segments: %lu\n",
  265. __func__, iommu->nid, segments);
  266. /* set up the segment table */
  267. stab_size = segments * sizeof(unsigned long);
  268. page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(stab_size));
  269. BUG_ON(!page);
  270. iommu->stab = page_address(page);
  271. memset(iommu->stab, 0, stab_size);
  272. }
  273. static unsigned long *cell_iommu_alloc_ptab(struct cbe_iommu *iommu,
  274. unsigned long base, unsigned long size, unsigned long gap_base,
  275. unsigned long gap_size, unsigned long page_shift)
  276. {
  277. struct page *page;
  278. int i;
  279. unsigned long reg, segments, pages_per_segment, ptab_size,
  280. n_pte_pages, start_seg, *ptab;
  281. start_seg = base >> IO_SEGMENT_SHIFT;
  282. segments = size >> IO_SEGMENT_SHIFT;
  283. pages_per_segment = 1ull << IO_PAGENO_BITS(page_shift);
  284. /* PTEs for each segment must start on a 4K bounday */
  285. pages_per_segment = max(pages_per_segment,
  286. (1 << 12) / sizeof(unsigned long));
  287. ptab_size = segments * pages_per_segment * sizeof(unsigned long);
  288. pr_debug("%s: iommu[%d]: ptab_size: %lu, order: %d\n", __func__,
  289. iommu->nid, ptab_size, get_order(ptab_size));
  290. page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(ptab_size));
  291. BUG_ON(!page);
  292. ptab = page_address(page);
  293. memset(ptab, 0, ptab_size);
  294. /* number of 4K pages needed for a page table */
  295. n_pte_pages = (pages_per_segment * sizeof(unsigned long)) >> 12;
  296. pr_debug("%s: iommu[%d]: stab at %p, ptab at %p, n_pte_pages: %lu\n",
  297. __func__, iommu->nid, iommu->stab, ptab,
  298. n_pte_pages);
  299. /* initialise the STEs */
  300. reg = IOSTE_V | ((n_pte_pages - 1) << 5);
  301. switch (page_shift) {
  302. case 12: reg |= IOSTE_PS_4K; break;
  303. case 16: reg |= IOSTE_PS_64K; break;
  304. case 20: reg |= IOSTE_PS_1M; break;
  305. case 24: reg |= IOSTE_PS_16M; break;
  306. default: BUG();
  307. }
  308. gap_base = gap_base >> IO_SEGMENT_SHIFT;
  309. gap_size = gap_size >> IO_SEGMENT_SHIFT;
  310. pr_debug("Setting up IOMMU stab:\n");
  311. for (i = start_seg; i < (start_seg + segments); i++) {
  312. if (i >= gap_base && i < (gap_base + gap_size)) {
  313. pr_debug("\toverlap at %d, skipping\n", i);
  314. continue;
  315. }
  316. iommu->stab[i] = reg | (__pa(ptab) + (n_pte_pages << 12) *
  317. (i - start_seg));
  318. pr_debug("\t[%d] 0x%016lx\n", i, iommu->stab[i]);
  319. }
  320. return ptab;
  321. }
  322. static void cell_iommu_enable_hardware(struct cbe_iommu *iommu)
  323. {
  324. int ret;
  325. unsigned long reg, xlate_base;
  326. unsigned int virq;
  327. if (cell_iommu_find_ioc(iommu->nid, &xlate_base))
  328. panic("%s: missing IOC register mappings for node %d\n",
  329. __func__, iommu->nid);
  330. iommu->xlate_regs = ioremap(xlate_base, IOC_Reg_Size);
  331. iommu->cmd_regs = iommu->xlate_regs + IOC_IOCmd_Offset;
  332. /* ensure that the STEs have updated */
  333. mb();
  334. /* setup interrupts for the iommu. */
  335. reg = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);
  336. out_be64(iommu->xlate_regs + IOC_IO_ExcpStat,
  337. reg & ~IOC_IO_ExcpStat_V);
  338. out_be64(iommu->xlate_regs + IOC_IO_ExcpMask,
  339. IOC_IO_ExcpMask_PFE | IOC_IO_ExcpMask_SFE);
  340. virq = irq_create_mapping(NULL,
  341. IIC_IRQ_IOEX_ATI | (iommu->nid << IIC_IRQ_NODE_SHIFT));
  342. BUG_ON(virq == NO_IRQ);
  343. ret = request_irq(virq, ioc_interrupt, IRQF_DISABLED,
  344. iommu->name, iommu);
  345. BUG_ON(ret);
  346. /* set the IOC segment table origin register (and turn on the iommu) */
  347. reg = IOC_IOST_Origin_E | __pa(iommu->stab) | IOC_IOST_Origin_HW;
  348. out_be64(iommu->xlate_regs + IOC_IOST_Origin, reg);
  349. in_be64(iommu->xlate_regs + IOC_IOST_Origin);
  350. /* turn on IO translation */
  351. reg = in_be64(iommu->cmd_regs + IOC_IOCmd_Cfg) | IOC_IOCmd_Cfg_TE;
  352. out_be64(iommu->cmd_regs + IOC_IOCmd_Cfg, reg);
  353. }
  354. static void cell_iommu_setup_hardware(struct cbe_iommu *iommu,
  355. unsigned long base, unsigned long size)
  356. {
  357. cell_iommu_setup_stab(iommu, base, size, 0, 0);
  358. iommu->ptab = cell_iommu_alloc_ptab(iommu, base, size, 0, 0,
  359. IOMMU_PAGE_SHIFT);
  360. cell_iommu_enable_hardware(iommu);
  361. }
  362. #if 0/* Unused for now */
  363. static struct iommu_window *find_window(struct cbe_iommu *iommu,
  364. unsigned long offset, unsigned long size)
  365. {
  366. struct iommu_window *window;
  367. /* todo: check for overlapping (but not equal) windows) */
  368. list_for_each_entry(window, &(iommu->windows), list) {
  369. if (window->offset == offset && window->size == size)
  370. return window;
  371. }
  372. return NULL;
  373. }
  374. #endif
  375. static inline u32 cell_iommu_get_ioid(struct device_node *np)
  376. {
  377. const u32 *ioid;
  378. ioid = of_get_property(np, "ioid", NULL);
  379. if (ioid == NULL) {
  380. printk(KERN_WARNING "iommu: missing ioid for %s using 0\n",
  381. np->full_name);
  382. return 0;
  383. }
  384. return *ioid;
  385. }
  386. static struct iommu_window * __init
  387. cell_iommu_setup_window(struct cbe_iommu *iommu, struct device_node *np,
  388. unsigned long offset, unsigned long size,
  389. unsigned long pte_offset)
  390. {
  391. struct iommu_window *window;
  392. struct page *page;
  393. u32 ioid;
  394. ioid = cell_iommu_get_ioid(np);
  395. window = kmalloc_node(sizeof(*window), GFP_KERNEL, iommu->nid);
  396. BUG_ON(window == NULL);
  397. window->offset = offset;
  398. window->size = size;
  399. window->ioid = ioid;
  400. window->iommu = iommu;
  401. window->table.it_blocksize = 16;
  402. window->table.it_base = (unsigned long)iommu->ptab;
  403. window->table.it_index = iommu->nid;
  404. window->table.it_offset = (offset >> IOMMU_PAGE_SHIFT) + pte_offset;
  405. window->table.it_size = size >> IOMMU_PAGE_SHIFT;
  406. iommu_init_table(&window->table, iommu->nid);
  407. pr_debug("\tioid %d\n", window->ioid);
  408. pr_debug("\tblocksize %ld\n", window->table.it_blocksize);
  409. pr_debug("\tbase 0x%016lx\n", window->table.it_base);
  410. pr_debug("\toffset 0x%lx\n", window->table.it_offset);
  411. pr_debug("\tsize %ld\n", window->table.it_size);
  412. list_add(&window->list, &iommu->windows);
  413. if (offset != 0)
  414. return window;
  415. /* We need to map and reserve the first IOMMU page since it's used
  416. * by the spider workaround. In theory, we only need to do that when
  417. * running on spider but it doesn't really matter.
  418. *
  419. * This code also assumes that we have a window that starts at 0,
  420. * which is the case on all spider based blades.
  421. */
  422. page = alloc_pages_node(iommu->nid, GFP_KERNEL, 0);
  423. BUG_ON(!page);
  424. iommu->pad_page = page_address(page);
  425. clear_page(iommu->pad_page);
  426. __set_bit(0, window->table.it_map);
  427. tce_build_cell(&window->table, window->table.it_offset, 1,
  428. (unsigned long)iommu->pad_page, DMA_TO_DEVICE, NULL);
  429. window->table.it_hint = window->table.it_blocksize;
  430. return window;
  431. }
  432. static struct cbe_iommu *cell_iommu_for_node(int nid)
  433. {
  434. int i;
  435. for (i = 0; i < cbe_nr_iommus; i++)
  436. if (iommus[i].nid == nid)
  437. return &iommus[i];
  438. return NULL;
  439. }
  440. static unsigned long cell_dma_direct_offset;
  441. static unsigned long dma_iommu_fixed_base;
  442. /* iommu_fixed_is_weak is set if booted with iommu_fixed=weak */
  443. static int iommu_fixed_is_weak;
  444. static struct iommu_table *cell_get_iommu_table(struct device *dev)
  445. {
  446. struct iommu_window *window;
  447. struct cbe_iommu *iommu;
  448. struct dev_archdata *archdata = &dev->archdata;
  449. /* Current implementation uses the first window available in that
  450. * node's iommu. We -might- do something smarter later though it may
  451. * never be necessary
  452. */
  453. iommu = cell_iommu_for_node(dev_to_node(dev));
  454. if (iommu == NULL || list_empty(&iommu->windows)) {
  455. printk(KERN_ERR "iommu: missing iommu for %s (node %d)\n",
  456. archdata->of_node ? archdata->of_node->full_name : "?",
  457. dev_to_node(dev));
  458. return NULL;
  459. }
  460. window = list_entry(iommu->windows.next, struct iommu_window, list);
  461. return &window->table;
  462. }
  463. /* A coherent allocation implies strong ordering */
  464. static void *dma_fixed_alloc_coherent(struct device *dev, size_t size,
  465. dma_addr_t *dma_handle, gfp_t flag)
  466. {
  467. if (iommu_fixed_is_weak)
  468. return iommu_alloc_coherent(dev, cell_get_iommu_table(dev),
  469. size, dma_handle,
  470. device_to_mask(dev), flag,
  471. dev_to_node(dev));
  472. else
  473. return dma_direct_ops.alloc_coherent(dev, size, dma_handle,
  474. flag);
  475. }
  476. static void dma_fixed_free_coherent(struct device *dev, size_t size,
  477. void *vaddr, dma_addr_t dma_handle)
  478. {
  479. if (iommu_fixed_is_weak)
  480. iommu_free_coherent(cell_get_iommu_table(dev), size, vaddr,
  481. dma_handle);
  482. else
  483. dma_direct_ops.free_coherent(dev, size, vaddr, dma_handle);
  484. }
  485. static dma_addr_t dma_fixed_map_page(struct device *dev, struct page *page,
  486. unsigned long offset, size_t size,
  487. enum dma_data_direction direction,
  488. struct dma_attrs *attrs)
  489. {
  490. if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))
  491. return dma_direct_ops.map_page(dev, page, offset, size,
  492. direction, attrs);
  493. else
  494. return iommu_map_page(dev, cell_get_iommu_table(dev), page,
  495. offset, size, device_to_mask(dev),
  496. direction, attrs);
  497. }
  498. static void dma_fixed_unmap_page(struct device *dev, dma_addr_t dma_addr,
  499. size_t size, enum dma_data_direction direction,
  500. struct dma_attrs *attrs)
  501. {
  502. if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))
  503. dma_direct_ops.unmap_page(dev, dma_addr, size, direction,
  504. attrs);
  505. else
  506. iommu_unmap_page(cell_get_iommu_table(dev), dma_addr, size,
  507. direction, attrs);
  508. }
  509. static int dma_fixed_map_sg(struct device *dev, struct scatterlist *sg,
  510. int nents, enum dma_data_direction direction,
  511. struct dma_attrs *attrs)
  512. {
  513. if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))
  514. return dma_direct_ops.map_sg(dev, sg, nents, direction, attrs);
  515. else
  516. return iommu_map_sg(dev, cell_get_iommu_table(dev), sg, nents,
  517. device_to_mask(dev), direction, attrs);
  518. }
  519. static void dma_fixed_unmap_sg(struct device *dev, struct scatterlist *sg,
  520. int nents, enum dma_data_direction direction,
  521. struct dma_attrs *attrs)
  522. {
  523. if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))
  524. dma_direct_ops.unmap_sg(dev, sg, nents, direction, attrs);
  525. else
  526. iommu_unmap_sg(cell_get_iommu_table(dev), sg, nents, direction,
  527. attrs);
  528. }
  529. static int dma_fixed_dma_supported(struct device *dev, u64 mask)
  530. {
  531. return mask == DMA_64BIT_MASK;
  532. }
  533. static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask);
  534. struct dma_mapping_ops dma_iommu_fixed_ops = {
  535. .alloc_coherent = dma_fixed_alloc_coherent,
  536. .free_coherent = dma_fixed_free_coherent,
  537. .map_sg = dma_fixed_map_sg,
  538. .unmap_sg = dma_fixed_unmap_sg,
  539. .dma_supported = dma_fixed_dma_supported,
  540. .set_dma_mask = dma_set_mask_and_switch,
  541. .map_page = dma_fixed_map_page,
  542. .unmap_page = dma_fixed_unmap_page,
  543. };
  544. static void cell_dma_dev_setup_fixed(struct device *dev);
  545. static void cell_dma_dev_setup(struct device *dev)
  546. {
  547. struct dev_archdata *archdata = &dev->archdata;
  548. /* Order is important here, these are not mutually exclusive */
  549. if (get_dma_ops(dev) == &dma_iommu_fixed_ops)
  550. cell_dma_dev_setup_fixed(dev);
  551. else if (get_pci_dma_ops() == &dma_iommu_ops)
  552. archdata->dma_data = cell_get_iommu_table(dev);
  553. else if (get_pci_dma_ops() == &dma_direct_ops)
  554. archdata->dma_data = (void *)cell_dma_direct_offset;
  555. else
  556. BUG();
  557. }
  558. static void cell_pci_dma_dev_setup(struct pci_dev *dev)
  559. {
  560. cell_dma_dev_setup(&dev->dev);
  561. }
  562. static int cell_of_bus_notify(struct notifier_block *nb, unsigned long action,
  563. void *data)
  564. {
  565. struct device *dev = data;
  566. /* We are only intereted in device addition */
  567. if (action != BUS_NOTIFY_ADD_DEVICE)
  568. return 0;
  569. /* We use the PCI DMA ops */
  570. dev->archdata.dma_ops = get_pci_dma_ops();
  571. cell_dma_dev_setup(dev);
  572. return 0;
  573. }
  574. static struct notifier_block cell_of_bus_notifier = {
  575. .notifier_call = cell_of_bus_notify
  576. };
  577. static int __init cell_iommu_get_window(struct device_node *np,
  578. unsigned long *base,
  579. unsigned long *size)
  580. {
  581. const void *dma_window;
  582. unsigned long index;
  583. /* Use ibm,dma-window if available, else, hard code ! */
  584. dma_window = of_get_property(np, "ibm,dma-window", NULL);
  585. if (dma_window == NULL) {
  586. *base = 0;
  587. *size = 0x80000000u;
  588. return -ENODEV;
  589. }
  590. of_parse_dma_window(np, dma_window, &index, base, size);
  591. return 0;
  592. }
  593. static struct cbe_iommu * __init cell_iommu_alloc(struct device_node *np)
  594. {
  595. struct cbe_iommu *iommu;
  596. int nid, i;
  597. /* Get node ID */
  598. nid = of_node_to_nid(np);
  599. if (nid < 0) {
  600. printk(KERN_ERR "iommu: failed to get node for %s\n",
  601. np->full_name);
  602. return NULL;
  603. }
  604. pr_debug("iommu: setting up iommu for node %d (%s)\n",
  605. nid, np->full_name);
  606. /* XXX todo: If we can have multiple windows on the same IOMMU, which
  607. * isn't the case today, we probably want here to check wether the
  608. * iommu for that node is already setup.
  609. * However, there might be issue with getting the size right so let's
  610. * ignore that for now. We might want to completely get rid of the
  611. * multiple window support since the cell iommu supports per-page ioids
  612. */
  613. if (cbe_nr_iommus >= NR_IOMMUS) {
  614. printk(KERN_ERR "iommu: too many IOMMUs detected ! (%s)\n",
  615. np->full_name);
  616. return NULL;
  617. }
  618. /* Init base fields */
  619. i = cbe_nr_iommus++;
  620. iommu = &iommus[i];
  621. iommu->stab = NULL;
  622. iommu->nid = nid;
  623. snprintf(iommu->name, sizeof(iommu->name), "iommu%d", i);
  624. INIT_LIST_HEAD(&iommu->windows);
  625. return iommu;
  626. }
  627. static void __init cell_iommu_init_one(struct device_node *np,
  628. unsigned long offset)
  629. {
  630. struct cbe_iommu *iommu;
  631. unsigned long base, size;
  632. iommu = cell_iommu_alloc(np);
  633. if (!iommu)
  634. return;
  635. /* Obtain a window for it */
  636. cell_iommu_get_window(np, &base, &size);
  637. pr_debug("\ttranslating window 0x%lx...0x%lx\n",
  638. base, base + size - 1);
  639. /* Initialize the hardware */
  640. cell_iommu_setup_hardware(iommu, base, size);
  641. /* Setup the iommu_table */
  642. cell_iommu_setup_window(iommu, np, base, size,
  643. offset >> IOMMU_PAGE_SHIFT);
  644. }
  645. static void __init cell_disable_iommus(void)
  646. {
  647. int node;
  648. unsigned long base, val;
  649. void __iomem *xregs, *cregs;
  650. /* Make sure IOC translation is disabled on all nodes */
  651. for_each_online_node(node) {
  652. if (cell_iommu_find_ioc(node, &base))
  653. continue;
  654. xregs = ioremap(base, IOC_Reg_Size);
  655. if (xregs == NULL)
  656. continue;
  657. cregs = xregs + IOC_IOCmd_Offset;
  658. pr_debug("iommu: cleaning up iommu on node %d\n", node);
  659. out_be64(xregs + IOC_IOST_Origin, 0);
  660. (void)in_be64(xregs + IOC_IOST_Origin);
  661. val = in_be64(cregs + IOC_IOCmd_Cfg);
  662. val &= ~IOC_IOCmd_Cfg_TE;
  663. out_be64(cregs + IOC_IOCmd_Cfg, val);
  664. (void)in_be64(cregs + IOC_IOCmd_Cfg);
  665. iounmap(xregs);
  666. }
  667. }
  668. static int __init cell_iommu_init_disabled(void)
  669. {
  670. struct device_node *np = NULL;
  671. unsigned long base = 0, size;
  672. /* When no iommu is present, we use direct DMA ops */
  673. set_pci_dma_ops(&dma_direct_ops);
  674. /* First make sure all IOC translation is turned off */
  675. cell_disable_iommus();
  676. /* If we have no Axon, we set up the spider DMA magic offset */
  677. if (of_find_node_by_name(NULL, "axon") == NULL)
  678. cell_dma_direct_offset = SPIDER_DMA_OFFSET;
  679. /* Now we need to check to see where the memory is mapped
  680. * in PCI space. We assume that all busses use the same dma
  681. * window which is always the case so far on Cell, thus we
  682. * pick up the first pci-internal node we can find and check
  683. * the DMA window from there.
  684. */
  685. for_each_node_by_name(np, "axon") {
  686. if (np->parent == NULL || np->parent->parent != NULL)
  687. continue;
  688. if (cell_iommu_get_window(np, &base, &size) == 0)
  689. break;
  690. }
  691. if (np == NULL) {
  692. for_each_node_by_name(np, "pci-internal") {
  693. if (np->parent == NULL || np->parent->parent != NULL)
  694. continue;
  695. if (cell_iommu_get_window(np, &base, &size) == 0)
  696. break;
  697. }
  698. }
  699. of_node_put(np);
  700. /* If we found a DMA window, we check if it's big enough to enclose
  701. * all of physical memory. If not, we force enable IOMMU
  702. */
  703. if (np && size < lmb_end_of_DRAM()) {
  704. printk(KERN_WARNING "iommu: force-enabled, dma window"
  705. " (%ldMB) smaller than total memory (%ldMB)\n",
  706. size >> 20, lmb_end_of_DRAM() >> 20);
  707. return -ENODEV;
  708. }
  709. cell_dma_direct_offset += base;
  710. if (cell_dma_direct_offset != 0)
  711. ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
  712. printk("iommu: disabled, direct DMA offset is 0x%lx\n",
  713. cell_dma_direct_offset);
  714. return 0;
  715. }
  716. /*
  717. * Fixed IOMMU mapping support
  718. *
  719. * This code adds support for setting up a fixed IOMMU mapping on certain
  720. * cell machines. For 64-bit devices this avoids the performance overhead of
  721. * mapping and unmapping pages at runtime. 32-bit devices are unable to use
  722. * the fixed mapping.
  723. *
  724. * The fixed mapping is established at boot, and maps all of physical memory
  725. * 1:1 into device space at some offset. On machines with < 30 GB of memory
  726. * we setup the fixed mapping immediately above the normal IOMMU window.
  727. *
  728. * For example a machine with 4GB of memory would end up with the normal
  729. * IOMMU window from 0-2GB and the fixed mapping window from 2GB to 6GB. In
  730. * this case a 64-bit device wishing to DMA to 1GB would be told to DMA to
  731. * 3GB, plus any offset required by firmware. The firmware offset is encoded
  732. * in the "dma-ranges" property.
  733. *
  734. * On machines with 30GB or more of memory, we are unable to place the fixed
  735. * mapping above the normal IOMMU window as we would run out of address space.
  736. * Instead we move the normal IOMMU window to coincide with the hash page
  737. * table, this region does not need to be part of the fixed mapping as no
  738. * device should ever be DMA'ing to it. We then setup the fixed mapping
  739. * from 0 to 32GB.
  740. */
  741. static u64 cell_iommu_get_fixed_address(struct device *dev)
  742. {
  743. u64 cpu_addr, size, best_size, dev_addr = OF_BAD_ADDR;
  744. struct device_node *np;
  745. const u32 *ranges = NULL;
  746. int i, len, best, naddr, nsize, pna, range_size;
  747. np = of_node_get(dev->archdata.of_node);
  748. while (1) {
  749. naddr = of_n_addr_cells(np);
  750. nsize = of_n_size_cells(np);
  751. np = of_get_next_parent(np);
  752. if (!np)
  753. break;
  754. ranges = of_get_property(np, "dma-ranges", &len);
  755. /* Ignore empty ranges, they imply no translation required */
  756. if (ranges && len > 0)
  757. break;
  758. }
  759. if (!ranges) {
  760. dev_dbg(dev, "iommu: no dma-ranges found\n");
  761. goto out;
  762. }
  763. len /= sizeof(u32);
  764. pna = of_n_addr_cells(np);
  765. range_size = naddr + nsize + pna;
  766. /* dma-ranges format:
  767. * child addr : naddr cells
  768. * parent addr : pna cells
  769. * size : nsize cells
  770. */
  771. for (i = 0, best = -1, best_size = 0; i < len; i += range_size) {
  772. cpu_addr = of_translate_dma_address(np, ranges + i + naddr);
  773. size = of_read_number(ranges + i + naddr + pna, nsize);
  774. if (cpu_addr == 0 && size > best_size) {
  775. best = i;
  776. best_size = size;
  777. }
  778. }
  779. if (best >= 0) {
  780. dev_addr = of_read_number(ranges + best, naddr);
  781. } else
  782. dev_dbg(dev, "iommu: no suitable range found!\n");
  783. out:
  784. of_node_put(np);
  785. return dev_addr;
  786. }
  787. static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask)
  788. {
  789. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  790. return -EIO;
  791. if (dma_mask == DMA_BIT_MASK(64) &&
  792. cell_iommu_get_fixed_address(dev) != OF_BAD_ADDR)
  793. {
  794. dev_dbg(dev, "iommu: 64-bit OK, using fixed ops\n");
  795. set_dma_ops(dev, &dma_iommu_fixed_ops);
  796. } else {
  797. dev_dbg(dev, "iommu: not 64-bit, using default ops\n");
  798. set_dma_ops(dev, get_pci_dma_ops());
  799. }
  800. cell_dma_dev_setup(dev);
  801. *dev->dma_mask = dma_mask;
  802. return 0;
  803. }
  804. static void cell_dma_dev_setup_fixed(struct device *dev)
  805. {
  806. struct dev_archdata *archdata = &dev->archdata;
  807. u64 addr;
  808. addr = cell_iommu_get_fixed_address(dev) + dma_iommu_fixed_base;
  809. archdata->dma_data = (void *)addr;
  810. dev_dbg(dev, "iommu: fixed addr = %lx\n", addr);
  811. }
  812. static void insert_16M_pte(unsigned long addr, unsigned long *ptab,
  813. unsigned long base_pte)
  814. {
  815. unsigned long segment, offset;
  816. segment = addr >> IO_SEGMENT_SHIFT;
  817. offset = (addr >> 24) - (segment << IO_PAGENO_BITS(24));
  818. ptab = ptab + (segment * (1 << 12) / sizeof(unsigned long));
  819. pr_debug("iommu: addr %lx ptab %p segment %lx offset %lx\n",
  820. addr, ptab, segment, offset);
  821. ptab[offset] = base_pte | (__pa(addr) & IOPTE_RPN_Mask);
  822. }
  823. static void cell_iommu_setup_fixed_ptab(struct cbe_iommu *iommu,
  824. struct device_node *np, unsigned long dbase, unsigned long dsize,
  825. unsigned long fbase, unsigned long fsize)
  826. {
  827. unsigned long base_pte, uaddr, ioaddr, *ptab;
  828. ptab = cell_iommu_alloc_ptab(iommu, fbase, fsize, dbase, dsize, 24);
  829. dma_iommu_fixed_base = fbase;
  830. pr_debug("iommu: mapping 0x%lx pages from 0x%lx\n", fsize, fbase);
  831. base_pte = IOPTE_PP_W | IOPTE_PP_R | IOPTE_M
  832. | (cell_iommu_get_ioid(np) & IOPTE_IOID_Mask);
  833. if (iommu_fixed_is_weak)
  834. pr_info("IOMMU: Using weak ordering for fixed mapping\n");
  835. else {
  836. pr_info("IOMMU: Using strong ordering for fixed mapping\n");
  837. base_pte |= IOPTE_SO_RW;
  838. }
  839. for (uaddr = 0; uaddr < fsize; uaddr += (1 << 24)) {
  840. /* Don't touch the dynamic region */
  841. ioaddr = uaddr + fbase;
  842. if (ioaddr >= dbase && ioaddr < (dbase + dsize)) {
  843. pr_debug("iommu: fixed/dynamic overlap, skipping\n");
  844. continue;
  845. }
  846. insert_16M_pte(uaddr, ptab, base_pte);
  847. }
  848. mb();
  849. }
  850. static int __init cell_iommu_fixed_mapping_init(void)
  851. {
  852. unsigned long dbase, dsize, fbase, fsize, hbase, hend;
  853. struct cbe_iommu *iommu;
  854. struct device_node *np;
  855. /* The fixed mapping is only supported on axon machines */
  856. np = of_find_node_by_name(NULL, "axon");
  857. if (!np) {
  858. pr_debug("iommu: fixed mapping disabled, no axons found\n");
  859. return -1;
  860. }
  861. /* We must have dma-ranges properties for fixed mapping to work */
  862. for (np = NULL; (np = of_find_all_nodes(np));) {
  863. if (of_find_property(np, "dma-ranges", NULL))
  864. break;
  865. }
  866. of_node_put(np);
  867. if (!np) {
  868. pr_debug("iommu: no dma-ranges found, no fixed mapping\n");
  869. return -1;
  870. }
  871. /* The default setup is to have the fixed mapping sit after the
  872. * dynamic region, so find the top of the largest IOMMU window
  873. * on any axon, then add the size of RAM and that's our max value.
  874. * If that is > 32GB we have to do other shennanigans.
  875. */
  876. fbase = 0;
  877. for_each_node_by_name(np, "axon") {
  878. cell_iommu_get_window(np, &dbase, &dsize);
  879. fbase = max(fbase, dbase + dsize);
  880. }
  881. fbase = _ALIGN_UP(fbase, 1 << IO_SEGMENT_SHIFT);
  882. fsize = lmb_phys_mem_size();
  883. if ((fbase + fsize) <= 0x800000000)
  884. hbase = 0; /* use the device tree window */
  885. else {
  886. /* If we're over 32 GB we need to cheat. We can't map all of
  887. * RAM with the fixed mapping, and also fit the dynamic
  888. * region. So try to place the dynamic region where the hash
  889. * table sits, drivers never need to DMA to it, we don't
  890. * need a fixed mapping for that area.
  891. */
  892. if (!htab_address) {
  893. pr_debug("iommu: htab is NULL, on LPAR? Huh?\n");
  894. return -1;
  895. }
  896. hbase = __pa(htab_address);
  897. hend = hbase + htab_size_bytes;
  898. /* The window must start and end on a segment boundary */
  899. if ((hbase != _ALIGN_UP(hbase, 1 << IO_SEGMENT_SHIFT)) ||
  900. (hend != _ALIGN_UP(hend, 1 << IO_SEGMENT_SHIFT))) {
  901. pr_debug("iommu: hash window not segment aligned\n");
  902. return -1;
  903. }
  904. /* Check the hash window fits inside the real DMA window */
  905. for_each_node_by_name(np, "axon") {
  906. cell_iommu_get_window(np, &dbase, &dsize);
  907. if (hbase < dbase || (hend > (dbase + dsize))) {
  908. pr_debug("iommu: hash window doesn't fit in"
  909. "real DMA window\n");
  910. return -1;
  911. }
  912. }
  913. fbase = 0;
  914. }
  915. /* Setup the dynamic regions */
  916. for_each_node_by_name(np, "axon") {
  917. iommu = cell_iommu_alloc(np);
  918. BUG_ON(!iommu);
  919. if (hbase == 0)
  920. cell_iommu_get_window(np, &dbase, &dsize);
  921. else {
  922. dbase = hbase;
  923. dsize = htab_size_bytes;
  924. }
  925. printk(KERN_DEBUG "iommu: node %d, dynamic window 0x%lx-0x%lx "
  926. "fixed window 0x%lx-0x%lx\n", iommu->nid, dbase,
  927. dbase + dsize, fbase, fbase + fsize);
  928. cell_iommu_setup_stab(iommu, dbase, dsize, fbase, fsize);
  929. iommu->ptab = cell_iommu_alloc_ptab(iommu, dbase, dsize, 0, 0,
  930. IOMMU_PAGE_SHIFT);
  931. cell_iommu_setup_fixed_ptab(iommu, np, dbase, dsize,
  932. fbase, fsize);
  933. cell_iommu_enable_hardware(iommu);
  934. cell_iommu_setup_window(iommu, np, dbase, dsize, 0);
  935. }
  936. dma_iommu_ops.set_dma_mask = dma_set_mask_and_switch;
  937. set_pci_dma_ops(&dma_iommu_ops);
  938. return 0;
  939. }
  940. static int iommu_fixed_disabled;
  941. static int __init setup_iommu_fixed(char *str)
  942. {
  943. struct device_node *pciep;
  944. if (strcmp(str, "off") == 0)
  945. iommu_fixed_disabled = 1;
  946. /* If we can find a pcie-endpoint in the device tree assume that
  947. * we're on a triblade or a CAB so by default the fixed mapping
  948. * should be set to be weakly ordered; but only if the boot
  949. * option WASN'T set for strong ordering
  950. */
  951. pciep = of_find_node_by_type(NULL, "pcie-endpoint");
  952. if (strcmp(str, "weak") == 0 || (pciep && strcmp(str, "strong") != 0))
  953. iommu_fixed_is_weak = 1;
  954. of_node_put(pciep);
  955. return 1;
  956. }
  957. __setup("iommu_fixed=", setup_iommu_fixed);
  958. static int __init cell_iommu_init(void)
  959. {
  960. struct device_node *np;
  961. /* If IOMMU is disabled or we have little enough RAM to not need
  962. * to enable it, we setup a direct mapping.
  963. *
  964. * Note: should we make sure we have the IOMMU actually disabled ?
  965. */
  966. if (iommu_is_off ||
  967. (!iommu_force_on && lmb_end_of_DRAM() <= 0x80000000ull))
  968. if (cell_iommu_init_disabled() == 0)
  969. goto bail;
  970. /* Setup various ppc_md. callbacks */
  971. ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
  972. ppc_md.tce_build = tce_build_cell;
  973. ppc_md.tce_free = tce_free_cell;
  974. if (!iommu_fixed_disabled && cell_iommu_fixed_mapping_init() == 0)
  975. goto bail;
  976. /* Create an iommu for each /axon node. */
  977. for_each_node_by_name(np, "axon") {
  978. if (np->parent == NULL || np->parent->parent != NULL)
  979. continue;
  980. cell_iommu_init_one(np, 0);
  981. }
  982. /* Create an iommu for each toplevel /pci-internal node for
  983. * old hardware/firmware
  984. */
  985. for_each_node_by_name(np, "pci-internal") {
  986. if (np->parent == NULL || np->parent->parent != NULL)
  987. continue;
  988. cell_iommu_init_one(np, SPIDER_DMA_OFFSET);
  989. }
  990. /* Setup default PCI iommu ops */
  991. set_pci_dma_ops(&dma_iommu_ops);
  992. bail:
  993. /* Register callbacks on OF platform device addition/removal
  994. * to handle linking them to the right DMA operations
  995. */
  996. bus_register_notifier(&of_platform_bus_type, &cell_of_bus_notifier);
  997. return 0;
  998. }
  999. machine_arch_initcall(cell, cell_iommu_init);
  1000. machine_arch_initcall(celleb_native, cell_iommu_init);