Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 65 additions & 21 deletions src/asahi/lib/agx_bg_eot.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ agx_g17p_rt0_eot_text_supported(const struct agx_shader_part *bin)
static bool
agx_g17p_upload_bg_eot(struct agx_bg_eot_cache *cache,
const struct agx_shader_part *bin,
struct agx_bg_eot_shader *res)
struct agx_bg_eot_shader *res,
enum agx_bg_eot_error *error)
{
/* Match the standalone allocation lead already used for G17P main
* shaders. Native packed CODE blocks do not each own such a lead. */
Expand Down Expand Up @@ -122,8 +123,10 @@ agx_g17p_upload_bg_eot(struct agx_bg_eot_cache *cache,

const size_t size = lead_size + block_size;
uint8_t *image = malloc(size);
if (!image)
if (!image) {
*error = AGX_BG_EOT_HOST_OOM;
return false;
}

if (cache->dev->chip == AGX_CHIP_G18G) {
if (!agx3_build_g18g_container_lead(image, lead_size)) {
Expand Down Expand Up @@ -153,6 +156,8 @@ agx_g17p_upload_bg_eot(struct agx_bg_eot_cache *cache,
&res->bo)
: 0;
if (!allocation) {
if (built)
*error = AGX_BG_EOT_DEVICE_OOM;
free(image);
return false;
}
Expand Down Expand Up @@ -288,20 +293,35 @@ agx_compile_bg_eot_nir(const struct agx_device *dev, nir_shader *shader,
return true;
}

static struct agx_bg_eot_shader *
agx_alloc_bg_eot_shader(struct agx_bg_eot_cache *cache,
enum agx_bg_eot_error *error)
{
struct agx_bg_eot_shader *res = rzalloc(cache->ht, struct agx_bg_eot_shader);
if (!res)
*error = AGX_BG_EOT_HOST_OOM;
return res;
}

static struct agx_bg_eot_shader *
agx_upload_bg_eot_part(struct agx_bg_eot_cache *cache,
struct agx_shader_part *bin, bool check_rt0_eot)
struct agx_shader_part *bin, bool check_rt0_eot,
enum agx_bg_eot_error *error)
{
if (check_rt0_eot && agx_chip_native_agx3(cache->dev->chip) &&
!agx_g17p_rt0_eot_text_supported(bin)) {
free(bin->binary);
return NULL;
}

struct agx_bg_eot_shader *res = rzalloc(cache->ht, struct agx_bg_eot_shader);
struct agx_bg_eot_shader *res = agx_alloc_bg_eot_shader(cache, error);
if (!res) {
free(bin->binary);
return NULL;
}
res->info = bin->info;
if (agx_chip_agx3_path(cache->dev->chip)) {
if (!agx_g17p_upload_bg_eot(cache, bin, res)) {
if (!agx_g17p_upload_bg_eot(cache, bin, res, error)) {
free(bin->binary);
ralloc_free(res);
return NULL;
Expand All @@ -311,18 +331,27 @@ agx_upload_bg_eot_part(struct agx_bg_eot_cache *cache,
&cache->pool, bin->binary, bin->info.binary_size, 128, &res->bo);
}
free(bin->binary);

/* Do not cache a failed upload. */
if (!res->ptr) {
*error = AGX_BG_EOT_DEVICE_OOM;
ralloc_free(res);
return NULL;
}

return res;
}

static struct agx_bg_eot_shader *
agx_compile_bg_eot_shader(struct agx_bg_eot_cache *cache, nir_shader *shader,
struct agx_shader_key *key,
struct agx_tilebuffer_layout *tib)
struct agx_tilebuffer_layout *tib,
enum agx_bg_eot_error *error)
{
struct agx_shader_part bin = {0};
if (!agx_compile_bg_eot_nir(cache->dev, shader, key, tib, &bin))
return NULL;
return agx_upload_bg_eot_part(cache, &bin, tib == NULL);
return agx_upload_bg_eot_part(cache, &bin, tib == NULL, error);
}

static nir_def *
Expand Down Expand Up @@ -546,7 +575,8 @@ agx_compile_background_shader(const struct agx_device *dev,

static struct agx_bg_eot_shader *
agx_build_background_shader(struct agx_bg_eot_cache *cache,
struct agx_bg_eot_key *key)
struct agx_bg_eot_key *key,
enum agx_bg_eot_error *error)
{
const bool g18g = cache->dev->chip == AGX_CHIP_G18G;
if (agx_chip_hal300_path(cache->dev->chip) && !key->tib.sample_size_B &&
Expand Down Expand Up @@ -586,8 +616,8 @@ agx_build_background_shader(struct agx_bg_eot_cache *cache,
},
};
struct agx_bg_eot_shader *res =
rzalloc(cache->ht, struct agx_bg_eot_shader);
if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res)) {
agx_alloc_bg_eot_shader(cache, error);
if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res, error)) {
ralloc_free(res);
return NULL;
}
Expand All @@ -599,12 +629,13 @@ agx_build_background_shader(struct agx_bg_eot_cache *cache,
struct agx_shader_part bin = {0};
if (!agx_compile_background_shader(cache->dev, key, &bin))
return NULL;
return agx_upload_bg_eot_part(cache, &bin, false);
return agx_upload_bg_eot_part(cache, &bin, false, error);
}

static struct agx_bg_eot_shader *
agx_build_end_of_tile_shader(struct agx_bg_eot_cache *cache,
struct agx_bg_eot_key *key)
struct agx_bg_eot_key *key,
enum agx_bg_eot_error *error)
{
const bool g18g = cache->dev->chip == AGX_CHIP_G18G;
const unsigned g18_mrt = cache->dev->chip == AGX_CHIP_G18G
Expand Down Expand Up @@ -726,8 +757,8 @@ agx_build_end_of_tile_shader(struct agx_bg_eot_cache *cache,
.agx3_staging_halfs = agx3_m5_mrt_eot_staging_halfs(m5_stores),
},
};
struct agx_bg_eot_shader *res = rzalloc(cache->ht, struct agx_bg_eot_shader);
if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res)) {
struct agx_bg_eot_shader *res = agx_alloc_bg_eot_shader(cache, error);
if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res, error)) {
ralloc_free(res);
return NULL;
}
Expand Down Expand Up @@ -760,8 +791,8 @@ agx_build_end_of_tile_shader(struct agx_bg_eot_cache *cache,
.agx3_staging_halfs = native.staging_halfs,
},
};
struct agx_bg_eot_shader *res = rzalloc(cache->ht, struct agx_bg_eot_shader);
if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res)) {
struct agx_bg_eot_shader *res = agx_alloc_bg_eot_shader(cache, error);
if (!res || !agx_g17p_upload_bg_eot(cache, &bin, res, error)) {
ralloc_free(res);
return NULL;
}
Expand Down Expand Up @@ -961,12 +992,13 @@ agx_build_end_of_tile_shader(struct agx_bg_eot_cache *cache,
agx_chip_native_agx3(cache->dev->chip) &&
agx_g17p_classify_mrt(&key->tib) == AGX_G17P_MRT_R11G11B10;

return agx_compile_bg_eot_shader(cache, b.shader, &compiler_key, NULL);
return agx_compile_bg_eot_shader(cache, b.shader, &compiler_key, NULL, error);
}

static struct agx_bg_eot_shader *
agx_get_bg_eot_shader_locked(struct agx_bg_eot_cache *cache,
struct agx_bg_eot_key *key)
struct agx_bg_eot_key *key,
enum agx_bg_eot_error *error)
{
struct hash_entry *ent = _mesa_hash_table_search(cache->ht, key);
if (ent)
Expand All @@ -978,13 +1010,13 @@ agx_get_bg_eot_shader_locked(struct agx_bg_eot_cache *cache,
for (unsigned rt = 0; rt < ARRAY_SIZE(key->op); ++rt) {
if (key->op[rt] == AGX_EOT_STORE) {
has_store = true;
ret = agx_build_end_of_tile_shader(cache, key);
ret = agx_build_end_of_tile_shader(cache, key, error);
break;
}
}

if (!ret && !has_store)
ret = agx_build_background_shader(cache, key);
ret = agx_build_background_shader(cache, key, error);

if (!ret)
return NULL;
Expand All @@ -1002,9 +1034,21 @@ agx_get_bg_eot_shader_locked(struct agx_bg_eot_cache *cache,
struct agx_bg_eot_shader *
agx_get_bg_eot_shader(struct agx_bg_eot_cache *cache,
struct agx_bg_eot_key *key)
{
return agx_get_bg_eot_shader_checked(cache, key, NULL);
}

struct agx_bg_eot_shader *
agx_get_bg_eot_shader_checked(struct agx_bg_eot_cache *cache,
struct agx_bg_eot_key *key,
enum agx_bg_eot_error *error)
{
simple_mtx_lock(&cache->lock);
struct agx_bg_eot_shader *ret = agx_get_bg_eot_shader_locked(cache, key);
enum agx_bg_eot_error lookup_error = AGX_BG_EOT_NO_ERROR;
struct agx_bg_eot_shader *ret =
agx_get_bg_eot_shader_locked(cache, key, &lookup_error);
if (error)
*error = lookup_error;
simple_mtx_unlock(&cache->lock);
return ret;
}
Expand Down
11 changes: 11 additions & 0 deletions src/asahi/lib/agx_bg_eot.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ struct agx_precompiled_shader {
struct agx_precompiled_kernel_info info;
};

enum agx_bg_eot_error {
AGX_BG_EOT_NO_ERROR,
AGX_BG_EOT_HOST_OOM,
AGX_BG_EOT_DEVICE_OOM,
};

struct agx_bg_eot_cache {
struct agx_device *dev;
struct agx_pool pool;
Expand Down Expand Up @@ -202,6 +208,11 @@ agx_g17g_rt0_eot_physical_format_supported(enum pipe_format physical)

struct agx_shader_part;

struct agx_bg_eot_shader *
agx_get_bg_eot_shader_checked(struct agx_bg_eot_cache *cache,
struct agx_bg_eot_key *key,
enum agx_bg_eot_error *error);

struct agx_bg_eot_shader *agx_get_bg_eot_shader(struct agx_bg_eot_cache *cache,
struct agx_bg_eot_key *key);

Expand Down
46 changes: 31 additions & 15 deletions src/asahi/lib/agx_scratch.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,9 @@ agx_scratch_get_bucket(uint32_t dwords)
1 + util_logbase2_ceil(DIV_ROUND_UP(dwords, AGX_SPILL_UNIT_DWORDS)));
}

static void
static bool
agx_scratch_realloc(struct agx_scratch *scratch)
{
if (scratch->buf)
agx_bo_unreference(scratch->dev, scratch->buf);

struct spill_size size = agx_scratch_get_spill_size(scratch->size_dwords);

if (scratch->dev->debug & AGX_DBG_SCRATCH)
Expand Down Expand Up @@ -111,25 +108,32 @@ agx_scratch_realloc(struct agx_scratch *scratch)
#ifdef SCRATCH_DEBUG
flags = AGX_BO_WRITEBACK;
#endif
scratch->buf = agx_bo_create(scratch->dev, total_alloc, block_size_bytes,
flags, "Scratch");
void *map = agx_bo_map(scratch->buf);
struct agx_bo *buf = agx_bo_create(scratch->dev, total_alloc,
block_size_bytes, flags, "Scratch");
if (!buf)
return false;

void *map = agx_bo_map(buf);
if (!map) {
agx_bo_unreference(scratch->dev, buf);
return false;
}

memset(map, 0, blocks_off);

struct agx_helper_header *hdr = map;
scratch->header = hdr;

uint64_t blocklist_gpu = scratch->buf->va->addr + blocklist_off;
uint64_t blocklist_gpu = buf->va->addr + blocklist_off;
struct agx_helper_block *blocklist_cpu =
(struct agx_helper_block *)((uint8_t *)map + blocklist_off);

#ifdef SCRATCH_DEBUG
scratch->blocklist = blocklist_cpu;
scratch->data = scratch->buf->map + blocks_off;
scratch->data = (uint8_t *)map + blocks_off;
scratch->core_size = block_size_bytes * block_count * scratch->subgroups;
#endif

uint64_t blocks_gpu = scratch->buf->va->addr + blocks_off;
uint64_t blocks_gpu = buf->va->addr + blocks_off;

hdr->subgroups = scratch->subgroups;

Expand Down Expand Up @@ -174,23 +178,31 @@ agx_scratch_realloc(struct agx_scratch *scratch)
blocklist_gpu += sizeof(struct agx_helper_block) * scratch->subgroups;
blocklist_cpu += scratch->subgroups;
}
agx_bo_unreference(scratch->dev, scratch->buf);
scratch->buf = buf;
scratch->header = hdr;
scratch->max_core_id = core_id;
assert(num_cores == scratch->num_cores);
(void)num_cores;

if (scratch->dev->debug & AGX_DBG_SCRATCH)
fprintf(stderr, "New Scratch @ 0x%" PRIx64 " (size: 0x%zx)\n",
scratch->buf->va->addr, scratch->buf->size);
return true;
}

void
/* False when the larger buffer could not be allocated: the old one stays, too
* small for the caller's shader. */
bool
agx_scratch_alloc(struct agx_scratch *scratch, unsigned dwords,
size_t subgroups)
{
bool realloc = false;
const uint32_t old_dwords = scratch->size_dwords;
const uint32_t old_subgroups = scratch->subgroups;

if (!dwords)
return;
return true;

assert(dwords <= AGX_MAX_SCRATCH_DWORDS && "Scratch size too large");

Expand All @@ -209,9 +221,13 @@ agx_scratch_alloc(struct agx_scratch *scratch, unsigned dwords,
realloc = true;
}

if (realloc) {
agx_scratch_realloc(scratch);
if (realloc && !agx_scratch_realloc(scratch)) {
scratch->size_dwords = old_dwords;
scratch->subgroups = old_subgroups;
return false;
}

return true;
}

void
Expand Down
2 changes: 1 addition & 1 deletion src/asahi/lib/agx_scratch.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ void agx_scratch_debug_pre(struct agx_scratch *scratch);
void agx_scratch_debug_post(struct agx_scratch *scratch);

uint32_t agx_scratch_get_bucket(uint32_t dwords);
void agx_scratch_alloc(struct agx_scratch *scratch, uint32_t dwords,
bool agx_scratch_alloc(struct agx_scratch *scratch, uint32_t dwords,
size_t subgroups);
Loading