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