blob: 115cbdf3b13b79d39ecd33c9d3bc688297e6ac70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/ppc64/kernel/u3_iommu.c
3 *
4 * Copyright (C) 2004 Olof Johansson <olof@austin.ibm.com>, IBM Corporation
5 *
6 * Based on pSeries_iommu.c:
7 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
8 * Copyright (C) 2004 Olof Johansson <olof@austin.ibm.com>, IBM Corporation
9 *
10 * Dynamic DMA mapping support, Apple U3 & IBM CPC925 "DART" iommu.
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include <linux/config.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/slab.h>
32#include <linux/mm.h>
33#include <linux/spinlock.h>
34#include <linux/string.h>
35#include <linux/pci.h>
36#include <linux/dma-mapping.h>
37#include <linux/vmalloc.h>
38#include <asm/io.h>
39#include <asm/prom.h>
40#include <asm/ppcdebug.h>
41#include <asm/iommu.h>
42#include <asm/pci-bridge.h>
43#include <asm/machdep.h>
44#include <asm/abs_addr.h>
45#include <asm/cacheflush.h>
46#include <asm/lmb.h>
Olof Johanssonc707ffc2005-09-20 13:45:41 +100047#include <asm/dart.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include "pci.h"
50
51extern int iommu_force_on;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Physical base address and size of the DART table */
54unsigned long dart_tablebase; /* exported to htab_initialize */
55static unsigned long dart_tablesize;
56
57/* Virtual base address of the DART table */
58static u32 *dart_vbase;
59
60/* Mapped base address for the dart */
61static unsigned int *dart;
62
63/* Dummy val that entries are set to when unused */
64static unsigned int dart_emptyval;
65
66static struct iommu_table iommu_table_u3;
67static int iommu_table_u3_inited;
68static int dart_dirty;
69
70#define DBG(...)
71
72static inline void dart_tlb_invalidate_all(void)
73{
74 unsigned long l = 0;
75 unsigned int reg;
76 unsigned long limit;
77
78 DBG("dart: flush\n");
79
80 /* To invalidate the DART, set the DARTCNTL_FLUSHTLB bit in the
81 * control register and wait for it to clear.
82 *
83 * Gotcha: Sometimes, the DART won't detect that the bit gets
84 * set. If so, clear it and set it again.
85 */
86
87 limit = 0;
88
89retry:
90 reg = in_be32((unsigned int *)dart+DARTCNTL);
91 reg |= DARTCNTL_FLUSHTLB;
92 out_be32((unsigned int *)dart+DARTCNTL, reg);
93
94 l = 0;
95 while ((in_be32((unsigned int *)dart+DARTCNTL) & DARTCNTL_FLUSHTLB) &&
96 l < (1L<<limit)) {
97 l++;
98 }
99 if (l == (1L<<limit)) {
100 if (limit < 4) {
101 limit++;
102 reg = in_be32((unsigned int *)dart+DARTCNTL);
103 reg &= ~DARTCNTL_FLUSHTLB;
104 out_be32((unsigned int *)dart+DARTCNTL, reg);
105 goto retry;
106 } else
107 panic("U3-DART: TLB did not flush after waiting a long "
108 "time. Buggy U3 ?");
109 }
110}
111
112static void dart_flush(struct iommu_table *tbl)
113{
114 if (dart_dirty)
115 dart_tlb_invalidate_all();
116 dart_dirty = 0;
117}
118
119static void dart_build(struct iommu_table *tbl, long index,
120 long npages, unsigned long uaddr,
121 enum dma_data_direction direction)
122{
123 unsigned int *dp;
124 unsigned int rpn;
125
126 DBG("dart: build at: %lx, %lx, addr: %x\n", index, npages, uaddr);
127
128 dp = ((unsigned int*)tbl->it_base) + index;
129
130 /* On U3, all memory is contigous, so we can move this
131 * out of the loop.
132 */
133 while (npages--) {
134 rpn = virt_to_abs(uaddr) >> PAGE_SHIFT;
135
136 *(dp++) = DARTMAP_VALID | (rpn & DARTMAP_RPNMASK);
137
138 rpn++;
139 uaddr += PAGE_SIZE;
140 }
141
142 dart_dirty = 1;
143}
144
145
146static void dart_free(struct iommu_table *tbl, long index, long npages)
147{
148 unsigned int *dp;
149
150 /* We don't worry about flushing the TLB cache. The only drawback of
151 * not doing it is that we won't catch buggy device drivers doing
152 * bad DMAs, but then no 32-bit architecture ever does either.
153 */
154
155 DBG("dart: free at: %lx, %lx\n", index, npages);
156
157 dp = ((unsigned int *)tbl->it_base) + index;
158
159 while (npages--)
160 *(dp++) = dart_emptyval;
161}
162
163
164static int dart_init(struct device_node *dart_node)
165{
166 unsigned int regword;
167 unsigned int i;
168 unsigned long tmp;
169
170 if (dart_tablebase == 0 || dart_tablesize == 0) {
171 printk(KERN_INFO "U3-DART: table not allocated, using direct DMA\n");
172 return -ENODEV;
173 }
174
175 /* Make sure nothing from the DART range remains in the CPU cache
176 * from a previous mapping that existed before the kernel took
177 * over
178 */
179 flush_dcache_phys_range(dart_tablebase, dart_tablebase + dart_tablesize);
180
181 /* Allocate a spare page to map all invalid DART pages. We need to do
182 * that to work around what looks like a problem with the HT bridge
183 * prefetching into invalid pages and corrupting data
184 */
185 tmp = lmb_alloc(PAGE_SIZE, PAGE_SIZE);
186 if (!tmp)
187 panic("U3-DART: Cannot allocate spare page!");
188 dart_emptyval = DARTMAP_VALID | ((tmp >> PAGE_SHIFT) & DARTMAP_RPNMASK);
189
190 /* Map in DART registers. FIXME: Use device node to get base address */
191 dart = ioremap(DART_BASE, 0x7000);
192 if (dart == NULL)
193 panic("U3-DART: Cannot map registers!");
194
195 /* Set initial control register contents: table base,
196 * table size and enable bit
197 */
198 regword = DARTCNTL_ENABLE |
199 ((dart_tablebase >> PAGE_SHIFT) << DARTCNTL_BASE_SHIFT) |
200 (((dart_tablesize >> PAGE_SHIFT) & DARTCNTL_SIZE_MASK)
201 << DARTCNTL_SIZE_SHIFT);
202 dart_vbase = ioremap(virt_to_abs(dart_tablebase), dart_tablesize);
203
204 /* Fill initial table */
205 for (i = 0; i < dart_tablesize/4; i++)
206 dart_vbase[i] = dart_emptyval;
207
208 /* Initialize DART with table base and enable it. */
209 out_be32((unsigned int *)dart, regword);
210
211 /* Invalidate DART to get rid of possible stale TLBs */
212 dart_tlb_invalidate_all();
213
214 printk(KERN_INFO "U3/CPC925 DART IOMMU initialized\n");
215
216 return 0;
217}
218
219static void iommu_table_u3_setup(void)
220{
221 iommu_table_u3.it_busno = 0;
222 iommu_table_u3.it_offset = 0;
223 /* it_size is in number of entries */
224 iommu_table_u3.it_size = dart_tablesize / sizeof(u32);
225
226 /* Initialize the common IOMMU code */
227 iommu_table_u3.it_base = (unsigned long)dart_vbase;
228 iommu_table_u3.it_index = 0;
229 iommu_table_u3.it_blocksize = 1;
230 iommu_init_table(&iommu_table_u3);
231
232 /* Reserve the last page of the DART to avoid possible prefetch
233 * past the DART mapped area
234 */
235 set_bit(iommu_table_u3.it_size - 1, iommu_table_u3.it_map);
236}
237
238static void iommu_dev_setup_u3(struct pci_dev *dev)
239{
240 struct device_node *dn;
241
242 /* We only have one iommu table on the mac for now, which makes
243 * things simple. Setup all PCI devices to point to this table
244 *
245 * We must use pci_device_to_OF_node() to make sure that
246 * we get the real "final" pointer to the device in the
247 * pci_dev sysdata and not the temporary PHB one
248 */
249 dn = pci_device_to_OF_node(dev);
250
251 if (dn)
Paul Mackerras16353172005-09-06 13:17:54 +1000252 PCI_DN(dn)->iommu_table = &iommu_table_u3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255static void iommu_bus_setup_u3(struct pci_bus *bus)
256{
257 struct device_node *dn;
258
259 if (!iommu_table_u3_inited) {
260 iommu_table_u3_inited = 1;
261 iommu_table_u3_setup();
262 }
263
264 dn = pci_bus_to_OF_node(bus);
265
266 if (dn)
Paul Mackerras16353172005-09-06 13:17:54 +1000267 PCI_DN(dn)->iommu_table = &iommu_table_u3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270static void iommu_dev_setup_null(struct pci_dev *dev) { }
271static void iommu_bus_setup_null(struct pci_bus *bus) { }
272
273void iommu_init_early_u3(void)
274{
275 struct device_node *dn;
276
277 /* Find the DART in the device-tree */
278 dn = of_find_compatible_node(NULL, "dart", "u3-dart");
279 if (dn == NULL)
280 return;
281
282 /* Setup low level TCE operations for the core IOMMU code */
283 ppc_md.tce_build = dart_build;
284 ppc_md.tce_free = dart_free;
285 ppc_md.tce_flush = dart_flush;
286
287 /* Initialize the DART HW */
288 if (dart_init(dn)) {
289 /* If init failed, use direct iommu and null setup functions */
290 ppc_md.iommu_dev_setup = iommu_dev_setup_null;
291 ppc_md.iommu_bus_setup = iommu_bus_setup_null;
292
293 /* Setup pci_dma ops */
294 pci_direct_iommu_init();
295 } else {
296 ppc_md.iommu_dev_setup = iommu_dev_setup_u3;
297 ppc_md.iommu_bus_setup = iommu_bus_setup_u3;
298
299 /* Setup pci_dma ops */
300 pci_iommu_init();
301 }
302}
303
304
305void __init alloc_u3_dart_table(void)
306{
307 /* Only reserve DART space if machine has more than 2GB of RAM
308 * or if requested with iommu=on on cmdline.
309 */
310 if (lmb_end_of_DRAM() <= 0x80000000ull && !iommu_force_on)
311 return;
312
313 /* 512 pages (2MB) is max DART tablesize. */
314 dart_tablesize = 1UL << 21;
315 /* 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
316 * will blow up an entire large page anyway in the kernel mapping
317 */
318 dart_tablebase = (unsigned long)
319 abs_to_virt(lmb_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
320
321 printk(KERN_INFO "U3-DART allocated at: %lx\n", dart_tablebase);
322}