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>
31 /* SMU communication registers */
32 #define AMD_PMC_REGISTER_MESSAGE 0x538
33 #define AMD_PMC_REGISTER_RESPONSE 0x980
34 #define AMD_PMC_REGISTER_ARGUMENT 0x9BC
36 /* PMC Scratch Registers */
37 #define AMD_PMC_SCRATCH_REG_CZN 0x94
38 #define AMD_PMC_SCRATCH_REG_YC 0xD14
41 #define AMD_PMC_STB_PMI_0 0x03E30600
42 #define AMD_PMC_STB_S2IDLE_PREPARE 0xC6000001
43 #define AMD_PMC_STB_S2IDLE_RESTORE 0xC6000002
44 #define AMD_PMC_STB_S2IDLE_CHECK 0xC6000003
45 #define AMD_PMC_STB_DUMMY_PC 0xC6000007
47 /* STB S2D(Spill to DRAM) has different message port offset */
48 #define AMD_S2D_REGISTER_MESSAGE 0xA20
49 #define AMD_S2D_REGISTER_RESPONSE 0xA80
50 #define AMD_S2D_REGISTER_ARGUMENT 0xA88
52 /* STB Spill to DRAM Parameters */
53 #define S2D_TELEMETRY_BYTES_MAX 0x100000
54 #define S2D_TELEMETRY_DRAMBYTES_MAX 0x1000000
56 /* Base address of SMU for mapping physical address to virtual address */
57 #define AMD_PMC_MAPPING_SIZE 0x01000
58 #define AMD_PMC_BASE_ADDR_OFFSET 0x10000
59 #define AMD_PMC_BASE_ADDR_LO 0x13B102E8
60 #define AMD_PMC_BASE_ADDR_HI 0x13B102EC
61 #define AMD_PMC_BASE_ADDR_LO_MASK GENMASK(15, 0)
62 #define AMD_PMC_BASE_ADDR_HI_MASK GENMASK(31, 20)
64 /* SMU Response Codes */
65 #define AMD_PMC_RESULT_OK 0x01
66 #define AMD_PMC_RESULT_CMD_REJECT_BUSY 0xFC
67 #define AMD_PMC_RESULT_CMD_REJECT_PREREQ 0xFD
68 #define AMD_PMC_RESULT_CMD_UNKNOWN 0xFE
69 #define AMD_PMC_RESULT_FAILED 0xFF
71 /* FCH SSC Registers */
72 #define FCH_S0I3_ENTRY_TIME_L_OFFSET 0x30
73 #define FCH_S0I3_ENTRY_TIME_H_OFFSET 0x34
74 #define FCH_S0I3_EXIT_TIME_L_OFFSET 0x38
75 #define FCH_S0I3_EXIT_TIME_H_OFFSET 0x3C
76 #define FCH_SSC_MAPPING_SIZE 0x800
77 #define FCH_BASE_PHY_ADDR_LOW 0xFED81100
78 #define FCH_BASE_PHY_ADDR_HIGH 0x00000000
80 /* SMU Message Definations */
81 #define SMU_MSG_GETSMUVERSION 0x02
82 #define SMU_MSG_LOG_GETDRAM_ADDR_HI 0x04
83 #define SMU_MSG_LOG_GETDRAM_ADDR_LO 0x05
84 #define SMU_MSG_LOG_START 0x06
85 #define SMU_MSG_LOG_RESET 0x07
86 #define SMU_MSG_LOG_DUMP_DATA 0x08
87 #define SMU_MSG_GET_SUP_CONSTRAINTS 0x09
88 /* List of supported CPU ids */
89 #define AMD_CPU_ID_RV 0x15D0
90 #define AMD_CPU_ID_RN 0x1630
91 #define AMD_CPU_ID_PCO AMD_CPU_ID_RV
92 #define AMD_CPU_ID_CZN AMD_CPU_ID_RN
93 #define AMD_CPU_ID_YC 0x14B5
94 #define AMD_CPU_ID_CB 0x14D8
95 #define AMD_CPU_ID_PS 0x14E8
96 #define AMD_CPU_ID_SP 0x14A4
98 #define PMC_MSG_DELAY_MIN_US 50
99 #define RESPONSE_REGISTER_LOOP_MAX 20000
101 #define DELAY_MIN_US 2000
102 #define DELAY_MAX_US 3000
103 #define FIFO_SIZE 4096
112 S2D_TELEMETRY_SIZE = 0x01,
119 struct amd_pmc_bit_map {
124 static const struct amd_pmc_bit_map soc15_ip_blk[] = {
150 void __iomem *regbase;
151 void __iomem *smu_virt_addr;
152 void __iomem *stb_virt_addr;
153 void __iomem *fch_virt_addr;
161 /* SMU version information */
167 struct pci_dev *rdev;
168 struct mutex lock; /* generic mutex lock */
169 struct dentry *dbgfs_dir;
172 static bool enable_stb;
173 module_param(enable_stb, bool, 0644);
174 MODULE_PARM_DESC(enable_stb, "Enable the STB debug mechanism");
176 static bool disable_workarounds;
177 module_param(disable_workarounds, bool, 0644);
178 MODULE_PARM_DESC(disable_workarounds, "Disable workarounds for platform bugs");
180 static struct amd_pmc_dev pmc;
181 static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret);
182 static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf);
183 static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data);
185 static inline u32 amd_pmc_reg_read(struct amd_pmc_dev *dev, int reg_offset)
187 return ioread32(dev->regbase + reg_offset);
190 static inline void amd_pmc_reg_write(struct amd_pmc_dev *dev, int reg_offset, u32 val)
192 iowrite32(val, dev->regbase + reg_offset);
198 u32 s0i3_last_entry_status;
200 u64 timeentering_s0i3_lastcapture;
201 u64 timeentering_s0i3_totaltime;
202 u64 timeto_resume_to_os_lastcapture;
203 u64 timeto_resume_to_os_totaltime;
204 u64 timein_s0i3_lastcapture;
205 u64 timein_s0i3_totaltime;
206 u64 timein_swdrips_lastcapture;
207 u64 timein_swdrips_totaltime;
208 u64 timecondition_notmet_lastcapture[32];
209 u64 timecondition_notmet_totaltime[32];
212 static int amd_pmc_stb_debugfs_open(struct inode *inode, struct file *filp)
214 struct amd_pmc_dev *dev = filp->f_inode->i_private;
215 u32 size = FIFO_SIZE * sizeof(u32);
219 buf = kzalloc(size, GFP_KERNEL);
223 rc = amd_pmc_read_stb(dev, buf);
229 filp->private_data = buf;
233 static ssize_t amd_pmc_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,
236 if (!filp->private_data)
239 return simple_read_from_buffer(buf, size, pos, filp->private_data,
240 FIFO_SIZE * sizeof(u32));
243 static int amd_pmc_stb_debugfs_release(struct inode *inode, struct file *filp)
245 kfree(filp->private_data);
249 static const struct file_operations amd_pmc_stb_debugfs_fops = {
250 .owner = THIS_MODULE,
251 .open = amd_pmc_stb_debugfs_open,
252 .read = amd_pmc_stb_debugfs_read,
253 .release = amd_pmc_stb_debugfs_release,
256 static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp)
258 struct amd_pmc_dev *dev = filp->f_inode->i_private;
259 u32 *buf, fsize, num_samples, stb_rdptr_offset = 0;
262 /* Write dummy postcode while reading the STB buffer */
263 ret = amd_pmc_write_stb(dev, AMD_PMC_STB_DUMMY_PC);
265 dev_err(dev->dev, "error writing to STB: %d\n", ret);
267 buf = kzalloc(S2D_TELEMETRY_BYTES_MAX, GFP_KERNEL);
271 /* Spill to DRAM num_samples uses separate SMU message port */
274 /* Get the num_samples to calculate the last push location */
275 ret = amd_pmc_send_cmd(dev, S2D_NUM_SAMPLES, &num_samples, dev->s2d_msg_id, true);
276 /* Clear msg_port for other SMU operation */
279 dev_err(dev->dev, "error: S2D_NUM_SAMPLES not supported : %d\n", ret);
284 /* Start capturing data from the last push location */
285 if (num_samples > S2D_TELEMETRY_BYTES_MAX) {
286 fsize = S2D_TELEMETRY_BYTES_MAX;
287 stb_rdptr_offset = num_samples - fsize;
290 stb_rdptr_offset = 0;
293 memcpy_fromio(buf, dev->stb_virt_addr + stb_rdptr_offset, fsize);
294 filp->private_data = buf;
299 static ssize_t amd_pmc_stb_debugfs_read_v2(struct file *filp, char __user *buf, size_t size,
302 if (!filp->private_data)
305 return simple_read_from_buffer(buf, size, pos, filp->private_data,
306 S2D_TELEMETRY_BYTES_MAX);
309 static int amd_pmc_stb_debugfs_release_v2(struct inode *inode, struct file *filp)
311 kfree(filp->private_data);
315 static const struct file_operations amd_pmc_stb_debugfs_fops_v2 = {
316 .owner = THIS_MODULE,
317 .open = amd_pmc_stb_debugfs_open_v2,
318 .read = amd_pmc_stb_debugfs_read_v2,
319 .release = amd_pmc_stb_debugfs_release_v2,
322 static void amd_pmc_get_ip_info(struct amd_pmc_dev *dev)
324 switch (dev->cpu_id) {
330 dev->s2d_msg_id = 0xBE;
334 dev->s2d_msg_id = 0x85;
339 static int amd_pmc_setup_smu_logging(struct amd_pmc_dev *dev)
341 if (dev->cpu_id == AMD_CPU_ID_PCO) {
342 dev_warn_once(dev->dev, "SMU debugging info not supported on this platform\n");
346 /* Get Active devices list from SMU */
347 if (!dev->active_ips)
348 amd_pmc_send_cmd(dev, 0, &dev->active_ips, SMU_MSG_GET_SUP_CONSTRAINTS, true);
350 /* Get dram address */
351 if (!dev->smu_virt_addr) {
352 u32 phys_addr_low, phys_addr_hi;
355 amd_pmc_send_cmd(dev, 0, &phys_addr_low, SMU_MSG_LOG_GETDRAM_ADDR_LO, true);
356 amd_pmc_send_cmd(dev, 0, &phys_addr_hi, SMU_MSG_LOG_GETDRAM_ADDR_HI, true);
357 smu_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low);
359 dev->smu_virt_addr = devm_ioremap(dev->dev, smu_phys_addr,
360 sizeof(struct smu_metrics));
361 if (!dev->smu_virt_addr)
365 /* Start the logging */
366 amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_RESET, false);
367 amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_START, false);
372 static int get_metrics_table(struct amd_pmc_dev *pdev, struct smu_metrics *table)
374 if (!pdev->smu_virt_addr) {
375 int ret = amd_pmc_setup_smu_logging(pdev);
381 if (pdev->cpu_id == AMD_CPU_ID_PCO)
383 memcpy_fromio(table, pdev->smu_virt_addr, sizeof(struct smu_metrics));
387 static void amd_pmc_validate_deepest(struct amd_pmc_dev *pdev)
389 struct smu_metrics table;
391 if (get_metrics_table(pdev, &table))
394 if (!table.s0i3_last_entry_status)
395 dev_warn(pdev->dev, "Last suspend didn't reach deepest state\n");
396 pm_report_hw_sleep_time(table.s0i3_last_entry_status ?
397 table.timein_s0i3_lastcapture : 0);
400 static int amd_pmc_get_smu_version(struct amd_pmc_dev *dev)
405 if (dev->cpu_id == AMD_CPU_ID_PCO)
408 rc = amd_pmc_send_cmd(dev, 0, &val, SMU_MSG_GETSMUVERSION, true);
412 dev->smu_program = (val >> 24) & GENMASK(7, 0);
413 dev->major = (val >> 16) & GENMASK(7, 0);
414 dev->minor = (val >> 8) & GENMASK(7, 0);
415 dev->rev = (val >> 0) & GENMASK(7, 0);
417 dev_dbg(dev->dev, "SMU program %u version is %u.%u.%u\n",
418 dev->smu_program, dev->major, dev->minor, dev->rev);
423 static ssize_t smu_fw_version_show(struct device *d, struct device_attribute *attr,
426 struct amd_pmc_dev *dev = dev_get_drvdata(d);
429 int rc = amd_pmc_get_smu_version(dev);
434 return sysfs_emit(buf, "%u.%u.%u\n", dev->major, dev->minor, dev->rev);
437 static ssize_t smu_program_show(struct device *d, struct device_attribute *attr,
440 struct amd_pmc_dev *dev = dev_get_drvdata(d);
443 int rc = amd_pmc_get_smu_version(dev);
448 return sysfs_emit(buf, "%u\n", dev->smu_program);
451 static DEVICE_ATTR_RO(smu_fw_version);
452 static DEVICE_ATTR_RO(smu_program);
454 static umode_t pmc_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
456 struct device *dev = kobj_to_dev(kobj);
457 struct amd_pmc_dev *pdev = dev_get_drvdata(dev);
459 if (pdev->cpu_id == AMD_CPU_ID_PCO)
464 static struct attribute *pmc_attrs[] = {
465 &dev_attr_smu_fw_version.attr,
466 &dev_attr_smu_program.attr,
470 static struct attribute_group pmc_attr_group = {
472 .is_visible = pmc_attr_is_visible,
475 static const struct attribute_group *pmc_groups[] = {
480 static int smu_fw_info_show(struct seq_file *s, void *unused)
482 struct amd_pmc_dev *dev = s->private;
483 struct smu_metrics table;
486 if (get_metrics_table(dev, &table))
489 seq_puts(s, "\n=== SMU Statistics ===\n");
490 seq_printf(s, "Table Version: %d\n", table.table_version);
491 seq_printf(s, "Hint Count: %d\n", table.hint_count);
492 seq_printf(s, "Last S0i3 Status: %s\n", table.s0i3_last_entry_status ? "Success" :
494 seq_printf(s, "Time (in us) to S0i3: %lld\n", table.timeentering_s0i3_lastcapture);
495 seq_printf(s, "Time (in us) in S0i3: %lld\n", table.timein_s0i3_lastcapture);
496 seq_printf(s, "Time (in us) to resume from S0i3: %lld\n",
497 table.timeto_resume_to_os_lastcapture);
499 seq_puts(s, "\n=== Active time (in us) ===\n");
500 for (idx = 0 ; idx < dev->num_ips ; idx++) {
501 if (soc15_ip_blk[idx].bit_mask & dev->active_ips)
502 seq_printf(s, "%-8s : %lld\n", soc15_ip_blk[idx].name,
503 table.timecondition_notmet_lastcapture[idx]);
508 DEFINE_SHOW_ATTRIBUTE(smu_fw_info);
510 static int s0ix_stats_show(struct seq_file *s, void *unused)
512 struct amd_pmc_dev *dev = s->private;
513 u64 entry_time, exit_time, residency;
515 /* Use FCH registers to get the S0ix stats */
516 if (!dev->fch_virt_addr) {
517 u32 base_addr_lo = FCH_BASE_PHY_ADDR_LOW;
518 u32 base_addr_hi = FCH_BASE_PHY_ADDR_HIGH;
519 u64 fch_phys_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
521 dev->fch_virt_addr = devm_ioremap(dev->dev, fch_phys_addr, FCH_SSC_MAPPING_SIZE);
522 if (!dev->fch_virt_addr)
526 entry_time = ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_H_OFFSET);
527 entry_time = entry_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_L_OFFSET);
529 exit_time = ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_H_OFFSET);
530 exit_time = exit_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_L_OFFSET);
532 /* It's in 48MHz. We need to convert it */
533 residency = exit_time - entry_time;
534 do_div(residency, 48);
536 seq_puts(s, "=== S0ix statistics ===\n");
537 seq_printf(s, "S0ix Entry Time: %lld\n", entry_time);
538 seq_printf(s, "S0ix Exit Time: %lld\n", exit_time);
539 seq_printf(s, "Residency Time: %lld\n", residency);
543 DEFINE_SHOW_ATTRIBUTE(s0ix_stats);
545 static int amd_pmc_idlemask_read(struct amd_pmc_dev *pdev, struct device *dev,
551 switch (pdev->cpu_id) {
553 /* we haven't yet read SMU version */
555 rc = amd_pmc_get_smu_version(pdev);
559 if (pdev->major > 56 || (pdev->major >= 55 && pdev->minor >= 37))
560 val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_CZN);
567 val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_YC);
574 pm_pr_dbg("SMU idlemask s0i3: 0x%x\n", val);
577 seq_printf(s, "SMU idlemask : 0x%x\n", val);
582 static int amd_pmc_idlemask_show(struct seq_file *s, void *unused)
584 return amd_pmc_idlemask_read(s->private, NULL, s);
586 DEFINE_SHOW_ATTRIBUTE(amd_pmc_idlemask);
588 static void amd_pmc_dbgfs_unregister(struct amd_pmc_dev *dev)
590 debugfs_remove_recursive(dev->dbgfs_dir);
593 static bool amd_pmc_is_stb_supported(struct amd_pmc_dev *dev)
595 switch (dev->cpu_id) {
605 static void amd_pmc_dbgfs_register(struct amd_pmc_dev *dev)
607 dev->dbgfs_dir = debugfs_create_dir("amd_pmc", NULL);
608 debugfs_create_file("smu_fw_info", 0644, dev->dbgfs_dir, dev,
610 debugfs_create_file("s0ix_stats", 0644, dev->dbgfs_dir, dev,
612 debugfs_create_file("amd_pmc_idlemask", 0644, dev->dbgfs_dir, dev,
613 &amd_pmc_idlemask_fops);
614 /* Enable STB only when the module_param is set */
616 if (amd_pmc_is_stb_supported(dev))
617 debugfs_create_file("stb_read", 0644, dev->dbgfs_dir, dev,
618 &amd_pmc_stb_debugfs_fops_v2);
620 debugfs_create_file("stb_read", 0644, dev->dbgfs_dir, dev,
621 &amd_pmc_stb_debugfs_fops);
625 static void amd_pmc_dump_registers(struct amd_pmc_dev *dev)
627 u32 value, message, argument, response;
630 message = AMD_S2D_REGISTER_MESSAGE;
631 argument = AMD_S2D_REGISTER_ARGUMENT;
632 response = AMD_S2D_REGISTER_RESPONSE;
634 message = AMD_PMC_REGISTER_MESSAGE;
635 argument = AMD_PMC_REGISTER_ARGUMENT;
636 response = AMD_PMC_REGISTER_RESPONSE;
639 value = amd_pmc_reg_read(dev, response);
640 dev_dbg(dev->dev, "AMD_%s_REGISTER_RESPONSE:%x\n", dev->msg_port ? "S2D" : "PMC", value);
642 value = amd_pmc_reg_read(dev, argument);
643 dev_dbg(dev->dev, "AMD_%s_REGISTER_ARGUMENT:%x\n", dev->msg_port ? "S2D" : "PMC", value);
645 value = amd_pmc_reg_read(dev, message);
646 dev_dbg(dev->dev, "AMD_%s_REGISTER_MESSAGE:%x\n", dev->msg_port ? "S2D" : "PMC", value);
649 static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret)
652 u32 val, message, argument, response;
654 mutex_lock(&dev->lock);
657 message = AMD_S2D_REGISTER_MESSAGE;
658 argument = AMD_S2D_REGISTER_ARGUMENT;
659 response = AMD_S2D_REGISTER_RESPONSE;
661 message = AMD_PMC_REGISTER_MESSAGE;
662 argument = AMD_PMC_REGISTER_ARGUMENT;
663 response = AMD_PMC_REGISTER_RESPONSE;
666 /* Wait until we get a valid response */
667 rc = readx_poll_timeout(ioread32, dev->regbase + response,
668 val, val != 0, PMC_MSG_DELAY_MIN_US,
669 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
671 dev_err(dev->dev, "failed to talk to SMU\n");
675 /* Write zero to response register */
676 amd_pmc_reg_write(dev, response, 0);
678 /* Write argument into response register */
679 amd_pmc_reg_write(dev, argument, arg);
681 /* Write message ID to message ID register */
682 amd_pmc_reg_write(dev, message, msg);
684 /* Wait until we get a valid response */
685 rc = readx_poll_timeout(ioread32, dev->regbase + response,
686 val, val != 0, PMC_MSG_DELAY_MIN_US,
687 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
689 dev_err(dev->dev, "SMU response timed out\n");
694 case AMD_PMC_RESULT_OK:
696 /* PMFW may take longer time to return back the data */
697 usleep_range(DELAY_MIN_US, 10 * DELAY_MAX_US);
698 *data = amd_pmc_reg_read(dev, argument);
701 case AMD_PMC_RESULT_CMD_REJECT_BUSY:
702 dev_err(dev->dev, "SMU not ready. err: 0x%x\n", val);
705 case AMD_PMC_RESULT_CMD_UNKNOWN:
706 dev_err(dev->dev, "SMU cmd unknown. err: 0x%x\n", val);
709 case AMD_PMC_RESULT_CMD_REJECT_PREREQ:
710 case AMD_PMC_RESULT_FAILED:
712 dev_err(dev->dev, "SMU cmd failed. err: 0x%x\n", val);
718 mutex_unlock(&dev->lock);
719 amd_pmc_dump_registers(dev);
723 static int amd_pmc_get_os_hint(struct amd_pmc_dev *dev)
725 switch (dev->cpu_id) {
727 return MSG_OS_HINT_PCO;
732 return MSG_OS_HINT_RN;
737 static int amd_pmc_czn_wa_irq1(struct amd_pmc_dev *pdev)
743 rc = amd_pmc_get_smu_version(pdev);
748 if (pdev->major > 64 || (pdev->major == 64 && pdev->minor > 65))
751 d = bus_find_device_by_name(&serio_bus, NULL, "serio0");
754 if (device_may_wakeup(d)) {
755 dev_info_once(d, "Disabling IRQ1 wakeup source to avoid platform firmware bug\n");
757 device_set_wakeup_enable(d, false);
764 static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg)
766 struct rtc_device *rtc_device;
767 time64_t then, now, duration;
768 struct rtc_wkalrm alarm;
772 /* we haven't yet read SMU version */
774 rc = amd_pmc_get_smu_version(pdev);
779 if (pdev->major < 64 || (pdev->major == 64 && pdev->minor < 53))
782 rtc_device = rtc_class_open("rtc0");
785 rc = rtc_read_alarm(rtc_device, &alarm);
788 if (!alarm.enabled) {
789 dev_dbg(pdev->dev, "alarm not enabled\n");
792 rc = rtc_read_time(rtc_device, &tm);
795 then = rtc_tm_to_time64(&alarm.time);
796 now = rtc_tm_to_time64(&tm);
803 /* will be stored in upper 16 bits of s0i3 hint argument,
804 * so timer wakeup from s0i3 is limited to ~18 hours or less
806 if (duration <= 4 || duration > U16_MAX)
809 *arg |= (duration << 16);
810 rc = rtc_alarm_irq_enable(rtc_device, 0);
811 pm_pr_dbg("wakeup timer programmed for %lld seconds\n", duration);
816 static void amd_pmc_s2idle_prepare(void)
818 struct amd_pmc_dev *pdev = &pmc;
823 /* Reset and Start SMU logging - to monitor the s0i3 stats */
824 amd_pmc_setup_smu_logging(pdev);
826 /* Activate CZN specific platform bug workarounds */
827 if (pdev->cpu_id == AMD_CPU_ID_CZN && !disable_workarounds) {
828 rc = amd_pmc_verify_czn_rtc(pdev, &arg);
830 dev_err(pdev->dev, "failed to set RTC: %d\n", rc);
835 msg = amd_pmc_get_os_hint(pdev);
836 rc = amd_pmc_send_cmd(pdev, arg, NULL, msg, false);
838 dev_err(pdev->dev, "suspend failed: %d\n", rc);
842 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_PREPARE);
844 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
847 static void amd_pmc_s2idle_check(void)
849 struct amd_pmc_dev *pdev = &pmc;
850 struct smu_metrics table;
853 /* CZN: Ensure that future s0i3 entry attempts at least 10ms passed */
854 if (pdev->cpu_id == AMD_CPU_ID_CZN && !get_metrics_table(pdev, &table) &&
855 table.s0i3_last_entry_status)
856 usleep_range(10000, 20000);
858 /* Dump the IdleMask before we add to the STB */
859 amd_pmc_idlemask_read(pdev, pdev->dev, NULL);
861 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_CHECK);
863 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
866 static int amd_pmc_dump_data(struct amd_pmc_dev *pdev)
868 if (pdev->cpu_id == AMD_CPU_ID_PCO)
871 return amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_DUMP_DATA, false);
874 static void amd_pmc_s2idle_restore(void)
876 struct amd_pmc_dev *pdev = &pmc;
880 msg = amd_pmc_get_os_hint(pdev);
881 rc = amd_pmc_send_cmd(pdev, 0, NULL, msg, false);
883 dev_err(pdev->dev, "resume failed: %d\n", rc);
885 /* Let SMU know that we are looking for stats */
886 amd_pmc_dump_data(pdev);
888 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_RESTORE);
890 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
892 /* Notify on failed entry */
893 amd_pmc_validate_deepest(pdev);
896 static struct acpi_s2idle_dev_ops amd_pmc_s2idle_dev_ops = {
897 .prepare = amd_pmc_s2idle_prepare,
898 .check = amd_pmc_s2idle_check,
899 .restore = amd_pmc_s2idle_restore,
902 static int amd_pmc_suspend_handler(struct device *dev)
904 struct amd_pmc_dev *pdev = dev_get_drvdata(dev);
906 if (pdev->cpu_id == AMD_CPU_ID_CZN && !disable_workarounds) {
907 int rc = amd_pmc_czn_wa_irq1(pdev);
910 dev_err(pdev->dev, "failed to adjust keyboard wakeup: %d\n", rc);
918 static DEFINE_SIMPLE_DEV_PM_OPS(amd_pmc_pm, amd_pmc_suspend_handler, NULL);
920 static const struct pci_device_id pmc_pci_ids[] = {
921 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PS) },
922 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CB) },
923 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_YC) },
924 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CZN) },
925 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RN) },
926 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PCO) },
927 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RV) },
928 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_SP) },
932 static int amd_pmc_get_dram_size(struct amd_pmc_dev *dev)
936 switch (dev->cpu_id) {
938 if (!(dev->major > 90 || (dev->major == 90 && dev->minor > 39))) {
948 ret = amd_pmc_send_cmd(dev, S2D_DRAM_SIZE, &dev->dram_size, dev->s2d_msg_id, true);
949 if (ret || !dev->dram_size)
955 dev_err(dev->dev, "DRAM size command not supported for this platform\n");
959 static int amd_pmc_s2d_init(struct amd_pmc_dev *dev)
961 u32 phys_addr_low, phys_addr_hi;
966 /* Spill to DRAM feature uses separate SMU message port */
969 /* Get num of IP blocks within the SoC */
970 amd_pmc_get_ip_info(dev);
972 amd_pmc_send_cmd(dev, S2D_TELEMETRY_SIZE, &size, dev->s2d_msg_id, true);
973 if (size != S2D_TELEMETRY_BYTES_MAX)
977 ret = amd_pmc_get_dram_size(dev);
979 dev->dram_size = S2D_TELEMETRY_DRAMBYTES_MAX;
981 /* Get STB DRAM address */
982 amd_pmc_send_cmd(dev, S2D_PHYS_ADDR_LOW, &phys_addr_low, dev->s2d_msg_id, true);
983 amd_pmc_send_cmd(dev, S2D_PHYS_ADDR_HIGH, &phys_addr_hi, dev->s2d_msg_id, true);
985 stb_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low);
987 /* Clear msg_port for other SMU operation */
990 dev->stb_virt_addr = devm_ioremap(dev->dev, stb_phys_addr, dev->dram_size);
991 if (!dev->stb_virt_addr)
997 static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data)
1001 err = amd_smn_write(0, AMD_PMC_STB_PMI_0, data);
1003 dev_err(dev->dev, "failed to write data in stb: 0x%X\n", AMD_PMC_STB_PMI_0);
1004 return pcibios_err_to_errno(err);
1010 static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf)
1014 for (i = 0; i < FIFO_SIZE; i++) {
1015 err = amd_smn_read(0, AMD_PMC_STB_PMI_0, buf++);
1017 dev_err(dev->dev, "error reading data from stb: 0x%X\n", AMD_PMC_STB_PMI_0);
1018 return pcibios_err_to_errno(err);
1025 static int amd_pmc_probe(struct platform_device *pdev)
1027 struct amd_pmc_dev *dev = &pmc;
1028 struct pci_dev *rdev;
1029 u32 base_addr_lo, base_addr_hi;
1034 dev->dev = &pdev->dev;
1036 rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
1037 if (!rdev || !pci_match_id(pmc_pci_ids, rdev)) {
1039 goto err_pci_dev_put;
1042 dev->cpu_id = rdev->device;
1044 if (dev->cpu_id == AMD_CPU_ID_SP) {
1045 dev_warn_once(dev->dev, "S0i3 is not supported on this hardware\n");
1047 goto err_pci_dev_put;
1051 err = amd_smn_read(0, AMD_PMC_BASE_ADDR_LO, &val);
1053 dev_err(dev->dev, "error reading 0x%x\n", AMD_PMC_BASE_ADDR_LO);
1054 err = pcibios_err_to_errno(err);
1055 goto err_pci_dev_put;
1058 base_addr_lo = val & AMD_PMC_BASE_ADDR_HI_MASK;
1060 err = amd_smn_read(0, AMD_PMC_BASE_ADDR_HI, &val);
1062 dev_err(dev->dev, "error reading 0x%x\n", AMD_PMC_BASE_ADDR_HI);
1063 err = pcibios_err_to_errno(err);
1064 goto err_pci_dev_put;
1067 base_addr_hi = val & AMD_PMC_BASE_ADDR_LO_MASK;
1068 base_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
1070 dev->regbase = devm_ioremap(dev->dev, base_addr + AMD_PMC_BASE_ADDR_OFFSET,
1071 AMD_PMC_MAPPING_SIZE);
1072 if (!dev->regbase) {
1074 goto err_pci_dev_put;
1077 mutex_init(&dev->lock);
1079 if (enable_stb && amd_pmc_is_stb_supported(dev)) {
1080 err = amd_pmc_s2d_init(dev);
1082 goto err_pci_dev_put;
1085 platform_set_drvdata(pdev, dev);
1086 if (IS_ENABLED(CONFIG_SUSPEND)) {
1087 err = acpi_register_lps0_dev(&amd_pmc_s2idle_dev_ops);
1089 dev_warn(dev->dev, "failed to register LPS0 sleep handler, expect increased power consumption\n");
1092 amd_pmc_dbgfs_register(dev);
1093 pm_report_max_hw_sleep(U64_MAX);
1101 static void amd_pmc_remove(struct platform_device *pdev)
1103 struct amd_pmc_dev *dev = platform_get_drvdata(pdev);
1105 if (IS_ENABLED(CONFIG_SUSPEND))
1106 acpi_unregister_lps0_dev(&amd_pmc_s2idle_dev_ops);
1107 amd_pmc_dbgfs_unregister(dev);
1108 pci_dev_put(dev->rdev);
1109 mutex_destroy(&dev->lock);
1112 static const struct acpi_device_id amd_pmc_acpi_ids[] = {
1122 MODULE_DEVICE_TABLE(acpi, amd_pmc_acpi_ids);
1124 static struct platform_driver amd_pmc_driver = {
1127 .acpi_match_table = amd_pmc_acpi_ids,
1128 .dev_groups = pmc_groups,
1129 .pm = pm_sleep_ptr(&amd_pmc_pm),
1131 .probe = amd_pmc_probe,
1132 .remove_new = amd_pmc_remove,
1134 module_platform_driver(amd_pmc_driver);
1136 MODULE_LICENSE("GPL v2");
1137 MODULE_DESCRIPTION("AMD PMC Driver");