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