ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sh / mm / ioremap.c
1 /* $Id: ioremap.c,v 1.9 2004/02/25 04:59:10 lethal Exp $
2  *
3  * arch/sh/mm/ioremap.c
4  *
5  * Re-map IO memory to kernel address space so that we can access it.
6  * This is needed for high PCI addresses that aren't mapped in the
7  * 640k-1MB IO memory area on PC's
8  *
9  * (C) Copyright 1995 1996 Linus Torvalds
10  */
11
12 #include <linux/vmalloc.h>
13 #include <linux/mm.h>
14 #include <asm/io.h>
15 #include <asm/page.h>
16 #include <asm/pgalloc.h>
17 #include <asm/cacheflush.h>
18 #include <asm/tlbflush.h>
19
20 static inline void remap_area_pte(pte_t * pte, unsigned long address,
21         unsigned long size, unsigned long phys_addr, unsigned long flags)
22 {
23         unsigned long end;
24         unsigned long pfn;
25         pgprot_t pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW |
26                                    _PAGE_DIRTY | _PAGE_ACCESSED |
27                                    _PAGE_HW_SHARED | _PAGE_FLAGS_HARD | flags);
28
29         address &= ~PMD_MASK;
30         end = address + size;
31         if (end > PMD_SIZE)
32                 end = PMD_SIZE;
33         if (address >= end)
34                 BUG();
35         pfn = phys_addr >> PAGE_SHIFT;
36         do {
37                 if (!pte_none(*pte)) {
38                         printk("remap_area_pte: page already exists\n");
39                         BUG();
40                 }
41                 set_pte(pte, pfn_pte(pfn, pgprot));
42                 address += PAGE_SIZE;
43                 pfn++;
44                 pte++;
45         } while (address && (address < end));
46 }
47
48 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
49         unsigned long size, unsigned long phys_addr, unsigned long flags)
50 {
51         unsigned long end;
52
53         address &= ~PGDIR_MASK;
54         end = address + size;
55         if (end > PGDIR_SIZE)
56                 end = PGDIR_SIZE;
57         phys_addr -= address;
58         if (address >= end)
59                 BUG();
60         do {
61                 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
62                 if (!pte)
63                         return -ENOMEM;
64                 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
65                 address = (address + PMD_SIZE) & PMD_MASK;
66                 pmd++;
67         } while (address && (address < end));
68         return 0;
69 }
70
71 int remap_area_pages(unsigned long address, unsigned long phys_addr,
72                      unsigned long size, unsigned long flags)
73 {
74         int error;
75         pgd_t * dir;
76         unsigned long end = address + size;
77
78         phys_addr -= address;
79         dir = pgd_offset_k(address);
80         flush_cache_all();
81         if (address >= end)
82                 BUG();
83         spin_lock(&init_mm.page_table_lock);
84         do {
85                 pmd_t *pmd;
86                 pmd = pmd_alloc(&init_mm, dir, address);
87                 error = -ENOMEM;
88                 if (!pmd)
89                         break;
90                 if (remap_area_pmd(pmd, address, end - address,
91                                         phys_addr + address, flags))
92                         break;
93                 error = 0;
94                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
95                 dir++;
96         } while (address && (address < end));
97         spin_unlock(&init_mm.page_table_lock);
98         flush_tlb_all();
99         return error;
100 }
101
102 /*
103  * Generic mapping function (not visible outside):
104  */
105
106 /*
107  * Remap an arbitrary physical address space into the kernel virtual
108  * address space. Needed when the kernel wants to access high addresses
109  * directly.
110  *
111  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
112  * have to convert them into an offset in a page-aligned mapping, but the
113  * caller shouldn't need to know that small detail.
114  */
115 void * p3_ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
116 {
117         void * addr;
118         struct vm_struct * area;
119         unsigned long offset, last_addr;
120
121         /* Don't allow wraparound or zero size */
122         last_addr = phys_addr + size - 1;
123         if (!size || last_addr < phys_addr)
124                 return NULL;
125
126         /*
127          * Don't remap the low PCI/ISA area, it's always mapped..
128          */
129         if (phys_addr >= 0xA0000 && last_addr < 0x100000)
130                 return phys_to_virt(phys_addr);
131
132         /*
133          * Don't allow anybody to remap normal RAM that we're using..
134          */
135         if (phys_addr < virt_to_phys(high_memory))
136                 return NULL;
137
138         /*
139          * Mappings have to be page-aligned
140          */
141         offset = phys_addr & ~PAGE_MASK;
142         phys_addr &= PAGE_MASK;
143         size = PAGE_ALIGN(last_addr+1) - phys_addr;
144
145         /*
146          * Ok, go for it..
147          */
148         area = get_vm_area(size, VM_IOREMAP);
149         if (!area)
150                 return NULL;
151         area->phys_addr = phys_addr;
152         addr = area->addr;
153         if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
154                 vunmap(addr);
155                 return NULL;
156         }
157         return (void *) (offset + (char *)addr);
158 }
159
160 void p3_iounmap(void *addr)
161 {
162         if (addr > high_memory)
163                 vfree((void *)(PAGE_MASK & (unsigned long)addr));
164 }