blob: 39445cf6399dfe1e86230cb093c13ccb1e3ae1cc [file] [log] [blame]
Kevin O'Connor59a23bb2008-06-08 23:09:42 -04001// Support for building memory maps suitable for int 15 e820 calls.
2//
Kevin O'Connor2929c352009-07-25 13:48:27 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor59a23bb2008-06-08 23:09:42 -04004//
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
Kevin O'Connore52ad392013-02-20 23:48:22 -05007#include "config.h" // BUILD_MAX_E820
Kevin O'Connorc167e542015-09-29 09:40:46 -04008#include "e820map.h" // struct e820entry
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04009#include "output.h" // dprintf
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040010#include "string.h" // memmove
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040011
Kevin O'Connor2929c352009-07-25 13:48:27 -040012
13/****************************************************************
14 * e820 memory map
15 ****************************************************************/
16
Kevin O'Connor89a2f962013-02-18 23:36:03 -050017// Info on e820 map location and size.
Kevin O'Connore52ad392013-02-20 23:48:22 -050018struct e820entry e820_list[BUILD_MAX_E820] VARFSEG;
Kevin O'Connor89a2f962013-02-18 23:36:03 -050019int e820_count VARFSEG;
20
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040021// Remove an entry from the e820_list.
22static void
23remove_e820(int i)
24{
25 e820_count--;
26 memmove(&e820_list[i], &e820_list[i+1]
27 , sizeof(e820_list[0]) * (e820_count - i));
28}
29
30// Insert an entry in the e820_list at the given position.
31static void
32insert_e820(int i, u64 start, u64 size, u32 type)
33{
Kevin O'Connore52ad392013-02-20 23:48:22 -050034 if (e820_count >= BUILD_MAX_E820) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050035 warn_noalloc();
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040036 return;
37 }
38
39 memmove(&e820_list[i+1], &e820_list[i]
40 , sizeof(e820_list[0]) * (e820_count - i));
41 e820_count++;
42 struct e820entry *e = &e820_list[i];
43 e->start = start;
44 e->size = size;
45 e->type = type;
46}
47
Ian Campbell1f4e8622011-05-13 16:30:55 +010048static const char *
49e820_type_name(u32 type)
50{
Kevin O'Connor7a08ae72012-03-14 21:11:39 -040051 switch (type) {
52 case E820_RAM: return "RAM";
53 case E820_RESERVED: return "RESERVED";
54 case E820_ACPI: return "ACPI";
55 case E820_NVS: return "NVS";
56 case E820_UNUSABLE: return "UNUSABLE";
Kevin O'Connor7a08ae72012-03-14 21:11:39 -040057 default: return "UNKNOWN";
58 }
Ian Campbell1f4e8622011-05-13 16:30:55 +010059}
60
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040061// Show the current e820_list.
62static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050063dump_map(void)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040064{
65 dprintf(1, "e820 map has %d items:\n", e820_count);
66 int i;
67 for (i=0; i<e820_count; i++) {
68 struct e820entry *e = &e820_list[i];
69 u64 e_end = e->start + e->size;
Kevin O'Connorc04090d2012-03-05 10:14:07 -050070 dprintf(1, " %d: %016llx - %016llx = %d %s\n", i
71 , e->start, e_end, e->type, e820_type_name(e->type));
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040072 }
73}
74
Kevin O'Connor8e385142015-09-29 08:53:38 -040075#define E820_HOLE ((u32)-1) // Used internally to remove entries
76
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040077// Add a new entry to the list. This scans for overlaps and keeps the
78// list sorted.
79void
Kevin O'Connorc167e542015-09-29 09:40:46 -040080e820_add(u64 start, u64 size, u32 type)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040081{
Kevin O'Connor5ba22ab2015-09-30 23:00:43 -040082 dprintf(8, "Add to e820 map: %08llx %08llx %d\n", start, size, type);
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040083
84 if (! size)
85 // Huh? Nothing to do.
86 return;
87
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -050088 // Find position of new item (splitting existing item if needed).
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040089 u64 end = start + size;
90 int i;
91 for (i=0; i<e820_count; i++) {
92 struct e820entry *e = &e820_list[i];
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040093 u64 e_end = e->start + e->size;
Kevin O'Connoraa0c66d2008-06-11 21:23:24 -040094 if (start > e_end)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040095 continue;
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -050096 // Found position - check if an existing item needs to be split.
Kevin O'Connor59a23bb2008-06-08 23:09:42 -040097 if (start > e->start) {
Kevin O'Connoraa0c66d2008-06-11 21:23:24 -040098 if (type == e->type) {
99 // Same type - merge them.
100 size += start - e->start;
101 start = e->start;
Kevin O'Connor1bedcc02009-04-26 21:25:53 -0400102 } else {
103 // Split existing item.
104 e->size = start - e->start;
105 i++;
106 if (e_end > end)
107 insert_e820(i, end, e_end - end, e->type);
Kevin O'Connoraa0c66d2008-06-11 21:23:24 -0400108 }
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400109 }
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -0500110 break;
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400111 }
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -0500112 // Remove/adjust existing items that are overlapping.
113 while (i<e820_count) {
114 struct e820entry *e = &e820_list[i];
115 if (end < e->start)
116 // No overlap - done.
117 break;
118 u64 e_end = e->start + e->size;
119 if (end >= e_end) {
120 // Existing item completely overlapped - remove it.
121 remove_e820(i);
122 continue;
123 }
124 // Not completely overlapped - adjust its start.
125 e->start = end;
Kevin O'Connor1bedcc02009-04-26 21:25:53 -0400126 e->size = e_end - end;
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -0500127 if (type == e->type) {
128 // Same type - merge them.
Kevin O'Connor1bedcc02009-04-26 21:25:53 -0400129 size += e->size;
Kevin O'Connor5d8ec3e2009-02-27 20:19:27 -0500130 remove_e820(i);
131 }
132 break;
133 }
Kevin O'Connor1bedcc02009-04-26 21:25:53 -0400134 // Insert new item.
135 if (type != E820_HOLE)
136 insert_e820(i, start, size, type);
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400137 //dump_map();
138}
139
Kevin O'Connor8e385142015-09-29 08:53:38 -0400140// Remove any definitions in a memory range (make a memory hole).
141void
142e820_remove(u64 start, u64 size)
143{
Kevin O'Connorc167e542015-09-29 09:40:46 -0400144 e820_add(start, size, E820_HOLE);
Kevin O'Connor8e385142015-09-29 08:53:38 -0400145}
146
Kevin O'Connor9649a962008-12-10 20:53:35 -0500147// Report on final memory locations.
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400148void
Kevin O'Connorc167e542015-09-29 09:40:46 -0400149e820_prepboot(void)
Kevin O'Connor59a23bb2008-06-08 23:09:42 -0400150{
151 dump_map();
Kevin O'Connor2929c352009-07-25 13:48:27 -0400152}