summaryrefslogtreecommitdiff
path: root/drivers/platform/x86/amd/wbrf.c
blob: dd197b3aebe06a8638aa11ed24155b038a79a5b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// SPDX-License-Identifier: GPL-2.0
/*
 * Wifi Frequency Band Manage Interface
 * Copyright (C) 2023 Advanced Micro Devices
 */

#include <linux/acpi.h>
#include <linux/acpi_amd_wbrf.h>

/*
 * Functions bit vector for WBRF method
 *
 * Bit 0: WBRF supported.
 * Bit 1: Function 1 (Add / Remove frequency) is supported.
 * Bit 2: Function 2 (Get frequency list) is supported.
 */
#define WBRF_ENABLED		0x0
#define WBRF_RECORD			0x1
#define WBRF_RETRIEVE		0x2

#define WBRF_REVISION		0x1

/*
 * The data structure used for WBRF_RETRIEVE is not naturally aligned.
 * And unfortunately the design has been settled down.
 */
struct amd_wbrf_ranges_out {
	u32			num_of_ranges;
	struct freq_band_range	band_list[MAX_NUM_OF_WBRF_RANGES];
} __packed;

static const guid_t wifi_acpi_dsm_guid =
	GUID_INIT(0x7b7656cf, 0xdc3d, 0x4c1c,
		  0x83, 0xe9, 0x66, 0xe7, 0x21, 0xde, 0x30, 0x70);

/*
 * Used to notify consumer (amdgpu driver currently) about
 * the wifi frequency is change.
 */
static BLOCKING_NOTIFIER_HEAD(wbrf_chain_head);

static int wbrf_record(struct acpi_device *adev, uint8_t action, struct wbrf_ranges_in_out *in)
{
	union acpi_object argv4;
	union acpi_object *tmp;
	union acpi_object *obj;
	u32 num_of_ranges = 0;
	u32 num_of_elements;
	u32 arg_idx = 0;
	int ret;
	u32 i;

	if (!in)
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(in->band_list); i++) {
		if (in->band_list[i].start && in->band_list[i].end)
			num_of_ranges++;
	}

	/*
	 * The num_of_ranges value in the "in" object supplied by
	 * the caller is required to be equal to the number of
	 * entries in the band_list array in there.
	 */
	if (num_of_ranges != in->num_of_ranges)
		return -EINVAL;

	/*
	 * Every input frequency band comes with two end points(start/end)
	 * and each is accounted as an element. Meanwhile the range count
	 * and action type are accounted as an element each.
	 * So, the total element count = 2 * num_of_ranges + 1 + 1.
	 */
	num_of_elements = 2 * num_of_ranges + 2;

	tmp = kcalloc(num_of_elements, sizeof(*tmp), GFP_KERNEL);
	if (!tmp)
		return -ENOMEM;

	argv4.package.type = ACPI_TYPE_PACKAGE;
	argv4.package.count = num_of_elements;
	argv4.package.elements = tmp;

	/* save the number of ranges*/
	tmp[0].integer.type = ACPI_TYPE_INTEGER;
	tmp[0].integer.value = num_of_ranges;

	/* save the action(WBRF_RECORD_ADD/REMOVE/RETRIEVE) */
	tmp[1].integer.type = ACPI_TYPE_INTEGER;
	tmp[1].integer.value = action;

	arg_idx = 2;
	for (i = 0; i < ARRAY_SIZE(in->band_list); i++) {
		if (!in->band_list[i].start || !in->band_list[i].end)
			continue;

		tmp[arg_idx].integer.type = ACPI_TYPE_INTEGER;
		tmp[arg_idx++].integer.value = in->band_list[i].start;
		tmp[arg_idx].integer.type = ACPI_TYPE_INTEGER;
		tmp[arg_idx++].integer.value = in->band_list[i].end;
	}

	obj = acpi_evaluate_dsm(adev->handle, &wifi_acpi_dsm_guid,
				WBRF_REVISION, WBRF_RECORD, &argv4);

	if (!obj)
		return -EINVAL;

	if (obj->type != ACPI_TYPE_INTEGER) {
		ret = -EINVAL;
		goto out;
	}

	ret = obj->integer.value;
	if (ret)
		ret = -EINVAL;

out:
	ACPI_FREE(obj);
	kfree(tmp);

	return ret;
}

/**
 * acpi_amd_wbrf_add_remove - add or remove the frequency band the device is using
 *
 * @dev: device pointer
 * @action: remove or add the frequency band into bios
 * @in: input structure containing the frequency band the device is using
 *
 * Broadcast to other consumers the frequency band the device starts
 * to use. Underneath the surface the information is cached into an
 * internal buffer first. Then a notification is sent to all those
 * registered consumers. So then they can retrieve that buffer to
 * know the latest active frequency bands. Consumers that haven't
 * yet been registered can retrieve the information from the cache
 * when they register.
 *
 * Return:
 * 0 for success add/remove wifi frequency band.
 * Returns a negative error code for failure.
 */
int acpi_amd_wbrf_add_remove(struct device *dev, uint8_t action, struct wbrf_ranges_in_out *in)
{
	struct acpi_device *adev;
	int ret;

	adev = ACPI_COMPANION(dev);
	if (!adev)
		return -ENODEV;

	ret = wbrf_record(adev, action, in);
	if (ret)
		return ret;

	blocking_notifier_call_chain(&wbrf_chain_head, WBRF_CHANGED, NULL);

	return 0;
}
EXPORT_SYMBOL_GPL(acpi_amd_wbrf_add_remove);

/**
 * acpi_amd_wbrf_supported_producer - determine if the WBRF can be enabled
 *                                    for the device as a producer
 *
 * @dev: device pointer
 *
 * Check if the platform equipped with necessary implementations to
 * support WBRF for the device as a producer.
 *
 * Return:
 * true if WBRF is supported, otherwise returns false
 */
bool acpi_amd_wbrf_supported_producer(struct device *dev)
{
	struct acpi_device *adev;

	adev = ACPI_COMPANION(dev);
	if (!adev)
		return false;

	return acpi_check_dsm(adev->handle, &wifi_acpi_dsm_guid,
			      WBRF_REVISION, BIT(WBRF_RECORD));
}
EXPORT_SYMBOL_GPL(acpi_amd_wbrf_supported_producer);

/**
 * acpi_amd_wbrf_supported_consumer - determine if the WBRF can be enabled
 *                                    for the device as a consumer
 *
 * @dev: device pointer
 *
 * Determine if the platform equipped with necessary implementations to
 * support WBRF for the device as a consumer.
 *
 * Return:
 * true if WBRF is supported, otherwise returns false.
 */
bool acpi_amd_wbrf_supported_consumer(struct device *dev)
{
	struct acpi_device *adev;

	adev = ACPI_COMPANION(dev);
	if (!adev)
		return false;

	return acpi_check_dsm(adev->handle, &wifi_acpi_dsm_guid,
			      WBRF_REVISION, BIT(WBRF_RETRIEVE));
}
EXPORT_SYMBOL_GPL(acpi_amd_wbrf_supported_consumer);

/**
 * amd_wbrf_retrieve_freq_band - retrieve current active frequency bands
 *
 * @dev: device pointer
 * @out: output structure containing all the active frequency bands
 *
 * Retrieve the current active frequency bands which were broadcasted
 * by other producers. The consumer who calls this API should take
 * proper actions if any of the frequency band may cause RFI with its
 * own frequency band used.
 *
 * Return:
 * 0 for getting wifi freq band successfully.
 * Returns a negative error code for failure.
 */
int amd_wbrf_retrieve_freq_band(struct device *dev, struct wbrf_ranges_in_out *out)
{
	struct amd_wbrf_ranges_out acpi_out = {0};
	struct acpi_device *adev;
	union acpi_object *obj;
	union acpi_object param;
	int ret = 0;

	adev = ACPI_COMPANION(dev);
	if (!adev)
		return -ENODEV;

	param.type = ACPI_TYPE_STRING;
	param.string.length = 0;
	param.string.pointer = NULL;

	obj = acpi_evaluate_dsm(adev->handle, &wifi_acpi_dsm_guid,
							WBRF_REVISION, WBRF_RETRIEVE, &param);
	if (!obj)
		return -EINVAL;

	/*
	 * The return buffer is with variable length and the format below:
	 * number_of_entries(1 DWORD):       Number of entries
	 * start_freq of 1st entry(1 QWORD): Start frequency of the 1st entry
	 * end_freq of 1st entry(1 QWORD):   End frequency of the 1st entry
	 * ...
	 * ...
	 * start_freq of the last entry(1 QWORD)
	 * end_freq of the last entry(1 QWORD)
	 *
	 * Thus the buffer length is determined by the number of entries.
	 * - For zero entry scenario, the buffer length will be 4 bytes.
	 * - For one entry scenario, the buffer length will be 20 bytes.
	 */
	if (obj->buffer.length > sizeof(acpi_out) || obj->buffer.length < 4) {
		dev_err(dev, "Wrong sized WBRT information");
		ret = -EINVAL;
		goto out;
	}
	memcpy(&acpi_out, obj->buffer.pointer, obj->buffer.length);

	out->num_of_ranges = acpi_out.num_of_ranges;
	memcpy(out->band_list, acpi_out.band_list, sizeof(acpi_out.band_list));

out:
	ACPI_FREE(obj);
	return ret;
}
EXPORT_SYMBOL_GPL(amd_wbrf_retrieve_freq_band);

/**
 * amd_wbrf_register_notifier - register for notifications of frequency
 *                                   band update
 *
 * @nb: driver notifier block
 *
 * The consumer should register itself via this API so that it can get
 * notified on the frequency band updates from other producers.
 *
 * Return:
 * 0 for registering a consumer driver successfully.
 * Returns a negative error code for failure.
 */
int amd_wbrf_register_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(&wbrf_chain_head, nb);
}
EXPORT_SYMBOL_GPL(amd_wbrf_register_notifier);

/**
 * amd_wbrf_unregister_notifier - unregister for notifications of
 *                                     frequency band update
 *
 * @nb: driver notifier block
 *
 * The consumer should call this API when it is longer interested with
 * the frequency band updates from other producers. Usually, this should
 * be performed during driver cleanup.
 *
 * Return:
 * 0 for unregistering a consumer driver.
 * Returns a negative error code for failure.
 */
int amd_wbrf_unregister_notifier(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(&wbrf_chain_head, nb);
}
EXPORT_SYMBOL_GPL(amd_wbrf_unregister_notifier);