iommu.c 30 KB

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