2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include <linux/module.h>
7 #include <linux/bootmem.h>
11 #include <as-layout.h>
17 static int physmem_fd
= -1;
19 /* Changed during early boot */
20 unsigned long high_physmem
;
21 EXPORT_SYMBOL(high_physmem
);
23 extern unsigned long long physmem_size
;
25 int __init
init_maps(unsigned long physmem
, unsigned long iomem
,
26 unsigned long highmem
)
29 unsigned long phys_len
, phys_pages
, highmem_len
, highmem_pages
;
30 unsigned long iomem_len
, iomem_pages
, total_len
, total_pages
;
33 phys_pages
= physmem
>> PAGE_SHIFT
;
34 phys_len
= phys_pages
* sizeof(struct page
);
36 iomem_pages
= iomem
>> PAGE_SHIFT
;
37 iomem_len
= iomem_pages
* sizeof(struct page
);
39 highmem_pages
= highmem
>> PAGE_SHIFT
;
40 highmem_len
= highmem_pages
* sizeof(struct page
);
42 total_pages
= phys_pages
+ iomem_pages
+ highmem_pages
;
43 total_len
= phys_len
+ iomem_len
+ highmem_len
;
45 map
= alloc_bootmem_low_pages(total_len
);
49 for (i
= 0; i
< total_pages
; i
++) {
51 memset(p
, 0, sizeof(struct page
));
53 INIT_LIST_HEAD(&p
->lru
);
56 max_mapnr
= total_pages
;
60 void map_memory(unsigned long virt
, unsigned long phys
, unsigned long len
,
66 fd
= phys_mapping(phys
, &offset
);
67 err
= os_map_memory((void *) virt
, fd
, offset
, len
, r
, w
, x
);
70 printk(KERN_ERR
"try increasing the host's "
71 "/proc/sys/vm/max_map_count to <physical "
72 "memory size>/4096\n");
73 panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, "
74 "err = %d\n", virt
, fd
, offset
, len
, r
, w
, x
, err
);
78 extern int __syscall_stub_start
;
80 void __init
setup_physmem(unsigned long start
, unsigned long reserve_end
,
81 unsigned long len
, unsigned long long highmem
)
83 unsigned long reserve
= reserve_end
- start
;
84 int pfn
= PFN_UP(__pa(reserve_end
));
85 int delta
= (len
- reserve
) >> PAGE_SHIFT
;
86 int err
, offset
, bootmap_size
;
88 physmem_fd
= create_mem_file(len
+ highmem
);
90 offset
= uml_reserved
- uml_physmem
;
91 err
= os_map_memory((void *) uml_reserved
, physmem_fd
, offset
,
92 len
- offset
, 1, 1, 1);
94 printf("setup_physmem - mapping %ld bytes of memory at 0x%p "
95 "failed - errno = %d\n", len
- offset
,
96 (void *) uml_reserved
, err
);
101 * Special kludge - This page will be mapped in to userspace processes
102 * from physmem_fd, so it needs to be written out there.
104 os_seek_file(physmem_fd
, __pa(&__syscall_stub_start
));
105 os_write_file(physmem_fd
, &__syscall_stub_start
, PAGE_SIZE
);
106 os_fsync_file(physmem_fd
);
108 bootmap_size
= init_bootmem(pfn
, pfn
+ delta
);
109 free_bootmem(__pa(reserve_end
) + bootmap_size
,
110 len
- bootmap_size
- reserve
);
113 int phys_mapping(unsigned long phys
, unsigned long long *offset_out
)
117 if (phys
< physmem_size
) {
121 else if (phys
< __pa(end_iomem
)) {
122 struct iomem_region
*region
= iomem_regions
;
124 while (region
!= NULL
) {
125 if ((phys
>= region
->phys
) &&
126 (phys
< region
->phys
+ region
->size
)) {
128 *offset_out
= phys
- region
->phys
;
131 region
= region
->next
;
134 else if (phys
< __pa(end_iomem
) + highmem
) {
136 *offset_out
= phys
- iomem_size
;
142 static int __init
uml_mem_setup(char *line
, int *add
)
145 physmem_size
= memparse(line
,&retptr
);
148 __uml_setup("mem=", uml_mem_setup
,
149 "mem=<Amount of desired ram>\n"
150 " This controls how much \"physical\" memory the kernel allocates\n"
151 " for the system. The size is specified as a number followed by\n"
152 " one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
153 " This is not related to the amount of memory in the host. It can\n"
154 " be more, and the excess, if it's ever used, will just be swapped out.\n"
155 " Example: mem=64M\n\n"
158 extern int __init
parse_iomem(char *str
, int *add
);
160 __uml_setup("iomem=", parse_iomem
,
161 "iomem=<name>,<file>\n"
162 " Configure <file> as an IO memory region named <name>.\n\n"
166 * This list is constructed in parse_iomem and addresses filled in in
167 * setup_iomem, both of which run during early boot. Afterwards, it's
170 struct iomem_region
*iomem_regions
;
172 /* Initialized in parse_iomem and unchanged thereafter */
175 unsigned long find_iomem(char *driver
, unsigned long *len_out
)
177 struct iomem_region
*region
= iomem_regions
;
179 while (region
!= NULL
) {
180 if (!strcmp(region
->driver
, driver
)) {
181 *len_out
= region
->size
;
185 region
= region
->next
;
190 EXPORT_SYMBOL(find_iomem
);
192 static int setup_iomem(void)
194 struct iomem_region
*region
= iomem_regions
;
195 unsigned long iomem_start
= high_physmem
+ PAGE_SIZE
;
198 while (region
!= NULL
) {
199 err
= os_map_memory((void *) iomem_start
, region
->fd
, 0,
200 region
->size
, 1, 1, 0);
202 printk(KERN_ERR
"Mapping iomem region for driver '%s' "
203 "failed, errno = %d\n", region
->driver
, -err
);
205 region
->virt
= iomem_start
;
206 region
->phys
= __pa(region
->virt
);
209 iomem_start
+= region
->size
+ PAGE_SIZE
;
210 region
= region
->next
;
216 __initcall(setup_iomem
);