dart_iommu.c 9.1 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 38 39 40 41 42 43 44 45 46 47
 * 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/config.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/slab.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>
#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>
#include <asm/lmb.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 58 59 60 61
extern int iommu_force_on;

/* 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;

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

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

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

#define DBG(...)

static inline void dart_tlb_invalidate_all(void)
{
	unsigned long l = 0;
77
	unsigned int reg, inv_bit;
L
Linus Torvalds 已提交
78 79 80 81 82 83 84 85 86
	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.
87
	 */
L
Linus Torvalds 已提交
88 89 90

	limit = 0;

91
	inv_bit = dart_is_u4 ? DART_CNTL_U4_FLUSHTLB : DART_CNTL_U3_FLUSHTLB;
L
Linus Torvalds 已提交
92 93
retry:
	l = 0;
94 95 96 97 98
	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 已提交
99
		l++;
100
	if (l == (1L << limit)) {
L
Linus Torvalds 已提交
101 102
		if (limit < 4) {
			limit++;
103 104 105
		        reg = DART_IN(DART_CNTL);
		        reg &= ~inv_bit;
			DART_OUT(DART_CNTL, reg);
L
Linus Torvalds 已提交
106 107
			goto retry;
		} else
108
			panic("DART: TLB did not flush after waiting a long "
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119
			      "time. Buggy U3 ?");
	}
}

static void dart_flush(struct iommu_table *tbl)
{
	if (dart_dirty)
		dart_tlb_invalidate_all();
	dart_dirty = 0;
}

120
static void dart_build(struct iommu_table *tbl, long index,
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128
		       long npages, unsigned long uaddr,
		       enum dma_data_direction direction)
{
	unsigned int *dp;
	unsigned int rpn;

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

129 130 131
	index <<= DART_PAGE_FACTOR;
	npages <<= DART_PAGE_FACTOR;

L
Linus Torvalds 已提交
132
	dp = ((unsigned int*)tbl->it_base) + index;
133

L
Linus Torvalds 已提交
134 135 136 137
	/* On U3, all memory is contigous, so we can move this
	 * out of the loop.
	 */
	while (npages--) {
138
		rpn = virt_to_abs(uaddr) >> DART_PAGE_SHIFT;
L
Linus Torvalds 已提交
139 140 141

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

142
		uaddr += DART_PAGE_SIZE;
L
Linus Torvalds 已提交
143 144 145 146 147 148 149 150 151
	}

	dart_dirty = 1;
}


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

L
Linus Torvalds 已提交
153 154 155 156 157 158 159
	/* 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);

160 161 162
	index <<= DART_PAGE_FACTOR;
	npages <<= DART_PAGE_FACTOR;

L
Linus Torvalds 已提交
163
	dp  = ((unsigned int *)tbl->it_base) + index;
164

L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172
	while (npages--)
		*(dp++) = dart_emptyval;
}


static int dart_init(struct device_node *dart_node)
{
	unsigned int i;
173 174
	unsigned long tmp, base, size;
	struct resource r;
L
Linus Torvalds 已提交
175 176

	if (dart_tablebase == 0 || dart_tablesize == 0) {
177 178
		printk(KERN_INFO "DART: table not allocated, using "
		       "direct DMA\n");
L
Linus Torvalds 已提交
179 180 181
		return -ENODEV;
	}

182 183 184
	if (of_address_to_resource(dart_node, 0, &r))
		panic("DART: can't get register base ! ");

L
Linus Torvalds 已提交
185 186 187 188
	/* Make sure nothing from the DART range remains in the CPU cache
	 * from a previous mapping that existed before the kernel took
	 * over
	 */
189 190
	flush_dcache_phys_range(dart_tablebase,
				dart_tablebase + dart_tablesize);
L
Linus Torvalds 已提交
191 192 193 194 195

	/* 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
	 */
196
	tmp = lmb_alloc(DART_PAGE_SIZE, DART_PAGE_SIZE);
197 198
	dart_emptyval = DARTMAP_VALID | ((tmp >> DART_PAGE_SHIFT) &
					 DARTMAP_RPNMASK);
L
Linus Torvalds 已提交
199

200 201
	/* Map in DART registers */
	dart = ioremap(r.start, r.end - r.start + 1);
L
Linus Torvalds 已提交
202
	if (dart == NULL)
203
		panic("DART: Cannot map registers!");
L
Linus Torvalds 已提交
204

205
	/* Map in DART table */
L
Linus Torvalds 已提交
206 207 208 209 210 211 212
	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. */
213 214 215
	base = dart_tablebase >> DART_PAGE_SHIFT;
	size = dart_tablesize >> DART_PAGE_SHIFT;
	if (dart_is_u4) {
216
		size &= DART_SIZE_U4_SIZE_MASK;
217 218 219 220
		DART_OUT(DART_BASE_U4, base);
		DART_OUT(DART_SIZE_U4, size);
		DART_OUT(DART_CNTL, DART_CNTL_U4_ENABLE);
	} else {
221
		size &= DART_CNTL_U3_SIZE_MASK;
222 223 224 225 226
		DART_OUT(DART_CNTL,
			 DART_CNTL_U3_ENABLE |
			 (base << DART_CNTL_U3_BASE_SHIFT) |
			 (size << DART_CNTL_U3_SIZE_SHIFT));
	}
L
Linus Torvalds 已提交
227 228 229 230

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

231 232
	printk(KERN_INFO "DART IOMMU initialized for %s type chipset\n",
	       dart_is_u4 ? "U4" : "U3");
L
Linus Torvalds 已提交
233 234 235 236

	return 0;
}

237
static void iommu_table_dart_setup(void)
L
Linus Torvalds 已提交
238
{
239 240
	iommu_table_dart.it_busno = 0;
	iommu_table_dart.it_offset = 0;
L
Linus Torvalds 已提交
241
	/* it_size is in number of entries */
242
	iommu_table_dart.it_size = (dart_tablesize / sizeof(u32)) >> DART_PAGE_FACTOR;
L
Linus Torvalds 已提交
243 244

	/* Initialize the common IOMMU code */
245 246 247 248
	iommu_table_dart.it_base = (unsigned long)dart_vbase;
	iommu_table_dart.it_index = 0;
	iommu_table_dart.it_blocksize = 1;
	iommu_init_table(&iommu_table_dart);
L
Linus Torvalds 已提交
249 250 251 252

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

256
static void iommu_dev_setup_dart(struct pci_dev *dev)
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269
{
	struct device_node *dn;

	/* We only have one iommu table on the mac for now, which makes
	 * things simple. Setup all PCI devices to point to this table
	 *
	 * We must use pci_device_to_OF_node() to make sure that
	 * we get the real "final" pointer to the device in the
	 * pci_dev sysdata and not the temporary PHB one
	 */
	dn = pci_device_to_OF_node(dev);

	if (dn)
270
		PCI_DN(dn)->iommu_table = &iommu_table_dart;
L
Linus Torvalds 已提交
271 272
}

273
static void iommu_bus_setup_dart(struct pci_bus *bus)
L
Linus Torvalds 已提交
274 275 276
{
	struct device_node *dn;

277 278 279
	if (!iommu_table_dart_inited) {
		iommu_table_dart_inited = 1;
		iommu_table_dart_setup();
L
Linus Torvalds 已提交
280 281 282 283 284
	}

	dn = pci_bus_to_OF_node(bus);

	if (dn)
285
		PCI_DN(dn)->iommu_table = &iommu_table_dart;
L
Linus Torvalds 已提交
286 287 288 289 290
}

static void iommu_dev_setup_null(struct pci_dev *dev) { }
static void iommu_bus_setup_null(struct pci_bus *bus) { }

291
void iommu_init_early_dart(void)
L
Linus Torvalds 已提交
292 293 294 295 296
{
	struct device_node *dn;

	/* Find the DART in the device-tree */
	dn = of_find_compatible_node(NULL, "dart", "u3-dart");
297 298 299 300 301 302
	if (dn == NULL) {
		dn = of_find_compatible_node(NULL, "dart", "u4-dart");
		if (dn == NULL)
			goto bail;
		dart_is_u4 = 1;
	}
L
Linus Torvalds 已提交
303 304 305 306 307 308 309

	/* 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;

	/* Initialize the DART HW */
310 311 312
	if (dart_init(dn) == 0) {
		ppc_md.iommu_dev_setup = iommu_dev_setup_dart;
		ppc_md.iommu_bus_setup = iommu_bus_setup_dart;
L
Linus Torvalds 已提交
313 314 315

		/* Setup pci_dma ops */
		pci_iommu_init();
316 317

		return;
L
Linus Torvalds 已提交
318
	}
319 320 321 322 323 324 325 326

 bail:
	/* If init failed, use direct iommu and null setup functions */
	ppc_md.iommu_dev_setup = iommu_dev_setup_null;
	ppc_md.iommu_bus_setup = iommu_bus_setup_null;

	/* Setup pci_dma ops */
	pci_direct_iommu_init();
L
Linus Torvalds 已提交
327 328 329
}


330
void __init alloc_dart_table(void)
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
	/* Only reserve DART space if machine has more than 2GB of RAM
	 * or if requested with iommu=on on cmdline.
	 */
	if (lmb_end_of_DRAM() <= 0x80000000ull && !iommu_force_on)
		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)
		abs_to_virt(lmb_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));

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