ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / arm / mm / ioremap.c
1 /*
2  *  linux/arch/arm/mm/ioremap.c
3  *
4  * Re-map IO memory to kernel address space so that we can access it.
5  *
6  * (C) Copyright 1995 1996 Linus Torvalds
7  *
8  * Hacked for ARM by Phil Blundell <philb@gnu.org>
9  * Hacked to allow all architectures to build, and various cleanups
10  * by Russell King
11  *
12  * This allows a driver to remap an arbitrary region of bus memory into
13  * virtual space.  One should *only* use readl, writel, memcpy_toio and
14  * so on with such remapped areas.
15  *
16  * Because the ARM only has a 32-bit address space we can't address the
17  * whole of the (physical) PCI space at once.  PCI huge-mode addressing
18  * allows us to circumvent this restriction by splitting PCI space into
19  * two 2GB chunks and mapping only one at a time into processor memory.
20  * We use MMU protection domains to trap any attempt to access the bank
21  * that is not currently mapped.  (This isn't fully implemented yet.)
22  */
23 #include <linux/errno.h>
24 #include <linux/mm.h>
25 #include <linux/vmalloc.h>
26
27 #include <asm/cacheflush.h>
28 #include <asm/io.h>
29 #include <asm/tlbflush.h>
30
31 static inline void
32 remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
33                unsigned long phys_addr, pgprot_t pgprot)
34 {
35         unsigned long end;
36
37         address &= ~PMD_MASK;
38         end = address + size;
39         if (end > PMD_SIZE)
40                 end = PMD_SIZE;
41         BUG_ON(address >= end);
42         do {
43                 if (!pte_none(*pte))
44                         goto bad;
45
46                 set_pte(pte, pfn_pte(phys_addr >> PAGE_SHIFT, pgprot));
47                 address += PAGE_SIZE;
48                 phys_addr += PAGE_SIZE;
49                 pte++;
50         } while (address && (address < end));
51         return;
52
53  bad:
54         printk("remap_area_pte: page already exists\n");
55         BUG();
56 }
57
58 static inline int
59 remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
60                unsigned long phys_addr, unsigned long flags)
61 {
62         unsigned long end;
63         pgprot_t pgprot;
64
65         address &= ~PGDIR_MASK;
66         end = address + size;
67
68         if (end > PGDIR_SIZE)
69                 end = PGDIR_SIZE;
70
71         phys_addr -= address;
72         BUG_ON(address >= end);
73
74         pgprot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_WRITE | flags);
75         do {
76                 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
77                 if (!pte)
78                         return -ENOMEM;
79                 remap_area_pte(pte, address, end - address, address + phys_addr, pgprot);
80                 address = (address + PMD_SIZE) & PMD_MASK;
81                 pmd++;
82         } while (address && (address < end));
83         return 0;
84 }
85
86 static int
87 remap_area_pages(unsigned long start, unsigned long phys_addr,
88                  unsigned long size, unsigned long flags)
89 {
90         unsigned long address = start;
91         unsigned long end = start + size;
92         int err = 0;
93         pgd_t * dir;
94
95         phys_addr -= address;
96         dir = pgd_offset(&init_mm, address);
97         BUG_ON(address >= end);
98         spin_lock(&init_mm.page_table_lock);
99         do {
100                 pmd_t *pmd = pmd_alloc(&init_mm, dir, address);
101                 if (!pmd) {
102                         err = -ENOMEM;
103                         break;
104                 }
105                 if (remap_area_pmd(pmd, address, end - address,
106                                          phys_addr + address, flags)) {
107                         err = -ENOMEM;
108                         break;
109                 }
110
111                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
112                 dir++;
113         } while (address && (address < end));
114
115         spin_unlock(&init_mm.page_table_lock);
116         flush_cache_vmap(start, end);
117         return err;
118 }
119
120 /*
121  * Remap an arbitrary physical address space into the kernel virtual
122  * address space. Needed when the kernel wants to access high addresses
123  * directly.
124  *
125  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
126  * have to convert them into an offset in a page-aligned mapping, but the
127  * caller shouldn't need to know that small detail.
128  *
129  * 'flags' are the extra L_PTE_ flags that you want to specify for this
130  * mapping.  See include/asm-arm/proc-armv/pgtable.h for more information.
131  */
132 void *
133 __ioremap(unsigned long phys_addr, size_t size, unsigned long flags,
134           unsigned long align)
135 {
136         void * addr;
137         struct vm_struct * area;
138         unsigned long offset, last_addr;
139
140         /* Don't allow wraparound or zero size */
141         last_addr = phys_addr + size - 1;
142         if (!size || last_addr < phys_addr)
143                 return NULL;
144
145         /*
146          * Mappings have to be page-aligned
147          */
148         offset = phys_addr & ~PAGE_MASK;
149         phys_addr &= PAGE_MASK;
150         size = PAGE_ALIGN(last_addr) - phys_addr;
151
152         /*
153          * Ok, go for it..
154          */
155         area = get_vm_area(size, VM_IOREMAP);
156         if (!area)
157                 return NULL;
158         addr = area->addr;
159         if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
160                 vfree(addr);
161                 return NULL;
162         }
163         return (void *) (offset + (char *)addr);
164 }
165
166 void __iounmap(void *addr)
167 {
168         vfree((void *) (PAGE_MASK & (unsigned long) addr));
169 }