Loading...
1/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23#include <drm/drmP.h>
24#include "amdgpu.h"
25#include "amdgpu_pm.h"
26#include "amdgpu_i2c.h"
27#include "vid.h"
28#include "atom.h"
29#include "amdgpu_atombios.h"
30#include "atombios_crtc.h"
31#include "atombios_encoders.h"
32#include "amdgpu_pll.h"
33#include "amdgpu_connectors.h"
34#include "dce_v10_0.h"
35
36#include "dce/dce_10_0_d.h"
37#include "dce/dce_10_0_sh_mask.h"
38#include "dce/dce_10_0_enum.h"
39#include "oss/oss_3_0_d.h"
40#include "oss/oss_3_0_sh_mask.h"
41#include "gmc/gmc_8_1_d.h"
42#include "gmc/gmc_8_1_sh_mask.h"
43
44static void dce_v10_0_set_display_funcs(struct amdgpu_device *adev);
45static void dce_v10_0_set_irq_funcs(struct amdgpu_device *adev);
46
47static const u32 crtc_offsets[] =
48{
49 CRTC0_REGISTER_OFFSET,
50 CRTC1_REGISTER_OFFSET,
51 CRTC2_REGISTER_OFFSET,
52 CRTC3_REGISTER_OFFSET,
53 CRTC4_REGISTER_OFFSET,
54 CRTC5_REGISTER_OFFSET,
55 CRTC6_REGISTER_OFFSET
56};
57
58static const u32 hpd_offsets[] =
59{
60 HPD0_REGISTER_OFFSET,
61 HPD1_REGISTER_OFFSET,
62 HPD2_REGISTER_OFFSET,
63 HPD3_REGISTER_OFFSET,
64 HPD4_REGISTER_OFFSET,
65 HPD5_REGISTER_OFFSET
66};
67
68static const uint32_t dig_offsets[] = {
69 DIG0_REGISTER_OFFSET,
70 DIG1_REGISTER_OFFSET,
71 DIG2_REGISTER_OFFSET,
72 DIG3_REGISTER_OFFSET,
73 DIG4_REGISTER_OFFSET,
74 DIG5_REGISTER_OFFSET,
75 DIG6_REGISTER_OFFSET
76};
77
78static const struct {
79 uint32_t reg;
80 uint32_t vblank;
81 uint32_t vline;
82 uint32_t hpd;
83
84} interrupt_status_offsets[] = { {
85 .reg = mmDISP_INTERRUPT_STATUS,
86 .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
87 .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
88 .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
89}, {
90 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
91 .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
92 .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
93 .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
94}, {
95 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
96 .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
97 .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
98 .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
99}, {
100 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
101 .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
102 .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
103 .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
104}, {
105 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
106 .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
107 .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
108 .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
109}, {
110 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
111 .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
112 .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
113 .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
114} };
115
116static const u32 golden_settings_tonga_a11[] =
117{
118 mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
119 mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
120 mmFBC_MISC, 0x1f311fff, 0x12300000,
121 mmHDMI_CONTROL, 0x31000111, 0x00000011,
122};
123
124static const u32 tonga_mgcg_cgcg_init[] =
125{
126 mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
127 mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
128};
129
130static const u32 golden_settings_fiji_a10[] =
131{
132 mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
133 mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
134 mmFBC_MISC, 0x1f311fff, 0x12300000,
135 mmHDMI_CONTROL, 0x31000111, 0x00000011,
136};
137
138static const u32 fiji_mgcg_cgcg_init[] =
139{
140 mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
141 mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
142};
143
144static void dce_v10_0_init_golden_registers(struct amdgpu_device *adev)
145{
146 switch (adev->asic_type) {
147 case CHIP_FIJI:
148 amdgpu_device_program_register_sequence(adev,
149 fiji_mgcg_cgcg_init,
150 ARRAY_SIZE(fiji_mgcg_cgcg_init));
151 amdgpu_device_program_register_sequence(adev,
152 golden_settings_fiji_a10,
153 ARRAY_SIZE(golden_settings_fiji_a10));
154 break;
155 case CHIP_TONGA:
156 amdgpu_device_program_register_sequence(adev,
157 tonga_mgcg_cgcg_init,
158 ARRAY_SIZE(tonga_mgcg_cgcg_init));
159 amdgpu_device_program_register_sequence(adev,
160 golden_settings_tonga_a11,
161 ARRAY_SIZE(golden_settings_tonga_a11));
162 break;
163 default:
164 break;
165 }
166}
167
168static u32 dce_v10_0_audio_endpt_rreg(struct amdgpu_device *adev,
169 u32 block_offset, u32 reg)
170{
171 unsigned long flags;
172 u32 r;
173
174 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
175 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
176 r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
177 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
178
179 return r;
180}
181
182static void dce_v10_0_audio_endpt_wreg(struct amdgpu_device *adev,
183 u32 block_offset, u32 reg, u32 v)
184{
185 unsigned long flags;
186
187 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
188 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
189 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
190 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
191}
192
193static u32 dce_v10_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
194{
195 if (crtc >= adev->mode_info.num_crtc)
196 return 0;
197 else
198 return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
199}
200
201static void dce_v10_0_pageflip_interrupt_init(struct amdgpu_device *adev)
202{
203 unsigned i;
204
205 /* Enable pflip interrupts */
206 for (i = 0; i < adev->mode_info.num_crtc; i++)
207 amdgpu_irq_get(adev, &adev->pageflip_irq, i);
208}
209
210static void dce_v10_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
211{
212 unsigned i;
213
214 /* Disable pflip interrupts */
215 for (i = 0; i < adev->mode_info.num_crtc; i++)
216 amdgpu_irq_put(adev, &adev->pageflip_irq, i);
217}
218
219/**
220 * dce_v10_0_page_flip - pageflip callback.
221 *
222 * @adev: amdgpu_device pointer
223 * @crtc_id: crtc to cleanup pageflip on
224 * @crtc_base: new address of the crtc (GPU MC address)
225 *
226 * Triggers the actual pageflip by updating the primary
227 * surface base address.
228 */
229static void dce_v10_0_page_flip(struct amdgpu_device *adev,
230 int crtc_id, u64 crtc_base, bool async)
231{
232 struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
233 u32 tmp;
234
235 /* flip at hsync for async, default is vsync */
236 tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
237 tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
238 GRPH_SURFACE_UPDATE_H_RETRACE_EN, async ? 1 : 0);
239 WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
240 /* update the primary scanout address */
241 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
242 upper_32_bits(crtc_base));
243 /* writing to the low address triggers the update */
244 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
245 lower_32_bits(crtc_base));
246 /* post the write */
247 RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
248}
249
250static int dce_v10_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
251 u32 *vbl, u32 *position)
252{
253 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
254 return -EINVAL;
255
256 *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
257 *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
258
259 return 0;
260}
261
262/**
263 * dce_v10_0_hpd_sense - hpd sense callback.
264 *
265 * @adev: amdgpu_device pointer
266 * @hpd: hpd (hotplug detect) pin
267 *
268 * Checks if a digital monitor is connected (evergreen+).
269 * Returns true if connected, false if not connected.
270 */
271static bool dce_v10_0_hpd_sense(struct amdgpu_device *adev,
272 enum amdgpu_hpd_id hpd)
273{
274 bool connected = false;
275
276 if (hpd >= adev->mode_info.num_hpd)
277 return connected;
278
279 if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[hpd]) &
280 DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
281 connected = true;
282
283 return connected;
284}
285
286/**
287 * dce_v10_0_hpd_set_polarity - hpd set polarity callback.
288 *
289 * @adev: amdgpu_device pointer
290 * @hpd: hpd (hotplug detect) pin
291 *
292 * Set the polarity of the hpd pin (evergreen+).
293 */
294static void dce_v10_0_hpd_set_polarity(struct amdgpu_device *adev,
295 enum amdgpu_hpd_id hpd)
296{
297 u32 tmp;
298 bool connected = dce_v10_0_hpd_sense(adev, hpd);
299
300 if (hpd >= adev->mode_info.num_hpd)
301 return;
302
303 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
304 if (connected)
305 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
306 else
307 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
308 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
309}
310
311/**
312 * dce_v10_0_hpd_init - hpd setup callback.
313 *
314 * @adev: amdgpu_device pointer
315 *
316 * Setup the hpd pins used by the card (evergreen+).
317 * Enable the pin, set the polarity, and enable the hpd interrupts.
318 */
319static void dce_v10_0_hpd_init(struct amdgpu_device *adev)
320{
321 struct drm_device *dev = adev->ddev;
322 struct drm_connector *connector;
323 u32 tmp;
324
325 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
326 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
327
328 if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
329 continue;
330
331 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
332 connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
333 /* don't try to enable hpd on eDP or LVDS avoid breaking the
334 * aux dp channel on imac and help (but not completely fix)
335 * https://bugzilla.redhat.com/show_bug.cgi?id=726143
336 * also avoid interrupt storms during dpms.
337 */
338 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
339 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
340 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
341 continue;
342 }
343
344 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
345 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
346 WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
347
348 tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd]);
349 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
350 DC_HPD_CONNECT_INT_DELAY,
351 AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
352 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
353 DC_HPD_DISCONNECT_INT_DELAY,
354 AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
355 WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
356
357 dce_v10_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
358 amdgpu_irq_get(adev, &adev->hpd_irq,
359 amdgpu_connector->hpd.hpd);
360 }
361}
362
363/**
364 * dce_v10_0_hpd_fini - hpd tear down callback.
365 *
366 * @adev: amdgpu_device pointer
367 *
368 * Tear down the hpd pins used by the card (evergreen+).
369 * Disable the hpd interrupts.
370 */
371static void dce_v10_0_hpd_fini(struct amdgpu_device *adev)
372{
373 struct drm_device *dev = adev->ddev;
374 struct drm_connector *connector;
375 u32 tmp;
376
377 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
378 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
379
380 if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
381 continue;
382
383 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
384 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
385 WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
386
387 amdgpu_irq_put(adev, &adev->hpd_irq,
388 amdgpu_connector->hpd.hpd);
389 }
390}
391
392static u32 dce_v10_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
393{
394 return mmDC_GPIO_HPD_A;
395}
396
397static bool dce_v10_0_is_display_hung(struct amdgpu_device *adev)
398{
399 u32 crtc_hung = 0;
400 u32 crtc_status[6];
401 u32 i, j, tmp;
402
403 for (i = 0; i < adev->mode_info.num_crtc; i++) {
404 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
405 if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
406 crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
407 crtc_hung |= (1 << i);
408 }
409 }
410
411 for (j = 0; j < 10; j++) {
412 for (i = 0; i < adev->mode_info.num_crtc; i++) {
413 if (crtc_hung & (1 << i)) {
414 tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
415 if (tmp != crtc_status[i])
416 crtc_hung &= ~(1 << i);
417 }
418 }
419 if (crtc_hung == 0)
420 return false;
421 udelay(100);
422 }
423
424 return true;
425}
426
427static void dce_v10_0_set_vga_render_state(struct amdgpu_device *adev,
428 bool render)
429{
430 u32 tmp;
431
432 /* Lockout access through VGA aperture*/
433 tmp = RREG32(mmVGA_HDP_CONTROL);
434 if (render)
435 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
436 else
437 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
438 WREG32(mmVGA_HDP_CONTROL, tmp);
439
440 /* disable VGA render */
441 tmp = RREG32(mmVGA_RENDER_CONTROL);
442 if (render)
443 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
444 else
445 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
446 WREG32(mmVGA_RENDER_CONTROL, tmp);
447}
448
449static int dce_v10_0_get_num_crtc(struct amdgpu_device *adev)
450{
451 int num_crtc = 0;
452
453 switch (adev->asic_type) {
454 case CHIP_FIJI:
455 case CHIP_TONGA:
456 num_crtc = 6;
457 break;
458 default:
459 num_crtc = 0;
460 }
461 return num_crtc;
462}
463
464void dce_v10_0_disable_dce(struct amdgpu_device *adev)
465{
466 /*Disable VGA render and enabled crtc, if has DCE engine*/
467 if (amdgpu_atombios_has_dce_engine_info(adev)) {
468 u32 tmp;
469 int crtc_enabled, i;
470
471 dce_v10_0_set_vga_render_state(adev, false);
472
473 /*Disable crtc*/
474 for (i = 0; i < dce_v10_0_get_num_crtc(adev); i++) {
475 crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
476 CRTC_CONTROL, CRTC_MASTER_EN);
477 if (crtc_enabled) {
478 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
479 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
480 tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
481 WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
482 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
483 }
484 }
485 }
486}
487
488static void dce_v10_0_program_fmt(struct drm_encoder *encoder)
489{
490 struct drm_device *dev = encoder->dev;
491 struct amdgpu_device *adev = dev->dev_private;
492 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
493 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
494 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
495 int bpc = 0;
496 u32 tmp = 0;
497 enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
498
499 if (connector) {
500 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
501 bpc = amdgpu_connector_get_monitor_bpc(connector);
502 dither = amdgpu_connector->dither;
503 }
504
505 /* LVDS/eDP FMT is set up by atom */
506 if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
507 return;
508
509 /* not needed for analog */
510 if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
511 (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
512 return;
513
514 if (bpc == 0)
515 return;
516
517 switch (bpc) {
518 case 6:
519 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
520 /* XXX sort out optimal dither settings */
521 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
522 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
523 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
524 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
525 } else {
526 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
527 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
528 }
529 break;
530 case 8:
531 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
532 /* XXX sort out optimal dither settings */
533 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
534 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
535 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
536 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
537 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
538 } else {
539 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
540 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
541 }
542 break;
543 case 10:
544 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
545 /* XXX sort out optimal dither settings */
546 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
547 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
548 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
549 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
550 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
551 } else {
552 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
553 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
554 }
555 break;
556 default:
557 /* not needed */
558 break;
559 }
560
561 WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
562}
563
564
565/* display watermark setup */
566/**
567 * dce_v10_0_line_buffer_adjust - Set up the line buffer
568 *
569 * @adev: amdgpu_device pointer
570 * @amdgpu_crtc: the selected display controller
571 * @mode: the current display mode on the selected display
572 * controller
573 *
574 * Setup up the line buffer allocation for
575 * the selected display controller (CIK).
576 * Returns the line buffer size in pixels.
577 */
578static u32 dce_v10_0_line_buffer_adjust(struct amdgpu_device *adev,
579 struct amdgpu_crtc *amdgpu_crtc,
580 struct drm_display_mode *mode)
581{
582 u32 tmp, buffer_alloc, i, mem_cfg;
583 u32 pipe_offset = amdgpu_crtc->crtc_id;
584 /*
585 * Line Buffer Setup
586 * There are 6 line buffers, one for each display controllers.
587 * There are 3 partitions per LB. Select the number of partitions
588 * to enable based on the display width. For display widths larger
589 * than 4096, you need use to use 2 display controllers and combine
590 * them using the stereo blender.
591 */
592 if (amdgpu_crtc->base.enabled && mode) {
593 if (mode->crtc_hdisplay < 1920) {
594 mem_cfg = 1;
595 buffer_alloc = 2;
596 } else if (mode->crtc_hdisplay < 2560) {
597 mem_cfg = 2;
598 buffer_alloc = 2;
599 } else if (mode->crtc_hdisplay < 4096) {
600 mem_cfg = 0;
601 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
602 } else {
603 DRM_DEBUG_KMS("Mode too big for LB!\n");
604 mem_cfg = 0;
605 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
606 }
607 } else {
608 mem_cfg = 1;
609 buffer_alloc = 0;
610 }
611
612 tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
613 tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
614 WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
615
616 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
617 tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
618 WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
619
620 for (i = 0; i < adev->usec_timeout; i++) {
621 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
622 if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
623 break;
624 udelay(1);
625 }
626
627 if (amdgpu_crtc->base.enabled && mode) {
628 switch (mem_cfg) {
629 case 0:
630 default:
631 return 4096 * 2;
632 case 1:
633 return 1920 * 2;
634 case 2:
635 return 2560 * 2;
636 }
637 }
638
639 /* controller not enabled, so no lb used */
640 return 0;
641}
642
643/**
644 * cik_get_number_of_dram_channels - get the number of dram channels
645 *
646 * @adev: amdgpu_device pointer
647 *
648 * Look up the number of video ram channels (CIK).
649 * Used for display watermark bandwidth calculations
650 * Returns the number of dram channels
651 */
652static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
653{
654 u32 tmp = RREG32(mmMC_SHARED_CHMAP);
655
656 switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
657 case 0:
658 default:
659 return 1;
660 case 1:
661 return 2;
662 case 2:
663 return 4;
664 case 3:
665 return 8;
666 case 4:
667 return 3;
668 case 5:
669 return 6;
670 case 6:
671 return 10;
672 case 7:
673 return 12;
674 case 8:
675 return 16;
676 }
677}
678
679struct dce10_wm_params {
680 u32 dram_channels; /* number of dram channels */
681 u32 yclk; /* bandwidth per dram data pin in kHz */
682 u32 sclk; /* engine clock in kHz */
683 u32 disp_clk; /* display clock in kHz */
684 u32 src_width; /* viewport width */
685 u32 active_time; /* active display time in ns */
686 u32 blank_time; /* blank time in ns */
687 bool interlaced; /* mode is interlaced */
688 fixed20_12 vsc; /* vertical scale ratio */
689 u32 num_heads; /* number of active crtcs */
690 u32 bytes_per_pixel; /* bytes per pixel display + overlay */
691 u32 lb_size; /* line buffer allocated to pipe */
692 u32 vtaps; /* vertical scaler taps */
693};
694
695/**
696 * dce_v10_0_dram_bandwidth - get the dram bandwidth
697 *
698 * @wm: watermark calculation data
699 *
700 * Calculate the raw dram bandwidth (CIK).
701 * Used for display watermark bandwidth calculations
702 * Returns the dram bandwidth in MBytes/s
703 */
704static u32 dce_v10_0_dram_bandwidth(struct dce10_wm_params *wm)
705{
706 /* Calculate raw DRAM Bandwidth */
707 fixed20_12 dram_efficiency; /* 0.7 */
708 fixed20_12 yclk, dram_channels, bandwidth;
709 fixed20_12 a;
710
711 a.full = dfixed_const(1000);
712 yclk.full = dfixed_const(wm->yclk);
713 yclk.full = dfixed_div(yclk, a);
714 dram_channels.full = dfixed_const(wm->dram_channels * 4);
715 a.full = dfixed_const(10);
716 dram_efficiency.full = dfixed_const(7);
717 dram_efficiency.full = dfixed_div(dram_efficiency, a);
718 bandwidth.full = dfixed_mul(dram_channels, yclk);
719 bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
720
721 return dfixed_trunc(bandwidth);
722}
723
724/**
725 * dce_v10_0_dram_bandwidth_for_display - get the dram bandwidth for display
726 *
727 * @wm: watermark calculation data
728 *
729 * Calculate the dram bandwidth used for display (CIK).
730 * Used for display watermark bandwidth calculations
731 * Returns the dram bandwidth for display in MBytes/s
732 */
733static u32 dce_v10_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
734{
735 /* Calculate DRAM Bandwidth and the part allocated to display. */
736 fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
737 fixed20_12 yclk, dram_channels, bandwidth;
738 fixed20_12 a;
739
740 a.full = dfixed_const(1000);
741 yclk.full = dfixed_const(wm->yclk);
742 yclk.full = dfixed_div(yclk, a);
743 dram_channels.full = dfixed_const(wm->dram_channels * 4);
744 a.full = dfixed_const(10);
745 disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
746 disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
747 bandwidth.full = dfixed_mul(dram_channels, yclk);
748 bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
749
750 return dfixed_trunc(bandwidth);
751}
752
753/**
754 * dce_v10_0_data_return_bandwidth - get the data return bandwidth
755 *
756 * @wm: watermark calculation data
757 *
758 * Calculate the data return bandwidth used for display (CIK).
759 * Used for display watermark bandwidth calculations
760 * Returns the data return bandwidth in MBytes/s
761 */
762static u32 dce_v10_0_data_return_bandwidth(struct dce10_wm_params *wm)
763{
764 /* Calculate the display Data return Bandwidth */
765 fixed20_12 return_efficiency; /* 0.8 */
766 fixed20_12 sclk, bandwidth;
767 fixed20_12 a;
768
769 a.full = dfixed_const(1000);
770 sclk.full = dfixed_const(wm->sclk);
771 sclk.full = dfixed_div(sclk, a);
772 a.full = dfixed_const(10);
773 return_efficiency.full = dfixed_const(8);
774 return_efficiency.full = dfixed_div(return_efficiency, a);
775 a.full = dfixed_const(32);
776 bandwidth.full = dfixed_mul(a, sclk);
777 bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
778
779 return dfixed_trunc(bandwidth);
780}
781
782/**
783 * dce_v10_0_dmif_request_bandwidth - get the dmif bandwidth
784 *
785 * @wm: watermark calculation data
786 *
787 * Calculate the dmif bandwidth used for display (CIK).
788 * Used for display watermark bandwidth calculations
789 * Returns the dmif bandwidth in MBytes/s
790 */
791static u32 dce_v10_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
792{
793 /* Calculate the DMIF Request Bandwidth */
794 fixed20_12 disp_clk_request_efficiency; /* 0.8 */
795 fixed20_12 disp_clk, bandwidth;
796 fixed20_12 a, b;
797
798 a.full = dfixed_const(1000);
799 disp_clk.full = dfixed_const(wm->disp_clk);
800 disp_clk.full = dfixed_div(disp_clk, a);
801 a.full = dfixed_const(32);
802 b.full = dfixed_mul(a, disp_clk);
803
804 a.full = dfixed_const(10);
805 disp_clk_request_efficiency.full = dfixed_const(8);
806 disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
807
808 bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
809
810 return dfixed_trunc(bandwidth);
811}
812
813/**
814 * dce_v10_0_available_bandwidth - get the min available bandwidth
815 *
816 * @wm: watermark calculation data
817 *
818 * Calculate the min available bandwidth used for display (CIK).
819 * Used for display watermark bandwidth calculations
820 * Returns the min available bandwidth in MBytes/s
821 */
822static u32 dce_v10_0_available_bandwidth(struct dce10_wm_params *wm)
823{
824 /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
825 u32 dram_bandwidth = dce_v10_0_dram_bandwidth(wm);
826 u32 data_return_bandwidth = dce_v10_0_data_return_bandwidth(wm);
827 u32 dmif_req_bandwidth = dce_v10_0_dmif_request_bandwidth(wm);
828
829 return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
830}
831
832/**
833 * dce_v10_0_average_bandwidth - get the average available bandwidth
834 *
835 * @wm: watermark calculation data
836 *
837 * Calculate the average available bandwidth used for display (CIK).
838 * Used for display watermark bandwidth calculations
839 * Returns the average available bandwidth in MBytes/s
840 */
841static u32 dce_v10_0_average_bandwidth(struct dce10_wm_params *wm)
842{
843 /* Calculate the display mode Average Bandwidth
844 * DisplayMode should contain the source and destination dimensions,
845 * timing, etc.
846 */
847 fixed20_12 bpp;
848 fixed20_12 line_time;
849 fixed20_12 src_width;
850 fixed20_12 bandwidth;
851 fixed20_12 a;
852
853 a.full = dfixed_const(1000);
854 line_time.full = dfixed_const(wm->active_time + wm->blank_time);
855 line_time.full = dfixed_div(line_time, a);
856 bpp.full = dfixed_const(wm->bytes_per_pixel);
857 src_width.full = dfixed_const(wm->src_width);
858 bandwidth.full = dfixed_mul(src_width, bpp);
859 bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
860 bandwidth.full = dfixed_div(bandwidth, line_time);
861
862 return dfixed_trunc(bandwidth);
863}
864
865/**
866 * dce_v10_0_latency_watermark - get the latency watermark
867 *
868 * @wm: watermark calculation data
869 *
870 * Calculate the latency watermark (CIK).
871 * Used for display watermark bandwidth calculations
872 * Returns the latency watermark in ns
873 */
874static u32 dce_v10_0_latency_watermark(struct dce10_wm_params *wm)
875{
876 /* First calculate the latency in ns */
877 u32 mc_latency = 2000; /* 2000 ns. */
878 u32 available_bandwidth = dce_v10_0_available_bandwidth(wm);
879 u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
880 u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
881 u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
882 u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
883 (wm->num_heads * cursor_line_pair_return_time);
884 u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
885 u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
886 u32 tmp, dmif_size = 12288;
887 fixed20_12 a, b, c;
888
889 if (wm->num_heads == 0)
890 return 0;
891
892 a.full = dfixed_const(2);
893 b.full = dfixed_const(1);
894 if ((wm->vsc.full > a.full) ||
895 ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
896 (wm->vtaps >= 5) ||
897 ((wm->vsc.full >= a.full) && wm->interlaced))
898 max_src_lines_per_dst_line = 4;
899 else
900 max_src_lines_per_dst_line = 2;
901
902 a.full = dfixed_const(available_bandwidth);
903 b.full = dfixed_const(wm->num_heads);
904 a.full = dfixed_div(a, b);
905 tmp = div_u64((u64) dmif_size * (u64) wm->disp_clk, mc_latency + 512);
906 tmp = min(dfixed_trunc(a), tmp);
907
908 lb_fill_bw = min(tmp, wm->disp_clk * wm->bytes_per_pixel / 1000);
909
910 a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
911 b.full = dfixed_const(1000);
912 c.full = dfixed_const(lb_fill_bw);
913 b.full = dfixed_div(c, b);
914 a.full = dfixed_div(a, b);
915 line_fill_time = dfixed_trunc(a);
916
917 if (line_fill_time < wm->active_time)
918 return latency;
919 else
920 return latency + (line_fill_time - wm->active_time);
921
922}
923
924/**
925 * dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display - check
926 * average and available dram bandwidth
927 *
928 * @wm: watermark calculation data
929 *
930 * Check if the display average bandwidth fits in the display
931 * dram bandwidth (CIK).
932 * Used for display watermark bandwidth calculations
933 * Returns true if the display fits, false if not.
934 */
935static bool dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
936{
937 if (dce_v10_0_average_bandwidth(wm) <=
938 (dce_v10_0_dram_bandwidth_for_display(wm) / wm->num_heads))
939 return true;
940 else
941 return false;
942}
943
944/**
945 * dce_v10_0_average_bandwidth_vs_available_bandwidth - check
946 * average and available bandwidth
947 *
948 * @wm: watermark calculation data
949 *
950 * Check if the display average bandwidth fits in the display
951 * available bandwidth (CIK).
952 * Used for display watermark bandwidth calculations
953 * Returns true if the display fits, false if not.
954 */
955static bool dce_v10_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
956{
957 if (dce_v10_0_average_bandwidth(wm) <=
958 (dce_v10_0_available_bandwidth(wm) / wm->num_heads))
959 return true;
960 else
961 return false;
962}
963
964/**
965 * dce_v10_0_check_latency_hiding - check latency hiding
966 *
967 * @wm: watermark calculation data
968 *
969 * Check latency hiding (CIK).
970 * Used for display watermark bandwidth calculations
971 * Returns true if the display fits, false if not.
972 */
973static bool dce_v10_0_check_latency_hiding(struct dce10_wm_params *wm)
974{
975 u32 lb_partitions = wm->lb_size / wm->src_width;
976 u32 line_time = wm->active_time + wm->blank_time;
977 u32 latency_tolerant_lines;
978 u32 latency_hiding;
979 fixed20_12 a;
980
981 a.full = dfixed_const(1);
982 if (wm->vsc.full > a.full)
983 latency_tolerant_lines = 1;
984 else {
985 if (lb_partitions <= (wm->vtaps + 1))
986 latency_tolerant_lines = 1;
987 else
988 latency_tolerant_lines = 2;
989 }
990
991 latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
992
993 if (dce_v10_0_latency_watermark(wm) <= latency_hiding)
994 return true;
995 else
996 return false;
997}
998
999/**
1000 * dce_v10_0_program_watermarks - program display watermarks
1001 *
1002 * @adev: amdgpu_device pointer
1003 * @amdgpu_crtc: the selected display controller
1004 * @lb_size: line buffer size
1005 * @num_heads: number of display controllers in use
1006 *
1007 * Calculate and program the display watermarks for the
1008 * selected display controller (CIK).
1009 */
1010static void dce_v10_0_program_watermarks(struct amdgpu_device *adev,
1011 struct amdgpu_crtc *amdgpu_crtc,
1012 u32 lb_size, u32 num_heads)
1013{
1014 struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
1015 struct dce10_wm_params wm_low, wm_high;
1016 u32 active_time;
1017 u32 line_time = 0;
1018 u32 latency_watermark_a = 0, latency_watermark_b = 0;
1019 u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
1020
1021 if (amdgpu_crtc->base.enabled && num_heads && mode) {
1022 active_time = (u32) div_u64((u64)mode->crtc_hdisplay * 1000000,
1023 (u32)mode->clock);
1024 line_time = (u32) div_u64((u64)mode->crtc_htotal * 1000000,
1025 (u32)mode->clock);
1026 line_time = min(line_time, (u32)65535);
1027
1028 /* watermark for high clocks */
1029 if (adev->pm.dpm_enabled) {
1030 wm_high.yclk =
1031 amdgpu_dpm_get_mclk(adev, false) * 10;
1032 wm_high.sclk =
1033 amdgpu_dpm_get_sclk(adev, false) * 10;
1034 } else {
1035 wm_high.yclk = adev->pm.current_mclk * 10;
1036 wm_high.sclk = adev->pm.current_sclk * 10;
1037 }
1038
1039 wm_high.disp_clk = mode->clock;
1040 wm_high.src_width = mode->crtc_hdisplay;
1041 wm_high.active_time = active_time;
1042 wm_high.blank_time = line_time - wm_high.active_time;
1043 wm_high.interlaced = false;
1044 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1045 wm_high.interlaced = true;
1046 wm_high.vsc = amdgpu_crtc->vsc;
1047 wm_high.vtaps = 1;
1048 if (amdgpu_crtc->rmx_type != RMX_OFF)
1049 wm_high.vtaps = 2;
1050 wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
1051 wm_high.lb_size = lb_size;
1052 wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
1053 wm_high.num_heads = num_heads;
1054
1055 /* set for high clocks */
1056 latency_watermark_a = min(dce_v10_0_latency_watermark(&wm_high), (u32)65535);
1057
1058 /* possibly force display priority to high */
1059 /* should really do this at mode validation time... */
1060 if (!dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
1061 !dce_v10_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
1062 !dce_v10_0_check_latency_hiding(&wm_high) ||
1063 (adev->mode_info.disp_priority == 2)) {
1064 DRM_DEBUG_KMS("force priority to high\n");
1065 }
1066
1067 /* watermark for low clocks */
1068 if (adev->pm.dpm_enabled) {
1069 wm_low.yclk =
1070 amdgpu_dpm_get_mclk(adev, true) * 10;
1071 wm_low.sclk =
1072 amdgpu_dpm_get_sclk(adev, true) * 10;
1073 } else {
1074 wm_low.yclk = adev->pm.current_mclk * 10;
1075 wm_low.sclk = adev->pm.current_sclk * 10;
1076 }
1077
1078 wm_low.disp_clk = mode->clock;
1079 wm_low.src_width = mode->crtc_hdisplay;
1080 wm_low.active_time = active_time;
1081 wm_low.blank_time = line_time - wm_low.active_time;
1082 wm_low.interlaced = false;
1083 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1084 wm_low.interlaced = true;
1085 wm_low.vsc = amdgpu_crtc->vsc;
1086 wm_low.vtaps = 1;
1087 if (amdgpu_crtc->rmx_type != RMX_OFF)
1088 wm_low.vtaps = 2;
1089 wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
1090 wm_low.lb_size = lb_size;
1091 wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
1092 wm_low.num_heads = num_heads;
1093
1094 /* set for low clocks */
1095 latency_watermark_b = min(dce_v10_0_latency_watermark(&wm_low), (u32)65535);
1096
1097 /* possibly force display priority to high */
1098 /* should really do this at mode validation time... */
1099 if (!dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
1100 !dce_v10_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
1101 !dce_v10_0_check_latency_hiding(&wm_low) ||
1102 (adev->mode_info.disp_priority == 2)) {
1103 DRM_DEBUG_KMS("force priority to high\n");
1104 }
1105 lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
1106 }
1107
1108 /* select wm A */
1109 wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
1110 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
1111 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1112 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1113 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
1114 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1115 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1116 /* select wm B */
1117 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
1118 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1119 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1120 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
1121 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1122 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1123 /* restore original selection */
1124 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
1125
1126 /* save values for DPM */
1127 amdgpu_crtc->line_time = line_time;
1128 amdgpu_crtc->wm_high = latency_watermark_a;
1129 amdgpu_crtc->wm_low = latency_watermark_b;
1130 /* Save number of lines the linebuffer leads before the scanout */
1131 amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
1132}
1133
1134/**
1135 * dce_v10_0_bandwidth_update - program display watermarks
1136 *
1137 * @adev: amdgpu_device pointer
1138 *
1139 * Calculate and program the display watermarks and line
1140 * buffer allocation (CIK).
1141 */
1142static void dce_v10_0_bandwidth_update(struct amdgpu_device *adev)
1143{
1144 struct drm_display_mode *mode = NULL;
1145 u32 num_heads = 0, lb_size;
1146 int i;
1147
1148 amdgpu_display_update_priority(adev);
1149
1150 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1151 if (adev->mode_info.crtcs[i]->base.enabled)
1152 num_heads++;
1153 }
1154 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1155 mode = &adev->mode_info.crtcs[i]->base.mode;
1156 lb_size = dce_v10_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
1157 dce_v10_0_program_watermarks(adev, adev->mode_info.crtcs[i],
1158 lb_size, num_heads);
1159 }
1160}
1161
1162static void dce_v10_0_audio_get_connected_pins(struct amdgpu_device *adev)
1163{
1164 int i;
1165 u32 offset, tmp;
1166
1167 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1168 offset = adev->mode_info.audio.pin[i].offset;
1169 tmp = RREG32_AUDIO_ENDPT(offset,
1170 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
1171 if (((tmp &
1172 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
1173 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
1174 adev->mode_info.audio.pin[i].connected = false;
1175 else
1176 adev->mode_info.audio.pin[i].connected = true;
1177 }
1178}
1179
1180static struct amdgpu_audio_pin *dce_v10_0_audio_get_pin(struct amdgpu_device *adev)
1181{
1182 int i;
1183
1184 dce_v10_0_audio_get_connected_pins(adev);
1185
1186 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1187 if (adev->mode_info.audio.pin[i].connected)
1188 return &adev->mode_info.audio.pin[i];
1189 }
1190 DRM_ERROR("No connected audio pins found!\n");
1191 return NULL;
1192}
1193
1194static void dce_v10_0_afmt_audio_select_pin(struct drm_encoder *encoder)
1195{
1196 struct amdgpu_device *adev = encoder->dev->dev_private;
1197 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1198 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1199 u32 tmp;
1200
1201 if (!dig || !dig->afmt || !dig->afmt->pin)
1202 return;
1203
1204 tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
1205 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
1206 WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
1207}
1208
1209static void dce_v10_0_audio_write_latency_fields(struct drm_encoder *encoder,
1210 struct drm_display_mode *mode)
1211{
1212 struct amdgpu_device *adev = encoder->dev->dev_private;
1213 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1214 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1215 struct drm_connector *connector;
1216 struct amdgpu_connector *amdgpu_connector = NULL;
1217 u32 tmp;
1218 int interlace = 0;
1219
1220 if (!dig || !dig->afmt || !dig->afmt->pin)
1221 return;
1222
1223 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
1224 if (connector->encoder == encoder) {
1225 amdgpu_connector = to_amdgpu_connector(connector);
1226 break;
1227 }
1228 }
1229
1230 if (!amdgpu_connector) {
1231 DRM_ERROR("Couldn't find encoder's connector\n");
1232 return;
1233 }
1234
1235 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1236 interlace = 1;
1237 if (connector->latency_present[interlace]) {
1238 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1239 VIDEO_LIPSYNC, connector->video_latency[interlace]);
1240 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1241 AUDIO_LIPSYNC, connector->audio_latency[interlace]);
1242 } else {
1243 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1244 VIDEO_LIPSYNC, 0);
1245 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1246 AUDIO_LIPSYNC, 0);
1247 }
1248 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1249 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
1250}
1251
1252static void dce_v10_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
1253{
1254 struct amdgpu_device *adev = encoder->dev->dev_private;
1255 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1256 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1257 struct drm_connector *connector;
1258 struct amdgpu_connector *amdgpu_connector = NULL;
1259 u32 tmp;
1260 u8 *sadb = NULL;
1261 int sad_count;
1262
1263 if (!dig || !dig->afmt || !dig->afmt->pin)
1264 return;
1265
1266 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
1267 if (connector->encoder == encoder) {
1268 amdgpu_connector = to_amdgpu_connector(connector);
1269 break;
1270 }
1271 }
1272
1273 if (!amdgpu_connector) {
1274 DRM_ERROR("Couldn't find encoder's connector\n");
1275 return;
1276 }
1277
1278 sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
1279 if (sad_count < 0) {
1280 DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
1281 sad_count = 0;
1282 }
1283
1284 /* program the speaker allocation */
1285 tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1286 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
1287 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1288 DP_CONNECTION, 0);
1289 /* set HDMI mode */
1290 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1291 HDMI_CONNECTION, 1);
1292 if (sad_count)
1293 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1294 SPEAKER_ALLOCATION, sadb[0]);
1295 else
1296 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1297 SPEAKER_ALLOCATION, 5); /* stereo */
1298 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1299 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
1300
1301 kfree(sadb);
1302}
1303
1304static void dce_v10_0_audio_write_sad_regs(struct drm_encoder *encoder)
1305{
1306 struct amdgpu_device *adev = encoder->dev->dev_private;
1307 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1308 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1309 struct drm_connector *connector;
1310 struct amdgpu_connector *amdgpu_connector = NULL;
1311 struct cea_sad *sads;
1312 int i, sad_count;
1313
1314 static const u16 eld_reg_to_type[][2] = {
1315 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
1316 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
1317 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
1318 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
1319 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
1320 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
1321 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
1322 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
1323 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
1324 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
1325 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
1326 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
1327 };
1328
1329 if (!dig || !dig->afmt || !dig->afmt->pin)
1330 return;
1331
1332 list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
1333 if (connector->encoder == encoder) {
1334 amdgpu_connector = to_amdgpu_connector(connector);
1335 break;
1336 }
1337 }
1338
1339 if (!amdgpu_connector) {
1340 DRM_ERROR("Couldn't find encoder's connector\n");
1341 return;
1342 }
1343
1344 sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
1345 if (sad_count <= 0) {
1346 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
1347 return;
1348 }
1349 BUG_ON(!sads);
1350
1351 for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
1352 u32 tmp = 0;
1353 u8 stereo_freqs = 0;
1354 int max_channels = -1;
1355 int j;
1356
1357 for (j = 0; j < sad_count; j++) {
1358 struct cea_sad *sad = &sads[j];
1359
1360 if (sad->format == eld_reg_to_type[i][1]) {
1361 if (sad->channels > max_channels) {
1362 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1363 MAX_CHANNELS, sad->channels);
1364 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1365 DESCRIPTOR_BYTE_2, sad->byte2);
1366 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1367 SUPPORTED_FREQUENCIES, sad->freq);
1368 max_channels = sad->channels;
1369 }
1370
1371 if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
1372 stereo_freqs |= sad->freq;
1373 else
1374 break;
1375 }
1376 }
1377
1378 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1379 SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
1380 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
1381 }
1382
1383 kfree(sads);
1384}
1385
1386static void dce_v10_0_audio_enable(struct amdgpu_device *adev,
1387 struct amdgpu_audio_pin *pin,
1388 bool enable)
1389{
1390 if (!pin)
1391 return;
1392
1393 WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
1394 enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
1395}
1396
1397static const u32 pin_offsets[] =
1398{
1399 AUD0_REGISTER_OFFSET,
1400 AUD1_REGISTER_OFFSET,
1401 AUD2_REGISTER_OFFSET,
1402 AUD3_REGISTER_OFFSET,
1403 AUD4_REGISTER_OFFSET,
1404 AUD5_REGISTER_OFFSET,
1405 AUD6_REGISTER_OFFSET,
1406};
1407
1408static int dce_v10_0_audio_init(struct amdgpu_device *adev)
1409{
1410 int i;
1411
1412 if (!amdgpu_audio)
1413 return 0;
1414
1415 adev->mode_info.audio.enabled = true;
1416
1417 adev->mode_info.audio.num_pins = 7;
1418
1419 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1420 adev->mode_info.audio.pin[i].channels = -1;
1421 adev->mode_info.audio.pin[i].rate = -1;
1422 adev->mode_info.audio.pin[i].bits_per_sample = -1;
1423 adev->mode_info.audio.pin[i].status_bits = 0;
1424 adev->mode_info.audio.pin[i].category_code = 0;
1425 adev->mode_info.audio.pin[i].connected = false;
1426 adev->mode_info.audio.pin[i].offset = pin_offsets[i];
1427 adev->mode_info.audio.pin[i].id = i;
1428 /* disable audio. it will be set up later */
1429 /* XXX remove once we switch to ip funcs */
1430 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1431 }
1432
1433 return 0;
1434}
1435
1436static void dce_v10_0_audio_fini(struct amdgpu_device *adev)
1437{
1438 int i;
1439
1440 if (!amdgpu_audio)
1441 return;
1442
1443 if (!adev->mode_info.audio.enabled)
1444 return;
1445
1446 for (i = 0; i < adev->mode_info.audio.num_pins; i++)
1447 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1448
1449 adev->mode_info.audio.enabled = false;
1450}
1451
1452/*
1453 * update the N and CTS parameters for a given pixel clock rate
1454 */
1455static void dce_v10_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
1456{
1457 struct drm_device *dev = encoder->dev;
1458 struct amdgpu_device *adev = dev->dev_private;
1459 struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
1460 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1461 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1462 u32 tmp;
1463
1464 tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
1465 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
1466 WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
1467 tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
1468 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
1469 WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
1470
1471 tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
1472 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
1473 WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
1474 tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
1475 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
1476 WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
1477
1478 tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
1479 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
1480 WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
1481 tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
1482 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
1483 WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
1484
1485}
1486
1487/*
1488 * build a HDMI Video Info Frame
1489 */
1490static void dce_v10_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
1491 void *buffer, size_t size)
1492{
1493 struct drm_device *dev = encoder->dev;
1494 struct amdgpu_device *adev = dev->dev_private;
1495 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1496 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1497 uint8_t *frame = buffer + 3;
1498 uint8_t *header = buffer;
1499
1500 WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
1501 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
1502 WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
1503 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
1504 WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
1505 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
1506 WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
1507 frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
1508}
1509
1510static void dce_v10_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
1511{
1512 struct drm_device *dev = encoder->dev;
1513 struct amdgpu_device *adev = dev->dev_private;
1514 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1515 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1516 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1517 u32 dto_phase = 24 * 1000;
1518 u32 dto_modulo = clock;
1519 u32 tmp;
1520
1521 if (!dig || !dig->afmt)
1522 return;
1523
1524 /* XXX two dtos; generally use dto0 for hdmi */
1525 /* Express [24MHz / target pixel clock] as an exact rational
1526 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
1527 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
1528 */
1529 tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
1530 tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
1531 amdgpu_crtc->crtc_id);
1532 WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
1533 WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
1534 WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
1535}
1536
1537/*
1538 * update the info frames with the data from the current display mode
1539 */
1540static void dce_v10_0_afmt_setmode(struct drm_encoder *encoder,
1541 struct drm_display_mode *mode)
1542{
1543 struct drm_device *dev = encoder->dev;
1544 struct amdgpu_device *adev = dev->dev_private;
1545 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1546 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1547 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1548 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
1549 struct hdmi_avi_infoframe frame;
1550 ssize_t err;
1551 u32 tmp;
1552 int bpc = 8;
1553
1554 if (!dig || !dig->afmt)
1555 return;
1556
1557 /* Silent, r600_hdmi_enable will raise WARN for us */
1558 if (!dig->afmt->enabled)
1559 return;
1560
1561 /* hdmi deep color mode general control packets setup, if bpc > 8 */
1562 if (encoder->crtc) {
1563 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1564 bpc = amdgpu_crtc->bpc;
1565 }
1566
1567 /* disable audio prior to setting up hw */
1568 dig->afmt->pin = dce_v10_0_audio_get_pin(adev);
1569 dce_v10_0_audio_enable(adev, dig->afmt->pin, false);
1570
1571 dce_v10_0_audio_set_dto(encoder, mode->clock);
1572
1573 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1574 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
1575 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
1576
1577 WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
1578
1579 tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
1580 switch (bpc) {
1581 case 0:
1582 case 6:
1583 case 8:
1584 case 16:
1585 default:
1586 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
1587 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
1588 DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
1589 connector->name, bpc);
1590 break;
1591 case 10:
1592 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1593 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
1594 DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
1595 connector->name);
1596 break;
1597 case 12:
1598 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1599 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 2);
1600 DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
1601 connector->name);
1602 break;
1603 }
1604 WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
1605
1606 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1607 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
1608 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
1609 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
1610 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
1611
1612 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1613 /* enable audio info frames (frames won't be set until audio is enabled) */
1614 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
1615 /* required for audio info values to be updated */
1616 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
1617 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1618
1619 tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
1620 /* required for audio info values to be updated */
1621 tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
1622 WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1623
1624 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1625 /* anything other than 0 */
1626 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
1627 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1628
1629 WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
1630
1631 tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1632 /* set the default audio delay */
1633 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
1634 /* should be suffient for all audio modes and small enough for all hblanks */
1635 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
1636 WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1637
1638 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1639 /* allow 60958 channel status fields to be updated */
1640 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
1641 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1642
1643 tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
1644 if (bpc > 8)
1645 /* clear SW CTS value */
1646 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
1647 else
1648 /* select SW CTS value */
1649 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
1650 /* allow hw to sent ACR packets when required */
1651 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
1652 WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
1653
1654 dce_v10_0_afmt_update_ACR(encoder, mode->clock);
1655
1656 tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
1657 tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
1658 WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
1659
1660 tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
1661 tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
1662 WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
1663
1664 tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
1665 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
1666 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
1667 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
1668 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
1669 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
1670 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
1671 WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
1672
1673 dce_v10_0_audio_write_speaker_allocation(encoder);
1674
1675 WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
1676 (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
1677
1678 dce_v10_0_afmt_audio_select_pin(encoder);
1679 dce_v10_0_audio_write_sad_regs(encoder);
1680 dce_v10_0_audio_write_latency_fields(encoder, mode);
1681
1682 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
1683 if (err < 0) {
1684 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
1685 return;
1686 }
1687
1688 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1689 if (err < 0) {
1690 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
1691 return;
1692 }
1693
1694 dce_v10_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
1695
1696 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1697 /* enable AVI info frames */
1698 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
1699 /* required for audio info values to be updated */
1700 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
1701 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1702
1703 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1704 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
1705 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1706
1707 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1708 /* send audio packets */
1709 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
1710 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1711
1712 WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
1713 WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
1714 WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
1715 WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
1716
1717 /* enable audio after to setting up hw */
1718 dce_v10_0_audio_enable(adev, dig->afmt->pin, true);
1719}
1720
1721static void dce_v10_0_afmt_enable(struct drm_encoder *encoder, bool enable)
1722{
1723 struct drm_device *dev = encoder->dev;
1724 struct amdgpu_device *adev = dev->dev_private;
1725 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1726 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1727
1728 if (!dig || !dig->afmt)
1729 return;
1730
1731 /* Silent, r600_hdmi_enable will raise WARN for us */
1732 if (enable && dig->afmt->enabled)
1733 return;
1734 if (!enable && !dig->afmt->enabled)
1735 return;
1736
1737 if (!enable && dig->afmt->pin) {
1738 dce_v10_0_audio_enable(adev, dig->afmt->pin, false);
1739 dig->afmt->pin = NULL;
1740 }
1741
1742 dig->afmt->enabled = enable;
1743
1744 DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
1745 enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
1746}
1747
1748static int dce_v10_0_afmt_init(struct amdgpu_device *adev)
1749{
1750 int i;
1751
1752 for (i = 0; i < adev->mode_info.num_dig; i++)
1753 adev->mode_info.afmt[i] = NULL;
1754
1755 /* DCE10 has audio blocks tied to DIG encoders */
1756 for (i = 0; i < adev->mode_info.num_dig; i++) {
1757 adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
1758 if (adev->mode_info.afmt[i]) {
1759 adev->mode_info.afmt[i]->offset = dig_offsets[i];
1760 adev->mode_info.afmt[i]->id = i;
1761 } else {
1762 int j;
1763 for (j = 0; j < i; j++) {
1764 kfree(adev->mode_info.afmt[j]);
1765 adev->mode_info.afmt[j] = NULL;
1766 }
1767 return -ENOMEM;
1768 }
1769 }
1770 return 0;
1771}
1772
1773static void dce_v10_0_afmt_fini(struct amdgpu_device *adev)
1774{
1775 int i;
1776
1777 for (i = 0; i < adev->mode_info.num_dig; i++) {
1778 kfree(adev->mode_info.afmt[i]);
1779 adev->mode_info.afmt[i] = NULL;
1780 }
1781}
1782
1783static const u32 vga_control_regs[6] =
1784{
1785 mmD1VGA_CONTROL,
1786 mmD2VGA_CONTROL,
1787 mmD3VGA_CONTROL,
1788 mmD4VGA_CONTROL,
1789 mmD5VGA_CONTROL,
1790 mmD6VGA_CONTROL,
1791};
1792
1793static void dce_v10_0_vga_enable(struct drm_crtc *crtc, bool enable)
1794{
1795 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1796 struct drm_device *dev = crtc->dev;
1797 struct amdgpu_device *adev = dev->dev_private;
1798 u32 vga_control;
1799
1800 vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
1801 if (enable)
1802 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
1803 else
1804 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
1805}
1806
1807static void dce_v10_0_grph_enable(struct drm_crtc *crtc, bool enable)
1808{
1809 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1810 struct drm_device *dev = crtc->dev;
1811 struct amdgpu_device *adev = dev->dev_private;
1812
1813 if (enable)
1814 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
1815 else
1816 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
1817}
1818
1819static int dce_v10_0_crtc_do_set_base(struct drm_crtc *crtc,
1820 struct drm_framebuffer *fb,
1821 int x, int y, int atomic)
1822{
1823 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1824 struct drm_device *dev = crtc->dev;
1825 struct amdgpu_device *adev = dev->dev_private;
1826 struct amdgpu_framebuffer *amdgpu_fb;
1827 struct drm_framebuffer *target_fb;
1828 struct drm_gem_object *obj;
1829 struct amdgpu_bo *abo;
1830 uint64_t fb_location, tiling_flags;
1831 uint32_t fb_format, fb_pitch_pixels;
1832 u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
1833 u32 pipe_config;
1834 u32 tmp, viewport_w, viewport_h;
1835 int r;
1836 bool bypass_lut = false;
1837 struct drm_format_name_buf format_name;
1838
1839 /* no fb bound */
1840 if (!atomic && !crtc->primary->fb) {
1841 DRM_DEBUG_KMS("No FB bound\n");
1842 return 0;
1843 }
1844
1845 if (atomic) {
1846 amdgpu_fb = to_amdgpu_framebuffer(fb);
1847 target_fb = fb;
1848 } else {
1849 amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
1850 target_fb = crtc->primary->fb;
1851 }
1852
1853 /* If atomic, assume fb object is pinned & idle & fenced and
1854 * just update base pointers
1855 */
1856 obj = amdgpu_fb->obj;
1857 abo = gem_to_amdgpu_bo(obj);
1858 r = amdgpu_bo_reserve(abo, false);
1859 if (unlikely(r != 0))
1860 return r;
1861
1862 if (atomic) {
1863 fb_location = amdgpu_bo_gpu_offset(abo);
1864 } else {
1865 r = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM, &fb_location);
1866 if (unlikely(r != 0)) {
1867 amdgpu_bo_unreserve(abo);
1868 return -EINVAL;
1869 }
1870 }
1871
1872 amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
1873 amdgpu_bo_unreserve(abo);
1874
1875 pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
1876
1877 switch (target_fb->format->format) {
1878 case DRM_FORMAT_C8:
1879 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
1880 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1881 break;
1882 case DRM_FORMAT_XRGB4444:
1883 case DRM_FORMAT_ARGB4444:
1884 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1885 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
1886#ifdef __BIG_ENDIAN
1887 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1888 ENDIAN_8IN16);
1889#endif
1890 break;
1891 case DRM_FORMAT_XRGB1555:
1892 case DRM_FORMAT_ARGB1555:
1893 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1894 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1895#ifdef __BIG_ENDIAN
1896 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1897 ENDIAN_8IN16);
1898#endif
1899 break;
1900 case DRM_FORMAT_BGRX5551:
1901 case DRM_FORMAT_BGRA5551:
1902 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1903 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
1904#ifdef __BIG_ENDIAN
1905 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1906 ENDIAN_8IN16);
1907#endif
1908 break;
1909 case DRM_FORMAT_RGB565:
1910 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1911 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
1912#ifdef __BIG_ENDIAN
1913 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1914 ENDIAN_8IN16);
1915#endif
1916 break;
1917 case DRM_FORMAT_XRGB8888:
1918 case DRM_FORMAT_ARGB8888:
1919 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1920 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1921#ifdef __BIG_ENDIAN
1922 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1923 ENDIAN_8IN32);
1924#endif
1925 break;
1926 case DRM_FORMAT_XRGB2101010:
1927 case DRM_FORMAT_ARGB2101010:
1928 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1929 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
1930#ifdef __BIG_ENDIAN
1931 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1932 ENDIAN_8IN32);
1933#endif
1934 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1935 bypass_lut = true;
1936 break;
1937 case DRM_FORMAT_BGRX1010102:
1938 case DRM_FORMAT_BGRA1010102:
1939 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1940 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
1941#ifdef __BIG_ENDIAN
1942 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1943 ENDIAN_8IN32);
1944#endif
1945 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1946 bypass_lut = true;
1947 break;
1948 default:
1949 DRM_ERROR("Unsupported screen format %s\n",
1950 drm_get_format_name(target_fb->format->format, &format_name));
1951 return -EINVAL;
1952 }
1953
1954 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
1955 unsigned bankw, bankh, mtaspect, tile_split, num_banks;
1956
1957 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
1958 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
1959 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
1960 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
1961 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
1962
1963 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
1964 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
1965 ARRAY_2D_TILED_THIN1);
1966 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
1967 tile_split);
1968 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
1969 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
1970 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
1971 mtaspect);
1972 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
1973 ADDR_SURF_MICRO_TILING_DISPLAY);
1974 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
1975 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
1976 ARRAY_1D_TILED_THIN1);
1977 }
1978
1979 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
1980 pipe_config);
1981
1982 dce_v10_0_vga_enable(crtc, false);
1983
1984 /* Make sure surface address is updated at vertical blank rather than
1985 * horizontal blank
1986 */
1987 tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
1988 tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
1989 GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
1990 WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1991
1992 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
1993 upper_32_bits(fb_location));
1994 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
1995 upper_32_bits(fb_location));
1996 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
1997 (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
1998 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
1999 (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
2000 WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
2001 WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
2002
2003 /*
2004 * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
2005 * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
2006 * retain the full precision throughout the pipeline.
2007 */
2008 tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
2009 if (bypass_lut)
2010 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
2011 else
2012 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
2013 WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
2014
2015 if (bypass_lut)
2016 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
2017
2018 WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
2019 WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
2020 WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
2021 WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
2022 WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
2023 WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
2024
2025 fb_pitch_pixels = target_fb->pitches[0] / target_fb->format->cpp[0];
2026 WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
2027
2028 dce_v10_0_grph_enable(crtc, true);
2029
2030 WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
2031 target_fb->height);
2032
2033 x &= ~3;
2034 y &= ~1;
2035 WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
2036 (x << 16) | y);
2037 viewport_w = crtc->mode.hdisplay;
2038 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
2039 WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
2040 (viewport_w << 16) | viewport_h);
2041
2042 /* set pageflip to happen anywhere in vblank interval */
2043 WREG32(mmMASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 0);
2044
2045 if (!atomic && fb && fb != crtc->primary->fb) {
2046 amdgpu_fb = to_amdgpu_framebuffer(fb);
2047 abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
2048 r = amdgpu_bo_reserve(abo, true);
2049 if (unlikely(r != 0))
2050 return r;
2051 amdgpu_bo_unpin(abo);
2052 amdgpu_bo_unreserve(abo);
2053 }
2054
2055 /* Bytes per pixel may have changed */
2056 dce_v10_0_bandwidth_update(adev);
2057
2058 return 0;
2059}
2060
2061static void dce_v10_0_set_interleave(struct drm_crtc *crtc,
2062 struct drm_display_mode *mode)
2063{
2064 struct drm_device *dev = crtc->dev;
2065 struct amdgpu_device *adev = dev->dev_private;
2066 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2067 u32 tmp;
2068
2069 tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
2070 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2071 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
2072 else
2073 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
2074 WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
2075}
2076
2077static void dce_v10_0_crtc_load_lut(struct drm_crtc *crtc)
2078{
2079 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2080 struct drm_device *dev = crtc->dev;
2081 struct amdgpu_device *adev = dev->dev_private;
2082 u16 *r, *g, *b;
2083 int i;
2084 u32 tmp;
2085
2086 DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
2087
2088 tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2089 tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
2090 tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_OVL_MODE, 0);
2091 WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2092
2093 tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
2094 tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
2095 WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2096
2097 tmp = RREG32(mmPRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset);
2098 tmp = REG_SET_FIELD(tmp, PRESCALE_OVL_CONTROL, OVL_PRESCALE_BYPASS, 1);
2099 WREG32(mmPRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2100
2101 tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2102 tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
2103 tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, OVL_INPUT_GAMMA_MODE, 0);
2104 WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2105
2106 WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
2107
2108 WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
2109 WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
2110 WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
2111
2112 WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
2113 WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
2114 WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
2115
2116 WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
2117 WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
2118
2119 WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
2120 r = crtc->gamma_store;
2121 g = r + crtc->gamma_size;
2122 b = g + crtc->gamma_size;
2123 for (i = 0; i < 256; i++) {
2124 WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
2125 ((*r++ & 0xffc0) << 14) |
2126 ((*g++ & 0xffc0) << 4) |
2127 (*b++ >> 6));
2128 }
2129
2130 tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2131 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
2132 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, OVL_DEGAMMA_MODE, 0);
2133 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
2134 WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2135
2136 tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
2137 tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
2138 tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, OVL_GAMUT_REMAP_MODE, 0);
2139 WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2140
2141 tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2142 tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
2143 tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, OVL_REGAMMA_MODE, 0);
2144 WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2145
2146 tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2147 tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
2148 tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_OVL_MODE, 0);
2149 WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2150
2151 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
2152 WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
2153 /* XXX this only needs to be programmed once per crtc at startup,
2154 * not sure where the best place for it is
2155 */
2156 tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
2157 tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
2158 WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2159}
2160
2161static int dce_v10_0_pick_dig_encoder(struct drm_encoder *encoder)
2162{
2163 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
2164 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
2165
2166 switch (amdgpu_encoder->encoder_id) {
2167 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2168 if (dig->linkb)
2169 return 1;
2170 else
2171 return 0;
2172 break;
2173 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2174 if (dig->linkb)
2175 return 3;
2176 else
2177 return 2;
2178 break;
2179 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2180 if (dig->linkb)
2181 return 5;
2182 else
2183 return 4;
2184 break;
2185 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2186 return 6;
2187 break;
2188 default:
2189 DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
2190 return 0;
2191 }
2192}
2193
2194/**
2195 * dce_v10_0_pick_pll - Allocate a PPLL for use by the crtc.
2196 *
2197 * @crtc: drm crtc
2198 *
2199 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
2200 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
2201 * monitors a dedicated PPLL must be used. If a particular board has
2202 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
2203 * as there is no need to program the PLL itself. If we are not able to
2204 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
2205 * avoid messing up an existing monitor.
2206 *
2207 * Asic specific PLL information
2208 *
2209 * DCE 10.x
2210 * Tonga
2211 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
2212 * CI
2213 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
2214 *
2215 */
2216static u32 dce_v10_0_pick_pll(struct drm_crtc *crtc)
2217{
2218 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2219 struct drm_device *dev = crtc->dev;
2220 struct amdgpu_device *adev = dev->dev_private;
2221 u32 pll_in_use;
2222 int pll;
2223
2224 if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
2225 if (adev->clock.dp_extclk)
2226 /* skip PPLL programming if using ext clock */
2227 return ATOM_PPLL_INVALID;
2228 else {
2229 /* use the same PPLL for all DP monitors */
2230 pll = amdgpu_pll_get_shared_dp_ppll(crtc);
2231 if (pll != ATOM_PPLL_INVALID)
2232 return pll;
2233 }
2234 } else {
2235 /* use the same PPLL for all monitors with the same clock */
2236 pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
2237 if (pll != ATOM_PPLL_INVALID)
2238 return pll;
2239 }
2240
2241 /* DCE10 has PPLL0, PPLL1, and PPLL2 */
2242 pll_in_use = amdgpu_pll_get_use_mask(crtc);
2243 if (!(pll_in_use & (1 << ATOM_PPLL2)))
2244 return ATOM_PPLL2;
2245 if (!(pll_in_use & (1 << ATOM_PPLL1)))
2246 return ATOM_PPLL1;
2247 if (!(pll_in_use & (1 << ATOM_PPLL0)))
2248 return ATOM_PPLL0;
2249 DRM_ERROR("unable to allocate a PPLL\n");
2250 return ATOM_PPLL_INVALID;
2251}
2252
2253static void dce_v10_0_lock_cursor(struct drm_crtc *crtc, bool lock)
2254{
2255 struct amdgpu_device *adev = crtc->dev->dev_private;
2256 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2257 uint32_t cur_lock;
2258
2259 cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
2260 if (lock)
2261 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
2262 else
2263 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
2264 WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
2265}
2266
2267static void dce_v10_0_hide_cursor(struct drm_crtc *crtc)
2268{
2269 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2270 struct amdgpu_device *adev = crtc->dev->dev_private;
2271 u32 tmp;
2272
2273 tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2274 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
2275 WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2276}
2277
2278static void dce_v10_0_show_cursor(struct drm_crtc *crtc)
2279{
2280 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2281 struct amdgpu_device *adev = crtc->dev->dev_private;
2282 u32 tmp;
2283
2284 WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2285 upper_32_bits(amdgpu_crtc->cursor_addr));
2286 WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2287 lower_32_bits(amdgpu_crtc->cursor_addr));
2288
2289 tmp = RREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2290 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
2291 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
2292 WREG32_IDX(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2293}
2294
2295static int dce_v10_0_cursor_move_locked(struct drm_crtc *crtc,
2296 int x, int y)
2297{
2298 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2299 struct amdgpu_device *adev = crtc->dev->dev_private;
2300 int xorigin = 0, yorigin = 0;
2301
2302 amdgpu_crtc->cursor_x = x;
2303 amdgpu_crtc->cursor_y = y;
2304
2305 /* avivo cursor are offset into the total surface */
2306 x += crtc->x;
2307 y += crtc->y;
2308 DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
2309
2310 if (x < 0) {
2311 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
2312 x = 0;
2313 }
2314 if (y < 0) {
2315 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
2316 y = 0;
2317 }
2318
2319 WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
2320 WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
2321 WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
2322 ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
2323
2324 return 0;
2325}
2326
2327static int dce_v10_0_crtc_cursor_move(struct drm_crtc *crtc,
2328 int x, int y)
2329{
2330 int ret;
2331
2332 dce_v10_0_lock_cursor(crtc, true);
2333 ret = dce_v10_0_cursor_move_locked(crtc, x, y);
2334 dce_v10_0_lock_cursor(crtc, false);
2335
2336 return ret;
2337}
2338
2339static int dce_v10_0_crtc_cursor_set2(struct drm_crtc *crtc,
2340 struct drm_file *file_priv,
2341 uint32_t handle,
2342 uint32_t width,
2343 uint32_t height,
2344 int32_t hot_x,
2345 int32_t hot_y)
2346{
2347 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2348 struct drm_gem_object *obj;
2349 struct amdgpu_bo *aobj;
2350 int ret;
2351
2352 if (!handle) {
2353 /* turn off cursor */
2354 dce_v10_0_hide_cursor(crtc);
2355 obj = NULL;
2356 goto unpin;
2357 }
2358
2359 if ((width > amdgpu_crtc->max_cursor_width) ||
2360 (height > amdgpu_crtc->max_cursor_height)) {
2361 DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
2362 return -EINVAL;
2363 }
2364
2365 obj = drm_gem_object_lookup(file_priv, handle);
2366 if (!obj) {
2367 DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
2368 return -ENOENT;
2369 }
2370
2371 aobj = gem_to_amdgpu_bo(obj);
2372 ret = amdgpu_bo_reserve(aobj, false);
2373 if (ret != 0) {
2374 drm_gem_object_put_unlocked(obj);
2375 return ret;
2376 }
2377
2378 ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM, &amdgpu_crtc->cursor_addr);
2379 amdgpu_bo_unreserve(aobj);
2380 if (ret) {
2381 DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
2382 drm_gem_object_put_unlocked(obj);
2383 return ret;
2384 }
2385
2386 dce_v10_0_lock_cursor(crtc, true);
2387
2388 if (width != amdgpu_crtc->cursor_width ||
2389 height != amdgpu_crtc->cursor_height ||
2390 hot_x != amdgpu_crtc->cursor_hot_x ||
2391 hot_y != amdgpu_crtc->cursor_hot_y) {
2392 int x, y;
2393
2394 x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
2395 y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
2396
2397 dce_v10_0_cursor_move_locked(crtc, x, y);
2398
2399 amdgpu_crtc->cursor_width = width;
2400 amdgpu_crtc->cursor_height = height;
2401 amdgpu_crtc->cursor_hot_x = hot_x;
2402 amdgpu_crtc->cursor_hot_y = hot_y;
2403 }
2404
2405 dce_v10_0_show_cursor(crtc);
2406 dce_v10_0_lock_cursor(crtc, false);
2407
2408unpin:
2409 if (amdgpu_crtc->cursor_bo) {
2410 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2411 ret = amdgpu_bo_reserve(aobj, true);
2412 if (likely(ret == 0)) {
2413 amdgpu_bo_unpin(aobj);
2414 amdgpu_bo_unreserve(aobj);
2415 }
2416 drm_gem_object_put_unlocked(amdgpu_crtc->cursor_bo);
2417 }
2418
2419 amdgpu_crtc->cursor_bo = obj;
2420 return 0;
2421}
2422
2423static void dce_v10_0_cursor_reset(struct drm_crtc *crtc)
2424{
2425 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2426
2427 if (amdgpu_crtc->cursor_bo) {
2428 dce_v10_0_lock_cursor(crtc, true);
2429
2430 dce_v10_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
2431 amdgpu_crtc->cursor_y);
2432
2433 dce_v10_0_show_cursor(crtc);
2434
2435 dce_v10_0_lock_cursor(crtc, false);
2436 }
2437}
2438
2439static int dce_v10_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
2440 u16 *blue, uint32_t size,
2441 struct drm_modeset_acquire_ctx *ctx)
2442{
2443 dce_v10_0_crtc_load_lut(crtc);
2444
2445 return 0;
2446}
2447
2448static void dce_v10_0_crtc_destroy(struct drm_crtc *crtc)
2449{
2450 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2451
2452 drm_crtc_cleanup(crtc);
2453 kfree(amdgpu_crtc);
2454}
2455
2456static const struct drm_crtc_funcs dce_v10_0_crtc_funcs = {
2457 .cursor_set2 = dce_v10_0_crtc_cursor_set2,
2458 .cursor_move = dce_v10_0_crtc_cursor_move,
2459 .gamma_set = dce_v10_0_crtc_gamma_set,
2460 .set_config = amdgpu_display_crtc_set_config,
2461 .destroy = dce_v10_0_crtc_destroy,
2462 .page_flip_target = amdgpu_display_crtc_page_flip_target,
2463};
2464
2465static void dce_v10_0_crtc_dpms(struct drm_crtc *crtc, int mode)
2466{
2467 struct drm_device *dev = crtc->dev;
2468 struct amdgpu_device *adev = dev->dev_private;
2469 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2470 unsigned type;
2471
2472 switch (mode) {
2473 case DRM_MODE_DPMS_ON:
2474 amdgpu_crtc->enabled = true;
2475 amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
2476 dce_v10_0_vga_enable(crtc, true);
2477 amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
2478 dce_v10_0_vga_enable(crtc, false);
2479 /* Make sure VBLANK and PFLIP interrupts are still enabled */
2480 type = amdgpu_display_crtc_idx_to_irq_type(adev,
2481 amdgpu_crtc->crtc_id);
2482 amdgpu_irq_update(adev, &adev->crtc_irq, type);
2483 amdgpu_irq_update(adev, &adev->pageflip_irq, type);
2484 drm_crtc_vblank_on(crtc);
2485 dce_v10_0_crtc_load_lut(crtc);
2486 break;
2487 case DRM_MODE_DPMS_STANDBY:
2488 case DRM_MODE_DPMS_SUSPEND:
2489 case DRM_MODE_DPMS_OFF:
2490 drm_crtc_vblank_off(crtc);
2491 if (amdgpu_crtc->enabled) {
2492 dce_v10_0_vga_enable(crtc, true);
2493 amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
2494 dce_v10_0_vga_enable(crtc, false);
2495 }
2496 amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
2497 amdgpu_crtc->enabled = false;
2498 break;
2499 }
2500 /* adjust pm to dpms */
2501 amdgpu_pm_compute_clocks(adev);
2502}
2503
2504static void dce_v10_0_crtc_prepare(struct drm_crtc *crtc)
2505{
2506 /* disable crtc pair power gating before programming */
2507 amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
2508 amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
2509 dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2510}
2511
2512static void dce_v10_0_crtc_commit(struct drm_crtc *crtc)
2513{
2514 dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
2515 amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
2516}
2517
2518static void dce_v10_0_crtc_disable(struct drm_crtc *crtc)
2519{
2520 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2521 struct drm_device *dev = crtc->dev;
2522 struct amdgpu_device *adev = dev->dev_private;
2523 struct amdgpu_atom_ss ss;
2524 int i;
2525
2526 dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2527 if (crtc->primary->fb) {
2528 int r;
2529 struct amdgpu_framebuffer *amdgpu_fb;
2530 struct amdgpu_bo *abo;
2531
2532 amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
2533 abo = gem_to_amdgpu_bo(amdgpu_fb->obj);
2534 r = amdgpu_bo_reserve(abo, true);
2535 if (unlikely(r))
2536 DRM_ERROR("failed to reserve abo before unpin\n");
2537 else {
2538 amdgpu_bo_unpin(abo);
2539 amdgpu_bo_unreserve(abo);
2540 }
2541 }
2542 /* disable the GRPH */
2543 dce_v10_0_grph_enable(crtc, false);
2544
2545 amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
2546
2547 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2548 if (adev->mode_info.crtcs[i] &&
2549 adev->mode_info.crtcs[i]->enabled &&
2550 i != amdgpu_crtc->crtc_id &&
2551 amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
2552 /* one other crtc is using this pll don't turn
2553 * off the pll
2554 */
2555 goto done;
2556 }
2557 }
2558
2559 switch (amdgpu_crtc->pll_id) {
2560 case ATOM_PPLL0:
2561 case ATOM_PPLL1:
2562 case ATOM_PPLL2:
2563 /* disable the ppll */
2564 amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
2565 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2566 break;
2567 default:
2568 break;
2569 }
2570done:
2571 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2572 amdgpu_crtc->adjusted_clock = 0;
2573 amdgpu_crtc->encoder = NULL;
2574 amdgpu_crtc->connector = NULL;
2575}
2576
2577static int dce_v10_0_crtc_mode_set(struct drm_crtc *crtc,
2578 struct drm_display_mode *mode,
2579 struct drm_display_mode *adjusted_mode,
2580 int x, int y, struct drm_framebuffer *old_fb)
2581{
2582 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2583
2584 if (!amdgpu_crtc->adjusted_clock)
2585 return -EINVAL;
2586
2587 amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
2588 amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
2589 dce_v10_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2590 amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
2591 amdgpu_atombios_crtc_scaler_setup(crtc);
2592 dce_v10_0_cursor_reset(crtc);
2593 /* update the hw version fpr dpm */
2594 amdgpu_crtc->hw_mode = *adjusted_mode;
2595
2596 return 0;
2597}
2598
2599static bool dce_v10_0_crtc_mode_fixup(struct drm_crtc *crtc,
2600 const struct drm_display_mode *mode,
2601 struct drm_display_mode *adjusted_mode)
2602{
2603 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2604 struct drm_device *dev = crtc->dev;
2605 struct drm_encoder *encoder;
2606
2607 /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
2608 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2609 if (encoder->crtc == crtc) {
2610 amdgpu_crtc->encoder = encoder;
2611 amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
2612 break;
2613 }
2614 }
2615 if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
2616 amdgpu_crtc->encoder = NULL;
2617 amdgpu_crtc->connector = NULL;
2618 return false;
2619 }
2620 if (!amdgpu_display_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2621 return false;
2622 if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
2623 return false;
2624 /* pick pll */
2625 amdgpu_crtc->pll_id = dce_v10_0_pick_pll(crtc);
2626 /* if we can't get a PPLL for a non-DP encoder, fail */
2627 if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
2628 !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
2629 return false;
2630
2631 return true;
2632}
2633
2634static int dce_v10_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
2635 struct drm_framebuffer *old_fb)
2636{
2637 return dce_v10_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2638}
2639
2640static int dce_v10_0_crtc_set_base_atomic(struct drm_crtc *crtc,
2641 struct drm_framebuffer *fb,
2642 int x, int y, enum mode_set_atomic state)
2643{
2644 return dce_v10_0_crtc_do_set_base(crtc, fb, x, y, 1);
2645}
2646
2647static const struct drm_crtc_helper_funcs dce_v10_0_crtc_helper_funcs = {
2648 .dpms = dce_v10_0_crtc_dpms,
2649 .mode_fixup = dce_v10_0_crtc_mode_fixup,
2650 .mode_set = dce_v10_0_crtc_mode_set,
2651 .mode_set_base = dce_v10_0_crtc_set_base,
2652 .mode_set_base_atomic = dce_v10_0_crtc_set_base_atomic,
2653 .prepare = dce_v10_0_crtc_prepare,
2654 .commit = dce_v10_0_crtc_commit,
2655 .disable = dce_v10_0_crtc_disable,
2656};
2657
2658static int dce_v10_0_crtc_init(struct amdgpu_device *adev, int index)
2659{
2660 struct amdgpu_crtc *amdgpu_crtc;
2661
2662 amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
2663 (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
2664 if (amdgpu_crtc == NULL)
2665 return -ENOMEM;
2666
2667 drm_crtc_init(adev->ddev, &amdgpu_crtc->base, &dce_v10_0_crtc_funcs);
2668
2669 drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
2670 amdgpu_crtc->crtc_id = index;
2671 adev->mode_info.crtcs[index] = amdgpu_crtc;
2672
2673 amdgpu_crtc->max_cursor_width = 128;
2674 amdgpu_crtc->max_cursor_height = 128;
2675 adev->ddev->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
2676 adev->ddev->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
2677
2678 switch (amdgpu_crtc->crtc_id) {
2679 case 0:
2680 default:
2681 amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
2682 break;
2683 case 1:
2684 amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
2685 break;
2686 case 2:
2687 amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
2688 break;
2689 case 3:
2690 amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
2691 break;
2692 case 4:
2693 amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
2694 break;
2695 case 5:
2696 amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
2697 break;
2698 }
2699
2700 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2701 amdgpu_crtc->adjusted_clock = 0;
2702 amdgpu_crtc->encoder = NULL;
2703 amdgpu_crtc->connector = NULL;
2704 drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v10_0_crtc_helper_funcs);
2705
2706 return 0;
2707}
2708
2709static int dce_v10_0_early_init(void *handle)
2710{
2711 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2712
2713 adev->audio_endpt_rreg = &dce_v10_0_audio_endpt_rreg;
2714 adev->audio_endpt_wreg = &dce_v10_0_audio_endpt_wreg;
2715
2716 dce_v10_0_set_display_funcs(adev);
2717
2718 adev->mode_info.num_crtc = dce_v10_0_get_num_crtc(adev);
2719
2720 switch (adev->asic_type) {
2721 case CHIP_FIJI:
2722 case CHIP_TONGA:
2723 adev->mode_info.num_hpd = 6;
2724 adev->mode_info.num_dig = 7;
2725 break;
2726 default:
2727 /* FIXME: not supported yet */
2728 return -EINVAL;
2729 }
2730
2731 dce_v10_0_set_irq_funcs(adev);
2732
2733 return 0;
2734}
2735
2736static int dce_v10_0_sw_init(void *handle)
2737{
2738 int r, i;
2739 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2740
2741 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2742 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, i + 1, &adev->crtc_irq);
2743 if (r)
2744 return r;
2745 }
2746
2747 for (i = 8; i < 20; i += 2) {
2748 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, i, &adev->pageflip_irq);
2749 if (r)
2750 return r;
2751 }
2752
2753 /* HPD hotplug */
2754 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 42, &adev->hpd_irq);
2755 if (r)
2756 return r;
2757
2758 adev->ddev->mode_config.funcs = &amdgpu_mode_funcs;
2759
2760 adev->ddev->mode_config.async_page_flip = true;
2761
2762 adev->ddev->mode_config.max_width = 16384;
2763 adev->ddev->mode_config.max_height = 16384;
2764
2765 adev->ddev->mode_config.preferred_depth = 24;
2766 adev->ddev->mode_config.prefer_shadow = 1;
2767
2768 adev->ddev->mode_config.fb_base = adev->gmc.aper_base;
2769
2770 r = amdgpu_display_modeset_create_props(adev);
2771 if (r)
2772 return r;
2773
2774 adev->ddev->mode_config.max_width = 16384;
2775 adev->ddev->mode_config.max_height = 16384;
2776
2777 /* allocate crtcs */
2778 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2779 r = dce_v10_0_crtc_init(adev, i);
2780 if (r)
2781 return r;
2782 }
2783
2784 if (amdgpu_atombios_get_connector_info_from_object_table(adev))
2785 amdgpu_display_print_display_setup(adev->ddev);
2786 else
2787 return -EINVAL;
2788
2789 /* setup afmt */
2790 r = dce_v10_0_afmt_init(adev);
2791 if (r)
2792 return r;
2793
2794 r = dce_v10_0_audio_init(adev);
2795 if (r)
2796 return r;
2797
2798 drm_kms_helper_poll_init(adev->ddev);
2799
2800 adev->mode_info.mode_config_initialized = true;
2801 return 0;
2802}
2803
2804static int dce_v10_0_sw_fini(void *handle)
2805{
2806 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2807
2808 kfree(adev->mode_info.bios_hardcoded_edid);
2809
2810 drm_kms_helper_poll_fini(adev->ddev);
2811
2812 dce_v10_0_audio_fini(adev);
2813
2814 dce_v10_0_afmt_fini(adev);
2815
2816 drm_mode_config_cleanup(adev->ddev);
2817 adev->mode_info.mode_config_initialized = false;
2818
2819 return 0;
2820}
2821
2822static int dce_v10_0_hw_init(void *handle)
2823{
2824 int i;
2825 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2826
2827 dce_v10_0_init_golden_registers(adev);
2828
2829 /* disable vga render */
2830 dce_v10_0_set_vga_render_state(adev, false);
2831 /* init dig PHYs, disp eng pll */
2832 amdgpu_atombios_encoder_init_dig(adev);
2833 amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
2834
2835 /* initialize hpd */
2836 dce_v10_0_hpd_init(adev);
2837
2838 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
2839 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
2840 }
2841
2842 dce_v10_0_pageflip_interrupt_init(adev);
2843
2844 return 0;
2845}
2846
2847static int dce_v10_0_hw_fini(void *handle)
2848{
2849 int i;
2850 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2851
2852 dce_v10_0_hpd_fini(adev);
2853
2854 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
2855 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
2856 }
2857
2858 dce_v10_0_pageflip_interrupt_fini(adev);
2859
2860 return 0;
2861}
2862
2863static int dce_v10_0_suspend(void *handle)
2864{
2865 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2866
2867 adev->mode_info.bl_level =
2868 amdgpu_atombios_encoder_get_backlight_level_from_reg(adev);
2869
2870 return dce_v10_0_hw_fini(handle);
2871}
2872
2873static int dce_v10_0_resume(void *handle)
2874{
2875 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2876 int ret;
2877
2878 amdgpu_atombios_encoder_set_backlight_level_to_reg(adev,
2879 adev->mode_info.bl_level);
2880
2881 ret = dce_v10_0_hw_init(handle);
2882
2883 /* turn on the BL */
2884 if (adev->mode_info.bl_encoder) {
2885 u8 bl_level = amdgpu_display_backlight_get_level(adev,
2886 adev->mode_info.bl_encoder);
2887 amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
2888 bl_level);
2889 }
2890
2891 return ret;
2892}
2893
2894static bool dce_v10_0_is_idle(void *handle)
2895{
2896 return true;
2897}
2898
2899static int dce_v10_0_wait_for_idle(void *handle)
2900{
2901 return 0;
2902}
2903
2904static bool dce_v10_0_check_soft_reset(void *handle)
2905{
2906 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2907
2908 return dce_v10_0_is_display_hung(adev);
2909}
2910
2911static int dce_v10_0_soft_reset(void *handle)
2912{
2913 u32 srbm_soft_reset = 0, tmp;
2914 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2915
2916 if (dce_v10_0_is_display_hung(adev))
2917 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
2918
2919 if (srbm_soft_reset) {
2920 tmp = RREG32(mmSRBM_SOFT_RESET);
2921 tmp |= srbm_soft_reset;
2922 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
2923 WREG32(mmSRBM_SOFT_RESET, tmp);
2924 tmp = RREG32(mmSRBM_SOFT_RESET);
2925
2926 udelay(50);
2927
2928 tmp &= ~srbm_soft_reset;
2929 WREG32(mmSRBM_SOFT_RESET, tmp);
2930 tmp = RREG32(mmSRBM_SOFT_RESET);
2931
2932 /* Wait a little for things to settle down */
2933 udelay(50);
2934 }
2935 return 0;
2936}
2937
2938static void dce_v10_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
2939 int crtc,
2940 enum amdgpu_interrupt_state state)
2941{
2942 u32 lb_interrupt_mask;
2943
2944 if (crtc >= adev->mode_info.num_crtc) {
2945 DRM_DEBUG("invalid crtc %d\n", crtc);
2946 return;
2947 }
2948
2949 switch (state) {
2950 case AMDGPU_IRQ_STATE_DISABLE:
2951 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
2952 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
2953 VBLANK_INTERRUPT_MASK, 0);
2954 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
2955 break;
2956 case AMDGPU_IRQ_STATE_ENABLE:
2957 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
2958 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
2959 VBLANK_INTERRUPT_MASK, 1);
2960 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
2961 break;
2962 default:
2963 break;
2964 }
2965}
2966
2967static void dce_v10_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
2968 int crtc,
2969 enum amdgpu_interrupt_state state)
2970{
2971 u32 lb_interrupt_mask;
2972
2973 if (crtc >= adev->mode_info.num_crtc) {
2974 DRM_DEBUG("invalid crtc %d\n", crtc);
2975 return;
2976 }
2977
2978 switch (state) {
2979 case AMDGPU_IRQ_STATE_DISABLE:
2980 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
2981 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
2982 VLINE_INTERRUPT_MASK, 0);
2983 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
2984 break;
2985 case AMDGPU_IRQ_STATE_ENABLE:
2986 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
2987 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
2988 VLINE_INTERRUPT_MASK, 1);
2989 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
2990 break;
2991 default:
2992 break;
2993 }
2994}
2995
2996static int dce_v10_0_set_hpd_irq_state(struct amdgpu_device *adev,
2997 struct amdgpu_irq_src *source,
2998 unsigned hpd,
2999 enum amdgpu_interrupt_state state)
3000{
3001 u32 tmp;
3002
3003 if (hpd >= adev->mode_info.num_hpd) {
3004 DRM_DEBUG("invalid hdp %d\n", hpd);
3005 return 0;
3006 }
3007
3008 switch (state) {
3009 case AMDGPU_IRQ_STATE_DISABLE:
3010 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3011 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
3012 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3013 break;
3014 case AMDGPU_IRQ_STATE_ENABLE:
3015 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3016 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
3017 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3018 break;
3019 default:
3020 break;
3021 }
3022
3023 return 0;
3024}
3025
3026static int dce_v10_0_set_crtc_irq_state(struct amdgpu_device *adev,
3027 struct amdgpu_irq_src *source,
3028 unsigned type,
3029 enum amdgpu_interrupt_state state)
3030{
3031 switch (type) {
3032 case AMDGPU_CRTC_IRQ_VBLANK1:
3033 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 0, state);
3034 break;
3035 case AMDGPU_CRTC_IRQ_VBLANK2:
3036 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 1, state);
3037 break;
3038 case AMDGPU_CRTC_IRQ_VBLANK3:
3039 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 2, state);
3040 break;
3041 case AMDGPU_CRTC_IRQ_VBLANK4:
3042 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 3, state);
3043 break;
3044 case AMDGPU_CRTC_IRQ_VBLANK5:
3045 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 4, state);
3046 break;
3047 case AMDGPU_CRTC_IRQ_VBLANK6:
3048 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 5, state);
3049 break;
3050 case AMDGPU_CRTC_IRQ_VLINE1:
3051 dce_v10_0_set_crtc_vline_interrupt_state(adev, 0, state);
3052 break;
3053 case AMDGPU_CRTC_IRQ_VLINE2:
3054 dce_v10_0_set_crtc_vline_interrupt_state(adev, 1, state);
3055 break;
3056 case AMDGPU_CRTC_IRQ_VLINE3:
3057 dce_v10_0_set_crtc_vline_interrupt_state(adev, 2, state);
3058 break;
3059 case AMDGPU_CRTC_IRQ_VLINE4:
3060 dce_v10_0_set_crtc_vline_interrupt_state(adev, 3, state);
3061 break;
3062 case AMDGPU_CRTC_IRQ_VLINE5:
3063 dce_v10_0_set_crtc_vline_interrupt_state(adev, 4, state);
3064 break;
3065 case AMDGPU_CRTC_IRQ_VLINE6:
3066 dce_v10_0_set_crtc_vline_interrupt_state(adev, 5, state);
3067 break;
3068 default:
3069 break;
3070 }
3071 return 0;
3072}
3073
3074static int dce_v10_0_set_pageflip_irq_state(struct amdgpu_device *adev,
3075 struct amdgpu_irq_src *src,
3076 unsigned type,
3077 enum amdgpu_interrupt_state state)
3078{
3079 u32 reg;
3080
3081 if (type >= adev->mode_info.num_crtc) {
3082 DRM_ERROR("invalid pageflip crtc %d\n", type);
3083 return -EINVAL;
3084 }
3085
3086 reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
3087 if (state == AMDGPU_IRQ_STATE_DISABLE)
3088 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3089 reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3090 else
3091 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3092 reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3093
3094 return 0;
3095}
3096
3097static int dce_v10_0_pageflip_irq(struct amdgpu_device *adev,
3098 struct amdgpu_irq_src *source,
3099 struct amdgpu_iv_entry *entry)
3100{
3101 unsigned long flags;
3102 unsigned crtc_id;
3103 struct amdgpu_crtc *amdgpu_crtc;
3104 struct amdgpu_flip_work *works;
3105
3106 crtc_id = (entry->src_id - 8) >> 1;
3107 amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
3108
3109 if (crtc_id >= adev->mode_info.num_crtc) {
3110 DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
3111 return -EINVAL;
3112 }
3113
3114 if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
3115 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
3116 WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
3117 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
3118
3119 /* IRQ could occur when in initial stage */
3120 if (amdgpu_crtc == NULL)
3121 return 0;
3122
3123 spin_lock_irqsave(&adev->ddev->event_lock, flags);
3124 works = amdgpu_crtc->pflip_works;
3125 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
3126 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
3127 "AMDGPU_FLIP_SUBMITTED(%d)\n",
3128 amdgpu_crtc->pflip_status,
3129 AMDGPU_FLIP_SUBMITTED);
3130 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
3131 return 0;
3132 }
3133
3134 /* page flip completed. clean up */
3135 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
3136 amdgpu_crtc->pflip_works = NULL;
3137
3138 /* wakeup usersapce */
3139 if (works->event)
3140 drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
3141
3142 spin_unlock_irqrestore(&adev->ddev->event_lock, flags);
3143
3144 drm_crtc_vblank_put(&amdgpu_crtc->base);
3145 schedule_work(&works->unpin_work);
3146
3147 return 0;
3148}
3149
3150static void dce_v10_0_hpd_int_ack(struct amdgpu_device *adev,
3151 int hpd)
3152{
3153 u32 tmp;
3154
3155 if (hpd >= adev->mode_info.num_hpd) {
3156 DRM_DEBUG("invalid hdp %d\n", hpd);
3157 return;
3158 }
3159
3160 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3161 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
3162 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3163}
3164
3165static void dce_v10_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
3166 int crtc)
3167{
3168 u32 tmp;
3169
3170 if (crtc >= adev->mode_info.num_crtc) {
3171 DRM_DEBUG("invalid crtc %d\n", crtc);
3172 return;
3173 }
3174
3175 tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
3176 tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
3177 WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
3178}
3179
3180static void dce_v10_0_crtc_vline_int_ack(struct amdgpu_device *adev,
3181 int crtc)
3182{
3183 u32 tmp;
3184
3185 if (crtc >= adev->mode_info.num_crtc) {
3186 DRM_DEBUG("invalid crtc %d\n", crtc);
3187 return;
3188 }
3189
3190 tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
3191 tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
3192 WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
3193}
3194
3195static int dce_v10_0_crtc_irq(struct amdgpu_device *adev,
3196 struct amdgpu_irq_src *source,
3197 struct amdgpu_iv_entry *entry)
3198{
3199 unsigned crtc = entry->src_id - 1;
3200 uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
3201 unsigned int irq_type = amdgpu_display_crtc_idx_to_irq_type(adev, crtc);
3202
3203 switch (entry->src_data[0]) {
3204 case 0: /* vblank */
3205 if (disp_int & interrupt_status_offsets[crtc].vblank)
3206 dce_v10_0_crtc_vblank_int_ack(adev, crtc);
3207 else
3208 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3209
3210 if (amdgpu_irq_enabled(adev, source, irq_type)) {
3211 drm_handle_vblank(adev->ddev, crtc);
3212 }
3213 DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
3214
3215 break;
3216 case 1: /* vline */
3217 if (disp_int & interrupt_status_offsets[crtc].vline)
3218 dce_v10_0_crtc_vline_int_ack(adev, crtc);
3219 else
3220 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3221
3222 DRM_DEBUG("IH: D%d vline\n", crtc + 1);
3223
3224 break;
3225 default:
3226 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3227 break;
3228 }
3229
3230 return 0;
3231}
3232
3233static int dce_v10_0_hpd_irq(struct amdgpu_device *adev,
3234 struct amdgpu_irq_src *source,
3235 struct amdgpu_iv_entry *entry)
3236{
3237 uint32_t disp_int, mask;
3238 unsigned hpd;
3239
3240 if (entry->src_data[0] >= adev->mode_info.num_hpd) {
3241 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3242 return 0;
3243 }
3244
3245 hpd = entry->src_data[0];
3246 disp_int = RREG32(interrupt_status_offsets[hpd].reg);
3247 mask = interrupt_status_offsets[hpd].hpd;
3248
3249 if (disp_int & mask) {
3250 dce_v10_0_hpd_int_ack(adev, hpd);
3251 schedule_work(&adev->hotplug_work);
3252 DRM_DEBUG("IH: HPD%d\n", hpd + 1);
3253 }
3254
3255 return 0;
3256}
3257
3258static int dce_v10_0_set_clockgating_state(void *handle,
3259 enum amd_clockgating_state state)
3260{
3261 return 0;
3262}
3263
3264static int dce_v10_0_set_powergating_state(void *handle,
3265 enum amd_powergating_state state)
3266{
3267 return 0;
3268}
3269
3270static const struct amd_ip_funcs dce_v10_0_ip_funcs = {
3271 .name = "dce_v10_0",
3272 .early_init = dce_v10_0_early_init,
3273 .late_init = NULL,
3274 .sw_init = dce_v10_0_sw_init,
3275 .sw_fini = dce_v10_0_sw_fini,
3276 .hw_init = dce_v10_0_hw_init,
3277 .hw_fini = dce_v10_0_hw_fini,
3278 .suspend = dce_v10_0_suspend,
3279 .resume = dce_v10_0_resume,
3280 .is_idle = dce_v10_0_is_idle,
3281 .wait_for_idle = dce_v10_0_wait_for_idle,
3282 .check_soft_reset = dce_v10_0_check_soft_reset,
3283 .soft_reset = dce_v10_0_soft_reset,
3284 .set_clockgating_state = dce_v10_0_set_clockgating_state,
3285 .set_powergating_state = dce_v10_0_set_powergating_state,
3286};
3287
3288static void
3289dce_v10_0_encoder_mode_set(struct drm_encoder *encoder,
3290 struct drm_display_mode *mode,
3291 struct drm_display_mode *adjusted_mode)
3292{
3293 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3294
3295 amdgpu_encoder->pixel_clock = adjusted_mode->clock;
3296
3297 /* need to call this here rather than in prepare() since we need some crtc info */
3298 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3299
3300 /* set scaler clears this on some chips */
3301 dce_v10_0_set_interleave(encoder->crtc, mode);
3302
3303 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
3304 dce_v10_0_afmt_enable(encoder, true);
3305 dce_v10_0_afmt_setmode(encoder, adjusted_mode);
3306 }
3307}
3308
3309static void dce_v10_0_encoder_prepare(struct drm_encoder *encoder)
3310{
3311 struct amdgpu_device *adev = encoder->dev->dev_private;
3312 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3313 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
3314
3315 if ((amdgpu_encoder->active_device &
3316 (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
3317 (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
3318 ENCODER_OBJECT_ID_NONE)) {
3319 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
3320 if (dig) {
3321 dig->dig_encoder = dce_v10_0_pick_dig_encoder(encoder);
3322 if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
3323 dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
3324 }
3325 }
3326
3327 amdgpu_atombios_scratch_regs_lock(adev, true);
3328
3329 if (connector) {
3330 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
3331
3332 /* select the clock/data port if it uses a router */
3333 if (amdgpu_connector->router.cd_valid)
3334 amdgpu_i2c_router_select_cd_port(amdgpu_connector);
3335
3336 /* turn eDP panel on for mode set */
3337 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
3338 amdgpu_atombios_encoder_set_edp_panel_power(connector,
3339 ATOM_TRANSMITTER_ACTION_POWER_ON);
3340 }
3341
3342 /* this is needed for the pll/ss setup to work correctly in some cases */
3343 amdgpu_atombios_encoder_set_crtc_source(encoder);
3344 /* set up the FMT blocks */
3345 dce_v10_0_program_fmt(encoder);
3346}
3347
3348static void dce_v10_0_encoder_commit(struct drm_encoder *encoder)
3349{
3350 struct drm_device *dev = encoder->dev;
3351 struct amdgpu_device *adev = dev->dev_private;
3352
3353 /* need to call this here as we need the crtc set up */
3354 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
3355 amdgpu_atombios_scratch_regs_lock(adev, false);
3356}
3357
3358static void dce_v10_0_encoder_disable(struct drm_encoder *encoder)
3359{
3360 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3361 struct amdgpu_encoder_atom_dig *dig;
3362
3363 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3364
3365 if (amdgpu_atombios_encoder_is_digital(encoder)) {
3366 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
3367 dce_v10_0_afmt_enable(encoder, false);
3368 dig = amdgpu_encoder->enc_priv;
3369 dig->dig_encoder = -1;
3370 }
3371 amdgpu_encoder->active_device = 0;
3372}
3373
3374/* these are handled by the primary encoders */
3375static void dce_v10_0_ext_prepare(struct drm_encoder *encoder)
3376{
3377
3378}
3379
3380static void dce_v10_0_ext_commit(struct drm_encoder *encoder)
3381{
3382
3383}
3384
3385static void
3386dce_v10_0_ext_mode_set(struct drm_encoder *encoder,
3387 struct drm_display_mode *mode,
3388 struct drm_display_mode *adjusted_mode)
3389{
3390
3391}
3392
3393static void dce_v10_0_ext_disable(struct drm_encoder *encoder)
3394{
3395
3396}
3397
3398static void
3399dce_v10_0_ext_dpms(struct drm_encoder *encoder, int mode)
3400{
3401
3402}
3403
3404static const struct drm_encoder_helper_funcs dce_v10_0_ext_helper_funcs = {
3405 .dpms = dce_v10_0_ext_dpms,
3406 .prepare = dce_v10_0_ext_prepare,
3407 .mode_set = dce_v10_0_ext_mode_set,
3408 .commit = dce_v10_0_ext_commit,
3409 .disable = dce_v10_0_ext_disable,
3410 /* no detect for TMDS/LVDS yet */
3411};
3412
3413static const struct drm_encoder_helper_funcs dce_v10_0_dig_helper_funcs = {
3414 .dpms = amdgpu_atombios_encoder_dpms,
3415 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3416 .prepare = dce_v10_0_encoder_prepare,
3417 .mode_set = dce_v10_0_encoder_mode_set,
3418 .commit = dce_v10_0_encoder_commit,
3419 .disable = dce_v10_0_encoder_disable,
3420 .detect = amdgpu_atombios_encoder_dig_detect,
3421};
3422
3423static const struct drm_encoder_helper_funcs dce_v10_0_dac_helper_funcs = {
3424 .dpms = amdgpu_atombios_encoder_dpms,
3425 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3426 .prepare = dce_v10_0_encoder_prepare,
3427 .mode_set = dce_v10_0_encoder_mode_set,
3428 .commit = dce_v10_0_encoder_commit,
3429 .detect = amdgpu_atombios_encoder_dac_detect,
3430};
3431
3432static void dce_v10_0_encoder_destroy(struct drm_encoder *encoder)
3433{
3434 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3435 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3436 amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
3437 kfree(amdgpu_encoder->enc_priv);
3438 drm_encoder_cleanup(encoder);
3439 kfree(amdgpu_encoder);
3440}
3441
3442static const struct drm_encoder_funcs dce_v10_0_encoder_funcs = {
3443 .destroy = dce_v10_0_encoder_destroy,
3444};
3445
3446static void dce_v10_0_encoder_add(struct amdgpu_device *adev,
3447 uint32_t encoder_enum,
3448 uint32_t supported_device,
3449 u16 caps)
3450{
3451 struct drm_device *dev = adev->ddev;
3452 struct drm_encoder *encoder;
3453 struct amdgpu_encoder *amdgpu_encoder;
3454
3455 /* see if we already added it */
3456 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3457 amdgpu_encoder = to_amdgpu_encoder(encoder);
3458 if (amdgpu_encoder->encoder_enum == encoder_enum) {
3459 amdgpu_encoder->devices |= supported_device;
3460 return;
3461 }
3462
3463 }
3464
3465 /* add a new one */
3466 amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
3467 if (!amdgpu_encoder)
3468 return;
3469
3470 encoder = &amdgpu_encoder->base;
3471 switch (adev->mode_info.num_crtc) {
3472 case 1:
3473 encoder->possible_crtcs = 0x1;
3474 break;
3475 case 2:
3476 default:
3477 encoder->possible_crtcs = 0x3;
3478 break;
3479 case 4:
3480 encoder->possible_crtcs = 0xf;
3481 break;
3482 case 6:
3483 encoder->possible_crtcs = 0x3f;
3484 break;
3485 }
3486
3487 amdgpu_encoder->enc_priv = NULL;
3488
3489 amdgpu_encoder->encoder_enum = encoder_enum;
3490 amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
3491 amdgpu_encoder->devices = supported_device;
3492 amdgpu_encoder->rmx_type = RMX_OFF;
3493 amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
3494 amdgpu_encoder->is_ext_encoder = false;
3495 amdgpu_encoder->caps = caps;
3496
3497 switch (amdgpu_encoder->encoder_id) {
3498 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
3499 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
3500 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3501 DRM_MODE_ENCODER_DAC, NULL);
3502 drm_encoder_helper_add(encoder, &dce_v10_0_dac_helper_funcs);
3503 break;
3504 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
3505 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
3506 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
3507 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
3508 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
3509 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
3510 amdgpu_encoder->rmx_type = RMX_FULL;
3511 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3512 DRM_MODE_ENCODER_LVDS, NULL);
3513 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
3514 } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
3515 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3516 DRM_MODE_ENCODER_DAC, NULL);
3517 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3518 } else {
3519 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3520 DRM_MODE_ENCODER_TMDS, NULL);
3521 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3522 }
3523 drm_encoder_helper_add(encoder, &dce_v10_0_dig_helper_funcs);
3524 break;
3525 case ENCODER_OBJECT_ID_SI170B:
3526 case ENCODER_OBJECT_ID_CH7303:
3527 case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
3528 case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
3529 case ENCODER_OBJECT_ID_TITFP513:
3530 case ENCODER_OBJECT_ID_VT1623:
3531 case ENCODER_OBJECT_ID_HDMI_SI1930:
3532 case ENCODER_OBJECT_ID_TRAVIS:
3533 case ENCODER_OBJECT_ID_NUTMEG:
3534 /* these are handled by the primary encoders */
3535 amdgpu_encoder->is_ext_encoder = true;
3536 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3537 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3538 DRM_MODE_ENCODER_LVDS, NULL);
3539 else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
3540 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3541 DRM_MODE_ENCODER_DAC, NULL);
3542 else
3543 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3544 DRM_MODE_ENCODER_TMDS, NULL);
3545 drm_encoder_helper_add(encoder, &dce_v10_0_ext_helper_funcs);
3546 break;
3547 }
3548}
3549
3550static const struct amdgpu_display_funcs dce_v10_0_display_funcs = {
3551 .bandwidth_update = &dce_v10_0_bandwidth_update,
3552 .vblank_get_counter = &dce_v10_0_vblank_get_counter,
3553 .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
3554 .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
3555 .hpd_sense = &dce_v10_0_hpd_sense,
3556 .hpd_set_polarity = &dce_v10_0_hpd_set_polarity,
3557 .hpd_get_gpio_reg = &dce_v10_0_hpd_get_gpio_reg,
3558 .page_flip = &dce_v10_0_page_flip,
3559 .page_flip_get_scanoutpos = &dce_v10_0_crtc_get_scanoutpos,
3560 .add_encoder = &dce_v10_0_encoder_add,
3561 .add_connector = &amdgpu_connector_add,
3562};
3563
3564static void dce_v10_0_set_display_funcs(struct amdgpu_device *adev)
3565{
3566 if (adev->mode_info.funcs == NULL)
3567 adev->mode_info.funcs = &dce_v10_0_display_funcs;
3568}
3569
3570static const struct amdgpu_irq_src_funcs dce_v10_0_crtc_irq_funcs = {
3571 .set = dce_v10_0_set_crtc_irq_state,
3572 .process = dce_v10_0_crtc_irq,
3573};
3574
3575static const struct amdgpu_irq_src_funcs dce_v10_0_pageflip_irq_funcs = {
3576 .set = dce_v10_0_set_pageflip_irq_state,
3577 .process = dce_v10_0_pageflip_irq,
3578};
3579
3580static const struct amdgpu_irq_src_funcs dce_v10_0_hpd_irq_funcs = {
3581 .set = dce_v10_0_set_hpd_irq_state,
3582 .process = dce_v10_0_hpd_irq,
3583};
3584
3585static void dce_v10_0_set_irq_funcs(struct amdgpu_device *adev)
3586{
3587 if (adev->mode_info.num_crtc > 0)
3588 adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_VLINE1 + adev->mode_info.num_crtc;
3589 else
3590 adev->crtc_irq.num_types = 0;
3591 adev->crtc_irq.funcs = &dce_v10_0_crtc_irq_funcs;
3592
3593 adev->pageflip_irq.num_types = adev->mode_info.num_crtc;
3594 adev->pageflip_irq.funcs = &dce_v10_0_pageflip_irq_funcs;
3595
3596 adev->hpd_irq.num_types = adev->mode_info.num_hpd;
3597 adev->hpd_irq.funcs = &dce_v10_0_hpd_irq_funcs;
3598}
3599
3600const struct amdgpu_ip_block_version dce_v10_0_ip_block =
3601{
3602 .type = AMD_IP_BLOCK_TYPE_DCE,
3603 .major = 10,
3604 .minor = 0,
3605 .rev = 0,
3606 .funcs = &dce_v10_0_ip_funcs,
3607};
3608
3609const struct amdgpu_ip_block_version dce_v10_1_ip_block =
3610{
3611 .type = AMD_IP_BLOCK_TYPE_DCE,
3612 .major = 10,
3613 .minor = 1,
3614 .rev = 0,
3615 .funcs = &dce_v10_0_ip_funcs,
3616};
1/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 */
23
24#include <drm/drm_edid.h>
25#include <drm/drm_fourcc.h>
26#include <drm/drm_modeset_helper.h>
27#include <drm/drm_modeset_helper_vtables.h>
28#include <drm/drm_vblank.h>
29
30#include "amdgpu.h"
31#include "amdgpu_pm.h"
32#include "amdgpu_i2c.h"
33#include "vid.h"
34#include "atom.h"
35#include "amdgpu_atombios.h"
36#include "atombios_crtc.h"
37#include "atombios_encoders.h"
38#include "amdgpu_pll.h"
39#include "amdgpu_connectors.h"
40#include "amdgpu_display.h"
41#include "dce_v10_0.h"
42
43#include "dce/dce_10_0_d.h"
44#include "dce/dce_10_0_sh_mask.h"
45#include "dce/dce_10_0_enum.h"
46#include "oss/oss_3_0_d.h"
47#include "oss/oss_3_0_sh_mask.h"
48#include "gmc/gmc_8_1_d.h"
49#include "gmc/gmc_8_1_sh_mask.h"
50
51#include "ivsrcid/ivsrcid_vislands30.h"
52
53static void dce_v10_0_set_display_funcs(struct amdgpu_device *adev);
54static void dce_v10_0_set_irq_funcs(struct amdgpu_device *adev);
55
56static const u32 crtc_offsets[] = {
57 CRTC0_REGISTER_OFFSET,
58 CRTC1_REGISTER_OFFSET,
59 CRTC2_REGISTER_OFFSET,
60 CRTC3_REGISTER_OFFSET,
61 CRTC4_REGISTER_OFFSET,
62 CRTC5_REGISTER_OFFSET,
63 CRTC6_REGISTER_OFFSET
64};
65
66static const u32 hpd_offsets[] = {
67 HPD0_REGISTER_OFFSET,
68 HPD1_REGISTER_OFFSET,
69 HPD2_REGISTER_OFFSET,
70 HPD3_REGISTER_OFFSET,
71 HPD4_REGISTER_OFFSET,
72 HPD5_REGISTER_OFFSET
73};
74
75static const uint32_t dig_offsets[] = {
76 DIG0_REGISTER_OFFSET,
77 DIG1_REGISTER_OFFSET,
78 DIG2_REGISTER_OFFSET,
79 DIG3_REGISTER_OFFSET,
80 DIG4_REGISTER_OFFSET,
81 DIG5_REGISTER_OFFSET,
82 DIG6_REGISTER_OFFSET
83};
84
85static const struct {
86 uint32_t reg;
87 uint32_t vblank;
88 uint32_t vline;
89 uint32_t hpd;
90
91} interrupt_status_offsets[] = { {
92 .reg = mmDISP_INTERRUPT_STATUS,
93 .vblank = DISP_INTERRUPT_STATUS__LB_D1_VBLANK_INTERRUPT_MASK,
94 .vline = DISP_INTERRUPT_STATUS__LB_D1_VLINE_INTERRUPT_MASK,
95 .hpd = DISP_INTERRUPT_STATUS__DC_HPD1_INTERRUPT_MASK
96}, {
97 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE,
98 .vblank = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VBLANK_INTERRUPT_MASK,
99 .vline = DISP_INTERRUPT_STATUS_CONTINUE__LB_D2_VLINE_INTERRUPT_MASK,
100 .hpd = DISP_INTERRUPT_STATUS_CONTINUE__DC_HPD2_INTERRUPT_MASK
101}, {
102 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE2,
103 .vblank = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VBLANK_INTERRUPT_MASK,
104 .vline = DISP_INTERRUPT_STATUS_CONTINUE2__LB_D3_VLINE_INTERRUPT_MASK,
105 .hpd = DISP_INTERRUPT_STATUS_CONTINUE2__DC_HPD3_INTERRUPT_MASK
106}, {
107 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE3,
108 .vblank = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VBLANK_INTERRUPT_MASK,
109 .vline = DISP_INTERRUPT_STATUS_CONTINUE3__LB_D4_VLINE_INTERRUPT_MASK,
110 .hpd = DISP_INTERRUPT_STATUS_CONTINUE3__DC_HPD4_INTERRUPT_MASK
111}, {
112 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE4,
113 .vblank = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VBLANK_INTERRUPT_MASK,
114 .vline = DISP_INTERRUPT_STATUS_CONTINUE4__LB_D5_VLINE_INTERRUPT_MASK,
115 .hpd = DISP_INTERRUPT_STATUS_CONTINUE4__DC_HPD5_INTERRUPT_MASK
116}, {
117 .reg = mmDISP_INTERRUPT_STATUS_CONTINUE5,
118 .vblank = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VBLANK_INTERRUPT_MASK,
119 .vline = DISP_INTERRUPT_STATUS_CONTINUE5__LB_D6_VLINE_INTERRUPT_MASK,
120 .hpd = DISP_INTERRUPT_STATUS_CONTINUE5__DC_HPD6_INTERRUPT_MASK
121} };
122
123static const u32 golden_settings_tonga_a11[] = {
124 mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
125 mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
126 mmFBC_MISC, 0x1f311fff, 0x12300000,
127 mmHDMI_CONTROL, 0x31000111, 0x00000011,
128};
129
130static const u32 tonga_mgcg_cgcg_init[] = {
131 mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
132 mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
133};
134
135static const u32 golden_settings_fiji_a10[] = {
136 mmDCI_CLK_CNTL, 0x00000080, 0x00000000,
137 mmFBC_DEBUG_COMP, 0x000000f0, 0x00000070,
138 mmFBC_MISC, 0x1f311fff, 0x12300000,
139 mmHDMI_CONTROL, 0x31000111, 0x00000011,
140};
141
142static const u32 fiji_mgcg_cgcg_init[] = {
143 mmXDMA_CLOCK_GATING_CNTL, 0xffffffff, 0x00000100,
144 mmXDMA_MEM_POWER_CNTL, 0x00000101, 0x00000000,
145};
146
147static void dce_v10_0_init_golden_registers(struct amdgpu_device *adev)
148{
149 switch (adev->asic_type) {
150 case CHIP_FIJI:
151 amdgpu_device_program_register_sequence(adev,
152 fiji_mgcg_cgcg_init,
153 ARRAY_SIZE(fiji_mgcg_cgcg_init));
154 amdgpu_device_program_register_sequence(adev,
155 golden_settings_fiji_a10,
156 ARRAY_SIZE(golden_settings_fiji_a10));
157 break;
158 case CHIP_TONGA:
159 amdgpu_device_program_register_sequence(adev,
160 tonga_mgcg_cgcg_init,
161 ARRAY_SIZE(tonga_mgcg_cgcg_init));
162 amdgpu_device_program_register_sequence(adev,
163 golden_settings_tonga_a11,
164 ARRAY_SIZE(golden_settings_tonga_a11));
165 break;
166 default:
167 break;
168 }
169}
170
171static u32 dce_v10_0_audio_endpt_rreg(struct amdgpu_device *adev,
172 u32 block_offset, u32 reg)
173{
174 unsigned long flags;
175 u32 r;
176
177 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
178 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
179 r = RREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset);
180 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
181
182 return r;
183}
184
185static void dce_v10_0_audio_endpt_wreg(struct amdgpu_device *adev,
186 u32 block_offset, u32 reg, u32 v)
187{
188 unsigned long flags;
189
190 spin_lock_irqsave(&adev->audio_endpt_idx_lock, flags);
191 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
192 WREG32(mmAZALIA_F0_CODEC_ENDPOINT_DATA + block_offset, v);
193 spin_unlock_irqrestore(&adev->audio_endpt_idx_lock, flags);
194}
195
196static u32 dce_v10_0_vblank_get_counter(struct amdgpu_device *adev, int crtc)
197{
198 if (crtc >= adev->mode_info.num_crtc)
199 return 0;
200 else
201 return RREG32(mmCRTC_STATUS_FRAME_COUNT + crtc_offsets[crtc]);
202}
203
204static void dce_v10_0_pageflip_interrupt_init(struct amdgpu_device *adev)
205{
206 unsigned i;
207
208 /* Enable pflip interrupts */
209 for (i = 0; i < adev->mode_info.num_crtc; i++)
210 amdgpu_irq_get(adev, &adev->pageflip_irq, i);
211}
212
213static void dce_v10_0_pageflip_interrupt_fini(struct amdgpu_device *adev)
214{
215 unsigned i;
216
217 /* Disable pflip interrupts */
218 for (i = 0; i < adev->mode_info.num_crtc; i++)
219 amdgpu_irq_put(adev, &adev->pageflip_irq, i);
220}
221
222/**
223 * dce_v10_0_page_flip - pageflip callback.
224 *
225 * @adev: amdgpu_device pointer
226 * @crtc_id: crtc to cleanup pageflip on
227 * @crtc_base: new address of the crtc (GPU MC address)
228 * @async: asynchronous flip
229 *
230 * Triggers the actual pageflip by updating the primary
231 * surface base address.
232 */
233static void dce_v10_0_page_flip(struct amdgpu_device *adev,
234 int crtc_id, u64 crtc_base, bool async)
235{
236 struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
237 struct drm_framebuffer *fb = amdgpu_crtc->base.primary->fb;
238 u32 tmp;
239
240 /* flip at hsync for async, default is vsync */
241 tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
242 tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
243 GRPH_SURFACE_UPDATE_H_RETRACE_EN, async ? 1 : 0);
244 WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
245 /* update pitch */
246 WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset,
247 fb->pitches[0] / fb->format->cpp[0]);
248 /* update the primary scanout address */
249 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
250 upper_32_bits(crtc_base));
251 /* writing to the low address triggers the update */
252 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
253 lower_32_bits(crtc_base));
254 /* post the write */
255 RREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset);
256}
257
258static int dce_v10_0_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc,
259 u32 *vbl, u32 *position)
260{
261 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc))
262 return -EINVAL;
263
264 *vbl = RREG32(mmCRTC_V_BLANK_START_END + crtc_offsets[crtc]);
265 *position = RREG32(mmCRTC_STATUS_POSITION + crtc_offsets[crtc]);
266
267 return 0;
268}
269
270/**
271 * dce_v10_0_hpd_sense - hpd sense callback.
272 *
273 * @adev: amdgpu_device pointer
274 * @hpd: hpd (hotplug detect) pin
275 *
276 * Checks if a digital monitor is connected (evergreen+).
277 * Returns true if connected, false if not connected.
278 */
279static bool dce_v10_0_hpd_sense(struct amdgpu_device *adev,
280 enum amdgpu_hpd_id hpd)
281{
282 bool connected = false;
283
284 if (hpd >= adev->mode_info.num_hpd)
285 return connected;
286
287 if (RREG32(mmDC_HPD_INT_STATUS + hpd_offsets[hpd]) &
288 DC_HPD_INT_STATUS__DC_HPD_SENSE_MASK)
289 connected = true;
290
291 return connected;
292}
293
294/**
295 * dce_v10_0_hpd_set_polarity - hpd set polarity callback.
296 *
297 * @adev: amdgpu_device pointer
298 * @hpd: hpd (hotplug detect) pin
299 *
300 * Set the polarity of the hpd pin (evergreen+).
301 */
302static void dce_v10_0_hpd_set_polarity(struct amdgpu_device *adev,
303 enum amdgpu_hpd_id hpd)
304{
305 u32 tmp;
306 bool connected = dce_v10_0_hpd_sense(adev, hpd);
307
308 if (hpd >= adev->mode_info.num_hpd)
309 return;
310
311 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
312 if (connected)
313 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 0);
314 else
315 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_POLARITY, 1);
316 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
317}
318
319/**
320 * dce_v10_0_hpd_init - hpd setup callback.
321 *
322 * @adev: amdgpu_device pointer
323 *
324 * Setup the hpd pins used by the card (evergreen+).
325 * Enable the pin, set the polarity, and enable the hpd interrupts.
326 */
327static void dce_v10_0_hpd_init(struct amdgpu_device *adev)
328{
329 struct drm_device *dev = adev_to_drm(adev);
330 struct drm_connector *connector;
331 struct drm_connector_list_iter iter;
332 u32 tmp;
333
334 drm_connector_list_iter_begin(dev, &iter);
335 drm_for_each_connector_iter(connector, &iter) {
336 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
337
338 if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
339 continue;
340
341 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP ||
342 connector->connector_type == DRM_MODE_CONNECTOR_LVDS) {
343 /* don't try to enable hpd on eDP or LVDS avoid breaking the
344 * aux dp channel on imac and help (but not completely fix)
345 * https://bugzilla.redhat.com/show_bug.cgi?id=726143
346 * also avoid interrupt storms during dpms.
347 */
348 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
349 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
350 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
351 continue;
352 }
353
354 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
355 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 1);
356 WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
357
358 tmp = RREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd]);
359 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
360 DC_HPD_CONNECT_INT_DELAY,
361 AMDGPU_HPD_CONNECT_INT_DELAY_IN_MS);
362 tmp = REG_SET_FIELD(tmp, DC_HPD_TOGGLE_FILT_CNTL,
363 DC_HPD_DISCONNECT_INT_DELAY,
364 AMDGPU_HPD_DISCONNECT_INT_DELAY_IN_MS);
365 WREG32(mmDC_HPD_TOGGLE_FILT_CNTL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
366
367 dce_v10_0_hpd_set_polarity(adev, amdgpu_connector->hpd.hpd);
368 amdgpu_irq_get(adev, &adev->hpd_irq,
369 amdgpu_connector->hpd.hpd);
370 }
371 drm_connector_list_iter_end(&iter);
372}
373
374/**
375 * dce_v10_0_hpd_fini - hpd tear down callback.
376 *
377 * @adev: amdgpu_device pointer
378 *
379 * Tear down the hpd pins used by the card (evergreen+).
380 * Disable the hpd interrupts.
381 */
382static void dce_v10_0_hpd_fini(struct amdgpu_device *adev)
383{
384 struct drm_device *dev = adev_to_drm(adev);
385 struct drm_connector *connector;
386 struct drm_connector_list_iter iter;
387 u32 tmp;
388
389 drm_connector_list_iter_begin(dev, &iter);
390 drm_for_each_connector_iter(connector, &iter) {
391 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
392
393 if (amdgpu_connector->hpd.hpd >= adev->mode_info.num_hpd)
394 continue;
395
396 tmp = RREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd]);
397 tmp = REG_SET_FIELD(tmp, DC_HPD_CONTROL, DC_HPD_EN, 0);
398 WREG32(mmDC_HPD_CONTROL + hpd_offsets[amdgpu_connector->hpd.hpd], tmp);
399
400 amdgpu_irq_put(adev, &adev->hpd_irq,
401 amdgpu_connector->hpd.hpd);
402 }
403 drm_connector_list_iter_end(&iter);
404}
405
406static u32 dce_v10_0_hpd_get_gpio_reg(struct amdgpu_device *adev)
407{
408 return mmDC_GPIO_HPD_A;
409}
410
411static bool dce_v10_0_is_display_hung(struct amdgpu_device *adev)
412{
413 u32 crtc_hung = 0;
414 u32 crtc_status[6];
415 u32 i, j, tmp;
416
417 for (i = 0; i < adev->mode_info.num_crtc; i++) {
418 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
419 if (REG_GET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN)) {
420 crtc_status[i] = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
421 crtc_hung |= (1 << i);
422 }
423 }
424
425 for (j = 0; j < 10; j++) {
426 for (i = 0; i < adev->mode_info.num_crtc; i++) {
427 if (crtc_hung & (1 << i)) {
428 tmp = RREG32(mmCRTC_STATUS_HV_COUNT + crtc_offsets[i]);
429 if (tmp != crtc_status[i])
430 crtc_hung &= ~(1 << i);
431 }
432 }
433 if (crtc_hung == 0)
434 return false;
435 udelay(100);
436 }
437
438 return true;
439}
440
441static void dce_v10_0_set_vga_render_state(struct amdgpu_device *adev,
442 bool render)
443{
444 u32 tmp;
445
446 /* Lockout access through VGA aperture*/
447 tmp = RREG32(mmVGA_HDP_CONTROL);
448 if (render)
449 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 0);
450 else
451 tmp = REG_SET_FIELD(tmp, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
452 WREG32(mmVGA_HDP_CONTROL, tmp);
453
454 /* disable VGA render */
455 tmp = RREG32(mmVGA_RENDER_CONTROL);
456 if (render)
457 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 1);
458 else
459 tmp = REG_SET_FIELD(tmp, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
460 WREG32(mmVGA_RENDER_CONTROL, tmp);
461}
462
463static int dce_v10_0_get_num_crtc(struct amdgpu_device *adev)
464{
465 int num_crtc = 0;
466
467 switch (adev->asic_type) {
468 case CHIP_FIJI:
469 case CHIP_TONGA:
470 num_crtc = 6;
471 break;
472 default:
473 num_crtc = 0;
474 }
475 return num_crtc;
476}
477
478void dce_v10_0_disable_dce(struct amdgpu_device *adev)
479{
480 /*Disable VGA render and enabled crtc, if has DCE engine*/
481 if (amdgpu_atombios_has_dce_engine_info(adev)) {
482 u32 tmp;
483 int crtc_enabled, i;
484
485 dce_v10_0_set_vga_render_state(adev, false);
486
487 /*Disable crtc*/
488 for (i = 0; i < dce_v10_0_get_num_crtc(adev); i++) {
489 crtc_enabled = REG_GET_FIELD(RREG32(mmCRTC_CONTROL + crtc_offsets[i]),
490 CRTC_CONTROL, CRTC_MASTER_EN);
491 if (crtc_enabled) {
492 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 1);
493 tmp = RREG32(mmCRTC_CONTROL + crtc_offsets[i]);
494 tmp = REG_SET_FIELD(tmp, CRTC_CONTROL, CRTC_MASTER_EN, 0);
495 WREG32(mmCRTC_CONTROL + crtc_offsets[i], tmp);
496 WREG32(mmCRTC_UPDATE_LOCK + crtc_offsets[i], 0);
497 }
498 }
499 }
500}
501
502static void dce_v10_0_program_fmt(struct drm_encoder *encoder)
503{
504 struct drm_device *dev = encoder->dev;
505 struct amdgpu_device *adev = drm_to_adev(dev);
506 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
507 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
508 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
509 int bpc = 0;
510 u32 tmp = 0;
511 enum amdgpu_connector_dither dither = AMDGPU_FMT_DITHER_DISABLE;
512
513 if (connector) {
514 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
515 bpc = amdgpu_connector_get_monitor_bpc(connector);
516 dither = amdgpu_connector->dither;
517 }
518
519 /* LVDS/eDP FMT is set up by atom */
520 if (amdgpu_encoder->devices & ATOM_DEVICE_LCD_SUPPORT)
521 return;
522
523 /* not needed for analog */
524 if ((amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1) ||
525 (amdgpu_encoder->encoder_id == ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2))
526 return;
527
528 if (bpc == 0)
529 return;
530
531 switch (bpc) {
532 case 6:
533 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
534 /* XXX sort out optimal dither settings */
535 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
536 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
537 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
538 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 0);
539 } else {
540 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
541 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 0);
542 }
543 break;
544 case 8:
545 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
546 /* XXX sort out optimal dither settings */
547 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
548 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
549 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
550 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
551 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 1);
552 } else {
553 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
554 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 1);
555 }
556 break;
557 case 10:
558 if (dither == AMDGPU_FMT_DITHER_ENABLE) {
559 /* XXX sort out optimal dither settings */
560 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_FRAME_RANDOM_ENABLE, 1);
561 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_HIGHPASS_RANDOM_ENABLE, 1);
562 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_RGB_RANDOM_ENABLE, 1);
563 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_EN, 1);
564 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_SPATIAL_DITHER_DEPTH, 2);
565 } else {
566 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_EN, 1);
567 tmp = REG_SET_FIELD(tmp, FMT_BIT_DEPTH_CONTROL, FMT_TRUNCATE_DEPTH, 2);
568 }
569 break;
570 default:
571 /* not needed */
572 break;
573 }
574
575 WREG32(mmFMT_BIT_DEPTH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
576}
577
578
579/* display watermark setup */
580/**
581 * dce_v10_0_line_buffer_adjust - Set up the line buffer
582 *
583 * @adev: amdgpu_device pointer
584 * @amdgpu_crtc: the selected display controller
585 * @mode: the current display mode on the selected display
586 * controller
587 *
588 * Setup up the line buffer allocation for
589 * the selected display controller (CIK).
590 * Returns the line buffer size in pixels.
591 */
592static u32 dce_v10_0_line_buffer_adjust(struct amdgpu_device *adev,
593 struct amdgpu_crtc *amdgpu_crtc,
594 struct drm_display_mode *mode)
595{
596 u32 tmp, buffer_alloc, i, mem_cfg;
597 u32 pipe_offset = amdgpu_crtc->crtc_id;
598 /*
599 * Line Buffer Setup
600 * There are 6 line buffers, one for each display controllers.
601 * There are 3 partitions per LB. Select the number of partitions
602 * to enable based on the display width. For display widths larger
603 * than 4096, you need use to use 2 display controllers and combine
604 * them using the stereo blender.
605 */
606 if (amdgpu_crtc->base.enabled && mode) {
607 if (mode->crtc_hdisplay < 1920) {
608 mem_cfg = 1;
609 buffer_alloc = 2;
610 } else if (mode->crtc_hdisplay < 2560) {
611 mem_cfg = 2;
612 buffer_alloc = 2;
613 } else if (mode->crtc_hdisplay < 4096) {
614 mem_cfg = 0;
615 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
616 } else {
617 DRM_DEBUG_KMS("Mode too big for LB!\n");
618 mem_cfg = 0;
619 buffer_alloc = (adev->flags & AMD_IS_APU) ? 2 : 4;
620 }
621 } else {
622 mem_cfg = 1;
623 buffer_alloc = 0;
624 }
625
626 tmp = RREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset);
627 tmp = REG_SET_FIELD(tmp, LB_MEMORY_CTRL, LB_MEMORY_CONFIG, mem_cfg);
628 WREG32(mmLB_MEMORY_CTRL + amdgpu_crtc->crtc_offset, tmp);
629
630 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
631 tmp = REG_SET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATED, buffer_alloc);
632 WREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset, tmp);
633
634 for (i = 0; i < adev->usec_timeout; i++) {
635 tmp = RREG32(mmPIPE0_DMIF_BUFFER_CONTROL + pipe_offset);
636 if (REG_GET_FIELD(tmp, PIPE0_DMIF_BUFFER_CONTROL, DMIF_BUFFERS_ALLOCATION_COMPLETED))
637 break;
638 udelay(1);
639 }
640
641 if (amdgpu_crtc->base.enabled && mode) {
642 switch (mem_cfg) {
643 case 0:
644 default:
645 return 4096 * 2;
646 case 1:
647 return 1920 * 2;
648 case 2:
649 return 2560 * 2;
650 }
651 }
652
653 /* controller not enabled, so no lb used */
654 return 0;
655}
656
657/**
658 * cik_get_number_of_dram_channels - get the number of dram channels
659 *
660 * @adev: amdgpu_device pointer
661 *
662 * Look up the number of video ram channels (CIK).
663 * Used for display watermark bandwidth calculations
664 * Returns the number of dram channels
665 */
666static u32 cik_get_number_of_dram_channels(struct amdgpu_device *adev)
667{
668 u32 tmp = RREG32(mmMC_SHARED_CHMAP);
669
670 switch (REG_GET_FIELD(tmp, MC_SHARED_CHMAP, NOOFCHAN)) {
671 case 0:
672 default:
673 return 1;
674 case 1:
675 return 2;
676 case 2:
677 return 4;
678 case 3:
679 return 8;
680 case 4:
681 return 3;
682 case 5:
683 return 6;
684 case 6:
685 return 10;
686 case 7:
687 return 12;
688 case 8:
689 return 16;
690 }
691}
692
693struct dce10_wm_params {
694 u32 dram_channels; /* number of dram channels */
695 u32 yclk; /* bandwidth per dram data pin in kHz */
696 u32 sclk; /* engine clock in kHz */
697 u32 disp_clk; /* display clock in kHz */
698 u32 src_width; /* viewport width */
699 u32 active_time; /* active display time in ns */
700 u32 blank_time; /* blank time in ns */
701 bool interlaced; /* mode is interlaced */
702 fixed20_12 vsc; /* vertical scale ratio */
703 u32 num_heads; /* number of active crtcs */
704 u32 bytes_per_pixel; /* bytes per pixel display + overlay */
705 u32 lb_size; /* line buffer allocated to pipe */
706 u32 vtaps; /* vertical scaler taps */
707};
708
709/**
710 * dce_v10_0_dram_bandwidth - get the dram bandwidth
711 *
712 * @wm: watermark calculation data
713 *
714 * Calculate the raw dram bandwidth (CIK).
715 * Used for display watermark bandwidth calculations
716 * Returns the dram bandwidth in MBytes/s
717 */
718static u32 dce_v10_0_dram_bandwidth(struct dce10_wm_params *wm)
719{
720 /* Calculate raw DRAM Bandwidth */
721 fixed20_12 dram_efficiency; /* 0.7 */
722 fixed20_12 yclk, dram_channels, bandwidth;
723 fixed20_12 a;
724
725 a.full = dfixed_const(1000);
726 yclk.full = dfixed_const(wm->yclk);
727 yclk.full = dfixed_div(yclk, a);
728 dram_channels.full = dfixed_const(wm->dram_channels * 4);
729 a.full = dfixed_const(10);
730 dram_efficiency.full = dfixed_const(7);
731 dram_efficiency.full = dfixed_div(dram_efficiency, a);
732 bandwidth.full = dfixed_mul(dram_channels, yclk);
733 bandwidth.full = dfixed_mul(bandwidth, dram_efficiency);
734
735 return dfixed_trunc(bandwidth);
736}
737
738/**
739 * dce_v10_0_dram_bandwidth_for_display - get the dram bandwidth for display
740 *
741 * @wm: watermark calculation data
742 *
743 * Calculate the dram bandwidth used for display (CIK).
744 * Used for display watermark bandwidth calculations
745 * Returns the dram bandwidth for display in MBytes/s
746 */
747static u32 dce_v10_0_dram_bandwidth_for_display(struct dce10_wm_params *wm)
748{
749 /* Calculate DRAM Bandwidth and the part allocated to display. */
750 fixed20_12 disp_dram_allocation; /* 0.3 to 0.7 */
751 fixed20_12 yclk, dram_channels, bandwidth;
752 fixed20_12 a;
753
754 a.full = dfixed_const(1000);
755 yclk.full = dfixed_const(wm->yclk);
756 yclk.full = dfixed_div(yclk, a);
757 dram_channels.full = dfixed_const(wm->dram_channels * 4);
758 a.full = dfixed_const(10);
759 disp_dram_allocation.full = dfixed_const(3); /* XXX worse case value 0.3 */
760 disp_dram_allocation.full = dfixed_div(disp_dram_allocation, a);
761 bandwidth.full = dfixed_mul(dram_channels, yclk);
762 bandwidth.full = dfixed_mul(bandwidth, disp_dram_allocation);
763
764 return dfixed_trunc(bandwidth);
765}
766
767/**
768 * dce_v10_0_data_return_bandwidth - get the data return bandwidth
769 *
770 * @wm: watermark calculation data
771 *
772 * Calculate the data return bandwidth used for display (CIK).
773 * Used for display watermark bandwidth calculations
774 * Returns the data return bandwidth in MBytes/s
775 */
776static u32 dce_v10_0_data_return_bandwidth(struct dce10_wm_params *wm)
777{
778 /* Calculate the display Data return Bandwidth */
779 fixed20_12 return_efficiency; /* 0.8 */
780 fixed20_12 sclk, bandwidth;
781 fixed20_12 a;
782
783 a.full = dfixed_const(1000);
784 sclk.full = dfixed_const(wm->sclk);
785 sclk.full = dfixed_div(sclk, a);
786 a.full = dfixed_const(10);
787 return_efficiency.full = dfixed_const(8);
788 return_efficiency.full = dfixed_div(return_efficiency, a);
789 a.full = dfixed_const(32);
790 bandwidth.full = dfixed_mul(a, sclk);
791 bandwidth.full = dfixed_mul(bandwidth, return_efficiency);
792
793 return dfixed_trunc(bandwidth);
794}
795
796/**
797 * dce_v10_0_dmif_request_bandwidth - get the dmif bandwidth
798 *
799 * @wm: watermark calculation data
800 *
801 * Calculate the dmif bandwidth used for display (CIK).
802 * Used for display watermark bandwidth calculations
803 * Returns the dmif bandwidth in MBytes/s
804 */
805static u32 dce_v10_0_dmif_request_bandwidth(struct dce10_wm_params *wm)
806{
807 /* Calculate the DMIF Request Bandwidth */
808 fixed20_12 disp_clk_request_efficiency; /* 0.8 */
809 fixed20_12 disp_clk, bandwidth;
810 fixed20_12 a, b;
811
812 a.full = dfixed_const(1000);
813 disp_clk.full = dfixed_const(wm->disp_clk);
814 disp_clk.full = dfixed_div(disp_clk, a);
815 a.full = dfixed_const(32);
816 b.full = dfixed_mul(a, disp_clk);
817
818 a.full = dfixed_const(10);
819 disp_clk_request_efficiency.full = dfixed_const(8);
820 disp_clk_request_efficiency.full = dfixed_div(disp_clk_request_efficiency, a);
821
822 bandwidth.full = dfixed_mul(b, disp_clk_request_efficiency);
823
824 return dfixed_trunc(bandwidth);
825}
826
827/**
828 * dce_v10_0_available_bandwidth - get the min available bandwidth
829 *
830 * @wm: watermark calculation data
831 *
832 * Calculate the min available bandwidth used for display (CIK).
833 * Used for display watermark bandwidth calculations
834 * Returns the min available bandwidth in MBytes/s
835 */
836static u32 dce_v10_0_available_bandwidth(struct dce10_wm_params *wm)
837{
838 /* Calculate the Available bandwidth. Display can use this temporarily but not in average. */
839 u32 dram_bandwidth = dce_v10_0_dram_bandwidth(wm);
840 u32 data_return_bandwidth = dce_v10_0_data_return_bandwidth(wm);
841 u32 dmif_req_bandwidth = dce_v10_0_dmif_request_bandwidth(wm);
842
843 return min(dram_bandwidth, min(data_return_bandwidth, dmif_req_bandwidth));
844}
845
846/**
847 * dce_v10_0_average_bandwidth - get the average available bandwidth
848 *
849 * @wm: watermark calculation data
850 *
851 * Calculate the average available bandwidth used for display (CIK).
852 * Used for display watermark bandwidth calculations
853 * Returns the average available bandwidth in MBytes/s
854 */
855static u32 dce_v10_0_average_bandwidth(struct dce10_wm_params *wm)
856{
857 /* Calculate the display mode Average Bandwidth
858 * DisplayMode should contain the source and destination dimensions,
859 * timing, etc.
860 */
861 fixed20_12 bpp;
862 fixed20_12 line_time;
863 fixed20_12 src_width;
864 fixed20_12 bandwidth;
865 fixed20_12 a;
866
867 a.full = dfixed_const(1000);
868 line_time.full = dfixed_const(wm->active_time + wm->blank_time);
869 line_time.full = dfixed_div(line_time, a);
870 bpp.full = dfixed_const(wm->bytes_per_pixel);
871 src_width.full = dfixed_const(wm->src_width);
872 bandwidth.full = dfixed_mul(src_width, bpp);
873 bandwidth.full = dfixed_mul(bandwidth, wm->vsc);
874 bandwidth.full = dfixed_div(bandwidth, line_time);
875
876 return dfixed_trunc(bandwidth);
877}
878
879/**
880 * dce_v10_0_latency_watermark - get the latency watermark
881 *
882 * @wm: watermark calculation data
883 *
884 * Calculate the latency watermark (CIK).
885 * Used for display watermark bandwidth calculations
886 * Returns the latency watermark in ns
887 */
888static u32 dce_v10_0_latency_watermark(struct dce10_wm_params *wm)
889{
890 /* First calculate the latency in ns */
891 u32 mc_latency = 2000; /* 2000 ns. */
892 u32 available_bandwidth = dce_v10_0_available_bandwidth(wm);
893 u32 worst_chunk_return_time = (512 * 8 * 1000) / available_bandwidth;
894 u32 cursor_line_pair_return_time = (128 * 4 * 1000) / available_bandwidth;
895 u32 dc_latency = 40000000 / wm->disp_clk; /* dc pipe latency */
896 u32 other_heads_data_return_time = ((wm->num_heads + 1) * worst_chunk_return_time) +
897 (wm->num_heads * cursor_line_pair_return_time);
898 u32 latency = mc_latency + other_heads_data_return_time + dc_latency;
899 u32 max_src_lines_per_dst_line, lb_fill_bw, line_fill_time;
900 u32 tmp, dmif_size = 12288;
901 fixed20_12 a, b, c;
902
903 if (wm->num_heads == 0)
904 return 0;
905
906 a.full = dfixed_const(2);
907 b.full = dfixed_const(1);
908 if ((wm->vsc.full > a.full) ||
909 ((wm->vsc.full > b.full) && (wm->vtaps >= 3)) ||
910 (wm->vtaps >= 5) ||
911 ((wm->vsc.full >= a.full) && wm->interlaced))
912 max_src_lines_per_dst_line = 4;
913 else
914 max_src_lines_per_dst_line = 2;
915
916 a.full = dfixed_const(available_bandwidth);
917 b.full = dfixed_const(wm->num_heads);
918 a.full = dfixed_div(a, b);
919 tmp = div_u64((u64) dmif_size * (u64) wm->disp_clk, mc_latency + 512);
920 tmp = min(dfixed_trunc(a), tmp);
921
922 lb_fill_bw = min(tmp, wm->disp_clk * wm->bytes_per_pixel / 1000);
923
924 a.full = dfixed_const(max_src_lines_per_dst_line * wm->src_width * wm->bytes_per_pixel);
925 b.full = dfixed_const(1000);
926 c.full = dfixed_const(lb_fill_bw);
927 b.full = dfixed_div(c, b);
928 a.full = dfixed_div(a, b);
929 line_fill_time = dfixed_trunc(a);
930
931 if (line_fill_time < wm->active_time)
932 return latency;
933 else
934 return latency + (line_fill_time - wm->active_time);
935
936}
937
938/**
939 * dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display - check
940 * average and available dram bandwidth
941 *
942 * @wm: watermark calculation data
943 *
944 * Check if the display average bandwidth fits in the display
945 * dram bandwidth (CIK).
946 * Used for display watermark bandwidth calculations
947 * Returns true if the display fits, false if not.
948 */
949static bool dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(struct dce10_wm_params *wm)
950{
951 if (dce_v10_0_average_bandwidth(wm) <=
952 (dce_v10_0_dram_bandwidth_for_display(wm) / wm->num_heads))
953 return true;
954 else
955 return false;
956}
957
958/**
959 * dce_v10_0_average_bandwidth_vs_available_bandwidth - check
960 * average and available bandwidth
961 *
962 * @wm: watermark calculation data
963 *
964 * Check if the display average bandwidth fits in the display
965 * available bandwidth (CIK).
966 * Used for display watermark bandwidth calculations
967 * Returns true if the display fits, false if not.
968 */
969static bool dce_v10_0_average_bandwidth_vs_available_bandwidth(struct dce10_wm_params *wm)
970{
971 if (dce_v10_0_average_bandwidth(wm) <=
972 (dce_v10_0_available_bandwidth(wm) / wm->num_heads))
973 return true;
974 else
975 return false;
976}
977
978/**
979 * dce_v10_0_check_latency_hiding - check latency hiding
980 *
981 * @wm: watermark calculation data
982 *
983 * Check latency hiding (CIK).
984 * Used for display watermark bandwidth calculations
985 * Returns true if the display fits, false if not.
986 */
987static bool dce_v10_0_check_latency_hiding(struct dce10_wm_params *wm)
988{
989 u32 lb_partitions = wm->lb_size / wm->src_width;
990 u32 line_time = wm->active_time + wm->blank_time;
991 u32 latency_tolerant_lines;
992 u32 latency_hiding;
993 fixed20_12 a;
994
995 a.full = dfixed_const(1);
996 if (wm->vsc.full > a.full)
997 latency_tolerant_lines = 1;
998 else {
999 if (lb_partitions <= (wm->vtaps + 1))
1000 latency_tolerant_lines = 1;
1001 else
1002 latency_tolerant_lines = 2;
1003 }
1004
1005 latency_hiding = (latency_tolerant_lines * line_time + wm->blank_time);
1006
1007 if (dce_v10_0_latency_watermark(wm) <= latency_hiding)
1008 return true;
1009 else
1010 return false;
1011}
1012
1013/**
1014 * dce_v10_0_program_watermarks - program display watermarks
1015 *
1016 * @adev: amdgpu_device pointer
1017 * @amdgpu_crtc: the selected display controller
1018 * @lb_size: line buffer size
1019 * @num_heads: number of display controllers in use
1020 *
1021 * Calculate and program the display watermarks for the
1022 * selected display controller (CIK).
1023 */
1024static void dce_v10_0_program_watermarks(struct amdgpu_device *adev,
1025 struct amdgpu_crtc *amdgpu_crtc,
1026 u32 lb_size, u32 num_heads)
1027{
1028 struct drm_display_mode *mode = &amdgpu_crtc->base.mode;
1029 struct dce10_wm_params wm_low, wm_high;
1030 u32 active_time;
1031 u32 line_time = 0;
1032 u32 latency_watermark_a = 0, latency_watermark_b = 0;
1033 u32 tmp, wm_mask, lb_vblank_lead_lines = 0;
1034
1035 if (amdgpu_crtc->base.enabled && num_heads && mode) {
1036 active_time = (u32) div_u64((u64)mode->crtc_hdisplay * 1000000,
1037 (u32)mode->clock);
1038 line_time = (u32) div_u64((u64)mode->crtc_htotal * 1000000,
1039 (u32)mode->clock);
1040 line_time = min_t(u32, line_time, 65535);
1041
1042 /* watermark for high clocks */
1043 if (adev->pm.dpm_enabled) {
1044 wm_high.yclk =
1045 amdgpu_dpm_get_mclk(adev, false) * 10;
1046 wm_high.sclk =
1047 amdgpu_dpm_get_sclk(adev, false) * 10;
1048 } else {
1049 wm_high.yclk = adev->pm.current_mclk * 10;
1050 wm_high.sclk = adev->pm.current_sclk * 10;
1051 }
1052
1053 wm_high.disp_clk = mode->clock;
1054 wm_high.src_width = mode->crtc_hdisplay;
1055 wm_high.active_time = active_time;
1056 wm_high.blank_time = line_time - wm_high.active_time;
1057 wm_high.interlaced = false;
1058 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1059 wm_high.interlaced = true;
1060 wm_high.vsc = amdgpu_crtc->vsc;
1061 wm_high.vtaps = 1;
1062 if (amdgpu_crtc->rmx_type != RMX_OFF)
1063 wm_high.vtaps = 2;
1064 wm_high.bytes_per_pixel = 4; /* XXX: get this from fb config */
1065 wm_high.lb_size = lb_size;
1066 wm_high.dram_channels = cik_get_number_of_dram_channels(adev);
1067 wm_high.num_heads = num_heads;
1068
1069 /* set for high clocks */
1070 latency_watermark_a = min_t(u32, dce_v10_0_latency_watermark(&wm_high), 65535);
1071
1072 /* possibly force display priority to high */
1073 /* should really do this at mode validation time... */
1074 if (!dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_high) ||
1075 !dce_v10_0_average_bandwidth_vs_available_bandwidth(&wm_high) ||
1076 !dce_v10_0_check_latency_hiding(&wm_high) ||
1077 (adev->mode_info.disp_priority == 2)) {
1078 DRM_DEBUG_KMS("force priority to high\n");
1079 }
1080
1081 /* watermark for low clocks */
1082 if (adev->pm.dpm_enabled) {
1083 wm_low.yclk =
1084 amdgpu_dpm_get_mclk(adev, true) * 10;
1085 wm_low.sclk =
1086 amdgpu_dpm_get_sclk(adev, true) * 10;
1087 } else {
1088 wm_low.yclk = adev->pm.current_mclk * 10;
1089 wm_low.sclk = adev->pm.current_sclk * 10;
1090 }
1091
1092 wm_low.disp_clk = mode->clock;
1093 wm_low.src_width = mode->crtc_hdisplay;
1094 wm_low.active_time = active_time;
1095 wm_low.blank_time = line_time - wm_low.active_time;
1096 wm_low.interlaced = false;
1097 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1098 wm_low.interlaced = true;
1099 wm_low.vsc = amdgpu_crtc->vsc;
1100 wm_low.vtaps = 1;
1101 if (amdgpu_crtc->rmx_type != RMX_OFF)
1102 wm_low.vtaps = 2;
1103 wm_low.bytes_per_pixel = 4; /* XXX: get this from fb config */
1104 wm_low.lb_size = lb_size;
1105 wm_low.dram_channels = cik_get_number_of_dram_channels(adev);
1106 wm_low.num_heads = num_heads;
1107
1108 /* set for low clocks */
1109 latency_watermark_b = min_t(u32, dce_v10_0_latency_watermark(&wm_low), 65535);
1110
1111 /* possibly force display priority to high */
1112 /* should really do this at mode validation time... */
1113 if (!dce_v10_0_average_bandwidth_vs_dram_bandwidth_for_display(&wm_low) ||
1114 !dce_v10_0_average_bandwidth_vs_available_bandwidth(&wm_low) ||
1115 !dce_v10_0_check_latency_hiding(&wm_low) ||
1116 (adev->mode_info.disp_priority == 2)) {
1117 DRM_DEBUG_KMS("force priority to high\n");
1118 }
1119 lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode->crtc_hdisplay);
1120 }
1121
1122 /* select wm A */
1123 wm_mask = RREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset);
1124 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 1);
1125 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1126 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1127 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_a);
1128 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1129 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1130 /* select wm B */
1131 tmp = REG_SET_FIELD(wm_mask, DPG_WATERMARK_MASK_CONTROL, URGENCY_WATERMARK_MASK, 2);
1132 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1133 tmp = RREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset);
1134 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_LOW_WATERMARK, latency_watermark_b);
1135 tmp = REG_SET_FIELD(tmp, DPG_PIPE_URGENCY_CONTROL, URGENCY_HIGH_WATERMARK, line_time);
1136 WREG32(mmDPG_PIPE_URGENCY_CONTROL + amdgpu_crtc->crtc_offset, tmp);
1137 /* restore original selection */
1138 WREG32(mmDPG_WATERMARK_MASK_CONTROL + amdgpu_crtc->crtc_offset, wm_mask);
1139
1140 /* save values for DPM */
1141 amdgpu_crtc->line_time = line_time;
1142 amdgpu_crtc->wm_high = latency_watermark_a;
1143 amdgpu_crtc->wm_low = latency_watermark_b;
1144 /* Save number of lines the linebuffer leads before the scanout */
1145 amdgpu_crtc->lb_vblank_lead_lines = lb_vblank_lead_lines;
1146}
1147
1148/**
1149 * dce_v10_0_bandwidth_update - program display watermarks
1150 *
1151 * @adev: amdgpu_device pointer
1152 *
1153 * Calculate and program the display watermarks and line
1154 * buffer allocation (CIK).
1155 */
1156static void dce_v10_0_bandwidth_update(struct amdgpu_device *adev)
1157{
1158 struct drm_display_mode *mode = NULL;
1159 u32 num_heads = 0, lb_size;
1160 int i;
1161
1162 amdgpu_display_update_priority(adev);
1163
1164 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1165 if (adev->mode_info.crtcs[i]->base.enabled)
1166 num_heads++;
1167 }
1168 for (i = 0; i < adev->mode_info.num_crtc; i++) {
1169 mode = &adev->mode_info.crtcs[i]->base.mode;
1170 lb_size = dce_v10_0_line_buffer_adjust(adev, adev->mode_info.crtcs[i], mode);
1171 dce_v10_0_program_watermarks(adev, adev->mode_info.crtcs[i],
1172 lb_size, num_heads);
1173 }
1174}
1175
1176static void dce_v10_0_audio_get_connected_pins(struct amdgpu_device *adev)
1177{
1178 int i;
1179 u32 offset, tmp;
1180
1181 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1182 offset = adev->mode_info.audio.pin[i].offset;
1183 tmp = RREG32_AUDIO_ENDPT(offset,
1184 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
1185 if (((tmp &
1186 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY_MASK) >>
1187 AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT__PORT_CONNECTIVITY__SHIFT) == 1)
1188 adev->mode_info.audio.pin[i].connected = false;
1189 else
1190 adev->mode_info.audio.pin[i].connected = true;
1191 }
1192}
1193
1194static struct amdgpu_audio_pin *dce_v10_0_audio_get_pin(struct amdgpu_device *adev)
1195{
1196 int i;
1197
1198 dce_v10_0_audio_get_connected_pins(adev);
1199
1200 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1201 if (adev->mode_info.audio.pin[i].connected)
1202 return &adev->mode_info.audio.pin[i];
1203 }
1204 DRM_ERROR("No connected audio pins found!\n");
1205 return NULL;
1206}
1207
1208static void dce_v10_0_afmt_audio_select_pin(struct drm_encoder *encoder)
1209{
1210 struct amdgpu_device *adev = drm_to_adev(encoder->dev);
1211 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1212 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1213 u32 tmp;
1214
1215 if (!dig || !dig->afmt || !dig->afmt->pin)
1216 return;
1217
1218 tmp = RREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset);
1219 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_SRC_CONTROL, AFMT_AUDIO_SRC_SELECT, dig->afmt->pin->id);
1220 WREG32(mmAFMT_AUDIO_SRC_CONTROL + dig->afmt->offset, tmp);
1221}
1222
1223static void dce_v10_0_audio_write_latency_fields(struct drm_encoder *encoder,
1224 struct drm_display_mode *mode)
1225{
1226 struct drm_device *dev = encoder->dev;
1227 struct amdgpu_device *adev = drm_to_adev(dev);
1228 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1229 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1230 struct drm_connector *connector;
1231 struct drm_connector_list_iter iter;
1232 struct amdgpu_connector *amdgpu_connector = NULL;
1233 u32 tmp;
1234 int interlace = 0;
1235
1236 if (!dig || !dig->afmt || !dig->afmt->pin)
1237 return;
1238
1239 drm_connector_list_iter_begin(dev, &iter);
1240 drm_for_each_connector_iter(connector, &iter) {
1241 if (connector->encoder == encoder) {
1242 amdgpu_connector = to_amdgpu_connector(connector);
1243 break;
1244 }
1245 }
1246 drm_connector_list_iter_end(&iter);
1247
1248 if (!amdgpu_connector) {
1249 DRM_ERROR("Couldn't find encoder's connector\n");
1250 return;
1251 }
1252
1253 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1254 interlace = 1;
1255 if (connector->latency_present[interlace]) {
1256 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1257 VIDEO_LIPSYNC, connector->video_latency[interlace]);
1258 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1259 AUDIO_LIPSYNC, connector->audio_latency[interlace]);
1260 } else {
1261 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1262 VIDEO_LIPSYNC, 0);
1263 tmp = REG_SET_FIELD(0, AZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC,
1264 AUDIO_LIPSYNC, 0);
1265 }
1266 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1267 ixAZALIA_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
1268}
1269
1270static void dce_v10_0_audio_write_speaker_allocation(struct drm_encoder *encoder)
1271{
1272 struct drm_device *dev = encoder->dev;
1273 struct amdgpu_device *adev = drm_to_adev(dev);
1274 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1275 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1276 struct drm_connector *connector;
1277 struct drm_connector_list_iter iter;
1278 struct amdgpu_connector *amdgpu_connector = NULL;
1279 u32 tmp;
1280 u8 *sadb = NULL;
1281 int sad_count;
1282
1283 if (!dig || !dig->afmt || !dig->afmt->pin)
1284 return;
1285
1286 drm_connector_list_iter_begin(dev, &iter);
1287 drm_for_each_connector_iter(connector, &iter) {
1288 if (connector->encoder == encoder) {
1289 amdgpu_connector = to_amdgpu_connector(connector);
1290 break;
1291 }
1292 }
1293 drm_connector_list_iter_end(&iter);
1294
1295 if (!amdgpu_connector) {
1296 DRM_ERROR("Couldn't find encoder's connector\n");
1297 return;
1298 }
1299
1300 sad_count = drm_edid_to_speaker_allocation(amdgpu_connector_edid(connector), &sadb);
1301 if (sad_count < 0) {
1302 DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
1303 sad_count = 0;
1304 }
1305
1306 /* program the speaker allocation */
1307 tmp = RREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1308 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
1309 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1310 DP_CONNECTION, 0);
1311 /* set HDMI mode */
1312 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1313 HDMI_CONNECTION, 1);
1314 if (sad_count)
1315 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1316 SPEAKER_ALLOCATION, sadb[0]);
1317 else
1318 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER,
1319 SPEAKER_ALLOCATION, 5); /* stereo */
1320 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset,
1321 ixAZALIA_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
1322
1323 kfree(sadb);
1324}
1325
1326static void dce_v10_0_audio_write_sad_regs(struct drm_encoder *encoder)
1327{
1328 struct drm_device *dev = encoder->dev;
1329 struct amdgpu_device *adev = drm_to_adev(dev);
1330 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1331 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1332 struct drm_connector *connector;
1333 struct drm_connector_list_iter iter;
1334 struct amdgpu_connector *amdgpu_connector = NULL;
1335 struct cea_sad *sads;
1336 int i, sad_count;
1337
1338 static const u16 eld_reg_to_type[][2] = {
1339 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
1340 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
1341 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
1342 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
1343 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
1344 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
1345 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
1346 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
1347 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
1348 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
1349 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
1350 { ixAZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
1351 };
1352
1353 if (!dig || !dig->afmt || !dig->afmt->pin)
1354 return;
1355
1356 drm_connector_list_iter_begin(dev, &iter);
1357 drm_for_each_connector_iter(connector, &iter) {
1358 if (connector->encoder == encoder) {
1359 amdgpu_connector = to_amdgpu_connector(connector);
1360 break;
1361 }
1362 }
1363 drm_connector_list_iter_end(&iter);
1364
1365 if (!amdgpu_connector) {
1366 DRM_ERROR("Couldn't find encoder's connector\n");
1367 return;
1368 }
1369
1370 sad_count = drm_edid_to_sad(amdgpu_connector_edid(connector), &sads);
1371 if (sad_count < 0)
1372 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
1373 if (sad_count <= 0)
1374 return;
1375 BUG_ON(!sads);
1376
1377 for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
1378 u32 tmp = 0;
1379 u8 stereo_freqs = 0;
1380 int max_channels = -1;
1381 int j;
1382
1383 for (j = 0; j < sad_count; j++) {
1384 struct cea_sad *sad = &sads[j];
1385
1386 if (sad->format == eld_reg_to_type[i][1]) {
1387 if (sad->channels > max_channels) {
1388 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1389 MAX_CHANNELS, sad->channels);
1390 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1391 DESCRIPTOR_BYTE_2, sad->byte2);
1392 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1393 SUPPORTED_FREQUENCIES, sad->freq);
1394 max_channels = sad->channels;
1395 }
1396
1397 if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
1398 stereo_freqs |= sad->freq;
1399 else
1400 break;
1401 }
1402 }
1403
1404 tmp = REG_SET_FIELD(tmp, AZALIA_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0,
1405 SUPPORTED_FREQUENCIES_STEREO, stereo_freqs);
1406 WREG32_AUDIO_ENDPT(dig->afmt->pin->offset, eld_reg_to_type[i][0], tmp);
1407 }
1408
1409 kfree(sads);
1410}
1411
1412static void dce_v10_0_audio_enable(struct amdgpu_device *adev,
1413 struct amdgpu_audio_pin *pin,
1414 bool enable)
1415{
1416 if (!pin)
1417 return;
1418
1419 WREG32_AUDIO_ENDPT(pin->offset, ixAZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
1420 enable ? AZALIA_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL__AUDIO_ENABLED_MASK : 0);
1421}
1422
1423static const u32 pin_offsets[] = {
1424 AUD0_REGISTER_OFFSET,
1425 AUD1_REGISTER_OFFSET,
1426 AUD2_REGISTER_OFFSET,
1427 AUD3_REGISTER_OFFSET,
1428 AUD4_REGISTER_OFFSET,
1429 AUD5_REGISTER_OFFSET,
1430 AUD6_REGISTER_OFFSET,
1431};
1432
1433static int dce_v10_0_audio_init(struct amdgpu_device *adev)
1434{
1435 int i;
1436
1437 if (!amdgpu_audio)
1438 return 0;
1439
1440 adev->mode_info.audio.enabled = true;
1441
1442 adev->mode_info.audio.num_pins = 7;
1443
1444 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
1445 adev->mode_info.audio.pin[i].channels = -1;
1446 adev->mode_info.audio.pin[i].rate = -1;
1447 adev->mode_info.audio.pin[i].bits_per_sample = -1;
1448 adev->mode_info.audio.pin[i].status_bits = 0;
1449 adev->mode_info.audio.pin[i].category_code = 0;
1450 adev->mode_info.audio.pin[i].connected = false;
1451 adev->mode_info.audio.pin[i].offset = pin_offsets[i];
1452 adev->mode_info.audio.pin[i].id = i;
1453 /* disable audio. it will be set up later */
1454 /* XXX remove once we switch to ip funcs */
1455 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1456 }
1457
1458 return 0;
1459}
1460
1461static void dce_v10_0_audio_fini(struct amdgpu_device *adev)
1462{
1463 int i;
1464
1465 if (!amdgpu_audio)
1466 return;
1467
1468 if (!adev->mode_info.audio.enabled)
1469 return;
1470
1471 for (i = 0; i < adev->mode_info.audio.num_pins; i++)
1472 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
1473
1474 adev->mode_info.audio.enabled = false;
1475}
1476
1477/*
1478 * update the N and CTS parameters for a given pixel clock rate
1479 */
1480static void dce_v10_0_afmt_update_ACR(struct drm_encoder *encoder, uint32_t clock)
1481{
1482 struct drm_device *dev = encoder->dev;
1483 struct amdgpu_device *adev = drm_to_adev(dev);
1484 struct amdgpu_afmt_acr acr = amdgpu_afmt_acr(clock);
1485 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1486 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1487 u32 tmp;
1488
1489 tmp = RREG32(mmHDMI_ACR_32_0 + dig->afmt->offset);
1490 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_0, HDMI_ACR_CTS_32, acr.cts_32khz);
1491 WREG32(mmHDMI_ACR_32_0 + dig->afmt->offset, tmp);
1492 tmp = RREG32(mmHDMI_ACR_32_1 + dig->afmt->offset);
1493 tmp = REG_SET_FIELD(tmp, HDMI_ACR_32_1, HDMI_ACR_N_32, acr.n_32khz);
1494 WREG32(mmHDMI_ACR_32_1 + dig->afmt->offset, tmp);
1495
1496 tmp = RREG32(mmHDMI_ACR_44_0 + dig->afmt->offset);
1497 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_0, HDMI_ACR_CTS_44, acr.cts_44_1khz);
1498 WREG32(mmHDMI_ACR_44_0 + dig->afmt->offset, tmp);
1499 tmp = RREG32(mmHDMI_ACR_44_1 + dig->afmt->offset);
1500 tmp = REG_SET_FIELD(tmp, HDMI_ACR_44_1, HDMI_ACR_N_44, acr.n_44_1khz);
1501 WREG32(mmHDMI_ACR_44_1 + dig->afmt->offset, tmp);
1502
1503 tmp = RREG32(mmHDMI_ACR_48_0 + dig->afmt->offset);
1504 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_0, HDMI_ACR_CTS_48, acr.cts_48khz);
1505 WREG32(mmHDMI_ACR_48_0 + dig->afmt->offset, tmp);
1506 tmp = RREG32(mmHDMI_ACR_48_1 + dig->afmt->offset);
1507 tmp = REG_SET_FIELD(tmp, HDMI_ACR_48_1, HDMI_ACR_N_48, acr.n_48khz);
1508 WREG32(mmHDMI_ACR_48_1 + dig->afmt->offset, tmp);
1509
1510}
1511
1512/*
1513 * build a HDMI Video Info Frame
1514 */
1515static void dce_v10_0_afmt_update_avi_infoframe(struct drm_encoder *encoder,
1516 void *buffer, size_t size)
1517{
1518 struct drm_device *dev = encoder->dev;
1519 struct amdgpu_device *adev = drm_to_adev(dev);
1520 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1521 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1522 uint8_t *frame = buffer + 3;
1523 uint8_t *header = buffer;
1524
1525 WREG32(mmAFMT_AVI_INFO0 + dig->afmt->offset,
1526 frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
1527 WREG32(mmAFMT_AVI_INFO1 + dig->afmt->offset,
1528 frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
1529 WREG32(mmAFMT_AVI_INFO2 + dig->afmt->offset,
1530 frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
1531 WREG32(mmAFMT_AVI_INFO3 + dig->afmt->offset,
1532 frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
1533}
1534
1535static void dce_v10_0_audio_set_dto(struct drm_encoder *encoder, u32 clock)
1536{
1537 struct drm_device *dev = encoder->dev;
1538 struct amdgpu_device *adev = drm_to_adev(dev);
1539 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1540 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1541 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1542 u32 dto_phase = 24 * 1000;
1543 u32 dto_modulo = clock;
1544 u32 tmp;
1545
1546 if (!dig || !dig->afmt)
1547 return;
1548
1549 /* XXX two dtos; generally use dto0 for hdmi */
1550 /* Express [24MHz / target pixel clock] as an exact rational
1551 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
1552 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
1553 */
1554 tmp = RREG32(mmDCCG_AUDIO_DTO_SOURCE);
1555 tmp = REG_SET_FIELD(tmp, DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL,
1556 amdgpu_crtc->crtc_id);
1557 WREG32(mmDCCG_AUDIO_DTO_SOURCE, tmp);
1558 WREG32(mmDCCG_AUDIO_DTO0_PHASE, dto_phase);
1559 WREG32(mmDCCG_AUDIO_DTO0_MODULE, dto_modulo);
1560}
1561
1562/*
1563 * update the info frames with the data from the current display mode
1564 */
1565static void dce_v10_0_afmt_setmode(struct drm_encoder *encoder,
1566 struct drm_display_mode *mode)
1567{
1568 struct drm_device *dev = encoder->dev;
1569 struct amdgpu_device *adev = drm_to_adev(dev);
1570 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1571 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1572 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
1573 u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
1574 struct hdmi_avi_infoframe frame;
1575 ssize_t err;
1576 u32 tmp;
1577 int bpc = 8;
1578
1579 if (!dig || !dig->afmt)
1580 return;
1581
1582 /* Silent, r600_hdmi_enable will raise WARN for us */
1583 if (!dig->afmt->enabled)
1584 return;
1585
1586 /* hdmi deep color mode general control packets setup, if bpc > 8 */
1587 if (encoder->crtc) {
1588 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(encoder->crtc);
1589 bpc = amdgpu_crtc->bpc;
1590 }
1591
1592 /* disable audio prior to setting up hw */
1593 dig->afmt->pin = dce_v10_0_audio_get_pin(adev);
1594 dce_v10_0_audio_enable(adev, dig->afmt->pin, false);
1595
1596 dce_v10_0_audio_set_dto(encoder, mode->clock);
1597
1598 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1599 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1);
1600 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp); /* send null packets when required */
1601
1602 WREG32(mmAFMT_AUDIO_CRC_CONTROL + dig->afmt->offset, 0x1000);
1603
1604 tmp = RREG32(mmHDMI_CONTROL + dig->afmt->offset);
1605 switch (bpc) {
1606 case 0:
1607 case 6:
1608 case 8:
1609 case 16:
1610 default:
1611 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 0);
1612 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 0);
1613 DRM_DEBUG("%s: Disabling hdmi deep color for %d bpc.\n",
1614 connector->name, bpc);
1615 break;
1616 case 10:
1617 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1618 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 1);
1619 DRM_DEBUG("%s: Enabling hdmi deep color 30 for 10 bpc.\n",
1620 connector->name);
1621 break;
1622 case 12:
1623 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_ENABLE, 1);
1624 tmp = REG_SET_FIELD(tmp, HDMI_CONTROL, HDMI_DEEP_COLOR_DEPTH, 2);
1625 DRM_DEBUG("%s: Enabling hdmi deep color 36 for 12 bpc.\n",
1626 connector->name);
1627 break;
1628 }
1629 WREG32(mmHDMI_CONTROL + dig->afmt->offset, tmp);
1630
1631 tmp = RREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset);
1632 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_NULL_SEND, 1); /* send null packets when required */
1633 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_SEND, 1); /* send general control packets */
1634 tmp = REG_SET_FIELD(tmp, HDMI_VBI_PACKET_CONTROL, HDMI_GC_CONT, 1); /* send general control packets every frame */
1635 WREG32(mmHDMI_VBI_PACKET_CONTROL + dig->afmt->offset, tmp);
1636
1637 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1638 /* enable audio info frames (frames won't be set until audio is enabled) */
1639 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_SEND, 1);
1640 /* required for audio info values to be updated */
1641 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AUDIO_INFO_CONT, 1);
1642 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1643
1644 tmp = RREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset);
1645 /* required for audio info values to be updated */
1646 tmp = REG_SET_FIELD(tmp, AFMT_INFOFRAME_CONTROL0, AFMT_AUDIO_INFO_UPDATE, 1);
1647 WREG32(mmAFMT_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1648
1649 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1650 /* anything other than 0 */
1651 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AUDIO_INFO_LINE, 2);
1652 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1653
1654 WREG32(mmHDMI_GC + dig->afmt->offset, 0); /* unset HDMI_GC_AVMUTE */
1655
1656 tmp = RREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1657 /* set the default audio delay */
1658 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_DELAY_EN, 1);
1659 /* should be suffient for all audio modes and small enough for all hblanks */
1660 tmp = REG_SET_FIELD(tmp, HDMI_AUDIO_PACKET_CONTROL, HDMI_AUDIO_PACKETS_PER_LINE, 3);
1661 WREG32(mmHDMI_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1662
1663 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1664 /* allow 60958 channel status fields to be updated */
1665 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_60958_CS_UPDATE, 1);
1666 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1667
1668 tmp = RREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset);
1669 if (bpc > 8)
1670 /* clear SW CTS value */
1671 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 0);
1672 else
1673 /* select SW CTS value */
1674 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_SOURCE, 1);
1675 /* allow hw to sent ACR packets when required */
1676 tmp = REG_SET_FIELD(tmp, HDMI_ACR_PACKET_CONTROL, HDMI_ACR_AUTO_SEND, 1);
1677 WREG32(mmHDMI_ACR_PACKET_CONTROL + dig->afmt->offset, tmp);
1678
1679 dce_v10_0_afmt_update_ACR(encoder, mode->clock);
1680
1681 tmp = RREG32(mmAFMT_60958_0 + dig->afmt->offset);
1682 tmp = REG_SET_FIELD(tmp, AFMT_60958_0, AFMT_60958_CS_CHANNEL_NUMBER_L, 1);
1683 WREG32(mmAFMT_60958_0 + dig->afmt->offset, tmp);
1684
1685 tmp = RREG32(mmAFMT_60958_1 + dig->afmt->offset);
1686 tmp = REG_SET_FIELD(tmp, AFMT_60958_1, AFMT_60958_CS_CHANNEL_NUMBER_R, 2);
1687 WREG32(mmAFMT_60958_1 + dig->afmt->offset, tmp);
1688
1689 tmp = RREG32(mmAFMT_60958_2 + dig->afmt->offset);
1690 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_2, 3);
1691 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_3, 4);
1692 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_4, 5);
1693 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_5, 6);
1694 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_6, 7);
1695 tmp = REG_SET_FIELD(tmp, AFMT_60958_2, AFMT_60958_CS_CHANNEL_NUMBER_7, 8);
1696 WREG32(mmAFMT_60958_2 + dig->afmt->offset, tmp);
1697
1698 dce_v10_0_audio_write_speaker_allocation(encoder);
1699
1700 WREG32(mmAFMT_AUDIO_PACKET_CONTROL2 + dig->afmt->offset,
1701 (0xff << AFMT_AUDIO_PACKET_CONTROL2__AFMT_AUDIO_CHANNEL_ENABLE__SHIFT));
1702
1703 dce_v10_0_afmt_audio_select_pin(encoder);
1704 dce_v10_0_audio_write_sad_regs(encoder);
1705 dce_v10_0_audio_write_latency_fields(encoder, mode);
1706
1707 err = drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);
1708 if (err < 0) {
1709 DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
1710 return;
1711 }
1712
1713 err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
1714 if (err < 0) {
1715 DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
1716 return;
1717 }
1718
1719 dce_v10_0_afmt_update_avi_infoframe(encoder, buffer, sizeof(buffer));
1720
1721 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset);
1722 /* enable AVI info frames */
1723 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_SEND, 1);
1724 /* required for audio info values to be updated */
1725 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL0, HDMI_AVI_INFO_CONT, 1);
1726 WREG32(mmHDMI_INFOFRAME_CONTROL0 + dig->afmt->offset, tmp);
1727
1728 tmp = RREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset);
1729 tmp = REG_SET_FIELD(tmp, HDMI_INFOFRAME_CONTROL1, HDMI_AVI_INFO_LINE, 2);
1730 WREG32(mmHDMI_INFOFRAME_CONTROL1 + dig->afmt->offset, tmp);
1731
1732 tmp = RREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset);
1733 /* send audio packets */
1734 tmp = REG_SET_FIELD(tmp, AFMT_AUDIO_PACKET_CONTROL, AFMT_AUDIO_SAMPLE_SEND, 1);
1735 WREG32(mmAFMT_AUDIO_PACKET_CONTROL + dig->afmt->offset, tmp);
1736
1737 WREG32(mmAFMT_RAMP_CONTROL0 + dig->afmt->offset, 0x00FFFFFF);
1738 WREG32(mmAFMT_RAMP_CONTROL1 + dig->afmt->offset, 0x007FFFFF);
1739 WREG32(mmAFMT_RAMP_CONTROL2 + dig->afmt->offset, 0x00000001);
1740 WREG32(mmAFMT_RAMP_CONTROL3 + dig->afmt->offset, 0x00000001);
1741
1742 /* enable audio after to setting up hw */
1743 dce_v10_0_audio_enable(adev, dig->afmt->pin, true);
1744}
1745
1746static void dce_v10_0_afmt_enable(struct drm_encoder *encoder, bool enable)
1747{
1748 struct drm_device *dev = encoder->dev;
1749 struct amdgpu_device *adev = drm_to_adev(dev);
1750 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
1751 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
1752
1753 if (!dig || !dig->afmt)
1754 return;
1755
1756 /* Silent, r600_hdmi_enable will raise WARN for us */
1757 if (enable && dig->afmt->enabled)
1758 return;
1759 if (!enable && !dig->afmt->enabled)
1760 return;
1761
1762 if (!enable && dig->afmt->pin) {
1763 dce_v10_0_audio_enable(adev, dig->afmt->pin, false);
1764 dig->afmt->pin = NULL;
1765 }
1766
1767 dig->afmt->enabled = enable;
1768
1769 DRM_DEBUG("%sabling AFMT interface @ 0x%04X for encoder 0x%x\n",
1770 enable ? "En" : "Dis", dig->afmt->offset, amdgpu_encoder->encoder_id);
1771}
1772
1773static int dce_v10_0_afmt_init(struct amdgpu_device *adev)
1774{
1775 int i;
1776
1777 for (i = 0; i < adev->mode_info.num_dig; i++)
1778 adev->mode_info.afmt[i] = NULL;
1779
1780 /* DCE10 has audio blocks tied to DIG encoders */
1781 for (i = 0; i < adev->mode_info.num_dig; i++) {
1782 adev->mode_info.afmt[i] = kzalloc(sizeof(struct amdgpu_afmt), GFP_KERNEL);
1783 if (adev->mode_info.afmt[i]) {
1784 adev->mode_info.afmt[i]->offset = dig_offsets[i];
1785 adev->mode_info.afmt[i]->id = i;
1786 } else {
1787 int j;
1788 for (j = 0; j < i; j++) {
1789 kfree(adev->mode_info.afmt[j]);
1790 adev->mode_info.afmt[j] = NULL;
1791 }
1792 return -ENOMEM;
1793 }
1794 }
1795 return 0;
1796}
1797
1798static void dce_v10_0_afmt_fini(struct amdgpu_device *adev)
1799{
1800 int i;
1801
1802 for (i = 0; i < adev->mode_info.num_dig; i++) {
1803 kfree(adev->mode_info.afmt[i]);
1804 adev->mode_info.afmt[i] = NULL;
1805 }
1806}
1807
1808static const u32 vga_control_regs[6] = {
1809 mmD1VGA_CONTROL,
1810 mmD2VGA_CONTROL,
1811 mmD3VGA_CONTROL,
1812 mmD4VGA_CONTROL,
1813 mmD5VGA_CONTROL,
1814 mmD6VGA_CONTROL,
1815};
1816
1817static void dce_v10_0_vga_enable(struct drm_crtc *crtc, bool enable)
1818{
1819 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1820 struct drm_device *dev = crtc->dev;
1821 struct amdgpu_device *adev = drm_to_adev(dev);
1822 u32 vga_control;
1823
1824 vga_control = RREG32(vga_control_regs[amdgpu_crtc->crtc_id]) & ~1;
1825 if (enable)
1826 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control | 1);
1827 else
1828 WREG32(vga_control_regs[amdgpu_crtc->crtc_id], vga_control);
1829}
1830
1831static void dce_v10_0_grph_enable(struct drm_crtc *crtc, bool enable)
1832{
1833 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1834 struct drm_device *dev = crtc->dev;
1835 struct amdgpu_device *adev = drm_to_adev(dev);
1836
1837 if (enable)
1838 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 1);
1839 else
1840 WREG32(mmGRPH_ENABLE + amdgpu_crtc->crtc_offset, 0);
1841}
1842
1843static int dce_v10_0_crtc_do_set_base(struct drm_crtc *crtc,
1844 struct drm_framebuffer *fb,
1845 int x, int y, int atomic)
1846{
1847 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1848 struct drm_device *dev = crtc->dev;
1849 struct amdgpu_device *adev = drm_to_adev(dev);
1850 struct drm_framebuffer *target_fb;
1851 struct drm_gem_object *obj;
1852 struct amdgpu_bo *abo;
1853 uint64_t fb_location, tiling_flags;
1854 uint32_t fb_format, fb_pitch_pixels;
1855 u32 fb_swap = REG_SET_FIELD(0, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP, ENDIAN_NONE);
1856 u32 pipe_config;
1857 u32 tmp, viewport_w, viewport_h;
1858 int r;
1859 bool bypass_lut = false;
1860
1861 /* no fb bound */
1862 if (!atomic && !crtc->primary->fb) {
1863 DRM_DEBUG_KMS("No FB bound\n");
1864 return 0;
1865 }
1866
1867 if (atomic)
1868 target_fb = fb;
1869 else
1870 target_fb = crtc->primary->fb;
1871
1872 /* If atomic, assume fb object is pinned & idle & fenced and
1873 * just update base pointers
1874 */
1875 obj = target_fb->obj[0];
1876 abo = gem_to_amdgpu_bo(obj);
1877 r = amdgpu_bo_reserve(abo, false);
1878 if (unlikely(r != 0))
1879 return r;
1880
1881 if (!atomic) {
1882 r = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM);
1883 if (unlikely(r != 0)) {
1884 amdgpu_bo_unreserve(abo);
1885 return -EINVAL;
1886 }
1887 }
1888 fb_location = amdgpu_bo_gpu_offset(abo);
1889
1890 amdgpu_bo_get_tiling_flags(abo, &tiling_flags);
1891 amdgpu_bo_unreserve(abo);
1892
1893 pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
1894
1895 switch (target_fb->format->format) {
1896 case DRM_FORMAT_C8:
1897 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 0);
1898 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1899 break;
1900 case DRM_FORMAT_XRGB4444:
1901 case DRM_FORMAT_ARGB4444:
1902 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1903 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 2);
1904#ifdef __BIG_ENDIAN
1905 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1906 ENDIAN_8IN16);
1907#endif
1908 break;
1909 case DRM_FORMAT_XRGB1555:
1910 case DRM_FORMAT_ARGB1555:
1911 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1912 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1913#ifdef __BIG_ENDIAN
1914 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1915 ENDIAN_8IN16);
1916#endif
1917 break;
1918 case DRM_FORMAT_BGRX5551:
1919 case DRM_FORMAT_BGRA5551:
1920 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1921 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 5);
1922#ifdef __BIG_ENDIAN
1923 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1924 ENDIAN_8IN16);
1925#endif
1926 break;
1927 case DRM_FORMAT_RGB565:
1928 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 1);
1929 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
1930#ifdef __BIG_ENDIAN
1931 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1932 ENDIAN_8IN16);
1933#endif
1934 break;
1935 case DRM_FORMAT_XRGB8888:
1936 case DRM_FORMAT_ARGB8888:
1937 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1938 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1939#ifdef __BIG_ENDIAN
1940 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1941 ENDIAN_8IN32);
1942#endif
1943 break;
1944 case DRM_FORMAT_XRGB2101010:
1945 case DRM_FORMAT_ARGB2101010:
1946 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1947 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 1);
1948#ifdef __BIG_ENDIAN
1949 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1950 ENDIAN_8IN32);
1951#endif
1952 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1953 bypass_lut = true;
1954 break;
1955 case DRM_FORMAT_BGRX1010102:
1956 case DRM_FORMAT_BGRA1010102:
1957 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1958 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 4);
1959#ifdef __BIG_ENDIAN
1960 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1961 ENDIAN_8IN32);
1962#endif
1963 /* Greater 8 bpc fb needs to bypass hw-lut to retain precision */
1964 bypass_lut = true;
1965 break;
1966 case DRM_FORMAT_XBGR8888:
1967 case DRM_FORMAT_ABGR8888:
1968 fb_format = REG_SET_FIELD(0, GRPH_CONTROL, GRPH_DEPTH, 2);
1969 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_FORMAT, 0);
1970 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_RED_CROSSBAR, 2);
1971 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_BLUE_CROSSBAR, 2);
1972#ifdef __BIG_ENDIAN
1973 fb_swap = REG_SET_FIELD(fb_swap, GRPH_SWAP_CNTL, GRPH_ENDIAN_SWAP,
1974 ENDIAN_8IN32);
1975#endif
1976 break;
1977 default:
1978 DRM_ERROR("Unsupported screen format %p4cc\n",
1979 &target_fb->format->format);
1980 return -EINVAL;
1981 }
1982
1983 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_2D_TILED_THIN1) {
1984 unsigned bankw, bankh, mtaspect, tile_split, num_banks;
1985
1986 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
1987 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
1988 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
1989 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT);
1990 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
1991
1992 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_NUM_BANKS, num_banks);
1993 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
1994 ARRAY_2D_TILED_THIN1);
1995 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_TILE_SPLIT,
1996 tile_split);
1997 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_WIDTH, bankw);
1998 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_BANK_HEIGHT, bankh);
1999 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MACRO_TILE_ASPECT,
2000 mtaspect);
2001 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_MICRO_TILE_MODE,
2002 ADDR_SURF_MICRO_TILING_DISPLAY);
2003 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == ARRAY_1D_TILED_THIN1) {
2004 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_ARRAY_MODE,
2005 ARRAY_1D_TILED_THIN1);
2006 }
2007
2008 fb_format = REG_SET_FIELD(fb_format, GRPH_CONTROL, GRPH_PIPE_CONFIG,
2009 pipe_config);
2010
2011 dce_v10_0_vga_enable(crtc, false);
2012
2013 /* Make sure surface address is updated at vertical blank rather than
2014 * horizontal blank
2015 */
2016 tmp = RREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset);
2017 tmp = REG_SET_FIELD(tmp, GRPH_FLIP_CONTROL,
2018 GRPH_SURFACE_UPDATE_H_RETRACE_EN, 0);
2019 WREG32(mmGRPH_FLIP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2020
2021 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2022 upper_32_bits(fb_location));
2023 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2024 upper_32_bits(fb_location));
2025 WREG32(mmGRPH_PRIMARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2026 (u32)fb_location & GRPH_PRIMARY_SURFACE_ADDRESS__GRPH_PRIMARY_SURFACE_ADDRESS_MASK);
2027 WREG32(mmGRPH_SECONDARY_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2028 (u32) fb_location & GRPH_SECONDARY_SURFACE_ADDRESS__GRPH_SECONDARY_SURFACE_ADDRESS_MASK);
2029 WREG32(mmGRPH_CONTROL + amdgpu_crtc->crtc_offset, fb_format);
2030 WREG32(mmGRPH_SWAP_CNTL + amdgpu_crtc->crtc_offset, fb_swap);
2031
2032 /*
2033 * The LUT only has 256 slots for indexing by a 8 bpc fb. Bypass the LUT
2034 * for > 8 bpc scanout to avoid truncation of fb indices to 8 msb's, to
2035 * retain the full precision throughout the pipeline.
2036 */
2037 tmp = RREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset);
2038 if (bypass_lut)
2039 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 1);
2040 else
2041 tmp = REG_SET_FIELD(tmp, GRPH_LUT_10BIT_BYPASS, GRPH_LUT_10BIT_BYPASS_EN, 0);
2042 WREG32(mmGRPH_LUT_10BIT_BYPASS + amdgpu_crtc->crtc_offset, tmp);
2043
2044 if (bypass_lut)
2045 DRM_DEBUG_KMS("Bypassing hardware LUT due to 10 bit fb scanout.\n");
2046
2047 WREG32(mmGRPH_SURFACE_OFFSET_X + amdgpu_crtc->crtc_offset, 0);
2048 WREG32(mmGRPH_SURFACE_OFFSET_Y + amdgpu_crtc->crtc_offset, 0);
2049 WREG32(mmGRPH_X_START + amdgpu_crtc->crtc_offset, 0);
2050 WREG32(mmGRPH_Y_START + amdgpu_crtc->crtc_offset, 0);
2051 WREG32(mmGRPH_X_END + amdgpu_crtc->crtc_offset, target_fb->width);
2052 WREG32(mmGRPH_Y_END + amdgpu_crtc->crtc_offset, target_fb->height);
2053
2054 fb_pitch_pixels = target_fb->pitches[0] / target_fb->format->cpp[0];
2055 WREG32(mmGRPH_PITCH + amdgpu_crtc->crtc_offset, fb_pitch_pixels);
2056
2057 dce_v10_0_grph_enable(crtc, true);
2058
2059 WREG32(mmLB_DESKTOP_HEIGHT + amdgpu_crtc->crtc_offset,
2060 target_fb->height);
2061
2062 x &= ~3;
2063 y &= ~1;
2064 WREG32(mmVIEWPORT_START + amdgpu_crtc->crtc_offset,
2065 (x << 16) | y);
2066 viewport_w = crtc->mode.hdisplay;
2067 viewport_h = (crtc->mode.vdisplay + 1) & ~1;
2068 WREG32(mmVIEWPORT_SIZE + amdgpu_crtc->crtc_offset,
2069 (viewport_w << 16) | viewport_h);
2070
2071 /* set pageflip to happen anywhere in vblank interval */
2072 WREG32(mmMASTER_UPDATE_MODE + amdgpu_crtc->crtc_offset, 0);
2073
2074 if (!atomic && fb && fb != crtc->primary->fb) {
2075 abo = gem_to_amdgpu_bo(fb->obj[0]);
2076 r = amdgpu_bo_reserve(abo, true);
2077 if (unlikely(r != 0))
2078 return r;
2079 amdgpu_bo_unpin(abo);
2080 amdgpu_bo_unreserve(abo);
2081 }
2082
2083 /* Bytes per pixel may have changed */
2084 dce_v10_0_bandwidth_update(adev);
2085
2086 return 0;
2087}
2088
2089static void dce_v10_0_set_interleave(struct drm_crtc *crtc,
2090 struct drm_display_mode *mode)
2091{
2092 struct drm_device *dev = crtc->dev;
2093 struct amdgpu_device *adev = drm_to_adev(dev);
2094 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2095 u32 tmp;
2096
2097 tmp = RREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset);
2098 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
2099 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 1);
2100 else
2101 tmp = REG_SET_FIELD(tmp, LB_DATA_FORMAT, INTERLEAVE_EN, 0);
2102 WREG32(mmLB_DATA_FORMAT + amdgpu_crtc->crtc_offset, tmp);
2103}
2104
2105static void dce_v10_0_crtc_load_lut(struct drm_crtc *crtc)
2106{
2107 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2108 struct drm_device *dev = crtc->dev;
2109 struct amdgpu_device *adev = drm_to_adev(dev);
2110 u16 *r, *g, *b;
2111 int i;
2112 u32 tmp;
2113
2114 DRM_DEBUG_KMS("%d\n", amdgpu_crtc->crtc_id);
2115
2116 tmp = RREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2117 tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_GRPH_MODE, 0);
2118 tmp = REG_SET_FIELD(tmp, INPUT_CSC_CONTROL, INPUT_CSC_OVL_MODE, 0);
2119 WREG32(mmINPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2120
2121 tmp = RREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset);
2122 tmp = REG_SET_FIELD(tmp, PRESCALE_GRPH_CONTROL, GRPH_PRESCALE_BYPASS, 1);
2123 WREG32(mmPRESCALE_GRPH_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2124
2125 tmp = RREG32(mmPRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset);
2126 tmp = REG_SET_FIELD(tmp, PRESCALE_OVL_CONTROL, OVL_PRESCALE_BYPASS, 1);
2127 WREG32(mmPRESCALE_OVL_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2128
2129 tmp = RREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2130 tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, GRPH_INPUT_GAMMA_MODE, 0);
2131 tmp = REG_SET_FIELD(tmp, INPUT_GAMMA_CONTROL, OVL_INPUT_GAMMA_MODE, 0);
2132 WREG32(mmINPUT_GAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2133
2134 WREG32(mmDC_LUT_CONTROL + amdgpu_crtc->crtc_offset, 0);
2135
2136 WREG32(mmDC_LUT_BLACK_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0);
2137 WREG32(mmDC_LUT_BLACK_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0);
2138 WREG32(mmDC_LUT_BLACK_OFFSET_RED + amdgpu_crtc->crtc_offset, 0);
2139
2140 WREG32(mmDC_LUT_WHITE_OFFSET_BLUE + amdgpu_crtc->crtc_offset, 0xffff);
2141 WREG32(mmDC_LUT_WHITE_OFFSET_GREEN + amdgpu_crtc->crtc_offset, 0xffff);
2142 WREG32(mmDC_LUT_WHITE_OFFSET_RED + amdgpu_crtc->crtc_offset, 0xffff);
2143
2144 WREG32(mmDC_LUT_RW_MODE + amdgpu_crtc->crtc_offset, 0);
2145 WREG32(mmDC_LUT_WRITE_EN_MASK + amdgpu_crtc->crtc_offset, 0x00000007);
2146
2147 WREG32(mmDC_LUT_RW_INDEX + amdgpu_crtc->crtc_offset, 0);
2148 r = crtc->gamma_store;
2149 g = r + crtc->gamma_size;
2150 b = g + crtc->gamma_size;
2151 for (i = 0; i < 256; i++) {
2152 WREG32(mmDC_LUT_30_COLOR + amdgpu_crtc->crtc_offset,
2153 ((*r++ & 0xffc0) << 14) |
2154 ((*g++ & 0xffc0) << 4) |
2155 (*b++ >> 6));
2156 }
2157
2158 tmp = RREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2159 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, GRPH_DEGAMMA_MODE, 0);
2160 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, OVL_DEGAMMA_MODE, 0);
2161 tmp = REG_SET_FIELD(tmp, DEGAMMA_CONTROL, CURSOR_DEGAMMA_MODE, 0);
2162 WREG32(mmDEGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2163
2164 tmp = RREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset);
2165 tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, GRPH_GAMUT_REMAP_MODE, 0);
2166 tmp = REG_SET_FIELD(tmp, GAMUT_REMAP_CONTROL, OVL_GAMUT_REMAP_MODE, 0);
2167 WREG32(mmGAMUT_REMAP_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2168
2169 tmp = RREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset);
2170 tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, GRPH_REGAMMA_MODE, 0);
2171 tmp = REG_SET_FIELD(tmp, REGAMMA_CONTROL, OVL_REGAMMA_MODE, 0);
2172 WREG32(mmREGAMMA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2173
2174 tmp = RREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset);
2175 tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_GRPH_MODE, 0);
2176 tmp = REG_SET_FIELD(tmp, OUTPUT_CSC_CONTROL, OUTPUT_CSC_OVL_MODE, 0);
2177 WREG32(mmOUTPUT_CSC_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2178
2179 /* XXX match this to the depth of the crtc fmt block, move to modeset? */
2180 WREG32(mmDENORM_CONTROL + amdgpu_crtc->crtc_offset, 0);
2181 /* XXX this only needs to be programmed once per crtc at startup,
2182 * not sure where the best place for it is
2183 */
2184 tmp = RREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset);
2185 tmp = REG_SET_FIELD(tmp, ALPHA_CONTROL, CURSOR_ALPHA_BLND_ENA, 1);
2186 WREG32(mmALPHA_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2187}
2188
2189static int dce_v10_0_pick_dig_encoder(struct drm_encoder *encoder)
2190{
2191 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
2192 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
2193
2194 switch (amdgpu_encoder->encoder_id) {
2195 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
2196 if (dig->linkb)
2197 return 1;
2198 else
2199 return 0;
2200 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
2201 if (dig->linkb)
2202 return 3;
2203 else
2204 return 2;
2205 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
2206 if (dig->linkb)
2207 return 5;
2208 else
2209 return 4;
2210 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
2211 return 6;
2212 default:
2213 DRM_ERROR("invalid encoder_id: 0x%x\n", amdgpu_encoder->encoder_id);
2214 return 0;
2215 }
2216}
2217
2218/**
2219 * dce_v10_0_pick_pll - Allocate a PPLL for use by the crtc.
2220 *
2221 * @crtc: drm crtc
2222 *
2223 * Returns the PPLL (Pixel PLL) to be used by the crtc. For DP monitors
2224 * a single PPLL can be used for all DP crtcs/encoders. For non-DP
2225 * monitors a dedicated PPLL must be used. If a particular board has
2226 * an external DP PLL, return ATOM_PPLL_INVALID to skip PLL programming
2227 * as there is no need to program the PLL itself. If we are not able to
2228 * allocate a PLL, return ATOM_PPLL_INVALID to skip PLL programming to
2229 * avoid messing up an existing monitor.
2230 *
2231 * Asic specific PLL information
2232 *
2233 * DCE 10.x
2234 * Tonga
2235 * - PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP)
2236 * CI
2237 * - PPLL0, PPLL1, PPLL2 are available for all UNIPHY (both DP and non-DP) and DAC
2238 *
2239 */
2240static u32 dce_v10_0_pick_pll(struct drm_crtc *crtc)
2241{
2242 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2243 struct drm_device *dev = crtc->dev;
2244 struct amdgpu_device *adev = drm_to_adev(dev);
2245 u32 pll_in_use;
2246 int pll;
2247
2248 if (ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder))) {
2249 if (adev->clock.dp_extclk)
2250 /* skip PPLL programming if using ext clock */
2251 return ATOM_PPLL_INVALID;
2252 else {
2253 /* use the same PPLL for all DP monitors */
2254 pll = amdgpu_pll_get_shared_dp_ppll(crtc);
2255 if (pll != ATOM_PPLL_INVALID)
2256 return pll;
2257 }
2258 } else {
2259 /* use the same PPLL for all monitors with the same clock */
2260 pll = amdgpu_pll_get_shared_nondp_ppll(crtc);
2261 if (pll != ATOM_PPLL_INVALID)
2262 return pll;
2263 }
2264
2265 /* DCE10 has PPLL0, PPLL1, and PPLL2 */
2266 pll_in_use = amdgpu_pll_get_use_mask(crtc);
2267 if (!(pll_in_use & (1 << ATOM_PPLL2)))
2268 return ATOM_PPLL2;
2269 if (!(pll_in_use & (1 << ATOM_PPLL1)))
2270 return ATOM_PPLL1;
2271 if (!(pll_in_use & (1 << ATOM_PPLL0)))
2272 return ATOM_PPLL0;
2273 DRM_ERROR("unable to allocate a PPLL\n");
2274 return ATOM_PPLL_INVALID;
2275}
2276
2277static void dce_v10_0_lock_cursor(struct drm_crtc *crtc, bool lock)
2278{
2279 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2280 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2281 uint32_t cur_lock;
2282
2283 cur_lock = RREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset);
2284 if (lock)
2285 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 1);
2286 else
2287 cur_lock = REG_SET_FIELD(cur_lock, CUR_UPDATE, CURSOR_UPDATE_LOCK, 0);
2288 WREG32(mmCUR_UPDATE + amdgpu_crtc->crtc_offset, cur_lock);
2289}
2290
2291static void dce_v10_0_hide_cursor(struct drm_crtc *crtc)
2292{
2293 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2294 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2295 u32 tmp;
2296
2297 tmp = RREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2298 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 0);
2299 WREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2300}
2301
2302static void dce_v10_0_show_cursor(struct drm_crtc *crtc)
2303{
2304 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2305 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2306 u32 tmp;
2307
2308 WREG32(mmCUR_SURFACE_ADDRESS_HIGH + amdgpu_crtc->crtc_offset,
2309 upper_32_bits(amdgpu_crtc->cursor_addr));
2310 WREG32(mmCUR_SURFACE_ADDRESS + amdgpu_crtc->crtc_offset,
2311 lower_32_bits(amdgpu_crtc->cursor_addr));
2312
2313 tmp = RREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset);
2314 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_EN, 1);
2315 tmp = REG_SET_FIELD(tmp, CUR_CONTROL, CURSOR_MODE, 2);
2316 WREG32(mmCUR_CONTROL + amdgpu_crtc->crtc_offset, tmp);
2317}
2318
2319static int dce_v10_0_cursor_move_locked(struct drm_crtc *crtc,
2320 int x, int y)
2321{
2322 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2323 struct amdgpu_device *adev = drm_to_adev(crtc->dev);
2324 int xorigin = 0, yorigin = 0;
2325
2326 amdgpu_crtc->cursor_x = x;
2327 amdgpu_crtc->cursor_y = y;
2328
2329 /* avivo cursor are offset into the total surface */
2330 x += crtc->x;
2331 y += crtc->y;
2332 DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
2333
2334 if (x < 0) {
2335 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1);
2336 x = 0;
2337 }
2338 if (y < 0) {
2339 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1);
2340 y = 0;
2341 }
2342
2343 WREG32(mmCUR_POSITION + amdgpu_crtc->crtc_offset, (x << 16) | y);
2344 WREG32(mmCUR_HOT_SPOT + amdgpu_crtc->crtc_offset, (xorigin << 16) | yorigin);
2345 WREG32(mmCUR_SIZE + amdgpu_crtc->crtc_offset,
2346 ((amdgpu_crtc->cursor_width - 1) << 16) | (amdgpu_crtc->cursor_height - 1));
2347
2348 return 0;
2349}
2350
2351static int dce_v10_0_crtc_cursor_move(struct drm_crtc *crtc,
2352 int x, int y)
2353{
2354 int ret;
2355
2356 dce_v10_0_lock_cursor(crtc, true);
2357 ret = dce_v10_0_cursor_move_locked(crtc, x, y);
2358 dce_v10_0_lock_cursor(crtc, false);
2359
2360 return ret;
2361}
2362
2363static int dce_v10_0_crtc_cursor_set2(struct drm_crtc *crtc,
2364 struct drm_file *file_priv,
2365 uint32_t handle,
2366 uint32_t width,
2367 uint32_t height,
2368 int32_t hot_x,
2369 int32_t hot_y)
2370{
2371 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2372 struct drm_gem_object *obj;
2373 struct amdgpu_bo *aobj;
2374 int ret;
2375
2376 if (!handle) {
2377 /* turn off cursor */
2378 dce_v10_0_hide_cursor(crtc);
2379 obj = NULL;
2380 goto unpin;
2381 }
2382
2383 if ((width > amdgpu_crtc->max_cursor_width) ||
2384 (height > amdgpu_crtc->max_cursor_height)) {
2385 DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
2386 return -EINVAL;
2387 }
2388
2389 obj = drm_gem_object_lookup(file_priv, handle);
2390 if (!obj) {
2391 DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, amdgpu_crtc->crtc_id);
2392 return -ENOENT;
2393 }
2394
2395 aobj = gem_to_amdgpu_bo(obj);
2396 ret = amdgpu_bo_reserve(aobj, false);
2397 if (ret != 0) {
2398 drm_gem_object_put(obj);
2399 return ret;
2400 }
2401
2402 ret = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
2403 amdgpu_bo_unreserve(aobj);
2404 if (ret) {
2405 DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
2406 drm_gem_object_put(obj);
2407 return ret;
2408 }
2409 amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
2410
2411 dce_v10_0_lock_cursor(crtc, true);
2412
2413 if (width != amdgpu_crtc->cursor_width ||
2414 height != amdgpu_crtc->cursor_height ||
2415 hot_x != amdgpu_crtc->cursor_hot_x ||
2416 hot_y != amdgpu_crtc->cursor_hot_y) {
2417 int x, y;
2418
2419 x = amdgpu_crtc->cursor_x + amdgpu_crtc->cursor_hot_x - hot_x;
2420 y = amdgpu_crtc->cursor_y + amdgpu_crtc->cursor_hot_y - hot_y;
2421
2422 dce_v10_0_cursor_move_locked(crtc, x, y);
2423
2424 amdgpu_crtc->cursor_width = width;
2425 amdgpu_crtc->cursor_height = height;
2426 amdgpu_crtc->cursor_hot_x = hot_x;
2427 amdgpu_crtc->cursor_hot_y = hot_y;
2428 }
2429
2430 dce_v10_0_show_cursor(crtc);
2431 dce_v10_0_lock_cursor(crtc, false);
2432
2433unpin:
2434 if (amdgpu_crtc->cursor_bo) {
2435 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2436 ret = amdgpu_bo_reserve(aobj, true);
2437 if (likely(ret == 0)) {
2438 amdgpu_bo_unpin(aobj);
2439 amdgpu_bo_unreserve(aobj);
2440 }
2441 drm_gem_object_put(amdgpu_crtc->cursor_bo);
2442 }
2443
2444 amdgpu_crtc->cursor_bo = obj;
2445 return 0;
2446}
2447
2448static void dce_v10_0_cursor_reset(struct drm_crtc *crtc)
2449{
2450 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2451
2452 if (amdgpu_crtc->cursor_bo) {
2453 dce_v10_0_lock_cursor(crtc, true);
2454
2455 dce_v10_0_cursor_move_locked(crtc, amdgpu_crtc->cursor_x,
2456 amdgpu_crtc->cursor_y);
2457
2458 dce_v10_0_show_cursor(crtc);
2459
2460 dce_v10_0_lock_cursor(crtc, false);
2461 }
2462}
2463
2464static int dce_v10_0_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
2465 u16 *blue, uint32_t size,
2466 struct drm_modeset_acquire_ctx *ctx)
2467{
2468 dce_v10_0_crtc_load_lut(crtc);
2469
2470 return 0;
2471}
2472
2473static void dce_v10_0_crtc_destroy(struct drm_crtc *crtc)
2474{
2475 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2476
2477 drm_crtc_cleanup(crtc);
2478 kfree(amdgpu_crtc);
2479}
2480
2481static const struct drm_crtc_funcs dce_v10_0_crtc_funcs = {
2482 .cursor_set2 = dce_v10_0_crtc_cursor_set2,
2483 .cursor_move = dce_v10_0_crtc_cursor_move,
2484 .gamma_set = dce_v10_0_crtc_gamma_set,
2485 .set_config = amdgpu_display_crtc_set_config,
2486 .destroy = dce_v10_0_crtc_destroy,
2487 .page_flip_target = amdgpu_display_crtc_page_flip_target,
2488 .get_vblank_counter = amdgpu_get_vblank_counter_kms,
2489 .enable_vblank = amdgpu_enable_vblank_kms,
2490 .disable_vblank = amdgpu_disable_vblank_kms,
2491 .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
2492};
2493
2494static void dce_v10_0_crtc_dpms(struct drm_crtc *crtc, int mode)
2495{
2496 struct drm_device *dev = crtc->dev;
2497 struct amdgpu_device *adev = drm_to_adev(dev);
2498 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2499 unsigned type;
2500
2501 switch (mode) {
2502 case DRM_MODE_DPMS_ON:
2503 amdgpu_crtc->enabled = true;
2504 amdgpu_atombios_crtc_enable(crtc, ATOM_ENABLE);
2505 dce_v10_0_vga_enable(crtc, true);
2506 amdgpu_atombios_crtc_blank(crtc, ATOM_DISABLE);
2507 dce_v10_0_vga_enable(crtc, false);
2508 /* Make sure VBLANK and PFLIP interrupts are still enabled */
2509 type = amdgpu_display_crtc_idx_to_irq_type(adev,
2510 amdgpu_crtc->crtc_id);
2511 amdgpu_irq_update(adev, &adev->crtc_irq, type);
2512 amdgpu_irq_update(adev, &adev->pageflip_irq, type);
2513 drm_crtc_vblank_on(crtc);
2514 dce_v10_0_crtc_load_lut(crtc);
2515 break;
2516 case DRM_MODE_DPMS_STANDBY:
2517 case DRM_MODE_DPMS_SUSPEND:
2518 case DRM_MODE_DPMS_OFF:
2519 drm_crtc_vblank_off(crtc);
2520 if (amdgpu_crtc->enabled) {
2521 dce_v10_0_vga_enable(crtc, true);
2522 amdgpu_atombios_crtc_blank(crtc, ATOM_ENABLE);
2523 dce_v10_0_vga_enable(crtc, false);
2524 }
2525 amdgpu_atombios_crtc_enable(crtc, ATOM_DISABLE);
2526 amdgpu_crtc->enabled = false;
2527 break;
2528 }
2529 /* adjust pm to dpms */
2530 amdgpu_dpm_compute_clocks(adev);
2531}
2532
2533static void dce_v10_0_crtc_prepare(struct drm_crtc *crtc)
2534{
2535 /* disable crtc pair power gating before programming */
2536 amdgpu_atombios_crtc_powergate(crtc, ATOM_DISABLE);
2537 amdgpu_atombios_crtc_lock(crtc, ATOM_ENABLE);
2538 dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2539}
2540
2541static void dce_v10_0_crtc_commit(struct drm_crtc *crtc)
2542{
2543 dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
2544 amdgpu_atombios_crtc_lock(crtc, ATOM_DISABLE);
2545}
2546
2547static void dce_v10_0_crtc_disable(struct drm_crtc *crtc)
2548{
2549 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2550 struct drm_device *dev = crtc->dev;
2551 struct amdgpu_device *adev = drm_to_adev(dev);
2552 struct amdgpu_atom_ss ss;
2553 int i;
2554
2555 dce_v10_0_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
2556 if (crtc->primary->fb) {
2557 int r;
2558 struct amdgpu_bo *abo;
2559
2560 abo = gem_to_amdgpu_bo(crtc->primary->fb->obj[0]);
2561 r = amdgpu_bo_reserve(abo, true);
2562 if (unlikely(r))
2563 DRM_ERROR("failed to reserve abo before unpin\n");
2564 else {
2565 amdgpu_bo_unpin(abo);
2566 amdgpu_bo_unreserve(abo);
2567 }
2568 }
2569 /* disable the GRPH */
2570 dce_v10_0_grph_enable(crtc, false);
2571
2572 amdgpu_atombios_crtc_powergate(crtc, ATOM_ENABLE);
2573
2574 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2575 if (adev->mode_info.crtcs[i] &&
2576 adev->mode_info.crtcs[i]->enabled &&
2577 i != amdgpu_crtc->crtc_id &&
2578 amdgpu_crtc->pll_id == adev->mode_info.crtcs[i]->pll_id) {
2579 /* one other crtc is using this pll don't turn
2580 * off the pll
2581 */
2582 goto done;
2583 }
2584 }
2585
2586 switch (amdgpu_crtc->pll_id) {
2587 case ATOM_PPLL0:
2588 case ATOM_PPLL1:
2589 case ATOM_PPLL2:
2590 /* disable the ppll */
2591 amdgpu_atombios_crtc_program_pll(crtc, amdgpu_crtc->crtc_id, amdgpu_crtc->pll_id,
2592 0, 0, ATOM_DISABLE, 0, 0, 0, 0, 0, false, &ss);
2593 break;
2594 default:
2595 break;
2596 }
2597done:
2598 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2599 amdgpu_crtc->adjusted_clock = 0;
2600 amdgpu_crtc->encoder = NULL;
2601 amdgpu_crtc->connector = NULL;
2602}
2603
2604static int dce_v10_0_crtc_mode_set(struct drm_crtc *crtc,
2605 struct drm_display_mode *mode,
2606 struct drm_display_mode *adjusted_mode,
2607 int x, int y, struct drm_framebuffer *old_fb)
2608{
2609 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2610
2611 if (!amdgpu_crtc->adjusted_clock)
2612 return -EINVAL;
2613
2614 amdgpu_atombios_crtc_set_pll(crtc, adjusted_mode);
2615 amdgpu_atombios_crtc_set_dtd_timing(crtc, adjusted_mode);
2616 dce_v10_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2617 amdgpu_atombios_crtc_overscan_setup(crtc, mode, adjusted_mode);
2618 amdgpu_atombios_crtc_scaler_setup(crtc);
2619 dce_v10_0_cursor_reset(crtc);
2620 /* update the hw version fpr dpm */
2621 amdgpu_crtc->hw_mode = *adjusted_mode;
2622
2623 return 0;
2624}
2625
2626static bool dce_v10_0_crtc_mode_fixup(struct drm_crtc *crtc,
2627 const struct drm_display_mode *mode,
2628 struct drm_display_mode *adjusted_mode)
2629{
2630 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2631 struct drm_device *dev = crtc->dev;
2632 struct drm_encoder *encoder;
2633
2634 /* assign the encoder to the amdgpu crtc to avoid repeated lookups later */
2635 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2636 if (encoder->crtc == crtc) {
2637 amdgpu_crtc->encoder = encoder;
2638 amdgpu_crtc->connector = amdgpu_get_connector_for_encoder(encoder);
2639 break;
2640 }
2641 }
2642 if ((amdgpu_crtc->encoder == NULL) || (amdgpu_crtc->connector == NULL)) {
2643 amdgpu_crtc->encoder = NULL;
2644 amdgpu_crtc->connector = NULL;
2645 return false;
2646 }
2647 if (!amdgpu_display_crtc_scaling_mode_fixup(crtc, mode, adjusted_mode))
2648 return false;
2649 if (amdgpu_atombios_crtc_prepare_pll(crtc, adjusted_mode))
2650 return false;
2651 /* pick pll */
2652 amdgpu_crtc->pll_id = dce_v10_0_pick_pll(crtc);
2653 /* if we can't get a PPLL for a non-DP encoder, fail */
2654 if ((amdgpu_crtc->pll_id == ATOM_PPLL_INVALID) &&
2655 !ENCODER_MODE_IS_DP(amdgpu_atombios_encoder_get_encoder_mode(amdgpu_crtc->encoder)))
2656 return false;
2657
2658 return true;
2659}
2660
2661static int dce_v10_0_crtc_set_base(struct drm_crtc *crtc, int x, int y,
2662 struct drm_framebuffer *old_fb)
2663{
2664 return dce_v10_0_crtc_do_set_base(crtc, old_fb, x, y, 0);
2665}
2666
2667static int dce_v10_0_crtc_set_base_atomic(struct drm_crtc *crtc,
2668 struct drm_framebuffer *fb,
2669 int x, int y, enum mode_set_atomic state)
2670{
2671 return dce_v10_0_crtc_do_set_base(crtc, fb, x, y, 1);
2672}
2673
2674static const struct drm_crtc_helper_funcs dce_v10_0_crtc_helper_funcs = {
2675 .dpms = dce_v10_0_crtc_dpms,
2676 .mode_fixup = dce_v10_0_crtc_mode_fixup,
2677 .mode_set = dce_v10_0_crtc_mode_set,
2678 .mode_set_base = dce_v10_0_crtc_set_base,
2679 .mode_set_base_atomic = dce_v10_0_crtc_set_base_atomic,
2680 .prepare = dce_v10_0_crtc_prepare,
2681 .commit = dce_v10_0_crtc_commit,
2682 .disable = dce_v10_0_crtc_disable,
2683 .get_scanout_position = amdgpu_crtc_get_scanout_position,
2684};
2685
2686static int dce_v10_0_crtc_init(struct amdgpu_device *adev, int index)
2687{
2688 struct amdgpu_crtc *amdgpu_crtc;
2689
2690 amdgpu_crtc = kzalloc(sizeof(struct amdgpu_crtc) +
2691 (AMDGPUFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
2692 if (amdgpu_crtc == NULL)
2693 return -ENOMEM;
2694
2695 drm_crtc_init(adev_to_drm(adev), &amdgpu_crtc->base, &dce_v10_0_crtc_funcs);
2696
2697 drm_mode_crtc_set_gamma_size(&amdgpu_crtc->base, 256);
2698 amdgpu_crtc->crtc_id = index;
2699 adev->mode_info.crtcs[index] = amdgpu_crtc;
2700
2701 amdgpu_crtc->max_cursor_width = 128;
2702 amdgpu_crtc->max_cursor_height = 128;
2703 adev_to_drm(adev)->mode_config.cursor_width = amdgpu_crtc->max_cursor_width;
2704 adev_to_drm(adev)->mode_config.cursor_height = amdgpu_crtc->max_cursor_height;
2705
2706 switch (amdgpu_crtc->crtc_id) {
2707 case 0:
2708 default:
2709 amdgpu_crtc->crtc_offset = CRTC0_REGISTER_OFFSET;
2710 break;
2711 case 1:
2712 amdgpu_crtc->crtc_offset = CRTC1_REGISTER_OFFSET;
2713 break;
2714 case 2:
2715 amdgpu_crtc->crtc_offset = CRTC2_REGISTER_OFFSET;
2716 break;
2717 case 3:
2718 amdgpu_crtc->crtc_offset = CRTC3_REGISTER_OFFSET;
2719 break;
2720 case 4:
2721 amdgpu_crtc->crtc_offset = CRTC4_REGISTER_OFFSET;
2722 break;
2723 case 5:
2724 amdgpu_crtc->crtc_offset = CRTC5_REGISTER_OFFSET;
2725 break;
2726 }
2727
2728 amdgpu_crtc->pll_id = ATOM_PPLL_INVALID;
2729 amdgpu_crtc->adjusted_clock = 0;
2730 amdgpu_crtc->encoder = NULL;
2731 amdgpu_crtc->connector = NULL;
2732 drm_crtc_helper_add(&amdgpu_crtc->base, &dce_v10_0_crtc_helper_funcs);
2733
2734 return 0;
2735}
2736
2737static int dce_v10_0_early_init(void *handle)
2738{
2739 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2740
2741 adev->audio_endpt_rreg = &dce_v10_0_audio_endpt_rreg;
2742 adev->audio_endpt_wreg = &dce_v10_0_audio_endpt_wreg;
2743
2744 dce_v10_0_set_display_funcs(adev);
2745
2746 adev->mode_info.num_crtc = dce_v10_0_get_num_crtc(adev);
2747
2748 switch (adev->asic_type) {
2749 case CHIP_FIJI:
2750 case CHIP_TONGA:
2751 adev->mode_info.num_hpd = 6;
2752 adev->mode_info.num_dig = 7;
2753 break;
2754 default:
2755 /* FIXME: not supported yet */
2756 return -EINVAL;
2757 }
2758
2759 dce_v10_0_set_irq_funcs(adev);
2760
2761 return 0;
2762}
2763
2764static int dce_v10_0_sw_init(void *handle)
2765{
2766 int r, i;
2767 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2768
2769 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2770 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, i + 1, &adev->crtc_irq);
2771 if (r)
2772 return r;
2773 }
2774
2775 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP; i < 20; i += 2) {
2776 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, i, &adev->pageflip_irq);
2777 if (r)
2778 return r;
2779 }
2780
2781 /* HPD hotplug */
2782 r = amdgpu_irq_add_id(adev, AMDGPU_IRQ_CLIENTID_LEGACY, VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq);
2783 if (r)
2784 return r;
2785
2786 adev_to_drm(adev)->mode_config.funcs = &amdgpu_mode_funcs;
2787
2788 adev_to_drm(adev)->mode_config.async_page_flip = true;
2789
2790 adev_to_drm(adev)->mode_config.max_width = 16384;
2791 adev_to_drm(adev)->mode_config.max_height = 16384;
2792
2793 adev_to_drm(adev)->mode_config.preferred_depth = 24;
2794 adev_to_drm(adev)->mode_config.prefer_shadow = 1;
2795
2796 adev_to_drm(adev)->mode_config.fb_modifiers_not_supported = true;
2797
2798 r = amdgpu_display_modeset_create_props(adev);
2799 if (r)
2800 return r;
2801
2802 adev_to_drm(adev)->mode_config.max_width = 16384;
2803 adev_to_drm(adev)->mode_config.max_height = 16384;
2804
2805 /* allocate crtcs */
2806 for (i = 0; i < adev->mode_info.num_crtc; i++) {
2807 r = dce_v10_0_crtc_init(adev, i);
2808 if (r)
2809 return r;
2810 }
2811
2812 if (amdgpu_atombios_get_connector_info_from_object_table(adev))
2813 amdgpu_display_print_display_setup(adev_to_drm(adev));
2814 else
2815 return -EINVAL;
2816
2817 /* setup afmt */
2818 r = dce_v10_0_afmt_init(adev);
2819 if (r)
2820 return r;
2821
2822 r = dce_v10_0_audio_init(adev);
2823 if (r)
2824 return r;
2825
2826 /* Disable vblank IRQs aggressively for power-saving */
2827 /* XXX: can this be enabled for DC? */
2828 adev_to_drm(adev)->vblank_disable_immediate = true;
2829
2830 r = drm_vblank_init(adev_to_drm(adev), adev->mode_info.num_crtc);
2831 if (r)
2832 return r;
2833
2834 INIT_DELAYED_WORK(&adev->hotplug_work,
2835 amdgpu_display_hotplug_work_func);
2836
2837 drm_kms_helper_poll_init(adev_to_drm(adev));
2838
2839 adev->mode_info.mode_config_initialized = true;
2840 return 0;
2841}
2842
2843static int dce_v10_0_sw_fini(void *handle)
2844{
2845 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2846
2847 kfree(adev->mode_info.bios_hardcoded_edid);
2848
2849 drm_kms_helper_poll_fini(adev_to_drm(adev));
2850
2851 dce_v10_0_audio_fini(adev);
2852
2853 dce_v10_0_afmt_fini(adev);
2854
2855 drm_mode_config_cleanup(adev_to_drm(adev));
2856 adev->mode_info.mode_config_initialized = false;
2857
2858 return 0;
2859}
2860
2861static int dce_v10_0_hw_init(void *handle)
2862{
2863 int i;
2864 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2865
2866 dce_v10_0_init_golden_registers(adev);
2867
2868 /* disable vga render */
2869 dce_v10_0_set_vga_render_state(adev, false);
2870 /* init dig PHYs, disp eng pll */
2871 amdgpu_atombios_encoder_init_dig(adev);
2872 amdgpu_atombios_crtc_set_disp_eng_pll(adev, adev->clock.default_dispclk);
2873
2874 /* initialize hpd */
2875 dce_v10_0_hpd_init(adev);
2876
2877 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
2878 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
2879 }
2880
2881 dce_v10_0_pageflip_interrupt_init(adev);
2882
2883 return 0;
2884}
2885
2886static int dce_v10_0_hw_fini(void *handle)
2887{
2888 int i;
2889 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2890
2891 dce_v10_0_hpd_fini(adev);
2892
2893 for (i = 0; i < adev->mode_info.audio.num_pins; i++) {
2894 dce_v10_0_audio_enable(adev, &adev->mode_info.audio.pin[i], false);
2895 }
2896
2897 dce_v10_0_pageflip_interrupt_fini(adev);
2898
2899 flush_delayed_work(&adev->hotplug_work);
2900
2901 return 0;
2902}
2903
2904static int dce_v10_0_suspend(void *handle)
2905{
2906 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2907 int r;
2908
2909 r = amdgpu_display_suspend_helper(adev);
2910 if (r)
2911 return r;
2912
2913 adev->mode_info.bl_level =
2914 amdgpu_atombios_encoder_get_backlight_level_from_reg(adev);
2915
2916 return dce_v10_0_hw_fini(handle);
2917}
2918
2919static int dce_v10_0_resume(void *handle)
2920{
2921 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2922 int ret;
2923
2924 amdgpu_atombios_encoder_set_backlight_level_to_reg(adev,
2925 adev->mode_info.bl_level);
2926
2927 ret = dce_v10_0_hw_init(handle);
2928
2929 /* turn on the BL */
2930 if (adev->mode_info.bl_encoder) {
2931 u8 bl_level = amdgpu_display_backlight_get_level(adev,
2932 adev->mode_info.bl_encoder);
2933 amdgpu_display_backlight_set_level(adev, adev->mode_info.bl_encoder,
2934 bl_level);
2935 }
2936 if (ret)
2937 return ret;
2938
2939 return amdgpu_display_resume_helper(adev);
2940}
2941
2942static bool dce_v10_0_is_idle(void *handle)
2943{
2944 return true;
2945}
2946
2947static int dce_v10_0_wait_for_idle(void *handle)
2948{
2949 return 0;
2950}
2951
2952static bool dce_v10_0_check_soft_reset(void *handle)
2953{
2954 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2955
2956 return dce_v10_0_is_display_hung(adev);
2957}
2958
2959static int dce_v10_0_soft_reset(void *handle)
2960{
2961 u32 srbm_soft_reset = 0, tmp;
2962 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2963
2964 if (dce_v10_0_is_display_hung(adev))
2965 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_DC_MASK;
2966
2967 if (srbm_soft_reset) {
2968 tmp = RREG32(mmSRBM_SOFT_RESET);
2969 tmp |= srbm_soft_reset;
2970 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
2971 WREG32(mmSRBM_SOFT_RESET, tmp);
2972 tmp = RREG32(mmSRBM_SOFT_RESET);
2973
2974 udelay(50);
2975
2976 tmp &= ~srbm_soft_reset;
2977 WREG32(mmSRBM_SOFT_RESET, tmp);
2978 tmp = RREG32(mmSRBM_SOFT_RESET);
2979
2980 /* Wait a little for things to settle down */
2981 udelay(50);
2982 }
2983 return 0;
2984}
2985
2986static void dce_v10_0_set_crtc_vblank_interrupt_state(struct amdgpu_device *adev,
2987 int crtc,
2988 enum amdgpu_interrupt_state state)
2989{
2990 u32 lb_interrupt_mask;
2991
2992 if (crtc >= adev->mode_info.num_crtc) {
2993 DRM_DEBUG("invalid crtc %d\n", crtc);
2994 return;
2995 }
2996
2997 switch (state) {
2998 case AMDGPU_IRQ_STATE_DISABLE:
2999 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3000 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3001 VBLANK_INTERRUPT_MASK, 0);
3002 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3003 break;
3004 case AMDGPU_IRQ_STATE_ENABLE:
3005 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3006 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3007 VBLANK_INTERRUPT_MASK, 1);
3008 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3009 break;
3010 default:
3011 break;
3012 }
3013}
3014
3015static void dce_v10_0_set_crtc_vline_interrupt_state(struct amdgpu_device *adev,
3016 int crtc,
3017 enum amdgpu_interrupt_state state)
3018{
3019 u32 lb_interrupt_mask;
3020
3021 if (crtc >= adev->mode_info.num_crtc) {
3022 DRM_DEBUG("invalid crtc %d\n", crtc);
3023 return;
3024 }
3025
3026 switch (state) {
3027 case AMDGPU_IRQ_STATE_DISABLE:
3028 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3029 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3030 VLINE_INTERRUPT_MASK, 0);
3031 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3032 break;
3033 case AMDGPU_IRQ_STATE_ENABLE:
3034 lb_interrupt_mask = RREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc]);
3035 lb_interrupt_mask = REG_SET_FIELD(lb_interrupt_mask, LB_INTERRUPT_MASK,
3036 VLINE_INTERRUPT_MASK, 1);
3037 WREG32(mmLB_INTERRUPT_MASK + crtc_offsets[crtc], lb_interrupt_mask);
3038 break;
3039 default:
3040 break;
3041 }
3042}
3043
3044static int dce_v10_0_set_hpd_irq_state(struct amdgpu_device *adev,
3045 struct amdgpu_irq_src *source,
3046 unsigned hpd,
3047 enum amdgpu_interrupt_state state)
3048{
3049 u32 tmp;
3050
3051 if (hpd >= adev->mode_info.num_hpd) {
3052 DRM_DEBUG("invalid hdp %d\n", hpd);
3053 return 0;
3054 }
3055
3056 switch (state) {
3057 case AMDGPU_IRQ_STATE_DISABLE:
3058 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3059 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 0);
3060 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3061 break;
3062 case AMDGPU_IRQ_STATE_ENABLE:
3063 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3064 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_EN, 1);
3065 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3066 break;
3067 default:
3068 break;
3069 }
3070
3071 return 0;
3072}
3073
3074static int dce_v10_0_set_crtc_irq_state(struct amdgpu_device *adev,
3075 struct amdgpu_irq_src *source,
3076 unsigned type,
3077 enum amdgpu_interrupt_state state)
3078{
3079 switch (type) {
3080 case AMDGPU_CRTC_IRQ_VBLANK1:
3081 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 0, state);
3082 break;
3083 case AMDGPU_CRTC_IRQ_VBLANK2:
3084 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 1, state);
3085 break;
3086 case AMDGPU_CRTC_IRQ_VBLANK3:
3087 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 2, state);
3088 break;
3089 case AMDGPU_CRTC_IRQ_VBLANK4:
3090 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 3, state);
3091 break;
3092 case AMDGPU_CRTC_IRQ_VBLANK5:
3093 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 4, state);
3094 break;
3095 case AMDGPU_CRTC_IRQ_VBLANK6:
3096 dce_v10_0_set_crtc_vblank_interrupt_state(adev, 5, state);
3097 break;
3098 case AMDGPU_CRTC_IRQ_VLINE1:
3099 dce_v10_0_set_crtc_vline_interrupt_state(adev, 0, state);
3100 break;
3101 case AMDGPU_CRTC_IRQ_VLINE2:
3102 dce_v10_0_set_crtc_vline_interrupt_state(adev, 1, state);
3103 break;
3104 case AMDGPU_CRTC_IRQ_VLINE3:
3105 dce_v10_0_set_crtc_vline_interrupt_state(adev, 2, state);
3106 break;
3107 case AMDGPU_CRTC_IRQ_VLINE4:
3108 dce_v10_0_set_crtc_vline_interrupt_state(adev, 3, state);
3109 break;
3110 case AMDGPU_CRTC_IRQ_VLINE5:
3111 dce_v10_0_set_crtc_vline_interrupt_state(adev, 4, state);
3112 break;
3113 case AMDGPU_CRTC_IRQ_VLINE6:
3114 dce_v10_0_set_crtc_vline_interrupt_state(adev, 5, state);
3115 break;
3116 default:
3117 break;
3118 }
3119 return 0;
3120}
3121
3122static int dce_v10_0_set_pageflip_irq_state(struct amdgpu_device *adev,
3123 struct amdgpu_irq_src *src,
3124 unsigned type,
3125 enum amdgpu_interrupt_state state)
3126{
3127 u32 reg;
3128
3129 if (type >= adev->mode_info.num_crtc) {
3130 DRM_ERROR("invalid pageflip crtc %d\n", type);
3131 return -EINVAL;
3132 }
3133
3134 reg = RREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type]);
3135 if (state == AMDGPU_IRQ_STATE_DISABLE)
3136 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3137 reg & ~GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3138 else
3139 WREG32(mmGRPH_INTERRUPT_CONTROL + crtc_offsets[type],
3140 reg | GRPH_INTERRUPT_CONTROL__GRPH_PFLIP_INT_MASK_MASK);
3141
3142 return 0;
3143}
3144
3145static int dce_v10_0_pageflip_irq(struct amdgpu_device *adev,
3146 struct amdgpu_irq_src *source,
3147 struct amdgpu_iv_entry *entry)
3148{
3149 unsigned long flags;
3150 unsigned crtc_id;
3151 struct amdgpu_crtc *amdgpu_crtc;
3152 struct amdgpu_flip_work *works;
3153
3154 crtc_id = (entry->src_id - 8) >> 1;
3155 amdgpu_crtc = adev->mode_info.crtcs[crtc_id];
3156
3157 if (crtc_id >= adev->mode_info.num_crtc) {
3158 DRM_ERROR("invalid pageflip crtc %d\n", crtc_id);
3159 return -EINVAL;
3160 }
3161
3162 if (RREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id]) &
3163 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_OCCURRED_MASK)
3164 WREG32(mmGRPH_INTERRUPT_STATUS + crtc_offsets[crtc_id],
3165 GRPH_INTERRUPT_STATUS__GRPH_PFLIP_INT_CLEAR_MASK);
3166
3167 /* IRQ could occur when in initial stage */
3168 if (amdgpu_crtc == NULL)
3169 return 0;
3170
3171 spin_lock_irqsave(&adev_to_drm(adev)->event_lock, flags);
3172 works = amdgpu_crtc->pflip_works;
3173 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED) {
3174 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d != "
3175 "AMDGPU_FLIP_SUBMITTED(%d)\n",
3176 amdgpu_crtc->pflip_status,
3177 AMDGPU_FLIP_SUBMITTED);
3178 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3179 return 0;
3180 }
3181
3182 /* page flip completed. clean up */
3183 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE;
3184 amdgpu_crtc->pflip_works = NULL;
3185
3186 /* wakeup usersapce */
3187 if (works->event)
3188 drm_crtc_send_vblank_event(&amdgpu_crtc->base, works->event);
3189
3190 spin_unlock_irqrestore(&adev_to_drm(adev)->event_lock, flags);
3191
3192 drm_crtc_vblank_put(&amdgpu_crtc->base);
3193 schedule_work(&works->unpin_work);
3194
3195 return 0;
3196}
3197
3198static void dce_v10_0_hpd_int_ack(struct amdgpu_device *adev,
3199 int hpd)
3200{
3201 u32 tmp;
3202
3203 if (hpd >= adev->mode_info.num_hpd) {
3204 DRM_DEBUG("invalid hdp %d\n", hpd);
3205 return;
3206 }
3207
3208 tmp = RREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd]);
3209 tmp = REG_SET_FIELD(tmp, DC_HPD_INT_CONTROL, DC_HPD_INT_ACK, 1);
3210 WREG32(mmDC_HPD_INT_CONTROL + hpd_offsets[hpd], tmp);
3211}
3212
3213static void dce_v10_0_crtc_vblank_int_ack(struct amdgpu_device *adev,
3214 int crtc)
3215{
3216 u32 tmp;
3217
3218 if (crtc >= adev->mode_info.num_crtc) {
3219 DRM_DEBUG("invalid crtc %d\n", crtc);
3220 return;
3221 }
3222
3223 tmp = RREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc]);
3224 tmp = REG_SET_FIELD(tmp, LB_VBLANK_STATUS, VBLANK_ACK, 1);
3225 WREG32(mmLB_VBLANK_STATUS + crtc_offsets[crtc], tmp);
3226}
3227
3228static void dce_v10_0_crtc_vline_int_ack(struct amdgpu_device *adev,
3229 int crtc)
3230{
3231 u32 tmp;
3232
3233 if (crtc >= adev->mode_info.num_crtc) {
3234 DRM_DEBUG("invalid crtc %d\n", crtc);
3235 return;
3236 }
3237
3238 tmp = RREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc]);
3239 tmp = REG_SET_FIELD(tmp, LB_VLINE_STATUS, VLINE_ACK, 1);
3240 WREG32(mmLB_VLINE_STATUS + crtc_offsets[crtc], tmp);
3241}
3242
3243static int dce_v10_0_crtc_irq(struct amdgpu_device *adev,
3244 struct amdgpu_irq_src *source,
3245 struct amdgpu_iv_entry *entry)
3246{
3247 unsigned crtc = entry->src_id - 1;
3248 uint32_t disp_int = RREG32(interrupt_status_offsets[crtc].reg);
3249 unsigned int irq_type = amdgpu_display_crtc_idx_to_irq_type(adev, crtc);
3250
3251 switch (entry->src_data[0]) {
3252 case 0: /* vblank */
3253 if (disp_int & interrupt_status_offsets[crtc].vblank)
3254 dce_v10_0_crtc_vblank_int_ack(adev, crtc);
3255 else
3256 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3257
3258 if (amdgpu_irq_enabled(adev, source, irq_type)) {
3259 drm_handle_vblank(adev_to_drm(adev), crtc);
3260 }
3261 DRM_DEBUG("IH: D%d vblank\n", crtc + 1);
3262
3263 break;
3264 case 1: /* vline */
3265 if (disp_int & interrupt_status_offsets[crtc].vline)
3266 dce_v10_0_crtc_vline_int_ack(adev, crtc);
3267 else
3268 DRM_DEBUG("IH: IH event w/o asserted irq bit?\n");
3269
3270 DRM_DEBUG("IH: D%d vline\n", crtc + 1);
3271
3272 break;
3273 default:
3274 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3275 break;
3276 }
3277
3278 return 0;
3279}
3280
3281static int dce_v10_0_hpd_irq(struct amdgpu_device *adev,
3282 struct amdgpu_irq_src *source,
3283 struct amdgpu_iv_entry *entry)
3284{
3285 uint32_t disp_int, mask;
3286 unsigned hpd;
3287
3288 if (entry->src_data[0] >= adev->mode_info.num_hpd) {
3289 DRM_DEBUG("Unhandled interrupt: %d %d\n", entry->src_id, entry->src_data[0]);
3290 return 0;
3291 }
3292
3293 hpd = entry->src_data[0];
3294 disp_int = RREG32(interrupt_status_offsets[hpd].reg);
3295 mask = interrupt_status_offsets[hpd].hpd;
3296
3297 if (disp_int & mask) {
3298 dce_v10_0_hpd_int_ack(adev, hpd);
3299 schedule_delayed_work(&adev->hotplug_work, 0);
3300 DRM_DEBUG("IH: HPD%d\n", hpd + 1);
3301 }
3302
3303 return 0;
3304}
3305
3306static int dce_v10_0_set_clockgating_state(void *handle,
3307 enum amd_clockgating_state state)
3308{
3309 return 0;
3310}
3311
3312static int dce_v10_0_set_powergating_state(void *handle,
3313 enum amd_powergating_state state)
3314{
3315 return 0;
3316}
3317
3318static const struct amd_ip_funcs dce_v10_0_ip_funcs = {
3319 .name = "dce_v10_0",
3320 .early_init = dce_v10_0_early_init,
3321 .late_init = NULL,
3322 .sw_init = dce_v10_0_sw_init,
3323 .sw_fini = dce_v10_0_sw_fini,
3324 .hw_init = dce_v10_0_hw_init,
3325 .hw_fini = dce_v10_0_hw_fini,
3326 .suspend = dce_v10_0_suspend,
3327 .resume = dce_v10_0_resume,
3328 .is_idle = dce_v10_0_is_idle,
3329 .wait_for_idle = dce_v10_0_wait_for_idle,
3330 .check_soft_reset = dce_v10_0_check_soft_reset,
3331 .soft_reset = dce_v10_0_soft_reset,
3332 .set_clockgating_state = dce_v10_0_set_clockgating_state,
3333 .set_powergating_state = dce_v10_0_set_powergating_state,
3334};
3335
3336static void
3337dce_v10_0_encoder_mode_set(struct drm_encoder *encoder,
3338 struct drm_display_mode *mode,
3339 struct drm_display_mode *adjusted_mode)
3340{
3341 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3342
3343 amdgpu_encoder->pixel_clock = adjusted_mode->clock;
3344
3345 /* need to call this here rather than in prepare() since we need some crtc info */
3346 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3347
3348 /* set scaler clears this on some chips */
3349 dce_v10_0_set_interleave(encoder->crtc, mode);
3350
3351 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI) {
3352 dce_v10_0_afmt_enable(encoder, true);
3353 dce_v10_0_afmt_setmode(encoder, adjusted_mode);
3354 }
3355}
3356
3357static void dce_v10_0_encoder_prepare(struct drm_encoder *encoder)
3358{
3359 struct amdgpu_device *adev = drm_to_adev(encoder->dev);
3360 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3361 struct drm_connector *connector = amdgpu_get_connector_for_encoder(encoder);
3362
3363 if ((amdgpu_encoder->active_device &
3364 (ATOM_DEVICE_DFP_SUPPORT | ATOM_DEVICE_LCD_SUPPORT)) ||
3365 (amdgpu_encoder_get_dp_bridge_encoder_id(encoder) !=
3366 ENCODER_OBJECT_ID_NONE)) {
3367 struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
3368 if (dig) {
3369 dig->dig_encoder = dce_v10_0_pick_dig_encoder(encoder);
3370 if (amdgpu_encoder->active_device & ATOM_DEVICE_DFP_SUPPORT)
3371 dig->afmt = adev->mode_info.afmt[dig->dig_encoder];
3372 }
3373 }
3374
3375 amdgpu_atombios_scratch_regs_lock(adev, true);
3376
3377 if (connector) {
3378 struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
3379
3380 /* select the clock/data port if it uses a router */
3381 if (amdgpu_connector->router.cd_valid)
3382 amdgpu_i2c_router_select_cd_port(amdgpu_connector);
3383
3384 /* turn eDP panel on for mode set */
3385 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
3386 amdgpu_atombios_encoder_set_edp_panel_power(connector,
3387 ATOM_TRANSMITTER_ACTION_POWER_ON);
3388 }
3389
3390 /* this is needed for the pll/ss setup to work correctly in some cases */
3391 amdgpu_atombios_encoder_set_crtc_source(encoder);
3392 /* set up the FMT blocks */
3393 dce_v10_0_program_fmt(encoder);
3394}
3395
3396static void dce_v10_0_encoder_commit(struct drm_encoder *encoder)
3397{
3398 struct drm_device *dev = encoder->dev;
3399 struct amdgpu_device *adev = drm_to_adev(dev);
3400
3401 /* need to call this here as we need the crtc set up */
3402 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
3403 amdgpu_atombios_scratch_regs_lock(adev, false);
3404}
3405
3406static void dce_v10_0_encoder_disable(struct drm_encoder *encoder)
3407{
3408 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3409 struct amdgpu_encoder_atom_dig *dig;
3410
3411 amdgpu_atombios_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
3412
3413 if (amdgpu_atombios_encoder_is_digital(encoder)) {
3414 if (amdgpu_atombios_encoder_get_encoder_mode(encoder) == ATOM_ENCODER_MODE_HDMI)
3415 dce_v10_0_afmt_enable(encoder, false);
3416 dig = amdgpu_encoder->enc_priv;
3417 dig->dig_encoder = -1;
3418 }
3419 amdgpu_encoder->active_device = 0;
3420}
3421
3422/* these are handled by the primary encoders */
3423static void dce_v10_0_ext_prepare(struct drm_encoder *encoder)
3424{
3425
3426}
3427
3428static void dce_v10_0_ext_commit(struct drm_encoder *encoder)
3429{
3430
3431}
3432
3433static void
3434dce_v10_0_ext_mode_set(struct drm_encoder *encoder,
3435 struct drm_display_mode *mode,
3436 struct drm_display_mode *adjusted_mode)
3437{
3438
3439}
3440
3441static void dce_v10_0_ext_disable(struct drm_encoder *encoder)
3442{
3443
3444}
3445
3446static void
3447dce_v10_0_ext_dpms(struct drm_encoder *encoder, int mode)
3448{
3449
3450}
3451
3452static const struct drm_encoder_helper_funcs dce_v10_0_ext_helper_funcs = {
3453 .dpms = dce_v10_0_ext_dpms,
3454 .prepare = dce_v10_0_ext_prepare,
3455 .mode_set = dce_v10_0_ext_mode_set,
3456 .commit = dce_v10_0_ext_commit,
3457 .disable = dce_v10_0_ext_disable,
3458 /* no detect for TMDS/LVDS yet */
3459};
3460
3461static const struct drm_encoder_helper_funcs dce_v10_0_dig_helper_funcs = {
3462 .dpms = amdgpu_atombios_encoder_dpms,
3463 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3464 .prepare = dce_v10_0_encoder_prepare,
3465 .mode_set = dce_v10_0_encoder_mode_set,
3466 .commit = dce_v10_0_encoder_commit,
3467 .disable = dce_v10_0_encoder_disable,
3468 .detect = amdgpu_atombios_encoder_dig_detect,
3469};
3470
3471static const struct drm_encoder_helper_funcs dce_v10_0_dac_helper_funcs = {
3472 .dpms = amdgpu_atombios_encoder_dpms,
3473 .mode_fixup = amdgpu_atombios_encoder_mode_fixup,
3474 .prepare = dce_v10_0_encoder_prepare,
3475 .mode_set = dce_v10_0_encoder_mode_set,
3476 .commit = dce_v10_0_encoder_commit,
3477 .detect = amdgpu_atombios_encoder_dac_detect,
3478};
3479
3480static void dce_v10_0_encoder_destroy(struct drm_encoder *encoder)
3481{
3482 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
3483 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3484 amdgpu_atombios_encoder_fini_backlight(amdgpu_encoder);
3485 kfree(amdgpu_encoder->enc_priv);
3486 drm_encoder_cleanup(encoder);
3487 kfree(amdgpu_encoder);
3488}
3489
3490static const struct drm_encoder_funcs dce_v10_0_encoder_funcs = {
3491 .destroy = dce_v10_0_encoder_destroy,
3492};
3493
3494static void dce_v10_0_encoder_add(struct amdgpu_device *adev,
3495 uint32_t encoder_enum,
3496 uint32_t supported_device,
3497 u16 caps)
3498{
3499 struct drm_device *dev = adev_to_drm(adev);
3500 struct drm_encoder *encoder;
3501 struct amdgpu_encoder *amdgpu_encoder;
3502
3503 /* see if we already added it */
3504 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3505 amdgpu_encoder = to_amdgpu_encoder(encoder);
3506 if (amdgpu_encoder->encoder_enum == encoder_enum) {
3507 amdgpu_encoder->devices |= supported_device;
3508 return;
3509 }
3510
3511 }
3512
3513 /* add a new one */
3514 amdgpu_encoder = kzalloc(sizeof(struct amdgpu_encoder), GFP_KERNEL);
3515 if (!amdgpu_encoder)
3516 return;
3517
3518 encoder = &amdgpu_encoder->base;
3519 switch (adev->mode_info.num_crtc) {
3520 case 1:
3521 encoder->possible_crtcs = 0x1;
3522 break;
3523 case 2:
3524 default:
3525 encoder->possible_crtcs = 0x3;
3526 break;
3527 case 4:
3528 encoder->possible_crtcs = 0xf;
3529 break;
3530 case 6:
3531 encoder->possible_crtcs = 0x3f;
3532 break;
3533 }
3534
3535 amdgpu_encoder->enc_priv = NULL;
3536
3537 amdgpu_encoder->encoder_enum = encoder_enum;
3538 amdgpu_encoder->encoder_id = (encoder_enum & OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
3539 amdgpu_encoder->devices = supported_device;
3540 amdgpu_encoder->rmx_type = RMX_OFF;
3541 amdgpu_encoder->underscan_type = UNDERSCAN_OFF;
3542 amdgpu_encoder->is_ext_encoder = false;
3543 amdgpu_encoder->caps = caps;
3544
3545 switch (amdgpu_encoder->encoder_id) {
3546 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC1:
3547 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DAC2:
3548 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3549 DRM_MODE_ENCODER_DAC, NULL);
3550 drm_encoder_helper_add(encoder, &dce_v10_0_dac_helper_funcs);
3551 break;
3552 case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
3553 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
3554 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
3555 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
3556 case ENCODER_OBJECT_ID_INTERNAL_UNIPHY3:
3557 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT)) {
3558 amdgpu_encoder->rmx_type = RMX_FULL;
3559 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3560 DRM_MODE_ENCODER_LVDS, NULL);
3561 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_lcd_info(amdgpu_encoder);
3562 } else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT)) {
3563 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3564 DRM_MODE_ENCODER_DAC, NULL);
3565 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3566 } else {
3567 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3568 DRM_MODE_ENCODER_TMDS, NULL);
3569 amdgpu_encoder->enc_priv = amdgpu_atombios_encoder_get_dig_info(amdgpu_encoder);
3570 }
3571 drm_encoder_helper_add(encoder, &dce_v10_0_dig_helper_funcs);
3572 break;
3573 case ENCODER_OBJECT_ID_SI170B:
3574 case ENCODER_OBJECT_ID_CH7303:
3575 case ENCODER_OBJECT_ID_EXTERNAL_SDVOA:
3576 case ENCODER_OBJECT_ID_EXTERNAL_SDVOB:
3577 case ENCODER_OBJECT_ID_TITFP513:
3578 case ENCODER_OBJECT_ID_VT1623:
3579 case ENCODER_OBJECT_ID_HDMI_SI1930:
3580 case ENCODER_OBJECT_ID_TRAVIS:
3581 case ENCODER_OBJECT_ID_NUTMEG:
3582 /* these are handled by the primary encoders */
3583 amdgpu_encoder->is_ext_encoder = true;
3584 if (amdgpu_encoder->devices & (ATOM_DEVICE_LCD_SUPPORT))
3585 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3586 DRM_MODE_ENCODER_LVDS, NULL);
3587 else if (amdgpu_encoder->devices & (ATOM_DEVICE_CRT_SUPPORT))
3588 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3589 DRM_MODE_ENCODER_DAC, NULL);
3590 else
3591 drm_encoder_init(dev, encoder, &dce_v10_0_encoder_funcs,
3592 DRM_MODE_ENCODER_TMDS, NULL);
3593 drm_encoder_helper_add(encoder, &dce_v10_0_ext_helper_funcs);
3594 break;
3595 }
3596}
3597
3598static const struct amdgpu_display_funcs dce_v10_0_display_funcs = {
3599 .bandwidth_update = &dce_v10_0_bandwidth_update,
3600 .vblank_get_counter = &dce_v10_0_vblank_get_counter,
3601 .backlight_set_level = &amdgpu_atombios_encoder_set_backlight_level,
3602 .backlight_get_level = &amdgpu_atombios_encoder_get_backlight_level,
3603 .hpd_sense = &dce_v10_0_hpd_sense,
3604 .hpd_set_polarity = &dce_v10_0_hpd_set_polarity,
3605 .hpd_get_gpio_reg = &dce_v10_0_hpd_get_gpio_reg,
3606 .page_flip = &dce_v10_0_page_flip,
3607 .page_flip_get_scanoutpos = &dce_v10_0_crtc_get_scanoutpos,
3608 .add_encoder = &dce_v10_0_encoder_add,
3609 .add_connector = &amdgpu_connector_add,
3610};
3611
3612static void dce_v10_0_set_display_funcs(struct amdgpu_device *adev)
3613{
3614 adev->mode_info.funcs = &dce_v10_0_display_funcs;
3615}
3616
3617static const struct amdgpu_irq_src_funcs dce_v10_0_crtc_irq_funcs = {
3618 .set = dce_v10_0_set_crtc_irq_state,
3619 .process = dce_v10_0_crtc_irq,
3620};
3621
3622static const struct amdgpu_irq_src_funcs dce_v10_0_pageflip_irq_funcs = {
3623 .set = dce_v10_0_set_pageflip_irq_state,
3624 .process = dce_v10_0_pageflip_irq,
3625};
3626
3627static const struct amdgpu_irq_src_funcs dce_v10_0_hpd_irq_funcs = {
3628 .set = dce_v10_0_set_hpd_irq_state,
3629 .process = dce_v10_0_hpd_irq,
3630};
3631
3632static void dce_v10_0_set_irq_funcs(struct amdgpu_device *adev)
3633{
3634 if (adev->mode_info.num_crtc > 0)
3635 adev->crtc_irq.num_types = AMDGPU_CRTC_IRQ_VLINE1 + adev->mode_info.num_crtc;
3636 else
3637 adev->crtc_irq.num_types = 0;
3638 adev->crtc_irq.funcs = &dce_v10_0_crtc_irq_funcs;
3639
3640 adev->pageflip_irq.num_types = adev->mode_info.num_crtc;
3641 adev->pageflip_irq.funcs = &dce_v10_0_pageflip_irq_funcs;
3642
3643 adev->hpd_irq.num_types = adev->mode_info.num_hpd;
3644 adev->hpd_irq.funcs = &dce_v10_0_hpd_irq_funcs;
3645}
3646
3647const struct amdgpu_ip_block_version dce_v10_0_ip_block = {
3648 .type = AMD_IP_BLOCK_TYPE_DCE,
3649 .major = 10,
3650 .minor = 0,
3651 .rev = 0,
3652 .funcs = &dce_v10_0_ip_funcs,
3653};
3654
3655const struct amdgpu_ip_block_version dce_v10_1_ip_block = {
3656 .type = AMD_IP_BLOCK_TYPE_DCE,
3657 .major = 10,
3658 .minor = 1,
3659 .rev = 0,
3660 .funcs = &dce_v10_0_ip_funcs,
3661};