Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / firmware / pcbios / memmap.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <errno.h>
24 #include <realmode.h>
25 #include <bios.h>
26 #include <memsizes.h>
27 #include <ipxe/io.h>
28
29 /**
30  * @file
31  *
32  * Memory mapping
33  *
34  */
35
36 /** Magic value for INT 15,e820 calls */
37 #define SMAP ( 0x534d4150 )
38
39 /** An INT 15,e820 memory map entry */
40 struct e820_entry {
41         /** Start of region */
42         uint64_t start;
43         /** Length of region */
44         uint64_t len;
45         /** Type of region */
46         uint32_t type;
47         /** Extended attributes (optional) */
48         uint32_t attrs;
49 } __attribute__ (( packed ));
50
51 #define E820_TYPE_RAM           1 /**< Normal memory */
52 #define E820_TYPE_RESERVED      2 /**< Reserved and unavailable */
53 #define E820_TYPE_ACPI          3 /**< ACPI reclaim memory */
54 #define E820_TYPE_NVS           4 /**< ACPI NVS memory */
55
56 #define E820_ATTR_ENABLED       0x00000001UL
57 #define E820_ATTR_NONVOLATILE   0x00000002UL
58 #define E820_ATTR_UNKNOWN       0xfffffffcUL
59
60 #define E820_MIN_SIZE           20
61
62 /** Buffer for INT 15,e820 calls */
63 static struct e820_entry __bss16 ( e820buf );
64 #define e820buf __use_data16 ( e820buf )
65
66 /** We are running during POST; inhibit INT 15,e820 and INT 15,e801 */
67 uint8_t __bss16 ( memmap_post );
68 #define memmap_post __use_data16 ( memmap_post )
69
70 /**
71  * Get size of extended memory via INT 15,e801
72  *
73  * @ret extmem          Extended memory size, in kB, or 0
74  */
75 static unsigned int extmemsize_e801 ( void ) {
76         uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
77         uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
78         unsigned int flags;
79         unsigned int extmem;
80
81         /* Inhibit INT 15,e801 during POST */
82         if ( memmap_post ) {
83                 DBG ( "INT 15,e801 not available during POST\n" );
84                 return 0;
85         }
86
87         __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
88                                            "int $0x15\n\t"
89                                            "pushfw\n\t"
90                                            "popw %w0\n\t" )
91                                : "=r" ( flags ),
92                                  "=a" ( extmem_1m_to_16m_k ),
93                                  "=b" ( extmem_16m_plus_64k ),
94                                  "=c" ( confmem_1m_to_16m_k ),
95                                  "=d" ( confmem_16m_plus_64k )
96                                : "a" ( 0xe801 ) );
97
98         if ( flags & CF ) {
99                 DBG ( "INT 15,e801 failed with CF set\n" );
100                 return 0;
101         }
102
103         if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
104                 DBG ( "INT 15,e801 extmem=0, using confmem\n" );
105                 extmem_1m_to_16m_k = confmem_1m_to_16m_k;
106                 extmem_16m_plus_64k = confmem_16m_plus_64k;
107         }
108
109         extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
110         DBG ( "INT 15,e801 extended memory size %d+64*%d=%d kB "
111               "[100000,%llx)\n", extmem_1m_to_16m_k, extmem_16m_plus_64k,
112               extmem, ( 0x100000 + ( ( ( uint64_t ) extmem ) * 1024 ) ) );
113
114         /* Sanity check.  Some BIOSes report the entire 4GB address
115          * space as available, which cannot be correct (since that
116          * would leave no address space available for 32-bit PCI
117          * BARs).
118          */
119         if ( extmem == ( 0x400000 - 0x400 ) ) {
120                 DBG ( "INT 15,e801 reported whole 4GB; assuming insane\n" );
121                 return 0;
122         }
123
124         return extmem;
125 }
126
127 /**
128  * Get size of extended memory via INT 15,88
129  *
130  * @ret extmem          Extended memory size, in kB
131  */
132 static unsigned int extmemsize_88 ( void ) {
133         uint16_t extmem;
134
135         /* Ignore CF; it is not reliable for this call */
136         __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
137                                : "=a" ( extmem ) : "a" ( 0x8800 ) );
138
139         DBG ( "INT 15,88 extended memory size %d kB [100000, %x)\n",
140               extmem, ( 0x100000 + ( extmem * 1024 ) ) );
141         return extmem;
142 }
143
144 /**
145  * Get size of extended memory
146  *
147  * @ret extmem          Extended memory size, in kB
148  *
149  * Note that this is only an approximation; for an accurate picture,
150  * use the E820 memory map obtained via get_memmap();
151  */
152 unsigned int extmemsize ( void ) {
153         unsigned int extmem_e801;
154         unsigned int extmem_88;
155
156         /* Try INT 15,e801 first, then fall back to INT 15,88 */
157         extmem_88 = extmemsize_88();
158         extmem_e801 = extmemsize_e801();
159         return ( extmem_e801 ? extmem_e801 : extmem_88 );
160 }
161
162 /**
163  * Get e820 memory map
164  *
165  * @v memmap            Memory map to fill in
166  * @ret rc              Return status code
167  */
168 static int meme820 ( struct memory_map *memmap ) {
169         struct memory_region *region = memmap->regions;
170         struct memory_region *prev_region = NULL;
171         uint32_t next = 0;
172         uint32_t smap;
173         size_t size;
174         unsigned int flags;
175         unsigned int discard_D;
176
177         /* Inhibit INT 15,e820 during POST */
178         if ( memmap_post ) {
179                 DBG ( "INT 15,e820 not available during POST\n" );
180                 return -ENOTTY;
181         }
182
183         /* Clear the E820 buffer.  Do this once before starting,
184          * rather than on each call; some BIOSes rely on the contents
185          * being preserved between calls.
186          */
187         memset ( &e820buf, 0, sizeof ( e820buf ) );
188
189         do {
190                 /* Some BIOSes corrupt %esi for fun. Guard against
191                  * this by telling gcc that all non-output registers
192                  * may be corrupted.
193                  */
194                 __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t"
195                                                    "stc\n\t"
196                                                    "int $0x15\n\t"
197                                                    "pushfw\n\t"
198                                                    "popw %%dx\n\t"
199                                                    "popl %%ebp\n\t" )
200                                        : "=a" ( smap ), "=b" ( next ),
201                                          "=c" ( size ), "=d" ( flags ),
202                                          "=D" ( discard_D )
203                                        : "a" ( 0xe820 ), "b" ( next ),
204                                          "D" ( __from_data16 ( &e820buf ) ),
205                                          "c" ( sizeof ( e820buf ) ),
206                                          "d" ( SMAP )
207                                        : "esi", "memory" );
208
209                 if ( smap != SMAP ) {
210                         DBG ( "INT 15,e820 failed SMAP signature check\n" );
211                         return -ENOTSUP;
212                 }
213
214                 if ( size < E820_MIN_SIZE ) {
215                         DBG ( "INT 15,e820 returned only %zd bytes\n", size );
216                         return -EINVAL;
217                 }
218
219                 if ( flags & CF ) {
220                         DBG ( "INT 15,e820 terminated on CF set\n" );
221                         break;
222                 }
223
224                 /* If first region is not RAM, assume map is invalid */
225                 if ( ( memmap->count == 0 ) &&
226                      ( e820buf.type != E820_TYPE_RAM ) ) {
227                        DBG ( "INT 15,e820 failed, first entry not RAM\n" );
228                        return -EINVAL;
229                 }
230
231                 DBG ( "INT 15,e820 region [%llx,%llx) type %d",
232                       e820buf.start, ( e820buf.start + e820buf.len ),
233                       ( int ) e820buf.type );
234                 if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
235                         DBG ( " (%s", ( ( e820buf.attrs & E820_ATTR_ENABLED )
236                                         ? "enabled" : "disabled" ) );
237                         if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
238                                 DBG ( ", non-volatile" );
239                         if ( e820buf.attrs & E820_ATTR_UNKNOWN )
240                                 DBG ( ", other [%08x]", e820buf.attrs );
241                         DBG ( ")" );
242                 }
243                 DBG ( "\n" );
244
245                 /* Discard non-RAM regions */
246                 if ( e820buf.type != E820_TYPE_RAM )
247                         continue;
248
249                 /* Check extended attributes, if present */
250                 if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
251                         if ( ! ( e820buf.attrs & E820_ATTR_ENABLED ) )
252                                 continue;
253                         if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
254                                 continue;
255                 }
256
257                 region->start = e820buf.start;
258                 region->end = e820buf.start + e820buf.len;
259
260                 /* Check for adjacent regions and merge them */
261                 if ( prev_region && ( region->start == prev_region->end ) ) {
262                         prev_region->end = region->end;
263                 } else {
264                         prev_region = region;
265                         region++;
266                         memmap->count++;
267                 }
268
269                 if ( memmap->count >= ( sizeof ( memmap->regions ) /
270                                         sizeof ( memmap->regions[0] ) ) ) {
271                         DBG ( "INT 15,e820 too many regions returned\n" );
272                         /* Not a fatal error; what we've got so far at
273                          * least represents valid regions of memory,
274                          * even if we couldn't get them all.
275                          */
276                         break;
277                 }
278         } while ( next != 0 );
279
280         /* Sanity checks.  Some BIOSes report complete garbage via INT
281          * 15,e820 (especially at POST time), despite passing the
282          * signature checks.  We currently check for a base memory
283          * region (starting at 0) and at least one high memory region
284          * (starting at 0x100000).
285          */
286         if ( memmap->count < 2 ) {
287                 DBG ( "INT 15,e820 returned only %d regions; assuming "
288                       "insane\n", memmap->count );
289                 return -EINVAL;
290         }
291         if ( memmap->regions[0].start != 0 ) {
292                 DBG ( "INT 15,e820 region 0 starts at %llx (expected 0); "
293                       "assuming insane\n", memmap->regions[0].start );
294                 return -EINVAL;
295         }
296         if ( memmap->regions[1].start != 0x100000 ) {
297                 DBG ( "INT 15,e820 region 1 starts at %llx (expected 100000); "
298                       "assuming insane\n", memmap->regions[0].start );
299                 return -EINVAL;
300         }
301
302         return 0;
303 }
304
305 /**
306  * Get memory map
307  *
308  * @v memmap            Memory map to fill in
309  */
310 void x86_get_memmap ( struct memory_map *memmap ) {
311         unsigned int basemem, extmem;
312         int rc;
313
314         DBG ( "Fetching system memory map\n" );
315
316         /* Clear memory map */
317         memset ( memmap, 0, sizeof ( *memmap ) );
318
319         /* Get base and extended memory sizes */
320         basemem = basememsize();
321         DBG ( "FBMS base memory size %d kB [0,%x)\n",
322               basemem, ( basemem * 1024 ) );
323         extmem = extmemsize();
324         
325         /* Try INT 15,e820 first */
326         if ( ( rc = meme820 ( memmap ) ) == 0 ) {
327                 DBG ( "Obtained system memory map via INT 15,e820\n" );
328                 return;
329         }
330
331         /* Fall back to constructing a map from basemem and extmem sizes */
332         DBG ( "INT 15,e820 failed; constructing map\n" );
333         memmap->regions[0].end = ( basemem * 1024 );
334         memmap->regions[1].start = 0x100000;
335         memmap->regions[1].end = 0x100000 + ( extmem * 1024 );
336         memmap->count = 2;
337 }
338
339 PROVIDE_IOAPI ( x86, get_memmap, x86_get_memmap );