This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / ppc64 / kernel / u3_iommu.c
1 /*
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>
47
48 #include "pci.h"
49
50 extern int iommu_force_on;
51
52 /* physical base of DART registers */
53 #define DART_BASE        0xf8033000UL
54
55 /* Offset from base to control register */
56 #define DARTCNTL   0
57 /* Offset from base to exception register */
58 #define DARTEXCP   0x10
59 /* Offset from base to TLB tag registers */
60 #define DARTTAG    0x1000
61
62
63 /* Control Register fields */
64
65 /* base address of table (pfn) */
66 #define DARTCNTL_BASE_MASK    0xfffff
67 #define DARTCNTL_BASE_SHIFT   12
68
69 #define DARTCNTL_FLUSHTLB     0x400
70 #define DARTCNTL_ENABLE       0x200
71
72 /* size of table in pages */
73 #define DARTCNTL_SIZE_MASK    0x1ff
74 #define DARTCNTL_SIZE_SHIFT   0
75
76 /* DART table fields */
77 #define DARTMAP_VALID   0x80000000
78 #define DARTMAP_RPNMASK 0x00ffffff
79
80 /* Physical base address and size of the DART table */
81 unsigned long dart_tablebase; /* exported to htab_initialize */
82 static unsigned long dart_tablesize;
83
84 /* Virtual base address of the DART table */
85 static u32 *dart_vbase;
86
87 /* Mapped base address for the dart */
88 static unsigned int *dart; 
89
90 /* Dummy val that entries are set to when unused */
91 static unsigned int dart_emptyval;
92
93 static struct iommu_table iommu_table_u3;
94 static int dart_dirty;
95
96 #define DBG(...)
97
98 static inline void dart_tlb_invalidate_all(void)
99 {
100         unsigned long l = 0;
101         unsigned int reg;
102         unsigned long limit;
103
104         DBG("dart: flush\n");
105
106         /* To invalidate the DART, set the DARTCNTL_FLUSHTLB bit in the
107          * control register and wait for it to clear.
108          *
109          * Gotcha: Sometimes, the DART won't detect that the bit gets
110          * set. If so, clear it and set it again.
111          */ 
112
113         limit = 0;
114
115 retry:
116         reg = in_be32((unsigned int *)dart+DARTCNTL);
117         reg |= DARTCNTL_FLUSHTLB;
118         out_be32((unsigned int *)dart+DARTCNTL, reg);
119
120         l = 0;
121         while ((in_be32((unsigned int *)dart+DARTCNTL) & DARTCNTL_FLUSHTLB) &&
122                 l < (1L<<limit)) {
123                 l++;
124         }
125         if (l == (1L<<limit)) {
126                 if (limit < 4) {
127                         limit++;
128                         reg = in_be32((unsigned int *)dart+DARTCNTL);
129                         reg &= ~DARTCNTL_FLUSHTLB;
130                         out_be32((unsigned int *)dart+DARTCNTL, reg);
131                         goto retry;
132                 } else
133                         panic("U3-DART: TLB did not flush after waiting a long "
134                               "time. Buggy U3 ?");
135         }
136 }
137
138 static void dart_flush(struct iommu_table *tbl)
139 {
140         if (dart_dirty)
141                 dart_tlb_invalidate_all();
142         dart_dirty = 0;
143 }
144
145 static void dart_build(struct iommu_table *tbl, long index, 
146                        long npages, unsigned long uaddr,
147                        enum dma_data_direction direction)
148 {
149         unsigned int *dp;
150         unsigned int rpn;
151
152         DBG("dart: build at: %lx, %lx, addr: %x\n", index, npages, uaddr);
153
154         dp = ((unsigned int*)tbl->it_base) + index;
155         
156         /* On U3, all memory is contigous, so we can move this
157          * out of the loop.
158          */
159         while (npages--) {
160                 rpn = virt_to_abs(uaddr) >> PAGE_SHIFT;
161
162                 *(dp++) = DARTMAP_VALID | (rpn & DARTMAP_RPNMASK);
163
164                 rpn++;
165                 uaddr += PAGE_SIZE;
166         }
167
168         dart_dirty = 1;
169 }
170
171
172 static void dart_free(struct iommu_table *tbl, long index, long npages)
173 {
174         unsigned int *dp;
175         
176         /* We don't worry about flushing the TLB cache. The only drawback of
177          * not doing it is that we won't catch buggy device drivers doing
178          * bad DMAs, but then no 32-bit architecture ever does either.
179          */
180
181         DBG("dart: free at: %lx, %lx\n", index, npages);
182
183         dp  = ((unsigned int *)tbl->it_base) + index;
184                 
185         while (npages--)
186                 *(dp++) = dart_emptyval;
187 }
188
189
190 static int dart_init(struct device_node *dart_node)
191 {
192         unsigned int regword;
193         unsigned int i;
194         unsigned long tmp;
195         struct page *p;
196
197         if (dart_tablebase == 0 || dart_tablesize == 0) {
198                 printk(KERN_INFO "U3-DART: table not allocated, using direct DMA\n");
199                 return -ENODEV;
200         }
201
202         /* Make sure nothing from the DART range remains in the CPU cache
203          * from a previous mapping that existed before the kernel took
204          * over
205          */
206         flush_dcache_phys_range(dart_tablebase, dart_tablebase + dart_tablesize);
207
208         /* Allocate a spare page to map all invalid DART pages. We need to do
209          * that to work around what looks like a problem with the HT bridge
210          * prefetching into invalid pages and corrupting data
211          */
212         tmp = __get_free_pages(GFP_ATOMIC, 1);
213         if (tmp == 0)
214                 panic("U3-DART: Cannot allocate spare page !");
215         dart_emptyval = DARTMAP_VALID |
216                 ((virt_to_abs(tmp) >> PAGE_SHIFT) & DARTMAP_RPNMASK);
217
218         /* Map in DART registers. FIXME: Use device node to get base address */
219         dart = ioremap(DART_BASE, 0x7000);
220         if (dart == NULL)
221                 panic("U3-DART: Cannot map registers !");
222
223         /* Set initial control register contents: table base, 
224          * table size and enable bit
225          */
226         regword = DARTCNTL_ENABLE | 
227                 ((dart_tablebase >> PAGE_SHIFT) << DARTCNTL_BASE_SHIFT) |
228                 (((dart_tablesize >> PAGE_SHIFT) & DARTCNTL_SIZE_MASK)
229                                  << DARTCNTL_SIZE_SHIFT);
230         p = virt_to_page(dart_tablebase);
231         dart_vbase = ioremap(virt_to_abs(dart_tablebase), dart_tablesize);
232
233         /* Fill initial table */
234         for (i = 0; i < dart_tablesize/4; i++)
235                 dart_vbase[i] = dart_emptyval;
236
237         /* Initialize DART with table base and enable it. */
238         out_be32((unsigned int *)dart, regword);
239
240         /* Invalidate DART to get rid of possible stale TLBs */
241         dart_tlb_invalidate_all();
242
243         iommu_table_u3.it_busno = 0;
244         
245         /* Units of tce entries */
246         iommu_table_u3.it_offset = 0;
247         
248         /* Set the tce table size - measured in pages */
249         iommu_table_u3.it_size = dart_tablesize >> PAGE_SHIFT;
250
251         /* Initialize the common IOMMU code */
252         iommu_table_u3.it_base = (unsigned long)dart_vbase;
253         iommu_table_u3.it_index = 0;
254         iommu_table_u3.it_blocksize = 1;
255         iommu_table_u3.it_entrysize = sizeof(u32);
256         iommu_init_table(&iommu_table_u3);
257
258         /* Reserve the last page of the DART to avoid possible prefetch
259          * past the DART mapped area
260          */
261         set_bit(iommu_table_u3.it_mapsize - 1, iommu_table_u3.it_map);
262
263         printk(KERN_INFO "U3/CPC925 DART IOMMU initialized\n");
264
265         return 0;
266 }
267
268 void iommu_setup_u3(void)
269 {
270         struct pci_dev *dev = NULL;
271         struct device_node *dn;
272
273         /* Find the DART in the device-tree */
274         dn = of_find_compatible_node(NULL, "dart", "u3-dart");
275         if (dn == NULL)
276                 return;
277
278         /* Setup low level TCE operations for the core IOMMU code */
279         ppc_md.tce_build = dart_build;
280         ppc_md.tce_free  = dart_free;
281         ppc_md.tce_flush = dart_flush;
282
283         /* Initialize the DART HW */
284         if (dart_init(dn))
285                 return;
286
287         /* Setup pci_dma ops */
288         pci_iommu_init();
289
290         /* We only have one iommu table on the mac for now, which makes
291          * things simple. Setup all PCI devices to point to this table
292          */
293         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
294                 /* We must use pci_device_to_OF_node() to make sure that
295                  * we get the real "final" pointer to the device in the
296                  * pci_dev sysdata and not the temporary PHB one
297                  */
298                 struct device_node *dn = pci_device_to_OF_node(dev);
299                 if (dn)
300                         dn->iommu_table = &iommu_table_u3;
301         }
302 }
303
304 void __init alloc_u3_dart_table(void)
305 {
306         /* Only reserve DART space if machine has more than 2GB of RAM
307          * or if requested with iommu=on on cmdline.
308          */
309         if (lmb_end_of_DRAM() <= 0x80000000ull && !iommu_force_on)
310                 return;
311
312         /* 512 pages (2MB) is max DART tablesize. */
313         dart_tablesize = 1UL << 21;
314         /* 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
315          * will blow up an entire large page anyway in the kernel mapping
316          */
317         dart_tablebase = (unsigned long)
318                 abs_to_virt(lmb_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
319
320         printk(KERN_INFO "U3-DART allocated at: %lx\n", dart_tablebase);
321 }