Commit | Line | Data |
---|---|---|
59a56802 KRW |
1 | /* |
2 | * Copyright 2012 by Oracle Inc | |
3 | * Author: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> | |
4 | * | |
5 | * This code borrows ideas from https://lkml.org/lkml/2011/11/30/249 | |
6 | * so many thanks go to Kevin Tian <kevin.tian@intel.com> | |
7 | * and Yu Ke <ke.yu@intel.com>. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms and conditions of the GNU General Public License, | |
11 | * version 2, as published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope it will be useful, but WITHOUT | |
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 | * more details. | |
17 | * | |
18 | */ | |
19 | ||
283c0972 JP |
20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
21 | ||
59a56802 KRW |
22 | #include <linux/cpumask.h> |
23 | #include <linux/cpufreq.h> | |
24 | #include <linux/freezer.h> | |
25 | #include <linux/kernel.h> | |
26 | #include <linux/kthread.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/types.h> | |
1914f0cd | 30 | #include <linux/syscore_ops.h> |
8b48463f | 31 | #include <linux/acpi.h> |
59a56802 | 32 | #include <acpi/processor.h> |
323f90a6 | 33 | #include <xen/xen.h> |
59a56802 KRW |
34 | #include <xen/interface/platform.h> |
35 | #include <asm/xen/hypercall.h> | |
36 | ||
59a56802 KRW |
37 | static int no_hypercall; |
38 | MODULE_PARM_DESC(off, "Inhibit the hypercall."); | |
39 | module_param_named(off, no_hypercall, int, 0400); | |
40 | ||
41 | /* | |
42 | * Note: Do not convert the acpi_id* below to cpumask_var_t or use cpumask_bit | |
43 | * - as those shrink to nr_cpu_bits (which is dependent on possible_cpu), which | |
44 | * can be less than what we want to put in. Instead use the 'nr_acpi_bits' | |
45 | * which is dynamically computed based on the MADT or x2APIC table. | |
46 | */ | |
47 | static unsigned int nr_acpi_bits; | |
48 | /* Mutex to protect the acpi_ids_done - for CPU hotplug use. */ | |
49 | static DEFINE_MUTEX(acpi_ids_mutex); | |
50 | /* Which ACPI ID we have processed from 'struct acpi_processor'. */ | |
51 | static unsigned long *acpi_ids_done; | |
52 | /* Which ACPI ID exist in the SSDT/DSDT processor definitions. */ | |
3fac1014 | 53 | static unsigned long *acpi_id_present; |
59a56802 | 54 | /* And if there is an _CST definition (or a PBLK) for the ACPI IDs */ |
3fac1014 | 55 | static unsigned long *acpi_id_cst_present; |
4d0f1ce6 JM |
56 | /* Which ACPI P-State dependencies for a enumerated processor */ |
57 | static struct acpi_psd_package *acpi_psd; | |
59a56802 KRW |
58 | |
59 | static int push_cxx_to_hypervisor(struct acpi_processor *_pr) | |
60 | { | |
61 | struct xen_platform_op op = { | |
62 | .cmd = XENPF_set_processor_pminfo, | |
63 | .interface_version = XENPF_INTERFACE_VERSION, | |
64 | .u.set_pminfo.id = _pr->acpi_id, | |
65 | .u.set_pminfo.type = XEN_PM_CX, | |
66 | }; | |
67 | struct xen_processor_cx *dst_cx, *dst_cx_states = NULL; | |
68 | struct acpi_processor_cx *cx; | |
69 | unsigned int i, ok; | |
70 | int ret = 0; | |
71 | ||
72 | dst_cx_states = kcalloc(_pr->power.count, | |
73 | sizeof(struct xen_processor_cx), GFP_KERNEL); | |
74 | if (!dst_cx_states) | |
75 | return -ENOMEM; | |
76 | ||
77 | for (ok = 0, i = 1; i <= _pr->power.count; i++) { | |
78 | cx = &_pr->power.states[i]; | |
79 | if (!cx->valid) | |
80 | continue; | |
81 | ||
82 | dst_cx = &(dst_cx_states[ok++]); | |
83 | ||
84 | dst_cx->reg.space_id = ACPI_ADR_SPACE_SYSTEM_IO; | |
85 | if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) { | |
86 | dst_cx->reg.bit_width = 8; | |
87 | dst_cx->reg.bit_offset = 0; | |
88 | dst_cx->reg.access_size = 1; | |
89 | } else { | |
90 | dst_cx->reg.space_id = ACPI_ADR_SPACE_FIXED_HARDWARE; | |
91 | if (cx->entry_method == ACPI_CSTATE_FFH) { | |
92 | /* NATIVE_CSTATE_BEYOND_HALT */ | |
93 | dst_cx->reg.bit_offset = 2; | |
94 | dst_cx->reg.bit_width = 1; /* VENDOR_INTEL */ | |
95 | } | |
96 | dst_cx->reg.access_size = 0; | |
97 | } | |
98 | dst_cx->reg.address = cx->address; | |
99 | ||
100 | dst_cx->type = cx->type; | |
101 | dst_cx->latency = cx->latency; | |
59a56802 KRW |
102 | |
103 | dst_cx->dpcnt = 0; | |
104 | set_xen_guest_handle(dst_cx->dp, NULL); | |
105 | } | |
106 | if (!ok) { | |
283c0972 | 107 | pr_debug("No _Cx for ACPI CPU %u\n", _pr->acpi_id); |
59a56802 KRW |
108 | kfree(dst_cx_states); |
109 | return -EINVAL; | |
110 | } | |
111 | op.u.set_pminfo.power.count = ok; | |
112 | op.u.set_pminfo.power.flags.bm_control = _pr->flags.bm_control; | |
113 | op.u.set_pminfo.power.flags.bm_check = _pr->flags.bm_check; | |
114 | op.u.set_pminfo.power.flags.has_cst = _pr->flags.has_cst; | |
115 | op.u.set_pminfo.power.flags.power_setup_done = | |
116 | _pr->flags.power_setup_done; | |
117 | ||
118 | set_xen_guest_handle(op.u.set_pminfo.power.states, dst_cx_states); | |
119 | ||
120 | if (!no_hypercall) | |
cfafae94 | 121 | ret = HYPERVISOR_platform_op(&op); |
59a56802 KRW |
122 | |
123 | if (!ret) { | |
124 | pr_debug("ACPI CPU%u - C-states uploaded.\n", _pr->acpi_id); | |
125 | for (i = 1; i <= _pr->power.count; i++) { | |
126 | cx = &_pr->power.states[i]; | |
127 | if (!cx->valid) | |
128 | continue; | |
129 | pr_debug(" C%d: %s %d uS\n", | |
130 | cx->type, cx->desc, (u32)cx->latency); | |
131 | } | |
1a4b50f6 | 132 | } else if ((ret != -EINVAL) && (ret != -ENOSYS)) |
b930fe5e KRW |
133 | /* EINVAL means the ACPI ID is incorrect - meaning the ACPI |
134 | * table is referencing a non-existing CPU - which can happen | |
135 | * with broken ACPI tables. */ | |
283c0972 | 136 | pr_err("(CX): Hypervisor error (%d) for ACPI CPU%u\n", |
59a56802 KRW |
137 | ret, _pr->acpi_id); |
138 | ||
139 | kfree(dst_cx_states); | |
140 | ||
141 | return ret; | |
142 | } | |
143 | static struct xen_processor_px * | |
144 | xen_copy_pss_data(struct acpi_processor *_pr, | |
145 | struct xen_processor_performance *dst_perf) | |
146 | { | |
147 | struct xen_processor_px *dst_states = NULL; | |
148 | unsigned int i; | |
149 | ||
150 | BUILD_BUG_ON(sizeof(struct xen_processor_px) != | |
151 | sizeof(struct acpi_processor_px)); | |
152 | ||
153 | dst_states = kcalloc(_pr->performance->state_count, | |
154 | sizeof(struct xen_processor_px), GFP_KERNEL); | |
155 | if (!dst_states) | |
156 | return ERR_PTR(-ENOMEM); | |
157 | ||
158 | dst_perf->state_count = _pr->performance->state_count; | |
159 | for (i = 0; i < _pr->performance->state_count; i++) { | |
160 | /* Fortunatly for us, they are both the same size */ | |
161 | memcpy(&(dst_states[i]), &(_pr->performance->states[i]), | |
162 | sizeof(struct acpi_processor_px)); | |
163 | } | |
164 | return dst_states; | |
165 | } | |
166 | static int xen_copy_psd_data(struct acpi_processor *_pr, | |
167 | struct xen_processor_performance *dst) | |
168 | { | |
169 | struct acpi_psd_package *pdomain; | |
170 | ||
171 | BUILD_BUG_ON(sizeof(struct xen_psd_package) != | |
172 | sizeof(struct acpi_psd_package)); | |
173 | ||
174 | /* This information is enumerated only if acpi_processor_preregister_performance | |
175 | * has been called. | |
176 | */ | |
177 | dst->shared_type = _pr->performance->shared_type; | |
178 | ||
179 | pdomain = &(_pr->performance->domain_info); | |
180 | ||
181 | /* 'acpi_processor_preregister_performance' does not parse if the | |
182 | * num_processors <= 1, but Xen still requires it. Do it manually here. | |
183 | */ | |
184 | if (pdomain->num_processors <= 1) { | |
185 | if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL) | |
186 | dst->shared_type = CPUFREQ_SHARED_TYPE_ALL; | |
187 | else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL) | |
188 | dst->shared_type = CPUFREQ_SHARED_TYPE_HW; | |
189 | else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY) | |
190 | dst->shared_type = CPUFREQ_SHARED_TYPE_ANY; | |
191 | ||
192 | } | |
193 | memcpy(&(dst->domain_info), pdomain, sizeof(struct acpi_psd_package)); | |
194 | return 0; | |
195 | } | |
196 | static int xen_copy_pct_data(struct acpi_pct_register *pct, | |
197 | struct xen_pct_register *dst_pct) | |
198 | { | |
199 | /* It would be nice if you could just do 'memcpy(pct, dst_pct') but | |
200 | * sadly the Xen structure did not have the proper padding so the | |
201 | * descriptor field takes two (dst_pct) bytes instead of one (pct). | |
202 | */ | |
203 | dst_pct->descriptor = pct->descriptor; | |
204 | dst_pct->length = pct->length; | |
205 | dst_pct->space_id = pct->space_id; | |
206 | dst_pct->bit_width = pct->bit_width; | |
207 | dst_pct->bit_offset = pct->bit_offset; | |
208 | dst_pct->reserved = pct->reserved; | |
209 | dst_pct->address = pct->address; | |
210 | return 0; | |
211 | } | |
212 | static int push_pxx_to_hypervisor(struct acpi_processor *_pr) | |
213 | { | |
214 | int ret = 0; | |
215 | struct xen_platform_op op = { | |
216 | .cmd = XENPF_set_processor_pminfo, | |
217 | .interface_version = XENPF_INTERFACE_VERSION, | |
218 | .u.set_pminfo.id = _pr->acpi_id, | |
219 | .u.set_pminfo.type = XEN_PM_PX, | |
220 | }; | |
221 | struct xen_processor_performance *dst_perf; | |
222 | struct xen_processor_px *dst_states = NULL; | |
223 | ||
224 | dst_perf = &op.u.set_pminfo.perf; | |
225 | ||
226 | dst_perf->platform_limit = _pr->performance_platform_limit; | |
227 | dst_perf->flags |= XEN_PX_PPC; | |
228 | xen_copy_pct_data(&(_pr->performance->control_register), | |
229 | &dst_perf->control_register); | |
230 | xen_copy_pct_data(&(_pr->performance->status_register), | |
231 | &dst_perf->status_register); | |
232 | dst_perf->flags |= XEN_PX_PCT; | |
233 | dst_states = xen_copy_pss_data(_pr, dst_perf); | |
234 | if (!IS_ERR_OR_NULL(dst_states)) { | |
235 | set_xen_guest_handle(dst_perf->states, dst_states); | |
236 | dst_perf->flags |= XEN_PX_PSS; | |
237 | } | |
238 | if (!xen_copy_psd_data(_pr, dst_perf)) | |
239 | dst_perf->flags |= XEN_PX_PSD; | |
240 | ||
241 | if (dst_perf->flags != (XEN_PX_PSD | XEN_PX_PSS | XEN_PX_PCT | XEN_PX_PPC)) { | |
283c0972 | 242 | pr_warn("ACPI CPU%u missing some P-state data (%x), skipping\n", |
59a56802 KRW |
243 | _pr->acpi_id, dst_perf->flags); |
244 | ret = -ENODEV; | |
245 | goto err_free; | |
246 | } | |
247 | ||
248 | if (!no_hypercall) | |
cfafae94 | 249 | ret = HYPERVISOR_platform_op(&op); |
59a56802 KRW |
250 | |
251 | if (!ret) { | |
252 | struct acpi_processor_performance *perf; | |
253 | unsigned int i; | |
254 | ||
255 | perf = _pr->performance; | |
256 | pr_debug("ACPI CPU%u - P-states uploaded.\n", _pr->acpi_id); | |
257 | for (i = 0; i < perf->state_count; i++) { | |
258 | pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n", | |
259 | (i == perf->state ? '*' : ' '), i, | |
260 | (u32) perf->states[i].core_frequency, | |
261 | (u32) perf->states[i].power, | |
262 | (u32) perf->states[i].transition_latency); | |
263 | } | |
1a4b50f6 | 264 | } else if ((ret != -EINVAL) && (ret != -ENOSYS)) |
59a56802 KRW |
265 | /* EINVAL means the ACPI ID is incorrect - meaning the ACPI |
266 | * table is referencing a non-existing CPU - which can happen | |
267 | * with broken ACPI tables. */ | |
283c0972 JP |
268 | pr_warn("(_PXX): Hypervisor error (%d) for ACPI CPU%u\n", |
269 | ret, _pr->acpi_id); | |
59a56802 KRW |
270 | err_free: |
271 | if (!IS_ERR_OR_NULL(dst_states)) | |
272 | kfree(dst_states); | |
273 | ||
274 | return ret; | |
275 | } | |
276 | static int upload_pm_data(struct acpi_processor *_pr) | |
277 | { | |
278 | int err = 0; | |
279 | ||
280 | mutex_lock(&acpi_ids_mutex); | |
281 | if (__test_and_set_bit(_pr->acpi_id, acpi_ids_done)) { | |
282 | mutex_unlock(&acpi_ids_mutex); | |
283 | return -EBUSY; | |
284 | } | |
285 | if (_pr->flags.power) | |
286 | err = push_cxx_to_hypervisor(_pr); | |
287 | ||
288 | if (_pr->performance && _pr->performance->states) | |
289 | err |= push_pxx_to_hypervisor(_pr); | |
290 | ||
291 | mutex_unlock(&acpi_ids_mutex); | |
292 | return err; | |
293 | } | |
294 | static unsigned int __init get_max_acpi_id(void) | |
295 | { | |
296 | struct xenpf_pcpuinfo *info; | |
297 | struct xen_platform_op op = { | |
298 | .cmd = XENPF_get_cpuinfo, | |
299 | .interface_version = XENPF_INTERFACE_VERSION, | |
300 | }; | |
301 | int ret = 0; | |
302 | unsigned int i, last_cpu, max_acpi_id = 0; | |
303 | ||
304 | info = &op.u.pcpu_info; | |
305 | info->xen_cpuid = 0; | |
306 | ||
cfafae94 | 307 | ret = HYPERVISOR_platform_op(&op); |
59a56802 KRW |
308 | if (ret) |
309 | return NR_CPUS; | |
310 | ||
311 | /* The max_present is the same irregardless of the xen_cpuid */ | |
312 | last_cpu = op.u.pcpu_info.max_present; | |
313 | for (i = 0; i <= last_cpu; i++) { | |
314 | info->xen_cpuid = i; | |
cfafae94 | 315 | ret = HYPERVISOR_platform_op(&op); |
59a56802 KRW |
316 | if (ret) |
317 | continue; | |
318 | max_acpi_id = max(info->acpi_id, max_acpi_id); | |
319 | } | |
320 | max_acpi_id *= 2; /* Slack for CPU hotplug support. */ | |
283c0972 | 321 | pr_debug("Max ACPI ID: %u\n", max_acpi_id); |
59a56802 KRW |
322 | return max_acpi_id; |
323 | } | |
324 | /* | |
325 | * The read_acpi_id and check_acpi_ids are there to support the Xen | |
326 | * oddity of virtual CPUs != physical CPUs in the initial domain. | |
327 | * The user can supply 'xen_max_vcpus=X' on the Xen hypervisor line | |
328 | * which will band the amount of CPUs the initial domain can see. | |
329 | * In general that is OK, except it plays havoc with any of the | |
330 | * for_each_[present|online]_cpu macros which are banded to the virtual | |
331 | * CPU amount. | |
332 | */ | |
18c0025b | 333 | static acpi_status |
59a56802 KRW |
334 | read_acpi_id(acpi_handle handle, u32 lvl, void *context, void **rv) |
335 | { | |
336 | u32 acpi_id; | |
337 | acpi_status status; | |
338 | acpi_object_type acpi_type; | |
339 | unsigned long long tmp; | |
340 | union acpi_object object = { 0 }; | |
341 | struct acpi_buffer buffer = { sizeof(union acpi_object), &object }; | |
342 | acpi_io_address pblk = 0; | |
343 | ||
344 | status = acpi_get_type(handle, &acpi_type); | |
345 | if (ACPI_FAILURE(status)) | |
346 | return AE_OK; | |
347 | ||
348 | switch (acpi_type) { | |
349 | case ACPI_TYPE_PROCESSOR: | |
350 | status = acpi_evaluate_object(handle, NULL, NULL, &buffer); | |
351 | if (ACPI_FAILURE(status)) | |
352 | return AE_OK; | |
353 | acpi_id = object.processor.proc_id; | |
354 | pblk = object.processor.pblk_address; | |
355 | break; | |
356 | case ACPI_TYPE_DEVICE: | |
357 | status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp); | |
358 | if (ACPI_FAILURE(status)) | |
359 | return AE_OK; | |
360 | acpi_id = tmp; | |
361 | break; | |
362 | default: | |
363 | return AE_OK; | |
364 | } | |
166deb0f JB |
365 | if (invalid_phys_cpuid(acpi_get_phys_id(handle, |
366 | acpi_type == ACPI_TYPE_DEVICE, | |
367 | acpi_id))) { | |
368 | pr_debug("CPU with ACPI ID %u is unavailable\n", acpi_id); | |
369 | return AE_OK; | |
370 | } | |
59a56802 KRW |
371 | /* There are more ACPI Processor objects than in x2APIC or MADT. |
372 | * This can happen with incorrect ACPI SSDT declerations. */ | |
c37a3c94 DC |
373 | if (acpi_id >= nr_acpi_bits) { |
374 | pr_debug("max acpi id %u, trying to set %u\n", | |
375 | nr_acpi_bits - 1, acpi_id); | |
59a56802 KRW |
376 | return AE_OK; |
377 | } | |
378 | /* OK, There is a ACPI Processor object */ | |
379 | __set_bit(acpi_id, acpi_id_present); | |
380 | ||
283c0972 | 381 | pr_debug("ACPI CPU%u w/ PBLK:0x%lx\n", acpi_id, (unsigned long)pblk); |
59a56802 | 382 | |
4d0f1ce6 JM |
383 | /* It has P-state dependencies */ |
384 | if (!acpi_processor_get_psd(handle, &acpi_psd[acpi_id])) { | |
385 | pr_debug("ACPI CPU%u w/ PST:coord_type = %llu domain = %llu\n", | |
386 | acpi_id, acpi_psd[acpi_id].coord_type, | |
387 | acpi_psd[acpi_id].domain); | |
388 | } | |
389 | ||
59a56802 KRW |
390 | status = acpi_evaluate_object(handle, "_CST", NULL, &buffer); |
391 | if (ACPI_FAILURE(status)) { | |
392 | if (!pblk) | |
393 | return AE_OK; | |
394 | } | |
395 | /* .. and it has a C-state */ | |
396 | __set_bit(acpi_id, acpi_id_cst_present); | |
397 | ||
398 | return AE_OK; | |
399 | } | |
18c0025b | 400 | static int check_acpi_ids(struct acpi_processor *pr_backup) |
59a56802 KRW |
401 | { |
402 | ||
403 | if (!pr_backup) | |
404 | return -ENODEV; | |
405 | ||
3fac1014 BG |
406 | if (acpi_id_present && acpi_id_cst_present) |
407 | /* OK, done this once .. skip to uploading */ | |
408 | goto upload; | |
409 | ||
59a56802 KRW |
410 | /* All online CPUs have been processed at this stage. Now verify |
411 | * whether in fact "online CPUs" == physical CPUs. | |
412 | */ | |
413 | acpi_id_present = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL); | |
414 | if (!acpi_id_present) | |
415 | return -ENOMEM; | |
416 | ||
417 | acpi_id_cst_present = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL); | |
418 | if (!acpi_id_cst_present) { | |
419 | kfree(acpi_id_present); | |
420 | return -ENOMEM; | |
421 | } | |
422 | ||
4d0f1ce6 JM |
423 | acpi_psd = kcalloc(nr_acpi_bits, sizeof(struct acpi_psd_package), |
424 | GFP_KERNEL); | |
425 | if (!acpi_psd) { | |
426 | kfree(acpi_id_present); | |
427 | kfree(acpi_id_cst_present); | |
428 | return -ENOMEM; | |
429 | } | |
430 | ||
59a56802 KRW |
431 | acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT, |
432 | ACPI_UINT32_MAX, | |
433 | read_acpi_id, NULL, NULL, NULL); | |
1c2593cc | 434 | acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, read_acpi_id, NULL, NULL); |
59a56802 | 435 | |
3fac1014 | 436 | upload: |
59a56802 KRW |
437 | if (!bitmap_equal(acpi_id_present, acpi_ids_done, nr_acpi_bits)) { |
438 | unsigned int i; | |
439 | for_each_set_bit(i, acpi_id_present, nr_acpi_bits) { | |
440 | pr_backup->acpi_id = i; | |
441 | /* Mask out C-states if there are no _CST or PBLK */ | |
442 | pr_backup->flags.power = test_bit(i, acpi_id_cst_present); | |
4d0f1ce6 JM |
443 | /* num_entries is non-zero if we evaluated _PSD */ |
444 | if (acpi_psd[i].num_entries) { | |
445 | memcpy(&pr_backup->performance->domain_info, | |
446 | &acpi_psd[i], | |
447 | sizeof(struct acpi_psd_package)); | |
448 | } | |
59a56802 KRW |
449 | (void)upload_pm_data(pr_backup); |
450 | } | |
451 | } | |
3fac1014 | 452 | |
59a56802 KRW |
453 | return 0; |
454 | } | |
59a56802 | 455 | |
59a56802 KRW |
456 | /* acpi_perf_data is a pointer to percpu data. */ |
457 | static struct acpi_processor_performance __percpu *acpi_perf_data; | |
458 | ||
459 | static void free_acpi_perf_data(void) | |
460 | { | |
461 | unsigned int i; | |
462 | ||
463 | /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */ | |
464 | for_each_possible_cpu(i) | |
465 | free_cpumask_var(per_cpu_ptr(acpi_perf_data, i) | |
466 | ->shared_cpu_map); | |
467 | free_percpu(acpi_perf_data); | |
468 | } | |
469 | ||
3fac1014 | 470 | static int xen_upload_processor_pm_data(void) |
59a56802 KRW |
471 | { |
472 | struct acpi_processor *pr_backup = NULL; | |
3fac1014 BG |
473 | unsigned int i; |
474 | int rc = 0; | |
475 | ||
283c0972 | 476 | pr_info("Uploading Xen processor PM info\n"); |
3fac1014 BG |
477 | |
478 | for_each_possible_cpu(i) { | |
479 | struct acpi_processor *_pr; | |
480 | _pr = per_cpu(processors, i /* APIC ID */); | |
481 | if (!_pr) | |
482 | continue; | |
483 | ||
484 | if (!pr_backup) { | |
485 | pr_backup = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL); | |
486 | if (pr_backup) | |
487 | memcpy(pr_backup, _pr, sizeof(struct acpi_processor)); | |
488 | } | |
489 | (void)upload_pm_data(_pr); | |
490 | } | |
491 | ||
492 | rc = check_acpi_ids(pr_backup); | |
493 | kfree(pr_backup); | |
494 | ||
495 | return rc; | |
496 | } | |
497 | ||
1914f0cd | 498 | static void xen_acpi_processor_resume_worker(struct work_struct *dummy) |
3fac1014 | 499 | { |
1914f0cd AA |
500 | int rc; |
501 | ||
3fac1014 | 502 | bitmap_zero(acpi_ids_done, nr_acpi_bits); |
1914f0cd AA |
503 | |
504 | rc = xen_upload_processor_pm_data(); | |
505 | if (rc != 0) | |
506 | pr_info("ACPI data upload failed, error = %d\n", rc); | |
507 | } | |
508 | ||
509 | static void xen_acpi_processor_resume(void) | |
510 | { | |
511 | static DECLARE_WORK(wq, xen_acpi_processor_resume_worker); | |
512 | ||
513 | /* | |
514 | * xen_upload_processor_pm_data() calls non-atomic code. | |
515 | * However, the context for xen_acpi_processor_resume is syscore | |
516 | * with only the boot CPU online and in an atomic context. | |
517 | * | |
518 | * So defer the upload for some point safer. | |
519 | */ | |
520 | schedule_work(&wq); | |
3fac1014 BG |
521 | } |
522 | ||
1914f0cd AA |
523 | static struct syscore_ops xap_syscore_ops = { |
524 | .resume = xen_acpi_processor_resume, | |
3fac1014 BG |
525 | }; |
526 | ||
527 | static int __init xen_acpi_processor_init(void) | |
528 | { | |
59a56802 | 529 | unsigned int i; |
6f2d9d99 | 530 | int rc; |
59a56802 | 531 | |
6f2d9d99 JB |
532 | if (!xen_initial_domain()) |
533 | return -ENODEV; | |
59a56802 KRW |
534 | |
535 | nr_acpi_bits = get_max_acpi_id() + 1; | |
536 | acpi_ids_done = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL); | |
537 | if (!acpi_ids_done) | |
538 | return -ENOMEM; | |
539 | ||
540 | acpi_perf_data = alloc_percpu(struct acpi_processor_performance); | |
541 | if (!acpi_perf_data) { | |
283c0972 | 542 | pr_debug("Memory allocation error for acpi_perf_data\n"); |
59a56802 KRW |
543 | kfree(acpi_ids_done); |
544 | return -ENOMEM; | |
545 | } | |
546 | for_each_possible_cpu(i) { | |
547 | if (!zalloc_cpumask_var_node( | |
548 | &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map, | |
549 | GFP_KERNEL, cpu_to_node(i))) { | |
550 | rc = -ENOMEM; | |
551 | goto err_out; | |
552 | } | |
553 | } | |
554 | ||
555 | /* Do initialization in ACPI core. It is OK to fail here. */ | |
556 | (void)acpi_processor_preregister_performance(acpi_perf_data); | |
557 | ||
558 | for_each_possible_cpu(i) { | |
c705c78c | 559 | struct acpi_processor *pr; |
59a56802 KRW |
560 | struct acpi_processor_performance *perf; |
561 | ||
c705c78c | 562 | pr = per_cpu(processors, i); |
59a56802 | 563 | perf = per_cpu_ptr(acpi_perf_data, i); |
949dd8c1 KRW |
564 | if (!pr) |
565 | continue; | |
566 | ||
c705c78c KRW |
567 | pr->performance = perf; |
568 | rc = acpi_processor_get_performance_info(pr); | |
27257fc0 | 569 | if (rc) |
59a56802 KRW |
570 | goto err_out; |
571 | } | |
59a56802 | 572 | |
3fac1014 | 573 | rc = xen_upload_processor_pm_data(); |
17f9b896 KRW |
574 | if (rc) |
575 | goto err_unregister; | |
59a56802 | 576 | |
1914f0cd | 577 | register_syscore_ops(&xap_syscore_ops); |
3fac1014 | 578 | |
59a56802 KRW |
579 | return 0; |
580 | err_unregister: | |
b2f8dc4c RW |
581 | for_each_possible_cpu(i) |
582 | acpi_processor_unregister_performance(i); | |
583 | ||
59a56802 KRW |
584 | err_out: |
585 | /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */ | |
586 | free_acpi_perf_data(); | |
587 | kfree(acpi_ids_done); | |
588 | return rc; | |
589 | } | |
590 | static void __exit xen_acpi_processor_exit(void) | |
591 | { | |
592 | int i; | |
593 | ||
1914f0cd | 594 | unregister_syscore_ops(&xap_syscore_ops); |
59a56802 | 595 | kfree(acpi_ids_done); |
3fac1014 BG |
596 | kfree(acpi_id_present); |
597 | kfree(acpi_id_cst_present); | |
4d0f1ce6 | 598 | kfree(acpi_psd); |
b2f8dc4c RW |
599 | for_each_possible_cpu(i) |
600 | acpi_processor_unregister_performance(i); | |
601 | ||
59a56802 KRW |
602 | free_acpi_perf_data(); |
603 | } | |
604 | ||
605 | MODULE_AUTHOR("Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>"); | |
606 | MODULE_DESCRIPTION("Xen ACPI Processor P-states (and Cx) driver which uploads PM data to Xen hypervisor"); | |
607 | MODULE_LICENSE("GPL"); | |
608 | ||
609 | /* We want to be loaded before the CPU freq scaling drivers are loaded. | |
610 | * They are loaded in late_initcall. */ | |
611 | device_initcall(xen_acpi_processor_init); | |
612 | module_exit(xen_acpi_processor_exit); |