Linux Audio

Check our new training course

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