blob: e66432f42485dad02584bb13a6bc28a626aa0814 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dike6d536e42007-10-16 01:26:47 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "linux/bootmem.h"
Jeff Dike6d536e42007-10-16 01:26:47 -07007#include "linux/mm.h"
Dave Hansen22a98352006-03-27 01:16:04 -08008#include "linux/pfn.h"
Jeff Dike6d536e42007-10-16 01:26:47 -07009#include "asm/page.h"
Jeff Dike4ff83ce2007-05-06 14:51:08 -070010#include "as-layout.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "init.h"
Jeff Dike6d536e42007-10-16 01:26:47 -070012#include "kern.h"
Jeff Dike77bf4402007-10-16 01:26:58 -070013#include "mem_user.h"
Jeff Dike6d536e42007-10-16 01:26:47 -070014#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016static int physmem_fd = -1;
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/* Changed during early boot */
19unsigned long high_physmem;
20
Jeff Dikeae173812005-11-07 00:58:57 -080021extern unsigned long long physmem_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Jeff Dike97a1fcb2007-07-23 18:43:48 -070023int __init init_maps(unsigned long physmem, unsigned long iomem,
24 unsigned long highmem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 struct page *p, *map;
27 unsigned long phys_len, phys_pages, highmem_len, highmem_pages;
28 unsigned long iomem_len, iomem_pages, total_len, total_pages;
29 int i;
30
31 phys_pages = physmem >> PAGE_SHIFT;
32 phys_len = phys_pages * sizeof(struct page);
33
34 iomem_pages = iomem >> PAGE_SHIFT;
35 iomem_len = iomem_pages * sizeof(struct page);
36
37 highmem_pages = highmem >> PAGE_SHIFT;
38 highmem_len = highmem_pages * sizeof(struct page);
39
40 total_pages = phys_pages + iomem_pages + highmem_pages;
Paolo 'Blaisorblade' Giarrusso3dfd95b2006-02-01 03:06:26 -080041 total_len = phys_len + iomem_len + highmem_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jeff Dike97a1fcb2007-07-23 18:43:48 -070043 map = alloc_bootmem_low_pages(total_len);
Jeff Dike6d536e42007-10-16 01:26:47 -070044 if (map == NULL)
Jeff Dike60678bb2007-02-10 01:44:10 -080045 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jeff Dike6d536e42007-10-16 01:26:47 -070047 for (i = 0; i < total_pages; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 p = &map[i];
Nick Piggin70dc9912006-03-22 00:08:35 -080049 memset(p, 0, sizeof(struct page));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 SetPageReserved(p);
51 INIT_LIST_HEAD(&p->lru);
52 }
53
54 max_mapnr = total_pages;
Jeff Dike60678bb2007-02-10 01:44:10 -080055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* Changed during early boot */
59static unsigned long kmem_top = 0;
60
61unsigned long get_kmem_end(void)
62{
Jeff Dike6d536e42007-10-16 01:26:47 -070063 if (kmem_top == 0)
Jeff Dike77bf4402007-10-16 01:26:58 -070064 kmem_top = host_task_size - 1024 * 1024;
Jeff Dike60678bb2007-02-10 01:44:10 -080065 return kmem_top;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68void map_memory(unsigned long virt, unsigned long phys, unsigned long len,
69 int r, int w, int x)
70{
71 __u64 offset;
72 int fd, err;
73
74 fd = phys_mapping(phys, &offset);
75 err = os_map_memory((void *) virt, fd, offset, len, r, w, x);
Jeff Dike6d536e42007-10-16 01:26:47 -070076 if (err) {
77 if (err == -ENOMEM)
Jeff Dikeba180fd2007-10-16 01:27:00 -070078 printk(KERN_ERR "try increasing the host's "
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 "/proc/sys/vm/max_map_count to <physical "
80 "memory size>/4096\n");
81 panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, "
82 "err = %d\n", virt, fd, offset, len, r, w, x, err);
83 }
84}
85
Jeff Dike23bbd5862006-07-10 04:45:06 -070086extern int __syscall_stub_start;
Jeff Diked67b5692005-07-07 17:56:49 -070087
Jeff Dike97a1fcb2007-07-23 18:43:48 -070088void __init setup_physmem(unsigned long start, unsigned long reserve_end,
89 unsigned long len, unsigned long long highmem)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 unsigned long reserve = reserve_end - start;
92 int pfn = PFN_UP(__pa(reserve_end));
93 int delta = (len - reserve) >> PAGE_SHIFT;
94 int err, offset, bootmap_size;
95
96 physmem_fd = create_mem_file(len + highmem);
97
98 offset = uml_reserved - uml_physmem;
99 err = os_map_memory((void *) uml_reserved, physmem_fd, offset,
Jeff Dike5c8aace2007-10-16 01:26:46 -0700100 len - offset, 1, 1, 1);
Jeff Dike6d536e42007-10-16 01:26:47 -0700101 if (err < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700102 printf("setup_physmem - mapping %ld bytes of memory at 0x%p "
103 "failed - errno = %d\n", len - offset,
104 (void *) uml_reserved, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 exit(1);
106 }
107
Jeff Dikeba180fd2007-10-16 01:27:00 -0700108 /*
109 * Special kludge - This page will be mapped in to userspace processes
Jeff Diked67b5692005-07-07 17:56:49 -0700110 * from physmem_fd, so it needs to be written out there.
111 */
112 os_seek_file(physmem_fd, __pa(&__syscall_stub_start));
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700113 os_write_file(physmem_fd, &__syscall_stub_start, PAGE_SIZE);
Jeff Diked67b5692005-07-07 17:56:49 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 bootmap_size = init_bootmem(pfn, pfn + delta);
116 free_bootmem(__pa(reserve_end) + bootmap_size,
117 len - bootmap_size - reserve);
118}
119
Jeff Dike0a7675a2007-10-16 01:27:05 -0700120int phys_mapping(unsigned long phys, unsigned long long *offset_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int fd = -1;
123
Jeff Dike6d536e42007-10-16 01:26:47 -0700124 if (phys < physmem_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 fd = physmem_fd;
126 *offset_out = phys;
127 }
Jeff Dike6d536e42007-10-16 01:26:47 -0700128 else if (phys < __pa(end_iomem)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct iomem_region *region = iomem_regions;
130
Jeff Dike6d536e42007-10-16 01:26:47 -0700131 while (region != NULL) {
132 if ((phys >= region->phys) &&
133 (phys < region->phys + region->size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 fd = region->fd;
135 *offset_out = phys - region->phys;
136 break;
137 }
138 region = region->next;
139 }
140 }
Jeff Dike6d536e42007-10-16 01:26:47 -0700141 else if (phys < __pa(end_iomem) + highmem) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 fd = physmem_fd;
143 *offset_out = phys - iomem_size;
144 }
145
Jeff Dike60678bb2007-02-10 01:44:10 -0800146 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149static int __init uml_mem_setup(char *line, int *add)
150{
151 char *retptr;
152 physmem_size = memparse(line,&retptr);
153 return 0;
154}
155__uml_setup("mem=", uml_mem_setup,
156"mem=<Amount of desired ram>\n"
157" This controls how much \"physical\" memory the kernel allocates\n"
158" for the system. The size is specified as a number followed by\n"
159" one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
160" This is not related to the amount of memory in the host. It can\n"
161" be more, and the excess, if it's ever used, will just be swapped out.\n"
162" Example: mem=64M\n\n"
163);
164
Jeff Dike94c282d2007-02-10 01:44:09 -0800165extern int __init parse_iomem(char *str, int *add);
166
167__uml_setup("iomem=", parse_iomem,
168"iomem=<name>,<file>\n"
169" Configure <file> as an IO memory region named <name>.\n\n"
170);
171
172/*
173 * This list is constructed in parse_iomem and addresses filled in in
174 * setup_iomem, both of which run during early boot. Afterwards, it's
175 * unchanged.
176 */
177struct iomem_region *iomem_regions = NULL;
178
179/* Initialized in parse_iomem */
180int iomem_size = 0;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182unsigned long find_iomem(char *driver, unsigned long *len_out)
183{
184 struct iomem_region *region = iomem_regions;
185
Jeff Dike6d536e42007-10-16 01:26:47 -0700186 while (region != NULL) {
187 if (!strcmp(region->driver, driver)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 *len_out = region->size;
Jeff Dike60678bb2007-02-10 01:44:10 -0800189 return region->virt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Victor V. Vengerovc39e50b2006-05-01 12:15:53 -0700191
192 region = region->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
Jeff Dike60678bb2007-02-10 01:44:10 -0800195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198int setup_iomem(void)
199{
200 struct iomem_region *region = iomem_regions;
201 unsigned long iomem_start = high_physmem + PAGE_SIZE;
202 int err;
203
Jeff Dike6d536e42007-10-16 01:26:47 -0700204 while (region != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 err = os_map_memory((void *) iomem_start, region->fd, 0,
206 region->size, 1, 1, 0);
Jeff Dike6d536e42007-10-16 01:26:47 -0700207 if (err)
Jeff Dikeba180fd2007-10-16 01:27:00 -0700208 printk(KERN_ERR "Mapping iomem region for driver '%s' "
209 "failed, errno = %d\n", region->driver, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 else {
211 region->virt = iomem_start;
212 region->phys = __pa(region->virt);
213 }
214
215 iomem_start += region->size + PAGE_SIZE;
216 region = region->next;
217 }
218
Jeff Dike60678bb2007-02-10 01:44:10 -0800219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222__initcall(setup_iomem);