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