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