ACPI: Introduce acpi_get_pci_dev()
[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 _COMPONENT ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME("pci_root");
41 #define ACPI_PCI_ROOT_CLASS "pci_bridge"
42 #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
43 static int acpi_pci_root_add(struct acpi_device *device);
44 static int acpi_pci_root_remove(struct acpi_device *device, int type);
45 static int acpi_pci_root_start(struct acpi_device *device);
46
47 static struct acpi_device_id root_device_ids[] = {
48 {"PNP0A03", 0},
49 {"", 0},
50 };
51 MODULE_DEVICE_TABLE(acpi, root_device_ids);
52
53 static struct acpi_driver acpi_pci_root_driver = {
54 .name = "pci_root",
55 .class = ACPI_PCI_ROOT_CLASS,
56 .ids = root_device_ids,
57 .ops = {
58 .add = acpi_pci_root_add,
59 .remove = acpi_pci_root_remove,
60 .start = acpi_pci_root_start,
61 },
62 };
63
64 struct acpi_pci_root {
65 struct list_head node;
66 struct acpi_device * device;
67 struct acpi_pci_id id;
68 struct pci_bus *bus;
69
70 u32 osc_support_set; /* _OSC state of support bits */
71 u32 osc_control_set; /* _OSC state of control bits */
72 u32 osc_control_qry; /* the latest _OSC query result */
73
74 u32 osc_queried:1; /* has _OSC control been queried? */
75 };
76
77 static LIST_HEAD(acpi_pci_roots);
78
79 static struct acpi_pci_driver *sub_driver;
80 static DEFINE_MUTEX(osc_lock);
81
82 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
83 {
84 int n = 0;
85 struct list_head *entry;
86
87 struct acpi_pci_driver **pptr = &sub_driver;
88 while (*pptr)
89 pptr = &(*pptr)->next;
90 *pptr = driver;
91
92 if (!driver->add)
93 return 0;
94
95 list_for_each(entry, &acpi_pci_roots) {
96 struct acpi_pci_root *root;
97 root = list_entry(entry, struct acpi_pci_root, node);
98 driver->add(root->device->handle);
99 n++;
100 }
101
102 return n;
103 }
104
105 EXPORT_SYMBOL(acpi_pci_register_driver);
106
107 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
108 {
109 struct list_head *entry;
110
111 struct acpi_pci_driver **pptr = &sub_driver;
112 while (*pptr) {
113 if (*pptr == driver)
114 break;
115 pptr = &(*pptr)->next;
116 }
117 BUG_ON(!*pptr);
118 *pptr = (*pptr)->next;
119
120 if (!driver->remove)
121 return;
122
123 list_for_each(entry, &acpi_pci_roots) {
124 struct acpi_pci_root *root;
125 root = list_entry(entry, struct acpi_pci_root, node);
126 driver->remove(root->device->handle);
127 }
128 }
129
130 EXPORT_SYMBOL(acpi_pci_unregister_driver);
131
132 acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
133 {
134 struct acpi_pci_root *tmp;
135
136 list_for_each_entry(tmp, &acpi_pci_roots, node) {
137 if ((tmp->id.segment == (u16) seg) && (tmp->id.bus == (u16) bus))
138 return tmp->device->handle;
139 }
140 return NULL;
141 }
142
143 EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
144
145 /**
146 * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
147 * @handle - the ACPI CA node in question.
148 *
149 * Note: we could make this API take a struct acpi_device * instead, but
150 * for now, it's more convenient to operate on an acpi_handle.
151 */
152 int acpi_is_root_bridge(acpi_handle handle)
153 {
154 int ret;
155 struct acpi_device *device;
156
157 ret = acpi_bus_get_device(handle, &device);
158 if (ret)
159 return 0;
160
161 ret = acpi_match_device_ids(device, root_device_ids);
162 if (ret)
163 return 0;
164 else
165 return 1;
166 }
167 EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
168
169 static acpi_status
170 get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
171 {
172 int *busnr = data;
173 struct acpi_resource_address64 address;
174
175 if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
176 resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
177 resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
178 return AE_OK;
179
180 acpi_resource_to_address64(resource, &address);
181 if ((address.address_length > 0) &&
182 (address.resource_type == ACPI_BUS_NUMBER_RANGE))
183 *busnr = address.minimum;
184
185 return AE_OK;
186 }
187
188 static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
189 {
190 acpi_status status;
191
192 *busnum = -1;
193 status =
194 acpi_walk_resources(handle, METHOD_NAME__CRS,
195 get_root_bridge_busnr_callback, busnum);
196 if (ACPI_FAILURE(status))
197 return status;
198 /* Check if we really get a bus number from _CRS */
199 if (*busnum == -1)
200 return AE_ERROR;
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 list_for_each_entry(root, &acpi_pci_roots, node) {
326 if (root->device->handle == handle)
327 return root;
328 }
329 return NULL;
330 }
331
332 struct acpi_handle_node {
333 struct list_head node;
334 acpi_handle handle;
335 };
336
337 /**
338 * acpi_get_pci_dev - convert ACPI CA handle to struct pci_dev
339 * @handle: the handle in question
340 *
341 * Given an ACPI CA handle, the desired PCI device is located in the
342 * list of PCI devices.
343 *
344 * If the device is found, its reference count is increased and this
345 * function returns a pointer to its data structure. The caller must
346 * decrement the reference count by calling pci_dev_put().
347 * If no device is found, %NULL is returned.
348 */
349 struct pci_dev *acpi_get_pci_dev(acpi_handle handle)
350 {
351 int dev, fn;
352 unsigned long long adr;
353 acpi_status status;
354 acpi_handle phandle;
355 struct pci_bus *pbus;
356 struct pci_dev *pdev = NULL;
357 struct acpi_handle_node *node, *tmp;
358 struct acpi_pci_root *root;
359 LIST_HEAD(device_list);
360
361 /*
362 * Walk up the ACPI CA namespace until we reach a PCI root bridge.
363 */
364 phandle = handle;
365 while (!acpi_is_root_bridge(phandle)) {
366 node = kzalloc(sizeof(struct acpi_handle_node), GFP_KERNEL);
367 if (!node)
368 goto out;
369
370 INIT_LIST_HEAD(&node->node);
371 node->handle = phandle;
372 list_add(&node->node, &device_list);
373
374 status = acpi_get_parent(phandle, &phandle);
375 if (ACPI_FAILURE(status))
376 goto out;
377 }
378
379 root = acpi_pci_find_root(phandle);
380 if (!root)
381 goto out;
382
383 pbus = root->bus;
384
385 /*
386 * Now, walk back down the PCI device tree until we return to our
387 * original handle. Assumes that everything between the PCI root
388 * bridge and the device we're looking for must be a P2P bridge.
389 */
390 list_for_each_entry(node, &device_list, node) {
391 acpi_handle hnd = node->handle;
392 status = acpi_evaluate_integer(hnd, "_ADR", NULL, &adr);
393 if (ACPI_FAILURE(status))
394 goto out;
395 dev = (adr >> 16) & 0xffff;
396 fn = adr & 0xffff;
397
398 pdev = pci_get_slot(pbus, PCI_DEVFN(dev, fn));
399 if (hnd == handle)
400 break;
401
402 pbus = pdev->subordinate;
403 pci_dev_put(pdev);
404 }
405 out:
406 list_for_each_entry_safe(node, tmp, &device_list, node)
407 kfree(node);
408
409 return pdev;
410 }
411 EXPORT_SYMBOL_GPL(acpi_get_pci_dev);
412
413 /**
414 * acpi_pci_osc_control_set - commit requested control to Firmware
415 * @handle: acpi_handle for the target ACPI object
416 * @flags: driver's requested control bits
417 *
418 * Attempt to take control from Firmware on requested control bits.
419 **/
420 acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 flags)
421 {
422 acpi_status status;
423 u32 control_req, result, capbuf[3];
424 acpi_handle tmp;
425 struct acpi_pci_root *root;
426
427 status = acpi_get_handle(handle, "_OSC", &tmp);
428 if (ACPI_FAILURE(status))
429 return status;
430
431 control_req = (flags & OSC_CONTROL_MASKS);
432 if (!control_req)
433 return AE_TYPE;
434
435 root = acpi_pci_find_root(handle);
436 if (!root)
437 return AE_NOT_EXIST;
438
439 mutex_lock(&osc_lock);
440 /* No need to evaluate _OSC if the control was already granted. */
441 if ((root->osc_control_set & control_req) == control_req)
442 goto out;
443
444 /* Need to query controls first before requesting them */
445 if (!root->osc_queried) {
446 status = acpi_pci_query_osc(root, root->osc_support_set);
447 if (ACPI_FAILURE(status))
448 goto out;
449 }
450 if ((root->osc_control_qry & control_req) != control_req) {
451 printk(KERN_DEBUG
452 "Firmware did not grant requested _OSC control\n");
453 status = AE_SUPPORT;
454 goto out;
455 }
456
457 capbuf[OSC_QUERY_TYPE] = 0;
458 capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
459 capbuf[OSC_CONTROL_TYPE] = root->osc_control_set | control_req;
460 status = acpi_pci_run_osc(handle, capbuf, &result);
461 if (ACPI_SUCCESS(status))
462 root->osc_control_set = result;
463 out:
464 mutex_unlock(&osc_lock);
465 return status;
466 }
467 EXPORT_SYMBOL(acpi_pci_osc_control_set);
468
469 static int __devinit acpi_pci_root_add(struct acpi_device *device)
470 {
471 int result = 0;
472 struct acpi_pci_root *root = NULL;
473 struct acpi_pci_root *tmp;
474 acpi_status status = AE_OK;
475 unsigned long long value = 0;
476 acpi_handle handle = NULL;
477 struct acpi_device *child;
478 u32 flags, base_flags;
479
480
481 if (!device)
482 return -EINVAL;
483
484 root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
485 if (!root)
486 return -ENOMEM;
487 INIT_LIST_HEAD(&root->node);
488
489 root->device = device;
490 strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
491 strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
492 device->driver_data = root;
493
494 /*
495 * All supported architectures that use ACPI have support for
496 * PCI domains, so we indicate this in _OSC support capabilities.
497 */
498 flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
499 acpi_pci_osc_support(root, flags);
500
501 /*
502 * Segment
503 * -------
504 * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
505 */
506 status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
507 &value);
508 switch (status) {
509 case AE_OK:
510 root->id.segment = (u16) value;
511 break;
512 case AE_NOT_FOUND:
513 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
514 "Assuming segment 0 (no _SEG)\n"));
515 root->id.segment = 0;
516 break;
517 default:
518 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
519 result = -ENODEV;
520 goto end;
521 }
522
523 /*
524 * Bus
525 * ---
526 * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
527 */
528 status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
529 &value);
530 switch (status) {
531 case AE_OK:
532 root->id.bus = (u16) value;
533 break;
534 case AE_NOT_FOUND:
535 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
536 root->id.bus = 0;
537 break;
538 default:
539 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
540 result = -ENODEV;
541 goto end;
542 }
543
544 /* Some systems have wrong _BBN */
545 list_for_each_entry(tmp, &acpi_pci_roots, node) {
546 if ((tmp->id.segment == root->id.segment)
547 && (tmp->id.bus == root->id.bus)) {
548 int bus = 0;
549 acpi_status status;
550
551 printk(KERN_ERR PREFIX
552 "Wrong _BBN value, reboot"
553 " and use option 'pci=noacpi'\n");
554
555 status = try_get_root_bridge_busnr(device->handle, &bus);
556 if (ACPI_FAILURE(status))
557 break;
558 if (bus != root->id.bus) {
559 printk(KERN_INFO PREFIX
560 "PCI _CRS %d overrides _BBN 0\n", bus);
561 root->id.bus = bus;
562 }
563 break;
564 }
565 }
566 /*
567 * Device & Function
568 * -----------------
569 * Obtained from _ADR (which has already been evaluated for us).
570 */
571 root->id.device = device->pnp.bus_address >> 16;
572 root->id.function = device->pnp.bus_address & 0xFFFF;
573
574 /*
575 * TBD: Need PCI interface for enumeration/configuration of roots.
576 */
577
578 /* TBD: Locking */
579 list_add_tail(&root->node, &acpi_pci_roots);
580
581 printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
582 acpi_device_name(device), acpi_device_bid(device),
583 root->id.segment, root->id.bus);
584
585 /*
586 * Scan the Root Bridge
587 * --------------------
588 * Must do this prior to any attempt to bind the root device, as the
589 * PCI namespace does not get created until this call is made (and
590 * thus the root bridge's pci_dev does not exist).
591 */
592 root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
593 if (!root->bus) {
594 printk(KERN_ERR PREFIX
595 "Bus %04x:%02x not present in PCI namespace\n",
596 root->id.segment, root->id.bus);
597 result = -ENODEV;
598 goto end;
599 }
600
601 /*
602 * Attach ACPI-PCI Context
603 * -----------------------
604 * Thus binding the ACPI and PCI devices.
605 */
606 result = acpi_pci_bind_root(device, &root->id, root->bus);
607 if (result)
608 goto end;
609
610 /*
611 * PCI Routing Table
612 * -----------------
613 * Evaluate and parse _PRT, if exists.
614 */
615 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
616 if (ACPI_SUCCESS(status))
617 result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
618 root->id.bus);
619
620 /*
621 * Scan and bind all _ADR-Based Devices
622 */
623 list_for_each_entry(child, &device->children, node)
624 acpi_pci_bridge_scan(child);
625
626 /* Indicate support for various _OSC capabilities. */
627 if (pci_ext_cfg_avail(root->bus->self))
628 flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
629 if (pcie_aspm_enabled())
630 flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
631 OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
632 if (pci_msi_enabled())
633 flags |= OSC_MSI_SUPPORT;
634 if (flags != base_flags)
635 acpi_pci_osc_support(root, flags);
636
637 end:
638 if (result) {
639 if (!list_empty(&root->node))
640 list_del(&root->node);
641 kfree(root);
642 }
643
644 return result;
645 }
646
647 static int acpi_pci_root_start(struct acpi_device *device)
648 {
649 struct acpi_pci_root *root;
650
651
652 list_for_each_entry(root, &acpi_pci_roots, node) {
653 if (root->device == device) {
654 pci_bus_add_devices(root->bus);
655 return 0;
656 }
657 }
658 return -ENODEV;
659 }
660
661 static int acpi_pci_root_remove(struct acpi_device *device, int type)
662 {
663 struct acpi_pci_root *root = NULL;
664
665
666 if (!device || !acpi_driver_data(device))
667 return -EINVAL;
668
669 root = acpi_driver_data(device);
670
671 kfree(root);
672
673 return 0;
674 }
675
676 static int __init acpi_pci_root_init(void)
677 {
678 if (acpi_pci_disabled)
679 return 0;
680
681 if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
682 return -ENODEV;
683
684 return 0;
685 }
686
687 subsys_initcall(acpi_pci_root_init);
This page took 0.059902 seconds and 5 git commands to generate.