blob: 5b7eba5f3514f7a7a918a891013678593b509cb9 [file] [log] [blame]
Kevin O'Connor59a23bb2008-06-08 23:09:42 -04001// Support for building memory maps suitable for int 15 e820 calls.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor59a23bb2008-06-08 23:09:42 -04006
7#include "memmap.h" // struct e820entry
8#include "util.h" // dprintf.h
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "biosvar.h" // SET_EBDA
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040010
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040011// Remove an entry from the e820_list.
12static void
13remove_e820(int i)
14{
15 e820_count--;
16 memmove(&e820_list[i], &e820_list[i+1]
17 , sizeof(e820_list[0]) * (e820_count - i));
18}
19
20// Insert an entry in the e820_list at the given position.
21static void
22insert_e820(int i, u64 start, u64 size, u32 type)
23{
Kevin O'Connord995b3d2008-11-08 13:05:27 -050024 if (e820_count >= CONFIG_MAX_E820) {
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040025 dprintf(1, "Overflowed e820 list!\n");
26 return;
27 }
28
29 memmove(&e820_list[i+1], &e820_list[i]
30 , sizeof(e820_list[0]) * (e820_count - i));
31 e820_count++;
32 struct e820entry *e = &e820_list[i];
33 e->start = start;
34 e->size = size;
35 e->type = type;
36}
37
38// Show the current e820_list.
39static void
40dump_map()
41{
42 dprintf(1, "e820 map has %d items:\n", e820_count);
43 int i;
44 for (i=0; i<e820_count; i++) {
45 struct e820entry *e = &e820_list[i];
46 u64 e_end = e->start + e->size;
47 dprintf(1, " %d: %x%x - %x%x = %d\n", i
48 , (u32)(e->start >> 32), (u32)e->start
49 , (u32)(e_end >> 32), (u32)e_end
50 , e->type);
51 }
52}
53
54// Add a new entry to the list. This scans for overlaps and keeps the
55// list sorted.
56void
57add_e820(u64 start, u64 size, u32 type)
58{
59 dprintf(8, "Add to e820 map: %x %x %d\n", (u32)start, (u32)size, type);
60
61 if (! size)
62 // Huh? Nothing to do.
63 return;
64
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -050065 // Find position of new item (splitting existing item if needed).
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040066 u64 end = start + size;
67 int i;
68 for (i=0; i<e820_count; i++) {
69 struct e820entry *e = &e820_list[i];
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040070 u64 e_end = e->start + e->size;
Kevin O'Connoraa0c66d2008-06-11 21:23:24 -040071 if (start > e_end)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040072 continue;
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -050073 // Found position - check if an existing item needs to be split.
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040074 if (start > e->start) {
75 e->size = start - e->start;
76 i++;
77 if (end < e_end)
78 // Need to split existing item
79 insert_e820(i, end, e_end - end, e->type);
Kevin O'Connoraa0c66d2008-06-11 21:23:24 -040080 if (type == e->type) {
81 // Same type - merge them.
82 size += start - e->start;
83 start = e->start;
84 i--;
85 remove_e820(i);
86 }
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040087 }
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -050088 break;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040089 }
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -050090 // Insert new item.
91 if (type != E820_HOLE) {
92 insert_e820(i, start, size, type);
93 i++;
94 }
95 // Remove/adjust existing items that are overlapping.
96 while (i<e820_count) {
97 struct e820entry *e = &e820_list[i];
98 if (end < e->start)
99 // No overlap - done.
100 break;
101 u64 e_end = e->start + e->size;
102 if (end >= e_end) {
103 // Existing item completely overlapped - remove it.
104 remove_e820(i);
105 continue;
106 }
107 // Not completely overlapped - adjust its start.
108 e->start = end;
109 e->size = e_end - e->start;
110 if (type == e->type) {
111 // Same type - merge them.
112 (e-1)->size += e->size;
113 remove_e820(i);
114 }
115 break;
116 }
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400117 //dump_map();
118}
119
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400120// Prep for memmap stuff - init bios table locations.
121void
122memmap_setup()
123{
Kevin O'Connor07633812009-02-17 22:36:49 -0500124 memset(BiosTableSpace, 0, CONFIG_MAX_BIOSTABLE);
Kevin O'Connordf2c19a2009-01-17 20:07:09 -0500125 bios_table_cur_addr = (u32)BiosTableSpace;
126 bios_table_end_addr = bios_table_cur_addr + CONFIG_MAX_BIOSTABLE;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400127 dprintf(1, "bios_table_addr: 0x%08x end=0x%08x\n",
128 bios_table_cur_addr, bios_table_end_addr);
Kevin O'Connord995b3d2008-11-08 13:05:27 -0500129
Kevin O'Connoracf13742008-11-29 11:19:19 -0500130 e820_count = 0;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400131}
132
Kevin O'Connor9649a962008-12-10 20:53:35 -0500133// Report on final memory locations.
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400134void
135memmap_finalize()
136{
137 dump_map();
138
Kevin O'Connor15b31652008-08-29 21:21:09 -0400139 dprintf(1, "final bios_table_addr: 0x%08x (used %d%%)\n"
140 , bios_table_cur_addr
Kevin O'Connordf2c19a2009-01-17 20:07:09 -0500141 , (100 * (bios_table_cur_addr - (u32)&BiosTableSpace)
142 / CONFIG_MAX_BIOSTABLE));
Kevin O'Connor0525d292008-07-04 06:18:30 -0400143 if (bios_table_cur_addr > bios_table_end_addr)
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500144 panic("bios_table_end_addr overflow!\n");
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400145}