xref: /openbmc/linux/drivers/platform/x86/amd/pmf/acpi.c (revision b755c25f)
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 int is_apmf_func_supported(struct amd_pmf_dev *pdev, unsigned long index)
94 {
95 	/* If bit-n is set, that indicates function n+1 is supported */
96 	return !!(pdev->supported_func & BIT(index - 1));
97 }
98 
99 int apmf_get_static_slider_granular(struct amd_pmf_dev *pdev,
100 				    struct apmf_static_slider_granular_output *data)
101 {
102 	if (!is_apmf_func_supported(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR))
103 		return -EINVAL;
104 
105 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_STATIC_SLIDER_GRANULAR,
106 									 data, sizeof(*data));
107 }
108 
109 int apmf_os_power_slider_update(struct amd_pmf_dev *pdev, u8 event)
110 {
111 	struct os_power_slider args;
112 	struct acpi_buffer params;
113 	union acpi_object *info;
114 	int err = 0;
115 
116 	args.size = sizeof(args);
117 	args.slider_event = event;
118 
119 	params.length = sizeof(args);
120 	params.pointer = (void *)&args;
121 
122 	info = apmf_if_call(pdev, APMF_FUNC_OS_POWER_SLIDER_UPDATE, &params);
123 	if (!info)
124 		err = -EIO;
125 
126 	kfree(info);
127 	return err;
128 }
129 
130 static void apmf_sbios_heartbeat_notify(struct work_struct *work)
131 {
132 	struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, heart_beat.work);
133 	union acpi_object *info;
134 
135 	dev_dbg(dev->dev, "Sending heartbeat to SBIOS\n");
136 	info = apmf_if_call(dev, APMF_FUNC_SBIOS_HEARTBEAT, NULL);
137 	if (!info)
138 		goto out;
139 
140 	schedule_delayed_work(&dev->heart_beat, msecs_to_jiffies(dev->hb_interval * 1000));
141 
142 out:
143 	kfree(info);
144 }
145 
146 int apmf_update_fan_idx(struct amd_pmf_dev *pdev, bool manual, u32 idx)
147 {
148 	union acpi_object *info;
149 	struct apmf_fan_idx args;
150 	struct acpi_buffer params;
151 	int err = 0;
152 
153 	args.size = sizeof(args);
154 	args.fan_ctl_mode = manual;
155 	args.fan_ctl_idx = idx;
156 
157 	params.length = sizeof(args);
158 	params.pointer = (void *)&args;
159 
160 	info = apmf_if_call(pdev, APMF_FUNC_SET_FAN_IDX, &params);
161 	if (!info) {
162 		err = -EIO;
163 		goto out;
164 	}
165 
166 out:
167 	kfree(info);
168 	return err;
169 }
170 
171 int apmf_get_auto_mode_def(struct amd_pmf_dev *pdev, struct apmf_auto_mode *data)
172 {
173 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_AUTO_MODE, data, sizeof(*data));
174 }
175 
176 int apmf_get_sbios_requests(struct amd_pmf_dev *pdev, struct apmf_sbios_req *req)
177 {
178 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_SBIOS_REQUESTS,
179 									 req, sizeof(*req));
180 }
181 
182 static void apmf_event_handler(acpi_handle handle, u32 event, void *data)
183 {
184 	struct amd_pmf_dev *pmf_dev = data;
185 	struct apmf_sbios_req req;
186 	int ret;
187 
188 	mutex_lock(&pmf_dev->update_mutex);
189 	ret = apmf_get_sbios_requests(pmf_dev, &req);
190 	if (ret) {
191 		dev_err(pmf_dev->dev, "Failed to get SBIOS requests:%d\n", ret);
192 		goto out;
193 	}
194 
195 	if (req.pending_req & BIT(APMF_AMT_NOTIFICATION)) {
196 		dev_dbg(pmf_dev->dev, "AMT is supported and notifications %s\n",
197 			req.amt_event ? "Enabled" : "Disabled");
198 		pmf_dev->amt_enabled = !!req.amt_event;
199 
200 		if (pmf_dev->amt_enabled)
201 			amd_pmf_handle_amt(pmf_dev);
202 		else
203 			amd_pmf_reset_amt(pmf_dev);
204 	}
205 
206 	if (req.pending_req & BIT(APMF_CQL_NOTIFICATION)) {
207 		dev_dbg(pmf_dev->dev, "CQL is supported and notifications %s\n",
208 			req.cql_event ? "Enabled" : "Disabled");
209 
210 		/* update the target mode information */
211 		if (pmf_dev->amt_enabled)
212 			amd_pmf_update_2_cql(pmf_dev, req.cql_event);
213 	}
214 out:
215 	mutex_unlock(&pmf_dev->update_mutex);
216 }
217 
218 static int apmf_if_verify_interface(struct amd_pmf_dev *pdev)
219 {
220 	struct apmf_verify_interface output;
221 	int err;
222 
223 	err = apmf_if_call_store_buffer(pdev, APMF_FUNC_VERIFY_INTERFACE, &output, sizeof(output));
224 	if (err)
225 		return err;
226 
227 	pdev->supported_func = output.supported_functions;
228 	dev_dbg(pdev->dev, "supported functions:0x%x notifications:0x%x\n",
229 		output.supported_functions, output.notification_mask);
230 
231 	return 0;
232 }
233 
234 static int apmf_get_system_params(struct amd_pmf_dev *dev)
235 {
236 	struct apmf_system_params params;
237 	int err;
238 
239 	if (!is_apmf_func_supported(dev, APMF_FUNC_GET_SYS_PARAMS))
240 		return -EINVAL;
241 
242 	err = apmf_if_call_store_buffer(dev, APMF_FUNC_GET_SYS_PARAMS, &params, sizeof(params));
243 	if (err)
244 		return err;
245 
246 	dev_dbg(dev->dev, "system params mask:0x%x flags:0x%x cmd_code:0x%x heartbeat:%d\n",
247 		params.valid_mask,
248 		params.flags,
249 		params.command_code,
250 		params.heartbeat_int);
251 	params.flags = params.flags & params.valid_mask;
252 	dev->hb_interval = params.heartbeat_int;
253 
254 	return 0;
255 }
256 
257 int apmf_get_dyn_slider_def_ac(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
258 {
259 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_AC, data, sizeof(*data));
260 }
261 
262 int apmf_get_dyn_slider_def_dc(struct amd_pmf_dev *pdev, struct apmf_dyn_slider_output *data)
263 {
264 	return apmf_if_call_store_buffer(pdev, APMF_FUNC_DYN_SLIDER_DC, data, sizeof(*data));
265 }
266 
267 int apmf_install_handler(struct amd_pmf_dev *pmf_dev)
268 {
269 	acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
270 	acpi_status status;
271 
272 	/* Install the APMF Notify handler */
273 	if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
274 	    is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS)) {
275 		status = acpi_install_notify_handler(ahandle, ACPI_ALL_NOTIFY,
276 						     apmf_event_handler, pmf_dev);
277 		if (ACPI_FAILURE(status)) {
278 			dev_err(pmf_dev->dev, "failed to install notify handler\n");
279 			return -ENODEV;
280 		}
281 
282 		/* Call the handler once manually to catch up with possibly missed notifies. */
283 		apmf_event_handler(ahandle, 0, pmf_dev);
284 	}
285 
286 	return 0;
287 }
288 
289 void apmf_acpi_deinit(struct amd_pmf_dev *pmf_dev)
290 {
291 	acpi_handle ahandle = ACPI_HANDLE(pmf_dev->dev);
292 
293 	if (pmf_dev->hb_interval)
294 		cancel_delayed_work_sync(&pmf_dev->heart_beat);
295 
296 	if (is_apmf_func_supported(pmf_dev, APMF_FUNC_AUTO_MODE) &&
297 	    is_apmf_func_supported(pmf_dev, APMF_FUNC_SBIOS_REQUESTS))
298 		acpi_remove_notify_handler(ahandle, ACPI_ALL_NOTIFY, apmf_event_handler);
299 }
300 
301 int apmf_acpi_init(struct amd_pmf_dev *pmf_dev)
302 {
303 	int ret;
304 
305 	ret = apmf_if_verify_interface(pmf_dev);
306 	if (ret) {
307 		dev_err(pmf_dev->dev, "APMF verify interface failed :%d\n", ret);
308 		goto out;
309 	}
310 
311 	ret = apmf_get_system_params(pmf_dev);
312 	if (ret) {
313 		dev_dbg(pmf_dev->dev, "APMF apmf_get_system_params failed :%d\n", ret);
314 		goto out;
315 	}
316 
317 	if (pmf_dev->hb_interval) {
318 		/* send heartbeats only if the interval is not zero */
319 		INIT_DELAYED_WORK(&pmf_dev->heart_beat, apmf_sbios_heartbeat_notify);
320 		schedule_delayed_work(&pmf_dev->heart_beat, 0);
321 	}
322 
323 out:
324 	return ret;
325 }
326