summaryrefslogtreecommitdiff
path: root/bsp/rockchip/dm/dvfs/cpufreq/dvfs-rockchip-cpufreq.c
blob: f0e4b9e356bc56da18c11f8e162a56f197f14373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-21     GuEe-GUI     first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "dvfs.cpufreq"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include <opp-select.h>

struct cpufreq_cluster
{
    rt_list_t list;
    struct rt_dvfs_cpufreq cpufreq;
    struct opp_info opp_info;
    struct rt_platform_device *pdev;
#ifdef RT_THERMAL_COOL_DVFS
    struct rt_platform_device *cool_pdev;
#endif
    RT_BITMAP_DECLARE(cpus_map, RT_CPUS_NR);
};

static rt_list_t cluster_list = RT_LIST_OBJECT_INIT(cluster_list);

static struct cpufreq_cluster *cluster_by_opp(struct rt_ofw_node *opp_np)
{
    struct cpufreq_cluster *cluster;

    if (!opp_np)
    {
        return RT_NULL;
    }

    rt_list_for_each_entry(cluster, &cluster_list, list)
    {
        if (cluster->opp_info.opp_np == opp_np)
        {
            return cluster;
        }
    }

    return RT_NULL;
}

static rt_bool_t cpu_shares_opp(struct rt_ofw_node *cpu_np,
        struct rt_ofw_node *opp_np)
{
    struct rt_ofw_node *cpu_opp;

    cpu_opp = rt_ofw_parse_phandle(cpu_np, "operating-points-v2", 0);
    if (!cpu_opp)
    {
        return RT_FALSE;
    }

    if (cpu_opp == opp_np)
    {
        rt_ofw_node_put(cpu_opp);
        return RT_TRUE;
    }

    rt_ofw_node_put(cpu_opp);
    return RT_FALSE;
}

static rt_err_t scaling_set_opp(struct rt_dvfs_scaling *scaling,
        struct rt_dvfs_opp *opp)
{
    struct cpufreq_cluster *cluster = scaling->priv;

    return cpufreq_set_opp(scaling, opp, &cluster->opp_info);
}

static rt_err_t cpufreq_parse_opp(struct rt_dvfs_scaling *dvfs,
        struct rt_dvfs_opp *opp, void *fw_np)
{
    struct cpufreq_cluster *cluster = dvfs->priv;

    return opp_parse(dvfs, opp, (struct rt_ofw_node *)fw_np, &cluster->opp_info);
}

static struct rt_dvfs_scaling_ops cpufreq_ops =
{
    .set_opp = scaling_set_opp,
    .parse_opp = cpufreq_parse_opp,
};

#ifdef RT_THERMAL_COOL_DVFS
static rt_err_t cluster_cool_register(struct cpufreq_cluster *cluster,
        struct rt_ofw_node *cpu_np, struct rt_dvfs_scaling *scaling)
{
    rt_err_t err;
    struct rt_platform_device *cool;

    if (!rt_ofw_prop_read_bool(cpu_np, "#cooling-cells"))
    {
        return RT_EOK;
    }

    if (!(cool = rt_platform_device_alloc("dvfs-cool")))
    {
        return -RT_ENOMEM;
    }

    cool->parent.ofw_node = cpu_np;
    rt_ofw_node_get(cpu_np);
    cool->priv = scaling;

    if ((err = rt_platform_device_register(cool)))
    {
        rt_ofw_node_put(cpu_np);
        rt_free(cool);
        return err;
    }

    cluster->cool_pdev = cool;

    return RT_EOK;
}

static void cluster_cool_unregister(struct cpufreq_cluster *cluster)
{
    struct rt_platform_device *cool = cluster->cool_pdev;

    if (!cool)
    {
        return;
    }

    rt_bus_remove_device(&cool->parent);
    rt_ofw_node_put(cool->parent.ofw_node);
    rt_free(cool);
    cluster->cool_pdev = RT_NULL;
}
#endif /* RT_THERMAL_COOL_DVFS */

static void cluster_destroy(struct cpufreq_cluster *cluster,
        struct rt_device *dev, struct rt_dvfs_scaling *scaling)
{
#ifdef RT_THERMAL_COOL_DVFS
    cluster_cool_unregister(cluster);
#endif

    if (scaling)
    {
        rt_dvfs_cpufreq_unregister(&cluster->cpufreq);
    }

    if (dev)
    {
        opp_table_uninit(dev, &cluster->opp_info);
        rt_device_unregister(dev);
        rt_ofw_node_put(dev->ofw_node);
    }

    if (cluster->pdev)
    {
        rt_free(cluster->pdev);
        cluster->pdev = RT_NULL;
    }
}

static rt_err_t cluster_init(struct cpufreq_cluster *cluster,
        struct rt_ofw_node *cpu_np, struct rt_ofw_node *opp_np)
{
    rt_err_t err;
    const char *reg_name;
    struct rt_device *dev;
    struct rt_dvfs_scaling *scaling;
    struct rt_platform_device *pdev;

    if (!(reg_name = regulator_name(cpu_np)))
    {
        LOG_W("%s: no cpu supply property, skip cluster",
              rt_ofw_node_full_name(cpu_np));
        return -RT_ENOENT;
    }

    if (!(pdev = rt_platform_device_alloc("cpufreq")))
    {
        return -RT_ENOMEM;
    }

    dev = &pdev->parent;
    dev->ofw_node = cpu_np;
    rt_ofw_node_get(cpu_np);

    rt_dm_dev_set_name_auto(dev, "cpufreq");

    if ((err = rt_device_register(dev, rt_dm_dev_get_name(dev), RT_DEVICE_FLAG_DEACTIVATE)))
    {
        rt_ofw_node_put(cpu_np);
        rt_free(pdev);
        return err;
    }

    scaling = rt_dvfs_cpufreq_to_scaling(&cluster->cpufreq);
    scaling->dev = dev;
    scaling->ops = &cpufreq_ops;
    scaling->priv = cluster;

    if ((err = opp_table_init(dev, &cluster->opp_info, reg_name)))
    {
        goto _fail;
    }

    if (cluster->opp_info.opp_np != opp_np)
    {
        rt_ofw_node_put(cluster->opp_info.opp_np);
        cluster->opp_info.opp_np = opp_np;
        rt_ofw_node_get(opp_np);
    }

    scaling->clk = cluster->opp_info.clk;
    scaling->supply = cluster->opp_info.supply;

    if (!scaling->transition_latency)
    {
        scaling->transition_latency = 1000000;
    }

    if ((err = rt_dvfs_cpufreq_register(&cluster->cpufreq)))
    {
        goto _fail;
    }

    if ((err = opp_table_adjust(dev, &cluster->opp_info, scaling)))
    {
        goto _fail_scaling;
    }

    if ((err = cpufreq_sync_hw_state(scaling)))
    {
        LOG_W("%s: sync hw state failed: %s, cpufreq may retry regulator",
              rt_dm_dev_get_name(dev), rt_strerror(err));
    }

    if ((err = rt_dvfs_scaling_set_governor(scaling, RT_DVFS_GOVERNOR_TYPE_ONDEMAND)))
    {
        rt_dvfs_scaling_set_governor(scaling, RT_DVFS_GOVERNOR_TYPE_PERFORMANCE);
    }
#ifdef RT_THERMAL_COOL_DVFS
    if ((err = cluster_cool_register(cluster, cpu_np, scaling)))
    {
        LOG_W("%s: dvfs-cool register failed: %s",
              rt_dm_dev_get_name(dev), rt_strerror(err));
    }
#endif

    cluster->pdev = pdev;
    rt_list_insert_before(&cluster_list, &cluster->list);

    LOG_D("CPUFreq cluster %s %lu-%lu Hz on %s",
          rt_ofw_node_full_name(cpu_np),
          scaling->min_freq, scaling->max_freq,
          rt_dm_dev_get_name(dev));

    return RT_EOK;

_fail_scaling:
    rt_dvfs_cpufreq_unregister(&cluster->cpufreq);
_fail:
    cluster_destroy(cluster, dev, RT_NULL);
    return err;
}

static int rockchip_dvfs_cpufreq_init(void)
{
    rt_err_t err;
    int cpu_id = 0, registered = 0;
    struct rt_ofw_node *cpu_np = RT_NULL;
    struct cpufreq_cluster *cluster;
    struct rt_ofw_node *opp_np;

    rt_ofw_foreach_cpu_node(cpu_np)
    {
        int share_id = 0;
        struct rt_ofw_node *share_np = RT_NULL;

        if (!(opp_np = rt_ofw_parse_phandle(cpu_np, "operating-points-v2", 0)))
        {
            ++cpu_id;
            continue;
        }

        if (cluster_by_opp(opp_np))
        {
            rt_ofw_node_put(opp_np);
            ++cpu_id;
            continue;
        }

        if (!(cluster = rt_calloc(1, sizeof(*cluster))))
        {
            rt_ofw_node_put(opp_np);
            return -RT_ENOMEM;
        }

        cluster->cpufreq.master_cpu = cpu_id;
        rt_bitmap_set_bit(cluster->cpus_map, cpu_id);

        rt_ofw_foreach_cpu_node(share_np)
        {
            if (cpu_shares_opp(share_np, opp_np))
            {
                rt_bitmap_set_bit(cluster->cpus_map, share_id);
            }
            ++share_id;
        }

        rt_memcpy(cluster->cpufreq.cpus_map, cluster->cpus_map,
                sizeof(cluster->cpus_map));

        err = cluster_init(cluster, cpu_np, opp_np);
        rt_ofw_node_put(opp_np);

        if (err)
        {
            LOG_W("CPUFreq cluster %s init failed: %s",
                  rt_ofw_node_full_name(cpu_np), rt_strerror(err));
            rt_free(cluster);
        }
        else
        {
            ++registered;
        }

        ++cpu_id;
    }

    if (!registered)
    {
        return (int)-RT_ENOENT;
    }

    return (int)RT_EOK;
}
INIT_APP_EXPORT(rockchip_dvfs_cpufreq_init);