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