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 <[email protected]>
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>
4250b813b1SVincent Guittot #include <linux/dmi.h>
4350b813b1SVincent Guittot #include <linux/units.h>
445f60d5f6SAl Viro #include <linux/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
106aaf21ac9SMario Limonciello /* Check if a CPC register is in FFH */
107aaf21ac9SMario Limonciello #define CPC_IN_FFH(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
108aaf21ac9SMario Limonciello (cpc)->cpc_entry.reg.space_id == \
109aaf21ac9SMario Limonciello ACPI_ADR_SPACE_FIXED_HARDWARE)
110aaf21ac9SMario 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);
1688c6294ccSPetr Tesařík show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, guaranteed_perf);
1694773e77cSPrashanth Prakash show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_freq);
1704773e77cSPrashanth Prakash show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_freq);
1714773e77cSPrashanth Prakash
1722c74d847SPrakash, Prashanth show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, reference_perf);
1732c74d847SPrakash, Prashanth show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, wraparound_time);
1742c74d847SPrakash, Prashanth
1752f4a4d63SJarred White /* Check for valid access_width, otherwise, fallback to using bit_width */
1762f4a4d63SJarred White #define GET_BIT_WIDTH(reg) ((reg)->access_width ? (8 << ((reg)->access_width - 1)) : (reg)->bit_width)
1772f4a4d63SJarred White
1782f4a4d63SJarred White /* Shift and apply the mask for CPC reads/writes */
17960949b7bSClément Léger #define MASK_VAL_READ(reg, val) (((val) >> (reg)->bit_offset) & \
18005d92ee7SJarred White GENMASK(((reg)->bit_width) - 1, 0))
18160949b7bSClément Léger #define MASK_VAL_WRITE(reg, prev_val, val) \
18260949b7bSClément Léger ((((val) & GENMASK(((reg)->bit_width) - 1, 0)) << (reg)->bit_offset) | \
18360949b7bSClément Léger ((prev_val) & ~(GENMASK(((reg)->bit_width) - 1, 0) << (reg)->bit_offset))) \
1842f4a4d63SJarred White
show_feedback_ctrs(struct kobject * kobj,struct kobj_attribute * attr,char * buf)185158c998eSAshwin Chaugule static ssize_t show_feedback_ctrs(struct kobject *kobj,
1862bc6262cSNathan Chancellor struct kobj_attribute *attr, char *buf)
187158c998eSAshwin Chaugule {
188158c998eSAshwin Chaugule struct cpc_desc *cpc_ptr = to_cpc_desc(kobj);
189158c998eSAshwin Chaugule struct cppc_perf_fb_ctrs fb_ctrs = {0};
1902c74d847SPrakash, Prashanth int ret;
191158c998eSAshwin Chaugule
1922c74d847SPrakash, Prashanth ret = cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs);
1932c74d847SPrakash, Prashanth if (ret)
1942c74d847SPrakash, Prashanth return ret;
195158c998eSAshwin Chaugule
19692266c65Sye xingchen return sysfs_emit(buf, "ref:%llu del:%llu\n",
197158c998eSAshwin Chaugule fb_ctrs.reference, fb_ctrs.delivered);
198158c998eSAshwin Chaugule }
199158c998eSAshwin Chaugule define_one_cppc_ro(feedback_ctrs);
200158c998eSAshwin Chaugule
201158c998eSAshwin Chaugule static struct attribute *cppc_attrs[] = {
202158c998eSAshwin Chaugule &feedback_ctrs.attr,
203158c998eSAshwin Chaugule &reference_perf.attr,
204158c998eSAshwin Chaugule &wraparound_time.attr,
2052c74d847SPrakash, Prashanth &highest_perf.attr,
2062c74d847SPrakash, Prashanth &lowest_perf.attr,
2072c74d847SPrakash, Prashanth &lowest_nonlinear_perf.attr,
2088c6294ccSPetr Tesařík &guaranteed_perf.attr,
2092c74d847SPrakash, Prashanth &nominal_perf.attr,
2104773e77cSPrashanth Prakash &nominal_freq.attr,
2114773e77cSPrashanth Prakash &lowest_freq.attr,
212158c998eSAshwin Chaugule NULL
213158c998eSAshwin Chaugule };
21417f18417SGreg Kroah-Hartman ATTRIBUTE_GROUPS(cppc);
215158c998eSAshwin Chaugule
216a527b011SThomas Weißschuh static const struct kobj_type cppc_ktype = {
217158c998eSAshwin Chaugule .sysfs_ops = &kobj_sysfs_ops,
21817f18417SGreg Kroah-Hartman .default_groups = cppc_groups,
219158c998eSAshwin Chaugule };
220158c998eSAshwin Chaugule
check_pcc_chan(int pcc_ss_id,bool chk_err_bit)22185b1407bSGeorge Cherian static int check_pcc_chan(int pcc_ss_id, bool chk_err_bit)
222ad62e1e6SAshwin Chaugule {
22358e1c035SPrakash, Prashanth int ret, status;
22485b1407bSGeorge Cherian struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
22585b1407bSGeorge Cherian struct acpi_pcct_shared_memory __iomem *generic_comm_base =
22685b1407bSGeorge Cherian pcc_ss_data->pcc_comm_addr;
227ad62e1e6SAshwin Chaugule
22885b1407bSGeorge Cherian if (!pcc_ss_data->platform_owns_pcc)
229139aee73SPrakash, Prashanth return 0;
230139aee73SPrakash, Prashanth
231f387e5b9SPrakash, Prashanth /*
23258e1c035SPrakash, Prashanth * Poll PCC status register every 3us(delay_us) for maximum of
23358e1c035SPrakash, Prashanth * deadline_us(timeout_us) until PCC command complete bit is set(cond)
234f387e5b9SPrakash, Prashanth */
23558e1c035SPrakash, Prashanth ret = readw_relaxed_poll_timeout(&generic_comm_base->status, status,
23658e1c035SPrakash, Prashanth status & PCC_CMD_COMPLETE_MASK, 3,
23758e1c035SPrakash, Prashanth pcc_ss_data->deadline_us);
23858e1c035SPrakash, Prashanth
23958e1c035SPrakash, Prashanth if (likely(!ret)) {
24058e1c035SPrakash, Prashanth pcc_ss_data->platform_owns_pcc = false;
241139aee73SPrakash, Prashanth if (chk_err_bit && (status & PCC_ERROR_MASK))
242139aee73SPrakash, Prashanth ret = -EIO;
243ad62e1e6SAshwin Chaugule }
244ad62e1e6SAshwin Chaugule
24558e1c035SPrakash, Prashanth if (unlikely(ret))
24658e1c035SPrakash, Prashanth pr_err("PCC check channel failed for ss: %d. ret=%d\n",
24758e1c035SPrakash, Prashanth pcc_ss_id, ret);
248139aee73SPrakash, Prashanth
249ad62e1e6SAshwin Chaugule return ret;
250ad62e1e6SAshwin Chaugule }
251ad62e1e6SAshwin Chaugule
25280b8286aSPrakash, Prashanth /*
25380b8286aSPrakash, Prashanth * This function transfers the ownership of the PCC to the platform
25480b8286aSPrakash, Prashanth * So it must be called while holding write_lock(pcc_lock)
25580b8286aSPrakash, Prashanth */
send_pcc_cmd(int pcc_ss_id,u16 cmd)25685b1407bSGeorge Cherian static int send_pcc_cmd(int pcc_ss_id, u16 cmd)
257337aadffSAshwin Chaugule {
25880b8286aSPrakash, Prashanth int ret = -EIO, i;
25985b1407bSGeorge Cherian struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
2601d9b4abeSIonela Voinescu struct acpi_pcct_shared_memory __iomem *generic_comm_base =
2611d9b4abeSIonela Voinescu pcc_ss_data->pcc_comm_addr;
262f387e5b9SPrakash, Prashanth unsigned int time_delta;
263337aadffSAshwin Chaugule
264ad62e1e6SAshwin Chaugule /*
265ad62e1e6SAshwin Chaugule * For CMD_WRITE we know for a fact the caller should have checked
266ad62e1e6SAshwin Chaugule * the channel before writing to PCC space
267ad62e1e6SAshwin Chaugule */
268ad62e1e6SAshwin Chaugule if (cmd == CMD_READ) {
26980b8286aSPrakash, Prashanth /*
27080b8286aSPrakash, Prashanth * If there are pending cpc_writes, then we stole the channel
27180b8286aSPrakash, Prashanth * before write completion, so first send a WRITE command to
27280b8286aSPrakash, Prashanth * platform
27380b8286aSPrakash, Prashanth */
27485b1407bSGeorge Cherian if (pcc_ss_data->pending_pcc_write_cmd)
27585b1407bSGeorge Cherian send_pcc_cmd(pcc_ss_id, CMD_WRITE);
27680b8286aSPrakash, Prashanth
27785b1407bSGeorge Cherian ret = check_pcc_chan(pcc_ss_id, false);
278ad62e1e6SAshwin Chaugule if (ret)
27980b8286aSPrakash, Prashanth goto end;
28080b8286aSPrakash, Prashanth } else /* CMD_WRITE */
28185b1407bSGeorge Cherian pcc_ss_data->pending_pcc_write_cmd = FALSE;
282337aadffSAshwin Chaugule
283f387e5b9SPrakash, Prashanth /*
284f387e5b9SPrakash, Prashanth * Handle the Minimum Request Turnaround Time(MRTT)
285f387e5b9SPrakash, Prashanth * "The minimum amount of time that OSPM must wait after the completion
286f387e5b9SPrakash, Prashanth * of a command before issuing the next command, in microseconds"
287f387e5b9SPrakash, Prashanth */
28885b1407bSGeorge Cherian if (pcc_ss_data->pcc_mrtt) {
28985b1407bSGeorge Cherian time_delta = ktime_us_delta(ktime_get(),
29085b1407bSGeorge Cherian pcc_ss_data->last_cmd_cmpl_time);
29185b1407bSGeorge Cherian if (pcc_ss_data->pcc_mrtt > time_delta)
29285b1407bSGeorge Cherian udelay(pcc_ss_data->pcc_mrtt - time_delta);
293f387e5b9SPrakash, Prashanth }
294f387e5b9SPrakash, Prashanth
295f387e5b9SPrakash, Prashanth /*
296f387e5b9SPrakash, Prashanth * Handle the non-zero Maximum Periodic Access Rate(MPAR)
297f387e5b9SPrakash, Prashanth * "The maximum number of periodic requests that the subspace channel can
298f387e5b9SPrakash, Prashanth * support, reported in commands per minute. 0 indicates no limitation."
299f387e5b9SPrakash, Prashanth *
300f387e5b9SPrakash, Prashanth * This parameter should be ideally zero or large enough so that it can
301f387e5b9SPrakash, Prashanth * handle maximum number of requests that all the cores in the system can
302f387e5b9SPrakash, Prashanth * collectively generate. If it is not, we will follow the spec and just
303f387e5b9SPrakash, Prashanth * not send the request to the platform after hitting the MPAR limit in
304f387e5b9SPrakash, Prashanth * any 60s window
305f387e5b9SPrakash, Prashanth */
30685b1407bSGeorge Cherian if (pcc_ss_data->pcc_mpar) {
30785b1407bSGeorge Cherian if (pcc_ss_data->mpar_count == 0) {
30885b1407bSGeorge Cherian time_delta = ktime_ms_delta(ktime_get(),
30985b1407bSGeorge Cherian pcc_ss_data->last_mpar_reset);
31085b1407bSGeorge Cherian if ((time_delta < 60 * MSEC_PER_SEC) && pcc_ss_data->last_mpar_reset) {
311d29abc83SGeorge Cherian pr_debug("PCC cmd for subspace %d not sent due to MPAR limit",
312d29abc83SGeorge Cherian pcc_ss_id);
31380b8286aSPrakash, Prashanth ret = -EIO;
31480b8286aSPrakash, Prashanth goto end;
315f387e5b9SPrakash, Prashanth }
31685b1407bSGeorge Cherian pcc_ss_data->last_mpar_reset = ktime_get();
31785b1407bSGeorge Cherian pcc_ss_data->mpar_count = pcc_ss_data->pcc_mpar;
318f387e5b9SPrakash, Prashanth }
31985b1407bSGeorge Cherian pcc_ss_data->mpar_count--;
320f387e5b9SPrakash, Prashanth }
321f387e5b9SPrakash, Prashanth
322337aadffSAshwin Chaugule /* Write to the shared comm region. */
323beee23aeSPrakash, Prashanth writew_relaxed(cmd, &generic_comm_base->command);
324337aadffSAshwin Chaugule
325337aadffSAshwin Chaugule /* Flip CMD COMPLETE bit */
326beee23aeSPrakash, Prashanth writew_relaxed(0, &generic_comm_base->status);
327337aadffSAshwin Chaugule
32885b1407bSGeorge Cherian pcc_ss_data->platform_owns_pcc = true;
329139aee73SPrakash, Prashanth
330337aadffSAshwin Chaugule /* Ring doorbell */
3317b6da7feSSudeep Holla ret = mbox_send_message(pcc_ss_data->pcc_channel->mchan, &cmd);
332ad62e1e6SAshwin Chaugule if (ret < 0) {
333d29abc83SGeorge Cherian pr_err("Err sending PCC mbox message. ss: %d cmd:%d, ret:%d\n",
334d29abc83SGeorge Cherian pcc_ss_id, cmd, ret);
33580b8286aSPrakash, Prashanth goto end;
336337aadffSAshwin Chaugule }
337337aadffSAshwin Chaugule
3389e12eb82SJulia Lawall /* wait for completion and check for PCC error bit */
33985b1407bSGeorge Cherian ret = check_pcc_chan(pcc_ss_id, true);
340139aee73SPrakash, Prashanth
34185b1407bSGeorge Cherian if (pcc_ss_data->pcc_mrtt)
34285b1407bSGeorge Cherian pcc_ss_data->last_cmd_cmpl_time = ktime_get();
343337aadffSAshwin Chaugule
3447b6da7feSSudeep Holla if (pcc_ss_data->pcc_channel->mchan->mbox->txdone_irq)
3457b6da7feSSudeep Holla mbox_chan_txdone(pcc_ss_data->pcc_channel->mchan, ret);
346b59c4b3dSHoan Tran else
3477b6da7feSSudeep Holla mbox_client_txdone(pcc_ss_data->pcc_channel->mchan, ret);
34880b8286aSPrakash, Prashanth
34980b8286aSPrakash, Prashanth end:
35080b8286aSPrakash, Prashanth if (cmd == CMD_WRITE) {
35180b8286aSPrakash, Prashanth if (unlikely(ret)) {
35280b8286aSPrakash, Prashanth for_each_possible_cpu(i) {
35380b8286aSPrakash, Prashanth struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i);
354e69ae675SXiaofei Tan
35580b8286aSPrakash, Prashanth if (!desc)
35680b8286aSPrakash, Prashanth continue;
35780b8286aSPrakash, Prashanth
35885b1407bSGeorge Cherian if (desc->write_cmd_id == pcc_ss_data->pcc_write_cnt)
35980b8286aSPrakash, Prashanth desc->write_cmd_status = ret;
36080b8286aSPrakash, Prashanth }
36180b8286aSPrakash, Prashanth }
36285b1407bSGeorge Cherian pcc_ss_data->pcc_write_cnt++;
36385b1407bSGeorge Cherian wake_up_all(&pcc_ss_data->pcc_write_wait_q);
36480b8286aSPrakash, Prashanth }
36580b8286aSPrakash, Prashanth
366ad62e1e6SAshwin Chaugule return ret;
367337aadffSAshwin Chaugule }
368337aadffSAshwin Chaugule
cppc_chan_tx_done(struct mbox_client * cl,void * msg,int ret)369337aadffSAshwin Chaugule static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
370337aadffSAshwin Chaugule {
371ad62e1e6SAshwin Chaugule if (ret < 0)
372337aadffSAshwin Chaugule pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
373337aadffSAshwin Chaugule *(u16 *)msg, ret);
374337aadffSAshwin Chaugule else
375337aadffSAshwin Chaugule pr_debug("TX completed. CMD sent:%x, ret:%d\n",
376337aadffSAshwin Chaugule *(u16 *)msg, ret);
377337aadffSAshwin Chaugule }
378337aadffSAshwin Chaugule
3795c447c18SZou Wei static struct mbox_client cppc_mbox_cl = {
380337aadffSAshwin Chaugule .tx_done = cppc_chan_tx_done,
381337aadffSAshwin Chaugule .knows_txdone = true,
382337aadffSAshwin Chaugule };
383337aadffSAshwin Chaugule
acpi_get_psd(struct cpc_desc * cpc_ptr,acpi_handle handle)384337aadffSAshwin Chaugule static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
385337aadffSAshwin Chaugule {
386337aadffSAshwin Chaugule int result = -EFAULT;
387337aadffSAshwin Chaugule acpi_status status = AE_OK;
388337aadffSAshwin Chaugule struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
389337aadffSAshwin Chaugule struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
390337aadffSAshwin Chaugule struct acpi_buffer state = {0, NULL};
391337aadffSAshwin Chaugule union acpi_object *psd = NULL;
392337aadffSAshwin Chaugule struct acpi_psd_package *pdomain;
393337aadffSAshwin Chaugule
3944c4cdc4cSAl Stone status = acpi_evaluate_object_typed(handle, "_PSD", NULL,
3954c4cdc4cSAl Stone &buffer, ACPI_TYPE_PACKAGE);
3964c4cdc4cSAl Stone if (status == AE_NOT_FOUND) /* _PSD is optional */
3974c4cdc4cSAl Stone return 0;
398337aadffSAshwin Chaugule if (ACPI_FAILURE(status))
399337aadffSAshwin Chaugule return -ENODEV;
400337aadffSAshwin Chaugule
401337aadffSAshwin Chaugule psd = buffer.pointer;
402337aadffSAshwin Chaugule if (!psd || psd->package.count != 1) {
403337aadffSAshwin Chaugule pr_debug("Invalid _PSD data\n");
404337aadffSAshwin Chaugule goto end;
405337aadffSAshwin Chaugule }
406337aadffSAshwin Chaugule
407337aadffSAshwin Chaugule pdomain = &(cpc_ptr->domain_info);
408337aadffSAshwin Chaugule
409337aadffSAshwin Chaugule state.length = sizeof(struct acpi_psd_package);
410337aadffSAshwin Chaugule state.pointer = pdomain;
411337aadffSAshwin Chaugule
412337aadffSAshwin Chaugule status = acpi_extract_package(&(psd->package.elements[0]),
413337aadffSAshwin Chaugule &format, &state);
414337aadffSAshwin Chaugule if (ACPI_FAILURE(status)) {
415337aadffSAshwin Chaugule pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
416337aadffSAshwin Chaugule goto end;
417337aadffSAshwin Chaugule }
418337aadffSAshwin Chaugule
419337aadffSAshwin Chaugule if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
420337aadffSAshwin Chaugule pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
421337aadffSAshwin Chaugule goto end;
422337aadffSAshwin Chaugule }
423337aadffSAshwin Chaugule
424337aadffSAshwin Chaugule if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
425337aadffSAshwin Chaugule pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
426337aadffSAshwin Chaugule goto end;
427337aadffSAshwin Chaugule }
428337aadffSAshwin Chaugule
429337aadffSAshwin Chaugule if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
430337aadffSAshwin Chaugule pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
431337aadffSAshwin Chaugule pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
432337aadffSAshwin Chaugule pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
433337aadffSAshwin Chaugule goto end;
434337aadffSAshwin Chaugule }
435337aadffSAshwin Chaugule
436337aadffSAshwin Chaugule result = 0;
437337aadffSAshwin Chaugule end:
438337aadffSAshwin Chaugule kfree(buffer.pointer);
439337aadffSAshwin Chaugule return result;
440337aadffSAshwin Chaugule }
441337aadffSAshwin Chaugule
acpi_cpc_valid(void)442a28b2bfcSIonela Voinescu bool acpi_cpc_valid(void)
443a28b2bfcSIonela Voinescu {
444a28b2bfcSIonela Voinescu struct cpc_desc *cpc_ptr;
445a28b2bfcSIonela Voinescu int cpu;
446a28b2bfcSIonela Voinescu
447a2a9d185SPerry Yuan if (acpi_disabled)
448a2a9d185SPerry Yuan return false;
449a2a9d185SPerry Yuan
4502aeca6bdSMario Limonciello for_each_present_cpu(cpu) {
451a28b2bfcSIonela Voinescu cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
452a28b2bfcSIonela Voinescu if (!cpc_ptr)
453a28b2bfcSIonela Voinescu return false;
454a28b2bfcSIonela Voinescu }
455a28b2bfcSIonela Voinescu
456a28b2bfcSIonela Voinescu return true;
457a28b2bfcSIonela Voinescu }
458a28b2bfcSIonela Voinescu EXPORT_SYMBOL_GPL(acpi_cpc_valid);
459a28b2bfcSIonela Voinescu
cppc_allow_fast_switch(void)4603cc30dd0SPierre Gondois bool cppc_allow_fast_switch(void)
4613cc30dd0SPierre Gondois {
4623cc30dd0SPierre Gondois struct cpc_register_resource *desired_reg;
4633cc30dd0SPierre Gondois struct cpc_desc *cpc_ptr;
4643cc30dd0SPierre Gondois int cpu;
4653cc30dd0SPierre Gondois
4663cc30dd0SPierre Gondois for_each_possible_cpu(cpu) {
4673cc30dd0SPierre Gondois cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
4683cc30dd0SPierre Gondois desired_reg = &cpc_ptr->cpc_regs[DESIRED_PERF];
4693cc30dd0SPierre Gondois if (!CPC_IN_SYSTEM_MEMORY(desired_reg) &&
4703cc30dd0SPierre Gondois !CPC_IN_SYSTEM_IO(desired_reg))
4713cc30dd0SPierre Gondois return false;
4723cc30dd0SPierre Gondois }
4733cc30dd0SPierre Gondois
4743cc30dd0SPierre Gondois return true;
4753cc30dd0SPierre Gondois }
4763cc30dd0SPierre Gondois EXPORT_SYMBOL_GPL(cppc_allow_fast_switch);
4773cc30dd0SPierre Gondois
478337aadffSAshwin Chaugule /**
479a28b2bfcSIonela Voinescu * acpi_get_psd_map - Map the CPUs in the freq domain of a given cpu
480a28b2bfcSIonela Voinescu * @cpu: Find all CPUs that share a domain with cpu.
481a28b2bfcSIonela Voinescu * @cpu_data: Pointer to CPU specific CPPC data including PSD info.
482337aadffSAshwin Chaugule *
483337aadffSAshwin Chaugule * Return: 0 for success or negative value for err.
484337aadffSAshwin Chaugule */
acpi_get_psd_map(unsigned int cpu,struct cppc_cpudata * cpu_data)485a28b2bfcSIonela Voinescu int acpi_get_psd_map(unsigned int cpu, struct cppc_cpudata *cpu_data)
486337aadffSAshwin Chaugule {
487337aadffSAshwin Chaugule struct cpc_desc *cpc_ptr, *match_cpc_ptr;
488a28b2bfcSIonela Voinescu struct acpi_psd_package *match_pdomain;
489a28b2bfcSIonela Voinescu struct acpi_psd_package *pdomain;
490a28b2bfcSIonela Voinescu int count_target, i;
491337aadffSAshwin Chaugule
492337aadffSAshwin Chaugule /*
493603fadf3SBjorn Helgaas * Now that we have _PSD data from all CPUs, let's setup P-state
494337aadffSAshwin Chaugule * domain info.
495337aadffSAshwin Chaugule */
496a28b2bfcSIonela Voinescu cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
497a28b2bfcSIonela Voinescu if (!cpc_ptr)
498a28b2bfcSIonela Voinescu return -EFAULT;
499337aadffSAshwin Chaugule
500337aadffSAshwin Chaugule pdomain = &(cpc_ptr->domain_info);
501a28b2bfcSIonela Voinescu cpumask_set_cpu(cpu, cpu_data->shared_cpu_map);
502337aadffSAshwin Chaugule if (pdomain->num_processors <= 1)
503a28b2bfcSIonela Voinescu return 0;
504337aadffSAshwin Chaugule
505337aadffSAshwin Chaugule /* Validate the Domain info */
506337aadffSAshwin Chaugule count_target = pdomain->num_processors;
507337aadffSAshwin Chaugule if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
508a28b2bfcSIonela Voinescu cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ALL;
509337aadffSAshwin Chaugule else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
510a28b2bfcSIonela Voinescu cpu_data->shared_type = CPUFREQ_SHARED_TYPE_HW;
511337aadffSAshwin Chaugule else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
512a28b2bfcSIonela Voinescu cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ANY;
513337aadffSAshwin Chaugule
514a28b2bfcSIonela Voinescu for_each_possible_cpu(i) {
515a28b2bfcSIonela Voinescu if (i == cpu)
516337aadffSAshwin Chaugule continue;
517337aadffSAshwin Chaugule
518a28b2bfcSIonela Voinescu match_cpc_ptr = per_cpu(cpc_desc_ptr, i);
519a28b2bfcSIonela Voinescu if (!match_cpc_ptr)
520a28b2bfcSIonela Voinescu goto err_fault;
521337aadffSAshwin Chaugule
522337aadffSAshwin Chaugule match_pdomain = &(match_cpc_ptr->domain_info);
523337aadffSAshwin Chaugule if (match_pdomain->domain != pdomain->domain)
524337aadffSAshwin Chaugule continue;
525337aadffSAshwin Chaugule
526a28b2bfcSIonela Voinescu /* Here i and cpu are in the same domain */
527a28b2bfcSIonela Voinescu if (match_pdomain->num_processors != count_target)
528a28b2bfcSIonela Voinescu goto err_fault;
529a28b2bfcSIonela Voinescu
530a28b2bfcSIonela Voinescu if (pdomain->coord_type != match_pdomain->coord_type)
531a28b2bfcSIonela Voinescu goto err_fault;
532a28b2bfcSIonela Voinescu
533a28b2bfcSIonela Voinescu cpumask_set_cpu(i, cpu_data->shared_cpu_map);
534337aadffSAshwin Chaugule }
535337aadffSAshwin Chaugule
536a28b2bfcSIonela Voinescu return 0;
537337aadffSAshwin Chaugule
538a28b2bfcSIonela Voinescu err_fault:
539337aadffSAshwin Chaugule /* Assume no coordination on any error parsing domain info */
540a28b2bfcSIonela Voinescu cpumask_clear(cpu_data->shared_cpu_map);
541a28b2bfcSIonela Voinescu cpumask_set_cpu(cpu, cpu_data->shared_cpu_map);
542a28b2bfcSIonela Voinescu cpu_data->shared_type = CPUFREQ_SHARED_TYPE_NONE;
543a28b2bfcSIonela Voinescu
544a28b2bfcSIonela Voinescu return -EFAULT;
545337aadffSAshwin Chaugule }
546337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(acpi_get_psd_map);
547337aadffSAshwin Chaugule
register_pcc_channel(int pcc_ss_idx)54885b1407bSGeorge Cherian static int register_pcc_channel(int pcc_ss_idx)
549337aadffSAshwin Chaugule {
5507b6da7feSSudeep Holla struct pcc_mbox_chan *pcc_chan;
551ad62e1e6SAshwin Chaugule u64 usecs_lat;
552337aadffSAshwin Chaugule
55385b1407bSGeorge Cherian if (pcc_ss_idx >= 0) {
5547b6da7feSSudeep Holla pcc_chan = pcc_mbox_request_channel(&cppc_mbox_cl, pcc_ss_idx);
555337aadffSAshwin Chaugule
5567b6da7feSSudeep Holla if (IS_ERR(pcc_chan)) {
557d29abc83SGeorge Cherian pr_err("Failed to find PCC channel for subspace %d\n",
558d29abc83SGeorge Cherian pcc_ss_idx);
559337aadffSAshwin Chaugule return -ENODEV;
560337aadffSAshwin Chaugule }
561337aadffSAshwin Chaugule
5627b6da7feSSudeep Holla pcc_data[pcc_ss_idx]->pcc_channel = pcc_chan;
563ad62e1e6SAshwin Chaugule /*
564ad62e1e6SAshwin Chaugule * cppc_ss->latency is just a Nominal value. In reality
565ad62e1e6SAshwin Chaugule * the remote processor could be much slower to reply.
566ad62e1e6SAshwin Chaugule * So add an arbitrary amount of wait on top of Nominal.
567ad62e1e6SAshwin Chaugule */
5687b6da7feSSudeep Holla usecs_lat = NUM_RETRIES * pcc_chan->latency;
56958e1c035SPrakash, Prashanth pcc_data[pcc_ss_idx]->deadline_us = usecs_lat;
5707b6da7feSSudeep Holla pcc_data[pcc_ss_idx]->pcc_mrtt = pcc_chan->min_turnaround_time;
5717b6da7feSSudeep Holla pcc_data[pcc_ss_idx]->pcc_mpar = pcc_chan->max_access_rate;
5727b6da7feSSudeep Holla pcc_data[pcc_ss_idx]->pcc_nominal = pcc_chan->latency;
573337aadffSAshwin Chaugule
57485b1407bSGeorge Cherian pcc_data[pcc_ss_idx]->pcc_comm_addr =
5757b6da7feSSudeep Holla acpi_os_ioremap(pcc_chan->shmem_base_addr,
5767b6da7feSSudeep Holla pcc_chan->shmem_size);
57785b1407bSGeorge Cherian if (!pcc_data[pcc_ss_idx]->pcc_comm_addr) {
578d29abc83SGeorge Cherian pr_err("Failed to ioremap PCC comm region mem for %d\n",
579d29abc83SGeorge Cherian pcc_ss_idx);
580337aadffSAshwin Chaugule return -ENOMEM;
581337aadffSAshwin Chaugule }
582337aadffSAshwin Chaugule
583603fadf3SBjorn Helgaas /* Set flag so that we don't come here for each CPU. */
58485b1407bSGeorge Cherian pcc_data[pcc_ss_idx]->pcc_channel_acquired = true;
585337aadffSAshwin Chaugule }
586337aadffSAshwin Chaugule
587337aadffSAshwin Chaugule return 0;
588337aadffSAshwin Chaugule }
589337aadffSAshwin Chaugule
590a6cbcdd5SSrinivas Pandruvada /**
591a6cbcdd5SSrinivas Pandruvada * cpc_ffh_supported() - check if FFH reading supported
592a6cbcdd5SSrinivas Pandruvada *
593a6cbcdd5SSrinivas Pandruvada * Check if the architecture has support for functional fixed hardware
594a6cbcdd5SSrinivas Pandruvada * read/write capability.
595a6cbcdd5SSrinivas Pandruvada *
596a6cbcdd5SSrinivas Pandruvada * Return: true for supported, false for not supported
597a6cbcdd5SSrinivas Pandruvada */
cpc_ffh_supported(void)598a6cbcdd5SSrinivas Pandruvada bool __weak cpc_ffh_supported(void)
599a6cbcdd5SSrinivas Pandruvada {
600a6cbcdd5SSrinivas Pandruvada return false;
601a6cbcdd5SSrinivas Pandruvada }
602a6cbcdd5SSrinivas Pandruvada
60385b1407bSGeorge Cherian /**
6048b356e53SMario Limonciello * cpc_supported_by_cpu() - check if CPPC is supported by CPU
6058b356e53SMario Limonciello *
6068b356e53SMario Limonciello * Check if the architectural support for CPPC is present even
6078b356e53SMario Limonciello * if the _OSC hasn't prescribed it
6088b356e53SMario Limonciello *
6098b356e53SMario Limonciello * Return: true for supported, false for not supported
6108b356e53SMario Limonciello */
cpc_supported_by_cpu(void)6118b356e53SMario Limonciello bool __weak cpc_supported_by_cpu(void)
6128b356e53SMario Limonciello {
6138b356e53SMario Limonciello return false;
6148b356e53SMario Limonciello }
6158b356e53SMario Limonciello
6168b356e53SMario Limonciello /**
61785b1407bSGeorge Cherian * pcc_data_alloc() - Allocate the pcc_data memory for pcc subspace
618fda7be20SYang Li * @pcc_ss_id: PCC Subspace index as in the PCC client ACPI package.
61985b1407bSGeorge Cherian *
62085b1407bSGeorge Cherian * Check and allocate the cppc_pcc_data memory.
62185b1407bSGeorge Cherian * In some processor configurations it is possible that same subspace
622603fadf3SBjorn Helgaas * is shared between multiple CPUs. This is seen especially in CPUs
62385b1407bSGeorge Cherian * with hardware multi-threading support.
62485b1407bSGeorge Cherian *
62585b1407bSGeorge Cherian * Return: 0 for success, errno for failure
62685b1407bSGeorge Cherian */
pcc_data_alloc(int pcc_ss_id)6275c447c18SZou Wei static int pcc_data_alloc(int pcc_ss_id)
62885b1407bSGeorge Cherian {
62985b1407bSGeorge Cherian if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES)
63085b1407bSGeorge Cherian return -EINVAL;
63185b1407bSGeorge Cherian
63285b1407bSGeorge Cherian if (pcc_data[pcc_ss_id]) {
63385b1407bSGeorge Cherian pcc_data[pcc_ss_id]->refcount++;
63485b1407bSGeorge Cherian } else {
63585b1407bSGeorge Cherian pcc_data[pcc_ss_id] = kzalloc(sizeof(struct cppc_pcc_data),
63685b1407bSGeorge Cherian GFP_KERNEL);
63785b1407bSGeorge Cherian if (!pcc_data[pcc_ss_id])
63885b1407bSGeorge Cherian return -ENOMEM;
63985b1407bSGeorge Cherian pcc_data[pcc_ss_id]->refcount++;
64085b1407bSGeorge Cherian }
64185b1407bSGeorge Cherian
64285b1407bSGeorge Cherian return 0;
64385b1407bSGeorge Cherian }
6444773e77cSPrashanth Prakash
645337aadffSAshwin Chaugule /*
646337aadffSAshwin Chaugule * An example CPC table looks like the following.
647337aadffSAshwin Chaugule *
6481a901c91SAndy Shevchenko * Name (_CPC, Package() {
6491a901c91SAndy Shevchenko * 17, // NumEntries
6501a901c91SAndy Shevchenko * 1, // Revision
6511a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x120, 2)}, // Highest Performance
6521a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x124, 2)}, // Nominal Performance
6531a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x128, 2)}, // Lowest Nonlinear Performance
6541a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x12C, 2)}, // Lowest Performance
6551a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x130, 2)}, // Guaranteed Performance Register
6561a901c91SAndy Shevchenko * ResourceTemplate() {Register(PCC, 32, 0, 0x110, 2)}, // Desired Performance Register
657337aadffSAshwin Chaugule * ResourceTemplate() {Register(SystemMemory, 0, 0, 0, 0)},
6581a901c91SAndy Shevchenko * ...
6591a901c91SAndy Shevchenko * ...
6601a901c91SAndy Shevchenko * ...
661337aadffSAshwin Chaugule * }
662337aadffSAshwin Chaugule * Each Register() encodes how to access that specific register.
663337aadffSAshwin Chaugule * e.g. a sample PCC entry has the following encoding:
664337aadffSAshwin Chaugule *
665337aadffSAshwin Chaugule * Register (
6661a901c91SAndy Shevchenko * PCC, // AddressSpaceKeyword
6671a901c91SAndy Shevchenko * 8, // RegisterBitWidth
6681a901c91SAndy Shevchenko * 8, // RegisterBitOffset
6691a901c91SAndy Shevchenko * 0x30, // RegisterAddress
6701a901c91SAndy Shevchenko * 9, // AccessSize (subspace ID)
671337aadffSAshwin Chaugule * )
672337aadffSAshwin Chaugule */
673337aadffSAshwin Chaugule
674337aadffSAshwin Chaugule /**
675337aadffSAshwin Chaugule * acpi_cppc_processor_probe - Search for per CPU _CPC objects.
676603fadf3SBjorn Helgaas * @pr: Ptr to acpi_processor containing this CPU's logical ID.
677337aadffSAshwin Chaugule *
678337aadffSAshwin Chaugule * Return: 0 for success or negative value for err.
679337aadffSAshwin Chaugule */
acpi_cppc_processor_probe(struct acpi_processor * pr)680337aadffSAshwin Chaugule int acpi_cppc_processor_probe(struct acpi_processor *pr)
681337aadffSAshwin Chaugule {
682337aadffSAshwin Chaugule struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
683337aadffSAshwin Chaugule union acpi_object *out_obj, *cpc_obj;
684337aadffSAshwin Chaugule struct cpc_desc *cpc_ptr;
685337aadffSAshwin Chaugule struct cpc_reg *gas_t;
686158c998eSAshwin Chaugule struct device *cpu_dev;
687337aadffSAshwin Chaugule acpi_handle handle = pr->handle;
688337aadffSAshwin Chaugule unsigned int num_ent, i, cpc_rev;
68985b1407bSGeorge Cherian int pcc_subspace_id = -1;
690337aadffSAshwin Chaugule acpi_status status;
691f21a3509SRafael J. Wysocki int ret = -ENODATA;
692337aadffSAshwin Chaugule
6937feec743SMario Limonciello if (!osc_sb_cppc2_support_acked) {
6947feec743SMario Limonciello pr_debug("CPPC v2 _OSC not acked\n");
6955f8f9bc4SPerry Yuan if (!cpc_supported_by_cpu()) {
6965f8f9bc4SPerry Yuan pr_debug("CPPC is not supported by the CPU\n");
697c42fa24bSRafael J. Wysocki return -ENODEV;
6987feec743SMario Limonciello }
6995f8f9bc4SPerry Yuan }
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;
7982f4a4d63SJarred 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
8062f4a4d63SJarred White access_width = GET_BIT_WIDTH(gas_t) / 8;
8072f4a4d63SJarred 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*1c10941eSPierre 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
904337aadffSAshwin Chaugule kfree(output.pointer);
905337aadffSAshwin Chaugule return 0;
906337aadffSAshwin Chaugule
907337aadffSAshwin Chaugule out_free:
9085bbb86aaSAshwin Chaugule /* Free all the mapped sys mem areas for this CPU */
9095bbb86aaSAshwin Chaugule for (i = 2; i < cpc_ptr->num_entries; i++) {
9105bbb86aaSAshwin Chaugule void __iomem *addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
9115bbb86aaSAshwin Chaugule
9125bbb86aaSAshwin Chaugule if (addr)
9135bbb86aaSAshwin Chaugule iounmap(addr);
9145bbb86aaSAshwin Chaugule }
915337aadffSAshwin Chaugule kfree(cpc_ptr);
916337aadffSAshwin Chaugule
917337aadffSAshwin Chaugule out_buf_free:
918337aadffSAshwin Chaugule kfree(output.pointer);
919337aadffSAshwin Chaugule return ret;
920337aadffSAshwin Chaugule }
921337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
922337aadffSAshwin Chaugule
923337aadffSAshwin Chaugule /**
924337aadffSAshwin Chaugule * acpi_cppc_processor_exit - Cleanup CPC structs.
925603fadf3SBjorn Helgaas * @pr: Ptr to acpi_processor containing this CPU's logical ID.
926337aadffSAshwin Chaugule *
927337aadffSAshwin Chaugule * Return: Void
928337aadffSAshwin Chaugule */
acpi_cppc_processor_exit(struct acpi_processor * pr)929337aadffSAshwin Chaugule void acpi_cppc_processor_exit(struct acpi_processor *pr)
930337aadffSAshwin Chaugule {
931337aadffSAshwin Chaugule struct cpc_desc *cpc_ptr;
9325bbb86aaSAshwin Chaugule unsigned int i;
9335bbb86aaSAshwin Chaugule void __iomem *addr;
93485b1407bSGeorge Cherian int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id);
93585b1407bSGeorge Cherian
93685b1407bSGeorge Cherian if (pcc_ss_id >= 0 && pcc_data[pcc_ss_id]) {
93785b1407bSGeorge Cherian if (pcc_data[pcc_ss_id]->pcc_channel_acquired) {
93885b1407bSGeorge Cherian pcc_data[pcc_ss_id]->refcount--;
93985b1407bSGeorge Cherian if (!pcc_data[pcc_ss_id]->refcount) {
94085b1407bSGeorge Cherian pcc_mbox_free_channel(pcc_data[pcc_ss_id]->pcc_channel);
94185b1407bSGeorge Cherian kfree(pcc_data[pcc_ss_id]);
94256a0b978SJohn Garry pcc_data[pcc_ss_id] = NULL;
94385b1407bSGeorge Cherian }
94485b1407bSGeorge Cherian }
94585b1407bSGeorge Cherian }
946158c998eSAshwin Chaugule
947337aadffSAshwin Chaugule cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
9489e9d68daSSebastian Andrzej Siewior if (!cpc_ptr)
9499e9d68daSSebastian Andrzej Siewior return;
9505bbb86aaSAshwin Chaugule
9515bbb86aaSAshwin Chaugule /* Free all the mapped sys mem areas for this CPU */
9525bbb86aaSAshwin Chaugule for (i = 2; i < cpc_ptr->num_entries; i++) {
9535bbb86aaSAshwin Chaugule addr = cpc_ptr->cpc_regs[i-2].sys_mem_vaddr;
9545bbb86aaSAshwin Chaugule if (addr)
9555bbb86aaSAshwin Chaugule iounmap(addr);
9565bbb86aaSAshwin Chaugule }
9575bbb86aaSAshwin Chaugule
958158c998eSAshwin Chaugule kobject_put(&cpc_ptr->kobj);
959337aadffSAshwin Chaugule kfree(cpc_ptr);
960337aadffSAshwin Chaugule }
961337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
962337aadffSAshwin Chaugule
963a6cbcdd5SSrinivas Pandruvada /**
964a6cbcdd5SSrinivas Pandruvada * cpc_read_ffh() - Read FFH register
965603fadf3SBjorn Helgaas * @cpunum: CPU number to read
966a6cbcdd5SSrinivas Pandruvada * @reg: cppc register information
967a6cbcdd5SSrinivas Pandruvada * @val: place holder for return value
968a6cbcdd5SSrinivas Pandruvada *
969a6cbcdd5SSrinivas Pandruvada * Read bit_width bits from a specified address and bit_offset
970a6cbcdd5SSrinivas Pandruvada *
971a6cbcdd5SSrinivas Pandruvada * Return: 0 for success and error code
972a6cbcdd5SSrinivas Pandruvada */
cpc_read_ffh(int cpunum,struct cpc_reg * reg,u64 * val)973a6cbcdd5SSrinivas Pandruvada int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
974a6cbcdd5SSrinivas Pandruvada {
975a6cbcdd5SSrinivas Pandruvada return -ENOTSUPP;
976a6cbcdd5SSrinivas Pandruvada }
977a6cbcdd5SSrinivas Pandruvada
978a6cbcdd5SSrinivas Pandruvada /**
979a6cbcdd5SSrinivas Pandruvada * cpc_write_ffh() - Write FFH register
980603fadf3SBjorn Helgaas * @cpunum: CPU number to write
981a6cbcdd5SSrinivas Pandruvada * @reg: cppc register information
982a6cbcdd5SSrinivas Pandruvada * @val: value to write
983a6cbcdd5SSrinivas Pandruvada *
984a6cbcdd5SSrinivas Pandruvada * Write value of bit_width bits to a specified address and bit_offset
985a6cbcdd5SSrinivas Pandruvada *
986a6cbcdd5SSrinivas Pandruvada * Return: 0 for success and error code
987a6cbcdd5SSrinivas Pandruvada */
cpc_write_ffh(int cpunum,struct cpc_reg * reg,u64 val)988a6cbcdd5SSrinivas Pandruvada int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
989a6cbcdd5SSrinivas Pandruvada {
990a6cbcdd5SSrinivas Pandruvada return -ENOTSUPP;
991a6cbcdd5SSrinivas Pandruvada }
992a6cbcdd5SSrinivas Pandruvada
99377e3d86fSPrakash, Prashanth /*
99477e3d86fSPrakash, Prashanth * Since cpc_read and cpc_write are called while holding pcc_lock, it should be
99577e3d86fSPrakash, Prashanth * as fast as possible. We have already mapped the PCC subspace during init, so
99677e3d86fSPrakash, Prashanth * we can directly write to it.
99777e3d86fSPrakash, Prashanth */
99877e3d86fSPrakash, Prashanth
cpc_read(int cpu,struct cpc_register_resource * reg_res,u64 * val)999a6cbcdd5SSrinivas Pandruvada static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val)
1000337aadffSAshwin Chaugule {
100126692cd9SIonela Voinescu void __iomem *vaddr = NULL;
10022f4a4d63SJarred White int size;
100385b1407bSGeorge Cherian int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
10045bbb86aaSAshwin Chaugule struct cpc_reg *reg = ®_res->cpc_entry.reg;
10055bbb86aaSAshwin Chaugule
10065bbb86aaSAshwin Chaugule if (reg_res->type == ACPI_TYPE_INTEGER) {
10075bbb86aaSAshwin Chaugule *val = reg_res->cpc_entry.int_value;
1008f684b107SRafael J. Wysocki return 0;
10095bbb86aaSAshwin Chaugule }
101077e3d86fSPrakash, Prashanth
101177e3d86fSPrakash, Prashanth *val = 0;
1012f489c948SVanshidhar Konda size = GET_BIT_WIDTH(reg);
1013a2c8f92bSSteven Noonan
1014a2c8f92bSSteven Noonan if (IS_ENABLED(CONFIG_HAS_IOPORT) &&
10155f51c7ceSRafael J. Wysocki reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
1016a2c8f92bSSteven Noonan u32 val_u32;
1017a2c8f92bSSteven Noonan acpi_status status;
1018a2c8f92bSSteven Noonan
1019f489c948SVanshidhar Konda status = acpi_os_read_port((acpi_io_address)reg->address,
1020a2c8f92bSSteven Noonan &val_u32, size);
1021a2c8f92bSSteven Noonan if (ACPI_FAILURE(status)) {
1022a2c8f92bSSteven Noonan pr_debug("Error: Failed to read SystemIO port %llx\n",
1023a2c8f92bSSteven Noonan reg->address);
1024a2c8f92bSSteven Noonan return -EFAULT;
1025a2c8f92bSSteven Noonan }
10265f51c7ceSRafael J. Wysocki
1027a2c8f92bSSteven Noonan *val = val_u32;
1028f489c948SVanshidhar Konda return 0;
1029f489c948SVanshidhar Konda } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0) {
1030f489c948SVanshidhar Konda /*
1031f489c948SVanshidhar Konda * For registers in PCC space, the register size is determined
1032f489c948SVanshidhar Konda * by the bit width field; the access size is used to indicate
1033f489c948SVanshidhar Konda * the PCC subspace id.
1034f489c948SVanshidhar Konda */
103585b1407bSGeorge Cherian size = reg->bit_width;
1036f489c948SVanshidhar Konda vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
10375bbb86aaSAshwin Chaugule }
10385bbb86aaSAshwin Chaugule else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1039a6cbcdd5SSrinivas Pandruvada vaddr = reg_res->sys_mem_vaddr;
1040a6cbcdd5SSrinivas Pandruvada else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
10415bbb86aaSAshwin Chaugule return cpc_read_ffh(cpu, reg, val);
10425bbb86aaSAshwin Chaugule else
1043f489c948SVanshidhar Konda return acpi_os_read_memory((acpi_physical_address)reg->address,
10442f4a4d63SJarred White val, size);
10452f4a4d63SJarred White
104677e3d86fSPrakash, Prashanth switch (size) {
1047beee23aeSPrakash, Prashanth case 8:
104877e3d86fSPrakash, Prashanth *val = readb_relaxed(vaddr);
104977e3d86fSPrakash, Prashanth break;
1050beee23aeSPrakash, Prashanth case 16:
105177e3d86fSPrakash, Prashanth *val = readw_relaxed(vaddr);
105277e3d86fSPrakash, Prashanth break;
1053beee23aeSPrakash, Prashanth case 32:
105477e3d86fSPrakash, Prashanth *val = readl_relaxed(vaddr);
105577e3d86fSPrakash, Prashanth break;
1056beee23aeSPrakash, Prashanth case 64:
105777e3d86fSPrakash, Prashanth *val = readq_relaxed(vaddr);
105877e3d86fSPrakash, Prashanth break;
1059f489c948SVanshidhar Konda default:
1060f489c948SVanshidhar Konda if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
1061f489c948SVanshidhar Konda pr_debug("Error: Cannot read %u bit width from system memory: 0x%llx\n",
1062f489c948SVanshidhar Konda size, reg->address);
1063d29abc83SGeorge Cherian } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
1064f489c948SVanshidhar Konda pr_debug("Error: Cannot read %u bit width from PCC for ss: %d\n",
1065f489c948SVanshidhar Konda size, pcc_ss_id);
1066f684b107SRafael J. Wysocki }
106777e3d86fSPrakash, Prashanth return -EFAULT;
10685bbb86aaSAshwin Chaugule }
10692f4a4d63SJarred White
107060949b7bSClément Léger if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
10712f4a4d63SJarred White *val = MASK_VAL_READ(reg, *val);
1072f684b107SRafael J. Wysocki
1073337aadffSAshwin Chaugule return 0;
1074337aadffSAshwin Chaugule }
1075a6cbcdd5SSrinivas Pandruvada
cpc_write(int cpu,struct cpc_register_resource * reg_res,u64 val)1076337aadffSAshwin Chaugule static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)
107777e3d86fSPrakash, Prashanth {
10782f4a4d63SJarred White int ret_val = 0;
107960949b7bSClément Léger int size;
108026692cd9SIonela Voinescu u64 prev_val;
108185b1407bSGeorge Cherian void __iomem *vaddr = NULL;
10825bbb86aaSAshwin Chaugule int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
108360949b7bSClément Léger struct cpc_reg *reg = ®_res->cpc_entry.reg;
1084*1c10941eSPierre Gondois struct cpc_desc *cpc_desc;
1085337aadffSAshwin Chaugule unsigned long flags;
1086f489c948SVanshidhar Konda
1087f489c948SVanshidhar Konda size = GET_BIT_WIDTH(reg);
1088a2c8f92bSSteven Noonan
1089a2c8f92bSSteven Noonan if (IS_ENABLED(CONFIG_HAS_IOPORT) &&
1090a2c8f92bSSteven Noonan reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
1091a2c8f92bSSteven Noonan acpi_status status;
1092f489c948SVanshidhar Konda
1093a2c8f92bSSteven Noonan status = acpi_os_write_port((acpi_io_address)reg->address,
1094a2c8f92bSSteven Noonan (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 }
1100f489c948SVanshidhar Konda
1101f489c948SVanshidhar Konda return 0;
1102f489c948SVanshidhar Konda } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM && pcc_ss_id >= 0) {
1103f489c948SVanshidhar Konda /*
1104f489c948SVanshidhar Konda * For registers in PCC space, the register size is determined
1105f489c948SVanshidhar Konda * by the bit width field; the access size is used to indicate
1106f489c948SVanshidhar Konda * the PCC subspace id.
110785b1407bSGeorge Cherian */
1108f489c948SVanshidhar Konda size = reg->bit_width;
11095bbb86aaSAshwin Chaugule vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
11105bbb86aaSAshwin Chaugule }
1111a6cbcdd5SSrinivas Pandruvada else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
1112a6cbcdd5SSrinivas Pandruvada vaddr = reg_res->sys_mem_vaddr;
11135bbb86aaSAshwin Chaugule else if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
11145bbb86aaSAshwin Chaugule return cpc_write_ffh(cpu, reg, val);
1115f489c948SVanshidhar Konda else
11162f4a4d63SJarred White return acpi_os_write_memory((acpi_physical_address)reg->address,
111760949b7bSClément Léger val, size);
111860949b7bSClément Léger
111960949b7bSClément Léger if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
112060949b7bSClément Léger cpc_desc = per_cpu(cpc_desc_ptr, cpu);
112160949b7bSClément Léger if (!cpc_desc) {
112260949b7bSClément Léger pr_debug("No CPC descriptor for CPU:%d\n", cpu);
112360949b7bSClément Léger return -ENODEV;
1124*1c10941eSPierre Gondois }
112560949b7bSClément Léger
112660949b7bSClément Léger raw_spin_lock_irqsave(&cpc_desc->rmw_lock, flags);
112760949b7bSClément Léger switch (size) {
112860949b7bSClément Léger case 8:
112960949b7bSClément Léger prev_val = readb_relaxed(vaddr);
113060949b7bSClément Léger break;
113160949b7bSClément Léger case 16:
113260949b7bSClément Léger prev_val = readw_relaxed(vaddr);
113360949b7bSClément Léger break;
113460949b7bSClément Léger case 32:
113560949b7bSClément Léger prev_val = readl_relaxed(vaddr);
113660949b7bSClément Léger break;
113760949b7bSClément Léger case 64:
113860949b7bSClément Léger prev_val = readq_relaxed(vaddr);
1139*1c10941eSPierre Gondois break;
114060949b7bSClément Léger default:
114160949b7bSClément Léger raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, flags);
114260949b7bSClément Léger return -EFAULT;
114360949b7bSClément Léger }
11442f4a4d63SJarred White val = MASK_VAL_WRITE(reg, prev_val, val);
11452f4a4d63SJarred White }
114677e3d86fSPrakash, Prashanth
1147beee23aeSPrakash, Prashanth switch (size) {
114877e3d86fSPrakash, Prashanth case 8:
114977e3d86fSPrakash, Prashanth writeb_relaxed(val, vaddr);
1150beee23aeSPrakash, Prashanth break;
115177e3d86fSPrakash, Prashanth case 16:
115277e3d86fSPrakash, Prashanth writew_relaxed(val, vaddr);
1153beee23aeSPrakash, Prashanth break;
115477e3d86fSPrakash, Prashanth case 32:
115577e3d86fSPrakash, Prashanth writel_relaxed(val, vaddr);
1156beee23aeSPrakash, Prashanth break;
115777e3d86fSPrakash, Prashanth case 64:
115877e3d86fSPrakash, Prashanth writeq_relaxed(val, vaddr);
1159f489c948SVanshidhar Konda break;
1160f489c948SVanshidhar Konda default:
1161f489c948SVanshidhar Konda if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
1162f489c948SVanshidhar Konda pr_debug("Error: Cannot write %u bit width to system memory: 0x%llx\n",
1163d29abc83SGeorge Cherian size, reg->address);
1164f489c948SVanshidhar Konda } else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
1165f489c948SVanshidhar Konda pr_debug("Error: Cannot write %u bit width to PCC for ss: %d\n",
116677e3d86fSPrakash, Prashanth size, pcc_ss_id);
116777e3d86fSPrakash, Prashanth }
1168337aadffSAshwin Chaugule ret_val = -EFAULT;
11695bbb86aaSAshwin Chaugule break;
117060949b7bSClément Léger }
1171*1c10941eSPierre Gondois
117260949b7bSClément Léger if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
117377e3d86fSPrakash, Prashanth raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, flags);
1174337aadffSAshwin Chaugule
1175337aadffSAshwin Chaugule return ret_val;
11760654cf05SRafael J. Wysocki }
11771757d05fSXiongfeng Wang
cppc_get_perf(int cpunum,enum cppc_regs reg_idx,u64 * perf)11781757d05fSXiongfeng Wang static int cppc_get_perf(int cpunum, enum cppc_regs reg_idx, u64 *perf)
1179935dff30SRafael J. Wysocki {
1180935dff30SRafael J. Wysocki 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 }
11870654cf05SRafael J. Wysocki
11880654cf05SRafael J. Wysocki reg = &cpc_desc->cpc_regs[reg_idx];
11891757d05fSXiongfeng Wang
11901757d05fSXiongfeng Wang 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);
12010654cf05SRafael J. Wysocki
12021757d05fSXiongfeng Wang if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0)
12031757d05fSXiongfeng Wang 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;
12100654cf05SRafael J. Wysocki }
12111757d05fSXiongfeng Wang
12121757d05fSXiongfeng Wang cpc_read(cpunum, reg, perf);
12131757d05fSXiongfeng Wang
12140654cf05SRafael J. Wysocki return 0;
12150654cf05SRafael J. Wysocki }
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 {
12261757d05fSXiongfeng Wang return cppc_get_perf(cpunum, DESIRED_PERF, desired_perf);
12271757d05fSXiongfeng Wang }
12281757d05fSXiongfeng Wang EXPORT_SYMBOL_GPL(cppc_get_desired_perf);
12290654cf05SRafael J. Wysocki
12300654cf05SRafael J. Wysocki /**
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 }
124112753d71SMeng Li
124212753d71SMeng Li /**
124312753d71SMeng Li * cppc_get_highest_perf - Get the highest performance register value.
124412753d71SMeng Li * @cpunum: CPU from which to get highest performance.
124512753d71SMeng Li * @highest_perf: Return address.
124612753d71SMeng Li *
124712753d71SMeng Li * Return: 0 for success, -EIO otherwise.
124812753d71SMeng Li */
cppc_get_highest_perf(int cpunum,u64 * highest_perf)124912753d71SMeng Li int cppc_get_highest_perf(int cpunum, u64 *highest_perf)
125012753d71SMeng Li {
125112753d71SMeng Li return cppc_get_perf(cpunum, HIGHEST_PERF, highest_perf);
125212753d71SMeng Li }
125312753d71SMeng Li EXPORT_SYMBOL_GPL(cppc_get_highest_perf);
12547bc1fcd3SPerry Yuan
12557bc1fcd3SPerry Yuan /**
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);
1267603fadf3SBjorn Helgaas
1268337aadffSAshwin Chaugule /**
1269337aadffSAshwin Chaugule * 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)
1276368520a6SPrakash, Prashanth {
127729523f09SSrinivas Pandruvada struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
12784773e77cSPrashanth Prakash struct cpc_register_resource *highest_reg, *lowest_reg,
127929523f09SSrinivas Pandruvada *lowest_non_linear_reg, *nominal_reg, *guaranteed_reg,
128085b1407bSGeorge Cherian *low_freq_reg = NULL, *nom_freq_reg = NULL;
12816fa12d58SPrashanth Prakash u64 high, low, guaranteed, nom, min_nonlinear, low_f = 0, nom_f = 0;
1282850d64a4SPrakash, Prashanth int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
1283337aadffSAshwin Chaugule struct cppc_pcc_data *pcc_ss_data = NULL;
12846fa12d58SPrashanth Prakash int ret = 0, regs_in_pcc = 0;
1285337aadffSAshwin Chaugule
1286337aadffSAshwin Chaugule if (!cpc_desc) {
1287337aadffSAshwin Chaugule pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
1288337aadffSAshwin Chaugule return -ENODEV;
1289337aadffSAshwin Chaugule }
1290337aadffSAshwin Chaugule
1291368520a6SPrakash, Prashanth highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
1292368520a6SPrakash, Prashanth lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
12934773e77cSPrashanth Prakash lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF];
12944773e77cSPrashanth Prakash nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
129529523f09SSrinivas Pandruvada low_freq_reg = &cpc_desc->cpc_regs[LOWEST_FREQ];
1296337aadffSAshwin Chaugule nom_freq_reg = &cpc_desc->cpc_regs[NOMINAL_FREQ];
1297337aadffSAshwin Chaugule guaranteed_reg = &cpc_desc->cpc_regs[GUARANTEED_PERF];
129880b8286aSPrakash, Prashanth
12994773e77cSPrashanth Prakash /* Are any of the regs PCC ?*/
13004773e77cSPrashanth Prakash if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
13016fa12d58SPrashanth Prakash CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg) ||
13026fa12d58SPrashanth 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;
1306850d64a4SPrakash, Prashanth }
130785b1407bSGeorge Cherian pcc_ss_data = pcc_data[pcc_ss_id];
1308337aadffSAshwin Chaugule regs_in_pcc = 1;
130985b1407bSGeorge Cherian down_write(&pcc_ss_data->pcc_lock);
1310337aadffSAshwin Chaugule /* Ring doorbell once to update PCC subspace */
1311337aadffSAshwin Chaugule if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
1312337aadffSAshwin Chaugule ret = -EIO;
1313337aadffSAshwin Chaugule goto out_err;
1314337aadffSAshwin Chaugule }
1315a6cbcdd5SSrinivas Pandruvada }
1316337aadffSAshwin Chaugule
1317337aadffSAshwin Chaugule cpc_read(cpunum, highest_reg, &high);
1318a6cbcdd5SSrinivas Pandruvada perf_caps->highest_perf = high;
1319337aadffSAshwin Chaugule
1320337aadffSAshwin Chaugule cpc_read(cpunum, lowest_reg, &low);
1321368520a6SPrakash, Prashanth perf_caps->lowest_perf = low;
1322337aadffSAshwin Chaugule
1323337aadffSAshwin Chaugule cpc_read(cpunum, nominal_reg, &nom);
1324edef1ef1SSrinivas Pandruvada perf_caps->nominal_perf = nom;
1325edef1ef1SSrinivas Pandruvada
1326edef1ef1SSrinivas Pandruvada if (guaranteed_reg->type != ACPI_TYPE_BUFFER ||
1327edef1ef1SSrinivas Pandruvada IS_NULL_REG(&guaranteed_reg->cpc_entry.reg)) {
132829523f09SSrinivas Pandruvada perf_caps->guaranteed_perf = 0;
132929523f09SSrinivas Pandruvada } else {
1330edef1ef1SSrinivas Pandruvada cpc_read(cpunum, guaranteed_reg, &guaranteed);
133129523f09SSrinivas Pandruvada perf_caps->guaranteed_perf = guaranteed;
1332368520a6SPrakash, Prashanth }
1333368520a6SPrakash, Prashanth
1334368520a6SPrakash, Prashanth cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear);
1335368520a6SPrakash, Prashanth perf_caps->lowest_nonlinear_perf = min_nonlinear;
1336337aadffSAshwin Chaugule
1337337aadffSAshwin Chaugule if (!high || !low || !nom || !min_nonlinear)
13384773e77cSPrashanth Prakash ret = -EFAULT;
13394773e77cSPrashanth Prakash
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;
1349337aadffSAshwin Chaugule
1350850d64a4SPrakash, Prashanth
135185b1407bSGeorge Cherian out_err:
1352337aadffSAshwin Chaugule if (regs_in_pcc)
1353337aadffSAshwin Chaugule up_write(&pcc_ss_data->pcc_lock);
1354337aadffSAshwin Chaugule return ret;
1355337aadffSAshwin Chaugule }
1356337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
1357ae2df912SJeremy Linton
1358ae2df912SJeremy Linton /**
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);
1399603fadf3SBjorn Helgaas
1400337aadffSAshwin Chaugule /**
1401337aadffSAshwin Chaugule * 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)
1408158c998eSAshwin Chaugule {
1409158c998eSAshwin Chaugule struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
141085b1407bSGeorge Cherian struct cpc_register_resource *delivered_reg, *reference_reg,
14116fa12d58SPrashanth Prakash *ref_perf_reg, *ctr_wrap_reg;
1412158c998eSAshwin Chaugule int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
1413850d64a4SPrakash, Prashanth struct cppc_pcc_data *pcc_ss_data = NULL;
1414337aadffSAshwin Chaugule u64 delivered, reference, ref_perf, ctr_wrap_time;
14156fa12d58SPrashanth Prakash int ret = 0, regs_in_pcc = 0;
1416337aadffSAshwin Chaugule
1417337aadffSAshwin Chaugule if (!cpc_desc) {
1418337aadffSAshwin Chaugule pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
1419337aadffSAshwin Chaugule return -ENODEV;
1420337aadffSAshwin Chaugule }
1421337aadffSAshwin Chaugule
1422158c998eSAshwin Chaugule delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
1423158c998eSAshwin 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];
1426603fadf3SBjorn Helgaas
1427158c998eSAshwin Chaugule /*
1428158c998eSAshwin Chaugule * If reference perf register is not supported then we should
1429158c998eSAshwin Chaugule * use the nominal perf value
1430158c998eSAshwin Chaugule */
1431337aadffSAshwin Chaugule if (!CPC_SUPPORTED(ref_perf_reg))
1432337aadffSAshwin Chaugule ref_perf_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
1433158c998eSAshwin Chaugule
1434158c998eSAshwin Chaugule /* Are any of the regs PCC ?*/
14356fa12d58SPrashanth Prakash if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) ||
14366fa12d58SPrashanth Prakash 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;
144085b1407bSGeorge Cherian }
1441850d64a4SPrakash, Prashanth pcc_ss_data = pcc_data[pcc_ss_id];
1442337aadffSAshwin Chaugule down_write(&pcc_ss_data->pcc_lock);
144385b1407bSGeorge Cherian regs_in_pcc = 1;
1444337aadffSAshwin Chaugule /* Ring doorbell once to update PCC subspace */
1445337aadffSAshwin Chaugule if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
1446337aadffSAshwin Chaugule ret = -EIO;
1447337aadffSAshwin Chaugule goto out_err;
1448337aadffSAshwin Chaugule }
1449a6cbcdd5SSrinivas Pandruvada }
1450a6cbcdd5SSrinivas Pandruvada
1451a6cbcdd5SSrinivas Pandruvada cpc_read(cpunum, delivered_reg, &delivered);
1452337aadffSAshwin Chaugule cpc_read(cpunum, reference_reg, &reference);
1453158c998eSAshwin Chaugule cpc_read(cpunum, ref_perf_reg, &ref_perf);
1454158c998eSAshwin 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 */
1460a6cbcdd5SSrinivas Pandruvada ctr_wrap_time = (u64)(~((u64)0));
1461158c998eSAshwin Chaugule if (CPC_SUPPORTED(ctr_wrap_reg))
1462158c998eSAshwin Chaugule cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time);
1463337aadffSAshwin Chaugule
1464337aadffSAshwin Chaugule if (!delivered || !reference || !ref_perf) {
1465337aadffSAshwin Chaugule ret = -EFAULT;
1466337aadffSAshwin Chaugule goto out_err;
1467337aadffSAshwin Chaugule }
1468337aadffSAshwin Chaugule
1469158c998eSAshwin Chaugule perf_fb_ctrs->delivered = delivered;
14702c74d847SPrakash, Prashanth perf_fb_ctrs->reference = reference;
1471337aadffSAshwin Chaugule perf_fb_ctrs->reference_perf = ref_perf;
1472850d64a4SPrakash, Prashanth perf_fb_ctrs->wraparound_time = ctr_wrap_time;
147385b1407bSGeorge Cherian out_err:
1474337aadffSAshwin Chaugule if (regs_in_pcc)
1475337aadffSAshwin Chaugule up_write(&pcc_ss_data->pcc_lock);
1476337aadffSAshwin Chaugule return ret;
1477337aadffSAshwin Chaugule }
14787bc1fcd3SPerry Yuan EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
14797bc1fcd3SPerry Yuan
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 */
1523aaf21ac9SMario Limonciello ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
1524aaf21ac9SMario Limonciello up_write(&pcc_ss_data->pcc_lock);
1525aaf21ac9SMario Limonciello } else if (osc_cpc_flexible_adr_space_confirmed &&
15267bc1fcd3SPerry Yuan CPC_SUPPORTED(epp_set_reg) && CPC_IN_FFH(epp_set_reg)) {
15277bc1fcd3SPerry Yuan ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf);
1528aaf21ac9SMario Limonciello } else {
15297bc1fcd3SPerry Yuan ret = -ENOTSUPP;
15307bc1fcd3SPerry Yuan pr_debug("_CPC in PCC and _CPC in FFH are not supported\n");
15317bc1fcd3SPerry Yuan }
15327bc1fcd3SPerry Yuan
15337bc1fcd3SPerry Yuan return ret;
15347bc1fcd3SPerry Yuan }
1535337aadffSAshwin Chaugule EXPORT_SYMBOL_GPL(cppc_set_epp_perf);
1536c984f5d5SWyes Karny
1537c984f5d5SWyes Karny /**
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);
1632fb0b00afSJinzhou Su
1633fb0b00afSJinzhou Su /**
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);
1677603fadf3SBjorn Helgaas
1678337aadffSAshwin Chaugule /**
1679337aadffSAshwin Chaugule * 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)
168676531df5SWyes Karny {
168785b1407bSGeorge Cherian struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
16886fa12d58SPrashanth Prakash struct cpc_register_resource *desired_reg, *min_perf_reg, *max_perf_reg;
1689337aadffSAshwin Chaugule int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
1690337aadffSAshwin Chaugule struct cppc_pcc_data *pcc_ss_data = NULL;
16916fa12d58SPrashanth Prakash int ret = 0;
1692337aadffSAshwin Chaugule
1693337aadffSAshwin Chaugule if (!cpc_desc) {
1694337aadffSAshwin Chaugule pr_debug("No CPC descriptor for CPU:%d\n", cpu);
1695337aadffSAshwin Chaugule return -ENODEV;
1696337aadffSAshwin Chaugule }
169776531df5SWyes Karny
169876531df5SWyes Karny desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
1699337aadffSAshwin Chaugule min_perf_reg = &cpc_desc->cpc_regs[MIN_PERF];
170080b8286aSPrakash, Prashanth max_perf_reg = &cpc_desc->cpc_regs[MAX_PERF];
170180b8286aSPrakash, Prashanth
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
170776531df5SWyes Karny * achieve that goal here
17086fa12d58SPrashanth Prakash */
17096fa12d58SPrashanth Prakash 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;
171385b1407bSGeorge Cherian }
171485b1407bSGeorge Cherian pcc_ss_data = pcc_data[pcc_ss_id];
171585b1407bSGeorge Cherian down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */
171680b8286aSPrakash, Prashanth if (pcc_ss_data->platform_owns_pcc) {
171785b1407bSGeorge Cherian ret = check_pcc_chan(pcc_ss_id, false);
171880b8286aSPrakash, Prashanth if (ret) {
171980b8286aSPrakash, Prashanth up_read(&pcc_ss_data->pcc_lock);
172080b8286aSPrakash, Prashanth return ret;
1721139aee73SPrakash, Prashanth }
1722139aee73SPrakash, Prashanth }
1723139aee73SPrakash, Prashanth /*
1724139aee73SPrakash, Prashanth * Update the pending_write to make sure a PCC CMD_READ will not
172585b1407bSGeorge Cherian * arrive and steal the channel during the switch to write lock
172685b1407bSGeorge Cherian */
172780b8286aSPrakash, Prashanth pcc_ss_data->pending_pcc_write_cmd = true;
1728ad62e1e6SAshwin Chaugule cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt;
1729ad62e1e6SAshwin Chaugule cpc_desc->write_cmd_status = 0;
1730a6cbcdd5SSrinivas Pandruvada }
1731337aadffSAshwin Chaugule
173276531df5SWyes Karny cpc_write(cpu, desired_reg, perf_ctrls->desired_perf);
173376531df5SWyes Karny
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);
174385b1407bSGeorge Cherian
174480b8286aSPrakash, Prashanth if (CPC_IN_PCC(desired_reg) || CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg))
174580b8286aSPrakash, Prashanth 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.
1786935ab850STom Saeger * 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
178880b8286aSPrakash, Prashanth * 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
179076531df5SWyes Karny * the write command before servicing the read command
179185b1407bSGeorge Cherian */
179280b8286aSPrakash, Prashanth 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 */
179485b1407bSGeorge Cherian /* Update only if there are pending write commands */
179585b1407bSGeorge Cherian if (pcc_ss_data->pending_pcc_write_cmd)
179680b8286aSPrakash, Prashanth send_pcc_cmd(pcc_ss_id, CMD_WRITE);
179780b8286aSPrakash, Prashanth up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */
179885b1407bSGeorge Cherian } else
179985b1407bSGeorge Cherian /* Wait until pcc_write_cnt is updated by send_pcc_cmd */
180080b8286aSPrakash, Prashanth wait_event(pcc_ss_data->pcc_write_wait_q,
180180b8286aSPrakash, Prashanth cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt);
180280b8286aSPrakash, Prashanth
1803337aadffSAshwin Chaugule /* send_pcc_cmd updates the status in case of failure */
1804337aadffSAshwin Chaugule ret = cpc_desc->write_cmd_status;
1805337aadffSAshwin Chaugule }
1806337aadffSAshwin Chaugule return ret;
1807be8b88d7SPrakash, Prashanth }
1808be8b88d7SPrakash, Prashanth EXPORT_SYMBOL_GPL(cppc_set_perf);
1809be8b88d7SPrakash, Prashanth
1810fda7be20SYang Li /**
1811be8b88d7SPrakash, Prashanth * cppc_get_transition_latency - returns frequency transition latency in ns
1812935ab850STom Saeger * @cpu_num: CPU number for per_cpu().
1813935ab850STom Saeger *
1814be8b88d7SPrakash, Prashanth * ACPI CPPC does not explicitly specify how a platform can specify the
1815be8b88d7SPrakash, Prashanth * transition latency for performance change requests. The closest we have
18166380b7b2SPierre Gondois * is the timing information from the PCCT tables which provides the info
18176380b7b2SPierre Gondois * on the number and frequency of PCC commands the platform can handle.
18186380b7b2SPierre Gondois *
1819be8b88d7SPrakash, Prashanth * If desired_reg is in the SystemMemory or SystemIo ACPI address space,
1820be8b88d7SPrakash, Prashanth * 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;
183685b1407bSGeorge Cherian struct cpc_desc *cpc_desc;
18371ecbd717SGeorge Cherian struct cpc_register_resource *desired_reg;
1838be8b88d7SPrakash, Prashanth int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num);
1839be8b88d7SPrakash, Prashanth 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;
18446380b7b2SPierre Gondois
18456380b7b2SPierre Gondois desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
18466380b7b2SPierre Gondois if (CPC_IN_SYSTEM_MEMORY(desired_reg) || CPC_IN_SYSTEM_IO(desired_reg))
1847be8b88d7SPrakash, Prashanth return 0;
1848be8b88d7SPrakash, Prashanth else if (!CPC_IN_PCC(desired_reg))
18491ecbd717SGeorge Cherian return CPUFREQ_ETERNAL;
18501ecbd717SGeorge Cherian
18511ecbd717SGeorge Cherian if (pcc_ss_id < 0)
18521ecbd717SGeorge Cherian return CPUFREQ_ETERNAL;
185385b1407bSGeorge Cherian
185485b1407bSGeorge Cherian pcc_ss_data = pcc_data[pcc_ss_id];
1855be8b88d7SPrakash, Prashanth if (pcc_ss_data->pcc_mpar)
185685b1407bSGeorge Cherian latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar);
185785b1407bSGeorge Cherian
1858be8b88d7SPrakash, Prashanth latency_ns = max(latency_ns, pcc_ss_data->pcc_nominal * 1000);
1859be8b88d7SPrakash, Prashanth latency_ns = max(latency_ns, pcc_ss_data->pcc_mrtt * 1000);
1860be8b88d7SPrakash, Prashanth
1861be8b88d7SPrakash, Prashanth return latency_ns;
186250b813b1SVincent Guittot }
186350b813b1SVincent Guittot EXPORT_SYMBOL_GPL(cppc_get_transition_latency);
186450b813b1SVincent Guittot
186550b813b1SVincent Guittot /* Minimum struct length needed for the DMI processor entry we want */
186650b813b1SVincent Guittot #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
186750b813b1SVincent Guittot
186850b813b1SVincent Guittot /* Offset in the DMI processor structure for the max frequency */
186950b813b1SVincent Guittot #define DMI_PROCESSOR_MAX_SPEED 0x14
187050b813b1SVincent Guittot
187150b813b1SVincent Guittot /* Callback function used to retrieve the max frequency from DMI */
cppc_find_dmi_mhz(const struct dmi_header * dm,void * private)187250b813b1SVincent Guittot static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
187350b813b1SVincent Guittot {
187450b813b1SVincent Guittot const u8 *dmi_data = (const u8 *)dm;
187550b813b1SVincent Guittot u16 *mhz = (u16 *)private;
187650b813b1SVincent Guittot
187750b813b1SVincent Guittot if (dm->type == DMI_ENTRY_PROCESSOR &&
187850b813b1SVincent Guittot dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
187986932cd8SPrabhakar Pujeri u16 val = (u16)get_unaligned((const u16 *)
188050b813b1SVincent Guittot (dmi_data + DMI_PROCESSOR_MAX_SPEED));
188150b813b1SVincent Guittot *mhz = umax(val, *mhz);
188250b813b1SVincent Guittot }
188350b813b1SVincent Guittot }
188450b813b1SVincent Guittot
188550b813b1SVincent Guittot /* Look up the max frequency in DMI */
cppc_get_dmi_max_khz(void)188650b813b1SVincent Guittot static u64 cppc_get_dmi_max_khz(void)
188750b813b1SVincent Guittot {
188850b813b1SVincent Guittot u16 mhz = 0;
188950b813b1SVincent Guittot
189050b813b1SVincent Guittot dmi_walk(cppc_find_dmi_mhz, &mhz);
189150b813b1SVincent Guittot
189250b813b1SVincent Guittot /*
189350b813b1SVincent Guittot * Real stupid fallback value, just in case there is no
189450b813b1SVincent Guittot * actual value set.
189550b813b1SVincent Guittot */
189650b813b1SVincent Guittot mhz = mhz ? mhz : 1;
189750b813b1SVincent Guittot
189850b813b1SVincent Guittot return KHZ_PER_MHZ * mhz;
189950b813b1SVincent Guittot }
190050b813b1SVincent Guittot
190150b813b1SVincent Guittot /*
190250b813b1SVincent Guittot * If CPPC lowest_freq and nominal_freq registers are exposed then we can
190350b813b1SVincent Guittot * use them to convert perf to freq and vice versa. The conversion is
190450b813b1SVincent Guittot * extrapolated as an affine function passing by the 2 points:
190550b813b1SVincent Guittot * - (Low perf, Low freq)
190650b813b1SVincent Guittot * - (Nominal perf, Nominal freq)
190750b813b1SVincent Guittot */
cppc_perf_to_khz(struct cppc_perf_caps * caps,unsigned int perf)190850b813b1SVincent Guittot unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned int perf)
190950b813b1SVincent Guittot {
191050b813b1SVincent Guittot s64 retval, offset = 0;
191150b813b1SVincent Guittot static u64 max_khz;
191250b813b1SVincent Guittot u64 mul, div;
1913d93df29bSliwei
1914d93df29bSliwei if (caps->lowest_freq && caps->nominal_freq) {
1915d93df29bSliwei /* Avoid special case when nominal_freq is equal to lowest_freq */
1916d93df29bSliwei if (caps->lowest_freq == caps->nominal_freq) {
1917d93df29bSliwei mul = caps->nominal_freq;
191850b813b1SVincent Guittot div = caps->nominal_perf;
191950b813b1SVincent Guittot } else {
1920d93df29bSliwei mul = caps->nominal_freq - caps->lowest_freq;
1921d93df29bSliwei div = caps->nominal_perf - caps->lowest_perf;
192250b813b1SVincent Guittot }
192350b813b1SVincent Guittot mul *= KHZ_PER_MHZ;
192450b813b1SVincent Guittot offset = caps->nominal_freq * KHZ_PER_MHZ -
192550b813b1SVincent Guittot div64_u64(caps->nominal_perf * mul, div);
192650b813b1SVincent Guittot } else {
192750b813b1SVincent Guittot if (!max_khz)
192850b813b1SVincent Guittot max_khz = cppc_get_dmi_max_khz();
192950b813b1SVincent Guittot mul = max_khz;
193050b813b1SVincent Guittot div = caps->highest_perf;
193150b813b1SVincent Guittot }
193250b813b1SVincent Guittot
193350b813b1SVincent Guittot retval = offset + div64_u64(perf * mul, div);
193450b813b1SVincent Guittot if (retval >= 0)
193550b813b1SVincent Guittot return retval;
193650b813b1SVincent Guittot return 0;
193750b813b1SVincent Guittot }
193850b813b1SVincent Guittot EXPORT_SYMBOL_GPL(cppc_perf_to_khz);
193950b813b1SVincent Guittot
cppc_khz_to_perf(struct cppc_perf_caps * caps,unsigned int freq)194050b813b1SVincent Guittot unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned int freq)
194150b813b1SVincent Guittot {
194250b813b1SVincent Guittot s64 retval, offset = 0;
194350b813b1SVincent Guittot static u64 max_khz;
194450b813b1SVincent Guittot u64 mul, div;
1945d93df29bSliwei
1946d93df29bSliwei if (caps->lowest_freq && caps->nominal_freq) {
1947d93df29bSliwei /* Avoid special case when nominal_freq is equal to lowest_freq */
1948d93df29bSliwei if (caps->lowest_freq == caps->nominal_freq) {
1949d93df29bSliwei mul = caps->nominal_perf;
195050b813b1SVincent Guittot div = caps->nominal_freq;
195150b813b1SVincent Guittot } else {
1952d93df29bSliwei mul = caps->nominal_perf - caps->lowest_perf;
195350b813b1SVincent Guittot div = caps->nominal_freq - caps->lowest_freq;
195450b813b1SVincent Guittot }
195550b813b1SVincent Guittot /*
195650b813b1SVincent Guittot * We don't need to convert to kHz for computing offset and can
195750b813b1SVincent Guittot * directly use nominal_freq and lowest_freq as the div64_u64
195850b813b1SVincent Guittot * will remove the frequency unit.
195950b813b1SVincent Guittot */
196050b813b1SVincent Guittot offset = caps->nominal_perf -
196150b813b1SVincent Guittot div64_u64(caps->nominal_freq * mul, div);
196250b813b1SVincent Guittot /* But we need it for computing the perf level. */
196350b813b1SVincent Guittot div *= KHZ_PER_MHZ;
196450b813b1SVincent Guittot } else {
196550b813b1SVincent Guittot if (!max_khz)
196650b813b1SVincent Guittot max_khz = cppc_get_dmi_max_khz();
196750b813b1SVincent Guittot mul = caps->highest_perf;
196850b813b1SVincent Guittot div = max_khz;
196950b813b1SVincent Guittot }
197050b813b1SVincent Guittot
197150b813b1SVincent Guittot retval = offset + div64_u64(freq * mul, div);
197250b813b1SVincent Guittot if (retval >= 0)
197350b813b1SVincent Guittot return retval;
197450b813b1SVincent Guittot return 0;
1975 }
1976 EXPORT_SYMBOL_GPL(cppc_khz_to_perf);
1977