]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/cppc_acpi.c
ACPI / CPPC: move all PCC related information into pcc_data
[mirror_ubuntu-zesty-kernel.git] / drivers / acpi / cppc_acpi.c
CommitLineData
337aadff
AC
1/*
2 * CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
3 *
4 * (C) Copyright 2014, 2015 Linaro Ltd.
5 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
11 *
12 * CPPC describes a few methods for controlling CPU performance using
13 * information from a per CPU table called CPC. This table is described in
14 * the ACPI v5.0+ specification. The table consists of a list of
15 * registers which may be memory mapped or hardware registers and also may
16 * include some static integer values.
17 *
18 * CPU performance is on an abstract continuous scale as against a discretized
19 * P-state scale which is tied to CPU frequency only. In brief, the basic
20 * operation involves:
21 *
22 * - OS makes a CPU performance request. (Can provide min and max bounds)
23 *
24 * - Platform (such as BMC) is free to optimize request within requested bounds
25 * depending on power/thermal budgets etc.
26 *
27 * - Platform conveys its decision back to OS
28 *
29 * The communication between OS and platform occurs through another medium
30 * called (PCC) Platform Communication Channel. This is a generic mailbox like
31 * mechanism which includes doorbell semantics to indicate register updates.
32 * See drivers/mailbox/pcc.c for details on PCC.
33 *
34 * Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
35 * above specifications.
36 */
37
38#define pr_fmt(fmt) "ACPI CPPC: " fmt
39
40#include <linux/cpufreq.h>
41#include <linux/delay.h>
ad62e1e6 42#include <linux/ktime.h>
80b8286a
PP
43#include <linux/rwsem.h>
44#include <linux/wait.h>
337aadff
AC
45
46#include <acpi/cppc_acpi.h>
80b8286a 47
8482ef8c
PP
48struct cppc_pcc_data {
49 struct mbox_chan *pcc_channel;
50 void __iomem *pcc_comm_addr;
51 int pcc_subspace_idx;
52 bool pcc_channel_acquired;
53 ktime_t deadline;
54 unsigned int pcc_mpar, pcc_mrtt, pcc_nominal;
80b8286a 55
8482ef8c
PP
56 bool pending_pcc_write_cmd; /* Any pending/batched PCC write cmds? */
57 unsigned int pcc_write_cnt; /* Running count of PCC write commands */
80b8286a 58
8482ef8c
PP
59 /*
60 * Lock to provide controlled access to the PCC channel.
61 *
62 * For performance critical usecases(currently cppc_set_perf)
63 * We need to take read_lock and check if channel belongs to OSPM
64 * before reading or writing to PCC subspace
65 * We need to take write_lock before transferring the channel
66 * ownership to the platform via a Doorbell
67 * This allows us to batch a number of CPPC requests if they happen
68 * to originate in about the same time
69 *
70 * For non-performance critical usecases(init)
71 * Take write_lock for all purposes which gives exclusive access
72 */
73 struct rw_semaphore pcc_lock;
74
75 /* Wait queue for CPUs whose requests were batched */
76 wait_queue_head_t pcc_write_wait_q;
77};
80b8286a 78
8482ef8c
PP
79/* Structure to represent the single PCC channel */
80static struct cppc_pcc_data pcc_data = {
81 .pcc_subspace_idx = -1,
82};
337aadff
AC
83
84/*
85 * The cpc_desc structure contains the ACPI register details
86 * as described in the per CPU _CPC tables. The details
87 * include the type of register (e.g. PCC, System IO, FFH etc.)
88 * and destination addresses which lets us READ/WRITE CPU performance
89 * information using the appropriate I/O methods.
90 */
91static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
92
77e3d86f 93/* pcc mapped address + header size + offset within PCC subspace */
8482ef8c 94#define GET_PCC_VADDR(offs) (pcc_data.pcc_comm_addr + 0x8 + (offs))
77e3d86f 95
80b8286a
PP
96/* Check if a CPC regsiter is in PCC */
97#define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
98 (cpc)->cpc_entry.reg.space_id == \
99 ACPI_ADR_SPACE_PLATFORM_COMM)
100
158c998e
AC
101/* Evalutes to True if reg is a NULL register descriptor */
102#define IS_NULL_REG(reg) ((reg)->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY && \
103 (reg)->address == 0 && \
104 (reg)->bit_width == 0 && \
105 (reg)->bit_offset == 0 && \
106 (reg)->access_width == 0)
107
108/* Evalutes to True if an optional cpc field is supported */
109#define CPC_SUPPORTED(cpc) ((cpc)->type == ACPI_TYPE_INTEGER ? \
110 !!(cpc)->cpc_entry.int_value : \
111 !IS_NULL_REG(&(cpc)->cpc_entry.reg))
337aadff
AC
112/*
113 * Arbitrary Retries in case the remote processor is slow to respond
ad62e1e6
AC
114 * to PCC commands. Keeping it high enough to cover emulators where
115 * the processors run painfully slow.
337aadff
AC
116 */
117#define NUM_RETRIES 500
118
158c998e
AC
119struct cppc_attr {
120 struct attribute attr;
121 ssize_t (*show)(struct kobject *kobj,
122 struct attribute *attr, char *buf);
123 ssize_t (*store)(struct kobject *kobj,
124 struct attribute *attr, const char *c, ssize_t count);
125};
126
127#define define_one_cppc_ro(_name) \
128static struct cppc_attr _name = \
129__ATTR(_name, 0444, show_##_name, NULL)
130
131#define to_cpc_desc(a) container_of(a, struct cpc_desc, kobj)
132
133static ssize_t show_feedback_ctrs(struct kobject *kobj,
134 struct attribute *attr, char *buf)
135{
136 struct cpc_desc *cpc_ptr = to_cpc_desc(kobj);
137 struct cppc_perf_fb_ctrs fb_ctrs = {0};
138
139 cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs);
140
141 return scnprintf(buf, PAGE_SIZE, "ref:%llu del:%llu\n",
142 fb_ctrs.reference, fb_ctrs.delivered);
143}
144define_one_cppc_ro(feedback_ctrs);
145
146static ssize_t show_reference_perf(struct kobject *kobj,
147 struct attribute *attr, char *buf)
148{
149 struct cpc_desc *cpc_ptr = to_cpc_desc(kobj);
150 struct cppc_perf_fb_ctrs fb_ctrs = {0};
151
152 cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs);
153
154 return scnprintf(buf, PAGE_SIZE, "%llu\n",
155 fb_ctrs.reference_perf);
156}
157define_one_cppc_ro(reference_perf);
158
159static ssize_t show_wraparound_time(struct kobject *kobj,
160 struct attribute *attr, char *buf)
161{
162 struct cpc_desc *cpc_ptr = to_cpc_desc(kobj);
163 struct cppc_perf_fb_ctrs fb_ctrs = {0};
164
165 cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs);
166
167 return scnprintf(buf, PAGE_SIZE, "%llu\n", fb_ctrs.ctr_wrap_time);
168
169}
170define_one_cppc_ro(wraparound_time);
171
172static struct attribute *cppc_attrs[] = {
173 &feedback_ctrs.attr,
174 &reference_perf.attr,
175 &wraparound_time.attr,
176 NULL
177};
178
179static struct kobj_type cppc_ktype = {
180 .sysfs_ops = &kobj_sysfs_ops,
181 .default_attrs = cppc_attrs,
182};
183
ad62e1e6
AC
184static int check_pcc_chan(void)
185{
186 int ret = -EIO;
8482ef8c
PP
187 struct acpi_pcct_shared_memory __iomem *generic_comm_base = pcc_data.pcc_comm_addr;
188 ktime_t next_deadline = ktime_add(ktime_get(), pcc_data.deadline);
ad62e1e6
AC
189
190 /* Retry in case the remote processor was too slow to catch up. */
191 while (!ktime_after(ktime_get(), next_deadline)) {
f387e5b9
PP
192 /*
193 * Per spec, prior to boot the PCC space wil be initialized by
194 * platform and should have set the command completion bit when
195 * PCC can be used by OSPM
196 */
ad62e1e6
AC
197 if (readw_relaxed(&generic_comm_base->status) & PCC_CMD_COMPLETE) {
198 ret = 0;
199 break;
200 }
201 /*
202 * Reducing the bus traffic in case this loop takes longer than
203 * a few retries.
204 */
205 udelay(3);
206 }
207
208 return ret;
209}
210
80b8286a
PP
211/*
212 * This function transfers the ownership of the PCC to the platform
213 * So it must be called while holding write_lock(pcc_lock)
214 */
337aadff
AC
215static int send_pcc_cmd(u16 cmd)
216{
80b8286a 217 int ret = -EIO, i;
337aadff 218 struct acpi_pcct_shared_memory *generic_comm_base =
8482ef8c 219 (struct acpi_pcct_shared_memory *) pcc_data.pcc_comm_addr;
f387e5b9
PP
220 static ktime_t last_cmd_cmpl_time, last_mpar_reset;
221 static int mpar_count;
222 unsigned int time_delta;
337aadff 223
ad62e1e6
AC
224 /*
225 * For CMD_WRITE we know for a fact the caller should have checked
226 * the channel before writing to PCC space
227 */
228 if (cmd == CMD_READ) {
80b8286a
PP
229 /*
230 * If there are pending cpc_writes, then we stole the channel
231 * before write completion, so first send a WRITE command to
232 * platform
233 */
8482ef8c 234 if (pcc_data.pending_pcc_write_cmd)
80b8286a
PP
235 send_pcc_cmd(CMD_WRITE);
236
ad62e1e6
AC
237 ret = check_pcc_chan();
238 if (ret)
80b8286a
PP
239 goto end;
240 } else /* CMD_WRITE */
8482ef8c 241 pcc_data.pending_pcc_write_cmd = FALSE;
337aadff 242
f387e5b9
PP
243 /*
244 * Handle the Minimum Request Turnaround Time(MRTT)
245 * "The minimum amount of time that OSPM must wait after the completion
246 * of a command before issuing the next command, in microseconds"
247 */
8482ef8c 248 if (pcc_data.pcc_mrtt) {
f387e5b9 249 time_delta = ktime_us_delta(ktime_get(), last_cmd_cmpl_time);
8482ef8c
PP
250 if (pcc_data.pcc_mrtt > time_delta)
251 udelay(pcc_data.pcc_mrtt - time_delta);
f387e5b9
PP
252 }
253
254 /*
255 * Handle the non-zero Maximum Periodic Access Rate(MPAR)
256 * "The maximum number of periodic requests that the subspace channel can
257 * support, reported in commands per minute. 0 indicates no limitation."
258 *
259 * This parameter should be ideally zero or large enough so that it can
260 * handle maximum number of requests that all the cores in the system can
261 * collectively generate. If it is not, we will follow the spec and just
262 * not send the request to the platform after hitting the MPAR limit in
263 * any 60s window
264 */
8482ef8c 265 if (pcc_data.pcc_mpar) {
f387e5b9
PP
266 if (mpar_count == 0) {
267 time_delta = ktime_ms_delta(ktime_get(), last_mpar_reset);
268 if (time_delta < 60 * MSEC_PER_SEC) {
269 pr_debug("PCC cmd not sent due to MPAR limit");
80b8286a
PP
270 ret = -EIO;
271 goto end;
f387e5b9
PP
272 }
273 last_mpar_reset = ktime_get();
8482ef8c 274 mpar_count = pcc_data.pcc_mpar;
f387e5b9
PP
275 }
276 mpar_count--;
277 }
278
337aadff 279 /* Write to the shared comm region. */
beee23ae 280 writew_relaxed(cmd, &generic_comm_base->command);
337aadff
AC
281
282 /* Flip CMD COMPLETE bit */
beee23ae 283 writew_relaxed(0, &generic_comm_base->status);
337aadff
AC
284
285 /* Ring doorbell */
8482ef8c 286 ret = mbox_send_message(pcc_data.pcc_channel, &cmd);
ad62e1e6 287 if (ret < 0) {
337aadff 288 pr_err("Err sending PCC mbox message. cmd:%d, ret:%d\n",
ad62e1e6 289 cmd, ret);
80b8286a 290 goto end;
337aadff
AC
291 }
292
ad62e1e6
AC
293 /*
294 * For READs we need to ensure the cmd completed to ensure
295 * the ensuing read()s can proceed. For WRITEs we dont care
296 * because the actual write()s are done before coming here
297 * and the next READ or WRITE will check if the channel
298 * is busy/free at the entry of this call.
f387e5b9
PP
299 *
300 * If Minimum Request Turnaround Time is non-zero, we need
301 * to record the completion time of both READ and WRITE
302 * command for proper handling of MRTT, so we need to check
303 * for pcc_mrtt in addition to CMD_READ
ad62e1e6 304 */
8482ef8c 305 if (cmd == CMD_READ || pcc_data.pcc_mrtt) {
ad62e1e6 306 ret = check_pcc_chan();
8482ef8c 307 if (pcc_data.pcc_mrtt)
f387e5b9
PP
308 last_cmd_cmpl_time = ktime_get();
309 }
337aadff 310
8482ef8c 311 mbox_client_txdone(pcc_data.pcc_channel, ret);
80b8286a
PP
312
313end:
314 if (cmd == CMD_WRITE) {
315 if (unlikely(ret)) {
316 for_each_possible_cpu(i) {
317 struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i);
318 if (!desc)
319 continue;
320
8482ef8c 321 if (desc->write_cmd_id == pcc_data.pcc_write_cnt)
80b8286a
PP
322 desc->write_cmd_status = ret;
323 }
324 }
8482ef8c
PP
325 pcc_data.pcc_write_cnt++;
326 wake_up_all(&pcc_data.pcc_write_wait_q);
80b8286a
PP
327 }
328
ad62e1e6 329 return ret;
337aadff
AC
330}
331
332static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
333{
ad62e1e6 334 if (ret < 0)
337aadff
AC
335 pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
336 *(u16 *)msg, ret);
337 else
338 pr_debug("TX completed. CMD sent:%x, ret:%d\n",
339 *(u16 *)msg, ret);
340}
341
342struct mbox_client cppc_mbox_cl = {
343 .tx_done = cppc_chan_tx_done,
344 .knows_txdone = true,
345};
346
347static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
348{
349 int result = -EFAULT;
350 acpi_status status = AE_OK;
351 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
352 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
353 struct acpi_buffer state = {0, NULL};
354 union acpi_object *psd = NULL;
355 struct acpi_psd_package *pdomain;
356
357 status = acpi_evaluate_object_typed(handle, "_PSD", NULL, &buffer,
358 ACPI_TYPE_PACKAGE);
359 if (ACPI_FAILURE(status))
360 return -ENODEV;
361
362 psd = buffer.pointer;
363 if (!psd || psd->package.count != 1) {
364 pr_debug("Invalid _PSD data\n");
365 goto end;
366 }
367
368 pdomain = &(cpc_ptr->domain_info);
369
370 state.length = sizeof(struct acpi_psd_package);
371 state.pointer = pdomain;
372
373 status = acpi_extract_package(&(psd->package.elements[0]),
374 &format, &state);
375 if (ACPI_FAILURE(status)) {
376 pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
377 goto end;
378 }
379
380 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
381 pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
382 goto end;
383 }
384
385 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
386 pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
387 goto end;
388 }
389
390 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
391 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
392 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
393 pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
394 goto end;
395 }
396
397 result = 0;
398end:
399 kfree(buffer.pointer);
400 return result;
401}
402
403/**
404 * acpi_get_psd_map - Map the CPUs in a common freq domain.
405 * @all_cpu_data: Ptrs to CPU specific CPPC data including PSD info.
406 *
407 * Return: 0 for success or negative value for err.
408 */
409int acpi_get_psd_map(struct cpudata **all_cpu_data)
410{
411 int count_target;
412 int retval = 0;
413 unsigned int i, j;
414 cpumask_var_t covered_cpus;
415 struct cpudata *pr, *match_pr;
416 struct acpi_psd_package *pdomain;
417 struct acpi_psd_package *match_pdomain;
418 struct cpc_desc *cpc_ptr, *match_cpc_ptr;
419
420 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
421 return -ENOMEM;
422
423 /*
424 * Now that we have _PSD data from all CPUs, lets setup P-state
425 * domain info.
426 */
427 for_each_possible_cpu(i) {
428 pr = all_cpu_data[i];
429 if (!pr)
430 continue;
431
432 if (cpumask_test_cpu(i, covered_cpus))
433 continue;
434
435 cpc_ptr = per_cpu(cpc_desc_ptr, i);
8343c40d
HT
436 if (!cpc_ptr) {
437 retval = -EFAULT;
438 goto err_ret;
439 }
337aadff
AC
440
441 pdomain = &(cpc_ptr->domain_info);
442 cpumask_set_cpu(i, pr->shared_cpu_map);
443 cpumask_set_cpu(i, covered_cpus);
444 if (pdomain->num_processors <= 1)
445 continue;
446
447 /* Validate the Domain info */
448 count_target = pdomain->num_processors;
449 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
450 pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
451 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
452 pr->shared_type = CPUFREQ_SHARED_TYPE_HW;
453 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
454 pr->shared_type = CPUFREQ_SHARED_TYPE_ANY;
455
456 for_each_possible_cpu(j) {
457 if (i == j)
458 continue;
459
460 match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
8343c40d
HT
461 if (!match_cpc_ptr) {
462 retval = -EFAULT;
463 goto err_ret;
464 }
337aadff
AC
465
466 match_pdomain = &(match_cpc_ptr->domain_info);
467 if (match_pdomain->domain != pdomain->domain)
468 continue;
469
470 /* Here i and j are in the same domain */
471 if (match_pdomain->num_processors != count_target) {
472 retval = -EFAULT;
473 goto err_ret;
474 }
475
476 if (pdomain->coord_type != match_pdomain->coord_type) {
477 retval = -EFAULT;
478 goto err_ret;
479 }
480
481 cpumask_set_cpu(j, covered_cpus);
482 cpumask_set_cpu(j, pr->shared_cpu_map);
483 }
484
485 for_each_possible_cpu(j) {
486 if (i == j)
487 continue;
488
489 match_pr = all_cpu_data[j];
490 if (!match_pr)
491 continue;
492
493 match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
8343c40d
HT
494 if (!match_cpc_ptr) {
495 retval = -EFAULT;
496 goto err_ret;
497 }
337aadff
AC
498
499 match_pdomain = &(match_cpc_ptr->domain_info);
500 if (match_pdomain->domain != pdomain->domain)
501 continue;
502
503 match_pr->shared_type = pr->shared_type;
504 cpumask_copy(match_pr->shared_cpu_map,
505 pr->shared_cpu_map);
506 }
507 }
508
509err_ret:
510 for_each_possible_cpu(i) {
511 pr = all_cpu_data[i];
512 if (!pr)
513 continue;
514
515 /* Assume no coordination on any error parsing domain info */
516 if (retval) {
517 cpumask_clear(pr->shared_cpu_map);
518 cpumask_set_cpu(i, pr->shared_cpu_map);
519 pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
520 }
521 }
522
523 free_cpumask_var(covered_cpus);
524 return retval;
525}
526EXPORT_SYMBOL_GPL(acpi_get_psd_map);
527
32c0b2f6 528static int register_pcc_channel(int pcc_subspace_idx)
337aadff 529{
d29d6735 530 struct acpi_pcct_hw_reduced *cppc_ss;
ad62e1e6 531 u64 usecs_lat;
337aadff
AC
532
533 if (pcc_subspace_idx >= 0) {
8482ef8c 534 pcc_data.pcc_channel = pcc_mbox_request_channel(&cppc_mbox_cl,
337aadff
AC
535 pcc_subspace_idx);
536
8482ef8c 537 if (IS_ERR(pcc_data.pcc_channel)) {
337aadff
AC
538 pr_err("Failed to find PCC communication channel\n");
539 return -ENODEV;
540 }
541
542 /*
543 * The PCC mailbox controller driver should
544 * have parsed the PCCT (global table of all
545 * PCC channels) and stored pointers to the
546 * subspace communication region in con_priv.
547 */
8482ef8c 548 cppc_ss = (pcc_data.pcc_channel)->con_priv;
337aadff
AC
549
550 if (!cppc_ss) {
551 pr_err("No PCC subspace found for CPPC\n");
552 return -ENODEV;
553 }
554
ad62e1e6
AC
555 /*
556 * cppc_ss->latency is just a Nominal value. In reality
557 * the remote processor could be much slower to reply.
558 * So add an arbitrary amount of wait on top of Nominal.
559 */
560 usecs_lat = NUM_RETRIES * cppc_ss->latency;
8482ef8c
PP
561 pcc_data.deadline = ns_to_ktime(usecs_lat * NSEC_PER_USEC);
562 pcc_data.pcc_mrtt = cppc_ss->min_turnaround_time;
563 pcc_data.pcc_mpar = cppc_ss->max_access_rate;
564 pcc_data.pcc_nominal = cppc_ss->latency;
337aadff 565
8482ef8c
PP
566 pcc_data.pcc_comm_addr = acpi_os_ioremap(cppc_ss->base_address, cppc_ss->length);
567 if (!pcc_data.pcc_comm_addr) {
337aadff
AC
568 pr_err("Failed to ioremap PCC comm region mem\n");
569 return -ENOMEM;
570 }
571
572 /* Set flag so that we dont come here for each CPU. */
8482ef8c 573 pcc_data.pcc_channel_acquired = true;
337aadff
AC
574 }
575
576 return 0;
577}
578
579/*
580 * An example CPC table looks like the following.
581 *
582 * Name(_CPC, Package()
583 * {
584 * 17,
585 * NumEntries
586 * 1,
587 * // Revision
588 * ResourceTemplate(){Register(PCC, 32, 0, 0x120, 2)},
589 * // Highest Performance
590 * ResourceTemplate(){Register(PCC, 32, 0, 0x124, 2)},
591 * // Nominal Performance
592 * ResourceTemplate(){Register(PCC, 32, 0, 0x128, 2)},
593 * // Lowest Nonlinear Performance
594 * ResourceTemplate(){Register(PCC, 32, 0, 0x12C, 2)},
595 * // Lowest Performance
596 * ResourceTemplate(){Register(PCC, 32, 0, 0x130, 2)},
597 * // Guaranteed Performance Register
598 * ResourceTemplate(){Register(PCC, 32, 0, 0x110, 2)},
599 * // Desired Performance Register
600 * ResourceTemplate(){Register(SystemMemory, 0, 0, 0, 0)},
601 * ..
602 * ..
603 * ..
604 *
605 * }
606 * Each Register() encodes how to access that specific register.
607 * e.g. a sample PCC entry has the following encoding:
608 *
609 * Register (
610 * PCC,
611 * AddressSpaceKeyword
612 * 8,
613 * //RegisterBitWidth
614 * 8,
615 * //RegisterBitOffset
616 * 0x30,
617 * //RegisterAddress
618 * 9
619 * //AccessSize (subspace ID)
620 * 0
621 * )
622 * }
623 */
624
625/**
626 * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
627 * @pr: Ptr to acpi_processor containing this CPUs logical Id.
628 *
629 * Return: 0 for success or negative value for err.
630 */
631int acpi_cppc_processor_probe(struct acpi_processor *pr)
632{
633 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
634 union acpi_object *out_obj, *cpc_obj;
635 struct cpc_desc *cpc_ptr;
636 struct cpc_reg *gas_t;
158c998e 637 struct device *cpu_dev;
337aadff
AC
638 acpi_handle handle = pr->handle;
639 unsigned int num_ent, i, cpc_rev;
640 acpi_status status;
641 int ret = -EFAULT;
642
643 /* Parse the ACPI _CPC table for this cpu. */
644 status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
645 ACPI_TYPE_PACKAGE);
646 if (ACPI_FAILURE(status)) {
647 ret = -ENODEV;
648 goto out_buf_free;
649 }
650
651 out_obj = (union acpi_object *) output.pointer;
652
653 cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL);
654 if (!cpc_ptr) {
655 ret = -ENOMEM;
656 goto out_buf_free;
657 }
658
659 /* First entry is NumEntries. */
660 cpc_obj = &out_obj->package.elements[0];
661 if (cpc_obj->type == ACPI_TYPE_INTEGER) {
662 num_ent = cpc_obj->integer.value;
663 } else {
664 pr_debug("Unexpected entry type(%d) for NumEntries\n",
665 cpc_obj->type);
666 goto out_free;
667 }
668
669 /* Only support CPPCv2. Bail otherwise. */
670 if (num_ent != CPPC_NUM_ENT) {
671 pr_debug("Firmware exports %d entries. Expected: %d\n",
672 num_ent, CPPC_NUM_ENT);
673 goto out_free;
674 }
675
5bbb86aa
AC
676 cpc_ptr->num_entries = num_ent;
677
337aadff
AC
678 /* Second entry should be revision. */
679 cpc_obj = &out_obj->package.elements[1];
680 if (cpc_obj->type == ACPI_TYPE_INTEGER) {
681 cpc_rev = cpc_obj->integer.value;
682 } else {
683 pr_debug("Unexpected entry type(%d) for Revision\n",
684 cpc_obj->type);
685 goto out_free;
686 }
687
688 if (cpc_rev != CPPC_REV) {
689 pr_debug("Firmware exports revision:%d. Expected:%d\n",
690 cpc_rev, CPPC_REV);
691 goto out_free;
692 }
693
694 /* Iterate through remaining entries in _CPC */
695 for (i = 2; i < num_ent; i++) {
696 cpc_obj = &out_obj->package.elements[i];
697
698 if (cpc_obj->type == ACPI_TYPE_INTEGER) {
699 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
700 cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
701 } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
702 gas_t = (struct cpc_reg *)
703 cpc_obj->buffer.pointer;
704
705 /*
706 * The PCC Subspace index is encoded inside
707 * the CPC table entries. The same PCC index
708 * will be used for all the PCC entries,
709 * so extract it only once.
710 */
711 if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
8482ef8c
PP
712 if (pcc_data.pcc_subspace_idx < 0)
713 pcc_data.pcc_subspace_idx = gas_t->access_width;
714 else if (pcc_data.pcc_subspace_idx != gas_t->access_width) {
337aadff
AC
715 pr_debug("Mismatched PCC ids.\n");
716 goto out_free;
717 }
5bbb86aa
AC
718 } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
719 if (gas_t->address) {
720 void __iomem *addr;
721
722 addr = ioremap(gas_t->address, gas_t->bit_width/8);
723 if (!addr)
724 goto out_free;
725 cpc_ptr->cpc_regs[i-2].sys_mem_vaddr = addr;
726 }
727 } else {
337aadff
AC
728 /* Support only PCC and SYS MEM type regs */
729 pr_debug("Unsupported register type: %d\n", gas_t->space_id);
730 goto out_free;
731 }
732
733 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
734 memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
735 } else {
736 pr_debug("Err in entry:%d in CPC table of CPU:%d \n", i, pr->id);
737 goto out_free;
738 }
739 }
740 /* Store CPU Logical ID */
741 cpc_ptr->cpu_id = pr->id;
742
337aadff
AC
743 /* Parse PSD data for this CPU */
744 ret = acpi_get_psd(cpc_ptr, handle);
745 if (ret)
746 goto out_free;
747
748 /* Register PCC channel once for all CPUs. */
8482ef8c
PP
749 if (!pcc_data.pcc_channel_acquired) {
750 ret = register_pcc_channel(pcc_data.pcc_subspace_idx);
337aadff
AC
751 if (ret)
752 goto out_free;
8482ef8c
PP
753
754 init_rwsem(&pcc_data.pcc_lock);
755 init_waitqueue_head(&pcc_data.pcc_write_wait_q);
337aadff
AC
756 }
757
2324d154
HT
758 /* Plug PSD data into this CPUs CPC descriptor. */
759 per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
760
337aadff
AC
761 /* Everything looks okay */
762 pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
763
158c998e
AC
764 /* Add per logical CPU nodes for reading its feedback counters. */
765 cpu_dev = get_cpu_device(pr->id);
766 if (!cpu_dev)
767 goto out_free;
768
769 ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj,
770 "acpi_cppc");
771 if (ret)
772 goto out_free;
773
337aadff
AC
774 kfree(output.pointer);
775 return 0;
776
777out_free:
5bbb86aa
AC
778 /* Free all the mapped sys mem areas for this CPU */
779 for (i = 2; i < cpc_ptr->num_entries; i++) {
780 void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
781
782 if (addr)
783 iounmap(addr);
784 }
337aadff
AC
785 kfree(cpc_ptr);
786
787out_buf_free:
788 kfree(output.pointer);
789 return ret;
790}
791EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
792
793/**
794 * acpi_cppc_processor_exit - Cleanup CPC structs.
795 * @pr: Ptr to acpi_processor containing this CPUs logical Id.
796 *
797 * Return: Void
798 */
799void acpi_cppc_processor_exit(struct acpi_processor *pr)
800{
801 struct cpc_desc *cpc_ptr;
5bbb86aa
AC
802 unsigned int i;
803 void __iomem *addr;
158c998e 804
337aadff 805 cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
5bbb86aa
AC
806
807 /* Free all the mapped sys mem areas for this CPU */
808 for (i = 2; i < cpc_ptr->num_entries; i++) {
809 addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
810 if (addr)
811 iounmap(addr);
812 }
813
158c998e 814 kobject_put(&cpc_ptr->kobj);
337aadff
AC
815 kfree(cpc_ptr);
816}
817EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
818
77e3d86f
PP
819/*
820 * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
821 * as fast as possible. We have already mapped the PCC subspace during init, so
822 * we can directly write to it.
823 */
337aadff 824
5bbb86aa 825static int cpc_read(struct cpc_register_resource *reg_res, u64 *val)
337aadff 826{
77e3d86f 827 int ret_val = 0;
5bbb86aa
AC
828 void __iomem *vaddr = 0;
829 struct cpc_reg *reg = &reg_res->cpc_entry.reg;
830
831 if (reg_res->type == ACPI_TYPE_INTEGER) {
832 *val = reg_res->cpc_entry.int_value;
833 return ret_val;
834 }
77e3d86f
PP
835
836 *val = 0;
5bbb86aa
AC
837 if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
838 vaddr = GET_PCC_VADDR(reg->address);
839 else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
840 vaddr = reg_res->sys_mem_vaddr;
841 else
842 return acpi_os_read_memory((acpi_physical_address)reg->address,
843 val, reg->bit_width);
337aadff 844
5bbb86aa 845 switch (reg->bit_width) {
77e3d86f 846 case 8:
beee23ae 847 *val = readb_relaxed(vaddr);
77e3d86f
PP
848 break;
849 case 16:
beee23ae 850 *val = readw_relaxed(vaddr);
77e3d86f
PP
851 break;
852 case 32:
beee23ae 853 *val = readl_relaxed(vaddr);
77e3d86f
PP
854 break;
855 case 64:
beee23ae 856 *val = readq_relaxed(vaddr);
77e3d86f
PP
857 break;
858 default:
859 pr_debug("Error: Cannot read %u bit width from PCC\n",
5bbb86aa 860 reg->bit_width);
77e3d86f 861 ret_val = -EFAULT;
5bbb86aa
AC
862 }
863
77e3d86f 864 return ret_val;
337aadff
AC
865}
866
5bbb86aa 867static int cpc_write(struct cpc_register_resource *reg_res, u64 val)
337aadff 868{
77e3d86f 869 int ret_val = 0;
5bbb86aa
AC
870 void __iomem *vaddr = 0;
871 struct cpc_reg *reg = &reg_res->cpc_entry.reg;
77e3d86f 872
5bbb86aa
AC
873 if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
874 vaddr = GET_PCC_VADDR(reg->address);
875 else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
876 vaddr = reg_res->sys_mem_vaddr;
877 else
878 return acpi_os_write_memory((acpi_physical_address)reg->address,
879 val, reg->bit_width);
337aadff 880
5bbb86aa 881 switch (reg->bit_width) {
77e3d86f 882 case 8:
beee23ae 883 writeb_relaxed(val, vaddr);
77e3d86f
PP
884 break;
885 case 16:
beee23ae 886 writew_relaxed(val, vaddr);
77e3d86f
PP
887 break;
888 case 32:
beee23ae 889 writel_relaxed(val, vaddr);
77e3d86f
PP
890 break;
891 case 64:
beee23ae 892 writeq_relaxed(val, vaddr);
77e3d86f
PP
893 break;
894 default:
895 pr_debug("Error: Cannot write %u bit width to PCC\n",
5bbb86aa 896 reg->bit_width);
77e3d86f
PP
897 ret_val = -EFAULT;
898 break;
5bbb86aa
AC
899 }
900
77e3d86f 901 return ret_val;
337aadff
AC
902}
903
904/**
905 * cppc_get_perf_caps - Get a CPUs performance capabilities.
906 * @cpunum: CPU from which to get capabilities info.
907 * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
908 *
909 * Return: 0 for success with perf_caps populated else -ERRNO.
910 */
911int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
912{
913 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
914 struct cpc_register_resource *highest_reg, *lowest_reg, *ref_perf,
915 *nom_perf;
158c998e 916 u64 high, low, nom;
850d64a4 917 int ret = 0, regs_in_pcc = 0;
337aadff
AC
918
919 if (!cpc_desc) {
920 pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
921 return -ENODEV;
922 }
923
924 highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
925 lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
926 ref_perf = &cpc_desc->cpc_regs[REFERENCE_PERF];
927 nom_perf = &cpc_desc->cpc_regs[NOMINAL_PERF];
928
337aadff 929 /* Are any of the regs PCC ?*/
80b8286a
PP
930 if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
931 CPC_IN_PCC(ref_perf) || CPC_IN_PCC(nom_perf)) {
850d64a4 932 regs_in_pcc = 1;
8482ef8c 933 down_write(&pcc_data.pcc_lock);
337aadff 934 /* Ring doorbell once to update PCC subspace */
ad62e1e6 935 if (send_pcc_cmd(CMD_READ) < 0) {
337aadff
AC
936 ret = -EIO;
937 goto out_err;
938 }
939 }
940
5bbb86aa 941 cpc_read(highest_reg, &high);
337aadff
AC
942 perf_caps->highest_perf = high;
943
5bbb86aa 944 cpc_read(lowest_reg, &low);
337aadff
AC
945 perf_caps->lowest_perf = low;
946
5bbb86aa 947 cpc_read(nom_perf, &nom);
337aadff
AC
948 perf_caps->nominal_perf = nom;
949
337aadff
AC
950 if (!high || !low || !nom)
951 ret = -EFAULT;
952
953out_err:
850d64a4 954 if (regs_in_pcc)
8482ef8c 955 up_write(&pcc_data.pcc_lock);
337aadff
AC
956 return ret;
957}
958EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
959
960/**
961 * cppc_get_perf_ctrs - Read a CPUs performance feedback counters.
962 * @cpunum: CPU from which to read counters.
963 * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
964 *
965 * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
966 */
967int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
968{
969 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
158c998e
AC
970 struct cpc_register_resource *delivered_reg, *reference_reg,
971 *ref_perf_reg, *ctr_wrap_reg;
972 u64 delivered, reference, ref_perf, ctr_wrap_time;
850d64a4 973 int ret = 0, regs_in_pcc = 0;
337aadff
AC
974
975 if (!cpc_desc) {
976 pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
977 return -ENODEV;
978 }
979
980 delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
981 reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
158c998e
AC
982 ref_perf_reg = &cpc_desc->cpc_regs[REFERENCE_PERF];
983 ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME];
984
985 /*
986 * If refernce perf register is not supported then we should
987 * use the nominal perf value
988 */
989 if (!CPC_SUPPORTED(ref_perf_reg))
990 ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
337aadff 991
337aadff 992 /* Are any of the regs PCC ?*/
158c998e
AC
993 if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) ||
994 CPC_IN_PCC(ctr_wrap_reg) || CPC_IN_PCC(ref_perf_reg)) {
8482ef8c 995 down_write(&pcc_data.pcc_lock);
850d64a4 996 regs_in_pcc = 1;
337aadff 997 /* Ring doorbell once to update PCC subspace */
ad62e1e6 998 if (send_pcc_cmd(CMD_READ) < 0) {
337aadff
AC
999 ret = -EIO;
1000 goto out_err;
1001 }
1002 }
1003
5bbb86aa
AC
1004 cpc_read(delivered_reg, &delivered);
1005 cpc_read(reference_reg, &reference);
158c998e
AC
1006 cpc_read(ref_perf_reg, &ref_perf);
1007
1008 /*
1009 * Per spec, if ctr_wrap_time optional register is unsupported, then the
1010 * performance counters are assumed to never wrap during the lifetime of
1011 * platform
1012 */
1013 ctr_wrap_time = (u64)(~((u64)0));
1014 if (CPC_SUPPORTED(ctr_wrap_reg))
1015 cpc_read(ctr_wrap_reg, &ctr_wrap_time);
337aadff 1016
158c998e 1017 if (!delivered || !reference || !ref_perf) {
337aadff
AC
1018 ret = -EFAULT;
1019 goto out_err;
1020 }
1021
1022 perf_fb_ctrs->delivered = delivered;
1023 perf_fb_ctrs->reference = reference;
158c998e
AC
1024 perf_fb_ctrs->reference_perf = ref_perf;
1025 perf_fb_ctrs->ctr_wrap_time = ctr_wrap_time;
337aadff 1026out_err:
850d64a4 1027 if (regs_in_pcc)
8482ef8c 1028 up_write(&pcc_data.pcc_lock);
337aadff
AC
1029 return ret;
1030}
1031EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
1032
1033/**
1034 * cppc_set_perf - Set a CPUs performance controls.
1035 * @cpu: CPU for which to set performance controls.
1036 * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
1037 *
1038 * Return: 0 for success, -ERRNO otherwise.
1039 */
1040int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
1041{
1042 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
1043 struct cpc_register_resource *desired_reg;
1044 int ret = 0;
1045
1046 if (!cpc_desc) {
1047 pr_debug("No CPC descriptor for CPU:%d\n", cpu);
1048 return -ENODEV;
1049 }
1050
1051 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
1052
80b8286a
PP
1053 /*
1054 * This is Phase-I where we want to write to CPC registers
1055 * -> We want all CPUs to be able to execute this phase in parallel
1056 *
1057 * Since read_lock can be acquired by multiple CPUs simultaneously we
1058 * achieve that goal here
1059 */
1060 if (CPC_IN_PCC(desired_reg)) {
8482ef8c 1061 down_read(&pcc_data.pcc_lock); /* BEGIN Phase-I */
80b8286a
PP
1062 /*
1063 * If there are pending write commands i.e pending_pcc_write_cmd
1064 * is TRUE, then we know OSPM owns the channel as another CPU
1065 * has already checked for command completion bit and updated
1066 * the corresponding CPC registers
1067 */
8482ef8c 1068 if (!pcc_data.pending_pcc_write_cmd) {
80b8286a
PP
1069 ret = check_pcc_chan();
1070 if (ret) {
8482ef8c 1071 up_read(&pcc_data.pcc_lock);
80b8286a
PP
1072 return ret;
1073 }
1074 /*
1075 * Update the pending_write to make sure a PCC CMD_READ
1076 * will not arrive and steal the channel during the
1077 * transition to write lock
1078 */
8482ef8c 1079 pcc_data.pending_pcc_write_cmd = TRUE;
80b8286a 1080 }
8482ef8c 1081 cpc_desc->write_cmd_id = pcc_data.pcc_write_cnt;
80b8286a 1082 cpc_desc->write_cmd_status = 0;
ad62e1e6
AC
1083 }
1084
337aadff
AC
1085 /*
1086 * Skip writing MIN/MAX until Linux knows how to come up with
1087 * useful values.
1088 */
5bbb86aa 1089 cpc_write(desired_reg, perf_ctrls->desired_perf);
337aadff 1090
80b8286a 1091 if (CPC_IN_PCC(desired_reg))
8482ef8c 1092 up_read(&pcc_data.pcc_lock); /* END Phase-I */
80b8286a
PP
1093 /*
1094 * This is Phase-II where we transfer the ownership of PCC to Platform
1095 *
1096 * Short Summary: Basically if we think of a group of cppc_set_perf
1097 * requests that happened in short overlapping interval. The last CPU to
1098 * come out of Phase-I will enter Phase-II and ring the doorbell.
1099 *
1100 * We have the following requirements for Phase-II:
1101 * 1. We want to execute Phase-II only when there are no CPUs
1102 * currently executing in Phase-I
1103 * 2. Once we start Phase-II we want to avoid all other CPUs from
1104 * entering Phase-I.
1105 * 3. We want only one CPU among all those who went through Phase-I
1106 * to run phase-II
1107 *
1108 * If write_trylock fails to get the lock and doesn't transfer the
1109 * PCC ownership to the platform, then one of the following will be TRUE
1110 * 1. There is at-least one CPU in Phase-I which will later execute
1111 * write_trylock, so the CPUs in Phase-I will be responsible for
1112 * executing the Phase-II.
1113 * 2. Some other CPU has beaten this CPU to successfully execute the
1114 * write_trylock and has already acquired the write_lock. We know for a
1115 * fact it(other CPU acquiring the write_lock) couldn't have happened
1116 * before this CPU's Phase-I as we held the read_lock.
1117 * 3. Some other CPU executing pcc CMD_READ has stolen the
1118 * down_write, in which case, send_pcc_cmd will check for pending
1119 * CMD_WRITE commands by checking the pending_pcc_write_cmd.
1120 * So this CPU can be certain that its request will be delivered
1121 * So in all cases, this CPU knows that its request will be delivered
1122 * by another CPU and can return
1123 *
1124 * After getting the down_write we still need to check for
1125 * pending_pcc_write_cmd to take care of the following scenario
1126 * The thread running this code could be scheduled out between
1127 * Phase-I and Phase-II. Before it is scheduled back on, another CPU
1128 * could have delivered the request to Platform by triggering the
1129 * doorbell and transferred the ownership of PCC to platform. So this
1130 * avoids triggering an unnecessary doorbell and more importantly before
1131 * triggering the doorbell it makes sure that the PCC channel ownership
1132 * is still with OSPM.
1133 * pending_pcc_write_cmd can also be cleared by a different CPU, if
1134 * there was a pcc CMD_READ waiting on down_write and it steals the lock
1135 * before the pcc CMD_WRITE is completed. pcc_send_cmd checks for this
1136 * case during a CMD_READ and if there are pending writes it delivers
1137 * the write command before servicing the read command
1138 */
1139 if (CPC_IN_PCC(desired_reg)) {
8482ef8c 1140 if (down_write_trylock(&pcc_data.pcc_lock)) { /* BEGIN Phase-II */
80b8286a 1141 /* Update only if there are pending write commands */
8482ef8c 1142 if (pcc_data.pending_pcc_write_cmd)
80b8286a 1143 send_pcc_cmd(CMD_WRITE);
8482ef8c 1144 up_write(&pcc_data.pcc_lock); /* END Phase-II */
80b8286a
PP
1145 } else
1146 /* Wait until pcc_write_cnt is updated by send_pcc_cmd */
8482ef8c
PP
1147 wait_event(pcc_data.pcc_write_wait_q,
1148 cpc_desc->write_cmd_id != pcc_data.pcc_write_cnt);
80b8286a
PP
1149
1150 /* send_pcc_cmd updates the status in case of failure */
1151 ret = cpc_desc->write_cmd_status;
337aadff 1152 }
337aadff
AC
1153 return ret;
1154}
1155EXPORT_SYMBOL_GPL(cppc_set_perf);
be8b88d7
PP
1156
1157/**
1158 * cppc_get_transition_latency - returns frequency transition latency in ns
1159 *
1160 * ACPI CPPC does not explicitly specifiy how a platform can specify the
1161 * transition latency for perfromance change requests. The closest we have
1162 * is the timing information from the PCCT tables which provides the info
1163 * on the number and frequency of PCC commands the platform can handle.
1164 */
1165unsigned int cppc_get_transition_latency(int cpu_num)
1166{
1167 /*
1168 * Expected transition latency is based on the PCCT timing values
1169 * Below are definition from ACPI spec:
1170 * pcc_nominal- Expected latency to process a command, in microseconds
1171 * pcc_mpar - The maximum number of periodic requests that the subspace
1172 * channel can support, reported in commands per minute. 0
1173 * indicates no limitation.
1174 * pcc_mrtt - The minimum amount of time that OSPM must wait after the
1175 * completion of a command before issuing the next command,
1176 * in microseconds.
1177 */
1178 unsigned int latency_ns = 0;
1179 struct cpc_desc *cpc_desc;
1180 struct cpc_register_resource *desired_reg;
1181
1182 cpc_desc = per_cpu(cpc_desc_ptr, cpu_num);
1183 if (!cpc_desc)
1184 return CPUFREQ_ETERNAL;
1185
1186 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
1187 if (!CPC_IN_PCC(desired_reg))
1188 return CPUFREQ_ETERNAL;
1189
8482ef8c
PP
1190 if (pcc_data.pcc_mpar)
1191 latency_ns = 60 * (1000 * 1000 * 1000 / pcc_data.pcc_mpar);
be8b88d7 1192
8482ef8c
PP
1193 latency_ns = max(latency_ns, pcc_data.pcc_nominal * 1000);
1194 latency_ns = max(latency_ns, pcc_data.pcc_mrtt * 1000);
be8b88d7
PP
1195
1196 return latency_ns;
1197}
1198EXPORT_SYMBOL_GPL(cppc_get_transition_latency);