blob: bccaa1cf664508ecc44ca10fa1030ad09fb6ae6b [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
5 *
6 * This file is part of the Linux kernel, and is made available under
7 * the terms of the GNU General Public License version 2.
8 *
9 * ----------------------------------------------------------------------- */
10
11/*
12 * arch/i386/boot/memory.c
13 *
14 * Memory detection code
15 */
16
17#include "boot.h"
18
19#define SMAP 0x534d4150 /* ASCII "SMAP" */
20
21static int detect_memory_e820(void)
22{
H. Peter Anvin2efa33f2007-09-26 14:11:43 -070023 int count = 0;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070024 u32 next = 0;
25 u32 size, id;
26 u8 err;
27 struct e820entry *desc = boot_params.e820_map;
28
29 do {
30 size = sizeof(struct e820entry);
31 id = SMAP;
32 asm("int $0x15; setc %0"
33 : "=am" (err), "+b" (next), "+d" (id), "+c" (size),
34 "=m" (*desc)
35 : "D" (desc), "a" (0xe820));
36
H. Peter Anvin2efa33f2007-09-26 14:11:43 -070037 /* Some BIOSes stop returning SMAP in the middle of
38 the search loop. We don't know exactly how the BIOS
39 screwed up the map at that point, we might have a
40 partial map, the full map, or complete garbage, so
41 just return failure. */
42 if (id != SMAP) {
43 count = 0;
44 break;
45 }
46
47 if (err)
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070048 break;
49
H. Peter Anvin2efa33f2007-09-26 14:11:43 -070050 count++;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070051 desc++;
H. Peter Anvin2efa33f2007-09-26 14:11:43 -070052 } while (next && count < E820MAX);
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070053
H. Peter Anvin2efa33f2007-09-26 14:11:43 -070054 return boot_params.e820_entries = count;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -070055}
56
57static int detect_memory_e801(void)
58{
59 u16 ax, bx, cx, dx;
60 u8 err;
61
62 bx = cx = dx = 0;
63 ax = 0xe801;
64 asm("stc; int $0x15; setc %0"
65 : "=m" (err), "+a" (ax), "+b" (bx), "+c" (cx), "+d" (dx));
66
67 if (err)
68 return -1;
69
70 /* Do we really need to do this? */
71 if (cx || dx) {
72 ax = cx;
73 bx = dx;
74 }
75
76 if (ax > 15*1024)
77 return -1; /* Bogus! */
78
79 /* This ignores memory above 16MB if we have a memory hole
80 there. If someone actually finds a machine with a memory
81 hole at 16MB and no support for 0E820h they should probably
82 generate a fake e820 map. */
83 boot_params.alt_mem_k = (ax == 15*1024) ? (dx << 6)+ax : ax;
84
85 return 0;
86}
87
88static int detect_memory_88(void)
89{
90 u16 ax;
91 u8 err;
92
93 ax = 0x8800;
94 asm("stc; int $0x15; setc %0" : "=bcdm" (err), "+a" (ax));
95
96 boot_params.screen_info.ext_mem_k = ax;
97
98 return -err;
99}
100
101int detect_memory(void)
102{
H. Peter Anvin2efa33f2007-09-26 14:11:43 -0700103 int err = -1;
104
H. Peter Anvin449f2ab2007-07-11 12:18:50 -0700105 if (detect_memory_e820() > 0)
H. Peter Anvin2efa33f2007-09-26 14:11:43 -0700106 err = 0;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -0700107
108 if (!detect_memory_e801())
H. Peter Anvin2efa33f2007-09-26 14:11:43 -0700109 err = 0;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -0700110
H. Peter Anvin2efa33f2007-09-26 14:11:43 -0700111 if (!detect_memory_88())
112 err = 0;
113
114 return err;
H. Peter Anvin449f2ab2007-07-11 12:18:50 -0700115}