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