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