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/module.h>
21 #include <linux/pci.h>
22 #include <linux/platform_device.h>
23 #include <linux/suspend.h>
24 #include <linux/seq_file.h>
25 #include <linux/uaccess.h>
27 /* SMU communication registers */
28 #define AMD_PMC_REGISTER_MESSAGE 0x538
29 #define AMD_PMC_REGISTER_RESPONSE 0x980
30 #define AMD_PMC_REGISTER_ARGUMENT 0x9BC
32 /* Base address of SMU for mapping physical address to virtual address */
33 #define AMD_PMC_SMU_INDEX_ADDRESS 0xB8
34 #define AMD_PMC_SMU_INDEX_DATA 0xBC
35 #define AMD_PMC_MAPPING_SIZE 0x01000
36 #define AMD_PMC_BASE_ADDR_OFFSET 0x10000
37 #define AMD_PMC_BASE_ADDR_LO 0x13B102E8
38 #define AMD_PMC_BASE_ADDR_HI 0x13B102EC
39 #define AMD_PMC_BASE_ADDR_LO_MASK GENMASK(15, 0)
40 #define AMD_PMC_BASE_ADDR_HI_MASK GENMASK(31, 20)
42 /* SMU Response Codes */
43 #define AMD_PMC_RESULT_OK 0x01
44 #define AMD_PMC_RESULT_CMD_REJECT_BUSY 0xFC
45 #define AMD_PMC_RESULT_CMD_REJECT_PREREQ 0xFD
46 #define AMD_PMC_RESULT_CMD_UNKNOWN 0xFE
47 #define AMD_PMC_RESULT_FAILED 0xFF
49 /* FCH SSC Registers */
50 #define FCH_S0I3_ENTRY_TIME_L_OFFSET 0x30
51 #define FCH_S0I3_ENTRY_TIME_H_OFFSET 0x34
52 #define FCH_S0I3_EXIT_TIME_L_OFFSET 0x38
53 #define FCH_S0I3_EXIT_TIME_H_OFFSET 0x3C
54 #define FCH_SSC_MAPPING_SIZE 0x800
55 #define FCH_BASE_PHY_ADDR_LOW 0xFED81100
56 #define FCH_BASE_PHY_ADDR_HIGH 0x00000000
58 /* SMU Message Definations */
59 #define SMU_MSG_GETSMUVERSION 0x02
60 #define SMU_MSG_LOG_GETDRAM_ADDR_HI 0x04
61 #define SMU_MSG_LOG_GETDRAM_ADDR_LO 0x05
62 #define SMU_MSG_LOG_START 0x06
63 #define SMU_MSG_LOG_RESET 0x07
64 #define SMU_MSG_LOG_DUMP_DATA 0x08
65 #define SMU_MSG_GET_SUP_CONSTRAINTS 0x09
66 /* List of supported CPU ids */
67 #define AMD_CPU_ID_RV 0x15D0
68 #define AMD_CPU_ID_RN 0x1630
69 #define AMD_CPU_ID_PCO AMD_CPU_ID_RV
70 #define AMD_CPU_ID_CZN AMD_CPU_ID_RN
71 #define AMD_CPU_ID_YC 0x14B5
73 #define PMC_MSG_DELAY_MIN_US 100
74 #define RESPONSE_REGISTER_LOOP_MAX 20000
76 #define SOC_SUBSYSTEM_IP_MAX 12
77 #define DELAY_MIN_US 2000
78 #define DELAY_MAX_US 3000
85 struct amd_pmc_bit_map {
90 static const struct amd_pmc_bit_map soc15_ip_blk[] = {
107 void __iomem *regbase;
108 void __iomem *smu_virt_addr;
109 void __iomem *fch_virt_addr;
114 struct mutex lock; /* generic mutex lock */
115 #if IS_ENABLED(CONFIG_DEBUG_FS)
116 struct dentry *dbgfs_dir;
117 #endif /* CONFIG_DEBUG_FS */
120 static struct amd_pmc_dev pmc;
121 static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, bool set, u32 *data, u8 msg, bool ret);
123 static inline u32 amd_pmc_reg_read(struct amd_pmc_dev *dev, int reg_offset)
125 return ioread32(dev->regbase + reg_offset);
128 static inline void amd_pmc_reg_write(struct amd_pmc_dev *dev, int reg_offset, u32 val)
130 iowrite32(val, dev->regbase + reg_offset);
138 u64 timeentering_s0i3_lastcapture;
139 u64 timeentering_s0i3_totaltime;
140 u64 timeto_resume_to_os_lastcapture;
141 u64 timeto_resume_to_os_totaltime;
142 u64 timein_s0i3_lastcapture;
143 u64 timein_s0i3_totaltime;
144 u64 timein_swdrips_lastcapture;
145 u64 timein_swdrips_totaltime;
146 u64 timecondition_notmet_lastcapture[SOC_SUBSYSTEM_IP_MAX];
147 u64 timecondition_notmet_totaltime[SOC_SUBSYSTEM_IP_MAX];
150 #ifdef CONFIG_DEBUG_FS
151 static int smu_fw_info_show(struct seq_file *s, void *unused)
153 struct amd_pmc_dev *dev = s->private;
154 struct smu_metrics table;
157 if (dev->cpu_id == AMD_CPU_ID_PCO)
160 memcpy_fromio(&table, dev->smu_virt_addr, sizeof(struct smu_metrics));
162 seq_puts(s, "\n=== SMU Statistics ===\n");
163 seq_printf(s, "Table Version: %d\n", table.table_version);
164 seq_printf(s, "Hint Count: %d\n", table.hint_count);
165 seq_printf(s, "S0i3 Cycle Count: %d\n", table.s0i3_cyclecount);
166 seq_printf(s, "Time (in us) to S0i3: %lld\n", table.timeentering_s0i3_lastcapture);
167 seq_printf(s, "Time (in us) in S0i3: %lld\n", table.timein_s0i3_lastcapture);
169 seq_puts(s, "\n=== Active time (in us) ===\n");
170 for (idx = 0 ; idx < SOC_SUBSYSTEM_IP_MAX ; idx++) {
171 if (soc15_ip_blk[idx].bit_mask & dev->active_ips)
172 seq_printf(s, "%-8s : %lld\n", soc15_ip_blk[idx].name,
173 table.timecondition_notmet_lastcapture[idx]);
178 DEFINE_SHOW_ATTRIBUTE(smu_fw_info);
180 static int s0ix_stats_show(struct seq_file *s, void *unused)
182 struct amd_pmc_dev *dev = s->private;
183 u64 entry_time, exit_time, residency;
185 entry_time = ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_H_OFFSET);
186 entry_time = entry_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_ENTRY_TIME_L_OFFSET);
188 exit_time = ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_H_OFFSET);
189 exit_time = exit_time << 32 | ioread32(dev->fch_virt_addr + FCH_S0I3_EXIT_TIME_L_OFFSET);
191 /* It's in 48MHz. We need to convert it */
192 residency = exit_time - entry_time;
193 do_div(residency, 48);
195 seq_puts(s, "=== S0ix statistics ===\n");
196 seq_printf(s, "S0ix Entry Time: %lld\n", entry_time);
197 seq_printf(s, "S0ix Exit Time: %lld\n", exit_time);
198 seq_printf(s, "Residency Time: %lld\n", residency);
202 DEFINE_SHOW_ATTRIBUTE(s0ix_stats);
204 static void amd_pmc_dbgfs_unregister(struct amd_pmc_dev *dev)
206 debugfs_remove_recursive(dev->dbgfs_dir);
209 static void amd_pmc_dbgfs_register(struct amd_pmc_dev *dev)
211 dev->dbgfs_dir = debugfs_create_dir("amd_pmc", NULL);
212 debugfs_create_file("smu_fw_info", 0644, dev->dbgfs_dir, dev,
214 debugfs_create_file("s0ix_stats", 0644, dev->dbgfs_dir, dev,
218 static inline void amd_pmc_dbgfs_register(struct amd_pmc_dev *dev)
222 static inline void amd_pmc_dbgfs_unregister(struct amd_pmc_dev *dev)
225 #endif /* CONFIG_DEBUG_FS */
227 static int amd_pmc_setup_smu_logging(struct amd_pmc_dev *dev)
229 u32 phys_addr_low, phys_addr_hi;
232 if (dev->cpu_id == AMD_CPU_ID_PCO)
235 /* Get Active devices list from SMU */
236 amd_pmc_send_cmd(dev, 0, &dev->active_ips, SMU_MSG_GET_SUP_CONSTRAINTS, 1);
238 /* Get dram address */
239 amd_pmc_send_cmd(dev, 0, &phys_addr_low, SMU_MSG_LOG_GETDRAM_ADDR_LO, 1);
240 amd_pmc_send_cmd(dev, 0, &phys_addr_hi, SMU_MSG_LOG_GETDRAM_ADDR_HI, 1);
241 smu_phys_addr = ((u64)phys_addr_hi << 32 | phys_addr_low);
243 dev->smu_virt_addr = devm_ioremap(dev->dev, smu_phys_addr, sizeof(struct smu_metrics));
244 if (!dev->smu_virt_addr)
247 /* Start the logging */
248 amd_pmc_send_cmd(dev, 0, NULL, SMU_MSG_LOG_START, 0);
253 static void amd_pmc_dump_registers(struct amd_pmc_dev *dev)
257 value = amd_pmc_reg_read(dev, AMD_PMC_REGISTER_RESPONSE);
258 dev_dbg(dev->dev, "AMD_PMC_REGISTER_RESPONSE:%x\n", value);
260 value = amd_pmc_reg_read(dev, AMD_PMC_REGISTER_ARGUMENT);
261 dev_dbg(dev->dev, "AMD_PMC_REGISTER_ARGUMENT:%x\n", value);
263 value = amd_pmc_reg_read(dev, AMD_PMC_REGISTER_MESSAGE);
264 dev_dbg(dev->dev, "AMD_PMC_REGISTER_MESSAGE:%x\n", value);
267 static int amd_pmc_send_cmd(struct amd_pmc_dev *dev, bool set, u32 *data, u8 msg, bool ret)
272 mutex_lock(&dev->lock);
273 /* Wait until we get a valid response */
274 rc = readx_poll_timeout(ioread32, dev->regbase + AMD_PMC_REGISTER_RESPONSE,
275 val, val != 0, PMC_MSG_DELAY_MIN_US,
276 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
278 dev_err(dev->dev, "failed to talk to SMU\n");
282 /* Write zero to response register */
283 amd_pmc_reg_write(dev, AMD_PMC_REGISTER_RESPONSE, 0);
285 /* Write argument into response register */
286 amd_pmc_reg_write(dev, AMD_PMC_REGISTER_ARGUMENT, set);
288 /* Write message ID to message ID register */
289 amd_pmc_reg_write(dev, AMD_PMC_REGISTER_MESSAGE, msg);
291 /* Wait until we get a valid response */
292 rc = readx_poll_timeout(ioread32, dev->regbase + AMD_PMC_REGISTER_RESPONSE,
293 val, val != 0, PMC_MSG_DELAY_MIN_US,
294 PMC_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
296 dev_err(dev->dev, "SMU response timed out\n");
301 case AMD_PMC_RESULT_OK:
303 /* PMFW may take longer time to return back the data */
304 usleep_range(DELAY_MIN_US, 10 * DELAY_MAX_US);
305 *data = amd_pmc_reg_read(dev, AMD_PMC_REGISTER_ARGUMENT);
308 case AMD_PMC_RESULT_CMD_REJECT_BUSY:
309 dev_err(dev->dev, "SMU not ready. err: 0x%x\n", val);
312 case AMD_PMC_RESULT_CMD_UNKNOWN:
313 dev_err(dev->dev, "SMU cmd unknown. err: 0x%x\n", val);
316 case AMD_PMC_RESULT_CMD_REJECT_PREREQ:
317 case AMD_PMC_RESULT_FAILED:
319 dev_err(dev->dev, "SMU cmd failed. err: 0x%x\n", val);
325 mutex_unlock(&dev->lock);
326 amd_pmc_dump_registers(dev);
330 static int amd_pmc_get_os_hint(struct amd_pmc_dev *dev)
332 switch (dev->cpu_id) {
334 return MSG_OS_HINT_PCO;
337 return MSG_OS_HINT_RN;
342 static int __maybe_unused amd_pmc_suspend(struct device *dev)
344 struct amd_pmc_dev *pdev = dev_get_drvdata(dev);
348 /* Reset and Start SMU logging - to monitor the s0i3 stats */
349 amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_RESET, 0);
350 amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_START, 0);
352 msg = amd_pmc_get_os_hint(pdev);
353 rc = amd_pmc_send_cmd(pdev, 1, NULL, msg, 0);
355 dev_err(pdev->dev, "suspend failed\n");
360 static int __maybe_unused amd_pmc_resume(struct device *dev)
362 struct amd_pmc_dev *pdev = dev_get_drvdata(dev);
366 /* Let SMU know that we are looking for stats */
367 amd_pmc_send_cmd(pdev, 0, NULL, SMU_MSG_LOG_DUMP_DATA, 0);
369 msg = amd_pmc_get_os_hint(pdev);
370 rc = amd_pmc_send_cmd(pdev, 0, NULL, msg, 0);
372 dev_err(pdev->dev, "resume failed\n");
377 static const struct dev_pm_ops amd_pmc_pm_ops = {
378 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(amd_pmc_suspend, amd_pmc_resume)
381 static const struct pci_device_id pmc_pci_ids[] = {
382 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_YC) },
383 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_CZN) },
384 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RN) },
385 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PCO) },
386 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RV) },
390 static int amd_pmc_probe(struct platform_device *pdev)
392 struct amd_pmc_dev *dev = &pmc;
393 struct pci_dev *rdev;
394 u32 base_addr_lo, base_addr_hi;
395 u64 base_addr, fch_phys_addr;
399 dev->dev = &pdev->dev;
401 rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
402 if (!rdev || !pci_match_id(pmc_pci_ids, rdev)) {
407 dev->cpu_id = rdev->device;
408 err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_LO);
410 dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS);
412 return pcibios_err_to_errno(err);
415 err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val);
418 return pcibios_err_to_errno(err);
421 base_addr_lo = val & AMD_PMC_BASE_ADDR_HI_MASK;
423 err = pci_write_config_dword(rdev, AMD_PMC_SMU_INDEX_ADDRESS, AMD_PMC_BASE_ADDR_HI);
425 dev_err(dev->dev, "error writing to 0x%x\n", AMD_PMC_SMU_INDEX_ADDRESS);
427 return pcibios_err_to_errno(err);
430 err = pci_read_config_dword(rdev, AMD_PMC_SMU_INDEX_DATA, &val);
433 return pcibios_err_to_errno(err);
436 base_addr_hi = val & AMD_PMC_BASE_ADDR_LO_MASK;
438 base_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
440 dev->regbase = devm_ioremap(dev->dev, base_addr + AMD_PMC_BASE_ADDR_OFFSET,
441 AMD_PMC_MAPPING_SIZE);
445 mutex_init(&dev->lock);
447 /* Use FCH registers to get the S0ix stats */
448 base_addr_lo = FCH_BASE_PHY_ADDR_LOW;
449 base_addr_hi = FCH_BASE_PHY_ADDR_HIGH;
450 fch_phys_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
451 dev->fch_virt_addr = devm_ioremap(dev->dev, fch_phys_addr, FCH_SSC_MAPPING_SIZE);
452 if (!dev->fch_virt_addr)
455 /* Use SMU to get the s0i3 debug stats */
456 err = amd_pmc_setup_smu_logging(dev);
458 dev_err(dev->dev, "SMU debugging info not supported on this platform\n");
460 platform_set_drvdata(pdev, dev);
461 amd_pmc_dbgfs_register(dev);
465 static int amd_pmc_remove(struct platform_device *pdev)
467 struct amd_pmc_dev *dev = platform_get_drvdata(pdev);
469 amd_pmc_dbgfs_unregister(dev);
470 mutex_destroy(&dev->lock);
474 static const struct acpi_device_id amd_pmc_acpi_ids[] = {
481 MODULE_DEVICE_TABLE(acpi, amd_pmc_acpi_ids);
483 static struct platform_driver amd_pmc_driver = {
486 .acpi_match_table = amd_pmc_acpi_ids,
487 .pm = &amd_pmc_pm_ops,
489 .probe = amd_pmc_probe,
490 .remove = amd_pmc_remove,
492 module_platform_driver(amd_pmc_driver);
494 MODULE_LICENSE("GPL v2");
495 MODULE_DESCRIPTION("AMD PMC Driver");