dart_iommu.c 11.8 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * arch/powerpc/sysdev/dart_iommu.c
L
Linus Torvalds 已提交
3
 *
4
 * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
5 6
 * Copyright (C) 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>,
 *                    IBM Corporation
L
Linus Torvalds 已提交
7 8 9
 *
 * Based on pSeries_iommu.c:
 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
10
 * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
L
Linus Torvalds 已提交
11
 *
12 13
 * Dynamic DMA mapping support, Apple U3, U4 & IBM CPC925 "DART" iommu.
 *
L
Linus Torvalds 已提交
14 15 16 17 18
 *
 * 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 of the License, or
 * (at your option) any later version.
19
 *
L
Linus Torvalds 已提交
20 21 22 23
 * 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.
24
 *
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/init.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/vmalloc.h>
J
Johannes Berg 已提交
38
#include <linux/suspend.h>
Y
Yinghai Lu 已提交
39
#include <linux/memblock.h>
40
#include <linux/gfp.h>
L
Linus Torvalds 已提交
41 42 43 44 45 46 47
#include <asm/io.h>
#include <asm/prom.h>
#include <asm/iommu.h>
#include <asm/pci-bridge.h>
#include <asm/machdep.h>
#include <asm/abs_addr.h>
#include <asm/cacheflush.h>
48
#include <asm/ppc-pci.h>
L
Linus Torvalds 已提交
49

D
David Gibson 已提交
50 51
#include "dart.h"

L
Linus Torvalds 已提交
52 53 54 55 56 57
/* Physical base address and size of the DART table */
unsigned long dart_tablebase; /* exported to htab_initialize */
static unsigned long dart_tablesize;

/* Virtual base address of the DART table */
static u32 *dart_vbase;
J
Johannes Berg 已提交
58 59 60
#ifdef CONFIG_PM
static u32 *dart_copy;
#endif
L
Linus Torvalds 已提交
61 62

/* Mapped base address for the dart */
63
static unsigned int __iomem *dart;
L
Linus Torvalds 已提交
64 65 66 67

/* Dummy val that entries are set to when unused */
static unsigned int dart_emptyval;

68 69
static struct iommu_table iommu_table_dart;
static int iommu_table_dart_inited;
L
Linus Torvalds 已提交
70
static int dart_dirty;
71
static int dart_is_u4;
L
Linus Torvalds 已提交
72

73 74
#define DART_U4_BYPASS_BASE	0x8000000000ull

L
Linus Torvalds 已提交
75 76 77 78 79
#define DBG(...)

static inline void dart_tlb_invalidate_all(void)
{
	unsigned long l = 0;
80
	unsigned int reg, inv_bit;
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89
	unsigned long limit;

	DBG("dart: flush\n");

	/* To invalidate the DART, set the DARTCNTL_FLUSHTLB bit in the
	 * control register and wait for it to clear.
	 *
	 * Gotcha: Sometimes, the DART won't detect that the bit gets
	 * set. If so, clear it and set it again.
90
	 */
L
Linus Torvalds 已提交
91 92 93

	limit = 0;

94
	inv_bit = dart_is_u4 ? DART_CNTL_U4_FLUSHTLB : DART_CNTL_U3_FLUSHTLB;
L
Linus Torvalds 已提交
95 96
retry:
	l = 0;
97 98 99 100 101
	reg = DART_IN(DART_CNTL);
	reg |= inv_bit;
	DART_OUT(DART_CNTL, reg);

	while ((DART_IN(DART_CNTL) & inv_bit) && l < (1L << limit))
L
Linus Torvalds 已提交
102
		l++;
103
	if (l == (1L << limit)) {
L
Linus Torvalds 已提交
104 105
		if (limit < 4) {
			limit++;
O
Olof Johansson 已提交
106 107
			reg = DART_IN(DART_CNTL);
			reg &= ~inv_bit;
108
			DART_OUT(DART_CNTL, reg);
L
Linus Torvalds 已提交
109 110
			goto retry;
		} else
111
			panic("DART: TLB did not flush after waiting a long "
L
Linus Torvalds 已提交
112 113 114 115
			      "time. Buggy U3 ?");
	}
}

O
Olof Johansson 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static inline void dart_tlb_invalidate_one(unsigned long bus_rpn)
{
	unsigned int reg;
	unsigned int l, limit;

	reg = DART_CNTL_U4_ENABLE | DART_CNTL_U4_IONE |
		(bus_rpn & DART_CNTL_U4_IONE_MASK);
	DART_OUT(DART_CNTL, reg);

	limit = 0;
wait_more:
	l = 0;
	while ((DART_IN(DART_CNTL) & DART_CNTL_U4_IONE) && l < (1L << limit)) {
		rmb();
		l++;
	}

	if (l == (1L << limit)) {
		if (limit < 4) {
			limit++;
			goto wait_more;
		} else
			panic("DART: TLB did not flush after waiting a long "
			      "time. Buggy U4 ?");
	}
}

L
Linus Torvalds 已提交
143 144
static void dart_flush(struct iommu_table *tbl)
{
145
	mb();
O
Olof Johansson 已提交
146
	if (dart_dirty) {
L
Linus Torvalds 已提交
147
		dart_tlb_invalidate_all();
O
Olof Johansson 已提交
148 149
		dart_dirty = 0;
	}
L
Linus Torvalds 已提交
150 151
}

152
static int dart_build(struct iommu_table *tbl, long index,
L
Linus Torvalds 已提交
153
		       long npages, unsigned long uaddr,
154 155
		       enum dma_data_direction direction,
		       struct dma_attrs *attrs)
L
Linus Torvalds 已提交
156 157 158
{
	unsigned int *dp;
	unsigned int rpn;
O
Olof Johansson 已提交
159
	long l;
L
Linus Torvalds 已提交
160 161 162 163

	DBG("dart: build at: %lx, %lx, addr: %x\n", index, npages, uaddr);

	dp = ((unsigned int*)tbl->it_base) + index;
164

165
	/* On U3, all memory is contiguous, so we can move this
L
Linus Torvalds 已提交
166 167
	 * out of the loop.
	 */
O
Olof Johansson 已提交
168 169
	l = npages;
	while (l--) {
170
		rpn = virt_to_abs(uaddr) >> DART_PAGE_SHIFT;
L
Linus Torvalds 已提交
171 172 173

		*(dp++) = DARTMAP_VALID | (rpn & DARTMAP_RPNMASK);

174
		uaddr += DART_PAGE_SIZE;
L
Linus Torvalds 已提交
175 176
	}

177 178 179 180 181
	/* make sure all updates have reached memory */
	mb();
	in_be32((unsigned __iomem *)dp);
	mb();

O
Olof Johansson 已提交
182 183 184 185 186 187 188
	if (dart_is_u4) {
		rpn = index;
		while (npages--)
			dart_tlb_invalidate_one(rpn++);
	} else {
		dart_dirty = 1;
	}
189
	return 0;
L
Linus Torvalds 已提交
190 191 192 193 194 195
}


static void dart_free(struct iommu_table *tbl, long index, long npages)
{
	unsigned int *dp;
196

L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204
	/* We don't worry about flushing the TLB cache. The only drawback of
	 * not doing it is that we won't catch buggy device drivers doing
	 * bad DMAs, but then no 32-bit architecture ever does either.
	 */

	DBG("dart: free at: %lx, %lx\n", index, npages);

	dp  = ((unsigned int *)tbl->it_base) + index;
205

L
Linus Torvalds 已提交
206 207 208 209 210
	while (npages--)
		*(dp++) = dart_emptyval;
}


211
static int __init dart_init(struct device_node *dart_node)
L
Linus Torvalds 已提交
212 213
{
	unsigned int i;
214 215
	unsigned long tmp, base, size;
	struct resource r;
L
Linus Torvalds 已提交
216 217

	if (dart_tablebase == 0 || dart_tablesize == 0) {
218 219
		printk(KERN_INFO "DART: table not allocated, using "
		       "direct DMA\n");
L
Linus Torvalds 已提交
220 221 222
		return -ENODEV;
	}

223 224 225
	if (of_address_to_resource(dart_node, 0, &r))
		panic("DART: can't get register base ! ");

L
Linus Torvalds 已提交
226 227 228 229
	/* Make sure nothing from the DART range remains in the CPU cache
	 * from a previous mapping that existed before the kernel took
	 * over
	 */
230 231
	flush_dcache_phys_range(dart_tablebase,
				dart_tablebase + dart_tablesize);
L
Linus Torvalds 已提交
232 233 234 235 236

	/* Allocate a spare page to map all invalid DART pages. We need to do
	 * that to work around what looks like a problem with the HT bridge
	 * prefetching into invalid pages and corrupting data
	 */
Y
Yinghai Lu 已提交
237
	tmp = memblock_alloc(DART_PAGE_SIZE, DART_PAGE_SIZE);
238 239
	dart_emptyval = DARTMAP_VALID | ((tmp >> DART_PAGE_SHIFT) &
					 DARTMAP_RPNMASK);
L
Linus Torvalds 已提交
240

241 242
	/* Map in DART registers */
	dart = ioremap(r.start, r.end - r.start + 1);
L
Linus Torvalds 已提交
243
	if (dart == NULL)
244
		panic("DART: Cannot map registers!");
L
Linus Torvalds 已提交
245

246
	/* Map in DART table */
L
Linus Torvalds 已提交
247 248 249 250 251 252 253
	dart_vbase = ioremap(virt_to_abs(dart_tablebase), dart_tablesize);

	/* Fill initial table */
	for (i = 0; i < dart_tablesize/4; i++)
		dart_vbase[i] = dart_emptyval;

	/* Initialize DART with table base and enable it. */
254 255 256
	base = dart_tablebase >> DART_PAGE_SHIFT;
	size = dart_tablesize >> DART_PAGE_SHIFT;
	if (dart_is_u4) {
257
		size &= DART_SIZE_U4_SIZE_MASK;
258 259 260 261
		DART_OUT(DART_BASE_U4, base);
		DART_OUT(DART_SIZE_U4, size);
		DART_OUT(DART_CNTL, DART_CNTL_U4_ENABLE);
	} else {
262
		size &= DART_CNTL_U3_SIZE_MASK;
263 264 265 266 267
		DART_OUT(DART_CNTL,
			 DART_CNTL_U3_ENABLE |
			 (base << DART_CNTL_U3_BASE_SHIFT) |
			 (size << DART_CNTL_U3_SIZE_SHIFT));
	}
L
Linus Torvalds 已提交
268 269 270 271

	/* Invalidate DART to get rid of possible stale TLBs */
	dart_tlb_invalidate_all();

272 273
	printk(KERN_INFO "DART IOMMU initialized for %s type chipset\n",
	       dart_is_u4 ? "U4" : "U3");
L
Linus Torvalds 已提交
274 275 276 277

	return 0;
}

278
static void iommu_table_dart_setup(void)
L
Linus Torvalds 已提交
279
{
280 281
	iommu_table_dart.it_busno = 0;
	iommu_table_dart.it_offset = 0;
L
Linus Torvalds 已提交
282
	/* it_size is in number of entries */
283
	iommu_table_dart.it_size = dart_tablesize / sizeof(u32);
L
Linus Torvalds 已提交
284 285

	/* Initialize the common IOMMU code */
286 287 288
	iommu_table_dart.it_base = (unsigned long)dart_vbase;
	iommu_table_dart.it_index = 0;
	iommu_table_dart.it_blocksize = 1;
289
	iommu_init_table(&iommu_table_dart, -1);
L
Linus Torvalds 已提交
290 291 292 293

	/* Reserve the last page of the DART to avoid possible prefetch
	 * past the DART mapped area
	 */
294
	set_bit(iommu_table_dart.it_size - 1, iommu_table_dart.it_map);
L
Linus Torvalds 已提交
295 296
}

297
static void dma_dev_setup_dart(struct device *dev)
L
Linus Torvalds 已提交
298 299 300 301
{
	/* We only have one iommu table on the mac for now, which makes
	 * things simple. Setup all PCI devices to point to this table
	 */
302 303 304 305 306 307 308 309 310
	if (get_dma_ops(dev) == &dma_direct_ops)
		set_dma_offset(dev, DART_U4_BYPASS_BASE);
	else
		set_iommu_table_base(dev, &iommu_table_dart);
}

static void pci_dma_dev_setup_dart(struct pci_dev *dev)
{
	dma_dev_setup_dart(&dev->dev);
L
Linus Torvalds 已提交
311 312
}

313
static void pci_dma_bus_setup_dart(struct pci_bus *bus)
L
Linus Torvalds 已提交
314
{
315 316 317
	if (!iommu_table_dart_inited) {
		iommu_table_dart_inited = 1;
		iommu_table_dart_setup();
L
Linus Torvalds 已提交
318 319 320
	}
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
static bool dart_device_on_pcie(struct device *dev)
{
	struct device_node *np = of_node_get(dev->of_node);

	while(np) {
		if (of_device_is_compatible(np, "U4-pcie") ||
		    of_device_is_compatible(np, "u4-pcie")) {
			of_node_put(np);
			return true;
		}
		np = of_get_next_parent(np);
	}
	return false;
}

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

	/* U4 supports a DART bypass, we use it for 64-bit capable
	 * devices to improve performances. However, that only works
	 * for devices connected to U4 own PCIe interface, not bridged
	 * through hypertransport. We need the device to support at
	 * least 40 bits of addresses.
	 */
	if (dart_device_on_pcie(dev) && dma_mask >= DMA_BIT_MASK(40)) {
		dev_info(dev, "Using 64-bit DMA iommu bypass\n");
		set_dma_ops(dev, &dma_direct_ops);
	} else {
		dev_info(dev, "Using 32-bit DMA via iommu\n");
		set_dma_ops(dev, &dma_iommu_ops);
	}
	dma_dev_setup_dart(dev);

	*dev->dma_mask = dma_mask;
	return 0;
}

360
void __init iommu_init_early_dart(void)
L
Linus Torvalds 已提交
361 362 363 364 365
{
	struct device_node *dn;

	/* Find the DART in the device-tree */
	dn = of_find_compatible_node(NULL, "dart", "u3-dart");
366 367 368
	if (dn == NULL) {
		dn = of_find_compatible_node(NULL, "dart", "u4-dart");
		if (dn == NULL)
369
			return;	/* use default direct_dma_ops */
370 371
		dart_is_u4 = 1;
	}
L
Linus Torvalds 已提交
372

373 374 375 376
	/* Initialize the DART HW */
	if (dart_init(dn) != 0)
		goto bail;

L
Linus Torvalds 已提交
377 378 379 380 381
	/* Setup low level TCE operations for the core IOMMU code */
	ppc_md.tce_build = dart_build;
	ppc_md.tce_free  = dart_free;
	ppc_md.tce_flush = dart_flush;

382 383 384
	/* Setup bypass if supported */
	if (dart_is_u4)
		ppc_md.dma_set_mask = dart_dma_set_mask;
L
Linus Torvalds 已提交
385

386 387 388 389 390 391
	ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_dart;
	ppc_md.pci_dma_bus_setup = pci_dma_bus_setup_dart;

	/* Setup pci_dma ops */
	set_pci_dma_ops(&dma_iommu_ops);
	return;
392 393 394

 bail:
	/* If init failed, use direct iommu and null setup functions */
395 396
	ppc_md.pci_dma_dev_setup = NULL;
	ppc_md.pci_dma_bus_setup = NULL;
397 398

	/* Setup pci_dma ops */
399
	set_pci_dma_ops(&dma_direct_ops);
L
Linus Torvalds 已提交
400 401
}

J
Johannes Berg 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
#ifdef CONFIG_PM
static void iommu_dart_save(void)
{
	memcpy(dart_copy, dart_vbase, 2*1024*1024);
}

static void iommu_dart_restore(void)
{
	memcpy(dart_vbase, dart_copy, 2*1024*1024);
	dart_tlb_invalidate_all();
}

static int __init iommu_init_late_dart(void)
{
	unsigned long tbasepfn;
	struct page *p;

	/* if no dart table exists then we won't need to save it
	 * and the area has also not been reserved */
	if (!dart_tablebase)
		return 0;

	tbasepfn = __pa(dart_tablebase) >> PAGE_SHIFT;
	register_nosave_region_late(tbasepfn,
				    tbasepfn + ((1<<24) >> PAGE_SHIFT));

	/* For suspend we need to copy the dart contents because
	 * it is not part of the regular mapping (see above) and
	 * thus not saved automatically. The memory for this copy
	 * must be allocated early because we need 2 MB. */
	p = alloc_pages(GFP_KERNEL, 21 - PAGE_SHIFT);
	BUG_ON(!p);
	dart_copy = page_address(p);

	ppc_md.iommu_save = iommu_dart_save;
	ppc_md.iommu_restore = iommu_dart_restore;

	return 0;
}

late_initcall(iommu_init_late_dart);
#endif
L
Linus Torvalds 已提交
444

445
void __init alloc_dart_table(void)
L
Linus Torvalds 已提交
446
{
447
	/* Only reserve DART space if machine has more than 1GB of RAM
L
Linus Torvalds 已提交
448
	 * or if requested with iommu=on on cmdline.
449 450 451
	 *
	 * 1GB of RAM is picked as limit because some default devices
	 * (i.e. Airport Extreme) have 30 bit address range limits.
L
Linus Torvalds 已提交
452
	 */
453 454 455 456

	if (iommu_is_off)
		return;

Y
Yinghai Lu 已提交
457
	if (!iommu_force_on && memblock_end_of_DRAM() <= 0x40000000ull)
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465
		return;

	/* 512 pages (2MB) is max DART tablesize. */
	dart_tablesize = 1UL << 21;
	/* 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
	 * will blow up an entire large page anyway in the kernel mapping
	 */
	dart_tablebase = (unsigned long)
Y
Yinghai Lu 已提交
466
		abs_to_virt(memblock_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
L
Linus Torvalds 已提交
467

468
	printk(KERN_INFO "DART table allocated at: %lx\n", dart_tablebase);
L
Linus Torvalds 已提交
469
}