/* * This register provides access to two counter values with a single * 64-bit read. The counter values are used to determine the average * actual frequency a core has run at over a period of time. * [63:32] PLLP counter: Counts at fixed frequency (408 MHz) * [31:0] Core clock counter: Counts on every core clock cycle
*/ staticvoid tegra234_read_counters(struct tegra_cpu_ctr *c)
{ struct tegra194_cpufreq_data *data = cpufreq_get_driver_data(); void __iomem *actmon_reg;
u32 delta_refcnt; int cnt = 0;
u64 val;
val = readq(actmon_reg);
c->last_refclk_cnt = upper_32_bits(val);
c->last_coreclk_cnt = lower_32_bits(val);
/* * The sampling window is based on the minimum number of reference * clock cycles which is known to give a stable value of CPU frequency.
*/ do {
val = readq(actmon_reg);
c->refclk_cnt = upper_32_bits(val);
c->coreclk_cnt = lower_32_bits(val); if (c->refclk_cnt < c->last_refclk_cnt)
delta_refcnt = c->refclk_cnt + (MAX_CNT - c->last_refclk_cnt); else
delta_refcnt = c->refclk_cnt - c->last_refclk_cnt; if (++cnt >= 0xFFFF) {
pr_warn("cpufreq: problem with refclk on cpu:%d, delta_refcnt:%u, cnt:%d\n",
c->cpu, delta_refcnt, cnt); break;
}
} while (delta_refcnt < data->soc->refclk_delta_min);
}
if (cpuid)
*cpuid = MPIDR_AFFINITY_LEVEL(mpidr, 0); if (clusterid)
*clusterid = MPIDR_AFFINITY_LEVEL(mpidr, 1);
}
/* * Read per-core Read-only system register NVFREQ_FEEDBACK_EL1. * The register provides frequency feedback information to * determine the average actual frequency a core has run at over * a period of time. * [31:0] PLLP counter: Counts at fixed frequency (408 MHz) * [63:32] Core clock counter: counts on every core clock cycle * where the core is architecturally clocking
*/ static u64 read_freq_feedback(void)
{
u64 val = 0;
val = read_freq_feedback();
c->last_refclk_cnt = lower_32_bits(val);
c->last_coreclk_cnt = upper_32_bits(val);
/* * The sampling window is based on the minimum number of reference * clock cycles which is known to give a stable value of CPU frequency.
*/ do {
val = read_freq_feedback();
c->refclk_cnt = lower_32_bits(val);
c->coreclk_cnt = upper_32_bits(val); if (c->refclk_cnt < c->last_refclk_cnt)
delta_refcnt = c->refclk_cnt + (MAX_CNT - c->last_refclk_cnt); else
delta_refcnt = c->refclk_cnt - c->last_refclk_cnt; if (++cnt >= 0xFFFF) {
pr_warn("cpufreq: problem with refclk on cpu:%d, delta_refcnt:%u, cnt:%d\n",
c->cpu, delta_refcnt, cnt); break;
}
} while (delta_refcnt < data->soc->refclk_delta_min);
}
/* * ref_clk_counter(32 bit counter) runs on constant clk, * pll_p(408MHz). * It will take = 2 ^ 32 / 408 MHz to overflow ref clk counter * = 10526880 usec = 10.527 sec to overflow * * Like wise core_clk_counter(32 bit counter) runs on core clock. * It's synchronized to crab_clk (cpu_crab_clk) which runs at * freq of cluster. Assuming max cluster clock ~2000MHz, * It will take = 2 ^ 32 / 2000 MHz to overflow core clk counter * = ~2.147 sec to overflow
*/
read_counters_work = container_of(work, struct read_counters_work,
work);
c = &read_counters_work->c;
data->soc->ops->read_counters(c);
}
/* * Return instantaneous cpu speed * Instantaneous freq is calculated as - * -Takes sample on every query of getting the freq. * - Read core and ref clock counters; * - Delay for X us * - Read above cycle counters again * - Calculates freq by subtracting current and previous counters * divided by the delay time or eqv. of ref_clk_counter in delta time * - Return Kcycles/second, freq in KHz * * delta time period = x sec * = delta ref_clk_counter / (408 * 10^6) sec * freq in Hz = cycles/sec * = (delta cycles / x sec * = (delta cycles * 408 * 10^6) / delta ref_clk_counter * in KHz = (delta cycles * 408 * 10^3) / delta ref_clk_counter * * @cpu - logical cpu whose freq to be updated * Returns freq in KHz on success, 0 if cpu is offline
*/ staticunsignedint tegra194_calculate_speed(u32 cpu)
{ struct read_counters_work read_counters_work; struct tegra_cpu_ctr c;
u32 delta_refcnt;
u32 delta_ccnt;
u32 rate_mhz;
/* * Reconstruct cpu frequency over an observation/sampling window. * Using workqueue to keep interrupts enabled during the interval.
*/
read_counters_work.c.cpu = cpu;
INIT_WORK_ONSTACK(&read_counters_work.work, tegra_read_counters);
queue_work_on(cpu, read_counters_wq, &read_counters_work.work);
flush_work(&read_counters_work.work);
c = read_counters_work.c;
/* reconstruct actual cpu freq using counters */
rate = tegra194_calculate_speed(cpu);
/* get last written ndiv value */
ret = data->soc->ops->get_cpu_ndiv(cpu, data->cpu_data[cpu].cpuid, clusterid, &ndiv); if (WARN_ON_ONCE(ret)) return rate;
/* * If the reconstructed frequency has acceptable delta from * the last written value, then return freq corresponding * to the last written ndiv value from freq_table. This is * done to return consistent value.
*/
cpufreq_for_each_valid_entry(pos, data->bpmp_luts[clusterid]) { if (pos->driver_data != ndiv) continue;
cpu_dev = get_cpu_device(policy->cpu); if (!cpu_dev) {
pr_err("%s: failed to get cpu%d device\n", __func__, policy->cpu); return -ENODEV;
}
/* Initialize OPP table mentioned in operating-points-v2 property in DT */
ret = dev_pm_opp_of_add_table_indexed(cpu_dev, 0); if (!ret) {
max_opps = dev_pm_opp_get_opp_count(cpu_dev); if (max_opps <= 0) {
dev_err(cpu_dev, "Failed to add OPPs\n"); return max_opps;
}
/* Disable all opps and cross-validate against LUT later */ for (rate = 0; ; rate++) {
opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate); if (IS_ERR(opp)) break;
dev_pm_opp_put(opp);
dev_pm_opp_disable(cpu_dev, rate);
}
} else {
dev_err(cpu_dev, "Invalid or empty opp table in device tree\n");
data->icc_dram_bw_scaling = false; return ret;
}
/* * Cross check the frequencies from BPMP-FW LUT against the OPP's present in DT. * Enable only those DT OPP's which are present in LUT also.
*/
cpufreq_for_each_valid_entry(pos, bpmp_lut) {
opp = dev_pm_opp_find_freq_exact(cpu_dev, pos->frequency * KHZ, false); if (IS_ERR(opp)) continue;
dev_pm_opp_put(opp);
ret = dev_pm_opp_enable(cpu_dev, pos->frequency * KHZ); if (ret < 0) return ret;
if (clusterid >= data->soc->num_clusters || !data->bpmp_luts[clusterid]) return -EINVAL;
start_cpu = rounddown(policy->cpu, maxcpus_per_cluster); /* set same policy for all cpus in a cluster */ for (cpu = start_cpu; cpu < (start_cpu + maxcpus_per_cluster); cpu++) { if (cpu_possible(cpu))
cpumask_set_cpu(cpu, policy->cpus);
}
policy->cpuinfo.transition_latency = TEGRA_CPUFREQ_TRANSITION_LATENCY;
bpmp_lut = data->bpmp_luts[clusterid];
if (data->icc_dram_bw_scaling) {
ret = tegra_cpufreq_init_cpufreq_table(policy, bpmp_lut, &freq_table); if (!ret) {
policy->freq_table = freq_table; return 0;
}
}
data->icc_dram_bw_scaling = false;
policy->freq_table = bpmp_lut;
pr_info("OPP tables missing from DT, EMC frequency scaling disabled\n");
return 0;
}
staticint tegra194_cpufreq_online(struct cpufreq_policy *policy)
{ /* We did light-weight tear down earlier, nothing to do here */ return 0;
}
staticint tegra194_cpufreq_offline(struct cpufreq_policy *policy)
{ /* * Preserve policy->driver_data and don't free resources on light-weight * tear down.
*/
/* * Each core writes frequency in per core register. Then both cores * in a cluster run at same frequency which is the maximum frequency * request out of the values requested by both cores in that cluster.
*/
data->soc->ops->set_cpu_ndiv(policy, (u64)tbl->driver_data);
if (data->icc_dram_bw_scaling)
tegra_cpufreq_set_bw(policy, tbl->frequency);
err = tegra_bpmp_transfer(bpmp, &msg); if (err) return ERR_PTR(err); if (msg.rx.ret == -BPMP_EINVAL) { /* Cluster not available */ return NULL;
} if (msg.rx.ret) return ERR_PTR(-EINVAL);
/* * Make sure frequency table step is a multiple of mdiv to match * vhint table granularity.
*/
freq_table_step_size = resp.mdiv *
DIV_ROUND_UP(CPUFREQ_TBL_STEP_HZ, resp.ref_clk_hz);
dev_dbg(&pdev->dev, "cluster %d: frequency table step size: %d\n",
cluster_id, freq_table_step_size);
delta_ndiv = resp.ndiv_max - resp.ndiv_min;
if (unlikely(delta_ndiv == 0)) {
num_freqs = 1;
} else { /* We store both ndiv_min and ndiv_max hence the +1 */
num_freqs = delta_ndiv / freq_table_step_size + 1;
}
data->bpmp_luts = devm_kcalloc(&pdev->dev, data->soc->num_clusters, sizeof(*data->bpmp_luts), GFP_KERNEL); if (!data->bpmp_luts) return -ENOMEM;
if (soc->actmon_cntr_base) { /* mmio registers are used for frequency request and re-construction */
data->regs = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(data->regs)) return PTR_ERR(data->regs);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.