radeonsi: drop support for LLVM 3.8
[mesa.git] / src / amd / common / ac_binary.c
1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors: Tom Stellard <thomas.stellard@amd.com>
24 *
25 * Based on radeon_elf_util.c.
26 */
27
28 #include "ac_binary.h"
29
30 #include "util/u_math.h"
31 #include "util/u_memory.h"
32
33 #include <gelf.h>
34 #include <libelf.h>
35 #include <stdio.h>
36
37 #include <sid.h>
38
39 #define SPILLED_SGPRS 0x4
40 #define SPILLED_VGPRS 0x8
41
42 static void parse_symbol_table(Elf_Data *symbol_table_data,
43 const GElf_Shdr *symbol_table_header,
44 struct ac_shader_binary *binary)
45 {
46 GElf_Sym symbol;
47 unsigned i = 0;
48 unsigned symbol_count =
49 symbol_table_header->sh_size / symbol_table_header->sh_entsize;
50
51 /* We are over allocating this list, because symbol_count gives the
52 * total number of symbols, and we will only be filling the list
53 * with offsets of global symbols. The memory savings from
54 * allocating the correct size of this list will be small, and
55 * I don't think it is worth the cost of pre-computing the number
56 * of global symbols.
57 */
58 binary->global_symbol_offsets = CALLOC(symbol_count, sizeof(uint64_t));
59
60 while (gelf_getsym(symbol_table_data, i++, &symbol)) {
61 unsigned i;
62 if (GELF_ST_BIND(symbol.st_info) != STB_GLOBAL ||
63 symbol.st_shndx == 0 /* Undefined symbol */) {
64 continue;
65 }
66
67 binary->global_symbol_offsets[binary->global_symbol_count] =
68 symbol.st_value;
69
70 /* Sort the list using bubble sort. This list will usually
71 * be small. */
72 for (i = binary->global_symbol_count; i > 0; --i) {
73 uint64_t lhs = binary->global_symbol_offsets[i - 1];
74 uint64_t rhs = binary->global_symbol_offsets[i];
75 if (lhs < rhs) {
76 break;
77 }
78 binary->global_symbol_offsets[i] = lhs;
79 binary->global_symbol_offsets[i - 1] = rhs;
80 }
81 ++binary->global_symbol_count;
82 }
83 }
84
85 static void parse_relocs(Elf *elf, Elf_Data *relocs, Elf_Data *symbols,
86 unsigned symbol_sh_link,
87 struct ac_shader_binary *binary)
88 {
89 unsigned i;
90
91 if (!relocs || !symbols || !binary->reloc_count) {
92 return;
93 }
94 binary->relocs = CALLOC(binary->reloc_count,
95 sizeof(struct ac_shader_reloc));
96 for (i = 0; i < binary->reloc_count; i++) {
97 GElf_Sym symbol;
98 GElf_Rel rel;
99 char *symbol_name;
100 struct ac_shader_reloc *reloc = &binary->relocs[i];
101
102 gelf_getrel(relocs, i, &rel);
103 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &symbol);
104 symbol_name = elf_strptr(elf, symbol_sh_link, symbol.st_name);
105
106 reloc->offset = rel.r_offset;
107 strncpy(reloc->name, symbol_name, sizeof(reloc->name)-1);
108 reloc->name[sizeof(reloc->name)-1] = 0;
109 }
110 }
111
112 void ac_elf_read(const char *elf_data, unsigned elf_size,
113 struct ac_shader_binary *binary)
114 {
115 char *elf_buffer;
116 Elf *elf;
117 Elf_Scn *section = NULL;
118 Elf_Data *symbols = NULL, *relocs = NULL;
119 size_t section_str_index;
120 unsigned symbol_sh_link = 0;
121
122 /* One of the libelf implementations
123 * (http://www.mr511.de/software/english.htm) requires calling
124 * elf_version() before elf_memory().
125 */
126 elf_version(EV_CURRENT);
127 elf_buffer = MALLOC(elf_size);
128 memcpy(elf_buffer, elf_data, elf_size);
129
130 elf = elf_memory(elf_buffer, elf_size);
131
132 elf_getshdrstrndx(elf, &section_str_index);
133
134 while ((section = elf_nextscn(elf, section))) {
135 const char *name;
136 Elf_Data *section_data = NULL;
137 GElf_Shdr section_header;
138 if (gelf_getshdr(section, &section_header) != &section_header) {
139 fprintf(stderr, "Failed to read ELF section header\n");
140 return;
141 }
142 name = elf_strptr(elf, section_str_index, section_header.sh_name);
143 if (!strcmp(name, ".text")) {
144 section_data = elf_getdata(section, section_data);
145 binary->code_size = section_data->d_size;
146 binary->code = MALLOC(binary->code_size * sizeof(unsigned char));
147 memcpy(binary->code, section_data->d_buf, binary->code_size);
148 } else if (!strcmp(name, ".AMDGPU.config")) {
149 section_data = elf_getdata(section, section_data);
150 binary->config_size = section_data->d_size;
151 binary->config = MALLOC(binary->config_size * sizeof(unsigned char));
152 memcpy(binary->config, section_data->d_buf, binary->config_size);
153 } else if (!strcmp(name, ".AMDGPU.disasm")) {
154 /* Always read disassembly if it's available. */
155 section_data = elf_getdata(section, section_data);
156 binary->disasm_string = strndup(section_data->d_buf,
157 section_data->d_size);
158 } else if (!strncmp(name, ".rodata", 7)) {
159 section_data = elf_getdata(section, section_data);
160 binary->rodata_size = section_data->d_size;
161 binary->rodata = MALLOC(binary->rodata_size * sizeof(unsigned char));
162 memcpy(binary->rodata, section_data->d_buf, binary->rodata_size);
163 } else if (!strncmp(name, ".symtab", 7)) {
164 symbols = elf_getdata(section, section_data);
165 symbol_sh_link = section_header.sh_link;
166 parse_symbol_table(symbols, &section_header, binary);
167 } else if (!strcmp(name, ".rel.text")) {
168 relocs = elf_getdata(section, section_data);
169 binary->reloc_count = section_header.sh_size /
170 section_header.sh_entsize;
171 }
172 }
173
174 parse_relocs(elf, relocs, symbols, symbol_sh_link, binary);
175
176 if (elf){
177 elf_end(elf);
178 }
179 FREE(elf_buffer);
180
181 /* Cache the config size per symbol */
182 if (binary->global_symbol_count) {
183 binary->config_size_per_symbol =
184 binary->config_size / binary->global_symbol_count;
185 } else {
186 binary->global_symbol_count = 1;
187 binary->config_size_per_symbol = binary->config_size;
188 }
189 }
190
191 const unsigned char *ac_shader_binary_config_start(
192 const struct ac_shader_binary *binary,
193 uint64_t symbol_offset)
194 {
195 unsigned i;
196 for (i = 0; i < binary->global_symbol_count; ++i) {
197 if (binary->global_symbol_offsets[i] == symbol_offset) {
198 unsigned offset = i * binary->config_size_per_symbol;
199 return binary->config + offset;
200 }
201 }
202 return binary->config;
203 }
204
205
206 static const char *scratch_rsrc_dword0_symbol =
207 "SCRATCH_RSRC_DWORD0";
208
209 static const char *scratch_rsrc_dword1_symbol =
210 "SCRATCH_RSRC_DWORD1";
211
212 void ac_shader_binary_read_config(struct ac_shader_binary *binary,
213 struct ac_shader_config *conf,
214 unsigned symbol_offset,
215 bool supports_spill)
216 {
217 unsigned i;
218 const unsigned char *config =
219 ac_shader_binary_config_start(binary, symbol_offset);
220 bool really_needs_scratch = false;
221 uint32_t wavesize = 0;
222 /* LLVM adds SGPR spills to the scratch size.
223 * Find out if we really need the scratch buffer.
224 */
225 if (supports_spill) {
226 really_needs_scratch = true;
227 } else {
228 for (i = 0; i < binary->reloc_count; i++) {
229 const struct ac_shader_reloc *reloc = &binary->relocs[i];
230
231 if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name) ||
232 !strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
233 really_needs_scratch = true;
234 break;
235 }
236 }
237 }
238
239 for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
240 unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
241 unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
242 switch (reg) {
243 case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
244 case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
245 case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
246 case R_00B848_COMPUTE_PGM_RSRC1:
247 conf->num_sgprs = MAX2(conf->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
248 conf->num_vgprs = MAX2(conf->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
249 conf->float_mode = G_00B028_FLOAT_MODE(value);
250 break;
251 case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
252 conf->lds_size = MAX2(conf->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
253 break;
254 case R_00B84C_COMPUTE_PGM_RSRC2:
255 conf->lds_size = MAX2(conf->lds_size, G_00B84C_LDS_SIZE(value));
256 break;
257 case R_0286CC_SPI_PS_INPUT_ENA:
258 conf->spi_ps_input_ena = value;
259 break;
260 case R_0286D0_SPI_PS_INPUT_ADDR:
261 conf->spi_ps_input_addr = value;
262 break;
263 case R_0286E8_SPI_TMPRING_SIZE:
264 case R_00B860_COMPUTE_TMPRING_SIZE:
265 /* WAVESIZE is in units of 256 dwords. */
266 wavesize = value;
267 break;
268 case SPILLED_SGPRS:
269 conf->spilled_sgprs = value;
270 break;
271 case SPILLED_VGPRS:
272 conf->spilled_vgprs = value;
273 break;
274 default:
275 {
276 static bool printed;
277
278 if (!printed) {
279 fprintf(stderr, "Warning: LLVM emitted unknown "
280 "config register: 0x%x\n", reg);
281 printed = true;
282 }
283 }
284 break;
285 }
286
287 if (!conf->spi_ps_input_addr)
288 conf->spi_ps_input_addr = conf->spi_ps_input_ena;
289 }
290
291 if (really_needs_scratch) {
292 /* sgprs spills aren't spilling */
293 conf->scratch_bytes_per_wave = G_00B860_WAVESIZE(wavesize) * 256 * 4;
294 }
295 }