coreboot_table.c 4.4 KB

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