xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_atombios.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2007-8 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  * Copyright 2008 Red Hat Inc.
4b843c749SSergey Zigachev  *
5b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
6b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
7b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
8b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
10b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
11b843c749SSergey Zigachev  *
12b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
13b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
14b843c749SSergey Zigachev  *
15b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
22b843c749SSergey Zigachev  *
23b843c749SSergey Zigachev  * Authors: Dave Airlie
24b843c749SSergey Zigachev  *          Alex Deucher
25b843c749SSergey Zigachev  */
26b843c749SSergey Zigachev #include <drm/drmP.h>
27b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
28b843c749SSergey Zigachev #include "amdgpu.h"
29b843c749SSergey Zigachev #include "amdgpu_atombios.h"
30b843c749SSergey Zigachev #include "amdgpu_atomfirmware.h"
31b843c749SSergey Zigachev #include "amdgpu_i2c.h"
32b843c749SSergey Zigachev 
33b843c749SSergey Zigachev #include "atom.h"
34b843c749SSergey Zigachev #include "atom-bits.h"
35b843c749SSergey Zigachev #include "atombios_encoders.h"
36b843c749SSergey Zigachev #include "bif/bif_4_1_d.h"
37b843c749SSergey Zigachev 
amdgpu_atombios_lookup_i2c_gpio_quirks(struct amdgpu_device * adev,ATOM_GPIO_I2C_ASSIGMENT * gpio,u8 index)38b843c749SSergey Zigachev static void amdgpu_atombios_lookup_i2c_gpio_quirks(struct amdgpu_device *adev,
39b843c749SSergey Zigachev 					  ATOM_GPIO_I2C_ASSIGMENT *gpio,
40b843c749SSergey Zigachev 					  u8 index)
41b843c749SSergey Zigachev {
42b843c749SSergey Zigachev 
43b843c749SSergey Zigachev }
44b843c749SSergey Zigachev 
amdgpu_atombios_get_bus_rec_for_i2c_gpio(ATOM_GPIO_I2C_ASSIGMENT * gpio)45b843c749SSergey Zigachev static struct amdgpu_i2c_bus_rec amdgpu_atombios_get_bus_rec_for_i2c_gpio(ATOM_GPIO_I2C_ASSIGMENT *gpio)
46b843c749SSergey Zigachev {
47b843c749SSergey Zigachev 	struct amdgpu_i2c_bus_rec i2c;
48b843c749SSergey Zigachev 
49b843c749SSergey Zigachev 	memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
50b843c749SSergey Zigachev 
51b843c749SSergey Zigachev 	i2c.mask_clk_reg = le16_to_cpu(gpio->usClkMaskRegisterIndex);
52b843c749SSergey Zigachev 	i2c.mask_data_reg = le16_to_cpu(gpio->usDataMaskRegisterIndex);
53b843c749SSergey Zigachev 	i2c.en_clk_reg = le16_to_cpu(gpio->usClkEnRegisterIndex);
54b843c749SSergey Zigachev 	i2c.en_data_reg = le16_to_cpu(gpio->usDataEnRegisterIndex);
55b843c749SSergey Zigachev 	i2c.y_clk_reg = le16_to_cpu(gpio->usClkY_RegisterIndex);
56b843c749SSergey Zigachev 	i2c.y_data_reg = le16_to_cpu(gpio->usDataY_RegisterIndex);
57b843c749SSergey Zigachev 	i2c.a_clk_reg = le16_to_cpu(gpio->usClkA_RegisterIndex);
58b843c749SSergey Zigachev 	i2c.a_data_reg = le16_to_cpu(gpio->usDataA_RegisterIndex);
59b843c749SSergey Zigachev 	i2c.mask_clk_mask = (1 << gpio->ucClkMaskShift);
60b843c749SSergey Zigachev 	i2c.mask_data_mask = (1 << gpio->ucDataMaskShift);
61b843c749SSergey Zigachev 	i2c.en_clk_mask = (1 << gpio->ucClkEnShift);
62b843c749SSergey Zigachev 	i2c.en_data_mask = (1 << gpio->ucDataEnShift);
63b843c749SSergey Zigachev 	i2c.y_clk_mask = (1 << gpio->ucClkY_Shift);
64b843c749SSergey Zigachev 	i2c.y_data_mask = (1 << gpio->ucDataY_Shift);
65b843c749SSergey Zigachev 	i2c.a_clk_mask = (1 << gpio->ucClkA_Shift);
66b843c749SSergey Zigachev 	i2c.a_data_mask = (1 << gpio->ucDataA_Shift);
67b843c749SSergey Zigachev 
68b843c749SSergey Zigachev 	if (gpio->sucI2cId.sbfAccess.bfHW_Capable)
69b843c749SSergey Zigachev 		i2c.hw_capable = true;
70b843c749SSergey Zigachev 	else
71b843c749SSergey Zigachev 		i2c.hw_capable = false;
72b843c749SSergey Zigachev 
73b843c749SSergey Zigachev 	if (gpio->sucI2cId.ucAccess == 0xa0)
74b843c749SSergey Zigachev 		i2c.mm_i2c = true;
75b843c749SSergey Zigachev 	else
76b843c749SSergey Zigachev 		i2c.mm_i2c = false;
77b843c749SSergey Zigachev 
78b843c749SSergey Zigachev 	i2c.i2c_id = gpio->sucI2cId.ucAccess;
79b843c749SSergey Zigachev 
80b843c749SSergey Zigachev 	if (i2c.mask_clk_reg)
81b843c749SSergey Zigachev 		i2c.valid = true;
82b843c749SSergey Zigachev 	else
83b843c749SSergey Zigachev 		i2c.valid = false;
84b843c749SSergey Zigachev 
85b843c749SSergey Zigachev 	return i2c;
86b843c749SSergey Zigachev }
87b843c749SSergey Zigachev 
amdgpu_atombios_lookup_i2c_gpio(struct amdgpu_device * adev,uint8_t id)88b843c749SSergey Zigachev struct amdgpu_i2c_bus_rec amdgpu_atombios_lookup_i2c_gpio(struct amdgpu_device *adev,
89b843c749SSergey Zigachev 							  uint8_t id)
90b843c749SSergey Zigachev {
91b843c749SSergey Zigachev 	struct atom_context *ctx = adev->mode_info.atom_context;
92b843c749SSergey Zigachev 	ATOM_GPIO_I2C_ASSIGMENT *gpio;
93b843c749SSergey Zigachev 	struct amdgpu_i2c_bus_rec i2c;
94b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
95b843c749SSergey Zigachev 	struct _ATOM_GPIO_I2C_INFO *i2c_info;
96b843c749SSergey Zigachev 	uint16_t data_offset, size;
97b843c749SSergey Zigachev 	int i, num_indices;
98b843c749SSergey Zigachev 
99b843c749SSergey Zigachev 	memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
100b843c749SSergey Zigachev 	i2c.valid = false;
101b843c749SSergey Zigachev 
102b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
103b843c749SSergey Zigachev 		i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
104b843c749SSergey Zigachev 
105b843c749SSergey Zigachev 		num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
106b843c749SSergey Zigachev 			sizeof(ATOM_GPIO_I2C_ASSIGMENT);
107b843c749SSergey Zigachev 
108b843c749SSergey Zigachev 		gpio = &i2c_info->asGPIO_Info[0];
109b843c749SSergey Zigachev 		for (i = 0; i < num_indices; i++) {
110b843c749SSergey Zigachev 
111b843c749SSergey Zigachev 			amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
112b843c749SSergey Zigachev 
113b843c749SSergey Zigachev 			if (gpio->sucI2cId.ucAccess == id) {
114b843c749SSergey Zigachev 				i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
115b843c749SSergey Zigachev 				break;
116b843c749SSergey Zigachev 			}
117b843c749SSergey Zigachev 			gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
118b843c749SSergey Zigachev 				((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
119b843c749SSergey Zigachev 		}
120b843c749SSergey Zigachev 	}
121b843c749SSergey Zigachev 
122b843c749SSergey Zigachev 	return i2c;
123b843c749SSergey Zigachev }
124b843c749SSergey Zigachev 
amdgpu_atombios_i2c_init(struct amdgpu_device * adev)125b843c749SSergey Zigachev void amdgpu_atombios_i2c_init(struct amdgpu_device *adev)
126b843c749SSergey Zigachev {
127b843c749SSergey Zigachev 	struct atom_context *ctx = adev->mode_info.atom_context;
128b843c749SSergey Zigachev 	ATOM_GPIO_I2C_ASSIGMENT *gpio;
129b843c749SSergey Zigachev 	struct amdgpu_i2c_bus_rec i2c;
130b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
131b843c749SSergey Zigachev 	struct _ATOM_GPIO_I2C_INFO *i2c_info;
132b843c749SSergey Zigachev 	uint16_t data_offset, size;
133b843c749SSergey Zigachev 	int i, num_indices;
134b843c749SSergey Zigachev 	char stmp[32];
135b843c749SSergey Zigachev 
136b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
137b843c749SSergey Zigachev 		i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
138b843c749SSergey Zigachev 
139b843c749SSergey Zigachev 		num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
140b843c749SSergey Zigachev 			sizeof(ATOM_GPIO_I2C_ASSIGMENT);
141b843c749SSergey Zigachev 
142b843c749SSergey Zigachev 		gpio = &i2c_info->asGPIO_Info[0];
143b843c749SSergey Zigachev 		for (i = 0; i < num_indices; i++) {
144b843c749SSergey Zigachev 			amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
145b843c749SSergey Zigachev 
146b843c749SSergey Zigachev 			i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
147b843c749SSergey Zigachev 
148b843c749SSergey Zigachev 			if (i2c.valid) {
149*78973132SSergey Zigachev 				ksprintf(stmp, "0x%x", i2c.i2c_id);
150b843c749SSergey Zigachev 				adev->i2c_bus[i] = amdgpu_i2c_create(adev->ddev, &i2c, stmp);
151b843c749SSergey Zigachev 			}
152b843c749SSergey Zigachev 			gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
153b843c749SSergey Zigachev 				((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
154b843c749SSergey Zigachev 		}
155b843c749SSergey Zigachev 	}
156b843c749SSergey Zigachev }
157b843c749SSergey Zigachev 
158b843c749SSergey Zigachev struct amdgpu_gpio_rec
amdgpu_atombios_lookup_gpio(struct amdgpu_device * adev,u8 id)159b843c749SSergey Zigachev amdgpu_atombios_lookup_gpio(struct amdgpu_device *adev,
160b843c749SSergey Zigachev 			    u8 id)
161b843c749SSergey Zigachev {
162b843c749SSergey Zigachev 	struct atom_context *ctx = adev->mode_info.atom_context;
163b843c749SSergey Zigachev 	struct amdgpu_gpio_rec gpio;
164b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, GPIO_Pin_LUT);
165b843c749SSergey Zigachev 	struct _ATOM_GPIO_PIN_LUT *gpio_info;
166b843c749SSergey Zigachev 	ATOM_GPIO_PIN_ASSIGNMENT *pin;
167b843c749SSergey Zigachev 	u16 data_offset, size;
168b843c749SSergey Zigachev 	int i, num_indices;
169b843c749SSergey Zigachev 
170b843c749SSergey Zigachev 	memset(&gpio, 0, sizeof(struct amdgpu_gpio_rec));
171b843c749SSergey Zigachev 	gpio.valid = false;
172b843c749SSergey Zigachev 
173b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
174b843c749SSergey Zigachev 		gpio_info = (struct _ATOM_GPIO_PIN_LUT *)(ctx->bios + data_offset);
175b843c749SSergey Zigachev 
176b843c749SSergey Zigachev 		num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
177b843c749SSergey Zigachev 			sizeof(ATOM_GPIO_PIN_ASSIGNMENT);
178b843c749SSergey Zigachev 
179b843c749SSergey Zigachev 		pin = gpio_info->asGPIO_Pin;
180b843c749SSergey Zigachev 		for (i = 0; i < num_indices; i++) {
181b843c749SSergey Zigachev 			if (id == pin->ucGPIO_ID) {
182b843c749SSergey Zigachev 				gpio.id = pin->ucGPIO_ID;
183b843c749SSergey Zigachev 				gpio.reg = le16_to_cpu(pin->usGpioPin_AIndex);
184b843c749SSergey Zigachev 				gpio.shift = pin->ucGpioPinBitShift;
185b843c749SSergey Zigachev 				gpio.mask = (1 << pin->ucGpioPinBitShift);
186b843c749SSergey Zigachev 				gpio.valid = true;
187b843c749SSergey Zigachev 				break;
188b843c749SSergey Zigachev 			}
189b843c749SSergey Zigachev 			pin = (ATOM_GPIO_PIN_ASSIGNMENT *)
190b843c749SSergey Zigachev 				((u8 *)pin + sizeof(ATOM_GPIO_PIN_ASSIGNMENT));
191b843c749SSergey Zigachev 		}
192b843c749SSergey Zigachev 	}
193b843c749SSergey Zigachev 
194b843c749SSergey Zigachev 	return gpio;
195b843c749SSergey Zigachev }
196b843c749SSergey Zigachev 
197b843c749SSergey Zigachev static struct amdgpu_hpd
amdgpu_atombios_get_hpd_info_from_gpio(struct amdgpu_device * adev,struct amdgpu_gpio_rec * gpio)198b843c749SSergey Zigachev amdgpu_atombios_get_hpd_info_from_gpio(struct amdgpu_device *adev,
199b843c749SSergey Zigachev 				       struct amdgpu_gpio_rec *gpio)
200b843c749SSergey Zigachev {
201b843c749SSergey Zigachev 	struct amdgpu_hpd hpd;
202b843c749SSergey Zigachev 	u32 reg;
203b843c749SSergey Zigachev 
204b843c749SSergey Zigachev 	memset(&hpd, 0, sizeof(struct amdgpu_hpd));
205b843c749SSergey Zigachev 
206b843c749SSergey Zigachev 	reg = amdgpu_display_hpd_get_gpio_reg(adev);
207b843c749SSergey Zigachev 
208b843c749SSergey Zigachev 	hpd.gpio = *gpio;
209b843c749SSergey Zigachev 	if (gpio->reg == reg) {
210b843c749SSergey Zigachev 		switch(gpio->mask) {
211b843c749SSergey Zigachev 		case (1 << 0):
212b843c749SSergey Zigachev 			hpd.hpd = AMDGPU_HPD_1;
213b843c749SSergey Zigachev 			break;
214b843c749SSergey Zigachev 		case (1 << 8):
215b843c749SSergey Zigachev 			hpd.hpd = AMDGPU_HPD_2;
216b843c749SSergey Zigachev 			break;
217b843c749SSergey Zigachev 		case (1 << 16):
218b843c749SSergey Zigachev 			hpd.hpd = AMDGPU_HPD_3;
219b843c749SSergey Zigachev 			break;
220b843c749SSergey Zigachev 		case (1 << 24):
221b843c749SSergey Zigachev 			hpd.hpd = AMDGPU_HPD_4;
222b843c749SSergey Zigachev 			break;
223b843c749SSergey Zigachev 		case (1 << 26):
224b843c749SSergey Zigachev 			hpd.hpd = AMDGPU_HPD_5;
225b843c749SSergey Zigachev 			break;
226b843c749SSergey Zigachev 		case (1 << 28):
227b843c749SSergey Zigachev 			hpd.hpd = AMDGPU_HPD_6;
228b843c749SSergey Zigachev 			break;
229b843c749SSergey Zigachev 		default:
230b843c749SSergey Zigachev 			hpd.hpd = AMDGPU_HPD_NONE;
231b843c749SSergey Zigachev 			break;
232b843c749SSergey Zigachev 		}
233b843c749SSergey Zigachev 	} else
234b843c749SSergey Zigachev 		hpd.hpd = AMDGPU_HPD_NONE;
235b843c749SSergey Zigachev 	return hpd;
236b843c749SSergey Zigachev }
237b843c749SSergey Zigachev 
238b843c749SSergey Zigachev static const int object_connector_convert[] = {
239b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_Unknown,
240b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_DVII,
241b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_DVII,
242b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_DVID,
243b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_DVID,
244b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_VGA,
245b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_Composite,
246b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_SVIDEO,
247b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_Unknown,
248b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_Unknown,
249b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_9PinDIN,
250b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_Unknown,
251b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_HDMIA,
252b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_HDMIB,
253b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_LVDS,
254b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_9PinDIN,
255b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_Unknown,
256b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_Unknown,
257b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_Unknown,
258b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_DisplayPort,
259b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_eDP,
260b843c749SSergey Zigachev 	DRM_MODE_CONNECTOR_Unknown
261b843c749SSergey Zigachev };
262b843c749SSergey Zigachev 
amdgpu_atombios_has_dce_engine_info(struct amdgpu_device * adev)263b843c749SSergey Zigachev bool amdgpu_atombios_has_dce_engine_info(struct amdgpu_device *adev)
264b843c749SSergey Zigachev {
265b843c749SSergey Zigachev 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
266b843c749SSergey Zigachev 	struct atom_context *ctx = mode_info->atom_context;
267b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, Object_Header);
268b843c749SSergey Zigachev 	u16 size, data_offset;
269b843c749SSergey Zigachev 	u8 frev, crev;
270b843c749SSergey Zigachev 	ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
271b843c749SSergey Zigachev 	ATOM_OBJECT_HEADER *obj_header;
272b843c749SSergey Zigachev 
273b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
274b843c749SSergey Zigachev 		return false;
275b843c749SSergey Zigachev 
276b843c749SSergey Zigachev 	if (crev < 2)
277b843c749SSergey Zigachev 		return false;
278b843c749SSergey Zigachev 
279b843c749SSergey Zigachev 	obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
280b843c749SSergey Zigachev 	path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
281b843c749SSergey Zigachev 	    (ctx->bios + data_offset +
282b843c749SSergey Zigachev 	     le16_to_cpu(obj_header->usDisplayPathTableOffset));
283b843c749SSergey Zigachev 
284b843c749SSergey Zigachev 	if (path_obj->ucNumOfDispPath)
285b843c749SSergey Zigachev 		return true;
286b843c749SSergey Zigachev 	else
287b843c749SSergey Zigachev 		return false;
288b843c749SSergey Zigachev }
289b843c749SSergey Zigachev 
amdgpu_atombios_get_connector_info_from_object_table(struct amdgpu_device * adev)290b843c749SSergey Zigachev bool amdgpu_atombios_get_connector_info_from_object_table(struct amdgpu_device *adev)
291b843c749SSergey Zigachev {
292b843c749SSergey Zigachev 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
293b843c749SSergey Zigachev 	struct atom_context *ctx = mode_info->atom_context;
294b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, Object_Header);
295b843c749SSergey Zigachev 	u16 size, data_offset;
296b843c749SSergey Zigachev 	u8 frev, crev;
297b843c749SSergey Zigachev 	ATOM_CONNECTOR_OBJECT_TABLE *con_obj;
298b843c749SSergey Zigachev 	ATOM_ENCODER_OBJECT_TABLE *enc_obj;
299b843c749SSergey Zigachev 	ATOM_OBJECT_TABLE *router_obj;
300b843c749SSergey Zigachev 	ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
301b843c749SSergey Zigachev 	ATOM_OBJECT_HEADER *obj_header;
302b843c749SSergey Zigachev 	int i, j, k, path_size, device_support;
303b843c749SSergey Zigachev 	int connector_type;
304b843c749SSergey Zigachev 	u16 conn_id, connector_object_id;
305b843c749SSergey Zigachev 	struct amdgpu_i2c_bus_rec ddc_bus;
306b843c749SSergey Zigachev 	struct amdgpu_router router;
307b843c749SSergey Zigachev 	struct amdgpu_gpio_rec gpio;
308b843c749SSergey Zigachev 	struct amdgpu_hpd hpd;
309b843c749SSergey Zigachev 
310b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
311b843c749SSergey Zigachev 		return false;
312b843c749SSergey Zigachev 
313b843c749SSergey Zigachev 	if (crev < 2)
314b843c749SSergey Zigachev 		return false;
315b843c749SSergey Zigachev 
316b843c749SSergey Zigachev 	obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
317b843c749SSergey Zigachev 	path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
318b843c749SSergey Zigachev 	    (ctx->bios + data_offset +
319b843c749SSergey Zigachev 	     le16_to_cpu(obj_header->usDisplayPathTableOffset));
320b843c749SSergey Zigachev 	con_obj = (ATOM_CONNECTOR_OBJECT_TABLE *)
321b843c749SSergey Zigachev 	    (ctx->bios + data_offset +
322b843c749SSergey Zigachev 	     le16_to_cpu(obj_header->usConnectorObjectTableOffset));
323b843c749SSergey Zigachev 	enc_obj = (ATOM_ENCODER_OBJECT_TABLE *)
324b843c749SSergey Zigachev 	    (ctx->bios + data_offset +
325b843c749SSergey Zigachev 	     le16_to_cpu(obj_header->usEncoderObjectTableOffset));
326b843c749SSergey Zigachev 	router_obj = (ATOM_OBJECT_TABLE *)
327b843c749SSergey Zigachev 		(ctx->bios + data_offset +
328b843c749SSergey Zigachev 		 le16_to_cpu(obj_header->usRouterObjectTableOffset));
329b843c749SSergey Zigachev 	device_support = le16_to_cpu(obj_header->usDeviceSupport);
330b843c749SSergey Zigachev 
331b843c749SSergey Zigachev 	path_size = 0;
332b843c749SSergey Zigachev 	for (i = 0; i < path_obj->ucNumOfDispPath; i++) {
333b843c749SSergey Zigachev 		uint8_t *addr = (uint8_t *) path_obj->asDispPath;
334b843c749SSergey Zigachev 		ATOM_DISPLAY_OBJECT_PATH *path;
335b843c749SSergey Zigachev 		addr += path_size;
336b843c749SSergey Zigachev 		path = (ATOM_DISPLAY_OBJECT_PATH *) addr;
337b843c749SSergey Zigachev 		path_size += le16_to_cpu(path->usSize);
338b843c749SSergey Zigachev 
339b843c749SSergey Zigachev 		if (device_support & le16_to_cpu(path->usDeviceTag)) {
340b843c749SSergey Zigachev 			uint8_t con_obj_id =
341b843c749SSergey Zigachev 			    (le16_to_cpu(path->usConnObjectId) & OBJECT_ID_MASK)
342b843c749SSergey Zigachev 			    >> OBJECT_ID_SHIFT;
343b843c749SSergey Zigachev 
344b843c749SSergey Zigachev 			/* Skip TV/CV support */
345b843c749SSergey Zigachev 			if ((le16_to_cpu(path->usDeviceTag) ==
346b843c749SSergey Zigachev 			     ATOM_DEVICE_TV1_SUPPORT) ||
347b843c749SSergey Zigachev 			    (le16_to_cpu(path->usDeviceTag) ==
348b843c749SSergey Zigachev 			     ATOM_DEVICE_CV_SUPPORT))
349b843c749SSergey Zigachev 				continue;
350b843c749SSergey Zigachev 
351b843c749SSergey Zigachev 			if (con_obj_id >= ARRAY_SIZE(object_connector_convert)) {
352b843c749SSergey Zigachev 				DRM_ERROR("invalid con_obj_id %d for device tag 0x%04x\n",
353b843c749SSergey Zigachev 					  con_obj_id, le16_to_cpu(path->usDeviceTag));
354b843c749SSergey Zigachev 				continue;
355b843c749SSergey Zigachev 			}
356b843c749SSergey Zigachev 
357b843c749SSergey Zigachev 			connector_type =
358b843c749SSergey Zigachev 				object_connector_convert[con_obj_id];
359b843c749SSergey Zigachev 			connector_object_id = con_obj_id;
360b843c749SSergey Zigachev 
361b843c749SSergey Zigachev 			if (connector_type == DRM_MODE_CONNECTOR_Unknown)
362b843c749SSergey Zigachev 				continue;
363b843c749SSergey Zigachev 
364b843c749SSergey Zigachev 			router.ddc_valid = false;
365b843c749SSergey Zigachev 			router.cd_valid = false;
366b843c749SSergey Zigachev 			for (j = 0; j < ((le16_to_cpu(path->usSize) - 8) / 2); j++) {
367b843c749SSergey Zigachev 				uint8_t grph_obj_type =
368b843c749SSergey Zigachev 				    (le16_to_cpu(path->usGraphicObjIds[j]) &
369b843c749SSergey Zigachev 				     OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
370b843c749SSergey Zigachev 
371b843c749SSergey Zigachev 				if (grph_obj_type == GRAPH_OBJECT_TYPE_ENCODER) {
372b843c749SSergey Zigachev 					for (k = 0; k < enc_obj->ucNumberOfObjects; k++) {
373b843c749SSergey Zigachev 						u16 encoder_obj = le16_to_cpu(enc_obj->asObjects[k].usObjectID);
374b843c749SSergey Zigachev 						if (le16_to_cpu(path->usGraphicObjIds[j]) == encoder_obj) {
375b843c749SSergey Zigachev 							ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
376b843c749SSergey Zigachev 								(ctx->bios + data_offset +
377b843c749SSergey Zigachev 								 le16_to_cpu(enc_obj->asObjects[k].usRecordOffset));
378b843c749SSergey Zigachev 							ATOM_ENCODER_CAP_RECORD *cap_record;
379b843c749SSergey Zigachev 							u16 caps = 0;
380b843c749SSergey Zigachev 
381b843c749SSergey Zigachev 							while (record->ucRecordSize > 0 &&
382b843c749SSergey Zigachev 							       record->ucRecordType > 0 &&
383b843c749SSergey Zigachev 							       record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
384b843c749SSergey Zigachev 								switch (record->ucRecordType) {
385b843c749SSergey Zigachev 								case ATOM_ENCODER_CAP_RECORD_TYPE:
386b843c749SSergey Zigachev 									cap_record =(ATOM_ENCODER_CAP_RECORD *)
387b843c749SSergey Zigachev 										record;
388b843c749SSergey Zigachev 									caps = le16_to_cpu(cap_record->usEncoderCap);
389b843c749SSergey Zigachev 									break;
390b843c749SSergey Zigachev 								}
391b843c749SSergey Zigachev 								record = (ATOM_COMMON_RECORD_HEADER *)
392b843c749SSergey Zigachev 									((char *)record + record->ucRecordSize);
393b843c749SSergey Zigachev 							}
394b843c749SSergey Zigachev 							amdgpu_display_add_encoder(adev, encoder_obj,
395b843c749SSergey Zigachev 										    le16_to_cpu(path->usDeviceTag),
396b843c749SSergey Zigachev 										    caps);
397b843c749SSergey Zigachev 						}
398b843c749SSergey Zigachev 					}
399b843c749SSergey Zigachev 				} else if (grph_obj_type == GRAPH_OBJECT_TYPE_ROUTER) {
400b843c749SSergey Zigachev 					for (k = 0; k < router_obj->ucNumberOfObjects; k++) {
401b843c749SSergey Zigachev 						u16 router_obj_id = le16_to_cpu(router_obj->asObjects[k].usObjectID);
402b843c749SSergey Zigachev 						if (le16_to_cpu(path->usGraphicObjIds[j]) == router_obj_id) {
403b843c749SSergey Zigachev 							ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
404b843c749SSergey Zigachev 								(ctx->bios + data_offset +
405b843c749SSergey Zigachev 								 le16_to_cpu(router_obj->asObjects[k].usRecordOffset));
406b843c749SSergey Zigachev 							ATOM_I2C_RECORD *i2c_record;
407b843c749SSergey Zigachev 							ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
408b843c749SSergey Zigachev 							ATOM_ROUTER_DDC_PATH_SELECT_RECORD *ddc_path;
409b843c749SSergey Zigachev 							ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *cd_path;
410b843c749SSergey Zigachev 							ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *router_src_dst_table =
411b843c749SSergey Zigachev 								(ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *)
412b843c749SSergey Zigachev 								(ctx->bios + data_offset +
413b843c749SSergey Zigachev 								 le16_to_cpu(router_obj->asObjects[k].usSrcDstTableOffset));
414b843c749SSergey Zigachev 							u8 *num_dst_objs = (u8 *)
415b843c749SSergey Zigachev 								((u8 *)router_src_dst_table + 1 +
416b843c749SSergey Zigachev 								 (router_src_dst_table->ucNumberOfSrc * 2));
417b843c749SSergey Zigachev 							u16 *dst_objs = (u16 *)(num_dst_objs + 1);
418b843c749SSergey Zigachev 							int enum_id;
419b843c749SSergey Zigachev 
420b843c749SSergey Zigachev 							router.router_id = router_obj_id;
421b843c749SSergey Zigachev 							for (enum_id = 0; enum_id < (*num_dst_objs); enum_id++) {
422b843c749SSergey Zigachev 								if (le16_to_cpu(path->usConnObjectId) ==
423b843c749SSergey Zigachev 								    le16_to_cpu(dst_objs[enum_id]))
424b843c749SSergey Zigachev 									break;
425b843c749SSergey Zigachev 							}
426b843c749SSergey Zigachev 
427b843c749SSergey Zigachev 							while (record->ucRecordSize > 0 &&
428b843c749SSergey Zigachev 							       record->ucRecordType > 0 &&
429b843c749SSergey Zigachev 							       record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
430b843c749SSergey Zigachev 								switch (record->ucRecordType) {
431b843c749SSergey Zigachev 								case ATOM_I2C_RECORD_TYPE:
432b843c749SSergey Zigachev 									i2c_record =
433b843c749SSergey Zigachev 										(ATOM_I2C_RECORD *)
434b843c749SSergey Zigachev 										record;
435b843c749SSergey Zigachev 									i2c_config =
436b843c749SSergey Zigachev 										(ATOM_I2C_ID_CONFIG_ACCESS *)
437b843c749SSergey Zigachev 										&i2c_record->sucI2cId;
438b843c749SSergey Zigachev 									router.i2c_info =
439b843c749SSergey Zigachev 										amdgpu_atombios_lookup_i2c_gpio(adev,
440b843c749SSergey Zigachev 												       i2c_config->
441b843c749SSergey Zigachev 												       ucAccess);
442b843c749SSergey Zigachev 									router.i2c_addr = i2c_record->ucI2CAddr >> 1;
443b843c749SSergey Zigachev 									break;
444b843c749SSergey Zigachev 								case ATOM_ROUTER_DDC_PATH_SELECT_RECORD_TYPE:
445b843c749SSergey Zigachev 									ddc_path = (ATOM_ROUTER_DDC_PATH_SELECT_RECORD *)
446b843c749SSergey Zigachev 										record;
447b843c749SSergey Zigachev 									router.ddc_valid = true;
448b843c749SSergey Zigachev 									router.ddc_mux_type = ddc_path->ucMuxType;
449b843c749SSergey Zigachev 									router.ddc_mux_control_pin = ddc_path->ucMuxControlPin;
450b843c749SSergey Zigachev 									router.ddc_mux_state = ddc_path->ucMuxState[enum_id];
451b843c749SSergey Zigachev 									break;
452b843c749SSergey Zigachev 								case ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD_TYPE:
453b843c749SSergey Zigachev 									cd_path = (ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *)
454b843c749SSergey Zigachev 										record;
455b843c749SSergey Zigachev 									router.cd_valid = true;
456b843c749SSergey Zigachev 									router.cd_mux_type = cd_path->ucMuxType;
457b843c749SSergey Zigachev 									router.cd_mux_control_pin = cd_path->ucMuxControlPin;
458b843c749SSergey Zigachev 									router.cd_mux_state = cd_path->ucMuxState[enum_id];
459b843c749SSergey Zigachev 									break;
460b843c749SSergey Zigachev 								}
461b843c749SSergey Zigachev 								record = (ATOM_COMMON_RECORD_HEADER *)
462b843c749SSergey Zigachev 									((char *)record + record->ucRecordSize);
463b843c749SSergey Zigachev 							}
464b843c749SSergey Zigachev 						}
465b843c749SSergey Zigachev 					}
466b843c749SSergey Zigachev 				}
467b843c749SSergey Zigachev 			}
468b843c749SSergey Zigachev 
469b843c749SSergey Zigachev 			/* look up gpio for ddc, hpd */
470b843c749SSergey Zigachev 			ddc_bus.valid = false;
471b843c749SSergey Zigachev 			hpd.hpd = AMDGPU_HPD_NONE;
472b843c749SSergey Zigachev 			if ((le16_to_cpu(path->usDeviceTag) &
473b843c749SSergey Zigachev 			     (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT)) == 0) {
474b843c749SSergey Zigachev 				for (j = 0; j < con_obj->ucNumberOfObjects; j++) {
475b843c749SSergey Zigachev 					if (le16_to_cpu(path->usConnObjectId) ==
476b843c749SSergey Zigachev 					    le16_to_cpu(con_obj->asObjects[j].
477b843c749SSergey Zigachev 							usObjectID)) {
478b843c749SSergey Zigachev 						ATOM_COMMON_RECORD_HEADER
479b843c749SSergey Zigachev 						    *record =
480b843c749SSergey Zigachev 						    (ATOM_COMMON_RECORD_HEADER
481b843c749SSergey Zigachev 						     *)
482b843c749SSergey Zigachev 						    (ctx->bios + data_offset +
483b843c749SSergey Zigachev 						     le16_to_cpu(con_obj->
484b843c749SSergey Zigachev 								 asObjects[j].
485b843c749SSergey Zigachev 								 usRecordOffset));
486b843c749SSergey Zigachev 						ATOM_I2C_RECORD *i2c_record;
487b843c749SSergey Zigachev 						ATOM_HPD_INT_RECORD *hpd_record;
488b843c749SSergey Zigachev 						ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
489b843c749SSergey Zigachev 
490b843c749SSergey Zigachev 						while (record->ucRecordSize > 0 &&
491b843c749SSergey Zigachev 						       record->ucRecordType > 0 &&
492b843c749SSergey Zigachev 						       record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
493b843c749SSergey Zigachev 							switch (record->ucRecordType) {
494b843c749SSergey Zigachev 							case ATOM_I2C_RECORD_TYPE:
495b843c749SSergey Zigachev 								i2c_record =
496b843c749SSergey Zigachev 								    (ATOM_I2C_RECORD *)
497b843c749SSergey Zigachev 									record;
498b843c749SSergey Zigachev 								i2c_config =
499b843c749SSergey Zigachev 									(ATOM_I2C_ID_CONFIG_ACCESS *)
500b843c749SSergey Zigachev 									&i2c_record->sucI2cId;
501b843c749SSergey Zigachev 								ddc_bus = amdgpu_atombios_lookup_i2c_gpio(adev,
502b843c749SSergey Zigachev 												 i2c_config->
503b843c749SSergey Zigachev 												 ucAccess);
504b843c749SSergey Zigachev 								break;
505b843c749SSergey Zigachev 							case ATOM_HPD_INT_RECORD_TYPE:
506b843c749SSergey Zigachev 								hpd_record =
507b843c749SSergey Zigachev 									(ATOM_HPD_INT_RECORD *)
508b843c749SSergey Zigachev 									record;
509b843c749SSergey Zigachev 								gpio = amdgpu_atombios_lookup_gpio(adev,
510b843c749SSergey Zigachev 											  hpd_record->ucHPDIntGPIOID);
511b843c749SSergey Zigachev 								hpd = amdgpu_atombios_get_hpd_info_from_gpio(adev, &gpio);
512b843c749SSergey Zigachev 								hpd.plugged_state = hpd_record->ucPlugged_PinState;
513b843c749SSergey Zigachev 								break;
514b843c749SSergey Zigachev 							}
515b843c749SSergey Zigachev 							record =
516b843c749SSergey Zigachev 							    (ATOM_COMMON_RECORD_HEADER
517b843c749SSergey Zigachev 							     *) ((char *)record
518b843c749SSergey Zigachev 								 +
519b843c749SSergey Zigachev 								 record->
520b843c749SSergey Zigachev 								 ucRecordSize);
521b843c749SSergey Zigachev 						}
522b843c749SSergey Zigachev 						break;
523b843c749SSergey Zigachev 					}
524b843c749SSergey Zigachev 				}
525b843c749SSergey Zigachev 			}
526b843c749SSergey Zigachev 
527b843c749SSergey Zigachev 			/* needed for aux chan transactions */
528b843c749SSergey Zigachev 			ddc_bus.hpd = hpd.hpd;
529b843c749SSergey Zigachev 
530b843c749SSergey Zigachev 			conn_id = le16_to_cpu(path->usConnObjectId);
531b843c749SSergey Zigachev 
532b843c749SSergey Zigachev 			amdgpu_display_add_connector(adev,
533b843c749SSergey Zigachev 						      conn_id,
534b843c749SSergey Zigachev 						      le16_to_cpu(path->usDeviceTag),
535b843c749SSergey Zigachev 						      connector_type, &ddc_bus,
536b843c749SSergey Zigachev 						      connector_object_id,
537b843c749SSergey Zigachev 						      &hpd,
538b843c749SSergey Zigachev 						      &router);
539b843c749SSergey Zigachev 
540b843c749SSergey Zigachev 		}
541b843c749SSergey Zigachev 	}
542b843c749SSergey Zigachev 
543b843c749SSergey Zigachev 	amdgpu_link_encoder_connector(adev->ddev);
544b843c749SSergey Zigachev 
545b843c749SSergey Zigachev 	return true;
546b843c749SSergey Zigachev }
547b843c749SSergey Zigachev 
548b843c749SSergey Zigachev union firmware_info {
549b843c749SSergey Zigachev 	ATOM_FIRMWARE_INFO info;
550b843c749SSergey Zigachev 	ATOM_FIRMWARE_INFO_V1_2 info_12;
551b843c749SSergey Zigachev 	ATOM_FIRMWARE_INFO_V1_3 info_13;
552b843c749SSergey Zigachev 	ATOM_FIRMWARE_INFO_V1_4 info_14;
553b843c749SSergey Zigachev 	ATOM_FIRMWARE_INFO_V2_1 info_21;
554b843c749SSergey Zigachev 	ATOM_FIRMWARE_INFO_V2_2 info_22;
555b843c749SSergey Zigachev };
556b843c749SSergey Zigachev 
amdgpu_atombios_get_clock_info(struct amdgpu_device * adev)557b843c749SSergey Zigachev int amdgpu_atombios_get_clock_info(struct amdgpu_device *adev)
558b843c749SSergey Zigachev {
559b843c749SSergey Zigachev 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
560b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
561b843c749SSergey Zigachev 	uint8_t frev, crev;
562b843c749SSergey Zigachev 	uint16_t data_offset;
563b843c749SSergey Zigachev 	int ret = -EINVAL;
564b843c749SSergey Zigachev 
565b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
566b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
567b843c749SSergey Zigachev 		int i;
568b843c749SSergey Zigachev 		struct amdgpu_pll *ppll = &adev->clock.ppll[0];
569b843c749SSergey Zigachev 		struct amdgpu_pll *spll = &adev->clock.spll;
570b843c749SSergey Zigachev 		struct amdgpu_pll *mpll = &adev->clock.mpll;
571b843c749SSergey Zigachev 		union firmware_info *firmware_info =
572b843c749SSergey Zigachev 			(union firmware_info *)(mode_info->atom_context->bios +
573b843c749SSergey Zigachev 						data_offset);
574b843c749SSergey Zigachev 		/* pixel clocks */
575b843c749SSergey Zigachev 		ppll->reference_freq =
576b843c749SSergey Zigachev 		    le16_to_cpu(firmware_info->info.usReferenceClock);
577b843c749SSergey Zigachev 		ppll->reference_div = 0;
578b843c749SSergey Zigachev 
579b843c749SSergey Zigachev 		ppll->pll_out_min =
580b843c749SSergey Zigachev 			le32_to_cpu(firmware_info->info_12.ulMinPixelClockPLL_Output);
581b843c749SSergey Zigachev 		ppll->pll_out_max =
582b843c749SSergey Zigachev 		    le32_to_cpu(firmware_info->info.ulMaxPixelClockPLL_Output);
583b843c749SSergey Zigachev 
584b843c749SSergey Zigachev 		ppll->lcd_pll_out_min =
585b843c749SSergey Zigachev 			le16_to_cpu(firmware_info->info_14.usLcdMinPixelClockPLL_Output) * 100;
586b843c749SSergey Zigachev 		if (ppll->lcd_pll_out_min == 0)
587b843c749SSergey Zigachev 			ppll->lcd_pll_out_min = ppll->pll_out_min;
588b843c749SSergey Zigachev 		ppll->lcd_pll_out_max =
589b843c749SSergey Zigachev 			le16_to_cpu(firmware_info->info_14.usLcdMaxPixelClockPLL_Output) * 100;
590b843c749SSergey Zigachev 		if (ppll->lcd_pll_out_max == 0)
591b843c749SSergey Zigachev 			ppll->lcd_pll_out_max = ppll->pll_out_max;
592b843c749SSergey Zigachev 
593b843c749SSergey Zigachev 		if (ppll->pll_out_min == 0)
594b843c749SSergey Zigachev 			ppll->pll_out_min = 64800;
595b843c749SSergey Zigachev 
596b843c749SSergey Zigachev 		ppll->pll_in_min =
597b843c749SSergey Zigachev 		    le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Input);
598b843c749SSergey Zigachev 		ppll->pll_in_max =
599b843c749SSergey Zigachev 		    le16_to_cpu(firmware_info->info.usMaxPixelClockPLL_Input);
600b843c749SSergey Zigachev 
601b843c749SSergey Zigachev 		ppll->min_post_div = 2;
602b843c749SSergey Zigachev 		ppll->max_post_div = 0x7f;
603b843c749SSergey Zigachev 		ppll->min_frac_feedback_div = 0;
604b843c749SSergey Zigachev 		ppll->max_frac_feedback_div = 9;
605b843c749SSergey Zigachev 		ppll->min_ref_div = 2;
606b843c749SSergey Zigachev 		ppll->max_ref_div = 0x3ff;
607b843c749SSergey Zigachev 		ppll->min_feedback_div = 4;
608b843c749SSergey Zigachev 		ppll->max_feedback_div = 0xfff;
609b843c749SSergey Zigachev 		ppll->best_vco = 0;
610b843c749SSergey Zigachev 
611b843c749SSergey Zigachev 		for (i = 1; i < AMDGPU_MAX_PPLL; i++)
612b843c749SSergey Zigachev 			adev->clock.ppll[i] = *ppll;
613b843c749SSergey Zigachev 
614b843c749SSergey Zigachev 		/* system clock */
615b843c749SSergey Zigachev 		spll->reference_freq =
616b843c749SSergey Zigachev 			le16_to_cpu(firmware_info->info_21.usCoreReferenceClock);
617b843c749SSergey Zigachev 		spll->reference_div = 0;
618b843c749SSergey Zigachev 
619b843c749SSergey Zigachev 		spll->pll_out_min =
620b843c749SSergey Zigachev 		    le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Output);
621b843c749SSergey Zigachev 		spll->pll_out_max =
622b843c749SSergey Zigachev 		    le32_to_cpu(firmware_info->info.ulMaxEngineClockPLL_Output);
623b843c749SSergey Zigachev 
624b843c749SSergey Zigachev 		/* ??? */
625b843c749SSergey Zigachev 		if (spll->pll_out_min == 0)
626b843c749SSergey Zigachev 			spll->pll_out_min = 64800;
627b843c749SSergey Zigachev 
628b843c749SSergey Zigachev 		spll->pll_in_min =
629b843c749SSergey Zigachev 		    le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Input);
630b843c749SSergey Zigachev 		spll->pll_in_max =
631b843c749SSergey Zigachev 		    le16_to_cpu(firmware_info->info.usMaxEngineClockPLL_Input);
632b843c749SSergey Zigachev 
633b843c749SSergey Zigachev 		spll->min_post_div = 1;
634b843c749SSergey Zigachev 		spll->max_post_div = 1;
635b843c749SSergey Zigachev 		spll->min_ref_div = 2;
636b843c749SSergey Zigachev 		spll->max_ref_div = 0xff;
637b843c749SSergey Zigachev 		spll->min_feedback_div = 4;
638b843c749SSergey Zigachev 		spll->max_feedback_div = 0xff;
639b843c749SSergey Zigachev 		spll->best_vco = 0;
640b843c749SSergey Zigachev 
641b843c749SSergey Zigachev 		/* memory clock */
642b843c749SSergey Zigachev 		mpll->reference_freq =
643b843c749SSergey Zigachev 			le16_to_cpu(firmware_info->info_21.usMemoryReferenceClock);
644b843c749SSergey Zigachev 		mpll->reference_div = 0;
645b843c749SSergey Zigachev 
646b843c749SSergey Zigachev 		mpll->pll_out_min =
647b843c749SSergey Zigachev 		    le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Output);
648b843c749SSergey Zigachev 		mpll->pll_out_max =
649b843c749SSergey Zigachev 		    le32_to_cpu(firmware_info->info.ulMaxMemoryClockPLL_Output);
650b843c749SSergey Zigachev 
651b843c749SSergey Zigachev 		/* ??? */
652b843c749SSergey Zigachev 		if (mpll->pll_out_min == 0)
653b843c749SSergey Zigachev 			mpll->pll_out_min = 64800;
654b843c749SSergey Zigachev 
655b843c749SSergey Zigachev 		mpll->pll_in_min =
656b843c749SSergey Zigachev 		    le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Input);
657b843c749SSergey Zigachev 		mpll->pll_in_max =
658b843c749SSergey Zigachev 		    le16_to_cpu(firmware_info->info.usMaxMemoryClockPLL_Input);
659b843c749SSergey Zigachev 
660b843c749SSergey Zigachev 		adev->clock.default_sclk =
661b843c749SSergey Zigachev 		    le32_to_cpu(firmware_info->info.ulDefaultEngineClock);
662b843c749SSergey Zigachev 		adev->clock.default_mclk =
663b843c749SSergey Zigachev 		    le32_to_cpu(firmware_info->info.ulDefaultMemoryClock);
664b843c749SSergey Zigachev 
665b843c749SSergey Zigachev 		mpll->min_post_div = 1;
666b843c749SSergey Zigachev 		mpll->max_post_div = 1;
667b843c749SSergey Zigachev 		mpll->min_ref_div = 2;
668b843c749SSergey Zigachev 		mpll->max_ref_div = 0xff;
669b843c749SSergey Zigachev 		mpll->min_feedback_div = 4;
670b843c749SSergey Zigachev 		mpll->max_feedback_div = 0xff;
671b843c749SSergey Zigachev 		mpll->best_vco = 0;
672b843c749SSergey Zigachev 
673b843c749SSergey Zigachev 		/* disp clock */
674b843c749SSergey Zigachev 		adev->clock.default_dispclk =
675b843c749SSergey Zigachev 			le32_to_cpu(firmware_info->info_21.ulDefaultDispEngineClkFreq);
676b843c749SSergey Zigachev 		/* set a reasonable default for DP */
677b843c749SSergey Zigachev 		if (adev->clock.default_dispclk < 53900) {
678b843c749SSergey Zigachev 			DRM_DEBUG("Changing default dispclk from %dMhz to 600Mhz\n",
679b843c749SSergey Zigachev 				  adev->clock.default_dispclk / 100);
680b843c749SSergey Zigachev 			adev->clock.default_dispclk = 60000;
681b843c749SSergey Zigachev 		} else if (adev->clock.default_dispclk <= 60000) {
682b843c749SSergey Zigachev 			DRM_DEBUG("Changing default dispclk from %dMhz to 625Mhz\n",
683b843c749SSergey Zigachev 				  adev->clock.default_dispclk / 100);
684b843c749SSergey Zigachev 			adev->clock.default_dispclk = 62500;
685b843c749SSergey Zigachev 		}
686b843c749SSergey Zigachev 		adev->clock.dp_extclk =
687b843c749SSergey Zigachev 			le16_to_cpu(firmware_info->info_21.usUniphyDPModeExtClkFreq);
688b843c749SSergey Zigachev 		adev->clock.current_dispclk = adev->clock.default_dispclk;
689b843c749SSergey Zigachev 
690b843c749SSergey Zigachev 		adev->clock.max_pixel_clock = le16_to_cpu(firmware_info->info.usMaxPixelClock);
691b843c749SSergey Zigachev 		if (adev->clock.max_pixel_clock == 0)
692b843c749SSergey Zigachev 			adev->clock.max_pixel_clock = 40000;
693b843c749SSergey Zigachev 
694b843c749SSergey Zigachev 		/* not technically a clock, but... */
695b843c749SSergey Zigachev 		adev->mode_info.firmware_flags =
696b843c749SSergey Zigachev 			le16_to_cpu(firmware_info->info.usFirmwareCapability.susAccess);
697b843c749SSergey Zigachev 
698b843c749SSergey Zigachev 		ret = 0;
699b843c749SSergey Zigachev 	}
700b843c749SSergey Zigachev 
701b843c749SSergey Zigachev 	adev->pm.current_sclk = adev->clock.default_sclk;
702b843c749SSergey Zigachev 	adev->pm.current_mclk = adev->clock.default_mclk;
703b843c749SSergey Zigachev 
704b843c749SSergey Zigachev 	return ret;
705b843c749SSergey Zigachev }
706b843c749SSergey Zigachev 
707b843c749SSergey Zigachev union gfx_info {
708b843c749SSergey Zigachev 	ATOM_GFX_INFO_V2_1 info;
709b843c749SSergey Zigachev };
710b843c749SSergey Zigachev 
amdgpu_atombios_get_gfx_info(struct amdgpu_device * adev)711b843c749SSergey Zigachev int amdgpu_atombios_get_gfx_info(struct amdgpu_device *adev)
712b843c749SSergey Zigachev {
713b843c749SSergey Zigachev 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
714b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, GFX_Info);
715b843c749SSergey Zigachev 	uint8_t frev, crev;
716b843c749SSergey Zigachev 	uint16_t data_offset;
717b843c749SSergey Zigachev 	int ret = -EINVAL;
718b843c749SSergey Zigachev 
719b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
720b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
721b843c749SSergey Zigachev 		union gfx_info *gfx_info = (union gfx_info *)
722b843c749SSergey Zigachev 			(mode_info->atom_context->bios + data_offset);
723b843c749SSergey Zigachev 
724b843c749SSergey Zigachev 		adev->gfx.config.max_shader_engines = gfx_info->info.max_shader_engines;
725b843c749SSergey Zigachev 		adev->gfx.config.max_tile_pipes = gfx_info->info.max_tile_pipes;
726b843c749SSergey Zigachev 		adev->gfx.config.max_cu_per_sh = gfx_info->info.max_cu_per_sh;
727b843c749SSergey Zigachev 		adev->gfx.config.max_sh_per_se = gfx_info->info.max_sh_per_se;
728b843c749SSergey Zigachev 		adev->gfx.config.max_backends_per_se = gfx_info->info.max_backends_per_se;
729b843c749SSergey Zigachev 		adev->gfx.config.max_texture_channel_caches =
730b843c749SSergey Zigachev 			gfx_info->info.max_texture_channel_caches;
731b843c749SSergey Zigachev 
732b843c749SSergey Zigachev 		ret = 0;
733b843c749SSergey Zigachev 	}
734b843c749SSergey Zigachev 	return ret;
735b843c749SSergey Zigachev }
736b843c749SSergey Zigachev 
737b843c749SSergey Zigachev union igp_info {
738b843c749SSergey Zigachev 	struct _ATOM_INTEGRATED_SYSTEM_INFO info;
739b843c749SSergey Zigachev 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
740b843c749SSergey Zigachev 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
741b843c749SSergey Zigachev 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
742b843c749SSergey Zigachev 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
743b843c749SSergey Zigachev 	struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_9 info_9;
744b843c749SSergey Zigachev };
745b843c749SSergey Zigachev 
746b843c749SSergey Zigachev /*
747b843c749SSergey Zigachev  * Return vram width from integrated system info table, if available,
748b843c749SSergey Zigachev  * or 0 if not.
749b843c749SSergey Zigachev  */
amdgpu_atombios_get_vram_width(struct amdgpu_device * adev)750b843c749SSergey Zigachev int amdgpu_atombios_get_vram_width(struct amdgpu_device *adev)
751b843c749SSergey Zigachev {
752b843c749SSergey Zigachev 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
753b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
754b843c749SSergey Zigachev 	u16 data_offset, size;
755b843c749SSergey Zigachev 	union igp_info *igp_info;
756b843c749SSergey Zigachev 	u8 frev, crev;
757b843c749SSergey Zigachev 
758b843c749SSergey Zigachev 	/* get any igp specific overrides */
759b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
760b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
761b843c749SSergey Zigachev 		igp_info = (union igp_info *)
762b843c749SSergey Zigachev 			(mode_info->atom_context->bios + data_offset);
763b843c749SSergey Zigachev 		switch (crev) {
764b843c749SSergey Zigachev 		case 8:
765b843c749SSergey Zigachev 		case 9:
766b843c749SSergey Zigachev 			return igp_info->info_8.ucUMAChannelNumber * 64;
767b843c749SSergey Zigachev 		default:
768b843c749SSergey Zigachev 			return 0;
769b843c749SSergey Zigachev 		}
770b843c749SSergey Zigachev 	}
771b843c749SSergey Zigachev 
772b843c749SSergey Zigachev 	return 0;
773b843c749SSergey Zigachev }
774b843c749SSergey Zigachev 
amdgpu_atombios_get_igp_ss_overrides(struct amdgpu_device * adev,struct amdgpu_atom_ss * ss,int id)775b843c749SSergey Zigachev static void amdgpu_atombios_get_igp_ss_overrides(struct amdgpu_device *adev,
776b843c749SSergey Zigachev 						 struct amdgpu_atom_ss *ss,
777b843c749SSergey Zigachev 						 int id)
778b843c749SSergey Zigachev {
779b843c749SSergey Zigachev 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
780b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
781b843c749SSergey Zigachev 	u16 data_offset, size;
782b843c749SSergey Zigachev 	union igp_info *igp_info;
783b843c749SSergey Zigachev 	u8 frev, crev;
784b843c749SSergey Zigachev 	u16 percentage = 0, rate = 0;
785b843c749SSergey Zigachev 
786b843c749SSergey Zigachev 	/* get any igp specific overrides */
787b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
788b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
789b843c749SSergey Zigachev 		igp_info = (union igp_info *)
790b843c749SSergey Zigachev 			(mode_info->atom_context->bios + data_offset);
791b843c749SSergey Zigachev 		switch (crev) {
792b843c749SSergey Zigachev 		case 6:
793b843c749SSergey Zigachev 			switch (id) {
794b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_TMDS:
795b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_6.usDVISSPercentage);
796b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_6.usDVISSpreadRateIn10Hz);
797b843c749SSergey Zigachev 				break;
798b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_HDMI:
799b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_6.usHDMISSPercentage);
800b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_6.usHDMISSpreadRateIn10Hz);
801b843c749SSergey Zigachev 				break;
802b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_LVDS:
803b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_6.usLvdsSSPercentage);
804b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_6.usLvdsSSpreadRateIn10Hz);
805b843c749SSergey Zigachev 				break;
806b843c749SSergey Zigachev 			}
807b843c749SSergey Zigachev 			break;
808b843c749SSergey Zigachev 		case 7:
809b843c749SSergey Zigachev 			switch (id) {
810b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_TMDS:
811b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_7.usDVISSPercentage);
812b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_7.usDVISSpreadRateIn10Hz);
813b843c749SSergey Zigachev 				break;
814b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_HDMI:
815b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_7.usHDMISSPercentage);
816b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_7.usHDMISSpreadRateIn10Hz);
817b843c749SSergey Zigachev 				break;
818b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_LVDS:
819b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_7.usLvdsSSPercentage);
820b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_7.usLvdsSSpreadRateIn10Hz);
821b843c749SSergey Zigachev 				break;
822b843c749SSergey Zigachev 			}
823b843c749SSergey Zigachev 			break;
824b843c749SSergey Zigachev 		case 8:
825b843c749SSergey Zigachev 			switch (id) {
826b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_TMDS:
827b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_8.usDVISSPercentage);
828b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_8.usDVISSpreadRateIn10Hz);
829b843c749SSergey Zigachev 				break;
830b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_HDMI:
831b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_8.usHDMISSPercentage);
832b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_8.usHDMISSpreadRateIn10Hz);
833b843c749SSergey Zigachev 				break;
834b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_LVDS:
835b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_8.usLvdsSSPercentage);
836b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_8.usLvdsSSpreadRateIn10Hz);
837b843c749SSergey Zigachev 				break;
838b843c749SSergey Zigachev 			}
839b843c749SSergey Zigachev 			break;
840b843c749SSergey Zigachev 		case 9:
841b843c749SSergey Zigachev 			switch (id) {
842b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_TMDS:
843b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_9.usDVISSPercentage);
844b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_9.usDVISSpreadRateIn10Hz);
845b843c749SSergey Zigachev 				break;
846b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_HDMI:
847b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_9.usHDMISSPercentage);
848b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_9.usHDMISSpreadRateIn10Hz);
849b843c749SSergey Zigachev 				break;
850b843c749SSergey Zigachev 			case ASIC_INTERNAL_SS_ON_LVDS:
851b843c749SSergey Zigachev 				percentage = le16_to_cpu(igp_info->info_9.usLvdsSSPercentage);
852b843c749SSergey Zigachev 				rate = le16_to_cpu(igp_info->info_9.usLvdsSSpreadRateIn10Hz);
853b843c749SSergey Zigachev 				break;
854b843c749SSergey Zigachev 			}
855b843c749SSergey Zigachev 			break;
856b843c749SSergey Zigachev 		default:
857b843c749SSergey Zigachev 			DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
858b843c749SSergey Zigachev 			break;
859b843c749SSergey Zigachev 		}
860b843c749SSergey Zigachev 		if (percentage)
861b843c749SSergey Zigachev 			ss->percentage = percentage;
862b843c749SSergey Zigachev 		if (rate)
863b843c749SSergey Zigachev 			ss->rate = rate;
864b843c749SSergey Zigachev 	}
865b843c749SSergey Zigachev }
866b843c749SSergey Zigachev 
867b843c749SSergey Zigachev union asic_ss_info {
868b843c749SSergey Zigachev 	struct _ATOM_ASIC_INTERNAL_SS_INFO info;
869b843c749SSergey Zigachev 	struct _ATOM_ASIC_INTERNAL_SS_INFO_V2 info_2;
870b843c749SSergey Zigachev 	struct _ATOM_ASIC_INTERNAL_SS_INFO_V3 info_3;
871b843c749SSergey Zigachev };
872b843c749SSergey Zigachev 
873b843c749SSergey Zigachev union asic_ss_assignment {
874b843c749SSergey Zigachev 	struct _ATOM_ASIC_SS_ASSIGNMENT v1;
875b843c749SSergey Zigachev 	struct _ATOM_ASIC_SS_ASSIGNMENT_V2 v2;
876b843c749SSergey Zigachev 	struct _ATOM_ASIC_SS_ASSIGNMENT_V3 v3;
877b843c749SSergey Zigachev };
878b843c749SSergey Zigachev 
amdgpu_atombios_get_asic_ss_info(struct amdgpu_device * adev,struct amdgpu_atom_ss * ss,int id,u32 clock)879b843c749SSergey Zigachev bool amdgpu_atombios_get_asic_ss_info(struct amdgpu_device *adev,
880b843c749SSergey Zigachev 				      struct amdgpu_atom_ss *ss,
881b843c749SSergey Zigachev 				      int id, u32 clock)
882b843c749SSergey Zigachev {
883b843c749SSergey Zigachev 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
884b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
885b843c749SSergey Zigachev 	uint16_t data_offset, size;
886b843c749SSergey Zigachev 	union asic_ss_info *ss_info;
887b843c749SSergey Zigachev 	union asic_ss_assignment *ss_assign;
888b843c749SSergey Zigachev 	uint8_t frev, crev;
889b843c749SSergey Zigachev 	int i, num_indices;
890b843c749SSergey Zigachev 
891b843c749SSergey Zigachev 	if (id == ASIC_INTERNAL_MEMORY_SS) {
892b843c749SSergey Zigachev 		if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_MEMORY_CLOCK_SS_SUPPORT))
893b843c749SSergey Zigachev 			return false;
894b843c749SSergey Zigachev 	}
895b843c749SSergey Zigachev 	if (id == ASIC_INTERNAL_ENGINE_SS) {
896b843c749SSergey Zigachev 		if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_ENGINE_CLOCK_SS_SUPPORT))
897b843c749SSergey Zigachev 			return false;
898b843c749SSergey Zigachev 	}
899b843c749SSergey Zigachev 
900b843c749SSergey Zigachev 	memset(ss, 0, sizeof(struct amdgpu_atom_ss));
901b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
902b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
903b843c749SSergey Zigachev 
904b843c749SSergey Zigachev 		ss_info =
905b843c749SSergey Zigachev 			(union asic_ss_info *)(mode_info->atom_context->bios + data_offset);
906b843c749SSergey Zigachev 
907b843c749SSergey Zigachev 		switch (frev) {
908b843c749SSergey Zigachev 		case 1:
909b843c749SSergey Zigachev 			num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
910b843c749SSergey Zigachev 				sizeof(ATOM_ASIC_SS_ASSIGNMENT);
911b843c749SSergey Zigachev 
912b843c749SSergey Zigachev 			ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info.asSpreadSpectrum[0]);
913b843c749SSergey Zigachev 			for (i = 0; i < num_indices; i++) {
914b843c749SSergey Zigachev 				if ((ss_assign->v1.ucClockIndication == id) &&
915b843c749SSergey Zigachev 				    (clock <= le32_to_cpu(ss_assign->v1.ulTargetClockRange))) {
916b843c749SSergey Zigachev 					ss->percentage =
917b843c749SSergey Zigachev 						le16_to_cpu(ss_assign->v1.usSpreadSpectrumPercentage);
918b843c749SSergey Zigachev 					ss->type = ss_assign->v1.ucSpreadSpectrumMode;
919b843c749SSergey Zigachev 					ss->rate = le16_to_cpu(ss_assign->v1.usSpreadRateInKhz);
920b843c749SSergey Zigachev 					ss->percentage_divider = 100;
921b843c749SSergey Zigachev 					return true;
922b843c749SSergey Zigachev 				}
923b843c749SSergey Zigachev 				ss_assign = (union asic_ss_assignment *)
924b843c749SSergey Zigachev 					((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT));
925b843c749SSergey Zigachev 			}
926b843c749SSergey Zigachev 			break;
927b843c749SSergey Zigachev 		case 2:
928b843c749SSergey Zigachev 			num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
929b843c749SSergey Zigachev 				sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2);
930b843c749SSergey Zigachev 			ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_2.asSpreadSpectrum[0]);
931b843c749SSergey Zigachev 			for (i = 0; i < num_indices; i++) {
932b843c749SSergey Zigachev 				if ((ss_assign->v2.ucClockIndication == id) &&
933b843c749SSergey Zigachev 				    (clock <= le32_to_cpu(ss_assign->v2.ulTargetClockRange))) {
934b843c749SSergey Zigachev 					ss->percentage =
935b843c749SSergey Zigachev 						le16_to_cpu(ss_assign->v2.usSpreadSpectrumPercentage);
936b843c749SSergey Zigachev 					ss->type = ss_assign->v2.ucSpreadSpectrumMode;
937b843c749SSergey Zigachev 					ss->rate = le16_to_cpu(ss_assign->v2.usSpreadRateIn10Hz);
938b843c749SSergey Zigachev 					ss->percentage_divider = 100;
939b843c749SSergey Zigachev 					if ((crev == 2) &&
940b843c749SSergey Zigachev 					    ((id == ASIC_INTERNAL_ENGINE_SS) ||
941b843c749SSergey Zigachev 					     (id == ASIC_INTERNAL_MEMORY_SS)))
942b843c749SSergey Zigachev 						ss->rate /= 100;
943b843c749SSergey Zigachev 					return true;
944b843c749SSergey Zigachev 				}
945b843c749SSergey Zigachev 				ss_assign = (union asic_ss_assignment *)
946b843c749SSergey Zigachev 					((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2));
947b843c749SSergey Zigachev 			}
948b843c749SSergey Zigachev 			break;
949b843c749SSergey Zigachev 		case 3:
950b843c749SSergey Zigachev 			num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
951b843c749SSergey Zigachev 				sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3);
952b843c749SSergey Zigachev 			ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_3.asSpreadSpectrum[0]);
953b843c749SSergey Zigachev 			for (i = 0; i < num_indices; i++) {
954b843c749SSergey Zigachev 				if ((ss_assign->v3.ucClockIndication == id) &&
955b843c749SSergey Zigachev 				    (clock <= le32_to_cpu(ss_assign->v3.ulTargetClockRange))) {
956b843c749SSergey Zigachev 					ss->percentage =
957b843c749SSergey Zigachev 						le16_to_cpu(ss_assign->v3.usSpreadSpectrumPercentage);
958b843c749SSergey Zigachev 					ss->type = ss_assign->v3.ucSpreadSpectrumMode;
959b843c749SSergey Zigachev 					ss->rate = le16_to_cpu(ss_assign->v3.usSpreadRateIn10Hz);
960b843c749SSergey Zigachev 					if (ss_assign->v3.ucSpreadSpectrumMode &
961b843c749SSergey Zigachev 					    SS_MODE_V3_PERCENTAGE_DIV_BY_1000_MASK)
962b843c749SSergey Zigachev 						ss->percentage_divider = 1000;
963b843c749SSergey Zigachev 					else
964b843c749SSergey Zigachev 						ss->percentage_divider = 100;
965b843c749SSergey Zigachev 					if ((id == ASIC_INTERNAL_ENGINE_SS) ||
966b843c749SSergey Zigachev 					    (id == ASIC_INTERNAL_MEMORY_SS))
967b843c749SSergey Zigachev 						ss->rate /= 100;
968b843c749SSergey Zigachev 					if (adev->flags & AMD_IS_APU)
969b843c749SSergey Zigachev 						amdgpu_atombios_get_igp_ss_overrides(adev, ss, id);
970b843c749SSergey Zigachev 					return true;
971b843c749SSergey Zigachev 				}
972b843c749SSergey Zigachev 				ss_assign = (union asic_ss_assignment *)
973b843c749SSergey Zigachev 					((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3));
974b843c749SSergey Zigachev 			}
975b843c749SSergey Zigachev 			break;
976b843c749SSergey Zigachev 		default:
977b843c749SSergey Zigachev 			DRM_ERROR("Unsupported ASIC_InternalSS_Info table: %d %d\n", frev, crev);
978b843c749SSergey Zigachev 			break;
979b843c749SSergey Zigachev 		}
980b843c749SSergey Zigachev 
981b843c749SSergey Zigachev 	}
982b843c749SSergey Zigachev 	return false;
983b843c749SSergey Zigachev }
984b843c749SSergey Zigachev 
985b843c749SSergey Zigachev union get_clock_dividers {
986b843c749SSergey Zigachev 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS v1;
987b843c749SSergey Zigachev 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V2 v2;
988b843c749SSergey Zigachev 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V3 v3;
989b843c749SSergey Zigachev 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V4 v4;
990b843c749SSergey Zigachev 	struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V5 v5;
991b843c749SSergey Zigachev 	struct _COMPUTE_GPU_CLOCK_INPUT_PARAMETERS_V1_6 v6_in;
992b843c749SSergey Zigachev 	struct _COMPUTE_GPU_CLOCK_OUTPUT_PARAMETERS_V1_6 v6_out;
993b843c749SSergey Zigachev };
994b843c749SSergey Zigachev 
amdgpu_atombios_get_clock_dividers(struct amdgpu_device * adev,u8 clock_type,u32 clock,bool strobe_mode,struct atom_clock_dividers * dividers)995b843c749SSergey Zigachev int amdgpu_atombios_get_clock_dividers(struct amdgpu_device *adev,
996b843c749SSergey Zigachev 				       u8 clock_type,
997b843c749SSergey Zigachev 				       u32 clock,
998b843c749SSergey Zigachev 				       bool strobe_mode,
999b843c749SSergey Zigachev 				       struct atom_clock_dividers *dividers)
1000b843c749SSergey Zigachev {
1001b843c749SSergey Zigachev 	union get_clock_dividers args;
1002b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryEnginePLL);
1003b843c749SSergey Zigachev 	u8 frev, crev;
1004b843c749SSergey Zigachev 
1005b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
1006b843c749SSergey Zigachev 	memset(dividers, 0, sizeof(struct atom_clock_dividers));
1007b843c749SSergey Zigachev 
1008b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1009b843c749SSergey Zigachev 		return -EINVAL;
1010b843c749SSergey Zigachev 
1011b843c749SSergey Zigachev 	switch (crev) {
1012b843c749SSergey Zigachev 	case 2:
1013b843c749SSergey Zigachev 	case 3:
1014b843c749SSergey Zigachev 	case 5:
1015b843c749SSergey Zigachev 		/* r6xx, r7xx, evergreen, ni, si.
1016b843c749SSergey Zigachev 		 * TODO: add support for asic_type <= CHIP_RV770*/
1017b843c749SSergey Zigachev 		if (clock_type == COMPUTE_ENGINE_PLL_PARAM) {
1018b843c749SSergey Zigachev 			args.v3.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
1019b843c749SSergey Zigachev 
1020b843c749SSergey Zigachev 			amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1021b843c749SSergey Zigachev 
1022b843c749SSergey Zigachev 			dividers->post_div = args.v3.ucPostDiv;
1023b843c749SSergey Zigachev 			dividers->enable_post_div = (args.v3.ucCntlFlag &
1024b843c749SSergey Zigachev 						     ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
1025b843c749SSergey Zigachev 			dividers->enable_dithen = (args.v3.ucCntlFlag &
1026b843c749SSergey Zigachev 						   ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
1027b843c749SSergey Zigachev 			dividers->whole_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDiv);
1028b843c749SSergey Zigachev 			dividers->frac_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDivFrac);
1029b843c749SSergey Zigachev 			dividers->ref_div = args.v3.ucRefDiv;
1030b843c749SSergey Zigachev 			dividers->vco_mode = (args.v3.ucCntlFlag &
1031b843c749SSergey Zigachev 					      ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
1032b843c749SSergey Zigachev 		} else {
1033b843c749SSergey Zigachev 			/* for SI we use ComputeMemoryClockParam for memory plls */
1034b843c749SSergey Zigachev 			if (adev->asic_type >= CHIP_TAHITI)
1035b843c749SSergey Zigachev 				return -EINVAL;
1036b843c749SSergey Zigachev 			args.v5.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
1037b843c749SSergey Zigachev 			if (strobe_mode)
1038b843c749SSergey Zigachev 				args.v5.ucInputFlag = ATOM_PLL_INPUT_FLAG_PLL_STROBE_MODE_EN;
1039b843c749SSergey Zigachev 
1040b843c749SSergey Zigachev 			amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1041b843c749SSergey Zigachev 
1042b843c749SSergey Zigachev 			dividers->post_div = args.v5.ucPostDiv;
1043b843c749SSergey Zigachev 			dividers->enable_post_div = (args.v5.ucCntlFlag &
1044b843c749SSergey Zigachev 						     ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
1045b843c749SSergey Zigachev 			dividers->enable_dithen = (args.v5.ucCntlFlag &
1046b843c749SSergey Zigachev 						   ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
1047b843c749SSergey Zigachev 			dividers->whole_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDiv);
1048b843c749SSergey Zigachev 			dividers->frac_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDivFrac);
1049b843c749SSergey Zigachev 			dividers->ref_div = args.v5.ucRefDiv;
1050b843c749SSergey Zigachev 			dividers->vco_mode = (args.v5.ucCntlFlag &
1051b843c749SSergey Zigachev 					      ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
1052b843c749SSergey Zigachev 		}
1053b843c749SSergey Zigachev 		break;
1054b843c749SSergey Zigachev 	case 4:
1055b843c749SSergey Zigachev 		/* fusion */
1056b843c749SSergey Zigachev 		args.v4.ulClock = cpu_to_le32(clock);	/* 10 khz */
1057b843c749SSergey Zigachev 
1058b843c749SSergey Zigachev 		amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1059b843c749SSergey Zigachev 
1060b843c749SSergey Zigachev 		dividers->post_divider = dividers->post_div = args.v4.ucPostDiv;
1061b843c749SSergey Zigachev 		dividers->real_clock = le32_to_cpu(args.v4.ulClock);
1062b843c749SSergey Zigachev 		break;
1063b843c749SSergey Zigachev 	case 6:
1064b843c749SSergey Zigachev 		/* CI */
1065b843c749SSergey Zigachev 		/* COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK, COMPUTE_GPUCLK_INPUT_FLAG_SCLK */
1066b843c749SSergey Zigachev 		args.v6_in.ulClock.ulComputeClockFlag = clock_type;
1067b843c749SSergey Zigachev 		args.v6_in.ulClock.ulClockFreq = cpu_to_le32(clock);	/* 10 khz */
1068b843c749SSergey Zigachev 
1069b843c749SSergey Zigachev 		amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1070b843c749SSergey Zigachev 
1071b843c749SSergey Zigachev 		dividers->whole_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDiv);
1072b843c749SSergey Zigachev 		dividers->frac_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDivFrac);
1073b843c749SSergey Zigachev 		dividers->ref_div = args.v6_out.ucPllRefDiv;
1074b843c749SSergey Zigachev 		dividers->post_div = args.v6_out.ucPllPostDiv;
1075b843c749SSergey Zigachev 		dividers->flags = args.v6_out.ucPllCntlFlag;
1076b843c749SSergey Zigachev 		dividers->real_clock = le32_to_cpu(args.v6_out.ulClock.ulClock);
1077b843c749SSergey Zigachev 		dividers->post_divider = args.v6_out.ulClock.ucPostDiv;
1078b843c749SSergey Zigachev 		break;
1079b843c749SSergey Zigachev 	default:
1080b843c749SSergey Zigachev 		return -EINVAL;
1081b843c749SSergey Zigachev 	}
1082b843c749SSergey Zigachev 	return 0;
1083b843c749SSergey Zigachev }
1084b843c749SSergey Zigachev 
amdgpu_atombios_get_memory_pll_dividers(struct amdgpu_device * adev,u32 clock,bool strobe_mode,struct atom_mpll_param * mpll_param)1085b843c749SSergey Zigachev int amdgpu_atombios_get_memory_pll_dividers(struct amdgpu_device *adev,
1086b843c749SSergey Zigachev 					    u32 clock,
1087b843c749SSergey Zigachev 					    bool strobe_mode,
1088b843c749SSergey Zigachev 					    struct atom_mpll_param *mpll_param)
1089b843c749SSergey Zigachev {
1090b843c749SSergey Zigachev 	COMPUTE_MEMORY_CLOCK_PARAM_PARAMETERS_V2_1 args;
1091b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryClockParam);
1092b843c749SSergey Zigachev 	u8 frev, crev;
1093b843c749SSergey Zigachev 
1094b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
1095b843c749SSergey Zigachev 	memset(mpll_param, 0, sizeof(struct atom_mpll_param));
1096b843c749SSergey Zigachev 
1097b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1098b843c749SSergey Zigachev 		return -EINVAL;
1099b843c749SSergey Zigachev 
1100b843c749SSergey Zigachev 	switch (frev) {
1101b843c749SSergey Zigachev 	case 2:
1102b843c749SSergey Zigachev 		switch (crev) {
1103b843c749SSergey Zigachev 		case 1:
1104b843c749SSergey Zigachev 			/* SI */
1105b843c749SSergey Zigachev 			args.ulClock = cpu_to_le32(clock);	/* 10 khz */
1106b843c749SSergey Zigachev 			args.ucInputFlag = 0;
1107b843c749SSergey Zigachev 			if (strobe_mode)
1108b843c749SSergey Zigachev 				args.ucInputFlag |= MPLL_INPUT_FLAG_STROBE_MODE_EN;
1109b843c749SSergey Zigachev 
1110b843c749SSergey Zigachev 			amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1111b843c749SSergey Zigachev 
1112b843c749SSergey Zigachev 			mpll_param->clkfrac = le16_to_cpu(args.ulFbDiv.usFbDivFrac);
1113b843c749SSergey Zigachev 			mpll_param->clkf = le16_to_cpu(args.ulFbDiv.usFbDiv);
1114b843c749SSergey Zigachev 			mpll_param->post_div = args.ucPostDiv;
1115b843c749SSergey Zigachev 			mpll_param->dll_speed = args.ucDllSpeed;
1116b843c749SSergey Zigachev 			mpll_param->bwcntl = args.ucBWCntl;
1117b843c749SSergey Zigachev 			mpll_param->vco_mode =
1118b843c749SSergey Zigachev 				(args.ucPllCntlFlag & MPLL_CNTL_FLAG_VCO_MODE_MASK);
1119b843c749SSergey Zigachev 			mpll_param->yclk_sel =
1120b843c749SSergey Zigachev 				(args.ucPllCntlFlag & MPLL_CNTL_FLAG_BYPASS_DQ_PLL) ? 1 : 0;
1121b843c749SSergey Zigachev 			mpll_param->qdr =
1122b843c749SSergey Zigachev 				(args.ucPllCntlFlag & MPLL_CNTL_FLAG_QDR_ENABLE) ? 1 : 0;
1123b843c749SSergey Zigachev 			mpll_param->half_rate =
1124b843c749SSergey Zigachev 				(args.ucPllCntlFlag & MPLL_CNTL_FLAG_AD_HALF_RATE) ? 1 : 0;
1125b843c749SSergey Zigachev 			break;
1126b843c749SSergey Zigachev 		default:
1127b843c749SSergey Zigachev 			return -EINVAL;
1128b843c749SSergey Zigachev 		}
1129b843c749SSergey Zigachev 		break;
1130b843c749SSergey Zigachev 	default:
1131b843c749SSergey Zigachev 		return -EINVAL;
1132b843c749SSergey Zigachev 	}
1133b843c749SSergey Zigachev 	return 0;
1134b843c749SSergey Zigachev }
1135b843c749SSergey Zigachev 
amdgpu_atombios_set_engine_dram_timings(struct amdgpu_device * adev,u32 eng_clock,u32 mem_clock)1136b843c749SSergey Zigachev void amdgpu_atombios_set_engine_dram_timings(struct amdgpu_device *adev,
1137b843c749SSergey Zigachev 					     u32 eng_clock, u32 mem_clock)
1138b843c749SSergey Zigachev {
1139b843c749SSergey Zigachev 	SET_ENGINE_CLOCK_PS_ALLOCATION args;
1140b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, DynamicMemorySettings);
1141b843c749SSergey Zigachev 	u32 tmp;
1142b843c749SSergey Zigachev 
1143b843c749SSergey Zigachev 	memset(&args, 0, sizeof(args));
1144b843c749SSergey Zigachev 
1145b843c749SSergey Zigachev 	tmp = eng_clock & SET_CLOCK_FREQ_MASK;
1146b843c749SSergey Zigachev 	tmp |= (COMPUTE_ENGINE_PLL_PARAM << 24);
1147b843c749SSergey Zigachev 
1148b843c749SSergey Zigachev 	args.ulTargetEngineClock = cpu_to_le32(tmp);
1149b843c749SSergey Zigachev 	if (mem_clock)
1150b843c749SSergey Zigachev 		args.sReserved.ulClock = cpu_to_le32(mem_clock & SET_CLOCK_FREQ_MASK);
1151b843c749SSergey Zigachev 
1152b843c749SSergey Zigachev 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1153b843c749SSergey Zigachev }
1154b843c749SSergey Zigachev 
amdgpu_atombios_get_default_voltages(struct amdgpu_device * adev,u16 * vddc,u16 * vddci,u16 * mvdd)1155b843c749SSergey Zigachev void amdgpu_atombios_get_default_voltages(struct amdgpu_device *adev,
1156b843c749SSergey Zigachev 					  u16 *vddc, u16 *vddci, u16 *mvdd)
1157b843c749SSergey Zigachev {
1158b843c749SSergey Zigachev 	struct amdgpu_mode_info *mode_info = &adev->mode_info;
1159b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
1160b843c749SSergey Zigachev 	u8 frev, crev;
1161b843c749SSergey Zigachev 	u16 data_offset;
1162b843c749SSergey Zigachev 	union firmware_info *firmware_info;
1163b843c749SSergey Zigachev 
1164b843c749SSergey Zigachev 	*vddc = 0;
1165b843c749SSergey Zigachev 	*vddci = 0;
1166b843c749SSergey Zigachev 	*mvdd = 0;
1167b843c749SSergey Zigachev 
1168b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
1169b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
1170b843c749SSergey Zigachev 		firmware_info =
1171b843c749SSergey Zigachev 			(union firmware_info *)(mode_info->atom_context->bios +
1172b843c749SSergey Zigachev 						data_offset);
1173b843c749SSergey Zigachev 		*vddc = le16_to_cpu(firmware_info->info_14.usBootUpVDDCVoltage);
1174b843c749SSergey Zigachev 		if ((frev == 2) && (crev >= 2)) {
1175b843c749SSergey Zigachev 			*vddci = le16_to_cpu(firmware_info->info_22.usBootUpVDDCIVoltage);
1176b843c749SSergey Zigachev 			*mvdd = le16_to_cpu(firmware_info->info_22.usBootUpMVDDCVoltage);
1177b843c749SSergey Zigachev 		}
1178b843c749SSergey Zigachev 	}
1179b843c749SSergey Zigachev }
1180b843c749SSergey Zigachev 
1181b843c749SSergey Zigachev union set_voltage {
1182b843c749SSergey Zigachev 	struct _SET_VOLTAGE_PS_ALLOCATION alloc;
1183b843c749SSergey Zigachev 	struct _SET_VOLTAGE_PARAMETERS v1;
1184b843c749SSergey Zigachev 	struct _SET_VOLTAGE_PARAMETERS_V2 v2;
1185b843c749SSergey Zigachev 	struct _SET_VOLTAGE_PARAMETERS_V1_3 v3;
1186b843c749SSergey Zigachev };
1187b843c749SSergey Zigachev 
amdgpu_atombios_get_max_vddc(struct amdgpu_device * adev,u8 voltage_type,u16 voltage_id,u16 * voltage)1188b843c749SSergey Zigachev int amdgpu_atombios_get_max_vddc(struct amdgpu_device *adev, u8 voltage_type,
1189b843c749SSergey Zigachev 			     u16 voltage_id, u16 *voltage)
1190b843c749SSergey Zigachev {
1191b843c749SSergey Zigachev 	union set_voltage args;
1192b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
1193b843c749SSergey Zigachev 	u8 frev, crev;
1194b843c749SSergey Zigachev 
1195b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1196b843c749SSergey Zigachev 		return -EINVAL;
1197b843c749SSergey Zigachev 
1198b843c749SSergey Zigachev 	switch (crev) {
1199b843c749SSergey Zigachev 	case 1:
1200b843c749SSergey Zigachev 		return -EINVAL;
1201b843c749SSergey Zigachev 	case 2:
1202b843c749SSergey Zigachev 		args.v2.ucVoltageType = SET_VOLTAGE_GET_MAX_VOLTAGE;
1203b843c749SSergey Zigachev 		args.v2.ucVoltageMode = 0;
1204b843c749SSergey Zigachev 		args.v2.usVoltageLevel = 0;
1205b843c749SSergey Zigachev 
1206b843c749SSergey Zigachev 		amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1207b843c749SSergey Zigachev 
1208b843c749SSergey Zigachev 		*voltage = le16_to_cpu(args.v2.usVoltageLevel);
1209b843c749SSergey Zigachev 		break;
1210b843c749SSergey Zigachev 	case 3:
1211b843c749SSergey Zigachev 		args.v3.ucVoltageType = voltage_type;
1212b843c749SSergey Zigachev 		args.v3.ucVoltageMode = ATOM_GET_VOLTAGE_LEVEL;
1213b843c749SSergey Zigachev 		args.v3.usVoltageLevel = cpu_to_le16(voltage_id);
1214b843c749SSergey Zigachev 
1215b843c749SSergey Zigachev 		amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1216b843c749SSergey Zigachev 
1217b843c749SSergey Zigachev 		*voltage = le16_to_cpu(args.v3.usVoltageLevel);
1218b843c749SSergey Zigachev 		break;
1219b843c749SSergey Zigachev 	default:
1220b843c749SSergey Zigachev 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1221b843c749SSergey Zigachev 		return -EINVAL;
1222b843c749SSergey Zigachev 	}
1223b843c749SSergey Zigachev 
1224b843c749SSergey Zigachev 	return 0;
1225b843c749SSergey Zigachev }
1226b843c749SSergey Zigachev 
amdgpu_atombios_get_leakage_vddc_based_on_leakage_idx(struct amdgpu_device * adev,u16 * voltage,u16 leakage_idx)1227b843c749SSergey Zigachev int amdgpu_atombios_get_leakage_vddc_based_on_leakage_idx(struct amdgpu_device *adev,
1228b843c749SSergey Zigachev 						      u16 *voltage,
1229b843c749SSergey Zigachev 						      u16 leakage_idx)
1230b843c749SSergey Zigachev {
1231b843c749SSergey Zigachev 	return amdgpu_atombios_get_max_vddc(adev, VOLTAGE_TYPE_VDDC, leakage_idx, voltage);
1232b843c749SSergey Zigachev }
1233b843c749SSergey Zigachev 
amdgpu_atombios_get_leakage_id_from_vbios(struct amdgpu_device * adev,u16 * leakage_id)1234b843c749SSergey Zigachev int amdgpu_atombios_get_leakage_id_from_vbios(struct amdgpu_device *adev,
1235b843c749SSergey Zigachev 					      u16 *leakage_id)
1236b843c749SSergey Zigachev {
1237b843c749SSergey Zigachev 	union set_voltage args;
1238b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
1239b843c749SSergey Zigachev 	u8 frev, crev;
1240b843c749SSergey Zigachev 
1241b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1242b843c749SSergey Zigachev 		return -EINVAL;
1243b843c749SSergey Zigachev 
1244b843c749SSergey Zigachev 	switch (crev) {
1245b843c749SSergey Zigachev 	case 3:
1246b843c749SSergey Zigachev 	case 4:
1247b843c749SSergey Zigachev 		args.v3.ucVoltageType = 0;
1248b843c749SSergey Zigachev 		args.v3.ucVoltageMode = ATOM_GET_LEAKAGE_ID;
1249b843c749SSergey Zigachev 		args.v3.usVoltageLevel = 0;
1250b843c749SSergey Zigachev 
1251b843c749SSergey Zigachev 		amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1252b843c749SSergey Zigachev 
1253b843c749SSergey Zigachev 		*leakage_id = le16_to_cpu(args.v3.usVoltageLevel);
1254b843c749SSergey Zigachev 		break;
1255b843c749SSergey Zigachev 	default:
1256b843c749SSergey Zigachev 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1257b843c749SSergey Zigachev 		return -EINVAL;
1258b843c749SSergey Zigachev 	}
1259b843c749SSergey Zigachev 
1260b843c749SSergey Zigachev 	return 0;
1261b843c749SSergey Zigachev }
1262b843c749SSergey Zigachev 
amdgpu_atombios_get_leakage_vddc_based_on_leakage_params(struct amdgpu_device * adev,u16 * vddc,u16 * vddci,u16 virtual_voltage_id,u16 vbios_voltage_id)1263b843c749SSergey Zigachev int amdgpu_atombios_get_leakage_vddc_based_on_leakage_params(struct amdgpu_device *adev,
1264b843c749SSergey Zigachev 							     u16 *vddc, u16 *vddci,
1265b843c749SSergey Zigachev 							     u16 virtual_voltage_id,
1266b843c749SSergey Zigachev 							     u16 vbios_voltage_id)
1267b843c749SSergey Zigachev {
1268b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, ASIC_ProfilingInfo);
1269b843c749SSergey Zigachev 	u8 frev, crev;
1270b843c749SSergey Zigachev 	u16 data_offset, size;
1271b843c749SSergey Zigachev 	int i, j;
1272b843c749SSergey Zigachev 	ATOM_ASIC_PROFILING_INFO_V2_1 *profile;
1273b843c749SSergey Zigachev 	u16 *leakage_bin, *vddc_id_buf, *vddc_buf, *vddci_id_buf, *vddci_buf;
1274b843c749SSergey Zigachev 
1275b843c749SSergey Zigachev 	*vddc = 0;
1276b843c749SSergey Zigachev 	*vddci = 0;
1277b843c749SSergey Zigachev 
1278b843c749SSergey Zigachev 	if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1279b843c749SSergey Zigachev 				    &frev, &crev, &data_offset))
1280b843c749SSergey Zigachev 		return -EINVAL;
1281b843c749SSergey Zigachev 
1282b843c749SSergey Zigachev 	profile = (ATOM_ASIC_PROFILING_INFO_V2_1 *)
1283b843c749SSergey Zigachev 		(adev->mode_info.atom_context->bios + data_offset);
1284b843c749SSergey Zigachev 
1285b843c749SSergey Zigachev 	switch (frev) {
1286b843c749SSergey Zigachev 	case 1:
1287b843c749SSergey Zigachev 		return -EINVAL;
1288b843c749SSergey Zigachev 	case 2:
1289b843c749SSergey Zigachev 		switch (crev) {
1290b843c749SSergey Zigachev 		case 1:
1291b843c749SSergey Zigachev 			if (size < sizeof(ATOM_ASIC_PROFILING_INFO_V2_1))
1292b843c749SSergey Zigachev 				return -EINVAL;
1293b843c749SSergey Zigachev 			leakage_bin = (u16 *)
1294b843c749SSergey Zigachev 				(adev->mode_info.atom_context->bios + data_offset +
1295b843c749SSergey Zigachev 				 le16_to_cpu(profile->usLeakageBinArrayOffset));
1296b843c749SSergey Zigachev 			vddc_id_buf = (u16 *)
1297b843c749SSergey Zigachev 				(adev->mode_info.atom_context->bios + data_offset +
1298b843c749SSergey Zigachev 				 le16_to_cpu(profile->usElbVDDC_IdArrayOffset));
1299b843c749SSergey Zigachev 			vddc_buf = (u16 *)
1300b843c749SSergey Zigachev 				(adev->mode_info.atom_context->bios + data_offset +
1301b843c749SSergey Zigachev 				 le16_to_cpu(profile->usElbVDDC_LevelArrayOffset));
1302b843c749SSergey Zigachev 			vddci_id_buf = (u16 *)
1303b843c749SSergey Zigachev 				(adev->mode_info.atom_context->bios + data_offset +
1304b843c749SSergey Zigachev 				 le16_to_cpu(profile->usElbVDDCI_IdArrayOffset));
1305b843c749SSergey Zigachev 			vddci_buf = (u16 *)
1306b843c749SSergey Zigachev 				(adev->mode_info.atom_context->bios + data_offset +
1307b843c749SSergey Zigachev 				 le16_to_cpu(profile->usElbVDDCI_LevelArrayOffset));
1308b843c749SSergey Zigachev 
1309b843c749SSergey Zigachev 			if (profile->ucElbVDDC_Num > 0) {
1310b843c749SSergey Zigachev 				for (i = 0; i < profile->ucElbVDDC_Num; i++) {
1311b843c749SSergey Zigachev 					if (vddc_id_buf[i] == virtual_voltage_id) {
1312b843c749SSergey Zigachev 						for (j = 0; j < profile->ucLeakageBinNum; j++) {
1313b843c749SSergey Zigachev 							if (vbios_voltage_id <= leakage_bin[j]) {
1314b843c749SSergey Zigachev 								*vddc = vddc_buf[j * profile->ucElbVDDC_Num + i];
1315b843c749SSergey Zigachev 								break;
1316b843c749SSergey Zigachev 							}
1317b843c749SSergey Zigachev 						}
1318b843c749SSergey Zigachev 						break;
1319b843c749SSergey Zigachev 					}
1320b843c749SSergey Zigachev 				}
1321b843c749SSergey Zigachev 			}
1322b843c749SSergey Zigachev 			if (profile->ucElbVDDCI_Num > 0) {
1323b843c749SSergey Zigachev 				for (i = 0; i < profile->ucElbVDDCI_Num; i++) {
1324b843c749SSergey Zigachev 					if (vddci_id_buf[i] == virtual_voltage_id) {
1325b843c749SSergey Zigachev 						for (j = 0; j < profile->ucLeakageBinNum; j++) {
1326b843c749SSergey Zigachev 							if (vbios_voltage_id <= leakage_bin[j]) {
1327b843c749SSergey Zigachev 								*vddci = vddci_buf[j * profile->ucElbVDDCI_Num + i];
1328b843c749SSergey Zigachev 								break;
1329b843c749SSergey Zigachev 							}
1330b843c749SSergey Zigachev 						}
1331b843c749SSergey Zigachev 						break;
1332b843c749SSergey Zigachev 					}
1333b843c749SSergey Zigachev 				}
1334b843c749SSergey Zigachev 			}
1335b843c749SSergey Zigachev 			break;
1336b843c749SSergey Zigachev 		default:
1337b843c749SSergey Zigachev 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1338b843c749SSergey Zigachev 			return -EINVAL;
1339b843c749SSergey Zigachev 		}
1340b843c749SSergey Zigachev 		break;
1341b843c749SSergey Zigachev 	default:
1342b843c749SSergey Zigachev 		DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1343b843c749SSergey Zigachev 		return -EINVAL;
1344b843c749SSergey Zigachev 	}
1345b843c749SSergey Zigachev 
1346b843c749SSergey Zigachev 	return 0;
1347b843c749SSergey Zigachev }
1348b843c749SSergey Zigachev 
1349b843c749SSergey Zigachev union get_voltage_info {
1350b843c749SSergey Zigachev 	struct _GET_VOLTAGE_INFO_INPUT_PARAMETER_V1_2 in;
1351b843c749SSergey Zigachev 	struct _GET_EVV_VOLTAGE_INFO_OUTPUT_PARAMETER_V1_2 evv_out;
1352b843c749SSergey Zigachev };
1353b843c749SSergey Zigachev 
amdgpu_atombios_get_voltage_evv(struct amdgpu_device * adev,u16 virtual_voltage_id,u16 * voltage)1354b843c749SSergey Zigachev int amdgpu_atombios_get_voltage_evv(struct amdgpu_device *adev,
1355b843c749SSergey Zigachev 				    u16 virtual_voltage_id,
1356b843c749SSergey Zigachev 				    u16 *voltage)
1357b843c749SSergey Zigachev {
1358b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(COMMAND, GetVoltageInfo);
1359b843c749SSergey Zigachev 	u32 entry_id;
1360b843c749SSergey Zigachev 	u32 count = adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count;
1361b843c749SSergey Zigachev 	union get_voltage_info args;
1362b843c749SSergey Zigachev 
1363b843c749SSergey Zigachev 	for (entry_id = 0; entry_id < count; entry_id++) {
1364b843c749SSergey Zigachev 		if (adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].v ==
1365b843c749SSergey Zigachev 		    virtual_voltage_id)
1366b843c749SSergey Zigachev 			break;
1367b843c749SSergey Zigachev 	}
1368b843c749SSergey Zigachev 
1369b843c749SSergey Zigachev 	if (entry_id >= count)
1370b843c749SSergey Zigachev 		return -EINVAL;
1371b843c749SSergey Zigachev 
1372b843c749SSergey Zigachev 	args.in.ucVoltageType = VOLTAGE_TYPE_VDDC;
1373b843c749SSergey Zigachev 	args.in.ucVoltageMode = ATOM_GET_VOLTAGE_EVV_VOLTAGE;
1374b843c749SSergey Zigachev 	args.in.usVoltageLevel = cpu_to_le16(virtual_voltage_id);
1375b843c749SSergey Zigachev 	args.in.ulSCLKFreq =
1376b843c749SSergey Zigachev 		cpu_to_le32(adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].clk);
1377b843c749SSergey Zigachev 
1378b843c749SSergey Zigachev 	amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1379b843c749SSergey Zigachev 
1380b843c749SSergey Zigachev 	*voltage = le16_to_cpu(args.evv_out.usVoltageLevel);
1381b843c749SSergey Zigachev 
1382b843c749SSergey Zigachev 	return 0;
1383b843c749SSergey Zigachev }
1384b843c749SSergey Zigachev 
1385b843c749SSergey Zigachev union voltage_object_info {
1386b843c749SSergey Zigachev 	struct _ATOM_VOLTAGE_OBJECT_INFO v1;
1387b843c749SSergey Zigachev 	struct _ATOM_VOLTAGE_OBJECT_INFO_V2 v2;
1388b843c749SSergey Zigachev 	struct _ATOM_VOLTAGE_OBJECT_INFO_V3_1 v3;
1389b843c749SSergey Zigachev };
1390b843c749SSergey Zigachev 
1391b843c749SSergey Zigachev union voltage_object {
1392b843c749SSergey Zigachev 	struct _ATOM_VOLTAGE_OBJECT v1;
1393b843c749SSergey Zigachev 	struct _ATOM_VOLTAGE_OBJECT_V2 v2;
1394b843c749SSergey Zigachev 	union _ATOM_VOLTAGE_OBJECT_V3 v3;
1395b843c749SSergey Zigachev };
1396b843c749SSergey Zigachev 
1397b843c749SSergey Zigachev 
amdgpu_atombios_lookup_voltage_object_v3(ATOM_VOLTAGE_OBJECT_INFO_V3_1 * v3,u8 voltage_type,u8 voltage_mode)1398b843c749SSergey Zigachev static ATOM_VOLTAGE_OBJECT_V3 *amdgpu_atombios_lookup_voltage_object_v3(ATOM_VOLTAGE_OBJECT_INFO_V3_1 *v3,
1399b843c749SSergey Zigachev 									u8 voltage_type, u8 voltage_mode)
1400b843c749SSergey Zigachev {
1401b843c749SSergey Zigachev 	u32 size = le16_to_cpu(v3->sHeader.usStructureSize);
1402b843c749SSergey Zigachev 	u32 offset = offsetof(ATOM_VOLTAGE_OBJECT_INFO_V3_1, asVoltageObj[0]);
1403b843c749SSergey Zigachev 	u8 *start = (u8*)v3;
1404b843c749SSergey Zigachev 
1405b843c749SSergey Zigachev 	while (offset < size) {
1406b843c749SSergey Zigachev 		ATOM_VOLTAGE_OBJECT_V3 *vo = (ATOM_VOLTAGE_OBJECT_V3 *)(start + offset);
1407b843c749SSergey Zigachev 		if ((vo->asGpioVoltageObj.sHeader.ucVoltageType == voltage_type) &&
1408b843c749SSergey Zigachev 		    (vo->asGpioVoltageObj.sHeader.ucVoltageMode == voltage_mode))
1409b843c749SSergey Zigachev 			return vo;
1410b843c749SSergey Zigachev 		offset += le16_to_cpu(vo->asGpioVoltageObj.sHeader.usSize);
1411b843c749SSergey Zigachev 	}
1412b843c749SSergey Zigachev 	return NULL;
1413b843c749SSergey Zigachev }
1414b843c749SSergey Zigachev 
amdgpu_atombios_get_svi2_info(struct amdgpu_device * adev,u8 voltage_type,u8 * svd_gpio_id,u8 * svc_gpio_id)1415b843c749SSergey Zigachev int amdgpu_atombios_get_svi2_info(struct amdgpu_device *adev,
1416b843c749SSergey Zigachev 			      u8 voltage_type,
1417b843c749SSergey Zigachev 			      u8 *svd_gpio_id, u8 *svc_gpio_id)
1418b843c749SSergey Zigachev {
1419b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
1420b843c749SSergey Zigachev 	u8 frev, crev;
1421b843c749SSergey Zigachev 	u16 data_offset, size;
1422b843c749SSergey Zigachev 	union voltage_object_info *voltage_info;
1423b843c749SSergey Zigachev 	union voltage_object *voltage_object = NULL;
1424b843c749SSergey Zigachev 
1425b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1426b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
1427b843c749SSergey Zigachev 		voltage_info = (union voltage_object_info *)
1428b843c749SSergey Zigachev 			(adev->mode_info.atom_context->bios + data_offset);
1429b843c749SSergey Zigachev 
1430b843c749SSergey Zigachev 		switch (frev) {
1431b843c749SSergey Zigachev 		case 3:
1432b843c749SSergey Zigachev 			switch (crev) {
1433b843c749SSergey Zigachev 			case 1:
1434b843c749SSergey Zigachev 				voltage_object = (union voltage_object *)
1435b843c749SSergey Zigachev 					amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
1436b843c749SSergey Zigachev 								      voltage_type,
1437b843c749SSergey Zigachev 								      VOLTAGE_OBJ_SVID2);
1438b843c749SSergey Zigachev 				if (voltage_object) {
1439b843c749SSergey Zigachev 					*svd_gpio_id = voltage_object->v3.asSVID2Obj.ucSVDGpioId;
1440b843c749SSergey Zigachev 					*svc_gpio_id = voltage_object->v3.asSVID2Obj.ucSVCGpioId;
1441b843c749SSergey Zigachev 				} else {
1442b843c749SSergey Zigachev 					return -EINVAL;
1443b843c749SSergey Zigachev 				}
1444b843c749SSergey Zigachev 				break;
1445b843c749SSergey Zigachev 			default:
1446b843c749SSergey Zigachev 				DRM_ERROR("unknown voltage object table\n");
1447b843c749SSergey Zigachev 				return -EINVAL;
1448b843c749SSergey Zigachev 			}
1449b843c749SSergey Zigachev 			break;
1450b843c749SSergey Zigachev 		default:
1451b843c749SSergey Zigachev 			DRM_ERROR("unknown voltage object table\n");
1452b843c749SSergey Zigachev 			return -EINVAL;
1453b843c749SSergey Zigachev 		}
1454b843c749SSergey Zigachev 
1455b843c749SSergey Zigachev 	}
1456b843c749SSergey Zigachev 	return 0;
1457b843c749SSergey Zigachev }
1458b843c749SSergey Zigachev 
1459b843c749SSergey Zigachev bool
amdgpu_atombios_is_voltage_gpio(struct amdgpu_device * adev,u8 voltage_type,u8 voltage_mode)1460b843c749SSergey Zigachev amdgpu_atombios_is_voltage_gpio(struct amdgpu_device *adev,
1461b843c749SSergey Zigachev 				u8 voltage_type, u8 voltage_mode)
1462b843c749SSergey Zigachev {
1463b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
1464b843c749SSergey Zigachev 	u8 frev, crev;
1465b843c749SSergey Zigachev 	u16 data_offset, size;
1466b843c749SSergey Zigachev 	union voltage_object_info *voltage_info;
1467b843c749SSergey Zigachev 
1468b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1469b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
1470b843c749SSergey Zigachev 		voltage_info = (union voltage_object_info *)
1471b843c749SSergey Zigachev 			(adev->mode_info.atom_context->bios + data_offset);
1472b843c749SSergey Zigachev 
1473b843c749SSergey Zigachev 		switch (frev) {
1474b843c749SSergey Zigachev 		case 3:
1475b843c749SSergey Zigachev 			switch (crev) {
1476b843c749SSergey Zigachev 			case 1:
1477b843c749SSergey Zigachev 				if (amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
1478b843c749SSergey Zigachev 								  voltage_type, voltage_mode))
1479b843c749SSergey Zigachev 					return true;
1480b843c749SSergey Zigachev 				break;
1481b843c749SSergey Zigachev 			default:
1482b843c749SSergey Zigachev 				DRM_ERROR("unknown voltage object table\n");
1483b843c749SSergey Zigachev 				return false;
1484b843c749SSergey Zigachev 			}
1485b843c749SSergey Zigachev 			break;
1486b843c749SSergey Zigachev 		default:
1487b843c749SSergey Zigachev 			DRM_ERROR("unknown voltage object table\n");
1488b843c749SSergey Zigachev 			return false;
1489b843c749SSergey Zigachev 		}
1490b843c749SSergey Zigachev 
1491b843c749SSergey Zigachev 	}
1492b843c749SSergey Zigachev 	return false;
1493b843c749SSergey Zigachev }
1494b843c749SSergey Zigachev 
amdgpu_atombios_get_voltage_table(struct amdgpu_device * adev,u8 voltage_type,u8 voltage_mode,struct atom_voltage_table * voltage_table)1495b843c749SSergey Zigachev int amdgpu_atombios_get_voltage_table(struct amdgpu_device *adev,
1496b843c749SSergey Zigachev 				      u8 voltage_type, u8 voltage_mode,
1497b843c749SSergey Zigachev 				      struct atom_voltage_table *voltage_table)
1498b843c749SSergey Zigachev {
1499b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
1500b843c749SSergey Zigachev 	u8 frev, crev;
1501b843c749SSergey Zigachev 	u16 data_offset, size;
1502b843c749SSergey Zigachev 	int i;
1503b843c749SSergey Zigachev 	union voltage_object_info *voltage_info;
1504b843c749SSergey Zigachev 	union voltage_object *voltage_object = NULL;
1505b843c749SSergey Zigachev 
1506b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1507b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
1508b843c749SSergey Zigachev 		voltage_info = (union voltage_object_info *)
1509b843c749SSergey Zigachev 			(adev->mode_info.atom_context->bios + data_offset);
1510b843c749SSergey Zigachev 
1511b843c749SSergey Zigachev 		switch (frev) {
1512b843c749SSergey Zigachev 		case 3:
1513b843c749SSergey Zigachev 			switch (crev) {
1514b843c749SSergey Zigachev 			case 1:
1515b843c749SSergey Zigachev 				voltage_object = (union voltage_object *)
1516b843c749SSergey Zigachev 					amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
1517b843c749SSergey Zigachev 								      voltage_type, voltage_mode);
1518b843c749SSergey Zigachev 				if (voltage_object) {
1519b843c749SSergey Zigachev 					ATOM_GPIO_VOLTAGE_OBJECT_V3 *gpio =
1520b843c749SSergey Zigachev 						&voltage_object->v3.asGpioVoltageObj;
1521b843c749SSergey Zigachev 					VOLTAGE_LUT_ENTRY_V2 *lut;
1522b843c749SSergey Zigachev 					if (gpio->ucGpioEntryNum > MAX_VOLTAGE_ENTRIES)
1523b843c749SSergey Zigachev 						return -EINVAL;
1524b843c749SSergey Zigachev 					lut = &gpio->asVolGpioLut[0];
1525b843c749SSergey Zigachev 					for (i = 0; i < gpio->ucGpioEntryNum; i++) {
1526b843c749SSergey Zigachev 						voltage_table->entries[i].value =
1527b843c749SSergey Zigachev 							le16_to_cpu(lut->usVoltageValue);
1528b843c749SSergey Zigachev 						voltage_table->entries[i].smio_low =
1529b843c749SSergey Zigachev 							le32_to_cpu(lut->ulVoltageId);
1530b843c749SSergey Zigachev 						lut = (VOLTAGE_LUT_ENTRY_V2 *)
1531b843c749SSergey Zigachev 							((u8 *)lut + sizeof(VOLTAGE_LUT_ENTRY_V2));
1532b843c749SSergey Zigachev 					}
1533b843c749SSergey Zigachev 					voltage_table->mask_low = le32_to_cpu(gpio->ulGpioMaskVal);
1534b843c749SSergey Zigachev 					voltage_table->count = gpio->ucGpioEntryNum;
1535b843c749SSergey Zigachev 					voltage_table->phase_delay = gpio->ucPhaseDelay;
1536b843c749SSergey Zigachev 					return 0;
1537b843c749SSergey Zigachev 				}
1538b843c749SSergey Zigachev 				break;
1539b843c749SSergey Zigachev 			default:
1540b843c749SSergey Zigachev 				DRM_ERROR("unknown voltage object table\n");
1541b843c749SSergey Zigachev 				return -EINVAL;
1542b843c749SSergey Zigachev 			}
1543b843c749SSergey Zigachev 			break;
1544b843c749SSergey Zigachev 		default:
1545b843c749SSergey Zigachev 			DRM_ERROR("unknown voltage object table\n");
1546b843c749SSergey Zigachev 			return -EINVAL;
1547b843c749SSergey Zigachev 		}
1548b843c749SSergey Zigachev 	}
1549b843c749SSergey Zigachev 	return -EINVAL;
1550b843c749SSergey Zigachev }
1551b843c749SSergey Zigachev 
1552b843c749SSergey Zigachev union vram_info {
1553b843c749SSergey Zigachev 	struct _ATOM_VRAM_INFO_V3 v1_3;
1554b843c749SSergey Zigachev 	struct _ATOM_VRAM_INFO_V4 v1_4;
1555b843c749SSergey Zigachev 	struct _ATOM_VRAM_INFO_HEADER_V2_1 v2_1;
1556b843c749SSergey Zigachev };
1557b843c749SSergey Zigachev 
1558b843c749SSergey Zigachev #define MEM_ID_MASK           0xff000000
1559b843c749SSergey Zigachev #define MEM_ID_SHIFT          24
1560b843c749SSergey Zigachev #define CLOCK_RANGE_MASK      0x00ffffff
1561b843c749SSergey Zigachev #define CLOCK_RANGE_SHIFT     0
1562b843c749SSergey Zigachev #define LOW_NIBBLE_MASK       0xf
1563b843c749SSergey Zigachev #define DATA_EQU_PREV         0
1564b843c749SSergey Zigachev #define DATA_FROM_TABLE       4
1565b843c749SSergey Zigachev 
amdgpu_atombios_init_mc_reg_table(struct amdgpu_device * adev,u8 module_index,struct atom_mc_reg_table * reg_table)1566b843c749SSergey Zigachev int amdgpu_atombios_init_mc_reg_table(struct amdgpu_device *adev,
1567b843c749SSergey Zigachev 				      u8 module_index,
1568b843c749SSergey Zigachev 				      struct atom_mc_reg_table *reg_table)
1569b843c749SSergey Zigachev {
1570b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, VRAM_Info);
1571b843c749SSergey Zigachev 	u8 frev, crev, num_entries, t_mem_id, num_ranges = 0;
1572b843c749SSergey Zigachev 	u32 i = 0, j;
1573b843c749SSergey Zigachev 	u16 data_offset, size;
1574b843c749SSergey Zigachev 	union vram_info *vram_info;
1575b843c749SSergey Zigachev 
1576b843c749SSergey Zigachev 	memset(reg_table, 0, sizeof(struct atom_mc_reg_table));
1577b843c749SSergey Zigachev 
1578b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1579b843c749SSergey Zigachev 				   &frev, &crev, &data_offset)) {
1580b843c749SSergey Zigachev 		vram_info = (union vram_info *)
1581b843c749SSergey Zigachev 			(adev->mode_info.atom_context->bios + data_offset);
1582b843c749SSergey Zigachev 		switch (frev) {
1583b843c749SSergey Zigachev 		case 1:
1584b843c749SSergey Zigachev 			DRM_ERROR("old table version %d, %d\n", frev, crev);
1585b843c749SSergey Zigachev 			return -EINVAL;
1586b843c749SSergey Zigachev 		case 2:
1587b843c749SSergey Zigachev 			switch (crev) {
1588b843c749SSergey Zigachev 			case 1:
1589b843c749SSergey Zigachev 				if (module_index < vram_info->v2_1.ucNumOfVRAMModule) {
1590b843c749SSergey Zigachev 					ATOM_INIT_REG_BLOCK *reg_block =
1591b843c749SSergey Zigachev 						(ATOM_INIT_REG_BLOCK *)
1592b843c749SSergey Zigachev 						((u8 *)vram_info + le16_to_cpu(vram_info->v2_1.usMemClkPatchTblOffset));
1593b843c749SSergey Zigachev 					ATOM_MEMORY_SETTING_DATA_BLOCK *reg_data =
1594b843c749SSergey Zigachev 						(ATOM_MEMORY_SETTING_DATA_BLOCK *)
1595b843c749SSergey Zigachev 						((u8 *)reg_block + (2 * sizeof(u16)) +
1596b843c749SSergey Zigachev 						 le16_to_cpu(reg_block->usRegIndexTblSize));
1597b843c749SSergey Zigachev 					ATOM_INIT_REG_INDEX_FORMAT *format = &reg_block->asRegIndexBuf[0];
1598b843c749SSergey Zigachev 					num_entries = (u8)((le16_to_cpu(reg_block->usRegIndexTblSize)) /
1599b843c749SSergey Zigachev 							   sizeof(ATOM_INIT_REG_INDEX_FORMAT)) - 1;
1600b843c749SSergey Zigachev 					if (num_entries > VBIOS_MC_REGISTER_ARRAY_SIZE)
1601b843c749SSergey Zigachev 						return -EINVAL;
1602b843c749SSergey Zigachev 					while (i < num_entries) {
1603b843c749SSergey Zigachev 						if (format->ucPreRegDataLength & ACCESS_PLACEHOLDER)
1604b843c749SSergey Zigachev 							break;
1605b843c749SSergey Zigachev 						reg_table->mc_reg_address[i].s1 =
1606b843c749SSergey Zigachev 							(u16)(le16_to_cpu(format->usRegIndex));
1607b843c749SSergey Zigachev 						reg_table->mc_reg_address[i].pre_reg_data =
1608b843c749SSergey Zigachev 							(u8)(format->ucPreRegDataLength);
1609b843c749SSergey Zigachev 						i++;
1610b843c749SSergey Zigachev 						format = (ATOM_INIT_REG_INDEX_FORMAT *)
1611b843c749SSergey Zigachev 							((u8 *)format + sizeof(ATOM_INIT_REG_INDEX_FORMAT));
1612b843c749SSergey Zigachev 					}
1613b843c749SSergey Zigachev 					reg_table->last = i;
1614b843c749SSergey Zigachev 					while ((le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK) &&
1615b843c749SSergey Zigachev 					       (num_ranges < VBIOS_MAX_AC_TIMING_ENTRIES)) {
1616b843c749SSergey Zigachev 						t_mem_id = (u8)((le32_to_cpu(*(u32 *)reg_data) & MEM_ID_MASK)
1617b843c749SSergey Zigachev 								>> MEM_ID_SHIFT);
1618b843c749SSergey Zigachev 						if (module_index == t_mem_id) {
1619b843c749SSergey Zigachev 							reg_table->mc_reg_table_entry[num_ranges].mclk_max =
1620b843c749SSergey Zigachev 								(u32)((le32_to_cpu(*(u32 *)reg_data) & CLOCK_RANGE_MASK)
1621b843c749SSergey Zigachev 								      >> CLOCK_RANGE_SHIFT);
1622b843c749SSergey Zigachev 							for (i = 0, j = 1; i < reg_table->last; i++) {
1623b843c749SSergey Zigachev 								if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_FROM_TABLE) {
1624b843c749SSergey Zigachev 									reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
1625b843c749SSergey Zigachev 										(u32)le32_to_cpu(*((u32 *)reg_data + j));
1626b843c749SSergey Zigachev 									j++;
1627b843c749SSergey Zigachev 								} else if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_EQU_PREV) {
1628b843c749SSergey Zigachev 									reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
1629b843c749SSergey Zigachev 										reg_table->mc_reg_table_entry[num_ranges].mc_data[i - 1];
1630b843c749SSergey Zigachev 								}
1631b843c749SSergey Zigachev 							}
1632b843c749SSergey Zigachev 							num_ranges++;
1633b843c749SSergey Zigachev 						}
1634b843c749SSergey Zigachev 						reg_data = (ATOM_MEMORY_SETTING_DATA_BLOCK *)
1635b843c749SSergey Zigachev 							((u8 *)reg_data + le16_to_cpu(reg_block->usRegDataBlkSize));
1636b843c749SSergey Zigachev 					}
1637b843c749SSergey Zigachev 					if (le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK)
1638b843c749SSergey Zigachev 						return -EINVAL;
1639b843c749SSergey Zigachev 					reg_table->num_entries = num_ranges;
1640b843c749SSergey Zigachev 				} else
1641b843c749SSergey Zigachev 					return -EINVAL;
1642b843c749SSergey Zigachev 				break;
1643b843c749SSergey Zigachev 			default:
1644b843c749SSergey Zigachev 				DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1645b843c749SSergey Zigachev 				return -EINVAL;
1646b843c749SSergey Zigachev 			}
1647b843c749SSergey Zigachev 			break;
1648b843c749SSergey Zigachev 		default:
1649b843c749SSergey Zigachev 			DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1650b843c749SSergey Zigachev 			return -EINVAL;
1651b843c749SSergey Zigachev 		}
1652b843c749SSergey Zigachev 		return 0;
1653b843c749SSergey Zigachev 	}
1654b843c749SSergey Zigachev 	return -EINVAL;
1655b843c749SSergey Zigachev }
1656b843c749SSergey Zigachev 
amdgpu_atombios_has_gpu_virtualization_table(struct amdgpu_device * adev)1657b843c749SSergey Zigachev bool amdgpu_atombios_has_gpu_virtualization_table(struct amdgpu_device *adev)
1658b843c749SSergey Zigachev {
1659b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, GPUVirtualizationInfo);
1660b843c749SSergey Zigachev 	u8 frev, crev;
1661b843c749SSergey Zigachev 	u16 data_offset, size;
1662b843c749SSergey Zigachev 
1663b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1664b843c749SSergey Zigachev 					  &frev, &crev, &data_offset))
1665b843c749SSergey Zigachev 		return true;
1666b843c749SSergey Zigachev 
1667b843c749SSergey Zigachev 	return false;
1668b843c749SSergey Zigachev }
1669b843c749SSergey Zigachev 
amdgpu_atombios_scratch_regs_lock(struct amdgpu_device * adev,bool lock)1670b843c749SSergey Zigachev void amdgpu_atombios_scratch_regs_lock(struct amdgpu_device *adev, bool lock)
1671b843c749SSergey Zigachev {
1672b843c749SSergey Zigachev 	uint32_t bios_6_scratch;
1673b843c749SSergey Zigachev 
1674b843c749SSergey Zigachev 	bios_6_scratch = RREG32(adev->bios_scratch_reg_offset + 6);
1675b843c749SSergey Zigachev 
1676b843c749SSergey Zigachev 	if (lock) {
1677b843c749SSergey Zigachev 		bios_6_scratch |= ATOM_S6_CRITICAL_STATE;
1678b843c749SSergey Zigachev 		bios_6_scratch &= ~ATOM_S6_ACC_MODE;
1679b843c749SSergey Zigachev 	} else {
1680b843c749SSergey Zigachev 		bios_6_scratch &= ~ATOM_S6_CRITICAL_STATE;
1681b843c749SSergey Zigachev 		bios_6_scratch |= ATOM_S6_ACC_MODE;
1682b843c749SSergey Zigachev 	}
1683b843c749SSergey Zigachev 
1684b843c749SSergey Zigachev 	WREG32(adev->bios_scratch_reg_offset + 6, bios_6_scratch);
1685b843c749SSergey Zigachev }
1686b843c749SSergey Zigachev 
amdgpu_atombios_scratch_regs_init(struct amdgpu_device * adev)1687b843c749SSergey Zigachev static void amdgpu_atombios_scratch_regs_init(struct amdgpu_device *adev)
1688b843c749SSergey Zigachev {
1689b843c749SSergey Zigachev 	uint32_t bios_2_scratch, bios_6_scratch;
1690b843c749SSergey Zigachev 
1691b843c749SSergey Zigachev 	adev->bios_scratch_reg_offset = mmBIOS_SCRATCH_0;
1692b843c749SSergey Zigachev 
1693b843c749SSergey Zigachev 	bios_2_scratch = RREG32(adev->bios_scratch_reg_offset + 2);
1694b843c749SSergey Zigachev 	bios_6_scratch = RREG32(adev->bios_scratch_reg_offset + 6);
1695b843c749SSergey Zigachev 
1696b843c749SSergey Zigachev 	/* let the bios control the backlight */
1697b843c749SSergey Zigachev 	bios_2_scratch &= ~ATOM_S2_VRI_BRIGHT_ENABLE;
1698b843c749SSergey Zigachev 
1699b843c749SSergey Zigachev 	/* tell the bios not to handle mode switching */
1700b843c749SSergey Zigachev 	bios_6_scratch |= ATOM_S6_ACC_BLOCK_DISPLAY_SWITCH;
1701b843c749SSergey Zigachev 
1702b843c749SSergey Zigachev 	/* clear the vbios dpms state */
1703b843c749SSergey Zigachev 	bios_2_scratch &= ~ATOM_S2_DEVICE_DPMS_STATE;
1704b843c749SSergey Zigachev 
1705b843c749SSergey Zigachev 	WREG32(adev->bios_scratch_reg_offset + 2, bios_2_scratch);
1706b843c749SSergey Zigachev 	WREG32(adev->bios_scratch_reg_offset + 6, bios_6_scratch);
1707b843c749SSergey Zigachev }
1708b843c749SSergey Zigachev 
amdgpu_atombios_scratch_regs_engine_hung(struct amdgpu_device * adev,bool hung)1709b843c749SSergey Zigachev void amdgpu_atombios_scratch_regs_engine_hung(struct amdgpu_device *adev,
1710b843c749SSergey Zigachev 					      bool hung)
1711b843c749SSergey Zigachev {
1712b843c749SSergey Zigachev 	u32 tmp = RREG32(adev->bios_scratch_reg_offset + 3);
1713b843c749SSergey Zigachev 
1714b843c749SSergey Zigachev 	if (hung)
1715b843c749SSergey Zigachev 		tmp |= ATOM_S3_ASIC_GUI_ENGINE_HUNG;
1716b843c749SSergey Zigachev 	else
1717b843c749SSergey Zigachev 		tmp &= ~ATOM_S3_ASIC_GUI_ENGINE_HUNG;
1718b843c749SSergey Zigachev 
1719b843c749SSergey Zigachev 	WREG32(adev->bios_scratch_reg_offset + 3, tmp);
1720b843c749SSergey Zigachev }
1721b843c749SSergey Zigachev 
amdgpu_atombios_scratch_need_asic_init(struct amdgpu_device * adev)1722b843c749SSergey Zigachev bool amdgpu_atombios_scratch_need_asic_init(struct amdgpu_device *adev)
1723b843c749SSergey Zigachev {
1724b843c749SSergey Zigachev 	u32 tmp = RREG32(adev->bios_scratch_reg_offset + 7);
1725b843c749SSergey Zigachev 
1726b843c749SSergey Zigachev 	if (tmp & ATOM_S7_ASIC_INIT_COMPLETE_MASK)
1727b843c749SSergey Zigachev 		return false;
1728b843c749SSergey Zigachev 	else
1729b843c749SSergey Zigachev 		return true;
1730b843c749SSergey Zigachev }
1731b843c749SSergey Zigachev 
1732b843c749SSergey Zigachev /* Atom needs data in little endian format so swap as appropriate when copying
1733b843c749SSergey Zigachev  * data to or from atom. Note that atom operates on dw units.
1734b843c749SSergey Zigachev  *
1735b843c749SSergey Zigachev  * Use to_le=true when sending data to atom and provide at least
1736b843c749SSergey Zigachev  * ALIGN(num_bytes,4) bytes in the dst buffer.
1737b843c749SSergey Zigachev  *
1738b843c749SSergey Zigachev  * Use to_le=false when receiving data from atom and provide ALIGN(num_bytes,4)
1739b843c749SSergey Zigachev  * byes in the src buffer.
1740b843c749SSergey Zigachev  */
amdgpu_atombios_copy_swap(u8 * dst,u8 * src,u8 num_bytes,bool to_le)1741b843c749SSergey Zigachev void amdgpu_atombios_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
1742b843c749SSergey Zigachev {
1743b843c749SSergey Zigachev #ifdef __BIG_ENDIAN
1744b843c749SSergey Zigachev 	u32 src_tmp[5], dst_tmp[5];
1745b843c749SSergey Zigachev 	int i;
1746b843c749SSergey Zigachev 	u8 align_num_bytes = ALIGN(num_bytes, 4);
1747b843c749SSergey Zigachev 
1748b843c749SSergey Zigachev 	if (to_le) {
1749b843c749SSergey Zigachev 		memcpy(src_tmp, src, num_bytes);
1750b843c749SSergey Zigachev 		for (i = 0; i < align_num_bytes / 4; i++)
1751b843c749SSergey Zigachev 			dst_tmp[i] = cpu_to_le32(src_tmp[i]);
1752b843c749SSergey Zigachev 		memcpy(dst, dst_tmp, align_num_bytes);
1753b843c749SSergey Zigachev 	} else {
1754b843c749SSergey Zigachev 		memcpy(src_tmp, src, align_num_bytes);
1755b843c749SSergey Zigachev 		for (i = 0; i < align_num_bytes / 4; i++)
1756b843c749SSergey Zigachev 			dst_tmp[i] = le32_to_cpu(src_tmp[i]);
1757b843c749SSergey Zigachev 		memcpy(dst, dst_tmp, num_bytes);
1758b843c749SSergey Zigachev 	}
1759b843c749SSergey Zigachev #else
1760b843c749SSergey Zigachev 	memcpy(dst, src, num_bytes);
1761b843c749SSergey Zigachev #endif
1762b843c749SSergey Zigachev }
1763b843c749SSergey Zigachev 
amdgpu_atombios_allocate_fb_scratch(struct amdgpu_device * adev)1764b843c749SSergey Zigachev static int amdgpu_atombios_allocate_fb_scratch(struct amdgpu_device *adev)
1765b843c749SSergey Zigachev {
1766b843c749SSergey Zigachev 	struct atom_context *ctx = adev->mode_info.atom_context;
1767b843c749SSergey Zigachev 	int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
1768b843c749SSergey Zigachev 	uint16_t data_offset;
1769b843c749SSergey Zigachev 	int usage_bytes = 0;
1770b843c749SSergey Zigachev 	struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
1771b843c749SSergey Zigachev 	u64 start_addr;
1772b843c749SSergey Zigachev 	u64 size;
1773b843c749SSergey Zigachev 
1774b843c749SSergey Zigachev 	if (amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
1775b843c749SSergey Zigachev 		firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)(ctx->bios + data_offset);
1776b843c749SSergey Zigachev 
1777b843c749SSergey Zigachev 		DRM_DEBUG("atom firmware requested %08x %dkb\n",
1778b843c749SSergey Zigachev 			  le32_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware),
1779b843c749SSergey Zigachev 			  le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb));
1780b843c749SSergey Zigachev 
1781b843c749SSergey Zigachev 		start_addr = firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware;
1782b843c749SSergey Zigachev 		size = firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb;
1783b843c749SSergey Zigachev 
1784b843c749SSergey Zigachev 		if ((uint32_t)(start_addr & ATOM_VRAM_OPERATION_FLAGS_MASK) ==
1785b843c749SSergey Zigachev 			(uint32_t)(ATOM_VRAM_BLOCK_SRIOV_MSG_SHARE_RESERVATION <<
1786b843c749SSergey Zigachev 			ATOM_VRAM_OPERATION_FLAGS_SHIFT)) {
1787b843c749SSergey Zigachev 			/* Firmware request VRAM reservation for SR-IOV */
1788b843c749SSergey Zigachev 			adev->fw_vram_usage.start_offset = (start_addr &
1789b843c749SSergey Zigachev 				(~ATOM_VRAM_OPERATION_FLAGS_MASK)) << 10;
1790b843c749SSergey Zigachev 			adev->fw_vram_usage.size = size << 10;
1791b843c749SSergey Zigachev 			/* Use the default scratch size */
1792b843c749SSergey Zigachev 			usage_bytes = 0;
1793b843c749SSergey Zigachev 		} else {
1794b843c749SSergey Zigachev 			usage_bytes = le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb) * 1024;
1795b843c749SSergey Zigachev 		}
1796b843c749SSergey Zigachev 	}
1797b843c749SSergey Zigachev 	ctx->scratch_size_bytes = 0;
1798b843c749SSergey Zigachev 	if (usage_bytes == 0)
1799b843c749SSergey Zigachev 		usage_bytes = 20 * 1024;
1800b843c749SSergey Zigachev 	/* allocate some scratch memory */
1801b843c749SSergey Zigachev 	ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
1802b843c749SSergey Zigachev 	if (!ctx->scratch)
1803b843c749SSergey Zigachev 		return -ENOMEM;
1804b843c749SSergey Zigachev 	ctx->scratch_size_bytes = usage_bytes;
1805b843c749SSergey Zigachev 	return 0;
1806b843c749SSergey Zigachev }
1807b843c749SSergey Zigachev 
1808b843c749SSergey Zigachev /* ATOM accessor methods */
1809b843c749SSergey Zigachev /*
1810b843c749SSergey Zigachev  * ATOM is an interpreted byte code stored in tables in the vbios.  The
1811b843c749SSergey Zigachev  * driver registers callbacks to access registers and the interpreter
1812b843c749SSergey Zigachev  * in the driver parses the tables and executes then to program specific
1813b843c749SSergey Zigachev  * actions (set display modes, asic init, etc.).  See amdgpu_atombios.c,
1814b843c749SSergey Zigachev  * atombios.h, and atom.c
1815b843c749SSergey Zigachev  */
1816b843c749SSergey Zigachev 
1817b843c749SSergey Zigachev /**
1818b843c749SSergey Zigachev  * cail_pll_read - read PLL register
1819b843c749SSergey Zigachev  *
1820b843c749SSergey Zigachev  * @info: atom card_info pointer
1821b843c749SSergey Zigachev  * @reg: PLL register offset
1822b843c749SSergey Zigachev  *
1823b843c749SSergey Zigachev  * Provides a PLL register accessor for the atom interpreter (r4xx+).
1824b843c749SSergey Zigachev  * Returns the value of the PLL register.
1825b843c749SSergey Zigachev  */
cail_pll_read(struct card_info * info,uint32_t reg)1826b843c749SSergey Zigachev static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
1827b843c749SSergey Zigachev {
1828b843c749SSergey Zigachev 	return 0;
1829b843c749SSergey Zigachev }
1830b843c749SSergey Zigachev 
1831b843c749SSergey Zigachev /**
1832b843c749SSergey Zigachev  * cail_pll_write - write PLL register
1833b843c749SSergey Zigachev  *
1834b843c749SSergey Zigachev  * @info: atom card_info pointer
1835b843c749SSergey Zigachev  * @reg: PLL register offset
1836b843c749SSergey Zigachev  * @val: value to write to the pll register
1837b843c749SSergey Zigachev  *
1838b843c749SSergey Zigachev  * Provides a PLL register accessor for the atom interpreter (r4xx+).
1839b843c749SSergey Zigachev  */
cail_pll_write(struct card_info * info,uint32_t reg,uint32_t val)1840b843c749SSergey Zigachev static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
1841b843c749SSergey Zigachev {
1842b843c749SSergey Zigachev 
1843b843c749SSergey Zigachev }
1844b843c749SSergey Zigachev 
1845b843c749SSergey Zigachev /**
1846b843c749SSergey Zigachev  * cail_mc_read - read MC (Memory Controller) register
1847b843c749SSergey Zigachev  *
1848b843c749SSergey Zigachev  * @info: atom card_info pointer
1849b843c749SSergey Zigachev  * @reg: MC register offset
1850b843c749SSergey Zigachev  *
1851b843c749SSergey Zigachev  * Provides an MC register accessor for the atom interpreter (r4xx+).
1852b843c749SSergey Zigachev  * Returns the value of the MC register.
1853b843c749SSergey Zigachev  */
cail_mc_read(struct card_info * info,uint32_t reg)1854b843c749SSergey Zigachev static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
1855b843c749SSergey Zigachev {
1856b843c749SSergey Zigachev 	return 0;
1857b843c749SSergey Zigachev }
1858b843c749SSergey Zigachev 
1859b843c749SSergey Zigachev /**
1860b843c749SSergey Zigachev  * cail_mc_write - write MC (Memory Controller) register
1861b843c749SSergey Zigachev  *
1862b843c749SSergey Zigachev  * @info: atom card_info pointer
1863b843c749SSergey Zigachev  * @reg: MC register offset
1864b843c749SSergey Zigachev  * @val: value to write to the pll register
1865b843c749SSergey Zigachev  *
1866b843c749SSergey Zigachev  * Provides a MC register accessor for the atom interpreter (r4xx+).
1867b843c749SSergey Zigachev  */
cail_mc_write(struct card_info * info,uint32_t reg,uint32_t val)1868b843c749SSergey Zigachev static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
1869b843c749SSergey Zigachev {
1870b843c749SSergey Zigachev 
1871b843c749SSergey Zigachev }
1872b843c749SSergey Zigachev 
1873b843c749SSergey Zigachev /**
1874b843c749SSergey Zigachev  * cail_reg_write - write MMIO register
1875b843c749SSergey Zigachev  *
1876b843c749SSergey Zigachev  * @info: atom card_info pointer
1877b843c749SSergey Zigachev  * @reg: MMIO register offset
1878b843c749SSergey Zigachev  * @val: value to write to the pll register
1879b843c749SSergey Zigachev  *
1880b843c749SSergey Zigachev  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
1881b843c749SSergey Zigachev  */
cail_reg_write(struct card_info * info,uint32_t reg,uint32_t val)1882b843c749SSergey Zigachev static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
1883b843c749SSergey Zigachev {
1884b843c749SSergey Zigachev 	struct amdgpu_device *adev = info->dev->dev_private;
1885b843c749SSergey Zigachev 
1886b843c749SSergey Zigachev 	WREG32(reg, val);
1887b843c749SSergey Zigachev }
1888b843c749SSergey Zigachev 
1889b843c749SSergey Zigachev /**
1890b843c749SSergey Zigachev  * cail_reg_read - read MMIO register
1891b843c749SSergey Zigachev  *
1892b843c749SSergey Zigachev  * @info: atom card_info pointer
1893b843c749SSergey Zigachev  * @reg: MMIO register offset
1894b843c749SSergey Zigachev  *
1895b843c749SSergey Zigachev  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
1896b843c749SSergey Zigachev  * Returns the value of the MMIO register.
1897b843c749SSergey Zigachev  */
cail_reg_read(struct card_info * info,uint32_t reg)1898b843c749SSergey Zigachev static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
1899b843c749SSergey Zigachev {
1900b843c749SSergey Zigachev 	struct amdgpu_device *adev = info->dev->dev_private;
1901b843c749SSergey Zigachev 	uint32_t r;
1902b843c749SSergey Zigachev 
1903b843c749SSergey Zigachev 	r = RREG32(reg);
1904b843c749SSergey Zigachev 	return r;
1905b843c749SSergey Zigachev }
1906b843c749SSergey Zigachev 
1907b843c749SSergey Zigachev /**
1908b843c749SSergey Zigachev  * cail_ioreg_write - write IO register
1909b843c749SSergey Zigachev  *
1910b843c749SSergey Zigachev  * @info: atom card_info pointer
1911b843c749SSergey Zigachev  * @reg: IO register offset
1912b843c749SSergey Zigachev  * @val: value to write to the pll register
1913b843c749SSergey Zigachev  *
1914b843c749SSergey Zigachev  * Provides a IO register accessor for the atom interpreter (r4xx+).
1915b843c749SSergey Zigachev  */
cail_ioreg_write(struct card_info * info,uint32_t reg,uint32_t val)1916b843c749SSergey Zigachev static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
1917b843c749SSergey Zigachev {
1918b843c749SSergey Zigachev 	struct amdgpu_device *adev = info->dev->dev_private;
1919b843c749SSergey Zigachev 
1920b843c749SSergey Zigachev 	WREG32_IO(reg, val);
1921b843c749SSergey Zigachev }
1922b843c749SSergey Zigachev 
1923b843c749SSergey Zigachev /**
1924b843c749SSergey Zigachev  * cail_ioreg_read - read IO register
1925b843c749SSergey Zigachev  *
1926b843c749SSergey Zigachev  * @info: atom card_info pointer
1927b843c749SSergey Zigachev  * @reg: IO register offset
1928b843c749SSergey Zigachev  *
1929b843c749SSergey Zigachev  * Provides an IO register accessor for the atom interpreter (r4xx+).
1930b843c749SSergey Zigachev  * Returns the value of the IO register.
1931b843c749SSergey Zigachev  */
cail_ioreg_read(struct card_info * info,uint32_t reg)1932b843c749SSergey Zigachev static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
1933b843c749SSergey Zigachev {
1934b843c749SSergey Zigachev 	struct amdgpu_device *adev = info->dev->dev_private;
1935b843c749SSergey Zigachev 	uint32_t r;
1936b843c749SSergey Zigachev 
1937b843c749SSergey Zigachev 	r = RREG32_IO(reg);
1938b843c749SSergey Zigachev 	return r;
1939b843c749SSergey Zigachev }
1940b843c749SSergey Zigachev 
1941*78973132SSergey Zigachev #if 0
1942b843c749SSergey Zigachev static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
1943b843c749SSergey Zigachev 						 struct device_attribute *attr,
1944b843c749SSergey Zigachev 						 char *buf)
1945b843c749SSergey Zigachev {
1946b843c749SSergey Zigachev 	struct drm_device *ddev = dev_get_drvdata(dev);
1947b843c749SSergey Zigachev 	struct amdgpu_device *adev = ddev->dev_private;
1948b843c749SSergey Zigachev 	struct atom_context *ctx = adev->mode_info.atom_context;
1949b843c749SSergey Zigachev 
1950b843c749SSergey Zigachev 	return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
1951b843c749SSergey Zigachev }
1952b843c749SSergey Zigachev 
1953b843c749SSergey Zigachev static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
1954b843c749SSergey Zigachev 		   NULL);
1955*78973132SSergey Zigachev #endif
1956b843c749SSergey Zigachev 
1957b843c749SSergey Zigachev /**
1958b843c749SSergey Zigachev  * amdgpu_atombios_fini - free the driver info and callbacks for atombios
1959b843c749SSergey Zigachev  *
1960b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1961b843c749SSergey Zigachev  *
1962b843c749SSergey Zigachev  * Frees the driver info and register access callbacks for the ATOM
1963b843c749SSergey Zigachev  * interpreter (r4xx+).
1964b843c749SSergey Zigachev  * Called at driver shutdown.
1965b843c749SSergey Zigachev  */
amdgpu_atombios_fini(struct amdgpu_device * adev)1966b843c749SSergey Zigachev void amdgpu_atombios_fini(struct amdgpu_device *adev)
1967b843c749SSergey Zigachev {
1968b843c749SSergey Zigachev 	if (adev->mode_info.atom_context) {
1969b843c749SSergey Zigachev 		kfree(adev->mode_info.atom_context->scratch);
1970b843c749SSergey Zigachev 		kfree(adev->mode_info.atom_context->iio);
1971b843c749SSergey Zigachev 	}
1972b843c749SSergey Zigachev 	kfree(adev->mode_info.atom_context);
1973b843c749SSergey Zigachev 	adev->mode_info.atom_context = NULL;
1974b843c749SSergey Zigachev 	kfree(adev->mode_info.atom_card_info);
1975b843c749SSergey Zigachev 	adev->mode_info.atom_card_info = NULL;
1976*78973132SSergey Zigachev #if 0
1977b843c749SSergey Zigachev 	device_remove_file(adev->dev, &dev_attr_vbios_version);
1978*78973132SSergey Zigachev #endif
1979b843c749SSergey Zigachev }
1980b843c749SSergey Zigachev 
1981b843c749SSergey Zigachev /**
1982b843c749SSergey Zigachev  * amdgpu_atombios_init - init the driver info and callbacks for atombios
1983b843c749SSergey Zigachev  *
1984b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1985b843c749SSergey Zigachev  *
1986b843c749SSergey Zigachev  * Initializes the driver info and register access callbacks for the
1987b843c749SSergey Zigachev  * ATOM interpreter (r4xx+).
1988b843c749SSergey Zigachev  * Returns 0 on sucess, -ENOMEM on failure.
1989b843c749SSergey Zigachev  * Called at driver startup.
1990b843c749SSergey Zigachev  */
amdgpu_atombios_init(struct amdgpu_device * adev)1991b843c749SSergey Zigachev int amdgpu_atombios_init(struct amdgpu_device *adev)
1992b843c749SSergey Zigachev {
1993b843c749SSergey Zigachev 	struct card_info *atom_card_info =
1994b843c749SSergey Zigachev 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
1995*78973132SSergey Zigachev #if 0
1996b843c749SSergey Zigachev 	int ret;
1997*78973132SSergey Zigachev #endif
1998b843c749SSergey Zigachev 
1999b843c749SSergey Zigachev 	if (!atom_card_info)
2000b843c749SSergey Zigachev 		return -ENOMEM;
2001b843c749SSergey Zigachev 
2002b843c749SSergey Zigachev 	adev->mode_info.atom_card_info = atom_card_info;
2003b843c749SSergey Zigachev 	atom_card_info->dev = adev->ddev;
2004b843c749SSergey Zigachev 	atom_card_info->reg_read = cail_reg_read;
2005b843c749SSergey Zigachev 	atom_card_info->reg_write = cail_reg_write;
2006b843c749SSergey Zigachev 	/* needed for iio ops */
2007b843c749SSergey Zigachev 	if (adev->rio_mem) {
2008b843c749SSergey Zigachev 		atom_card_info->ioreg_read = cail_ioreg_read;
2009b843c749SSergey Zigachev 		atom_card_info->ioreg_write = cail_ioreg_write;
2010b843c749SSergey Zigachev 	} else {
2011b843c749SSergey Zigachev 		DRM_DEBUG("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
2012b843c749SSergey Zigachev 		atom_card_info->ioreg_read = cail_reg_read;
2013b843c749SSergey Zigachev 		atom_card_info->ioreg_write = cail_reg_write;
2014b843c749SSergey Zigachev 	}
2015b843c749SSergey Zigachev 	atom_card_info->mc_read = cail_mc_read;
2016b843c749SSergey Zigachev 	atom_card_info->mc_write = cail_mc_write;
2017b843c749SSergey Zigachev 	atom_card_info->pll_read = cail_pll_read;
2018b843c749SSergey Zigachev 	atom_card_info->pll_write = cail_pll_write;
2019b843c749SSergey Zigachev 
2020b843c749SSergey Zigachev 	adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
2021b843c749SSergey Zigachev 	if (!adev->mode_info.atom_context) {
2022b843c749SSergey Zigachev 		amdgpu_atombios_fini(adev);
2023b843c749SSergey Zigachev 		return -ENOMEM;
2024b843c749SSergey Zigachev 	}
2025b843c749SSergey Zigachev 
2026*78973132SSergey Zigachev 	lockinit(&adev->mode_info.atom_context->mutex, "agmiacm", 0, LK_CANRECURSE);
2027b843c749SSergey Zigachev 	if (adev->is_atom_fw) {
2028b843c749SSergey Zigachev 		amdgpu_atomfirmware_scratch_regs_init(adev);
2029b843c749SSergey Zigachev 		amdgpu_atomfirmware_allocate_fb_scratch(adev);
2030b843c749SSergey Zigachev 	} else {
2031b843c749SSergey Zigachev 		amdgpu_atombios_scratch_regs_init(adev);
2032b843c749SSergey Zigachev 		amdgpu_atombios_allocate_fb_scratch(adev);
2033b843c749SSergey Zigachev 	}
2034b843c749SSergey Zigachev 
2035*78973132SSergey Zigachev #if 0
2036b843c749SSergey Zigachev 	ret = device_create_file(adev->dev, &dev_attr_vbios_version);
2037b843c749SSergey Zigachev 	if (ret) {
2038b843c749SSergey Zigachev 		DRM_ERROR("Failed to create device file for VBIOS version\n");
2039b843c749SSergey Zigachev 		return ret;
2040b843c749SSergey Zigachev 	}
2041*78973132SSergey Zigachev #endif
2042b843c749SSergey Zigachev 
2043b843c749SSergey Zigachev 	return 0;
2044b843c749SSergey Zigachev }
2045b843c749SSergey Zigachev 
2046