1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AMD SoC Power Management Controller Driver
5 * Copyright (c) 2020, Advanced Micro Devices, Inc.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/acpi.h>
14 #include <linux/bitfield.h>
15 #include <linux/bits.h>
16 #include <linux/debugfs.h>
17 #include <linux/delay.h>
19 #include <linux/iopoll.h>
20 #include <linux/limits.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/rtc.h>
25 #include <linux/serio.h>
26 #include <linux/suspend.h>
27 #include <linux/seq_file.h>
28 #include <linux/uaccess.h>
30 /* SMU communication registers */
31 #define AMD_PMC_REGISTER_MESSAGE 0x538
32 #define AMD_PMC_REGISTER_RESPONSE 0x980
33 #define AMD_PMC_REGISTER_ARGUMENT 0x9BC
35 /* PMC Scratch Registers */
36 #define AMD_PMC_SCRATCH_REG_CZN 0x94
37 #define AMD_PMC_SCRATCH_REG_YC 0xD14
40 #define AMD_PMC_STB_INDEX_ADDRESS 0xF8
41 #define AMD_PMC_STB_INDEX_DATA 0xFC
42 #define AMD_PMC_STB_PMI_0 0x03E30600
43 #define AMD_PMC_STB_S2IDLE_PREPARE 0xC6000001
44 #define AMD_PMC_STB_S2IDLE_RESTORE 0xC6000002
45 #define AMD_PMC_STB_S2IDLE_CHECK 0xC6000003
46 #define AMD_PMC_STB_DUMMY_PC 0xC6000007
48 /* STB S2D(Spill to DRAM) has different message port offset */
49 #define STB_SPILL_TO_DRAM 0xBE
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 0x100000
56 #define S2D_TELEMETRY_DRAMBYTES_MAX 0x1000000
58 /* Base address of SMU for mapping physical address to virtual address */
59 #define AMD_PMC_SMU_INDEX_ADDRESS 0xB8
60 #define AMD_PMC_SMU_INDEX_DATA 0xBC
61 #define AMD_PMC_MAPPING_SIZE 0x01000
62 #define AMD_PMC_BASE_ADDR_OFFSET 0x10000
63 #define AMD_PMC_BASE_ADDR_LO 0x13B102E8
64 #define AMD_PMC_BASE_ADDR_HI 0x13B102EC
65 #define AMD_PMC_BASE_ADDR_LO_MASK GENMASK(15, 0)
66 #define AMD_PMC_BASE_ADDR_HI_MASK GENMASK(31, 20)
68 /* SMU Response Codes */
69 #define AMD_PMC_RESULT_OK 0x01
70 #define AMD_PMC_RESULT_CMD_REJECT_BUSY 0xFC
71 #define AMD_PMC_RESULT_CMD_REJECT_PREREQ 0xFD
72 #define AMD_PMC_RESULT_CMD_UNKNOWN 0xFE
73 #define AMD_PMC_RESULT_FAILED 0xFF
75 /* FCH SSC Registers */
76 #define FCH_S0I3_ENTRY_TIME_L_OFFSET 0x30
77 #define FCH_S0I3_ENTRY_TIME_H_OFFSET 0x34
78 #define FCH_S0I3_EXIT_TIME_L_OFFSET 0x38
79 #define FCH_S0I3_EXIT_TIME_H_OFFSET 0x3C
80 #define FCH_SSC_MAPPING_SIZE 0x800
81 #define FCH_BASE_PHY_ADDR_LOW 0xFED81100
82 #define FCH_BASE_PHY_ADDR_HIGH 0x00000000
84 /* SMU Message Definations */
85 #define SMU_MSG_GETSMUVERSION 0x02
86 #define SMU_MSG_LOG_GETDRAM_ADDR_HI 0x04
87 #define SMU_MSG_LOG_GETDRAM_ADDR_LO 0x05
88 #define SMU_MSG_LOG_START 0x06
89 #define SMU_MSG_LOG_RESET 0x07
90 #define SMU_MSG_LOG_DUMP_DATA 0x08
91 #define SMU_MSG_GET_SUP_CONSTRAINTS 0x09
92 /* List of supported CPU ids */
93 #define AMD_CPU_ID_RV 0x15D0
94 #define AMD_CPU_ID_RN 0x1630
95 #define AMD_CPU_ID_PCO AMD_CPU_ID_RV
96 #define AMD_CPU_ID_CZN AMD_CPU_ID_RN
97 #define AMD_CPU_ID_YC 0x14B5
98 #define AMD_CPU_ID_CB 0x14D8
99 #define AMD_CPU_ID_PS 0x14E8
101 #define PMC_MSG_DELAY_MIN_US 50
102 #define RESPONSE_REGISTER_LOOP_MAX 20000
104 #define SOC_SUBSYSTEM_IP_MAX 12
105 #define DELAY_MIN_US 2000
106 #define DELAY_MAX_US 3000
107 #define FIFO_SIZE 4096
116 S2D_TELEMETRY_SIZE = 0x01,
122 struct amd_pmc_bit_map {
127 static const struct amd_pmc_bit_map soc15_ip_blk[] = {
144 void __iomem *regbase;
145 void __iomem *smu_virt_addr;
146 void __iomem *stb_virt_addr;
147 void __iomem *fch_virt_addr;
152 /* SMU version information */
158 struct pci_dev *rdev;
159 struct mutex lock; /* generic mutex lock */
160 struct dentry *dbgfs_dir;
163 static bool enable_stb;
164 module_param(enable_stb, bool, 0644);
165 MODULE_PARM_DESC(enable_stb, "Enable the STB debug mechanism");
167 static bool disable_workarounds;
168 module_param(disable_workarounds, bool, 0644);
169 MODULE_PARM_DESC(disable_workarounds, "Disable workarounds for platform bugs");
171 static struct amd_pmc_dev pmc;
172 static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret);
173 static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf);
174 static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data);
176 static inline u32 amd_pmc_reg_read(struct amd_pmc_dev *dev, int reg_offset)
178 return ioread32(dev->regbase + reg_offset);
181 static inline void amd_pmc_reg_write(struct amd_pmc_dev *dev, int reg_offset, u32 val)
183 iowrite32(val, dev->regbase + reg_offset);
189 u32 s0i3_last_entry_status;
191 u64 timeentering_s0i3_lastcapture;
192 u64 timeentering_s0i3_totaltime;
193 u64 timeto_resume_to_os_lastcapture;
194 u64 timeto_resume_to_os_totaltime;
195 u64 timein_s0i3_lastcapture;
196 u64 timein_s0i3_totaltime;
197 u64 timein_swdrips_lastcapture;
198 u64 timein_swdrips_totaltime;
199 u64 timecondition_notmet_lastcapture[SOC_SUBSYSTEM_IP_MAX];
200 u64 timecondition_notmet_totaltime[SOC_SUBSYSTEM_IP_MAX];
203 static int amd_pmc_stb_debugfs_open(struct inode *inode, struct file *filp)
205 struct amd_pmc_dev *dev = filp->f_inode->i_private;
206 u32 size = FIFO_SIZE * sizeof(u32);
210 buf = kzalloc(size, GFP_KERNEL);
214 rc = amd_pmc_read_stb(dev, buf);
220 filp->private_data = buf;
224 static ssize_t amd_pmc_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,
227 if (!filp->private_data)
230 return simple_read_from_buffer(buf, size, pos, filp->private_data,
231 FIFO_SIZE * sizeof(u32));
234 static int amd_pmc_stb_debugfs_release(struct inode *inode, struct file *filp)
236 kfree(filp->private_data);
240 static const struct file_operations amd_pmc_stb_debugfs_fops = {
241 .owner = THIS_MODULE,
242 .open = amd_pmc_stb_debugfs_open,
243 .read = amd_pmc_stb_debugfs_read,
244 .release = amd_pmc_stb_debugfs_release,
247 static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp)
249 struct amd_pmc_dev *dev = filp->f_inode->i_private;
250 u32 *buf, fsize, num_samples, stb_rdptr_offset = 0;
253 /* Write dummy postcode while reading the STB buffer */
254 ret = amd_pmc_write_stb(dev, AMD_PMC_STB_DUMMY_PC);
256 dev_err(dev->dev, "error writing to STB: %d\n", ret);
258 buf = kzalloc(S2D_TELEMETRY_BYTES_MAX, GFP_KERNEL);
262 /* Spill to DRAM num_samples uses separate SMU message port */
265 /* Get the num_samples to calculate the last push location */
266 ret = amd_pmc_send_cmd(dev, S2D_NUM_SAMPLES, &num_samples, STB_SPILL_TO_DRAM, 1);
267 /* Clear msg_port for other SMU operation */
270 dev_err(dev->dev, "error: S2D_NUM_SAMPLES not supported : %d\n", ret);
274 /* Start capturing data from the last push location */
275 if (num_samples > S2D_TELEMETRY_BYTES_MAX) {
276 fsize = S2D_TELEMETRY_BYTES_MAX;
277 stb_rdptr_offset = num_samples - fsize;
280 stb_rdptr_offset = 0;
283 memcpy_fromio(buf, dev->stb_virt_addr + stb_rdptr_offset, fsize);
284 filp->private_data = buf;
289 static ssize_t amd_pmc_stb_debugfs_read_v2(struct file *filp, char __user *buf, size_t size,
292 if (!filp->private_data)
295 return simple_read_from_buffer(buf, size, pos, filp->private_data,
296 S2D_TELEMETRY_BYTES_MAX);
299 static int amd_pmc_stb_debugfs_release_v2(struct inode *inode, struct file *filp)
301 kfree(filp->private_data);
305 static const struct file_operations amd_pmc_stb_debugfs_fops_v2 = {
306 .owner = THIS_MODULE,
307 .open = amd_pmc_stb_debugfs_open_v2,
308 .read = amd_pmc_stb_debugfs_read_v2,
309 .release = amd_pmc_stb_debugfs_release_v2,
312 static int amd_pmc_setup_smu_logging(struct amd_pmc_dev *dev)
314 if (dev->cpu_id == AMD_CPU_ID_PCO) {
315 dev_warn_once(dev->dev, "SMU debugging info not supported on this platform\n");
319 /* Get Active devices list from SMU */
320 if (!dev->active_ips)
321 amd_pmc_send_cmd(dev, 0, &dev->active_ips, SMU_MSG_GET_SUP_CONSTRAINTS, 1);
323 /* Get dram address */
324 if (!dev->smu_virt_addr) {
325 u32 phys_addr_low, phys_addr_hi;
328 amd_pmc_send_cmd(dev, 0, &phys_addr_low, SMU_MSG_LOG_GETDRAM_ADDR_LO, 1);
329 amd_pmc_send_cmd(dev, 0, &phys_addr_hi, SMU_MSG_LOG_GETDRAM_ADDR_HI, 1);
330 smu_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low);
332 dev->smu_virt_addr = devm_ioremap(dev->dev, smu_phys_addr,
333 sizeof(struct smu_metrics));
334 if (!dev->smu_virt_addr)
338 /* Start the logging */
339 amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_RESET, 0);
340 amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_START, 0);
345 static int amd_pmc_idlemask_read(struct amd_pmc_dev *pdev, struct device *dev,
350 switch (pdev->cpu_id) {
352 val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_CZN);
357 val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_YC);
364 dev_dbg(pdev->dev, "SMU idlemask s0i3: 0x%x\n", val);
367 seq_printf(s, "SMU idlemask : 0x%x\n", val);
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");
397 dev_dbg(pdev->dev, "Last suspend in deepest state for %lluus\n",
398 table.timein_s0i3_lastcapture);
401 static int amd_pmc_get_smu_version(struct amd_pmc_dev *dev)
406 rc = amd_pmc_send_cmd(dev, 0, &val, SMU_MSG_GETSMUVERSION, 1);
410 dev->smu_program = (val >> 24) & GENMASK(7, 0);
411 dev->major = (val >> 16) & GENMASK(7, 0);
412 dev->minor = (val >> 8) & GENMASK(7, 0);
413 dev->rev = (val >> 0) & GENMASK(7, 0);
415 dev_dbg(dev->dev, "SMU program %u version is %u.%u.%u\n",
416 dev->smu_program, dev->major, dev->minor, dev->rev);
421 static ssize_t smu_fw_version_show(struct device *d, struct device_attribute *attr,
424 struct amd_pmc_dev *dev = dev_get_drvdata(d);
427 int rc = amd_pmc_get_smu_version(dev);
432 return sysfs_emit(buf, "%u.%u.%u\n", dev->major, dev->minor, dev->rev);
435 static ssize_t smu_program_show(struct device *d, struct device_attribute *attr,
438 struct amd_pmc_dev *dev = dev_get_drvdata(d);
441 int rc = amd_pmc_get_smu_version(dev);
446 return sysfs_emit(buf, "%u\n", dev->smu_program);
449 static DEVICE_ATTR_RO(smu_fw_version);
450 static DEVICE_ATTR_RO(smu_program);
452 static struct attribute *pmc_attrs[] = {
453 &dev_attr_smu_fw_version.attr,
454 &dev_attr_smu_program.attr,
457 ATTRIBUTE_GROUPS(pmc);
459 static int smu_fw_info_show(struct seq_file *s, void *unused)
461 struct amd_pmc_dev *dev = s->private;
462 struct smu_metrics table;
465 if (get_metrics_table(dev, &table))
468 seq_puts(s, "\n=== SMU Statistics ===\n");
469 seq_printf(s, "Table Version: %d\n", table.table_version);
470 seq_printf(s, "Hint Count: %d\n", table.hint_count);
471 seq_printf(s, "Last S0i3 Status: %s\n", table.s0i3_last_entry_status ? "Success" :
473 seq_printf(s, "Time (in us) to S0i3: %lld\n", table.timeentering_s0i3_lastcapture);
474 seq_printf(s, "Time (in us) in S0i3: %lld\n", table.timein_s0i3_lastcapture);
475 seq_printf(s, "Time (in us) to resume from S0i3: %lld\n",
476 table.timeto_resume_to_os_lastcapture);
478 seq_puts(s, "\n=== Active time (in us) ===\n");
479 for (idx = 0 ; idx < SOC_SUBSYSTEM_IP_MAX ; idx++) {
480 if (soc15_ip_blk[idx].bit_mask & dev->active_ips)
481 seq_printf(s, "%-8s : %lld\n", soc15_ip_blk[idx].name,
482 table.timecondition_notmet_lastcapture[idx]);
487 DEFINE_SHOW_ATTRIBUTE(smu_fw_info);
489 static int s0ix_stats_show(struct seq_file *s, void *unused)
491 struct amd_pmc_dev *dev = s->private;
492 u64 entry_time, exit_time, residency;
494 /* Use FCH registers to get the S0ix stats */
495 if (!dev->fch_virt_addr) {
496 u32 base_addr_lo = FCH_BASE_PHY_ADDR_LOW;
497 u32 base_addr_hi = FCH_BASE_PHY_ADDR_HIGH;
498 u64 fch_phys_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
500 dev->fch_virt_addr = devm_ioremap(dev->dev, fch_phys_addr, FCH_SSC_MAPPING_SIZE);
501 if (!dev->fch_virt_addr)
505 entry_time = ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_H_OFFSET);
506 entry_time = entry_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_L_OFFSET);
508 exit_time = ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_H_OFFSET);
509 exit_time = exit_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_L_OFFSET);
511 /* It's in 48MHz. We need to convert it */
512 residency = exit_time - entry_time;
513 do_div(residency, 48);
515 seq_puts(s, "=== S0ix statistics ===\n");
516 seq_printf(s, "S0ix Entry Time: %lld\n", entry_time);
517 seq_printf(s, "S0ix Exit Time: %lld\n", exit_time);
518 seq_printf(s, "Residency Time: %lld\n", residency);
522 DEFINE_SHOW_ATTRIBUTE(s0ix_stats);
524 static int amd_pmc_idlemask_show(struct seq_file *s, void *unused)
526 struct amd_pmc_dev *dev = s->private;
529 /* we haven't yet read SMU version */
531 rc = amd_pmc_get_smu_version(dev);
536 if (dev->major > 56 || (dev->major >= 55 && dev->minor >= 37)) {
537 rc = amd_pmc_idlemask_read(dev, NULL, s);
541 seq_puts(s, "Unsupported SMU version for Idlemask\n");
546 DEFINE_SHOW_ATTRIBUTE(amd_pmc_idlemask);
548 static void amd_pmc_dbgfs_unregister(struct amd_pmc_dev *dev)
550 debugfs_remove_recursive(dev->dbgfs_dir);
553 static void amd_pmc_dbgfs_register(struct amd_pmc_dev *dev)
555 dev->dbgfs_dir = debugfs_create_dir("amd_pmc", NULL);
556 debugfs_create_file("smu_fw_info", 0644, dev->dbgfs_dir, dev,
558 debugfs_create_file("s0ix_stats", 0644, dev->dbgfs_dir, dev,
560 debugfs_create_file("amd_pmc_idlemask", 0644, dev->dbgfs_dir, dev,
561 &amd_pmc_idlemask_fops);
562 /* Enable STB only when the module_param is set */
564 if (dev->cpu_id == AMD_CPU_ID_YC || dev->cpu_id == AMD_CPU_ID_CB ||
565 dev->cpu_id == AMD_CPU_ID_PS)
566 debugfs_create_file("stb_read", 0644, dev->dbgfs_dir, dev,
567 &amd_pmc_stb_debugfs_fops_v2);
569 debugfs_create_file("stb_read", 0644, dev->dbgfs_dir, dev,
570 &amd_pmc_stb_debugfs_fops);
574 static void amd_pmc_dump_registers(struct amd_pmc_dev *dev)
576 u32 value, message, argument, response;
579 message = AMD_S2D_REGISTER_MESSAGE;
580 argument = AMD_S2D_REGISTER_ARGUMENT;
581 response = AMD_S2D_REGISTER_RESPONSE;
583 message = AMD_PMC_REGISTER_MESSAGE;
584 argument = AMD_PMC_REGISTER_ARGUMENT;
585 response = AMD_PMC_REGISTER_RESPONSE;
588 value = amd_pmc_reg_read(dev, response);
589 dev_dbg(dev->dev, "AMD_%s_REGISTER_RESPONSE:%x\n", dev->msg_port ? "S2D" : "PMC", value);
591 value = amd_pmc_reg_read(dev, argument);
592 dev_dbg(dev->dev, "AMD_%s_REGISTER_ARGUMENT:%x\n", dev->msg_port ? "S2D" : "PMC", value);
594 value = amd_pmc_reg_read(dev, message);
595 dev_dbg(dev->dev, "AMD_%s_REGISTER_MESSAGE:%x\n", dev->msg_port ? "S2D" : "PMC", value);
598 static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret)
601 u32 val, message, argument, response;
603 mutex_lock(&dev->lock);
606 message = AMD_S2D_REGISTER_MESSAGE;
607 argument = AMD_S2D_REGISTER_ARGUMENT;
608 response = AMD_S2D_REGISTER_RESPONSE;
610 message = AMD_PMC_REGISTER_MESSAGE;
611 argument = AMD_PMC_REGISTER_ARGUMENT;
612 response = AMD_PMC_REGISTER_RESPONSE;
615 /* Wait until we get a valid response */
616 rc = readx_poll_timeout(ioread32, dev->regbase + response,
617 val, val != 0, PMC_MSG_DELAY_MIN_US,
618 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
620 dev_err(dev->dev, "failed to talk to SMU\n");
624 /* Write zero to response register */
625 amd_pmc_reg_write(dev, response, 0);
627 /* Write argument into response register */
628 amd_pmc_reg_write(dev, argument, arg);
630 /* Write message ID to message ID register */
631 amd_pmc_reg_write(dev, message, msg);
633 /* Wait until we get a valid response */
634 rc = readx_poll_timeout(ioread32, dev->regbase + response,
635 val, val != 0, PMC_MSG_DELAY_MIN_US,
636 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
638 dev_err(dev->dev, "SMU response timed out\n");
643 case AMD_PMC_RESULT_OK:
645 /* PMFW may take longer time to return back the data */
646 usleep_range(DELAY_MIN_US, 10 * DELAY_MAX_US);
647 *data = amd_pmc_reg_read(dev, argument);
650 case AMD_PMC_RESULT_CMD_REJECT_BUSY:
651 dev_err(dev->dev, "SMU not ready. err: 0x%x\n", val);
654 case AMD_PMC_RESULT_CMD_UNKNOWN:
655 dev_err(dev->dev, "SMU cmd unknown. err: 0x%x\n", val);
658 case AMD_PMC_RESULT_CMD_REJECT_PREREQ:
659 case AMD_PMC_RESULT_FAILED:
661 dev_err(dev->dev, "SMU cmd failed. err: 0x%x\n", val);
667 mutex_unlock(&dev->lock);
668 amd_pmc_dump_registers(dev);
672 static int amd_pmc_get_os_hint(struct amd_pmc_dev *dev)
674 switch (dev->cpu_id) {
676 return MSG_OS_HINT_PCO;
681 return MSG_OS_HINT_RN;
686 static int amd_pmc_czn_wa_irq1(struct amd_pmc_dev *pdev)
692 rc = amd_pmc_get_smu_version(pdev);
697 if (pdev->major > 64 || (pdev->major == 64 && pdev->minor > 65))
700 d = bus_find_device_by_name(&serio_bus, NULL, "serio0");
703 if (device_may_wakeup(d)) {
704 dev_info_once(d, "Disabling IRQ1 wakeup source to avoid platform firmware bug\n");
706 device_set_wakeup_enable(d, false);
713 static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg)
715 struct rtc_device *rtc_device;
716 time64_t then, now, duration;
717 struct rtc_wkalrm alarm;
721 /* we haven't yet read SMU version */
723 rc = amd_pmc_get_smu_version(pdev);
728 if (pdev->major < 64 || (pdev->major == 64 && pdev->minor < 53))
731 rtc_device = rtc_class_open("rtc0");
734 rc = rtc_read_alarm(rtc_device, &alarm);
737 if (!alarm.enabled) {
738 dev_dbg(pdev->dev, "alarm not enabled\n");
741 rc = rtc_read_time(rtc_device, &tm);
744 then = rtc_tm_to_time64(&alarm.time);
745 now = rtc_tm_to_time64(&tm);
752 /* will be stored in upper 16 bits of s0i3 hint argument,
753 * so timer wakeup from s0i3 is limited to ~18 hours or less
755 if (duration <= 4 || duration > U16_MAX)
758 *arg |= (duration << 16);
759 rc = rtc_alarm_irq_enable(rtc_device, 0);
760 dev_dbg(pdev->dev, "wakeup timer programmed for %lld seconds\n", duration);
765 static void amd_pmc_s2idle_prepare(void)
767 struct amd_pmc_dev *pdev = &pmc;
772 /* Reset and Start SMU logging - to monitor the s0i3 stats */
773 amd_pmc_setup_smu_logging(pdev);
775 /* Activate CZN specific platform bug workarounds */
776 if (pdev->cpu_id == AMD_CPU_ID_CZN && !disable_workarounds) {
777 rc = amd_pmc_verify_czn_rtc(pdev, &arg);
779 dev_err(pdev->dev, "failed to set RTC: %d\n", rc);
784 msg = amd_pmc_get_os_hint(pdev);
785 rc = amd_pmc_send_cmd(pdev, arg, NULL, msg, 0);
787 dev_err(pdev->dev, "suspend failed: %d\n", rc);
791 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_PREPARE);
793 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
796 static void amd_pmc_s2idle_check(void)
798 struct amd_pmc_dev *pdev = &pmc;
799 struct smu_metrics table;
802 /* CZN: Ensure that future s0i3 entry attempts at least 10ms passed */
803 if (pdev->cpu_id == AMD_CPU_ID_CZN && !get_metrics_table(pdev, &table) &&
804 table.s0i3_last_entry_status)
805 usleep_range(10000, 20000);
807 /* Dump the IdleMask before we add to the STB */
808 amd_pmc_idlemask_read(pdev, pdev->dev, NULL);
810 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_CHECK);
812 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
815 static void amd_pmc_s2idle_restore(void)
817 struct amd_pmc_dev *pdev = &pmc;
821 msg = amd_pmc_get_os_hint(pdev);
822 rc = amd_pmc_send_cmd(pdev, 0, NULL, msg, 0);
824 dev_err(pdev->dev, "resume failed: %d\n", rc);
826 /* Let SMU know that we are looking for stats */
827 amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_DUMP_DATA, 0);
829 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_RESTORE);
831 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
833 /* Notify on failed entry */
834 amd_pmc_validate_deepest(pdev);
837 static struct acpi_s2idle_dev_ops amd_pmc_s2idle_dev_ops = {
838 .prepare = amd_pmc_s2idle_prepare,
839 .check = amd_pmc_s2idle_check,
840 .restore = amd_pmc_s2idle_restore,
843 static int __maybe_unused amd_pmc_suspend_handler(struct device *dev)
845 struct amd_pmc_dev *pdev = dev_get_drvdata(dev);
847 if (pdev->cpu_id == AMD_CPU_ID_CZN && !disable_workarounds) {
848 int rc = amd_pmc_czn_wa_irq1(pdev);
851 dev_err(pdev->dev, "failed to adjust keyboard wakeup: %d\n", rc);
859 static DEFINE_SIMPLE_DEV_PM_OPS(amd_pmc_pm, amd_pmc_suspend_handler, NULL);
861 static const struct pci_device_id pmc_pci_ids[] = {
862 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PS) },
863 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CB) },
864 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_YC) },
865 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CZN) },
866 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RN) },
867 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PCO) },
868 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RV) },
872 static int amd_pmc_s2d_init(struct amd_pmc_dev *dev)
874 u32 phys_addr_low, phys_addr_hi;
878 /* Spill to DRAM feature uses separate SMU message port */
881 amd_pmc_send_cmd(dev, S2D_TELEMETRY_SIZE, &size, STB_SPILL_TO_DRAM, 1);
882 if (size != S2D_TELEMETRY_BYTES_MAX)
885 /* Get STB DRAM address */
886 amd_pmc_send_cmd(dev, S2D_PHYS_ADDR_LOW, &phys_addr_low, STB_SPILL_TO_DRAM, 1);
887 amd_pmc_send_cmd(dev, S2D_PHYS_ADDR_HIGH, &phys_addr_hi, STB_SPILL_TO_DRAM, 1);
889 stb_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low);
891 /* Clear msg_port for other SMU operation */
894 dev->stb_virt_addr = devm_ioremap(dev->dev, stb_phys_addr, S2D_TELEMETRY_DRAMBYTES_MAX);
895 if (!dev->stb_virt_addr)
901 static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data)
905 err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_ADDRESS, AMD_PMC_STB_PMI_0);
907 dev_err(dev->dev, "failed to write addr in stb: 0x%X\n",
908 AMD_PMC_STB_INDEX_ADDRESS);
909 return pcibios_err_to_errno(err);
912 err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_DATA, data);
914 dev_err(dev->dev, "failed to write data in stb: 0x%X\n",
915 AMD_PMC_STB_INDEX_DATA);
916 return pcibios_err_to_errno(err);
922 static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf)
926 err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_ADDRESS, AMD_PMC_STB_PMI_0);
928 dev_err(dev->dev, "error writing addr to stb: 0x%X\n",
929 AMD_PMC_STB_INDEX_ADDRESS);
930 return pcibios_err_to_errno(err);
933 for (i = 0; i < FIFO_SIZE; i++) {
934 err = pci_read_config_dword(dev->rdev, AMD_PMC_STB_INDEX_DATA, buf++);
936 dev_err(dev->dev, "error reading data from stb: 0x%X\n",
937 AMD_PMC_STB_INDEX_DATA);
938 return pcibios_err_to_errno(err);
945 static int amd_pmc_probe(struct platform_device *pdev)
947 struct amd_pmc_dev *dev = &pmc;
948 struct pci_dev *rdev;
949 u32 base_addr_lo, base_addr_hi;
954 dev->dev = &pdev->dev;
956 rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
957 if (!rdev || !pci_match_id(pmc_pci_ids, rdev)) {
959 goto err_pci_dev_put;
962 dev->cpu_id = rdev->device;
964 err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_LO);
966 dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS);
967 err = pcibios_err_to_errno(err);
968 goto err_pci_dev_put;
971 err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val);
973 err = pcibios_err_to_errno(err);
974 goto err_pci_dev_put;
977 base_addr_lo = val & AMD_PMC_BASE_ADDR_HI_MASK;
979 err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_HI);
981 dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS);
982 err = pcibios_err_to_errno(err);
983 goto err_pci_dev_put;
986 err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val);
988 err = pcibios_err_to_errno(err);
989 goto err_pci_dev_put;
992 base_addr_hi = val & AMD_PMC_BASE_ADDR_LO_MASK;
993 base_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
995 dev->regbase = devm_ioremap(dev->dev, base_addr + AMD_PMC_BASE_ADDR_OFFSET,
996 AMD_PMC_MAPPING_SIZE);
999 goto err_pci_dev_put;
1002 mutex_init(&dev->lock);
1004 if (enable_stb && (dev->cpu_id == AMD_CPU_ID_YC || dev->cpu_id == AMD_CPU_ID_CB)) {
1005 err = amd_pmc_s2d_init(dev);
1007 goto err_pci_dev_put;
1010 platform_set_drvdata(pdev, dev);
1011 if (IS_ENABLED(CONFIG_SUSPEND)) {
1012 err = acpi_register_lps0_dev(&amd_pmc_s2idle_dev_ops);
1014 dev_warn(dev->dev, "failed to register LPS0 sleep handler, expect increased power consumption\n");
1017 amd_pmc_dbgfs_register(dev);
1025 static int amd_pmc_remove(struct platform_device *pdev)
1027 struct amd_pmc_dev *dev = platform_get_drvdata(pdev);
1029 if (IS_ENABLED(CONFIG_SUSPEND))
1030 acpi_unregister_lps0_dev(&amd_pmc_s2idle_dev_ops);
1031 amd_pmc_dbgfs_unregister(dev);
1032 pci_dev_put(dev->rdev);
1033 mutex_destroy(&dev->lock);
1037 static const struct acpi_device_id amd_pmc_acpi_ids[] = {
1047 MODULE_DEVICE_TABLE(acpi, amd_pmc_acpi_ids);
1049 static struct platform_driver amd_pmc_driver = {
1052 .acpi_match_table = amd_pmc_acpi_ids,
1053 .dev_groups = pmc_groups,
1054 .pm = pm_sleep_ptr(&amd_pmc_pm),
1056 .probe = amd_pmc_probe,
1057 .remove = amd_pmc_remove,
1059 module_platform_driver(amd_pmc_driver);
1061 MODULE_LICENSE("GPL v2");
1062 MODULE_DESCRIPTION("AMD PMC Driver");