iommu.c 34.5 KB
Newer Older
1
/*
2
 * IOMMU implementation for Cell Broadband Processor Architecture
3
 *
4
 * (C) Copyright IBM Corporation 2006-2008
5
 *
J
Jeremy Kerr 已提交
6
 * Author: Jeremy Kerr <jk@ozlabs.org>
7
 *
J
Jeremy Kerr 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 22 23 24 25 26
 */

#undef DEBUG

#include <linux/kernel.h>
#include <linux/init.h>
J
Jeremy Kerr 已提交
27 28
#include <linux/interrupt.h>
#include <linux/notifier.h>
29
#include <linux/of.h>
30
#include <linux/of_platform.h>
31
#include <linux/lmb.h>
32 33

#include <asm/prom.h>
J
Jeremy Kerr 已提交
34
#include <asm/iommu.h>
35
#include <asm/machdep.h>
J
Jeremy Kerr 已提交
36
#include <asm/pci-bridge.h>
37
#include <asm/udbg.h>
38
#include <asm/firmware.h>
39
#include <asm/cell-regs.h>
40

J
Jeremy Kerr 已提交
41
#include "interrupt.h"
42

J
Jeremy Kerr 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/* Define CELL_IOMMU_REAL_UNMAP to actually unmap non-used pages
 * instead of leaving them mapped to some dummy page. This can be
 * enabled once the appropriate workarounds for spider bugs have
 * been enabled
 */
#define CELL_IOMMU_REAL_UNMAP

/* Define CELL_IOMMU_STRICT_PROTECTION to enforce protection of
 * IO PTEs based on the transfer direction. That can be enabled
 * once spider-net has been fixed to pass the correct direction
 * to the DMA mapping functions
 */
#define CELL_IOMMU_STRICT_PROTECTION


#define NR_IOMMUS			2

/* IOC mmap registers */
#define IOC_Reg_Size			0x2000

#define IOC_IOPT_CacheInvd		0x908
#define IOC_IOPT_CacheInvd_NE_Mask	0xffe0000000000000ul
#define IOC_IOPT_CacheInvd_IOPTE_Mask	0x000003fffffffff8ul
#define IOC_IOPT_CacheInvd_Busy		0x0000000000000001ul

#define IOC_IOST_Origin			0x918
#define IOC_IOST_Origin_E		0x8000000000000000ul
#define IOC_IOST_Origin_HW		0x0000000000000800ul
#define IOC_IOST_Origin_HL		0x0000000000000400ul

#define IOC_IO_ExcpStat			0x920
#define IOC_IO_ExcpStat_V		0x8000000000000000ul
#define IOC_IO_ExcpStat_SPF_Mask	0x6000000000000000ul
#define IOC_IO_ExcpStat_SPF_S		0x6000000000000000ul
#define IOC_IO_ExcpStat_SPF_P		0x4000000000000000ul
#define IOC_IO_ExcpStat_ADDR_Mask	0x00000007fffff000ul
#define IOC_IO_ExcpStat_RW_Mask		0x0000000000000800ul
#define IOC_IO_ExcpStat_IOID_Mask	0x00000000000007fful

#define IOC_IO_ExcpMask			0x928
#define IOC_IO_ExcpMask_SFE		0x4000000000000000ul
#define IOC_IO_ExcpMask_PFE		0x2000000000000000ul

#define IOC_IOCmd_Offset		0x1000

#define IOC_IOCmd_Cfg			0xc00
#define IOC_IOCmd_Cfg_TE		0x0000800000000000ul


/* Segment table entries */
#define IOSTE_V			0x8000000000000000ul /* valid */
#define IOSTE_H			0x4000000000000000ul /* cache hint */
#define IOSTE_PT_Base_RPN_Mask  0x3ffffffffffff000ul /* base RPN of IOPT */
#define IOSTE_NPPT_Mask		0x0000000000000fe0ul /* no. pages in IOPT */
#define IOSTE_PS_Mask		0x0000000000000007ul /* page size */
#define IOSTE_PS_4K		0x0000000000000001ul /*   - 4kB  */
#define IOSTE_PS_64K		0x0000000000000003ul /*   - 64kB */
#define IOSTE_PS_1M		0x0000000000000005ul /*   - 1MB  */
#define IOSTE_PS_16M		0x0000000000000007ul /*   - 16MB */

/* Page table entries */
#define IOPTE_PP_W		0x8000000000000000ul /* protection: write */
#define IOPTE_PP_R		0x4000000000000000ul /* protection: read */
#define IOPTE_M			0x2000000000000000ul /* coherency required */
#define IOPTE_SO_R		0x1000000000000000ul /* ordering: writes */
#define IOPTE_SO_RW             0x1800000000000000ul /* ordering: r & w */
#define IOPTE_RPN_Mask		0x07fffffffffff000ul /* RPN */
#define IOPTE_H			0x0000000000000800ul /* cache hint */
#define IOPTE_IOID_Mask		0x00000000000007fful /* ioid */


/* IOMMU sizing */
#define IO_SEGMENT_SHIFT	28
116
#define IO_PAGENO_BITS(shift)	(IO_SEGMENT_SHIFT - (shift))
J
Jeremy Kerr 已提交
117 118 119 120 121 122 123 124 125 126 127 128

/* The high bit needs to be set on every DMA address */
#define SPIDER_DMA_OFFSET	0x80000000ul

struct iommu_window {
	struct list_head list;
	struct cbe_iommu *iommu;
	unsigned long offset;
	unsigned long size;
	unsigned int ioid;
	struct iommu_table table;
};
129

J
Jeremy Kerr 已提交
130 131 132 133 134 135 136 137 138 139 140
#define NAMESIZE 8
struct cbe_iommu {
	int nid;
	char name[NAMESIZE];
	void __iomem *xlate_regs;
	void __iomem *cmd_regs;
	unsigned long *stab;
	unsigned long *ptab;
	void *pad_page;
	struct list_head windows;
};
141

J
Jeremy Kerr 已提交
142 143 144 145 146 147 148
/* Static array of iommus, one per node
 *   each contains a list of windows, keyed from dma_window property
 *   - on bus setup, look for a matching window, or create one
 *   - on dev setup, assign iommu_table ptr
 */
static struct cbe_iommu iommus[NR_IOMMUS];
static int cbe_nr_iommus;
149

J
Jeremy Kerr 已提交
150 151
static void invalidate_tce_cache(struct cbe_iommu *iommu, unsigned long *pte,
		long n_ptes)
152
{
153 154
	unsigned long __iomem *reg;
	unsigned long val;
J
Jeremy Kerr 已提交
155
	long n;
156

J
Jeremy Kerr 已提交
157
	reg = iommu->xlate_regs + IOC_IOPT_CacheInvd;
158

J
Jeremy Kerr 已提交
159 160 161 162 163 164
	while (n_ptes > 0) {
		/* we can invalidate up to 1 << 11 PTEs at once */
		n = min(n_ptes, 1l << 11);
		val = (((n /*- 1*/) << 53) & IOC_IOPT_CacheInvd_NE_Mask)
			| (__pa(pte) & IOC_IOPT_CacheInvd_IOPTE_Mask)
		        | IOC_IOPT_CacheInvd_Busy;
165

J
Jeremy Kerr 已提交
166 167 168
		out_be64(reg, val);
		while (in_be64(reg) & IOC_IOPT_CacheInvd_Busy)
			;
169

J
Jeremy Kerr 已提交
170 171 172
		n_ptes -= n;
		pte += n;
	}
173 174
}

175
static int tce_build_cell(struct iommu_table *tbl, long index, long npages,
176 177
		unsigned long uaddr, enum dma_data_direction direction,
		struct dma_attrs *attrs)
178
{
J
Jeremy Kerr 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	int i;
	unsigned long *io_pte, base_pte;
	struct iommu_window *window =
		container_of(tbl, struct iommu_window, table);

	/* implementing proper protection causes problems with the spidernet
	 * driver - check mapping directions later, but allow read & write by
	 * default for now.*/
#ifdef CELL_IOMMU_STRICT_PROTECTION
	/* to avoid referencing a global, we use a trick here to setup the
	 * protection bit. "prot" is setup to be 3 fields of 4 bits apprended
	 * together for each of the 3 supported direction values. It is then
	 * shifted left so that the fields matching the desired direction
	 * lands on the appropriate bits, and other bits are masked out.
	 */
	const unsigned long prot = 0xc48;
	base_pte =
		((prot << (52 + 4 * direction)) & (IOPTE_PP_W | IOPTE_PP_R))
		| IOPTE_M | IOPTE_SO_RW | (window->ioid & IOPTE_IOID_Mask);
#else
	base_pte = IOPTE_PP_W | IOPTE_PP_R | IOPTE_M | IOPTE_SO_RW |
		(window->ioid & IOPTE_IOID_Mask);
#endif
202 203
	if (unlikely(dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs)))
		base_pte &= ~IOPTE_SO_RW;
J
Jeremy Kerr 已提交
204

205
	io_pte = (unsigned long *)tbl->it_base + (index - tbl->it_offset);
J
Jeremy Kerr 已提交
206 207 208 209 210 211 212 213 214 215

	for (i = 0; i < npages; i++, uaddr += IOMMU_PAGE_SIZE)
		io_pte[i] = base_pte | (__pa(uaddr) & IOPTE_RPN_Mask);

	mb();

	invalidate_tce_cache(window->iommu, io_pte, npages);

	pr_debug("tce_build_cell(index=%lx,n=%lx,dir=%d,base_pte=%lx)\n",
		 index, npages, direction, base_pte);
216
	return 0;
217 218
}

J
Jeremy Kerr 已提交
219
static void tce_free_cell(struct iommu_table *tbl, long index, long npages)
220 221
{

J
Jeremy Kerr 已提交
222 223 224 225
	int i;
	unsigned long *io_pte, pte;
	struct iommu_window *window =
		container_of(tbl, struct iommu_window, table);
226

J
Jeremy Kerr 已提交
227
	pr_debug("tce_free_cell(index=%lx,n=%lx)\n", index, npages);
228

J
Jeremy Kerr 已提交
229 230 231 232 233 234 235 236
#ifdef CELL_IOMMU_REAL_UNMAP
	pte = 0;
#else
	/* spider bridge does PCI reads after freeing - insert a mapping
	 * to a scratch page instead of an invalid entry */
	pte = IOPTE_PP_R | IOPTE_M | IOPTE_SO_RW | __pa(window->iommu->pad_page)
		| (window->ioid & IOPTE_IOID_Mask);
#endif
237

238
	io_pte = (unsigned long *)tbl->it_base + (index - tbl->it_offset);
239

J
Jeremy Kerr 已提交
240 241 242 243
	for (i = 0; i < npages; i++)
		io_pte[i] = pte;

	mb();
244

J
Jeremy Kerr 已提交
245
	invalidate_tce_cache(window->iommu, io_pte, npages);
246 247
}

J
Jeremy Kerr 已提交
248
static irqreturn_t ioc_interrupt(int irq, void *data)
249
{
J
Jeremy Kerr 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	unsigned long stat;
	struct cbe_iommu *iommu = data;

	stat = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);

	/* Might want to rate limit it */
	printk(KERN_ERR "iommu: DMA exception 0x%016lx\n", stat);
	printk(KERN_ERR "  V=%d, SPF=[%c%c], RW=%s, IOID=0x%04x\n",
	       !!(stat & IOC_IO_ExcpStat_V),
	       (stat & IOC_IO_ExcpStat_SPF_S) ? 'S' : ' ',
	       (stat & IOC_IO_ExcpStat_SPF_P) ? 'P' : ' ',
	       (stat & IOC_IO_ExcpStat_RW_Mask) ? "Read" : "Write",
	       (unsigned int)(stat & IOC_IO_ExcpStat_IOID_Mask));
	printk(KERN_ERR "  page=0x%016lx\n",
	       stat & IOC_IO_ExcpStat_ADDR_Mask);

	/* clear interrupt */
	stat &= ~IOC_IO_ExcpStat_V;
	out_be64(iommu->xlate_regs + IOC_IO_ExcpStat, stat);

	return IRQ_HANDLED;
271 272
}

J
Jeremy Kerr 已提交
273
static int cell_iommu_find_ioc(int nid, unsigned long *base)
274
{
J
Jeremy Kerr 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	struct device_node *np;
	struct resource r;

	*base = 0;

	/* First look for new style /be nodes */
	for_each_node_by_name(np, "ioc") {
		if (of_node_to_nid(np) != nid)
			continue;
		if (of_address_to_resource(np, 0, &r)) {
			printk(KERN_ERR "iommu: can't get address for %s\n",
			       np->full_name);
			continue;
		}
		*base = r.start;
		of_node_put(np);
		return 0;
	}

	/* Ok, let's try the old way */
	for_each_node_by_type(np, "cpu") {
		const unsigned int *nidp;
		const unsigned long *tmp;

299
		nidp = of_get_property(np, "node-id", NULL);
J
Jeremy Kerr 已提交
300
		if (nidp && *nidp == nid) {
301
			tmp = of_get_property(np, "ioc-translation", NULL);
J
Jeremy Kerr 已提交
302 303 304 305 306 307 308
			if (tmp) {
				*base = *tmp;
				of_node_put(np);
				return 0;
			}
		}
	}
309

J
Jeremy Kerr 已提交
310
	return -ENODEV;
311 312
}

313
static void cell_iommu_setup_stab(struct cbe_iommu *iommu,
314 315
				unsigned long dbase, unsigned long dsize,
				unsigned long fbase, unsigned long fsize)
316
{
J
Jeremy Kerr 已提交
317
	struct page *page;
318
	unsigned long segments, stab_size;
319 320

	segments = max(dbase + dsize, fbase + fsize) >> IO_SEGMENT_SHIFT;
J
Jeremy Kerr 已提交
321

322
	pr_debug("%s: iommu[%d]: segments: %lu\n",
323
			__func__, iommu->nid, segments);
J
Jeremy Kerr 已提交
324 325

	/* set up the segment table */
326 327
	stab_size = segments * sizeof(unsigned long);
	page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(stab_size));
J
Jeremy Kerr 已提交
328 329
	BUG_ON(!page);
	iommu->stab = page_address(page);
330 331 332 333 334
	memset(iommu->stab, 0, stab_size);
}

static unsigned long *cell_iommu_alloc_ptab(struct cbe_iommu *iommu,
		unsigned long base, unsigned long size, unsigned long gap_base,
335
		unsigned long gap_size, unsigned long page_shift)
336 337 338 339 340 341 342 343
{
	struct page *page;
	int i;
	unsigned long reg, segments, pages_per_segment, ptab_size,
		      n_pte_pages, start_seg, *ptab;

	start_seg = base >> IO_SEGMENT_SHIFT;
	segments  = size >> IO_SEGMENT_SHIFT;
344 345 346 347
	pages_per_segment = 1ull << IO_PAGENO_BITS(page_shift);
	/* PTEs for each segment must start on a 4K bounday */
	pages_per_segment = max(pages_per_segment,
				(1 << 12) / sizeof(unsigned long));
J
Jeremy Kerr 已提交
348 349

	ptab_size = segments * pages_per_segment * sizeof(unsigned long);
350
	pr_debug("%s: iommu[%d]: ptab_size: %lu, order: %d\n", __func__,
J
Jeremy Kerr 已提交
351 352 353 354
			iommu->nid, ptab_size, get_order(ptab_size));
	page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(ptab_size));
	BUG_ON(!page);

355 356
	ptab = page_address(page);
	memset(ptab, 0, ptab_size);
J
Jeremy Kerr 已提交
357

358 359
	/* number of 4K pages needed for a page table */
	n_pte_pages = (pages_per_segment * sizeof(unsigned long)) >> 12;
J
Jeremy Kerr 已提交
360 361

	pr_debug("%s: iommu[%d]: stab at %p, ptab at %p, n_pte_pages: %lu\n",
362
			__func__, iommu->nid, iommu->stab, ptab,
J
Jeremy Kerr 已提交
363 364 365 366 367
			n_pte_pages);

	/* initialise the STEs */
	reg = IOSTE_V | ((n_pte_pages - 1) << 5);

368 369 370 371 372 373
	switch (page_shift) {
	case 12: reg |= IOSTE_PS_4K;  break;
	case 16: reg |= IOSTE_PS_64K; break;
	case 20: reg |= IOSTE_PS_1M;  break;
	case 24: reg |= IOSTE_PS_16M; break;
	default: BUG();
J
Jeremy Kerr 已提交
374
	}
375

376 377 378
	gap_base = gap_base >> IO_SEGMENT_SHIFT;
	gap_size = gap_size >> IO_SEGMENT_SHIFT;

J
Jeremy Kerr 已提交
379
	pr_debug("Setting up IOMMU stab:\n");
380 381 382 383 384
	for (i = start_seg; i < (start_seg + segments); i++) {
		if (i >= gap_base && i < (gap_base + gap_size)) {
			pr_debug("\toverlap at %d, skipping\n", i);
			continue;
		}
385 386
		iommu->stab[i] = reg | (__pa(ptab) + (n_pte_pages << 12) *
					(i - start_seg));
J
Jeremy Kerr 已提交
387 388
		pr_debug("\t[%d] 0x%016lx\n", i, iommu->stab[i]);
	}
389 390

	return ptab;
391 392 393 394 395 396 397 398 399 400
}

static void cell_iommu_enable_hardware(struct cbe_iommu *iommu)
{
	int ret;
	unsigned long reg, xlate_base;
	unsigned int virq;

	if (cell_iommu_find_ioc(iommu->nid, &xlate_base))
		panic("%s: missing IOC register mappings for node %d\n",
401
		      __func__, iommu->nid);
402 403 404

	iommu->xlate_regs = ioremap(xlate_base, IOC_Reg_Size);
	iommu->cmd_regs = iommu->xlate_regs + IOC_IOCmd_Offset;
J
Jeremy Kerr 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

	/* ensure that the STEs have updated */
	mb();

	/* setup interrupts for the iommu. */
	reg = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);
	out_be64(iommu->xlate_regs + IOC_IO_ExcpStat,
			reg & ~IOC_IO_ExcpStat_V);
	out_be64(iommu->xlate_regs + IOC_IO_ExcpMask,
			IOC_IO_ExcpMask_PFE | IOC_IO_ExcpMask_SFE);

	virq = irq_create_mapping(NULL,
			IIC_IRQ_IOEX_ATI | (iommu->nid << IIC_IRQ_NODE_SHIFT));
	BUG_ON(virq == NO_IRQ);

	ret = request_irq(virq, ioc_interrupt, IRQF_DISABLED,
			iommu->name, iommu);
	BUG_ON(ret);
423

J
Jeremy Kerr 已提交
424 425 426 427
	/* set the IOC segment table origin register (and turn on the iommu) */
	reg = IOC_IOST_Origin_E | __pa(iommu->stab) | IOC_IOST_Origin_HW;
	out_be64(iommu->xlate_regs + IOC_IOST_Origin, reg);
	in_be64(iommu->xlate_regs + IOC_IOST_Origin);
428

J
Jeremy Kerr 已提交
429 430 431 432 433
	/* turn on IO translation */
	reg = in_be64(iommu->cmd_regs + IOC_IOCmd_Cfg) | IOC_IOCmd_Cfg_TE;
	out_be64(iommu->cmd_regs + IOC_IOCmd_Cfg, reg);
}

434 435 436
static void cell_iommu_setup_hardware(struct cbe_iommu *iommu,
	unsigned long base, unsigned long size)
{
437
	cell_iommu_setup_stab(iommu, base, size, 0, 0);
438 439
	iommu->ptab = cell_iommu_alloc_ptab(iommu, base, size, 0, 0,
					    IOMMU_PAGE_SHIFT);
440 441 442
	cell_iommu_enable_hardware(iommu);
}

J
Jeremy Kerr 已提交
443 444 445
#if 0/* Unused for now */
static struct iommu_window *find_window(struct cbe_iommu *iommu,
		unsigned long offset, unsigned long size)
446
{
J
Jeremy Kerr 已提交
447
	struct iommu_window *window;
448

J
Jeremy Kerr 已提交
449 450 451 452 453
	/* todo: check for overlapping (but not equal) windows) */

	list_for_each_entry(window, &(iommu->windows), list) {
		if (window->offset == offset && window->size == size)
			return window;
454
	}
J
Jeremy Kerr 已提交
455 456

	return NULL;
457
}
J
Jeremy Kerr 已提交
458
#endif
459

460 461 462 463 464 465 466 467 468 469 470 471 472 473
static inline u32 cell_iommu_get_ioid(struct device_node *np)
{
	const u32 *ioid;

	ioid = of_get_property(np, "ioid", NULL);
	if (ioid == NULL) {
		printk(KERN_WARNING "iommu: missing ioid for %s using 0\n",
		       np->full_name);
		return 0;
	}

	return *ioid;
}

J
Jeremy Kerr 已提交
474 475 476 477
static struct iommu_window * __init
cell_iommu_setup_window(struct cbe_iommu *iommu, struct device_node *np,
			unsigned long offset, unsigned long size,
			unsigned long pte_offset)
478
{
J
Jeremy Kerr 已提交
479
	struct iommu_window *window;
480
	struct page *page;
481
	u32 ioid;
482

483
	ioid = cell_iommu_get_ioid(np);
J
Jeremy Kerr 已提交
484 485 486 487 488 489

	window = kmalloc_node(sizeof(*window), GFP_KERNEL, iommu->nid);
	BUG_ON(window == NULL);

	window->offset = offset;
	window->size = size;
490
	window->ioid = ioid;
J
Jeremy Kerr 已提交
491 492 493 494 495
	window->iommu = iommu;

	window->table.it_blocksize = 16;
	window->table.it_base = (unsigned long)iommu->ptab;
	window->table.it_index = iommu->nid;
496
	window->table.it_offset = (offset >> IOMMU_PAGE_SHIFT) + pte_offset;
J
Jeremy Kerr 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
	window->table.it_size = size >> IOMMU_PAGE_SHIFT;

	iommu_init_table(&window->table, iommu->nid);

	pr_debug("\tioid      %d\n", window->ioid);
	pr_debug("\tblocksize %ld\n", window->table.it_blocksize);
	pr_debug("\tbase      0x%016lx\n", window->table.it_base);
	pr_debug("\toffset    0x%lx\n", window->table.it_offset);
	pr_debug("\tsize      %ld\n", window->table.it_size);

	list_add(&window->list, &iommu->windows);

	if (offset != 0)
		return window;

	/* We need to map and reserve the first IOMMU page since it's used
	 * by the spider workaround. In theory, we only need to do that when
	 * running on spider but it doesn't really matter.
	 *
	 * This code also assumes that we have a window that starts at 0,
	 * which is the case on all spider based blades.
	 */
519 520 521 522 523
	page = alloc_pages_node(iommu->nid, GFP_KERNEL, 0);
	BUG_ON(!page);
	iommu->pad_page = page_address(page);
	clear_page(iommu->pad_page);

J
Jeremy Kerr 已提交
524 525
	__set_bit(0, window->table.it_map);
	tce_build_cell(&window->table, window->table.it_offset, 1,
526
		       (unsigned long)iommu->pad_page, DMA_TO_DEVICE, NULL);
J
Jeremy Kerr 已提交
527 528 529 530
	window->table.it_hint = window->table.it_blocksize;

	return window;
}
531

J
Jeremy Kerr 已提交
532 533 534
static struct cbe_iommu *cell_iommu_for_node(int nid)
{
	int i;
535

J
Jeremy Kerr 已提交
536 537 538 539 540
	for (i = 0; i < cbe_nr_iommus; i++)
		if (iommus[i].nid == nid)
			return &iommus[i];
	return NULL;
}
541

542 543
static unsigned long cell_dma_direct_offset;

544
static unsigned long dma_iommu_fixed_base;
545 546 547

/* iommu_fixed_is_weak is set if booted with iommu_fixed=weak */
static int iommu_fixed_is_weak;
548

549
static struct iommu_table *cell_get_iommu_table(struct device *dev)
J
Jeremy Kerr 已提交
550 551 552 553 554 555 556 557 558
{
	struct iommu_window *window;
	struct cbe_iommu *iommu;
	struct dev_archdata *archdata = &dev->archdata;

	/* Current implementation uses the first window available in that
	 * node's iommu. We -might- do something smarter later though it may
	 * never be necessary
	 */
B
Becky Bruce 已提交
559
	iommu = cell_iommu_for_node(dev_to_node(dev));
J
Jeremy Kerr 已提交
560 561 562
	if (iommu == NULL || list_empty(&iommu->windows)) {
		printk(KERN_ERR "iommu: missing iommu for %s (node %d)\n",
		       archdata->of_node ? archdata->of_node->full_name : "?",
B
Becky Bruce 已提交
563
		       dev_to_node(dev));
564
		return NULL;
J
Jeremy Kerr 已提交
565 566
	}
	window = list_entry(iommu->windows.next, struct iommu_window, list);
567

568
	return &window->table;
569 570
}

571 572 573 574 575 576 577 578 579
/* A coherent allocation implies strong ordering */

static void *dma_fixed_alloc_coherent(struct device *dev, size_t size,
				      dma_addr_t *dma_handle, gfp_t flag)
{
	if (iommu_fixed_is_weak)
		return iommu_alloc_coherent(dev, cell_get_iommu_table(dev),
					    size, dma_handle,
					    device_to_mask(dev), flag,
B
Becky Bruce 已提交
580
					    dev_to_node(dev));
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
	else
		return dma_direct_ops.alloc_coherent(dev, size, dma_handle,
						     flag);
}

static void dma_fixed_free_coherent(struct device *dev, size_t size,
				    void *vaddr, dma_addr_t dma_handle)
{
	if (iommu_fixed_is_weak)
		iommu_free_coherent(cell_get_iommu_table(dev), size, vaddr,
				    dma_handle);
	else
		dma_direct_ops.free_coherent(dev, size, vaddr, dma_handle);
}

596 597 598 599
static dma_addr_t dma_fixed_map_page(struct device *dev, struct page *page,
				     unsigned long offset, size_t size,
				     enum dma_data_direction direction,
				     struct dma_attrs *attrs)
600 601
{
	if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))
602 603
		return dma_direct_ops.map_page(dev, page, offset, size,
					       direction, attrs);
604
	else
605 606 607
		return iommu_map_page(dev, cell_get_iommu_table(dev), page,
				      offset, size, device_to_mask(dev),
				      direction, attrs);
608 609
}

610 611 612
static void dma_fixed_unmap_page(struct device *dev, dma_addr_t dma_addr,
				 size_t size, enum dma_data_direction direction,
				 struct dma_attrs *attrs)
613 614
{
	if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))
615 616
		dma_direct_ops.unmap_page(dev, dma_addr, size, direction,
					  attrs);
617
	else
618 619
		iommu_unmap_page(cell_get_iommu_table(dev), dma_addr, size,
				 direction, attrs);
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
}

static int dma_fixed_map_sg(struct device *dev, struct scatterlist *sg,
			   int nents, enum dma_data_direction direction,
			   struct dma_attrs *attrs)
{
	if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))
		return dma_direct_ops.map_sg(dev, sg, nents, direction, attrs);
	else
		return iommu_map_sg(dev, cell_get_iommu_table(dev), sg, nents,
				    device_to_mask(dev), direction, attrs);
}

static void dma_fixed_unmap_sg(struct device *dev, struct scatterlist *sg,
			       int nents, enum dma_data_direction direction,
			       struct dma_attrs *attrs)
{
	if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))
		dma_direct_ops.unmap_sg(dev, sg, nents, direction, attrs);
	else
		iommu_unmap_sg(cell_get_iommu_table(dev), sg, nents, direction,
			       attrs);
}

static int dma_fixed_dma_supported(struct device *dev, u64 mask)
{
	return mask == DMA_64BIT_MASK;
}

static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask);

struct dma_mapping_ops dma_iommu_fixed_ops = {
	.alloc_coherent = dma_fixed_alloc_coherent,
	.free_coherent  = dma_fixed_free_coherent,
	.map_sg         = dma_fixed_map_sg,
	.unmap_sg       = dma_fixed_unmap_sg,
	.dma_supported  = dma_fixed_dma_supported,
	.set_dma_mask   = dma_set_mask_and_switch,
658 659
	.map_page       = dma_fixed_map_page,
	.unmap_page     = dma_fixed_unmap_page,
660 661
};

662
static void cell_dma_dev_setup_fixed(struct device *dev);
663

664 665 666 667
static void cell_dma_dev_setup(struct device *dev)
{
	struct dev_archdata *archdata = &dev->archdata;

668 669
	/* Order is important here, these are not mutually exclusive */
	if (get_dma_ops(dev) == &dma_iommu_fixed_ops)
670
		cell_dma_dev_setup_fixed(dev);
671
	else if (get_pci_dma_ops() == &dma_iommu_ops)
672
		archdata->dma_data = cell_get_iommu_table(dev);
673 674 675 676 677 678
	else if (get_pci_dma_ops() == &dma_direct_ops)
		archdata->dma_data = (void *)cell_dma_direct_offset;
	else
		BUG();
}

J
Jeremy Kerr 已提交
679
static void cell_pci_dma_dev_setup(struct pci_dev *dev)
680
{
J
Jeremy Kerr 已提交
681 682
	cell_dma_dev_setup(&dev->dev);
}
683

J
Jeremy Kerr 已提交
684 685 686 687
static int cell_of_bus_notify(struct notifier_block *nb, unsigned long action,
			      void *data)
{
	struct device *dev = data;
688

J
Jeremy Kerr 已提交
689 690 691
	/* We are only intereted in device addition */
	if (action != BUS_NOTIFY_ADD_DEVICE)
		return 0;
692

J
Jeremy Kerr 已提交
693
	/* We use the PCI DMA ops */
694
	dev->archdata.dma_ops = get_pci_dma_ops();
695

J
Jeremy Kerr 已提交
696
	cell_dma_dev_setup(dev);
697

J
Jeremy Kerr 已提交
698 699
	return 0;
}
700

J
Jeremy Kerr 已提交
701 702 703
static struct notifier_block cell_of_bus_notifier = {
	.notifier_call = cell_of_bus_notify
};
704

J
Jeremy Kerr 已提交
705 706 707 708 709 710
static int __init cell_iommu_get_window(struct device_node *np,
					 unsigned long *base,
					 unsigned long *size)
{
	const void *dma_window;
	unsigned long index;
711

J
Jeremy Kerr 已提交
712
	/* Use ibm,dma-window if available, else, hard code ! */
713
	dma_window = of_get_property(np, "ibm,dma-window", NULL);
J
Jeremy Kerr 已提交
714 715 716 717 718
	if (dma_window == NULL) {
		*base = 0;
		*size = 0x80000000u;
		return -ENODEV;
	}
719

J
Jeremy Kerr 已提交
720
	of_parse_dma_window(np, dma_window, &index, base, size);
721
	return 0;
722 723
}

724
static struct cbe_iommu * __init cell_iommu_alloc(struct device_node *np)
725
{
J
Jeremy Kerr 已提交
726 727 728 729 730 731 732 733
	struct cbe_iommu *iommu;
	int nid, i;

	/* Get node ID */
	nid = of_node_to_nid(np);
	if (nid < 0) {
		printk(KERN_ERR "iommu: failed to get node for %s\n",
		       np->full_name);
734
		return NULL;
J
Jeremy Kerr 已提交
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
	}
	pr_debug("iommu: setting up iommu for node %d (%s)\n",
		 nid, np->full_name);

	/* XXX todo: If we can have multiple windows on the same IOMMU, which
	 * isn't the case today, we probably want here to check wether the
	 * iommu for that node is already setup.
	 * However, there might be issue with getting the size right so let's
	 * ignore that for now. We might want to completely get rid of the
	 * multiple window support since the cell iommu supports per-page ioids
	 */

	if (cbe_nr_iommus >= NR_IOMMUS) {
		printk(KERN_ERR "iommu: too many IOMMUs detected ! (%s)\n",
		       np->full_name);
750
		return NULL;
J
Jeremy Kerr 已提交
751 752 753 754 755
	}

	/* Init base fields */
	i = cbe_nr_iommus++;
	iommu = &iommus[i];
756
	iommu->stab = NULL;
J
Jeremy Kerr 已提交
757 758 759
	iommu->nid = nid;
	snprintf(iommu->name, sizeof(iommu->name), "iommu%d", i);
	INIT_LIST_HEAD(&iommu->windows);
760

761 762 763 764 765 766 767 768 769 770 771 772 773
	return iommu;
}

static void __init cell_iommu_init_one(struct device_node *np,
				       unsigned long offset)
{
	struct cbe_iommu *iommu;
	unsigned long base, size;

	iommu = cell_iommu_alloc(np);
	if (!iommu)
		return;

J
Jeremy Kerr 已提交
774 775
	/* Obtain a window for it */
	cell_iommu_get_window(np, &base, &size);
776

J
Jeremy Kerr 已提交
777 778
	pr_debug("\ttranslating window 0x%lx...0x%lx\n",
		 base, base + size - 1);
779

J
Jeremy Kerr 已提交
780
	/* Initialize the hardware */
781
	cell_iommu_setup_hardware(iommu, base, size);
782

J
Jeremy Kerr 已提交
783 784 785 786
	/* Setup the iommu_table */
	cell_iommu_setup_window(iommu, np, base, size,
				offset >> IOMMU_PAGE_SHIFT);
}
787

J
Jeremy Kerr 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
static void __init cell_disable_iommus(void)
{
	int node;
	unsigned long base, val;
	void __iomem *xregs, *cregs;

	/* Make sure IOC translation is disabled on all nodes */
	for_each_online_node(node) {
		if (cell_iommu_find_ioc(node, &base))
			continue;
		xregs = ioremap(base, IOC_Reg_Size);
		if (xregs == NULL)
			continue;
		cregs = xregs + IOC_IOCmd_Offset;

		pr_debug("iommu: cleaning up iommu on node %d\n", node);

		out_be64(xregs + IOC_IOST_Origin, 0);
		(void)in_be64(xregs + IOC_IOST_Origin);
		val = in_be64(cregs + IOC_IOCmd_Cfg);
		val &= ~IOC_IOCmd_Cfg_TE;
		out_be64(cregs + IOC_IOCmd_Cfg, val);
		(void)in_be64(cregs + IOC_IOCmd_Cfg);

		iounmap(xregs);
	}
}
815

J
Jeremy Kerr 已提交
816 817 818 819
static int __init cell_iommu_init_disabled(void)
{
	struct device_node *np = NULL;
	unsigned long base = 0, size;
820

J
Jeremy Kerr 已提交
821
	/* When no iommu is present, we use direct DMA ops */
822
	set_pci_dma_ops(&dma_direct_ops);
823

J
Jeremy Kerr 已提交
824 825 826 827 828
	/* First make sure all IOC translation is turned off */
	cell_disable_iommus();

	/* If we have no Axon, we set up the spider DMA magic offset */
	if (of_find_node_by_name(NULL, "axon") == NULL)
829
		cell_dma_direct_offset = SPIDER_DMA_OFFSET;
J
Jeremy Kerr 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

	/* Now we need to check to see where the memory is mapped
	 * in PCI space. We assume that all busses use the same dma
	 * window which is always the case so far on Cell, thus we
	 * pick up the first pci-internal node we can find and check
	 * the DMA window from there.
	 */
	for_each_node_by_name(np, "axon") {
		if (np->parent == NULL || np->parent->parent != NULL)
			continue;
		if (cell_iommu_get_window(np, &base, &size) == 0)
			break;
	}
	if (np == NULL) {
		for_each_node_by_name(np, "pci-internal") {
			if (np->parent == NULL || np->parent->parent != NULL)
				continue;
			if (cell_iommu_get_window(np, &base, &size) == 0)
				break;
		}
	}
	of_node_put(np);

	/* If we found a DMA window, we check if it's big enough to enclose
	 * all of physical memory. If not, we force enable IOMMU
	 */
	if (np && size < lmb_end_of_DRAM()) {
		printk(KERN_WARNING "iommu: force-enabled, dma window"
		       " (%ldMB) smaller than total memory (%ldMB)\n",
		       size >> 20, lmb_end_of_DRAM() >> 20);
		return -ENODEV;
861 862
	}

863
	cell_dma_direct_offset += base;
J
Jeremy Kerr 已提交
864

865
	if (cell_dma_direct_offset != 0)
866 867
		ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;

J
Jeremy Kerr 已提交
868
	printk("iommu: disabled, direct DMA offset is 0x%lx\n",
869
	       cell_dma_direct_offset);
J
Jeremy Kerr 已提交
870 871

	return 0;
872 873
}

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
/*
 *  Fixed IOMMU mapping support
 *
 *  This code adds support for setting up a fixed IOMMU mapping on certain
 *  cell machines. For 64-bit devices this avoids the performance overhead of
 *  mapping and unmapping pages at runtime. 32-bit devices are unable to use
 *  the fixed mapping.
 *
 *  The fixed mapping is established at boot, and maps all of physical memory
 *  1:1 into device space at some offset. On machines with < 30 GB of memory
 *  we setup the fixed mapping immediately above the normal IOMMU window.
 *
 *  For example a machine with 4GB of memory would end up with the normal
 *  IOMMU window from 0-2GB and the fixed mapping window from 2GB to 6GB. In
 *  this case a 64-bit device wishing to DMA to 1GB would be told to DMA to
 *  3GB, plus any offset required by firmware. The firmware offset is encoded
 *  in the "dma-ranges" property.
 *
 *  On machines with 30GB or more of memory, we are unable to place the fixed
 *  mapping above the normal IOMMU window as we would run out of address space.
 *  Instead we move the normal IOMMU window to coincide with the hash page
 *  table, this region does not need to be part of the fixed mapping as no
 *  device should ever be DMA'ing to it. We then setup the fixed mapping
 *  from 0 to 32GB.
 */

static u64 cell_iommu_get_fixed_address(struct device *dev)
{
902
	u64 cpu_addr, size, best_size, dev_addr = OF_BAD_ADDR;
903
	struct device_node *np;
904
	const u32 *ranges = NULL;
905
	int i, len, best, naddr, nsize, pna, range_size;
906

907
	np = of_node_get(dev->archdata.of_node);
908 909 910 911 912 913 914
	while (1) {
		naddr = of_n_addr_cells(np);
		nsize = of_n_size_cells(np);
		np = of_get_next_parent(np);
		if (!np)
			break;

915
		ranges = of_get_property(np, "dma-ranges", &len);
916 917 918

		/* Ignore empty ranges, they imply no translation required */
		if (ranges && len > 0)
919
			break;
920 921 922 923 924 925 926 927 928
	}

	if (!ranges) {
		dev_dbg(dev, "iommu: no dma-ranges found\n");
		goto out;
	}

	len /= sizeof(u32);

929 930 931
	pna = of_n_addr_cells(np);
	range_size = naddr + nsize + pna;

932
	/* dma-ranges format:
933 934 935
	 * child addr	: naddr cells
	 * parent addr	: pna cells
	 * size		: nsize cells
936
	 */
937 938 939
	for (i = 0, best = -1, best_size = 0; i < len; i += range_size) {
		cpu_addr = of_translate_dma_address(np, ranges + i + naddr);
		size = of_read_number(ranges + i + naddr + pna, nsize);
940 941 942 943 944 945 946

		if (cpu_addr == 0 && size > best_size) {
			best = i;
			best_size = size;
		}
	}

947 948 949
	if (best >= 0) {
		dev_addr = of_read_number(ranges + best, naddr);
	} else
950 951 952 953 954
		dev_dbg(dev, "iommu: no suitable range found!\n");

out:
	of_node_put(np);

955
	return dev_addr;
956 957 958 959 960 961 962
}

static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask)
{
	if (!dev->dma_mask || !dma_supported(dev, dma_mask))
		return -EIO;

963 964 965 966 967
	if (dma_mask == DMA_BIT_MASK(64) &&
		cell_iommu_get_fixed_address(dev) != OF_BAD_ADDR)
	{
		dev_dbg(dev, "iommu: 64-bit OK, using fixed ops\n");
		set_dma_ops(dev, &dma_iommu_fixed_ops);
968 969 970 971 972
	} else {
		dev_dbg(dev, "iommu: not 64-bit, using default ops\n");
		set_dma_ops(dev, get_pci_dma_ops());
	}

973 974
	cell_dma_dev_setup(dev);

975 976 977 978 979
	*dev->dma_mask = dma_mask;

	return 0;
}

980
static void cell_dma_dev_setup_fixed(struct device *dev)
981 982 983 984 985 986 987 988 989 990
{
	struct dev_archdata *archdata = &dev->archdata;
	u64 addr;

	addr = cell_iommu_get_fixed_address(dev) + dma_iommu_fixed_base;
	archdata->dma_data = (void *)addr;

	dev_dbg(dev, "iommu: fixed addr = %lx\n", addr);
}

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
static void insert_16M_pte(unsigned long addr, unsigned long *ptab,
			   unsigned long base_pte)
{
	unsigned long segment, offset;

	segment = addr >> IO_SEGMENT_SHIFT;
	offset = (addr >> 24) - (segment << IO_PAGENO_BITS(24));
	ptab = ptab + (segment * (1 << 12) / sizeof(unsigned long));

	pr_debug("iommu: addr %lx ptab %p segment %lx offset %lx\n",
		  addr, ptab, segment, offset);

	ptab[offset] = base_pte | (__pa(addr) & IOPTE_RPN_Mask);
}

1006 1007 1008 1009
static void cell_iommu_setup_fixed_ptab(struct cbe_iommu *iommu,
	struct device_node *np, unsigned long dbase, unsigned long dsize,
	unsigned long fbase, unsigned long fsize)
{
1010
	unsigned long base_pte, uaddr, ioaddr, *ptab;
1011

1012
	ptab = cell_iommu_alloc_ptab(iommu, fbase, fsize, dbase, dsize, 24);
1013 1014 1015 1016 1017

	dma_iommu_fixed_base = fbase;

	pr_debug("iommu: mapping 0x%lx pages from 0x%lx\n", fsize, fbase);

1018
	base_pte = IOPTE_PP_W | IOPTE_PP_R | IOPTE_M
1019 1020
		    | (cell_iommu_get_ioid(np) & IOPTE_IOID_Mask);

1021 1022 1023 1024 1025 1026 1027
	if (iommu_fixed_is_weak)
		pr_info("IOMMU: Using weak ordering for fixed mapping\n");
	else {
		pr_info("IOMMU: Using strong ordering for fixed mapping\n");
		base_pte |= IOPTE_SO_RW;
	}

1028
	for (uaddr = 0; uaddr < fsize; uaddr += (1 << 24)) {
1029
		/* Don't touch the dynamic region */
1030 1031
		ioaddr = uaddr + fbase;
		if (ioaddr >= dbase && ioaddr < (dbase + dsize)) {
1032
			pr_debug("iommu: fixed/dynamic overlap, skipping\n");
1033 1034
			continue;
		}
1035 1036

		insert_16M_pte(uaddr, ptab, base_pte);
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	}

	mb();
}

static int __init cell_iommu_fixed_mapping_init(void)
{
	unsigned long dbase, dsize, fbase, fsize, hbase, hend;
	struct cbe_iommu *iommu;
	struct device_node *np;

	/* The fixed mapping is only supported on axon machines */
	np = of_find_node_by_name(NULL, "axon");
	if (!np) {
		pr_debug("iommu: fixed mapping disabled, no axons found\n");
		return -1;
	}

1055
	/* We must have dma-ranges properties for fixed mapping to work */
1056
	np = of_find_node_with_property(NULL, "dma-ranges");
1057 1058 1059 1060 1061 1062 1063
	of_node_put(np);

	if (!np) {
		pr_debug("iommu: no dma-ranges found, no fixed mapping\n");
		return -1;
	}

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	/* The default setup is to have the fixed mapping sit after the
	 * dynamic region, so find the top of the largest IOMMU window
	 * on any axon, then add the size of RAM and that's our max value.
	 * If that is > 32GB we have to do other shennanigans.
	 */
	fbase = 0;
	for_each_node_by_name(np, "axon") {
		cell_iommu_get_window(np, &dbase, &dsize);
		fbase = max(fbase, dbase + dsize);
	}

	fbase = _ALIGN_UP(fbase, 1 << IO_SEGMENT_SHIFT);
	fsize = lmb_phys_mem_size();

	if ((fbase + fsize) <= 0x800000000)
		hbase = 0; /* use the device tree window */
	else {
		/* If we're over 32 GB we need to cheat. We can't map all of
		 * RAM with the fixed mapping, and also fit the dynamic
		 * region. So try to place the dynamic region where the hash
		 * table sits, drivers never need to DMA to it, we don't
		 * need a fixed mapping for that area.
		 */
		if (!htab_address) {
			pr_debug("iommu: htab is NULL, on LPAR? Huh?\n");
			return -1;
		}
		hbase = __pa(htab_address);
		hend  = hbase + htab_size_bytes;

		/* The window must start and end on a segment boundary */
		if ((hbase != _ALIGN_UP(hbase, 1 << IO_SEGMENT_SHIFT)) ||
		    (hend != _ALIGN_UP(hend, 1 << IO_SEGMENT_SHIFT))) {
			pr_debug("iommu: hash window not segment aligned\n");
			return -1;
		}

		/* Check the hash window fits inside the real DMA window */
		for_each_node_by_name(np, "axon") {
			cell_iommu_get_window(np, &dbase, &dsize);

			if (hbase < dbase || (hend > (dbase + dsize))) {
				pr_debug("iommu: hash window doesn't fit in"
					 "real DMA window\n");
				return -1;
			}
		}

		fbase = 0;
	}

	/* Setup the dynamic regions */
	for_each_node_by_name(np, "axon") {
		iommu = cell_iommu_alloc(np);
		BUG_ON(!iommu);

		if (hbase == 0)
			cell_iommu_get_window(np, &dbase, &dsize);
		else {
			dbase = hbase;
			dsize = htab_size_bytes;
		}

1127 1128
		printk(KERN_DEBUG "iommu: node %d, dynamic window 0x%lx-0x%lx "
			"fixed window 0x%lx-0x%lx\n", iommu->nid, dbase,
1129 1130
			 dbase + dsize, fbase, fbase + fsize);

1131
		cell_iommu_setup_stab(iommu, dbase, dsize, fbase, fsize);
1132 1133
		iommu->ptab = cell_iommu_alloc_ptab(iommu, dbase, dsize, 0, 0,
						    IOMMU_PAGE_SHIFT);
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
		cell_iommu_setup_fixed_ptab(iommu, np, dbase, dsize,
					     fbase, fsize);
		cell_iommu_enable_hardware(iommu);
		cell_iommu_setup_window(iommu, np, dbase, dsize, 0);
	}

	dma_iommu_ops.set_dma_mask = dma_set_mask_and_switch;
	set_pci_dma_ops(&dma_iommu_ops);

	return 0;
}

static int iommu_fixed_disabled;

static int __init setup_iommu_fixed(char *str)
{
1150 1151
	struct device_node *pciep;

1152 1153 1154
	if (strcmp(str, "off") == 0)
		iommu_fixed_disabled = 1;

1155 1156 1157 1158 1159 1160 1161 1162
	/* If we can find a pcie-endpoint in the device tree assume that
	 * we're on a triblade or a CAB so by default the fixed mapping
	 * should be set to be weakly ordered; but only if the boot
	 * option WASN'T set for strong ordering
	 */
	pciep = of_find_node_by_type(NULL, "pcie-endpoint");

	if (strcmp(str, "weak") == 0 || (pciep && strcmp(str, "strong") != 0))
1163 1164
		iommu_fixed_is_weak = 1;

1165 1166
	of_node_put(pciep);

1167 1168 1169 1170
	return 1;
}
__setup("iommu_fixed=", setup_iommu_fixed);

J
Jeremy Kerr 已提交
1171
static int __init cell_iommu_init(void)
1172
{
J
Jeremy Kerr 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
	struct device_node *np;

	/* If IOMMU is disabled or we have little enough RAM to not need
	 * to enable it, we setup a direct mapping.
	 *
	 * Note: should we make sure we have the IOMMU actually disabled ?
	 */
	if (iommu_is_off ||
	    (!iommu_force_on && lmb_end_of_DRAM() <= 0x80000000ull))
		if (cell_iommu_init_disabled() == 0)
			goto bail;

	/* Setup various ppc_md. callbacks */
	ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;
	ppc_md.tce_build = tce_build_cell;
	ppc_md.tce_free = tce_free_cell;

1190 1191 1192
	if (!iommu_fixed_disabled && cell_iommu_fixed_mapping_init() == 0)
		goto bail;

J
Jeremy Kerr 已提交
1193 1194 1195 1196 1197
	/* Create an iommu for each /axon node.  */
	for_each_node_by_name(np, "axon") {
		if (np->parent == NULL || np->parent->parent != NULL)
			continue;
		cell_iommu_init_one(np, 0);
1198
	}
1199

J
Jeremy Kerr 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	/* Create an iommu for each toplevel /pci-internal node for
	 * old hardware/firmware
	 */
	for_each_node_by_name(np, "pci-internal") {
		if (np->parent == NULL || np->parent->parent != NULL)
			continue;
		cell_iommu_init_one(np, SPIDER_DMA_OFFSET);
	}

	/* Setup default PCI iommu ops */
1210
	set_pci_dma_ops(&dma_iommu_ops);
J
Jeremy Kerr 已提交
1211 1212 1213 1214 1215 1216 1217 1218

 bail:
	/* Register callbacks on OF platform device addition/removal
	 * to handle linking them to the right DMA operations
	 */
	bus_register_notifier(&of_platform_bus_type, &cell_of_bus_notifier);

	return 0;
1219
}
1220 1221
machine_arch_initcall(cell, cell_iommu_init);
machine_arch_initcall(celleb_native, cell_iommu_init);
J
Jeremy Kerr 已提交
1222