• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * File:	pci-acpi.c
3  * Purpose:	Provide PCI support in ACPI
4  *
5  * Copyright (C) 2005 David Shaohua Li <shaohua.li@intel.com>
6  * Copyright (C) 2004 Tom Long Nguyen <tom.l.nguyen@intel.com>
7  * Copyright (C) 2004 Intel Corp.
8  */
9 
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/pci.h>
13 #include <linux/pci_hotplug.h>
14 #include <linux/module.h>
15 #include <linux/pci-aspm.h>
16 #include <linux/pci-acpi.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/pm_qos.h>
19 #include "pci.h"
20 
acpi_pci_root_get_mcfg_addr(acpi_handle handle)21 phys_addr_t acpi_pci_root_get_mcfg_addr(acpi_handle handle)
22 {
23 	acpi_status status = AE_NOT_EXIST;
24 	unsigned long long mcfg_addr;
25 
26 	if (handle)
27 		status = acpi_evaluate_integer(handle, METHOD_NAME__CBA,
28 					       NULL, &mcfg_addr);
29 	if (ACPI_FAILURE(status))
30 		return 0;
31 
32 	return (phys_addr_t)mcfg_addr;
33 }
34 
decode_type0_hpx_record(union acpi_object * record,struct hotplug_params * hpx)35 static acpi_status decode_type0_hpx_record(union acpi_object *record,
36 					   struct hotplug_params *hpx)
37 {
38 	int i;
39 	union acpi_object *fields = record->package.elements;
40 	u32 revision = fields[1].integer.value;
41 
42 	switch (revision) {
43 	case 1:
44 		if (record->package.count != 6)
45 			return AE_ERROR;
46 		for (i = 2; i < 6; i++)
47 			if (fields[i].type != ACPI_TYPE_INTEGER)
48 				return AE_ERROR;
49 		hpx->t0 = &hpx->type0_data;
50 		hpx->t0->revision        = revision;
51 		hpx->t0->cache_line_size = fields[2].integer.value;
52 		hpx->t0->latency_timer   = fields[3].integer.value;
53 		hpx->t0->enable_serr     = fields[4].integer.value;
54 		hpx->t0->enable_perr     = fields[5].integer.value;
55 		break;
56 	default:
57 		printk(KERN_WARNING
58 		       "%s: Type 0 Revision %d record not supported\n",
59 		       __func__, revision);
60 		return AE_ERROR;
61 	}
62 	return AE_OK;
63 }
64 
decode_type1_hpx_record(union acpi_object * record,struct hotplug_params * hpx)65 static acpi_status decode_type1_hpx_record(union acpi_object *record,
66 					   struct hotplug_params *hpx)
67 {
68 	int i;
69 	union acpi_object *fields = record->package.elements;
70 	u32 revision = fields[1].integer.value;
71 
72 	switch (revision) {
73 	case 1:
74 		if (record->package.count != 5)
75 			return AE_ERROR;
76 		for (i = 2; i < 5; i++)
77 			if (fields[i].type != ACPI_TYPE_INTEGER)
78 				return AE_ERROR;
79 		hpx->t1 = &hpx->type1_data;
80 		hpx->t1->revision      = revision;
81 		hpx->t1->max_mem_read  = fields[2].integer.value;
82 		hpx->t1->avg_max_split = fields[3].integer.value;
83 		hpx->t1->tot_max_split = fields[4].integer.value;
84 		break;
85 	default:
86 		printk(KERN_WARNING
87 		       "%s: Type 1 Revision %d record not supported\n",
88 		       __func__, revision);
89 		return AE_ERROR;
90 	}
91 	return AE_OK;
92 }
93 
decode_type2_hpx_record(union acpi_object * record,struct hotplug_params * hpx)94 static acpi_status decode_type2_hpx_record(union acpi_object *record,
95 					   struct hotplug_params *hpx)
96 {
97 	int i;
98 	union acpi_object *fields = record->package.elements;
99 	u32 revision = fields[1].integer.value;
100 
101 	switch (revision) {
102 	case 1:
103 		if (record->package.count != 18)
104 			return AE_ERROR;
105 		for (i = 2; i < 18; i++)
106 			if (fields[i].type != ACPI_TYPE_INTEGER)
107 				return AE_ERROR;
108 		hpx->t2 = &hpx->type2_data;
109 		hpx->t2->revision      = revision;
110 		hpx->t2->unc_err_mask_and      = fields[2].integer.value;
111 		hpx->t2->unc_err_mask_or       = fields[3].integer.value;
112 		hpx->t2->unc_err_sever_and     = fields[4].integer.value;
113 		hpx->t2->unc_err_sever_or      = fields[5].integer.value;
114 		hpx->t2->cor_err_mask_and      = fields[6].integer.value;
115 		hpx->t2->cor_err_mask_or       = fields[7].integer.value;
116 		hpx->t2->adv_err_cap_and       = fields[8].integer.value;
117 		hpx->t2->adv_err_cap_or        = fields[9].integer.value;
118 		hpx->t2->pci_exp_devctl_and    = fields[10].integer.value;
119 		hpx->t2->pci_exp_devctl_or     = fields[11].integer.value;
120 		hpx->t2->pci_exp_lnkctl_and    = fields[12].integer.value;
121 		hpx->t2->pci_exp_lnkctl_or     = fields[13].integer.value;
122 		hpx->t2->sec_unc_err_sever_and = fields[14].integer.value;
123 		hpx->t2->sec_unc_err_sever_or  = fields[15].integer.value;
124 		hpx->t2->sec_unc_err_mask_and  = fields[16].integer.value;
125 		hpx->t2->sec_unc_err_mask_or   = fields[17].integer.value;
126 		break;
127 	default:
128 		printk(KERN_WARNING
129 		       "%s: Type 2 Revision %d record not supported\n",
130 		       __func__, revision);
131 		return AE_ERROR;
132 	}
133 	return AE_OK;
134 }
135 
acpi_run_hpx(acpi_handle handle,struct hotplug_params * hpx)136 static acpi_status acpi_run_hpx(acpi_handle handle, struct hotplug_params *hpx)
137 {
138 	acpi_status status;
139 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
140 	union acpi_object *package, *record, *fields;
141 	u32 type;
142 	int i;
143 
144 	/* Clear the return buffer with zeros */
145 	memset(hpx, 0, sizeof(struct hotplug_params));
146 
147 	status = acpi_evaluate_object(handle, "_HPX", NULL, &buffer);
148 	if (ACPI_FAILURE(status))
149 		return status;
150 
151 	package = (union acpi_object *)buffer.pointer;
152 	if (package->type != ACPI_TYPE_PACKAGE) {
153 		status = AE_ERROR;
154 		goto exit;
155 	}
156 
157 	for (i = 0; i < package->package.count; i++) {
158 		record = &package->package.elements[i];
159 		if (record->type != ACPI_TYPE_PACKAGE) {
160 			status = AE_ERROR;
161 			goto exit;
162 		}
163 
164 		fields = record->package.elements;
165 		if (fields[0].type != ACPI_TYPE_INTEGER ||
166 		    fields[1].type != ACPI_TYPE_INTEGER) {
167 			status = AE_ERROR;
168 			goto exit;
169 		}
170 
171 		type = fields[0].integer.value;
172 		switch (type) {
173 		case 0:
174 			status = decode_type0_hpx_record(record, hpx);
175 			if (ACPI_FAILURE(status))
176 				goto exit;
177 			break;
178 		case 1:
179 			status = decode_type1_hpx_record(record, hpx);
180 			if (ACPI_FAILURE(status))
181 				goto exit;
182 			break;
183 		case 2:
184 			status = decode_type2_hpx_record(record, hpx);
185 			if (ACPI_FAILURE(status))
186 				goto exit;
187 			break;
188 		default:
189 			printk(KERN_ERR "%s: Type %d record not supported\n",
190 			       __func__, type);
191 			status = AE_ERROR;
192 			goto exit;
193 		}
194 	}
195  exit:
196 	kfree(buffer.pointer);
197 	return status;
198 }
199 
acpi_run_hpp(acpi_handle handle,struct hotplug_params * hpp)200 static acpi_status acpi_run_hpp(acpi_handle handle, struct hotplug_params *hpp)
201 {
202 	acpi_status status;
203 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
204 	union acpi_object *package, *fields;
205 	int i;
206 
207 	memset(hpp, 0, sizeof(struct hotplug_params));
208 
209 	status = acpi_evaluate_object(handle, "_HPP", NULL, &buffer);
210 	if (ACPI_FAILURE(status))
211 		return status;
212 
213 	package = (union acpi_object *) buffer.pointer;
214 	if (package->type != ACPI_TYPE_PACKAGE ||
215 	    package->package.count != 4) {
216 		status = AE_ERROR;
217 		goto exit;
218 	}
219 
220 	fields = package->package.elements;
221 	for (i = 0; i < 4; i++) {
222 		if (fields[i].type != ACPI_TYPE_INTEGER) {
223 			status = AE_ERROR;
224 			goto exit;
225 		}
226 	}
227 
228 	hpp->t0 = &hpp->type0_data;
229 	hpp->t0->revision        = 1;
230 	hpp->t0->cache_line_size = fields[0].integer.value;
231 	hpp->t0->latency_timer   = fields[1].integer.value;
232 	hpp->t0->enable_serr     = fields[2].integer.value;
233 	hpp->t0->enable_perr     = fields[3].integer.value;
234 
235 exit:
236 	kfree(buffer.pointer);
237 	return status;
238 }
239 
240 /* pci_get_hp_params
241  *
242  * @dev - the pci_dev for which we want parameters
243  * @hpp - allocated by the caller
244  */
pci_get_hp_params(struct pci_dev * dev,struct hotplug_params * hpp)245 int pci_get_hp_params(struct pci_dev *dev, struct hotplug_params *hpp)
246 {
247 	acpi_status status;
248 	acpi_handle handle, phandle;
249 	struct pci_bus *pbus;
250 
251 	if (acpi_pci_disabled)
252 		return -ENODEV;
253 
254 	handle = NULL;
255 	for (pbus = dev->bus; pbus; pbus = pbus->parent) {
256 		handle = acpi_pci_get_bridge_handle(pbus);
257 		if (handle)
258 			break;
259 	}
260 
261 	/*
262 	 * _HPP settings apply to all child buses, until another _HPP is
263 	 * encountered. If we don't find an _HPP for the input pci dev,
264 	 * look for it in the parent device scope since that would apply to
265 	 * this pci dev.
266 	 */
267 	while (handle) {
268 		status = acpi_run_hpx(handle, hpp);
269 		if (ACPI_SUCCESS(status))
270 			return 0;
271 		status = acpi_run_hpp(handle, hpp);
272 		if (ACPI_SUCCESS(status))
273 			return 0;
274 		if (acpi_is_root_bridge(handle))
275 			break;
276 		status = acpi_get_parent(handle, &phandle);
277 		if (ACPI_FAILURE(status))
278 			break;
279 		handle = phandle;
280 	}
281 	return -ENODEV;
282 }
283 EXPORT_SYMBOL_GPL(pci_get_hp_params);
284 
285 /**
286  * pci_acpi_wake_bus - Root bus wakeup notification fork function.
287  * @work: Work item to handle.
288  */
pci_acpi_wake_bus(struct work_struct * work)289 static void pci_acpi_wake_bus(struct work_struct *work)
290 {
291 	struct acpi_device *adev;
292 	struct acpi_pci_root *root;
293 
294 	adev = container_of(work, struct acpi_device, wakeup.context.work);
295 	root = acpi_driver_data(adev);
296 	pci_pme_wakeup_bus(root->bus);
297 }
298 
299 /**
300  * pci_acpi_wake_dev - PCI device wakeup notification work function.
301  * @handle: ACPI handle of a device the notification is for.
302  * @work: Work item to handle.
303  */
pci_acpi_wake_dev(struct work_struct * work)304 static void pci_acpi_wake_dev(struct work_struct *work)
305 {
306 	struct acpi_device_wakeup_context *context;
307 	struct pci_dev *pci_dev;
308 
309 	context = container_of(work, struct acpi_device_wakeup_context, work);
310 	pci_dev = to_pci_dev(context->dev);
311 
312 	if (pci_dev->pme_poll)
313 		pci_dev->pme_poll = false;
314 
315 	if (pci_dev->current_state == PCI_D3cold) {
316 		pci_wakeup_event(pci_dev);
317 		pm_runtime_resume(&pci_dev->dev);
318 		return;
319 	}
320 
321 	/* Clear PME Status if set. */
322 	if (pci_dev->pme_support)
323 		pci_check_pme_status(pci_dev);
324 
325 	pci_wakeup_event(pci_dev);
326 	pm_runtime_resume(&pci_dev->dev);
327 
328 	if (pci_dev->subordinate)
329 		pci_pme_wakeup_bus(pci_dev->subordinate);
330 }
331 
332 /**
333  * pci_acpi_add_bus_pm_notifier - Register PM notifier for root PCI bus.
334  * @dev: PCI root bridge ACPI device.
335  */
pci_acpi_add_bus_pm_notifier(struct acpi_device * dev)336 acpi_status pci_acpi_add_bus_pm_notifier(struct acpi_device *dev)
337 {
338 	return acpi_add_pm_notifier(dev, NULL, pci_acpi_wake_bus);
339 }
340 
341 /**
342  * pci_acpi_add_pm_notifier - Register PM notifier for given PCI device.
343  * @dev: ACPI device to add the notifier for.
344  * @pci_dev: PCI device to check for the PME status if an event is signaled.
345  */
pci_acpi_add_pm_notifier(struct acpi_device * dev,struct pci_dev * pci_dev)346 acpi_status pci_acpi_add_pm_notifier(struct acpi_device *dev,
347 				     struct pci_dev *pci_dev)
348 {
349 	return acpi_add_pm_notifier(dev, &pci_dev->dev, pci_acpi_wake_dev);
350 }
351 
352 /*
353  * _SxD returns the D-state with the highest power
354  * (lowest D-state number) supported in the S-state "x".
355  *
356  * If the devices does not have a _PRW
357  * (Power Resources for Wake) supporting system wakeup from "x"
358  * then the OS is free to choose a lower power (higher number
359  * D-state) than the return value from _SxD.
360  *
361  * But if _PRW is enabled at S-state "x", the OS
362  * must not choose a power lower than _SxD --
363  * unless the device has an _SxW method specifying
364  * the lowest power (highest D-state number) the device
365  * may enter while still able to wake the system.
366  *
367  * ie. depending on global OS policy:
368  *
369  * if (_PRW at S-state x)
370  *	choose from highest power _SxD to lowest power _SxW
371  * else // no _PRW at S-state x
372  *	choose highest power _SxD or any lower power
373  */
374 
acpi_pci_choose_state(struct pci_dev * pdev)375 static pci_power_t acpi_pci_choose_state(struct pci_dev *pdev)
376 {
377 	int acpi_state, d_max;
378 
379 	if (pdev->no_d3cold)
380 		d_max = ACPI_STATE_D3_HOT;
381 	else
382 		d_max = ACPI_STATE_D3_COLD;
383 	acpi_state = acpi_pm_device_sleep_state(&pdev->dev, NULL, d_max);
384 	if (acpi_state < 0)
385 		return PCI_POWER_ERROR;
386 
387 	switch (acpi_state) {
388 	case ACPI_STATE_D0:
389 		return PCI_D0;
390 	case ACPI_STATE_D1:
391 		return PCI_D1;
392 	case ACPI_STATE_D2:
393 		return PCI_D2;
394 	case ACPI_STATE_D3_HOT:
395 		return PCI_D3hot;
396 	case ACPI_STATE_D3_COLD:
397 		return PCI_D3cold;
398 	}
399 	return PCI_POWER_ERROR;
400 }
401 
acpi_pci_power_manageable(struct pci_dev * dev)402 static bool acpi_pci_power_manageable(struct pci_dev *dev)
403 {
404 	struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
405 	return adev ? acpi_device_power_manageable(adev) : false;
406 }
407 
acpi_pci_set_power_state(struct pci_dev * dev,pci_power_t state)408 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
409 {
410 	struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
411 	static const u8 state_conv[] = {
412 		[PCI_D0] = ACPI_STATE_D0,
413 		[PCI_D1] = ACPI_STATE_D1,
414 		[PCI_D2] = ACPI_STATE_D2,
415 		[PCI_D3hot] = ACPI_STATE_D3_COLD,
416 		[PCI_D3cold] = ACPI_STATE_D3_COLD,
417 	};
418 	int error = -EINVAL;
419 
420 	/* If the ACPI device has _EJ0, ignore the device */
421 	if (!adev || acpi_has_method(adev->handle, "_EJ0"))
422 		return -ENODEV;
423 
424 	switch (state) {
425 	case PCI_D3cold:
426 		if (dev_pm_qos_flags(&dev->dev, PM_QOS_FLAG_NO_POWER_OFF) ==
427 				PM_QOS_FLAGS_ALL) {
428 			error = -EBUSY;
429 			break;
430 		}
431 	case PCI_D0:
432 	case PCI_D1:
433 	case PCI_D2:
434 	case PCI_D3hot:
435 		error = acpi_device_set_power(adev, state_conv[state]);
436 	}
437 
438 	if (!error)
439 		dev_dbg(&dev->dev, "power state changed by ACPI to %s\n",
440 			 acpi_power_state_string(state_conv[state]));
441 
442 	return error;
443 }
444 
acpi_pci_can_wakeup(struct pci_dev * dev)445 static bool acpi_pci_can_wakeup(struct pci_dev *dev)
446 {
447 	struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
448 	return adev ? acpi_device_can_wakeup(adev) : false;
449 }
450 
acpi_pci_propagate_wakeup_enable(struct pci_bus * bus,bool enable)451 static void acpi_pci_propagate_wakeup_enable(struct pci_bus *bus, bool enable)
452 {
453 	while (bus->parent) {
454 		if (!acpi_pm_device_sleep_wake(&bus->self->dev, enable))
455 			return;
456 		bus = bus->parent;
457 	}
458 
459 	/* We have reached the root bus. */
460 	if (bus->bridge)
461 		acpi_pm_device_sleep_wake(bus->bridge, enable);
462 }
463 
acpi_pci_sleep_wake(struct pci_dev * dev,bool enable)464 static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
465 {
466 	if (acpi_pci_can_wakeup(dev))
467 		return acpi_pm_device_sleep_wake(&dev->dev, enable);
468 
469 	acpi_pci_propagate_wakeup_enable(dev->bus, enable);
470 	return 0;
471 }
472 
acpi_pci_propagate_run_wake(struct pci_bus * bus,bool enable)473 static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
474 {
475 	while (bus->parent) {
476 		struct pci_dev *bridge = bus->self;
477 
478 		if (bridge->pme_interrupt)
479 			return;
480 		if (!acpi_pm_device_run_wake(&bridge->dev, enable))
481 			return;
482 		bus = bus->parent;
483 	}
484 
485 	/* We have reached the root bus. */
486 	if (bus->bridge)
487 		acpi_pm_device_run_wake(bus->bridge, enable);
488 }
489 
acpi_pci_run_wake(struct pci_dev * dev,bool enable)490 static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
491 {
492 	/*
493 	 * Per PCI Express Base Specification Revision 2.0 section
494 	 * 5.3.3.2 Link Wakeup, platform support is needed for D3cold
495 	 * waking up to power on the main link even if there is PME
496 	 * support for D3cold
497 	 */
498 	if (dev->pme_interrupt && !dev->runtime_d3cold)
499 		return 0;
500 
501 	if (!acpi_pm_device_run_wake(&dev->dev, enable))
502 		return 0;
503 
504 	acpi_pci_propagate_run_wake(dev->bus, enable);
505 	return 0;
506 }
507 
508 static struct pci_platform_pm_ops acpi_pci_platform_pm = {
509 	.is_manageable = acpi_pci_power_manageable,
510 	.set_state = acpi_pci_set_power_state,
511 	.choose_state = acpi_pci_choose_state,
512 	.sleep_wake = acpi_pci_sleep_wake,
513 	.run_wake = acpi_pci_run_wake,
514 };
515 
acpi_pci_add_bus(struct pci_bus * bus)516 void acpi_pci_add_bus(struct pci_bus *bus)
517 {
518 	if (acpi_pci_disabled || !bus->bridge)
519 		return;
520 
521 	acpi_pci_slot_enumerate(bus);
522 	acpiphp_enumerate_slots(bus);
523 }
524 
acpi_pci_remove_bus(struct pci_bus * bus)525 void acpi_pci_remove_bus(struct pci_bus *bus)
526 {
527 	if (acpi_pci_disabled || !bus->bridge)
528 		return;
529 
530 	acpiphp_remove_slots(bus);
531 	acpi_pci_slot_remove(bus);
532 }
533 
534 /* ACPI bus type */
acpi_pci_find_companion(struct device * dev)535 static struct acpi_device *acpi_pci_find_companion(struct device *dev)
536 {
537 	struct pci_dev *pci_dev = to_pci_dev(dev);
538 	bool check_children;
539 	u64 addr;
540 
541 	check_children = pci_is_bridge(pci_dev);
542 	/* Please ref to ACPI spec for the syntax of _ADR */
543 	addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
544 	return acpi_find_child_device(ACPI_COMPANION(dev->parent), addr,
545 				      check_children);
546 }
547 
pci_acpi_setup(struct device * dev)548 static void pci_acpi_setup(struct device *dev)
549 {
550 	struct pci_dev *pci_dev = to_pci_dev(dev);
551 	struct acpi_device *adev = ACPI_COMPANION(dev);
552 
553 	if (!adev)
554 		return;
555 
556 	pci_acpi_add_pm_notifier(adev, pci_dev);
557 	if (!adev->wakeup.flags.valid)
558 		return;
559 
560 	device_set_wakeup_capable(dev, true);
561 	acpi_pci_sleep_wake(pci_dev, false);
562 	if (adev->wakeup.flags.run_wake)
563 		device_set_run_wake(dev, true);
564 }
565 
pci_acpi_cleanup(struct device * dev)566 static void pci_acpi_cleanup(struct device *dev)
567 {
568 	struct acpi_device *adev = ACPI_COMPANION(dev);
569 
570 	if (!adev)
571 		return;
572 
573 	pci_acpi_remove_pm_notifier(adev);
574 	if (adev->wakeup.flags.valid) {
575 		device_set_wakeup_capable(dev, false);
576 		device_set_run_wake(dev, false);
577 	}
578 }
579 
pci_acpi_bus_match(struct device * dev)580 static bool pci_acpi_bus_match(struct device *dev)
581 {
582 	return dev_is_pci(dev);
583 }
584 
585 static struct acpi_bus_type acpi_pci_bus = {
586 	.name = "PCI",
587 	.match = pci_acpi_bus_match,
588 	.find_companion = acpi_pci_find_companion,
589 	.setup = pci_acpi_setup,
590 	.cleanup = pci_acpi_cleanup,
591 };
592 
acpi_pci_init(void)593 static int __init acpi_pci_init(void)
594 {
595 	int ret;
596 
597 	if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_MSI) {
598 		pr_info("ACPI FADT declares the system doesn't support MSI, so disable it\n");
599 		pci_no_msi();
600 	}
601 
602 	if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_ASPM) {
603 		pr_info("ACPI FADT declares the system doesn't support PCIe ASPM, so disable it\n");
604 		pcie_no_aspm();
605 	}
606 
607 	ret = register_acpi_bus_type(&acpi_pci_bus);
608 	if (ret)
609 		return 0;
610 
611 	pci_set_platform_pm(&acpi_pci_platform_pm);
612 	acpi_pci_slot_init();
613 	acpiphp_init();
614 
615 	return 0;
616 }
617 arch_initcall(acpi_pci_init);
618