blob: 1b621160b52e5ae5db1b21888e380873d6f0d73e [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include <drm/drmP.h>
27#include <drm/amdgpu_drm.h>
28#include "amdgpu.h"
29#include "amdgpu_atombios.h"
30#include "amdgpu_i2c.h"
31
32#include "atom.h"
33#include "atom-bits.h"
34#include "atombios_encoders.h"
35#include "bif/bif_4_1_d.h"
36
37static void amdgpu_atombios_lookup_i2c_gpio_quirks(struct amdgpu_device *adev,
38 ATOM_GPIO_I2C_ASSIGMENT *gpio,
39 u8 index)
40{
41
42}
43
44static struct amdgpu_i2c_bus_rec amdgpu_atombios_get_bus_rec_for_i2c_gpio(ATOM_GPIO_I2C_ASSIGMENT *gpio)
45{
46 struct amdgpu_i2c_bus_rec i2c;
47
48 memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
49
50 i2c.mask_clk_reg = le16_to_cpu(gpio->usClkMaskRegisterIndex);
51 i2c.mask_data_reg = le16_to_cpu(gpio->usDataMaskRegisterIndex);
52 i2c.en_clk_reg = le16_to_cpu(gpio->usClkEnRegisterIndex);
53 i2c.en_data_reg = le16_to_cpu(gpio->usDataEnRegisterIndex);
54 i2c.y_clk_reg = le16_to_cpu(gpio->usClkY_RegisterIndex);
55 i2c.y_data_reg = le16_to_cpu(gpio->usDataY_RegisterIndex);
56 i2c.a_clk_reg = le16_to_cpu(gpio->usClkA_RegisterIndex);
57 i2c.a_data_reg = le16_to_cpu(gpio->usDataA_RegisterIndex);
58 i2c.mask_clk_mask = (1 << gpio->ucClkMaskShift);
59 i2c.mask_data_mask = (1 << gpio->ucDataMaskShift);
60 i2c.en_clk_mask = (1 << gpio->ucClkEnShift);
61 i2c.en_data_mask = (1 << gpio->ucDataEnShift);
62 i2c.y_clk_mask = (1 << gpio->ucClkY_Shift);
63 i2c.y_data_mask = (1 << gpio->ucDataY_Shift);
64 i2c.a_clk_mask = (1 << gpio->ucClkA_Shift);
65 i2c.a_data_mask = (1 << gpio->ucDataA_Shift);
66
67 if (gpio->sucI2cId.sbfAccess.bfHW_Capable)
68 i2c.hw_capable = true;
69 else
70 i2c.hw_capable = false;
71
72 if (gpio->sucI2cId.ucAccess == 0xa0)
73 i2c.mm_i2c = true;
74 else
75 i2c.mm_i2c = false;
76
77 i2c.i2c_id = gpio->sucI2cId.ucAccess;
78
79 if (i2c.mask_clk_reg)
80 i2c.valid = true;
81 else
82 i2c.valid = false;
83
84 return i2c;
85}
86
87struct amdgpu_i2c_bus_rec amdgpu_atombios_lookup_i2c_gpio(struct amdgpu_device *adev,
88 uint8_t id)
89{
90 struct atom_context *ctx = adev->mode_info.atom_context;
91 ATOM_GPIO_I2C_ASSIGMENT *gpio;
92 struct amdgpu_i2c_bus_rec i2c;
93 int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
94 struct _ATOM_GPIO_I2C_INFO *i2c_info;
95 uint16_t data_offset, size;
96 int i, num_indices;
97
98 memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
99 i2c.valid = false;
100
101 if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
102 i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
103
104 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
105 sizeof(ATOM_GPIO_I2C_ASSIGMENT);
106
107 gpio = &i2c_info->asGPIO_Info[0];
108 for (i = 0; i < num_indices; i++) {
109
110 amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
111
112 if (gpio->sucI2cId.ucAccess == id) {
113 i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
114 break;
115 }
116 gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
117 ((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
118 }
119 }
120
121 return i2c;
122}
123
124void amdgpu_atombios_i2c_init(struct amdgpu_device *adev)
125{
126 struct atom_context *ctx = adev->mode_info.atom_context;
127 ATOM_GPIO_I2C_ASSIGMENT *gpio;
128 struct amdgpu_i2c_bus_rec i2c;
129 int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
130 struct _ATOM_GPIO_I2C_INFO *i2c_info;
131 uint16_t data_offset, size;
132 int i, num_indices;
133 char stmp[32];
134
135 if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
136 i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
137
138 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
139 sizeof(ATOM_GPIO_I2C_ASSIGMENT);
140
141 gpio = &i2c_info->asGPIO_Info[0];
142 for (i = 0; i < num_indices; i++) {
143 amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
144
145 i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
146
147 if (i2c.valid) {
148 sprintf(stmp, "0x%x", i2c.i2c_id);
149 adev->i2c_bus[i] = amdgpu_i2c_create(adev->ddev, &i2c, stmp);
150 }
151 gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
152 ((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
153 }
154 }
155}
156
157struct amdgpu_gpio_rec
158amdgpu_atombios_lookup_gpio(struct amdgpu_device *adev,
159 u8 id)
160{
161 struct atom_context *ctx = adev->mode_info.atom_context;
162 struct amdgpu_gpio_rec gpio;
163 int index = GetIndexIntoMasterTable(DATA, GPIO_Pin_LUT);
164 struct _ATOM_GPIO_PIN_LUT *gpio_info;
165 ATOM_GPIO_PIN_ASSIGNMENT *pin;
166 u16 data_offset, size;
167 int i, num_indices;
168
169 memset(&gpio, 0, sizeof(struct amdgpu_gpio_rec));
170 gpio.valid = false;
171
172 if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
173 gpio_info = (struct _ATOM_GPIO_PIN_LUT *)(ctx->bios + data_offset);
174
175 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
176 sizeof(ATOM_GPIO_PIN_ASSIGNMENT);
177
178 pin = gpio_info->asGPIO_Pin;
179 for (i = 0; i < num_indices; i++) {
180 if (id == pin->ucGPIO_ID) {
181 gpio.id = pin->ucGPIO_ID;
182 gpio.reg = le16_to_cpu(pin->usGpioPin_AIndex);
183 gpio.shift = pin->ucGpioPinBitShift;
184 gpio.mask = (1 << pin->ucGpioPinBitShift);
185 gpio.valid = true;
186 break;
187 }
188 pin = (ATOM_GPIO_PIN_ASSIGNMENT *)
189 ((u8 *)pin + sizeof(ATOM_GPIO_PIN_ASSIGNMENT));
190 }
191 }
192
193 return gpio;
194}
195
196static struct amdgpu_hpd
197amdgpu_atombios_get_hpd_info_from_gpio(struct amdgpu_device *adev,
198 struct amdgpu_gpio_rec *gpio)
199{
200 struct amdgpu_hpd hpd;
201 u32 reg;
202
203 memset(&hpd, 0, sizeof(struct amdgpu_hpd));
204
205 reg = amdgpu_display_hpd_get_gpio_reg(adev);
206
207 hpd.gpio = *gpio;
208 if (gpio->reg == reg) {
209 switch(gpio->mask) {
210 case (1 << 0):
211 hpd.hpd = AMDGPU_HPD_1;
212 break;
213 case (1 << 8):
214 hpd.hpd = AMDGPU_HPD_2;
215 break;
216 case (1 << 16):
217 hpd.hpd = AMDGPU_HPD_3;
218 break;
219 case (1 << 24):
220 hpd.hpd = AMDGPU_HPD_4;
221 break;
222 case (1 << 26):
223 hpd.hpd = AMDGPU_HPD_5;
224 break;
225 case (1 << 28):
226 hpd.hpd = AMDGPU_HPD_6;
227 break;
228 default:
229 hpd.hpd = AMDGPU_HPD_NONE;
230 break;
231 }
232 } else
233 hpd.hpd = AMDGPU_HPD_NONE;
234 return hpd;
235}
236
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400237static const int object_connector_convert[] = {
238 DRM_MODE_CONNECTOR_Unknown,
239 DRM_MODE_CONNECTOR_DVII,
240 DRM_MODE_CONNECTOR_DVII,
241 DRM_MODE_CONNECTOR_DVID,
242 DRM_MODE_CONNECTOR_DVID,
243 DRM_MODE_CONNECTOR_VGA,
244 DRM_MODE_CONNECTOR_Composite,
245 DRM_MODE_CONNECTOR_SVIDEO,
246 DRM_MODE_CONNECTOR_Unknown,
247 DRM_MODE_CONNECTOR_Unknown,
248 DRM_MODE_CONNECTOR_9PinDIN,
249 DRM_MODE_CONNECTOR_Unknown,
250 DRM_MODE_CONNECTOR_HDMIA,
251 DRM_MODE_CONNECTOR_HDMIB,
252 DRM_MODE_CONNECTOR_LVDS,
253 DRM_MODE_CONNECTOR_9PinDIN,
254 DRM_MODE_CONNECTOR_Unknown,
255 DRM_MODE_CONNECTOR_Unknown,
256 DRM_MODE_CONNECTOR_Unknown,
257 DRM_MODE_CONNECTOR_DisplayPort,
258 DRM_MODE_CONNECTOR_eDP,
259 DRM_MODE_CONNECTOR_Unknown
260};
261
Emily Deng0bad1612016-08-08 11:32:35 +0800262bool amdgpu_atombios_has_dce_engine_info(struct amdgpu_device *adev)
263{
264 struct amdgpu_mode_info *mode_info = &adev->mode_info;
265 struct atom_context *ctx = mode_info->atom_context;
266 int index = GetIndexIntoMasterTable(DATA, Object_Header);
267 u16 size, data_offset;
268 u8 frev, crev;
269 ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
270 ATOM_OBJECT_HEADER *obj_header;
271
272 if (!amdgpu_atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
273 return false;
274
275 if (crev < 2)
276 return false;
277
278 obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
279 path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
280 (ctx->bios + data_offset +
281 le16_to_cpu(obj_header->usDisplayPathTableOffset));
282
283 if (path_obj->ucNumOfDispPath)
284 return true;
285 else
286 return false;
287}
288
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400289bool amdgpu_atombios_get_connector_info_from_object_table(struct amdgpu_device *adev)
290{
291 struct amdgpu_mode_info *mode_info = &adev->mode_info;
292 struct atom_context *ctx = mode_info->atom_context;
293 int index = GetIndexIntoMasterTable(DATA, Object_Header);
294 u16 size, data_offset;
295 u8 frev, crev;
296 ATOM_CONNECTOR_OBJECT_TABLE *con_obj;
297 ATOM_ENCODER_OBJECT_TABLE *enc_obj;
298 ATOM_OBJECT_TABLE *router_obj;
299 ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
300 ATOM_OBJECT_HEADER *obj_header;
301 int i, j, k, path_size, device_support;
302 int connector_type;
303 u16 conn_id, connector_object_id;
304 struct amdgpu_i2c_bus_rec ddc_bus;
305 struct amdgpu_router router;
306 struct amdgpu_gpio_rec gpio;
307 struct amdgpu_hpd hpd;
308
309 if (!amdgpu_atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
310 return false;
311
312 if (crev < 2)
313 return false;
314
315 obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
316 path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
317 (ctx->bios + data_offset +
318 le16_to_cpu(obj_header->usDisplayPathTableOffset));
319 con_obj = (ATOM_CONNECTOR_OBJECT_TABLE *)
320 (ctx->bios + data_offset +
321 le16_to_cpu(obj_header->usConnectorObjectTableOffset));
322 enc_obj = (ATOM_ENCODER_OBJECT_TABLE *)
323 (ctx->bios + data_offset +
324 le16_to_cpu(obj_header->usEncoderObjectTableOffset));
325 router_obj = (ATOM_OBJECT_TABLE *)
326 (ctx->bios + data_offset +
327 le16_to_cpu(obj_header->usRouterObjectTableOffset));
328 device_support = le16_to_cpu(obj_header->usDeviceSupport);
329
330 path_size = 0;
331 for (i = 0; i < path_obj->ucNumOfDispPath; i++) {
332 uint8_t *addr = (uint8_t *) path_obj->asDispPath;
333 ATOM_DISPLAY_OBJECT_PATH *path;
334 addr += path_size;
335 path = (ATOM_DISPLAY_OBJECT_PATH *) addr;
336 path_size += le16_to_cpu(path->usSize);
337
338 if (device_support & le16_to_cpu(path->usDeviceTag)) {
339 uint8_t con_obj_id, con_obj_num, con_obj_type;
340
341 con_obj_id =
342 (le16_to_cpu(path->usConnObjectId) & OBJECT_ID_MASK)
343 >> OBJECT_ID_SHIFT;
344 con_obj_num =
345 (le16_to_cpu(path->usConnObjectId) & ENUM_ID_MASK)
346 >> ENUM_ID_SHIFT;
347 con_obj_type =
348 (le16_to_cpu(path->usConnObjectId) &
349 OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
350
351 connector_type =
352 object_connector_convert[con_obj_id];
353 connector_object_id = con_obj_id;
354
355 if (connector_type == DRM_MODE_CONNECTOR_Unknown)
356 continue;
357
358 router.ddc_valid = false;
359 router.cd_valid = false;
360 for (j = 0; j < ((le16_to_cpu(path->usSize) - 8) / 2); j++) {
361 uint8_t grph_obj_id, grph_obj_num, grph_obj_type;
362
363 grph_obj_id =
364 (le16_to_cpu(path->usGraphicObjIds[j]) &
365 OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
366 grph_obj_num =
367 (le16_to_cpu(path->usGraphicObjIds[j]) &
368 ENUM_ID_MASK) >> ENUM_ID_SHIFT;
369 grph_obj_type =
370 (le16_to_cpu(path->usGraphicObjIds[j]) &
371 OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
372
373 if (grph_obj_type == GRAPH_OBJECT_TYPE_ENCODER) {
374 for (k = 0; k < enc_obj->ucNumberOfObjects; k++) {
375 u16 encoder_obj = le16_to_cpu(enc_obj->asObjects[k].usObjectID);
376 if (le16_to_cpu(path->usGraphicObjIds[j]) == encoder_obj) {
377 ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
378 (ctx->bios + data_offset +
379 le16_to_cpu(enc_obj->asObjects[k].usRecordOffset));
380 ATOM_ENCODER_CAP_RECORD *cap_record;
381 u16 caps = 0;
382
383 while (record->ucRecordSize > 0 &&
384 record->ucRecordType > 0 &&
385 record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
386 switch (record->ucRecordType) {
387 case ATOM_ENCODER_CAP_RECORD_TYPE:
388 cap_record =(ATOM_ENCODER_CAP_RECORD *)
389 record;
390 caps = le16_to_cpu(cap_record->usEncoderCap);
391 break;
392 }
393 record = (ATOM_COMMON_RECORD_HEADER *)
394 ((char *)record + record->ucRecordSize);
395 }
396 amdgpu_display_add_encoder(adev, encoder_obj,
397 le16_to_cpu(path->usDeviceTag),
398 caps);
399 }
400 }
401 } else if (grph_obj_type == GRAPH_OBJECT_TYPE_ROUTER) {
402 for (k = 0; k < router_obj->ucNumberOfObjects; k++) {
403 u16 router_obj_id = le16_to_cpu(router_obj->asObjects[k].usObjectID);
404 if (le16_to_cpu(path->usGraphicObjIds[j]) == router_obj_id) {
405 ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
406 (ctx->bios + data_offset +
407 le16_to_cpu(router_obj->asObjects[k].usRecordOffset));
408 ATOM_I2C_RECORD *i2c_record;
409 ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
410 ATOM_ROUTER_DDC_PATH_SELECT_RECORD *ddc_path;
411 ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *cd_path;
412 ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *router_src_dst_table =
413 (ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *)
414 (ctx->bios + data_offset +
415 le16_to_cpu(router_obj->asObjects[k].usSrcDstTableOffset));
416 u8 *num_dst_objs = (u8 *)
417 ((u8 *)router_src_dst_table + 1 +
418 (router_src_dst_table->ucNumberOfSrc * 2));
419 u16 *dst_objs = (u16 *)(num_dst_objs + 1);
420 int enum_id;
421
422 router.router_id = router_obj_id;
423 for (enum_id = 0; enum_id < (*num_dst_objs); enum_id++) {
424 if (le16_to_cpu(path->usConnObjectId) ==
425 le16_to_cpu(dst_objs[enum_id]))
426 break;
427 }
428
429 while (record->ucRecordSize > 0 &&
430 record->ucRecordType > 0 &&
431 record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
432 switch (record->ucRecordType) {
433 case ATOM_I2C_RECORD_TYPE:
434 i2c_record =
435 (ATOM_I2C_RECORD *)
436 record;
437 i2c_config =
438 (ATOM_I2C_ID_CONFIG_ACCESS *)
439 &i2c_record->sucI2cId;
440 router.i2c_info =
441 amdgpu_atombios_lookup_i2c_gpio(adev,
442 i2c_config->
443 ucAccess);
444 router.i2c_addr = i2c_record->ucI2CAddr >> 1;
445 break;
446 case ATOM_ROUTER_DDC_PATH_SELECT_RECORD_TYPE:
447 ddc_path = (ATOM_ROUTER_DDC_PATH_SELECT_RECORD *)
448 record;
449 router.ddc_valid = true;
450 router.ddc_mux_type = ddc_path->ucMuxType;
451 router.ddc_mux_control_pin = ddc_path->ucMuxControlPin;
452 router.ddc_mux_state = ddc_path->ucMuxState[enum_id];
453 break;
454 case ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD_TYPE:
455 cd_path = (ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *)
456 record;
457 router.cd_valid = true;
458 router.cd_mux_type = cd_path->ucMuxType;
459 router.cd_mux_control_pin = cd_path->ucMuxControlPin;
460 router.cd_mux_state = cd_path->ucMuxState[enum_id];
461 break;
462 }
463 record = (ATOM_COMMON_RECORD_HEADER *)
464 ((char *)record + record->ucRecordSize);
465 }
466 }
467 }
468 }
469 }
470
471 /* look up gpio for ddc, hpd */
472 ddc_bus.valid = false;
473 hpd.hpd = AMDGPU_HPD_NONE;
474 if ((le16_to_cpu(path->usDeviceTag) &
475 (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT)) == 0) {
476 for (j = 0; j < con_obj->ucNumberOfObjects; j++) {
477 if (le16_to_cpu(path->usConnObjectId) ==
478 le16_to_cpu(con_obj->asObjects[j].
479 usObjectID)) {
480 ATOM_COMMON_RECORD_HEADER
481 *record =
482 (ATOM_COMMON_RECORD_HEADER
483 *)
484 (ctx->bios + data_offset +
485 le16_to_cpu(con_obj->
486 asObjects[j].
487 usRecordOffset));
488 ATOM_I2C_RECORD *i2c_record;
489 ATOM_HPD_INT_RECORD *hpd_record;
490 ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
491
492 while (record->ucRecordSize > 0 &&
493 record->ucRecordType > 0 &&
494 record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
495 switch (record->ucRecordType) {
496 case ATOM_I2C_RECORD_TYPE:
497 i2c_record =
498 (ATOM_I2C_RECORD *)
499 record;
500 i2c_config =
501 (ATOM_I2C_ID_CONFIG_ACCESS *)
502 &i2c_record->sucI2cId;
503 ddc_bus = amdgpu_atombios_lookup_i2c_gpio(adev,
504 i2c_config->
505 ucAccess);
506 break;
507 case ATOM_HPD_INT_RECORD_TYPE:
508 hpd_record =
509 (ATOM_HPD_INT_RECORD *)
510 record;
511 gpio = amdgpu_atombios_lookup_gpio(adev,
512 hpd_record->ucHPDIntGPIOID);
513 hpd = amdgpu_atombios_get_hpd_info_from_gpio(adev, &gpio);
514 hpd.plugged_state = hpd_record->ucPlugged_PinState;
515 break;
516 }
517 record =
518 (ATOM_COMMON_RECORD_HEADER
519 *) ((char *)record
520 +
521 record->
522 ucRecordSize);
523 }
524 break;
525 }
526 }
527 }
528
529 /* needed for aux chan transactions */
530 ddc_bus.hpd = hpd.hpd;
531
532 conn_id = le16_to_cpu(path->usConnObjectId);
533
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 amdgpu_display_add_connector(adev,
535 conn_id,
536 le16_to_cpu(path->usDeviceTag),
537 connector_type, &ddc_bus,
538 connector_object_id,
539 &hpd,
540 &router);
541
542 }
543 }
544
545 amdgpu_link_encoder_connector(adev->ddev);
546
547 return true;
548}
549
550union firmware_info {
551 ATOM_FIRMWARE_INFO info;
552 ATOM_FIRMWARE_INFO_V1_2 info_12;
553 ATOM_FIRMWARE_INFO_V1_3 info_13;
554 ATOM_FIRMWARE_INFO_V1_4 info_14;
555 ATOM_FIRMWARE_INFO_V2_1 info_21;
556 ATOM_FIRMWARE_INFO_V2_2 info_22;
557};
558
559int amdgpu_atombios_get_clock_info(struct amdgpu_device *adev)
560{
561 struct amdgpu_mode_info *mode_info = &adev->mode_info;
562 int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
563 uint8_t frev, crev;
564 uint16_t data_offset;
565 int ret = -EINVAL;
566
567 if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
568 &frev, &crev, &data_offset)) {
569 int i;
570 struct amdgpu_pll *ppll = &adev->clock.ppll[0];
571 struct amdgpu_pll *spll = &adev->clock.spll;
572 struct amdgpu_pll *mpll = &adev->clock.mpll;
573 union firmware_info *firmware_info =
574 (union firmware_info *)(mode_info->atom_context->bios +
575 data_offset);
576 /* pixel clocks */
577 ppll->reference_freq =
578 le16_to_cpu(firmware_info->info.usReferenceClock);
579 ppll->reference_div = 0;
580
Alex Deuchera8a04c92016-07-27 15:31:59 -0400581 ppll->pll_out_min =
582 le32_to_cpu(firmware_info->info_12.ulMinPixelClockPLL_Output);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 ppll->pll_out_max =
584 le32_to_cpu(firmware_info->info.ulMaxPixelClockPLL_Output);
585
Alex Deuchera8a04c92016-07-27 15:31:59 -0400586 ppll->lcd_pll_out_min =
587 le16_to_cpu(firmware_info->info_14.usLcdMinPixelClockPLL_Output) * 100;
588 if (ppll->lcd_pll_out_min == 0)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589 ppll->lcd_pll_out_min = ppll->pll_out_min;
Alex Deuchera8a04c92016-07-27 15:31:59 -0400590 ppll->lcd_pll_out_max =
591 le16_to_cpu(firmware_info->info_14.usLcdMaxPixelClockPLL_Output) * 100;
592 if (ppll->lcd_pll_out_max == 0)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400593 ppll->lcd_pll_out_max = ppll->pll_out_max;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594
595 if (ppll->pll_out_min == 0)
596 ppll->pll_out_min = 64800;
597
598 ppll->pll_in_min =
599 le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Input);
600 ppll->pll_in_max =
601 le16_to_cpu(firmware_info->info.usMaxPixelClockPLL_Input);
602
603 ppll->min_post_div = 2;
604 ppll->max_post_div = 0x7f;
605 ppll->min_frac_feedback_div = 0;
606 ppll->max_frac_feedback_div = 9;
607 ppll->min_ref_div = 2;
608 ppll->max_ref_div = 0x3ff;
609 ppll->min_feedback_div = 4;
610 ppll->max_feedback_div = 0xfff;
611 ppll->best_vco = 0;
612
613 for (i = 1; i < AMDGPU_MAX_PPLL; i++)
614 adev->clock.ppll[i] = *ppll;
615
616 /* system clock */
617 spll->reference_freq =
618 le16_to_cpu(firmware_info->info_21.usCoreReferenceClock);
619 spll->reference_div = 0;
620
621 spll->pll_out_min =
622 le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Output);
623 spll->pll_out_max =
624 le32_to_cpu(firmware_info->info.ulMaxEngineClockPLL_Output);
625
626 /* ??? */
627 if (spll->pll_out_min == 0)
628 spll->pll_out_min = 64800;
629
630 spll->pll_in_min =
631 le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Input);
632 spll->pll_in_max =
633 le16_to_cpu(firmware_info->info.usMaxEngineClockPLL_Input);
634
635 spll->min_post_div = 1;
636 spll->max_post_div = 1;
637 spll->min_ref_div = 2;
638 spll->max_ref_div = 0xff;
639 spll->min_feedback_div = 4;
640 spll->max_feedback_div = 0xff;
641 spll->best_vco = 0;
642
643 /* memory clock */
644 mpll->reference_freq =
645 le16_to_cpu(firmware_info->info_21.usMemoryReferenceClock);
646 mpll->reference_div = 0;
647
648 mpll->pll_out_min =
649 le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Output);
650 mpll->pll_out_max =
651 le32_to_cpu(firmware_info->info.ulMaxMemoryClockPLL_Output);
652
653 /* ??? */
654 if (mpll->pll_out_min == 0)
655 mpll->pll_out_min = 64800;
656
657 mpll->pll_in_min =
658 le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Input);
659 mpll->pll_in_max =
660 le16_to_cpu(firmware_info->info.usMaxMemoryClockPLL_Input);
661
662 adev->clock.default_sclk =
663 le32_to_cpu(firmware_info->info.ulDefaultEngineClock);
664 adev->clock.default_mclk =
665 le32_to_cpu(firmware_info->info.ulDefaultMemoryClock);
666
667 mpll->min_post_div = 1;
668 mpll->max_post_div = 1;
669 mpll->min_ref_div = 2;
670 mpll->max_ref_div = 0xff;
671 mpll->min_feedback_div = 4;
672 mpll->max_feedback_div = 0xff;
673 mpll->best_vco = 0;
674
675 /* disp clock */
676 adev->clock.default_dispclk =
677 le32_to_cpu(firmware_info->info_21.ulDefaultDispEngineClkFreq);
Alex Deucher80c083c2015-10-12 10:38:02 -0400678 /* set a reasonable default for DP */
679 if (adev->clock.default_dispclk < 53900) {
680 DRM_INFO("Changing default dispclk from %dMhz to 600Mhz\n",
681 adev->clock.default_dispclk / 100);
682 adev->clock.default_dispclk = 60000;
683 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684 adev->clock.dp_extclk =
685 le16_to_cpu(firmware_info->info_21.usUniphyDPModeExtClkFreq);
686 adev->clock.current_dispclk = adev->clock.default_dispclk;
687
688 adev->clock.max_pixel_clock = le16_to_cpu(firmware_info->info.usMaxPixelClock);
689 if (adev->clock.max_pixel_clock == 0)
690 adev->clock.max_pixel_clock = 40000;
691
692 /* not technically a clock, but... */
693 adev->mode_info.firmware_flags =
694 le16_to_cpu(firmware_info->info.usFirmwareCapability.susAccess);
695
696 ret = 0;
697 }
698
699 adev->pm.current_sclk = adev->clock.default_sclk;
700 adev->pm.current_mclk = adev->clock.default_mclk;
701
702 return ret;
703}
704
Alex Deucher397a2702016-03-14 16:51:24 -0400705union gfx_info {
706 ATOM_GFX_INFO_V2_1 info;
707};
708
709int amdgpu_atombios_get_gfx_info(struct amdgpu_device *adev)
710{
711 struct amdgpu_mode_info *mode_info = &adev->mode_info;
712 int index = GetIndexIntoMasterTable(DATA, GFX_Info);
713 uint8_t frev, crev;
714 uint16_t data_offset;
715 int ret = -EINVAL;
716
717 if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
718 &frev, &crev, &data_offset)) {
719 union gfx_info *gfx_info = (union gfx_info *)
720 (mode_info->atom_context->bios + data_offset);
721
722 adev->gfx.config.max_shader_engines = gfx_info->info.max_shader_engines;
723 adev->gfx.config.max_tile_pipes = gfx_info->info.max_tile_pipes;
724 adev->gfx.config.max_cu_per_sh = gfx_info->info.max_cu_per_sh;
725 adev->gfx.config.max_sh_per_se = gfx_info->info.max_sh_per_se;
726 adev->gfx.config.max_backends_per_se = gfx_info->info.max_backends_per_se;
727 adev->gfx.config.max_texture_channel_caches =
728 gfx_info->info.max_texture_channel_caches;
729
730 ret = 0;
731 }
732 return ret;
733}
734
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400735union igp_info {
736 struct _ATOM_INTEGRATED_SYSTEM_INFO info;
737 struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
738 struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
739 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
740 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
741 struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_9 info_9;
742};
743
744static void amdgpu_atombios_get_igp_ss_overrides(struct amdgpu_device *adev,
745 struct amdgpu_atom_ss *ss,
746 int id)
747{
748 struct amdgpu_mode_info *mode_info = &adev->mode_info;
749 int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
750 u16 data_offset, size;
751 union igp_info *igp_info;
752 u8 frev, crev;
753 u16 percentage = 0, rate = 0;
754
755 /* get any igp specific overrides */
756 if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
757 &frev, &crev, &data_offset)) {
758 igp_info = (union igp_info *)
759 (mode_info->atom_context->bios + data_offset);
760 switch (crev) {
761 case 6:
762 switch (id) {
763 case ASIC_INTERNAL_SS_ON_TMDS:
764 percentage = le16_to_cpu(igp_info->info_6.usDVISSPercentage);
765 rate = le16_to_cpu(igp_info->info_6.usDVISSpreadRateIn10Hz);
766 break;
767 case ASIC_INTERNAL_SS_ON_HDMI:
768 percentage = le16_to_cpu(igp_info->info_6.usHDMISSPercentage);
769 rate = le16_to_cpu(igp_info->info_6.usHDMISSpreadRateIn10Hz);
770 break;
771 case ASIC_INTERNAL_SS_ON_LVDS:
772 percentage = le16_to_cpu(igp_info->info_6.usLvdsSSPercentage);
773 rate = le16_to_cpu(igp_info->info_6.usLvdsSSpreadRateIn10Hz);
774 break;
775 }
776 break;
777 case 7:
778 switch (id) {
779 case ASIC_INTERNAL_SS_ON_TMDS:
780 percentage = le16_to_cpu(igp_info->info_7.usDVISSPercentage);
781 rate = le16_to_cpu(igp_info->info_7.usDVISSpreadRateIn10Hz);
782 break;
783 case ASIC_INTERNAL_SS_ON_HDMI:
784 percentage = le16_to_cpu(igp_info->info_7.usHDMISSPercentage);
785 rate = le16_to_cpu(igp_info->info_7.usHDMISSpreadRateIn10Hz);
786 break;
787 case ASIC_INTERNAL_SS_ON_LVDS:
788 percentage = le16_to_cpu(igp_info->info_7.usLvdsSSPercentage);
789 rate = le16_to_cpu(igp_info->info_7.usLvdsSSpreadRateIn10Hz);
790 break;
791 }
792 break;
793 case 8:
794 switch (id) {
795 case ASIC_INTERNAL_SS_ON_TMDS:
796 percentage = le16_to_cpu(igp_info->info_8.usDVISSPercentage);
797 rate = le16_to_cpu(igp_info->info_8.usDVISSpreadRateIn10Hz);
798 break;
799 case ASIC_INTERNAL_SS_ON_HDMI:
800 percentage = le16_to_cpu(igp_info->info_8.usHDMISSPercentage);
801 rate = le16_to_cpu(igp_info->info_8.usHDMISSpreadRateIn10Hz);
802 break;
803 case ASIC_INTERNAL_SS_ON_LVDS:
804 percentage = le16_to_cpu(igp_info->info_8.usLvdsSSPercentage);
805 rate = le16_to_cpu(igp_info->info_8.usLvdsSSpreadRateIn10Hz);
806 break;
807 }
808 break;
809 case 9:
810 switch (id) {
811 case ASIC_INTERNAL_SS_ON_TMDS:
812 percentage = le16_to_cpu(igp_info->info_9.usDVISSPercentage);
813 rate = le16_to_cpu(igp_info->info_9.usDVISSpreadRateIn10Hz);
814 break;
815 case ASIC_INTERNAL_SS_ON_HDMI:
816 percentage = le16_to_cpu(igp_info->info_9.usHDMISSPercentage);
817 rate = le16_to_cpu(igp_info->info_9.usHDMISSpreadRateIn10Hz);
818 break;
819 case ASIC_INTERNAL_SS_ON_LVDS:
820 percentage = le16_to_cpu(igp_info->info_9.usLvdsSSPercentage);
821 rate = le16_to_cpu(igp_info->info_9.usLvdsSSpreadRateIn10Hz);
822 break;
823 }
824 break;
825 default:
826 DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
827 break;
828 }
829 if (percentage)
830 ss->percentage = percentage;
831 if (rate)
832 ss->rate = rate;
833 }
834}
835
836union asic_ss_info {
837 struct _ATOM_ASIC_INTERNAL_SS_INFO info;
838 struct _ATOM_ASIC_INTERNAL_SS_INFO_V2 info_2;
839 struct _ATOM_ASIC_INTERNAL_SS_INFO_V3 info_3;
840};
841
842union asic_ss_assignment {
843 struct _ATOM_ASIC_SS_ASSIGNMENT v1;
844 struct _ATOM_ASIC_SS_ASSIGNMENT_V2 v2;
845 struct _ATOM_ASIC_SS_ASSIGNMENT_V3 v3;
846};
847
848bool amdgpu_atombios_get_asic_ss_info(struct amdgpu_device *adev,
849 struct amdgpu_atom_ss *ss,
850 int id, u32 clock)
851{
852 struct amdgpu_mode_info *mode_info = &adev->mode_info;
853 int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
854 uint16_t data_offset, size;
855 union asic_ss_info *ss_info;
856 union asic_ss_assignment *ss_assign;
857 uint8_t frev, crev;
858 int i, num_indices;
859
860 if (id == ASIC_INTERNAL_MEMORY_SS) {
861 if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_MEMORY_CLOCK_SS_SUPPORT))
862 return false;
863 }
864 if (id == ASIC_INTERNAL_ENGINE_SS) {
865 if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_ENGINE_CLOCK_SS_SUPPORT))
866 return false;
867 }
868
869 memset(ss, 0, sizeof(struct amdgpu_atom_ss));
870 if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
871 &frev, &crev, &data_offset)) {
872
873 ss_info =
874 (union asic_ss_info *)(mode_info->atom_context->bios + data_offset);
875
876 switch (frev) {
877 case 1:
878 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
879 sizeof(ATOM_ASIC_SS_ASSIGNMENT);
880
881 ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info.asSpreadSpectrum[0]);
882 for (i = 0; i < num_indices; i++) {
883 if ((ss_assign->v1.ucClockIndication == id) &&
884 (clock <= le32_to_cpu(ss_assign->v1.ulTargetClockRange))) {
885 ss->percentage =
886 le16_to_cpu(ss_assign->v1.usSpreadSpectrumPercentage);
887 ss->type = ss_assign->v1.ucSpreadSpectrumMode;
888 ss->rate = le16_to_cpu(ss_assign->v1.usSpreadRateInKhz);
889 ss->percentage_divider = 100;
890 return true;
891 }
892 ss_assign = (union asic_ss_assignment *)
893 ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT));
894 }
895 break;
896 case 2:
897 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
898 sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2);
899 ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_2.asSpreadSpectrum[0]);
900 for (i = 0; i < num_indices; i++) {
901 if ((ss_assign->v2.ucClockIndication == id) &&
902 (clock <= le32_to_cpu(ss_assign->v2.ulTargetClockRange))) {
903 ss->percentage =
904 le16_to_cpu(ss_assign->v2.usSpreadSpectrumPercentage);
905 ss->type = ss_assign->v2.ucSpreadSpectrumMode;
906 ss->rate = le16_to_cpu(ss_assign->v2.usSpreadRateIn10Hz);
907 ss->percentage_divider = 100;
908 if ((crev == 2) &&
909 ((id == ASIC_INTERNAL_ENGINE_SS) ||
910 (id == ASIC_INTERNAL_MEMORY_SS)))
911 ss->rate /= 100;
912 return true;
913 }
914 ss_assign = (union asic_ss_assignment *)
915 ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2));
916 }
917 break;
918 case 3:
919 num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
920 sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3);
921 ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_3.asSpreadSpectrum[0]);
922 for (i = 0; i < num_indices; i++) {
923 if ((ss_assign->v3.ucClockIndication == id) &&
924 (clock <= le32_to_cpu(ss_assign->v3.ulTargetClockRange))) {
925 ss->percentage =
926 le16_to_cpu(ss_assign->v3.usSpreadSpectrumPercentage);
927 ss->type = ss_assign->v3.ucSpreadSpectrumMode;
928 ss->rate = le16_to_cpu(ss_assign->v3.usSpreadRateIn10Hz);
929 if (ss_assign->v3.ucSpreadSpectrumMode &
930 SS_MODE_V3_PERCENTAGE_DIV_BY_1000_MASK)
931 ss->percentage_divider = 1000;
932 else
933 ss->percentage_divider = 100;
934 if ((id == ASIC_INTERNAL_ENGINE_SS) ||
935 (id == ASIC_INTERNAL_MEMORY_SS))
936 ss->rate /= 100;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800937 if (adev->flags & AMD_IS_APU)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400938 amdgpu_atombios_get_igp_ss_overrides(adev, ss, id);
939 return true;
940 }
941 ss_assign = (union asic_ss_assignment *)
942 ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3));
943 }
944 break;
945 default:
946 DRM_ERROR("Unsupported ASIC_InternalSS_Info table: %d %d\n", frev, crev);
947 break;
948 }
949
950 }
951 return false;
952}
953
954union get_clock_dividers {
955 struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS v1;
956 struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V2 v2;
957 struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V3 v3;
958 struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V4 v4;
959 struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V5 v5;
960 struct _COMPUTE_GPU_CLOCK_INPUT_PARAMETERS_V1_6 v6_in;
961 struct _COMPUTE_GPU_CLOCK_OUTPUT_PARAMETERS_V1_6 v6_out;
962};
963
964int amdgpu_atombios_get_clock_dividers(struct amdgpu_device *adev,
965 u8 clock_type,
966 u32 clock,
967 bool strobe_mode,
968 struct atom_clock_dividers *dividers)
969{
970 union get_clock_dividers args;
971 int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryEnginePLL);
972 u8 frev, crev;
973
974 memset(&args, 0, sizeof(args));
975 memset(dividers, 0, sizeof(struct atom_clock_dividers));
976
977 if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
978 return -EINVAL;
979
980 switch (crev) {
981 case 4:
982 /* fusion */
983 args.v4.ulClock = cpu_to_le32(clock); /* 10 khz */
984
985 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
986
987 dividers->post_divider = dividers->post_div = args.v4.ucPostDiv;
988 dividers->real_clock = le32_to_cpu(args.v4.ulClock);
989 break;
990 case 6:
991 /* CI */
992 /* COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK, COMPUTE_GPUCLK_INPUT_FLAG_SCLK */
993 args.v6_in.ulClock.ulComputeClockFlag = clock_type;
994 args.v6_in.ulClock.ulClockFreq = cpu_to_le32(clock); /* 10 khz */
995
996 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
997
998 dividers->whole_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDiv);
999 dividers->frac_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDivFrac);
1000 dividers->ref_div = args.v6_out.ucPllRefDiv;
1001 dividers->post_div = args.v6_out.ucPllPostDiv;
1002 dividers->flags = args.v6_out.ucPllCntlFlag;
1003 dividers->real_clock = le32_to_cpu(args.v6_out.ulClock.ulClock);
1004 dividers->post_divider = args.v6_out.ulClock.ucPostDiv;
1005 break;
1006 default:
1007 return -EINVAL;
1008 }
1009 return 0;
1010}
1011
1012int amdgpu_atombios_get_memory_pll_dividers(struct amdgpu_device *adev,
1013 u32 clock,
1014 bool strobe_mode,
1015 struct atom_mpll_param *mpll_param)
1016{
1017 COMPUTE_MEMORY_CLOCK_PARAM_PARAMETERS_V2_1 args;
1018 int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryClockParam);
1019 u8 frev, crev;
1020
1021 memset(&args, 0, sizeof(args));
1022 memset(mpll_param, 0, sizeof(struct atom_mpll_param));
1023
1024 if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1025 return -EINVAL;
1026
1027 switch (frev) {
1028 case 2:
1029 switch (crev) {
1030 case 1:
1031 /* SI */
1032 args.ulClock = cpu_to_le32(clock); /* 10 khz */
1033 args.ucInputFlag = 0;
1034 if (strobe_mode)
1035 args.ucInputFlag |= MPLL_INPUT_FLAG_STROBE_MODE_EN;
1036
1037 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1038
1039 mpll_param->clkfrac = le16_to_cpu(args.ulFbDiv.usFbDivFrac);
1040 mpll_param->clkf = le16_to_cpu(args.ulFbDiv.usFbDiv);
1041 mpll_param->post_div = args.ucPostDiv;
1042 mpll_param->dll_speed = args.ucDllSpeed;
1043 mpll_param->bwcntl = args.ucBWCntl;
1044 mpll_param->vco_mode =
1045 (args.ucPllCntlFlag & MPLL_CNTL_FLAG_VCO_MODE_MASK);
1046 mpll_param->yclk_sel =
1047 (args.ucPllCntlFlag & MPLL_CNTL_FLAG_BYPASS_DQ_PLL) ? 1 : 0;
1048 mpll_param->qdr =
1049 (args.ucPllCntlFlag & MPLL_CNTL_FLAG_QDR_ENABLE) ? 1 : 0;
1050 mpll_param->half_rate =
1051 (args.ucPllCntlFlag & MPLL_CNTL_FLAG_AD_HALF_RATE) ? 1 : 0;
1052 break;
1053 default:
1054 return -EINVAL;
1055 }
1056 break;
1057 default:
1058 return -EINVAL;
1059 }
1060 return 0;
1061}
1062
1063uint32_t amdgpu_atombios_get_engine_clock(struct amdgpu_device *adev)
1064{
1065 GET_ENGINE_CLOCK_PS_ALLOCATION args;
1066 int index = GetIndexIntoMasterTable(COMMAND, GetEngineClock);
1067
1068 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1069 return le32_to_cpu(args.ulReturnEngineClock);
1070}
1071
1072uint32_t amdgpu_atombios_get_memory_clock(struct amdgpu_device *adev)
1073{
1074 GET_MEMORY_CLOCK_PS_ALLOCATION args;
1075 int index = GetIndexIntoMasterTable(COMMAND, GetMemoryClock);
1076
1077 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1078 return le32_to_cpu(args.ulReturnMemoryClock);
1079}
1080
1081void amdgpu_atombios_set_engine_clock(struct amdgpu_device *adev,
1082 uint32_t eng_clock)
1083{
1084 SET_ENGINE_CLOCK_PS_ALLOCATION args;
1085 int index = GetIndexIntoMasterTable(COMMAND, SetEngineClock);
1086
1087 args.ulTargetEngineClock = cpu_to_le32(eng_clock); /* 10 khz */
1088
1089 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1090}
1091
1092void amdgpu_atombios_set_memory_clock(struct amdgpu_device *adev,
1093 uint32_t mem_clock)
1094{
1095 SET_MEMORY_CLOCK_PS_ALLOCATION args;
1096 int index = GetIndexIntoMasterTable(COMMAND, SetMemoryClock);
1097
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001098 if (adev->flags & AMD_IS_APU)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099 return;
1100
1101 args.ulTargetMemoryClock = cpu_to_le32(mem_clock); /* 10 khz */
1102
1103 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1104}
1105
1106void amdgpu_atombios_set_engine_dram_timings(struct amdgpu_device *adev,
1107 u32 eng_clock, u32 mem_clock)
1108{
1109 SET_ENGINE_CLOCK_PS_ALLOCATION args;
1110 int index = GetIndexIntoMasterTable(COMMAND, DynamicMemorySettings);
1111 u32 tmp;
1112
1113 memset(&args, 0, sizeof(args));
1114
1115 tmp = eng_clock & SET_CLOCK_FREQ_MASK;
1116 tmp |= (COMPUTE_ENGINE_PLL_PARAM << 24);
1117
1118 args.ulTargetEngineClock = cpu_to_le32(tmp);
1119 if (mem_clock)
1120 args.sReserved.ulClock = cpu_to_le32(mem_clock & SET_CLOCK_FREQ_MASK);
1121
1122 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1123}
1124
1125union set_voltage {
1126 struct _SET_VOLTAGE_PS_ALLOCATION alloc;
1127 struct _SET_VOLTAGE_PARAMETERS v1;
1128 struct _SET_VOLTAGE_PARAMETERS_V2 v2;
1129 struct _SET_VOLTAGE_PARAMETERS_V1_3 v3;
1130};
1131
1132void amdgpu_atombios_set_voltage(struct amdgpu_device *adev,
1133 u16 voltage_level,
1134 u8 voltage_type)
1135{
1136 union set_voltage args;
1137 int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
1138 u8 frev, crev, volt_index = voltage_level;
1139
1140 if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1141 return;
1142
1143 /* 0xff01 is a flag rather then an actual voltage */
1144 if (voltage_level == 0xff01)
1145 return;
1146
1147 switch (crev) {
1148 case 1:
1149 args.v1.ucVoltageType = voltage_type;
1150 args.v1.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_ALL_SOURCE;
1151 args.v1.ucVoltageIndex = volt_index;
1152 break;
1153 case 2:
1154 args.v2.ucVoltageType = voltage_type;
1155 args.v2.ucVoltageMode = SET_ASIC_VOLTAGE_MODE_SET_VOLTAGE;
1156 args.v2.usVoltageLevel = cpu_to_le16(voltage_level);
1157 break;
1158 case 3:
1159 args.v3.ucVoltageType = voltage_type;
1160 args.v3.ucVoltageMode = ATOM_SET_VOLTAGE;
1161 args.v3.usVoltageLevel = cpu_to_le16(voltage_level);
1162 break;
1163 default:
1164 DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1165 return;
1166 }
1167
1168 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1169}
1170
1171int amdgpu_atombios_get_leakage_id_from_vbios(struct amdgpu_device *adev,
1172 u16 *leakage_id)
1173{
1174 union set_voltage args;
1175 int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
1176 u8 frev, crev;
1177
1178 if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
1179 return -EINVAL;
1180
1181 switch (crev) {
1182 case 3:
1183 case 4:
1184 args.v3.ucVoltageType = 0;
1185 args.v3.ucVoltageMode = ATOM_GET_LEAKAGE_ID;
1186 args.v3.usVoltageLevel = 0;
1187
1188 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1189
1190 *leakage_id = le16_to_cpu(args.v3.usVoltageLevel);
1191 break;
1192 default:
1193 DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1194 return -EINVAL;
1195 }
1196
1197 return 0;
1198}
1199
1200int amdgpu_atombios_get_leakage_vddc_based_on_leakage_params(struct amdgpu_device *adev,
1201 u16 *vddc, u16 *vddci,
1202 u16 virtual_voltage_id,
1203 u16 vbios_voltage_id)
1204{
1205 int index = GetIndexIntoMasterTable(DATA, ASIC_ProfilingInfo);
1206 u8 frev, crev;
1207 u16 data_offset, size;
1208 int i, j;
1209 ATOM_ASIC_PROFILING_INFO_V2_1 *profile;
1210 u16 *leakage_bin, *vddc_id_buf, *vddc_buf, *vddci_id_buf, *vddci_buf;
1211
1212 *vddc = 0;
1213 *vddci = 0;
1214
1215 if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1216 &frev, &crev, &data_offset))
1217 return -EINVAL;
1218
1219 profile = (ATOM_ASIC_PROFILING_INFO_V2_1 *)
1220 (adev->mode_info.atom_context->bios + data_offset);
1221
1222 switch (frev) {
1223 case 1:
1224 return -EINVAL;
1225 case 2:
1226 switch (crev) {
1227 case 1:
1228 if (size < sizeof(ATOM_ASIC_PROFILING_INFO_V2_1))
1229 return -EINVAL;
1230 leakage_bin = (u16 *)
1231 (adev->mode_info.atom_context->bios + data_offset +
1232 le16_to_cpu(profile->usLeakageBinArrayOffset));
1233 vddc_id_buf = (u16 *)
1234 (adev->mode_info.atom_context->bios + data_offset +
1235 le16_to_cpu(profile->usElbVDDC_IdArrayOffset));
1236 vddc_buf = (u16 *)
1237 (adev->mode_info.atom_context->bios + data_offset +
1238 le16_to_cpu(profile->usElbVDDC_LevelArrayOffset));
1239 vddci_id_buf = (u16 *)
1240 (adev->mode_info.atom_context->bios + data_offset +
1241 le16_to_cpu(profile->usElbVDDCI_IdArrayOffset));
1242 vddci_buf = (u16 *)
1243 (adev->mode_info.atom_context->bios + data_offset +
1244 le16_to_cpu(profile->usElbVDDCI_LevelArrayOffset));
1245
1246 if (profile->ucElbVDDC_Num > 0) {
1247 for (i = 0; i < profile->ucElbVDDC_Num; i++) {
1248 if (vddc_id_buf[i] == virtual_voltage_id) {
1249 for (j = 0; j < profile->ucLeakageBinNum; j++) {
1250 if (vbios_voltage_id <= leakage_bin[j]) {
1251 *vddc = vddc_buf[j * profile->ucElbVDDC_Num + i];
1252 break;
1253 }
1254 }
1255 break;
1256 }
1257 }
1258 }
1259 if (profile->ucElbVDDCI_Num > 0) {
1260 for (i = 0; i < profile->ucElbVDDCI_Num; i++) {
1261 if (vddci_id_buf[i] == virtual_voltage_id) {
1262 for (j = 0; j < profile->ucLeakageBinNum; j++) {
1263 if (vbios_voltage_id <= leakage_bin[j]) {
1264 *vddci = vddci_buf[j * profile->ucElbVDDCI_Num + i];
1265 break;
1266 }
1267 }
1268 break;
1269 }
1270 }
1271 }
1272 break;
1273 default:
1274 DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1275 return -EINVAL;
1276 }
1277 break;
1278 default:
1279 DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1280 return -EINVAL;
1281 }
1282
1283 return 0;
1284}
1285
1286union get_voltage_info {
1287 struct _GET_VOLTAGE_INFO_INPUT_PARAMETER_V1_2 in;
1288 struct _GET_EVV_VOLTAGE_INFO_OUTPUT_PARAMETER_V1_2 evv_out;
1289};
1290
1291int amdgpu_atombios_get_voltage_evv(struct amdgpu_device *adev,
1292 u16 virtual_voltage_id,
1293 u16 *voltage)
1294{
1295 int index = GetIndexIntoMasterTable(COMMAND, GetVoltageInfo);
1296 u32 entry_id;
1297 u32 count = adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count;
1298 union get_voltage_info args;
1299
1300 for (entry_id = 0; entry_id < count; entry_id++) {
1301 if (adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].v ==
1302 virtual_voltage_id)
1303 break;
1304 }
1305
1306 if (entry_id >= count)
1307 return -EINVAL;
1308
1309 args.in.ucVoltageType = VOLTAGE_TYPE_VDDC;
1310 args.in.ucVoltageMode = ATOM_GET_VOLTAGE_EVV_VOLTAGE;
1311 args.in.usVoltageLevel = cpu_to_le16(virtual_voltage_id);
1312 args.in.ulSCLKFreq =
1313 cpu_to_le32(adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].clk);
1314
1315 amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
1316
1317 *voltage = le16_to_cpu(args.evv_out.usVoltageLevel);
1318
1319 return 0;
1320}
1321
1322union voltage_object_info {
1323 struct _ATOM_VOLTAGE_OBJECT_INFO v1;
1324 struct _ATOM_VOLTAGE_OBJECT_INFO_V2 v2;
1325 struct _ATOM_VOLTAGE_OBJECT_INFO_V3_1 v3;
1326};
1327
1328union voltage_object {
1329 struct _ATOM_VOLTAGE_OBJECT v1;
1330 struct _ATOM_VOLTAGE_OBJECT_V2 v2;
1331 union _ATOM_VOLTAGE_OBJECT_V3 v3;
1332};
1333
1334
1335static ATOM_VOLTAGE_OBJECT_V3 *amdgpu_atombios_lookup_voltage_object_v3(ATOM_VOLTAGE_OBJECT_INFO_V3_1 *v3,
1336 u8 voltage_type, u8 voltage_mode)
1337{
1338 u32 size = le16_to_cpu(v3->sHeader.usStructureSize);
1339 u32 offset = offsetof(ATOM_VOLTAGE_OBJECT_INFO_V3_1, asVoltageObj[0]);
1340 u8 *start = (u8*)v3;
1341
1342 while (offset < size) {
1343 ATOM_VOLTAGE_OBJECT_V3 *vo = (ATOM_VOLTAGE_OBJECT_V3 *)(start + offset);
1344 if ((vo->asGpioVoltageObj.sHeader.ucVoltageType == voltage_type) &&
1345 (vo->asGpioVoltageObj.sHeader.ucVoltageMode == voltage_mode))
1346 return vo;
1347 offset += le16_to_cpu(vo->asGpioVoltageObj.sHeader.usSize);
1348 }
1349 return NULL;
1350}
1351
1352bool
1353amdgpu_atombios_is_voltage_gpio(struct amdgpu_device *adev,
1354 u8 voltage_type, u8 voltage_mode)
1355{
1356 int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
1357 u8 frev, crev;
1358 u16 data_offset, size;
1359 union voltage_object_info *voltage_info;
1360
1361 if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1362 &frev, &crev, &data_offset)) {
1363 voltage_info = (union voltage_object_info *)
1364 (adev->mode_info.atom_context->bios + data_offset);
1365
1366 switch (frev) {
1367 case 3:
1368 switch (crev) {
1369 case 1:
1370 if (amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
1371 voltage_type, voltage_mode))
1372 return true;
1373 break;
1374 default:
1375 DRM_ERROR("unknown voltage object table\n");
1376 return false;
1377 }
1378 break;
1379 default:
1380 DRM_ERROR("unknown voltage object table\n");
1381 return false;
1382 }
1383
1384 }
1385 return false;
1386}
1387
1388int amdgpu_atombios_get_voltage_table(struct amdgpu_device *adev,
1389 u8 voltage_type, u8 voltage_mode,
1390 struct atom_voltage_table *voltage_table)
1391{
1392 int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
1393 u8 frev, crev;
1394 u16 data_offset, size;
1395 int i;
1396 union voltage_object_info *voltage_info;
1397 union voltage_object *voltage_object = NULL;
1398
1399 if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1400 &frev, &crev, &data_offset)) {
1401 voltage_info = (union voltage_object_info *)
1402 (adev->mode_info.atom_context->bios + data_offset);
1403
1404 switch (frev) {
1405 case 3:
1406 switch (crev) {
1407 case 1:
1408 voltage_object = (union voltage_object *)
1409 amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
1410 voltage_type, voltage_mode);
1411 if (voltage_object) {
1412 ATOM_GPIO_VOLTAGE_OBJECT_V3 *gpio =
1413 &voltage_object->v3.asGpioVoltageObj;
1414 VOLTAGE_LUT_ENTRY_V2 *lut;
1415 if (gpio->ucGpioEntryNum > MAX_VOLTAGE_ENTRIES)
1416 return -EINVAL;
1417 lut = &gpio->asVolGpioLut[0];
1418 for (i = 0; i < gpio->ucGpioEntryNum; i++) {
1419 voltage_table->entries[i].value =
1420 le16_to_cpu(lut->usVoltageValue);
1421 voltage_table->entries[i].smio_low =
1422 le32_to_cpu(lut->ulVoltageId);
1423 lut = (VOLTAGE_LUT_ENTRY_V2 *)
1424 ((u8 *)lut + sizeof(VOLTAGE_LUT_ENTRY_V2));
1425 }
1426 voltage_table->mask_low = le32_to_cpu(gpio->ulGpioMaskVal);
1427 voltage_table->count = gpio->ucGpioEntryNum;
1428 voltage_table->phase_delay = gpio->ucPhaseDelay;
1429 return 0;
1430 }
1431 break;
1432 default:
1433 DRM_ERROR("unknown voltage object table\n");
1434 return -EINVAL;
1435 }
1436 break;
1437 default:
1438 DRM_ERROR("unknown voltage object table\n");
1439 return -EINVAL;
1440 }
1441 }
1442 return -EINVAL;
1443}
1444
1445union vram_info {
1446 struct _ATOM_VRAM_INFO_V3 v1_3;
1447 struct _ATOM_VRAM_INFO_V4 v1_4;
1448 struct _ATOM_VRAM_INFO_HEADER_V2_1 v2_1;
1449};
1450
1451#define MEM_ID_MASK 0xff000000
1452#define MEM_ID_SHIFT 24
1453#define CLOCK_RANGE_MASK 0x00ffffff
1454#define CLOCK_RANGE_SHIFT 0
1455#define LOW_NIBBLE_MASK 0xf
1456#define DATA_EQU_PREV 0
1457#define DATA_FROM_TABLE 4
1458
1459int amdgpu_atombios_init_mc_reg_table(struct amdgpu_device *adev,
1460 u8 module_index,
1461 struct atom_mc_reg_table *reg_table)
1462{
1463 int index = GetIndexIntoMasterTable(DATA, VRAM_Info);
1464 u8 frev, crev, num_entries, t_mem_id, num_ranges = 0;
1465 u32 i = 0, j;
1466 u16 data_offset, size;
1467 union vram_info *vram_info;
1468
1469 memset(reg_table, 0, sizeof(struct atom_mc_reg_table));
1470
1471 if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1472 &frev, &crev, &data_offset)) {
1473 vram_info = (union vram_info *)
1474 (adev->mode_info.atom_context->bios + data_offset);
1475 switch (frev) {
1476 case 1:
1477 DRM_ERROR("old table version %d, %d\n", frev, crev);
1478 return -EINVAL;
1479 case 2:
1480 switch (crev) {
1481 case 1:
1482 if (module_index < vram_info->v2_1.ucNumOfVRAMModule) {
1483 ATOM_INIT_REG_BLOCK *reg_block =
1484 (ATOM_INIT_REG_BLOCK *)
1485 ((u8 *)vram_info + le16_to_cpu(vram_info->v2_1.usMemClkPatchTblOffset));
1486 ATOM_MEMORY_SETTING_DATA_BLOCK *reg_data =
1487 (ATOM_MEMORY_SETTING_DATA_BLOCK *)
1488 ((u8 *)reg_block + (2 * sizeof(u16)) +
1489 le16_to_cpu(reg_block->usRegIndexTblSize));
1490 ATOM_INIT_REG_INDEX_FORMAT *format = &reg_block->asRegIndexBuf[0];
1491 num_entries = (u8)((le16_to_cpu(reg_block->usRegIndexTblSize)) /
1492 sizeof(ATOM_INIT_REG_INDEX_FORMAT)) - 1;
1493 if (num_entries > VBIOS_MC_REGISTER_ARRAY_SIZE)
1494 return -EINVAL;
1495 while (i < num_entries) {
1496 if (format->ucPreRegDataLength & ACCESS_PLACEHOLDER)
1497 break;
1498 reg_table->mc_reg_address[i].s1 =
1499 (u16)(le16_to_cpu(format->usRegIndex));
1500 reg_table->mc_reg_address[i].pre_reg_data =
1501 (u8)(format->ucPreRegDataLength);
1502 i++;
1503 format = (ATOM_INIT_REG_INDEX_FORMAT *)
1504 ((u8 *)format + sizeof(ATOM_INIT_REG_INDEX_FORMAT));
1505 }
1506 reg_table->last = i;
1507 while ((le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK) &&
1508 (num_ranges < VBIOS_MAX_AC_TIMING_ENTRIES)) {
1509 t_mem_id = (u8)((le32_to_cpu(*(u32 *)reg_data) & MEM_ID_MASK)
1510 >> MEM_ID_SHIFT);
1511 if (module_index == t_mem_id) {
1512 reg_table->mc_reg_table_entry[num_ranges].mclk_max =
1513 (u32)((le32_to_cpu(*(u32 *)reg_data) & CLOCK_RANGE_MASK)
1514 >> CLOCK_RANGE_SHIFT);
1515 for (i = 0, j = 1; i < reg_table->last; i++) {
1516 if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_FROM_TABLE) {
1517 reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
1518 (u32)le32_to_cpu(*((u32 *)reg_data + j));
1519 j++;
1520 } else if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_EQU_PREV) {
1521 reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
1522 reg_table->mc_reg_table_entry[num_ranges].mc_data[i - 1];
1523 }
1524 }
1525 num_ranges++;
1526 }
1527 reg_data = (ATOM_MEMORY_SETTING_DATA_BLOCK *)
1528 ((u8 *)reg_data + le16_to_cpu(reg_block->usRegDataBlkSize));
1529 }
1530 if (le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK)
1531 return -EINVAL;
1532 reg_table->num_entries = num_ranges;
1533 } else
1534 return -EINVAL;
1535 break;
1536 default:
1537 DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1538 return -EINVAL;
1539 }
1540 break;
1541 default:
1542 DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
1543 return -EINVAL;
1544 }
1545 return 0;
1546 }
1547 return -EINVAL;
1548}
1549
Alex Deuchere74adf22016-02-01 11:00:49 -05001550bool amdgpu_atombios_has_gpu_virtualization_table(struct amdgpu_device *adev)
1551{
1552 int index = GetIndexIntoMasterTable(DATA, GPUVirtualizationInfo);
1553 u8 frev, crev;
1554 u16 data_offset, size;
1555
1556 if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
1557 &frev, &crev, &data_offset))
1558 return true;
1559
1560 return false;
1561}
1562
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001563void amdgpu_atombios_scratch_regs_lock(struct amdgpu_device *adev, bool lock)
1564{
1565 uint32_t bios_6_scratch;
1566
1567 bios_6_scratch = RREG32(mmBIOS_SCRATCH_6);
1568
1569 if (lock) {
1570 bios_6_scratch |= ATOM_S6_CRITICAL_STATE;
1571 bios_6_scratch &= ~ATOM_S6_ACC_MODE;
1572 } else {
1573 bios_6_scratch &= ~ATOM_S6_CRITICAL_STATE;
1574 bios_6_scratch |= ATOM_S6_ACC_MODE;
1575 }
1576
1577 WREG32(mmBIOS_SCRATCH_6, bios_6_scratch);
1578}
1579
1580void amdgpu_atombios_scratch_regs_init(struct amdgpu_device *adev)
1581{
1582 uint32_t bios_2_scratch, bios_6_scratch;
1583
1584 bios_2_scratch = RREG32(mmBIOS_SCRATCH_2);
1585 bios_6_scratch = RREG32(mmBIOS_SCRATCH_6);
1586
1587 /* let the bios control the backlight */
1588 bios_2_scratch &= ~ATOM_S2_VRI_BRIGHT_ENABLE;
1589
1590 /* tell the bios not to handle mode switching */
1591 bios_6_scratch |= ATOM_S6_ACC_BLOCK_DISPLAY_SWITCH;
1592
1593 /* clear the vbios dpms state */
1594 bios_2_scratch &= ~ATOM_S2_DEVICE_DPMS_STATE;
1595
1596 WREG32(mmBIOS_SCRATCH_2, bios_2_scratch);
1597 WREG32(mmBIOS_SCRATCH_6, bios_6_scratch);
1598}
1599
1600void amdgpu_atombios_scratch_regs_save(struct amdgpu_device *adev)
1601{
1602 int i;
1603
1604 for (i = 0; i < AMDGPU_BIOS_NUM_SCRATCH; i++)
1605 adev->bios_scratch[i] = RREG32(mmBIOS_SCRATCH_0 + i);
1606}
1607
1608void amdgpu_atombios_scratch_regs_restore(struct amdgpu_device *adev)
1609{
1610 int i;
1611
1612 for (i = 0; i < AMDGPU_BIOS_NUM_SCRATCH; i++)
1613 WREG32(mmBIOS_SCRATCH_0 + i, adev->bios_scratch[i]);
1614}
1615
1616/* Atom needs data in little endian format
1617 * so swap as appropriate when copying data to
1618 * or from atom. Note that atom operates on
1619 * dw units.
1620 */
1621void amdgpu_atombios_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
1622{
1623#ifdef __BIG_ENDIAN
1624 u8 src_tmp[20], dst_tmp[20]; /* used for byteswapping */
1625 u32 *dst32, *src32;
1626 int i;
1627
1628 memcpy(src_tmp, src, num_bytes);
1629 src32 = (u32 *)src_tmp;
1630 dst32 = (u32 *)dst_tmp;
1631 if (to_le) {
1632 for (i = 0; i < ((num_bytes + 3) / 4); i++)
1633 dst32[i] = cpu_to_le32(src32[i]);
1634 memcpy(dst, dst_tmp, num_bytes);
1635 } else {
1636 u8 dws = num_bytes & ~3;
1637 for (i = 0; i < ((num_bytes + 3) / 4); i++)
1638 dst32[i] = le32_to_cpu(src32[i]);
1639 memcpy(dst, dst_tmp, dws);
1640 if (num_bytes % 4) {
1641 for (i = 0; i < (num_bytes % 4); i++)
1642 dst[dws+i] = dst_tmp[dws+i];
1643 }
1644 }
1645#else
1646 memcpy(dst, src, num_bytes);
1647#endif
1648}