Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * AMD SoC Power Management Controller Driver |
| 4 | * |
| 5 | * Copyright (c) 2020, Advanced Micro Devices, Inc. |
| 6 | * All Rights Reserved. |
| 7 | * |
| 8 | * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com> |
| 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
| 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> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/iopoll.h> |
Mario Limonciello | 5934840 | 2021-10-20 11:29:46 -0500 | [diff] [blame] | 20 | #include <linux/limits.h> |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/pci.h> |
| 23 | #include <linux/platform_device.h> |
Mario Limonciello | 5934840 | 2021-10-20 11:29:46 -0500 | [diff] [blame] | 24 | #include <linux/rtc.h> |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 25 | #include <linux/suspend.h> |
| 26 | #include <linux/seq_file.h> |
| 27 | #include <linux/uaccess.h> |
| 28 | |
| 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 |
| 33 | |
Sanket Goswami | f6045de | 2021-09-16 18:10:02 +0530 | [diff] [blame] | 34 | /* PMC Scratch Registers */ |
| 35 | #define AMD_PMC_SCRATCH_REG_CZN 0x94 |
| 36 | #define AMD_PMC_SCRATCH_REG_YC 0xD14 |
| 37 | |
Sanket Goswami | 426c0ff | 2021-11-30 16:53:18 +0530 | [diff] [blame] | 38 | /* STB Registers */ |
| 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_PREDEF 0xC6000001 |
| 43 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 44 | /* Base address of SMU for mapping physical address to virtual address */ |
| 45 | #define AMD_PMC_SMU_INDEX_ADDRESS 0xB8 |
| 46 | #define AMD_PMC_SMU_INDEX_DATA 0xBC |
| 47 | #define AMD_PMC_MAPPING_SIZE 0x01000 |
| 48 | #define AMD_PMC_BASE_ADDR_OFFSET 0x10000 |
| 49 | #define AMD_PMC_BASE_ADDR_LO 0x13B102E8 |
| 50 | #define AMD_PMC_BASE_ADDR_HI 0x13B102EC |
| 51 | #define AMD_PMC_BASE_ADDR_LO_MASK GENMASK(15, 0) |
| 52 | #define AMD_PMC_BASE_ADDR_HI_MASK GENMASK(31, 20) |
| 53 | |
| 54 | /* SMU Response Codes */ |
| 55 | #define AMD_PMC_RESULT_OK 0x01 |
| 56 | #define AMD_PMC_RESULT_CMD_REJECT_BUSY 0xFC |
| 57 | #define AMD_PMC_RESULT_CMD_REJECT_PREREQ 0xFD |
| 58 | #define AMD_PMC_RESULT_CMD_UNKNOWN 0xFE |
| 59 | #define AMD_PMC_RESULT_FAILED 0xFF |
| 60 | |
Shyam Sundar S K | b9a4fa6 | 2021-06-29 14:18:01 +0530 | [diff] [blame] | 61 | /* FCH SSC Registers */ |
| 62 | #define FCH_S0I3_ENTRY_TIME_L_OFFSET 0x30 |
| 63 | #define FCH_S0I3_ENTRY_TIME_H_OFFSET 0x34 |
| 64 | #define FCH_S0I3_EXIT_TIME_L_OFFSET 0x38 |
| 65 | #define FCH_S0I3_EXIT_TIME_H_OFFSET 0x3C |
| 66 | #define FCH_SSC_MAPPING_SIZE 0x800 |
| 67 | #define FCH_BASE_PHY_ADDR_LOW 0xFED81100 |
| 68 | #define FCH_BASE_PHY_ADDR_HIGH 0x00000000 |
| 69 | |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 70 | /* SMU Message Definations */ |
| 71 | #define SMU_MSG_GETSMUVERSION 0x02 |
| 72 | #define SMU_MSG_LOG_GETDRAM_ADDR_HI 0x04 |
| 73 | #define SMU_MSG_LOG_GETDRAM_ADDR_LO 0x05 |
| 74 | #define SMU_MSG_LOG_START 0x06 |
| 75 | #define SMU_MSG_LOG_RESET 0x07 |
| 76 | #define SMU_MSG_LOG_DUMP_DATA 0x08 |
| 77 | #define SMU_MSG_GET_SUP_CONSTRAINTS 0x09 |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 78 | /* List of supported CPU ids */ |
| 79 | #define AMD_CPU_ID_RV 0x15D0 |
| 80 | #define AMD_CPU_ID_RN 0x1630 |
| 81 | #define AMD_CPU_ID_PCO AMD_CPU_ID_RV |
| 82 | #define AMD_CPU_ID_CZN AMD_CPU_ID_RN |
Shyam Sundar S K | 83cbaf1 | 2021-06-29 14:18:03 +0530 | [diff] [blame] | 83 | #define AMD_CPU_ID_YC 0x14B5 |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 84 | |
Fabrizio Bertocci | a602f51 | 2021-11-29 23:15:40 -0500 | [diff] [blame] | 85 | #define PMC_MSG_DELAY_MIN_US 50 |
Mario Limonciello | 3c3c8e8 | 2021-09-13 21:01:14 -0500 | [diff] [blame] | 86 | #define RESPONSE_REGISTER_LOOP_MAX 20000 |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 87 | |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 88 | #define SOC_SUBSYSTEM_IP_MAX 12 |
| 89 | #define DELAY_MIN_US 2000 |
| 90 | #define DELAY_MAX_US 3000 |
Sanket Goswami | 426c0ff | 2021-11-30 16:53:18 +0530 | [diff] [blame] | 91 | #define FIFO_SIZE 4096 |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 92 | enum amd_pmc_def { |
| 93 | MSG_TEST = 0x01, |
| 94 | MSG_OS_HINT_PCO, |
| 95 | MSG_OS_HINT_RN, |
| 96 | }; |
| 97 | |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 98 | struct amd_pmc_bit_map { |
| 99 | const char *name; |
| 100 | u32 bit_mask; |
| 101 | }; |
| 102 | |
| 103 | static const struct amd_pmc_bit_map soc15_ip_blk[] = { |
| 104 | {"DISPLAY", BIT(0)}, |
| 105 | {"CPU", BIT(1)}, |
| 106 | {"GFX", BIT(2)}, |
| 107 | {"VDD", BIT(3)}, |
| 108 | {"ACP", BIT(4)}, |
| 109 | {"VCN", BIT(5)}, |
| 110 | {"ISP", BIT(6)}, |
| 111 | {"NBIO", BIT(7)}, |
| 112 | {"DF", BIT(8)}, |
| 113 | {"USB0", BIT(9)}, |
| 114 | {"USB1", BIT(10)}, |
| 115 | {"LAPIC", BIT(11)}, |
| 116 | {} |
| 117 | }; |
| 118 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 119 | struct amd_pmc_dev { |
| 120 | void __iomem *regbase; |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 121 | void __iomem *smu_virt_addr; |
Shyam Sundar S K | b9a4fa6 | 2021-06-29 14:18:01 +0530 | [diff] [blame] | 122 | void __iomem *fch_virt_addr; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 123 | u32 base_addr; |
| 124 | u32 cpu_id; |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 125 | u32 active_ips; |
Sanket Goswami | f6045de | 2021-09-16 18:10:02 +0530 | [diff] [blame] | 126 | /* SMU version information */ |
| 127 | u16 major; |
| 128 | u16 minor; |
| 129 | u16 rev; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 130 | struct device *dev; |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 131 | struct pci_dev *rdev; |
Shyam Sundar S K | 95e1b60 | 2021-06-29 14:17:57 +0530 | [diff] [blame] | 132 | struct mutex lock; /* generic mutex lock */ |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 133 | #if IS_ENABLED(CONFIG_DEBUG_FS) |
| 134 | struct dentry *dbgfs_dir; |
| 135 | #endif /* CONFIG_DEBUG_FS */ |
| 136 | }; |
| 137 | |
Sanket Goswami | 426c0ff | 2021-11-30 16:53:18 +0530 | [diff] [blame] | 138 | static bool enable_stb; |
| 139 | module_param(enable_stb, bool, 0644); |
| 140 | MODULE_PARM_DESC(enable_stb, "Enable the STB debug mechanism"); |
| 141 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 142 | static struct amd_pmc_dev pmc; |
Mario Limonciello | 4c9dbf8 | 2021-10-20 11:29:45 -0500 | [diff] [blame] | 143 | static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret); |
Sanket Goswami | 426c0ff | 2021-11-30 16:53:18 +0530 | [diff] [blame] | 144 | static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data); |
| 145 | static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf); |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 146 | |
| 147 | static inline u32 amd_pmc_reg_read(struct amd_pmc_dev *dev, int reg_offset) |
| 148 | { |
| 149 | return ioread32(dev->regbase + reg_offset); |
| 150 | } |
| 151 | |
| 152 | static inline void amd_pmc_reg_write(struct amd_pmc_dev *dev, int reg_offset, u32 val) |
| 153 | { |
| 154 | iowrite32(val, dev->regbase + reg_offset); |
| 155 | } |
| 156 | |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 157 | struct smu_metrics { |
| 158 | u32 table_version; |
| 159 | u32 hint_count; |
Sanket Goswami | 9cfe020 | 2021-09-16 18:11:30 +0530 | [diff] [blame] | 160 | u32 s0i3_last_entry_status; |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 161 | u32 timein_s0i2; |
| 162 | u64 timeentering_s0i3_lastcapture; |
| 163 | u64 timeentering_s0i3_totaltime; |
| 164 | u64 timeto_resume_to_os_lastcapture; |
| 165 | u64 timeto_resume_to_os_totaltime; |
| 166 | u64 timein_s0i3_lastcapture; |
| 167 | u64 timein_s0i3_totaltime; |
| 168 | u64 timein_swdrips_lastcapture; |
| 169 | u64 timein_swdrips_totaltime; |
| 170 | u64 timecondition_notmet_lastcapture[SOC_SUBSYSTEM_IP_MAX]; |
| 171 | u64 timecondition_notmet_totaltime[SOC_SUBSYSTEM_IP_MAX]; |
| 172 | } __packed; |
| 173 | |
Hans de Goede | 40635cd | 2021-09-28 16:16:06 +0200 | [diff] [blame] | 174 | static int amd_pmc_get_smu_version(struct amd_pmc_dev *dev) |
| 175 | { |
| 176 | int rc; |
| 177 | u32 val; |
| 178 | |
| 179 | rc = amd_pmc_send_cmd(dev, 0, &val, SMU_MSG_GETSMUVERSION, 1); |
| 180 | if (rc) |
| 181 | return rc; |
| 182 | |
| 183 | dev->major = (val >> 16) & GENMASK(15, 0); |
| 184 | dev->minor = (val >> 8) & GENMASK(7, 0); |
| 185 | dev->rev = (val >> 0) & GENMASK(7, 0); |
| 186 | |
| 187 | dev_dbg(dev->dev, "SMU version is %u.%u.%u\n", dev->major, dev->minor, dev->rev); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Sanket Goswami | 426c0ff | 2021-11-30 16:53:18 +0530 | [diff] [blame] | 192 | static int amd_pmc_stb_debugfs_open(struct inode *inode, struct file *filp) |
| 193 | { |
| 194 | struct amd_pmc_dev *dev = filp->f_inode->i_private; |
| 195 | u32 size = FIFO_SIZE * sizeof(u32); |
| 196 | u32 *buf; |
| 197 | int rc; |
| 198 | |
| 199 | buf = kzalloc(size, GFP_KERNEL); |
| 200 | if (!buf) |
| 201 | return -ENOMEM; |
| 202 | |
| 203 | rc = amd_pmc_read_stb(dev, buf); |
| 204 | if (rc) { |
| 205 | kfree(buf); |
| 206 | return rc; |
| 207 | } |
| 208 | |
| 209 | filp->private_data = buf; |
| 210 | return rc; |
| 211 | } |
| 212 | |
| 213 | static ssize_t amd_pmc_stb_debugfs_read(struct file *filp, char __user *buf, size_t size, |
| 214 | loff_t *pos) |
| 215 | { |
| 216 | if (!filp->private_data) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | return simple_read_from_buffer(buf, size, pos, filp->private_data, |
| 220 | FIFO_SIZE * sizeof(u32)); |
| 221 | } |
| 222 | |
| 223 | static int amd_pmc_stb_debugfs_release(struct inode *inode, struct file *filp) |
| 224 | { |
| 225 | kfree(filp->private_data); |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | const struct file_operations amd_pmc_stb_debugfs_fops = { |
| 230 | .owner = THIS_MODULE, |
| 231 | .open = amd_pmc_stb_debugfs_open, |
| 232 | .read = amd_pmc_stb_debugfs_read, |
| 233 | .release = amd_pmc_stb_debugfs_release, |
| 234 | }; |
| 235 | |
Hans de Goede | 40635cd | 2021-09-28 16:16:06 +0200 | [diff] [blame] | 236 | static int amd_pmc_idlemask_read(struct amd_pmc_dev *pdev, struct device *dev, |
| 237 | struct seq_file *s) |
| 238 | { |
| 239 | u32 val; |
| 240 | |
| 241 | switch (pdev->cpu_id) { |
| 242 | case AMD_CPU_ID_CZN: |
| 243 | val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_CZN); |
| 244 | break; |
| 245 | case AMD_CPU_ID_YC: |
| 246 | val = amd_pmc_reg_read(pdev, AMD_PMC_SCRATCH_REG_YC); |
| 247 | break; |
| 248 | default: |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | if (dev) |
| 253 | dev_dbg(pdev->dev, "SMU idlemask s0i3: 0x%x\n", val); |
| 254 | |
| 255 | if (s) |
| 256 | seq_printf(s, "SMU idlemask : 0x%x\n", val); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Shyam Sundar S K | 5b56930 | 2020-12-30 13:40:28 +0530 | [diff] [blame] | 261 | #ifdef CONFIG_DEBUG_FS |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 262 | static int smu_fw_info_show(struct seq_file *s, void *unused) |
| 263 | { |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 264 | struct amd_pmc_dev *dev = s->private; |
| 265 | struct smu_metrics table; |
| 266 | int idx; |
| 267 | |
| 268 | if (dev->cpu_id == AMD_CPU_ID_PCO) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | memcpy_fromio(&table, dev->smu_virt_addr, sizeof(struct smu_metrics)); |
| 272 | |
| 273 | seq_puts(s, "\n=== SMU Statistics ===\n"); |
| 274 | seq_printf(s, "Table Version: %d\n", table.table_version); |
| 275 | seq_printf(s, "Hint Count: %d\n", table.hint_count); |
Sanket Goswami | 9cfe020 | 2021-09-16 18:11:30 +0530 | [diff] [blame] | 276 | seq_printf(s, "Last S0i3 Status: %s\n", table.s0i3_last_entry_status ? "Success" : |
| 277 | "Unknown/Fail"); |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 278 | seq_printf(s, "Time (in us) to S0i3: %lld\n", table.timeentering_s0i3_lastcapture); |
| 279 | seq_printf(s, "Time (in us) in S0i3: %lld\n", table.timein_s0i3_lastcapture); |
Sanket Goswami | 7dbcaf7 | 2021-09-21 17:30:20 +0530 | [diff] [blame] | 280 | seq_printf(s, "Time (in us) to resume from S0i3: %lld\n", |
| 281 | table.timeto_resume_to_os_lastcapture); |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 282 | |
| 283 | seq_puts(s, "\n=== Active time (in us) ===\n"); |
| 284 | for (idx = 0 ; idx < SOC_SUBSYSTEM_IP_MAX ; idx++) { |
| 285 | if (soc15_ip_blk[idx].bit_mask & dev->active_ips) |
| 286 | seq_printf(s, "%-8s : %lld\n", soc15_ip_blk[idx].name, |
| 287 | table.timecondition_notmet_lastcapture[idx]); |
| 288 | } |
| 289 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | DEFINE_SHOW_ATTRIBUTE(smu_fw_info); |
| 293 | |
Shyam Sundar S K | b9a4fa6 | 2021-06-29 14:18:01 +0530 | [diff] [blame] | 294 | static int s0ix_stats_show(struct seq_file *s, void *unused) |
| 295 | { |
| 296 | struct amd_pmc_dev *dev = s->private; |
| 297 | u64 entry_time, exit_time, residency; |
| 298 | |
| 299 | entry_time = ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_H_OFFSET); |
| 300 | entry_time = entry_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_L_OFFSET); |
| 301 | |
| 302 | exit_time = ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_H_OFFSET); |
| 303 | exit_time = exit_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_L_OFFSET); |
| 304 | |
| 305 | /* It's in 48MHz. We need to convert it */ |
Shyam Sundar S K | 7f5231b | 2021-07-16 21:08:02 +0530 | [diff] [blame] | 306 | residency = exit_time - entry_time; |
| 307 | do_div(residency, 48); |
Shyam Sundar S K | b9a4fa6 | 2021-06-29 14:18:01 +0530 | [diff] [blame] | 308 | |
| 309 | seq_puts(s, "=== S0ix statistics ===\n"); |
| 310 | seq_printf(s, "S0ix Entry Time: %lld\n", entry_time); |
| 311 | seq_printf(s, "S0ix Exit Time: %lld\n", exit_time); |
| 312 | seq_printf(s, "Residency Time: %lld\n", residency); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | DEFINE_SHOW_ATTRIBUTE(s0ix_stats); |
| 317 | |
Sanket Goswami | f6045de | 2021-09-16 18:10:02 +0530 | [diff] [blame] | 318 | static int amd_pmc_idlemask_show(struct seq_file *s, void *unused) |
| 319 | { |
| 320 | struct amd_pmc_dev *dev = s->private; |
| 321 | int rc; |
| 322 | |
| 323 | if (dev->major > 56 || (dev->major >= 55 && dev->minor >= 37)) { |
| 324 | rc = amd_pmc_idlemask_read(dev, NULL, s); |
| 325 | if (rc) |
| 326 | return rc; |
| 327 | } else { |
| 328 | seq_puts(s, "Unsupported SMU version for Idlemask\n"); |
| 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | DEFINE_SHOW_ATTRIBUTE(amd_pmc_idlemask); |
| 334 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 335 | static void amd_pmc_dbgfs_unregister(struct amd_pmc_dev *dev) |
| 336 | { |
| 337 | debugfs_remove_recursive(dev->dbgfs_dir); |
| 338 | } |
| 339 | |
| 340 | static void amd_pmc_dbgfs_register(struct amd_pmc_dev *dev) |
| 341 | { |
| 342 | dev->dbgfs_dir = debugfs_create_dir("amd_pmc", NULL); |
| 343 | debugfs_create_file("smu_fw_info", 0644, dev->dbgfs_dir, dev, |
| 344 | &smu_fw_info_fops); |
Shyam Sundar S K | b9a4fa6 | 2021-06-29 14:18:01 +0530 | [diff] [blame] | 345 | debugfs_create_file("s0ix_stats", 0644, dev->dbgfs_dir, dev, |
| 346 | &s0ix_stats_fops); |
Sanket Goswami | f6045de | 2021-09-16 18:10:02 +0530 | [diff] [blame] | 347 | debugfs_create_file("amd_pmc_idlemask", 0644, dev->dbgfs_dir, dev, |
| 348 | &amd_pmc_idlemask_fops); |
Sanket Goswami | 426c0ff | 2021-11-30 16:53:18 +0530 | [diff] [blame] | 349 | /* Enable STB only when the module_param is set */ |
| 350 | if (enable_stb) |
| 351 | debugfs_create_file("stb_read", 0644, dev->dbgfs_dir, dev, |
| 352 | &amd_pmc_stb_debugfs_fops); |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 353 | } |
| 354 | #else |
| 355 | static inline void amd_pmc_dbgfs_register(struct amd_pmc_dev *dev) |
| 356 | { |
| 357 | } |
| 358 | |
| 359 | static inline void amd_pmc_dbgfs_unregister(struct amd_pmc_dev *dev) |
| 360 | { |
| 361 | } |
| 362 | #endif /* CONFIG_DEBUG_FS */ |
| 363 | |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 364 | static int amd_pmc_setup_smu_logging(struct amd_pmc_dev *dev) |
| 365 | { |
| 366 | u32 phys_addr_low, phys_addr_hi; |
| 367 | u64 smu_phys_addr; |
| 368 | |
| 369 | if (dev->cpu_id == AMD_CPU_ID_PCO) |
| 370 | return -EINVAL; |
| 371 | |
| 372 | /* Get Active devices list from SMU */ |
| 373 | amd_pmc_send_cmd(dev, 0, &dev->active_ips, SMU_MSG_GET_SUP_CONSTRAINTS, 1); |
| 374 | |
| 375 | /* Get dram address */ |
| 376 | amd_pmc_send_cmd(dev, 0, &phys_addr_low, SMU_MSG_LOG_GETDRAM_ADDR_LO, 1); |
| 377 | amd_pmc_send_cmd(dev, 0, &phys_addr_hi, SMU_MSG_LOG_GETDRAM_ADDR_HI, 1); |
| 378 | smu_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low); |
| 379 | |
| 380 | dev->smu_virt_addr = devm_ioremap(dev->dev, smu_phys_addr, sizeof(struct smu_metrics)); |
| 381 | if (!dev->smu_virt_addr) |
| 382 | return -ENOMEM; |
| 383 | |
| 384 | /* Start the logging */ |
| 385 | amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_START, 0); |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 390 | static void amd_pmc_dump_registers(struct amd_pmc_dev *dev) |
| 391 | { |
| 392 | u32 value; |
| 393 | |
| 394 | value = amd_pmc_reg_read(dev, AMD_PMC_REGISTER_RESPONSE); |
| 395 | dev_dbg(dev->dev, "AMD_PMC_REGISTER_RESPONSE:%x\n", value); |
| 396 | |
| 397 | value = amd_pmc_reg_read(dev, AMD_PMC_REGISTER_ARGUMENT); |
| 398 | dev_dbg(dev->dev, "AMD_PMC_REGISTER_ARGUMENT:%x\n", value); |
| 399 | |
| 400 | value = amd_pmc_reg_read(dev, AMD_PMC_REGISTER_MESSAGE); |
| 401 | dev_dbg(dev->dev, "AMD_PMC_REGISTER_MESSAGE:%x\n", value); |
| 402 | } |
| 403 | |
Mario Limonciello | 4c9dbf8 | 2021-10-20 11:29:45 -0500 | [diff] [blame] | 404 | static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, u32 arg, u32 *data, u8 msg, bool ret) |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 405 | { |
| 406 | int rc; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 407 | u32 val; |
| 408 | |
Shyam Sundar S K | 95e1b60 | 2021-06-29 14:17:57 +0530 | [diff] [blame] | 409 | mutex_lock(&dev->lock); |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 410 | /* Wait until we get a valid response */ |
| 411 | rc = readx_poll_timeout(ioread32, dev->regbase + AMD_PMC_REGISTER_RESPONSE, |
Shyam Sundar S K | 95e1b60 | 2021-06-29 14:17:57 +0530 | [diff] [blame] | 412 | val, val != 0, PMC_MSG_DELAY_MIN_US, |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 413 | PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX); |
| 414 | if (rc) { |
| 415 | dev_err(dev->dev, "failed to talk to SMU\n"); |
Yang Yingliang | 95edbbf | 2021-07-15 15:43:27 +0800 | [diff] [blame] | 416 | goto out_unlock; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* Write zero to response register */ |
| 420 | amd_pmc_reg_write(dev, AMD_PMC_REGISTER_RESPONSE, 0); |
| 421 | |
| 422 | /* Write argument into response register */ |
Mario Limonciello | 4c9dbf8 | 2021-10-20 11:29:45 -0500 | [diff] [blame] | 423 | amd_pmc_reg_write(dev, AMD_PMC_REGISTER_ARGUMENT, arg); |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 424 | |
| 425 | /* Write message ID to message ID register */ |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 426 | amd_pmc_reg_write(dev, AMD_PMC_REGISTER_MESSAGE, msg); |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 427 | |
Shyam Sundar S K | 95e1b60 | 2021-06-29 14:17:57 +0530 | [diff] [blame] | 428 | /* Wait until we get a valid response */ |
| 429 | rc = readx_poll_timeout(ioread32, dev->regbase + AMD_PMC_REGISTER_RESPONSE, |
| 430 | val, val != 0, PMC_MSG_DELAY_MIN_US, |
| 431 | PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX); |
| 432 | if (rc) { |
| 433 | dev_err(dev->dev, "SMU response timed out\n"); |
| 434 | goto out_unlock; |
| 435 | } |
| 436 | |
| 437 | switch (val) { |
| 438 | case AMD_PMC_RESULT_OK: |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 439 | if (ret) { |
| 440 | /* PMFW may take longer time to return back the data */ |
| 441 | usleep_range(DELAY_MIN_US, 10 * DELAY_MAX_US); |
| 442 | *data = amd_pmc_reg_read(dev, AMD_PMC_REGISTER_ARGUMENT); |
| 443 | } |
Shyam Sundar S K | 95e1b60 | 2021-06-29 14:17:57 +0530 | [diff] [blame] | 444 | break; |
| 445 | case AMD_PMC_RESULT_CMD_REJECT_BUSY: |
| 446 | dev_err(dev->dev, "SMU not ready. err: 0x%x\n", val); |
| 447 | rc = -EBUSY; |
| 448 | goto out_unlock; |
| 449 | case AMD_PMC_RESULT_CMD_UNKNOWN: |
| 450 | dev_err(dev->dev, "SMU cmd unknown. err: 0x%x\n", val); |
| 451 | rc = -EINVAL; |
| 452 | goto out_unlock; |
| 453 | case AMD_PMC_RESULT_CMD_REJECT_PREREQ: |
| 454 | case AMD_PMC_RESULT_FAILED: |
| 455 | default: |
| 456 | dev_err(dev->dev, "SMU cmd failed. err: 0x%x\n", val); |
| 457 | rc = -EIO; |
| 458 | goto out_unlock; |
| 459 | } |
| 460 | |
| 461 | out_unlock: |
| 462 | mutex_unlock(&dev->lock); |
Shyam Sundar S K | 162b937 | 2021-06-29 14:17:59 +0530 | [diff] [blame] | 463 | amd_pmc_dump_registers(dev); |
Shyam Sundar S K | 95e1b60 | 2021-06-29 14:17:57 +0530 | [diff] [blame] | 464 | return rc; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 465 | } |
| 466 | |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 467 | static int amd_pmc_get_os_hint(struct amd_pmc_dev *dev) |
| 468 | { |
| 469 | switch (dev->cpu_id) { |
| 470 | case AMD_CPU_ID_PCO: |
| 471 | return MSG_OS_HINT_PCO; |
| 472 | case AMD_CPU_ID_RN: |
Shyam Sundar S K | 83cbaf1 | 2021-06-29 14:18:03 +0530 | [diff] [blame] | 473 | case AMD_CPU_ID_YC: |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 474 | return MSG_OS_HINT_RN; |
| 475 | } |
| 476 | return -EINVAL; |
| 477 | } |
| 478 | |
Mario Limonciello | 5934840 | 2021-10-20 11:29:46 -0500 | [diff] [blame] | 479 | static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg) |
| 480 | { |
| 481 | struct rtc_device *rtc_device; |
| 482 | time64_t then, now, duration; |
| 483 | struct rtc_wkalrm alarm; |
| 484 | struct rtc_time tm; |
| 485 | int rc; |
| 486 | |
| 487 | if (pdev->major < 64 || (pdev->major == 64 && pdev->minor < 53)) |
| 488 | return 0; |
| 489 | |
Mario Limonciello | 2978891 | 2021-10-26 12:14:41 -0500 | [diff] [blame] | 490 | rtc_device = rtc_class_open("rtc0"); |
Mario Limonciello | 5934840 | 2021-10-20 11:29:46 -0500 | [diff] [blame] | 491 | if (!rtc_device) |
| 492 | return 0; |
| 493 | rc = rtc_read_alarm(rtc_device, &alarm); |
| 494 | if (rc) |
| 495 | return rc; |
| 496 | if (!alarm.enabled) { |
| 497 | dev_dbg(pdev->dev, "alarm not enabled\n"); |
| 498 | return 0; |
| 499 | } |
Mario Limonciello | 5934840 | 2021-10-20 11:29:46 -0500 | [diff] [blame] | 500 | rc = rtc_read_time(rtc_device, &tm); |
| 501 | if (rc) |
| 502 | return rc; |
| 503 | then = rtc_tm_to_time64(&alarm.time); |
| 504 | now = rtc_tm_to_time64(&tm); |
| 505 | duration = then-now; |
| 506 | |
| 507 | /* in the past */ |
| 508 | if (then < now) |
| 509 | return 0; |
| 510 | |
| 511 | /* will be stored in upper 16 bits of s0i3 hint argument, |
| 512 | * so timer wakeup from s0i3 is limited to ~18 hours or less |
| 513 | */ |
| 514 | if (duration <= 4 || duration > U16_MAX) |
| 515 | return -EINVAL; |
| 516 | |
| 517 | *arg |= (duration << 16); |
| 518 | rc = rtc_alarm_irq_enable(rtc_device, 0); |
Mario Limonciello | 16a035a | 2021-10-26 12:14:42 -0500 | [diff] [blame] | 519 | dev_dbg(pdev->dev, "wakeup timer programmed for %lld seconds\n", duration); |
Mario Limonciello | 5934840 | 2021-10-20 11:29:46 -0500 | [diff] [blame] | 520 | |
| 521 | return rc; |
| 522 | } |
| 523 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 524 | static int __maybe_unused amd_pmc_suspend(struct device *dev) |
| 525 | { |
| 526 | struct amd_pmc_dev *pdev = dev_get_drvdata(dev); |
| 527 | int rc; |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 528 | u8 msg; |
Mario Limonciello | 5934840 | 2021-10-20 11:29:46 -0500 | [diff] [blame] | 529 | u32 arg = 1; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 530 | |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 531 | /* Reset and Start SMU logging - to monitor the s0i3 stats */ |
| 532 | amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_RESET, 0); |
| 533 | amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_START, 0); |
| 534 | |
Mario Limonciello | 5934840 | 2021-10-20 11:29:46 -0500 | [diff] [blame] | 535 | /* Activate CZN specific RTC functionality */ |
| 536 | if (pdev->cpu_id == AMD_CPU_ID_CZN) { |
| 537 | rc = amd_pmc_verify_czn_rtc(pdev, &arg); |
| 538 | if (rc < 0) |
| 539 | return rc; |
| 540 | } |
| 541 | |
Sanket Goswami | f6045de | 2021-09-16 18:10:02 +0530 | [diff] [blame] | 542 | /* Dump the IdleMask before we send hint to SMU */ |
| 543 | amd_pmc_idlemask_read(pdev, dev, NULL); |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 544 | msg = amd_pmc_get_os_hint(pdev); |
Mario Limonciello | 5934840 | 2021-10-20 11:29:46 -0500 | [diff] [blame] | 545 | rc = amd_pmc_send_cmd(pdev, arg, NULL, msg, 0); |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 546 | if (rc) |
| 547 | dev_err(pdev->dev, "suspend failed\n"); |
| 548 | |
Sanket Goswami | 426c0ff | 2021-11-30 16:53:18 +0530 | [diff] [blame] | 549 | if (enable_stb) |
| 550 | rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_PREDEF); |
| 551 | if (rc) { |
| 552 | dev_err(pdev->dev, "error writing to STB\n"); |
| 553 | return rc; |
| 554 | } |
| 555 | |
Mario Limonciello | a973c98 | 2021-07-07 09:16:47 -0500 | [diff] [blame] | 556 | return rc; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static int __maybe_unused amd_pmc_resume(struct device *dev) |
| 560 | { |
| 561 | struct amd_pmc_dev *pdev = dev_get_drvdata(dev); |
| 562 | int rc; |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 563 | u8 msg; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 564 | |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 565 | msg = amd_pmc_get_os_hint(pdev); |
| 566 | rc = amd_pmc_send_cmd(pdev, 0, NULL, msg, 0); |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 567 | if (rc) |
| 568 | dev_err(pdev->dev, "resume failed\n"); |
| 569 | |
Sanket Goswami | 9c93f8f | 2021-09-21 17:29:10 +0530 | [diff] [blame] | 570 | /* Let SMU know that we are looking for stats */ |
| 571 | amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_DUMP_DATA, 0); |
| 572 | |
Sanket Goswami | f6045de | 2021-09-16 18:10:02 +0530 | [diff] [blame] | 573 | /* Dump the IdleMask to see the blockers */ |
| 574 | amd_pmc_idlemask_read(pdev, dev, NULL); |
| 575 | |
Sanket Goswami | 426c0ff | 2021-11-30 16:53:18 +0530 | [diff] [blame] | 576 | /* Write data incremented by 1 to distinguish in stb_read */ |
| 577 | if (enable_stb) |
| 578 | rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_PREDEF + 1); |
| 579 | if (rc) { |
| 580 | dev_err(pdev->dev, "error writing to STB\n"); |
| 581 | return rc; |
| 582 | } |
| 583 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static const struct dev_pm_ops amd_pmc_pm_ops = { |
Mario Limonciello | d386f7e | 2021-12-10 08:35:29 -0600 | [diff] [blame] | 588 | .suspend_noirq = amd_pmc_suspend, |
| 589 | .resume_noirq = amd_pmc_resume, |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | static const struct pci_device_id pmc_pci_ids[] = { |
Shyam Sundar S K | 83cbaf1 | 2021-06-29 14:18:03 +0530 | [diff] [blame] | 593 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_YC) }, |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 594 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CZN) }, |
| 595 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RN) }, |
| 596 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PCO) }, |
| 597 | { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RV) }, |
| 598 | { } |
| 599 | }; |
| 600 | |
Sanket Goswami | 426c0ff | 2021-11-30 16:53:18 +0530 | [diff] [blame] | 601 | static int amd_pmc_write_stb(struct amd_pmc_dev *dev, u32 data) |
| 602 | { |
| 603 | int err; |
| 604 | |
| 605 | err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_ADDRESS, AMD_PMC_STB_PMI_0); |
| 606 | if (err) { |
| 607 | dev_err(dev->dev, "failed to write addr in stb: 0x%X\n", |
| 608 | AMD_PMC_STB_INDEX_ADDRESS); |
| 609 | return pcibios_err_to_errno(err); |
| 610 | } |
| 611 | |
| 612 | err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_DATA, data); |
| 613 | if (err) { |
| 614 | dev_err(dev->dev, "failed to write data in stb: 0x%X\n", |
| 615 | AMD_PMC_STB_INDEX_DATA); |
| 616 | return pcibios_err_to_errno(err); |
| 617 | } |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int amd_pmc_read_stb(struct amd_pmc_dev *dev, u32 *buf) |
| 623 | { |
| 624 | int i, err; |
| 625 | |
| 626 | err = pci_write_config_dword(dev->rdev, AMD_PMC_STB_INDEX_ADDRESS, AMD_PMC_STB_PMI_0); |
| 627 | if (err) { |
| 628 | dev_err(dev->dev, "error writing addr to stb: 0x%X\n", |
| 629 | AMD_PMC_STB_INDEX_ADDRESS); |
| 630 | return pcibios_err_to_errno(err); |
| 631 | } |
| 632 | |
| 633 | for (i = 0; i < FIFO_SIZE; i++) { |
| 634 | err = pci_read_config_dword(dev->rdev, AMD_PMC_STB_INDEX_DATA, buf++); |
| 635 | if (err) { |
| 636 | dev_err(dev->dev, "error reading data from stb: 0x%X\n", |
| 637 | AMD_PMC_STB_INDEX_DATA); |
| 638 | return pcibios_err_to_errno(err); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 645 | static int amd_pmc_probe(struct platform_device *pdev) |
| 646 | { |
| 647 | struct amd_pmc_dev *dev = &pmc; |
| 648 | struct pci_dev *rdev; |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 649 | u32 base_addr_lo, base_addr_hi; |
Shyam Sundar S K | b9a4fa6 | 2021-06-29 14:18:01 +0530 | [diff] [blame] | 650 | u64 base_addr, fch_phys_addr; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 651 | int err; |
| 652 | u32 val; |
| 653 | |
| 654 | dev->dev = &pdev->dev; |
| 655 | |
| 656 | rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0)); |
Pan Bian | 745ed17 | 2021-01-20 20:50:05 -0800 | [diff] [blame] | 657 | if (!rdev || !pci_match_id(pmc_pci_ids, rdev)) { |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 658 | err = -ENODEV; |
| 659 | goto err_pci_dev_put; |
Pan Bian | 745ed17 | 2021-01-20 20:50:05 -0800 | [diff] [blame] | 660 | } |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 661 | |
| 662 | dev->cpu_id = rdev->device; |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 663 | dev->rdev = rdev; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 664 | err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_LO); |
| 665 | if (err) { |
| 666 | dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS); |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 667 | err = pcibios_err_to_errno(err); |
| 668 | goto err_pci_dev_put; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val); |
Pan Bian | 745ed17 | 2021-01-20 20:50:05 -0800 | [diff] [blame] | 672 | if (err) { |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 673 | err = pcibios_err_to_errno(err); |
| 674 | goto err_pci_dev_put; |
Pan Bian | 745ed17 | 2021-01-20 20:50:05 -0800 | [diff] [blame] | 675 | } |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 676 | |
| 677 | base_addr_lo = val & AMD_PMC_BASE_ADDR_HI_MASK; |
| 678 | |
| 679 | err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_HI); |
| 680 | if (err) { |
| 681 | dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS); |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 682 | err = pcibios_err_to_errno(err); |
| 683 | goto err_pci_dev_put; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val); |
Pan Bian | 745ed17 | 2021-01-20 20:50:05 -0800 | [diff] [blame] | 687 | if (err) { |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 688 | err = pcibios_err_to_errno(err); |
| 689 | goto err_pci_dev_put; |
Pan Bian | 745ed17 | 2021-01-20 20:50:05 -0800 | [diff] [blame] | 690 | } |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 691 | |
| 692 | base_addr_hi = val & AMD_PMC_BASE_ADDR_LO_MASK; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 693 | base_addr = ((u64)base_addr_hi << 32 | base_addr_lo); |
| 694 | |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 695 | dev->regbase = devm_ioremap(dev->dev, base_addr + AMD_PMC_BASE_ADDR_OFFSET, |
| 696 | AMD_PMC_MAPPING_SIZE); |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 697 | if (!dev->regbase) { |
| 698 | err = -ENOMEM; |
| 699 | goto err_pci_dev_put; |
| 700 | } |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 701 | |
Shyam Sundar S K | 95e1b60 | 2021-06-29 14:17:57 +0530 | [diff] [blame] | 702 | mutex_init(&dev->lock); |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 703 | |
Shyam Sundar S K | b9a4fa6 | 2021-06-29 14:18:01 +0530 | [diff] [blame] | 704 | /* Use FCH registers to get the S0ix stats */ |
| 705 | base_addr_lo = FCH_BASE_PHY_ADDR_LOW; |
| 706 | base_addr_hi = FCH_BASE_PHY_ADDR_HIGH; |
| 707 | fch_phys_addr = ((u64)base_addr_hi << 32 | base_addr_lo); |
| 708 | dev->fch_virt_addr = devm_ioremap(dev->dev, fch_phys_addr, FCH_SSC_MAPPING_SIZE); |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 709 | if (!dev->fch_virt_addr) { |
| 710 | err = -ENOMEM; |
| 711 | goto err_pci_dev_put; |
| 712 | } |
Shyam Sundar S K | b9a4fa6 | 2021-06-29 14:18:01 +0530 | [diff] [blame] | 713 | |
Shyam Sundar S K | 7662056 | 2021-06-29 14:18:00 +0530 | [diff] [blame] | 714 | /* Use SMU to get the s0i3 debug stats */ |
| 715 | err = amd_pmc_setup_smu_logging(dev); |
| 716 | if (err) |
| 717 | dev_err(dev->dev, "SMU debugging info not supported on this platform\n"); |
| 718 | |
Sanket Goswami | f6045de | 2021-09-16 18:10:02 +0530 | [diff] [blame] | 719 | amd_pmc_get_smu_version(dev); |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 720 | platform_set_drvdata(pdev, dev); |
| 721 | amd_pmc_dbgfs_register(dev); |
| 722 | return 0; |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 723 | |
| 724 | err_pci_dev_put: |
| 725 | pci_dev_put(rdev); |
| 726 | return err; |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static int amd_pmc_remove(struct platform_device *pdev) |
| 730 | { |
| 731 | struct amd_pmc_dev *dev = platform_get_drvdata(pdev); |
| 732 | |
| 733 | amd_pmc_dbgfs_unregister(dev); |
Sanket Goswami | 6a5a14b | 2021-11-30 16:53:17 +0530 | [diff] [blame] | 734 | pci_dev_put(dev->rdev); |
Shyam Sundar S K | 95e1b60 | 2021-06-29 14:17:57 +0530 | [diff] [blame] | 735 | mutex_destroy(&dev->lock); |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static const struct acpi_device_id amd_pmc_acpi_ids[] = { |
| 740 | {"AMDI0005", 0}, |
Shyam Sundar S K | 9422584 | 2021-06-29 14:18:02 +0530 | [diff] [blame] | 741 | {"AMDI0006", 0}, |
Shyam Sundar S K | 83cbaf1 | 2021-06-29 14:18:03 +0530 | [diff] [blame] | 742 | {"AMDI0007", 0}, |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 743 | {"AMD0004", 0}, |
Sachi King | 432cce2 | 2021-10-02 14:18:39 +1000 | [diff] [blame] | 744 | {"AMD0005", 0}, |
Shyam Sundar S K | 156ec47 | 2020-11-05 19:35:31 +0530 | [diff] [blame] | 745 | { } |
| 746 | }; |
| 747 | MODULE_DEVICE_TABLE(acpi, amd_pmc_acpi_ids); |
| 748 | |
| 749 | static struct platform_driver amd_pmc_driver = { |
| 750 | .driver = { |
| 751 | .name = "amd_pmc", |
| 752 | .acpi_match_table = amd_pmc_acpi_ids, |
| 753 | .pm = &amd_pmc_pm_ops, |
| 754 | }, |
| 755 | .probe = amd_pmc_probe, |
| 756 | .remove = amd_pmc_remove, |
| 757 | }; |
| 758 | module_platform_driver(amd_pmc_driver); |
| 759 | |
| 760 | MODULE_LICENSE("GPL v2"); |
| 761 | MODULE_DESCRIPTION("AMD PMC Driver"); |