You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memmap_settings.c 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2013 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. * 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.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <byteswap.h>
  27. #include <ipxe/init.h>
  28. #include <ipxe/settings.h>
  29. #include <ipxe/io.h>
  30. /** @file
  31. *
  32. * Memory map settings
  33. *
  34. * Memory map settings are numerically encoded as:
  35. *
  36. * Bits 31-24 Number of regions, minus one
  37. * Bits 23-16 Starting region
  38. * Bits 15-11 Unused
  39. * Bit 10 Ignore non-existent regions (rather than generating an error)
  40. * Bit 9 Include length
  41. * Bit 8 Include start address
  42. * Bits 7-6 Unused
  43. * Bits 5-0 Scale factor (i.e. right shift count)
  44. */
  45. /**
  46. * Construct memory map setting tag
  47. *
  48. * @v start Starting region
  49. * @v count Number of regions
  50. * @v include_start Include start address
  51. * @v include_length Include length
  52. * @v ignore Ignore non-existent regions
  53. * @v scale Scale factor
  54. * @ret tag Setting tag
  55. */
  56. #define MEMMAP_TAG( start, count, include_start, include_length, \
  57. ignore, scale ) \
  58. ( ( (start) << 16 ) | ( ( (count) - 1 ) << 24 ) | \
  59. ( (ignore) << 10 ) | ( (include_length) << 9 ) | \
  60. ( (include_start) << 8 ) | (scale) )
  61. /**
  62. * Extract number of regions from setting tag
  63. *
  64. * @v tag Setting tag
  65. * @ret count Number of regions
  66. */
  67. #define MEMMAP_COUNT( tag ) ( ( ( (tag) >> 24 ) & 0xff ) + 1 )
  68. /**
  69. * Extract starting region from setting tag
  70. *
  71. * @v tag Setting tag
  72. * @ret start Starting region
  73. */
  74. #define MEMMAP_START( tag ) ( ( (tag) >> 16 ) & 0xff )
  75. /**
  76. * Extract ignore flag from setting tag
  77. *
  78. * @v tag Setting tag
  79. * @ret ignore Ignore non-existent regions
  80. */
  81. #define MEMMAP_IGNORE_NONEXISTENT( tag ) ( (tag) & 0x00000400UL )
  82. /**
  83. * Extract length inclusion flag from setting tag
  84. *
  85. * @v tag Setting tag
  86. * @ret include_length Include length
  87. */
  88. #define MEMMAP_INCLUDE_LENGTH( tag ) ( (tag) & 0x00000200UL )
  89. /**
  90. * Extract start address inclusion flag from setting tag
  91. *
  92. * @v tag Setting tag
  93. * @ret include_start Include start address
  94. */
  95. #define MEMMAP_INCLUDE_START( tag ) ( (tag) & 0x00000100UL )
  96. /**
  97. * Extract scale factor from setting tag
  98. *
  99. * @v tag Setting tag
  100. * @v scale Scale factor
  101. */
  102. #define MEMMAP_SCALE( tag ) ( (tag) & 0x3f )
  103. /** Memory map settings scope */
  104. static const struct settings_scope memmap_settings_scope;
  105. /**
  106. * Check applicability of memory map setting
  107. *
  108. * @v settings Settings block
  109. * @v setting Setting
  110. * @ret applies Setting applies within this settings block
  111. */
  112. static int memmap_settings_applies ( struct settings *settings __unused,
  113. const struct setting *setting ) {
  114. return ( setting->scope == &memmap_settings_scope );
  115. }
  116. /**
  117. * Fetch value of memory map setting
  118. *
  119. * @v settings Settings block
  120. * @v setting Setting to fetch
  121. * @v data Buffer to fill with setting data
  122. * @v len Length of buffer
  123. * @ret len Length of setting data, or negative error
  124. */
  125. static int memmap_settings_fetch ( struct settings *settings,
  126. struct setting *setting,
  127. void *data, size_t len ) {
  128. struct memory_map memmap;
  129. struct memory_region *region;
  130. uint64_t result = 0;
  131. unsigned int i;
  132. unsigned int count;
  133. DBGC ( settings, "MEMMAP start %d count %d %s%s%s%s scale %d\n",
  134. MEMMAP_START ( setting->tag ), MEMMAP_COUNT ( setting->tag ),
  135. ( MEMMAP_INCLUDE_START ( setting->tag ) ? "start" : "" ),
  136. ( ( MEMMAP_INCLUDE_START ( setting->tag ) &&
  137. MEMMAP_INCLUDE_LENGTH ( setting->tag ) ) ? "+" : "" ),
  138. ( MEMMAP_INCLUDE_LENGTH ( setting->tag ) ? "length" : "" ),
  139. ( MEMMAP_IGNORE_NONEXISTENT ( setting->tag ) ? " ignore" : "" ),
  140. MEMMAP_SCALE ( setting->tag ) );
  141. /* Fetch memory map */
  142. get_memmap ( &memmap );
  143. /* Extract results from memory map */
  144. count = MEMMAP_COUNT ( setting->tag );
  145. for ( i = MEMMAP_START ( setting->tag ) ; count-- ; i++ ) {
  146. /* Check that region exists */
  147. if ( i >= memmap.count ) {
  148. if ( MEMMAP_IGNORE_NONEXISTENT ( setting->tag ) ) {
  149. continue;
  150. } else {
  151. DBGC ( settings, "MEMMAP region %d does not "
  152. "exist\n", i );
  153. return -ENOENT;
  154. }
  155. }
  156. /* Extract results from this region */
  157. region = &memmap.regions[i];
  158. if ( MEMMAP_INCLUDE_START ( setting->tag ) ) {
  159. result += region->start;
  160. DBGC ( settings, "MEMMAP %d start %08llx\n",
  161. i, region->start );
  162. }
  163. if ( MEMMAP_INCLUDE_LENGTH ( setting->tag ) ) {
  164. result += ( region->end - region->start );
  165. DBGC ( settings, "MEMMAP %d length %08llx\n",
  166. i, ( region->end - region->start ) );
  167. }
  168. }
  169. /* Scale result */
  170. result >>= MEMMAP_SCALE ( setting->tag );
  171. /* Return result */
  172. result = cpu_to_be64 ( result );
  173. if ( len > sizeof ( result ) )
  174. len = sizeof ( result );
  175. memcpy ( data, &result, len );
  176. /* Set type if not already specified */
  177. if ( ! setting->type )
  178. setting->type = &setting_type_hexraw;
  179. return sizeof ( result );
  180. }
  181. /** Memory map settings operations */
  182. static struct settings_operations memmap_settings_operations = {
  183. .applies = memmap_settings_applies,
  184. .fetch = memmap_settings_fetch,
  185. };
  186. /** Memory map settings */
  187. static struct settings memmap_settings = {
  188. .refcnt = NULL,
  189. .siblings = LIST_HEAD_INIT ( memmap_settings.siblings ),
  190. .children = LIST_HEAD_INIT ( memmap_settings.children ),
  191. .op = &memmap_settings_operations,
  192. .default_scope = &memmap_settings_scope,
  193. };
  194. /** Initialise memory map settings */
  195. static void memmap_settings_init ( void ) {
  196. int rc;
  197. if ( ( rc = register_settings ( &memmap_settings, NULL,
  198. "memmap" ) ) != 0 ) {
  199. DBG ( "MEMMAP could not register settings: %s\n",
  200. strerror ( rc ) );
  201. return;
  202. }
  203. }
  204. /** Memory map settings initialiser */
  205. struct init_fn memmap_settings_init_fn __init_fn ( INIT_NORMAL ) = {
  206. .initialise = memmap_settings_init,
  207. };
  208. /** Memory map predefined settings */
  209. const struct setting memsize_setting __setting ( SETTING_MISC, memsize ) = {
  210. .name = "memsize",
  211. .description = "Memory size (in MB)",
  212. .tag = MEMMAP_TAG ( 0, 0x100, 0, 1, 1, 20 ),
  213. .type = &setting_type_int32,
  214. .scope = &memmap_settings_scope,
  215. };