summaryrefslogtreecommitdiffstats
path: root/src/arch/i386/firmware/pcbios/memmap.c
blob: daa1d99ccf82fa629bbc86f2bcc3734fc5d4bb25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdint.h>
#include <errno.h>
#include <realmode.h>
#include <bios.h>
#include <memmap.h>

/**
 * @file
 *
 * Memory mapping
 *
 */

/** Magic value for INT 15,e820 calls */
#define SMAP ( 0x534d4150 )

/** An INT 15,e820 memory map entry */
struct e820_entry {
	/** Start of region */
	uint64_t start;
	/** Length of region */
	uint64_t len;
	/** Type of region */
	uint32_t type;
} __attribute__ (( packed ));

#define E820_TYPE_RAM		1 /**< Normal memory */
#define E820_TYPE_RESERVED	2 /**< Reserved and unavailable */
#define E820_TYPE_ACPI		3 /**< ACPI reclaim memory */
#define E820_TYPE_NVS		4 /**< ACPI NVS memory */

/** Buffer for INT 15,e820 calls */
static struct e820_entry __data16 ( e820buf );
#define e820buf __use_data16 ( e820buf )

/**
 * Get size of base memory from BIOS free base memory counter
 *
 * @ret basemem		Base memory size, in kB
 */
static unsigned int basememsize ( void ) {
	uint16_t basemem;

	get_real ( basemem, BDA_SEG, 0x0013 );
	DBG ( "Base memory size %dkB\n", basemem );
	return basemem;
}

/**
 * Get size of extended memory via INT 15,e801
 *
 * @ret extmem		Extended memory size, in kB, or 0
 */
static unsigned int extmemsize_e801 ( void ) {
	uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
	uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
	unsigned int flags;
	unsigned int extmem;

	__asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
					   "int $0x15\n\t"
					   "pushfw\n\t"
					   "popw %w0\n\t" )
			       : "=r" ( flags ),
				 "=a" ( extmem_1m_to_16m_k ),
				 "=b" ( extmem_16m_plus_64k ),
				 "=c" ( confmem_1m_to_16m_k ),
				 "=d" ( confmem_16m_plus_64k )
			       : "a" ( 0xe801 ) );

	if ( flags & CF )
		return 0;

	if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
		extmem_1m_to_16m_k = confmem_1m_to_16m_k;
		extmem_16m_plus_64k = confmem_16m_plus_64k;
	}

	extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
	DBG ( "Extended memory size %d+64*%d=%d kB\n",
	      extmem_1m_to_16m_k, extmem_16m_plus_64k, extmem );
	return extmem;
}

/**
 * Get size of extended memory via INT 15,88
 *
 * @ret extmem		Extended memory size, in kB
 */
static unsigned int extmemsize_88 ( void ) {
	uint16_t extmem;

	/* Ignore CF; it is not reliable for this call */
	__asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
			       : "=a" ( extmem ) : "a" ( 0x8800 ) );

	DBG ( "Extended memory size %d kB\n", extmem );
	return extmem;
}

/**
 * Get size of extended memory
 *
 * @ret extmem		Extended memory size, in kB
 */
static unsigned int extmemsize ( void ) {
	unsigned int extmem;

	/* Try INT 15,e801 first, then fall back to INT 15,88 */
	extmem = extmemsize_e801();
	if ( ! extmem )
		extmem = extmemsize_88();
	return extmem;
}

/**
 * Get e820 memory map
 *
 * @v memmap		Memory map to fill in
 * @ret rc		Return status code
 */
static int meme820 ( struct memory_map *memmap ) {
	struct memory_region *region = memmap->regions;
	uint32_t next = 0;
	uint32_t smap;
	unsigned int flags;
	unsigned int discard_c, discard_d, discard_D;

	do {
		__asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
						   "int $0x15\n\t"
						   "pushfw\n\t"
						   "popw %w0\n\t" )
				       : "=r" ( flags ), "=a" ( smap ),
					 "=b" ( next ), "=D" ( discard_D ),
					 "=c" ( discard_c ), "=d" ( discard_d )
				       : "a" ( 0xe820 ), "b" ( next ),
					 "D" ( &__from_data16 ( e820buf ) ),
					 "c" ( sizeof ( e820buf ) ),
					 "d" ( SMAP )
				       : "memory" );

		if ( smap != SMAP )
			return -ENOTSUP;

		if ( flags & CF )
			break;

		DBG ( "E820 region [%llx,%llx) type %d\n", e820buf.start,
		      ( e820buf.start + e820buf.len ), ( int ) e820buf.type );
		if ( e820buf.type != E820_TYPE_RAM )
			continue;

		region->start = e820buf.start;
		region->end = e820buf.start + e820buf.len;
		region++;
		memmap->count++;
	} while ( ( next != 0 ) && 
		  ( memmap->count < ( sizeof ( memmap->regions ) /
				      sizeof ( memmap->regions[0] ) ) ) );
	return 0;
}

/**
 * Get memory map
 *
 * @v memmap		Memory map to fill in
 */
void get_memmap ( struct memory_map *memmap ) {
	unsigned int basemem, extmem;
	int rc;

	/* Clear memory map */
	memset ( memmap, 0, sizeof ( *memmap ) );

	/* Get base and extended memory sizes */
	basemem = basememsize();
	extmem = extmemsize();
	
	/* Try INT 15,e820 first */
	if ( ( rc = meme820 ( memmap ) ) == 0 )
		return;

	/* Fall back to constructing a map from basemem and extmem sizes */
	memmap->regions[0].end = ( basemem * 1024 );
	memmap->regions[1].start = 0x100000;
	memmap->regions[1].end = 0x100000 + ( extmem * 1024 );
	memmap->count = 2;
}