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