xref: /linux/drivers/acpi/bus.c (revision 8669f2b6c96c89dab028b294fa069a672989afad)
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
43 static inline int set_copy_dsdt(const struct dmi_system_id *id)
44 {
45 	return 0;
46 }
47 #else
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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, errors;
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, test = 0; i < bufsize; i++) {
399 		test |= capbuf[i] & ~retbuf[i];
400 		capbuf[i] &= retbuf[i];
401 	}
402 
403 	errors = retbuf[OSC_QUERY_DWORD] & OSC_ERROR_MASK;
404 	/*
405 	 * Some platforms set OSC_CAPABILITIES_MASK_ERROR even though they
406 	 * acknowledge all of the requested features, so avoid complaining in
407 	 * those cases unless any other error bits are also set.
408 	 */
409 	if (errors && (test || errors != OSC_CAPABILITIES_MASK_ERROR)) {
410 		/*
411 		 * Complain about the unexpected errors and print diagnostic
412 		 * information related to them.
413 		 */
414 		acpi_handle_err(handle, "_OSC: errors while processing control request\n");
415 		acpi_handle_err(handle, "_OSC: some features may be missing\n");
416 		acpi_osc_error_check(handle, &guid, rev, &cap, retbuf);
417 	}
418 
419 out:
420 	ACPI_FREE(out_obj);
421 	return ret;
422 }
423 
424 bool osc_sb_apei_support_acked;
425 
426 /*
427  * ACPI 6.0 Section 8.4.4.2 Idle State Coordination
428  * OSPM supports platform coordinated low power idle(LPI) states
429  */
430 bool osc_pc_lpi_support_confirmed;
431 EXPORT_SYMBOL_GPL(osc_pc_lpi_support_confirmed);
432 
433 /*
434  * ACPI 6.2 Section 6.2.11.2 'Platform-Wide OSPM Capabilities':
435  *   Starting with ACPI Specification 6.2, all _CPC registers can be in
436  *   PCC, System Memory, System IO, or Functional Fixed Hardware address
437  *   spaces. OSPM support for this more flexible register space scheme is
438  *   indicated by the “Flexible Address Space for CPPC Registers” _OSC bit.
439  *
440  * Otherwise (cf ACPI 6.1, s8.4.7.1.1.X), _CPC registers must be in:
441  * - PCC or Functional Fixed Hardware address space if defined
442  * - SystemMemory address space (NULL register) if not defined
443  */
444 bool osc_cpc_flexible_adr_space_confirmed;
445 EXPORT_SYMBOL_GPL(osc_cpc_flexible_adr_space_confirmed);
446 
447 /*
448  * ACPI 6.4 Operating System Capabilities for USB.
449  */
450 bool osc_sb_native_usb4_support_confirmed;
451 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_support_confirmed);
452 
453 bool osc_sb_cppc2_support_acked;
454 
455 static void acpi_bus_osc_negotiate_platform_control(void)
456 {
457 	static const u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
458 	u32 capbuf[2], feature_mask;
459 	acpi_handle handle;
460 
461 	feature_mask = OSC_SB_PR3_SUPPORT | OSC_SB_HOTPLUG_OST_SUPPORT |
462 			OSC_SB_PCLPI_SUPPORT | OSC_SB_OVER_16_PSTATES_SUPPORT |
463 			OSC_SB_GED_SUPPORT | OSC_SB_IRQ_RESOURCE_SOURCE_SUPPORT;
464 
465 	if (IS_ENABLED(CONFIG_ARM64) || IS_ENABLED(CONFIG_X86))
466 		feature_mask |= OSC_SB_GENERIC_INITIATOR_SUPPORT;
467 
468 	if (IS_ENABLED(CONFIG_ACPI_CPPC_LIB)) {
469 		feature_mask |= OSC_SB_CPC_SUPPORT | OSC_SB_CPCV2_SUPPORT |
470 				OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
471 		if (IS_ENABLED(CONFIG_SCHED_MC_PRIO))
472 			feature_mask |= OSC_SB_CPC_DIVERSE_HIGH_SUPPORT;
473 	}
474 
475 	if (IS_ENABLED(CONFIG_ACPI_PROCESSOR_AGGREGATOR))
476 		feature_mask |= OSC_SB_PAD_SUPPORT;
477 
478 	if (IS_ENABLED(CONFIG_ACPI_PROCESSOR))
479 		feature_mask |= OSC_SB_PPC_OST_SUPPORT;
480 
481 	if (IS_ENABLED(CONFIG_ACPI_THERMAL))
482 		feature_mask |= OSC_SB_FAST_THERMAL_SAMPLING_SUPPORT;
483 
484 	if (IS_ENABLED(CONFIG_ACPI_BATTERY))
485 		feature_mask |= OSC_SB_BATTERY_CHARGE_LIMITING_SUPPORT;
486 
487 	if (IS_ENABLED(CONFIG_ACPI_PRMT))
488 		feature_mask |= OSC_SB_PRM_SUPPORT;
489 
490 	if (IS_ENABLED(CONFIG_ACPI_FFH))
491 		feature_mask |= OSC_SB_FFH_OPR_SUPPORT;
492 
493 	if (IS_ENABLED(CONFIG_USB4))
494 		feature_mask |= OSC_SB_NATIVE_USB4_SUPPORT;
495 
496 	if (!ghes_disable)
497 		feature_mask |= OSC_SB_APEI_SUPPORT;
498 
499 	if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
500 		return;
501 
502 	capbuf[OSC_SUPPORT_DWORD] = feature_mask;
503 
504 	acpi_handle_info(handle, "platform _OSC: OS support mask [%08x]\n", feature_mask);
505 
506 	if (acpi_osc_handshake(handle, sb_uuid_str, 1, capbuf, ARRAY_SIZE(capbuf)))
507 		return;
508 
509 	feature_mask = capbuf[OSC_SUPPORT_DWORD];
510 
511 	acpi_handle_info(handle, "platform _OSC: OS control mask [%08x]\n", feature_mask);
512 
513 	osc_sb_cppc2_support_acked = feature_mask & OSC_SB_CPCV2_SUPPORT;
514 	osc_sb_apei_support_acked = feature_mask & OSC_SB_APEI_SUPPORT;
515 	osc_pc_lpi_support_confirmed = feature_mask & OSC_SB_PCLPI_SUPPORT;
516 	osc_sb_native_usb4_support_confirmed = feature_mask & OSC_SB_NATIVE_USB4_SUPPORT;
517 	osc_cpc_flexible_adr_space_confirmed = feature_mask & OSC_SB_CPC_FLEXIBLE_ADR_SPACE;
518 }
519 
520 /*
521  * Native control of USB4 capabilities. If any of the tunneling bits is
522  * set it means OS is in control and we use software based connection
523  * manager.
524  */
525 u32 osc_sb_native_usb4_control;
526 EXPORT_SYMBOL_GPL(osc_sb_native_usb4_control);
527 
528 static void acpi_bus_decode_usb_osc(const char *msg, u32 bits)
529 {
530 	pr_info("%s USB3%c DisplayPort%c PCIe%c XDomain%c\n", msg,
531 	       (bits & OSC_USB_USB3_TUNNELING) ? '+' : '-',
532 	       (bits & OSC_USB_DP_TUNNELING) ? '+' : '-',
533 	       (bits & OSC_USB_PCIE_TUNNELING) ? '+' : '-',
534 	       (bits & OSC_USB_XDOMAIN) ? '+' : '-');
535 }
536 
537 static void acpi_bus_osc_negotiate_usb_control(void)
538 {
539 	static const u8 sb_usb_uuid_str[] = "23A0D13A-26AB-486C-9C5F-0FFA525A575A";
540 	u32 capbuf[3], control;
541 	acpi_handle handle;
542 
543 	if (!osc_sb_native_usb4_support_confirmed)
544 		return;
545 
546 	if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
547 		return;
548 
549 	control = OSC_USB_USB3_TUNNELING | OSC_USB_DP_TUNNELING |
550 		  OSC_USB_PCIE_TUNNELING | OSC_USB_XDOMAIN;
551 
552 	capbuf[OSC_SUPPORT_DWORD] = 0;
553 	capbuf[OSC_CONTROL_DWORD] = control;
554 
555 	if (acpi_osc_handshake(handle, sb_usb_uuid_str, 1, capbuf, ARRAY_SIZE(capbuf)))
556 		return;
557 
558 	osc_sb_native_usb4_control = capbuf[OSC_CONTROL_DWORD];
559 
560 	acpi_bus_decode_usb_osc("USB4 _OSC: OS supports", control);
561 	acpi_bus_decode_usb_osc("USB4 _OSC: OS controls", osc_sb_native_usb4_control);
562 }
563 
564 /* --------------------------------------------------------------------------
565                              Notification Handling
566    -------------------------------------------------------------------------- */
567 
568 /**
569  * acpi_bus_notify - Global system-level (0x00-0x7F) notifications handler
570  * @handle: Target ACPI object.
571  * @type: Notification type.
572  * @data: Ignored.
573  *
574  * This only handles notifications related to device hotplug.
575  */
576 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
577 {
578 	struct acpi_device *adev;
579 
580 	switch (type) {
581 	case ACPI_NOTIFY_BUS_CHECK:
582 		acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
583 		break;
584 
585 	case ACPI_NOTIFY_DEVICE_CHECK:
586 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
587 		break;
588 
589 	case ACPI_NOTIFY_DEVICE_WAKE:
590 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_WAKE event\n");
591 		return;
592 
593 	case ACPI_NOTIFY_EJECT_REQUEST:
594 		acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
595 		break;
596 
597 	case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
598 		acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK_LIGHT event\n");
599 		/* TBD: Exactly what does 'light' mean? */
600 		return;
601 
602 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
603 		acpi_handle_err(handle, "Device cannot be configured due "
604 				"to a frequency mismatch\n");
605 		return;
606 
607 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
608 		acpi_handle_err(handle, "Device cannot be configured due "
609 				"to a bus mode mismatch\n");
610 		return;
611 
612 	case ACPI_NOTIFY_POWER_FAULT:
613 		acpi_handle_err(handle, "Device has suffered a power fault\n");
614 		return;
615 
616 	default:
617 		acpi_handle_debug(handle, "Unknown event type 0x%x\n", type);
618 		return;
619 	}
620 
621 	adev = acpi_get_acpi_dev(handle);
622 
623 	if (adev && ACPI_SUCCESS(acpi_hotplug_schedule(adev, type)))
624 		return;
625 
626 	acpi_put_acpi_dev(adev);
627 
628 	acpi_evaluate_ost(handle, type, ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
629 }
630 
631 int acpi_dev_install_notify_handler(struct acpi_device *adev,
632 				    u32 handler_type,
633 				    acpi_notify_handler handler, void *context)
634 {
635 	acpi_status status;
636 
637 	status = acpi_install_notify_handler(adev->handle, handler_type,
638 					     handler, context);
639 	if (ACPI_FAILURE(status))
640 		return -ENODEV;
641 
642 	return 0;
643 }
644 EXPORT_SYMBOL_GPL(acpi_dev_install_notify_handler);
645 
646 void acpi_dev_remove_notify_handler(struct acpi_device *adev,
647 				    u32 handler_type,
648 				    acpi_notify_handler handler)
649 {
650 	acpi_remove_notify_handler(adev->handle, handler_type, handler);
651 	acpi_os_wait_events_complete();
652 }
653 EXPORT_SYMBOL_GPL(acpi_dev_remove_notify_handler);
654 
655 struct acpi_notify_handler_devres {
656 	struct acpi_device *adev;
657 	acpi_notify_handler handler;
658 	u32 handler_type;
659 };
660 
661 static void devm_acpi_notify_handler_release(struct device *dev, void *res)
662 {
663 	struct acpi_notify_handler_devres *dr = res;
664 
665 	acpi_dev_remove_notify_handler(dr->adev, dr->handler_type, dr->handler);
666 }
667 
668 /**
669  * devm_acpi_install_notify_handler - Install an ACPI notify handler for a
670  *				      managed device
671  * @dev: Device to install a notify handler for
672  * @handler_type: Type of the notify handler
673  * @handler: Handler function to install
674  * @context: Data passed back to the handler function
675  *
676  * This function performs the same function as acpi_dev_install_notify_handler()
677  * called for the ACPI companion of @dev with the same @handler_type, @handler,
678  * and @context arguments, but the ACPI notify handler installed by it will be
679  * automatically removed on driver detach.
680  *
681  * Callers should ensure that all resources used by @handler have been allocated
682  * prior to invoking this function, in which case those resources should be
683  * devres-managed so that they won't be released before the notify handler
684  * removal.  Otherwise, special synchronization between @handler and the
685  * management of those resources is required.
686  *
687  * When the request fails, an error message is printed.  Don't add extra error
688  * messages at the call sites.
689  *
690  * Return: 0 on success or a negative error number.
691  */
692 int devm_acpi_install_notify_handler(struct device *dev, u32 handler_type,
693 				     acpi_notify_handler handler, void *context)
694 {
695 	struct acpi_notify_handler_devres *dr;
696 	struct acpi_device *adev;
697 	int ret;
698 
699 	adev = ACPI_COMPANION(dev);
700 	if (!adev)
701 		return dev_err_probe(dev, -ENODEV, "No ACPI companion\n");
702 
703 	dr = devres_alloc(devm_acpi_notify_handler_release, sizeof(*dr), GFP_KERNEL);
704 	if (!dr)
705 		return -ENOMEM;
706 
707 	ret = acpi_dev_install_notify_handler(adev, handler_type, handler, context);
708 	if (ret) {
709 		devres_free(dr);
710 		return dev_err_probe(dev, ret, "Failed to install an ACPI notify handler\n");
711 	}
712 
713 	dr->adev = adev;
714 	dr->handler = handler;
715 	dr->handler_type = handler_type;
716 	devres_add(dev, dr);
717 
718 	return 0;
719 }
720 EXPORT_SYMBOL_GPL(devm_acpi_install_notify_handler);
721 
722 /* Handle events targeting \_SB device (at present only graceful shutdown) */
723 
724 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
725 #define ACPI_SB_INDICATE_INTERVAL	10000
726 
727 static void sb_notify_work(struct work_struct *dummy)
728 {
729 	acpi_handle sb_handle;
730 
731 	orderly_poweroff(true);
732 
733 	/*
734 	 * After initiating graceful shutdown, the ACPI spec requires OSPM
735 	 * to evaluate _OST method once every 10seconds to indicate that
736 	 * the shutdown is in progress
737 	 */
738 	acpi_get_handle(NULL, "\\_SB", &sb_handle);
739 	while (1) {
740 		pr_info("Graceful shutdown in progress.\n");
741 		acpi_evaluate_ost(sb_handle, ACPI_OST_EC_OSPM_SHUTDOWN,
742 				ACPI_OST_SC_OS_SHUTDOWN_IN_PROGRESS, NULL);
743 		msleep(ACPI_SB_INDICATE_INTERVAL);
744 	}
745 }
746 
747 static void acpi_sb_notify(acpi_handle handle, u32 event, void *data)
748 {
749 	static DECLARE_WORK(acpi_sb_work, sb_notify_work);
750 
751 	if (event == ACPI_SB_NOTIFY_SHUTDOWN_REQUEST) {
752 		if (!work_busy(&acpi_sb_work))
753 			schedule_work(&acpi_sb_work);
754 	} else {
755 		pr_warn("event %x is not supported by \\_SB device\n", event);
756 	}
757 }
758 
759 static int __init acpi_setup_sb_notify_handler(void)
760 {
761 	acpi_handle sb_handle;
762 
763 	if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &sb_handle)))
764 		return -ENXIO;
765 
766 	if (ACPI_FAILURE(acpi_install_notify_handler(sb_handle, ACPI_DEVICE_NOTIFY,
767 						acpi_sb_notify, NULL)))
768 		return -EINVAL;
769 
770 	return 0;
771 }
772 
773 /* --------------------------------------------------------------------------
774                              Device Matching
775    -------------------------------------------------------------------------- */
776 
777 
778 static struct device *primary_physical_device(struct acpi_device *adev)
779 {
780 	struct acpi_device_physical_node *pn;
781 
782 	pn = list_first_entry_or_null(&adev->physical_node_list,
783 				      struct acpi_device_physical_node, node);
784 	if (pn)
785 		return pn->dev;
786 
787 	return NULL;
788 }
789 
790 /**
791  * acpi_bus_get_primary_device - Get first physical device for a given ACPI one
792  * @adev: ACPI device to get the first physical device for.
793  *
794  * Find the first physical device for which @adev is the ACPI companion and
795  * reference count it if present.
796  *
797  * Return: Pointer to the first physical counterpart of @adev or NULL if there
798  * are none.  Callers are responsible for invoking put_device() on the returned
799  * device.
800  */
801 struct device *acpi_bus_get_primary_device(struct acpi_device *adev)
802 {
803 	if (!adev)
804 		return NULL;
805 
806 	guard(mutex)(&adev->physical_node_lock);
807 
808 	return get_device(primary_physical_device(adev));
809 }
810 EXPORT_SYMBOL_GPL(acpi_bus_get_primary_device);
811 
812 static struct acpi_device *acpi_primary_dev_companion(struct acpi_device *adev,
813 						      const struct device *dev)
814 {
815 	const struct device *phys_dev;
816 
817 	guard(mutex)(&adev->physical_node_lock);
818 
819 	phys_dev = primary_physical_device(adev);
820 
821 	return phys_dev && phys_dev == dev ? adev : NULL;
822 }
823 
824 /**
825  * acpi_device_is_first_physical_node - Is given dev first physical node
826  * @adev: ACPI companion device
827  * @dev: Physical device to check
828  *
829  * Function checks if given @dev is the first physical devices attached to
830  * the ACPI companion device. This distinction is needed in some cases
831  * where the same companion device is shared between many physical devices.
832  *
833  * Note that the caller have to provide valid @adev pointer.
834  */
835 bool acpi_device_is_first_physical_node(struct acpi_device *adev,
836 					const struct device *dev)
837 {
838 	return !!acpi_primary_dev_companion(adev, dev);
839 }
840 
841 /*
842  * acpi_companion_match() - Can we match via ACPI companion device
843  * @dev: Device in question
844  *
845  * Check if the given device has an ACPI companion and if that companion has
846  * a valid list of PNP IDs, and if the device is the first (primary) physical
847  * device associated with it.  Return the companion pointer if that's the case
848  * or NULL otherwise.
849  *
850  * If multiple physical devices are attached to a single ACPI companion, we need
851  * to be careful.  The usage scenario for this kind of relationship is that all
852  * of the physical devices in question use resources provided by the ACPI
853  * companion.  A typical case is an MFD device where all the sub-devices share
854  * the parent's ACPI companion.  In such cases we can only allow the primary
855  * (first) physical device to be matched with the help of the companion's PNP
856  * IDs.
857  *
858  * Additional physical devices sharing the ACPI companion can still use
859  * resources available from it but they will be matched normally using functions
860  * provided by their bus types (and analogously for their modalias).
861  */
862 const struct acpi_device *acpi_companion_match(const struct device *dev)
863 {
864 	struct acpi_device *adev;
865 
866 	adev = ACPI_COMPANION(dev);
867 	if (!adev)
868 		return NULL;
869 
870 	if (list_empty(&adev->pnp.ids))
871 		return NULL;
872 
873 	return acpi_primary_dev_companion(adev, dev);
874 }
875 
876 /**
877  * acpi_of_match_device - Match device object using the "compatible" property.
878  * @adev: ACPI device object to match.
879  * @of_match_table: List of device IDs to match against.
880  * @of_id: OF ID if matched
881  *
882  * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
883  * identifiers and a _DSD object with the "compatible" property, use that
884  * property to match against the given list of identifiers.
885  */
886 static bool acpi_of_match_device(const struct acpi_device *adev,
887 				 const struct of_device_id *of_match_table,
888 				 const struct of_device_id **of_id)
889 {
890 	const union acpi_object *of_compatible, *obj;
891 	int i, nval;
892 
893 	if (!adev)
894 		return false;
895 
896 	of_compatible = adev->data.of_compatible;
897 	if (!of_match_table || !of_compatible)
898 		return false;
899 
900 	if (of_compatible->type == ACPI_TYPE_PACKAGE) {
901 		nval = of_compatible->package.count;
902 		obj = of_compatible->package.elements;
903 	} else { /* Must be ACPI_TYPE_STRING. */
904 		nval = 1;
905 		obj = of_compatible;
906 	}
907 	/* Now we can look for the driver DT compatible strings */
908 	for (i = 0; i < nval; i++, obj++) {
909 		const struct of_device_id *id;
910 
911 		for (id = of_match_table; id->compatible[0]; id++)
912 			if (!strcasecmp(obj->string.pointer, id->compatible)) {
913 				if (of_id)
914 					*of_id = id;
915 				return true;
916 			}
917 	}
918 
919 	return false;
920 }
921 
922 static bool acpi_of_modalias(struct acpi_device *adev,
923 			     char *modalias, size_t len)
924 {
925 	const union acpi_object *of_compatible;
926 	const union acpi_object *obj;
927 	const char *str, *chr;
928 
929 	of_compatible = adev->data.of_compatible;
930 	if (!of_compatible)
931 		return false;
932 
933 	if (of_compatible->type == ACPI_TYPE_PACKAGE)
934 		obj = of_compatible->package.elements;
935 	else /* Must be ACPI_TYPE_STRING. */
936 		obj = of_compatible;
937 
938 	str = obj->string.pointer;
939 	chr = strchr(str, ',');
940 	strscpy(modalias, chr ? chr + 1 : str, len);
941 
942 	return true;
943 }
944 
945 /**
946  * acpi_set_modalias - Set modalias using "compatible" property or supplied ID
947  * @adev:	ACPI device object to match
948  * @default_id:	ID string to use as default if no compatible string found
949  * @modalias:   Pointer to buffer that modalias value will be copied into
950  * @len:	Length of modalias buffer
951  *
952  * This is a counterpart of of_alias_from_compatible() for struct acpi_device
953  * objects. If there is a compatible string for @adev, it will be copied to
954  * @modalias with the vendor prefix stripped; otherwise, @default_id will be
955  * used.
956  */
957 void acpi_set_modalias(struct acpi_device *adev, const char *default_id,
958 		       char *modalias, size_t len)
959 {
960 	if (!acpi_of_modalias(adev, modalias, len))
961 		strscpy(modalias, default_id, len);
962 }
963 EXPORT_SYMBOL_GPL(acpi_set_modalias);
964 
965 static bool __acpi_match_device_cls(const struct acpi_device_id *id,
966 				    struct acpi_hardware_id *hwid)
967 {
968 	int i, msk, byte_shift;
969 	char buf[3];
970 
971 	if (!id->cls)
972 		return false;
973 
974 	/* Apply class-code bitmask, before checking each class-code byte */
975 	for (i = 1; i <= 3; i++) {
976 		byte_shift = 8 * (3 - i);
977 		msk = (id->cls_msk >> byte_shift) & 0xFF;
978 		if (!msk)
979 			continue;
980 
981 		sprintf(buf, "%02x", (id->cls >> byte_shift) & msk);
982 		if (strncmp(buf, &hwid->id[(i - 1) * 2], 2))
983 			return false;
984 	}
985 	return true;
986 }
987 
988 static bool __acpi_match_device(const struct acpi_device *device,
989 				const struct acpi_device_id *acpi_ids,
990 				const struct of_device_id *of_ids,
991 				const struct acpi_device_id **acpi_id,
992 				const struct of_device_id **of_id)
993 {
994 	const struct acpi_device_id *id;
995 	struct acpi_hardware_id *hwid;
996 
997 	/*
998 	 * If the device is not present, it is unnecessary to load device
999 	 * driver for it.
1000 	 */
1001 	if (!device || !device->status.present)
1002 		return false;
1003 
1004 	list_for_each_entry(hwid, &device->pnp.ids, list) {
1005 		/* First, check the ACPI/PNP IDs provided by the caller. */
1006 		if (acpi_ids) {
1007 			for (id = acpi_ids; id->id[0] || id->cls; id++) {
1008 				if (id->id[0] && !strcmp((char *)id->id, hwid->id))
1009 					goto out_acpi_match;
1010 				if (id->cls && __acpi_match_device_cls(id, hwid))
1011 					goto out_acpi_match;
1012 			}
1013 		}
1014 
1015 		/*
1016 		 * Next, check ACPI_DT_NAMESPACE_HID and try to match the
1017 		 * "compatible" property if found.
1018 		 */
1019 		if (!strcmp(ACPI_DT_NAMESPACE_HID, hwid->id))
1020 			return acpi_of_match_device(device, of_ids, of_id);
1021 	}
1022 	return false;
1023 
1024 out_acpi_match:
1025 	if (acpi_id)
1026 		*acpi_id = id;
1027 	return true;
1028 }
1029 
1030 /**
1031  * acpi_match_acpi_device - Match an ACPI device against a given list of ACPI IDs
1032  * @ids: Array of struct acpi_device_id objects to match against.
1033  * @adev: The ACPI device pointer to match.
1034  *
1035  * Match the ACPI device @adev against a given list of ACPI IDs @ids.
1036  *
1037  * Return:
1038  * a pointer to the first matching ACPI ID on success or %NULL on failure.
1039  */
1040 const struct acpi_device_id *acpi_match_acpi_device(const struct acpi_device_id *ids,
1041 						    const struct acpi_device *adev)
1042 {
1043 	const struct acpi_device_id *id = NULL;
1044 
1045 	__acpi_match_device(adev, ids, NULL, &id, NULL);
1046 	return id;
1047 }
1048 EXPORT_SYMBOL_GPL(acpi_match_acpi_device);
1049 
1050 /**
1051  * acpi_match_device - Match a struct device against a given list of ACPI IDs
1052  * @ids: Array of struct acpi_device_id object to match against.
1053  * @dev: The device structure to match.
1054  *
1055  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
1056  * object for that handle and use that object to match against a given list of
1057  * device IDs.
1058  *
1059  * Return a pointer to the first matching ID on success or %NULL on failure.
1060  */
1061 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
1062 					       const struct device *dev)
1063 {
1064 	return acpi_match_acpi_device(ids, acpi_companion_match(dev));
1065 }
1066 EXPORT_SYMBOL_GPL(acpi_match_device);
1067 
1068 const void *acpi_device_get_match_data(const struct device *dev)
1069 {
1070 	const struct acpi_device_id *acpi_ids = dev->driver->acpi_match_table;
1071 	const struct of_device_id *of_ids = dev->driver->of_match_table;
1072 	const struct acpi_device *adev = acpi_companion_match(dev);
1073 	const struct acpi_device_id *acpi_id = NULL;
1074 	const struct of_device_id *of_id = NULL;
1075 
1076 	if (!__acpi_match_device(adev, acpi_ids, of_ids, &acpi_id, &of_id))
1077 		return NULL;
1078 
1079 	if (acpi_id)
1080 		return (const void *)acpi_id->driver_data;
1081 
1082 	if (of_id)
1083 		return of_id->data;
1084 
1085 	return NULL;
1086 }
1087 EXPORT_SYMBOL_GPL(acpi_device_get_match_data);
1088 
1089 int acpi_match_device_ids(struct acpi_device *device,
1090 			  const struct acpi_device_id *ids)
1091 {
1092 	return __acpi_match_device(device, ids, NULL, NULL, NULL) ? 0 : -ENOENT;
1093 }
1094 EXPORT_SYMBOL(acpi_match_device_ids);
1095 
1096 bool acpi_driver_match_device(struct device *dev,
1097 			      const struct device_driver *drv)
1098 {
1099 	const struct acpi_device_id *acpi_ids = drv->acpi_match_table;
1100 	const struct of_device_id *of_ids = drv->of_match_table;
1101 
1102 	if (!acpi_ids)
1103 		return acpi_of_match_device(ACPI_COMPANION(dev), of_ids, NULL);
1104 
1105 	return __acpi_match_device(acpi_companion_match(dev), acpi_ids, of_ids, NULL, NULL);
1106 }
1107 EXPORT_SYMBOL_GPL(acpi_driver_match_device);
1108 
1109 /* --------------------------------------------------------------------------
1110                               ACPI Bus operations
1111    -------------------------------------------------------------------------- */
1112 
1113 static int acpi_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
1114 {
1115 	return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
1116 }
1117 
1118 const struct bus_type acpi_bus_type = {
1119 	.name		= "acpi",
1120 	.uevent		= acpi_device_uevent,
1121 };
1122 
1123 int acpi_bus_for_each_dev(int (*fn)(struct device *, void *), void *data)
1124 {
1125 	return bus_for_each_dev(&acpi_bus_type, NULL, data, fn);
1126 }
1127 EXPORT_SYMBOL_GPL(acpi_bus_for_each_dev);
1128 
1129 /**
1130  * acpi_bus_find_device_by_name() - Locate an ACPI device by its name
1131  * @name: Name of the device to match
1132  *
1133  * The caller is responsible for calling put_device() on the returned object.
1134  *
1135  * Returns:
1136  * New reference to the matched device or NULL if the device can't be found.
1137  */
1138 struct device *acpi_bus_find_device_by_name(const char *name)
1139 {
1140 	return bus_find_device_by_name(&acpi_bus_type, NULL, name);
1141 }
1142 EXPORT_SYMBOL_GPL(acpi_bus_find_device_by_name);
1143 
1144 struct acpi_dev_walk_context {
1145 	int (*fn)(struct acpi_device *, void *);
1146 	void *data;
1147 };
1148 
1149 static int acpi_dev_for_one_check(struct device *dev, void *context)
1150 {
1151 	struct acpi_dev_walk_context *adwc = context;
1152 
1153 	if (dev->bus != &acpi_bus_type)
1154 		return 0;
1155 
1156 	return adwc->fn(to_acpi_device(dev), adwc->data);
1157 }
1158 EXPORT_SYMBOL_GPL(acpi_dev_for_each_child);
1159 
1160 int acpi_dev_for_each_child(struct acpi_device *adev,
1161 			    int (*fn)(struct acpi_device *, void *), void *data)
1162 {
1163 	struct acpi_dev_walk_context adwc = {
1164 		.fn = fn,
1165 		.data = data,
1166 	};
1167 
1168 	return device_for_each_child(&adev->dev, &adwc, acpi_dev_for_one_check);
1169 }
1170 
1171 int acpi_dev_for_each_child_reverse(struct acpi_device *adev,
1172 				    int (*fn)(struct acpi_device *, void *),
1173 				    void *data)
1174 {
1175 	struct acpi_dev_walk_context adwc = {
1176 		.fn = fn,
1177 		.data = data,
1178 	};
1179 
1180 	return device_for_each_child_reverse(&adev->dev, &adwc, acpi_dev_for_one_check);
1181 }
1182 
1183 /* --------------------------------------------------------------------------
1184                              Initialization/Cleanup
1185    -------------------------------------------------------------------------- */
1186 
1187 static int __init acpi_bus_init_irq(void)
1188 {
1189 	acpi_status status;
1190 	char *message = NULL;
1191 
1192 
1193 	/*
1194 	 * Let the system know what interrupt model we are using by
1195 	 * evaluating the \_PIC object, if exists.
1196 	 */
1197 
1198 	switch (acpi_irq_model) {
1199 	case ACPI_IRQ_MODEL_PIC:
1200 		message = "PIC";
1201 		break;
1202 	case ACPI_IRQ_MODEL_IOAPIC:
1203 		message = "IOAPIC";
1204 		break;
1205 	case ACPI_IRQ_MODEL_IOSAPIC:
1206 		message = "IOSAPIC";
1207 		break;
1208 	case ACPI_IRQ_MODEL_GIC:
1209 		message = "GIC";
1210 		break;
1211 	case ACPI_IRQ_MODEL_GIC_V5:
1212 		message = "GICv5";
1213 		break;
1214 	case ACPI_IRQ_MODEL_PLATFORM:
1215 		message = "platform specific model";
1216 		break;
1217 	case ACPI_IRQ_MODEL_LPIC:
1218 		message = "LPIC";
1219 		break;
1220 	case ACPI_IRQ_MODEL_RINTC:
1221 		message = "RINTC";
1222 		break;
1223 	default:
1224 		pr_info("Unknown interrupt routing model\n");
1225 		return -ENODEV;
1226 	}
1227 
1228 	pr_info("Using %s for interrupt routing\n", message);
1229 
1230 	status = acpi_execute_simple_method(NULL, "\\_PIC", acpi_irq_model);
1231 	if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
1232 		pr_info("_PIC evaluation failed: %s\n", acpi_format_exception(status));
1233 		return -ENODEV;
1234 	}
1235 
1236 	return 0;
1237 }
1238 
1239 /**
1240  * acpi_early_init - Initialize ACPICA and populate the ACPI namespace.
1241  *
1242  * The ACPI tables are accessible after this, but the handling of events has not
1243  * been initialized and the global lock is not available yet, so AML should not
1244  * be executed at this point.
1245  *
1246  * Doing this before switching the EFI runtime services to virtual mode allows
1247  * the EfiBootServices memory to be freed slightly earlier on boot.
1248  */
1249 void __init acpi_early_init(void)
1250 {
1251 	acpi_status status;
1252 
1253 	if (acpi_disabled)
1254 		return;
1255 
1256 	pr_info("Core revision %08x\n", ACPI_CA_VERSION);
1257 
1258 	/* enable workarounds, unless strict ACPI spec. compliance */
1259 	if (!acpi_strict)
1260 		acpi_gbl_enable_interpreter_slack = TRUE;
1261 
1262 	acpi_permanent_mmap = true;
1263 
1264 #ifdef CONFIG_X86
1265 	/*
1266 	 * If the machine falls into the DMI check table,
1267 	 * DSDT will be copied to memory.
1268 	 * Note that calling dmi_check_system() here on other architectures
1269 	 * would not be OK because only x86 initializes dmi early enough.
1270 	 * Thankfully only x86 systems need such quirks for now.
1271 	 */
1272 	dmi_check_system(dsdt_dmi_table);
1273 #endif
1274 
1275 	status = acpi_reallocate_root_table();
1276 	if (ACPI_FAILURE(status)) {
1277 		pr_err("Unable to reallocate ACPI tables\n");
1278 		goto error0;
1279 	}
1280 
1281 	status = acpi_initialize_subsystem();
1282 	if (ACPI_FAILURE(status)) {
1283 		pr_err("Unable to initialize the ACPI Interpreter\n");
1284 		goto error0;
1285 	}
1286 
1287 #ifdef CONFIG_X86
1288 	if (!acpi_ioapic) {
1289 		/* compatible (0) means level (3) */
1290 		if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
1291 			acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
1292 			acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
1293 		}
1294 		/* Set PIC-mode SCI trigger type */
1295 		acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
1296 					 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
1297 	} else {
1298 		/*
1299 		 * now that acpi_gbl_FADT is initialized,
1300 		 * update it with result from INT_SRC_OVR parsing
1301 		 */
1302 		acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
1303 	}
1304 #endif
1305 	return;
1306 
1307  error0:
1308 	disable_acpi();
1309 }
1310 
1311 /**
1312  * acpi_subsystem_init - Finalize the early initialization of ACPI.
1313  *
1314  * Switch over the platform to the ACPI mode (if possible).
1315  *
1316  * Doing this too early is generally unsafe, but at the same time it needs to be
1317  * done before all things that really depend on ACPI.  The right spot appears to
1318  * be before finalizing the EFI initialization.
1319  */
1320 void __init acpi_subsystem_init(void)
1321 {
1322 	acpi_status status;
1323 
1324 	if (acpi_disabled)
1325 		return;
1326 
1327 	status = acpi_enable_subsystem(~ACPI_NO_ACPI_ENABLE);
1328 	if (ACPI_FAILURE(status)) {
1329 		pr_err("Unable to enable ACPI\n");
1330 		disable_acpi();
1331 	} else {
1332 		/*
1333 		 * If the system is using ACPI then we can be reasonably
1334 		 * confident that any regulators are managed by the firmware
1335 		 * so tell the regulator core it has everything it needs to
1336 		 * know.
1337 		 */
1338 		regulator_has_full_constraints();
1339 	}
1340 }
1341 
1342 static acpi_status acpi_bus_table_handler(u32 event, void *table, void *context)
1343 {
1344 	if (event == ACPI_TABLE_EVENT_LOAD)
1345 		acpi_scan_table_notify();
1346 
1347 	return acpi_sysfs_table_handler(event, table, context);
1348 }
1349 
1350 static int __init acpi_bus_init(void)
1351 {
1352 	int result;
1353 	acpi_status status;
1354 
1355 	acpi_os_initialize1();
1356 
1357 	status = acpi_load_tables();
1358 	if (ACPI_FAILURE(status)) {
1359 		pr_err("Unable to load the System Description Tables\n");
1360 		goto error1;
1361 	}
1362 
1363 	/*
1364 	 * ACPI 2.0 requires the EC driver to be loaded and work before the EC
1365 	 * device is found in the namespace.
1366 	 *
1367 	 * This is accomplished by looking for the ECDT table and getting the EC
1368 	 * parameters out of that.
1369 	 *
1370 	 * Do that before calling acpi_initialize_objects() which may trigger EC
1371 	 * address space accesses.
1372 	 */
1373 	acpi_ec_ecdt_probe();
1374 
1375 	status = acpi_enable_subsystem(ACPI_NO_ACPI_ENABLE);
1376 	if (ACPI_FAILURE(status)) {
1377 		pr_err("Unable to start the ACPI Interpreter\n");
1378 		goto error1;
1379 	}
1380 
1381 	status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
1382 	if (ACPI_FAILURE(status)) {
1383 		pr_err("Unable to initialize ACPI objects\n");
1384 		goto error1;
1385 	}
1386 
1387 	/*
1388 	 * _OSC method may exist in module level code,
1389 	 * so it must be run after ACPI_FULL_INITIALIZATION
1390 	 */
1391 	acpi_bus_osc_negotiate_platform_control();
1392 	acpi_bus_osc_negotiate_usb_control();
1393 
1394 	/*
1395 	 * _PDC control method may load dynamic SSDT tables,
1396 	 * and we need to install the table handler before that.
1397 	 */
1398 	status = acpi_install_table_handler(acpi_bus_table_handler, NULL);
1399 
1400 	acpi_sysfs_init();
1401 
1402 	acpi_early_processor_control_setup();
1403 
1404 	/*
1405 	 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
1406 	 * is necessary to enable it as early as possible.
1407 	 */
1408 	acpi_ec_dsdt_probe();
1409 
1410 	pr_info("Interpreter enabled\n");
1411 
1412 	/* Initialize sleep structures */
1413 	acpi_sleep_init();
1414 
1415 	/*
1416 	 * Get the system interrupt model and evaluate \_PIC.
1417 	 */
1418 	result = acpi_bus_init_irq();
1419 	if (result)
1420 		goto error1;
1421 
1422 	/*
1423 	 * Register for all standard device notifications.
1424 	 */
1425 	status =
1426 	    acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
1427 					&acpi_bus_notify, NULL);
1428 	if (ACPI_FAILURE(status)) {
1429 		pr_err("Unable to register for system notifications\n");
1430 		goto error1;
1431 	}
1432 
1433 	/*
1434 	 * Create the top ACPI proc directory
1435 	 */
1436 	acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
1437 
1438 	result = companion_bus_register(&acpi_bus_type);
1439 	if (!result)
1440 		return 0;
1441 
1442 	/* Mimic structured exception handling */
1443       error1:
1444 	acpi_terminate();
1445 	return -ENODEV;
1446 }
1447 
1448 struct kobject *acpi_kobj;
1449 EXPORT_SYMBOL_GPL(acpi_kobj);
1450 
1451 void __weak __init acpi_arch_init(void) { }
1452 
1453 static int __init acpi_init(void)
1454 {
1455 	int result;
1456 
1457 	if (acpi_disabled) {
1458 		pr_info("Interpreter disabled.\n");
1459 		return -ENODEV;
1460 	}
1461 
1462 	acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
1463 	if (!acpi_kobj) {
1464 		pr_err("Failed to register kobject\n");
1465 		return -ENOMEM;
1466 	}
1467 
1468 	init_prmt();
1469 	acpi_init_pcc();
1470 	result = acpi_bus_init();
1471 	if (result) {
1472 		kobject_put(acpi_kobj);
1473 		disable_acpi();
1474 		return result;
1475 	}
1476 	acpi_init_ffh();
1477 
1478 	pci_mmcfg_late_init();
1479 	acpi_viot_early_init();
1480 	acpi_hest_init();
1481 	acpi_ghes_init();
1482 	acpi_arch_init();
1483 	acpi_scan_init();
1484 	acpi_ec_init();
1485 	acpi_debugfs_init();
1486 	acpi_sleep_proc_init();
1487 	acpi_wakeup_device_init();
1488 	acpi_debugger_init();
1489 	acpi_setup_sb_notify_handler();
1490 	acpi_viot_init();
1491 	return 0;
1492 }
1493 
1494 subsys_initcall(acpi_init);
1495