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