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.6KB

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