xref: /openbmc/linux/drivers/pci/pci-acpi.c (revision 64c70b1c)
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/module.h>
14 #include <acpi/acpi.h>
15 #include <acpi/acnamesp.h>
16 #include <acpi/acresrc.h>
17 #include <acpi/acpi_bus.h>
18 
19 #include <linux/pci-acpi.h>
20 #include "pci.h"
21 
22 static u32 ctrlset_buf[3] = {0, 0, 0};
23 static u32 global_ctrlsets = 0;
24 static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40, 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
25 
26 static acpi_status
27 acpi_query_osc (
28 	acpi_handle	handle,
29 	u32		level,
30 	void		*context,
31 	void		**retval )
32 {
33 	acpi_status		status;
34 	struct acpi_object_list	input;
35 	union acpi_object 	in_params[4];
36 	struct acpi_buffer	output = {ACPI_ALLOCATE_BUFFER, NULL};
37 	union acpi_object 	*out_obj;
38 	u32			osc_dw0;
39 	acpi_status *ret_status = (acpi_status *)retval;
40 
41 
42 	/* Setting up input parameters */
43 	input.count = 4;
44 	input.pointer = in_params;
45 	in_params[0].type 		= ACPI_TYPE_BUFFER;
46 	in_params[0].buffer.length 	= 16;
47 	in_params[0].buffer.pointer	= OSC_UUID;
48 	in_params[1].type 		= ACPI_TYPE_INTEGER;
49 	in_params[1].integer.value 	= 1;
50 	in_params[2].type 		= ACPI_TYPE_INTEGER;
51 	in_params[2].integer.value	= 3;
52 	in_params[3].type		= ACPI_TYPE_BUFFER;
53 	in_params[3].buffer.length 	= 12;
54 	in_params[3].buffer.pointer 	= (u8 *)context;
55 
56 	status = acpi_evaluate_object(handle, "_OSC", &input, &output);
57 	if (ACPI_FAILURE (status)) {
58 		printk(KERN_DEBUG
59 			"Evaluate _OSC Set fails. Status = 0x%04x\n", status);
60 		*ret_status = status;
61 		return status;
62 	}
63 	out_obj = output.pointer;
64 
65 	if (out_obj->type != ACPI_TYPE_BUFFER) {
66 		printk(KERN_DEBUG
67 			"Evaluate _OSC returns wrong type\n");
68 		status = AE_TYPE;
69 		goto query_osc_out;
70 	}
71 	osc_dw0 = *((u32 *) out_obj->buffer.pointer);
72 	if (osc_dw0) {
73 		if (osc_dw0 & OSC_REQUEST_ERROR)
74 			printk(KERN_DEBUG "_OSC request fails\n");
75 		if (osc_dw0 & OSC_INVALID_UUID_ERROR)
76 			printk(KERN_DEBUG "_OSC invalid UUID\n");
77 		if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
78 			printk(KERN_DEBUG "_OSC invalid revision\n");
79 		if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
80 			/* Update Global Control Set */
81 			global_ctrlsets = *((u32 *)(out_obj->buffer.pointer+8));
82 			status = AE_OK;
83 			goto query_osc_out;
84 		}
85 		status = AE_ERROR;
86 		goto query_osc_out;
87 	}
88 
89 	/* Update Global Control Set */
90 	global_ctrlsets = *((u32 *)(out_obj->buffer.pointer + 8));
91 	status = AE_OK;
92 
93 query_osc_out:
94 	kfree(output.pointer);
95 	*ret_status = status;
96 	return status;
97 }
98 
99 
100 static acpi_status
101 acpi_run_osc (
102 	acpi_handle	handle,
103 	void		*context)
104 {
105 	acpi_status		status;
106 	struct acpi_object_list	input;
107 	union acpi_object 	in_params[4];
108 	struct acpi_buffer	output = {ACPI_ALLOCATE_BUFFER, NULL};
109 	union acpi_object 	*out_obj;
110 	u32			osc_dw0;
111 
112 	/* Setting up input parameters */
113 	input.count = 4;
114 	input.pointer = in_params;
115 	in_params[0].type 		= ACPI_TYPE_BUFFER;
116 	in_params[0].buffer.length 	= 16;
117 	in_params[0].buffer.pointer	= OSC_UUID;
118 	in_params[1].type 		= ACPI_TYPE_INTEGER;
119 	in_params[1].integer.value 	= 1;
120 	in_params[2].type 		= ACPI_TYPE_INTEGER;
121 	in_params[2].integer.value	= 3;
122 	in_params[3].type		= ACPI_TYPE_BUFFER;
123 	in_params[3].buffer.length 	= 12;
124 	in_params[3].buffer.pointer 	= (u8 *)context;
125 
126 	status = acpi_evaluate_object(handle, "_OSC", &input, &output);
127 	if (ACPI_FAILURE (status)) {
128 		printk(KERN_DEBUG
129 			"Evaluate _OSC Set fails. Status = 0x%04x\n", status);
130 		return status;
131 	}
132 	out_obj = output.pointer;
133 	if (out_obj->type != ACPI_TYPE_BUFFER) {
134 		printk(KERN_DEBUG
135 			"Evaluate _OSC returns wrong type\n");
136 		status = AE_TYPE;
137 		goto run_osc_out;
138 	}
139 	osc_dw0 = *((u32 *) out_obj->buffer.pointer);
140 	if (osc_dw0) {
141 		if (osc_dw0 & OSC_REQUEST_ERROR)
142 			printk(KERN_DEBUG "_OSC request fails\n");
143 		if (osc_dw0 & OSC_INVALID_UUID_ERROR)
144 			printk(KERN_DEBUG "_OSC invalid UUID\n");
145 		if (osc_dw0 & OSC_INVALID_REVISION_ERROR)
146 			printk(KERN_DEBUG "_OSC invalid revision\n");
147 		if (osc_dw0 & OSC_CAPABILITIES_MASK_ERROR) {
148 			printk(KERN_DEBUG "_OSC FW not grant req. control\n");
149 			status = AE_SUPPORT;
150 			goto run_osc_out;
151 		}
152 		status = AE_ERROR;
153 		goto run_osc_out;
154 	}
155 	status = AE_OK;
156 
157 run_osc_out:
158 	kfree(output.pointer);
159 	return status;
160 }
161 
162 /**
163  * pci_osc_support_set - register OS support to Firmware
164  * @flags: OS support bits
165  *
166  * Update OS support fields and doing a _OSC Query to obtain an update
167  * from Firmware on supported control bits.
168  **/
169 acpi_status pci_osc_support_set(u32 flags)
170 {
171 	u32 temp;
172 	acpi_status retval;
173 
174 	if (!(flags & OSC_SUPPORT_MASKS)) {
175 		return AE_TYPE;
176 	}
177 	ctrlset_buf[OSC_SUPPORT_TYPE] |= (flags & OSC_SUPPORT_MASKS);
178 
179 	/* do _OSC query for all possible controls */
180 	temp = ctrlset_buf[OSC_CONTROL_TYPE];
181 	ctrlset_buf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
182 	ctrlset_buf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
183 	acpi_get_devices ( PCI_ROOT_HID_STRING,
184 			acpi_query_osc,
185 			ctrlset_buf,
186 			(void **) &retval );
187 	ctrlset_buf[OSC_QUERY_TYPE] = !OSC_QUERY_ENABLE;
188 	ctrlset_buf[OSC_CONTROL_TYPE] = temp;
189 	if (ACPI_FAILURE(retval)) {
190 		/* no osc support at all */
191 		ctrlset_buf[OSC_SUPPORT_TYPE] = 0;
192 	}
193 	return AE_OK;
194 }
195 EXPORT_SYMBOL(pci_osc_support_set);
196 
197 /**
198  * pci_osc_control_set - commit requested control to Firmware
199  * @handle: acpi_handle for the target ACPI object
200  * @flags: driver's requested control bits
201  *
202  * Attempt to take control from Firmware on requested control bits.
203  **/
204 acpi_status pci_osc_control_set(acpi_handle handle, u32 flags)
205 {
206 	acpi_status	status;
207 	u32		ctrlset;
208 
209 	ctrlset = (flags & OSC_CONTROL_MASKS);
210 	if (!ctrlset) {
211 		return AE_TYPE;
212 	}
213 	if (ctrlset_buf[OSC_SUPPORT_TYPE] &&
214 	 	((global_ctrlsets & ctrlset) != ctrlset)) {
215 		return AE_SUPPORT;
216 	}
217 	ctrlset_buf[OSC_CONTROL_TYPE] |= ctrlset;
218 	status = acpi_run_osc(handle, ctrlset_buf);
219 	if (ACPI_FAILURE (status)) {
220 		ctrlset_buf[OSC_CONTROL_TYPE] &= ~ctrlset;
221 	}
222 
223 	return status;
224 }
225 EXPORT_SYMBOL(pci_osc_control_set);
226 
227 /*
228  * _SxD returns the D-state with the highest power
229  * (lowest D-state number) supported in the S-state "x".
230  *
231  * If the devices does not have a _PRW
232  * (Power Resources for Wake) supporting system wakeup from "x"
233  * then the OS is free to choose a lower power (higher number
234  * D-state) than the return value from _SxD.
235  *
236  * But if _PRW is enabled at S-state "x", the OS
237  * must not choose a power lower than _SxD --
238  * unless the device has an _SxW method specifying
239  * the lowest power (highest D-state number) the device
240  * may enter while still able to wake the system.
241  *
242  * ie. depending on global OS policy:
243  *
244  * if (_PRW at S-state x)
245  *	choose from highest power _SxD to lowest power _SxW
246  * else // no _PRW at S-state x
247  * 	choose highest power _SxD or any lower power
248  *
249  * currently we simply return _SxD, if present.
250  */
251 
252 static int acpi_pci_choose_state(struct pci_dev *pdev, pm_message_t state)
253 {
254 	/* TBD */
255 
256 	return -ENODEV;
257 }
258 
259 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
260 {
261 	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
262 	static int state_conv[] = {
263 		[0] = 0,
264 		[1] = 1,
265 		[2] = 2,
266 		[3] = 3,
267 		[4] = 3
268 	};
269 	int acpi_state = state_conv[(int __force) state];
270 
271 	if (!handle)
272 		return -ENODEV;
273 	return acpi_bus_set_power(handle, acpi_state);
274 }
275 
276 
277 /* ACPI bus type */
278 static int acpi_pci_find_device(struct device *dev, acpi_handle *handle)
279 {
280 	struct pci_dev * pci_dev;
281 	acpi_integer	addr;
282 
283 	pci_dev = to_pci_dev(dev);
284 	/* Please ref to ACPI spec for the syntax of _ADR */
285 	addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
286 	*handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
287 	if (!*handle)
288 		return -ENODEV;
289 	return 0;
290 }
291 
292 static int acpi_pci_find_root_bridge(struct device *dev, acpi_handle *handle)
293 {
294 	int num;
295 	unsigned int seg, bus;
296 
297 	/*
298 	 * The string should be the same as root bridge's name
299 	 * Please look at 'pci_scan_bus_parented'
300 	 */
301 	num = sscanf(dev->bus_id, "pci%04x:%02x", &seg, &bus);
302 	if (num != 2)
303 		return -ENODEV;
304 	*handle = acpi_get_pci_rootbridge_handle(seg, bus);
305 	if (!*handle)
306 		return -ENODEV;
307 	return 0;
308 }
309 
310 static struct acpi_bus_type acpi_pci_bus = {
311 	.bus = &pci_bus_type,
312 	.find_device = acpi_pci_find_device,
313 	.find_bridge = acpi_pci_find_root_bridge,
314 };
315 
316 static int __init acpi_pci_init(void)
317 {
318 	int ret;
319 
320 	if (acpi_gbl_FADT.boot_flags & BAF_MSI_NOT_SUPPORTED) {
321 		printk(KERN_INFO"ACPI FADT declares the system doesn't support MSI, so disable it\n");
322 		pci_no_msi();
323 	}
324 	ret = register_acpi_bus_type(&acpi_pci_bus);
325 	if (ret)
326 		return 0;
327 	platform_pci_choose_state = acpi_pci_choose_state;
328 	platform_pci_set_power_state = acpi_pci_set_power_state;
329 	return 0;
330 }
331 arch_initcall(acpi_pci_init);
332