8aeac4d9351168e30ba8fc978caf679fce47077d
[linux-2.6.git] / arch / um / kernel / physmem.c
1 /*
2  * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/mm.h"
7 #include "linux/rbtree.h"
8 #include "linux/slab.h"
9 #include "linux/vmalloc.h"
10 #include "linux/bootmem.h"
11 #include "linux/module.h"
12 #include "linux/pfn.h"
13 #include "asm/types.h"
14 #include "asm/pgtable.h"
15 #include "kern_util.h"
16 #include "as-layout.h"
17 #include "mode_kern.h"
18 #include "mem.h"
19 #include "mem_user.h"
20 #include "os.h"
21 #include "kern.h"
22 #include "init.h"
23
24 static int physmem_fd = -1;
25
26 /* Changed during early boot */
27 unsigned long high_physmem;
28
29 extern unsigned long long physmem_size;
30
31 int __init init_maps(unsigned long physmem, unsigned long iomem,
32                      unsigned long highmem)
33 {
34         struct page *p, *map;
35         unsigned long phys_len, phys_pages, highmem_len, highmem_pages;
36         unsigned long iomem_len, iomem_pages, total_len, total_pages;
37         int i;
38
39         phys_pages = physmem >> PAGE_SHIFT;
40         phys_len = phys_pages * sizeof(struct page);
41
42         iomem_pages = iomem >> PAGE_SHIFT;
43         iomem_len = iomem_pages * sizeof(struct page);
44
45         highmem_pages = highmem >> PAGE_SHIFT;
46         highmem_len = highmem_pages * sizeof(struct page);
47
48         total_pages = phys_pages + iomem_pages + highmem_pages;
49         total_len = phys_len + iomem_len + highmem_len;
50
51         map = alloc_bootmem_low_pages(total_len);
52         if(map == NULL)
53                 return -ENOMEM;
54
55         for(i = 0; i < total_pages; i++){
56                 p = &map[i];
57                 memset(p, 0, sizeof(struct page));
58                 SetPageReserved(p);
59                 INIT_LIST_HEAD(&p->lru);
60         }
61
62         max_mapnr = total_pages;
63         return 0;
64 }
65
66 /* Changed during early boot */
67 static unsigned long kmem_top = 0;
68
69 unsigned long get_kmem_end(void)
70 {
71         if(kmem_top == 0)
72                 kmem_top = CHOOSE_MODE(kmem_end_tt, kmem_end_skas);
73         return kmem_top;
74 }
75
76 void map_memory(unsigned long virt, unsigned long phys, unsigned long len,
77                 int r, int w, int x)
78 {
79         __u64 offset;
80         int fd, err;
81
82         fd = phys_mapping(phys, &offset);
83         err = os_map_memory((void *) virt, fd, offset, len, r, w, x);
84         if(err) {
85                 if(err == -ENOMEM)
86                         printk("try increasing the host's "
87                                "/proc/sys/vm/max_map_count to <physical "
88                                "memory size>/4096\n");
89                 panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, "
90                       "err = %d\n", virt, fd, offset, len, r, w, x, err);
91         }
92 }
93
94 extern int __syscall_stub_start;
95
96 void __init setup_physmem(unsigned long start, unsigned long reserve_end,
97                           unsigned long len, unsigned long long highmem)
98 {
99         unsigned long reserve = reserve_end - start;
100         int pfn = PFN_UP(__pa(reserve_end));
101         int delta = (len - reserve) >> PAGE_SHIFT;
102         int err, offset, bootmap_size;
103
104         physmem_fd = create_mem_file(len + highmem);
105
106         offset = uml_reserved - uml_physmem;
107         err = os_map_memory((void *) uml_reserved, physmem_fd, offset,
108                             len - offset, 1, 1, 1);
109         if(err < 0){
110                 os_print_error(err, "Mapping memory");
111                 exit(1);
112         }
113
114         /* Special kludge - This page will be mapped in to userspace processes
115          * from physmem_fd, so it needs to be written out there.
116          */
117         os_seek_file(physmem_fd, __pa(&__syscall_stub_start));
118         os_write_file(physmem_fd, &__syscall_stub_start, PAGE_SIZE);
119
120         bootmap_size = init_bootmem(pfn, pfn + delta);
121         free_bootmem(__pa(reserve_end) + bootmap_size,
122                      len - bootmap_size - reserve);
123 }
124
125 int phys_mapping(unsigned long phys, __u64 *offset_out)
126 {
127         int fd = -1;
128
129         if(phys < physmem_size){
130                 fd = physmem_fd;
131                 *offset_out = phys;
132         }
133         else if(phys < __pa(end_iomem)){
134                 struct iomem_region *region = iomem_regions;
135
136                 while(region != NULL){
137                         if((phys >= region->phys) &&
138                            (phys < region->phys + region->size)){
139                                 fd = region->fd;
140                                 *offset_out = phys - region->phys;
141                                 break;
142                         }
143                         region = region->next;
144                 }
145         }
146         else if(phys < __pa(end_iomem) + highmem){
147                 fd = physmem_fd;
148                 *offset_out = phys - iomem_size;
149         }
150
151         return fd;
152 }
153
154 static int __init uml_mem_setup(char *line, int *add)
155 {
156         char *retptr;
157         physmem_size = memparse(line,&retptr);
158         return 0;
159 }
160 __uml_setup("mem=", uml_mem_setup,
161 "mem=<Amount of desired ram>\n"
162 "    This controls how much \"physical\" memory the kernel allocates\n"
163 "    for the system. The size is specified as a number followed by\n"
164 "    one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
165 "    This is not related to the amount of memory in the host.  It can\n"
166 "    be more, and the excess, if it's ever used, will just be swapped out.\n"
167 "       Example: mem=64M\n\n"
168 );
169
170 extern int __init parse_iomem(char *str, int *add);
171
172 __uml_setup("iomem=", parse_iomem,
173 "iomem=<name>,<file>\n"
174 "    Configure <file> as an IO memory region named <name>.\n\n"
175 );
176
177 /*
178  * This list is constructed in parse_iomem and addresses filled in in
179  * setup_iomem, both of which run during early boot.  Afterwards, it's
180  * unchanged.
181  */
182 struct iomem_region *iomem_regions = NULL;
183
184 /* Initialized in parse_iomem */
185 int iomem_size = 0;
186
187 unsigned long find_iomem(char *driver, unsigned long *len_out)
188 {
189         struct iomem_region *region = iomem_regions;
190
191         while(region != NULL){
192                 if(!strcmp(region->driver, driver)){
193                         *len_out = region->size;
194                         return region->virt;
195                 }
196
197                 region = region->next;
198         }
199
200         return 0;
201 }
202
203 int setup_iomem(void)
204 {
205         struct iomem_region *region = iomem_regions;
206         unsigned long iomem_start = high_physmem + PAGE_SIZE;
207         int err;
208
209         while(region != NULL){
210                 err = os_map_memory((void *) iomem_start, region->fd, 0,
211                                     region->size, 1, 1, 0);
212                 if(err)
213                         printk("Mapping iomem region for driver '%s' failed, "
214                                "errno = %d\n", region->driver, -err);
215                 else {
216                         region->virt = iomem_start;
217                         region->phys = __pa(region->virt);
218                 }
219
220                 iomem_start += region->size + PAGE_SIZE;
221                 region = region->next;
222         }
223
224         return 0;
225 }
226
227 __initcall(setup_iomem);