1 /*
2  * This file provides the ACPI based P-state support. This
3  * module works with generic cpufreq infrastructure. Most of
4  * the code is based on i386 version
5  * (arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c)
6  *
7  * Copyright (C) 2005 Intel Corp
8  *      Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/cpufreq.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <asm/io.h>
21 #include <linux/uaccess.h>
22 #include <asm/pal.h>
23 
24 #include <linux/acpi.h>
25 #include <acpi/processor.h>
26 
27 MODULE_AUTHOR("Venkatesh Pallipadi");
28 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
29 MODULE_LICENSE("GPL");
30 
31 
32 struct cpufreq_acpi_io {
33 	struct acpi_processor_performance	acpi_data;
34 	unsigned int				resume;
35 };
36 
37 struct cpufreq_acpi_req {
38 	unsigned int		cpu;
39 	unsigned int		state;
40 };
41 
42 static struct cpufreq_acpi_io	*acpi_io_data[NR_CPUS];
43 
44 static struct cpufreq_driver acpi_cpufreq_driver;
45 
46 
47 static int
processor_set_pstate(u32 value)48 processor_set_pstate (
49 	u32	value)
50 {
51 	s64 retval;
52 
53 	pr_debug("processor_set_pstate\n");
54 
55 	retval = ia64_pal_set_pstate((u64)value);
56 
57 	if (retval) {
58 		pr_debug("Failed to set freq to 0x%x, with error 0x%lx\n",
59 		        value, retval);
60 		return -ENODEV;
61 	}
62 	return (int)retval;
63 }
64 
65 
66 static int
processor_get_pstate(u32 * value)67 processor_get_pstate (
68 	u32	*value)
69 {
70 	u64	pstate_index = 0;
71 	s64 	retval;
72 
73 	pr_debug("processor_get_pstate\n");
74 
75 	retval = ia64_pal_get_pstate(&pstate_index,
76 	                             PAL_GET_PSTATE_TYPE_INSTANT);
77 	*value = (u32) pstate_index;
78 
79 	if (retval)
80 		pr_debug("Failed to get current freq with "
81 			"error 0x%lx, idx 0x%x\n", retval, *value);
82 
83 	return (int)retval;
84 }
85 
86 
87 /* To be used only after data->acpi_data is initialized */
88 static unsigned
extract_clock(struct cpufreq_acpi_io * data,unsigned value)89 extract_clock (
90 	struct cpufreq_acpi_io *data,
91 	unsigned value)
92 {
93 	unsigned long i;
94 
95 	pr_debug("extract_clock\n");
96 
97 	for (i = 0; i < data->acpi_data.state_count; i++) {
98 		if (value == data->acpi_data.states[i].status)
99 			return data->acpi_data.states[i].core_frequency;
100 	}
101 	return data->acpi_data.states[i-1].core_frequency;
102 }
103 
104 
105 static long
processor_get_freq(void * arg)106 processor_get_freq (
107 	void *arg)
108 {
109 	struct cpufreq_acpi_req *req = arg;
110 	unsigned int		cpu = req->cpu;
111 	struct cpufreq_acpi_io	*data = acpi_io_data[cpu];
112 	u32			value;
113 	int			ret;
114 
115 	pr_debug("processor_get_freq\n");
116 	if (smp_processor_id() != cpu)
117 		return -EAGAIN;
118 
119 	/* processor_get_pstate gets the instantaneous frequency */
120 	ret = processor_get_pstate(&value);
121 	if (ret) {
122 		pr_warn("get performance failed with error %d\n", ret);
123 		return ret;
124 	}
125 	return 1000 * extract_clock(data, value);
126 }
127 
128 
129 static long
processor_set_freq(void * arg)130 processor_set_freq (
131 	void *arg)
132 {
133 	struct cpufreq_acpi_req *req = arg;
134 	unsigned int		cpu = req->cpu;
135 	struct cpufreq_acpi_io	*data = acpi_io_data[cpu];
136 	int			ret, state = req->state;
137 	u32			value;
138 
139 	pr_debug("processor_set_freq\n");
140 	if (smp_processor_id() != cpu)
141 		return -EAGAIN;
142 
143 	if (state == data->acpi_data.state) {
144 		if (unlikely(data->resume)) {
145 			pr_debug("Called after resume, resetting to P%d\n", state);
146 			data->resume = 0;
147 		} else {
148 			pr_debug("Already at target state (P%d)\n", state);
149 			return 0;
150 		}
151 	}
152 
153 	pr_debug("Transitioning from P%d to P%d\n",
154 		data->acpi_data.state, state);
155 
156 	/*
157 	 * First we write the target state's 'control' value to the
158 	 * control_register.
159 	 */
160 	value = (u32) data->acpi_data.states[state].control;
161 
162 	pr_debug("Transitioning to state: 0x%08x\n", value);
163 
164 	ret = processor_set_pstate(value);
165 	if (ret) {
166 		pr_warn("Transition failed with error %d\n", ret);
167 		return -ENODEV;
168 	}
169 
170 	data->acpi_data.state = state;
171 	return 0;
172 }
173 
174 
175 static unsigned int
acpi_cpufreq_get(unsigned int cpu)176 acpi_cpufreq_get (
177 	unsigned int		cpu)
178 {
179 	struct cpufreq_acpi_req req;
180 	long ret;
181 
182 	req.cpu = cpu;
183 	ret = work_on_cpu(cpu, processor_get_freq, &req);
184 
185 	return ret > 0 ? (unsigned int) ret : 0;
186 }
187 
188 
189 static int
acpi_cpufreq_target(struct cpufreq_policy * policy,unsigned int index)190 acpi_cpufreq_target (
191 	struct cpufreq_policy   *policy,
192 	unsigned int index)
193 {
194 	struct cpufreq_acpi_req req;
195 
196 	req.cpu = policy->cpu;
197 	req.state = index;
198 
199 	return work_on_cpu(req.cpu, processor_set_freq, &req);
200 }
201 
202 static int
acpi_cpufreq_cpu_init(struct cpufreq_policy * policy)203 acpi_cpufreq_cpu_init (
204 	struct cpufreq_policy   *policy)
205 {
206 	unsigned int		i;
207 	unsigned int		cpu = policy->cpu;
208 	struct cpufreq_acpi_io	*data;
209 	unsigned int		result = 0;
210 	struct cpufreq_frequency_table *freq_table;
211 
212 	pr_debug("acpi_cpufreq_cpu_init\n");
213 
214 	data = kzalloc(sizeof(*data), GFP_KERNEL);
215 	if (!data)
216 		return (-ENOMEM);
217 
218 	acpi_io_data[cpu] = data;
219 
220 	result = acpi_processor_register_performance(&data->acpi_data, cpu);
221 
222 	if (result)
223 		goto err_free;
224 
225 	/* capability check */
226 	if (data->acpi_data.state_count <= 1) {
227 		pr_debug("No P-States\n");
228 		result = -ENODEV;
229 		goto err_unreg;
230 	}
231 
232 	if ((data->acpi_data.control_register.space_id !=
233 					ACPI_ADR_SPACE_FIXED_HARDWARE) ||
234 	    (data->acpi_data.status_register.space_id !=
235 					ACPI_ADR_SPACE_FIXED_HARDWARE)) {
236 		pr_debug("Unsupported address space [%d, %d]\n",
237 			(u32) (data->acpi_data.control_register.space_id),
238 			(u32) (data->acpi_data.status_register.space_id));
239 		result = -ENODEV;
240 		goto err_unreg;
241 	}
242 
243 	/* alloc freq_table */
244 	freq_table = kcalloc(data->acpi_data.state_count + 1,
245 	                           sizeof(*freq_table),
246 	                           GFP_KERNEL);
247 	if (!freq_table) {
248 		result = -ENOMEM;
249 		goto err_unreg;
250 	}
251 
252 	/* detect transition latency */
253 	policy->cpuinfo.transition_latency = 0;
254 	for (i=0; i<data->acpi_data.state_count; i++) {
255 		if ((data->acpi_data.states[i].transition_latency * 1000) >
256 		    policy->cpuinfo.transition_latency) {
257 			policy->cpuinfo.transition_latency =
258 			    data->acpi_data.states[i].transition_latency * 1000;
259 		}
260 	}
261 
262 	/* table init */
263 	for (i = 0; i <= data->acpi_data.state_count; i++)
264 	{
265 		if (i < data->acpi_data.state_count) {
266 			freq_table[i].frequency =
267 			      data->acpi_data.states[i].core_frequency * 1000;
268 		} else {
269 			freq_table[i].frequency = CPUFREQ_TABLE_END;
270 		}
271 	}
272 
273 	policy->freq_table = freq_table;
274 
275 	/* notify BIOS that we exist */
276 	acpi_processor_notify_smm(THIS_MODULE);
277 
278 	pr_info("CPU%u - ACPI performance management activated\n", cpu);
279 
280 	for (i = 0; i < data->acpi_data.state_count; i++)
281 		pr_debug("     %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
282 			(i == data->acpi_data.state?'*':' '), i,
283 			(u32) data->acpi_data.states[i].core_frequency,
284 			(u32) data->acpi_data.states[i].power,
285 			(u32) data->acpi_data.states[i].transition_latency,
286 			(u32) data->acpi_data.states[i].bus_master_latency,
287 			(u32) data->acpi_data.states[i].status,
288 			(u32) data->acpi_data.states[i].control);
289 
290 	/* the first call to ->target() should result in us actually
291 	 * writing something to the appropriate registers. */
292 	data->resume = 1;
293 
294 	return (result);
295 
296  err_unreg:
297 	acpi_processor_unregister_performance(cpu);
298  err_free:
299 	kfree(data);
300 	acpi_io_data[cpu] = NULL;
301 
302 	return (result);
303 }
304 
305 
306 static int
acpi_cpufreq_cpu_exit(struct cpufreq_policy * policy)307 acpi_cpufreq_cpu_exit (
308 	struct cpufreq_policy   *policy)
309 {
310 	struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
311 
312 	pr_debug("acpi_cpufreq_cpu_exit\n");
313 
314 	if (data) {
315 		acpi_io_data[policy->cpu] = NULL;
316 		acpi_processor_unregister_performance(policy->cpu);
317 		kfree(policy->freq_table);
318 		kfree(data);
319 	}
320 
321 	return (0);
322 }
323 
324 
325 static struct cpufreq_driver acpi_cpufreq_driver = {
326 	.verify 	= cpufreq_generic_frequency_table_verify,
327 	.target_index	= acpi_cpufreq_target,
328 	.get 		= acpi_cpufreq_get,
329 	.init		= acpi_cpufreq_cpu_init,
330 	.exit		= acpi_cpufreq_cpu_exit,
331 	.name		= "acpi-cpufreq",
332 	.attr		= cpufreq_generic_attr,
333 };
334 
335 
336 static int __init
acpi_cpufreq_init(void)337 acpi_cpufreq_init (void)
338 {
339 	pr_debug("acpi_cpufreq_init\n");
340 
341  	return cpufreq_register_driver(&acpi_cpufreq_driver);
342 }
343 
344 
345 static void __exit
acpi_cpufreq_exit(void)346 acpi_cpufreq_exit (void)
347 {
348 	pr_debug("acpi_cpufreq_exit\n");
349 
350 	cpufreq_unregister_driver(&acpi_cpufreq_driver);
351 	return;
352 }
353 
354 
355 late_initcall(acpi_cpufreq_init);
356 module_exit(acpi_cpufreq_exit);
357 
358