Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / memmap.c
1 // Support for building memory maps suitable for int 15 e820 calls.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "config.h" // BUILD_MAX_E820
8 #include "memmap.h" // struct e820entry
9 #include "output.h" // dprintf
10 #include "string.h" // memmove
11
12
13 /****************************************************************
14  * e820 memory map
15  ****************************************************************/
16
17 // Info on e820 map location and size.
18 struct e820entry e820_list[BUILD_MAX_E820] VARFSEG;
19 int e820_count VARFSEG;
20
21 // Remove an entry from the e820_list.
22 static void
23 remove_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.
31 static void
32 insert_e820(int i, u64 start, u64 size, u32 type)
33 {
34     if (e820_count >= BUILD_MAX_E820) {
35         warn_noalloc();
36         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
48 static const char *
49 e820_type_name(u32 type)
50 {
51     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";
57     case E820_HOLE:     return "HOLE";
58     default:            return "UNKNOWN";
59     }
60 }
61
62 // Show the current e820_list.
63 static void
64 dump_map(void)
65 {
66     dprintf(1, "e820 map has %d items:\n", e820_count);
67     int i;
68     for (i=0; i<e820_count; i++) {
69         struct e820entry *e = &e820_list[i];
70         u64 e_end = e->start + e->size;
71         dprintf(1, "  %d: %016llx - %016llx = %d %s\n", i
72                 , e->start, e_end, e->type, e820_type_name(e->type));
73     }
74 }
75
76 // Add a new entry to the list.  This scans for overlaps and keeps the
77 // list sorted.
78 void
79 add_e820(u64 start, u64 size, u32 type)
80 {
81     dprintf(8, "Add to e820 map: %08x %08x %d\n", (u32)start, (u32)size, type);
82
83     if (! size)
84         // Huh?  Nothing to do.
85         return;
86
87     // Find position of new item (splitting existing item if needed).
88     u64 end = start + size;
89     int i;
90     for (i=0; i<e820_count; i++) {
91         struct e820entry *e = &e820_list[i];
92         u64 e_end = e->start + e->size;
93         if (start > e_end)
94             continue;
95         // Found position - check if an existing item needs to be split.
96         if (start > e->start) {
97             if (type == e->type) {
98                 // Same type - merge them.
99                 size += start - e->start;
100                 start = e->start;
101             } else {
102                 // Split existing item.
103                 e->size = start - e->start;
104                 i++;
105                 if (e_end > end)
106                     insert_e820(i, end, e_end - end, e->type);
107             }
108         }
109         break;
110     }
111     // Remove/adjust existing items that are overlapping.
112     while (i<e820_count) {
113         struct e820entry *e = &e820_list[i];
114         if (end < e->start)
115             // No overlap - done.
116             break;
117         u64 e_end = e->start + e->size;
118         if (end >= e_end) {
119             // Existing item completely overlapped - remove it.
120             remove_e820(i);
121             continue;
122         }
123         // Not completely overlapped - adjust its start.
124         e->start = end;
125         e->size = e_end - end;
126         if (type == e->type) {
127             // Same type - merge them.
128             size += e->size;
129             remove_e820(i);
130         }
131         break;
132     }
133     // Insert new item.
134     if (type != E820_HOLE)
135         insert_e820(i, start, size, type);
136     //dump_map();
137 }
138
139 // Report on final memory locations.
140 void
141 memmap_prepboot(void)
142 {
143     dump_map();
144 }