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 <[email protected]>
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, &params);
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, &params);
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, &params);
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, &params);
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_v2(acpi_handle handle,u32 event,void * data)324 static void apmf_event_handler_v2(acpi_handle handle, u32 event, void *data)
325 {
326 	struct amd_pmf_dev *pmf_dev = data;
327 	int ret;
328 
329 	guard(mutex)(&pmf_dev->cb_mutex);
330 
331 	ret = apmf_get_sbios_requests_v2(pmf_dev, &pmf_dev->req);
332 	if (ret)
333 		dev_err(pmf_dev->dev, "Failed to get v2 SBIOS requests: %d\n", ret);
334 }
335 
apmf_event_handler(acpi_handle handle,u32 event,void * data)336 static void apmf_event_handler(acpi_handle handle, u32 event, void *data)
337 {
338 	struct amd_pmf_dev *pmf_dev = data;
339 	struct apmf_sbios_req req;
340 	int ret;
341 
342 	guard(mutex)(&pmf_dev->update_mutex);
343 	ret = apmf_get_sbios_requests(pmf_dev, &req);
344 	if (ret) {
345 		dev_err(pmf_dev->dev, "Failed to get SBIOS requests:%d\n", ret);
346 		return;
347 	}
348 
349 	if (req.pending_req & BIT(APMF_AMT_NOTIFICATION)) {
350 		dev_dbg(pmf_dev->dev, "AMT is supported and notifications %s\n",
351 			req.amt_event ? "Enabled" : "Disabled");
352 		pmf_dev->amt_enabled = !!req.amt_event;
353 
354 		if (pmf_dev->amt_enabled)
355 			amd_pmf_handle_amt(pmf_dev);
356 		else
357 			amd_pmf_reset_amt(pmf_dev);
358 	}
359 
360 	if (req.pending_req & BIT(APMF_CQL_NOTIFICATION)) {
361 		dev_dbg(pmf_dev->dev, "CQL is supported and notifications %s\n",
362 			req.cql_event ? "Enabled" : "Disabled");
363 
364 		/* update the target mode information */
365 		if (pmf_dev->amt_enabled)
366 			amd_pmf_update_2_cql(pmf_dev, req.cql_event);
367 	}
368 }
369 
apmf_if_verify_interface(struct amd_pmf_dev * pdev)370 static int apmf_if_verify_interface(struct amd_pmf_dev *pdev)
371 {
372 	struct apmf_verify_interface output;
373 	int err;
374 
375 	err = apmf_if_call_store_buffer(pdev, APMF_FUNC_VERIFY_INTERFACE, &output, sizeof(output));
376 	if (err)
377 		return err;
378 
379 	/* only set if not already set by a quirk */
380 	if (!pdev->supported_func)
381 		pdev->supported_func = output.supported_functions;
382 
383 	dev_dbg(pdev->dev, "supported functions:0x%x notifications:0x%x version:%u\n",
384 		output.supported_functions, output.notification_mask, output.version);
385 
386 	pdev->pmf_if_version = output.version;
387 
388 	return 0;
389 }
390 
apmf_get_system_params(struct amd_pmf_dev * dev)391 static int apmf_get_system_params(struct amd_pmf_dev *dev)
392 {
393 	struct apmf_system_params params;
394 	int err;
395 
396 	if (!is_apmf_func_supported(dev, APMF_FUNC_GET_SYS_PARAMS))
397 		return -EINVAL;
398 
399 	err = apmf_if_call_store_buffer(dev, APMF_FUNC_GET_SYS_PARAMS, &params, sizeof(params));
400 	if (err)
401 		return err;
402 
403 	dev_dbg(dev->dev, "system params mask:0x%x flags:0x%x cmd_code:0x%x heartbeat:%d\n",
404 		params.valid_mask,
405 		params.flags,
406 		params.command_code,
407 		params.heartbeat_int);
408 	params.flags = params.flags & params.valid_mask;
409 	dev->hb_interval = params.heartbeat_int;
410 
411 	return 0;
412 }
413 
apmf_get_dyn_slider_def_ac(struct amd_pmf_dev * pdev,struct apmf_dyn_slider_output * data)414 int apmf_get_dyn_slider_def_ac(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
415 {
416 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_AC, data, sizeof(*data));
417 }
418 
apmf_get_dyn_slider_def_dc(struct amd_pmf_dev * pdev,struct apmf_dyn_slider_output * data)419 int apmf_get_dyn_slider_def_dc(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
420 {
421 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_DC, data, sizeof(*data));
422 }
423 
apmf_install_handler(struct amd_pmf_dev * pmf_dev)424 int apmf_install_handler(struct amd_pmf_dev *pmf_dev)
425 {
426 	acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
427 	acpi_status status;
428 
429 	/* Install the APMF Notify handler */
430 	if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
431 	    is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS)) {
432 		status = acpi_install_notify_handler(ahandle, ACPI_ALL_NOTIFY,
433 						     apmf_event_handler, pmf_dev);
434 		if (ACPI_FAILURE(status)) {
435 			dev_err(pmf_dev->dev, "failed to install notify handler\n");
436 			return -ENODEV;
437 		}
438 
439 		/* Call the handler once manually to catch up with possibly missed notifies. */
440 		apmf_event_handler(ahandle, 0, pmf_dev);
441 	}
442 
443 	if (pmf_dev->smart_pc_enabled && pmf_dev->pmf_if_version == PMF_IF_V2) {
444 		status = acpi_install_notify_handler(ahandle, ACPI_ALL_NOTIFY,
445 						     apmf_event_handler_v2, pmf_dev);
446 		if (ACPI_FAILURE(status)) {
447 			dev_err(pmf_dev->dev, "failed to install notify handler for custom BIOS inputs\n");
448 			return -ENODEV;
449 		}
450 	}
451 
452 	return 0;
453 }
454 
apmf_check_smart_pc(struct amd_pmf_dev * pmf_dev)455 int apmf_check_smart_pc(struct amd_pmf_dev *pmf_dev)
456 {
457 	struct platform_device *pdev = to_platform_device(pmf_dev->dev);
458 
459 	pmf_dev->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
460 	if (!pmf_dev->res) {
461 		dev_dbg(pmf_dev->dev, "Failed to get I/O memory resource\n");
462 		return -EINVAL;
463 	}
464 
465 	pmf_dev->policy_addr = pmf_dev->res->start;
466 	/*
467 	 * We cannot use resource_size() here because it adds an extra byte to round off the size.
468 	 * In the case of PMF ResourceTemplate(), this rounding is already handled within the _CRS.
469 	 * Using resource_size() would increase the resource size by 1, causing a mismatch with the
470 	 * length field and leading to issues. Therefore, simply use end-start of the ACPI resource
471 	 * to obtain the actual length.
472 	 */
473 	pmf_dev->policy_sz = pmf_dev->res->end - pmf_dev->res->start;
474 
475 	if (!pmf_dev->policy_addr || pmf_dev->policy_sz > POLICY_BUF_MAX_SZ ||
476 	    pmf_dev->policy_sz == 0) {
477 		dev_err(pmf_dev->dev, "Incorrect policy params, possibly a SBIOS bug\n");
478 		return -EINVAL;
479 	}
480 
481 	return 0;
482 }
483 
amd_pmf_smartpc_apply_bios_output(struct amd_pmf_dev * dev,u32 val,u32 preq,u32 idx)484 int amd_pmf_smartpc_apply_bios_output(struct amd_pmf_dev *dev, u32 val, u32 preq, u32 idx)
485 {
486 	if (!is_apmf_func_supported(dev, APMF_FUNC_NOTIFY_SMART_PC_UPDATES))
487 		return -EINVAL;
488 
489 	return apmf_notify_smart_pc_update(dev, val, preq, idx);
490 }
491 
apmf_acpi_deinit(struct amd_pmf_dev * pmf_dev)492 void apmf_acpi_deinit(struct amd_pmf_dev *pmf_dev)
493 {
494 	acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
495 
496 	if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1)
497 		cancel_delayed_work_sync(&pmf_dev->heart_beat);
498 
499 	if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
500 	    is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS))
501 		acpi_remove_notify_handler(ahandle, ACPI_ALL_NOTIFY, apmf_event_handler);
502 
503 	if (pmf_dev->smart_pc_enabled && pmf_dev->pmf_if_version == PMF_IF_V2)
504 		acpi_remove_notify_handler(ahandle, ACPI_ALL_NOTIFY, apmf_event_handler_v2);
505 }
506 
apmf_acpi_init(struct amd_pmf_dev * pmf_dev)507 int apmf_acpi_init(struct amd_pmf_dev *pmf_dev)
508 {
509 	int ret;
510 
511 	ret = apmf_if_verify_interface(pmf_dev);
512 	if (ret) {
513 		dev_err(pmf_dev->dev, "APMF verify interface failed :%d\n", ret);
514 		goto out;
515 	}
516 
517 	ret = apmf_get_system_params(pmf_dev);
518 	if (ret) {
519 		dev_dbg(pmf_dev->dev, "APMF apmf_get_system_params failed :%d\n", ret);
520 		goto out;
521 	}
522 
523 	if (pmf_dev->hb_interval && pmf_dev->pmf_if_version == PMF_IF_V1) {
524 		/* send heartbeats only if the interval is not zero */
525 		INIT_DELAYED_WORK(&pmf_dev->heart_beat, apmf_sbios_heartbeat_notify);
526 		schedule_delayed_work(&pmf_dev->heart_beat, 0);
527 	}
528 
529 out:
530 	return ret;
531 }
532