[x86 setup] Handle case of improperly terminated E820 chain
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / i386 / 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  * arch/i386/boot/memory.c
13  *
14  * Memory detection code
15  */
16
17 #include "boot.h"
18
19 #define SMAP    0x534d4150      /* ASCII "SMAP" */
20
21 static int detect_memory_e820(void)
22 {
23         int count = 0;
24         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
37                 /* 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)
48                         break;
49
50                 count++;
51                 desc++;
52         } while (next && count < E820MAX);
53
54         return boot_params.e820_entries = count;
55 }
56
57 static 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
88 static 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
101 int detect_memory(void)
102 {
103         int err = -1;
104
105         if (detect_memory_e820() > 0)
106                 err = 0;
107
108         if (!detect_memory_e801())
109                 err = 0;
110
111         if (!detect_memory_88())
112                 err = 0;
113
114         return err;
115 }