radeon_atpx_handler.c 15.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Copyright (c) 2010 Red Hat Inc.
 * Author : Dave Airlie <airlied@redhat.com>
 *
 * Licensed under GPLv2
 *
 * ATPX support for both Intel/ATI
 */
#include <linux/vga_switcheroo.h>
10
#include <linux/slab.h>
11
#include <linux/acpi.h>
12
#include <linux/pci.h>
13
#include <linux/delay.h>
14

15
#include "radeon_acpi.h"
16

17 18 19 20 21 22 23 24 25 26 27 28
struct radeon_atpx_functions {
	bool px_params;
	bool power_cntl;
	bool disp_mux_cntl;
	bool i2c_mux_cntl;
	bool switch_start;
	bool switch_end;
	bool disp_connectors_mapping;
	bool disp_detetion_ports;
};

struct radeon_atpx {
29
	acpi_handle handle;
30
	struct radeon_atpx_functions functions;
31
	bool is_hybrid;
32
	bool dgpu_req_power_for_displays;
33 34
};

35 36
static struct radeon_atpx_priv {
	bool atpx_detected;
37
	bool bridge_pm_usable;
38 39
	/* handle for device - and atpx */
	acpi_handle dhandle;
40
	struct radeon_atpx atpx;
41 42
} radeon_atpx_priv;

43 44 45 46 47 48
struct atpx_verify_interface {
	u16 size;		/* structure size in bytes (includes size field) */
	u16 version;		/* version */
	u32 function_bits;	/* supported functions bit vector */
} __packed;

49 50 51 52 53 54
struct atpx_px_params {
	u16 size;		/* structure size in bytes (includes size field) */
	u32 valid_flags;	/* which flags are valid */
	u32 flags;		/* flags */
} __packed;

55 56 57 58 59 60 61 62 63 64
struct atpx_power_control {
	u16 size;
	u8 dgpu_state;
} __packed;

struct atpx_mux {
	u16 size;
	u16 mux;
} __packed;

65
bool radeon_has_atpx(void) {
66 67 68
	return radeon_atpx_priv.atpx_detected;
}

69 70 71 72
bool radeon_has_atpx_dgpu_power_cntl(void) {
	return radeon_atpx_priv.atpx.functions.power_cntl;
}

73 74 75 76
bool radeon_is_atpx_hybrid(void) {
	return radeon_atpx_priv.atpx.is_hybrid;
}

77 78 79 80
bool radeon_atpx_dgpu_req_power_for_displays(void) {
	return radeon_atpx_priv.atpx.dgpu_req_power_for_displays;
}

81 82 83 84 85 86 87 88 89 90 91 92
/**
 * radeon_atpx_call - call an ATPX method
 *
 * @handle: acpi handle
 * @function: the ATPX function to execute
 * @params: ATPX function params
 *
 * Executes the requested ATPX function (all asics).
 * Returns a pointer to the acpi output buffer.
 */
static union acpi_object *radeon_atpx_call(acpi_handle handle, int function,
					   struct acpi_buffer *params)
93 94
{
	acpi_status status;
95
	union acpi_object atpx_arg_elements[2];
96 97 98 99 100 101 102
	struct acpi_object_list atpx_arg;
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };

	atpx_arg.count = 2;
	atpx_arg.pointer = &atpx_arg_elements[0];

	atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
103 104 105 106 107 108 109 110 111 112 113
	atpx_arg_elements[0].integer.value = function;

	if (params) {
		atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
		atpx_arg_elements[1].buffer.length = params->length;
		atpx_arg_elements[1].buffer.pointer = params->pointer;
	} else {
		/* We need a second fake parameter */
		atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
		atpx_arg_elements[1].integer.value = 0;
	}
114

115
	status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
116

117 118 119 120 121 122
	/* Fail only if calling the method fails and ATPX is supported */
	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
		printk("failed to evaluate ATPX got %s\n",
		       acpi_format_exception(status));
		kfree(buffer.pointer);
		return NULL;
123
	}
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

	return buffer.pointer;
}

/**
 * radeon_atpx_parse_functions - parse supported functions
 *
 * @f: supported functions struct
 * @mask: supported functions mask from ATPX
 *
 * Use the supported functions mask from ATPX function
 * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
 * are supported (all asics).
 */
static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask)
{
	f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;
	f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;
	f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;
	f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;
	f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;
	f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;
	f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;
	f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;
}

150 151 152 153 154 155 156 157 158 159
/**
 * radeon_atpx_validate_functions - validate ATPX functions
 *
 * @atpx: radeon atpx struct
 *
 * Validate that required functions are enabled (all asics).
 * returns 0 on success, error on failure.
 */
static int radeon_atpx_validate(struct radeon_atpx *atpx)
{
160
	u32 valid_bits = 0;
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	if (atpx->functions.px_params) {
		union acpi_object *info;
		struct atpx_px_params output;
		size_t size;

		info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_GET_PX_PARAMETERS, NULL);
		if (!info)
			return -EIO;

		memset(&output, 0, sizeof(output));

		size = *(u16 *) info->buffer.pointer;
		if (size < 10) {
			printk("ATPX buffer is too small: %zu\n", size);
			kfree(info);
			return -EINVAL;
		}
		size = min(sizeof(output), size);

		memcpy(&output, info->buffer.pointer, size);

		valid_bits = output.flags & output.valid_flags;
184

185 186
		kfree(info);
	}
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	/* if separate mux flag is set, mux controls are required */
	if (valid_bits & ATPX_SEPARATE_MUX_FOR_I2C) {
		atpx->functions.i2c_mux_cntl = true;
		atpx->functions.disp_mux_cntl = true;
	}
	/* if any outputs are muxed, mux controls are required */
	if (valid_bits & (ATPX_CRT1_RGB_SIGNAL_MUXED |
			  ATPX_TV_SIGNAL_MUXED |
			  ATPX_DFP_SIGNAL_MUXED))
		atpx->functions.disp_mux_cntl = true;

	/* some bioses set these bits rather than flagging power_cntl as supported */
	if (valid_bits & (ATPX_DYNAMIC_PX_SUPPORTED |
			  ATPX_DYNAMIC_DGPU_POWER_OFF_SUPPORTED))
		atpx->functions.power_cntl = true;

204
	atpx->is_hybrid = false;
205
	if (valid_bits & ATPX_MS_HYBRID_GFX_SUPPORTED) {
206
		printk("ATPX Hybrid Graphics\n");
207 208 209 210 211
		/*
		 * Disable legacy PM methods only when pcie port PM is usable,
		 * otherwise the device might fail to power off or power on.
		 */
		atpx->functions.power_cntl = !radeon_atpx_priv.bridge_pm_usable;
212
		atpx->is_hybrid = true;
213 214
	}

215 216 217
	return 0;
}

218 219 220 221 222 223 224 225 226 227
/**
 * radeon_atpx_verify_interface - verify ATPX
 *
 * @atpx: radeon atpx struct
 *
 * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
 * to initialize ATPX and determine what features are supported
 * (all asics).
 * returns 0 on success, error on failure.
 */
228
static int radeon_atpx_verify_interface(struct radeon_atpx *atpx)
229 230 231 232 233 234
{
	union acpi_object *info;
	struct atpx_verify_interface output;
	size_t size;
	int err = 0;

235
	info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
236 237 238 239 240 241 242
	if (!info)
		return -EIO;

	memset(&output, 0, sizeof(output));

	size = *(u16 *) info->buffer.pointer;
	if (size < 8) {
243
		printk("ATPX buffer is too small: %zu\n", size);
244 245 246 247 248 249 250 251
		err = -EINVAL;
		goto out;
	}
	size = min(sizeof(output), size);

	memcpy(&output, info->buffer.pointer, size);

	/* TODO: check version? */
252 253
	printk("ATPX version %u, functions 0x%08x\n",
	       output.version, output.function_bits);
254 255 256 257 258 259

	radeon_atpx_parse_functions(&atpx->functions, output.function_bits);

out:
	kfree(info);
	return err;
260 261
}

262 263 264 265 266 267 268 269 270 271
/**
 * radeon_atpx_set_discrete_state - power up/down discrete GPU
 *
 * @atpx: atpx info struct
 * @state: discrete GPU state (0 = power down, 1 = power up)
 *
 * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
 * power down/up the discrete GPU (all asics).
 * Returns 0 on success, error on failure.
 */
272
static int radeon_atpx_set_discrete_state(struct radeon_atpx *atpx, u8 state)
273
{
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	struct acpi_buffer params;
	union acpi_object *info;
	struct atpx_power_control input;

	if (atpx->functions.power_cntl) {
		input.size = 3;
		input.dgpu_state = state;
		params.length = input.size;
		params.pointer = &input;
		info = radeon_atpx_call(atpx->handle,
					ATPX_FUNCTION_POWER_CONTROL,
					&params);
		if (!info)
			return -EIO;
		kfree(info);
289 290 291 292

		/* 200ms delay is required after off */
		if (state == 0)
			msleep(200);
293 294 295 296
	}
	return 0;
}

297 298 299 300 301 302 303 304 305 306 307
/**
 * radeon_atpx_switch_disp_mux - switch display mux
 *
 * @atpx: atpx info struct
 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
 *
 * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
 * switch the display mux between the discrete GPU and integrated GPU
 * (all asics).
 * Returns 0 on success, error on failure.
 */
308
static int radeon_atpx_switch_disp_mux(struct radeon_atpx *atpx, u16 mux_id)
309
{
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	struct acpi_buffer params;
	union acpi_object *info;
	struct atpx_mux input;

	if (atpx->functions.disp_mux_cntl) {
		input.size = 4;
		input.mux = mux_id;
		params.length = input.size;
		params.pointer = &input;
		info = radeon_atpx_call(atpx->handle,
					ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
					&params);
		if (!info)
			return -EIO;
		kfree(info);
	}
	return 0;
327 328
}

329 330 331 332 333 334 335 336 337 338 339
/**
 * radeon_atpx_switch_i2c_mux - switch i2c/hpd mux
 *
 * @atpx: atpx info struct
 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
 *
 * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
 * switch the i2c/hpd mux between the discrete GPU and integrated GPU
 * (all asics).
 * Returns 0 on success, error on failure.
 */
340
static int radeon_atpx_switch_i2c_mux(struct radeon_atpx *atpx, u16 mux_id)
341
{
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	struct acpi_buffer params;
	union acpi_object *info;
	struct atpx_mux input;

	if (atpx->functions.i2c_mux_cntl) {
		input.size = 4;
		input.mux = mux_id;
		params.length = input.size;
		params.pointer = &input;
		info = radeon_atpx_call(atpx->handle,
					ATPX_FUNCTION_I2C_MUX_CONTROL,
					&params);
		if (!info)
			return -EIO;
		kfree(info);
	}
	return 0;
359 360
}

361 362 363 364 365 366 367 368 369 370 371
/**
 * radeon_atpx_switch_start - notify the sbios of a GPU switch
 *
 * @atpx: atpx info struct
 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
 *
 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX
 * function to notify the sbios that a switch between the discrete GPU and
 * integrated GPU has begun (all asics).
 * Returns 0 on success, error on failure.
 */
372
static int radeon_atpx_switch_start(struct radeon_atpx *atpx, u16 mux_id)
373
{
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	struct acpi_buffer params;
	union acpi_object *info;
	struct atpx_mux input;

	if (atpx->functions.switch_start) {
		input.size = 4;
		input.mux = mux_id;
		params.length = input.size;
		params.pointer = &input;
		info = radeon_atpx_call(atpx->handle,
					ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
					&params);
		if (!info)
			return -EIO;
		kfree(info);
	}
	return 0;
391 392
}

393 394 395 396 397 398 399 400 401 402 403
/**
 * radeon_atpx_switch_end - notify the sbios of a GPU switch
 *
 * @atpx: atpx info struct
 * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
 *
 * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
 * function to notify the sbios that a switch between the discrete GPU and
 * integrated GPU has ended (all asics).
 * Returns 0 on success, error on failure.
 */
404
static int radeon_atpx_switch_end(struct radeon_atpx *atpx, u16 mux_id)
405
{
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	struct acpi_buffer params;
	union acpi_object *info;
	struct atpx_mux input;

	if (atpx->functions.switch_end) {
		input.size = 4;
		input.mux = mux_id;
		params.length = input.size;
		params.pointer = &input;
		info = radeon_atpx_call(atpx->handle,
					ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
					&params);
		if (!info)
			return -EIO;
		kfree(info);
	}
	return 0;
423
}
424

425 426 427 428 429 430 431 432 433
/**
 * radeon_atpx_switchto - switch to the requested GPU
 *
 * @id: GPU to switch to
 *
 * Execute the necessary ATPX functions to switch between the discrete GPU and
 * integrated GPU (all asics).
 * Returns 0 on success, error on failure.
 */
434 435
static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
{
436
	u16 gpu_id;
437

438
	if (id == VGA_SWITCHEROO_IGD)
439
		gpu_id = ATPX_INTEGRATED_GPU;
440
	else
441
		gpu_id = ATPX_DISCRETE_GPU;
442

443 444 445 446
	radeon_atpx_switch_start(&radeon_atpx_priv.atpx, gpu_id);
	radeon_atpx_switch_disp_mux(&radeon_atpx_priv.atpx, gpu_id);
	radeon_atpx_switch_i2c_mux(&radeon_atpx_priv.atpx, gpu_id);
	radeon_atpx_switch_end(&radeon_atpx_priv.atpx, gpu_id);
447

448 449 450
	return 0;
}

451
/**
452
 * radeon_atpx_power_state - power down/up the requested GPU
453
 *
454
 * @id: GPU to power down/up
455 456 457 458 459 460
 * @state: requested power state (0 = off, 1 = on)
 *
 * Execute the necessary ATPX function to power down/up the discrete GPU
 * (all asics).
 * Returns 0 on success, error on failure.
 */
461 462 463 464 465 466 467
static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
				   enum vga_switcheroo_state state)
{
	/* on w500 ACPI can't change intel gpu state */
	if (id == VGA_SWITCHEROO_IGD)
		return 0;

468
	radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state);
469 470 471
	return 0;
}

472
/**
473
 * radeon_atpx_pci_probe_handle - look up the ATPX handle
474 475 476
 *
 * @pdev: pci device
 *
477
 * Look up the ATPX handles (all asics).
478 479
 * Returns true if the handles are found, false if not.
 */
480 481
static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
{
482
	acpi_handle dhandle, atpx_handle;
483 484
	acpi_status status;

485
	dhandle = ACPI_HANDLE(&pdev->dev);
486 487 488 489
	if (!dhandle)
		return false;

	status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
490
	if (ACPI_FAILURE(status))
491
		return false;
492

493
	radeon_atpx_priv.dhandle = dhandle;
494
	radeon_atpx_priv.atpx.handle = atpx_handle;
495 496 497
	return true;
}

498 499 500 501 502 503
/**
 * radeon_atpx_init - verify the ATPX interface
 *
 * Verify the ATPX interface (all asics).
 * Returns 0 on success, error on failure.
 */
504 505
static int radeon_atpx_init(void)
{
506 507
	int r;

508
	/* set up the ATPX handle */
509 510 511 512 513 514 515 516 517 518
	r = radeon_atpx_verify_interface(&radeon_atpx_priv.atpx);
	if (r)
		return r;

	/* validate the atpx setup */
	r = radeon_atpx_validate(&radeon_atpx_priv.atpx);
	if (r)
		return r;

	return 0;
519 520
}

521 522 523 524 525 526 527 528
/**
 * radeon_atpx_get_client_id - get the client id
 *
 * @pdev: pci device
 *
 * look up whether we are the integrated or discrete GPU (all asics).
 * Returns the client id.
 */
529
static enum vga_switcheroo_client_id radeon_atpx_get_client_id(struct pci_dev *pdev)
530
{
531
	if (radeon_atpx_priv.dhandle == ACPI_HANDLE(&pdev->dev))
532 533 534 535 536
		return VGA_SWITCHEROO_IGD;
	else
		return VGA_SWITCHEROO_DIS;
}

537
static const struct vga_switcheroo_handler radeon_atpx_handler = {
538 539 540 541 542
	.switchto = radeon_atpx_switchto,
	.power_state = radeon_atpx_power_state,
	.get_client_id = radeon_atpx_get_client_id,
};

543 544 545 546 547 548
/**
 * radeon_atpx_detect - detect whether we have PX
 *
 * Check if we have a PX system (all asics).
 * Returns true if we have a PX system, false if not.
 */
549 550 551 552 553 554 555
static bool radeon_atpx_detect(void)
{
	char acpi_method_name[255] = { 0 };
	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
	struct pci_dev *pdev = NULL;
	bool has_atpx = false;
	int vga_count = 0;
556 557
	bool d3_supported = false;
	struct pci_dev *parent_pdev;
558 559 560 561 562

	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
		vga_count++;

		has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
563 564 565

		parent_pdev = pci_upstream_bridge(pdev);
		d3_supported |= parent_pdev && parent_pdev->bridge_d3;
566 567
	}

568 569 570 571 572
	/* some newer PX laptops mark the dGPU as a non-VGA display device */
	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
		vga_count++;

		has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
573 574 575

		parent_pdev = pci_upstream_bridge(pdev);
		d3_supported |= parent_pdev && parent_pdev->bridge_d3;
576 577
	}

578
	if (has_atpx && vga_count == 2) {
579
		acpi_get_name(radeon_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
580 581
		pr_info("vga_switcheroo: detected switching method %s handle\n",
			acpi_method_name);
582
		radeon_atpx_priv.atpx_detected = true;
583
		radeon_atpx_priv.bridge_pm_usable = d3_supported;
584
		radeon_atpx_init();
585 586 587 588 589
		return true;
	}
	return false;
}

590 591 592 593 594
/**
 * radeon_register_atpx_handler - register with vga_switcheroo
 *
 * Register the PX callbacks with vga_switcheroo (all asics).
 */
595 596 597
void radeon_register_atpx_handler(void)
{
	bool r;
598
	enum vga_switcheroo_handler_flags_t handler_flags = 0;
599 600 601 602 603 604

	/* detect if we have any ATPX + 2 VGA in the system */
	r = radeon_atpx_detect();
	if (!r)
		return;

605
	vga_switcheroo_register_handler(&radeon_atpx_handler, handler_flags);
606 607
}

608 609 610 611 612
/**
 * radeon_unregister_atpx_handler - unregister with vga_switcheroo
 *
 * Unregister the PX callbacks with vga_switcheroo (all asics).
 */
613 614 615 616
void radeon_unregister_atpx_handler(void)
{
	vga_switcheroo_unregister_handler();
}