iommu.c 34 KB

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