iommu.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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 <asm/prom.h>
  30. #include <asm/iommu.h>
  31. #include <asm/machdep.h>
  32. #include <asm/pci-bridge.h>
  33. #include <asm/udbg.h>
  34. #include <asm/lmb.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 (IO_SEGMENT_SHIFT - IOMMU_PAGE_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_page_tables(struct cbe_iommu *iommu,
  254. unsigned long dbase, unsigned long dsize,
  255. unsigned long fbase, unsigned long fsize)
  256. {
  257. struct page *page;
  258. int i;
  259. unsigned long reg, segments, pages_per_segment, ptab_size, stab_size,
  260. n_pte_pages, base;
  261. base = dbase;
  262. if (fsize != 0)
  263. base = min(fbase, dbase);
  264. segments = max(dbase + dsize, fbase + fsize) >> 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. stab_size = segments * sizeof(unsigned long);
  270. page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(stab_size));
  271. BUG_ON(!page);
  272. iommu->stab = page_address(page);
  273. clear_page(iommu->stab);
  274. /* ... and the page tables. Since these are contiguous, we can treat
  275. * the page tables as one array of ptes, like pSeries does.
  276. */
  277. ptab_size = segments * pages_per_segment * sizeof(unsigned long);
  278. pr_debug("%s: iommu[%d]: ptab_size: %lu, order: %d\n", __FUNCTION__,
  279. iommu->nid, ptab_size, get_order(ptab_size));
  280. page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(ptab_size));
  281. BUG_ON(!page);
  282. iommu->ptab = page_address(page);
  283. memset(iommu->ptab, 0, ptab_size);
  284. /* allocate a bogus page for the end of each mapping */
  285. page = alloc_pages_node(iommu->nid, GFP_KERNEL, 0);
  286. BUG_ON(!page);
  287. iommu->pad_page = page_address(page);
  288. clear_page(iommu->pad_page);
  289. /* number of pages needed for a page table */
  290. n_pte_pages = (pages_per_segment *
  291. sizeof(unsigned long)) >> IOMMU_PAGE_SHIFT;
  292. pr_debug("%s: iommu[%d]: stab at %p, ptab at %p, n_pte_pages: %lu\n",
  293. __FUNCTION__, iommu->nid, iommu->stab, iommu->ptab,
  294. n_pte_pages);
  295. /* initialise the STEs */
  296. reg = IOSTE_V | ((n_pte_pages - 1) << 5);
  297. if (IOMMU_PAGE_SIZE == 0x1000)
  298. reg |= IOSTE_PS_4K;
  299. else if (IOMMU_PAGE_SIZE == 0x10000)
  300. reg |= IOSTE_PS_64K;
  301. else {
  302. extern void __unknown_page_size_error(void);
  303. __unknown_page_size_error();
  304. }
  305. pr_debug("Setting up IOMMU stab:\n");
  306. for (i = base >> IO_SEGMENT_SHIFT; i < segments; i++) {
  307. iommu->stab[i] = reg |
  308. (__pa(iommu->ptab) + n_pte_pages * IOMMU_PAGE_SIZE * i);
  309. pr_debug("\t[%d] 0x%016lx\n", i, iommu->stab[i]);
  310. }
  311. }
  312. static void cell_iommu_enable_hardware(struct cbe_iommu *iommu)
  313. {
  314. int ret;
  315. unsigned long reg, xlate_base;
  316. unsigned int virq;
  317. if (cell_iommu_find_ioc(iommu->nid, &xlate_base))
  318. panic("%s: missing IOC register mappings for node %d\n",
  319. __FUNCTION__, iommu->nid);
  320. iommu->xlate_regs = ioremap(xlate_base, IOC_Reg_Size);
  321. iommu->cmd_regs = iommu->xlate_regs + IOC_IOCmd_Offset;
  322. /* ensure that the STEs have updated */
  323. mb();
  324. /* setup interrupts for the iommu. */
  325. reg = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);
  326. out_be64(iommu->xlate_regs + IOC_IO_ExcpStat,
  327. reg & ~IOC_IO_ExcpStat_V);
  328. out_be64(iommu->xlate_regs + IOC_IO_ExcpMask,
  329. IOC_IO_ExcpMask_PFE | IOC_IO_ExcpMask_SFE);
  330. virq = irq_create_mapping(NULL,
  331. IIC_IRQ_IOEX_ATI | (iommu->nid << IIC_IRQ_NODE_SHIFT));
  332. BUG_ON(virq == NO_IRQ);
  333. ret = request_irq(virq, ioc_interrupt, IRQF_DISABLED,
  334. iommu->name, iommu);
  335. BUG_ON(ret);
  336. /* set the IOC segment table origin register (and turn on the iommu) */
  337. reg = IOC_IOST_Origin_E | __pa(iommu->stab) | IOC_IOST_Origin_HW;
  338. out_be64(iommu->xlate_regs + IOC_IOST_Origin, reg);
  339. in_be64(iommu->xlate_regs + IOC_IOST_Origin);
  340. /* turn on IO translation */
  341. reg = in_be64(iommu->cmd_regs + IOC_IOCmd_Cfg) | IOC_IOCmd_Cfg_TE;
  342. out_be64(iommu->cmd_regs + IOC_IOCmd_Cfg, reg);
  343. }
  344. static void cell_iommu_setup_hardware(struct cbe_iommu *iommu,
  345. unsigned long base, unsigned long size)
  346. {
  347. cell_iommu_setup_page_tables(iommu, base, size, 0, 0);
  348. cell_iommu_enable_hardware(iommu);
  349. }
  350. #if 0/* Unused for now */
  351. static struct iommu_window *find_window(struct cbe_iommu *iommu,
  352. unsigned long offset, unsigned long size)
  353. {
  354. struct iommu_window *window;
  355. /* todo: check for overlapping (but not equal) windows) */
  356. list_for_each_entry(window, &(iommu->windows), list) {
  357. if (window->offset == offset && window->size == size)
  358. return window;
  359. }
  360. return NULL;
  361. }
  362. #endif
  363. static inline u32 cell_iommu_get_ioid(struct device_node *np)
  364. {
  365. const u32 *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. return 0;
  371. }
  372. return *ioid;
  373. }
  374. static struct iommu_window * __init
  375. cell_iommu_setup_window(struct cbe_iommu *iommu, struct device_node *np,
  376. unsigned long offset, unsigned long size,
  377. unsigned long pte_offset)
  378. {
  379. struct iommu_window *window;
  380. u32 ioid;
  381. ioid = cell_iommu_get_ioid(np);
  382. window = kmalloc_node(sizeof(*window), GFP_KERNEL, iommu->nid);
  383. BUG_ON(window == NULL);
  384. window->offset = offset;
  385. window->size = size;
  386. window->ioid = ioid;
  387. window->iommu = iommu;
  388. window->table.it_blocksize = 16;
  389. window->table.it_base = (unsigned long)iommu->ptab;
  390. window->table.it_index = iommu->nid;
  391. window->table.it_offset = (offset >> IOMMU_PAGE_SHIFT) + pte_offset;
  392. window->table.it_size = size >> IOMMU_PAGE_SHIFT;
  393. iommu_init_table(&window->table, iommu->nid);
  394. pr_debug("\tioid %d\n", window->ioid);
  395. pr_debug("\tblocksize %ld\n", window->table.it_blocksize);
  396. pr_debug("\tbase 0x%016lx\n", window->table.it_base);
  397. pr_debug("\toffset 0x%lx\n", window->table.it_offset);
  398. pr_debug("\tsize %ld\n", window->table.it_size);
  399. list_add(&window->list, &iommu->windows);
  400. if (offset != 0)
  401. return window;
  402. /* We need to map and reserve the first IOMMU page since it's used
  403. * by the spider workaround. In theory, we only need to do that when
  404. * running on spider but it doesn't really matter.
  405. *
  406. * This code also assumes that we have a window that starts at 0,
  407. * which is the case on all spider based blades.
  408. */
  409. __set_bit(0, window->table.it_map);
  410. tce_build_cell(&window->table, window->table.it_offset, 1,
  411. (unsigned long)iommu->pad_page, DMA_TO_DEVICE);
  412. window->table.it_hint = window->table.it_blocksize;
  413. return window;
  414. }
  415. static struct cbe_iommu *cell_iommu_for_node(int nid)
  416. {
  417. int i;
  418. for (i = 0; i < cbe_nr_iommus; i++)
  419. if (iommus[i].nid == nid)
  420. return &iommus[i];
  421. return NULL;
  422. }
  423. static unsigned long cell_dma_direct_offset;
  424. static unsigned long dma_iommu_fixed_base;
  425. struct dma_mapping_ops dma_iommu_fixed_ops;
  426. static void cell_dma_dev_setup_iommu(struct device *dev)
  427. {
  428. struct iommu_window *window;
  429. struct cbe_iommu *iommu;
  430. struct dev_archdata *archdata = &dev->archdata;
  431. /* Current implementation uses the first window available in that
  432. * node's iommu. We -might- do something smarter later though it may
  433. * never be necessary
  434. */
  435. iommu = cell_iommu_for_node(archdata->numa_node);
  436. if (iommu == NULL || list_empty(&iommu->windows)) {
  437. printk(KERN_ERR "iommu: missing iommu for %s (node %d)\n",
  438. archdata->of_node ? archdata->of_node->full_name : "?",
  439. archdata->numa_node);
  440. return;
  441. }
  442. window = list_entry(iommu->windows.next, struct iommu_window, list);
  443. archdata->dma_data = &window->table;
  444. }
  445. static void cell_dma_dev_setup_fixed(struct device *dev);
  446. static void cell_dma_dev_setup(struct device *dev)
  447. {
  448. struct dev_archdata *archdata = &dev->archdata;
  449. /* Order is important here, these are not mutually exclusive */
  450. if (get_dma_ops(dev) == &dma_iommu_fixed_ops)
  451. cell_dma_dev_setup_fixed(dev);
  452. else if (get_pci_dma_ops() == &dma_iommu_ops)
  453. cell_dma_dev_setup_iommu(dev);
  454. else if (get_pci_dma_ops() == &dma_direct_ops)
  455. archdata->dma_data = (void *)cell_dma_direct_offset;
  456. else
  457. BUG();
  458. }
  459. static void cell_pci_dma_dev_setup(struct pci_dev *dev)
  460. {
  461. cell_dma_dev_setup(&dev->dev);
  462. }
  463. static int cell_of_bus_notify(struct notifier_block *nb, unsigned long action,
  464. void *data)
  465. {
  466. struct device *dev = data;
  467. /* We are only intereted in device addition */
  468. if (action != BUS_NOTIFY_ADD_DEVICE)
  469. return 0;
  470. /* We use the PCI DMA ops */
  471. dev->archdata.dma_ops = get_pci_dma_ops();
  472. cell_dma_dev_setup(dev);
  473. return 0;
  474. }
  475. static struct notifier_block cell_of_bus_notifier = {
  476. .notifier_call = cell_of_bus_notify
  477. };
  478. static int __init cell_iommu_get_window(struct device_node *np,
  479. unsigned long *base,
  480. unsigned long *size)
  481. {
  482. const void *dma_window;
  483. unsigned long index;
  484. /* Use ibm,dma-window if available, else, hard code ! */
  485. dma_window = of_get_property(np, "ibm,dma-window", NULL);
  486. if (dma_window == NULL) {
  487. *base = 0;
  488. *size = 0x80000000u;
  489. return -ENODEV;
  490. }
  491. of_parse_dma_window(np, dma_window, &index, base, size);
  492. return 0;
  493. }
  494. static struct cbe_iommu * __init cell_iommu_alloc(struct device_node *np)
  495. {
  496. struct cbe_iommu *iommu;
  497. int nid, i;
  498. /* Get node ID */
  499. nid = of_node_to_nid(np);
  500. if (nid < 0) {
  501. printk(KERN_ERR "iommu: failed to get node for %s\n",
  502. np->full_name);
  503. return NULL;
  504. }
  505. pr_debug("iommu: setting up iommu for node %d (%s)\n",
  506. nid, np->full_name);
  507. /* XXX todo: If we can have multiple windows on the same IOMMU, which
  508. * isn't the case today, we probably want here to check wether the
  509. * iommu for that node is already setup.
  510. * However, there might be issue with getting the size right so let's
  511. * ignore that for now. We might want to completely get rid of the
  512. * multiple window support since the cell iommu supports per-page ioids
  513. */
  514. if (cbe_nr_iommus >= NR_IOMMUS) {
  515. printk(KERN_ERR "iommu: too many IOMMUs detected ! (%s)\n",
  516. np->full_name);
  517. return NULL;
  518. }
  519. /* Init base fields */
  520. i = cbe_nr_iommus++;
  521. iommu = &iommus[i];
  522. iommu->stab = NULL;
  523. iommu->nid = nid;
  524. snprintf(iommu->name, sizeof(iommu->name), "iommu%d", i);
  525. INIT_LIST_HEAD(&iommu->windows);
  526. return iommu;
  527. }
  528. static void __init cell_iommu_init_one(struct device_node *np,
  529. unsigned long offset)
  530. {
  531. struct cbe_iommu *iommu;
  532. unsigned long base, size;
  533. iommu = cell_iommu_alloc(np);
  534. if (!iommu)
  535. return;
  536. /* Obtain a window for it */
  537. cell_iommu_get_window(np, &base, &size);
  538. pr_debug("\ttranslating window 0x%lx...0x%lx\n",
  539. base, base + size - 1);
  540. /* Initialize the hardware */
  541. cell_iommu_setup_hardware(iommu, base, size);
  542. /* Setup the iommu_table */
  543. cell_iommu_setup_window(iommu, np, base, size,
  544. offset >> IOMMU_PAGE_SHIFT);
  545. }
  546. static void __init cell_disable_iommus(void)
  547. {
  548. int node;
  549. unsigned long base, val;
  550. void __iomem *xregs, *cregs;
  551. /* Make sure IOC translation is disabled on all nodes */
  552. for_each_online_node(node) {
  553. if (cell_iommu_find_ioc(node, &base))
  554. continue;
  555. xregs = ioremap(base, IOC_Reg_Size);
  556. if (xregs == NULL)
  557. continue;
  558. cregs = xregs + IOC_IOCmd_Offset;
  559. pr_debug("iommu: cleaning up iommu on node %d\n", node);
  560. out_be64(xregs + IOC_IOST_Origin, 0);
  561. (void)in_be64(xregs + IOC_IOST_Origin);
  562. val = in_be64(cregs + IOC_IOCmd_Cfg);
  563. val &= ~IOC_IOCmd_Cfg_TE;
  564. out_be64(cregs + IOC_IOCmd_Cfg, val);
  565. (void)in_be64(cregs + IOC_IOCmd_Cfg);
  566. iounmap(xregs);
  567. }
  568. }
  569. static int __init cell_iommu_init_disabled(void)
  570. {
  571. struct device_node *np = NULL;
  572. unsigned long base = 0, size;
  573. /* When no iommu is present, we use direct DMA ops */
  574. set_pci_dma_ops(&dma_direct_ops);
  575. /* First make sure all IOC translation is turned off */
  576. cell_disable_iommus();
  577. /* If we have no Axon, we set up the spider DMA magic offset */
  578. if (of_find_node_by_name(NULL, "axon") == NULL)
  579. cell_dma_direct_offset = SPIDER_DMA_OFFSET;
  580. /* Now we need to check to see where the memory is mapped
  581. * in PCI space. We assume that all busses use the same dma
  582. * window which is always the case so far on Cell, thus we
  583. * pick up the first pci-internal node we can find and check
  584. * the DMA window from there.
  585. */
  586. for_each_node_by_name(np, "axon") {
  587. if (np->parent == NULL || np->parent->parent != NULL)
  588. continue;
  589. if (cell_iommu_get_window(np, &base, &size) == 0)
  590. break;
  591. }
  592. if (np == NULL) {
  593. for_each_node_by_name(np, "pci-internal") {
  594. if (np->parent == NULL || np->parent->parent != NULL)
  595. continue;
  596. if (cell_iommu_get_window(np, &base, &size) == 0)
  597. break;
  598. }
  599. }
  600. of_node_put(np);
  601. /* If we found a DMA window, we check if it's big enough to enclose
  602. * all of physical memory. If not, we force enable IOMMU
  603. */
  604. if (np && size < lmb_end_of_DRAM()) {
  605. printk(KERN_WARNING "iommu: force-enabled, dma window"
  606. " (%ldMB) smaller than total memory (%ldMB)\n",
  607. size >> 20, lmb_end_of_DRAM() >> 20);
  608. return -ENODEV;
  609. }
  610. cell_dma_direct_offset += base;
  611. if (cell_dma_direct_offset != 0)
  612. ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
  613. printk("iommu: disabled, direct DMA offset is 0x%lx\n",
  614. cell_dma_direct_offset);
  615. return 0;
  616. }
  617. /*
  618. * Fixed IOMMU mapping support
  619. *
  620. * This code adds support for setting up a fixed IOMMU mapping on certain
  621. * cell machines. For 64-bit devices this avoids the performance overhead of
  622. * mapping and unmapping pages at runtime. 32-bit devices are unable to use
  623. * the fixed mapping.
  624. *
  625. * The fixed mapping is established at boot, and maps all of physical memory
  626. * 1:1 into device space at some offset. On machines with < 30 GB of memory
  627. * we setup the fixed mapping immediately above the normal IOMMU window.
  628. *
  629. * For example a machine with 4GB of memory would end up with the normal
  630. * IOMMU window from 0-2GB and the fixed mapping window from 2GB to 6GB. In
  631. * this case a 64-bit device wishing to DMA to 1GB would be told to DMA to
  632. * 3GB, plus any offset required by firmware. The firmware offset is encoded
  633. * in the "dma-ranges" property.
  634. *
  635. * On machines with 30GB or more of memory, we are unable to place the fixed
  636. * mapping above the normal IOMMU window as we would run out of address space.
  637. * Instead we move the normal IOMMU window to coincide with the hash page
  638. * table, this region does not need to be part of the fixed mapping as no
  639. * device should ever be DMA'ing to it. We then setup the fixed mapping
  640. * from 0 to 32GB.
  641. */
  642. static u64 cell_iommu_get_fixed_address(struct device *dev)
  643. {
  644. u64 cpu_addr, size, best_size, pci_addr = OF_BAD_ADDR;
  645. struct device_node *np;
  646. const u32 *ranges = NULL;
  647. int i, len, best;
  648. np = of_node_get(dev->archdata.of_node);
  649. while (np) {
  650. ranges = of_get_property(np, "dma-ranges", &len);
  651. if (ranges)
  652. break;
  653. np = of_get_next_parent(np);
  654. }
  655. if (!ranges) {
  656. dev_dbg(dev, "iommu: no dma-ranges found\n");
  657. goto out;
  658. }
  659. len /= sizeof(u32);
  660. /* dma-ranges format:
  661. * 1 cell: pci space
  662. * 2 cells: pci address
  663. * 2 cells: parent address
  664. * 2 cells: size
  665. */
  666. for (i = 0, best = -1, best_size = 0; i < len; i += 7) {
  667. cpu_addr = of_translate_dma_address(np, ranges +i + 3);
  668. size = of_read_number(ranges + i + 5, 2);
  669. if (cpu_addr == 0 && size > best_size) {
  670. best = i;
  671. best_size = size;
  672. }
  673. }
  674. if (best >= 0)
  675. pci_addr = of_read_number(ranges + best + 1, 2);
  676. else
  677. dev_dbg(dev, "iommu: no suitable range found!\n");
  678. out:
  679. of_node_put(np);
  680. return pci_addr;
  681. }
  682. static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask)
  683. {
  684. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  685. return -EIO;
  686. if (dma_mask == DMA_BIT_MASK(64) &&
  687. cell_iommu_get_fixed_address(dev) != OF_BAD_ADDR)
  688. {
  689. dev_dbg(dev, "iommu: 64-bit OK, using fixed ops\n");
  690. set_dma_ops(dev, &dma_iommu_fixed_ops);
  691. } else {
  692. dev_dbg(dev, "iommu: not 64-bit, using default ops\n");
  693. set_dma_ops(dev, get_pci_dma_ops());
  694. }
  695. cell_dma_dev_setup(dev);
  696. *dev->dma_mask = dma_mask;
  697. return 0;
  698. }
  699. static void cell_dma_dev_setup_fixed(struct device *dev)
  700. {
  701. struct dev_archdata *archdata = &dev->archdata;
  702. u64 addr;
  703. addr = cell_iommu_get_fixed_address(dev) + dma_iommu_fixed_base;
  704. archdata->dma_data = (void *)addr;
  705. dev_dbg(dev, "iommu: fixed addr = %lx\n", addr);
  706. }
  707. static void cell_iommu_setup_fixed_ptab(struct cbe_iommu *iommu,
  708. struct device_node *np, unsigned long dbase, unsigned long dsize,
  709. unsigned long fbase, unsigned long fsize)
  710. {
  711. unsigned long base_pte, uaddr, *io_pte;
  712. int i;
  713. dma_iommu_fixed_base = fbase;
  714. /* convert from bytes into page table indices */
  715. dbase = dbase >> IOMMU_PAGE_SHIFT;
  716. dsize = dsize >> IOMMU_PAGE_SHIFT;
  717. fbase = fbase >> IOMMU_PAGE_SHIFT;
  718. fsize = fsize >> IOMMU_PAGE_SHIFT;
  719. pr_debug("iommu: mapping 0x%lx pages from 0x%lx\n", fsize, fbase);
  720. io_pte = iommu->ptab;
  721. base_pte = IOPTE_PP_W | IOPTE_PP_R | IOPTE_M | IOPTE_SO_RW
  722. | (cell_iommu_get_ioid(np) & IOPTE_IOID_Mask);
  723. uaddr = 0;
  724. for (i = fbase; i < fbase + fsize; i++, uaddr += IOMMU_PAGE_SIZE) {
  725. /* Don't touch the dynamic region */
  726. if (i >= dbase && i < (dbase + dsize)) {
  727. pr_debug("iommu: fixed/dynamic overlap, skipping\n");
  728. continue;
  729. }
  730. io_pte[i] = base_pte | (__pa(uaddr) & IOPTE_RPN_Mask);
  731. }
  732. mb();
  733. }
  734. static int __init cell_iommu_fixed_mapping_init(void)
  735. {
  736. unsigned long dbase, dsize, fbase, fsize, hbase, hend;
  737. struct cbe_iommu *iommu;
  738. struct device_node *np;
  739. /* The fixed mapping is only supported on axon machines */
  740. np = of_find_node_by_name(NULL, "axon");
  741. if (!np) {
  742. pr_debug("iommu: fixed mapping disabled, no axons found\n");
  743. return -1;
  744. }
  745. /* We must have dma-ranges properties for fixed mapping to work */
  746. for (np = NULL; (np = of_find_all_nodes(np));) {
  747. if (of_find_property(np, "dma-ranges", NULL))
  748. break;
  749. }
  750. of_node_put(np);
  751. if (!np) {
  752. pr_debug("iommu: no dma-ranges found, no fixed mapping\n");
  753. return -1;
  754. }
  755. /* The default setup is to have the fixed mapping sit after the
  756. * dynamic region, so find the top of the largest IOMMU window
  757. * on any axon, then add the size of RAM and that's our max value.
  758. * If that is > 32GB we have to do other shennanigans.
  759. */
  760. fbase = 0;
  761. for_each_node_by_name(np, "axon") {
  762. cell_iommu_get_window(np, &dbase, &dsize);
  763. fbase = max(fbase, dbase + dsize);
  764. }
  765. fbase = _ALIGN_UP(fbase, 1 << IO_SEGMENT_SHIFT);
  766. fsize = lmb_phys_mem_size();
  767. if ((fbase + fsize) <= 0x800000000)
  768. hbase = 0; /* use the device tree window */
  769. else {
  770. /* If we're over 32 GB we need to cheat. We can't map all of
  771. * RAM with the fixed mapping, and also fit the dynamic
  772. * region. So try to place the dynamic region where the hash
  773. * table sits, drivers never need to DMA to it, we don't
  774. * need a fixed mapping for that area.
  775. */
  776. if (!htab_address) {
  777. pr_debug("iommu: htab is NULL, on LPAR? Huh?\n");
  778. return -1;
  779. }
  780. hbase = __pa(htab_address);
  781. hend = hbase + htab_size_bytes;
  782. /* The window must start and end on a segment boundary */
  783. if ((hbase != _ALIGN_UP(hbase, 1 << IO_SEGMENT_SHIFT)) ||
  784. (hend != _ALIGN_UP(hend, 1 << IO_SEGMENT_SHIFT))) {
  785. pr_debug("iommu: hash window not segment aligned\n");
  786. return -1;
  787. }
  788. /* Check the hash window fits inside the real DMA window */
  789. for_each_node_by_name(np, "axon") {
  790. cell_iommu_get_window(np, &dbase, &dsize);
  791. if (hbase < dbase || (hend > (dbase + dsize))) {
  792. pr_debug("iommu: hash window doesn't fit in"
  793. "real DMA window\n");
  794. return -1;
  795. }
  796. }
  797. fbase = 0;
  798. }
  799. /* Setup the dynamic regions */
  800. for_each_node_by_name(np, "axon") {
  801. iommu = cell_iommu_alloc(np);
  802. BUG_ON(!iommu);
  803. if (hbase == 0)
  804. cell_iommu_get_window(np, &dbase, &dsize);
  805. else {
  806. dbase = hbase;
  807. dsize = htab_size_bytes;
  808. }
  809. printk(KERN_DEBUG "iommu: node %d, dynamic window 0x%lx-0x%lx "
  810. "fixed window 0x%lx-0x%lx\n", iommu->nid, dbase,
  811. dbase + dsize, fbase, fbase + fsize);
  812. cell_iommu_setup_page_tables(iommu, dbase, dsize, fbase, fsize);
  813. cell_iommu_setup_fixed_ptab(iommu, np, dbase, dsize,
  814. fbase, fsize);
  815. cell_iommu_enable_hardware(iommu);
  816. cell_iommu_setup_window(iommu, np, dbase, dsize, 0);
  817. }
  818. dma_iommu_fixed_ops = dma_direct_ops;
  819. dma_iommu_fixed_ops.set_dma_mask = dma_set_mask_and_switch;
  820. dma_iommu_ops.set_dma_mask = dma_set_mask_and_switch;
  821. set_pci_dma_ops(&dma_iommu_ops);
  822. return 0;
  823. }
  824. static int iommu_fixed_disabled;
  825. static int __init setup_iommu_fixed(char *str)
  826. {
  827. if (strcmp(str, "off") == 0)
  828. iommu_fixed_disabled = 1;
  829. return 1;
  830. }
  831. __setup("iommu_fixed=", setup_iommu_fixed);
  832. static int __init cell_iommu_init(void)
  833. {
  834. struct device_node *np;
  835. /* If IOMMU is disabled or we have little enough RAM to not need
  836. * to enable it, we setup a direct mapping.
  837. *
  838. * Note: should we make sure we have the IOMMU actually disabled ?
  839. */
  840. if (iommu_is_off ||
  841. (!iommu_force_on && lmb_end_of_DRAM() <= 0x80000000ull))
  842. if (cell_iommu_init_disabled() == 0)
  843. goto bail;
  844. /* Setup various ppc_md. callbacks */
  845. ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
  846. ppc_md.tce_build = tce_build_cell;
  847. ppc_md.tce_free = tce_free_cell;
  848. if (!iommu_fixed_disabled && cell_iommu_fixed_mapping_init() == 0)
  849. goto bail;
  850. /* Create an iommu for each /axon node. */
  851. for_each_node_by_name(np, "axon") {
  852. if (np->parent == NULL || np->parent->parent != NULL)
  853. continue;
  854. cell_iommu_init_one(np, 0);
  855. }
  856. /* Create an iommu for each toplevel /pci-internal node for
  857. * old hardware/firmware
  858. */
  859. for_each_node_by_name(np, "pci-internal") {
  860. if (np->parent == NULL || np->parent->parent != NULL)
  861. continue;
  862. cell_iommu_init_one(np, SPIDER_DMA_OFFSET);
  863. }
  864. /* Setup default PCI iommu ops */
  865. set_pci_dma_ops(&dma_iommu_ops);
  866. bail:
  867. /* Register callbacks on OF platform device addition/removal
  868. * to handle linking them to the right DMA operations
  869. */
  870. bus_register_notifier(&of_platform_bus_type, &cell_of_bus_notifier);
  871. return 0;
  872. }
  873. machine_arch_initcall(cell, cell_iommu_init);
  874. machine_arch_initcall(celleb_native, cell_iommu_init);