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