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