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