iommu.c 35 KB

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