Tim Wawrzynczak | 103bd5e | 2020-05-29 13:11:00 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <acpi/acpigen.h> |
Tim Wawrzynczak | d40a4c2 | 2021-02-25 13:14:49 -0700 | [diff] [blame] | 4 | #include <acpi/acpigen_pci.h> |
Tim Wawrzynczak | 103bd5e | 2020-05-29 13:11:00 -0600 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <device/device.h> |
| 7 | #include "chip.h" |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 8 | #include "dptf.h" |
Tim Wawrzynczak | 103bd5e | 2020-05-29 13:11:00 -0600 | [diff] [blame] | 9 | |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 10 | /* Generic DPTF participants have a PTYP field to distinguish them */ |
| 11 | enum dptf_generic_participant_type { |
| 12 | DPTF_GENERIC_PARTICIPANT_TYPE_TSR = 0x3, |
| 13 | DPTF_GENERIC_PARTICIPANT_TYPE_CHARGER = 0xB, |
| 14 | }; |
| 15 | |
| 16 | #define DEFAULT_CHARGER_STR "Battery Charger" |
| 17 | |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 18 | /* |
| 19 | * Helper method to determine if a device is "used" (called out anywhere as a source or a target |
| 20 | * of any policies, and therefore should be included in the ACPI tables. |
| 21 | */ |
| 22 | static bool is_participant_used(const struct drivers_intel_dptf_config *config, |
| 23 | enum dptf_participant participant) |
| 24 | { |
Tim Wawrzynczak | c41f7f1 | 2020-05-29 13:56:37 -0600 | [diff] [blame] | 25 | int i; |
| 26 | |
| 27 | /* Active? */ |
| 28 | for (i = 0; i < DPTF_MAX_ACTIVE_POLICIES; ++i) |
| 29 | if (config->policies.active[i].target == participant) |
| 30 | return true; |
| 31 | |
Tim Wawrzynczak | 7eb1136 | 2020-05-29 14:10:53 -0600 | [diff] [blame] | 32 | /* Passive? */ |
| 33 | for (i = 0; i < DPTF_MAX_PASSIVE_POLICIES; ++i) |
| 34 | if (config->policies.passive[i].source == participant || |
| 35 | config->policies.passive[i].target == participant) |
| 36 | return true; |
| 37 | |
Tim Wawrzynczak | 3a9cde9 | 2020-05-29 14:19:15 -0600 | [diff] [blame] | 38 | /* Critical? */ |
| 39 | for (i = 0; i < DPTF_MAX_CRITICAL_POLICIES; ++i) |
| 40 | if (config->policies.critical[i].source == participant) |
| 41 | return true; |
| 42 | |
Tim Wawrzynczak | c41f7f1 | 2020-05-29 13:56:37 -0600 | [diff] [blame] | 43 | /* Check fan as well (its use is implicit in the Active policy) */ |
| 44 | if (participant == DPTF_FAN && config->policies.active[0].target != DPTF_NONE) |
| 45 | return true; |
| 46 | |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | |
Tim Wawrzynczak | 103bd5e | 2020-05-29 13:11:00 -0600 | [diff] [blame] | 50 | static const char *dptf_acpi_name(const struct device *dev) |
| 51 | { |
| 52 | return "DPTF"; |
| 53 | } |
| 54 | |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 55 | static int get_STA_value(const struct drivers_intel_dptf_config *config, |
| 56 | enum dptf_participant participant) |
| 57 | { |
| 58 | return is_participant_used(config, participant) ? |
| 59 | ACPI_STATUS_DEVICE_ALL_ON : |
| 60 | ACPI_STATUS_DEVICE_ALL_OFF; |
| 61 | } |
| 62 | |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 63 | static void dptf_write_hid(bool is_eisa, const char *hid) |
| 64 | { |
| 65 | if (is_eisa) |
| 66 | acpigen_emit_eisaid(hid); |
| 67 | else |
| 68 | acpigen_write_string(hid); |
| 69 | } |
| 70 | |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 71 | /* Devices with GENERIC _HID (distinguished by PTYP) */ |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 72 | static void dptf_write_generic_participant(const char *name, |
| 73 | enum dptf_generic_participant_type ptype, |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 74 | const char *str, int sta_val, |
| 75 | const struct dptf_platform_info *platform_info) |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 76 | { |
| 77 | /* Auto-incrementing UID for generic participants */ |
| 78 | static int generic_uid = 0; |
| 79 | |
| 80 | acpigen_write_device(name); |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 81 | acpigen_write_name("_HID"); |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 82 | dptf_write_hid(platform_info->use_eisa_hids, platform_info->generic_hid); |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 83 | |
| 84 | acpigen_write_name_integer("_UID", generic_uid++); |
| 85 | acpigen_write_STA(sta_val); |
| 86 | |
| 87 | if (str) |
| 88 | acpigen_write_name_string("_STR", str); |
| 89 | |
| 90 | acpigen_write_name_integer("PTYP", ptype); |
| 91 | |
| 92 | acpigen_pop_len(); /* Device */ |
| 93 | } |
| 94 | |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 95 | /* \_SB.PCI0.TCPU */ |
| 96 | static void write_tcpu(const struct device *pci_dev, |
| 97 | const struct drivers_intel_dptf_config *config) |
Tim Wawrzynczak | 103bd5e | 2020-05-29 13:11:00 -0600 | [diff] [blame] | 98 | { |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 99 | /* DPTF CPU device - \_SB.PCI0.TCPU */ |
| 100 | acpigen_write_scope(TCPU_SCOPE); |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 101 | acpigen_write_device("TCPU"); |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 102 | acpigen_write_ADR_pci_device(pci_dev); |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 103 | acpigen_write_STA(get_STA_value(config, DPTF_CPU)); |
| 104 | acpigen_pop_len(); /* Device */ |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 105 | acpigen_pop_len(); /* TCPU Scope */ |
| 106 | } |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 107 | |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 108 | /* \_SB.DPTF.TFN1 */ |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 109 | static void write_fan(const struct drivers_intel_dptf_config *config, |
| 110 | const struct dptf_platform_info *platform_info) |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 111 | { |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 112 | acpigen_write_device("TFN1"); |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 113 | acpigen_write_name("_HID"); |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 114 | dptf_write_hid(platform_info->use_eisa_hids, platform_info->fan_hid); |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 115 | acpigen_write_name_integer("_UID", 0); |
| 116 | acpigen_write_STA(get_STA_value(config, DPTF_FAN)); |
| 117 | acpigen_pop_len(); /* Device */ |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* \_SB.DPTF.xxxx */ |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 121 | static void write_generic_devices(const struct drivers_intel_dptf_config *config, |
| 122 | const struct dptf_platform_info *platform_info) |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 123 | { |
| 124 | enum dptf_participant participant; |
| 125 | char name[ACPI_NAME_BUFFER_SIZE]; |
| 126 | int i; |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 127 | |
| 128 | dptf_write_generic_participant("TCHG", DPTF_GENERIC_PARTICIPANT_TYPE_CHARGER, |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 129 | DEFAULT_CHARGER_STR, |
| 130 | get_STA_value(config, DPTF_CHARGER), |
| 131 | platform_info); |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 132 | |
| 133 | for (i = 0, participant = DPTF_TEMP_SENSOR_0; i < 4; ++i, ++participant) { |
| 134 | snprintf(name, sizeof(name), "TSR%1d", i); |
| 135 | dptf_write_generic_participant(name, DPTF_GENERIC_PARTICIPANT_TYPE_TSR, |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 136 | NULL, get_STA_value(config, participant), |
| 137 | platform_info); |
Tim Wawrzynczak | ff2f6b2 | 2020-05-29 13:24:03 -0600 | [diff] [blame] | 138 | } |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 139 | } |
Tim Wawrzynczak | 103bd5e | 2020-05-29 13:11:00 -0600 | [diff] [blame] | 140 | |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 141 | /* \_SB.DPTF - note: leaves the Scope open for child devices*/ |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 142 | static void write_open_dptf_device(const struct device *dev, |
| 143 | const struct dptf_platform_info *platform_info) |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 144 | { |
| 145 | acpigen_write_scope("\\_SB"); |
| 146 | acpigen_write_device(acpi_device_name(dev)); |
| 147 | acpigen_write_name("_HID"); |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 148 | dptf_write_hid(platform_info->use_eisa_hids, platform_info->dptf_device_hid); |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 149 | acpigen_write_name_integer("_UID", 0); |
| 150 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 151 | } |
| 152 | |
| 153 | /* Add minimal definitions of DPTF devices into the SSDT */ |
| 154 | static void write_device_definitions(const struct device *dev) |
| 155 | { |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 156 | const struct dptf_platform_info *platform_info = get_dptf_platform_info(); |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 157 | const struct drivers_intel_dptf_config *config; |
| 158 | struct device *parent; |
| 159 | |
| 160 | /* The CPU device gets an _ADR that matches the ACPI PCI address for 00:04.00 */ |
| 161 | parent = dev && dev->bus ? dev->bus->dev : NULL; |
| 162 | if (!parent || parent->path.type != DEVICE_PATH_PCI) { |
| 163 | printk(BIOS_ERR, "%s: DPTF objects must live under 00:04.0 PCI device\n", |
| 164 | __func__); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | config = config_of(dev); |
| 169 | write_tcpu(parent, config); |
Tim Wawrzynczak | 7f7c388 | 2021-04-09 12:15:21 -0600 | [diff] [blame] | 170 | write_open_dptf_device(dev, platform_info); |
| 171 | write_fan(config, platform_info); |
| 172 | write_generic_devices(config, platform_info); |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 173 | |
| 174 | acpigen_pop_len(); /* DPTF Device (write_open_dptf_device) */ |
Tim Wawrzynczak | 103bd5e | 2020-05-29 13:11:00 -0600 | [diff] [blame] | 175 | acpigen_pop_len(); /* Scope */ |
| 176 | } |
| 177 | |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 178 | /* Emites policy definitions for each policy type */ |
| 179 | static void write_policies(const struct drivers_intel_dptf_config *config) |
| 180 | { |
| 181 | dptf_write_enabled_policies(config->policies.active, DPTF_MAX_ACTIVE_POLICIES, |
| 182 | config->policies.passive, DPTF_MAX_PASSIVE_POLICIES, |
| 183 | config->policies.critical, DPTF_MAX_CRITICAL_POLICIES); |
| 184 | |
| 185 | dptf_write_active_policies(config->policies.active, |
| 186 | DPTF_MAX_ACTIVE_POLICIES); |
| 187 | |
| 188 | dptf_write_passive_policies(config->policies.passive, |
| 189 | DPTF_MAX_PASSIVE_POLICIES); |
| 190 | |
| 191 | dptf_write_critical_policies(config->policies.critical, |
| 192 | DPTF_MAX_CRITICAL_POLICIES); |
| 193 | } |
| 194 | |
| 195 | /* Writes other static tables that are used by DPTF */ |
| 196 | static void write_controls(const struct drivers_intel_dptf_config *config) |
| 197 | { |
| 198 | dptf_write_charger_perf(config->controls.charger_perf, DPTF_MAX_CHARGER_PERF_STATES); |
| 199 | dptf_write_fan_perf(config->controls.fan_perf, DPTF_MAX_FAN_PERF_STATES); |
| 200 | dptf_write_power_limits(&config->controls.power_limits); |
| 201 | } |
| 202 | |
| 203 | /* Options to control the behavior of devices */ |
| 204 | static void write_options(const struct drivers_intel_dptf_config *config) |
| 205 | { |
| 206 | enum dptf_participant p; |
| 207 | int i; |
| 208 | |
| 209 | /* Fan options */ |
Tim Wawrzynczak | 3a658ad | 2020-07-24 18:58:24 -0600 | [diff] [blame] | 210 | dptf_write_scope(DPTF_FAN); |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 211 | dptf_write_fan_options(config->options.fan.fine_grained_control, |
| 212 | config->options.fan.step_size, |
| 213 | config->options.fan.low_speed_notify); |
Tim Wawrzynczak | 3a658ad | 2020-07-24 18:58:24 -0600 | [diff] [blame] | 214 | acpigen_pop_len(); /* Scope */ |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 215 | |
| 216 | /* TSR options */ |
| 217 | for (p = DPTF_TEMP_SENSOR_0, i = 0; p <= DPTF_TEMP_SENSOR_3; ++p, ++i) { |
Tim Wawrzynczak | c6a593b | 2020-07-14 13:19:03 -0600 | [diff] [blame] | 218 | if (is_participant_used(config, p) && (config->options.tsr[i].hysteresis || |
| 219 | config->options.tsr[i].desc)) { |
| 220 | dptf_write_scope(p); |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 221 | dptf_write_tsr_hysteresis(config->options.tsr[i].hysteresis); |
| 222 | dptf_write_STR(config->options.tsr[i].desc); |
Tim Wawrzynczak | c6a593b | 2020-07-14 13:19:03 -0600 | [diff] [blame] | 223 | acpigen_pop_len(); /* Scope */ |
Tim Wawrzynczak | 5212ece6 | 2020-07-16 11:54:04 -0600 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* Add custom tables and methods to SSDT */ |
| 229 | static void dptf_fill_ssdt(const struct device *dev) |
| 230 | { |
| 231 | struct drivers_intel_dptf_config *config = config_of(dev); |
| 232 | |
| 233 | write_device_definitions(dev); |
| 234 | write_policies(config); |
| 235 | write_controls(config); |
| 236 | write_options(config); |
| 237 | |
| 238 | printk(BIOS_INFO, DPTF_DEVICE_PATH ": %s at %s\n", dev->chip_ops->name, dev_path(dev)); |
| 239 | } |
| 240 | |
Tim Wawrzynczak | 103bd5e | 2020-05-29 13:11:00 -0600 | [diff] [blame] | 241 | static struct device_operations dptf_ops = { |
| 242 | .read_resources = noop_read_resources, |
| 243 | .set_resources = noop_set_resources, |
| 244 | .acpi_name = dptf_acpi_name, |
| 245 | .acpi_fill_ssdt = dptf_fill_ssdt, |
Tim Wawrzynczak | 103bd5e | 2020-05-29 13:11:00 -0600 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | static void dptf_enable_dev(struct device *dev) |
| 249 | { |
| 250 | dev->ops = &dptf_ops; |
| 251 | } |
| 252 | |
| 253 | struct chip_operations drivers_intel_dptf_ops = { |
| 254 | CHIP_NAME("Intel DPTF") |
| 255 | .enable_dev = dptf_enable_dev, |
| 256 | }; |