H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 1 | /* -*- linux-c -*- ------------------------------------------------------- * |
| 2 | * |
| 3 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 4 | * Copyright 2007 rPath, Inc. - All Rights Reserved |
H. Peter Anvin | c549e71 | 2009-03-28 13:53:26 -0700 | [diff] [blame] | 5 | * Copyright 2009 Intel Corporation; author H. Peter Anvin |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 6 | * |
| 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 Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 13 | * Memory detection code |
| 14 | */ |
| 15 | |
| 16 | #include "boot.h" |
| 17 | |
| 18 | #define SMAP 0x534d4150 /* ASCII "SMAP" */ |
| 19 | |
H. Peter Anvin | c549e71 | 2009-03-28 13:53:26 -0700 | [diff] [blame] | 20 | struct e820_ext_entry { |
| 21 | struct e820entry std; |
| 22 | u32 ext_flags; |
| 23 | } __attribute__((packed)); |
| 24 | |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 25 | static int detect_memory_e820(void) |
| 26 | { |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 27 | int count = 0; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 28 | u32 next = 0; |
H. Peter Anvin | 32ec7fd | 2009-03-28 13:53:26 -0700 | [diff] [blame] | 29 | u32 size, id, edi; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 30 | u8 err; |
| 31 | struct e820entry *desc = boot_params.e820_map; |
H. Peter Anvin | c549e71 | 2009-03-28 13:53:26 -0700 | [diff] [blame] | 32 | static struct e820_ext_entry buf; /* static so it is zeroed */ |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 33 | |
H. Peter Anvin | cd67059 | 2009-04-01 11:35:00 -0700 | [diff] [blame^] | 34 | /* |
| 35 | * Set this here so that if the BIOS doesn't change this field |
| 36 | * but still doesn't change %ecx, we're still okay... |
| 37 | */ |
| 38 | buf.ext_flags = 1; |
| 39 | |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 40 | do { |
H. Peter Anvin | c549e71 | 2009-03-28 13:53:26 -0700 | [diff] [blame] | 41 | size = sizeof buf; |
H. Peter Anvin | 4ee5b10 | 2007-09-27 17:17:12 -0700 | [diff] [blame] | 42 | |
Michael K. Johnson | 01522df | 2009-03-27 13:14:41 -0400 | [diff] [blame] | 43 | /* Important: %edx and %esi are clobbered by some BIOSes, |
| 44 | so they must be either used for the error output |
H. Peter Anvin | 32ec7fd | 2009-03-28 13:53:26 -0700 | [diff] [blame] | 45 | or explicitly marked clobbered. Given that, assume there |
| 46 | is something out there clobbering %ebp and %edi, too. */ |
| 47 | asm("pushl %%ebp; int $0x15; popl %%ebp; setc %0" |
H. Peter Anvin | 4ee5b10 | 2007-09-27 17:17:12 -0700 | [diff] [blame] | 48 | : "=d" (err), "+b" (next), "=a" (id), "+c" (size), |
H. Peter Anvin | c549e71 | 2009-03-28 13:53:26 -0700 | [diff] [blame] | 49 | "=D" (edi), "+m" (buf) |
| 50 | : "D" (&buf), "d" (SMAP), "a" (0xe820) |
Michael K. Johnson | 01522df | 2009-03-27 13:14:41 -0400 | [diff] [blame] | 51 | : "esi"); |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 52 | |
H. Peter Anvin | 829157b | 2008-02-13 11:16:46 -0800 | [diff] [blame] | 53 | /* BIOSes which terminate the chain with CF = 1 as opposed |
| 54 | to %ebx = 0 don't always report the SMAP signature on |
| 55 | the final, failing, probe. */ |
| 56 | if (err) |
| 57 | break; |
| 58 | |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 59 | /* Some BIOSes stop returning SMAP in the middle of |
| 60 | the search loop. We don't know exactly how the BIOS |
| 61 | screwed up the map at that point, we might have a |
| 62 | partial map, the full map, or complete garbage, so |
| 63 | just return failure. */ |
| 64 | if (id != SMAP) { |
| 65 | count = 0; |
| 66 | break; |
| 67 | } |
| 68 | |
H. Peter Anvin | c549e71 | 2009-03-28 13:53:26 -0700 | [diff] [blame] | 69 | /* ACPI 3.0 added the extended flags support. If bit 0 |
| 70 | in the extended flags is zero, we're supposed to simply |
| 71 | ignore the entry -- a backwards incompatible change! */ |
| 72 | if (size > 20 && !(buf.ext_flags & 1)) |
| 73 | continue; |
| 74 | |
| 75 | *desc++ = buf.std; |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 76 | count++; |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame] | 77 | } while (next && count < ARRAY_SIZE(boot_params.e820_map)); |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 78 | |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 79 | return boot_params.e820_entries = count; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int detect_memory_e801(void) |
| 83 | { |
| 84 | u16 ax, bx, cx, dx; |
| 85 | u8 err; |
| 86 | |
| 87 | bx = cx = dx = 0; |
| 88 | ax = 0xe801; |
| 89 | asm("stc; int $0x15; setc %0" |
| 90 | : "=m" (err), "+a" (ax), "+b" (bx), "+c" (cx), "+d" (dx)); |
| 91 | |
| 92 | if (err) |
| 93 | return -1; |
| 94 | |
| 95 | /* Do we really need to do this? */ |
| 96 | if (cx || dx) { |
| 97 | ax = cx; |
| 98 | bx = dx; |
| 99 | } |
| 100 | |
| 101 | if (ax > 15*1024) |
| 102 | return -1; /* Bogus! */ |
| 103 | |
| 104 | /* This ignores memory above 16MB if we have a memory hole |
| 105 | there. If someone actually finds a machine with a memory |
| 106 | hole at 16MB and no support for 0E820h they should probably |
| 107 | generate a fake e820 map. */ |
| 108 | boot_params.alt_mem_k = (ax == 15*1024) ? (dx << 6)+ax : ax; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int detect_memory_88(void) |
| 114 | { |
| 115 | u16 ax; |
| 116 | u8 err; |
| 117 | |
| 118 | ax = 0x8800; |
| 119 | asm("stc; int $0x15; setc %0" : "=bcdm" (err), "+a" (ax)); |
| 120 | |
| 121 | boot_params.screen_info.ext_mem_k = ax; |
| 122 | |
| 123 | return -err; |
| 124 | } |
| 125 | |
| 126 | int detect_memory(void) |
| 127 | { |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 128 | int err = -1; |
| 129 | |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 130 | if (detect_memory_e820() > 0) |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 131 | err = 0; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 132 | |
| 133 | if (!detect_memory_e801()) |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 134 | err = 0; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 135 | |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 136 | if (!detect_memory_88()) |
| 137 | err = 0; |
| 138 | |
| 139 | return err; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 140 | } |