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