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