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
21 #include "qemu/osdep.h"
22 #include "qemu/units.h"
23 #include "qemu/cutils.h"
24 #include "qemu/bswap.h"
25 #include "exec/hwaddr.h"
26 #include "hw/mips/loongson3_bootp.h"
27
init_cpu_info(void * g_cpuinfo,uint32_t cpu_count,uint32_t processor_id,uint64_t cpu_freq)28 static void init_cpu_info(void *g_cpuinfo, uint32_t cpu_count,
29 uint32_t processor_id, uint64_t cpu_freq)
30 {
31 struct efi_cpuinfo_loongson *c = g_cpuinfo;
32
33 c->cputype = cpu_to_le32(Loongson_3A);
34 c->processor_id = cpu_to_le32(processor_id);
35 if (cpu_freq > UINT_MAX) {
36 c->cpu_clock_freq = cpu_to_le32(UINT_MAX);
37 } else {
38 c->cpu_clock_freq = cpu_to_le32(cpu_freq);
39 }
40
41 c->cpu_startup_core_id = cpu_to_le16(0);
42 c->nr_cpus = cpu_to_le32(cpu_count);
43 c->total_node = cpu_to_le32(DIV_ROUND_UP(cpu_count,
44 LOONGSON3_CORE_PER_NODE));
45 }
46
init_memory_map(void * g_map,uint64_t ram_size)47 static void init_memory_map(void *g_map, uint64_t ram_size)
48 {
49 struct efi_memory_map_loongson *emap = g_map;
50
51 emap->nr_map = cpu_to_le32(2);
52 emap->mem_freq = cpu_to_le32(300000000);
53
54 emap->map[0].node_id = cpu_to_le32(0);
55 emap->map[0].mem_type = cpu_to_le32(1);
56 emap->map[0].mem_start = cpu_to_le64(0x0);
57 emap->map[0].mem_size = cpu_to_le32(240);
58
59 emap->map[1].node_id = cpu_to_le32(0);
60 emap->map[1].mem_type = cpu_to_le32(2);
61 emap->map[1].mem_start = cpu_to_le64(0x90000000);
62 emap->map[1].mem_size = cpu_to_le32((ram_size / MiB) - 256);
63 }
64
init_system_loongson(void * g_system)65 static void init_system_loongson(void *g_system)
66 {
67 struct system_loongson *s = g_system;
68
69 s->ccnuma_smp = cpu_to_le32(0);
70 s->sing_double_channel = cpu_to_le32(1);
71 s->nr_uarts = cpu_to_le32(1);
72 s->uarts[0].iotype = cpu_to_le32(2);
73 s->uarts[0].int_offset = cpu_to_le32(2);
74 s->uarts[0].uartclk = cpu_to_le32(25000000); /* Random value */
75 s->uarts[0].uart_base = cpu_to_le64(virt_memmap[VIRT_UART].base);
76 }
77
init_irq_source(void * g_irq_source)78 static void init_irq_source(void *g_irq_source)
79 {
80 struct irq_source_routing_table *irq_info = g_irq_source;
81
82 irq_info->node_id = cpu_to_le32(0);
83 irq_info->PIC_type = cpu_to_le32(0);
84 irq_info->dma_mask_bits = cpu_to_le16(64);
85 irq_info->pci_mem_start_addr = cpu_to_le64(virt_memmap[VIRT_PCIE_MMIO].base);
86 irq_info->pci_mem_end_addr = cpu_to_le64(virt_memmap[VIRT_PCIE_MMIO].base +
87 virt_memmap[VIRT_PCIE_MMIO].size - 1);
88 irq_info->pci_io_start_addr = cpu_to_le64(virt_memmap[VIRT_PCIE_PIO].base);
89 }
90
init_interface_info(void * g_interface)91 static void init_interface_info(void *g_interface)
92 {
93 struct interface_info *interface = g_interface;
94
95 interface->vers = cpu_to_le16(0x01);
96 strpadcpy(interface->description, 64, "UEFI_Version_v1.0", '\0');
97 }
98
board_devices_info(void * g_board)99 static void board_devices_info(void *g_board)
100 {
101 struct board_devices *bd = g_board;
102
103 strpadcpy(bd->name, 64, "Loongson-3A-VIRT-1w-V1.00-demo", '\0');
104 }
105
init_special_info(void * g_special)106 static void init_special_info(void *g_special)
107 {
108 struct loongson_special_attribute *special = g_special;
109
110 strpadcpy(special->special_name, 64, "2018-05-01", '\0');
111 }
112
init_loongson_params(struct loongson_params * lp,void * p,uint32_t cpu_count,uint32_t processor_id,uint64_t cpu_freq,uint64_t ram_size)113 void init_loongson_params(struct loongson_params *lp, void *p,
114 uint32_t cpu_count, uint32_t processor_id,
115 uint64_t cpu_freq, uint64_t ram_size)
116 {
117 init_cpu_info(p, cpu_count, processor_id, cpu_freq);
118 lp->cpu_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
119 p += ROUND_UP(sizeof(struct efi_cpuinfo_loongson), 64);
120
121 init_memory_map(p, ram_size);
122 lp->memory_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
123 p += ROUND_UP(sizeof(struct efi_memory_map_loongson), 64);
124
125 init_system_loongson(p);
126 lp->system_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
127 p += ROUND_UP(sizeof(struct system_loongson), 64);
128
129 init_irq_source(p);
130 lp->irq_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
131 p += ROUND_UP(sizeof(struct irq_source_routing_table), 64);
132
133 init_interface_info(p);
134 lp->interface_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
135 p += ROUND_UP(sizeof(struct interface_info), 64);
136
137 board_devices_info(p);
138 lp->boarddev_table_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
139 p += ROUND_UP(sizeof(struct board_devices), 64);
140
141 init_special_info(p);
142 lp->special_offset = cpu_to_le64((uintptr_t)p - (uintptr_t)lp);
143 p += ROUND_UP(sizeof(struct loongson_special_attribute), 64);
144 }
145
init_reset_system(struct efi_reset_system_t * reset)146 void init_reset_system(struct efi_reset_system_t *reset)
147 {
148 reset->Shutdown = cpu_to_le64(0xffffffffbfc000a8);
149 reset->ResetCold = cpu_to_le64(0xffffffffbfc00080);
150 reset->ResetWarm = cpu_to_le64(0xffffffffbfc00080);
151 reset->DoSuspend = cpu_to_le64(0xffffffffbfc000d0);
152 }
153