Merge branches 'acpi-prm', 'acpi-sysfs' and 'acpi-x86'
[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 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
610 {
611         struct acpi_device_bus_id *acpi_device_bus_id;
612
613         /* Find suitable bus_id and instance number in acpi_bus_id_list. */
614         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
615                 if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
616                         return acpi_device_bus_id;
617         }
618         return NULL;
619 }
620
621 static int acpi_device_set_name(struct acpi_device *device,
622                                 struct acpi_device_bus_id *acpi_device_bus_id)
623 {
624         struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
625         int result;
626
627         result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
628         if (result < 0)
629                 return result;
630
631         device->pnp.instance_no = result;
632         dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
633         return 0;
634 }
635
636 static int acpi_tie_acpi_dev(struct acpi_device *adev)
637 {
638         acpi_handle handle = adev->handle;
639         acpi_status status;
640
641         if (!handle)
642                 return 0;
643
644         status = acpi_attach_data(handle, acpi_scan_drop_device, adev);
645         if (ACPI_FAILURE(status)) {
646                 acpi_handle_err(handle, "Unable to attach device data\n");
647                 return -ENODEV;
648         }
649
650         return 0;
651 }
652
653 static int __acpi_device_add(struct acpi_device *device,
654                              void (*release)(struct device *))
655 {
656         struct acpi_device_bus_id *acpi_device_bus_id;
657         int result;
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 int acpi_device_add(struct acpi_device *adev, void (*release)(struct device *))
748 {
749         int ret;
750
751         ret = acpi_tie_acpi_dev(adev);
752         if (ret)
753                 return ret;
754
755         return __acpi_device_add(adev, release);
756 }
757
758 /* --------------------------------------------------------------------------
759                                  Device Enumeration
760    -------------------------------------------------------------------------- */
761 static bool acpi_info_matches_ids(struct acpi_device_info *info,
762                                   const char * const ids[])
763 {
764         struct acpi_pnp_device_id_list *cid_list = NULL;
765         int i, index;
766
767         if (!(info->valid & ACPI_VALID_HID))
768                 return false;
769
770         index = match_string(ids, -1, info->hardware_id.string);
771         if (index >= 0)
772                 return true;
773
774         if (info->valid & ACPI_VALID_CID)
775                 cid_list = &info->compatible_id_list;
776
777         if (!cid_list)
778                 return false;
779
780         for (i = 0; i < cid_list->count; i++) {
781                 index = match_string(ids, -1, cid_list->ids[i].string);
782                 if (index >= 0)
783                         return true;
784         }
785
786         return false;
787 }
788
789 /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
790 static const char * const acpi_ignore_dep_ids[] = {
791         "PNP0D80", /* Windows-compatible System Power Management Controller */
792         "INT33BD", /* Intel Baytrail Mailbox Device */
793         NULL
794 };
795
796 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
797 {
798         struct acpi_device *device = NULL;
799         acpi_status status;
800
801         /*
802          * Fixed hardware devices do not appear in the namespace and do not
803          * have handles, but we fabricate acpi_devices for them, so we have
804          * to deal with them specially.
805          */
806         if (!handle)
807                 return acpi_root;
808
809         do {
810                 status = acpi_get_parent(handle, &handle);
811                 if (ACPI_FAILURE(status))
812                         return status == AE_NULL_ENTRY ? NULL : acpi_root;
813         } while (acpi_bus_get_device(handle, &device));
814         return device;
815 }
816
817 acpi_status
818 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
819 {
820         acpi_status status;
821         acpi_handle tmp;
822         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
823         union acpi_object *obj;
824
825         status = acpi_get_handle(handle, "_EJD", &tmp);
826         if (ACPI_FAILURE(status))
827                 return status;
828
829         status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
830         if (ACPI_SUCCESS(status)) {
831                 obj = buffer.pointer;
832                 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
833                                          ejd);
834                 kfree(buffer.pointer);
835         }
836         return status;
837 }
838 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
839
840 static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
841 {
842         acpi_handle handle = dev->handle;
843         struct acpi_device_wakeup *wakeup = &dev->wakeup;
844         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
845         union acpi_object *package = NULL;
846         union acpi_object *element = NULL;
847         acpi_status status;
848         int err = -ENODATA;
849
850         INIT_LIST_HEAD(&wakeup->resources);
851
852         /* _PRW */
853         status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
854         if (ACPI_FAILURE(status)) {
855                 acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
856                                  acpi_format_exception(status));
857                 return err;
858         }
859
860         package = (union acpi_object *)buffer.pointer;
861
862         if (!package || package->package.count < 2)
863                 goto out;
864
865         element = &(package->package.elements[0]);
866         if (!element)
867                 goto out;
868
869         if (element->type == ACPI_TYPE_PACKAGE) {
870                 if ((element->package.count < 2) ||
871                     (element->package.elements[0].type !=
872                      ACPI_TYPE_LOCAL_REFERENCE)
873                     || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
874                         goto out;
875
876                 wakeup->gpe_device =
877                     element->package.elements[0].reference.handle;
878                 wakeup->gpe_number =
879                     (u32) element->package.elements[1].integer.value;
880         } else if (element->type == ACPI_TYPE_INTEGER) {
881                 wakeup->gpe_device = NULL;
882                 wakeup->gpe_number = element->integer.value;
883         } else {
884                 goto out;
885         }
886
887         element = &(package->package.elements[1]);
888         if (element->type != ACPI_TYPE_INTEGER)
889                 goto out;
890
891         wakeup->sleep_state = element->integer.value;
892
893         err = acpi_extract_power_resources(package, 2, &wakeup->resources);
894         if (err)
895                 goto out;
896
897         if (!list_empty(&wakeup->resources)) {
898                 int sleep_state;
899
900                 err = acpi_power_wakeup_list_init(&wakeup->resources,
901                                                   &sleep_state);
902                 if (err) {
903                         acpi_handle_warn(handle, "Retrieving current states "
904                                          "of wakeup power resources failed\n");
905                         acpi_power_resources_list_free(&wakeup->resources);
906                         goto out;
907                 }
908                 if (sleep_state < wakeup->sleep_state) {
909                         acpi_handle_warn(handle, "Overriding _PRW sleep state "
910                                          "(S%d) by S%d from power resources\n",
911                                          (int)wakeup->sleep_state, sleep_state);
912                         wakeup->sleep_state = sleep_state;
913                 }
914         }
915
916  out:
917         kfree(buffer.pointer);
918         return err;
919 }
920
921 static bool acpi_wakeup_gpe_init(struct acpi_device *device)
922 {
923         static const struct acpi_device_id button_device_ids[] = {
924                 {"PNP0C0C", 0},         /* Power button */
925                 {"PNP0C0D", 0},         /* Lid */
926                 {"PNP0C0E", 0},         /* Sleep button */
927                 {"", 0},
928         };
929         struct acpi_device_wakeup *wakeup = &device->wakeup;
930         acpi_status status;
931
932         wakeup->flags.notifier_present = 0;
933
934         /* Power button, Lid switch always enable wakeup */
935         if (!acpi_match_device_ids(device, button_device_ids)) {
936                 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
937                         /* Do not use Lid/sleep button for S5 wakeup */
938                         if (wakeup->sleep_state == ACPI_STATE_S5)
939                                 wakeup->sleep_state = ACPI_STATE_S4;
940                 }
941                 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
942                 device_set_wakeup_capable(&device->dev, true);
943                 return true;
944         }
945
946         status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
947                                          wakeup->gpe_number);
948         return ACPI_SUCCESS(status);
949 }
950
951 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
952 {
953         int err;
954
955         /* Presence of _PRW indicates wake capable */
956         if (!acpi_has_method(device->handle, "_PRW"))
957                 return;
958
959         err = acpi_bus_extract_wakeup_device_power_package(device);
960         if (err) {
961                 dev_err(&device->dev, "Unable to extract wakeup power resources");
962                 return;
963         }
964
965         device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
966         device->wakeup.prepare_count = 0;
967         /*
968          * Call _PSW/_DSW object to disable its ability to wake the sleeping
969          * system for the ACPI device with the _PRW object.
970          * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
971          * So it is necessary to call _DSW object first. Only when it is not
972          * present will the _PSW object used.
973          */
974         err = acpi_device_sleep_wake(device, 0, 0, 0);
975         if (err)
976                 pr_debug("error in _DSW or _PSW evaluation\n");
977 }
978
979 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
980 {
981         struct acpi_device_power_state *ps = &device->power.states[state];
982         char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
983         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
984         acpi_status status;
985
986         INIT_LIST_HEAD(&ps->resources);
987
988         /* Evaluate "_PRx" to get referenced power resources */
989         status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
990         if (ACPI_SUCCESS(status)) {
991                 union acpi_object *package = buffer.pointer;
992
993                 if (buffer.length && package
994                     && package->type == ACPI_TYPE_PACKAGE
995                     && package->package.count)
996                         acpi_extract_power_resources(package, 0, &ps->resources);
997
998                 ACPI_FREE(buffer.pointer);
999         }
1000
1001         /* Evaluate "_PSx" to see if we can do explicit sets */
1002         pathname[2] = 'S';
1003         if (acpi_has_method(device->handle, pathname))
1004                 ps->flags.explicit_set = 1;
1005
1006         /* State is valid if there are means to put the device into it. */
1007         if (!list_empty(&ps->resources) || ps->flags.explicit_set)
1008                 ps->flags.valid = 1;
1009
1010         ps->power = -1;         /* Unknown - driver assigned */
1011         ps->latency = -1;       /* Unknown - driver assigned */
1012 }
1013
1014 static void acpi_bus_get_power_flags(struct acpi_device *device)
1015 {
1016         u32 i;
1017
1018         /* Presence of _PS0|_PR0 indicates 'power manageable' */
1019         if (!acpi_has_method(device->handle, "_PS0") &&
1020             !acpi_has_method(device->handle, "_PR0"))
1021                 return;
1022
1023         device->flags.power_manageable = 1;
1024
1025         /*
1026          * Power Management Flags
1027          */
1028         if (acpi_has_method(device->handle, "_PSC"))
1029                 device->power.flags.explicit_get = 1;
1030
1031         if (acpi_has_method(device->handle, "_IRC"))
1032                 device->power.flags.inrush_current = 1;
1033
1034         if (acpi_has_method(device->handle, "_DSW"))
1035                 device->power.flags.dsw_present = 1;
1036
1037         /*
1038          * Enumerate supported power management states
1039          */
1040         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1041                 acpi_bus_init_power_state(device, i);
1042
1043         INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1044
1045         /* Set the defaults for D0 and D3hot (always supported). */
1046         device->power.states[ACPI_STATE_D0].flags.valid = 1;
1047         device->power.states[ACPI_STATE_D0].power = 100;
1048         device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1049
1050         /*
1051          * Use power resources only if the D0 list of them is populated, because
1052          * some platforms may provide _PR3 only to indicate D3cold support and
1053          * in those cases the power resources list returned by it may be bogus.
1054          */
1055         if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1056                 device->power.flags.power_resources = 1;
1057                 /*
1058                  * D3cold is supported if the D3hot list of power resources is
1059                  * not empty.
1060                  */
1061                 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1062                         device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1063         }
1064
1065         if (acpi_bus_init_power(device))
1066                 device->flags.power_manageable = 0;
1067 }
1068
1069 static void acpi_bus_get_flags(struct acpi_device *device)
1070 {
1071         /* Presence of _STA indicates 'dynamic_status' */
1072         if (acpi_has_method(device->handle, "_STA"))
1073                 device->flags.dynamic_status = 1;
1074
1075         /* Presence of _RMV indicates 'removable' */
1076         if (acpi_has_method(device->handle, "_RMV"))
1077                 device->flags.removable = 1;
1078
1079         /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1080         if (acpi_has_method(device->handle, "_EJD") ||
1081             acpi_has_method(device->handle, "_EJ0"))
1082                 device->flags.ejectable = 1;
1083 }
1084
1085 static void acpi_device_get_busid(struct acpi_device *device)
1086 {
1087         char bus_id[5] = { '?', 0 };
1088         struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1089         int i = 0;
1090
1091         /*
1092          * Bus ID
1093          * ------
1094          * The device's Bus ID is simply the object name.
1095          * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1096          */
1097         if (ACPI_IS_ROOT_DEVICE(device)) {
1098                 strcpy(device->pnp.bus_id, "ACPI");
1099                 return;
1100         }
1101
1102         switch (device->device_type) {
1103         case ACPI_BUS_TYPE_POWER_BUTTON:
1104                 strcpy(device->pnp.bus_id, "PWRF");
1105                 break;
1106         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1107                 strcpy(device->pnp.bus_id, "SLPF");
1108                 break;
1109         case ACPI_BUS_TYPE_ECDT_EC:
1110                 strcpy(device->pnp.bus_id, "ECDT");
1111                 break;
1112         default:
1113                 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1114                 /* Clean up trailing underscores (if any) */
1115                 for (i = 3; i > 1; i--) {
1116                         if (bus_id[i] == '_')
1117                                 bus_id[i] = '\0';
1118                         else
1119                                 break;
1120                 }
1121                 strcpy(device->pnp.bus_id, bus_id);
1122                 break;
1123         }
1124 }
1125
1126 /*
1127  * acpi_ata_match - see if an acpi object is an ATA device
1128  *
1129  * If an acpi object has one of the ACPI ATA methods defined,
1130  * then we can safely call it an ATA device.
1131  */
1132 bool acpi_ata_match(acpi_handle handle)
1133 {
1134         return acpi_has_method(handle, "_GTF") ||
1135                acpi_has_method(handle, "_GTM") ||
1136                acpi_has_method(handle, "_STM") ||
1137                acpi_has_method(handle, "_SDD");
1138 }
1139
1140 /*
1141  * acpi_bay_match - see if an acpi object is an ejectable driver bay
1142  *
1143  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1144  * then we can safely call it an ejectable drive bay
1145  */
1146 bool acpi_bay_match(acpi_handle handle)
1147 {
1148         acpi_handle phandle;
1149
1150         if (!acpi_has_method(handle, "_EJ0"))
1151                 return false;
1152         if (acpi_ata_match(handle))
1153                 return true;
1154         if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1155                 return false;
1156
1157         return acpi_ata_match(phandle);
1158 }
1159
1160 bool acpi_device_is_battery(struct acpi_device *adev)
1161 {
1162         struct acpi_hardware_id *hwid;
1163
1164         list_for_each_entry(hwid, &adev->pnp.ids, list)
1165                 if (!strcmp("PNP0C0A", hwid->id))
1166                         return true;
1167
1168         return false;
1169 }
1170
1171 static bool is_ejectable_bay(struct acpi_device *adev)
1172 {
1173         acpi_handle handle = adev->handle;
1174
1175         if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1176                 return true;
1177
1178         return acpi_bay_match(handle);
1179 }
1180
1181 /*
1182  * acpi_dock_match - see if an acpi object has a _DCK method
1183  */
1184 bool acpi_dock_match(acpi_handle handle)
1185 {
1186         return acpi_has_method(handle, "_DCK");
1187 }
1188
1189 static acpi_status
1190 acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1191                           void **return_value)
1192 {
1193         long *cap = context;
1194
1195         if (acpi_has_method(handle, "_BCM") &&
1196             acpi_has_method(handle, "_BCL")) {
1197                 acpi_handle_debug(handle, "Found generic backlight support\n");
1198                 *cap |= ACPI_VIDEO_BACKLIGHT;
1199                 /* We have backlight support, no need to scan further */
1200                 return AE_CTRL_TERMINATE;
1201         }
1202         return 0;
1203 }
1204
1205 /* Returns true if the ACPI object is a video device which can be
1206  * handled by video.ko.
1207  * The device will get a Linux specific CID added in scan.c to
1208  * identify the device as an ACPI graphics device
1209  * Be aware that the graphics device may not be physically present
1210  * Use acpi_video_get_capabilities() to detect general ACPI video
1211  * capabilities of present cards
1212  */
1213 long acpi_is_video_device(acpi_handle handle)
1214 {
1215         long video_caps = 0;
1216
1217         /* Is this device able to support video switching ? */
1218         if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1219                 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1220
1221         /* Is this device able to retrieve a video ROM ? */
1222         if (acpi_has_method(handle, "_ROM"))
1223                 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1224
1225         /* Is this device able to configure which video head to be POSTed ? */
1226         if (acpi_has_method(handle, "_VPO") &&
1227             acpi_has_method(handle, "_GPD") &&
1228             acpi_has_method(handle, "_SPD"))
1229                 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1230
1231         /* Only check for backlight functionality if one of the above hit. */
1232         if (video_caps)
1233                 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1234                                     ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1235                                     &video_caps, NULL);
1236
1237         return video_caps;
1238 }
1239 EXPORT_SYMBOL(acpi_is_video_device);
1240
1241 const char *acpi_device_hid(struct acpi_device *device)
1242 {
1243         struct acpi_hardware_id *hid;
1244
1245         if (list_empty(&device->pnp.ids))
1246                 return dummy_hid;
1247
1248         hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1249         return hid->id;
1250 }
1251 EXPORT_SYMBOL(acpi_device_hid);
1252
1253 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1254 {
1255         struct acpi_hardware_id *id;
1256
1257         id = kmalloc(sizeof(*id), GFP_KERNEL);
1258         if (!id)
1259                 return;
1260
1261         id->id = kstrdup_const(dev_id, GFP_KERNEL);
1262         if (!id->id) {
1263                 kfree(id);
1264                 return;
1265         }
1266
1267         list_add_tail(&id->list, &pnp->ids);
1268         pnp->type.hardware_id = 1;
1269 }
1270
1271 /*
1272  * Old IBM workstations have a DSDT bug wherein the SMBus object
1273  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1274  * prefix.  Work around this.
1275  */
1276 static bool acpi_ibm_smbus_match(acpi_handle handle)
1277 {
1278         char node_name[ACPI_PATH_SEGMENT_LENGTH];
1279         struct acpi_buffer path = { sizeof(node_name), node_name };
1280
1281         if (!dmi_name_in_vendors("IBM"))
1282                 return false;
1283
1284         /* Look for SMBS object */
1285         if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1286             strcmp("SMBS", path.pointer))
1287                 return false;
1288
1289         /* Does it have the necessary (but misnamed) methods? */
1290         if (acpi_has_method(handle, "SBI") &&
1291             acpi_has_method(handle, "SBR") &&
1292             acpi_has_method(handle, "SBW"))
1293                 return true;
1294
1295         return false;
1296 }
1297
1298 static bool acpi_object_is_system_bus(acpi_handle handle)
1299 {
1300         acpi_handle tmp;
1301
1302         if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1303             tmp == handle)
1304                 return true;
1305         if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1306             tmp == handle)
1307                 return true;
1308
1309         return false;
1310 }
1311
1312 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1313                              int device_type)
1314 {
1315         struct acpi_device_info *info = NULL;
1316         struct acpi_pnp_device_id_list *cid_list;
1317         int i;
1318
1319         switch (device_type) {
1320         case ACPI_BUS_TYPE_DEVICE:
1321                 if (handle == ACPI_ROOT_OBJECT) {
1322                         acpi_add_id(pnp, ACPI_SYSTEM_HID);
1323                         break;
1324                 }
1325
1326                 acpi_get_object_info(handle, &info);
1327                 if (!info) {
1328                         pr_err(PREFIX "%s: Error reading device info\n",
1329                                         __func__);
1330                         return;
1331                 }
1332
1333                 if (info->valid & ACPI_VALID_HID) {
1334                         acpi_add_id(pnp, info->hardware_id.string);
1335                         pnp->type.platform_id = 1;
1336                 }
1337                 if (info->valid & ACPI_VALID_CID) {
1338                         cid_list = &info->compatible_id_list;
1339                         for (i = 0; i < cid_list->count; i++)
1340                                 acpi_add_id(pnp, cid_list->ids[i].string);
1341                 }
1342                 if (info->valid & ACPI_VALID_ADR) {
1343                         pnp->bus_address = info->address;
1344                         pnp->type.bus_address = 1;
1345                 }
1346                 if (info->valid & ACPI_VALID_UID)
1347                         pnp->unique_id = kstrdup(info->unique_id.string,
1348                                                         GFP_KERNEL);
1349                 if (info->valid & ACPI_VALID_CLS)
1350                         acpi_add_id(pnp, info->class_code.string);
1351
1352                 kfree(info);
1353
1354                 /*
1355                  * Some devices don't reliably have _HIDs & _CIDs, so add
1356                  * synthetic HIDs to make sure drivers can find them.
1357                  */
1358                 if (acpi_is_video_device(handle))
1359                         acpi_add_id(pnp, ACPI_VIDEO_HID);
1360                 else if (acpi_bay_match(handle))
1361                         acpi_add_id(pnp, ACPI_BAY_HID);
1362                 else if (acpi_dock_match(handle))
1363                         acpi_add_id(pnp, ACPI_DOCK_HID);
1364                 else if (acpi_ibm_smbus_match(handle))
1365                         acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1366                 else if (list_empty(&pnp->ids) &&
1367                          acpi_object_is_system_bus(handle)) {
1368                         /* \_SB, \_TZ, LNXSYBUS */
1369                         acpi_add_id(pnp, ACPI_BUS_HID);
1370                         strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1371                         strcpy(pnp->device_class, ACPI_BUS_CLASS);
1372                 }
1373
1374                 break;
1375         case ACPI_BUS_TYPE_POWER:
1376                 acpi_add_id(pnp, ACPI_POWER_HID);
1377                 break;
1378         case ACPI_BUS_TYPE_PROCESSOR:
1379                 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1380                 break;
1381         case ACPI_BUS_TYPE_THERMAL:
1382                 acpi_add_id(pnp, ACPI_THERMAL_HID);
1383                 break;
1384         case ACPI_BUS_TYPE_POWER_BUTTON:
1385                 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1386                 break;
1387         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1388                 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1389                 break;
1390         case ACPI_BUS_TYPE_ECDT_EC:
1391                 acpi_add_id(pnp, ACPI_ECDT_HID);
1392                 break;
1393         }
1394 }
1395
1396 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1397 {
1398         struct acpi_hardware_id *id, *tmp;
1399
1400         list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1401                 kfree_const(id->id);
1402                 kfree(id);
1403         }
1404         kfree(pnp->unique_id);
1405 }
1406
1407 /**
1408  * acpi_dma_supported - Check DMA support for the specified device.
1409  * @adev: The pointer to acpi device
1410  *
1411  * Return false if DMA is not supported. Otherwise, return true
1412  */
1413 bool acpi_dma_supported(struct acpi_device *adev)
1414 {
1415         if (!adev)
1416                 return false;
1417
1418         if (adev->flags.cca_seen)
1419                 return true;
1420
1421         /*
1422         * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1423         * DMA on "Intel platforms".  Presumably that includes all x86 and
1424         * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1425         */
1426         if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1427                 return true;
1428
1429         return false;
1430 }
1431
1432 /**
1433  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1434  * @adev: The pointer to acpi device
1435  *
1436  * Return enum dev_dma_attr.
1437  */
1438 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1439 {
1440         if (!acpi_dma_supported(adev))
1441                 return DEV_DMA_NOT_SUPPORTED;
1442
1443         if (adev->flags.coherent_dma)
1444                 return DEV_DMA_COHERENT;
1445         else
1446                 return DEV_DMA_NON_COHERENT;
1447 }
1448
1449 /**
1450  * acpi_dma_get_range() - Get device DMA parameters.
1451  *
1452  * @dev: device to configure
1453  * @dma_addr: pointer device DMA address result
1454  * @offset: pointer to the DMA offset result
1455  * @size: pointer to DMA range size result
1456  *
1457  * Evaluate DMA regions and return respectively DMA region start, offset
1458  * and size in dma_addr, offset and size on parsing success; it does not
1459  * update the passed in values on failure.
1460  *
1461  * Return 0 on success, < 0 on failure.
1462  */
1463 int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1464                        u64 *size)
1465 {
1466         struct acpi_device *adev;
1467         LIST_HEAD(list);
1468         struct resource_entry *rentry;
1469         int ret;
1470         struct device *dma_dev = dev;
1471         u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1472
1473         /*
1474          * Walk the device tree chasing an ACPI companion with a _DMA
1475          * object while we go. Stop if we find a device with an ACPI
1476          * companion containing a _DMA method.
1477          */
1478         do {
1479                 adev = ACPI_COMPANION(dma_dev);
1480                 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1481                         break;
1482
1483                 dma_dev = dma_dev->parent;
1484         } while (dma_dev);
1485
1486         if (!dma_dev)
1487                 return -ENODEV;
1488
1489         if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1490                 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1491                 return -EINVAL;
1492         }
1493
1494         ret = acpi_dev_get_dma_resources(adev, &list);
1495         if (ret > 0) {
1496                 list_for_each_entry(rentry, &list, node) {
1497                         if (dma_offset && rentry->offset != dma_offset) {
1498                                 ret = -EINVAL;
1499                                 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1500                                 goto out;
1501                         }
1502                         dma_offset = rentry->offset;
1503
1504                         /* Take lower and upper limits */
1505                         if (rentry->res->start < dma_start)
1506                                 dma_start = rentry->res->start;
1507                         if (rentry->res->end > dma_end)
1508                                 dma_end = rentry->res->end;
1509                 }
1510
1511                 if (dma_start >= dma_end) {
1512                         ret = -EINVAL;
1513                         dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1514                         goto out;
1515                 }
1516
1517                 *dma_addr = dma_start - dma_offset;
1518                 len = dma_end - dma_start;
1519                 *size = max(len, len + 1);
1520                 *offset = dma_offset;
1521         }
1522  out:
1523         acpi_dev_free_resource_list(&list);
1524
1525         return ret >= 0 ? 0 : ret;
1526 }
1527
1528 /**
1529  * acpi_dma_configure_id - Set-up DMA configuration for the device.
1530  * @dev: The pointer to the device
1531  * @attr: device dma attributes
1532  * @input_id: input device id const value pointer
1533  */
1534 int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1535                           const u32 *input_id)
1536 {
1537         const struct iommu_ops *iommu;
1538         u64 dma_addr = 0, size = 0;
1539
1540         if (attr == DEV_DMA_NOT_SUPPORTED) {
1541                 set_dma_ops(dev, &dma_dummy_ops);
1542                 return 0;
1543         }
1544
1545         iort_dma_setup(dev, &dma_addr, &size);
1546
1547         iommu = iort_iommu_configure_id(dev, input_id);
1548         if (PTR_ERR(iommu) == -EPROBE_DEFER)
1549                 return -EPROBE_DEFER;
1550
1551         arch_setup_dma_ops(dev, dma_addr, size,
1552                                 iommu, attr == DEV_DMA_COHERENT);
1553
1554         return 0;
1555 }
1556 EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1557
1558 static void acpi_init_coherency(struct acpi_device *adev)
1559 {
1560         unsigned long long cca = 0;
1561         acpi_status status;
1562         struct acpi_device *parent = adev->parent;
1563
1564         if (parent && parent->flags.cca_seen) {
1565                 /*
1566                  * From ACPI spec, OSPM will ignore _CCA if an ancestor
1567                  * already saw one.
1568                  */
1569                 adev->flags.cca_seen = 1;
1570                 cca = parent->flags.coherent_dma;
1571         } else {
1572                 status = acpi_evaluate_integer(adev->handle, "_CCA",
1573                                                NULL, &cca);
1574                 if (ACPI_SUCCESS(status))
1575                         adev->flags.cca_seen = 1;
1576                 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1577                         /*
1578                          * If architecture does not specify that _CCA is
1579                          * required for DMA-able devices (e.g. x86),
1580                          * we default to _CCA=1.
1581                          */
1582                         cca = 1;
1583                 else
1584                         acpi_handle_debug(adev->handle,
1585                                           "ACPI device is missing _CCA.\n");
1586         }
1587
1588         adev->flags.coherent_dma = cca;
1589 }
1590
1591 static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1592 {
1593         bool *is_serial_bus_slave_p = data;
1594
1595         if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1596                 return 1;
1597
1598         *is_serial_bus_slave_p = true;
1599
1600          /* no need to do more checking */
1601         return -1;
1602 }
1603
1604 static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1605 {
1606         struct acpi_device *parent = device->parent;
1607         static const struct acpi_device_id indirect_io_hosts[] = {
1608                 {"HISI0191", 0},
1609                 {}
1610         };
1611
1612         return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1613 }
1614
1615 static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1616 {
1617         struct list_head resource_list;
1618         bool is_serial_bus_slave = false;
1619         /*
1620          * These devices have multiple I2cSerialBus resources and an i2c-client
1621          * must be instantiated for each, each with its own i2c_device_id.
1622          * Normally we only instantiate an i2c-client for the first resource,
1623          * using the ACPI HID as id. These special cases are handled by the
1624          * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1625          * which i2c_device_id to use for each resource.
1626          */
1627         static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1628                 {"BSG1160", },
1629                 {"BSG2150", },
1630                 {"INT33FE", },
1631                 {"INT3515", },
1632                 {}
1633         };
1634
1635         if (acpi_is_indirect_io_slave(device))
1636                 return true;
1637
1638         /* Macs use device properties in lieu of _CRS resources */
1639         if (x86_apple_machine &&
1640             (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1641              fwnode_property_present(&device->fwnode, "i2cAddress") ||
1642              fwnode_property_present(&device->fwnode, "baud")))
1643                 return true;
1644
1645         /* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1646         if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1647                 return false;
1648
1649         INIT_LIST_HEAD(&resource_list);
1650         acpi_dev_get_resources(device, &resource_list,
1651                                acpi_check_serial_bus_slave,
1652                                &is_serial_bus_slave);
1653         acpi_dev_free_resource_list(&resource_list);
1654
1655         return is_serial_bus_slave;
1656 }
1657
1658 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1659                              int type)
1660 {
1661         INIT_LIST_HEAD(&device->pnp.ids);
1662         device->device_type = type;
1663         device->handle = handle;
1664         device->parent = acpi_bus_get_parent(handle);
1665         fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1666         acpi_set_device_status(device, ACPI_STA_DEFAULT);
1667         acpi_device_get_busid(device);
1668         acpi_set_pnp_ids(handle, &device->pnp, type);
1669         acpi_init_properties(device);
1670         acpi_bus_get_flags(device);
1671         device->flags.match_driver = false;
1672         device->flags.initialized = true;
1673         device->flags.enumeration_by_parent =
1674                 acpi_device_enumeration_by_parent(device);
1675         acpi_device_clear_enumerated(device);
1676         device_initialize(&device->dev);
1677         dev_set_uevent_suppress(&device->dev, true);
1678         acpi_init_coherency(device);
1679 }
1680
1681 static void acpi_scan_dep_init(struct acpi_device *adev)
1682 {
1683         struct acpi_dep_data *dep;
1684
1685         list_for_each_entry(dep, &acpi_dep_list, node) {
1686                 if (dep->consumer == adev->handle)
1687                         adev->dep_unmet++;
1688         }
1689 }
1690
1691 void acpi_device_add_finalize(struct acpi_device *device)
1692 {
1693         dev_set_uevent_suppress(&device->dev, false);
1694         kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1695 }
1696
1697 static void acpi_scan_init_status(struct acpi_device *adev)
1698 {
1699         if (acpi_bus_get_status(adev))
1700                 acpi_set_device_status(adev, 0);
1701 }
1702
1703 static int acpi_add_single_object(struct acpi_device **child,
1704                                   acpi_handle handle, int type, bool dep_init)
1705 {
1706         struct acpi_device *device;
1707         bool release_dep_lock = false;
1708         int result;
1709
1710         device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1711         if (!device)
1712                 return -ENOMEM;
1713
1714         acpi_init_device_object(device, handle, type);
1715         /*
1716          * Getting the status is delayed till here so that we can call
1717          * acpi_bus_get_status() and use its quirk handling.  Note that
1718          * this must be done before the get power-/wakeup_dev-flags calls.
1719          */
1720         if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
1721                 if (dep_init) {
1722                         mutex_lock(&acpi_dep_list_lock);
1723                         /*
1724                          * Hold the lock until the acpi_tie_acpi_dev() call
1725                          * below to prevent concurrent acpi_scan_clear_dep()
1726                          * from deleting a dependency list entry without
1727                          * updating dep_unmet for the device.
1728                          */
1729                         release_dep_lock = true;
1730                         acpi_scan_dep_init(device);
1731                 }
1732                 acpi_scan_init_status(device);
1733         }
1734
1735         acpi_bus_get_power_flags(device);
1736         acpi_bus_get_wakeup_device_flags(device);
1737
1738         result = acpi_tie_acpi_dev(device);
1739
1740         if (release_dep_lock)
1741                 mutex_unlock(&acpi_dep_list_lock);
1742
1743         if (!result)
1744                 result = __acpi_device_add(device, acpi_device_release);
1745
1746         if (result) {
1747                 acpi_device_release(&device->dev);
1748                 return result;
1749         }
1750
1751         acpi_power_add_remove_device(device, true);
1752         acpi_device_add_finalize(device);
1753
1754         acpi_handle_debug(handle, "Added as %s, parent %s\n",
1755                           dev_name(&device->dev), device->parent ?
1756                                 dev_name(&device->parent->dev) : "(null)");
1757
1758         *child = device;
1759         return 0;
1760 }
1761
1762 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1763                                             void *context)
1764 {
1765         struct resource *res = context;
1766
1767         if (acpi_dev_resource_memory(ares, res))
1768                 return AE_CTRL_TERMINATE;
1769
1770         return AE_OK;
1771 }
1772
1773 static bool acpi_device_should_be_hidden(acpi_handle handle)
1774 {
1775         acpi_status status;
1776         struct resource res;
1777
1778         /* Check if it should ignore the UART device */
1779         if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1780                 return false;
1781
1782         /*
1783          * The UART device described in SPCR table is assumed to have only one
1784          * memory resource present. So we only look for the first one here.
1785          */
1786         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1787                                      acpi_get_resource_memory, &res);
1788         if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1789                 return false;
1790
1791         acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1792                          &res.start);
1793
1794         return true;
1795 }
1796
1797 bool acpi_device_is_present(const struct acpi_device *adev)
1798 {
1799         return adev->status.present || adev->status.functional;
1800 }
1801
1802 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1803                                        const char *idstr,
1804                                        const struct acpi_device_id **matchid)
1805 {
1806         const struct acpi_device_id *devid;
1807
1808         if (handler->match)
1809                 return handler->match(idstr, matchid);
1810
1811         for (devid = handler->ids; devid->id[0]; devid++)
1812                 if (!strcmp((char *)devid->id, idstr)) {
1813                         if (matchid)
1814                                 *matchid = devid;
1815
1816                         return true;
1817                 }
1818
1819         return false;
1820 }
1821
1822 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1823                                         const struct acpi_device_id **matchid)
1824 {
1825         struct acpi_scan_handler *handler;
1826
1827         list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1828                 if (acpi_scan_handler_matching(handler, idstr, matchid))
1829                         return handler;
1830
1831         return NULL;
1832 }
1833
1834 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1835 {
1836         if (!!hotplug->enabled == !!val)
1837                 return;
1838
1839         mutex_lock(&acpi_scan_lock);
1840
1841         hotplug->enabled = val;
1842
1843         mutex_unlock(&acpi_scan_lock);
1844 }
1845
1846 static void acpi_scan_init_hotplug(struct acpi_device *adev)
1847 {
1848         struct acpi_hardware_id *hwid;
1849
1850         if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1851                 acpi_dock_add(adev);
1852                 return;
1853         }
1854         list_for_each_entry(hwid, &adev->pnp.ids, list) {
1855                 struct acpi_scan_handler *handler;
1856
1857                 handler = acpi_scan_match_handler(hwid->id, NULL);
1858                 if (handler) {
1859                         adev->flags.hotplug_notify = true;
1860                         break;
1861                 }
1862         }
1863 }
1864
1865 static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
1866 {
1867         struct acpi_handle_list dep_devices;
1868         acpi_status status;
1869         u32 count;
1870         int i;
1871
1872         /*
1873          * Check for _HID here to avoid deferring the enumeration of:
1874          * 1. PCI devices.
1875          * 2. ACPI nodes describing USB ports.
1876          * Still, checking for _HID catches more then just these cases ...
1877          */
1878         if (!check_dep || !acpi_has_method(handle, "_DEP") ||
1879             !acpi_has_method(handle, "_HID"))
1880                 return 0;
1881
1882         status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
1883         if (ACPI_FAILURE(status)) {
1884                 acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
1885                 return 0;
1886         }
1887
1888         for (count = 0, i = 0; i < dep_devices.count; i++) {
1889                 struct acpi_device_info *info;
1890                 struct acpi_dep_data *dep;
1891                 bool skip;
1892
1893                 status = acpi_get_object_info(dep_devices.handles[i], &info);
1894                 if (ACPI_FAILURE(status)) {
1895                         acpi_handle_debug(handle, "Error reading _DEP device info\n");
1896                         continue;
1897                 }
1898
1899                 skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
1900                 kfree(info);
1901
1902                 if (skip)
1903                         continue;
1904
1905                 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1906                 if (!dep)
1907                         continue;
1908
1909                 count++;
1910
1911                 dep->supplier = dep_devices.handles[i];
1912                 dep->consumer = handle;
1913
1914                 mutex_lock(&acpi_dep_list_lock);
1915                 list_add_tail(&dep->node , &acpi_dep_list);
1916                 mutex_unlock(&acpi_dep_list_lock);
1917         }
1918
1919         return count;
1920 }
1921
1922 static bool acpi_bus_scan_second_pass;
1923
1924 static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
1925                                       struct acpi_device **adev_p)
1926 {
1927         struct acpi_device *device = NULL;
1928         acpi_object_type acpi_type;
1929         int type;
1930
1931         acpi_bus_get_device(handle, &device);
1932         if (device)
1933                 goto out;
1934
1935         if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
1936                 return AE_OK;
1937
1938         switch (acpi_type) {
1939         case ACPI_TYPE_DEVICE:
1940                 if (acpi_device_should_be_hidden(handle))
1941                         return AE_OK;
1942
1943                 /* Bail out if there are dependencies. */
1944                 if (acpi_scan_check_dep(handle, check_dep) > 0) {
1945                         acpi_bus_scan_second_pass = true;
1946                         return AE_CTRL_DEPTH;
1947                 }
1948
1949                 fallthrough;
1950         case ACPI_TYPE_ANY:     /* for ACPI_ROOT_OBJECT */
1951                 type = ACPI_BUS_TYPE_DEVICE;
1952                 break;
1953
1954         case ACPI_TYPE_PROCESSOR:
1955                 type = ACPI_BUS_TYPE_PROCESSOR;
1956                 break;
1957
1958         case ACPI_TYPE_THERMAL:
1959                 type = ACPI_BUS_TYPE_THERMAL;
1960                 break;
1961
1962         case ACPI_TYPE_POWER:
1963                 acpi_add_power_resource(handle);
1964                 fallthrough;
1965         default:
1966                 return AE_OK;
1967         }
1968
1969         /*
1970          * If check_dep is true at this point, the device has no dependencies,
1971          * or the creation of the device object would have been postponed above.
1972          */
1973         acpi_add_single_object(&device, handle, type, !check_dep);
1974         if (!device)
1975                 return AE_CTRL_DEPTH;
1976
1977         acpi_scan_init_hotplug(device);
1978
1979 out:
1980         if (!*adev_p)
1981                 *adev_p = device;
1982
1983         return AE_OK;
1984 }
1985
1986 static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
1987                                         void *not_used, void **ret_p)
1988 {
1989         return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
1990 }
1991
1992 static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
1993                                         void *not_used, void **ret_p)
1994 {
1995         return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
1996 }
1997
1998 static void acpi_default_enumeration(struct acpi_device *device)
1999 {
2000         /*
2001          * Do not enumerate devices with enumeration_by_parent flag set as
2002          * they will be enumerated by their respective parents.
2003          */
2004         if (!device->flags.enumeration_by_parent) {
2005                 acpi_create_platform_device(device, NULL);
2006                 acpi_device_set_enumerated(device);
2007         } else {
2008                 blocking_notifier_call_chain(&acpi_reconfig_chain,
2009                                              ACPI_RECONFIG_DEVICE_ADD, device);
2010         }
2011 }
2012
2013 static const struct acpi_device_id generic_device_ids[] = {
2014         {ACPI_DT_NAMESPACE_HID, },
2015         {"", },
2016 };
2017
2018 static int acpi_generic_device_attach(struct acpi_device *adev,
2019                                       const struct acpi_device_id *not_used)
2020 {
2021         /*
2022          * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2023          * below can be unconditional.
2024          */
2025         if (adev->data.of_compatible)
2026                 acpi_default_enumeration(adev);
2027
2028         return 1;
2029 }
2030
2031 static struct acpi_scan_handler generic_device_handler = {
2032         .ids = generic_device_ids,
2033         .attach = acpi_generic_device_attach,
2034 };
2035
2036 static int acpi_scan_attach_handler(struct acpi_device *device)
2037 {
2038         struct acpi_hardware_id *hwid;
2039         int ret = 0;
2040
2041         list_for_each_entry(hwid, &device->pnp.ids, list) {
2042                 const struct acpi_device_id *devid;
2043                 struct acpi_scan_handler *handler;
2044
2045                 handler = acpi_scan_match_handler(hwid->id, &devid);
2046                 if (handler) {
2047                         if (!handler->attach) {
2048                                 device->pnp.type.platform_id = 0;
2049                                 continue;
2050                         }
2051                         device->handler = handler;
2052                         ret = handler->attach(device, devid);
2053                         if (ret > 0)
2054                                 break;
2055
2056                         device->handler = NULL;
2057                         if (ret < 0)
2058                                 break;
2059                 }
2060         }
2061
2062         return ret;
2063 }
2064
2065 static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
2066 {
2067         struct acpi_device *child;
2068         bool skip = !first_pass && device->flags.visited;
2069         acpi_handle ejd;
2070         int ret;
2071
2072         if (skip)
2073                 goto ok;
2074
2075         if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2076                 register_dock_dependent_device(device, ejd);
2077
2078         acpi_bus_get_status(device);
2079         /* Skip devices that are not present. */
2080         if (!acpi_device_is_present(device)) {
2081                 device->flags.initialized = false;
2082                 acpi_device_clear_enumerated(device);
2083                 device->flags.power_manageable = 0;
2084                 return;
2085         }
2086         if (device->handler)
2087                 goto ok;
2088
2089         if (!device->flags.initialized) {
2090                 device->flags.power_manageable =
2091                         device->power.states[ACPI_STATE_D0].flags.valid;
2092                 if (acpi_bus_init_power(device))
2093                         device->flags.power_manageable = 0;
2094
2095                 device->flags.initialized = true;
2096         } else if (device->flags.visited) {
2097                 goto ok;
2098         }
2099
2100         ret = acpi_scan_attach_handler(device);
2101         if (ret < 0)
2102                 return;
2103
2104         device->flags.match_driver = true;
2105         if (ret > 0 && !device->flags.enumeration_by_parent) {
2106                 acpi_device_set_enumerated(device);
2107                 goto ok;
2108         }
2109
2110         ret = device_attach(&device->dev);
2111         if (ret < 0)
2112                 return;
2113
2114         if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2115                 acpi_default_enumeration(device);
2116         else
2117                 acpi_device_set_enumerated(device);
2118
2119  ok:
2120         list_for_each_entry(child, &device->children, node)
2121                 acpi_bus_attach(child, first_pass);
2122
2123         if (!skip && device->handler && device->handler->hotplug.notify_online)
2124                 device->handler->hotplug.notify_online(device);
2125 }
2126
2127 static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
2128 {
2129         struct acpi_device *adev;
2130
2131         adev = acpi_bus_get_acpi_device(dep->consumer);
2132         if (adev) {
2133                 *(struct acpi_device **)data = adev;
2134                 return 1;
2135         }
2136         /* Continue parsing if the device object is not present. */
2137         return 0;
2138 }
2139
2140 struct acpi_scan_clear_dep_work {
2141         struct work_struct work;
2142         struct acpi_device *adev;
2143 };
2144
2145 static void acpi_scan_clear_dep_fn(struct work_struct *work)
2146 {
2147         struct acpi_scan_clear_dep_work *cdw;
2148
2149         cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
2150
2151         acpi_scan_lock_acquire();
2152         acpi_bus_attach(cdw->adev, true);
2153         acpi_scan_lock_release();
2154
2155         acpi_dev_put(cdw->adev);
2156         kfree(cdw);
2157 }
2158
2159 static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
2160 {
2161         struct acpi_scan_clear_dep_work *cdw;
2162
2163         if (adev->dep_unmet)
2164                 return false;
2165
2166         cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
2167         if (!cdw)
2168                 return false;
2169
2170         cdw->adev = adev;
2171         INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
2172         /*
2173          * Since the work function may block on the lock until the entire
2174          * initial enumeration of devices is complete, put it into the unbound
2175          * workqueue.
2176          */
2177         queue_work(system_unbound_wq, &cdw->work);
2178
2179         return true;
2180 }
2181
2182 static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
2183 {
2184         struct acpi_device *adev = acpi_bus_get_acpi_device(dep->consumer);
2185
2186         if (adev) {
2187                 adev->dep_unmet--;
2188                 if (!acpi_scan_clear_dep_queue(adev))
2189                         acpi_dev_put(adev);
2190         }
2191
2192         list_del(&dep->node);
2193         kfree(dep);
2194
2195         return 0;
2196 }
2197
2198 /**
2199  * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2200  * @handle:     The ACPI handle of the supplier device
2201  * @callback:   Pointer to the callback function to apply
2202  * @data:       Pointer to some data to pass to the callback
2203  *
2204  * The return value of the callback determines this function's behaviour. If 0
2205  * is returned we continue to iterate over acpi_dep_list. If a positive value
2206  * is returned then the loop is broken but this function returns 0. If a
2207  * negative value is returned by the callback then the loop is broken and that
2208  * value is returned as the final error.
2209  */
2210 static int acpi_walk_dep_device_list(acpi_handle handle,
2211                                 int (*callback)(struct acpi_dep_data *, void *),
2212                                 void *data)
2213 {
2214         struct acpi_dep_data *dep, *tmp;
2215         int ret = 0;
2216
2217         mutex_lock(&acpi_dep_list_lock);
2218         list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2219                 if (dep->supplier == handle) {
2220                         ret = callback(dep, data);
2221                         if (ret)
2222                                 break;
2223                 }
2224         }
2225         mutex_unlock(&acpi_dep_list_lock);
2226
2227         return ret > 0 ? 0 : ret;
2228 }
2229
2230 /**
2231  * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2232  * @supplier: Pointer to the supplier &struct acpi_device
2233  *
2234  * Clear dependencies on the given device.
2235  */
2236 void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2237 {
2238         acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2239 }
2240 EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2241
2242 /**
2243  * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier
2244  * @supplier: Pointer to the dependee device
2245  *
2246  * Returns the first &struct acpi_device which declares itself dependent on
2247  * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2248  *
2249  * The caller is responsible for putting the reference to adev when it is no
2250  * longer needed.
2251  */
2252 struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier)
2253 {
2254         struct acpi_device *adev = NULL;
2255
2256         acpi_walk_dep_device_list(supplier->handle,
2257                                   acpi_dev_get_first_consumer_dev_cb, &adev);
2258
2259         return adev;
2260 }
2261 EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev);
2262
2263 /**
2264  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2265  * @handle: Root of the namespace scope to scan.
2266  *
2267  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2268  * found devices.
2269  *
2270  * If no devices were found, -ENODEV is returned, but it does not mean that
2271  * there has been a real error.  There just have been no suitable ACPI objects
2272  * in the table trunk from which the kernel could create a device and add an
2273  * appropriate driver.
2274  *
2275  * Must be called under acpi_scan_lock.
2276  */
2277 int acpi_bus_scan(acpi_handle handle)
2278 {
2279         struct acpi_device *device = NULL;
2280
2281         acpi_bus_scan_second_pass = false;
2282
2283         /* Pass 1: Avoid enumerating devices with missing dependencies. */
2284
2285         if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
2286                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2287                                     acpi_bus_check_add_1, NULL, NULL,
2288                                     (void **)&device);
2289
2290         if (!device)
2291                 return -ENODEV;
2292
2293         acpi_bus_attach(device, true);
2294
2295         if (!acpi_bus_scan_second_pass)
2296                 return 0;
2297
2298         /* Pass 2: Enumerate all of the remaining devices. */
2299
2300         device = NULL;
2301
2302         if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
2303                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2304                                     acpi_bus_check_add_2, NULL, NULL,
2305                                     (void **)&device);
2306
2307         acpi_bus_attach(device, false);
2308
2309         return 0;
2310 }
2311 EXPORT_SYMBOL(acpi_bus_scan);
2312
2313 /**
2314  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2315  * @adev: Root of the ACPI namespace scope to walk.
2316  *
2317  * Must be called under acpi_scan_lock.
2318  */
2319 void acpi_bus_trim(struct acpi_device *adev)
2320 {
2321         struct acpi_scan_handler *handler = adev->handler;
2322         struct acpi_device *child;
2323
2324         list_for_each_entry_reverse(child, &adev->children, node)
2325                 acpi_bus_trim(child);
2326
2327         adev->flags.match_driver = false;
2328         if (handler) {
2329                 if (handler->detach)
2330                         handler->detach(adev);
2331
2332                 adev->handler = NULL;
2333         } else {
2334                 device_release_driver(&adev->dev);
2335         }
2336         /*
2337          * Most likely, the device is going away, so put it into D3cold before
2338          * that.
2339          */
2340         acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2341         adev->flags.initialized = false;
2342         acpi_device_clear_enumerated(adev);
2343 }
2344 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2345
2346 int acpi_bus_register_early_device(int type)
2347 {
2348         struct acpi_device *device = NULL;
2349         int result;
2350
2351         result = acpi_add_single_object(&device, NULL, type, false);
2352         if (result)
2353                 return result;
2354
2355         device->flags.match_driver = true;
2356         return device_attach(&device->dev);
2357 }
2358 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2359
2360 static int acpi_bus_scan_fixed(void)
2361 {
2362         int result = 0;
2363
2364         /*
2365          * Enumerate all fixed-feature devices.
2366          */
2367         if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2368                 struct acpi_device *device = NULL;
2369
2370                 result = acpi_add_single_object(&device, NULL,
2371                                                 ACPI_BUS_TYPE_POWER_BUTTON, false);
2372                 if (result)
2373                         return result;
2374
2375                 device->flags.match_driver = true;
2376                 result = device_attach(&device->dev);
2377                 if (result < 0)
2378                         return result;
2379
2380                 device_init_wakeup(&device->dev, true);
2381         }
2382
2383         if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2384                 struct acpi_device *device = NULL;
2385
2386                 result = acpi_add_single_object(&device, NULL,
2387                                                 ACPI_BUS_TYPE_SLEEP_BUTTON, false);
2388                 if (result)
2389                         return result;
2390
2391                 device->flags.match_driver = true;
2392                 result = device_attach(&device->dev);
2393         }
2394
2395         return result < 0 ? result : 0;
2396 }
2397
2398 static void __init acpi_get_spcr_uart_addr(void)
2399 {
2400         acpi_status status;
2401         struct acpi_table_spcr *spcr_ptr;
2402
2403         status = acpi_get_table(ACPI_SIG_SPCR, 0,
2404                                 (struct acpi_table_header **)&spcr_ptr);
2405         if (ACPI_FAILURE(status)) {
2406                 pr_warn(PREFIX "STAO table present, but SPCR is missing\n");
2407                 return;
2408         }
2409
2410         spcr_uart_addr = spcr_ptr->serial_port.address;
2411         acpi_put_table((struct acpi_table_header *)spcr_ptr);
2412 }
2413
2414 static bool acpi_scan_initialized;
2415
2416 int __init acpi_scan_init(void)
2417 {
2418         int result;
2419         acpi_status status;
2420         struct acpi_table_stao *stao_ptr;
2421
2422         acpi_pci_root_init();
2423         acpi_pci_link_init();
2424         acpi_processor_init();
2425         acpi_platform_init();
2426         acpi_lpss_init();
2427         acpi_apd_init();
2428         acpi_cmos_rtc_init();
2429         acpi_container_init();
2430         acpi_memory_hotplug_init();
2431         acpi_watchdog_init();
2432         acpi_pnp_init();
2433         acpi_int340x_thermal_init();
2434         acpi_amba_init();
2435         acpi_init_lpit();
2436
2437         acpi_scan_add_handler(&generic_device_handler);
2438
2439         /*
2440          * If there is STAO table, check whether it needs to ignore the UART
2441          * device in SPCR table.
2442          */
2443         status = acpi_get_table(ACPI_SIG_STAO, 0,
2444                                 (struct acpi_table_header **)&stao_ptr);
2445         if (ACPI_SUCCESS(status)) {
2446                 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2447                         pr_info(PREFIX "STAO Name List not yet supported.\n");
2448
2449                 if (stao_ptr->ignore_uart)
2450                         acpi_get_spcr_uart_addr();
2451
2452                 acpi_put_table((struct acpi_table_header *)stao_ptr);
2453         }
2454
2455         acpi_gpe_apply_masked_gpes();
2456         acpi_update_all_gpes();
2457
2458         /*
2459          * Although we call __add_memory() that is documented to require the
2460          * device_hotplug_lock, it is not necessary here because this is an
2461          * early code when userspace or any other code path cannot trigger
2462          * hotplug/hotunplug operations.
2463          */
2464         mutex_lock(&acpi_scan_lock);
2465         /*
2466          * Enumerate devices in the ACPI namespace.
2467          */
2468         result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2469         if (result)
2470                 goto out;
2471
2472         result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2473         if (result)
2474                 goto out;
2475
2476         /* Fixed feature devices do not exist on HW-reduced platform */
2477         if (!acpi_gbl_reduced_hardware) {
2478                 result = acpi_bus_scan_fixed();
2479                 if (result) {
2480                         acpi_detach_data(acpi_root->handle,
2481                                          acpi_scan_drop_device);
2482                         acpi_device_del(acpi_root);
2483                         acpi_bus_put_acpi_device(acpi_root);
2484                         goto out;
2485                 }
2486         }
2487
2488         acpi_turn_off_unused_power_resources();
2489
2490         acpi_scan_initialized = true;
2491
2492  out:
2493         mutex_unlock(&acpi_scan_lock);
2494         return result;
2495 }
2496
2497 static struct acpi_probe_entry *ape;
2498 static int acpi_probe_count;
2499 static DEFINE_MUTEX(acpi_probe_mutex);
2500
2501 static int __init acpi_match_madt(union acpi_subtable_headers *header,
2502                                   const unsigned long end)
2503 {
2504         if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2505                 if (!ape->probe_subtbl(header, end))
2506                         acpi_probe_count++;
2507
2508         return 0;
2509 }
2510
2511 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2512 {
2513         int count = 0;
2514
2515         if (acpi_disabled)
2516                 return 0;
2517
2518         mutex_lock(&acpi_probe_mutex);
2519         for (ape = ap_head; nr; ape++, nr--) {
2520                 if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2521                         acpi_probe_count = 0;
2522                         acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2523                         count += acpi_probe_count;
2524                 } else {
2525                         int res;
2526                         res = acpi_table_parse(ape->id, ape->probe_table);
2527                         if (!res)
2528                                 count++;
2529                 }
2530         }
2531         mutex_unlock(&acpi_probe_mutex);
2532
2533         return count;
2534 }
2535
2536 static void acpi_table_events_fn(struct work_struct *work)
2537 {
2538         acpi_scan_lock_acquire();
2539         acpi_bus_scan(ACPI_ROOT_OBJECT);
2540         acpi_scan_lock_release();
2541
2542         kfree(work);
2543 }
2544
2545 void acpi_scan_table_notify(void)
2546 {
2547         struct work_struct *work;
2548
2549         if (!acpi_scan_initialized)
2550                 return;
2551
2552         work = kmalloc(sizeof(*work), GFP_KERNEL);
2553         if (!work)
2554                 return;
2555
2556         INIT_WORK(work, acpi_table_events_fn);
2557         schedule_work(work);
2558 }
2559
2560 int acpi_reconfig_notifier_register(struct notifier_block *nb)
2561 {
2562         return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2563 }
2564 EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2565
2566 int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2567 {
2568         return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2569 }
2570 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);