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