]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/acpi/glue.c
ACPI / processor: Use common hotplug infrastructure
[mirror_ubuntu-bionic-kernel.git] / drivers / acpi / glue.c
CommitLineData
4e10d12a
DSL
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 */
214f2c90 9#include <linux/export.h>
4e10d12a
DSL
10#include <linux/init.h>
11#include <linux/list.h>
12#include <linux/device.h>
5a0e3ad6 13#include <linux/slab.h>
4e10d12a
DSL
14#include <linux/rwsem.h>
15#include <linux/acpi.h>
16
a192a958
LB
17#include "internal.h"
18
4e10d12a
DSL
19#define ACPI_GLUE_DEBUG 0
20#if ACPI_GLUE_DEBUG
23415eb5
JP
21#define DBG(fmt, ...) \
22 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
4e10d12a 23#else
23415eb5
JP
24#define DBG(fmt, ...) \
25do { \
26 if (0) \
27 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
28} while (0)
4e10d12a
DSL
29#endif
30static LIST_HEAD(bus_type_list);
31static DECLARE_RWSEM(bus_type_sem);
32
1033f904
LT
33#define PHYSICAL_NODE_STRING "physical_node"
34
4e10d12a
DSL
35int register_acpi_bus_type(struct acpi_bus_type *type)
36{
37 if (acpi_disabled)
38 return -ENODEV;
53540098 39 if (type && type->match && type->find_device) {
4e10d12a
DSL
40 down_write(&bus_type_sem);
41 list_add_tail(&type->list, &bus_type_list);
42 up_write(&bus_type_sem);
53540098 43 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
4e10d12a
DSL
44 return 0;
45 }
46 return -ENODEV;
47}
91e4d5a1 48EXPORT_SYMBOL_GPL(register_acpi_bus_type);
4e10d12a 49
4e10d12a
DSL
50int 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);
53540098
RW
58 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
59 type->name);
4e10d12a
DSL
60 return 0;
61 }
62 return -ENODEV;
63}
91e4d5a1 64EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
4e10d12a 65
53540098 66static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
4e10d12a
DSL
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) {
53540098 72 if (tmp->match(dev)) {
4e10d12a
DSL
73 ret = tmp;
74 break;
75 }
76 }
77 up_read(&bus_type_sem);
78 return ret;
79}
80
33f767d7
RW
81static acpi_status do_acpi_find_child(acpi_handle handle, u32 lvl_not_used,
82 void *addr_p, void **ret_p)
4e10d12a 83{
33f767d7 84 unsigned long long addr;
4e10d12a 85 acpi_status status;
33f767d7
RW
86
87 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
88 if (ACPI_SUCCESS(status) && addr == *((u64 *)addr_p)) {
89 *ret_p = handle;
90 return AE_CTRL_TERMINATE;
4e10d12a
DSL
91 }
92 return AE_OK;
93}
94
439913ff 95acpi_handle acpi_get_child(acpi_handle parent, u64 address)
4e10d12a 96{
33f767d7 97 void *ret = NULL;
4e10d12a
DSL
98
99 if (!parent)
100 return NULL;
4e10d12a 101
33f767d7
RW
102 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, NULL,
103 do_acpi_find_child, &address, &ret);
104 return (acpi_handle)ret;
105}
4e10d12a
DSL
106EXPORT_SYMBOL(acpi_get_child);
107
ac212b69 108int acpi_bind_one(struct device *dev, acpi_handle handle)
4e10d12a 109{
1071695f 110 struct acpi_device *acpi_dev;
4e10d12a 111 acpi_status status;
f3fd0c8a 112 struct acpi_device_physical_node *physical_node, *pn;
1033f904
LT
113 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
114 int retval = -EINVAL;
4e10d12a 115
95f8a082 116 if (ACPI_HANDLE(dev)) {
f3fd0c8a
RW
117 if (handle) {
118 dev_warn(dev, "ACPI handle is already set\n");
119 return -EINVAL;
120 } else {
95f8a082 121 handle = ACPI_HANDLE(dev);
f3fd0c8a 122 }
4e10d12a 123 }
f3fd0c8a
RW
124 if (!handle)
125 return -EINVAL;
1033f904 126
4e10d12a 127 get_device(dev);
1033f904
LT
128 status = acpi_bus_get_device(handle, &acpi_dev);
129 if (ACPI_FAILURE(status))
130 goto err;
131
f3fd0c8a 132 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
1033f904
LT
133 if (!physical_node) {
134 retval = -ENOMEM;
135 goto err;
4e10d12a 136 }
4e10d12a 137
1033f904 138 mutex_lock(&acpi_dev->physical_node_lock);
f3fd0c8a
RW
139
140 /* Sanity check. */
141 list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
142 if (pn->dev == dev) {
143 dev_warn(dev, "Already associated with ACPI node\n");
144 goto err_free;
145 }
146
1033f904
LT
147 /* allocate physical node id according to physical_node_id_bitmap */
148 physical_node->node_id =
149 find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
150 ACPI_MAX_PHYSICAL_NODE);
151 if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
152 retval = -ENOSPC;
f3fd0c8a 153 goto err_free;
1071695f
DB
154 }
155
1033f904
LT
156 set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
157 physical_node->dev = dev;
158 list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
159 acpi_dev->physical_node_count++;
f3fd0c8a 160
1033f904
LT
161 mutex_unlock(&acpi_dev->physical_node_lock);
162
95f8a082
RW
163 if (!ACPI_HANDLE(dev))
164 ACPI_HANDLE_SET(dev, acpi_dev->handle);
1033f904
LT
165
166 if (!physical_node->node_id)
167 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
168 else
169 sprintf(physical_node_name,
170 "physical_node%d", physical_node->node_id);
171 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
172 physical_node_name);
173 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
174 "firmware_node");
175
176 if (acpi_dev->wakeup.flags.valid)
177 device_set_wakeup_capable(dev, true);
178
4e10d12a 179 return 0;
1033f904
LT
180
181 err:
95f8a082 182 ACPI_HANDLE_SET(dev, NULL);
1033f904
LT
183 put_device(dev);
184 return retval;
f3fd0c8a
RW
185
186 err_free:
187 mutex_unlock(&acpi_dev->physical_node_lock);
188 kfree(physical_node);
189 goto err;
4e10d12a 190}
ac212b69 191EXPORT_SYMBOL_GPL(acpi_bind_one);
4e10d12a 192
ac212b69 193int acpi_unbind_one(struct device *dev)
4e10d12a 194{
1033f904
LT
195 struct acpi_device_physical_node *entry;
196 struct acpi_device *acpi_dev;
197 acpi_status status;
198 struct list_head *node, *next;
199
95f8a082 200 if (!ACPI_HANDLE(dev))
4e10d12a 201 return 0;
1071695f 202
95f8a082 203 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
1033f904
LT
204 if (ACPI_FAILURE(status))
205 goto err;
1071695f 206
1033f904
LT
207 mutex_lock(&acpi_dev->physical_node_lock);
208 list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
209 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
210
211 entry = list_entry(node, struct acpi_device_physical_node,
212 node);
213 if (entry->dev != dev)
214 continue;
215
216 list_del(node);
217 clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
1071695f 218
1033f904
LT
219 acpi_dev->physical_node_count--;
220
221 if (!entry->node_id)
222 strcpy(physical_node_name, PHYSICAL_NODE_STRING);
223 else
224 sprintf(physical_node_name,
225 "physical_node%d", entry->node_id);
226
227 sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
228 sysfs_remove_link(&dev->kobj, "firmware_node");
95f8a082 229 ACPI_HANDLE_SET(dev, NULL);
4e10d12a
DSL
230 /* acpi_bind_one increase refcnt by one */
231 put_device(dev);
1033f904 232 kfree(entry);
4e10d12a 233 }
1033f904
LT
234 mutex_unlock(&acpi_dev->physical_node_lock);
235
4e10d12a 236 return 0;
1033f904
LT
237
238err:
239 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
240 return -EINVAL;
4e10d12a 241}
ac212b69 242EXPORT_SYMBOL_GPL(acpi_unbind_one);
4e10d12a
DSL
243
244static int acpi_platform_notify(struct device *dev)
245{
53540098 246 struct acpi_bus_type *type = acpi_get_bus_type(dev);
4e10d12a 247 acpi_handle handle;
11909ca1 248 int ret;
4e10d12a 249
f3fd0c8a 250 ret = acpi_bind_one(dev, NULL);
92414481 251 if (ret && type) {
11909ca1
RW
252 ret = type->find_device(dev, &handle);
253 if (ret) {
254 DBG("Unable to get handle for %s\n", dev_name(dev));
255 goto out;
256 }
257 ret = acpi_bind_one(dev, handle);
258 if (ret)
259 goto out;
4e10d12a 260 }
11909ca1
RW
261
262 if (type && type->setup)
263 type->setup(dev);
4e10d12a 264
f3fd0c8a 265 out:
4e10d12a
DSL
266#if ACPI_GLUE_DEBUG
267 if (!ret) {
268 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
269
a412a11d 270 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
db1461ad 271 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
02438d87 272 kfree(buffer.pointer);
4e10d12a 273 } else
db1461ad 274 DBG("Device %s -> No ACPI support\n", dev_name(dev));
4e10d12a
DSL
275#endif
276
277 return ret;
278}
279
280static int acpi_platform_notify_remove(struct device *dev)
281{
11909ca1
RW
282 struct acpi_bus_type *type;
283
53540098 284 type = acpi_get_bus_type(dev);
11909ca1
RW
285 if (type && type->cleanup)
286 type->cleanup(dev);
287
4e10d12a
DSL
288 acpi_unbind_one(dev);
289 return 0;
290}
291
0e46517d 292int __init init_acpi_device_notify(void)
4e10d12a 293{
4e10d12a
DSL
294 if (platform_notify || platform_notify_remove) {
295 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
296 return 0;
297 }
298 platform_notify = acpi_platform_notify;
299 platform_notify_remove = acpi_platform_notify_remove;
300 return 0;
301}