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/pci-acpi.h>
34 #include <acpi/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/actypes.h>
38 #define MY_NAME "acpi_pcihp"
40 #define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
41 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
42 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
43 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
45 #define METHOD_NAME__SUN "_SUN"
46 #define METHOD_NAME__HPP "_HPP"
47 #define METHOD_NAME_OSHP "OSHP"
49 static int debug_acpi
;
52 decode_type0_hpx_record(union acpi_object
*record
, struct hotplug_params
*hpx
)
55 union acpi_object
*fields
= record
->package
.elements
;
56 u32 revision
= fields
[1].integer
.value
;
60 if (record
->package
.count
!= 6)
62 for (i
= 2; i
< 6; i
++)
63 if (fields
[i
].type
!= ACPI_TYPE_INTEGER
)
65 hpx
->t0
= &hpx
->type0_data
;
66 hpx
->t0
->revision
= revision
;
67 hpx
->t0
->cache_line_size
= fields
[2].integer
.value
;
68 hpx
->t0
->latency_timer
= fields
[3].integer
.value
;
69 hpx
->t0
->enable_serr
= fields
[4].integer
.value
;
70 hpx
->t0
->enable_perr
= fields
[5].integer
.value
;
74 "%s: Type 0 Revision %d record not supported\n",
82 decode_type1_hpx_record(union acpi_object
*record
, struct hotplug_params
*hpx
)
85 union acpi_object
*fields
= record
->package
.elements
;
86 u32 revision
= fields
[1].integer
.value
;
90 if (record
->package
.count
!= 5)
92 for (i
= 2; i
< 5; i
++)
93 if (fields
[i
].type
!= ACPI_TYPE_INTEGER
)
95 hpx
->t1
= &hpx
->type1_data
;
96 hpx
->t1
->revision
= revision
;
97 hpx
->t1
->max_mem_read
= fields
[2].integer
.value
;
98 hpx
->t1
->avg_max_split
= fields
[3].integer
.value
;
99 hpx
->t1
->tot_max_split
= fields
[4].integer
.value
;
103 "%s: Type 1 Revision %d record not supported\n",
111 decode_type2_hpx_record(union acpi_object
*record
, struct hotplug_params
*hpx
)
114 union acpi_object
*fields
= record
->package
.elements
;
115 u32 revision
= fields
[1].integer
.value
;
119 if (record
->package
.count
!= 18)
121 for (i
= 2; i
< 18; i
++)
122 if (fields
[i
].type
!= ACPI_TYPE_INTEGER
)
124 hpx
->t2
= &hpx
->type2_data
;
125 hpx
->t2
->revision
= revision
;
126 hpx
->t2
->unc_err_mask_and
= fields
[2].integer
.value
;
127 hpx
->t2
->unc_err_mask_or
= fields
[3].integer
.value
;
128 hpx
->t2
->unc_err_sever_and
= fields
[4].integer
.value
;
129 hpx
->t2
->unc_err_sever_or
= fields
[5].integer
.value
;
130 hpx
->t2
->cor_err_mask_and
= fields
[6].integer
.value
;
131 hpx
->t2
->cor_err_mask_or
= fields
[7].integer
.value
;
132 hpx
->t2
->adv_err_cap_and
= fields
[8].integer
.value
;
133 hpx
->t2
->adv_err_cap_or
= fields
[9].integer
.value
;
134 hpx
->t2
->pci_exp_devctl_and
= fields
[10].integer
.value
;
135 hpx
->t2
->pci_exp_devctl_or
= fields
[11].integer
.value
;
136 hpx
->t2
->pci_exp_lnkctl_and
= fields
[12].integer
.value
;
137 hpx
->t2
->pci_exp_lnkctl_or
= fields
[13].integer
.value
;
138 hpx
->t2
->sec_unc_err_sever_and
= fields
[14].integer
.value
;
139 hpx
->t2
->sec_unc_err_sever_or
= fields
[15].integer
.value
;
140 hpx
->t2
->sec_unc_err_mask_and
= fields
[16].integer
.value
;
141 hpx
->t2
->sec_unc_err_mask_or
= fields
[17].integer
.value
;
145 "%s: Type 2 Revision %d record not supported\n",
153 acpi_run_hpx(acpi_handle handle
, struct hotplug_params
*hpx
)
156 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
157 union acpi_object
*package
, *record
, *fields
;
161 /* Clear the return buffer with zeros */
162 memset(hpx
, 0, sizeof(struct hotplug_params
));
164 status
= acpi_evaluate_object(handle
, "_HPX", NULL
, &buffer
);
165 if (ACPI_FAILURE(status
))
168 package
= (union acpi_object
*)buffer
.pointer
;
169 if (package
->type
!= ACPI_TYPE_PACKAGE
) {
174 for (i
= 0; i
< package
->package
.count
; i
++) {
175 record
= &package
->package
.elements
[i
];
176 if (record
->type
!= ACPI_TYPE_PACKAGE
) {
181 fields
= record
->package
.elements
;
182 if (fields
[0].type
!= ACPI_TYPE_INTEGER
||
183 fields
[1].type
!= ACPI_TYPE_INTEGER
) {
188 type
= fields
[0].integer
.value
;
191 status
= decode_type0_hpx_record(record
, hpx
);
192 if (ACPI_FAILURE(status
))
196 status
= decode_type1_hpx_record(record
, hpx
);
197 if (ACPI_FAILURE(status
))
201 status
= decode_type2_hpx_record(record
, hpx
);
202 if (ACPI_FAILURE(status
))
206 printk(KERN_ERR
"%s: Type %d record not supported\n",
213 kfree(buffer
.pointer
);
218 acpi_run_hpp(acpi_handle handle
, struct hotplug_params
*hpp
)
222 struct acpi_buffer ret_buf
= { 0, NULL
};
223 struct acpi_buffer string
= { ACPI_ALLOCATE_BUFFER
, NULL
};
224 union acpi_object
*ext_obj
, *package
;
227 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &string
);
229 /* Clear the return buffer with zeros */
230 memset(hpp
, 0, sizeof(struct hotplug_params
));
233 status
= acpi_evaluate_object(handle
, METHOD_NAME__HPP
, NULL
, &ret_buf
);
235 case AE_BUFFER_OVERFLOW
:
236 ret_buf
.pointer
= kmalloc (ret_buf
.length
, GFP_KERNEL
);
237 if (!ret_buf
.pointer
) {
238 printk(KERN_ERR
"%s:%s alloc for _HPP fail\n",
239 __func__
, (char *)string
.pointer
);
240 kfree(string
.pointer
);
243 status
= acpi_evaluate_object(handle
, METHOD_NAME__HPP
,
245 if (ACPI_SUCCESS(status
))
248 if (ACPI_FAILURE(status
)) {
249 pr_debug("%s:%s _HPP fail=0x%x\n", __func__
,
250 (char *)string
.pointer
, status
);
251 kfree(string
.pointer
);
256 ext_obj
= (union acpi_object
*) ret_buf
.pointer
;
257 if (ext_obj
->type
!= ACPI_TYPE_PACKAGE
) {
258 printk(KERN_ERR
"%s:%s _HPP obj not a package\n", __func__
,
259 (char *)string
.pointer
);
261 goto free_and_return
;
264 len
= ext_obj
->package
.count
;
265 package
= (union acpi_object
*) ret_buf
.pointer
;
266 for ( i
= 0; (i
< len
) || (i
< 4); i
++) {
267 ext_obj
= (union acpi_object
*) &package
->package
.elements
[i
];
268 switch (ext_obj
->type
) {
269 case ACPI_TYPE_INTEGER
:
270 nui
[i
] = (u8
)ext_obj
->integer
.value
;
273 printk(KERN_ERR
"%s:%s _HPP obj type incorrect\n",
274 __func__
, (char *)string
.pointer
);
276 goto free_and_return
;
280 hpp
->t0
= &hpp
->type0_data
;
281 hpp
->t0
->cache_line_size
= nui
[0];
282 hpp
->t0
->latency_timer
= nui
[1];
283 hpp
->t0
->enable_serr
= nui
[2];
284 hpp
->t0
->enable_perr
= nui
[3];
286 pr_debug(" _HPP: cache_line_size=0x%x\n", hpp
->t0
->cache_line_size
);
287 pr_debug(" _HPP: latency timer =0x%x\n", hpp
->t0
->latency_timer
);
288 pr_debug(" _HPP: enable SERR =0x%x\n", hpp
->t0
->enable_serr
);
289 pr_debug(" _HPP: enable PERR =0x%x\n", hpp
->t0
->enable_perr
);
292 kfree(string
.pointer
);
293 kfree(ret_buf
.pointer
);
299 /* acpi_run_oshp - get control of hotplug from the firmware
301 * @handle - the handle of the hotplug controller.
303 static acpi_status
acpi_run_oshp(acpi_handle handle
)
306 struct acpi_buffer string
= { ACPI_ALLOCATE_BUFFER
, NULL
};
308 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &string
);
311 status
= acpi_evaluate_object(handle
, METHOD_NAME_OSHP
, NULL
, NULL
);
312 if (ACPI_FAILURE(status
))
313 if (status
!= AE_NOT_FOUND
)
314 printk(KERN_ERR
"%s:%s OSHP fails=0x%x\n",
315 __func__
, (char *)string
.pointer
, status
);
317 dbg("%s:%s OSHP not found\n",
318 __func__
, (char *)string
.pointer
);
320 pr_debug("%s:%s OSHP passes\n", __func__
,
321 (char *)string
.pointer
);
323 kfree(string
.pointer
);
327 /* acpi_get_hp_params_from_firmware
329 * @bus - the pci_bus of the bus on which the device is newly added
330 * @hpp - allocated by the caller
332 acpi_status
acpi_get_hp_params_from_firmware(struct pci_bus
*bus
,
333 struct hotplug_params
*hpp
)
335 acpi_status status
= AE_NOT_FOUND
;
336 acpi_handle handle
, phandle
;
337 struct pci_bus
*pbus
= bus
;
338 struct pci_dev
*pdev
;
343 handle
= acpi_get_pci_rootbridge_handle(
344 pci_domain_nr(pbus
), pbus
->number
);
347 handle
= DEVICE_ACPI_HANDLE(&(pdev
->dev
));
352 * _HPP settings apply to all child buses, until another _HPP is
353 * encountered. If we don't find an _HPP for the input pci dev,
354 * look for it in the parent device scope since that would apply to
355 * this pci dev. If we don't find any _HPP, use hardcoded defaults
358 status
= acpi_run_hpx(handle
, hpp
);
359 if (ACPI_SUCCESS(status
))
361 status
= acpi_run_hpp(handle
, hpp
);
362 if (ACPI_SUCCESS(status
))
364 if (acpi_root_bridge(handle
))
366 status
= acpi_get_parent(handle
, &phandle
);
367 if (ACPI_FAILURE(status
))
373 EXPORT_SYMBOL_GPL(acpi_get_hp_params_from_firmware
);
376 * acpi_get_hp_hw_control_from_firmware
377 * @dev: the pci_dev of the bridge that has a hotplug controller
378 * @flags: requested control bits for _OSC
380 * Attempt to take hotplug control from firmware.
382 int acpi_get_hp_hw_control_from_firmware(struct pci_dev
*dev
, u32 flags
)
385 acpi_handle chandle
, handle
;
386 struct pci_dev
*pdev
= dev
;
387 struct pci_bus
*parent
;
388 struct acpi_buffer string
= { ACPI_ALLOCATE_BUFFER
, NULL
};
390 flags
&= (OSC_PCI_EXPRESS_NATIVE_HP_CONTROL
|
391 OSC_SHPC_NATIVE_HP_CONTROL
|
392 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL
);
394 err("Invalid flags %u specified!\n", flags
);
399 * Per PCI firmware specification, we should run the ACPI _OSC
400 * method to get control of hotplug hardware before using it. If
401 * an _OSC is missing, we look for an OSHP to do the same thing.
402 * To handle different BIOS behavior, we look for _OSC on a root
403 * bridge preferentially (according to PCI fw spec). Later for
404 * OSHP within the scope of the hotplug controller and its parents,
405 * upto the host bridge under which this controller exists.
407 handle
= acpi_find_root_bridge_handle(pdev
);
409 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &string
);
410 dbg("Trying to get hotplug control for %s\n",
411 (char *)string
.pointer
);
412 status
= pci_osc_control_set(handle
, flags
);
413 if (ACPI_SUCCESS(status
))
415 kfree(string
.pointer
);
416 string
= (struct acpi_buffer
){ ACPI_ALLOCATE_BUFFER
, NULL
};
420 handle
= DEVICE_ACPI_HANDLE(&dev
->dev
);
423 * This hotplug controller was not listed in the ACPI name
424 * space at all. Try to get acpi handle of parent pci bus.
426 if (!pdev
|| !pdev
->bus
->parent
)
428 parent
= pdev
->bus
->parent
;
429 dbg("Could not find %s in acpi namespace, trying parent\n",
432 /* Parent must be a host bridge */
433 handle
= acpi_get_pci_rootbridge_handle(
434 pci_domain_nr(parent
),
437 handle
= DEVICE_ACPI_HANDLE(&(parent
->self
->dev
));
442 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &string
);
443 dbg("Trying to get hotplug control for %s \n",
444 (char *)string
.pointer
);
445 status
= acpi_run_oshp(handle
);
446 if (ACPI_SUCCESS(status
))
448 if (acpi_root_bridge(handle
))
451 status
= acpi_get_parent(chandle
, &handle
);
452 if (ACPI_FAILURE(status
))
456 dbg("Cannot get control of hotplug hardware for pci %s\n",
459 kfree(string
.pointer
);
462 dbg("Gained control for hotplug HW for pci %s (%s)\n", pci_name(dev
),
463 (char *)string
.pointer
);
464 kfree(string
.pointer
);
467 EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware
);
469 /* acpi_root_bridge - check to see if this acpi object is a root bridge
471 * @handle - the acpi object in question.
473 int acpi_root_bridge(acpi_handle handle
)
476 struct acpi_device_info
*info
;
477 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
480 status
= acpi_get_object_info(handle
, &buffer
);
481 if (ACPI_SUCCESS(status
)) {
482 info
= buffer
.pointer
;
483 if ((info
->valid
& ACPI_VALID_HID
) &&
484 !strcmp(PCI_ROOT_HID_STRING
,
485 info
->hardware_id
.value
)) {
486 kfree(buffer
.pointer
);
489 if (info
->valid
& ACPI_VALID_CID
) {
490 for (i
=0; i
< info
->compatibility_id
.count
; i
++) {
491 if (!strcmp(PCI_ROOT_HID_STRING
,
492 info
->compatibility_id
.id
[i
].value
)) {
493 kfree(buffer
.pointer
);
498 kfree(buffer
.pointer
);
502 EXPORT_SYMBOL_GPL(acpi_root_bridge
);
504 module_param(debug_acpi
, bool, 0644);
505 MODULE_PARM_DESC(debug_acpi
, "Debugging mode for ACPI enabled or not");