2 * Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
29 #include <ipxe/init.h>
30 #include <ipxe/settings.h>
37 * Memory map settings are numerically encoded as:
39 * Bits 31-24 Number of regions, minus one
40 * Bits 23-16 Starting region
42 * Bit 10 Ignore non-existent regions (rather than generating an error)
43 * Bit 9 Include length
44 * Bit 8 Include start address
46 * Bits 5-0 Scale factor (i.e. right shift count)
50 * Construct memory map setting tag
52 * @v start Starting region
53 * @v count Number of regions
54 * @v include_start Include start address
55 * @v include_length Include length
56 * @v ignore Ignore non-existent regions
57 * @v scale Scale factor
58 * @ret tag Setting tag
60 #define MEMMAP_TAG( start, count, include_start, include_length, \
62 ( ( (start) << 16 ) | ( ( (count) - 1 ) << 24 ) | \
63 ( (ignore) << 10 ) | ( (include_length) << 9 ) | \
64 ( (include_start) << 8 ) | (scale) )
67 * Extract number of regions from setting tag
70 * @ret count Number of regions
72 #define MEMMAP_COUNT( tag ) ( ( ( (tag) >> 24 ) & 0xff ) + 1 )
75 * Extract starting region from setting tag
78 * @ret start Starting region
80 #define MEMMAP_START( tag ) ( ( (tag) >> 16 ) & 0xff )
83 * Extract ignore flag from setting tag
86 * @ret ignore Ignore non-existent regions
88 #define MEMMAP_IGNORE_NONEXISTENT( tag ) ( (tag) & 0x00000400UL )
91 * Extract length inclusion flag from setting tag
94 * @ret include_length Include length
96 #define MEMMAP_INCLUDE_LENGTH( tag ) ( (tag) & 0x00000200UL )
99 * Extract start address inclusion flag from setting tag
102 * @ret include_start Include start address
104 #define MEMMAP_INCLUDE_START( tag ) ( (tag) & 0x00000100UL )
107 * Extract scale factor from setting tag
110 * @v scale Scale factor
112 #define MEMMAP_SCALE( tag ) ( (tag) & 0x3f )
114 /** Memory map settings scope */
115 static const struct settings_scope memmap_settings_scope
;
118 * Check applicability of memory map setting
120 * @v settings Settings block
122 * @ret applies Setting applies within this settings block
124 static int memmap_settings_applies ( struct settings
*settings __unused
,
125 const struct setting
*setting
) {
127 return ( setting
->scope
== &memmap_settings_scope
);
131 * Fetch value of memory map setting
133 * @v settings Settings block
134 * @v setting Setting to fetch
135 * @v data Buffer to fill with setting data
136 * @v len Length of buffer
137 * @ret len Length of setting data, or negative error
139 static int memmap_settings_fetch ( struct settings
*settings
,
140 struct setting
*setting
,
141 void *data
, size_t len
) {
142 struct memory_map memmap
;
143 struct memory_region
*region
;
148 DBGC ( settings
, "MEMMAP start %d count %d %s%s%s%s scale %d\n",
149 MEMMAP_START ( setting
->tag
), MEMMAP_COUNT ( setting
->tag
),
150 ( MEMMAP_INCLUDE_START ( setting
->tag
) ?
"start" : "" ),
151 ( ( MEMMAP_INCLUDE_START ( setting
->tag
) &&
152 MEMMAP_INCLUDE_LENGTH ( setting
->tag
) ) ?
"+" : "" ),
153 ( MEMMAP_INCLUDE_LENGTH ( setting
->tag
) ?
"length" : "" ),
154 ( MEMMAP_IGNORE_NONEXISTENT ( setting
->tag
) ?
" ignore" : "" ),
155 MEMMAP_SCALE ( setting
->tag
) );
157 /* Fetch memory map */
158 get_memmap ( &memmap
);
160 /* Extract results from memory map */
161 count
= MEMMAP_COUNT ( setting
->tag
);
162 for ( i
= MEMMAP_START ( setting
->tag
) ; count
-- ; i
++ ) {
164 /* Check that region exists */
165 if ( i
>= memmap
.count
) {
166 if ( MEMMAP_IGNORE_NONEXISTENT ( setting
->tag
) ) {
169 DBGC ( settings
, "MEMMAP region %d does not "
175 /* Extract results from this region */
176 region
= &memmap
.regions
[i
];
177 if ( MEMMAP_INCLUDE_START ( setting
->tag
) ) {
178 result
+= region
->start
;
179 DBGC ( settings
, "MEMMAP %d start %08llx\n",
182 if ( MEMMAP_INCLUDE_LENGTH ( setting
->tag
) ) {
183 result
+= ( region
->end
- region
->start
);
184 DBGC ( settings
, "MEMMAP %d length %08llx\n",
185 i
, ( region
->end
- region
->start
) );
190 result
>>= MEMMAP_SCALE ( setting
->tag
);
193 result
= cpu_to_be64 ( result
);
194 if ( len
> sizeof ( result
) )
195 len
= sizeof ( result
);
196 memcpy ( data
, &result
, len
);
198 /* Set type if not already specified */
199 if ( ! setting
->type
)
200 setting
->type
= &setting_type_hexraw
;
202 return sizeof ( result
);
205 /** Memory map settings operations */
206 static struct settings_operations memmap_settings_operations
= {
207 .applies
= memmap_settings_applies
,
208 .fetch
= memmap_settings_fetch
,
211 /** Memory map settings */
212 static struct settings memmap_settings
= {
214 .siblings
= LIST_HEAD_INIT ( memmap_settings
.siblings
),
215 .children
= LIST_HEAD_INIT ( memmap_settings
.children
),
216 .op
= &memmap_settings_operations
,
217 .default_scope
= &memmap_settings_scope
,
220 /** Initialise memory map settings */
221 static void memmap_settings_init ( void ) {
224 if ( ( rc
= register_settings ( &memmap_settings
, NULL
,
225 "memmap" ) ) != 0 ) {
226 DBG ( "MEMMAP could not register settings: %s\n",
232 /** Memory map settings initialiser */
233 struct init_fn memmap_settings_init_fn
__init_fn ( INIT_NORMAL
) = {
234 .initialise
= memmap_settings_init
,
237 /** Memory map predefined settings */
238 const struct setting memsize_setting
__setting ( SETTING_MISC
, memsize
) = {
240 .description
= "Memory size (in MB)",
241 .tag
= MEMMAP_TAG ( 0, 0x100, 0, 1, 1, 20 ),
242 .type
= &setting_type_int32
,
243 .scope
= &memmap_settings_scope
,