xref: /linux/drivers/gpu/drm/radeon/radeon_atombios.c (revision 7345797c8652c976d18dc4be4bae632557c3bc38)
1 /*
2  * Copyright 2007-8 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: Dave Airlie
24  *          Alex Deucher
25  */
26 
27 #include <linux/pci.h>
28 
29 #include <drm/drm_device.h>
30 #include <drm/drm_edid.h>
31 #include <drm/radeon_drm.h>
32 
33 #include "radeon.h"
34 
35 #include "atom.h"
36 #include "atom-bits.h"
37 #include "radeon_asic.h"
38 #include "radeon_atombios.h"
39 #include "radeon_legacy_encoders.h"
40 
41 union atom_supported_devices {
42 	struct _ATOM_SUPPORTED_DEVICES_INFO info;
43 	struct _ATOM_SUPPORTED_DEVICES_INFO_2 info_2;
44 	struct _ATOM_SUPPORTED_DEVICES_INFO_2d1 info_2d1;
45 };
46 
47 static void radeon_lookup_i2c_gpio_quirks(struct radeon_device *rdev,
48 					  ATOM_GPIO_I2C_ASSIGMENT *gpio,
49 					  u8 index)
50 {
51 	/* r4xx mask is technically not used by the hw, so patch in the legacy mask bits */
52 	if ((rdev->family == CHIP_R420) ||
53 	    (rdev->family == CHIP_R423) ||
54 	    (rdev->family == CHIP_RV410)) {
55 		if ((le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x0018) ||
56 		    (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x0019) ||
57 		    (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x001a)) {
58 			gpio->ucClkMaskShift = 0x19;
59 			gpio->ucDataMaskShift = 0x18;
60 		}
61 	}
62 
63 	/* some evergreen boards have bad data for this entry */
64 	if (ASIC_IS_DCE4(rdev)) {
65 		if ((index == 7) &&
66 		    (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x1936) &&
67 		    (gpio->sucI2cId.ucAccess == 0)) {
68 			gpio->sucI2cId.ucAccess = 0x97;
69 			gpio->ucDataMaskShift = 8;
70 			gpio->ucDataEnShift = 8;
71 			gpio->ucDataY_Shift = 8;
72 			gpio->ucDataA_Shift = 8;
73 		}
74 	}
75 
76 	/* some DCE3 boards have bad data for this entry */
77 	if (ASIC_IS_DCE3(rdev)) {
78 		if ((index == 4) &&
79 		    (le16_to_cpu(gpio->usClkMaskRegisterIndex) == 0x1fda) &&
80 		    (gpio->sucI2cId.ucAccess == 0x94))
81 			gpio->sucI2cId.ucAccess = 0x14;
82 	}
83 }
84 
85 static struct radeon_i2c_bus_rec radeon_get_bus_rec_for_i2c_gpio(ATOM_GPIO_I2C_ASSIGMENT *gpio)
86 {
87 	struct radeon_i2c_bus_rec i2c;
88 
89 	memset(&i2c, 0, sizeof(struct radeon_i2c_bus_rec));
90 
91 	i2c.mask_clk_reg = le16_to_cpu(gpio->usClkMaskRegisterIndex) * 4;
92 	i2c.mask_data_reg = le16_to_cpu(gpio->usDataMaskRegisterIndex) * 4;
93 	i2c.en_clk_reg = le16_to_cpu(gpio->usClkEnRegisterIndex) * 4;
94 	i2c.en_data_reg = le16_to_cpu(gpio->usDataEnRegisterIndex) * 4;
95 	i2c.y_clk_reg = le16_to_cpu(gpio->usClkY_RegisterIndex) * 4;
96 	i2c.y_data_reg = le16_to_cpu(gpio->usDataY_RegisterIndex) * 4;
97 	i2c.a_clk_reg = le16_to_cpu(gpio->usClkA_RegisterIndex) * 4;
98 	i2c.a_data_reg = le16_to_cpu(gpio->usDataA_RegisterIndex) * 4;
99 	i2c.mask_clk_mask = (1 << gpio->ucClkMaskShift);
100 	i2c.mask_data_mask = (1 << gpio->ucDataMaskShift);
101 	i2c.en_clk_mask = (1 << gpio->ucClkEnShift);
102 	i2c.en_data_mask = (1 << gpio->ucDataEnShift);
103 	i2c.y_clk_mask = (1 << gpio->ucClkY_Shift);
104 	i2c.y_data_mask = (1 << gpio->ucDataY_Shift);
105 	i2c.a_clk_mask = (1 << gpio->ucClkA_Shift);
106 	i2c.a_data_mask = (1 << gpio->ucDataA_Shift);
107 
108 	if (gpio->sucI2cId.sbfAccess.bfHW_Capable)
109 		i2c.hw_capable = true;
110 	else
111 		i2c.hw_capable = false;
112 
113 	if (gpio->sucI2cId.ucAccess == 0xa0)
114 		i2c.mm_i2c = true;
115 	else
116 		i2c.mm_i2c = false;
117 
118 	i2c.i2c_id = gpio->sucI2cId.ucAccess;
119 
120 	if (i2c.mask_clk_reg)
121 		i2c.valid = true;
122 	else
123 		i2c.valid = false;
124 
125 	return i2c;
126 }
127 
128 /*
129  * The VBIOS table size is not checked against the image; cap the
130  * entry count to RADEON_MAX_I2C_BUS, the size of rdev->i2c_bus[].
131  */
132 static int radeon_atombios_gpio_i2c_num_entries(uint16_t size)
133 {
134 	u32 bytes;
135 
136 	if (size < sizeof(ATOM_COMMON_TABLE_HEADER))
137 		return 0;
138 
139 	bytes = size - sizeof(ATOM_COMMON_TABLE_HEADER);
140 	return min_t(u32, bytes / sizeof(ATOM_GPIO_I2C_ASSIGMENT),
141 		     RADEON_MAX_I2C_BUS);
142 }
143 
144 static struct radeon_i2c_bus_rec radeon_lookup_i2c_gpio(struct radeon_device *rdev,
145 							       uint8_t id)
146 {
147 	struct atom_context *ctx = rdev->mode_info.atom_context;
148 	ATOM_GPIO_I2C_ASSIGMENT *gpio;
149 	struct radeon_i2c_bus_rec i2c;
150 	int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
151 	struct _ATOM_GPIO_I2C_INFO *i2c_info;
152 	uint16_t data_offset, size;
153 	int i, num_indices;
154 
155 	memset(&i2c, 0, sizeof(struct radeon_i2c_bus_rec));
156 	i2c.valid = false;
157 
158 	if (atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
159 		i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
160 
161 		num_indices = radeon_atombios_gpio_i2c_num_entries(size);
162 
163 		gpio = &i2c_info->asGPIO_Info[0];
164 		for (i = 0; i < num_indices; i++) {
165 
166 			radeon_lookup_i2c_gpio_quirks(rdev, gpio, i);
167 
168 			if (gpio->sucI2cId.ucAccess == id) {
169 				i2c = radeon_get_bus_rec_for_i2c_gpio(gpio);
170 				break;
171 			}
172 			gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
173 				((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
174 		}
175 	}
176 
177 	return i2c;
178 }
179 
180 void radeon_atombios_i2c_init(struct radeon_device *rdev)
181 {
182 	struct atom_context *ctx = rdev->mode_info.atom_context;
183 	ATOM_GPIO_I2C_ASSIGMENT *gpio;
184 	struct radeon_i2c_bus_rec i2c;
185 	int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
186 	struct _ATOM_GPIO_I2C_INFO *i2c_info;
187 	uint16_t data_offset, size;
188 	int i, num_indices;
189 	char stmp[32];
190 
191 	if (atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
192 		i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
193 
194 		num_indices = radeon_atombios_gpio_i2c_num_entries(size);
195 
196 		gpio = &i2c_info->asGPIO_Info[0];
197 		for (i = 0; i < num_indices; i++) {
198 			radeon_lookup_i2c_gpio_quirks(rdev, gpio, i);
199 
200 			i2c = radeon_get_bus_rec_for_i2c_gpio(gpio);
201 
202 			if (i2c.valid) {
203 				sprintf(stmp, "0x%x", i2c.i2c_id);
204 				rdev->i2c_bus[i] = radeon_i2c_create(rdev_to_drm(rdev), &i2c, stmp);
205 			}
206 			gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
207 				((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
208 		}
209 	}
210 }
211 
212 struct radeon_gpio_rec radeon_atombios_lookup_gpio(struct radeon_device *rdev,
213 						   u8 id)
214 {
215 	struct atom_context *ctx = rdev->mode_info.atom_context;
216 	struct radeon_gpio_rec gpio;
217 	int index = GetIndexIntoMasterTable(DATA, GPIO_Pin_LUT);
218 	struct _ATOM_GPIO_PIN_LUT *gpio_info;
219 	ATOM_GPIO_PIN_ASSIGNMENT *pin;
220 	u16 data_offset, size;
221 	int i, num_indices;
222 
223 	memset(&gpio, 0, sizeof(struct radeon_gpio_rec));
224 	gpio.valid = false;
225 
226 	if (atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
227 		gpio_info = (struct _ATOM_GPIO_PIN_LUT *)(ctx->bios + data_offset);
228 
229 		num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
230 			sizeof(ATOM_GPIO_PIN_ASSIGNMENT);
231 
232 		pin = gpio_info->asGPIO_Pin;
233 		for (i = 0; i < num_indices; i++) {
234 			if (id == pin->ucGPIO_ID) {
235 				gpio.id = pin->ucGPIO_ID;
236 				gpio.reg = le16_to_cpu(pin->usGpioPin_AIndex) * 4;
237 				gpio.shift = pin->ucGpioPinBitShift;
238 				gpio.mask = (1 << pin->ucGpioPinBitShift);
239 				gpio.valid = true;
240 				break;
241 			}
242 			pin = (ATOM_GPIO_PIN_ASSIGNMENT *)
243 				((u8 *)pin + sizeof(ATOM_GPIO_PIN_ASSIGNMENT));
244 		}
245 	}
246 
247 	return gpio;
248 }
249 
250 static struct radeon_hpd radeon_atom_get_hpd_info_from_gpio(struct radeon_device *rdev,
251 							    struct radeon_gpio_rec *gpio)
252 {
253 	struct radeon_hpd hpd;
254 	u32 reg;
255 
256 	memset(&hpd, 0, sizeof(struct radeon_hpd));
257 
258 	if (ASIC_IS_DCE6(rdev))
259 		reg = SI_DC_GPIO_HPD_A;
260 	else if (ASIC_IS_DCE4(rdev))
261 		reg = EVERGREEN_DC_GPIO_HPD_A;
262 	else
263 		reg = AVIVO_DC_GPIO_HPD_A;
264 
265 	hpd.gpio = *gpio;
266 	if (gpio->reg == reg) {
267 		switch(gpio->mask) {
268 		case (1 << 0):
269 			hpd.hpd = RADEON_HPD_1;
270 			break;
271 		case (1 << 8):
272 			hpd.hpd = RADEON_HPD_2;
273 			break;
274 		case (1 << 16):
275 			hpd.hpd = RADEON_HPD_3;
276 			break;
277 		case (1 << 24):
278 			hpd.hpd = RADEON_HPD_4;
279 			break;
280 		case (1 << 26):
281 			hpd.hpd = RADEON_HPD_5;
282 			break;
283 		case (1 << 28):
284 			hpd.hpd = RADEON_HPD_6;
285 			break;
286 		default:
287 			hpd.hpd = RADEON_HPD_NONE;
288 			break;
289 		}
290 	} else
291 		hpd.hpd = RADEON_HPD_NONE;
292 	return hpd;
293 }
294 
295 static bool radeon_atom_apply_quirks(struct drm_device *dev,
296 				     uint32_t supported_device,
297 				     int *connector_type,
298 				     struct radeon_i2c_bus_rec *i2c_bus,
299 				     uint16_t *line_mux,
300 				     struct radeon_hpd *hpd)
301 {
302 	struct pci_dev *pdev = to_pci_dev(dev->dev);
303 
304 	/* Asus M2A-VM HDMI board lists the DVI port as HDMI */
305 	if ((pdev->device == 0x791e) &&
306 	    (pdev->subsystem_vendor == 0x1043) &&
307 	    (pdev->subsystem_device == 0x826d)) {
308 		if ((*connector_type == DRM_MODE_CONNECTOR_HDMIA) &&
309 		    (supported_device == ATOM_DEVICE_DFP3_SUPPORT))
310 			*connector_type = DRM_MODE_CONNECTOR_DVID;
311 	}
312 
313 	/* Asrock RS600 board lists the DVI port as HDMI */
314 	if ((pdev->device == 0x7941) &&
315 	    (pdev->subsystem_vendor == 0x1849) &&
316 	    (pdev->subsystem_device == 0x7941)) {
317 		if ((*connector_type == DRM_MODE_CONNECTOR_HDMIA) &&
318 		    (supported_device == ATOM_DEVICE_DFP3_SUPPORT))
319 			*connector_type = DRM_MODE_CONNECTOR_DVID;
320 	}
321 
322 	/* MSI K9A2GM V2/V3 board has no HDMI or DVI */
323 	if ((pdev->device == 0x796e) &&
324 	    (pdev->subsystem_vendor == 0x1462) &&
325 	    (pdev->subsystem_device == 0x7302)) {
326 		if ((supported_device == ATOM_DEVICE_DFP2_SUPPORT) ||
327 		    (supported_device == ATOM_DEVICE_DFP3_SUPPORT))
328 			return false;
329 	}
330 
331 	/* a-bit f-i90hd - ciaranm on #radeonhd - this board has no DVI */
332 	if ((pdev->device == 0x7941) &&
333 	    (pdev->subsystem_vendor == 0x147b) &&
334 	    (pdev->subsystem_device == 0x2412)) {
335 		if (*connector_type == DRM_MODE_CONNECTOR_DVII)
336 			return false;
337 	}
338 
339 	/* Falcon NW laptop lists vga ddc line for LVDS */
340 	if ((pdev->device == 0x5653) &&
341 	    (pdev->subsystem_vendor == 0x1462) &&
342 	    (pdev->subsystem_device == 0x0291)) {
343 		if (*connector_type == DRM_MODE_CONNECTOR_LVDS) {
344 			i2c_bus->valid = false;
345 			*line_mux = 53;
346 		}
347 	}
348 
349 	/* HIS X1300 is DVI+VGA, not DVI+DVI */
350 	if ((pdev->device == 0x7146) &&
351 	    (pdev->subsystem_vendor == 0x17af) &&
352 	    (pdev->subsystem_device == 0x2058)) {
353 		if (supported_device == ATOM_DEVICE_DFP1_SUPPORT)
354 			return false;
355 	}
356 
357 	/* Gigabyte X1300 is DVI+VGA, not DVI+DVI */
358 	if ((pdev->device == 0x7142) &&
359 	    (pdev->subsystem_vendor == 0x1458) &&
360 	    (pdev->subsystem_device == 0x2134)) {
361 		if (supported_device == ATOM_DEVICE_DFP1_SUPPORT)
362 			return false;
363 	}
364 
365 
366 	/* Funky macbooks */
367 	if ((pdev->device == 0x71C5) &&
368 	    (pdev->subsystem_vendor == 0x106b) &&
369 	    (pdev->subsystem_device == 0x0080)) {
370 		if ((supported_device == ATOM_DEVICE_CRT1_SUPPORT) ||
371 		    (supported_device == ATOM_DEVICE_DFP2_SUPPORT))
372 			return false;
373 		if (supported_device == ATOM_DEVICE_CRT2_SUPPORT)
374 			*line_mux = 0x90;
375 	}
376 
377 	/* mac rv630, rv730, others */
378 	if ((supported_device == ATOM_DEVICE_TV1_SUPPORT) &&
379 	    (*connector_type == DRM_MODE_CONNECTOR_DVII)) {
380 		*connector_type = DRM_MODE_CONNECTOR_9PinDIN;
381 		*line_mux = CONNECTOR_7PIN_DIN_ENUM_ID1;
382 	}
383 
384 	/* ASUS HD 3600 XT board lists the DVI port as HDMI */
385 	if ((pdev->device == 0x9598) &&
386 	    (pdev->subsystem_vendor == 0x1043) &&
387 	    (pdev->subsystem_device == 0x01da)) {
388 		if (*connector_type == DRM_MODE_CONNECTOR_HDMIA) {
389 			*connector_type = DRM_MODE_CONNECTOR_DVII;
390 		}
391 	}
392 
393 	/* ASUS HD 3600 board lists the DVI port as HDMI */
394 	if ((pdev->device == 0x9598) &&
395 	    (pdev->subsystem_vendor == 0x1043) &&
396 	    (pdev->subsystem_device == 0x01e4)) {
397 		if (*connector_type == DRM_MODE_CONNECTOR_HDMIA) {
398 			*connector_type = DRM_MODE_CONNECTOR_DVII;
399 		}
400 	}
401 
402 	/* ASUS HD 3450 board lists the DVI port as HDMI */
403 	if ((pdev->device == 0x95C5) &&
404 	    (pdev->subsystem_vendor == 0x1043) &&
405 	    (pdev->subsystem_device == 0x01e2)) {
406 		if (*connector_type == DRM_MODE_CONNECTOR_HDMIA) {
407 			*connector_type = DRM_MODE_CONNECTOR_DVII;
408 		}
409 	}
410 
411 	/* some BIOSes seem to report DAC on HDMI - usually this is a board with
412 	 * HDMI + VGA reporting as HDMI
413 	 */
414 	if (*connector_type == DRM_MODE_CONNECTOR_HDMIA) {
415 		if (supported_device & (ATOM_DEVICE_CRT_SUPPORT)) {
416 			*connector_type = DRM_MODE_CONNECTOR_VGA;
417 			*line_mux = 0;
418 		}
419 	}
420 
421 	/* Acer laptop (Acer TravelMate 5730/5730G) has an HDMI port
422 	 * on the laptop and a DVI port on the docking station and
423 	 * both share the same encoder, hpd pin, and ddc line.
424 	 * So while the bios table is technically correct,
425 	 * we drop the DVI port here since xrandr has no concept of
426 	 * encoders and will try and drive both connectors
427 	 * with different crtcs which isn't possible on the hardware
428 	 * side and leaves no crtcs for LVDS or VGA.
429 	 */
430 	if (((pdev->device == 0x95c4) || (pdev->device == 0x9591)) &&
431 	    (pdev->subsystem_vendor == 0x1025) &&
432 	    (pdev->subsystem_device == 0x013c)) {
433 		if ((*connector_type == DRM_MODE_CONNECTOR_DVII) &&
434 		    (supported_device == ATOM_DEVICE_DFP1_SUPPORT)) {
435 			/* actually it's a DVI-D port not DVI-I */
436 			*connector_type = DRM_MODE_CONNECTOR_DVID;
437 			return false;
438 		}
439 	}
440 
441 	/* XFX Pine Group device rv730 reports no VGA DDC lines
442 	 * even though they are wired up to record 0x93
443 	 */
444 	if ((pdev->device == 0x9498) &&
445 	    (pdev->subsystem_vendor == 0x1682) &&
446 	    (pdev->subsystem_device == 0x2452) &&
447 	    (i2c_bus->valid == false) &&
448 	    !(supported_device & (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))) {
449 		struct radeon_device *rdev = dev->dev_private;
450 		*i2c_bus = radeon_lookup_i2c_gpio(rdev, 0x93);
451 	}
452 
453 	/* Fujitsu D3003-S2 board lists DVI-I as DVI-D and VGA */
454 	if (((pdev->device == 0x9802) ||
455 	     (pdev->device == 0x9805) ||
456 	     (pdev->device == 0x9806)) &&
457 	    (pdev->subsystem_vendor == 0x1734) &&
458 	    (pdev->subsystem_device == 0x11bd)) {
459 		if (*connector_type == DRM_MODE_CONNECTOR_VGA) {
460 			*connector_type = DRM_MODE_CONNECTOR_DVII;
461 			*line_mux = 0x3103;
462 		} else if (*connector_type == DRM_MODE_CONNECTOR_DVID) {
463 			*connector_type = DRM_MODE_CONNECTOR_DVII;
464 		}
465 	}
466 
467 	return true;
468 }
469 
470 static const int supported_devices_connector_convert[] = {
471 	DRM_MODE_CONNECTOR_Unknown,
472 	DRM_MODE_CONNECTOR_VGA,
473 	DRM_MODE_CONNECTOR_DVII,
474 	DRM_MODE_CONNECTOR_DVID,
475 	DRM_MODE_CONNECTOR_DVIA,
476 	DRM_MODE_CONNECTOR_SVIDEO,
477 	DRM_MODE_CONNECTOR_Composite,
478 	DRM_MODE_CONNECTOR_LVDS,
479 	DRM_MODE_CONNECTOR_Unknown,
480 	DRM_MODE_CONNECTOR_Unknown,
481 	DRM_MODE_CONNECTOR_HDMIA,
482 	DRM_MODE_CONNECTOR_HDMIB,
483 	DRM_MODE_CONNECTOR_Unknown,
484 	DRM_MODE_CONNECTOR_Unknown,
485 	DRM_MODE_CONNECTOR_9PinDIN,
486 	DRM_MODE_CONNECTOR_DisplayPort
487 };
488 
489 static const uint16_t supported_devices_connector_object_id_convert[] = {
490 	CONNECTOR_OBJECT_ID_NONE,
491 	CONNECTOR_OBJECT_ID_VGA,
492 	CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I, /* not all boards support DL */
493 	CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D, /* not all boards support DL */
494 	CONNECTOR_OBJECT_ID_VGA, /* technically DVI-A */
495 	CONNECTOR_OBJECT_ID_COMPOSITE,
496 	CONNECTOR_OBJECT_ID_SVIDEO,
497 	CONNECTOR_OBJECT_ID_LVDS,
498 	CONNECTOR_OBJECT_ID_9PIN_DIN,
499 	CONNECTOR_OBJECT_ID_9PIN_DIN,
500 	CONNECTOR_OBJECT_ID_DISPLAYPORT,
501 	CONNECTOR_OBJECT_ID_HDMI_TYPE_A,
502 	CONNECTOR_OBJECT_ID_HDMI_TYPE_B,
503 	CONNECTOR_OBJECT_ID_SVIDEO
504 };
505 
506 static const int object_connector_convert[] = {
507 	DRM_MODE_CONNECTOR_Unknown,
508 	DRM_MODE_CONNECTOR_DVII,
509 	DRM_MODE_CONNECTOR_DVII,
510 	DRM_MODE_CONNECTOR_DVID,
511 	DRM_MODE_CONNECTOR_DVID,
512 	DRM_MODE_CONNECTOR_VGA,
513 	DRM_MODE_CONNECTOR_Composite,
514 	DRM_MODE_CONNECTOR_SVIDEO,
515 	DRM_MODE_CONNECTOR_Unknown,
516 	DRM_MODE_CONNECTOR_Unknown,
517 	DRM_MODE_CONNECTOR_9PinDIN,
518 	DRM_MODE_CONNECTOR_Unknown,
519 	DRM_MODE_CONNECTOR_HDMIA,
520 	DRM_MODE_CONNECTOR_HDMIB,
521 	DRM_MODE_CONNECTOR_LVDS,
522 	DRM_MODE_CONNECTOR_9PinDIN,
523 	DRM_MODE_CONNECTOR_Unknown,
524 	DRM_MODE_CONNECTOR_Unknown,
525 	DRM_MODE_CONNECTOR_Unknown,
526 	DRM_MODE_CONNECTOR_DisplayPort,
527 	DRM_MODE_CONNECTOR_eDP,
528 	DRM_MODE_CONNECTOR_Unknown
529 };
530 
531 bool radeon_get_atom_connector_info_from_object_table(struct drm_device *dev)
532 {
533 	struct radeon_device *rdev = dev->dev_private;
534 	struct radeon_mode_info *mode_info = &rdev->mode_info;
535 	struct atom_context *ctx = mode_info->atom_context;
536 	int index = GetIndexIntoMasterTable(DATA, Object_Header);
537 	u16 size, data_offset;
538 	u8 frev, crev;
539 	ATOM_CONNECTOR_OBJECT_TABLE *con_obj;
540 	ATOM_ENCODER_OBJECT_TABLE *enc_obj;
541 	ATOM_OBJECT_TABLE *router_obj;
542 	ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
543 	ATOM_OBJECT_HEADER *obj_header;
544 	int i, j, k, path_size, device_support;
545 	int connector_type;
546 	u16 igp_lane_info, conn_id, connector_object_id;
547 	struct radeon_i2c_bus_rec ddc_bus;
548 	struct radeon_router router;
549 	struct radeon_gpio_rec gpio;
550 	struct radeon_hpd hpd;
551 
552 	if (!atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
553 		return false;
554 
555 	if (crev < 2)
556 		return false;
557 
558 	obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
559 	path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
560 	    (ctx->bios + data_offset +
561 	     le16_to_cpu(obj_header->usDisplayPathTableOffset));
562 	con_obj = (ATOM_CONNECTOR_OBJECT_TABLE *)
563 	    (ctx->bios + data_offset +
564 	     le16_to_cpu(obj_header->usConnectorObjectTableOffset));
565 	enc_obj = (ATOM_ENCODER_OBJECT_TABLE *)
566 	    (ctx->bios + data_offset +
567 	     le16_to_cpu(obj_header->usEncoderObjectTableOffset));
568 	router_obj = (ATOM_OBJECT_TABLE *)
569 		(ctx->bios + data_offset +
570 		 le16_to_cpu(obj_header->usRouterObjectTableOffset));
571 	device_support = le16_to_cpu(obj_header->usDeviceSupport);
572 
573 	path_size = 0;
574 	for (i = 0; i < path_obj->ucNumOfDispPath; i++) {
575 		uint8_t *addr = (uint8_t *) path_obj->asDispPath;
576 		ATOM_DISPLAY_OBJECT_PATH *path;
577 		addr += path_size;
578 		path = (ATOM_DISPLAY_OBJECT_PATH *) addr;
579 		path_size += le16_to_cpu(path->usSize);
580 
581 		if (device_support & le16_to_cpu(path->usDeviceTag)) {
582 			uint8_t con_obj_id, con_obj_num;
583 
584 			con_obj_id =
585 			    (le16_to_cpu(path->usConnObjectId) & OBJECT_ID_MASK)
586 			    >> OBJECT_ID_SHIFT;
587 			con_obj_num =
588 			    (le16_to_cpu(path->usConnObjectId) & ENUM_ID_MASK)
589 			    >> ENUM_ID_SHIFT;
590 
591 			/* TODO CV support */
592 			if (le16_to_cpu(path->usDeviceTag) ==
593 				ATOM_DEVICE_CV_SUPPORT)
594 				continue;
595 
596 			/* IGP chips */
597 			if ((rdev->flags & RADEON_IS_IGP) &&
598 			    (con_obj_id ==
599 			     CONNECTOR_OBJECT_ID_PCIE_CONNECTOR)) {
600 				uint16_t igp_offset = 0;
601 				ATOM_INTEGRATED_SYSTEM_INFO_V2 *igp_obj;
602 
603 				index =
604 				    GetIndexIntoMasterTable(DATA,
605 							    IntegratedSystemInfo);
606 
607 				if (atom_parse_data_header(ctx, index, &size, &frev,
608 							   &crev, &igp_offset)) {
609 
610 					if (crev >= 2) {
611 						igp_obj =
612 							(ATOM_INTEGRATED_SYSTEM_INFO_V2
613 							 *) (ctx->bios + igp_offset);
614 
615 						if (igp_obj) {
616 							uint32_t slot_config, ct;
617 
618 							if (con_obj_num == 1)
619 								slot_config =
620 									igp_obj->
621 									ulDDISlot1Config;
622 							else
623 								slot_config =
624 									igp_obj->
625 									ulDDISlot2Config;
626 
627 							ct = (slot_config >> 16) & 0xff;
628 							connector_type =
629 								object_connector_convert
630 								[ct];
631 							connector_object_id = ct;
632 							igp_lane_info =
633 								slot_config & 0xffff;
634 						} else
635 							continue;
636 					} else
637 						continue;
638 				} else {
639 					igp_lane_info = 0;
640 					connector_type =
641 						object_connector_convert[con_obj_id];
642 					connector_object_id = con_obj_id;
643 				}
644 			} else {
645 				igp_lane_info = 0;
646 				connector_type =
647 				    object_connector_convert[con_obj_id];
648 				connector_object_id = con_obj_id;
649 			}
650 
651 			if (connector_type == DRM_MODE_CONNECTOR_Unknown)
652 				continue;
653 
654 			router.ddc_valid = false;
655 			router.cd_valid = false;
656 			for (j = 0; j < ((le16_to_cpu(path->usSize) - 8) / 2); j++) {
657 				uint8_t grph_obj_type =
658 				    (le16_to_cpu(path->usGraphicObjIds[j]) &
659 				     OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
660 
661 				if (grph_obj_type == GRAPH_OBJECT_TYPE_ENCODER) {
662 					for (k = 0; k < enc_obj->ucNumberOfObjects; k++) {
663 						u16 encoder_obj = le16_to_cpu(enc_obj->asObjects[k].usObjectID);
664 						if (le16_to_cpu(path->usGraphicObjIds[j]) == encoder_obj) {
665 							ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
666 								(ctx->bios + data_offset +
667 								 le16_to_cpu(enc_obj->asObjects[k].usRecordOffset));
668 							ATOM_ENCODER_CAP_RECORD *cap_record;
669 							u16 caps = 0;
670 
671 							while (record->ucRecordSize > 0 &&
672 							       record->ucRecordType > 0 &&
673 							       record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
674 								switch (record->ucRecordType) {
675 								case ATOM_ENCODER_CAP_RECORD_TYPE:
676 									cap_record =(ATOM_ENCODER_CAP_RECORD *)
677 										record;
678 									caps = le16_to_cpu(cap_record->usEncoderCap);
679 									break;
680 								}
681 								record = (ATOM_COMMON_RECORD_HEADER *)
682 									((char *)record + record->ucRecordSize);
683 							}
684 							radeon_add_atom_encoder(dev,
685 										encoder_obj,
686 										le16_to_cpu
687 										(path->
688 										 usDeviceTag),
689 										caps);
690 						}
691 					}
692 				} else if (grph_obj_type == GRAPH_OBJECT_TYPE_ROUTER) {
693 					for (k = 0; k < router_obj->ucNumberOfObjects; k++) {
694 						u16 router_obj_id = le16_to_cpu(router_obj->asObjects[k].usObjectID);
695 						if (le16_to_cpu(path->usGraphicObjIds[j]) == router_obj_id) {
696 							ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
697 								(ctx->bios + data_offset +
698 								 le16_to_cpu(router_obj->asObjects[k].usRecordOffset));
699 							ATOM_I2C_RECORD *i2c_record;
700 							ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
701 							ATOM_ROUTER_DDC_PATH_SELECT_RECORD *ddc_path;
702 							ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *cd_path;
703 							ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *router_src_dst_table =
704 								(ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *)
705 								(ctx->bios + data_offset +
706 								 le16_to_cpu(router_obj->asObjects[k].usSrcDstTableOffset));
707 							u8 *num_dst_objs = (u8 *)
708 								((u8 *)router_src_dst_table + 1 +
709 								 (router_src_dst_table->ucNumberOfSrc * 2));
710 							u16 *dst_objs = (u16 *)(num_dst_objs + 1);
711 							int enum_id;
712 
713 							router.router_id = router_obj_id;
714 							for (enum_id = 0; enum_id < (*num_dst_objs); enum_id++) {
715 								if (le16_to_cpu(path->usConnObjectId) ==
716 								    le16_to_cpu(dst_objs[enum_id]))
717 									break;
718 							}
719 
720 							while (record->ucRecordSize > 0 &&
721 							       record->ucRecordType > 0 &&
722 							       record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
723 								switch (record->ucRecordType) {
724 								case ATOM_I2C_RECORD_TYPE:
725 									i2c_record =
726 										(ATOM_I2C_RECORD *)
727 										record;
728 									i2c_config =
729 										(ATOM_I2C_ID_CONFIG_ACCESS *)
730 										&i2c_record->sucI2cId;
731 									router.i2c_info =
732 										radeon_lookup_i2c_gpio(rdev,
733 												       i2c_config->
734 												       ucAccess);
735 									router.i2c_addr = i2c_record->ucI2CAddr >> 1;
736 									break;
737 								case ATOM_ROUTER_DDC_PATH_SELECT_RECORD_TYPE:
738 									ddc_path = (ATOM_ROUTER_DDC_PATH_SELECT_RECORD *)
739 										record;
740 									router.ddc_valid = true;
741 									router.ddc_mux_type = ddc_path->ucMuxType;
742 									router.ddc_mux_control_pin = ddc_path->ucMuxControlPin;
743 									router.ddc_mux_state = ddc_path->ucMuxState[enum_id];
744 									break;
745 								case ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD_TYPE:
746 									cd_path = (ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *)
747 										record;
748 									router.cd_valid = true;
749 									router.cd_mux_type = cd_path->ucMuxType;
750 									router.cd_mux_control_pin = cd_path->ucMuxControlPin;
751 									router.cd_mux_state = cd_path->ucMuxState[enum_id];
752 									break;
753 								}
754 								record = (ATOM_COMMON_RECORD_HEADER *)
755 									((char *)record + record->ucRecordSize);
756 							}
757 						}
758 					}
759 				}
760 			}
761 
762 			/* look up gpio for ddc, hpd */
763 			ddc_bus.valid = false;
764 			hpd.hpd = RADEON_HPD_NONE;
765 			if ((le16_to_cpu(path->usDeviceTag) &
766 			     (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT)) == 0) {
767 				for (j = 0; j < con_obj->ucNumberOfObjects; j++) {
768 					if (le16_to_cpu(path->usConnObjectId) ==
769 					    le16_to_cpu(con_obj->asObjects[j].
770 							usObjectID)) {
771 						ATOM_COMMON_RECORD_HEADER
772 						    *record =
773 						    (ATOM_COMMON_RECORD_HEADER
774 						     *)
775 						    (ctx->bios + data_offset +
776 						     le16_to_cpu(con_obj->
777 								 asObjects[j].
778 								 usRecordOffset));
779 						ATOM_I2C_RECORD *i2c_record;
780 						ATOM_HPD_INT_RECORD *hpd_record;
781 						ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
782 
783 						while (record->ucRecordSize > 0 &&
784 						       record->ucRecordType > 0 &&
785 						       record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
786 							switch (record->ucRecordType) {
787 							case ATOM_I2C_RECORD_TYPE:
788 								i2c_record =
789 								    (ATOM_I2C_RECORD *)
790 									record;
791 								i2c_config =
792 									(ATOM_I2C_ID_CONFIG_ACCESS *)
793 									&i2c_record->sucI2cId;
794 								ddc_bus = radeon_lookup_i2c_gpio(rdev,
795 												 i2c_config->
796 												 ucAccess);
797 								break;
798 							case ATOM_HPD_INT_RECORD_TYPE:
799 								hpd_record =
800 									(ATOM_HPD_INT_RECORD *)
801 									record;
802 								gpio = radeon_atombios_lookup_gpio(rdev,
803 											  hpd_record->ucHPDIntGPIOID);
804 								hpd = radeon_atom_get_hpd_info_from_gpio(rdev, &gpio);
805 								hpd.plugged_state = hpd_record->ucPlugged_PinState;
806 								break;
807 							}
808 							record =
809 							    (ATOM_COMMON_RECORD_HEADER
810 							     *) ((char *)record
811 								 +
812 								 record->
813 								 ucRecordSize);
814 						}
815 						break;
816 					}
817 				}
818 			}
819 
820 			/* needed for aux chan transactions */
821 			ddc_bus.hpd = hpd.hpd;
822 
823 			conn_id = le16_to_cpu(path->usConnObjectId);
824 
825 			if (!radeon_atom_apply_quirks
826 			    (dev, le16_to_cpu(path->usDeviceTag), &connector_type,
827 			     &ddc_bus, &conn_id, &hpd))
828 				continue;
829 
830 			radeon_add_atom_connector(dev,
831 						  conn_id,
832 						  le16_to_cpu(path->
833 							      usDeviceTag),
834 						  connector_type, &ddc_bus,
835 						  igp_lane_info,
836 						  connector_object_id,
837 						  &hpd,
838 						  &router);
839 
840 		}
841 	}
842 
843 	radeon_link_encoder_connector(dev);
844 	return true;
845 }
846 
847 static uint16_t atombios_get_connector_object_id(struct drm_device *dev,
848 						 int connector_type,
849 						 uint16_t devices)
850 {
851 	struct radeon_device *rdev = dev->dev_private;
852 
853 	if (rdev->flags & RADEON_IS_IGP) {
854 		return supported_devices_connector_object_id_convert
855 			[connector_type];
856 	} else if (((connector_type == DRM_MODE_CONNECTOR_DVII) ||
857 		    (connector_type == DRM_MODE_CONNECTOR_DVID)) &&
858 		   (devices & ATOM_DEVICE_DFP2_SUPPORT))  {
859 		struct radeon_mode_info *mode_info = &rdev->mode_info;
860 		struct atom_context *ctx = mode_info->atom_context;
861 		int index = GetIndexIntoMasterTable(DATA, XTMDS_Info);
862 		uint16_t size, data_offset;
863 		uint8_t frev, crev;
864 		ATOM_XTMDS_INFO *xtmds;
865 
866 		if (atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset)) {
867 			xtmds = (ATOM_XTMDS_INFO *)(ctx->bios + data_offset);
868 
869 			if (xtmds->ucSupportedLink & ATOM_XTMDS_SUPPORTED_DUALLINK) {
870 				if (connector_type == DRM_MODE_CONNECTOR_DVII)
871 					return CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_I;
872 				else
873 					return CONNECTOR_OBJECT_ID_DUAL_LINK_DVI_D;
874 			} else {
875 				if (connector_type == DRM_MODE_CONNECTOR_DVII)
876 					return CONNECTOR_OBJECT_ID_SINGLE_LINK_DVI_I;
877 				else
878 					return CONNECTOR_OBJECT_ID_SINGLE_LINK_DVI_D;
879 			}
880 		} else
881 			return supported_devices_connector_object_id_convert
882 				[connector_type];
883 	} else {
884 		return supported_devices_connector_object_id_convert
885 			[connector_type];
886 	}
887 }
888 
889 struct bios_connector {
890 	bool valid;
891 	uint16_t line_mux;
892 	uint16_t devices;
893 	int connector_type;
894 	struct radeon_i2c_bus_rec ddc_bus;
895 	struct radeon_hpd hpd;
896 };
897 
898 bool radeon_get_atom_connector_info_from_supported_devices_table(struct
899 								 drm_device
900 								 *dev)
901 {
902 	struct radeon_device *rdev = dev->dev_private;
903 	struct radeon_mode_info *mode_info = &rdev->mode_info;
904 	struct atom_context *ctx = mode_info->atom_context;
905 	int index = GetIndexIntoMasterTable(DATA, SupportedDevicesInfo);
906 	uint16_t size, data_offset;
907 	uint8_t frev, crev;
908 	uint16_t device_support;
909 	uint8_t dac;
910 	union atom_supported_devices *supported_devices;
911 	int i, j, max_device;
912 	struct bios_connector *bios_connectors;
913 	size_t bc_size = sizeof(*bios_connectors) * ATOM_MAX_SUPPORTED_DEVICE;
914 	struct radeon_router router;
915 
916 	router.ddc_valid = false;
917 	router.cd_valid = false;
918 
919 	bios_connectors = kzalloc(bc_size, GFP_KERNEL);
920 	if (!bios_connectors)
921 		return false;
922 
923 	if (!atom_parse_data_header(ctx, index, &size, &frev, &crev,
924 				    &data_offset)) {
925 		kfree(bios_connectors);
926 		return false;
927 	}
928 
929 	supported_devices =
930 	    (union atom_supported_devices *)(ctx->bios + data_offset);
931 
932 	device_support = le16_to_cpu(supported_devices->info.usDeviceSupport);
933 
934 	if (frev > 1)
935 		max_device = ATOM_MAX_SUPPORTED_DEVICE;
936 	else
937 		max_device = ATOM_MAX_SUPPORTED_DEVICE_INFO;
938 
939 	for (i = 0; i < max_device; i++) {
940 		ATOM_CONNECTOR_INFO_I2C ci;
941 
942 		if (frev > 1)
943 			ci = supported_devices->info_2d1.asConnInfo[i];
944 		else
945 			ci = supported_devices->info.asConnInfo[i];
946 
947 		bios_connectors[i].valid = false;
948 
949 		if (!(device_support & (1 << i))) {
950 			continue;
951 		}
952 
953 		if (i == ATOM_DEVICE_CV_INDEX) {
954 			DRM_DEBUG_KMS("Skipping Component Video\n");
955 			continue;
956 		}
957 
958 		bios_connectors[i].connector_type =
959 		    supported_devices_connector_convert[ci.sucConnectorInfo.
960 							sbfAccess.
961 							bfConnectorType];
962 
963 		if (bios_connectors[i].connector_type ==
964 		    DRM_MODE_CONNECTOR_Unknown)
965 			continue;
966 
967 		dac = ci.sucConnectorInfo.sbfAccess.bfAssociatedDAC;
968 
969 		bios_connectors[i].line_mux =
970 			ci.sucI2cId.ucAccess;
971 
972 		/* give tv unique connector ids */
973 		if (i == ATOM_DEVICE_TV1_INDEX) {
974 			bios_connectors[i].ddc_bus.valid = false;
975 			bios_connectors[i].line_mux = 50;
976 		} else if (i == ATOM_DEVICE_TV2_INDEX) {
977 			bios_connectors[i].ddc_bus.valid = false;
978 			bios_connectors[i].line_mux = 51;
979 		} else if (i == ATOM_DEVICE_CV_INDEX) {
980 			bios_connectors[i].ddc_bus.valid = false;
981 			bios_connectors[i].line_mux = 52;
982 		} else
983 			bios_connectors[i].ddc_bus =
984 			    radeon_lookup_i2c_gpio(rdev,
985 						   bios_connectors[i].line_mux);
986 
987 		if ((crev > 1) && (frev > 1)) {
988 			u8 isb = supported_devices->info_2d1.asIntSrcInfo[i].ucIntSrcBitmap;
989 			switch (isb) {
990 			case 0x4:
991 				bios_connectors[i].hpd.hpd = RADEON_HPD_1;
992 				break;
993 			case 0xa:
994 				bios_connectors[i].hpd.hpd = RADEON_HPD_2;
995 				break;
996 			default:
997 				bios_connectors[i].hpd.hpd = RADEON_HPD_NONE;
998 				break;
999 			}
1000 		} else {
1001 			if (i == ATOM_DEVICE_DFP1_INDEX)
1002 				bios_connectors[i].hpd.hpd = RADEON_HPD_1;
1003 			else if (i == ATOM_DEVICE_DFP2_INDEX)
1004 				bios_connectors[i].hpd.hpd = RADEON_HPD_2;
1005 			else
1006 				bios_connectors[i].hpd.hpd = RADEON_HPD_NONE;
1007 		}
1008 
1009 		/* Always set the connector type to VGA for CRT1/CRT2. if they are
1010 		 * shared with a DVI port, we'll pick up the DVI connector when we
1011 		 * merge the outputs.  Some bioses incorrectly list VGA ports as DVI.
1012 		 */
1013 		if (i == ATOM_DEVICE_CRT1_INDEX || i == ATOM_DEVICE_CRT2_INDEX)
1014 			bios_connectors[i].connector_type =
1015 			    DRM_MODE_CONNECTOR_VGA;
1016 
1017 		if (!radeon_atom_apply_quirks
1018 		    (dev, (1 << i), &bios_connectors[i].connector_type,
1019 		     &bios_connectors[i].ddc_bus, &bios_connectors[i].line_mux,
1020 		     &bios_connectors[i].hpd))
1021 			continue;
1022 
1023 		bios_connectors[i].valid = true;
1024 		bios_connectors[i].devices = (1 << i);
1025 
1026 		if (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom)
1027 			radeon_add_atom_encoder(dev,
1028 						radeon_get_encoder_enum(dev,
1029 								      (1 << i),
1030 								      dac),
1031 						(1 << i),
1032 						0);
1033 		else
1034 			radeon_add_legacy_encoder(dev,
1035 						  radeon_get_encoder_enum(dev,
1036 									(1 << i),
1037 									dac),
1038 						  (1 << i));
1039 	}
1040 
1041 	/* combine shared connectors */
1042 	for (i = 0; i < max_device; i++) {
1043 		if (bios_connectors[i].valid) {
1044 			for (j = 0; j < max_device; j++) {
1045 				if (bios_connectors[j].valid && (i != j)) {
1046 					if (bios_connectors[i].line_mux ==
1047 					    bios_connectors[j].line_mux) {
1048 						/* make sure not to combine LVDS */
1049 						if (bios_connectors[i].devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1050 							bios_connectors[i].line_mux = 53;
1051 							bios_connectors[i].ddc_bus.valid = false;
1052 							continue;
1053 						}
1054 						if (bios_connectors[j].devices & (ATOM_DEVICE_LCD_SUPPORT)) {
1055 							bios_connectors[j].line_mux = 53;
1056 							bios_connectors[j].ddc_bus.valid = false;
1057 							continue;
1058 						}
1059 						/* combine analog and digital for DVI-I */
1060 						if (((bios_connectors[i].devices & (ATOM_DEVICE_DFP_SUPPORT)) &&
1061 						     (bios_connectors[j].devices & (ATOM_DEVICE_CRT_SUPPORT))) ||
1062 						    ((bios_connectors[j].devices & (ATOM_DEVICE_DFP_SUPPORT)) &&
1063 						     (bios_connectors[i].devices & (ATOM_DEVICE_CRT_SUPPORT)))) {
1064 							bios_connectors[i].devices |=
1065 								bios_connectors[j].devices;
1066 							bios_connectors[i].connector_type =
1067 								DRM_MODE_CONNECTOR_DVII;
1068 							if (bios_connectors[j].devices & (ATOM_DEVICE_DFP_SUPPORT))
1069 								bios_connectors[i].hpd =
1070 									bios_connectors[j].hpd;
1071 							bios_connectors[j].valid = false;
1072 						}
1073 					}
1074 				}
1075 			}
1076 		}
1077 	}
1078 
1079 	/* add the connectors */
1080 	for (i = 0; i < max_device; i++) {
1081 		if (bios_connectors[i].valid) {
1082 			uint16_t connector_object_id =
1083 				atombios_get_connector_object_id(dev,
1084 						      bios_connectors[i].connector_type,
1085 						      bios_connectors[i].devices);
1086 			radeon_add_atom_connector(dev,
1087 						  bios_connectors[i].line_mux,
1088 						  bios_connectors[i].devices,
1089 						  bios_connectors[i].
1090 						  connector_type,
1091 						  &bios_connectors[i].ddc_bus,
1092 						  0,
1093 						  connector_object_id,
1094 						  &bios_connectors[i].hpd,
1095 						  &router);
1096 		}
1097 	}
1098 
1099 	radeon_link_encoder_connector(dev);
1100 
1101 	kfree(bios_connectors);
1102 	return true;
1103 }
1104 
1105 union firmware_info {
1106 	ATOM_FIRMWARE_INFO info;
1107 	ATOM_FIRMWARE_INFO_V1_2 info_12;
1108 	ATOM_FIRMWARE_INFO_V1_3 info_13;
1109 	ATOM_FIRMWARE_INFO_V1_4 info_14;
1110 	ATOM_FIRMWARE_INFO_V2_1 info_21;
1111 	ATOM_FIRMWARE_INFO_V2_2 info_22;
1112 };
1113 
1114 union igp_info {
1115 	struct _ATOM_INTEGRATED_SYSTEM_INFO info;
1116 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
1117 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
1118 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
1119 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
1120 };
1121 
1122 static void radeon_atombios_get_dentist_vco_freq(struct radeon_device *rdev)
1123 {
1124 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1125 	int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
1126 	union igp_info *igp_info;
1127 	u8 frev, crev;
1128 	u16 data_offset;
1129 
1130 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1131 			&frev, &crev, &data_offset)) {
1132 		igp_info = (union igp_info *)(mode_info->atom_context->bios +
1133 			data_offset);
1134 		rdev->clock.vco_freq =
1135 			le32_to_cpu(igp_info->info_6.ulDentistVCOFreq);
1136 	}
1137 }
1138 
1139 bool radeon_atom_get_clock_info(struct drm_device *dev)
1140 {
1141 	struct radeon_device *rdev = dev->dev_private;
1142 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1143 	int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
1144 	union firmware_info *firmware_info;
1145 	uint8_t frev, crev;
1146 	struct radeon_pll *p1pll = &rdev->clock.p1pll;
1147 	struct radeon_pll *p2pll = &rdev->clock.p2pll;
1148 	struct radeon_pll *dcpll = &rdev->clock.dcpll;
1149 	struct radeon_pll *spll = &rdev->clock.spll;
1150 	struct radeon_pll *mpll = &rdev->clock.mpll;
1151 	uint16_t data_offset;
1152 
1153 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1154 				   &frev, &crev, &data_offset)) {
1155 		firmware_info =
1156 			(union firmware_info *)(mode_info->atom_context->bios +
1157 						data_offset);
1158 		/* pixel clocks */
1159 		p1pll->reference_freq =
1160 		    le16_to_cpu(firmware_info->info.usReferenceClock);
1161 		p1pll->reference_div = 0;
1162 
1163 		if ((frev < 2) && (crev < 2))
1164 			p1pll->pll_out_min =
1165 				le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Output);
1166 		else
1167 			p1pll->pll_out_min =
1168 				le32_to_cpu(firmware_info->info_12.ulMinPixelClockPLL_Output);
1169 		p1pll->pll_out_max =
1170 		    le32_to_cpu(firmware_info->info.ulMaxPixelClockPLL_Output);
1171 
1172 		if (((frev < 2) && (crev >= 4)) || (frev >= 2)) {
1173 			p1pll->lcd_pll_out_min =
1174 				le16_to_cpu(firmware_info->info_14.usLcdMinPixelClockPLL_Output) * 100;
1175 			if (p1pll->lcd_pll_out_min == 0)
1176 				p1pll->lcd_pll_out_min = p1pll->pll_out_min;
1177 			p1pll->lcd_pll_out_max =
1178 				le16_to_cpu(firmware_info->info_14.usLcdMaxPixelClockPLL_Output) * 100;
1179 			if (p1pll->lcd_pll_out_max == 0)
1180 				p1pll->lcd_pll_out_max = p1pll->pll_out_max;
1181 		} else {
1182 			p1pll->lcd_pll_out_min = p1pll->pll_out_min;
1183 			p1pll->lcd_pll_out_max = p1pll->pll_out_max;
1184 		}
1185 
1186 		if (p1pll->pll_out_min == 0) {
1187 			if (ASIC_IS_AVIVO(rdev))
1188 				p1pll->pll_out_min = 64800;
1189 			else
1190 				p1pll->pll_out_min = 20000;
1191 		}
1192 
1193 		p1pll->pll_in_min =
1194 		    le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Input);
1195 		p1pll->pll_in_max =
1196 		    le16_to_cpu(firmware_info->info.usMaxPixelClockPLL_Input);
1197 
1198 		*p2pll = *p1pll;
1199 
1200 		/* system clock */
1201 		if (ASIC_IS_DCE4(rdev))
1202 			spll->reference_freq =
1203 				le16_to_cpu(firmware_info->info_21.usCoreReferenceClock);
1204 		else
1205 			spll->reference_freq =
1206 				le16_to_cpu(firmware_info->info.usReferenceClock);
1207 		spll->reference_div = 0;
1208 
1209 		spll->pll_out_min =
1210 		    le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Output);
1211 		spll->pll_out_max =
1212 		    le32_to_cpu(firmware_info->info.ulMaxEngineClockPLL_Output);
1213 
1214 		/* ??? */
1215 		if (spll->pll_out_min == 0) {
1216 			if (ASIC_IS_AVIVO(rdev))
1217 				spll->pll_out_min = 64800;
1218 			else
1219 				spll->pll_out_min = 20000;
1220 		}
1221 
1222 		spll->pll_in_min =
1223 		    le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Input);
1224 		spll->pll_in_max =
1225 		    le16_to_cpu(firmware_info->info.usMaxEngineClockPLL_Input);
1226 
1227 		/* memory clock */
1228 		if (ASIC_IS_DCE4(rdev))
1229 			mpll->reference_freq =
1230 				le16_to_cpu(firmware_info->info_21.usMemoryReferenceClock);
1231 		else
1232 			mpll->reference_freq =
1233 				le16_to_cpu(firmware_info->info.usReferenceClock);
1234 		mpll->reference_div = 0;
1235 
1236 		mpll->pll_out_min =
1237 		    le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Output);
1238 		mpll->pll_out_max =
1239 		    le32_to_cpu(firmware_info->info.ulMaxMemoryClockPLL_Output);
1240 
1241 		/* ??? */
1242 		if (mpll->pll_out_min == 0) {
1243 			if (ASIC_IS_AVIVO(rdev))
1244 				mpll->pll_out_min = 64800;
1245 			else
1246 				mpll->pll_out_min = 20000;
1247 		}
1248 
1249 		mpll->pll_in_min =
1250 		    le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Input);
1251 		mpll->pll_in_max =
1252 		    le16_to_cpu(firmware_info->info.usMaxMemoryClockPLL_Input);
1253 
1254 		rdev->clock.default_sclk =
1255 		    le32_to_cpu(firmware_info->info.ulDefaultEngineClock);
1256 		rdev->clock.default_mclk =
1257 		    le32_to_cpu(firmware_info->info.ulDefaultMemoryClock);
1258 
1259 		if (ASIC_IS_DCE4(rdev)) {
1260 			rdev->clock.default_dispclk =
1261 				le32_to_cpu(firmware_info->info_21.ulDefaultDispEngineClkFreq);
1262 			if (rdev->clock.default_dispclk == 0) {
1263 				if (ASIC_IS_DCE6(rdev))
1264 					rdev->clock.default_dispclk = 60000; /* 600 Mhz */
1265 				else if (ASIC_IS_DCE5(rdev))
1266 					rdev->clock.default_dispclk = 54000; /* 540 Mhz */
1267 				else
1268 					rdev->clock.default_dispclk = 60000; /* 600 Mhz */
1269 			}
1270 			/* set a reasonable default for DP */
1271 			if (ASIC_IS_DCE6(rdev) && (rdev->clock.default_dispclk < 53900)) {
1272 				DRM_INFO("Changing default dispclk from %dMhz to 600Mhz\n",
1273 					 rdev->clock.default_dispclk / 100);
1274 				rdev->clock.default_dispclk = 60000;
1275 			}
1276 			rdev->clock.dp_extclk =
1277 				le16_to_cpu(firmware_info->info_21.usUniphyDPModeExtClkFreq);
1278 			rdev->clock.current_dispclk = rdev->clock.default_dispclk;
1279 		}
1280 		*dcpll = *p1pll;
1281 
1282 		rdev->clock.max_pixel_clock = le16_to_cpu(firmware_info->info.usMaxPixelClock);
1283 		if (rdev->clock.max_pixel_clock == 0)
1284 			rdev->clock.max_pixel_clock = 40000;
1285 
1286 		/* not technically a clock, but... */
1287 		rdev->mode_info.firmware_flags =
1288 			le16_to_cpu(firmware_info->info.usFirmwareCapability.susAccess);
1289 
1290 		if (ASIC_IS_DCE8(rdev))
1291 			rdev->clock.vco_freq =
1292 				le32_to_cpu(firmware_info->info_22.ulGPUPLL_OutputFreq);
1293 		else if (ASIC_IS_DCE5(rdev))
1294 			rdev->clock.vco_freq = rdev->clock.current_dispclk;
1295 		else if (ASIC_IS_DCE41(rdev))
1296 			radeon_atombios_get_dentist_vco_freq(rdev);
1297 		else
1298 			rdev->clock.vco_freq = rdev->clock.current_dispclk;
1299 
1300 		if (rdev->clock.vco_freq == 0)
1301 			rdev->clock.vco_freq = 360000;	/* 3.6 GHz */
1302 
1303 		return true;
1304 	}
1305 
1306 	return false;
1307 }
1308 
1309 bool radeon_atombios_sideport_present(struct radeon_device *rdev)
1310 {
1311 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1312 	int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
1313 	union igp_info *igp_info;
1314 	u8 frev, crev;
1315 	u16 data_offset;
1316 
1317 	/* sideport is AMD only */
1318 	if (rdev->family == CHIP_RS600)
1319 		return false;
1320 
1321 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1322 				   &frev, &crev, &data_offset)) {
1323 		igp_info = (union igp_info *)(mode_info->atom_context->bios +
1324 				      data_offset);
1325 		switch (crev) {
1326 		case 1:
1327 			if (le32_to_cpu(igp_info->info.ulBootUpMemoryClock))
1328 				return true;
1329 			break;
1330 		case 2:
1331 			if (le32_to_cpu(igp_info->info_2.ulBootUpSidePortClock))
1332 				return true;
1333 			break;
1334 		default:
1335 			DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
1336 			break;
1337 		}
1338 	}
1339 	return false;
1340 }
1341 
1342 bool radeon_atombios_get_tmds_info(struct radeon_encoder *encoder,
1343 				   struct radeon_encoder_int_tmds *tmds)
1344 {
1345 	struct drm_device *dev = encoder->base.dev;
1346 	struct radeon_device *rdev = dev->dev_private;
1347 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1348 	int index = GetIndexIntoMasterTable(DATA, TMDS_Info);
1349 	uint16_t data_offset;
1350 	struct _ATOM_TMDS_INFO *tmds_info;
1351 	uint8_t frev, crev;
1352 	uint16_t maxfreq;
1353 	int i;
1354 
1355 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1356 				   &frev, &crev, &data_offset)) {
1357 		tmds_info =
1358 			(struct _ATOM_TMDS_INFO *)(mode_info->atom_context->bios +
1359 						   data_offset);
1360 
1361 		maxfreq = le16_to_cpu(tmds_info->usMaxFrequency);
1362 		for (i = 0; i < 4; i++) {
1363 			tmds->tmds_pll[i].freq =
1364 			    le16_to_cpu(tmds_info->asMiscInfo[i].usFrequency);
1365 			tmds->tmds_pll[i].value =
1366 			    tmds_info->asMiscInfo[i].ucPLL_ChargePump & 0x3f;
1367 			tmds->tmds_pll[i].value |=
1368 			    (tmds_info->asMiscInfo[i].
1369 			     ucPLL_VCO_Gain & 0x3f) << 6;
1370 			tmds->tmds_pll[i].value |=
1371 			    (tmds_info->asMiscInfo[i].
1372 			     ucPLL_DutyCycle & 0xf) << 12;
1373 			tmds->tmds_pll[i].value |=
1374 			    (tmds_info->asMiscInfo[i].
1375 			     ucPLL_VoltageSwing & 0xf) << 16;
1376 
1377 			DRM_DEBUG_KMS("TMDS PLL From ATOMBIOS %u %x\n",
1378 				  tmds->tmds_pll[i].freq,
1379 				  tmds->tmds_pll[i].value);
1380 
1381 			if (maxfreq == tmds->tmds_pll[i].freq) {
1382 				tmds->tmds_pll[i].freq = 0xffffffff;
1383 				break;
1384 			}
1385 		}
1386 		return true;
1387 	}
1388 	return false;
1389 }
1390 
1391 bool radeon_atombios_get_ppll_ss_info(struct radeon_device *rdev,
1392 				      struct radeon_atom_ss *ss,
1393 				      int id)
1394 {
1395 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1396 	int index = GetIndexIntoMasterTable(DATA, PPLL_SS_Info);
1397 	uint16_t data_offset, size;
1398 	struct _ATOM_SPREAD_SPECTRUM_INFO *ss_info;
1399 	struct _ATOM_SPREAD_SPECTRUM_ASSIGNMENT *ss_assign;
1400 	uint8_t frev, crev;
1401 	int i, num_indices;
1402 
1403 	memset(ss, 0, sizeof(struct radeon_atom_ss));
1404 	if (atom_parse_data_header(mode_info->atom_context, index, &size,
1405 				   &frev, &crev, &data_offset)) {
1406 		ss_info =
1407 			(struct _ATOM_SPREAD_SPECTRUM_INFO *)(mode_info->atom_context->bios + data_offset);
1408 
1409 		num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
1410 			sizeof(ATOM_SPREAD_SPECTRUM_ASSIGNMENT);
1411 		ss_assign = (struct _ATOM_SPREAD_SPECTRUM_ASSIGNMENT *)
1412 			((u8 *)&ss_info->asSS_Info[0]);
1413 		for (i = 0; i < num_indices; i++) {
1414 			if (ss_assign->ucSS_Id == id) {
1415 				ss->percentage =
1416 					le16_to_cpu(ss_assign->usSpreadSpectrumPercentage);
1417 				ss->type = ss_assign->ucSpreadSpectrumType;
1418 				ss->step = ss_assign->ucSS_Step;
1419 				ss->delay = ss_assign->ucSS_Delay;
1420 				ss->range = ss_assign->ucSS_Range;
1421 				ss->refdiv = ss_assign->ucRecommendedRef_Div;
1422 				return true;
1423 			}
1424 			ss_assign = (struct _ATOM_SPREAD_SPECTRUM_ASSIGNMENT *)
1425 				((u8 *)ss_assign + sizeof(struct _ATOM_SPREAD_SPECTRUM_ASSIGNMENT));
1426 		}
1427 	}
1428 	return false;
1429 }
1430 
1431 static void radeon_atombios_get_igp_ss_overrides(struct radeon_device *rdev,
1432 						 struct radeon_atom_ss *ss,
1433 						 int id)
1434 {
1435 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1436 	int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
1437 	u16 data_offset, size;
1438 	union igp_info *igp_info;
1439 	u8 frev, crev;
1440 	u16 percentage = 0, rate = 0;
1441 
1442 	/* get any igp specific overrides */
1443 	if (atom_parse_data_header(mode_info->atom_context, index, &size,
1444 				   &frev, &crev, &data_offset)) {
1445 		igp_info = (union igp_info *)
1446 			(mode_info->atom_context->bios + data_offset);
1447 		switch (crev) {
1448 		case 6:
1449 			switch (id) {
1450 			case ASIC_INTERNAL_SS_ON_TMDS:
1451 				percentage = le16_to_cpu(igp_info->info_6.usDVISSPercentage);
1452 				rate = le16_to_cpu(igp_info->info_6.usDVISSpreadRateIn10Hz);
1453 				break;
1454 			case ASIC_INTERNAL_SS_ON_HDMI:
1455 				percentage = le16_to_cpu(igp_info->info_6.usHDMISSPercentage);
1456 				rate = le16_to_cpu(igp_info->info_6.usHDMISSpreadRateIn10Hz);
1457 				break;
1458 			case ASIC_INTERNAL_SS_ON_LVDS:
1459 				percentage = le16_to_cpu(igp_info->info_6.usLvdsSSPercentage);
1460 				rate = le16_to_cpu(igp_info->info_6.usLvdsSSpreadRateIn10Hz);
1461 				break;
1462 			}
1463 			break;
1464 		case 7:
1465 			switch (id) {
1466 			case ASIC_INTERNAL_SS_ON_TMDS:
1467 				percentage = le16_to_cpu(igp_info->info_7.usDVISSPercentage);
1468 				rate = le16_to_cpu(igp_info->info_7.usDVISSpreadRateIn10Hz);
1469 				break;
1470 			case ASIC_INTERNAL_SS_ON_HDMI:
1471 				percentage = le16_to_cpu(igp_info->info_7.usHDMISSPercentage);
1472 				rate = le16_to_cpu(igp_info->info_7.usHDMISSpreadRateIn10Hz);
1473 				break;
1474 			case ASIC_INTERNAL_SS_ON_LVDS:
1475 				percentage = le16_to_cpu(igp_info->info_7.usLvdsSSPercentage);
1476 				rate = le16_to_cpu(igp_info->info_7.usLvdsSSpreadRateIn10Hz);
1477 				break;
1478 			}
1479 			break;
1480 		case 8:
1481 			switch (id) {
1482 			case ASIC_INTERNAL_SS_ON_TMDS:
1483 				percentage = le16_to_cpu(igp_info->info_8.usDVISSPercentage);
1484 				rate = le16_to_cpu(igp_info->info_8.usDVISSpreadRateIn10Hz);
1485 				break;
1486 			case ASIC_INTERNAL_SS_ON_HDMI:
1487 				percentage = le16_to_cpu(igp_info->info_8.usHDMISSPercentage);
1488 				rate = le16_to_cpu(igp_info->info_8.usHDMISSpreadRateIn10Hz);
1489 				break;
1490 			case ASIC_INTERNAL_SS_ON_LVDS:
1491 				percentage = le16_to_cpu(igp_info->info_8.usLvdsSSPercentage);
1492 				rate = le16_to_cpu(igp_info->info_8.usLvdsSSpreadRateIn10Hz);
1493 				break;
1494 			}
1495 			break;
1496 		default:
1497 			DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
1498 			break;
1499 		}
1500 		if (percentage)
1501 			ss->percentage = percentage;
1502 		if (rate)
1503 			ss->rate = rate;
1504 	}
1505 }
1506 
1507 union asic_ss_info {
1508 	struct _ATOM_ASIC_INTERNAL_SS_INFO info;
1509 	struct _ATOM_ASIC_INTERNAL_SS_INFO_V2 info_2;
1510 	struct _ATOM_ASIC_INTERNAL_SS_INFO_V3 info_3;
1511 };
1512 
1513 union asic_ss_assignment {
1514 	struct _ATOM_ASIC_SS_ASSIGNMENT v1;
1515 	struct _ATOM_ASIC_SS_ASSIGNMENT_V2 v2;
1516 	struct _ATOM_ASIC_SS_ASSIGNMENT_V3 v3;
1517 };
1518 
1519 bool radeon_atombios_get_asic_ss_info(struct radeon_device *rdev,
1520 				      struct radeon_atom_ss *ss,
1521 				      int id, u32 clock)
1522 {
1523 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1524 	int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
1525 	uint16_t data_offset, size;
1526 	union asic_ss_info *ss_info;
1527 	union asic_ss_assignment *ss_assign;
1528 	uint8_t frev, crev;
1529 	int i, num_indices;
1530 
1531 	if (id == ASIC_INTERNAL_MEMORY_SS) {
1532 		if (!(rdev->mode_info.firmware_flags & ATOM_BIOS_INFO_MEMORY_CLOCK_SS_SUPPORT))
1533 			return false;
1534 	}
1535 	if (id == ASIC_INTERNAL_ENGINE_SS) {
1536 		if (!(rdev->mode_info.firmware_flags & ATOM_BIOS_INFO_ENGINE_CLOCK_SS_SUPPORT))
1537 			return false;
1538 	}
1539 
1540 	memset(ss, 0, sizeof(struct radeon_atom_ss));
1541 	if (atom_parse_data_header(mode_info->atom_context, index, &size,
1542 				   &frev, &crev, &data_offset)) {
1543 
1544 		ss_info =
1545 			(union asic_ss_info *)(mode_info->atom_context->bios + data_offset);
1546 
1547 		switch (frev) {
1548 		case 1:
1549 			num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
1550 				sizeof(ATOM_ASIC_SS_ASSIGNMENT);
1551 
1552 			ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info.asSpreadSpectrum[0]);
1553 			for (i = 0; i < num_indices; i++) {
1554 				if ((ss_assign->v1.ucClockIndication == id) &&
1555 				    (clock <= le32_to_cpu(ss_assign->v1.ulTargetClockRange))) {
1556 					ss->percentage =
1557 						le16_to_cpu(ss_assign->v1.usSpreadSpectrumPercentage);
1558 					ss->type = ss_assign->v1.ucSpreadSpectrumMode;
1559 					ss->rate = le16_to_cpu(ss_assign->v1.usSpreadRateInKhz);
1560 					ss->percentage_divider = 100;
1561 					return true;
1562 				}
1563 				ss_assign = (union asic_ss_assignment *)
1564 					((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT));
1565 			}
1566 			break;
1567 		case 2:
1568 			num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
1569 				sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2);
1570 			ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_2.asSpreadSpectrum[0]);
1571 			for (i = 0; i < num_indices; i++) {
1572 				if ((ss_assign->v2.ucClockIndication == id) &&
1573 				    (clock <= le32_to_cpu(ss_assign->v2.ulTargetClockRange))) {
1574 					ss->percentage =
1575 						le16_to_cpu(ss_assign->v2.usSpreadSpectrumPercentage);
1576 					ss->type = ss_assign->v2.ucSpreadSpectrumMode;
1577 					ss->rate = le16_to_cpu(ss_assign->v2.usSpreadRateIn10Hz);
1578 					ss->percentage_divider = 100;
1579 					if ((crev == 2) &&
1580 					    ((id == ASIC_INTERNAL_ENGINE_SS) ||
1581 					     (id == ASIC_INTERNAL_MEMORY_SS)))
1582 						ss->rate /= 100;
1583 					return true;
1584 				}
1585 				ss_assign = (union asic_ss_assignment *)
1586 					((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2));
1587 			}
1588 			break;
1589 		case 3:
1590 			num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
1591 				sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3);
1592 			ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_3.asSpreadSpectrum[0]);
1593 			for (i = 0; i < num_indices; i++) {
1594 				if ((ss_assign->v3.ucClockIndication == id) &&
1595 				    (clock <= le32_to_cpu(ss_assign->v3.ulTargetClockRange))) {
1596 					ss->percentage =
1597 						le16_to_cpu(ss_assign->v3.usSpreadSpectrumPercentage);
1598 					ss->type = ss_assign->v3.ucSpreadSpectrumMode;
1599 					ss->rate = le16_to_cpu(ss_assign->v3.usSpreadRateIn10Hz);
1600 					if (ss_assign->v3.ucSpreadSpectrumMode &
1601 					    SS_MODE_V3_PERCENTAGE_DIV_BY_1000_MASK)
1602 						ss->percentage_divider = 1000;
1603 					else
1604 						ss->percentage_divider = 100;
1605 					if ((id == ASIC_INTERNAL_ENGINE_SS) ||
1606 					    (id == ASIC_INTERNAL_MEMORY_SS))
1607 						ss->rate /= 100;
1608 					if (rdev->flags & RADEON_IS_IGP)
1609 						radeon_atombios_get_igp_ss_overrides(rdev, ss, id);
1610 					return true;
1611 				}
1612 				ss_assign = (union asic_ss_assignment *)
1613 					((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3));
1614 			}
1615 			break;
1616 		default:
1617 			DRM_ERROR("Unsupported ASIC_InternalSS_Info table: %d %d\n", frev, crev);
1618 			break;
1619 		}
1620 
1621 	}
1622 	return false;
1623 }
1624 
1625 union lvds_info {
1626 	struct _ATOM_LVDS_INFO info;
1627 	struct _ATOM_LVDS_INFO_V12 info_12;
1628 };
1629 
1630 struct radeon_encoder_atom_dig *radeon_atombios_get_lvds_info(struct
1631 							      radeon_encoder
1632 							      *encoder)
1633 {
1634 	struct drm_device *dev = encoder->base.dev;
1635 	struct radeon_device *rdev = dev->dev_private;
1636 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1637 	int index = GetIndexIntoMasterTable(DATA, LVDS_Info);
1638 	uint16_t data_offset, misc;
1639 	union lvds_info *lvds_info;
1640 	uint8_t frev, crev;
1641 	struct radeon_encoder_atom_dig *lvds = NULL;
1642 	int encoder_enum = (encoder->encoder_enum & ENUM_ID_MASK) >> ENUM_ID_SHIFT;
1643 
1644 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1645 				   &frev, &crev, &data_offset)) {
1646 		lvds_info =
1647 			(union lvds_info *)(mode_info->atom_context->bios + data_offset);
1648 		lvds =
1649 		    kzalloc_obj(struct radeon_encoder_atom_dig);
1650 
1651 		if (!lvds)
1652 			return NULL;
1653 
1654 		lvds->native_mode.clock =
1655 		    le16_to_cpu(lvds_info->info.sLCDTiming.usPixClk) * 10;
1656 		lvds->native_mode.hdisplay =
1657 		    le16_to_cpu(lvds_info->info.sLCDTiming.usHActive);
1658 		lvds->native_mode.vdisplay =
1659 		    le16_to_cpu(lvds_info->info.sLCDTiming.usVActive);
1660 		lvds->native_mode.htotal = lvds->native_mode.hdisplay +
1661 			le16_to_cpu(lvds_info->info.sLCDTiming.usHBlanking_Time);
1662 		lvds->native_mode.hsync_start = lvds->native_mode.hdisplay +
1663 			le16_to_cpu(lvds_info->info.sLCDTiming.usHSyncOffset);
1664 		lvds->native_mode.hsync_end = lvds->native_mode.hsync_start +
1665 			le16_to_cpu(lvds_info->info.sLCDTiming.usHSyncWidth);
1666 		lvds->native_mode.vtotal = lvds->native_mode.vdisplay +
1667 			le16_to_cpu(lvds_info->info.sLCDTiming.usVBlanking_Time);
1668 		lvds->native_mode.vsync_start = lvds->native_mode.vdisplay +
1669 			le16_to_cpu(lvds_info->info.sLCDTiming.usVSyncOffset);
1670 		lvds->native_mode.vsync_end = lvds->native_mode.vsync_start +
1671 			le16_to_cpu(lvds_info->info.sLCDTiming.usVSyncWidth);
1672 		lvds->panel_pwr_delay =
1673 		    le16_to_cpu(lvds_info->info.usOffDelayInMs);
1674 		lvds->lcd_misc = lvds_info->info.ucLVDS_Misc;
1675 
1676 		misc = le16_to_cpu(lvds_info->info.sLCDTiming.susModeMiscInfo.usAccess);
1677 		if (misc & ATOM_VSYNC_POLARITY)
1678 			lvds->native_mode.flags |= DRM_MODE_FLAG_NVSYNC;
1679 		if (misc & ATOM_HSYNC_POLARITY)
1680 			lvds->native_mode.flags |= DRM_MODE_FLAG_NHSYNC;
1681 		if (misc & ATOM_COMPOSITESYNC)
1682 			lvds->native_mode.flags |= DRM_MODE_FLAG_CSYNC;
1683 		if (misc & ATOM_INTERLACE)
1684 			lvds->native_mode.flags |= DRM_MODE_FLAG_INTERLACE;
1685 		if (misc & ATOM_DOUBLE_CLOCK_MODE)
1686 			lvds->native_mode.flags |= DRM_MODE_FLAG_DBLSCAN;
1687 
1688 		lvds->native_mode.width_mm = le16_to_cpu(lvds_info->info.sLCDTiming.usImageHSize);
1689 		lvds->native_mode.height_mm = le16_to_cpu(lvds_info->info.sLCDTiming.usImageVSize);
1690 
1691 		/* set crtc values */
1692 		drm_mode_set_crtcinfo(&lvds->native_mode, CRTC_INTERLACE_HALVE_V);
1693 
1694 		lvds->lcd_ss_id = lvds_info->info.ucSS_Id;
1695 
1696 		encoder->native_mode = lvds->native_mode;
1697 
1698 		if (encoder_enum == 2)
1699 			lvds->linkb = true;
1700 		else
1701 			lvds->linkb = false;
1702 
1703 		/* parse the lcd record table */
1704 		if (le16_to_cpu(lvds_info->info.usModePatchTableOffset)) {
1705 			ATOM_FAKE_EDID_PATCH_RECORD *fake_edid_record;
1706 			ATOM_PANEL_RESOLUTION_PATCH_RECORD *panel_res_record;
1707 			bool bad_record = false;
1708 			u8 *record;
1709 
1710 			if ((frev == 1) && (crev < 2))
1711 				/* absolute */
1712 				record = (u8 *)(mode_info->atom_context->bios +
1713 						le16_to_cpu(lvds_info->info.usModePatchTableOffset));
1714 			else
1715 				/* relative */
1716 				record = (u8 *)(mode_info->atom_context->bios +
1717 						data_offset +
1718 						le16_to_cpu(lvds_info->info.usModePatchTableOffset));
1719 			while (*record != ATOM_RECORD_END_TYPE) {
1720 				switch (*record) {
1721 				case LCD_MODE_PATCH_RECORD_MODE_TYPE:
1722 					record += sizeof(ATOM_PATCH_RECORD_MODE);
1723 					break;
1724 				case LCD_RTS_RECORD_TYPE:
1725 					record += sizeof(ATOM_LCD_RTS_RECORD);
1726 					break;
1727 				case LCD_CAP_RECORD_TYPE:
1728 					record += sizeof(ATOM_LCD_MODE_CONTROL_CAP);
1729 					break;
1730 				case LCD_FAKE_EDID_PATCH_RECORD_TYPE:
1731 					fake_edid_record = (ATOM_FAKE_EDID_PATCH_RECORD *)record;
1732 					if (fake_edid_record->ucFakeEDIDLength) {
1733 						const struct drm_edid *edid;
1734 						int edid_size;
1735 
1736 						if (fake_edid_record->ucFakeEDIDLength == 128)
1737 							edid_size = fake_edid_record->ucFakeEDIDLength;
1738 						else
1739 							edid_size = fake_edid_record->ucFakeEDIDLength * 128;
1740 						edid = drm_edid_alloc(fake_edid_record->ucFakeEDIDString, edid_size);
1741 						if (drm_edid_valid(edid))
1742 							rdev->mode_info.bios_hardcoded_edid = edid;
1743 						else
1744 							drm_edid_free(edid);
1745 						record += struct_size(fake_edid_record,
1746 								      ucFakeEDIDString,
1747 								      edid_size);
1748 					} else {
1749 						/* empty fake edid record must be 3 bytes long */
1750 						record += sizeof(ATOM_FAKE_EDID_PATCH_RECORD) + 1;
1751 					}
1752 					break;
1753 				case LCD_PANEL_RESOLUTION_RECORD_TYPE:
1754 					panel_res_record = (ATOM_PANEL_RESOLUTION_PATCH_RECORD *)record;
1755 					lvds->native_mode.width_mm = panel_res_record->usHSize;
1756 					lvds->native_mode.height_mm = panel_res_record->usVSize;
1757 					record += sizeof(ATOM_PANEL_RESOLUTION_PATCH_RECORD);
1758 					break;
1759 				default:
1760 					DRM_ERROR("Bad LCD record %d\n", *record);
1761 					bad_record = true;
1762 					break;
1763 				}
1764 				if (bad_record)
1765 					break;
1766 			}
1767 		}
1768 	}
1769 	return lvds;
1770 }
1771 
1772 struct radeon_encoder_primary_dac *
1773 radeon_atombios_get_primary_dac_info(struct radeon_encoder *encoder)
1774 {
1775 	struct drm_device *dev = encoder->base.dev;
1776 	struct radeon_device *rdev = dev->dev_private;
1777 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1778 	int index = GetIndexIntoMasterTable(DATA, CompassionateData);
1779 	uint16_t data_offset;
1780 	struct _COMPASSIONATE_DATA *dac_info;
1781 	uint8_t frev, crev;
1782 	uint8_t bg, dac;
1783 	struct radeon_encoder_primary_dac *p_dac = NULL;
1784 
1785 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1786 				   &frev, &crev, &data_offset)) {
1787 		dac_info = (struct _COMPASSIONATE_DATA *)
1788 			(mode_info->atom_context->bios + data_offset);
1789 
1790 		p_dac = kzalloc_obj(struct radeon_encoder_primary_dac);
1791 
1792 		if (!p_dac)
1793 			return NULL;
1794 
1795 		bg = dac_info->ucDAC1_BG_Adjustment;
1796 		dac = dac_info->ucDAC1_DAC_Adjustment;
1797 		p_dac->ps2_pdac_adj = (bg << 8) | (dac);
1798 
1799 	}
1800 	return p_dac;
1801 }
1802 
1803 bool radeon_atom_get_tv_timings(struct radeon_device *rdev, int index,
1804 				struct drm_display_mode *mode)
1805 {
1806 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1807 	ATOM_ANALOG_TV_INFO *tv_info;
1808 	ATOM_ANALOG_TV_INFO_V1_2 *tv_info_v1_2;
1809 	ATOM_DTD_FORMAT *dtd_timings;
1810 	int data_index = GetIndexIntoMasterTable(DATA, AnalogTV_Info);
1811 	u8 frev, crev;
1812 	u16 data_offset, misc;
1813 
1814 	if (!atom_parse_data_header(mode_info->atom_context, data_index, NULL,
1815 				    &frev, &crev, &data_offset))
1816 		return false;
1817 
1818 	switch (crev) {
1819 	case 1:
1820 		tv_info = (ATOM_ANALOG_TV_INFO *)(mode_info->atom_context->bios + data_offset);
1821 		if (index >= MAX_SUPPORTED_TV_TIMING)
1822 			return false;
1823 
1824 		mode->crtc_htotal = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_Total);
1825 		mode->crtc_hdisplay = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_Disp);
1826 		mode->crtc_hsync_start = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_SyncStart);
1827 		mode->crtc_hsync_end = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_SyncStart) +
1828 			le16_to_cpu(tv_info->aModeTimings[index].usCRTC_H_SyncWidth);
1829 
1830 		mode->crtc_vtotal = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_Total);
1831 		mode->crtc_vdisplay = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_Disp);
1832 		mode->crtc_vsync_start = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_SyncStart);
1833 		mode->crtc_vsync_end = le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_SyncStart) +
1834 			le16_to_cpu(tv_info->aModeTimings[index].usCRTC_V_SyncWidth);
1835 
1836 		mode->flags = 0;
1837 		misc = le16_to_cpu(tv_info->aModeTimings[index].susModeMiscInfo.usAccess);
1838 		if (misc & ATOM_VSYNC_POLARITY)
1839 			mode->flags |= DRM_MODE_FLAG_NVSYNC;
1840 		if (misc & ATOM_HSYNC_POLARITY)
1841 			mode->flags |= DRM_MODE_FLAG_NHSYNC;
1842 		if (misc & ATOM_COMPOSITESYNC)
1843 			mode->flags |= DRM_MODE_FLAG_CSYNC;
1844 		if (misc & ATOM_INTERLACE)
1845 			mode->flags |= DRM_MODE_FLAG_INTERLACE;
1846 		if (misc & ATOM_DOUBLE_CLOCK_MODE)
1847 			mode->flags |= DRM_MODE_FLAG_DBLSCAN;
1848 
1849 		mode->crtc_clock = mode->clock =
1850 			le16_to_cpu(tv_info->aModeTimings[index].usPixelClock) * 10;
1851 
1852 		if (index == 1) {
1853 			/* PAL timings appear to have wrong values for totals */
1854 			mode->crtc_htotal -= 1;
1855 			mode->crtc_vtotal -= 1;
1856 		}
1857 		break;
1858 	case 2:
1859 		tv_info_v1_2 = (ATOM_ANALOG_TV_INFO_V1_2 *)(mode_info->atom_context->bios + data_offset);
1860 		if (index >= MAX_SUPPORTED_TV_TIMING_V1_2)
1861 			return false;
1862 
1863 		dtd_timings = &tv_info_v1_2->aModeTimings[index];
1864 		mode->crtc_htotal = le16_to_cpu(dtd_timings->usHActive) +
1865 			le16_to_cpu(dtd_timings->usHBlanking_Time);
1866 		mode->crtc_hdisplay = le16_to_cpu(dtd_timings->usHActive);
1867 		mode->crtc_hsync_start = le16_to_cpu(dtd_timings->usHActive) +
1868 			le16_to_cpu(dtd_timings->usHSyncOffset);
1869 		mode->crtc_hsync_end = mode->crtc_hsync_start +
1870 			le16_to_cpu(dtd_timings->usHSyncWidth);
1871 
1872 		mode->crtc_vtotal = le16_to_cpu(dtd_timings->usVActive) +
1873 			le16_to_cpu(dtd_timings->usVBlanking_Time);
1874 		mode->crtc_vdisplay = le16_to_cpu(dtd_timings->usVActive);
1875 		mode->crtc_vsync_start = le16_to_cpu(dtd_timings->usVActive) +
1876 			le16_to_cpu(dtd_timings->usVSyncOffset);
1877 		mode->crtc_vsync_end = mode->crtc_vsync_start +
1878 			le16_to_cpu(dtd_timings->usVSyncWidth);
1879 
1880 		mode->flags = 0;
1881 		misc = le16_to_cpu(dtd_timings->susModeMiscInfo.usAccess);
1882 		if (misc & ATOM_VSYNC_POLARITY)
1883 			mode->flags |= DRM_MODE_FLAG_NVSYNC;
1884 		if (misc & ATOM_HSYNC_POLARITY)
1885 			mode->flags |= DRM_MODE_FLAG_NHSYNC;
1886 		if (misc & ATOM_COMPOSITESYNC)
1887 			mode->flags |= DRM_MODE_FLAG_CSYNC;
1888 		if (misc & ATOM_INTERLACE)
1889 			mode->flags |= DRM_MODE_FLAG_INTERLACE;
1890 		if (misc & ATOM_DOUBLE_CLOCK_MODE)
1891 			mode->flags |= DRM_MODE_FLAG_DBLSCAN;
1892 
1893 		mode->crtc_clock = mode->clock =
1894 			le16_to_cpu(dtd_timings->usPixClk) * 10;
1895 		break;
1896 	}
1897 	return true;
1898 }
1899 
1900 enum radeon_tv_std
1901 radeon_atombios_get_tv_info(struct radeon_device *rdev)
1902 {
1903 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1904 	int index = GetIndexIntoMasterTable(DATA, AnalogTV_Info);
1905 	uint16_t data_offset;
1906 	uint8_t frev, crev;
1907 	struct _ATOM_ANALOG_TV_INFO *tv_info;
1908 	enum radeon_tv_std tv_std = TV_STD_NTSC;
1909 
1910 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1911 				   &frev, &crev, &data_offset)) {
1912 
1913 		tv_info = (struct _ATOM_ANALOG_TV_INFO *)
1914 			(mode_info->atom_context->bios + data_offset);
1915 
1916 		switch (tv_info->ucTV_BootUpDefaultStandard) {
1917 		case ATOM_TV_NTSC:
1918 			tv_std = TV_STD_NTSC;
1919 			DRM_DEBUG_KMS("Default TV standard: NTSC\n");
1920 			break;
1921 		case ATOM_TV_NTSCJ:
1922 			tv_std = TV_STD_NTSC_J;
1923 			DRM_DEBUG_KMS("Default TV standard: NTSC-J\n");
1924 			break;
1925 		case ATOM_TV_PAL:
1926 			tv_std = TV_STD_PAL;
1927 			DRM_DEBUG_KMS("Default TV standard: PAL\n");
1928 			break;
1929 		case ATOM_TV_PALM:
1930 			tv_std = TV_STD_PAL_M;
1931 			DRM_DEBUG_KMS("Default TV standard: PAL-M\n");
1932 			break;
1933 		case ATOM_TV_PALN:
1934 			tv_std = TV_STD_PAL_N;
1935 			DRM_DEBUG_KMS("Default TV standard: PAL-N\n");
1936 			break;
1937 		case ATOM_TV_PALCN:
1938 			tv_std = TV_STD_PAL_CN;
1939 			DRM_DEBUG_KMS("Default TV standard: PAL-CN\n");
1940 			break;
1941 		case ATOM_TV_PAL60:
1942 			tv_std = TV_STD_PAL_60;
1943 			DRM_DEBUG_KMS("Default TV standard: PAL-60\n");
1944 			break;
1945 		case ATOM_TV_SECAM:
1946 			tv_std = TV_STD_SECAM;
1947 			DRM_DEBUG_KMS("Default TV standard: SECAM\n");
1948 			break;
1949 		default:
1950 			tv_std = TV_STD_NTSC;
1951 			DRM_DEBUG_KMS("Unknown TV standard; defaulting to NTSC\n");
1952 			break;
1953 		}
1954 	}
1955 	return tv_std;
1956 }
1957 
1958 struct radeon_encoder_tv_dac *
1959 radeon_atombios_get_tv_dac_info(struct radeon_encoder *encoder)
1960 {
1961 	struct drm_device *dev = encoder->base.dev;
1962 	struct radeon_device *rdev = dev->dev_private;
1963 	struct radeon_mode_info *mode_info = &rdev->mode_info;
1964 	int index = GetIndexIntoMasterTable(DATA, CompassionateData);
1965 	uint16_t data_offset;
1966 	struct _COMPASSIONATE_DATA *dac_info;
1967 	uint8_t frev, crev;
1968 	uint8_t bg, dac;
1969 	struct radeon_encoder_tv_dac *tv_dac = NULL;
1970 
1971 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
1972 				   &frev, &crev, &data_offset)) {
1973 
1974 		dac_info = (struct _COMPASSIONATE_DATA *)
1975 			(mode_info->atom_context->bios + data_offset);
1976 
1977 		tv_dac = kzalloc_obj(struct radeon_encoder_tv_dac);
1978 
1979 		if (!tv_dac)
1980 			return NULL;
1981 
1982 		bg = dac_info->ucDAC2_CRT2_BG_Adjustment;
1983 		dac = dac_info->ucDAC2_CRT2_DAC_Adjustment;
1984 		tv_dac->ps2_tvdac_adj = (bg << 16) | (dac << 20);
1985 
1986 		bg = dac_info->ucDAC2_PAL_BG_Adjustment;
1987 		dac = dac_info->ucDAC2_PAL_DAC_Adjustment;
1988 		tv_dac->pal_tvdac_adj = (bg << 16) | (dac << 20);
1989 
1990 		bg = dac_info->ucDAC2_NTSC_BG_Adjustment;
1991 		dac = dac_info->ucDAC2_NTSC_DAC_Adjustment;
1992 		tv_dac->ntsc_tvdac_adj = (bg << 16) | (dac << 20);
1993 
1994 		tv_dac->tv_std = radeon_atombios_get_tv_info(rdev);
1995 	}
1996 	return tv_dac;
1997 }
1998 
1999 static const char *thermal_controller_names[] = {
2000 	"NONE",
2001 	"lm63",
2002 	"adm1032",
2003 	"adm1030",
2004 	"max6649",
2005 	"lm63", /* lm64 */
2006 	"f75375",
2007 	"asc7xxx",
2008 };
2009 
2010 static const char *pp_lib_thermal_controller_names[] = {
2011 	"NONE",
2012 	"lm63",
2013 	"adm1032",
2014 	"adm1030",
2015 	"max6649",
2016 	"lm63", /* lm64 */
2017 	"f75375",
2018 	"RV6xx",
2019 	"RV770",
2020 	"adt7473",
2021 	"NONE",
2022 	"External GPIO",
2023 	"Evergreen",
2024 	"emc2103",
2025 	"Sumo",
2026 	"Northern Islands",
2027 	"Southern Islands",
2028 	"lm96163",
2029 	"Sea Islands",
2030 };
2031 
2032 union power_info {
2033 	struct _ATOM_POWERPLAY_INFO info;
2034 	struct _ATOM_POWERPLAY_INFO_V2 info_2;
2035 	struct _ATOM_POWERPLAY_INFO_V3 info_3;
2036 	struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
2037 	struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
2038 	struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
2039 };
2040 
2041 union pplib_clock_info {
2042 	struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
2043 	struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
2044 	struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
2045 	struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
2046 	struct _ATOM_PPLIB_SI_CLOCK_INFO si;
2047 	struct _ATOM_PPLIB_CI_CLOCK_INFO ci;
2048 };
2049 
2050 union pplib_power_state {
2051 	struct _ATOM_PPLIB_STATE v1;
2052 	struct _ATOM_PPLIB_STATE_V2 v2;
2053 };
2054 
2055 static void radeon_atombios_parse_misc_flags_1_3(struct radeon_device *rdev,
2056 						 int state_index,
2057 						 u32 misc, u32 misc2)
2058 {
2059 	rdev->pm.power_state[state_index].misc = misc;
2060 	rdev->pm.power_state[state_index].misc2 = misc2;
2061 	/* order matters! */
2062 	if (misc & ATOM_PM_MISCINFO_POWER_SAVING_MODE)
2063 		rdev->pm.power_state[state_index].type =
2064 			POWER_STATE_TYPE_POWERSAVE;
2065 	if (misc & ATOM_PM_MISCINFO_DEFAULT_DC_STATE_ENTRY_TRUE)
2066 		rdev->pm.power_state[state_index].type =
2067 			POWER_STATE_TYPE_BATTERY;
2068 	if (misc & ATOM_PM_MISCINFO_DEFAULT_LOW_DC_STATE_ENTRY_TRUE)
2069 		rdev->pm.power_state[state_index].type =
2070 			POWER_STATE_TYPE_BATTERY;
2071 	if (misc & ATOM_PM_MISCINFO_LOAD_BALANCE_EN)
2072 		rdev->pm.power_state[state_index].type =
2073 			POWER_STATE_TYPE_BALANCED;
2074 	if (misc & ATOM_PM_MISCINFO_3D_ACCELERATION_EN) {
2075 		rdev->pm.power_state[state_index].type =
2076 			POWER_STATE_TYPE_PERFORMANCE;
2077 		rdev->pm.power_state[state_index].flags &=
2078 			~RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2079 	}
2080 	if (misc2 & ATOM_PM_MISCINFO2_SYSTEM_AC_LITE_MODE)
2081 		rdev->pm.power_state[state_index].type =
2082 			POWER_STATE_TYPE_BALANCED;
2083 	if (misc & ATOM_PM_MISCINFO_DRIVER_DEFAULT_MODE) {
2084 		rdev->pm.power_state[state_index].type =
2085 			POWER_STATE_TYPE_DEFAULT;
2086 		rdev->pm.default_power_state_index = state_index;
2087 		rdev->pm.power_state[state_index].default_clock_mode =
2088 			&rdev->pm.power_state[state_index].clock_info[0];
2089 	} else if (state_index == 0) {
2090 		rdev->pm.power_state[state_index].clock_info[0].flags |=
2091 			RADEON_PM_MODE_NO_DISPLAY;
2092 	}
2093 }
2094 
2095 static int radeon_atombios_parse_power_table_1_3(struct radeon_device *rdev)
2096 {
2097 	struct radeon_mode_info *mode_info = &rdev->mode_info;
2098 	u32 misc, misc2 = 0;
2099 	int num_modes = 0, i;
2100 	int state_index = 0;
2101 	struct radeon_i2c_bus_rec i2c_bus;
2102 	union power_info *power_info;
2103 	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2104 	u16 data_offset;
2105 	u8 frev, crev;
2106 
2107 	if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
2108 				   &frev, &crev, &data_offset))
2109 		return state_index;
2110 	power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
2111 
2112 	/* add the i2c bus for thermal/fan chip */
2113 	if ((power_info->info.ucOverdriveThermalController > 0) &&
2114 	    (power_info->info.ucOverdriveThermalController < ARRAY_SIZE(thermal_controller_names))) {
2115 		DRM_INFO("Possible %s thermal controller at 0x%02x\n",
2116 			 thermal_controller_names[power_info->info.ucOverdriveThermalController],
2117 			 power_info->info.ucOverdriveControllerAddress >> 1);
2118 		i2c_bus = radeon_lookup_i2c_gpio(rdev, power_info->info.ucOverdriveI2cLine);
2119 		rdev->pm.i2c_bus = radeon_i2c_lookup(rdev, &i2c_bus);
2120 		if (rdev->pm.i2c_bus) {
2121 			struct i2c_board_info info = { };
2122 			const char *name = thermal_controller_names[power_info->info.
2123 								    ucOverdriveThermalController];
2124 			info.addr = power_info->info.ucOverdriveControllerAddress >> 1;
2125 			strscpy(info.type, name, sizeof(info.type));
2126 			i2c_new_client_device(&rdev->pm.i2c_bus->adapter, &info);
2127 		}
2128 	}
2129 	num_modes = power_info->info.ucNumOfPowerModeEntries;
2130 	if (num_modes > ATOM_MAX_NUMBEROF_POWER_BLOCK)
2131 		num_modes = ATOM_MAX_NUMBEROF_POWER_BLOCK;
2132 	if (num_modes == 0)
2133 		return state_index;
2134 	rdev->pm.power_state = kzalloc_objs(struct radeon_power_state,
2135 					    num_modes);
2136 	if (!rdev->pm.power_state)
2137 		return state_index;
2138 	/* last mode is usually default, array is low to high */
2139 	for (i = 0; i < num_modes; i++) {
2140 		/* avoid memory leaks from invalid modes or unknown frev. */
2141 		if (!rdev->pm.power_state[state_index].clock_info) {
2142 			rdev->pm.power_state[state_index].clock_info =
2143 				kzalloc_obj(struct radeon_pm_clock_info);
2144 		}
2145 		if (!rdev->pm.power_state[state_index].clock_info)
2146 			goto out;
2147 		rdev->pm.power_state[state_index].num_clock_modes = 1;
2148 		rdev->pm.power_state[state_index].clock_info[0].voltage.type = VOLTAGE_NONE;
2149 		switch (frev) {
2150 		case 1:
2151 			rdev->pm.power_state[state_index].clock_info[0].mclk =
2152 				le16_to_cpu(power_info->info.asPowerPlayInfo[i].usMemoryClock);
2153 			rdev->pm.power_state[state_index].clock_info[0].sclk =
2154 				le16_to_cpu(power_info->info.asPowerPlayInfo[i].usEngineClock);
2155 			/* skip invalid modes */
2156 			if ((rdev->pm.power_state[state_index].clock_info[0].mclk == 0) ||
2157 			    (rdev->pm.power_state[state_index].clock_info[0].sclk == 0))
2158 				continue;
2159 			rdev->pm.power_state[state_index].pcie_lanes =
2160 				power_info->info.asPowerPlayInfo[i].ucNumPciELanes;
2161 			misc = le32_to_cpu(power_info->info.asPowerPlayInfo[i].ulMiscInfo);
2162 			if ((misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) ||
2163 			    (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)) {
2164 				rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2165 					VOLTAGE_GPIO;
2166 				rdev->pm.power_state[state_index].clock_info[0].voltage.gpio =
2167 					radeon_atombios_lookup_gpio(rdev,
2168 							   power_info->info.asPowerPlayInfo[i].ucVoltageDropIndex);
2169 				if (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)
2170 					rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2171 						true;
2172 				else
2173 					rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2174 						false;
2175 			} else if (misc & ATOM_PM_MISCINFO_PROGRAM_VOLTAGE) {
2176 				rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2177 					VOLTAGE_VDDC;
2178 				rdev->pm.power_state[state_index].clock_info[0].voltage.vddc_id =
2179 					power_info->info.asPowerPlayInfo[i].ucVoltageDropIndex;
2180 			}
2181 			rdev->pm.power_state[state_index].flags = RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2182 			radeon_atombios_parse_misc_flags_1_3(rdev, state_index, misc, 0);
2183 			state_index++;
2184 			break;
2185 		case 2:
2186 			rdev->pm.power_state[state_index].clock_info[0].mclk =
2187 				le32_to_cpu(power_info->info_2.asPowerPlayInfo[i].ulMemoryClock);
2188 			rdev->pm.power_state[state_index].clock_info[0].sclk =
2189 				le32_to_cpu(power_info->info_2.asPowerPlayInfo[i].ulEngineClock);
2190 			/* skip invalid modes */
2191 			if ((rdev->pm.power_state[state_index].clock_info[0].mclk == 0) ||
2192 			    (rdev->pm.power_state[state_index].clock_info[0].sclk == 0))
2193 				continue;
2194 			rdev->pm.power_state[state_index].pcie_lanes =
2195 				power_info->info_2.asPowerPlayInfo[i].ucNumPciELanes;
2196 			misc = le32_to_cpu(power_info->info_2.asPowerPlayInfo[i].ulMiscInfo);
2197 			misc2 = le32_to_cpu(power_info->info_2.asPowerPlayInfo[i].ulMiscInfo2);
2198 			if ((misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) ||
2199 			    (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)) {
2200 				rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2201 					VOLTAGE_GPIO;
2202 				rdev->pm.power_state[state_index].clock_info[0].voltage.gpio =
2203 					radeon_atombios_lookup_gpio(rdev,
2204 							   power_info->info_2.asPowerPlayInfo[i].ucVoltageDropIndex);
2205 				if (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)
2206 					rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2207 						true;
2208 				else
2209 					rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2210 						false;
2211 			} else if (misc & ATOM_PM_MISCINFO_PROGRAM_VOLTAGE) {
2212 				rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2213 					VOLTAGE_VDDC;
2214 				rdev->pm.power_state[state_index].clock_info[0].voltage.vddc_id =
2215 					power_info->info_2.asPowerPlayInfo[i].ucVoltageDropIndex;
2216 			}
2217 			rdev->pm.power_state[state_index].flags = RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2218 			radeon_atombios_parse_misc_flags_1_3(rdev, state_index, misc, misc2);
2219 			state_index++;
2220 			break;
2221 		case 3:
2222 			rdev->pm.power_state[state_index].clock_info[0].mclk =
2223 				le32_to_cpu(power_info->info_3.asPowerPlayInfo[i].ulMemoryClock);
2224 			rdev->pm.power_state[state_index].clock_info[0].sclk =
2225 				le32_to_cpu(power_info->info_3.asPowerPlayInfo[i].ulEngineClock);
2226 			/* skip invalid modes */
2227 			if ((rdev->pm.power_state[state_index].clock_info[0].mclk == 0) ||
2228 			    (rdev->pm.power_state[state_index].clock_info[0].sclk == 0))
2229 				continue;
2230 			rdev->pm.power_state[state_index].pcie_lanes =
2231 				power_info->info_3.asPowerPlayInfo[i].ucNumPciELanes;
2232 			misc = le32_to_cpu(power_info->info_3.asPowerPlayInfo[i].ulMiscInfo);
2233 			misc2 = le32_to_cpu(power_info->info_3.asPowerPlayInfo[i].ulMiscInfo2);
2234 			if ((misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) ||
2235 			    (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)) {
2236 				rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2237 					VOLTAGE_GPIO;
2238 				rdev->pm.power_state[state_index].clock_info[0].voltage.gpio =
2239 					radeon_atombios_lookup_gpio(rdev,
2240 							   power_info->info_3.asPowerPlayInfo[i].ucVoltageDropIndex);
2241 				if (misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_ACTIVE_HIGH)
2242 					rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2243 						true;
2244 				else
2245 					rdev->pm.power_state[state_index].clock_info[0].voltage.active_high =
2246 						false;
2247 			} else if (misc & ATOM_PM_MISCINFO_PROGRAM_VOLTAGE) {
2248 				rdev->pm.power_state[state_index].clock_info[0].voltage.type =
2249 					VOLTAGE_VDDC;
2250 				rdev->pm.power_state[state_index].clock_info[0].voltage.vddc_id =
2251 					power_info->info_3.asPowerPlayInfo[i].ucVoltageDropIndex;
2252 				if (misc2 & ATOM_PM_MISCINFO2_VDDCI_DYNAMIC_VOLTAGE_EN) {
2253 					rdev->pm.power_state[state_index].clock_info[0].voltage.vddci_enabled =
2254 						true;
2255 					rdev->pm.power_state[state_index].clock_info[0].voltage.vddci_id =
2256 						power_info->info_3.asPowerPlayInfo[i].ucVDDCI_VoltageDropIndex;
2257 				}
2258 			}
2259 			rdev->pm.power_state[state_index].flags = RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2260 			radeon_atombios_parse_misc_flags_1_3(rdev, state_index, misc, misc2);
2261 			state_index++;
2262 			break;
2263 		}
2264 	}
2265 out:
2266 	/* free any unused clock_info allocation. */
2267 	if (state_index && state_index < num_modes) {
2268 		kfree(rdev->pm.power_state[state_index].clock_info);
2269 		rdev->pm.power_state[state_index].clock_info = NULL;
2270 	}
2271 
2272 	/* last mode is usually default */
2273 	if (state_index && rdev->pm.default_power_state_index == -1) {
2274 		rdev->pm.power_state[state_index - 1].type =
2275 			POWER_STATE_TYPE_DEFAULT;
2276 		rdev->pm.default_power_state_index = state_index - 1;
2277 		rdev->pm.power_state[state_index - 1].default_clock_mode =
2278 			&rdev->pm.power_state[state_index - 1].clock_info[0];
2279 		rdev->pm.power_state[state_index - 1].flags &=
2280 			~RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2281 		rdev->pm.power_state[state_index - 1].misc = 0;
2282 		rdev->pm.power_state[state_index - 1].misc2 = 0;
2283 	}
2284 	return state_index;
2285 }
2286 
2287 static void radeon_atombios_add_pplib_thermal_controller(struct radeon_device *rdev,
2288 							 ATOM_PPLIB_THERMALCONTROLLER *controller)
2289 {
2290 	struct radeon_i2c_bus_rec i2c_bus;
2291 
2292 	/* add the i2c bus for thermal/fan chip */
2293 	if (controller->ucType > 0) {
2294 		if (controller->ucFanParameters & ATOM_PP_FANPARAMETERS_NOFAN)
2295 			rdev->pm.no_fan = true;
2296 		rdev->pm.fan_pulses_per_revolution =
2297 			controller->ucFanParameters & ATOM_PP_FANPARAMETERS_TACHOMETER_PULSES_PER_REVOLUTION_MASK;
2298 		if (rdev->pm.fan_pulses_per_revolution) {
2299 			rdev->pm.fan_min_rpm = controller->ucFanMinRPM;
2300 			rdev->pm.fan_max_rpm = controller->ucFanMaxRPM;
2301 		}
2302 		if (controller->ucType == ATOM_PP_THERMALCONTROLLER_RV6xx) {
2303 			DRM_INFO("Internal thermal controller %s fan control\n",
2304 				 (controller->ucFanParameters &
2305 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2306 			rdev->pm.int_thermal_type = THERMAL_TYPE_RV6XX;
2307 		} else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_RV770) {
2308 			DRM_INFO("Internal thermal controller %s fan control\n",
2309 				 (controller->ucFanParameters &
2310 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2311 			rdev->pm.int_thermal_type = THERMAL_TYPE_RV770;
2312 		} else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_EVERGREEN) {
2313 			DRM_INFO("Internal thermal controller %s fan control\n",
2314 				 (controller->ucFanParameters &
2315 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2316 			rdev->pm.int_thermal_type = THERMAL_TYPE_EVERGREEN;
2317 		} else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_SUMO) {
2318 			DRM_INFO("Internal thermal controller %s fan control\n",
2319 				 (controller->ucFanParameters &
2320 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2321 			rdev->pm.int_thermal_type = THERMAL_TYPE_SUMO;
2322 		} else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_NISLANDS) {
2323 			DRM_INFO("Internal thermal controller %s fan control\n",
2324 				 (controller->ucFanParameters &
2325 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2326 			rdev->pm.int_thermal_type = THERMAL_TYPE_NI;
2327 		} else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_SISLANDS) {
2328 			DRM_INFO("Internal thermal controller %s fan control\n",
2329 				 (controller->ucFanParameters &
2330 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2331 			rdev->pm.int_thermal_type = THERMAL_TYPE_SI;
2332 		} else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_CISLANDS) {
2333 			DRM_INFO("Internal thermal controller %s fan control\n",
2334 				 (controller->ucFanParameters &
2335 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2336 			rdev->pm.int_thermal_type = THERMAL_TYPE_CI;
2337 		} else if (controller->ucType == ATOM_PP_THERMALCONTROLLER_KAVERI) {
2338 			DRM_INFO("Internal thermal controller %s fan control\n",
2339 				 (controller->ucFanParameters &
2340 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2341 			rdev->pm.int_thermal_type = THERMAL_TYPE_KV;
2342 		} else if (controller->ucType ==
2343 			   ATOM_PP_THERMALCONTROLLER_EXTERNAL_GPIO) {
2344 			DRM_INFO("External GPIO thermal controller %s fan control\n",
2345 				 (controller->ucFanParameters &
2346 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2347 			rdev->pm.int_thermal_type = THERMAL_TYPE_EXTERNAL_GPIO;
2348 		} else if (controller->ucType ==
2349 			   ATOM_PP_THERMALCONTROLLER_ADT7473_WITH_INTERNAL) {
2350 			DRM_INFO("ADT7473 with internal thermal controller %s fan control\n",
2351 				 (controller->ucFanParameters &
2352 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2353 			rdev->pm.int_thermal_type = THERMAL_TYPE_ADT7473_WITH_INTERNAL;
2354 		} else if (controller->ucType ==
2355 			   ATOM_PP_THERMALCONTROLLER_EMC2103_WITH_INTERNAL) {
2356 			DRM_INFO("EMC2103 with internal thermal controller %s fan control\n",
2357 				 (controller->ucFanParameters &
2358 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2359 			rdev->pm.int_thermal_type = THERMAL_TYPE_EMC2103_WITH_INTERNAL;
2360 		} else if (controller->ucType < ARRAY_SIZE(pp_lib_thermal_controller_names)) {
2361 			DRM_INFO("Possible %s thermal controller at 0x%02x %s fan control\n",
2362 				 pp_lib_thermal_controller_names[controller->ucType],
2363 				 controller->ucI2cAddress >> 1,
2364 				 (controller->ucFanParameters &
2365 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2366 			rdev->pm.int_thermal_type = THERMAL_TYPE_EXTERNAL;
2367 			i2c_bus = radeon_lookup_i2c_gpio(rdev, controller->ucI2cLine);
2368 			rdev->pm.i2c_bus = radeon_i2c_lookup(rdev, &i2c_bus);
2369 			if (rdev->pm.i2c_bus) {
2370 				struct i2c_board_info info = { };
2371 				const char *name = pp_lib_thermal_controller_names[controller->ucType];
2372 				info.addr = controller->ucI2cAddress >> 1;
2373 				strscpy(info.type, name, sizeof(info.type));
2374 				i2c_new_client_device(&rdev->pm.i2c_bus->adapter, &info);
2375 			}
2376 		} else {
2377 			DRM_INFO("Unknown thermal controller type %d at 0x%02x %s fan control\n",
2378 				 controller->ucType,
2379 				 controller->ucI2cAddress >> 1,
2380 				 (controller->ucFanParameters &
2381 				  ATOM_PP_FANPARAMETERS_NOFAN) ? "without" : "with");
2382 		}
2383 	}
2384 }
2385 
2386 void radeon_atombios_get_default_voltages(struct radeon_device *rdev,
2387 					  u16 *vddc, u16 *vddci, u16 *mvdd)
2388 {
2389 	struct radeon_mode_info *mode_info = &rdev->mode_info;
2390 	int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
2391 	u8 frev, crev;
2392 	u16 data_offset;
2393 	union firmware_info *firmware_info;
2394 
2395 	*vddc = 0;
2396 	*vddci = 0;
2397 	*mvdd = 0;
2398 
2399 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
2400 				   &frev, &crev, &data_offset)) {
2401 		firmware_info =
2402 			(union firmware_info *)(mode_info->atom_context->bios +
2403 						data_offset);
2404 		*vddc = le16_to_cpu(firmware_info->info_14.usBootUpVDDCVoltage);
2405 		if ((frev == 2) && (crev >= 2)) {
2406 			*vddci = le16_to_cpu(firmware_info->info_22.usBootUpVDDCIVoltage);
2407 			*mvdd = le16_to_cpu(firmware_info->info_22.usBootUpMVDDCVoltage);
2408 		}
2409 	}
2410 }
2411 
2412 static void radeon_atombios_parse_pplib_non_clock_info(struct radeon_device *rdev,
2413 						       int state_index, int mode_index,
2414 						       struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info)
2415 {
2416 	int j;
2417 	u32 misc = le32_to_cpu(non_clock_info->ulCapsAndSettings);
2418 	u32 misc2 = le16_to_cpu(non_clock_info->usClassification);
2419 	u16 vddc, vddci, mvdd;
2420 
2421 	radeon_atombios_get_default_voltages(rdev, &vddc, &vddci, &mvdd);
2422 
2423 	rdev->pm.power_state[state_index].misc = misc;
2424 	rdev->pm.power_state[state_index].misc2 = misc2;
2425 	rdev->pm.power_state[state_index].pcie_lanes =
2426 		((misc & ATOM_PPLIB_PCIE_LINK_WIDTH_MASK) >>
2427 		 ATOM_PPLIB_PCIE_LINK_WIDTH_SHIFT) + 1;
2428 	switch (misc2 & ATOM_PPLIB_CLASSIFICATION_UI_MASK) {
2429 	case ATOM_PPLIB_CLASSIFICATION_UI_BATTERY:
2430 		rdev->pm.power_state[state_index].type =
2431 			POWER_STATE_TYPE_BATTERY;
2432 		break;
2433 	case ATOM_PPLIB_CLASSIFICATION_UI_BALANCED:
2434 		rdev->pm.power_state[state_index].type =
2435 			POWER_STATE_TYPE_BALANCED;
2436 		break;
2437 	case ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE:
2438 		rdev->pm.power_state[state_index].type =
2439 			POWER_STATE_TYPE_PERFORMANCE;
2440 		break;
2441 	case ATOM_PPLIB_CLASSIFICATION_UI_NONE:
2442 		if (misc2 & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
2443 			rdev->pm.power_state[state_index].type =
2444 				POWER_STATE_TYPE_PERFORMANCE;
2445 		break;
2446 	}
2447 	rdev->pm.power_state[state_index].flags = 0;
2448 	if (misc & ATOM_PPLIB_SINGLE_DISPLAY_ONLY)
2449 		rdev->pm.power_state[state_index].flags |=
2450 			RADEON_PM_STATE_SINGLE_DISPLAY_ONLY;
2451 	if (misc2 & ATOM_PPLIB_CLASSIFICATION_BOOT) {
2452 		rdev->pm.power_state[state_index].type =
2453 			POWER_STATE_TYPE_DEFAULT;
2454 		rdev->pm.default_power_state_index = state_index;
2455 		rdev->pm.power_state[state_index].default_clock_mode =
2456 			&rdev->pm.power_state[state_index].clock_info[mode_index - 1];
2457 		if ((rdev->family >= CHIP_BARTS) && !(rdev->flags & RADEON_IS_IGP)) {
2458 			/* NI chips post without MC ucode, so default clocks are strobe mode only */
2459 			rdev->pm.default_sclk = rdev->pm.power_state[state_index].clock_info[0].sclk;
2460 			rdev->pm.default_mclk = rdev->pm.power_state[state_index].clock_info[0].mclk;
2461 			rdev->pm.default_vddc = rdev->pm.power_state[state_index].clock_info[0].voltage.voltage;
2462 			rdev->pm.default_vddci = rdev->pm.power_state[state_index].clock_info[0].voltage.vddci;
2463 		} else {
2464 			u16 max_vddci = 0;
2465 
2466 			if (ASIC_IS_DCE4(rdev))
2467 				radeon_atom_get_max_voltage(rdev,
2468 							    SET_VOLTAGE_TYPE_ASIC_VDDCI,
2469 							    &max_vddci);
2470 			/* patch the table values with the default sclk/mclk from firmware info */
2471 			for (j = 0; j < mode_index; j++) {
2472 				rdev->pm.power_state[state_index].clock_info[j].mclk =
2473 					rdev->clock.default_mclk;
2474 				rdev->pm.power_state[state_index].clock_info[j].sclk =
2475 					rdev->clock.default_sclk;
2476 				if (vddc)
2477 					rdev->pm.power_state[state_index].clock_info[j].voltage.voltage =
2478 						vddc;
2479 				if (max_vddci)
2480 					rdev->pm.power_state[state_index].clock_info[j].voltage.vddci =
2481 						max_vddci;
2482 			}
2483 		}
2484 	}
2485 }
2486 
2487 static bool radeon_atombios_parse_pplib_clock_info(struct radeon_device *rdev,
2488 						   int state_index, int mode_index,
2489 						   union pplib_clock_info *clock_info)
2490 {
2491 	u32 sclk, mclk;
2492 	u16 vddc;
2493 
2494 	if (rdev->flags & RADEON_IS_IGP) {
2495 		if (rdev->family >= CHIP_PALM) {
2496 			sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
2497 			sclk |= clock_info->sumo.ucEngineClockHigh << 16;
2498 			rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2499 		} else {
2500 			sclk = le16_to_cpu(clock_info->rs780.usLowEngineClockLow);
2501 			sclk |= clock_info->rs780.ucLowEngineClockHigh << 16;
2502 			rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2503 		}
2504 	} else if (rdev->family >= CHIP_BONAIRE) {
2505 		sclk = le16_to_cpu(clock_info->ci.usEngineClockLow);
2506 		sclk |= clock_info->ci.ucEngineClockHigh << 16;
2507 		mclk = le16_to_cpu(clock_info->ci.usMemoryClockLow);
2508 		mclk |= clock_info->ci.ucMemoryClockHigh << 16;
2509 		rdev->pm.power_state[state_index].clock_info[mode_index].mclk = mclk;
2510 		rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2511 		rdev->pm.power_state[state_index].clock_info[mode_index].voltage.type =
2512 			VOLTAGE_NONE;
2513 	} else if (rdev->family >= CHIP_TAHITI) {
2514 		sclk = le16_to_cpu(clock_info->si.usEngineClockLow);
2515 		sclk |= clock_info->si.ucEngineClockHigh << 16;
2516 		mclk = le16_to_cpu(clock_info->si.usMemoryClockLow);
2517 		mclk |= clock_info->si.ucMemoryClockHigh << 16;
2518 		rdev->pm.power_state[state_index].clock_info[mode_index].mclk = mclk;
2519 		rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2520 		rdev->pm.power_state[state_index].clock_info[mode_index].voltage.type =
2521 			VOLTAGE_SW;
2522 		rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage =
2523 			le16_to_cpu(clock_info->si.usVDDC);
2524 		rdev->pm.power_state[state_index].clock_info[mode_index].voltage.vddci =
2525 			le16_to_cpu(clock_info->si.usVDDCI);
2526 	} else if (rdev->family >= CHIP_CEDAR) {
2527 		sclk = le16_to_cpu(clock_info->evergreen.usEngineClockLow);
2528 		sclk |= clock_info->evergreen.ucEngineClockHigh << 16;
2529 		mclk = le16_to_cpu(clock_info->evergreen.usMemoryClockLow);
2530 		mclk |= clock_info->evergreen.ucMemoryClockHigh << 16;
2531 		rdev->pm.power_state[state_index].clock_info[mode_index].mclk = mclk;
2532 		rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2533 		rdev->pm.power_state[state_index].clock_info[mode_index].voltage.type =
2534 			VOLTAGE_SW;
2535 		rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage =
2536 			le16_to_cpu(clock_info->evergreen.usVDDC);
2537 		rdev->pm.power_state[state_index].clock_info[mode_index].voltage.vddci =
2538 			le16_to_cpu(clock_info->evergreen.usVDDCI);
2539 	} else {
2540 		sclk = le16_to_cpu(clock_info->r600.usEngineClockLow);
2541 		sclk |= clock_info->r600.ucEngineClockHigh << 16;
2542 		mclk = le16_to_cpu(clock_info->r600.usMemoryClockLow);
2543 		mclk |= clock_info->r600.ucMemoryClockHigh << 16;
2544 		rdev->pm.power_state[state_index].clock_info[mode_index].mclk = mclk;
2545 		rdev->pm.power_state[state_index].clock_info[mode_index].sclk = sclk;
2546 		rdev->pm.power_state[state_index].clock_info[mode_index].voltage.type =
2547 			VOLTAGE_SW;
2548 		rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage =
2549 			le16_to_cpu(clock_info->r600.usVDDC);
2550 	}
2551 
2552 	/* patch up vddc if necessary */
2553 	switch (rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage) {
2554 	case ATOM_VIRTUAL_VOLTAGE_ID0:
2555 	case ATOM_VIRTUAL_VOLTAGE_ID1:
2556 	case ATOM_VIRTUAL_VOLTAGE_ID2:
2557 	case ATOM_VIRTUAL_VOLTAGE_ID3:
2558 	case ATOM_VIRTUAL_VOLTAGE_ID4:
2559 	case ATOM_VIRTUAL_VOLTAGE_ID5:
2560 	case ATOM_VIRTUAL_VOLTAGE_ID6:
2561 	case ATOM_VIRTUAL_VOLTAGE_ID7:
2562 		if (radeon_atom_get_max_vddc(rdev, VOLTAGE_TYPE_VDDC,
2563 					     rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage,
2564 					     &vddc) == 0)
2565 			rdev->pm.power_state[state_index].clock_info[mode_index].voltage.voltage = vddc;
2566 		break;
2567 	default:
2568 		break;
2569 	}
2570 
2571 	if (rdev->flags & RADEON_IS_IGP) {
2572 		/* skip invalid modes */
2573 		if (rdev->pm.power_state[state_index].clock_info[mode_index].sclk == 0)
2574 			return false;
2575 	} else {
2576 		/* skip invalid modes */
2577 		if ((rdev->pm.power_state[state_index].clock_info[mode_index].mclk == 0) ||
2578 		    (rdev->pm.power_state[state_index].clock_info[mode_index].sclk == 0))
2579 			return false;
2580 	}
2581 	return true;
2582 }
2583 
2584 static int radeon_atombios_parse_power_table_4_5(struct radeon_device *rdev)
2585 {
2586 	struct radeon_mode_info *mode_info = &rdev->mode_info;
2587 	struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
2588 	union pplib_power_state *power_state;
2589 	int i, j;
2590 	int state_index = 0, mode_index = 0;
2591 	union pplib_clock_info *clock_info;
2592 	bool valid;
2593 	union power_info *power_info;
2594 	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2595 	u16 data_offset;
2596 	u8 frev, crev;
2597 
2598 	if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
2599 				   &frev, &crev, &data_offset))
2600 		return state_index;
2601 	power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
2602 
2603 	radeon_atombios_add_pplib_thermal_controller(rdev, &power_info->pplib.sThermalController);
2604 	if (power_info->pplib.ucNumStates == 0)
2605 		return state_index;
2606 	rdev->pm.power_state = kzalloc_objs(struct radeon_power_state,
2607 					    power_info->pplib.ucNumStates);
2608 	if (!rdev->pm.power_state)
2609 		return state_index;
2610 	/* first mode is usually default, followed by low to high */
2611 	for (i = 0; i < power_info->pplib.ucNumStates; i++) {
2612 		mode_index = 0;
2613 		power_state = (union pplib_power_state *)
2614 			(mode_info->atom_context->bios + data_offset +
2615 			 le16_to_cpu(power_info->pplib.usStateArrayOffset) +
2616 			 i * power_info->pplib.ucStateEntrySize);
2617 		non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
2618 			(mode_info->atom_context->bios + data_offset +
2619 			 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset) +
2620 			 (power_state->v1.ucNonClockStateIndex *
2621 			  power_info->pplib.ucNonClockSize));
2622 		rdev->pm.power_state[i].clock_info =
2623 			kzalloc_objs(struct radeon_pm_clock_info,
2624 				     (power_info->pplib.ucStateEntrySize - 1) ? (power_info->pplib.ucStateEntrySize - 1) : 1);
2625 		if (!rdev->pm.power_state[i].clock_info)
2626 			return state_index;
2627 		if (power_info->pplib.ucStateEntrySize - 1) {
2628 			for (j = 0; j < (power_info->pplib.ucStateEntrySize - 1); j++) {
2629 				clock_info = (union pplib_clock_info *)
2630 					(mode_info->atom_context->bios + data_offset +
2631 					 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset) +
2632 					 (power_state->v1.ucClockStateIndices[j] *
2633 					  power_info->pplib.ucClockInfoSize));
2634 				valid = radeon_atombios_parse_pplib_clock_info(rdev,
2635 									       state_index, mode_index,
2636 									       clock_info);
2637 				if (valid)
2638 					mode_index++;
2639 			}
2640 		} else {
2641 			rdev->pm.power_state[state_index].clock_info[0].mclk =
2642 				rdev->clock.default_mclk;
2643 			rdev->pm.power_state[state_index].clock_info[0].sclk =
2644 				rdev->clock.default_sclk;
2645 			mode_index++;
2646 		}
2647 		rdev->pm.power_state[state_index].num_clock_modes = mode_index;
2648 		if (mode_index) {
2649 			radeon_atombios_parse_pplib_non_clock_info(rdev, state_index, mode_index,
2650 								   non_clock_info);
2651 			state_index++;
2652 		}
2653 	}
2654 	/* if multiple clock modes, mark the lowest as no display */
2655 	for (i = 0; i < state_index; i++) {
2656 		if (rdev->pm.power_state[i].num_clock_modes > 1)
2657 			rdev->pm.power_state[i].clock_info[0].flags |=
2658 				RADEON_PM_MODE_NO_DISPLAY;
2659 	}
2660 	/* first mode is usually default */
2661 	if (rdev->pm.default_power_state_index == -1) {
2662 		rdev->pm.power_state[0].type =
2663 			POWER_STATE_TYPE_DEFAULT;
2664 		rdev->pm.default_power_state_index = 0;
2665 		rdev->pm.power_state[0].default_clock_mode =
2666 			&rdev->pm.power_state[0].clock_info[0];
2667 	}
2668 	return state_index;
2669 }
2670 
2671 static int radeon_atombios_parse_power_table_6(struct radeon_device *rdev)
2672 {
2673 	struct radeon_mode_info *mode_info = &rdev->mode_info;
2674 	struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
2675 	union pplib_power_state *power_state;
2676 	int i, j, non_clock_array_index, clock_array_index;
2677 	int state_index = 0, mode_index = 0;
2678 	union pplib_clock_info *clock_info;
2679 	struct _StateArray *state_array;
2680 	struct _ClockInfoArray *clock_info_array;
2681 	struct _NonClockInfoArray *non_clock_info_array;
2682 	bool valid;
2683 	union power_info *power_info;
2684 	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2685 	u16 data_offset;
2686 	u8 frev, crev;
2687 	u8 *power_state_offset;
2688 
2689 	if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
2690 				   &frev, &crev, &data_offset))
2691 		return state_index;
2692 	power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
2693 
2694 	radeon_atombios_add_pplib_thermal_controller(rdev, &power_info->pplib.sThermalController);
2695 	state_array = (struct _StateArray *)
2696 		(mode_info->atom_context->bios + data_offset +
2697 		 le16_to_cpu(power_info->pplib.usStateArrayOffset));
2698 	clock_info_array = (struct _ClockInfoArray *)
2699 		(mode_info->atom_context->bios + data_offset +
2700 		 le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
2701 	non_clock_info_array = (struct _NonClockInfoArray *)
2702 		(mode_info->atom_context->bios + data_offset +
2703 		 le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
2704 	if (state_array->ucNumEntries == 0)
2705 		return state_index;
2706 	rdev->pm.power_state = kzalloc_objs(struct radeon_power_state,
2707 					    state_array->ucNumEntries);
2708 	if (!rdev->pm.power_state)
2709 		return state_index;
2710 	power_state_offset = (u8 *)state_array->states;
2711 	for (i = 0; i < state_array->ucNumEntries; i++) {
2712 		mode_index = 0;
2713 		power_state = (union pplib_power_state *)power_state_offset;
2714 		non_clock_array_index = power_state->v2.nonClockInfoIndex;
2715 		non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
2716 			&non_clock_info_array->nonClockInfo[non_clock_array_index];
2717 		rdev->pm.power_state[i].clock_info =
2718 			kzalloc_objs(struct radeon_pm_clock_info,
2719 				     power_state->v2.ucNumDPMLevels ? power_state->v2.ucNumDPMLevels : 1);
2720 		if (!rdev->pm.power_state[i].clock_info)
2721 			return state_index;
2722 		if (power_state->v2.ucNumDPMLevels) {
2723 			for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
2724 				clock_array_index = power_state->v2.clockInfoIndex[j];
2725 				clock_info = (union pplib_clock_info *)
2726 					&clock_info_array->clockInfo[clock_array_index * clock_info_array->ucEntrySize];
2727 				valid = radeon_atombios_parse_pplib_clock_info(rdev,
2728 									       state_index, mode_index,
2729 									       clock_info);
2730 				if (valid)
2731 					mode_index++;
2732 			}
2733 		} else {
2734 			rdev->pm.power_state[state_index].clock_info[0].mclk =
2735 				rdev->clock.default_mclk;
2736 			rdev->pm.power_state[state_index].clock_info[0].sclk =
2737 				rdev->clock.default_sclk;
2738 			mode_index++;
2739 		}
2740 		rdev->pm.power_state[state_index].num_clock_modes = mode_index;
2741 		if (mode_index) {
2742 			radeon_atombios_parse_pplib_non_clock_info(rdev, state_index, mode_index,
2743 								   non_clock_info);
2744 			state_index++;
2745 		}
2746 		power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
2747 	}
2748 	/* if multiple clock modes, mark the lowest as no display */
2749 	for (i = 0; i < state_index; i++) {
2750 		if (rdev->pm.power_state[i].num_clock_modes > 1)
2751 			rdev->pm.power_state[i].clock_info[0].flags |=
2752 				RADEON_PM_MODE_NO_DISPLAY;
2753 	}
2754 	/* first mode is usually default */
2755 	if (rdev->pm.default_power_state_index == -1) {
2756 		rdev->pm.power_state[0].type =
2757 			POWER_STATE_TYPE_DEFAULT;
2758 		rdev->pm.default_power_state_index = 0;
2759 		rdev->pm.power_state[0].default_clock_mode =
2760 			&rdev->pm.power_state[0].clock_info[0];
2761 	}
2762 	return state_index;
2763 }
2764 
2765 void radeon_atombios_get_power_modes(struct radeon_device *rdev)
2766 {
2767 	struct radeon_mode_info *mode_info = &rdev->mode_info;
2768 	int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
2769 	u16 data_offset;
2770 	u8 frev, crev;
2771 	int state_index = 0;
2772 
2773 	rdev->pm.default_power_state_index = -1;
2774 
2775 	if (atom_parse_data_header(mode_info->atom_context, index, NULL,
2776 				   &frev, &crev, &data_offset)) {
2777 		switch (frev) {
2778 		case 1:
2779 		case 2:
2780 		case 3:
2781 			state_index = radeon_atombios_parse_power_table_1_3(rdev);
2782 			break;
2783 		case 4:
2784 		case 5:
2785 			state_index = radeon_atombios_parse_power_table_4_5(rdev);
2786 			break;
2787 		case 6:
2788 			state_index = radeon_atombios_parse_power_table_6(rdev);
2789 			break;
2790 		default:
2791 			break;
2792 		}
2793 	}
2794 
2795 	if (state_index == 0) {
2796 		rdev->pm.power_state = kzalloc_obj(struct radeon_power_state);
2797 		if (rdev->pm.power_state) {
2798 			rdev->pm.power_state[0].clock_info =
2799 				kzalloc_objs(struct radeon_pm_clock_info, 1);
2800 			if (rdev->pm.power_state[0].clock_info) {
2801 				/* add the default mode */
2802 				rdev->pm.power_state[state_index].type =
2803 					POWER_STATE_TYPE_DEFAULT;
2804 				rdev->pm.power_state[state_index].num_clock_modes = 1;
2805 				rdev->pm.power_state[state_index].clock_info[0].mclk = rdev->clock.default_mclk;
2806 				rdev->pm.power_state[state_index].clock_info[0].sclk = rdev->clock.default_sclk;
2807 				rdev->pm.power_state[state_index].default_clock_mode =
2808 					&rdev->pm.power_state[state_index].clock_info[0];
2809 				rdev->pm.power_state[state_index].clock_info[0].voltage.type = VOLTAGE_NONE;
2810 				rdev->pm.power_state[state_index].pcie_lanes = 16;
2811 				rdev->pm.default_power_state_index = state_index;
2812 				rdev->pm.power_state[state_index].flags = 0;
2813 				state_index++;
2814 			}
2815 		}
2816 	}
2817 
2818 	rdev->pm.num_power_states = state_index;
2819 
2820 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
2821 	rdev->pm.current_clock_mode_index = 0;
2822 	if (rdev->pm.default_power_state_index >= 0)
2823 		rdev->pm.current_vddc =
2824 			rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
2825 	else
2826 		rdev->pm.current_vddc = 0;
2827 }
2828 
2829 union get_clock_dividers {
2830 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS v1;
2831 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V2 v2;
2832 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V3 v3;
2833 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V4 v4;
2834 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V5 v5;
2835 	struct _COMPUTE_GPU_CLOCK_INPUT_PARAMETERS_V1_6 v6_in;
2836 	struct _COMPUTE_GPU_CLOCK_OUTPUT_PARAMETERS_V1_6 v6_out;
2837 };
2838 
2839 int radeon_atom_get_clock_dividers(struct radeon_device *rdev,
2840 				   u8 clock_type,
2841 				   u32 clock,
2842 				   bool strobe_mode,
2843 				   struct atom_clock_dividers *dividers)
2844 {
2845 	union get_clock_dividers args;
2846 	int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryEnginePLL);
2847 	u8 frev, crev;
2848 
2849 	memset(&args, 0, sizeof(args));
2850 	memset(dividers, 0, sizeof(struct atom_clock_dividers));
2851 
2852 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
2853 		return -EINVAL;
2854 
2855 	switch (crev) {
2856 	case 1:
2857 		/* r4xx, r5xx */
2858 		args.v1.ucAction = clock_type;
2859 		args.v1.ulClock = cpu_to_le32(clock);	/* 10 khz */
2860 
2861 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
2862 
2863 		dividers->post_div = args.v1.ucPostDiv;
2864 		dividers->fb_div = args.v1.ucFbDiv;
2865 		dividers->enable_post_div = true;
2866 		break;
2867 	case 2:
2868 	case 3:
2869 	case 5:
2870 		/* r6xx, r7xx, evergreen, ni, si */
2871 		if (rdev->family <= CHIP_RV770) {
2872 			args.v2.ucAction = clock_type;
2873 			args.v2.ulClock = cpu_to_le32(clock);	/* 10 khz */
2874 
2875 			atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
2876 
2877 			dividers->post_div = args.v2.ucPostDiv;
2878 			dividers->fb_div = le16_to_cpu(args.v2.usFbDiv);
2879 			dividers->ref_div = args.v2.ucAction;
2880 			if (rdev->family == CHIP_RV770) {
2881 				dividers->enable_post_div = (le32_to_cpu(args.v2.ulClock) & (1 << 24)) ?
2882 					true : false;
2883 				dividers->vco_mode = (le32_to_cpu(args.v2.ulClock) & (1 << 25)) ? 1 : 0;
2884 			} else
2885 				dividers->enable_post_div = (dividers->fb_div & 1) ? true : false;
2886 		} else {
2887 			if (clock_type == COMPUTE_ENGINE_PLL_PARAM) {
2888 				args.v3.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
2889 
2890 				atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
2891 
2892 				dividers->post_div = args.v3.ucPostDiv;
2893 				dividers->enable_post_div = (args.v3.ucCntlFlag &
2894 							     ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
2895 				dividers->enable_dithen = (args.v3.ucCntlFlag &
2896 							   ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
2897 				dividers->whole_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDiv);
2898 				dividers->frac_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDivFrac);
2899 				dividers->ref_div = args.v3.ucRefDiv;
2900 				dividers->vco_mode = (args.v3.ucCntlFlag &
2901 						      ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
2902 			} else {
2903 				/* for SI we use ComputeMemoryClockParam for memory plls */
2904 				if (rdev->family >= CHIP_TAHITI)
2905 					return -EINVAL;
2906 				args.v5.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
2907 				if (strobe_mode)
2908 					args.v5.ucInputFlag = ATOM_PLL_INPUT_FLAG_PLL_STROBE_MODE_EN;
2909 
2910 				atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
2911 
2912 				dividers->post_div = args.v5.ucPostDiv;
2913 				dividers->enable_post_div = (args.v5.ucCntlFlag &
2914 							     ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
2915 				dividers->enable_dithen = (args.v5.ucCntlFlag &
2916 							   ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
2917 				dividers->whole_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDiv);
2918 				dividers->frac_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDivFrac);
2919 				dividers->ref_div = args.v5.ucRefDiv;
2920 				dividers->vco_mode = (args.v5.ucCntlFlag &
2921 						      ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
2922 			}
2923 		}
2924 		break;
2925 	case 4:
2926 		/* fusion */
2927 		args.v4.ulClock = cpu_to_le32(clock);	/* 10 khz */
2928 
2929 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
2930 
2931 		dividers->post_divider = dividers->post_div = args.v4.ucPostDiv;
2932 		dividers->real_clock = le32_to_cpu(args.v4.ulClock);
2933 		break;
2934 	case 6:
2935 		/* CI */
2936 		/* COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK, COMPUTE_GPUCLK_INPUT_FLAG_SCLK */
2937 		args.v6_in.ulClock.ulComputeClockFlag = clock_type;
2938 		args.v6_in.ulClock.ulClockFreq = cpu_to_le32(clock);	/* 10 khz */
2939 
2940 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
2941 
2942 		dividers->whole_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDiv);
2943 		dividers->frac_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDivFrac);
2944 		dividers->ref_div = args.v6_out.ucPllRefDiv;
2945 		dividers->post_div = args.v6_out.ucPllPostDiv;
2946 		dividers->flags = args.v6_out.ucPllCntlFlag;
2947 		dividers->real_clock = le32_to_cpu(args.v6_out.ulClock.ulClock);
2948 		dividers->post_divider = args.v6_out.ulClock.ucPostDiv;
2949 		break;
2950 	default:
2951 		return -EINVAL;
2952 	}
2953 	return 0;
2954 }
2955 
2956 int radeon_atom_get_memory_pll_dividers(struct radeon_device *rdev,
2957 					u32 clock,
2958 					bool strobe_mode,
2959 					struct atom_mpll_param *mpll_param)
2960 {
2961 	COMPUTE_MEMORY_CLOCK_PARAM_PARAMETERS_V2_1 args;
2962 	int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryClockParam);
2963 	u8 frev, crev;
2964 
2965 	memset(&args, 0, sizeof(args));
2966 	memset(mpll_param, 0, sizeof(struct atom_mpll_param));
2967 
2968 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
2969 		return -EINVAL;
2970 
2971 	switch (frev) {
2972 	case 2:
2973 		switch (crev) {
2974 		case 1:
2975 			/* SI */
2976 			args.ulClock = cpu_to_le32(clock);	/* 10 khz */
2977 			args.ucInputFlag = 0;
2978 			if (strobe_mode)
2979 				args.ucInputFlag |= MPLL_INPUT_FLAG_STROBE_MODE_EN;
2980 
2981 			atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
2982 
2983 			mpll_param->clkfrac = le16_to_cpu(args.ulFbDiv.usFbDivFrac);
2984 			mpll_param->clkf = le16_to_cpu(args.ulFbDiv.usFbDiv);
2985 			mpll_param->post_div = args.ucPostDiv;
2986 			mpll_param->dll_speed = args.ucDllSpeed;
2987 			mpll_param->bwcntl = args.ucBWCntl;
2988 			mpll_param->vco_mode =
2989 				(args.ucPllCntlFlag & MPLL_CNTL_FLAG_VCO_MODE_MASK);
2990 			mpll_param->yclk_sel =
2991 				(args.ucPllCntlFlag & MPLL_CNTL_FLAG_BYPASS_DQ_PLL) ? 1 : 0;
2992 			mpll_param->qdr =
2993 				(args.ucPllCntlFlag & MPLL_CNTL_FLAG_QDR_ENABLE) ? 1 : 0;
2994 			mpll_param->half_rate =
2995 				(args.ucPllCntlFlag & MPLL_CNTL_FLAG_AD_HALF_RATE) ? 1 : 0;
2996 			break;
2997 		default:
2998 			return -EINVAL;
2999 		}
3000 		break;
3001 	default:
3002 		return -EINVAL;
3003 	}
3004 	return 0;
3005 }
3006 
3007 void radeon_atom_set_clock_gating(struct radeon_device *rdev, int enable)
3008 {
3009 	DYNAMIC_CLOCK_GATING_PS_ALLOCATION args;
3010 	int index = GetIndexIntoMasterTable(COMMAND, DynamicClockGating);
3011 
3012 	args.ucEnable = enable;
3013 
3014 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3015 }
3016 
3017 uint32_t radeon_atom_get_engine_clock(struct radeon_device *rdev)
3018 {
3019 	GET_ENGINE_CLOCK_PS_ALLOCATION args;
3020 	int index = GetIndexIntoMasterTable(COMMAND, GetEngineClock);
3021 
3022 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3023 	return le32_to_cpu(args.ulReturnEngineClock);
3024 }
3025 
3026 uint32_t radeon_atom_get_memory_clock(struct radeon_device *rdev)
3027 {
3028 	GET_MEMORY_CLOCK_PS_ALLOCATION args;
3029 	int index = GetIndexIntoMasterTable(COMMAND, GetMemoryClock);
3030 
3031 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3032 	return le32_to_cpu(args.ulReturnMemoryClock);
3033 }
3034 
3035 void radeon_atom_set_engine_clock(struct radeon_device *rdev,
3036 				  uint32_t eng_clock)
3037 {
3038 	SET_ENGINE_CLOCK_PS_ALLOCATION args;
3039 	int index = GetIndexIntoMasterTable(COMMAND, SetEngineClock);
3040 
3041 	args.ulTargetEngineClock = cpu_to_le32(eng_clock);	/* 10 khz */
3042 
3043 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3044 }
3045 
3046 void radeon_atom_set_memory_clock(struct radeon_device *rdev,
3047 				  uint32_t mem_clock)
3048 {
3049 	SET_MEMORY_CLOCK_PS_ALLOCATION args;
3050 	int index = GetIndexIntoMasterTable(COMMAND, SetMemoryClock);
3051 
3052 	if (rdev->flags & RADEON_IS_IGP)
3053 		return;
3054 
3055 	args.ulTargetMemoryClock = cpu_to_le32(mem_clock);	/* 10 khz */
3056 
3057 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3058 }
3059 
3060 void radeon_atom_set_engine_dram_timings(struct radeon_device *rdev,
3061 					 u32 eng_clock, u32 mem_clock)
3062 {
3063 	SET_ENGINE_CLOCK_PS_ALLOCATION args;
3064 	int index = GetIndexIntoMasterTable(COMMAND, DynamicMemorySettings);
3065 	u32 tmp;
3066 
3067 	memset(&args, 0, sizeof(args));
3068 
3069 	tmp = eng_clock & SET_CLOCK_FREQ_MASK;
3070 	tmp |= (COMPUTE_ENGINE_PLL_PARAM << 24);
3071 
3072 	args.ulTargetEngineClock = cpu_to_le32(tmp);
3073 	if (mem_clock)
3074 		args.sReserved.ulClock = cpu_to_le32(mem_clock & SET_CLOCK_FREQ_MASK);
3075 
3076 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3077 }
3078 
3079 void radeon_atom_update_memory_dll(struct radeon_device *rdev,
3080 				   u32 mem_clock)
3081 {
3082 	u32 args;
3083 	int index = GetIndexIntoMasterTable(COMMAND, DynamicMemorySettings);
3084 
3085 	args = cpu_to_le32(mem_clock);	/* 10 khz */
3086 
3087 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3088 }
3089 
3090 void radeon_atom_set_ac_timing(struct radeon_device *rdev,
3091 			       u32 mem_clock)
3092 {
3093 	SET_MEMORY_CLOCK_PS_ALLOCATION args;
3094 	int index = GetIndexIntoMasterTable(COMMAND, DynamicMemorySettings);
3095 	u32 tmp = mem_clock | (COMPUTE_MEMORY_PLL_PARAM << 24);
3096 
3097 	args.ulTargetMemoryClock = cpu_to_le32(tmp);	/* 10 khz */
3098 
3099 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3100 }
3101 
3102 union set_voltage {
3103 	struct _SET_VOLTAGE_PS_ALLOCATION alloc;
3104 	struct _SET_VOLTAGE_PARAMETERS v1;
3105 	struct _SET_VOLTAGE_PARAMETERS_V2 v2;
3106 	struct _SET_VOLTAGE_PARAMETERS_V1_3 v3;
3107 };
3108 
3109 void radeon_atom_set_voltage(struct radeon_device *rdev, u16 voltage_level, u8 voltage_type)
3110 {
3111 	union set_voltage args;
3112 	int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
3113 	u8 frev, crev, volt_index = voltage_level;
3114 
3115 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
3116 		return;
3117 
3118 	/* 0xff01 is a flag rather then an actual voltage */
3119 	if (voltage_level == 0xff01)
3120 		return;
3121 
3122 	switch (crev) {
3123 	case 1:
3124 		args.v1.ucVoltageType = voltage_type;
3125 		args.v1.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_ALL_SOURCE;
3126 		args.v1.ucVoltageIndex = volt_index;
3127 		break;
3128 	case 2:
3129 		args.v2.ucVoltageType = voltage_type;
3130 		args.v2.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_SET_VOLTAGE;
3131 		args.v2.usVoltageLevel = cpu_to_le16(voltage_level);
3132 		break;
3133 	case 3:
3134 		args.v3.ucVoltageType = voltage_type;
3135 		args.v3.ucVoltageMode = ATOM_SET_VOLTAGE;
3136 		args.v3.usVoltageLevel = cpu_to_le16(voltage_level);
3137 		break;
3138 	default:
3139 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3140 		return;
3141 	}
3142 
3143 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3144 }
3145 
3146 int radeon_atom_get_max_vddc(struct radeon_device *rdev, u8 voltage_type,
3147 			     u16 voltage_id, u16 *voltage)
3148 {
3149 	union set_voltage args;
3150 	int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
3151 	u8 frev, crev;
3152 
3153 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
3154 		return -EINVAL;
3155 
3156 	switch (crev) {
3157 	case 1:
3158 		return -EINVAL;
3159 	case 2:
3160 		args.v2.ucVoltageType = SET_VOLTAGE_GET_MAX_VOLTAGE;
3161 		args.v2.ucVoltageMode = 0;
3162 		args.v2.usVoltageLevel = 0;
3163 
3164 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3165 
3166 		*voltage = le16_to_cpu(args.v2.usVoltageLevel);
3167 		break;
3168 	case 3:
3169 		args.v3.ucVoltageType = voltage_type;
3170 		args.v3.ucVoltageMode = ATOM_GET_VOLTAGE_LEVEL;
3171 		args.v3.usVoltageLevel = cpu_to_le16(voltage_id);
3172 
3173 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3174 
3175 		*voltage = le16_to_cpu(args.v3.usVoltageLevel);
3176 		break;
3177 	default:
3178 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3179 		return -EINVAL;
3180 	}
3181 
3182 	return 0;
3183 }
3184 
3185 int radeon_atom_get_leakage_vddc_based_on_leakage_idx(struct radeon_device *rdev,
3186 						      u16 *voltage,
3187 						      u16 leakage_idx)
3188 {
3189 	return radeon_atom_get_max_vddc(rdev, VOLTAGE_TYPE_VDDC, leakage_idx, voltage);
3190 }
3191 
3192 int radeon_atom_get_leakage_id_from_vbios(struct radeon_device *rdev,
3193 					  u16 *leakage_id)
3194 {
3195 	union set_voltage args;
3196 	int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
3197 	u8 frev, crev;
3198 
3199 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
3200 		return -EINVAL;
3201 
3202 	switch (crev) {
3203 	case 3:
3204 	case 4:
3205 		args.v3.ucVoltageType = 0;
3206 		args.v3.ucVoltageMode = ATOM_GET_LEAKAGE_ID;
3207 		args.v3.usVoltageLevel = 0;
3208 
3209 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3210 
3211 		*leakage_id = le16_to_cpu(args.v3.usVoltageLevel);
3212 		break;
3213 	default:
3214 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3215 		return -EINVAL;
3216 	}
3217 
3218 	return 0;
3219 }
3220 
3221 int radeon_atom_get_leakage_vddc_based_on_leakage_params(struct radeon_device *rdev,
3222 							 u16 *vddc, u16 *vddci,
3223 							 u16 virtual_voltage_id,
3224 							 u16 vbios_voltage_id)
3225 {
3226 	int index = GetIndexIntoMasterTable(DATA, ASIC_ProfilingInfo);
3227 	u8 frev, crev;
3228 	u16 data_offset, size;
3229 	int i, j;
3230 	ATOM_ASIC_PROFILING_INFO_V2_1 *profile;
3231 	u16 *leakage_bin, *vddc_id_buf, *vddc_buf, *vddci_id_buf, *vddci_buf;
3232 
3233 	*vddc = 0;
3234 	*vddci = 0;
3235 
3236 	if (!atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
3237 				    &frev, &crev, &data_offset))
3238 		return -EINVAL;
3239 
3240 	profile = (ATOM_ASIC_PROFILING_INFO_V2_1 *)
3241 		(rdev->mode_info.atom_context->bios + data_offset);
3242 
3243 	switch (frev) {
3244 	case 1:
3245 		return -EINVAL;
3246 	case 2:
3247 		switch (crev) {
3248 		case 1:
3249 			if (size < sizeof(ATOM_ASIC_PROFILING_INFO_V2_1))
3250 				return -EINVAL;
3251 			leakage_bin = (u16 *)
3252 				(rdev->mode_info.atom_context->bios + data_offset +
3253 				 le16_to_cpu(profile->usLeakageBinArrayOffset));
3254 			vddc_id_buf = (u16 *)
3255 				(rdev->mode_info.atom_context->bios + data_offset +
3256 				 le16_to_cpu(profile->usElbVDDC_IdArrayOffset));
3257 			vddc_buf = (u16 *)
3258 				(rdev->mode_info.atom_context->bios + data_offset +
3259 				 le16_to_cpu(profile->usElbVDDC_LevelArrayOffset));
3260 			vddci_id_buf = (u16 *)
3261 				(rdev->mode_info.atom_context->bios + data_offset +
3262 				 le16_to_cpu(profile->usElbVDDCI_IdArrayOffset));
3263 			vddci_buf = (u16 *)
3264 				(rdev->mode_info.atom_context->bios + data_offset +
3265 				 le16_to_cpu(profile->usElbVDDCI_LevelArrayOffset));
3266 
3267 			if (profile->ucElbVDDC_Num > 0) {
3268 				for (i = 0; i < profile->ucElbVDDC_Num; i++) {
3269 					if (vddc_id_buf[i] == virtual_voltage_id) {
3270 						for (j = 0; j < profile->ucLeakageBinNum; j++) {
3271 							if (vbios_voltage_id <= leakage_bin[j]) {
3272 								*vddc = vddc_buf[j * profile->ucElbVDDC_Num + i];
3273 								break;
3274 							}
3275 						}
3276 						break;
3277 					}
3278 				}
3279 			}
3280 			if (profile->ucElbVDDCI_Num > 0) {
3281 				for (i = 0; i < profile->ucElbVDDCI_Num; i++) {
3282 					if (vddci_id_buf[i] == virtual_voltage_id) {
3283 						for (j = 0; j < profile->ucLeakageBinNum; j++) {
3284 							if (vbios_voltage_id <= leakage_bin[j]) {
3285 								*vddci = vddci_buf[j * profile->ucElbVDDCI_Num + i];
3286 								break;
3287 							}
3288 						}
3289 						break;
3290 					}
3291 				}
3292 			}
3293 			break;
3294 		default:
3295 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3296 			return -EINVAL;
3297 		}
3298 		break;
3299 	default:
3300 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3301 		return -EINVAL;
3302 	}
3303 
3304 	return 0;
3305 }
3306 
3307 union get_voltage_info {
3308 	struct  _GET_VOLTAGE_INFO_INPUT_PARAMETER_V1_2 in;
3309 	struct  _GET_EVV_VOLTAGE_INFO_OUTPUT_PARAMETER_V1_2 evv_out;
3310 };
3311 
3312 int radeon_atom_get_voltage_evv(struct radeon_device *rdev,
3313 				u16 virtual_voltage_id,
3314 				u16 *voltage)
3315 {
3316 	int index = GetIndexIntoMasterTable(COMMAND, GetVoltageInfo);
3317 	u32 entry_id;
3318 	u32 count = rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count;
3319 	union get_voltage_info args;
3320 
3321 	for (entry_id = 0; entry_id < count; entry_id++) {
3322 		if (rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].v ==
3323 		    virtual_voltage_id)
3324 			break;
3325 	}
3326 
3327 	if (entry_id >= count)
3328 		return -EINVAL;
3329 
3330 	args.in.ucVoltageType = VOLTAGE_TYPE_VDDC;
3331 	args.in.ucVoltageMode = ATOM_GET_VOLTAGE_EVV_VOLTAGE;
3332 	args.in.usVoltageLevel = cpu_to_le16(virtual_voltage_id);
3333 	args.in.ulSCLKFreq =
3334 		cpu_to_le32(rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].clk);
3335 
3336 	atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3337 
3338 	*voltage = le16_to_cpu(args.evv_out.usVoltageLevel);
3339 
3340 	return 0;
3341 }
3342 
3343 int radeon_atom_get_voltage_gpio_settings(struct radeon_device *rdev,
3344 					  u16 voltage_level, u8 voltage_type,
3345 					  u32 *gpio_value, u32 *gpio_mask)
3346 {
3347 	union set_voltage args;
3348 	int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
3349 	u8 frev, crev;
3350 
3351 	if (!atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev))
3352 		return -EINVAL;
3353 
3354 	switch (crev) {
3355 	case 1:
3356 		return -EINVAL;
3357 	case 2:
3358 		args.v2.ucVoltageType = voltage_type;
3359 		args.v2.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_GET_GPIOMASK;
3360 		args.v2.usVoltageLevel = cpu_to_le16(voltage_level);
3361 
3362 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3363 
3364 		*gpio_mask = le32_to_cpu(*(u32 *)&args.v2);
3365 
3366 		args.v2.ucVoltageType = voltage_type;
3367 		args.v2.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_GET_GPIOVAL;
3368 		args.v2.usVoltageLevel = cpu_to_le16(voltage_level);
3369 
3370 		atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args, sizeof(args));
3371 
3372 		*gpio_value = le32_to_cpu(*(u32 *)&args.v2);
3373 		break;
3374 	default:
3375 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3376 		return -EINVAL;
3377 	}
3378 
3379 	return 0;
3380 }
3381 
3382 union voltage_object_info {
3383 	struct _ATOM_VOLTAGE_OBJECT_INFO v1;
3384 	struct _ATOM_VOLTAGE_OBJECT_INFO_V2 v2;
3385 	struct _ATOM_VOLTAGE_OBJECT_INFO_V3_1 v3;
3386 };
3387 
3388 union voltage_object {
3389 	struct _ATOM_VOLTAGE_OBJECT v1;
3390 	struct _ATOM_VOLTAGE_OBJECT_V2 v2;
3391 	union _ATOM_VOLTAGE_OBJECT_V3 v3;
3392 };
3393 
3394 static ATOM_VOLTAGE_OBJECT *atom_lookup_voltage_object_v1(ATOM_VOLTAGE_OBJECT_INFO *v1,
3395 							  u8 voltage_type)
3396 {
3397 	u32 size = le16_to_cpu(v1->sHeader.usStructureSize);
3398 	u32 offset = offsetof(ATOM_VOLTAGE_OBJECT_INFO, asVoltageObj[0]);
3399 	u8 *start = (u8 *)v1;
3400 
3401 	while (offset < size) {
3402 		ATOM_VOLTAGE_OBJECT *vo = (ATOM_VOLTAGE_OBJECT *)(start + offset);
3403 		if (vo->ucVoltageType == voltage_type)
3404 			return vo;
3405 		offset += offsetof(ATOM_VOLTAGE_OBJECT, asFormula.ucVIDAdjustEntries) +
3406 			vo->asFormula.ucNumOfVoltageEntries;
3407 	}
3408 	return NULL;
3409 }
3410 
3411 static ATOM_VOLTAGE_OBJECT_V2 *atom_lookup_voltage_object_v2(ATOM_VOLTAGE_OBJECT_INFO_V2 *v2,
3412 							     u8 voltage_type)
3413 {
3414 	u32 size = le16_to_cpu(v2->sHeader.usStructureSize);
3415 	u32 offset = offsetof(ATOM_VOLTAGE_OBJECT_INFO_V2, asVoltageObj[0]);
3416 	u8 *start = (u8 *)v2;
3417 
3418 	while (offset < size) {
3419 		ATOM_VOLTAGE_OBJECT_V2 *vo = (ATOM_VOLTAGE_OBJECT_V2 *)(start + offset);
3420 		if (vo->ucVoltageType == voltage_type)
3421 			return vo;
3422 		offset += offsetof(ATOM_VOLTAGE_OBJECT_V2, asFormula.asVIDAdjustEntries) +
3423 			(vo->asFormula.ucNumOfVoltageEntries * sizeof(VOLTAGE_LUT_ENTRY));
3424 	}
3425 	return NULL;
3426 }
3427 
3428 static ATOM_VOLTAGE_OBJECT_V3 *atom_lookup_voltage_object_v3(ATOM_VOLTAGE_OBJECT_INFO_V3_1 *v3,
3429 							     u8 voltage_type, u8 voltage_mode)
3430 {
3431 	u32 size = le16_to_cpu(v3->sHeader.usStructureSize);
3432 	u32 offset = offsetof(ATOM_VOLTAGE_OBJECT_INFO_V3_1, asVoltageObj[0]);
3433 	u8 *start = (u8 *)v3;
3434 
3435 	while (offset < size) {
3436 		ATOM_VOLTAGE_OBJECT_V3 *vo = (ATOM_VOLTAGE_OBJECT_V3 *)(start + offset);
3437 		if ((vo->asGpioVoltageObj.sHeader.ucVoltageType == voltage_type) &&
3438 		    (vo->asGpioVoltageObj.sHeader.ucVoltageMode == voltage_mode))
3439 			return vo;
3440 		offset += le16_to_cpu(vo->asGpioVoltageObj.sHeader.usSize);
3441 	}
3442 	return NULL;
3443 }
3444 
3445 bool
3446 radeon_atom_is_voltage_gpio(struct radeon_device *rdev,
3447 			    u8 voltage_type, u8 voltage_mode)
3448 {
3449 	int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
3450 	u8 frev, crev;
3451 	u16 data_offset, size;
3452 	union voltage_object_info *voltage_info;
3453 	union voltage_object *voltage_object = NULL;
3454 
3455 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
3456 				   &frev, &crev, &data_offset)) {
3457 		voltage_info = (union voltage_object_info *)
3458 			(rdev->mode_info.atom_context->bios + data_offset);
3459 
3460 		switch (frev) {
3461 		case 1:
3462 		case 2:
3463 			switch (crev) {
3464 			case 1:
3465 				voltage_object = (union voltage_object *)
3466 					atom_lookup_voltage_object_v1(&voltage_info->v1, voltage_type);
3467 				if (voltage_object &&
3468 				    (voltage_object->v1.asControl.ucVoltageControlId == VOLTAGE_CONTROLLED_BY_GPIO))
3469 					return true;
3470 				break;
3471 			case 2:
3472 				voltage_object = (union voltage_object *)
3473 					atom_lookup_voltage_object_v2(&voltage_info->v2, voltage_type);
3474 				if (voltage_object &&
3475 				    (voltage_object->v2.asControl.ucVoltageControlId == VOLTAGE_CONTROLLED_BY_GPIO))
3476 					return true;
3477 				break;
3478 			default:
3479 				DRM_ERROR("unknown voltage object table\n");
3480 				return false;
3481 			}
3482 			break;
3483 		case 3:
3484 			switch (crev) {
3485 			case 1:
3486 				if (atom_lookup_voltage_object_v3(&voltage_info->v3,
3487 								  voltage_type, voltage_mode))
3488 					return true;
3489 				break;
3490 			default:
3491 				DRM_ERROR("unknown voltage object table\n");
3492 				return false;
3493 			}
3494 			break;
3495 		default:
3496 			DRM_ERROR("unknown voltage object table\n");
3497 			return false;
3498 		}
3499 
3500 	}
3501 	return false;
3502 }
3503 
3504 int radeon_atom_get_svi2_info(struct radeon_device *rdev,
3505 			      u8 voltage_type,
3506 			      u8 *svd_gpio_id, u8 *svc_gpio_id)
3507 {
3508 	int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
3509 	u8 frev, crev;
3510 	u16 data_offset, size;
3511 	union voltage_object_info *voltage_info;
3512 	union voltage_object *voltage_object = NULL;
3513 
3514 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
3515 				   &frev, &crev, &data_offset)) {
3516 		voltage_info = (union voltage_object_info *)
3517 			(rdev->mode_info.atom_context->bios + data_offset);
3518 
3519 		switch (frev) {
3520 		case 3:
3521 			switch (crev) {
3522 			case 1:
3523 				voltage_object = (union voltage_object *)
3524 					atom_lookup_voltage_object_v3(&voltage_info->v3,
3525 								      voltage_type,
3526 								      VOLTAGE_OBJ_SVID2);
3527 				if (voltage_object) {
3528 					*svd_gpio_id = voltage_object->v3.asSVID2Obj.ucSVDGpioId;
3529 					*svc_gpio_id = voltage_object->v3.asSVID2Obj.ucSVCGpioId;
3530 				} else {
3531 					return -EINVAL;
3532 				}
3533 				break;
3534 			default:
3535 				DRM_ERROR("unknown voltage object table\n");
3536 				return -EINVAL;
3537 			}
3538 			break;
3539 		default:
3540 			DRM_ERROR("unknown voltage object table\n");
3541 			return -EINVAL;
3542 		}
3543 
3544 	}
3545 	return 0;
3546 }
3547 
3548 int radeon_atom_get_max_voltage(struct radeon_device *rdev,
3549 				u8 voltage_type, u16 *max_voltage)
3550 {
3551 	int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
3552 	u8 frev, crev;
3553 	u16 data_offset, size;
3554 	union voltage_object_info *voltage_info;
3555 	union voltage_object *voltage_object = NULL;
3556 
3557 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
3558 				   &frev, &crev, &data_offset)) {
3559 		voltage_info = (union voltage_object_info *)
3560 			(rdev->mode_info.atom_context->bios + data_offset);
3561 
3562 		switch (crev) {
3563 		case 1:
3564 			voltage_object = (union voltage_object *)
3565 				atom_lookup_voltage_object_v1(&voltage_info->v1, voltage_type);
3566 			if (voltage_object) {
3567 				ATOM_VOLTAGE_FORMULA *formula =
3568 					&voltage_object->v1.asFormula;
3569 				if (formula->ucFlag & 1)
3570 					*max_voltage =
3571 						le16_to_cpu(formula->usVoltageBaseLevel) +
3572 						formula->ucNumOfVoltageEntries / 2 *
3573 						le16_to_cpu(formula->usVoltageStep);
3574 				else
3575 					*max_voltage =
3576 						le16_to_cpu(formula->usVoltageBaseLevel) +
3577 						(formula->ucNumOfVoltageEntries - 1) *
3578 						le16_to_cpu(formula->usVoltageStep);
3579 				return 0;
3580 			}
3581 			break;
3582 		case 2:
3583 			voltage_object = (union voltage_object *)
3584 				atom_lookup_voltage_object_v2(&voltage_info->v2, voltage_type);
3585 			if (voltage_object) {
3586 				ATOM_VOLTAGE_FORMULA_V2 *formula =
3587 					&voltage_object->v2.asFormula;
3588 				if (formula->ucNumOfVoltageEntries) {
3589 					VOLTAGE_LUT_ENTRY *lut = (VOLTAGE_LUT_ENTRY *)
3590 						((u8 *)&formula->asVIDAdjustEntries[0] +
3591 						 (sizeof(VOLTAGE_LUT_ENTRY) * (formula->ucNumOfVoltageEntries - 1)));
3592 					*max_voltage =
3593 						le16_to_cpu(lut->usVoltageValue);
3594 					return 0;
3595 				}
3596 			}
3597 			break;
3598 		default:
3599 			DRM_ERROR("unknown voltage object table\n");
3600 			return -EINVAL;
3601 		}
3602 
3603 	}
3604 	return -EINVAL;
3605 }
3606 
3607 int radeon_atom_get_min_voltage(struct radeon_device *rdev,
3608 				u8 voltage_type, u16 *min_voltage)
3609 {
3610 	int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
3611 	u8 frev, crev;
3612 	u16 data_offset, size;
3613 	union voltage_object_info *voltage_info;
3614 	union voltage_object *voltage_object = NULL;
3615 
3616 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
3617 				   &frev, &crev, &data_offset)) {
3618 		voltage_info = (union voltage_object_info *)
3619 			(rdev->mode_info.atom_context->bios + data_offset);
3620 
3621 		switch (crev) {
3622 		case 1:
3623 			voltage_object = (union voltage_object *)
3624 				atom_lookup_voltage_object_v1(&voltage_info->v1, voltage_type);
3625 			if (voltage_object) {
3626 				ATOM_VOLTAGE_FORMULA *formula =
3627 					&voltage_object->v1.asFormula;
3628 				*min_voltage =
3629 					le16_to_cpu(formula->usVoltageBaseLevel);
3630 				return 0;
3631 			}
3632 			break;
3633 		case 2:
3634 			voltage_object = (union voltage_object *)
3635 				atom_lookup_voltage_object_v2(&voltage_info->v2, voltage_type);
3636 			if (voltage_object) {
3637 				ATOM_VOLTAGE_FORMULA_V2 *formula =
3638 					&voltage_object->v2.asFormula;
3639 				if (formula->ucNumOfVoltageEntries) {
3640 					*min_voltage =
3641 						le16_to_cpu(formula->asVIDAdjustEntries[
3642 								    0
3643 								    ].usVoltageValue);
3644 					return 0;
3645 				}
3646 			}
3647 			break;
3648 		default:
3649 			DRM_ERROR("unknown voltage object table\n");
3650 			return -EINVAL;
3651 		}
3652 
3653 	}
3654 	return -EINVAL;
3655 }
3656 
3657 int radeon_atom_get_voltage_step(struct radeon_device *rdev,
3658 				 u8 voltage_type, u16 *voltage_step)
3659 {
3660 	int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
3661 	u8 frev, crev;
3662 	u16 data_offset, size;
3663 	union voltage_object_info *voltage_info;
3664 	union voltage_object *voltage_object = NULL;
3665 
3666 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
3667 				   &frev, &crev, &data_offset)) {
3668 		voltage_info = (union voltage_object_info *)
3669 			(rdev->mode_info.atom_context->bios + data_offset);
3670 
3671 		switch (crev) {
3672 		case 1:
3673 			voltage_object = (union voltage_object *)
3674 				atom_lookup_voltage_object_v1(&voltage_info->v1, voltage_type);
3675 			if (voltage_object) {
3676 				ATOM_VOLTAGE_FORMULA *formula =
3677 					&voltage_object->v1.asFormula;
3678 				if (formula->ucFlag & 1)
3679 					*voltage_step =
3680 						(le16_to_cpu(formula->usVoltageStep) + 1) / 2;
3681 				else
3682 					*voltage_step =
3683 						le16_to_cpu(formula->usVoltageStep);
3684 				return 0;
3685 			}
3686 			break;
3687 		case 2:
3688 			return -EINVAL;
3689 		default:
3690 			DRM_ERROR("unknown voltage object table\n");
3691 			return -EINVAL;
3692 		}
3693 
3694 	}
3695 	return -EINVAL;
3696 }
3697 
3698 int radeon_atom_round_to_true_voltage(struct radeon_device *rdev,
3699 				      u8 voltage_type,
3700 				      u16 nominal_voltage,
3701 				      u16 *true_voltage)
3702 {
3703 	u16 min_voltage, max_voltage, voltage_step;
3704 
3705 	if (radeon_atom_get_max_voltage(rdev, voltage_type, &max_voltage))
3706 		return -EINVAL;
3707 	if (radeon_atom_get_min_voltage(rdev, voltage_type, &min_voltage))
3708 		return -EINVAL;
3709 	if (radeon_atom_get_voltage_step(rdev, voltage_type, &voltage_step))
3710 		return -EINVAL;
3711 
3712 	if (nominal_voltage <= min_voltage)
3713 		*true_voltage = min_voltage;
3714 	else if (nominal_voltage >= max_voltage)
3715 		*true_voltage = max_voltage;
3716 	else
3717 		*true_voltage = min_voltage +
3718 			((nominal_voltage - min_voltage) / voltage_step) *
3719 			voltage_step;
3720 
3721 	return 0;
3722 }
3723 
3724 int radeon_atom_get_voltage_table(struct radeon_device *rdev,
3725 				  u8 voltage_type, u8 voltage_mode,
3726 				  struct atom_voltage_table *voltage_table)
3727 {
3728 	int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
3729 	u8 frev, crev;
3730 	u16 data_offset, size;
3731 	int i, ret;
3732 	union voltage_object_info *voltage_info;
3733 	union voltage_object *voltage_object = NULL;
3734 
3735 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
3736 				   &frev, &crev, &data_offset)) {
3737 		voltage_info = (union voltage_object_info *)
3738 			(rdev->mode_info.atom_context->bios + data_offset);
3739 
3740 		switch (frev) {
3741 		case 1:
3742 		case 2:
3743 			switch (crev) {
3744 			case 1:
3745 				DRM_ERROR("old table version %d, %d\n", frev, crev);
3746 				return -EINVAL;
3747 			case 2:
3748 				voltage_object = (union voltage_object *)
3749 					atom_lookup_voltage_object_v2(&voltage_info->v2, voltage_type);
3750 				if (voltage_object) {
3751 					ATOM_VOLTAGE_FORMULA_V2 *formula =
3752 						&voltage_object->v2.asFormula;
3753 					VOLTAGE_LUT_ENTRY *lut;
3754 					if (formula->ucNumOfVoltageEntries > MAX_VOLTAGE_ENTRIES)
3755 						return -EINVAL;
3756 					lut = &formula->asVIDAdjustEntries[0];
3757 					for (i = 0; i < formula->ucNumOfVoltageEntries; i++) {
3758 						voltage_table->entries[i].value =
3759 							le16_to_cpu(lut->usVoltageValue);
3760 						ret = radeon_atom_get_voltage_gpio_settings(rdev,
3761 											    voltage_table->entries[i].value,
3762 											    voltage_type,
3763 											    &voltage_table->entries[i].smio_low,
3764 											    &voltage_table->mask_low);
3765 						if (ret)
3766 							return ret;
3767 						lut = (VOLTAGE_LUT_ENTRY *)
3768 							((u8 *)lut + sizeof(VOLTAGE_LUT_ENTRY));
3769 					}
3770 					voltage_table->count = formula->ucNumOfVoltageEntries;
3771 					return 0;
3772 				}
3773 				break;
3774 			default:
3775 				DRM_ERROR("unknown voltage object table\n");
3776 				return -EINVAL;
3777 			}
3778 			break;
3779 		case 3:
3780 			switch (crev) {
3781 			case 1:
3782 				voltage_object = (union voltage_object *)
3783 					atom_lookup_voltage_object_v3(&voltage_info->v3,
3784 								      voltage_type, voltage_mode);
3785 				if (voltage_object) {
3786 					ATOM_GPIO_VOLTAGE_OBJECT_V3 *gpio =
3787 						&voltage_object->v3.asGpioVoltageObj;
3788 					VOLTAGE_LUT_ENTRY_V2 *lut;
3789 					if (gpio->ucGpioEntryNum > MAX_VOLTAGE_ENTRIES)
3790 						return -EINVAL;
3791 					lut = &gpio->asVolGpioLut[0];
3792 					for (i = 0; i < gpio->ucGpioEntryNum; i++) {
3793 						voltage_table->entries[i].value =
3794 							le16_to_cpu(lut->usVoltageValue);
3795 						voltage_table->entries[i].smio_low =
3796 							le32_to_cpu(lut->ulVoltageId);
3797 						lut = (VOLTAGE_LUT_ENTRY_V2 *)
3798 							((u8 *)lut + sizeof(VOLTAGE_LUT_ENTRY_V2));
3799 					}
3800 					voltage_table->mask_low = le32_to_cpu(gpio->ulGpioMaskVal);
3801 					voltage_table->count = gpio->ucGpioEntryNum;
3802 					voltage_table->phase_delay = gpio->ucPhaseDelay;
3803 					return 0;
3804 				}
3805 				break;
3806 			default:
3807 				DRM_ERROR("unknown voltage object table\n");
3808 				return -EINVAL;
3809 			}
3810 			break;
3811 		default:
3812 			DRM_ERROR("unknown voltage object table\n");
3813 			return -EINVAL;
3814 		}
3815 	}
3816 	return -EINVAL;
3817 }
3818 
3819 union vram_info {
3820 	struct _ATOM_VRAM_INFO_V3 v1_3;
3821 	struct _ATOM_VRAM_INFO_V4 v1_4;
3822 	struct _ATOM_VRAM_INFO_HEADER_V2_1 v2_1;
3823 };
3824 
3825 int radeon_atom_get_memory_info(struct radeon_device *rdev,
3826 				u8 module_index, struct atom_memory_info *mem_info)
3827 {
3828 	int index = GetIndexIntoMasterTable(DATA, VRAM_Info);
3829 	u8 frev, crev, i;
3830 	u16 data_offset, size;
3831 	union vram_info *vram_info;
3832 
3833 	memset(mem_info, 0, sizeof(struct atom_memory_info));
3834 
3835 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
3836 				   &frev, &crev, &data_offset)) {
3837 		vram_info = (union vram_info *)
3838 			(rdev->mode_info.atom_context->bios + data_offset);
3839 		switch (frev) {
3840 		case 1:
3841 			switch (crev) {
3842 			case 3:
3843 				/* r6xx */
3844 				if (module_index < vram_info->v1_3.ucNumOfVRAMModule) {
3845 					ATOM_VRAM_MODULE_V3 *vram_module =
3846 						(ATOM_VRAM_MODULE_V3 *)vram_info->v1_3.aVramInfo;
3847 
3848 					for (i = 0; i < module_index; i++) {
3849 						if (le16_to_cpu(vram_module->usSize) == 0)
3850 							return -EINVAL;
3851 						vram_module = (ATOM_VRAM_MODULE_V3 *)
3852 							((u8 *)vram_module + le16_to_cpu(vram_module->usSize));
3853 					}
3854 					mem_info->mem_vendor = vram_module->asMemory.ucMemoryVenderID & 0xf;
3855 					mem_info->mem_type = vram_module->asMemory.ucMemoryType & 0xf0;
3856 				} else
3857 					return -EINVAL;
3858 				break;
3859 			case 4:
3860 				/* r7xx, evergreen */
3861 				if (module_index < vram_info->v1_4.ucNumOfVRAMModule) {
3862 					ATOM_VRAM_MODULE_V4 *vram_module =
3863 						(ATOM_VRAM_MODULE_V4 *)vram_info->v1_4.aVramInfo;
3864 
3865 					for (i = 0; i < module_index; i++) {
3866 						if (le16_to_cpu(vram_module->usModuleSize) == 0)
3867 							return -EINVAL;
3868 						vram_module = (ATOM_VRAM_MODULE_V4 *)
3869 							((u8 *)vram_module + le16_to_cpu(vram_module->usModuleSize));
3870 					}
3871 					mem_info->mem_vendor = vram_module->ucMemoryVenderID & 0xf;
3872 					mem_info->mem_type = vram_module->ucMemoryType & 0xf0;
3873 				} else
3874 					return -EINVAL;
3875 				break;
3876 			default:
3877 				DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3878 				return -EINVAL;
3879 			}
3880 			break;
3881 		case 2:
3882 			switch (crev) {
3883 			case 1:
3884 				/* ni */
3885 				if (module_index < vram_info->v2_1.ucNumOfVRAMModule) {
3886 					ATOM_VRAM_MODULE_V7 *vram_module =
3887 						(ATOM_VRAM_MODULE_V7 *)vram_info->v2_1.aVramInfo;
3888 
3889 					for (i = 0; i < module_index; i++) {
3890 						if (le16_to_cpu(vram_module->usModuleSize) == 0)
3891 							return -EINVAL;
3892 						vram_module = (ATOM_VRAM_MODULE_V7 *)
3893 							((u8 *)vram_module + le16_to_cpu(vram_module->usModuleSize));
3894 					}
3895 					mem_info->mem_vendor = vram_module->ucMemoryVenderID & 0xf;
3896 					mem_info->mem_type = vram_module->ucMemoryType & 0xf0;
3897 				} else
3898 					return -EINVAL;
3899 				break;
3900 			default:
3901 				DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3902 				return -EINVAL;
3903 			}
3904 			break;
3905 		default:
3906 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3907 			return -EINVAL;
3908 		}
3909 		return 0;
3910 	}
3911 	return -EINVAL;
3912 }
3913 
3914 int radeon_atom_get_mclk_range_table(struct radeon_device *rdev,
3915 				     bool gddr5, u8 module_index,
3916 				     struct atom_memory_clock_range_table *mclk_range_table)
3917 {
3918 	int index = GetIndexIntoMasterTable(DATA, VRAM_Info);
3919 	u8 frev, crev, i;
3920 	u16 data_offset, size;
3921 	union vram_info *vram_info;
3922 	u32 mem_timing_size = gddr5 ?
3923 		sizeof(ATOM_MEMORY_TIMING_FORMAT_V2) : sizeof(ATOM_MEMORY_TIMING_FORMAT);
3924 
3925 	memset(mclk_range_table, 0, sizeof(struct atom_memory_clock_range_table));
3926 
3927 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
3928 				   &frev, &crev, &data_offset)) {
3929 		vram_info = (union vram_info *)
3930 			(rdev->mode_info.atom_context->bios + data_offset);
3931 		switch (frev) {
3932 		case 1:
3933 			switch (crev) {
3934 			case 3:
3935 				DRM_ERROR("old table version %d, %d\n", frev, crev);
3936 				return -EINVAL;
3937 			case 4:
3938 				/* r7xx, evergreen */
3939 				if (module_index < vram_info->v1_4.ucNumOfVRAMModule) {
3940 					ATOM_VRAM_MODULE_V4 *vram_module =
3941 						(ATOM_VRAM_MODULE_V4 *)vram_info->v1_4.aVramInfo;
3942 					ATOM_MEMORY_TIMING_FORMAT *format;
3943 
3944 					for (i = 0; i < module_index; i++) {
3945 						if (le16_to_cpu(vram_module->usModuleSize) == 0)
3946 							return -EINVAL;
3947 						vram_module = (ATOM_VRAM_MODULE_V4 *)
3948 							((u8 *)vram_module + le16_to_cpu(vram_module->usModuleSize));
3949 					}
3950 					mclk_range_table->num_entries = (u8)
3951 						((le16_to_cpu(vram_module->usModuleSize) - offsetof(ATOM_VRAM_MODULE_V4, asMemTiming)) /
3952 						 mem_timing_size);
3953 					format = &vram_module->asMemTiming[0];
3954 					for (i = 0; i < mclk_range_table->num_entries; i++) {
3955 						mclk_range_table->mclk[i] = le32_to_cpu(format->ulClkRange);
3956 						format = (ATOM_MEMORY_TIMING_FORMAT *)
3957 							((u8 *)format + mem_timing_size);
3958 					}
3959 				} else
3960 					return -EINVAL;
3961 				break;
3962 			default:
3963 				DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3964 				return -EINVAL;
3965 			}
3966 			break;
3967 		case 2:
3968 			DRM_ERROR("new table version %d, %d\n", frev, crev);
3969 			return -EINVAL;
3970 		default:
3971 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
3972 			return -EINVAL;
3973 		}
3974 		return 0;
3975 	}
3976 	return -EINVAL;
3977 }
3978 
3979 #define MEM_ID_MASK           0xff000000
3980 #define MEM_ID_SHIFT          24
3981 #define CLOCK_RANGE_MASK      0x00ffffff
3982 #define CLOCK_RANGE_SHIFT     0
3983 #define LOW_NIBBLE_MASK       0xf
3984 #define DATA_EQU_PREV         0
3985 #define DATA_FROM_TABLE       4
3986 
3987 int radeon_atom_init_mc_reg_table(struct radeon_device *rdev,
3988 				  u8 module_index,
3989 				  struct atom_mc_reg_table *reg_table)
3990 {
3991 	int index = GetIndexIntoMasterTable(DATA, VRAM_Info);
3992 	u8 frev, crev, num_entries, t_mem_id, num_ranges = 0;
3993 	u32 i = 0, j;
3994 	u16 data_offset, size;
3995 	union vram_info *vram_info;
3996 
3997 	memset(reg_table, 0, sizeof(struct atom_mc_reg_table));
3998 
3999 	if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
4000 				   &frev, &crev, &data_offset)) {
4001 		vram_info = (union vram_info *)
4002 			(rdev->mode_info.atom_context->bios + data_offset);
4003 		switch (frev) {
4004 		case 1:
4005 			DRM_ERROR("old table version %d, %d\n", frev, crev);
4006 			return -EINVAL;
4007 		case 2:
4008 			switch (crev) {
4009 			case 1:
4010 				if (module_index < vram_info->v2_1.ucNumOfVRAMModule) {
4011 					ATOM_INIT_REG_BLOCK *reg_block =
4012 						(ATOM_INIT_REG_BLOCK *)
4013 						((u8 *)vram_info + le16_to_cpu(vram_info->v2_1.usMemClkPatchTblOffset));
4014 					ATOM_MEMORY_SETTING_DATA_BLOCK *reg_data =
4015 						(ATOM_MEMORY_SETTING_DATA_BLOCK *)
4016 						((u8 *)reg_block + (2 * sizeof(u16)) +
4017 						 le16_to_cpu(reg_block->usRegIndexTblSize));
4018 					ATOM_INIT_REG_INDEX_FORMAT *format = &reg_block->asRegIndexBuf[0];
4019 					num_entries = (u8)((le16_to_cpu(reg_block->usRegIndexTblSize)) /
4020 							   sizeof(ATOM_INIT_REG_INDEX_FORMAT)) - 1;
4021 					if (num_entries > VBIOS_MC_REGISTER_ARRAY_SIZE)
4022 						return -EINVAL;
4023 					while (i < num_entries) {
4024 						if (format->ucPreRegDataLength & ACCESS_PLACEHOLDER)
4025 							break;
4026 						reg_table->mc_reg_address[i].s1 =
4027 							(u16)(le16_to_cpu(format->usRegIndex));
4028 						reg_table->mc_reg_address[i].pre_reg_data =
4029 							(u8)(format->ucPreRegDataLength);
4030 						i++;
4031 						format = (ATOM_INIT_REG_INDEX_FORMAT *)
4032 							((u8 *)format + sizeof(ATOM_INIT_REG_INDEX_FORMAT));
4033 					}
4034 					reg_table->last = i;
4035 					while ((le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK) &&
4036 					       (num_ranges < VBIOS_MAX_AC_TIMING_ENTRIES)) {
4037 						t_mem_id = (u8)((le32_to_cpu(*(u32 *)reg_data) & MEM_ID_MASK)
4038 								>> MEM_ID_SHIFT);
4039 						if (module_index == t_mem_id) {
4040 							reg_table->mc_reg_table_entry[num_ranges].mclk_max =
4041 								(u32)((le32_to_cpu(*(u32 *)reg_data) & CLOCK_RANGE_MASK)
4042 								      >> CLOCK_RANGE_SHIFT);
4043 							for (i = 0, j = 1; i < reg_table->last; i++) {
4044 								if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_FROM_TABLE) {
4045 									reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
4046 										(u32)le32_to_cpu(*((u32 *)reg_data + j));
4047 									j++;
4048 								} else if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_EQU_PREV) {
4049 									reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
4050 										reg_table->mc_reg_table_entry[num_ranges].mc_data[i - 1];
4051 								}
4052 							}
4053 							num_ranges++;
4054 						}
4055 						reg_data = (ATOM_MEMORY_SETTING_DATA_BLOCK *)
4056 							((u8 *)reg_data + le16_to_cpu(reg_block->usRegDataBlkSize));
4057 					}
4058 					if (le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK)
4059 						return -EINVAL;
4060 					reg_table->num_entries = num_ranges;
4061 				} else
4062 					return -EINVAL;
4063 				break;
4064 			default:
4065 				DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
4066 				return -EINVAL;
4067 			}
4068 			break;
4069 		default:
4070 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
4071 			return -EINVAL;
4072 		}
4073 		return 0;
4074 	}
4075 	return -EINVAL;
4076 }
4077 
4078 void radeon_atom_initialize_bios_scratch_regs(struct drm_device *dev)
4079 {
4080 	struct radeon_device *rdev = dev->dev_private;
4081 	uint32_t bios_2_scratch, bios_6_scratch;
4082 
4083 	if (rdev->family >= CHIP_R600) {
4084 		bios_2_scratch = RREG32(R600_BIOS_2_SCRATCH);
4085 		bios_6_scratch = RREG32(R600_BIOS_6_SCRATCH);
4086 	} else {
4087 		bios_2_scratch = RREG32(RADEON_BIOS_2_SCRATCH);
4088 		bios_6_scratch = RREG32(RADEON_BIOS_6_SCRATCH);
4089 	}
4090 
4091 	/* let the bios control the backlight */
4092 	bios_2_scratch &= ~ATOM_S2_VRI_BRIGHT_ENABLE;
4093 
4094 	/* tell the bios not to handle mode switching */
4095 	bios_6_scratch |= ATOM_S6_ACC_BLOCK_DISPLAY_SWITCH;
4096 
4097 	/* clear the vbios dpms state */
4098 	if (ASIC_IS_DCE4(rdev))
4099 		bios_2_scratch &= ~ATOM_S2_DEVICE_DPMS_STATE;
4100 
4101 	if (rdev->family >= CHIP_R600) {
4102 		WREG32(R600_BIOS_2_SCRATCH, bios_2_scratch);
4103 		WREG32(R600_BIOS_6_SCRATCH, bios_6_scratch);
4104 	} else {
4105 		WREG32(RADEON_BIOS_2_SCRATCH, bios_2_scratch);
4106 		WREG32(RADEON_BIOS_6_SCRATCH, bios_6_scratch);
4107 	}
4108 
4109 }
4110 
4111 void radeon_save_bios_scratch_regs(struct radeon_device *rdev)
4112 {
4113 	uint32_t scratch_reg;
4114 	int i;
4115 
4116 	if (rdev->family >= CHIP_R600)
4117 		scratch_reg = R600_BIOS_0_SCRATCH;
4118 	else
4119 		scratch_reg = RADEON_BIOS_0_SCRATCH;
4120 
4121 	for (i = 0; i < RADEON_BIOS_NUM_SCRATCH; i++)
4122 		rdev->bios_scratch[i] = RREG32(scratch_reg + (i * 4));
4123 }
4124 
4125 void radeon_restore_bios_scratch_regs(struct radeon_device *rdev)
4126 {
4127 	uint32_t scratch_reg;
4128 	int i;
4129 
4130 	if (rdev->family >= CHIP_R600)
4131 		scratch_reg = R600_BIOS_0_SCRATCH;
4132 	else
4133 		scratch_reg = RADEON_BIOS_0_SCRATCH;
4134 
4135 	for (i = 0; i < RADEON_BIOS_NUM_SCRATCH; i++)
4136 		WREG32(scratch_reg + (i * 4), rdev->bios_scratch[i]);
4137 }
4138 
4139 void radeon_atom_output_lock(struct drm_encoder *encoder, bool lock)
4140 {
4141 	struct drm_device *dev = encoder->dev;
4142 	struct radeon_device *rdev = dev->dev_private;
4143 	uint32_t bios_6_scratch;
4144 
4145 	if (rdev->family >= CHIP_R600)
4146 		bios_6_scratch = RREG32(R600_BIOS_6_SCRATCH);
4147 	else
4148 		bios_6_scratch = RREG32(RADEON_BIOS_6_SCRATCH);
4149 
4150 	if (lock) {
4151 		bios_6_scratch |= ATOM_S6_CRITICAL_STATE;
4152 		bios_6_scratch &= ~ATOM_S6_ACC_MODE;
4153 	} else {
4154 		bios_6_scratch &= ~ATOM_S6_CRITICAL_STATE;
4155 		bios_6_scratch |= ATOM_S6_ACC_MODE;
4156 	}
4157 
4158 	if (rdev->family >= CHIP_R600)
4159 		WREG32(R600_BIOS_6_SCRATCH, bios_6_scratch);
4160 	else
4161 		WREG32(RADEON_BIOS_6_SCRATCH, bios_6_scratch);
4162 }
4163 
4164 /* at some point we may want to break this out into individual functions */
4165 void
4166 radeon_atombios_connected_scratch_regs(struct drm_connector *connector,
4167 				       struct drm_encoder *encoder,
4168 				       bool connected)
4169 {
4170 	struct drm_device *dev = connector->dev;
4171 	struct radeon_device *rdev = dev->dev_private;
4172 	struct radeon_connector *radeon_connector =
4173 	    to_radeon_connector(connector);
4174 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
4175 	uint32_t bios_0_scratch, bios_3_scratch, bios_6_scratch;
4176 
4177 	if (rdev->family >= CHIP_R600) {
4178 		bios_0_scratch = RREG32(R600_BIOS_0_SCRATCH);
4179 		bios_3_scratch = RREG32(R600_BIOS_3_SCRATCH);
4180 		bios_6_scratch = RREG32(R600_BIOS_6_SCRATCH);
4181 	} else {
4182 		bios_0_scratch = RREG32(RADEON_BIOS_0_SCRATCH);
4183 		bios_3_scratch = RREG32(RADEON_BIOS_3_SCRATCH);
4184 		bios_6_scratch = RREG32(RADEON_BIOS_6_SCRATCH);
4185 	}
4186 
4187 	if ((radeon_encoder->devices & ATOM_DEVICE_TV1_SUPPORT) &&
4188 	    (radeon_connector->devices & ATOM_DEVICE_TV1_SUPPORT)) {
4189 		if (connected) {
4190 			DRM_DEBUG_KMS("TV1 connected\n");
4191 			bios_3_scratch |= ATOM_S3_TV1_ACTIVE;
4192 			bios_6_scratch |= ATOM_S6_ACC_REQ_TV1;
4193 		} else {
4194 			DRM_DEBUG_KMS("TV1 disconnected\n");
4195 			bios_0_scratch &= ~ATOM_S0_TV1_MASK;
4196 			bios_3_scratch &= ~ATOM_S3_TV1_ACTIVE;
4197 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_TV1;
4198 		}
4199 	}
4200 	if ((radeon_encoder->devices & ATOM_DEVICE_CV_SUPPORT) &&
4201 	    (radeon_connector->devices & ATOM_DEVICE_CV_SUPPORT)) {
4202 		if (connected) {
4203 			DRM_DEBUG_KMS("CV connected\n");
4204 			bios_3_scratch |= ATOM_S3_CV_ACTIVE;
4205 			bios_6_scratch |= ATOM_S6_ACC_REQ_CV;
4206 		} else {
4207 			DRM_DEBUG_KMS("CV disconnected\n");
4208 			bios_0_scratch &= ~ATOM_S0_CV_MASK;
4209 			bios_3_scratch &= ~ATOM_S3_CV_ACTIVE;
4210 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_CV;
4211 		}
4212 	}
4213 	if ((radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) &&
4214 	    (radeon_connector->devices & ATOM_DEVICE_LCD1_SUPPORT)) {
4215 		if (connected) {
4216 			DRM_DEBUG_KMS("LCD1 connected\n");
4217 			bios_0_scratch |= ATOM_S0_LCD1;
4218 			bios_3_scratch |= ATOM_S3_LCD1_ACTIVE;
4219 			bios_6_scratch |= ATOM_S6_ACC_REQ_LCD1;
4220 		} else {
4221 			DRM_DEBUG_KMS("LCD1 disconnected\n");
4222 			bios_0_scratch &= ~ATOM_S0_LCD1;
4223 			bios_3_scratch &= ~ATOM_S3_LCD1_ACTIVE;
4224 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_LCD1;
4225 		}
4226 	}
4227 	if ((radeon_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) &&
4228 	    (radeon_connector->devices & ATOM_DEVICE_CRT1_SUPPORT)) {
4229 		if (connected) {
4230 			DRM_DEBUG_KMS("CRT1 connected\n");
4231 			bios_0_scratch |= ATOM_S0_CRT1_COLOR;
4232 			bios_3_scratch |= ATOM_S3_CRT1_ACTIVE;
4233 			bios_6_scratch |= ATOM_S6_ACC_REQ_CRT1;
4234 		} else {
4235 			DRM_DEBUG_KMS("CRT1 disconnected\n");
4236 			bios_0_scratch &= ~ATOM_S0_CRT1_MASK;
4237 			bios_3_scratch &= ~ATOM_S3_CRT1_ACTIVE;
4238 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_CRT1;
4239 		}
4240 	}
4241 	if ((radeon_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) &&
4242 	    (radeon_connector->devices & ATOM_DEVICE_CRT2_SUPPORT)) {
4243 		if (connected) {
4244 			DRM_DEBUG_KMS("CRT2 connected\n");
4245 			bios_0_scratch |= ATOM_S0_CRT2_COLOR;
4246 			bios_3_scratch |= ATOM_S3_CRT2_ACTIVE;
4247 			bios_6_scratch |= ATOM_S6_ACC_REQ_CRT2;
4248 		} else {
4249 			DRM_DEBUG_KMS("CRT2 disconnected\n");
4250 			bios_0_scratch &= ~ATOM_S0_CRT2_MASK;
4251 			bios_3_scratch &= ~ATOM_S3_CRT2_ACTIVE;
4252 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_CRT2;
4253 		}
4254 	}
4255 	if ((radeon_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) &&
4256 	    (radeon_connector->devices & ATOM_DEVICE_DFP1_SUPPORT)) {
4257 		if (connected) {
4258 			DRM_DEBUG_KMS("DFP1 connected\n");
4259 			bios_0_scratch |= ATOM_S0_DFP1;
4260 			bios_3_scratch |= ATOM_S3_DFP1_ACTIVE;
4261 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP1;
4262 		} else {
4263 			DRM_DEBUG_KMS("DFP1 disconnected\n");
4264 			bios_0_scratch &= ~ATOM_S0_DFP1;
4265 			bios_3_scratch &= ~ATOM_S3_DFP1_ACTIVE;
4266 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP1;
4267 		}
4268 	}
4269 	if ((radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) &&
4270 	    (radeon_connector->devices & ATOM_DEVICE_DFP2_SUPPORT)) {
4271 		if (connected) {
4272 			DRM_DEBUG_KMS("DFP2 connected\n");
4273 			bios_0_scratch |= ATOM_S0_DFP2;
4274 			bios_3_scratch |= ATOM_S3_DFP2_ACTIVE;
4275 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP2;
4276 		} else {
4277 			DRM_DEBUG_KMS("DFP2 disconnected\n");
4278 			bios_0_scratch &= ~ATOM_S0_DFP2;
4279 			bios_3_scratch &= ~ATOM_S3_DFP2_ACTIVE;
4280 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP2;
4281 		}
4282 	}
4283 	if ((radeon_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) &&
4284 	    (radeon_connector->devices & ATOM_DEVICE_DFP3_SUPPORT)) {
4285 		if (connected) {
4286 			DRM_DEBUG_KMS("DFP3 connected\n");
4287 			bios_0_scratch |= ATOM_S0_DFP3;
4288 			bios_3_scratch |= ATOM_S3_DFP3_ACTIVE;
4289 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP3;
4290 		} else {
4291 			DRM_DEBUG_KMS("DFP3 disconnected\n");
4292 			bios_0_scratch &= ~ATOM_S0_DFP3;
4293 			bios_3_scratch &= ~ATOM_S3_DFP3_ACTIVE;
4294 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP3;
4295 		}
4296 	}
4297 	if ((radeon_encoder->devices & ATOM_DEVICE_DFP4_SUPPORT) &&
4298 	    (radeon_connector->devices & ATOM_DEVICE_DFP4_SUPPORT)) {
4299 		if (connected) {
4300 			DRM_DEBUG_KMS("DFP4 connected\n");
4301 			bios_0_scratch |= ATOM_S0_DFP4;
4302 			bios_3_scratch |= ATOM_S3_DFP4_ACTIVE;
4303 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP4;
4304 		} else {
4305 			DRM_DEBUG_KMS("DFP4 disconnected\n");
4306 			bios_0_scratch &= ~ATOM_S0_DFP4;
4307 			bios_3_scratch &= ~ATOM_S3_DFP4_ACTIVE;
4308 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP4;
4309 		}
4310 	}
4311 	if ((radeon_encoder->devices & ATOM_DEVICE_DFP5_SUPPORT) &&
4312 	    (radeon_connector->devices & ATOM_DEVICE_DFP5_SUPPORT)) {
4313 		if (connected) {
4314 			DRM_DEBUG_KMS("DFP5 connected\n");
4315 			bios_0_scratch |= ATOM_S0_DFP5;
4316 			bios_3_scratch |= ATOM_S3_DFP5_ACTIVE;
4317 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP5;
4318 		} else {
4319 			DRM_DEBUG_KMS("DFP5 disconnected\n");
4320 			bios_0_scratch &= ~ATOM_S0_DFP5;
4321 			bios_3_scratch &= ~ATOM_S3_DFP5_ACTIVE;
4322 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP5;
4323 		}
4324 	}
4325 	if ((radeon_encoder->devices & ATOM_DEVICE_DFP6_SUPPORT) &&
4326 	    (radeon_connector->devices & ATOM_DEVICE_DFP6_SUPPORT)) {
4327 		if (connected) {
4328 			DRM_DEBUG_KMS("DFP6 connected\n");
4329 			bios_0_scratch |= ATOM_S0_DFP6;
4330 			bios_3_scratch |= ATOM_S3_DFP6_ACTIVE;
4331 			bios_6_scratch |= ATOM_S6_ACC_REQ_DFP6;
4332 		} else {
4333 			DRM_DEBUG_KMS("DFP6 disconnected\n");
4334 			bios_0_scratch &= ~ATOM_S0_DFP6;
4335 			bios_3_scratch &= ~ATOM_S3_DFP6_ACTIVE;
4336 			bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP6;
4337 		}
4338 	}
4339 
4340 	if (rdev->family >= CHIP_R600) {
4341 		WREG32(R600_BIOS_0_SCRATCH, bios_0_scratch);
4342 		WREG32(R600_BIOS_3_SCRATCH, bios_3_scratch);
4343 		WREG32(R600_BIOS_6_SCRATCH, bios_6_scratch);
4344 	} else {
4345 		WREG32(RADEON_BIOS_0_SCRATCH, bios_0_scratch);
4346 		WREG32(RADEON_BIOS_3_SCRATCH, bios_3_scratch);
4347 		WREG32(RADEON_BIOS_6_SCRATCH, bios_6_scratch);
4348 	}
4349 }
4350 
4351 void
4352 radeon_atombios_encoder_crtc_scratch_regs(struct drm_encoder *encoder, int crtc)
4353 {
4354 	struct drm_device *dev = encoder->dev;
4355 	struct radeon_device *rdev = dev->dev_private;
4356 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
4357 	uint32_t bios_3_scratch;
4358 
4359 	if (ASIC_IS_DCE4(rdev))
4360 		return;
4361 
4362 	if (rdev->family >= CHIP_R600)
4363 		bios_3_scratch = RREG32(R600_BIOS_3_SCRATCH);
4364 	else
4365 		bios_3_scratch = RREG32(RADEON_BIOS_3_SCRATCH);
4366 
4367 	if (radeon_encoder->devices & ATOM_DEVICE_TV1_SUPPORT) {
4368 		bios_3_scratch &= ~ATOM_S3_TV1_CRTC_ACTIVE;
4369 		bios_3_scratch |= (crtc << 18);
4370 	}
4371 	if (radeon_encoder->devices & ATOM_DEVICE_CV_SUPPORT) {
4372 		bios_3_scratch &= ~ATOM_S3_CV_CRTC_ACTIVE;
4373 		bios_3_scratch |= (crtc << 24);
4374 	}
4375 	if (radeon_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) {
4376 		bios_3_scratch &= ~ATOM_S3_CRT1_CRTC_ACTIVE;
4377 		bios_3_scratch |= (crtc << 16);
4378 	}
4379 	if (radeon_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) {
4380 		bios_3_scratch &= ~ATOM_S3_CRT2_CRTC_ACTIVE;
4381 		bios_3_scratch |= (crtc << 20);
4382 	}
4383 	if (radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) {
4384 		bios_3_scratch &= ~ATOM_S3_LCD1_CRTC_ACTIVE;
4385 		bios_3_scratch |= (crtc << 17);
4386 	}
4387 	if (radeon_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) {
4388 		bios_3_scratch &= ~ATOM_S3_DFP1_CRTC_ACTIVE;
4389 		bios_3_scratch |= (crtc << 19);
4390 	}
4391 	if (radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) {
4392 		bios_3_scratch &= ~ATOM_S3_DFP2_CRTC_ACTIVE;
4393 		bios_3_scratch |= (crtc << 23);
4394 	}
4395 	if (radeon_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) {
4396 		bios_3_scratch &= ~ATOM_S3_DFP3_CRTC_ACTIVE;
4397 		bios_3_scratch |= (crtc << 25);
4398 	}
4399 
4400 	if (rdev->family >= CHIP_R600)
4401 		WREG32(R600_BIOS_3_SCRATCH, bios_3_scratch);
4402 	else
4403 		WREG32(RADEON_BIOS_3_SCRATCH, bios_3_scratch);
4404 }
4405 
4406 void
4407 radeon_atombios_encoder_dpms_scratch_regs(struct drm_encoder *encoder, bool on)
4408 {
4409 	struct drm_device *dev = encoder->dev;
4410 	struct radeon_device *rdev = dev->dev_private;
4411 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
4412 	uint32_t bios_2_scratch;
4413 
4414 	if (ASIC_IS_DCE4(rdev))
4415 		return;
4416 
4417 	if (rdev->family >= CHIP_R600)
4418 		bios_2_scratch = RREG32(R600_BIOS_2_SCRATCH);
4419 	else
4420 		bios_2_scratch = RREG32(RADEON_BIOS_2_SCRATCH);
4421 
4422 	if (radeon_encoder->devices & ATOM_DEVICE_TV1_SUPPORT) {
4423 		if (on)
4424 			bios_2_scratch &= ~ATOM_S2_TV1_DPMS_STATE;
4425 		else
4426 			bios_2_scratch |= ATOM_S2_TV1_DPMS_STATE;
4427 	}
4428 	if (radeon_encoder->devices & ATOM_DEVICE_CV_SUPPORT) {
4429 		if (on)
4430 			bios_2_scratch &= ~ATOM_S2_CV_DPMS_STATE;
4431 		else
4432 			bios_2_scratch |= ATOM_S2_CV_DPMS_STATE;
4433 	}
4434 	if (radeon_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) {
4435 		if (on)
4436 			bios_2_scratch &= ~ATOM_S2_CRT1_DPMS_STATE;
4437 		else
4438 			bios_2_scratch |= ATOM_S2_CRT1_DPMS_STATE;
4439 	}
4440 	if (radeon_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) {
4441 		if (on)
4442 			bios_2_scratch &= ~ATOM_S2_CRT2_DPMS_STATE;
4443 		else
4444 			bios_2_scratch |= ATOM_S2_CRT2_DPMS_STATE;
4445 	}
4446 	if (radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) {
4447 		if (on)
4448 			bios_2_scratch &= ~ATOM_S2_LCD1_DPMS_STATE;
4449 		else
4450 			bios_2_scratch |= ATOM_S2_LCD1_DPMS_STATE;
4451 	}
4452 	if (radeon_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) {
4453 		if (on)
4454 			bios_2_scratch &= ~ATOM_S2_DFP1_DPMS_STATE;
4455 		else
4456 			bios_2_scratch |= ATOM_S2_DFP1_DPMS_STATE;
4457 	}
4458 	if (radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) {
4459 		if (on)
4460 			bios_2_scratch &= ~ATOM_S2_DFP2_DPMS_STATE;
4461 		else
4462 			bios_2_scratch |= ATOM_S2_DFP2_DPMS_STATE;
4463 	}
4464 	if (radeon_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) {
4465 		if (on)
4466 			bios_2_scratch &= ~ATOM_S2_DFP3_DPMS_STATE;
4467 		else
4468 			bios_2_scratch |= ATOM_S2_DFP3_DPMS_STATE;
4469 	}
4470 	if (radeon_encoder->devices & ATOM_DEVICE_DFP4_SUPPORT) {
4471 		if (on)
4472 			bios_2_scratch &= ~ATOM_S2_DFP4_DPMS_STATE;
4473 		else
4474 			bios_2_scratch |= ATOM_S2_DFP4_DPMS_STATE;
4475 	}
4476 	if (radeon_encoder->devices & ATOM_DEVICE_DFP5_SUPPORT) {
4477 		if (on)
4478 			bios_2_scratch &= ~ATOM_S2_DFP5_DPMS_STATE;
4479 		else
4480 			bios_2_scratch |= ATOM_S2_DFP5_DPMS_STATE;
4481 	}
4482 
4483 	if (rdev->family >= CHIP_R600)
4484 		WREG32(R600_BIOS_2_SCRATCH, bios_2_scratch);
4485 	else
4486 		WREG32(RADEON_BIOS_2_SCRATCH, bios_2_scratch);
4487 }
4488