blob: 5623fca54ca1a5b3ec924fc03522d097c9588224 [file] [log] [blame]
Ashwin Chaugule337aadf2015-10-02 10:01:19 -04001/*
2 * CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
3 *
4 * (C) Copyright 2014, 2015 Linaro Ltd.
5 * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
11 *
12 * CPPC describes a few methods for controlling CPU performance using
13 * information from a per CPU table called CPC. This table is described in
14 * the ACPI v5.0+ specification. The table consists of a list of
15 * registers which may be memory mapped or hardware registers and also may
16 * include some static integer values.
17 *
18 * CPU performance is on an abstract continuous scale as against a discretized
19 * P-state scale which is tied to CPU frequency only. In brief, the basic
20 * operation involves:
21 *
22 * - OS makes a CPU performance request. (Can provide min and max bounds)
23 *
24 * - Platform (such as BMC) is free to optimize request within requested bounds
25 * depending on power/thermal budgets etc.
26 *
27 * - Platform conveys its decision back to OS
28 *
29 * The communication between OS and platform occurs through another medium
30 * called (PCC) Platform Communication Channel. This is a generic mailbox like
31 * mechanism which includes doorbell semantics to indicate register updates.
32 * See drivers/mailbox/pcc.c for details on PCC.
33 *
34 * Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
35 * above specifications.
36 */
37
38#define pr_fmt(fmt) "ACPI CPPC: " fmt
39
40#include <linux/cpufreq.h>
41#include <linux/delay.h>
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -070042#include <linux/ktime.h>
Prakash, Prashanth80b82862016-08-16 14:39:40 -060043#include <linux/rwsem.h>
44#include <linux/wait.h>
Ashwin Chaugule337aadf2015-10-02 10:01:19 -040045
46#include <acpi/cppc_acpi.h>
Prakash, Prashanth80b82862016-08-16 14:39:40 -060047
Ashwin Chaugule337aadf2015-10-02 10:01:19 -040048/*
Prakash, Prashanth80b82862016-08-16 14:39:40 -060049 * Lock to provide controlled access to the PCC channel.
50 *
51 * For performance critical usecases(currently cppc_set_perf)
52 * We need to take read_lock and check if channel belongs to OSPM before
53 * reading or writing to PCC subspace
54 * We need to take write_lock before transferring the channel ownership to
55 * the platform via a Doorbell
56 * This allows us to batch a number of CPPC requests if they happen to
57 * originate in about the same time
58 *
59 * For non-performance critical usecases(init)
60 * Take write_lock for all purposes which gives exclusive access
Ashwin Chaugule337aadf2015-10-02 10:01:19 -040061 */
Prakash, Prashanth80b82862016-08-16 14:39:40 -060062static DECLARE_RWSEM(pcc_lock);
63
64/* Indicates if there are any pending/batched PCC write commands */
65static bool pending_pcc_write_cmd;
66
67/* Wait queue for CPUs whose requests were batched */
68static DECLARE_WAIT_QUEUE_HEAD(pcc_write_wait_q);
69
70/* Used to identify if a batched request is delivered to platform */
71static unsigned int pcc_write_cnt;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -040072
73/*
74 * The cpc_desc structure contains the ACPI register details
75 * as described in the per CPU _CPC tables. The details
76 * include the type of register (e.g. PCC, System IO, FFH etc.)
77 * and destination addresses which lets us READ/WRITE CPU performance
78 * information using the appropriate I/O methods.
79 */
80static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
81
82/* This layer handles all the PCC specifics for CPPC. */
83static struct mbox_chan *pcc_channel;
84static void __iomem *pcc_comm_addr;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -040085static int pcc_subspace_idx = -1;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -040086static bool pcc_channel_acquired;
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -070087static ktime_t deadline;
Prakash, Prashanthf387e5b2016-02-17 13:21:03 -070088static unsigned int pcc_mpar, pcc_mrtt;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -040089
Prakash, Prashanth77e3d862016-02-17 13:21:00 -070090/* pcc mapped address + header size + offset within PCC subspace */
91#define GET_PCC_VADDR(offs) (pcc_comm_addr + 0x8 + (offs))
92
Prakash, Prashanth80b82862016-08-16 14:39:40 -060093/* Check if a CPC regsiter is in PCC */
94#define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
95 (cpc)->cpc_entry.reg.space_id == \
96 ACPI_ADR_SPACE_PLATFORM_COMM)
97
Ashwin Chaugule337aadf2015-10-02 10:01:19 -040098/*
99 * Arbitrary Retries in case the remote processor is slow to respond
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700100 * to PCC commands. Keeping it high enough to cover emulators where
101 * the processors run painfully slow.
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400102 */
103#define NUM_RETRIES 500
104
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700105static int check_pcc_chan(void)
106{
107 int ret = -EIO;
108 struct acpi_pcct_shared_memory __iomem *generic_comm_base = pcc_comm_addr;
109 ktime_t next_deadline = ktime_add(ktime_get(), deadline);
110
111 /* Retry in case the remote processor was too slow to catch up. */
112 while (!ktime_after(ktime_get(), next_deadline)) {
Prakash, Prashanthf387e5b2016-02-17 13:21:03 -0700113 /*
114 * Per spec, prior to boot the PCC space wil be initialized by
115 * platform and should have set the command completion bit when
116 * PCC can be used by OSPM
117 */
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700118 if (readw_relaxed(&generic_comm_base->status) & PCC_CMD_COMPLETE) {
119 ret = 0;
120 break;
121 }
122 /*
123 * Reducing the bus traffic in case this loop takes longer than
124 * a few retries.
125 */
126 udelay(3);
127 }
128
129 return ret;
130}
131
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600132/*
133 * This function transfers the ownership of the PCC to the platform
134 * So it must be called while holding write_lock(pcc_lock)
135 */
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400136static int send_pcc_cmd(u16 cmd)
137{
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600138 int ret = -EIO, i;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400139 struct acpi_pcct_shared_memory *generic_comm_base =
140 (struct acpi_pcct_shared_memory *) pcc_comm_addr;
Prakash, Prashanthf387e5b2016-02-17 13:21:03 -0700141 static ktime_t last_cmd_cmpl_time, last_mpar_reset;
142 static int mpar_count;
143 unsigned int time_delta;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400144
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700145 /*
146 * For CMD_WRITE we know for a fact the caller should have checked
147 * the channel before writing to PCC space
148 */
149 if (cmd == CMD_READ) {
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600150 /*
151 * If there are pending cpc_writes, then we stole the channel
152 * before write completion, so first send a WRITE command to
153 * platform
154 */
155 if (pending_pcc_write_cmd)
156 send_pcc_cmd(CMD_WRITE);
157
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700158 ret = check_pcc_chan();
159 if (ret)
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600160 goto end;
161 } else /* CMD_WRITE */
162 pending_pcc_write_cmd = FALSE;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400163
Prakash, Prashanthf387e5b2016-02-17 13:21:03 -0700164 /*
165 * Handle the Minimum Request Turnaround Time(MRTT)
166 * "The minimum amount of time that OSPM must wait after the completion
167 * of a command before issuing the next command, in microseconds"
168 */
169 if (pcc_mrtt) {
170 time_delta = ktime_us_delta(ktime_get(), last_cmd_cmpl_time);
171 if (pcc_mrtt > time_delta)
172 udelay(pcc_mrtt - time_delta);
173 }
174
175 /*
176 * Handle the non-zero Maximum Periodic Access Rate(MPAR)
177 * "The maximum number of periodic requests that the subspace channel can
178 * support, reported in commands per minute. 0 indicates no limitation."
179 *
180 * This parameter should be ideally zero or large enough so that it can
181 * handle maximum number of requests that all the cores in the system can
182 * collectively generate. If it is not, we will follow the spec and just
183 * not send the request to the platform after hitting the MPAR limit in
184 * any 60s window
185 */
186 if (pcc_mpar) {
187 if (mpar_count == 0) {
188 time_delta = ktime_ms_delta(ktime_get(), last_mpar_reset);
189 if (time_delta < 60 * MSEC_PER_SEC) {
190 pr_debug("PCC cmd not sent due to MPAR limit");
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600191 ret = -EIO;
192 goto end;
Prakash, Prashanthf387e5b2016-02-17 13:21:03 -0700193 }
194 last_mpar_reset = ktime_get();
195 mpar_count = pcc_mpar;
196 }
197 mpar_count--;
198 }
199
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400200 /* Write to the shared comm region. */
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700201 writew_relaxed(cmd, &generic_comm_base->command);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400202
203 /* Flip CMD COMPLETE bit */
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700204 writew_relaxed(0, &generic_comm_base->status);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400205
206 /* Ring doorbell */
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700207 ret = mbox_send_message(pcc_channel, &cmd);
208 if (ret < 0) {
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400209 pr_err("Err sending PCC mbox message. cmd:%d, ret:%d\n",
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700210 cmd, ret);
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600211 goto end;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400212 }
213
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700214 /*
215 * For READs we need to ensure the cmd completed to ensure
216 * the ensuing read()s can proceed. For WRITEs we dont care
217 * because the actual write()s are done before coming here
218 * and the next READ or WRITE will check if the channel
219 * is busy/free at the entry of this call.
Prakash, Prashanthf387e5b2016-02-17 13:21:03 -0700220 *
221 * If Minimum Request Turnaround Time is non-zero, we need
222 * to record the completion time of both READ and WRITE
223 * command for proper handling of MRTT, so we need to check
224 * for pcc_mrtt in addition to CMD_READ
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700225 */
Prakash, Prashanthf387e5b2016-02-17 13:21:03 -0700226 if (cmd == CMD_READ || pcc_mrtt) {
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700227 ret = check_pcc_chan();
Prakash, Prashanthf387e5b2016-02-17 13:21:03 -0700228 if (pcc_mrtt)
229 last_cmd_cmpl_time = ktime_get();
230 }
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400231
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700232 mbox_client_txdone(pcc_channel, ret);
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600233
234end:
235 if (cmd == CMD_WRITE) {
236 if (unlikely(ret)) {
237 for_each_possible_cpu(i) {
238 struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i);
239 if (!desc)
240 continue;
241
242 if (desc->write_cmd_id == pcc_write_cnt)
243 desc->write_cmd_status = ret;
244 }
245 }
246 pcc_write_cnt++;
247 wake_up_all(&pcc_write_wait_q);
248 }
249
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700250 return ret;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400251}
252
253static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
254{
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700255 if (ret < 0)
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400256 pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
257 *(u16 *)msg, ret);
258 else
259 pr_debug("TX completed. CMD sent:%x, ret:%d\n",
260 *(u16 *)msg, ret);
261}
262
263struct mbox_client cppc_mbox_cl = {
264 .tx_done = cppc_chan_tx_done,
265 .knows_txdone = true,
266};
267
268static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
269{
270 int result = -EFAULT;
271 acpi_status status = AE_OK;
272 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
273 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
274 struct acpi_buffer state = {0, NULL};
275 union acpi_object *psd = NULL;
276 struct acpi_psd_package *pdomain;
277
278 status = acpi_evaluate_object_typed(handle, "_PSD", NULL, &buffer,
279 ACPI_TYPE_PACKAGE);
280 if (ACPI_FAILURE(status))
281 return -ENODEV;
282
283 psd = buffer.pointer;
284 if (!psd || psd->package.count != 1) {
285 pr_debug("Invalid _PSD data\n");
286 goto end;
287 }
288
289 pdomain = &(cpc_ptr->domain_info);
290
291 state.length = sizeof(struct acpi_psd_package);
292 state.pointer = pdomain;
293
294 status = acpi_extract_package(&(psd->package.elements[0]),
295 &format, &state);
296 if (ACPI_FAILURE(status)) {
297 pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
298 goto end;
299 }
300
301 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
302 pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
303 goto end;
304 }
305
306 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
307 pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
308 goto end;
309 }
310
311 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
312 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
313 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
314 pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
315 goto end;
316 }
317
318 result = 0;
319end:
320 kfree(buffer.pointer);
321 return result;
322}
323
324/**
325 * acpi_get_psd_map - Map the CPUs in a common freq domain.
326 * @all_cpu_data: Ptrs to CPU specific CPPC data including PSD info.
327 *
328 * Return: 0 for success or negative value for err.
329 */
330int acpi_get_psd_map(struct cpudata **all_cpu_data)
331{
332 int count_target;
333 int retval = 0;
334 unsigned int i, j;
335 cpumask_var_t covered_cpus;
336 struct cpudata *pr, *match_pr;
337 struct acpi_psd_package *pdomain;
338 struct acpi_psd_package *match_pdomain;
339 struct cpc_desc *cpc_ptr, *match_cpc_ptr;
340
341 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
342 return -ENOMEM;
343
344 /*
345 * Now that we have _PSD data from all CPUs, lets setup P-state
346 * domain info.
347 */
348 for_each_possible_cpu(i) {
349 pr = all_cpu_data[i];
350 if (!pr)
351 continue;
352
353 if (cpumask_test_cpu(i, covered_cpus))
354 continue;
355
356 cpc_ptr = per_cpu(cpc_desc_ptr, i);
Hoan Tran8343c402016-06-17 15:16:31 -0700357 if (!cpc_ptr) {
358 retval = -EFAULT;
359 goto err_ret;
360 }
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400361
362 pdomain = &(cpc_ptr->domain_info);
363 cpumask_set_cpu(i, pr->shared_cpu_map);
364 cpumask_set_cpu(i, covered_cpus);
365 if (pdomain->num_processors <= 1)
366 continue;
367
368 /* Validate the Domain info */
369 count_target = pdomain->num_processors;
370 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
371 pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
372 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
373 pr->shared_type = CPUFREQ_SHARED_TYPE_HW;
374 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
375 pr->shared_type = CPUFREQ_SHARED_TYPE_ANY;
376
377 for_each_possible_cpu(j) {
378 if (i == j)
379 continue;
380
381 match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
Hoan Tran8343c402016-06-17 15:16:31 -0700382 if (!match_cpc_ptr) {
383 retval = -EFAULT;
384 goto err_ret;
385 }
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400386
387 match_pdomain = &(match_cpc_ptr->domain_info);
388 if (match_pdomain->domain != pdomain->domain)
389 continue;
390
391 /* Here i and j are in the same domain */
392 if (match_pdomain->num_processors != count_target) {
393 retval = -EFAULT;
394 goto err_ret;
395 }
396
397 if (pdomain->coord_type != match_pdomain->coord_type) {
398 retval = -EFAULT;
399 goto err_ret;
400 }
401
402 cpumask_set_cpu(j, covered_cpus);
403 cpumask_set_cpu(j, pr->shared_cpu_map);
404 }
405
406 for_each_possible_cpu(j) {
407 if (i == j)
408 continue;
409
410 match_pr = all_cpu_data[j];
411 if (!match_pr)
412 continue;
413
414 match_cpc_ptr = per_cpu(cpc_desc_ptr, j);
Hoan Tran8343c402016-06-17 15:16:31 -0700415 if (!match_cpc_ptr) {
416 retval = -EFAULT;
417 goto err_ret;
418 }
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400419
420 match_pdomain = &(match_cpc_ptr->domain_info);
421 if (match_pdomain->domain != pdomain->domain)
422 continue;
423
424 match_pr->shared_type = pr->shared_type;
425 cpumask_copy(match_pr->shared_cpu_map,
426 pr->shared_cpu_map);
427 }
428 }
429
430err_ret:
431 for_each_possible_cpu(i) {
432 pr = all_cpu_data[i];
433 if (!pr)
434 continue;
435
436 /* Assume no coordination on any error parsing domain info */
437 if (retval) {
438 cpumask_clear(pr->shared_cpu_map);
439 cpumask_set_cpu(i, pr->shared_cpu_map);
440 pr->shared_type = CPUFREQ_SHARED_TYPE_ALL;
441 }
442 }
443
444 free_cpumask_var(covered_cpus);
445 return retval;
446}
447EXPORT_SYMBOL_GPL(acpi_get_psd_map);
448
Dan Carpenter32c0b2f2015-10-22 22:52:59 +0300449static int register_pcc_channel(int pcc_subspace_idx)
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400450{
Ashwin Chauguled29d6732015-11-12 19:52:30 -0500451 struct acpi_pcct_hw_reduced *cppc_ss;
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700452 u64 usecs_lat;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400453
454 if (pcc_subspace_idx >= 0) {
455 pcc_channel = pcc_mbox_request_channel(&cppc_mbox_cl,
456 pcc_subspace_idx);
457
458 if (IS_ERR(pcc_channel)) {
459 pr_err("Failed to find PCC communication channel\n");
460 return -ENODEV;
461 }
462
463 /*
464 * The PCC mailbox controller driver should
465 * have parsed the PCCT (global table of all
466 * PCC channels) and stored pointers to the
467 * subspace communication region in con_priv.
468 */
469 cppc_ss = pcc_channel->con_priv;
470
471 if (!cppc_ss) {
472 pr_err("No PCC subspace found for CPPC\n");
473 return -ENODEV;
474 }
475
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700476
477 /*
478 * cppc_ss->latency is just a Nominal value. In reality
479 * the remote processor could be much slower to reply.
480 * So add an arbitrary amount of wait on top of Nominal.
481 */
482 usecs_lat = NUM_RETRIES * cppc_ss->latency;
483 deadline = ns_to_ktime(usecs_lat * NSEC_PER_USEC);
Prakash, Prashanthf387e5b2016-02-17 13:21:03 -0700484 pcc_mrtt = cppc_ss->min_turnaround_time;
485 pcc_mpar = cppc_ss->max_access_rate;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400486
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600487 pcc_comm_addr = acpi_os_ioremap(cppc_ss->base_address, cppc_ss->length);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400488 if (!pcc_comm_addr) {
489 pr_err("Failed to ioremap PCC comm region mem\n");
490 return -ENOMEM;
491 }
492
493 /* Set flag so that we dont come here for each CPU. */
494 pcc_channel_acquired = true;
495 }
496
497 return 0;
498}
499
500/*
501 * An example CPC table looks like the following.
502 *
503 * Name(_CPC, Package()
504 * {
505 * 17,
506 * NumEntries
507 * 1,
508 * // Revision
509 * ResourceTemplate(){Register(PCC, 32, 0, 0x120, 2)},
510 * // Highest Performance
511 * ResourceTemplate(){Register(PCC, 32, 0, 0x124, 2)},
512 * // Nominal Performance
513 * ResourceTemplate(){Register(PCC, 32, 0, 0x128, 2)},
514 * // Lowest Nonlinear Performance
515 * ResourceTemplate(){Register(PCC, 32, 0, 0x12C, 2)},
516 * // Lowest Performance
517 * ResourceTemplate(){Register(PCC, 32, 0, 0x130, 2)},
518 * // Guaranteed Performance Register
519 * ResourceTemplate(){Register(PCC, 32, 0, 0x110, 2)},
520 * // Desired Performance Register
521 * ResourceTemplate(){Register(SystemMemory, 0, 0, 0, 0)},
522 * ..
523 * ..
524 * ..
525 *
526 * }
527 * Each Register() encodes how to access that specific register.
528 * e.g. a sample PCC entry has the following encoding:
529 *
530 * Register (
531 * PCC,
532 * AddressSpaceKeyword
533 * 8,
534 * //RegisterBitWidth
535 * 8,
536 * //RegisterBitOffset
537 * 0x30,
538 * //RegisterAddress
539 * 9
540 * //AccessSize (subspace ID)
541 * 0
542 * )
543 * }
544 */
545
546/**
547 * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
548 * @pr: Ptr to acpi_processor containing this CPUs logical Id.
549 *
550 * Return: 0 for success or negative value for err.
551 */
552int acpi_cppc_processor_probe(struct acpi_processor *pr)
553{
554 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
555 union acpi_object *out_obj, *cpc_obj;
556 struct cpc_desc *cpc_ptr;
557 struct cpc_reg *gas_t;
558 acpi_handle handle = pr->handle;
559 unsigned int num_ent, i, cpc_rev;
560 acpi_status status;
561 int ret = -EFAULT;
562
563 /* Parse the ACPI _CPC table for this cpu. */
564 status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
565 ACPI_TYPE_PACKAGE);
566 if (ACPI_FAILURE(status)) {
567 ret = -ENODEV;
568 goto out_buf_free;
569 }
570
571 out_obj = (union acpi_object *) output.pointer;
572
573 cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL);
574 if (!cpc_ptr) {
575 ret = -ENOMEM;
576 goto out_buf_free;
577 }
578
579 /* First entry is NumEntries. */
580 cpc_obj = &out_obj->package.elements[0];
581 if (cpc_obj->type == ACPI_TYPE_INTEGER) {
582 num_ent = cpc_obj->integer.value;
583 } else {
584 pr_debug("Unexpected entry type(%d) for NumEntries\n",
585 cpc_obj->type);
586 goto out_free;
587 }
588
589 /* Only support CPPCv2. Bail otherwise. */
590 if (num_ent != CPPC_NUM_ENT) {
591 pr_debug("Firmware exports %d entries. Expected: %d\n",
592 num_ent, CPPC_NUM_ENT);
593 goto out_free;
594 }
595
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600596 cpc_ptr->num_entries = num_ent;
597
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400598 /* Second entry should be revision. */
599 cpc_obj = &out_obj->package.elements[1];
600 if (cpc_obj->type == ACPI_TYPE_INTEGER) {
601 cpc_rev = cpc_obj->integer.value;
602 } else {
603 pr_debug("Unexpected entry type(%d) for Revision\n",
604 cpc_obj->type);
605 goto out_free;
606 }
607
608 if (cpc_rev != CPPC_REV) {
609 pr_debug("Firmware exports revision:%d. Expected:%d\n",
610 cpc_rev, CPPC_REV);
611 goto out_free;
612 }
613
614 /* Iterate through remaining entries in _CPC */
615 for (i = 2; i < num_ent; i++) {
616 cpc_obj = &out_obj->package.elements[i];
617
618 if (cpc_obj->type == ACPI_TYPE_INTEGER) {
619 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
620 cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
621 } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
622 gas_t = (struct cpc_reg *)
623 cpc_obj->buffer.pointer;
624
625 /*
626 * The PCC Subspace index is encoded inside
627 * the CPC table entries. The same PCC index
628 * will be used for all the PCC entries,
629 * so extract it only once.
630 */
631 if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
632 if (pcc_subspace_idx < 0)
633 pcc_subspace_idx = gas_t->access_width;
634 else if (pcc_subspace_idx != gas_t->access_width) {
635 pr_debug("Mismatched PCC ids.\n");
636 goto out_free;
637 }
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600638 } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
639 if (gas_t->address) {
640 void __iomem *addr;
641
642 addr = ioremap(gas_t->address, gas_t->bit_width/8);
643 if (!addr)
644 goto out_free;
645 cpc_ptr->cpc_regs[i-2].sys_mem_vaddr = addr;
646 }
647 } else {
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400648 /* Support only PCC and SYS MEM type regs */
649 pr_debug("Unsupported register type: %d\n", gas_t->space_id);
650 goto out_free;
651 }
652
653 cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
654 memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
655 } else {
656 pr_debug("Err in entry:%d in CPC table of CPU:%d \n", i, pr->id);
657 goto out_free;
658 }
659 }
660 /* Store CPU Logical ID */
661 cpc_ptr->cpu_id = pr->id;
662
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400663 /* Parse PSD data for this CPU */
664 ret = acpi_get_psd(cpc_ptr, handle);
665 if (ret)
666 goto out_free;
667
668 /* Register PCC channel once for all CPUs. */
669 if (!pcc_channel_acquired) {
670 ret = register_pcc_channel(pcc_subspace_idx);
671 if (ret)
672 goto out_free;
673 }
674
Hoan Tran2324d152016-05-25 12:09:23 -0700675 /* Plug PSD data into this CPUs CPC descriptor. */
676 per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
677
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400678 /* Everything looks okay */
679 pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
680
681 kfree(output.pointer);
682 return 0;
683
684out_free:
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600685 /* Free all the mapped sys mem areas for this CPU */
686 for (i = 2; i < cpc_ptr->num_entries; i++) {
687 void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
688
689 if (addr)
690 iounmap(addr);
691 }
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400692 kfree(cpc_ptr);
693
694out_buf_free:
695 kfree(output.pointer);
696 return ret;
697}
698EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
699
700/**
701 * acpi_cppc_processor_exit - Cleanup CPC structs.
702 * @pr: Ptr to acpi_processor containing this CPUs logical Id.
703 *
704 * Return: Void
705 */
706void acpi_cppc_processor_exit(struct acpi_processor *pr)
707{
708 struct cpc_desc *cpc_ptr;
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600709 unsigned int i;
710 void __iomem *addr;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400711 cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600712
713 /* Free all the mapped sys mem areas for this CPU */
714 for (i = 2; i < cpc_ptr->num_entries; i++) {
715 addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
716 if (addr)
717 iounmap(addr);
718 }
719
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400720 kfree(cpc_ptr);
721}
722EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
723
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700724/*
725 * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
726 * as fast as possible. We have already mapped the PCC subspace during init, so
727 * we can directly write to it.
728 */
729
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600730static int cpc_read(struct cpc_register_resource *reg_res, u64 *val)
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400731{
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700732 int ret_val = 0;
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600733 void __iomem *vaddr = 0;
734 struct cpc_reg *reg = &reg_res->cpc_entry.reg;
735
736 if (reg_res->type == ACPI_TYPE_INTEGER) {
737 *val = reg_res->cpc_entry.int_value;
738 return ret_val;
739 }
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700740
741 *val = 0;
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600742 if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
743 vaddr = GET_PCC_VADDR(reg->address);
744 else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
745 vaddr = reg_res->sys_mem_vaddr;
746 else
747 return acpi_os_read_memory((acpi_physical_address)reg->address,
748 val, reg->bit_width);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700749
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600750 switch (reg->bit_width) {
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700751 case 8:
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700752 *val = readb_relaxed(vaddr);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700753 break;
754 case 16:
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700755 *val = readw_relaxed(vaddr);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700756 break;
757 case 32:
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700758 *val = readl_relaxed(vaddr);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700759 break;
760 case 64:
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700761 *val = readq_relaxed(vaddr);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700762 break;
763 default:
764 pr_debug("Error: Cannot read %u bit width from PCC\n",
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600765 reg->bit_width);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700766 ret_val = -EFAULT;
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600767 }
768
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700769 return ret_val;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400770}
771
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600772static int cpc_write(struct cpc_register_resource *reg_res, u64 val)
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400773{
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700774 int ret_val = 0;
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600775 void __iomem *vaddr = 0;
776 struct cpc_reg *reg = &reg_res->cpc_entry.reg;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400777
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600778 if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
779 vaddr = GET_PCC_VADDR(reg->address);
780 else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
781 vaddr = reg_res->sys_mem_vaddr;
782 else
783 return acpi_os_write_memory((acpi_physical_address)reg->address,
784 val, reg->bit_width);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400785
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600786 switch (reg->bit_width) {
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700787 case 8:
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700788 writeb_relaxed(val, vaddr);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700789 break;
790 case 16:
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700791 writew_relaxed(val, vaddr);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700792 break;
793 case 32:
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700794 writel_relaxed(val, vaddr);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700795 break;
796 case 64:
Prakash, Prashanthbeee23a2016-02-17 13:21:02 -0700797 writeq_relaxed(val, vaddr);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700798 break;
799 default:
800 pr_debug("Error: Cannot write %u bit width to PCC\n",
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600801 reg->bit_width);
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700802 ret_val = -EFAULT;
803 break;
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600804 }
805
Prakash, Prashanth77e3d862016-02-17 13:21:00 -0700806 return ret_val;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400807}
808
809/**
810 * cppc_get_perf_caps - Get a CPUs performance capabilities.
811 * @cpunum: CPU from which to get capabilities info.
812 * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
813 *
814 * Return: 0 for success with perf_caps populated else -ERRNO.
815 */
816int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
817{
818 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
819 struct cpc_register_resource *highest_reg, *lowest_reg, *ref_perf,
820 *nom_perf;
821 u64 high, low, ref, nom;
Prakash, Prashanth850d64a2016-08-16 14:39:39 -0600822 int ret = 0, regs_in_pcc = 0;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400823
824 if (!cpc_desc) {
825 pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
826 return -ENODEV;
827 }
828
829 highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
830 lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
831 ref_perf = &cpc_desc->cpc_regs[REFERENCE_PERF];
832 nom_perf = &cpc_desc->cpc_regs[NOMINAL_PERF];
833
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400834 /* Are any of the regs PCC ?*/
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600835 if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
836 CPC_IN_PCC(ref_perf) || CPC_IN_PCC(nom_perf)) {
Prakash, Prashanth850d64a2016-08-16 14:39:39 -0600837 regs_in_pcc = 1;
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600838 down_write(&pcc_lock);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400839 /* Ring doorbell once to update PCC subspace */
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700840 if (send_pcc_cmd(CMD_READ) < 0) {
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400841 ret = -EIO;
842 goto out_err;
843 }
844 }
845
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600846 cpc_read(highest_reg, &high);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400847 perf_caps->highest_perf = high;
848
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600849 cpc_read(lowest_reg, &low);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400850 perf_caps->lowest_perf = low;
851
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600852 cpc_read(ref_perf, &ref);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400853 perf_caps->reference_perf = ref;
854
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600855 cpc_read(nom_perf, &nom);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400856 perf_caps->nominal_perf = nom;
857
858 if (!ref)
859 perf_caps->reference_perf = perf_caps->nominal_perf;
860
861 if (!high || !low || !nom)
862 ret = -EFAULT;
863
864out_err:
Prakash, Prashanth850d64a2016-08-16 14:39:39 -0600865 if (regs_in_pcc)
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600866 up_write(&pcc_lock);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400867 return ret;
868}
869EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
870
871/**
872 * cppc_get_perf_ctrs - Read a CPUs performance feedback counters.
873 * @cpunum: CPU from which to read counters.
874 * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
875 *
876 * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
877 */
878int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
879{
880 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
881 struct cpc_register_resource *delivered_reg, *reference_reg;
882 u64 delivered, reference;
Prakash, Prashanth850d64a2016-08-16 14:39:39 -0600883 int ret = 0, regs_in_pcc = 0;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400884
885 if (!cpc_desc) {
886 pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
887 return -ENODEV;
888 }
889
890 delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
891 reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
892
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400893 /* Are any of the regs PCC ?*/
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600894 if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg)) {
895 down_write(&pcc_lock);
Prakash, Prashanth850d64a2016-08-16 14:39:39 -0600896 regs_in_pcc = 1;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400897 /* Ring doorbell once to update PCC subspace */
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700898 if (send_pcc_cmd(CMD_READ) < 0) {
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400899 ret = -EIO;
900 goto out_err;
901 }
902 }
903
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600904 cpc_read(delivered_reg, &delivered);
905 cpc_read(reference_reg, &reference);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400906
907 if (!delivered || !reference) {
908 ret = -EFAULT;
909 goto out_err;
910 }
911
912 perf_fb_ctrs->delivered = delivered;
913 perf_fb_ctrs->reference = reference;
914
915 perf_fb_ctrs->delivered -= perf_fb_ctrs->prev_delivered;
916 perf_fb_ctrs->reference -= perf_fb_ctrs->prev_reference;
917
918 perf_fb_ctrs->prev_delivered = delivered;
919 perf_fb_ctrs->prev_reference = reference;
920
921out_err:
Prakash, Prashanth850d64a2016-08-16 14:39:39 -0600922 if (regs_in_pcc)
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600923 up_write(&pcc_lock);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400924 return ret;
925}
926EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
927
928/**
929 * cppc_set_perf - Set a CPUs performance controls.
930 * @cpu: CPU for which to set performance controls.
931 * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
932 *
933 * Return: 0 for success, -ERRNO otherwise.
934 */
935int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
936{
937 struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
938 struct cpc_register_resource *desired_reg;
939 int ret = 0;
940
941 if (!cpc_desc) {
942 pr_debug("No CPC descriptor for CPU:%d\n", cpu);
943 return -ENODEV;
944 }
945
946 desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
947
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600948 /*
949 * This is Phase-I where we want to write to CPC registers
950 * -> We want all CPUs to be able to execute this phase in parallel
951 *
952 * Since read_lock can be acquired by multiple CPUs simultaneously we
953 * achieve that goal here
954 */
955 if (CPC_IN_PCC(desired_reg)) {
956 down_read(&pcc_lock); /* BEGIN Phase-I */
957 /*
958 * If there are pending write commands i.e pending_pcc_write_cmd
959 * is TRUE, then we know OSPM owns the channel as another CPU
960 * has already checked for command completion bit and updated
961 * the corresponding CPC registers
962 */
963 if (!pending_pcc_write_cmd) {
964 ret = check_pcc_chan();
965 if (ret) {
966 up_read(&pcc_lock);
967 return ret;
968 }
969 /*
970 * Update the pending_write to make sure a PCC CMD_READ
971 * will not arrive and steal the channel during the
972 * transition to write lock
973 */
974 pending_pcc_write_cmd = TRUE;
975 }
976 cpc_desc->write_cmd_id = pcc_write_cnt;
977 cpc_desc->write_cmd_status = 0;
Ashwin Chaugulead62e1e62016-02-17 13:20:59 -0700978 }
979
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400980 /*
981 * Skip writing MIN/MAX until Linux knows how to come up with
982 * useful values.
983 */
Ashwin Chaugule5bbb86a2016-08-16 14:39:38 -0600984 cpc_write(desired_reg, perf_ctrls->desired_perf);
Ashwin Chaugule337aadf2015-10-02 10:01:19 -0400985
Prakash, Prashanth80b82862016-08-16 14:39:40 -0600986 if (CPC_IN_PCC(desired_reg))
987 up_read(&pcc_lock); /* END Phase-I */
988 /*
989 * This is Phase-II where we transfer the ownership of PCC to Platform
990 *
991 * Short Summary: Basically if we think of a group of cppc_set_perf
992 * requests that happened in short overlapping interval. The last CPU to
993 * come out of Phase-I will enter Phase-II and ring the doorbell.
994 *
995 * We have the following requirements for Phase-II:
996 * 1. We want to execute Phase-II only when there are no CPUs
997 * currently executing in Phase-I
998 * 2. Once we start Phase-II we want to avoid all other CPUs from
999 * entering Phase-I.
1000 * 3. We want only one CPU among all those who went through Phase-I
1001 * to run phase-II
1002 *
1003 * If write_trylock fails to get the lock and doesn't transfer the
1004 * PCC ownership to the platform, then one of the following will be TRUE
1005 * 1. There is at-least one CPU in Phase-I which will later execute
1006 * write_trylock, so the CPUs in Phase-I will be responsible for
1007 * executing the Phase-II.
1008 * 2. Some other CPU has beaten this CPU to successfully execute the
1009 * write_trylock and has already acquired the write_lock. We know for a
1010 * fact it(other CPU acquiring the write_lock) couldn't have happened
1011 * before this CPU's Phase-I as we held the read_lock.
1012 * 3. Some other CPU executing pcc CMD_READ has stolen the
1013 * down_write, in which case, send_pcc_cmd will check for pending
1014 * CMD_WRITE commands by checking the pending_pcc_write_cmd.
1015 * So this CPU can be certain that its request will be delivered
1016 * So in all cases, this CPU knows that its request will be delivered
1017 * by another CPU and can return
1018 *
1019 * After getting the down_write we still need to check for
1020 * pending_pcc_write_cmd to take care of the following scenario
1021 * The thread running this code could be scheduled out between
1022 * Phase-I and Phase-II. Before it is scheduled back on, another CPU
1023 * could have delivered the request to Platform by triggering the
1024 * doorbell and transferred the ownership of PCC to platform. So this
1025 * avoids triggering an unnecessary doorbell and more importantly before
1026 * triggering the doorbell it makes sure that the PCC channel ownership
1027 * is still with OSPM.
1028 * pending_pcc_write_cmd can also be cleared by a different CPU, if
1029 * there was a pcc CMD_READ waiting on down_write and it steals the lock
1030 * before the pcc CMD_WRITE is completed. pcc_send_cmd checks for this
1031 * case during a CMD_READ and if there are pending writes it delivers
1032 * the write command before servicing the read command
1033 */
1034 if (CPC_IN_PCC(desired_reg)) {
1035 if (down_write_trylock(&pcc_lock)) { /* BEGIN Phase-II */
1036 /* Update only if there are pending write commands */
1037 if (pending_pcc_write_cmd)
1038 send_pcc_cmd(CMD_WRITE);
1039 up_write(&pcc_lock); /* END Phase-II */
1040 } else
1041 /* Wait until pcc_write_cnt is updated by send_pcc_cmd */
1042 wait_event(pcc_write_wait_q,
1043 cpc_desc->write_cmd_id != pcc_write_cnt);
1044
1045 /* send_pcc_cmd updates the status in case of failure */
1046 ret = cpc_desc->write_cmd_status;
Ashwin Chaugule337aadf2015-10-02 10:01:19 -04001047 }
Ashwin Chaugule337aadf2015-10-02 10:01:19 -04001048 return ret;
1049}
1050EXPORT_SYMBOL_GPL(cppc_set_perf);