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