1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: utosi - Support for the _OSI predefined control method
5  *
6  * Copyright (C) 2000 - 2023, Intel Corp.
7  *
8  *****************************************************************************/
9 
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 
13 #define _COMPONENT          ACPI_UTILITIES
14 ACPI_MODULE_NAME("utosi")
15 
16 /******************************************************************************
17  *
18  * ACPICA policy for new _OSI strings:
19  *
20  * It is the stated policy of ACPICA that new _OSI strings will be integrated
21  * into this module as soon as possible after they are defined. It is strongly
22  * recommended that all ACPICA hosts mirror this policy and integrate any
23  * changes to this module as soon as possible. There are several historical
24  * reasons behind this policy:
25  *
26  * 1) New BIOSs tend to test only the case where the host responds TRUE to
27  *    the latest version of Windows, which would respond to the latest/newest
28  *    _OSI string. Not responding TRUE to the latest version of Windows will
29  *    risk executing untested code paths throughout the DSDT and SSDTs.
30  *
31  * 2) If a new _OSI string is recognized only after a significant delay, this
32  *    has the potential to cause problems on existing working machines because
33  *    of the possibility that a new and different path through the ASL code
34  *    will be executed.
35  *
36  * 3) New _OSI strings are tending to come out about once per year. A delay
37  *    in recognizing a new string for a significant amount of time risks the
38  *    release of another string which only compounds the initial problem.
39  *
40  *****************************************************************************/
41 /*
42  * Strings supported by the _OSI predefined control method (which is
43  * implemented internally within this module.)
44  *
45  * March 2009: Removed "Linux" as this host no longer wants to respond true
46  * for this string. Basically, the only safe OS strings are windows-related
47  * and in many or most cases represent the only test path within the
48  * BIOS-provided ASL code.
49  *
50  * The last element of each entry is used to track the newest version of
51  * Windows that the BIOS has requested.
52  */
53 static struct acpi_interface_info acpi_default_supported_interfaces[] = {
54 	/* Operating System Vendor Strings */
55 
56 	{"Windows 2000", NULL, 0, ACPI_OSI_WIN_2000},	/* Windows 2000 */
57 	{"Windows 2001", NULL, 0, ACPI_OSI_WIN_XP},	/* Windows XP */
58 	{"Windows 2001 SP1", NULL, 0, ACPI_OSI_WIN_XP_SP1},	/* Windows XP SP1 */
59 	{"Windows 2001.1", NULL, 0, ACPI_OSI_WINSRV_2003},	/* Windows Server 2003 */
60 	{"Windows 2001 SP2", NULL, 0, ACPI_OSI_WIN_XP_SP2},	/* Windows XP SP2 */
61 	{"Windows 2001.1 SP1", NULL, 0, ACPI_OSI_WINSRV_2003_SP1},	/* Windows Server 2003 SP1 - Added 03/2006 */
62 	{"Windows 2006", NULL, 0, ACPI_OSI_WIN_VISTA},	/* Windows vista - Added 03/2006 */
63 	{"Windows 2006.1", NULL, 0, ACPI_OSI_WINSRV_2008},	/* Windows Server 2008 - Added 09/2009 */
64 	{"Windows 2006 SP1", NULL, 0, ACPI_OSI_WIN_VISTA_SP1},	/* Windows Vista SP1 - Added 09/2009 */
65 	{"Windows 2006 SP2", NULL, 0, ACPI_OSI_WIN_VISTA_SP2},	/* Windows Vista SP2 - Added 09/2010 */
66 	{"Windows 2009", NULL, 0, ACPI_OSI_WIN_7},	/* Windows 7 and Server 2008 R2 - Added 09/2009 */
67 	{"Windows 2012", NULL, 0, ACPI_OSI_WIN_8},	/* Windows 8 and Server 2012 - Added 08/2012 */
68 	{"Windows 2013", NULL, 0, ACPI_OSI_WIN_8_1},	/* Windows 8.1 and Server 2012 R2 - Added 01/2014 */
69 	{"Windows 2015", NULL, 0, ACPI_OSI_WIN_10},	/* Windows 10 - Added 03/2015 */
70 	{"Windows 2016", NULL, 0, ACPI_OSI_WIN_10_RS1},	/* Windows 10 version 1607 - Added 12/2017 */
71 	{"Windows 2017", NULL, 0, ACPI_OSI_WIN_10_RS2},	/* Windows 10 version 1703 - Added 12/2017 */
72 	{"Windows 2017.2", NULL, 0, ACPI_OSI_WIN_10_RS3},	/* Windows 10 version 1709 - Added 02/2018 */
73 	{"Windows 2018", NULL, 0, ACPI_OSI_WIN_10_RS4},	/* Windows 10 version 1803 - Added 11/2018 */
74 	{"Windows 2018.2", NULL, 0, ACPI_OSI_WIN_10_RS5},	/* Windows 10 version 1809 - Added 11/2018 */
75 	{"Windows 2019", NULL, 0, ACPI_OSI_WIN_10_19H1},	/* Windows 10 version 1903 - Added 08/2019 */
76 	{"Windows 2020", NULL, 0, ACPI_OSI_WIN_10_20H1},	/* Windows 10 version 2004 - Added 08/2021 */
77 	{"Windows 2021", NULL, 0, ACPI_OSI_WIN_11},	/* Windows 11 - Added 01/2022 */
78 	{"Windows 2022", NULL, 0, ACPI_OSI_WIN_11_22H2},	/* Windows 11 version 22H2 - Added 04/2024 */
79 
80 	/* Feature Group Strings */
81 
82 	{"Extended Address Space Descriptor", NULL, ACPI_OSI_FEATURE, 0},
83 
84 	/*
85 	 * All "optional" feature group strings (features that are implemented
86 	 * by the host) should be dynamically modified to VALID by the host via
87 	 * acpi_install_interface or acpi_update_interfaces. Such optional feature
88 	 * group strings are set as INVALID by default here.
89 	 */
90 
91 	{"Module Device", NULL, ACPI_OSI_OPTIONAL_FEATURE, 0},
92 	{"Processor Device", NULL, ACPI_OSI_OPTIONAL_FEATURE, 0},
93 	{"3.0 Thermal Model", NULL, ACPI_OSI_OPTIONAL_FEATURE, 0},
94 	{"3.0 _SCP Extensions", NULL, ACPI_OSI_OPTIONAL_FEATURE, 0},
95 	{"Processor Aggregator Device", NULL, ACPI_OSI_OPTIONAL_FEATURE, 0}
96 };
97 
98 /*******************************************************************************
99  *
100  * FUNCTION:    acpi_ut_initialize_interfaces
101  *
102  * PARAMETERS:  None
103  *
104  * RETURN:      Status
105  *
106  * DESCRIPTION: Initialize the global _OSI supported interfaces list
107  *
108  ******************************************************************************/
109 
acpi_ut_initialize_interfaces(void)110 acpi_status acpi_ut_initialize_interfaces(void)
111 {
112 	acpi_status status;
113 	u32 i;
114 
115 	status = acpi_os_acquire_mutex(acpi_gbl_osi_mutex, ACPI_WAIT_FOREVER);
116 	if (ACPI_FAILURE(status)) {
117 		return (status);
118 	}
119 
120 	acpi_gbl_supported_interfaces = acpi_default_supported_interfaces;
121 
122 	/* Link the static list of supported interfaces */
123 
124 	for (i = 0;
125 	     i < (ACPI_ARRAY_LENGTH(acpi_default_supported_interfaces) - 1);
126 	     i++) {
127 		acpi_default_supported_interfaces[i].next =
128 		    &acpi_default_supported_interfaces[(acpi_size)i + 1];
129 	}
130 
131 	acpi_os_release_mutex(acpi_gbl_osi_mutex);
132 	return (AE_OK);
133 }
134 
135 /*******************************************************************************
136  *
137  * FUNCTION:    acpi_ut_interface_terminate
138  *
139  * PARAMETERS:  None
140  *
141  * RETURN:      Status
142  *
143  * DESCRIPTION: Delete all interfaces in the global list. Sets
144  *              acpi_gbl_supported_interfaces to NULL.
145  *
146  ******************************************************************************/
147 
acpi_ut_interface_terminate(void)148 acpi_status acpi_ut_interface_terminate(void)
149 {
150 	acpi_status status;
151 	struct acpi_interface_info *next_interface;
152 
153 	status = acpi_os_acquire_mutex(acpi_gbl_osi_mutex, ACPI_WAIT_FOREVER);
154 	if (ACPI_FAILURE(status)) {
155 		return (status);
156 	}
157 
158 	next_interface = acpi_gbl_supported_interfaces;
159 	while (next_interface) {
160 		acpi_gbl_supported_interfaces = next_interface->next;
161 
162 		if (next_interface->flags & ACPI_OSI_DYNAMIC) {
163 
164 			/* Only interfaces added at runtime can be freed */
165 
166 			ACPI_FREE(next_interface->name);
167 			ACPI_FREE(next_interface);
168 		} else {
169 			/* Interface is in static list. Reset it to invalid or valid. */
170 
171 			if (next_interface->flags & ACPI_OSI_DEFAULT_INVALID) {
172 				next_interface->flags |= ACPI_OSI_INVALID;
173 			} else {
174 				next_interface->flags &= ~ACPI_OSI_INVALID;
175 			}
176 		}
177 
178 		next_interface = acpi_gbl_supported_interfaces;
179 	}
180 
181 	acpi_os_release_mutex(acpi_gbl_osi_mutex);
182 	return (AE_OK);
183 }
184 
185 /*******************************************************************************
186  *
187  * FUNCTION:    acpi_ut_install_interface
188  *
189  * PARAMETERS:  interface_name      - The interface to install
190  *
191  * RETURN:      Status
192  *
193  * DESCRIPTION: Install the interface into the global interface list.
194  *              Caller MUST hold acpi_gbl_osi_mutex
195  *
196  ******************************************************************************/
197 
acpi_ut_install_interface(acpi_string interface_name)198 acpi_status acpi_ut_install_interface(acpi_string interface_name)
199 {
200 	struct acpi_interface_info *interface_info;
201 
202 	/* Allocate info block and space for the name string */
203 
204 	interface_info =
205 	    ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_interface_info));
206 	if (!interface_info) {
207 		return (AE_NO_MEMORY);
208 	}
209 
210 	interface_info->name = ACPI_ALLOCATE_ZEROED(strlen(interface_name) + 1);
211 	if (!interface_info->name) {
212 		ACPI_FREE(interface_info);
213 		return (AE_NO_MEMORY);
214 	}
215 
216 	/* Initialize new info and insert at the head of the global list */
217 
218 	strcpy(interface_info->name, interface_name);
219 	interface_info->flags = ACPI_OSI_DYNAMIC;
220 	interface_info->next = acpi_gbl_supported_interfaces;
221 
222 	acpi_gbl_supported_interfaces = interface_info;
223 	return (AE_OK);
224 }
225 
226 /*******************************************************************************
227  *
228  * FUNCTION:    acpi_ut_remove_interface
229  *
230  * PARAMETERS:  interface_name      - The interface to remove
231  *
232  * RETURN:      Status
233  *
234  * DESCRIPTION: Remove the interface from the global interface list.
235  *              Caller MUST hold acpi_gbl_osi_mutex
236  *
237  ******************************************************************************/
238 
acpi_ut_remove_interface(acpi_string interface_name)239 acpi_status acpi_ut_remove_interface(acpi_string interface_name)
240 {
241 	struct acpi_interface_info *previous_interface;
242 	struct acpi_interface_info *next_interface;
243 
244 	previous_interface = next_interface = acpi_gbl_supported_interfaces;
245 	while (next_interface) {
246 		if (!strcmp(interface_name, next_interface->name)) {
247 			/*
248 			 * Found: name is in either the static list
249 			 * or was added at runtime
250 			 */
251 			if (next_interface->flags & ACPI_OSI_DYNAMIC) {
252 
253 				/* Interface was added dynamically, remove and free it */
254 
255 				if (previous_interface == next_interface) {
256 					acpi_gbl_supported_interfaces =
257 					    next_interface->next;
258 				} else {
259 					previous_interface->next =
260 					    next_interface->next;
261 				}
262 
263 				ACPI_FREE(next_interface->name);
264 				ACPI_FREE(next_interface);
265 			} else {
266 				/*
267 				 * Interface is in static list. If marked invalid, then
268 				 * it does not actually exist. Else, mark it invalid.
269 				 */
270 				if (next_interface->flags & ACPI_OSI_INVALID) {
271 					return (AE_NOT_EXIST);
272 				}
273 
274 				next_interface->flags |= ACPI_OSI_INVALID;
275 			}
276 
277 			return (AE_OK);
278 		}
279 
280 		previous_interface = next_interface;
281 		next_interface = next_interface->next;
282 	}
283 
284 	/* Interface was not found */
285 
286 	return (AE_NOT_EXIST);
287 }
288 
289 /*******************************************************************************
290  *
291  * FUNCTION:    acpi_ut_update_interfaces
292  *
293  * PARAMETERS:  action              - Actions to be performed during the
294  *                                    update
295  *
296  * RETURN:      Status
297  *
298  * DESCRIPTION: Update _OSI interface strings, disabling or enabling OS vendor
299  *              strings or/and feature group strings.
300  *              Caller MUST hold acpi_gbl_osi_mutex
301  *
302  ******************************************************************************/
303 
acpi_ut_update_interfaces(u8 action)304 acpi_status acpi_ut_update_interfaces(u8 action)
305 {
306 	struct acpi_interface_info *next_interface;
307 
308 	next_interface = acpi_gbl_supported_interfaces;
309 	while (next_interface) {
310 		if (((next_interface->flags & ACPI_OSI_FEATURE) &&
311 		     (action & ACPI_FEATURE_STRINGS)) ||
312 		    (!(next_interface->flags & ACPI_OSI_FEATURE) &&
313 		     (action & ACPI_VENDOR_STRINGS))) {
314 			if (action & ACPI_DISABLE_INTERFACES) {
315 
316 				/* Mark the interfaces as invalid */
317 
318 				next_interface->flags |= ACPI_OSI_INVALID;
319 			} else {
320 				/* Mark the interfaces as valid */
321 
322 				next_interface->flags &= ~ACPI_OSI_INVALID;
323 			}
324 		}
325 
326 		next_interface = next_interface->next;
327 	}
328 
329 	return (AE_OK);
330 }
331 
332 /*******************************************************************************
333  *
334  * FUNCTION:    acpi_ut_get_interface
335  *
336  * PARAMETERS:  interface_name      - The interface to find
337  *
338  * RETURN:      struct acpi_interface_info if found. NULL if not found.
339  *
340  * DESCRIPTION: Search for the specified interface name in the global list.
341  *              Caller MUST hold acpi_gbl_osi_mutex
342  *
343  ******************************************************************************/
344 
acpi_ut_get_interface(acpi_string interface_name)345 struct acpi_interface_info *acpi_ut_get_interface(acpi_string interface_name)
346 {
347 	struct acpi_interface_info *next_interface;
348 
349 	next_interface = acpi_gbl_supported_interfaces;
350 	while (next_interface) {
351 		if (!strcmp(interface_name, next_interface->name)) {
352 			return (next_interface);
353 		}
354 
355 		next_interface = next_interface->next;
356 	}
357 
358 	return (NULL);
359 }
360 
361 /*******************************************************************************
362  *
363  * FUNCTION:    acpi_ut_osi_implementation
364  *
365  * PARAMETERS:  walk_state          - Current walk state
366  *
367  * RETURN:      Status
368  *              Integer: TRUE (0) if input string is matched
369  *                       FALSE (-1) if string is not matched
370  *
371  * DESCRIPTION: Implementation of the _OSI predefined control method. When
372  *              an invocation of _OSI is encountered in the system AML,
373  *              control is transferred to this function.
374  *
375  * (August 2016)
376  * Note:  _OSI is now defined to return "Ones" to indicate a match, for
377  * compatibility with other ACPI implementations. On a 32-bit DSDT, Ones
378  * is 0xFFFFFFFF. On a 64-bit DSDT, Ones is 0xFFFFFFFFFFFFFFFF
379  * (ACPI_UINT64_MAX).
380  *
381  * This function always returns ACPI_UINT64_MAX for TRUE, and later code
382  * will truncate this to 32 bits if necessary.
383  *
384  ******************************************************************************/
385 
acpi_ut_osi_implementation(struct acpi_walk_state * walk_state)386 acpi_status acpi_ut_osi_implementation(struct acpi_walk_state *walk_state)
387 {
388 	union acpi_operand_object *string_desc;
389 	union acpi_operand_object *return_desc;
390 	struct acpi_interface_info *interface_info;
391 	acpi_interface_handler interface_handler;
392 	acpi_status status;
393 	u64 return_value;
394 
395 	ACPI_FUNCTION_TRACE(ut_osi_implementation);
396 
397 	/* Validate the string input argument (from the AML caller) */
398 
399 	string_desc = walk_state->arguments[0].object;
400 	if (!string_desc || (string_desc->common.type != ACPI_TYPE_STRING)) {
401 		return_ACPI_STATUS(AE_TYPE);
402 	}
403 
404 	/* Create a return object */
405 
406 	return_desc = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
407 	if (!return_desc) {
408 		return_ACPI_STATUS(AE_NO_MEMORY);
409 	}
410 
411 	/* Default return value is 0, NOT SUPPORTED */
412 
413 	return_value = 0;
414 	status = acpi_os_acquire_mutex(acpi_gbl_osi_mutex, ACPI_WAIT_FOREVER);
415 	if (ACPI_FAILURE(status)) {
416 		acpi_ut_remove_reference(return_desc);
417 		return_ACPI_STATUS(status);
418 	}
419 
420 	/* Lookup the interface in the global _OSI list */
421 
422 	interface_info = acpi_ut_get_interface(string_desc->string.pointer);
423 	if (interface_info && !(interface_info->flags & ACPI_OSI_INVALID)) {
424 		/*
425 		 * The interface is supported.
426 		 * Update the osi_data if necessary. We keep track of the latest
427 		 * version of Windows that has been requested by the BIOS.
428 		 */
429 		if (interface_info->value > acpi_gbl_osi_data) {
430 			acpi_gbl_osi_data = interface_info->value;
431 		}
432 
433 		return_value = ACPI_UINT64_MAX;
434 	}
435 
436 	acpi_os_release_mutex(acpi_gbl_osi_mutex);
437 
438 	/*
439 	 * Invoke an optional _OSI interface handler. The host OS may wish
440 	 * to do some interface-specific handling. For example, warn about
441 	 * certain interfaces or override the true/false support value.
442 	 */
443 	interface_handler = acpi_gbl_interface_handler;
444 	if (interface_handler) {
445 		if (interface_handler
446 		    (string_desc->string.pointer, (u32)return_value)) {
447 			return_value = ACPI_UINT64_MAX;
448 		}
449 	}
450 
451 	ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
452 			      "ACPI: BIOS _OSI(\"%s\") is %ssupported\n",
453 			      string_desc->string.pointer,
454 			      return_value == 0 ? "not " : ""));
455 
456 	/* Complete the return object */
457 
458 	return_desc->integer.value = return_value;
459 	walk_state->return_desc = return_desc;
460 	return_ACPI_STATUS(AE_OK);
461 }
462