2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <gpxe/memmap.h>
33 /** Magic value for INT 15,e820 calls */
34 #define SMAP ( 0x534d4150 )
36 /** An INT 15,e820 memory map entry */
38 /** Start of region */
40 /** Length of region */
44 } __attribute__ (( packed ));
46 #define E820_TYPE_RAM 1 /**< Normal memory */
47 #define E820_TYPE_RESERVED 2 /**< Reserved and unavailable */
48 #define E820_TYPE_ACPI 3 /**< ACPI reclaim memory */
49 #define E820_TYPE_NVS 4 /**< ACPI NVS memory */
51 /** Buffer for INT 15,e820 calls */
52 static struct e820_entry __data16 ( e820buf );
53 #define e820buf __use_data16 ( e820buf )
56 * Get size of extended memory via INT 15,e801
58 * @ret extmem Extended memory size, in kB, or 0
60 static unsigned int extmemsize_e801 ( void ) {
61 uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
62 uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
66 __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
71 "=a" ( extmem_1m_to_16m_k ),
72 "=b" ( extmem_16m_plus_64k ),
73 "=c" ( confmem_1m_to_16m_k ),
74 "=d" ( confmem_16m_plus_64k )
80 if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
81 extmem_1m_to_16m_k = confmem_1m_to_16m_k;
82 extmem_16m_plus_64k = confmem_16m_plus_64k;
85 extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
86 DBG ( "Extended memory size %d+64*%d=%d kB\n",
87 extmem_1m_to_16m_k, extmem_16m_plus_64k, extmem );
92 * Get size of extended memory via INT 15,88
94 * @ret extmem Extended memory size, in kB
96 static unsigned int extmemsize_88 ( void ) {
99 /* Ignore CF; it is not reliable for this call */
100 __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
101 : "=a" ( extmem ) : "a" ( 0x8800 ) );
103 DBG ( "Extended memory size %d kB\n", extmem );
108 * Get size of extended memory
110 * @ret extmem Extended memory size, in kB
112 * Note that this is only an approximation; for an accurate picture,
113 * use the E820 memory map obtained via get_memmap();
115 unsigned int extmemsize ( void ) {
118 /* Try INT 15,e801 first, then fall back to INT 15,88 */
119 extmem = extmemsize_e801();
121 extmem = extmemsize_88();
126 * Get e820 memory map
128 * @v memmap Memory map to fill in
129 * @ret rc Return status code
131 static int meme820 ( struct memory_map *memmap ) {
132 struct memory_region *region = memmap->regions;
136 unsigned int discard_c, discard_d, discard_D;
139 __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
143 : "=r" ( flags ), "=a" ( smap ),
144 "=b" ( next ), "=D" ( discard_D ),
145 "=c" ( discard_c ), "=d" ( discard_d )
146 : "a" ( 0xe820 ), "b" ( next ),
147 "D" ( &__from_data16 ( e820buf ) ),
148 "c" ( sizeof ( e820buf ) ),
158 DBG ( "E820 region [%llx,%llx) type %d\n", e820buf.start,
159 ( e820buf.start + e820buf.len ), ( int ) e820buf.type );
160 if ( e820buf.type != E820_TYPE_RAM )
163 region->start = e820buf.start;
164 region->end = e820buf.start + e820buf.len;
167 } while ( ( next != 0 ) &&
168 ( memmap->count < ( sizeof ( memmap->regions ) /
169 sizeof ( memmap->regions[0] ) ) ) );
176 * @v memmap Memory map to fill in
178 void get_memmap ( struct memory_map *memmap ) {
179 unsigned int basemem, extmem;
182 /* Clear memory map */
183 memset ( memmap, 0, sizeof ( *memmap ) );
185 /* Get base and extended memory sizes */
186 basemem = basememsize();
187 extmem = extmemsize();
189 /* Try INT 15,e820 first */
190 if ( ( rc = meme820 ( memmap ) ) == 0 )
193 /* Fall back to constructing a map from basemem and extmem sizes */
194 memmap->regions[0].end = ( basemem * 1024 );
195 memmap->regions[1].start = 0x100000;
196 memmap->regions[1].end = 0x100000 + ( extmem * 1024 );