Merge tag 'pinctrl-v6.9-2' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-block.git] / drivers / platform / x86 / amd / pmf / acpi.c
CommitLineData
5eb315eb
SS
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
7d77dcc8
SS
14#define APMF_CQL_NOTIFICATION 2
15#define APMF_AMT_NOTIFICATION 3
16
5eb315eb
SS
17static 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
50static 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
88out:
89 kfree(info);
90 return err;
91}
92
3eecb434
SS
93static 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
121static 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);
153out:
154 kfree(info);
155 return err;
156}
157
5eb315eb
SS
158int 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
3eecb434
SS
164int 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
48d38f56
SS
173int 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
4c71ae41
SS
183int 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
33c9ab5b
SS
193int 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;
33c9ab5b
SS
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, &params);
206 if (!info)
4527898e 207 return -EIO;
33c9ab5b
SS
208
209 kfree(info);
4527898e 210 return 0;
33c9ab5b
SS
211}
212
b9ab888b
SS
213static 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)
9dd3f1ef 221 return;
b9ab888b
SS
222
223 schedule_delayed_work(&dev->heart_beat, msecs_to_jiffies(dev->hb_interval * 1000));
b9ab888b
SS
224 kfree(info);
225}
226
6262938e
SS
227int 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, &params);
257 if (!info)
258 return -EIO;
259
260 kfree(info);
261 return 0;
262}
263
a3281ec5
SS
264int 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;
a3281ec5
SS
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, &params);
4527898e
ME
278 if (!info)
279 return -EIO;
a3281ec5 280
a3281ec5 281 kfree(info);
4527898e 282 return 0;
a3281ec5
SS
283}
284
3f5571d9
SS
285int 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
5fdc8b82
SS
290int 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
7d77dcc8
SS
295int 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
301static 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 }
333out:
334 mutex_unlock(&pmf_dev->update_mutex);
335}
336
5eb315eb
SS
337static 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
ed13f622
ML
346 /* only set if not already set by a quirk */
347 if (!pdev->supported_func)
348 pdev->supported_func = output.supported_functions;
349
a33e9e10
SS
350 dev_dbg(pdev->dev, "supported functions:0x%x notifications:0x%x version:%u\n",
351 output.supported_functions, output.notification_mask, output.version);
352
353 pdev->pmf_if_version = output.version;
5eb315eb
SS
354
355 return 0;
356}
357
358static int apmf_get_system_params(struct amd_pmf_dev *dev)
359{
360 struct apmf_system_params params;
361 int err;
362
363 if (!is_apmf_func_supported(dev, APMF_FUNC_GET_SYS_PARAMS))
364 return -EINVAL;
365
366 err = apmf_if_call_store_buffer(dev, APMF_FUNC_GET_SYS_PARAMS, &params, sizeof(params));
367 if (err)
368 return err;
369
b9ab888b 370 dev_dbg(dev->dev, "system params mask:0x%x flags:0x%x cmd_code:0x%x heartbeat:%d\n",
5eb315eb
SS
371 params.valid_mask,
372 params.flags,
b9ab888b
SS
373 params.command_code,
374 params.heartbeat_int);
5eb315eb 375 params.flags = params.flags & params.valid_mask;
b9ab888b 376 dev->hb_interval = params.heartbeat_int;
5eb315eb
SS
377
378 return 0;
379}
380
1738061c
SS
381int apmf_get_dyn_slider_def_ac(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
382{
383 return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_AC, data, sizeof(*data));
384}
385
386int apmf_get_dyn_slider_def_dc(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
387{
388 return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_DC, data, sizeof(*data));
389}
390
22ee98cb
SS
391int apmf_install_handler(struct amd_pmf_dev *pmf_dev)
392{
393 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
394 acpi_status status;
395
396 /* Install the APMF Notify handler */
397 if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
398 is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS)) {
399 status = acpi_install_notify_handler(ahandle, ACPI_ALL_NOTIFY,
400 apmf_event_handler, pmf_dev);
401 if (ACPI_FAILURE(status)) {
402 dev_err(pmf_dev->dev, "failed to install notify handler\n");
403 return -ENODEV;
404 }
405
406 /* Call the handler once manually to catch up with possibly missed notifies. */
407 apmf_event_handler(ahandle, 0, pmf_dev);
408 }
409
410 return 0;
411}
412
7c45534a
SS
413static acpi_status apmf_walk_resources(struct acpi_resource *res, void *data)
414{
415 struct amd_pmf_dev *dev = data;
416
417 switch (res->type) {
418 case ACPI_RESOURCE_TYPE_ADDRESS64:
419 dev->policy_addr = res->data.address64.address.minimum;
420 dev->policy_sz = res->data.address64.address.address_length;
421 break;
422 case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
423 dev->policy_addr = res->data.fixed_memory32.address;
424 dev->policy_sz = res->data.fixed_memory32.address_length;
425 break;
426 }
427
428 if (!dev->policy_addr || dev->policy_sz > POLICY_BUF_MAX_SZ || dev->policy_sz == 0) {
429 pr_err("Incorrect Policy params, possibly a SBIOS bug\n");
430 return AE_ERROR;
431 }
432
433 return AE_OK;
434}
435
436int apmf_check_smart_pc(struct amd_pmf_dev *pmf_dev)
437{
438 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
439 acpi_status status;
440
441 status = acpi_walk_resources(ahandle, METHOD_NAME__CRS, apmf_walk_resources, pmf_dev);
442 if (ACPI_FAILURE(status)) {
03cea821 443 dev_dbg(pmf_dev->dev, "acpi_walk_resources failed :%d\n", status);
7c45534a
SS
444 return -EINVAL;
445 }
446
447 return 0;
448}
449
b9ab888b
SS
450void apmf_acpi_deinit(struct amd_pmf_dev *pmf_dev)
451{
7d77dcc8
SS
452 acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
453
6262938e 454 if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1)
b9ab888b 455 cancel_delayed_work_sync(&pmf_dev->heart_beat);
7d77dcc8
SS
456
457 if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
458 is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS))
459 acpi_remove_notify_handler(ahandle, ACPI_ALL_NOTIFY, apmf_event_handler);
b9ab888b
SS
460}
461
5eb315eb
SS
462int apmf_acpi_init(struct amd_pmf_dev *pmf_dev)
463{
464 int ret;
465
466 ret = apmf_if_verify_interface(pmf_dev);
467 if (ret) {
468 dev_err(pmf_dev->dev, "APMF verify interface failed :%d\n", ret);
469 goto out;
470 }
471
472 ret = apmf_get_system_params(pmf_dev);
473 if (ret) {
839e90e7 474 dev_dbg(pmf_dev->dev, "APMF apmf_get_system_params failed :%d\n", ret);
5eb315eb
SS
475 goto out;
476 }
477
6262938e 478 if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1) {
b9ab888b
SS
479 /* send heartbeats only if the interval is not zero */
480 INIT_DELAYED_WORK(&pmf_dev->heart_beat, apmf_sbios_heartbeat_notify);
481 schedule_delayed_work(&pmf_dev->heart_beat, 0);
482 }
483
5eb315eb
SS
484out:
485 return ret;
486}