|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * AMD Hardware Feedback Interface Driver |
| 4 | + * |
| 5 | + * Copyright (C) 2025 Advanced Micro Devices, Inc. All Rights Reserved. |
| 6 | + * |
| 7 | + * Authors: Perry Yuan <[email protected]> |
| 8 | + * Mario Limonciello <[email protected]> |
| 9 | + */ |
| 10 | + |
| 11 | +#define pr_fmt(fmt) "amd-hfi: " fmt |
| 12 | + |
| 13 | +#include <linux/acpi.h> |
| 14 | +#include <linux/cpu.h> |
| 15 | +#include <linux/cpumask.h> |
| 16 | +#include <linux/gfp.h> |
| 17 | +#include <linux/init.h> |
| 18 | +#include <linux/io.h> |
| 19 | +#include <linux/kernel.h> |
| 20 | +#include <linux/module.h> |
| 21 | +#include <linux/mutex.h> |
| 22 | +#include <linux/platform_device.h> |
| 23 | +#include <linux/smp.h> |
| 24 | + |
| 25 | +#define AMD_HFI_DRIVER "amd_hfi" |
| 26 | + |
| 27 | +#define AMD_HETERO_CPUID_27 0x80000027 |
| 28 | + |
| 29 | +static struct platform_device *device; |
| 30 | + |
| 31 | +struct amd_hfi_data { |
| 32 | + const char *name; |
| 33 | + struct device *dev; |
| 34 | +}; |
| 35 | + |
| 36 | +struct amd_hfi_classes { |
| 37 | + u32 perf; |
| 38 | + u32 eff; |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * struct amd_hfi_cpuinfo - HFI workload class info per CPU |
| 43 | + * @cpu: CPU index |
| 44 | + * @class_index: workload class ID index |
| 45 | + * @nr_class: max number of workload class supported |
| 46 | + * @amd_hfi_classes: current CPU workload class ranking data |
| 47 | + * |
| 48 | + * Parameters of a logical processor linked with hardware feedback class. |
| 49 | + */ |
| 50 | +struct amd_hfi_cpuinfo { |
| 51 | + int cpu; |
| 52 | + s16 class_index; |
| 53 | + u8 nr_class; |
| 54 | + struct amd_hfi_classes *amd_hfi_classes; |
| 55 | +}; |
| 56 | + |
| 57 | +static DEFINE_PER_CPU(struct amd_hfi_cpuinfo, amd_hfi_cpuinfo) = {.class_index = -1}; |
| 58 | + |
| 59 | +static int amd_hfi_alloc_class_data(struct platform_device *pdev) |
| 60 | +{ |
| 61 | + struct amd_hfi_cpuinfo *hfi_cpuinfo; |
| 62 | + struct device *dev = &pdev->dev; |
| 63 | + u32 nr_class_id; |
| 64 | + int idx; |
| 65 | + |
| 66 | + nr_class_id = cpuid_eax(AMD_HETERO_CPUID_27); |
| 67 | + if (nr_class_id > 255) { |
| 68 | + dev_err(dev, "number of supported classes too large: %d\n", |
| 69 | + nr_class_id); |
| 70 | + return -EINVAL; |
| 71 | + } |
| 72 | + |
| 73 | + for_each_possible_cpu(idx) { |
| 74 | + struct amd_hfi_classes *classes; |
| 75 | + |
| 76 | + classes = devm_kcalloc(dev, |
| 77 | + nr_class_id, |
| 78 | + sizeof(struct amd_hfi_classes), |
| 79 | + GFP_KERNEL); |
| 80 | + if (!classes) |
| 81 | + return -ENOMEM; |
| 82 | + hfi_cpuinfo = per_cpu_ptr(&amd_hfi_cpuinfo, idx); |
| 83 | + hfi_cpuinfo->amd_hfi_classes = classes; |
| 84 | + hfi_cpuinfo->nr_class = nr_class_id; |
| 85 | + } |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +static const struct acpi_device_id amd_hfi_platform_match[] = { |
| 91 | + {"AMDI0104", 0}, |
| 92 | + { } |
| 93 | +}; |
| 94 | +MODULE_DEVICE_TABLE(acpi, amd_hfi_platform_match); |
| 95 | + |
| 96 | +static int amd_hfi_probe(struct platform_device *pdev) |
| 97 | +{ |
| 98 | + struct amd_hfi_data *amd_hfi_data; |
| 99 | + int ret; |
| 100 | + |
| 101 | + if (!acpi_match_device(amd_hfi_platform_match, &pdev->dev)) |
| 102 | + return -ENODEV; |
| 103 | + |
| 104 | + amd_hfi_data = devm_kzalloc(&pdev->dev, sizeof(*amd_hfi_data), GFP_KERNEL); |
| 105 | + if (!amd_hfi_data) |
| 106 | + return -ENOMEM; |
| 107 | + |
| 108 | + amd_hfi_data->dev = &pdev->dev; |
| 109 | + platform_set_drvdata(pdev, amd_hfi_data); |
| 110 | + |
| 111 | + ret = amd_hfi_alloc_class_data(pdev); |
| 112 | + if (ret) |
| 113 | + return ret; |
| 114 | + |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +static struct platform_driver amd_hfi_driver = { |
| 119 | + .driver = { |
| 120 | + .name = AMD_HFI_DRIVER, |
| 121 | + .owner = THIS_MODULE, |
| 122 | + .acpi_match_table = ACPI_PTR(amd_hfi_platform_match), |
| 123 | + }, |
| 124 | + .probe = amd_hfi_probe, |
| 125 | +}; |
| 126 | + |
| 127 | +static int __init amd_hfi_init(void) |
| 128 | +{ |
| 129 | + int ret; |
| 130 | + |
| 131 | + if (acpi_disabled || |
| 132 | + !cpu_feature_enabled(X86_FEATURE_AMD_HTR_CORES) || |
| 133 | + !cpu_feature_enabled(X86_FEATURE_AMD_WORKLOAD_CLASS)) |
| 134 | + return -ENODEV; |
| 135 | + |
| 136 | + device = platform_device_register_simple(AMD_HFI_DRIVER, -1, NULL, 0); |
| 137 | + if (IS_ERR(device)) { |
| 138 | + pr_err("unable to register HFI platform device\n"); |
| 139 | + return PTR_ERR(device); |
| 140 | + } |
| 141 | + |
| 142 | + ret = platform_driver_register(&amd_hfi_driver); |
| 143 | + if (ret) |
| 144 | + pr_err("failed to register HFI driver\n"); |
| 145 | + |
| 146 | + return ret; |
| 147 | +} |
| 148 | + |
| 149 | +static __exit void amd_hfi_exit(void) |
| 150 | +{ |
| 151 | + platform_driver_unregister(&amd_hfi_driver); |
| 152 | + platform_device_unregister(device); |
| 153 | +} |
| 154 | +module_init(amd_hfi_init); |
| 155 | +module_exit(amd_hfi_exit); |
| 156 | + |
| 157 | +MODULE_LICENSE("GPL"); |
| 158 | +MODULE_DESCRIPTION("AMD Hardware Feedback Interface Driver"); |
0 commit comments