1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AMD Platform Management Framework Driver
5 * Copyright (c) 2022, Advanced Micro Devices, Inc.
11 #include <asm/amd_nb.h>
12 #include <linux/debugfs.h>
13 #include <linux/iopoll.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/platform_device.h>
17 #include <linux/power_supply.h>
20 /* PMF-SMU communication registers */
21 #define AMD_PMF_REGISTER_MESSAGE 0xA18
22 #define AMD_PMF_REGISTER_RESPONSE 0xA78
23 #define AMD_PMF_REGISTER_ARGUMENT 0xA58
25 /* Base address of SMU for mapping physical address to virtual address */
26 #define AMD_PMF_MAPPING_SIZE 0x01000
27 #define AMD_PMF_BASE_ADDR_OFFSET 0x10000
28 #define AMD_PMF_BASE_ADDR_LO 0x13B102E8
29 #define AMD_PMF_BASE_ADDR_HI 0x13B102EC
30 #define AMD_PMF_BASE_ADDR_LO_MASK GENMASK(15, 0)
31 #define AMD_PMF_BASE_ADDR_HI_MASK GENMASK(31, 20)
33 /* SMU Response Codes */
34 #define AMD_PMF_RESULT_OK 0x01
35 #define AMD_PMF_RESULT_CMD_REJECT_BUSY 0xFC
36 #define AMD_PMF_RESULT_CMD_REJECT_PREREQ 0xFD
37 #define AMD_PMF_RESULT_CMD_UNKNOWN 0xFE
38 #define AMD_PMF_RESULT_FAILED 0xFF
40 /* List of supported CPU ids */
41 #define AMD_CPU_ID_RMB 0x14b5
42 #define AMD_CPU_ID_PS 0x14e8
43 #define PCI_DEVICE_ID_AMD_1AH_M20H_ROOT 0x1507
45 #define PMF_MSG_DELAY_MIN_US 50
46 #define RESPONSE_REGISTER_LOOP_MAX 20000
48 #define DELAY_MIN_US 2000
49 #define DELAY_MAX_US 3000
51 /* override Metrics Table sample size time (in ms) */
52 static int metrics_table_loop_ms = 1000;
53 module_param(metrics_table_loop_ms, int, 0644);
54 MODULE_PARM_DESC(metrics_table_loop_ms, "Metrics Table sample size time (default = 1000ms)");
56 /* Force load on supported older platforms */
57 static bool force_load;
58 module_param(force_load, bool, 0444);
59 MODULE_PARM_DESC(force_load, "Force load this driver on supported older platforms (experimental)");
61 static int amd_pmf_pwr_src_notify_call(struct notifier_block *nb, unsigned long event, void *data)
63 struct amd_pmf_dev *pmf = container_of(nb, struct amd_pmf_dev, pwr_src_notifier);
65 if (event != PSY_EVENT_PROP_CHANGED)
68 if (is_apmf_func_supported(pmf, APMF_FUNC_AUTO_MODE) ||
69 is_apmf_func_supported(pmf, APMF_FUNC_DYN_SLIDER_DC) ||
70 is_apmf_func_supported(pmf, APMF_FUNC_DYN_SLIDER_AC)) {
71 if ((pmf->amt_enabled || pmf->cnqf_enabled) && is_pprof_balanced(pmf))
75 if (is_apmf_func_supported(pmf, APMF_FUNC_STATIC_SLIDER_GRANULAR))
76 amd_pmf_set_sps_power_limits(pmf);
78 if (is_apmf_func_supported(pmf, APMF_FUNC_OS_POWER_SLIDER_UPDATE))
79 amd_pmf_power_slider_update_event(pmf);
84 static int current_power_limits_show(struct seq_file *seq, void *unused)
86 struct amd_pmf_dev *dev = seq->private;
87 struct amd_pmf_static_slider_granular table;
90 mode = amd_pmf_get_pprof_modes(dev);
94 src = amd_pmf_get_power_source();
95 amd_pmf_update_slider(dev, SLIDER_OP_GET, mode, &table);
96 seq_printf(seq, "spl:%u fppt:%u sppt:%u sppt_apu_only:%u stt_min:%u stt[APU]:%u stt[HS2]: %u\n",
97 table.prop[src][mode].spl,
98 table.prop[src][mode].fppt,
99 table.prop[src][mode].sppt,
100 table.prop[src][mode].sppt_apu_only,
101 table.prop[src][mode].stt_min,
102 table.prop[src][mode].stt_skin_temp[STT_TEMP_APU],
103 table.prop[src][mode].stt_skin_temp[STT_TEMP_HS2]);
106 DEFINE_SHOW_ATTRIBUTE(current_power_limits);
108 static void amd_pmf_dbgfs_unregister(struct amd_pmf_dev *dev)
110 debugfs_remove_recursive(dev->dbgfs_dir);
113 static void amd_pmf_dbgfs_register(struct amd_pmf_dev *dev)
115 dev->dbgfs_dir = debugfs_create_dir("amd_pmf", NULL);
116 if (dev->pmf_if_version == PMF_IF_V1)
117 debugfs_create_file("current_power_limits", 0644, dev->dbgfs_dir, dev,
118 ¤t_power_limits_fops);
121 int amd_pmf_get_power_source(void)
123 if (power_supply_is_system_supplied() > 0)
124 return POWER_SOURCE_AC;
126 return POWER_SOURCE_DC;
129 static void amd_pmf_get_metrics(struct work_struct *work)
131 struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, work_buffer.work);
132 ktime_t time_elapsed_ms;
135 mutex_lock(&dev->update_mutex);
136 /* Transfer table contents */
137 memset(dev->buf, 0, sizeof(dev->m_table));
138 amd_pmf_send_cmd(dev, SET_TRANSFER_TABLE, 0, 7, NULL);
139 memcpy(&dev->m_table, dev->buf, sizeof(dev->m_table));
141 time_elapsed_ms = ktime_to_ms(ktime_get()) - dev->start_time;
142 /* Calculate the avg SoC power consumption */
143 socket_power = dev->m_table.apu_power + dev->m_table.dgpu_power;
145 if (dev->amt_enabled) {
146 /* Apply the Auto Mode transition */
147 amd_pmf_trans_automode(dev, socket_power, time_elapsed_ms);
150 if (dev->cnqf_enabled) {
151 /* Apply the CnQF transition */
152 amd_pmf_trans_cnqf(dev, socket_power, time_elapsed_ms);
155 dev->start_time = ktime_to_ms(ktime_get());
156 schedule_delayed_work(&dev->work_buffer, msecs_to_jiffies(metrics_table_loop_ms));
157 mutex_unlock(&dev->update_mutex);
160 static inline u32 amd_pmf_reg_read(struct amd_pmf_dev *dev, int reg_offset)
162 return ioread32(dev->regbase + reg_offset);
165 static inline void amd_pmf_reg_write(struct amd_pmf_dev *dev, int reg_offset, u32 val)
167 iowrite32(val, dev->regbase + reg_offset);
170 static void __maybe_unused amd_pmf_dump_registers(struct amd_pmf_dev *dev)
174 value = amd_pmf_reg_read(dev, AMD_PMF_REGISTER_RESPONSE);
175 dev_dbg(dev->dev, "AMD_PMF_REGISTER_RESPONSE:%x\n", value);
177 value = amd_pmf_reg_read(dev, AMD_PMF_REGISTER_ARGUMENT);
178 dev_dbg(dev->dev, "AMD_PMF_REGISTER_ARGUMENT:%d\n", value);
180 value = amd_pmf_reg_read(dev, AMD_PMF_REGISTER_MESSAGE);
181 dev_dbg(dev->dev, "AMD_PMF_REGISTER_MESSAGE:%x\n", value);
184 int amd_pmf_send_cmd(struct amd_pmf_dev *dev, u8 message, bool get, u32 arg, u32 *data)
189 mutex_lock(&dev->lock);
191 /* Wait until we get a valid response */
192 rc = readx_poll_timeout(ioread32, dev->regbase + AMD_PMF_REGISTER_RESPONSE,
193 val, val != 0, PMF_MSG_DELAY_MIN_US,
194 PMF_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
196 dev_err(dev->dev, "failed to talk to SMU\n");
200 /* Write zero to response register */
201 amd_pmf_reg_write(dev, AMD_PMF_REGISTER_RESPONSE, 0);
203 /* Write argument into argument register */
204 amd_pmf_reg_write(dev, AMD_PMF_REGISTER_ARGUMENT, arg);
206 /* Write message ID to message ID register */
207 amd_pmf_reg_write(dev, AMD_PMF_REGISTER_MESSAGE, message);
209 /* Wait until we get a valid response */
210 rc = readx_poll_timeout(ioread32, dev->regbase + AMD_PMF_REGISTER_RESPONSE,
211 val, val != 0, PMF_MSG_DELAY_MIN_US,
212 PMF_MSG_DELAY_MIN_US * RESPONSE_REGISTER_LOOP_MAX);
214 dev_err(dev->dev, "SMU response timed out\n");
219 case AMD_PMF_RESULT_OK:
221 /* PMFW may take longer time to return back the data */
222 usleep_range(DELAY_MIN_US, 10 * DELAY_MAX_US);
223 *data = amd_pmf_reg_read(dev, AMD_PMF_REGISTER_ARGUMENT);
226 case AMD_PMF_RESULT_CMD_REJECT_BUSY:
227 dev_err(dev->dev, "SMU not ready. err: 0x%x\n", val);
230 case AMD_PMF_RESULT_CMD_UNKNOWN:
231 dev_err(dev->dev, "SMU cmd unknown. err: 0x%x\n", val);
234 case AMD_PMF_RESULT_CMD_REJECT_PREREQ:
235 case AMD_PMF_RESULT_FAILED:
237 dev_err(dev->dev, "SMU cmd failed. err: 0x%x\n", val);
243 mutex_unlock(&dev->lock);
244 amd_pmf_dump_registers(dev);
248 static const struct pci_device_id pmf_pci_ids[] = {
249 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_RMB) },
250 { PCI_DEVICE(PCI_VENDOR_ID_AMD, AMD_CPU_ID_PS) },
251 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_1AH_M20H_ROOT) },
255 int amd_pmf_set_dram_addr(struct amd_pmf_dev *dev, bool alloc_buffer)
260 /* Get Metrics Table Address */
262 dev->buf = kzalloc(sizeof(dev->m_table), GFP_KERNEL);
267 phys_addr = virt_to_phys(dev->buf);
268 hi = phys_addr >> 32;
269 low = phys_addr & GENMASK(31, 0);
271 amd_pmf_send_cmd(dev, SET_DRAM_ADDR_HIGH, 0, hi, NULL);
272 amd_pmf_send_cmd(dev, SET_DRAM_ADDR_LOW, 0, low, NULL);
277 int amd_pmf_init_metrics_table(struct amd_pmf_dev *dev)
281 INIT_DELAYED_WORK(&dev->work_buffer, amd_pmf_get_metrics);
283 ret = amd_pmf_set_dram_addr(dev, true);
288 * Start collecting the metrics data after a small delay
289 * or else, we might end up getting stale values from PMFW.
291 schedule_delayed_work(&dev->work_buffer, msecs_to_jiffies(metrics_table_loop_ms * 3));
296 static int amd_pmf_suspend_handler(struct device *dev)
298 struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
300 if (pdev->smart_pc_enabled)
301 cancel_delayed_work_sync(&pdev->pb_work);
303 if (is_apmf_func_supported(pdev, APMF_FUNC_SBIOS_HEARTBEAT_V2))
304 amd_pmf_notify_sbios_heartbeat_event_v2(pdev, ON_SUSPEND);
309 static int amd_pmf_resume_handler(struct device *dev)
311 struct amd_pmf_dev *pdev = dev_get_drvdata(dev);
315 ret = amd_pmf_set_dram_addr(pdev, false);
320 if (is_apmf_func_supported(pdev, APMF_FUNC_SBIOS_HEARTBEAT_V2))
321 amd_pmf_notify_sbios_heartbeat_event_v2(pdev, ON_RESUME);
323 if (pdev->smart_pc_enabled)
324 schedule_delayed_work(&pdev->pb_work, msecs_to_jiffies(2000));
329 static DEFINE_SIMPLE_DEV_PM_OPS(amd_pmf_pm, amd_pmf_suspend_handler, amd_pmf_resume_handler);
331 static void amd_pmf_init_features(struct amd_pmf_dev *dev)
335 /* Enable Static Slider */
336 if (is_apmf_func_supported(dev, APMF_FUNC_STATIC_SLIDER_GRANULAR) ||
337 is_apmf_func_supported(dev, APMF_FUNC_OS_POWER_SLIDER_UPDATE)) {
338 amd_pmf_init_sps(dev);
339 dev->pwr_src_notifier.notifier_call = amd_pmf_pwr_src_notify_call;
340 power_supply_reg_notifier(&dev->pwr_src_notifier);
341 dev_dbg(dev->dev, "SPS enabled and Platform Profiles registered\n");
344 amd_pmf_init_smart_pc(dev);
345 if (dev->smart_pc_enabled) {
346 dev_dbg(dev->dev, "Smart PC Solution Enabled\n");
347 /* If Smart PC is enabled, no need to check for other features */
351 if (is_apmf_func_supported(dev, APMF_FUNC_AUTO_MODE)) {
352 amd_pmf_init_auto_mode(dev);
353 dev_dbg(dev->dev, "Auto Mode Init done\n");
354 } else if (is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_AC) ||
355 is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_DC)) {
356 ret = amd_pmf_init_cnqf(dev);
358 dev_warn(dev->dev, "CnQF Init failed\n");
362 static void amd_pmf_deinit_features(struct amd_pmf_dev *dev)
364 if (is_apmf_func_supported(dev, APMF_FUNC_STATIC_SLIDER_GRANULAR) ||
365 is_apmf_func_supported(dev, APMF_FUNC_OS_POWER_SLIDER_UPDATE)) {
366 power_supply_unreg_notifier(&dev->pwr_src_notifier);
367 amd_pmf_deinit_sps(dev);
370 if (dev->smart_pc_enabled) {
371 amd_pmf_deinit_smart_pc(dev);
372 } else if (is_apmf_func_supported(dev, APMF_FUNC_AUTO_MODE)) {
373 amd_pmf_deinit_auto_mode(dev);
374 } else if (is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_AC) ||
375 is_apmf_func_supported(dev, APMF_FUNC_DYN_SLIDER_DC)) {
376 amd_pmf_deinit_cnqf(dev);
380 static const struct acpi_device_id amd_pmf_acpi_ids[] = {
387 MODULE_DEVICE_TABLE(acpi, amd_pmf_acpi_ids);
389 static int amd_pmf_probe(struct platform_device *pdev)
391 const struct acpi_device_id *id;
392 struct amd_pmf_dev *dev;
393 struct pci_dev *rdev;
400 id = acpi_match_device(amd_pmf_acpi_ids, &pdev->dev);
404 if (id->driver_data == 0x100 && !force_load)
407 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
411 dev->dev = &pdev->dev;
413 rdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
414 if (!rdev || !pci_match_id(pmf_pci_ids, rdev)) {
419 dev->cpu_id = rdev->device;
421 err = amd_smn_read(0, AMD_PMF_BASE_ADDR_LO, &val);
423 dev_err(dev->dev, "error in reading from 0x%x\n", AMD_PMF_BASE_ADDR_LO);
425 return pcibios_err_to_errno(err);
428 base_addr_lo = val & AMD_PMF_BASE_ADDR_HI_MASK;
430 err = amd_smn_read(0, AMD_PMF_BASE_ADDR_HI, &val);
432 dev_err(dev->dev, "error in reading from 0x%x\n", AMD_PMF_BASE_ADDR_HI);
434 return pcibios_err_to_errno(err);
437 base_addr_hi = val & AMD_PMF_BASE_ADDR_LO_MASK;
439 base_addr = ((u64)base_addr_hi << 32 | base_addr_lo);
441 dev->regbase = devm_ioremap(dev->dev, base_addr + AMD_PMF_BASE_ADDR_OFFSET,
442 AMD_PMF_MAPPING_SIZE);
446 mutex_init(&dev->lock);
447 mutex_init(&dev->update_mutex);
449 amd_pmf_quirks_init(dev);
451 platform_set_drvdata(pdev, dev);
452 amd_pmf_dbgfs_register(dev);
453 amd_pmf_init_features(dev);
454 apmf_install_handler(dev);
455 if (is_apmf_func_supported(dev, APMF_FUNC_SBIOS_HEARTBEAT_V2))
456 amd_pmf_notify_sbios_heartbeat_event_v2(dev, ON_LOAD);
458 dev_info(dev->dev, "registered PMF device successfully\n");
463 static void amd_pmf_remove(struct platform_device *pdev)
465 struct amd_pmf_dev *dev = platform_get_drvdata(pdev);
467 amd_pmf_deinit_features(dev);
468 if (is_apmf_func_supported(dev, APMF_FUNC_SBIOS_HEARTBEAT_V2))
469 amd_pmf_notify_sbios_heartbeat_event_v2(dev, ON_UNLOAD);
470 apmf_acpi_deinit(dev);
471 amd_pmf_dbgfs_unregister(dev);
472 mutex_destroy(&dev->lock);
473 mutex_destroy(&dev->update_mutex);
477 static const struct attribute_group *amd_pmf_driver_groups[] = {
478 &cnqf_feature_attribute_group,
482 static struct platform_driver amd_pmf_driver = {
485 .acpi_match_table = amd_pmf_acpi_ids,
486 .dev_groups = amd_pmf_driver_groups,
487 .pm = pm_sleep_ptr(&amd_pmf_pm),
489 .probe = amd_pmf_probe,
490 .remove_new = amd_pmf_remove,
492 module_platform_driver(amd_pmf_driver);
494 MODULE_LICENSE("GPL");
495 MODULE_DESCRIPTION("AMD Platform Management Framework Driver");
496 MODULE_SOFTDEP("pre: amdtee");