fcdb10add9c8219f39df24b38bfee2e2e46d2792
[pandora-kernel.git] / arch / x86 / boot / memory.c
1 /* -*- 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  * Memory detection code
13  */
14
15 #include "boot.h"
16
17 #define SMAP    0x534d4150      /* ASCII "SMAP" */
18
19 static int detect_memory_e820(void)
20 {
21         int count = 0;
22         u32 next = 0;
23         u32 size, id, edi;
24         u8 err;
25         struct e820entry *desc = boot_params.e820_map;
26
27         do {
28                 size = sizeof(struct e820entry);
29
30                 /* Important: %edx and %esi are clobbered by some BIOSes,
31                    so they must be either used for the error output
32                    or explicitly marked clobbered.  Given that, assume there
33                    is something out there clobbering %ebp and %edi, too. */
34                 asm("pushl %%ebp; int $0x15; popl %%ebp; setc %0"
35                     : "=d" (err), "+b" (next), "=a" (id), "+c" (size),
36                       "=D" (edi), "=m" (*desc)
37                     : "D" (desc), "d" (SMAP), "a" (0xe820)
38                     : "esi");
39
40                 /* BIOSes which terminate the chain with CF = 1 as opposed
41                    to %ebx = 0 don't always report the SMAP signature on
42                    the final, failing, probe. */
43                 if (err)
44                         break;
45
46                 /* Some BIOSes stop returning SMAP in the middle of
47                    the search loop.  We don't know exactly how the BIOS
48                    screwed up the map at that point, we might have a
49                    partial map, the full map, or complete garbage, so
50                    just return failure. */
51                 if (id != SMAP) {
52                         count = 0;
53                         break;
54                 }
55
56                 count++;
57                 desc++;
58         } while (next && count < ARRAY_SIZE(boot_params.e820_map));
59
60         return boot_params.e820_entries = count;
61 }
62
63 static int detect_memory_e801(void)
64 {
65         u16 ax, bx, cx, dx;
66         u8 err;
67
68         bx = cx = dx = 0;
69         ax = 0xe801;
70         asm("stc; int $0x15; setc %0"
71             : "=m" (err), "+a" (ax), "+b" (bx), "+c" (cx), "+d" (dx));
72
73         if (err)
74                 return -1;
75
76         /* Do we really need to do this? */
77         if (cx || dx) {
78                 ax = cx;
79                 bx = dx;
80         }
81
82         if (ax > 15*1024)
83                 return -1;      /* Bogus! */
84
85         /* This ignores memory above 16MB if we have a memory hole
86            there.  If someone actually finds a machine with a memory
87            hole at 16MB and no support for 0E820h they should probably
88            generate a fake e820 map. */
89         boot_params.alt_mem_k = (ax == 15*1024) ? (dx << 6)+ax : ax;
90
91         return 0;
92 }
93
94 static int detect_memory_88(void)
95 {
96         u16 ax;
97         u8 err;
98
99         ax = 0x8800;
100         asm("stc; int $0x15; setc %0" : "=bcdm" (err), "+a" (ax));
101
102         boot_params.screen_info.ext_mem_k = ax;
103
104         return -err;
105 }
106
107 int detect_memory(void)
108 {
109         int err = -1;
110
111         if (detect_memory_e820() > 0)
112                 err = 0;
113
114         if (!detect_memory_e801())
115                 err = 0;
116
117         if (!detect_memory_88())
118                 err = 0;
119
120         return err;
121 }