Thomas Gleixner | de6cc65 | 2019-05-27 08:55:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 2 | /* |
| 3 | * pmi backend for the cbe_cpufreq driver |
| 4 | * |
| 5 | * (C) Copyright IBM Deutschland Entwicklung GmbH 2005-2007 |
| 6 | * |
| 7 | * Author: Christian Krafft <krafft@de.ibm.com> |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/timer.h> |
Paul Gortmaker | dbbe972 | 2016-03-27 18:08:17 -0400 | [diff] [blame] | 13 | #include <linux/init.h> |
Jon Loeliger | d8caf74 | 2007-11-13 11:10:58 -0600 | [diff] [blame] | 14 | #include <linux/of_platform.h> |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 15 | #include <linux/pm_qos.h> |
Jon Loeliger | d8caf74 | 2007-11-13 11:10:58 -0600 | [diff] [blame] | 16 | |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 17 | #include <asm/processor.h> |
| 18 | #include <asm/prom.h> |
| 19 | #include <asm/pmi.h> |
Benjamin Herrenschmidt | eef686a0 | 2007-10-04 15:40:42 +1000 | [diff] [blame] | 20 | #include <asm/cell-regs.h> |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 21 | |
| 22 | #ifdef DEBUG |
| 23 | #include <asm/time.h> |
| 24 | #endif |
| 25 | |
Viresh Kumar | 6eb1c37 | 2013-03-25 11:20:23 +0530 | [diff] [blame] | 26 | #include "ppc_cbe_cpufreq.h" |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 27 | |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 28 | bool cbe_cpufreq_has_pmi = false; |
| 29 | EXPORT_SYMBOL_GPL(cbe_cpufreq_has_pmi); |
| 30 | |
| 31 | /* |
| 32 | * hardware specific functions |
| 33 | */ |
| 34 | |
| 35 | int cbe_cpufreq_set_pmode_pmi(int cpu, unsigned int pmode) |
| 36 | { |
| 37 | int ret; |
| 38 | pmi_message_t pmi_msg; |
| 39 | #ifdef DEBUG |
| 40 | long time; |
| 41 | #endif |
| 42 | pmi_msg.type = PMI_TYPE_FREQ_CHANGE; |
| 43 | pmi_msg.data1 = cbe_cpu_to_node(cpu); |
| 44 | pmi_msg.data2 = pmode; |
| 45 | |
| 46 | #ifdef DEBUG |
| 47 | time = jiffies; |
| 48 | #endif |
| 49 | pmi_send_message(pmi_msg); |
| 50 | |
| 51 | #ifdef DEBUG |
| 52 | time = jiffies - time; |
| 53 | time = jiffies_to_msecs(time); |
| 54 | pr_debug("had to wait %lu ms for a transition using " \ |
| 55 | "PMI\n", time); |
| 56 | #endif |
| 57 | ret = pmi_msg.data2; |
| 58 | pr_debug("PMI returned slow mode %d\n", ret); |
| 59 | |
| 60 | return ret; |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(cbe_cpufreq_set_pmode_pmi); |
| 63 | |
| 64 | |
| 65 | static void cbe_cpufreq_handle_pmi(pmi_message_t pmi_msg) |
| 66 | { |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 67 | struct cpufreq_policy *policy; |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 68 | struct freq_qos_request *req; |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 69 | u8 node, slow_mode; |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 70 | int cpu, ret; |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 71 | |
| 72 | BUG_ON(pmi_msg.type != PMI_TYPE_FREQ_CHANGE); |
| 73 | |
| 74 | node = pmi_msg.data1; |
| 75 | slow_mode = pmi_msg.data2; |
| 76 | |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 77 | cpu = cbe_node_to_cpu(node); |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 78 | |
| 79 | pr_debug("cbe_handle_pmi: node: %d max_freq: %d\n", node, slow_mode); |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 80 | |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 81 | policy = cpufreq_cpu_get(cpu); |
| 82 | if (!policy) { |
| 83 | pr_warn("cpufreq policy not found cpu%d\n", cpu); |
| 84 | return; |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 87 | req = policy->driver_data; |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 88 | |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 89 | ret = freq_qos_update_request(req, |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 90 | policy->freq_table[slow_mode].frequency); |
| 91 | if (ret < 0) |
| 92 | pr_warn("Failed to update freq constraint: %d\n", ret); |
| 93 | else |
| 94 | pr_debug("limiting node %d to slow mode %d\n", node, slow_mode); |
| 95 | |
| 96 | cpufreq_cpu_put(policy); |
| 97 | } |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 98 | |
| 99 | static struct pmi_handler cbe_pmi_handler = { |
| 100 | .type = PMI_TYPE_FREQ_CHANGE, |
| 101 | .handle_pmi_message = cbe_cpufreq_handle_pmi, |
| 102 | }; |
| 103 | |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 104 | void cbe_cpufreq_pmi_policy_init(struct cpufreq_policy *policy) |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 105 | { |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 106 | struct freq_qos_request *req; |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 107 | int ret; |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 108 | |
| 109 | if (!cbe_cpufreq_has_pmi) |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 110 | return; |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 111 | |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 112 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 113 | if (!req) |
| 114 | return; |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 115 | |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 116 | ret = freq_qos_add_request(&policy->constraints, req, FREQ_QOS_MAX, |
| 117 | policy->freq_table[0].frequency); |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 118 | if (ret < 0) { |
| 119 | pr_err("Failed to add freq constraint (%d)\n", ret); |
| 120 | kfree(req); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | policy->driver_data = req; |
Christian Krafft | 74889e4 | 2007-07-20 21:39:22 +0200 | [diff] [blame] | 125 | } |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 126 | EXPORT_SYMBOL_GPL(cbe_cpufreq_pmi_policy_init); |
| 127 | |
| 128 | void cbe_cpufreq_pmi_policy_exit(struct cpufreq_policy *policy) |
| 129 | { |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 130 | struct freq_qos_request *req = policy->driver_data; |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 131 | |
| 132 | if (cbe_cpufreq_has_pmi) { |
Rafael J. Wysocki | 3000ce3 | 2019-10-16 12:47:06 +0200 | [diff] [blame] | 133 | freq_qos_remove_request(req); |
Viresh Kumar | afe9690 | 2019-07-05 15:49:48 +0530 | [diff] [blame] | 134 | kfree(req); |
| 135 | } |
| 136 | } |
| 137 | EXPORT_SYMBOL_GPL(cbe_cpufreq_pmi_policy_exit); |
| 138 | |
| 139 | void cbe_cpufreq_pmi_init(void) |
| 140 | { |
| 141 | if (!pmi_register_handler(&cbe_pmi_handler)) |
| 142 | cbe_cpufreq_has_pmi = true; |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(cbe_cpufreq_pmi_init); |
| 145 | |
| 146 | void cbe_cpufreq_pmi_exit(void) |
| 147 | { |
| 148 | pmi_unregister_handler(&cbe_pmi_handler); |
| 149 | cbe_cpufreq_has_pmi = false; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(cbe_cpufreq_pmi_exit); |