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