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