loongson3_bootp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * LEFI (a UEFI-like interface for BIOS-Kernel boot parameters) helpers
  3. *
  4. * Copyright (c) 2018-2020 Huacai Chen (chenhc@lemote.com)
  5. * Copyright (c) 2018-2020 Jiaxun Yang <jiaxun.yang@flygoat.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/units.h"
  22. #include "qemu/cutils.h"
  23. #include "cpu.h"
  24. #include "hw/boards.h"
  25. #include "hw/mips/loongson3_bootp.h"
  26. #define LOONGSON3_CORE_PER_NODE 4
  27. static void init_cpu_info(void *g_cpuinfo, uint64_t cpu_freq)
  28. {
  29. struct efi_cpuinfo_loongson *c = g_cpuinfo;
  30. c->cputype = cpu_to_le32(Loongson_3A);
  31. c->processor_id = cpu_to_le32(MIPS_CPU(first_cpu)->env.CP0_PRid);
  32. if (cpu_freq > UINT_MAX) {
  33. c->cpu_clock_freq = cpu_to_le32(UINT_MAX);
  34. } else {
  35. c->cpu_clock_freq = cpu_to_le32(cpu_freq);
  36. }
  37. c->cpu_startup_core_id = cpu_to_le16(0);
  38. c->nr_cpus = cpu_to_le32(current_machine->smp.cpus);
  39. c->total_node = cpu_to_le32(DIV_ROUND_UP(current_machine->smp.cpus,
  40. LOONGSON3_CORE_PER_NODE));
  41. }
  42. static void init_memory_map(void *g_map, uint64_t ram_size)
  43. {
  44. struct efi_memory_map_loongson *emap = g_map;
  45. emap->nr_map = cpu_to_le32(2);
  46. emap->mem_freq = cpu_to_le32(300000000);
  47. emap->map[0].node_id = cpu_to_le32(0);
  48. emap->map[0].mem_type = cpu_to_le32(1);
  49. emap->map[0].mem_start = cpu_to_le64(0x0);
  50. emap->map[0].mem_size = cpu_to_le32(240);
  51. emap->map[1].node_id = cpu_to_le32(0);
  52. emap->map[1].mem_type = cpu_to_le32(2);
  53. emap->map[1].mem_start = cpu_to_le64(0x90000000);
  54. emap->map[1].mem_size = cpu_to_le32((ram_size / MiB) - 256);
  55. }
  56. static void init_system_loongson(void *g_system)
  57. {
  58. struct system_loongson *s = g_system;
  59. s->ccnuma_smp = cpu_to_le32(0);
  60. s->sing_double_channel = cpu_to_le32(1);
  61. s->nr_uarts = cpu_to_le32(1);
  62. s->uarts[0].iotype = cpu_to_le32(2);
  63. s->uarts[0].int_offset = cpu_to_le32(2);
  64. s->uarts[0].uartclk = cpu_to_le32(25000000); /* Random value */
  65. s->uarts[0].uart_base = cpu_to_le64(virt_memmap[VIRT_UART].base);
  66. }
  67. static void init_irq_source(void *g_irq_source)
  68. {
  69. struct irq_source_routing_table *irq_info = g_irq_source;
  70. irq_info->node_id = cpu_to_le32(0);
  71. irq_info->PIC_type = cpu_to_le32(0);
  72. irq_info->dma_mask_bits = cpu_to_le16(64);
  73. irq_info->pci_mem_start_addr = cpu_to_le64(virt_memmap[VIRT_PCIE_MMIO].base);
  74. irq_info->pci_mem_end_addr = cpu_to_le64(virt_memmap[VIRT_PCIE_MMIO].base +
  75. virt_memmap[VIRT_PCIE_MMIO].size - 1);
  76. irq_info->pci_io_start_addr = cpu_to_le64(virt_memmap[VIRT_PCIE_PIO].base);
  77. }
  78. static void init_interface_info(void *g_interface)
  79. {
  80. struct interface_info *interface = g_interface;
  81. interface->vers = cpu_to_le16(0x01);
  82. strpadcpy(interface->description, 64, "UEFI_Version_v1.0", '\0');
  83. }
  84. static void board_devices_info(void *g_board)
  85. {
  86. struct board_devices *bd = g_board;
  87. strpadcpy(bd->name, 64, "Loongson-3A-VIRT-1w-V1.00-demo", '\0');
  88. }
  89. static void init_special_info(void *g_special)
  90. {
  91. struct loongson_special_attribute *special = g_special;
  92. strpadcpy(special->special_name, 64, "2018-05-01", '\0');
  93. }
  94. void init_loongson_params(struct loongson_params *lp, void *p,
  95. uint64_t cpu_freq, uint64_t ram_size)
  96. {
  97. init_cpu_info(p, cpu_freq);
  98. lp->cpu_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
  99. p += ROUND_UP(sizeof(struct efi_cpuinfo_loongson), 64);
  100. init_memory_map(p, ram_size);
  101. lp->memory_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
  102. p += ROUND_UP(sizeof(struct efi_memory_map_loongson), 64);
  103. init_system_loongson(p);
  104. lp->system_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
  105. p += ROUND_UP(sizeof(struct system_loongson), 64);
  106. init_irq_source(p);
  107. lp->irq_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
  108. p += ROUND_UP(sizeof(struct irq_source_routing_table), 64);
  109. init_interface_info(p);
  110. lp->interface_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
  111. p += ROUND_UP(sizeof(struct interface_info), 64);
  112. board_devices_info(p);
  113. lp->boarddev_table_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
  114. p += ROUND_UP(sizeof(struct board_devices), 64);
  115. init_special_info(p);
  116. lp->special_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
  117. p += ROUND_UP(sizeof(struct loongson_special_attribute), 64);
  118. }
  119. void init_reset_system(struct efi_reset_system_t *reset)
  120. {
  121. reset->Shutdown = cpu_to_le64(0xffffffffbfc000a8);
  122. reset->ResetCold = cpu_to_le64(0xffffffffbfc00080);
  123. reset->ResetWarm = cpu_to_le64(0xffffffffbfc00080);
  124. }