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 static union acpi_object *apts_if_call(struct amd_pmf_dev *pdev, u32 state_index) 94 { 95 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 96 acpi_handle ahandle = ACPI_HANDLE(pdev->dev); 97 struct acpi_object_list apts_if_arg_list; 98 union acpi_object apts_if_args[3]; 99 acpi_status status; 100 101 apts_if_arg_list.count = 3; 102 apts_if_arg_list.pointer = &apts_if_args[0]; 103 104 apts_if_args[0].type = ACPI_TYPE_INTEGER; 105 apts_if_args[0].integer.value = 1; 106 apts_if_args[1].type = ACPI_TYPE_INTEGER; 107 apts_if_args[1].integer.value = state_index; 108 apts_if_args[2].type = ACPI_TYPE_INTEGER; 109 apts_if_args[2].integer.value = 0; 110 111 status = acpi_evaluate_object(ahandle, "APTS", &apts_if_arg_list, &buffer); 112 if (ACPI_FAILURE(status)) { 113 dev_err(pdev->dev, "APTS state_idx:%u call failed\n", state_index); 114 kfree(buffer.pointer); 115 return NULL; 116 } 117 118 return buffer.pointer; 119 } 120 121 static int apts_if_call_store_buffer(struct amd_pmf_dev *pdev, 122 u32 index, void *data, size_t out_sz) 123 { 124 union acpi_object *info; 125 size_t size; 126 int err = 0; 127 128 info = apts_if_call(pdev, index); 129 if (!info) 130 return -EIO; 131 132 if (info->type != ACPI_TYPE_BUFFER) { 133 dev_err(pdev->dev, "object is not a buffer\n"); 134 err = -EINVAL; 135 goto out; 136 } 137 138 size = *(u16 *)info->buffer.pointer; 139 if (info->buffer.length < size) { 140 dev_err(pdev->dev, "buffer smaller than header size %u < %zu\n", 141 info->buffer.length, size); 142 err = -EINVAL; 143 goto out; 144 } 145 146 if (size < out_sz) { 147 dev_err(pdev->dev, "buffer too small %zu\n", size); 148 err = -EINVAL; 149 goto out; 150 } 151 152 memcpy(data, info->buffer.pointer, out_sz); 153 out: 154 kfree(info); 155 return err; 156 } 157 158 int is_apmf_func_supported(struct amd_pmf_dev *pdev, unsigned long index) 159 { 160 /* If bit-n is set, that indicates function n+1 is supported */ 161 return !!(pdev->supported_func & BIT(index - 1)); 162 } 163 164 int apts_get_static_slider_granular_v2(struct amd_pmf_dev *pdev, 165 struct amd_pmf_apts_granular_output *data, u32 apts_idx) 166 { 167 if (!is_apmf_func_supported(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR)) 168 return -EINVAL; 169 170 return apts_if_call_store_buffer(pdev, apts_idx, data, sizeof(*data)); 171 } 172 173 int apmf_get_static_slider_granular_v2(struct amd_pmf_dev *pdev, 174 struct apmf_static_slider_granular_output_v2 *data) 175 { 176 if (!is_apmf_func_supported(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR)) 177 return -EINVAL; 178 179 return apmf_if_call_store_buffer(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR, 180 data, sizeof(*data)); 181 } 182 183 int apmf_get_static_slider_granular(struct amd_pmf_dev *pdev, 184 struct apmf_static_slider_granular_output *data) 185 { 186 if (!is_apmf_func_supported(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR)) 187 return -EINVAL; 188 189 return apmf_if_call_store_buffer(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR, 190 data, sizeof(*data)); 191 } 192 193 int apmf_os_power_slider_update(struct amd_pmf_dev *pdev, u8 event) 194 { 195 struct os_power_slider args; 196 struct acpi_buffer params; 197 union acpi_object *info; 198 199 args.size = sizeof(args); 200 args.slider_event = event; 201 202 params.length = sizeof(args); 203 params.pointer = (void *)&args; 204 205 info = apmf_if_call(pdev, APMF_FUNC_OS_POWER_SLIDER_UPDATE, ¶ms); 206 if (!info) 207 return -EIO; 208 209 kfree(info); 210 return 0; 211 } 212 213 static void apmf_sbios_heartbeat_notify(struct work_struct *work) 214 { 215 struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, heart_beat.work); 216 union acpi_object *info; 217 218 dev_dbg(dev->dev, "Sending heartbeat to SBIOS\n"); 219 info = apmf_if_call(dev, APMF_FUNC_SBIOS_HEARTBEAT, NULL); 220 if (!info) 221 return; 222 223 schedule_delayed_work(&dev->heart_beat, msecs_to_jiffies(dev->hb_interval * 1000)); 224 kfree(info); 225 } 226 227 int amd_pmf_notify_sbios_heartbeat_event_v2(struct amd_pmf_dev *dev, u8 flag) 228 { 229 struct sbios_hb_event_v2 args = { }; 230 struct acpi_buffer params; 231 union acpi_object *info; 232 233 args.size = sizeof(args); 234 235 switch (flag) { 236 case ON_LOAD: 237 args.load = 1; 238 break; 239 case ON_UNLOAD: 240 args.unload = 1; 241 break; 242 case ON_SUSPEND: 243 args.suspend = 1; 244 break; 245 case ON_RESUME: 246 args.resume = 1; 247 break; 248 default: 249 dev_dbg(dev->dev, "Failed to send v2 heartbeat event, flag:0x%x\n", flag); 250 return -EINVAL; 251 } 252 253 params.length = sizeof(args); 254 params.pointer = &args; 255 256 info = apmf_if_call(dev, APMF_FUNC_SBIOS_HEARTBEAT_V2, ¶ms); 257 if (!info) 258 return -EIO; 259 260 kfree(info); 261 return 0; 262 } 263 264 int apmf_update_fan_idx(struct amd_pmf_dev *pdev, bool manual, u32 idx) 265 { 266 union acpi_object *info; 267 struct apmf_fan_idx args; 268 struct acpi_buffer params; 269 270 args.size = sizeof(args); 271 args.fan_ctl_mode = manual; 272 args.fan_ctl_idx = idx; 273 274 params.length = sizeof(args); 275 params.pointer = (void *)&args; 276 277 info = apmf_if_call(pdev, APMF_FUNC_SET_FAN_IDX, ¶ms); 278 if (!info) 279 return -EIO; 280 281 kfree(info); 282 return 0; 283 } 284 285 int apmf_get_auto_mode_def(struct amd_pmf_dev *pdev, struct apmf_auto_mode *data) 286 { 287 return apmf_if_call_store_buffer(pdev, APMF_FUNC_AUTO_MODE, data, sizeof(*data)); 288 } 289 290 int apmf_get_sbios_requests_v2(struct amd_pmf_dev *pdev, struct apmf_sbios_req_v2 *req) 291 { 292 return apmf_if_call_store_buffer(pdev, APMF_FUNC_SBIOS_REQUESTS, req, sizeof(*req)); 293 } 294 295 int apmf_get_sbios_requests(struct amd_pmf_dev *pdev, struct apmf_sbios_req *req) 296 { 297 return apmf_if_call_store_buffer(pdev, APMF_FUNC_SBIOS_REQUESTS, 298 req, sizeof(*req)); 299 } 300 301 static void apmf_event_handler(acpi_handle handle, u32 event, void *data) 302 { 303 struct amd_pmf_dev *pmf_dev = data; 304 struct apmf_sbios_req req; 305 int ret; 306 307 mutex_lock(&pmf_dev->update_mutex); 308 ret = apmf_get_sbios_requests(pmf_dev, &req); 309 if (ret) { 310 dev_err(pmf_dev->dev, "Failed to get SBIOS requests:%d\n", ret); 311 goto out; 312 } 313 314 if (req.pending_req & BIT(APMF_AMT_NOTIFICATION)) { 315 dev_dbg(pmf_dev->dev, "AMT is supported and notifications %s\n", 316 req.amt_event ? "Enabled" : "Disabled"); 317 pmf_dev->amt_enabled = !!req.amt_event; 318 319 if (pmf_dev->amt_enabled) 320 amd_pmf_handle_amt(pmf_dev); 321 else 322 amd_pmf_reset_amt(pmf_dev); 323 } 324 325 if (req.pending_req & BIT(APMF_CQL_NOTIFICATION)) { 326 dev_dbg(pmf_dev->dev, "CQL is supported and notifications %s\n", 327 req.cql_event ? "Enabled" : "Disabled"); 328 329 /* update the target mode information */ 330 if (pmf_dev->amt_enabled) 331 amd_pmf_update_2_cql(pmf_dev, req.cql_event); 332 } 333 out: 334 mutex_unlock(&pmf_dev->update_mutex); 335 } 336 337 static int apmf_if_verify_interface(struct amd_pmf_dev *pdev) 338 { 339 struct apmf_verify_interface output; 340 int err; 341 342 err = apmf_if_call_store_buffer(pdev, APMF_FUNC_VERIFY_INTERFACE, &output, sizeof(output)); 343 if (err) 344 return err; 345 346 pdev->supported_func = output.supported_functions; 347 dev_dbg(pdev->dev, "supported functions:0x%x notifications:0x%x version:%u\n", 348 output.supported_functions, output.notification_mask, output.version); 349 350 pdev->pmf_if_version = output.version; 351 352 return 0; 353 } 354 355 static int apmf_get_system_params(struct amd_pmf_dev *dev) 356 { 357 struct apmf_system_params params; 358 int err; 359 360 if (!is_apmf_func_supported(dev, APMF_FUNC_GET_SYS_PARAMS)) 361 return -EINVAL; 362 363 err = apmf_if_call_store_buffer(dev, APMF_FUNC_GET_SYS_PARAMS, ¶ms, sizeof(params)); 364 if (err) 365 return err; 366 367 dev_dbg(dev->dev, "system params mask:0x%x flags:0x%x cmd_code:0x%x heartbeat:%d\n", 368 params.valid_mask, 369 params.flags, 370 params.command_code, 371 params.heartbeat_int); 372 params.flags = params.flags & params.valid_mask; 373 dev->hb_interval = params.heartbeat_int; 374 375 return 0; 376 } 377 378 int apmf_get_dyn_slider_def_ac(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data) 379 { 380 return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_AC, data, sizeof(*data)); 381 } 382 383 int apmf_get_dyn_slider_def_dc(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data) 384 { 385 return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_DC, data, sizeof(*data)); 386 } 387 388 int apmf_install_handler(struct amd_pmf_dev *pmf_dev) 389 { 390 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev); 391 acpi_status status; 392 393 /* Install the APMF Notify handler */ 394 if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) && 395 is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS)) { 396 status = acpi_install_notify_handler(ahandle, ACPI_ALL_NOTIFY, 397 apmf_event_handler, pmf_dev); 398 if (ACPI_FAILURE(status)) { 399 dev_err(pmf_dev->dev, "failed to install notify handler\n"); 400 return -ENODEV; 401 } 402 403 /* Call the handler once manually to catch up with possibly missed notifies. */ 404 apmf_event_handler(ahandle, 0, pmf_dev); 405 } 406 407 return 0; 408 } 409 410 static acpi_status apmf_walk_resources(struct acpi_resource *res, void *data) 411 { 412 struct amd_pmf_dev *dev = data; 413 414 switch (res->type) { 415 case ACPI_RESOURCE_TYPE_ADDRESS64: 416 dev->policy_addr = res->data.address64.address.minimum; 417 dev->policy_sz = res->data.address64.address.address_length; 418 break; 419 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32: 420 dev->policy_addr = res->data.fixed_memory32.address; 421 dev->policy_sz = res->data.fixed_memory32.address_length; 422 break; 423 } 424 425 if (!dev->policy_addr || dev->policy_sz > POLICY_BUF_MAX_SZ || dev->policy_sz == 0) { 426 pr_err("Incorrect Policy params, possibly a SBIOS bug\n"); 427 return AE_ERROR; 428 } 429 430 return AE_OK; 431 } 432 433 int apmf_check_smart_pc(struct amd_pmf_dev *pmf_dev) 434 { 435 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev); 436 acpi_status status; 437 438 status = acpi_walk_resources(ahandle, METHOD_NAME__CRS, apmf_walk_resources, pmf_dev); 439 if (ACPI_FAILURE(status)) { 440 dev_err(pmf_dev->dev, "acpi_walk_resources failed :%d\n", status); 441 return -EINVAL; 442 } 443 444 return 0; 445 } 446 447 void apmf_acpi_deinit(struct amd_pmf_dev *pmf_dev) 448 { 449 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev); 450 451 if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1) 452 cancel_delayed_work_sync(&pmf_dev->heart_beat); 453 454 if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) && 455 is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS)) 456 acpi_remove_notify_handler(ahandle, ACPI_ALL_NOTIFY, apmf_event_handler); 457 } 458 459 int apmf_acpi_init(struct amd_pmf_dev *pmf_dev) 460 { 461 int ret; 462 463 ret = apmf_if_verify_interface(pmf_dev); 464 if (ret) { 465 dev_err(pmf_dev->dev, "APMF verify interface failed :%d\n", ret); 466 goto out; 467 } 468 469 ret = apmf_get_system_params(pmf_dev); 470 if (ret) { 471 dev_dbg(pmf_dev->dev, "APMF apmf_get_system_params failed :%d\n", ret); 472 goto out; 473 } 474 475 if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1) { 476 /* send heartbeats only if the interval is not zero */ 477 INIT_DELAYED_WORK(&pmf_dev->heart_beat, apmf_sbios_heartbeat_notify); 478 schedule_delayed_work(&pmf_dev->heart_beat, 0); 479 } 480 481 out: 482 return ret; 483 } 484