1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * AMD Platform Management Framework Driver 4 * 5 * Copyright (c) 2022, Advanced Micro Devices, Inc. 6 * All Rights Reserved. 7 * 8 * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com> 9 */ 10 11 #include <linux/acpi.h> 12 #include "pmf.h" 13 14 #define APMF_CQL_NOTIFICATION 2 15 #define APMF_AMT_NOTIFICATION 3 16 17 static union acpi_object *apmf_if_call(struct amd_pmf_dev *pdev, int fn, struct acpi_buffer *param) 18 { 19 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 20 acpi_handle ahandle = ACPI_HANDLE(pdev->dev); 21 struct acpi_object_list apmf_if_arg_list; 22 union acpi_object apmf_if_args[2]; 23 acpi_status status; 24 25 apmf_if_arg_list.count = 2; 26 apmf_if_arg_list.pointer = &apmf_if_args[0]; 27 28 apmf_if_args[0].type = ACPI_TYPE_INTEGER; 29 apmf_if_args[0].integer.value = fn; 30 31 if (param) { 32 apmf_if_args[1].type = ACPI_TYPE_BUFFER; 33 apmf_if_args[1].buffer.length = param->length; 34 apmf_if_args[1].buffer.pointer = param->pointer; 35 } else { 36 apmf_if_args[1].type = ACPI_TYPE_INTEGER; 37 apmf_if_args[1].integer.value = 0; 38 } 39 40 status = acpi_evaluate_object(ahandle, "APMF", &apmf_if_arg_list, &buffer); 41 if (ACPI_FAILURE(status)) { 42 dev_err(pdev->dev, "APMF method:%d call failed\n", fn); 43 kfree(buffer.pointer); 44 return NULL; 45 } 46 47 return buffer.pointer; 48 } 49 50 static int apmf_if_call_store_buffer(struct amd_pmf_dev *pdev, int fn, void *dest, size_t out_sz) 51 { 52 union acpi_object *info; 53 size_t size; 54 int err = 0; 55 56 info = apmf_if_call(pdev, fn, NULL); 57 if (!info) 58 return -EIO; 59 60 if (info->type != ACPI_TYPE_BUFFER) { 61 dev_err(pdev->dev, "object is not a buffer\n"); 62 err = -EINVAL; 63 goto out; 64 } 65 66 if (info->buffer.length < 2) { 67 dev_err(pdev->dev, "buffer too small\n"); 68 err = -EINVAL; 69 goto out; 70 } 71 72 size = *(u16 *)info->buffer.pointer; 73 if (info->buffer.length < size) { 74 dev_err(pdev->dev, "buffer smaller then headersize %u < %zu\n", 75 info->buffer.length, size); 76 err = -EINVAL; 77 goto out; 78 } 79 80 if (size < out_sz) { 81 dev_err(pdev->dev, "buffer too small %zu\n", size); 82 err = -EINVAL; 83 goto out; 84 } 85 86 memcpy(dest, info->buffer.pointer, out_sz); 87 88 out: 89 kfree(info); 90 return err; 91 } 92 93 int is_apmf_func_supported(struct amd_pmf_dev *pdev, unsigned long index) 94 { 95 /* If bit-n is set, that indicates function n+1 is supported */ 96 return !!(pdev->supported_func & BIT(index - 1)); 97 } 98 99 int apmf_get_static_slider_granular(struct amd_pmf_dev *pdev, 100 struct apmf_static_slider_granular_output *data) 101 { 102 if (!is_apmf_func_supported(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR)) 103 return -EINVAL; 104 105 return apmf_if_call_store_buffer(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR, 106 data, sizeof(*data)); 107 } 108 109 int apmf_os_power_slider_update(struct amd_pmf_dev *pdev, u8 event) 110 { 111 struct os_power_slider args; 112 struct acpi_buffer params; 113 union acpi_object *info; 114 115 args.size = sizeof(args); 116 args.slider_event = event; 117 118 params.length = sizeof(args); 119 params.pointer = (void *)&args; 120 121 info = apmf_if_call(pdev, APMF_FUNC_OS_POWER_SLIDER_UPDATE, ¶ms); 122 if (!info) 123 return -EIO; 124 125 kfree(info); 126 return 0; 127 } 128 129 static void apmf_sbios_heartbeat_notify(struct work_struct *work) 130 { 131 struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, heart_beat.work); 132 union acpi_object *info; 133 134 dev_dbg(dev->dev, "Sending heartbeat to SBIOS\n"); 135 info = apmf_if_call(dev, APMF_FUNC_SBIOS_HEARTBEAT, NULL); 136 if (!info) 137 return; 138 139 schedule_delayed_work(&dev->heart_beat, msecs_to_jiffies(dev->hb_interval * 1000)); 140 kfree(info); 141 } 142 143 int apmf_update_fan_idx(struct amd_pmf_dev *pdev, bool manual, u32 idx) 144 { 145 union acpi_object *info; 146 struct apmf_fan_idx args; 147 struct acpi_buffer params; 148 149 args.size = sizeof(args); 150 args.fan_ctl_mode = manual; 151 args.fan_ctl_idx = idx; 152 153 params.length = sizeof(args); 154 params.pointer = (void *)&args; 155 156 info = apmf_if_call(pdev, APMF_FUNC_SET_FAN_IDX, ¶ms); 157 if (!info) 158 return -EIO; 159 160 kfree(info); 161 return 0; 162 } 163 164 int apmf_get_auto_mode_def(struct amd_pmf_dev *pdev, struct apmf_auto_mode *data) 165 { 166 return apmf_if_call_store_buffer(pdev, APMF_FUNC_AUTO_MODE, data, sizeof(*data)); 167 } 168 169 int apmf_get_sbios_requests(struct amd_pmf_dev *pdev, struct apmf_sbios_req *req) 170 { 171 return apmf_if_call_store_buffer(pdev, APMF_FUNC_SBIOS_REQUESTS, 172 req, sizeof(*req)); 173 } 174 175 static void apmf_event_handler(acpi_handle handle, u32 event, void *data) 176 { 177 struct amd_pmf_dev *pmf_dev = data; 178 struct apmf_sbios_req req; 179 int ret; 180 181 mutex_lock(&pmf_dev->update_mutex); 182 ret = apmf_get_sbios_requests(pmf_dev, &req); 183 if (ret) { 184 dev_err(pmf_dev->dev, "Failed to get SBIOS requests:%d\n", ret); 185 goto out; 186 } 187 188 if (req.pending_req & BIT(APMF_AMT_NOTIFICATION)) { 189 dev_dbg(pmf_dev->dev, "AMT is supported and notifications %s\n", 190 req.amt_event ? "Enabled" : "Disabled"); 191 pmf_dev->amt_enabled = !!req.amt_event; 192 193 if (pmf_dev->amt_enabled) 194 amd_pmf_handle_amt(pmf_dev); 195 else 196 amd_pmf_reset_amt(pmf_dev); 197 } 198 199 if (req.pending_req & BIT(APMF_CQL_NOTIFICATION)) { 200 dev_dbg(pmf_dev->dev, "CQL is supported and notifications %s\n", 201 req.cql_event ? "Enabled" : "Disabled"); 202 203 /* update the target mode information */ 204 if (pmf_dev->amt_enabled) 205 amd_pmf_update_2_cql(pmf_dev, req.cql_event); 206 } 207 out: 208 mutex_unlock(&pmf_dev->update_mutex); 209 } 210 211 static int apmf_if_verify_interface(struct amd_pmf_dev *pdev) 212 { 213 struct apmf_verify_interface output; 214 int err; 215 216 err = apmf_if_call_store_buffer(pdev, APMF_FUNC_VERIFY_INTERFACE, &output, sizeof(output)); 217 if (err) 218 return err; 219 220 pdev->supported_func = output.supported_functions; 221 dev_dbg(pdev->dev, "supported functions:0x%x notifications:0x%x\n", 222 output.supported_functions, output.notification_mask); 223 224 return 0; 225 } 226 227 static int apmf_get_system_params(struct amd_pmf_dev *dev) 228 { 229 struct apmf_system_params params; 230 int err; 231 232 if (!is_apmf_func_supported(dev, APMF_FUNC_GET_SYS_PARAMS)) 233 return -EINVAL; 234 235 err = apmf_if_call_store_buffer(dev, APMF_FUNC_GET_SYS_PARAMS, ¶ms, sizeof(params)); 236 if (err) 237 return err; 238 239 dev_dbg(dev->dev, "system params mask:0x%x flags:0x%x cmd_code:0x%x heartbeat:%d\n", 240 params.valid_mask, 241 params.flags, 242 params.command_code, 243 params.heartbeat_int); 244 params.flags = params.flags & params.valid_mask; 245 dev->hb_interval = params.heartbeat_int; 246 247 return 0; 248 } 249 250 int apmf_get_dyn_slider_def_ac(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data) 251 { 252 return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_AC, data, sizeof(*data)); 253 } 254 255 int apmf_get_dyn_slider_def_dc(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data) 256 { 257 return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_DC, data, sizeof(*data)); 258 } 259 260 int apmf_install_handler(struct amd_pmf_dev *pmf_dev) 261 { 262 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev); 263 acpi_status status; 264 265 /* Install the APMF Notify handler */ 266 if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) && 267 is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS)) { 268 status = acpi_install_notify_handler(ahandle, ACPI_ALL_NOTIFY, 269 apmf_event_handler, pmf_dev); 270 if (ACPI_FAILURE(status)) { 271 dev_err(pmf_dev->dev, "failed to install notify handler\n"); 272 return -ENODEV; 273 } 274 275 /* Call the handler once manually to catch up with possibly missed notifies. */ 276 apmf_event_handler(ahandle, 0, pmf_dev); 277 } 278 279 return 0; 280 } 281 282 static acpi_status apmf_walk_resources(struct acpi_resource *res, void *data) 283 { 284 struct amd_pmf_dev *dev = data; 285 286 switch (res->type) { 287 case ACPI_RESOURCE_TYPE_ADDRESS64: 288 dev->policy_addr = res->data.address64.address.minimum; 289 dev->policy_sz = res->data.address64.address.address_length; 290 break; 291 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: 292 dev->policy_addr = res->data.fixed_memory32.address; 293 dev->policy_sz = res->data.fixed_memory32.address_length; 294 break; 295 } 296 297 if (!dev->policy_addr || dev->policy_sz > POLICY_BUF_MAX_SZ || dev->policy_sz == 0) { 298 pr_err("Incorrect Policy params, possibly a SBIOS bug\n"); 299 return AE_ERROR; 300 } 301 302 return AE_OK; 303 } 304 305 int apmf_check_smart_pc(struct amd_pmf_dev *pmf_dev) 306 { 307 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev); 308 acpi_status status; 309 310 status = acpi_walk_resources(ahandle, METHOD_NAME__CRS, apmf_walk_resources, pmf_dev); 311 if (ACPI_FAILURE(status)) { 312 dev_err(pmf_dev->dev, "acpi_walk_resources failed :%d\n", status); 313 return -EINVAL; 314 } 315 316 return 0; 317 } 318 319 void apmf_acpi_deinit(struct amd_pmf_dev *pmf_dev) 320 { 321 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev); 322 323 if (pmf_dev->hb_interval) 324 cancel_delayed_work_sync(&pmf_dev->heart_beat); 325 326 if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) && 327 is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS)) 328 acpi_remove_notify_handler(ahandle, ACPI_ALL_NOTIFY, apmf_event_handler); 329 } 330 331 int apmf_acpi_init(struct amd_pmf_dev *pmf_dev) 332 { 333 int ret; 334 335 ret = apmf_if_verify_interface(pmf_dev); 336 if (ret) { 337 dev_err(pmf_dev->dev, "APMF verify interface failed :%d\n", ret); 338 goto out; 339 } 340 341 ret = apmf_get_system_params(pmf_dev); 342 if (ret) { 343 dev_dbg(pmf_dev->dev, "APMF apmf_get_system_params failed :%d\n", ret); 344 goto out; 345 } 346 347 if (pmf_dev->hb_interval) { 348 /* send heartbeats only if the interval is not zero */ 349 INIT_DELAYED_WORK(&pmf_dev->heart_beat, apmf_sbios_heartbeat_notify); 350 schedule_delayed_work(&pmf_dev->heart_beat, 0); 351 } 352 353 out: 354 return ret; 355 } 356