# BRCM_VERSION=3
[bcm963xx.git] / kernel / linux / arch / cris / mm / ioremap.c
1 /*
2  * arch/cris/mm/ioremap.c
3  *
4  * Re-map IO memory to kernel address space so that we can access it.
5  * Needed for memory-mapped I/O devices mapped outside our normal DRAM
6  * window (that is, all memory-mapped I/O devices).
7  *
8  * (C) Copyright 1995 1996 Linus Torvalds
9  * CRIS-port by Axis Communications AB
10  */
11
12 #include <linux/vmalloc.h>
13 #include <asm/io.h>
14 #include <asm/pgalloc.h>
15 #include <asm/cacheflush.h>
16 #include <asm/tlbflush.h>
17
18 extern inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
19         unsigned long phys_addr, unsigned long flags)
20 {
21         unsigned long end;
22
23         address &= ~PMD_MASK;
24         end = address + size;
25         if (end > PMD_SIZE)
26                 end = PMD_SIZE;
27         if (address >= end)
28                 BUG();
29         do {
30                 if (!pte_none(*pte)) {
31                         printk("remap_area_pte: page already exists\n");
32                         BUG();
33                 }
34                 set_pte(pte, mk_pte_phys(phys_addr, __pgprot(_PAGE_PRESENT | __READABLE | 
35                                                              __WRITEABLE | _PAGE_GLOBAL |
36                                                              _PAGE_KERNEL | flags)));
37                 address += PAGE_SIZE;
38                 phys_addr += PAGE_SIZE;
39                 pte++;
40         } while (address && (address < end));
41 }
42
43 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
44         unsigned long phys_addr, unsigned long flags)
45 {
46         unsigned long end;
47
48         address &= ~PGDIR_MASK;
49         end = address + size;
50         if (end > PGDIR_SIZE)
51                 end = PGDIR_SIZE;
52         phys_addr -= address;
53         if (address >= end)
54                 BUG();
55         do {
56                 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
57                 if (!pte)
58                         return -ENOMEM;
59                 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
60                 address = (address + PMD_SIZE) & PMD_MASK;
61                 pmd++;
62         } while (address && (address < end));
63         return 0;
64 }
65
66 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
67                                  unsigned long size, unsigned long flags)
68 {
69         int error;
70         pgd_t * dir;
71         unsigned long end = address + size;
72
73         phys_addr -= address;
74         dir = pgd_offset(&init_mm, address);
75         flush_cache_all();
76         if (address >= end)
77                 BUG();
78         spin_lock(&init_mm.page_table_lock);
79         do {
80                 pmd_t *pmd;
81                 pmd = pmd_alloc(&init_mm, dir, address);
82                 error = -ENOMEM;
83                 if (!pmd)
84                         break;
85                 if (remap_area_pmd(pmd, address, end - address,
86                                    phys_addr + address, flags))
87                         break;
88                 error = 0;
89                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
90                 dir++;
91         } while (address && (address < end));
92         spin_unlock(&init_mm.page_table_lock);
93         flush_tlb_all();
94         return error;
95 }
96
97 /*
98  * Generic mapping function (not visible outside):
99  */
100
101 /*
102  * Remap an arbitrary physical address space into the kernel virtual
103  * address space. Needed when the kernel wants to access high addresses
104  * directly.
105  *
106  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
107  * have to convert them into an offset in a page-aligned mapping, but the
108  * caller shouldn't need to know that small detail.
109  */
110 void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
111 {
112         void * addr;
113         struct vm_struct * area;
114         unsigned long offset, last_addr;
115
116         /* Don't allow wraparound or zero size */
117         last_addr = phys_addr + size - 1;
118         if (!size || last_addr < phys_addr)
119                 return NULL;
120
121         /*
122          * Mappings have to be page-aligned
123          */
124         offset = phys_addr & ~PAGE_MASK;
125         phys_addr &= PAGE_MASK;
126         size = PAGE_ALIGN(last_addr+1) - phys_addr;
127
128         /*
129          * Ok, go for it..
130          */
131         area = get_vm_area(size, VM_IOREMAP);
132         if (!area)
133                 return NULL;
134         addr = area->addr;
135         if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
136                 vfree(addr);
137                 return NULL;
138         }
139         return (void *) (offset + (char *)addr);
140 }
141
142 void iounmap(void *addr)
143 {
144         if (addr > high_memory)
145                 return vfree((void *) (PAGE_MASK & (unsigned long) addr));
146 }