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