Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 1 | // Support for building memory maps suitable for int 15 e820 calls. |
| 2 | // |
Kevin O'Connor | 2929c35 | 2009-07-25 13:48:27 -0400 | [diff] [blame] | 3 | // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 4 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 6 | |
| 7 | #include "memmap.h" // struct e820entry |
| 8 | #include "util.h" // dprintf.h |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 9 | #include "biosvar.h" // SET_EBDA |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 10 | |
Kevin O'Connor | 2929c35 | 2009-07-25 13:48:27 -0400 | [diff] [blame] | 11 | |
| 12 | /**************************************************************** |
| 13 | * e820 memory map |
| 14 | ****************************************************************/ |
| 15 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 16 | // Remove an entry from the e820_list. |
| 17 | static void |
| 18 | remove_e820(int i) |
| 19 | { |
| 20 | e820_count--; |
| 21 | memmove(&e820_list[i], &e820_list[i+1] |
| 22 | , sizeof(e820_list[0]) * (e820_count - i)); |
| 23 | } |
| 24 | |
| 25 | // Insert an entry in the e820_list at the given position. |
| 26 | static void |
| 27 | insert_e820(int i, u64 start, u64 size, u32 type) |
| 28 | { |
Kevin O'Connor | d995b3d | 2008-11-08 13:05:27 -0500 | [diff] [blame] | 29 | if (e820_count >= CONFIG_MAX_E820) { |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 30 | dprintf(1, "Overflowed e820 list!\n"); |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | memmove(&e820_list[i+1], &e820_list[i] |
| 35 | , sizeof(e820_list[0]) * (e820_count - i)); |
| 36 | e820_count++; |
| 37 | struct e820entry *e = &e820_list[i]; |
| 38 | e->start = start; |
| 39 | e->size = size; |
| 40 | e->type = type; |
| 41 | } |
| 42 | |
| 43 | // Show the current e820_list. |
| 44 | static void |
| 45 | dump_map() |
| 46 | { |
| 47 | dprintf(1, "e820 map has %d items:\n", e820_count); |
| 48 | int i; |
| 49 | for (i=0; i<e820_count; i++) { |
| 50 | struct e820entry *e = &e820_list[i]; |
| 51 | u64 e_end = e->start + e->size; |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 52 | dprintf(1, " %d: %08x%08x - %08x%08x = %d\n", i |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 53 | , (u32)(e->start >> 32), (u32)e->start |
| 54 | , (u32)(e_end >> 32), (u32)e_end |
| 55 | , e->type); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Add a new entry to the list. This scans for overlaps and keeps the |
| 60 | // list sorted. |
| 61 | void |
| 62 | add_e820(u64 start, u64 size, u32 type) |
| 63 | { |
Kevin O'Connor | 91b53a7 | 2009-05-05 22:52:09 -0400 | [diff] [blame] | 64 | dprintf(8, "Add to e820 map: %08x %08x %d\n", (u32)start, (u32)size, type); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 65 | |
| 66 | if (! size) |
| 67 | // Huh? Nothing to do. |
| 68 | return; |
| 69 | |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 70 | // Find position of new item (splitting existing item if needed). |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 71 | u64 end = start + size; |
| 72 | int i; |
| 73 | for (i=0; i<e820_count; i++) { |
| 74 | struct e820entry *e = &e820_list[i]; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 75 | u64 e_end = e->start + e->size; |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 76 | if (start > e_end) |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 77 | continue; |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 78 | // Found position - check if an existing item needs to be split. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 79 | if (start > e->start) { |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 80 | if (type == e->type) { |
| 81 | // Same type - merge them. |
| 82 | size += start - e->start; |
| 83 | start = e->start; |
Kevin O'Connor | 1bedcc0 | 2009-04-26 21:25:53 -0400 | [diff] [blame] | 84 | } else { |
| 85 | // Split existing item. |
| 86 | e->size = start - e->start; |
| 87 | i++; |
| 88 | if (e_end > end) |
| 89 | insert_e820(i, end, e_end - end, e->type); |
Kevin O'Connor | aa0c66d | 2008-06-11 21:23:24 -0400 | [diff] [blame] | 90 | } |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 91 | } |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 92 | break; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 93 | } |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 94 | // Remove/adjust existing items that are overlapping. |
| 95 | while (i<e820_count) { |
| 96 | struct e820entry *e = &e820_list[i]; |
| 97 | if (end < e->start) |
| 98 | // No overlap - done. |
| 99 | break; |
| 100 | u64 e_end = e->start + e->size; |
| 101 | if (end >= e_end) { |
| 102 | // Existing item completely overlapped - remove it. |
| 103 | remove_e820(i); |
| 104 | continue; |
| 105 | } |
| 106 | // Not completely overlapped - adjust its start. |
| 107 | e->start = end; |
Kevin O'Connor | 1bedcc0 | 2009-04-26 21:25:53 -0400 | [diff] [blame] | 108 | e->size = e_end - end; |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 109 | if (type == e->type) { |
| 110 | // Same type - merge them. |
Kevin O'Connor | 1bedcc0 | 2009-04-26 21:25:53 -0400 | [diff] [blame] | 111 | size += e->size; |
Kevin O'Connor | 5d8ec3e | 2009-02-27 20:19:27 -0500 | [diff] [blame] | 112 | remove_e820(i); |
| 113 | } |
| 114 | break; |
| 115 | } |
Kevin O'Connor | 1bedcc0 | 2009-04-26 21:25:53 -0400 | [diff] [blame] | 116 | // Insert new item. |
| 117 | if (type != E820_HOLE) |
| 118 | insert_e820(i, start, size, type); |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 119 | //dump_map(); |
| 120 | } |
| 121 | |
Kevin O'Connor | a3855ad | 2009-08-16 21:59:40 -0400 | [diff] [blame] | 122 | // Find highest area of 32bit memory that can hold the given size. |
| 123 | struct e820entry * |
| 124 | find_high_area(u32 size) |
| 125 | { |
| 126 | int i; |
| 127 | for (i=e820_count-1; i>=0; i--) { |
| 128 | struct e820entry *e = &e820_list[i]; |
| 129 | u64 end = e->start + e->size; |
| 130 | if (e->type != E820_RAM || end > 0xffffffff || e->size < size) |
| 131 | continue; |
| 132 | if (end < 1024*1024 + size) |
| 133 | break; |
| 134 | return e; |
| 135 | } |
| 136 | return NULL; |
| 137 | } |
| 138 | |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 139 | // Prep for memmap stuff - init bios table locations. |
| 140 | void |
| 141 | memmap_setup() |
| 142 | { |
Kevin O'Connor | acf1374 | 2008-11-29 11:19:19 -0500 | [diff] [blame] | 143 | e820_count = 0; |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Kevin O'Connor | 9649a96 | 2008-12-10 20:53:35 -0500 | [diff] [blame] | 146 | // Report on final memory locations. |
Kevin O'Connor | 59a23bb | 2008-06-08 23:09:42 -0400 | [diff] [blame] | 147 | void |
| 148 | memmap_finalize() |
| 149 | { |
| 150 | dump_map(); |
Kevin O'Connor | 2929c35 | 2009-07-25 13:48:27 -0400 | [diff] [blame] | 151 | } |