blob: 8a0324b43e53869adcf9f98a29c513a38b1c2d15 [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 */
9#include <linux/init.h>
10#include <linux/list.h>
11#include <linux/device.h>
12#include <linux/rwsem.h>
13#include <linux/acpi.h>
14
15#define ACPI_GLUE_DEBUG 0
16#if ACPI_GLUE_DEBUG
17#define DBG(x...) printk(PREFIX x)
18#else
19#define DBG(x...)
20#endif
21static LIST_HEAD(bus_type_list);
22static DECLARE_RWSEM(bus_type_sem);
23
24int register_acpi_bus_type(struct acpi_bus_type *type)
25{
26 if (acpi_disabled)
27 return -ENODEV;
28 if (type && type->bus && type->find_device) {
29 down_write(&bus_type_sem);
30 list_add_tail(&type->list, &bus_type_list);
31 up_write(&bus_type_sem);
Len Brown4be44fc2005-08-05 00:44:28 -040032 printk(KERN_INFO PREFIX "bus type %s registered\n",
33 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050034 return 0;
35 }
36 return -ENODEV;
37}
38
39EXPORT_SYMBOL(register_acpi_bus_type);
40
41int unregister_acpi_bus_type(struct acpi_bus_type *type)
42{
43 if (acpi_disabled)
44 return 0;
45 if (type) {
46 down_write(&bus_type_sem);
47 list_del_init(&type->list);
48 up_write(&bus_type_sem);
Len Brown4be44fc2005-08-05 00:44:28 -040049 printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
50 type->bus->name);
David Shaohua Li4e10d122005-03-18 18:45:35 -050051 return 0;
52 }
53 return -ENODEV;
54}
55
56EXPORT_SYMBOL(unregister_acpi_bus_type);
57
58static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
59{
60 struct acpi_bus_type *tmp, *ret = NULL;
61
62 down_read(&bus_type_sem);
63 list_for_each_entry(tmp, &bus_type_list, list) {
64 if (tmp->bus == type) {
65 ret = tmp;
66 break;
67 }
68 }
69 up_read(&bus_type_sem);
70 return ret;
71}
72
73static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
74{
75 struct acpi_bus_type *tmp;
76 int ret = -ENODEV;
77
78 down_read(&bus_type_sem);
79 list_for_each_entry(tmp, &bus_type_list, list) {
80 if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
81 ret = 0;
82 break;
83 }
84 }
85 up_read(&bus_type_sem);
86 return ret;
87}
88
89/* Get PCI root bridge's handle from its segment and bus number */
90struct acpi_find_pci_root {
91 unsigned int seg;
92 unsigned int bus;
93 acpi_handle handle;
94};
95
96static acpi_status
97do_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
98{
Jan Engelhardt50dd0962006-10-01 00:28:50 +020099 unsigned long *busnr = data;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500100 struct acpi_resource_address64 address;
101
Bob Moore50eca3e2005-09-30 19:03:00 -0400102 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
103 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
104 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500105 return AE_OK;
106
107 acpi_resource_to_address64(resource, &address);
108 if ((address.address_length > 0) &&
109 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
Bob Moore50eca3e2005-09-30 19:03:00 -0400110 *busnr = address.minimum;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500111
112 return AE_OK;
113}
114
115static int get_root_bridge_busnr(acpi_handle handle)
116{
117 acpi_status status;
Peter Chubb51b190b2005-10-19 22:45:14 -0700118 unsigned long bus, bbn;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500119 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
120
121 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
122
123 status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL,
Peter Chubb51b190b2005-10-19 22:45:14 -0700124 &bbn);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500125 if (status == AE_NOT_FOUND) {
126 /* Assume bus = 0 */
127 printk(KERN_INFO PREFIX
128 "Assume root bridge [%s] bus is 0\n",
129 (char *)buffer.pointer);
130 status = AE_OK;
131 bbn = 0;
132 }
133 if (ACPI_FAILURE(status)) {
134 bbn = -ENODEV;
135 goto exit;
136 }
137 if (bbn > 0)
138 goto exit;
139
140 /* _BBN in some systems return 0 for all root bridges */
141 bus = -1;
142 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
143 do_root_bridge_busnr_callback, &bus);
144 /* If _CRS failed, we just use _BBN */
145 if (ACPI_FAILURE(status) || (bus == -1))
146 goto exit;
147 /* We select _CRS */
148 if (bbn != bus) {
149 printk(KERN_INFO PREFIX
150 "_BBN and _CRS returns different value for %s. Select _CRS\n",
151 (char *)buffer.pointer);
152 bbn = bus;
153 }
154 exit:
Len Brown02438d82006-06-30 03:19:10 -0400155 kfree(buffer.pointer);
Peter Chubb51b190b2005-10-19 22:45:14 -0700156 return (int)bbn;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500157}
158
159static acpi_status
160find_pci_rootbridge(acpi_handle handle, u32 lvl, void *context, void **rv)
161{
162 struct acpi_find_pci_root *find = (struct acpi_find_pci_root *)context;
163 unsigned long seg, bus;
164 acpi_status status;
165 int tmp;
166 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
167
168 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
169
170 status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &seg);
171 if (status == AE_NOT_FOUND) {
172 /* Assume seg = 0 */
David Shaohua Li4e10d122005-03-18 18:45:35 -0500173 status = AE_OK;
174 seg = 0;
175 }
176 if (ACPI_FAILURE(status)) {
177 status = AE_CTRL_DEPTH;
178 goto exit;
179 }
180
181 tmp = get_root_bridge_busnr(handle);
182 if (tmp < 0) {
183 printk(KERN_ERR PREFIX
184 "Find root bridge failed for %s\n",
185 (char *)buffer.pointer);
186 status = AE_CTRL_DEPTH;
187 goto exit;
188 }
189 bus = tmp;
190
191 if (seg == find->seg && bus == find->bus)
Chen, Justin2f000f52006-10-10 17:07:00 -0400192 {
David Shaohua Li4e10d122005-03-18 18:45:35 -0500193 find->handle = handle;
Chen, Justin2f000f52006-10-10 17:07:00 -0400194 status = AE_CTRL_TERMINATE;
195 }
196 else
197 status = AE_OK;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500198 exit:
Len Brown02438d82006-06-30 03:19:10 -0400199 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500200 return status;
201}
202
203acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
204{
205 struct acpi_find_pci_root find = { seg, bus, NULL };
206
207 acpi_get_devices(PCI_ROOT_HID_STRING, find_pci_rootbridge, &find, NULL);
208 return find.handle;
209}
rajesh.shah@intel.coma3a45ec2005-10-31 16:20:12 -0800210EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500211
212/* Get device's handler per its address under its parent */
213struct acpi_find_child {
214 acpi_handle handle;
215 acpi_integer address;
216};
217
218static acpi_status
219do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
220{
221 acpi_status status;
222 struct acpi_device_info *info;
223 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200224 struct acpi_find_child *find = context;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500225
226 status = acpi_get_object_info(handle, &buffer);
227 if (ACPI_SUCCESS(status)) {
228 info = buffer.pointer;
229 if (info->address == find->address)
230 find->handle = handle;
Len Brown02438d82006-06-30 03:19:10 -0400231 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500232 }
233 return AE_OK;
234}
235
236acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
237{
238 struct acpi_find_child find = { NULL, address };
239
240 if (!parent)
241 return NULL;
242 acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
243 1, do_acpi_find_child, &find, NULL);
244 return find.handle;
245}
246
247EXPORT_SYMBOL(acpi_get_child);
248
249/* Link ACPI devices with physical devices */
250static void acpi_glue_data_handler(acpi_handle handle,
251 u32 function, void *context)
252{
253 /* we provide an empty handler */
254}
255
256/* Note: a success call will increase reference count by one */
257struct device *acpi_get_physical_device(acpi_handle handle)
258{
259 acpi_status status;
260 struct device *dev;
261
262 status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
263 if (ACPI_SUCCESS(status))
264 return get_device(dev);
265 return NULL;
266}
267
268EXPORT_SYMBOL(acpi_get_physical_device);
269
270static int acpi_bind_one(struct device *dev, acpi_handle handle)
271{
272 acpi_status status;
273
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100274 if (dev->archdata.acpi_handle) {
David Shaohua Li4e10d122005-03-18 18:45:35 -0500275 printk(KERN_WARNING PREFIX
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100276 "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500277 return -EINVAL;
278 }
279 get_device(dev);
280 status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
281 if (ACPI_FAILURE(status)) {
282 put_device(dev);
283 return -EINVAL;
284 }
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100285 dev->archdata.acpi_handle = handle;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500286
287 return 0;
288}
289
290static int acpi_unbind_one(struct device *dev)
291{
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100292 if (!dev->archdata.acpi_handle)
David Shaohua Li4e10d122005-03-18 18:45:35 -0500293 return 0;
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100294 if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
David Shaohua Li4e10d122005-03-18 18:45:35 -0500295 /* acpi_get_physical_device increase refcnt by one */
296 put_device(dev);
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100297 acpi_detach_data(dev->archdata.acpi_handle,
298 acpi_glue_data_handler);
299 dev->archdata.acpi_handle = NULL;
David Shaohua Li4e10d122005-03-18 18:45:35 -0500300 /* acpi_bind_one increase refcnt by one */
301 put_device(dev);
302 } else {
303 printk(KERN_ERR PREFIX
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100304 "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500305 }
306 return 0;
307}
308
309static int acpi_platform_notify(struct device *dev)
310{
311 struct acpi_bus_type *type;
312 acpi_handle handle;
313 int ret = -EINVAL;
314
315 if (!dev->bus || !dev->parent) {
316 /* bridge devices genernally haven't bus or parent */
317 ret = acpi_find_bridge_device(dev, &handle);
318 goto end;
319 }
320 type = acpi_get_bus_type(dev->bus);
321 if (!type) {
David Shaohua Lief7b06c2005-04-18 22:59:23 -0400322 DBG("No ACPI bus support for %s\n", dev->bus_id);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500323 ret = -EINVAL;
324 goto end;
325 }
326 if ((ret = type->find_device(dev, &handle)) != 0)
David Shaohua Lief7b06c2005-04-18 22:59:23 -0400327 DBG("Can't get handler for %s\n", dev->bus_id);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500328 end:
329 if (!ret)
330 acpi_bind_one(dev, handle);
331
332#if ACPI_GLUE_DEBUG
333 if (!ret) {
334 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
335
Benjamin Herrenschmidt465ae642006-11-11 17:18:42 +1100336 acpi_get_name(dev->archdata.acpi_handle,
337 ACPI_FULL_PATHNAME, &buffer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500338 DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
Len Brown02438d82006-06-30 03:19:10 -0400339 kfree(buffer.pointer);
David Shaohua Li4e10d122005-03-18 18:45:35 -0500340 } else
341 DBG("Device %s -> No ACPI support\n", dev->bus_id);
342#endif
343
344 return ret;
345}
346
347static int acpi_platform_notify_remove(struct device *dev)
348{
349 acpi_unbind_one(dev);
350 return 0;
351}
352
353static int __init init_acpi_device_notify(void)
354{
355 if (acpi_disabled)
356 return 0;
357 if (platform_notify || platform_notify_remove) {
358 printk(KERN_ERR PREFIX "Can't use platform_notify\n");
359 return 0;
360 }
361 platform_notify = acpi_platform_notify;
362 platform_notify_remove = acpi_platform_notify_remove;
363 return 0;
364}
365
366arch_initcall(init_acpi_device_notify);