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>
14 static inline void remap_area_pte(pte_t
* pte
, unsigned long address
, unsigned long size
,
15 unsigned long phys_addr
, unsigned long flags
)
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
)));
29 phys_addr
+= PAGE_SIZE
;
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
)
39 address
&= ~PGDIR_MASK
;
45 pte_t
* pte
= pte_alloc_kernel(pmd
, address
);
48 remap_area_pte(pte
, address
, end
- address
, address
+ phys_addr
, flags
);
49 address
= (address
+ PMD_SIZE
) & PMD_MASK
;
51 } while (address
< end
);
55 static int remap_area_pages(unsigned long address
, unsigned long phys_addr
,
56 unsigned long size
, unsigned long flags
)
59 unsigned long end
= address
+ size
;
62 dir
= pgd_offset(&init_mm
, address
);
64 while (address
< end
) {
65 pmd_t
*pmd
= pmd_alloc_kernel(dir
, address
);
68 if (remap_area_pmd(pmd
, address
, end
- address
,
69 phys_addr
+ address
, flags
))
71 set_pgdir(address
, *dir
);
72 address
= (address
+ PGDIR_SIZE
) & PGDIR_MASK
;
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
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
)
95 struct vm_struct
* area
;
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
))
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
)
126 area
= get_vm_area(size
);
130 if (remap_area_pages(VMALLOC_VMADDR(addr
), phys_addr
, size
, flags
)) {
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
));