ACPICA: Tables: Fix an issue that FACS initialization is performed twice
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / glue.c
1 /*
2  * Link physical devices with ACPI devices support
3  *
4  * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
5  * Copyright (c) 2005 Intel Corp.
6  *
7  * This file is released under the GPLv2.
8  */
9 #include <linux/export.h>
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/rwsem.h>
15 #include <linux/acpi.h>
16
17 #include "internal.h"
18
19 #define ACPI_GLUE_DEBUG 0
20 #if ACPI_GLUE_DEBUG
21 #define DBG(fmt, ...)                                           \
22         printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
23 #else
24 #define DBG(fmt, ...)                                           \
25 do {                                                            \
26         if (0)                                                  \
27                 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__);   \
28 } while (0)
29 #endif
30 static LIST_HEAD(bus_type_list);
31 static DECLARE_RWSEM(bus_type_sem);
32
33 #define PHYSICAL_NODE_STRING "physical_node"
34
35 int register_acpi_bus_type(struct acpi_bus_type *type)
36 {
37         if (acpi_disabled)
38                 return -ENODEV;
39         if (type && type->match && type->find_device) {
40                 down_write(&bus_type_sem);
41                 list_add_tail(&type->list, &bus_type_list);
42                 up_write(&bus_type_sem);
43                 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
44                 return 0;
45         }
46         return -ENODEV;
47 }
48 EXPORT_SYMBOL_GPL(register_acpi_bus_type);
49
50 int unregister_acpi_bus_type(struct acpi_bus_type *type)
51 {
52         if (acpi_disabled)
53                 return 0;
54         if (type) {
55                 down_write(&bus_type_sem);
56                 list_del_init(&type->list);
57                 up_write(&bus_type_sem);
58                 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
59                        type->name);
60                 return 0;
61         }
62         return -ENODEV;
63 }
64 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
65
66 static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
67 {
68         struct acpi_bus_type *tmp, *ret = NULL;
69
70         down_read(&bus_type_sem);
71         list_for_each_entry(tmp, &bus_type_list, list) {
72                 if (tmp->match(dev)) {
73                         ret = tmp;
74                         break;
75                 }
76         }
77         up_read(&bus_type_sem);
78         return ret;
79 }
80
81 static acpi_status acpi_dev_present(acpi_handle handle, u32 lvl_not_used,
82                                   void *not_used, void **ret_p)
83 {
84         struct acpi_device *adev = NULL;
85
86         acpi_bus_get_device(handle, &adev);
87         if (adev) {
88                 *ret_p = handle;
89                 return AE_CTRL_TERMINATE;
90         }
91         return AE_OK;
92 }
93
94 static bool acpi_extra_checks_passed(acpi_handle handle, bool is_bridge)
95 {
96         unsigned long long sta;
97         acpi_status status;
98
99         status = acpi_bus_get_status_handle(handle, &sta);
100         if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
101                 return false;
102
103         if (is_bridge) {
104                 void *test = NULL;
105
106                 /* Check if this object has at least one child device. */
107                 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
108                                     acpi_dev_present, NULL, NULL, &test);
109                 return !!test;
110         }
111         return true;
112 }
113
114 struct find_child_context {
115         u64 addr;
116         bool is_bridge;
117         acpi_handle ret;
118         bool ret_checked;
119 };
120
121 static acpi_status do_find_child(acpi_handle handle, u32 lvl_not_used,
122                                  void *data, void **not_used)
123 {
124         struct find_child_context *context = data;
125         unsigned long long addr;
126         acpi_status status;
127
128         status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
129         if (ACPI_FAILURE(status) || addr != context->addr)
130                 return AE_OK;
131
132         if (!context->ret) {
133                 /* This is the first matching object.  Save its handle. */
134                 context->ret = handle;
135                 return AE_OK;
136         }
137         /*
138          * There is more than one matching object with the same _ADR value.
139          * That really is unexpected, so we are kind of beyond the scope of the
140          * spec here.  We have to choose which one to return, though.
141          *
142          * First, check if the previously found object is good enough and return
143          * its handle if so.  Second, check the same for the object that we've
144          * just found.
145          */
146         if (!context->ret_checked) {
147                 if (acpi_extra_checks_passed(context->ret, context->is_bridge))
148                         return AE_CTRL_TERMINATE;
149                 else
150                         context->ret_checked = true;
151         }
152         if (acpi_extra_checks_passed(handle, context->is_bridge)) {
153                 context->ret = handle;
154                 return AE_CTRL_TERMINATE;
155         }
156         return AE_OK;
157 }
158
159 acpi_handle acpi_find_child(acpi_handle parent, u64 addr, bool is_bridge)
160 {
161         if (parent) {
162                 struct find_child_context context = {
163                         .addr = addr,
164                         .is_bridge = is_bridge,
165                 };
166
167                 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, do_find_child,
168                                     NULL, &context, NULL);
169                 return context.ret;
170         }
171         return NULL;
172 }
173 EXPORT_SYMBOL_GPL(acpi_find_child);
174
175 static int acpi_bind_one(struct device *dev, acpi_handle handle)
176 {
177         struct acpi_device *acpi_dev;
178         acpi_status status;
179         struct acpi_device_physical_node *physical_node, *pn;
180         char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
181         int retval = -EINVAL;
182
183         if (ACPI_HANDLE(dev)) {
184                 if (handle) {
185                         dev_warn(dev, "ACPI handle is already set\n");
186                         return -EINVAL;
187                 } else {
188                         handle = ACPI_HANDLE(dev);
189                 }
190         }
191         if (!handle)
192                 return -EINVAL;
193
194         get_device(dev);
195         status = acpi_bus_get_device(handle, &acpi_dev);
196         if (ACPI_FAILURE(status))
197                 goto err;
198
199         physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
200         if (!physical_node) {
201                 retval = -ENOMEM;
202                 goto err;
203         }
204
205         mutex_lock(&acpi_dev->physical_node_lock);
206
207         /* Sanity check. */
208         list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
209                 if (pn->dev == dev) {
210                         dev_warn(dev, "Already associated with ACPI node\n");
211                         goto err_free;
212                 }
213
214         /* allocate physical node id according to physical_node_id_bitmap */
215         physical_node->node_id =
216                 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
217                 ACPI_MAX_PHYSICAL_NODE);
218         if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
219                 retval = -ENOSPC;
220                 goto err_free;
221         }
222
223         set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
224         physical_node->dev = dev;
225         list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
226         acpi_dev->physical_node_count++;
227
228         mutex_unlock(&acpi_dev->physical_node_lock);
229
230         if (!ACPI_HANDLE(dev))
231                 ACPI_HANDLE_SET(dev, acpi_dev->handle);
232
233         if (!physical_node->node_id)
234                 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
235         else
236                 sprintf(physical_node_name,
237                         "physical_node%d", physical_node->node_id);
238         retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
239                         physical_node_name);
240         retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
241                 "firmware_node");
242
243         if (acpi_dev->wakeup.flags.valid)
244                 device_set_wakeup_capable(dev, true);
245
246         return 0;
247
248  err:
249         ACPI_HANDLE_SET(dev, NULL);
250         put_device(dev);
251         return retval;
252
253  err_free:
254         mutex_unlock(&acpi_dev->physical_node_lock);
255         kfree(physical_node);
256         goto err;
257 }
258
259 static int acpi_unbind_one(struct device *dev)
260 {
261         struct acpi_device_physical_node *entry;
262         struct acpi_device *acpi_dev;
263         acpi_status status;
264         struct list_head *node, *next;
265
266         if (!ACPI_HANDLE(dev))
267                 return 0;
268
269         status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
270         if (ACPI_FAILURE(status))
271                 goto err;
272
273         mutex_lock(&acpi_dev->physical_node_lock);
274         list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
275                 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
276
277                 entry = list_entry(node, struct acpi_device_physical_node,
278                         node);
279                 if (entry->dev != dev)
280                         continue;
281
282                 list_del(node);
283                 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
284
285                 acpi_dev->physical_node_count--;
286
287                 if (!entry->node_id)
288                         strcpy(physical_node_name, PHYSICAL_NODE_STRING);
289                 else
290                         sprintf(physical_node_name,
291                                 "physical_node%d", entry->node_id);
292
293                 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
294                 sysfs_remove_link(&dev->kobj, "firmware_node");
295                 ACPI_HANDLE_SET(dev, NULL);
296                 /* acpi_bind_one increase refcnt by one */
297                 put_device(dev);
298                 kfree(entry);
299         }
300         mutex_unlock(&acpi_dev->physical_node_lock);
301
302         return 0;
303
304 err:
305         dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
306         return -EINVAL;
307 }
308
309 static int acpi_platform_notify(struct device *dev)
310 {
311         struct acpi_bus_type *type = acpi_get_bus_type(dev);
312         acpi_handle handle;
313         int ret;
314
315         ret = acpi_bind_one(dev, NULL);
316         if (ret && type) {
317                 ret = type->find_device(dev, &handle);
318                 if (ret) {
319                         DBG("Unable to get handle for %s\n", dev_name(dev));
320                         goto out;
321                 }
322                 ret = acpi_bind_one(dev, handle);
323                 if (ret)
324                         goto out;
325         }
326
327         if (type && type->setup)
328                 type->setup(dev);
329
330  out:
331 #if ACPI_GLUE_DEBUG
332         if (!ret) {
333                 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
334
335                 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
336                 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
337                 kfree(buffer.pointer);
338         } else
339                 DBG("Device %s -> No ACPI support\n", dev_name(dev));
340 #endif
341
342         return ret;
343 }
344
345 static int acpi_platform_notify_remove(struct device *dev)
346 {
347         struct acpi_bus_type *type;
348
349         type = acpi_get_bus_type(dev);
350         if (type && type->cleanup)
351                 type->cleanup(dev);
352
353         acpi_unbind_one(dev);
354         return 0;
355 }
356
357 int __init init_acpi_device_notify(void)
358 {
359         if (platform_notify || platform_notify_remove) {
360                 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
361                 return 0;
362         }
363         platform_notify = acpi_platform_notify;
364         platform_notify_remove = acpi_platform_notify_remove;
365         return 0;
366 }