iommu.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IOMMU implementation for Cell Broadband Processor Architecture
  4. *
  5. * (C) Copyright IBM Corporation 2006-2008
  6. *
  7. * Author: Jeremy Kerr <[email protected]>
  8. */
  9. #undef DEBUG
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/notifier.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/slab.h>
  19. #include <linux/memblock.h>
  20. #include <asm/prom.h>
  21. #include <asm/iommu.h>
  22. #include <asm/machdep.h>
  23. #include <asm/pci-bridge.h>
  24. #include <asm/udbg.h>
  25. #include <asm/firmware.h>
  26. #include <asm/cell-regs.h>
  27. #include "cell.h"
  28. #include "interrupt.h"
  29. /* Define CELL_IOMMU_REAL_UNMAP to actually unmap non-used pages
  30. * instead of leaving them mapped to some dummy page. This can be
  31. * enabled once the appropriate workarounds for spider bugs have
  32. * been enabled
  33. */
  34. #define CELL_IOMMU_REAL_UNMAP
  35. /* Define CELL_IOMMU_STRICT_PROTECTION to enforce protection of
  36. * IO PTEs based on the transfer direction. That can be enabled
  37. * once spider-net has been fixed to pass the correct direction
  38. * to the DMA mapping functions
  39. */
  40. #define CELL_IOMMU_STRICT_PROTECTION
  41. #define NR_IOMMUS 2
  42. /* IOC mmap registers */
  43. #define IOC_Reg_Size 0x2000
  44. #define IOC_IOPT_CacheInvd 0x908
  45. #define IOC_IOPT_CacheInvd_NE_Mask 0xffe0000000000000ul
  46. #define IOC_IOPT_CacheInvd_IOPTE_Mask 0x000003fffffffff8ul
  47. #define IOC_IOPT_CacheInvd_Busy 0x0000000000000001ul
  48. #define IOC_IOST_Origin 0x918
  49. #define IOC_IOST_Origin_E 0x8000000000000000ul
  50. #define IOC_IOST_Origin_HW 0x0000000000000800ul
  51. #define IOC_IOST_Origin_HL 0x0000000000000400ul
  52. #define IOC_IO_ExcpStat 0x920
  53. #define IOC_IO_ExcpStat_V 0x8000000000000000ul
  54. #define IOC_IO_ExcpStat_SPF_Mask 0x6000000000000000ul
  55. #define IOC_IO_ExcpStat_SPF_S 0x6000000000000000ul
  56. #define IOC_IO_ExcpStat_SPF_P 0x2000000000000000ul
  57. #define IOC_IO_ExcpStat_ADDR_Mask 0x00000007fffff000ul
  58. #define IOC_IO_ExcpStat_RW_Mask 0x0000000000000800ul
  59. #define IOC_IO_ExcpStat_IOID_Mask 0x00000000000007fful
  60. #define IOC_IO_ExcpMask 0x928
  61. #define IOC_IO_ExcpMask_SFE 0x4000000000000000ul
  62. #define IOC_IO_ExcpMask_PFE 0x2000000000000000ul
  63. #define IOC_IOCmd_Offset 0x1000
  64. #define IOC_IOCmd_Cfg 0xc00
  65. #define IOC_IOCmd_Cfg_TE 0x0000800000000000ul
  66. /* Segment table entries */
  67. #define IOSTE_V 0x8000000000000000ul /* valid */
  68. #define IOSTE_H 0x4000000000000000ul /* cache hint */
  69. #define IOSTE_PT_Base_RPN_Mask 0x3ffffffffffff000ul /* base RPN of IOPT */
  70. #define IOSTE_NPPT_Mask 0x0000000000000fe0ul /* no. pages in IOPT */
  71. #define IOSTE_PS_Mask 0x0000000000000007ul /* page size */
  72. #define IOSTE_PS_4K 0x0000000000000001ul /* - 4kB */
  73. #define IOSTE_PS_64K 0x0000000000000003ul /* - 64kB */
  74. #define IOSTE_PS_1M 0x0000000000000005ul /* - 1MB */
  75. #define IOSTE_PS_16M 0x0000000000000007ul /* - 16MB */
  76. /* IOMMU sizing */
  77. #define IO_SEGMENT_SHIFT 28
  78. #define IO_PAGENO_BITS(shift) (IO_SEGMENT_SHIFT - (shift))
  79. /* The high bit needs to be set on every DMA address */
  80. #define SPIDER_DMA_OFFSET 0x80000000ul
  81. struct iommu_window {
  82. struct list_head list;
  83. struct cbe_iommu *iommu;
  84. unsigned long offset;
  85. unsigned long size;
  86. unsigned int ioid;
  87. struct iommu_table table;
  88. };
  89. #define NAMESIZE 8
  90. struct cbe_iommu {
  91. int nid;
  92. char name[NAMESIZE];
  93. void __iomem *xlate_regs;
  94. void __iomem *cmd_regs;
  95. unsigned long *stab;
  96. unsigned long *ptab;
  97. void *pad_page;
  98. struct list_head windows;
  99. };
  100. /* Static array of iommus, one per node
  101. * each contains a list of windows, keyed from dma_window property
  102. * - on bus setup, look for a matching window, or create one
  103. * - on dev setup, assign iommu_table ptr
  104. */
  105. static struct cbe_iommu iommus[NR_IOMMUS];
  106. static int cbe_nr_iommus;
  107. static void invalidate_tce_cache(struct cbe_iommu *iommu, unsigned long *pte,
  108. long n_ptes)
  109. {
  110. u64 __iomem *reg;
  111. u64 val;
  112. long n;
  113. reg = iommu->xlate_regs + IOC_IOPT_CacheInvd;
  114. while (n_ptes > 0) {
  115. /* we can invalidate up to 1 << 11 PTEs at once */
  116. n = min(n_ptes, 1l << 11);
  117. val = (((n /*- 1*/) << 53) & IOC_IOPT_CacheInvd_NE_Mask)
  118. | (__pa(pte) & IOC_IOPT_CacheInvd_IOPTE_Mask)
  119. | IOC_IOPT_CacheInvd_Busy;
  120. out_be64(reg, val);
  121. while (in_be64(reg) & IOC_IOPT_CacheInvd_Busy)
  122. ;
  123. n_ptes -= n;
  124. pte += n;
  125. }
  126. }
  127. static int tce_build_cell(struct iommu_table *tbl, long index, long npages,
  128. unsigned long uaddr, enum dma_data_direction direction,
  129. unsigned long attrs)
  130. {
  131. int i;
  132. unsigned long *io_pte, base_pte;
  133. struct iommu_window *window =
  134. container_of(tbl, struct iommu_window, table);
  135. /* implementing proper protection causes problems with the spidernet
  136. * driver - check mapping directions later, but allow read & write by
  137. * default for now.*/
  138. #ifdef CELL_IOMMU_STRICT_PROTECTION
  139. /* to avoid referencing a global, we use a trick here to setup the
  140. * protection bit. "prot" is setup to be 3 fields of 4 bits appended
  141. * together for each of the 3 supported direction values. It is then
  142. * shifted left so that the fields matching the desired direction
  143. * lands on the appropriate bits, and other bits are masked out.
  144. */
  145. const unsigned long prot = 0xc48;
  146. base_pte =
  147. ((prot << (52 + 4 * direction)) &
  148. (CBE_IOPTE_PP_W | CBE_IOPTE_PP_R)) |
  149. CBE_IOPTE_M | CBE_IOPTE_SO_RW |
  150. (window->ioid & CBE_IOPTE_IOID_Mask);
  151. #else
  152. base_pte = CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_M |
  153. CBE_IOPTE_SO_RW | (window->ioid & CBE_IOPTE_IOID_Mask);
  154. #endif
  155. if (unlikely(attrs & DMA_ATTR_WEAK_ORDERING))
  156. base_pte &= ~CBE_IOPTE_SO_RW;
  157. io_pte = (unsigned long *)tbl->it_base + (index - tbl->it_offset);
  158. for (i = 0; i < npages; i++, uaddr += (1 << tbl->it_page_shift))
  159. io_pte[i] = base_pte | (__pa(uaddr) & CBE_IOPTE_RPN_Mask);
  160. mb();
  161. invalidate_tce_cache(window->iommu, io_pte, npages);
  162. pr_debug("tce_build_cell(index=%lx,n=%lx,dir=%d,base_pte=%lx)\n",
  163. index, npages, direction, base_pte);
  164. return 0;
  165. }
  166. static void tce_free_cell(struct iommu_table *tbl, long index, long npages)
  167. {
  168. int i;
  169. unsigned long *io_pte, pte;
  170. struct iommu_window *window =
  171. container_of(tbl, struct iommu_window, table);
  172. pr_debug("tce_free_cell(index=%lx,n=%lx)\n", index, npages);
  173. #ifdef CELL_IOMMU_REAL_UNMAP
  174. pte = 0;
  175. #else
  176. /* spider bridge does PCI reads after freeing - insert a mapping
  177. * to a scratch page instead of an invalid entry */
  178. pte = CBE_IOPTE_PP_R | CBE_IOPTE_M | CBE_IOPTE_SO_RW |
  179. __pa(window->iommu->pad_page) |
  180. (window->ioid & CBE_IOPTE_IOID_Mask);
  181. #endif
  182. io_pte = (unsigned long *)tbl->it_base + (index - tbl->it_offset);
  183. for (i = 0; i < npages; i++)
  184. io_pte[i] = pte;
  185. mb();
  186. invalidate_tce_cache(window->iommu, io_pte, npages);
  187. }
  188. static irqreturn_t ioc_interrupt(int irq, void *data)
  189. {
  190. unsigned long stat, spf;
  191. struct cbe_iommu *iommu = data;
  192. stat = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);
  193. spf = stat & IOC_IO_ExcpStat_SPF_Mask;
  194. /* Might want to rate limit it */
  195. printk(KERN_ERR "iommu: DMA exception 0x%016lx\n", stat);
  196. printk(KERN_ERR " V=%d, SPF=[%c%c], RW=%s, IOID=0x%04x\n",
  197. !!(stat & IOC_IO_ExcpStat_V),
  198. (spf == IOC_IO_ExcpStat_SPF_S) ? 'S' : ' ',
  199. (spf == IOC_IO_ExcpStat_SPF_P) ? 'P' : ' ',
  200. (stat & IOC_IO_ExcpStat_RW_Mask) ? "Read" : "Write",
  201. (unsigned int)(stat & IOC_IO_ExcpStat_IOID_Mask));
  202. printk(KERN_ERR " page=0x%016lx\n",
  203. stat & IOC_IO_ExcpStat_ADDR_Mask);
  204. /* clear interrupt */
  205. stat &= ~IOC_IO_ExcpStat_V;
  206. out_be64(iommu->xlate_regs + IOC_IO_ExcpStat, stat);
  207. return IRQ_HANDLED;
  208. }
  209. static int __init cell_iommu_find_ioc(int nid, unsigned long *base)
  210. {
  211. struct device_node *np;
  212. struct resource r;
  213. *base = 0;
  214. /* First look for new style /be nodes */
  215. for_each_node_by_name(np, "ioc") {
  216. if (of_node_to_nid(np) != nid)
  217. continue;
  218. if (of_address_to_resource(np, 0, &r)) {
  219. printk(KERN_ERR "iommu: can't get address for %pOF\n",
  220. np);
  221. continue;
  222. }
  223. *base = r.start;
  224. of_node_put(np);
  225. return 0;
  226. }
  227. /* Ok, let's try the old way */
  228. for_each_node_by_type(np, "cpu") {
  229. const unsigned int *nidp;
  230. const unsigned long *tmp;
  231. nidp = of_get_property(np, "node-id", NULL);
  232. if (nidp && *nidp == nid) {
  233. tmp = of_get_property(np, "ioc-translation", NULL);
  234. if (tmp) {
  235. *base = *tmp;
  236. of_node_put(np);
  237. return 0;
  238. }
  239. }
  240. }
  241. return -ENODEV;
  242. }
  243. static void __init cell_iommu_setup_stab(struct cbe_iommu *iommu,
  244. unsigned long dbase, unsigned long dsize,
  245. unsigned long fbase, unsigned long fsize)
  246. {
  247. struct page *page;
  248. unsigned long segments, stab_size;
  249. segments = max(dbase + dsize, fbase + fsize) >> IO_SEGMENT_SHIFT;
  250. pr_debug("%s: iommu[%d]: segments: %lu\n",
  251. __func__, iommu->nid, segments);
  252. /* set up the segment table */
  253. stab_size = segments * sizeof(unsigned long);
  254. page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(stab_size));
  255. BUG_ON(!page);
  256. iommu->stab = page_address(page);
  257. memset(iommu->stab, 0, stab_size);
  258. }
  259. static unsigned long *__init cell_iommu_alloc_ptab(struct cbe_iommu *iommu,
  260. unsigned long base, unsigned long size, unsigned long gap_base,
  261. unsigned long gap_size, unsigned long page_shift)
  262. {
  263. struct page *page;
  264. int i;
  265. unsigned long reg, segments, pages_per_segment, ptab_size,
  266. n_pte_pages, start_seg, *ptab;
  267. start_seg = base >> IO_SEGMENT_SHIFT;
  268. segments = size >> IO_SEGMENT_SHIFT;
  269. pages_per_segment = 1ull << IO_PAGENO_BITS(page_shift);
  270. /* PTEs for each segment must start on a 4K boundary */
  271. pages_per_segment = max(pages_per_segment,
  272. (1 << 12) / sizeof(unsigned long));
  273. ptab_size = segments * pages_per_segment * sizeof(unsigned long);
  274. pr_debug("%s: iommu[%d]: ptab_size: %lu, order: %d\n", __func__,
  275. iommu->nid, ptab_size, get_order(ptab_size));
  276. page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(ptab_size));
  277. BUG_ON(!page);
  278. ptab = page_address(page);
  279. memset(ptab, 0, ptab_size);
  280. /* number of 4K pages needed for a page table */
  281. n_pte_pages = (pages_per_segment * sizeof(unsigned long)) >> 12;
  282. pr_debug("%s: iommu[%d]: stab at %p, ptab at %p, n_pte_pages: %lu\n",
  283. __func__, iommu->nid, iommu->stab, ptab,
  284. n_pte_pages);
  285. /* initialise the STEs */
  286. reg = IOSTE_V | ((n_pte_pages - 1) << 5);
  287. switch (page_shift) {
  288. case 12: reg |= IOSTE_PS_4K; break;
  289. case 16: reg |= IOSTE_PS_64K; break;
  290. case 20: reg |= IOSTE_PS_1M; break;
  291. case 24: reg |= IOSTE_PS_16M; break;
  292. default: BUG();
  293. }
  294. gap_base = gap_base >> IO_SEGMENT_SHIFT;
  295. gap_size = gap_size >> IO_SEGMENT_SHIFT;
  296. pr_debug("Setting up IOMMU stab:\n");
  297. for (i = start_seg; i < (start_seg + segments); i++) {
  298. if (i >= gap_base && i < (gap_base + gap_size)) {
  299. pr_debug("\toverlap at %d, skipping\n", i);
  300. continue;
  301. }
  302. iommu->stab[i] = reg | (__pa(ptab) + (n_pte_pages << 12) *
  303. (i - start_seg));
  304. pr_debug("\t[%d] 0x%016lx\n", i, iommu->stab[i]);
  305. }
  306. return ptab;
  307. }
  308. static void __init cell_iommu_enable_hardware(struct cbe_iommu *iommu)
  309. {
  310. int ret;
  311. unsigned long reg, xlate_base;
  312. unsigned int virq;
  313. if (cell_iommu_find_ioc(iommu->nid, &xlate_base))
  314. panic("%s: missing IOC register mappings for node %d\n",
  315. __func__, iommu->nid);
  316. iommu->xlate_regs = ioremap(xlate_base, IOC_Reg_Size);
  317. iommu->cmd_regs = iommu->xlate_regs + IOC_IOCmd_Offset;
  318. /* ensure that the STEs have updated */
  319. mb();
  320. /* setup interrupts for the iommu. */
  321. reg = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);
  322. out_be64(iommu->xlate_regs + IOC_IO_ExcpStat,
  323. reg & ~IOC_IO_ExcpStat_V);
  324. out_be64(iommu->xlate_regs + IOC_IO_ExcpMask,
  325. IOC_IO_ExcpMask_PFE | IOC_IO_ExcpMask_SFE);
  326. virq = irq_create_mapping(NULL,
  327. IIC_IRQ_IOEX_ATI | (iommu->nid << IIC_IRQ_NODE_SHIFT));
  328. BUG_ON(!virq);
  329. ret = request_irq(virq, ioc_interrupt, 0, iommu->name, iommu);
  330. BUG_ON(ret);
  331. /* set the IOC segment table origin register (and turn on the iommu) */
  332. reg = IOC_IOST_Origin_E | __pa(iommu->stab) | IOC_IOST_Origin_HW;
  333. out_be64(iommu->xlate_regs + IOC_IOST_Origin, reg);
  334. in_be64(iommu->xlate_regs + IOC_IOST_Origin);
  335. /* turn on IO translation */
  336. reg = in_be64(iommu->cmd_regs + IOC_IOCmd_Cfg) | IOC_IOCmd_Cfg_TE;
  337. out_be64(iommu->cmd_regs + IOC_IOCmd_Cfg, reg);
  338. }
  339. static void __init cell_iommu_setup_hardware(struct cbe_iommu *iommu,
  340. unsigned long base, unsigned long size)
  341. {
  342. cell_iommu_setup_stab(iommu, base, size, 0, 0);
  343. iommu->ptab = cell_iommu_alloc_ptab(iommu, base, size, 0, 0,
  344. IOMMU_PAGE_SHIFT_4K);
  345. cell_iommu_enable_hardware(iommu);
  346. }
  347. #if 0/* Unused for now */
  348. static struct iommu_window *find_window(struct cbe_iommu *iommu,
  349. unsigned long offset, unsigned long size)
  350. {
  351. struct iommu_window *window;
  352. /* todo: check for overlapping (but not equal) windows) */
  353. list_for_each_entry(window, &(iommu->windows), list) {
  354. if (window->offset == offset && window->size == size)
  355. return window;
  356. }
  357. return NULL;
  358. }
  359. #endif
  360. static inline u32 cell_iommu_get_ioid(struct device_node *np)
  361. {
  362. const u32 *ioid;
  363. ioid = of_get_property(np, "ioid", NULL);
  364. if (ioid == NULL) {
  365. printk(KERN_WARNING "iommu: missing ioid for %pOF using 0\n",
  366. np);
  367. return 0;
  368. }
  369. return *ioid;
  370. }
  371. static struct iommu_table_ops cell_iommu_ops = {
  372. .set = tce_build_cell,
  373. .clear = tce_free_cell
  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. struct page *page;
  382. u32 ioid;
  383. ioid = cell_iommu_get_ioid(np);
  384. window = kzalloc_node(sizeof(*window), GFP_KERNEL, iommu->nid);
  385. BUG_ON(window == NULL);
  386. window->offset = offset;
  387. window->size = size;
  388. window->ioid = ioid;
  389. window->iommu = iommu;
  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_page_shift = IOMMU_PAGE_SHIFT_4K;
  394. window->table.it_offset =
  395. (offset >> window->table.it_page_shift) + pte_offset;
  396. window->table.it_size = size >> window->table.it_page_shift;
  397. window->table.it_ops = &cell_iommu_ops;
  398. if (!iommu_init_table(&window->table, iommu->nid, 0, 0))
  399. panic("Failed to initialize iommu table");
  400. pr_debug("\tioid %d\n", window->ioid);
  401. pr_debug("\tblocksize %ld\n", window->table.it_blocksize);
  402. pr_debug("\tbase 0x%016lx\n", window->table.it_base);
  403. pr_debug("\toffset 0x%lx\n", window->table.it_offset);
  404. pr_debug("\tsize %ld\n", window->table.it_size);
  405. list_add(&window->list, &iommu->windows);
  406. if (offset != 0)
  407. return window;
  408. /* We need to map and reserve the first IOMMU page since it's used
  409. * by the spider workaround. In theory, we only need to do that when
  410. * running on spider but it doesn't really matter.
  411. *
  412. * This code also assumes that we have a window that starts at 0,
  413. * which is the case on all spider based blades.
  414. */
  415. page = alloc_pages_node(iommu->nid, GFP_KERNEL, 0);
  416. BUG_ON(!page);
  417. iommu->pad_page = page_address(page);
  418. clear_page(iommu->pad_page);
  419. __set_bit(0, window->table.it_map);
  420. tce_build_cell(&window->table, window->table.it_offset, 1,
  421. (unsigned long)iommu->pad_page, DMA_TO_DEVICE, 0);
  422. return window;
  423. }
  424. static struct cbe_iommu *cell_iommu_for_node(int nid)
  425. {
  426. int i;
  427. for (i = 0; i < cbe_nr_iommus; i++)
  428. if (iommus[i].nid == nid)
  429. return &iommus[i];
  430. return NULL;
  431. }
  432. static unsigned long cell_dma_nommu_offset;
  433. static unsigned long dma_iommu_fixed_base;
  434. static bool cell_iommu_enabled;
  435. /* iommu_fixed_is_weak is set if booted with iommu_fixed=weak */
  436. bool iommu_fixed_is_weak;
  437. static struct iommu_table *cell_get_iommu_table(struct device *dev)
  438. {
  439. struct iommu_window *window;
  440. struct cbe_iommu *iommu;
  441. /* Current implementation uses the first window available in that
  442. * node's iommu. We -might- do something smarter later though it may
  443. * never be necessary
  444. */
  445. iommu = cell_iommu_for_node(dev_to_node(dev));
  446. if (iommu == NULL || list_empty(&iommu->windows)) {
  447. dev_err(dev, "iommu: missing iommu for %pOF (node %d)\n",
  448. dev->of_node, dev_to_node(dev));
  449. return NULL;
  450. }
  451. window = list_entry(iommu->windows.next, struct iommu_window, list);
  452. return &window->table;
  453. }
  454. static u64 cell_iommu_get_fixed_address(struct device *dev);
  455. static void cell_dma_dev_setup(struct device *dev)
  456. {
  457. if (cell_iommu_enabled) {
  458. u64 addr = cell_iommu_get_fixed_address(dev);
  459. if (addr != OF_BAD_ADDR)
  460. dev->archdata.dma_offset = addr + dma_iommu_fixed_base;
  461. set_iommu_table_base(dev, cell_get_iommu_table(dev));
  462. } else {
  463. dev->archdata.dma_offset = cell_dma_nommu_offset;
  464. }
  465. }
  466. static void cell_pci_dma_dev_setup(struct pci_dev *dev)
  467. {
  468. cell_dma_dev_setup(&dev->dev);
  469. }
  470. static int cell_of_bus_notify(struct notifier_block *nb, unsigned long action,
  471. void *data)
  472. {
  473. struct device *dev = data;
  474. /* We are only interested in device addition */
  475. if (action != BUS_NOTIFY_ADD_DEVICE)
  476. return 0;
  477. if (cell_iommu_enabled)
  478. dev->dma_ops = &dma_iommu_ops;
  479. cell_dma_dev_setup(dev);
  480. return 0;
  481. }
  482. static struct notifier_block cell_of_bus_notifier = {
  483. .notifier_call = cell_of_bus_notify
  484. };
  485. static int __init cell_iommu_get_window(struct device_node *np,
  486. unsigned long *base,
  487. unsigned long *size)
  488. {
  489. const __be32 *dma_window;
  490. unsigned long index;
  491. /* Use ibm,dma-window if available, else, hard code ! */
  492. dma_window = of_get_property(np, "ibm,dma-window", NULL);
  493. if (dma_window == NULL) {
  494. *base = 0;
  495. *size = 0x80000000u;
  496. return -ENODEV;
  497. }
  498. of_parse_dma_window(np, dma_window, &index, base, size);
  499. return 0;
  500. }
  501. static struct cbe_iommu * __init cell_iommu_alloc(struct device_node *np)
  502. {
  503. struct cbe_iommu *iommu;
  504. int nid, i;
  505. /* Get node ID */
  506. nid = of_node_to_nid(np);
  507. if (nid < 0) {
  508. printk(KERN_ERR "iommu: failed to get node for %pOF\n",
  509. np);
  510. return NULL;
  511. }
  512. pr_debug("iommu: setting up iommu for node %d (%pOF)\n",
  513. nid, np);
  514. /* XXX todo: If we can have multiple windows on the same IOMMU, which
  515. * isn't the case today, we probably want here to check whether the
  516. * iommu for that node is already setup.
  517. * However, there might be issue with getting the size right so let's
  518. * ignore that for now. We might want to completely get rid of the
  519. * multiple window support since the cell iommu supports per-page ioids
  520. */
  521. if (cbe_nr_iommus >= NR_IOMMUS) {
  522. printk(KERN_ERR "iommu: too many IOMMUs detected ! (%pOF)\n",
  523. np);
  524. return NULL;
  525. }
  526. /* Init base fields */
  527. i = cbe_nr_iommus++;
  528. iommu = &iommus[i];
  529. iommu->stab = NULL;
  530. iommu->nid = nid;
  531. snprintf(iommu->name, sizeof(iommu->name), "iommu%d", i);
  532. INIT_LIST_HEAD(&iommu->windows);
  533. return iommu;
  534. }
  535. static void __init cell_iommu_init_one(struct device_node *np,
  536. unsigned long offset)
  537. {
  538. struct cbe_iommu *iommu;
  539. unsigned long base, size;
  540. iommu = cell_iommu_alloc(np);
  541. if (!iommu)
  542. return;
  543. /* Obtain a window for it */
  544. cell_iommu_get_window(np, &base, &size);
  545. pr_debug("\ttranslating window 0x%lx...0x%lx\n",
  546. base, base + size - 1);
  547. /* Initialize the hardware */
  548. cell_iommu_setup_hardware(iommu, base, size);
  549. /* Setup the iommu_table */
  550. cell_iommu_setup_window(iommu, np, base, size,
  551. offset >> IOMMU_PAGE_SHIFT_4K);
  552. }
  553. static void __init cell_disable_iommus(void)
  554. {
  555. int node;
  556. unsigned long base, val;
  557. void __iomem *xregs, *cregs;
  558. /* Make sure IOC translation is disabled on all nodes */
  559. for_each_online_node(node) {
  560. if (cell_iommu_find_ioc(node, &base))
  561. continue;
  562. xregs = ioremap(base, IOC_Reg_Size);
  563. if (xregs == NULL)
  564. continue;
  565. cregs = xregs + IOC_IOCmd_Offset;
  566. pr_debug("iommu: cleaning up iommu on node %d\n", node);
  567. out_be64(xregs + IOC_IOST_Origin, 0);
  568. (void)in_be64(xregs + IOC_IOST_Origin);
  569. val = in_be64(cregs + IOC_IOCmd_Cfg);
  570. val &= ~IOC_IOCmd_Cfg_TE;
  571. out_be64(cregs + IOC_IOCmd_Cfg, val);
  572. (void)in_be64(cregs + IOC_IOCmd_Cfg);
  573. iounmap(xregs);
  574. }
  575. }
  576. static int __init cell_iommu_init_disabled(void)
  577. {
  578. struct device_node *np = NULL;
  579. unsigned long base = 0, size;
  580. /* When no iommu is present, we use direct DMA ops */
  581. /* First make sure all IOC translation is turned off */
  582. cell_disable_iommus();
  583. /* If we have no Axon, we set up the spider DMA magic offset */
  584. np = of_find_node_by_name(NULL, "axon");
  585. if (!np)
  586. cell_dma_nommu_offset = SPIDER_DMA_OFFSET;
  587. of_node_put(np);
  588. /* Now we need to check to see where the memory is mapped
  589. * in PCI space. We assume that all busses use the same dma
  590. * window which is always the case so far on Cell, thus we
  591. * pick up the first pci-internal node we can find and check
  592. * the DMA window from there.
  593. */
  594. for_each_node_by_name(np, "axon") {
  595. if (np->parent == NULL || np->parent->parent != NULL)
  596. continue;
  597. if (cell_iommu_get_window(np, &base, &size) == 0)
  598. break;
  599. }
  600. if (np == NULL) {
  601. for_each_node_by_name(np, "pci-internal") {
  602. if (np->parent == NULL || np->parent->parent != NULL)
  603. continue;
  604. if (cell_iommu_get_window(np, &base, &size) == 0)
  605. break;
  606. }
  607. }
  608. of_node_put(np);
  609. /* If we found a DMA window, we check if it's big enough to enclose
  610. * all of physical memory. If not, we force enable IOMMU
  611. */
  612. if (np && size < memblock_end_of_DRAM()) {
  613. printk(KERN_WARNING "iommu: force-enabled, dma window"
  614. " (%ldMB) smaller than total memory (%lldMB)\n",
  615. size >> 20, memblock_end_of_DRAM() >> 20);
  616. return -ENODEV;
  617. }
  618. cell_dma_nommu_offset += base;
  619. if (cell_dma_nommu_offset != 0)
  620. cell_pci_controller_ops.dma_dev_setup = cell_pci_dma_dev_setup;
  621. printk("iommu: disabled, direct DMA offset is 0x%lx\n",
  622. cell_dma_nommu_offset);
  623. return 0;
  624. }
  625. /*
  626. * Fixed IOMMU mapping support
  627. *
  628. * This code adds support for setting up a fixed IOMMU mapping on certain
  629. * cell machines. For 64-bit devices this avoids the performance overhead of
  630. * mapping and unmapping pages at runtime. 32-bit devices are unable to use
  631. * the fixed mapping.
  632. *
  633. * The fixed mapping is established at boot, and maps all of physical memory
  634. * 1:1 into device space at some offset. On machines with < 30 GB of memory
  635. * we setup the fixed mapping immediately above the normal IOMMU window.
  636. *
  637. * For example a machine with 4GB of memory would end up with the normal
  638. * IOMMU window from 0-2GB and the fixed mapping window from 2GB to 6GB. In
  639. * this case a 64-bit device wishing to DMA to 1GB would be told to DMA to
  640. * 3GB, plus any offset required by firmware. The firmware offset is encoded
  641. * in the "dma-ranges" property.
  642. *
  643. * On machines with 30GB or more of memory, we are unable to place the fixed
  644. * mapping above the normal IOMMU window as we would run out of address space.
  645. * Instead we move the normal IOMMU window to coincide with the hash page
  646. * table, this region does not need to be part of the fixed mapping as no
  647. * device should ever be DMA'ing to it. We then setup the fixed mapping
  648. * from 0 to 32GB.
  649. */
  650. static u64 cell_iommu_get_fixed_address(struct device *dev)
  651. {
  652. u64 cpu_addr, size, best_size, dev_addr = OF_BAD_ADDR;
  653. struct device_node *np;
  654. const u32 *ranges = NULL;
  655. int i, len, best, naddr, nsize, pna, range_size;
  656. /* We can be called for platform devices that have no of_node */
  657. np = of_node_get(dev->of_node);
  658. if (!np)
  659. goto out;
  660. while (1) {
  661. naddr = of_n_addr_cells(np);
  662. nsize = of_n_size_cells(np);
  663. np = of_get_next_parent(np);
  664. if (!np)
  665. break;
  666. ranges = of_get_property(np, "dma-ranges", &len);
  667. /* Ignore empty ranges, they imply no translation required */
  668. if (ranges && len > 0)
  669. break;
  670. }
  671. if (!ranges) {
  672. dev_dbg(dev, "iommu: no dma-ranges found\n");
  673. goto out;
  674. }
  675. len /= sizeof(u32);
  676. pna = of_n_addr_cells(np);
  677. range_size = naddr + nsize + pna;
  678. /* dma-ranges format:
  679. * child addr : naddr cells
  680. * parent addr : pna cells
  681. * size : nsize cells
  682. */
  683. for (i = 0, best = -1, best_size = 0; i < len; i += range_size) {
  684. cpu_addr = of_translate_dma_address(np, ranges + i + naddr);
  685. size = of_read_number(ranges + i + naddr + pna, nsize);
  686. if (cpu_addr == 0 && size > best_size) {
  687. best = i;
  688. best_size = size;
  689. }
  690. }
  691. if (best >= 0) {
  692. dev_addr = of_read_number(ranges + best, naddr);
  693. } else
  694. dev_dbg(dev, "iommu: no suitable range found!\n");
  695. out:
  696. of_node_put(np);
  697. return dev_addr;
  698. }
  699. static bool cell_pci_iommu_bypass_supported(struct pci_dev *pdev, u64 mask)
  700. {
  701. return mask == DMA_BIT_MASK(64) &&
  702. cell_iommu_get_fixed_address(&pdev->dev) != OF_BAD_ADDR;
  703. }
  704. static void __init insert_16M_pte(unsigned long addr, unsigned long *ptab,
  705. unsigned long base_pte)
  706. {
  707. unsigned long segment, offset;
  708. segment = addr >> IO_SEGMENT_SHIFT;
  709. offset = (addr >> 24) - (segment << IO_PAGENO_BITS(24));
  710. ptab = ptab + (segment * (1 << 12) / sizeof(unsigned long));
  711. pr_debug("iommu: addr %lx ptab %p segment %lx offset %lx\n",
  712. addr, ptab, segment, offset);
  713. ptab[offset] = base_pte | (__pa(addr) & CBE_IOPTE_RPN_Mask);
  714. }
  715. static void __init cell_iommu_setup_fixed_ptab(struct cbe_iommu *iommu,
  716. struct device_node *np, unsigned long dbase, unsigned long dsize,
  717. unsigned long fbase, unsigned long fsize)
  718. {
  719. unsigned long base_pte, uaddr, ioaddr, *ptab;
  720. ptab = cell_iommu_alloc_ptab(iommu, fbase, fsize, dbase, dsize, 24);
  721. dma_iommu_fixed_base = fbase;
  722. pr_debug("iommu: mapping 0x%lx pages from 0x%lx\n", fsize, fbase);
  723. base_pte = CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_M |
  724. (cell_iommu_get_ioid(np) & CBE_IOPTE_IOID_Mask);
  725. if (iommu_fixed_is_weak)
  726. pr_info("IOMMU: Using weak ordering for fixed mapping\n");
  727. else {
  728. pr_info("IOMMU: Using strong ordering for fixed mapping\n");
  729. base_pte |= CBE_IOPTE_SO_RW;
  730. }
  731. for (uaddr = 0; uaddr < fsize; uaddr += (1 << 24)) {
  732. /* Don't touch the dynamic region */
  733. ioaddr = uaddr + fbase;
  734. if (ioaddr >= dbase && ioaddr < (dbase + dsize)) {
  735. pr_debug("iommu: fixed/dynamic overlap, skipping\n");
  736. continue;
  737. }
  738. insert_16M_pte(uaddr, ptab, base_pte);
  739. }
  740. mb();
  741. }
  742. static int __init cell_iommu_fixed_mapping_init(void)
  743. {
  744. unsigned long dbase, dsize, fbase, fsize, hbase, hend;
  745. struct cbe_iommu *iommu;
  746. struct device_node *np;
  747. /* The fixed mapping is only supported on axon machines */
  748. np = of_find_node_by_name(NULL, "axon");
  749. of_node_put(np);
  750. if (!np) {
  751. pr_debug("iommu: fixed mapping disabled, no axons found\n");
  752. return -1;
  753. }
  754. /* We must have dma-ranges properties for fixed mapping to work */
  755. np = of_find_node_with_property(NULL, "dma-ranges");
  756. of_node_put(np);
  757. if (!np) {
  758. pr_debug("iommu: no dma-ranges found, no fixed mapping\n");
  759. return -1;
  760. }
  761. /* The default setup is to have the fixed mapping sit after the
  762. * dynamic region, so find the top of the largest IOMMU window
  763. * on any axon, then add the size of RAM and that's our max value.
  764. * If that is > 32GB we have to do other shennanigans.
  765. */
  766. fbase = 0;
  767. for_each_node_by_name(np, "axon") {
  768. cell_iommu_get_window(np, &dbase, &dsize);
  769. fbase = max(fbase, dbase + dsize);
  770. }
  771. fbase = ALIGN(fbase, 1 << IO_SEGMENT_SHIFT);
  772. fsize = memblock_phys_mem_size();
  773. if ((fbase + fsize) <= 0x800000000ul)
  774. hbase = 0; /* use the device tree window */
  775. else {
  776. /* If we're over 32 GB we need to cheat. We can't map all of
  777. * RAM with the fixed mapping, and also fit the dynamic
  778. * region. So try to place the dynamic region where the hash
  779. * table sits, drivers never need to DMA to it, we don't
  780. * need a fixed mapping for that area.
  781. */
  782. if (!htab_address) {
  783. pr_debug("iommu: htab is NULL, on LPAR? Huh?\n");
  784. return -1;
  785. }
  786. hbase = __pa(htab_address);
  787. hend = hbase + htab_size_bytes;
  788. /* The window must start and end on a segment boundary */
  789. if ((hbase != ALIGN(hbase, 1 << IO_SEGMENT_SHIFT)) ||
  790. (hend != ALIGN(hend, 1 << IO_SEGMENT_SHIFT))) {
  791. pr_debug("iommu: hash window not segment aligned\n");
  792. return -1;
  793. }
  794. /* Check the hash window fits inside the real DMA window */
  795. for_each_node_by_name(np, "axon") {
  796. cell_iommu_get_window(np, &dbase, &dsize);
  797. if (hbase < dbase || (hend > (dbase + dsize))) {
  798. pr_debug("iommu: hash window doesn't fit in"
  799. "real DMA window\n");
  800. of_node_put(np);
  801. return -1;
  802. }
  803. }
  804. fbase = 0;
  805. }
  806. /* Setup the dynamic regions */
  807. for_each_node_by_name(np, "axon") {
  808. iommu = cell_iommu_alloc(np);
  809. BUG_ON(!iommu);
  810. if (hbase == 0)
  811. cell_iommu_get_window(np, &dbase, &dsize);
  812. else {
  813. dbase = hbase;
  814. dsize = htab_size_bytes;
  815. }
  816. printk(KERN_DEBUG "iommu: node %d, dynamic window 0x%lx-0x%lx "
  817. "fixed window 0x%lx-0x%lx\n", iommu->nid, dbase,
  818. dbase + dsize, fbase, fbase + fsize);
  819. cell_iommu_setup_stab(iommu, dbase, dsize, fbase, fsize);
  820. iommu->ptab = cell_iommu_alloc_ptab(iommu, dbase, dsize, 0, 0,
  821. IOMMU_PAGE_SHIFT_4K);
  822. cell_iommu_setup_fixed_ptab(iommu, np, dbase, dsize,
  823. fbase, fsize);
  824. cell_iommu_enable_hardware(iommu);
  825. cell_iommu_setup_window(iommu, np, dbase, dsize, 0);
  826. }
  827. cell_pci_controller_ops.iommu_bypass_supported =
  828. cell_pci_iommu_bypass_supported;
  829. return 0;
  830. }
  831. static int iommu_fixed_disabled;
  832. static int __init setup_iommu_fixed(char *str)
  833. {
  834. struct device_node *pciep;
  835. if (strcmp(str, "off") == 0)
  836. iommu_fixed_disabled = 1;
  837. /* If we can find a pcie-endpoint in the device tree assume that
  838. * we're on a triblade or a CAB so by default the fixed mapping
  839. * should be set to be weakly ordered; but only if the boot
  840. * option WASN'T set for strong ordering
  841. */
  842. pciep = of_find_node_by_type(NULL, "pcie-endpoint");
  843. if (strcmp(str, "weak") == 0 || (pciep && strcmp(str, "strong") != 0))
  844. iommu_fixed_is_weak = true;
  845. of_node_put(pciep);
  846. return 1;
  847. }
  848. __setup("iommu_fixed=", setup_iommu_fixed);
  849. static int __init cell_iommu_init(void)
  850. {
  851. struct device_node *np;
  852. /* If IOMMU is disabled or we have little enough RAM to not need
  853. * to enable it, we setup a direct mapping.
  854. *
  855. * Note: should we make sure we have the IOMMU actually disabled ?
  856. */
  857. if (iommu_is_off ||
  858. (!iommu_force_on && memblock_end_of_DRAM() <= 0x80000000ull))
  859. if (cell_iommu_init_disabled() == 0)
  860. goto bail;
  861. /* Setup various callbacks */
  862. cell_pci_controller_ops.dma_dev_setup = cell_pci_dma_dev_setup;
  863. if (!iommu_fixed_disabled && cell_iommu_fixed_mapping_init() == 0)
  864. goto done;
  865. /* Create an iommu for each /axon node. */
  866. for_each_node_by_name(np, "axon") {
  867. if (np->parent == NULL || np->parent->parent != NULL)
  868. continue;
  869. cell_iommu_init_one(np, 0);
  870. }
  871. /* Create an iommu for each toplevel /pci-internal node for
  872. * old hardware/firmware
  873. */
  874. for_each_node_by_name(np, "pci-internal") {
  875. if (np->parent == NULL || np->parent->parent != NULL)
  876. continue;
  877. cell_iommu_init_one(np, SPIDER_DMA_OFFSET);
  878. }
  879. done:
  880. /* Setup default PCI iommu ops */
  881. set_pci_dma_ops(&dma_iommu_ops);
  882. cell_iommu_enabled = true;
  883. bail:
  884. /* Register callbacks on OF platform device addition/removal
  885. * to handle linking them to the right DMA operations
  886. */
  887. bus_register_notifier(&platform_bus_type, &cell_of_bus_notifier);
  888. return 0;
  889. }
  890. machine_arch_initcall(cell, cell_iommu_init);