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