1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2337aadffSAshwin Chaugule /*
3337aadffSAshwin Chaugule * CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
4337aadffSAshwin Chaugule *
5337aadffSAshwin Chaugule * (C) Copyright 2014, 2015 Linaro Ltd.
6337aadffSAshwin Chaugule * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
7337aadffSAshwin Chaugule *
8337aadffSAshwin Chaugule * CPPC describes a few methods for controlling CPU performance using
9337aadffSAshwin Chaugule * information from a per CPU table called CPC. This table is described in
10337aadffSAshwin Chaugule * the ACPI v5.0+ specification. The table consists of a list of
11337aadffSAshwin Chaugule * registers which may be memory mapped or hardware registers and also may
12337aadffSAshwin Chaugule * include some static integer values.
13337aadffSAshwin Chaugule *
14337aadffSAshwin Chaugule * CPU performance is on an abstract continuous scale as against a discretized
15337aadffSAshwin Chaugule * P-state scale which is tied to CPU frequency only. In brief, the basic
16337aadffSAshwin Chaugule * operation involves:
17337aadffSAshwin Chaugule *
18337aadffSAshwin Chaugule * - OS makes a CPU performance request. (Can provide min and max bounds)
19337aadffSAshwin Chaugule *
20337aadffSAshwin Chaugule * - Platform (such as BMC) is free to optimize request within requested bounds
21337aadffSAshwin Chaugule * depending on power/thermal budgets etc.
22337aadffSAshwin Chaugule *
23337aadffSAshwin Chaugule * - Platform conveys its decision back to OS
24337aadffSAshwin Chaugule *
25337aadffSAshwin Chaugule * The communication between OS and platform occurs through another medium
26337aadffSAshwin Chaugule * called (PCC) Platform Communication Channel. This is a generic mailbox like
27337aadffSAshwin Chaugule * mechanism which includes doorbell semantics to indicate register updates.
28337aadffSAshwin Chaugule * See drivers/mailbox/pcc.c for details on PCC.
29337aadffSAshwin Chaugule *
30337aadffSAshwin Chaugule * Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
31337aadffSAshwin Chaugule * above specifications.
32337aadffSAshwin Chaugule */
33337aadffSAshwin Chaugule
34337aadffSAshwin Chaugule #define pr_fmt(fmt) "ACPI CPPC: " fmt
35337aadffSAshwin Chaugule
36337aadffSAshwin Chaugule #include <linux/delay.h>
3758e1c035SPrakash, Prashanth #include <linux/iopoll.h>
38ad62e1e6SAshwin Chaugule #include <linux/ktime.h>
3980b8286aSPrakash, Prashanth #include <linux/rwsem.h>
4080b8286aSPrakash, Prashanth #include <linux/wait.h>
4141ea6672SNathan Fontenot #include <linux/topology.h>
4233e89c16SVincent Guittot #include <linux/dmi.h>
4333e89c16SVincent Guittot #include <linux/units.h>
4433e89c16SVincent Guittot #include <asm/unaligned.h>
45337aadffSAshwin Chaugule
46337aadffSAshwin Chaugule #include <acpi/cppc_acpi.h>
4780b8286aSPrakash, Prashanth
488482ef8cSPrakash, Prashanth struct cppc_pcc_data {
497b6da7feSSudeep Holla struct pcc_mbox_chan *pcc_channel;
508482ef8cSPrakash, Prashanth void __iomem *pcc_comm_addr;
518482ef8cSPrakash, Prashanth bool pcc_channel_acquired;
5258e1c035SPrakash, Prashanth unsigned int deadline_us;
538482ef8cSPrakash, Prashanth unsigned int pcc_mpar, pcc_mrtt, pcc_nominal;
548482ef8cSPrakash, Prashanth
558482ef8cSPrakash, Prashanth bool pending_pcc_write_cmd; /* Any pending/batched PCC write cmds? */
56139aee73SPrakash, Prashanth bool platform_owns_pcc; /* Ownership of PCC subspace */
578482ef8cSPrakash, Prashanth unsigned int pcc_write_cnt; /* Running count of PCC write commands */
588482ef8cSPrakash, Prashanth
59337aadffSAshwin Chaugule /*
6080b8286aSPrakash, Prashanth * Lock to provide controlled access to the PCC channel.
6180b8286aSPrakash, Prashanth *
6280b8286aSPrakash, Prashanth * For performance critical usecases(currently cppc_set_perf)
638482ef8cSPrakash, Prashanth * We need to take read_lock and check if channel belongs to OSPM
648482ef8cSPrakash, Prashanth * before reading or writing to PCC subspace
658482ef8cSPrakash, Prashanth * We need to take write_lock before transferring the channel
668482ef8cSPrakash, Prashanth * ownership to the platform via a Doorbell
678482ef8cSPrakash, Prashanth * This allows us to batch a number of CPPC requests if they happen
688482ef8cSPrakash, Prashanth * to originate in about the same time
6980b8286aSPrakash, Prashanth *
7080b8286aSPrakash, Prashanth * For non-performance critical usecases(init)
7180b8286aSPrakash, Prashanth * Take write_lock for all purposes which gives exclusive access
72337aadffSAshwin Chaugule */
738482ef8cSPrakash, Prashanth struct rw_semaphore pcc_lock;
7480b8286aSPrakash, Prashanth
7580b8286aSPrakash, Prashanth /* Wait queue for CPUs whose requests were batched */
768482ef8cSPrakash, Prashanth wait_queue_head_t pcc_write_wait_q;
7785b1407bSGeorge Cherian ktime_t last_cmd_cmpl_time;
7885b1407bSGeorge Cherian ktime_t last_mpar_reset;
7985b1407bSGeorge Cherian int mpar_count;
8085b1407bSGeorge Cherian int refcount;
818482ef8cSPrakash, Prashanth };
8280b8286aSPrakash, Prashanth
83603fadf3SBjorn Helgaas /* Array to represent the PCC channel per subspace ID */
8485b1407bSGeorge Cherian static struct cppc_pcc_data *pcc_data[MAX_PCC_SUBSPACES];
85603fadf3SBjorn Helgaas /* The cpu_pcc_subspace_idx contains per CPU subspace ID */
8685b1407bSGeorge Cherian static DEFINE_PER_CPU(int, cpu_pcc_subspace_idx);
87337aadffSAshwin Chaugule
88337aadffSAshwin Chaugule /*
89337aadffSAshwin Chaugule * The cpc_desc structure contains the ACPI register details
90337aadffSAshwin Chaugule * as described in the per CPU _CPC tables. The details
91337aadffSAshwin Chaugule * include the type of register (e.g. PCC, System IO, FFH etc.)
92337aadffSAshwin Chaugule * and destination addresses which lets us READ/WRITE CPU performance
93337aadffSAshwin Chaugule * information using the appropriate I/O methods.
94337aadffSAshwin Chaugule */
95337aadffSAshwin Chaugule static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
96337aadffSAshwin Chaugule
9777e3d86fSPrakash, Prashanth /* pcc mapped address + header size + offset within PCC subspace */
9885b1407bSGeorge Cherian #define GET_PCC_VADDR(offs, pcc_ss_id) (pcc_data[pcc_ss_id]->pcc_comm_addr + \
9985b1407bSGeorge Cherian 0x8 + (offs))
10077e3d86fSPrakash, Prashanth
101ad61dd30SStephen Boyd /* Check if a CPC register is in PCC */
10280b8286aSPrakash, Prashanth #define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
10380b8286aSPrakash, Prashanth (cpc)->cpc_entry.reg.space_id == \
10480b8286aSPrakash, Prashanth ACPI_ADR_SPACE_PLATFORM_COMM)
10580b8286aSPrakash, Prashanth
10629dbea4cSMario Limonciello /* Check if a CPC register is in FFH */
10729dbea4cSMario Limonciello #define CPC_IN_FFH(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
10829dbea4cSMario Limonciello (cpc)->cpc_entry.reg.space_id == \
10929dbea4cSMario Limonciello ACPI_ADR_SPACE_FIXED_HARDWARE)
11029dbea4cSMario Limonciello
1116380b7b2SPierre Gondois /* Check if a CPC register is in SystemMemory */
1126380b7b2SPierre Gondois #define CPC_IN_SYSTEM_MEMORY(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
1136380b7b2SPierre Gondois (cpc)->cpc_entry.reg.space_id == \
1146380b7b2SPierre Gondois ACPI_ADR_SPACE_SYSTEM_MEMORY)
1156380b7b2SPierre Gondois
1166380b7b2SPierre Gondois /* Check if a CPC register is in SystemIo */
1176380b7b2SPierre Gondois #define CPC_IN_SYSTEM_IO(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
1186380b7b2SPierre Gondois (cpc)->cpc_entry.reg.space_id == \
1196380b7b2SPierre Gondois ACPI_ADR_SPACE_SYSTEM_IO)
1206380b7b2SPierre Gondois
121935ab850STom Saeger /* Evaluates to True if reg is a NULL register descriptor */
122158c998eSAshwin Chaugule #define IS_NULL_REG(reg) ((reg)->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY && \
123158c998eSAshwin Chaugule (reg)->address == 0 && \
124158c998eSAshwin Chaugule (reg)->bit_width == 0 && \
125158c998eSAshwin Chaugule (reg)->bit_offset == 0 && \
126158c998eSAshwin Chaugule (reg)->access_width == 0)
127158c998eSAshwin Chaugule
128935ab850STom Saeger /* Evaluates to True if an optional cpc field is supported */
129158c998eSAshwin Chaugule #define CPC_SUPPORTED(cpc) ((cpc)->type == ACPI_TYPE_INTEGER ? \
130158c998eSAshwin Chaugule !!(cpc)->cpc_entry.int_value : \
131158c998eSAshwin Chaugule !IS_NULL_REG(&(cpc)->cpc_entry.reg))
132337aadffSAshwin Chaugule /*
133337aadffSAshwin Chaugule * Arbitrary Retries in case the remote processor is slow to respond
134ad62e1e6SAshwin Chaugule * to PCC commands. Keeping it high enough to cover emulators where
135ad62e1e6SAshwin Chaugule * the processors run painfully slow.
136337aadffSAshwin Chaugule */
137b52f4511SGustavo A. R. Silva #define NUM_RETRIES 500ULL
138337aadffSAshwin Chaugule
139a2c8f92bSSteven Noonan #define OVER_16BTS_MASK ~0xFFFFULL
140a2c8f92bSSteven Noonan
141158c998eSAshwin Chaugule #define define_one_cppc_ro(_name) \
1422bc6262cSNathan Chancellor static struct kobj_attribute _name = \
143158c998eSAshwin Chaugule __ATTR(_name, 0444, show_##_name, NULL)
144158c998eSAshwin Chaugule
145158c998eSAshwin Chaugule #define to_cpc_desc(a) container_of(a, struct cpc_desc, kobj)
146158c998eSAshwin Chaugule
1472c74d847SPrakash, Prashanth #define show_cppc_data(access_fn, struct_name, member_name) \
1482c74d847SPrakash, Prashanth static ssize_t show_##member_name(struct kobject *kobj, \
1492bc6262cSNathan Chancellor struct kobj_attribute *attr, char *buf) \
1502c74d847SPrakash, Prashanth { \
1512c74d847SPrakash, Prashanth struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); \
1522c74d847SPrakash, Prashanth struct struct_name st_name = {0}; \
1532c74d847SPrakash, Prashanth int ret; \
1542c74d847SPrakash, Prashanth \
1552c74d847SPrakash, Prashanth ret = access_fn(cpc_ptr->cpu_id, &st_name); \
1562c74d847SPrakash, Prashanth if (ret) \
1572c74d847SPrakash, Prashanth return ret; \
1582c74d847SPrakash, Prashanth \
15992266c65Sye xingchen return sysfs_emit(buf, "%llu\n", \
1602c74d847SPrakash, Prashanth (u64)st_name.member_name); \
1612c74d847SPrakash, Prashanth } \
1622c74d847SPrakash, Prashanth define_one_cppc_ro(member_name)
1632c74d847SPrakash, Prashanth
1642c74d847SPrakash, Prashanth show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, highest_perf);
1652c74d847SPrakash, Prashanth show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_perf);
1662c74d847SPrakash, Prashanth show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_perf);
1672c74d847SPrakash, Prashanth show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_nonlinear_perf);
1684773e77cSPrashanth Prakash show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_freq);
1694773e77cSPrashanth Prakash show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_freq);
1704773e77cSPrashanth Prakash
1712c74d847SPrakash, Prashanth show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, reference_perf);
1722c74d847SPrakash, Prashanth show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, wraparound_time);
1732c74d847SPrakash, Prashanth
1741b890ae4SJarred White /* Check for valid access_width, otherwise, fallback to using bit_width */
1751b890ae4SJarred White #define GET_BIT_WIDTH(reg) ((reg)->access_width ? (8 << ((reg)->access_width - 1)) : (reg)->bit_width)
1761b890ae4SJarred White
1771b890ae4SJarred White /* Shift and apply the mask for CPC reads/writes */
1788ad28208SClément Léger #define MASK_VAL_READ(reg, val) (((val) >> (reg)->bit_offset) & \
1796a8fda8aSJarred White GENMASK(((reg)->bit_width) - 1, 0))
1808ad28208SClément Léger #define MASK_VAL_WRITE(reg, prev_val, val) \
1818ad28208SClément Léger ((((val) & GENMASK(((reg)->bit_width) - 1, 0)) << (reg)->bit_offset) | \
1828ad28208SClément Léger ((prev_val) & ~(GENMASK(((reg)->bit_width) - 1, 0) << (reg)->bit_offset))) \
1831b890ae4SJarred White
show_feedback_ctrs(struct kobject * kobj,struct kobj_attribute * attr,char * buf)184158c998eSAshwin Chaugule static ssize_t show_feedback_ctrs(struct kobject *kobj,
1852bc6262cSNathan Chancellor struct kobj_attribute *attr, char *buf)
186158c998eSAshwin Chaugule {
187158c998eSAshwin Chaugule struct cpc_desc *cpc_ptr = to_cpc_desc(kobj);
188158c998eSAshwin Chaugule struct cppc_perf_fb_ctrs fb_ctrs = {0};
1892c74d847SPrakash, Prashanth int ret;
190158c998eSAshwin Chaugule
1912c74d847SPrakash, Prashanth ret = cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs);
1922c74d847SPrakash, Prashanth if (ret)
1932c74d847SPrakash, Prashanth return ret;
194158c998eSAshwin Chaugule
19592266c65Sye xingchen return sysfs_emit(buf, "ref:%llu del:%llu\n",
196158c998eSAshwin Chaugule fb_ctrs.reference, fb_ctrs.delivered);
197158c998eSAshwin Chaugule }
198158c998eSAshwin Chaugule define_one_cppc_ro(feedback_ctrs);
199158c998eSAshwin Chaugule
200158c998eSAshwin Chaugule static struct attribute *cppc_attrs[] = {
201158c998eSAshwin Chaugule &feedback_ctrs.attr,
202158c998eSAshwin Chaugule &reference_perf.attr,
203158c998eSAshwin Chaugule &wraparound_time.attr,
2042c74d847SPrakash, Prashanth &highest_perf.attr,
2052c74d847SPrakash, Prashanth &lowest_perf.attr,
2062c74d847SPrakash, Prashanth &lowest_nonlinear_perf.attr,
2072c74d847SPrakash, Prashanth &nominal_perf.attr,
2084773e77cSPrashanth Prakash &nominal_freq.attr,
2094773e77cSPrashanth Prakash &lowest_freq.attr,
210158c998eSAshwin Chaugule NULL
211158c998eSAshwin Chaugule };
21217f18417SGreg Kroah-Hartman ATTRIBUTE_GROUPS(cppc);
213158c998eSAshwin Chaugule
214a527b011SThomas Weißschuh static const struct kobj_type cppc_ktype = {
215158c998eSAshwin Chaugule .sysfs_ops = &kobj_sysfs_ops,
21617f18417SGreg Kroah-Hartman .default_groups = cppc_groups,
217158c998eSAshwin Chaugule };
218158c998eSAshwin Chaugule
check_pcc_chan(int pcc_ss_id,bool chk_err_bit)21985b1407bSGeorge Cherian static int check_pcc_chan(int pcc_ss_id, bool chk_err_bit)
220ad62e1e6SAshwin Chaugule {
22158e1c035SPrakash, Prashanth int ret, status;
22285b1407bSGeorge Cherian struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
22385b1407bSGeorge Cherian struct acpi_pcct_shared_memory __iomem *generic_comm_base =
22485b1407bSGeorge Cherian pcc_ss_data->pcc_comm_addr;
225ad62e1e6SAshwin Chaugule
22685b1407bSGeorge Cherian if (!pcc_ss_data->platform_owns_pcc)
227139aee73SPrakash, Prashanth return 0;
228139aee73SPrakash, Prashanth
229f387e5b9SPrakash, Prashanth /*
23058e1c035SPrakash, Prashanth * Poll PCC status register every 3us(delay_us) for maximum of
23158e1c035SPrakash, Prashanth * deadline_us(timeout_us) until PCC command complete bit is set(cond)
232f387e5b9SPrakash, Prashanth */
23358e1c035SPrakash, Prashanth ret = readw_relaxed_poll_timeout(&generic_comm_base->status, status,
23458e1c035SPrakash, Prashanth status & PCC_CMD_COMPLETE_MASK, 3,
23558e1c035SPrakash, Prashanth pcc_ss_data->deadline_us);
23658e1c035SPrakash, Prashanth
23758e1c035SPrakash, Prashanth if (likely(!ret)) {
23858e1c035SPrakash, Prashanth pcc_ss_data->platform_owns_pcc = false;
239139aee73SPrakash, Prashanth if (chk_err_bit && (status & PCC_ERROR_MASK))
240139aee73SPrakash, Prashanth ret = -EIO;
241ad62e1e6SAshwin Chaugule }
242ad62e1e6SAshwin Chaugule
24358e1c035SPrakash, Prashanth if (unlikely(ret))
24458e1c035SPrakash, Prashanth pr_err("PCC check channel failed for ss: %d. ret=%d\n",
24558e1c035SPrakash, Prashanth pcc_ss_id, ret);
246139aee73SPrakash, Prashanth
247ad62e1e6SAshwin Chaugule return ret;
248ad62e1e6SAshwin Chaugule }
249ad62e1e6SAshwin Chaugule
25080b8286aSPrakash, Prashanth /*
25180b8286aSPrakash, Prashanth * This function transfers the ownership of the PCC to the platform
25280b8286aSPrakash, Prashanth * So it must be called while holding write_lock(pcc_lock)
25380b8286aSPrakash, Prashanth */
send_pcc_cmd(int pcc_ss_id,u16 cmd)25485b1407bSGeorge Cherian static int send_pcc_cmd(int pcc_ss_id, u16 cmd)
255337aadffSAshwin Chaugule {
25680b8286aSPrakash, Prashanth int ret = -EIO, i;
25785b1407bSGeorge Cherian struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
2581d9b4abeSIonela Voinescu struct acpi_pcct_shared_memory __iomem *generic_comm_base =
2591d9b4abeSIonela Voinescu pcc_ss_data->pcc_comm_addr;
260f387e5b9SPrakash, Prashanth unsigned int time_delta;
261337aadffSAshwin Chaugule
262ad62e1e6SAshwin Chaugule /*
263ad62e1e6SAshwin Chaugule * For CMD_WRITE we know for a fact the caller should have checked
264ad62e1e6SAshwin Chaugule * the channel before writing to PCC space
265ad62e1e6SAshwin Chaugule */
266ad62e1e6SAshwin Chaugule if (cmd == CMD_READ) {
26780b8286aSPrakash, Prashanth /*
26880b8286aSPrakash, Prashanth * If there are pending cpc_writes, then we stole the channel
26980b8286aSPrakash, Prashanth * before write completion, so first send a WRITE command to
27080b8286aSPrakash, Prashanth * platform
27180b8286aSPrakash, Prashanth */
27285b1407bSGeorge Cherian if (pcc_ss_data->pending_pcc_write_cmd)
27385b1407bSGeorge Cherian send_pcc_cmd(pcc_ss_id, CMD_WRITE);
27480b8286aSPrakash, Prashanth
27585b1407bSGeorge Cherian ret = check_pcc_chan(pcc_ss_id, false);
276ad62e1e6SAshwin Chaugule if (ret)
27780b8286aSPrakash, Prashanth goto end;
27880b8286aSPrakash, Prashanth } else /* CMD_WRITE */
27985b1407bSGeorge Cherian pcc_ss_data->pending_pcc_write_cmd = FALSE;
280337aadffSAshwin Chaugule
281f387e5b9SPrakash, Prashanth /*
282f387e5b9SPrakash, Prashanth * Handle the Minimum Request Turnaround Time(MRTT)
283f387e5b9SPrakash, Prashanth * "The minimum amount of time that OSPM must wait after the completion
284f387e5b9SPrakash, Prashanth * of a command before issuing the next command, in microseconds"
285f387e5b9SPrakash, Prashanth */
28685b1407bSGeorge Cherian if (pcc_ss_data->pcc_mrtt) {
28785b1407bSGeorge Cherian time_delta = ktime_us_delta(ktime_get(),
28885b1407bSGeorge Cherian pcc_ss_data->last_cmd_cmpl_time);
28985b1407bSGeorge Cherian if (pcc_ss_data->pcc_mrtt > time_delta)
29085b1407bSGeorge Cherian udelay(pcc_ss_data->pcc_mrtt - time_delta);
291f387e5b9SPrakash, Prashanth }
292f387e5b9SPrakash, Prashanth
293f387e5b9SPrakash, Prashanth /*
294f387e5b9SPrakash, Prashanth * Handle the non-zero Maximum Periodic Access Rate(MPAR)
295f387e5b9SPrakash, Prashanth * "The maximum number of periodic requests that the subspace channel can
296f387e5b9SPrakash, Prashanth * support, reported in commands per minute. 0 indicates no limitation."
297f387e5b9SPrakash, Prashanth *
298f387e5b9SPrakash, Prashanth * This parameter should be ideally zero or large enough so that it can
299f387e5b9SPrakash, Prashanth * handle maximum number of requests that all the cores in the system can
300f387e5b9SPrakash, Prashanth * collectively generate. If it is not, we will follow the spec and just
301f387e5b9SPrakash, Prashanth * not send the request to the platform after hitting the MPAR limit in
302f387e5b9SPrakash, Prashanth * any 60s window
303f387e5b9SPrakash, Prashanth */
30485b1407bSGeorge Cherian if (pcc_ss_data->pcc_mpar) {
30585b1407bSGeorge Cherian if (pcc_ss_data->mpar_count == 0) {
30685b1407bSGeorge Cherian time_delta = ktime_ms_delta(ktime_get(),
30785b1407bSGeorge Cherian pcc_ss_data->last_mpar_reset);
30885b1407bSGeorge Cherian if ((time_delta < 60 * MSEC_PER_SEC) && pcc_ss_data->last_mpar_reset) {
309d29abc83SGeorge Cherian pr_debug("PCC cmd for subspace %d not sent due to MPAR limit",
310d29abc83SGeorge Cherian pcc_ss_id);
31180b8286aSPrakash, Prashanth ret = -EIO;
31280b8286aSPrakash, Prashanth goto end;
313f387e5b9SPrakash, Prashanth }
31485b1407bSGeorge Cherian pcc_ss_data->last_mpar_reset = ktime_get();
31585b1407bSGeorge Cherian pcc_ss_data->mpar_count = pcc_ss_data->pcc_mpar;
316f387e5b9SPrakash, Prashanth }
31785b1407bSGeorge Cherian pcc_ss_data->mpar_count--;
318f387e5b9SPrakash, Prashanth }
319f387e5b9SPrakash, Prashanth
320337aadffSAshwin Chaugule /* Write to the shared comm region. */
321beee23aeSPrakash, Prashanth writew_relaxed(cmd, &generic_comm_base->command);
322337aadffSAshwin Chaugule
323337aadffSAshwin Chaugule /* Flip CMD COMPLETE bit */
324beee23aeSPrakash, Prashanth writew_relaxed(0, &generic_comm_base->status);
325337aadffSAshwin Chaugule
32685b1407bSGeorge Cherian pcc_ss_data->platform_owns_pcc = true;
327139aee73SPrakash, Prashanth
328337aadffSAshwin Chaugule /* Ring doorbell */
3297b6da7feSSudeep Holla ret = mbox_send_message(pcc_ss_data->pcc_channel->mchan, &cmd);
330ad62e1e6SAshwin Chaugule if (ret < 0) {
331d29abc83SGeorge Cherian pr_err("Err sending PCC mbox message. ss: %d cmd:%d, ret:%d\n",
332d29abc83SGeorge Cherian pcc_ss_id, cmd, ret);
33380b8286aSPrakash, Prashanth goto end;
334337aadffSAshwin Chaugule }
335337aadffSAshwin Chaugule
3369e12eb82SJulia Lawall /* wait for completion and check for PCC error bit */
33785b1407bSGeorge Cherian ret = check_pcc_chan(pcc_ss_id, true);
338139aee73SPrakash, Prashanth
33985b1407bSGeorge Cherian if (pcc_ss_data->pcc_mrtt)
34085b1407bSGeorge Cherian pcc_ss_data->last_cmd_cmpl_time = ktime_get();
341337aadffSAshwin Chaugule
3427b6da7feSSudeep Holla if (pcc_ss_data->pcc_channel->mchan->mbox->txdone_irq)
3437b6da7feSSudeep Holla mbox_chan_txdone(pcc_ss_data->pcc_channel->mchan, ret);
344b59c4b3dSHoan Tran else
3457b6da7feSSudeep Holla mbox_client_txdone(pcc_ss_data->pcc_channel->mchan, ret);
34680b8286aSPrakash, Prashanth
34780b8286aSPrakash, Prashanth end:
34880b8286aSPrakash, Prashanth if (cmd == CMD_WRITE) {
34980b8286aSPrakash, Prashanth if (unlikely(ret)) {
35080b8286aSPrakash, Prashanth for_each_possible_cpu(i) {
35180b8286aSPrakash, Prashanth struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i);
352e69ae675SXiaofei Tan
35380b8286aSPrakash, Prashanth if (!desc)
35480b8286aSPrakash, Prashanth continue;
35580b8286aSPrakash, Prashanth
35685b1407bSGeorge Cherian if (desc->write_cmd_id == pcc_ss_data->pcc_write_cnt)
35780b8286aSPrakash, Prashanth desc->write_cmd_status = ret;
35880b8286aSPrakash, Prashanth }
35980b8286aSPrakash, Prashanth }
36085b1407bSGeorge Cherian pcc_ss_data->pcc_write_cnt++;
36185b1407bSGeorge Cherian wake_up_all(&pcc_ss_data->pcc_write_wait_q);
36280b8286aSPrakash, Prashanth }
36380b8286aSPrakash, Prashanth
364ad62e1e6SAshwin Chaugule return ret;
365337aadffSAshwin Chaugule }
366337aadffSAshwin Chaugule
cppc_chan_tx_done(struct mbox_client * cl,void * msg,int ret)367337aadffSAshwin Chaugule static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
368337aadffSAshwin Chaugule {
369ad62e1e6SAshwin Chaugule if (ret < 0)
370337aadffSAshwin Chaugule pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
371337aadffSAshwin Chaugule *(u16 *)msg, ret);
372337aadffSAshwin Chaugule else
373337aadffSAshwin Chaugule pr_debug("TX completed. CMD sent:%x, ret:%d\n",
374337aadffSAshwin Chaugule *(u16 *)msg, ret);
375337aadffSAshwin Chaugule }
376337aadffSAshwin Chaugule
3775c447c18SZou Wei static struct mbox_client cppc_mbox_cl = {
378337aadffSAshwin Chaugule .tx_done = cppc_chan_tx_done,
379337aadffSAshwin Chaugule .knows_txdone = true,
380337aadffSAshwin Chaugule };
381337aadffSAshwin Chaugule
acpi_get_psd(struct cpc_desc * cpc_ptr,acpi_handle handle)382337aadffSAshwin Chaugule static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
383337aadffSAshwin Chaugule {
384337aadffSAshwin Chaugule int result = -EFAULT;
385337aadffSAshwin Chaugule acpi_status status = AE_OK;
386337aadffSAshwin Chaugule struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
387337aadffSAshwin Chaugule struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
388337aadffSAshwin Chaugule struct acpi_buffer state = {0, NULL};
389337aadffSAshwin Chaugule union acpi_object *psd = NULL;
390337aadffSAshwin Chaugule struct acpi_psd_package *pdomain;
391337aadffSAshwin Chaugule
3924c4cdc4cSAl Stone status = acpi_evaluate_object_typed(handle, "_PSD", NULL,
3934c4cdc4cSAl Stone &buffer, ACPI_TYPE_PACKAGE);
3944c4cdc4cSAl Stone if (status == AE_NOT_FOUND) /* _PSD is optional */
3954c4cdc4cSAl Stone return 0;
396337aadffSAshwin Chaugule if (ACPI_FAILURE(status))
397337aadffSAshwin Chaugule return -ENODEV;
398337aadffSAshwin Chaugule
399337aadffSAshwin Chaugule psd = buffer.pointer;
400337aadffSAshwin Chaugule if (!psd || psd->package.count != 1) {
401337aadffSAshwin Chaugule pr_debug("Invalid _PSD data\n");
402337aadffSAshwin Chaugule goto end;
403337aadffSAshwin Chaugule }
404337aadffSAshwin Chaugule
405337aadffSAshwin Chaugule pdomain = &(cpc_ptr->domain_info);
406337aadffSAshwin Chaugule
407337aadffSAshwin Chaugule state.length = sizeof(struct acpi_psd_package);
408337aadffSAshwin Chaugule state.pointer = pdomain;
409337aadffSAshwin Chaugule
410337aadffSAshwin Chaugule status = acpi_extract_package(&(psd->package.elements[0]),
411337aadffSAshwin Chaugule &format, &state);
412337aadffSAshwin Chaugule if (ACPI_FAILURE(status)) {
413337aadffSAshwin Chaugule pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
414337aadffSAshwin Chaugule goto end;
415337aadffSAshwin Chaugule }
416337aadffSAshwin Chaugule
417337aadffSAshwin Chaugule if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
418337aadffSAshwin Chaugule pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
419337aadffSAshwin Chaugule goto end;
420337aadffSAshwin Chaugule }
421337aadffSAshwin Chaugule
422337aadffSAshwin Chaugule if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
423337aadffSAshwin Chaugule pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
424337aadffSAshwin Chaugule goto end;
425337aadffSAshwin Chaugule }
426337aadffSAshwin Chaugule
427337aadffSAshwin Chaugule if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
428337aadffSAshwin Chaugule pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
429337aadffSAshwin Chaugule pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
430337aadffSAshwin Chaugule pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
431337aadffSAshwin Chaugule goto end;
432337aadffSAshwin Chaugule }
433337aadffSAshwin Chaugule
434337aadffSAshwin Chaugule result = 0;
435337aadffSAshwin Chaugule end:
436337aadffSAshwin Chaugule kfree(buffer.pointer);
437337aadffSAshwin Chaugule return result;
438337aadffSAshwin Chaugule }
439337aadffSAshwin Chaugule
acpi_cpc_valid(void)440a28b2bfcSIonela Voinescu bool acpi_cpc_valid(void)
441a28b2bfcSIonela Voinescu {
442a28b2bfcSIonela Voinescu struct cpc_desc *cpc_ptr;
443a28b2bfcSIonela Voinescu int cpu;
444a28b2bfcSIonela Voinescu
445a2a9d185SPerry Yuan if (acpi_disabled)
446a2a9d185SPerry Yuan return false;
447a2a9d185SPerry Yuan
4482aeca6bdSMario Limonciello for_each_present_cpu(cpu) {
449a28b2bfcSIonela Voinescu cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
450a28b2bfcSIonela Voinescu if (!cpc_ptr)
451a28b2bfcSIonela Voinescu return false;
452a28b2bfcSIonela Voinescu }
453a28b2bfcSIonela Voinescu
454a28b2bfcSIonela Voinescu return true;
455a28b2bfcSIonela Voinescu }
456a28b2bfcSIonela Voinescu EXPORT_SYMBOL_GPL(acpi_cpc_valid);
457a28b2bfcSIonela Voinescu
cppc_allow_fast_switch(void)4583cc30dd0SPierre Gondois bool cppc_allow_fast_switch(void)
4593cc30dd0SPierre Gondois {
4603cc30dd0SPierre Gondois struct cpc_register_resource *desired_reg;
4613cc30dd0SPierre Gondois struct cpc_desc *cpc_ptr;
4623cc30dd0SPierre Gondois int cpu;
4633cc30dd0SPierre Gondois
4643cc30dd0SPierre Gondois for_each_possible_cpu(cpu) {
4653cc30dd0SPierre Gondois cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
4663cc30dd0SPierre Gondois desired_reg = &cpc_ptr->cpc_regs[DESIRED_PERF];
4673cc30dd0SPierre Gondois if (!CPC_IN_SYSTEM_MEMORY(desired_reg) &&
4683cc30dd0SPierre Gondois !CPC_IN_SYSTEM_IO(desired_reg))
4693cc30dd0SPierre Gondois return false;
4703cc30dd0SPierre Gondois }
4713cc30dd0SPierre Gondois
4723cc30dd0SPierre Gondois return true;
4733cc30dd0SPierre Gondois }
4743cc30dd0SPierre Gondois EXPORT_SYMBOL_GPL(cppc_allow_fast_switch);
4753cc30dd0SPierre Gondois
476337aadffSAshwin Chaugule /**
477a28b2bfcSIonela Voinescu * acpi_get_psd_map - Map the CPUs in the freq domain of a given cpu
478a28b2bfcSIonela Voinescu * @cpu: Find all CPUs that share a domain with cpu.
479a28b2bfcSIonela Voinescu * @cpu_data: Pointer to CPU specific CPPC data including PSD info.
480337aadffSAshwin Chaugule *
481337aadffSAshwin Chaugule * Return: 0 for success or negative value for err.
482337aadffSAshwin Chaugule */
acpi_get_psd_map(unsigned int cpu,struct cppc_cpudata * cpu_data)483a28b2bfcSIonela Voinescu int acpi_get_psd_map(unsigned int cpu, struct cppc_cpudata *cpu_data)
484337aadffSAshwin Chaugule {
485337aadffSAshwin Chaugule struct cpc_desc *cpc_ptr, *match_cpc_ptr;
486a28b2bfcSIonela Voinescu struct acpi_psd_package *match_pdomain;
487a28b2bfcSIonela Voinescu struct acpi_psd_package *pdomain;
488a28b2bfcSIonela Voinescu int count_target, i;
489337aadffSAshwin Chaugule
490337aadffSAshwin Chaugule /*
491603fadf3SBjorn Helgaas * Now that we have _PSD data from all CPUs, let's setup P-state
492337aadffSAshwin Chaugule * domain info.
493337aadffSAshwin Chaugule */
494a28b2bfcSIonela Voinescu cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
495a28b2bfcSIonela Voinescu if (!cpc_ptr)
496a28b2bfcSIonela Voinescu return -EFAULT;
497337aadffSAshwin Chaugule
498337aadffSAshwin Chaugule pdomain = &(cpc_ptr->domain_info);
499a28b2bfcSIonela Voinescu cpumask_set_cpu(cpu, cpu_data->shared_cpu_map);
500337aadffSAshwin Chaugule if (pdomain->num_processors <= 1)
501a28b2bfcSIonela Voinescu return 0;
502337aadffSAshwin Chaugule
503337aadffSAshwin Chaugule /* Validate the Domain info */
504337aadffSAshwin Chaugule count_target = pdomain->num_processors;
505337aadffSAshwin Chaugule if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
506a28b2bfcSIonela Voinescu cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ALL;
507337aadffSAshwin Chaugule else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
508a28b2bfcSIonela Voinescu cpu_data->shared_type = CPUFREQ_SHARED_TYPE_HW;
509337aadffSAshwin Chaugule else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
510a28b2bfcSIonela Voinescu cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ANY;
511337aadffSAshwin Chaugule
512a28b2bfcSIonela Voinescu for_each_possible_cpu(i) {
513a28b2bfcSIonela Voinescu if (i == cpu)
514337aadffSAshwin Chaugule continue;
515337aadffSAshwin Chaugule
516a28b2bfcSIonela Voinescu match_cpc_ptr = per_cpu(cpc_desc_ptr, i);
517a28b2bfcSIonela Voinescu if (!match_cpc_ptr)
518a28b2bfcSIonela Voinescu goto err_fault;
519337aadffSAshwin Chaugule
520337aadffSAshwin Chaugule match_pdomain = &(match_cpc_ptr->domain_info);
521337aadffSAshwin Chaugule if (match_pdomain->domain != pdomain->domain)
522337aadffSAshwin Chaugule continue;
523337aadffSAshwin Chaugule
524a28b2bfcSIonela Voinescu /* Here i and cpu are in the same domain */
525a28b2bfcSIonela Voinescu if (match_pdomain->num_processors != count_target)
526a28b2bfcSIonela Voinescu goto err_fault;
527a28b2bfcSIonela Voinescu
528a28b2bfcSIonela Voinescu if (pdomain->coord_type != match_pdomain->coord_type)
529a28b2bfcSIonela Voinescu goto err_fault;
530a28b2bfcSIonela Voinescu
531a28b2bfcSIonela Voinescu cpumask_set_cpu(i, cpu_data->shared_cpu_map);
532337aadffSAshwin Chaugule }
533337aadffSAshwin Chaugule
534a28b2bfcSIonela Voinescu return 0;
535337aadffSAshwin Chaugule
536a28b2bfcSIonela Voinescu err_fault:
537337aadffSAshwin Chaugule /* Assume no coordination on any error parsing domain info */
538a28b2bfcSIonela Voinescu cpumask_clear(cpu_data->shared_cpu_map);
539a28b2bfcSIonela Voinescu cpumask_set_cpu(cpu, cpu_data->shared_cpu_map);
540a28b2bfcSIonela Voinescu cpu_data->shared_type = CPUFREQ_SHARED_TYPE_NONE;
541a28b2bfcSIonela Voinescu
542a28b2bfcSIonela Voinescu return -EFAULT;
543337aadffSAshwin Chaugule }
544337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(acpi_get_psd_map);
545337aadffSAshwin Chaugule
register_pcc_channel(int pcc_ss_idx)54685b1407bSGeorge Cherian static int register_pcc_channel(int pcc_ss_idx)
547337aadffSAshwin Chaugule {
5487b6da7feSSudeep Holla struct pcc_mbox_chan *pcc_chan;
549ad62e1e6SAshwin Chaugule u64 usecs_lat;
550337aadffSAshwin Chaugule
55185b1407bSGeorge Cherian if (pcc_ss_idx >= 0) {
5527b6da7feSSudeep Holla pcc_chan = pcc_mbox_request_channel(&cppc_mbox_cl, pcc_ss_idx);
553337aadffSAshwin Chaugule
5547b6da7feSSudeep Holla if (IS_ERR(pcc_chan)) {
555d29abc83SGeorge Cherian pr_err("Failed to find PCC channel for subspace %d\n",
556d29abc83SGeorge Cherian pcc_ss_idx);
557337aadffSAshwin Chaugule return -ENODEV;
558337aadffSAshwin Chaugule }
559337aadffSAshwin Chaugule
5607b6da7feSSudeep Holla pcc_data[pcc_ss_idx]->pcc_channel = pcc_chan;
561ad62e1e6SAshwin Chaugule /*
562ad62e1e6SAshwin Chaugule * cppc_ss->latency is just a Nominal value. In reality
563ad62e1e6SAshwin Chaugule * the remote processor could be much slower to reply.
564ad62e1e6SAshwin Chaugule * So add an arbitrary amount of wait on top of Nominal.
565ad62e1e6SAshwin Chaugule */
5667b6da7feSSudeep Holla usecs_lat = NUM_RETRIES * pcc_chan->latency;
56758e1c035SPrakash, Prashanth pcc_data[pcc_ss_idx]->deadline_us = usecs_lat;
5687b6da7feSSudeep Holla pcc_data[pcc_ss_idx]->pcc_mrtt = pcc_chan->min_turnaround_time;
5697b6da7feSSudeep Holla pcc_data[pcc_ss_idx]->pcc_mpar = pcc_chan->max_access_rate;
5707b6da7feSSudeep Holla pcc_data[pcc_ss_idx]->pcc_nominal = pcc_chan->latency;
571337aadffSAshwin Chaugule
57285b1407bSGeorge Cherian pcc_data[pcc_ss_idx]->pcc_comm_addr =
5737b6da7feSSudeep Holla acpi_os_ioremap(pcc_chan->shmem_base_addr,
5747b6da7feSSudeep Holla pcc_chan->shmem_size);
57585b1407bSGeorge Cherian if (!pcc_data[pcc_ss_idx]->pcc_comm_addr) {
576d29abc83SGeorge Cherian pr_err("Failed to ioremap PCC comm region mem for %d\n",
577d29abc83SGeorge Cherian pcc_ss_idx);
578337aadffSAshwin Chaugule return -ENOMEM;
579337aadffSAshwin Chaugule }
580337aadffSAshwin Chaugule
581603fadf3SBjorn Helgaas /* Set flag so that we don't come here for each CPU. */
58285b1407bSGeorge Cherian pcc_data[pcc_ss_idx]->pcc_channel_acquired = true;
583337aadffSAshwin Chaugule }
584337aadffSAshwin Chaugule
585337aadffSAshwin Chaugule return 0;
586337aadffSAshwin Chaugule }
587337aadffSAshwin Chaugule
588a6cbcdd5SSrinivas Pandruvada /**
589a6cbcdd5SSrinivas Pandruvada * cpc_ffh_supported() - check if FFH reading supported
590a6cbcdd5SSrinivas Pandruvada *
591a6cbcdd5SSrinivas Pandruvada * Check if the architecture has support for functional fixed hardware
592a6cbcdd5SSrinivas Pandruvada * read/write capability.
593a6cbcdd5SSrinivas Pandruvada *
594a6cbcdd5SSrinivas Pandruvada * Return: true for supported, false for not supported
595a6cbcdd5SSrinivas Pandruvada */
cpc_ffh_supported(void)596a6cbcdd5SSrinivas Pandruvada bool __weak cpc_ffh_supported(void)
597a6cbcdd5SSrinivas Pandruvada {
598a6cbcdd5SSrinivas Pandruvada return false;
599a6cbcdd5SSrinivas Pandruvada }
600a6cbcdd5SSrinivas Pandruvada
60185b1407bSGeorge Cherian /**
6028b356e53SMario Limonciello * cpc_supported_by_cpu() - check if CPPC is supported by CPU
6038b356e53SMario Limonciello *
6048b356e53SMario Limonciello * Check if the architectural support for CPPC is present even
6058b356e53SMario Limonciello * if the _OSC hasn't prescribed it
6068b356e53SMario Limonciello *
6078b356e53SMario Limonciello * Return: true for supported, false for not supported
6088b356e53SMario Limonciello */
cpc_supported_by_cpu(void)6098b356e53SMario Limonciello bool __weak cpc_supported_by_cpu(void)
6108b356e53SMario Limonciello {
6118b356e53SMario Limonciello return false;
6128b356e53SMario Limonciello }
6138b356e53SMario Limonciello
6148b356e53SMario Limonciello /**
61585b1407bSGeorge Cherian * pcc_data_alloc() - Allocate the pcc_data memory for pcc subspace
616fda7be20SYang Li * @pcc_ss_id: PCC Subspace index as in the PCC client ACPI package.
61785b1407bSGeorge Cherian *
61885b1407bSGeorge Cherian * Check and allocate the cppc_pcc_data memory.
61985b1407bSGeorge Cherian * In some processor configurations it is possible that same subspace
620603fadf3SBjorn Helgaas * is shared between multiple CPUs. This is seen especially in CPUs
62185b1407bSGeorge Cherian * with hardware multi-threading support.
62285b1407bSGeorge Cherian *
62385b1407bSGeorge Cherian * Return: 0 for success, errno for failure
62485b1407bSGeorge Cherian */
pcc_data_alloc(int pcc_ss_id)6255c447c18SZou Wei static int pcc_data_alloc(int pcc_ss_id)
62685b1407bSGeorge Cherian {
62785b1407bSGeorge Cherian if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES)
62885b1407bSGeorge Cherian return -EINVAL;
62985b1407bSGeorge Cherian
63085b1407bSGeorge Cherian if (pcc_data[pcc_ss_id]) {
63185b1407bSGeorge Cherian pcc_data[pcc_ss_id]->refcount++;
63285b1407bSGeorge Cherian } else {
63385b1407bSGeorge Cherian pcc_data[pcc_ss_id] = kzalloc(sizeof(struct cppc_pcc_data),
63485b1407bSGeorge Cherian GFP_KERNEL);
63585b1407bSGeorge Cherian if (!pcc_data[pcc_ss_id])
63685b1407bSGeorge Cherian return -ENOMEM;
63785b1407bSGeorge Cherian pcc_data[pcc_ss_id]->refcount++;
63885b1407bSGeorge Cherian }
63985b1407bSGeorge Cherian
64085b1407bSGeorge Cherian return 0;
64185b1407bSGeorge Cherian }
6424773e77cSPrashanth Prakash
643337aadffSAshwin Chaugule /*
644337aadffSAshwin Chaugule * An example CPC table looks like the following.
645337aadffSAshwin Chaugule *
6461a901c91SAndy Shevchenko * Name (_CPC, Package() {
6471a901c91SAndy Shevchenko * 17, // NumEntries
6481a901c91SAndy Shevchenko * 1, // Revision
6491a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x120, 2)}, // Highest Performance
6501a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x124, 2)}, // Nominal Performance
6511a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x128, 2)}, // Lowest Nonlinear Performance
6521a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x12C, 2)}, // Lowest Performance
6531a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x130, 2)}, // Guaranteed Performance Register
6541a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x110, 2)}, // Desired Performance Register
655337aadffSAshwin Chaugule * ResourceTemplate() {Register(SystemMemory, 0, 0, 0, 0)},
6561a901c91SAndy Shevchenko * ...
6571a901c91SAndy Shevchenko * ...
6581a901c91SAndy Shevchenko * ...
659337aadffSAshwin Chaugule * }
660337aadffSAshwin Chaugule * Each Register() encodes how to access that specific register.
661337aadffSAshwin Chaugule * e.g. a sample PCC entry has the following encoding:
662337aadffSAshwin Chaugule *
663337aadffSAshwin Chaugule * Register (
6641a901c91SAndy Shevchenko * PCC, // AddressSpaceKeyword
6651a901c91SAndy Shevchenko * 8, // RegisterBitWidth
6661a901c91SAndy Shevchenko * 8, // RegisterBitOffset
6671a901c91SAndy Shevchenko * 0x30, // RegisterAddress
6681a901c91SAndy Shevchenko * 9, // AccessSize (subspace ID)
669337aadffSAshwin Chaugule * )
670337aadffSAshwin Chaugule */
671337aadffSAshwin Chaugule
6721132e6deSIonela Voinescu #ifndef arch_init_invariance_cppc
arch_init_invariance_cppc(void)6731132e6deSIonela Voinescu static inline void arch_init_invariance_cppc(void) { }
67441ea6672SNathan Fontenot #endif
67541ea6672SNathan Fontenot
676337aadffSAshwin Chaugule /**
677337aadffSAshwin Chaugule * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
678603fadf3SBjorn Helgaas * @pr: Ptr to acpi_processor containing this CPU's logical ID.
679337aadffSAshwin Chaugule *
680337aadffSAshwin Chaugule * Return: 0 for success or negative value for err.
681337aadffSAshwin Chaugule */
acpi_cppc_processor_probe(struct acpi_processor * pr)682337aadffSAshwin Chaugule int acpi_cppc_processor_probe(struct acpi_processor *pr)
683337aadffSAshwin Chaugule {
684337aadffSAshwin Chaugule struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
685337aadffSAshwin Chaugule union acpi_object *out_obj, *cpc_obj;
686337aadffSAshwin Chaugule struct cpc_desc *cpc_ptr;
687337aadffSAshwin Chaugule struct cpc_reg *gas_t;
688158c998eSAshwin Chaugule struct device *cpu_dev;
689337aadffSAshwin Chaugule acpi_handle handle = pr->handle;
690337aadffSAshwin Chaugule unsigned int num_ent, i, cpc_rev;
69185b1407bSGeorge Cherian int pcc_subspace_id = -1;
692337aadffSAshwin Chaugule acpi_status status;
693f21a3509SRafael J. Wysocki int ret = -ENODATA;
694337aadffSAshwin Chaugule
6957feec743SMario Limonciello if (!osc_sb_cppc2_support_acked) {
6967feec743SMario Limonciello pr_debug("CPPC v2 _OSC not acked\n");
6978b356e53SMario Limonciello if (!cpc_supported_by_cpu())
698c42fa24bSRafael J. Wysocki return -ENODEV;
6997feec743SMario Limonciello }
700c42fa24bSRafael J. Wysocki
701603fadf3SBjorn Helgaas /* Parse the ACPI _CPC table for this CPU. */
702337aadffSAshwin Chaugule status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
703337aadffSAshwin Chaugule ACPI_TYPE_PACKAGE);
704337aadffSAshwin Chaugule if (ACPI_FAILURE(status)) {
705337aadffSAshwin Chaugule ret = -ENODEV;
706337aadffSAshwin Chaugule goto out_buf_free;
707337aadffSAshwin Chaugule }
708337aadffSAshwin Chaugule
709337aadffSAshwin Chaugule out_obj = (union acpi_object *) output.pointer;
710337aadffSAshwin Chaugule
711337aadffSAshwin Chaugule cpc_ptr = kzalloc(sizeof(struct cpc_desc), GFP_KERNEL);
712337aadffSAshwin Chaugule if (!cpc_ptr) {
713337aadffSAshwin Chaugule ret = -ENOMEM;
714337aadffSAshwin Chaugule goto out_buf_free;
715337aadffSAshwin Chaugule }
716337aadffSAshwin Chaugule
717337aadffSAshwin Chaugule /* First entry is NumEntries. */
718337aadffSAshwin Chaugule cpc_obj = &out_obj->package.elements[0];
719337aadffSAshwin Chaugule if (cpc_obj->type == ACPI_TYPE_INTEGER) {
720337aadffSAshwin Chaugule num_ent = cpc_obj->integer.value;
72140d8abf3SRafael J. Wysocki if (num_ent <= 1) {
72240d8abf3SRafael J. Wysocki pr_debug("Unexpected _CPC NumEntries value (%d) for CPU:%d\n",
72340d8abf3SRafael J. Wysocki num_ent, pr->id);
72440d8abf3SRafael J. Wysocki goto out_free;
72540d8abf3SRafael J. Wysocki }
726337aadffSAshwin Chaugule } else {
727f21a3509SRafael J. Wysocki pr_debug("Unexpected _CPC NumEntries entry type (%d) for CPU:%d\n",
728f21a3509SRafael J. Wysocki cpc_obj->type, pr->id);
729337aadffSAshwin Chaugule goto out_free;
730337aadffSAshwin Chaugule }
7315bbb86aaSAshwin Chaugule
732337aadffSAshwin Chaugule /* Second entry should be revision. */
733337aadffSAshwin Chaugule cpc_obj = &out_obj->package.elements[1];
734337aadffSAshwin Chaugule if (cpc_obj->type == ACPI_TYPE_INTEGER) {
735337aadffSAshwin Chaugule cpc_rev = cpc_obj->integer.value;
736337aadffSAshwin Chaugule } else {
737f21a3509SRafael J. Wysocki pr_debug("Unexpected _CPC Revision entry type (%d) for CPU:%d\n",
738f21a3509SRafael J. Wysocki cpc_obj->type, pr->id);
739337aadffSAshwin Chaugule goto out_free;
740337aadffSAshwin Chaugule }
741337aadffSAshwin Chaugule
7424f4179fcSRafael J. Wysocki if (cpc_rev < CPPC_V2_REV) {
7434f4179fcSRafael J. Wysocki pr_debug("Unsupported _CPC Revision (%d) for CPU:%d\n", cpc_rev,
7444f4179fcSRafael J. Wysocki pr->id);
745337aadffSAshwin Chaugule goto out_free;
7464f4179fcSRafael J. Wysocki }
7474f4179fcSRafael J. Wysocki
7484f4179fcSRafael J. Wysocki /*
7494f4179fcSRafael J. Wysocki * Disregard _CPC if the number of entries in the return pachage is not
7504f4179fcSRafael J. Wysocki * as expected, but support future revisions being proper supersets of
7514f4179fcSRafael J. Wysocki * the v3 and only causing more entries to be returned by _CPC.
7524f4179fcSRafael J. Wysocki */
7534f4179fcSRafael J. Wysocki if ((cpc_rev == CPPC_V2_REV && num_ent != CPPC_V2_NUM_ENT) ||
7544f4179fcSRafael J. Wysocki (cpc_rev == CPPC_V3_REV && num_ent != CPPC_V3_NUM_ENT) ||
7554f4179fcSRafael J. Wysocki (cpc_rev > CPPC_V3_REV && num_ent <= CPPC_V3_NUM_ENT)) {
7564f4179fcSRafael J. Wysocki pr_debug("Unexpected number of _CPC return package entries (%d) for CPU:%d\n",
7574f4179fcSRafael J. Wysocki num_ent, pr->id);
7584f4179fcSRafael J. Wysocki goto out_free;
7594f4179fcSRafael J. Wysocki }
7604f4179fcSRafael J. Wysocki if (cpc_rev > CPPC_V3_REV) {
7614f4179fcSRafael J. Wysocki num_ent = CPPC_V3_NUM_ENT;
7624f4179fcSRafael J. Wysocki cpc_rev = CPPC_V3_REV;
7634f4179fcSRafael J. Wysocki }
7644f4179fcSRafael J. Wysocki
7654f4179fcSRafael J. Wysocki cpc_ptr->num_entries = num_ent;
7664f4179fcSRafael J. Wysocki cpc_ptr->version = cpc_rev;
767337aadffSAshwin Chaugule
768337aadffSAshwin Chaugule /* Iterate through remaining entries in _CPC */
769337aadffSAshwin Chaugule for (i = 2; i < num_ent; i++) {
770337aadffSAshwin Chaugule cpc_obj = &out_obj->package.elements[i];
771337aadffSAshwin Chaugule
772337aadffSAshwin Chaugule if (cpc_obj->type == ACPI_TYPE_INTEGER) {
773337aadffSAshwin Chaugule cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
774337aadffSAshwin Chaugule cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = cpc_obj->integer.value;
775337aadffSAshwin Chaugule } else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
776337aadffSAshwin Chaugule gas_t = (struct cpc_reg *)
777337aadffSAshwin Chaugule cpc_obj->buffer.pointer;
778337aadffSAshwin Chaugule
779337aadffSAshwin Chaugule /*
780337aadffSAshwin Chaugule * The PCC Subspace index is encoded inside
781337aadffSAshwin Chaugule * the CPC table entries. The same PCC index
782337aadffSAshwin Chaugule * will be used for all the PCC entries,
783337aadffSAshwin Chaugule * so extract it only once.
784337aadffSAshwin Chaugule */
785337aadffSAshwin Chaugule if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
78685b1407bSGeorge Cherian if (pcc_subspace_id < 0) {
78785b1407bSGeorge Cherian pcc_subspace_id = gas_t->access_width;
78885b1407bSGeorge Cherian if (pcc_data_alloc(pcc_subspace_id))
78985b1407bSGeorge Cherian goto out_free;
79085b1407bSGeorge Cherian } else if (pcc_subspace_id != gas_t->access_width) {
791f21a3509SRafael J. Wysocki pr_debug("Mismatched PCC ids in _CPC for CPU:%d\n",
792f21a3509SRafael J. Wysocki pr->id);
793337aadffSAshwin Chaugule goto out_free;
794337aadffSAshwin Chaugule }
7955bbb86aaSAshwin Chaugule } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
7965bbb86aaSAshwin Chaugule if (gas_t->address) {
7975bbb86aaSAshwin Chaugule void __iomem *addr;
7981b890ae4SJarred White size_t access_width;
7995bbb86aaSAshwin Chaugule
8000651ab90SPierre Gondois if (!osc_cpc_flexible_adr_space_confirmed) {
8010651ab90SPierre Gondois pr_debug("Flexible address space capability not supported\n");
80209073396SMario Limonciello if (!cpc_supported_by_cpu())
8030651ab90SPierre Gondois goto out_free;
8040651ab90SPierre Gondois }
8050651ab90SPierre Gondois
8061b890ae4SJarred White access_width = GET_BIT_WIDTH(gas_t) / 8;
8071b890ae4SJarred White addr = ioremap(gas_t->address, access_width);
8085bbb86aaSAshwin Chaugule if (!addr)
8095bbb86aaSAshwin Chaugule goto out_free;
8105bbb86aaSAshwin Chaugule cpc_ptr->cpc_regs[i-2].sys_mem_vaddr = addr;
8115bbb86aaSAshwin Chaugule }
812a2c8f92bSSteven Noonan } else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
813a2c8f92bSSteven Noonan if (gas_t->access_width < 1 || gas_t->access_width > 3) {
814a2c8f92bSSteven Noonan /*
815a2c8f92bSSteven Noonan * 1 = 8-bit, 2 = 16-bit, and 3 = 32-bit.
816a2c8f92bSSteven Noonan * SystemIO doesn't implement 64-bit
817a2c8f92bSSteven Noonan * registers.
818a2c8f92bSSteven Noonan */
819f21a3509SRafael J. Wysocki pr_debug("Invalid access width %d for SystemIO register in _CPC\n",
820a2c8f92bSSteven Noonan gas_t->access_width);
821a2c8f92bSSteven Noonan goto out_free;
822a2c8f92bSSteven Noonan }
823a2c8f92bSSteven Noonan if (gas_t->address & OVER_16BTS_MASK) {
824a2c8f92bSSteven Noonan /* SystemIO registers use 16-bit integer addresses */
825f21a3509SRafael J. Wysocki pr_debug("Invalid IO port %llu for SystemIO register in _CPC\n",
826a2c8f92bSSteven Noonan gas_t->address);
827a2c8f92bSSteven Noonan goto out_free;
828a2c8f92bSSteven Noonan }
8290651ab90SPierre Gondois if (!osc_cpc_flexible_adr_space_confirmed) {
8300651ab90SPierre Gondois pr_debug("Flexible address space capability not supported\n");
83109073396SMario Limonciello if (!cpc_supported_by_cpu())
8320651ab90SPierre Gondois goto out_free;
8330651ab90SPierre Gondois }
8345bbb86aaSAshwin Chaugule } else {
835a6cbcdd5SSrinivas Pandruvada if (gas_t->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported()) {
836a2c8f92bSSteven Noonan /* Support only PCC, SystemMemory, SystemIO, and FFH type regs. */
837f21a3509SRafael J. Wysocki pr_debug("Unsupported register type (%d) in _CPC\n",
838f21a3509SRafael J. Wysocki gas_t->space_id);
839337aadffSAshwin Chaugule goto out_free;
840337aadffSAshwin Chaugule }
841a6cbcdd5SSrinivas Pandruvada }
842337aadffSAshwin Chaugule
843337aadffSAshwin Chaugule cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_BUFFER;
844337aadffSAshwin Chaugule memcpy(&cpc_ptr->cpc_regs[i-2].cpc_entry.reg, gas_t, sizeof(*gas_t));
845337aadffSAshwin Chaugule } else {
846f21a3509SRafael J. Wysocki pr_debug("Invalid entry type (%d) in _CPC for CPU:%d\n",
847f21a3509SRafael J. Wysocki i, pr->id);
848337aadffSAshwin Chaugule goto out_free;
849337aadffSAshwin Chaugule }
850337aadffSAshwin Chaugule }
85185b1407bSGeorge Cherian per_cpu(cpu_pcc_subspace_idx, pr->id) = pcc_subspace_id;
8524773e77cSPrashanth Prakash
8534773e77cSPrashanth Prakash /*
8544773e77cSPrashanth Prakash * Initialize the remaining cpc_regs as unsupported.
8554773e77cSPrashanth Prakash * Example: In case FW exposes CPPC v2, the below loop will initialize
8564773e77cSPrashanth Prakash * LOWEST_FREQ and NOMINAL_FREQ regs as unsupported
8574773e77cSPrashanth Prakash */
8584773e77cSPrashanth Prakash for (i = num_ent - 2; i < MAX_CPC_REG_ENT; i++) {
8594773e77cSPrashanth Prakash cpc_ptr->cpc_regs[i].type = ACPI_TYPE_INTEGER;
8604773e77cSPrashanth Prakash cpc_ptr->cpc_regs[i].cpc_entry.int_value = 0;
8614773e77cSPrashanth Prakash }
8624773e77cSPrashanth Prakash
8634773e77cSPrashanth Prakash
864337aadffSAshwin Chaugule /* Store CPU Logical ID */
865337aadffSAshwin Chaugule cpc_ptr->cpu_id = pr->id;
866*0eb2b767SPierre Gondois raw_spin_lock_init(&cpc_ptr->rmw_lock);
867337aadffSAshwin Chaugule
868337aadffSAshwin Chaugule /* Parse PSD data for this CPU */
869337aadffSAshwin Chaugule ret = acpi_get_psd(cpc_ptr, handle);
870337aadffSAshwin Chaugule if (ret)
871337aadffSAshwin Chaugule goto out_free;
872337aadffSAshwin Chaugule
873603fadf3SBjorn Helgaas /* Register PCC channel once for all PCC subspace ID. */
87485b1407bSGeorge Cherian if (pcc_subspace_id >= 0 && !pcc_data[pcc_subspace_id]->pcc_channel_acquired) {
87585b1407bSGeorge Cherian ret = register_pcc_channel(pcc_subspace_id);
876337aadffSAshwin Chaugule if (ret)
877337aadffSAshwin Chaugule goto out_free;
8788482ef8cSPrakash, Prashanth
87985b1407bSGeorge Cherian init_rwsem(&pcc_data[pcc_subspace_id]->pcc_lock);
88085b1407bSGeorge Cherian init_waitqueue_head(&pcc_data[pcc_subspace_id]->pcc_write_wait_q);
881337aadffSAshwin Chaugule }
882337aadffSAshwin Chaugule
883337aadffSAshwin Chaugule /* Everything looks okay */
884337aadffSAshwin Chaugule pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
885337aadffSAshwin Chaugule
886158c998eSAshwin Chaugule /* Add per logical CPU nodes for reading its feedback counters. */
887158c998eSAshwin Chaugule cpu_dev = get_cpu_device(pr->id);
88850163475SDan Carpenter if (!cpu_dev) {
88950163475SDan Carpenter ret = -EINVAL;
890158c998eSAshwin Chaugule goto out_free;
89150163475SDan Carpenter }
892158c998eSAshwin Chaugule
893603fadf3SBjorn Helgaas /* Plug PSD data into this CPU's CPC descriptor. */
89428076483SRafael J. Wysocki per_cpu(cpc_desc_ptr, pr->id) = cpc_ptr;
89528076483SRafael J. Wysocki
896158c998eSAshwin Chaugule ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj,
897158c998eSAshwin Chaugule "acpi_cppc");
89828076483SRafael J. Wysocki if (ret) {
89928076483SRafael J. Wysocki per_cpu(cpc_desc_ptr, pr->id) = NULL;
9004d8be4bcSQiushi Wu kobject_put(&cpc_ptr->kobj);
901158c998eSAshwin Chaugule goto out_free;
90228076483SRafael J. Wysocki }
903158c998eSAshwin Chaugule
9041132e6deSIonela Voinescu arch_init_invariance_cppc();
90541ea6672SNathan Fontenot
906337aadffSAshwin Chaugule kfree(output.pointer);
907337aadffSAshwin Chaugule return 0;
908337aadffSAshwin Chaugule
909337aadffSAshwin Chaugule out_free:
9105bbb86aaSAshwin Chaugule /* Free all the mapped sys mem areas for this CPU */
9115bbb86aaSAshwin Chaugule for (i = 2; i < cpc_ptr->num_entries; i++) {
9125bbb86aaSAshwin Chaugule void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
9135bbb86aaSAshwin Chaugule
9145bbb86aaSAshwin Chaugule if (addr)
9155bbb86aaSAshwin Chaugule iounmap(addr);
9165bbb86aaSAshwin Chaugule }
917337aadffSAshwin Chaugule kfree(cpc_ptr);
918337aadffSAshwin Chaugule
919337aadffSAshwin Chaugule out_buf_free:
920337aadffSAshwin Chaugule kfree(output.pointer);
921337aadffSAshwin Chaugule return ret;
922337aadffSAshwin Chaugule }
923337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
924337aadffSAshwin Chaugule
925337aadffSAshwin Chaugule /**
926337aadffSAshwin Chaugule * acpi_cppc_processor_exit - Cleanup CPC structs.
927603fadf3SBjorn Helgaas * @pr: Ptr to acpi_processor containing this CPU's logical ID.
928337aadffSAshwin Chaugule *
929337aadffSAshwin Chaugule * Return: Void
930337aadffSAshwin Chaugule */
acpi_cppc_processor_exit(struct acpi_processor * pr)931337aadffSAshwin Chaugule void acpi_cppc_processor_exit(struct acpi_processor *pr)
932337aadffSAshwin Chaugule {
933337aadffSAshwin Chaugule struct cpc_desc *cpc_ptr;
9345bbb86aaSAshwin Chaugule unsigned int i;
9355bbb86aaSAshwin Chaugule void __iomem *addr;
93685b1407bSGeorge Cherian int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id);
93785b1407bSGeorge Cherian
93885b1407bSGeorge Cherian if (pcc_ss_id >= 0 && pcc_data[pcc_ss_id]) {
93985b1407bSGeorge Cherian if (pcc_data[pcc_ss_id]->pcc_channel_acquired) {
94085b1407bSGeorge Cherian pcc_data[pcc_ss_id]->refcount--;
94185b1407bSGeorge Cherian if (!pcc_data[pcc_ss_id]->refcount) {
94285b1407bSGeorge Cherian pcc_mbox_free_channel(pcc_data[pcc_ss_id]->pcc_channel);
94385b1407bSGeorge Cherian kfree(pcc_data[pcc_ss_id]);
94456a0b978SJohn Garry pcc_data[pcc_ss_id] = NULL;
94585b1407bSGeorge Cherian }
94685b1407bSGeorge Cherian }
94785b1407bSGeorge Cherian }
948158c998eSAshwin Chaugule
949337aadffSAshwin Chaugule cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
9509e9d68daSSebastian Andrzej Siewior if (!cpc_ptr)
9519e9d68daSSebastian Andrzej Siewior return;
9525bbb86aaSAshwin Chaugule
9535bbb86aaSAshwin Chaugule /* Free all the mapped sys mem areas for this CPU */
9545bbb86aaSAshwin Chaugule for (i = 2; i < cpc_ptr->num_entries; i++) {
9555bbb86aaSAshwin Chaugule addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
9565bbb86aaSAshwin Chaugule if (addr)
9575bbb86aaSAshwin Chaugule iounmap(addr);
9585bbb86aaSAshwin Chaugule }
9595bbb86aaSAshwin Chaugule
960158c998eSAshwin Chaugule kobject_put(&cpc_ptr->kobj);
961337aadffSAshwin Chaugule kfree(cpc_ptr);
962337aadffSAshwin Chaugule }
963337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
964337aadffSAshwin Chaugule
965a6cbcdd5SSrinivas Pandruvada /**
966a6cbcdd5SSrinivas Pandruvada * cpc_read_ffh() - Read FFH register
967603fadf3SBjorn Helgaas * @cpunum: CPU number to read
968a6cbcdd5SSrinivas Pandruvada * @reg: cppc register information
969a6cbcdd5SSrinivas Pandruvada * @val: place holder for return value
970a6cbcdd5SSrinivas Pandruvada *
971a6cbcdd5SSrinivas Pandruvada * Read bit_width bits from a specified address and bit_offset
972a6cbcdd5SSrinivas Pandruvada *
973a6cbcdd5SSrinivas Pandruvada * Return: 0 for success and error code
974a6cbcdd5SSrinivas Pandruvada */
cpc_read_ffh(int cpunum,struct cpc_reg * reg,u64 * val)975a6cbcdd5SSrinivas Pandruvada int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
976a6cbcdd5SSrinivas Pandruvada {
977a6cbcdd5SSrinivas Pandruvada return -ENOTSUPP;
978a6cbcdd5SSrinivas Pandruvada }
979a6cbcdd5SSrinivas Pandruvada
980a6cbcdd5SSrinivas Pandruvada /**
981a6cbcdd5SSrinivas Pandruvada * cpc_write_ffh() - Write FFH register
982603fadf3SBjorn Helgaas * @cpunum: CPU number to write
983a6cbcdd5SSrinivas Pandruvada * @reg: cppc register information
984a6cbcdd5SSrinivas Pandruvada * @val: value to write
985a6cbcdd5SSrinivas Pandruvada *
986a6cbcdd5SSrinivas Pandruvada * Write value of bit_width bits to a specified address and bit_offset
987a6cbcdd5SSrinivas Pandruvada *
988a6cbcdd5SSrinivas Pandruvada * Return: 0 for success and error code
989a6cbcdd5SSrinivas Pandruvada */
cpc_write_ffh(int cpunum,struct cpc_reg * reg,u64 val)990a6cbcdd5SSrinivas Pandruvada int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
991a6cbcdd5SSrinivas Pandruvada {
992a6cbcdd5SSrinivas Pandruvada return -ENOTSUPP;
993a6cbcdd5SSrinivas Pandruvada }
994a6cbcdd5SSrinivas Pandruvada
99577e3d86fSPrakash, Prashanth /*
99677e3d86fSPrakash, Prashanth * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
99777e3d86fSPrakash, Prashanth * as fast as possible. We have already mapped the PCC subspace during init, so
99877e3d86fSPrakash, Prashanth * we can directly write to it.
99977e3d86fSPrakash, Prashanth */
100077e3d86fSPrakash, Prashanth
cpc_read(int cpu,struct cpc_register_resource * reg_res,u64 * val)1001a6cbcdd5SSrinivas Pandruvada static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val)
1002337aadffSAshwin Chaugule {
100326692cd9SIonela Voinescu void __iomem *vaddr = NULL;
10041b890ae4SJarred White int size;
100585b1407bSGeorge Cherian int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
10065bbb86aaSAshwin Chaugule struct cpc_reg *reg = ®_res->cpc_entry.reg;
10075bbb86aaSAshwin Chaugule
10085bbb86aaSAshwin Chaugule if (reg_res->type == ACPI_TYPE_INTEGER) {
10095bbb86aaSAshwin Chaugule *val = reg_res->cpc_entry.int_value;
1010f684b107SRafael J. Wysocki return 0;
10115bbb86aaSAshwin Chaugule }
101277e3d86fSPrakash, Prashanth
101377e3d86fSPrakash, Prashanth *val = 0;
10143ecf2249SVanshidhar Konda size = GET_BIT_WIDTH(reg);
1015a2c8f92bSSteven Noonan
1016a2c8f92bSSteven Noonan if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
10175f51c7ceSRafael J. Wysocki u32 val_u32;
1018a2c8f92bSSteven Noonan acpi_status status;
1019a2c8f92bSSteven Noonan
1020a2c8f92bSSteven Noonan status = acpi_os_read_port((acpi_io_address)reg->address,
10213ecf2249SVanshidhar Konda &val_u32, size);
1022a2c8f92bSSteven Noonan if (ACPI_FAILURE(status)) {
1023a2c8f92bSSteven Noonan pr_debug("Error: Failed to read SystemIO port %llx\n",
1024a2c8f92bSSteven Noonan reg->address);
1025a2c8f92bSSteven Noonan return -EFAULT;
1026a2c8f92bSSteven Noonan }
1027a2c8f92bSSteven Noonan
10285f51c7ceSRafael J. Wysocki *val = val_u32;
1029a2c8f92bSSteven Noonan return 0;
10303ecf2249SVanshidhar Konda } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0) {
10313ecf2249SVanshidhar Konda /*
10323ecf2249SVanshidhar Konda * For registers in PCC space, the register size is determined
10333ecf2249SVanshidhar Konda * by the bit width field; the access size is used to indicate
10343ecf2249SVanshidhar Konda * the PCC subspace id.
10353ecf2249SVanshidhar Konda */
10363ecf2249SVanshidhar Konda size = reg->bit_width;
103785b1407bSGeorge Cherian vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
10383ecf2249SVanshidhar Konda }
10395bbb86aaSAshwin Chaugule else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
10405bbb86aaSAshwin Chaugule vaddr = reg_res->sys_mem_vaddr;
1041a6cbcdd5SSrinivas Pandruvada else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
1042a6cbcdd5SSrinivas Pandruvada return cpc_read_ffh(cpu, reg, val);
10435bbb86aaSAshwin Chaugule else
10445bbb86aaSAshwin Chaugule return acpi_os_read_memory((acpi_physical_address)reg->address,
10453ecf2249SVanshidhar Konda val, size);
10461b890ae4SJarred White
10471b890ae4SJarred White switch (size) {
104877e3d86fSPrakash, Prashanth case 8:
1049beee23aeSPrakash, Prashanth *val = readb_relaxed(vaddr);
105077e3d86fSPrakash, Prashanth break;
105177e3d86fSPrakash, Prashanth case 16:
1052beee23aeSPrakash, Prashanth *val = readw_relaxed(vaddr);
105377e3d86fSPrakash, Prashanth break;
105477e3d86fSPrakash, Prashanth case 32:
1055beee23aeSPrakash, Prashanth *val = readl_relaxed(vaddr);
105677e3d86fSPrakash, Prashanth break;
105777e3d86fSPrakash, Prashanth case 64:
1058beee23aeSPrakash, Prashanth *val = readq_relaxed(vaddr);
105977e3d86fSPrakash, Prashanth break;
106077e3d86fSPrakash, Prashanth default:
10613ecf2249SVanshidhar Konda if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
10623ecf2249SVanshidhar Konda pr_debug("Error: Cannot read %u bit width from system memory: 0x%llx\n",
10633ecf2249SVanshidhar Konda size, reg->address);
10643ecf2249SVanshidhar Konda } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
1065d29abc83SGeorge Cherian pr_debug("Error: Cannot read %u bit width from PCC for ss: %d\n",
10663ecf2249SVanshidhar Konda size, pcc_ss_id);
10673ecf2249SVanshidhar Konda }
1068f684b107SRafael J. Wysocki return -EFAULT;
106977e3d86fSPrakash, Prashanth }
10705bbb86aaSAshwin Chaugule
10711b890ae4SJarred White if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
10728ad28208SClément Léger *val = MASK_VAL_READ(reg, *val);
10731b890ae4SJarred White
1074f684b107SRafael J. Wysocki return 0;
1075337aadffSAshwin Chaugule }
1076337aadffSAshwin Chaugule
cpc_write(int cpu,struct cpc_register_resource * reg_res,u64 val)1077a6cbcdd5SSrinivas Pandruvada static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)
1078337aadffSAshwin Chaugule {
107977e3d86fSPrakash, Prashanth int ret_val = 0;
10801b890ae4SJarred White int size;
10818ad28208SClément Léger u64 prev_val;
108226692cd9SIonela Voinescu void __iomem *vaddr = NULL;
108385b1407bSGeorge Cherian int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
10845bbb86aaSAshwin Chaugule struct cpc_reg *reg = ®_res->cpc_entry.reg;
10858ad28208SClément Léger struct cpc_desc *cpc_desc;
1086*0eb2b767SPierre Gondois unsigned long flags;
1087337aadffSAshwin Chaugule
10883ecf2249SVanshidhar Konda size = GET_BIT_WIDTH(reg);
10893ecf2249SVanshidhar Konda
1090a2c8f92bSSteven Noonan if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
1091a2c8f92bSSteven Noonan acpi_status status;
1092a2c8f92bSSteven Noonan
1093a2c8f92bSSteven Noonan status = acpi_os_write_port((acpi_io_address)reg->address,
10943ecf2249SVanshidhar Konda (u32)val, size);
1095a2c8f92bSSteven Noonan if (ACPI_FAILURE(status)) {
1096a2c8f92bSSteven Noonan pr_debug("Error: Failed to write SystemIO port %llx\n",
1097a2c8f92bSSteven Noonan reg->address);
1098a2c8f92bSSteven Noonan return -EFAULT;
1099a2c8f92bSSteven Noonan }
1100a2c8f92bSSteven Noonan
1101a2c8f92bSSteven Noonan return 0;
11023ecf2249SVanshidhar Konda } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0) {
11033ecf2249SVanshidhar Konda /*
11043ecf2249SVanshidhar Konda * For registers in PCC space, the register size is determined
11053ecf2249SVanshidhar Konda * by the bit width field; the access size is used to indicate
11063ecf2249SVanshidhar Konda * the PCC subspace id.
11073ecf2249SVanshidhar Konda */
11083ecf2249SVanshidhar Konda size = reg->bit_width;
110985b1407bSGeorge Cherian vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
11103ecf2249SVanshidhar Konda }
11115bbb86aaSAshwin Chaugule else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
11125bbb86aaSAshwin Chaugule vaddr = reg_res->sys_mem_vaddr;
1113a6cbcdd5SSrinivas Pandruvada else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
1114a6cbcdd5SSrinivas Pandruvada return cpc_write_ffh(cpu, reg, val);
11155bbb86aaSAshwin Chaugule else
11165bbb86aaSAshwin Chaugule return acpi_os_write_memory((acpi_physical_address)reg->address,
11173ecf2249SVanshidhar Konda val, size);
11181b890ae4SJarred White
11198ad28208SClément Léger if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
11208ad28208SClément Léger cpc_desc = per_cpu(cpc_desc_ptr, cpu);
11218ad28208SClément Léger if (!cpc_desc) {
11228ad28208SClément Léger pr_debug("No CPC descriptor for CPU:%d\n", cpu);
11238ad28208SClément Léger return -ENODEV;
11248ad28208SClément Léger }
11258ad28208SClément Léger
1126*0eb2b767SPierre Gondois raw_spin_lock_irqsave(&cpc_desc->rmw_lock, flags);
11278ad28208SClément Léger switch (size) {
11288ad28208SClément Léger case 8:
11298ad28208SClément Léger prev_val = readb_relaxed(vaddr);
11308ad28208SClément Léger break;
11318ad28208SClément Léger case 16:
11328ad28208SClément Léger prev_val = readw_relaxed(vaddr);
11338ad28208SClément Léger break;
11348ad28208SClément Léger case 32:
11358ad28208SClément Léger prev_val = readl_relaxed(vaddr);
11368ad28208SClément Léger break;
11378ad28208SClément Léger case 64:
11388ad28208SClément Léger prev_val = readq_relaxed(vaddr);
11398ad28208SClément Léger break;
11408ad28208SClément Léger default:
1141*0eb2b767SPierre Gondois raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, flags);
11428ad28208SClément Léger return -EFAULT;
11438ad28208SClément Léger }
11448ad28208SClément Léger val = MASK_VAL_WRITE(reg, prev_val, val);
11458ad28208SClément Léger }
11461b890ae4SJarred White
11471b890ae4SJarred White switch (size) {
114877e3d86fSPrakash, Prashanth case 8:
1149beee23aeSPrakash, Prashanth writeb_relaxed(val, vaddr);
115077e3d86fSPrakash, Prashanth break;
115177e3d86fSPrakash, Prashanth case 16:
1152beee23aeSPrakash, Prashanth writew_relaxed(val, vaddr);
115377e3d86fSPrakash, Prashanth break;
115477e3d86fSPrakash, Prashanth case 32:
1155beee23aeSPrakash, Prashanth writel_relaxed(val, vaddr);
115677e3d86fSPrakash, Prashanth break;
115777e3d86fSPrakash, Prashanth case 64:
1158beee23aeSPrakash, Prashanth writeq_relaxed(val, vaddr);
115977e3d86fSPrakash, Prashanth break;
116077e3d86fSPrakash, Prashanth default:
11613ecf2249SVanshidhar Konda if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
11623ecf2249SVanshidhar Konda pr_debug("Error: Cannot write %u bit width to system memory: 0x%llx\n",
11633ecf2249SVanshidhar Konda size, reg->address);
11643ecf2249SVanshidhar Konda } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
1165d29abc83SGeorge Cherian pr_debug("Error: Cannot write %u bit width to PCC for ss: %d\n",
11663ecf2249SVanshidhar Konda size, pcc_ss_id);
11673ecf2249SVanshidhar Konda }
116877e3d86fSPrakash, Prashanth ret_val = -EFAULT;
116977e3d86fSPrakash, Prashanth break;
1170337aadffSAshwin Chaugule }
11715bbb86aaSAshwin Chaugule
11728ad28208SClément Léger if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1173*0eb2b767SPierre Gondois raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, flags);
11748ad28208SClément Léger
117577e3d86fSPrakash, Prashanth return ret_val;
1176337aadffSAshwin Chaugule }
1177337aadffSAshwin Chaugule
cppc_get_perf(int cpunum,enum cppc_regs reg_idx,u64 * perf)11780654cf05SRafael J. Wysocki static int cppc_get_perf(int cpunum, enum cppc_regs reg_idx, u64 *perf)
11791757d05fSXiongfeng Wang {
11801757d05fSXiongfeng Wang struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
1181935dff30SRafael J. Wysocki struct cpc_register_resource *reg;
1182935dff30SRafael J. Wysocki
1183935dff30SRafael J. Wysocki if (!cpc_desc) {
1184935dff30SRafael J. Wysocki pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
1185935dff30SRafael J. Wysocki return -ENODEV;
1186935dff30SRafael J. Wysocki }
1187935dff30SRafael J. Wysocki
1188935dff30SRafael J. Wysocki reg = &cpc_desc->cpc_regs[reg_idx];
11890654cf05SRafael J. Wysocki
11900654cf05SRafael J. Wysocki if (CPC_IN_PCC(reg)) {
11911757d05fSXiongfeng Wang int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
11921757d05fSXiongfeng Wang struct cppc_pcc_data *pcc_ss_data = NULL;
11931757d05fSXiongfeng Wang int ret = 0;
11941757d05fSXiongfeng Wang
11951757d05fSXiongfeng Wang if (pcc_ss_id < 0)
11961757d05fSXiongfeng Wang return -EIO;
11971757d05fSXiongfeng Wang
11981757d05fSXiongfeng Wang pcc_ss_data = pcc_data[pcc_ss_id];
11991757d05fSXiongfeng Wang
12001757d05fSXiongfeng Wang down_write(&pcc_ss_data->pcc_lock);
12011757d05fSXiongfeng Wang
12021757d05fSXiongfeng Wang if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0)
12030654cf05SRafael J. Wysocki cpc_read(cpunum, reg, perf);
12041757d05fSXiongfeng Wang else
12051757d05fSXiongfeng Wang ret = -EIO;
12061757d05fSXiongfeng Wang
12071757d05fSXiongfeng Wang up_write(&pcc_ss_data->pcc_lock);
12081757d05fSXiongfeng Wang
12091757d05fSXiongfeng Wang return ret;
12101757d05fSXiongfeng Wang }
12111757d05fSXiongfeng Wang
12120654cf05SRafael J. Wysocki cpc_read(cpunum, reg, perf);
12131757d05fSXiongfeng Wang
12141757d05fSXiongfeng Wang return 0;
12151757d05fSXiongfeng Wang }
12160654cf05SRafael J. Wysocki
12170654cf05SRafael J. Wysocki /**
12180654cf05SRafael J. Wysocki * cppc_get_desired_perf - Get the desired performance register value.
12190654cf05SRafael J. Wysocki * @cpunum: CPU from which to get desired performance.
12200654cf05SRafael J. Wysocki * @desired_perf: Return address.
12210654cf05SRafael J. Wysocki *
12220654cf05SRafael J. Wysocki * Return: 0 for success, -EIO otherwise.
12230654cf05SRafael J. Wysocki */
cppc_get_desired_perf(int cpunum,u64 * desired_perf)12240654cf05SRafael J. Wysocki int cppc_get_desired_perf(int cpunum, u64 *desired_perf)
12250654cf05SRafael J. Wysocki {
12260654cf05SRafael J. Wysocki return cppc_get_perf(cpunum, DESIRED_PERF, desired_perf);
12270654cf05SRafael J. Wysocki }
12281757d05fSXiongfeng Wang EXPORT_SYMBOL_GPL(cppc_get_desired_perf);
12291757d05fSXiongfeng Wang
12301757d05fSXiongfeng Wang /**
12310654cf05SRafael J. Wysocki * cppc_get_nominal_perf - Get the nominal performance register value.
12320654cf05SRafael J. Wysocki * @cpunum: CPU from which to get nominal performance.
12330654cf05SRafael J. Wysocki * @nominal_perf: Return address.
12340654cf05SRafael J. Wysocki *
12350654cf05SRafael J. Wysocki * Return: 0 for success, -EIO otherwise.
12360654cf05SRafael J. Wysocki */
cppc_get_nominal_perf(int cpunum,u64 * nominal_perf)12370654cf05SRafael J. Wysocki int cppc_get_nominal_perf(int cpunum, u64 *nominal_perf)
12380654cf05SRafael J. Wysocki {
12390654cf05SRafael J. Wysocki return cppc_get_perf(cpunum, NOMINAL_PERF, nominal_perf);
12400654cf05SRafael J. Wysocki }
12410654cf05SRafael J. Wysocki
12420654cf05SRafael J. Wysocki /**
12430b983c08SMeng Li * cppc_get_highest_perf - Get the highest performance register value.
12440b983c08SMeng Li * @cpunum: CPU from which to get highest performance.
12450b983c08SMeng Li * @highest_perf: Return address.
12460b983c08SMeng Li *
12470b983c08SMeng Li * Return: 0 for success, -EIO otherwise.
12480b983c08SMeng Li */
cppc_get_highest_perf(int cpunum,u64 * highest_perf)12490b983c08SMeng Li int cppc_get_highest_perf(int cpunum, u64 *highest_perf)
12500b983c08SMeng Li {
12510b983c08SMeng Li return cppc_get_perf(cpunum, HIGHEST_PERF, highest_perf);
12520b983c08SMeng Li }
12530b983c08SMeng Li EXPORT_SYMBOL_GPL(cppc_get_highest_perf);
12540b983c08SMeng Li
12550b983c08SMeng Li /**
12567bc1fcd3SPerry Yuan * cppc_get_epp_perf - Get the epp register value.
12577bc1fcd3SPerry Yuan * @cpunum: CPU from which to get epp preference value.
12587bc1fcd3SPerry Yuan * @epp_perf: Return address.
12597bc1fcd3SPerry Yuan *
12607bc1fcd3SPerry Yuan * Return: 0 for success, -EIO otherwise.
12617bc1fcd3SPerry Yuan */
cppc_get_epp_perf(int cpunum,u64 * epp_perf)12627bc1fcd3SPerry Yuan int cppc_get_epp_perf(int cpunum, u64 *epp_perf)
12637bc1fcd3SPerry Yuan {
12647bc1fcd3SPerry Yuan return cppc_get_perf(cpunum, ENERGY_PERF, epp_perf);
12657bc1fcd3SPerry Yuan }
12667bc1fcd3SPerry Yuan EXPORT_SYMBOL_GPL(cppc_get_epp_perf);
12677bc1fcd3SPerry Yuan
12687bc1fcd3SPerry Yuan /**
1269603fadf3SBjorn Helgaas * cppc_get_perf_caps - Get a CPU's performance capabilities.
1270337aadffSAshwin Chaugule * @cpunum: CPU from which to get capabilities info.
1271337aadffSAshwin Chaugule * @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
1272337aadffSAshwin Chaugule *
1273337aadffSAshwin Chaugule * Return: 0 for success with perf_caps populated else -ERRNO.
1274337aadffSAshwin Chaugule */
cppc_get_perf_caps(int cpunum,struct cppc_perf_caps * perf_caps)1275337aadffSAshwin Chaugule int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
1276337aadffSAshwin Chaugule {
1277337aadffSAshwin Chaugule struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
1278368520a6SPrakash, Prashanth struct cpc_register_resource *highest_reg, *lowest_reg,
127929523f09SSrinivas Pandruvada *lowest_non_linear_reg, *nominal_reg, *guaranteed_reg,
12804773e77cSPrashanth Prakash *low_freq_reg = NULL, *nom_freq_reg = NULL;
128129523f09SSrinivas Pandruvada u64 high, low, guaranteed, nom, min_nonlinear, low_f = 0, nom_f = 0;
128285b1407bSGeorge Cherian int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
12836fa12d58SPrashanth Prakash struct cppc_pcc_data *pcc_ss_data = NULL;
1284850d64a4SPrakash, Prashanth int ret = 0, regs_in_pcc = 0;
1285337aadffSAshwin Chaugule
12866fa12d58SPrashanth Prakash if (!cpc_desc) {
1287337aadffSAshwin Chaugule pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
1288337aadffSAshwin Chaugule return -ENODEV;
1289337aadffSAshwin Chaugule }
1290337aadffSAshwin Chaugule
1291337aadffSAshwin Chaugule highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
1292337aadffSAshwin Chaugule lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
1293368520a6SPrakash, Prashanth lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF];
1294368520a6SPrakash, Prashanth nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
12954773e77cSPrashanth Prakash low_freq_reg = &cpc_desc->cpc_regs[LOWEST_FREQ];
12964773e77cSPrashanth Prakash nom_freq_reg = &cpc_desc->cpc_regs[NOMINAL_FREQ];
129729523f09SSrinivas Pandruvada guaranteed_reg = &cpc_desc->cpc_regs[GUARANTEED_PERF];
1298337aadffSAshwin Chaugule
1299337aadffSAshwin Chaugule /* Are any of the regs PCC ?*/
130080b8286aSPrakash, Prashanth if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
13014773e77cSPrashanth Prakash CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg) ||
13024773e77cSPrashanth Prakash CPC_IN_PCC(low_freq_reg) || CPC_IN_PCC(nom_freq_reg)) {
13036fa12d58SPrashanth Prakash if (pcc_ss_id < 0) {
13046fa12d58SPrashanth Prakash pr_debug("Invalid pcc_ss_id\n");
13056fa12d58SPrashanth Prakash return -ENODEV;
13066fa12d58SPrashanth Prakash }
13076fa12d58SPrashanth Prakash pcc_ss_data = pcc_data[pcc_ss_id];
1308850d64a4SPrakash, Prashanth regs_in_pcc = 1;
130985b1407bSGeorge Cherian down_write(&pcc_ss_data->pcc_lock);
1310337aadffSAshwin Chaugule /* Ring doorbell once to update PCC subspace */
131185b1407bSGeorge Cherian if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
1312337aadffSAshwin Chaugule ret = -EIO;
1313337aadffSAshwin Chaugule goto out_err;
1314337aadffSAshwin Chaugule }
1315337aadffSAshwin Chaugule }
1316337aadffSAshwin Chaugule
1317a6cbcdd5SSrinivas Pandruvada cpc_read(cpunum, highest_reg, &high);
1318337aadffSAshwin Chaugule perf_caps->highest_perf = high;
1319337aadffSAshwin Chaugule
1320a6cbcdd5SSrinivas Pandruvada cpc_read(cpunum, lowest_reg, &low);
1321337aadffSAshwin Chaugule perf_caps->lowest_perf = low;
1322337aadffSAshwin Chaugule
1323368520a6SPrakash, Prashanth cpc_read(cpunum, nominal_reg, &nom);
1324337aadffSAshwin Chaugule perf_caps->nominal_perf = nom;
1325337aadffSAshwin Chaugule
1326edef1ef1SSrinivas Pandruvada if (guaranteed_reg->type != ACPI_TYPE_BUFFER ||
1327edef1ef1SSrinivas Pandruvada IS_NULL_REG(&guaranteed_reg->cpc_entry.reg)) {
1328edef1ef1SSrinivas Pandruvada perf_caps->guaranteed_perf = 0;
1329edef1ef1SSrinivas Pandruvada } else {
133029523f09SSrinivas Pandruvada cpc_read(cpunum, guaranteed_reg, &guaranteed);
133129523f09SSrinivas Pandruvada perf_caps->guaranteed_perf = guaranteed;
1332edef1ef1SSrinivas Pandruvada }
133329523f09SSrinivas Pandruvada
1334368520a6SPrakash, Prashanth cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear);
1335368520a6SPrakash, Prashanth perf_caps->lowest_nonlinear_perf = min_nonlinear;
1336368520a6SPrakash, Prashanth
1337368520a6SPrakash, Prashanth if (!high || !low || !nom || !min_nonlinear)
1338337aadffSAshwin Chaugule ret = -EFAULT;
1339337aadffSAshwin Chaugule
13404773e77cSPrashanth Prakash /* Read optional lowest and nominal frequencies if present */
13414773e77cSPrashanth Prakash if (CPC_SUPPORTED(low_freq_reg))
13424773e77cSPrashanth Prakash cpc_read(cpunum, low_freq_reg, &low_f);
13434773e77cSPrashanth Prakash
13444773e77cSPrashanth Prakash if (CPC_SUPPORTED(nom_freq_reg))
13454773e77cSPrashanth Prakash cpc_read(cpunum, nom_freq_reg, &nom_f);
13464773e77cSPrashanth Prakash
13474773e77cSPrashanth Prakash perf_caps->lowest_freq = low_f;
13484773e77cSPrashanth Prakash perf_caps->nominal_freq = nom_f;
13494773e77cSPrashanth Prakash
13504773e77cSPrashanth Prakash
1351337aadffSAshwin Chaugule out_err:
1352850d64a4SPrakash, Prashanth if (regs_in_pcc)
135385b1407bSGeorge Cherian up_write(&pcc_ss_data->pcc_lock);
1354337aadffSAshwin Chaugule return ret;
1355337aadffSAshwin Chaugule }
1356337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
1357337aadffSAshwin Chaugule
1358337aadffSAshwin Chaugule /**
1359ae2df912SJeremy Linton * cppc_perf_ctrs_in_pcc - Check if any perf counters are in a PCC region.
1360ae2df912SJeremy Linton *
1361ae2df912SJeremy Linton * CPPC has flexibility about how CPU performance counters are accessed.
1362ae2df912SJeremy Linton * One of the choices is PCC regions, which can have a high access latency. This
1363ae2df912SJeremy Linton * routine allows callers of cppc_get_perf_ctrs() to know this ahead of time.
1364ae2df912SJeremy Linton *
1365ae2df912SJeremy Linton * Return: true if any of the counters are in PCC regions, false otherwise
1366ae2df912SJeremy Linton */
cppc_perf_ctrs_in_pcc(void)1367ae2df912SJeremy Linton bool cppc_perf_ctrs_in_pcc(void)
1368ae2df912SJeremy Linton {
1369ae2df912SJeremy Linton int cpu;
1370ae2df912SJeremy Linton
1371ae2df912SJeremy Linton for_each_present_cpu(cpu) {
1372ae2df912SJeremy Linton struct cpc_register_resource *ref_perf_reg;
1373ae2df912SJeremy Linton struct cpc_desc *cpc_desc;
1374ae2df912SJeremy Linton
1375ae2df912SJeremy Linton cpc_desc = per_cpu(cpc_desc_ptr, cpu);
1376ae2df912SJeremy Linton
1377ae2df912SJeremy Linton if (CPC_IN_PCC(&cpc_desc->cpc_regs[DELIVERED_CTR]) ||
1378ae2df912SJeremy Linton CPC_IN_PCC(&cpc_desc->cpc_regs[REFERENCE_CTR]) ||
1379ae2df912SJeremy Linton CPC_IN_PCC(&cpc_desc->cpc_regs[CTR_WRAP_TIME]))
1380ae2df912SJeremy Linton return true;
1381ae2df912SJeremy Linton
1382ae2df912SJeremy Linton
1383ae2df912SJeremy Linton ref_perf_reg = &cpc_desc->cpc_regs[REFERENCE_PERF];
1384ae2df912SJeremy Linton
1385ae2df912SJeremy Linton /*
1386ae2df912SJeremy Linton * If reference perf register is not supported then we should
1387ae2df912SJeremy Linton * use the nominal perf value
1388ae2df912SJeremy Linton */
1389ae2df912SJeremy Linton if (!CPC_SUPPORTED(ref_perf_reg))
1390ae2df912SJeremy Linton ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
1391ae2df912SJeremy Linton
1392ae2df912SJeremy Linton if (CPC_IN_PCC(ref_perf_reg))
1393ae2df912SJeremy Linton return true;
1394ae2df912SJeremy Linton }
1395ae2df912SJeremy Linton
1396ae2df912SJeremy Linton return false;
1397ae2df912SJeremy Linton }
1398ae2df912SJeremy Linton EXPORT_SYMBOL_GPL(cppc_perf_ctrs_in_pcc);
1399ae2df912SJeremy Linton
1400ae2df912SJeremy Linton /**
1401603fadf3SBjorn Helgaas * cppc_get_perf_ctrs - Read a CPU's performance feedback counters.
1402337aadffSAshwin Chaugule * @cpunum: CPU from which to read counters.
1403337aadffSAshwin Chaugule * @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
1404337aadffSAshwin Chaugule *
1405337aadffSAshwin Chaugule * Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
1406337aadffSAshwin Chaugule */
cppc_get_perf_ctrs(int cpunum,struct cppc_perf_fb_ctrs * perf_fb_ctrs)1407337aadffSAshwin Chaugule int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
1408337aadffSAshwin Chaugule {
1409337aadffSAshwin Chaugule struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
1410158c998eSAshwin Chaugule struct cpc_register_resource *delivered_reg, *reference_reg,
1411158c998eSAshwin Chaugule *ref_perf_reg, *ctr_wrap_reg;
141285b1407bSGeorge Cherian int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
14136fa12d58SPrashanth Prakash struct cppc_pcc_data *pcc_ss_data = NULL;
1414158c998eSAshwin Chaugule u64 delivered, reference, ref_perf, ctr_wrap_time;
1415850d64a4SPrakash, Prashanth int ret = 0, regs_in_pcc = 0;
1416337aadffSAshwin Chaugule
14176fa12d58SPrashanth Prakash if (!cpc_desc) {
1418337aadffSAshwin Chaugule pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
1419337aadffSAshwin Chaugule return -ENODEV;
1420337aadffSAshwin Chaugule }
1421337aadffSAshwin Chaugule
1422337aadffSAshwin Chaugule delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
1423337aadffSAshwin Chaugule reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
1424158c998eSAshwin Chaugule ref_perf_reg = &cpc_desc->cpc_regs[REFERENCE_PERF];
1425158c998eSAshwin Chaugule ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME];
1426158c998eSAshwin Chaugule
1427158c998eSAshwin Chaugule /*
1428603fadf3SBjorn Helgaas * If reference perf register is not supported then we should
1429158c998eSAshwin Chaugule * use the nominal perf value
1430158c998eSAshwin Chaugule */
1431158c998eSAshwin Chaugule if (!CPC_SUPPORTED(ref_perf_reg))
1432158c998eSAshwin Chaugule ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
1433337aadffSAshwin Chaugule
1434337aadffSAshwin Chaugule /* Are any of the regs PCC ?*/
1435158c998eSAshwin Chaugule if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) ||
1436158c998eSAshwin Chaugule CPC_IN_PCC(ctr_wrap_reg) || CPC_IN_PCC(ref_perf_reg)) {
14376fa12d58SPrashanth Prakash if (pcc_ss_id < 0) {
14386fa12d58SPrashanth Prakash pr_debug("Invalid pcc_ss_id\n");
14396fa12d58SPrashanth Prakash return -ENODEV;
14406fa12d58SPrashanth Prakash }
14416fa12d58SPrashanth Prakash pcc_ss_data = pcc_data[pcc_ss_id];
144285b1407bSGeorge Cherian down_write(&pcc_ss_data->pcc_lock);
1443850d64a4SPrakash, Prashanth regs_in_pcc = 1;
1444337aadffSAshwin Chaugule /* Ring doorbell once to update PCC subspace */
144585b1407bSGeorge Cherian if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
1446337aadffSAshwin Chaugule ret = -EIO;
1447337aadffSAshwin Chaugule goto out_err;
1448337aadffSAshwin Chaugule }
1449337aadffSAshwin Chaugule }
1450337aadffSAshwin Chaugule
1451a6cbcdd5SSrinivas Pandruvada cpc_read(cpunum, delivered_reg, &delivered);
1452a6cbcdd5SSrinivas Pandruvada cpc_read(cpunum, reference_reg, &reference);
1453a6cbcdd5SSrinivas Pandruvada cpc_read(cpunum, ref_perf_reg, &ref_perf);
1454337aadffSAshwin Chaugule
1455158c998eSAshwin Chaugule /*
1456158c998eSAshwin Chaugule * Per spec, if ctr_wrap_time optional register is unsupported, then the
1457158c998eSAshwin Chaugule * performance counters are assumed to never wrap during the lifetime of
1458158c998eSAshwin Chaugule * platform
1459158c998eSAshwin Chaugule */
1460158c998eSAshwin Chaugule ctr_wrap_time = (u64)(~((u64)0));
1461158c998eSAshwin Chaugule if (CPC_SUPPORTED(ctr_wrap_reg))
1462a6cbcdd5SSrinivas Pandruvada cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time);
1463158c998eSAshwin Chaugule
1464158c998eSAshwin Chaugule if (!delivered || !reference || !ref_perf) {
1465337aadffSAshwin Chaugule ret = -EFAULT;
1466337aadffSAshwin Chaugule goto out_err;
1467337aadffSAshwin Chaugule }
1468337aadffSAshwin Chaugule
1469337aadffSAshwin Chaugule perf_fb_ctrs->delivered = delivered;
1470337aadffSAshwin Chaugule perf_fb_ctrs->reference = reference;
1471158c998eSAshwin Chaugule perf_fb_ctrs->reference_perf = ref_perf;
14722c74d847SPrakash, Prashanth perf_fb_ctrs->wraparound_time = ctr_wrap_time;
1473337aadffSAshwin Chaugule out_err:
1474850d64a4SPrakash, Prashanth if (regs_in_pcc)
147585b1407bSGeorge Cherian up_write(&pcc_ss_data->pcc_lock);
1476337aadffSAshwin Chaugule return ret;
1477337aadffSAshwin Chaugule }
1478337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
1479337aadffSAshwin Chaugule
14807bc1fcd3SPerry Yuan /*
14817bc1fcd3SPerry Yuan * Set Energy Performance Preference Register value through
14827bc1fcd3SPerry Yuan * Performance Controls Interface
14837bc1fcd3SPerry Yuan */
cppc_set_epp_perf(int cpu,struct cppc_perf_ctrls * perf_ctrls,bool enable)14847bc1fcd3SPerry Yuan int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, bool enable)
14857bc1fcd3SPerry Yuan {
14867bc1fcd3SPerry Yuan int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
14877bc1fcd3SPerry Yuan struct cpc_register_resource *epp_set_reg;
14887bc1fcd3SPerry Yuan struct cpc_register_resource *auto_sel_reg;
14897bc1fcd3SPerry Yuan struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
14907bc1fcd3SPerry Yuan struct cppc_pcc_data *pcc_ss_data = NULL;
14917bc1fcd3SPerry Yuan int ret;
14927bc1fcd3SPerry Yuan
14937bc1fcd3SPerry Yuan if (!cpc_desc) {
14947bc1fcd3SPerry Yuan pr_debug("No CPC descriptor for CPU:%d\n", cpu);
14957bc1fcd3SPerry Yuan return -ENODEV;
14967bc1fcd3SPerry Yuan }
14977bc1fcd3SPerry Yuan
14987bc1fcd3SPerry Yuan auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
14997bc1fcd3SPerry Yuan epp_set_reg = &cpc_desc->cpc_regs[ENERGY_PERF];
15007bc1fcd3SPerry Yuan
15017bc1fcd3SPerry Yuan if (CPC_IN_PCC(epp_set_reg) || CPC_IN_PCC(auto_sel_reg)) {
15027bc1fcd3SPerry Yuan if (pcc_ss_id < 0) {
15037bc1fcd3SPerry Yuan pr_debug("Invalid pcc_ss_id for CPU:%d\n", cpu);
15047bc1fcd3SPerry Yuan return -ENODEV;
15057bc1fcd3SPerry Yuan }
15067bc1fcd3SPerry Yuan
15077bc1fcd3SPerry Yuan if (CPC_SUPPORTED(auto_sel_reg)) {
15087bc1fcd3SPerry Yuan ret = cpc_write(cpu, auto_sel_reg, enable);
15097bc1fcd3SPerry Yuan if (ret)
15107bc1fcd3SPerry Yuan return ret;
15117bc1fcd3SPerry Yuan }
15127bc1fcd3SPerry Yuan
15137bc1fcd3SPerry Yuan if (CPC_SUPPORTED(epp_set_reg)) {
15147bc1fcd3SPerry Yuan ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf);
15157bc1fcd3SPerry Yuan if (ret)
15167bc1fcd3SPerry Yuan return ret;
15177bc1fcd3SPerry Yuan }
15187bc1fcd3SPerry Yuan
15197bc1fcd3SPerry Yuan pcc_ss_data = pcc_data[pcc_ss_id];
15207bc1fcd3SPerry Yuan
15217bc1fcd3SPerry Yuan down_write(&pcc_ss_data->pcc_lock);
15227bc1fcd3SPerry Yuan /* after writing CPC, transfer the ownership of PCC to platform */
15237bc1fcd3SPerry Yuan ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
15247bc1fcd3SPerry Yuan up_write(&pcc_ss_data->pcc_lock);
152529dbea4cSMario Limonciello } else if (osc_cpc_flexible_adr_space_confirmed &&
152629dbea4cSMario Limonciello CPC_SUPPORTED(epp_set_reg) && CPC_IN_FFH(epp_set_reg)) {
152729dbea4cSMario Limonciello ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf);
15287bc1fcd3SPerry Yuan } else {
15297bc1fcd3SPerry Yuan ret = -ENOTSUPP;
153029dbea4cSMario Limonciello pr_debug("_CPC in PCC and _CPC in FFH are not supported\n");
15317bc1fcd3SPerry Yuan }
15327bc1fcd3SPerry Yuan
15337bc1fcd3SPerry Yuan return ret;
15347bc1fcd3SPerry Yuan }
15357bc1fcd3SPerry Yuan EXPORT_SYMBOL_GPL(cppc_set_epp_perf);
15367bc1fcd3SPerry Yuan
1537337aadffSAshwin Chaugule /**
1538c984f5d5SWyes Karny * cppc_get_auto_sel_caps - Read autonomous selection register.
1539c984f5d5SWyes Karny * @cpunum : CPU from which to read register.
1540c984f5d5SWyes Karny * @perf_caps : struct where autonomous selection register value is updated.
1541c984f5d5SWyes Karny */
cppc_get_auto_sel_caps(int cpunum,struct cppc_perf_caps * perf_caps)1542c984f5d5SWyes Karny int cppc_get_auto_sel_caps(int cpunum, struct cppc_perf_caps *perf_caps)
1543c984f5d5SWyes Karny {
1544c984f5d5SWyes Karny struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
1545c984f5d5SWyes Karny struct cpc_register_resource *auto_sel_reg;
1546c984f5d5SWyes Karny u64 auto_sel;
1547c984f5d5SWyes Karny
1548c984f5d5SWyes Karny if (!cpc_desc) {
1549c984f5d5SWyes Karny pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
1550c984f5d5SWyes Karny return -ENODEV;
1551c984f5d5SWyes Karny }
1552c984f5d5SWyes Karny
1553c984f5d5SWyes Karny auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
1554c984f5d5SWyes Karny
1555c984f5d5SWyes Karny if (!CPC_SUPPORTED(auto_sel_reg))
1556c984f5d5SWyes Karny pr_warn_once("Autonomous mode is not unsupported!\n");
1557c984f5d5SWyes Karny
1558c984f5d5SWyes Karny if (CPC_IN_PCC(auto_sel_reg)) {
1559c984f5d5SWyes Karny int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
1560c984f5d5SWyes Karny struct cppc_pcc_data *pcc_ss_data = NULL;
1561c984f5d5SWyes Karny int ret = 0;
1562c984f5d5SWyes Karny
1563c984f5d5SWyes Karny if (pcc_ss_id < 0)
1564c984f5d5SWyes Karny return -ENODEV;
1565c984f5d5SWyes Karny
1566c984f5d5SWyes Karny pcc_ss_data = pcc_data[pcc_ss_id];
1567c984f5d5SWyes Karny
1568c984f5d5SWyes Karny down_write(&pcc_ss_data->pcc_lock);
1569c984f5d5SWyes Karny
1570c984f5d5SWyes Karny if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0) {
1571c984f5d5SWyes Karny cpc_read(cpunum, auto_sel_reg, &auto_sel);
1572c984f5d5SWyes Karny perf_caps->auto_sel = (bool)auto_sel;
1573c984f5d5SWyes Karny } else {
1574c984f5d5SWyes Karny ret = -EIO;
1575c984f5d5SWyes Karny }
1576c984f5d5SWyes Karny
1577c984f5d5SWyes Karny up_write(&pcc_ss_data->pcc_lock);
1578c984f5d5SWyes Karny
1579c984f5d5SWyes Karny return ret;
1580c984f5d5SWyes Karny }
1581c984f5d5SWyes Karny
1582c984f5d5SWyes Karny return 0;
1583c984f5d5SWyes Karny }
1584c984f5d5SWyes Karny EXPORT_SYMBOL_GPL(cppc_get_auto_sel_caps);
1585c984f5d5SWyes Karny
1586c984f5d5SWyes Karny /**
1587c984f5d5SWyes Karny * cppc_set_auto_sel - Write autonomous selection register.
1588c984f5d5SWyes Karny * @cpu : CPU to which to write register.
1589c984f5d5SWyes Karny * @enable : the desired value of autonomous selection resiter to be updated.
1590c984f5d5SWyes Karny */
cppc_set_auto_sel(int cpu,bool enable)1591c984f5d5SWyes Karny int cppc_set_auto_sel(int cpu, bool enable)
1592c984f5d5SWyes Karny {
1593c984f5d5SWyes Karny int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
1594c984f5d5SWyes Karny struct cpc_register_resource *auto_sel_reg;
1595c984f5d5SWyes Karny struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
1596c984f5d5SWyes Karny struct cppc_pcc_data *pcc_ss_data = NULL;
1597c984f5d5SWyes Karny int ret = -EINVAL;
1598c984f5d5SWyes Karny
1599c984f5d5SWyes Karny if (!cpc_desc) {
1600c984f5d5SWyes Karny pr_debug("No CPC descriptor for CPU:%d\n", cpu);
1601c984f5d5SWyes Karny return -ENODEV;
1602c984f5d5SWyes Karny }
1603c984f5d5SWyes Karny
1604c984f5d5SWyes Karny auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
1605c984f5d5SWyes Karny
1606c984f5d5SWyes Karny if (CPC_IN_PCC(auto_sel_reg)) {
1607c984f5d5SWyes Karny if (pcc_ss_id < 0) {
1608c984f5d5SWyes Karny pr_debug("Invalid pcc_ss_id\n");
1609c984f5d5SWyes Karny return -ENODEV;
1610c984f5d5SWyes Karny }
1611c984f5d5SWyes Karny
1612c984f5d5SWyes Karny if (CPC_SUPPORTED(auto_sel_reg)) {
1613c984f5d5SWyes Karny ret = cpc_write(cpu, auto_sel_reg, enable);
1614c984f5d5SWyes Karny if (ret)
1615c984f5d5SWyes Karny return ret;
1616c984f5d5SWyes Karny }
1617c984f5d5SWyes Karny
1618c984f5d5SWyes Karny pcc_ss_data = pcc_data[pcc_ss_id];
1619c984f5d5SWyes Karny
1620c984f5d5SWyes Karny down_write(&pcc_ss_data->pcc_lock);
1621c984f5d5SWyes Karny /* after writing CPC, transfer the ownership of PCC to platform */
1622c984f5d5SWyes Karny ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
1623c984f5d5SWyes Karny up_write(&pcc_ss_data->pcc_lock);
1624c984f5d5SWyes Karny } else {
1625c984f5d5SWyes Karny ret = -ENOTSUPP;
1626c984f5d5SWyes Karny pr_debug("_CPC in PCC is not supported\n");
1627c984f5d5SWyes Karny }
1628c984f5d5SWyes Karny
1629c984f5d5SWyes Karny return ret;
1630c984f5d5SWyes Karny }
1631c984f5d5SWyes Karny EXPORT_SYMBOL_GPL(cppc_set_auto_sel);
1632c984f5d5SWyes Karny
1633c984f5d5SWyes Karny /**
1634fb0b00afSJinzhou Su * cppc_set_enable - Set to enable CPPC on the processor by writing the
1635fb0b00afSJinzhou Su * Continuous Performance Control package EnableRegister field.
1636fb0b00afSJinzhou Su * @cpu: CPU for which to enable CPPC register.
1637fb0b00afSJinzhou Su * @enable: 0 - disable, 1 - enable CPPC feature on the processor.
1638fb0b00afSJinzhou Su *
1639fb0b00afSJinzhou Su * Return: 0 for success, -ERRNO or -EIO otherwise.
1640fb0b00afSJinzhou Su */
cppc_set_enable(int cpu,bool enable)1641fb0b00afSJinzhou Su int cppc_set_enable(int cpu, bool enable)
1642fb0b00afSJinzhou Su {
1643fb0b00afSJinzhou Su int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
1644fb0b00afSJinzhou Su struct cpc_register_resource *enable_reg;
1645fb0b00afSJinzhou Su struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
1646fb0b00afSJinzhou Su struct cppc_pcc_data *pcc_ss_data = NULL;
1647fb0b00afSJinzhou Su int ret = -EINVAL;
1648fb0b00afSJinzhou Su
1649fb0b00afSJinzhou Su if (!cpc_desc) {
1650fb0b00afSJinzhou Su pr_debug("No CPC descriptor for CPU:%d\n", cpu);
1651fb0b00afSJinzhou Su return -EINVAL;
1652fb0b00afSJinzhou Su }
1653fb0b00afSJinzhou Su
1654fb0b00afSJinzhou Su enable_reg = &cpc_desc->cpc_regs[ENABLE];
1655fb0b00afSJinzhou Su
1656fb0b00afSJinzhou Su if (CPC_IN_PCC(enable_reg)) {
1657fb0b00afSJinzhou Su
1658fb0b00afSJinzhou Su if (pcc_ss_id < 0)
1659fb0b00afSJinzhou Su return -EIO;
1660fb0b00afSJinzhou Su
1661fb0b00afSJinzhou Su ret = cpc_write(cpu, enable_reg, enable);
1662fb0b00afSJinzhou Su if (ret)
1663fb0b00afSJinzhou Su return ret;
1664fb0b00afSJinzhou Su
1665fb0b00afSJinzhou Su pcc_ss_data = pcc_data[pcc_ss_id];
1666fb0b00afSJinzhou Su
1667fb0b00afSJinzhou Su down_write(&pcc_ss_data->pcc_lock);
1668fb0b00afSJinzhou Su /* after writing CPC, transfer the ownership of PCC to platfrom */
1669fb0b00afSJinzhou Su ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
1670fb0b00afSJinzhou Su up_write(&pcc_ss_data->pcc_lock);
1671fb0b00afSJinzhou Su return ret;
1672fb0b00afSJinzhou Su }
1673fb0b00afSJinzhou Su
1674fb0b00afSJinzhou Su return cpc_write(cpu, enable_reg, enable);
1675fb0b00afSJinzhou Su }
1676fb0b00afSJinzhou Su EXPORT_SYMBOL_GPL(cppc_set_enable);
1677fb0b00afSJinzhou Su
1678fb0b00afSJinzhou Su /**
1679603fadf3SBjorn Helgaas * cppc_set_perf - Set a CPU's performance controls.
1680337aadffSAshwin Chaugule * @cpu: CPU for which to set performance controls.
1681337aadffSAshwin Chaugule * @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
1682337aadffSAshwin Chaugule *
1683337aadffSAshwin Chaugule * Return: 0 for success, -ERRNO otherwise.
1684337aadffSAshwin Chaugule */
cppc_set_perf(int cpu,struct cppc_perf_ctrls * perf_ctrls)1685337aadffSAshwin Chaugule int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
1686337aadffSAshwin Chaugule {
1687337aadffSAshwin Chaugule struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
168876531df5SWyes Karny struct cpc_register_resource *desired_reg, *min_perf_reg, *max_perf_reg;
168985b1407bSGeorge Cherian int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
16906fa12d58SPrashanth Prakash struct cppc_pcc_data *pcc_ss_data = NULL;
1691337aadffSAshwin Chaugule int ret = 0;
1692337aadffSAshwin Chaugule
16936fa12d58SPrashanth Prakash if (!cpc_desc) {
1694337aadffSAshwin Chaugule pr_debug("No CPC descriptor for CPU:%d\n", cpu);
1695337aadffSAshwin Chaugule return -ENODEV;
1696337aadffSAshwin Chaugule }
1697337aadffSAshwin Chaugule
1698337aadffSAshwin Chaugule desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
169976531df5SWyes Karny min_perf_reg = &cpc_desc->cpc_regs[MIN_PERF];
170076531df5SWyes Karny max_perf_reg = &cpc_desc->cpc_regs[MAX_PERF];
1701337aadffSAshwin Chaugule
170280b8286aSPrakash, Prashanth /*
170380b8286aSPrakash, Prashanth * This is Phase-I where we want to write to CPC registers
170480b8286aSPrakash, Prashanth * -> We want all CPUs to be able to execute this phase in parallel
170580b8286aSPrakash, Prashanth *
170680b8286aSPrakash, Prashanth * Since read_lock can be acquired by multiple CPUs simultaneously we
170780b8286aSPrakash, Prashanth * achieve that goal here
170880b8286aSPrakash, Prashanth */
170976531df5SWyes Karny if (CPC_IN_PCC(desired_reg) || CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg)) {
17106fa12d58SPrashanth Prakash if (pcc_ss_id < 0) {
17116fa12d58SPrashanth Prakash pr_debug("Invalid pcc_ss_id\n");
17126fa12d58SPrashanth Prakash return -ENODEV;
17136fa12d58SPrashanth Prakash }
17146fa12d58SPrashanth Prakash pcc_ss_data = pcc_data[pcc_ss_id];
171585b1407bSGeorge Cherian down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */
171685b1407bSGeorge Cherian if (pcc_ss_data->platform_owns_pcc) {
171785b1407bSGeorge Cherian ret = check_pcc_chan(pcc_ss_id, false);
171880b8286aSPrakash, Prashanth if (ret) {
171985b1407bSGeorge Cherian up_read(&pcc_ss_data->pcc_lock);
172080b8286aSPrakash, Prashanth return ret;
172180b8286aSPrakash, Prashanth }
172280b8286aSPrakash, Prashanth }
1723139aee73SPrakash, Prashanth /*
1724139aee73SPrakash, Prashanth * Update the pending_write to make sure a PCC CMD_READ will not
1725139aee73SPrakash, Prashanth * arrive and steal the channel during the switch to write lock
1726139aee73SPrakash, Prashanth */
172785b1407bSGeorge Cherian pcc_ss_data->pending_pcc_write_cmd = true;
172885b1407bSGeorge Cherian cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt;
172980b8286aSPrakash, Prashanth cpc_desc->write_cmd_status = 0;
1730ad62e1e6SAshwin Chaugule }
1731ad62e1e6SAshwin Chaugule
1732a6cbcdd5SSrinivas Pandruvada cpc_write(cpu, desired_reg, perf_ctrls->desired_perf);
1733337aadffSAshwin Chaugule
173476531df5SWyes Karny /*
173576531df5SWyes Karny * Only write if min_perf and max_perf not zero. Some drivers pass zero
173676531df5SWyes Karny * value to min and max perf, but they don't mean to set the zero value,
173776531df5SWyes Karny * they just don't want to write to those registers.
173876531df5SWyes Karny */
173976531df5SWyes Karny if (perf_ctrls->min_perf)
174076531df5SWyes Karny cpc_write(cpu, min_perf_reg, perf_ctrls->min_perf);
174176531df5SWyes Karny if (perf_ctrls->max_perf)
174276531df5SWyes Karny cpc_write(cpu, max_perf_reg, perf_ctrls->max_perf);
174376531df5SWyes Karny
174476531df5SWyes Karny if (CPC_IN_PCC(desired_reg) || CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg))
174585b1407bSGeorge Cherian up_read(&pcc_ss_data->pcc_lock); /* END Phase-I */
174680b8286aSPrakash, Prashanth /*
174780b8286aSPrakash, Prashanth * This is Phase-II where we transfer the ownership of PCC to Platform
174880b8286aSPrakash, Prashanth *
174980b8286aSPrakash, Prashanth * Short Summary: Basically if we think of a group of cppc_set_perf
175080b8286aSPrakash, Prashanth * requests that happened in short overlapping interval. The last CPU to
175180b8286aSPrakash, Prashanth * come out of Phase-I will enter Phase-II and ring the doorbell.
175280b8286aSPrakash, Prashanth *
175380b8286aSPrakash, Prashanth * We have the following requirements for Phase-II:
175480b8286aSPrakash, Prashanth * 1. We want to execute Phase-II only when there are no CPUs
175580b8286aSPrakash, Prashanth * currently executing in Phase-I
175680b8286aSPrakash, Prashanth * 2. Once we start Phase-II we want to avoid all other CPUs from
175780b8286aSPrakash, Prashanth * entering Phase-I.
175880b8286aSPrakash, Prashanth * 3. We want only one CPU among all those who went through Phase-I
175980b8286aSPrakash, Prashanth * to run phase-II
176080b8286aSPrakash, Prashanth *
176180b8286aSPrakash, Prashanth * If write_trylock fails to get the lock and doesn't transfer the
176280b8286aSPrakash, Prashanth * PCC ownership to the platform, then one of the following will be TRUE
176380b8286aSPrakash, Prashanth * 1. There is at-least one CPU in Phase-I which will later execute
176480b8286aSPrakash, Prashanth * write_trylock, so the CPUs in Phase-I will be responsible for
176580b8286aSPrakash, Prashanth * executing the Phase-II.
176680b8286aSPrakash, Prashanth * 2. Some other CPU has beaten this CPU to successfully execute the
176780b8286aSPrakash, Prashanth * write_trylock and has already acquired the write_lock. We know for a
176880b8286aSPrakash, Prashanth * fact it (other CPU acquiring the write_lock) couldn't have happened
176980b8286aSPrakash, Prashanth * before this CPU's Phase-I as we held the read_lock.
177080b8286aSPrakash, Prashanth * 3. Some other CPU executing pcc CMD_READ has stolen the
177180b8286aSPrakash, Prashanth * down_write, in which case, send_pcc_cmd will check for pending
177280b8286aSPrakash, Prashanth * CMD_WRITE commands by checking the pending_pcc_write_cmd.
177380b8286aSPrakash, Prashanth * So this CPU can be certain that its request will be delivered
177480b8286aSPrakash, Prashanth * So in all cases, this CPU knows that its request will be delivered
177580b8286aSPrakash, Prashanth * by another CPU and can return
177680b8286aSPrakash, Prashanth *
177780b8286aSPrakash, Prashanth * After getting the down_write we still need to check for
177880b8286aSPrakash, Prashanth * pending_pcc_write_cmd to take care of the following scenario
177980b8286aSPrakash, Prashanth * The thread running this code could be scheduled out between
178080b8286aSPrakash, Prashanth * Phase-I and Phase-II. Before it is scheduled back on, another CPU
178180b8286aSPrakash, Prashanth * could have delivered the request to Platform by triggering the
178280b8286aSPrakash, Prashanth * doorbell and transferred the ownership of PCC to platform. So this
178380b8286aSPrakash, Prashanth * avoids triggering an unnecessary doorbell and more importantly before
178480b8286aSPrakash, Prashanth * triggering the doorbell it makes sure that the PCC channel ownership
178580b8286aSPrakash, Prashanth * is still with OSPM.
178680b8286aSPrakash, Prashanth * pending_pcc_write_cmd can also be cleared by a different CPU, if
178780b8286aSPrakash, Prashanth * there was a pcc CMD_READ waiting on down_write and it steals the lock
1788935ab850STom Saeger * before the pcc CMD_WRITE is completed. send_pcc_cmd checks for this
178980b8286aSPrakash, Prashanth * case during a CMD_READ and if there are pending writes it delivers
179080b8286aSPrakash, Prashanth * the write command before servicing the read command
179180b8286aSPrakash, Prashanth */
179276531df5SWyes Karny if (CPC_IN_PCC(desired_reg) || CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg)) {
179385b1407bSGeorge Cherian if (down_write_trylock(&pcc_ss_data->pcc_lock)) {/* BEGIN Phase-II */
179480b8286aSPrakash, Prashanth /* Update only if there are pending write commands */
179585b1407bSGeorge Cherian if (pcc_ss_data->pending_pcc_write_cmd)
179685b1407bSGeorge Cherian send_pcc_cmd(pcc_ss_id, CMD_WRITE);
179785b1407bSGeorge Cherian up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */
179880b8286aSPrakash, Prashanth } else
179980b8286aSPrakash, Prashanth /* Wait until pcc_write_cnt is updated by send_pcc_cmd */
180085b1407bSGeorge Cherian wait_event(pcc_ss_data->pcc_write_wait_q,
180185b1407bSGeorge Cherian cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt);
180280b8286aSPrakash, Prashanth
180380b8286aSPrakash, Prashanth /* send_pcc_cmd updates the status in case of failure */
180480b8286aSPrakash, Prashanth ret = cpc_desc->write_cmd_status;
1805337aadffSAshwin Chaugule }
1806337aadffSAshwin Chaugule return ret;
1807337aadffSAshwin Chaugule }
1808337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(cppc_set_perf);
1809be8b88d7SPrakash, Prashanth
1810be8b88d7SPrakash, Prashanth /**
1811be8b88d7SPrakash, Prashanth * cppc_get_transition_latency - returns frequency transition latency in ns
1812fda7be20SYang Li * @cpu_num: CPU number for per_cpu().
1813be8b88d7SPrakash, Prashanth *
1814935ab850STom Saeger * ACPI CPPC does not explicitly specify how a platform can specify the
1815935ab850STom Saeger * transition latency for performance change requests. The closest we have
1816be8b88d7SPrakash, Prashanth * is the timing information from the PCCT tables which provides the info
1817be8b88d7SPrakash, Prashanth * on the number and frequency of PCC commands the platform can handle.
18186380b7b2SPierre Gondois *
18196380b7b2SPierre Gondois * If desired_reg is in the SystemMemory or SystemIo ACPI address space,
18206380b7b2SPierre Gondois * then assume there is no latency.
1821be8b88d7SPrakash, Prashanth */
cppc_get_transition_latency(int cpu_num)1822be8b88d7SPrakash, Prashanth unsigned int cppc_get_transition_latency(int cpu_num)
1823be8b88d7SPrakash, Prashanth {
1824be8b88d7SPrakash, Prashanth /*
1825be8b88d7SPrakash, Prashanth * Expected transition latency is based on the PCCT timing values
1826be8b88d7SPrakash, Prashanth * Below are definition from ACPI spec:
1827be8b88d7SPrakash, Prashanth * pcc_nominal- Expected latency to process a command, in microseconds
1828be8b88d7SPrakash, Prashanth * pcc_mpar - The maximum number of periodic requests that the subspace
1829be8b88d7SPrakash, Prashanth * channel can support, reported in commands per minute. 0
1830be8b88d7SPrakash, Prashanth * indicates no limitation.
1831be8b88d7SPrakash, Prashanth * pcc_mrtt - The minimum amount of time that OSPM must wait after the
1832be8b88d7SPrakash, Prashanth * completion of a command before issuing the next command,
1833be8b88d7SPrakash, Prashanth * in microseconds.
1834be8b88d7SPrakash, Prashanth */
1835be8b88d7SPrakash, Prashanth unsigned int latency_ns = 0;
1836be8b88d7SPrakash, Prashanth struct cpc_desc *cpc_desc;
1837be8b88d7SPrakash, Prashanth struct cpc_register_resource *desired_reg;
183885b1407bSGeorge Cherian int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num);
18391ecbd717SGeorge Cherian struct cppc_pcc_data *pcc_ss_data;
1840be8b88d7SPrakash, Prashanth
1841be8b88d7SPrakash, Prashanth cpc_desc = per_cpu(cpc_desc_ptr, cpu_num);
1842be8b88d7SPrakash, Prashanth if (!cpc_desc)
1843be8b88d7SPrakash, Prashanth return CPUFREQ_ETERNAL;
1844be8b88d7SPrakash, Prashanth
1845be8b88d7SPrakash, Prashanth desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
18466380b7b2SPierre Gondois if (CPC_IN_SYSTEM_MEMORY(desired_reg) || CPC_IN_SYSTEM_IO(desired_reg))
18476380b7b2SPierre Gondois return 0;
18486380b7b2SPierre Gondois else if (!CPC_IN_PCC(desired_reg))
1849be8b88d7SPrakash, Prashanth return CPUFREQ_ETERNAL;
1850be8b88d7SPrakash, Prashanth
18511ecbd717SGeorge Cherian if (pcc_ss_id < 0)
18521ecbd717SGeorge Cherian return CPUFREQ_ETERNAL;
18531ecbd717SGeorge Cherian
18541ecbd717SGeorge Cherian pcc_ss_data = pcc_data[pcc_ss_id];
185585b1407bSGeorge Cherian if (pcc_ss_data->pcc_mpar)
185685b1407bSGeorge Cherian latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar);
1857be8b88d7SPrakash, Prashanth
185885b1407bSGeorge Cherian latency_ns = max(latency_ns, pcc_ss_data->pcc_nominal * 1000);
185985b1407bSGeorge Cherian latency_ns = max(latency_ns, pcc_ss_data->pcc_mrtt * 1000);
1860be8b88d7SPrakash, Prashanth
1861be8b88d7SPrakash, Prashanth return latency_ns;
1862be8b88d7SPrakash, Prashanth }
1863be8b88d7SPrakash, Prashanth EXPORT_SYMBOL_GPL(cppc_get_transition_latency);
186433e89c16SVincent Guittot
186533e89c16SVincent Guittot /* Minimum struct length needed for the DMI processor entry we want */
186633e89c16SVincent Guittot #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
186733e89c16SVincent Guittot
186833e89c16SVincent Guittot /* Offset in the DMI processor structure for the max frequency */
186933e89c16SVincent Guittot #define DMI_PROCESSOR_MAX_SPEED 0x14
187033e89c16SVincent Guittot
187133e89c16SVincent Guittot /* Callback function used to retrieve the max frequency from DMI */
cppc_find_dmi_mhz(const struct dmi_header * dm,void * private)187233e89c16SVincent Guittot static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
187333e89c16SVincent Guittot {
187433e89c16SVincent Guittot const u8 *dmi_data = (const u8 *)dm;
187533e89c16SVincent Guittot u16 *mhz = (u16 *)private;
187633e89c16SVincent Guittot
187733e89c16SVincent Guittot if (dm->type == DMI_ENTRY_PROCESSOR &&
187833e89c16SVincent Guittot dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
187933e89c16SVincent Guittot u16 val = (u16)get_unaligned((const u16 *)
188033e89c16SVincent Guittot (dmi_data + DMI_PROCESSOR_MAX_SPEED));
188133e89c16SVincent Guittot *mhz = val > *mhz ? val : *mhz;
188233e89c16SVincent Guittot }
188333e89c16SVincent Guittot }
188433e89c16SVincent Guittot
188533e89c16SVincent Guittot /* Look up the max frequency in DMI */
cppc_get_dmi_max_khz(void)188633e89c16SVincent Guittot static u64 cppc_get_dmi_max_khz(void)
188733e89c16SVincent Guittot {
188833e89c16SVincent Guittot u16 mhz = 0;
188933e89c16SVincent Guittot
189033e89c16SVincent Guittot dmi_walk(cppc_find_dmi_mhz, &mhz);
189133e89c16SVincent Guittot
189233e89c16SVincent Guittot /*
189333e89c16SVincent Guittot * Real stupid fallback value, just in case there is no
189433e89c16SVincent Guittot * actual value set.
189533e89c16SVincent Guittot */
189633e89c16SVincent Guittot mhz = mhz ? mhz : 1;
189733e89c16SVincent Guittot
189833e89c16SVincent Guittot return KHZ_PER_MHZ * mhz;
189933e89c16SVincent Guittot }
190033e89c16SVincent Guittot
190133e89c16SVincent Guittot /*
190233e89c16SVincent Guittot * If CPPC lowest_freq and nominal_freq registers are exposed then we can
190333e89c16SVincent Guittot * use them to convert perf to freq and vice versa. The conversion is
190433e89c16SVincent Guittot * extrapolated as an affine function passing by the 2 points:
190533e89c16SVincent Guittot * - (Low perf, Low freq)
190633e89c16SVincent Guittot * - (Nominal perf, Nominal freq)
190733e89c16SVincent Guittot */
cppc_perf_to_khz(struct cppc_perf_caps * caps,unsigned int perf)190833e89c16SVincent Guittot unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned int perf)
190933e89c16SVincent Guittot {
191033e89c16SVincent Guittot s64 retval, offset = 0;
191133e89c16SVincent Guittot static u64 max_khz;
191233e89c16SVincent Guittot u64 mul, div;
191333e89c16SVincent Guittot
191433e89c16SVincent Guittot if (caps->lowest_freq && caps->nominal_freq) {
19152f2684c7Sliwei /* Avoid special case when nominal_freq is equal to lowest_freq */
19162f2684c7Sliwei if (caps->lowest_freq == caps->nominal_freq) {
19172f2684c7Sliwei mul = caps->nominal_freq;
19182f2684c7Sliwei div = caps->nominal_perf;
19192f2684c7Sliwei } else {
192033e89c16SVincent Guittot mul = caps->nominal_freq - caps->lowest_freq;
192133e89c16SVincent Guittot div = caps->nominal_perf - caps->lowest_perf;
19222f2684c7Sliwei }
19232f2684c7Sliwei mul *= KHZ_PER_MHZ;
192433e89c16SVincent Guittot offset = caps->nominal_freq * KHZ_PER_MHZ -
192533e89c16SVincent Guittot div64_u64(caps->nominal_perf * mul, div);
192633e89c16SVincent Guittot } else {
192733e89c16SVincent Guittot if (!max_khz)
192833e89c16SVincent Guittot max_khz = cppc_get_dmi_max_khz();
192933e89c16SVincent Guittot mul = max_khz;
193033e89c16SVincent Guittot div = caps->highest_perf;
193133e89c16SVincent Guittot }
193233e89c16SVincent Guittot
193333e89c16SVincent Guittot retval = offset + div64_u64(perf * mul, div);
193433e89c16SVincent Guittot if (retval >= 0)
193533e89c16SVincent Guittot return retval;
193633e89c16SVincent Guittot return 0;
193733e89c16SVincent Guittot }
193833e89c16SVincent Guittot EXPORT_SYMBOL_GPL(cppc_perf_to_khz);
193933e89c16SVincent Guittot
cppc_khz_to_perf(struct cppc_perf_caps * caps,unsigned int freq)194033e89c16SVincent Guittot unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned int freq)
194133e89c16SVincent Guittot {
194233e89c16SVincent Guittot s64 retval, offset = 0;
194333e89c16SVincent Guittot static u64 max_khz;
194433e89c16SVincent Guittot u64 mul, div;
194533e89c16SVincent Guittot
194633e89c16SVincent Guittot if (caps->lowest_freq && caps->nominal_freq) {
19472f2684c7Sliwei /* Avoid special case when nominal_freq is equal to lowest_freq */
19482f2684c7Sliwei if (caps->lowest_freq == caps->nominal_freq) {
19492f2684c7Sliwei mul = caps->nominal_perf;
19502f2684c7Sliwei div = caps->nominal_freq;
19512f2684c7Sliwei } else {
195233e89c16SVincent Guittot mul = caps->nominal_perf - caps->lowest_perf;
195333e89c16SVincent Guittot div = caps->nominal_freq - caps->lowest_freq;
19542f2684c7Sliwei }
195533e89c16SVincent Guittot /*
195633e89c16SVincent Guittot * We don't need to convert to kHz for computing offset and can
195733e89c16SVincent Guittot * directly use nominal_freq and lowest_freq as the div64_u64
195833e89c16SVincent Guittot * will remove the frequency unit.
195933e89c16SVincent Guittot */
196033e89c16SVincent Guittot offset = caps->nominal_perf -
196133e89c16SVincent Guittot div64_u64(caps->nominal_freq * mul, div);
196233e89c16SVincent Guittot /* But we need it for computing the perf level. */
196333e89c16SVincent Guittot div *= KHZ_PER_MHZ;
196433e89c16SVincent Guittot } else {
196533e89c16SVincent Guittot if (!max_khz)
196633e89c16SVincent Guittot max_khz = cppc_get_dmi_max_khz();
196733e89c16SVincent Guittot mul = caps->highest_perf;
196833e89c16SVincent Guittot div = max_khz;
196933e89c16SVincent Guittot }
197033e89c16SVincent Guittot
197133e89c16SVincent Guittot retval = offset + div64_u64(freq * mul, div);
197233e89c16SVincent Guittot if (retval >= 0)
197333e89c16SVincent Guittot return retval;
197433e89c16SVincent Guittot return 0;
197533e89c16SVincent Guittot }
197633e89c16SVincent Guittot EXPORT_SYMBOL_GPL(cppc_khz_to_perf);
1977