986f4b2e5ac7f6d9a06a96f7ff83af1db0e5cdab
[oweals/u-boot.git] / include / qemu_fw_cfg.h
1 /*
2  * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #ifndef __FW_CFG__
8 #define __FW_CFG__
9
10 #define FW_CONTROL_PORT 0x510
11 #define FW_DATA_PORT            0x511
12 #define FW_DMA_PORT_LOW 0x514
13 #define FW_DMA_PORT_HIGH        0x518
14
15 #include <linux/list.h>
16
17 enum qemu_fwcfg_items {
18         FW_CFG_SIGNATURE        = 0x00,
19         FW_CFG_ID               = 0x01,
20         FW_CFG_UUID             = 0x02,
21         FW_CFG_RAM_SIZE         = 0x03,
22         FW_CFG_NOGRAPHIC        = 0x04,
23         FW_CFG_NB_CPUS          = 0x05,
24         FW_CFG_MACHINE_ID       = 0x06,
25         FW_CFG_KERNEL_ADDR      = 0x07,
26         FW_CFG_KERNEL_SIZE      = 0x08,
27         FW_CFG_KERNEL_CMDLINE   = 0x09,
28         FW_CFG_INITRD_ADDR      = 0x0a,
29         FW_CFG_INITRD_SIZE      = 0x0b,
30         FW_CFG_BOOT_DEVICE      = 0x0c,
31         FW_CFG_NUMA             = 0x0d,
32         FW_CFG_BOOT_MENU        = 0x0e,
33         FW_CFG_MAX_CPUS         = 0x0f,
34         FW_CFG_KERNEL_ENTRY     = 0x10,
35         FW_CFG_KERNEL_DATA      = 0x11,
36         FW_CFG_INITRD_DATA      = 0x12,
37         FW_CFG_CMDLINE_ADDR     = 0x13,
38         FW_CFG_CMDLINE_SIZE     = 0x14,
39         FW_CFG_CMDLINE_DATA     = 0x15,
40         FW_CFG_SETUP_ADDR       = 0x16,
41         FW_CFG_SETUP_SIZE       = 0x17,
42         FW_CFG_SETUP_DATA       = 0x18,
43         FW_CFG_FILE_DIR         = 0x19,
44         FW_CFG_FILE_FIRST       = 0x20,
45         FW_CFG_WRITE_CHANNEL    = 0x4000,
46         FW_CFG_ARCH_LOCAL       = 0x8000,
47         FW_CFG_INVALID          = 0xffff,
48 };
49
50 enum {
51         BIOS_LINKER_LOADER_COMMAND_ALLOCATE     = 0x1,
52         BIOS_LINKER_LOADER_COMMAND_ADD_POINTER  = 0x2,
53         BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM = 0x3,
54 };
55
56 enum {
57         BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1,
58         BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2,
59 };
60
61 #define FW_CFG_FILE_SLOTS       0x10
62 #define FW_CFG_MAX_ENTRY        (FW_CFG_FILE_FIRST + FW_CFG_FILE_SLOTS)
63 #define FW_CFG_ENTRY_MASK        ~(FW_CFG_WRITE_CHANNEL | FW_CFG_ARCH_LOCAL)
64
65 #define FW_CFG_MAX_FILE_PATH    56
66 #define BIOS_LINKER_LOADER_FILESZ FW_CFG_MAX_FILE_PATH
67
68 #define QEMU_FW_CFG_SIGNATURE   (('Q' << 24) | ('E' << 16) | ('M' << 8) | 'U')
69
70 #define FW_CFG_DMA_ERROR        (1 << 0)
71 #define FW_CFG_DMA_READ (1 << 1)
72 #define FW_CFG_DMA_SKIP (1 << 2)
73 #define FW_CFG_DMA_SELECT       (1 << 3)
74
75 #define FW_CFG_DMA_ENABLED      (1 << 1)
76
77 struct fw_cfg_file {
78         __be32 size;
79         __be16 select;
80         __be16 reserved;
81         char name[FW_CFG_MAX_FILE_PATH];
82 };
83
84 struct fw_file {
85         struct fw_cfg_file cfg; /* firmware file information */
86         unsigned long addr;     /* firmware file in-memory address */
87         struct list_head list;  /* list node to link to fw_list */
88 };
89
90 struct fw_cfg_file_iter {
91         struct list_head *entry; /* structure to iterate file list */
92 };
93
94 struct fw_cfg_dma_access {
95         __be32 control;
96         __be32 length;
97         __be64 address;
98 };
99
100 struct bios_linker_entry {
101         __le32 command;
102         union {
103                 /*
104                  * COMMAND_ALLOCATE - allocate a table from @alloc.file
105                  * subject to @alloc.align alignment (must be power of 2)
106                  * and @alloc.zone (can be HIGH or FSEG) requirements.
107                  *
108                  * Must appear exactly once for each file, and before
109                  * this file is referenced by any other command.
110                  */
111                 struct {
112                         char file[BIOS_LINKER_LOADER_FILESZ];
113                         __le32 align;
114                         uint8_t zone;
115                 } alloc;
116
117                 /*
118                  * COMMAND_ADD_POINTER - patch the table (originating from
119                  * @dest_file) at @pointer.offset, by adding a pointer to the
120                  * table originating from @src_file. 1,2,4 or 8 byte unsigned
121                  * addition is used depending on @pointer.size.
122                  */
123                 struct {
124                         char dest_file[BIOS_LINKER_LOADER_FILESZ];
125                         char src_file[BIOS_LINKER_LOADER_FILESZ];
126                         __le32 offset;
127                         uint8_t size;
128                 } pointer;
129
130                 /*
131                  * COMMAND_ADD_CHECKSUM - calculate checksum of the range
132                  * specified by @cksum_start and @cksum_length fields,
133                  * and then add the value at @cksum.offset.
134                  * Checksum simply sums -X for each byte X in the range
135                  * using 8-bit math.
136                  */
137                 struct {
138                         char file[BIOS_LINKER_LOADER_FILESZ];
139                         __le32 offset;
140                         __le32 start;
141                         __le32 length;
142                 } cksum;
143
144                 /* padding */
145                 char pad[124];
146         };
147 } __packed;
148
149 /**
150  * Initialize QEMU fw_cfg interface
151  */
152 void qemu_fwcfg_init(void);
153
154 void qemu_fwcfg_read_entry(uint16_t entry, uint32_t length, void *address);
155 int qemu_fwcfg_read_firmware_list(void);
156 struct fw_file *qemu_fwcfg_find_file(const char *name);
157
158 /**
159  * Get system cpu number
160  *
161  * @return:   cpu number in system
162  */
163 int qemu_fwcfg_online_cpus(void);
164
165 /* helper functions to iterate firmware file list */
166 struct fw_file *qemu_fwcfg_file_iter_init(struct fw_cfg_file_iter *iter);
167 struct fw_file *qemu_fwcfg_file_iter_next(struct fw_cfg_file_iter *iter);
168 bool qemu_fwcfg_file_iter_end(struct fw_cfg_file_iter *iter);
169
170 #endif