iommu.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*
  2. * IOMMU implementation for Cell Broadband Processor Architecture
  3. *
  4. * (C) Copyright IBM Corporation 2006-2008
  5. *
  6. * Author: Jeremy Kerr <jk@ozlabs.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #undef DEBUG
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/notifier.h>
  27. #include <linux/of.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/lmb.h>
  30. #include <asm/prom.h>
  31. #include <asm/iommu.h>
  32. #include <asm/machdep.h>
  33. #include <asm/pci-bridge.h>
  34. #include <asm/udbg.h>
  35. #include <asm/firmware.h>
  36. #include <asm/cell-regs.h>
  37. #include "interrupt.h"
  38. /* Define CELL_IOMMU_REAL_UNMAP to actually unmap non-used pages
  39. * instead of leaving them mapped to some dummy page. This can be
  40. * enabled once the appropriate workarounds for spider bugs have
  41. * been enabled
  42. */
  43. #define CELL_IOMMU_REAL_UNMAP
  44. /* Define CELL_IOMMU_STRICT_PROTECTION to enforce protection of
  45. * IO PTEs based on the transfer direction. That can be enabled
  46. * once spider-net has been fixed to pass the correct direction
  47. * to the DMA mapping functions
  48. */
  49. #define CELL_IOMMU_STRICT_PROTECTION
  50. #define NR_IOMMUS 2
  51. /* IOC mmap registers */
  52. #define IOC_Reg_Size 0x2000
  53. #define IOC_IOPT_CacheInvd 0x908
  54. #define IOC_IOPT_CacheInvd_NE_Mask 0xffe0000000000000ul
  55. #define IOC_IOPT_CacheInvd_IOPTE_Mask 0x000003fffffffff8ul
  56. #define IOC_IOPT_CacheInvd_Busy 0x0000000000000001ul
  57. #define IOC_IOST_Origin 0x918
  58. #define IOC_IOST_Origin_E 0x8000000000000000ul
  59. #define IOC_IOST_Origin_HW 0x0000000000000800ul
  60. #define IOC_IOST_Origin_HL 0x0000000000000400ul
  61. #define IOC_IO_ExcpStat 0x920
  62. #define IOC_IO_ExcpStat_V 0x8000000000000000ul
  63. #define IOC_IO_ExcpStat_SPF_Mask 0x6000000000000000ul
  64. #define IOC_IO_ExcpStat_SPF_S 0x6000000000000000ul
  65. #define IOC_IO_ExcpStat_SPF_P 0x4000000000000000ul
  66. #define IOC_IO_ExcpStat_ADDR_Mask 0x00000007fffff000ul
  67. #define IOC_IO_ExcpStat_RW_Mask 0x0000000000000800ul
  68. #define IOC_IO_ExcpStat_IOID_Mask 0x00000000000007fful
  69. #define IOC_IO_ExcpMask 0x928
  70. #define IOC_IO_ExcpMask_SFE 0x4000000000000000ul
  71. #define IOC_IO_ExcpMask_PFE 0x2000000000000000ul
  72. #define IOC_IOCmd_Offset 0x1000
  73. #define IOC_IOCmd_Cfg 0xc00
  74. #define IOC_IOCmd_Cfg_TE 0x0000800000000000ul
  75. /* Segment table entries */
  76. #define IOSTE_V 0x8000000000000000ul /* valid */
  77. #define IOSTE_H 0x4000000000000000ul /* cache hint */
  78. #define IOSTE_PT_Base_RPN_Mask 0x3ffffffffffff000ul /* base RPN of IOPT */
  79. #define IOSTE_NPPT_Mask 0x0000000000000fe0ul /* no. pages in IOPT */
  80. #define IOSTE_PS_Mask 0x0000000000000007ul /* page size */
  81. #define IOSTE_PS_4K 0x0000000000000001ul /* - 4kB */
  82. #define IOSTE_PS_64K 0x0000000000000003ul /* - 64kB */
  83. #define IOSTE_PS_1M 0x0000000000000005ul /* - 1MB */
  84. #define IOSTE_PS_16M 0x0000000000000007ul /* - 16MB */
  85. /* Page table entries */
  86. #define IOPTE_PP_W 0x8000000000000000ul /* protection: write */
  87. #define IOPTE_PP_R 0x4000000000000000ul /* protection: read */
  88. #define IOPTE_M 0x2000000000000000ul /* coherency required */
  89. #define IOPTE_SO_R 0x1000000000000000ul /* ordering: writes */
  90. #define IOPTE_SO_RW 0x1800000000000000ul /* ordering: r & w */
  91. #define IOPTE_RPN_Mask 0x07fffffffffff000ul /* RPN */
  92. #define IOPTE_H 0x0000000000000800ul /* cache hint */
  93. #define IOPTE_IOID_Mask 0x00000000000007fful /* ioid */
  94. /* IOMMU sizing */
  95. #define IO_SEGMENT_SHIFT 28
  96. #define IO_PAGENO_BITS(shift) (IO_SEGMENT_SHIFT - (shift))
  97. /* The high bit needs to be set on every DMA address */
  98. #define SPIDER_DMA_OFFSET 0x80000000ul
  99. struct iommu_window {
  100. struct list_head list;
  101. struct cbe_iommu *iommu;
  102. unsigned long offset;
  103. unsigned long size;
  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 - tbl->it_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 - 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;
  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_stab(struct cbe_iommu *iommu,
  254. unsigned long dbase, unsigned long dsize,
  255. unsigned long fbase, unsigned long fsize)
  256. {
  257. struct page *page;
  258. unsigned long segments, stab_size;
  259. segments = max(dbase + dsize, fbase + fsize) >> IO_SEGMENT_SHIFT;
  260. pr_debug("%s: iommu[%d]: segments: %lu\n",
  261. __func__, iommu->nid, segments);
  262. /* set up the segment table */
  263. stab_size = segments * sizeof(unsigned long);
  264. page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(stab_size));
  265. BUG_ON(!page);
  266. iommu->stab = page_address(page);
  267. memset(iommu->stab, 0, stab_size);
  268. }
  269. static unsigned long *cell_iommu_alloc_ptab(struct cbe_iommu *iommu,
  270. unsigned long base, unsigned long size, unsigned long gap_base,
  271. unsigned long gap_size, unsigned long page_shift)
  272. {
  273. struct page *page;
  274. int i;
  275. unsigned long reg, segments, pages_per_segment, ptab_size,
  276. n_pte_pages, start_seg, *ptab;
  277. start_seg = base >> IO_SEGMENT_SHIFT;
  278. segments = size >> IO_SEGMENT_SHIFT;
  279. pages_per_segment = 1ull << IO_PAGENO_BITS(page_shift);
  280. /* PTEs for each segment must start on a 4K bounday */
  281. pages_per_segment = max(pages_per_segment,
  282. (1 << 12) / sizeof(unsigned long));
  283. ptab_size = segments * pages_per_segment * sizeof(unsigned long);
  284. pr_debug("%s: iommu[%d]: ptab_size: %lu, order: %d\n", __func__,
  285. iommu->nid, ptab_size, get_order(ptab_size));
  286. page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(ptab_size));
  287. BUG_ON(!page);
  288. ptab = page_address(page);
  289. memset(ptab, 0, ptab_size);
  290. /* number of 4K pages needed for a page table */
  291. n_pte_pages = (pages_per_segment * sizeof(unsigned long)) >> 12;
  292. pr_debug("%s: iommu[%d]: stab at %p, ptab at %p, n_pte_pages: %lu\n",
  293. __func__, iommu->nid, iommu->stab, ptab,
  294. n_pte_pages);
  295. /* initialise the STEs */
  296. reg = IOSTE_V | ((n_pte_pages - 1) << 5);
  297. switch (page_shift) {
  298. case 12: reg |= IOSTE_PS_4K; break;
  299. case 16: reg |= IOSTE_PS_64K; break;
  300. case 20: reg |= IOSTE_PS_1M; break;
  301. case 24: reg |= IOSTE_PS_16M; break;
  302. default: BUG();
  303. }
  304. gap_base = gap_base >> IO_SEGMENT_SHIFT;
  305. gap_size = gap_size >> IO_SEGMENT_SHIFT;
  306. pr_debug("Setting up IOMMU stab:\n");
  307. for (i = start_seg; i < (start_seg + segments); i++) {
  308. if (i >= gap_base && i < (gap_base + gap_size)) {
  309. pr_debug("\toverlap at %d, skipping\n", i);
  310. continue;
  311. }
  312. iommu->stab[i] = reg | (__pa(ptab) + (n_pte_pages << 12) *
  313. (i - start_seg));
  314. pr_debug("\t[%d] 0x%016lx\n", i, iommu->stab[i]);
  315. }
  316. return ptab;
  317. }
  318. static void cell_iommu_enable_hardware(struct cbe_iommu *iommu)
  319. {
  320. int ret;
  321. unsigned long reg, xlate_base;
  322. unsigned int virq;
  323. if (cell_iommu_find_ioc(iommu->nid, &xlate_base))
  324. panic("%s: missing IOC register mappings for node %d\n",
  325. __func__, iommu->nid);
  326. iommu->xlate_regs = ioremap(xlate_base, IOC_Reg_Size);
  327. iommu->cmd_regs = iommu->xlate_regs + IOC_IOCmd_Offset;
  328. /* ensure that the STEs have updated */
  329. mb();
  330. /* setup interrupts for the iommu. */
  331. reg = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);
  332. out_be64(iommu->xlate_regs + IOC_IO_ExcpStat,
  333. reg & ~IOC_IO_ExcpStat_V);
  334. out_be64(iommu->xlate_regs + IOC_IO_ExcpMask,
  335. IOC_IO_ExcpMask_PFE | IOC_IO_ExcpMask_SFE);
  336. virq = irq_create_mapping(NULL,
  337. IIC_IRQ_IOEX_ATI | (iommu->nid << IIC_IRQ_NODE_SHIFT));
  338. BUG_ON(virq == NO_IRQ);
  339. ret = request_irq(virq, ioc_interrupt, IRQF_DISABLED,
  340. 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);
  356. cell_iommu_enable_hardware(iommu);
  357. }
  358. #if 0/* Unused for now */
  359. static struct iommu_window *find_window(struct cbe_iommu *iommu,
  360. unsigned long offset, unsigned long size)
  361. {
  362. struct iommu_window *window;
  363. /* todo: check for overlapping (but not equal) windows) */
  364. list_for_each_entry(window, &(iommu->windows), list) {
  365. if (window->offset == offset && window->size == size)
  366. return window;
  367. }
  368. return NULL;
  369. }
  370. #endif
  371. static inline u32 cell_iommu_get_ioid(struct device_node *np)
  372. {
  373. const u32 *ioid;
  374. ioid = of_get_property(np, "ioid", NULL);
  375. if (ioid == NULL) {
  376. printk(KERN_WARNING "iommu: missing ioid for %s using 0\n",
  377. np->full_name);
  378. return 0;
  379. }
  380. return *ioid;
  381. }
  382. static struct iommu_window * __init
  383. cell_iommu_setup_window(struct cbe_iommu *iommu, struct device_node *np,
  384. unsigned long offset, unsigned long size,
  385. unsigned long pte_offset)
  386. {
  387. struct iommu_window *window;
  388. struct page *page;
  389. u32 ioid;
  390. ioid = cell_iommu_get_ioid(np);
  391. window = kmalloc_node(sizeof(*window), GFP_KERNEL, iommu->nid);
  392. BUG_ON(window == NULL);
  393. window->offset = offset;
  394. window->size = size;
  395. window->ioid = ioid;
  396. window->iommu = iommu;
  397. window->table.it_blocksize = 16;
  398. window->table.it_base = (unsigned long)iommu->ptab;
  399. window->table.it_index = iommu->nid;
  400. window->table.it_offset = (offset >> IOMMU_PAGE_SHIFT) + pte_offset;
  401. window->table.it_size = size >> IOMMU_PAGE_SHIFT;
  402. iommu_init_table(&window->table, iommu->nid);
  403. pr_debug("\tioid %d\n", window->ioid);
  404. pr_debug("\tblocksize %ld\n", window->table.it_blocksize);
  405. pr_debug("\tbase 0x%016lx\n", window->table.it_base);
  406. pr_debug("\toffset 0x%lx\n", window->table.it_offset);
  407. pr_debug("\tsize %ld\n", window->table.it_size);
  408. list_add(&window->list, &iommu->windows);
  409. if (offset != 0)
  410. return window;
  411. /* We need to map and reserve the first IOMMU page since it's used
  412. * by the spider workaround. In theory, we only need to do that when
  413. * running on spider but it doesn't really matter.
  414. *
  415. * This code also assumes that we have a window that starts at 0,
  416. * which is the case on all spider based blades.
  417. */
  418. page = alloc_pages_node(iommu->nid, GFP_KERNEL, 0);
  419. BUG_ON(!page);
  420. iommu->pad_page = page_address(page);
  421. clear_page(iommu->pad_page);
  422. __set_bit(0, window->table.it_map);
  423. tce_build_cell(&window->table, window->table.it_offset, 1,
  424. (unsigned long)iommu->pad_page, DMA_TO_DEVICE);
  425. window->table.it_hint = window->table.it_blocksize;
  426. return window;
  427. }
  428. static struct cbe_iommu *cell_iommu_for_node(int nid)
  429. {
  430. int i;
  431. for (i = 0; i < cbe_nr_iommus; i++)
  432. if (iommus[i].nid == nid)
  433. return &iommus[i];
  434. return NULL;
  435. }
  436. static unsigned long cell_dma_direct_offset;
  437. static unsigned long dma_iommu_fixed_base;
  438. struct dma_mapping_ops dma_iommu_fixed_ops;
  439. static void cell_dma_dev_setup_iommu(struct device *dev)
  440. {
  441. struct iommu_window *window;
  442. struct cbe_iommu *iommu;
  443. struct dev_archdata *archdata = &dev->archdata;
  444. /* Current implementation uses the first window available in that
  445. * node's iommu. We -might- do something smarter later though it may
  446. * never be necessary
  447. */
  448. iommu = cell_iommu_for_node(archdata->numa_node);
  449. if (iommu == NULL || list_empty(&iommu->windows)) {
  450. printk(KERN_ERR "iommu: missing iommu for %s (node %d)\n",
  451. archdata->of_node ? archdata->of_node->full_name : "?",
  452. archdata->numa_node);
  453. return;
  454. }
  455. window = list_entry(iommu->windows.next, struct iommu_window, list);
  456. archdata->dma_data = &window->table;
  457. }
  458. static void cell_dma_dev_setup_fixed(struct device *dev);
  459. static void cell_dma_dev_setup(struct device *dev)
  460. {
  461. struct dev_archdata *archdata = &dev->archdata;
  462. /* Order is important here, these are not mutually exclusive */
  463. if (get_dma_ops(dev) == &dma_iommu_fixed_ops)
  464. cell_dma_dev_setup_fixed(dev);
  465. else if (get_pci_dma_ops() == &dma_iommu_ops)
  466. cell_dma_dev_setup_iommu(dev);
  467. else if (get_pci_dma_ops() == &dma_direct_ops)
  468. archdata->dma_data = (void *)cell_dma_direct_offset;
  469. else
  470. BUG();
  471. }
  472. static void cell_pci_dma_dev_setup(struct pci_dev *dev)
  473. {
  474. cell_dma_dev_setup(&dev->dev);
  475. }
  476. static int cell_of_bus_notify(struct notifier_block *nb, unsigned long action,
  477. void *data)
  478. {
  479. struct device *dev = data;
  480. /* We are only intereted in device addition */
  481. if (action != BUS_NOTIFY_ADD_DEVICE)
  482. return 0;
  483. /* We use the PCI DMA ops */
  484. dev->archdata.dma_ops = get_pci_dma_ops();
  485. cell_dma_dev_setup(dev);
  486. return 0;
  487. }
  488. static struct notifier_block cell_of_bus_notifier = {
  489. .notifier_call = cell_of_bus_notify
  490. };
  491. static int __init cell_iommu_get_window(struct device_node *np,
  492. unsigned long *base,
  493. unsigned long *size)
  494. {
  495. const void *dma_window;
  496. unsigned long index;
  497. /* Use ibm,dma-window if available, else, hard code ! */
  498. dma_window = of_get_property(np, "ibm,dma-window", NULL);
  499. if (dma_window == NULL) {
  500. *base = 0;
  501. *size = 0x80000000u;
  502. return -ENODEV;
  503. }
  504. of_parse_dma_window(np, dma_window, &index, base, size);
  505. return 0;
  506. }
  507. static struct cbe_iommu * __init cell_iommu_alloc(struct device_node *np)
  508. {
  509. struct cbe_iommu *iommu;
  510. int nid, i;
  511. /* Get node ID */
  512. nid = of_node_to_nid(np);
  513. if (nid < 0) {
  514. printk(KERN_ERR "iommu: failed to get node for %s\n",
  515. np->full_name);
  516. return NULL;
  517. }
  518. pr_debug("iommu: setting up iommu for node %d (%s)\n",
  519. nid, np->full_name);
  520. /* XXX todo: If we can have multiple windows on the same IOMMU, which
  521. * isn't the case today, we probably want here to check wether the
  522. * iommu for that node is already setup.
  523. * However, there might be issue with getting the size right so let's
  524. * ignore that for now. We might want to completely get rid of the
  525. * multiple window support since the cell iommu supports per-page ioids
  526. */
  527. if (cbe_nr_iommus >= NR_IOMMUS) {
  528. printk(KERN_ERR "iommu: too many IOMMUs detected ! (%s)\n",
  529. np->full_name);
  530. return NULL;
  531. }
  532. /* Init base fields */
  533. i = cbe_nr_iommus++;
  534. iommu = &iommus[i];
  535. iommu->stab = NULL;
  536. iommu->nid = nid;
  537. snprintf(iommu->name, sizeof(iommu->name), "iommu%d", i);
  538. INIT_LIST_HEAD(&iommu->windows);
  539. return iommu;
  540. }
  541. static void __init cell_iommu_init_one(struct device_node *np,
  542. unsigned long offset)
  543. {
  544. struct cbe_iommu *iommu;
  545. unsigned long base, size;
  546. iommu = cell_iommu_alloc(np);
  547. if (!iommu)
  548. return;
  549. /* Obtain a window for it */
  550. cell_iommu_get_window(np, &base, &size);
  551. pr_debug("\ttranslating window 0x%lx...0x%lx\n",
  552. base, base + size - 1);
  553. /* Initialize the hardware */
  554. cell_iommu_setup_hardware(iommu, base, size);
  555. /* Setup the iommu_table */
  556. cell_iommu_setup_window(iommu, np, base, size,
  557. offset >> IOMMU_PAGE_SHIFT);
  558. }
  559. static void __init cell_disable_iommus(void)
  560. {
  561. int node;
  562. unsigned long base, val;
  563. void __iomem *xregs, *cregs;
  564. /* Make sure IOC translation is disabled on all nodes */
  565. for_each_online_node(node) {
  566. if (cell_iommu_find_ioc(node, &base))
  567. continue;
  568. xregs = ioremap(base, IOC_Reg_Size);
  569. if (xregs == NULL)
  570. continue;
  571. cregs = xregs + IOC_IOCmd_Offset;
  572. pr_debug("iommu: cleaning up iommu on node %d\n", node);
  573. out_be64(xregs + IOC_IOST_Origin, 0);
  574. (void)in_be64(xregs + IOC_IOST_Origin);
  575. val = in_be64(cregs + IOC_IOCmd_Cfg);
  576. val &= ~IOC_IOCmd_Cfg_TE;
  577. out_be64(cregs + IOC_IOCmd_Cfg, val);
  578. (void)in_be64(cregs + IOC_IOCmd_Cfg);
  579. iounmap(xregs);
  580. }
  581. }
  582. static int __init cell_iommu_init_disabled(void)
  583. {
  584. struct device_node *np = NULL;
  585. unsigned long base = 0, size;
  586. /* When no iommu is present, we use direct DMA ops */
  587. set_pci_dma_ops(&dma_direct_ops);
  588. /* First make sure all IOC translation is turned off */
  589. cell_disable_iommus();
  590. /* If we have no Axon, we set up the spider DMA magic offset */
  591. if (of_find_node_by_name(NULL, "axon") == NULL)
  592. cell_dma_direct_offset = SPIDER_DMA_OFFSET;
  593. /* Now we need to check to see where the memory is mapped
  594. * in PCI space. We assume that all busses use the same dma
  595. * window which is always the case so far on Cell, thus we
  596. * pick up the first pci-internal node we can find and check
  597. * the DMA window from there.
  598. */
  599. for_each_node_by_name(np, "axon") {
  600. if (np->parent == NULL || np->parent->parent != NULL)
  601. continue;
  602. if (cell_iommu_get_window(np, &base, &size) == 0)
  603. break;
  604. }
  605. if (np == NULL) {
  606. for_each_node_by_name(np, "pci-internal") {
  607. if (np->parent == NULL || np->parent->parent != NULL)
  608. continue;
  609. if (cell_iommu_get_window(np, &base, &size) == 0)
  610. break;
  611. }
  612. }
  613. of_node_put(np);
  614. /* If we found a DMA window, we check if it's big enough to enclose
  615. * all of physical memory. If not, we force enable IOMMU
  616. */
  617. if (np && size < lmb_end_of_DRAM()) {
  618. printk(KERN_WARNING "iommu: force-enabled, dma window"
  619. " (%ldMB) smaller than total memory (%ldMB)\n",
  620. size >> 20, lmb_end_of_DRAM() >> 20);
  621. return -ENODEV;
  622. }
  623. cell_dma_direct_offset += base;
  624. if (cell_dma_direct_offset != 0)
  625. ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
  626. printk("iommu: disabled, direct DMA offset is 0x%lx\n",
  627. cell_dma_direct_offset);
  628. return 0;
  629. }
  630. /*
  631. * Fixed IOMMU mapping support
  632. *
  633. * This code adds support for setting up a fixed IOMMU mapping on certain
  634. * cell machines. For 64-bit devices this avoids the performance overhead of
  635. * mapping and unmapping pages at runtime. 32-bit devices are unable to use
  636. * the fixed mapping.
  637. *
  638. * The fixed mapping is established at boot, and maps all of physical memory
  639. * 1:1 into device space at some offset. On machines with < 30 GB of memory
  640. * we setup the fixed mapping immediately above the normal IOMMU window.
  641. *
  642. * For example a machine with 4GB of memory would end up with the normal
  643. * IOMMU window from 0-2GB and the fixed mapping window from 2GB to 6GB. In
  644. * this case a 64-bit device wishing to DMA to 1GB would be told to DMA to
  645. * 3GB, plus any offset required by firmware. The firmware offset is encoded
  646. * in the "dma-ranges" property.
  647. *
  648. * On machines with 30GB or more of memory, we are unable to place the fixed
  649. * mapping above the normal IOMMU window as we would run out of address space.
  650. * Instead we move the normal IOMMU window to coincide with the hash page
  651. * table, this region does not need to be part of the fixed mapping as no
  652. * device should ever be DMA'ing to it. We then setup the fixed mapping
  653. * from 0 to 32GB.
  654. */
  655. static u64 cell_iommu_get_fixed_address(struct device *dev)
  656. {
  657. u64 cpu_addr, size, best_size, dev_addr = OF_BAD_ADDR;
  658. struct device_node *np;
  659. const u32 *ranges = NULL;
  660. int i, len, best, naddr, nsize, pna, range_size;
  661. np = of_node_get(dev->archdata.of_node);
  662. while (1) {
  663. naddr = of_n_addr_cells(np);
  664. nsize = of_n_size_cells(np);
  665. np = of_get_next_parent(np);
  666. if (!np)
  667. break;
  668. ranges = of_get_property(np, "dma-ranges", &len);
  669. /* Ignore empty ranges, they imply no translation required */
  670. if (ranges && len > 0)
  671. break;
  672. }
  673. if (!ranges) {
  674. dev_dbg(dev, "iommu: no dma-ranges found\n");
  675. goto out;
  676. }
  677. len /= sizeof(u32);
  678. pna = of_n_addr_cells(np);
  679. range_size = naddr + nsize + pna;
  680. /* dma-ranges format:
  681. * child addr : naddr cells
  682. * parent addr : pna cells
  683. * size : nsize cells
  684. */
  685. for (i = 0, best = -1, best_size = 0; i < len; i += range_size) {
  686. cpu_addr = of_translate_dma_address(np, ranges + i + naddr);
  687. size = of_read_number(ranges + i + naddr + pna, nsize);
  688. if (cpu_addr == 0 && size > best_size) {
  689. best = i;
  690. best_size = size;
  691. }
  692. }
  693. if (best >= 0) {
  694. dev_addr = of_read_number(ranges + best, naddr);
  695. } else
  696. dev_dbg(dev, "iommu: no suitable range found!\n");
  697. out:
  698. of_node_put(np);
  699. return dev_addr;
  700. }
  701. static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask)
  702. {
  703. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  704. return -EIO;
  705. if (dma_mask == DMA_BIT_MASK(64) &&
  706. cell_iommu_get_fixed_address(dev) != OF_BAD_ADDR)
  707. {
  708. dev_dbg(dev, "iommu: 64-bit OK, using fixed ops\n");
  709. set_dma_ops(dev, &dma_iommu_fixed_ops);
  710. } else {
  711. dev_dbg(dev, "iommu: not 64-bit, using default ops\n");
  712. set_dma_ops(dev, get_pci_dma_ops());
  713. }
  714. cell_dma_dev_setup(dev);
  715. *dev->dma_mask = dma_mask;
  716. return 0;
  717. }
  718. static void cell_dma_dev_setup_fixed(struct device *dev)
  719. {
  720. struct dev_archdata *archdata = &dev->archdata;
  721. u64 addr;
  722. addr = cell_iommu_get_fixed_address(dev) + dma_iommu_fixed_base;
  723. archdata->dma_data = (void *)addr;
  724. dev_dbg(dev, "iommu: fixed addr = %lx\n", addr);
  725. }
  726. static void insert_16M_pte(unsigned long addr, unsigned long *ptab,
  727. unsigned long base_pte)
  728. {
  729. unsigned long segment, offset;
  730. segment = addr >> IO_SEGMENT_SHIFT;
  731. offset = (addr >> 24) - (segment << IO_PAGENO_BITS(24));
  732. ptab = ptab + (segment * (1 << 12) / sizeof(unsigned long));
  733. pr_debug("iommu: addr %lx ptab %p segment %lx offset %lx\n",
  734. addr, ptab, segment, offset);
  735. ptab[offset] = base_pte | (__pa(addr) & IOPTE_RPN_Mask);
  736. }
  737. static void cell_iommu_setup_fixed_ptab(struct cbe_iommu *iommu,
  738. struct device_node *np, unsigned long dbase, unsigned long dsize,
  739. unsigned long fbase, unsigned long fsize)
  740. {
  741. unsigned long base_pte, uaddr, ioaddr, *ptab;
  742. ptab = cell_iommu_alloc_ptab(iommu, fbase, fsize, dbase, dsize, 24);
  743. dma_iommu_fixed_base = fbase;
  744. pr_debug("iommu: mapping 0x%lx pages from 0x%lx\n", fsize, fbase);
  745. base_pte = IOPTE_PP_W | IOPTE_PP_R | IOPTE_M | IOPTE_SO_RW
  746. | (cell_iommu_get_ioid(np) & IOPTE_IOID_Mask);
  747. for (uaddr = 0; uaddr < fsize; uaddr += (1 << 24)) {
  748. /* Don't touch the dynamic region */
  749. ioaddr = uaddr + fbase;
  750. if (ioaddr >= dbase && ioaddr < (dbase + dsize)) {
  751. pr_debug("iommu: fixed/dynamic overlap, skipping\n");
  752. continue;
  753. }
  754. insert_16M_pte(uaddr, ptab, base_pte);
  755. }
  756. mb();
  757. }
  758. static int __init cell_iommu_fixed_mapping_init(void)
  759. {
  760. unsigned long dbase, dsize, fbase, fsize, hbase, hend;
  761. struct cbe_iommu *iommu;
  762. struct device_node *np;
  763. /* The fixed mapping is only supported on axon machines */
  764. np = of_find_node_by_name(NULL, "axon");
  765. if (!np) {
  766. pr_debug("iommu: fixed mapping disabled, no axons found\n");
  767. return -1;
  768. }
  769. /* We must have dma-ranges properties for fixed mapping to work */
  770. for (np = NULL; (np = of_find_all_nodes(np));) {
  771. if (of_find_property(np, "dma-ranges", NULL))
  772. break;
  773. }
  774. of_node_put(np);
  775. if (!np) {
  776. pr_debug("iommu: no dma-ranges found, no fixed mapping\n");
  777. return -1;
  778. }
  779. /* The default setup is to have the fixed mapping sit after the
  780. * dynamic region, so find the top of the largest IOMMU window
  781. * on any axon, then add the size of RAM and that's our max value.
  782. * If that is > 32GB we have to do other shennanigans.
  783. */
  784. fbase = 0;
  785. for_each_node_by_name(np, "axon") {
  786. cell_iommu_get_window(np, &dbase, &dsize);
  787. fbase = max(fbase, dbase + dsize);
  788. }
  789. fbase = _ALIGN_UP(fbase, 1 << IO_SEGMENT_SHIFT);
  790. fsize = lmb_phys_mem_size();
  791. if ((fbase + fsize) <= 0x800000000)
  792. hbase = 0; /* use the device tree window */
  793. else {
  794. /* If we're over 32 GB we need to cheat. We can't map all of
  795. * RAM with the fixed mapping, and also fit the dynamic
  796. * region. So try to place the dynamic region where the hash
  797. * table sits, drivers never need to DMA to it, we don't
  798. * need a fixed mapping for that area.
  799. */
  800. if (!htab_address) {
  801. pr_debug("iommu: htab is NULL, on LPAR? Huh?\n");
  802. return -1;
  803. }
  804. hbase = __pa(htab_address);
  805. hend = hbase + htab_size_bytes;
  806. /* The window must start and end on a segment boundary */
  807. if ((hbase != _ALIGN_UP(hbase, 1 << IO_SEGMENT_SHIFT)) ||
  808. (hend != _ALIGN_UP(hend, 1 << IO_SEGMENT_SHIFT))) {
  809. pr_debug("iommu: hash window not segment aligned\n");
  810. return -1;
  811. }
  812. /* Check the hash window fits inside the real DMA window */
  813. for_each_node_by_name(np, "axon") {
  814. cell_iommu_get_window(np, &dbase, &dsize);
  815. if (hbase < dbase || (hend > (dbase + dsize))) {
  816. pr_debug("iommu: hash window doesn't fit in"
  817. "real DMA window\n");
  818. return -1;
  819. }
  820. }
  821. fbase = 0;
  822. }
  823. /* Setup the dynamic regions */
  824. for_each_node_by_name(np, "axon") {
  825. iommu = cell_iommu_alloc(np);
  826. BUG_ON(!iommu);
  827. if (hbase == 0)
  828. cell_iommu_get_window(np, &dbase, &dsize);
  829. else {
  830. dbase = hbase;
  831. dsize = htab_size_bytes;
  832. }
  833. printk(KERN_DEBUG "iommu: node %d, dynamic window 0x%lx-0x%lx "
  834. "fixed window 0x%lx-0x%lx\n", iommu->nid, dbase,
  835. dbase + dsize, fbase, fbase + fsize);
  836. cell_iommu_setup_stab(iommu, dbase, dsize, fbase, fsize);
  837. iommu->ptab = cell_iommu_alloc_ptab(iommu, dbase, dsize, 0, 0,
  838. IOMMU_PAGE_SHIFT);
  839. cell_iommu_setup_fixed_ptab(iommu, np, dbase, dsize,
  840. fbase, fsize);
  841. cell_iommu_enable_hardware(iommu);
  842. cell_iommu_setup_window(iommu, np, dbase, dsize, 0);
  843. }
  844. dma_iommu_fixed_ops = dma_direct_ops;
  845. dma_iommu_fixed_ops.set_dma_mask = dma_set_mask_and_switch;
  846. dma_iommu_ops.set_dma_mask = dma_set_mask_and_switch;
  847. set_pci_dma_ops(&dma_iommu_ops);
  848. return 0;
  849. }
  850. static int iommu_fixed_disabled;
  851. static int __init setup_iommu_fixed(char *str)
  852. {
  853. if (strcmp(str, "off") == 0)
  854. iommu_fixed_disabled = 1;
  855. return 1;
  856. }
  857. __setup("iommu_fixed=", setup_iommu_fixed);
  858. static int __init cell_iommu_init(void)
  859. {
  860. struct device_node *np;
  861. /* If IOMMU is disabled or we have little enough RAM to not need
  862. * to enable it, we setup a direct mapping.
  863. *
  864. * Note: should we make sure we have the IOMMU actually disabled ?
  865. */
  866. if (iommu_is_off ||
  867. (!iommu_force_on && lmb_end_of_DRAM() <= 0x80000000ull))
  868. if (cell_iommu_init_disabled() == 0)
  869. goto bail;
  870. /* Setup various ppc_md. callbacks */
  871. ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
  872. ppc_md.tce_build = tce_build_cell;
  873. ppc_md.tce_free = tce_free_cell;
  874. if (!iommu_fixed_disabled && cell_iommu_fixed_mapping_init() == 0)
  875. goto bail;
  876. /* Create an iommu for each /axon node. */
  877. for_each_node_by_name(np, "axon") {
  878. if (np->parent == NULL || np->parent->parent != NULL)
  879. continue;
  880. cell_iommu_init_one(np, 0);
  881. }
  882. /* Create an iommu for each toplevel /pci-internal node for
  883. * old hardware/firmware
  884. */
  885. for_each_node_by_name(np, "pci-internal") {
  886. if (np->parent == NULL || np->parent->parent != NULL)
  887. continue;
  888. cell_iommu_init_one(np, SPIDER_DMA_OFFSET);
  889. }
  890. /* Setup default PCI iommu ops */
  891. set_pci_dma_ops(&dma_iommu_ops);
  892. bail:
  893. /* Register callbacks on OF platform device addition/removal
  894. * to handle linking them to the right DMA operations
  895. */
  896. bus_register_notifier(&of_platform_bus_type, &cell_of_bus_notifier);
  897. return 0;
  898. }
  899. machine_arch_initcall(cell, cell_iommu_init);
  900. machine_arch_initcall(celleb_native, cell_iommu_init);