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
apmf_if_call(struct amd_pmf_dev * pdev,int fn,struct acpi_buffer * param)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
apmf_if_call_store_buffer(struct amd_pmf_dev * pdev,int fn,void * dest,size_t out_sz)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
apts_if_call(struct amd_pmf_dev * pdev,u32 state_index)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
apts_if_call_store_buffer(struct amd_pmf_dev * pdev,u32 index,void * data,size_t out_sz)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
is_apmf_func_supported(struct amd_pmf_dev * pdev,unsigned long index)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
apts_get_static_slider_granular_v2(struct amd_pmf_dev * pdev,struct amd_pmf_apts_granular_output * data,u32 apts_idx)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
apmf_get_static_slider_granular_v2(struct amd_pmf_dev * pdev,struct apmf_static_slider_granular_output_v2 * data)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
apmf_get_static_slider_granular(struct amd_pmf_dev * pdev,struct apmf_static_slider_granular_output * data)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
apmf_os_power_slider_update(struct amd_pmf_dev * pdev,u8 event)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
apmf_sbios_heartbeat_notify(struct work_struct * work)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
amd_pmf_notify_sbios_heartbeat_event_v2(struct amd_pmf_dev * dev,u8 flag)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
apmf_update_fan_idx(struct amd_pmf_dev * pdev,bool manual,u32 idx)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
apmf_notify_smart_pc_update(struct amd_pmf_dev * pdev,u32 val,u32 preq,u32 index)285 static int apmf_notify_smart_pc_update(struct amd_pmf_dev *pdev, u32 val, u32 preq, u32 index)
286 {
287 struct amd_pmf_notify_smart_pc_update args;
288 struct acpi_buffer params;
289 union acpi_object *info;
290
291 args.size = sizeof(args);
292 args.pending_req = preq;
293 args.custom_bios[index] = val;
294
295 params.length = sizeof(args);
296 params.pointer = &args;
297
298 info = apmf_if_call(pdev, APMF_FUNC_NOTIFY_SMART_PC_UPDATES, ¶ms);
299 if (!info)
300 return -EIO;
301
302 kfree(info);
303 dev_dbg(pdev->dev, "Notify smart pc update, val: %u\n", val);
304
305 return 0;
306 }
307
apmf_get_auto_mode_def(struct amd_pmf_dev * pdev,struct apmf_auto_mode * data)308 int apmf_get_auto_mode_def(struct amd_pmf_dev *pdev, struct apmf_auto_mode *data)
309 {
310 return apmf_if_call_store_buffer(pdev, APMF_FUNC_AUTO_MODE, data, sizeof(*data));
311 }
312
apmf_get_sbios_requests_v2(struct amd_pmf_dev * pdev,struct apmf_sbios_req_v2 * req)313 int apmf_get_sbios_requests_v2(struct amd_pmf_dev *pdev, struct apmf_sbios_req_v2 *req)
314 {
315 return apmf_if_call_store_buffer(pdev, APMF_FUNC_SBIOS_REQUESTS, req, sizeof(*req));
316 }
317
apmf_get_sbios_requests(struct amd_pmf_dev * pdev,struct apmf_sbios_req * req)318 int apmf_get_sbios_requests(struct amd_pmf_dev *pdev, struct apmf_sbios_req *req)
319 {
320 return apmf_if_call_store_buffer(pdev, APMF_FUNC_SBIOS_REQUESTS,
321 req, sizeof(*req));
322 }
323
apmf_event_handler(acpi_handle handle,u32 event,void * data)324 static void apmf_event_handler(acpi_handle handle, u32 event, void *data)
325 {
326 struct amd_pmf_dev *pmf_dev = data;
327 struct apmf_sbios_req req;
328 int ret;
329
330 mutex_lock(&pmf_dev->update_mutex);
331 ret = apmf_get_sbios_requests(pmf_dev, &req);
332 if (ret) {
333 dev_err(pmf_dev->dev, "Failed to get SBIOS requests:%d\n", ret);
334 goto out;
335 }
336
337 if (req.pending_req & BIT(APMF_AMT_NOTIFICATION)) {
338 dev_dbg(pmf_dev->dev, "AMT is supported and notifications %s\n",
339 req.amt_event ? "Enabled" : "Disabled");
340 pmf_dev->amt_enabled = !!req.amt_event;
341
342 if (pmf_dev->amt_enabled)
343 amd_pmf_handle_amt(pmf_dev);
344 else
345 amd_pmf_reset_amt(pmf_dev);
346 }
347
348 if (req.pending_req & BIT(APMF_CQL_NOTIFICATION)) {
349 dev_dbg(pmf_dev->dev, "CQL is supported and notifications %s\n",
350 req.cql_event ? "Enabled" : "Disabled");
351
352 /* update the target mode information */
353 if (pmf_dev->amt_enabled)
354 amd_pmf_update_2_cql(pmf_dev, req.cql_event);
355 }
356 out:
357 mutex_unlock(&pmf_dev->update_mutex);
358 }
359
apmf_if_verify_interface(struct amd_pmf_dev * pdev)360 static int apmf_if_verify_interface(struct amd_pmf_dev *pdev)
361 {
362 struct apmf_verify_interface output;
363 int err;
364
365 err = apmf_if_call_store_buffer(pdev, APMF_FUNC_VERIFY_INTERFACE, &output, sizeof(output));
366 if (err)
367 return err;
368
369 /* only set if not already set by a quirk */
370 if (!pdev->supported_func)
371 pdev->supported_func = output.supported_functions;
372
373 dev_dbg(pdev->dev, "supported functions:0x%x notifications:0x%x version:%u\n",
374 output.supported_functions, output.notification_mask, output.version);
375
376 pdev->pmf_if_version = output.version;
377
378 return 0;
379 }
380
apmf_get_system_params(struct amd_pmf_dev * dev)381 static int apmf_get_system_params(struct amd_pmf_dev *dev)
382 {
383 struct apmf_system_params params;
384 int err;
385
386 if (!is_apmf_func_supported(dev, APMF_FUNC_GET_SYS_PARAMS))
387 return -EINVAL;
388
389 err = apmf_if_call_store_buffer(dev, APMF_FUNC_GET_SYS_PARAMS, ¶ms, sizeof(params));
390 if (err)
391 return err;
392
393 dev_dbg(dev->dev, "system params mask:0x%x flags:0x%x cmd_code:0x%x heartbeat:%d\n",
394 params.valid_mask,
395 params.flags,
396 params.command_code,
397 params.heartbeat_int);
398 params.flags = params.flags & params.valid_mask;
399 dev->hb_interval = params.heartbeat_int;
400
401 return 0;
402 }
403
apmf_get_dyn_slider_def_ac(struct amd_pmf_dev * pdev,struct apmf_dyn_slider_output * data)404 int apmf_get_dyn_slider_def_ac(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
405 {
406 return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_AC, data, sizeof(*data));
407 }
408
apmf_get_dyn_slider_def_dc(struct amd_pmf_dev * pdev,struct apmf_dyn_slider_output * data)409 int apmf_get_dyn_slider_def_dc(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
410 {
411 return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_DC, data, sizeof(*data));
412 }
413
apmf_install_handler(struct amd_pmf_dev * pmf_dev)414 int apmf_install_handler(struct amd_pmf_dev *pmf_dev)
415 {
416 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
417 acpi_status status;
418
419 /* Install the APMF Notify handler */
420 if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
421 is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS)) {
422 status = acpi_install_notify_handler(ahandle, ACPI_ALL_NOTIFY,
423 apmf_event_handler, pmf_dev);
424 if (ACPI_FAILURE(status)) {
425 dev_err(pmf_dev->dev, "failed to install notify handler\n");
426 return -ENODEV;
427 }
428
429 /* Call the handler once manually to catch up with possibly missed notifies. */
430 apmf_event_handler(ahandle, 0, pmf_dev);
431 }
432
433 return 0;
434 }
435
apmf_walk_resources(struct acpi_resource * res,void * data)436 static acpi_status apmf_walk_resources(struct acpi_resource *res, void *data)
437 {
438 struct amd_pmf_dev *dev = data;
439
440 switch (res->type) {
441 case ACPI_RESOURCE_TYPE_ADDRESS64:
442 dev->policy_addr = res->data.address64.address.minimum;
443 dev->policy_sz = res->data.address64.address.address_length;
444 break;
445 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
446 dev->policy_addr = res->data.fixed_memory32.address;
447 dev->policy_sz = res->data.fixed_memory32.address_length;
448 break;
449 }
450
451 if (!dev->policy_addr || dev->policy_sz > POLICY_BUF_MAX_SZ || dev->policy_sz == 0) {
452 pr_err("Incorrect Policy params, possibly a SBIOS bug\n");
453 return AE_ERROR;
454 }
455
456 return AE_OK;
457 }
458
apmf_check_smart_pc(struct amd_pmf_dev * pmf_dev)459 int apmf_check_smart_pc(struct amd_pmf_dev *pmf_dev)
460 {
461 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
462 acpi_status status;
463
464 status = acpi_walk_resources(ahandle, METHOD_NAME__CRS, apmf_walk_resources, pmf_dev);
465 if (ACPI_FAILURE(status)) {
466 dev_dbg(pmf_dev->dev, "acpi_walk_resources failed :%d\n", status);
467 return -EINVAL;
468 }
469
470 return 0;
471 }
472
amd_pmf_smartpc_apply_bios_output(struct amd_pmf_dev * dev,u32 val,u32 preq,u32 idx)473 int amd_pmf_smartpc_apply_bios_output(struct amd_pmf_dev *dev, u32 val, u32 preq, u32 idx)
474 {
475 if (!is_apmf_func_supported(dev, APMF_FUNC_NOTIFY_SMART_PC_UPDATES))
476 return -EINVAL;
477
478 return apmf_notify_smart_pc_update(dev, val, preq, idx);
479 }
480
apmf_acpi_deinit(struct amd_pmf_dev * pmf_dev)481 void apmf_acpi_deinit(struct amd_pmf_dev *pmf_dev)
482 {
483 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
484
485 if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1)
486 cancel_delayed_work_sync(&pmf_dev->heart_beat);
487
488 if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
489 is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS))
490 acpi_remove_notify_handler(ahandle, ACPI_ALL_NOTIFY, apmf_event_handler);
491 }
492
apmf_acpi_init(struct amd_pmf_dev * pmf_dev)493 int apmf_acpi_init(struct amd_pmf_dev *pmf_dev)
494 {
495 int ret;
496
497 ret = apmf_if_verify_interface(pmf_dev);
498 if (ret) {
499 dev_err(pmf_dev->dev, "APMF verify interface failed :%d\n", ret);
500 goto out;
501 }
502
503 ret = apmf_get_system_params(pmf_dev);
504 if (ret) {
505 dev_dbg(pmf_dev->dev, "APMF apmf_get_system_params failed :%d\n", ret);
506 goto out;
507 }
508
509 if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1) {
510 /* send heartbeats only if the interval is not zero */
511 INIT_DELAYED_WORK(&pmf_dev->heart_beat, apmf_sbios_heartbeat_notify);
512 schedule_delayed_work(&pmf_dev->heart_beat, 0);
513 }
514
515 out:
516 return ret;
517 }
518