xref: /linux/drivers/platform/x86/amd/pmf/acpi.c (revision 6093a688a07da07808f0122f9aa2a3eed250d853)
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 is_apmf_bios_input_notifications_supported(struct amd_pmf_dev *pdev)
165 {
166 	return !!(pdev->notifications & CUSTOM_BIOS_INPUT_BITS);
167 }
168 
169 int apts_get_static_slider_granular_v2(struct amd_pmf_dev *pdev,
170 				       struct amd_pmf_apts_granular_output *data, u32 apts_idx)
171 {
172 	if (!is_apmf_func_supported(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR))
173 		return -EINVAL;
174 
175 	return apts_if_call_store_buffer(pdev, apts_idx, data, sizeof(*data));
176 }
177 
178 int apmf_get_static_slider_granular_v2(struct amd_pmf_dev *pdev,
179 				       struct apmf_static_slider_granular_output_v2 *data)
180 {
181 	if (!is_apmf_func_supported(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR))
182 		return -EINVAL;
183 
184 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR,
185 					 data, sizeof(*data));
186 }
187 
188 int apmf_get_static_slider_granular(struct amd_pmf_dev *pdev,
189 				    struct apmf_static_slider_granular_output *data)
190 {
191 	if (!is_apmf_func_supported(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR))
192 		return -EINVAL;
193 
194 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR,
195 									 data, sizeof(*data));
196 }
197 
198 int apmf_os_power_slider_update(struct amd_pmf_dev *pdev, u8 event)
199 {
200 	struct os_power_slider args;
201 	struct acpi_buffer params;
202 	union acpi_object *info;
203 
204 	args.size = sizeof(args);
205 	args.slider_event = event;
206 
207 	params.length = sizeof(args);
208 	params.pointer = (void *)&args;
209 
210 	info = apmf_if_call(pdev, APMF_FUNC_OS_POWER_SLIDER_UPDATE, &params);
211 	if (!info)
212 		return -EIO;
213 
214 	kfree(info);
215 	return 0;
216 }
217 
218 static void apmf_sbios_heartbeat_notify(struct work_struct *work)
219 {
220 	struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, heart_beat.work);
221 	union acpi_object *info;
222 
223 	dev_dbg(dev->dev, "Sending heartbeat to SBIOS\n");
224 	info = apmf_if_call(dev, APMF_FUNC_SBIOS_HEARTBEAT, NULL);
225 	if (!info)
226 		return;
227 
228 	schedule_delayed_work(&dev->heart_beat, secs_to_jiffies(dev->hb_interval));
229 	kfree(info);
230 }
231 
232 int amd_pmf_notify_sbios_heartbeat_event_v2(struct amd_pmf_dev *dev, u8 flag)
233 {
234 	struct sbios_hb_event_v2 args = { };
235 	struct acpi_buffer params;
236 	union acpi_object *info;
237 
238 	args.size = sizeof(args);
239 
240 	switch (flag) {
241 	case ON_LOAD:
242 		args.load = 1;
243 		break;
244 	case ON_UNLOAD:
245 		args.unload = 1;
246 		break;
247 	case ON_SUSPEND:
248 		args.suspend = 1;
249 		break;
250 	case ON_RESUME:
251 		args.resume = 1;
252 		break;
253 	default:
254 		dev_dbg(dev->dev, "Failed to send v2 heartbeat event, flag:0x%x\n", flag);
255 		return -EINVAL;
256 	}
257 
258 	params.length = sizeof(args);
259 	params.pointer = &args;
260 
261 	info = apmf_if_call(dev, APMF_FUNC_SBIOS_HEARTBEAT_V2, &params);
262 	if (!info)
263 		return -EIO;
264 
265 	kfree(info);
266 	return 0;
267 }
268 
269 int apmf_update_fan_idx(struct amd_pmf_dev *pdev, bool manual, u32 idx)
270 {
271 	union acpi_object *info;
272 	struct apmf_fan_idx args;
273 	struct acpi_buffer params;
274 
275 	args.size = sizeof(args);
276 	args.fan_ctl_mode = manual;
277 	args.fan_ctl_idx = idx;
278 
279 	params.length = sizeof(args);
280 	params.pointer = (void *)&args;
281 
282 	info = apmf_if_call(pdev, APMF_FUNC_SET_FAN_IDX, &params);
283 	if (!info)
284 		return -EIO;
285 
286 	kfree(info);
287 	return 0;
288 }
289 
290 static int apmf_notify_smart_pc_update(struct amd_pmf_dev *pdev, u32 val, u32 preq, u32 index)
291 {
292 	struct amd_pmf_notify_smart_pc_update args;
293 	struct acpi_buffer params;
294 	union acpi_object *info;
295 
296 	args.size = sizeof(args);
297 	args.pending_req = preq;
298 	args.custom_bios[index] = val;
299 
300 	params.length = sizeof(args);
301 	params.pointer = &args;
302 
303 	info = apmf_if_call(pdev, APMF_FUNC_NOTIFY_SMART_PC_UPDATES, &params);
304 	if (!info)
305 		return -EIO;
306 
307 	kfree(info);
308 	dev_dbg(pdev->dev, "Notify smart pc update, val: %u\n", val);
309 
310 	return 0;
311 }
312 
313 int apmf_get_auto_mode_def(struct amd_pmf_dev *pdev, struct apmf_auto_mode *data)
314 {
315 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_AUTO_MODE, data, sizeof(*data));
316 }
317 
318 int apmf_get_sbios_requests_v2(struct amd_pmf_dev *pdev, struct apmf_sbios_req_v2 *req)
319 {
320 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_SBIOS_REQUESTS, req, sizeof(*req));
321 }
322 
323 int apmf_get_sbios_requests_v1(struct amd_pmf_dev *pdev, struct apmf_sbios_req_v1 *req)
324 {
325 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_SBIOS_REQUESTS, req, sizeof(*req));
326 }
327 
328 int apmf_get_sbios_requests(struct amd_pmf_dev *pdev, struct apmf_sbios_req *req)
329 {
330 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_SBIOS_REQUESTS,
331 									 req, sizeof(*req));
332 }
333 
334 static void amd_pmf_handle_early_preq(struct amd_pmf_dev *pdev)
335 {
336 	if (!pdev->cb_flag)
337 		return;
338 
339 	amd_pmf_invoke_cmd_enact(pdev);
340 	pdev->cb_flag = false;
341 }
342 
343 static void apmf_event_handler_v2(acpi_handle handle, u32 event, void *data)
344 {
345 	struct amd_pmf_dev *pmf_dev = data;
346 	int ret;
347 
348 	guard(mutex)(&pmf_dev->cb_mutex);
349 
350 	ret = apmf_get_sbios_requests_v2(pmf_dev, &pmf_dev->req);
351 	if (ret) {
352 		dev_err(pmf_dev->dev, "Failed to get v2 SBIOS requests: %d\n", ret);
353 		return;
354 	}
355 
356 	dev_dbg(pmf_dev->dev, "Pending request (preq): 0x%x\n", pmf_dev->req.pending_req);
357 
358 	amd_pmf_handle_early_preq(pmf_dev);
359 }
360 
361 static void apmf_event_handler_v1(acpi_handle handle, u32 event, void *data)
362 {
363 	struct amd_pmf_dev *pmf_dev = data;
364 	int ret;
365 
366 	guard(mutex)(&pmf_dev->cb_mutex);
367 
368 	ret = apmf_get_sbios_requests_v1(pmf_dev, &pmf_dev->req1);
369 	if (ret) {
370 		dev_err(pmf_dev->dev, "Failed to get v1 SBIOS requests: %d\n", ret);
371 		return;
372 	}
373 
374 	dev_dbg(pmf_dev->dev, "Pending request (preq1): 0x%x\n", pmf_dev->req1.pending_req);
375 
376 	amd_pmf_handle_early_preq(pmf_dev);
377 }
378 
379 static void apmf_event_handler(acpi_handle handle, u32 event, void *data)
380 {
381 	struct amd_pmf_dev *pmf_dev = data;
382 	struct apmf_sbios_req req;
383 	int ret;
384 
385 	guard(mutex)(&pmf_dev->update_mutex);
386 	ret = apmf_get_sbios_requests(pmf_dev, &req);
387 	if (ret) {
388 		dev_err(pmf_dev->dev, "Failed to get SBIOS requests:%d\n", ret);
389 		return;
390 	}
391 
392 	if (req.pending_req & BIT(APMF_AMT_NOTIFICATION)) {
393 		dev_dbg(pmf_dev->dev, "AMT is supported and notifications %s\n",
394 			req.amt_event ? "Enabled" : "Disabled");
395 		pmf_dev->amt_enabled = !!req.amt_event;
396 
397 		if (pmf_dev->amt_enabled)
398 			amd_pmf_handle_amt(pmf_dev);
399 		else
400 			amd_pmf_reset_amt(pmf_dev);
401 	}
402 
403 	if (req.pending_req & BIT(APMF_CQL_NOTIFICATION)) {
404 		dev_dbg(pmf_dev->dev, "CQL is supported and notifications %s\n",
405 			req.cql_event ? "Enabled" : "Disabled");
406 
407 		/* update the target mode information */
408 		if (pmf_dev->amt_enabled)
409 			amd_pmf_update_2_cql(pmf_dev, req.cql_event);
410 	}
411 }
412 
413 static int apmf_if_verify_interface(struct amd_pmf_dev *pdev)
414 {
415 	struct apmf_verify_interface output;
416 	int err;
417 
418 	err = apmf_if_call_store_buffer(pdev, APMF_FUNC_VERIFY_INTERFACE, &output, sizeof(output));
419 	if (err)
420 		return err;
421 
422 	/* only set if not already set by a quirk */
423 	if (!pdev->supported_func)
424 		pdev->supported_func = output.supported_functions;
425 
426 	dev_dbg(pdev->dev, "supported functions:0x%x notifications:0x%x version:%u\n",
427 		output.supported_functions, output.notification_mask, output.version);
428 
429 	pdev->pmf_if_version = output.version;
430 
431 	pdev->notifications =  output.notification_mask;
432 	return 0;
433 }
434 
435 static int apmf_get_system_params(struct amd_pmf_dev *dev)
436 {
437 	struct apmf_system_params params;
438 	int err;
439 
440 	if (!is_apmf_func_supported(dev, APMF_FUNC_GET_SYS_PARAMS))
441 		return -EINVAL;
442 
443 	err = apmf_if_call_store_buffer(dev, APMF_FUNC_GET_SYS_PARAMS, &params, sizeof(params));
444 	if (err)
445 		return err;
446 
447 	dev_dbg(dev->dev, "system params mask:0x%x flags:0x%x cmd_code:0x%x heartbeat:%d\n",
448 		params.valid_mask,
449 		params.flags,
450 		params.command_code,
451 		params.heartbeat_int);
452 	params.flags = params.flags & params.valid_mask;
453 	dev->hb_interval = params.heartbeat_int;
454 
455 	return 0;
456 }
457 
458 int apmf_get_dyn_slider_def_ac(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
459 {
460 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_AC, data, sizeof(*data));
461 }
462 
463 int apmf_get_dyn_slider_def_dc(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
464 {
465 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_DC, data, sizeof(*data));
466 }
467 
468 static apmf_event_handler_t apmf_event_handlers[] = {
469 	[PMF_IF_V1] = apmf_event_handler_v1,
470 	[PMF_IF_V2] = apmf_event_handler_v2,
471 };
472 
473 int apmf_install_handler(struct amd_pmf_dev *pmf_dev)
474 {
475 	acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
476 	acpi_status status;
477 
478 	/* Install the APMF Notify handler */
479 	if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
480 	    is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS)) {
481 		status = acpi_install_notify_handler(ahandle, ACPI_ALL_NOTIFY,
482 						     apmf_event_handler, pmf_dev);
483 		if (ACPI_FAILURE(status)) {
484 			dev_err(pmf_dev->dev, "failed to install notify handler\n");
485 			return -ENODEV;
486 		}
487 
488 		/* Call the handler once manually to catch up with possibly missed notifies. */
489 		apmf_event_handler(ahandle, 0, pmf_dev);
490 	}
491 
492 	if (!pmf_dev->smart_pc_enabled)
493 		return -EINVAL;
494 
495 	switch (pmf_dev->pmf_if_version) {
496 	case PMF_IF_V1:
497 		if (!is_apmf_bios_input_notifications_supported(pmf_dev))
498 			break;
499 		fallthrough;
500 	case PMF_IF_V2:
501 		status = acpi_install_notify_handler(ahandle, ACPI_ALL_NOTIFY,
502 				apmf_event_handlers[pmf_dev->pmf_if_version], pmf_dev);
503 		if (ACPI_FAILURE(status)) {
504 			dev_err(pmf_dev->dev,
505 				"failed to install notify handler v%d for custom BIOS inputs\n",
506 				pmf_dev->pmf_if_version);
507 			return -ENODEV;
508 		}
509 		break;
510 	default:
511 		break;
512 	}
513 
514 	return 0;
515 }
516 
517 int apmf_check_smart_pc(struct amd_pmf_dev *pmf_dev)
518 {
519 	struct platform_device *pdev = to_platform_device(pmf_dev->dev);
520 
521 	pmf_dev->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
522 	if (!pmf_dev->res) {
523 		dev_dbg(pmf_dev->dev, "Failed to get I/O memory resource\n");
524 		return -EINVAL;
525 	}
526 
527 	pmf_dev->policy_addr = pmf_dev->res->start;
528 	/*
529 	 * We cannot use resource_size() here because it adds an extra byte to round off the size.
530 	 * In the case of PMF ResourceTemplate(), this rounding is already handled within the _CRS.
531 	 * Using resource_size() would increase the resource size by 1, causing a mismatch with the
532 	 * length field and leading to issues. Therefore, simply use end-start of the ACPI resource
533 	 * to obtain the actual length.
534 	 */
535 	pmf_dev->policy_sz = pmf_dev->res->end - pmf_dev->res->start;
536 
537 	if (!pmf_dev->policy_addr || pmf_dev->policy_sz > POLICY_BUF_MAX_SZ ||
538 	    pmf_dev->policy_sz == 0) {
539 		dev_err(pmf_dev->dev, "Incorrect policy params, possibly a SBIOS bug\n");
540 		return -EINVAL;
541 	}
542 
543 	return 0;
544 }
545 
546 int amd_pmf_smartpc_apply_bios_output(struct amd_pmf_dev *dev, u32 val, u32 preq, u32 idx)
547 {
548 	if (!is_apmf_func_supported(dev, APMF_FUNC_NOTIFY_SMART_PC_UPDATES))
549 		return -EINVAL;
550 
551 	return apmf_notify_smart_pc_update(dev, val, preq, idx);
552 }
553 
554 void apmf_acpi_deinit(struct amd_pmf_dev *pmf_dev)
555 {
556 	acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
557 
558 	if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1)
559 		cancel_delayed_work_sync(&pmf_dev->heart_beat);
560 
561 	if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
562 	    is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS))
563 		acpi_remove_notify_handler(ahandle, ACPI_ALL_NOTIFY, apmf_event_handler);
564 
565 	if (!pmf_dev->smart_pc_enabled)
566 		return;
567 
568 	switch (pmf_dev->pmf_if_version) {
569 	case PMF_IF_V1:
570 		if (!is_apmf_bios_input_notifications_supported(pmf_dev))
571 			break;
572 		fallthrough;
573 	case PMF_IF_V2:
574 		acpi_remove_notify_handler(ahandle, ACPI_ALL_NOTIFY,
575 					   apmf_event_handlers[pmf_dev->pmf_if_version]);
576 		break;
577 	default:
578 		break;
579 	}
580 }
581 
582 int apmf_acpi_init(struct amd_pmf_dev *pmf_dev)
583 {
584 	int ret;
585 
586 	ret = apmf_if_verify_interface(pmf_dev);
587 	if (ret) {
588 		dev_err(pmf_dev->dev, "APMF verify interface failed :%d\n", ret);
589 		goto out;
590 	}
591 
592 	ret = apmf_get_system_params(pmf_dev);
593 	if (ret) {
594 		dev_dbg(pmf_dev->dev, "APMF apmf_get_system_params failed :%d\n", ret);
595 		goto out;
596 	}
597 
598 	if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1) {
599 		/* send heartbeats only if the interval is not zero */
600 		INIT_DELAYED_WORK(&pmf_dev->heart_beat, apmf_sbios_heartbeat_notify);
601 		schedule_delayed_work(&pmf_dev->heart_beat, 0);
602 	}
603 
604 out:
605 	return ret;
606 }
607