Import 2.2.7pre1
[davej-history.git] / arch / i386 / mm / ioremap.c
blob28250b0bd20649d185f73277ab6748818498b80b
1 /*
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
9 */
11 #include <linux/vmalloc.h>
12 #include <asm/io.h>
14 static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
15 unsigned long phys_addr, unsigned long flags)
17 unsigned long end;
19 address &= ~PMD_MASK;
20 end = address + size;
21 if (end > PMD_SIZE)
22 end = PMD_SIZE;
23 do {
24 if (!pte_none(*pte))
25 printk("remap_area_pte: page already exists\n");
26 set_pte(pte, mk_pte_phys(phys_addr, __pgprot(_PAGE_PRESENT | _PAGE_RW |
27 _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
28 address += PAGE_SIZE;
29 phys_addr += PAGE_SIZE;
30 pte++;
31 } while (address < end);
34 static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
35 unsigned long phys_addr, unsigned long flags)
37 unsigned long end;
39 address &= ~PGDIR_MASK;
40 end = address + size;
41 if (end > PGDIR_SIZE)
42 end = PGDIR_SIZE;
43 phys_addr -= address;
44 do {
45 pte_t * pte = pte_alloc_kernel(pmd, address);
46 if (!pte)
47 return -ENOMEM;
48 remap_area_pte(pte, address, end - address, address + phys_addr, flags);
49 address = (address + PMD_SIZE) & PMD_MASK;
50 pmd++;
51 } while (address < end);
52 return 0;
55 static int remap_area_pages(unsigned long address, unsigned long phys_addr,
56 unsigned long size, unsigned long flags)
58 pgd_t * dir;
59 unsigned long end = address + size;
61 phys_addr -= address;
62 dir = pgd_offset(&init_mm, address);
63 flush_cache_all();
64 while (address < end) {
65 pmd_t *pmd = pmd_alloc_kernel(dir, address);
66 if (!pmd)
67 return -ENOMEM;
68 if (remap_area_pmd(pmd, address, end - address,
69 phys_addr + address, flags))
70 return -ENOMEM;
71 set_pgdir(address, *dir);
72 address = (address + PGDIR_SIZE) & PGDIR_MASK;
73 dir++;
75 flush_tlb_all();
76 return 0;
80 * Generic mapping function (not visible outside):
84 * Remap an arbitrary physical address space into the kernel virtual
85 * address space. Needed when the kernel wants to access high addresses
86 * directly.
88 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
89 * have to convert them into an offset in a page-aligned mapping, but the
90 * caller shouldn't need to know that small detail.
92 void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
94 void * addr;
95 struct vm_struct * area;
96 unsigned long offset;
99 * Don't remap the low PCI/ISA area, it's always mapped..
101 if (phys_addr >= 0xA0000 && (phys_addr+size) <= 0x100000)
102 return phys_to_virt(phys_addr);
105 * Don't allow anybody to remap normal RAM that we're using..
107 if (phys_addr < virt_to_phys(high_memory))
108 return NULL;
111 * Mappings have to be page-aligned
113 offset = phys_addr & ~PAGE_MASK;
114 phys_addr &= PAGE_MASK;
115 size = PAGE_ALIGN(size + offset);
118 * Don't allow mappings that wrap..
120 if (!size || size > phys_addr + size)
121 return NULL;
124 * Ok, go for it..
126 area = get_vm_area(size);
127 if (!area)
128 return NULL;
129 addr = area->addr;
130 if (remap_area_pages(VMALLOC_VMADDR(addr), phys_addr, size, flags)) {
131 vfree(addr);
132 return NULL;
134 return (void *) (offset + (char *)addr);
137 void iounmap(void *addr)
139 if (addr > high_memory)
140 return vfree((void *) (PAGE_MASK & (unsigned long) addr));