libnvdimm/altmap: Track namespace boundaries in altmap
[linux-2.6-block.git] / drivers / acpi / processor_perflib.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * processor_perflib.c - ACPI Processor P-States Library ($Revision: 71 $)
4 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
8 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
9 * - Added processor hotplug support
1da177e4
LT
10 */
11
1da177e4
LT
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/cpufreq.h>
5a0e3ad6 16#include <linux/slab.h>
8b48463f
LZ
17#include <linux/acpi.h>
18#include <acpi/processor.h>
16be87ea 19#ifdef CONFIG_X86
910dfae2 20#include <asm/cpufeature.h>
16be87ea 21#endif
1da177e4 22
a192a958
LB
23#define PREFIX "ACPI: "
24
1da177e4 25#define ACPI_PROCESSOR_CLASS "processor"
1da177e4
LT
26#define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
27#define _COMPONENT ACPI_PROCESSOR_COMPONENT
f52fd66d 28ACPI_MODULE_NAME("processor_perflib");
1da177e4 29
65c19bbd 30static DEFINE_MUTEX(performance_mutex);
1da177e4
LT
31
32/*
33 * _PPC support is implemented as a CPUfreq policy notifier:
34 * This means each time a CPUfreq driver registered also with
35 * the ACPI core is asked to change the speed policy, the maximum
36 * value is adjusted so that it is within the platform limit.
37 *
38 * Also, when a new platform limit value is detected, the CPUfreq
39 * policy is adjusted accordingly.
40 */
41
a1531acd
TR
42/* ignore_ppc:
43 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
44 * ignore _PPC
45 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
46 * 1 -> ignore _PPC totally -> forced by user through boot param
47 */
9f497bcc 48static int ignore_ppc = -1;
613e5f33 49module_param(ignore_ppc, int, 0644);
623b78c3
TR
50MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
51 "limited by BIOS, this should help");
52
1da177e4
LT
53#define PPC_REGISTERED 1
54#define PPC_IN_USE 2
55
a1531acd 56static int acpi_processor_ppc_status;
1da177e4
LT
57
58static int acpi_processor_ppc_notifier(struct notifier_block *nb,
4be44fcd 59 unsigned long event, void *data)
1da177e4
LT
60{
61 struct cpufreq_policy *policy = data;
62 struct acpi_processor *pr;
63 unsigned int ppc = 0;
64
fa29ae5f 65 if (ignore_ppc < 0)
a1531acd 66 ignore_ppc = 0;
a1531acd 67
623b78c3
TR
68 if (ignore_ppc)
69 return 0;
70
6bfb7c74 71 if (event != CPUFREQ_ADJUST)
9b67c5d4
TR
72 return 0;
73
74 mutex_lock(&performance_mutex);
1da177e4 75
706546d0 76 pr = per_cpu(processors, policy->cpu);
1da177e4
LT
77 if (!pr || !pr->performance)
78 goto out;
79
4be44fcd 80 ppc = (unsigned int)pr->performance_platform_limit;
1da177e4 81
0916bd3e 82 if (ppc >= pr->performance->state_count)
1da177e4
LT
83 goto out;
84
85 cpufreq_verify_within_limits(policy, 0,
4be44fcd
LB
86 pr->performance->states[ppc].
87 core_frequency * 1000);
1da177e4 88
4be44fcd 89 out:
65c19bbd 90 mutex_unlock(&performance_mutex);
1da177e4
LT
91
92 return 0;
93}
94
1da177e4
LT
95static struct notifier_block acpi_ppc_notifier_block = {
96 .notifier_call = acpi_processor_ppc_notifier,
97};
98
4be44fcd 99static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
1da177e4 100{
4be44fcd 101 acpi_status status = 0;
27663c58 102 unsigned long long ppc = 0;
1da177e4 103
1da177e4
LT
104
105 if (!pr)
d550d98d 106 return -EINVAL;
1da177e4
LT
107
108 /*
109 * _PPC indicates the maximum state currently supported by the platform
110 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
111 */
112 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
113
114 if (status != AE_NOT_FOUND)
115 acpi_processor_ppc_status |= PPC_IN_USE;
116
4be44fcd 117 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
a6fc6720 118 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
d550d98d 119 return -ENODEV;
1da177e4
LT
120 }
121
2d06d8c4 122 pr_debug("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
919158d1
TR
123 (int)ppc, ppc ? "" : "not");
124
4be44fcd 125 pr->performance_platform_limit = (int)ppc;
1da177e4 126
d550d98d 127 return 0;
1da177e4
LT
128}
129
d81c45e1
ZY
130#define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
131/*
132 * acpi_processor_ppc_ost: Notify firmware the _PPC evaluation status
133 * @handle: ACPI processor handle
134 * @status: the status code of _PPC evaluation
135 * 0: success. OSPM is now using the performance state specificed.
136 * 1: failure. OSPM has not changed the number of P-states in use
137 */
138static void acpi_processor_ppc_ost(acpi_handle handle, int status)
139{
4a6172a4
JL
140 if (acpi_has_method(handle, "_OST"))
141 acpi_evaluate_ost(handle, ACPI_PROCESSOR_NOTIFY_PERFORMANCE,
142 status, NULL);
d81c45e1
ZY
143}
144
bca5f557 145void acpi_processor_ppc_has_changed(struct acpi_processor *pr, int event_flag)
1da177e4 146{
623b78c3
TR
147 int ret;
148
ba1edb9a 149 if (ignore_ppc || !pr->performance) {
d81c45e1
ZY
150 /*
151 * Only when it is notification event, the _OST object
152 * will be evaluated. Otherwise it is skipped.
153 */
154 if (event_flag)
155 acpi_processor_ppc_ost(pr->handle, 1);
bca5f557 156 return;
d81c45e1 157 }
623b78c3
TR
158
159 ret = acpi_processor_get_platform_limit(pr);
d81c45e1
ZY
160 /*
161 * Only when it is notification event, the _OST object
162 * will be evaluated. Otherwise it is skipped.
163 */
164 if (event_flag) {
165 if (ret < 0)
166 acpi_processor_ppc_ost(pr->handle, 1);
167 else
168 acpi_processor_ppc_ost(pr->handle, 0);
169 }
bca5f557 170 if (ret >= 0)
5a25e3f7 171 cpufreq_update_limits(pr->id);
1da177e4
LT
172}
173
e2f74f35
TR
174int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
175{
176 struct acpi_processor *pr;
177
178 pr = per_cpu(processors, cpu);
179 if (!pr || !pr->performance || !pr->performance->state_count)
180 return -ENODEV;
181 *limit = pr->performance->states[pr->performance_platform_limit].
182 core_frequency * 1000;
183 return 0;
184}
185EXPORT_SYMBOL(acpi_processor_get_bios_limit);
186
4be44fcd
LB
187void acpi_processor_ppc_init(void)
188{
189 if (!cpufreq_register_notifier
190 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
1da177e4
LT
191 acpi_processor_ppc_status |= PPC_REGISTERED;
192 else
4be44fcd
LB
193 printk(KERN_DEBUG
194 "Warning: Processor Platform Limit not supported.\n");
1da177e4
LT
195}
196
4be44fcd
LB
197void acpi_processor_ppc_exit(void)
198{
1da177e4 199 if (acpi_processor_ppc_status & PPC_REGISTERED)
4be44fcd
LB
200 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
201 CPUFREQ_POLICY_NOTIFIER);
1da177e4
LT
202
203 acpi_processor_ppc_status &= ~PPC_REGISTERED;
204}
205
4be44fcd 206static int acpi_processor_get_performance_control(struct acpi_processor *pr)
1da177e4 207{
4be44fcd
LB
208 int result = 0;
209 acpi_status status = 0;
210 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
211 union acpi_object *pct = NULL;
212 union acpi_object obj = { 0 };
1da177e4 213
1da177e4
LT
214
215 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
4be44fcd 216 if (ACPI_FAILURE(status)) {
a6fc6720 217 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
d550d98d 218 return -ENODEV;
1da177e4
LT
219 }
220
4be44fcd 221 pct = (union acpi_object *)buffer.pointer;
1da177e4 222 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
4be44fcd 223 || (pct->package.count != 2)) {
6468463a 224 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
1da177e4
LT
225 result = -EFAULT;
226 goto end;
227 }
228
229 /*
230 * control_register
231 */
232
233 obj = pct->package.elements[0];
234
235 if ((obj.type != ACPI_TYPE_BUFFER)
4be44fcd
LB
236 || (obj.buffer.length < sizeof(struct acpi_pct_register))
237 || (obj.buffer.pointer == NULL)) {
6468463a 238 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
1da177e4
LT
239 result = -EFAULT;
240 goto end;
241 }
4be44fcd
LB
242 memcpy(&pr->performance->control_register, obj.buffer.pointer,
243 sizeof(struct acpi_pct_register));
1da177e4
LT
244
245 /*
246 * status_register
247 */
248
249 obj = pct->package.elements[1];
250
251 if ((obj.type != ACPI_TYPE_BUFFER)
4be44fcd
LB
252 || (obj.buffer.length < sizeof(struct acpi_pct_register))
253 || (obj.buffer.pointer == NULL)) {
6468463a 254 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
1da177e4
LT
255 result = -EFAULT;
256 goto end;
257 }
258
4be44fcd
LB
259 memcpy(&pr->performance->status_register, obj.buffer.pointer,
260 sizeof(struct acpi_pct_register));
1da177e4 261
4be44fcd 262 end:
02438d87 263 kfree(buffer.pointer);
1da177e4 264
d550d98d 265 return result;
1da177e4
LT
266}
267
f594065f
MG
268#ifdef CONFIG_X86
269/*
270 * Some AMDs have 50MHz frequency multiples, but only provide 100MHz rounding
271 * in their ACPI data. Calculate the real values and fix up the _PSS data.
272 */
273static void amd_fixup_frequency(struct acpi_processor_px *px, int i)
274{
275 u32 hi, lo, fid, did;
276 int index = px->control & 0x00000007;
277
278 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
279 return;
280
281 if ((boot_cpu_data.x86 == 0x10 && boot_cpu_data.x86_model < 10)
282 || boot_cpu_data.x86 == 0x11) {
283 rdmsr(MSR_AMD_PSTATE_DEF_BASE + index, lo, hi);
9855d8ce
SB
284 /*
285 * MSR C001_0064+:
286 * Bit 63: PstateEn. Read-write. If set, the P-state is valid.
287 */
288 if (!(hi & BIT(31)))
289 return;
290
f594065f
MG
291 fid = lo & 0x3f;
292 did = (lo >> 6) & 7;
293 if (boot_cpu_data.x86 == 0x10)
294 px->core_frequency = (100 * (fid + 0x10)) >> did;
295 else
296 px->core_frequency = (100 * (fid + 8)) >> did;
297 }
298}
299#else
300static void amd_fixup_frequency(struct acpi_processor_px *px, int i) {};
301#endif
302
4be44fcd 303static int acpi_processor_get_performance_states(struct acpi_processor *pr)
1da177e4 304{
4be44fcd
LB
305 int result = 0;
306 acpi_status status = AE_OK;
307 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
308 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
309 struct acpi_buffer state = { 0, NULL };
310 union acpi_object *pss = NULL;
311 int i;
d8e725f3 312 int last_invalid = -1;
1da177e4 313
1da177e4
LT
314
315 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
4be44fcd 316 if (ACPI_FAILURE(status)) {
a6fc6720 317 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
d550d98d 318 return -ENODEV;
1da177e4
LT
319 }
320
50dd0969 321 pss = buffer.pointer;
1da177e4 322 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
6468463a 323 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
1da177e4
LT
324 result = -EFAULT;
325 goto end;
326 }
327
328 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
4be44fcd 329 pss->package.count));
1da177e4
LT
330
331 pr->performance->state_count = pss->package.count;
4be44fcd 332 pr->performance->states =
6da2ec56
KC
333 kmalloc_array(pss->package.count,
334 sizeof(struct acpi_processor_px),
335 GFP_KERNEL);
1da177e4
LT
336 if (!pr->performance->states) {
337 result = -ENOMEM;
338 goto end;
339 }
340
341 for (i = 0; i < pr->performance->state_count; i++) {
342
343 struct acpi_processor_px *px = &(pr->performance->states[i]);
344
345 state.length = sizeof(struct acpi_processor_px);
346 state.pointer = px;
347
348 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
349
350 status = acpi_extract_package(&(pss->package.elements[i]),
4be44fcd 351 &format, &state);
1da177e4 352 if (ACPI_FAILURE(status)) {
a6fc6720 353 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
1da177e4
LT
354 result = -EFAULT;
355 kfree(pr->performance->states);
356 goto end;
357 }
358
f594065f
MG
359 amd_fixup_frequency(px, i);
360
1da177e4 361 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd
LB
362 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
363 i,
364 (u32) px->core_frequency,
365 (u32) px->power,
366 (u32) px->transition_latency,
367 (u32) px->bus_master_latency,
368 (u32) px->control, (u32) px->status));
1da177e4 369
34d531e6
LB
370 /*
371 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
372 */
373 if (!px->core_frequency ||
374 ((u32)(px->core_frequency * 1000) !=
375 (px->core_frequency * 1000))) {
376 printk(KERN_ERR FW_BUG PREFIX
d8e725f3
MAC
377 "Invalid BIOS _PSS frequency found for processor %d: 0x%llx MHz\n",
378 pr->id, px->core_frequency);
379 if (last_invalid == -1)
380 last_invalid = i;
381 } else {
382 if (last_invalid != -1) {
383 /*
384 * Copy this valid entry over last_invalid entry
385 */
386 memcpy(&(pr->performance->states[last_invalid]),
387 px, sizeof(struct acpi_processor_px));
388 ++last_invalid;
389 }
1da177e4
LT
390 }
391 }
392
d8e725f3
MAC
393 if (last_invalid == 0) {
394 printk(KERN_ERR FW_BUG PREFIX
395 "No valid BIOS _PSS frequency found for processor %d\n", pr->id);
396 result = -EFAULT;
397 kfree(pr->performance->states);
398 pr->performance->states = NULL;
399 }
400
401 if (last_invalid > 0)
402 pr->performance->state_count = last_invalid;
403
4be44fcd 404 end:
02438d87 405 kfree(buffer.pointer);
1da177e4 406
d550d98d 407 return result;
1da177e4
LT
408}
409
c705c78c 410int acpi_processor_get_performance_info(struct acpi_processor *pr)
1da177e4 411{
4be44fcd 412 int result = 0;
1da177e4 413
1da177e4 414 if (!pr || !pr->performance || !pr->handle)
d550d98d 415 return -EINVAL;
1da177e4 416
952c63e9 417 if (!acpi_has_method(pr->handle, "_PCT")) {
1da177e4 418 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 419 "ACPI-based processor performance control unavailable\n"));
d550d98d 420 return -ENODEV;
1da177e4
LT
421 }
422
423 result = acpi_processor_get_performance_control(pr);
424 if (result)
910dfae2 425 goto update_bios;
1da177e4
LT
426
427 result = acpi_processor_get_performance_states(pr);
428 if (result)
910dfae2 429 goto update_bios;
1da177e4 430
455c0d71
DW
431 /* We need to call _PPC once when cpufreq starts */
432 if (ignore_ppc != 1)
433 result = acpi_processor_get_platform_limit(pr);
434
435 return result;
910dfae2
TR
436
437 /*
438 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
439 * the BIOS is older than the CPU and does not know its frequencies
440 */
441 update_bios:
16be87ea 442#ifdef CONFIG_X86
952c63e9 443 if (acpi_has_method(pr->handle, "_PPC")) {
910dfae2
TR
444 if(boot_cpu_has(X86_FEATURE_EST))
445 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
446 "frequency support\n");
447 }
16be87ea 448#endif
910dfae2 449 return result;
1da177e4 450}
c705c78c 451EXPORT_SYMBOL_GPL(acpi_processor_get_performance_info);
d0ea59e1
RW
452
453int acpi_processor_pstate_control(void)
4be44fcd
LB
454{
455 acpi_status status;
1da177e4 456
d0ea59e1
RW
457 if (!acpi_gbl_FADT.smi_command || !acpi_gbl_FADT.pstate_control)
458 return 0;
459
460 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
461 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
462 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
463
464 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
465 (u32)acpi_gbl_FADT.pstate_control, 8);
466 if (ACPI_SUCCESS(status))
467 return 1;
468
469 ACPI_EXCEPTION((AE_INFO, status,
470 "Failed to write pstate_control [0x%x] to smi_command [0x%x]",
471 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
472 return -EIO;
473}
474
475int acpi_processor_notify_smm(struct module *calling_module)
476{
477 static int is_done = 0;
478 int result;
1da177e4
LT
479
480 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
d550d98d 481 return -EBUSY;
1da177e4
LT
482
483 if (!try_module_get(calling_module))
d550d98d 484 return -EINVAL;
1da177e4 485
58f87ed0
LDM
486 /* is_done is set to negative if an error occurred,
487 * and to postitive if _no_ error occurred, but SMM
1da177e4
LT
488 * was already notified. This avoids double notification
489 * which might lead to unexpected results...
490 */
491 if (is_done > 0) {
492 module_put(calling_module);
d550d98d 493 return 0;
4be44fcd 494 } else if (is_done < 0) {
1da177e4 495 module_put(calling_module);
d550d98d 496 return is_done;
1da177e4
LT
497 }
498
499 is_done = -EIO;
500
d0ea59e1
RW
501 result = acpi_processor_pstate_control();
502 if (!result) {
ad71860a 503 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
1da177e4 504 module_put(calling_module);
d550d98d 505 return 0;
1da177e4 506 }
d0ea59e1 507 if (result < 0) {
1da177e4 508 module_put(calling_module);
d0ea59e1 509 return result;
1da177e4
LT
510 }
511
512 /* Success. If there's no _PPC, we need to fear nothing, so
513 * we can allow the cpufreq driver to be rmmod'ed. */
514 is_done = 1;
515
516 if (!(acpi_processor_ppc_status & PPC_IN_USE))
517 module_put(calling_module);
518
d550d98d 519 return 0;
1da177e4 520}
1da177e4 521
4be44fcd 522EXPORT_SYMBOL(acpi_processor_notify_smm);
1da177e4 523
4d0f1ce6 524int acpi_processor_get_psd(acpi_handle handle, struct acpi_psd_package *pdomain)
3b2d9942
VP
525{
526 int result = 0;
527 acpi_status status = AE_OK;
528 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
529 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
530 struct acpi_buffer state = {0, NULL};
531 union acpi_object *psd = NULL;
3b2d9942 532
4d0f1ce6 533 status = acpi_evaluate_object(handle, "_PSD", NULL, &buffer);
3b2d9942 534 if (ACPI_FAILURE(status)) {
9011bff4 535 return -ENODEV;
3b2d9942
VP
536 }
537
50dd0969 538 psd = buffer.pointer;
3b2d9942 539 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
55ac9a01 540 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
3b2d9942
VP
541 result = -EFAULT;
542 goto end;
543 }
544
545 if (psd->package.count != 1) {
55ac9a01 546 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
3b2d9942
VP
547 result = -EFAULT;
548 goto end;
549 }
550
3b2d9942
VP
551 state.length = sizeof(struct acpi_psd_package);
552 state.pointer = pdomain;
553
554 status = acpi_extract_package(&(psd->package.elements[0]),
555 &format, &state);
556 if (ACPI_FAILURE(status)) {
55ac9a01 557 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
3b2d9942
VP
558 result = -EFAULT;
559 goto end;
560 }
561
562 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
55ac9a01 563 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
3b2d9942
VP
564 result = -EFAULT;
565 goto end;
566 }
567
568 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
55ac9a01 569 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
3b2d9942
VP
570 result = -EFAULT;
571 goto end;
572 }
573
e1eb4779
SG
574 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
575 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
576 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
577 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
578 result = -EFAULT;
579 goto end;
580 }
3b2d9942 581end:
02438d87 582 kfree(buffer.pointer);
9011bff4 583 return result;
3b2d9942 584}
4d0f1ce6 585EXPORT_SYMBOL(acpi_processor_get_psd);
3b2d9942
VP
586
587int acpi_processor_preregister_performance(
a29d8b8e 588 struct acpi_processor_performance __percpu *performance)
3b2d9942 589{
09d5ca80 590 int count_target;
3b2d9942
VP
591 int retval = 0;
592 unsigned int i, j;
2fdf66b4 593 cpumask_var_t covered_cpus;
3b2d9942
VP
594 struct acpi_processor *pr;
595 struct acpi_psd_package *pdomain;
596 struct acpi_processor *match_pr;
597 struct acpi_psd_package *match_pdomain;
598
79f55997 599 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
2fdf66b4
RR
600 return -ENOMEM;
601
785fcccd 602 mutex_lock(&performance_mutex);
3b2d9942 603
e1eb4779
SG
604 /*
605 * Check if another driver has already registered, and abort before
606 * changing pr->performance if it has. Check input data as well.
607 */
193de0c7 608 for_each_possible_cpu(i) {
706546d0 609 pr = per_cpu(processors, i);
3b2d9942
VP
610 if (!pr) {
611 /* Look only at processors in ACPI namespace */
612 continue;
613 }
614
615 if (pr->performance) {
616 retval = -EBUSY;
e1eb4779 617 goto err_out;
3b2d9942
VP
618 }
619
b36128c8 620 if (!performance || !per_cpu_ptr(performance, i)) {
3b2d9942 621 retval = -EINVAL;
e1eb4779 622 goto err_out;
3b2d9942 623 }
e1eb4779
SG
624 }
625
626 /* Call _PSD for all CPUs */
627 for_each_possible_cpu(i) {
628 pr = per_cpu(processors, i);
629 if (!pr)
630 continue;
3b2d9942 631
b36128c8 632 pr->performance = per_cpu_ptr(performance, i);
2fdf66b4 633 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
4d0f1ce6
JM
634 pdomain = &(pr->performance->domain_info);
635 if (acpi_processor_get_psd(pr->handle, pdomain)) {
3b2d9942
VP
636 retval = -EINVAL;
637 continue;
638 }
639 }
640 if (retval)
641 goto err_ret;
642
643 /*
644 * Now that we have _PSD data from all CPUs, lets setup P-state
645 * domain info.
646 */
193de0c7 647 for_each_possible_cpu(i) {
706546d0 648 pr = per_cpu(processors, i);
3b2d9942
VP
649 if (!pr)
650 continue;
651
2fdf66b4 652 if (cpumask_test_cpu(i, covered_cpus))
3b2d9942
VP
653 continue;
654
655 pdomain = &(pr->performance->domain_info);
2fdf66b4
RR
656 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
657 cpumask_set_cpu(i, covered_cpus);
3b2d9942
VP
658 if (pdomain->num_processors <= 1)
659 continue;
660
661 /* Validate the Domain info */
662 count_target = pdomain->num_processors;
46f18e3a 663 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
3b2d9942 664 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
46f18e3a
VP
665 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
666 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
667 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
3b2d9942 668 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
3b2d9942 669
193de0c7 670 for_each_possible_cpu(j) {
3b2d9942
VP
671 if (i == j)
672 continue;
673
706546d0 674 match_pr = per_cpu(processors, j);
3b2d9942
VP
675 if (!match_pr)
676 continue;
677
678 match_pdomain = &(match_pr->performance->domain_info);
679 if (match_pdomain->domain != pdomain->domain)
680 continue;
681
682 /* Here i and j are in the same domain */
683
684 if (match_pdomain->num_processors != count_target) {
685 retval = -EINVAL;
686 goto err_ret;
687 }
688
689 if (pdomain->coord_type != match_pdomain->coord_type) {
690 retval = -EINVAL;
691 goto err_ret;
692 }
693
2fdf66b4
RR
694 cpumask_set_cpu(j, covered_cpus);
695 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
3b2d9942
VP
696 }
697
193de0c7 698 for_each_possible_cpu(j) {
3b2d9942
VP
699 if (i == j)
700 continue;
701
706546d0 702 match_pr = per_cpu(processors, j);
3b2d9942
VP
703 if (!match_pr)
704 continue;
705
706 match_pdomain = &(match_pr->performance->domain_info);
707 if (match_pdomain->domain != pdomain->domain)
708 continue;
709
710 match_pr->performance->shared_type =
711 pr->performance->shared_type;
2fdf66b4
RR
712 cpumask_copy(match_pr->performance->shared_cpu_map,
713 pr->performance->shared_cpu_map);
3b2d9942
VP
714 }
715 }
716
717err_ret:
193de0c7 718 for_each_possible_cpu(i) {
706546d0 719 pr = per_cpu(processors, i);
3b2d9942
VP
720 if (!pr || !pr->performance)
721 continue;
722
723 /* Assume no coordination on any error parsing domain info */
724 if (retval) {
2fdf66b4
RR
725 cpumask_clear(pr->performance->shared_cpu_map);
726 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
3b2d9942
VP
727 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
728 }
729 pr->performance = NULL; /* Will be set for real in register */
730 }
731
e1eb4779 732err_out:
785fcccd 733 mutex_unlock(&performance_mutex);
2fdf66b4 734 free_cpumask_var(covered_cpus);
9011bff4 735 return retval;
3b2d9942
VP
736}
737EXPORT_SYMBOL(acpi_processor_preregister_performance);
738
1da177e4 739int
4be44fcd
LB
740acpi_processor_register_performance(struct acpi_processor_performance
741 *performance, unsigned int cpu)
1da177e4
LT
742{
743 struct acpi_processor *pr;
744
1da177e4 745 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
d550d98d 746 return -EINVAL;
1da177e4 747
65c19bbd 748 mutex_lock(&performance_mutex);
1da177e4 749
706546d0 750 pr = per_cpu(processors, cpu);
1da177e4 751 if (!pr) {
65c19bbd 752 mutex_unlock(&performance_mutex);
d550d98d 753 return -ENODEV;
1da177e4
LT
754 }
755
756 if (pr->performance) {
65c19bbd 757 mutex_unlock(&performance_mutex);
d550d98d 758 return -EBUSY;
1da177e4
LT
759 }
760
a913f507
AM
761 WARN_ON(!performance);
762
1da177e4
LT
763 pr->performance = performance;
764
765 if (acpi_processor_get_performance_info(pr)) {
766 pr->performance = NULL;
65c19bbd 767 mutex_unlock(&performance_mutex);
d550d98d 768 return -EIO;
1da177e4
LT
769 }
770
65c19bbd 771 mutex_unlock(&performance_mutex);
d550d98d 772 return 0;
1da177e4 773}
1da177e4 774
4be44fcd 775EXPORT_SYMBOL(acpi_processor_register_performance);
1da177e4 776
b2f8dc4c 777void acpi_processor_unregister_performance(unsigned int cpu)
1da177e4
LT
778{
779 struct acpi_processor *pr;
780
65c19bbd 781 mutex_lock(&performance_mutex);
1da177e4 782
706546d0 783 pr = per_cpu(processors, cpu);
1da177e4 784 if (!pr) {
65c19bbd 785 mutex_unlock(&performance_mutex);
d550d98d 786 return;
1da177e4
LT
787 }
788
a913f507
AM
789 if (pr->performance)
790 kfree(pr->performance->states);
1da177e4
LT
791 pr->performance = NULL;
792
65c19bbd 793 mutex_unlock(&performance_mutex);
1da177e4 794
d550d98d 795 return;
1da177e4 796}
4be44fcd 797
1da177e4 798EXPORT_SYMBOL(acpi_processor_unregister_performance);