iommu.c 34 KB

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