iommu.c 35 KB

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