2 * Common ACPI functions for hot plug platforms
4 * Copyright (C) 2006 Intel Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16 * NON INFRINGEMENT. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Send feedback to <kristen.c.accardi@intel.com>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/kernel.h>
30 #include <linux/types.h>
31 #include <linux/pci.h>
32 #include <linux/pci_hotplug.h>
33 #include <linux/acpi.h>
34 #include <linux/pci-acpi.h>
36 #define MY_NAME "acpi_pcihp"
38 #define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
39 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
40 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
41 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
43 #define METHOD_NAME__SUN "_SUN"
44 #define METHOD_NAME__HPP "_HPP"
45 #define METHOD_NAME_OSHP "OSHP"
47 static int debug_acpi;
50 decode_type0_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
53 union acpi_object *fields = record->package.elements;
54 u32 revision = fields[1].integer.value;
58 if (record->package.count != 6)
60 for (i = 2; i < 6; i++)
61 if (fields[i].type != ACPI_TYPE_INTEGER)
63 hpx->t0 = &hpx->type0_data;
64 hpx->t0->revision = revision;
65 hpx->t0->cache_line_size = fields[2].integer.value;
66 hpx->t0->latency_timer = fields[3].integer.value;
67 hpx->t0->enable_serr = fields[4].integer.value;
68 hpx->t0->enable_perr = fields[5].integer.value;
72 "%s: Type 0 Revision %d record not supported\n",
80 decode_type1_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
83 union acpi_object *fields = record->package.elements;
84 u32 revision = fields[1].integer.value;
88 if (record->package.count != 5)
90 for (i = 2; i < 5; i++)
91 if (fields[i].type != ACPI_TYPE_INTEGER)
93 hpx->t1 = &hpx->type1_data;
94 hpx->t1->revision = revision;
95 hpx->t1->max_mem_read = fields[2].integer.value;
96 hpx->t1->avg_max_split = fields[3].integer.value;
97 hpx->t1->tot_max_split = fields[4].integer.value;
101 "%s: Type 1 Revision %d record not supported\n",
109 decode_type2_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
112 union acpi_object *fields = record->package.elements;
113 u32 revision = fields[1].integer.value;
117 if (record->package.count != 18)
119 for (i = 2; i < 18; i++)
120 if (fields[i].type != ACPI_TYPE_INTEGER)
122 hpx->t2 = &hpx->type2_data;
123 hpx->t2->revision = revision;
124 hpx->t2->unc_err_mask_and = fields[2].integer.value;
125 hpx->t2->unc_err_mask_or = fields[3].integer.value;
126 hpx->t2->unc_err_sever_and = fields[4].integer.value;
127 hpx->t2->unc_err_sever_or = fields[5].integer.value;
128 hpx->t2->cor_err_mask_and = fields[6].integer.value;
129 hpx->t2->cor_err_mask_or = fields[7].integer.value;
130 hpx->t2->adv_err_cap_and = fields[8].integer.value;
131 hpx->t2->adv_err_cap_or = fields[9].integer.value;
132 hpx->t2->pci_exp_devctl_and = fields[10].integer.value;
133 hpx->t2->pci_exp_devctl_or = fields[11].integer.value;
134 hpx->t2->pci_exp_lnkctl_and = fields[12].integer.value;
135 hpx->t2->pci_exp_lnkctl_or = fields[13].integer.value;
136 hpx->t2->sec_unc_err_sever_and = fields[14].integer.value;
137 hpx->t2->sec_unc_err_sever_or = fields[15].integer.value;
138 hpx->t2->sec_unc_err_mask_and = fields[16].integer.value;
139 hpx->t2->sec_unc_err_mask_or = fields[17].integer.value;
143 "%s: Type 2 Revision %d record not supported\n",
151 acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx)
154 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
155 union acpi_object *package, *record, *fields;
159 /* Clear the return buffer with zeros */
160 memset(hpx, 0, sizeof(struct hotplug_params));
162 status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
163 if (ACPI_FAILURE(status))
166 package = (union acpi_object *)buffer.pointer;
167 if (package->type != ACPI_TYPE_PACKAGE) {
172 for (i = 0; i < package->package.count; i++) {
173 record = &package->package.elements[i];
174 if (record->type != ACPI_TYPE_PACKAGE) {
179 fields = record->package.elements;
180 if (fields[0].type != ACPI_TYPE_INTEGER ||
181 fields[1].type != ACPI_TYPE_INTEGER) {
186 type = fields[0].integer.value;
189 status = decode_type0_hpx_record(record, hpx);
190 if (ACPI_FAILURE(status))
194 status = decode_type1_hpx_record(record, hpx);
195 if (ACPI_FAILURE(status))
199 status = decode_type2_hpx_record(record, hpx);
200 if (ACPI_FAILURE(status))
204 printk(KERN_ERR "%s: Type %d record not supported\n",
211 kfree(buffer.pointer);
216 acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp)
220 struct acpi_buffer ret_buf = { 0, NULL};
221 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
222 union acpi_object *ext_obj, *package;
225 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
227 /* Clear the return buffer with zeros */
228 memset(hpp, 0, sizeof(struct hotplug_params));
231 status = acpi_evaluate_object(handle, METHOD_NAME__HPP, NULL, &ret_buf);
233 case AE_BUFFER_OVERFLOW:
234 ret_buf.pointer = kmalloc (ret_buf.length, GFP_KERNEL);
235 if (!ret_buf.pointer) {
236 printk(KERN_ERR "%s:%s alloc for _HPP fail\n",
237 __func__, (char *)string.pointer);
238 kfree(string.pointer);
241 status = acpi_evaluate_object(handle, METHOD_NAME__HPP,
243 if (ACPI_SUCCESS(status))
246 if (ACPI_FAILURE(status)) {
247 pr_debug("%s:%s _HPP fail=0x%x\n", __func__,
248 (char *)string.pointer, status);
249 kfree(string.pointer);
254 ext_obj = (union acpi_object *) ret_buf.pointer;
255 if (ext_obj->type != ACPI_TYPE_PACKAGE) {
256 printk(KERN_ERR "%s:%s _HPP obj not a package\n", __func__,
257 (char *)string.pointer);
259 goto free_and_return;
262 len = ext_obj->package.count;
263 package = (union acpi_object *) ret_buf.pointer;
264 for ( i = 0; (i < len) || (i < 4); i++) {
265 ext_obj = (union acpi_object *) &package->package.elements[i];
266 switch (ext_obj->type) {
267 case ACPI_TYPE_INTEGER:
268 nui[i] = (u8)ext_obj->integer.value;
271 printk(KERN_ERR "%s:%s _HPP obj type incorrect\n",
272 __func__, (char *)string.pointer);
274 goto free_and_return;
278 hpp->t0 = &hpp->type0_data;
279 hpp->t0->cache_line_size = nui[0];
280 hpp->t0->latency_timer = nui[1];
281 hpp->t0->enable_serr = nui[2];
282 hpp->t0->enable_perr = nui[3];
284 pr_debug(" _HPP: cache_line_size=0x%x\n", hpp->t0->cache_line_size);
285 pr_debug(" _HPP: latency timer =0x%x\n", hpp->t0->latency_timer);
286 pr_debug(" _HPP: enable SERR =0x%x\n", hpp->t0->enable_serr);
287 pr_debug(" _HPP: enable PERR =0x%x\n", hpp->t0->enable_perr);
290 kfree(string.pointer);
291 kfree(ret_buf.pointer);
297 /* acpi_run_oshp - get control of hotplug from the firmware
299 * @handle - the handle of the hotplug controller.
301 static acpi_status acpi_run_oshp(acpi_handle handle)
304 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
306 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
309 status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
310 if (ACPI_FAILURE(status))
311 if (status != AE_NOT_FOUND)
312 printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
313 __func__, (char *)string.pointer, status);
315 dbg("%s:%s OSHP not found\n",
316 __func__, (char *)string.pointer);
318 pr_debug("%s:%s OSHP passes\n", __func__,
319 (char *)string.pointer);
321 kfree(string.pointer);
325 /* acpi_get_hp_params_from_firmware
327 * @bus - the pci_bus of the bus on which the device is newly added
328 * @hpp - allocated by the caller
330 acpi_status acpi_get_hp_params_from_firmware(struct pci_bus *bus,
331 struct hotplug_params *hpp)
333 acpi_status status = AE_NOT_FOUND;
334 acpi_handle handle, phandle;
335 struct pci_bus *pbus = bus;
336 struct pci_dev *pdev;
341 handle = acpi_get_pci_rootbridge_handle(
342 pci_domain_nr(pbus), pbus->number);
345 handle = DEVICE_ACPI_HANDLE(&(pdev->dev));
350 * _HPP settings apply to all child buses, until another _HPP is
351 * encountered. If we don't find an _HPP for the input pci dev,
352 * look for it in the parent device scope since that would apply to
353 * this pci dev. If we don't find any _HPP, use hardcoded defaults
356 status = acpi_run_hpx(handle, hpp);
357 if (ACPI_SUCCESS(status))
359 status = acpi_run_hpp(handle, hpp);
360 if (ACPI_SUCCESS(status))
362 if (acpi_root_bridge(handle))
364 status = acpi_get_parent(handle, &phandle);
365 if (ACPI_FAILURE(status))
371 EXPORT_SYMBOL_GPL(acpi_get_hp_params_from_firmware);
374 * acpi_get_hp_hw_control_from_firmware
375 * @dev: the pci_dev of the bridge that has a hotplug controller
376 * @flags: requested control bits for _OSC
378 * Attempt to take hotplug control from firmware.
380 int acpi_get_hp_hw_control_from_firmware(struct pci_dev *dev, u32 flags)
383 acpi_handle chandle, handle;
384 struct pci_dev *pdev = dev;
385 struct pci_bus *parent;
386 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
388 flags &= (OSC_PCI_EXPRESS_NATIVE_HP_CONTROL |
389 OSC_SHPC_NATIVE_HP_CONTROL |
390 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
392 err("Invalid flags %u specified!\n", flags);
397 * Per PCI firmware specification, we should run the ACPI _OSC
398 * method to get control of hotplug hardware before using it. If
399 * an _OSC is missing, we look for an OSHP to do the same thing.
400 * To handle different BIOS behavior, we look for _OSC on a root
401 * bridge preferentially (according to PCI fw spec). Later for
402 * OSHP within the scope of the hotplug controller and its parents,
403 * upto the host bridge under which this controller exists.
405 handle = acpi_find_root_bridge_handle(pdev);
407 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
408 dbg("Trying to get hotplug control for %s\n",
409 (char *)string.pointer);
410 status = acpi_pci_osc_control_set(handle, flags);
411 if (ACPI_SUCCESS(status))
413 kfree(string.pointer);
414 string = (struct acpi_buffer){ ACPI_ALLOCATE_BUFFER, NULL };
418 handle = DEVICE_ACPI_HANDLE(&dev->dev);
421 * This hotplug controller was not listed in the ACPI name
422 * space at all. Try to get acpi handle of parent pci bus.
424 if (!pdev || !pdev->bus->parent)
426 parent = pdev->bus->parent;
427 dbg("Could not find %s in acpi namespace, trying parent\n",
430 /* Parent must be a host bridge */
431 handle = acpi_get_pci_rootbridge_handle(
432 pci_domain_nr(parent),
435 handle = DEVICE_ACPI_HANDLE(&(parent->self->dev));
440 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
441 dbg("Trying to get hotplug control for %s \n",
442 (char *)string.pointer);
443 status = acpi_run_oshp(handle);
444 if (ACPI_SUCCESS(status))
446 if (acpi_root_bridge(handle))
449 status = acpi_get_parent(chandle, &handle);
450 if (ACPI_FAILURE(status))
454 dbg("Cannot get control of hotplug hardware for pci %s\n",
457 kfree(string.pointer);
460 dbg("Gained control for hotplug HW for pci %s (%s)\n", pci_name(dev),
461 (char *)string.pointer);
462 kfree(string.pointer);
465 EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
467 /* acpi_root_bridge - check to see if this acpi object is a root bridge
469 * @handle - the acpi object in question.
471 int acpi_root_bridge(acpi_handle handle)
474 struct acpi_device_info *info;
475 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
478 status = acpi_get_object_info(handle, &buffer);
479 if (ACPI_SUCCESS(status)) {
480 info = buffer.pointer;
481 if ((info->valid & ACPI_VALID_HID) &&
482 !strcmp(PCI_ROOT_HID_STRING,
483 info->hardware_id.value)) {
484 kfree(buffer.pointer);
487 if (info->valid & ACPI_VALID_CID) {
488 for (i=0; i < info->compatibility_id.count; i++) {
489 if (!strcmp(PCI_ROOT_HID_STRING,
490 info->compatibility_id.id[i].value)) {
491 kfree(buffer.pointer);
496 kfree(buffer.pointer);
500 EXPORT_SYMBOL_GPL(acpi_root_bridge);
503 static int is_ejectable(acpi_handle handle)
507 unsigned long long removable;
508 status = acpi_get_handle(handle, "_ADR", &tmp);
509 if (ACPI_FAILURE(status))
511 status = acpi_get_handle(handle, "_EJ0", &tmp);
512 if (ACPI_SUCCESS(status))
514 status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
515 if (ACPI_SUCCESS(status) && removable)
521 * acpi_pcihp_check_ejectable - check if handle is ejectable ACPI PCI slot
522 * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
523 * @handle: ACPI handle to check
525 * Return 1 if handle is ejectable PCI slot, 0 otherwise.
527 int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
529 acpi_handle bridge_handle, parent_handle;
531 if (!(bridge_handle = acpi_pci_get_bridge_handle(pbus)))
533 if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
535 if (bridge_handle != parent_handle)
537 return is_ejectable(handle);
539 EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);
542 check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
544 int *found = (int *)context;
545 if (is_ejectable(handle)) {
547 return AE_CTRL_TERMINATE;
553 * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
554 * @pbus - PCI bus to scan
556 * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
558 int acpi_pci_detect_ejectable(struct pci_bus *pbus)
563 if (!(handle = acpi_pci_get_bridge_handle(pbus)))
565 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
566 check_hotplug, (void *)&found, NULL);
569 EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);
571 module_param(debug_acpi, bool, 0644);
572 MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");