xref: /linux/drivers/net/wireless/intel/iwlwifi/fw/regulatory.c (revision 0d91e1d069fa5740d23e9a1f47d36279f50b699b)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2023, 2025-2026 Intel Corporation
4  */
5 #include <linux/dmi.h>
6 #include "iwl-drv.h"
7 #include "iwl-debug.h"
8 #include "regulatory.h"
9 #include "fw/runtime.h"
10 #include "fw/uefi.h"
11 
12 #define GET_BIOS_TABLE(__name, ...)					\
13 do {									\
14 	int ret = iwl_uefi_get_ ## __name(__VA_ARGS__);			\
15 	if (ret < 0)							\
16 		ret = iwl_acpi_get_ ## __name(__VA_ARGS__);		\
17 	return ret;							\
18 } while (0)
19 
20 #define IWL_BIOS_TABLE_LOADER(__name)					\
21 int iwl_bios_get_ ## __name(struct iwl_fw_runtime *fwrt)		\
22 {GET_BIOS_TABLE(__name, fwrt); }					\
23 IWL_EXPORT_SYMBOL(iwl_bios_get_ ## __name)
24 
25 #define IWL_BIOS_TABLE_LOADER_DATA(__name, data_type)			\
26 int iwl_bios_get_ ## __name(struct iwl_fw_runtime *fwrt,		\
27 			    data_type * data)				\
28 {GET_BIOS_TABLE(__name, fwrt, data); }					\
29 IWL_EXPORT_SYMBOL(iwl_bios_get_ ## __name)
30 
31 IWL_BIOS_TABLE_LOADER(wrds_table);
32 IWL_BIOS_TABLE_LOADER(ewrd_table);
33 IWL_BIOS_TABLE_LOADER(wsss_table);
34 IWL_BIOS_TABLE_LOADER(ewss_table);
35 IWL_BIOS_TABLE_LOADER(wgds_table);
36 IWL_BIOS_TABLE_LOADER(ppag_table);
37 IWL_BIOS_TABLE_LOADER(phy_filters);
38 IWL_BIOS_TABLE_LOADER_DATA(tas_table, struct iwl_tas_data);
39 IWL_BIOS_TABLE_LOADER_DATA(pwr_limit, u64);
40 IWL_BIOS_TABLE_LOADER_DATA(mcc, char);
41 IWL_BIOS_TABLE_LOADER_DATA(eckv, u32);
42 IWL_BIOS_TABLE_LOADER_DATA(wbem, u32);
43 IWL_BIOS_TABLE_LOADER_DATA(dsbr, u32);
44 
45 
46 static const struct dmi_system_id dmi_ppag_approved_list[] = {
47 	{ .ident = "HP",
48 	  .matches = {
49 			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
50 		},
51 	},
52 	{ .ident = "SAMSUNG_ELECTRONICS",
53 	  .matches = {
54 			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD"),
55 		},
56 	},
57 	{ .ident = "SAMSUNG",
58 	  .matches = {
59 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Samsung"),
60 		},
61 	},
62 	{ .ident = "MSFT",
63 	  .matches = {
64 			DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
65 		},
66 	},
67 	{ .ident = "ASUSTEK",
68 	  .matches = {
69 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
70 		},
71 	},
72 	{ .ident = "ASUS",
73 	  .matches = {
74 			DMI_MATCH(DMI_SYS_VENDOR, "ASUS"),
75 		},
76 	},
77 	{ .ident = "GOOGLE-HP",
78 	  .matches = {
79 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
80 			DMI_MATCH(DMI_BOARD_VENDOR, "HP"),
81 		},
82 	},
83 	{ .ident = "GOOGLE-ASUS",
84 	  .matches = {
85 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
86 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek COMPUTER INC."),
87 		},
88 	},
89 	{ .ident = "GOOGLE-SAMSUNG",
90 	  .matches = {
91 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
92 			DMI_MATCH(DMI_BOARD_VENDOR, "SAMSUNG ELECTRONICS CO., LTD"),
93 		},
94 	},
95 	{ .ident = "DELL",
96 	  .matches = {
97 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
98 		},
99 	},
100 	{ .ident = "DELL",
101 	  .matches = {
102 			DMI_MATCH(DMI_SYS_VENDOR, "Alienware"),
103 		},
104 	},
105 	{ .ident = "RAZER",
106 	  .matches = {
107 			DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
108 		},
109 	},
110 	{ .ident = "Honor",
111 	  .matches = {
112 			DMI_MATCH(DMI_SYS_VENDOR, "HONOR"),
113 		},
114 	},
115 	{ .ident = "WIKO",
116 	  .matches = {
117 			DMI_MATCH(DMI_SYS_VENDOR, "WIKO"),
118 		},
119 	},
120 	{ .ident = "XIAOMI",
121 	  .matches = {
122 			DMI_MATCH(DMI_SYS_VENDOR, "XIAOMI"),
123 		},
124 	},
125 	{}
126 };
127 
128 static const struct dmi_system_id dmi_tas_approved_list[] = {
129 	{ .ident = "HP",
130 	  .matches = {
131 			DMI_MATCH(DMI_SYS_VENDOR, "HP"),
132 		},
133 	},
134 	{ .ident = "SAMSUNG_ELECTRONICS",
135 	  .matches = {
136 			DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD"),
137 		},
138 	},
139 	{ .ident = "SAMSUNG",
140 	  .matches = {
141 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Samsung"),
142 		},
143 	},
144 		{ .ident = "LENOVO",
145 	  .matches = {
146 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
147 		},
148 	},
149 	{ .ident = "DELL",
150 	  .matches = {
151 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
152 		},
153 	},
154 	{ .ident = "MSFT",
155 	  .matches = {
156 			DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
157 		},
158 	},
159 	{ .ident = "Acer",
160 	  .matches = {
161 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
162 		},
163 	},
164 	{ .ident = "ASUSTEK",
165 	  .matches = {
166 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
167 		},
168 	},
169 	{ .ident = "ASUS",
170 	  .matches = {
171 			DMI_MATCH(DMI_SYS_VENDOR, "ASUS"),
172 		},
173 	},
174 	{ .ident = "GOOGLE-HP",
175 	  .matches = {
176 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
177 			DMI_MATCH(DMI_BOARD_VENDOR, "HP"),
178 		},
179 	},
180 	{ .ident = "GOOGLE-ASUS",
181 	  .matches = {
182 			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
183 			DMI_MATCH(DMI_BOARD_VENDOR, "ASUS"),
184 		},
185 	},
186 	{ .ident = "MSI",
187 	  .matches = {
188 			DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star International Co., Ltd."),
189 		},
190 	},
191 	{ .ident = "Honor",
192 	  .matches = {
193 			DMI_MATCH(DMI_SYS_VENDOR, "HONOR"),
194 		},
195 	},
196 	/* keep last */
197 	{}
198 };
199 
200 bool iwl_sar_geo_support(struct iwl_fw_runtime *fwrt)
201 {
202 	u32 ucode_ver = fwrt->fw->ucode_ver;
203 
204 	/*
205 	 * The PER_CHAIN_LIMIT_OFFSET_CMD command is not supported on
206 	 * earlier firmware versions.  Unfortunately, we don't have a
207 	 * TLV API flag to rely on, so rely on the ucode version.  This
208 	 * was implemented initially on version 38 and then backported
209 	 * to 17.  It was also backported to 29, but only for 7265D
210 	 * devices.  The intention was to have it in 36 as well, but not
211 	 * all 8000 family got this feature enabled.  The 8000 family is
212 	 * the only one using version 36, so skip this version entirely.
213 	 */
214 	return ucode_ver >= 38 ||
215 		(ucode_ver == 17 &&
216 		 fwrt->trans->info.hw_rev != CSR_HW_REV_TYPE_3160) ||
217 		(ucode_ver == 29 &&
218 		 ((fwrt->trans->info.hw_rev & CSR_HW_REV_TYPE_MSK) ==
219 		  CSR_HW_REV_TYPE_7265D));
220 }
221 IWL_EXPORT_SYMBOL(iwl_sar_geo_support);
222 
223 int iwl_sar_geo_fill_table(struct iwl_fw_runtime *fwrt,
224 			   struct iwl_per_chain_offset *table,
225 			   u32 n_bands, u32 n_profiles)
226 {
227 	int i, j;
228 
229 	if (!fwrt->geo_enabled)
230 		return -ENODATA;
231 
232 	if (!iwl_sar_geo_support(fwrt))
233 		return -EOPNOTSUPP;
234 
235 	for (i = 0; i < n_profiles; i++) {
236 		for (j = 0; j < n_bands; j++) {
237 			struct iwl_per_chain_offset *chain =
238 				&table[i * n_bands + j];
239 
240 			chain->max_tx_power =
241 				cpu_to_le16(fwrt->geo_profiles[i].bands[j].max);
242 			chain->chain_a =
243 				fwrt->geo_profiles[i].bands[j].chains[0];
244 			chain->chain_b =
245 				fwrt->geo_profiles[i].bands[j].chains[1];
246 			IWL_DEBUG_RADIO(fwrt,
247 					"SAR geographic profile[%d] Band[%d]: chain A = %d chain B = %d max_tx_power = %d\n",
248 					i, j,
249 					fwrt->geo_profiles[i].bands[j].chains[0],
250 					fwrt->geo_profiles[i].bands[j].chains[1],
251 					fwrt->geo_profiles[i].bands[j].max);
252 		}
253 	}
254 
255 	return 0;
256 }
257 IWL_EXPORT_SYMBOL(iwl_sar_geo_fill_table);
258 
259 static int iwl_sar_fill_table(struct iwl_fw_runtime *fwrt,
260 			      __le16 *per_chain, u32 n_subbands,
261 			      int prof_a, int prof_b,
262 			      const struct iwl_sar_profile *profiles,
263 			      bool is_standalone)
264 {
265 	int profs[BIOS_SAR_NUM_CHAINS] = { prof_a, prof_b };
266 	int i, j;
267 
268 	BUILD_BUG_ON(ARRAY_SIZE(fwrt->sar_standalone_profiles[0].chains[0].subbands) !=
269 		     ARRAY_SIZE(fwrt->sar_profiles[0].chains[0].subbands));
270 
271 	if (WARN_ON_ONCE(n_subbands >
272 			 ARRAY_SIZE(fwrt->sar_profiles[0].chains[0].subbands)))
273 		return -EINVAL;
274 
275 	for (i = 0; i < BIOS_SAR_NUM_CHAINS; i++) {
276 		const struct iwl_sar_profile *prof;
277 
278 		/* don't allow SAR to be disabled (profile 0 means disable) */
279 		if (profs[i] == 0)
280 			return -EPERM;
281 
282 		/* we are off by one, so allow up to BIOS_SAR_MAX_PROFILE_NUM */
283 		if (profs[i] > BIOS_SAR_MAX_PROFILE_NUM)
284 			return -EINVAL;
285 
286 		/* profiles go from 1 to 4, so decrement to access the array */
287 		prof = &profiles[profs[i] - 1];
288 
289 		/* if the profile is disabled, do nothing */
290 		if (!prof->enabled) {
291 			IWL_DEBUG_RADIO(fwrt,
292 					"SAR %sprofile %d is disabled.\n",
293 					is_standalone ? "standalone " : "",
294 					profs[i]);
295 			/*
296 			 * if one of the profiles is disabled, we
297 			 * ignore all of them and return 1 to
298 			 * differentiate disabled from other failures.
299 			 */
300 			return 1;
301 		}
302 
303 		IWL_DEBUG_INFO(fwrt, "SAR %s: chain %d profile index %d\n",
304 			       is_standalone ? "standalone" : "EWRD",
305 			       i, profs[i]);
306 		IWL_DEBUG_RADIO(fwrt, "  Chain[%d]:\n", i);
307 		for (j = 0; j < n_subbands; j++) {
308 			per_chain[i * n_subbands + j] =
309 				cpu_to_le16(prof->chains[i].subbands[j]);
310 			IWL_DEBUG_RADIO(fwrt, "    Band[%d] = %d * .125dBm\n",
311 					j, prof->chains[i].subbands[j]);
312 		}
313 	}
314 
315 	return 0;
316 }
317 
318 int iwl_sar_fill_profile(struct iwl_fw_runtime *fwrt,
319 			 __le16 *per_chain, u32 n_tables, u32 n_subbands,
320 			 int prof_a, int prof_b,
321 			 const struct iwl_sar_profile *profiles,
322 			 bool is_standalone)
323 {
324 	int i, ret = 0;
325 
326 	for (i = 0; i < n_tables; i++) {
327 		ret = iwl_sar_fill_table(fwrt,
328 			&per_chain[i * n_subbands * BIOS_SAR_NUM_CHAINS],
329 			n_subbands, prof_a, prof_b, profiles,
330 			is_standalone);
331 		if (ret)
332 			break;
333 	}
334 
335 	return ret;
336 }
337 IWL_EXPORT_SYMBOL(iwl_sar_fill_profile);
338 
339 bool iwl_is_ppag_approved(struct iwl_fw_runtime *fwrt)
340 {
341 	if (!dmi_check_system(dmi_ppag_approved_list)) {
342 		IWL_DEBUG_RADIO(fwrt,
343 				"System vendor '%s' is not in the approved list, disabling PPAG.\n",
344 				dmi_get_system_info(DMI_SYS_VENDOR) ?: "<unknown>");
345 		fwrt->ppag_flags = 0;
346 		return false;
347 	}
348 
349 	return true;
350 }
351 IWL_EXPORT_SYMBOL(iwl_is_ppag_approved);
352 
353 /* Print the PPAG table as read from BIOS */
354 void iwl_bios_print_ppag(struct iwl_fw_runtime *fwrt, int n_subbands)
355 {
356 	int i, j;
357 
358 	IWL_DEBUG_RADIO(fwrt, "PPAG table as read from BIOS:\n");
359 	IWL_DEBUG_RADIO(fwrt, "PPAG revision = %d\n", fwrt->ppag_bios_rev);
360 	IWL_DEBUG_RADIO(fwrt, "PPAG flags = 0x%x\n", fwrt->ppag_flags);
361 
362 	if (WARN_ON_ONCE(n_subbands >
363 			 ARRAY_SIZE(fwrt->ppag_chains[0].subbands)))
364 		return;
365 
366 	for (i = 0; i < ARRAY_SIZE(fwrt->ppag_chains); i++)
367 		for (j = 0; j < n_subbands; j++)
368 			IWL_DEBUG_RADIO(fwrt,
369 					"ppag_chains[%d].subbands[%d] = %d\n",
370 					i, j,
371 					fwrt->ppag_chains[i].subbands[j]);
372 }
373 
374 bool iwl_is_tas_approved(void)
375 {
376 	return dmi_check_system(dmi_tas_approved_list);
377 }
378 IWL_EXPORT_SYMBOL(iwl_is_tas_approved);
379 
380 struct iwl_tas_selection_data
381 iwl_parse_tas_selection(const u32 tas_selection_in, const u8 tbl_rev)
382 {
383 	struct iwl_tas_selection_data tas_selection_out = {};
384 	u8 override_iec = u32_get_bits(tas_selection_in,
385 				       IWL_WTAS_OVERRIDE_IEC_MSK);
386 	u8 canada_tas_uhb = u32_get_bits(tas_selection_in,
387 					 IWL_WTAS_CANADA_UHB_MSK);
388 	u8 enabled_iec = u32_get_bits(tas_selection_in,
389 				      IWL_WTAS_ENABLE_IEC_MSK);
390 	u8 usa_tas_uhb = u32_get_bits(tas_selection_in,
391 				      IWL_WTAS_USA_UHB_MSK);
392 
393 	if (tbl_rev > 0) {
394 		tas_selection_out.usa_tas_uhb_allowed = usa_tas_uhb;
395 		tas_selection_out.override_tas_iec = override_iec;
396 		tas_selection_out.enable_tas_iec = enabled_iec;
397 	}
398 
399 	if (tbl_rev > 1)
400 		tas_selection_out.canada_tas_uhb_allowed = canada_tas_uhb;
401 
402 	return tas_selection_out;
403 }
404 IWL_EXPORT_SYMBOL(iwl_parse_tas_selection);
405 
406 bool iwl_add_mcc_to_tas_block_list(u16 *list, u8 *size, u16 mcc)
407 {
408 	for (int i = 0; i < *size; i++) {
409 		if (list[i] == mcc)
410 			return true;
411 	}
412 
413 	/* Verify that there is room for another country
414 	 * If *size == IWL_WTAS_BLACK_LIST_MAX, then the table is full.
415 	 */
416 	if (*size >= IWL_WTAS_BLACK_LIST_MAX)
417 		return false;
418 
419 	list[(*size)++] = mcc;
420 	return true;
421 }
422 IWL_EXPORT_SYMBOL(iwl_add_mcc_to_tas_block_list);
423 
424 int iwl_bios_get_dsm(struct iwl_fw_runtime *fwrt, enum iwl_dsm_funcs func,
425 		     u32 *value)
426 {
427 	GET_BIOS_TABLE(dsm, fwrt, func, value);
428 }
429 IWL_EXPORT_SYMBOL(iwl_bios_get_dsm);
430 
431 bool iwl_puncturing_is_allowed_in_bios(u32 puncturing, u16 mcc)
432 {
433 	/* Some kind of regulatory mess means we need to currently disallow
434 	 * puncturing in the US and Canada unless enabled in BIOS.
435 	 */
436 	switch (mcc) {
437 	case IWL_MCC_US:
438 		return puncturing & IWL_UEFI_CNV_PUNCTURING_USA_EN_MSK;
439 	case IWL_MCC_CANADA:
440 		return puncturing & IWL_UEFI_CNV_PUNCTURING_CANADA_EN_MSK;
441 	default:
442 		return true;
443 	}
444 }
445 IWL_EXPORT_SYMBOL(iwl_puncturing_is_allowed_in_bios);
446 
447 bool iwl_rfi_is_enabled_in_bios(struct iwl_fw_runtime *fwrt)
448 {
449 	/* default behaviour is disabled */
450 	u32 value = 0;
451 	int ret = iwl_bios_get_dsm(fwrt, DSM_FUNC_RFI_CONFIG, &value);
452 
453 	if (ret < 0) {
454 		IWL_DEBUG_RADIO(fwrt, "Failed to get DSM RFI, ret=%d\n", ret);
455 		return false;
456 	}
457 
458 	value &= DSM_VALUE_RFI_DISABLE;
459 	/* RFI BIOS CONFIG value can be 0 or 3 only.
460 	 * i.e 0 means DDR and DLVR enabled. 3 means DDR and DLVR disabled.
461 	 * 1 and 2 are invalid BIOS configurations, So, it's not possible to
462 	 * disable ddr/dlvr separately.
463 	 */
464 	if (!value) {
465 		IWL_DEBUG_RADIO(fwrt, "DSM RFI is evaluated to enable\n");
466 		return true;
467 	} else if (value == DSM_VALUE_RFI_DISABLE) {
468 		IWL_DEBUG_RADIO(fwrt, "DSM RFI is evaluated to disable\n");
469 	} else {
470 		IWL_DEBUG_RADIO(fwrt,
471 				"DSM RFI got invalid value, value=%d\n", value);
472 	}
473 
474 	return false;
475 }
476 IWL_EXPORT_SYMBOL(iwl_rfi_is_enabled_in_bios);
477 
478 /**
479  * iwl_bios_get_guid_lock_status - resolve connectivity variables lock status
480  * @fwrt: the firmware runtime context
481  *
482  * Determine the root-of-trust for the connectivity UEFI variables. The UEFI
483  * GUID Lock Indicator (GLUI) is preferred: if it attests the tables as locked
484  * or in test mode, that result is used. Otherwise (GLUI is missing, invalid or
485  * reports unlocked) the ACPI GUID Lock Indicator is consulted and can override
486  * the GLUI result.
487  *
488  * This only caches the result in &fwrt->uefi_tables_lock_status
489  * (see &enum iwl_uefi_cnv_guid_status).
490  */
491 void iwl_bios_get_guid_lock_status(struct iwl_fw_runtime *fwrt)
492 {
493 	iwl_uefi_get_guid_lock_status(fwrt);
494 	if (fwrt->uefi_tables_lock_status == UEFI_CNV_GUID_LOCKED ||
495 	    fwrt->uefi_tables_lock_status == UEFI_CNV_GUID_TEST_MODE)
496 		return;
497 
498 	iwl_acpi_get_guid_lock_status(fwrt);
499 }
500 IWL_EXPORT_SYMBOL(iwl_bios_get_guid_lock_status);
501