2 * Intel Core SoC Power Management Controller Driver
4 * Copyright (c) 2016, Intel Corporation.
7 * Authors: Rajneesh Bhardwaj <rajneesh.bhardwaj@intel.com>
8 * Vishwanath Somayaji <vishwanath.somayaji@intel.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/acpi.h>
24 #include <linux/debugfs.h>
25 #include <linux/delay.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/uaccess.h>
31 #include <asm/cpu_device_id.h>
32 #include <asm/intel-family.h>
34 #include "intel_pmc_core.h"
36 #define ICPU(model, data) \
37 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (kernel_ulong_t)data }
39 static struct pmc_dev pmc
;
41 static const struct pmc_bit_map spt_pll_map
[] = {
42 {"MIPI PLL", SPT_PMC_BIT_MPHY_CMN_LANE0
},
43 {"GEN2 USB2PCIE2 PLL", SPT_PMC_BIT_MPHY_CMN_LANE1
},
44 {"DMIPCIE3 PLL", SPT_PMC_BIT_MPHY_CMN_LANE2
},
45 {"SATA PLL", SPT_PMC_BIT_MPHY_CMN_LANE3
},
49 static const struct pmc_bit_map spt_mphy_map
[] = {
50 {"MPHY CORE LANE 0", SPT_PMC_BIT_MPHY_LANE0
},
51 {"MPHY CORE LANE 1", SPT_PMC_BIT_MPHY_LANE1
},
52 {"MPHY CORE LANE 2", SPT_PMC_BIT_MPHY_LANE2
},
53 {"MPHY CORE LANE 3", SPT_PMC_BIT_MPHY_LANE3
},
54 {"MPHY CORE LANE 4", SPT_PMC_BIT_MPHY_LANE4
},
55 {"MPHY CORE LANE 5", SPT_PMC_BIT_MPHY_LANE5
},
56 {"MPHY CORE LANE 6", SPT_PMC_BIT_MPHY_LANE6
},
57 {"MPHY CORE LANE 7", SPT_PMC_BIT_MPHY_LANE7
},
58 {"MPHY CORE LANE 8", SPT_PMC_BIT_MPHY_LANE8
},
59 {"MPHY CORE LANE 9", SPT_PMC_BIT_MPHY_LANE9
},
60 {"MPHY CORE LANE 10", SPT_PMC_BIT_MPHY_LANE10
},
61 {"MPHY CORE LANE 11", SPT_PMC_BIT_MPHY_LANE11
},
62 {"MPHY CORE LANE 12", SPT_PMC_BIT_MPHY_LANE12
},
63 {"MPHY CORE LANE 13", SPT_PMC_BIT_MPHY_LANE13
},
64 {"MPHY CORE LANE 14", SPT_PMC_BIT_MPHY_LANE14
},
65 {"MPHY CORE LANE 15", SPT_PMC_BIT_MPHY_LANE15
},
69 static const struct pmc_bit_map spt_pfear_map
[] = {
70 {"PMC", SPT_PMC_BIT_PMC
},
71 {"OPI-DMI", SPT_PMC_BIT_OPI
},
72 {"SPI / eSPI", SPT_PMC_BIT_SPI
},
73 {"XHCI", SPT_PMC_BIT_XHCI
},
74 {"SPA", SPT_PMC_BIT_SPA
},
75 {"SPB", SPT_PMC_BIT_SPB
},
76 {"SPC", SPT_PMC_BIT_SPC
},
77 {"GBE", SPT_PMC_BIT_GBE
},
78 {"SATA", SPT_PMC_BIT_SATA
},
79 {"HDA-PGD0", SPT_PMC_BIT_HDA_PGD0
},
80 {"HDA-PGD1", SPT_PMC_BIT_HDA_PGD1
},
81 {"HDA-PGD2", SPT_PMC_BIT_HDA_PGD2
},
82 {"HDA-PGD3", SPT_PMC_BIT_HDA_PGD3
},
83 {"RSVD", SPT_PMC_BIT_RSVD_0B
},
84 {"LPSS", SPT_PMC_BIT_LPSS
},
85 {"LPC", SPT_PMC_BIT_LPC
},
86 {"SMB", SPT_PMC_BIT_SMB
},
87 {"ISH", SPT_PMC_BIT_ISH
},
88 {"P2SB", SPT_PMC_BIT_P2SB
},
89 {"DFX", SPT_PMC_BIT_DFX
},
90 {"SCC", SPT_PMC_BIT_SCC
},
91 {"RSVD", SPT_PMC_BIT_RSVD_0C
},
92 {"FUSE", SPT_PMC_BIT_FUSE
},
93 {"CAMERA", SPT_PMC_BIT_CAMREA
},
94 {"RSVD", SPT_PMC_BIT_RSVD_0D
},
95 {"USB3-OTG", SPT_PMC_BIT_USB3_OTG
},
96 {"EXI", SPT_PMC_BIT_EXI
},
97 {"CSE", SPT_PMC_BIT_CSE
},
98 {"CSME_KVM", SPT_PMC_BIT_CSME_KVM
},
99 {"CSME_PMT", SPT_PMC_BIT_CSME_PMT
},
100 {"CSME_CLINK", SPT_PMC_BIT_CSME_CLINK
},
101 {"CSME_PTIO", SPT_PMC_BIT_CSME_PTIO
},
102 {"CSME_USBR", SPT_PMC_BIT_CSME_USBR
},
103 {"CSME_SUSRAM", SPT_PMC_BIT_CSME_SUSRAM
},
104 {"CSME_SMT", SPT_PMC_BIT_CSME_SMT
},
105 {"RSVD", SPT_PMC_BIT_RSVD_1A
},
106 {"CSME_SMS2", SPT_PMC_BIT_CSME_SMS2
},
107 {"CSME_SMS1", SPT_PMC_BIT_CSME_SMS1
},
108 {"CSME_RTC", SPT_PMC_BIT_CSME_RTC
},
109 {"CSME_PSF", SPT_PMC_BIT_CSME_PSF
},
113 static const struct pmc_reg_map spt_reg_map
= {
114 .pfear_sts
= spt_pfear_map
,
115 .mphy_sts
= spt_mphy_map
,
116 .pll_sts
= spt_pll_map
,
117 .slp_s0_offset
= SPT_PMC_SLP_S0_RES_COUNTER_OFFSET
,
118 .ltr_ignore_offset
= SPT_PMC_LTR_IGNORE_OFFSET
,
119 .regmap_length
= SPT_PMC_MMIO_REG_LEN
,
120 .ppfear0_offset
= SPT_PMC_XRAM_PPFEAR0A
,
121 .ppfear_buckets
= SPT_PPFEAR_NUM_ENTRIES
,
122 .pm_cfg_offset
= SPT_PMC_PM_CFG_OFFSET
,
123 .pm_read_disable_bit
= SPT_PMC_READ_DISABLE_BIT
,
126 /* Cannonlake: PGD PFET Enable Ack Status Register(s) bitmap */
127 static const struct pmc_bit_map cnp_pfear_map
[] = {
130 {"SPI/eSPI", BIT(2)},
138 {"HDA_PGD0", BIT(1)},
139 {"HDA_PGD1", BIT(2)},
140 {"HDA_PGD2", BIT(3)},
141 {"HDA_PGD3", BIT(4)},
155 {"CSME_FSC", BIT(0)},
156 {"USB3_OTG", BIT(1)},
159 {"csme_kvm", BIT(4)},
160 {"csme_pmt", BIT(5)},
161 {"csme_clink", BIT(6)},
162 {"csme_ptio", BIT(7)},
164 {"csme_usbr", BIT(0)},
165 {"csme_susram", BIT(1)},
166 {"csme_smt1", BIT(2)},
167 {"CSME_SMT4", BIT(3)},
168 {"csme_sms2", BIT(4)},
169 {"csme_sms1", BIT(5)},
170 {"csme_rtc", BIT(6)},
171 {"csme_psf", BIT(7)},
179 {"CSME_PECI", BIT(6)},
193 {"HDA_PGD4", BIT(2)},
194 {"HDA_PGD5", BIT(3)},
195 {"HDA_PGD6", BIT(4)},
199 static const struct pmc_reg_map cnp_reg_map
= {
200 .pfear_sts
= cnp_pfear_map
,
201 .slp_s0_offset
= CNP_PMC_SLP_S0_RES_COUNTER_OFFSET
,
202 .ltr_ignore_offset
= CNP_PMC_LTR_IGNORE_OFFSET
,
203 .regmap_length
= CNP_PMC_MMIO_REG_LEN
,
204 .ppfear0_offset
= CNP_PMC_HOST_PPFEAR0A
,
205 .ppfear_buckets
= CNP_PPFEAR_NUM_ENTRIES
,
206 .pm_cfg_offset
= CNP_PMC_PM_CFG_OFFSET
,
207 .pm_read_disable_bit
= CNP_PMC_READ_DISABLE_BIT
,
210 static inline u8
pmc_core_reg_read_byte(struct pmc_dev
*pmcdev
, int offset
)
212 return readb(pmcdev
->regbase
+ offset
);
215 static inline u32
pmc_core_reg_read(struct pmc_dev
*pmcdev
, int reg_offset
)
217 return readl(pmcdev
->regbase
+ reg_offset
);
220 static inline void pmc_core_reg_write(struct pmc_dev
*pmcdev
, int
223 writel(val
, pmcdev
->regbase
+ reg_offset
);
226 static inline u32
pmc_core_adjust_slp_s0_step(u32 value
)
228 return value
* SPT_PMC_SLP_S0_RES_COUNTER_STEP
;
231 static int pmc_core_dev_state_get(void *data
, u64
*val
)
233 struct pmc_dev
*pmcdev
= data
;
234 const struct pmc_reg_map
*map
= pmcdev
->map
;
237 value
= pmc_core_reg_read(pmcdev
, map
->slp_s0_offset
);
238 *val
= pmc_core_adjust_slp_s0_step(value
);
243 DEFINE_DEBUGFS_ATTRIBUTE(pmc_core_dev_state
, pmc_core_dev_state_get
, NULL
, "%llu\n");
245 static int pmc_core_check_read_lock_bit(void)
247 struct pmc_dev
*pmcdev
= &pmc
;
250 value
= pmc_core_reg_read(pmcdev
, pmcdev
->map
->pm_cfg_offset
);
251 return value
& BIT(pmcdev
->map
->pm_read_disable_bit
);
254 #if IS_ENABLED(CONFIG_DEBUG_FS)
255 static void pmc_core_display_map(struct seq_file
*s
, int index
,
256 u8 pf_reg
, const struct pmc_bit_map
*pf_map
)
258 seq_printf(s
, "PCH IP: %-2d - %-32s\tState: %s\n",
259 index
, pf_map
[index
].name
,
260 pf_map
[index
].bit_mask
& pf_reg
? "Off" : "On");
263 static int pmc_core_ppfear_sts_show(struct seq_file
*s
, void *unused
)
265 struct pmc_dev
*pmcdev
= s
->private;
266 const struct pmc_bit_map
*map
= pmcdev
->map
->pfear_sts
;
267 u8 pf_regs
[PPFEAR_MAX_NUM_ENTRIES
];
270 iter
= pmcdev
->map
->ppfear0_offset
;
272 for (index
= 0; index
< pmcdev
->map
->ppfear_buckets
&&
273 index
< PPFEAR_MAX_NUM_ENTRIES
; index
++, iter
++)
274 pf_regs
[index
] = pmc_core_reg_read_byte(pmcdev
, iter
);
276 for (index
= 0; map
[index
].name
; index
++)
277 pmc_core_display_map(s
, index
, pf_regs
[index
/ 8], map
);
282 static int pmc_core_ppfear_sts_open(struct inode
*inode
, struct file
*file
)
284 return single_open(file
, pmc_core_ppfear_sts_show
, inode
->i_private
);
287 static const struct file_operations pmc_core_ppfear_ops
= {
288 .open
= pmc_core_ppfear_sts_open
,
291 .release
= single_release
,
294 /* This function should return link status, 0 means ready */
295 static int pmc_core_mtpmc_link_status(void)
297 struct pmc_dev
*pmcdev
= &pmc
;
300 value
= pmc_core_reg_read(pmcdev
, SPT_PMC_PM_STS_OFFSET
);
301 return value
& BIT(SPT_PMC_MSG_FULL_STS_BIT
);
304 static int pmc_core_send_msg(u32
*addr_xram
)
306 struct pmc_dev
*pmcdev
= &pmc
;
310 for (timeout
= NUM_RETRIES
; timeout
> 0; timeout
--) {
311 if (pmc_core_mtpmc_link_status() == 0)
316 if (timeout
<= 0 && pmc_core_mtpmc_link_status())
319 dest
= (*addr_xram
& MTPMC_MASK
) | (1U << 1);
320 pmc_core_reg_write(pmcdev
, SPT_PMC_MTPMC_OFFSET
, dest
);
324 static int pmc_core_mphy_pg_sts_show(struct seq_file
*s
, void *unused
)
326 struct pmc_dev
*pmcdev
= s
->private;
327 const struct pmc_bit_map
*map
= pmcdev
->map
->mphy_sts
;
328 u32 mphy_core_reg_low
, mphy_core_reg_high
;
329 u32 val_low
, val_high
;
332 if (pmcdev
->pmc_xram_read_bit
) {
333 seq_puts(s
, "Access denied: please disable PMC_READ_DISABLE setting in BIOS.");
337 mphy_core_reg_low
= (SPT_PMC_MPHY_CORE_STS_0
<< 16);
338 mphy_core_reg_high
= (SPT_PMC_MPHY_CORE_STS_1
<< 16);
340 mutex_lock(&pmcdev
->lock
);
342 if (pmc_core_send_msg(&mphy_core_reg_low
) != 0) {
348 val_low
= pmc_core_reg_read(pmcdev
, SPT_PMC_MFPMC_OFFSET
);
350 if (pmc_core_send_msg(&mphy_core_reg_high
) != 0) {
356 val_high
= pmc_core_reg_read(pmcdev
, SPT_PMC_MFPMC_OFFSET
);
358 for (index
= 0; map
[index
].name
&& index
< 8; index
++) {
359 seq_printf(s
, "%-32s\tState: %s\n",
361 map
[index
].bit_mask
& val_low
? "Not power gated" :
365 for (index
= 8; map
[index
].name
; index
++) {
366 seq_printf(s
, "%-32s\tState: %s\n",
368 map
[index
].bit_mask
& val_high
? "Not power gated" :
373 mutex_unlock(&pmcdev
->lock
);
377 static int pmc_core_mphy_pg_sts_open(struct inode
*inode
, struct file
*file
)
379 return single_open(file
, pmc_core_mphy_pg_sts_show
, inode
->i_private
);
382 static const struct file_operations pmc_core_mphy_pg_ops
= {
383 .open
= pmc_core_mphy_pg_sts_open
,
386 .release
= single_release
,
389 static int pmc_core_pll_show(struct seq_file
*s
, void *unused
)
391 struct pmc_dev
*pmcdev
= s
->private;
392 const struct pmc_bit_map
*map
= pmcdev
->map
->pll_sts
;
393 u32 mphy_common_reg
, val
;
396 if (pmcdev
->pmc_xram_read_bit
) {
397 seq_puts(s
, "Access denied: please disable PMC_READ_DISABLE setting in BIOS.");
401 mphy_common_reg
= (SPT_PMC_MPHY_COM_STS_0
<< 16);
402 mutex_lock(&pmcdev
->lock
);
404 if (pmc_core_send_msg(&mphy_common_reg
) != 0) {
409 /* Observed PMC HW response latency for MTPMC-MFPMC is ~10 ms */
411 val
= pmc_core_reg_read(pmcdev
, SPT_PMC_MFPMC_OFFSET
);
413 for (index
= 0; map
[index
].name
; index
++) {
414 seq_printf(s
, "%-32s\tState: %s\n",
416 map
[index
].bit_mask
& val
? "Active" : "Idle");
420 mutex_unlock(&pmcdev
->lock
);
424 static int pmc_core_pll_open(struct inode
*inode
, struct file
*file
)
426 return single_open(file
, pmc_core_pll_show
, inode
->i_private
);
429 static const struct file_operations pmc_core_pll_ops
= {
430 .open
= pmc_core_pll_open
,
433 .release
= single_release
,
436 static ssize_t
pmc_core_ltr_ignore_write(struct file
*file
, const char __user
437 *userbuf
, size_t count
, loff_t
*ppos
)
439 struct pmc_dev
*pmcdev
= &pmc
;
440 const struct pmc_reg_map
*map
= pmcdev
->map
;
441 u32 val
, buf_size
, fd
;
444 buf_size
= count
< 64 ? count
: 64;
445 mutex_lock(&pmcdev
->lock
);
447 if (kstrtou32_from_user(userbuf
, buf_size
, 10, &val
)) {
452 if (val
> NUM_IP_IGN_ALLOWED
) {
457 fd
= pmc_core_reg_read(pmcdev
, map
->ltr_ignore_offset
);
459 pmc_core_reg_write(pmcdev
, map
->ltr_ignore_offset
, fd
);
462 mutex_unlock(&pmcdev
->lock
);
463 return err
== 0 ? count
: err
;
466 static int pmc_core_ltr_ignore_show(struct seq_file
*s
, void *unused
)
471 static int pmc_core_ltr_ignore_open(struct inode
*inode
, struct file
*file
)
473 return single_open(file
, pmc_core_ltr_ignore_show
, inode
->i_private
);
476 static const struct file_operations pmc_core_ltr_ignore_ops
= {
477 .open
= pmc_core_ltr_ignore_open
,
479 .write
= pmc_core_ltr_ignore_write
,
481 .release
= single_release
,
484 static void pmc_core_dbgfs_unregister(struct pmc_dev
*pmcdev
)
486 debugfs_remove_recursive(pmcdev
->dbgfs_dir
);
489 static int pmc_core_dbgfs_register(struct pmc_dev
*pmcdev
)
493 dir
= debugfs_create_dir("pmc_core", NULL
);
497 pmcdev
->dbgfs_dir
= dir
;
499 debugfs_create_file("slp_s0_residency_usec", 0444, dir
, pmcdev
,
500 &pmc_core_dev_state
);
502 debugfs_create_file("pch_ip_power_gating_status", 0444, dir
, pmcdev
,
503 &pmc_core_ppfear_ops
);
505 debugfs_create_file("ltr_ignore", 0644, dir
, pmcdev
,
506 &pmc_core_ltr_ignore_ops
);
508 if (pmcdev
->map
->pll_sts
)
509 debugfs_create_file("pll_status", 0444, dir
, pmcdev
,
512 if (pmcdev
->map
->mphy_sts
)
513 debugfs_create_file("mphy_core_lanes_power_gating_status",
515 &pmc_core_mphy_pg_ops
);
520 static inline int pmc_core_dbgfs_register(struct pmc_dev
*pmcdev
)
525 static inline void pmc_core_dbgfs_unregister(struct pmc_dev
*pmcdev
)
528 #endif /* CONFIG_DEBUG_FS */
530 static const struct x86_cpu_id intel_pmc_core_ids
[] = {
531 ICPU(INTEL_FAM6_SKYLAKE_MOBILE
, &spt_reg_map
),
532 ICPU(INTEL_FAM6_SKYLAKE_DESKTOP
, &spt_reg_map
),
533 ICPU(INTEL_FAM6_KABYLAKE_MOBILE
, &spt_reg_map
),
534 ICPU(INTEL_FAM6_KABYLAKE_DESKTOP
, &spt_reg_map
),
535 ICPU(INTEL_FAM6_CANNONLAKE_MOBILE
, &cnp_reg_map
),
539 MODULE_DEVICE_TABLE(x86cpu
, intel_pmc_core_ids
);
541 static const struct pci_device_id pmc_pci_ids
[] = {
542 { PCI_VDEVICE(INTEL
, SPT_PMC_PCI_DEVICE_ID
), 0},
546 static int __init
pmc_core_probe(void)
548 struct pmc_dev
*pmcdev
= &pmc
;
549 const struct x86_cpu_id
*cpu_id
;
553 cpu_id
= x86_match_cpu(intel_pmc_core_ids
);
557 pmcdev
->map
= (struct pmc_reg_map
*)cpu_id
->driver_data
;
560 * Coffeelake has CPU ID of Kabylake and Cannonlake PCH. So here
561 * Sunrisepoint PCH regmap can't be used. Use Cannonlake PCH regmap
564 if (!pci_dev_present(pmc_pci_ids
))
565 pmcdev
->map
= &cnp_reg_map
;
567 if (lpit_read_residency_count_address(&slp_s0_addr
))
568 pmcdev
->base_addr
= PMC_BASE_ADDR_DEFAULT
;
570 pmcdev
->base_addr
= slp_s0_addr
- pmcdev
->map
->slp_s0_offset
;
572 pmcdev
->regbase
= ioremap(pmcdev
->base_addr
,
573 pmcdev
->map
->regmap_length
);
574 if (!pmcdev
->regbase
)
577 mutex_init(&pmcdev
->lock
);
578 pmcdev
->pmc_xram_read_bit
= pmc_core_check_read_lock_bit();
580 err
= pmc_core_dbgfs_register(pmcdev
);
582 pr_warn(" debugfs register failed.\n");
583 iounmap(pmcdev
->regbase
);
587 pr_info(" initialized\n");
590 module_init(pmc_core_probe
)
592 static void __exit
pmc_core_remove(void)
594 struct pmc_dev
*pmcdev
= &pmc
;
596 pmc_core_dbgfs_unregister(pmcdev
);
597 mutex_destroy(&pmcdev
->lock
);
598 iounmap(pmcdev
->regbase
);
600 module_exit(pmc_core_remove
)
602 MODULE_LICENSE("GPL v2");
603 MODULE_DESCRIPTION("Intel PMC Core Driver");