1 /*
2  * Copyright (c) 2010 Red Hat Inc.
3  * Author : Dave Airlie <airlied@redhat.com>
4  *
5  * Licensed under GPLv2
6  *
7  * ATPX support for both Intel/ATI
8  */
9 #include <linux/vga_switcheroo.h>
10 #include <linux/slab.h>
11 #include <acpi/acpi.h>
12 #include <acpi/acpi_bus.h>
13 #include <linux/pci.h>
14 
15 #define ATPX_VERSION 0
16 #define ATPX_GPU_PWR 2
17 #define ATPX_MUX_SELECT 3
18 #define ATPX_I2C_MUX_SELECT 4
19 #define ATPX_SWITCH_START 5
20 #define ATPX_SWITCH_END 6
21 
22 #define ATPX_INTEGRATED 0
23 #define ATPX_DISCRETE 1
24 
25 #define ATPX_MUX_IGD 0
26 #define ATPX_MUX_DISCRETE 1
27 
28 static struct radeon_atpx_priv {
29 	bool atpx_detected;
30 	/* handle for device - and atpx */
31 	acpi_handle dhandle;
32 	acpi_handle atpx_handle;
33 	acpi_handle atrm_handle;
34 } radeon_atpx_priv;
35 
36 /* retrieve the ROM in 4k blocks */
37 static int radeon_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
38 			    int offset, int len)
39 {
40 	acpi_status status;
41 	union acpi_object atrm_arg_elements[2], *obj;
42 	struct acpi_object_list atrm_arg;
43 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
44 
45 	atrm_arg.count = 2;
46 	atrm_arg.pointer = &atrm_arg_elements[0];
47 
48 	atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
49 	atrm_arg_elements[0].integer.value = offset;
50 
51 	atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
52 	atrm_arg_elements[1].integer.value = len;
53 
54 	status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
55 	if (ACPI_FAILURE(status)) {
56 		printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
57 		return -ENODEV;
58 	}
59 
60 	obj = (union acpi_object *)buffer.pointer;
61 	memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
62 	len = obj->buffer.length;
63 	kfree(buffer.pointer);
64 	return len;
65 }
66 
67 bool radeon_atrm_supported(struct pci_dev *pdev)
68 {
69 	/* get the discrete ROM only via ATRM */
70 	if (!radeon_atpx_priv.atpx_detected)
71 		return false;
72 
73 	if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
74 		return false;
75 	return true;
76 }
77 
78 
79 int radeon_atrm_get_bios_chunk(uint8_t *bios, int offset, int len)
80 {
81 	return radeon_atrm_call(radeon_atpx_priv.atrm_handle, bios, offset, len);
82 }
83 
84 static int radeon_atpx_get_version(acpi_handle handle)
85 {
86 	acpi_status status;
87 	union acpi_object atpx_arg_elements[2], *obj;
88 	struct acpi_object_list atpx_arg;
89 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
90 
91 	atpx_arg.count = 2;
92 	atpx_arg.pointer = &atpx_arg_elements[0];
93 
94 	atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
95 	atpx_arg_elements[0].integer.value = ATPX_VERSION;
96 
97 	atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
98 	atpx_arg_elements[1].integer.value = ATPX_VERSION;
99 
100 	status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
101 	if (ACPI_FAILURE(status)) {
102 		printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
103 		return -ENOSYS;
104 	}
105 	obj = (union acpi_object *)buffer.pointer;
106 	if (obj && (obj->type == ACPI_TYPE_BUFFER))
107 		printk(KERN_INFO "radeon atpx: version is %d\n", *((u8 *)(obj->buffer.pointer) + 2));
108 	kfree(buffer.pointer);
109 	return 0;
110 }
111 
112 static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value)
113 {
114 	acpi_status status;
115 	union acpi_object atpx_arg_elements[2];
116 	struct acpi_object_list atpx_arg;
117 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
118 	uint8_t buf[4] = {0};
119 
120 	if (!handle)
121 		return -EINVAL;
122 
123 	atpx_arg.count = 2;
124 	atpx_arg.pointer = &atpx_arg_elements[0];
125 
126 	atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
127 	atpx_arg_elements[0].integer.value = cmd_id;
128 
129 	buf[2] = value & 0xff;
130 	buf[3] = (value >> 8) & 0xff;
131 
132 	atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
133 	atpx_arg_elements[1].buffer.length = 4;
134 	atpx_arg_elements[1].buffer.pointer = buf;
135 
136 	status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
137 	if (ACPI_FAILURE(status)) {
138 		printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
139 		return -ENOSYS;
140 	}
141 	kfree(buffer.pointer);
142 
143 	return 0;
144 }
145 
146 static int radeon_atpx_set_discrete_state(acpi_handle handle, int state)
147 {
148 	return radeon_atpx_execute(handle, ATPX_GPU_PWR, state);
149 }
150 
151 static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id)
152 {
153 	return radeon_atpx_execute(handle, ATPX_MUX_SELECT, mux_id);
154 }
155 
156 static int radeon_atpx_switch_i2c_mux(acpi_handle handle, int mux_id)
157 {
158 	return radeon_atpx_execute(handle, ATPX_I2C_MUX_SELECT, mux_id);
159 }
160 
161 static int radeon_atpx_switch_start(acpi_handle handle, int gpu_id)
162 {
163 	return radeon_atpx_execute(handle, ATPX_SWITCH_START, gpu_id);
164 }
165 
166 static int radeon_atpx_switch_end(acpi_handle handle, int gpu_id)
167 {
168 	return radeon_atpx_execute(handle, ATPX_SWITCH_END, gpu_id);
169 }
170 
171 static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
172 {
173 	int gpu_id;
174 
175 	if (id == VGA_SWITCHEROO_IGD)
176 		gpu_id = ATPX_INTEGRATED;
177 	else
178 		gpu_id = ATPX_DISCRETE;
179 
180 	radeon_atpx_switch_start(radeon_atpx_priv.atpx_handle, gpu_id);
181 	radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, gpu_id);
182 	radeon_atpx_switch_i2c_mux(radeon_atpx_priv.atpx_handle, gpu_id);
183 	radeon_atpx_switch_end(radeon_atpx_priv.atpx_handle, gpu_id);
184 
185 	return 0;
186 }
187 
188 static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
189 				   enum vga_switcheroo_state state)
190 {
191 	/* on w500 ACPI can't change intel gpu state */
192 	if (id == VGA_SWITCHEROO_IGD)
193 		return 0;
194 
195 	radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state);
196 	return 0;
197 }
198 
199 static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
200 {
201 	acpi_handle dhandle, atpx_handle, atrm_handle;
202 	acpi_status status;
203 
204 	dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
205 	if (!dhandle)
206 		return false;
207 
208 	status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
209 	if (ACPI_FAILURE(status))
210 		return false;
211 
212 	status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
213 	if (ACPI_FAILURE(status))
214 		return false;
215 
216 	radeon_atpx_priv.dhandle = dhandle;
217 	radeon_atpx_priv.atpx_handle = atpx_handle;
218 	radeon_atpx_priv.atrm_handle = atrm_handle;
219 	return true;
220 }
221 
222 static int radeon_atpx_init(void)
223 {
224 	/* set up the ATPX handle */
225 
226 	radeon_atpx_get_version(radeon_atpx_priv.atpx_handle);
227 	return 0;
228 }
229 
230 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
231 {
232 	if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
233 		return VGA_SWITCHEROO_IGD;
234 	else
235 		return VGA_SWITCHEROO_DIS;
236 }
237 
238 static struct vga_switcheroo_handler radeon_atpx_handler = {
239 	.switchto = radeon_atpx_switchto,
240 	.power_state = radeon_atpx_power_state,
241 	.init = radeon_atpx_init,
242 	.get_client_id = radeon_atpx_get_client_id,
243 };
244 
245 static bool radeon_atpx_detect(void)
246 {
247 	char acpi_method_name[255] = { 0 };
248 	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
249 	struct pci_dev *pdev = NULL;
250 	bool has_atpx = false;
251 	int vga_count = 0;
252 
253 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
254 		vga_count++;
255 
256 		has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
257 	}
258 
259 	if (has_atpx && vga_count == 2) {
260 		acpi_get_name(radeon_atpx_priv.atpx_handle, ACPI_FULL_PATHNAME, &buffer);
261 		printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
262 		       acpi_method_name);
263 		radeon_atpx_priv.atpx_detected = true;
264 		return true;
265 	}
266 	return false;
267 }
268 
269 void radeon_register_atpx_handler(void)
270 {
271 	bool r;
272 
273 	/* detect if we have any ATPX + 2 VGA in the system */
274 	r = radeon_atpx_detect();
275 	if (!r)
276 		return;
277 
278 	vga_switcheroo_register_handler(&radeon_atpx_handler);
279 }
280 
281 void radeon_unregister_atpx_handler(void)
282 {
283 	vga_switcheroo_unregister_handler();
284 }
285