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