]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/mm/pgtable_64.c
a895de73beae4290862a48211ab69b8dca33b6d9
[linux-2.6-omap-h63xx.git] / 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/signal.h>
26 #include <linux/sched.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/string.h>
30 #include <linux/types.h>
31 #include <linux/mman.h>
32 #include <linux/mm.h>
33 #include <linux/swap.h>
34 #include <linux/stddef.h>
35 #include <linux/vmalloc.h>
36 #include <linux/init.h>
37
38 #include <asm/pgalloc.h>
39 #include <asm/page.h>
40 #include <asm/prom.h>
41 #include <asm/io.h>
42 #include <asm/mmu_context.h>
43 #include <asm/pgtable.h>
44 #include <asm/mmu.h>
45 #include <asm/smp.h>
46 #include <asm/machdep.h>
47 #include <asm/tlb.h>
48 #include <asm/processor.h>
49 #include <asm/cputable.h>
50 #include <asm/sections.h>
51 #include <asm/system.h>
52 #include <asm/abs_addr.h>
53 #include <asm/firmware.h>
54
55 #include "mmu_decl.h"
56
57 unsigned long ioremap_bot = IOREMAP_BASE;
58
59 /*
60  * map_io_page currently only called by __ioremap
61  * map_io_page adds an entry to the ioremap page table
62  * and adds an entry to the HPT, possibly bolting it
63  */
64 static int map_io_page(unsigned long ea, unsigned long pa, int flags)
65 {
66         pgd_t *pgdp;
67         pud_t *pudp;
68         pmd_t *pmdp;
69         pte_t *ptep;
70
71         if (mem_init_done) {
72                 pgdp = pgd_offset_k(ea);
73                 pudp = pud_alloc(&init_mm, pgdp, ea);
74                 if (!pudp)
75                         return -ENOMEM;
76                 pmdp = pmd_alloc(&init_mm, pudp, ea);
77                 if (!pmdp)
78                         return -ENOMEM;
79                 ptep = pte_alloc_kernel(pmdp, ea);
80                 if (!ptep)
81                         return -ENOMEM;
82                 set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT,
83                                                           __pgprot(flags)));
84         } else {
85                 /*
86                  * If the mm subsystem is not fully up, we cannot create a
87                  * linux page table entry for this mapping.  Simply bolt an
88                  * entry in the hardware page table.
89                  *
90                  */
91                 if (htab_bolt_mapping(ea, (unsigned long)ea + PAGE_SIZE,
92                                       pa, flags, mmu_io_psize)) {
93                         printk(KERN_ERR "Failed to do bolted mapping IO "
94                                "memory at %016lx !\n", pa);
95                         return -ENOMEM;
96                 }
97         }
98         return 0;
99 }
100
101
102 /**
103  * __ioremap_at - Low level function to establish the page tables
104  *                for an IO mapping
105  */
106 void __iomem * __ioremap_at(phys_addr_t pa, void *ea, unsigned long size,
107                             unsigned long flags)
108 {
109         unsigned long i;
110
111         if ((flags & _PAGE_PRESENT) == 0)
112                 flags |= pgprot_val(PAGE_KERNEL);
113
114         WARN_ON(pa & ~PAGE_MASK);
115         WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
116         WARN_ON(size & ~PAGE_MASK);
117
118         for (i = 0; i < size; i += PAGE_SIZE)
119                 if (map_io_page((unsigned long)ea+i, pa+i, flags))
120                         return NULL;
121
122         return (void __iomem *)ea;
123 }
124
125 /**
126  * __iounmap_from - Low level function to tear down the page tables
127  *                  for an IO mapping. This is used for mappings that
128  *                  are manipulated manually, like partial unmapping of
129  *                  PCI IOs or ISA space.
130  */
131 void __iounmap_at(void *ea, unsigned long size)
132 {
133         WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
134         WARN_ON(size & ~PAGE_MASK);
135
136         unmap_kernel_range((unsigned long)ea, size);
137 }
138
139 void __iomem * __ioremap(phys_addr_t addr, unsigned long size,
140                          unsigned long flags)
141 {
142         phys_addr_t paligned;
143         void __iomem *ret;
144
145         /*
146          * Choose an address to map it to.
147          * Once the imalloc system is running, we use it.
148          * Before that, we map using addresses going
149          * up from ioremap_bot.  imalloc will use
150          * the addresses from ioremap_bot through
151          * IMALLOC_END
152          * 
153          */
154         paligned = addr & PAGE_MASK;
155         size = PAGE_ALIGN(addr + size) - paligned;
156
157         if ((size == 0) || (paligned == 0))
158                 return NULL;
159
160         if (mem_init_done) {
161                 struct vm_struct *area;
162
163                 area = __get_vm_area(size, VM_IOREMAP,
164                                      ioremap_bot, IOREMAP_END);
165                 if (area == NULL)
166                         return NULL;
167                 ret = __ioremap_at(paligned, area->addr, size, flags);
168                 if (!ret)
169                         vunmap(area->addr);
170         } else {
171                 ret = __ioremap_at(paligned, (void *)ioremap_bot, size, flags);
172                 if (ret)
173                         ioremap_bot += size;
174         }
175
176         if (ret)
177                 ret += addr & ~PAGE_MASK;
178         return ret;
179 }
180
181
182 void __iomem * ioremap(phys_addr_t addr, unsigned long size)
183 {
184         unsigned long flags = _PAGE_NO_CACHE | _PAGE_GUARDED;
185
186         if (ppc_md.ioremap)
187                 return ppc_md.ioremap(addr, size, flags);
188         return __ioremap(addr, size, flags);
189 }
190
191 void __iomem * ioremap_flags(phys_addr_t addr, unsigned long size,
192                              unsigned long flags)
193 {
194         if (ppc_md.ioremap)
195                 return ppc_md.ioremap(addr, size, flags);
196         return __ioremap(addr, size, flags);
197 }
198
199
200 /*  
201  * Unmap an IO region and remove it from imalloc'd list.
202  * Access to IO memory should be serialized by driver.
203  */
204 void __iounmap(volatile void __iomem *token)
205 {
206         void *addr;
207
208         if (!mem_init_done)
209                 return;
210         
211         addr = (void *) ((unsigned long __force)
212                          PCI_FIX_ADDR(token) & PAGE_MASK);
213         if ((unsigned long)addr < ioremap_bot) {
214                 printk(KERN_WARNING "Attempt to iounmap early bolted mapping"
215                        " at 0x%p\n", addr);
216                 return;
217         }
218         vunmap(addr);
219 }
220
221 void iounmap(volatile void __iomem *token)
222 {
223         if (ppc_md.iounmap)
224                 ppc_md.iounmap(token);
225         else
226                 __iounmap(token);
227 }
228
229 EXPORT_SYMBOL(ioremap);
230 EXPORT_SYMBOL(ioremap_flags);
231 EXPORT_SYMBOL(__ioremap);
232 EXPORT_SYMBOL(iounmap);
233 EXPORT_SYMBOL(__iounmap);