2 * arch/i386/mm/ioremap.c
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
8 * (C) Copyright 1995 1996 Linus Torvalds
11 #include <linux/vmalloc.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
15 #include <asm/fixmap.h>
16 #include <asm/cacheflush.h>
17 #include <asm/tlbflush.h>
18 #include <asm/pgtable.h>
20 static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
21 unsigned long phys_addr, unsigned long flags)
32 pfn = phys_addr >> PAGE_SHIFT;
34 if (!pte_none(*pte)) {
35 printk("remap_area_pte: page already exists\n");
38 set_pte(pte, pfn_pte(pfn, __pgprot(_PAGE_PRESENT | _PAGE_RW |
39 _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
43 } while (address && (address < end));
46 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
47 unsigned long phys_addr, unsigned long flags)
51 address &= ~PGDIR_MASK;
59 pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
62 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
63 address = (address + PMD_SIZE) & PMD_MASK;
65 } while (address && (address < end));
69 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
70 unsigned long size, unsigned long flags)
74 unsigned long end = address + size;
77 dir = pgd_offset(&init_mm, address);
81 spin_lock(&init_mm.page_table_lock);
84 pmd = pmd_alloc(&init_mm, dir, address);
88 if (remap_area_pmd(pmd, address, end - address,
89 phys_addr + address, flags))
92 address = (address + PGDIR_SIZE) & PGDIR_MASK;
94 } while (address && (address < end));
95 spin_unlock(&init_mm.page_table_lock);
101 * Generic mapping function (not visible outside):
105 * Remap an arbitrary physical address space into the kernel virtual
106 * address space. Needed when the kernel wants to access high addresses
109 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
110 * have to convert them into an offset in a page-aligned mapping, but the
111 * caller shouldn't need to know that small detail.
113 void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
116 struct vm_struct * area;
117 unsigned long offset, last_addr;
119 /* Don't allow wraparound or zero size */
120 last_addr = phys_addr + size - 1;
121 if (!size || last_addr < phys_addr)
125 * Don't remap the low PCI/ISA area, it's always mapped..
127 if (phys_addr >= 0xA0000 && last_addr < 0x100000)
128 return phys_to_virt(phys_addr);
131 * Don't allow anybody to remap normal RAM that we're using..
133 if (phys_addr < virt_to_phys(high_memory)) {
134 char *t_addr, *t_end;
137 t_addr = __va(phys_addr);
138 t_end = t_addr + (size - 1);
140 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
141 if(!PageReserved(page))
146 * Mappings have to be page-aligned
148 offset = phys_addr & ~PAGE_MASK;
149 phys_addr &= PAGE_MASK;
150 size = PAGE_ALIGN(last_addr+1) - phys_addr;
155 area = get_vm_area(size, VM_IOREMAP);
158 area->phys_addr = phys_addr;
160 if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
164 return (void *) (offset + (char *)addr);
169 * ioremap_nocache - map bus memory into CPU space
170 * @offset: bus address of the memory
171 * @size: size of the resource to map
173 * ioremap_nocache performs a platform specific sequence of operations to
174 * make bus memory CPU accessible via the readb/readw/readl/writeb/
175 * writew/writel functions and the other mmio helpers. The returned
176 * address is not guaranteed to be usable directly as a virtual
179 * This version of ioremap ensures that the memory is marked uncachable
180 * on the CPU as well as honouring existing caching rules from things like
181 * the PCI bus. Note that there are other caches and buffers on many
182 * busses. In particular driver authors should read up on PCI writes
184 * It's useful if some control registers are in such an area and
185 * write combining or read caching is not desirable:
187 * Must be freed with iounmap.
190 void *ioremap_nocache (unsigned long phys_addr, unsigned long size)
192 unsigned long last_addr;
193 void *p = __ioremap(phys_addr, size, _PAGE_PCD);
197 /* Guaranteed to be > phys_addr, as per __ioremap() */
198 last_addr = phys_addr + size - 1;
200 if (last_addr < virt_to_phys(high_memory)) {
201 struct page *ppage = virt_to_page(__va(phys_addr));
202 unsigned long npages;
204 phys_addr &= PAGE_MASK;
206 /* This might overflow and become zero.. */
207 last_addr = PAGE_ALIGN(last_addr);
209 /* .. but that's ok, because modulo-2**n arithmetic will make
210 * the page-aligned "last - first" come out right.
212 npages = (last_addr - phys_addr) >> PAGE_SHIFT;
214 if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) {
224 void iounmap(void *addr)
227 if (addr <= high_memory)
229 p = remove_vm_area((void *) (PAGE_MASK & (unsigned long) addr));
231 printk("__iounmap: bad address %p\n", addr);
235 if (p->flags && p->phys_addr < virt_to_phys(high_memory)) {
236 change_page_attr(virt_to_page(__va(p->phys_addr)),
237 p->size >> PAGE_SHIFT,
244 void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
246 unsigned long offset, last_addr;
247 unsigned int nrpages;
248 enum fixed_addresses idx;
250 /* Don't allow wraparound or zero size */
251 last_addr = phys_addr + size - 1;
252 if (!size || last_addr < phys_addr)
256 * Don't remap the low PCI/ISA area, it's always mapped..
258 if (phys_addr >= 0xA0000 && last_addr < 0x100000)
259 return phys_to_virt(phys_addr);
262 * Mappings have to be page-aligned
264 offset = phys_addr & ~PAGE_MASK;
265 phys_addr &= PAGE_MASK;
266 size = PAGE_ALIGN(last_addr) - phys_addr;
269 * Mappings have to fit in the FIX_BTMAP area.
271 nrpages = size >> PAGE_SHIFT;
272 if (nrpages > NR_FIX_BTMAPS)
278 idx = FIX_BTMAP_BEGIN;
279 while (nrpages > 0) {
280 set_fixmap(idx, phys_addr);
281 phys_addr += PAGE_SIZE;
285 return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
288 void __init bt_iounmap(void *addr, unsigned long size)
290 unsigned long virt_addr;
291 unsigned long offset;
292 unsigned int nrpages;
293 enum fixed_addresses idx;
295 virt_addr = (unsigned long)addr;
296 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
298 offset = virt_addr & ~PAGE_MASK;
299 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
301 idx = FIX_BTMAP_BEGIN;
302 while (nrpages > 0) {