blob: 238412077c8394ee42ce66af4609697c670d6405 [file] [log] [blame]
David Shaohua Li4e10d122005-03-18 18:45:35 -05001/*
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 */
Paul Gortmaker214f2c92011-10-26 16:22:14 -04009#include <linux/export.h>
David Shaohua Li4e10d122005-03-18 18:45:35 -050010#include <linux/init.h>
11#include <linux/list.h>
12#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Shaohua Li4e10d122005-03-18 18:45:35 -050014#include <linux/rwsem.h>
15#include <linux/acpi.h>
16
Len Browna192a952009-07-28 16:45:54 -040017#include "internal.h"
18
David Shaohua Li4e10d122005-03-18 18:45:35 -050019#define ACPI_GLUE_DEBUG 0
20#if ACPI_GLUE_DEBUG
Joe Perches23415eb2012-12-18 06:31:30 +000021#define DBG(fmt, ...) \
22 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
David Shaohua Li4e10d122005-03-18 18:45:35 -050023#else
Joe Perches23415eb2012-12-18 06:31:30 +000024#define DBG(fmt, ...) \
25do { \
26 if (0) \
27 printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
28} while (0)
David Shaohua Li4e10d122005-03-18 18:45:35 -050029#endif
30static LIST_HEAD(bus_type_list);
31static DECLARE_RWSEM(bus_type_sem);
32
Lan Tianyu1033f902012-08-17 14:44:09 +080033#define PHYSICAL_NODE_STRING "physical_node"
34
David Shaohua Li4e10d122005-03-18 18:45:35 -050035int register_acpi_bus_type(struct acpi_bus_type *type)
36{
37 if (acpi_disabled)
38 return -ENODEV;
Rafael J. Wysocki53540092013-03-03 22:35:20 +010039 if (type && type->match && type->find_device) {
David Shaohua Li4e10d122005-03-18 18:45:35 -050040 down_write(&bus_type_sem);
41 list_add_tail(&type->list, &bus_type_list);
42 up_write(&bus_type_sem);
Rafael J. Wysocki53540092013-03-03 22:35:20 +010043 printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050044 return 0;
45 }
46 return -ENODEV;
47}
Jeff Garzik91e4d5a2012-07-25 14:24:13 -040048EXPORT_SYMBOL_GPL(register_acpi_bus_type);
David Shaohua Li4e10d122005-03-18 18:45:35 -050049
David Shaohua Li4e10d122005-03-18 18:45:35 -050050int 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);
Rafael J. Wysocki53540092013-03-03 22:35:20 +010058 printk(KERN_INFO PREFIX "bus type %s unregistered\n",
59 type->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050060 return 0;
61 }
62 return -ENODEV;
63}
Jeff Garzik91e4d5a2012-07-25 14:24:13 -040064EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
David Shaohua Li4e10d122005-03-18 18:45:35 -050065
Rafael J. Wysocki53540092013-03-03 22:35:20 +010066static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
David Shaohua Li4e10d122005-03-18 18:45:35 -050067{
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) {
Rafael J. Wysocki53540092013-03-03 22:35:20 +010072 if (tmp->match(dev)) {
David Shaohua Li4e10d122005-03-18 18:45:35 -050073 ret = tmp;
74 break;
75 }
76 }
77 up_read(&bus_type_sem);
78 return ret;
79}
80
Rafael J. Wysocki6e4fdb82013-08-07 22:55:00 +020081static acpi_status acpi_dev_present(acpi_handle handle, u32 lvl_not_used,
82 void *not_used, void **ret_p)
David Shaohua Li4e10d122005-03-18 18:45:35 -050083{
Rafael J. Wysocki6e4fdb82013-08-07 22:55:00 +020084 struct acpi_device *adev = NULL;
David Shaohua Li4e10d122005-03-18 18:45:35 -050085
Rafael J. Wysocki6e4fdb82013-08-07 22:55:00 +020086 acpi_bus_get_device(handle, &adev);
87 if (adev) {
Rafael J. Wysocki33f767d2013-01-10 13:13:49 +010088 *ret_p = handle;
Rafael J. Wysocki6e4fdb82013-08-07 22:55:00 +020089 return AE_CTRL_TERMINATE;
David Shaohua Li4e10d122005-03-18 18:45:35 -050090 }
91 return AE_OK;
92}
93
Rafael J. Wysocki6e4fdb82013-08-07 22:55:00 +020094static bool acpi_extra_checks_passed(acpi_handle handle, bool is_bridge)
David Shaohua Li4e10d122005-03-18 18:45:35 -050095{
Rafael J. Wysocki6e4fdb82013-08-07 22:55:00 +020096 unsigned long long sta;
97 acpi_status status;
David Shaohua Li4e10d122005-03-18 18:45:35 -050098
Rafael J. Wysocki6e4fdb82013-08-07 22:55:00 +020099 status = acpi_bus_get_status_handle(handle, &sta);
100 if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
101 return false;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500102
Rafael J. Wysocki6e4fdb82013-08-07 22:55:00 +0200103 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;
Rafael J. Wysocki33f767d2013-01-10 13:13:49 +0100112}
Rafael J. Wysocki6e4fdb82013-08-07 22:55:00 +0200113
114struct find_child_context {
115 u64 addr;
116 bool is_bridge;
117 acpi_handle ret;
118 bool ret_checked;
119};
120
121static 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
159acpi_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}
173EXPORT_SYMBOL_GPL(acpi_find_child);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500174
David Shaohua Li4e10d122005-03-18 18:45:35 -0500175static int acpi_bind_one(struct device *dev, acpi_handle handle)
176{
David Brownell10716952008-02-22 21:54:24 -0800177 struct acpi_device *acpi_dev;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500178 acpi_status status;
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100179 struct acpi_device_physical_node *physical_node, *pn;
Lan Tianyu1033f902012-08-17 14:44:09 +0800180 char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
181 int retval = -EINVAL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500182
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100183 if (ACPI_HANDLE(dev)) {
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100184 if (handle) {
185 dev_warn(dev, "ACPI handle is already set\n");
186 return -EINVAL;
187 } else {
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100188 handle = ACPI_HANDLE(dev);
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100189 }
David Shaohua Li4e10d122005-03-18 18:45:35 -0500190 }
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100191 if (!handle)
192 return -EINVAL;
Lan Tianyu1033f902012-08-17 14:44:09 +0800193
David Shaohua Li4e10d122005-03-18 18:45:35 -0500194 get_device(dev);
Lan Tianyu1033f902012-08-17 14:44:09 +0800195 status = acpi_bus_get_device(handle, &acpi_dev);
196 if (ACPI_FAILURE(status))
197 goto err;
198
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100199 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
Lan Tianyu1033f902012-08-17 14:44:09 +0800200 if (!physical_node) {
201 retval = -ENOMEM;
202 goto err;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500203 }
Lan Tianyu1033f902012-08-17 14:44:09 +0800204
205 mutex_lock(&acpi_dev->physical_node_lock);
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100206
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
Lan Tianyu1033f902012-08-17 14:44:09 +0800214 /* 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;
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100220 goto err_free;
Lan Tianyu1033f902012-08-17 14:44:09 +0800221 }
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++;
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100227
Lan Tianyu1033f902012-08-17 14:44:09 +0800228 mutex_unlock(&acpi_dev->physical_node_lock);
229
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100230 if (!ACPI_HANDLE(dev))
231 ACPI_HANDLE_SET(dev, acpi_dev->handle);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500232
Lan Tianyu1033f902012-08-17 14:44:09 +0800233 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");
David Brownell10716952008-02-22 21:54:24 -0800242
Lan Tianyu1033f902012-08-17 14:44:09 +0800243 if (acpi_dev->wakeup.flags.valid)
244 device_set_wakeup_capable(dev, true);
David Brownell10716952008-02-22 21:54:24 -0800245
David Shaohua Li4e10d122005-03-18 18:45:35 -0500246 return 0;
Lan Tianyu1033f902012-08-17 14:44:09 +0800247
248 err:
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100249 ACPI_HANDLE_SET(dev, NULL);
Lan Tianyu1033f902012-08-17 14:44:09 +0800250 put_device(dev);
251 return retval;
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100252
253 err_free:
254 mutex_unlock(&acpi_dev->physical_node_lock);
255 kfree(physical_node);
256 goto err;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500257}
258
259static int acpi_unbind_one(struct device *dev)
260{
Lan Tianyu1033f902012-08-17 14:44:09 +0800261 struct acpi_device_physical_node *entry;
262 struct acpi_device *acpi_dev;
263 acpi_status status;
264 struct list_head *node, *next;
265
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100266 if (!ACPI_HANDLE(dev))
David Shaohua Li4e10d122005-03-18 18:45:35 -0500267 return 0;
David Brownell10716952008-02-22 21:54:24 -0800268
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100269 status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
Lan Tianyu1033f902012-08-17 14:44:09 +0800270 if (ACPI_FAILURE(status))
271 goto err;
David Brownell10716952008-02-22 21:54:24 -0800272
Lan Tianyu1033f902012-08-17 14:44:09 +0800273 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];
David Brownell10716952008-02-22 21:54:24 -0800276
Lan Tianyu1033f902012-08-17 14:44:09 +0800277 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");
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100295 ACPI_HANDLE_SET(dev, NULL);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500296 /* acpi_bind_one increase refcnt by one */
297 put_device(dev);
Lan Tianyu1033f902012-08-17 14:44:09 +0800298 kfree(entry);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500299 }
Lan Tianyu1033f902012-08-17 14:44:09 +0800300 mutex_unlock(&acpi_dev->physical_node_lock);
301
David Shaohua Li4e10d122005-03-18 18:45:35 -0500302 return 0;
Lan Tianyu1033f902012-08-17 14:44:09 +0800303
304err:
305 dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
306 return -EINVAL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500307}
308
309static int acpi_platform_notify(struct device *dev)
310{
Rafael J. Wysocki53540092013-03-03 22:35:20 +0100311 struct acpi_bus_type *type = acpi_get_bus_type(dev);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500312 acpi_handle handle;
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100313 int ret;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500314
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100315 ret = acpi_bind_one(dev, NULL);
Rafael J. Wysocki92414482013-03-03 22:35:44 +0100316 if (ret && type) {
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100317 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;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500325 }
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100326
327 if (type && type->setup)
328 type->setup(dev);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500329
Rafael J. Wysockif3fd0c82012-11-21 00:21:39 +0100330 out:
David Shaohua Li4e10d122005-03-18 18:45:35 -0500331#if ACPI_GLUE_DEBUG
332 if (!ret) {
333 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
334
Yinghai Lua412a112013-01-12 14:00:06 +0100335 acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
Kay Sieversdb1461a2009-01-25 23:40:56 +0100336 DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
Len Brown02438d82006-06-30 03:19:10 -0400337 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500338 } else
Kay Sieversdb1461a2009-01-25 23:40:56 +0100339 DBG("Device %s -> No ACPI support\n", dev_name(dev));
David Shaohua Li4e10d122005-03-18 18:45:35 -0500340#endif
341
342 return ret;
343}
344
345static int acpi_platform_notify_remove(struct device *dev)
346{
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100347 struct acpi_bus_type *type;
348
Rafael J. Wysocki53540092013-03-03 22:35:20 +0100349 type = acpi_get_bus_type(dev);
Rafael J. Wysocki11909ca2012-12-23 00:02:13 +0100350 if (type && type->cleanup)
351 type->cleanup(dev);
352
David Shaohua Li4e10d122005-03-18 18:45:35 -0500353 acpi_unbind_one(dev);
354 return 0;
355}
356
Bjorn Helgaas0e465172009-03-24 16:50:09 -0600357int __init init_acpi_device_notify(void)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500358{
David Shaohua Li4e10d122005-03-18 18:45:35 -0500359 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}