ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / parisc / mm / ioremap.c
1 /*
2  * arch/parisc/mm/ioremap.c
3  *
4  * Re-map IO memory to kernel address space so that we can access it.
5  * This is needed for high PCI addresses that aren't mapped in the
6  * 640k-1MB IO memory area on PC's
7  *
8  * (C) Copyright 1995 1996 Linus Torvalds
9  * (C) Copyright 2001 Helge Deller <deller@gmx.de>
10  */
11
12 #include <linux/vmalloc.h>
13 #include <linux/errno.h>
14 #include <asm/io.h>
15 #include <asm/pgalloc.h>
16
17 static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
18         unsigned long phys_addr, unsigned long flags)
19 {
20         unsigned long end;
21
22         address &= ~PMD_MASK;
23         end = address + size;
24         if (end > PMD_SIZE)
25                 end = PMD_SIZE;
26         if (address >= end)
27                 BUG();
28         do {
29                 if (!pte_none(*pte)) {
30                         printk(KERN_ERR "remap_area_pte: page already exists\n");
31                         BUG();
32                 }
33                 set_pte(pte, mk_pte_phys(phys_addr, __pgprot(_PAGE_PRESENT | _PAGE_RW | 
34                                         _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
35                 address += PAGE_SIZE;
36                 phys_addr += PAGE_SIZE;
37                 pte++;
38         } while (address && (address < end));
39 }
40
41 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
42         unsigned long phys_addr, unsigned long flags)
43 {
44         unsigned long end;
45
46         address &= ~PGDIR_MASK;
47         end = address + size;
48         if (end > PGDIR_SIZE)
49                 end = PGDIR_SIZE;
50         phys_addr -= address;
51         if (address >= end)
52                 BUG();
53         do {
54                 pte_t * pte = pte_alloc_kernel(NULL, pmd, address);
55                 if (!pte)
56                         return -ENOMEM;
57                 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
58                 address = (address + PMD_SIZE) & PMD_MASK;
59                 pmd++;
60         } while (address && (address < end));
61         return 0;
62 }
63
64 #if (USE_HPPA_IOREMAP)
65 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
66                                  unsigned long size, unsigned long flags)
67 {
68         int error;
69         pgd_t * dir;
70         unsigned long end = address + size;
71
72         phys_addr -= address;
73         dir = pgd_offset(&init_mm, address);
74         flush_cache_all();
75         if (address >= end)
76                 BUG();
77         spin_lock(&init_mm.page_table_lock);
78         do {
79                 pmd_t *pmd;
80                 pmd = pmd_alloc(dir, address);
81                 error = -ENOMEM;
82                 if (!pmd)
83                         break;
84                 if (remap_area_pmd(pmd, address, end - address,
85                                          phys_addr + address, flags))
86                         break;
87                 error = 0;
88                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
89                 dir++;
90         } while (address && (address < end));
91         spin_unlock(&init_mm.page_table_lock);
92         flush_tlb_all();
93         return error;
94 }
95 #endif /* USE_HPPA_IOREMAP */
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 #if !(USE_HPPA_IOREMAP)
113
114         unsigned long end = phys_addr + size - 1;
115         /* Support EISA addresses */
116         if ((phys_addr >= 0x00080000 && end < 0x000fffff)
117                         || (phys_addr >= 0x00500000 && end < 0x03bfffff)) {
118                 phys_addr |= 0xfc000000;
119         }
120
121         return (void *)phys_addr;
122
123 #else
124         void * addr;
125         struct vm_struct * area;
126         unsigned long offset, last_addr;
127
128         /* Don't allow wraparound or zero size */
129         last_addr = phys_addr + size - 1;
130         if (!size || last_addr < phys_addr)
131                 return NULL;
132
133         /*
134          * Don't allow anybody to remap normal RAM that we're using..
135          */
136         if (phys_addr < virt_to_phys(high_memory)) {
137                 char *t_addr, *t_end;
138                 struct page *page;
139
140                 t_addr = __va(phys_addr);
141                 t_end = t_addr + (size - 1);
142            
143                 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
144                         if(!PageReserved(page))
145                                 return NULL;
146         }
147
148         /*
149          * Mappings have to be page-aligned
150          */
151         offset = phys_addr & ~PAGE_MASK;
152         phys_addr &= PAGE_MASK;
153         size = PAGE_ALIGN(last_addr) - phys_addr;
154
155         /*
156          * Ok, go for it..
157          */
158         area = get_vm_area(size, VM_IOREMAP);
159         if (!area)
160                 return NULL;
161         addr = area->addr;
162         if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
163                 vfree(addr);
164                 return NULL;
165         }
166         return (void *) (offset + (char *)addr);
167 #endif
168 }
169
170 void iounmap(void *addr)
171 {
172 #if !(USE_HPPA_IOREMAP)
173         return;
174 #else
175         if (addr > high_memory)
176                 return vfree((void *) (PAGE_MASK & (unsigned long) addr));
177 #endif
178 }