iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * IOMMU implementation for Cell Broadband Processor Architecture
  3. *
  4. * (C) Copyright IBM Corporation 2006
  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 <asm/prom.h>
  28. #include <asm/iommu.h>
  29. #include <asm/machdep.h>
  30. #include <asm/pci-bridge.h>
  31. #include <asm/udbg.h>
  32. #include <asm/of_platform.h>
  33. #include <asm/lmb.h>
  34. #include <asm/cell-regs.h>
  35. #include "interrupt.h"
  36. /* Define CELL_IOMMU_REAL_UNMAP to actually unmap non-used pages
  37. * instead of leaving them mapped to some dummy page. This can be
  38. * enabled once the appropriate workarounds for spider bugs have
  39. * been enabled
  40. */
  41. #define CELL_IOMMU_REAL_UNMAP
  42. /* Define CELL_IOMMU_STRICT_PROTECTION to enforce protection of
  43. * IO PTEs based on the transfer direction. That can be enabled
  44. * once spider-net has been fixed to pass the correct direction
  45. * to the DMA mapping functions
  46. */
  47. #define CELL_IOMMU_STRICT_PROTECTION
  48. #define NR_IOMMUS 2
  49. /* IOC mmap registers */
  50. #define IOC_Reg_Size 0x2000
  51. #define IOC_IOPT_CacheInvd 0x908
  52. #define IOC_IOPT_CacheInvd_NE_Mask 0xffe0000000000000ul
  53. #define IOC_IOPT_CacheInvd_IOPTE_Mask 0x000003fffffffff8ul
  54. #define IOC_IOPT_CacheInvd_Busy 0x0000000000000001ul
  55. #define IOC_IOST_Origin 0x918
  56. #define IOC_IOST_Origin_E 0x8000000000000000ul
  57. #define IOC_IOST_Origin_HW 0x0000000000000800ul
  58. #define IOC_IOST_Origin_HL 0x0000000000000400ul
  59. #define IOC_IO_ExcpStat 0x920
  60. #define IOC_IO_ExcpStat_V 0x8000000000000000ul
  61. #define IOC_IO_ExcpStat_SPF_Mask 0x6000000000000000ul
  62. #define IOC_IO_ExcpStat_SPF_S 0x6000000000000000ul
  63. #define IOC_IO_ExcpStat_SPF_P 0x4000000000000000ul
  64. #define IOC_IO_ExcpStat_ADDR_Mask 0x00000007fffff000ul
  65. #define IOC_IO_ExcpStat_RW_Mask 0x0000000000000800ul
  66. #define IOC_IO_ExcpStat_IOID_Mask 0x00000000000007fful
  67. #define IOC_IO_ExcpMask 0x928
  68. #define IOC_IO_ExcpMask_SFE 0x4000000000000000ul
  69. #define IOC_IO_ExcpMask_PFE 0x2000000000000000ul
  70. #define IOC_IOCmd_Offset 0x1000
  71. #define IOC_IOCmd_Cfg 0xc00
  72. #define IOC_IOCmd_Cfg_TE 0x0000800000000000ul
  73. /* Segment table entries */
  74. #define IOSTE_V 0x8000000000000000ul /* valid */
  75. #define IOSTE_H 0x4000000000000000ul /* cache hint */
  76. #define IOSTE_PT_Base_RPN_Mask 0x3ffffffffffff000ul /* base RPN of IOPT */
  77. #define IOSTE_NPPT_Mask 0x0000000000000fe0ul /* no. pages in IOPT */
  78. #define IOSTE_PS_Mask 0x0000000000000007ul /* page size */
  79. #define IOSTE_PS_4K 0x0000000000000001ul /* - 4kB */
  80. #define IOSTE_PS_64K 0x0000000000000003ul /* - 64kB */
  81. #define IOSTE_PS_1M 0x0000000000000005ul /* - 1MB */
  82. #define IOSTE_PS_16M 0x0000000000000007ul /* - 16MB */
  83. /* Page table entries */
  84. #define IOPTE_PP_W 0x8000000000000000ul /* protection: write */
  85. #define IOPTE_PP_R 0x4000000000000000ul /* protection: read */
  86. #define IOPTE_M 0x2000000000000000ul /* coherency required */
  87. #define IOPTE_SO_R 0x1000000000000000ul /* ordering: writes */
  88. #define IOPTE_SO_RW 0x1800000000000000ul /* ordering: r & w */
  89. #define IOPTE_RPN_Mask 0x07fffffffffff000ul /* RPN */
  90. #define IOPTE_H 0x0000000000000800ul /* cache hint */
  91. #define IOPTE_IOID_Mask 0x00000000000007fful /* ioid */
  92. /* IOMMU sizing */
  93. #define IO_SEGMENT_SHIFT 28
  94. #define IO_PAGENO_BITS (IO_SEGMENT_SHIFT - IOMMU_PAGE_SHIFT)
  95. /* The high bit needs to be set on every DMA address */
  96. #define SPIDER_DMA_OFFSET 0x80000000ul
  97. struct iommu_window {
  98. struct list_head list;
  99. struct cbe_iommu *iommu;
  100. unsigned long offset;
  101. unsigned long size;
  102. unsigned long pte_offset;
  103. unsigned int ioid;
  104. struct iommu_table table;
  105. };
  106. #define NAMESIZE 8
  107. struct cbe_iommu {
  108. int nid;
  109. char name[NAMESIZE];
  110. void __iomem *xlate_regs;
  111. void __iomem *cmd_regs;
  112. unsigned long *stab;
  113. unsigned long *ptab;
  114. void *pad_page;
  115. struct list_head windows;
  116. };
  117. /* Static array of iommus, one per node
  118. * each contains a list of windows, keyed from dma_window property
  119. * - on bus setup, look for a matching window, or create one
  120. * - on dev setup, assign iommu_table ptr
  121. */
  122. static struct cbe_iommu iommus[NR_IOMMUS];
  123. static int cbe_nr_iommus;
  124. static void invalidate_tce_cache(struct cbe_iommu *iommu, unsigned long *pte,
  125. long n_ptes)
  126. {
  127. unsigned long __iomem *reg;
  128. unsigned long val;
  129. long n;
  130. reg = iommu->xlate_regs + IOC_IOPT_CacheInvd;
  131. while (n_ptes > 0) {
  132. /* we can invalidate up to 1 << 11 PTEs at once */
  133. n = min(n_ptes, 1l << 11);
  134. val = (((n /*- 1*/) << 53) & IOC_IOPT_CacheInvd_NE_Mask)
  135. | (__pa(pte) & IOC_IOPT_CacheInvd_IOPTE_Mask)
  136. | IOC_IOPT_CacheInvd_Busy;
  137. out_be64(reg, val);
  138. while (in_be64(reg) & IOC_IOPT_CacheInvd_Busy)
  139. ;
  140. n_ptes -= n;
  141. pte += n;
  142. }
  143. }
  144. static void tce_build_cell(struct iommu_table *tbl, long index, long npages,
  145. unsigned long uaddr, enum dma_data_direction direction)
  146. {
  147. int i;
  148. unsigned long *io_pte, base_pte;
  149. struct iommu_window *window =
  150. container_of(tbl, struct iommu_window, table);
  151. /* implementing proper protection causes problems with the spidernet
  152. * driver - check mapping directions later, but allow read & write by
  153. * default for now.*/
  154. #ifdef CELL_IOMMU_STRICT_PROTECTION
  155. /* to avoid referencing a global, we use a trick here to setup the
  156. * protection bit. "prot" is setup to be 3 fields of 4 bits apprended
  157. * together for each of the 3 supported direction values. It is then
  158. * shifted left so that the fields matching the desired direction
  159. * lands on the appropriate bits, and other bits are masked out.
  160. */
  161. const unsigned long prot = 0xc48;
  162. base_pte =
  163. ((prot << (52 + 4 * direction)) & (IOPTE_PP_W | IOPTE_PP_R))
  164. | IOPTE_M | IOPTE_SO_RW | (window->ioid & IOPTE_IOID_Mask);
  165. #else
  166. base_pte = IOPTE_PP_W | IOPTE_PP_R | IOPTE_M | IOPTE_SO_RW |
  167. (window->ioid & IOPTE_IOID_Mask);
  168. #endif
  169. io_pte = (unsigned long *)tbl->it_base + (index - window->pte_offset);
  170. for (i = 0; i < npages; i++, uaddr += IOMMU_PAGE_SIZE)
  171. io_pte[i] = base_pte | (__pa(uaddr) & IOPTE_RPN_Mask);
  172. mb();
  173. invalidate_tce_cache(window->iommu, io_pte, npages);
  174. pr_debug("tce_build_cell(index=%lx,n=%lx,dir=%d,base_pte=%lx)\n",
  175. index, npages, direction, base_pte);
  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 = IOPTE_PP_R | IOPTE_M | IOPTE_SO_RW | __pa(window->iommu->pad_page)
  190. | (window->ioid & IOPTE_IOID_Mask);
  191. #endif
  192. io_pte = (unsigned long *)tbl->it_base + (index - window->pte_offset);
  193. for (i = 0; i < npages; i++)
  194. io_pte[i] = pte;
  195. mb();
  196. invalidate_tce_cache(window->iommu, io_pte, npages);
  197. }
  198. static irqreturn_t ioc_interrupt(int irq, void *data)
  199. {
  200. unsigned long stat;
  201. struct cbe_iommu *iommu = data;
  202. stat = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);
  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. (stat & IOC_IO_ExcpStat_SPF_S) ? 'S' : ' ',
  208. (stat & 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_hardware(struct cbe_iommu *iommu, unsigned long size)
  253. {
  254. struct page *page;
  255. int ret, i;
  256. unsigned long reg, segments, pages_per_segment, ptab_size, n_pte_pages;
  257. unsigned long xlate_base;
  258. unsigned int virq;
  259. if (cell_iommu_find_ioc(iommu->nid, &xlate_base))
  260. panic("%s: missing IOC register mappings for node %d\n",
  261. __FUNCTION__, iommu->nid);
  262. iommu->xlate_regs = ioremap(xlate_base, IOC_Reg_Size);
  263. iommu->cmd_regs = iommu->xlate_regs + IOC_IOCmd_Offset;
  264. segments = size >> IO_SEGMENT_SHIFT;
  265. pages_per_segment = 1ull << IO_PAGENO_BITS;
  266. pr_debug("%s: iommu[%d]: segments: %lu, pages per segment: %lu\n",
  267. __FUNCTION__, iommu->nid, segments, pages_per_segment);
  268. /* set up the segment table */
  269. page = alloc_pages_node(iommu->nid, GFP_KERNEL, 0);
  270. BUG_ON(!page);
  271. iommu->stab = page_address(page);
  272. clear_page(iommu->stab);
  273. /* ... and the page tables. Since these are contiguous, we can treat
  274. * the page tables as one array of ptes, like pSeries does.
  275. */
  276. ptab_size = segments * pages_per_segment * sizeof(unsigned long);
  277. pr_debug("%s: iommu[%d]: ptab_size: %lu, order: %d\n", __FUNCTION__,
  278. iommu->nid, ptab_size, get_order(ptab_size));
  279. page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(ptab_size));
  280. BUG_ON(!page);
  281. iommu->ptab = page_address(page);
  282. memset(iommu->ptab, 0, ptab_size);
  283. /* allocate a bogus page for the end of each mapping */
  284. page = alloc_pages_node(iommu->nid, GFP_KERNEL, 0);
  285. BUG_ON(!page);
  286. iommu->pad_page = page_address(page);
  287. clear_page(iommu->pad_page);
  288. /* number of pages needed for a page table */
  289. n_pte_pages = (pages_per_segment *
  290. sizeof(unsigned long)) >> IOMMU_PAGE_SHIFT;
  291. pr_debug("%s: iommu[%d]: stab at %p, ptab at %p, n_pte_pages: %lu\n",
  292. __FUNCTION__, iommu->nid, iommu->stab, iommu->ptab,
  293. n_pte_pages);
  294. /* initialise the STEs */
  295. reg = IOSTE_V | ((n_pte_pages - 1) << 5);
  296. if (IOMMU_PAGE_SIZE == 0x1000)
  297. reg |= IOSTE_PS_4K;
  298. else if (IOMMU_PAGE_SIZE == 0x10000)
  299. reg |= IOSTE_PS_64K;
  300. else {
  301. extern void __unknown_page_size_error(void);
  302. __unknown_page_size_error();
  303. }
  304. pr_debug("Setting up IOMMU stab:\n");
  305. for (i = 0; i * (1ul << IO_SEGMENT_SHIFT) < size; i++) {
  306. iommu->stab[i] = reg |
  307. (__pa(iommu->ptab) + n_pte_pages * IOMMU_PAGE_SIZE * i);
  308. pr_debug("\t[%d] 0x%016lx\n", i, iommu->stab[i]);
  309. }
  310. /* ensure that the STEs have updated */
  311. mb();
  312. /* setup interrupts for the iommu. */
  313. reg = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);
  314. out_be64(iommu->xlate_regs + IOC_IO_ExcpStat,
  315. reg & ~IOC_IO_ExcpStat_V);
  316. out_be64(iommu->xlate_regs + IOC_IO_ExcpMask,
  317. IOC_IO_ExcpMask_PFE | IOC_IO_ExcpMask_SFE);
  318. virq = irq_create_mapping(NULL,
  319. IIC_IRQ_IOEX_ATI | (iommu->nid << IIC_IRQ_NODE_SHIFT));
  320. BUG_ON(virq == NO_IRQ);
  321. ret = request_irq(virq, ioc_interrupt, IRQF_DISABLED,
  322. iommu->name, iommu);
  323. BUG_ON(ret);
  324. /* set the IOC segment table origin register (and turn on the iommu) */
  325. reg = IOC_IOST_Origin_E | __pa(iommu->stab) | IOC_IOST_Origin_HW;
  326. out_be64(iommu->xlate_regs + IOC_IOST_Origin, reg);
  327. in_be64(iommu->xlate_regs + IOC_IOST_Origin);
  328. /* turn on IO translation */
  329. reg = in_be64(iommu->cmd_regs + IOC_IOCmd_Cfg) | IOC_IOCmd_Cfg_TE;
  330. out_be64(iommu->cmd_regs + IOC_IOCmd_Cfg, reg);
  331. }
  332. #if 0/* Unused for now */
  333. static struct iommu_window *find_window(struct cbe_iommu *iommu,
  334. unsigned long offset, unsigned long size)
  335. {
  336. struct iommu_window *window;
  337. /* todo: check for overlapping (but not equal) windows) */
  338. list_for_each_entry(window, &(iommu->windows), list) {
  339. if (window->offset == offset && window->size == size)
  340. return window;
  341. }
  342. return NULL;
  343. }
  344. #endif
  345. static struct iommu_window * __init
  346. cell_iommu_setup_window(struct cbe_iommu *iommu, struct device_node *np,
  347. unsigned long offset, unsigned long size,
  348. unsigned long pte_offset)
  349. {
  350. struct iommu_window *window;
  351. const unsigned int *ioid;
  352. ioid = of_get_property(np, "ioid", NULL);
  353. if (ioid == NULL)
  354. printk(KERN_WARNING "iommu: missing ioid for %s using 0\n",
  355. np->full_name);
  356. window = kmalloc_node(sizeof(*window), GFP_KERNEL, iommu->nid);
  357. BUG_ON(window == NULL);
  358. window->offset = offset;
  359. window->size = size;
  360. window->ioid = ioid ? *ioid : 0;
  361. window->iommu = iommu;
  362. window->pte_offset = pte_offset;
  363. window->table.it_blocksize = 16;
  364. window->table.it_base = (unsigned long)iommu->ptab;
  365. window->table.it_index = iommu->nid;
  366. window->table.it_offset = (offset >> IOMMU_PAGE_SHIFT) +
  367. window->pte_offset;
  368. window->table.it_size = size >> IOMMU_PAGE_SHIFT;
  369. iommu_init_table(&window->table, iommu->nid);
  370. pr_debug("\tioid %d\n", window->ioid);
  371. pr_debug("\tblocksize %ld\n", window->table.it_blocksize);
  372. pr_debug("\tbase 0x%016lx\n", window->table.it_base);
  373. pr_debug("\toffset 0x%lx\n", window->table.it_offset);
  374. pr_debug("\tsize %ld\n", window->table.it_size);
  375. list_add(&window->list, &iommu->windows);
  376. if (offset != 0)
  377. return window;
  378. /* We need to map and reserve the first IOMMU page since it's used
  379. * by the spider workaround. In theory, we only need to do that when
  380. * running on spider but it doesn't really matter.
  381. *
  382. * This code also assumes that we have a window that starts at 0,
  383. * which is the case on all spider based blades.
  384. */
  385. __set_bit(0, window->table.it_map);
  386. tce_build_cell(&window->table, window->table.it_offset, 1,
  387. (unsigned long)iommu->pad_page, DMA_TO_DEVICE);
  388. window->table.it_hint = window->table.it_blocksize;
  389. return window;
  390. }
  391. static struct cbe_iommu *cell_iommu_for_node(int nid)
  392. {
  393. int i;
  394. for (i = 0; i < cbe_nr_iommus; i++)
  395. if (iommus[i].nid == nid)
  396. return &iommus[i];
  397. return NULL;
  398. }
  399. static void cell_dma_dev_setup(struct device *dev)
  400. {
  401. struct iommu_window *window;
  402. struct cbe_iommu *iommu;
  403. struct dev_archdata *archdata = &dev->archdata;
  404. /* If we run without iommu, no need to do anything */
  405. if (get_pci_dma_ops() == &dma_direct_ops)
  406. return;
  407. /* Current implementation uses the first window available in that
  408. * node's iommu. We -might- do something smarter later though it may
  409. * never be necessary
  410. */
  411. iommu = cell_iommu_for_node(archdata->numa_node);
  412. if (iommu == NULL || list_empty(&iommu->windows)) {
  413. printk(KERN_ERR "iommu: missing iommu for %s (node %d)\n",
  414. archdata->of_node ? archdata->of_node->full_name : "?",
  415. archdata->numa_node);
  416. return;
  417. }
  418. window = list_entry(iommu->windows.next, struct iommu_window, list);
  419. archdata->dma_data = &window->table;
  420. }
  421. static void cell_pci_dma_dev_setup(struct pci_dev *dev)
  422. {
  423. cell_dma_dev_setup(&dev->dev);
  424. }
  425. static int cell_of_bus_notify(struct notifier_block *nb, unsigned long action,
  426. void *data)
  427. {
  428. struct device *dev = data;
  429. /* We are only intereted in device addition */
  430. if (action != BUS_NOTIFY_ADD_DEVICE)
  431. return 0;
  432. /* We use the PCI DMA ops */
  433. dev->archdata.dma_ops = get_pci_dma_ops();
  434. cell_dma_dev_setup(dev);
  435. return 0;
  436. }
  437. static struct notifier_block cell_of_bus_notifier = {
  438. .notifier_call = cell_of_bus_notify
  439. };
  440. static int __init cell_iommu_get_window(struct device_node *np,
  441. unsigned long *base,
  442. unsigned long *size)
  443. {
  444. const void *dma_window;
  445. unsigned long index;
  446. /* Use ibm,dma-window if available, else, hard code ! */
  447. dma_window = of_get_property(np, "ibm,dma-window", NULL);
  448. if (dma_window == NULL) {
  449. *base = 0;
  450. *size = 0x80000000u;
  451. return -ENODEV;
  452. }
  453. of_parse_dma_window(np, dma_window, &index, base, size);
  454. return 0;
  455. }
  456. static void __init cell_iommu_init_one(struct device_node *np, unsigned long offset)
  457. {
  458. struct cbe_iommu *iommu;
  459. unsigned long base, size;
  460. int nid, i;
  461. /* Get node ID */
  462. nid = of_node_to_nid(np);
  463. if (nid < 0) {
  464. printk(KERN_ERR "iommu: failed to get node for %s\n",
  465. np->full_name);
  466. return;
  467. }
  468. pr_debug("iommu: setting up iommu for node %d (%s)\n",
  469. nid, np->full_name);
  470. /* XXX todo: If we can have multiple windows on the same IOMMU, which
  471. * isn't the case today, we probably want here to check wether the
  472. * iommu for that node is already setup.
  473. * However, there might be issue with getting the size right so let's
  474. * ignore that for now. We might want to completely get rid of the
  475. * multiple window support since the cell iommu supports per-page ioids
  476. */
  477. if (cbe_nr_iommus >= NR_IOMMUS) {
  478. printk(KERN_ERR "iommu: too many IOMMUs detected ! (%s)\n",
  479. np->full_name);
  480. return;
  481. }
  482. /* Init base fields */
  483. i = cbe_nr_iommus++;
  484. iommu = &iommus[i];
  485. iommu->stab = NULL;
  486. iommu->nid = nid;
  487. snprintf(iommu->name, sizeof(iommu->name), "iommu%d", i);
  488. INIT_LIST_HEAD(&iommu->windows);
  489. /* Obtain a window for it */
  490. cell_iommu_get_window(np, &base, &size);
  491. pr_debug("\ttranslating window 0x%lx...0x%lx\n",
  492. base, base + size - 1);
  493. /* Initialize the hardware */
  494. cell_iommu_setup_hardware(iommu, size);
  495. /* Setup the iommu_table */
  496. cell_iommu_setup_window(iommu, np, base, size,
  497. offset >> IOMMU_PAGE_SHIFT);
  498. }
  499. static void __init cell_disable_iommus(void)
  500. {
  501. int node;
  502. unsigned long base, val;
  503. void __iomem *xregs, *cregs;
  504. /* Make sure IOC translation is disabled on all nodes */
  505. for_each_online_node(node) {
  506. if (cell_iommu_find_ioc(node, &base))
  507. continue;
  508. xregs = ioremap(base, IOC_Reg_Size);
  509. if (xregs == NULL)
  510. continue;
  511. cregs = xregs + IOC_IOCmd_Offset;
  512. pr_debug("iommu: cleaning up iommu on node %d\n", node);
  513. out_be64(xregs + IOC_IOST_Origin, 0);
  514. (void)in_be64(xregs + IOC_IOST_Origin);
  515. val = in_be64(cregs + IOC_IOCmd_Cfg);
  516. val &= ~IOC_IOCmd_Cfg_TE;
  517. out_be64(cregs + IOC_IOCmd_Cfg, val);
  518. (void)in_be64(cregs + IOC_IOCmd_Cfg);
  519. iounmap(xregs);
  520. }
  521. }
  522. static int __init cell_iommu_init_disabled(void)
  523. {
  524. struct device_node *np = NULL;
  525. unsigned long base = 0, size;
  526. /* When no iommu is present, we use direct DMA ops */
  527. set_pci_dma_ops(&dma_direct_ops);
  528. /* First make sure all IOC translation is turned off */
  529. cell_disable_iommus();
  530. /* If we have no Axon, we set up the spider DMA magic offset */
  531. if (of_find_node_by_name(NULL, "axon") == NULL)
  532. dma_direct_offset = SPIDER_DMA_OFFSET;
  533. /* Now we need to check to see where the memory is mapped
  534. * in PCI space. We assume that all busses use the same dma
  535. * window which is always the case so far on Cell, thus we
  536. * pick up the first pci-internal node we can find and check
  537. * the DMA window from there.
  538. */
  539. for_each_node_by_name(np, "axon") {
  540. if (np->parent == NULL || np->parent->parent != NULL)
  541. continue;
  542. if (cell_iommu_get_window(np, &base, &size) == 0)
  543. break;
  544. }
  545. if (np == NULL) {
  546. for_each_node_by_name(np, "pci-internal") {
  547. if (np->parent == NULL || np->parent->parent != NULL)
  548. continue;
  549. if (cell_iommu_get_window(np, &base, &size) == 0)
  550. break;
  551. }
  552. }
  553. of_node_put(np);
  554. /* If we found a DMA window, we check if it's big enough to enclose
  555. * all of physical memory. If not, we force enable IOMMU
  556. */
  557. if (np && size < lmb_end_of_DRAM()) {
  558. printk(KERN_WARNING "iommu: force-enabled, dma window"
  559. " (%ldMB) smaller than total memory (%ldMB)\n",
  560. size >> 20, lmb_end_of_DRAM() >> 20);
  561. return -ENODEV;
  562. }
  563. dma_direct_offset += base;
  564. printk("iommu: disabled, direct DMA offset is 0x%lx\n",
  565. dma_direct_offset);
  566. return 0;
  567. }
  568. static int __init cell_iommu_init(void)
  569. {
  570. struct device_node *np;
  571. if (!machine_is(cell))
  572. return -ENODEV;
  573. /* If IOMMU is disabled or we have little enough RAM to not need
  574. * to enable it, we setup a direct mapping.
  575. *
  576. * Note: should we make sure we have the IOMMU actually disabled ?
  577. */
  578. if (iommu_is_off ||
  579. (!iommu_force_on && lmb_end_of_DRAM() <= 0x80000000ull))
  580. if (cell_iommu_init_disabled() == 0)
  581. goto bail;
  582. /* Setup various ppc_md. callbacks */
  583. ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
  584. ppc_md.tce_build = tce_build_cell;
  585. ppc_md.tce_free = tce_free_cell;
  586. /* Create an iommu for each /axon node. */
  587. for_each_node_by_name(np, "axon") {
  588. if (np->parent == NULL || np->parent->parent != NULL)
  589. continue;
  590. cell_iommu_init_one(np, 0);
  591. }
  592. /* Create an iommu for each toplevel /pci-internal node for
  593. * old hardware/firmware
  594. */
  595. for_each_node_by_name(np, "pci-internal") {
  596. if (np->parent == NULL || np->parent->parent != NULL)
  597. continue;
  598. cell_iommu_init_one(np, SPIDER_DMA_OFFSET);
  599. }
  600. /* Setup default PCI iommu ops */
  601. set_pci_dma_ops(&dma_iommu_ops);
  602. bail:
  603. /* Register callbacks on OF platform device addition/removal
  604. * to handle linking them to the right DMA operations
  605. */
  606. bus_register_notifier(&of_platform_bus_type, &cell_of_bus_notifier);
  607. return 0;
  608. }
  609. arch_initcall(cell_iommu_init);