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 | |
| 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 | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 23 | struct biosregs ireg, oreg; |
Ingo Molnar | 7410aa1 | 2017-01-29 12:56:13 +0100 | [diff] [blame] | 24 | struct boot_e820_entry *desc = boot_params.e820_table; |
| 25 | static struct boot_e820_entry buf; /* static so it is zeroed */ |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 26 | |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 27 | initregs(&ireg); |
| 28 | ireg.ax = 0xe820; |
| 29 | ireg.cx = sizeof buf; |
| 30 | ireg.edx = SMAP; |
| 31 | ireg.di = (size_t)&buf; |
| 32 | |
H. Peter Anvin | cd67059 | 2009-04-01 11:35:00 -0700 | [diff] [blame] | 33 | /* |
H. Peter Anvin | bca23db | 2009-05-21 11:46:16 -0700 | [diff] [blame] | 34 | * Note: at least one BIOS is known which assumes that the |
| 35 | * buffer pointed to by one e820 call is the same one as |
| 36 | * the previous call, and only changes modified fields. Therefore, |
| 37 | * we use a temporary buffer and copy the results entry by entry. |
| 38 | * |
| 39 | * This routine deliberately does not try to account for |
| 40 | * ACPI 3+ extended attributes. This is because there are |
| 41 | * BIOSes in the field which report zero for the valid bit for |
| 42 | * all ranges, and we don't currently make any use of the |
| 43 | * other attribute bits. Revisit this if we see the extended |
| 44 | * attribute bits deployed in a meaningful way in the future. |
H. Peter Anvin | cd67059 | 2009-04-01 11:35:00 -0700 | [diff] [blame] | 45 | */ |
H. Peter Anvin | cd67059 | 2009-04-01 11:35:00 -0700 | [diff] [blame] | 46 | |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 47 | do { |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 48 | intcall(0x15, &ireg, &oreg); |
| 49 | ireg.ebx = oreg.ebx; /* for next iteration... */ |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 50 | |
H. Peter Anvin | 829157b | 2008-02-13 11:16:46 -0800 | [diff] [blame] | 51 | /* BIOSes which terminate the chain with CF = 1 as opposed |
| 52 | to %ebx = 0 don't always report the SMAP signature on |
| 53 | the final, failing, probe. */ |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 54 | if (oreg.eflags & X86_EFLAGS_CF) |
H. Peter Anvin | 829157b | 2008-02-13 11:16:46 -0800 | [diff] [blame] | 55 | break; |
| 56 | |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 57 | /* Some BIOSes stop returning SMAP in the middle of |
| 58 | the search loop. We don't know exactly how the BIOS |
| 59 | screwed up the map at that point, we might have a |
| 60 | partial map, the full map, or complete garbage, so |
| 61 | just return failure. */ |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 62 | if (oreg.eax != SMAP) { |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 63 | count = 0; |
| 64 | break; |
| 65 | } |
| 66 | |
H. Peter Anvin | bca23db | 2009-05-21 11:46:16 -0700 | [diff] [blame] | 67 | *desc++ = buf; |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 68 | count++; |
Ingo Molnar | 61a5010 | 2017-01-27 13:54:38 +0100 | [diff] [blame] | 69 | } while (ireg.ebx && count < ARRAY_SIZE(boot_params.e820_table)); |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 70 | |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 71 | return boot_params.e820_entries = count; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static int detect_memory_e801(void) |
| 75 | { |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 76 | struct biosregs ireg, oreg; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 77 | |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 78 | initregs(&ireg); |
| 79 | ireg.ax = 0xe801; |
| 80 | intcall(0x15, &ireg, &oreg); |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 81 | |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 82 | if (oreg.eflags & X86_EFLAGS_CF) |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 83 | return -1; |
| 84 | |
| 85 | /* Do we really need to do this? */ |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 86 | if (oreg.cx || oreg.dx) { |
| 87 | oreg.ax = oreg.cx; |
| 88 | oreg.bx = oreg.dx; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 89 | } |
| 90 | |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 91 | if (oreg.ax > 15*1024) { |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 92 | return -1; /* Bogus! */ |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 93 | } else if (oreg.ax == 15*1024) { |
H. Peter Anvin | 39b6897 | 2011-04-25 14:52:37 -0700 | [diff] [blame] | 94 | boot_params.alt_mem_k = (oreg.bx << 6) + oreg.ax; |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 95 | } else { |
| 96 | /* |
| 97 | * This ignores memory above 16MB if we have a memory |
| 98 | * hole there. If someone actually finds a machine |
| 99 | * with a memory hole at 16MB and no support for |
| 100 | * 0E820h they should probably generate a fake e820 |
| 101 | * map. |
| 102 | */ |
| 103 | boot_params.alt_mem_k = oreg.ax; |
| 104 | } |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int detect_memory_88(void) |
| 110 | { |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 111 | struct biosregs ireg, oreg; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 112 | |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 113 | initregs(&ireg); |
| 114 | ireg.ah = 0x88; |
| 115 | intcall(0x15, &ireg, &oreg); |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 116 | |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 117 | boot_params.screen_info.ext_mem_k = oreg.ax; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 118 | |
H. Peter Anvin | df7699c | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 119 | return -(oreg.eflags & X86_EFLAGS_CF); /* 0 or -1 */ |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | int detect_memory(void) |
| 123 | { |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 124 | int err = -1; |
| 125 | |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 126 | if (detect_memory_e820() > 0) |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 127 | err = 0; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 128 | |
| 129 | if (!detect_memory_e801()) |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 130 | err = 0; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 131 | |
H. Peter Anvin | 2efa33f | 2007-09-26 14:11:43 -0700 | [diff] [blame] | 132 | if (!detect_memory_88()) |
| 133 | err = 0; |
| 134 | |
| 135 | return err; |
H. Peter Anvin | 449f2ab | 2007-07-11 12:18:50 -0700 | [diff] [blame] | 136 | } |