Merge tag 'for-linus-2021-04-08' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.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/signal.h>
13 #include <linux/kthread.h>
14 #include <linux/dmi.h>
15 #include <linux/nls.h>
16 #include <linux/dma-map-ops.h>
17 #include <linux/platform_data/x86/apple.h>
18 #include <linux/pgtable.h>
19
20 #include "internal.h"
21
22 extern struct acpi_device *acpi_root;
23
24 #define ACPI_BUS_CLASS                  "system_bus"
25 #define ACPI_BUS_HID                    "LNXSYBUS"
26 #define ACPI_BUS_DEVICE_NAME            "System Bus"
27
28 #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
29
30 #define INVALID_ACPI_HANDLE     ((acpi_handle)empty_zero_page)
31
32 static const char *dummy_hid = "device";
33
34 static LIST_HEAD(acpi_dep_list);
35 static DEFINE_MUTEX(acpi_dep_list_lock);
36 LIST_HEAD(acpi_bus_id_list);
37 static DEFINE_MUTEX(acpi_scan_lock);
38 static LIST_HEAD(acpi_scan_handlers_list);
39 DEFINE_MUTEX(acpi_device_lock);
40 LIST_HEAD(acpi_wakeup_device_list);
41 static DEFINE_MUTEX(acpi_hp_context_lock);
42
43 /*
44  * The UART device described by the SPCR table is the only object which needs
45  * special-casing. Everything else is covered by ACPI namespace paths in STAO
46  * table.
47  */
48 static u64 spcr_uart_addr;
49
50 struct acpi_dep_data {
51         struct list_head node;
52         acpi_handle supplier;
53         acpi_handle consumer;
54 };
55
56 void acpi_scan_lock_acquire(void)
57 {
58         mutex_lock(&acpi_scan_lock);
59 }
60 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
61
62 void acpi_scan_lock_release(void)
63 {
64         mutex_unlock(&acpi_scan_lock);
65 }
66 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
67
68 void acpi_lock_hp_context(void)
69 {
70         mutex_lock(&acpi_hp_context_lock);
71 }
72
73 void acpi_unlock_hp_context(void)
74 {
75         mutex_unlock(&acpi_hp_context_lock);
76 }
77
78 void acpi_initialize_hp_context(struct acpi_device *adev,
79                                 struct acpi_hotplug_context *hp,
80                                 int (*notify)(struct acpi_device *, u32),
81                                 void (*uevent)(struct acpi_device *, u32))
82 {
83         acpi_lock_hp_context();
84         hp->notify = notify;
85         hp->uevent = uevent;
86         acpi_set_hp_context(adev, hp);
87         acpi_unlock_hp_context();
88 }
89 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
90
91 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
92 {
93         if (!handler)
94                 return -EINVAL;
95
96         list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
97         return 0;
98 }
99
100 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
101                                        const char *hotplug_profile_name)
102 {
103         int error;
104
105         error = acpi_scan_add_handler(handler);
106         if (error)
107                 return error;
108
109         acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
110         return 0;
111 }
112
113 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
114 {
115         struct acpi_device_physical_node *pn;
116         bool offline = true;
117         char *envp[] = { "EVENT=offline", NULL };
118
119         /*
120          * acpi_container_offline() calls this for all of the container's
121          * children under the container's physical_node_lock lock.
122          */
123         mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
124
125         list_for_each_entry(pn, &adev->physical_node_list, node)
126                 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
127                         if (uevent)
128                                 kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
129
130                         offline = false;
131                         break;
132                 }
133
134         mutex_unlock(&adev->physical_node_lock);
135         return offline;
136 }
137
138 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
139                                     void **ret_p)
140 {
141         struct acpi_device *device = NULL;
142         struct acpi_device_physical_node *pn;
143         bool second_pass = (bool)data;
144         acpi_status status = AE_OK;
145
146         if (acpi_bus_get_device(handle, &device))
147                 return AE_OK;
148
149         if (device->handler && !device->handler->hotplug.enabled) {
150                 *ret_p = &device->dev;
151                 return AE_SUPPORT;
152         }
153
154         mutex_lock(&device->physical_node_lock);
155
156         list_for_each_entry(pn, &device->physical_node_list, node) {
157                 int ret;
158
159                 if (second_pass) {
160                         /* Skip devices offlined by the first pass. */
161                         if (pn->put_online)
162                                 continue;
163                 } else {
164                         pn->put_online = false;
165                 }
166                 ret = device_offline(pn->dev);
167                 if (ret >= 0) {
168                         pn->put_online = !ret;
169                 } else {
170                         *ret_p = pn->dev;
171                         if (second_pass) {
172                                 status = AE_ERROR;
173                                 break;
174                         }
175                 }
176         }
177
178         mutex_unlock(&device->physical_node_lock);
179
180         return status;
181 }
182
183 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
184                                    void **ret_p)
185 {
186         struct acpi_device *device = NULL;
187         struct acpi_device_physical_node *pn;
188
189         if (acpi_bus_get_device(handle, &device))
190                 return AE_OK;
191
192         mutex_lock(&device->physical_node_lock);
193
194         list_for_each_entry(pn, &device->physical_node_list, node)
195                 if (pn->put_online) {
196                         device_online(pn->dev);
197                         pn->put_online = false;
198                 }
199
200         mutex_unlock(&device->physical_node_lock);
201
202         return AE_OK;
203 }
204
205 static int acpi_scan_try_to_offline(struct acpi_device *device)
206 {
207         acpi_handle handle = device->handle;
208         struct device *errdev = NULL;
209         acpi_status status;
210
211         /*
212          * Carry out two passes here and ignore errors in the first pass,
213          * because if the devices in question are memory blocks and
214          * CONFIG_MEMCG is set, one of the blocks may hold data structures
215          * that the other blocks depend on, but it is not known in advance which
216          * block holds them.
217          *
218          * If the first pass is successful, the second one isn't needed, though.
219          */
220         status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
221                                      NULL, acpi_bus_offline, (void *)false,
222                                      (void **)&errdev);
223         if (status == AE_SUPPORT) {
224                 dev_warn(errdev, "Offline disabled.\n");
225                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
226                                     acpi_bus_online, NULL, NULL, NULL);
227                 return -EPERM;
228         }
229         acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
230         if (errdev) {
231                 errdev = NULL;
232                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
233                                     NULL, acpi_bus_offline, (void *)true,
234                                     (void **)&errdev);
235                 if (!errdev)
236                         acpi_bus_offline(handle, 0, (void *)true,
237                                          (void **)&errdev);
238
239                 if (errdev) {
240                         dev_warn(errdev, "Offline failed.\n");
241                         acpi_bus_online(handle, 0, NULL, NULL);
242                         acpi_walk_namespace(ACPI_TYPE_ANY, handle,
243                                             ACPI_UINT32_MAX, acpi_bus_online,
244                                             NULL, NULL, NULL);
245                         return -EBUSY;
246                 }
247         }
248         return 0;
249 }
250
251 static int acpi_scan_hot_remove(struct acpi_device *device)
252 {
253         acpi_handle handle = device->handle;
254         unsigned long long sta;
255         acpi_status status;
256
257         if (device->handler && device->handler->hotplug.demand_offline) {
258                 if (!acpi_scan_is_offline(device, true))
259                         return -EBUSY;
260         } else {
261                 int error = acpi_scan_try_to_offline(device);
262                 if (error)
263                         return error;
264         }
265
266         acpi_handle_debug(handle, "Ejecting\n");
267
268         acpi_bus_trim(device);
269
270         acpi_evaluate_lck(handle, 0);
271         /*
272          * TBD: _EJD support.
273          */
274         status = acpi_evaluate_ej0(handle);
275         if (status == AE_NOT_FOUND)
276                 return -ENODEV;
277         else if (ACPI_FAILURE(status))
278                 return -EIO;
279
280         /*
281          * Verify if eject was indeed successful.  If not, log an error
282          * message.  No need to call _OST since _EJ0 call was made OK.
283          */
284         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
285         if (ACPI_FAILURE(status)) {
286                 acpi_handle_warn(handle,
287                         "Status check after eject failed (0x%x)\n", status);
288         } else if (sta & ACPI_STA_DEVICE_ENABLED) {
289                 acpi_handle_warn(handle,
290                         "Eject incomplete - status 0x%llx\n", sta);
291         }
292
293         return 0;
294 }
295
296 static int acpi_scan_device_not_present(struct acpi_device *adev)
297 {
298         if (!acpi_device_enumerated(adev)) {
299                 dev_warn(&adev->dev, "Still not present\n");
300                 return -EALREADY;
301         }
302         acpi_bus_trim(adev);
303         return 0;
304 }
305
306 static int acpi_scan_device_check(struct acpi_device *adev)
307 {
308         int error;
309
310         acpi_bus_get_status(adev);
311         if (adev->status.present || adev->status.functional) {
312                 /*
313                  * This function is only called for device objects for which
314                  * matching scan handlers exist.  The only situation in which
315                  * the scan handler is not attached to this device object yet
316                  * is when the device has just appeared (either it wasn't
317                  * present at all before or it was removed and then added
318                  * again).
319                  */
320                 if (adev->handler) {
321                         dev_warn(&adev->dev, "Already enumerated\n");
322                         return -EALREADY;
323                 }
324                 error = acpi_bus_scan(adev->handle);
325                 if (error) {
326                         dev_warn(&adev->dev, "Namespace scan failure\n");
327                         return error;
328                 }
329                 if (!adev->handler) {
330                         dev_warn(&adev->dev, "Enumeration failure\n");
331                         error = -ENODEV;
332                 }
333         } else {
334                 error = acpi_scan_device_not_present(adev);
335         }
336         return error;
337 }
338
339 static int acpi_scan_bus_check(struct acpi_device *adev)
340 {
341         struct acpi_scan_handler *handler = adev->handler;
342         struct acpi_device *child;
343         int error;
344
345         acpi_bus_get_status(adev);
346         if (!(adev->status.present || adev->status.functional)) {
347                 acpi_scan_device_not_present(adev);
348                 return 0;
349         }
350         if (handler && handler->hotplug.scan_dependent)
351                 return handler->hotplug.scan_dependent(adev);
352
353         error = acpi_bus_scan(adev->handle);
354         if (error) {
355                 dev_warn(&adev->dev, "Namespace scan failure\n");
356                 return error;
357         }
358         list_for_each_entry(child, &adev->children, node) {
359                 error = acpi_scan_bus_check(child);
360                 if (error)
361                         return error;
362         }
363         return 0;
364 }
365
366 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
367 {
368         switch (type) {
369         case ACPI_NOTIFY_BUS_CHECK:
370                 return acpi_scan_bus_check(adev);
371         case ACPI_NOTIFY_DEVICE_CHECK:
372                 return acpi_scan_device_check(adev);
373         case ACPI_NOTIFY_EJECT_REQUEST:
374         case ACPI_OST_EC_OSPM_EJECT:
375                 if (adev->handler && !adev->handler->hotplug.enabled) {
376                         dev_info(&adev->dev, "Eject disabled\n");
377                         return -EPERM;
378                 }
379                 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
380                                   ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
381                 return acpi_scan_hot_remove(adev);
382         }
383         return -EINVAL;
384 }
385
386 void acpi_device_hotplug(struct acpi_device *adev, u32 src)
387 {
388         u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
389         int error = -ENODEV;
390
391         lock_device_hotplug();
392         mutex_lock(&acpi_scan_lock);
393
394         /*
395          * The device object's ACPI handle cannot become invalid as long as we
396          * are holding acpi_scan_lock, but it might have become invalid before
397          * that lock was acquired.
398          */
399         if (adev->handle == INVALID_ACPI_HANDLE)
400                 goto err_out;
401
402         if (adev->flags.is_dock_station) {
403                 error = dock_notify(adev, src);
404         } else if (adev->flags.hotplug_notify) {
405                 error = acpi_generic_hotplug_event(adev, src);
406         } else {
407                 int (*notify)(struct acpi_device *, u32);
408
409                 acpi_lock_hp_context();
410                 notify = adev->hp ? adev->hp->notify : NULL;
411                 acpi_unlock_hp_context();
412                 /*
413                  * There may be additional notify handlers for device objects
414                  * without the .event() callback, so ignore them here.
415                  */
416                 if (notify)
417                         error = notify(adev, src);
418                 else
419                         goto out;
420         }
421         switch (error) {
422         case 0:
423                 ost_code = ACPI_OST_SC_SUCCESS;
424                 break;
425         case -EPERM:
426                 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
427                 break;
428         case -EBUSY:
429                 ost_code = ACPI_OST_SC_DEVICE_BUSY;
430                 break;
431         default:
432                 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
433                 break;
434         }
435
436  err_out:
437         acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
438
439  out:
440         acpi_bus_put_acpi_device(adev);
441         mutex_unlock(&acpi_scan_lock);
442         unlock_device_hotplug();
443 }
444
445 static void acpi_free_power_resources_lists(struct acpi_device *device)
446 {
447         int i;
448
449         if (device->wakeup.flags.valid)
450                 acpi_power_resources_list_free(&device->wakeup.resources);
451
452         if (!device->power.flags.power_resources)
453                 return;
454
455         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
456                 struct acpi_device_power_state *ps = &device->power.states[i];
457                 acpi_power_resources_list_free(&ps->resources);
458         }
459 }
460
461 static void acpi_device_release(struct device *dev)
462 {
463         struct acpi_device *acpi_dev = to_acpi_device(dev);
464
465         acpi_free_properties(acpi_dev);
466         acpi_free_pnp_ids(&acpi_dev->pnp);
467         acpi_free_power_resources_lists(acpi_dev);
468         kfree(acpi_dev);
469 }
470
471 static void acpi_device_del(struct acpi_device *device)
472 {
473         struct acpi_device_bus_id *acpi_device_bus_id;
474
475         mutex_lock(&acpi_device_lock);
476         if (device->parent)
477                 list_del(&device->node);
478
479         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
480                 if (!strcmp(acpi_device_bus_id->bus_id,
481                             acpi_device_hid(device))) {
482                         ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
483                         if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
484                                 list_del(&acpi_device_bus_id->node);
485                                 kfree_const(acpi_device_bus_id->bus_id);
486                                 kfree(acpi_device_bus_id);
487                         }
488                         break;
489                 }
490
491         list_del(&device->wakeup_list);
492         mutex_unlock(&acpi_device_lock);
493
494         acpi_power_add_remove_device(device, false);
495         acpi_device_remove_files(device);
496         if (device->remove)
497                 device->remove(device);
498
499         device_del(&device->dev);
500 }
501
502 static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
503
504 static LIST_HEAD(acpi_device_del_list);
505 static DEFINE_MUTEX(acpi_device_del_lock);
506
507 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
508 {
509         for (;;) {
510                 struct acpi_device *adev;
511
512                 mutex_lock(&acpi_device_del_lock);
513
514                 if (list_empty(&acpi_device_del_list)) {
515                         mutex_unlock(&acpi_device_del_lock);
516                         break;
517                 }
518                 adev = list_first_entry(&acpi_device_del_list,
519                                         struct acpi_device, del_list);
520                 list_del(&adev->del_list);
521
522                 mutex_unlock(&acpi_device_del_lock);
523
524                 blocking_notifier_call_chain(&acpi_reconfig_chain,
525                                              ACPI_RECONFIG_DEVICE_REMOVE, adev);
526
527                 acpi_device_del(adev);
528                 /*
529                  * Drop references to all power resources that might have been
530                  * used by the device.
531                  */
532                 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
533                 put_device(&adev->dev);
534         }
535 }
536
537 /**
538  * acpi_scan_drop_device - Drop an ACPI device object.
539  * @handle: Handle of an ACPI namespace node, not used.
540  * @context: Address of the ACPI device object to drop.
541  *
542  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
543  * namespace node the device object pointed to by @context is attached to.
544  *
545  * The unregistration is carried out asynchronously to avoid running
546  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
547  * ensure the correct ordering (the device objects must be unregistered in the
548  * same order in which the corresponding namespace nodes are deleted).
549  */
550 static void acpi_scan_drop_device(acpi_handle handle, void *context)
551 {
552         static DECLARE_WORK(work, acpi_device_del_work_fn);
553         struct acpi_device *adev = context;
554
555         mutex_lock(&acpi_device_del_lock);
556
557         /*
558          * Use the ACPI hotplug workqueue which is ordered, so this work item
559          * won't run after any hotplug work items submitted subsequently.  That
560          * prevents attempts to register device objects identical to those being
561          * deleted from happening concurrently (such attempts result from
562          * hotplug events handled via the ACPI hotplug workqueue).  It also will
563          * run after all of the work items submitted previosuly, which helps
564          * those work items to ensure that they are not accessing stale device
565          * objects.
566          */
567         if (list_empty(&acpi_device_del_list))
568                 acpi_queue_hotplug_work(&work);
569
570         list_add_tail(&adev->del_list, &acpi_device_del_list);
571         /* Make acpi_ns_validate_handle() return NULL for this handle. */
572         adev->handle = INVALID_ACPI_HANDLE;
573
574         mutex_unlock(&acpi_device_del_lock);
575 }
576
577 static struct acpi_device *handle_to_device(acpi_handle handle,
578                                             void (*callback)(void *))
579 {
580         struct acpi_device *adev = NULL;
581         acpi_status status;
582
583         status = acpi_get_data_full(handle, acpi_scan_drop_device,
584                                     (void **)&adev, callback);
585         if (ACPI_FAILURE(status) || !adev) {
586                 acpi_handle_debug(handle, "No context!\n");
587                 return NULL;
588         }
589         return adev;
590 }
591
592 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
593 {
594         if (!device)
595                 return -EINVAL;
596
597         *device = handle_to_device(handle, NULL);
598         if (!*device)
599                 return -ENODEV;
600
601         return 0;
602 }
603 EXPORT_SYMBOL(acpi_bus_get_device);
604
605 static void get_acpi_device(void *dev)
606 {
607         if (dev)
608                 get_device(&((struct acpi_device *)dev)->dev);
609 }
610
611 struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
612 {
613         return handle_to_device(handle, get_acpi_device);
614 }
615
616 void acpi_bus_put_acpi_device(struct acpi_device *adev)
617 {
618         put_device(&adev->dev);
619 }
620
621 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
622 {
623         struct acpi_device_bus_id *acpi_device_bus_id;
624
625         /* Find suitable bus_id and instance number in acpi_bus_id_list. */
626         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
627                 if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
628                         return acpi_device_bus_id;
629         }
630         return NULL;
631 }
632
633 static int acpi_device_set_name(struct acpi_device *device,
634                                 struct acpi_device_bus_id *acpi_device_bus_id)
635 {
636         struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
637         int result;
638
639         result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
640         if (result < 0)
641                 return result;
642
643         device->pnp.instance_no = result;
644         dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
645         return 0;
646 }
647
648 int acpi_device_add(struct acpi_device *device,
649                     void (*release)(struct device *))
650 {
651         struct acpi_device_bus_id *acpi_device_bus_id;
652         int result;
653
654         if (device->handle) {
655                 acpi_status status;
656
657                 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
658                                           device);
659                 if (ACPI_FAILURE(status)) {
660                         acpi_handle_err(device->handle,
661                                         "Unable to attach device data\n");
662                         return -ENODEV;
663                 }
664         }
665
666         /*
667          * Linkage
668          * -------
669          * Link this device to its parent and siblings.
670          */
671         INIT_LIST_HEAD(&device->children);
672         INIT_LIST_HEAD(&device->node);
673         INIT_LIST_HEAD(&device->wakeup_list);
674         INIT_LIST_HEAD(&device->physical_node_list);
675         INIT_LIST_HEAD(&device->del_list);
676         mutex_init(&device->physical_node_lock);
677
678         mutex_lock(&acpi_device_lock);
679
680         acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
681         if (acpi_device_bus_id) {
682                 result = acpi_device_set_name(device, acpi_device_bus_id);
683                 if (result)
684                         goto err_unlock;
685         } else {
686                 acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
687                                              GFP_KERNEL);
688                 if (!acpi_device_bus_id) {
689                         result = -ENOMEM;
690                         goto err_unlock;
691                 }
692                 acpi_device_bus_id->bus_id =
693                         kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
694                 if (!acpi_device_bus_id->bus_id) {
695                         kfree(acpi_device_bus_id);
696                         result = -ENOMEM;
697                         goto err_unlock;
698                 }
699
700                 ida_init(&acpi_device_bus_id->instance_ida);
701
702                 result = acpi_device_set_name(device, acpi_device_bus_id);
703                 if (result) {
704                         kfree(acpi_device_bus_id);
705                         goto err_unlock;
706                 }
707
708                 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
709         }
710
711         if (device->parent)
712                 list_add_tail(&device->node, &device->parent->children);
713
714         if (device->wakeup.flags.valid)
715                 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
716
717         mutex_unlock(&acpi_device_lock);
718
719         if (device->parent)
720                 device->dev.parent = &device->parent->dev;
721
722         device->dev.bus = &acpi_bus_type;
723         device->dev.release = release;
724         result = device_add(&device->dev);
725         if (result) {
726                 dev_err(&device->dev, "Error registering device\n");
727                 goto err;
728         }
729
730         result = acpi_device_setup_files(device);
731         if (result)
732                 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
733                        dev_name(&device->dev));
734
735         return 0;
736
737 err:
738         mutex_lock(&acpi_device_lock);
739
740         if (device->parent)
741                 list_del(&device->node);
742
743         list_del(&device->wakeup_list);
744
745 err_unlock:
746         mutex_unlock(&acpi_device_lock);
747
748         acpi_detach_data(device->handle, acpi_scan_drop_device);
749
750         return result;
751 }
752
753 /* --------------------------------------------------------------------------
754                                  Device Enumeration
755    -------------------------------------------------------------------------- */
756 static bool acpi_info_matches_ids(struct acpi_device_info *info,
757                                   const char * const ids[])
758 {
759         struct acpi_pnp_device_id_list *cid_list = NULL;
760         int i;
761
762         if (!(info->valid & ACPI_VALID_HID))
763                 return false;
764
765         if (info->valid & ACPI_VALID_CID)
766                 cid_list = &info->compatible_id_list;
767
768         for (i = 0; ids[i]; i++) {
769                 int j;
770
771                 if (!strcmp(info->hardware_id.string, ids[i]))
772                         return true;
773
774                 if (!cid_list)
775                         continue;
776
777                 for (j = 0; j < cid_list->count; j++) {
778                         if (!strcmp(cid_list->ids[j].string, ids[i]))
779                                 return true;
780                 }
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, struct acpi_device_info *info)
1311 {
1312         struct acpi_pnp_device_id_list *cid_list;
1313         int i;
1314
1315         switch (device_type) {
1316         case ACPI_BUS_TYPE_DEVICE:
1317                 if (handle == ACPI_ROOT_OBJECT) {
1318                         acpi_add_id(pnp, ACPI_SYSTEM_HID);
1319                         break;
1320                 }
1321
1322                 if (!info) {
1323                         pr_err(PREFIX "%s: Error reading device info\n",
1324                                         __func__);
1325                         return;
1326                 }
1327
1328                 if (info->valid & ACPI_VALID_HID) {
1329                         acpi_add_id(pnp, info->hardware_id.string);
1330                         pnp->type.platform_id = 1;
1331                 }
1332                 if (info->valid & ACPI_VALID_CID) {
1333                         cid_list = &info->compatible_id_list;
1334                         for (i = 0; i < cid_list->count; i++)
1335                                 acpi_add_id(pnp, cid_list->ids[i].string);
1336                 }
1337                 if (info->valid & ACPI_VALID_ADR) {
1338                         pnp->bus_address = info->address;
1339                         pnp->type.bus_address = 1;
1340                 }
1341                 if (info->valid & ACPI_VALID_UID)
1342                         pnp->unique_id = kstrdup(info->unique_id.string,
1343                                                         GFP_KERNEL);
1344                 if (info->valid & ACPI_VALID_CLS)
1345                         acpi_add_id(pnp, info->class_code.string);
1346
1347                 /*
1348                  * Some devices don't reliably have _HIDs & _CIDs, so add
1349                  * synthetic HIDs to make sure drivers can find them.
1350                  */
1351                 if (acpi_is_video_device(handle))
1352                         acpi_add_id(pnp, ACPI_VIDEO_HID);
1353                 else if (acpi_bay_match(handle))
1354                         acpi_add_id(pnp, ACPI_BAY_HID);
1355                 else if (acpi_dock_match(handle))
1356                         acpi_add_id(pnp, ACPI_DOCK_HID);
1357                 else if (acpi_ibm_smbus_match(handle))
1358                         acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1359                 else if (list_empty(&pnp->ids) &&
1360                          acpi_object_is_system_bus(handle)) {
1361                         /* \_SB, \_TZ, LNXSYBUS */
1362                         acpi_add_id(pnp, ACPI_BUS_HID);
1363                         strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1364                         strcpy(pnp->device_class, ACPI_BUS_CLASS);
1365                 }
1366
1367                 break;
1368         case ACPI_BUS_TYPE_POWER:
1369                 acpi_add_id(pnp, ACPI_POWER_HID);
1370                 break;
1371         case ACPI_BUS_TYPE_PROCESSOR:
1372                 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1373                 break;
1374         case ACPI_BUS_TYPE_THERMAL:
1375                 acpi_add_id(pnp, ACPI_THERMAL_HID);
1376                 break;
1377         case ACPI_BUS_TYPE_POWER_BUTTON:
1378                 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1379                 break;
1380         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1381                 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1382                 break;
1383         case ACPI_BUS_TYPE_ECDT_EC:
1384                 acpi_add_id(pnp, ACPI_ECDT_HID);
1385                 break;
1386         }
1387 }
1388
1389 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1390 {
1391         struct acpi_hardware_id *id, *tmp;
1392
1393         list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1394                 kfree_const(id->id);
1395                 kfree(id);
1396         }
1397         kfree(pnp->unique_id);
1398 }
1399
1400 /**
1401  * acpi_dma_supported - Check DMA support for the specified device.
1402  * @adev: The pointer to acpi device
1403  *
1404  * Return false if DMA is not supported. Otherwise, return true
1405  */
1406 bool acpi_dma_supported(struct acpi_device *adev)
1407 {
1408         if (!adev)
1409                 return false;
1410
1411         if (adev->flags.cca_seen)
1412                 return true;
1413
1414         /*
1415         * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1416         * DMA on "Intel platforms".  Presumably that includes all x86 and
1417         * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1418         */
1419         if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1420                 return true;
1421
1422         return false;
1423 }
1424
1425 /**
1426  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1427  * @adev: The pointer to acpi device
1428  *
1429  * Return enum dev_dma_attr.
1430  */
1431 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1432 {
1433         if (!acpi_dma_supported(adev))
1434                 return DEV_DMA_NOT_SUPPORTED;
1435
1436         if (adev->flags.coherent_dma)
1437                 return DEV_DMA_COHERENT;
1438         else
1439                 return DEV_DMA_NON_COHERENT;
1440 }
1441
1442 /**
1443  * acpi_dma_get_range() - Get device DMA parameters.
1444  *
1445  * @dev: device to configure
1446  * @dma_addr: pointer device DMA address result
1447  * @offset: pointer to the DMA offset result
1448  * @size: pointer to DMA range size result
1449  *
1450  * Evaluate DMA regions and return respectively DMA region start, offset
1451  * and size in dma_addr, offset and size on parsing success; it does not
1452  * update the passed in values on failure.
1453  *
1454  * Return 0 on success, < 0 on failure.
1455  */
1456 int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1457                        u64 *size)
1458 {
1459         struct acpi_device *adev;
1460         LIST_HEAD(list);
1461         struct resource_entry *rentry;
1462         int ret;
1463         struct device *dma_dev = dev;
1464         u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1465
1466         /*
1467          * Walk the device tree chasing an ACPI companion with a _DMA
1468          * object while we go. Stop if we find a device with an ACPI
1469          * companion containing a _DMA method.
1470          */
1471         do {
1472                 adev = ACPI_COMPANION(dma_dev);
1473                 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1474                         break;
1475
1476                 dma_dev = dma_dev->parent;
1477         } while (dma_dev);
1478
1479         if (!dma_dev)
1480                 return -ENODEV;
1481
1482         if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1483                 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1484                 return -EINVAL;
1485         }
1486
1487         ret = acpi_dev_get_dma_resources(adev, &list);
1488         if (ret > 0) {
1489                 list_for_each_entry(rentry, &list, node) {
1490                         if (dma_offset && rentry->offset != dma_offset) {
1491                                 ret = -EINVAL;
1492                                 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1493                                 goto out;
1494                         }
1495                         dma_offset = rentry->offset;
1496
1497                         /* Take lower and upper limits */
1498                         if (rentry->res->start < dma_start)
1499                                 dma_start = rentry->res->start;
1500                         if (rentry->res->end > dma_end)
1501                                 dma_end = rentry->res->end;
1502                 }
1503
1504                 if (dma_start >= dma_end) {
1505                         ret = -EINVAL;
1506                         dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1507                         goto out;
1508                 }
1509
1510                 *dma_addr = dma_start - dma_offset;
1511                 len = dma_end - dma_start;
1512                 *size = max(len, len + 1);
1513                 *offset = dma_offset;
1514         }
1515  out:
1516         acpi_dev_free_resource_list(&list);
1517
1518         return ret >= 0 ? 0 : ret;
1519 }
1520
1521 /**
1522  * acpi_dma_configure_id - Set-up DMA configuration for the device.
1523  * @dev: The pointer to the device
1524  * @attr: device dma attributes
1525  * @input_id: input device id const value pointer
1526  */
1527 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1528                           const u32 *input_id)
1529 {
1530         const struct iommu_ops *iommu;
1531         u64 dma_addr = 0, size = 0;
1532
1533         if (attr == DEV_DMA_NOT_SUPPORTED) {
1534                 set_dma_ops(dev, &dma_dummy_ops);
1535                 return 0;
1536         }
1537
1538         iort_dma_setup(dev, &dma_addr, &size);
1539
1540         iommu = iort_iommu_configure_id(dev, input_id);
1541         if (PTR_ERR(iommu) == -EPROBE_DEFER)
1542                 return -EPROBE_DEFER;
1543
1544         arch_setup_dma_ops(dev, dma_addr, size,
1545                                 iommu, attr == DEV_DMA_COHERENT);
1546
1547         return 0;
1548 }
1549 EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1550
1551 static void acpi_init_coherency(struct acpi_device *adev)
1552 {
1553         unsigned long long cca = 0;
1554         acpi_status status;
1555         struct acpi_device *parent = adev->parent;
1556
1557         if (parent && parent->flags.cca_seen) {
1558                 /*
1559                  * From ACPI spec, OSPM will ignore _CCA if an ancestor
1560                  * already saw one.
1561                  */
1562                 adev->flags.cca_seen = 1;
1563                 cca = parent->flags.coherent_dma;
1564         } else {
1565                 status = acpi_evaluate_integer(adev->handle, "_CCA",
1566                                                NULL, &cca);
1567                 if (ACPI_SUCCESS(status))
1568                         adev->flags.cca_seen = 1;
1569                 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1570                         /*
1571                          * If architecture does not specify that _CCA is
1572                          * required for DMA-able devices (e.g. x86),
1573                          * we default to _CCA=1.
1574                          */
1575                         cca = 1;
1576                 else
1577                         acpi_handle_debug(adev->handle,
1578                                           "ACPI device is missing _CCA.\n");
1579         }
1580
1581         adev->flags.coherent_dma = cca;
1582 }
1583
1584 static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1585 {
1586         bool *is_serial_bus_slave_p = data;
1587
1588         if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1589                 return 1;
1590
1591         *is_serial_bus_slave_p = true;
1592
1593          /* no need to do more checking */
1594         return -1;
1595 }
1596
1597 static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1598 {
1599         struct acpi_device *parent = device->parent;
1600         static const struct acpi_device_id indirect_io_hosts[] = {
1601                 {"HISI0191", 0},
1602                 {}
1603         };
1604
1605         return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1606 }
1607
1608 static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1609 {
1610         struct list_head resource_list;
1611         bool is_serial_bus_slave = false;
1612         /*
1613          * These devices have multiple I2cSerialBus resources and an i2c-client
1614          * must be instantiated for each, each with its own i2c_device_id.
1615          * Normally we only instantiate an i2c-client for the first resource,
1616          * using the ACPI HID as id. These special cases are handled by the
1617          * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1618          * which i2c_device_id to use for each resource.
1619          */
1620         static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1621                 {"BSG1160", },
1622                 {"BSG2150", },
1623                 {"INT33FE", },
1624                 {"INT3515", },
1625                 {}
1626         };
1627
1628         if (acpi_is_indirect_io_slave(device))
1629                 return true;
1630
1631         /* Macs use device properties in lieu of _CRS resources */
1632         if (x86_apple_machine &&
1633             (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1634              fwnode_property_present(&device->fwnode, "i2cAddress") ||
1635              fwnode_property_present(&device->fwnode, "baud")))
1636                 return true;
1637
1638         /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1639         if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1640                 return false;
1641
1642         INIT_LIST_HEAD(&resource_list);
1643         acpi_dev_get_resources(device, &resource_list,
1644                                acpi_check_serial_bus_slave,
1645                                &is_serial_bus_slave);
1646         acpi_dev_free_resource_list(&resource_list);
1647
1648         return is_serial_bus_slave;
1649 }
1650
1651 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1652                              int type, unsigned long long sta,
1653                              struct acpi_device_info *info)
1654 {
1655         INIT_LIST_HEAD(&device->pnp.ids);
1656         device->device_type = type;
1657         device->handle = handle;
1658         device->parent = acpi_bus_get_parent(handle);
1659         fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1660         acpi_set_device_status(device, sta);
1661         acpi_device_get_busid(device);
1662         acpi_set_pnp_ids(handle, &device->pnp, type, info);
1663         acpi_init_properties(device);
1664         acpi_bus_get_flags(device);
1665         device->flags.match_driver = false;
1666         device->flags.initialized = true;
1667         device->flags.enumeration_by_parent =
1668                 acpi_device_enumeration_by_parent(device);
1669         acpi_device_clear_enumerated(device);
1670         device_initialize(&device->dev);
1671         dev_set_uevent_suppress(&device->dev, true);
1672         acpi_init_coherency(device);
1673         /* Assume there are unmet deps to start with. */
1674         device->dep_unmet = 1;
1675 }
1676
1677 void acpi_device_add_finalize(struct acpi_device *device)
1678 {
1679         dev_set_uevent_suppress(&device->dev, false);
1680         kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1681 }
1682
1683 static int acpi_add_single_object(struct acpi_device **child,
1684                                   acpi_handle handle, int type,
1685                                   unsigned long long sta)
1686 {
1687         struct acpi_device_info *info = NULL;
1688         struct acpi_device *device;
1689         int result;
1690
1691         if (handle != ACPI_ROOT_OBJECT && type == ACPI_BUS_TYPE_DEVICE)
1692                 acpi_get_object_info(handle, &info);
1693
1694         device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1695         if (!device) {
1696                 kfree(info);
1697                 return -ENOMEM;
1698         }
1699
1700         acpi_init_device_object(device, handle, type, sta, info);
1701         kfree(info);
1702         /*
1703          * For ACPI_BUS_TYPE_DEVICE getting the status is delayed till here so
1704          * that we can call acpi_bus_get_status() and use its quirk handling.
1705          * Note this must be done before the get power-/wakeup_dev-flags calls.
1706          */
1707         if (type == ACPI_BUS_TYPE_DEVICE)
1708                 if (acpi_bus_get_status(device) < 0)
1709                         acpi_set_device_status(device, 0);
1710
1711         acpi_bus_get_power_flags(device);
1712         acpi_bus_get_wakeup_device_flags(device);
1713
1714         result = acpi_device_add(device, acpi_device_release);
1715         if (result) {
1716                 acpi_device_release(&device->dev);
1717                 return result;
1718         }
1719
1720         acpi_power_add_remove_device(device, true);
1721         acpi_device_add_finalize(device);
1722
1723         acpi_handle_debug(handle, "Added as %s, parent %s\n",
1724                           dev_name(&device->dev), device->parent ?
1725                                 dev_name(&device->parent->dev) : "(null)");
1726
1727         *child = device;
1728         return 0;
1729 }
1730
1731 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1732                                             void *context)
1733 {
1734         struct resource *res = context;
1735
1736         if (acpi_dev_resource_memory(ares, res))
1737                 return AE_CTRL_TERMINATE;
1738
1739         return AE_OK;
1740 }
1741
1742 static bool acpi_device_should_be_hidden(acpi_handle handle)
1743 {
1744         acpi_status status;
1745         struct resource res;
1746
1747         /* Check if it should ignore the UART device */
1748         if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1749                 return false;
1750
1751         /*
1752          * The UART device described in SPCR table is assumed to have only one
1753          * memory resource present. So we only look for the first one here.
1754          */
1755         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1756                                      acpi_get_resource_memory, &res);
1757         if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1758                 return false;
1759
1760         acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1761                          &res.start);
1762
1763         return true;
1764 }
1765
1766 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1767                                     unsigned long long *sta)
1768 {
1769         acpi_status status;
1770         acpi_object_type acpi_type;
1771
1772         status = acpi_get_type(handle, &acpi_type);
1773         if (ACPI_FAILURE(status))
1774                 return -ENODEV;
1775
1776         switch (acpi_type) {
1777         case ACPI_TYPE_ANY:             /* for ACPI_ROOT_OBJECT */
1778         case ACPI_TYPE_DEVICE:
1779                 if (acpi_device_should_be_hidden(handle))
1780                         return -ENODEV;
1781
1782                 *type = ACPI_BUS_TYPE_DEVICE;
1783                 /*
1784                  * acpi_add_single_object updates this once we've an acpi_device
1785                  * so that acpi_bus_get_status' quirk handling can be used.
1786                  */
1787                 *sta = ACPI_STA_DEFAULT;
1788                 break;
1789         case ACPI_TYPE_PROCESSOR:
1790                 *type = ACPI_BUS_TYPE_PROCESSOR;
1791                 status = acpi_bus_get_status_handle(handle, sta);
1792                 if (ACPI_FAILURE(status))
1793                         return -ENODEV;
1794                 break;
1795         case ACPI_TYPE_THERMAL:
1796                 *type = ACPI_BUS_TYPE_THERMAL;
1797                 *sta = ACPI_STA_DEFAULT;
1798                 break;
1799         case ACPI_TYPE_POWER:
1800                 *type = ACPI_BUS_TYPE_POWER;
1801                 *sta = ACPI_STA_DEFAULT;
1802                 break;
1803         default:
1804                 return -ENODEV;
1805         }
1806
1807         return 0;
1808 }
1809
1810 bool acpi_device_is_present(const struct acpi_device *adev)
1811 {
1812         return adev->status.present || adev->status.functional;
1813 }
1814
1815 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1816                                        const char *idstr,
1817                                        const struct acpi_device_id **matchid)
1818 {
1819         const struct acpi_device_id *devid;
1820
1821         if (handler->match)
1822                 return handler->match(idstr, matchid);
1823
1824         for (devid = handler->ids; devid->id[0]; devid++)
1825                 if (!strcmp((char *)devid->id, idstr)) {
1826                         if (matchid)
1827                                 *matchid = devid;
1828
1829                         return true;
1830                 }
1831
1832         return false;
1833 }
1834
1835 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1836                                         const struct acpi_device_id **matchid)
1837 {
1838         struct acpi_scan_handler *handler;
1839
1840         list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1841                 if (acpi_scan_handler_matching(handler, idstr, matchid))
1842                         return handler;
1843
1844         return NULL;
1845 }
1846
1847 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1848 {
1849         if (!!hotplug->enabled == !!val)
1850                 return;
1851
1852         mutex_lock(&acpi_scan_lock);
1853
1854         hotplug->enabled = val;
1855
1856         mutex_unlock(&acpi_scan_lock);
1857 }
1858
1859 static void acpi_scan_init_hotplug(struct acpi_device *adev)
1860 {
1861         struct acpi_hardware_id *hwid;
1862
1863         if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1864                 acpi_dock_add(adev);
1865                 return;
1866         }
1867         list_for_each_entry(hwid, &adev->pnp.ids, list) {
1868                 struct acpi_scan_handler *handler;
1869
1870                 handler = acpi_scan_match_handler(hwid->id, NULL);
1871                 if (handler) {
1872                         adev->flags.hotplug_notify = true;
1873                         break;
1874                 }
1875         }
1876 }
1877
1878 static u32 acpi_scan_check_dep(acpi_handle handle)
1879 {
1880         struct acpi_handle_list dep_devices;
1881         acpi_status status;
1882         u32 count;
1883         int i;
1884
1885         /*
1886          * Check for _HID here to avoid deferring the enumeration of:
1887          * 1. PCI devices.
1888          * 2. ACPI nodes describing USB ports.
1889          * Still, checking for _HID catches more then just these cases ...
1890          */
1891         if (!acpi_has_method(handle, "_DEP") || !acpi_has_method(handle, "_HID"))
1892                 return 0;
1893
1894         status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
1895         if (ACPI_FAILURE(status)) {
1896                 acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
1897                 return 0;
1898         }
1899
1900         for (count = 0, i = 0; i < dep_devices.count; i++) {
1901                 struct acpi_device_info *info;
1902                 struct acpi_dep_data *dep;
1903                 bool skip;
1904
1905                 status = acpi_get_object_info(dep_devices.handles[i], &info);
1906                 if (ACPI_FAILURE(status)) {
1907                         acpi_handle_debug(handle, "Error reading _DEP device info\n");
1908                         continue;
1909                 }
1910
1911                 skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
1912                 kfree(info);
1913
1914                 if (skip)
1915                         continue;
1916
1917                 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1918                 if (!dep)
1919                         continue;
1920
1921                 count++;
1922
1923                 dep->supplier = dep_devices.handles[i];
1924                 dep->consumer = handle;
1925
1926                 mutex_lock(&acpi_dep_list_lock);
1927                 list_add_tail(&dep->node , &acpi_dep_list);
1928                 mutex_unlock(&acpi_dep_list_lock);
1929         }
1930
1931         return count;
1932 }
1933
1934 static void acpi_scan_dep_init(struct acpi_device *adev)
1935 {
1936         struct acpi_dep_data *dep;
1937
1938         adev->dep_unmet = 0;
1939
1940         mutex_lock(&acpi_dep_list_lock);
1941
1942         list_for_each_entry(dep, &acpi_dep_list, node) {
1943                 if (dep->consumer == adev->handle)
1944                         adev->dep_unmet++;
1945         }
1946
1947         mutex_unlock(&acpi_dep_list_lock);
1948 }
1949
1950 static bool acpi_bus_scan_second_pass;
1951
1952 static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
1953                                       struct acpi_device **adev_p)
1954 {
1955         struct acpi_device *device = NULL;
1956         unsigned long long sta;
1957         int type;
1958         int result;
1959
1960         acpi_bus_get_device(handle, &device);
1961         if (device)
1962                 goto out;
1963
1964         result = acpi_bus_type_and_status(handle, &type, &sta);
1965         if (result)
1966                 return AE_OK;
1967
1968         if (type == ACPI_BUS_TYPE_POWER) {
1969                 acpi_add_power_resource(handle);
1970                 return AE_OK;
1971         }
1972
1973         if (type == ACPI_BUS_TYPE_DEVICE && check_dep) {
1974                 u32 count = acpi_scan_check_dep(handle);
1975                 /* Bail out if the number of recorded dependencies is not 0. */
1976                 if (count > 0) {
1977                         acpi_bus_scan_second_pass = true;
1978                         return AE_CTRL_DEPTH;
1979                 }
1980         }
1981
1982         acpi_add_single_object(&device, handle, type, sta);
1983         if (!device)
1984                 return AE_CTRL_DEPTH;
1985
1986         acpi_scan_init_hotplug(device);
1987         /*
1988          * If check_dep is true at this point, the device has no dependencies,
1989          * or the creation of the device object would have been postponed above.
1990          */
1991         if (check_dep)
1992                 device->dep_unmet = 0;
1993         else
1994                 acpi_scan_dep_init(device);
1995
1996 out:
1997         if (!*adev_p)
1998                 *adev_p = device;
1999
2000         return AE_OK;
2001 }
2002
2003 static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
2004                                         void *not_used, void **ret_p)
2005 {
2006         return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
2007 }
2008
2009 static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
2010                                         void *not_used, void **ret_p)
2011 {
2012         return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
2013 }
2014
2015 static void acpi_default_enumeration(struct acpi_device *device)
2016 {
2017         /*
2018          * Do not enumerate devices with enumeration_by_parent flag set as
2019          * they will be enumerated by their respective parents.
2020          */
2021         if (!device->flags.enumeration_by_parent) {
2022                 acpi_create_platform_device(device, NULL);
2023                 acpi_device_set_enumerated(device);
2024         } else {
2025                 blocking_notifier_call_chain(&acpi_reconfig_chain,
2026                                              ACPI_RECONFIG_DEVICE_ADD, device);
2027         }
2028 }
2029
2030 static const struct acpi_device_id generic_device_ids[] = {
2031         {ACPI_DT_NAMESPACE_HID, },
2032         {"", },
2033 };
2034
2035 static int acpi_generic_device_attach(struct acpi_device *adev,
2036                                       const struct acpi_device_id *not_used)
2037 {
2038         /*
2039          * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2040          * below can be unconditional.
2041          */
2042         if (adev->data.of_compatible)
2043                 acpi_default_enumeration(adev);
2044
2045         return 1;
2046 }
2047
2048 static struct acpi_scan_handler generic_device_handler = {
2049         .ids = generic_device_ids,
2050         .attach = acpi_generic_device_attach,
2051 };
2052
2053 static int acpi_scan_attach_handler(struct acpi_device *device)
2054 {
2055         struct acpi_hardware_id *hwid;
2056         int ret = 0;
2057
2058         list_for_each_entry(hwid, &device->pnp.ids, list) {
2059                 const struct acpi_device_id *devid;
2060                 struct acpi_scan_handler *handler;
2061
2062                 handler = acpi_scan_match_handler(hwid->id, &devid);
2063                 if (handler) {
2064                         if (!handler->attach) {
2065                                 device->pnp.type.platform_id = 0;
2066                                 continue;
2067                         }
2068                         device->handler = handler;
2069                         ret = handler->attach(device, devid);
2070                         if (ret > 0)
2071                                 break;
2072
2073                         device->handler = NULL;
2074                         if (ret < 0)
2075                                 break;
2076                 }
2077         }
2078
2079         return ret;
2080 }
2081
2082 static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
2083 {
2084         struct acpi_device *child;
2085         bool skip = !first_pass && device->flags.visited;
2086         acpi_handle ejd;
2087         int ret;
2088
2089         if (skip)
2090                 goto ok;
2091
2092         if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2093                 register_dock_dependent_device(device, ejd);
2094
2095         acpi_bus_get_status(device);
2096         /* Skip devices that are not present. */
2097         if (!acpi_device_is_present(device)) {
2098                 device->flags.initialized = false;
2099                 acpi_device_clear_enumerated(device);
2100                 device->flags.power_manageable = 0;
2101                 return;
2102         }
2103         if (device->handler)
2104                 goto ok;
2105
2106         if (!device->flags.initialized) {
2107                 device->flags.power_manageable =
2108                         device->power.states[ACPI_STATE_D0].flags.valid;
2109                 if (acpi_bus_init_power(device))
2110                         device->flags.power_manageable = 0;
2111
2112                 device->flags.initialized = true;
2113         } else if (device->flags.visited) {
2114                 goto ok;
2115         }
2116
2117         ret = acpi_scan_attach_handler(device);
2118         if (ret < 0)
2119                 return;
2120
2121         device->flags.match_driver = true;
2122         if (ret > 0 && !device->flags.enumeration_by_parent) {
2123                 acpi_device_set_enumerated(device);
2124                 goto ok;
2125         }
2126
2127         ret = device_attach(&device->dev);
2128         if (ret < 0)
2129                 return;
2130
2131         if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2132                 acpi_default_enumeration(device);
2133         else
2134                 acpi_device_set_enumerated(device);
2135
2136  ok:
2137         list_for_each_entry(child, &device->children, node)
2138                 acpi_bus_attach(child, first_pass);
2139
2140         if (!skip && device->handler && device->handler->hotplug.notify_online)
2141                 device->handler->hotplug.notify_online(device);
2142 }
2143
2144 void acpi_walk_dep_device_list(acpi_handle handle)
2145 {
2146         struct acpi_dep_data *dep, *tmp;
2147         struct acpi_device *adev;
2148
2149         mutex_lock(&acpi_dep_list_lock);
2150         list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2151                 if (dep->supplier == handle) {
2152                         acpi_bus_get_device(dep->consumer, &adev);
2153
2154                         if (adev) {
2155                                 adev->dep_unmet--;
2156                                 if (!adev->dep_unmet)
2157                                         acpi_bus_attach(adev, true);
2158                         }
2159
2160                         list_del(&dep->node);
2161                         kfree(dep);
2162                 }
2163         }
2164         mutex_unlock(&acpi_dep_list_lock);
2165 }
2166 EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2167
2168 /**
2169  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2170  * @handle: Root of the namespace scope to scan.
2171  *
2172  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2173  * found devices.
2174  *
2175  * If no devices were found, -ENODEV is returned, but it does not mean that
2176  * there has been a real error.  There just have been no suitable ACPI objects
2177  * in the table trunk from which the kernel could create a device and add an
2178  * appropriate driver.
2179  *
2180  * Must be called under acpi_scan_lock.
2181  */
2182 int acpi_bus_scan(acpi_handle handle)
2183 {
2184         struct acpi_device *device = NULL;
2185
2186         acpi_bus_scan_second_pass = false;
2187
2188         /* Pass 1: Avoid enumerating devices with missing dependencies. */
2189
2190         if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
2191                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2192                                     acpi_bus_check_add_1, NULL, NULL,
2193                                     (void **)&device);
2194
2195         if (!device)
2196                 return -ENODEV;
2197
2198         acpi_bus_attach(device, true);
2199
2200         if (!acpi_bus_scan_second_pass)
2201                 return 0;
2202
2203         /* Pass 2: Enumerate all of the remaining devices. */
2204
2205         device = NULL;
2206
2207         if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
2208                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2209                                     acpi_bus_check_add_2, NULL, NULL,
2210                                     (void **)&device);
2211
2212         acpi_bus_attach(device, false);
2213
2214         return 0;
2215 }
2216 EXPORT_SYMBOL(acpi_bus_scan);
2217
2218 /**
2219  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2220  * @adev: Root of the ACPI namespace scope to walk.
2221  *
2222  * Must be called under acpi_scan_lock.
2223  */
2224 void acpi_bus_trim(struct acpi_device *adev)
2225 {
2226         struct acpi_scan_handler *handler = adev->handler;
2227         struct acpi_device *child;
2228
2229         list_for_each_entry_reverse(child, &adev->children, node)
2230                 acpi_bus_trim(child);
2231
2232         adev->flags.match_driver = false;
2233         if (handler) {
2234                 if (handler->detach)
2235                         handler->detach(adev);
2236
2237                 adev->handler = NULL;
2238         } else {
2239                 device_release_driver(&adev->dev);
2240         }
2241         /*
2242          * Most likely, the device is going away, so put it into D3cold before
2243          * that.
2244          */
2245         acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2246         adev->flags.initialized = false;
2247         acpi_device_clear_enumerated(adev);
2248 }
2249 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2250
2251 int acpi_bus_register_early_device(int type)
2252 {
2253         struct acpi_device *device = NULL;
2254         int result;
2255
2256         result = acpi_add_single_object(&device, NULL,
2257                                         type, ACPI_STA_DEFAULT);
2258         if (result)
2259                 return result;
2260
2261         device->flags.match_driver = true;
2262         return device_attach(&device->dev);
2263 }
2264 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2265
2266 static int acpi_bus_scan_fixed(void)
2267 {
2268         int result = 0;
2269
2270         /*
2271          * Enumerate all fixed-feature devices.
2272          */
2273         if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2274                 struct acpi_device *device = NULL;
2275
2276                 result = acpi_add_single_object(&device, NULL,
2277                                                 ACPI_BUS_TYPE_POWER_BUTTON,
2278                                                 ACPI_STA_DEFAULT);
2279                 if (result)
2280                         return result;
2281
2282                 device->flags.match_driver = true;
2283                 result = device_attach(&device->dev);
2284                 if (result < 0)
2285                         return result;
2286
2287                 device_init_wakeup(&device->dev, true);
2288         }
2289
2290         if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2291                 struct acpi_device *device = NULL;
2292
2293                 result = acpi_add_single_object(&device, NULL,
2294                                                 ACPI_BUS_TYPE_SLEEP_BUTTON,
2295                                                 ACPI_STA_DEFAULT);
2296                 if (result)
2297                         return result;
2298
2299                 device->flags.match_driver = true;
2300                 result = device_attach(&device->dev);
2301         }
2302
2303         return result < 0 ? result : 0;
2304 }
2305
2306 static void __init acpi_get_spcr_uart_addr(void)
2307 {
2308         acpi_status status;
2309         struct acpi_table_spcr *spcr_ptr;
2310
2311         status = acpi_get_table(ACPI_SIG_SPCR, 0,
2312                                 (struct acpi_table_header **)&spcr_ptr);
2313         if (ACPI_FAILURE(status)) {
2314                 pr_warn(PREFIX "STAO table present, but SPCR is missing\n");
2315                 return;
2316         }
2317
2318         spcr_uart_addr = spcr_ptr->serial_port.address;
2319         acpi_put_table((struct acpi_table_header *)spcr_ptr);
2320 }
2321
2322 static bool acpi_scan_initialized;
2323
2324 int __init acpi_scan_init(void)
2325 {
2326         int result;
2327         acpi_status status;
2328         struct acpi_table_stao *stao_ptr;
2329
2330         acpi_pci_root_init();
2331         acpi_pci_link_init();
2332         acpi_processor_init();
2333         acpi_platform_init();
2334         acpi_lpss_init();
2335         acpi_apd_init();
2336         acpi_cmos_rtc_init();
2337         acpi_container_init();
2338         acpi_memory_hotplug_init();
2339         acpi_watchdog_init();
2340         acpi_pnp_init();
2341         acpi_int340x_thermal_init();
2342         acpi_amba_init();
2343         acpi_init_lpit();
2344
2345         acpi_scan_add_handler(&generic_device_handler);
2346
2347         /*
2348          * If there is STAO table, check whether it needs to ignore the UART
2349          * device in SPCR table.
2350          */
2351         status = acpi_get_table(ACPI_SIG_STAO, 0,
2352                                 (struct acpi_table_header **)&stao_ptr);
2353         if (ACPI_SUCCESS(status)) {
2354                 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2355                         pr_info(PREFIX "STAO Name List not yet supported.\n");
2356
2357                 if (stao_ptr->ignore_uart)
2358                         acpi_get_spcr_uart_addr();
2359
2360                 acpi_put_table((struct acpi_table_header *)stao_ptr);
2361         }
2362
2363         acpi_gpe_apply_masked_gpes();
2364         acpi_update_all_gpes();
2365
2366         /*
2367          * Although we call __add_memory() that is documented to require the
2368          * device_hotplug_lock, it is not necessary here because this is an
2369          * early code when userspace or any other code path cannot trigger
2370          * hotplug/hotunplug operations.
2371          */
2372         mutex_lock(&acpi_scan_lock);
2373         /*
2374          * Enumerate devices in the ACPI namespace.
2375          */
2376         result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2377         if (result)
2378                 goto out;
2379
2380         result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2381         if (result)
2382                 goto out;
2383
2384         /* Fixed feature devices do not exist on HW-reduced platform */
2385         if (!acpi_gbl_reduced_hardware) {
2386                 result = acpi_bus_scan_fixed();
2387                 if (result) {
2388                         acpi_detach_data(acpi_root->handle,
2389                                          acpi_scan_drop_device);
2390                         acpi_device_del(acpi_root);
2391                         put_device(&acpi_root->dev);
2392                         goto out;
2393                 }
2394         }
2395
2396         acpi_scan_initialized = true;
2397
2398  out:
2399         mutex_unlock(&acpi_scan_lock);
2400         return result;
2401 }
2402
2403 static struct acpi_probe_entry *ape;
2404 static int acpi_probe_count;
2405 static DEFINE_MUTEX(acpi_probe_mutex);
2406
2407 static int __init acpi_match_madt(union acpi_subtable_headers *header,
2408                                   const unsigned long end)
2409 {
2410         if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2411                 if (!ape->probe_subtbl(header, end))
2412                         acpi_probe_count++;
2413
2414         return 0;
2415 }
2416
2417 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2418 {
2419         int count = 0;
2420
2421         if (acpi_disabled)
2422                 return 0;
2423
2424         mutex_lock(&acpi_probe_mutex);
2425         for (ape = ap_head; nr; ape++, nr--) {
2426                 if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2427                         acpi_probe_count = 0;
2428                         acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2429                         count += acpi_probe_count;
2430                 } else {
2431                         int res;
2432                         res = acpi_table_parse(ape->id, ape->probe_table);
2433                         if (!res)
2434                                 count++;
2435                 }
2436         }
2437         mutex_unlock(&acpi_probe_mutex);
2438
2439         return count;
2440 }
2441
2442 struct acpi_table_events_work {
2443         struct work_struct work;
2444         void *table;
2445         u32 event;
2446 };
2447
2448 static void acpi_table_events_fn(struct work_struct *work)
2449 {
2450         struct acpi_table_events_work *tew;
2451
2452         tew = container_of(work, struct acpi_table_events_work, work);
2453
2454         if (tew->event == ACPI_TABLE_EVENT_LOAD) {
2455                 acpi_scan_lock_acquire();
2456                 acpi_bus_scan(ACPI_ROOT_OBJECT);
2457                 acpi_scan_lock_release();
2458         }
2459
2460         kfree(tew);
2461 }
2462
2463 void acpi_scan_table_handler(u32 event, void *table, void *context)
2464 {
2465         struct acpi_table_events_work *tew;
2466
2467         if (!acpi_scan_initialized)
2468                 return;
2469
2470         if (event != ACPI_TABLE_EVENT_LOAD)
2471                 return;
2472
2473         tew = kmalloc(sizeof(*tew), GFP_KERNEL);
2474         if (!tew)
2475                 return;
2476
2477         INIT_WORK(&tew->work, acpi_table_events_fn);
2478         tew->table = table;
2479         tew->event = event;
2480
2481         schedule_work(&tew->work);
2482 }
2483
2484 int acpi_reconfig_notifier_register(struct notifier_block *nb)
2485 {
2486         return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2487 }
2488 EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2489
2490 int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2491 {
2492         return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2493 }
2494 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);