]> Pileus Git - ~andy/linux/blob - arch/powerpc/mm/pgtable_64.c
[PATCH] powerpc: Kill ppcdebug
[~andy/linux] / arch / powerpc / mm / pgtable_64.c
1 /*
2  *  This file contains ioremap and related functions for 64-bit machines.
3  *
4  *  Derived from arch/ppc64/mm/init.c
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  *  Modifications by Paul Mackerras (PowerMac) (paulus@samba.org)
8  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
9  *    Copyright (C) 1996 Paul Mackerras
10  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
11  *
12  *  Derived from "arch/i386/mm/init.c"
13  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
14  *
15  *  Dave Engebretsen <engebret@us.ibm.com>
16  *      Rework for PPC64 port.
17  *
18  *  This program is free software; you can redistribute it and/or
19  *  modify it under the terms of the GNU General Public License
20  *  as published by the Free Software Foundation; either version
21  *  2 of the License, or (at your option) any later version.
22  *
23  */
24
25 #include <linux/config.h>
26 #include <linux/signal.h>
27 #include <linux/sched.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/string.h>
31 #include <linux/types.h>
32 #include <linux/mman.h>
33 #include <linux/mm.h>
34 #include <linux/swap.h>
35 #include <linux/stddef.h>
36 #include <linux/vmalloc.h>
37 #include <linux/init.h>
38 #include <linux/delay.h>
39 #include <linux/bootmem.h>
40 #include <linux/highmem.h>
41 #include <linux/idr.h>
42 #include <linux/nodemask.h>
43 #include <linux/module.h>
44
45 #include <asm/pgalloc.h>
46 #include <asm/page.h>
47 #include <asm/prom.h>
48 #include <asm/lmb.h>
49 #include <asm/rtas.h>
50 #include <asm/io.h>
51 #include <asm/mmu_context.h>
52 #include <asm/pgtable.h>
53 #include <asm/mmu.h>
54 #include <asm/uaccess.h>
55 #include <asm/smp.h>
56 #include <asm/machdep.h>
57 #include <asm/tlb.h>
58 #include <asm/eeh.h>
59 #include <asm/processor.h>
60 #include <asm/mmzone.h>
61 #include <asm/cputable.h>
62 #include <asm/sections.h>
63 #include <asm/system.h>
64 #include <asm/iommu.h>
65 #include <asm/abs_addr.h>
66 #include <asm/vdso.h>
67 #include <asm/imalloc.h>
68
69 unsigned long ioremap_bot = IMALLOC_BASE;
70 static unsigned long phbs_io_bot = PHBS_IO_BASE;
71
72 #ifdef CONFIG_PPC_ISERIES
73
74 void __iomem *ioremap(unsigned long addr, unsigned long size)
75 {
76         return (void __iomem *)addr;
77 }
78
79 extern void __iomem *__ioremap(unsigned long addr, unsigned long size,
80                        unsigned long flags)
81 {
82         return (void __iomem *)addr;
83 }
84
85 void iounmap(volatile void __iomem *addr)
86 {
87         return;
88 }
89
90 #else
91
92 /*
93  * map_io_page currently only called by __ioremap
94  * map_io_page adds an entry to the ioremap page table
95  * and adds an entry to the HPT, possibly bolting it
96  */
97 static int map_io_page(unsigned long ea, unsigned long pa, int flags)
98 {
99         pgd_t *pgdp;
100         pud_t *pudp;
101         pmd_t *pmdp;
102         pte_t *ptep;
103         unsigned long vsid;
104
105         if (mem_init_done) {
106                 pgdp = pgd_offset_k(ea);
107                 pudp = pud_alloc(&init_mm, pgdp, ea);
108                 if (!pudp)
109                         return -ENOMEM;
110                 pmdp = pmd_alloc(&init_mm, pudp, ea);
111                 if (!pmdp)
112                         return -ENOMEM;
113                 ptep = pte_alloc_kernel(pmdp, ea);
114                 if (!ptep)
115                         return -ENOMEM;
116                 set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
117                                                           __pgprot(flags)));
118         } else {
119                 unsigned long va, vpn, hash, hpteg;
120
121                 /*
122                  * If the mm subsystem is not fully up, we cannot create a
123                  * linux page table entry for this mapping.  Simply bolt an
124                  * entry in the hardware page table.
125                  */
126                 vsid = get_kernel_vsid(ea);
127                 va = (vsid << 28) | (ea & 0xFFFFFFF);
128                 vpn = va >> PAGE_SHIFT;
129
130                 hash = hpt_hash(vpn, 0);
131
132                 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
133
134                 /* Panic if a pte grpup is full */
135                 if (ppc_md.hpte_insert(hpteg, va, pa >> PAGE_SHIFT,
136                                        HPTE_V_BOLTED,
137                                        _PAGE_NO_CACHE|_PAGE_GUARDED|PP_RWXX)
138                     == -1) {
139                         panic("map_io_page: could not insert mapping");
140                 }
141         }
142         return 0;
143 }
144
145
146 static void __iomem * __ioremap_com(unsigned long addr, unsigned long pa,
147                             unsigned long ea, unsigned long size,
148                             unsigned long flags)
149 {
150         unsigned long i;
151
152         if ((flags & _PAGE_PRESENT) == 0)
153                 flags |= pgprot_val(PAGE_KERNEL);
154
155         for (i = 0; i < size; i += PAGE_SIZE)
156                 if (map_io_page(ea+i, pa+i, flags))
157                         return NULL;
158
159         return (void __iomem *) (ea + (addr & ~PAGE_MASK));
160 }
161
162
163 void __iomem *
164 ioremap(unsigned long addr, unsigned long size)
165 {
166         return __ioremap(addr, size, _PAGE_NO_CACHE | _PAGE_GUARDED);
167 }
168
169 void __iomem * __ioremap(unsigned long addr, unsigned long size,
170                          unsigned long flags)
171 {
172         unsigned long pa, ea;
173         void __iomem *ret;
174
175         /*
176          * Choose an address to map it to.
177          * Once the imalloc system is running, we use it.
178          * Before that, we map using addresses going
179          * up from ioremap_bot.  imalloc will use
180          * the addresses from ioremap_bot through
181          * IMALLOC_END
182          * 
183          */
184         pa = addr & PAGE_MASK;
185         size = PAGE_ALIGN(addr + size) - pa;
186
187         if (size == 0)
188                 return NULL;
189
190         if (mem_init_done) {
191                 struct vm_struct *area;
192                 area = im_get_free_area(size);
193                 if (area == NULL)
194                         return NULL;
195                 ea = (unsigned long)(area->addr);
196                 ret = __ioremap_com(addr, pa, ea, size, flags);
197                 if (!ret)
198                         im_free(area->addr);
199         } else {
200                 ea = ioremap_bot;
201                 ret = __ioremap_com(addr, pa, ea, size, flags);
202                 if (ret)
203                         ioremap_bot += size;
204         }
205         return ret;
206 }
207
208 #define IS_PAGE_ALIGNED(_val) ((_val) == ((_val) & PAGE_MASK))
209
210 int __ioremap_explicit(unsigned long pa, unsigned long ea,
211                        unsigned long size, unsigned long flags)
212 {
213         struct vm_struct *area;
214         void __iomem *ret;
215         
216         /* For now, require page-aligned values for pa, ea, and size */
217         if (!IS_PAGE_ALIGNED(pa) || !IS_PAGE_ALIGNED(ea) ||
218             !IS_PAGE_ALIGNED(size)) {
219                 printk(KERN_ERR "unaligned value in %s\n", __FUNCTION__);
220                 return 1;
221         }
222         
223         if (!mem_init_done) {
224                 /* Two things to consider in this case:
225                  * 1) No records will be kept (imalloc, etc) that the region
226                  *    has been remapped
227                  * 2) It won't be easy to iounmap() the region later (because
228                  *    of 1)
229                  */
230                 ;
231         } else {
232                 area = im_get_area(ea, size,
233                         IM_REGION_UNUSED|IM_REGION_SUBSET|IM_REGION_EXISTS);
234                 if (area == NULL) {
235                         /* Expected when PHB-dlpar is in play */
236                         return 1;
237                 }
238                 if (ea != (unsigned long) area->addr) {
239                         printk(KERN_ERR "unexpected addr return from "
240                                "im_get_area\n");
241                         return 1;
242                 }
243         }
244         
245         ret = __ioremap_com(pa, pa, ea, size, flags);
246         if (ret == NULL) {
247                 printk(KERN_ERR "ioremap_explicit() allocation failure !\n");
248                 return 1;
249         }
250         if (ret != (void *) ea) {
251                 printk(KERN_ERR "__ioremap_com() returned unexpected addr\n");
252                 return 1;
253         }
254
255         return 0;
256 }
257
258 /*  
259  * Unmap an IO region and remove it from imalloc'd list.
260  * Access to IO memory should be serialized by driver.
261  * This code is modeled after vmalloc code - unmap_vm_area()
262  *
263  * XXX  what about calls before mem_init_done (ie python_countermeasures())
264  */
265 void iounmap(volatile void __iomem *token)
266 {
267         void *addr;
268
269         if (!mem_init_done)
270                 return;
271         
272         addr = (void *) ((unsigned long __force) token & PAGE_MASK);
273
274         im_free(addr);
275 }
276
277 static int iounmap_subset_regions(unsigned long addr, unsigned long size)
278 {
279         struct vm_struct *area;
280
281         /* Check whether subsets of this region exist */
282         area = im_get_area(addr, size, IM_REGION_SUPERSET);
283         if (area == NULL)
284                 return 1;
285
286         while (area) {
287                 iounmap((void __iomem *) area->addr);
288                 area = im_get_area(addr, size,
289                                 IM_REGION_SUPERSET);
290         }
291
292         return 0;
293 }
294
295 int iounmap_explicit(volatile void __iomem *start, unsigned long size)
296 {
297         struct vm_struct *area;
298         unsigned long addr;
299         int rc;
300         
301         addr = (unsigned long __force) start & PAGE_MASK;
302
303         /* Verify that the region either exists or is a subset of an existing
304          * region.  In the latter case, split the parent region to create 
305          * the exact region 
306          */
307         area = im_get_area(addr, size, 
308                             IM_REGION_EXISTS | IM_REGION_SUBSET);
309         if (area == NULL) {
310                 /* Determine whether subset regions exist.  If so, unmap */
311                 rc = iounmap_subset_regions(addr, size);
312                 if (rc) {
313                         printk(KERN_ERR
314                                "%s() cannot unmap nonexistent range 0x%lx\n",
315                                 __FUNCTION__, addr);
316                         return 1;
317                 }
318         } else {
319                 iounmap((void __iomem *) area->addr);
320         }
321         /*
322          * FIXME! This can't be right:
323         iounmap(area->addr);
324          * Maybe it should be "iounmap(area);"
325          */
326         return 0;
327 }
328
329 #endif
330
331 EXPORT_SYMBOL(ioremap);
332 EXPORT_SYMBOL(__ioremap);
333 EXPORT_SYMBOL(iounmap);
334
335 void __iomem * reserve_phb_iospace(unsigned long size)
336 {
337         void __iomem *virt_addr;
338                 
339         if (phbs_io_bot >= IMALLOC_BASE) 
340                 panic("reserve_phb_iospace(): phb io space overflow\n");
341                         
342         virt_addr = (void __iomem *) phbs_io_bot;
343         phbs_io_bot += size;
344
345         return virt_addr;
346 }