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/suspend.h>
26 #include <linux/seq_file.h>
27 #include <linux/uaccess.h>
29 /* SMU communication registers */
30 #define AMD_PMC_REGISTER_MESSAGE 0x538
31 #define AMD_PMC_REGISTER_RESPONSE 0x980
32 #define AMD_PMC_REGISTER_ARGUMENT 0x9BC
34 /* PMC Scratch Registers */
35 #define AMD_PMC_SCRATCH_REG_CZN 0x94
36 #define AMD_PMC_SCRATCH_REG_YC 0xD14
39 #define AMD_PMC_STB_INDEX_ADDRESS 0xF8
40 #define AMD_PMC_STB_INDEX_DATA 0xFC
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
46 /* STB S2D(Spill to DRAM) has different message port offset */
47 #define STB_SPILL_TO_DRAM 0xBE
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_SMU_INDEX_ADDRESS 0xB8
58 #define AMD_PMC_SMU_INDEX_DATA 0xBC
59 #define AMD_PMC_MAPPING_SIZE 0x01000
60 #define AMD_PMC_BASE_ADDR_OFFSET 0x10000
61 #define AMD_PMC_BASE_ADDR_LO 0x13B102E8
62 #define AMD_PMC_BASE_ADDR_HI 0x13B102EC
63 #define AMD_PMC_BASE_ADDR_LO_MASK GENMASK(15, 0)
64 #define AMD_PMC_BASE_ADDR_HI_MASK GENMASK(31, 20)
66 /* SMU Response Codes */
67 #define AMD_PMC_RESULT_OK 0x01
68 #define AMD_PMC_RESULT_CMD_REJECT_BUSY 0xFC
69 #define AMD_PMC_RESULT_CMD_REJECT_PREREQ 0xFD
70 #define AMD_PMC_RESULT_CMD_UNKNOWN 0xFE
71 #define AMD_PMC_RESULT_FAILED 0xFF
73 /* FCH SSC Registers */
74 #define FCH_S0I3_ENTRY_TIME_L_OFFSET 0x30
75 #define FCH_S0I3_ENTRY_TIME_H_OFFSET 0x34
76 #define FCH_S0I3_EXIT_TIME_L_OFFSET 0x38
77 #define FCH_S0I3_EXIT_TIME_H_OFFSET 0x3C
78 #define FCH_SSC_MAPPING_SIZE 0x800
79 #define FCH_BASE_PHY_ADDR_LOW 0xFED81100
80 #define FCH_BASE_PHY_ADDR_HIGH 0x00000000
82 /* SMU Message Definations */
83 #define SMU_MSG_GETSMUVERSION 0x02
84 #define SMU_MSG_LOG_GETDRAM_ADDR_HI 0x04
85 #define SMU_MSG_LOG_GETDRAM_ADDR_LO 0x05
86 #define SMU_MSG_LOG_START 0x06
87 #define SMU_MSG_LOG_RESET 0x07
88 #define SMU_MSG_LOG_DUMP_DATA 0x08
89 #define SMU_MSG_GET_SUP_CONSTRAINTS 0x09
90 /* List of supported CPU ids */
91 #define AMD_CPU_ID_RV 0x15D0
92 #define AMD_CPU_ID_RN 0x1630
93 #define AMD_CPU_ID_PCO AMD_CPU_ID_RV
94 #define AMD_CPU_ID_CZN AMD_CPU_ID_RN
95 #define AMD_CPU_ID_YC 0x14B5
96 #define AMD_CPU_ID_CB 0x14D8
97 #define AMD_CPU_ID_PS 0x14E8
99 #define PMC_MSG_DELAY_MIN_US 50
100 #define RESPONSE_REGISTER_LOOP_MAX 20000
102 #define SOC_SUBSYSTEM_IP_MAX 12
103 #define DELAY_MIN_US 2000
104 #define DELAY_MAX_US 3000
105 #define FIFO_SIZE 4096
113 S2D_TELEMETRY_SIZE = 0x01,
118 struct amd_pmc_bit_map {
123 static const struct amd_pmc_bit_map soc15_ip_blk[] = {
140 void __iomem *regbase;
141 void __iomem *smu_virt_addr;
142 void __iomem *stb_virt_addr;
143 void __iomem *fch_virt_addr;
148 /* SMU version information */
154 struct pci_dev *rdev;
155 struct mutex lock; /* generic mutex lock */
156 struct dentry *dbgfs_dir;
159 static bool enable_stb;
160 module_param(enable_stb, bool, 0644);
161 MODULE_PARM_DESC(enable_stb, "Enable the STB debug mechanism");
163 static struct amd_pmc_dev pmc;
164 static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret);
165 static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf);
166 #ifdef CONFIG_SUSPEND
167 static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data);
170 static inline u32 amd_pmc_reg_read(struct amd_pmc_dev *dev, int reg_offset)
172 return ioread32(dev->regbase + reg_offset);
175 static inline void amd_pmc_reg_write(struct amd_pmc_dev *dev, int reg_offset, u32 val)
177 iowrite32(val, dev->regbase + reg_offset);
183 u32 s0i3_last_entry_status;
185 u64 timeentering_s0i3_lastcapture;
186 u64 timeentering_s0i3_totaltime;
187 u64 timeto_resume_to_os_lastcapture;
188 u64 timeto_resume_to_os_totaltime;
189 u64 timein_s0i3_lastcapture;
190 u64 timein_s0i3_totaltime;
191 u64 timein_swdrips_lastcapture;
192 u64 timein_swdrips_totaltime;
193 u64 timecondition_notmet_lastcapture[SOC_SUBSYSTEM_IP_MAX];
194 u64 timecondition_notmet_totaltime[SOC_SUBSYSTEM_IP_MAX];
197 static int amd_pmc_stb_debugfs_open(struct inode *inode, struct file *filp)
199 struct amd_pmc_dev *dev = filp->f_inode->i_private;
200 u32 size = FIFO_SIZE * sizeof(u32);
204 buf = kzalloc(size, GFP_KERNEL);
208 rc = amd_pmc_read_stb(dev, buf);
214 filp->private_data = buf;
218 static ssize_t amd_pmc_stb_debugfs_read(struct file *filp, char __user *buf, size_t size,
221 if (!filp->private_data)
224 return simple_read_from_buffer(buf, size, pos, filp->private_data,
225 FIFO_SIZE * sizeof(u32));
228 static int amd_pmc_stb_debugfs_release(struct inode *inode, struct file *filp)
230 kfree(filp->private_data);
234 static const struct file_operations amd_pmc_stb_debugfs_fops = {
235 .owner = THIS_MODULE,
236 .open = amd_pmc_stb_debugfs_open,
237 .read = amd_pmc_stb_debugfs_read,
238 .release = amd_pmc_stb_debugfs_release,
241 static int amd_pmc_stb_debugfs_open_v2(struct inode *inode, struct file *filp)
243 struct amd_pmc_dev *dev = filp->f_inode->i_private;
246 buf = kzalloc(S2D_TELEMETRY_BYTES_MAX, GFP_KERNEL);
250 memcpy_fromio(buf, dev->stb_virt_addr, S2D_TELEMETRY_BYTES_MAX);
251 filp->private_data = buf;
256 static ssize_t amd_pmc_stb_debugfs_read_v2(struct file *filp, char __user *buf, size_t size,
259 if (!filp->private_data)
262 return simple_read_from_buffer(buf, size, pos, filp->private_data,
263 S2D_TELEMETRY_BYTES_MAX);
266 static int amd_pmc_stb_debugfs_release_v2(struct inode *inode, struct file *filp)
268 kfree(filp->private_data);
272 static const struct file_operations amd_pmc_stb_debugfs_fops_v2 = {
273 .owner = THIS_MODULE,
274 .open = amd_pmc_stb_debugfs_open_v2,
275 .read = amd_pmc_stb_debugfs_read_v2,
276 .release = amd_pmc_stb_debugfs_release_v2,
279 #if defined(CONFIG_SUSPEND) || defined(CONFIG_DEBUG_FS)
280 static int amd_pmc_setup_smu_logging(struct amd_pmc_dev *dev)
282 if (dev->cpu_id == AMD_CPU_ID_PCO) {
283 dev_warn_once(dev->dev, "SMU debugging info not supported on this platform\n");
287 /* Get Active devices list from SMU */
288 if (!dev->active_ips)
289 amd_pmc_send_cmd(dev, 0, &dev->active_ips, SMU_MSG_GET_SUP_CONSTRAINTS, 1);
291 /* Get dram address */
292 if (!dev->smu_virt_addr) {
293 u32 phys_addr_low, phys_addr_hi;
296 amd_pmc_send_cmd(dev, 0, &phys_addr_low, SMU_MSG_LOG_GETDRAM_ADDR_LO, 1);
297 amd_pmc_send_cmd(dev, 0, &phys_addr_hi, SMU_MSG_LOG_GETDRAM_ADDR_HI, 1);
298 smu_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low);
300 dev->smu_virt_addr = devm_ioremap(dev->dev, smu_phys_addr,
301 sizeof(struct smu_metrics));
302 if (!dev->smu_virt_addr)
306 /* Start the logging */
307 amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_RESET, 0);
308 amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_START, 0);
313 static int amd_pmc_idlemask_read(struct amd_pmc_dev *pdev, struct device *dev,
318 switch (pdev->cpu_id) {
320 val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_CZN);
325 val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_YC);
332 dev_dbg(pdev->dev, "SMU idlemask s0i3: 0x%x\n", val);
335 seq_printf(s, "SMU idlemask : 0x%x\n", val);
340 static int get_metrics_table(struct amd_pmc_dev *pdev, struct smu_metrics *table)
342 if (!pdev->smu_virt_addr) {
343 int ret = amd_pmc_setup_smu_logging(pdev);
349 if (pdev->cpu_id == AMD_CPU_ID_PCO)
351 memcpy_fromio(table, pdev->smu_virt_addr, sizeof(struct smu_metrics));
354 #endif /* CONFIG_SUSPEND || CONFIG_DEBUG_FS */
356 #ifdef CONFIG_SUSPEND
357 static void amd_pmc_validate_deepest(struct amd_pmc_dev *pdev)
359 struct smu_metrics table;
361 if (get_metrics_table(pdev, &table))
364 if (!table.s0i3_last_entry_status)
365 dev_warn(pdev->dev, "Last suspend didn't reach deepest state\n");
367 dev_dbg(pdev->dev, "Last suspend in deepest state for %lluus\n",
368 table.timein_s0i3_lastcapture);
372 static int amd_pmc_get_smu_version(struct amd_pmc_dev *dev)
377 rc = amd_pmc_send_cmd(dev, 0, &val, SMU_MSG_GETSMUVERSION, 1);
381 dev->smu_program = (val >> 24) & GENMASK(7, 0);
382 dev->major = (val >> 16) & GENMASK(7, 0);
383 dev->minor = (val >> 8) & GENMASK(7, 0);
384 dev->rev = (val >> 0) & GENMASK(7, 0);
386 dev_dbg(dev->dev, "SMU program %u version is %u.%u.%u\n",
387 dev->smu_program, dev->major, dev->minor, dev->rev);
392 static ssize_t smu_fw_version_show(struct device *d, struct device_attribute *attr,
395 struct amd_pmc_dev *dev = dev_get_drvdata(d);
398 int rc = amd_pmc_get_smu_version(dev);
403 return sysfs_emit(buf, "%u.%u.%u\n", dev->major, dev->minor, dev->rev);
406 static ssize_t smu_program_show(struct device *d, struct device_attribute *attr,
409 struct amd_pmc_dev *dev = dev_get_drvdata(d);
412 int rc = amd_pmc_get_smu_version(dev);
417 return sysfs_emit(buf, "%u\n", dev->smu_program);
420 static DEVICE_ATTR_RO(smu_fw_version);
421 static DEVICE_ATTR_RO(smu_program);
423 static struct attribute *pmc_attrs[] = {
424 &dev_attr_smu_fw_version.attr,
425 &dev_attr_smu_program.attr,
428 ATTRIBUTE_GROUPS(pmc);
430 static int smu_fw_info_show(struct seq_file *s, void *unused)
432 struct amd_pmc_dev *dev = s->private;
433 struct smu_metrics table;
436 if (get_metrics_table(dev, &table))
439 seq_puts(s, "\n=== SMU Statistics ===\n");
440 seq_printf(s, "Table Version: %d\n", table.table_version);
441 seq_printf(s, "Hint Count: %d\n", table.hint_count);
442 seq_printf(s, "Last S0i3 Status: %s\n", table.s0i3_last_entry_status ? "Success" :
444 seq_printf(s, "Time (in us) to S0i3: %lld\n", table.timeentering_s0i3_lastcapture);
445 seq_printf(s, "Time (in us) in S0i3: %lld\n", table.timein_s0i3_lastcapture);
446 seq_printf(s, "Time (in us) to resume from S0i3: %lld\n",
447 table.timeto_resume_to_os_lastcapture);
449 seq_puts(s, "\n=== Active time (in us) ===\n");
450 for (idx = 0 ; idx < SOC_SUBSYSTEM_IP_MAX ; idx++) {
451 if (soc15_ip_blk[idx].bit_mask & dev->active_ips)
452 seq_printf(s, "%-8s : %lld\n", soc15_ip_blk[idx].name,
453 table.timecondition_notmet_lastcapture[idx]);
458 DEFINE_SHOW_ATTRIBUTE(smu_fw_info);
460 static int s0ix_stats_show(struct seq_file *s, void *unused)
462 struct amd_pmc_dev *dev = s->private;
463 u64 entry_time, exit_time, residency;
465 /* Use FCH registers to get the S0ix stats */
466 if (!dev->fch_virt_addr) {
467 u32 base_addr_lo = FCH_BASE_PHY_ADDR_LOW;
468 u32 base_addr_hi = FCH_BASE_PHY_ADDR_HIGH;
469 u64 fch_phys_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
471 dev->fch_virt_addr = devm_ioremap(dev->dev, fch_phys_addr, FCH_SSC_MAPPING_SIZE);
472 if (!dev->fch_virt_addr)
476 entry_time = ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_H_OFFSET);
477 entry_time = entry_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_L_OFFSET);
479 exit_time = ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_H_OFFSET);
480 exit_time = exit_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_L_OFFSET);
482 /* It's in 48MHz. We need to convert it */
483 residency = exit_time - entry_time;
484 do_div(residency, 48);
486 seq_puts(s, "=== S0ix statistics ===\n");
487 seq_printf(s, "S0ix Entry Time: %lld\n", entry_time);
488 seq_printf(s, "S0ix Exit Time: %lld\n", exit_time);
489 seq_printf(s, "Residency Time: %lld\n", residency);
493 DEFINE_SHOW_ATTRIBUTE(s0ix_stats);
495 static int amd_pmc_idlemask_show(struct seq_file *s, void *unused)
497 struct amd_pmc_dev *dev = s->private;
500 /* we haven't yet read SMU version */
502 rc = amd_pmc_get_smu_version(dev);
507 if (dev->major > 56 || (dev->major >= 55 && dev->minor >= 37)) {
508 rc = amd_pmc_idlemask_read(dev, NULL, s);
512 seq_puts(s, "Unsupported SMU version for Idlemask\n");
517 DEFINE_SHOW_ATTRIBUTE(amd_pmc_idlemask);
519 static void amd_pmc_dbgfs_unregister(struct amd_pmc_dev *dev)
521 debugfs_remove_recursive(dev->dbgfs_dir);
524 static void amd_pmc_dbgfs_register(struct amd_pmc_dev *dev)
526 dev->dbgfs_dir = debugfs_create_dir("amd_pmc", NULL);
527 debugfs_create_file("smu_fw_info", 0644, dev->dbgfs_dir, dev,
529 debugfs_create_file("s0ix_stats", 0644, dev->dbgfs_dir, dev,
531 debugfs_create_file("amd_pmc_idlemask", 0644, dev->dbgfs_dir, dev,
532 &amd_pmc_idlemask_fops);
533 /* Enable STB only when the module_param is set */
535 if (dev->cpu_id == AMD_CPU_ID_YC || dev->cpu_id == AMD_CPU_ID_CB ||
536 dev->cpu_id == AMD_CPU_ID_PS)
537 debugfs_create_file("stb_read", 0644, dev->dbgfs_dir, dev,
538 &amd_pmc_stb_debugfs_fops_v2);
540 debugfs_create_file("stb_read", 0644, dev->dbgfs_dir, dev,
541 &amd_pmc_stb_debugfs_fops);
545 static void amd_pmc_dump_registers(struct amd_pmc_dev *dev)
547 u32 value, message, argument, response;
550 message = AMD_S2D_REGISTER_MESSAGE;
551 argument = AMD_S2D_REGISTER_ARGUMENT;
552 response = AMD_S2D_REGISTER_RESPONSE;
554 message = AMD_PMC_REGISTER_MESSAGE;
555 argument = AMD_PMC_REGISTER_ARGUMENT;
556 response = AMD_PMC_REGISTER_RESPONSE;
559 value = amd_pmc_reg_read(dev, response);
560 dev_dbg(dev->dev, "AMD_PMC_REGISTER_RESPONSE:%x\n", value);
562 value = amd_pmc_reg_read(dev, argument);
563 dev_dbg(dev->dev, "AMD_PMC_REGISTER_ARGUMENT:%x\n", value);
565 value = amd_pmc_reg_read(dev, message);
566 dev_dbg(dev->dev, "AMD_PMC_REGISTER_MESSAGE:%x\n", value);
569 static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret)
572 u32 val, message, argument, response;
574 mutex_lock(&dev->lock);
577 message = AMD_S2D_REGISTER_MESSAGE;
578 argument = AMD_S2D_REGISTER_ARGUMENT;
579 response = AMD_S2D_REGISTER_RESPONSE;
581 message = AMD_PMC_REGISTER_MESSAGE;
582 argument = AMD_PMC_REGISTER_ARGUMENT;
583 response = AMD_PMC_REGISTER_RESPONSE;
586 /* Wait until we get a valid response */
587 rc = readx_poll_timeout(ioread32, dev->regbase + response,
588 val, val != 0, PMC_MSG_DELAY_MIN_US,
589 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
591 dev_err(dev->dev, "failed to talk to SMU\n");
595 /* Write zero to response register */
596 amd_pmc_reg_write(dev, response, 0);
598 /* Write argument into response register */
599 amd_pmc_reg_write(dev, argument, arg);
601 /* Write message ID to message ID register */
602 amd_pmc_reg_write(dev, message, msg);
604 /* Wait until we get a valid response */
605 rc = readx_poll_timeout(ioread32, dev->regbase + response,
606 val, val != 0, PMC_MSG_DELAY_MIN_US,
607 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
609 dev_err(dev->dev, "SMU response timed out\n");
614 case AMD_PMC_RESULT_OK:
616 /* PMFW may take longer time to return back the data */
617 usleep_range(DELAY_MIN_US, 10 * DELAY_MAX_US);
618 *data = amd_pmc_reg_read(dev, argument);
621 case AMD_PMC_RESULT_CMD_REJECT_BUSY:
622 dev_err(dev->dev, "SMU not ready. err: 0x%x\n", val);
625 case AMD_PMC_RESULT_CMD_UNKNOWN:
626 dev_err(dev->dev, "SMU cmd unknown. err: 0x%x\n", val);
629 case AMD_PMC_RESULT_CMD_REJECT_PREREQ:
630 case AMD_PMC_RESULT_FAILED:
632 dev_err(dev->dev, "SMU cmd failed. err: 0x%x\n", val);
638 mutex_unlock(&dev->lock);
639 amd_pmc_dump_registers(dev);
643 #ifdef CONFIG_SUSPEND
644 static int amd_pmc_get_os_hint(struct amd_pmc_dev *dev)
646 switch (dev->cpu_id) {
648 return MSG_OS_HINT_PCO;
653 return MSG_OS_HINT_RN;
658 static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg)
660 struct rtc_device *rtc_device;
661 time64_t then, now, duration;
662 struct rtc_wkalrm alarm;
666 /* we haven't yet read SMU version */
668 rc = amd_pmc_get_smu_version(pdev);
673 if (pdev->major < 64 || (pdev->major == 64 && pdev->minor < 53))
676 rtc_device = rtc_class_open("rtc0");
679 rc = rtc_read_alarm(rtc_device, &alarm);
682 if (!alarm.enabled) {
683 dev_dbg(pdev->dev, "alarm not enabled\n");
686 rc = rtc_read_time(rtc_device, &tm);
689 then = rtc_tm_to_time64(&alarm.time);
690 now = rtc_tm_to_time64(&tm);
697 /* will be stored in upper 16 bits of s0i3 hint argument,
698 * so timer wakeup from s0i3 is limited to ~18 hours or less
700 if (duration <= 4 || duration > U16_MAX)
703 *arg |= (duration << 16);
704 rc = rtc_alarm_irq_enable(rtc_device, 0);
705 dev_dbg(pdev->dev, "wakeup timer programmed for %lld seconds\n", duration);
710 static void amd_pmc_s2idle_prepare(void)
712 struct amd_pmc_dev *pdev = &pmc;
717 /* Reset and Start SMU logging - to monitor the s0i3 stats */
718 amd_pmc_setup_smu_logging(pdev);
720 /* Activate CZN specific RTC functionality */
721 if (pdev->cpu_id == AMD_CPU_ID_CZN) {
722 rc = amd_pmc_verify_czn_rtc(pdev, &arg);
724 dev_err(pdev->dev, "failed to set RTC: %d\n", rc);
729 msg = amd_pmc_get_os_hint(pdev);
730 rc = amd_pmc_send_cmd(pdev, arg, NULL, msg, 0);
732 dev_err(pdev->dev, "suspend failed: %d\n", rc);
736 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_PREPARE);
738 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
741 static void amd_pmc_s2idle_check(void)
743 struct amd_pmc_dev *pdev = &pmc;
746 /* Dump the IdleMask before we add to the STB */
747 amd_pmc_idlemask_read(pdev, pdev->dev, NULL);
749 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_CHECK);
751 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
754 static void amd_pmc_s2idle_restore(void)
756 struct amd_pmc_dev *pdev = &pmc;
760 msg = amd_pmc_get_os_hint(pdev);
761 rc = amd_pmc_send_cmd(pdev, 0, NULL, msg, 0);
763 dev_err(pdev->dev, "resume failed: %d\n", rc);
765 /* Let SMU know that we are looking for stats */
766 amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_DUMP_DATA, 0);
768 rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_S2IDLE_RESTORE);
770 dev_err(pdev->dev, "error writing to STB: %d\n", rc);
772 /* Notify on failed entry */
773 amd_pmc_validate_deepest(pdev);
776 static struct acpi_s2idle_dev_ops amd_pmc_s2idle_dev_ops = {
777 .prepare = amd_pmc_s2idle_prepare,
778 .check = amd_pmc_s2idle_check,
779 .restore = amd_pmc_s2idle_restore,
783 static const struct pci_device_id pmc_pci_ids[] = {
784 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PS) },
785 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CB) },
786 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_YC) },
787 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CZN) },
788 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RN) },
789 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PCO) },
790 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RV) },
794 static int amd_pmc_s2d_init(struct amd_pmc_dev *dev)
796 u32 phys_addr_low, phys_addr_hi;
800 /* Spill to DRAM feature uses separate SMU message port */
803 amd_pmc_send_cmd(dev, S2D_TELEMETRY_SIZE, &size, STB_SPILL_TO_DRAM, 1);
804 if (size != S2D_TELEMETRY_BYTES_MAX)
807 /* Get STB DRAM address */
808 amd_pmc_send_cmd(dev, S2D_PHYS_ADDR_LOW, &phys_addr_low, STB_SPILL_TO_DRAM, 1);
809 amd_pmc_send_cmd(dev, S2D_PHYS_ADDR_HIGH, &phys_addr_hi, STB_SPILL_TO_DRAM, 1);
811 stb_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low);
813 /* Clear msg_port for other SMU operation */
816 dev->stb_virt_addr = devm_ioremap(dev->dev, stb_phys_addr, S2D_TELEMETRY_DRAMBYTES_MAX);
817 if (!dev->stb_virt_addr)
823 #ifdef CONFIG_SUSPEND
824 static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data)
828 err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_ADDRESS, AMD_PMC_STB_PMI_0);
830 dev_err(dev->dev, "failed to write addr in stb: 0x%X\n",
831 AMD_PMC_STB_INDEX_ADDRESS);
832 return pcibios_err_to_errno(err);
835 err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_DATA, data);
837 dev_err(dev->dev, "failed to write data in stb: 0x%X\n",
838 AMD_PMC_STB_INDEX_DATA);
839 return pcibios_err_to_errno(err);
846 static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf)
850 err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_ADDRESS, AMD_PMC_STB_PMI_0);
852 dev_err(dev->dev, "error writing addr to stb: 0x%X\n",
853 AMD_PMC_STB_INDEX_ADDRESS);
854 return pcibios_err_to_errno(err);
857 for (i = 0; i < FIFO_SIZE; i++) {
858 err = pci_read_config_dword(dev->rdev, AMD_PMC_STB_INDEX_DATA, buf++);
860 dev_err(dev->dev, "error reading data from stb: 0x%X\n",
861 AMD_PMC_STB_INDEX_DATA);
862 return pcibios_err_to_errno(err);
869 static int amd_pmc_probe(struct platform_device *pdev)
871 struct amd_pmc_dev *dev = &pmc;
872 struct pci_dev *rdev;
873 u32 base_addr_lo, base_addr_hi;
878 dev->dev = &pdev->dev;
880 rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
881 if (!rdev || !pci_match_id(pmc_pci_ids, rdev)) {
883 goto err_pci_dev_put;
886 dev->cpu_id = rdev->device;
888 err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_LO);
890 dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS);
891 err = pcibios_err_to_errno(err);
892 goto err_pci_dev_put;
895 err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val);
897 err = pcibios_err_to_errno(err);
898 goto err_pci_dev_put;
901 base_addr_lo = val & AMD_PMC_BASE_ADDR_HI_MASK;
903 err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_HI);
905 dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS);
906 err = pcibios_err_to_errno(err);
907 goto err_pci_dev_put;
910 err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val);
912 err = pcibios_err_to_errno(err);
913 goto err_pci_dev_put;
916 base_addr_hi = val & AMD_PMC_BASE_ADDR_LO_MASK;
917 base_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
919 dev->regbase = devm_ioremap(dev->dev, base_addr + AMD_PMC_BASE_ADDR_OFFSET,
920 AMD_PMC_MAPPING_SIZE);
923 goto err_pci_dev_put;
926 mutex_init(&dev->lock);
928 if (enable_stb && (dev->cpu_id == AMD_CPU_ID_YC || dev->cpu_id == AMD_CPU_ID_CB)) {
929 err = amd_pmc_s2d_init(dev);
934 platform_set_drvdata(pdev, dev);
935 #ifdef CONFIG_SUSPEND
936 err = acpi_register_lps0_dev(&amd_pmc_s2idle_dev_ops);
938 dev_warn(dev->dev, "failed to register LPS0 sleep handler, expect increased power consumption\n");
941 amd_pmc_dbgfs_register(dev);
949 static int amd_pmc_remove(struct platform_device *pdev)
951 struct amd_pmc_dev *dev = platform_get_drvdata(pdev);
953 #ifdef CONFIG_SUSPEND
954 acpi_unregister_lps0_dev(&amd_pmc_s2idle_dev_ops);
956 amd_pmc_dbgfs_unregister(dev);
957 pci_dev_put(dev->rdev);
958 mutex_destroy(&dev->lock);
962 static const struct acpi_device_id amd_pmc_acpi_ids[] = {
971 MODULE_DEVICE_TABLE(acpi, amd_pmc_acpi_ids);
973 static struct platform_driver amd_pmc_driver = {
976 .acpi_match_table = amd_pmc_acpi_ids,
977 .dev_groups = pmc_groups,
979 .probe = amd_pmc_probe,
980 .remove = amd_pmc_remove,
982 module_platform_driver(amd_pmc_driver);
984 MODULE_LICENSE("GPL v2");
985 MODULE_DESCRIPTION("AMD PMC Driver");