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 <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 #ifdef CONFIG_SUSPEND
175 static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data);
178 static inline u32 amd_pmc_reg_read(struct amd_pmc_dev *dev, int reg_offset)
180 return ioread32(dev->regbase + reg_offset);
183 static inline void amd_pmc_reg_write(struct amd_pmc_dev *dev, int reg_offset, u32 val)
185 iowrite32(val, dev->regbase + reg_offset);
191 u32 s0i3_last_entry_status;
193 u64 timeentering_s0i3_lastcapture;
194 u64 timeentering_s0i3_totaltime;
195 u64 timeto_resume_to_os_lastcapture;
196 u64 timeto_resume_to_os_totaltime;
197 u64 timein_s0i3_lastcapture;
198 u64 timein_s0i3_totaltime;
199 u64 timein_swdrips_lastcapture;
200 u64 timein_swdrips_totaltime;
201 u64 timecondition_notmet_lastcapture[SOC_SUBSYSTEM_IP_MAX];
202 u64 timecondition_notmet_totaltime[SOC_SUBSYSTEM_IP_MAX];
205 static int amd_pmc_stb_debugfs_open(struct inode *inode, struct file *filp)
207 struct amd_pmc_dev *dev = filp->f_inode->i_private;
208 u32 size = FIFO_SIZE * sizeof(u32);
212 buf = kzalloc(size, GFP_KERNEL);
216 rc = amd_pmc_read_stb(dev, buf);
222 filp->private_data = buf;
226 static ssize_t amd_pmc_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,
229 if (!filp->private_data)
232 return simple_read_from_buffer(buf, size, pos, filp->private_data,
233 FIFO_SIZE * sizeof(u32));
236 static int amd_pmc_stb_debugfs_release(struct inode *inode, struct file *filp)
238 kfree(filp->private_data);
242 static const struct file_operations amd_pmc_stb_debugfs_fops = {
243 .owner = THIS_MODULE,
244 .open = amd_pmc_stb_debugfs_open,
245 .read = amd_pmc_stb_debugfs_read,
246 .release = amd_pmc_stb_debugfs_release,
249 static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp)
251 struct amd_pmc_dev *dev = filp->f_inode->i_private;
252 u32 *buf, fsize, num_samples, stb_rdptr_offset = 0;
255 /* Write dummy postcode while reading the STB buffer */
256 ret = amd_pmc_write_stb(dev, AMD_PMC_STB_DUMMY_PC);
258 dev_err(dev->dev, "error writing to STB: %d\n", ret);
260 buf = kzalloc(S2D_TELEMETRY_BYTES_MAX, GFP_KERNEL);
264 /* Spill to DRAM num_samples uses separate SMU message port */
267 /* Get the num_samples to calculate the last push location */
268 ret = amd_pmc_send_cmd(dev, S2D_NUM_SAMPLES, &num_samples, STB_SPILL_TO_DRAM, 1);
269 /* Clear msg_port for other SMU operation */
272 dev_err(dev->dev, "error: S2D_NUM_SAMPLES not supported : %d\n", ret);
276 /* Start capturing data from the last push location */
277 if (num_samples > S2D_TELEMETRY_BYTES_MAX) {
278 fsize = S2D_TELEMETRY_BYTES_MAX;
279 stb_rdptr_offset = num_samples - fsize;
282 stb_rdptr_offset = 0;
285 memcpy_fromio(buf, dev->stb_virt_addr + stb_rdptr_offset, fsize);
286 filp->private_data = buf;
291 static ssize_t amd_pmc_stb_debugfs_read_v2(struct file *filp, char __user *buf, size_t size,
294 if (!filp->private_data)
297 return simple_read_from_buffer(buf, size, pos, filp->private_data,
298 S2D_TELEMETRY_BYTES_MAX);
301 static int amd_pmc_stb_debugfs_release_v2(struct inode *inode, struct file *filp)
303 kfree(filp->private_data);
307 static const struct file_operations amd_pmc_stb_debugfs_fops_v2 = {
308 .owner = THIS_MODULE,
309 .open = amd_pmc_stb_debugfs_open_v2,
310 .read = amd_pmc_stb_debugfs_read_v2,
311 .release = amd_pmc_stb_debugfs_release_v2,
314 static int amd_pmc_setup_smu_logging(struct amd_pmc_dev *dev)
316 if (dev->cpu_id == AMD_CPU_ID_PCO) {
317 dev_warn_once(dev->dev, "SMU debugging info not supported on this platform\n");
321 /* Get Active devices list from SMU */
322 if (!dev->active_ips)
323 amd_pmc_send_cmd(dev, 0, &dev->active_ips, SMU_MSG_GET_SUP_CONSTRAINTS, 1);
325 /* Get dram address */
326 if (!dev->smu_virt_addr) {
327 u32 phys_addr_low, phys_addr_hi;
330 amd_pmc_send_cmd(dev, 0, &phys_addr_low, SMU_MSG_LOG_GETDRAM_ADDR_LO, 1);
331 amd_pmc_send_cmd(dev, 0, &phys_addr_hi, SMU_MSG_LOG_GETDRAM_ADDR_HI, 1);
332 smu_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low);
334 dev->smu_virt_addr = devm_ioremap(dev->dev, smu_phys_addr,
335 sizeof(struct smu_metrics));
336 if (!dev->smu_virt_addr)
340 /* Start the logging */
341 amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_RESET, 0);
342 amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_START, 0);
347 static int amd_pmc_idlemask_read(struct amd_pmc_dev *pdev, struct device *dev,
352 switch (pdev->cpu_id) {
354 val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_CZN);
359 val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_YC);
366 dev_dbg(pdev->dev, "SMU idlemask s0i3: 0x%x\n", val);
369 seq_printf(s, "SMU idlemask : 0x%x\n", val);
374 static int get_metrics_table(struct amd_pmc_dev *pdev, struct smu_metrics *table)
376 if (!pdev->smu_virt_addr) {
377 int ret = amd_pmc_setup_smu_logging(pdev);
383 if (pdev->cpu_id == AMD_CPU_ID_PCO)
385 memcpy_fromio(table, pdev->smu_virt_addr, sizeof(struct smu_metrics));
389 #ifdef CONFIG_SUSPEND
390 static void amd_pmc_validate_deepest(struct amd_pmc_dev *pdev)
392 struct smu_metrics table;
394 if (get_metrics_table(pdev, &table))
397 if (!table.s0i3_last_entry_status)
398 dev_warn(pdev->dev, "Last suspend didn't reach deepest state\n");
400 dev_dbg(pdev->dev, "Last suspend in deepest state for %lluus\n",
401 table.timein_s0i3_lastcapture);
405 static int amd_pmc_get_smu_version(struct amd_pmc_dev *dev)
410 rc = amd_pmc_send_cmd(dev, 0, &val, SMU_MSG_GETSMUVERSION, 1);
414 dev->smu_program = (val >> 24) & GENMASK(7, 0);
415 dev->major = (val >> 16) & GENMASK(7, 0);
416 dev->minor = (val >> 8) & GENMASK(7, 0);
417 dev->rev = (val >> 0) & GENMASK(7, 0);
419 dev_dbg(dev->dev, "SMU program %u version is %u.%u.%u\n",
420 dev->smu_program, dev->major, dev->minor, dev->rev);
425 static ssize_t smu_fw_version_show(struct device *d, struct device_attribute *attr,
428 struct amd_pmc_dev *dev = dev_get_drvdata(d);
431 int rc = amd_pmc_get_smu_version(dev);
436 return sysfs_emit(buf, "%u.%u.%u\n", dev->major, dev->minor, dev->rev);
439 static ssize_t smu_program_show(struct device *d, struct device_attribute *attr,
442 struct amd_pmc_dev *dev = dev_get_drvdata(d);
445 int rc = amd_pmc_get_smu_version(dev);
450 return sysfs_emit(buf, "%u\n", dev->smu_program);
453 static DEVICE_ATTR_RO(smu_fw_version);
454 static DEVICE_ATTR_RO(smu_program);
456 static struct attribute *pmc_attrs[] = {
457 &dev_attr_smu_fw_version.attr,
458 &dev_attr_smu_program.attr,
461 ATTRIBUTE_GROUPS(pmc);
463 static int smu_fw_info_show(struct seq_file *s, void *unused)
465 struct amd_pmc_dev *dev = s->private;
466 struct smu_metrics table;
469 if (get_metrics_table(dev, &table))
472 seq_puts(s, "\n=== SMU Statistics ===\n");
473 seq_printf(s, "Table Version: %d\n", table.table_version);
474 seq_printf(s, "Hint Count: %d\n", table.hint_count);
475 seq_printf(s, "Last S0i3 Status: %s\n", table.s0i3_last_entry_status ? "Success" :
477 seq_printf(s, "Time (in us) to S0i3: %lld\n", table.timeentering_s0i3_lastcapture);
478 seq_printf(s, "Time (in us) in S0i3: %lld\n", table.timein_s0i3_lastcapture);
479 seq_printf(s, "Time (in us) to resume from S0i3: %lld\n",
480 table.timeto_resume_to_os_lastcapture);
482 seq_puts(s, "\n=== Active time (in us) ===\n");
483 for (idx = 0 ; idx < SOC_SUBSYSTEM_IP_MAX ; idx++) {
484 if (soc15_ip_blk[idx].bit_mask & dev->active_ips)
485 seq_printf(s, "%-8s : %lld\n", soc15_ip_blk[idx].name,
486 table.timecondition_notmet_lastcapture[idx]);
491 DEFINE_SHOW_ATTRIBUTE(smu_fw_info);
493 static int s0ix_stats_show(struct seq_file *s, void *unused)
495 struct amd_pmc_dev *dev = s->private;
496 u64 entry_time, exit_time, residency;
498 /* Use FCH registers to get the S0ix stats */
499 if (!dev->fch_virt_addr) {
500 u32 base_addr_lo = FCH_BASE_PHY_ADDR_LOW;
501 u32 base_addr_hi = FCH_BASE_PHY_ADDR_HIGH;
502 u64 fch_phys_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
504 dev->fch_virt_addr = devm_ioremap(dev->dev, fch_phys_addr, FCH_SSC_MAPPING_SIZE);
505 if (!dev->fch_virt_addr)
509 entry_time = ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_H_OFFSET);
510 entry_time = entry_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_L_OFFSET);
512 exit_time = ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_H_OFFSET);
513 exit_time = exit_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_L_OFFSET);
515 /* It's in 48MHz. We need to convert it */
516 residency = exit_time - entry_time;
517 do_div(residency, 48);
519 seq_puts(s, "=== S0ix statistics ===\n");
520 seq_printf(s, "S0ix Entry Time: %lld\n", entry_time);
521 seq_printf(s, "S0ix Exit Time: %lld\n", exit_time);
522 seq_printf(s, "Residency Time: %lld\n", residency);
526 DEFINE_SHOW_ATTRIBUTE(s0ix_stats);
528 static int amd_pmc_idlemask_show(struct seq_file *s, void *unused)
530 struct amd_pmc_dev *dev = s->private;
533 /* we haven't yet read SMU version */
535 rc = amd_pmc_get_smu_version(dev);
540 if (dev->major > 56 || (dev->major >= 55 && dev->minor >= 37)) {
541 rc = amd_pmc_idlemask_read(dev, NULL, s);
545 seq_puts(s, "Unsupported SMU version for Idlemask\n");
550 DEFINE_SHOW_ATTRIBUTE(amd_pmc_idlemask);
552 static void amd_pmc_dbgfs_unregister(struct amd_pmc_dev *dev)
554 debugfs_remove_recursive(dev->dbgfs_dir);
557 static void amd_pmc_dbgfs_register(struct amd_pmc_dev *dev)
559 dev->dbgfs_dir = debugfs_create_dir("amd_pmc", NULL);
560 debugfs_create_file("smu_fw_info", 0644, dev->dbgfs_dir, dev,
562 debugfs_create_file("s0ix_stats", 0644, dev->dbgfs_dir, dev,
564 debugfs_create_file("amd_pmc_idlemask", 0644, dev->dbgfs_dir, dev,
565 &amd_pmc_idlemask_fops);
566 /* Enable STB only when the module_param is set */
568 if (dev->cpu_id == AMD_CPU_ID_YC || dev->cpu_id == AMD_CPU_ID_CB ||
569 dev->cpu_id == AMD_CPU_ID_PS)
570 debugfs_create_file("stb_read", 0644, dev->dbgfs_dir, dev,
571 &amd_pmc_stb_debugfs_fops_v2);
573 debugfs_create_file("stb_read", 0644, dev->dbgfs_dir, dev,
574 &amd_pmc_stb_debugfs_fops);
578 static void amd_pmc_dump_registers(struct amd_pmc_dev *dev)
580 u32 value, message, argument, response;
583 message = AMD_S2D_REGISTER_MESSAGE;
584 argument = AMD_S2D_REGISTER_ARGUMENT;
585 response = AMD_S2D_REGISTER_RESPONSE;
587 message = AMD_PMC_REGISTER_MESSAGE;
588 argument = AMD_PMC_REGISTER_ARGUMENT;
589 response = AMD_PMC_REGISTER_RESPONSE;
592 value = amd_pmc_reg_read(dev, response);
593 dev_dbg(dev->dev, "AMD_%s_REGISTER_RESPONSE:%x\n", dev->msg_port ? "S2D" : "PMC", value);
595 value = amd_pmc_reg_read(dev, argument);
596 dev_dbg(dev->dev, "AMD_%s_REGISTER_ARGUMENT:%x\n", dev->msg_port ? "S2D" : "PMC", value);
598 value = amd_pmc_reg_read(dev, message);
599 dev_dbg(dev->dev, "AMD_%s_REGISTER_MESSAGE:%x\n", dev->msg_port ? "S2D" : "PMC", value);
602 static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret)
605 u32 val, message, argument, response;
607 mutex_lock(&dev->lock);
610 message = AMD_S2D_REGISTER_MESSAGE;
611 argument = AMD_S2D_REGISTER_ARGUMENT;
612 response = AMD_S2D_REGISTER_RESPONSE;
614 message = AMD_PMC_REGISTER_MESSAGE;
615 argument = AMD_PMC_REGISTER_ARGUMENT;
616 response = AMD_PMC_REGISTER_RESPONSE;
619 /* Wait until we get a valid response */
620 rc = readx_poll_timeout(ioread32, dev->regbase + response,
621 val, val != 0, PMC_MSG_DELAY_MIN_US,
622 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
624 dev_err(dev->dev, "failed to talk to SMU\n");
628 /* Write zero to response register */
629 amd_pmc_reg_write(dev, response, 0);
631 /* Write argument into response register */
632 amd_pmc_reg_write(dev, argument, arg);
634 /* Write message ID to message ID register */
635 amd_pmc_reg_write(dev, message, msg);
637 /* Wait until we get a valid response */
638 rc = readx_poll_timeout(ioread32, dev->regbase + response,
639 val, val != 0, PMC_MSG_DELAY_MIN_US,
640 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
642 dev_err(dev->dev, "SMU response timed out\n");
647 case AMD_PMC_RESULT_OK:
649 /* PMFW may take longer time to return back the data */
650 usleep_range(DELAY_MIN_US, 10 * DELAY_MAX_US);
651 *data = amd_pmc_reg_read(dev, argument);
654 case AMD_PMC_RESULT_CMD_REJECT_BUSY:
655 dev_err(dev->dev, "SMU not ready. err: 0x%x\n", val);
658 case AMD_PMC_RESULT_CMD_UNKNOWN:
659 dev_err(dev->dev, "SMU cmd unknown. err: 0x%x\n", val);
662 case AMD_PMC_RESULT_CMD_REJECT_PREREQ:
663 case AMD_PMC_RESULT_FAILED:
665 dev_err(dev->dev, "SMU cmd failed. err: 0x%x\n", val);
671 mutex_unlock(&dev->lock);
672 amd_pmc_dump_registers(dev);
676 #ifdef CONFIG_SUSPEND
677 static int amd_pmc_get_os_hint(struct amd_pmc_dev *dev)
679 switch (dev->cpu_id) {
681 return MSG_OS_HINT_PCO;
686 return MSG_OS_HINT_RN;
691 static int amd_pmc_czn_wa_irq1(struct amd_pmc_dev *pdev)
697 rc = amd_pmc_get_smu_version(pdev);
702 if (pdev->major > 64 || (pdev->major == 64 && pdev->minor > 65))
705 d = bus_find_device_by_name(&serio_bus, NULL, "serio0");
708 if (device_may_wakeup(d)) {
709 dev_info_once(d, "Disabling IRQ1 wakeup source to avoid platform firmware bug\n");
711 device_set_wakeup_enable(d, false);
718 static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg)
720 struct rtc_device *rtc_device;
721 time64_t then, now, duration;
722 struct rtc_wkalrm alarm;
726 /* we haven't yet read SMU version */
728 rc = amd_pmc_get_smu_version(pdev);
733 if (pdev->major < 64 || (pdev->major == 64 && pdev->minor < 53))
736 rtc_device = rtc_class_open("rtc0");
739 rc = rtc_read_alarm(rtc_device, &alarm);
742 if (!alarm.enabled) {
743 dev_dbg(pdev->dev, "alarm not enabled\n");
746 rc = rtc_read_time(rtc_device, &tm);
749 then = rtc_tm_to_time64(&alarm.time);
750 now = rtc_tm_to_time64(&tm);
757 /* will be stored in upper 16 bits of s0i3 hint argument,
758 * so timer wakeup from s0i3 is limited to ~18 hours or less
760 if (duration <= 4 || duration > U16_MAX)
763 *arg |= (duration << 16);
764 rc = rtc_alarm_irq_enable(rtc_device, 0);
765 dev_dbg(pdev->dev, "wakeup timer programmed for %lld seconds\n", duration);
770 static void amd_pmc_s2idle_prepare(void)
772 struct amd_pmc_dev *pdev = &pmc;
777 /* Reset and Start SMU logging - to monitor the s0i3 stats */
778 amd_pmc_setup_smu_logging(pdev);
780 /* Activate CZN specific platform bug workarounds */
781 if (pdev->cpu_id == AMD_CPU_ID_CZN && !disable_workarounds) {
782 rc = amd_pmc_verify_czn_rtc(pdev, &arg);
784 dev_err(pdev->dev, "failed to set RTC: %d\n", rc);
789 msg = amd_pmc_get_os_hint(pdev);
790 rc = amd_pmc_send_cmd(pdev, arg, NULL, msg, 0);
792 dev_err(pdev->dev, "suspend failed: %d\n", rc);
796 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_PREPARE);
798 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
801 static void amd_pmc_s2idle_check(void)
803 struct amd_pmc_dev *pdev = &pmc;
804 struct smu_metrics table;
807 /* CZN: Ensure that future s0i3 entry attempts at least 10ms passed */
808 if (pdev->cpu_id == AMD_CPU_ID_CZN && !get_metrics_table(pdev, &table) &&
809 table.s0i3_last_entry_status)
810 usleep_range(10000, 20000);
812 /* Dump the IdleMask before we add to the STB */
813 amd_pmc_idlemask_read(pdev, pdev->dev, NULL);
815 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_CHECK);
817 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
820 static void amd_pmc_s2idle_restore(void)
822 struct amd_pmc_dev *pdev = &pmc;
826 msg = amd_pmc_get_os_hint(pdev);
827 rc = amd_pmc_send_cmd(pdev, 0, NULL, msg, 0);
829 dev_err(pdev->dev, "resume failed: %d\n", rc);
831 /* Let SMU know that we are looking for stats */
832 amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_DUMP_DATA, 0);
834 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_RESTORE);
836 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
838 /* Notify on failed entry */
839 amd_pmc_validate_deepest(pdev);
842 static struct acpi_s2idle_dev_ops amd_pmc_s2idle_dev_ops = {
843 .prepare = amd_pmc_s2idle_prepare,
844 .check = amd_pmc_s2idle_check,
845 .restore = amd_pmc_s2idle_restore,
848 static int __maybe_unused amd_pmc_suspend_handler(struct device *dev)
850 struct amd_pmc_dev *pdev = dev_get_drvdata(dev);
852 if (pdev->cpu_id == AMD_CPU_ID_CZN && !disable_workarounds) {
853 int rc = amd_pmc_czn_wa_irq1(pdev);
856 dev_err(pdev->dev, "failed to adjust keyboard wakeup: %d\n", rc);
864 static SIMPLE_DEV_PM_OPS(amd_pmc_pm, amd_pmc_suspend_handler, NULL);
868 static const struct pci_device_id pmc_pci_ids[] = {
869 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PS) },
870 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CB) },
871 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_YC) },
872 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CZN) },
873 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RN) },
874 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PCO) },
875 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RV) },
879 static int amd_pmc_s2d_init(struct amd_pmc_dev *dev)
881 u32 phys_addr_low, phys_addr_hi;
885 /* Spill to DRAM feature uses separate SMU message port */
888 amd_pmc_send_cmd(dev, S2D_TELEMETRY_SIZE, &size, STB_SPILL_TO_DRAM, 1);
889 if (size != S2D_TELEMETRY_BYTES_MAX)
892 /* Get STB DRAM address */
893 amd_pmc_send_cmd(dev, S2D_PHYS_ADDR_LOW, &phys_addr_low, STB_SPILL_TO_DRAM, 1);
894 amd_pmc_send_cmd(dev, S2D_PHYS_ADDR_HIGH, &phys_addr_hi, STB_SPILL_TO_DRAM, 1);
896 stb_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low);
898 /* Clear msg_port for other SMU operation */
901 dev->stb_virt_addr = devm_ioremap(dev->dev, stb_phys_addr, S2D_TELEMETRY_DRAMBYTES_MAX);
902 if (!dev->stb_virt_addr)
908 #ifdef CONFIG_SUSPEND
909 static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data)
913 err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_ADDRESS, AMD_PMC_STB_PMI_0);
915 dev_err(dev->dev, "failed to write addr in stb: 0x%X\n",
916 AMD_PMC_STB_INDEX_ADDRESS);
917 return pcibios_err_to_errno(err);
920 err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_DATA, data);
922 dev_err(dev->dev, "failed to write data in stb: 0x%X\n",
923 AMD_PMC_STB_INDEX_DATA);
924 return pcibios_err_to_errno(err);
931 static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf)
935 err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_ADDRESS, AMD_PMC_STB_PMI_0);
937 dev_err(dev->dev, "error writing addr to stb: 0x%X\n",
938 AMD_PMC_STB_INDEX_ADDRESS);
939 return pcibios_err_to_errno(err);
942 for (i = 0; i < FIFO_SIZE; i++) {
943 err = pci_read_config_dword(dev->rdev, AMD_PMC_STB_INDEX_DATA, buf++);
945 dev_err(dev->dev, "error reading data from stb: 0x%X\n",
946 AMD_PMC_STB_INDEX_DATA);
947 return pcibios_err_to_errno(err);
954 static int amd_pmc_probe(struct platform_device *pdev)
956 struct amd_pmc_dev *dev = &pmc;
957 struct pci_dev *rdev;
958 u32 base_addr_lo, base_addr_hi;
963 dev->dev = &pdev->dev;
965 rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
966 if (!rdev || !pci_match_id(pmc_pci_ids, rdev)) {
968 goto err_pci_dev_put;
971 dev->cpu_id = rdev->device;
973 err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_LO);
975 dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS);
976 err = pcibios_err_to_errno(err);
977 goto err_pci_dev_put;
980 err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val);
982 err = pcibios_err_to_errno(err);
983 goto err_pci_dev_put;
986 base_addr_lo = val & AMD_PMC_BASE_ADDR_HI_MASK;
988 err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_HI);
990 dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS);
991 err = pcibios_err_to_errno(err);
992 goto err_pci_dev_put;
995 err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val);
997 err = pcibios_err_to_errno(err);
998 goto err_pci_dev_put;
1001 base_addr_hi = val & AMD_PMC_BASE_ADDR_LO_MASK;
1002 base_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
1004 dev->regbase = devm_ioremap(dev->dev, base_addr + AMD_PMC_BASE_ADDR_OFFSET,
1005 AMD_PMC_MAPPING_SIZE);
1006 if (!dev->regbase) {
1008 goto err_pci_dev_put;
1011 mutex_init(&dev->lock);
1013 if (enable_stb && (dev->cpu_id == AMD_CPU_ID_YC || dev->cpu_id == AMD_CPU_ID_CB)) {
1014 err = amd_pmc_s2d_init(dev);
1016 goto err_pci_dev_put;
1019 platform_set_drvdata(pdev, dev);
1020 #ifdef CONFIG_SUSPEND
1021 err = acpi_register_lps0_dev(&amd_pmc_s2idle_dev_ops);
1023 dev_warn(dev->dev, "failed to register LPS0 sleep handler, expect increased power consumption\n");
1026 amd_pmc_dbgfs_register(dev);
1034 static int amd_pmc_remove(struct platform_device *pdev)
1036 struct amd_pmc_dev *dev = platform_get_drvdata(pdev);
1038 #ifdef CONFIG_SUSPEND
1039 acpi_unregister_lps0_dev(&amd_pmc_s2idle_dev_ops);
1041 amd_pmc_dbgfs_unregister(dev);
1042 pci_dev_put(dev->rdev);
1043 mutex_destroy(&dev->lock);
1047 static const struct acpi_device_id amd_pmc_acpi_ids[] = {
1057 MODULE_DEVICE_TABLE(acpi, amd_pmc_acpi_ids);
1059 static struct platform_driver amd_pmc_driver = {
1062 .acpi_match_table = amd_pmc_acpi_ids,
1063 .dev_groups = pmc_groups,
1064 #ifdef CONFIG_SUSPEND
1068 .probe = amd_pmc_probe,
1069 .remove = amd_pmc_remove,
1071 module_platform_driver(amd_pmc_driver);
1073 MODULE_LICENSE("GPL v2");
1074 MODULE_DESCRIPTION("AMD PMC Driver");