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
100 changes: 6 additions & 94 deletions src/cpufreq.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <string.h>

#include "cpufreq.h"
#include "adt.h"
#include "firmware.h"
#include "pmgr.h"
#include "soc.h"
#include "utils.h"

#define CLUSTER_PSTATE 0x20020
#define CLUSTER_PSTATE_STATUS 0x20050
#define CLUSTER_CPM 0x40000
#define CLUSTER_PSTATE 0x20020
#define CLUSTER_CPM 0x40000

#define CLUSTER_PSTATE_FIXED_FREQ_PLL_RECLOCK BIT(42)
#define CLUSTER_PSTATE_BUSY BIT(31)
Expand All @@ -25,9 +22,6 @@
#define CLUSTER_PSTATE_DESIRED1 GENMASK(4, 0)
#define CLUSTER_PSTATE_DESIRED1_S5L8960X GENMASK(24, 22)

#define CLUSTER_PSTATE_STATUS_CURRENT_T8132 GENMASK(9, 5)
#define CLUSTER_PSTATE_STATUS_TARGET_T8132 GENMASK(4, 0)

#define PMGR_VOLTAGE_CTL_OFF_S5L8960X 0x20c00
#define PMGR_VOLTAGE_CTL_OFF_T7000 0x23000
#define PMGR_VOLTAGE_CTL_OFF_S8000 0xa0000
Expand Down Expand Up @@ -85,49 +79,8 @@ static u32 pstate_reg_to_pstate(u64 val)
}
}

static int set_pstate_t8132(const struct cluster_t *cluster, uint32_t pstate)
{
u64 command = cluster->base + CLUSTER_PSTATE;
u64 status = cluster->base + CLUSTER_PSTATE_STATUS;
u64 value;

/*
* T8132 uses Linux apple-soc-cpufreq's T8112 layout: DVFS +0x20 is
* the 64-bit command, +0x50 is 32-bit status. Wait for command bit 31
* BEFORE submitting a request; current status need not reach the request.
*/
printf("cpufreq: T8132 %s request P%u: poll64 @0x%lx bit31=0 before write "
"(2us, %uus); status32 @0x%lx\n",
cluster->name, pstate, command, CLUSTER_SWITCH_TIMEOUT, status);
for (u32 elapsed = 0;; elapsed += 2) {
value = read64(command);
if (!(value & CLUSTER_PSTATE_BUSY))
break;
if (elapsed == CLUSTER_SWITCH_TIMEOUT) {
printf("cpufreq: T8132 %s command busy timeout @0x%lx\n", cluster->name, command);
return -1;
}
udelay(2);
}

value &= ~CLUSTER_PSTATE_DESIRED1;
value |= CLUSTER_PSTATE_SET | FIELD_PREP(CLUSTER_PSTATE_DESIRED1, pstate);
printf("cpufreq: T8132 %s write command @0x%lx = 0x%lx\n", cluster->name, command, value);
write64(command, value);

/* Observe once; Linux does not wait for actual frequency after the write. */
u32 observed = read32(status);
printf("cpufreq: T8132 %s status32 @0x%lx = 0x%x (current P%u target P%u)\n", cluster->name,
status, observed, (u32)FIELD_GET(CLUSTER_PSTATE_STATUS_CURRENT_T8132, observed),
(u32)FIELD_GET(CLUSTER_PSTATE_STATUS_TARGET_T8132, observed));
return 0;
}

static int set_pstate(const struct cluster_t *cluster, uint32_t pstate)
{
if (chip_id == T8132)
return set_pstate_t8132(cluster, pstate);

u64 val = read64(cluster->base + CLUSTER_PSTATE);

if (pstate_reg_to_pstate(val) != pstate) {
Expand Down Expand Up @@ -160,6 +113,7 @@ static int set_pstate(const struct cluster_t *cluster, uint32_t pstate)
case T6031:
case T6034:
case T8122:
case T8132:
val &= ~CLUSTER_PSTATE_DESIRED1;
val |= CLUSTER_PSTATE_SET | FIELD_PREP(CLUSTER_PSTATE_DESIRED1, pstate);
break;
Expand Down Expand Up @@ -191,20 +145,9 @@ int cpufreq_init_cluster(const struct cluster_t *cluster, const struct feat_t *f
if (features->pcluster_only && !cluster->pcluster)
continue;

u32 enabled = pmgr_get_feature(features->name);
/* Disabled T8132 throttle gates can fault even on a diagnostic read. */
if (chip_id == T8132 && !enabled &&
(!strcmp(features->name, "ppt-thrtl") || !strcmp(features->name, "llc-thrtl") ||
!strcmp(features->name, "amx-thrtl"))) {
printf("cpufreq: T8132 %s %s=0 skipped (ADT disabled; no MMIO)\n", cluster->name,
features->name);
continue;
}

u64 reg = cluster->base + features->offset;
u64 before = chip_id == T8132 ? read64(reg) : 0;

if (enabled)
if (pmgr_get_feature(features->name))
mask64(reg, features->clear, features->set);
else
mask64(reg, features->set, features->clear);
Expand All @@ -214,19 +157,6 @@ int cpufreq_init_cluster(const struct cluster_t *cluster, const struct feat_t *f
cluster->name);
return -1;
}

if (chip_id == T8132) {
u64 after = read64(reg);
u64 mask = features->clear | features->set;
u64 expected = enabled ? features->set : features->clear;

printf("cpufreq: T8132 %s %s=%u @0x%lx: 0x%lx -> 0x%lx\n", cluster->name,
features->name, enabled, reg, before, after);
if ((after & mask) != expected) {
printf("cpufreq: T8132 %s feature readback mismatch\n", cluster->name);
return -1;
}
}
}

int pmgr_path[8];
Expand Down Expand Up @@ -285,7 +215,7 @@ int cpufreq_init_cluster(const struct cluster_t *cluster, const struct feat_t *f
write64(cluster->base + 0x440f8, 1);
break;
case T8132:
/* No ADT feature justifies the older SoCs' unknown +0x440f8 write. */
/* Write to 0x440f8 serrors in raw boot mode */
break;
default:
printf("cpufreq: Chip 0x%x is unsupported\n", chip_id);
Expand Down Expand Up @@ -313,10 +243,6 @@ int cpufreq_init_cluster(const struct cluster_t *cluster, const struct feat_t *f
if (cluster->default_pstate && set_pstate(cluster, cluster->default_pstate))
return -1;

if (chip_id == T8132)
printf("cpufreq: T8132 %s initialized, base=0x%lx command=0x%lx\n", cluster->name,
cluster->base, read64(cluster->base + CLUSTER_PSTATE));

return 0;
}

Expand Down Expand Up @@ -452,7 +378,7 @@ static const struct cluster_t t6030_clusters[] = {
{},
};

/* The boot states follow T6030; both J713 and J773g have these ADT OPPs. */
/* Read bases from ADT */
static struct cluster_t t8132_clusters[] = {
{"ECPU0", 0, false, 1, 5},
{"PCPU0", 0, true, 1, 6},
Expand Down Expand Up @@ -635,19 +561,8 @@ static const struct feat_t t6030_features[] = {
{},
};

/*
* T6030's CPU-complex layout is the template for T8132. J713/J773g advertise
* cpu-apsc, unlike the T8122 template, so retain the M2/M3 APSC enable handling.
* Access throttle gates only when ADT-enabled: disabled P-cluster PPT access
* faults on J713, even though the corresponding E-cluster access completes.
* This does not establish the M4 firmware's thermal thresholds or alarm action.
*/
static const struct feat_t t8132_features[] = {
{"cpu-apsc", CLUSTER_PSTATE, CLUSTER_PSTATE_M2_APSC_DIS, 0, CLUSTER_PSTATE_APSC_BUSY, false},
{"ppt-thrtl", 0x48400, 0, BIT(63), 0, false},
{"ppt-thrtl", 0x48408, 0, BIT(63), 0, false},
{"llc-thrtl", 0x40270, 0, BIT(63), 0, false},
{"amx-thrtl", 0x40250, 0, BIT(63), 0, false},
{"cpu-fixed-freq-pll-relock", CLUSTER_PSTATE, 0, CLUSTER_PSTATE_FIXED_FREQ_PLL_RECLOCK, 0,
false},
{},
Expand Down Expand Up @@ -714,9 +629,6 @@ int cpufreq_init(void)
err |= cpufreq_init_cluster(cluster++, features);
}

if (chip_id == T8132)
printf("cpufreq: T8132 initialization %s\n", err ? "FAILED" : "complete");

return err ? -1 : 0;
}

Expand Down
85 changes: 61 additions & 24 deletions src/mcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,30 +456,15 @@ int mcc_init_m3(int node, int *path)
{
u32 reg_len;
u32 reg_offset = 3;
bool t8132 = adt_is_compatible(adt, node, "mcc,t8132");

if (!adt_getprop(adt, node, "reg", &reg_len)) {
printf("MCC: Failed to get reg property!\n");
return -1;
}

if (t8132) {
u32 count;

/* T8132 places SOCNI before the AMCC apertures and PMP/MCW after them. */
if (ADT_GETPROP(adt, node, "amcc_aperture_reg_idx", &reg_offset) < 0 ||
ADT_GETPROP(adt, node, "amcc_aperture_count", &count) < 0 || !count ||
reg_len % 16 || reg_offset >= reg_len / 16 || count > reg_len / 16 - reg_offset) {
printf("MCC: Invalid T8132 AMCC aperture layout!\n");
return -1;
}

mcc_count = count;
} else {
mcc_count = reg_len / 16 - reg_offset;
}
mcc_count = reg_len / 16 - reg_offset;

printf("MCC: Initializing M3/M4 MCCs (%d instances)...\n", mcc_count);
printf("MCC: Initializing M3* MCCs (%d instances)...\n", mcc_count);

if (mcc_count > MAX_MCC_INSTANCES) {
printf("MCC: Too many instances, increase MAX_MCC_INSTANCES!\n");
Expand All @@ -489,22 +474,19 @@ int mcc_init_m3(int node, int *path)
u32 plane_count = 0;
u32 dcs_count = 0;

if (ADT_GETPROP(adt, node, "dcs-count-per-amcc", &dcs_count) < 0 || !dcs_count) {
if (!ADT_GETPROP(adt, node, "dcs-count-per-amcc", &dcs_count)) {
printf("MCC: Failed to get dcs count!\n");
return -1;
}

if (ADT_GETPROP(adt, node, "plane-count-per-amcc", &plane_count) < 0 || !plane_count) {
if (!ADT_GETPROP(adt, node, "plane-count-per-amcc", &plane_count)) {
printf("MCC: Failed to get plane count!\n");
return -1;
}

int ret = -1;
if (adt_is_compatible(adt, node, "mcc,t8122"))
ret = mcc_init_t8122(path, reg_offset, plane_count, dcs_count, &t8122_tz_regs);
else if (t8132)
/* T8132's iBoot TZ bounds match the ADT carveouts with the T6030 layout. */
ret = mcc_init_t8122(path, reg_offset, plane_count, dcs_count, &t6030_tz_regs);
else if (adt_is_compatible(adt, node, "mcc,t6030"))
ret = mcc_init_t8122(path, reg_offset, plane_count, dcs_count, &t6030_tz_regs);
else if (adt_is_compatible(adt, node, "mcc,t6031"))
Expand All @@ -515,7 +497,62 @@ int mcc_init_m3(int node, int *path)
if (ret)
return ret;

printf("MCC: Initialized M3/M4 MCCs (%d instances, %d planes, %d channels)\n", mcc_count,
printf("MCC: Initialized M3* MCCs (%d instances, %d planes, %d channels)\n", mcc_count,
mcc_regs[0].plane_count, mcc_regs[0].dcs_count);

mcc_initialized = true;

return 0;
}

int mcc_init_m4(int node, int *path)
{
u64 amcc_count = 0;
u32 count_len;
const void *count = adt_getprop(adt, node, "amcc_aperture_count", &count_len);
if (!count)
count = adt_getprop(adt, node, "amcc-count", &count_len);
if (!count || (count_len != sizeof(u32) && count_len != sizeof(u64))) {
printf("MCC: Failed to get mcc count!\n");
return -1;
}
/* T8132 uses a 32-bit aperture count; other M4 ADTs use a 64-bit count. */
memcpy(&amcc_count, count, count_len);

if (amcc_count > MAX_MCC_INSTANCES) {
printf("MCC: Too many instances, increase MAX_MCC_INSTANCES!\n");
mcc_count = MAX_MCC_INSTANCES;
} else {
mcc_count = amcc_count;
}

u32 plane_count = 0;
u32 dcs_count = 0;

if (ADT_GETPROP(adt, node, "dcs-count-per-amcc", &dcs_count) != sizeof(dcs_count)) {
printf("MCC: Failed to get dcs count!\n");
return -1;
}

if (ADT_GETPROP(adt, node, "plane-count-per-amcc", &plane_count) != sizeof(plane_count)) {
printf("MCC: Failed to get plane count!\n");
return -1;
}

printf("MCC: Initializing T%x MCCs (%d instances)...\n", chip_id, mcc_count);

int ret = -1;
if (adt_is_compatible(adt, node, "mcc,t8132")) {
int reg_offset = 7;
ret = mcc_init_t8122(path, reg_offset, plane_count, dcs_count, &t6030_tz_regs);
} else {
printf("MCC: Unsupported version:%s\n", adt_get_property(adt, node, "compatible")->value);
}

if (ret)
return ret;

printf("MCC: Initialized T%x MCCs (%d instances, %d planes, %d channels)\n", chip_id, mcc_count,
mcc_regs[0].plane_count, mcc_regs[0].dcs_count);

mcc_initialized = true;
Expand Down Expand Up @@ -609,7 +646,7 @@ int mcc_init(void)
} else if (adt_is_compatible(adt, node, "mcc,t8122")) {
return mcc_init_m3(node, path);
} else if (adt_is_compatible(adt, node, "mcc,t8132")) {
return mcc_init_m3(node, path);
return mcc_init_m4(node, path);
} else if (adt_is_compatible(adt, node, "mcc,t6030")) {
return mcc_init_m3(node, path);
} else if (adt_is_compatible(adt, node, "mcc,t6031")) {
Expand Down
Loading
Loading