physmem.c 5.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5 6
 * Licensed under the GPL
 */

#include "linux/bootmem.h"
J
Jeff Dike 已提交
7
#include "linux/mm.h"
D
Dave Hansen 已提交
8
#include "linux/pfn.h"
J
Jeff Dike 已提交
9
#include "asm/page.h"
J
Jeff Dike 已提交
10
#include "as-layout.h"
J
Jeff Dike 已提交
11 12
#include "init.h"
#include "kern.h"
13
#include "mem_user.h"
L
Linus Torvalds 已提交
14 15 16 17 18 19 20
#include "os.h"

static int physmem_fd = -1;

/* Changed during early boot */
unsigned long high_physmem;

J
Jeff Dike 已提交
21
extern unsigned long long physmem_size;
L
Linus Torvalds 已提交
22

J
Jeff Dike 已提交
23 24
int __init init_maps(unsigned long physmem, unsigned long iomem,
		     unsigned long highmem)
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
{
	struct page *p, *map;
	unsigned long phys_len, phys_pages, highmem_len, highmem_pages;
	unsigned long iomem_len, iomem_pages, total_len, total_pages;
	int i;

	phys_pages = physmem >> PAGE_SHIFT;
	phys_len = phys_pages * sizeof(struct page);

	iomem_pages = iomem >> PAGE_SHIFT;
	iomem_len = iomem_pages * sizeof(struct page);

	highmem_pages = highmem >> PAGE_SHIFT;
	highmem_len = highmem_pages * sizeof(struct page);

	total_pages = phys_pages + iomem_pages + highmem_pages;
41
	total_len = phys_len + iomem_len + highmem_len;
L
Linus Torvalds 已提交
42

J
Jeff Dike 已提交
43
	map = alloc_bootmem_low_pages(total_len);
J
Jeff Dike 已提交
44
	if (map == NULL)
45
		return -ENOMEM;
L
Linus Torvalds 已提交
46

J
Jeff Dike 已提交
47
	for (i = 0; i < total_pages; i++) {
L
Linus Torvalds 已提交
48
		p = &map[i];
49
		memset(p, 0, sizeof(struct page));
L
Linus Torvalds 已提交
50 51 52 53 54
		SetPageReserved(p);
		INIT_LIST_HEAD(&p->lru);
	}

	max_mapnr = total_pages;
55
	return 0;
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65
}

void map_memory(unsigned long virt, unsigned long phys, unsigned long len,
		int r, int w, int x)
{
	__u64 offset;
	int fd, err;

	fd = phys_mapping(phys, &offset);
	err = os_map_memory((void *) virt, fd, offset, len, r, w, x);
J
Jeff Dike 已提交
66 67
	if (err) {
		if (err == -ENOMEM)
J
Jeff Dike 已提交
68
			printk(KERN_ERR "try increasing the host's "
L
Linus Torvalds 已提交
69 70 71 72 73 74 75
			       "/proc/sys/vm/max_map_count to <physical "
			       "memory size>/4096\n");
		panic("map_memory(0x%lx, %d, 0x%llx, %ld, %d, %d, %d) failed, "
		      "err = %d\n", virt, fd, offset, len, r, w, x, err);
	}
}

76
extern int __syscall_stub_start;
77

J
Jeff Dike 已提交
78 79
void __init setup_physmem(unsigned long start, unsigned long reserve_end,
			  unsigned long len, unsigned long long highmem)
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89
{
	unsigned long reserve = reserve_end - start;
	int pfn = PFN_UP(__pa(reserve_end));
	int delta = (len - reserve) >> PAGE_SHIFT;
	int err, offset, bootmap_size;

	physmem_fd = create_mem_file(len + highmem);

	offset = uml_reserved - uml_physmem;
	err = os_map_memory((void *) uml_reserved, physmem_fd, offset,
90
			    len - offset, 1, 1, 1);
J
Jeff Dike 已提交
91
	if (err < 0) {
92 93 94
		printf("setup_physmem - mapping %ld bytes of memory at 0x%p "
		       "failed - errno = %d\n", len - offset,
		       (void *) uml_reserved, err);
L
Linus Torvalds 已提交
95 96 97
		exit(1);
	}

J
Jeff Dike 已提交
98 99
	/*
	 * Special kludge - This page will be mapped in to userspace processes
100 101 102
	 * from physmem_fd, so it needs to be written out there.
	 */
	os_seek_file(physmem_fd, __pa(&__syscall_stub_start));
103
	os_write_file(physmem_fd, &__syscall_stub_start, PAGE_SIZE);
104

L
Linus Torvalds 已提交
105 106 107 108 109
	bootmap_size = init_bootmem(pfn, pfn + delta);
	free_bootmem(__pa(reserve_end) + bootmap_size,
		     len - bootmap_size - reserve);
}

110
int phys_mapping(unsigned long phys, unsigned long long *offset_out)
L
Linus Torvalds 已提交
111 112 113
{
	int fd = -1;

J
Jeff Dike 已提交
114
	if (phys < physmem_size) {
L
Linus Torvalds 已提交
115 116 117
		fd = physmem_fd;
		*offset_out = phys;
	}
J
Jeff Dike 已提交
118
	else if (phys < __pa(end_iomem)) {
L
Linus Torvalds 已提交
119 120
		struct iomem_region *region = iomem_regions;

J
Jeff Dike 已提交
121 122 123
		while (region != NULL) {
			if ((phys >= region->phys) &&
			    (phys < region->phys + region->size)) {
L
Linus Torvalds 已提交
124 125 126 127 128 129 130
				fd = region->fd;
				*offset_out = phys - region->phys;
				break;
			}
			region = region->next;
		}
	}
J
Jeff Dike 已提交
131
	else if (phys < __pa(end_iomem) + highmem) {
L
Linus Torvalds 已提交
132 133 134 135
		fd = physmem_fd;
		*offset_out = phys - iomem_size;
	}

136
	return fd;
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

static int __init uml_mem_setup(char *line, int *add)
{
	char *retptr;
	physmem_size = memparse(line,&retptr);
	return 0;
}
__uml_setup("mem=", uml_mem_setup,
"mem=<Amount of desired ram>\n"
"    This controls how much \"physical\" memory the kernel allocates\n"
"    for the system. The size is specified as a number followed by\n"
"    one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n"
"    This is not related to the amount of memory in the host.  It can\n"
"    be more, and the excess, if it's ever used, will just be swapped out.\n"
"	Example: mem=64M\n\n"
);

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
extern int __init parse_iomem(char *str, int *add);

__uml_setup("iomem=", parse_iomem,
"iomem=<name>,<file>\n"
"    Configure <file> as an IO memory region named <name>.\n\n"
);

/*
 * This list is constructed in parse_iomem and addresses filled in in
 * setup_iomem, both of which run during early boot.  Afterwards, it's
 * unchanged.
 */
struct iomem_region *iomem_regions = NULL;

/* Initialized in parse_iomem */
int iomem_size = 0;

L
Linus Torvalds 已提交
172 173 174 175
unsigned long find_iomem(char *driver, unsigned long *len_out)
{
	struct iomem_region *region = iomem_regions;

J
Jeff Dike 已提交
176 177
	while (region != NULL) {
		if (!strcmp(region->driver, driver)) {
L
Linus Torvalds 已提交
178
			*len_out = region->size;
179
			return region->virt;
L
Linus Torvalds 已提交
180
		}
181 182

		region = region->next;
L
Linus Torvalds 已提交
183 184
	}

185
	return 0;
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193
}

int setup_iomem(void)
{
	struct iomem_region *region = iomem_regions;
	unsigned long iomem_start = high_physmem + PAGE_SIZE;
	int err;

J
Jeff Dike 已提交
194
	while (region != NULL) {
L
Linus Torvalds 已提交
195 196
		err = os_map_memory((void *) iomem_start, region->fd, 0,
				    region->size, 1, 1, 0);
J
Jeff Dike 已提交
197
		if (err)
J
Jeff Dike 已提交
198 199
			printk(KERN_ERR "Mapping iomem region for driver '%s' "
			       "failed, errno = %d\n", region->driver, -err);
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208
		else {
			region->virt = iomem_start;
			region->phys = __pa(region->virt);
		}

		iomem_start += region->size + PAGE_SIZE;
		region = region->next;
	}

209
	return 0;
L
Linus Torvalds 已提交
210 211 212
}

__initcall(setup_iomem);