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
114 changes: 114 additions & 0 deletions src/asahi/lib/agx_chip_caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2026 Aurora Silicon
* SPDX-License-Identifier: MIT
*/

#pragma once

#include "agx_chip.h"

/*
* Hardware capabilities, one bit per feature a driver may use.
*
* The chip table in agx_chip.h says what a chip is: its instruction set,
* encodings, command-stream and descriptor generations, and command path.
* This table says what it can do. A capability is a positive property of the
* hardware, measured on the chips whose row carries it; a call site asks for
* the capability it needs and never for a chip or a set of chips. Hardware
* defects that need a workaround are not capabilities and stay in
* enum agx_chip_quirk.
*
* Each capability is first introduced with exactly the chips of the
* predicate it replaces, and tests/test-chip-caps.c checks the two agree on
* every chip. Enabling a capability on another chip is then a change to that
* chip's row, made together with the hardware evidence for it.
*/
enum agx_chip_cap {
/* The 24-byte mode-2 indirect compute launch and its grid helper
* (agx_chip_native_indirect_compute). */
AGX_CAP_NATIVE_INDIRECT_COMPUTE = 1u << 0,

/* The Apple9 imageblock: up to 128 bytes a sample in a 32 KiB tile
* (agx_chip_native_imageblock). */
AGX_CAP_NATIVE_IMAGEBLOCK = 1u << 1,

/* Native shaders may carry an Ioto constant program and a shader preamble
* (agx_chip_native_preamble). */
AGX_CAP_NATIVE_PREAMBLE = 1u << 2,

/* Consecutive application dispatches of one CDM stream may omit the
* ordering barrier between them (agx_chip_elide_app_dispatch_barrier). */
AGX_CAP_ELIDE_APP_DISPATCH_BARRIER = 1u << 3,

/* An indirect indexed VDM Index List reads indices past its index-buffer
* size word as zero (agx_chip_vdm_indirect_index_bounds). */
AGX_CAP_VDM_INDIRECT_INDEX_BOUNDS = 1u << 4,

/* The texture unit bounds integer texel fetches to the view and returns
* the robustImageAccess2 value (agx_chip_hw_texel_fetch_bounds). */
AGX_CAP_HW_TXF_BOUNDS = 1u << 5,

/* Native fragment programs may merge triangles across memory writes and
* wide subgroup operations (agx_chip_tri_merge_across_side_effects). */
AGX_CAP_TRI_MERGE_ACROSS_SIDE_EFFECTS = 1u << 6,

/* The matrix multiply-add backing a subgroup-scope cooperative matrix
* (agx_chip_cooperative_matrix_supported). */
AGX_CAP_COOPERATIVE_MATRIX = 1u << 7,
};

/* What the five native AGX3 chips (G15G, G15S, G15C, G16G, G17P) share. */
#define AGX_CAPS_NATIVE \
(AGX_CAP_NATIVE_INDIRECT_COMPUTE | AGX_CAP_NATIVE_IMAGEBLOCK | \
AGX_CAP_ELIDE_APP_DISPATCH_BARRIER | AGX_CAP_COOPERATIVE_MATRIX)

#define AGX_CAPS_G17P \
(AGX_CAPS_NATIVE | AGX_CAP_NATIVE_PREAMBLE | \
AGX_CAP_TRI_MERGE_ACROSS_SIDE_EFFECTS)

/* What the M5 parts (G17G, G17S, G17C) share. */
#define AGX_CAPS_M5 \
(AGX_CAP_ELIDE_APP_DISPATCH_BARRIER | AGX_CAP_VDM_INDIRECT_INDEX_BOUNDS | \
AGX_CAP_HW_TXF_BOUNDS | AGX_CAP_COOPERATIVE_MATRIX)

/*
* One row per chip of AGX_CHIPS, in the same order: chip, capabilities.
*
* G15D, G16P, G16S, G16C and G17A are recognised but not admitted; they keep
* the capabilities their encoding implies so the table answers as the
* predicates do.
*/
#define AGX_CHIP_CAPS(X, ...) \
X(G13G, 0, __VA_ARGS__) \
X(G13X, 0, __VA_ARGS__) \
X(G14G, 0, __VA_ARGS__) \
X(G14X, 0, __VA_ARGS__) \
X(G15P, 0, __VA_ARGS__) \
X(G15G, AGX_CAPS_NATIVE, __VA_ARGS__) \
X(G15S, AGX_CAPS_NATIVE, __VA_ARGS__) \
X(G15C, AGX_CAPS_NATIVE, __VA_ARGS__) \
X(G15D, AGX_CAP_COOPERATIVE_MATRIX, __VA_ARGS__) \
X(G16P, AGX_CAP_COOPERATIVE_MATRIX, __VA_ARGS__) \
X(G16G, AGX_CAPS_NATIVE, __VA_ARGS__) \
X(G16S, AGX_CAP_COOPERATIVE_MATRIX, __VA_ARGS__) \
X(G16C, AGX_CAP_COOPERATIVE_MATRIX, __VA_ARGS__) \
X(G17P, AGX_CAPS_G17P, __VA_ARGS__) \
X(G17A, AGX_CAP_COOPERATIVE_MATRIX, __VA_ARGS__) \
X(G17G, AGX_CAPS_M5, __VA_ARGS__) \
X(G17S, AGX_CAPS_M5, __VA_ARGS__) \
X(G17C, AGX_CAPS_M5, __VA_ARGS__) \
X(G18G, 0, __VA_ARGS__)

/* Like agx_chip_has_record(), a test of the table expands to comparisons, so
* the precompiled OpenCL kernels can call it and the C units run the same
* expression. */
#define AGX_CHIP_CAP_ROW_TEST(row, caps, chip, cap) \
| (((chip) == AGX_CHIP_##row) & (((caps) & (cap)) != 0))

/* Whether a chip has a capability (one enum agx_chip_cap bit). Unknown chips
* and values outside the enum have none. */
static inline bool
agx_chip_has_cap(enum agx_chip chip, unsigned cap)
{
return false AGX_CHIP_CAPS(AGX_CHIP_CAP_ROW_TEST, chip, cap);
}
12 changes: 12 additions & 0 deletions src/asahi/lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,18 @@ if with_tests
),
suite : ['asahi'],
)
test(
'asahi-agx-chip-caps',
executable(
'asahi-agx-chip-caps',
files('tests/test-chip-caps.c'),
include_directories : [inc_include, inc_src, inc_asahi],
link_with : [libasahi_lib],
c_args : [c_msvc_compat_args],
gnu_symbol_visibility : 'hidden',
),
suite : ['asahi'],
)

test(
'asahi-agx-g17g-tile-format-gate',
Expand Down
119 changes: 119 additions & 0 deletions src/asahi/lib/tests/test-chip-caps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2026 Aurora Silicon
* SPDX-License-Identifier: MIT
*/

#include "agx_chip_caps.h"

#include <stdio.h>

static unsigned failures;
static unsigned checks;

#define CHECK(condition) \
do { \
checks++; \
if (!(condition)) { \
fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #condition); \
failures++; \
} \
} while (0)

#define CHECK_CHIP(condition, chip) \
do { \
checks++; \
if (!(condition)) { \
fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, \
agx_chip_name(chip), #condition); \
failures++; \
} \
} while (0)

/* Every capability, with the agx_chip.h predicate it was introduced from. */
#define CAPS(X) \
X(AGX_CAP_NATIVE_INDIRECT_COMPUTE, agx_chip_native_indirect_compute) \
X(AGX_CAP_NATIVE_IMAGEBLOCK, agx_chip_native_imageblock) \
X(AGX_CAP_NATIVE_PREAMBLE, agx_chip_native_preamble) \
X(AGX_CAP_ELIDE_APP_DISPATCH_BARRIER, agx_chip_elide_app_dispatch_barrier) \
X(AGX_CAP_VDM_INDIRECT_INDEX_BOUNDS, agx_chip_vdm_indirect_index_bounds) \
X(AGX_CAP_HW_TXF_BOUNDS, agx_chip_hw_texel_fetch_bounds) \
X(AGX_CAP_TRI_MERGE_ACROSS_SIDE_EFFECTS, \
agx_chip_tri_merge_across_side_effects) \
X(AGX_CAP_COOPERATIVE_MATRIX, agx_chip_cooperative_matrix_supported)

#define CAP_BIT(cap, predicate) | (cap)
static const unsigned all_caps = 0 CAPS(CAP_BIT);
#undef CAP_BIT

/* Each chip of AGX_CHIPS has exactly one row, and the rows are in its
* order. */
#define ROW_COUNT(row, caps, chip) +((chip) == AGX_CHIP_##row)
#define ROW_CHIP(row, caps, ...) AGX_CHIP_##row,
#define ROW_CAPS(row, caps, ...) (caps),

static void
test_table_covers_every_chip(void)
{
static const enum agx_chip rows[] = {AGX_CHIP_CAPS(ROW_CHIP)};
static const unsigned row_caps[] = {AGX_CHIP_CAPS(ROW_CAPS)};

CHECK(sizeof(rows) / sizeof(rows[0]) == AGX_CHIP_UNKNOWN);

for (unsigned i = 0; i < sizeof(rows) / sizeof(rows[0]); ++i) {
CHECK(rows[i] == (enum agx_chip)i);
/* No row sets a bit outside enum agx_chip_cap. */
CHECK((row_caps[i] & ~all_caps) == 0);
}

for (unsigned i = 0; i < AGX_CHIP_UNKNOWN; ++i) {
const enum agx_chip c = (enum agx_chip)i;
CHECK_CHIP((0 AGX_CHIP_CAPS(ROW_COUNT, c)) == 1, c);
}
}

/* Every capability answers what its predicate answers, on every chip. */
static void
test_caps_match_predicates(void)
{
for (unsigned i = 0; i <= AGX_CHIP_UNKNOWN; ++i) {
const enum agx_chip c = (enum agx_chip)i;

#define CAP_MATCHES(cap, predicate) \
CHECK_CHIP(agx_chip_has_cap(c, cap) == (bool)predicate(c), c);
CAPS(CAP_MATCHES)
#undef CAP_MATCHES
}
}

/* Unknown chips and values outside the enum have no capability. */
static void
test_unknown_has_none(void)
{
const int outside[] = {-1, AGX_CHIP_UNKNOWN, AGX_CHIP_UNKNOWN + 1, 255};

for (unsigned i = 0; i < sizeof(outside) / sizeof(outside[0]); ++i) {
for (unsigned bit = 0; bit < 32; ++bit)
CHECK(!agx_chip_has_cap((enum agx_chip)outside[i], 1u << bit));
}

/* Asking for no capability is never true. */
for (unsigned i = 0; i <= AGX_CHIP_UNKNOWN; ++i)
CHECK(!agx_chip_has_cap((enum agx_chip)i, 0));
}

int
main(void)
{
test_table_covers_every_chip();
test_caps_match_predicates();
test_unknown_has_none();

if (failures) {
fprintf(stderr, "agx_chip_caps: %u of %u checks failed\n", failures,
checks);
return 1;
}

printf("agx_chip_caps: all %u checks passed\n", checks);
return 0;
}