ACPI: scan: Eliminate __acpi_device_add()
[linux-block.git] / drivers / acpi / scan.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * scan.c - support for transforming the ACPI namespace into individual objects
4 */
5
8acf4108
HG
6#define pr_fmt(fmt) "ACPI: " fmt
7
1da177e4
LT
8#include <linux/module.h>
9#include <linux/init.h>
5a0e3ad6 10#include <linux/slab.h>
9b6d97b6 11#include <linux/kernel.h>
1da177e4 12#include <linux/acpi.h>
643b8e4d 13#include <linux/acpi_iort.h>
3cf48554 14#include <linux/acpi_viot.h>
11a8c5e3 15#include <linux/iommu.h>
74523c90
AK
16#include <linux/signal.h>
17#include <linux/kthread.h>
222e82ac 18#include <linux/dmi.h>
0a0f0d8b 19#include <linux/dma-map-ops.h>
ca9ef3ab 20#include <linux/platform_data/x86/apple.h>
ca5999fd 21#include <linux/pgtable.h>
882c982d 22#include <linux/crc32.h>
d783156e 23
e60cc7a6
BH
24#include "internal.h"
25
4be44fcd 26extern struct acpi_device *acpi_root;
1da177e4
LT
27
28#define ACPI_BUS_CLASS "system_bus"
29b71a1c 29#define ACPI_BUS_HID "LNXSYBUS"
1da177e4
LT
30#define ACPI_BUS_DEVICE_NAME "System Bus"
31
859ac9a4
BH
32#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
33
d783156e
RW
34#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
35
620e112c 36static const char *dummy_hid = "device";
2b2ae7c7 37
40e7fcb1
LT
38static LIST_HEAD(acpi_dep_list);
39static DEFINE_MUTEX(acpi_dep_list_lock);
2d12b6b3 40LIST_HEAD(acpi_bus_id_list);
c511cc19 41static DEFINE_MUTEX(acpi_scan_lock);
ca589f94 42static LIST_HEAD(acpi_scan_handlers_list);
9090589d 43DEFINE_MUTEX(acpi_device_lock);
1da177e4 44LIST_HEAD(acpi_wakeup_device_list);
e525506f 45static DEFINE_MUTEX(acpi_hp_context_lock);
1da177e4 46
a4e081b0
SZ
47/*
48 * The UART device described by the SPCR table is the only object which needs
49 * special-casing. Everything else is covered by ACPI namespace paths in STAO
50 * table.
51 */
52static u64 spcr_uart_addr;
53
3757b948
RW
54void acpi_scan_lock_acquire(void)
55{
56 mutex_lock(&acpi_scan_lock);
57}
58EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
59
60void acpi_scan_lock_release(void)
61{
62 mutex_unlock(&acpi_scan_lock);
63}
64EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
65
e525506f
RW
66void acpi_lock_hp_context(void)
67{
68 mutex_lock(&acpi_hp_context_lock);
69}
70
71void acpi_unlock_hp_context(void)
72{
73 mutex_unlock(&acpi_hp_context_lock);
74}
75
5d513205
RW
76void acpi_initialize_hp_context(struct acpi_device *adev,
77 struct acpi_hotplug_context *hp,
78 int (*notify)(struct acpi_device *, u32),
79 void (*uevent)(struct acpi_device *, u32))
80{
81 acpi_lock_hp_context();
ba574dc8
RW
82 hp->notify = notify;
83 hp->uevent = uevent;
84 acpi_set_hp_context(adev, hp);
5d513205
RW
85 acpi_unlock_hp_context();
86}
87EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
88
ca589f94
RW
89int acpi_scan_add_handler(struct acpi_scan_handler *handler)
90{
d34afa9d 91 if (!handler)
ca589f94
RW
92 return -EINVAL;
93
94 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
95 return 0;
96}
97
3f8055c3
RW
98int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
99 const char *hotplug_profile_name)
100{
101 int error;
102
103 error = acpi_scan_add_handler(handler);
104 if (error)
105 return error;
106
107 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
108 return 0;
109}
110
caa73ea1 111bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
d22ddcbc
RW
112{
113 struct acpi_device_physical_node *pn;
114 bool offline = true;
27664c58 115 char *envp[] = { "EVENT=offline", NULL };
d22ddcbc 116
4c533c80
RW
117 /*
118 * acpi_container_offline() calls this for all of the container's
119 * children under the container's physical_node_lock lock.
120 */
121 mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
d22ddcbc
RW
122
123 list_for_each_entry(pn, &adev->physical_node_list, node)
124 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
caa73ea1 125 if (uevent)
27664c58 126 kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
caa73ea1 127
d22ddcbc
RW
128 offline = false;
129 break;
130 }
131
132 mutex_unlock(&adev->physical_node_lock);
133 return offline;
134}
135
7f28ddec
RW
136static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
137 void **ret_p)
683058e3 138{
e3c963c4 139 struct acpi_device *device = acpi_fetch_acpi_dev(handle);
683058e3 140 struct acpi_device_physical_node *pn;
303bfdb1 141 bool second_pass = (bool)data;
683058e3
RW
142 acpi_status status = AE_OK;
143
e3c963c4 144 if (!device)
683058e3
RW
145 return AE_OK;
146
7f28ddec
RW
147 if (device->handler && !device->handler->hotplug.enabled) {
148 *ret_p = &device->dev;
149 return AE_SUPPORT;
150 }
151
683058e3
RW
152 mutex_lock(&device->physical_node_lock);
153
154 list_for_each_entry(pn, &device->physical_node_list, node) {
155 int ret;
156
303bfdb1
RW
157 if (second_pass) {
158 /* Skip devices offlined by the first pass. */
159 if (pn->put_online)
160 continue;
161 } else {
162 pn->put_online = false;
163 }
683058e3 164 ret = device_offline(pn->dev);
303bfdb1
RW
165 if (ret >= 0) {
166 pn->put_online = !ret;
167 } else {
168 *ret_p = pn->dev;
169 if (second_pass) {
170 status = AE_ERROR;
171 break;
172 }
683058e3 173 }
683058e3
RW
174 }
175
176 mutex_unlock(&device->physical_node_lock);
177
178 return status;
179}
180
7f28ddec
RW
181static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
182 void **ret_p)
683058e3 183{
e3c963c4 184 struct acpi_device *device = acpi_fetch_acpi_dev(handle);
683058e3
RW
185 struct acpi_device_physical_node *pn;
186
e3c963c4 187 if (!device)
683058e3
RW
188 return AE_OK;
189
190 mutex_lock(&device->physical_node_lock);
191
192 list_for_each_entry(pn, &device->physical_node_list, node)
193 if (pn->put_online) {
194 device_online(pn->dev);
195 pn->put_online = false;
196 }
197
198 mutex_unlock(&device->physical_node_lock);
199
200 return AE_OK;
201}
202
d22ddcbc 203static int acpi_scan_try_to_offline(struct acpi_device *device)
1da177e4 204{
5993c467 205 acpi_handle handle = device->handle;
d22ddcbc 206 struct device *errdev = NULL;
a33ec399 207 acpi_status status;
f058cdf4 208
303bfdb1
RW
209 /*
210 * Carry out two passes here and ignore errors in the first pass,
211 * because if the devices in question are memory blocks and
212 * CONFIG_MEMCG is set, one of the blocks may hold data structures
213 * that the other blocks depend on, but it is not known in advance which
214 * block holds them.
215 *
216 * If the first pass is successful, the second one isn't needed, though.
217 */
7f28ddec
RW
218 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
219 NULL, acpi_bus_offline, (void *)false,
220 (void **)&errdev);
221 if (status == AE_SUPPORT) {
222 dev_warn(errdev, "Offline disabled.\n");
223 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
224 acpi_bus_online, NULL, NULL, NULL);
7f28ddec
RW
225 return -EPERM;
226 }
227 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
303bfdb1
RW
228 if (errdev) {
229 errdev = NULL;
683058e3 230 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
7f28ddec
RW
231 NULL, acpi_bus_offline, (void *)true,
232 (void **)&errdev);
ffc10d82 233 if (!errdev)
7f28ddec
RW
234 acpi_bus_offline(handle, 0, (void *)true,
235 (void **)&errdev);
303bfdb1 236
ffc10d82 237 if (errdev) {
303bfdb1 238 dev_warn(errdev, "Offline failed.\n");
7f28ddec 239 acpi_bus_online(handle, 0, NULL, NULL);
303bfdb1 240 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
7f28ddec
RW
241 ACPI_UINT32_MAX, acpi_bus_online,
242 NULL, NULL, NULL);
303bfdb1
RW
243 return -EBUSY;
244 }
683058e3 245 }
d22ddcbc
RW
246 return 0;
247}
248
249static int acpi_scan_hot_remove(struct acpi_device *device)
250{
251 acpi_handle handle = device->handle;
252 unsigned long long sta;
253 acpi_status status;
254
ffc10d82 255 if (device->handler && device->handler->hotplug.demand_offline) {
caa73ea1 256 if (!acpi_scan_is_offline(device, true))
d22ddcbc
RW
257 return -EBUSY;
258 } else {
259 int error = acpi_scan_try_to_offline(device);
260 if (error)
261 return error;
262 }
683058e3 263
e52d9d8c 264 acpi_handle_debug(handle, "Ejecting\n");
26d46867 265
bfee26db 266 acpi_bus_trim(device);
683058e3 267
7d2421f8 268 acpi_evaluate_lck(handle, 0);
1da177e4
LT
269 /*
270 * TBD: _EJD support.
271 */
7d2421f8
JL
272 status = acpi_evaluate_ej0(handle);
273 if (status == AE_NOT_FOUND)
274 return -ENODEV;
275 else if (ACPI_FAILURE(status))
276 return -EIO;
1da177e4 277
882fd12e
TK
278 /*
279 * Verify if eject was indeed successful. If not, log an error
280 * message. No need to call _OST since _EJ0 call was made OK.
281 */
282 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
283 if (ACPI_FAILURE(status)) {
284 acpi_handle_warn(handle,
285 "Status check after eject failed (0x%x)\n", status);
286 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
287 acpi_handle_warn(handle,
288 "Eject incomplete - status 0x%llx\n", sta);
289 }
290
a33ec399
RW
291 return 0;
292}
1da177e4 293
443fc820
RW
294static int acpi_scan_device_not_present(struct acpi_device *adev)
295{
296 if (!acpi_device_enumerated(adev)) {
297 dev_warn(&adev->dev, "Still not present\n");
298 return -EALREADY;
299 }
300 acpi_bus_trim(adev);
301 return 0;
302}
303
c27b2c33 304static int acpi_scan_device_check(struct acpi_device *adev)
a33ec399 305{
f943db40 306 int error;
a33ec399 307
443fc820
RW
308 acpi_bus_get_status(adev);
309 if (adev->status.present || adev->status.functional) {
310 /*
311 * This function is only called for device objects for which
312 * matching scan handlers exist. The only situation in which
313 * the scan handler is not attached to this device object yet
314 * is when the device has just appeared (either it wasn't
315 * present at all before or it was removed and then added
316 * again).
317 */
318 if (adev->handler) {
319 dev_warn(&adev->dev, "Already enumerated\n");
320 return -EALREADY;
321 }
322 error = acpi_bus_scan(adev->handle);
323 if (error) {
324 dev_warn(&adev->dev, "Namespace scan failure\n");
325 return error;
326 }
327 if (!adev->handler) {
328 dev_warn(&adev->dev, "Enumeration failure\n");
329 error = -ENODEV;
330 }
331 } else {
332 error = acpi_scan_device_not_present(adev);
333 }
334 return error;
335}
336
a976a2ac 337static int acpi_scan_bus_check(struct acpi_device *adev, void *not_used)
443fc820
RW
338{
339 struct acpi_scan_handler *handler = adev->handler;
443fc820
RW
340 int error;
341
342 acpi_bus_get_status(adev);
343 if (!(adev->status.present || adev->status.functional)) {
344 acpi_scan_device_not_present(adev);
345 return 0;
346 }
347 if (handler && handler->hotplug.scan_dependent)
348 return handler->hotplug.scan_dependent(adev);
349
c27b2c33
RW
350 error = acpi_bus_scan(adev->handle);
351 if (error) {
352 dev_warn(&adev->dev, "Namespace scan failure\n");
353 return error;
354 }
a976a2ac 355 return acpi_dev_for_each_child(adev, acpi_scan_bus_check, NULL);
a33ec399
RW
356}
357
3c2cc7ff
RW
358static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
359{
360 switch (type) {
361 case ACPI_NOTIFY_BUS_CHECK:
a976a2ac 362 return acpi_scan_bus_check(adev, NULL);
3c2cc7ff
RW
363 case ACPI_NOTIFY_DEVICE_CHECK:
364 return acpi_scan_device_check(adev);
365 case ACPI_NOTIFY_EJECT_REQUEST:
366 case ACPI_OST_EC_OSPM_EJECT:
dd2151be
RW
367 if (adev->handler && !adev->handler->hotplug.enabled) {
368 dev_info(&adev->dev, "Eject disabled\n");
369 return -EPERM;
370 }
700b8422
RW
371 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
372 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
3c2cc7ff
RW
373 return acpi_scan_hot_remove(adev);
374 }
375 return -EINVAL;
376}
377
1e3bcb59 378void acpi_device_hotplug(struct acpi_device *adev, u32 src)
a33ec399 379{
a33ec399 380 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
3c2cc7ff 381 int error = -ENODEV;
a33ec399 382
683058e3 383 lock_device_hotplug();
e0ae8fee 384 mutex_lock(&acpi_scan_lock);
a33ec399 385
c27b2c33
RW
386 /*
387 * The device object's ACPI handle cannot become invalid as long as we
3c2cc7ff 388 * are holding acpi_scan_lock, but it might have become invalid before
c27b2c33
RW
389 * that lock was acquired.
390 */
391 if (adev->handle == INVALID_ACPI_HANDLE)
3c2cc7ff 392 goto err_out;
c27b2c33 393
1e2380cd
RW
394 if (adev->flags.is_dock_station) {
395 error = dock_notify(adev, src);
396 } else if (adev->flags.hotplug_notify) {
3c2cc7ff
RW
397 error = acpi_generic_hotplug_event(adev, src);
398 } else {
be27b3dc 399 int (*notify)(struct acpi_device *, u32);
3c2cc7ff
RW
400
401 acpi_lock_hp_context();
be27b3dc 402 notify = adev->hp ? adev->hp->notify : NULL;
3c2cc7ff
RW
403 acpi_unlock_hp_context();
404 /*
405 * There may be additional notify handlers for device objects
406 * without the .event() callback, so ignore them here.
407 */
be27b3dc
RW
408 if (notify)
409 error = notify(adev, src);
3c2cc7ff
RW
410 else
411 goto out;
a33ec399 412 }
d429e5c1
LCY
413 switch (error) {
414 case 0:
c27b2c33 415 ost_code = ACPI_OST_SC_SUCCESS;
d429e5c1
LCY
416 break;
417 case -EPERM:
418 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
419 break;
420 case -EBUSY:
421 ost_code = ACPI_OST_SC_DEVICE_BUSY;
422 break;
423 default:
424 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
425 break;
426 }
a33ec399 427
3c2cc7ff 428 err_out:
700b8422 429 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
3c2cc7ff
RW
430
431 out:
45e9aa1f 432 acpi_put_acpi_dev(adev);
a33ec399 433 mutex_unlock(&acpi_scan_lock);
e0ae8fee 434 unlock_device_hotplug();
a33ec399
RW
435}
436
0b224527
RW
437static void acpi_free_power_resources_lists(struct acpi_device *device)
438{
439 int i;
440
993cbe59
RW
441 if (device->wakeup.flags.valid)
442 acpi_power_resources_list_free(&device->wakeup.resources);
443
1b1f3e16 444 if (!device->power.flags.power_resources)
0b224527
RW
445 return;
446
447 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
448 struct acpi_device_power_state *ps = &device->power.states[i];
449 acpi_power_resources_list_free(&ps->resources);
450 }
7f47fa6c
BH
451}
452
1890a97a 453static void acpi_device_release(struct device *dev)
1da177e4 454{
1890a97a 455 struct acpi_device *acpi_dev = to_acpi_device(dev);
1da177e4 456
ffdcd955 457 acpi_free_properties(acpi_dev);
c0af4175 458 acpi_free_pnp_ids(&acpi_dev->pnp);
0b224527 459 acpi_free_power_resources_lists(acpi_dev);
1890a97a 460 kfree(acpi_dev);
1da177e4
LT
461}
462
d783156e
RW
463static void acpi_device_del(struct acpi_device *device)
464{
ca9dc8d4
LW
465 struct acpi_device_bus_id *acpi_device_bus_id;
466
d783156e 467 mutex_lock(&acpi_device_lock);
d783156e 468
ca9dc8d4
LW
469 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
470 if (!strcmp(acpi_device_bus_id->bus_id,
471 acpi_device_hid(device))) {
ad2f3b08
RW
472 ida_free(&acpi_device_bus_id->instance_ida,
473 device->pnp.instance_no);
eb50aaf9 474 if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
ca9dc8d4 475 list_del(&acpi_device_bus_id->node);
a58015d6 476 kfree_const(acpi_device_bus_id->bus_id);
ca9dc8d4
LW
477 kfree(acpi_device_bus_id);
478 }
479 break;
480 }
481
d783156e 482 list_del(&device->wakeup_list);
e6bdbcc7 483
d783156e
RW
484 mutex_unlock(&acpi_device_lock);
485
486 acpi_power_add_remove_device(device, false);
487 acpi_device_remove_files(device);
488 if (device->remove)
489 device->remove(device);
490
491 device_del(&device->dev);
492}
493
68bdb677
OP
494static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
495
d783156e
RW
496static LIST_HEAD(acpi_device_del_list);
497static DEFINE_MUTEX(acpi_device_del_lock);
498
499static void acpi_device_del_work_fn(struct work_struct *work_not_used)
500{
501 for (;;) {
502 struct acpi_device *adev;
503
504 mutex_lock(&acpi_device_del_lock);
505
506 if (list_empty(&acpi_device_del_list)) {
507 mutex_unlock(&acpi_device_del_lock);
508 break;
509 }
510 adev = list_first_entry(&acpi_device_del_list,
511 struct acpi_device, del_list);
512 list_del(&adev->del_list);
513
514 mutex_unlock(&acpi_device_del_lock);
515
68bdb677
OP
516 blocking_notifier_call_chain(&acpi_reconfig_chain,
517 ACPI_RECONFIG_DEVICE_REMOVE, adev);
518
d783156e
RW
519 acpi_device_del(adev);
520 /*
521 * Drop references to all power resources that might have been
522 * used by the device.
523 */
524 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
4cbaba4e 525 acpi_dev_put(adev);
d783156e
RW
526 }
527}
528
529/**
530 * acpi_scan_drop_device - Drop an ACPI device object.
531 * @handle: Handle of an ACPI namespace node, not used.
532 * @context: Address of the ACPI device object to drop.
533 *
534 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
535 * namespace node the device object pointed to by @context is attached to.
536 *
537 * The unregistration is carried out asynchronously to avoid running
538 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
539 * ensure the correct ordering (the device objects must be unregistered in the
540 * same order in which the corresponding namespace nodes are deleted).
541 */
542static void acpi_scan_drop_device(acpi_handle handle, void *context)
caf5c03f 543{
d783156e
RW
544 static DECLARE_WORK(work, acpi_device_del_work_fn);
545 struct acpi_device *adev = context;
546
547 mutex_lock(&acpi_device_del_lock);
548
549 /*
550 * Use the ACPI hotplug workqueue which is ordered, so this work item
551 * won't run after any hotplug work items submitted subsequently. That
552 * prevents attempts to register device objects identical to those being
553 * deleted from happening concurrently (such attempts result from
554 * hotplug events handled via the ACPI hotplug workqueue). It also will
935ab850 555 * run after all of the work items submitted previously, which helps
d783156e
RW
556 * those work items to ensure that they are not accessing stale device
557 * objects.
558 */
559 if (list_empty(&acpi_device_del_list))
560 acpi_queue_hotplug_work(&work);
561
562 list_add_tail(&adev->del_list, &acpi_device_del_list);
563 /* Make acpi_ns_validate_handle() return NULL for this handle. */
564 adev->handle = INVALID_ACPI_HANDLE;
565
566 mutex_unlock(&acpi_device_del_lock);
caf5c03f
RW
567}
568
83e2c8fc
RW
569static struct acpi_device *handle_to_device(acpi_handle handle,
570 void (*callback)(void *))
caf5c03f 571{
83e2c8fc 572 struct acpi_device *adev = NULL;
caf5c03f
RW
573 acpi_status status;
574
78ea4639 575 status = acpi_get_data_full(handle, acpi_scan_drop_device,
83e2c8fc
RW
576 (void **)&adev, callback);
577 if (ACPI_FAILURE(status) || !adev) {
578 acpi_handle_debug(handle, "No context!\n");
579 return NULL;
caf5c03f 580 }
83e2c8fc 581 return adev;
caf5c03f 582}
78ea4639 583
e3c963c4
RW
584/**
585 * acpi_fetch_acpi_dev - Retrieve ACPI device object.
586 * @handle: ACPI handle associated with the requested ACPI device object.
587 *
588 * Return a pointer to the ACPI device object associated with @handle, if
589 * present, or NULL otherwise.
590 */
591struct acpi_device *acpi_fetch_acpi_dev(acpi_handle handle)
592{
593 return handle_to_device(handle, NULL);
594}
595EXPORT_SYMBOL_GPL(acpi_fetch_acpi_dev);
596
78ea4639
RW
597static void get_acpi_device(void *dev)
598{
4cbaba4e 599 acpi_dev_get(dev);
78ea4639
RW
600}
601
45e9aa1f
RW
602/**
603 * acpi_get_acpi_dev - Retrieve ACPI device object and reference count it.
604 * @handle: ACPI handle associated with the requested ACPI device object.
605 *
606 * Return a pointer to the ACPI device object associated with @handle and bump
607 * up that object's reference counter (under the ACPI Namespace lock), if
608 * present, or return NULL otherwise.
609 *
610 * The ACPI device object reference acquired by this function needs to be
611 * dropped via acpi_dev_put().
612 */
613struct acpi_device *acpi_get_acpi_dev(acpi_handle handle)
78ea4639 614{
83e2c8fc 615 return handle_to_device(handle, get_acpi_device);
78ea4639 616}
45e9aa1f 617EXPORT_SYMBOL_GPL(acpi_get_acpi_dev);
78ea4639 618
c1013ff7
RW
619static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
620{
621 struct acpi_device_bus_id *acpi_device_bus_id;
622
623 /* Find suitable bus_id and instance number in acpi_bus_id_list. */
624 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
625 if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
626 return acpi_device_bus_id;
627 }
628 return NULL;
629}
630
eb50aaf9
AS
631static int acpi_device_set_name(struct acpi_device *device,
632 struct acpi_device_bus_id *acpi_device_bus_id)
633{
634 struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
635 int result;
636
ad2f3b08 637 result = ida_alloc(instance_ida, GFP_KERNEL);
eb50aaf9
AS
638 if (result < 0)
639 return result;
640
641 device->pnp.instance_no = result;
642 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
643 return 0;
644}
645
6e1850b2 646int acpi_tie_acpi_dev(struct acpi_device *adev)
1da177e4 647{
c6a493a1
RW
648 acpi_handle handle = adev->handle;
649 acpi_status status;
66b7ed40 650
c6a493a1
RW
651 if (!handle)
652 return 0;
d43e167d 653
c6a493a1
RW
654 status = acpi_attach_data(handle, acpi_scan_drop_device, adev);
655 if (ACPI_FAILURE(status)) {
656 acpi_handle_err(handle, "Unable to attach device data\n");
657 return -ENODEV;
d43e167d
RW
658 }
659
c6a493a1
RW
660 return 0;
661}
662
882c982d
HK
663static void acpi_store_pld_crc(struct acpi_device *adev)
664{
665 struct acpi_pld_info *pld;
666 acpi_status status;
667
668 status = acpi_get_physical_device_location(adev->handle, &pld);
669 if (ACPI_FAILURE(status))
670 return;
671
672 adev->pld_crc = crc32(~0, pld, sizeof(*pld));
673 ACPI_FREE(pld);
674}
675
6e1850b2 676int acpi_device_add(struct acpi_device *device)
1da177e4 677{
c1013ff7 678 struct acpi_device_bus_id *acpi_device_bus_id;
4be44fcd 679 int result;
66b7ed40 680
9e89dde2
ZR
681 /*
682 * Linkage
683 * -------
684 * Link this device to its parent and siblings.
685 */
9e89dde2 686 INIT_LIST_HEAD(&device->wakeup_list);
1033f904 687 INIT_LIST_HEAD(&device->physical_node_list);
d783156e 688 INIT_LIST_HEAD(&device->del_list);
1033f904 689 mutex_init(&device->physical_node_lock);
1da177e4 690
9090589d 691 mutex_lock(&acpi_device_lock);
c1013ff7
RW
692
693 acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
694 if (acpi_device_bus_id) {
eb50aaf9
AS
695 result = acpi_device_set_name(device, acpi_device_bus_id);
696 if (result)
697 goto err_unlock;
c1013ff7
RW
698 } else {
699 acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
700 GFP_KERNEL);
701 if (!acpi_device_bus_id) {
702 result = -ENOMEM;
703 goto err_unlock;
e49bd2dd 704 }
a58015d6
DC
705 acpi_device_bus_id->bus_id =
706 kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
707 if (!acpi_device_bus_id->bus_id) {
c1013ff7 708 kfree(acpi_device_bus_id);
a58015d6 709 result = -ENOMEM;
c1013ff7 710 goto err_unlock;
a58015d6
DC
711 }
712
eb50aaf9
AS
713 ida_init(&acpi_device_bus_id->instance_ida);
714
715 result = acpi_device_set_name(device, acpi_device_bus_id);
716 if (result) {
0c8bd174 717 kfree_const(acpi_device_bus_id->bus_id);
eb50aaf9
AS
718 kfree(acpi_device_bus_id);
719 goto err_unlock;
720 }
721
e49bd2dd 722 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1da177e4
LT
723 }
724
9e89dde2
ZR
725 if (device->wakeup.flags.valid)
726 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
5e73c518 727
882c982d
HK
728 acpi_store_pld_crc(device);
729
9090589d 730 mutex_unlock(&acpi_device_lock);
1da177e4 731
cf860be6 732 result = device_add(&device->dev);
0c526d96 733 if (result) {
8b12b922 734 dev_err(&device->dev, "Error registering device\n");
d43e167d 735 goto err;
1da177e4 736 }
1da177e4 737
e49bd2dd 738 result = acpi_device_setup_files(device);
0c526d96 739 if (result)
8acf4108 740 pr_err("Error creating sysfs interface for device %s\n",
0794469d 741 dev_name(&device->dev));
1da177e4 742
1da177e4 743 return 0;
d43e167d 744
5e73c518 745err:
9090589d 746 mutex_lock(&acpi_device_lock);
5e73c518 747
e49bd2dd 748 list_del(&device->wakeup_list);
a58015d6 749
5e73c518 750err_unlock:
9090589d 751 mutex_unlock(&acpi_device_lock);
d43e167d 752
d783156e 753 acpi_detach_data(device->handle, acpi_scan_drop_device);
5e73c518 754
e49bd2dd 755 return result;
1da177e4
LT
756}
757
9e89dde2
ZR
758/* --------------------------------------------------------------------------
759 Device Enumeration
760 -------------------------------------------------------------------------- */
2ef33ee7
RW
761static bool acpi_info_matches_ids(struct acpi_device_info *info,
762 const char * const ids[])
637b9f1a 763{
2ef33ee7 764 struct acpi_pnp_device_id_list *cid_list = NULL;
02050558 765 int i, index;
637b9f1a
HG
766
767 if (!(info->valid & ACPI_VALID_HID))
768 return false;
769
02050558
AS
770 index = match_string(ids, -1, info->hardware_id.string);
771 if (index >= 0)
772 return true;
773
2ef33ee7
RW
774 if (info->valid & ACPI_VALID_CID)
775 cid_list = &info->compatible_id_list;
776
02050558
AS
777 if (!cid_list)
778 return false;
2ef33ee7 779
02050558
AS
780 for (i = 0; i < cid_list->count; i++) {
781 index = match_string(ids, -1, cid_list->ids[i].string);
782 if (index >= 0)
637b9f1a
HG
783 return true;
784 }
785
786 return false;
787}
788
789/* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
2ef33ee7 790static const char * const acpi_ignore_dep_ids[] = {
2ef33ee7 791 "PNP0D80", /* Windows-compatible System Power Management Controller */
9272e97a 792 "INT33BD", /* Intel Baytrail Mailbox Device */
637b9f1a
HG
793 NULL
794};
795
9d9bcae4
HG
796/* List of HIDs for which we honor deps of matching ACPI devs, when checking _DEP lists. */
797static const char * const acpi_honor_dep_ids[] = {
798 "INT3472", /* Camera sensor PMIC / clk and regulator info */
799 NULL
800};
801
f6f1e12f 802static struct acpi_device *acpi_find_parent_acpi_dev(acpi_handle handle)
5c478f49 803{
f6f1e12f 804 struct acpi_device *adev;
5c478f49
BH
805
806 /*
807 * Fixed hardware devices do not appear in the namespace and do not
808 * have handles, but we fabricate acpi_devices for them, so we have
809 * to deal with them specially.
810 */
456de893 811 if (!handle)
5c478f49
BH
812 return acpi_root;
813
814 do {
f6f1e12f
RW
815 acpi_status status;
816
5c478f49 817 status = acpi_get_parent(handle, &handle);
f6f1e12f
RW
818 if (ACPI_FAILURE(status)) {
819 if (status != AE_NULL_ENTRY)
820 return acpi_root;
e3c963c4 821
f6f1e12f
RW
822 return NULL;
823 }
824 adev = acpi_fetch_acpi_dev(handle);
825 } while (!adev);
826 return adev;
5c478f49
BH
827}
828
9e89dde2
ZR
829acpi_status
830acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
831{
832 acpi_status status;
833 acpi_handle tmp;
834 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
835 union acpi_object *obj;
836
837 status = acpi_get_handle(handle, "_EJD", &tmp);
838 if (ACPI_FAILURE(status))
839 return status;
840
841 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
842 if (ACPI_SUCCESS(status)) {
843 obj = buffer.pointer;
3b5fee59
HM
844 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
845 ejd);
9e89dde2
ZR
846 kfree(buffer.pointer);
847 }
848 return status;
849}
850EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
851
40381a3c 852static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
9e89dde2 853{
40381a3c
BH
854 acpi_handle handle = dev->handle;
855 struct acpi_device_wakeup *wakeup = &dev->wakeup;
b581a7f9
RW
856 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
857 union acpi_object *package = NULL;
9e89dde2 858 union acpi_object *element = NULL;
b581a7f9 859 acpi_status status;
e88c9c60 860 int err = -ENODATA;
9e89dde2 861
993cbe59 862 INIT_LIST_HEAD(&wakeup->resources);
9e89dde2 863
b581a7f9
RW
864 /* _PRW */
865 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
866 if (ACPI_FAILURE(status)) {
e52d9d8c
RW
867 acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
868 acpi_format_exception(status));
e88c9c60 869 return err;
b581a7f9
RW
870 }
871
872 package = (union acpi_object *)buffer.pointer;
873
e88c9c60 874 if (!package || package->package.count < 2)
b581a7f9 875 goto out;
b581a7f9 876
9e89dde2 877 element = &(package->package.elements[0]);
e88c9c60 878 if (!element)
b581a7f9 879 goto out;
e88c9c60 880
9e89dde2
ZR
881 if (element->type == ACPI_TYPE_PACKAGE) {
882 if ((element->package.count < 2) ||
883 (element->package.elements[0].type !=
884 ACPI_TYPE_LOCAL_REFERENCE)
e88c9c60 885 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
b581a7f9 886 goto out;
e88c9c60 887
b581a7f9 888 wakeup->gpe_device =
9e89dde2 889 element->package.elements[0].reference.handle;
b581a7f9 890 wakeup->gpe_number =
9e89dde2
ZR
891 (u32) element->package.elements[1].integer.value;
892 } else if (element->type == ACPI_TYPE_INTEGER) {
b581a7f9
RW
893 wakeup->gpe_device = NULL;
894 wakeup->gpe_number = element->integer.value;
895 } else {
b581a7f9
RW
896 goto out;
897 }
9e89dde2
ZR
898
899 element = &(package->package.elements[1]);
e88c9c60 900 if (element->type != ACPI_TYPE_INTEGER)
b581a7f9 901 goto out;
e88c9c60 902
b581a7f9 903 wakeup->sleep_state = element->integer.value;
9e89dde2 904
e88c9c60
RW
905 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
906 if (err)
b581a7f9 907 goto out;
9e89dde2 908
0596a52b
RW
909 if (!list_empty(&wakeup->resources)) {
910 int sleep_state;
9e89dde2 911
b5d667eb
RW
912 err = acpi_power_wakeup_list_init(&wakeup->resources,
913 &sleep_state);
914 if (err) {
915 acpi_handle_warn(handle, "Retrieving current states "
916 "of wakeup power resources failed\n");
917 acpi_power_resources_list_free(&wakeup->resources);
918 goto out;
919 }
0596a52b
RW
920 if (sleep_state < wakeup->sleep_state) {
921 acpi_handle_warn(handle, "Overriding _PRW sleep state "
922 "(S%d) by S%d from power resources\n",
923 (int)wakeup->sleep_state, sleep_state);
924 wakeup->sleep_state = sleep_state;
925 }
926 }
9874647b 927
b581a7f9
RW
928 out:
929 kfree(buffer.pointer);
e88c9c60 930 return err;
1da177e4
LT
931}
932
a1a66393 933static bool acpi_wakeup_gpe_init(struct acpi_device *device)
1da177e4 934{
0519ade7 935 static const struct acpi_device_id button_device_ids[] = {
5ceb5f05
BH
936 {"PNP0C0C", 0}, /* Power button */
937 {"PNP0C0D", 0}, /* Lid */
938 {"PNP0C0E", 0}, /* Sleep button */
29b71a1c
TR
939 {"", 0},
940 };
bd9b2f9a 941 struct acpi_device_wakeup *wakeup = &device->wakeup;
f517709d 942 acpi_status status;
f517709d 943
bd9b2f9a 944 wakeup->flags.notifier_present = 0;
f517709d
RW
945
946 /* Power button, Lid switch always enable wakeup */
947 if (!acpi_match_device_ids(device, button_device_ids)) {
b7e38304
ZR
948 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
949 /* Do not use Lid/sleep button for S5 wakeup */
bd9b2f9a
RW
950 if (wakeup->sleep_state == ACPI_STATE_S5)
951 wakeup->sleep_state = ACPI_STATE_S4;
b7e38304 952 }
bd9b2f9a 953 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
f2b56bc8 954 device_set_wakeup_capable(&device->dev, true);
a1a66393 955 return true;
f517709d
RW
956 }
957
a1a66393
RW
958 status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
959 wakeup->gpe_number);
960 return ACPI_SUCCESS(status);
f517709d
RW
961}
962
d57d09a4 963static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
f517709d 964{
e88c9c60 965 int err;
29b71a1c 966
d57d09a4 967 /* Presence of _PRW indicates wake capable */
952c63e9 968 if (!acpi_has_method(device->handle, "_PRW"))
d57d09a4
RW
969 return;
970
40381a3c 971 err = acpi_bus_extract_wakeup_device_power_package(device);
e88c9c60 972 if (err) {
e52d9d8c 973 dev_err(&device->dev, "Unable to extract wakeup power resources");
d57d09a4 974 return;
9e89dde2 975 }
1da177e4 976
a1a66393 977 device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
9b83ccd2 978 device->wakeup.prepare_count = 0;
a1a66393
RW
979 /*
980 * Call _PSW/_DSW object to disable its ability to wake the sleeping
729b2bdb 981 * system for the ACPI device with the _PRW object.
603fadf3 982 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
729b2bdb
ZY
983 * So it is necessary to call _DSW object first. Only when it is not
984 * present will the _PSW object used.
985 */
e88c9c60
RW
986 err = acpi_device_sleep_wake(device, 0, 0, 0);
987 if (err)
05de0686 988 pr_debug("error in _DSW or _PSW evaluation\n");
1da177e4 989}
1da177e4 990
f33ce568
RW
991static void acpi_bus_init_power_state(struct acpi_device *device, int state)
992{
993 struct acpi_device_power_state *ps = &device->power.states[state];
ef85bdbe
RW
994 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
995 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
f33ce568 996 acpi_status status;
bf325f95 997
f33ce568
RW
998 INIT_LIST_HEAD(&ps->resources);
999
ef85bdbe
RW
1000 /* Evaluate "_PRx" to get referenced power resources */
1001 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1002 if (ACPI_SUCCESS(status)) {
1003 union acpi_object *package = buffer.pointer;
1004
1005 if (buffer.length && package
1006 && package->type == ACPI_TYPE_PACKAGE
956ad9d9
RW
1007 && package->package.count)
1008 acpi_extract_power_resources(package, 0, &ps->resources);
1009
ef85bdbe 1010 ACPI_FREE(buffer.pointer);
f33ce568
RW
1011 }
1012
1013 /* Evaluate "_PSx" to see if we can do explicit sets */
ef85bdbe 1014 pathname[2] = 'S';
952c63e9 1015 if (acpi_has_method(device->handle, pathname))
f33ce568
RW
1016 ps->flags.explicit_set = 1;
1017
20dacb71
RW
1018 /* State is valid if there are means to put the device into it. */
1019 if (!list_empty(&ps->resources) || ps->flags.explicit_set)
f33ce568 1020 ps->flags.valid = 1;
f33ce568
RW
1021
1022 ps->power = -1; /* Unknown - driver assigned */
1023 ps->latency = -1; /* Unknown - driver assigned */
1024}
1025
d43e167d 1026static void acpi_bus_get_power_flags(struct acpi_device *device)
1da177e4 1027{
b340c7d6 1028 unsigned long long dsc = ACPI_STATE_D0;
8bc5053b 1029 u32 i;
1a365616 1030
d43e167d 1031 /* Presence of _PS0|_PR0 indicates 'power manageable' */
952c63e9
JL
1032 if (!acpi_has_method(device->handle, "_PS0") &&
1033 !acpi_has_method(device->handle, "_PR0"))
1034 return;
1a365616 1035
d43e167d 1036 device->flags.power_manageable = 1;
4be44fcd 1037
9e89dde2
ZR
1038 /*
1039 * Power Management Flags
1040 */
952c63e9 1041 if (acpi_has_method(device->handle, "_PSC"))
9e89dde2 1042 device->power.flags.explicit_get = 1;
f25c0ae2 1043
952c63e9 1044 if (acpi_has_method(device->handle, "_IRC"))
9e89dde2 1045 device->power.flags.inrush_current = 1;
1da177e4 1046
f25c0ae2
RW
1047 if (acpi_has_method(device->handle, "_DSW"))
1048 device->power.flags.dsw_present = 1;
1049
b340c7d6
SA
1050 acpi_evaluate_integer(device->handle, "_DSC", NULL, &dsc);
1051 device->power.state_for_enumeration = dsc;
1052
9e89dde2
ZR
1053 /*
1054 * Enumerate supported power management states
1055 */
f33ce568
RW
1056 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1057 acpi_bus_init_power_state(device, i);
bf325f95 1058
0b224527 1059 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1da177e4 1060
956ad9d9 1061 /* Set the defaults for D0 and D3hot (always supported). */
9e89dde2
ZR
1062 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1063 device->power.states[ACPI_STATE_D0].power = 100;
20dacb71 1064 device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1399dfcd 1065
956ad9d9
RW
1066 /*
1067 * Use power resources only if the D0 list of them is populated, because
1068 * some platforms may provide _PR3 only to indicate D3cold support and
1069 * in those cases the power resources list returned by it may be bogus.
1070 */
1071 if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1072 device->power.flags.power_resources = 1;
1073 /*
1074 * D3cold is supported if the D3hot list of power resources is
1075 * not empty.
1076 */
1077 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1078 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1079 }
1080
1b1f3e16 1081 if (acpi_bus_init_power(device))
b3785492 1082 device->flags.power_manageable = 0;
9e89dde2 1083}
c8f7a62c 1084
d43e167d 1085static void acpi_bus_get_flags(struct acpi_device *device)
1da177e4 1086{
1da177e4 1087 /* Presence of _STA indicates 'dynamic_status' */
952c63e9 1088 if (acpi_has_method(device->handle, "_STA"))
1da177e4
LT
1089 device->flags.dynamic_status = 1;
1090
1da177e4 1091 /* Presence of _RMV indicates 'removable' */
952c63e9 1092 if (acpi_has_method(device->handle, "_RMV"))
1da177e4
LT
1093 device->flags.removable = 1;
1094
1095 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
952c63e9
JL
1096 if (acpi_has_method(device->handle, "_EJD") ||
1097 acpi_has_method(device->handle, "_EJ0"))
1da177e4 1098 device->flags.ejectable = 1;
1da177e4
LT
1099}
1100
c7bcb4e9 1101static void acpi_device_get_busid(struct acpi_device *device)
1da177e4 1102{
4be44fcd
LB
1103 char bus_id[5] = { '?', 0 };
1104 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1105 int i = 0;
1da177e4
LT
1106
1107 /*
1108 * Bus ID
1109 * ------
1110 * The device's Bus ID is simply the object name.
1111 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1112 */
859ac9a4 1113 if (ACPI_IS_ROOT_DEVICE(device)) {
1da177e4 1114 strcpy(device->pnp.bus_id, "ACPI");
859ac9a4
BH
1115 return;
1116 }
1117
1118 switch (device->device_type) {
1da177e4
LT
1119 case ACPI_BUS_TYPE_POWER_BUTTON:
1120 strcpy(device->pnp.bus_id, "PWRF");
1121 break;
1122 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1123 strcpy(device->pnp.bus_id, "SLPF");
1124 break;
a64a62ce
LZ
1125 case ACPI_BUS_TYPE_ECDT_EC:
1126 strcpy(device->pnp.bus_id, "ECDT");
1127 break;
1da177e4 1128 default:
66b7ed40 1129 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1da177e4
LT
1130 /* Clean up trailing underscores (if any) */
1131 for (i = 3; i > 1; i--) {
1132 if (bus_id[i] == '_')
1133 bus_id[i] = '\0';
1134 else
1135 break;
1136 }
1137 strcpy(device->pnp.bus_id, bus_id);
1138 break;
1139 }
1140}
1141
ebf4df8d
JL
1142/*
1143 * acpi_ata_match - see if an acpi object is an ATA device
1144 *
1145 * If an acpi object has one of the ACPI ATA methods defined,
1146 * then we can safely call it an ATA device.
1147 */
1148bool acpi_ata_match(acpi_handle handle)
1149{
1150 return acpi_has_method(handle, "_GTF") ||
1151 acpi_has_method(handle, "_GTM") ||
1152 acpi_has_method(handle, "_STM") ||
1153 acpi_has_method(handle, "_SDD");
1154}
1155
54735266 1156/*
d4e1a692 1157 * acpi_bay_match - see if an acpi object is an ejectable driver bay
54735266
ZR
1158 *
1159 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1160 * then we can safely call it an ejectable drive bay
1161 */
ebf4df8d 1162bool acpi_bay_match(acpi_handle handle)
d4e1a692 1163{
54735266
ZR
1164 acpi_handle phandle;
1165
952c63e9 1166 if (!acpi_has_method(handle, "_EJ0"))
ebf4df8d
JL
1167 return false;
1168 if (acpi_ata_match(handle))
1169 return true;
1170 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1171 return false;
54735266 1172
ebf4df8d 1173 return acpi_ata_match(phandle);
54735266
ZR
1174}
1175
b43109fa 1176bool acpi_device_is_battery(struct acpi_device *adev)
1e2380cd 1177{
b43109fa 1178 struct acpi_hardware_id *hwid;
1e2380cd 1179
b43109fa
RW
1180 list_for_each_entry(hwid, &adev->pnp.ids, list)
1181 if (!strcmp("PNP0C0A", hwid->id))
1182 return true;
1e2380cd 1183
b43109fa 1184 return false;
1e2380cd
RW
1185}
1186
b43109fa 1187static bool is_ejectable_bay(struct acpi_device *adev)
1e2380cd 1188{
b43109fa
RW
1189 acpi_handle handle = adev->handle;
1190
1191 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1e2380cd
RW
1192 return true;
1193
1194 return acpi_bay_match(handle);
1195}
1196
3620f2f2 1197/*
d4e1a692 1198 * acpi_dock_match - see if an acpi object has a _DCK method
3620f2f2 1199 */
ebf4df8d 1200bool acpi_dock_match(acpi_handle handle)
3620f2f2 1201{
ebf4df8d 1202 return acpi_has_method(handle, "_DCK");
3620f2f2
FS
1203}
1204
adc8bb8e
HG
1205static acpi_status
1206acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1207 void **return_value)
1208{
1209 long *cap = context;
1210
1211 if (acpi_has_method(handle, "_BCM") &&
1212 acpi_has_method(handle, "_BCL")) {
e52d9d8c 1213 acpi_handle_debug(handle, "Found generic backlight support\n");
adc8bb8e 1214 *cap |= ACPI_VIDEO_BACKLIGHT;
adc8bb8e
HG
1215 /* We have backlight support, no need to scan further */
1216 return AE_CTRL_TERMINATE;
1217 }
1218 return 0;
1219}
1220
1221/* Returns true if the ACPI object is a video device which can be
1222 * handled by video.ko.
1223 * The device will get a Linux specific CID added in scan.c to
1224 * identify the device as an ACPI graphics device
1225 * Be aware that the graphics device may not be physically present
1226 * Use acpi_video_get_capabilities() to detect general ACPI video
1227 * capabilities of present cards
1228 */
1229long acpi_is_video_device(acpi_handle handle)
1230{
1231 long video_caps = 0;
1232
1233 /* Is this device able to support video switching ? */
1234 if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1235 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1236
1237 /* Is this device able to retrieve a video ROM ? */
1238 if (acpi_has_method(handle, "_ROM"))
1239 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1240
1241 /* Is this device able to configure which video head to be POSTed ? */
1242 if (acpi_has_method(handle, "_VPO") &&
1243 acpi_has_method(handle, "_GPD") &&
1244 acpi_has_method(handle, "_SPD"))
1245 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1246
1247 /* Only check for backlight functionality if one of the above hit. */
1248 if (video_caps)
1249 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1250 ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1251 &video_caps, NULL);
1252
1253 return video_caps;
1254}
1255EXPORT_SYMBOL(acpi_is_video_device);
1256
620e112c 1257const char *acpi_device_hid(struct acpi_device *device)
15b8dd53 1258{
7f47fa6c 1259 struct acpi_hardware_id *hid;
15b8dd53 1260
2b2ae7c7
TR
1261 if (list_empty(&device->pnp.ids))
1262 return dummy_hid;
1263
7f47fa6c
BH
1264 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1265 return hid->id;
1266}
1267EXPORT_SYMBOL(acpi_device_hid);
15b8dd53 1268
d4e1a692 1269static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
7f47fa6c
BH
1270{
1271 struct acpi_hardware_id *id;
15b8dd53 1272
7f47fa6c
BH
1273 id = kmalloc(sizeof(*id), GFP_KERNEL);
1274 if (!id)
1275 return;
15b8dd53 1276
6a0d12ef 1277 id->id = kstrdup_const(dev_id, GFP_KERNEL);
7f47fa6c
BH
1278 if (!id->id) {
1279 kfree(id);
1280 return;
15b8dd53
BM
1281 }
1282
d4e1a692
TK
1283 list_add_tail(&id->list, &pnp->ids);
1284 pnp->type.hardware_id = 1;
15b8dd53
BM
1285}
1286
222e82ac
DW
1287/*
1288 * Old IBM workstations have a DSDT bug wherein the SMBus object
1289 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1290 * prefix. Work around this.
1291 */
ebf4df8d 1292static bool acpi_ibm_smbus_match(acpi_handle handle)
222e82ac 1293{
ebf4df8d
JL
1294 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1295 struct acpi_buffer path = { sizeof(node_name), node_name };
222e82ac
DW
1296
1297 if (!dmi_name_in_vendors("IBM"))
ebf4df8d 1298 return false;
222e82ac
DW
1299
1300 /* Look for SMBS object */
ebf4df8d
JL
1301 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1302 strcmp("SMBS", path.pointer))
1303 return false;
222e82ac
DW
1304
1305 /* Does it have the necessary (but misnamed) methods? */
952c63e9
JL
1306 if (acpi_has_method(handle, "SBI") &&
1307 acpi_has_method(handle, "SBR") &&
1308 acpi_has_method(handle, "SBW"))
ebf4df8d
JL
1309 return true;
1310
1311 return false;
222e82ac
DW
1312}
1313
ae3caa80
JL
1314static bool acpi_object_is_system_bus(acpi_handle handle)
1315{
1316 acpi_handle tmp;
1317
1318 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1319 tmp == handle)
1320 return true;
1321 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1322 tmp == handle)
1323 return true;
1324
1325 return false;
1326}
1327
c0af4175 1328static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
c830dbcf 1329 int device_type)
1da177e4 1330{
c830dbcf 1331 struct acpi_device_info *info = NULL;
78e25fef 1332 struct acpi_pnp_device_id_list *cid_list;
7f47fa6c 1333 int i;
1da177e4 1334
c0af4175 1335 switch (device_type) {
1da177e4 1336 case ACPI_BUS_TYPE_DEVICE:
c0af4175
TK
1337 if (handle == ACPI_ROOT_OBJECT) {
1338 acpi_add_id(pnp, ACPI_SYSTEM_HID);
859ac9a4
BH
1339 break;
1340 }
1341
c830dbcf 1342 acpi_get_object_info(handle, &info);
6091b263 1343 if (!info) {
8acf4108 1344 pr_err("%s: Error reading device info\n", __func__);
1da177e4
LT
1345 return;
1346 }
1347
549e6845 1348 if (info->valid & ACPI_VALID_HID) {
c0af4175 1349 acpi_add_id(pnp, info->hardware_id.string);
549e6845 1350 pnp->type.platform_id = 1;
462ccc35
RW
1351 }
1352 if (info->valid & ACPI_VALID_CID) {
1353 cid_list = &info->compatible_id_list;
1354 for (i = 0; i < cid_list->count; i++)
1355 acpi_add_id(pnp, cid_list->ids[i].string);
57f3674f 1356 }
1da177e4 1357 if (info->valid & ACPI_VALID_ADR) {
c0af4175
TK
1358 pnp->bus_address = info->address;
1359 pnp->type.bus_address = 1;
1da177e4 1360 }
ccf78040 1361 if (info->valid & ACPI_VALID_UID)
c0af4175 1362 pnp->unique_id = kstrdup(info->unique_id.string,
ccf78040 1363 GFP_KERNEL);
26095a01
SS
1364 if (info->valid & ACPI_VALID_CLS)
1365 acpi_add_id(pnp, info->class_code.string);
ae843332 1366
c830dbcf
RW
1367 kfree(info);
1368
57f3674f
BH
1369 /*
1370 * Some devices don't reliably have _HIDs & _CIDs, so add
1371 * synthetic HIDs to make sure drivers can find them.
1372 */
c0af4175
TK
1373 if (acpi_is_video_device(handle))
1374 acpi_add_id(pnp, ACPI_VIDEO_HID);
ebf4df8d 1375 else if (acpi_bay_match(handle))
c0af4175 1376 acpi_add_id(pnp, ACPI_BAY_HID);
ebf4df8d 1377 else if (acpi_dock_match(handle))
c0af4175 1378 acpi_add_id(pnp, ACPI_DOCK_HID);
ebf4df8d 1379 else if (acpi_ibm_smbus_match(handle))
c0af4175 1380 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
ae3caa80
JL
1381 else if (list_empty(&pnp->ids) &&
1382 acpi_object_is_system_bus(handle)) {
1383 /* \_SB, \_TZ, LNXSYBUS */
1384 acpi_add_id(pnp, ACPI_BUS_HID);
c0af4175
TK
1385 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1386 strcpy(pnp->device_class, ACPI_BUS_CLASS);
b7b30de5 1387 }
54735266 1388
1da177e4
LT
1389 break;
1390 case ACPI_BUS_TYPE_POWER:
c0af4175 1391 acpi_add_id(pnp, ACPI_POWER_HID);
1da177e4
LT
1392 break;
1393 case ACPI_BUS_TYPE_PROCESSOR:
c0af4175 1394 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1da177e4 1395 break;
1da177e4 1396 case ACPI_BUS_TYPE_THERMAL:
c0af4175 1397 acpi_add_id(pnp, ACPI_THERMAL_HID);
1da177e4
LT
1398 break;
1399 case ACPI_BUS_TYPE_POWER_BUTTON:
c0af4175 1400 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1da177e4
LT
1401 break;
1402 case ACPI_BUS_TYPE_SLEEP_BUTTON:
c0af4175 1403 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1da177e4 1404 break;
a64a62ce
LZ
1405 case ACPI_BUS_TYPE_ECDT_EC:
1406 acpi_add_id(pnp, ACPI_ECDT_HID);
1407 break;
1da177e4 1408 }
1da177e4
LT
1409}
1410
c0af4175
TK
1411void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1412{
1413 struct acpi_hardware_id *id, *tmp;
1414
1415 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
6a0d12ef 1416 kfree_const(id->id);
c0af4175
TK
1417 kfree(id);
1418 }
1419 kfree(pnp->unique_id);
1420}
1421
b84f196d
SS
1422/**
1423 * acpi_dma_supported - Check DMA support for the specified device.
1424 * @adev: The pointer to acpi device
1425 *
1426 * Return false if DMA is not supported. Otherwise, return true
1427 */
3d7c821c 1428bool acpi_dma_supported(const struct acpi_device *adev)
b84f196d
SS
1429{
1430 if (!adev)
1431 return false;
1432
1433 if (adev->flags.cca_seen)
1434 return true;
1435
1436 /*
1437 * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1438 * DMA on "Intel platforms". Presumably that includes all x86 and
1439 * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1440 */
1441 if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1442 return true;
1443
1444 return false;
1445}
1446
1447/**
1448 * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1449 * @adev: The pointer to acpi device
1450 *
1451 * Return enum dev_dma_attr.
1452 */
1453enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1454{
1455 if (!acpi_dma_supported(adev))
1456 return DEV_DMA_NOT_SUPPORTED;
1457
1458 if (adev->flags.coherent_dma)
1459 return DEV_DMA_COHERENT;
1460 else
1461 return DEV_DMA_NON_COHERENT;
1462}
1463
c04ac679
LP
1464/**
1465 * acpi_dma_get_range() - Get device DMA parameters.
1466 *
1467 * @dev: device to configure
1468 * @dma_addr: pointer device DMA address result
1469 * @offset: pointer to the DMA offset result
1470 * @size: pointer to DMA range size result
1471 *
1472 * Evaluate DMA regions and return respectively DMA region start, offset
1473 * and size in dma_addr, offset and size on parsing success; it does not
1474 * update the passed in values on failure.
1475 *
1476 * Return 0 on success, < 0 on failure.
1477 */
1478int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1479 u64 *size)
1480{
1481 struct acpi_device *adev;
1482 LIST_HEAD(list);
1483 struct resource_entry *rentry;
1484 int ret;
1485 struct device *dma_dev = dev;
1486 u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1487
1488 /*
1489 * Walk the device tree chasing an ACPI companion with a _DMA
1490 * object while we go. Stop if we find a device with an ACPI
1491 * companion containing a _DMA method.
1492 */
1493 do {
1494 adev = ACPI_COMPANION(dma_dev);
1495 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1496 break;
1497
1498 dma_dev = dma_dev->parent;
1499 } while (dma_dev);
1500
1501 if (!dma_dev)
1502 return -ENODEV;
1503
1504 if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1505 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1506 return -EINVAL;
1507 }
1508
1509 ret = acpi_dev_get_dma_resources(adev, &list);
1510 if (ret > 0) {
1511 list_for_each_entry(rentry, &list, node) {
1512 if (dma_offset && rentry->offset != dma_offset) {
1513 ret = -EINVAL;
1514 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1515 goto out;
1516 }
1517 dma_offset = rentry->offset;
1518
1519 /* Take lower and upper limits */
1520 if (rentry->res->start < dma_start)
1521 dma_start = rentry->res->start;
1522 if (rentry->res->end > dma_end)
1523 dma_end = rentry->res->end;
1524 }
1525
1526 if (dma_start >= dma_end) {
1527 ret = -EINVAL;
1528 dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1529 goto out;
1530 }
1531
1532 *dma_addr = dma_start - dma_offset;
1533 len = dma_end - dma_start;
1534 *size = max(len, len + 1);
1535 *offset = dma_offset;
1536 }
1537 out:
1538 acpi_dev_free_resource_list(&list);
1539
1540 return ret >= 0 ? 0 : ret;
1541}
1542
11a8c5e3
JPB
1543#ifdef CONFIG_IOMMU_API
1544int acpi_iommu_fwspec_init(struct device *dev, u32 id,
1545 struct fwnode_handle *fwnode,
1546 const struct iommu_ops *ops)
1547{
1548 int ret = iommu_fwspec_init(dev, fwnode, ops);
1549
1550 if (!ret)
1551 ret = iommu_fwspec_add_ids(dev, &id, 1);
1552
1553 return ret;
1554}
1555
1556static inline const struct iommu_ops *acpi_iommu_fwspec_ops(struct device *dev)
1557{
1558 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1559
1560 return fwspec ? fwspec->ops : NULL;
1561}
1562
1563static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
1564 const u32 *id_in)
1565{
1566 int err;
1567 const struct iommu_ops *ops;
1568
1569 /*
1570 * If we already translated the fwspec there is nothing left to do,
1571 * return the iommu_ops.
1572 */
1573 ops = acpi_iommu_fwspec_ops(dev);
1574 if (ops)
1575 return ops;
1576
1577 err = iort_iommu_configure_id(dev, id_in);
3cf48554
JPB
1578 if (err && err != -EPROBE_DEFER)
1579 err = viot_iommu_configure(dev);
11a8c5e3
JPB
1580
1581 /*
1582 * If we have reason to believe the IOMMU driver missed the initial
1583 * iommu_probe_device() call for dev, replay it to get things in order.
1584 */
1585 if (!err && dev->bus && !device_iommu_mapped(dev))
1586 err = iommu_probe_device(dev);
1587
1588 /* Ignore all other errors apart from EPROBE_DEFER */
1589 if (err == -EPROBE_DEFER) {
1590 return ERR_PTR(err);
1591 } else if (err) {
1592 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1593 return NULL;
1594 }
1595 return acpi_iommu_fwspec_ops(dev);
1596}
1597
1598#else /* !CONFIG_IOMMU_API */
1599
1600int acpi_iommu_fwspec_init(struct device *dev, u32 id,
1601 struct fwnode_handle *fwnode,
1602 const struct iommu_ops *ops)
1603{
1604 return -ENODEV;
1605}
1606
1607static const struct iommu_ops *acpi_iommu_configure_id(struct device *dev,
1608 const u32 *id_in)
1609{
1610 return NULL;
1611}
1612
1613#endif /* !CONFIG_IOMMU_API */
1614
d760a1ba 1615/**
c1e97359 1616 * acpi_dma_configure_id - Set-up DMA configuration for the device.
d760a1ba
LP
1617 * @dev: The pointer to the device
1618 * @attr: device dma attributes
b8e069a2 1619 * @input_id: input device id const value pointer
d760a1ba 1620 */
b8e069a2
LP
1621int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1622 const u32 *input_id)
d760a1ba 1623{
643b8e4d 1624 const struct iommu_ops *iommu;
7ad42639 1625 u64 dma_addr = 0, size = 0;
643b8e4d 1626
e5361ca2
RM
1627 if (attr == DEV_DMA_NOT_SUPPORTED) {
1628 set_dma_ops(dev, &dma_dummy_ops);
1629 return 0;
1630 }
1631
db59e1b6 1632 acpi_arch_dma_setup(dev, &dma_addr, &size);
d760a1ba 1633
11a8c5e3 1634 iommu = acpi_iommu_configure_id(dev, input_id);
45586c70 1635 if (PTR_ERR(iommu) == -EPROBE_DEFER)
058f8c3f 1636 return -EPROBE_DEFER;
643b8e4d 1637
7ad42639
LP
1638 arch_setup_dma_ops(dev, dma_addr, size,
1639 iommu, attr == DEV_DMA_COHERENT);
5a1bb638
S
1640
1641 return 0;
d760a1ba 1642}
b8e069a2 1643EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
d760a1ba 1644
d0562674
SS
1645static void acpi_init_coherency(struct acpi_device *adev)
1646{
1647 unsigned long long cca = 0;
1648 acpi_status status;
1649 struct acpi_device *parent = adev->parent;
1650
1651 if (parent && parent->flags.cca_seen) {
1652 /*
1653 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1654 * already saw one.
1655 */
1656 adev->flags.cca_seen = 1;
1657 cca = parent->flags.coherent_dma;
1658 } else {
1659 status = acpi_evaluate_integer(adev->handle, "_CCA",
1660 NULL, &cca);
1661 if (ACPI_SUCCESS(status))
1662 adev->flags.cca_seen = 1;
1663 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1664 /*
1665 * If architecture does not specify that _CCA is
1666 * required for DMA-able devices (e.g. x86),
1667 * we default to _CCA=1.
1668 */
1669 cca = 1;
1670 else
1671 acpi_handle_debug(adev->handle,
1672 "ACPI device is missing _CCA.\n");
1673 }
1674
1675 adev->flags.coherent_dma = cca;
1676}
1677
e361d1f8 1678static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
e4330d8b 1679{
e361d1f8 1680 bool *is_serial_bus_slave_p = data;
e4330d8b
JN
1681
1682 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1683 return 1;
1684
e361d1f8 1685 *is_serial_bus_slave_p = true;
e4330d8b
JN
1686
1687 /* no need to do more checking */
1688 return -1;
1689}
1690
dfda4492
JG
1691static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1692{
1693 struct acpi_device *parent = device->parent;
a6f07295 1694 static const struct acpi_device_id indirect_io_hosts[] = {
dfda4492
JG
1695 {"HISI0191", 0},
1696 {}
1697 };
1698
1699 return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1700}
1701
d87fb091 1702static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
e4330d8b
JN
1703{
1704 struct list_head resource_list;
e361d1f8 1705 bool is_serial_bus_slave = false;
f85196bd 1706 static const struct acpi_device_id ignore_serial_bus_ids[] = {
aba94139 1707 /*
5e63b2ea
LT
1708 * These devices have multiple SerialBus resources and a client
1709 * device must be instantiated for each of them, each with
1710 * its own device id.
1711 * Normally we only instantiate one client device for the first
1712 * resource, using the ACPI HID as id. These special cases are handled
1713 * by the drivers/platform/x86/serial-multi-instantiate.c driver, which
1714 * knows which client device id to use for each resource.
aba94139 1715 */
aba94139 1716 {"BSG1160", },
96f984d3 1717 {"BSG2150", },
d9c01c53 1718 {"CSC3551", },
589edb56 1719 {"INT33FE", },
a3dd034a 1720 {"INT3515", },
d9c01c53
LT
1721 /* Non-conforming _HID for Cirrus Logic already released */
1722 {"CLSA0100", },
87eb04bb 1723 {"CLSA0101", },
60c7353c
HG
1724 /*
1725 * Some ACPI devs contain SerialBus resources even though they are not
1726 * attached to a serial bus at all.
1727 */
1728 {"MSHW0028", },
f85196bd
HG
1729 /*
1730 * HIDs of device with an UartSerialBusV2 resource for which userspace
1731 * expects a regular tty cdev to be created (instead of the in kernel
1732 * serdev) and which have a kernel driver which expects a platform_dev
1733 * such as the rfkill-gpio driver.
1734 */
1735 {"BCM4752", },
1736 {"LNV4752", },
aba94139
HG
1737 {}
1738 };
e4330d8b 1739
dfda4492
JG
1740 if (acpi_is_indirect_io_slave(device))
1741 return true;
1742
ca9ef3ab
LW
1743 /* Macs use device properties in lieu of _CRS resources */
1744 if (x86_apple_machine &&
1745 (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
e361d1f8
FD
1746 fwnode_property_present(&device->fwnode, "i2cAddress") ||
1747 fwnode_property_present(&device->fwnode, "baud")))
ca9ef3ab
LW
1748 return true;
1749
f85196bd 1750 if (!acpi_match_device_ids(device, ignore_serial_bus_ids))
aba94139
HG
1751 return false;
1752
e4330d8b 1753 INIT_LIST_HEAD(&resource_list);
e361d1f8
FD
1754 acpi_dev_get_resources(device, &resource_list,
1755 acpi_check_serial_bus_slave,
1756 &is_serial_bus_slave);
e4330d8b
JN
1757 acpi_dev_free_resource_list(&resource_list);
1758
e361d1f8 1759 return is_serial_bus_slave;
e4330d8b
JN
1760}
1761
82c7d5ef 1762void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
5c5e1237 1763 int type, void (*release)(struct device *))
1da177e4 1764{
5c5e1237
RW
1765 struct acpi_device *parent = acpi_find_parent_acpi_dev(handle);
1766
d43e167d
RW
1767 INIT_LIST_HEAD(&device->pnp.ids);
1768 device->device_type = type;
1769 device->handle = handle;
5c5e1237
RW
1770 if (parent) {
1771 device->parent = parent;
1772 device->dev.parent = &parent->dev;
1773 }
1774 device->dev.release = release;
1775 device->dev.bus = &acpi_bus_type;
01bb86b3 1776 fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
f5d9ab1d 1777 acpi_set_device_status(device, ACPI_STA_DEFAULT);
d43e167d 1778 acpi_device_get_busid(device);
c830dbcf 1779 acpi_set_pnp_ids(handle, &device->pnp, type);
ffdcd955 1780 acpi_init_properties(device);
d43e167d 1781 acpi_bus_get_flags(device);
2c0d4fe0 1782 device->flags.match_driver = false;
202317a5 1783 device->flags.initialized = true;
d87fb091
JG
1784 device->flags.enumeration_by_parent =
1785 acpi_device_enumeration_by_parent(device);
10c7e20b 1786 acpi_device_clear_enumerated(device);
cf860be6
RW
1787 device_initialize(&device->dev);
1788 dev_set_uevent_suppress(&device->dev, true);
d0562674 1789 acpi_init_coherency(device);
6d279758
RW
1790}
1791
1792static void acpi_scan_dep_init(struct acpi_device *adev)
1793{
1794 struct acpi_dep_data *dep;
1795
6d279758 1796 list_for_each_entry(dep, &acpi_dep_list, node) {
9d9bcae4
HG
1797 if (dep->consumer == adev->handle) {
1798 if (dep->honor_dep)
1799 adev->flags.honor_deps = 1;
1800
6d279758 1801 adev->dep_unmet++;
9d9bcae4 1802 }
6d279758 1803 }
cf860be6 1804}
bc3b0772 1805
cf860be6
RW
1806void acpi_device_add_finalize(struct acpi_device *device)
1807{
1808 dev_set_uevent_suppress(&device->dev, false);
1809 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1da177e4
LT
1810}
1811
f5d9ab1d
RW
1812static void acpi_scan_init_status(struct acpi_device *adev)
1813{
1814 if (acpi_bus_get_status(adev))
1815 acpi_set_device_status(adev, 0);
1816}
1817
5c478f49 1818static int acpi_add_single_object(struct acpi_device **child,
6d279758 1819 acpi_handle handle, int type, bool dep_init)
1da177e4 1820{
e52d9d8c 1821 struct acpi_device *device;
5f4ce260 1822 bool release_dep_lock = false;
e52d9d8c 1823 int result;
6091b263 1824
36bcbec7 1825 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
c830dbcf 1826 if (!device)
d550d98d 1827 return -ENOMEM;
1da177e4 1828
5c5e1237 1829 acpi_init_device_object(device, handle, type, acpi_device_release);
63347db0 1830 /*
f5d9ab1d
RW
1831 * Getting the status is delayed till here so that we can call
1832 * acpi_bus_get_status() and use its quirk handling. Note that
1833 * this must be done before the get power-/wakeup_dev-flags calls.
63347db0 1834 */
6d279758 1835 if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
5f4ce260
RW
1836 if (dep_init) {
1837 mutex_lock(&acpi_dep_list_lock);
1838 /*
1839 * Hold the lock until the acpi_tie_acpi_dev() call
1840 * below to prevent concurrent acpi_scan_clear_dep()
1841 * from deleting a dependency list entry without
1842 * updating dep_unmet for the device.
1843 */
1844 release_dep_lock = true;
6d279758 1845 acpi_scan_dep_init(device);
5f4ce260 1846 }
f5d9ab1d 1847 acpi_scan_init_status(device);
6d279758 1848 }
63347db0 1849
d43e167d 1850 acpi_bus_get_power_flags(device);
d57d09a4 1851 acpi_bus_get_wakeup_device_flags(device);
1da177e4 1852
5f4ce260
RW
1853 result = acpi_tie_acpi_dev(device);
1854
1855 if (release_dep_lock)
1856 mutex_unlock(&acpi_dep_list_lock);
1857
1858 if (!result)
6e1850b2 1859 result = acpi_device_add(device);
5f4ce260 1860
d43e167d 1861 if (result) {
718fb0de 1862 acpi_device_release(&device->dev);
d43e167d
RW
1863 return result;
1864 }
1da177e4 1865
d43e167d 1866 acpi_power_add_remove_device(device, true);
cf860be6 1867 acpi_device_add_finalize(device);
e52d9d8c
RW
1868
1869 acpi_handle_debug(handle, "Added as %s, parent %s\n",
1870 dev_name(&device->dev), device->parent ?
1871 dev_name(&device->parent->dev) : "(null)");
1872
d43e167d
RW
1873 *child = device;
1874 return 0;
bf325f95
RW
1875}
1876
a4e081b0
SZ
1877static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1878 void *context)
1879{
1880 struct resource *res = context;
1881
1882 if (acpi_dev_resource_memory(ares, res))
1883 return AE_CTRL_TERMINATE;
1884
1885 return AE_OK;
1886}
1887
1888static bool acpi_device_should_be_hidden(acpi_handle handle)
1889{
1890 acpi_status status;
1891 struct resource res;
1892
1893 /* Check if it should ignore the UART device */
1894 if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1895 return false;
1896
1897 /*
1898 * The UART device described in SPCR table is assumed to have only one
1899 * memory resource present. So we only look for the first one here.
1900 */
1901 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1902 acpi_get_resource_memory, &res);
1903 if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1904 return false;
1905
1906 acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1907 &res.start);
1908
1909 return true;
1910}
1911
cde1f95f 1912bool acpi_device_is_present(const struct acpi_device *adev)
202317a5 1913{
cde1f95f 1914 return adev->status.present || adev->status.functional;
202317a5
RW
1915}
1916
4b59cc1f 1917static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
844142c3 1918 const char *idstr,
4b59cc1f
RW
1919 const struct acpi_device_id **matchid)
1920{
1921 const struct acpi_device_id *devid;
1922
aca0a4eb
RW
1923 if (handler->match)
1924 return handler->match(idstr, matchid);
1925
4b59cc1f
RW
1926 for (devid = handler->ids; devid->id[0]; devid++)
1927 if (!strcmp((char *)devid->id, idstr)) {
1928 if (matchid)
1929 *matchid = devid;
1930
1931 return true;
1932 }
1933
1934 return false;
1935}
1936
844142c3 1937static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
6b772e8f
TK
1938 const struct acpi_device_id **matchid)
1939{
1940 struct acpi_scan_handler *handler;
1941
1942 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1943 if (acpi_scan_handler_matching(handler, idstr, matchid))
1944 return handler;
1945
1946 return NULL;
1947}
1948
3f8055c3
RW
1949void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1950{
3f8055c3
RW
1951 if (!!hotplug->enabled == !!val)
1952 return;
1953
1954 mutex_lock(&acpi_scan_lock);
1955
1956 hotplug->enabled = val;
3f8055c3
RW
1957
1958 mutex_unlock(&acpi_scan_lock);
1959}
1960
3c2cc7ff 1961static void acpi_scan_init_hotplug(struct acpi_device *adev)
a33ec399 1962{
6b772e8f 1963 struct acpi_hardware_id *hwid;
a33ec399 1964
b43109fa 1965 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1e2380cd
RW
1966 acpi_dock_add(adev);
1967 return;
1968 }
3c2cc7ff
RW
1969 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1970 struct acpi_scan_handler *handler;
a33ec399 1971
6b772e8f 1972 handler = acpi_scan_match_handler(hwid->id, NULL);
1a699476
RW
1973 if (handler) {
1974 adev->flags.hotplug_notify = true;
1975 break;
1976 }
3c2cc7ff 1977 }
a33ec399
RW
1978}
1979
02056a4f 1980static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
40e7fcb1 1981{
40e7fcb1
LT
1982 struct acpi_handle_list dep_devices;
1983 acpi_status status;
6fc25088 1984 u32 count;
40e7fcb1
LT
1985 int i;
1986
71da201f
RW
1987 /*
1988 * Check for _HID here to avoid deferring the enumeration of:
1989 * 1. PCI devices.
1990 * 2. ACPI nodes describing USB ports.
1991 * Still, checking for _HID catches more then just these cases ...
1992 */
02056a4f
RW
1993 if (!check_dep || !acpi_has_method(handle, "_DEP") ||
1994 !acpi_has_method(handle, "_HID"))
6fc25088 1995 return 0;
40e7fcb1 1996
6fc25088 1997 status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
40e7fcb1 1998 if (ACPI_FAILURE(status)) {
6fc25088
RW
1999 acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
2000 return 0;
40e7fcb1
LT
2001 }
2002
6fc25088 2003 for (count = 0, i = 0; i < dep_devices.count; i++) {
40e7fcb1 2004 struct acpi_device_info *info;
6fc25088 2005 struct acpi_dep_data *dep;
9d9bcae4 2006 bool skip, honor_dep;
40e7fcb1
LT
2007
2008 status = acpi_get_object_info(dep_devices.handles[i], &info);
2009 if (ACPI_FAILURE(status)) {
6fc25088 2010 acpi_handle_debug(handle, "Error reading _DEP device info\n");
40e7fcb1
LT
2011 continue;
2012 }
2013
2ef33ee7 2014 skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
9d9bcae4 2015 honor_dep = acpi_info_matches_ids(info, acpi_honor_dep_ids);
40e7fcb1
LT
2016 kfree(info);
2017
2018 if (skip)
2019 continue;
2020
6fc25088 2021 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
40e7fcb1 2022 if (!dep)
6fc25088
RW
2023 continue;
2024
2025 count++;
40e7fcb1 2026
91438aeb 2027 dep->supplier = dep_devices.handles[i];
6fc25088 2028 dep->consumer = handle;
9d9bcae4 2029 dep->honor_dep = honor_dep;
40e7fcb1
LT
2030
2031 mutex_lock(&acpi_dep_list_lock);
2032 list_add_tail(&dep->node , &acpi_dep_list);
2033 mutex_unlock(&acpi_dep_list_lock);
2034 }
6fc25088
RW
2035
2036 return count;
40e7fcb1
LT
2037}
2038
0de7fb7c
RW
2039static bool acpi_bus_scan_second_pass;
2040
71da201f
RW
2041static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
2042 struct acpi_device **adev_p)
778cbc1d 2043{
e3c963c4 2044 struct acpi_device *device = acpi_fetch_acpi_dev(handle);
e6c1067d 2045 acpi_object_type acpi_type;
6fc25088 2046 int type;
778cbc1d 2047
4002bf38
RW
2048 if (device)
2049 goto out;
2050
e6c1067d 2051 if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
778cbc1d
BH
2052 return AE_OK;
2053
e6c1067d
RW
2054 switch (acpi_type) {
2055 case ACPI_TYPE_DEVICE:
2056 if (acpi_device_should_be_hidden(handle))
2057 return AE_OK;
82c7d5ef 2058
02056a4f
RW
2059 /* Bail out if there are dependencies. */
2060 if (acpi_scan_check_dep(handle, check_dep) > 0) {
0de7fb7c 2061 acpi_bus_scan_second_pass = true;
71da201f 2062 return AE_CTRL_DEPTH;
0de7fb7c 2063 }
02056a4f 2064
e6c1067d
RW
2065 fallthrough;
2066 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
2067 type = ACPI_BUS_TYPE_DEVICE;
2068 break;
2069
2070 case ACPI_TYPE_PROCESSOR:
e6c1067d
RW
2071 type = ACPI_BUS_TYPE_PROCESSOR;
2072 break;
2073
2074 case ACPI_TYPE_THERMAL:
2075 type = ACPI_BUS_TYPE_THERMAL;
2076 break;
2077
2078 case ACPI_TYPE_POWER:
82c7d5ef 2079 acpi_add_power_resource(handle);
02056a4f
RW
2080 fallthrough;
2081 default:
82c7d5ef 2082 return AE_OK;
71da201f 2083 }
6fc25088 2084
3e759425
HG
2085 /*
2086 * If check_dep is true at this point, the device has no dependencies,
2087 * or the creation of the device object would have been postponed above.
2088 */
6d279758
RW
2089 acpi_add_single_object(&device, handle, type, !check_dep);
2090 if (!device)
2091 return AE_CTRL_DEPTH;
2092
2093 acpi_scan_init_hotplug(device);
3c2cc7ff 2094
71da201f
RW
2095out:
2096 if (!*adev_p)
2097 *adev_p = device;
805d410f 2098
51a85faf
BH
2099 return AE_OK;
2100}
3fb02738 2101
71da201f
RW
2102static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
2103 void *not_used, void **ret_p)
2104{
2105 return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
2106}
2107
2108static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
2109 void *not_used, void **ret_p)
2110{
2111 return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
2112}
2113
48459340
ZR
2114static void acpi_default_enumeration(struct acpi_device *device)
2115{
48459340 2116 /*
d87fb091
JG
2117 * Do not enumerate devices with enumeration_by_parent flag set as
2118 * they will be enumerated by their respective parents.
48459340 2119 */
d87fb091 2120 if (!device->flags.enumeration_by_parent) {
1571875b 2121 acpi_create_platform_device(device, NULL);
10c7e20b 2122 acpi_device_set_enumerated(device);
68bdb677
OP
2123 } else {
2124 blocking_notifier_call_chain(&acpi_reconfig_chain,
2125 ACPI_RECONFIG_DEVICE_ADD, device);
10c7e20b 2126 }
48459340
ZR
2127}
2128
7d284352 2129static const struct acpi_device_id generic_device_ids[] = {
ee892094 2130 {ACPI_DT_NAMESPACE_HID, },
7d284352
RW
2131 {"", },
2132};
2133
2134static int acpi_generic_device_attach(struct acpi_device *adev,
2135 const struct acpi_device_id *not_used)
2136{
2137 /*
ee892094
RW
2138 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2139 * below can be unconditional.
7d284352
RW
2140 */
2141 if (adev->data.of_compatible)
2142 acpi_default_enumeration(adev);
2143
2144 return 1;
2145}
2146
2147static struct acpi_scan_handler generic_device_handler = {
2148 .ids = generic_device_ids,
2149 .attach = acpi_generic_device_attach,
2150};
2151
c5698074 2152static int acpi_scan_attach_handler(struct acpi_device *device)
ca589f94 2153{
c5698074
RW
2154 struct acpi_hardware_id *hwid;
2155 int ret = 0;
ca589f94 2156
c5698074 2157 list_for_each_entry(hwid, &device->pnp.ids, list) {
87b85b3c 2158 const struct acpi_device_id *devid;
c5698074 2159 struct acpi_scan_handler *handler;
ca589f94 2160
c5698074
RW
2161 handler = acpi_scan_match_handler(hwid->id, &devid);
2162 if (handler) {
d34afa9d
RW
2163 if (!handler->attach) {
2164 device->pnp.type.platform_id = 0;
2165 continue;
2166 }
9cb32acf 2167 device->handler = handler;
87b85b3c 2168 ret = handler->attach(device, devid);
9cb32acf 2169 if (ret > 0)
c5698074 2170 break;
9cb32acf
RW
2171
2172 device->handler = NULL;
2173 if (ret < 0)
c5698074 2174 break;
ca589f94
RW
2175 }
2176 }
48459340 2177
ca589f94
RW
2178 return ret;
2179}
2180
a976a2ac 2181static int acpi_bus_attach(struct acpi_device *device, void *first_pass)
51a85faf 2182{
71da201f 2183 bool skip = !first_pass && device->flags.visited;
1e2380cd 2184 acpi_handle ejd;
ca589f94 2185 int ret;
3fb02738 2186
71da201f
RW
2187 if (skip)
2188 goto ok;
2189
1e2380cd
RW
2190 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2191 register_dock_dependent_device(device, ejd);
2192
2c22e652 2193 acpi_bus_get_status(device);
9d9bcae4
HG
2194 /* Skip devices that are not ready for enumeration (e.g. not present) */
2195 if (!acpi_dev_ready_for_enumeration(device)) {
cde1f95f 2196 device->flags.initialized = false;
10c7e20b 2197 acpi_device_clear_enumerated(device);
1b1f3e16 2198 device->flags.power_manageable = 0;
a976a2ac 2199 return 0;
2c22e652 2200 }
3a391a39 2201 if (device->handler)
2c22e652 2202 goto ok;
3a391a39 2203
202317a5 2204 if (!device->flags.initialized) {
1b1f3e16
RW
2205 device->flags.power_manageable =
2206 device->power.states[ACPI_STATE_D0].flags.valid;
2207 if (acpi_bus_init_power(device))
2208 device->flags.power_manageable = 0;
2209
202317a5 2210 device->flags.initialized = true;
c381fc3a
RW
2211 } else if (device->flags.visited) {
2212 goto ok;
202317a5 2213 }
10c7e20b 2214
ca589f94 2215 ret = acpi_scan_attach_handler(device);
6931007c 2216 if (ret < 0)
a976a2ac 2217 return 0;
6931007c
RW
2218
2219 device->flags.match_driver = true;
d87fb091 2220 if (ret > 0 && !device->flags.enumeration_by_parent) {
f406270b
RW
2221 acpi_device_set_enumerated(device);
2222 goto ok;
2c22e652 2223 }
202317a5 2224
f406270b
RW
2225 ret = device_attach(&device->dev);
2226 if (ret < 0)
a976a2ac 2227 return 0;
f406270b 2228
d87fb091 2229 if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
e4330d8b 2230 acpi_default_enumeration(device);
d87fb091
JG
2231 else
2232 acpi_device_set_enumerated(device);
f406270b 2233
a976a2ac
RW
2234ok:
2235 acpi_dev_for_each_child(device, acpi_bus_attach, first_pass);
8ab17fc9 2236
71da201f 2237 if (!skip && device->handler && device->handler->hotplug.notify_online)
8ab17fc9 2238 device->handler->hotplug.notify_online(device);
a976a2ac
RW
2239
2240 return 0;
1da177e4 2241}
1da177e4 2242
b83e2b30 2243static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
40e7fcb1 2244{
40e7fcb1
LT
2245 struct acpi_device *adev;
2246
45e9aa1f 2247 adev = acpi_get_acpi_dev(dep->consumer);
ad4d451e
RW
2248 if (adev) {
2249 *(struct acpi_device **)data = adev;
2250 return 1;
2251 }
2252 /* Continue parsing if the device object is not present. */
2253 return 0;
b83e2b30
DS
2254}
2255
dc612486
RW
2256struct acpi_scan_clear_dep_work {
2257 struct work_struct work;
40e7fcb1 2258 struct acpi_device *adev;
dc612486
RW
2259};
2260
2261static void acpi_scan_clear_dep_fn(struct work_struct *work)
2262{
2263 struct acpi_scan_clear_dep_work *cdw;
2264
2265 cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
40e7fcb1 2266
dc612486 2267 acpi_scan_lock_acquire();
a976a2ac 2268 acpi_bus_attach(cdw->adev, (void *)true);
dc612486
RW
2269 acpi_scan_lock_release();
2270
2271 acpi_dev_put(cdw->adev);
2272 kfree(cdw);
2273}
2274
2275static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
2276{
2277 struct acpi_scan_clear_dep_work *cdw;
2278
2279 if (adev->dep_unmet)
2280 return false;
2281
2282 cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
2283 if (!cdw)
2284 return false;
2285
2286 cdw->adev = adev;
2287 INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
2288 /*
2289 * Since the work function may block on the lock until the entire
2290 * initial enumeration of devices is complete, put it into the unbound
2291 * workqueue.
2292 */
2293 queue_work(system_unbound_wq, &cdw->work);
2294
2295 return true;
2296}
2297
2298static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
2299{
45e9aa1f 2300 struct acpi_device *adev = acpi_get_acpi_dev(dep->consumer);
a9e10e58
DS
2301
2302 if (adev) {
2303 adev->dep_unmet--;
dc612486
RW
2304 if (!acpi_scan_clear_dep_queue(adev))
2305 acpi_dev_put(adev);
a9e10e58
DS
2306 }
2307
2308 list_del(&dep->node);
2309 kfree(dep);
2310
2311 return 0;
2312}
2313
2314/**
2315 * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2316 * @handle: The ACPI handle of the supplier device
2317 * @callback: Pointer to the callback function to apply
2318 * @data: Pointer to some data to pass to the callback
2319 *
2320 * The return value of the callback determines this function's behaviour. If 0
2321 * is returned we continue to iterate over acpi_dep_list. If a positive value
2322 * is returned then the loop is broken but this function returns 0. If a
2323 * negative value is returned by the callback then the loop is broken and that
2324 * value is returned as the final error.
2325 */
aff0dbd0
RW
2326static int acpi_walk_dep_device_list(acpi_handle handle,
2327 int (*callback)(struct acpi_dep_data *, void *),
2328 void *data)
a9e10e58
DS
2329{
2330 struct acpi_dep_data *dep, *tmp;
23db673d 2331 int ret = 0;
a9e10e58 2332
40e7fcb1
LT
2333 mutex_lock(&acpi_dep_list_lock);
2334 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
91438aeb 2335 if (dep->supplier == handle) {
a9e10e58
DS
2336 ret = callback(dep, data);
2337 if (ret)
2338 break;
40e7fcb1
LT
2339 }
2340 }
2341 mutex_unlock(&acpi_dep_list_lock);
a9e10e58
DS
2342
2343 return ret > 0 ? 0 : ret;
40e7fcb1 2344}
40e7fcb1 2345
a9e10e58
DS
2346/**
2347 * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2348 * @supplier: Pointer to the supplier &struct acpi_device
2349 *
2350 * Clear dependencies on the given device.
2351 */
2352void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2353{
2354 acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2355}
2356EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2357
9d9bcae4
HG
2358/**
2359 * acpi_dev_ready_for_enumeration - Check if the ACPI device is ready for enumeration
2360 * @device: Pointer to the &struct acpi_device to check
2361 *
2362 * Check if the device is present and has no unmet dependencies.
2363 *
2364 * Return true if the device is ready for enumeratino. Otherwise, return false.
2365 */
2366bool acpi_dev_ready_for_enumeration(const struct acpi_device *device)
2367{
2368 if (device->flags.honor_deps && device->dep_unmet)
2369 return false;
2370
2371 return acpi_device_is_present(device);
2372}
2373EXPORT_SYMBOL_GPL(acpi_dev_ready_for_enumeration);
2374
b83e2b30
DS
2375/**
2376 * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier
2377 * @supplier: Pointer to the dependee device
2378 *
2379 * Returns the first &struct acpi_device which declares itself dependent on
2380 * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2381 *
2382 * The caller is responsible for putting the reference to adev when it is no
2383 * longer needed.
2384 */
2385struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier)
2386{
2387 struct acpi_device *adev = NULL;
2388
2389 acpi_walk_dep_device_list(supplier->handle,
2390 acpi_dev_get_first_consumer_dev_cb, &adev);
2391
2392 return adev;
40e7fcb1 2393}
b83e2b30 2394EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev);
40e7fcb1 2395
636458de 2396/**
b8bd759a 2397 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
636458de 2398 * @handle: Root of the namespace scope to scan.
7779688f 2399 *
636458de
RW
2400 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2401 * found devices.
7779688f 2402 *
636458de
RW
2403 * If no devices were found, -ENODEV is returned, but it does not mean that
2404 * there has been a real error. There just have been no suitable ACPI objects
2405 * in the table trunk from which the kernel could create a device and add an
2406 * appropriate driver.
3757b948
RW
2407 *
2408 * Must be called under acpi_scan_lock.
7779688f 2409 */
b8bd759a 2410int acpi_bus_scan(acpi_handle handle)
3fb02738 2411{
71da201f
RW
2412 struct acpi_device *device = NULL;
2413
0de7fb7c 2414 acpi_bus_scan_second_pass = false;
3fb02738 2415
71da201f 2416 /* Pass 1: Avoid enumerating devices with missing dependencies. */
3fb02738 2417
71da201f 2418 if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
b8bd759a 2419 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
71da201f
RW
2420 acpi_bus_check_add_1, NULL, NULL,
2421 (void **)&device);
3fb02738 2422
71da201f
RW
2423 if (!device)
2424 return -ENODEV;
3fb02738 2425
a976a2ac 2426 acpi_bus_attach(device, (void *)true);
71da201f 2427
0de7fb7c 2428 if (!acpi_bus_scan_second_pass)
2c22e652 2429 return 0;
0de7fb7c 2430
71da201f
RW
2431 /* Pass 2: Enumerate all of the remaining devices. */
2432
2433 device = NULL;
2434
2435 if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
2436 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2437 acpi_bus_check_add_2, NULL, NULL,
2438 (void **)&device);
2439
a976a2ac 2440 acpi_bus_attach(device, NULL);
71da201f
RW
2441
2442 return 0;
3fb02738 2443}
2c22e652 2444EXPORT_SYMBOL(acpi_bus_scan);
1da177e4 2445
a976a2ac 2446static int acpi_bus_trim_one(struct acpi_device *adev, void *not_used)
1da177e4 2447{
2c22e652 2448 struct acpi_scan_handler *handler = adev->handler;
2c22e652 2449
a976a2ac 2450 acpi_dev_for_each_child_reverse(adev, acpi_bus_trim_one, NULL);
2c22e652 2451
a951c773 2452 adev->flags.match_driver = false;
2c22e652
RW
2453 if (handler) {
2454 if (handler->detach)
2455 handler->detach(adev);
2456
2457 adev->handler = NULL;
2458 } else {
2459 device_release_driver(&adev->dev);
2460 }
05404d8f 2461 /*
2c22e652
RW
2462 * Most likely, the device is going away, so put it into D3cold before
2463 * that.
05404d8f 2464 */
2c22e652
RW
2465 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2466 adev->flags.initialized = false;
10c7e20b 2467 acpi_device_clear_enumerated(adev);
a976a2ac
RW
2468
2469 return 0;
2470}
2471
2472/**
2473 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2474 * @adev: Root of the ACPI namespace scope to walk.
2475 *
2476 * Must be called under acpi_scan_lock.
2477 */
2478void acpi_bus_trim(struct acpi_device *adev)
2479{
2480 acpi_bus_trim_one(adev, NULL);
1da177e4 2481}
ceaba663
KA
2482EXPORT_SYMBOL_GPL(acpi_bus_trim);
2483
a64a62ce
LZ
2484int acpi_bus_register_early_device(int type)
2485{
2486 struct acpi_device *device = NULL;
2487 int result;
2488
6d279758 2489 result = acpi_add_single_object(&device, NULL, type, false);
a64a62ce
LZ
2490 if (result)
2491 return result;
2492
2493 device->flags.match_driver = true;
2494 return device_attach(&device->dev);
2495}
2496EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2497
681e7187 2498static void acpi_bus_scan_fixed(void)
1da177e4 2499{
2c0d4fe0 2500 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
681e7187
RW
2501 struct acpi_device *adev = NULL;
2502
2503 acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_POWER_BUTTON,
2504 false);
2505 if (adev) {
2506 adev->flags.match_driver = true;
2507 if (device_attach(&adev->dev) >= 0)
2508 device_init_wakeup(&adev->dev, true);
2509 else
2510 dev_dbg(&adev->dev, "No driver\n");
2511 }
3fb02738 2512 }
1da177e4 2513
2c0d4fe0 2514 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
681e7187
RW
2515 struct acpi_device *adev = NULL;
2516
2517 acpi_add_single_object(&adev, NULL, ACPI_BUS_TYPE_SLEEP_BUTTON,
2518 false);
2519 if (adev) {
2520 adev->flags.match_driver = true;
2521 if (device_attach(&adev->dev) < 0)
2522 dev_dbg(&adev->dev, "No driver\n");
2523 }
3fb02738 2524 }
1da177e4
LT
2525}
2526
a4e081b0
SZ
2527static void __init acpi_get_spcr_uart_addr(void)
2528{
2529 acpi_status status;
2530 struct acpi_table_spcr *spcr_ptr;
2531
2532 status = acpi_get_table(ACPI_SIG_SPCR, 0,
2533 (struct acpi_table_header **)&spcr_ptr);
ead7ba13 2534 if (ACPI_FAILURE(status)) {
8acf4108 2535 pr_warn("STAO table present, but SPCR is missing\n");
ead7ba13
HG
2536 return;
2537 }
2538
2539 spcr_uart_addr = spcr_ptr->serial_port.address;
2540 acpi_put_table((struct acpi_table_header *)spcr_ptr);
a4e081b0
SZ
2541}
2542
68bdb677
OP
2543static bool acpi_scan_initialized;
2544
b6c55b16 2545void __init acpi_scan_init(void)
1da177e4 2546{
a4e081b0
SZ
2547 acpi_status status;
2548 struct acpi_table_stao *stao_ptr;
1da177e4 2549
92ef2a25 2550 acpi_pci_root_init();
4daeaf68 2551 acpi_pci_link_init();
ac212b69 2552 acpi_processor_init();
cb0701ac 2553 acpi_platform_init();
f58b082a 2554 acpi_lpss_init();
92082a88 2555 acpi_apd_init();
2fa97feb 2556 acpi_cmos_rtc_init();
737f1a9f 2557 acpi_container_init();
0a347644 2558 acpi_memory_hotplug_init();
cc6a0e31 2559 acpi_watchdog_init();
eec15edb 2560 acpi_pnp_init();
3230bbfc 2561 acpi_int340x_thermal_init();
6ce2e188 2562 acpi_amba_init();
eeb2d80d 2563 acpi_init_lpit();
97d9a9e9 2564
7d284352
RW
2565 acpi_scan_add_handler(&generic_device_handler);
2566
a4e081b0
SZ
2567 /*
2568 * If there is STAO table, check whether it needs to ignore the UART
2569 * device in SPCR table.
2570 */
2571 status = acpi_get_table(ACPI_SIG_STAO, 0,
2572 (struct acpi_table_header **)&stao_ptr);
2573 if (ACPI_SUCCESS(status)) {
2574 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
8acf4108 2575 pr_info("STAO Name List not yet supported.\n");
a4e081b0
SZ
2576
2577 if (stao_ptr->ignore_uart)
2578 acpi_get_spcr_uart_addr();
ead7ba13
HG
2579
2580 acpi_put_table((struct acpi_table_header *)stao_ptr);
a4e081b0
SZ
2581 }
2582
eb7f43c4
RW
2583 acpi_gpe_apply_masked_gpes();
2584 acpi_update_all_gpes();
2585
7291edca
DH
2586 /*
2587 * Although we call __add_memory() that is documented to require the
2588 * device_hotplug_lock, it is not necessary here because this is an
2589 * early code when userspace or any other code path cannot trigger
2590 * hotplug/hotunplug operations.
2591 */
3757b948 2592 mutex_lock(&acpi_scan_lock);
1da177e4
LT
2593 /*
2594 * Enumerate devices in the ACPI namespace.
2595 */
b6c55b16 2596 if (acpi_bus_scan(ACPI_ROOT_OBJECT))
c96f195d 2597 goto unlock;
1da177e4 2598
e3c963c4
RW
2599 acpi_root = acpi_fetch_acpi_dev(ACPI_ROOT_OBJECT);
2600 if (!acpi_root)
c96f195d 2601 goto unlock;
1da177e4 2602
2650ef42 2603 /* Fixed feature devices do not exist on HW-reduced platform */
681e7187
RW
2604 if (!acpi_gbl_reduced_hardware)
2605 acpi_bus_scan_fixed();
1da177e4 2606
6381195a 2607 acpi_turn_off_unused_power_resources();
29038ae2 2608
68bdb677
OP
2609 acpi_scan_initialized = true;
2610
c96f195d 2611unlock:
3757b948 2612 mutex_unlock(&acpi_scan_lock);
1da177e4 2613}
e647b532
MZ
2614
2615static struct acpi_probe_entry *ape;
2616static int acpi_probe_count;
5331d9ca 2617static DEFINE_MUTEX(acpi_probe_mutex);
e647b532 2618
60574d1e 2619static int __init acpi_match_madt(union acpi_subtable_headers *header,
e647b532
MZ
2620 const unsigned long end)
2621{
60574d1e 2622 if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
e647b532
MZ
2623 if (!ape->probe_subtbl(header, end))
2624 acpi_probe_count++;
2625
2626 return 0;
2627}
2628
2629int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2630{
2631 int count = 0;
2632
2633 if (acpi_disabled)
2634 return 0;
2635
5331d9ca 2636 mutex_lock(&acpi_probe_mutex);
e647b532 2637 for (ape = ap_head; nr; ape++, nr--) {
5599fb69 2638 if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
e647b532
MZ
2639 acpi_probe_count = 0;
2640 acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2641 count += acpi_probe_count;
2642 } else {
2643 int res;
2644 res = acpi_table_parse(ape->id, ape->probe_table);
2645 if (!res)
2646 count++;
2647 }
2648 }
5331d9ca 2649 mutex_unlock(&acpi_probe_mutex);
e647b532
MZ
2650
2651 return count;
2652}
68bdb677 2653
68bdb677
OP
2654static void acpi_table_events_fn(struct work_struct *work)
2655{
8d287e82
RW
2656 acpi_scan_lock_acquire();
2657 acpi_bus_scan(ACPI_ROOT_OBJECT);
2658 acpi_scan_lock_release();
68bdb677 2659
8d287e82 2660 kfree(work);
68bdb677
OP
2661}
2662
8d287e82 2663void acpi_scan_table_notify(void)
68bdb677 2664{
8d287e82 2665 struct work_struct *work;
68bdb677
OP
2666
2667 if (!acpi_scan_initialized)
2668 return;
2669
8d287e82
RW
2670 work = kmalloc(sizeof(*work), GFP_KERNEL);
2671 if (!work)
68bdb677
OP
2672 return;
2673
8d287e82
RW
2674 INIT_WORK(work, acpi_table_events_fn);
2675 schedule_work(work);
68bdb677
OP
2676}
2677
2678int acpi_reconfig_notifier_register(struct notifier_block *nb)
2679{
2680 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2681}
2682EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2683
2684int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2685{
2686 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2687}
2688EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);