blob: 74b3d2ba84e90a39bb29f166278ce47bcf56437c [file] [log] [blame]
H. Peter Anvin449f2ab2007-07-11 12:18:50 -07001/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
H. Peter Anvinc549e712009-03-28 13:53:26 -07005 * Copyright 2009 Intel Corporation; author H. Peter Anvin
H. Peter Anvin449f2ab2007-07-11 12:18:50 -07006 *
7 * This file is part of the Linux kernel, and is made available under
8 * the terms of the GNU General Public License version 2.
9 *
10 * ----------------------------------------------------------------------- */
11
12/*
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070013 * Memory detection code
14 */
15
16#include "boot.h"
17
18#define SMAP 0x534d4150 /* ASCII "SMAP" */
19
20static int detect_memory_e820(void)
21{
H. Peter Anvin2efa33f2007-09-26 14:11:43 -070022 int count = 0;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070023 u32 next = 0;
H. Peter Anvin32ec7fd2009-03-28 13:53:26 -070024 u32 size, id, edi;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070025 u8 err;
26 struct e820entry *desc = boot_params.e820_map;
H. Peter Anvinbca23db2009-05-21 11:46:16 -070027 static struct e820entry buf; /* static so it is zeroed */
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070028
H. Peter Anvincd670592009-04-01 11:35:00 -070029 /*
H. Peter Anvinbca23db2009-05-21 11:46:16 -070030 * Note: at least one BIOS is known which assumes that the
31 * buffer pointed to by one e820 call is the same one as
32 * the previous call, and only changes modified fields. Therefore,
33 * we use a temporary buffer and copy the results entry by entry.
34 *
35 * This routine deliberately does not try to account for
36 * ACPI 3+ extended attributes. This is because there are
37 * BIOSes in the field which report zero for the valid bit for
38 * all ranges, and we don't currently make any use of the
39 * other attribute bits. Revisit this if we see the extended
40 * attribute bits deployed in a meaningful way in the future.
H. Peter Anvincd670592009-04-01 11:35:00 -070041 */
H. Peter Anvincd670592009-04-01 11:35:00 -070042
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070043 do {
H. Peter Anvinc549e712009-03-28 13:53:26 -070044 size = sizeof buf;
H. Peter Anvin4ee5b102007-09-27 17:17:12 -070045
Michael K. Johnson01522df2009-03-27 13:14:41 -040046 /* Important: %edx and %esi are clobbered by some BIOSes,
47 so they must be either used for the error output
H. Peter Anvin32ec7fd2009-03-28 13:53:26 -070048 or explicitly marked clobbered. Given that, assume there
49 is something out there clobbering %ebp and %edi, too. */
50 asm("pushl %%ebp; int $0x15; popl %%ebp; setc %0"
H. Peter Anvin4ee5b102007-09-27 17:17:12 -070051 : "=d" (err), "+b" (next), "=a" (id), "+c" (size),
H. Peter Anvinc549e712009-03-28 13:53:26 -070052 "=D" (edi), "+m" (buf)
53 : "D" (&buf), "d" (SMAP), "a" (0xe820)
Michael K. Johnson01522df2009-03-27 13:14:41 -040054 : "esi");
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070055
H. Peter Anvin829157b2008-02-13 11:16:46 -080056 /* BIOSes which terminate the chain with CF = 1 as opposed
57 to %ebx = 0 don't always report the SMAP signature on
58 the final, failing, probe. */
59 if (err)
60 break;
61
H. Peter Anvin2efa33f2007-09-26 14:11:43 -070062 /* Some BIOSes stop returning SMAP in the middle of
63 the search loop. We don't know exactly how the BIOS
64 screwed up the map at that point, we might have a
65 partial map, the full map, or complete garbage, so
66 just return failure. */
67 if (id != SMAP) {
68 count = 0;
69 break;
70 }
71
H. Peter Anvinbca23db2009-05-21 11:46:16 -070072 *desc++ = buf;
H. Peter Anvin2efa33f2007-09-26 14:11:43 -070073 count++;
Paul Jacksonc3965bd2008-05-14 08:15:34 -070074 } while (next && count < ARRAY_SIZE(boot_params.e820_map));
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070075
H. Peter Anvin2efa33f2007-09-26 14:11:43 -070076 return boot_params.e820_entries = count;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070077}
78
79static int detect_memory_e801(void)
80{
81 u16 ax, bx, cx, dx;
82 u8 err;
83
84 bx = cx = dx = 0;
85 ax = 0xe801;
86 asm("stc; int $0x15; setc %0"
87 : "=m" (err), "+a" (ax), "+b" (bx), "+c" (cx), "+d" (dx));
88
89 if (err)
90 return -1;
91
92 /* Do we really need to do this? */
93 if (cx || dx) {
94 ax = cx;
95 bx = dx;
96 }
97
98 if (ax > 15*1024)
99 return -1; /* Bogus! */
100
101 /* This ignores memory above 16MB if we have a memory hole
102 there. If someone actually finds a machine with a memory
103 hole at 16MB and no support for 0E820h they should probably
104 generate a fake e820 map. */
105 boot_params.alt_mem_k = (ax == 15*1024) ? (dx << 6)+ax : ax;
106
107 return 0;
108}
109
110static int detect_memory_88(void)
111{
112 u16 ax;
113 u8 err;
114
115 ax = 0x8800;
116 asm("stc; int $0x15; setc %0" : "=bcdm" (err), "+a" (ax));
117
118 boot_params.screen_info.ext_mem_k = ax;
119
120 return -err;
121}
122
123int detect_memory(void)
124{
H. Peter Anvin2efa33f2007-09-26 14:11:43 -0700125 int err = -1;
126
H. Peter Anvin449f2ab2007-07-11 12:18:50 -0700127 if (detect_memory_e820() > 0)
H. Peter Anvin2efa33f2007-09-26 14:11:43 -0700128 err = 0;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -0700129
130 if (!detect_memory_e801())
H. Peter Anvin2efa33f2007-09-26 14:11:43 -0700131 err = 0;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -0700132
H. Peter Anvin2efa33f2007-09-26 14:11:43 -0700133 if (!detect_memory_88())
134 err = 0;
135
136 return err;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -0700137}