1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AMD SoC Power Management Controller Driver
5 * Copyright (c) 2020, Advanced Micro Devices, Inc.
8 * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <asm/amd_nb.h>
14 #include <linux/acpi.h>
15 #include <linux/bitfield.h>
16 #include <linux/bits.h>
17 #include <linux/debugfs.h>
18 #include <linux/delay.h>
20 #include <linux/iopoll.h>
21 #include <linux/limits.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
24 #include <linux/platform_device.h>
25 #include <linux/rtc.h>
26 #include <linux/serio.h>
27 #include <linux/suspend.h>
28 #include <linux/seq_file.h>
29 #include <linux/uaccess.h>
33 /* SMU communication registers */
34 #define AMD_PMC_REGISTER_RESPONSE 0x980
35 #define AMD_PMC_REGISTER_ARGUMENT 0x9BC
37 /* PMC Scratch Registers */
38 #define AMD_PMC_SCRATCH_REG_CZN 0x94
39 #define AMD_PMC_SCRATCH_REG_YC 0xD14
40 #define AMD_PMC_SCRATCH_REG_1AH 0xF14
43 #define AMD_PMC_STB_PMI_0 0x03E30600
44 #define AMD_PMC_STB_S2IDLE_PREPARE 0xC6000001
45 #define AMD_PMC_STB_S2IDLE_RESTORE 0xC6000002
46 #define AMD_PMC_STB_S2IDLE_CHECK 0xC6000003
47 #define AMD_PMC_STB_DUMMY_PC 0xC6000007
49 /* STB S2D(Spill to DRAM) has different message port offset */
50 #define AMD_S2D_REGISTER_MESSAGE 0xA20
51 #define AMD_S2D_REGISTER_RESPONSE 0xA80
52 #define AMD_S2D_REGISTER_ARGUMENT 0xA88
54 /* STB Spill to DRAM Parameters */
55 #define S2D_TELEMETRY_BYTES_MAX 0x100000U
56 #define S2D_RSVD_RAM_SPACE 0x100000
57 #define S2D_TELEMETRY_DRAMBYTES_MAX 0x1000000
59 /* STB Spill to DRAM Message Definition */
60 #define STB_FORCE_FLUSH_DATA 0xCF
62 /* Base address of SMU for mapping physical address to virtual address */
63 #define AMD_PMC_MAPPING_SIZE 0x01000
64 #define AMD_PMC_BASE_ADDR_OFFSET 0x10000
65 #define AMD_PMC_BASE_ADDR_LO 0x13B102E8
66 #define AMD_PMC_BASE_ADDR_HI 0x13B102EC
67 #define AMD_PMC_BASE_ADDR_LO_MASK GENMASK(15, 0)
68 #define AMD_PMC_BASE_ADDR_HI_MASK GENMASK(31, 20)
70 /* SMU Response Codes */
71 #define AMD_PMC_RESULT_OK 0x01
72 #define AMD_PMC_RESULT_CMD_REJECT_BUSY 0xFC
73 #define AMD_PMC_RESULT_CMD_REJECT_PREREQ 0xFD
74 #define AMD_PMC_RESULT_CMD_UNKNOWN 0xFE
75 #define AMD_PMC_RESULT_FAILED 0xFF
77 /* FCH SSC Registers */
78 #define FCH_S0I3_ENTRY_TIME_L_OFFSET 0x30
79 #define FCH_S0I3_ENTRY_TIME_H_OFFSET 0x34
80 #define FCH_S0I3_EXIT_TIME_L_OFFSET 0x38
81 #define FCH_S0I3_EXIT_TIME_H_OFFSET 0x3C
82 #define FCH_SSC_MAPPING_SIZE 0x800
83 #define FCH_BASE_PHY_ADDR_LOW 0xFED81100
84 #define FCH_BASE_PHY_ADDR_HIGH 0x00000000
86 /* SMU Message Definations */
87 #define SMU_MSG_GETSMUVERSION 0x02
88 #define SMU_MSG_LOG_GETDRAM_ADDR_HI 0x04
89 #define SMU_MSG_LOG_GETDRAM_ADDR_LO 0x05
90 #define SMU_MSG_LOG_START 0x06
91 #define SMU_MSG_LOG_RESET 0x07
92 #define SMU_MSG_LOG_DUMP_DATA 0x08
93 #define SMU_MSG_GET_SUP_CONSTRAINTS 0x09
95 #define PMC_MSG_DELAY_MIN_US 50
96 #define RESPONSE_REGISTER_LOOP_MAX 20000
98 #define DELAY_MIN_US 2000
99 #define DELAY_MAX_US 3000
100 #define FIFO_SIZE 4096
109 S2D_TELEMETRY_SIZE
= 0x01,
116 struct amd_pmc_stb_v2_data
{
118 u8 data
[] __counted_by(size
);
121 struct amd_pmc_bit_map
{
126 static const struct amd_pmc_bit_map soc15_ip_blk
[] = {
152 static bool enable_stb
;
153 module_param(enable_stb
, bool, 0644);
154 MODULE_PARM_DESC(enable_stb
, "Enable the STB debug mechanism");
156 static bool disable_workarounds
;
157 module_param(disable_workarounds
, bool, 0644);
158 MODULE_PARM_DESC(disable_workarounds
, "Disable workarounds for platform bugs");
160 static bool dump_custom_stb
;
161 module_param(dump_custom_stb
, bool, 0644);
162 MODULE_PARM_DESC(dump_custom_stb
, "Enable to dump full STB buffer");
164 static struct amd_pmc_dev pmc
;
165 static int amd_pmc_send_cmd(struct amd_pmc_dev
*dev
, u32 arg
, u32
*data
, u8 msg
, bool ret
);
166 static int amd_pmc_read_stb(struct amd_pmc_dev
*dev
, u32
*buf
);
167 static int amd_pmc_write_stb(struct amd_pmc_dev
*dev
, u32 data
);
169 static inline u32
amd_pmc_reg_read(struct amd_pmc_dev
*dev
, int reg_offset
)
171 return ioread32(dev
->regbase
+ reg_offset
);
174 static inline void amd_pmc_reg_write(struct amd_pmc_dev
*dev
, int reg_offset
, u32 val
)
176 iowrite32(val
, dev
->regbase
+ reg_offset
);
182 u32 s0i3_last_entry_status
;
184 u64 timeentering_s0i3_lastcapture
;
185 u64 timeentering_s0i3_totaltime
;
186 u64 timeto_resume_to_os_lastcapture
;
187 u64 timeto_resume_to_os_totaltime
;
188 u64 timein_s0i3_lastcapture
;
189 u64 timein_s0i3_totaltime
;
190 u64 timein_swdrips_lastcapture
;
191 u64 timein_swdrips_totaltime
;
192 u64 timecondition_notmet_lastcapture
[32];
193 u64 timecondition_notmet_totaltime
[32];
196 static int amd_pmc_stb_debugfs_open(struct inode
*inode
, struct file
*filp
)
198 struct amd_pmc_dev
*dev
= filp
->f_inode
->i_private
;
199 u32 size
= FIFO_SIZE
* sizeof(u32
);
203 buf
= kzalloc(size
, GFP_KERNEL
);
207 rc
= amd_pmc_read_stb(dev
, buf
);
213 filp
->private_data
= buf
;
217 static ssize_t
amd_pmc_stb_debugfs_read(struct file
*filp
, char __user
*buf
, size_t size
,
220 if (!filp
->private_data
)
223 return simple_read_from_buffer(buf
, size
, pos
, filp
->private_data
,
224 FIFO_SIZE
* sizeof(u32
));
227 static int amd_pmc_stb_debugfs_release(struct inode
*inode
, struct file
*filp
)
229 kfree(filp
->private_data
);
233 static const struct file_operations amd_pmc_stb_debugfs_fops
= {
234 .owner
= THIS_MODULE
,
235 .open
= amd_pmc_stb_debugfs_open
,
236 .read
= amd_pmc_stb_debugfs_read
,
237 .release
= amd_pmc_stb_debugfs_release
,
240 /* Enhanced STB Firmware Reporting Mechanism */
241 static int amd_pmc_stb_handle_efr(struct file
*filp
)
243 struct amd_pmc_dev
*dev
= filp
->f_inode
->i_private
;
244 struct amd_pmc_stb_v2_data
*stb_data_arr
;
247 fsize
= dev
->dram_size
- S2D_RSVD_RAM_SPACE
;
248 stb_data_arr
= kmalloc(struct_size(stb_data_arr
, data
, fsize
), GFP_KERNEL
);
252 stb_data_arr
->size
= fsize
;
253 memcpy_fromio(stb_data_arr
->data
, dev
->stb_virt_addr
, fsize
);
254 filp
->private_data
= stb_data_arr
;
259 static int amd_pmc_stb_debugfs_open_v2(struct inode
*inode
, struct file
*filp
)
261 struct amd_pmc_dev
*dev
= filp
->f_inode
->i_private
;
262 u32 fsize
, num_samples
, val
, stb_rdptr_offset
= 0;
263 struct amd_pmc_stb_v2_data
*stb_data_arr
;
266 /* Write dummy postcode while reading the STB buffer */
267 ret
= amd_pmc_write_stb(dev
, AMD_PMC_STB_DUMMY_PC
);
269 dev_err(dev
->dev
, "error writing to STB: %d\n", ret
);
271 /* Spill to DRAM num_samples uses separate SMU message port */
274 ret
= amd_pmc_send_cmd(dev
, 0, &val
, STB_FORCE_FLUSH_DATA
, 1);
276 dev_dbg_once(dev
->dev
, "S2D force flush not supported: %d\n", ret
);
279 * We have a custom stb size and the PMFW is supposed to give
280 * the enhanced dram size. Note that we land here only for the
281 * platforms that support enhanced dram size reporting.
284 return amd_pmc_stb_handle_efr(filp
);
286 /* Get the num_samples to calculate the last push location */
287 ret
= amd_pmc_send_cmd(dev
, S2D_NUM_SAMPLES
, &num_samples
, dev
->s2d_msg_id
, true);
288 /* Clear msg_port for other SMU operation */
291 dev_err(dev
->dev
, "error: S2D_NUM_SAMPLES not supported : %d\n", ret
);
295 fsize
= min(num_samples
, S2D_TELEMETRY_BYTES_MAX
);
296 stb_data_arr
= kmalloc(struct_size(stb_data_arr
, data
, fsize
), GFP_KERNEL
);
300 stb_data_arr
->size
= fsize
;
303 * Start capturing data from the last push location.
304 * This is for general cases, where the stb limits
305 * are meant for standard usage.
307 if (num_samples
> S2D_TELEMETRY_BYTES_MAX
) {
308 /* First read oldest data starting 1 behind last write till end of ringbuffer */
309 stb_rdptr_offset
= num_samples
% S2D_TELEMETRY_BYTES_MAX
;
310 fsize
= S2D_TELEMETRY_BYTES_MAX
- stb_rdptr_offset
;
312 memcpy_fromio(stb_data_arr
->data
, dev
->stb_virt_addr
+ stb_rdptr_offset
, fsize
);
313 /* Second copy the newer samples from offset 0 - last write */
314 memcpy_fromio(stb_data_arr
->data
+ fsize
, dev
->stb_virt_addr
, stb_rdptr_offset
);
316 memcpy_fromio(stb_data_arr
->data
, dev
->stb_virt_addr
, fsize
);
319 filp
->private_data
= stb_data_arr
;
324 static ssize_t
amd_pmc_stb_debugfs_read_v2(struct file
*filp
, char __user
*buf
, size_t size
,
327 struct amd_pmc_stb_v2_data
*data
= filp
->private_data
;
329 return simple_read_from_buffer(buf
, size
, pos
, data
->data
, data
->size
);
332 static int amd_pmc_stb_debugfs_release_v2(struct inode
*inode
, struct file
*filp
)
334 kfree(filp
->private_data
);
338 static const struct file_operations amd_pmc_stb_debugfs_fops_v2
= {
339 .owner
= THIS_MODULE
,
340 .open
= amd_pmc_stb_debugfs_open_v2
,
341 .read
= amd_pmc_stb_debugfs_read_v2
,
342 .release
= amd_pmc_stb_debugfs_release_v2
,
345 static void amd_pmc_get_ip_info(struct amd_pmc_dev
*dev
)
347 switch (dev
->cpu_id
) {
353 dev
->s2d_msg_id
= 0xBE;
354 dev
->smu_msg
= 0x538;
358 dev
->s2d_msg_id
= 0x85;
359 dev
->smu_msg
= 0x538;
361 case PCI_DEVICE_ID_AMD_1AH_M20H_ROOT
:
362 case PCI_DEVICE_ID_AMD_1AH_M60H_ROOT
:
364 dev
->s2d_msg_id
= 0xDE;
365 dev
->smu_msg
= 0x938;
370 static int amd_pmc_setup_smu_logging(struct amd_pmc_dev
*dev
)
372 if (dev
->cpu_id
== AMD_CPU_ID_PCO
) {
373 dev_warn_once(dev
->dev
, "SMU debugging info not supported on this platform\n");
377 /* Get Active devices list from SMU */
378 if (!dev
->active_ips
)
379 amd_pmc_send_cmd(dev
, 0, &dev
->active_ips
, SMU_MSG_GET_SUP_CONSTRAINTS
, true);
381 /* Get dram address */
382 if (!dev
->smu_virt_addr
) {
383 u32 phys_addr_low
, phys_addr_hi
;
386 amd_pmc_send_cmd(dev
, 0, &phys_addr_low
, SMU_MSG_LOG_GETDRAM_ADDR_LO
, true);
387 amd_pmc_send_cmd(dev
, 0, &phys_addr_hi
, SMU_MSG_LOG_GETDRAM_ADDR_HI
, true);
388 smu_phys_addr
= ((u64
)phys_addr_hi
<< 32 | phys_addr_low
);
390 dev
->smu_virt_addr
= devm_ioremap(dev
->dev
, smu_phys_addr
,
391 sizeof(struct smu_metrics
));
392 if (!dev
->smu_virt_addr
)
396 /* Start the logging */
397 amd_pmc_send_cmd(dev
, 0, NULL
, SMU_MSG_LOG_RESET
, false);
398 amd_pmc_send_cmd(dev
, 0, NULL
, SMU_MSG_LOG_START
, false);
403 static int get_metrics_table(struct amd_pmc_dev
*pdev
, struct smu_metrics
*table
)
405 if (!pdev
->smu_virt_addr
) {
406 int ret
= amd_pmc_setup_smu_logging(pdev
);
412 if (pdev
->cpu_id
== AMD_CPU_ID_PCO
)
414 memcpy_fromio(table
, pdev
->smu_virt_addr
, sizeof(struct smu_metrics
));
418 static void amd_pmc_validate_deepest(struct amd_pmc_dev
*pdev
)
420 struct smu_metrics table
;
422 if (get_metrics_table(pdev
, &table
))
425 if (!table
.s0i3_last_entry_status
)
426 dev_warn(pdev
->dev
, "Last suspend didn't reach deepest state\n");
427 pm_report_hw_sleep_time(table
.s0i3_last_entry_status
?
428 table
.timein_s0i3_lastcapture
: 0);
431 static int amd_pmc_get_smu_version(struct amd_pmc_dev
*dev
)
436 if (dev
->cpu_id
== AMD_CPU_ID_PCO
)
439 rc
= amd_pmc_send_cmd(dev
, 0, &val
, SMU_MSG_GETSMUVERSION
, true);
443 dev
->smu_program
= (val
>> 24) & GENMASK(7, 0);
444 dev
->major
= (val
>> 16) & GENMASK(7, 0);
445 dev
->minor
= (val
>> 8) & GENMASK(7, 0);
446 dev
->rev
= (val
>> 0) & GENMASK(7, 0);
448 dev_dbg(dev
->dev
, "SMU program %u version is %u.%u.%u\n",
449 dev
->smu_program
, dev
->major
, dev
->minor
, dev
->rev
);
454 static ssize_t
smu_fw_version_show(struct device
*d
, struct device_attribute
*attr
,
457 struct amd_pmc_dev
*dev
= dev_get_drvdata(d
);
460 int rc
= amd_pmc_get_smu_version(dev
);
465 return sysfs_emit(buf
, "%u.%u.%u\n", dev
->major
, dev
->minor
, dev
->rev
);
468 static ssize_t
smu_program_show(struct device
*d
, struct device_attribute
*attr
,
471 struct amd_pmc_dev
*dev
= dev_get_drvdata(d
);
474 int rc
= amd_pmc_get_smu_version(dev
);
479 return sysfs_emit(buf
, "%u\n", dev
->smu_program
);
482 static DEVICE_ATTR_RO(smu_fw_version
);
483 static DEVICE_ATTR_RO(smu_program
);
485 static umode_t
pmc_attr_is_visible(struct kobject
*kobj
, struct attribute
*attr
, int idx
)
487 struct device
*dev
= kobj_to_dev(kobj
);
488 struct amd_pmc_dev
*pdev
= dev_get_drvdata(dev
);
490 if (pdev
->cpu_id
== AMD_CPU_ID_PCO
)
495 static struct attribute
*pmc_attrs
[] = {
496 &dev_attr_smu_fw_version
.attr
,
497 &dev_attr_smu_program
.attr
,
501 static struct attribute_group pmc_attr_group
= {
503 .is_visible
= pmc_attr_is_visible
,
506 static const struct attribute_group
*pmc_groups
[] = {
511 static int smu_fw_info_show(struct seq_file
*s
, void *unused
)
513 struct amd_pmc_dev
*dev
= s
->private;
514 struct smu_metrics table
;
517 if (get_metrics_table(dev
, &table
))
520 seq_puts(s
, "\n=== SMU Statistics ===\n");
521 seq_printf(s
, "Table Version: %d\n", table
.table_version
);
522 seq_printf(s
, "Hint Count: %d\n", table
.hint_count
);
523 seq_printf(s
, "Last S0i3 Status: %s\n", table
.s0i3_last_entry_status
? "Success" :
525 seq_printf(s
, "Time (in us) to S0i3: %lld\n", table
.timeentering_s0i3_lastcapture
);
526 seq_printf(s
, "Time (in us) in S0i3: %lld\n", table
.timein_s0i3_lastcapture
);
527 seq_printf(s
, "Time (in us) to resume from S0i3: %lld\n",
528 table
.timeto_resume_to_os_lastcapture
);
530 seq_puts(s
, "\n=== Active time (in us) ===\n");
531 for (idx
= 0 ; idx
< dev
->num_ips
; idx
++) {
532 if (soc15_ip_blk
[idx
].bit_mask
& dev
->active_ips
)
533 seq_printf(s
, "%-8s : %lld\n", soc15_ip_blk
[idx
].name
,
534 table
.timecondition_notmet_lastcapture
[idx
]);
539 DEFINE_SHOW_ATTRIBUTE(smu_fw_info
);
541 static int s0ix_stats_show(struct seq_file
*s
, void *unused
)
543 struct amd_pmc_dev
*dev
= s
->private;
544 u64 entry_time
, exit_time
, residency
;
546 /* Use FCH registers to get the S0ix stats */
547 if (!dev
->fch_virt_addr
) {
548 u32 base_addr_lo
= FCH_BASE_PHY_ADDR_LOW
;
549 u32 base_addr_hi
= FCH_BASE_PHY_ADDR_HIGH
;
550 u64 fch_phys_addr
= ((u64
)base_addr_hi
<< 32 | base_addr_lo
);
552 dev
->fch_virt_addr
= devm_ioremap(dev
->dev
, fch_phys_addr
, FCH_SSC_MAPPING_SIZE
);
553 if (!dev
->fch_virt_addr
)
557 entry_time
= ioread32(dev
->fch_virt_addr
+ FCH_S0I3_ENTRY_TIME_H_OFFSET
);
558 entry_time
= entry_time
<< 32 | ioread32(dev
->fch_virt_addr
+ FCH_S0I3_ENTRY_TIME_L_OFFSET
);
560 exit_time
= ioread32(dev
->fch_virt_addr
+ FCH_S0I3_EXIT_TIME_H_OFFSET
);
561 exit_time
= exit_time
<< 32 | ioread32(dev
->fch_virt_addr
+ FCH_S0I3_EXIT_TIME_L_OFFSET
);
563 /* It's in 48MHz. We need to convert it */
564 residency
= exit_time
- entry_time
;
565 do_div(residency
, 48);
567 seq_puts(s
, "=== S0ix statistics ===\n");
568 seq_printf(s
, "S0ix Entry Time: %lld\n", entry_time
);
569 seq_printf(s
, "S0ix Exit Time: %lld\n", exit_time
);
570 seq_printf(s
, "Residency Time: %lld\n", residency
);
574 DEFINE_SHOW_ATTRIBUTE(s0ix_stats
);
576 static int amd_pmc_idlemask_read(struct amd_pmc_dev
*pdev
, struct device
*dev
,
582 switch (pdev
->cpu_id
) {
584 /* we haven't yet read SMU version */
586 rc
= amd_pmc_get_smu_version(pdev
);
590 if (pdev
->major
> 56 || (pdev
->major
>= 55 && pdev
->minor
>= 37))
591 val
= amd_pmc_reg_read(pdev
, AMD_PMC_SCRATCH_REG_CZN
);
598 val
= amd_pmc_reg_read(pdev
, AMD_PMC_SCRATCH_REG_YC
);
600 case PCI_DEVICE_ID_AMD_1AH_M20H_ROOT
:
601 case PCI_DEVICE_ID_AMD_1AH_M60H_ROOT
:
602 val
= amd_pmc_reg_read(pdev
, AMD_PMC_SCRATCH_REG_1AH
);
609 pm_pr_dbg("SMU idlemask s0i3: 0x%x\n", val
);
612 seq_printf(s
, "SMU idlemask : 0x%x\n", val
);
617 static int amd_pmc_idlemask_show(struct seq_file
*s
, void *unused
)
619 return amd_pmc_idlemask_read(s
->private, NULL
, s
);
621 DEFINE_SHOW_ATTRIBUTE(amd_pmc_idlemask
);
623 static void amd_pmc_dbgfs_unregister(struct amd_pmc_dev
*dev
)
625 debugfs_remove_recursive(dev
->dbgfs_dir
);
628 static bool amd_pmc_is_stb_supported(struct amd_pmc_dev
*dev
)
630 switch (dev
->cpu_id
) {
634 case PCI_DEVICE_ID_AMD_1AH_M20H_ROOT
:
635 case PCI_DEVICE_ID_AMD_1AH_M60H_ROOT
:
642 static void amd_pmc_dbgfs_register(struct amd_pmc_dev
*dev
)
644 dev
->dbgfs_dir
= debugfs_create_dir("amd_pmc", NULL
);
645 debugfs_create_file("smu_fw_info", 0644, dev
->dbgfs_dir
, dev
,
647 debugfs_create_file("s0ix_stats", 0644, dev
->dbgfs_dir
, dev
,
649 debugfs_create_file("amd_pmc_idlemask", 0644, dev
->dbgfs_dir
, dev
,
650 &amd_pmc_idlemask_fops
);
651 /* Enable STB only when the module_param is set */
653 if (amd_pmc_is_stb_supported(dev
))
654 debugfs_create_file("stb_read", 0644, dev
->dbgfs_dir
, dev
,
655 &amd_pmc_stb_debugfs_fops_v2
);
657 debugfs_create_file("stb_read", 0644, dev
->dbgfs_dir
, dev
,
658 &amd_pmc_stb_debugfs_fops
);
662 static void amd_pmc_dump_registers(struct amd_pmc_dev
*dev
)
664 u32 value
, message
, argument
, response
;
667 message
= AMD_S2D_REGISTER_MESSAGE
;
668 argument
= AMD_S2D_REGISTER_ARGUMENT
;
669 response
= AMD_S2D_REGISTER_RESPONSE
;
671 message
= dev
->smu_msg
;
672 argument
= AMD_PMC_REGISTER_ARGUMENT
;
673 response
= AMD_PMC_REGISTER_RESPONSE
;
676 value
= amd_pmc_reg_read(dev
, response
);
677 dev_dbg(dev
->dev
, "AMD_%s_REGISTER_RESPONSE:%x\n", dev
->msg_port
? "S2D" : "PMC", value
);
679 value
= amd_pmc_reg_read(dev
, argument
);
680 dev_dbg(dev
->dev
, "AMD_%s_REGISTER_ARGUMENT:%x\n", dev
->msg_port
? "S2D" : "PMC", value
);
682 value
= amd_pmc_reg_read(dev
, message
);
683 dev_dbg(dev
->dev
, "AMD_%s_REGISTER_MESSAGE:%x\n", dev
->msg_port
? "S2D" : "PMC", value
);
686 static int amd_pmc_send_cmd(struct amd_pmc_dev
*dev
, u32 arg
, u32
*data
, u8 msg
, bool ret
)
689 u32 val
, message
, argument
, response
;
691 mutex_lock(&dev
->lock
);
694 message
= AMD_S2D_REGISTER_MESSAGE
;
695 argument
= AMD_S2D_REGISTER_ARGUMENT
;
696 response
= AMD_S2D_REGISTER_RESPONSE
;
698 message
= dev
->smu_msg
;
699 argument
= AMD_PMC_REGISTER_ARGUMENT
;
700 response
= AMD_PMC_REGISTER_RESPONSE
;
703 /* Wait until we get a valid response */
704 rc
= readx_poll_timeout(ioread32
, dev
->regbase
+ response
,
705 val
, val
!= 0, PMC_MSG_DELAY_MIN_US
,
706 PMC_MSG_DELAY_MIN_US
* RESPONSE_REGISTER_LOOP_MAX
);
708 dev_err(dev
->dev
, "failed to talk to SMU\n");
712 /* Write zero to response register */
713 amd_pmc_reg_write(dev
, response
, 0);
715 /* Write argument into response register */
716 amd_pmc_reg_write(dev
, argument
, arg
);
718 /* Write message ID to message ID register */
719 amd_pmc_reg_write(dev
, message
, msg
);
721 /* Wait until we get a valid response */
722 rc
= readx_poll_timeout(ioread32
, dev
->regbase
+ response
,
723 val
, val
!= 0, PMC_MSG_DELAY_MIN_US
,
724 PMC_MSG_DELAY_MIN_US
* RESPONSE_REGISTER_LOOP_MAX
);
726 dev_err(dev
->dev
, "SMU response timed out\n");
731 case AMD_PMC_RESULT_OK
:
733 /* PMFW may take longer time to return back the data */
734 usleep_range(DELAY_MIN_US
, 10 * DELAY_MAX_US
);
735 *data
= amd_pmc_reg_read(dev
, argument
);
738 case AMD_PMC_RESULT_CMD_REJECT_BUSY
:
739 dev_err(dev
->dev
, "SMU not ready. err: 0x%x\n", val
);
742 case AMD_PMC_RESULT_CMD_UNKNOWN
:
743 dev_err(dev
->dev
, "SMU cmd unknown. err: 0x%x\n", val
);
746 case AMD_PMC_RESULT_CMD_REJECT_PREREQ
:
747 case AMD_PMC_RESULT_FAILED
:
749 dev_err(dev
->dev
, "SMU cmd failed. err: 0x%x\n", val
);
755 mutex_unlock(&dev
->lock
);
756 amd_pmc_dump_registers(dev
);
760 static int amd_pmc_get_os_hint(struct amd_pmc_dev
*dev
)
762 switch (dev
->cpu_id
) {
764 return MSG_OS_HINT_PCO
;
769 case PCI_DEVICE_ID_AMD_1AH_M20H_ROOT
:
770 case PCI_DEVICE_ID_AMD_1AH_M60H_ROOT
:
771 return MSG_OS_HINT_RN
;
776 static int amd_pmc_wa_irq1(struct amd_pmc_dev
*pdev
)
781 /* cezanne platform firmware has a fix in 64.66.0 */
782 if (pdev
->cpu_id
== AMD_CPU_ID_CZN
) {
784 rc
= amd_pmc_get_smu_version(pdev
);
789 if (pdev
->major
> 64 || (pdev
->major
== 64 && pdev
->minor
> 65))
793 d
= bus_find_device_by_name(&serio_bus
, NULL
, "serio0");
796 if (device_may_wakeup(d
)) {
797 dev_info_once(d
, "Disabling IRQ1 wakeup source to avoid platform firmware bug\n");
799 device_set_wakeup_enable(d
, false);
806 static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev
*pdev
, u32
*arg
)
808 struct rtc_device
*rtc_device
;
809 time64_t then
, now
, duration
;
810 struct rtc_wkalrm alarm
;
814 /* we haven't yet read SMU version */
816 rc
= amd_pmc_get_smu_version(pdev
);
821 if (pdev
->major
< 64 || (pdev
->major
== 64 && pdev
->minor
< 53))
824 rtc_device
= rtc_class_open("rtc0");
827 rc
= rtc_read_alarm(rtc_device
, &alarm
);
830 if (!alarm
.enabled
) {
831 dev_dbg(pdev
->dev
, "alarm not enabled\n");
834 rc
= rtc_read_time(rtc_device
, &tm
);
837 then
= rtc_tm_to_time64(&alarm
.time
);
838 now
= rtc_tm_to_time64(&tm
);
845 /* will be stored in upper 16 bits of s0i3 hint argument,
846 * so timer wakeup from s0i3 is limited to ~18 hours or less
848 if (duration
<= 4 || duration
> U16_MAX
)
851 *arg
|= (duration
<< 16);
852 rc
= rtc_alarm_irq_enable(rtc_device
, 0);
853 pm_pr_dbg("wakeup timer programmed for %lld seconds\n", duration
);
858 static void amd_pmc_s2idle_prepare(void)
860 struct amd_pmc_dev
*pdev
= &pmc
;
865 /* Reset and Start SMU logging - to monitor the s0i3 stats */
866 amd_pmc_setup_smu_logging(pdev
);
868 /* Activate CZN specific platform bug workarounds */
869 if (pdev
->cpu_id
== AMD_CPU_ID_CZN
&& !disable_workarounds
) {
870 rc
= amd_pmc_verify_czn_rtc(pdev
, &arg
);
872 dev_err(pdev
->dev
, "failed to set RTC: %d\n", rc
);
877 msg
= amd_pmc_get_os_hint(pdev
);
878 rc
= amd_pmc_send_cmd(pdev
, arg
, NULL
, msg
, false);
880 dev_err(pdev
->dev
, "suspend failed: %d\n", rc
);
884 rc
= amd_pmc_write_stb(pdev
, AMD_PMC_STB_S2IDLE_PREPARE
);
886 dev_err(pdev
->dev
, "error writing to STB: %d\n", rc
);
889 static void amd_pmc_s2idle_check(void)
891 struct amd_pmc_dev
*pdev
= &pmc
;
892 struct smu_metrics table
;
895 /* CZN: Ensure that future s0i3 entry attempts at least 10ms passed */
896 if (pdev
->cpu_id
== AMD_CPU_ID_CZN
&& !get_metrics_table(pdev
, &table
) &&
897 table
.s0i3_last_entry_status
)
898 usleep_range(10000, 20000);
900 /* Dump the IdleMask before we add to the STB */
901 amd_pmc_idlemask_read(pdev
, pdev
->dev
, NULL
);
903 rc
= amd_pmc_write_stb(pdev
, AMD_PMC_STB_S2IDLE_CHECK
);
905 dev_err(pdev
->dev
, "error writing to STB: %d\n", rc
);
908 static int amd_pmc_dump_data(struct amd_pmc_dev
*pdev
)
910 if (pdev
->cpu_id
== AMD_CPU_ID_PCO
)
913 return amd_pmc_send_cmd(pdev
, 0, NULL
, SMU_MSG_LOG_DUMP_DATA
, false);
916 static void amd_pmc_s2idle_restore(void)
918 struct amd_pmc_dev
*pdev
= &pmc
;
922 msg
= amd_pmc_get_os_hint(pdev
);
923 rc
= amd_pmc_send_cmd(pdev
, 0, NULL
, msg
, false);
925 dev_err(pdev
->dev
, "resume failed: %d\n", rc
);
927 /* Let SMU know that we are looking for stats */
928 amd_pmc_dump_data(pdev
);
930 rc
= amd_pmc_write_stb(pdev
, AMD_PMC_STB_S2IDLE_RESTORE
);
932 dev_err(pdev
->dev
, "error writing to STB: %d\n", rc
);
934 /* Notify on failed entry */
935 amd_pmc_validate_deepest(pdev
);
937 amd_pmc_process_restore_quirks(pdev
);
940 static struct acpi_s2idle_dev_ops amd_pmc_s2idle_dev_ops
= {
941 .prepare
= amd_pmc_s2idle_prepare
,
942 .check
= amd_pmc_s2idle_check
,
943 .restore
= amd_pmc_s2idle_restore
,
946 static int amd_pmc_suspend_handler(struct device
*dev
)
948 struct amd_pmc_dev
*pdev
= dev_get_drvdata(dev
);
950 if (pdev
->disable_8042_wakeup
&& !disable_workarounds
) {
951 int rc
= amd_pmc_wa_irq1(pdev
);
954 dev_err(pdev
->dev
, "failed to adjust keyboard wakeup: %d\n", rc
);
962 static DEFINE_SIMPLE_DEV_PM_OPS(amd_pmc_pm
, amd_pmc_suspend_handler
, NULL
);
964 static const struct pci_device_id pmc_pci_ids
[] = {
965 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_PS
) },
966 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_CB
) },
967 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_YC
) },
968 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_CZN
) },
969 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_RN
) },
970 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_PCO
) },
971 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_RV
) },
972 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, AMD_CPU_ID_SP
) },
973 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_1AH_M20H_ROOT
) },
974 { PCI_DEVICE(PCI_VENDOR_ID_AMD
, PCI_DEVICE_ID_AMD_1AH_M60H_ROOT
) },
978 static int amd_pmc_s2d_init(struct amd_pmc_dev
*dev
)
980 u32 phys_addr_low
, phys_addr_hi
;
985 /* Spill to DRAM feature uses separate SMU message port */
988 amd_pmc_send_cmd(dev
, S2D_TELEMETRY_SIZE
, &size
, dev
->s2d_msg_id
, true);
989 if (size
!= S2D_TELEMETRY_BYTES_MAX
)
993 ret
= amd_pmc_send_cmd(dev
, S2D_DRAM_SIZE
, &dev
->dram_size
, dev
->s2d_msg_id
, true);
994 if (ret
|| !dev
->dram_size
)
995 dev
->dram_size
= S2D_TELEMETRY_DRAMBYTES_MAX
;
997 /* Get STB DRAM address */
998 amd_pmc_send_cmd(dev
, S2D_PHYS_ADDR_LOW
, &phys_addr_low
, dev
->s2d_msg_id
, true);
999 amd_pmc_send_cmd(dev
, S2D_PHYS_ADDR_HIGH
, &phys_addr_hi
, dev
->s2d_msg_id
, true);
1001 if (!phys_addr_hi
&& !phys_addr_low
) {
1002 dev_err(dev
->dev
, "STB is not enabled on the system; disable enable_stb or contact system vendor\n");
1006 stb_phys_addr
= ((u64
)phys_addr_hi
<< 32 | phys_addr_low
);
1008 /* Clear msg_port for other SMU operation */
1011 dev
->stb_virt_addr
= devm_ioremap(dev
->dev
, stb_phys_addr
, dev
->dram_size
);
1012 if (!dev
->stb_virt_addr
)
1018 static int amd_pmc_write_stb(struct amd_pmc_dev
*dev
, u32 data
)
1022 err
= amd_smn_write(0, AMD_PMC_STB_PMI_0
, data
);
1024 dev_err(dev
->dev
, "failed to write data in stb: 0x%X\n", AMD_PMC_STB_PMI_0
);
1025 return pcibios_err_to_errno(err
);
1031 static int amd_pmc_read_stb(struct amd_pmc_dev
*dev
, u32
*buf
)
1035 for (i
= 0; i
< FIFO_SIZE
; i
++) {
1036 err
= amd_smn_read(0, AMD_PMC_STB_PMI_0
, buf
++);
1038 dev_err(dev
->dev
, "error reading data from stb: 0x%X\n", AMD_PMC_STB_PMI_0
);
1039 return pcibios_err_to_errno(err
);
1046 static int amd_pmc_probe(struct platform_device
*pdev
)
1048 struct amd_pmc_dev
*dev
= &pmc
;
1049 struct pci_dev
*rdev
;
1050 u32 base_addr_lo
, base_addr_hi
;
1055 dev
->dev
= &pdev
->dev
;
1057 rdev
= pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
1058 if (!rdev
|| !pci_match_id(pmc_pci_ids
, rdev
)) {
1060 goto err_pci_dev_put
;
1063 dev
->cpu_id
= rdev
->device
;
1065 if (dev
->cpu_id
== AMD_CPU_ID_SP
) {
1066 dev_warn_once(dev
->dev
, "S0i3 is not supported on this hardware\n");
1068 goto err_pci_dev_put
;
1072 err
= amd_smn_read(0, AMD_PMC_BASE_ADDR_LO
, &val
);
1074 dev_err(dev
->dev
, "error reading 0x%x\n", AMD_PMC_BASE_ADDR_LO
);
1075 err
= pcibios_err_to_errno(err
);
1076 goto err_pci_dev_put
;
1079 base_addr_lo
= val
& AMD_PMC_BASE_ADDR_HI_MASK
;
1081 err
= amd_smn_read(0, AMD_PMC_BASE_ADDR_HI
, &val
);
1083 dev_err(dev
->dev
, "error reading 0x%x\n", AMD_PMC_BASE_ADDR_HI
);
1084 err
= pcibios_err_to_errno(err
);
1085 goto err_pci_dev_put
;
1088 base_addr_hi
= val
& AMD_PMC_BASE_ADDR_LO_MASK
;
1089 base_addr
= ((u64
)base_addr_hi
<< 32 | base_addr_lo
);
1091 dev
->regbase
= devm_ioremap(dev
->dev
, base_addr
+ AMD_PMC_BASE_ADDR_OFFSET
,
1092 AMD_PMC_MAPPING_SIZE
);
1093 if (!dev
->regbase
) {
1095 goto err_pci_dev_put
;
1098 mutex_init(&dev
->lock
);
1100 /* Get num of IP blocks within the SoC */
1101 amd_pmc_get_ip_info(dev
);
1103 if (enable_stb
&& amd_pmc_is_stb_supported(dev
)) {
1104 err
= amd_pmc_s2d_init(dev
);
1106 goto err_pci_dev_put
;
1109 platform_set_drvdata(pdev
, dev
);
1110 if (IS_ENABLED(CONFIG_SUSPEND
)) {
1111 err
= acpi_register_lps0_dev(&amd_pmc_s2idle_dev_ops
);
1113 dev_warn(dev
->dev
, "failed to register LPS0 sleep handler, expect increased power consumption\n");
1114 if (!disable_workarounds
)
1115 amd_pmc_quirks_init(dev
);
1118 amd_pmc_dbgfs_register(dev
);
1119 if (IS_ENABLED(CONFIG_AMD_MP2_STB
))
1120 amd_mp2_stb_init(dev
);
1121 pm_report_max_hw_sleep(U64_MAX
);
1129 static void amd_pmc_remove(struct platform_device
*pdev
)
1131 struct amd_pmc_dev
*dev
= platform_get_drvdata(pdev
);
1133 if (IS_ENABLED(CONFIG_SUSPEND
))
1134 acpi_unregister_lps0_dev(&amd_pmc_s2idle_dev_ops
);
1135 amd_pmc_dbgfs_unregister(dev
);
1136 pci_dev_put(dev
->rdev
);
1137 if (IS_ENABLED(CONFIG_AMD_MP2_STB
))
1138 amd_mp2_stb_deinit(dev
);
1139 mutex_destroy(&dev
->lock
);
1142 static const struct acpi_device_id amd_pmc_acpi_ids
[] = {
1154 MODULE_DEVICE_TABLE(acpi
, amd_pmc_acpi_ids
);
1156 static struct platform_driver amd_pmc_driver
= {
1159 .acpi_match_table
= amd_pmc_acpi_ids
,
1160 .dev_groups
= pmc_groups
,
1161 .pm
= pm_sleep_ptr(&amd_pmc_pm
),
1163 .probe
= amd_pmc_probe
,
1164 .remove
= amd_pmc_remove
,
1166 module_platform_driver(amd_pmc_driver
);
1168 MODULE_LICENSE("GPL v2");
1169 MODULE_DESCRIPTION("AMD PMC Driver");