iommu.c 22 KB

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