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