xref: /linux/drivers/acpi/bus.c (revision d107dc8c9c6a9f9e4bb213f5a6398fc5c33a00a9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
4  *
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  */
7 
8 #define pr_fmt(fmt) "ACPI: " fmt
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/ioport.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/sched.h>
16 #include <linux/pm.h>
17 #include <linux/device.h>
18 #include <linux/proc_fs.h>
19 #include <linux/acpi.h>
20 #include <linux/slab.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/workqueue.h>
23 #include <linux/reboot.h>
24 #include <linux/delay.h>
25 #ifdef CONFIG_X86
26 #include <asm/mpspec.h>
27 #include <linux/dmi.h>
28 #endif
29 #include <linux/acpi_viot.h>
30 #include <linux/pci.h>
31 #include <acpi/apei.h>
32 #include <linux/suspend.h>
33 #include <linux/prmt.h>
34 
35 #include "internal.h"
36 
37 struct acpi_device *acpi_root;
38 struct proc_dir_entry *acpi_root_dir;
39 EXPORT_SYMBOL(acpi_root_dir);
40 
41 #ifdef CONFIG_X86
42 #ifdef CONFIG_ACPI_CUSTOM_DSDT
set_copy_dsdt(const struct dmi_system_id * id)43 static inline int set_copy_dsdt(const struct dmi_system_id *id)
44 {
45 	return 0;
46 }
47 #else
set_copy_dsdt(const struct dmi_system_id * id)48 static int set_copy_dsdt(const struct dmi_system_id *id)
49 {
50 	pr_notice("%s detected - force copy of DSDT to local memory\n", id->ident);
51 	acpi_gbl_copy_dsdt_locally = 1;
52 	return 0;
53 }
54 #endif
55 
56 static const struct dmi_system_id dsdt_dmi_table[] __initconst = {
57 	/*
58 	 * Invoke DSDT corruption work-around on all Toshiba Satellite.
59 	 * https://bugzilla.kernel.org/show_bug.cgi?id=14679
60 	 */
61 	{
62 	 .callback = set_copy_dsdt,
63 	 .ident = "TOSHIBA Satellite",
64 	 .matches = {
65 		DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
66 		DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
67 		},
68 	},
69 	{}
70 };
71 #endif
72 
73 /* --------------------------------------------------------------------------
74                                 Device Management
75    -------------------------------------------------------------------------- */
76 
acpi_bus_get_status_handle(acpi_handle handle,unsigned long long * sta)77 acpi_status acpi_bus_get_status_handle(acpi_handle handle,
78 				       unsigned long long *sta)
79 {
80 	acpi_status status;
81 
82 	status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
83 	if (ACPI_SUCCESS(status))
84 		return AE_OK;
85 
86 	if (status == AE_NOT_FOUND) {
87 		*sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
88 		       ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING;
89 		return AE_OK;
90 	}
91 	return status;
92 }
93 EXPORT_SYMBOL_GPL(acpi_bus_get_status_handle);
94 
acpi_bus_get_status(struct acpi_device * device)95 int acpi_bus_get_status(struct acpi_device *device)
96 {
97 	acpi_status status;
98 	unsigned long long sta;
99 
100 	if (acpi_device_override_status(device, &sta)) {
101 		acpi_set_device_status(device, sta);
102 		return 0;
103 	}
104 
105 	/* Battery devices must have their deps met before calling _STA */
106 	if (acpi_device_is_battery(device) && device->dep_unmet) {
107 		acpi_set_device_status(device, 0);
108 		return 0;
109 	}
110 
111 	status = acpi_bus_get_status_handle(device->handle, &sta);
112 	if (ACPI_FAILURE(status))
113 		return -ENODEV;
114 
115 	if (!device->status.present && device->status.enabled) {
116 		pr_info(FW_BUG "Device [%s] status [%08x]: not present and enabled\n",
117 			device->pnp.bus_id, (u32)sta);
118 		device->status.enabled = 0;
119 		/*
120 		 * The status is clearly invalid, so clear the functional bit as
121 		 * well to avoid attempting to use the device.
122 		 */
123 		device->status.functional = 0;
124 	}
125 
126 	acpi_set_device_status(device, sta);
127 
128 	if (device->status.functional && !device->status.present) {
129 		pr_debug("Device [%s] status [%08x]: functional but not present\n",
130 			 device->pnp.bus_id, (u32)sta);
131 	}
132 
133 	pr_debug("Device [%s] status [%08x]\n", device->pnp.bus_id, (u32)sta);
134 	return 0;
135 }
136 EXPORT_SYMBOL(acpi_bus_get_status);
137 
acpi_bus_private_data_handler(acpi_handle handle,void * context)138 void acpi_bus_private_data_handler(acpi_handle handle,
139 				   void *context)
140 {
141 	return;
142 }
143 EXPORT_SYMBOL(acpi_bus_private_data_handler);
144 
acpi_bus_attach_private_data(acpi_handle handle,void * data)145 int acpi_bus_attach_private_data(acpi_handle handle, void *data)
146 {
147 	acpi_status status;
148 
149 	status = acpi_attach_data(handle,
150 			acpi_bus_private_data_handler, data);
151 	if (ACPI_FAILURE(status)) {
152 		acpi_handle_debug(handle, "Error attaching device data\n");
153 		return -ENODEV;
154 	}
155 
156 	return 0;
157 }
158 EXPORT_SYMBOL_GPL(acpi_bus_attach_private_data);
159 
acpi_bus_get_private_data(acpi_handle handle,void ** data)160 int acpi_bus_get_private_data(acpi_handle handle, void **data)
161 {
162 	acpi_status status;
163 
164 	if (!data)
165 		return -EINVAL;
166 
167 	status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
168 	if (ACPI_FAILURE(status)) {
169 		acpi_handle_debug(handle, "No context for object\n");
170 		return -ENODEV;
171 	}
172 
173 	return 0;
174 }
175 EXPORT_SYMBOL_GPL(acpi_bus_get_private_data);
176 
acpi_bus_detach_private_data(acpi_handle handle)177 void acpi_bus_detach_private_data(acpi_handle handle)
178 {
179 	acpi_detach_data(handle, acpi_bus_private_data_handler);
180 }
181 EXPORT_SYMBOL_GPL(acpi_bus_detach_private_data);
182 
acpi_dump_osc_data(acpi_handle handle,const guid_t * guid,int rev,struct acpi_buffer * cap)183 static void acpi_dump_osc_data(acpi_handle handle, const guid_t *guid, int rev,
184 			       struct acpi_buffer *cap)
185 {
186 	u32 *capbuf = cap->pointer;
187 	int i;
188 
189 	acpi_handle_debug(handle, "_OSC: UUID: %pUL, rev: %d\n", guid, rev);
190 	for (i = 0; i < cap->length / sizeof(u32); i++)
191 		acpi_handle_debug(handle, "_OSC: capabilities DWORD %i: [%08x]\n",
192 				  i, capbuf[i]);
193 }
194 
195 #define OSC_ERROR_MASK 	(OSC_REQUEST_ERROR | OSC_INVALID_UUID_ERROR | \
196 			 OSC_INVALID_REVISION_ERROR | \
197 			 OSC_CAPABILITIES_MASK_ERROR)
198 
acpi_eval_osc(acpi_handle handle,guid_t * guid,int rev,struct acpi_buffer * cap,union acpi_object in_params[at_least4],struct acpi_buffer * output)199 static int acpi_eval_osc(acpi_handle handle, guid_t *guid, int rev,
200 			 struct acpi_buffer *cap,
201 			 union acpi_object in_params[at_least 4],
202 			 struct acpi_buffer *output)
203 {
204 	struct acpi_object_list input;
205 	union acpi_object *out_obj;
206 	acpi_status status;
207 
208 	in_params[0].type = ACPI_TYPE_BUFFER;
209 	in_params[0].buffer.length = sizeof(*guid);
210 	in_params[0].buffer.pointer = (u8 *)guid;
211 	in_params[1].type = ACPI_TYPE_INTEGER;
212 	in_params[1].integer.value = rev;
213 	in_params[2].type = ACPI_TYPE_INTEGER;
214 	in_params[2].integer.value = cap->length / sizeof(u32);
215 	in_params[3].type = ACPI_TYPE_BUFFER;
216 	in_params[3].buffer.length = cap->length;
217 	in_params[3].buffer.pointer = cap->pointer;
218 	input.pointer = in_params;
219 	input.count = 4;
220 
221 	output->length = ACPI_ALLOCATE_BUFFER;
222 	output->pointer = NULL;
223 
224 	status = acpi_evaluate_object(handle, "_OSC", &input, output);
225 	if (ACPI_FAILURE(status) || !output->length)
226 		return -ENODATA;
227 
228 	out_obj = output->pointer;
229 	if (out_obj->type != ACPI_TYPE_BUFFER ||
230 	    out_obj->buffer.length != cap->length) {
231 		acpi_handle_debug(handle, "Invalid _OSC return buffer\n");
232 		acpi_dump_osc_data(handle, guid, rev, cap);
233 		ACPI_FREE(out_obj);
234 		return -ENODATA;
235 	}
236 
237 	return 0;
238 }
239 
acpi_osc_error_check(acpi_handle handle,guid_t * guid,int rev,struct acpi_buffer * cap,u32 * retbuf)240 static bool acpi_osc_error_check(acpi_handle handle, guid_t *guid, int rev,
241 				 struct acpi_buffer *cap, u32 *retbuf)
242 {
243 	/* Only take defined error bits into account. */
244 	u32 errors = retbuf[OSC_QUERY_DWORD] & OSC_ERROR_MASK;
245 	u32 *capbuf = cap->pointer;
246 	bool fail;
247 
248 	/*
249 	 * If OSC_QUERY_ENABLE is set, ignore the "capabilities masked"
250 	 * bit because it merely means that some features have not been
251 	 * acknowledged which is not unexpected.
252 	 */
253 	if (capbuf[OSC_QUERY_DWORD] & OSC_QUERY_ENABLE)
254 		errors &= ~OSC_CAPABILITIES_MASK_ERROR;
255 
256 	if (!errors)
257 		return false;
258 
259 	acpi_dump_osc_data(handle, guid, rev, cap);
260 	/*
261 	 * As a rule, fail only if OSC_QUERY_ENABLE is set because otherwise the
262 	 * acknowledged features need to be controlled.
263 	 */
264 	fail = !!(capbuf[OSC_QUERY_DWORD] & OSC_QUERY_ENABLE);
265 
266 	if (errors & OSC_REQUEST_ERROR)
267 		acpi_handle_debug(handle, "_OSC: request failed\n");
268 
269 	if (errors & OSC_INVALID_UUID_ERROR) {
270 		acpi_handle_debug(handle, "_OSC: invalid UUID\n");
271 		/*
272 		 * Always fail if this bit is set because it means that the
273 		 * request could not be processed.
274 		 */
275 		fail = true;
276 	}
277 
278 	if (errors & OSC_INVALID_REVISION_ERROR)
279 		acpi_handle_debug(handle, "_OSC: invalid revision\n");
280 
281 	if (errors & OSC_CAPABILITIES_MASK_ERROR)
282 		acpi_handle_debug(handle, "_OSC: capability bits masked\n");
283 
284 	return fail;
285 }
286 
acpi_run_osc(acpi_handle handle,struct acpi_osc_context * context)287 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
288 {
289 	union acpi_object in_params[4], *out_obj;
290 	struct acpi_buffer output;
291 	acpi_status status = AE_OK;
292 	guid_t guid;
293 	u32 *retbuf;
294 	int ret;
295 
296 	if (!context || !context->cap.pointer ||
297 	    context->cap.length < 2 * sizeof(u32) ||
298 	    guid_parse(context->uuid_str, &guid))
299 		return AE_BAD_PARAMETER;
300 
301 	ret = acpi_eval_osc(handle, &guid, context->rev, &context->cap,
302 			    in_params, &output);
303 	if (ret)
304 		return AE_ERROR;
305 
306 	out_obj = output.pointer;
307 	retbuf = (u32 *)out_obj->buffer.pointer;
308 
309 	if (acpi_osc_error_check(handle, &guid, context->rev, &context->cap, retbuf)) {
310 		status = AE_ERROR;
311 		goto out;
312 	}
313 
314 	context->ret.length = out_obj->buffer.length;
315 	context->ret.pointer = kmemdup(retbuf, context->ret.length, GFP_KERNEL);
316 	if (!context->ret.pointer) {
317 		status =  AE_NO_MEMORY;
318 		goto out;
319 	}
320 	status =  AE_OK;
321 
322 out:
323 	ACPI_FREE(out_obj);
324 	return status;
325 }
326 EXPORT_SYMBOL(acpi_run_osc);
327 
acpi_osc_handshake(acpi_handle handle,const char * uuid_str,int rev,u32 * capbuf,size_t bufsize)328 static int acpi_osc_handshake(acpi_handle handle, const char *uuid_str,
329 			      int rev, u32 *capbuf, size_t bufsize)
330 {
331 	union acpi_object in_params[4], *out_obj;
332 	struct acpi_object_list input;
333 	struct acpi_buffer cap = {
334 		.pointer = capbuf,
335 		.length = bufsize * sizeof(u32),
336 	};
337 	struct acpi_buffer output;
338 	u32 *retbuf, test;
339 	guid_t guid;
340 	int ret, i;
341 
342 	if (!capbuf || bufsize < 2 || guid_parse(uuid_str, &guid))
343 		return -EINVAL;
344 
345 	/* First evaluate _OSC with OSC_QUERY_ENABLE set. */
346 	capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
347 
348 	ret = acpi_eval_osc(handle, &guid, rev, &cap, in_params, &output);
349 	if (ret)
350 		return ret;
351 
352 	out_obj = output.pointer;
353 	retbuf = (u32 *)out_obj->buffer.pointer;
354 
355 	if (acpi_osc_error_check(handle, &guid, rev, &cap, retbuf)) {
356 		ret = -ENODATA;
357 		goto out;
358 	}
359 
360 	/*
361 	 * Clear the feature bits in the capabilities buffer that have not been
362 	 * acknowledged and clear the return buffer.
363 	 */
364 	for (i = OSC_QUERY_DWORD + 1, test = 0; i < bufsize; i++) {
365 		capbuf[i] &= retbuf[i];
366 		test |= capbuf[i];
367 		retbuf[i] = 0;
368 	}
369 	/*
370 	 * If none of the feature bits have been acknowledged, there's nothing
371 	 * more to do.  capbuf[] contains a feature mask of all zeros.
372 	 */
373 	if (!test)
374 		goto out;
375 
376 	retbuf[OSC_QUERY_DWORD] = 0;
377 	/*
378 	 * Now evaluate _OSC again (directly) with OSC_QUERY_ENABLE clear and
379 	 * the updated input and output buffers used before.  Since the feature
380 	 * bits that were clear in the return buffer from the previous _OSC
381 	 * evaluation are also clear in the capabilities buffer now, this _OSC
382 	 * evaluation is not expected to fail.
383 	 */
384 	capbuf[OSC_QUERY_DWORD] = 0;
385 	/* Reuse in_params[] populated by acpi_eval_osc(). */
386 	input.pointer = in_params;
387 	input.count = 4;
388 
389 	if (ACPI_FAILURE(acpi_evaluate_object(handle, "_OSC", &input, &output))) {
390 		ret = -ENODATA;
391 		goto out;
392 	}
393 
394 	/*
395 	 * Clear the feature bits in capbuf[] that have not been acknowledged.
396 	 * After that, capbuf[] contains the resultant feature mask.
397 	 */
398 	for (i = OSC_QUERY_DWORD + 1; i < bufsize; i++)
399 		capbuf[i] &= retbuf[i];
400 
401 	if (retbuf[OSC_QUERY_DWORD] & OSC_ERROR_MASK) {
402 		/*
403 		 * Complain about the unexpected errors and print diagnostic
404 		 * information related to them.
405 		 */
406 		acpi_handle_err(handle, "_OSC: errors while processing control request\n");
407 		acpi_handle_err(handle, "_OSC: some features may be missing\n");
408 		acpi_osc_error_check(handle, &guid, rev, &cap, retbuf);
409 	}
410 
411 out:
412 	ACPI_FREE(out_obj);
413 	return ret;
414 }
415 
416 bool osc_sb_apei_support_acked;
417 
418 /*
419  * ACPI 6.0 Section 8.4.4.2 Idle State Coordination
420  * OSPM supports platform coordinated low power idle(LPI) states
421  */
422 bool osc_pc_lpi_support_confirmed;
423 EXPORT_SYMBOL_GPL(osc_pc_lpi_support_confirmed);
424 
425 /*
426  * ACPI 6.2 Section 6.2.11.2 'Platform-Wide OSPM Capabilities':
427  *   Starting with ACPI Specification 6.2, all _CPC registers can be in
428  *   PCC, System Memory, System IO, or Functional Fixed Hardware address
429  *   spaces. OSPM support for this more flexible register space scheme is
430  *   indicated by the “Flexible Address Space for CPPC Registers” _OSC bit.
431  *
432  * Otherwise (cf ACPI 6.1, s8.4.7.1.1.X), _CPC registers must be in:
433  * - PCC or Functional Fixed Hardware address space if defined
434  * - SystemMemory address space (NULL register) if not defined
435  */
436 bool osc_cpc_flexible_adr_space_confirmed;
437 EXPORT_SYMBOL_GPL(osc_cpc_flexible_adr_space_confirmed);
438 
439 /*
440  * ACPI 6.4 Operating System Capabilities for USB.
441  */
442 bool osc_sb_native_usb4_support_confirmed;
443 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_support_confirmed);
444 
445 bool osc_sb_cppc2_support_acked;
446 
acpi_bus_osc_negotiate_platform_control(void)447 static void acpi_bus_osc_negotiate_platform_control(void)
448 {
449 	static const u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
450 	u32 capbuf[2], feature_mask;
451 	acpi_handle handle;
452 
453 	feature_mask = OSC_SB_PR3_SUPPORT | OSC_SB_HOTPLUG_OST_SUPPORT |
454 			OSC_SB_PCLPI_SUPPORT | OSC_SB_OVER_16_PSTATES_SUPPORT |
455 			OSC_SB_GED_SUPPORT | OSC_SB_IRQ_RESOURCE_SOURCE_SUPPORT;
456 
457 	if (IS_ENABLED(CONFIG_ARM64) || IS_ENABLED(CONFIG_X86))
458 		feature_mask |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
459 
460 	if (IS_ENABLED(CONFIG_ACPI_CPPC_LIB)) {
461 		feature_mask |= OSC_SB_CPC_SUPPORT | OSC_SB_CPCV2_SUPPORT |
462 				OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
463 		if (IS_ENABLED(CONFIG_SCHED_MC_PRIO))
464 			feature_mask |= OSC_SB_CPC_DIVERSE_HIGH_SUPPORT;
465 	}
466 
467 	if (IS_ENABLED(CONFIG_ACPI_PROCESSOR_AGGREGATOR))
468 		feature_mask |= OSC_SB_PAD_SUPPORT;
469 
470 	if (IS_ENABLED(CONFIG_ACPI_PROCESSOR))
471 		feature_mask |= OSC_SB_PPC_OST_SUPPORT;
472 
473 	if (IS_ENABLED(CONFIG_ACPI_THERMAL))
474 		feature_mask |= OSC_SB_FAST_THERMAL_SAMPLING_SUPPORT;
475 
476 	if (IS_ENABLED(CONFIG_ACPI_BATTERY))
477 		feature_mask |= OSC_SB_BATTERY_CHARGE_LIMITING_SUPPORT;
478 
479 	if (IS_ENABLED(CONFIG_ACPI_PRMT))
480 		feature_mask |= OSC_SB_PRM_SUPPORT;
481 
482 	if (IS_ENABLED(CONFIG_ACPI_FFH))
483 		feature_mask |= OSC_SB_FFH_OPR_SUPPORT;
484 
485 	if (IS_ENABLED(CONFIG_USB4))
486 		feature_mask |= OSC_SB_NATIVE_USB4_SUPPORT;
487 
488 	if (!ghes_disable)
489 		feature_mask |= OSC_SB_APEI_SUPPORT;
490 
491 	if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
492 		return;
493 
494 	capbuf[OSC_SUPPORT_DWORD] = feature_mask;
495 
496 	acpi_handle_info(handle, "platform _OSC: OS support mask [%08x]\n", feature_mask);
497 
498 	if (acpi_osc_handshake(handle, sb_uuid_str, 1, capbuf, ARRAY_SIZE(capbuf)))
499 		return;
500 
501 	feature_mask = capbuf[OSC_SUPPORT_DWORD];
502 
503 	acpi_handle_info(handle, "platform _OSC: OS control mask [%08x]\n", feature_mask);
504 
505 	osc_sb_cppc2_support_acked = feature_mask & OSC_SB_CPCV2_SUPPORT;
506 	osc_sb_apei_support_acked = feature_mask & OSC_SB_APEI_SUPPORT;
507 	osc_pc_lpi_support_confirmed = feature_mask & OSC_SB_PCLPI_SUPPORT;
508 	osc_sb_native_usb4_support_confirmed = feature_mask & OSC_SB_NATIVE_USB4_SUPPORT;
509 	osc_cpc_flexible_adr_space_confirmed = feature_mask & OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
510 }
511 
512 /*
513  * Native control of USB4 capabilities. If any of the tunneling bits is
514  * set it means OS is in control and we use software based connection
515  * manager.
516  */
517 u32 osc_sb_native_usb4_control;
518 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control);
519 
acpi_bus_decode_usb_osc(const char * msg,u32 bits)520 static void acpi_bus_decode_usb_osc(const char *msg, u32 bits)
521 {
522 	pr_info("%s USB3%c DisplayPort%c PCIe%c XDomain%c\n", msg,
523 	       (bits & OSC_USB_USB3_TUNNELING) ? '+' : '-',
524 	       (bits & OSC_USB_DP_TUNNELING) ? '+' : '-',
525 	       (bits & OSC_USB_PCIE_TUNNELING) ? '+' : '-',
526 	       (bits & OSC_USB_XDOMAIN) ? '+' : '-');
527 }
528 
acpi_bus_osc_negotiate_usb_control(void)529 static void acpi_bus_osc_negotiate_usb_control(void)
530 {
531 	static const u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A";
532 	u32 capbuf[3], control;
533 	acpi_handle handle;
534 
535 	if (!osc_sb_native_usb4_support_confirmed)
536 		return;
537 
538 	if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
539 		return;
540 
541 	control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING |
542 		  OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN;
543 
544 	capbuf[OSC_SUPPORT_DWORD] = 0;
545 	capbuf[OSC_CONTROL_DWORD] = control;
546 
547 	if (acpi_osc_handshake(handle, sb_usb_uuid_str, 1, capbuf, ARRAY_SIZE(capbuf)))
548 		return;
549 
550 	osc_sb_native_usb4_control = capbuf[OSC_CONTROL_DWORD];
551 
552 	acpi_bus_decode_usb_osc("USB4 _OSC: OS supports", control);
553 	acpi_bus_decode_usb_osc("USB4 _OSC: OS controls", osc_sb_native_usb4_control);
554 }
555 
556 /* --------------------------------------------------------------------------
557                              Notification Handling
558    -------------------------------------------------------------------------- */
559 
560 /**
561  * acpi_bus_notify - Global system-level (0x00-0x7F) notifications handler
562  * @handle: Target ACPI object.
563  * @type: Notification type.
564  * @data: Ignored.
565  *
566  * This only handles notifications related to device hotplug.
567  */
acpi_bus_notify(acpi_handle handle,u32 type,void * data)568 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
569 {
570 	struct acpi_device *adev;
571 
572 	switch (type) {
573 	case ACPI_NOTIFY_BUS_CHECK:
574 		acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
575 		break;
576 
577 	case ACPI_NOTIFY_DEVICE_CHECK:
578 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
579 		break;
580 
581 	case ACPI_NOTIFY_DEVICE_WAKE:
582 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n");
583 		return;
584 
585 	case ACPI_NOTIFY_EJECT_REQUEST:
586 		acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
587 		break;
588 
589 	case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
590 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n");
591 		/* TBD: Exactly what does 'light' mean? */
592 		return;
593 
594 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
595 		acpi_handle_err(handle, "Device cannot be configured due "
596 				"to a frequency mismatch\n");
597 		return;
598 
599 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
600 		acpi_handle_err(handle, "Device cannot be configured due "
601 				"to a bus mode mismatch\n");
602 		return;
603 
604 	case ACPI_NOTIFY_POWER_FAULT:
605 		acpi_handle_err(handle, "Device has suffered a power fault\n");
606 		return;
607 
608 	default:
609 		acpi_handle_debug(handle, "Unknown event type 0x%x\n", type);
610 		return;
611 	}
612 
613 	adev = acpi_get_acpi_dev(handle);
614 
615 	if (adev && ACPI_SUCCESS(acpi_hotplug_schedule(adev, type)))
616 		return;
617 
618 	acpi_put_acpi_dev(adev);
619 
620 	acpi_evaluate_ost(handle, type, ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
621 }
622 
acpi_notify_device(acpi_handle handle,u32 event,void * data)623 static void acpi_notify_device(acpi_handle handle, u32 event, void *data)
624 {
625 	struct acpi_device *device = data;
626 	struct acpi_driver *acpi_drv = to_acpi_driver(device->dev.driver);
627 
628 	acpi_drv->ops.notify(device, event);
629 }
630 
acpi_device_install_notify_handler(struct acpi_device * device,struct acpi_driver * acpi_drv)631 static int acpi_device_install_notify_handler(struct acpi_device *device,
632 					      struct acpi_driver *acpi_drv)
633 {
634 	u32 type = acpi_drv->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS ?
635 				ACPI_ALL_NOTIFY : ACPI_DEVICE_NOTIFY;
636 	acpi_status status;
637 
638 	status = acpi_install_notify_handler(device->handle, type,
639 					     acpi_notify_device, device);
640 	if (ACPI_FAILURE(status))
641 		return -EINVAL;
642 
643 	return 0;
644 }
645 
acpi_device_remove_notify_handler(struct acpi_device * device,struct acpi_driver * acpi_drv)646 static void acpi_device_remove_notify_handler(struct acpi_device *device,
647 					      struct acpi_driver *acpi_drv)
648 {
649 	u32 type = acpi_drv->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS ?
650 				ACPI_ALL_NOTIFY : ACPI_DEVICE_NOTIFY;
651 
652 	acpi_remove_notify_handler(device->handle, type,
653 				   acpi_notify_device);
654 
655 	acpi_os_wait_events_complete();
656 }
657 
acpi_dev_install_notify_handler(struct acpi_device * adev,u32 handler_type,acpi_notify_handler handler,void * context)658 int acpi_dev_install_notify_handler(struct acpi_device *adev,
659 				    u32 handler_type,
660 				    acpi_notify_handler handler, void *context)
661 {
662 	acpi_status status;
663 
664 	status = acpi_install_notify_handler(adev->handle, handler_type,
665 					     handler, context);
666 	if (ACPI_FAILURE(status))
667 		return -ENODEV;
668 
669 	return 0;
670 }
671 EXPORT_SYMBOL_GPL(acpi_dev_install_notify_handler);
672 
acpi_dev_remove_notify_handler(struct acpi_device * adev,u32 handler_type,acpi_notify_handler handler)673 void acpi_dev_remove_notify_handler(struct acpi_device *adev,
674 				    u32 handler_type,
675 				    acpi_notify_handler handler)
676 {
677 	acpi_remove_notify_handler(adev->handle, handler_type, handler);
678 	acpi_os_wait_events_complete();
679 }
680 EXPORT_SYMBOL_GPL(acpi_dev_remove_notify_handler);
681 
682 /* Handle events targeting \_SB device (at present only graceful shutdown) */
683 
684 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
685 #define ACPI_SB_INDICATE_INTERVAL	10000
686 
sb_notify_work(struct work_struct * dummy)687 static void sb_notify_work(struct work_struct *dummy)
688 {
689 	acpi_handle sb_handle;
690 
691 	orderly_poweroff(true);
692 
693 	/*
694 	 * After initiating graceful shutdown, the ACPI spec requires OSPM
695 	 * to evaluate _OST method once every 10seconds to indicate that
696 	 * the shutdown is in progress
697 	 */
698 	acpi_get_handle(NULL, "\\_SB", &sb_handle);
699 	while (1) {
700 		pr_info("Graceful shutdown in progress.\n");
701 		acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN,
702 				ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL);
703 		msleep(ACPI_SB_INDICATE_INTERVAL);
704 	}
705 }
706 
acpi_sb_notify(acpi_handle handle,u32 event,void * data)707 static void acpi_sb_notify(acpi_handle handle, u32 event, void *data)
708 {
709 	static DECLARE_WORK(acpi_sb_work, sb_notify_work);
710 
711 	if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) {
712 		if (!work_busy(&acpi_sb_work))
713 			schedule_work(&acpi_sb_work);
714 	} else {
715 		pr_warn("event %x is not supported by \\_SB device\n", event);
716 	}
717 }
718 
acpi_setup_sb_notify_handler(void)719 static int __init acpi_setup_sb_notify_handler(void)
720 {
721 	acpi_handle sb_handle;
722 
723 	if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle)))
724 		return -ENXIO;
725 
726 	if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY,
727 						acpi_sb_notify, NULL)))
728 		return -EINVAL;
729 
730 	return 0;
731 }
732 
733 /* --------------------------------------------------------------------------
734                              Device Matching
735    -------------------------------------------------------------------------- */
736 
737 /**
738  * acpi_get_first_physical_node - Get first physical node of an ACPI device
739  * @adev:	ACPI device in question
740  *
741  * Return: First physical node of ACPI device @adev
742  */
acpi_get_first_physical_node(struct acpi_device * adev)743 struct device *acpi_get_first_physical_node(struct acpi_device *adev)
744 {
745 	struct mutex *physical_node_lock = &adev->physical_node_lock;
746 	struct device *phys_dev;
747 
748 	mutex_lock(physical_node_lock);
749 	if (list_empty(&adev->physical_node_list)) {
750 		phys_dev = NULL;
751 	} else {
752 		const struct acpi_device_physical_node *node;
753 
754 		node = list_first_entry(&adev->physical_node_list,
755 					struct acpi_device_physical_node, node);
756 
757 		phys_dev = node->dev;
758 	}
759 	mutex_unlock(physical_node_lock);
760 	return phys_dev;
761 }
762 EXPORT_SYMBOL_GPL(acpi_get_first_physical_node);
763 
acpi_primary_dev_companion(struct acpi_device * adev,const struct device * dev)764 static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
765 						      const struct device *dev)
766 {
767 	const struct device *phys_dev = acpi_get_first_physical_node(adev);
768 
769 	return phys_dev && phys_dev == dev ? adev : NULL;
770 }
771 
772 /**
773  * acpi_device_is_first_physical_node - Is given dev first physical node
774  * @adev: ACPI companion device
775  * @dev: Physical device to check
776  *
777  * Function checks if given @dev is the first physical devices attached to
778  * the ACPI companion device. This distinction is needed in some cases
779  * where the same companion device is shared between many physical devices.
780  *
781  * Note that the caller have to provide valid @adev pointer.
782  */
acpi_device_is_first_physical_node(struct acpi_device * adev,const struct device * dev)783 bool acpi_device_is_first_physical_node(struct acpi_device *adev,
784 					const struct device *dev)
785 {
786 	return !!acpi_primary_dev_companion(adev, dev);
787 }
788 
789 /*
790  * acpi_companion_match() - Can we match via ACPI companion device
791  * @dev: Device in question
792  *
793  * Check if the given device has an ACPI companion and if that companion has
794  * a valid list of PNP IDs, and if the device is the first (primary) physical
795  * device associated with it.  Return the companion pointer if that's the case
796  * or NULL otherwise.
797  *
798  * If multiple physical devices are attached to a single ACPI companion, we need
799  * to be careful.  The usage scenario for this kind of relationship is that all
800  * of the physical devices in question use resources provided by the ACPI
801  * companion.  A typical case is an MFD device where all the sub-devices share
802  * the parent's ACPI companion.  In such cases we can only allow the primary
803  * (first) physical device to be matched with the help of the companion's PNP
804  * IDs.
805  *
806  * Additional physical devices sharing the ACPI companion can still use
807  * resources available from it but they will be matched normally using functions
808  * provided by their bus types (and analogously for their modalias).
809  */
acpi_companion_match(const struct device * dev)810 const struct acpi_device *acpi_companion_match(const struct device *dev)
811 {
812 	struct acpi_device *adev;
813 
814 	adev = ACPI_COMPANION(dev);
815 	if (!adev)
816 		return NULL;
817 
818 	if (list_empty(&adev->pnp.ids))
819 		return NULL;
820 
821 	return acpi_primary_dev_companion(adev, dev);
822 }
823 
824 /**
825  * acpi_of_match_device - Match device object using the "compatible" property.
826  * @adev: ACPI device object to match.
827  * @of_match_table: List of device IDs to match against.
828  * @of_id: OF ID if matched
829  *
830  * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
831  * identifiers and a _DSD object with the "compatible" property, use that
832  * property to match against the given list of identifiers.
833  */
acpi_of_match_device(const struct acpi_device * adev,const struct of_device_id * of_match_table,const struct of_device_id ** of_id)834 static bool acpi_of_match_device(const struct acpi_device *adev,
835 				 const struct of_device_id *of_match_table,
836 				 const struct of_device_id **of_id)
837 {
838 	const union acpi_object *of_compatible, *obj;
839 	int i, nval;
840 
841 	if (!adev)
842 		return false;
843 
844 	of_compatible = adev->data.of_compatible;
845 	if (!of_match_table || !of_compatible)
846 		return false;
847 
848 	if (of_compatible->type == ACPI_TYPE_PACKAGE) {
849 		nval = of_compatible->package.count;
850 		obj = of_compatible->package.elements;
851 	} else { /* Must be ACPI_TYPE_STRING. */
852 		nval = 1;
853 		obj = of_compatible;
854 	}
855 	/* Now we can look for the driver DT compatible strings */
856 	for (i = 0; i < nval; i++, obj++) {
857 		const struct of_device_id *id;
858 
859 		for (id = of_match_table; id->compatible[0]; id++)
860 			if (!strcasecmp(obj->string.pointer, id->compatible)) {
861 				if (of_id)
862 					*of_id = id;
863 				return true;
864 			}
865 	}
866 
867 	return false;
868 }
869 
acpi_of_modalias(struct acpi_device * adev,char * modalias,size_t len)870 static bool acpi_of_modalias(struct acpi_device *adev,
871 			     char *modalias, size_t len)
872 {
873 	const union acpi_object *of_compatible;
874 	const union acpi_object *obj;
875 	const char *str, *chr;
876 
877 	of_compatible = adev->data.of_compatible;
878 	if (!of_compatible)
879 		return false;
880 
881 	if (of_compatible->type == ACPI_TYPE_PACKAGE)
882 		obj = of_compatible->package.elements;
883 	else /* Must be ACPI_TYPE_STRING. */
884 		obj = of_compatible;
885 
886 	str = obj->string.pointer;
887 	chr = strchr(str, ',');
888 	strscpy(modalias, chr ? chr + 1 : str, len);
889 
890 	return true;
891 }
892 
893 /**
894  * acpi_set_modalias - Set modalias using "compatible" property or supplied ID
895  * @adev:	ACPI device object to match
896  * @default_id:	ID string to use as default if no compatible string found
897  * @modalias:   Pointer to buffer that modalias value will be copied into
898  * @len:	Length of modalias buffer
899  *
900  * This is a counterpart of of_alias_from_compatible() for struct acpi_device
901  * objects. If there is a compatible string for @adev, it will be copied to
902  * @modalias with the vendor prefix stripped; otherwise, @default_id will be
903  * used.
904  */
acpi_set_modalias(struct acpi_device * adev,const char * default_id,char * modalias,size_t len)905 void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
906 		       char *modalias, size_t len)
907 {
908 	if (!acpi_of_modalias(adev, modalias, len))
909 		strscpy(modalias, default_id, len);
910 }
911 EXPORT_SYMBOL_GPL(acpi_set_modalias);
912 
__acpi_match_device_cls(const struct acpi_device_id * id,struct acpi_hardware_id * hwid)913 static bool __acpi_match_device_cls(const struct acpi_device_id *id,
914 				    struct acpi_hardware_id *hwid)
915 {
916 	int i, msk, byte_shift;
917 	char buf[3];
918 
919 	if (!id->cls)
920 		return false;
921 
922 	/* Apply class-code bitmask, before checking each class-code byte */
923 	for (i = 1; i <= 3; i++) {
924 		byte_shift = 8 * (3 - i);
925 		msk = (id->cls_msk >> byte_shift) & 0xFF;
926 		if (!msk)
927 			continue;
928 
929 		sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
930 		if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
931 			return false;
932 	}
933 	return true;
934 }
935 
__acpi_match_device(const struct acpi_device * device,const struct acpi_device_id * acpi_ids,const struct of_device_id * of_ids,const struct acpi_device_id ** acpi_id,const struct of_device_id ** of_id)936 static bool __acpi_match_device(const struct acpi_device *device,
937 				const struct acpi_device_id *acpi_ids,
938 				const struct of_device_id *of_ids,
939 				const struct acpi_device_id **acpi_id,
940 				const struct of_device_id **of_id)
941 {
942 	const struct acpi_device_id *id;
943 	struct acpi_hardware_id *hwid;
944 
945 	/*
946 	 * If the device is not present, it is unnecessary to load device
947 	 * driver for it.
948 	 */
949 	if (!device || !device->status.present)
950 		return false;
951 
952 	list_for_each_entry(hwid, &device->pnp.ids, list) {
953 		/* First, check the ACPI/PNP IDs provided by the caller. */
954 		if (acpi_ids) {
955 			for (id = acpi_ids; id->id[0] || id->cls; id++) {
956 				if (id->id[0] && !strcmp((char *)id->id, hwid->id))
957 					goto out_acpi_match;
958 				if (id->cls && __acpi_match_device_cls(id, hwid))
959 					goto out_acpi_match;
960 			}
961 		}
962 
963 		/*
964 		 * Next, check ACPI_DT_NAMESPACE_HID and try to match the
965 		 * "compatible" property if found.
966 		 */
967 		if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id))
968 			return acpi_of_match_device(device, of_ids, of_id);
969 	}
970 	return false;
971 
972 out_acpi_match:
973 	if (acpi_id)
974 		*acpi_id = id;
975 	return true;
976 }
977 
978 /**
979  * acpi_match_acpi_device - Match an ACPI device against a given list of ACPI IDs
980  * @ids: Array of struct acpi_device_id objects to match against.
981  * @adev: The ACPI device pointer to match.
982  *
983  * Match the ACPI device @adev against a given list of ACPI IDs @ids.
984  *
985  * Return:
986  * a pointer to the first matching ACPI ID on success or %NULL on failure.
987  */
acpi_match_acpi_device(const struct acpi_device_id * ids,const struct acpi_device * adev)988 const struct acpi_device_id *acpi_match_acpi_device(const struct acpi_device_id *ids,
989 						    const struct acpi_device *adev)
990 {
991 	const struct acpi_device_id *id = NULL;
992 
993 	__acpi_match_device(adev, ids, NULL, &id, NULL);
994 	return id;
995 }
996 EXPORT_SYMBOL_GPL(acpi_match_acpi_device);
997 
998 /**
999  * acpi_match_device - Match a struct device against a given list of ACPI IDs
1000  * @ids: Array of struct acpi_device_id object to match against.
1001  * @dev: The device structure to match.
1002  *
1003  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
1004  * object for that handle and use that object to match against a given list of
1005  * device IDs.
1006  *
1007  * Return a pointer to the first matching ID on success or %NULL on failure.
1008  */
acpi_match_device(const struct acpi_device_id * ids,const struct device * dev)1009 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
1010 					       const struct device *dev)
1011 {
1012 	return acpi_match_acpi_device(ids, acpi_companion_match(dev));
1013 }
1014 EXPORT_SYMBOL_GPL(acpi_match_device);
1015 
acpi_device_get_match_data(const struct device * dev)1016 const void *acpi_device_get_match_data(const struct device *dev)
1017 {
1018 	const struct acpi_device_id *acpi_ids = dev->driver->acpi_match_table;
1019 	const struct of_device_id *of_ids = dev->driver->of_match_table;
1020 	const struct acpi_device *adev = acpi_companion_match(dev);
1021 	const struct acpi_device_id *acpi_id = NULL;
1022 	const struct of_device_id *of_id = NULL;
1023 
1024 	if (!__acpi_match_device(adev, acpi_ids, of_ids, &acpi_id, &of_id))
1025 		return NULL;
1026 
1027 	if (acpi_id)
1028 		return (const void *)acpi_id->driver_data;
1029 
1030 	if (of_id)
1031 		return of_id->data;
1032 
1033 	return NULL;
1034 }
1035 EXPORT_SYMBOL_GPL(acpi_device_get_match_data);
1036 
acpi_match_device_ids(struct acpi_device * device,const struct acpi_device_id * ids)1037 int acpi_match_device_ids(struct acpi_device *device,
1038 			  const struct acpi_device_id *ids)
1039 {
1040 	return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT;
1041 }
1042 EXPORT_SYMBOL(acpi_match_device_ids);
1043 
acpi_driver_match_device(struct device * dev,const struct device_driver * drv)1044 bool acpi_driver_match_device(struct device *dev,
1045 			      const struct device_driver *drv)
1046 {
1047 	const struct acpi_device_id *acpi_ids = drv->acpi_match_table;
1048 	const struct of_device_id *of_ids = drv->of_match_table;
1049 
1050 	if (!acpi_ids)
1051 		return acpi_of_match_device(ACPI_COMPANION(dev), of_ids, NULL);
1052 
1053 	return __acpi_match_device(acpi_companion_match(dev), acpi_ids, of_ids, NULL, NULL);
1054 }
1055 EXPORT_SYMBOL_GPL(acpi_driver_match_device);
1056 
1057 /* --------------------------------------------------------------------------
1058                               ACPI Driver Management
1059    -------------------------------------------------------------------------- */
1060 
1061 /**
1062  * __acpi_bus_register_driver - register a driver with the ACPI bus
1063  * @driver: driver being registered
1064  * @owner: owning module/driver
1065  *
1066  * Registers a driver with the ACPI bus.  Searches the namespace for all
1067  * devices that match the driver's criteria and binds.  Returns zero for
1068  * success or a negative error status for failure.
1069  */
__acpi_bus_register_driver(struct acpi_driver * driver,struct module * owner)1070 int __acpi_bus_register_driver(struct acpi_driver *driver, struct module *owner)
1071 {
1072 	if (acpi_disabled)
1073 		return -ENODEV;
1074 	driver->drv.name = driver->name;
1075 	driver->drv.bus = &acpi_bus_type;
1076 	driver->drv.owner = owner;
1077 
1078 	return driver_register(&driver->drv);
1079 }
1080 
1081 EXPORT_SYMBOL(__acpi_bus_register_driver);
1082 
1083 /**
1084  * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1085  * @driver: driver to unregister
1086  *
1087  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
1088  * devices that match the driver's criteria and unbinds.
1089  */
acpi_bus_unregister_driver(struct acpi_driver * driver)1090 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1091 {
1092 	driver_unregister(&driver->drv);
1093 }
1094 
1095 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1096 
1097 /* --------------------------------------------------------------------------
1098                               ACPI Bus operations
1099    -------------------------------------------------------------------------- */
1100 
acpi_bus_match(struct device * dev,const struct device_driver * drv)1101 static int acpi_bus_match(struct device *dev, const struct device_driver *drv)
1102 {
1103 	struct acpi_device *acpi_dev = to_acpi_device(dev);
1104 	const struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1105 
1106 	return acpi_dev->flags.match_driver
1107 		&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
1108 }
1109 
acpi_device_uevent(const struct device * dev,struct kobj_uevent_env * env)1110 static int acpi_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
1111 {
1112 	return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
1113 }
1114 
acpi_device_probe(struct device * dev)1115 static int acpi_device_probe(struct device *dev)
1116 {
1117 	struct acpi_device *acpi_dev = to_acpi_device(dev);
1118 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1119 	int ret;
1120 
1121 	if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
1122 		return -EINVAL;
1123 
1124 	if (!acpi_drv->ops.add)
1125 		return -ENOSYS;
1126 
1127 	ret = acpi_drv->ops.add(acpi_dev);
1128 	if (ret) {
1129 		acpi_dev->driver_data = NULL;
1130 		return ret;
1131 	}
1132 
1133 	pr_debug("Driver [%s] successfully bound to device [%s]\n",
1134 		 acpi_drv->name, acpi_dev->pnp.bus_id);
1135 
1136 	if (acpi_drv->ops.notify) {
1137 		ret = acpi_device_install_notify_handler(acpi_dev, acpi_drv);
1138 		if (ret) {
1139 			if (acpi_drv->ops.remove)
1140 				acpi_drv->ops.remove(acpi_dev);
1141 
1142 			acpi_dev->driver_data = NULL;
1143 			return ret;
1144 		}
1145 	}
1146 
1147 	pr_debug("Found driver [%s] for device [%s]\n", acpi_drv->name,
1148 		 acpi_dev->pnp.bus_id);
1149 
1150 	get_device(dev);
1151 	return 0;
1152 }
1153 
acpi_device_remove(struct device * dev)1154 static void acpi_device_remove(struct device *dev)
1155 {
1156 	struct acpi_device *acpi_dev = to_acpi_device(dev);
1157 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1158 
1159 	if (acpi_drv->ops.notify)
1160 		acpi_device_remove_notify_handler(acpi_dev, acpi_drv);
1161 
1162 	if (acpi_drv->ops.remove)
1163 		acpi_drv->ops.remove(acpi_dev);
1164 
1165 	acpi_dev->driver_data = NULL;
1166 
1167 	put_device(dev);
1168 }
1169 
1170 const struct bus_type acpi_bus_type = {
1171 	.name		= "acpi",
1172 	.match		= acpi_bus_match,
1173 	.probe		= acpi_device_probe,
1174 	.remove		= acpi_device_remove,
1175 	.uevent		= acpi_device_uevent,
1176 };
1177 
acpi_bus_for_each_dev(int (* fn)(struct device *,void *),void * data)1178 int acpi_bus_for_each_dev(int (*fn)(struct device *, void *), void *data)
1179 {
1180 	return bus_for_each_dev(&acpi_bus_type, NULL, data, fn);
1181 }
1182 EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
1183 
1184 struct acpi_dev_walk_context {
1185 	int (*fn)(struct acpi_device *, void *);
1186 	void *data;
1187 };
1188 
acpi_dev_for_one_check(struct device * dev,void * context)1189 static int acpi_dev_for_one_check(struct device *dev, void *context)
1190 {
1191 	struct acpi_dev_walk_context *adwc = context;
1192 
1193 	if (dev->bus != &acpi_bus_type)
1194 		return 0;
1195 
1196 	return adwc->fn(to_acpi_device(dev), adwc->data);
1197 }
1198 EXPORT_SYMBOL_GPL(acpi_dev_for_each_child);
1199 
acpi_dev_for_each_child(struct acpi_device * adev,int (* fn)(struct acpi_device *,void *),void * data)1200 int acpi_dev_for_each_child(struct acpi_device *adev,
1201 			    int (*fn)(struct acpi_device *, void *), void *data)
1202 {
1203 	struct acpi_dev_walk_context adwc = {
1204 		.fn = fn,
1205 		.data = data,
1206 	};
1207 
1208 	return device_for_each_child(&adev->dev, &adwc, acpi_dev_for_one_check);
1209 }
1210 
acpi_dev_for_each_child_reverse(struct acpi_device * adev,int (* fn)(struct acpi_device *,void *),void * data)1211 int acpi_dev_for_each_child_reverse(struct acpi_device *adev,
1212 				    int (*fn)(struct acpi_device *, void *),
1213 				    void *data)
1214 {
1215 	struct acpi_dev_walk_context adwc = {
1216 		.fn = fn,
1217 		.data = data,
1218 	};
1219 
1220 	return device_for_each_child_reverse(&adev->dev, &adwc, acpi_dev_for_one_check);
1221 }
1222 
1223 /* --------------------------------------------------------------------------
1224                              Initialization/Cleanup
1225    -------------------------------------------------------------------------- */
1226 
acpi_bus_init_irq(void)1227 static int __init acpi_bus_init_irq(void)
1228 {
1229 	acpi_status status;
1230 	char *message = NULL;
1231 
1232 
1233 	/*
1234 	 * Let the system know what interrupt model we are using by
1235 	 * evaluating the \_PIC object, if exists.
1236 	 */
1237 
1238 	switch (acpi_irq_model) {
1239 	case ACPI_IRQ_MODEL_PIC:
1240 		message = "PIC";
1241 		break;
1242 	case ACPI_IRQ_MODEL_IOAPIC:
1243 		message = "IOAPIC";
1244 		break;
1245 	case ACPI_IRQ_MODEL_IOSAPIC:
1246 		message = "IOSAPIC";
1247 		break;
1248 	case ACPI_IRQ_MODEL_GIC:
1249 		message = "GIC";
1250 		break;
1251 	case ACPI_IRQ_MODEL_GIC_V5:
1252 		message = "GICv5";
1253 		break;
1254 	case ACPI_IRQ_MODEL_PLATFORM:
1255 		message = "platform specific model";
1256 		break;
1257 	case ACPI_IRQ_MODEL_LPIC:
1258 		message = "LPIC";
1259 		break;
1260 	case ACPI_IRQ_MODEL_RINTC:
1261 		message = "RINTC";
1262 		break;
1263 	default:
1264 		pr_info("Unknown interrupt routing model\n");
1265 		return -ENODEV;
1266 	}
1267 
1268 	pr_info("Using %s for interrupt routing\n", message);
1269 
1270 	status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
1271 	if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
1272 		pr_info("_PIC evaluation failed: %s\n", acpi_format_exception(status));
1273 		return -ENODEV;
1274 	}
1275 
1276 	return 0;
1277 }
1278 
1279 /**
1280  * acpi_early_init - Initialize ACPICA and populate the ACPI namespace.
1281  *
1282  * The ACPI tables are accessible after this, but the handling of events has not
1283  * been initialized and the global lock is not available yet, so AML should not
1284  * be executed at this point.
1285  *
1286  * Doing this before switching the EFI runtime services to virtual mode allows
1287  * the EfiBootServices memory to be freed slightly earlier on boot.
1288  */
acpi_early_init(void)1289 void __init acpi_early_init(void)
1290 {
1291 	acpi_status status;
1292 
1293 	if (acpi_disabled)
1294 		return;
1295 
1296 	pr_info("Core revision %08x\n", ACPI_CA_VERSION);
1297 
1298 	/* enable workarounds, unless strict ACPI spec. compliance */
1299 	if (!acpi_strict)
1300 		acpi_gbl_enable_interpreter_slack = TRUE;
1301 
1302 	acpi_permanent_mmap = true;
1303 
1304 #ifdef CONFIG_X86
1305 	/*
1306 	 * If the machine falls into the DMI check table,
1307 	 * DSDT will be copied to memory.
1308 	 * Note that calling dmi_check_system() here on other architectures
1309 	 * would not be OK because only x86 initializes dmi early enough.
1310 	 * Thankfully only x86 systems need such quirks for now.
1311 	 */
1312 	dmi_check_system(dsdt_dmi_table);
1313 #endif
1314 
1315 	status = acpi_reallocate_root_table();
1316 	if (ACPI_FAILURE(status)) {
1317 		pr_err("Unable to reallocate ACPI tables\n");
1318 		goto error0;
1319 	}
1320 
1321 	status = acpi_initialize_subsystem();
1322 	if (ACPI_FAILURE(status)) {
1323 		pr_err("Unable to initialize the ACPI Interpreter\n");
1324 		goto error0;
1325 	}
1326 
1327 #ifdef CONFIG_X86
1328 	if (!acpi_ioapic) {
1329 		/* compatible (0) means level (3) */
1330 		if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
1331 			acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
1332 			acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
1333 		}
1334 		/* Set PIC-mode SCI trigger type */
1335 		acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
1336 					 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
1337 	} else {
1338 		/*
1339 		 * now that acpi_gbl_FADT is initialized,
1340 		 * update it with result from INT_SRC_OVR parsing
1341 		 */
1342 		acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
1343 	}
1344 #endif
1345 	return;
1346 
1347  error0:
1348 	disable_acpi();
1349 }
1350 
1351 /**
1352  * acpi_subsystem_init - Finalize the early initialization of ACPI.
1353  *
1354  * Switch over the platform to the ACPI mode (if possible).
1355  *
1356  * Doing this too early is generally unsafe, but at the same time it needs to be
1357  * done before all things that really depend on ACPI.  The right spot appears to
1358  * be before finalizing the EFI initialization.
1359  */
acpi_subsystem_init(void)1360 void __init acpi_subsystem_init(void)
1361 {
1362 	acpi_status status;
1363 
1364 	if (acpi_disabled)
1365 		return;
1366 
1367 	status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
1368 	if (ACPI_FAILURE(status)) {
1369 		pr_err("Unable to enable ACPI\n");
1370 		disable_acpi();
1371 	} else {
1372 		/*
1373 		 * If the system is using ACPI then we can be reasonably
1374 		 * confident that any regulators are managed by the firmware
1375 		 * so tell the regulator core it has everything it needs to
1376 		 * know.
1377 		 */
1378 		regulator_has_full_constraints();
1379 	}
1380 }
1381 
acpi_bus_table_handler(u32 event,void * table,void * context)1382 static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context)
1383 {
1384 	if (event == ACPI_TABLE_EVENT_LOAD)
1385 		acpi_scan_table_notify();
1386 
1387 	return acpi_sysfs_table_handler(event, table, context);
1388 }
1389 
acpi_bus_init(void)1390 static int __init acpi_bus_init(void)
1391 {
1392 	int result;
1393 	acpi_status status;
1394 
1395 	acpi_os_initialize1();
1396 
1397 	status = acpi_load_tables();
1398 	if (ACPI_FAILURE(status)) {
1399 		pr_err("Unable to load the System Description Tables\n");
1400 		goto error1;
1401 	}
1402 
1403 	/*
1404 	 * ACPI 2.0 requires the EC driver to be loaded and work before the EC
1405 	 * device is found in the namespace.
1406 	 *
1407 	 * This is accomplished by looking for the ECDT table and getting the EC
1408 	 * parameters out of that.
1409 	 *
1410 	 * Do that before calling acpi_initialize_objects() which may trigger EC
1411 	 * address space accesses.
1412 	 */
1413 	acpi_ec_ecdt_probe();
1414 
1415 	status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
1416 	if (ACPI_FAILURE(status)) {
1417 		pr_err("Unable to start the ACPI Interpreter\n");
1418 		goto error1;
1419 	}
1420 
1421 	status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
1422 	if (ACPI_FAILURE(status)) {
1423 		pr_err("Unable to initialize ACPI objects\n");
1424 		goto error1;
1425 	}
1426 
1427 	/*
1428 	 * _OSC method may exist in module level code,
1429 	 * so it must be run after ACPI_FULL_INITIALIZATION
1430 	 */
1431 	acpi_bus_osc_negotiate_platform_control();
1432 	acpi_bus_osc_negotiate_usb_control();
1433 
1434 	/*
1435 	 * _PDC control method may load dynamic SSDT tables,
1436 	 * and we need to install the table handler before that.
1437 	 */
1438 	status = acpi_install_table_handler(acpi_bus_table_handler, NULL);
1439 
1440 	acpi_sysfs_init();
1441 
1442 	acpi_early_processor_control_setup();
1443 
1444 	/*
1445 	 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1446 	 * is necessary to enable it as early as possible.
1447 	 */
1448 	acpi_ec_dsdt_probe();
1449 
1450 	pr_info("Interpreter enabled\n");
1451 
1452 	/* Initialize sleep structures */
1453 	acpi_sleep_init();
1454 
1455 	/*
1456 	 * Get the system interrupt model and evaluate \_PIC.
1457 	 */
1458 	result = acpi_bus_init_irq();
1459 	if (result)
1460 		goto error1;
1461 
1462 	/*
1463 	 * Register for all standard device notifications.
1464 	 */
1465 	status =
1466 	    acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
1467 					&acpi_bus_notify, NULL);
1468 	if (ACPI_FAILURE(status)) {
1469 		pr_err("Unable to register for system notifications\n");
1470 		goto error1;
1471 	}
1472 
1473 	/*
1474 	 * Create the top ACPI proc directory
1475 	 */
1476 	acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
1477 
1478 	result = bus_register(&acpi_bus_type);
1479 	if (!result)
1480 		return 0;
1481 
1482 	/* Mimic structured exception handling */
1483       error1:
1484 	acpi_terminate();
1485 	return -ENODEV;
1486 }
1487 
1488 struct kobject *acpi_kobj;
1489 EXPORT_SYMBOL_GPL(acpi_kobj);
1490 
acpi_arch_init(void)1491 void __weak __init acpi_arch_init(void) { }
1492 
acpi_init(void)1493 static int __init acpi_init(void)
1494 {
1495 	int result;
1496 
1497 	if (acpi_disabled) {
1498 		pr_info("Interpreter disabled.\n");
1499 		return -ENODEV;
1500 	}
1501 
1502 	acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
1503 	if (!acpi_kobj) {
1504 		pr_err("Failed to register kobject\n");
1505 		return -ENOMEM;
1506 	}
1507 
1508 	init_prmt();
1509 	acpi_init_pcc();
1510 	result = acpi_bus_init();
1511 	if (result) {
1512 		kobject_put(acpi_kobj);
1513 		disable_acpi();
1514 		return result;
1515 	}
1516 	acpi_init_ffh();
1517 
1518 	pci_mmcfg_late_init();
1519 	acpi_viot_early_init();
1520 	acpi_hest_init();
1521 	acpi_ghes_init();
1522 	acpi_arch_init();
1523 	acpi_scan_init();
1524 	acpi_ec_init();
1525 	acpi_debugfs_init();
1526 	acpi_sleep_proc_init();
1527 	acpi_wakeup_device_init();
1528 	acpi_debugger_init();
1529 	acpi_setup_sb_notify_handler();
1530 	acpi_viot_init();
1531 	return 0;
1532 }
1533 
1534 subsys_initcall(acpi_init);
1535