Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Versatile Express SPC CPUFreq Interface driver
0004  *
0005  * Copyright (C) 2013 - 2019 ARM Ltd.
0006  * Sudeep Holla <sudeep.holla@arm.com>
0007  *
0008  * Copyright (C) 2013 Linaro.
0009  * Viresh Kumar <viresh.kumar@linaro.org>
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/clk.h>
0015 #include <linux/cpu.h>
0016 #include <linux/cpufreq.h>
0017 #include <linux/cpumask.h>
0018 #include <linux/device.h>
0019 #include <linux/module.h>
0020 #include <linux/mutex.h>
0021 #include <linux/of_platform.h>
0022 #include <linux/platform_device.h>
0023 #include <linux/pm_opp.h>
0024 #include <linux/slab.h>
0025 #include <linux/topology.h>
0026 #include <linux/types.h>
0027 
0028 /* Currently we support only two clusters */
0029 #define A15_CLUSTER 0
0030 #define A7_CLUSTER  1
0031 #define MAX_CLUSTERS    2
0032 
0033 #ifdef CONFIG_BL_SWITCHER
0034 #include <asm/bL_switcher.h>
0035 static bool bL_switching_enabled;
0036 #define is_bL_switching_enabled()   bL_switching_enabled
0037 #define set_switching_enabled(x)    (bL_switching_enabled = (x))
0038 #else
0039 #define is_bL_switching_enabled()   false
0040 #define set_switching_enabled(x)    do { } while (0)
0041 #define bL_switch_request(...)      do { } while (0)
0042 #define bL_switcher_put_enabled()   do { } while (0)
0043 #define bL_switcher_get_enabled()   do { } while (0)
0044 #endif
0045 
0046 #define ACTUAL_FREQ(cluster, freq)  ((cluster == A7_CLUSTER) ? freq << 1 : freq)
0047 #define VIRT_FREQ(cluster, freq)    ((cluster == A7_CLUSTER) ? freq >> 1 : freq)
0048 
0049 static struct clk *clk[MAX_CLUSTERS];
0050 static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1];
0051 static atomic_t cluster_usage[MAX_CLUSTERS + 1];
0052 
0053 static unsigned int clk_big_min;    /* (Big) clock frequencies */
0054 static unsigned int clk_little_max; /* Maximum clock frequency (Little) */
0055 
0056 static DEFINE_PER_CPU(unsigned int, physical_cluster);
0057 static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq);
0058 
0059 static struct mutex cluster_lock[MAX_CLUSTERS];
0060 
0061 static inline int raw_cpu_to_cluster(int cpu)
0062 {
0063     return topology_physical_package_id(cpu);
0064 }
0065 
0066 static inline int cpu_to_cluster(int cpu)
0067 {
0068     return is_bL_switching_enabled() ?
0069         MAX_CLUSTERS : raw_cpu_to_cluster(cpu);
0070 }
0071 
0072 static unsigned int find_cluster_maxfreq(int cluster)
0073 {
0074     int j;
0075     u32 max_freq = 0, cpu_freq;
0076 
0077     for_each_online_cpu(j) {
0078         cpu_freq = per_cpu(cpu_last_req_freq, j);
0079 
0080         if (cluster == per_cpu(physical_cluster, j) &&
0081             max_freq < cpu_freq)
0082             max_freq = cpu_freq;
0083     }
0084 
0085     return max_freq;
0086 }
0087 
0088 static unsigned int clk_get_cpu_rate(unsigned int cpu)
0089 {
0090     u32 cur_cluster = per_cpu(physical_cluster, cpu);
0091     u32 rate = clk_get_rate(clk[cur_cluster]) / 1000;
0092 
0093     /* For switcher we use virtual A7 clock rates */
0094     if (is_bL_switching_enabled())
0095         rate = VIRT_FREQ(cur_cluster, rate);
0096 
0097     return rate;
0098 }
0099 
0100 static unsigned int ve_spc_cpufreq_get_rate(unsigned int cpu)
0101 {
0102     if (is_bL_switching_enabled())
0103         return per_cpu(cpu_last_req_freq, cpu);
0104     else
0105         return clk_get_cpu_rate(cpu);
0106 }
0107 
0108 static unsigned int
0109 ve_spc_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate)
0110 {
0111     u32 new_rate, prev_rate;
0112     int ret;
0113     bool bLs = is_bL_switching_enabled();
0114 
0115     mutex_lock(&cluster_lock[new_cluster]);
0116 
0117     if (bLs) {
0118         prev_rate = per_cpu(cpu_last_req_freq, cpu);
0119         per_cpu(cpu_last_req_freq, cpu) = rate;
0120         per_cpu(physical_cluster, cpu) = new_cluster;
0121 
0122         new_rate = find_cluster_maxfreq(new_cluster);
0123         new_rate = ACTUAL_FREQ(new_cluster, new_rate);
0124     } else {
0125         new_rate = rate;
0126     }
0127 
0128     ret = clk_set_rate(clk[new_cluster], new_rate * 1000);
0129     if (!ret) {
0130         /*
0131          * FIXME: clk_set_rate hasn't returned an error here however it
0132          * may be that clk_change_rate failed due to hardware or
0133          * firmware issues and wasn't able to report that due to the
0134          * current design of the clk core layer. To work around this
0135          * problem we will read back the clock rate and check it is
0136          * correct. This needs to be removed once clk core is fixed.
0137          */
0138         if (clk_get_rate(clk[new_cluster]) != new_rate * 1000)
0139             ret = -EIO;
0140     }
0141 
0142     if (WARN_ON(ret)) {
0143         if (bLs) {
0144             per_cpu(cpu_last_req_freq, cpu) = prev_rate;
0145             per_cpu(physical_cluster, cpu) = old_cluster;
0146         }
0147 
0148         mutex_unlock(&cluster_lock[new_cluster]);
0149 
0150         return ret;
0151     }
0152 
0153     mutex_unlock(&cluster_lock[new_cluster]);
0154 
0155     /* Recalc freq for old cluster when switching clusters */
0156     if (old_cluster != new_cluster) {
0157         /* Switch cluster */
0158         bL_switch_request(cpu, new_cluster);
0159 
0160         mutex_lock(&cluster_lock[old_cluster]);
0161 
0162         /* Set freq of old cluster if there are cpus left on it */
0163         new_rate = find_cluster_maxfreq(old_cluster);
0164         new_rate = ACTUAL_FREQ(old_cluster, new_rate);
0165 
0166         if (new_rate &&
0167             clk_set_rate(clk[old_cluster], new_rate * 1000)) {
0168             pr_err("%s: clk_set_rate failed: %d, old cluster: %d\n",
0169                    __func__, ret, old_cluster);
0170         }
0171         mutex_unlock(&cluster_lock[old_cluster]);
0172     }
0173 
0174     return 0;
0175 }
0176 
0177 /* Set clock frequency */
0178 static int ve_spc_cpufreq_set_target(struct cpufreq_policy *policy,
0179                      unsigned int index)
0180 {
0181     u32 cpu = policy->cpu, cur_cluster, new_cluster, actual_cluster;
0182     unsigned int freqs_new;
0183 
0184     cur_cluster = cpu_to_cluster(cpu);
0185     new_cluster = actual_cluster = per_cpu(physical_cluster, cpu);
0186 
0187     freqs_new = freq_table[cur_cluster][index].frequency;
0188 
0189     if (is_bL_switching_enabled()) {
0190         if (actual_cluster == A15_CLUSTER && freqs_new < clk_big_min)
0191             new_cluster = A7_CLUSTER;
0192         else if (actual_cluster == A7_CLUSTER &&
0193              freqs_new > clk_little_max)
0194             new_cluster = A15_CLUSTER;
0195     }
0196 
0197     return ve_spc_cpufreq_set_rate(cpu, actual_cluster, new_cluster,
0198                        freqs_new);
0199 }
0200 
0201 static inline u32 get_table_count(struct cpufreq_frequency_table *table)
0202 {
0203     int count;
0204 
0205     for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++)
0206         ;
0207 
0208     return count;
0209 }
0210 
0211 /* get the minimum frequency in the cpufreq_frequency_table */
0212 static inline u32 get_table_min(struct cpufreq_frequency_table *table)
0213 {
0214     struct cpufreq_frequency_table *pos;
0215     u32 min_freq = ~0;
0216 
0217     cpufreq_for_each_entry(pos, table)
0218         if (pos->frequency < min_freq)
0219             min_freq = pos->frequency;
0220     return min_freq;
0221 }
0222 
0223 /* get the maximum frequency in the cpufreq_frequency_table */
0224 static inline u32 get_table_max(struct cpufreq_frequency_table *table)
0225 {
0226     struct cpufreq_frequency_table *pos;
0227     u32 max_freq = 0;
0228 
0229     cpufreq_for_each_entry(pos, table)
0230         if (pos->frequency > max_freq)
0231             max_freq = pos->frequency;
0232     return max_freq;
0233 }
0234 
0235 static bool search_frequency(struct cpufreq_frequency_table *table, int size,
0236                  unsigned int freq)
0237 {
0238     int count;
0239 
0240     for (count = 0; count < size; count++) {
0241         if (table[count].frequency == freq)
0242             return true;
0243     }
0244 
0245     return false;
0246 }
0247 
0248 static int merge_cluster_tables(void)
0249 {
0250     int i, j, k = 0, count = 1;
0251     struct cpufreq_frequency_table *table;
0252 
0253     for (i = 0; i < MAX_CLUSTERS; i++)
0254         count += get_table_count(freq_table[i]);
0255 
0256     table = kcalloc(count, sizeof(*table), GFP_KERNEL);
0257     if (!table)
0258         return -ENOMEM;
0259 
0260     freq_table[MAX_CLUSTERS] = table;
0261 
0262     /* Add in reverse order to get freqs in increasing order */
0263     for (i = MAX_CLUSTERS - 1; i >= 0; i--, count = k) {
0264         for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END;
0265              j++) {
0266             if (i == A15_CLUSTER &&
0267                 search_frequency(table, count, freq_table[i][j].frequency))
0268                 continue; /* skip duplicates */
0269             table[k++].frequency =
0270                 VIRT_FREQ(i, freq_table[i][j].frequency);
0271         }
0272     }
0273 
0274     table[k].driver_data = k;
0275     table[k].frequency = CPUFREQ_TABLE_END;
0276 
0277     return 0;
0278 }
0279 
0280 static void _put_cluster_clk_and_freq_table(struct device *cpu_dev,
0281                         const struct cpumask *cpumask)
0282 {
0283     u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
0284 
0285     if (!freq_table[cluster])
0286         return;
0287 
0288     clk_put(clk[cluster]);
0289     dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
0290 }
0291 
0292 static void put_cluster_clk_and_freq_table(struct device *cpu_dev,
0293                        const struct cpumask *cpumask)
0294 {
0295     u32 cluster = cpu_to_cluster(cpu_dev->id);
0296     int i;
0297 
0298     if (atomic_dec_return(&cluster_usage[cluster]))
0299         return;
0300 
0301     if (cluster < MAX_CLUSTERS)
0302         return _put_cluster_clk_and_freq_table(cpu_dev, cpumask);
0303 
0304     for_each_present_cpu(i) {
0305         struct device *cdev = get_cpu_device(i);
0306 
0307         if (!cdev)
0308             return;
0309 
0310         _put_cluster_clk_and_freq_table(cdev, cpumask);
0311     }
0312 
0313     /* free virtual table */
0314     kfree(freq_table[cluster]);
0315 }
0316 
0317 static int _get_cluster_clk_and_freq_table(struct device *cpu_dev,
0318                        const struct cpumask *cpumask)
0319 {
0320     u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
0321     int ret;
0322 
0323     if (freq_table[cluster])
0324         return 0;
0325 
0326     /*
0327      * platform specific SPC code must initialise the opp table
0328      * so just check if the OPP count is non-zero
0329      */
0330     ret = dev_pm_opp_get_opp_count(cpu_dev) <= 0;
0331     if (ret)
0332         goto out;
0333 
0334     ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
0335     if (ret)
0336         goto out;
0337 
0338     clk[cluster] = clk_get(cpu_dev, NULL);
0339     if (!IS_ERR(clk[cluster]))
0340         return 0;
0341 
0342     dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
0343         __func__, cpu_dev->id, cluster);
0344     ret = PTR_ERR(clk[cluster]);
0345     dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
0346 
0347 out:
0348     dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
0349         cluster);
0350     return ret;
0351 }
0352 
0353 static int get_cluster_clk_and_freq_table(struct device *cpu_dev,
0354                       const struct cpumask *cpumask)
0355 {
0356     u32 cluster = cpu_to_cluster(cpu_dev->id);
0357     int i, ret;
0358 
0359     if (atomic_inc_return(&cluster_usage[cluster]) != 1)
0360         return 0;
0361 
0362     if (cluster < MAX_CLUSTERS) {
0363         ret = _get_cluster_clk_and_freq_table(cpu_dev, cpumask);
0364         if (ret)
0365             atomic_dec(&cluster_usage[cluster]);
0366         return ret;
0367     }
0368 
0369     /*
0370      * Get data for all clusters and fill virtual cluster with a merge of
0371      * both
0372      */
0373     for_each_present_cpu(i) {
0374         struct device *cdev = get_cpu_device(i);
0375 
0376         if (!cdev)
0377             return -ENODEV;
0378 
0379         ret = _get_cluster_clk_and_freq_table(cdev, cpumask);
0380         if (ret)
0381             goto put_clusters;
0382     }
0383 
0384     ret = merge_cluster_tables();
0385     if (ret)
0386         goto put_clusters;
0387 
0388     /* Assuming 2 cluster, set clk_big_min and clk_little_max */
0389     clk_big_min = get_table_min(freq_table[A15_CLUSTER]);
0390     clk_little_max = VIRT_FREQ(A7_CLUSTER,
0391                    get_table_max(freq_table[A7_CLUSTER]));
0392 
0393     return 0;
0394 
0395 put_clusters:
0396     for_each_present_cpu(i) {
0397         struct device *cdev = get_cpu_device(i);
0398 
0399         if (!cdev)
0400             return -ENODEV;
0401 
0402         _put_cluster_clk_and_freq_table(cdev, cpumask);
0403     }
0404 
0405     atomic_dec(&cluster_usage[cluster]);
0406 
0407     return ret;
0408 }
0409 
0410 /* Per-CPU initialization */
0411 static int ve_spc_cpufreq_init(struct cpufreq_policy *policy)
0412 {
0413     u32 cur_cluster = cpu_to_cluster(policy->cpu);
0414     struct device *cpu_dev;
0415     int ret;
0416 
0417     cpu_dev = get_cpu_device(policy->cpu);
0418     if (!cpu_dev) {
0419         pr_err("%s: failed to get cpu%d device\n", __func__,
0420                policy->cpu);
0421         return -ENODEV;
0422     }
0423 
0424     if (cur_cluster < MAX_CLUSTERS) {
0425         int cpu;
0426 
0427         dev_pm_opp_get_sharing_cpus(cpu_dev, policy->cpus);
0428 
0429         for_each_cpu(cpu, policy->cpus)
0430             per_cpu(physical_cluster, cpu) = cur_cluster;
0431     } else {
0432         /* Assumption: during init, we are always running on A15 */
0433         per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER;
0434     }
0435 
0436     ret = get_cluster_clk_and_freq_table(cpu_dev, policy->cpus);
0437     if (ret)
0438         return ret;
0439 
0440     policy->freq_table = freq_table[cur_cluster];
0441     policy->cpuinfo.transition_latency = 1000000; /* 1 ms */
0442 
0443     if (is_bL_switching_enabled())
0444         per_cpu(cpu_last_req_freq, policy->cpu) =
0445                         clk_get_cpu_rate(policy->cpu);
0446 
0447     dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
0448     return 0;
0449 }
0450 
0451 static int ve_spc_cpufreq_exit(struct cpufreq_policy *policy)
0452 {
0453     struct device *cpu_dev;
0454 
0455     cpu_dev = get_cpu_device(policy->cpu);
0456     if (!cpu_dev) {
0457         pr_err("%s: failed to get cpu%d device\n", __func__,
0458                policy->cpu);
0459         return -ENODEV;
0460     }
0461 
0462     put_cluster_clk_and_freq_table(cpu_dev, policy->related_cpus);
0463     return 0;
0464 }
0465 
0466 static struct cpufreq_driver ve_spc_cpufreq_driver = {
0467     .name           = "vexpress-spc",
0468     .flags          = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
0469                     CPUFREQ_NEED_INITIAL_FREQ_CHECK,
0470     .verify         = cpufreq_generic_frequency_table_verify,
0471     .target_index       = ve_spc_cpufreq_set_target,
0472     .get            = ve_spc_cpufreq_get_rate,
0473     .init           = ve_spc_cpufreq_init,
0474     .exit           = ve_spc_cpufreq_exit,
0475     .register_em        = cpufreq_register_em_with_opp,
0476     .attr           = cpufreq_generic_attr,
0477 };
0478 
0479 #ifdef CONFIG_BL_SWITCHER
0480 static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb,
0481                     unsigned long action, void *_arg)
0482 {
0483     pr_debug("%s: action: %ld\n", __func__, action);
0484 
0485     switch (action) {
0486     case BL_NOTIFY_PRE_ENABLE:
0487     case BL_NOTIFY_PRE_DISABLE:
0488         cpufreq_unregister_driver(&ve_spc_cpufreq_driver);
0489         break;
0490 
0491     case BL_NOTIFY_POST_ENABLE:
0492         set_switching_enabled(true);
0493         cpufreq_register_driver(&ve_spc_cpufreq_driver);
0494         break;
0495 
0496     case BL_NOTIFY_POST_DISABLE:
0497         set_switching_enabled(false);
0498         cpufreq_register_driver(&ve_spc_cpufreq_driver);
0499         break;
0500 
0501     default:
0502         return NOTIFY_DONE;
0503     }
0504 
0505     return NOTIFY_OK;
0506 }
0507 
0508 static struct notifier_block bL_switcher_notifier = {
0509     .notifier_call = bL_cpufreq_switcher_notifier,
0510 };
0511 
0512 static int __bLs_register_notifier(void)
0513 {
0514     return bL_switcher_register_notifier(&bL_switcher_notifier);
0515 }
0516 
0517 static int __bLs_unregister_notifier(void)
0518 {
0519     return bL_switcher_unregister_notifier(&bL_switcher_notifier);
0520 }
0521 #else
0522 static int __bLs_register_notifier(void) { return 0; }
0523 static int __bLs_unregister_notifier(void) { return 0; }
0524 #endif
0525 
0526 static int ve_spc_cpufreq_probe(struct platform_device *pdev)
0527 {
0528     int ret, i;
0529 
0530     set_switching_enabled(bL_switcher_get_enabled());
0531 
0532     for (i = 0; i < MAX_CLUSTERS; i++)
0533         mutex_init(&cluster_lock[i]);
0534 
0535     if (!is_bL_switching_enabled())
0536         ve_spc_cpufreq_driver.flags |= CPUFREQ_IS_COOLING_DEV;
0537 
0538     ret = cpufreq_register_driver(&ve_spc_cpufreq_driver);
0539     if (ret) {
0540         pr_info("%s: Failed registering platform driver: %s, err: %d\n",
0541             __func__, ve_spc_cpufreq_driver.name, ret);
0542     } else {
0543         ret = __bLs_register_notifier();
0544         if (ret)
0545             cpufreq_unregister_driver(&ve_spc_cpufreq_driver);
0546         else
0547             pr_info("%s: Registered platform driver: %s\n",
0548                 __func__, ve_spc_cpufreq_driver.name);
0549     }
0550 
0551     bL_switcher_put_enabled();
0552     return ret;
0553 }
0554 
0555 static int ve_spc_cpufreq_remove(struct platform_device *pdev)
0556 {
0557     bL_switcher_get_enabled();
0558     __bLs_unregister_notifier();
0559     cpufreq_unregister_driver(&ve_spc_cpufreq_driver);
0560     bL_switcher_put_enabled();
0561     pr_info("%s: Un-registered platform driver: %s\n", __func__,
0562         ve_spc_cpufreq_driver.name);
0563     return 0;
0564 }
0565 
0566 static struct platform_driver ve_spc_cpufreq_platdrv = {
0567     .driver = {
0568         .name   = "vexpress-spc-cpufreq",
0569     },
0570     .probe      = ve_spc_cpufreq_probe,
0571     .remove     = ve_spc_cpufreq_remove,
0572 };
0573 module_platform_driver(ve_spc_cpufreq_platdrv);
0574 
0575 MODULE_ALIAS("platform:vexpress-spc-cpufreq");
0576 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
0577 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
0578 MODULE_DESCRIPTION("Vexpress SPC ARM big LITTLE cpufreq driver");
0579 MODULE_LICENSE("GPL v2");