blob: 2826bd45c61a1e2de835ae16da97fb49dc984385 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 * 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
David Brazdil0f672f62019-12-10 10:32:29 +000080/* Array to represent the PCC channel per subspace ID */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081static struct cppc_pcc_data *pcc_data[MAX_PCC_SUBSPACES];
David Brazdil0f672f62019-12-10 10:32:29 +000082/* The cpu_pcc_subspace_idx contains per CPU subspace ID */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083static 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121#define define_one_cppc_ro(_name) \
Olivier Deprez0e641232021-09-23 10:07:05 +0200122static struct kobj_attribute _name = \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123__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, \
Olivier Deprez0e641232021-09-23 10:07:05 +0200129 struct kobj_attribute *attr, char *buf) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000130 { \
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,
Olivier Deprez0e641232021-09-23 10:07:05 +0200155 struct kobj_attribute *attr, char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000156{
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
David Brazdil0f672f62019-12-10 10:32:29 +0000360 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000364 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 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000429 * Now that we have _PSD data from all CPUs, let's setup P-state
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430 * 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
David Brazdil0f672f62019-12-10 10:32:29 +0000581 /* Set flag so that we don't come here for each CPU. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000582 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
David Brazdil0f672f62019-12-10 10:32:29 +0000606 * is shared between multiple CPUs. This is seen especially in CPUs
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000607 * 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/* Check if CPPC revision + num_ent combination is supported */
630static bool is_cppc_supported(int revision, int num_ent)
631{
632 int expected_num_ent;
633
634 switch (revision) {
635 case CPPC_V2_REV:
636 expected_num_ent = CPPC_V2_NUM_ENT;
637 break;
638 case CPPC_V3_REV:
639 expected_num_ent = CPPC_V3_NUM_ENT;
640 break;
641 default:
642 pr_debug("Firmware exports unsupported CPPC revision: %d\n",
643 revision);
644 return false;
645 }
646
647 if (expected_num_ent != num_ent) {
648 pr_debug("Firmware exports %d entries. Expected: %d for CPPC rev:%d\n",
649 num_ent, expected_num_ent, revision);
650 return false;
651 }
652
653 return true;
654}
655
656/*
657 * An example CPC table looks like the following.
658 *
659 * Name(_CPC, Package()
660 * {
661 * 17,
662 * NumEntries
663 * 1,
664 * // Revision
665 * ResourceTemplate(){Register(PCC, 32, 0, 0x120, 2)},
666 * // Highest Performance
667 * ResourceTemplate(){Register(PCC, 32, 0, 0x124, 2)},
668 * // Nominal Performance
669 * ResourceTemplate(){Register(PCC, 32, 0, 0x128, 2)},
670 * // Lowest Nonlinear Performance
671 * ResourceTemplate(){Register(PCC, 32, 0, 0x12C, 2)},
672 * // Lowest Performance
673 * ResourceTemplate(){Register(PCC, 32, 0, 0x130, 2)},
674 * // Guaranteed Performance Register
675 * ResourceTemplate(){Register(PCC, 32, 0, 0x110, 2)},
676 * // Desired Performance Register
677 * ResourceTemplate(){Register(SystemMemory, 0, 0, 0, 0)},
678 * ..
679 * ..
680 * ..
681 *
682 * }
683 * Each Register() encodes how to access that specific register.
684 * e.g. a sample PCC entry has the following encoding:
685 *
686 * Register (
687 * PCC,
688 * AddressSpaceKeyword
689 * 8,
690 * //RegisterBitWidth
691 * 8,
692 * //RegisterBitOffset
693 * 0x30,
694 * //RegisterAddress
695 * 9
696 * //AccessSize (subspace ID)
697 * 0
698 * )
699 * }
700 */
701
702/**
703 * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
David Brazdil0f672f62019-12-10 10:32:29 +0000704 * @pr: Ptr to acpi_processor containing this CPU's logical ID.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000705 *
706 * Return: 0 for success or negative value for err.
707 */
708int acpi_cppc_processor_probe(struct acpi_processor *pr)
709{
710 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
711 union acpi_object *out_obj, *cpc_obj;
712 struct cpc_desc *cpc_ptr;
713 struct cpc_reg *gas_t;
714 struct device *cpu_dev;
715 acpi_handle handle = pr->handle;
716 unsigned int num_ent, i, cpc_rev;
717 int pcc_subspace_id = -1;
718 acpi_status status;
719 int ret = -EFAULT;
720
David Brazdil0f672f62019-12-10 10:32:29 +0000721 /* Parse the ACPI _CPC table for this CPU. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000722 status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
723 ACPI_TYPE_PACKAGE);
724 if (ACPI_FAILURE(status)) {
725 ret = -ENODEV;
726 goto out_buf_free;
727 }
728
729 out_obj = (union acpi_object *) output.pointer;
730
731 cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL);
732 if (!cpc_ptr) {
733 ret = -ENOMEM;
734 goto out_buf_free;
735 }
736
737 /* First entry is NumEntries. */
738 cpc_obj = &out_obj->package.elements[0];
739 if (cpc_obj->type == ACPI_TYPE_INTEGER) {
740 num_ent = cpc_obj->integer.value;
741 } else {
742 pr_debug("Unexpected entry type(%d) for NumEntries\n",
743 cpc_obj->type);
744 goto out_free;
745 }
746 cpc_ptr->num_entries = num_ent;
747
748 /* Second entry should be revision. */
749 cpc_obj = &out_obj->package.elements[1];
750 if (cpc_obj->type == ACPI_TYPE_INTEGER) {
751 cpc_rev = cpc_obj->integer.value;
752 } else {
753 pr_debug("Unexpected entry type(%d) for Revision\n",
754 cpc_obj->type);
755 goto out_free;
756 }
757 cpc_ptr->version = cpc_rev;
758
759 if (!is_cppc_supported(cpc_rev, num_ent))
760 goto out_free;
761
762 /* Iterate through remaining entries in _CPC */
763 for (i = 2; i < num_ent; i++) {
764 cpc_obj = &out_obj->package.elements[i];
765
766 if (cpc_obj->type == ACPI_TYPE_INTEGER) {
767 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
768 cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
769 } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
770 gas_t = (struct cpc_reg *)
771 cpc_obj->buffer.pointer;
772
773 /*
774 * The PCC Subspace index is encoded inside
775 * the CPC table entries. The same PCC index
776 * will be used for all the PCC entries,
777 * so extract it only once.
778 */
779 if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
780 if (pcc_subspace_id < 0) {
781 pcc_subspace_id = gas_t->access_width;
782 if (pcc_data_alloc(pcc_subspace_id))
783 goto out_free;
784 } else if (pcc_subspace_id != gas_t->access_width) {
785 pr_debug("Mismatched PCC ids.\n");
786 goto out_free;
787 }
788 } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
789 if (gas_t->address) {
790 void __iomem *addr;
791
792 addr = ioremap(gas_t->address, gas_t->bit_width/8);
793 if (!addr)
794 goto out_free;
795 cpc_ptr->cpc_regs[i-2].sys_mem_vaddr = addr;
796 }
797 } else {
798 if (gas_t->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported()) {
799 /* Support only PCC ,SYS MEM and FFH type regs */
800 pr_debug("Unsupported register type: %d\n", gas_t->space_id);
801 goto out_free;
802 }
803 }
804
805 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
806 memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
807 } else {
808 pr_debug("Err in entry:%d in CPC table of CPU:%d \n", i, pr->id);
809 goto out_free;
810 }
811 }
812 per_cpu(cpu_pcc_subspace_idx, pr->id) = pcc_subspace_id;
813
814 /*
815 * Initialize the remaining cpc_regs as unsupported.
816 * Example: In case FW exposes CPPC v2, the below loop will initialize
817 * LOWEST_FREQ and NOMINAL_FREQ regs as unsupported
818 */
819 for (i = num_ent - 2; i < MAX_CPC_REG_ENT; i++) {
820 cpc_ptr->cpc_regs[i].type = ACPI_TYPE_INTEGER;
821 cpc_ptr->cpc_regs[i].cpc_entry.int_value = 0;
822 }
823
824
825 /* Store CPU Logical ID */
826 cpc_ptr->cpu_id = pr->id;
827
828 /* Parse PSD data for this CPU */
829 ret = acpi_get_psd(cpc_ptr, handle);
830 if (ret)
831 goto out_free;
832
David Brazdil0f672f62019-12-10 10:32:29 +0000833 /* Register PCC channel once for all PCC subspace ID. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000834 if (pcc_subspace_id >= 0 && !pcc_data[pcc_subspace_id]->pcc_channel_acquired) {
835 ret = register_pcc_channel(pcc_subspace_id);
836 if (ret)
837 goto out_free;
838
839 init_rwsem(&pcc_data[pcc_subspace_id]->pcc_lock);
840 init_waitqueue_head(&pcc_data[pcc_subspace_id]->pcc_write_wait_q);
841 }
842
843 /* Everything looks okay */
844 pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
845
846 /* Add per logical CPU nodes for reading its feedback counters. */
847 cpu_dev = get_cpu_device(pr->id);
848 if (!cpu_dev) {
849 ret = -EINVAL;
850 goto out_free;
851 }
852
David Brazdil0f672f62019-12-10 10:32:29 +0000853 /* Plug PSD data into this CPU's CPC descriptor. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000854 per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
855
856 ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj,
857 "acpi_cppc");
858 if (ret) {
859 per_cpu(cpc_desc_ptr, pr->id) = NULL;
Olivier Deprez0e641232021-09-23 10:07:05 +0200860 kobject_put(&cpc_ptr->kobj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000861 goto out_free;
862 }
863
864 kfree(output.pointer);
865 return 0;
866
867out_free:
868 /* Free all the mapped sys mem areas for this CPU */
869 for (i = 2; i < cpc_ptr->num_entries; i++) {
870 void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
871
872 if (addr)
873 iounmap(addr);
874 }
875 kfree(cpc_ptr);
876
877out_buf_free:
878 kfree(output.pointer);
879 return ret;
880}
881EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
882
883/**
884 * acpi_cppc_processor_exit - Cleanup CPC structs.
David Brazdil0f672f62019-12-10 10:32:29 +0000885 * @pr: Ptr to acpi_processor containing this CPU's logical ID.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000886 *
887 * Return: Void
888 */
889void acpi_cppc_processor_exit(struct acpi_processor *pr)
890{
891 struct cpc_desc *cpc_ptr;
892 unsigned int i;
893 void __iomem *addr;
894 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id);
895
896 if (pcc_ss_id >=0 && pcc_data[pcc_ss_id]) {
897 if (pcc_data[pcc_ss_id]->pcc_channel_acquired) {
898 pcc_data[pcc_ss_id]->refcount--;
899 if (!pcc_data[pcc_ss_id]->refcount) {
900 pcc_mbox_free_channel(pcc_data[pcc_ss_id]->pcc_channel);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000901 kfree(pcc_data[pcc_ss_id]);
David Brazdil0f672f62019-12-10 10:32:29 +0000902 pcc_data[pcc_ss_id] = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000903 }
904 }
905 }
906
907 cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
908 if (!cpc_ptr)
909 return;
910
911 /* Free all the mapped sys mem areas for this CPU */
912 for (i = 2; i < cpc_ptr->num_entries; i++) {
913 addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
914 if (addr)
915 iounmap(addr);
916 }
917
918 kobject_put(&cpc_ptr->kobj);
919 kfree(cpc_ptr);
920}
921EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
922
923/**
924 * cpc_read_ffh() - Read FFH register
David Brazdil0f672f62019-12-10 10:32:29 +0000925 * @cpunum: CPU number to read
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000926 * @reg: cppc register information
927 * @val: place holder for return value
928 *
929 * Read bit_width bits from a specified address and bit_offset
930 *
931 * Return: 0 for success and error code
932 */
933int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
934{
935 return -ENOTSUPP;
936}
937
938/**
939 * cpc_write_ffh() - Write FFH register
David Brazdil0f672f62019-12-10 10:32:29 +0000940 * @cpunum: CPU number to write
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000941 * @reg: cppc register information
942 * @val: value to write
943 *
944 * Write value of bit_width bits to a specified address and bit_offset
945 *
946 * Return: 0 for success and error code
947 */
948int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
949{
950 return -ENOTSUPP;
951}
952
953/*
954 * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
955 * as fast as possible. We have already mapped the PCC subspace during init, so
956 * we can directly write to it.
957 */
958
959static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val)
960{
961 int ret_val = 0;
962 void __iomem *vaddr = 0;
963 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
964 struct cpc_reg *reg = &reg_res->cpc_entry.reg;
965
966 if (reg_res->type == ACPI_TYPE_INTEGER) {
967 *val = reg_res->cpc_entry.int_value;
968 return ret_val;
969 }
970
971 *val = 0;
972 if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0)
973 vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
974 else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
975 vaddr = reg_res->sys_mem_vaddr;
976 else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
977 return cpc_read_ffh(cpu, reg, val);
978 else
979 return acpi_os_read_memory((acpi_physical_address)reg->address,
980 val, reg->bit_width);
981
982 switch (reg->bit_width) {
983 case 8:
984 *val = readb_relaxed(vaddr);
985 break;
986 case 16:
987 *val = readw_relaxed(vaddr);
988 break;
989 case 32:
990 *val = readl_relaxed(vaddr);
991 break;
992 case 64:
993 *val = readq_relaxed(vaddr);
994 break;
995 default:
996 pr_debug("Error: Cannot read %u bit width from PCC for ss: %d\n",
997 reg->bit_width, pcc_ss_id);
998 ret_val = -EFAULT;
999 }
1000
1001 return ret_val;
1002}
1003
1004static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)
1005{
1006 int ret_val = 0;
1007 void __iomem *vaddr = 0;
1008 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
1009 struct cpc_reg *reg = &reg_res->cpc_entry.reg;
1010
1011 if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0)
1012 vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
1013 else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1014 vaddr = reg_res->sys_mem_vaddr;
1015 else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
1016 return cpc_write_ffh(cpu, reg, val);
1017 else
1018 return acpi_os_write_memory((acpi_physical_address)reg->address,
1019 val, reg->bit_width);
1020
1021 switch (reg->bit_width) {
1022 case 8:
1023 writeb_relaxed(val, vaddr);
1024 break;
1025 case 16:
1026 writew_relaxed(val, vaddr);
1027 break;
1028 case 32:
1029 writel_relaxed(val, vaddr);
1030 break;
1031 case 64:
1032 writeq_relaxed(val, vaddr);
1033 break;
1034 default:
1035 pr_debug("Error: Cannot write %u bit width to PCC for ss: %d\n",
1036 reg->bit_width, pcc_ss_id);
1037 ret_val = -EFAULT;
1038 break;
1039 }
1040
1041 return ret_val;
1042}
1043
1044/**
David Brazdil0f672f62019-12-10 10:32:29 +00001045 * cppc_get_desired_perf - Get the value of desired performance register.
1046 * @cpunum: CPU from which to get desired performance.
1047 * @desired_perf: address of a variable to store the returned desired performance
1048 *
1049 * Return: 0 for success, -EIO otherwise.
1050 */
1051int cppc_get_desired_perf(int cpunum, u64 *desired_perf)
1052{
1053 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
1054 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
1055 struct cpc_register_resource *desired_reg;
1056 struct cppc_pcc_data *pcc_ss_data = NULL;
1057
1058 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
1059
1060 if (CPC_IN_PCC(desired_reg)) {
1061 int ret = 0;
1062
1063 if (pcc_ss_id < 0)
1064 return -EIO;
1065
1066 pcc_ss_data = pcc_data[pcc_ss_id];
1067
1068 down_write(&pcc_ss_data->pcc_lock);
1069
1070 if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0)
1071 cpc_read(cpunum, desired_reg, desired_perf);
1072 else
1073 ret = -EIO;
1074
1075 up_write(&pcc_ss_data->pcc_lock);
1076
1077 return ret;
1078 }
1079
1080 cpc_read(cpunum, desired_reg, desired_perf);
1081
1082 return 0;
1083}
1084EXPORT_SYMBOL_GPL(cppc_get_desired_perf);
1085
1086/**
1087 * cppc_get_perf_caps - Get a CPU's performance capabilities.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001088 * @cpunum: CPU from which to get capabilities info.
1089 * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
1090 *
1091 * Return: 0 for success with perf_caps populated else -ERRNO.
1092 */
1093int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
1094{
1095 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
1096 struct cpc_register_resource *highest_reg, *lowest_reg,
David Brazdil0f672f62019-12-10 10:32:29 +00001097 *lowest_non_linear_reg, *nominal_reg, *guaranteed_reg,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001098 *low_freq_reg = NULL, *nom_freq_reg = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00001099 u64 high, low, guaranteed, nom, min_nonlinear, low_f = 0, nom_f = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001100 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
1101 struct cppc_pcc_data *pcc_ss_data = NULL;
1102 int ret = 0, regs_in_pcc = 0;
1103
1104 if (!cpc_desc) {
1105 pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
1106 return -ENODEV;
1107 }
1108
1109 highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
1110 lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
1111 lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF];
1112 nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
1113 low_freq_reg = &cpc_desc->cpc_regs[LOWEST_FREQ];
1114 nom_freq_reg = &cpc_desc->cpc_regs[NOMINAL_FREQ];
David Brazdil0f672f62019-12-10 10:32:29 +00001115 guaranteed_reg = &cpc_desc->cpc_regs[GUARANTEED_PERF];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001116
1117 /* Are any of the regs PCC ?*/
1118 if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
1119 CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg) ||
1120 CPC_IN_PCC(low_freq_reg) || CPC_IN_PCC(nom_freq_reg)) {
1121 if (pcc_ss_id < 0) {
1122 pr_debug("Invalid pcc_ss_id\n");
1123 return -ENODEV;
1124 }
1125 pcc_ss_data = pcc_data[pcc_ss_id];
1126 regs_in_pcc = 1;
1127 down_write(&pcc_ss_data->pcc_lock);
1128 /* Ring doorbell once to update PCC subspace */
1129 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
1130 ret = -EIO;
1131 goto out_err;
1132 }
1133 }
1134
1135 cpc_read(cpunum, highest_reg, &high);
1136 perf_caps->highest_perf = high;
1137
1138 cpc_read(cpunum, lowest_reg, &low);
1139 perf_caps->lowest_perf = low;
1140
1141 cpc_read(cpunum, nominal_reg, &nom);
1142 perf_caps->nominal_perf = nom;
1143
David Brazdil0f672f62019-12-10 10:32:29 +00001144 if (guaranteed_reg->type != ACPI_TYPE_BUFFER ||
1145 IS_NULL_REG(&guaranteed_reg->cpc_entry.reg)) {
1146 perf_caps->guaranteed_perf = 0;
1147 } else {
1148 cpc_read(cpunum, guaranteed_reg, &guaranteed);
1149 perf_caps->guaranteed_perf = guaranteed;
1150 }
1151
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001152 cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear);
1153 perf_caps->lowest_nonlinear_perf = min_nonlinear;
1154
1155 if (!high || !low || !nom || !min_nonlinear)
1156 ret = -EFAULT;
1157
1158 /* Read optional lowest and nominal frequencies if present */
1159 if (CPC_SUPPORTED(low_freq_reg))
1160 cpc_read(cpunum, low_freq_reg, &low_f);
1161
1162 if (CPC_SUPPORTED(nom_freq_reg))
1163 cpc_read(cpunum, nom_freq_reg, &nom_f);
1164
1165 perf_caps->lowest_freq = low_f;
1166 perf_caps->nominal_freq = nom_f;
1167
1168
1169out_err:
1170 if (regs_in_pcc)
1171 up_write(&pcc_ss_data->pcc_lock);
1172 return ret;
1173}
1174EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
1175
1176/**
David Brazdil0f672f62019-12-10 10:32:29 +00001177 * cppc_get_perf_ctrs - Read a CPU's performance feedback counters.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001178 * @cpunum: CPU from which to read counters.
1179 * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
1180 *
1181 * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
1182 */
1183int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
1184{
1185 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
1186 struct cpc_register_resource *delivered_reg, *reference_reg,
1187 *ref_perf_reg, *ctr_wrap_reg;
1188 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
1189 struct cppc_pcc_data *pcc_ss_data = NULL;
1190 u64 delivered, reference, ref_perf, ctr_wrap_time;
1191 int ret = 0, regs_in_pcc = 0;
1192
1193 if (!cpc_desc) {
1194 pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
1195 return -ENODEV;
1196 }
1197
1198 delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
1199 reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
1200 ref_perf_reg = &cpc_desc->cpc_regs[REFERENCE_PERF];
1201 ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME];
1202
1203 /*
David Brazdil0f672f62019-12-10 10:32:29 +00001204 * If reference perf register is not supported then we should
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001205 * use the nominal perf value
1206 */
1207 if (!CPC_SUPPORTED(ref_perf_reg))
1208 ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
1209
1210 /* Are any of the regs PCC ?*/
1211 if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) ||
1212 CPC_IN_PCC(ctr_wrap_reg) || CPC_IN_PCC(ref_perf_reg)) {
1213 if (pcc_ss_id < 0) {
1214 pr_debug("Invalid pcc_ss_id\n");
1215 return -ENODEV;
1216 }
1217 pcc_ss_data = pcc_data[pcc_ss_id];
1218 down_write(&pcc_ss_data->pcc_lock);
1219 regs_in_pcc = 1;
1220 /* Ring doorbell once to update PCC subspace */
1221 if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
1222 ret = -EIO;
1223 goto out_err;
1224 }
1225 }
1226
1227 cpc_read(cpunum, delivered_reg, &delivered);
1228 cpc_read(cpunum, reference_reg, &reference);
1229 cpc_read(cpunum, ref_perf_reg, &ref_perf);
1230
1231 /*
1232 * Per spec, if ctr_wrap_time optional register is unsupported, then the
1233 * performance counters are assumed to never wrap during the lifetime of
1234 * platform
1235 */
1236 ctr_wrap_time = (u64)(~((u64)0));
1237 if (CPC_SUPPORTED(ctr_wrap_reg))
1238 cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time);
1239
1240 if (!delivered || !reference || !ref_perf) {
1241 ret = -EFAULT;
1242 goto out_err;
1243 }
1244
1245 perf_fb_ctrs->delivered = delivered;
1246 perf_fb_ctrs->reference = reference;
1247 perf_fb_ctrs->reference_perf = ref_perf;
1248 perf_fb_ctrs->wraparound_time = ctr_wrap_time;
1249out_err:
1250 if (regs_in_pcc)
1251 up_write(&pcc_ss_data->pcc_lock);
1252 return ret;
1253}
1254EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
1255
1256/**
David Brazdil0f672f62019-12-10 10:32:29 +00001257 * cppc_set_perf - Set a CPU's performance controls.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001258 * @cpu: CPU for which to set performance controls.
1259 * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
1260 *
1261 * Return: 0 for success, -ERRNO otherwise.
1262 */
1263int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
1264{
1265 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
1266 struct cpc_register_resource *desired_reg;
1267 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
1268 struct cppc_pcc_data *pcc_ss_data = NULL;
1269 int ret = 0;
1270
1271 if (!cpc_desc) {
1272 pr_debug("No CPC descriptor for CPU:%d\n", cpu);
1273 return -ENODEV;
1274 }
1275
1276 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
1277
1278 /*
1279 * This is Phase-I where we want to write to CPC registers
1280 * -> We want all CPUs to be able to execute this phase in parallel
1281 *
1282 * Since read_lock can be acquired by multiple CPUs simultaneously we
1283 * achieve that goal here
1284 */
1285 if (CPC_IN_PCC(desired_reg)) {
1286 if (pcc_ss_id < 0) {
1287 pr_debug("Invalid pcc_ss_id\n");
1288 return -ENODEV;
1289 }
1290 pcc_ss_data = pcc_data[pcc_ss_id];
1291 down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */
1292 if (pcc_ss_data->platform_owns_pcc) {
1293 ret = check_pcc_chan(pcc_ss_id, false);
1294 if (ret) {
1295 up_read(&pcc_ss_data->pcc_lock);
1296 return ret;
1297 }
1298 }
1299 /*
1300 * Update the pending_write to make sure a PCC CMD_READ will not
1301 * arrive and steal the channel during the switch to write lock
1302 */
1303 pcc_ss_data->pending_pcc_write_cmd = true;
1304 cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt;
1305 cpc_desc->write_cmd_status = 0;
1306 }
1307
1308 /*
1309 * Skip writing MIN/MAX until Linux knows how to come up with
1310 * useful values.
1311 */
1312 cpc_write(cpu, desired_reg, perf_ctrls->desired_perf);
1313
1314 if (CPC_IN_PCC(desired_reg))
1315 up_read(&pcc_ss_data->pcc_lock); /* END Phase-I */
1316 /*
1317 * This is Phase-II where we transfer the ownership of PCC to Platform
1318 *
1319 * Short Summary: Basically if we think of a group of cppc_set_perf
1320 * requests that happened in short overlapping interval. The last CPU to
1321 * come out of Phase-I will enter Phase-II and ring the doorbell.
1322 *
1323 * We have the following requirements for Phase-II:
1324 * 1. We want to execute Phase-II only when there are no CPUs
1325 * currently executing in Phase-I
1326 * 2. Once we start Phase-II we want to avoid all other CPUs from
1327 * entering Phase-I.
1328 * 3. We want only one CPU among all those who went through Phase-I
1329 * to run phase-II
1330 *
1331 * If write_trylock fails to get the lock and doesn't transfer the
1332 * PCC ownership to the platform, then one of the following will be TRUE
1333 * 1. There is at-least one CPU in Phase-I which will later execute
1334 * write_trylock, so the CPUs in Phase-I will be responsible for
1335 * executing the Phase-II.
1336 * 2. Some other CPU has beaten this CPU to successfully execute the
1337 * write_trylock and has already acquired the write_lock. We know for a
David Brazdil0f672f62019-12-10 10:32:29 +00001338 * fact it (other CPU acquiring the write_lock) couldn't have happened
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001339 * before this CPU's Phase-I as we held the read_lock.
1340 * 3. Some other CPU executing pcc CMD_READ has stolen the
1341 * down_write, in which case, send_pcc_cmd will check for pending
1342 * CMD_WRITE commands by checking the pending_pcc_write_cmd.
1343 * So this CPU can be certain that its request will be delivered
1344 * So in all cases, this CPU knows that its request will be delivered
1345 * by another CPU and can return
1346 *
1347 * After getting the down_write we still need to check for
1348 * pending_pcc_write_cmd to take care of the following scenario
1349 * The thread running this code could be scheduled out between
1350 * Phase-I and Phase-II. Before it is scheduled back on, another CPU
1351 * could have delivered the request to Platform by triggering the
1352 * doorbell and transferred the ownership of PCC to platform. So this
1353 * avoids triggering an unnecessary doorbell and more importantly before
1354 * triggering the doorbell it makes sure that the PCC channel ownership
1355 * is still with OSPM.
1356 * pending_pcc_write_cmd can also be cleared by a different CPU, if
1357 * there was a pcc CMD_READ waiting on down_write and it steals the lock
1358 * before the pcc CMD_WRITE is completed. pcc_send_cmd checks for this
1359 * case during a CMD_READ and if there are pending writes it delivers
1360 * the write command before servicing the read command
1361 */
1362 if (CPC_IN_PCC(desired_reg)) {
1363 if (down_write_trylock(&pcc_ss_data->pcc_lock)) {/* BEGIN Phase-II */
1364 /* Update only if there are pending write commands */
1365 if (pcc_ss_data->pending_pcc_write_cmd)
1366 send_pcc_cmd(pcc_ss_id, CMD_WRITE);
1367 up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */
1368 } else
1369 /* Wait until pcc_write_cnt is updated by send_pcc_cmd */
1370 wait_event(pcc_ss_data->pcc_write_wait_q,
1371 cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt);
1372
1373 /* send_pcc_cmd updates the status in case of failure */
1374 ret = cpc_desc->write_cmd_status;
1375 }
1376 return ret;
1377}
1378EXPORT_SYMBOL_GPL(cppc_set_perf);
1379
1380/**
1381 * cppc_get_transition_latency - returns frequency transition latency in ns
1382 *
1383 * ACPI CPPC does not explicitly specifiy how a platform can specify the
1384 * transition latency for perfromance change requests. The closest we have
1385 * is the timing information from the PCCT tables which provides the info
1386 * on the number and frequency of PCC commands the platform can handle.
1387 */
1388unsigned int cppc_get_transition_latency(int cpu_num)
1389{
1390 /*
1391 * Expected transition latency is based on the PCCT timing values
1392 * Below are definition from ACPI spec:
1393 * pcc_nominal- Expected latency to process a command, in microseconds
1394 * pcc_mpar - The maximum number of periodic requests that the subspace
1395 * channel can support, reported in commands per minute. 0
1396 * indicates no limitation.
1397 * pcc_mrtt - The minimum amount of time that OSPM must wait after the
1398 * completion of a command before issuing the next command,
1399 * in microseconds.
1400 */
1401 unsigned int latency_ns = 0;
1402 struct cpc_desc *cpc_desc;
1403 struct cpc_register_resource *desired_reg;
1404 int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num);
1405 struct cppc_pcc_data *pcc_ss_data;
1406
1407 cpc_desc = per_cpu(cpc_desc_ptr, cpu_num);
1408 if (!cpc_desc)
1409 return CPUFREQ_ETERNAL;
1410
1411 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
1412 if (!CPC_IN_PCC(desired_reg))
1413 return CPUFREQ_ETERNAL;
1414
1415 if (pcc_ss_id < 0)
1416 return CPUFREQ_ETERNAL;
1417
1418 pcc_ss_data = pcc_data[pcc_ss_id];
1419 if (pcc_ss_data->pcc_mpar)
1420 latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar);
1421
1422 latency_ns = max(latency_ns, pcc_ss_data->pcc_nominal * 1000);
1423 latency_ns = max(latency_ns, pcc_ss_data->pcc_mrtt * 1000);
1424
1425 return latency_ns;
1426}
1427EXPORT_SYMBOL_GPL(cppc_get_transition_latency);