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);
87 pud = pud_alloc(&init_mm, dir, address);
90 pmd = pmd_alloc(&init_mm, pud, address);
93 if (remap_area_pmd(pmd, address, end - address,
94 phys_addr + address, flags))
97 address = (address + PGDIR_SIZE) & PGDIR_MASK;
99 } while (address && (address < end));
100 spin_unlock(&init_mm.page_table_lock);
106 * Generic mapping function (not visible outside):
110 * Remap an arbitrary physical address space into the kernel virtual
111 * address space. Needed when the kernel wants to access high addresses
114 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
115 * have to convert them into an offset in a page-aligned mapping, but the
116 * caller shouldn't need to know that small detail.
118 void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
121 struct vm_struct * area;
122 unsigned long offset, last_addr;
124 /* Don't allow wraparound or zero size */
125 last_addr = phys_addr + size - 1;
126 if (!size || last_addr < phys_addr)
130 * Don't remap the low PCI/ISA area, it's always mapped..
132 if (phys_addr >= 0xA0000 && last_addr < 0x100000)
133 return (void __iomem *) phys_to_virt(phys_addr);
136 * Don't allow anybody to remap normal RAM that we're using..
138 if (phys_addr <= virt_to_phys(high_memory - 1)) {
139 char *t_addr, *t_end;
142 t_addr = __va(phys_addr);
143 t_end = t_addr + (size - 1);
145 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
146 if(!PageReserved(page))
151 * Mappings have to be page-aligned
153 offset = phys_addr & ~PAGE_MASK;
154 phys_addr &= PAGE_MASK;
155 size = PAGE_ALIGN(last_addr+1) - phys_addr;
160 area = get_vm_area(size, VM_IOREMAP | (flags << 20));
163 area->phys_addr = phys_addr;
164 addr = (void __iomem *) area->addr;
165 if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
166 vunmap((void __force *) addr);
169 return (void __iomem *) (offset + (char __iomem *)addr);
174 * ioremap_nocache - map bus memory into CPU space
175 * @offset: bus address of the memory
176 * @size: size of the resource to map
178 * ioremap_nocache performs a platform specific sequence of operations to
179 * make bus memory CPU accessible via the readb/readw/readl/writeb/
180 * writew/writel functions and the other mmio helpers. The returned
181 * address is not guaranteed to be usable directly as a virtual
184 * This version of ioremap ensures that the memory is marked uncachable
185 * on the CPU as well as honouring existing caching rules from things like
186 * the PCI bus. Note that there are other caches and buffers on many
187 * busses. In particular driver authors should read up on PCI writes
189 * It's useful if some control registers are in such an area and
190 * write combining or read caching is not desirable:
192 * Must be freed with iounmap.
195 void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
197 unsigned long last_addr;
198 void __iomem *p = __ioremap(phys_addr, size, _PAGE_PCD);
202 /* Guaranteed to be > phys_addr, as per __ioremap() */
203 last_addr = phys_addr + size - 1;
205 if (last_addr < virt_to_phys(high_memory) - 1) {
206 struct page *ppage = virt_to_page(__va(phys_addr));
207 unsigned long npages;
209 phys_addr &= PAGE_MASK;
211 /* This might overflow and become zero.. */
212 last_addr = PAGE_ALIGN(last_addr);
214 /* .. but that's ok, because modulo-2**n arithmetic will make
215 * the page-aligned "last - first" come out right.
217 npages = (last_addr - phys_addr) >> PAGE_SHIFT;
219 if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) {
229 void iounmap(volatile void __iomem *addr)
232 if ((void __force *) addr <= high_memory)
234 p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
236 printk("__iounmap: bad address %p\n", addr);
240 if ((p->flags >> 20) && p->phys_addr < virt_to_phys(high_memory) - 1) {
241 /* p->size includes the guard page, but cpa doesn't like that */
242 change_page_attr(virt_to_page(__va(p->phys_addr)),
243 (p->size - PAGE_SIZE) >> PAGE_SHIFT,
250 void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
252 unsigned long offset, last_addr;
253 unsigned int nrpages;
254 enum fixed_addresses idx;
256 /* Don't allow wraparound or zero size */
257 last_addr = phys_addr + size - 1;
258 if (!size || last_addr < phys_addr)
262 * Don't remap the low PCI/ISA area, it's always mapped..
264 if (phys_addr >= 0xA0000 && last_addr < 0x100000)
265 return phys_to_virt(phys_addr);
268 * Mappings have to be page-aligned
270 offset = phys_addr & ~PAGE_MASK;
271 phys_addr &= PAGE_MASK;
272 size = PAGE_ALIGN(last_addr) - phys_addr;
275 * Mappings have to fit in the FIX_BTMAP area.
277 nrpages = size >> PAGE_SHIFT;
278 if (nrpages > NR_FIX_BTMAPS)
284 idx = FIX_BTMAP_BEGIN;
285 while (nrpages > 0) {
286 set_fixmap(idx, phys_addr);
287 phys_addr += PAGE_SIZE;
291 return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
294 void __init bt_iounmap(void *addr, unsigned long size)
296 unsigned long virt_addr;
297 unsigned long offset;
298 unsigned int nrpages;
299 enum fixed_addresses idx;
301 virt_addr = (unsigned long)addr;
302 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
304 offset = virt_addr & ~PAGE_MASK;
305 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
307 idx = FIX_BTMAP_BEGIN;
308 while (nrpages > 0) {