ACPI: Move definition of PREFIX from acpi_bus.h to internal..h
[deliverable/linux.git] / drivers / acpi / pci_root.c
1 /*
2 * pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/pm.h>
33 #include <linux/pci.h>
34 #include <linux/pci-acpi.h>
35 #include <linux/acpi.h>
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
38
39 #define PREFIX "ACPI: "
40
41 #define _COMPONENT ACPI_PCI_COMPONENT
42 ACPI_MODULE_NAME("pci_root");
43 #define ACPI_PCI_ROOT_CLASS "pci_bridge"
44 #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
45 static int acpi_pci_root_add(struct acpi_device *device);
46 static int acpi_pci_root_remove(struct acpi_device *device, int type);
47 static int acpi_pci_root_start(struct acpi_device *device);
48
49 static struct acpi_device_id root_device_ids[] = {
50 {"PNP0A03", 0},
51 {"", 0},
52 };
53 MODULE_DEVICE_TABLE(acpi, root_device_ids);
54
55 static struct acpi_driver acpi_pci_root_driver = {
56 .name = "pci_root",
57 .class = ACPI_PCI_ROOT_CLASS,
58 .ids = root_device_ids,
59 .ops = {
60 .add = acpi_pci_root_add,
61 .remove = acpi_pci_root_remove,
62 .start = acpi_pci_root_start,
63 },
64 };
65
66 struct acpi_pci_root {
67 struct list_head node;
68 struct acpi_device *device;
69 struct pci_bus *bus;
70 u16 segment;
71 u8 bus_nr;
72
73 u32 osc_support_set; /* _OSC state of support bits */
74 u32 osc_control_set; /* _OSC state of control bits */
75 u32 osc_control_qry; /* the latest _OSC query result */
76
77 u32 osc_queried:1; /* has _OSC control been queried? */
78 };
79
80 static LIST_HEAD(acpi_pci_roots);
81
82 static struct acpi_pci_driver *sub_driver;
83 static DEFINE_MUTEX(osc_lock);
84
85 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
86 {
87 int n = 0;
88 struct acpi_pci_root *root;
89
90 struct acpi_pci_driver **pptr = &sub_driver;
91 while (*pptr)
92 pptr = &(*pptr)->next;
93 *pptr = driver;
94
95 if (!driver->add)
96 return 0;
97
98 list_for_each_entry(root, &acpi_pci_roots, node) {
99 driver->add(root->device->handle);
100 n++;
101 }
102
103 return n;
104 }
105
106 EXPORT_SYMBOL(acpi_pci_register_driver);
107
108 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
109 {
110 struct acpi_pci_root *root;
111
112 struct acpi_pci_driver **pptr = &sub_driver;
113 while (*pptr) {
114 if (*pptr == driver)
115 break;
116 pptr = &(*pptr)->next;
117 }
118 BUG_ON(!*pptr);
119 *pptr = (*pptr)->next;
120
121 if (!driver->remove)
122 return;
123
124 list_for_each_entry(root, &acpi_pci_roots, node)
125 driver->remove(root->device->handle);
126 }
127
128 EXPORT_SYMBOL(acpi_pci_unregister_driver);
129
130 acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
131 {
132 struct acpi_pci_root *root;
133
134 list_for_each_entry(root, &acpi_pci_roots, node)
135 if ((root->segment == (u16) seg) && (root->bus_nr == (u16) bus))
136 return root->device->handle;
137 return NULL;
138 }
139
140 EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
141
142 /**
143 * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
144 * @handle - the ACPI CA node in question.
145 *
146 * Note: we could make this API take a struct acpi_device * instead, but
147 * for now, it's more convenient to operate on an acpi_handle.
148 */
149 int acpi_is_root_bridge(acpi_handle handle)
150 {
151 int ret;
152 struct acpi_device *device;
153
154 ret = acpi_bus_get_device(handle, &device);
155 if (ret)
156 return 0;
157
158 ret = acpi_match_device_ids(device, root_device_ids);
159 if (ret)
160 return 0;
161 else
162 return 1;
163 }
164 EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
165
166 static acpi_status
167 get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
168 {
169 int *busnr = data;
170 struct acpi_resource_address64 address;
171
172 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
173 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
174 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
175 return AE_OK;
176
177 acpi_resource_to_address64(resource, &address);
178 if ((address.address_length > 0) &&
179 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
180 *busnr = address.minimum;
181
182 return AE_OK;
183 }
184
185 static acpi_status try_get_root_bridge_busnr(acpi_handle handle,
186 unsigned long long *bus)
187 {
188 acpi_status status;
189 int busnum;
190
191 busnum = -1;
192 status =
193 acpi_walk_resources(handle, METHOD_NAME__CRS,
194 get_root_bridge_busnr_callback, &busnum);
195 if (ACPI_FAILURE(status))
196 return status;
197 /* Check if we really get a bus number from _CRS */
198 if (busnum == -1)
199 return AE_ERROR;
200 *bus = busnum;
201 return AE_OK;
202 }
203
204 static void acpi_pci_bridge_scan(struct acpi_device *device)
205 {
206 int status;
207 struct acpi_device *child = NULL;
208
209 if (device->flags.bus_address)
210 if (device->parent && device->parent->ops.bind) {
211 status = device->parent->ops.bind(device);
212 if (!status) {
213 list_for_each_entry(child, &device->children, node)
214 acpi_pci_bridge_scan(child);
215 }
216 }
217 }
218
219 static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40,
220 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
221
222 static acpi_status acpi_pci_run_osc(acpi_handle handle,
223 const u32 *capbuf, u32 *retval)
224 {
225 acpi_status status;
226 struct acpi_object_list input;
227 union acpi_object in_params[4];
228 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
229 union acpi_object *out_obj;
230 u32 errors;
231
232 /* Setting up input parameters */
233 input.count = 4;
234 input.pointer = in_params;
235 in_params[0].type = ACPI_TYPE_BUFFER;
236 in_params[0].buffer.length = 16;
237 in_params[0].buffer.pointer = OSC_UUID;
238 in_params[1].type = ACPI_TYPE_INTEGER;
239 in_params[1].integer.value = 1;
240 in_params[2].type = ACPI_TYPE_INTEGER;
241 in_params[2].integer.value = 3;
242 in_params[3].type = ACPI_TYPE_BUFFER;
243 in_params[3].buffer.length = 12;
244 in_params[3].buffer.pointer = (u8 *)capbuf;
245
246 status = acpi_evaluate_object(handle, "_OSC", &input, &output);
247 if (ACPI_FAILURE(status))
248 return status;
249
250 if (!output.length)
251 return AE_NULL_OBJECT;
252
253 out_obj = output.pointer;
254 if (out_obj->type != ACPI_TYPE_BUFFER) {
255 printk(KERN_DEBUG "_OSC evaluation returned wrong type\n");
256 status = AE_TYPE;
257 goto out_kfree;
258 }
259 /* Need to ignore the bit0 in result code */
260 errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
261 if (errors) {
262 if (errors & OSC_REQUEST_ERROR)
263 printk(KERN_DEBUG "_OSC request failed\n");
264 if (errors & OSC_INVALID_UUID_ERROR)
265 printk(KERN_DEBUG "_OSC invalid UUID\n");
266 if (errors & OSC_INVALID_REVISION_ERROR)
267 printk(KERN_DEBUG "_OSC invalid revision\n");
268 if (errors & OSC_CAPABILITIES_MASK_ERROR) {
269 if (capbuf[OSC_QUERY_TYPE] & OSC_QUERY_ENABLE)
270 goto out_success;
271 printk(KERN_DEBUG
272 "Firmware did not grant requested _OSC control\n");
273 status = AE_SUPPORT;
274 goto out_kfree;
275 }
276 status = AE_ERROR;
277 goto out_kfree;
278 }
279 out_success:
280 *retval = *((u32 *)(out_obj->buffer.pointer + 8));
281 status = AE_OK;
282
283 out_kfree:
284 kfree(output.pointer);
285 return status;
286 }
287
288 static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root, u32 flags)
289 {
290 acpi_status status;
291 u32 support_set, result, capbuf[3];
292
293 /* do _OSC query for all possible controls */
294 support_set = root->osc_support_set | (flags & OSC_SUPPORT_MASKS);
295 capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
296 capbuf[OSC_SUPPORT_TYPE] = support_set;
297 capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
298
299 status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
300 if (ACPI_SUCCESS(status)) {
301 root->osc_support_set = support_set;
302 root->osc_control_qry = result;
303 root->osc_queried = 1;
304 }
305 return status;
306 }
307
308 static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
309 {
310 acpi_status status;
311 acpi_handle tmp;
312
313 status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
314 if (ACPI_FAILURE(status))
315 return status;
316 mutex_lock(&osc_lock);
317 status = acpi_pci_query_osc(root, flags);
318 mutex_unlock(&osc_lock);
319 return status;
320 }
321
322 static struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
323 {
324 struct acpi_pci_root *root;
325
326 list_for_each_entry(root, &acpi_pci_roots, node) {
327 if (root->device->handle == handle)
328 return root;
329 }
330 return NULL;
331 }
332
333 struct acpi_handle_node {
334 struct list_head node;
335 acpi_handle handle;
336 };
337
338 /**
339 * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
340 * @handle: the handle in question
341 *
342 * Given an ACPI CA handle, the desired PCI device is located in the
343 * list of PCI devices.
344 *
345 * If the device is found, its reference count is increased and this
346 * function returns a pointer to its data structure. The caller must
347 * decrement the reference count by calling pci_dev_put().
348 * If no device is found, %NULL is returned.
349 */
350 struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
351 {
352 int dev, fn;
353 unsigned long long adr;
354 acpi_status status;
355 acpi_handle phandle;
356 struct pci_bus *pbus;
357 struct pci_dev *pdev = NULL;
358 struct acpi_handle_node *node, *tmp;
359 struct acpi_pci_root *root;
360 LIST_HEAD(device_list);
361
362 /*
363 * Walk up the ACPI CA namespace until we reach a PCI root bridge.
364 */
365 phandle = handle;
366 while (!acpi_is_root_bridge(phandle)) {
367 node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
368 if (!node)
369 goto out;
370
371 INIT_LIST_HEAD(&node->node);
372 node->handle = phandle;
373 list_add(&node->node, &device_list);
374
375 status = acpi_get_parent(phandle, &phandle);
376 if (ACPI_FAILURE(status))
377 goto out;
378 }
379
380 root = acpi_pci_find_root(phandle);
381 if (!root)
382 goto out;
383
384 pbus = root->bus;
385
386 /*
387 * Now, walk back down the PCI device tree until we return to our
388 * original handle. Assumes that everything between the PCI root
389 * bridge and the device we're looking for must be a P2P bridge.
390 */
391 list_for_each_entry(node, &device_list, node) {
392 acpi_handle hnd = node->handle;
393 status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
394 if (ACPI_FAILURE(status))
395 goto out;
396 dev = (adr >> 16) & 0xffff;
397 fn = adr & 0xffff;
398
399 pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
400 if (!pdev || hnd == handle)
401 break;
402
403 pbus = pdev->subordinate;
404 pci_dev_put(pdev);
405 }
406 out:
407 list_for_each_entry_safe(node, tmp, &device_list, node)
408 kfree(node);
409
410 return pdev;
411 }
412 EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
413
414 /**
415 * acpi_pci_osc_control_set - commit requested control to Firmware
416 * @handle: acpi_handle for the target ACPI object
417 * @flags: driver's requested control bits
418 *
419 * Attempt to take control from Firmware on requested control bits.
420 **/
421 acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 flags)
422 {
423 acpi_status status;
424 u32 control_req, result, capbuf[3];
425 acpi_handle tmp;
426 struct acpi_pci_root *root;
427
428 status = acpi_get_handle(handle, "_OSC", &tmp);
429 if (ACPI_FAILURE(status))
430 return status;
431
432 control_req = (flags & OSC_CONTROL_MASKS);
433 if (!control_req)
434 return AE_TYPE;
435
436 root = acpi_pci_find_root(handle);
437 if (!root)
438 return AE_NOT_EXIST;
439
440 mutex_lock(&osc_lock);
441 /* No need to evaluate _OSC if the control was already granted. */
442 if ((root->osc_control_set & control_req) == control_req)
443 goto out;
444
445 /* Need to query controls first before requesting them */
446 if (!root->osc_queried) {
447 status = acpi_pci_query_osc(root, root->osc_support_set);
448 if (ACPI_FAILURE(status))
449 goto out;
450 }
451 if ((root->osc_control_qry & control_req) != control_req) {
452 printk(KERN_DEBUG
453 "Firmware did not grant requested _OSC control\n");
454 status = AE_SUPPORT;
455 goto out;
456 }
457
458 capbuf[OSC_QUERY_TYPE] = 0;
459 capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
460 capbuf[OSC_CONTROL_TYPE] = root->osc_control_set | control_req;
461 status = acpi_pci_run_osc(handle, capbuf, &result);
462 if (ACPI_SUCCESS(status))
463 root->osc_control_set = result;
464 out:
465 mutex_unlock(&osc_lock);
466 return status;
467 }
468 EXPORT_SYMBOL(acpi_pci_osc_control_set);
469
470 static int __devinit acpi_pci_root_add(struct acpi_device *device)
471 {
472 unsigned long long segment, bus;
473 acpi_status status;
474 int result;
475 struct acpi_pci_root *root;
476 acpi_handle handle;
477 struct acpi_device *child;
478 u32 flags, base_flags;
479
480 segment = 0;
481 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
482 &segment);
483 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
484 printk(KERN_ERR PREFIX "can't evaluate _SEG\n");
485 return -ENODEV;
486 }
487
488 /* Check _CRS first, then _BBN. If no _BBN, default to zero. */
489 bus = 0;
490 status = try_get_root_bridge_busnr(device->handle, &bus);
491 if (ACPI_FAILURE(status)) {
492 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL, &bus);
493 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
494 printk(KERN_ERR PREFIX
495 "no bus number in _CRS and can't evaluate _BBN\n");
496 return -ENODEV;
497 }
498 }
499
500 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
501 if (!root)
502 return -ENOMEM;
503
504 INIT_LIST_HEAD(&root->node);
505 root->device = device;
506 root->segment = segment & 0xFFFF;
507 root->bus_nr = bus & 0xFF;
508 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
509 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
510 device->driver_data = root;
511
512 /*
513 * All supported architectures that use ACPI have support for
514 * PCI domains, so we indicate this in _OSC support capabilities.
515 */
516 flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
517 acpi_pci_osc_support(root, flags);
518
519 /*
520 * TBD: Need PCI interface for enumeration/configuration of roots.
521 */
522
523 /* TBD: Locking */
524 list_add_tail(&root->node, &acpi_pci_roots);
525
526 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
527 acpi_device_name(device), acpi_device_bid(device),
528 root->segment, root->bus_nr);
529
530 /*
531 * Scan the Root Bridge
532 * --------------------
533 * Must do this prior to any attempt to bind the root device, as the
534 * PCI namespace does not get created until this call is made (and
535 * thus the root bridge's pci_dev does not exist).
536 */
537 root->bus = pci_acpi_scan_root(device, segment, bus);
538 if (!root->bus) {
539 printk(KERN_ERR PREFIX
540 "Bus %04x:%02x not present in PCI namespace\n",
541 root->segment, root->bus_nr);
542 result = -ENODEV;
543 goto end;
544 }
545
546 /*
547 * Attach ACPI-PCI Context
548 * -----------------------
549 * Thus binding the ACPI and PCI devices.
550 */
551 result = acpi_pci_bind_root(device);
552 if (result)
553 goto end;
554
555 /*
556 * PCI Routing Table
557 * -----------------
558 * Evaluate and parse _PRT, if exists.
559 */
560 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
561 if (ACPI_SUCCESS(status))
562 result = acpi_pci_irq_add_prt(device->handle, root->bus);
563
564 /*
565 * Scan and bind all _ADR-Based Devices
566 */
567 list_for_each_entry(child, &device->children, node)
568 acpi_pci_bridge_scan(child);
569
570 /* Indicate support for various _OSC capabilities. */
571 if (pci_ext_cfg_avail(root->bus->self))
572 flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
573 if (pcie_aspm_enabled())
574 flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
575 OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
576 if (pci_msi_enabled())
577 flags |= OSC_MSI_SUPPORT;
578 if (flags != base_flags)
579 acpi_pci_osc_support(root, flags);
580
581 return 0;
582
583 end:
584 if (!list_empty(&root->node))
585 list_del(&root->node);
586 kfree(root);
587 return result;
588 }
589
590 static int acpi_pci_root_start(struct acpi_device *device)
591 {
592 struct acpi_pci_root *root = acpi_driver_data(device);
593
594 pci_bus_add_devices(root->bus);
595 return 0;
596 }
597
598 static int acpi_pci_root_remove(struct acpi_device *device, int type)
599 {
600 struct acpi_pci_root *root = acpi_driver_data(device);
601
602 kfree(root);
603 return 0;
604 }
605
606 static int __init acpi_pci_root_init(void)
607 {
608 if (acpi_pci_disabled)
609 return 0;
610
611 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
612 return -ENODEV;
613
614 return 0;
615 }
616
617 subsys_initcall(acpi_pci_root_init);
This page took 0.063055 seconds and 5 git commands to generate.