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