Skip to content
Merged
134 changes: 121 additions & 13 deletions src/kboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -3382,6 +3382,13 @@ static int dt_append_acio_tunable(int adt_node, int fdt_node,
}

u32 n_tunables = tunables_len / sizeof(*tunable_adt);
if (!n_tunables)
return 0;

fdt32_t *cells = malloc(n_tunables * 3 * sizeof(*cells));
if (!cells)
return -1;

for (size_t j = 0; j < n_tunables; j++) {
const struct acio_tunable *tunable = &tunable_adt[j];

Expand All @@ -3394,35 +3401,41 @@ static int dt_append_acio_tunable(int adt_node, int fdt_node,

if (tunable->size != 4) {
printf("kboot: ACIO tunable has invalid size %d\n", tunable->size);
free(cells);
return -1;
}

if (tunable->offset % tunable->size) {
printf("kboot: ACIO tunable has unaligned offset %x\n", tunable->offset);
free(cells);
return -1;
}

if (tunable_info->reg_size < tunable->size ||
tunable->offset > tunable_info->reg_size - tunable->size ||
tunable_info->reg_offset > UINT32_MAX - tunable->offset) {
printf("kboot: ACIO tunable has invalid offset %x\n", tunable->offset);
free(cells);
return -1;
}

if (tunable->mask > UINT32_MAX || tunable->value > UINT32_MAX) {
printf("kboot: ACIO tunable does not fit a 32-bit register\n");
free(cells);
return -1;
}
if (fdt_appendprop_u32(dt, fdt_node, tunable_info->fdt_name,
tunable->offset + tunable_info->reg_offset) < 0)
return -1;
if (fdt_appendprop_u32(dt, fdt_node, tunable_info->fdt_name, tunable->mask) < 0)
return -1;
if (fdt_appendprop_u32(dt, fdt_node, tunable_info->fdt_name, tunable->value) < 0)
return -1;

cells[3 * j] = cpu_to_fdt32(tunable->offset + tunable_info->reg_offset);
cells[3 * j + 1] = cpu_to_fdt32(tunable->mask);
cells[3 * j + 2] = cpu_to_fdt32(tunable->value);
}

return 0;
int ret = 0;
if (n_tunables)
ret = fdt_appendprop(dt, fdt_node, tunable_info->fdt_name, cells,
n_tunables * 3 * sizeof(*cells));
free(cells);
return ret < 0 ? -1 : 0;
}

static int dt_append_dart_tunable(int adt_node, int fdt_node,
Expand Down Expand Up @@ -3459,6 +3472,19 @@ static bool dt_node_is_apple_dart(int node)
return false;
}

struct dt_dart_entry {
u64 address;
int node;
bool duplicate;
};

struct dt_dart_index {
struct dt_dart_entry *entries;
size_t count;
size_t cap;
bool valid;
};

/* A physical register window is the only stable join key between the two trees. */
static int dt_find_dart_by_address(u64 address)
{
Expand Down Expand Up @@ -3499,15 +3525,84 @@ static u64 dt_dart_window(int dart)
return ((u64)fdt32_ld(&reg[ac]) << 32) | fdt32_ld(&reg[ac + 1]);
}

static int dt_copy_dart_tunable(int adt_node, const char *adt_name, u64 address)
static void dt_dart_index_init(struct dt_dart_index *idx)
{
memset(idx, 0, sizeof(*idx));
idx->valid = true;

for (int node = fdt_next_node(dt, -1, NULL); node >= 0; node = fdt_next_node(dt, node, NULL)) {
const char *status;

if (!dt_node_is_apple_dart(node))
continue;
status = fdt_getprop(dt, node, "status", NULL);
if (status && !strcmp(status, "disabled"))
continue;

u64 address = dt_get_address(dt, node);
bool matched = false;
for (size_t i = 0; i < idx->count; i++) {
if (idx->entries[i].address == address) {
idx->entries[i].duplicate = true;
matched = true;
break;
}
}
if (matched)
continue;

if (idx->count == idx->cap) {
size_t new_cap = idx->cap ? idx->cap * 2 : 64;
struct dt_dart_entry *grown = realloc(idx->entries, new_cap * sizeof(*grown));
if (!grown) {
free(idx->entries);
memset(idx, 0, sizeof(*idx));
return;
}
idx->entries = grown;
idx->cap = new_cap;
}
idx->entries[idx->count++] = (struct dt_dart_entry){
.address = address,
.node = node,
.duplicate = false,
};
}
}

static int dt_dart_index_find(const struct dt_dart_index *idx, u64 address)
{
if (!idx || !idx->valid)
return dt_find_dart_by_address(address);

for (size_t i = 0; i < idx->count; i++) {
if (idx->entries[i].address == address)
return idx->entries[i].duplicate ? -FDT_ERR_EXISTS : idx->entries[i].node;
}
return -FDT_ERR_NOTFOUND;
}

static void dt_dart_index_adjust(struct dt_dart_index *idx, int edited_node, int delta)
{
if (!idx || !idx->valid || !delta)
return;

for (size_t i = 0; i < idx->count; i++) {
if (idx->entries[i].node > edited_node)
idx->entries[i].node += delta;
}
}

static int dt_copy_dart_tunable(struct dt_dart_index *idx, int adt_node, const char *adt_name,
u64 address)
{
struct adt_tunable_info tunable = {
.adt_name = adt_name,
.fdt_name = "apple,tunable",
.reg_size = 0x4000,
.required = true,
};
int dart = dt_find_dart_by_address(address);
int dart = dt_dart_index_find(idx, address);

if (dart < 0) {
printf("FDT: no unique enabled DART at 0x%lx for ADT %s\n", address, adt_name);
Expand All @@ -3522,12 +3617,17 @@ static int dt_copy_dart_tunable(int adt_node, const char *adt_name, u64 address)
u64 window = dt_dart_window(dart);
if (window)
tunable.reg_size = window;
if (dt_append_dart_tunable(adt_node, dart, &tunable)) {

u32 before = fdt_size_dt_struct(dt);
int ret = dt_append_dart_tunable(adt_node, dart, &tunable);
if (ret) {
/* The converter can have appended valid rows before rejecting one. */
fdt_delprop(dt, dart, tunable.fdt_name);
dt_dart_index_adjust(idx, dart, (int)fdt_size_dt_struct(dt) - (int)before);
printf("FDT: rejected ADT DART table %s at 0x%lx\n", adt_name, address);
return -1;
}
dt_dart_index_adjust(idx, dart, (int)fdt_size_dt_struct(dt) - (int)before);

printf("FDT: copied ADT DART table %s to 0x%lx\n", adt_name, address);
return 0;
Expand Down Expand Up @@ -3560,6 +3660,9 @@ static void dt_copy_dart_tunables(const char *const *only)
}
path[depth + 1] = 0;

struct dt_dart_index dart_idx;
dt_dart_index_init(&dart_idx);

int node = arm_io;
ADT_FOREACH_CHILD(adt, node)
{
Expand Down Expand Up @@ -3591,23 +3694,28 @@ static void dt_copy_dart_tunables(const char *const *only)
* writes. The old PCIe-C-only path emitted this marker too.
*/
if (!instance && !strncmp(name, "dart-apciec", 11)) {
int dart = dt_find_dart_by_address(address);
int dart = dt_dart_index_find(&dart_idx, address);

if (dart >= 0 && !fdt_getprop(dt, dart, "apple,tunable", NULL)) {
u32 before = fdt_size_dt_struct(dt);
if (fdt_setprop_empty(dt, dart, "apple,tunable") < 0)
printf("FDT: could not mark empty PCIe-C DART table at 0x%lx\n",
address);
else
printf("FDT: ADT PCIe-C DART at 0x%lx requests no tunables\n", address);
dt_dart_index_adjust(&dart_idx, dart,
(int)fdt_size_dt_struct(dt) - (int)before);
}
}
continue;
}

/* Fail soft and continue with every other DART instance. */
dt_copy_dart_tunable(node, prop, address);
dt_copy_dart_tunable(&dart_idx, node, prop, address);
}
}

free(dart_idx.entries);
}

static u8 crc8(u8 *b, size_t len)
Expand Down
47 changes: 30 additions & 17 deletions src/kboot_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,12 @@ static int dt_amp_by_addr(const void *dt, u32 addr)
return found;
}

/* Set @name on the DT amp at @addr to @val (remove it when @val is NULL) */
static int dt_amp_setprop(void *dt, u32 addr, const char *name, const void *val, u32 len)
/* Set @name on the DT amp node @node (at I2C @addr) to @val (remove when @val is NULL) */
static int dt_amp_node_setprop(void *dt, int node, u32 addr, const char *name, const void *val,
u32 len)
{
/* Offsets move with every change to the tree: look the amp up each time */
int node = dt_amp_by_addr(dt, addr);
int ret;

if (node < 0)
return -1;
if (val)
ret = fdt_setprop(dt, node, name, val, len);
else
Expand Down Expand Up @@ -176,17 +173,14 @@ static int setup_amp_group(void *dt, const void *adt, int arm_io, int group)
}
}

const void *group_vals[ARRAY_SIZE(amp_group_props)];
u32 group_lens[ARRAY_SIZE(amp_group_props)];
for (size_t p = 0; p < ARRAY_SIZE(amp_group_props); p++) {
u32 len = 0;
const void *val = adt_getprop(adt, group, amp_group_props[p].adt, &len);

for (int i = 0; i < n; i++) {
if (dt_amp_setprop(dt, addrs[i], amp_group_props[p].dt, val, len))
return -1;
}
group_lens[p] = 0;
group_vals[p] = adt_getprop(adt, group, amp_group_props[p].adt, &group_lens[p]);
}

/* One row per amp, in the group's order */
const u8 *row_vals[ARRAY_SIZE(amp_row_props)];
for (size_t p = 0; p < ARRAY_SIZE(amp_row_props); p++) {
u32 len = 0;
u32 row = amp_row_props[p].row;
Expand All @@ -197,9 +191,28 @@ static int setup_amp_group(void *dt, const void *adt, int arm_io, int group)
amp_row_props[p].adt, len, n);
rows = NULL;
}
for (int i = 0; i < n; i++) {
if (dt_amp_setprop(dt, addrs[i], amp_row_props[p].dt, rows ? rows + i * row : NULL,
row))
row_vals[p] = rows;
}

/*
* Modifying properties of @node only shifts bytes after @node, so one
* lookup per amp suffices for all of its properties.
*/
for (int i = 0; i < n; i++) {
int node = dt_amp_by_addr(dt, addrs[i]);

if (node < 0)
return -1;
for (size_t p = 0; p < ARRAY_SIZE(amp_group_props); p++) {
if (dt_amp_node_setprop(dt, node, addrs[i], amp_group_props[p].dt, group_vals[p],
group_lens[p]))
return -1;
}
for (size_t p = 0; p < ARRAY_SIZE(amp_row_props); p++) {
u32 row = amp_row_props[p].row;

if (dt_amp_node_setprop(dt, node, addrs[i], amp_row_props[p].dt,
row_vals[p] ? row_vals[p] + i * row : NULL, row))
return -1;
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/kboot_display_handoff.c
Original file line number Diff line number Diff line change
Expand Up @@ -2556,22 +2556,28 @@ static int dcp6_publish(void *dt, const struct dcp6_dt *n)

const struct dcp6_run *r;
const u32 owner_ph[3] = {ph_dcp, ph_disp, ph_pio};
fdt32_t iommu_tuples[3 * 5];
int ntuples = 0;
for (int k = 0; k < 3; k++) {
if (k == 0 && i == dcp6.seg_oslog)
r = NULL; /* physical buffer: memory-region entry only, no DART address */
else if (!(r = dcp6_run_of(k, i)))
continue;
if (r) {
node = fdt_subnode_offset(dt, fdt_path_offset(dt, "/reserved-memory"), name);
if (node < 0 ||
dcp6_append_iommu_addr(dt, node, owner_ph[k], alias_base[k], r->dva, r->size))
return -1;
fdt32_t *tuple = &iommu_tuples[ntuples * 5];
tuple[0] = cpu_to_fdt32(owner_ph[k]);
fdt64_st(&tuple[1], alias_base[k] | r->dva);
fdt64_st(&tuple[3], r->size);
ntuples++;
}
size_t nl = strlen(d->name) + 1;
mr[k][count[k]++] = cpu_to_fdt32(ph);
memcpy(names[k] + names_len[k], d->name, nl);
names_len[k] += nl;
}
if (ntuples && fdt_appendprop(dt, node, "iommu-addresses", iommu_tuples,
ntuples * 5 * sizeof(fdt32_t)))
return -1;
}

if (dcp6_set_list(dt, n->dcp, mr[0], count[0], names[0], names_len[0]) ||
Expand Down
Loading
Loading