Merge branches 'arm/exynos', 'arm/omap', 'arm/rockchip', 'arm/mediatek', 'arm/smmu...
[platform/kernel/linux-rpi.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 #include <linux/dma-mapping.h>
17
18 #include "internal.h"
19
20 #define ACPI_GLUE_DEBUG 0
21 #if ACPI_GLUE_DEBUG
22 #define DBG(fmt, ...)                                           \
23         printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
24 #else
25 #define DBG(fmt, ...)                                           \
26 do {                                                            \
27         if (0)                                                  \
28                 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__);   \
29 } while (0)
30 #endif
31 static LIST_HEAD(bus_type_list);
32 static DECLARE_RWSEM(bus_type_sem);
33
34 #define PHYSICAL_NODE_STRING "physical_node"
35 #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
36
37 int register_acpi_bus_type(struct acpi_bus_type *type)
38 {
39         if (acpi_disabled)
40                 return -ENODEV;
41         if (type && type->match && type->find_companion) {
42                 down_write(&bus_type_sem);
43                 list_add_tail(&type->list, &bus_type_list);
44                 up_write(&bus_type_sem);
45                 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
46                 return 0;
47         }
48         return -ENODEV;
49 }
50 EXPORT_SYMBOL_GPL(register_acpi_bus_type);
51
52 int unregister_acpi_bus_type(struct acpi_bus_type *type)
53 {
54         if (acpi_disabled)
55                 return 0;
56         if (type) {
57                 down_write(&bus_type_sem);
58                 list_del_init(&type->list);
59                 up_write(&bus_type_sem);
60                 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
61                        type->name);
62                 return 0;
63         }
64         return -ENODEV;
65 }
66 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
67
68 static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
69 {
70         struct acpi_bus_type *tmp, *ret = NULL;
71
72         down_read(&bus_type_sem);
73         list_for_each_entry(tmp, &bus_type_list, list) {
74                 if (tmp->match(dev)) {
75                         ret = tmp;
76                         break;
77                 }
78         }
79         up_read(&bus_type_sem);
80         return ret;
81 }
82
83 #define FIND_CHILD_MIN_SCORE    1
84 #define FIND_CHILD_MAX_SCORE    2
85
86 static int find_child_checks(struct acpi_device *adev, bool check_children)
87 {
88         bool sta_present = true;
89         unsigned long long sta;
90         acpi_status status;
91
92         status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
93         if (status == AE_NOT_FOUND)
94                 sta_present = false;
95         else if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
96                 return -ENODEV;
97
98         if (check_children && list_empty(&adev->children))
99                 return -ENODEV;
100
101         /*
102          * If the device has a _HID returning a valid ACPI/PNP device ID, it is
103          * better to make it look less attractive here, so that the other device
104          * with the same _ADR value (that may not have a valid device ID) can be
105          * matched going forward.  [This means a second spec violation in a row,
106          * so whatever we do here is best effort anyway.]
107          */
108         return sta_present && !adev->pnp.type.platform_id ?
109                         FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE;
110 }
111
112 struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
113                                            u64 address, bool check_children)
114 {
115         struct acpi_device *adev, *ret = NULL;
116         int ret_score = 0;
117
118         if (!parent)
119                 return NULL;
120
121         list_for_each_entry(adev, &parent->children, node) {
122                 unsigned long long addr;
123                 acpi_status status;
124                 int score;
125
126                 status = acpi_evaluate_integer(adev->handle, METHOD_NAME__ADR,
127                                                NULL, &addr);
128                 if (ACPI_FAILURE(status) || addr != address)
129                         continue;
130
131                 if (!ret) {
132                         /* This is the first matching object.  Save it. */
133                         ret = adev;
134                         continue;
135                 }
136                 /*
137                  * There is more than one matching device object with the same
138                  * _ADR value.  That really is unexpected, so we are kind of
139                  * beyond the scope of the spec here.  We have to choose which
140                  * one to return, though.
141                  *
142                  * First, check if the previously found object is good enough
143                  * and return it if so.  Second, do the same for the object that
144                  * we've just found.
145                  */
146                 if (!ret_score) {
147                         ret_score = find_child_checks(ret, check_children);
148                         if (ret_score == FIND_CHILD_MAX_SCORE)
149                                 return ret;
150                 }
151                 score = find_child_checks(adev, check_children);
152                 if (score == FIND_CHILD_MAX_SCORE) {
153                         return adev;
154                 } else if (score > ret_score) {
155                         ret = adev;
156                         ret_score = score;
157                 }
158         }
159         return ret;
160 }
161 EXPORT_SYMBOL_GPL(acpi_find_child_device);
162
163 static void acpi_physnode_link_name(char *buf, unsigned int node_id)
164 {
165         if (node_id > 0)
166                 snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
167                          PHYSICAL_NODE_STRING "%u", node_id);
168         else
169                 strcpy(buf, PHYSICAL_NODE_STRING);
170 }
171
172 int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
173 {
174         struct acpi_device_physical_node *physical_node, *pn;
175         char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
176         struct list_head *physnode_list;
177         unsigned int node_id;
178         int retval = -EINVAL;
179
180         if (has_acpi_companion(dev)) {
181                 if (acpi_dev) {
182                         dev_warn(dev, "ACPI companion already set\n");
183                         return -EINVAL;
184                 } else {
185                         acpi_dev = ACPI_COMPANION(dev);
186                 }
187         }
188         if (!acpi_dev)
189                 return -EINVAL;
190
191         get_device(&acpi_dev->dev);
192         get_device(dev);
193         physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
194         if (!physical_node) {
195                 retval = -ENOMEM;
196                 goto err;
197         }
198
199         mutex_lock(&acpi_dev->physical_node_lock);
200
201         /*
202          * Keep the list sorted by node_id so that the IDs of removed nodes can
203          * be recycled easily.
204          */
205         physnode_list = &acpi_dev->physical_node_list;
206         node_id = 0;
207         list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
208                 /* Sanity check. */
209                 if (pn->dev == dev) {
210                         mutex_unlock(&acpi_dev->physical_node_lock);
211
212                         dev_warn(dev, "Already associated with ACPI node\n");
213                         kfree(physical_node);
214                         if (ACPI_COMPANION(dev) != acpi_dev)
215                                 goto err;
216
217                         put_device(dev);
218                         put_device(&acpi_dev->dev);
219                         return 0;
220                 }
221                 if (pn->node_id == node_id) {
222                         physnode_list = &pn->node;
223                         node_id++;
224                 }
225         }
226
227         physical_node->node_id = node_id;
228         physical_node->dev = dev;
229         list_add(&physical_node->node, physnode_list);
230         acpi_dev->physical_node_count++;
231
232         if (!has_acpi_companion(dev))
233                 ACPI_COMPANION_SET(dev, acpi_dev);
234
235         acpi_physnode_link_name(physical_node_name, node_id);
236         retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
237                                    physical_node_name);
238         if (retval)
239                 dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
240                         physical_node_name, retval);
241
242         retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
243                                    "firmware_node");
244         if (retval)
245                 dev_err(dev, "Failed to create link firmware_node (%d)\n",
246                         retval);
247
248         mutex_unlock(&acpi_dev->physical_node_lock);
249
250         if (acpi_dev->wakeup.flags.valid)
251                 device_set_wakeup_capable(dev, true);
252
253         return 0;
254
255  err:
256         ACPI_COMPANION_SET(dev, NULL);
257         put_device(dev);
258         put_device(&acpi_dev->dev);
259         return retval;
260 }
261 EXPORT_SYMBOL_GPL(acpi_bind_one);
262
263 int acpi_unbind_one(struct device *dev)
264 {
265         struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
266         struct acpi_device_physical_node *entry;
267
268         if (!acpi_dev)
269                 return 0;
270
271         mutex_lock(&acpi_dev->physical_node_lock);
272
273         list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
274                 if (entry->dev == dev) {
275                         char physnode_name[PHYSICAL_NODE_NAME_SIZE];
276
277                         list_del(&entry->node);
278                         acpi_dev->physical_node_count--;
279
280                         acpi_physnode_link_name(physnode_name, entry->node_id);
281                         sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
282                         sysfs_remove_link(&dev->kobj, "firmware_node");
283                         ACPI_COMPANION_SET(dev, NULL);
284                         /* Drop references taken by acpi_bind_one(). */
285                         put_device(dev);
286                         put_device(&acpi_dev->dev);
287                         kfree(entry);
288                         break;
289                 }
290
291         mutex_unlock(&acpi_dev->physical_node_lock);
292         return 0;
293 }
294 EXPORT_SYMBOL_GPL(acpi_unbind_one);
295
296 static int acpi_platform_notify(struct device *dev)
297 {
298         struct acpi_bus_type *type = acpi_get_bus_type(dev);
299         struct acpi_device *adev;
300         int ret;
301
302         ret = acpi_bind_one(dev, NULL);
303         if (ret && type) {
304                 struct acpi_device *adev;
305
306                 adev = type->find_companion(dev);
307                 if (!adev) {
308                         DBG("Unable to get handle for %s\n", dev_name(dev));
309                         ret = -ENODEV;
310                         goto out;
311                 }
312                 ret = acpi_bind_one(dev, adev);
313                 if (ret)
314                         goto out;
315         }
316         adev = ACPI_COMPANION(dev);
317         if (!adev)
318                 goto out;
319
320         if (type && type->setup)
321                 type->setup(dev);
322         else if (adev->handler && adev->handler->bind)
323                 adev->handler->bind(dev);
324
325  out:
326 #if ACPI_GLUE_DEBUG
327         if (!ret) {
328                 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
329
330                 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
331                 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
332                 kfree(buffer.pointer);
333         } else
334                 DBG("Device %s -> No ACPI support\n", dev_name(dev));
335 #endif
336
337         return ret;
338 }
339
340 static int acpi_platform_notify_remove(struct device *dev)
341 {
342         struct acpi_device *adev = ACPI_COMPANION(dev);
343         struct acpi_bus_type *type;
344
345         if (!adev)
346                 return 0;
347
348         type = acpi_get_bus_type(dev);
349         if (type && type->cleanup)
350                 type->cleanup(dev);
351         else if (adev->handler && adev->handler->unbind)
352                 adev->handler->unbind(dev);
353
354         acpi_unbind_one(dev);
355         return 0;
356 }
357
358 void __init init_acpi_device_notify(void)
359 {
360         if (platform_notify || platform_notify_remove) {
361                 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
362                 return;
363         }
364         platform_notify = acpi_platform_notify;
365         platform_notify_remove = acpi_platform_notify_remove;
366 }