iommu.c 35 KB

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