coreboot_table.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <vbe.h>
  7. #include <asm/acpi_s3.h>
  8. #include <asm/coreboot_tables.h>
  9. #include <asm/e820.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. int high_table_reserve(void)
  12. {
  13. /* adjust stack pointer to reserve space for configuration tables */
  14. gd->arch.high_table_limit = gd->start_addr_sp;
  15. gd->start_addr_sp -= CONFIG_HIGH_TABLE_SIZE;
  16. gd->arch.high_table_ptr = gd->start_addr_sp;
  17. /* clear the memory */
  18. #ifdef CONFIG_HAVE_ACPI_RESUME
  19. if (gd->arch.prev_sleep_state != ACPI_S3)
  20. #endif
  21. memset((void *)gd->arch.high_table_ptr, 0,
  22. CONFIG_HIGH_TABLE_SIZE);
  23. gd->start_addr_sp &= ~0xf;
  24. return 0;
  25. }
  26. void *high_table_malloc(size_t bytes)
  27. {
  28. u32 new_ptr;
  29. void *ptr;
  30. new_ptr = gd->arch.high_table_ptr + bytes;
  31. if (new_ptr >= gd->arch.high_table_limit)
  32. return NULL;
  33. ptr = (void *)gd->arch.high_table_ptr;
  34. gd->arch.high_table_ptr = new_ptr;
  35. return ptr;
  36. }
  37. /**
  38. * cb_table_init() - initialize a coreboot table header
  39. *
  40. * This fills in the coreboot table header signature and the header bytes.
  41. * Other fields are set to zero.
  42. *
  43. * @cbh: coreboot table header address
  44. */
  45. static void cb_table_init(struct cb_header *cbh)
  46. {
  47. memset(cbh, 0, sizeof(struct cb_header));
  48. memcpy(cbh->signature, "LBIO", 4);
  49. cbh->header_bytes = sizeof(struct cb_header);
  50. }
  51. /**
  52. * cb_table_add_entry() - add a coreboot table entry
  53. *
  54. * This increases the coreboot table entry size with added table entry length
  55. * and increases entry count by 1.
  56. *
  57. * @cbh: coreboot table header address
  58. * @cbr: to be added table entry address
  59. * @return: pointer to next table entry address
  60. */
  61. static u32 cb_table_add_entry(struct cb_header *cbh, struct cb_record *cbr)
  62. {
  63. cbh->table_bytes += cbr->size;
  64. cbh->table_entries++;
  65. return (u32)cbr + cbr->size;
  66. }
  67. /**
  68. * cb_table_finalize() - finalize the coreboot table
  69. *
  70. * This calculates the checksum for all coreboot table entries as well as
  71. * the checksum for the coreboot header itself.
  72. *
  73. * @cbh: coreboot table header address
  74. */
  75. static void cb_table_finalize(struct cb_header *cbh)
  76. {
  77. struct cb_record *cbr = (struct cb_record *)(cbh + 1);
  78. cbh->table_checksum = compute_ip_checksum(cbr, cbh->table_bytes);
  79. cbh->header_checksum = compute_ip_checksum(cbh, cbh->header_bytes);
  80. }
  81. void write_coreboot_table(u32 addr, struct memory_area *cfg_tables)
  82. {
  83. struct cb_header *cbh = (struct cb_header *)addr;
  84. struct cb_record *cbr;
  85. struct cb_memory *mem;
  86. struct cb_memory_range *map;
  87. struct e820_entry e820[32];
  88. struct cb_framebuffer *fb;
  89. struct vesa_mode_info *vesa;
  90. int i, num;
  91. cb_table_init(cbh);
  92. cbr = (struct cb_record *)(cbh + 1);
  93. /*
  94. * Two type of coreboot table entries are generated by us.
  95. * They are 'struct cb_memory' and 'struct cb_framebuffer'.
  96. */
  97. /* populate memory map table */
  98. mem = (struct cb_memory *)cbr;
  99. mem->tag = CB_TAG_MEMORY;
  100. map = mem->map;
  101. /* first install e820 defined memory maps */
  102. num = install_e820_map(ARRAY_SIZE(e820), e820);
  103. for (i = 0; i < num; i++) {
  104. map->start.lo = e820[i].addr & 0xffffffff;
  105. map->start.hi = e820[i].addr >> 32;
  106. map->size.lo = e820[i].size & 0xffffffff;
  107. map->size.hi = e820[i].size >> 32;
  108. map->type = e820[i].type;
  109. map++;
  110. }
  111. /* then install all configuration tables */
  112. while (cfg_tables->size) {
  113. map->start.lo = cfg_tables->start & 0xffffffff;
  114. map->start.hi = cfg_tables->start >> 32;
  115. map->size.lo = cfg_tables->size & 0xffffffff;
  116. map->size.hi = cfg_tables->size >> 32;
  117. map->type = CB_MEM_TABLE;
  118. map++;
  119. num++;
  120. cfg_tables++;
  121. }
  122. mem->size = num * sizeof(struct cb_memory_range) +
  123. sizeof(struct cb_record);
  124. cbr = (struct cb_record *)cb_table_add_entry(cbh, cbr);
  125. /* populate framebuffer table if we have sane vesa info */
  126. vesa = &mode_info.vesa;
  127. if (vesa->x_resolution && vesa->y_resolution) {
  128. fb = (struct cb_framebuffer *)cbr;
  129. fb->tag = CB_TAG_FRAMEBUFFER;
  130. fb->size = sizeof(struct cb_framebuffer);
  131. fb->x_resolution = vesa->x_resolution;
  132. fb->y_resolution = vesa->y_resolution;
  133. fb->bits_per_pixel = vesa->bits_per_pixel;
  134. fb->bytes_per_line = vesa->bytes_per_scanline;
  135. fb->physical_address = vesa->phys_base_ptr;
  136. fb->red_mask_size = vesa->red_mask_size;
  137. fb->red_mask_pos = vesa->red_mask_pos;
  138. fb->green_mask_size = vesa->green_mask_size;
  139. fb->green_mask_pos = vesa->green_mask_pos;
  140. fb->blue_mask_size = vesa->blue_mask_size;
  141. fb->blue_mask_pos = vesa->blue_mask_pos;
  142. fb->reserved_mask_size = vesa->reserved_mask_size;
  143. fb->reserved_mask_pos = vesa->reserved_mask_pos;
  144. cbr = (struct cb_record *)cb_table_add_entry(cbh, cbr);
  145. }
  146. cb_table_finalize(cbh);
  147. }