radv: add mipmaps support for DCC decompression on compute
[mesa.git] / src / amd / common / ac_binary.c
index 01cf000d9bea64140016940457e771cd0edd106c..c047666b656d2be446967efad11da1eb29a0ffbe 100644 (file)
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
- *
- * Authors: Tom Stellard <thomas.stellard@amd.com>
- *
- * Based on radeon_elf_util.c.
  */
 
 #include "ac_binary.h"
@@ -109,7 +105,7 @@ static void parse_relocs(Elf *elf, Elf_Data *relocs, Elf_Data *symbols,
        }
 }
 
-void ac_elf_read(const char *elf_data, unsigned elf_size,
+bool ac_elf_read(const char *elf_data, unsigned elf_size,
                 struct ac_shader_binary *binary)
 {
        char *elf_buffer;
@@ -118,6 +114,7 @@ void ac_elf_read(const char *elf_data, unsigned elf_size,
        Elf_Data *symbols = NULL, *relocs = NULL;
        size_t section_str_index;
        unsigned symbol_sh_link = 0;
+       bool success = true;
 
        /* One of the libelf implementations
         * (http://www.mr511.de/software/english.htm) requires calling
@@ -137,7 +134,8 @@ void ac_elf_read(const char *elf_data, unsigned elf_size,
                GElf_Shdr section_header;
                if (gelf_getshdr(section, &section_header) != &section_header) {
                        fprintf(stderr, "Failed to read ELF section header\n");
-                       return;
+                       success = false;
+                       break;
                }
                name = elf_strptr(elf, section_str_index, section_header.sh_name);
                if (!strcmp(name, ".text")) {
@@ -148,6 +146,11 @@ void ac_elf_read(const char *elf_data, unsigned elf_size,
                } else if (!strcmp(name, ".AMDGPU.config")) {
                        section_data = elf_getdata(section, section_data);
                        binary->config_size = section_data->d_size;
+                       if (!binary->config_size) {
+                               fprintf(stderr, ".AMDGPU.config is empty!\n");
+                               success = false;
+                               break;
+                       }
                        binary->config = MALLOC(binary->config_size * sizeof(unsigned char));
                        memcpy(binary->config, section_data->d_buf, binary->config_size);
                } else if (!strcmp(name, ".AMDGPU.disasm")) {
@@ -186,9 +189,9 @@ void ac_elf_read(const char *elf_data, unsigned elf_size,
                binary->global_symbol_count = 1;
                binary->config_size_per_symbol = binary->config_size;
        }
+       return success;
 }
 
-static
 const unsigned char *ac_shader_binary_config_start(
        const struct ac_shader_binary *binary,
        uint64_t symbol_offset)
@@ -203,52 +206,33 @@ const unsigned char *ac_shader_binary_config_start(
        return binary->config;
 }
 
-
-static const char *scratch_rsrc_dword0_symbol =
-       "SCRATCH_RSRC_DWORD0";
-
-static const char *scratch_rsrc_dword1_symbol =
-       "SCRATCH_RSRC_DWORD1";
-
-void ac_shader_binary_read_config(struct ac_shader_binary *binary,
-                                 struct ac_shader_config *conf,
-                                 unsigned symbol_offset)
+/* Parse configuration data in .AMDGPU.config section format. */
+void ac_parse_shader_binary_config(const char *data, size_t nbytes,
+                                  bool really_needs_scratch,
+                                  struct ac_shader_config *conf)
 {
-       unsigned i;
-       const unsigned char *config =
-               ac_shader_binary_config_start(binary, symbol_offset);
-       bool really_needs_scratch = false;
-
-       /* LLVM adds SGPR spills to the scratch size.
-        * Find out if we really need the scratch buffer.
-        */
-       for (i = 0; i < binary->reloc_count; i++) {
-               const struct ac_shader_reloc *reloc = &binary->relocs[i];
+       uint32_t wavesize = 0;
 
-               if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name) ||
-                   !strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
-                       really_needs_scratch = true;
-                       break;
-               }
-       }
-
-       for (i = 0; i < binary->config_size_per_symbol; i+= 8) {
-               unsigned reg = util_le32_to_cpu(*(uint32_t*)(config + i));
-               unsigned value = util_le32_to_cpu(*(uint32_t*)(config + i + 4));
+       for (size_t i = 0; i < nbytes; i += 8) {
+               unsigned reg = util_le32_to_cpu(*(uint32_t*)(data + i));
+               unsigned value = util_le32_to_cpu(*(uint32_t*)(data + i + 4));
                switch (reg) {
                case R_00B028_SPI_SHADER_PGM_RSRC1_PS:
                case R_00B128_SPI_SHADER_PGM_RSRC1_VS:
                case R_00B228_SPI_SHADER_PGM_RSRC1_GS:
                case R_00B848_COMPUTE_PGM_RSRC1:
+               case R_00B428_SPI_SHADER_PGM_RSRC1_HS:
                        conf->num_sgprs = MAX2(conf->num_sgprs, (G_00B028_SGPRS(value) + 1) * 8);
                        conf->num_vgprs = MAX2(conf->num_vgprs, (G_00B028_VGPRS(value) + 1) * 4);
                        conf->float_mode =  G_00B028_FLOAT_MODE(value);
+                       conf->rsrc1 = value;
                        break;
                case R_00B02C_SPI_SHADER_PGM_RSRC2_PS:
                        conf->lds_size = MAX2(conf->lds_size, G_00B02C_EXTRA_LDS_SIZE(value));
                        break;
                case R_00B84C_COMPUTE_PGM_RSRC2:
                        conf->lds_size = MAX2(conf->lds_size, G_00B84C_LDS_SIZE(value));
+                       conf->rsrc2 = value;
                        break;
                case R_0286CC_SPI_PS_INPUT_ENA:
                        conf->spi_ps_input_ena = value;
@@ -259,9 +243,7 @@ void ac_shader_binary_read_config(struct ac_shader_binary *binary,
                case R_0286E8_SPI_TMPRING_SIZE:
                case R_00B860_COMPUTE_TMPRING_SIZE:
                        /* WAVESIZE is in units of 256 dwords. */
-                       if (really_needs_scratch)
-                               conf->scratch_bytes_per_wave =
-                                       G_00B860_WAVESIZE(value) * 256 * 4;
+                       wavesize = value;
                        break;
                case SPILLED_SGPRS:
                        conf->spilled_sgprs = value;
@@ -281,8 +263,62 @@ void ac_shader_binary_read_config(struct ac_shader_binary *binary,
                        }
                        break;
                }
+       }
+
+       if (!conf->spi_ps_input_addr)
+               conf->spi_ps_input_addr = conf->spi_ps_input_ena;
 
-               if (!conf->spi_ps_input_addr)
-                       conf->spi_ps_input_addr = conf->spi_ps_input_ena;
+       if (really_needs_scratch) {
+               /* sgprs spills aren't spilling */
+               conf->scratch_bytes_per_wave = G_00B860_WAVESIZE(wavesize) * 256 * 4;
        }
 }
+
+static const char *scratch_rsrc_dword0_symbol =
+       "SCRATCH_RSRC_DWORD0";
+
+static const char *scratch_rsrc_dword1_symbol =
+       "SCRATCH_RSRC_DWORD1";
+
+void ac_shader_binary_read_config(struct ac_shader_binary *binary,
+                                 struct ac_shader_config *conf,
+                                 unsigned symbol_offset,
+                                 bool supports_spill)
+{
+       unsigned i;
+       const char *config =
+               (const char *)ac_shader_binary_config_start(binary, symbol_offset);
+       bool really_needs_scratch = false;
+       /* LLVM adds SGPR spills to the scratch size.
+        * Find out if we really need the scratch buffer.
+        */
+       if (supports_spill) {
+               really_needs_scratch = true;
+       } else {
+               for (i = 0; i < binary->reloc_count; i++) {
+                       const struct ac_shader_reloc *reloc = &binary->relocs[i];
+
+                       if (!strcmp(scratch_rsrc_dword0_symbol, reloc->name) ||
+                           !strcmp(scratch_rsrc_dword1_symbol, reloc->name)) {
+                               really_needs_scratch = true;
+                               break;
+                       }
+               }
+       }
+
+       ac_parse_shader_binary_config(config, binary->config_size_per_symbol,
+                                     really_needs_scratch, conf);
+}
+
+void ac_shader_binary_clean(struct ac_shader_binary *b)
+{
+       if (!b)
+               return;
+       FREE(b->code);
+       FREE(b->config);
+       FREE(b->rodata);
+       FREE(b->global_symbol_offsets);
+       FREE(b->relocs);
+       FREE(b->disasm_string);
+       FREE(b->llvm_ir_string);
+}