1 /* ----------------------------------------------------------------------- *
3 * Copyright 2001-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
16 * E820 range database manager
23 #include "memdisk.h" /* For memset() */
27 #define MAXRANGES 1024
28 /* All of memory starts out as one range of "indeterminate" type */
29 struct e820range ranges[MAXRANGES];
32 void e820map_init(void)
34 memset(ranges, 0, sizeof(ranges));
39 static void insertrange_at(int where, uint64_t start, uint32_t type)
43 for (i = nranges; i > where; i--)
44 ranges[i] = ranges[i - 1];
46 ranges[where].start = start;
47 ranges[where].type = type;
50 ranges[nranges].start = 0ULL;
51 ranges[nranges].type = -1U;
54 void insertrange(uint64_t start, uint64_t len, uint32_t type)
60 /* Remove this to make len == 0 mean all of memory */
62 return; /* Nothing to insert */
64 last = start + len - 1; /* May roll over */
68 while (start > ranges[i].start && ranges[i].type != -1U) {
69 oldtype = ranges[i].type;
73 /* Consider the replacement policy. This current one is "overwrite." */
75 if (start < ranges[i].start || ranges[i].type == -1U)
76 insertrange_at(i++, start, type);
78 while (i == 0 || last > ranges[i].start - 1) {
79 oldtype = ranges[i].type;
80 ranges[i].type = type;
84 if (last < ranges[i].start - 1)
85 insertrange_at(i, last + 1, oldtype);
87 /* Now the map is correct, but quite possibly not optimal. Scan the
88 map for ranges which are redundant and remove them. */
90 oldtype = ranges[0].type;
92 if (ranges[i].type == oldtype) {
95 oldtype = ranges[i].type;
97 ranges[j] = ranges[i];
104 ranges[j] = ranges[i]; /* Termination sentinel copy */