ACPI: Drop physical_node_id_bitmap from struct acpi_device
[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 #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
35
36 int register_acpi_bus_type(struct acpi_bus_type *type)
37 {
38         if (acpi_disabled)
39                 return -ENODEV;
40         if (type && type->match && type->find_device) {
41                 down_write(&bus_type_sem);
42                 list_add_tail(&type->list, &bus_type_list);
43                 up_write(&bus_type_sem);
44                 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
45                 return 0;
46         }
47         return -ENODEV;
48 }
49 EXPORT_SYMBOL_GPL(register_acpi_bus_type);
50
51 int unregister_acpi_bus_type(struct acpi_bus_type *type)
52 {
53         if (acpi_disabled)
54                 return 0;
55         if (type) {
56                 down_write(&bus_type_sem);
57                 list_del_init(&type->list);
58                 up_write(&bus_type_sem);
59                 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
60                        type->name);
61                 return 0;
62         }
63         return -ENODEV;
64 }
65 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
66
67 static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
68 {
69         struct acpi_bus_type *tmp, *ret = NULL;
70
71         down_read(&bus_type_sem);
72         list_for_each_entry(tmp, &bus_type_list, list) {
73                 if (tmp->match(dev)) {
74                         ret = tmp;
75                         break;
76                 }
77         }
78         up_read(&bus_type_sem);
79         return ret;
80 }
81
82 static acpi_status do_acpi_find_child(acpi_handle handle, u32 lvl_not_used,
83                                       void *addr_p, void **ret_p)
84 {
85         unsigned long long addr, sta;
86         acpi_status status;
87
88         status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
89         if (ACPI_SUCCESS(status) && addr == *((u64 *)addr_p)) {
90                 *ret_p = handle;
91                 status = acpi_bus_get_status_handle(handle, &sta);
92                 if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_ENABLED))
93                         return AE_CTRL_TERMINATE;
94         }
95         return AE_OK;
96 }
97
98 acpi_handle acpi_get_child(acpi_handle parent, u64 address)
99 {
100         void *ret = NULL;
101
102         if (!parent)
103                 return NULL;
104
105         acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, NULL,
106                             do_acpi_find_child, &address, &ret);
107         return (acpi_handle)ret;
108 }
109 EXPORT_SYMBOL(acpi_get_child);
110
111 int acpi_bind_one(struct device *dev, acpi_handle handle)
112 {
113         struct acpi_device *acpi_dev;
114         acpi_status status;
115         struct acpi_device_physical_node *physical_node, *pn;
116         char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
117         struct list_head *physnode_list;
118         unsigned int node_id;
119         int retval = -EINVAL;
120
121         if (ACPI_HANDLE(dev)) {
122                 if (handle) {
123                         dev_warn(dev, "ACPI handle is already set\n");
124                         return -EINVAL;
125                 } else {
126                         handle = ACPI_HANDLE(dev);
127                 }
128         }
129         if (!handle)
130                 return -EINVAL;
131
132         get_device(dev);
133         status = acpi_bus_get_device(handle, &acpi_dev);
134         if (ACPI_FAILURE(status))
135                 goto err;
136
137         physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
138         if (!physical_node) {
139                 retval = -ENOMEM;
140                 goto err;
141         }
142
143         mutex_lock(&acpi_dev->physical_node_lock);
144
145         /*
146          * Keep the list sorted by node_id so that the IDs of removed nodes can
147          * be recycled easily.
148          */
149         physnode_list = &acpi_dev->physical_node_list;
150         node_id = 0;
151         list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
152                 /* Sanity check. */
153                 if (pn->dev == dev) {
154                         dev_warn(dev, "Already associated with ACPI node\n");
155                         goto err_free;
156                 }
157                 if (pn->node_id == node_id) {
158                         physnode_list = &pn->node;
159                         node_id++;
160                 }
161         }
162
163         physical_node->node_id = node_id;
164         physical_node->dev = dev;
165         list_add(&physical_node->node, physnode_list);
166         acpi_dev->physical_node_count++;
167
168         mutex_unlock(&acpi_dev->physical_node_lock);
169
170         if (!ACPI_HANDLE(dev))
171                 ACPI_HANDLE_SET(dev, acpi_dev->handle);
172
173         if (!physical_node->node_id)
174                 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
175         else
176                 sprintf(physical_node_name,
177                         "physical_node%d", physical_node->node_id);
178         retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
179                         physical_node_name);
180         retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
181                 "firmware_node");
182
183         if (acpi_dev->wakeup.flags.valid)
184                 device_set_wakeup_capable(dev, true);
185
186         return 0;
187
188  err:
189         ACPI_HANDLE_SET(dev, NULL);
190         put_device(dev);
191         return retval;
192
193  err_free:
194         mutex_unlock(&acpi_dev->physical_node_lock);
195         kfree(physical_node);
196         goto err;
197 }
198 EXPORT_SYMBOL_GPL(acpi_bind_one);
199
200 int acpi_unbind_one(struct device *dev)
201 {
202         struct acpi_device_physical_node *entry;
203         struct acpi_device *acpi_dev;
204         acpi_status status;
205         struct list_head *node, *next;
206
207         if (!ACPI_HANDLE(dev))
208                 return 0;
209
210         status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
211         if (ACPI_FAILURE(status))
212                 goto err;
213
214         mutex_lock(&acpi_dev->physical_node_lock);
215         list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
216                 char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
217
218                 entry = list_entry(node, struct acpi_device_physical_node,
219                         node);
220                 if (entry->dev != dev)
221                         continue;
222
223                 list_del(node);
224
225                 acpi_dev->physical_node_count--;
226
227                 if (!entry->node_id)
228                         strcpy(physical_node_name, PHYSICAL_NODE_STRING);
229                 else
230                         sprintf(physical_node_name,
231                                 "physical_node%d", entry->node_id);
232
233                 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
234                 sysfs_remove_link(&dev->kobj, "firmware_node");
235                 ACPI_HANDLE_SET(dev, NULL);
236                 /* acpi_bind_one increase refcnt by one */
237                 put_device(dev);
238                 kfree(entry);
239         }
240         mutex_unlock(&acpi_dev->physical_node_lock);
241
242         return 0;
243
244 err:
245         dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
246         return -EINVAL;
247 }
248 EXPORT_SYMBOL_GPL(acpi_unbind_one);
249
250 static int acpi_platform_notify(struct device *dev)
251 {
252         struct acpi_bus_type *type = acpi_get_bus_type(dev);
253         acpi_handle handle;
254         int ret;
255
256         ret = acpi_bind_one(dev, NULL);
257         if (ret && type) {
258                 ret = type->find_device(dev, &handle);
259                 if (ret) {
260                         DBG("Unable to get handle for %s\n", dev_name(dev));
261                         goto out;
262                 }
263                 ret = acpi_bind_one(dev, handle);
264                 if (ret)
265                         goto out;
266         }
267
268         if (type && type->setup)
269                 type->setup(dev);
270
271  out:
272 #if ACPI_GLUE_DEBUG
273         if (!ret) {
274                 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
275
276                 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
277                 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
278                 kfree(buffer.pointer);
279         } else
280                 DBG("Device %s -> No ACPI support\n", dev_name(dev));
281 #endif
282
283         return ret;
284 }
285
286 static int acpi_platform_notify_remove(struct device *dev)
287 {
288         struct acpi_bus_type *type;
289
290         type = acpi_get_bus_type(dev);
291         if (type && type->cleanup)
292                 type->cleanup(dev);
293
294         acpi_unbind_one(dev);
295         return 0;
296 }
297
298 int __init init_acpi_device_notify(void)
299 {
300         if (platform_notify || platform_notify_remove) {
301                 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
302                 return 0;
303         }
304         platform_notify = acpi_platform_notify;
305         platform_notify_remove = acpi_platform_notify_remove;
306         return 0;
307 }