Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Al Viro | 73395a0 | 2011-08-18 20:14:10 +0100 | [diff] [blame^] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | static int physmem_fd = -1; |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | /* Changed during early boot */ |
| 20 | unsigned long high_physmem; |
Al Viro | 73395a0 | 2011-08-18 20:14:10 +0100 | [diff] [blame^] | 21 | EXPORT_SYMBOL(high_physmem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Jeff Dike | ae17381 | 2005-11-07 00:58:57 -0800 | [diff] [blame] | 23 | extern unsigned long long physmem_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Jeff Dike | 97a1fcb | 2007-07-23 18:43:48 -0700 | [diff] [blame] | 25 | int __init init_maps(unsigned long physmem, unsigned long iomem, |
| 26 | unsigned long highmem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | struct page *p, *map; |
| 29 | unsigned long phys_len, phys_pages, highmem_len, highmem_pages; |
| 30 | unsigned long iomem_len, iomem_pages, total_len, total_pages; |
| 31 | int i; |
| 32 | |
| 33 | phys_pages = physmem >> PAGE_SHIFT; |
| 34 | phys_len = phys_pages * sizeof(struct page); |
| 35 | |
| 36 | iomem_pages = iomem >> PAGE_SHIFT; |
| 37 | iomem_len = iomem_pages * sizeof(struct page); |
| 38 | |
| 39 | highmem_pages = highmem >> PAGE_SHIFT; |
| 40 | highmem_len = highmem_pages * sizeof(struct page); |
| 41 | |
| 42 | total_pages = phys_pages + iomem_pages + highmem_pages; |
Paolo 'Blaisorblade' Giarrusso | 3dfd95b | 2006-02-01 03:06:26 -0800 | [diff] [blame] | 43 | total_len = phys_len + iomem_len + highmem_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Jeff Dike | 97a1fcb | 2007-07-23 18:43:48 -0700 | [diff] [blame] | 45 | map = alloc_bootmem_low_pages(total_len); |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 46 | if (map == NULL) |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 47 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 49 | for (i = 0; i < total_pages; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | p = &map[i]; |
Nick Piggin | 70dc991 | 2006-03-22 00:08:35 -0800 | [diff] [blame] | 51 | memset(p, 0, sizeof(struct page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | SetPageReserved(p); |
| 53 | INIT_LIST_HEAD(&p->lru); |
| 54 | } |
| 55 | |
| 56 | max_mapnr = total_pages; |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 57 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | void map_memory(unsigned long virt, unsigned long phys, unsigned long len, |
| 61 | int r, int w, int x) |
| 62 | { |
| 63 | __u64 offset; |
| 64 | int fd, err; |
| 65 | |
| 66 | fd = phys_mapping(phys, &offset); |
| 67 | err = os_map_memory((void *) virt, fd, offset, len, r, w, x); |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 68 | if (err) { |
| 69 | if (err == -ENOMEM) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 70 | printk(KERN_ERR "try increasing the host's " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 75 | } |
| 76 | } |
| 77 | |
Jeff Dike | 23bbd586 | 2006-07-10 04:45:06 -0700 | [diff] [blame] | 78 | extern int __syscall_stub_start; |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 79 | |
Jeff Dike | 97a1fcb | 2007-07-23 18:43:48 -0700 | [diff] [blame] | 80 | void __init setup_physmem(unsigned long start, unsigned long reserve_end, |
| 81 | unsigned long len, unsigned long long highmem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 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; |
| 87 | |
| 88 | physmem_fd = create_mem_file(len + highmem); |
| 89 | |
| 90 | offset = uml_reserved - uml_physmem; |
| 91 | err = os_map_memory((void *) uml_reserved, physmem_fd, offset, |
Jeff Dike | 5c8aace | 2007-10-16 01:26:46 -0700 | [diff] [blame] | 92 | len - offset, 1, 1, 1); |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 93 | if (err < 0) { |
Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 94 | printf("setup_physmem - mapping %ld bytes of memory at 0x%p " |
| 95 | "failed - errno = %d\n", len - offset, |
| 96 | (void *) uml_reserved, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | exit(1); |
| 98 | } |
| 99 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 100 | /* |
| 101 | * Special kludge - This page will be mapped in to userspace processes |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 102 | * from physmem_fd, so it needs to be written out there. |
| 103 | */ |
| 104 | os_seek_file(physmem_fd, __pa(&__syscall_stub_start)); |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 105 | os_write_file(physmem_fd, &__syscall_stub_start, PAGE_SIZE); |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | bootmap_size = init_bootmem(pfn, pfn + delta); |
| 108 | free_bootmem(__pa(reserve_end) + bootmap_size, |
| 109 | len - bootmap_size - reserve); |
| 110 | } |
| 111 | |
Jeff Dike | 0a7675a | 2007-10-16 01:27:05 -0700 | [diff] [blame] | 112 | int phys_mapping(unsigned long phys, unsigned long long *offset_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | int fd = -1; |
| 115 | |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 116 | if (phys < physmem_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | fd = physmem_fd; |
| 118 | *offset_out = phys; |
| 119 | } |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 120 | else if (phys < __pa(end_iomem)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | struct iomem_region *region = iomem_regions; |
| 122 | |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 123 | while (region != NULL) { |
| 124 | if ((phys >= region->phys) && |
| 125 | (phys < region->phys + region->size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | fd = region->fd; |
| 127 | *offset_out = phys - region->phys; |
| 128 | break; |
| 129 | } |
| 130 | region = region->next; |
| 131 | } |
| 132 | } |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 133 | else if (phys < __pa(end_iomem) + highmem) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | fd = physmem_fd; |
| 135 | *offset_out = phys - iomem_size; |
| 136 | } |
| 137 | |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 138 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static int __init uml_mem_setup(char *line, int *add) |
| 142 | { |
| 143 | char *retptr; |
| 144 | physmem_size = memparse(line,&retptr); |
| 145 | return 0; |
| 146 | } |
| 147 | __uml_setup("mem=", uml_mem_setup, |
| 148 | "mem=<Amount of desired ram>\n" |
| 149 | " This controls how much \"physical\" memory the kernel allocates\n" |
| 150 | " for the system. The size is specified as a number followed by\n" |
| 151 | " one of 'k', 'K', 'm', 'M', which have the obvious meanings.\n" |
| 152 | " This is not related to the amount of memory in the host. It can\n" |
| 153 | " be more, and the excess, if it's ever used, will just be swapped out.\n" |
| 154 | " Example: mem=64M\n\n" |
| 155 | ); |
| 156 | |
Jeff Dike | 94c282d | 2007-02-10 01:44:09 -0800 | [diff] [blame] | 157 | extern int __init parse_iomem(char *str, int *add); |
| 158 | |
| 159 | __uml_setup("iomem=", parse_iomem, |
| 160 | "iomem=<name>,<file>\n" |
| 161 | " Configure <file> as an IO memory region named <name>.\n\n" |
| 162 | ); |
| 163 | |
| 164 | /* |
| 165 | * This list is constructed in parse_iomem and addresses filled in in |
| 166 | * setup_iomem, both of which run during early boot. Afterwards, it's |
| 167 | * unchanged. |
| 168 | */ |
Jeff Dike | 80e3931 | 2008-02-04 22:31:17 -0800 | [diff] [blame] | 169 | struct iomem_region *iomem_regions; |
Jeff Dike | 94c282d | 2007-02-10 01:44:09 -0800 | [diff] [blame] | 170 | |
Jeff Dike | 80e3931 | 2008-02-04 22:31:17 -0800 | [diff] [blame] | 171 | /* Initialized in parse_iomem and unchanged thereafter */ |
| 172 | int iomem_size; |
Jeff Dike | 94c282d | 2007-02-10 01:44:09 -0800 | [diff] [blame] | 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | unsigned long find_iomem(char *driver, unsigned long *len_out) |
| 175 | { |
| 176 | struct iomem_region *region = iomem_regions; |
| 177 | |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 178 | while (region != NULL) { |
| 179 | if (!strcmp(region->driver, driver)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | *len_out = region->size; |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 181 | return region->virt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
Victor V. Vengerov | c39e50b | 2006-05-01 12:15:53 -0700 | [diff] [blame] | 183 | |
| 184 | region = region->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 187 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
Al Viro | 73395a0 | 2011-08-18 20:14:10 +0100 | [diff] [blame^] | 189 | EXPORT_SYMBOL(find_iomem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
WANG Cong | 99764fa | 2008-07-23 21:28:49 -0700 | [diff] [blame] | 191 | static int setup_iomem(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | struct iomem_region *region = iomem_regions; |
| 194 | unsigned long iomem_start = high_physmem + PAGE_SIZE; |
| 195 | int err; |
| 196 | |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 197 | while (region != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | err = os_map_memory((void *) iomem_start, region->fd, 0, |
| 199 | region->size, 1, 1, 0); |
Jeff Dike | 6d536e4 | 2007-10-16 01:26:47 -0700 | [diff] [blame] | 200 | if (err) |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 201 | printk(KERN_ERR "Mapping iomem region for driver '%s' " |
| 202 | "failed, errno = %d\n", region->driver, -err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | else { |
| 204 | region->virt = iomem_start; |
| 205 | region->phys = __pa(region->virt); |
| 206 | } |
| 207 | |
| 208 | iomem_start += region->size + PAGE_SIZE; |
| 209 | region = region->next; |
| 210 | } |
| 211 | |
Jeff Dike | 60678bb | 2007-02-10 01:44:10 -0800 | [diff] [blame] | 212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | __initcall(setup_iomem); |