iommu.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. unsigned long 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 appended
  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(attrs & DMA_ATTR_WEAK_ORDERING))
  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 boundary */
  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);
  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_table_ops cell_iommu_ops = {
  383. .set = tce_build_cell,
  384. .clear = tce_free_cell
  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 = kzalloc_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_page_shift = IOMMU_PAGE_SHIFT_4K;
  405. window->table.it_offset =
  406. (offset >> window->table.it_page_shift) + pte_offset;
  407. window->table.it_size = size >> window->table.it_page_shift;
  408. window->table.it_ops = &cell_iommu_ops;
  409. iommu_init_table(&window->table, iommu->nid);
  410. pr_debug("\tioid %d\n", window->ioid);
  411. pr_debug("\tblocksize %ld\n", window->table.it_blocksize);
  412. pr_debug("\tbase 0x%016lx\n", window->table.it_base);
  413. pr_debug("\toffset 0x%lx\n", window->table.it_offset);
  414. pr_debug("\tsize %ld\n", window->table.it_size);
  415. list_add(&window->list, &iommu->windows);
  416. if (offset != 0)
  417. return window;
  418. /* We need to map and reserve the first IOMMU page since it's used
  419. * by the spider workaround. In theory, we only need to do that when
  420. * running on spider but it doesn't really matter.
  421. *
  422. * This code also assumes that we have a window that starts at 0,
  423. * which is the case on all spider based blades.
  424. */
  425. page = alloc_pages_node(iommu->nid, GFP_KERNEL, 0);
  426. BUG_ON(!page);
  427. iommu->pad_page = page_address(page);
  428. clear_page(iommu->pad_page);
  429. __set_bit(0, window->table.it_map);
  430. tce_build_cell(&window->table, window->table.it_offset, 1,
  431. (unsigned long)iommu->pad_page, DMA_TO_DEVICE, 0);
  432. return window;
  433. }
  434. static struct cbe_iommu *cell_iommu_for_node(int nid)
  435. {
  436. int i;
  437. for (i = 0; i < cbe_nr_iommus; i++)
  438. if (iommus[i].nid == nid)
  439. return &iommus[i];
  440. return NULL;
  441. }
  442. static unsigned long cell_dma_direct_offset;
  443. static unsigned long dma_iommu_fixed_base;
  444. /* iommu_fixed_is_weak is set if booted with iommu_fixed=weak */
  445. static int iommu_fixed_is_weak;
  446. static struct iommu_table *cell_get_iommu_table(struct device *dev)
  447. {
  448. struct iommu_window *window;
  449. struct cbe_iommu *iommu;
  450. /* Current implementation uses the first window available in that
  451. * node's iommu. We -might- do something smarter later though it may
  452. * never be necessary
  453. */
  454. iommu = cell_iommu_for_node(dev_to_node(dev));
  455. if (iommu == NULL || list_empty(&iommu->windows)) {
  456. dev_err(dev, "iommu: missing iommu for %s (node %d)\n",
  457. of_node_full_name(dev->of_node), 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. unsigned long attrs)
  467. {
  468. if (iommu_fixed_is_weak)
  469. return iommu_alloc_coherent(dev, cell_get_iommu_table(dev),
  470. size, dma_handle,
  471. device_to_mask(dev), flag,
  472. dev_to_node(dev));
  473. else
  474. return dma_direct_ops.alloc(dev, size, dma_handle, flag,
  475. attrs);
  476. }
  477. static void dma_fixed_free_coherent(struct device *dev, size_t size,
  478. void *vaddr, dma_addr_t dma_handle,
  479. unsigned long attrs)
  480. {
  481. if (iommu_fixed_is_weak)
  482. iommu_free_coherent(cell_get_iommu_table(dev), size, vaddr,
  483. dma_handle);
  484. else
  485. dma_direct_ops.free(dev, size, vaddr, dma_handle, attrs);
  486. }
  487. static dma_addr_t dma_fixed_map_page(struct device *dev, struct page *page,
  488. unsigned long offset, size_t size,
  489. enum dma_data_direction direction,
  490. unsigned long attrs)
  491. {
  492. if (iommu_fixed_is_weak == (attrs & DMA_ATTR_WEAK_ORDERING))
  493. return dma_direct_ops.map_page(dev, page, offset, size,
  494. direction, attrs);
  495. else
  496. return iommu_map_page(dev, cell_get_iommu_table(dev), page,
  497. offset, size, device_to_mask(dev),
  498. direction, attrs);
  499. }
  500. static void dma_fixed_unmap_page(struct device *dev, dma_addr_t dma_addr,
  501. size_t size, enum dma_data_direction direction,
  502. unsigned long attrs)
  503. {
  504. if (iommu_fixed_is_weak == (attrs & DMA_ATTR_WEAK_ORDERING))
  505. dma_direct_ops.unmap_page(dev, dma_addr, size, direction,
  506. attrs);
  507. else
  508. iommu_unmap_page(cell_get_iommu_table(dev), dma_addr, size,
  509. direction, attrs);
  510. }
  511. static int dma_fixed_map_sg(struct device *dev, struct scatterlist *sg,
  512. int nents, enum dma_data_direction direction,
  513. unsigned long attrs)
  514. {
  515. if (iommu_fixed_is_weak == (attrs & DMA_ATTR_WEAK_ORDERING))
  516. return dma_direct_ops.map_sg(dev, sg, nents, direction, attrs);
  517. else
  518. return ppc_iommu_map_sg(dev, cell_get_iommu_table(dev), sg,
  519. nents, device_to_mask(dev),
  520. direction, attrs);
  521. }
  522. static void dma_fixed_unmap_sg(struct device *dev, struct scatterlist *sg,
  523. int nents, enum dma_data_direction direction,
  524. unsigned long attrs)
  525. {
  526. if (iommu_fixed_is_weak == (attrs & DMA_ATTR_WEAK_ORDERING))
  527. dma_direct_ops.unmap_sg(dev, sg, nents, direction, attrs);
  528. else
  529. ppc_iommu_unmap_sg(cell_get_iommu_table(dev), sg, nents,
  530. direction, attrs);
  531. }
  532. static int dma_fixed_dma_supported(struct device *dev, u64 mask)
  533. {
  534. return mask == DMA_BIT_MASK(64);
  535. }
  536. static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask);
  537. static struct dma_map_ops dma_iommu_fixed_ops = {
  538. .alloc = dma_fixed_alloc_coherent,
  539. .free = dma_fixed_free_coherent,
  540. .map_sg = dma_fixed_map_sg,
  541. .unmap_sg = dma_fixed_unmap_sg,
  542. .dma_supported = dma_fixed_dma_supported,
  543. .set_dma_mask = dma_set_mask_and_switch,
  544. .map_page = dma_fixed_map_page,
  545. .unmap_page = dma_fixed_unmap_page,
  546. };
  547. static void cell_dma_dev_setup_fixed(struct device *dev);
  548. static void cell_dma_dev_setup(struct device *dev)
  549. {
  550. /* Order is important here, these are not mutually exclusive */
  551. if (get_dma_ops(dev) == &dma_iommu_fixed_ops)
  552. cell_dma_dev_setup_fixed(dev);
  553. else if (get_pci_dma_ops() == &dma_iommu_ops)
  554. set_iommu_table_base(dev, cell_get_iommu_table(dev));
  555. else if (get_pci_dma_ops() == &dma_direct_ops)
  556. set_dma_offset(dev, cell_dma_direct_offset);
  557. else
  558. BUG();
  559. }
  560. static void cell_pci_dma_dev_setup(struct pci_dev *dev)
  561. {
  562. cell_dma_dev_setup(&dev->dev);
  563. }
  564. static int cell_of_bus_notify(struct notifier_block *nb, unsigned long action,
  565. void *data)
  566. {
  567. struct device *dev = data;
  568. /* We are only intereted in device addition */
  569. if (action != BUS_NOTIFY_ADD_DEVICE)
  570. return 0;
  571. /* We use the PCI DMA ops */
  572. dev->archdata.dma_ops = get_pci_dma_ops();
  573. cell_dma_dev_setup(dev);
  574. return 0;
  575. }
  576. static struct notifier_block cell_of_bus_notifier = {
  577. .notifier_call = cell_of_bus_notify
  578. };
  579. static int __init cell_iommu_get_window(struct device_node *np,
  580. unsigned long *base,
  581. unsigned long *size)
  582. {
  583. const __be32 *dma_window;
  584. unsigned long index;
  585. /* Use ibm,dma-window if available, else, hard code ! */
  586. dma_window = of_get_property(np, "ibm,dma-window", NULL);
  587. if (dma_window == NULL) {
  588. *base = 0;
  589. *size = 0x80000000u;
  590. return -ENODEV;
  591. }
  592. of_parse_dma_window(np, dma_window, &index, base, size);
  593. return 0;
  594. }
  595. static struct cbe_iommu * __init cell_iommu_alloc(struct device_node *np)
  596. {
  597. struct cbe_iommu *iommu;
  598. int nid, i;
  599. /* Get node ID */
  600. nid = of_node_to_nid(np);
  601. if (nid < 0) {
  602. printk(KERN_ERR "iommu: failed to get node for %s\n",
  603. np->full_name);
  604. return NULL;
  605. }
  606. pr_debug("iommu: setting up iommu for node %d (%s)\n",
  607. nid, np->full_name);
  608. /* XXX todo: If we can have multiple windows on the same IOMMU, which
  609. * isn't the case today, we probably want here to check whether the
  610. * iommu for that node is already setup.
  611. * However, there might be issue with getting the size right so let's
  612. * ignore that for now. We might want to completely get rid of the
  613. * multiple window support since the cell iommu supports per-page ioids
  614. */
  615. if (cbe_nr_iommus >= NR_IOMMUS) {
  616. printk(KERN_ERR "iommu: too many IOMMUs detected ! (%s)\n",
  617. np->full_name);
  618. return NULL;
  619. }
  620. /* Init base fields */
  621. i = cbe_nr_iommus++;
  622. iommu = &iommus[i];
  623. iommu->stab = NULL;
  624. iommu->nid = nid;
  625. snprintf(iommu->name, sizeof(iommu->name), "iommu%d", i);
  626. INIT_LIST_HEAD(&iommu->windows);
  627. return iommu;
  628. }
  629. static void __init cell_iommu_init_one(struct device_node *np,
  630. unsigned long offset)
  631. {
  632. struct cbe_iommu *iommu;
  633. unsigned long base, size;
  634. iommu = cell_iommu_alloc(np);
  635. if (!iommu)
  636. return;
  637. /* Obtain a window for it */
  638. cell_iommu_get_window(np, &base, &size);
  639. pr_debug("\ttranslating window 0x%lx...0x%lx\n",
  640. base, base + size - 1);
  641. /* Initialize the hardware */
  642. cell_iommu_setup_hardware(iommu, base, size);
  643. /* Setup the iommu_table */
  644. cell_iommu_setup_window(iommu, np, base, size,
  645. offset >> IOMMU_PAGE_SHIFT_4K);
  646. }
  647. static void __init cell_disable_iommus(void)
  648. {
  649. int node;
  650. unsigned long base, val;
  651. void __iomem *xregs, *cregs;
  652. /* Make sure IOC translation is disabled on all nodes */
  653. for_each_online_node(node) {
  654. if (cell_iommu_find_ioc(node, &base))
  655. continue;
  656. xregs = ioremap(base, IOC_Reg_Size);
  657. if (xregs == NULL)
  658. continue;
  659. cregs = xregs + IOC_IOCmd_Offset;
  660. pr_debug("iommu: cleaning up iommu on node %d\n", node);
  661. out_be64(xregs + IOC_IOST_Origin, 0);
  662. (void)in_be64(xregs + IOC_IOST_Origin);
  663. val = in_be64(cregs + IOC_IOCmd_Cfg);
  664. val &= ~IOC_IOCmd_Cfg_TE;
  665. out_be64(cregs + IOC_IOCmd_Cfg, val);
  666. (void)in_be64(cregs + IOC_IOCmd_Cfg);
  667. iounmap(xregs);
  668. }
  669. }
  670. static int __init cell_iommu_init_disabled(void)
  671. {
  672. struct device_node *np = NULL;
  673. unsigned long base = 0, size;
  674. /* When no iommu is present, we use direct DMA ops */
  675. set_pci_dma_ops(&dma_direct_ops);
  676. /* First make sure all IOC translation is turned off */
  677. cell_disable_iommus();
  678. /* If we have no Axon, we set up the spider DMA magic offset */
  679. if (of_find_node_by_name(NULL, "axon") == NULL)
  680. cell_dma_direct_offset = SPIDER_DMA_OFFSET;
  681. /* Now we need to check to see where the memory is mapped
  682. * in PCI space. We assume that all busses use the same dma
  683. * window which is always the case so far on Cell, thus we
  684. * pick up the first pci-internal node we can find and check
  685. * the DMA window from there.
  686. */
  687. for_each_node_by_name(np, "axon") {
  688. if (np->parent == NULL || np->parent->parent != NULL)
  689. continue;
  690. if (cell_iommu_get_window(np, &base, &size) == 0)
  691. break;
  692. }
  693. if (np == NULL) {
  694. for_each_node_by_name(np, "pci-internal") {
  695. if (np->parent == NULL || np->parent->parent != NULL)
  696. continue;
  697. if (cell_iommu_get_window(np, &base, &size) == 0)
  698. break;
  699. }
  700. }
  701. of_node_put(np);
  702. /* If we found a DMA window, we check if it's big enough to enclose
  703. * all of physical memory. If not, we force enable IOMMU
  704. */
  705. if (np && size < memblock_end_of_DRAM()) {
  706. printk(KERN_WARNING "iommu: force-enabled, dma window"
  707. " (%ldMB) smaller than total memory (%lldMB)\n",
  708. size >> 20, memblock_end_of_DRAM() >> 20);
  709. return -ENODEV;
  710. }
  711. cell_dma_direct_offset += base;
  712. if (cell_dma_direct_offset != 0)
  713. cell_pci_controller_ops.dma_dev_setup = cell_pci_dma_dev_setup;
  714. printk("iommu: disabled, direct DMA offset is 0x%lx\n",
  715. cell_dma_direct_offset);
  716. return 0;
  717. }
  718. /*
  719. * Fixed IOMMU mapping support
  720. *
  721. * This code adds support for setting up a fixed IOMMU mapping on certain
  722. * cell machines. For 64-bit devices this avoids the performance overhead of
  723. * mapping and unmapping pages at runtime. 32-bit devices are unable to use
  724. * the fixed mapping.
  725. *
  726. * The fixed mapping is established at boot, and maps all of physical memory
  727. * 1:1 into device space at some offset. On machines with < 30 GB of memory
  728. * we setup the fixed mapping immediately above the normal IOMMU window.
  729. *
  730. * For example a machine with 4GB of memory would end up with the normal
  731. * IOMMU window from 0-2GB and the fixed mapping window from 2GB to 6GB. In
  732. * this case a 64-bit device wishing to DMA to 1GB would be told to DMA to
  733. * 3GB, plus any offset required by firmware. The firmware offset is encoded
  734. * in the "dma-ranges" property.
  735. *
  736. * On machines with 30GB or more of memory, we are unable to place the fixed
  737. * mapping above the normal IOMMU window as we would run out of address space.
  738. * Instead we move the normal IOMMU window to coincide with the hash page
  739. * table, this region does not need to be part of the fixed mapping as no
  740. * device should ever be DMA'ing to it. We then setup the fixed mapping
  741. * from 0 to 32GB.
  742. */
  743. static u64 cell_iommu_get_fixed_address(struct device *dev)
  744. {
  745. u64 cpu_addr, size, best_size, dev_addr = OF_BAD_ADDR;
  746. struct device_node *np;
  747. const u32 *ranges = NULL;
  748. int i, len, best, naddr, nsize, pna, range_size;
  749. np = of_node_get(dev->of_node);
  750. while (1) {
  751. naddr = of_n_addr_cells(np);
  752. nsize = of_n_size_cells(np);
  753. np = of_get_next_parent(np);
  754. if (!np)
  755. break;
  756. ranges = of_get_property(np, "dma-ranges", &len);
  757. /* Ignore empty ranges, they imply no translation required */
  758. if (ranges && len > 0)
  759. break;
  760. }
  761. if (!ranges) {
  762. dev_dbg(dev, "iommu: no dma-ranges found\n");
  763. goto out;
  764. }
  765. len /= sizeof(u32);
  766. pna = of_n_addr_cells(np);
  767. range_size = naddr + nsize + pna;
  768. /* dma-ranges format:
  769. * child addr : naddr cells
  770. * parent addr : pna cells
  771. * size : nsize cells
  772. */
  773. for (i = 0, best = -1, best_size = 0; i < len; i += range_size) {
  774. cpu_addr = of_translate_dma_address(np, ranges + i + naddr);
  775. size = of_read_number(ranges + i + naddr + pna, nsize);
  776. if (cpu_addr == 0 && size > best_size) {
  777. best = i;
  778. best_size = size;
  779. }
  780. }
  781. if (best >= 0) {
  782. dev_addr = of_read_number(ranges + best, naddr);
  783. } else
  784. dev_dbg(dev, "iommu: no suitable range found!\n");
  785. out:
  786. of_node_put(np);
  787. return dev_addr;
  788. }
  789. static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask)
  790. {
  791. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  792. return -EIO;
  793. if (dma_mask == DMA_BIT_MASK(64) &&
  794. cell_iommu_get_fixed_address(dev) != OF_BAD_ADDR)
  795. {
  796. dev_dbg(dev, "iommu: 64-bit OK, using fixed ops\n");
  797. set_dma_ops(dev, &dma_iommu_fixed_ops);
  798. } else {
  799. dev_dbg(dev, "iommu: not 64-bit, using default ops\n");
  800. set_dma_ops(dev, get_pci_dma_ops());
  801. }
  802. cell_dma_dev_setup(dev);
  803. *dev->dma_mask = dma_mask;
  804. return 0;
  805. }
  806. static void cell_dma_dev_setup_fixed(struct device *dev)
  807. {
  808. u64 addr;
  809. addr = cell_iommu_get_fixed_address(dev) + dma_iommu_fixed_base;
  810. set_dma_offset(dev, addr);
  811. dev_dbg(dev, "iommu: fixed addr = %llx\n", addr);
  812. }
  813. static void insert_16M_pte(unsigned long addr, unsigned long *ptab,
  814. unsigned long base_pte)
  815. {
  816. unsigned long segment, offset;
  817. segment = addr >> IO_SEGMENT_SHIFT;
  818. offset = (addr >> 24) - (segment << IO_PAGENO_BITS(24));
  819. ptab = ptab + (segment * (1 << 12) / sizeof(unsigned long));
  820. pr_debug("iommu: addr %lx ptab %p segment %lx offset %lx\n",
  821. addr, ptab, segment, offset);
  822. ptab[offset] = base_pte | (__pa(addr) & CBE_IOPTE_RPN_Mask);
  823. }
  824. static void cell_iommu_setup_fixed_ptab(struct cbe_iommu *iommu,
  825. struct device_node *np, unsigned long dbase, unsigned long dsize,
  826. unsigned long fbase, unsigned long fsize)
  827. {
  828. unsigned long base_pte, uaddr, ioaddr, *ptab;
  829. ptab = cell_iommu_alloc_ptab(iommu, fbase, fsize, dbase, dsize, 24);
  830. dma_iommu_fixed_base = fbase;
  831. pr_debug("iommu: mapping 0x%lx pages from 0x%lx\n", fsize, fbase);
  832. base_pte = CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_M |
  833. (cell_iommu_get_ioid(np) & CBE_IOPTE_IOID_Mask);
  834. if (iommu_fixed_is_weak)
  835. pr_info("IOMMU: Using weak ordering for fixed mapping\n");
  836. else {
  837. pr_info("IOMMU: Using strong ordering for fixed mapping\n");
  838. base_pte |= CBE_IOPTE_SO_RW;
  839. }
  840. for (uaddr = 0; uaddr < fsize; uaddr += (1 << 24)) {
  841. /* Don't touch the dynamic region */
  842. ioaddr = uaddr + fbase;
  843. if (ioaddr >= dbase && ioaddr < (dbase + dsize)) {
  844. pr_debug("iommu: fixed/dynamic overlap, skipping\n");
  845. continue;
  846. }
  847. insert_16M_pte(uaddr, ptab, base_pte);
  848. }
  849. mb();
  850. }
  851. static int __init cell_iommu_fixed_mapping_init(void)
  852. {
  853. unsigned long dbase, dsize, fbase, fsize, hbase, hend;
  854. struct cbe_iommu *iommu;
  855. struct device_node *np;
  856. /* The fixed mapping is only supported on axon machines */
  857. np = of_find_node_by_name(NULL, "axon");
  858. of_node_put(np);
  859. if (!np) {
  860. pr_debug("iommu: fixed mapping disabled, no axons found\n");
  861. return -1;
  862. }
  863. /* We must have dma-ranges properties for fixed mapping to work */
  864. np = of_find_node_with_property(NULL, "dma-ranges");
  865. of_node_put(np);
  866. if (!np) {
  867. pr_debug("iommu: no dma-ranges found, no fixed mapping\n");
  868. return -1;
  869. }
  870. /* The default setup is to have the fixed mapping sit after the
  871. * dynamic region, so find the top of the largest IOMMU window
  872. * on any axon, then add the size of RAM and that's our max value.
  873. * If that is > 32GB we have to do other shennanigans.
  874. */
  875. fbase = 0;
  876. for_each_node_by_name(np, "axon") {
  877. cell_iommu_get_window(np, &dbase, &dsize);
  878. fbase = max(fbase, dbase + dsize);
  879. }
  880. fbase = _ALIGN_UP(fbase, 1 << IO_SEGMENT_SHIFT);
  881. fsize = memblock_phys_mem_size();
  882. if ((fbase + fsize) <= 0x800000000ul)
  883. hbase = 0; /* use the device tree window */
  884. else {
  885. /* If we're over 32 GB we need to cheat. We can't map all of
  886. * RAM with the fixed mapping, and also fit the dynamic
  887. * region. So try to place the dynamic region where the hash
  888. * table sits, drivers never need to DMA to it, we don't
  889. * need a fixed mapping for that area.
  890. */
  891. if (!htab_address) {
  892. pr_debug("iommu: htab is NULL, on LPAR? Huh?\n");
  893. return -1;
  894. }
  895. hbase = __pa(htab_address);
  896. hend = hbase + htab_size_bytes;
  897. /* The window must start and end on a segment boundary */
  898. if ((hbase != _ALIGN_UP(hbase, 1 << IO_SEGMENT_SHIFT)) ||
  899. (hend != _ALIGN_UP(hend, 1 << IO_SEGMENT_SHIFT))) {
  900. pr_debug("iommu: hash window not segment aligned\n");
  901. return -1;
  902. }
  903. /* Check the hash window fits inside the real DMA window */
  904. for_each_node_by_name(np, "axon") {
  905. cell_iommu_get_window(np, &dbase, &dsize);
  906. if (hbase < dbase || (hend > (dbase + dsize))) {
  907. pr_debug("iommu: hash window doesn't fit in"
  908. "real DMA window\n");
  909. return -1;
  910. }
  911. }
  912. fbase = 0;
  913. }
  914. /* Setup the dynamic regions */
  915. for_each_node_by_name(np, "axon") {
  916. iommu = cell_iommu_alloc(np);
  917. BUG_ON(!iommu);
  918. if (hbase == 0)
  919. cell_iommu_get_window(np, &dbase, &dsize);
  920. else {
  921. dbase = hbase;
  922. dsize = htab_size_bytes;
  923. }
  924. printk(KERN_DEBUG "iommu: node %d, dynamic window 0x%lx-0x%lx "
  925. "fixed window 0x%lx-0x%lx\n", iommu->nid, dbase,
  926. dbase + dsize, fbase, fbase + fsize);
  927. cell_iommu_setup_stab(iommu, dbase, dsize, fbase, fsize);
  928. iommu->ptab = cell_iommu_alloc_ptab(iommu, dbase, dsize, 0, 0,
  929. IOMMU_PAGE_SHIFT_4K);
  930. cell_iommu_setup_fixed_ptab(iommu, np, dbase, dsize,
  931. fbase, fsize);
  932. cell_iommu_enable_hardware(iommu);
  933. cell_iommu_setup_window(iommu, np, dbase, dsize, 0);
  934. }
  935. dma_iommu_ops.set_dma_mask = dma_set_mask_and_switch;
  936. set_pci_dma_ops(&dma_iommu_ops);
  937. return 0;
  938. }
  939. static int iommu_fixed_disabled;
  940. static int __init setup_iommu_fixed(char *str)
  941. {
  942. struct device_node *pciep;
  943. if (strcmp(str, "off") == 0)
  944. iommu_fixed_disabled = 1;
  945. /* If we can find a pcie-endpoint in the device tree assume that
  946. * we're on a triblade or a CAB so by default the fixed mapping
  947. * should be set to be weakly ordered; but only if the boot
  948. * option WASN'T set for strong ordering
  949. */
  950. pciep = of_find_node_by_type(NULL, "pcie-endpoint");
  951. if (strcmp(str, "weak") == 0 || (pciep && strcmp(str, "strong") != 0))
  952. iommu_fixed_is_weak = DMA_ATTR_WEAK_ORDERING;
  953. of_node_put(pciep);
  954. return 1;
  955. }
  956. __setup("iommu_fixed=", setup_iommu_fixed);
  957. static u64 cell_dma_get_required_mask(struct device *dev)
  958. {
  959. struct dma_map_ops *dma_ops;
  960. if (!dev->dma_mask)
  961. return 0;
  962. if (!iommu_fixed_disabled &&
  963. cell_iommu_get_fixed_address(dev) != OF_BAD_ADDR)
  964. return DMA_BIT_MASK(64);
  965. dma_ops = get_dma_ops(dev);
  966. if (dma_ops->get_required_mask)
  967. return dma_ops->get_required_mask(dev);
  968. WARN_ONCE(1, "no get_required_mask in %p ops", dma_ops);
  969. return DMA_BIT_MASK(64);
  970. }
  971. static int __init cell_iommu_init(void)
  972. {
  973. struct device_node *np;
  974. /* If IOMMU is disabled or we have little enough RAM to not need
  975. * to enable it, we setup a direct mapping.
  976. *
  977. * Note: should we make sure we have the IOMMU actually disabled ?
  978. */
  979. if (iommu_is_off ||
  980. (!iommu_force_on && memblock_end_of_DRAM() <= 0x80000000ull))
  981. if (cell_iommu_init_disabled() == 0)
  982. goto bail;
  983. /* Setup various callbacks */
  984. cell_pci_controller_ops.dma_dev_setup = cell_pci_dma_dev_setup;
  985. ppc_md.dma_get_required_mask = cell_dma_get_required_mask;
  986. if (!iommu_fixed_disabled && cell_iommu_fixed_mapping_init() == 0)
  987. goto bail;
  988. /* Create an iommu for each /axon node. */
  989. for_each_node_by_name(np, "axon") {
  990. if (np->parent == NULL || np->parent->parent != NULL)
  991. continue;
  992. cell_iommu_init_one(np, 0);
  993. }
  994. /* Create an iommu for each toplevel /pci-internal node for
  995. * old hardware/firmware
  996. */
  997. for_each_node_by_name(np, "pci-internal") {
  998. if (np->parent == NULL || np->parent->parent != NULL)
  999. continue;
  1000. cell_iommu_init_one(np, SPIDER_DMA_OFFSET);
  1001. }
  1002. /* Setup default PCI iommu ops */
  1003. set_pci_dma_ops(&dma_iommu_ops);
  1004. bail:
  1005. /* Register callbacks on OF platform device addition/removal
  1006. * to handle linking them to the right DMA operations
  1007. */
  1008. bus_register_notifier(&platform_bus_type, &cell_of_bus_notifier);
  1009. return 0;
  1010. }
  1011. machine_arch_initcall(cell, cell_iommu_init);