2019-05-29 07:18:02 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-04-11 01:09:15 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/cpufreq.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
|
|
|
|
#include <soc/tegra/bpmp.h>
|
|
|
|
#include <soc/tegra/bpmp-abi.h>
|
|
|
|
|
2020-12-02 02:14:17 -07:00
|
|
|
#define TEGRA186_NUM_CLUSTERS 2
|
2020-12-02 02:14:16 -07:00
|
|
|
#define EDVD_OFFSET_A57(core) ((SZ_64K * 6) + (0x20 + (core) * 0x4))
|
|
|
|
#define EDVD_OFFSET_DENVER(core) ((SZ_64K * 7) + (0x20 + (core) * 0x4))
|
|
|
|
#define EDVD_CORE_VOLT_FREQ_F_SHIFT 0
|
|
|
|
#define EDVD_CORE_VOLT_FREQ_F_MASK 0xffff
|
|
|
|
#define EDVD_CORE_VOLT_FREQ_V_SHIFT 16
|
|
|
|
|
|
|
|
struct tegra186_cpufreq_cpu {
|
2020-12-02 02:14:17 -07:00
|
|
|
unsigned int bpmp_cluster_id;
|
2020-12-02 02:14:16 -07:00
|
|
|
unsigned int edvd_offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct tegra186_cpufreq_cpu tegra186_cpus[] = {
|
|
|
|
/* CPU0 - A57 Cluster */
|
|
|
|
{
|
2020-12-02 02:14:17 -07:00
|
|
|
.bpmp_cluster_id = 1,
|
2020-12-02 02:14:16 -07:00
|
|
|
.edvd_offset = EDVD_OFFSET_A57(0)
|
|
|
|
},
|
|
|
|
/* CPU1 - Denver Cluster */
|
|
|
|
{
|
2020-12-02 02:14:17 -07:00
|
|
|
.bpmp_cluster_id = 0,
|
2020-12-02 02:14:16 -07:00
|
|
|
.edvd_offset = EDVD_OFFSET_DENVER(0)
|
|
|
|
},
|
|
|
|
/* CPU2 - Denver Cluster */
|
|
|
|
{
|
2020-12-02 02:14:17 -07:00
|
|
|
.bpmp_cluster_id = 0,
|
2020-12-02 02:14:16 -07:00
|
|
|
.edvd_offset = EDVD_OFFSET_DENVER(1)
|
|
|
|
},
|
|
|
|
/* CPU3 - A57 Cluster */
|
|
|
|
{
|
2020-12-02 02:14:17 -07:00
|
|
|
.bpmp_cluster_id = 1,
|
2020-12-02 02:14:16 -07:00
|
|
|
.edvd_offset = EDVD_OFFSET_A57(1)
|
|
|
|
},
|
|
|
|
/* CPU4 - A57 Cluster */
|
|
|
|
{
|
2020-12-02 02:14:17 -07:00
|
|
|
.bpmp_cluster_id = 1,
|
2020-12-02 02:14:16 -07:00
|
|
|
.edvd_offset = EDVD_OFFSET_A57(2)
|
|
|
|
},
|
|
|
|
/* CPU5 - A57 Cluster */
|
|
|
|
{
|
2017-04-11 01:09:15 -07:00
|
|
|
.bpmp_cluster_id = 1,
|
2020-12-02 02:14:17 -07:00
|
|
|
.edvd_offset = EDVD_OFFSET_A57(3)
|
2017-04-11 01:09:15 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tegra186_cpufreq_cluster {
|
|
|
|
struct cpufreq_frequency_table *table;
|
2020-11-03 04:55:14 -07:00
|
|
|
u32 ref_clk_khz;
|
|
|
|
u32 div;
|
2017-04-11 01:09:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tegra186_cpufreq_data {
|
|
|
|
void __iomem *regs;
|
2020-12-02 02:14:16 -07:00
|
|
|
const struct tegra186_cpufreq_cpu *cpus;
|
cpufreq: tegra186: Use flexible array to simplify memory allocation
Use flexible array to simplify memory allocation.
It saves some memory, avoids an indirection when reading the 'clusters'
array and removes some LoC.
Detailed explanation:
====================
Knowing that:
- each devm_ allocation over-allocates 40 bytes for internal needs
- Some rounding is done by the memory allocator on 8, 16, 32, 64, 96,
128, 192, 256, 512, 1024, 2048, 4096, 8192 boundaries
and that:
- sizeof(struct tegra186_cpufreq_data) = 24
- sizeof(struct tegra186_cpufreq_cluster) = 16
Memory allocations in tegra186_cpufreq_probe() are:
data: (24 + 40) = 64 => 64 bytes
data->clusters: (2 * 16 + 40) = 72 => 96 bytes
So a total of 160 bytes are allocated.
56 for the real need, 80 for internal uses and 24 are wasted.
If 'struct tegra186_cpufreq_data' is reordered so that 'clusters' is a
flexible array:
- it saves one pointer in the structure
- only one allocation is needed
So, only 96 bytes are allocated:
16 + 2 * 16 + 40 = 88 => 96 bytes
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2022-11-20 10:19:02 -07:00
|
|
|
struct tegra186_cpufreq_cluster clusters[];
|
2017-04-11 01:09:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static int tegra186_cpufreq_init(struct cpufreq_policy *policy)
|
|
|
|
{
|
|
|
|
struct tegra186_cpufreq_data *data = cpufreq_get_driver_data();
|
2020-12-02 02:14:17 -07:00
|
|
|
unsigned int cluster = data->cpus[policy->cpu].bpmp_cluster_id;
|
2017-04-11 01:09:15 -07:00
|
|
|
|
2020-12-02 02:14:17 -07:00
|
|
|
policy->freq_table = data->clusters[cluster].table;
|
2017-04-11 01:09:15 -07:00
|
|
|
policy->cpuinfo.transition_latency = 300 * 1000;
|
2020-12-02 02:14:16 -07:00
|
|
|
policy->driver_data = NULL;
|
2017-04-11 01:09:15 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tegra186_cpufreq_set_target(struct cpufreq_policy *policy,
|
|
|
|
unsigned int index)
|
|
|
|
{
|
2020-12-02 02:14:16 -07:00
|
|
|
struct tegra186_cpufreq_data *data = cpufreq_get_driver_data();
|
2017-04-11 01:09:15 -07:00
|
|
|
struct cpufreq_frequency_table *tbl = policy->freq_table + index;
|
2020-12-02 02:14:16 -07:00
|
|
|
unsigned int edvd_offset = data->cpus[policy->cpu].edvd_offset;
|
2017-04-11 01:09:15 -07:00
|
|
|
u32 edvd_val = tbl->driver_data;
|
|
|
|
|
2020-12-02 02:14:16 -07:00
|
|
|
writel(edvd_val, data->regs + edvd_offset);
|
2017-04-11 01:09:15 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-24 07:59:07 -07:00
|
|
|
static unsigned int tegra186_cpufreq_get(unsigned int cpu)
|
|
|
|
{
|
2020-11-03 04:55:14 -07:00
|
|
|
struct tegra186_cpufreq_data *data = cpufreq_get_driver_data();
|
2020-12-02 02:14:17 -07:00
|
|
|
struct tegra186_cpufreq_cluster *cluster;
|
2020-08-24 07:59:07 -07:00
|
|
|
struct cpufreq_policy *policy;
|
2020-12-02 02:14:17 -07:00
|
|
|
unsigned int edvd_offset, cluster_id;
|
2020-08-24 07:59:07 -07:00
|
|
|
u32 ndiv;
|
|
|
|
|
|
|
|
policy = cpufreq_cpu_get(cpu);
|
|
|
|
if (!policy)
|
|
|
|
return 0;
|
|
|
|
|
2020-12-02 02:14:16 -07:00
|
|
|
edvd_offset = data->cpus[policy->cpu].edvd_offset;
|
|
|
|
ndiv = readl(data->regs + edvd_offset) & EDVD_CORE_VOLT_FREQ_F_MASK;
|
2020-12-02 02:14:17 -07:00
|
|
|
cluster_id = data->cpus[policy->cpu].bpmp_cluster_id;
|
|
|
|
cluster = &data->clusters[cluster_id];
|
2020-08-24 07:59:07 -07:00
|
|
|
cpufreq_cpu_put(policy);
|
|
|
|
|
2020-12-02 02:14:17 -07:00
|
|
|
return (cluster->ref_clk_khz * ndiv) / cluster->div;
|
2020-08-24 07:59:07 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 01:09:15 -07:00
|
|
|
static struct cpufreq_driver tegra186_cpufreq_driver = {
|
|
|
|
.name = "tegra186",
|
2021-02-01 21:55:11 -07:00
|
|
|
.flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
|
2019-10-23 02:27:03 -07:00
|
|
|
CPUFREQ_NEED_INITIAL_FREQ_CHECK,
|
2020-08-24 07:59:07 -07:00
|
|
|
.get = tegra186_cpufreq_get,
|
2017-04-11 01:09:15 -07:00
|
|
|
.verify = cpufreq_generic_frequency_table_verify,
|
|
|
|
.target_index = tegra186_cpufreq_set_target,
|
|
|
|
.init = tegra186_cpufreq_init,
|
|
|
|
.attr = cpufreq_generic_attr,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct cpufreq_frequency_table *init_vhint_table(
|
|
|
|
struct platform_device *pdev, struct tegra_bpmp *bpmp,
|
2020-12-02 02:14:17 -07:00
|
|
|
struct tegra186_cpufreq_cluster *cluster, unsigned int cluster_id)
|
2017-04-11 01:09:15 -07:00
|
|
|
{
|
|
|
|
struct cpufreq_frequency_table *table;
|
|
|
|
struct mrq_cpu_vhint_request req;
|
|
|
|
struct tegra_bpmp_message msg;
|
|
|
|
struct cpu_vhint_data *data;
|
|
|
|
int err, i, j, num_rates = 0;
|
|
|
|
dma_addr_t phys;
|
|
|
|
void *virt;
|
|
|
|
|
|
|
|
virt = dma_alloc_coherent(bpmp->dev, sizeof(*data), &phys,
|
2018-10-13 08:17:00 -07:00
|
|
|
GFP_KERNEL);
|
2017-04-11 01:09:15 -07:00
|
|
|
if (!virt)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
data = (struct cpu_vhint_data *)virt;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
req.addr = phys;
|
2020-12-02 02:14:17 -07:00
|
|
|
req.cluster_id = cluster_id;
|
2017-04-11 01:09:15 -07:00
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.mrq = MRQ_CPU_VHINT;
|
|
|
|
msg.tx.data = &req;
|
|
|
|
msg.tx.size = sizeof(req);
|
|
|
|
|
|
|
|
err = tegra_bpmp_transfer(bpmp, &msg);
|
|
|
|
if (err) {
|
|
|
|
table = ERR_PTR(err);
|
|
|
|
goto free;
|
|
|
|
}
|
2021-09-15 01:55:16 -07:00
|
|
|
if (msg.rx.ret) {
|
|
|
|
table = ERR_PTR(-EINVAL);
|
|
|
|
goto free;
|
|
|
|
}
|
2017-04-11 01:09:15 -07:00
|
|
|
|
|
|
|
for (i = data->vfloor; i <= data->vceil; i++) {
|
|
|
|
u16 ndiv = data->ndiv[i];
|
|
|
|
|
|
|
|
if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Only store lowest voltage index for each rate */
|
|
|
|
if (i > 0 && ndiv == data->ndiv[i - 1])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
num_rates++;
|
|
|
|
}
|
|
|
|
|
|
|
|
table = devm_kcalloc(&pdev->dev, num_rates + 1, sizeof(*table),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!table) {
|
|
|
|
table = ERR_PTR(-ENOMEM);
|
|
|
|
goto free;
|
|
|
|
}
|
|
|
|
|
2020-11-03 04:55:14 -07:00
|
|
|
cluster->ref_clk_khz = data->ref_clk_hz / 1000;
|
|
|
|
cluster->div = data->pdiv * data->mdiv;
|
|
|
|
|
2017-04-11 01:09:15 -07:00
|
|
|
for (i = data->vfloor, j = 0; i <= data->vceil; i++) {
|
|
|
|
struct cpufreq_frequency_table *point;
|
|
|
|
u16 ndiv = data->ndiv[i];
|
|
|
|
u32 edvd_val = 0;
|
|
|
|
|
|
|
|
if (ndiv < data->ndiv_min || ndiv > data->ndiv_max)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Only store lowest voltage index for each rate */
|
|
|
|
if (i > 0 && ndiv == data->ndiv[i - 1])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
edvd_val |= i << EDVD_CORE_VOLT_FREQ_V_SHIFT;
|
|
|
|
edvd_val |= ndiv << EDVD_CORE_VOLT_FREQ_F_SHIFT;
|
|
|
|
|
|
|
|
point = &table[j++];
|
|
|
|
point->driver_data = edvd_val;
|
2020-11-03 04:55:14 -07:00
|
|
|
point->frequency = (cluster->ref_clk_khz * ndiv) / cluster->div;
|
2017-04-11 01:09:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
table[j].frequency = CPUFREQ_TABLE_END;
|
|
|
|
|
|
|
|
free:
|
|
|
|
dma_free_coherent(bpmp->dev, sizeof(*data), virt, phys);
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tegra186_cpufreq_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct tegra186_cpufreq_data *data;
|
|
|
|
struct tegra_bpmp *bpmp;
|
|
|
|
unsigned int i = 0, err;
|
|
|
|
|
cpufreq: tegra186: Use flexible array to simplify memory allocation
Use flexible array to simplify memory allocation.
It saves some memory, avoids an indirection when reading the 'clusters'
array and removes some LoC.
Detailed explanation:
====================
Knowing that:
- each devm_ allocation over-allocates 40 bytes for internal needs
- Some rounding is done by the memory allocator on 8, 16, 32, 64, 96,
128, 192, 256, 512, 1024, 2048, 4096, 8192 boundaries
and that:
- sizeof(struct tegra186_cpufreq_data) = 24
- sizeof(struct tegra186_cpufreq_cluster) = 16
Memory allocations in tegra186_cpufreq_probe() are:
data: (24 + 40) = 64 => 64 bytes
data->clusters: (2 * 16 + 40) = 72 => 96 bytes
So a total of 160 bytes are allocated.
56 for the real need, 80 for internal uses and 24 are wasted.
If 'struct tegra186_cpufreq_data' is reordered so that 'clusters' is a
flexible array:
- it saves one pointer in the structure
- only one allocation is needed
So, only 96 bytes are allocated:
16 + 2 * 16 + 40 = 88 => 96 bytes
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
2022-11-20 10:19:02 -07:00
|
|
|
data = devm_kzalloc(&pdev->dev,
|
|
|
|
struct_size(data, clusters, TEGRA186_NUM_CLUSTERS),
|
|
|
|
GFP_KERNEL);
|
2017-04-11 01:09:15 -07:00
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-12-02 02:14:16 -07:00
|
|
|
data->cpus = tegra186_cpus;
|
2017-04-11 01:09:15 -07:00
|
|
|
|
|
|
|
bpmp = tegra_bpmp_get(&pdev->dev);
|
|
|
|
if (IS_ERR(bpmp))
|
|
|
|
return PTR_ERR(bpmp);
|
|
|
|
|
2019-12-15 07:05:22 -07:00
|
|
|
data->regs = devm_platform_ioremap_resource(pdev, 0);
|
2017-04-11 01:09:15 -07:00
|
|
|
if (IS_ERR(data->regs)) {
|
|
|
|
err = PTR_ERR(data->regs);
|
|
|
|
goto put_bpmp;
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:14:17 -07:00
|
|
|
for (i = 0; i < TEGRA186_NUM_CLUSTERS; i++) {
|
2017-04-11 01:09:15 -07:00
|
|
|
struct tegra186_cpufreq_cluster *cluster = &data->clusters[i];
|
|
|
|
|
2020-12-02 02:14:17 -07:00
|
|
|
cluster->table = init_vhint_table(pdev, bpmp, cluster, i);
|
2017-04-11 01:09:15 -07:00
|
|
|
if (IS_ERR(cluster->table)) {
|
|
|
|
err = PTR_ERR(cluster->table);
|
|
|
|
goto put_bpmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tegra186_cpufreq_driver.driver_data = data;
|
|
|
|
|
|
|
|
err = cpufreq_register_driver(&tegra186_cpufreq_driver);
|
|
|
|
|
|
|
|
put_bpmp:
|
|
|
|
tegra_bpmp_put(bpmp);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-07-12 02:33:19 -07:00
|
|
|
static void tegra186_cpufreq_remove(struct platform_device *pdev)
|
2017-04-11 01:09:15 -07:00
|
|
|
{
|
|
|
|
cpufreq_unregister_driver(&tegra186_cpufreq_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id tegra186_cpufreq_of_match[] = {
|
|
|
|
{ .compatible = "nvidia,tegra186-ccplex-cluster", },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, tegra186_cpufreq_of_match);
|
|
|
|
|
|
|
|
static struct platform_driver tegra186_cpufreq_platform_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "tegra186-cpufreq",
|
|
|
|
.of_match_table = tegra186_cpufreq_of_match,
|
|
|
|
},
|
|
|
|
.probe = tegra186_cpufreq_probe,
|
2023-07-12 02:33:19 -07:00
|
|
|
.remove_new = tegra186_cpufreq_remove,
|
2017-04-11 01:09:15 -07:00
|
|
|
};
|
|
|
|
module_platform_driver(tegra186_cpufreq_platform_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
|
|
|
|
MODULE_DESCRIPTION("NVIDIA Tegra186 cpufreq driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|