Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / pci / hotplug / acpi_pcihp.c
1 /*
2 * Common ACPI functions for hot plug platforms
3 *
4 * Copyright (C) 2006 Intel Corporation
5 *
6 * All rights reserved.
7 *
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.
12 *
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
17 * details.
18 *
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.
22 *
23 * Send feedback to <kristen.c.accardi@intel.com>
24 *
25 */
26
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>
35 #include <linux/slab.h>
36
37 #define MY_NAME "acpi_pcihp"
38
39 #define dbg(fmt, arg...) do { if (debug_acpi) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __func__ , ## arg); } while (0)
40 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
41 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
42 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
43
44 #define METHOD_NAME__SUN "_SUN"
45 #define METHOD_NAME_OSHP "OSHP"
46
47 static int debug_acpi;
48
49 static acpi_status
50 decode_type0_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
51 {
52 int i;
53 union acpi_object *fields = record->package.elements;
54 u32 revision = fields[1].integer.value;
55
56 switch (revision) {
57 case 1:
58 if (record->package.count != 6)
59 return AE_ERROR;
60 for (i = 2; i < 6; i++)
61 if (fields[i].type != ACPI_TYPE_INTEGER)
62 return AE_ERROR;
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;
69 break;
70 default:
71 printk(KERN_WARNING
72 "%s: Type 0 Revision %d record not supported\n",
73 __func__, revision);
74 return AE_ERROR;
75 }
76 return AE_OK;
77 }
78
79 static acpi_status
80 decode_type1_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
81 {
82 int i;
83 union acpi_object *fields = record->package.elements;
84 u32 revision = fields[1].integer.value;
85
86 switch (revision) {
87 case 1:
88 if (record->package.count != 5)
89 return AE_ERROR;
90 for (i = 2; i < 5; i++)
91 if (fields[i].type != ACPI_TYPE_INTEGER)
92 return AE_ERROR;
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;
98 break;
99 default:
100 printk(KERN_WARNING
101 "%s: Type 1 Revision %d record not supported\n",
102 __func__, revision);
103 return AE_ERROR;
104 }
105 return AE_OK;
106 }
107
108 static acpi_status
109 decode_type2_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
110 {
111 int i;
112 union acpi_object *fields = record->package.elements;
113 u32 revision = fields[1].integer.value;
114
115 switch (revision) {
116 case 1:
117 if (record->package.count != 18)
118 return AE_ERROR;
119 for (i = 2; i < 18; i++)
120 if (fields[i].type != ACPI_TYPE_INTEGER)
121 return AE_ERROR;
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;
140 break;
141 default:
142 printk(KERN_WARNING
143 "%s: Type 2 Revision %d record not supported\n",
144 __func__, revision);
145 return AE_ERROR;
146 }
147 return AE_OK;
148 }
149
150 static acpi_status
151 acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx)
152 {
153 acpi_status status;
154 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
155 union acpi_object *package, *record, *fields;
156 u32 type;
157 int i;
158
159 /* Clear the return buffer with zeros */
160 memset(hpx, 0, sizeof(struct hotplug_params));
161
162 status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
163 if (ACPI_FAILURE(status))
164 return status;
165
166 package = (union acpi_object *)buffer.pointer;
167 if (package->type != ACPI_TYPE_PACKAGE) {
168 status = AE_ERROR;
169 goto exit;
170 }
171
172 for (i = 0; i < package->package.count; i++) {
173 record = &package->package.elements[i];
174 if (record->type != ACPI_TYPE_PACKAGE) {
175 status = AE_ERROR;
176 goto exit;
177 }
178
179 fields = record->package.elements;
180 if (fields[0].type != ACPI_TYPE_INTEGER ||
181 fields[1].type != ACPI_TYPE_INTEGER) {
182 status = AE_ERROR;
183 goto exit;
184 }
185
186 type = fields[0].integer.value;
187 switch (type) {
188 case 0:
189 status = decode_type0_hpx_record(record, hpx);
190 if (ACPI_FAILURE(status))
191 goto exit;
192 break;
193 case 1:
194 status = decode_type1_hpx_record(record, hpx);
195 if (ACPI_FAILURE(status))
196 goto exit;
197 break;
198 case 2:
199 status = decode_type2_hpx_record(record, hpx);
200 if (ACPI_FAILURE(status))
201 goto exit;
202 break;
203 default:
204 printk(KERN_ERR "%s: Type %d record not supported\n",
205 __func__, type);
206 status = AE_ERROR;
207 goto exit;
208 }
209 }
210 exit:
211 kfree(buffer.pointer);
212 return status;
213 }
214
215 static acpi_status
216 acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp)
217 {
218 acpi_status status;
219 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
220 union acpi_object *package, *fields;
221 int i;
222
223 memset(hpp, 0, sizeof(struct hotplug_params));
224
225 status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer);
226 if (ACPI_FAILURE(status))
227 return status;
228
229 package = (union acpi_object *) buffer.pointer;
230 if (package->type != ACPI_TYPE_PACKAGE ||
231 package->package.count != 4) {
232 status = AE_ERROR;
233 goto exit;
234 }
235
236 fields = package->package.elements;
237 for (i = 0; i < 4; i++) {
238 if (fields[i].type != ACPI_TYPE_INTEGER) {
239 status = AE_ERROR;
240 goto exit;
241 }
242 }
243
244 hpp->t0 = &hpp->type0_data;
245 hpp->t0->revision = 1;
246 hpp->t0->cache_line_size = fields[0].integer.value;
247 hpp->t0->latency_timer = fields[1].integer.value;
248 hpp->t0->enable_serr = fields[2].integer.value;
249 hpp->t0->enable_perr = fields[3].integer.value;
250
251 exit:
252 kfree(buffer.pointer);
253 return status;
254 }
255
256
257
258 /* acpi_run_oshp - get control of hotplug from the firmware
259 *
260 * @handle - the handle of the hotplug controller.
261 */
262 static acpi_status acpi_run_oshp(acpi_handle handle)
263 {
264 acpi_status status;
265 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
266
267 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
268
269 /* run OSHP */
270 status = acpi_evaluate_object(handle, METHOD_NAME_OSHP, NULL, NULL);
271 if (ACPI_FAILURE(status))
272 if (status != AE_NOT_FOUND)
273 printk(KERN_ERR "%s:%s OSHP fails=0x%x\n",
274 __func__, (char *)string.pointer, status);
275 else
276 dbg("%s:%s OSHP not found\n",
277 __func__, (char *)string.pointer);
278 else
279 pr_debug("%s:%s OSHP passes\n", __func__,
280 (char *)string.pointer);
281
282 kfree(string.pointer);
283 return status;
284 }
285
286 /* pci_get_hp_params
287 *
288 * @dev - the pci_dev for which we want parameters
289 * @hpp - allocated by the caller
290 */
291 int pci_get_hp_params(struct pci_dev *dev, struct hotplug_params *hpp)
292 {
293 acpi_status status;
294 acpi_handle handle, phandle;
295 struct pci_bus *pbus;
296
297 handle = NULL;
298 for (pbus = dev->bus; pbus; pbus = pbus->parent) {
299 handle = acpi_pci_get_bridge_handle(pbus);
300 if (handle)
301 break;
302 }
303
304 /*
305 * _HPP settings apply to all child buses, until another _HPP is
306 * encountered. If we don't find an _HPP for the input pci dev,
307 * look for it in the parent device scope since that would apply to
308 * this pci dev.
309 */
310 while (handle) {
311 status = acpi_run_hpx(handle, hpp);
312 if (ACPI_SUCCESS(status))
313 return 0;
314 status = acpi_run_hpp(handle, hpp);
315 if (ACPI_SUCCESS(status))
316 return 0;
317 if (acpi_is_root_bridge(handle))
318 break;
319 status = acpi_get_parent(handle, &phandle);
320 if (ACPI_FAILURE(status))
321 break;
322 handle = phandle;
323 }
324 return -ENODEV;
325 }
326 EXPORT_SYMBOL_GPL(pci_get_hp_params);
327
328 /**
329 * acpi_get_hp_hw_control_from_firmware
330 * @dev: the pci_dev of the bridge that has a hotplug controller
331 * @flags: requested control bits for _OSC
332 *
333 * Attempt to take hotplug control from firmware.
334 */
335 int acpi_get_hp_hw_control_from_firmware(struct pci_dev *pdev, u32 flags)
336 {
337 acpi_status status;
338 acpi_handle chandle, handle;
339 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
340
341 flags &= (OSC_PCI_EXPRESS_NATIVE_HP_CONTROL |
342 OSC_SHPC_NATIVE_HP_CONTROL |
343 OSC_PCI_EXPRESS_CAP_STRUCTURE_CONTROL);
344 if (!flags) {
345 err("Invalid flags %u specified!\n", flags);
346 return -EINVAL;
347 }
348
349 /*
350 * Per PCI firmware specification, we should run the ACPI _OSC
351 * method to get control of hotplug hardware before using it. If
352 * an _OSC is missing, we look for an OSHP to do the same thing.
353 * To handle different BIOS behavior, we look for _OSC on a root
354 * bridge preferentially (according to PCI fw spec). Later for
355 * OSHP within the scope of the hotplug controller and its parents,
356 * upto the host bridge under which this controller exists.
357 */
358 handle = acpi_find_root_bridge_handle(pdev);
359 if (handle) {
360 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
361 dbg("Trying to get hotplug control for %s\n",
362 (char *)string.pointer);
363 status = acpi_pci_osc_control_set(handle, flags);
364 if (ACPI_SUCCESS(status))
365 goto got_one;
366 if (status == AE_SUPPORT)
367 goto no_control;
368 kfree(string.pointer);
369 string = (struct acpi_buffer){ ACPI_ALLOCATE_BUFFER, NULL };
370 }
371
372 handle = DEVICE_ACPI_HANDLE(&pdev->dev);
373 if (!handle) {
374 /*
375 * This hotplug controller was not listed in the ACPI name
376 * space at all. Try to get acpi handle of parent pci bus.
377 */
378 struct pci_bus *pbus;
379 for (pbus = pdev->bus; pbus; pbus = pbus->parent) {
380 handle = acpi_pci_get_bridge_handle(pbus);
381 if (handle)
382 break;
383 }
384 }
385
386 while (handle) {
387 acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
388 dbg("Trying to get hotplug control for %s \n",
389 (char *)string.pointer);
390 status = acpi_run_oshp(handle);
391 if (ACPI_SUCCESS(status))
392 goto got_one;
393 if (acpi_is_root_bridge(handle))
394 break;
395 chandle = handle;
396 status = acpi_get_parent(chandle, &handle);
397 if (ACPI_FAILURE(status))
398 break;
399 }
400 no_control:
401 dbg("Cannot get control of hotplug hardware for pci %s\n",
402 pci_name(pdev));
403 kfree(string.pointer);
404 return -ENODEV;
405 got_one:
406 dbg("Gained control for hotplug HW for pci %s (%s)\n",
407 pci_name(pdev), (char *)string.pointer);
408 kfree(string.pointer);
409 return 0;
410 }
411 EXPORT_SYMBOL(acpi_get_hp_hw_control_from_firmware);
412
413 static int is_ejectable(acpi_handle handle)
414 {
415 acpi_status status;
416 acpi_handle tmp;
417 unsigned long long removable;
418 status = acpi_get_handle(handle, "_ADR", &tmp);
419 if (ACPI_FAILURE(status))
420 return 0;
421 status = acpi_get_handle(handle, "_EJ0", &tmp);
422 if (ACPI_SUCCESS(status))
423 return 1;
424 status = acpi_evaluate_integer(handle, "_RMV", NULL, &removable);
425 if (ACPI_SUCCESS(status) && removable)
426 return 1;
427 return 0;
428 }
429
430 /**
431 * acpi_pcihp_check_ejectable - check if handle is ejectable ACPI PCI slot
432 * @pbus: the PCI bus of the PCI slot corresponding to 'handle'
433 * @handle: ACPI handle to check
434 *
435 * Return 1 if handle is ejectable PCI slot, 0 otherwise.
436 */
437 int acpi_pci_check_ejectable(struct pci_bus *pbus, acpi_handle handle)
438 {
439 acpi_handle bridge_handle, parent_handle;
440
441 if (!(bridge_handle = acpi_pci_get_bridge_handle(pbus)))
442 return 0;
443 if ((ACPI_FAILURE(acpi_get_parent(handle, &parent_handle))))
444 return 0;
445 if (bridge_handle != parent_handle)
446 return 0;
447 return is_ejectable(handle);
448 }
449 EXPORT_SYMBOL_GPL(acpi_pci_check_ejectable);
450
451 static acpi_status
452 check_hotplug(acpi_handle handle, u32 lvl, void *context, void **rv)
453 {
454 int *found = (int *)context;
455 if (is_ejectable(handle)) {
456 *found = 1;
457 return AE_CTRL_TERMINATE;
458 }
459 return AE_OK;
460 }
461
462 /**
463 * acpi_pci_detect_ejectable - check if the PCI bus has ejectable slots
464 * @handle - handle of the PCI bus to scan
465 *
466 * Returns 1 if the PCI bus has ACPI based ejectable slots, 0 otherwise.
467 */
468 int acpi_pci_detect_ejectable(acpi_handle handle)
469 {
470 int found = 0;
471
472 if (!handle)
473 return found;
474
475 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
476 check_hotplug, NULL, (void *)&found, NULL);
477 return found;
478 }
479 EXPORT_SYMBOL_GPL(acpi_pci_detect_ejectable);
480
481 module_param(debug_acpi, bool, 0644);
482 MODULE_PARM_DESC(debug_acpi, "Debugging mode for ACPI enabled or not");