Merge drm/drm-next into drm-intel-next
[platform/kernel/linux-starfive.git] / drivers / gpu / drm / i915 / display / intel_display.c
1 /*
2  * Copyright © 2006-2007 Intel Corporation
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Eric Anholt <eric@anholt.net>
25  */
26
27 #include <acpi/video.h>
28 #include <linux/i2c.h>
29 #include <linux/input.h>
30 #include <linux/intel-iommu.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/dma-resv.h>
34 #include <linux/slab.h>
35
36 #include <drm/drm_atomic.h>
37 #include <drm/drm_atomic_helper.h>
38 #include <drm/drm_atomic_uapi.h>
39 #include <drm/drm_damage_helper.h>
40 #include <drm/drm_dp_helper.h>
41 #include <drm/drm_edid.h>
42 #include <drm/drm_fourcc.h>
43 #include <drm/drm_plane_helper.h>
44 #include <drm/drm_probe_helper.h>
45 #include <drm/drm_rect.h>
46
47 #include "display/intel_audio.h"
48 #include "display/intel_crt.h"
49 #include "display/intel_ddi.h"
50 #include "display/intel_display_debugfs.h"
51 #include "display/intel_dp.h"
52 #include "display/intel_dp_mst.h"
53 #include "display/intel_dpll.h"
54 #include "display/intel_dpll_mgr.h"
55 #include "display/intel_dsi.h"
56 #include "display/intel_dvo.h"
57 #include "display/intel_gmbus.h"
58 #include "display/intel_hdmi.h"
59 #include "display/intel_lvds.h"
60 #include "display/intel_sdvo.h"
61 #include "display/intel_tv.h"
62 #include "display/intel_vdsc.h"
63 #include "display/intel_vrr.h"
64
65 #include "gem/i915_gem_object.h"
66
67 #include "gt/intel_rps.h"
68
69 #include "i915_drv.h"
70 #include "intel_acpi.h"
71 #include "intel_atomic.h"
72 #include "intel_atomic_plane.h"
73 #include "intel_bw.h"
74 #include "intel_cdclk.h"
75 #include "intel_color.h"
76 #include "intel_crtc.h"
77 #include "intel_csr.h"
78 #include "intel_display_types.h"
79 #include "intel_dp_link_training.h"
80 #include "intel_fbc.h"
81 #include "intel_fdi.h"
82 #include "intel_fbdev.h"
83 #include "intel_fifo_underrun.h"
84 #include "intel_frontbuffer.h"
85 #include "intel_hdcp.h"
86 #include "intel_hotplug.h"
87 #include "intel_overlay.h"
88 #include "intel_pipe_crc.h"
89 #include "intel_pm.h"
90 #include "intel_pps.h"
91 #include "intel_psr.h"
92 #include "intel_quirks.h"
93 #include "intel_sideband.h"
94 #include "intel_sprite.h"
95 #include "intel_tc.h"
96 #include "intel_vga.h"
97 #include "i9xx_plane.h"
98 #include "skl_scaler.h"
99 #include "skl_universal_plane.h"
100
101 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
102                                 struct intel_crtc_state *pipe_config);
103 static void ilk_pch_clock_get(struct intel_crtc *crtc,
104                               struct intel_crtc_state *pipe_config);
105
106 static int intel_framebuffer_init(struct intel_framebuffer *ifb,
107                                   struct drm_i915_gem_object *obj,
108                                   struct drm_mode_fb_cmd2 *mode_cmd);
109 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state);
110 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
111 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
112                                          const struct intel_link_m_n *m_n,
113                                          const struct intel_link_m_n *m2_n2);
114 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
115 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
116 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state);
117 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
118 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
119 static void intel_modeset_setup_hw_state(struct drm_device *dev,
120                                          struct drm_modeset_acquire_ctx *ctx);
121
122 /* returns HPLL frequency in kHz */
123 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
124 {
125         int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
126
127         /* Obtain SKU information */
128         hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
129                 CCK_FUSE_HPLL_FREQ_MASK;
130
131         return vco_freq[hpll_freq] * 1000;
132 }
133
134 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
135                       const char *name, u32 reg, int ref_freq)
136 {
137         u32 val;
138         int divider;
139
140         val = vlv_cck_read(dev_priv, reg);
141         divider = val & CCK_FREQUENCY_VALUES;
142
143         drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
144                  (divider << CCK_FREQUENCY_STATUS_SHIFT),
145                  "%s change in progress\n", name);
146
147         return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
148 }
149
150 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
151                            const char *name, u32 reg)
152 {
153         int hpll;
154
155         vlv_cck_get(dev_priv);
156
157         if (dev_priv->hpll_freq == 0)
158                 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
159
160         hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
161
162         vlv_cck_put(dev_priv);
163
164         return hpll;
165 }
166
167 static void intel_update_czclk(struct drm_i915_private *dev_priv)
168 {
169         if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
170                 return;
171
172         dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
173                                                       CCK_CZ_CLOCK_CONTROL);
174
175         drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
176                 dev_priv->czclk_freq);
177 }
178
179 /* WA Display #0827: Gen9:all */
180 static void
181 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
182 {
183         if (enable)
184                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
185                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DUPS1_GATING_DIS | DUPS2_GATING_DIS);
186         else
187                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
188                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
189 }
190
191 /* Wa_2006604312:icl,ehl */
192 static void
193 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
194                        bool enable)
195 {
196         if (enable)
197                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
198                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
199         else
200                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
201                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
202 }
203
204 static bool
205 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
206 {
207         return crtc_state->master_transcoder != INVALID_TRANSCODER;
208 }
209
210 static bool
211 is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
212 {
213         return crtc_state->sync_mode_slaves_mask != 0;
214 }
215
216 bool
217 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
218 {
219         return is_trans_port_sync_master(crtc_state) ||
220                 is_trans_port_sync_slave(crtc_state);
221 }
222
223 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
224                                     enum pipe pipe)
225 {
226         i915_reg_t reg = PIPEDSL(pipe);
227         u32 line1, line2;
228         u32 line_mask;
229
230         if (IS_GEN(dev_priv, 2))
231                 line_mask = DSL_LINEMASK_GEN2;
232         else
233                 line_mask = DSL_LINEMASK_GEN3;
234
235         line1 = intel_de_read(dev_priv, reg) & line_mask;
236         msleep(5);
237         line2 = intel_de_read(dev_priv, reg) & line_mask;
238
239         return line1 != line2;
240 }
241
242 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
243 {
244         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
245         enum pipe pipe = crtc->pipe;
246
247         /* Wait for the display line to settle/start moving */
248         if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
249                 drm_err(&dev_priv->drm,
250                         "pipe %c scanline %s wait timed out\n",
251                         pipe_name(pipe), onoff(state));
252 }
253
254 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
255 {
256         wait_for_pipe_scanline_moving(crtc, false);
257 }
258
259 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
260 {
261         wait_for_pipe_scanline_moving(crtc, true);
262 }
263
264 static void
265 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
266 {
267         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
268         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
269
270         if (INTEL_GEN(dev_priv) >= 4) {
271                 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
272                 i915_reg_t reg = PIPECONF(cpu_transcoder);
273
274                 /* Wait for the Pipe State to go off */
275                 if (intel_de_wait_for_clear(dev_priv, reg,
276                                             I965_PIPECONF_ACTIVE, 100))
277                         drm_WARN(&dev_priv->drm, 1,
278                                  "pipe_off wait timed out\n");
279         } else {
280                 intel_wait_for_pipe_scanline_stopped(crtc);
281         }
282 }
283
284 /* Only for pre-ILK configs */
285 void assert_pll(struct drm_i915_private *dev_priv,
286                 enum pipe pipe, bool state)
287 {
288         u32 val;
289         bool cur_state;
290
291         val = intel_de_read(dev_priv, DPLL(pipe));
292         cur_state = !!(val & DPLL_VCO_ENABLE);
293         I915_STATE_WARN(cur_state != state,
294              "PLL state assertion failure (expected %s, current %s)\n",
295                         onoff(state), onoff(cur_state));
296 }
297
298 /* XXX: the dsi pll is shared between MIPI DSI ports */
299 void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
300 {
301         u32 val;
302         bool cur_state;
303
304         vlv_cck_get(dev_priv);
305         val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
306         vlv_cck_put(dev_priv);
307
308         cur_state = val & DSI_PLL_VCO_EN;
309         I915_STATE_WARN(cur_state != state,
310              "DSI PLL state assertion failure (expected %s, current %s)\n",
311                         onoff(state), onoff(cur_state));
312 }
313
314 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
315                           enum pipe pipe, bool state)
316 {
317         bool cur_state;
318
319         if (HAS_DDI(dev_priv)) {
320                 /*
321                  * DDI does not have a specific FDI_TX register.
322                  *
323                  * FDI is never fed from EDP transcoder
324                  * so pipe->transcoder cast is fine here.
325                  */
326                 enum transcoder cpu_transcoder = (enum transcoder)pipe;
327                 u32 val = intel_de_read(dev_priv,
328                                         TRANS_DDI_FUNC_CTL(cpu_transcoder));
329                 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
330         } else {
331                 u32 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
332                 cur_state = !!(val & FDI_TX_ENABLE);
333         }
334         I915_STATE_WARN(cur_state != state,
335              "FDI TX state assertion failure (expected %s, current %s)\n",
336                         onoff(state), onoff(cur_state));
337 }
338 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
339 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
340
341 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
342                           enum pipe pipe, bool state)
343 {
344         u32 val;
345         bool cur_state;
346
347         val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
348         cur_state = !!(val & FDI_RX_ENABLE);
349         I915_STATE_WARN(cur_state != state,
350              "FDI RX state assertion failure (expected %s, current %s)\n",
351                         onoff(state), onoff(cur_state));
352 }
353 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
354 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
355
356 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
357                                       enum pipe pipe)
358 {
359         u32 val;
360
361         /* ILK FDI PLL is always enabled */
362         if (IS_GEN(dev_priv, 5))
363                 return;
364
365         /* On Haswell, DDI ports are responsible for the FDI PLL setup */
366         if (HAS_DDI(dev_priv))
367                 return;
368
369         val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
370         I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
371 }
372
373 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
374                        enum pipe pipe, bool state)
375 {
376         u32 val;
377         bool cur_state;
378
379         val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
380         cur_state = !!(val & FDI_RX_PLL_ENABLE);
381         I915_STATE_WARN(cur_state != state,
382              "FDI RX PLL assertion failure (expected %s, current %s)\n",
383                         onoff(state), onoff(cur_state));
384 }
385
386 void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
387 {
388         i915_reg_t pp_reg;
389         u32 val;
390         enum pipe panel_pipe = INVALID_PIPE;
391         bool locked = true;
392
393         if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
394                 return;
395
396         if (HAS_PCH_SPLIT(dev_priv)) {
397                 u32 port_sel;
398
399                 pp_reg = PP_CONTROL(0);
400                 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
401
402                 switch (port_sel) {
403                 case PANEL_PORT_SELECT_LVDS:
404                         intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
405                         break;
406                 case PANEL_PORT_SELECT_DPA:
407                         intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
408                         break;
409                 case PANEL_PORT_SELECT_DPC:
410                         intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
411                         break;
412                 case PANEL_PORT_SELECT_DPD:
413                         intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
414                         break;
415                 default:
416                         MISSING_CASE(port_sel);
417                         break;
418                 }
419         } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
420                 /* presumably write lock depends on pipe, not port select */
421                 pp_reg = PP_CONTROL(pipe);
422                 panel_pipe = pipe;
423         } else {
424                 u32 port_sel;
425
426                 pp_reg = PP_CONTROL(0);
427                 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
428
429                 drm_WARN_ON(&dev_priv->drm,
430                             port_sel != PANEL_PORT_SELECT_LVDS);
431                 intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
432         }
433
434         val = intel_de_read(dev_priv, pp_reg);
435         if (!(val & PANEL_POWER_ON) ||
436             ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
437                 locked = false;
438
439         I915_STATE_WARN(panel_pipe == pipe && locked,
440              "panel assertion failure, pipe %c regs locked\n",
441              pipe_name(pipe));
442 }
443
444 void assert_pipe(struct drm_i915_private *dev_priv,
445                  enum transcoder cpu_transcoder, bool state)
446 {
447         bool cur_state;
448         enum intel_display_power_domain power_domain;
449         intel_wakeref_t wakeref;
450
451         /* we keep both pipes enabled on 830 */
452         if (IS_I830(dev_priv))
453                 state = true;
454
455         power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
456         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
457         if (wakeref) {
458                 u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
459                 cur_state = !!(val & PIPECONF_ENABLE);
460
461                 intel_display_power_put(dev_priv, power_domain, wakeref);
462         } else {
463                 cur_state = false;
464         }
465
466         I915_STATE_WARN(cur_state != state,
467                         "transcoder %s assertion failure (expected %s, current %s)\n",
468                         transcoder_name(cpu_transcoder),
469                         onoff(state), onoff(cur_state));
470 }
471
472 static void assert_plane(struct intel_plane *plane, bool state)
473 {
474         enum pipe pipe;
475         bool cur_state;
476
477         cur_state = plane->get_hw_state(plane, &pipe);
478
479         I915_STATE_WARN(cur_state != state,
480                         "%s assertion failure (expected %s, current %s)\n",
481                         plane->base.name, onoff(state), onoff(cur_state));
482 }
483
484 #define assert_plane_enabled(p) assert_plane(p, true)
485 #define assert_plane_disabled(p) assert_plane(p, false)
486
487 static void assert_planes_disabled(struct intel_crtc *crtc)
488 {
489         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
490         struct intel_plane *plane;
491
492         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
493                 assert_plane_disabled(plane);
494 }
495
496 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
497                                     enum pipe pipe)
498 {
499         u32 val;
500         bool enabled;
501
502         val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
503         enabled = !!(val & TRANS_ENABLE);
504         I915_STATE_WARN(enabled,
505              "transcoder assertion failed, should be off on pipe %c but is still active\n",
506              pipe_name(pipe));
507 }
508
509 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
510                                    enum pipe pipe, enum port port,
511                                    i915_reg_t dp_reg)
512 {
513         enum pipe port_pipe;
514         bool state;
515
516         state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
517
518         I915_STATE_WARN(state && port_pipe == pipe,
519                         "PCH DP %c enabled on transcoder %c, should be disabled\n",
520                         port_name(port), pipe_name(pipe));
521
522         I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
523                         "IBX PCH DP %c still using transcoder B\n",
524                         port_name(port));
525 }
526
527 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
528                                      enum pipe pipe, enum port port,
529                                      i915_reg_t hdmi_reg)
530 {
531         enum pipe port_pipe;
532         bool state;
533
534         state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
535
536         I915_STATE_WARN(state && port_pipe == pipe,
537                         "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
538                         port_name(port), pipe_name(pipe));
539
540         I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
541                         "IBX PCH HDMI %c still using transcoder B\n",
542                         port_name(port));
543 }
544
545 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
546                                       enum pipe pipe)
547 {
548         enum pipe port_pipe;
549
550         assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
551         assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
552         assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
553
554         I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
555                         port_pipe == pipe,
556                         "PCH VGA enabled on transcoder %c, should be disabled\n",
557                         pipe_name(pipe));
558
559         I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
560                         port_pipe == pipe,
561                         "PCH LVDS enabled on transcoder %c, should be disabled\n",
562                         pipe_name(pipe));
563
564         /* PCH SDVOB multiplex with HDMIB */
565         assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
566         assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
567         assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
568 }
569
570 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
571                          struct intel_digital_port *dig_port,
572                          unsigned int expected_mask)
573 {
574         u32 port_mask;
575         i915_reg_t dpll_reg;
576
577         switch (dig_port->base.port) {
578         case PORT_B:
579                 port_mask = DPLL_PORTB_READY_MASK;
580                 dpll_reg = DPLL(0);
581                 break;
582         case PORT_C:
583                 port_mask = DPLL_PORTC_READY_MASK;
584                 dpll_reg = DPLL(0);
585                 expected_mask <<= 4;
586                 break;
587         case PORT_D:
588                 port_mask = DPLL_PORTD_READY_MASK;
589                 dpll_reg = DPIO_PHY_STATUS;
590                 break;
591         default:
592                 BUG();
593         }
594
595         if (intel_de_wait_for_register(dev_priv, dpll_reg,
596                                        port_mask, expected_mask, 1000))
597                 drm_WARN(&dev_priv->drm, 1,
598                          "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
599                          dig_port->base.base.base.id, dig_port->base.base.name,
600                          intel_de_read(dev_priv, dpll_reg) & port_mask,
601                          expected_mask);
602 }
603
604 static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
605 {
606         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
607         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
608         enum pipe pipe = crtc->pipe;
609         i915_reg_t reg;
610         u32 val, pipeconf_val;
611
612         /* Make sure PCH DPLL is enabled */
613         assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
614
615         /* FDI must be feeding us bits for PCH ports */
616         assert_fdi_tx_enabled(dev_priv, pipe);
617         assert_fdi_rx_enabled(dev_priv, pipe);
618
619         if (HAS_PCH_CPT(dev_priv)) {
620                 reg = TRANS_CHICKEN2(pipe);
621                 val = intel_de_read(dev_priv, reg);
622                 /*
623                  * Workaround: Set the timing override bit
624                  * before enabling the pch transcoder.
625                  */
626                 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
627                 /* Configure frame start delay to match the CPU */
628                 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
629                 val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
630                 intel_de_write(dev_priv, reg, val);
631         }
632
633         reg = PCH_TRANSCONF(pipe);
634         val = intel_de_read(dev_priv, reg);
635         pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
636
637         if (HAS_PCH_IBX(dev_priv)) {
638                 /* Configure frame start delay to match the CPU */
639                 val &= ~TRANS_FRAME_START_DELAY_MASK;
640                 val |= TRANS_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
641
642                 /*
643                  * Make the BPC in transcoder be consistent with
644                  * that in pipeconf reg. For HDMI we must use 8bpc
645                  * here for both 8bpc and 12bpc.
646                  */
647                 val &= ~PIPECONF_BPC_MASK;
648                 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
649                         val |= PIPECONF_8BPC;
650                 else
651                         val |= pipeconf_val & PIPECONF_BPC_MASK;
652         }
653
654         val &= ~TRANS_INTERLACE_MASK;
655         if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
656                 if (HAS_PCH_IBX(dev_priv) &&
657                     intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
658                         val |= TRANS_LEGACY_INTERLACED_ILK;
659                 else
660                         val |= TRANS_INTERLACED;
661         } else {
662                 val |= TRANS_PROGRESSIVE;
663         }
664
665         intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
666         if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
667                 drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
668                         pipe_name(pipe));
669 }
670
671 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
672                                       enum transcoder cpu_transcoder)
673 {
674         u32 val, pipeconf_val;
675
676         /* FDI must be feeding us bits for PCH ports */
677         assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
678         assert_fdi_rx_enabled(dev_priv, PIPE_A);
679
680         val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
681         /* Workaround: set timing override bit. */
682         val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
683         /* Configure frame start delay to match the CPU */
684         val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
685         val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
686         intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
687
688         val = TRANS_ENABLE;
689         pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
690
691         if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
692             PIPECONF_INTERLACED_ILK)
693                 val |= TRANS_INTERLACED;
694         else
695                 val |= TRANS_PROGRESSIVE;
696
697         intel_de_write(dev_priv, LPT_TRANSCONF, val);
698         if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
699                                   TRANS_STATE_ENABLE, 100))
700                 drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
701 }
702
703 static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
704                                        enum pipe pipe)
705 {
706         i915_reg_t reg;
707         u32 val;
708
709         /* FDI relies on the transcoder */
710         assert_fdi_tx_disabled(dev_priv, pipe);
711         assert_fdi_rx_disabled(dev_priv, pipe);
712
713         /* Ports must be off as well */
714         assert_pch_ports_disabled(dev_priv, pipe);
715
716         reg = PCH_TRANSCONF(pipe);
717         val = intel_de_read(dev_priv, reg);
718         val &= ~TRANS_ENABLE;
719         intel_de_write(dev_priv, reg, val);
720         /* wait for PCH transcoder off, transcoder state */
721         if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
722                 drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
723                         pipe_name(pipe));
724
725         if (HAS_PCH_CPT(dev_priv)) {
726                 /* Workaround: Clear the timing override chicken bit again. */
727                 reg = TRANS_CHICKEN2(pipe);
728                 val = intel_de_read(dev_priv, reg);
729                 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
730                 intel_de_write(dev_priv, reg, val);
731         }
732 }
733
734 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
735 {
736         u32 val;
737
738         val = intel_de_read(dev_priv, LPT_TRANSCONF);
739         val &= ~TRANS_ENABLE;
740         intel_de_write(dev_priv, LPT_TRANSCONF, val);
741         /* wait for PCH transcoder off, transcoder state */
742         if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
743                                     TRANS_STATE_ENABLE, 50))
744                 drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
745
746         /* Workaround: clear timing override bit. */
747         val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
748         val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
749         intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
750 }
751
752 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
753 {
754         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
755
756         if (HAS_PCH_LPT(dev_priv))
757                 return PIPE_A;
758         else
759                 return crtc->pipe;
760 }
761
762 void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
763 {
764         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
765         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
766         enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
767         enum pipe pipe = crtc->pipe;
768         i915_reg_t reg;
769         u32 val;
770
771         drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
772
773         assert_planes_disabled(crtc);
774
775         /*
776          * A pipe without a PLL won't actually be able to drive bits from
777          * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
778          * need the check.
779          */
780         if (HAS_GMCH(dev_priv)) {
781                 if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
782                         assert_dsi_pll_enabled(dev_priv);
783                 else
784                         assert_pll_enabled(dev_priv, pipe);
785         } else {
786                 if (new_crtc_state->has_pch_encoder) {
787                         /* if driving the PCH, we need FDI enabled */
788                         assert_fdi_rx_pll_enabled(dev_priv,
789                                                   intel_crtc_pch_transcoder(crtc));
790                         assert_fdi_tx_pll_enabled(dev_priv,
791                                                   (enum pipe) cpu_transcoder);
792                 }
793                 /* FIXME: assert CPU port conditions for SNB+ */
794         }
795
796         reg = PIPECONF(cpu_transcoder);
797         val = intel_de_read(dev_priv, reg);
798         if (val & PIPECONF_ENABLE) {
799                 /* we keep both pipes enabled on 830 */
800                 drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
801                 return;
802         }
803
804         intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
805         intel_de_posting_read(dev_priv, reg);
806
807         /*
808          * Until the pipe starts PIPEDSL reads will return a stale value,
809          * which causes an apparent vblank timestamp jump when PIPEDSL
810          * resets to its proper value. That also messes up the frame count
811          * when it's derived from the timestamps. So let's wait for the
812          * pipe to start properly before we call drm_crtc_vblank_on()
813          */
814         if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
815                 intel_wait_for_pipe_scanline_moving(crtc);
816 }
817
818 void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
819 {
820         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
821         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
822         enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
823         enum pipe pipe = crtc->pipe;
824         i915_reg_t reg;
825         u32 val;
826
827         drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
828
829         /*
830          * Make sure planes won't keep trying to pump pixels to us,
831          * or we might hang the display.
832          */
833         assert_planes_disabled(crtc);
834
835         reg = PIPECONF(cpu_transcoder);
836         val = intel_de_read(dev_priv, reg);
837         if ((val & PIPECONF_ENABLE) == 0)
838                 return;
839
840         /*
841          * Double wide has implications for planes
842          * so best keep it disabled when not needed.
843          */
844         if (old_crtc_state->double_wide)
845                 val &= ~PIPECONF_DOUBLE_WIDE;
846
847         /* Don't disable pipe or pipe PLLs if needed */
848         if (!IS_I830(dev_priv))
849                 val &= ~PIPECONF_ENABLE;
850
851         intel_de_write(dev_priv, reg, val);
852         if ((val & PIPECONF_ENABLE) == 0)
853                 intel_wait_for_pipe_off(old_crtc_state);
854 }
855
856 static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
857 {
858         return IS_GEN(dev_priv, 2) ? 2048 : 4096;
859 }
860
861 static bool is_aux_plane(const struct drm_framebuffer *fb, int plane)
862 {
863         if (is_ccs_modifier(fb->modifier))
864                 return is_ccs_plane(fb, plane);
865
866         return plane == 1;
867 }
868
869 bool
870 intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
871                                     u64 modifier)
872 {
873         return info->is_yuv &&
874                info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
875 }
876
877 static bool is_semiplanar_uv_plane(const struct drm_framebuffer *fb,
878                                    int color_plane)
879 {
880         return intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier) &&
881                color_plane == 1;
882 }
883
884 unsigned int
885 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
886 {
887         struct drm_i915_private *dev_priv = to_i915(fb->dev);
888         unsigned int cpp = fb->format->cpp[color_plane];
889
890         switch (fb->modifier) {
891         case DRM_FORMAT_MOD_LINEAR:
892                 return intel_tile_size(dev_priv);
893         case I915_FORMAT_MOD_X_TILED:
894                 if (IS_GEN(dev_priv, 2))
895                         return 128;
896                 else
897                         return 512;
898         case I915_FORMAT_MOD_Y_TILED_CCS:
899                 if (is_ccs_plane(fb, color_plane))
900                         return 128;
901                 fallthrough;
902         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
903         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
904         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
905                 if (is_ccs_plane(fb, color_plane))
906                         return 64;
907                 fallthrough;
908         case I915_FORMAT_MOD_Y_TILED:
909                 if (IS_GEN(dev_priv, 2) || HAS_128_BYTE_Y_TILING(dev_priv))
910                         return 128;
911                 else
912                         return 512;
913         case I915_FORMAT_MOD_Yf_TILED_CCS:
914                 if (is_ccs_plane(fb, color_plane))
915                         return 128;
916                 fallthrough;
917         case I915_FORMAT_MOD_Yf_TILED:
918                 switch (cpp) {
919                 case 1:
920                         return 64;
921                 case 2:
922                 case 4:
923                         return 128;
924                 case 8:
925                 case 16:
926                         return 256;
927                 default:
928                         MISSING_CASE(cpp);
929                         return cpp;
930                 }
931                 break;
932         default:
933                 MISSING_CASE(fb->modifier);
934                 return cpp;
935         }
936 }
937
938 unsigned int
939 intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
940 {
941         if (is_gen12_ccs_plane(fb, color_plane))
942                 return 1;
943
944         return intel_tile_size(to_i915(fb->dev)) /
945                 intel_tile_width_bytes(fb, color_plane);
946 }
947
948 /* Return the tile dimensions in pixel units */
949 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
950                             unsigned int *tile_width,
951                             unsigned int *tile_height)
952 {
953         unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
954         unsigned int cpp = fb->format->cpp[color_plane];
955
956         *tile_width = tile_width_bytes / cpp;
957         *tile_height = intel_tile_height(fb, color_plane);
958 }
959
960 static unsigned int intel_tile_row_size(const struct drm_framebuffer *fb,
961                                         int color_plane)
962 {
963         unsigned int tile_width, tile_height;
964
965         intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
966
967         return fb->pitches[color_plane] * tile_height;
968 }
969
970 unsigned int
971 intel_fb_align_height(const struct drm_framebuffer *fb,
972                       int color_plane, unsigned int height)
973 {
974         unsigned int tile_height = intel_tile_height(fb, color_plane);
975
976         return ALIGN(height, tile_height);
977 }
978
979 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
980 {
981         unsigned int size = 0;
982         int i;
983
984         for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
985                 size += rot_info->plane[i].width * rot_info->plane[i].height;
986
987         return size;
988 }
989
990 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
991 {
992         unsigned int size = 0;
993         int i;
994
995         for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
996                 size += rem_info->plane[i].width * rem_info->plane[i].height;
997
998         return size;
999 }
1000
1001 static void
1002 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
1003                         const struct drm_framebuffer *fb,
1004                         unsigned int rotation)
1005 {
1006         view->type = I915_GGTT_VIEW_NORMAL;
1007         if (drm_rotation_90_or_270(rotation)) {
1008                 view->type = I915_GGTT_VIEW_ROTATED;
1009                 view->rotated = to_intel_framebuffer(fb)->rot_info;
1010         }
1011 }
1012
1013 static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
1014 {
1015         if (IS_I830(dev_priv))
1016                 return 16 * 1024;
1017         else if (IS_I85X(dev_priv))
1018                 return 256;
1019         else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
1020                 return 32;
1021         else
1022                 return 4 * 1024;
1023 }
1024
1025 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
1026 {
1027         if (INTEL_GEN(dev_priv) >= 9)
1028                 return 256 * 1024;
1029         else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
1030                  IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
1031                 return 128 * 1024;
1032         else if (INTEL_GEN(dev_priv) >= 4)
1033                 return 4 * 1024;
1034         else
1035                 return 0;
1036 }
1037
1038 static bool has_async_flips(struct drm_i915_private *i915)
1039 {
1040         return INTEL_GEN(i915) >= 5;
1041 }
1042
1043 unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
1044                                   int color_plane)
1045 {
1046         struct drm_i915_private *dev_priv = to_i915(fb->dev);
1047
1048         /* AUX_DIST needs only 4K alignment */
1049         if ((INTEL_GEN(dev_priv) < 12 && is_aux_plane(fb, color_plane)) ||
1050             is_ccs_plane(fb, color_plane))
1051                 return 4096;
1052
1053         switch (fb->modifier) {
1054         case DRM_FORMAT_MOD_LINEAR:
1055                 return intel_linear_alignment(dev_priv);
1056         case I915_FORMAT_MOD_X_TILED:
1057                 if (has_async_flips(dev_priv))
1058                         return 256 * 1024;
1059                 return 0;
1060         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1061                 if (is_semiplanar_uv_plane(fb, color_plane))
1062                         return intel_tile_row_size(fb, color_plane);
1063                 fallthrough;
1064         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1065         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1066                 return 16 * 1024;
1067         case I915_FORMAT_MOD_Y_TILED_CCS:
1068         case I915_FORMAT_MOD_Yf_TILED_CCS:
1069         case I915_FORMAT_MOD_Y_TILED:
1070                 if (INTEL_GEN(dev_priv) >= 12 &&
1071                     is_semiplanar_uv_plane(fb, color_plane))
1072                         return intel_tile_row_size(fb, color_plane);
1073                 fallthrough;
1074         case I915_FORMAT_MOD_Yf_TILED:
1075                 return 1 * 1024 * 1024;
1076         default:
1077                 MISSING_CASE(fb->modifier);
1078                 return 0;
1079         }
1080 }
1081
1082 static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
1083 {
1084         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1085         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1086
1087         return INTEL_GEN(dev_priv) < 4 ||
1088                 (plane->has_fbc &&
1089                  plane_state->view.type == I915_GGTT_VIEW_NORMAL);
1090 }
1091
1092 struct i915_vma *
1093 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
1094                            const struct i915_ggtt_view *view,
1095                            bool uses_fence,
1096                            unsigned long *out_flags)
1097 {
1098         struct drm_device *dev = fb->dev;
1099         struct drm_i915_private *dev_priv = to_i915(dev);
1100         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1101         intel_wakeref_t wakeref;
1102         struct i915_vma *vma;
1103         unsigned int pinctl;
1104         u32 alignment;
1105
1106         if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
1107                 return ERR_PTR(-EINVAL);
1108
1109         alignment = intel_surf_alignment(fb, 0);
1110         if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
1111                 return ERR_PTR(-EINVAL);
1112
1113         /* Note that the w/a also requires 64 PTE of padding following the
1114          * bo. We currently fill all unused PTE with the shadow page and so
1115          * we should always have valid PTE following the scanout preventing
1116          * the VT-d warning.
1117          */
1118         if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
1119                 alignment = 256 * 1024;
1120
1121         /*
1122          * Global gtt pte registers are special registers which actually forward
1123          * writes to a chunk of system memory. Which means that there is no risk
1124          * that the register values disappear as soon as we call
1125          * intel_runtime_pm_put(), so it is correct to wrap only the
1126          * pin/unpin/fence and not more.
1127          */
1128         wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
1129
1130         atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
1131
1132         /*
1133          * Valleyview is definitely limited to scanning out the first
1134          * 512MiB. Lets presume this behaviour was inherited from the
1135          * g4x display engine and that all earlier gen are similarly
1136          * limited. Testing suggests that it is a little more
1137          * complicated than this. For example, Cherryview appears quite
1138          * happy to scanout from anywhere within its global aperture.
1139          */
1140         pinctl = 0;
1141         if (HAS_GMCH(dev_priv))
1142                 pinctl |= PIN_MAPPABLE;
1143
1144         vma = i915_gem_object_pin_to_display_plane(obj,
1145                                                    alignment, view, pinctl);
1146         if (IS_ERR(vma))
1147                 goto err;
1148
1149         if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
1150                 int ret;
1151
1152                 /*
1153                  * Install a fence for tiled scan-out. Pre-i965 always needs a
1154                  * fence, whereas 965+ only requires a fence if using
1155                  * framebuffer compression.  For simplicity, we always, when
1156                  * possible, install a fence as the cost is not that onerous.
1157                  *
1158                  * If we fail to fence the tiled scanout, then either the
1159                  * modeset will reject the change (which is highly unlikely as
1160                  * the affected systems, all but one, do not have unmappable
1161                  * space) or we will not be able to enable full powersaving
1162                  * techniques (also likely not to apply due to various limits
1163                  * FBC and the like impose on the size of the buffer, which
1164                  * presumably we violated anyway with this unmappable buffer).
1165                  * Anyway, it is presumably better to stumble onwards with
1166                  * something and try to run the system in a "less than optimal"
1167                  * mode that matches the user configuration.
1168                  */
1169                 ret = i915_vma_pin_fence(vma);
1170                 if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
1171                         i915_vma_unpin(vma);
1172                         vma = ERR_PTR(ret);
1173                         goto err;
1174                 }
1175
1176                 if (ret == 0 && vma->fence)
1177                         *out_flags |= PLANE_HAS_FENCE;
1178         }
1179
1180         i915_vma_get(vma);
1181 err:
1182         atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
1183         intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
1184         return vma;
1185 }
1186
1187 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
1188 {
1189         if (flags & PLANE_HAS_FENCE)
1190                 i915_vma_unpin_fence(vma);
1191         i915_vma_unpin(vma);
1192         i915_vma_put(vma);
1193 }
1194
1195 static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
1196                           unsigned int rotation)
1197 {
1198         if (drm_rotation_90_or_270(rotation))
1199                 return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
1200         else
1201                 return fb->pitches[color_plane];
1202 }
1203
1204 /*
1205  * Convert the x/y offsets into a linear offset.
1206  * Only valid with 0/180 degree rotation, which is fine since linear
1207  * offset is only used with linear buffers on pre-hsw and tiled buffers
1208  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
1209  */
1210 u32 intel_fb_xy_to_linear(int x, int y,
1211                           const struct intel_plane_state *state,
1212                           int color_plane)
1213 {
1214         const struct drm_framebuffer *fb = state->hw.fb;
1215         unsigned int cpp = fb->format->cpp[color_plane];
1216         unsigned int pitch = state->color_plane[color_plane].stride;
1217
1218         return y * pitch + x * cpp;
1219 }
1220
1221 /*
1222  * Add the x/y offsets derived from fb->offsets[] to the user
1223  * specified plane src x/y offsets. The resulting x/y offsets
1224  * specify the start of scanout from the beginning of the gtt mapping.
1225  */
1226 void intel_add_fb_offsets(int *x, int *y,
1227                           const struct intel_plane_state *state,
1228                           int color_plane)
1229
1230 {
1231         *x += state->color_plane[color_plane].x;
1232         *y += state->color_plane[color_plane].y;
1233 }
1234
1235 static u32 intel_adjust_tile_offset(int *x, int *y,
1236                                     unsigned int tile_width,
1237                                     unsigned int tile_height,
1238                                     unsigned int tile_size,
1239                                     unsigned int pitch_tiles,
1240                                     u32 old_offset,
1241                                     u32 new_offset)
1242 {
1243         unsigned int pitch_pixels = pitch_tiles * tile_width;
1244         unsigned int tiles;
1245
1246         WARN_ON(old_offset & (tile_size - 1));
1247         WARN_ON(new_offset & (tile_size - 1));
1248         WARN_ON(new_offset > old_offset);
1249
1250         tiles = (old_offset - new_offset) / tile_size;
1251
1252         *y += tiles / pitch_tiles * tile_height;
1253         *x += tiles % pitch_tiles * tile_width;
1254
1255         /* minimize x in case it got needlessly big */
1256         *y += *x / pitch_pixels * tile_height;
1257         *x %= pitch_pixels;
1258
1259         return new_offset;
1260 }
1261
1262 static bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
1263 {
1264         return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
1265                is_gen12_ccs_plane(fb, color_plane);
1266 }
1267
1268 static u32 intel_adjust_aligned_offset(int *x, int *y,
1269                                        const struct drm_framebuffer *fb,
1270                                        int color_plane,
1271                                        unsigned int rotation,
1272                                        unsigned int pitch,
1273                                        u32 old_offset, u32 new_offset)
1274 {
1275         struct drm_i915_private *dev_priv = to_i915(fb->dev);
1276         unsigned int cpp = fb->format->cpp[color_plane];
1277
1278         drm_WARN_ON(&dev_priv->drm, new_offset > old_offset);
1279
1280         if (!is_surface_linear(fb, color_plane)) {
1281                 unsigned int tile_size, tile_width, tile_height;
1282                 unsigned int pitch_tiles;
1283
1284                 tile_size = intel_tile_size(dev_priv);
1285                 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
1286
1287                 if (drm_rotation_90_or_270(rotation)) {
1288                         pitch_tiles = pitch / tile_height;
1289                         swap(tile_width, tile_height);
1290                 } else {
1291                         pitch_tiles = pitch / (tile_width * cpp);
1292                 }
1293
1294                 intel_adjust_tile_offset(x, y, tile_width, tile_height,
1295                                          tile_size, pitch_tiles,
1296                                          old_offset, new_offset);
1297         } else {
1298                 old_offset += *y * pitch + *x * cpp;
1299
1300                 *y = (old_offset - new_offset) / pitch;
1301                 *x = ((old_offset - new_offset) - *y * pitch) / cpp;
1302         }
1303
1304         return new_offset;
1305 }
1306
1307 /*
1308  * Adjust the tile offset by moving the difference into
1309  * the x/y offsets.
1310  */
1311 u32 intel_plane_adjust_aligned_offset(int *x, int *y,
1312                                       const struct intel_plane_state *state,
1313                                       int color_plane,
1314                                       u32 old_offset, u32 new_offset)
1315 {
1316         return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
1317                                            state->hw.rotation,
1318                                            state->color_plane[color_plane].stride,
1319                                            old_offset, new_offset);
1320 }
1321
1322 /*
1323  * Computes the aligned offset to the base tile and adjusts
1324  * x, y. bytes per pixel is assumed to be a power-of-two.
1325  *
1326  * In the 90/270 rotated case, x and y are assumed
1327  * to be already rotated to match the rotated GTT view, and
1328  * pitch is the tile_height aligned framebuffer height.
1329  *
1330  * This function is used when computing the derived information
1331  * under intel_framebuffer, so using any of that information
1332  * here is not allowed. Anything under drm_framebuffer can be
1333  * used. This is why the user has to pass in the pitch since it
1334  * is specified in the rotated orientation.
1335  */
1336 static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
1337                                         int *x, int *y,
1338                                         const struct drm_framebuffer *fb,
1339                                         int color_plane,
1340                                         unsigned int pitch,
1341                                         unsigned int rotation,
1342                                         u32 alignment)
1343 {
1344         unsigned int cpp = fb->format->cpp[color_plane];
1345         u32 offset, offset_aligned;
1346
1347         if (!is_surface_linear(fb, color_plane)) {
1348                 unsigned int tile_size, tile_width, tile_height;
1349                 unsigned int tile_rows, tiles, pitch_tiles;
1350
1351                 tile_size = intel_tile_size(dev_priv);
1352                 intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
1353
1354                 if (drm_rotation_90_or_270(rotation)) {
1355                         pitch_tiles = pitch / tile_height;
1356                         swap(tile_width, tile_height);
1357                 } else {
1358                         pitch_tiles = pitch / (tile_width * cpp);
1359                 }
1360
1361                 tile_rows = *y / tile_height;
1362                 *y %= tile_height;
1363
1364                 tiles = *x / tile_width;
1365                 *x %= tile_width;
1366
1367                 offset = (tile_rows * pitch_tiles + tiles) * tile_size;
1368
1369                 offset_aligned = offset;
1370                 if (alignment)
1371                         offset_aligned = rounddown(offset_aligned, alignment);
1372
1373                 intel_adjust_tile_offset(x, y, tile_width, tile_height,
1374                                          tile_size, pitch_tiles,
1375                                          offset, offset_aligned);
1376         } else {
1377                 offset = *y * pitch + *x * cpp;
1378                 offset_aligned = offset;
1379                 if (alignment) {
1380                         offset_aligned = rounddown(offset_aligned, alignment);
1381                         *y = (offset % alignment) / pitch;
1382                         *x = ((offset % alignment) - *y * pitch) / cpp;
1383                 } else {
1384                         *y = *x = 0;
1385                 }
1386         }
1387
1388         return offset_aligned;
1389 }
1390
1391 u32 intel_plane_compute_aligned_offset(int *x, int *y,
1392                                        const struct intel_plane_state *state,
1393                                        int color_plane)
1394 {
1395         struct intel_plane *intel_plane = to_intel_plane(state->uapi.plane);
1396         struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
1397         const struct drm_framebuffer *fb = state->hw.fb;
1398         unsigned int rotation = state->hw.rotation;
1399         int pitch = state->color_plane[color_plane].stride;
1400         u32 alignment;
1401
1402         if (intel_plane->id == PLANE_CURSOR)
1403                 alignment = intel_cursor_alignment(dev_priv);
1404         else
1405                 alignment = intel_surf_alignment(fb, color_plane);
1406
1407         return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
1408                                             pitch, rotation, alignment);
1409 }
1410
1411 /* Convert the fb->offset[] into x/y offsets */
1412 static int intel_fb_offset_to_xy(int *x, int *y,
1413                                  const struct drm_framebuffer *fb,
1414                                  int color_plane)
1415 {
1416         struct drm_i915_private *dev_priv = to_i915(fb->dev);
1417         unsigned int height;
1418         u32 alignment;
1419
1420         if (INTEL_GEN(dev_priv) >= 12 &&
1421             is_semiplanar_uv_plane(fb, color_plane))
1422                 alignment = intel_tile_row_size(fb, color_plane);
1423         else if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
1424                 alignment = intel_tile_size(dev_priv);
1425         else
1426                 alignment = 0;
1427
1428         if (alignment != 0 && fb->offsets[color_plane] % alignment) {
1429                 drm_dbg_kms(&dev_priv->drm,
1430                             "Misaligned offset 0x%08x for color plane %d\n",
1431                             fb->offsets[color_plane], color_plane);
1432                 return -EINVAL;
1433         }
1434
1435         height = drm_framebuffer_plane_height(fb->height, fb, color_plane);
1436         height = ALIGN(height, intel_tile_height(fb, color_plane));
1437
1438         /* Catch potential overflows early */
1439         if (add_overflows_t(u32, mul_u32_u32(height, fb->pitches[color_plane]),
1440                             fb->offsets[color_plane])) {
1441                 drm_dbg_kms(&dev_priv->drm,
1442                             "Bad offset 0x%08x or pitch %d for color plane %d\n",
1443                             fb->offsets[color_plane], fb->pitches[color_plane],
1444                             color_plane);
1445                 return -ERANGE;
1446         }
1447
1448         *x = 0;
1449         *y = 0;
1450
1451         intel_adjust_aligned_offset(x, y,
1452                                     fb, color_plane, DRM_MODE_ROTATE_0,
1453                                     fb->pitches[color_plane],
1454                                     fb->offsets[color_plane], 0);
1455
1456         return 0;
1457 }
1458
1459 static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
1460 {
1461         switch (fb_modifier) {
1462         case I915_FORMAT_MOD_X_TILED:
1463                 return I915_TILING_X;
1464         case I915_FORMAT_MOD_Y_TILED:
1465         case I915_FORMAT_MOD_Y_TILED_CCS:
1466         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1467         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1468         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1469                 return I915_TILING_Y;
1470         default:
1471                 return I915_TILING_NONE;
1472         }
1473 }
1474
1475 /*
1476  * From the Sky Lake PRM:
1477  * "The Color Control Surface (CCS) contains the compression status of
1478  *  the cache-line pairs. The compression state of the cache-line pair
1479  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
1480  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
1481  *  cache-line-pairs. CCS is always Y tiled."
1482  *
1483  * Since cache line pairs refers to horizontally adjacent cache lines,
1484  * each cache line in the CCS corresponds to an area of 32x16 cache
1485  * lines on the main surface. Since each pixel is 4 bytes, this gives
1486  * us a ratio of one byte in the CCS for each 8x16 pixels in the
1487  * main surface.
1488  */
1489 static const struct drm_format_info skl_ccs_formats[] = {
1490         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
1491           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
1492         { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
1493           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
1494         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
1495           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
1496         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
1497           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
1498 };
1499
1500 /*
1501  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
1502  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
1503  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
1504  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
1505  * the main surface.
1506  */
1507 static const struct drm_format_info gen12_ccs_formats[] = {
1508         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
1509           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1510           .hsub = 1, .vsub = 1, },
1511         { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
1512           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1513           .hsub = 1, .vsub = 1, },
1514         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
1515           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1516           .hsub = 1, .vsub = 1, .has_alpha = true },
1517         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
1518           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1519           .hsub = 1, .vsub = 1, .has_alpha = true },
1520         { .format = DRM_FORMAT_YUYV, .num_planes = 2,
1521           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1522           .hsub = 2, .vsub = 1, .is_yuv = true },
1523         { .format = DRM_FORMAT_YVYU, .num_planes = 2,
1524           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1525           .hsub = 2, .vsub = 1, .is_yuv = true },
1526         { .format = DRM_FORMAT_UYVY, .num_planes = 2,
1527           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1528           .hsub = 2, .vsub = 1, .is_yuv = true },
1529         { .format = DRM_FORMAT_VYUY, .num_planes = 2,
1530           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1531           .hsub = 2, .vsub = 1, .is_yuv = true },
1532         { .format = DRM_FORMAT_NV12, .num_planes = 4,
1533           .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
1534           .hsub = 2, .vsub = 2, .is_yuv = true },
1535         { .format = DRM_FORMAT_P010, .num_planes = 4,
1536           .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
1537           .hsub = 2, .vsub = 2, .is_yuv = true },
1538         { .format = DRM_FORMAT_P012, .num_planes = 4,
1539           .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
1540           .hsub = 2, .vsub = 2, .is_yuv = true },
1541         { .format = DRM_FORMAT_P016, .num_planes = 4,
1542           .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
1543           .hsub = 2, .vsub = 2, .is_yuv = true },
1544 };
1545
1546 /*
1547  * Same as gen12_ccs_formats[] above, but with additional surface used
1548  * to pass Clear Color information in plane 2 with 64 bits of data.
1549  */
1550 static const struct drm_format_info gen12_ccs_cc_formats[] = {
1551         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 3,
1552           .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1553           .hsub = 1, .vsub = 1, },
1554         { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 3,
1555           .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1556           .hsub = 1, .vsub = 1, },
1557         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 3,
1558           .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1559           .hsub = 1, .vsub = 1, .has_alpha = true },
1560         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 3,
1561           .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1562           .hsub = 1, .vsub = 1, .has_alpha = true },
1563 };
1564
1565 static const struct drm_format_info *
1566 lookup_format_info(const struct drm_format_info formats[],
1567                    int num_formats, u32 format)
1568 {
1569         int i;
1570
1571         for (i = 0; i < num_formats; i++) {
1572                 if (formats[i].format == format)
1573                         return &formats[i];
1574         }
1575
1576         return NULL;
1577 }
1578
1579 static const struct drm_format_info *
1580 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
1581 {
1582         switch (cmd->modifier[0]) {
1583         case I915_FORMAT_MOD_Y_TILED_CCS:
1584         case I915_FORMAT_MOD_Yf_TILED_CCS:
1585                 return lookup_format_info(skl_ccs_formats,
1586                                           ARRAY_SIZE(skl_ccs_formats),
1587                                           cmd->pixel_format);
1588         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1589         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1590                 return lookup_format_info(gen12_ccs_formats,
1591                                           ARRAY_SIZE(gen12_ccs_formats),
1592                                           cmd->pixel_format);
1593         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1594                 return lookup_format_info(gen12_ccs_cc_formats,
1595                                           ARRAY_SIZE(gen12_ccs_cc_formats),
1596                                           cmd->pixel_format);
1597         default:
1598                 return NULL;
1599         }
1600 }
1601
1602 static int gen12_ccs_aux_stride(struct drm_framebuffer *fb, int ccs_plane)
1603 {
1604         return DIV_ROUND_UP(fb->pitches[skl_ccs_to_main_plane(fb, ccs_plane)],
1605                             512) * 64;
1606 }
1607
1608 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
1609                               u32 pixel_format, u64 modifier)
1610 {
1611         struct intel_crtc *crtc;
1612         struct intel_plane *plane;
1613
1614         /*
1615          * We assume the primary plane for pipe A has
1616          * the highest stride limits of them all,
1617          * if in case pipe A is disabled, use the first pipe from pipe_mask.
1618          */
1619         crtc = intel_get_first_crtc(dev_priv);
1620         if (!crtc)
1621                 return 0;
1622
1623         plane = to_intel_plane(crtc->base.primary);
1624
1625         return plane->max_stride(plane, pixel_format, modifier,
1626                                  DRM_MODE_ROTATE_0);
1627 }
1628
1629 static
1630 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
1631                         u32 pixel_format, u64 modifier)
1632 {
1633         /*
1634          * Arbitrary limit for gen4+ chosen to match the
1635          * render engine max stride.
1636          *
1637          * The new CCS hash mode makes remapping impossible
1638          */
1639         if (!is_ccs_modifier(modifier)) {
1640                 if (INTEL_GEN(dev_priv) >= 7)
1641                         return 256*1024;
1642                 else if (INTEL_GEN(dev_priv) >= 4)
1643                         return 128*1024;
1644         }
1645
1646         return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
1647 }
1648
1649 static u32
1650 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
1651 {
1652         struct drm_i915_private *dev_priv = to_i915(fb->dev);
1653         u32 tile_width;
1654
1655         if (is_surface_linear(fb, color_plane)) {
1656                 u32 max_stride = intel_plane_fb_max_stride(dev_priv,
1657                                                            fb->format->format,
1658                                                            fb->modifier);
1659
1660                 /*
1661                  * To make remapping with linear generally feasible
1662                  * we need the stride to be page aligned.
1663                  */
1664                 if (fb->pitches[color_plane] > max_stride &&
1665                     !is_ccs_modifier(fb->modifier))
1666                         return intel_tile_size(dev_priv);
1667                 else
1668                         return 64;
1669         }
1670
1671         tile_width = intel_tile_width_bytes(fb, color_plane);
1672         if (is_ccs_modifier(fb->modifier)) {
1673                 /*
1674                  * Display WA #0531: skl,bxt,kbl,glk
1675                  *
1676                  * Render decompression and plane width > 3840
1677                  * combined with horizontal panning requires the
1678                  * plane stride to be a multiple of 4. We'll just
1679                  * require the entire fb to accommodate that to avoid
1680                  * potential runtime errors at plane configuration time.
1681                  */
1682                 if (IS_GEN(dev_priv, 9) && color_plane == 0 && fb->width > 3840)
1683                         tile_width *= 4;
1684                 /*
1685                  * The main surface pitch must be padded to a multiple of four
1686                  * tile widths.
1687                  */
1688                 else if (INTEL_GEN(dev_priv) >= 12)
1689                         tile_width *= 4;
1690         }
1691         return tile_width;
1692 }
1693
1694 bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
1695 {
1696         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1697         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1698         const struct drm_framebuffer *fb = plane_state->hw.fb;
1699         int i;
1700
1701         /* We don't want to deal with remapping with cursors */
1702         if (plane->id == PLANE_CURSOR)
1703                 return false;
1704
1705         /*
1706          * The display engine limits already match/exceed the
1707          * render engine limits, so not much point in remapping.
1708          * Would also need to deal with the fence POT alignment
1709          * and gen2 2KiB GTT tile size.
1710          */
1711         if (INTEL_GEN(dev_priv) < 4)
1712                 return false;
1713
1714         /*
1715          * The new CCS hash mode isn't compatible with remapping as
1716          * the virtual address of the pages affects the compressed data.
1717          */
1718         if (is_ccs_modifier(fb->modifier))
1719                 return false;
1720
1721         /* Linear needs a page aligned stride for remapping */
1722         if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
1723                 unsigned int alignment = intel_tile_size(dev_priv) - 1;
1724
1725                 for (i = 0; i < fb->format->num_planes; i++) {
1726                         if (fb->pitches[i] & alignment)
1727                                 return false;
1728                 }
1729         }
1730
1731         return true;
1732 }
1733
1734 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
1735 {
1736         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1737         const struct drm_framebuffer *fb = plane_state->hw.fb;
1738         unsigned int rotation = plane_state->hw.rotation;
1739         u32 stride, max_stride;
1740
1741         /*
1742          * No remapping for invisible planes since we don't have
1743          * an actual source viewport to remap.
1744          */
1745         if (!plane_state->uapi.visible)
1746                 return false;
1747
1748         if (!intel_plane_can_remap(plane_state))
1749                 return false;
1750
1751         /*
1752          * FIXME: aux plane limits on gen9+ are
1753          * unclear in Bspec, for now no checking.
1754          */
1755         stride = intel_fb_pitch(fb, 0, rotation);
1756         max_stride = plane->max_stride(plane, fb->format->format,
1757                                        fb->modifier, rotation);
1758
1759         return stride > max_stride;
1760 }
1761
1762 void
1763 intel_fb_plane_get_subsampling(int *hsub, int *vsub,
1764                                const struct drm_framebuffer *fb,
1765                                int color_plane)
1766 {
1767         int main_plane;
1768
1769         if (color_plane == 0) {
1770                 *hsub = 1;
1771                 *vsub = 1;
1772
1773                 return;
1774         }
1775
1776         /*
1777          * TODO: Deduct the subsampling from the char block for all CCS
1778          * formats and planes.
1779          */
1780         if (!is_gen12_ccs_plane(fb, color_plane)) {
1781                 *hsub = fb->format->hsub;
1782                 *vsub = fb->format->vsub;
1783
1784                 return;
1785         }
1786
1787         main_plane = skl_ccs_to_main_plane(fb, color_plane);
1788         *hsub = drm_format_info_block_width(fb->format, color_plane) /
1789                 drm_format_info_block_width(fb->format, main_plane);
1790
1791         /*
1792          * The min stride check in the core framebuffer_check() function
1793          * assumes that format->hsub applies to every plane except for the
1794          * first plane. That's incorrect for the CCS AUX plane of the first
1795          * plane, but for the above check to pass we must define the block
1796          * width with that subsampling applied to it. Adjust the width here
1797          * accordingly, so we can calculate the actual subsampling factor.
1798          */
1799         if (main_plane == 0)
1800                 *hsub *= fb->format->hsub;
1801
1802         *vsub = 32;
1803 }
1804 static int
1805 intel_fb_check_ccs_xy(struct drm_framebuffer *fb, int ccs_plane, int x, int y)
1806 {
1807         struct drm_i915_private *i915 = to_i915(fb->dev);
1808         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1809         int main_plane;
1810         int hsub, vsub;
1811         int tile_width, tile_height;
1812         int ccs_x, ccs_y;
1813         int main_x, main_y;
1814
1815         if (!is_ccs_plane(fb, ccs_plane) || is_gen12_ccs_cc_plane(fb, ccs_plane))
1816                 return 0;
1817
1818         intel_tile_dims(fb, ccs_plane, &tile_width, &tile_height);
1819         intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
1820
1821         tile_width *= hsub;
1822         tile_height *= vsub;
1823
1824         ccs_x = (x * hsub) % tile_width;
1825         ccs_y = (y * vsub) % tile_height;
1826
1827         main_plane = skl_ccs_to_main_plane(fb, ccs_plane);
1828         main_x = intel_fb->normal[main_plane].x % tile_width;
1829         main_y = intel_fb->normal[main_plane].y % tile_height;
1830
1831         /*
1832          * CCS doesn't have its own x/y offset register, so the intra CCS tile
1833          * x/y offsets must match between CCS and the main surface.
1834          */
1835         if (main_x != ccs_x || main_y != ccs_y) {
1836                 drm_dbg_kms(&i915->drm,
1837                               "Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
1838                               main_x, main_y,
1839                               ccs_x, ccs_y,
1840                               intel_fb->normal[main_plane].x,
1841                               intel_fb->normal[main_plane].y,
1842                               x, y);
1843                 return -EINVAL;
1844         }
1845
1846         return 0;
1847 }
1848
1849 static void
1850 intel_fb_plane_dims(int *w, int *h, struct drm_framebuffer *fb, int color_plane)
1851 {
1852         int main_plane = is_ccs_plane(fb, color_plane) ?
1853                          skl_ccs_to_main_plane(fb, color_plane) : 0;
1854         int main_hsub, main_vsub;
1855         int hsub, vsub;
1856
1857         intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb, main_plane);
1858         intel_fb_plane_get_subsampling(&hsub, &vsub, fb, color_plane);
1859         *w = fb->width / main_hsub / hsub;
1860         *h = fb->height / main_vsub / vsub;
1861 }
1862
1863 /*
1864  * Setup the rotated view for an FB plane and return the size the GTT mapping
1865  * requires for this view.
1866  */
1867 static u32
1868 setup_fb_rotation(int plane, const struct intel_remapped_plane_info *plane_info,
1869                   u32 gtt_offset_rotated, int x, int y,
1870                   unsigned int width, unsigned int height,
1871                   unsigned int tile_size,
1872                   unsigned int tile_width, unsigned int tile_height,
1873                   struct drm_framebuffer *fb)
1874 {
1875         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1876         struct intel_rotation_info *rot_info = &intel_fb->rot_info;
1877         unsigned int pitch_tiles;
1878         struct drm_rect r;
1879
1880         /* Y or Yf modifiers required for 90/270 rotation */
1881         if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
1882             fb->modifier != I915_FORMAT_MOD_Yf_TILED)
1883                 return 0;
1884
1885         if (drm_WARN_ON(fb->dev, plane >= ARRAY_SIZE(rot_info->plane)))
1886                 return 0;
1887
1888         rot_info->plane[plane] = *plane_info;
1889
1890         intel_fb->rotated[plane].pitch = plane_info->height * tile_height;
1891
1892         /* rotate the x/y offsets to match the GTT view */
1893         drm_rect_init(&r, x, y, width, height);
1894         drm_rect_rotate(&r,
1895                         plane_info->width * tile_width,
1896                         plane_info->height * tile_height,
1897                         DRM_MODE_ROTATE_270);
1898         x = r.x1;
1899         y = r.y1;
1900
1901         /* rotate the tile dimensions to match the GTT view */
1902         pitch_tiles = intel_fb->rotated[plane].pitch / tile_height;
1903         swap(tile_width, tile_height);
1904
1905         /*
1906          * We only keep the x/y offsets, so push all of the
1907          * gtt offset into the x/y offsets.
1908          */
1909         intel_adjust_tile_offset(&x, &y,
1910                                  tile_width, tile_height,
1911                                  tile_size, pitch_tiles,
1912                                  gtt_offset_rotated * tile_size, 0);
1913
1914         /*
1915          * First pixel of the framebuffer from
1916          * the start of the rotated gtt mapping.
1917          */
1918         intel_fb->rotated[plane].x = x;
1919         intel_fb->rotated[plane].y = y;
1920
1921         return plane_info->width * plane_info->height;
1922 }
1923
1924 static int
1925 intel_fill_fb_info(struct drm_i915_private *dev_priv,
1926                    struct drm_framebuffer *fb)
1927 {
1928         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1929         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1930         u32 gtt_offset_rotated = 0;
1931         unsigned int max_size = 0;
1932         int i, num_planes = fb->format->num_planes;
1933         unsigned int tile_size = intel_tile_size(dev_priv);
1934
1935         for (i = 0; i < num_planes; i++) {
1936                 unsigned int width, height;
1937                 unsigned int cpp, size;
1938                 u32 offset;
1939                 int x, y;
1940                 int ret;
1941
1942                 /*
1943                  * Plane 2 of Render Compression with Clear Color fb modifier
1944                  * is consumed by the driver and not passed to DE. Skip the
1945                  * arithmetic related to alignment and offset calculation.
1946                  */
1947                 if (is_gen12_ccs_cc_plane(fb, i)) {
1948                         if (IS_ALIGNED(fb->offsets[i], PAGE_SIZE))
1949                                 continue;
1950                         else
1951                                 return -EINVAL;
1952                 }
1953
1954                 cpp = fb->format->cpp[i];
1955                 intel_fb_plane_dims(&width, &height, fb, i);
1956
1957                 ret = intel_fb_offset_to_xy(&x, &y, fb, i);
1958                 if (ret) {
1959                         drm_dbg_kms(&dev_priv->drm,
1960                                     "bad fb plane %d offset: 0x%x\n",
1961                                     i, fb->offsets[i]);
1962                         return ret;
1963                 }
1964
1965                 ret = intel_fb_check_ccs_xy(fb, i, x, y);
1966                 if (ret)
1967                         return ret;
1968
1969                 /*
1970                  * The fence (if used) is aligned to the start of the object
1971                  * so having the framebuffer wrap around across the edge of the
1972                  * fenced region doesn't really work. We have no API to configure
1973                  * the fence start offset within the object (nor could we probably
1974                  * on gen2/3). So it's just easier if we just require that the
1975                  * fb layout agrees with the fence layout. We already check that the
1976                  * fb stride matches the fence stride elsewhere.
1977                  */
1978                 if (i == 0 && i915_gem_object_is_tiled(obj) &&
1979                     (x + width) * cpp > fb->pitches[i]) {
1980                         drm_dbg_kms(&dev_priv->drm,
1981                                     "bad fb plane %d offset: 0x%x\n",
1982                                      i, fb->offsets[i]);
1983                         return -EINVAL;
1984                 }
1985
1986                 /*
1987                  * First pixel of the framebuffer from
1988                  * the start of the normal gtt mapping.
1989                  */
1990                 intel_fb->normal[i].x = x;
1991                 intel_fb->normal[i].y = y;
1992
1993                 offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
1994                                                       fb->pitches[i],
1995                                                       DRM_MODE_ROTATE_0,
1996                                                       tile_size);
1997                 offset /= tile_size;
1998
1999                 if (!is_surface_linear(fb, i)) {
2000                         struct intel_remapped_plane_info plane_info;
2001                         unsigned int tile_width, tile_height;
2002
2003                         intel_tile_dims(fb, i, &tile_width, &tile_height);
2004
2005                         plane_info.offset = offset;
2006                         plane_info.stride = DIV_ROUND_UP(fb->pitches[i],
2007                                                          tile_width * cpp);
2008                         plane_info.width = DIV_ROUND_UP(x + width, tile_width);
2009                         plane_info.height = DIV_ROUND_UP(y + height,
2010                                                          tile_height);
2011
2012                         /* how many tiles does this plane need */
2013                         size = plane_info.stride * plane_info.height;
2014                         /*
2015                          * If the plane isn't horizontally tile aligned,
2016                          * we need one more tile.
2017                          */
2018                         if (x != 0)
2019                                 size++;
2020
2021                         gtt_offset_rotated +=
2022                                 setup_fb_rotation(i, &plane_info,
2023                                                   gtt_offset_rotated,
2024                                                   x, y, width, height,
2025                                                   tile_size,
2026                                                   tile_width, tile_height,
2027                                                   fb);
2028                 } else {
2029                         size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2030                                             x * cpp, tile_size);
2031                 }
2032
2033                 /* how many tiles in total needed in the bo */
2034                 max_size = max(max_size, offset + size);
2035         }
2036
2037         if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
2038                 drm_dbg_kms(&dev_priv->drm,
2039                             "fb too big for bo (need %llu bytes, have %zu bytes)\n",
2040                             mul_u32_u32(max_size, tile_size), obj->base.size);
2041                 return -EINVAL;
2042         }
2043
2044         return 0;
2045 }
2046
2047 static void
2048 intel_plane_remap_gtt(struct intel_plane_state *plane_state)
2049 {
2050         struct drm_i915_private *dev_priv =
2051                 to_i915(plane_state->uapi.plane->dev);
2052         struct drm_framebuffer *fb = plane_state->hw.fb;
2053         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2054         struct intel_rotation_info *info = &plane_state->view.rotated;
2055         unsigned int rotation = plane_state->hw.rotation;
2056         int i, num_planes = fb->format->num_planes;
2057         unsigned int tile_size = intel_tile_size(dev_priv);
2058         unsigned int src_x, src_y;
2059         unsigned int src_w, src_h;
2060         u32 gtt_offset = 0;
2061
2062         memset(&plane_state->view, 0, sizeof(plane_state->view));
2063         plane_state->view.type = drm_rotation_90_or_270(rotation) ?
2064                 I915_GGTT_VIEW_ROTATED : I915_GGTT_VIEW_REMAPPED;
2065
2066         src_x = plane_state->uapi.src.x1 >> 16;
2067         src_y = plane_state->uapi.src.y1 >> 16;
2068         src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
2069         src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
2070
2071         drm_WARN_ON(&dev_priv->drm, is_ccs_modifier(fb->modifier));
2072
2073         /* Make src coordinates relative to the viewport */
2074         drm_rect_translate(&plane_state->uapi.src,
2075                            -(src_x << 16), -(src_y << 16));
2076
2077         /* Rotate src coordinates to match rotated GTT view */
2078         if (drm_rotation_90_or_270(rotation))
2079                 drm_rect_rotate(&plane_state->uapi.src,
2080                                 src_w << 16, src_h << 16,
2081                                 DRM_MODE_ROTATE_270);
2082
2083         for (i = 0; i < num_planes; i++) {
2084                 unsigned int hsub = i ? fb->format->hsub : 1;
2085                 unsigned int vsub = i ? fb->format->vsub : 1;
2086                 unsigned int cpp = fb->format->cpp[i];
2087                 unsigned int tile_width, tile_height;
2088                 unsigned int width, height;
2089                 unsigned int pitch_tiles;
2090                 unsigned int x, y;
2091                 u32 offset;
2092
2093                 intel_tile_dims(fb, i, &tile_width, &tile_height);
2094
2095                 x = src_x / hsub;
2096                 y = src_y / vsub;
2097                 width = src_w / hsub;
2098                 height = src_h / vsub;
2099
2100                 /*
2101                  * First pixel of the src viewport from the
2102                  * start of the normal gtt mapping.
2103                  */
2104                 x += intel_fb->normal[i].x;
2105                 y += intel_fb->normal[i].y;
2106
2107                 offset = intel_compute_aligned_offset(dev_priv, &x, &y,
2108                                                       fb, i, fb->pitches[i],
2109                                                       DRM_MODE_ROTATE_0, tile_size);
2110                 offset /= tile_size;
2111
2112                 drm_WARN_ON(&dev_priv->drm, i >= ARRAY_SIZE(info->plane));
2113                 info->plane[i].offset = offset;
2114                 info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i],
2115                                                      tile_width * cpp);
2116                 info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2117                 info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2118
2119                 if (drm_rotation_90_or_270(rotation)) {
2120                         struct drm_rect r;
2121
2122                         /* rotate the x/y offsets to match the GTT view */
2123                         drm_rect_init(&r, x, y, width, height);
2124                         drm_rect_rotate(&r,
2125                                         info->plane[i].width * tile_width,
2126                                         info->plane[i].height * tile_height,
2127                                         DRM_MODE_ROTATE_270);
2128                         x = r.x1;
2129                         y = r.y1;
2130
2131                         pitch_tiles = info->plane[i].height;
2132                         plane_state->color_plane[i].stride = pitch_tiles * tile_height;
2133
2134                         /* rotate the tile dimensions to match the GTT view */
2135                         swap(tile_width, tile_height);
2136                 } else {
2137                         pitch_tiles = info->plane[i].width;
2138                         plane_state->color_plane[i].stride = pitch_tiles * tile_width * cpp;
2139                 }
2140
2141                 /*
2142                  * We only keep the x/y offsets, so push all of the
2143                  * gtt offset into the x/y offsets.
2144                  */
2145                 intel_adjust_tile_offset(&x, &y,
2146                                          tile_width, tile_height,
2147                                          tile_size, pitch_tiles,
2148                                          gtt_offset * tile_size, 0);
2149
2150                 gtt_offset += info->plane[i].width * info->plane[i].height;
2151
2152                 plane_state->color_plane[i].offset = 0;
2153                 plane_state->color_plane[i].x = x;
2154                 plane_state->color_plane[i].y = y;
2155         }
2156 }
2157
2158 int
2159 intel_plane_compute_gtt(struct intel_plane_state *plane_state)
2160 {
2161         const struct intel_framebuffer *fb =
2162                 to_intel_framebuffer(plane_state->hw.fb);
2163         unsigned int rotation = plane_state->hw.rotation;
2164         int i, num_planes;
2165
2166         if (!fb)
2167                 return 0;
2168
2169         num_planes = fb->base.format->num_planes;
2170
2171         if (intel_plane_needs_remap(plane_state)) {
2172                 intel_plane_remap_gtt(plane_state);
2173
2174                 /*
2175                  * Sometimes even remapping can't overcome
2176                  * the stride limitations :( Can happen with
2177                  * big plane sizes and suitably misaligned
2178                  * offsets.
2179                  */
2180                 return intel_plane_check_stride(plane_state);
2181         }
2182
2183         intel_fill_fb_ggtt_view(&plane_state->view, &fb->base, rotation);
2184
2185         for (i = 0; i < num_planes; i++) {
2186                 plane_state->color_plane[i].stride = intel_fb_pitch(&fb->base, i, rotation);
2187                 plane_state->color_plane[i].offset = 0;
2188
2189                 if (drm_rotation_90_or_270(rotation)) {
2190                         plane_state->color_plane[i].x = fb->rotated[i].x;
2191                         plane_state->color_plane[i].y = fb->rotated[i].y;
2192                 } else {
2193                         plane_state->color_plane[i].x = fb->normal[i].x;
2194                         plane_state->color_plane[i].y = fb->normal[i].y;
2195                 }
2196         }
2197
2198         /* Rotate src coordinates to match rotated GTT view */
2199         if (drm_rotation_90_or_270(rotation))
2200                 drm_rect_rotate(&plane_state->uapi.src,
2201                                 fb->base.width << 16, fb->base.height << 16,
2202                                 DRM_MODE_ROTATE_270);
2203
2204         return intel_plane_check_stride(plane_state);
2205 }
2206
2207 static struct i915_vma *
2208 initial_plane_vma(struct drm_i915_private *i915,
2209                   struct intel_initial_plane_config *plane_config)
2210 {
2211         struct drm_i915_gem_object *obj;
2212         struct i915_vma *vma;
2213         u32 base, size;
2214
2215         if (plane_config->size == 0)
2216                 return NULL;
2217
2218         base = round_down(plane_config->base,
2219                           I915_GTT_MIN_ALIGNMENT);
2220         size = round_up(plane_config->base + plane_config->size,
2221                         I915_GTT_MIN_ALIGNMENT);
2222         size -= base;
2223
2224         /*
2225          * If the FB is too big, just don't use it since fbdev is not very
2226          * important and we should probably use that space with FBC or other
2227          * features.
2228          */
2229         if (size * 2 > i915->stolen_usable_size)
2230                 return NULL;
2231
2232         obj = i915_gem_object_create_stolen_for_preallocated(i915, base, size);
2233         if (IS_ERR(obj))
2234                 return NULL;
2235
2236         /*
2237          * Mark it WT ahead of time to avoid changing the
2238          * cache_level during fbdev initialization. The
2239          * unbind there would get stuck waiting for rcu.
2240          */
2241         i915_gem_object_set_cache_coherency(obj, HAS_WT(i915) ?
2242                                             I915_CACHE_WT : I915_CACHE_NONE);
2243
2244         switch (plane_config->tiling) {
2245         case I915_TILING_NONE:
2246                 break;
2247         case I915_TILING_X:
2248         case I915_TILING_Y:
2249                 obj->tiling_and_stride =
2250                         plane_config->fb->base.pitches[0] |
2251                         plane_config->tiling;
2252                 break;
2253         default:
2254                 MISSING_CASE(plane_config->tiling);
2255                 goto err_obj;
2256         }
2257
2258         vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
2259         if (IS_ERR(vma))
2260                 goto err_obj;
2261
2262         if (i915_ggtt_pin(vma, NULL, 0, PIN_MAPPABLE | PIN_OFFSET_FIXED | base))
2263                 goto err_obj;
2264
2265         if (i915_gem_object_is_tiled(obj) &&
2266             !i915_vma_is_map_and_fenceable(vma))
2267                 goto err_obj;
2268
2269         return vma;
2270
2271 err_obj:
2272         i915_gem_object_put(obj);
2273         return NULL;
2274 }
2275
2276 static bool
2277 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2278                               struct intel_initial_plane_config *plane_config)
2279 {
2280         struct drm_device *dev = crtc->base.dev;
2281         struct drm_i915_private *dev_priv = to_i915(dev);
2282         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2283         struct drm_framebuffer *fb = &plane_config->fb->base;
2284         struct i915_vma *vma;
2285
2286         switch (fb->modifier) {
2287         case DRM_FORMAT_MOD_LINEAR:
2288         case I915_FORMAT_MOD_X_TILED:
2289         case I915_FORMAT_MOD_Y_TILED:
2290                 break;
2291         default:
2292                 drm_dbg(&dev_priv->drm,
2293                         "Unsupported modifier for initial FB: 0x%llx\n",
2294                         fb->modifier);
2295                 return false;
2296         }
2297
2298         vma = initial_plane_vma(dev_priv, plane_config);
2299         if (!vma)
2300                 return false;
2301
2302         mode_cmd.pixel_format = fb->format->format;
2303         mode_cmd.width = fb->width;
2304         mode_cmd.height = fb->height;
2305         mode_cmd.pitches[0] = fb->pitches[0];
2306         mode_cmd.modifier[0] = fb->modifier;
2307         mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
2308
2309         if (intel_framebuffer_init(to_intel_framebuffer(fb),
2310                                    vma->obj, &mode_cmd)) {
2311                 drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
2312                 goto err_vma;
2313         }
2314
2315         plane_config->vma = vma;
2316         return true;
2317
2318 err_vma:
2319         i915_vma_put(vma);
2320         return false;
2321 }
2322
2323 static void
2324 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2325                         struct intel_plane_state *plane_state,
2326                         bool visible)
2327 {
2328         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2329
2330         plane_state->uapi.visible = visible;
2331
2332         if (visible)
2333                 crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
2334         else
2335                 crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
2336 }
2337
2338 static void fixup_plane_bitmasks(struct intel_crtc_state *crtc_state)
2339 {
2340         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
2341         struct drm_plane *plane;
2342
2343         /*
2344          * Active_planes aliases if multiple "primary" or cursor planes
2345          * have been used on the same (or wrong) pipe. plane_mask uses
2346          * unique ids, hence we can use that to reconstruct active_planes.
2347          */
2348         crtc_state->enabled_planes = 0;
2349         crtc_state->active_planes = 0;
2350
2351         drm_for_each_plane_mask(plane, &dev_priv->drm,
2352                                 crtc_state->uapi.plane_mask) {
2353                 crtc_state->enabled_planes |= BIT(to_intel_plane(plane)->id);
2354                 crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
2355         }
2356 }
2357
2358 static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
2359                                          struct intel_plane *plane)
2360 {
2361         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2362         struct intel_crtc_state *crtc_state =
2363                 to_intel_crtc_state(crtc->base.state);
2364         struct intel_plane_state *plane_state =
2365                 to_intel_plane_state(plane->base.state);
2366
2367         drm_dbg_kms(&dev_priv->drm,
2368                     "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
2369                     plane->base.base.id, plane->base.name,
2370                     crtc->base.base.id, crtc->base.name);
2371
2372         intel_set_plane_visible(crtc_state, plane_state, false);
2373         fixup_plane_bitmasks(crtc_state);
2374         crtc_state->data_rate[plane->id] = 0;
2375         crtc_state->min_cdclk[plane->id] = 0;
2376
2377         if (plane->id == PLANE_PRIMARY)
2378                 hsw_disable_ips(crtc_state);
2379
2380         /*
2381          * Vblank time updates from the shadow to live plane control register
2382          * are blocked if the memory self-refresh mode is active at that
2383          * moment. So to make sure the plane gets truly disabled, disable
2384          * first the self-refresh mode. The self-refresh enable bit in turn
2385          * will be checked/applied by the HW only at the next frame start
2386          * event which is after the vblank start event, so we need to have a
2387          * wait-for-vblank between disabling the plane and the pipe.
2388          */
2389         if (HAS_GMCH(dev_priv) &&
2390             intel_set_memory_cxsr(dev_priv, false))
2391                 intel_wait_for_vblank(dev_priv, crtc->pipe);
2392
2393         /*
2394          * Gen2 reports pipe underruns whenever all planes are disabled.
2395          * So disable underrun reporting before all the planes get disabled.
2396          */
2397         if (IS_GEN(dev_priv, 2) && !crtc_state->active_planes)
2398                 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
2399
2400         intel_disable_plane(plane, crtc_state);
2401         intel_wait_for_vblank(dev_priv, crtc->pipe);
2402 }
2403
2404 static void
2405 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2406                              struct intel_initial_plane_config *plane_config)
2407 {
2408         struct drm_device *dev = intel_crtc->base.dev;
2409         struct drm_i915_private *dev_priv = to_i915(dev);
2410         struct drm_crtc *c;
2411         struct drm_plane *primary = intel_crtc->base.primary;
2412         struct drm_plane_state *plane_state = primary->state;
2413         struct intel_plane *intel_plane = to_intel_plane(primary);
2414         struct intel_plane_state *intel_state =
2415                 to_intel_plane_state(plane_state);
2416         struct intel_crtc_state *crtc_state =
2417                 to_intel_crtc_state(intel_crtc->base.state);
2418         struct drm_framebuffer *fb;
2419         struct i915_vma *vma;
2420
2421         if (!plane_config->fb)
2422                 return;
2423
2424         if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
2425                 fb = &plane_config->fb->base;
2426                 vma = plane_config->vma;
2427                 goto valid_fb;
2428         }
2429
2430         /*
2431          * Failed to alloc the obj, check to see if we should share
2432          * an fb with another CRTC instead
2433          */
2434         for_each_crtc(dev, c) {
2435                 struct intel_plane_state *state;
2436
2437                 if (c == &intel_crtc->base)
2438                         continue;
2439
2440                 if (!to_intel_crtc_state(c->state)->uapi.active)
2441                         continue;
2442
2443                 state = to_intel_plane_state(c->primary->state);
2444                 if (!state->vma)
2445                         continue;
2446
2447                 if (intel_plane_ggtt_offset(state) == plane_config->base) {
2448                         fb = state->hw.fb;
2449                         vma = state->vma;
2450                         goto valid_fb;
2451                 }
2452         }
2453
2454         /*
2455          * We've failed to reconstruct the BIOS FB.  Current display state
2456          * indicates that the primary plane is visible, but has a NULL FB,
2457          * which will lead to problems later if we don't fix it up.  The
2458          * simplest solution is to just disable the primary plane now and
2459          * pretend the BIOS never had it enabled.
2460          */
2461         intel_plane_disable_noatomic(intel_crtc, intel_plane);
2462         if (crtc_state->bigjoiner) {
2463                 struct intel_crtc *slave =
2464                         crtc_state->bigjoiner_linked_crtc;
2465                 intel_plane_disable_noatomic(slave, to_intel_plane(slave->base.primary));
2466         }
2467
2468         return;
2469
2470 valid_fb:
2471         intel_state->hw.rotation = plane_config->rotation;
2472         intel_fill_fb_ggtt_view(&intel_state->view, fb,
2473                                 intel_state->hw.rotation);
2474         intel_state->color_plane[0].stride =
2475                 intel_fb_pitch(fb, 0, intel_state->hw.rotation);
2476
2477         __i915_vma_pin(vma);
2478         intel_state->vma = i915_vma_get(vma);
2479         if (intel_plane_uses_fence(intel_state) && i915_vma_pin_fence(vma) == 0)
2480                 if (vma->fence)
2481                         intel_state->flags |= PLANE_HAS_FENCE;
2482
2483         plane_state->src_x = 0;
2484         plane_state->src_y = 0;
2485         plane_state->src_w = fb->width << 16;
2486         plane_state->src_h = fb->height << 16;
2487
2488         plane_state->crtc_x = 0;
2489         plane_state->crtc_y = 0;
2490         plane_state->crtc_w = fb->width;
2491         plane_state->crtc_h = fb->height;
2492
2493         intel_state->uapi.src = drm_plane_state_src(plane_state);
2494         intel_state->uapi.dst = drm_plane_state_dest(plane_state);
2495
2496         if (plane_config->tiling)
2497                 dev_priv->preserve_bios_swizzle = true;
2498
2499         plane_state->fb = fb;
2500         drm_framebuffer_get(fb);
2501
2502         plane_state->crtc = &intel_crtc->base;
2503         intel_plane_copy_uapi_to_hw_state(intel_state, intel_state,
2504                                           intel_crtc);
2505
2506         intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
2507
2508         atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2509                   &to_intel_frontbuffer(fb)->bits);
2510 }
2511
2512 unsigned int
2513 intel_plane_fence_y_offset(const struct intel_plane_state *plane_state)
2514 {
2515         int x = 0, y = 0;
2516
2517         intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
2518                                           plane_state->color_plane[0].offset, 0);
2519
2520         return y;
2521 }
2522
2523 static int
2524 __intel_display_resume(struct drm_device *dev,
2525                        struct drm_atomic_state *state,
2526                        struct drm_modeset_acquire_ctx *ctx)
2527 {
2528         struct drm_crtc_state *crtc_state;
2529         struct drm_crtc *crtc;
2530         int i, ret;
2531
2532         intel_modeset_setup_hw_state(dev, ctx);
2533         intel_vga_redisable(to_i915(dev));
2534
2535         if (!state)
2536                 return 0;
2537
2538         /*
2539          * We've duplicated the state, pointers to the old state are invalid.
2540          *
2541          * Don't attempt to use the old state until we commit the duplicated state.
2542          */
2543         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2544                 /*
2545                  * Force recalculation even if we restore
2546                  * current state. With fast modeset this may not result
2547                  * in a modeset when the state is compatible.
2548                  */
2549                 crtc_state->mode_changed = true;
2550         }
2551
2552         /* ignore any reset values/BIOS leftovers in the WM registers */
2553         if (!HAS_GMCH(to_i915(dev)))
2554                 to_intel_atomic_state(state)->skip_intermediate_wm = true;
2555
2556         ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
2557
2558         drm_WARN_ON(dev, ret == -EDEADLK);
2559         return ret;
2560 }
2561
2562 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
2563 {
2564         return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
2565                 intel_has_gpu_reset(&dev_priv->gt));
2566 }
2567
2568 void intel_display_prepare_reset(struct drm_i915_private *dev_priv)
2569 {
2570         struct drm_device *dev = &dev_priv->drm;
2571         struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
2572         struct drm_atomic_state *state;
2573         int ret;
2574
2575         if (!HAS_DISPLAY(dev_priv))
2576                 return;
2577
2578         /* reset doesn't touch the display */
2579         if (!dev_priv->params.force_reset_modeset_test &&
2580             !gpu_reset_clobbers_display(dev_priv))
2581                 return;
2582
2583         /* We have a modeset vs reset deadlock, defensively unbreak it. */
2584         set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
2585         smp_mb__after_atomic();
2586         wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
2587
2588         if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
2589                 drm_dbg_kms(&dev_priv->drm,
2590                             "Modeset potentially stuck, unbreaking through wedging\n");
2591                 intel_gt_set_wedged(&dev_priv->gt);
2592         }
2593
2594         /*
2595          * Need mode_config.mutex so that we don't
2596          * trample ongoing ->detect() and whatnot.
2597          */
2598         mutex_lock(&dev->mode_config.mutex);
2599         drm_modeset_acquire_init(ctx, 0);
2600         while (1) {
2601                 ret = drm_modeset_lock_all_ctx(dev, ctx);
2602                 if (ret != -EDEADLK)
2603                         break;
2604
2605                 drm_modeset_backoff(ctx);
2606         }
2607         /*
2608          * Disabling the crtcs gracefully seems nicer. Also the
2609          * g33 docs say we should at least disable all the planes.
2610          */
2611         state = drm_atomic_helper_duplicate_state(dev, ctx);
2612         if (IS_ERR(state)) {
2613                 ret = PTR_ERR(state);
2614                 drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
2615                         ret);
2616                 return;
2617         }
2618
2619         ret = drm_atomic_helper_disable_all(dev, ctx);
2620         if (ret) {
2621                 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
2622                         ret);
2623                 drm_atomic_state_put(state);
2624                 return;
2625         }
2626
2627         dev_priv->modeset_restore_state = state;
2628         state->acquire_ctx = ctx;
2629 }
2630
2631 void intel_display_finish_reset(struct drm_i915_private *dev_priv)
2632 {
2633         struct drm_device *dev = &dev_priv->drm;
2634         struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
2635         struct drm_atomic_state *state;
2636         int ret;
2637
2638         if (!HAS_DISPLAY(dev_priv))
2639                 return;
2640
2641         /* reset doesn't touch the display */
2642         if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
2643                 return;
2644
2645         state = fetch_and_zero(&dev_priv->modeset_restore_state);
2646         if (!state)
2647                 goto unlock;
2648
2649         /* reset doesn't touch the display */
2650         if (!gpu_reset_clobbers_display(dev_priv)) {
2651                 /* for testing only restore the display */
2652                 ret = __intel_display_resume(dev, state, ctx);
2653                 if (ret)
2654                         drm_err(&dev_priv->drm,
2655                                 "Restoring old state failed with %i\n", ret);
2656         } else {
2657                 /*
2658                  * The display has been reset as well,
2659                  * so need a full re-initialization.
2660                  */
2661                 intel_pps_unlock_regs_wa(dev_priv);
2662                 intel_modeset_init_hw(dev_priv);
2663                 intel_init_clock_gating(dev_priv);
2664                 intel_hpd_init(dev_priv);
2665
2666                 ret = __intel_display_resume(dev, state, ctx);
2667                 if (ret)
2668                         drm_err(&dev_priv->drm,
2669                                 "Restoring old state failed with %i\n", ret);
2670
2671                 intel_hpd_poll_disable(dev_priv);
2672         }
2673
2674         drm_atomic_state_put(state);
2675 unlock:
2676         drm_modeset_drop_locks(ctx);
2677         drm_modeset_acquire_fini(ctx);
2678         mutex_unlock(&dev->mode_config.mutex);
2679
2680         clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
2681 }
2682
2683 static void icl_set_pipe_chicken(struct intel_crtc *crtc)
2684 {
2685         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2686         enum pipe pipe = crtc->pipe;
2687         u32 tmp;
2688
2689         tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
2690
2691         /*
2692          * Display WA #1153: icl
2693          * enable hardware to bypass the alpha math
2694          * and rounding for per-pixel values 00 and 0xff
2695          */
2696         tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
2697         /*
2698          * Display WA # 1605353570: icl
2699          * Set the pixel rounding bit to 1 for allowing
2700          * passthrough of Frame buffer pixels unmodified
2701          * across pipe
2702          */
2703         tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
2704         intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
2705 }
2706
2707 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
2708 {
2709         struct drm_crtc *crtc;
2710         bool cleanup_done;
2711
2712         drm_for_each_crtc(crtc, &dev_priv->drm) {
2713                 struct drm_crtc_commit *commit;
2714                 spin_lock(&crtc->commit_lock);
2715                 commit = list_first_entry_or_null(&crtc->commit_list,
2716                                                   struct drm_crtc_commit, commit_entry);
2717                 cleanup_done = commit ?
2718                         try_wait_for_completion(&commit->cleanup_done) : true;
2719                 spin_unlock(&crtc->commit_lock);
2720
2721                 if (cleanup_done)
2722                         continue;
2723
2724                 drm_crtc_wait_one_vblank(crtc);
2725
2726                 return true;
2727         }
2728
2729         return false;
2730 }
2731
2732 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
2733 {
2734         u32 temp;
2735
2736         intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
2737
2738         mutex_lock(&dev_priv->sb_lock);
2739
2740         temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2741         temp |= SBI_SSCCTL_DISABLE;
2742         intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2743
2744         mutex_unlock(&dev_priv->sb_lock);
2745 }
2746
2747 /* Program iCLKIP clock to the desired frequency */
2748 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
2749 {
2750         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2751         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2752         int clock = crtc_state->hw.adjusted_mode.crtc_clock;
2753         u32 divsel, phaseinc, auxdiv, phasedir = 0;
2754         u32 temp;
2755
2756         lpt_disable_iclkip(dev_priv);
2757
2758         /* The iCLK virtual clock root frequency is in MHz,
2759          * but the adjusted_mode->crtc_clock in in KHz. To get the
2760          * divisors, it is necessary to divide one by another, so we
2761          * convert the virtual clock precision to KHz here for higher
2762          * precision.
2763          */
2764         for (auxdiv = 0; auxdiv < 2; auxdiv++) {
2765                 u32 iclk_virtual_root_freq = 172800 * 1000;
2766                 u32 iclk_pi_range = 64;
2767                 u32 desired_divisor;
2768
2769                 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
2770                                                     clock << auxdiv);
2771                 divsel = (desired_divisor / iclk_pi_range) - 2;
2772                 phaseinc = desired_divisor % iclk_pi_range;
2773
2774                 /*
2775                  * Near 20MHz is a corner case which is
2776                  * out of range for the 7-bit divisor
2777                  */
2778                 if (divsel <= 0x7f)
2779                         break;
2780         }
2781
2782         /* This should not happen with any sane values */
2783         drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2784                     ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2785         drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
2786                     ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2787
2788         drm_dbg_kms(&dev_priv->drm,
2789                     "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2790                     clock, auxdiv, divsel, phasedir, phaseinc);
2791
2792         mutex_lock(&dev_priv->sb_lock);
2793
2794         /* Program SSCDIVINTPHASE6 */
2795         temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2796         temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2797         temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2798         temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2799         temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2800         temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2801         temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2802         intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
2803
2804         /* Program SSCAUXDIV */
2805         temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2806         temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2807         temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2808         intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
2809
2810         /* Enable modulator and associated divider */
2811         temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2812         temp &= ~SBI_SSCCTL_DISABLE;
2813         intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2814
2815         mutex_unlock(&dev_priv->sb_lock);
2816
2817         /* Wait for initialization time */
2818         udelay(24);
2819
2820         intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2821 }
2822
2823 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
2824 {
2825         u32 divsel, phaseinc, auxdiv;
2826         u32 iclk_virtual_root_freq = 172800 * 1000;
2827         u32 iclk_pi_range = 64;
2828         u32 desired_divisor;
2829         u32 temp;
2830
2831         if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
2832                 return 0;
2833
2834         mutex_lock(&dev_priv->sb_lock);
2835
2836         temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2837         if (temp & SBI_SSCCTL_DISABLE) {
2838                 mutex_unlock(&dev_priv->sb_lock);
2839                 return 0;
2840         }
2841
2842         temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2843         divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
2844                 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
2845         phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
2846                 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
2847
2848         temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2849         auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
2850                 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
2851
2852         mutex_unlock(&dev_priv->sb_lock);
2853
2854         desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
2855
2856         return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
2857                                  desired_divisor << auxdiv);
2858 }
2859
2860 static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
2861                                            enum pipe pch_transcoder)
2862 {
2863         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2864         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2865         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2866
2867         intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
2868                        intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
2869         intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
2870                        intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
2871         intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
2872                        intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
2873
2874         intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
2875                        intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
2876         intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
2877                        intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
2878         intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
2879                        intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
2880         intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2881                        intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
2882 }
2883
2884 static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
2885 {
2886         u32 temp;
2887
2888         temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
2889         if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
2890                 return;
2891
2892         drm_WARN_ON(&dev_priv->drm,
2893                     intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
2894                     FDI_RX_ENABLE);
2895         drm_WARN_ON(&dev_priv->drm,
2896                     intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
2897                     FDI_RX_ENABLE);
2898
2899         temp &= ~FDI_BC_BIFURCATION_SELECT;
2900         if (enable)
2901                 temp |= FDI_BC_BIFURCATION_SELECT;
2902
2903         drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
2904                     enable ? "en" : "dis");
2905         intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
2906         intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
2907 }
2908
2909 static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
2910 {
2911         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2912         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2913
2914         switch (crtc->pipe) {
2915         case PIPE_A:
2916                 break;
2917         case PIPE_B:
2918                 if (crtc_state->fdi_lanes > 2)
2919                         cpt_set_fdi_bc_bifurcation(dev_priv, false);
2920                 else
2921                         cpt_set_fdi_bc_bifurcation(dev_priv, true);
2922
2923                 break;
2924         case PIPE_C:
2925                 cpt_set_fdi_bc_bifurcation(dev_priv, true);
2926
2927                 break;
2928         default:
2929                 BUG();
2930         }
2931 }
2932
2933 /*
2934  * Finds the encoder associated with the given CRTC. This can only be
2935  * used when we know that the CRTC isn't feeding multiple encoders!
2936  */
2937 struct intel_encoder *
2938 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
2939                            const struct intel_crtc_state *crtc_state)
2940 {
2941         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2942         const struct drm_connector_state *connector_state;
2943         const struct drm_connector *connector;
2944         struct intel_encoder *encoder = NULL;
2945         int num_encoders = 0;
2946         int i;
2947
2948         for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
2949                 if (connector_state->crtc != &crtc->base)
2950                         continue;
2951
2952                 encoder = to_intel_encoder(connector_state->best_encoder);
2953                 num_encoders++;
2954         }
2955
2956         drm_WARN(encoder->base.dev, num_encoders != 1,
2957                  "%d encoders for pipe %c\n",
2958                  num_encoders, pipe_name(crtc->pipe));
2959
2960         return encoder;
2961 }
2962
2963 /*
2964  * Enable PCH resources required for PCH ports:
2965  *   - PCH PLLs
2966  *   - FDI training & RX/TX
2967  *   - update transcoder timings
2968  *   - DP transcoding bits
2969  *   - transcoder
2970  */
2971 static void ilk_pch_enable(const struct intel_atomic_state *state,
2972                            const struct intel_crtc_state *crtc_state)
2973 {
2974         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2975         struct drm_device *dev = crtc->base.dev;
2976         struct drm_i915_private *dev_priv = to_i915(dev);
2977         enum pipe pipe = crtc->pipe;
2978         u32 temp;
2979
2980         assert_pch_transcoder_disabled(dev_priv, pipe);
2981
2982         if (IS_IVYBRIDGE(dev_priv))
2983                 ivb_update_fdi_bc_bifurcation(crtc_state);
2984
2985         /* Write the TU size bits before fdi link training, so that error
2986          * detection works. */
2987         intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
2988                        intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2989
2990         /* For PCH output, training FDI link */
2991         dev_priv->display.fdi_link_train(crtc, crtc_state);
2992
2993         /* We need to program the right clock selection before writing the pixel
2994          * mutliplier into the DPLL. */
2995         if (HAS_PCH_CPT(dev_priv)) {
2996                 u32 sel;
2997
2998                 temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
2999                 temp |= TRANS_DPLL_ENABLE(pipe);
3000                 sel = TRANS_DPLLB_SEL(pipe);
3001                 if (crtc_state->shared_dpll ==
3002                     intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
3003                         temp |= sel;
3004                 else
3005                         temp &= ~sel;
3006                 intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
3007         }
3008
3009         /* XXX: pch pll's can be enabled any time before we enable the PCH
3010          * transcoder, and we actually should do this to not upset any PCH
3011          * transcoder that already use the clock when we share it.
3012          *
3013          * Note that enable_shared_dpll tries to do the right thing, but
3014          * get_shared_dpll unconditionally resets the pll - we need that to have
3015          * the right LVDS enable sequence. */
3016         intel_enable_shared_dpll(crtc_state);
3017
3018         /* set transcoder timing, panel must allow it */
3019         assert_panel_unlocked(dev_priv, pipe);
3020         ilk_pch_transcoder_set_timings(crtc_state, pipe);
3021
3022         intel_fdi_normal_train(crtc);
3023
3024         /* For PCH DP, enable TRANS_DP_CTL */
3025         if (HAS_PCH_CPT(dev_priv) &&
3026             intel_crtc_has_dp_encoder(crtc_state)) {
3027                 const struct drm_display_mode *adjusted_mode =
3028                         &crtc_state->hw.adjusted_mode;
3029                 u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3030                 i915_reg_t reg = TRANS_DP_CTL(pipe);
3031                 enum port port;
3032
3033                 temp = intel_de_read(dev_priv, reg);
3034                 temp &= ~(TRANS_DP_PORT_SEL_MASK |
3035                           TRANS_DP_SYNC_MASK |
3036                           TRANS_DP_BPC_MASK);
3037                 temp |= TRANS_DP_OUTPUT_ENABLE;
3038                 temp |= bpc << 9; /* same format but at 11:9 */
3039
3040                 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
3041                         temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3042                 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
3043                         temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3044
3045                 port = intel_get_crtc_new_encoder(state, crtc_state)->port;
3046                 drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
3047                 temp |= TRANS_DP_PORT_SEL(port);
3048
3049                 intel_de_write(dev_priv, reg, temp);
3050         }
3051
3052         ilk_enable_pch_transcoder(crtc_state);
3053 }
3054
3055 void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
3056 {
3057         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3058         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3059         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3060
3061         assert_pch_transcoder_disabled(dev_priv, PIPE_A);
3062
3063         lpt_program_iclkip(crtc_state);
3064
3065         /* Set transcoder timing. */
3066         ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
3067
3068         lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3069 }
3070
3071 static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
3072                                enum pipe pipe)
3073 {
3074         i915_reg_t dslreg = PIPEDSL(pipe);
3075         u32 temp;
3076
3077         temp = intel_de_read(dev_priv, dslreg);
3078         udelay(500);
3079         if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
3080                 if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
3081                         drm_err(&dev_priv->drm,
3082                                 "mode set failed: pipe %c stuck\n",
3083                                 pipe_name(pipe));
3084         }
3085 }
3086
3087 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
3088 {
3089         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3090         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3091         const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
3092         enum pipe pipe = crtc->pipe;
3093         int width = drm_rect_width(dst);
3094         int height = drm_rect_height(dst);
3095         int x = dst->x1;
3096         int y = dst->y1;
3097
3098         if (!crtc_state->pch_pfit.enabled)
3099                 return;
3100
3101         /* Force use of hard-coded filter coefficients
3102          * as some pre-programmed values are broken,
3103          * e.g. x201.
3104          */
3105         if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
3106                 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
3107                                PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
3108         else
3109                 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
3110                                PF_FILTER_MED_3x3);
3111         intel_de_write(dev_priv, PF_WIN_POS(pipe), x << 16 | y);
3112         intel_de_write(dev_priv, PF_WIN_SZ(pipe), width << 16 | height);
3113 }
3114
3115 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
3116 {
3117         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3118         struct drm_device *dev = crtc->base.dev;
3119         struct drm_i915_private *dev_priv = to_i915(dev);
3120
3121         if (!crtc_state->ips_enabled)
3122                 return;
3123
3124         /*
3125          * We can only enable IPS after we enable a plane and wait for a vblank
3126          * This function is called from post_plane_update, which is run after
3127          * a vblank wait.
3128          */
3129         drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
3130
3131         if (IS_BROADWELL(dev_priv)) {
3132                 drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
3133                                                          IPS_ENABLE | IPS_PCODE_CONTROL));
3134                 /* Quoting Art Runyan: "its not safe to expect any particular
3135                  * value in IPS_CTL bit 31 after enabling IPS through the
3136                  * mailbox." Moreover, the mailbox may return a bogus state,
3137                  * so we need to just enable it and continue on.
3138                  */
3139         } else {
3140                 intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
3141                 /* The bit only becomes 1 in the next vblank, so this wait here
3142                  * is essentially intel_wait_for_vblank. If we don't have this
3143                  * and don't wait for vblanks until the end of crtc_enable, then
3144                  * the HW state readout code will complain that the expected
3145                  * IPS_CTL value is not the one we read. */
3146                 if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
3147                         drm_err(&dev_priv->drm,
3148                                 "Timed out waiting for IPS enable\n");
3149         }
3150 }
3151
3152 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
3153 {
3154         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3155         struct drm_device *dev = crtc->base.dev;
3156         struct drm_i915_private *dev_priv = to_i915(dev);
3157
3158         if (!crtc_state->ips_enabled)
3159                 return;
3160
3161         if (IS_BROADWELL(dev_priv)) {
3162                 drm_WARN_ON(dev,
3163                             sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3164                 /*
3165                  * Wait for PCODE to finish disabling IPS. The BSpec specified
3166                  * 42ms timeout value leads to occasional timeouts so use 100ms
3167                  * instead.
3168                  */
3169                 if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
3170                         drm_err(&dev_priv->drm,
3171                                 "Timed out waiting for IPS disable\n");
3172         } else {
3173                 intel_de_write(dev_priv, IPS_CTL, 0);
3174                 intel_de_posting_read(dev_priv, IPS_CTL);
3175         }
3176
3177         /* We need to wait for a vblank before we can disable the plane. */
3178         intel_wait_for_vblank(dev_priv, crtc->pipe);
3179 }
3180
3181 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
3182 {
3183         if (intel_crtc->overlay)
3184                 (void) intel_overlay_switch_off(intel_crtc->overlay);
3185
3186         /* Let userspace switch the overlay on again. In most cases userspace
3187          * has to recompute where to put it anyway.
3188          */
3189 }
3190
3191 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
3192                                        const struct intel_crtc_state *new_crtc_state)
3193 {
3194         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
3195         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3196
3197         if (!old_crtc_state->ips_enabled)
3198                 return false;
3199
3200         if (intel_crtc_needs_modeset(new_crtc_state))
3201                 return true;
3202
3203         /*
3204          * Workaround : Do not read or write the pipe palette/gamma data while
3205          * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3206          *
3207          * Disable IPS before we program the LUT.
3208          */
3209         if (IS_HASWELL(dev_priv) &&
3210             (new_crtc_state->uapi.color_mgmt_changed ||
3211              new_crtc_state->update_pipe) &&
3212             new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
3213                 return true;
3214
3215         return !new_crtc_state->ips_enabled;
3216 }
3217
3218 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
3219                                        const struct intel_crtc_state *new_crtc_state)
3220 {
3221         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
3222         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3223
3224         if (!new_crtc_state->ips_enabled)
3225                 return false;
3226
3227         if (intel_crtc_needs_modeset(new_crtc_state))
3228                 return true;
3229
3230         /*
3231          * Workaround : Do not read or write the pipe palette/gamma data while
3232          * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3233          *
3234          * Re-enable IPS after the LUT has been programmed.
3235          */
3236         if (IS_HASWELL(dev_priv) &&
3237             (new_crtc_state->uapi.color_mgmt_changed ||
3238              new_crtc_state->update_pipe) &&
3239             new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
3240                 return true;
3241
3242         /*
3243          * We can't read out IPS on broadwell, assume the worst and
3244          * forcibly enable IPS on the first fastset.
3245          */
3246         if (new_crtc_state->update_pipe && old_crtc_state->inherited)
3247                 return true;
3248
3249         return !old_crtc_state->ips_enabled;
3250 }
3251
3252 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
3253 {
3254         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3255
3256         if (!crtc_state->nv12_planes)
3257                 return false;
3258
3259         /* WA Display #0827: Gen9:all */
3260         if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
3261                 return true;
3262
3263         return false;
3264 }
3265
3266 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
3267 {
3268         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3269
3270         /* Wa_2006604312:icl,ehl */
3271         if (crtc_state->scaler_state.scaler_users > 0 && IS_GEN(dev_priv, 11))
3272                 return true;
3273
3274         return false;
3275 }
3276
3277 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
3278                             const struct intel_crtc_state *new_crtc_state)
3279 {
3280         return (!old_crtc_state->active_planes || intel_crtc_needs_modeset(new_crtc_state)) &&
3281                 new_crtc_state->active_planes;
3282 }
3283
3284 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
3285                              const struct intel_crtc_state *new_crtc_state)
3286 {
3287         return old_crtc_state->active_planes &&
3288                 (!new_crtc_state->active_planes || intel_crtc_needs_modeset(new_crtc_state));
3289 }
3290
3291 static void intel_post_plane_update(struct intel_atomic_state *state,
3292                                     struct intel_crtc *crtc)
3293 {
3294         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3295         const struct intel_crtc_state *old_crtc_state =
3296                 intel_atomic_get_old_crtc_state(state, crtc);
3297         const struct intel_crtc_state *new_crtc_state =
3298                 intel_atomic_get_new_crtc_state(state, crtc);
3299         enum pipe pipe = crtc->pipe;
3300
3301         intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
3302
3303         if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
3304                 intel_update_watermarks(crtc);
3305
3306         if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
3307                 hsw_enable_ips(new_crtc_state);
3308
3309         intel_fbc_post_update(state, crtc);
3310
3311         if (needs_nv12_wa(old_crtc_state) &&
3312             !needs_nv12_wa(new_crtc_state))
3313                 skl_wa_827(dev_priv, pipe, false);
3314
3315         if (needs_scalerclk_wa(old_crtc_state) &&
3316             !needs_scalerclk_wa(new_crtc_state))
3317                 icl_wa_scalerclkgating(dev_priv, pipe, false);
3318 }
3319
3320 static void intel_crtc_enable_flip_done(struct intel_atomic_state *state,
3321                                         struct intel_crtc *crtc)
3322 {
3323         const struct intel_crtc_state *crtc_state =
3324                 intel_atomic_get_new_crtc_state(state, crtc);
3325         u8 update_planes = crtc_state->update_planes;
3326         const struct intel_plane_state *plane_state;
3327         struct intel_plane *plane;
3328         int i;
3329
3330         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
3331                 if (plane->enable_flip_done &&
3332                     plane->pipe == crtc->pipe &&
3333                     update_planes & BIT(plane->id))
3334                         plane->enable_flip_done(plane);
3335         }
3336 }
3337
3338 static void intel_crtc_disable_flip_done(struct intel_atomic_state *state,
3339                                          struct intel_crtc *crtc)
3340 {
3341         const struct intel_crtc_state *crtc_state =
3342                 intel_atomic_get_new_crtc_state(state, crtc);
3343         u8 update_planes = crtc_state->update_planes;
3344         const struct intel_plane_state *plane_state;
3345         struct intel_plane *plane;
3346         int i;
3347
3348         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
3349                 if (plane->disable_flip_done &&
3350                     plane->pipe == crtc->pipe &&
3351                     update_planes & BIT(plane->id))
3352                         plane->disable_flip_done(plane);
3353         }
3354 }
3355
3356 static void intel_crtc_async_flip_disable_wa(struct intel_atomic_state *state,
3357                                              struct intel_crtc *crtc)
3358 {
3359         struct drm_i915_private *i915 = to_i915(state->base.dev);
3360         const struct intel_crtc_state *old_crtc_state =
3361                 intel_atomic_get_old_crtc_state(state, crtc);
3362         const struct intel_crtc_state *new_crtc_state =
3363                 intel_atomic_get_new_crtc_state(state, crtc);
3364         u8 update_planes = new_crtc_state->update_planes;
3365         const struct intel_plane_state *old_plane_state;
3366         struct intel_plane *plane;
3367         bool need_vbl_wait = false;
3368         int i;
3369
3370         for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
3371                 if (plane->need_async_flip_disable_wa &&
3372                     plane->pipe == crtc->pipe &&
3373                     update_planes & BIT(plane->id)) {
3374                         /*
3375                          * Apart from the async flip bit we want to
3376                          * preserve the old state for the plane.
3377                          */
3378                         plane->async_flip(plane, old_crtc_state,
3379                                           old_plane_state, false);
3380                         need_vbl_wait = true;
3381                 }
3382         }
3383
3384         if (need_vbl_wait)
3385                 intel_wait_for_vblank(i915, crtc->pipe);
3386 }
3387
3388 static void intel_pre_plane_update(struct intel_atomic_state *state,
3389                                    struct intel_crtc *crtc)
3390 {
3391         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3392         const struct intel_crtc_state *old_crtc_state =
3393                 intel_atomic_get_old_crtc_state(state, crtc);
3394         const struct intel_crtc_state *new_crtc_state =
3395                 intel_atomic_get_new_crtc_state(state, crtc);
3396         enum pipe pipe = crtc->pipe;
3397
3398         if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
3399                 hsw_disable_ips(old_crtc_state);
3400
3401         if (intel_fbc_pre_update(state, crtc))
3402                 intel_wait_for_vblank(dev_priv, pipe);
3403
3404         /* Display WA 827 */
3405         if (!needs_nv12_wa(old_crtc_state) &&
3406             needs_nv12_wa(new_crtc_state))
3407                 skl_wa_827(dev_priv, pipe, true);
3408
3409         /* Wa_2006604312:icl,ehl */
3410         if (!needs_scalerclk_wa(old_crtc_state) &&
3411             needs_scalerclk_wa(new_crtc_state))
3412                 icl_wa_scalerclkgating(dev_priv, pipe, true);
3413
3414         /*
3415          * Vblank time updates from the shadow to live plane control register
3416          * are blocked if the memory self-refresh mode is active at that
3417          * moment. So to make sure the plane gets truly disabled, disable
3418          * first the self-refresh mode. The self-refresh enable bit in turn
3419          * will be checked/applied by the HW only at the next frame start
3420          * event which is after the vblank start event, so we need to have a
3421          * wait-for-vblank between disabling the plane and the pipe.
3422          */
3423         if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
3424             new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
3425                 intel_wait_for_vblank(dev_priv, pipe);
3426
3427         /*
3428          * IVB workaround: must disable low power watermarks for at least
3429          * one frame before enabling scaling.  LP watermarks can be re-enabled
3430          * when scaling is disabled.
3431          *
3432          * WaCxSRDisabledForSpriteScaling:ivb
3433          */
3434         if (old_crtc_state->hw.active &&
3435             new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
3436                 intel_wait_for_vblank(dev_priv, pipe);
3437
3438         /*
3439          * If we're doing a modeset we don't need to do any
3440          * pre-vblank watermark programming here.
3441          */
3442         if (!intel_crtc_needs_modeset(new_crtc_state)) {
3443                 /*
3444                  * For platforms that support atomic watermarks, program the
3445                  * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
3446                  * will be the intermediate values that are safe for both pre- and
3447                  * post- vblank; when vblank happens, the 'active' values will be set
3448                  * to the final 'target' values and we'll do this again to get the
3449                  * optimal watermarks.  For gen9+ platforms, the values we program here
3450                  * will be the final target values which will get automatically latched
3451                  * at vblank time; no further programming will be necessary.
3452                  *
3453                  * If a platform hasn't been transitioned to atomic watermarks yet,
3454                  * we'll continue to update watermarks the old way, if flags tell
3455                  * us to.
3456                  */
3457                 if (dev_priv->display.initial_watermarks)
3458                         dev_priv->display.initial_watermarks(state, crtc);
3459                 else if (new_crtc_state->update_wm_pre)
3460                         intel_update_watermarks(crtc);
3461         }
3462
3463         /*
3464          * Gen2 reports pipe underruns whenever all planes are disabled.
3465          * So disable underrun reporting before all the planes get disabled.
3466          *
3467          * We do this after .initial_watermarks() so that we have a
3468          * chance of catching underruns with the intermediate watermarks
3469          * vs. the old plane configuration.
3470          */
3471         if (IS_GEN(dev_priv, 2) && planes_disabling(old_crtc_state, new_crtc_state))
3472                 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
3473
3474         /*
3475          * WA for platforms where async address update enable bit
3476          * is double buffered and only latched at start of vblank.
3477          */
3478         if (old_crtc_state->uapi.async_flip && !new_crtc_state->uapi.async_flip)
3479                 intel_crtc_async_flip_disable_wa(state, crtc);
3480 }
3481
3482 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
3483                                       struct intel_crtc *crtc)
3484 {
3485         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3486         const struct intel_crtc_state *new_crtc_state =
3487                 intel_atomic_get_new_crtc_state(state, crtc);
3488         unsigned int update_mask = new_crtc_state->update_planes;
3489         const struct intel_plane_state *old_plane_state;
3490         struct intel_plane *plane;
3491         unsigned fb_bits = 0;
3492         int i;
3493
3494         intel_crtc_dpms_overlay_disable(crtc);
3495
3496         for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
3497                 if (crtc->pipe != plane->pipe ||
3498                     !(update_mask & BIT(plane->id)))
3499                         continue;
3500
3501                 intel_disable_plane(plane, new_crtc_state);
3502
3503                 if (old_plane_state->uapi.visible)
3504                         fb_bits |= plane->frontbuffer_bit;
3505         }
3506
3507         intel_frontbuffer_flip(dev_priv, fb_bits);
3508 }
3509
3510 /*
3511  * intel_connector_primary_encoder - get the primary encoder for a connector
3512  * @connector: connector for which to return the encoder
3513  *
3514  * Returns the primary encoder for a connector. There is a 1:1 mapping from
3515  * all connectors to their encoder, except for DP-MST connectors which have
3516  * both a virtual and a primary encoder. These DP-MST primary encoders can be
3517  * pointed to by as many DP-MST connectors as there are pipes.
3518  */
3519 static struct intel_encoder *
3520 intel_connector_primary_encoder(struct intel_connector *connector)
3521 {
3522         struct intel_encoder *encoder;
3523
3524         if (connector->mst_port)
3525                 return &dp_to_dig_port(connector->mst_port)->base;
3526
3527         encoder = intel_attached_encoder(connector);
3528         drm_WARN_ON(connector->base.dev, !encoder);
3529
3530         return encoder;
3531 }
3532
3533 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
3534 {
3535         struct drm_connector_state *new_conn_state;
3536         struct drm_connector *connector;
3537         int i;
3538
3539         for_each_new_connector_in_state(&state->base, connector, new_conn_state,
3540                                         i) {
3541                 struct intel_connector *intel_connector;
3542                 struct intel_encoder *encoder;
3543                 struct intel_crtc *crtc;
3544
3545                 if (!intel_connector_needs_modeset(state, connector))
3546                         continue;
3547
3548                 intel_connector = to_intel_connector(connector);
3549                 encoder = intel_connector_primary_encoder(intel_connector);
3550                 if (!encoder->update_prepare)
3551                         continue;
3552
3553                 crtc = new_conn_state->crtc ?
3554                         to_intel_crtc(new_conn_state->crtc) : NULL;
3555                 encoder->update_prepare(state, encoder, crtc);
3556         }
3557 }
3558
3559 static void intel_encoders_update_complete(struct intel_atomic_state *state)
3560 {
3561         struct drm_connector_state *new_conn_state;
3562         struct drm_connector *connector;
3563         int i;
3564
3565         for_each_new_connector_in_state(&state->base, connector, new_conn_state,
3566                                         i) {
3567                 struct intel_connector *intel_connector;
3568                 struct intel_encoder *encoder;
3569                 struct intel_crtc *crtc;
3570
3571                 if (!intel_connector_needs_modeset(state, connector))
3572                         continue;
3573
3574                 intel_connector = to_intel_connector(connector);
3575                 encoder = intel_connector_primary_encoder(intel_connector);
3576                 if (!encoder->update_complete)
3577                         continue;
3578
3579                 crtc = new_conn_state->crtc ?
3580                         to_intel_crtc(new_conn_state->crtc) : NULL;
3581                 encoder->update_complete(state, encoder, crtc);
3582         }
3583 }
3584
3585 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
3586                                           struct intel_crtc *crtc)
3587 {
3588         const struct intel_crtc_state *crtc_state =
3589                 intel_atomic_get_new_crtc_state(state, crtc);
3590         const struct drm_connector_state *conn_state;
3591         struct drm_connector *conn;
3592         int i;
3593
3594         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3595                 struct intel_encoder *encoder =
3596                         to_intel_encoder(conn_state->best_encoder);
3597
3598                 if (conn_state->crtc != &crtc->base)
3599                         continue;
3600
3601                 if (encoder->pre_pll_enable)
3602                         encoder->pre_pll_enable(state, encoder,
3603                                                 crtc_state, conn_state);
3604         }
3605 }
3606
3607 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
3608                                       struct intel_crtc *crtc)
3609 {
3610         const struct intel_crtc_state *crtc_state =
3611                 intel_atomic_get_new_crtc_state(state, crtc);
3612         const struct drm_connector_state *conn_state;
3613         struct drm_connector *conn;
3614         int i;
3615
3616         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3617                 struct intel_encoder *encoder =
3618                         to_intel_encoder(conn_state->best_encoder);
3619
3620                 if (conn_state->crtc != &crtc->base)
3621                         continue;
3622
3623                 if (encoder->pre_enable)
3624                         encoder->pre_enable(state, encoder,
3625                                             crtc_state, conn_state);
3626         }
3627 }
3628
3629 static void intel_encoders_enable(struct intel_atomic_state *state,
3630                                   struct intel_crtc *crtc)
3631 {
3632         const struct intel_crtc_state *crtc_state =
3633                 intel_atomic_get_new_crtc_state(state, crtc);
3634         const struct drm_connector_state *conn_state;
3635         struct drm_connector *conn;
3636         int i;
3637
3638         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3639                 struct intel_encoder *encoder =
3640                         to_intel_encoder(conn_state->best_encoder);
3641
3642                 if (conn_state->crtc != &crtc->base)
3643                         continue;
3644
3645                 if (encoder->enable)
3646                         encoder->enable(state, encoder,
3647                                         crtc_state, conn_state);
3648                 intel_opregion_notify_encoder(encoder, true);
3649         }
3650 }
3651
3652 static void intel_encoders_disable(struct intel_atomic_state *state,
3653                                    struct intel_crtc *crtc)
3654 {
3655         const struct intel_crtc_state *old_crtc_state =
3656                 intel_atomic_get_old_crtc_state(state, crtc);
3657         const struct drm_connector_state *old_conn_state;
3658         struct drm_connector *conn;
3659         int i;
3660
3661         for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
3662                 struct intel_encoder *encoder =
3663                         to_intel_encoder(old_conn_state->best_encoder);
3664
3665                 if (old_conn_state->crtc != &crtc->base)
3666                         continue;
3667
3668                 intel_opregion_notify_encoder(encoder, false);
3669                 if (encoder->disable)
3670                         encoder->disable(state, encoder,
3671                                          old_crtc_state, old_conn_state);
3672         }
3673 }
3674
3675 static void intel_encoders_post_disable(struct intel_atomic_state *state,
3676                                         struct intel_crtc *crtc)
3677 {
3678         const struct intel_crtc_state *old_crtc_state =
3679                 intel_atomic_get_old_crtc_state(state, crtc);
3680         const struct drm_connector_state *old_conn_state;
3681         struct drm_connector *conn;
3682         int i;
3683
3684         for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
3685                 struct intel_encoder *encoder =
3686                         to_intel_encoder(old_conn_state->best_encoder);
3687
3688                 if (old_conn_state->crtc != &crtc->base)
3689                         continue;
3690
3691                 if (encoder->post_disable)
3692                         encoder->post_disable(state, encoder,
3693                                               old_crtc_state, old_conn_state);
3694         }
3695 }
3696
3697 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
3698                                             struct intel_crtc *crtc)
3699 {
3700         const struct intel_crtc_state *old_crtc_state =
3701                 intel_atomic_get_old_crtc_state(state, crtc);
3702         const struct drm_connector_state *old_conn_state;
3703         struct drm_connector *conn;
3704         int i;
3705
3706         for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
3707                 struct intel_encoder *encoder =
3708                         to_intel_encoder(old_conn_state->best_encoder);
3709
3710                 if (old_conn_state->crtc != &crtc->base)
3711                         continue;
3712
3713                 if (encoder->post_pll_disable)
3714                         encoder->post_pll_disable(state, encoder,
3715                                                   old_crtc_state, old_conn_state);
3716         }
3717 }
3718
3719 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
3720                                        struct intel_crtc *crtc)
3721 {
3722         const struct intel_crtc_state *crtc_state =
3723                 intel_atomic_get_new_crtc_state(state, crtc);
3724         const struct drm_connector_state *conn_state;
3725         struct drm_connector *conn;
3726         int i;
3727
3728         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3729                 struct intel_encoder *encoder =
3730                         to_intel_encoder(conn_state->best_encoder);
3731
3732                 if (conn_state->crtc != &crtc->base)
3733                         continue;
3734
3735                 if (encoder->update_pipe)
3736                         encoder->update_pipe(state, encoder,
3737                                              crtc_state, conn_state);
3738         }
3739 }
3740
3741 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
3742 {
3743         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3744         struct intel_plane *plane = to_intel_plane(crtc->base.primary);
3745
3746         plane->disable_plane(plane, crtc_state);
3747 }
3748
3749 static void ilk_crtc_enable(struct intel_atomic_state *state,
3750                             struct intel_crtc *crtc)
3751 {
3752         const struct intel_crtc_state *new_crtc_state =
3753                 intel_atomic_get_new_crtc_state(state, crtc);
3754         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3755         enum pipe pipe = crtc->pipe;
3756
3757         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3758                 return;
3759
3760         /*
3761          * Sometimes spurious CPU pipe underruns happen during FDI
3762          * training, at least with VGA+HDMI cloning. Suppress them.
3763          *
3764          * On ILK we get an occasional spurious CPU pipe underruns
3765          * between eDP port A enable and vdd enable. Also PCH port
3766          * enable seems to result in the occasional CPU pipe underrun.
3767          *
3768          * Spurious PCH underruns also occur during PCH enabling.
3769          */
3770         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
3771         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
3772
3773         if (new_crtc_state->has_pch_encoder)
3774                 intel_prepare_shared_dpll(new_crtc_state);
3775
3776         if (intel_crtc_has_dp_encoder(new_crtc_state))
3777                 intel_dp_set_m_n(new_crtc_state, M1_N1);
3778
3779         intel_set_transcoder_timings(new_crtc_state);
3780         intel_set_pipe_src_size(new_crtc_state);
3781
3782         if (new_crtc_state->has_pch_encoder)
3783                 intel_cpu_transcoder_set_m_n(new_crtc_state,
3784                                              &new_crtc_state->fdi_m_n, NULL);
3785
3786         ilk_set_pipeconf(new_crtc_state);
3787
3788         crtc->active = true;
3789
3790         intel_encoders_pre_enable(state, crtc);
3791
3792         if (new_crtc_state->has_pch_encoder) {
3793                 /* Note: FDI PLL enabling _must_ be done before we enable the
3794                  * cpu pipes, hence this is separate from all the other fdi/pch
3795                  * enabling. */
3796                 ilk_fdi_pll_enable(new_crtc_state);
3797         } else {
3798                 assert_fdi_tx_disabled(dev_priv, pipe);
3799                 assert_fdi_rx_disabled(dev_priv, pipe);
3800         }
3801
3802         ilk_pfit_enable(new_crtc_state);
3803
3804         /*
3805          * On ILK+ LUT must be loaded before the pipe is running but with
3806          * clocks enabled
3807          */
3808         intel_color_load_luts(new_crtc_state);
3809         intel_color_commit(new_crtc_state);
3810         /* update DSPCNTR to configure gamma for pipe bottom color */
3811         intel_disable_primary_plane(new_crtc_state);
3812
3813         if (dev_priv->display.initial_watermarks)
3814                 dev_priv->display.initial_watermarks(state, crtc);
3815         intel_enable_pipe(new_crtc_state);
3816
3817         if (new_crtc_state->has_pch_encoder)
3818                 ilk_pch_enable(state, new_crtc_state);
3819
3820         intel_crtc_vblank_on(new_crtc_state);
3821
3822         intel_encoders_enable(state, crtc);
3823
3824         if (HAS_PCH_CPT(dev_priv))
3825                 cpt_verify_modeset(dev_priv, pipe);
3826
3827         /*
3828          * Must wait for vblank to avoid spurious PCH FIFO underruns.
3829          * And a second vblank wait is needed at least on ILK with
3830          * some interlaced HDMI modes. Let's do the double wait always
3831          * in case there are more corner cases we don't know about.
3832          */
3833         if (new_crtc_state->has_pch_encoder) {
3834                 intel_wait_for_vblank(dev_priv, pipe);
3835                 intel_wait_for_vblank(dev_priv, pipe);
3836         }
3837         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3838         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
3839 }
3840
3841 /* IPS only exists on ULT machines and is tied to pipe A. */
3842 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3843 {
3844         return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
3845 }
3846
3847 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
3848                                             enum pipe pipe, bool apply)
3849 {
3850         u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
3851         u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
3852
3853         if (apply)
3854                 val |= mask;
3855         else
3856                 val &= ~mask;
3857
3858         intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
3859 }
3860
3861 static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
3862 {
3863         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3864         enum pipe pipe = crtc->pipe;
3865         u32 val;
3866
3867         val = MBUS_DBOX_A_CREDIT(2);
3868
3869         if (INTEL_GEN(dev_priv) >= 12) {
3870                 val |= MBUS_DBOX_BW_CREDIT(2);
3871                 val |= MBUS_DBOX_B_CREDIT(12);
3872         } else {
3873                 val |= MBUS_DBOX_BW_CREDIT(1);
3874                 val |= MBUS_DBOX_B_CREDIT(8);
3875         }
3876
3877         intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
3878 }
3879
3880 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
3881 {
3882         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3883         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3884
3885         intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
3886                        HSW_LINETIME(crtc_state->linetime) |
3887                        HSW_IPS_LINETIME(crtc_state->ips_linetime));
3888 }
3889
3890 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
3891 {
3892         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3893         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3894         i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
3895         u32 val;
3896
3897         val = intel_de_read(dev_priv, reg);
3898         val &= ~HSW_FRAME_START_DELAY_MASK;
3899         val |= HSW_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
3900         intel_de_write(dev_priv, reg, val);
3901 }
3902
3903 static void icl_ddi_bigjoiner_pre_enable(struct intel_atomic_state *state,
3904                                          const struct intel_crtc_state *crtc_state)
3905 {
3906         struct intel_crtc *master = to_intel_crtc(crtc_state->uapi.crtc);
3907         struct intel_crtc_state *master_crtc_state;
3908         struct drm_connector_state *conn_state;
3909         struct drm_connector *conn;
3910         struct intel_encoder *encoder = NULL;
3911         int i;
3912
3913         if (crtc_state->bigjoiner_slave)
3914                 master = crtc_state->bigjoiner_linked_crtc;
3915
3916         master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
3917
3918         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3919                 if (conn_state->crtc != &master->base)
3920                         continue;
3921
3922                 encoder = to_intel_encoder(conn_state->best_encoder);
3923                 break;
3924         }
3925
3926         if (!crtc_state->bigjoiner_slave) {
3927                 /* need to enable VDSC, which we skipped in pre-enable */
3928                 intel_dsc_enable(encoder, crtc_state);
3929         } else {
3930                 /*
3931                  * Enable sequence steps 1-7 on bigjoiner master
3932                  */
3933                 intel_encoders_pre_pll_enable(state, master);
3934                 intel_enable_shared_dpll(master_crtc_state);
3935                 intel_encoders_pre_enable(state, master);
3936
3937                 /* and DSC on slave */
3938                 intel_dsc_enable(NULL, crtc_state);
3939         }
3940 }
3941
3942 static void hsw_crtc_enable(struct intel_atomic_state *state,
3943                             struct intel_crtc *crtc)
3944 {
3945         const struct intel_crtc_state *new_crtc_state =
3946                 intel_atomic_get_new_crtc_state(state, crtc);
3947         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3948         enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
3949         enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
3950         bool psl_clkgate_wa;
3951
3952         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3953                 return;
3954
3955         if (!new_crtc_state->bigjoiner) {
3956                 intel_encoders_pre_pll_enable(state, crtc);
3957
3958                 if (new_crtc_state->shared_dpll)
3959                         intel_enable_shared_dpll(new_crtc_state);
3960
3961                 intel_encoders_pre_enable(state, crtc);
3962         } else {
3963                 icl_ddi_bigjoiner_pre_enable(state, new_crtc_state);
3964         }
3965
3966         intel_set_pipe_src_size(new_crtc_state);
3967         if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
3968                 bdw_set_pipemisc(new_crtc_state);
3969
3970         if (!new_crtc_state->bigjoiner_slave && !transcoder_is_dsi(cpu_transcoder)) {
3971                 intel_set_transcoder_timings(new_crtc_state);
3972
3973                 if (cpu_transcoder != TRANSCODER_EDP)
3974                         intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
3975                                        new_crtc_state->pixel_multiplier - 1);
3976
3977                 if (new_crtc_state->has_pch_encoder)
3978                         intel_cpu_transcoder_set_m_n(new_crtc_state,
3979                                                      &new_crtc_state->fdi_m_n, NULL);
3980
3981                 hsw_set_frame_start_delay(new_crtc_state);
3982         }
3983
3984         if (!transcoder_is_dsi(cpu_transcoder))
3985                 hsw_set_pipeconf(new_crtc_state);
3986
3987         crtc->active = true;
3988
3989         /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
3990         psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
3991                 new_crtc_state->pch_pfit.enabled;
3992         if (psl_clkgate_wa)
3993                 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
3994
3995         if (INTEL_GEN(dev_priv) >= 9)
3996                 skl_pfit_enable(new_crtc_state);
3997         else
3998                 ilk_pfit_enable(new_crtc_state);
3999
4000         /*
4001          * On ILK+ LUT must be loaded before the pipe is running but with
4002          * clocks enabled
4003          */
4004         intel_color_load_luts(new_crtc_state);
4005         intel_color_commit(new_crtc_state);
4006         /* update DSPCNTR to configure gamma/csc for pipe bottom color */
4007         if (INTEL_GEN(dev_priv) < 9)
4008                 intel_disable_primary_plane(new_crtc_state);
4009
4010         hsw_set_linetime_wm(new_crtc_state);
4011
4012         if (INTEL_GEN(dev_priv) >= 11)
4013                 icl_set_pipe_chicken(crtc);
4014
4015         if (dev_priv->display.initial_watermarks)
4016                 dev_priv->display.initial_watermarks(state, crtc);
4017
4018         if (INTEL_GEN(dev_priv) >= 11)
4019                 icl_pipe_mbus_enable(crtc);
4020
4021         if (new_crtc_state->bigjoiner_slave)
4022                 intel_crtc_vblank_on(new_crtc_state);
4023
4024         intel_encoders_enable(state, crtc);
4025
4026         if (psl_clkgate_wa) {
4027                 intel_wait_for_vblank(dev_priv, pipe);
4028                 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
4029         }
4030
4031         /* If we change the relative order between pipe/planes enabling, we need
4032          * to change the workaround. */
4033         hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
4034         if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
4035                 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
4036                 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
4037         }
4038 }
4039
4040 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
4041 {
4042         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
4043         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4044         enum pipe pipe = crtc->pipe;
4045
4046         /* To avoid upsetting the power well on haswell only disable the pfit if
4047          * it's in use. The hw state code will make sure we get this right. */
4048         if (!old_crtc_state->pch_pfit.enabled)
4049                 return;
4050
4051         intel_de_write(dev_priv, PF_CTL(pipe), 0);
4052         intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
4053         intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
4054 }
4055
4056 static void ilk_crtc_disable(struct intel_atomic_state *state,
4057                              struct intel_crtc *crtc)
4058 {
4059         const struct intel_crtc_state *old_crtc_state =
4060                 intel_atomic_get_old_crtc_state(state, crtc);
4061         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4062         enum pipe pipe = crtc->pipe;
4063
4064         /*
4065          * Sometimes spurious CPU pipe underruns happen when the
4066          * pipe is already disabled, but FDI RX/TX is still enabled.
4067          * Happens at least with VGA+HDMI cloning. Suppress them.
4068          */
4069         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4070         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4071
4072         intel_encoders_disable(state, crtc);
4073
4074         intel_crtc_vblank_off(old_crtc_state);
4075
4076         intel_disable_pipe(old_crtc_state);
4077
4078         ilk_pfit_disable(old_crtc_state);
4079
4080         if (old_crtc_state->has_pch_encoder)
4081                 ilk_fdi_disable(crtc);
4082
4083         intel_encoders_post_disable(state, crtc);
4084
4085         if (old_crtc_state->has_pch_encoder) {
4086                 ilk_disable_pch_transcoder(dev_priv, pipe);
4087
4088                 if (HAS_PCH_CPT(dev_priv)) {
4089                         i915_reg_t reg;
4090                         u32 temp;
4091
4092                         /* disable TRANS_DP_CTL */
4093                         reg = TRANS_DP_CTL(pipe);
4094                         temp = intel_de_read(dev_priv, reg);
4095                         temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4096                                   TRANS_DP_PORT_SEL_MASK);
4097                         temp |= TRANS_DP_PORT_SEL_NONE;
4098                         intel_de_write(dev_priv, reg, temp);
4099
4100                         /* disable DPLL_SEL */
4101                         temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
4102                         temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
4103                         intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
4104                 }
4105
4106                 ilk_fdi_pll_disable(crtc);
4107         }
4108
4109         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4110         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
4111 }
4112
4113 static void hsw_crtc_disable(struct intel_atomic_state *state,
4114                              struct intel_crtc *crtc)
4115 {
4116         /*
4117          * FIXME collapse everything to one hook.
4118          * Need care with mst->ddi interactions.
4119          */
4120         intel_encoders_disable(state, crtc);
4121         intel_encoders_post_disable(state, crtc);
4122 }
4123
4124 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
4125 {
4126         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4127         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4128
4129         if (!crtc_state->gmch_pfit.control)
4130                 return;
4131
4132         /*
4133          * The panel fitter should only be adjusted whilst the pipe is disabled,
4134          * according to register description and PRM.
4135          */
4136         drm_WARN_ON(&dev_priv->drm,
4137                     intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
4138         assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
4139
4140         intel_de_write(dev_priv, PFIT_PGM_RATIOS,
4141                        crtc_state->gmch_pfit.pgm_ratios);
4142         intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
4143
4144         /* Border color in case we don't scale up to the full screen. Black by
4145          * default, change to something else for debugging. */
4146         intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
4147 }
4148
4149 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
4150 {
4151         if (phy == PHY_NONE)
4152                 return false;
4153         else if (IS_ALDERLAKE_S(dev_priv))
4154                 return phy <= PHY_E;
4155         else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv))
4156                 return phy <= PHY_D;
4157         else if (IS_JSL_EHL(dev_priv))
4158                 return phy <= PHY_C;
4159         else if (INTEL_GEN(dev_priv) >= 11)
4160                 return phy <= PHY_B;
4161         else
4162                 return false;
4163 }
4164
4165 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
4166 {
4167         if (IS_TIGERLAKE(dev_priv))
4168                 return phy >= PHY_D && phy <= PHY_I;
4169         else if (IS_ICELAKE(dev_priv))
4170                 return phy >= PHY_C && phy <= PHY_F;
4171         else
4172                 return false;
4173 }
4174
4175 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
4176 {
4177         if (IS_ALDERLAKE_S(i915) && port >= PORT_TC1)
4178                 return PHY_B + port - PORT_TC1;
4179         else if ((IS_DG1(i915) || IS_ROCKETLAKE(i915)) && port >= PORT_TC1)
4180                 return PHY_C + port - PORT_TC1;
4181         else if (IS_JSL_EHL(i915) && port == PORT_D)
4182                 return PHY_A;
4183
4184         return PHY_A + port - PORT_A;
4185 }
4186
4187 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
4188 {
4189         if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
4190                 return TC_PORT_NONE;
4191
4192         if (INTEL_GEN(dev_priv) >= 12)
4193                 return TC_PORT_1 + port - PORT_TC1;
4194         else
4195                 return TC_PORT_1 + port - PORT_C;
4196 }
4197
4198 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
4199 {
4200         switch (port) {
4201         case PORT_A:
4202                 return POWER_DOMAIN_PORT_DDI_A_LANES;
4203         case PORT_B:
4204                 return POWER_DOMAIN_PORT_DDI_B_LANES;
4205         case PORT_C:
4206                 return POWER_DOMAIN_PORT_DDI_C_LANES;
4207         case PORT_D:
4208                 return POWER_DOMAIN_PORT_DDI_D_LANES;
4209         case PORT_E:
4210                 return POWER_DOMAIN_PORT_DDI_E_LANES;
4211         case PORT_F:
4212                 return POWER_DOMAIN_PORT_DDI_F_LANES;
4213         case PORT_G:
4214                 return POWER_DOMAIN_PORT_DDI_G_LANES;
4215         case PORT_H:
4216                 return POWER_DOMAIN_PORT_DDI_H_LANES;
4217         case PORT_I:
4218                 return POWER_DOMAIN_PORT_DDI_I_LANES;
4219         default:
4220                 MISSING_CASE(port);
4221                 return POWER_DOMAIN_PORT_OTHER;
4222         }
4223 }
4224
4225 enum intel_display_power_domain
4226 intel_aux_power_domain(struct intel_digital_port *dig_port)
4227 {
4228         struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4229         enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
4230
4231         if (intel_phy_is_tc(dev_priv, phy) &&
4232             dig_port->tc_mode == TC_PORT_TBT_ALT) {
4233                 switch (dig_port->aux_ch) {
4234                 case AUX_CH_C:
4235                         return POWER_DOMAIN_AUX_C_TBT;
4236                 case AUX_CH_D:
4237                         return POWER_DOMAIN_AUX_D_TBT;
4238                 case AUX_CH_E:
4239                         return POWER_DOMAIN_AUX_E_TBT;
4240                 case AUX_CH_F:
4241                         return POWER_DOMAIN_AUX_F_TBT;
4242                 case AUX_CH_G:
4243                         return POWER_DOMAIN_AUX_G_TBT;
4244                 case AUX_CH_H:
4245                         return POWER_DOMAIN_AUX_H_TBT;
4246                 case AUX_CH_I:
4247                         return POWER_DOMAIN_AUX_I_TBT;
4248                 default:
4249                         MISSING_CASE(dig_port->aux_ch);
4250                         return POWER_DOMAIN_AUX_C_TBT;
4251                 }
4252         }
4253
4254         return intel_legacy_aux_to_power_domain(dig_port->aux_ch);
4255 }
4256
4257 /*
4258  * Converts aux_ch to power_domain without caring about TBT ports for that use
4259  * intel_aux_power_domain()
4260  */
4261 enum intel_display_power_domain
4262 intel_legacy_aux_to_power_domain(enum aux_ch aux_ch)
4263 {
4264         switch (aux_ch) {
4265         case AUX_CH_A:
4266                 return POWER_DOMAIN_AUX_A;
4267         case AUX_CH_B:
4268                 return POWER_DOMAIN_AUX_B;
4269         case AUX_CH_C:
4270                 return POWER_DOMAIN_AUX_C;
4271         case AUX_CH_D:
4272                 return POWER_DOMAIN_AUX_D;
4273         case AUX_CH_E:
4274                 return POWER_DOMAIN_AUX_E;
4275         case AUX_CH_F:
4276                 return POWER_DOMAIN_AUX_F;
4277         case AUX_CH_G:
4278                 return POWER_DOMAIN_AUX_G;
4279         case AUX_CH_H:
4280                 return POWER_DOMAIN_AUX_H;
4281         case AUX_CH_I:
4282                 return POWER_DOMAIN_AUX_I;
4283         default:
4284                 MISSING_CASE(aux_ch);
4285                 return POWER_DOMAIN_AUX_A;
4286         }
4287 }
4288
4289 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
4290 {
4291         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4292         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4293         struct drm_encoder *encoder;
4294         enum pipe pipe = crtc->pipe;
4295         u64 mask;
4296         enum transcoder transcoder = crtc_state->cpu_transcoder;
4297
4298         if (!crtc_state->hw.active)
4299                 return 0;
4300
4301         mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
4302         mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
4303         if (crtc_state->pch_pfit.enabled ||
4304             crtc_state->pch_pfit.force_thru)
4305                 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4306
4307         drm_for_each_encoder_mask(encoder, &dev_priv->drm,
4308                                   crtc_state->uapi.encoder_mask) {
4309                 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
4310
4311                 mask |= BIT_ULL(intel_encoder->power_domain);
4312         }
4313
4314         if (HAS_DDI(dev_priv) && crtc_state->has_audio)
4315                 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
4316
4317         if (crtc_state->shared_dpll)
4318                 mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
4319
4320         if (crtc_state->dsc.compression_enable)
4321                 mask |= BIT_ULL(intel_dsc_power_domain(crtc_state));
4322
4323         return mask;
4324 }
4325
4326 static u64
4327 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
4328 {
4329         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4330         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4331         enum intel_display_power_domain domain;
4332         u64 domains, new_domains, old_domains;
4333
4334         domains = get_crtc_power_domains(crtc_state);
4335
4336         new_domains = domains & ~crtc->enabled_power_domains.mask;
4337         old_domains = crtc->enabled_power_domains.mask & ~domains;
4338
4339         for_each_power_domain(domain, new_domains)
4340                 intel_display_power_get_in_set(dev_priv,
4341                                                &crtc->enabled_power_domains,
4342                                                domain);
4343
4344         return old_domains;
4345 }
4346
4347 static void modeset_put_crtc_power_domains(struct intel_crtc *crtc,
4348                                            u64 domains)
4349 {
4350         intel_display_power_put_mask_in_set(to_i915(crtc->base.dev),
4351                                             &crtc->enabled_power_domains,
4352                                             domains);
4353 }
4354
4355 static void valleyview_crtc_enable(struct intel_atomic_state *state,
4356                                    struct intel_crtc *crtc)
4357 {
4358         const struct intel_crtc_state *new_crtc_state =
4359                 intel_atomic_get_new_crtc_state(state, crtc);
4360         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4361         enum pipe pipe = crtc->pipe;
4362
4363         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
4364                 return;
4365
4366         if (intel_crtc_has_dp_encoder(new_crtc_state))
4367                 intel_dp_set_m_n(new_crtc_state, M1_N1);
4368
4369         intel_set_transcoder_timings(new_crtc_state);
4370         intel_set_pipe_src_size(new_crtc_state);
4371
4372         if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
4373                 intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
4374                 intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
4375         }
4376
4377         i9xx_set_pipeconf(new_crtc_state);
4378
4379         crtc->active = true;
4380
4381         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4382
4383         intel_encoders_pre_pll_enable(state, crtc);
4384
4385         if (IS_CHERRYVIEW(dev_priv)) {
4386                 chv_prepare_pll(crtc, new_crtc_state);
4387                 chv_enable_pll(crtc, new_crtc_state);
4388         } else {
4389                 vlv_prepare_pll(crtc, new_crtc_state);
4390                 vlv_enable_pll(crtc, new_crtc_state);
4391         }
4392
4393         intel_encoders_pre_enable(state, crtc);
4394
4395         i9xx_pfit_enable(new_crtc_state);
4396
4397         intel_color_load_luts(new_crtc_state);
4398         intel_color_commit(new_crtc_state);
4399         /* update DSPCNTR to configure gamma for pipe bottom color */
4400         intel_disable_primary_plane(new_crtc_state);
4401
4402         dev_priv->display.initial_watermarks(state, crtc);
4403         intel_enable_pipe(new_crtc_state);
4404
4405         intel_crtc_vblank_on(new_crtc_state);
4406
4407         intel_encoders_enable(state, crtc);
4408 }
4409
4410 static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
4411 {
4412         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4413         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4414
4415         intel_de_write(dev_priv, FP0(crtc->pipe),
4416                        crtc_state->dpll_hw_state.fp0);
4417         intel_de_write(dev_priv, FP1(crtc->pipe),
4418                        crtc_state->dpll_hw_state.fp1);
4419 }
4420
4421 static void i9xx_crtc_enable(struct intel_atomic_state *state,
4422                              struct intel_crtc *crtc)
4423 {
4424         const struct intel_crtc_state *new_crtc_state =
4425                 intel_atomic_get_new_crtc_state(state, crtc);
4426         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4427         enum pipe pipe = crtc->pipe;
4428
4429         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
4430                 return;
4431
4432         i9xx_set_pll_dividers(new_crtc_state);
4433
4434         if (intel_crtc_has_dp_encoder(new_crtc_state))
4435                 intel_dp_set_m_n(new_crtc_state, M1_N1);
4436
4437         intel_set_transcoder_timings(new_crtc_state);
4438         intel_set_pipe_src_size(new_crtc_state);
4439
4440         i9xx_set_pipeconf(new_crtc_state);
4441
4442         crtc->active = true;
4443
4444         if (!IS_GEN(dev_priv, 2))
4445                 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4446
4447         intel_encoders_pre_enable(state, crtc);
4448
4449         i9xx_enable_pll(crtc, new_crtc_state);
4450
4451         i9xx_pfit_enable(new_crtc_state);
4452
4453         intel_color_load_luts(new_crtc_state);
4454         intel_color_commit(new_crtc_state);
4455         /* update DSPCNTR to configure gamma for pipe bottom color */
4456         intel_disable_primary_plane(new_crtc_state);
4457
4458         if (dev_priv->display.initial_watermarks)
4459                 dev_priv->display.initial_watermarks(state, crtc);
4460         else
4461                 intel_update_watermarks(crtc);
4462         intel_enable_pipe(new_crtc_state);
4463
4464         intel_crtc_vblank_on(new_crtc_state);
4465
4466         intel_encoders_enable(state, crtc);
4467
4468         /* prevents spurious underruns */
4469         if (IS_GEN(dev_priv, 2))
4470                 intel_wait_for_vblank(dev_priv, pipe);
4471 }
4472
4473 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
4474 {
4475         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
4476         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4477
4478         if (!old_crtc_state->gmch_pfit.control)
4479                 return;
4480
4481         assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
4482
4483         drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
4484                     intel_de_read(dev_priv, PFIT_CONTROL));
4485         intel_de_write(dev_priv, PFIT_CONTROL, 0);
4486 }
4487
4488 static void i9xx_crtc_disable(struct intel_atomic_state *state,
4489                               struct intel_crtc *crtc)
4490 {
4491         struct intel_crtc_state *old_crtc_state =
4492                 intel_atomic_get_old_crtc_state(state, crtc);
4493         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4494         enum pipe pipe = crtc->pipe;
4495
4496         /*
4497          * On gen2 planes are double buffered but the pipe isn't, so we must
4498          * wait for planes to fully turn off before disabling the pipe.
4499          */
4500         if (IS_GEN(dev_priv, 2))
4501                 intel_wait_for_vblank(dev_priv, pipe);
4502
4503         intel_encoders_disable(state, crtc);
4504
4505         intel_crtc_vblank_off(old_crtc_state);
4506
4507         intel_disable_pipe(old_crtc_state);
4508
4509         i9xx_pfit_disable(old_crtc_state);
4510
4511         intel_encoders_post_disable(state, crtc);
4512
4513         if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
4514                 if (IS_CHERRYVIEW(dev_priv))
4515                         chv_disable_pll(dev_priv, pipe);
4516                 else if (IS_VALLEYVIEW(dev_priv))
4517                         vlv_disable_pll(dev_priv, pipe);
4518                 else
4519                         i9xx_disable_pll(old_crtc_state);
4520         }
4521
4522         intel_encoders_post_pll_disable(state, crtc);
4523
4524         if (!IS_GEN(dev_priv, 2))
4525                 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4526
4527         if (!dev_priv->display.initial_watermarks)
4528                 intel_update_watermarks(crtc);
4529
4530         /* clock the pipe down to 640x480@60 to potentially save power */
4531         if (IS_I830(dev_priv))
4532                 i830_enable_pipe(dev_priv, pipe);
4533 }
4534
4535 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
4536                                         struct drm_modeset_acquire_ctx *ctx)
4537 {
4538         struct intel_encoder *encoder;
4539         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4540         struct intel_bw_state *bw_state =
4541                 to_intel_bw_state(dev_priv->bw_obj.state);
4542         struct intel_cdclk_state *cdclk_state =
4543                 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
4544         struct intel_dbuf_state *dbuf_state =
4545                 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
4546         struct intel_crtc_state *crtc_state =
4547                 to_intel_crtc_state(crtc->base.state);
4548         struct intel_plane *plane;
4549         struct drm_atomic_state *state;
4550         struct intel_crtc_state *temp_crtc_state;
4551         enum pipe pipe = crtc->pipe;
4552         int ret;
4553
4554         if (!crtc_state->hw.active)
4555                 return;
4556
4557         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
4558                 const struct intel_plane_state *plane_state =
4559                         to_intel_plane_state(plane->base.state);
4560
4561                 if (plane_state->uapi.visible)
4562                         intel_plane_disable_noatomic(crtc, plane);
4563         }
4564
4565         state = drm_atomic_state_alloc(&dev_priv->drm);
4566         if (!state) {
4567                 drm_dbg_kms(&dev_priv->drm,
4568                             "failed to disable [CRTC:%d:%s], out of memory",
4569                             crtc->base.base.id, crtc->base.name);
4570                 return;
4571         }
4572
4573         state->acquire_ctx = ctx;
4574
4575         /* Everything's already locked, -EDEADLK can't happen. */
4576         temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
4577         ret = drm_atomic_add_affected_connectors(state, &crtc->base);
4578
4579         drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
4580
4581         dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
4582
4583         drm_atomic_state_put(state);
4584
4585         drm_dbg_kms(&dev_priv->drm,
4586                     "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
4587                     crtc->base.base.id, crtc->base.name);
4588
4589         crtc->active = false;
4590         crtc->base.enabled = false;
4591
4592         drm_WARN_ON(&dev_priv->drm,
4593                     drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
4594         crtc_state->uapi.active = false;
4595         crtc_state->uapi.connector_mask = 0;
4596         crtc_state->uapi.encoder_mask = 0;
4597         intel_crtc_free_hw_state(crtc_state);
4598         memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
4599
4600         for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
4601                 encoder->base.crtc = NULL;
4602
4603         intel_fbc_disable(crtc);
4604         intel_update_watermarks(crtc);
4605         intel_disable_shared_dpll(crtc_state);
4606
4607         intel_display_power_put_all_in_set(dev_priv, &crtc->enabled_power_domains);
4608
4609         dev_priv->active_pipes &= ~BIT(pipe);
4610         cdclk_state->min_cdclk[pipe] = 0;
4611         cdclk_state->min_voltage_level[pipe] = 0;
4612         cdclk_state->active_pipes &= ~BIT(pipe);
4613
4614         dbuf_state->active_pipes &= ~BIT(pipe);
4615
4616         bw_state->data_rate[pipe] = 0;
4617         bw_state->num_active_planes[pipe] = 0;
4618 }
4619
4620 /*
4621  * turn all crtc's off, but do not adjust state
4622  * This has to be paired with a call to intel_modeset_setup_hw_state.
4623  */
4624 int intel_display_suspend(struct drm_device *dev)
4625 {
4626         struct drm_i915_private *dev_priv = to_i915(dev);
4627         struct drm_atomic_state *state;
4628         int ret;
4629
4630         state = drm_atomic_helper_suspend(dev);
4631         ret = PTR_ERR_OR_ZERO(state);
4632         if (ret)
4633                 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
4634                         ret);
4635         else
4636                 dev_priv->modeset_restore_state = state;
4637         return ret;
4638 }
4639
4640 void intel_encoder_destroy(struct drm_encoder *encoder)
4641 {
4642         struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
4643
4644         drm_encoder_cleanup(encoder);
4645         kfree(intel_encoder);
4646 }
4647
4648 /* Cross check the actual hw state with our own modeset state tracking (and it's
4649  * internal consistency). */
4650 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
4651                                          struct drm_connector_state *conn_state)
4652 {
4653         struct intel_connector *connector = to_intel_connector(conn_state->connector);
4654         struct drm_i915_private *i915 = to_i915(connector->base.dev);
4655
4656         drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
4657                     connector->base.base.id, connector->base.name);
4658
4659         if (connector->get_hw_state(connector)) {
4660                 struct intel_encoder *encoder = intel_attached_encoder(connector);
4661
4662                 I915_STATE_WARN(!crtc_state,
4663                          "connector enabled without attached crtc\n");
4664
4665                 if (!crtc_state)
4666                         return;
4667
4668                 I915_STATE_WARN(!crtc_state->hw.active,
4669                                 "connector is active, but attached crtc isn't\n");
4670
4671                 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
4672                         return;
4673
4674                 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
4675                         "atomic encoder doesn't match attached encoder\n");
4676
4677                 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
4678                         "attached encoder crtc differs from connector crtc\n");
4679         } else {
4680                 I915_STATE_WARN(crtc_state && crtc_state->hw.active,
4681                                 "attached crtc is active, but connector isn't\n");
4682                 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
4683                         "best encoder set without crtc!\n");
4684         }
4685 }
4686
4687 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
4688 {
4689         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4690         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4691
4692         /* IPS only exists on ULT machines and is tied to pipe A. */
4693         if (!hsw_crtc_supports_ips(crtc))
4694                 return false;
4695
4696         if (!dev_priv->params.enable_ips)
4697                 return false;
4698
4699         if (crtc_state->pipe_bpp > 24)
4700                 return false;
4701
4702         /*
4703          * We compare against max which means we must take
4704          * the increased cdclk requirement into account when
4705          * calculating the new cdclk.
4706          *
4707          * Should measure whether using a lower cdclk w/o IPS
4708          */
4709         if (IS_BROADWELL(dev_priv) &&
4710             crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
4711                 return false;
4712
4713         return true;
4714 }
4715
4716 static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
4717 {
4718         struct drm_i915_private *dev_priv =
4719                 to_i915(crtc_state->uapi.crtc->dev);
4720         struct intel_atomic_state *state =
4721                 to_intel_atomic_state(crtc_state->uapi.state);
4722
4723         crtc_state->ips_enabled = false;
4724
4725         if (!hsw_crtc_state_ips_capable(crtc_state))
4726                 return 0;
4727
4728         /*
4729          * When IPS gets enabled, the pipe CRC changes. Since IPS gets
4730          * enabled and disabled dynamically based on package C states,
4731          * user space can't make reliable use of the CRCs, so let's just
4732          * completely disable it.
4733          */
4734         if (crtc_state->crc_enabled)
4735                 return 0;
4736
4737         /* IPS should be fine as long as at least one plane is enabled. */
4738         if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
4739                 return 0;
4740
4741         if (IS_BROADWELL(dev_priv)) {
4742                 const struct intel_cdclk_state *cdclk_state;
4743
4744                 cdclk_state = intel_atomic_get_cdclk_state(state);
4745                 if (IS_ERR(cdclk_state))
4746                         return PTR_ERR(cdclk_state);
4747
4748                 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
4749                 if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
4750                         return 0;
4751         }
4752
4753         crtc_state->ips_enabled = true;
4754
4755         return 0;
4756 }
4757
4758 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
4759 {
4760         const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4761
4762         /* GDG double wide on either pipe, otherwise pipe A only */
4763         return INTEL_GEN(dev_priv) < 4 &&
4764                 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
4765 }
4766
4767 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
4768 {
4769         u32 pixel_rate = crtc_state->hw.pipe_mode.crtc_clock;
4770         unsigned int pipe_w, pipe_h, pfit_w, pfit_h;
4771
4772         /*
4773          * We only use IF-ID interlacing. If we ever use
4774          * PF-ID we'll need to adjust the pixel_rate here.
4775          */
4776
4777         if (!crtc_state->pch_pfit.enabled)
4778                 return pixel_rate;
4779
4780         pipe_w = crtc_state->pipe_src_w;
4781         pipe_h = crtc_state->pipe_src_h;
4782
4783         pfit_w = drm_rect_width(&crtc_state->pch_pfit.dst);
4784         pfit_h = drm_rect_height(&crtc_state->pch_pfit.dst);
4785
4786         if (pipe_w < pfit_w)
4787                 pipe_w = pfit_w;
4788         if (pipe_h < pfit_h)
4789                 pipe_h = pfit_h;
4790
4791         if (drm_WARN_ON(crtc_state->uapi.crtc->dev,
4792                         !pfit_w || !pfit_h))
4793                 return pixel_rate;
4794
4795         return div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
4796                        pfit_w * pfit_h);
4797 }
4798
4799 static void intel_mode_from_crtc_timings(struct drm_display_mode *mode,
4800                                          const struct drm_display_mode *timings)
4801 {
4802         mode->hdisplay = timings->crtc_hdisplay;
4803         mode->htotal = timings->crtc_htotal;
4804         mode->hsync_start = timings->crtc_hsync_start;
4805         mode->hsync_end = timings->crtc_hsync_end;
4806
4807         mode->vdisplay = timings->crtc_vdisplay;
4808         mode->vtotal = timings->crtc_vtotal;
4809         mode->vsync_start = timings->crtc_vsync_start;
4810         mode->vsync_end = timings->crtc_vsync_end;
4811
4812         mode->flags = timings->flags;
4813         mode->type = DRM_MODE_TYPE_DRIVER;
4814
4815         mode->clock = timings->crtc_clock;
4816
4817         drm_mode_set_name(mode);
4818 }
4819
4820 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
4821 {
4822         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4823
4824         if (HAS_GMCH(dev_priv))
4825                 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
4826                 crtc_state->pixel_rate =
4827                         crtc_state->hw.pipe_mode.crtc_clock;
4828         else
4829                 crtc_state->pixel_rate =
4830                         ilk_pipe_pixel_rate(crtc_state);
4831 }
4832
4833 static void intel_crtc_readout_derived_state(struct intel_crtc_state *crtc_state)
4834 {
4835         struct drm_display_mode *mode = &crtc_state->hw.mode;
4836         struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4837         struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
4838
4839         drm_mode_copy(pipe_mode, adjusted_mode);
4840
4841         if (crtc_state->bigjoiner) {
4842                 /*
4843                  * transcoder is programmed to the full mode,
4844                  * but pipe timings are half of the transcoder mode
4845                  */
4846                 pipe_mode->crtc_hdisplay /= 2;
4847                 pipe_mode->crtc_hblank_start /= 2;
4848                 pipe_mode->crtc_hblank_end /= 2;
4849                 pipe_mode->crtc_hsync_start /= 2;
4850                 pipe_mode->crtc_hsync_end /= 2;
4851                 pipe_mode->crtc_htotal /= 2;
4852                 pipe_mode->crtc_clock /= 2;
4853         }
4854
4855         if (crtc_state->splitter.enable) {
4856                 int n = crtc_state->splitter.link_count;
4857                 int overlap = crtc_state->splitter.pixel_overlap;
4858
4859                 /*
4860                  * eDP MSO uses segment timings from EDID for transcoder
4861                  * timings, but full mode for everything else.
4862                  *
4863                  * h_full = (h_segment - pixel_overlap) * link_count
4864                  */
4865                 pipe_mode->crtc_hdisplay = (pipe_mode->crtc_hdisplay - overlap) * n;
4866                 pipe_mode->crtc_hblank_start = (pipe_mode->crtc_hblank_start - overlap) * n;
4867                 pipe_mode->crtc_hblank_end = (pipe_mode->crtc_hblank_end - overlap) * n;
4868                 pipe_mode->crtc_hsync_start = (pipe_mode->crtc_hsync_start - overlap) * n;
4869                 pipe_mode->crtc_hsync_end = (pipe_mode->crtc_hsync_end - overlap) * n;
4870                 pipe_mode->crtc_htotal = (pipe_mode->crtc_htotal - overlap) * n;
4871                 pipe_mode->crtc_clock *= n;
4872
4873                 intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
4874                 intel_mode_from_crtc_timings(adjusted_mode, pipe_mode);
4875         } else {
4876                 intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
4877                 intel_mode_from_crtc_timings(adjusted_mode, adjusted_mode);
4878         }
4879
4880         intel_crtc_compute_pixel_rate(crtc_state);
4881
4882         drm_mode_copy(mode, adjusted_mode);
4883         mode->hdisplay = crtc_state->pipe_src_w << crtc_state->bigjoiner;
4884         mode->vdisplay = crtc_state->pipe_src_h;
4885 }
4886
4887 static void intel_encoder_get_config(struct intel_encoder *encoder,
4888                                      struct intel_crtc_state *crtc_state)
4889 {
4890         encoder->get_config(encoder, crtc_state);
4891
4892         intel_crtc_readout_derived_state(crtc_state);
4893 }
4894
4895 static int intel_crtc_compute_config(struct intel_crtc *crtc,
4896                                      struct intel_crtc_state *pipe_config)
4897 {
4898         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4899         struct drm_display_mode *pipe_mode = &pipe_config->hw.pipe_mode;
4900         int clock_limit = dev_priv->max_dotclk_freq;
4901
4902         drm_mode_copy(pipe_mode, &pipe_config->hw.adjusted_mode);
4903
4904         /* Adjust pipe_mode for bigjoiner, with half the horizontal mode */
4905         if (pipe_config->bigjoiner) {
4906                 pipe_mode->crtc_clock /= 2;
4907                 pipe_mode->crtc_hdisplay /= 2;
4908                 pipe_mode->crtc_hblank_start /= 2;
4909                 pipe_mode->crtc_hblank_end /= 2;
4910                 pipe_mode->crtc_hsync_start /= 2;
4911                 pipe_mode->crtc_hsync_end /= 2;
4912                 pipe_mode->crtc_htotal /= 2;
4913                 pipe_config->pipe_src_w /= 2;
4914         }
4915
4916         if (pipe_config->splitter.enable) {
4917                 int n = pipe_config->splitter.link_count;
4918                 int overlap = pipe_config->splitter.pixel_overlap;
4919
4920                 pipe_mode->crtc_hdisplay = (pipe_mode->crtc_hdisplay - overlap) * n;
4921                 pipe_mode->crtc_hblank_start = (pipe_mode->crtc_hblank_start - overlap) * n;
4922                 pipe_mode->crtc_hblank_end = (pipe_mode->crtc_hblank_end - overlap) * n;
4923                 pipe_mode->crtc_hsync_start = (pipe_mode->crtc_hsync_start - overlap) * n;
4924                 pipe_mode->crtc_hsync_end = (pipe_mode->crtc_hsync_end - overlap) * n;
4925                 pipe_mode->crtc_htotal = (pipe_mode->crtc_htotal - overlap) * n;
4926                 pipe_mode->crtc_clock *= n;
4927         }
4928
4929         intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
4930
4931         if (INTEL_GEN(dev_priv) < 4) {
4932                 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
4933
4934                 /*
4935                  * Enable double wide mode when the dot clock
4936                  * is > 90% of the (display) core speed.
4937                  */
4938                 if (intel_crtc_supports_double_wide(crtc) &&
4939                     pipe_mode->crtc_clock > clock_limit) {
4940                         clock_limit = dev_priv->max_dotclk_freq;
4941                         pipe_config->double_wide = true;
4942                 }
4943         }
4944
4945         if (pipe_mode->crtc_clock > clock_limit) {
4946                 drm_dbg_kms(&dev_priv->drm,
4947                             "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
4948                             pipe_mode->crtc_clock, clock_limit,
4949                             yesno(pipe_config->double_wide));
4950                 return -EINVAL;
4951         }
4952
4953         /*
4954          * Pipe horizontal size must be even in:
4955          * - DVO ganged mode
4956          * - LVDS dual channel mode
4957          * - Double wide pipe
4958          */
4959         if (pipe_config->pipe_src_w & 1) {
4960                 if (pipe_config->double_wide) {
4961                         drm_dbg_kms(&dev_priv->drm,
4962                                     "Odd pipe source width not supported with double wide pipe\n");
4963                         return -EINVAL;
4964                 }
4965
4966                 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
4967                     intel_is_dual_link_lvds(dev_priv)) {
4968                         drm_dbg_kms(&dev_priv->drm,
4969                                     "Odd pipe source width not supported with dual link LVDS\n");
4970                         return -EINVAL;
4971                 }
4972         }
4973
4974         /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4975          * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
4976          */
4977         if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
4978             pipe_mode->crtc_hsync_start == pipe_mode->crtc_hdisplay)
4979                 return -EINVAL;
4980
4981         intel_crtc_compute_pixel_rate(pipe_config);
4982
4983         if (pipe_config->has_pch_encoder)
4984                 return ilk_fdi_compute_config(crtc, pipe_config);
4985
4986         return 0;
4987 }
4988
4989 static void
4990 intel_reduce_m_n_ratio(u32 *num, u32 *den)
4991 {
4992         while (*num > DATA_LINK_M_N_MASK ||
4993                *den > DATA_LINK_M_N_MASK) {
4994                 *num >>= 1;
4995                 *den >>= 1;
4996         }
4997 }
4998
4999 static void compute_m_n(unsigned int m, unsigned int n,
5000                         u32 *ret_m, u32 *ret_n,
5001                         bool constant_n)
5002 {
5003         /*
5004          * Several DP dongles in particular seem to be fussy about
5005          * too large link M/N values. Give N value as 0x8000 that
5006          * should be acceptable by specific devices. 0x8000 is the
5007          * specified fixed N value for asynchronous clock mode,
5008          * which the devices expect also in synchronous clock mode.
5009          */
5010         if (constant_n)
5011                 *ret_n = DP_LINK_CONSTANT_N_VALUE;
5012         else
5013                 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5014
5015         *ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
5016         intel_reduce_m_n_ratio(ret_m, ret_n);
5017 }
5018
5019 void
5020 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
5021                        int pixel_clock, int link_clock,
5022                        struct intel_link_m_n *m_n,
5023                        bool constant_n, bool fec_enable)
5024 {
5025         u32 data_clock = bits_per_pixel * pixel_clock;
5026
5027         if (fec_enable)
5028                 data_clock = intel_dp_mode_to_fec_clock(data_clock);
5029
5030         m_n->tu = 64;
5031         compute_m_n(data_clock,
5032                     link_clock * nlanes * 8,
5033                     &m_n->gmch_m, &m_n->gmch_n,
5034                     constant_n);
5035
5036         compute_m_n(pixel_clock, link_clock,
5037                     &m_n->link_m, &m_n->link_n,
5038                     constant_n);
5039 }
5040
5041 static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
5042 {
5043         /*
5044          * There may be no VBT; and if the BIOS enabled SSC we can
5045          * just keep using it to avoid unnecessary flicker.  Whereas if the
5046          * BIOS isn't using it, don't assume it will work even if the VBT
5047          * indicates as much.
5048          */
5049         if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
5050                 bool bios_lvds_use_ssc = intel_de_read(dev_priv,
5051                                                        PCH_DREF_CONTROL) &
5052                         DREF_SSC1_ENABLE;
5053
5054                 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
5055                         drm_dbg_kms(&dev_priv->drm,
5056                                     "SSC %s by BIOS, overriding VBT which says %s\n",
5057                                     enableddisabled(bios_lvds_use_ssc),
5058                                     enableddisabled(dev_priv->vbt.lvds_use_ssc));
5059                         dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
5060                 }
5061         }
5062 }
5063
5064 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
5065                                          const struct intel_link_m_n *m_n)
5066 {
5067         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5068         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5069         enum pipe pipe = crtc->pipe;
5070
5071         intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
5072                        TU_SIZE(m_n->tu) | m_n->gmch_m);
5073         intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5074         intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5075         intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
5076 }
5077
5078 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
5079                                  enum transcoder transcoder)
5080 {
5081         if (IS_HASWELL(dev_priv))
5082                 return transcoder == TRANSCODER_EDP;
5083
5084         /*
5085          * Strictly speaking some registers are available before
5086          * gen7, but we only support DRRS on gen7+
5087          */
5088         return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
5089 }
5090
5091 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
5092                                          const struct intel_link_m_n *m_n,
5093                                          const struct intel_link_m_n *m2_n2)
5094 {
5095         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5096         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5097         enum pipe pipe = crtc->pipe;
5098         enum transcoder transcoder = crtc_state->cpu_transcoder;
5099
5100         if (INTEL_GEN(dev_priv) >= 5) {
5101                 intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
5102                                TU_SIZE(m_n->tu) | m_n->gmch_m);
5103                 intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
5104                                m_n->gmch_n);
5105                 intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
5106                                m_n->link_m);
5107                 intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
5108                                m_n->link_n);
5109                 /*
5110                  *  M2_N2 registers are set only if DRRS is supported
5111                  * (to make sure the registers are not unnecessarily accessed).
5112                  */
5113                 if (m2_n2 && crtc_state->has_drrs &&
5114                     transcoder_has_m2_n2(dev_priv, transcoder)) {
5115                         intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
5116                                        TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5117                         intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
5118                                        m2_n2->gmch_n);
5119                         intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
5120                                        m2_n2->link_m);
5121                         intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
5122                                        m2_n2->link_n);
5123                 }
5124         } else {
5125                 intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
5126                                TU_SIZE(m_n->tu) | m_n->gmch_m);
5127                 intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5128                 intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
5129                 intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
5130         }
5131 }
5132
5133 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
5134 {
5135         const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
5136         struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
5137
5138         if (m_n == M1_N1) {
5139                 dp_m_n = &crtc_state->dp_m_n;
5140                 dp_m2_n2 = &crtc_state->dp_m2_n2;
5141         } else if (m_n == M2_N2) {
5142
5143                 /*
5144                  * M2_N2 registers are not supported. Hence m2_n2 divider value
5145                  * needs to be programmed into M1_N1.
5146                  */
5147                 dp_m_n = &crtc_state->dp_m2_n2;
5148         } else {
5149                 drm_err(&i915->drm, "Unsupported divider value\n");
5150                 return;
5151         }
5152
5153         if (crtc_state->has_pch_encoder)
5154                 intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
5155         else
5156                 intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
5157 }
5158
5159 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
5160 {
5161         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5162         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5163         enum pipe pipe = crtc->pipe;
5164         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5165         const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
5166         u32 crtc_vtotal, crtc_vblank_end;
5167         int vsyncshift = 0;
5168
5169         /* We need to be careful not to changed the adjusted mode, for otherwise
5170          * the hw state checker will get angry at the mismatch. */
5171         crtc_vtotal = adjusted_mode->crtc_vtotal;
5172         crtc_vblank_end = adjusted_mode->crtc_vblank_end;
5173
5174         if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5175                 /* the chip adds 2 halflines automatically */
5176                 crtc_vtotal -= 1;
5177                 crtc_vblank_end -= 1;
5178
5179                 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
5180                         vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5181                 else
5182                         vsyncshift = adjusted_mode->crtc_hsync_start -
5183                                 adjusted_mode->crtc_htotal / 2;
5184                 if (vsyncshift < 0)
5185                         vsyncshift += adjusted_mode->crtc_htotal;
5186         }
5187
5188         if (INTEL_GEN(dev_priv) > 3)
5189                 intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
5190                                vsyncshift);
5191
5192         intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
5193                        (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
5194         intel_de_write(dev_priv, HBLANK(cpu_transcoder),
5195                        (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
5196         intel_de_write(dev_priv, HSYNC(cpu_transcoder),
5197                        (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
5198
5199         intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
5200                        (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
5201         intel_de_write(dev_priv, VBLANK(cpu_transcoder),
5202                        (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
5203         intel_de_write(dev_priv, VSYNC(cpu_transcoder),
5204                        (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
5205
5206         /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5207          * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5208          * documented on the DDI_FUNC_CTL register description, EDP Input Select
5209          * bits. */
5210         if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
5211             (pipe == PIPE_B || pipe == PIPE_C))
5212                 intel_de_write(dev_priv, VTOTAL(pipe),
5213                                intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
5214
5215 }
5216
5217 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
5218 {
5219         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5220         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5221         enum pipe pipe = crtc->pipe;
5222
5223         /* pipesrc controls the size that is scaled from, which should
5224          * always be the user's requested size.
5225          */
5226         intel_de_write(dev_priv, PIPESRC(pipe),
5227                        ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
5228 }
5229
5230 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
5231 {
5232         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5233         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5234
5235         if (IS_GEN(dev_priv, 2))
5236                 return false;
5237
5238         if (INTEL_GEN(dev_priv) >= 9 ||
5239             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
5240                 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
5241         else
5242                 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
5243 }
5244
5245 static void intel_get_transcoder_timings(struct intel_crtc *crtc,
5246                                          struct intel_crtc_state *pipe_config)
5247 {
5248         struct drm_device *dev = crtc->base.dev;
5249         struct drm_i915_private *dev_priv = to_i915(dev);
5250         enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5251         u32 tmp;
5252
5253         tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
5254         pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5255         pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5256
5257         if (!transcoder_is_dsi(cpu_transcoder)) {
5258                 tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
5259                 pipe_config->hw.adjusted_mode.crtc_hblank_start =
5260                                                         (tmp & 0xffff) + 1;
5261                 pipe_config->hw.adjusted_mode.crtc_hblank_end =
5262                                                 ((tmp >> 16) & 0xffff) + 1;
5263         }
5264         tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
5265         pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5266         pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5267
5268         tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
5269         pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5270         pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5271
5272         if (!transcoder_is_dsi(cpu_transcoder)) {
5273                 tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
5274                 pipe_config->hw.adjusted_mode.crtc_vblank_start =
5275                                                         (tmp & 0xffff) + 1;
5276                 pipe_config->hw.adjusted_mode.crtc_vblank_end =
5277                                                 ((tmp >> 16) & 0xffff) + 1;
5278         }
5279         tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
5280         pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5281         pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5282
5283         if (intel_pipe_is_interlaced(pipe_config)) {
5284                 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5285                 pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
5286                 pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
5287         }
5288 }
5289
5290 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
5291                                     struct intel_crtc_state *pipe_config)
5292 {
5293         struct drm_device *dev = crtc->base.dev;
5294         struct drm_i915_private *dev_priv = to_i915(dev);
5295         u32 tmp;
5296
5297         tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
5298         pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5299         pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5300 }
5301
5302 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
5303 {
5304         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5305         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5306         u32 pipeconf;
5307
5308         pipeconf = 0;
5309
5310         /* we keep both pipes enabled on 830 */
5311         if (IS_I830(dev_priv))
5312                 pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
5313
5314         if (crtc_state->double_wide)
5315                 pipeconf |= PIPECONF_DOUBLE_WIDE;
5316
5317         /* only g4x and later have fancy bpc/dither controls */
5318         if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
5319             IS_CHERRYVIEW(dev_priv)) {
5320                 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5321                 if (crtc_state->dither && crtc_state->pipe_bpp != 30)
5322                         pipeconf |= PIPECONF_DITHER_EN |
5323                                     PIPECONF_DITHER_TYPE_SP;
5324
5325                 switch (crtc_state->pipe_bpp) {
5326                 case 18:
5327                         pipeconf |= PIPECONF_6BPC;
5328                         break;
5329                 case 24:
5330                         pipeconf |= PIPECONF_8BPC;
5331                         break;
5332                 case 30:
5333                         pipeconf |= PIPECONF_10BPC;
5334                         break;
5335                 default:
5336                         /* Case prevented by intel_choose_pipe_bpp_dither. */
5337                         BUG();
5338                 }
5339         }
5340
5341         if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5342                 if (INTEL_GEN(dev_priv) < 4 ||
5343                     intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
5344                         pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5345                 else
5346                         pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5347         } else {
5348                 pipeconf |= PIPECONF_PROGRESSIVE;
5349         }
5350
5351         if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
5352              crtc_state->limited_color_range)
5353                 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
5354
5355         pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
5356
5357         pipeconf |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
5358
5359         intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
5360         intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
5361 }
5362
5363 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
5364 {
5365         if (IS_I830(dev_priv))
5366                 return false;
5367
5368         return INTEL_GEN(dev_priv) >= 4 ||
5369                 IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
5370 }
5371
5372 static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
5373 {
5374         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5375         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5376         u32 tmp;
5377
5378         if (!i9xx_has_pfit(dev_priv))
5379                 return;
5380
5381         tmp = intel_de_read(dev_priv, PFIT_CONTROL);
5382         if (!(tmp & PFIT_ENABLE))
5383                 return;
5384
5385         /* Check whether the pfit is attached to our pipe. */
5386         if (INTEL_GEN(dev_priv) < 4) {
5387                 if (crtc->pipe != PIPE_B)
5388                         return;
5389         } else {
5390                 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5391                         return;
5392         }
5393
5394         crtc_state->gmch_pfit.control = tmp;
5395         crtc_state->gmch_pfit.pgm_ratios =
5396                 intel_de_read(dev_priv, PFIT_PGM_RATIOS);
5397 }
5398
5399 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5400                                struct intel_crtc_state *pipe_config)
5401 {
5402         struct drm_device *dev = crtc->base.dev;
5403         struct drm_i915_private *dev_priv = to_i915(dev);
5404         enum pipe pipe = crtc->pipe;
5405         struct dpll clock;
5406         u32 mdiv;
5407         int refclk = 100000;
5408
5409         /* In case of DSI, DPLL will not be used */
5410         if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
5411                 return;
5412
5413         vlv_dpio_get(dev_priv);
5414         mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
5415         vlv_dpio_put(dev_priv);
5416
5417         clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5418         clock.m2 = mdiv & DPIO_M2DIV_MASK;
5419         clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5420         clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5421         clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5422
5423         pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
5424 }
5425
5426 static void chv_crtc_clock_get(struct intel_crtc *crtc,
5427                                struct intel_crtc_state *pipe_config)
5428 {
5429         struct drm_device *dev = crtc->base.dev;
5430         struct drm_i915_private *dev_priv = to_i915(dev);
5431         enum pipe pipe = crtc->pipe;
5432         enum dpio_channel port = vlv_pipe_to_channel(pipe);
5433         struct dpll clock;
5434         u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
5435         int refclk = 100000;
5436
5437         /* In case of DSI, DPLL will not be used */
5438         if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
5439                 return;
5440
5441         vlv_dpio_get(dev_priv);
5442         cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
5443         pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
5444         pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
5445         pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
5446         pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
5447         vlv_dpio_put(dev_priv);
5448
5449         clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
5450         clock.m2 = (pll_dw0 & 0xff) << 22;
5451         if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
5452                 clock.m2 |= pll_dw2 & 0x3fffff;
5453         clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
5454         clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
5455         clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
5456
5457         pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
5458 }
5459
5460 static enum intel_output_format
5461 bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
5462 {
5463         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5464         u32 tmp;
5465
5466         tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
5467
5468         if (tmp & PIPEMISC_YUV420_ENABLE) {
5469                 /* We support 4:2:0 in full blend mode only */
5470                 drm_WARN_ON(&dev_priv->drm,
5471                             (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
5472
5473                 return INTEL_OUTPUT_FORMAT_YCBCR420;
5474         } else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
5475                 return INTEL_OUTPUT_FORMAT_YCBCR444;
5476         } else {
5477                 return INTEL_OUTPUT_FORMAT_RGB;
5478         }
5479 }
5480
5481 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
5482 {
5483         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5484         struct intel_plane *plane = to_intel_plane(crtc->base.primary);
5485         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5486         enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
5487         u32 tmp;
5488
5489         tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
5490
5491         if (tmp & DISPPLANE_GAMMA_ENABLE)
5492                 crtc_state->gamma_enable = true;
5493
5494         if (!HAS_GMCH(dev_priv) &&
5495             tmp & DISPPLANE_PIPE_CSC_ENABLE)
5496                 crtc_state->csc_enable = true;
5497 }
5498
5499 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5500                                  struct intel_crtc_state *pipe_config)
5501 {
5502         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5503         enum intel_display_power_domain power_domain;
5504         intel_wakeref_t wakeref;
5505         u32 tmp;
5506         bool ret;
5507
5508         power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
5509         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
5510         if (!wakeref)
5511                 return false;
5512
5513         pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
5514         pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
5515         pipe_config->shared_dpll = NULL;
5516
5517         ret = false;
5518
5519         tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
5520         if (!(tmp & PIPECONF_ENABLE))
5521                 goto out;
5522
5523         if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
5524             IS_CHERRYVIEW(dev_priv)) {
5525                 switch (tmp & PIPECONF_BPC_MASK) {
5526                 case PIPECONF_6BPC:
5527                         pipe_config->pipe_bpp = 18;
5528                         break;
5529                 case PIPECONF_8BPC:
5530                         pipe_config->pipe_bpp = 24;
5531                         break;
5532                 case PIPECONF_10BPC:
5533                         pipe_config->pipe_bpp = 30;
5534                         break;
5535                 default:
5536                         break;
5537                 }
5538         }
5539
5540         if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
5541             (tmp & PIPECONF_COLOR_RANGE_SELECT))
5542                 pipe_config->limited_color_range = true;
5543
5544         pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
5545                 PIPECONF_GAMMA_MODE_SHIFT;
5546
5547         if (IS_CHERRYVIEW(dev_priv))
5548                 pipe_config->cgm_mode = intel_de_read(dev_priv,
5549                                                       CGM_PIPE_MODE(crtc->pipe));
5550
5551         i9xx_get_pipe_color_config(pipe_config);
5552         intel_color_get_config(pipe_config);
5553
5554         if (INTEL_GEN(dev_priv) < 4)
5555                 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5556
5557         intel_get_transcoder_timings(crtc, pipe_config);
5558         intel_get_pipe_src_size(crtc, pipe_config);
5559
5560         i9xx_get_pfit_config(pipe_config);
5561
5562         if (INTEL_GEN(dev_priv) >= 4) {
5563                 /* No way to read it out on pipes B and C */
5564                 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
5565                         tmp = dev_priv->chv_dpll_md[crtc->pipe];
5566                 else
5567                         tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
5568                 pipe_config->pixel_multiplier =
5569                         ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5570                          >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
5571                 pipe_config->dpll_hw_state.dpll_md = tmp;
5572         } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5573                    IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
5574                 tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
5575                 pipe_config->pixel_multiplier =
5576                         ((tmp & SDVO_MULTIPLIER_MASK)
5577                          >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5578         } else {
5579                 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5580                  * port and will be fixed up in the encoder->get_config
5581                  * function. */
5582                 pipe_config->pixel_multiplier = 1;
5583         }
5584         pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
5585                                                         DPLL(crtc->pipe));
5586         if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
5587                 pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
5588                                                                FP0(crtc->pipe));
5589                 pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
5590                                                                FP1(crtc->pipe));
5591         } else {
5592                 /* Mask out read-only status bits. */
5593                 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5594                                                      DPLL_PORTC_READY_MASK |
5595                                                      DPLL_PORTB_READY_MASK);
5596         }
5597
5598         if (IS_CHERRYVIEW(dev_priv))
5599                 chv_crtc_clock_get(crtc, pipe_config);
5600         else if (IS_VALLEYVIEW(dev_priv))
5601                 vlv_crtc_clock_get(crtc, pipe_config);
5602         else
5603                 i9xx_crtc_clock_get(crtc, pipe_config);
5604
5605         /*
5606          * Normally the dotclock is filled in by the encoder .get_config()
5607          * but in case the pipe is enabled w/o any ports we need a sane
5608          * default.
5609          */
5610         pipe_config->hw.adjusted_mode.crtc_clock =
5611                 pipe_config->port_clock / pipe_config->pixel_multiplier;
5612
5613         ret = true;
5614
5615 out:
5616         intel_display_power_put(dev_priv, power_domain, wakeref);
5617
5618         return ret;
5619 }
5620
5621 static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
5622 {
5623         struct intel_encoder *encoder;
5624         int i;
5625         u32 val, final;
5626         bool has_lvds = false;
5627         bool has_cpu_edp = false;
5628         bool has_panel = false;
5629         bool has_ck505 = false;
5630         bool can_ssc = false;
5631         bool using_ssc_source = false;
5632
5633         /* We need to take the global config into account */
5634         for_each_intel_encoder(&dev_priv->drm, encoder) {
5635                 switch (encoder->type) {
5636                 case INTEL_OUTPUT_LVDS:
5637                         has_panel = true;
5638                         has_lvds = true;
5639                         break;
5640                 case INTEL_OUTPUT_EDP:
5641                         has_panel = true;
5642                         if (encoder->port == PORT_A)
5643                                 has_cpu_edp = true;
5644                         break;
5645                 default:
5646                         break;
5647                 }
5648         }
5649
5650         if (HAS_PCH_IBX(dev_priv)) {
5651                 has_ck505 = dev_priv->vbt.display_clock_mode;
5652                 can_ssc = has_ck505;
5653         } else {
5654                 has_ck505 = false;
5655                 can_ssc = true;
5656         }
5657
5658         /* Check if any DPLLs are using the SSC source */
5659         for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
5660                 u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
5661
5662                 if (!(temp & DPLL_VCO_ENABLE))
5663                         continue;
5664
5665                 if ((temp & PLL_REF_INPUT_MASK) ==
5666                     PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5667                         using_ssc_source = true;
5668                         break;
5669                 }
5670         }
5671
5672         drm_dbg_kms(&dev_priv->drm,
5673                     "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
5674                     has_panel, has_lvds, has_ck505, using_ssc_source);
5675
5676         /* Ironlake: try to setup display ref clock before DPLL
5677          * enabling. This is only under driver's control after
5678          * PCH B stepping, previous chipset stepping should be
5679          * ignoring this setting.
5680          */
5681         val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
5682
5683         /* As we must carefully and slowly disable/enable each source in turn,
5684          * compute the final state we want first and check if we need to
5685          * make any changes at all.
5686          */
5687         final = val;
5688         final &= ~DREF_NONSPREAD_SOURCE_MASK;
5689         if (has_ck505)
5690                 final |= DREF_NONSPREAD_CK505_ENABLE;
5691         else
5692                 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5693
5694         final &= ~DREF_SSC_SOURCE_MASK;
5695         final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5696         final &= ~DREF_SSC1_ENABLE;
5697
5698         if (has_panel) {
5699                 final |= DREF_SSC_SOURCE_ENABLE;
5700
5701                 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5702                         final |= DREF_SSC1_ENABLE;
5703
5704                 if (has_cpu_edp) {
5705                         if (intel_panel_use_ssc(dev_priv) && can_ssc)
5706                                 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5707                         else
5708                                 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5709                 } else
5710                         final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5711         } else if (using_ssc_source) {
5712                 final |= DREF_SSC_SOURCE_ENABLE;
5713                 final |= DREF_SSC1_ENABLE;
5714         }
5715
5716         if (final == val)
5717                 return;
5718
5719         /* Always enable nonspread source */
5720         val &= ~DREF_NONSPREAD_SOURCE_MASK;
5721
5722         if (has_ck505)
5723                 val |= DREF_NONSPREAD_CK505_ENABLE;
5724         else
5725                 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5726
5727         if (has_panel) {
5728                 val &= ~DREF_SSC_SOURCE_MASK;
5729                 val |= DREF_SSC_SOURCE_ENABLE;
5730
5731                 /* SSC must be turned on before enabling the CPU output  */
5732                 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5733                         drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
5734                         val |= DREF_SSC1_ENABLE;
5735                 } else
5736                         val &= ~DREF_SSC1_ENABLE;
5737
5738                 /* Get SSC going before enabling the outputs */
5739                 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5740                 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5741                 udelay(200);
5742
5743                 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5744
5745                 /* Enable CPU source on CPU attached eDP */
5746                 if (has_cpu_edp) {
5747                         if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5748                                 drm_dbg_kms(&dev_priv->drm,
5749                                             "Using SSC on eDP\n");
5750                                 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5751                         } else
5752                                 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5753                 } else
5754                         val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5755
5756                 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5757                 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5758                 udelay(200);
5759         } else {
5760                 drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
5761
5762                 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5763
5764                 /* Turn off CPU output */
5765                 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5766
5767                 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5768                 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5769                 udelay(200);
5770
5771                 if (!using_ssc_source) {
5772                         drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
5773
5774                         /* Turn off the SSC source */
5775                         val &= ~DREF_SSC_SOURCE_MASK;
5776                         val |= DREF_SSC_SOURCE_DISABLE;
5777
5778                         /* Turn off SSC1 */
5779                         val &= ~DREF_SSC1_ENABLE;
5780
5781                         intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5782                         intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5783                         udelay(200);
5784                 }
5785         }
5786
5787         BUG_ON(val != final);
5788 }
5789
5790 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
5791 {
5792         u32 tmp;
5793
5794         tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
5795         tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5796         intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
5797
5798         if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
5799                         FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5800                 drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
5801
5802         tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
5803         tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5804         intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
5805
5806         if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
5807                          FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5808                 drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
5809 }
5810
5811 /* WaMPhyProgramming:hsw */
5812 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5813 {
5814         u32 tmp;
5815
5816         tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5817         tmp &= ~(0xFF << 24);
5818         tmp |= (0x12 << 24);
5819         intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5820
5821         tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5822         tmp |= (1 << 11);
5823         intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5824
5825         tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5826         tmp |= (1 << 11);
5827         intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5828
5829         tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5830         tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5831         intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5832
5833         tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5834         tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5835         intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5836
5837         tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5838         tmp &= ~(7 << 13);
5839         tmp |= (5 << 13);
5840         intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5841
5842         tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5843         tmp &= ~(7 << 13);
5844         tmp |= (5 << 13);
5845         intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5846
5847         tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5848         tmp &= ~0xFF;
5849         tmp |= 0x1C;
5850         intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5851
5852         tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5853         tmp &= ~0xFF;
5854         tmp |= 0x1C;
5855         intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5856
5857         tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5858         tmp &= ~(0xFF << 16);
5859         tmp |= (0x1C << 16);
5860         intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5861
5862         tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5863         tmp &= ~(0xFF << 16);
5864         tmp |= (0x1C << 16);
5865         intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5866
5867         tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5868         tmp |= (1 << 27);
5869         intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5870
5871         tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5872         tmp |= (1 << 27);
5873         intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5874
5875         tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5876         tmp &= ~(0xF << 28);
5877         tmp |= (4 << 28);
5878         intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5879
5880         tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5881         tmp &= ~(0xF << 28);
5882         tmp |= (4 << 28);
5883         intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5884 }
5885
5886 /* Implements 3 different sequences from BSpec chapter "Display iCLK
5887  * Programming" based on the parameters passed:
5888  * - Sequence to enable CLKOUT_DP
5889  * - Sequence to enable CLKOUT_DP without spread
5890  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5891  */
5892 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
5893                                  bool with_spread, bool with_fdi)
5894 {
5895         u32 reg, tmp;
5896
5897         if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
5898                      "FDI requires downspread\n"))
5899                 with_spread = true;
5900         if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
5901                      with_fdi, "LP PCH doesn't have FDI\n"))
5902                 with_fdi = false;
5903
5904         mutex_lock(&dev_priv->sb_lock);
5905
5906         tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5907         tmp &= ~SBI_SSCCTL_DISABLE;
5908         tmp |= SBI_SSCCTL_PATHALT;
5909         intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5910
5911         udelay(24);
5912
5913         if (with_spread) {
5914                 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5915                 tmp &= ~SBI_SSCCTL_PATHALT;
5916                 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5917
5918                 if (with_fdi) {
5919                         lpt_reset_fdi_mphy(dev_priv);
5920                         lpt_program_fdi_mphy(dev_priv);
5921                 }
5922         }
5923
5924         reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
5925         tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5926         tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5927         intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5928
5929         mutex_unlock(&dev_priv->sb_lock);
5930 }
5931
5932 /* Sequence to disable CLKOUT_DP */
5933 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
5934 {
5935         u32 reg, tmp;
5936
5937         mutex_lock(&dev_priv->sb_lock);
5938
5939         reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
5940         tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5941         tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5942         intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5943
5944         tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5945         if (!(tmp & SBI_SSCCTL_DISABLE)) {
5946                 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5947                         tmp |= SBI_SSCCTL_PATHALT;
5948                         intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5949                         udelay(32);
5950                 }
5951                 tmp |= SBI_SSCCTL_DISABLE;
5952                 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5953         }
5954
5955         mutex_unlock(&dev_priv->sb_lock);
5956 }
5957
5958 #define BEND_IDX(steps) ((50 + (steps)) / 5)
5959
5960 static const u16 sscdivintphase[] = {
5961         [BEND_IDX( 50)] = 0x3B23,
5962         [BEND_IDX( 45)] = 0x3B23,
5963         [BEND_IDX( 40)] = 0x3C23,
5964         [BEND_IDX( 35)] = 0x3C23,
5965         [BEND_IDX( 30)] = 0x3D23,
5966         [BEND_IDX( 25)] = 0x3D23,
5967         [BEND_IDX( 20)] = 0x3E23,
5968         [BEND_IDX( 15)] = 0x3E23,
5969         [BEND_IDX( 10)] = 0x3F23,
5970         [BEND_IDX(  5)] = 0x3F23,
5971         [BEND_IDX(  0)] = 0x0025,
5972         [BEND_IDX( -5)] = 0x0025,
5973         [BEND_IDX(-10)] = 0x0125,
5974         [BEND_IDX(-15)] = 0x0125,
5975         [BEND_IDX(-20)] = 0x0225,
5976         [BEND_IDX(-25)] = 0x0225,
5977         [BEND_IDX(-30)] = 0x0325,
5978         [BEND_IDX(-35)] = 0x0325,
5979         [BEND_IDX(-40)] = 0x0425,
5980         [BEND_IDX(-45)] = 0x0425,
5981         [BEND_IDX(-50)] = 0x0525,
5982 };
5983
5984 /*
5985  * Bend CLKOUT_DP
5986  * steps -50 to 50 inclusive, in steps of 5
5987  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
5988  * change in clock period = -(steps / 10) * 5.787 ps
5989  */
5990 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
5991 {
5992         u32 tmp;
5993         int idx = BEND_IDX(steps);
5994
5995         if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
5996                 return;
5997
5998         if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
5999                 return;
6000
6001         mutex_lock(&dev_priv->sb_lock);
6002
6003         if (steps % 10 != 0)
6004                 tmp = 0xAAAAAAAB;
6005         else
6006                 tmp = 0x00000000;
6007         intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
6008
6009         tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
6010         tmp &= 0xffff0000;
6011         tmp |= sscdivintphase[idx];
6012         intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
6013
6014         mutex_unlock(&dev_priv->sb_lock);
6015 }
6016
6017 #undef BEND_IDX
6018
6019 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
6020 {
6021         u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
6022         u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
6023
6024         if ((ctl & SPLL_PLL_ENABLE) == 0)
6025                 return false;
6026
6027         if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
6028             (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
6029                 return true;
6030
6031         if (IS_BROADWELL(dev_priv) &&
6032             (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
6033                 return true;
6034
6035         return false;
6036 }
6037
6038 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
6039                                enum intel_dpll_id id)
6040 {
6041         u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
6042         u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
6043
6044         if ((ctl & WRPLL_PLL_ENABLE) == 0)
6045                 return false;
6046
6047         if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
6048                 return true;
6049
6050         if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
6051             (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
6052             (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
6053                 return true;
6054
6055         return false;
6056 }
6057
6058 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
6059 {
6060         struct intel_encoder *encoder;
6061         bool has_fdi = false;
6062
6063         for_each_intel_encoder(&dev_priv->drm, encoder) {
6064                 switch (encoder->type) {
6065                 case INTEL_OUTPUT_ANALOG:
6066                         has_fdi = true;
6067                         break;
6068                 default:
6069                         break;
6070                 }
6071         }
6072
6073         /*
6074          * The BIOS may have decided to use the PCH SSC
6075          * reference so we must not disable it until the
6076          * relevant PLLs have stopped relying on it. We'll
6077          * just leave the PCH SSC reference enabled in case
6078          * any active PLL is using it. It will get disabled
6079          * after runtime suspend if we don't have FDI.
6080          *
6081          * TODO: Move the whole reference clock handling
6082          * to the modeset sequence proper so that we can
6083          * actually enable/disable/reconfigure these things
6084          * safely. To do that we need to introduce a real
6085          * clock hierarchy. That would also allow us to do
6086          * clock bending finally.
6087          */
6088         dev_priv->pch_ssc_use = 0;
6089
6090         if (spll_uses_pch_ssc(dev_priv)) {
6091                 drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
6092                 dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
6093         }
6094
6095         if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
6096                 drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
6097                 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
6098         }
6099
6100         if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
6101                 drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
6102                 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
6103         }
6104
6105         if (dev_priv->pch_ssc_use)
6106                 return;
6107
6108         if (has_fdi) {
6109                 lpt_bend_clkout_dp(dev_priv, 0);
6110                 lpt_enable_clkout_dp(dev_priv, true, true);
6111         } else {
6112                 lpt_disable_clkout_dp(dev_priv);
6113         }
6114 }
6115
6116 /*
6117  * Initialize reference clocks when the driver loads
6118  */
6119 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
6120 {
6121         if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
6122                 ilk_init_pch_refclk(dev_priv);
6123         else if (HAS_PCH_LPT(dev_priv))
6124                 lpt_init_pch_refclk(dev_priv);
6125 }
6126
6127 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
6128 {
6129         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6130         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6131         enum pipe pipe = crtc->pipe;
6132         u32 val;
6133
6134         val = 0;
6135
6136         switch (crtc_state->pipe_bpp) {
6137         case 18:
6138                 val |= PIPECONF_6BPC;
6139                 break;
6140         case 24:
6141                 val |= PIPECONF_8BPC;
6142                 break;
6143         case 30:
6144                 val |= PIPECONF_10BPC;
6145                 break;
6146         case 36:
6147                 val |= PIPECONF_12BPC;
6148                 break;
6149         default:
6150                 /* Case prevented by intel_choose_pipe_bpp_dither. */
6151                 BUG();
6152         }
6153
6154         if (crtc_state->dither)
6155                 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6156
6157         if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
6158                 val |= PIPECONF_INTERLACED_ILK;
6159         else
6160                 val |= PIPECONF_PROGRESSIVE;
6161
6162         /*
6163          * This would end up with an odd purple hue over
6164          * the entire display. Make sure we don't do it.
6165          */
6166         drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
6167                     crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
6168
6169         if (crtc_state->limited_color_range &&
6170             !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
6171                 val |= PIPECONF_COLOR_RANGE_SELECT;
6172
6173         if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
6174                 val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
6175
6176         val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
6177
6178         val |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
6179
6180         intel_de_write(dev_priv, PIPECONF(pipe), val);
6181         intel_de_posting_read(dev_priv, PIPECONF(pipe));
6182 }
6183
6184 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
6185 {
6186         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6187         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6188         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
6189         u32 val = 0;
6190
6191         if (IS_HASWELL(dev_priv) && crtc_state->dither)
6192                 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6193
6194         if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
6195                 val |= PIPECONF_INTERLACED_ILK;
6196         else
6197                 val |= PIPECONF_PROGRESSIVE;
6198
6199         if (IS_HASWELL(dev_priv) &&
6200             crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
6201                 val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
6202
6203         intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
6204         intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
6205 }
6206
6207 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
6208 {
6209         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6210         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6211         u32 val = 0;
6212
6213         switch (crtc_state->pipe_bpp) {
6214         case 18:
6215                 val |= PIPEMISC_DITHER_6_BPC;
6216                 break;
6217         case 24:
6218                 val |= PIPEMISC_DITHER_8_BPC;
6219                 break;
6220         case 30:
6221                 val |= PIPEMISC_DITHER_10_BPC;
6222                 break;
6223         case 36:
6224                 val |= PIPEMISC_DITHER_12_BPC;
6225                 break;
6226         default:
6227                 MISSING_CASE(crtc_state->pipe_bpp);
6228                 break;
6229         }
6230
6231         if (crtc_state->dither)
6232                 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6233
6234         if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
6235             crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
6236                 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
6237
6238         if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
6239                 val |= PIPEMISC_YUV420_ENABLE |
6240                         PIPEMISC_YUV420_MODE_FULL_BLEND;
6241
6242         if (INTEL_GEN(dev_priv) >= 11 &&
6243             (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
6244                                            BIT(PLANE_CURSOR))) == 0)
6245                 val |= PIPEMISC_HDR_MODE_PRECISION;
6246
6247         if (INTEL_GEN(dev_priv) >= 12)
6248                 val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
6249
6250         intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
6251 }
6252
6253 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
6254 {
6255         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6256         u32 tmp;
6257
6258         tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
6259
6260         switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
6261         case PIPEMISC_DITHER_6_BPC:
6262                 return 18;
6263         case PIPEMISC_DITHER_8_BPC:
6264                 return 24;
6265         case PIPEMISC_DITHER_10_BPC:
6266                 return 30;
6267         case PIPEMISC_DITHER_12_BPC:
6268                 return 36;
6269         default:
6270                 MISSING_CASE(tmp);
6271                 return 0;
6272         }
6273 }
6274
6275 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
6276 {
6277         /*
6278          * Account for spread spectrum to avoid
6279          * oversubscribing the link. Max center spread
6280          * is 2.5%; use 5% for safety's sake.
6281          */
6282         u32 bps = target_clock * bpp * 21 / 20;
6283         return DIV_ROUND_UP(bps, link_bw * 8);
6284 }
6285
6286 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6287                                          struct intel_link_m_n *m_n)
6288 {
6289         struct drm_device *dev = crtc->base.dev;
6290         struct drm_i915_private *dev_priv = to_i915(dev);
6291         enum pipe pipe = crtc->pipe;
6292
6293         m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
6294         m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
6295         m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
6296                 & ~TU_SIZE_MASK;
6297         m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
6298         m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
6299                     & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6300 }
6301
6302 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6303                                          enum transcoder transcoder,
6304                                          struct intel_link_m_n *m_n,
6305                                          struct intel_link_m_n *m2_n2)
6306 {
6307         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6308         enum pipe pipe = crtc->pipe;
6309
6310         if (INTEL_GEN(dev_priv) >= 5) {
6311                 m_n->link_m = intel_de_read(dev_priv,
6312                                             PIPE_LINK_M1(transcoder));
6313                 m_n->link_n = intel_de_read(dev_priv,
6314                                             PIPE_LINK_N1(transcoder));
6315                 m_n->gmch_m = intel_de_read(dev_priv,
6316                                             PIPE_DATA_M1(transcoder))
6317                         & ~TU_SIZE_MASK;
6318                 m_n->gmch_n = intel_de_read(dev_priv,
6319                                             PIPE_DATA_N1(transcoder));
6320                 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
6321                             & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6322
6323                 if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
6324                         m2_n2->link_m = intel_de_read(dev_priv,
6325                                                       PIPE_LINK_M2(transcoder));
6326                         m2_n2->link_n = intel_de_read(dev_priv,
6327                                                              PIPE_LINK_N2(transcoder));
6328                         m2_n2->gmch_m = intel_de_read(dev_priv,
6329                                                              PIPE_DATA_M2(transcoder))
6330                                         & ~TU_SIZE_MASK;
6331                         m2_n2->gmch_n = intel_de_read(dev_priv,
6332                                                              PIPE_DATA_N2(transcoder));
6333                         m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
6334                                         & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6335                 }
6336         } else {
6337                 m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
6338                 m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
6339                 m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
6340                         & ~TU_SIZE_MASK;
6341                 m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
6342                 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
6343                             & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6344         }
6345 }
6346
6347 void intel_dp_get_m_n(struct intel_crtc *crtc,
6348                       struct intel_crtc_state *pipe_config)
6349 {
6350         if (pipe_config->has_pch_encoder)
6351                 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6352         else
6353                 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6354                                              &pipe_config->dp_m_n,
6355                                              &pipe_config->dp_m2_n2);
6356 }
6357
6358 static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
6359                                    struct intel_crtc_state *pipe_config)
6360 {
6361         intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6362                                      &pipe_config->fdi_m_n, NULL);
6363 }
6364
6365 static void ilk_get_pfit_pos_size(struct intel_crtc_state *crtc_state,
6366                                   u32 pos, u32 size)
6367 {
6368         drm_rect_init(&crtc_state->pch_pfit.dst,
6369                       pos >> 16, pos & 0xffff,
6370                       size >> 16, size & 0xffff);
6371 }
6372
6373 static void skl_get_pfit_config(struct intel_crtc_state *crtc_state)
6374 {
6375         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6376         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6377         struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
6378         int id = -1;
6379         int i;
6380
6381         /* find scaler attached to this pipe */
6382         for (i = 0; i < crtc->num_scalers; i++) {
6383                 u32 ctl, pos, size;
6384
6385                 ctl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
6386                 if ((ctl & (PS_SCALER_EN | PS_PLANE_SEL_MASK)) != PS_SCALER_EN)
6387                         continue;
6388
6389                 id = i;
6390                 crtc_state->pch_pfit.enabled = true;
6391
6392                 pos = intel_de_read(dev_priv, SKL_PS_WIN_POS(crtc->pipe, i));
6393                 size = intel_de_read(dev_priv, SKL_PS_WIN_SZ(crtc->pipe, i));
6394
6395                 ilk_get_pfit_pos_size(crtc_state, pos, size);
6396
6397                 scaler_state->scalers[i].in_use = true;
6398                 break;
6399         }
6400
6401         scaler_state->scaler_id = id;
6402         if (id >= 0)
6403                 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
6404         else
6405                 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
6406 }
6407
6408 static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
6409 {
6410         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6411         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6412         u32 ctl, pos, size;
6413
6414         ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
6415         if ((ctl & PF_ENABLE) == 0)
6416                 return;
6417
6418         crtc_state->pch_pfit.enabled = true;
6419
6420         pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
6421         size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
6422
6423         ilk_get_pfit_pos_size(crtc_state, pos, size);
6424
6425         /*
6426          * We currently do not free assignements of panel fitters on
6427          * ivb/hsw (since we don't use the higher upscaling modes which
6428          * differentiates them) so just WARN about this case for now.
6429          */
6430         drm_WARN_ON(&dev_priv->drm, IS_GEN(dev_priv, 7) &&
6431                     (ctl & PF_PIPE_SEL_MASK_IVB) != PF_PIPE_SEL_IVB(crtc->pipe));
6432 }
6433
6434 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
6435                                 struct intel_crtc_state *pipe_config)
6436 {
6437         struct drm_device *dev = crtc->base.dev;
6438         struct drm_i915_private *dev_priv = to_i915(dev);
6439         enum intel_display_power_domain power_domain;
6440         intel_wakeref_t wakeref;
6441         u32 tmp;
6442         bool ret;
6443
6444         power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
6445         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
6446         if (!wakeref)
6447                 return false;
6448
6449         pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6450         pipe_config->shared_dpll = NULL;
6451
6452         ret = false;
6453         tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
6454         if (!(tmp & PIPECONF_ENABLE))
6455                 goto out;
6456
6457         switch (tmp & PIPECONF_BPC_MASK) {
6458         case PIPECONF_6BPC:
6459                 pipe_config->pipe_bpp = 18;
6460                 break;
6461         case PIPECONF_8BPC:
6462                 pipe_config->pipe_bpp = 24;
6463                 break;
6464         case PIPECONF_10BPC:
6465                 pipe_config->pipe_bpp = 30;
6466                 break;
6467         case PIPECONF_12BPC:
6468                 pipe_config->pipe_bpp = 36;
6469                 break;
6470         default:
6471                 break;
6472         }
6473
6474         if (tmp & PIPECONF_COLOR_RANGE_SELECT)
6475                 pipe_config->limited_color_range = true;
6476
6477         switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
6478         case PIPECONF_OUTPUT_COLORSPACE_YUV601:
6479         case PIPECONF_OUTPUT_COLORSPACE_YUV709:
6480                 pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
6481                 break;
6482         default:
6483                 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
6484                 break;
6485         }
6486
6487         pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
6488                 PIPECONF_GAMMA_MODE_SHIFT;
6489
6490         pipe_config->csc_mode = intel_de_read(dev_priv,
6491                                               PIPE_CSC_MODE(crtc->pipe));
6492
6493         i9xx_get_pipe_color_config(pipe_config);
6494         intel_color_get_config(pipe_config);
6495
6496         if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
6497                 struct intel_shared_dpll *pll;
6498                 enum intel_dpll_id pll_id;
6499                 bool pll_active;
6500
6501                 pipe_config->has_pch_encoder = true;
6502
6503                 tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
6504                 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6505                                           FDI_DP_PORT_WIDTH_SHIFT) + 1;
6506
6507                 ilk_get_fdi_m_n_config(crtc, pipe_config);
6508
6509                 if (HAS_PCH_IBX(dev_priv)) {
6510                         /*
6511                          * The pipe->pch transcoder and pch transcoder->pll
6512                          * mapping is fixed.
6513                          */
6514                         pll_id = (enum intel_dpll_id) crtc->pipe;
6515                 } else {
6516                         tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
6517                         if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6518                                 pll_id = DPLL_ID_PCH_PLL_B;
6519                         else
6520                                 pll_id= DPLL_ID_PCH_PLL_A;
6521                 }
6522
6523                 pipe_config->shared_dpll =
6524                         intel_get_shared_dpll_by_id(dev_priv, pll_id);
6525                 pll = pipe_config->shared_dpll;
6526
6527                 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
6528                                                      &pipe_config->dpll_hw_state);
6529                 drm_WARN_ON(dev, !pll_active);
6530
6531                 tmp = pipe_config->dpll_hw_state.dpll;
6532                 pipe_config->pixel_multiplier =
6533                         ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6534                          >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
6535
6536                 ilk_pch_clock_get(crtc, pipe_config);
6537         } else {
6538                 pipe_config->pixel_multiplier = 1;
6539         }
6540
6541         intel_get_transcoder_timings(crtc, pipe_config);
6542         intel_get_pipe_src_size(crtc, pipe_config);
6543
6544         ilk_get_pfit_config(pipe_config);
6545
6546         ret = true;
6547
6548 out:
6549         intel_display_power_put(dev_priv, power_domain, wakeref);
6550
6551         return ret;
6552 }
6553
6554 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
6555                                      struct intel_crtc_state *pipe_config,
6556                                      struct intel_display_power_domain_set *power_domain_set)
6557 {
6558         struct drm_device *dev = crtc->base.dev;
6559         struct drm_i915_private *dev_priv = to_i915(dev);
6560         unsigned long panel_transcoder_mask = BIT(TRANSCODER_EDP);
6561         unsigned long enabled_panel_transcoders = 0;
6562         enum transcoder panel_transcoder;
6563         u32 tmp;
6564
6565         if (INTEL_GEN(dev_priv) >= 11)
6566                 panel_transcoder_mask |=
6567                         BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
6568
6569         /*
6570          * The pipe->transcoder mapping is fixed with the exception of the eDP
6571          * and DSI transcoders handled below.
6572          */
6573         pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6574
6575         /*
6576          * XXX: Do intel_display_power_get_if_enabled before reading this (for
6577          * consistency and less surprising code; it's in always on power).
6578          */
6579         for_each_cpu_transcoder_masked(dev_priv, panel_transcoder,
6580                                        panel_transcoder_mask) {
6581                 bool force_thru = false;
6582                 enum pipe trans_pipe;
6583
6584                 tmp = intel_de_read(dev_priv,
6585                                     TRANS_DDI_FUNC_CTL(panel_transcoder));
6586                 if (!(tmp & TRANS_DDI_FUNC_ENABLE))
6587                         continue;
6588
6589                 /*
6590                  * Log all enabled ones, only use the first one.
6591                  *
6592                  * FIXME: This won't work for two separate DSI displays.
6593                  */
6594                 enabled_panel_transcoders |= BIT(panel_transcoder);
6595                 if (enabled_panel_transcoders != BIT(panel_transcoder))
6596                         continue;
6597
6598                 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6599                 default:
6600                         drm_WARN(dev, 1,
6601                                  "unknown pipe linked to transcoder %s\n",
6602                                  transcoder_name(panel_transcoder));
6603                         fallthrough;
6604                 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6605                         force_thru = true;
6606                         fallthrough;
6607                 case TRANS_DDI_EDP_INPUT_A_ON:
6608                         trans_pipe = PIPE_A;
6609                         break;
6610                 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6611                         trans_pipe = PIPE_B;
6612                         break;
6613                 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6614                         trans_pipe = PIPE_C;
6615                         break;
6616                 case TRANS_DDI_EDP_INPUT_D_ONOFF:
6617                         trans_pipe = PIPE_D;
6618                         break;
6619                 }
6620
6621                 if (trans_pipe == crtc->pipe) {
6622                         pipe_config->cpu_transcoder = panel_transcoder;
6623                         pipe_config->pch_pfit.force_thru = force_thru;
6624                 }
6625         }
6626
6627         /*
6628          * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
6629          */
6630         drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
6631                     enabled_panel_transcoders != BIT(TRANSCODER_EDP));
6632
6633         if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
6634                                                        POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6635                 return false;
6636
6637         tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
6638
6639         return tmp & PIPECONF_ENABLE;
6640 }
6641
6642 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
6643                                          struct intel_crtc_state *pipe_config,
6644                                          struct intel_display_power_domain_set *power_domain_set)
6645 {
6646         struct drm_device *dev = crtc->base.dev;
6647         struct drm_i915_private *dev_priv = to_i915(dev);
6648         enum transcoder cpu_transcoder;
6649         enum port port;
6650         u32 tmp;
6651
6652         for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
6653                 if (port == PORT_A)
6654                         cpu_transcoder = TRANSCODER_DSI_A;
6655                 else
6656                         cpu_transcoder = TRANSCODER_DSI_C;
6657
6658                 if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
6659                                                                POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
6660                         continue;
6661
6662                 /*
6663                  * The PLL needs to be enabled with a valid divider
6664                  * configuration, otherwise accessing DSI registers will hang
6665                  * the machine. See BSpec North Display Engine
6666                  * registers/MIPI[BXT]. We can break out here early, since we
6667                  * need the same DSI PLL to be enabled for both DSI ports.
6668                  */
6669                 if (!bxt_dsi_pll_is_enabled(dev_priv))
6670                         break;
6671
6672                 /* XXX: this works for video mode only */
6673                 tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
6674                 if (!(tmp & DPI_ENABLE))
6675                         continue;
6676
6677                 tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
6678                 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
6679                         continue;
6680
6681                 pipe_config->cpu_transcoder = cpu_transcoder;
6682                 break;
6683         }
6684
6685         return transcoder_is_dsi(pipe_config->cpu_transcoder);
6686 }
6687
6688 static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
6689                                    struct intel_crtc_state *pipe_config)
6690 {
6691         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6692         enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6693         enum port port;
6694         u32 tmp;
6695
6696         if (transcoder_is_dsi(cpu_transcoder)) {
6697                 port = (cpu_transcoder == TRANSCODER_DSI_A) ?
6698                                                 PORT_A : PORT_B;
6699         } else {
6700                 tmp = intel_de_read(dev_priv,
6701                                     TRANS_DDI_FUNC_CTL(cpu_transcoder));
6702                 if (!(tmp & TRANS_DDI_FUNC_ENABLE))
6703                         return;
6704                 if (INTEL_GEN(dev_priv) >= 12)
6705                         port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
6706                 else
6707                         port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
6708         }
6709
6710         /*
6711          * Haswell has only FDI/PCH transcoder A. It is which is connected to
6712          * DDI E. So just check whether this pipe is wired to DDI E and whether
6713          * the PCH transcoder is on.
6714          */
6715         if (INTEL_GEN(dev_priv) < 9 &&
6716             (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
6717                 pipe_config->has_pch_encoder = true;
6718
6719                 tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
6720                 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6721                                           FDI_DP_PORT_WIDTH_SHIFT) + 1;
6722
6723                 ilk_get_fdi_m_n_config(crtc, pipe_config);
6724         }
6725 }
6726
6727 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
6728                                 struct intel_crtc_state *pipe_config)
6729 {
6730         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6731         struct intel_display_power_domain_set power_domain_set = { };
6732         bool active;
6733         u32 tmp;
6734
6735         if (!intel_display_power_get_in_set_if_enabled(dev_priv, &power_domain_set,
6736                                                        POWER_DOMAIN_PIPE(crtc->pipe)))
6737                 return false;
6738
6739         pipe_config->shared_dpll = NULL;
6740
6741         active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_set);
6742
6743         if (IS_GEN9_LP(dev_priv) &&
6744             bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_set)) {
6745                 drm_WARN_ON(&dev_priv->drm, active);
6746                 active = true;
6747         }
6748
6749         intel_dsc_get_config(pipe_config);
6750
6751         if (!active) {
6752                 /* bigjoiner slave doesn't enable transcoder */
6753                 if (!pipe_config->bigjoiner_slave)
6754                         goto out;
6755
6756                 active = true;
6757                 pipe_config->pixel_multiplier = 1;
6758
6759                 /* we cannot read out most state, so don't bother.. */
6760                 pipe_config->quirks |= PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE;
6761         } else if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
6762             INTEL_GEN(dev_priv) >= 11) {
6763                 hsw_get_ddi_port_state(crtc, pipe_config);
6764                 intel_get_transcoder_timings(crtc, pipe_config);
6765         }
6766
6767         if (HAS_VRR(dev_priv) && !transcoder_is_dsi(pipe_config->cpu_transcoder))
6768                 intel_vrr_get_config(crtc, pipe_config);
6769
6770         intel_get_pipe_src_size(crtc, pipe_config);
6771
6772         if (IS_HASWELL(dev_priv)) {
6773                 u32 tmp = intel_de_read(dev_priv,
6774                                         PIPECONF(pipe_config->cpu_transcoder));
6775
6776                 if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
6777                         pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
6778                 else
6779                         pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
6780         } else {
6781                 pipe_config->output_format =
6782                         bdw_get_pipemisc_output_format(crtc);
6783         }
6784
6785         pipe_config->gamma_mode = intel_de_read(dev_priv,
6786                                                 GAMMA_MODE(crtc->pipe));
6787
6788         pipe_config->csc_mode = intel_de_read(dev_priv,
6789                                               PIPE_CSC_MODE(crtc->pipe));
6790
6791         if (INTEL_GEN(dev_priv) >= 9) {
6792                 tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
6793
6794                 if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
6795                         pipe_config->gamma_enable = true;
6796
6797                 if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
6798                         pipe_config->csc_enable = true;
6799         } else {
6800                 i9xx_get_pipe_color_config(pipe_config);
6801         }
6802
6803         intel_color_get_config(pipe_config);
6804
6805         tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
6806         pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
6807         if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
6808                 pipe_config->ips_linetime =
6809                         REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
6810
6811         if (intel_display_power_get_in_set_if_enabled(dev_priv, &power_domain_set,
6812                                                       POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe))) {
6813                 if (INTEL_GEN(dev_priv) >= 9)
6814                         skl_get_pfit_config(pipe_config);
6815                 else
6816                         ilk_get_pfit_config(pipe_config);
6817         }
6818
6819         if (hsw_crtc_supports_ips(crtc)) {
6820                 if (IS_HASWELL(dev_priv))
6821                         pipe_config->ips_enabled = intel_de_read(dev_priv,
6822                                                                  IPS_CTL) & IPS_ENABLE;
6823                 else {
6824                         /*
6825                          * We cannot readout IPS state on broadwell, set to
6826                          * true so we can set it to a defined state on first
6827                          * commit.
6828                          */
6829                         pipe_config->ips_enabled = true;
6830                 }
6831         }
6832
6833         if (pipe_config->bigjoiner_slave) {
6834                 /* Cannot be read out as a slave, set to 0. */
6835                 pipe_config->pixel_multiplier = 0;
6836         } else if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
6837             !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
6838                 pipe_config->pixel_multiplier =
6839                         intel_de_read(dev_priv,
6840                                       PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
6841         } else {
6842                 pipe_config->pixel_multiplier = 1;
6843         }
6844
6845 out:
6846         intel_display_power_put_all_in_set(dev_priv, &power_domain_set);
6847
6848         return active;
6849 }
6850
6851 static bool intel_crtc_get_pipe_config(struct intel_crtc_state *crtc_state)
6852 {
6853         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6854         struct drm_i915_private *i915 = to_i915(crtc->base.dev);
6855
6856         if (!i915->display.get_pipe_config(crtc, crtc_state))
6857                 return false;
6858
6859         crtc_state->hw.active = true;
6860
6861         intel_crtc_readout_derived_state(crtc_state);
6862
6863         return true;
6864 }
6865
6866 /* VESA 640x480x72Hz mode to set on the pipe */
6867 static const struct drm_display_mode load_detect_mode = {
6868         DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6869                  704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6870 };
6871
6872 struct drm_framebuffer *
6873 intel_framebuffer_create(struct drm_i915_gem_object *obj,
6874                          struct drm_mode_fb_cmd2 *mode_cmd)
6875 {
6876         struct intel_framebuffer *intel_fb;
6877         int ret;
6878
6879         intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6880         if (!intel_fb)
6881                 return ERR_PTR(-ENOMEM);
6882
6883         ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
6884         if (ret)
6885                 goto err;
6886
6887         return &intel_fb->base;
6888
6889 err:
6890         kfree(intel_fb);
6891         return ERR_PTR(ret);
6892 }
6893
6894 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
6895                                         struct drm_crtc *crtc)
6896 {
6897         struct drm_plane *plane;
6898         struct drm_plane_state *plane_state;
6899         int ret, i;
6900
6901         ret = drm_atomic_add_affected_planes(state, crtc);
6902         if (ret)
6903                 return ret;
6904
6905         for_each_new_plane_in_state(state, plane, plane_state, i) {
6906                 if (plane_state->crtc != crtc)
6907                         continue;
6908
6909                 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
6910                 if (ret)
6911                         return ret;
6912
6913                 drm_atomic_set_fb_for_plane(plane_state, NULL);
6914         }
6915
6916         return 0;
6917 }
6918
6919 int intel_get_load_detect_pipe(struct drm_connector *connector,
6920                                struct intel_load_detect_pipe *old,
6921                                struct drm_modeset_acquire_ctx *ctx)
6922 {
6923         struct intel_crtc *intel_crtc;
6924         struct intel_encoder *intel_encoder =
6925                 intel_attached_encoder(to_intel_connector(connector));
6926         struct drm_crtc *possible_crtc;
6927         struct drm_encoder *encoder = &intel_encoder->base;
6928         struct drm_crtc *crtc = NULL;
6929         struct drm_device *dev = encoder->dev;
6930         struct drm_i915_private *dev_priv = to_i915(dev);
6931         struct drm_mode_config *config = &dev->mode_config;
6932         struct drm_atomic_state *state = NULL, *restore_state = NULL;
6933         struct drm_connector_state *connector_state;
6934         struct intel_crtc_state *crtc_state;
6935         int ret, i = -1;
6936
6937         drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6938                     connector->base.id, connector->name,
6939                     encoder->base.id, encoder->name);
6940
6941         old->restore_state = NULL;
6942
6943         drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
6944
6945         /*
6946          * Algorithm gets a little messy:
6947          *
6948          *   - if the connector already has an assigned crtc, use it (but make
6949          *     sure it's on first)
6950          *
6951          *   - try to find the first unused crtc that can drive this connector,
6952          *     and use that if we find one
6953          */
6954
6955         /* See if we already have a CRTC for this connector */
6956         if (connector->state->crtc) {
6957                 crtc = connector->state->crtc;
6958
6959                 ret = drm_modeset_lock(&crtc->mutex, ctx);
6960                 if (ret)
6961                         goto fail;
6962
6963                 /* Make sure the crtc and connector are running */
6964                 goto found;
6965         }
6966
6967         /* Find an unused one (if possible) */
6968         for_each_crtc(dev, possible_crtc) {
6969                 i++;
6970                 if (!(encoder->possible_crtcs & (1 << i)))
6971                         continue;
6972
6973                 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
6974                 if (ret)
6975                         goto fail;
6976
6977                 if (possible_crtc->state->enable) {
6978                         drm_modeset_unlock(&possible_crtc->mutex);
6979                         continue;
6980                 }
6981
6982                 crtc = possible_crtc;
6983                 break;
6984         }
6985
6986         /*
6987          * If we didn't find an unused CRTC, don't use any.
6988          */
6989         if (!crtc) {
6990                 drm_dbg_kms(&dev_priv->drm,
6991                             "no pipe available for load-detect\n");
6992                 ret = -ENODEV;
6993                 goto fail;
6994         }
6995
6996 found:
6997         intel_crtc = to_intel_crtc(crtc);
6998
6999         state = drm_atomic_state_alloc(dev);
7000         restore_state = drm_atomic_state_alloc(dev);
7001         if (!state || !restore_state) {
7002                 ret = -ENOMEM;
7003                 goto fail;
7004         }
7005
7006         state->acquire_ctx = ctx;
7007         restore_state->acquire_ctx = ctx;
7008
7009         connector_state = drm_atomic_get_connector_state(state, connector);
7010         if (IS_ERR(connector_state)) {
7011                 ret = PTR_ERR(connector_state);
7012                 goto fail;
7013         }
7014
7015         ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
7016         if (ret)
7017                 goto fail;
7018
7019         crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
7020         if (IS_ERR(crtc_state)) {
7021                 ret = PTR_ERR(crtc_state);
7022                 goto fail;
7023         }
7024
7025         crtc_state->uapi.active = true;
7026
7027         ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
7028                                            &load_detect_mode);
7029         if (ret)
7030                 goto fail;
7031
7032         ret = intel_modeset_disable_planes(state, crtc);
7033         if (ret)
7034                 goto fail;
7035
7036         ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
7037         if (!ret)
7038                 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
7039         if (!ret)
7040                 ret = drm_atomic_add_affected_planes(restore_state, crtc);
7041         if (ret) {
7042                 drm_dbg_kms(&dev_priv->drm,
7043                             "Failed to create a copy of old state to restore: %i\n",
7044                             ret);
7045                 goto fail;
7046         }
7047
7048         ret = drm_atomic_commit(state);
7049         if (ret) {
7050                 drm_dbg_kms(&dev_priv->drm,
7051                             "failed to set mode on load-detect pipe\n");
7052                 goto fail;
7053         }
7054
7055         old->restore_state = restore_state;
7056         drm_atomic_state_put(state);
7057
7058         /* let the connector get through one full cycle before testing */
7059         intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7060         return true;
7061
7062 fail:
7063         if (state) {
7064                 drm_atomic_state_put(state);
7065                 state = NULL;
7066         }
7067         if (restore_state) {
7068                 drm_atomic_state_put(restore_state);
7069                 restore_state = NULL;
7070         }
7071
7072         if (ret == -EDEADLK)
7073                 return ret;
7074
7075         return false;
7076 }
7077
7078 void intel_release_load_detect_pipe(struct drm_connector *connector,
7079                                     struct intel_load_detect_pipe *old,
7080                                     struct drm_modeset_acquire_ctx *ctx)
7081 {
7082         struct intel_encoder *intel_encoder =
7083                 intel_attached_encoder(to_intel_connector(connector));
7084         struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
7085         struct drm_encoder *encoder = &intel_encoder->base;
7086         struct drm_atomic_state *state = old->restore_state;
7087         int ret;
7088
7089         drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7090                     connector->base.id, connector->name,
7091                     encoder->base.id, encoder->name);
7092
7093         if (!state)
7094                 return;
7095
7096         ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
7097         if (ret)
7098                 drm_dbg_kms(&i915->drm,
7099                             "Couldn't release load detect pipe: %i\n", ret);
7100         drm_atomic_state_put(state);
7101 }
7102
7103 static int i9xx_pll_refclk(struct drm_device *dev,
7104                            const struct intel_crtc_state *pipe_config)
7105 {
7106         struct drm_i915_private *dev_priv = to_i915(dev);
7107         u32 dpll = pipe_config->dpll_hw_state.dpll;
7108
7109         if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7110                 return dev_priv->vbt.lvds_ssc_freq;
7111         else if (HAS_PCH_SPLIT(dev_priv))
7112                 return 120000;
7113         else if (!IS_GEN(dev_priv, 2))
7114                 return 96000;
7115         else
7116                 return 48000;
7117 }
7118
7119 /* Returns the clock of the currently programmed mode of the given pipe. */
7120 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7121                                 struct intel_crtc_state *pipe_config)
7122 {
7123         struct drm_device *dev = crtc->base.dev;
7124         struct drm_i915_private *dev_priv = to_i915(dev);
7125         enum pipe pipe = crtc->pipe;
7126         u32 dpll = pipe_config->dpll_hw_state.dpll;
7127         u32 fp;
7128         struct dpll clock;
7129         int port_clock;
7130         int refclk = i9xx_pll_refclk(dev, pipe_config);
7131
7132         if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
7133                 fp = pipe_config->dpll_hw_state.fp0;
7134         else
7135                 fp = pipe_config->dpll_hw_state.fp1;
7136
7137         clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
7138         if (IS_PINEVIEW(dev_priv)) {
7139                 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7140                 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
7141         } else {
7142                 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7143                 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7144         }
7145
7146         if (!IS_GEN(dev_priv, 2)) {
7147                 if (IS_PINEVIEW(dev_priv))
7148                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7149                                 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
7150                 else
7151                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
7152                                DPLL_FPA01_P1_POST_DIV_SHIFT);
7153
7154                 switch (dpll & DPLL_MODE_MASK) {
7155                 case DPLLB_MODE_DAC_SERIAL:
7156                         clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7157                                 5 : 10;
7158                         break;
7159                 case DPLLB_MODE_LVDS:
7160                         clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7161                                 7 : 14;
7162                         break;
7163                 default:
7164                         drm_dbg_kms(&dev_priv->drm,
7165                                     "Unknown DPLL mode %08x in programmed "
7166                                     "mode\n", (int)(dpll & DPLL_MODE_MASK));
7167                         return;
7168                 }
7169
7170                 if (IS_PINEVIEW(dev_priv))
7171                         port_clock = pnv_calc_dpll_params(refclk, &clock);
7172                 else
7173                         port_clock = i9xx_calc_dpll_params(refclk, &clock);
7174         } else {
7175                 u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
7176                                                                  LVDS);
7177                 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
7178
7179                 if (is_lvds) {
7180                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7181                                        DPLL_FPA01_P1_POST_DIV_SHIFT);
7182
7183                         if (lvds & LVDS_CLKB_POWER_UP)
7184                                 clock.p2 = 7;
7185                         else
7186                                 clock.p2 = 14;
7187                 } else {
7188                         if (dpll & PLL_P1_DIVIDE_BY_TWO)
7189                                 clock.p1 = 2;
7190                         else {
7191                                 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7192                                             DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7193                         }
7194                         if (dpll & PLL_P2_DIVIDE_BY_4)
7195                                 clock.p2 = 4;
7196                         else
7197                                 clock.p2 = 2;
7198                 }
7199
7200                 port_clock = i9xx_calc_dpll_params(refclk, &clock);
7201         }
7202
7203         /*
7204          * This value includes pixel_multiplier. We will use
7205          * port_clock to compute adjusted_mode.crtc_clock in the
7206          * encoder's get_config() function.
7207          */
7208         pipe_config->port_clock = port_clock;
7209 }
7210
7211 int intel_dotclock_calculate(int link_freq,
7212                              const struct intel_link_m_n *m_n)
7213 {
7214         /*
7215          * The calculation for the data clock is:
7216          * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
7217          * But we want to avoid losing precison if possible, so:
7218          * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
7219          *
7220          * and the link clock is simpler:
7221          * link_clock = (m * link_clock) / n
7222          */
7223
7224         if (!m_n->link_n)
7225                 return 0;
7226
7227         return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
7228 }
7229
7230 static void ilk_pch_clock_get(struct intel_crtc *crtc,
7231                               struct intel_crtc_state *pipe_config)
7232 {
7233         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7234
7235         /* read out port_clock from the DPLL */
7236         i9xx_crtc_clock_get(crtc, pipe_config);
7237
7238         /*
7239          * In case there is an active pipe without active ports,
7240          * we may need some idea for the dotclock anyway.
7241          * Calculate one based on the FDI configuration.
7242          */
7243         pipe_config->hw.adjusted_mode.crtc_clock =
7244                 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
7245                                          &pipe_config->fdi_m_n);
7246 }
7247
7248 /* Returns the currently programmed mode of the given encoder. */
7249 struct drm_display_mode *
7250 intel_encoder_current_mode(struct intel_encoder *encoder)
7251 {
7252         struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
7253         struct intel_crtc_state *crtc_state;
7254         struct drm_display_mode *mode;
7255         struct intel_crtc *crtc;
7256         enum pipe pipe;
7257
7258         if (!encoder->get_hw_state(encoder, &pipe))
7259                 return NULL;
7260
7261         crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
7262
7263         mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7264         if (!mode)
7265                 return NULL;
7266
7267         crtc_state = intel_crtc_state_alloc(crtc);
7268         if (!crtc_state) {
7269                 kfree(mode);
7270                 return NULL;
7271         }
7272
7273         if (!intel_crtc_get_pipe_config(crtc_state)) {
7274                 kfree(crtc_state);
7275                 kfree(mode);
7276                 return NULL;
7277         }
7278
7279         intel_encoder_get_config(encoder, crtc_state);
7280
7281         intel_mode_from_crtc_timings(mode, &crtc_state->hw.adjusted_mode);
7282
7283         kfree(crtc_state);
7284
7285         return mode;
7286 }
7287
7288 /**
7289  * intel_wm_need_update - Check whether watermarks need updating
7290  * @cur: current plane state
7291  * @new: new plane state
7292  *
7293  * Check current plane state versus the new one to determine whether
7294  * watermarks need to be recalculated.
7295  *
7296  * Returns true or false.
7297  */
7298 static bool intel_wm_need_update(const struct intel_plane_state *cur,
7299                                  struct intel_plane_state *new)
7300 {
7301         /* Update watermarks on tiling or size changes. */
7302         if (new->uapi.visible != cur->uapi.visible)
7303                 return true;
7304
7305         if (!cur->hw.fb || !new->hw.fb)
7306                 return false;
7307
7308         if (cur->hw.fb->modifier != new->hw.fb->modifier ||
7309             cur->hw.rotation != new->hw.rotation ||
7310             drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
7311             drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
7312             drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
7313             drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
7314                 return true;
7315
7316         return false;
7317 }
7318
7319 static bool needs_scaling(const struct intel_plane_state *state)
7320 {
7321         int src_w = drm_rect_width(&state->uapi.src) >> 16;
7322         int src_h = drm_rect_height(&state->uapi.src) >> 16;
7323         int dst_w = drm_rect_width(&state->uapi.dst);
7324         int dst_h = drm_rect_height(&state->uapi.dst);
7325
7326         return (src_w != dst_w || src_h != dst_h);
7327 }
7328
7329 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
7330                                     struct intel_crtc_state *crtc_state,
7331                                     const struct intel_plane_state *old_plane_state,
7332                                     struct intel_plane_state *plane_state)
7333 {
7334         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7335         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
7336         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7337         bool mode_changed = intel_crtc_needs_modeset(crtc_state);
7338         bool was_crtc_enabled = old_crtc_state->hw.active;
7339         bool is_crtc_enabled = crtc_state->hw.active;
7340         bool turn_off, turn_on, visible, was_visible;
7341         int ret;
7342
7343         if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
7344                 ret = skl_update_scaler_plane(crtc_state, plane_state);
7345                 if (ret)
7346                         return ret;
7347         }
7348
7349         was_visible = old_plane_state->uapi.visible;
7350         visible = plane_state->uapi.visible;
7351
7352         if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
7353                 was_visible = false;
7354
7355         /*
7356          * Visibility is calculated as if the crtc was on, but
7357          * after scaler setup everything depends on it being off
7358          * when the crtc isn't active.
7359          *
7360          * FIXME this is wrong for watermarks. Watermarks should also
7361          * be computed as if the pipe would be active. Perhaps move
7362          * per-plane wm computation to the .check_plane() hook, and
7363          * only combine the results from all planes in the current place?
7364          */
7365         if (!is_crtc_enabled) {
7366                 intel_plane_set_invisible(crtc_state, plane_state);
7367                 visible = false;
7368         }
7369
7370         if (!was_visible && !visible)
7371                 return 0;
7372
7373         turn_off = was_visible && (!visible || mode_changed);
7374         turn_on = visible && (!was_visible || mode_changed);
7375
7376         drm_dbg_atomic(&dev_priv->drm,
7377                        "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
7378                        crtc->base.base.id, crtc->base.name,
7379                        plane->base.base.id, plane->base.name,
7380                        was_visible, visible,
7381                        turn_off, turn_on, mode_changed);
7382
7383         if (turn_on) {
7384                 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
7385                         crtc_state->update_wm_pre = true;
7386
7387                 /* must disable cxsr around plane enable/disable */
7388                 if (plane->id != PLANE_CURSOR)
7389                         crtc_state->disable_cxsr = true;
7390         } else if (turn_off) {
7391                 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
7392                         crtc_state->update_wm_post = true;
7393
7394                 /* must disable cxsr around plane enable/disable */
7395                 if (plane->id != PLANE_CURSOR)
7396                         crtc_state->disable_cxsr = true;
7397         } else if (intel_wm_need_update(old_plane_state, plane_state)) {
7398                 if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
7399                         /* FIXME bollocks */
7400                         crtc_state->update_wm_pre = true;
7401                         crtc_state->update_wm_post = true;
7402                 }
7403         }
7404
7405         if (visible || was_visible)
7406                 crtc_state->fb_bits |= plane->frontbuffer_bit;
7407
7408         /*
7409          * ILK/SNB DVSACNTR/Sprite Enable
7410          * IVB SPR_CTL/Sprite Enable
7411          * "When in Self Refresh Big FIFO mode, a write to enable the
7412          *  plane will be internally buffered and delayed while Big FIFO
7413          *  mode is exiting."
7414          *
7415          * Which means that enabling the sprite can take an extra frame
7416          * when we start in big FIFO mode (LP1+). Thus we need to drop
7417          * down to LP0 and wait for vblank in order to make sure the
7418          * sprite gets enabled on the next vblank after the register write.
7419          * Doing otherwise would risk enabling the sprite one frame after
7420          * we've already signalled flip completion. We can resume LP1+
7421          * once the sprite has been enabled.
7422          *
7423          *
7424          * WaCxSRDisabledForSpriteScaling:ivb
7425          * IVB SPR_SCALE/Scaling Enable
7426          * "Low Power watermarks must be disabled for at least one
7427          *  frame before enabling sprite scaling, and kept disabled
7428          *  until sprite scaling is disabled."
7429          *
7430          * ILK/SNB DVSASCALE/Scaling Enable
7431          * "When in Self Refresh Big FIFO mode, scaling enable will be
7432          *  masked off while Big FIFO mode is exiting."
7433          *
7434          * Despite the w/a only being listed for IVB we assume that
7435          * the ILK/SNB note has similar ramifications, hence we apply
7436          * the w/a on all three platforms.
7437          *
7438          * With experimental results seems this is needed also for primary
7439          * plane, not only sprite plane.
7440          */
7441         if (plane->id != PLANE_CURSOR &&
7442             (IS_GEN_RANGE(dev_priv, 5, 6) ||
7443              IS_IVYBRIDGE(dev_priv)) &&
7444             (turn_on || (!needs_scaling(old_plane_state) &&
7445                          needs_scaling(plane_state))))
7446                 crtc_state->disable_lp_wm = true;
7447
7448         return 0;
7449 }
7450
7451 static bool encoders_cloneable(const struct intel_encoder *a,
7452                                const struct intel_encoder *b)
7453 {
7454         /* masks could be asymmetric, so check both ways */
7455         return a == b || (a->cloneable & (1 << b->type) &&
7456                           b->cloneable & (1 << a->type));
7457 }
7458
7459 static bool check_single_encoder_cloning(struct intel_atomic_state *state,
7460                                          struct intel_crtc *crtc,
7461                                          struct intel_encoder *encoder)
7462 {
7463         struct intel_encoder *source_encoder;
7464         struct drm_connector *connector;
7465         struct drm_connector_state *connector_state;
7466         int i;
7467
7468         for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
7469                 if (connector_state->crtc != &crtc->base)
7470                         continue;
7471
7472                 source_encoder =
7473                         to_intel_encoder(connector_state->best_encoder);
7474                 if (!encoders_cloneable(encoder, source_encoder))
7475                         return false;
7476         }
7477
7478         return true;
7479 }
7480
7481 static int icl_add_linked_planes(struct intel_atomic_state *state)
7482 {
7483         struct intel_plane *plane, *linked;
7484         struct intel_plane_state *plane_state, *linked_plane_state;
7485         int i;
7486
7487         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7488                 linked = plane_state->planar_linked_plane;
7489
7490                 if (!linked)
7491                         continue;
7492
7493                 linked_plane_state = intel_atomic_get_plane_state(state, linked);
7494                 if (IS_ERR(linked_plane_state))
7495                         return PTR_ERR(linked_plane_state);
7496
7497                 drm_WARN_ON(state->base.dev,
7498                             linked_plane_state->planar_linked_plane != plane);
7499                 drm_WARN_ON(state->base.dev,
7500                             linked_plane_state->planar_slave == plane_state->planar_slave);
7501         }
7502
7503         return 0;
7504 }
7505
7506 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
7507 {
7508         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7509         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7510         struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
7511         struct intel_plane *plane, *linked;
7512         struct intel_plane_state *plane_state;
7513         int i;
7514
7515         if (INTEL_GEN(dev_priv) < 11)
7516                 return 0;
7517
7518         /*
7519          * Destroy all old plane links and make the slave plane invisible
7520          * in the crtc_state->active_planes mask.
7521          */
7522         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7523                 if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
7524                         continue;
7525
7526                 plane_state->planar_linked_plane = NULL;
7527                 if (plane_state->planar_slave && !plane_state->uapi.visible) {
7528                         crtc_state->enabled_planes &= ~BIT(plane->id);
7529                         crtc_state->active_planes &= ~BIT(plane->id);
7530                         crtc_state->update_planes |= BIT(plane->id);
7531                 }
7532
7533                 plane_state->planar_slave = false;
7534         }
7535
7536         if (!crtc_state->nv12_planes)
7537                 return 0;
7538
7539         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7540                 struct intel_plane_state *linked_state = NULL;
7541
7542                 if (plane->pipe != crtc->pipe ||
7543                     !(crtc_state->nv12_planes & BIT(plane->id)))
7544                         continue;
7545
7546                 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
7547                         if (!icl_is_nv12_y_plane(dev_priv, linked->id))
7548                                 continue;
7549
7550                         if (crtc_state->active_planes & BIT(linked->id))
7551                                 continue;
7552
7553                         linked_state = intel_atomic_get_plane_state(state, linked);
7554                         if (IS_ERR(linked_state))
7555                                 return PTR_ERR(linked_state);
7556
7557                         break;
7558                 }
7559
7560                 if (!linked_state) {
7561                         drm_dbg_kms(&dev_priv->drm,
7562                                     "Need %d free Y planes for planar YUV\n",
7563                                     hweight8(crtc_state->nv12_planes));
7564
7565                         return -EINVAL;
7566                 }
7567
7568                 plane_state->planar_linked_plane = linked;
7569
7570                 linked_state->planar_slave = true;
7571                 linked_state->planar_linked_plane = plane;
7572                 crtc_state->enabled_planes |= BIT(linked->id);
7573                 crtc_state->active_planes |= BIT(linked->id);
7574                 crtc_state->update_planes |= BIT(linked->id);
7575                 drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
7576                             linked->base.name, plane->base.name);
7577
7578                 /* Copy parameters to slave plane */
7579                 linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
7580                 linked_state->color_ctl = plane_state->color_ctl;
7581                 linked_state->view = plane_state->view;
7582                 memcpy(linked_state->color_plane, plane_state->color_plane,
7583                        sizeof(linked_state->color_plane));
7584
7585                 intel_plane_copy_hw_state(linked_state, plane_state);
7586                 linked_state->uapi.src = plane_state->uapi.src;
7587                 linked_state->uapi.dst = plane_state->uapi.dst;
7588
7589                 if (icl_is_hdr_plane(dev_priv, plane->id)) {
7590                         if (linked->id == PLANE_SPRITE5)
7591                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
7592                         else if (linked->id == PLANE_SPRITE4)
7593                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
7594                         else if (linked->id == PLANE_SPRITE3)
7595                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_5_RKL;
7596                         else if (linked->id == PLANE_SPRITE2)
7597                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_4_RKL;
7598                         else
7599                                 MISSING_CASE(linked->id);
7600                 }
7601         }
7602
7603         return 0;
7604 }
7605
7606 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
7607 {
7608         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
7609         struct intel_atomic_state *state =
7610                 to_intel_atomic_state(new_crtc_state->uapi.state);
7611         const struct intel_crtc_state *old_crtc_state =
7612                 intel_atomic_get_old_crtc_state(state, crtc);
7613
7614         return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
7615 }
7616
7617 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
7618 {
7619         const struct drm_display_mode *pipe_mode =
7620                 &crtc_state->hw.pipe_mode;
7621         int linetime_wm;
7622
7623         if (!crtc_state->hw.enable)
7624                 return 0;
7625
7626         linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
7627                                         pipe_mode->crtc_clock);
7628
7629         return min(linetime_wm, 0x1ff);
7630 }
7631
7632 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
7633                                const struct intel_cdclk_state *cdclk_state)
7634 {
7635         const struct drm_display_mode *pipe_mode =
7636                 &crtc_state->hw.pipe_mode;
7637         int linetime_wm;
7638
7639         if (!crtc_state->hw.enable)
7640                 return 0;
7641
7642         linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
7643                                         cdclk_state->logical.cdclk);
7644
7645         return min(linetime_wm, 0x1ff);
7646 }
7647
7648 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
7649 {
7650         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7651         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7652         const struct drm_display_mode *pipe_mode =
7653                 &crtc_state->hw.pipe_mode;
7654         int linetime_wm;
7655
7656         if (!crtc_state->hw.enable)
7657                 return 0;
7658
7659         linetime_wm = DIV_ROUND_UP(pipe_mode->crtc_htotal * 1000 * 8,
7660                                    crtc_state->pixel_rate);
7661
7662         /* Display WA #1135: BXT:ALL GLK:ALL */
7663         if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
7664                 linetime_wm /= 2;
7665
7666         return min(linetime_wm, 0x1ff);
7667 }
7668
7669 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
7670                                    struct intel_crtc *crtc)
7671 {
7672         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7673         struct intel_crtc_state *crtc_state =
7674                 intel_atomic_get_new_crtc_state(state, crtc);
7675         const struct intel_cdclk_state *cdclk_state;
7676
7677         if (INTEL_GEN(dev_priv) >= 9)
7678                 crtc_state->linetime = skl_linetime_wm(crtc_state);
7679         else
7680                 crtc_state->linetime = hsw_linetime_wm(crtc_state);
7681
7682         if (!hsw_crtc_supports_ips(crtc))
7683                 return 0;
7684
7685         cdclk_state = intel_atomic_get_cdclk_state(state);
7686         if (IS_ERR(cdclk_state))
7687                 return PTR_ERR(cdclk_state);
7688
7689         crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
7690                                                        cdclk_state);
7691
7692         return 0;
7693 }
7694
7695 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
7696                                    struct intel_crtc *crtc)
7697 {
7698         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7699         struct intel_crtc_state *crtc_state =
7700                 intel_atomic_get_new_crtc_state(state, crtc);
7701         bool mode_changed = intel_crtc_needs_modeset(crtc_state);
7702         int ret;
7703
7704         if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
7705             mode_changed && !crtc_state->hw.active)
7706                 crtc_state->update_wm_post = true;
7707
7708         if (mode_changed && crtc_state->hw.enable &&
7709             dev_priv->display.crtc_compute_clock &&
7710             !crtc_state->bigjoiner_slave &&
7711             !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
7712                 ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
7713                 if (ret)
7714                         return ret;
7715         }
7716
7717         /*
7718          * May need to update pipe gamma enable bits
7719          * when C8 planes are getting enabled/disabled.
7720          */
7721         if (c8_planes_changed(crtc_state))
7722                 crtc_state->uapi.color_mgmt_changed = true;
7723
7724         if (mode_changed || crtc_state->update_pipe ||
7725             crtc_state->uapi.color_mgmt_changed) {
7726                 ret = intel_color_check(crtc_state);
7727                 if (ret)
7728                         return ret;
7729         }
7730
7731         if (dev_priv->display.compute_pipe_wm) {
7732                 ret = dev_priv->display.compute_pipe_wm(crtc_state);
7733                 if (ret) {
7734                         drm_dbg_kms(&dev_priv->drm,
7735                                     "Target pipe watermarks are invalid\n");
7736                         return ret;
7737                 }
7738         }
7739
7740         if (dev_priv->display.compute_intermediate_wm) {
7741                 if (drm_WARN_ON(&dev_priv->drm,
7742                                 !dev_priv->display.compute_pipe_wm))
7743                         return 0;
7744
7745                 /*
7746                  * Calculate 'intermediate' watermarks that satisfy both the
7747                  * old state and the new state.  We can program these
7748                  * immediately.
7749                  */
7750                 ret = dev_priv->display.compute_intermediate_wm(crtc_state);
7751                 if (ret) {
7752                         drm_dbg_kms(&dev_priv->drm,
7753                                     "No valid intermediate pipe watermarks are possible\n");
7754                         return ret;
7755                 }
7756         }
7757
7758         if (INTEL_GEN(dev_priv) >= 9) {
7759                 if (mode_changed || crtc_state->update_pipe) {
7760                         ret = skl_update_scaler_crtc(crtc_state);
7761                         if (ret)
7762                                 return ret;
7763                 }
7764
7765                 ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
7766                 if (ret)
7767                         return ret;
7768         }
7769
7770         if (HAS_IPS(dev_priv)) {
7771                 ret = hsw_compute_ips_config(crtc_state);
7772                 if (ret)
7773                         return ret;
7774         }
7775
7776         if (INTEL_GEN(dev_priv) >= 9 ||
7777             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
7778                 ret = hsw_compute_linetime_wm(state, crtc);
7779                 if (ret)
7780                         return ret;
7781
7782         }
7783
7784         if (!mode_changed) {
7785                 ret = intel_psr2_sel_fetch_update(state, crtc);
7786                 if (ret)
7787                         return ret;
7788         }
7789
7790         return 0;
7791 }
7792
7793 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
7794 {
7795         struct intel_connector *connector;
7796         struct drm_connector_list_iter conn_iter;
7797
7798         drm_connector_list_iter_begin(dev, &conn_iter);
7799         for_each_intel_connector_iter(connector, &conn_iter) {
7800                 struct drm_connector_state *conn_state = connector->base.state;
7801                 struct intel_encoder *encoder =
7802                         to_intel_encoder(connector->base.encoder);
7803
7804                 if (conn_state->crtc)
7805                         drm_connector_put(&connector->base);
7806
7807                 if (encoder) {
7808                         struct intel_crtc *crtc =
7809                                 to_intel_crtc(encoder->base.crtc);
7810                         const struct intel_crtc_state *crtc_state =
7811                                 to_intel_crtc_state(crtc->base.state);
7812
7813                         conn_state->best_encoder = &encoder->base;
7814                         conn_state->crtc = &crtc->base;
7815                         conn_state->max_bpc = (crtc_state->pipe_bpp ?: 24) / 3;
7816
7817                         drm_connector_get(&connector->base);
7818                 } else {
7819                         conn_state->best_encoder = NULL;
7820                         conn_state->crtc = NULL;
7821                 }
7822         }
7823         drm_connector_list_iter_end(&conn_iter);
7824 }
7825
7826 static int
7827 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
7828                       struct intel_crtc_state *pipe_config)
7829 {
7830         struct drm_connector *connector = conn_state->connector;
7831         struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
7832         const struct drm_display_info *info = &connector->display_info;
7833         int bpp;
7834
7835         switch (conn_state->max_bpc) {
7836         case 6 ... 7:
7837                 bpp = 6 * 3;
7838                 break;
7839         case 8 ... 9:
7840                 bpp = 8 * 3;
7841                 break;
7842         case 10 ... 11:
7843                 bpp = 10 * 3;
7844                 break;
7845         case 12 ... 16:
7846                 bpp = 12 * 3;
7847                 break;
7848         default:
7849                 MISSING_CASE(conn_state->max_bpc);
7850                 return -EINVAL;
7851         }
7852
7853         if (bpp < pipe_config->pipe_bpp) {
7854                 drm_dbg_kms(&i915->drm,
7855                             "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
7856                             "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
7857                             connector->base.id, connector->name,
7858                             bpp, 3 * info->bpc,
7859                             3 * conn_state->max_requested_bpc,
7860                             pipe_config->pipe_bpp);
7861
7862                 pipe_config->pipe_bpp = bpp;
7863         }
7864
7865         return 0;
7866 }
7867
7868 static int
7869 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7870                           struct intel_crtc_state *pipe_config)
7871 {
7872         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7873         struct drm_atomic_state *state = pipe_config->uapi.state;
7874         struct drm_connector *connector;
7875         struct drm_connector_state *connector_state;
7876         int bpp, i;
7877
7878         if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7879             IS_CHERRYVIEW(dev_priv)))
7880                 bpp = 10*3;
7881         else if (INTEL_GEN(dev_priv) >= 5)
7882                 bpp = 12*3;
7883         else
7884                 bpp = 8*3;
7885
7886         pipe_config->pipe_bpp = bpp;
7887
7888         /* Clamp display bpp to connector max bpp */
7889         for_each_new_connector_in_state(state, connector, connector_state, i) {
7890                 int ret;
7891
7892                 if (connector_state->crtc != &crtc->base)
7893                         continue;
7894
7895                 ret = compute_sink_pipe_bpp(connector_state, pipe_config);
7896                 if (ret)
7897                         return ret;
7898         }
7899
7900         return 0;
7901 }
7902
7903 static void intel_dump_crtc_timings(struct drm_i915_private *i915,
7904                                     const struct drm_display_mode *mode)
7905 {
7906         drm_dbg_kms(&i915->drm, "crtc timings: %d %d %d %d %d %d %d %d %d, "
7907                     "type: 0x%x flags: 0x%x\n",
7908                     mode->crtc_clock,
7909                     mode->crtc_hdisplay, mode->crtc_hsync_start,
7910                     mode->crtc_hsync_end, mode->crtc_htotal,
7911                     mode->crtc_vdisplay, mode->crtc_vsync_start,
7912                     mode->crtc_vsync_end, mode->crtc_vtotal,
7913                     mode->type, mode->flags);
7914 }
7915
7916 static void
7917 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
7918                       const char *id, unsigned int lane_count,
7919                       const struct intel_link_m_n *m_n)
7920 {
7921         struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
7922
7923         drm_dbg_kms(&i915->drm,
7924                     "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7925                     id, lane_count,
7926                     m_n->gmch_m, m_n->gmch_n,
7927                     m_n->link_m, m_n->link_n, m_n->tu);
7928 }
7929
7930 static void
7931 intel_dump_infoframe(struct drm_i915_private *dev_priv,
7932                      const union hdmi_infoframe *frame)
7933 {
7934         if (!drm_debug_enabled(DRM_UT_KMS))
7935                 return;
7936
7937         hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
7938 }
7939
7940 static void
7941 intel_dump_dp_vsc_sdp(struct drm_i915_private *dev_priv,
7942                       const struct drm_dp_vsc_sdp *vsc)
7943 {
7944         if (!drm_debug_enabled(DRM_UT_KMS))
7945                 return;
7946
7947         drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, vsc);
7948 }
7949
7950 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
7951
7952 static const char * const output_type_str[] = {
7953         OUTPUT_TYPE(UNUSED),
7954         OUTPUT_TYPE(ANALOG),
7955         OUTPUT_TYPE(DVO),
7956         OUTPUT_TYPE(SDVO),
7957         OUTPUT_TYPE(LVDS),
7958         OUTPUT_TYPE(TVOUT),
7959         OUTPUT_TYPE(HDMI),
7960         OUTPUT_TYPE(DP),
7961         OUTPUT_TYPE(EDP),
7962         OUTPUT_TYPE(DSI),
7963         OUTPUT_TYPE(DDI),
7964         OUTPUT_TYPE(DP_MST),
7965 };
7966
7967 #undef OUTPUT_TYPE
7968
7969 static void snprintf_output_types(char *buf, size_t len,
7970                                   unsigned int output_types)
7971 {
7972         char *str = buf;
7973         int i;
7974
7975         str[0] = '\0';
7976
7977         for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
7978                 int r;
7979
7980                 if ((output_types & BIT(i)) == 0)
7981                         continue;
7982
7983                 r = snprintf(str, len, "%s%s",
7984                              str != buf ? "," : "", output_type_str[i]);
7985                 if (r >= len)
7986                         break;
7987                 str += r;
7988                 len -= r;
7989
7990                 output_types &= ~BIT(i);
7991         }
7992
7993         WARN_ON_ONCE(output_types != 0);
7994 }
7995
7996 static const char * const output_format_str[] = {
7997         [INTEL_OUTPUT_FORMAT_RGB] = "RGB",
7998         [INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
7999         [INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
8000 };
8001
8002 static const char *output_formats(enum intel_output_format format)
8003 {
8004         if (format >= ARRAY_SIZE(output_format_str))
8005                 return "invalid";
8006         return output_format_str[format];
8007 }
8008
8009 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
8010 {
8011         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
8012         struct drm_i915_private *i915 = to_i915(plane->base.dev);
8013         const struct drm_framebuffer *fb = plane_state->hw.fb;
8014         struct drm_format_name_buf format_name;
8015
8016         if (!fb) {
8017                 drm_dbg_kms(&i915->drm,
8018                             "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
8019                             plane->base.base.id, plane->base.name,
8020                             yesno(plane_state->uapi.visible));
8021                 return;
8022         }
8023
8024         drm_dbg_kms(&i915->drm,
8025                     "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %s modifier = 0x%llx, visible: %s\n",
8026                     plane->base.base.id, plane->base.name,
8027                     fb->base.id, fb->width, fb->height,
8028                     drm_get_format_name(fb->format->format, &format_name),
8029                     fb->modifier, yesno(plane_state->uapi.visible));
8030         drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
8031                     plane_state->hw.rotation, plane_state->scaler_id);
8032         if (plane_state->uapi.visible)
8033                 drm_dbg_kms(&i915->drm,
8034                             "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
8035                             DRM_RECT_FP_ARG(&plane_state->uapi.src),
8036                             DRM_RECT_ARG(&plane_state->uapi.dst));
8037 }
8038
8039 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
8040                                    struct intel_atomic_state *state,
8041                                    const char *context)
8042 {
8043         struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
8044         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8045         const struct intel_plane_state *plane_state;
8046         struct intel_plane *plane;
8047         char buf[64];
8048         int i;
8049
8050         drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
8051                     crtc->base.base.id, crtc->base.name,
8052                     yesno(pipe_config->hw.enable), context);
8053
8054         if (!pipe_config->hw.enable)
8055                 goto dump_planes;
8056
8057         snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
8058         drm_dbg_kms(&dev_priv->drm,
8059                     "active: %s, output_types: %s (0x%x), output format: %s\n",
8060                     yesno(pipe_config->hw.active),
8061                     buf, pipe_config->output_types,
8062                     output_formats(pipe_config->output_format));
8063
8064         drm_dbg_kms(&dev_priv->drm,
8065                     "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
8066                     transcoder_name(pipe_config->cpu_transcoder),
8067                     pipe_config->pipe_bpp, pipe_config->dither);
8068
8069         drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
8070                     transcoder_name(pipe_config->mst_master_transcoder));
8071
8072         drm_dbg_kms(&dev_priv->drm,
8073                     "port sync: master transcoder: %s, slave transcoder bitmask = 0x%x\n",
8074                     transcoder_name(pipe_config->master_transcoder),
8075                     pipe_config->sync_mode_slaves_mask);
8076
8077         drm_dbg_kms(&dev_priv->drm, "bigjoiner: %s\n",
8078                     pipe_config->bigjoiner_slave ? "slave" :
8079                     pipe_config->bigjoiner ? "master" : "no");
8080
8081         drm_dbg_kms(&dev_priv->drm, "splitter: %s, link count %d, overlap %d\n",
8082                     enableddisabled(pipe_config->splitter.enable),
8083                     pipe_config->splitter.link_count,
8084                     pipe_config->splitter.pixel_overlap);
8085
8086         if (pipe_config->has_pch_encoder)
8087                 intel_dump_m_n_config(pipe_config, "fdi",
8088                                       pipe_config->fdi_lanes,
8089                                       &pipe_config->fdi_m_n);
8090
8091         if (intel_crtc_has_dp_encoder(pipe_config)) {
8092                 intel_dump_m_n_config(pipe_config, "dp m_n",
8093                                 pipe_config->lane_count, &pipe_config->dp_m_n);
8094                 if (pipe_config->has_drrs)
8095                         intel_dump_m_n_config(pipe_config, "dp m2_n2",
8096                                               pipe_config->lane_count,
8097                                               &pipe_config->dp_m2_n2);
8098         }
8099
8100         drm_dbg_kms(&dev_priv->drm,
8101                     "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
8102                     pipe_config->has_audio, pipe_config->has_infoframe,
8103                     pipe_config->infoframes.enable);
8104
8105         if (pipe_config->infoframes.enable &
8106             intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
8107                 drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
8108                             pipe_config->infoframes.gcp);
8109         if (pipe_config->infoframes.enable &
8110             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
8111                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
8112         if (pipe_config->infoframes.enable &
8113             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
8114                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
8115         if (pipe_config->infoframes.enable &
8116             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
8117                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
8118         if (pipe_config->infoframes.enable &
8119             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM))
8120                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
8121         if (pipe_config->infoframes.enable &
8122             intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
8123                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
8124         if (pipe_config->infoframes.enable &
8125             intel_hdmi_infoframe_enable(DP_SDP_VSC))
8126                 intel_dump_dp_vsc_sdp(dev_priv, &pipe_config->infoframes.vsc);
8127
8128         drm_dbg_kms(&dev_priv->drm, "vrr: %s, vmin: %d, vmax: %d, pipeline full: %d, flipline: %d, vmin vblank: %d, vmax vblank: %d\n",
8129                     yesno(pipe_config->vrr.enable),
8130                     pipe_config->vrr.vmin, pipe_config->vrr.vmax,
8131                     pipe_config->vrr.pipeline_full, pipe_config->vrr.flipline,
8132                     intel_vrr_vmin_vblank_start(pipe_config),
8133                     intel_vrr_vmax_vblank_start(pipe_config));
8134
8135         drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
8136         drm_mode_debug_printmodeline(&pipe_config->hw.mode);
8137         drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
8138         drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
8139         intel_dump_crtc_timings(dev_priv, &pipe_config->hw.adjusted_mode);
8140         drm_dbg_kms(&dev_priv->drm, "pipe mode:\n");
8141         drm_mode_debug_printmodeline(&pipe_config->hw.pipe_mode);
8142         intel_dump_crtc_timings(dev_priv, &pipe_config->hw.pipe_mode);
8143         drm_dbg_kms(&dev_priv->drm,
8144                     "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
8145                     pipe_config->port_clock,
8146                     pipe_config->pipe_src_w, pipe_config->pipe_src_h,
8147                     pipe_config->pixel_rate);
8148
8149         drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
8150                     pipe_config->linetime, pipe_config->ips_linetime);
8151
8152         if (INTEL_GEN(dev_priv) >= 9)
8153                 drm_dbg_kms(&dev_priv->drm,
8154                             "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
8155                             crtc->num_scalers,
8156                             pipe_config->scaler_state.scaler_users,
8157                             pipe_config->scaler_state.scaler_id);
8158
8159         if (HAS_GMCH(dev_priv))
8160                 drm_dbg_kms(&dev_priv->drm,
8161                             "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8162                             pipe_config->gmch_pfit.control,
8163                             pipe_config->gmch_pfit.pgm_ratios,
8164                             pipe_config->gmch_pfit.lvds_border_bits);
8165         else
8166                 drm_dbg_kms(&dev_priv->drm,
8167                             "pch pfit: " DRM_RECT_FMT ", %s, force thru: %s\n",
8168                             DRM_RECT_ARG(&pipe_config->pch_pfit.dst),
8169                             enableddisabled(pipe_config->pch_pfit.enabled),
8170                             yesno(pipe_config->pch_pfit.force_thru));
8171
8172         drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
8173                     pipe_config->ips_enabled, pipe_config->double_wide);
8174
8175         intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
8176
8177         if (IS_CHERRYVIEW(dev_priv))
8178                 drm_dbg_kms(&dev_priv->drm,
8179                             "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
8180                             pipe_config->cgm_mode, pipe_config->gamma_mode,
8181                             pipe_config->gamma_enable, pipe_config->csc_enable);
8182         else
8183                 drm_dbg_kms(&dev_priv->drm,
8184                             "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
8185                             pipe_config->csc_mode, pipe_config->gamma_mode,
8186                             pipe_config->gamma_enable, pipe_config->csc_enable);
8187
8188         drm_dbg_kms(&dev_priv->drm, "degamma lut: %d entries, gamma lut: %d entries\n",
8189                     pipe_config->hw.degamma_lut ?
8190                     drm_color_lut_size(pipe_config->hw.degamma_lut) : 0,
8191                     pipe_config->hw.gamma_lut ?
8192                     drm_color_lut_size(pipe_config->hw.gamma_lut) : 0);
8193
8194 dump_planes:
8195         if (!state)
8196                 return;
8197
8198         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
8199                 if (plane->pipe == crtc->pipe)
8200                         intel_dump_plane_state(plane_state);
8201         }
8202 }
8203
8204 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
8205 {
8206         struct drm_device *dev = state->base.dev;
8207         struct drm_connector *connector;
8208         struct drm_connector_list_iter conn_iter;
8209         unsigned int used_ports = 0;
8210         unsigned int used_mst_ports = 0;
8211         bool ret = true;
8212
8213         /*
8214          * We're going to peek into connector->state,
8215          * hence connection_mutex must be held.
8216          */
8217         drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
8218
8219         /*
8220          * Walk the connector list instead of the encoder
8221          * list to detect the problem on ddi platforms
8222          * where there's just one encoder per digital port.
8223          */
8224         drm_connector_list_iter_begin(dev, &conn_iter);
8225         drm_for_each_connector_iter(connector, &conn_iter) {
8226                 struct drm_connector_state *connector_state;
8227                 struct intel_encoder *encoder;
8228
8229                 connector_state =
8230                         drm_atomic_get_new_connector_state(&state->base,
8231                                                            connector);
8232                 if (!connector_state)
8233                         connector_state = connector->state;
8234
8235                 if (!connector_state->best_encoder)
8236                         continue;
8237
8238                 encoder = to_intel_encoder(connector_state->best_encoder);
8239
8240                 drm_WARN_ON(dev, !connector_state->crtc);
8241
8242                 switch (encoder->type) {
8243                 case INTEL_OUTPUT_DDI:
8244                         if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
8245                                 break;
8246                         fallthrough;
8247                 case INTEL_OUTPUT_DP:
8248                 case INTEL_OUTPUT_HDMI:
8249                 case INTEL_OUTPUT_EDP:
8250                         /* the same port mustn't appear more than once */
8251                         if (used_ports & BIT(encoder->port))
8252                                 ret = false;
8253
8254                         used_ports |= BIT(encoder->port);
8255                         break;
8256                 case INTEL_OUTPUT_DP_MST:
8257                         used_mst_ports |=
8258                                 1 << encoder->port;
8259                         break;
8260                 default:
8261                         break;
8262                 }
8263         }
8264         drm_connector_list_iter_end(&conn_iter);
8265
8266         /* can't mix MST and SST/HDMI on the same port */
8267         if (used_ports & used_mst_ports)
8268                 return false;
8269
8270         return ret;
8271 }
8272
8273 static void
8274 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_atomic_state *state,
8275                                            struct intel_crtc_state *crtc_state)
8276 {
8277         const struct intel_crtc_state *from_crtc_state = crtc_state;
8278
8279         if (crtc_state->bigjoiner_slave) {
8280                 from_crtc_state = intel_atomic_get_new_crtc_state(state,
8281                                                                   crtc_state->bigjoiner_linked_crtc);
8282
8283                 /* No need to copy state if the master state is unchanged */
8284                 if (!from_crtc_state)
8285                         return;
8286         }
8287
8288         intel_crtc_copy_color_blobs(crtc_state, from_crtc_state);
8289 }
8290
8291 static void
8292 intel_crtc_copy_uapi_to_hw_state(struct intel_atomic_state *state,
8293                                  struct intel_crtc_state *crtc_state)
8294 {
8295         crtc_state->hw.enable = crtc_state->uapi.enable;
8296         crtc_state->hw.active = crtc_state->uapi.active;
8297         crtc_state->hw.mode = crtc_state->uapi.mode;
8298         crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
8299         crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
8300
8301         intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc_state);
8302 }
8303
8304 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
8305 {
8306         if (crtc_state->bigjoiner_slave)
8307                 return;
8308
8309         crtc_state->uapi.enable = crtc_state->hw.enable;
8310         crtc_state->uapi.active = crtc_state->hw.active;
8311         drm_WARN_ON(crtc_state->uapi.crtc->dev,
8312                     drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
8313
8314         crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
8315         crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
8316
8317         /* copy color blobs to uapi */
8318         drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
8319                                   crtc_state->hw.degamma_lut);
8320         drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
8321                                   crtc_state->hw.gamma_lut);
8322         drm_property_replace_blob(&crtc_state->uapi.ctm,
8323                                   crtc_state->hw.ctm);
8324 }
8325
8326 static int
8327 copy_bigjoiner_crtc_state(struct intel_crtc_state *crtc_state,
8328                           const struct intel_crtc_state *from_crtc_state)
8329 {
8330         struct intel_crtc_state *saved_state;
8331         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8332
8333         saved_state = kmemdup(from_crtc_state, sizeof(*saved_state), GFP_KERNEL);
8334         if (!saved_state)
8335                 return -ENOMEM;
8336
8337         saved_state->uapi = crtc_state->uapi;
8338         saved_state->scaler_state = crtc_state->scaler_state;
8339         saved_state->shared_dpll = crtc_state->shared_dpll;
8340         saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
8341         saved_state->crc_enabled = crtc_state->crc_enabled;
8342
8343         intel_crtc_free_hw_state(crtc_state);
8344         memcpy(crtc_state, saved_state, sizeof(*crtc_state));
8345         kfree(saved_state);
8346
8347         /* Re-init hw state */
8348         memset(&crtc_state->hw, 0, sizeof(saved_state->hw));
8349         crtc_state->hw.enable = from_crtc_state->hw.enable;
8350         crtc_state->hw.active = from_crtc_state->hw.active;
8351         crtc_state->hw.pipe_mode = from_crtc_state->hw.pipe_mode;
8352         crtc_state->hw.adjusted_mode = from_crtc_state->hw.adjusted_mode;
8353
8354         /* Some fixups */
8355         crtc_state->uapi.mode_changed = from_crtc_state->uapi.mode_changed;
8356         crtc_state->uapi.connectors_changed = from_crtc_state->uapi.connectors_changed;
8357         crtc_state->uapi.active_changed = from_crtc_state->uapi.active_changed;
8358         crtc_state->nv12_planes = crtc_state->c8_planes = crtc_state->update_planes = 0;
8359         crtc_state->bigjoiner_linked_crtc = to_intel_crtc(from_crtc_state->uapi.crtc);
8360         crtc_state->bigjoiner_slave = true;
8361         crtc_state->cpu_transcoder = (enum transcoder)crtc->pipe;
8362         crtc_state->has_audio = false;
8363
8364         return 0;
8365 }
8366
8367 static int
8368 intel_crtc_prepare_cleared_state(struct intel_atomic_state *state,
8369                                  struct intel_crtc_state *crtc_state)
8370 {
8371         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8372         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8373         struct intel_crtc_state *saved_state;
8374
8375         saved_state = intel_crtc_state_alloc(crtc);
8376         if (!saved_state)
8377                 return -ENOMEM;
8378
8379         /* free the old crtc_state->hw members */
8380         intel_crtc_free_hw_state(crtc_state);
8381
8382         /* FIXME: before the switch to atomic started, a new pipe_config was
8383          * kzalloc'd. Code that depends on any field being zero should be
8384          * fixed, so that the crtc_state can be safely duplicated. For now,
8385          * only fields that are know to not cause problems are preserved. */
8386
8387         saved_state->uapi = crtc_state->uapi;
8388         saved_state->scaler_state = crtc_state->scaler_state;
8389         saved_state->shared_dpll = crtc_state->shared_dpll;
8390         saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
8391         memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
8392                sizeof(saved_state->icl_port_dplls));
8393         saved_state->crc_enabled = crtc_state->crc_enabled;
8394         if (IS_G4X(dev_priv) ||
8395             IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
8396                 saved_state->wm = crtc_state->wm;
8397
8398         memcpy(crtc_state, saved_state, sizeof(*crtc_state));
8399         kfree(saved_state);
8400
8401         intel_crtc_copy_uapi_to_hw_state(state, crtc_state);
8402
8403         return 0;
8404 }
8405
8406 static int
8407 intel_modeset_pipe_config(struct intel_atomic_state *state,
8408                           struct intel_crtc_state *pipe_config)
8409 {
8410         struct drm_crtc *crtc = pipe_config->uapi.crtc;
8411         struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
8412         struct drm_connector *connector;
8413         struct drm_connector_state *connector_state;
8414         int base_bpp, ret, i;
8415         bool retry = true;
8416
8417         pipe_config->cpu_transcoder =
8418                 (enum transcoder) to_intel_crtc(crtc)->pipe;
8419
8420         /*
8421          * Sanitize sync polarity flags based on requested ones. If neither
8422          * positive or negative polarity is requested, treat this as meaning
8423          * negative polarity.
8424          */
8425         if (!(pipe_config->hw.adjusted_mode.flags &
8426               (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8427                 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8428
8429         if (!(pipe_config->hw.adjusted_mode.flags &
8430               (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8431                 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8432
8433         ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8434                                         pipe_config);
8435         if (ret)
8436                 return ret;
8437
8438         base_bpp = pipe_config->pipe_bpp;
8439
8440         /*
8441          * Determine the real pipe dimensions. Note that stereo modes can
8442          * increase the actual pipe size due to the frame doubling and
8443          * insertion of additional space for blanks between the frame. This
8444          * is stored in the crtc timings. We use the requested mode to do this
8445          * computation to clearly distinguish it from the adjusted mode, which
8446          * can be changed by the connectors in the below retry loop.
8447          */
8448         drm_mode_get_hv_timing(&pipe_config->hw.mode,
8449                                &pipe_config->pipe_src_w,
8450                                &pipe_config->pipe_src_h);
8451
8452         for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
8453                 struct intel_encoder *encoder =
8454                         to_intel_encoder(connector_state->best_encoder);
8455
8456                 if (connector_state->crtc != crtc)
8457                         continue;
8458
8459                 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
8460                         drm_dbg_kms(&i915->drm,
8461                                     "rejecting invalid cloning configuration\n");
8462                         return -EINVAL;
8463                 }
8464
8465                 /*
8466                  * Determine output_types before calling the .compute_config()
8467                  * hooks so that the hooks can use this information safely.
8468                  */
8469                 if (encoder->compute_output_type)
8470                         pipe_config->output_types |=
8471                                 BIT(encoder->compute_output_type(encoder, pipe_config,
8472                                                                  connector_state));
8473                 else
8474                         pipe_config->output_types |= BIT(encoder->type);
8475         }
8476
8477 encoder_retry:
8478         /* Ensure the port clock defaults are reset when retrying. */
8479         pipe_config->port_clock = 0;
8480         pipe_config->pixel_multiplier = 1;
8481
8482         /* Fill in default crtc timings, allow encoders to overwrite them. */
8483         drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
8484                               CRTC_STEREO_DOUBLE);
8485
8486         /* Pass our mode to the connectors and the CRTC to give them a chance to
8487          * adjust it according to limitations or connector properties, and also
8488          * a chance to reject the mode entirely.
8489          */
8490         for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
8491                 struct intel_encoder *encoder =
8492                         to_intel_encoder(connector_state->best_encoder);
8493
8494                 if (connector_state->crtc != crtc)
8495                         continue;
8496
8497                 ret = encoder->compute_config(encoder, pipe_config,
8498                                               connector_state);
8499                 if (ret < 0) {
8500                         if (ret != -EDEADLK)
8501                                 drm_dbg_kms(&i915->drm,
8502                                             "Encoder config failure: %d\n",
8503                                             ret);
8504                         return ret;
8505                 }
8506         }
8507
8508         /* Set default port clock if not overwritten by the encoder. Needs to be
8509          * done afterwards in case the encoder adjusts the mode. */
8510         if (!pipe_config->port_clock)
8511                 pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
8512                         * pipe_config->pixel_multiplier;
8513
8514         ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
8515         if (ret == -EDEADLK)
8516                 return ret;
8517         if (ret < 0) {
8518                 drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
8519                 return ret;
8520         }
8521
8522         if (ret == I915_DISPLAY_CONFIG_RETRY) {
8523                 if (drm_WARN(&i915->drm, !retry,
8524                              "loop in pipe configuration computation\n"))
8525                         return -EINVAL;
8526
8527                 drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
8528                 retry = false;
8529                 goto encoder_retry;
8530         }
8531
8532         /* Dithering seems to not pass-through bits correctly when it should, so
8533          * only enable it on 6bpc panels and when its not a compliance
8534          * test requesting 6bpc video pattern.
8535          */
8536         pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
8537                 !pipe_config->dither_force_disable;
8538         drm_dbg_kms(&i915->drm,
8539                     "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
8540                     base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8541
8542         return 0;
8543 }
8544
8545 static int
8546 intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
8547 {
8548         struct intel_atomic_state *state =
8549                 to_intel_atomic_state(crtc_state->uapi.state);
8550         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8551         struct drm_connector_state *conn_state;
8552         struct drm_connector *connector;
8553         int i;
8554
8555         for_each_new_connector_in_state(&state->base, connector,
8556                                         conn_state, i) {
8557                 struct intel_encoder *encoder =
8558                         to_intel_encoder(conn_state->best_encoder);
8559                 int ret;
8560
8561                 if (conn_state->crtc != &crtc->base ||
8562                     !encoder->compute_config_late)
8563                         continue;
8564
8565                 ret = encoder->compute_config_late(encoder, crtc_state,
8566                                                    conn_state);
8567                 if (ret)
8568                         return ret;
8569         }
8570
8571         return 0;
8572 }
8573
8574 bool intel_fuzzy_clock_check(int clock1, int clock2)
8575 {
8576         int diff;
8577
8578         if (clock1 == clock2)
8579                 return true;
8580
8581         if (!clock1 || !clock2)
8582                 return false;
8583
8584         diff = abs(clock1 - clock2);
8585
8586         if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8587                 return true;
8588
8589         return false;
8590 }
8591
8592 static bool
8593 intel_compare_m_n(unsigned int m, unsigned int n,
8594                   unsigned int m2, unsigned int n2,
8595                   bool exact)
8596 {
8597         if (m == m2 && n == n2)
8598                 return true;
8599
8600         if (exact || !m || !n || !m2 || !n2)
8601                 return false;
8602
8603         BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
8604
8605         if (n > n2) {
8606                 while (n > n2) {
8607                         m2 <<= 1;
8608                         n2 <<= 1;
8609                 }
8610         } else if (n < n2) {
8611                 while (n < n2) {
8612                         m <<= 1;
8613                         n <<= 1;
8614                 }
8615         }
8616
8617         if (n != n2)
8618                 return false;
8619
8620         return intel_fuzzy_clock_check(m, m2);
8621 }
8622
8623 static bool
8624 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
8625                        const struct intel_link_m_n *m2_n2,
8626                        bool exact)
8627 {
8628         return m_n->tu == m2_n2->tu &&
8629                 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
8630                                   m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
8631                 intel_compare_m_n(m_n->link_m, m_n->link_n,
8632                                   m2_n2->link_m, m2_n2->link_n, exact);
8633 }
8634
8635 static bool
8636 intel_compare_infoframe(const union hdmi_infoframe *a,
8637                         const union hdmi_infoframe *b)
8638 {
8639         return memcmp(a, b, sizeof(*a)) == 0;
8640 }
8641
8642 static bool
8643 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
8644                          const struct drm_dp_vsc_sdp *b)
8645 {
8646         return memcmp(a, b, sizeof(*a)) == 0;
8647 }
8648
8649 static void
8650 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
8651                                bool fastset, const char *name,
8652                                const union hdmi_infoframe *a,
8653                                const union hdmi_infoframe *b)
8654 {
8655         if (fastset) {
8656                 if (!drm_debug_enabled(DRM_UT_KMS))
8657                         return;
8658
8659                 drm_dbg_kms(&dev_priv->drm,
8660                             "fastset mismatch in %s infoframe\n", name);
8661                 drm_dbg_kms(&dev_priv->drm, "expected:\n");
8662                 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
8663                 drm_dbg_kms(&dev_priv->drm, "found:\n");
8664                 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
8665         } else {
8666                 drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
8667                 drm_err(&dev_priv->drm, "expected:\n");
8668                 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
8669                 drm_err(&dev_priv->drm, "found:\n");
8670                 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
8671         }
8672 }
8673
8674 static void
8675 pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
8676                                 bool fastset, const char *name,
8677                                 const struct drm_dp_vsc_sdp *a,
8678                                 const struct drm_dp_vsc_sdp *b)
8679 {
8680         if (fastset) {
8681                 if (!drm_debug_enabled(DRM_UT_KMS))
8682                         return;
8683
8684                 drm_dbg_kms(&dev_priv->drm,
8685                             "fastset mismatch in %s dp sdp\n", name);
8686                 drm_dbg_kms(&dev_priv->drm, "expected:\n");
8687                 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
8688                 drm_dbg_kms(&dev_priv->drm, "found:\n");
8689                 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
8690         } else {
8691                 drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
8692                 drm_err(&dev_priv->drm, "expected:\n");
8693                 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
8694                 drm_err(&dev_priv->drm, "found:\n");
8695                 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
8696         }
8697 }
8698
8699 static void __printf(4, 5)
8700 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
8701                      const char *name, const char *format, ...)
8702 {
8703         struct drm_i915_private *i915 = to_i915(crtc->base.dev);
8704         struct va_format vaf;
8705         va_list args;
8706
8707         va_start(args, format);
8708         vaf.fmt = format;
8709         vaf.va = &args;
8710
8711         if (fastset)
8712                 drm_dbg_kms(&i915->drm,
8713                             "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
8714                             crtc->base.base.id, crtc->base.name, name, &vaf);
8715         else
8716                 drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
8717                         crtc->base.base.id, crtc->base.name, name, &vaf);
8718
8719         va_end(args);
8720 }
8721
8722 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
8723 {
8724         if (dev_priv->params.fastboot != -1)
8725                 return dev_priv->params.fastboot;
8726
8727         /* Enable fastboot by default on Skylake and newer */
8728         if (INTEL_GEN(dev_priv) >= 9)
8729                 return true;
8730
8731         /* Enable fastboot by default on VLV and CHV */
8732         if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
8733                 return true;
8734
8735         /* Disabled by default on all others */
8736         return false;
8737 }
8738
8739 static bool
8740 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
8741                           const struct intel_crtc_state *pipe_config,
8742                           bool fastset)
8743 {
8744         struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
8745         struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
8746         bool ret = true;
8747         u32 bp_gamma = 0;
8748         bool fixup_inherited = fastset &&
8749                 current_config->inherited && !pipe_config->inherited;
8750
8751         if (fixup_inherited && !fastboot_enabled(dev_priv)) {
8752                 drm_dbg_kms(&dev_priv->drm,
8753                             "initial modeset and fastboot not set\n");
8754                 ret = false;
8755         }
8756
8757 #define PIPE_CONF_CHECK_X(name) do { \
8758         if (current_config->name != pipe_config->name) { \
8759                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8760                                      "(expected 0x%08x, found 0x%08x)", \
8761                                      current_config->name, \
8762                                      pipe_config->name); \
8763                 ret = false; \
8764         } \
8765 } while (0)
8766
8767 #define PIPE_CONF_CHECK_I(name) do { \
8768         if (current_config->name != pipe_config->name) { \
8769                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8770                                      "(expected %i, found %i)", \
8771                                      current_config->name, \
8772                                      pipe_config->name); \
8773                 ret = false; \
8774         } \
8775 } while (0)
8776
8777 #define PIPE_CONF_CHECK_BOOL(name) do { \
8778         if (current_config->name != pipe_config->name) { \
8779                 pipe_config_mismatch(fastset, crtc,  __stringify(name), \
8780                                      "(expected %s, found %s)", \
8781                                      yesno(current_config->name), \
8782                                      yesno(pipe_config->name)); \
8783                 ret = false; \
8784         } \
8785 } while (0)
8786
8787 /*
8788  * Checks state where we only read out the enabling, but not the entire
8789  * state itself (like full infoframes or ELD for audio). These states
8790  * require a full modeset on bootup to fix up.
8791  */
8792 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
8793         if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
8794                 PIPE_CONF_CHECK_BOOL(name); \
8795         } else { \
8796                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8797                                      "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
8798                                      yesno(current_config->name), \
8799                                      yesno(pipe_config->name)); \
8800                 ret = false; \
8801         } \
8802 } while (0)
8803
8804 #define PIPE_CONF_CHECK_P(name) do { \
8805         if (current_config->name != pipe_config->name) { \
8806                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8807                                      "(expected %p, found %p)", \
8808                                      current_config->name, \
8809                                      pipe_config->name); \
8810                 ret = false; \
8811         } \
8812 } while (0)
8813
8814 #define PIPE_CONF_CHECK_M_N(name) do { \
8815         if (!intel_compare_link_m_n(&current_config->name, \
8816                                     &pipe_config->name,\
8817                                     !fastset)) { \
8818                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8819                                      "(expected tu %i gmch %i/%i link %i/%i, " \
8820                                      "found tu %i, gmch %i/%i link %i/%i)", \
8821                                      current_config->name.tu, \
8822                                      current_config->name.gmch_m, \
8823                                      current_config->name.gmch_n, \
8824                                      current_config->name.link_m, \
8825                                      current_config->name.link_n, \
8826                                      pipe_config->name.tu, \
8827                                      pipe_config->name.gmch_m, \
8828                                      pipe_config->name.gmch_n, \
8829                                      pipe_config->name.link_m, \
8830                                      pipe_config->name.link_n); \
8831                 ret = false; \
8832         } \
8833 } while (0)
8834
8835 /* This is required for BDW+ where there is only one set of registers for
8836  * switching between high and low RR.
8837  * This macro can be used whenever a comparison has to be made between one
8838  * hw state and multiple sw state variables.
8839  */
8840 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
8841         if (!intel_compare_link_m_n(&current_config->name, \
8842                                     &pipe_config->name, !fastset) && \
8843             !intel_compare_link_m_n(&current_config->alt_name, \
8844                                     &pipe_config->name, !fastset)) { \
8845                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8846                                      "(expected tu %i gmch %i/%i link %i/%i, " \
8847                                      "or tu %i gmch %i/%i link %i/%i, " \
8848                                      "found tu %i, gmch %i/%i link %i/%i)", \
8849                                      current_config->name.tu, \
8850                                      current_config->name.gmch_m, \
8851                                      current_config->name.gmch_n, \
8852                                      current_config->name.link_m, \
8853                                      current_config->name.link_n, \
8854                                      current_config->alt_name.tu, \
8855                                      current_config->alt_name.gmch_m, \
8856                                      current_config->alt_name.gmch_n, \
8857                                      current_config->alt_name.link_m, \
8858                                      current_config->alt_name.link_n, \
8859                                      pipe_config->name.tu, \
8860                                      pipe_config->name.gmch_m, \
8861                                      pipe_config->name.gmch_n, \
8862                                      pipe_config->name.link_m, \
8863                                      pipe_config->name.link_n); \
8864                 ret = false; \
8865         } \
8866 } while (0)
8867
8868 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
8869         if ((current_config->name ^ pipe_config->name) & (mask)) { \
8870                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8871                                      "(%x) (expected %i, found %i)", \
8872                                      (mask), \
8873                                      current_config->name & (mask), \
8874                                      pipe_config->name & (mask)); \
8875                 ret = false; \
8876         } \
8877 } while (0)
8878
8879 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
8880         if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
8881                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8882                                      "(expected %i, found %i)", \
8883                                      current_config->name, \
8884                                      pipe_config->name); \
8885                 ret = false; \
8886         } \
8887 } while (0)
8888
8889 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
8890         if (!intel_compare_infoframe(&current_config->infoframes.name, \
8891                                      &pipe_config->infoframes.name)) { \
8892                 pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
8893                                                &current_config->infoframes.name, \
8894                                                &pipe_config->infoframes.name); \
8895                 ret = false; \
8896         } \
8897 } while (0)
8898
8899 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
8900         if (!current_config->has_psr && !pipe_config->has_psr && \
8901             !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
8902                                       &pipe_config->infoframes.name)) { \
8903                 pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
8904                                                 &current_config->infoframes.name, \
8905                                                 &pipe_config->infoframes.name); \
8906                 ret = false; \
8907         } \
8908 } while (0)
8909
8910 #define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
8911         if (current_config->name1 != pipe_config->name1) { \
8912                 pipe_config_mismatch(fastset, crtc, __stringify(name1), \
8913                                 "(expected %i, found %i, won't compare lut values)", \
8914                                 current_config->name1, \
8915                                 pipe_config->name1); \
8916                 ret = false;\
8917         } else { \
8918                 if (!intel_color_lut_equal(current_config->name2, \
8919                                         pipe_config->name2, pipe_config->name1, \
8920                                         bit_precision)) { \
8921                         pipe_config_mismatch(fastset, crtc, __stringify(name2), \
8922                                         "hw_state doesn't match sw_state"); \
8923                         ret = false; \
8924                 } \
8925         } \
8926 } while (0)
8927
8928 #define PIPE_CONF_QUIRK(quirk) \
8929         ((current_config->quirks | pipe_config->quirks) & (quirk))
8930
8931         PIPE_CONF_CHECK_I(cpu_transcoder);
8932
8933         PIPE_CONF_CHECK_BOOL(has_pch_encoder);
8934         PIPE_CONF_CHECK_I(fdi_lanes);
8935         PIPE_CONF_CHECK_M_N(fdi_m_n);
8936
8937         PIPE_CONF_CHECK_I(lane_count);
8938         PIPE_CONF_CHECK_X(lane_lat_optim_mask);
8939
8940         if (INTEL_GEN(dev_priv) < 8) {
8941                 PIPE_CONF_CHECK_M_N(dp_m_n);
8942
8943                 if (current_config->has_drrs)
8944                         PIPE_CONF_CHECK_M_N(dp_m2_n2);
8945         } else
8946                 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
8947
8948         PIPE_CONF_CHECK_X(output_types);
8949
8950         /* FIXME do the readout properly and get rid of this quirk */
8951         if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
8952                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hdisplay);
8953                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_htotal);
8954                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_start);
8955                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_end);
8956                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_start);
8957                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_end);
8958
8959                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vdisplay);
8960                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vtotal);
8961                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_start);
8962                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_end);
8963                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_start);
8964                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_end);
8965
8966                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
8967                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
8968                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
8969                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
8970                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
8971                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
8972
8973                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
8974                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
8975                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
8976                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
8977                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
8978                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
8979
8980                 PIPE_CONF_CHECK_I(pixel_multiplier);
8981
8982                 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8983                                       DRM_MODE_FLAG_INTERLACE);
8984
8985                 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8986                         PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8987                                               DRM_MODE_FLAG_PHSYNC);
8988                         PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8989                                               DRM_MODE_FLAG_NHSYNC);
8990                         PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8991                                               DRM_MODE_FLAG_PVSYNC);
8992                         PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8993                                               DRM_MODE_FLAG_NVSYNC);
8994                 }
8995         }
8996
8997         PIPE_CONF_CHECK_I(output_format);
8998         PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
8999         if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
9000             IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
9001                 PIPE_CONF_CHECK_BOOL(limited_color_range);
9002
9003         PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
9004         PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
9005         PIPE_CONF_CHECK_BOOL(has_infoframe);
9006         /* FIXME do the readout properly and get rid of this quirk */
9007         if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
9008                 PIPE_CONF_CHECK_BOOL(fec_enable);
9009
9010         PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
9011
9012         PIPE_CONF_CHECK_X(gmch_pfit.control);
9013         /* pfit ratios are autocomputed by the hw on gen4+ */
9014         if (INTEL_GEN(dev_priv) < 4)
9015                 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
9016         PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9017
9018         /*
9019          * Changing the EDP transcoder input mux
9020          * (A_ONOFF vs. A_ON) requires a full modeset.
9021          */
9022         PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
9023
9024         if (!fastset) {
9025                 PIPE_CONF_CHECK_I(pipe_src_w);
9026                 PIPE_CONF_CHECK_I(pipe_src_h);
9027
9028                 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
9029                 if (current_config->pch_pfit.enabled) {
9030                         PIPE_CONF_CHECK_I(pch_pfit.dst.x1);
9031                         PIPE_CONF_CHECK_I(pch_pfit.dst.y1);
9032                         PIPE_CONF_CHECK_I(pch_pfit.dst.x2);
9033                         PIPE_CONF_CHECK_I(pch_pfit.dst.y2);
9034                 }
9035
9036                 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
9037                 /* FIXME do the readout properly and get rid of this quirk */
9038                 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
9039                         PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
9040
9041                 PIPE_CONF_CHECK_X(gamma_mode);
9042                 if (IS_CHERRYVIEW(dev_priv))
9043                         PIPE_CONF_CHECK_X(cgm_mode);
9044                 else
9045                         PIPE_CONF_CHECK_X(csc_mode);
9046                 PIPE_CONF_CHECK_BOOL(gamma_enable);
9047                 PIPE_CONF_CHECK_BOOL(csc_enable);
9048
9049                 PIPE_CONF_CHECK_I(linetime);
9050                 PIPE_CONF_CHECK_I(ips_linetime);
9051
9052                 bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
9053                 if (bp_gamma)
9054                         PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
9055         }
9056
9057         PIPE_CONF_CHECK_BOOL(double_wide);
9058
9059         PIPE_CONF_CHECK_P(shared_dpll);
9060
9061         /* FIXME do the readout properly and get rid of this quirk */
9062         if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
9063                 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
9064                 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
9065                 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9066                 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
9067                 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
9068                 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
9069                 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
9070                 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
9071                 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
9072                 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
9073                 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
9074                 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
9075                 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
9076                 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
9077                 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
9078                 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
9079                 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
9080                 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
9081                 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
9082                 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
9083                 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
9084                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
9085                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
9086                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
9087                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
9088                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
9089                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
9090                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
9091                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
9092                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
9093                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
9094
9095                 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
9096                 PIPE_CONF_CHECK_X(dsi_pll.div);
9097
9098                 if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
9099                         PIPE_CONF_CHECK_I(pipe_bpp);
9100
9101                 PIPE_CONF_CHECK_CLOCK_FUZZY(hw.pipe_mode.crtc_clock);
9102                 PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
9103                 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
9104
9105                 PIPE_CONF_CHECK_I(min_voltage_level);
9106         }
9107
9108         PIPE_CONF_CHECK_X(infoframes.enable);
9109         PIPE_CONF_CHECK_X(infoframes.gcp);
9110         PIPE_CONF_CHECK_INFOFRAME(avi);
9111         PIPE_CONF_CHECK_INFOFRAME(spd);
9112         PIPE_CONF_CHECK_INFOFRAME(hdmi);
9113         PIPE_CONF_CHECK_INFOFRAME(drm);
9114         PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
9115
9116         PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
9117         PIPE_CONF_CHECK_I(master_transcoder);
9118         PIPE_CONF_CHECK_BOOL(bigjoiner);
9119         PIPE_CONF_CHECK_BOOL(bigjoiner_slave);
9120         PIPE_CONF_CHECK_P(bigjoiner_linked_crtc);
9121
9122         PIPE_CONF_CHECK_I(dsc.compression_enable);
9123         PIPE_CONF_CHECK_I(dsc.dsc_split);
9124         PIPE_CONF_CHECK_I(dsc.compressed_bpp);
9125
9126         PIPE_CONF_CHECK_BOOL(splitter.enable);
9127         PIPE_CONF_CHECK_I(splitter.link_count);
9128         PIPE_CONF_CHECK_I(splitter.pixel_overlap);
9129
9130         PIPE_CONF_CHECK_I(mst_master_transcoder);
9131
9132         PIPE_CONF_CHECK_BOOL(vrr.enable);
9133         PIPE_CONF_CHECK_I(vrr.vmin);
9134         PIPE_CONF_CHECK_I(vrr.vmax);
9135         PIPE_CONF_CHECK_I(vrr.flipline);
9136         PIPE_CONF_CHECK_I(vrr.pipeline_full);
9137
9138 #undef PIPE_CONF_CHECK_X
9139 #undef PIPE_CONF_CHECK_I
9140 #undef PIPE_CONF_CHECK_BOOL
9141 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
9142 #undef PIPE_CONF_CHECK_P
9143 #undef PIPE_CONF_CHECK_FLAGS
9144 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
9145 #undef PIPE_CONF_CHECK_COLOR_LUT
9146 #undef PIPE_CONF_QUIRK
9147
9148         return ret;
9149 }
9150
9151 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
9152                                            const struct intel_crtc_state *pipe_config)
9153 {
9154         if (pipe_config->has_pch_encoder) {
9155                 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
9156                                                             &pipe_config->fdi_m_n);
9157                 int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
9158
9159                 /*
9160                  * FDI already provided one idea for the dotclock.
9161                  * Yell if the encoder disagrees.
9162                  */
9163                 drm_WARN(&dev_priv->drm,
9164                          !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
9165                          "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
9166                          fdi_dotclock, dotclock);
9167         }
9168 }
9169
9170 static void verify_wm_state(struct intel_crtc *crtc,
9171                             struct intel_crtc_state *new_crtc_state)
9172 {
9173         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9174         struct skl_hw_state {
9175                 struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
9176                 struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
9177                 struct skl_pipe_wm wm;
9178         } *hw;
9179         const struct skl_pipe_wm *sw_wm = &new_crtc_state->wm.skl.optimal;
9180         int level, max_level = ilk_wm_max_level(dev_priv);
9181         struct intel_plane *plane;
9182         u8 hw_enabled_slices;
9183
9184         if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->hw.active)
9185                 return;
9186
9187         hw = kzalloc(sizeof(*hw), GFP_KERNEL);
9188         if (!hw)
9189                 return;
9190
9191         skl_pipe_wm_get_hw_state(crtc, &hw->wm);
9192
9193         skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
9194
9195         hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
9196
9197         if (INTEL_GEN(dev_priv) >= 11 &&
9198             hw_enabled_slices != dev_priv->dbuf.enabled_slices)
9199                 drm_err(&dev_priv->drm,
9200                         "mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
9201                         dev_priv->dbuf.enabled_slices,
9202                         hw_enabled_slices);
9203
9204         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
9205                 const struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
9206                 const struct skl_wm_level *hw_wm_level, *sw_wm_level;
9207
9208                 /* Watermarks */
9209                 for (level = 0; level <= max_level; level++) {
9210                         hw_wm_level = &hw->wm.planes[plane->id].wm[level];
9211                         sw_wm_level = skl_plane_wm_level(sw_wm, plane->id, level);
9212
9213                         if (skl_wm_level_equals(hw_wm_level, sw_wm_level))
9214                                 continue;
9215
9216                         drm_err(&dev_priv->drm,
9217                                 "[PLANE:%d:%s] mismatch in WM%d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
9218                                 plane->base.base.id, plane->base.name, level,
9219                                 sw_wm_level->plane_en,
9220                                 sw_wm_level->plane_res_b,
9221                                 sw_wm_level->plane_res_l,
9222                                 hw_wm_level->plane_en,
9223                                 hw_wm_level->plane_res_b,
9224                                 hw_wm_level->plane_res_l);
9225                 }
9226
9227                 hw_wm_level = &hw->wm.planes[plane->id].trans_wm;
9228                 sw_wm_level = skl_plane_trans_wm(sw_wm, plane->id);
9229
9230                 if (!skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
9231                         drm_err(&dev_priv->drm,
9232                                 "[PLANE:%d:%s] mismatch in trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
9233                                 plane->base.base.id, plane->base.name,
9234                                 sw_wm_level->plane_en,
9235                                 sw_wm_level->plane_res_b,
9236                                 sw_wm_level->plane_res_l,
9237                                 hw_wm_level->plane_en,
9238                                 hw_wm_level->plane_res_b,
9239                                 hw_wm_level->plane_res_l);
9240                 }
9241
9242                 /* DDB */
9243                 hw_ddb_entry = &hw->ddb_y[plane->id];
9244                 sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane->id];
9245
9246                 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
9247                         drm_err(&dev_priv->drm,
9248                                 "[PLANE:%d:%s] mismatch in DDB (expected (%u,%u), found (%u,%u))\n",
9249                                 plane->base.base.id, plane->base.name,
9250                                 sw_ddb_entry->start, sw_ddb_entry->end,
9251                                 hw_ddb_entry->start, hw_ddb_entry->end);
9252                 }
9253         }
9254
9255         kfree(hw);
9256 }
9257
9258 static void
9259 verify_connector_state(struct intel_atomic_state *state,
9260                        struct intel_crtc *crtc)
9261 {
9262         struct drm_connector *connector;
9263         struct drm_connector_state *new_conn_state;
9264         int i;
9265
9266         for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
9267                 struct drm_encoder *encoder = connector->encoder;
9268                 struct intel_crtc_state *crtc_state = NULL;
9269
9270                 if (new_conn_state->crtc != &crtc->base)
9271                         continue;
9272
9273                 if (crtc)
9274                         crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
9275
9276                 intel_connector_verify_state(crtc_state, new_conn_state);
9277
9278                 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
9279                      "connector's atomic encoder doesn't match legacy encoder\n");
9280         }
9281 }
9282
9283 static void
9284 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
9285 {
9286         struct intel_encoder *encoder;
9287         struct drm_connector *connector;
9288         struct drm_connector_state *old_conn_state, *new_conn_state;
9289         int i;
9290
9291         for_each_intel_encoder(&dev_priv->drm, encoder) {
9292                 bool enabled = false, found = false;
9293                 enum pipe pipe;
9294
9295                 drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
9296                             encoder->base.base.id,
9297                             encoder->base.name);
9298
9299                 for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
9300                                                    new_conn_state, i) {
9301                         if (old_conn_state->best_encoder == &encoder->base)
9302                                 found = true;
9303
9304                         if (new_conn_state->best_encoder != &encoder->base)
9305                                 continue;
9306                         found = enabled = true;
9307
9308                         I915_STATE_WARN(new_conn_state->crtc !=
9309                                         encoder->base.crtc,
9310                              "connector's crtc doesn't match encoder crtc\n");
9311                 }
9312
9313                 if (!found)
9314                         continue;
9315
9316                 I915_STATE_WARN(!!encoder->base.crtc != enabled,
9317                      "encoder's enabled state mismatch "
9318                      "(expected %i, found %i)\n",
9319                      !!encoder->base.crtc, enabled);
9320
9321                 if (!encoder->base.crtc) {
9322                         bool active;
9323
9324                         active = encoder->get_hw_state(encoder, &pipe);
9325                         I915_STATE_WARN(active,
9326                              "encoder detached but still enabled on pipe %c.\n",
9327                              pipe_name(pipe));
9328                 }
9329         }
9330 }
9331
9332 static void
9333 verify_crtc_state(struct intel_crtc *crtc,
9334                   struct intel_crtc_state *old_crtc_state,
9335                   struct intel_crtc_state *new_crtc_state)
9336 {
9337         struct drm_device *dev = crtc->base.dev;
9338         struct drm_i915_private *dev_priv = to_i915(dev);
9339         struct intel_encoder *encoder;
9340         struct intel_crtc_state *pipe_config = old_crtc_state;
9341         struct drm_atomic_state *state = old_crtc_state->uapi.state;
9342         struct intel_crtc *master = crtc;
9343
9344         __drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
9345         intel_crtc_free_hw_state(old_crtc_state);
9346         intel_crtc_state_reset(old_crtc_state, crtc);
9347         old_crtc_state->uapi.state = state;
9348
9349         drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
9350                     crtc->base.name);
9351
9352         pipe_config->hw.enable = new_crtc_state->hw.enable;
9353
9354         intel_crtc_get_pipe_config(pipe_config);
9355
9356         /* we keep both pipes enabled on 830 */
9357         if (IS_I830(dev_priv) && pipe_config->hw.active)
9358                 pipe_config->hw.active = new_crtc_state->hw.active;
9359
9360         I915_STATE_WARN(new_crtc_state->hw.active != pipe_config->hw.active,
9361                         "crtc active state doesn't match with hw state "
9362                         "(expected %i, found %i)\n",
9363                         new_crtc_state->hw.active, pipe_config->hw.active);
9364
9365         I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
9366                         "transitional active state does not match atomic hw state "
9367                         "(expected %i, found %i)\n",
9368                         new_crtc_state->hw.active, crtc->active);
9369
9370         if (new_crtc_state->bigjoiner_slave)
9371                 master = new_crtc_state->bigjoiner_linked_crtc;
9372
9373         for_each_encoder_on_crtc(dev, &master->base, encoder) {
9374                 enum pipe pipe;
9375                 bool active;
9376
9377                 active = encoder->get_hw_state(encoder, &pipe);
9378                 I915_STATE_WARN(active != new_crtc_state->hw.active,
9379                                 "[ENCODER:%i] active %i with crtc active %i\n",
9380                                 encoder->base.base.id, active,
9381                                 new_crtc_state->hw.active);
9382
9383                 I915_STATE_WARN(active && master->pipe != pipe,
9384                                 "Encoder connected to wrong pipe %c\n",
9385                                 pipe_name(pipe));
9386
9387                 if (active)
9388                         intel_encoder_get_config(encoder, pipe_config);
9389         }
9390
9391         if (!new_crtc_state->hw.active)
9392                 return;
9393
9394         intel_pipe_config_sanity_check(dev_priv, pipe_config);
9395
9396         if (!intel_pipe_config_compare(new_crtc_state,
9397                                        pipe_config, false)) {
9398                 I915_STATE_WARN(1, "pipe state doesn't match!\n");
9399                 intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
9400                 intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
9401         }
9402 }
9403
9404 static void
9405 intel_verify_planes(struct intel_atomic_state *state)
9406 {
9407         struct intel_plane *plane;
9408         const struct intel_plane_state *plane_state;
9409         int i;
9410
9411         for_each_new_intel_plane_in_state(state, plane,
9412                                           plane_state, i)
9413                 assert_plane(plane, plane_state->planar_slave ||
9414                              plane_state->uapi.visible);
9415 }
9416
9417 static void
9418 verify_single_dpll_state(struct drm_i915_private *dev_priv,
9419                          struct intel_shared_dpll *pll,
9420                          struct intel_crtc *crtc,
9421                          struct intel_crtc_state *new_crtc_state)
9422 {
9423         struct intel_dpll_hw_state dpll_hw_state;
9424         u8 pipe_mask;
9425         bool active;
9426
9427         memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9428
9429         drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
9430
9431         active = intel_dpll_get_hw_state(dev_priv, pll, &dpll_hw_state);
9432
9433         if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
9434                 I915_STATE_WARN(!pll->on && pll->active_mask,
9435                      "pll in active use but not on in sw tracking\n");
9436                 I915_STATE_WARN(pll->on && !pll->active_mask,
9437                      "pll is on but not used by any active pipe\n");
9438                 I915_STATE_WARN(pll->on != active,
9439                      "pll on state mismatch (expected %i, found %i)\n",
9440                      pll->on, active);
9441         }
9442
9443         if (!crtc) {
9444                 I915_STATE_WARN(pll->active_mask & ~pll->state.pipe_mask,
9445                                 "more active pll users than references: 0x%x vs 0x%x\n",
9446                                 pll->active_mask, pll->state.pipe_mask);
9447
9448                 return;
9449         }
9450
9451         pipe_mask = BIT(crtc->pipe);
9452
9453         if (new_crtc_state->hw.active)
9454                 I915_STATE_WARN(!(pll->active_mask & pipe_mask),
9455                                 "pll active mismatch (expected pipe %c in active mask 0x%x)\n",
9456                                 pipe_name(crtc->pipe), pll->active_mask);
9457         else
9458                 I915_STATE_WARN(pll->active_mask & pipe_mask,
9459                                 "pll active mismatch (didn't expect pipe %c in active mask 0x%x)\n",
9460                                 pipe_name(crtc->pipe), pll->active_mask);
9461
9462         I915_STATE_WARN(!(pll->state.pipe_mask & pipe_mask),
9463                         "pll enabled crtcs mismatch (expected 0x%x in 0x%x)\n",
9464                         pipe_mask, pll->state.pipe_mask);
9465
9466         I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
9467                                           &dpll_hw_state,
9468                                           sizeof(dpll_hw_state)),
9469                         "pll hw state mismatch\n");
9470 }
9471
9472 static void
9473 verify_shared_dpll_state(struct intel_crtc *crtc,
9474                          struct intel_crtc_state *old_crtc_state,
9475                          struct intel_crtc_state *new_crtc_state)
9476 {
9477         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9478
9479         if (new_crtc_state->shared_dpll)
9480                 verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
9481
9482         if (old_crtc_state->shared_dpll &&
9483             old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
9484                 u8 pipe_mask = BIT(crtc->pipe);
9485                 struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
9486
9487                 I915_STATE_WARN(pll->active_mask & pipe_mask,
9488                                 "pll active mismatch (didn't expect pipe %c in active mask (0x%x))\n",
9489                                 pipe_name(crtc->pipe), pll->active_mask);
9490                 I915_STATE_WARN(pll->state.pipe_mask & pipe_mask,
9491                                 "pll enabled crtcs mismatch (found %x in enabled mask (0x%x))\n",
9492                                 pipe_name(crtc->pipe), pll->state.pipe_mask);
9493         }
9494 }
9495
9496 static void
9497 intel_modeset_verify_crtc(struct intel_crtc *crtc,
9498                           struct intel_atomic_state *state,
9499                           struct intel_crtc_state *old_crtc_state,
9500                           struct intel_crtc_state *new_crtc_state)
9501 {
9502         if (!intel_crtc_needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
9503                 return;
9504
9505         verify_wm_state(crtc, new_crtc_state);
9506         verify_connector_state(state, crtc);
9507         verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
9508         verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
9509 }
9510
9511 static void
9512 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
9513 {
9514         int i;
9515
9516         for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
9517                 verify_single_dpll_state(dev_priv,
9518                                          &dev_priv->dpll.shared_dplls[i],
9519                                          NULL, NULL);
9520 }
9521
9522 static void
9523 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
9524                               struct intel_atomic_state *state)
9525 {
9526         verify_encoder_state(dev_priv, state);
9527         verify_connector_state(state, NULL);
9528         verify_disabled_dpll_state(dev_priv);
9529 }
9530
9531 static void
9532 intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
9533 {
9534         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9535         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9536         struct drm_display_mode adjusted_mode =
9537                 crtc_state->hw.adjusted_mode;
9538
9539         if (crtc_state->vrr.enable) {
9540                 adjusted_mode.crtc_vtotal = crtc_state->vrr.vmax;
9541                 adjusted_mode.crtc_vblank_end = crtc_state->vrr.vmax;
9542                 adjusted_mode.crtc_vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
9543                 crtc->vmax_vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
9544         }
9545
9546         drm_calc_timestamping_constants(&crtc->base, &adjusted_mode);
9547
9548         crtc->mode_flags = crtc_state->mode_flags;
9549
9550         /*
9551          * The scanline counter increments at the leading edge of hsync.
9552          *
9553          * On most platforms it starts counting from vtotal-1 on the
9554          * first active line. That means the scanline counter value is
9555          * always one less than what we would expect. Ie. just after
9556          * start of vblank, which also occurs at start of hsync (on the
9557          * last active line), the scanline counter will read vblank_start-1.
9558          *
9559          * On gen2 the scanline counter starts counting from 1 instead
9560          * of vtotal-1, so we have to subtract one (or rather add vtotal-1
9561          * to keep the value positive), instead of adding one.
9562          *
9563          * On HSW+ the behaviour of the scanline counter depends on the output
9564          * type. For DP ports it behaves like most other platforms, but on HDMI
9565          * there's an extra 1 line difference. So we need to add two instead of
9566          * one to the value.
9567          *
9568          * On VLV/CHV DSI the scanline counter would appear to increment
9569          * approx. 1/3 of a scanline before start of vblank. Unfortunately
9570          * that means we can't tell whether we're in vblank or not while
9571          * we're on that particular line. We must still set scanline_offset
9572          * to 1 so that the vblank timestamps come out correct when we query
9573          * the scanline counter from within the vblank interrupt handler.
9574          * However if queried just before the start of vblank we'll get an
9575          * answer that's slightly in the future.
9576          */
9577         if (IS_GEN(dev_priv, 2)) {
9578                 int vtotal;
9579
9580                 vtotal = adjusted_mode.crtc_vtotal;
9581                 if (adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
9582                         vtotal /= 2;
9583
9584                 crtc->scanline_offset = vtotal - 1;
9585         } else if (HAS_DDI(dev_priv) &&
9586                    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
9587                 crtc->scanline_offset = 2;
9588         } else {
9589                 crtc->scanline_offset = 1;
9590         }
9591 }
9592
9593 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
9594 {
9595         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9596         struct intel_crtc_state *new_crtc_state;
9597         struct intel_crtc *crtc;
9598         int i;
9599
9600         if (!dev_priv->display.crtc_compute_clock)
9601                 return;
9602
9603         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9604                 if (!intel_crtc_needs_modeset(new_crtc_state))
9605                         continue;
9606
9607                 intel_release_shared_dplls(state, crtc);
9608         }
9609 }
9610
9611 /*
9612  * This implements the workaround described in the "notes" section of the mode
9613  * set sequence documentation. When going from no pipes or single pipe to
9614  * multiple pipes, and planes are enabled after the pipe, we need to wait at
9615  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
9616  */
9617 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
9618 {
9619         struct intel_crtc_state *crtc_state;
9620         struct intel_crtc *crtc;
9621         struct intel_crtc_state *first_crtc_state = NULL;
9622         struct intel_crtc_state *other_crtc_state = NULL;
9623         enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
9624         int i;
9625
9626         /* look at all crtc's that are going to be enabled in during modeset */
9627         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9628                 if (!crtc_state->hw.active ||
9629                     !intel_crtc_needs_modeset(crtc_state))
9630                         continue;
9631
9632                 if (first_crtc_state) {
9633                         other_crtc_state = crtc_state;
9634                         break;
9635                 } else {
9636                         first_crtc_state = crtc_state;
9637                         first_pipe = crtc->pipe;
9638                 }
9639         }
9640
9641         /* No workaround needed? */
9642         if (!first_crtc_state)
9643                 return 0;
9644
9645         /* w/a possibly needed, check how many crtc's are already enabled. */
9646         for_each_intel_crtc(state->base.dev, crtc) {
9647                 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
9648                 if (IS_ERR(crtc_state))
9649                         return PTR_ERR(crtc_state);
9650
9651                 crtc_state->hsw_workaround_pipe = INVALID_PIPE;
9652
9653                 if (!crtc_state->hw.active ||
9654                     intel_crtc_needs_modeset(crtc_state))
9655                         continue;
9656
9657                 /* 2 or more enabled crtcs means no need for w/a */
9658                 if (enabled_pipe != INVALID_PIPE)
9659                         return 0;
9660
9661                 enabled_pipe = crtc->pipe;
9662         }
9663
9664         if (enabled_pipe != INVALID_PIPE)
9665                 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
9666         else if (other_crtc_state)
9667                 other_crtc_state->hsw_workaround_pipe = first_pipe;
9668
9669         return 0;
9670 }
9671
9672 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
9673                            u8 active_pipes)
9674 {
9675         const struct intel_crtc_state *crtc_state;
9676         struct intel_crtc *crtc;
9677         int i;
9678
9679         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9680                 if (crtc_state->hw.active)
9681                         active_pipes |= BIT(crtc->pipe);
9682                 else
9683                         active_pipes &= ~BIT(crtc->pipe);
9684         }
9685
9686         return active_pipes;
9687 }
9688
9689 static int intel_modeset_checks(struct intel_atomic_state *state)
9690 {
9691         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9692
9693         state->modeset = true;
9694
9695         if (IS_HASWELL(dev_priv))
9696                 return hsw_mode_set_planes_workaround(state);
9697
9698         return 0;
9699 }
9700
9701 /*
9702  * Handle calculation of various watermark data at the end of the atomic check
9703  * phase.  The code here should be run after the per-crtc and per-plane 'check'
9704  * handlers to ensure that all derived state has been updated.
9705  */
9706 static int calc_watermark_data(struct intel_atomic_state *state)
9707 {
9708         struct drm_device *dev = state->base.dev;
9709         struct drm_i915_private *dev_priv = to_i915(dev);
9710
9711         /* Is there platform-specific watermark information to calculate? */
9712         if (dev_priv->display.compute_global_watermarks)
9713                 return dev_priv->display.compute_global_watermarks(state);
9714
9715         return 0;
9716 }
9717
9718 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
9719                                      struct intel_crtc_state *new_crtc_state)
9720 {
9721         if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
9722                 return;
9723
9724         new_crtc_state->uapi.mode_changed = false;
9725         new_crtc_state->update_pipe = true;
9726 }
9727
9728 static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
9729                                     struct intel_crtc_state *new_crtc_state)
9730 {
9731         /*
9732          * If we're not doing the full modeset we want to
9733          * keep the current M/N values as they may be
9734          * sufficiently different to the computed values
9735          * to cause problems.
9736          *
9737          * FIXME: should really copy more fuzzy state here
9738          */
9739         new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
9740         new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
9741         new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
9742         new_crtc_state->has_drrs = old_crtc_state->has_drrs;
9743 }
9744
9745 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
9746                                           struct intel_crtc *crtc,
9747                                           u8 plane_ids_mask)
9748 {
9749         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9750         struct intel_plane *plane;
9751
9752         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
9753                 struct intel_plane_state *plane_state;
9754
9755                 if ((plane_ids_mask & BIT(plane->id)) == 0)
9756                         continue;
9757
9758                 plane_state = intel_atomic_get_plane_state(state, plane);
9759                 if (IS_ERR(plane_state))
9760                         return PTR_ERR(plane_state);
9761         }
9762
9763         return 0;
9764 }
9765
9766 int intel_atomic_add_affected_planes(struct intel_atomic_state *state,
9767                                      struct intel_crtc *crtc)
9768 {
9769         const struct intel_crtc_state *old_crtc_state =
9770                 intel_atomic_get_old_crtc_state(state, crtc);
9771         const struct intel_crtc_state *new_crtc_state =
9772                 intel_atomic_get_new_crtc_state(state, crtc);
9773
9774         return intel_crtc_add_planes_to_state(state, crtc,
9775                                               old_crtc_state->enabled_planes |
9776                                               new_crtc_state->enabled_planes);
9777 }
9778
9779 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
9780 {
9781         /* See {hsw,vlv,ivb}_plane_ratio() */
9782         return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
9783                 IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9784                 IS_IVYBRIDGE(dev_priv) || (INTEL_GEN(dev_priv) >= 11);
9785 }
9786
9787 static int intel_crtc_add_bigjoiner_planes(struct intel_atomic_state *state,
9788                                            struct intel_crtc *crtc,
9789                                            struct intel_crtc *other)
9790 {
9791         const struct intel_plane_state *plane_state;
9792         struct intel_plane *plane;
9793         u8 plane_ids = 0;
9794         int i;
9795
9796         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9797                 if (plane->pipe == crtc->pipe)
9798                         plane_ids |= BIT(plane->id);
9799         }
9800
9801         return intel_crtc_add_planes_to_state(state, other, plane_ids);
9802 }
9803
9804 static int intel_bigjoiner_add_affected_planes(struct intel_atomic_state *state)
9805 {
9806         const struct intel_crtc_state *crtc_state;
9807         struct intel_crtc *crtc;
9808         int i;
9809
9810         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9811                 int ret;
9812
9813                 if (!crtc_state->bigjoiner)
9814                         continue;
9815
9816                 ret = intel_crtc_add_bigjoiner_planes(state, crtc,
9817                                                       crtc_state->bigjoiner_linked_crtc);
9818                 if (ret)
9819                         return ret;
9820         }
9821
9822         return 0;
9823 }
9824
9825 static int intel_atomic_check_planes(struct intel_atomic_state *state)
9826 {
9827         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9828         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
9829         struct intel_plane_state *plane_state;
9830         struct intel_plane *plane;
9831         struct intel_crtc *crtc;
9832         int i, ret;
9833
9834         ret = icl_add_linked_planes(state);
9835         if (ret)
9836                 return ret;
9837
9838         ret = intel_bigjoiner_add_affected_planes(state);
9839         if (ret)
9840                 return ret;
9841
9842         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9843                 ret = intel_plane_atomic_check(state, plane);
9844                 if (ret) {
9845                         drm_dbg_atomic(&dev_priv->drm,
9846                                        "[PLANE:%d:%s] atomic driver check failed\n",
9847                                        plane->base.base.id, plane->base.name);
9848                         return ret;
9849                 }
9850         }
9851
9852         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9853                                             new_crtc_state, i) {
9854                 u8 old_active_planes, new_active_planes;
9855
9856                 ret = icl_check_nv12_planes(new_crtc_state);
9857                 if (ret)
9858                         return ret;
9859
9860                 /*
9861                  * On some platforms the number of active planes affects
9862                  * the planes' minimum cdclk calculation. Add such planes
9863                  * to the state before we compute the minimum cdclk.
9864                  */
9865                 if (!active_planes_affects_min_cdclk(dev_priv))
9866                         continue;
9867
9868                 old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
9869                 new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
9870
9871                 /*
9872                  * Not only the number of planes, but if the plane configuration had
9873                  * changed might already mean we need to recompute min CDCLK,
9874                  * because different planes might consume different amount of Dbuf bandwidth
9875                  * according to formula: Bw per plane = Pixel rate * bpp * pipe/plane scale factor
9876                  */
9877                 if (old_active_planes == new_active_planes)
9878                         continue;
9879
9880                 ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
9881                 if (ret)
9882                         return ret;
9883         }
9884
9885         return 0;
9886 }
9887
9888 static int intel_atomic_check_cdclk(struct intel_atomic_state *state,
9889                                     bool *need_cdclk_calc)
9890 {
9891         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9892         const struct intel_cdclk_state *old_cdclk_state;
9893         const struct intel_cdclk_state *new_cdclk_state;
9894         struct intel_plane_state *plane_state;
9895         struct intel_bw_state *new_bw_state;
9896         struct intel_plane *plane;
9897         int min_cdclk = 0;
9898         enum pipe pipe;
9899         int ret;
9900         int i;
9901         /*
9902          * active_planes bitmask has been updated, and potentially
9903          * affected planes are part of the state. We can now
9904          * compute the minimum cdclk for each plane.
9905          */
9906         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9907                 ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
9908                 if (ret)
9909                         return ret;
9910         }
9911
9912         old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
9913         new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
9914
9915         if (new_cdclk_state &&
9916             old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk)
9917                 *need_cdclk_calc = true;
9918
9919         ret = dev_priv->display.bw_calc_min_cdclk(state);
9920         if (ret)
9921                 return ret;
9922
9923         new_bw_state = intel_atomic_get_new_bw_state(state);
9924
9925         if (!new_cdclk_state || !new_bw_state)
9926                 return 0;
9927
9928         for_each_pipe(dev_priv, pipe) {
9929                 min_cdclk = max(new_cdclk_state->min_cdclk[pipe], min_cdclk);
9930
9931                 /*
9932                  * Currently do this change only if we need to increase
9933                  */
9934                 if (new_bw_state->min_cdclk > min_cdclk)
9935                         *need_cdclk_calc = true;
9936         }
9937
9938         return 0;
9939 }
9940
9941 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
9942 {
9943         struct intel_crtc_state *crtc_state;
9944         struct intel_crtc *crtc;
9945         int i;
9946
9947         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9948                 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
9949                 int ret;
9950
9951                 ret = intel_crtc_atomic_check(state, crtc);
9952                 if (ret) {
9953                         drm_dbg_atomic(&i915->drm,
9954                                        "[CRTC:%d:%s] atomic driver check failed\n",
9955                                        crtc->base.base.id, crtc->base.name);
9956                         return ret;
9957                 }
9958         }
9959
9960         return 0;
9961 }
9962
9963 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
9964                                                u8 transcoders)
9965 {
9966         const struct intel_crtc_state *new_crtc_state;
9967         struct intel_crtc *crtc;
9968         int i;
9969
9970         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9971                 if (new_crtc_state->hw.enable &&
9972                     transcoders & BIT(new_crtc_state->cpu_transcoder) &&
9973                     intel_crtc_needs_modeset(new_crtc_state))
9974                         return true;
9975         }
9976
9977         return false;
9978 }
9979
9980 static int intel_atomic_check_bigjoiner(struct intel_atomic_state *state,
9981                                         struct intel_crtc *crtc,
9982                                         struct intel_crtc_state *old_crtc_state,
9983                                         struct intel_crtc_state *new_crtc_state)
9984 {
9985         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9986         struct intel_crtc_state *slave_crtc_state, *master_crtc_state;
9987         struct intel_crtc *slave, *master;
9988
9989         /* slave being enabled, is master is still claiming this crtc? */
9990         if (old_crtc_state->bigjoiner_slave) {
9991                 slave = crtc;
9992                 master = old_crtc_state->bigjoiner_linked_crtc;
9993                 master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
9994                 if (!master_crtc_state || !intel_crtc_needs_modeset(master_crtc_state))
9995                         goto claimed;
9996         }
9997
9998         if (!new_crtc_state->bigjoiner)
9999                 return 0;
10000
10001         if (1 + crtc->pipe >= INTEL_NUM_PIPES(dev_priv)) {
10002                 DRM_DEBUG_KMS("[CRTC:%d:%s] Big joiner configuration requires "
10003                               "CRTC + 1 to be used, doesn't exist\n",
10004                               crtc->base.base.id, crtc->base.name);
10005                 return -EINVAL;
10006         }
10007
10008         slave = new_crtc_state->bigjoiner_linked_crtc =
10009                 intel_get_crtc_for_pipe(dev_priv, crtc->pipe + 1);
10010         slave_crtc_state = intel_atomic_get_crtc_state(&state->base, slave);
10011         master = crtc;
10012         if (IS_ERR(slave_crtc_state))
10013                 return PTR_ERR(slave_crtc_state);
10014
10015         /* master being enabled, slave was already configured? */
10016         if (slave_crtc_state->uapi.enable)
10017                 goto claimed;
10018
10019         DRM_DEBUG_KMS("[CRTC:%d:%s] Used as slave for big joiner\n",
10020                       slave->base.base.id, slave->base.name);
10021
10022         return copy_bigjoiner_crtc_state(slave_crtc_state, new_crtc_state);
10023
10024 claimed:
10025         DRM_DEBUG_KMS("[CRTC:%d:%s] Slave is enabled as normal CRTC, but "
10026                       "[CRTC:%d:%s] claiming this CRTC for bigjoiner.\n",
10027                       slave->base.base.id, slave->base.name,
10028                       master->base.base.id, master->base.name);
10029         return -EINVAL;
10030 }
10031
10032 static void kill_bigjoiner_slave(struct intel_atomic_state *state,
10033                                  struct intel_crtc_state *master_crtc_state)
10034 {
10035         struct intel_crtc_state *slave_crtc_state =
10036                 intel_atomic_get_new_crtc_state(state, master_crtc_state->bigjoiner_linked_crtc);
10037
10038         slave_crtc_state->bigjoiner = master_crtc_state->bigjoiner = false;
10039         slave_crtc_state->bigjoiner_slave = master_crtc_state->bigjoiner_slave = false;
10040         slave_crtc_state->bigjoiner_linked_crtc = master_crtc_state->bigjoiner_linked_crtc = NULL;
10041         intel_crtc_copy_uapi_to_hw_state(state, slave_crtc_state);
10042 }
10043
10044 /**
10045  * DOC: asynchronous flip implementation
10046  *
10047  * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
10048  * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
10049  * Correspondingly, support is currently added for primary plane only.
10050  *
10051  * Async flip can only change the plane surface address, so anything else
10052  * changing is rejected from the intel_atomic_check_async() function.
10053  * Once this check is cleared, flip done interrupt is enabled using
10054  * the intel_crtc_enable_flip_done() function.
10055  *
10056  * As soon as the surface address register is written, flip done interrupt is
10057  * generated and the requested events are sent to the usersapce in the interrupt
10058  * handler itself. The timestamp and sequence sent during the flip done event
10059  * correspond to the last vblank and have no relation to the actual time when
10060  * the flip done event was sent.
10061  */
10062 static int intel_atomic_check_async(struct intel_atomic_state *state)
10063 {
10064         struct drm_i915_private *i915 = to_i915(state->base.dev);
10065         const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10066         const struct intel_plane_state *new_plane_state, *old_plane_state;
10067         struct intel_crtc *crtc;
10068         struct intel_plane *plane;
10069         int i;
10070
10071         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10072                                             new_crtc_state, i) {
10073                 if (intel_crtc_needs_modeset(new_crtc_state)) {
10074                         drm_dbg_kms(&i915->drm, "Modeset Required. Async flip not supported\n");
10075                         return -EINVAL;
10076                 }
10077
10078                 if (!new_crtc_state->hw.active) {
10079                         drm_dbg_kms(&i915->drm, "CRTC inactive\n");
10080                         return -EINVAL;
10081                 }
10082                 if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
10083                         drm_dbg_kms(&i915->drm,
10084                                     "Active planes cannot be changed during async flip\n");
10085                         return -EINVAL;
10086                 }
10087         }
10088
10089         for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
10090                                              new_plane_state, i) {
10091                 /*
10092                  * TODO: Async flip is only supported through the page flip IOCTL
10093                  * as of now. So support currently added for primary plane only.
10094                  * Support for other planes on platforms on which supports
10095                  * this(vlv/chv and icl+) should be added when async flip is
10096                  * enabled in the atomic IOCTL path.
10097                  */
10098                 if (!plane->async_flip)
10099                         return -EINVAL;
10100
10101                 /*
10102                  * FIXME: This check is kept generic for all platforms.
10103                  * Need to verify this for all gen9 and gen10 platforms to enable
10104                  * this selectively if required.
10105                  */
10106                 switch (new_plane_state->hw.fb->modifier) {
10107                 case I915_FORMAT_MOD_X_TILED:
10108                 case I915_FORMAT_MOD_Y_TILED:
10109                 case I915_FORMAT_MOD_Yf_TILED:
10110                         break;
10111                 default:
10112                         drm_dbg_kms(&i915->drm,
10113                                     "Linear memory/CCS does not support async flips\n");
10114                         return -EINVAL;
10115                 }
10116
10117                 if (old_plane_state->color_plane[0].stride !=
10118                     new_plane_state->color_plane[0].stride) {
10119                         drm_dbg_kms(&i915->drm, "Stride cannot be changed in async flip\n");
10120                         return -EINVAL;
10121                 }
10122
10123                 if (old_plane_state->hw.fb->modifier !=
10124                     new_plane_state->hw.fb->modifier) {
10125                         drm_dbg_kms(&i915->drm,
10126                                     "Framebuffer modifiers cannot be changed in async flip\n");
10127                         return -EINVAL;
10128                 }
10129
10130                 if (old_plane_state->hw.fb->format !=
10131                     new_plane_state->hw.fb->format) {
10132                         drm_dbg_kms(&i915->drm,
10133                                     "Framebuffer format cannot be changed in async flip\n");
10134                         return -EINVAL;
10135                 }
10136
10137                 if (old_plane_state->hw.rotation !=
10138                     new_plane_state->hw.rotation) {
10139                         drm_dbg_kms(&i915->drm, "Rotation cannot be changed in async flip\n");
10140                         return -EINVAL;
10141                 }
10142
10143                 if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
10144                     !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
10145                         drm_dbg_kms(&i915->drm,
10146                                     "Plane size/co-ordinates cannot be changed in async flip\n");
10147                         return -EINVAL;
10148                 }
10149
10150                 if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
10151                         drm_dbg_kms(&i915->drm, "Alpha value cannot be changed in async flip\n");
10152                         return -EINVAL;
10153                 }
10154
10155                 if (old_plane_state->hw.pixel_blend_mode !=
10156                     new_plane_state->hw.pixel_blend_mode) {
10157                         drm_dbg_kms(&i915->drm,
10158                                     "Pixel blend mode cannot be changed in async flip\n");
10159                         return -EINVAL;
10160                 }
10161
10162                 if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
10163                         drm_dbg_kms(&i915->drm,
10164                                     "Color encoding cannot be changed in async flip\n");
10165                         return -EINVAL;
10166                 }
10167
10168                 if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
10169                         drm_dbg_kms(&i915->drm, "Color range cannot be changed in async flip\n");
10170                         return -EINVAL;
10171                 }
10172         }
10173
10174         return 0;
10175 }
10176
10177 static int intel_bigjoiner_add_affected_crtcs(struct intel_atomic_state *state)
10178 {
10179         struct intel_crtc_state *crtc_state;
10180         struct intel_crtc *crtc;
10181         int i;
10182
10183         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
10184                 struct intel_crtc_state *linked_crtc_state;
10185                 struct intel_crtc *linked_crtc;
10186                 int ret;
10187
10188                 if (!crtc_state->bigjoiner)
10189                         continue;
10190
10191                 linked_crtc = crtc_state->bigjoiner_linked_crtc;
10192                 linked_crtc_state = intel_atomic_get_crtc_state(&state->base, linked_crtc);
10193                 if (IS_ERR(linked_crtc_state))
10194                         return PTR_ERR(linked_crtc_state);
10195
10196                 if (!intel_crtc_needs_modeset(crtc_state))
10197                         continue;
10198
10199                 linked_crtc_state->uapi.mode_changed = true;
10200
10201                 ret = drm_atomic_add_affected_connectors(&state->base,
10202                                                          &linked_crtc->base);
10203                 if (ret)
10204                         return ret;
10205
10206                 ret = intel_atomic_add_affected_planes(state, linked_crtc);
10207                 if (ret)
10208                         return ret;
10209         }
10210
10211         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
10212                 /* Kill old bigjoiner link, we may re-establish afterwards */
10213                 if (intel_crtc_needs_modeset(crtc_state) &&
10214                     crtc_state->bigjoiner && !crtc_state->bigjoiner_slave)
10215                         kill_bigjoiner_slave(state, crtc_state);
10216         }
10217
10218         return 0;
10219 }
10220
10221 /**
10222  * intel_atomic_check - validate state object
10223  * @dev: drm device
10224  * @_state: state to validate
10225  */
10226 static int intel_atomic_check(struct drm_device *dev,
10227                               struct drm_atomic_state *_state)
10228 {
10229         struct drm_i915_private *dev_priv = to_i915(dev);
10230         struct intel_atomic_state *state = to_intel_atomic_state(_state);
10231         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10232         struct intel_crtc *crtc;
10233         int ret, i;
10234         bool any_ms = false;
10235
10236         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10237                                             new_crtc_state, i) {
10238                 if (new_crtc_state->inherited != old_crtc_state->inherited)
10239                         new_crtc_state->uapi.mode_changed = true;
10240         }
10241
10242         intel_vrr_check_modeset(state);
10243
10244         ret = drm_atomic_helper_check_modeset(dev, &state->base);
10245         if (ret)
10246                 goto fail;
10247
10248         ret = intel_bigjoiner_add_affected_crtcs(state);
10249         if (ret)
10250                 goto fail;
10251
10252         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10253                                             new_crtc_state, i) {
10254                 if (!intel_crtc_needs_modeset(new_crtc_state)) {
10255                         /* Light copy */
10256                         intel_crtc_copy_uapi_to_hw_state_nomodeset(state, new_crtc_state);
10257
10258                         continue;
10259                 }
10260
10261                 if (!new_crtc_state->uapi.enable) {
10262                         if (!new_crtc_state->bigjoiner_slave) {
10263                                 intel_crtc_copy_uapi_to_hw_state(state, new_crtc_state);
10264                                 any_ms = true;
10265                         }
10266                         continue;
10267                 }
10268
10269                 ret = intel_crtc_prepare_cleared_state(state, new_crtc_state);
10270                 if (ret)
10271                         goto fail;
10272
10273                 ret = intel_modeset_pipe_config(state, new_crtc_state);
10274                 if (ret)
10275                         goto fail;
10276
10277                 ret = intel_atomic_check_bigjoiner(state, crtc, old_crtc_state,
10278                                                    new_crtc_state);
10279                 if (ret)
10280                         goto fail;
10281         }
10282
10283         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10284                                             new_crtc_state, i) {
10285                 if (!intel_crtc_needs_modeset(new_crtc_state))
10286                         continue;
10287
10288                 ret = intel_modeset_pipe_config_late(new_crtc_state);
10289                 if (ret)
10290                         goto fail;
10291
10292                 intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
10293         }
10294
10295         /**
10296          * Check if fastset is allowed by external dependencies like other
10297          * pipes and transcoders.
10298          *
10299          * Right now it only forces a fullmodeset when the MST master
10300          * transcoder did not changed but the pipe of the master transcoder
10301          * needs a fullmodeset so all slaves also needs to do a fullmodeset or
10302          * in case of port synced crtcs, if one of the synced crtcs
10303          * needs a full modeset, all other synced crtcs should be
10304          * forced a full modeset.
10305          */
10306         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10307                 if (!new_crtc_state->hw.enable || intel_crtc_needs_modeset(new_crtc_state))
10308                         continue;
10309
10310                 if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
10311                         enum transcoder master = new_crtc_state->mst_master_transcoder;
10312
10313                         if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
10314                                 new_crtc_state->uapi.mode_changed = true;
10315                                 new_crtc_state->update_pipe = false;
10316                         }
10317                 }
10318
10319                 if (is_trans_port_sync_mode(new_crtc_state)) {
10320                         u8 trans = new_crtc_state->sync_mode_slaves_mask;
10321
10322                         if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
10323                                 trans |= BIT(new_crtc_state->master_transcoder);
10324
10325                         if (intel_cpu_transcoders_need_modeset(state, trans)) {
10326                                 new_crtc_state->uapi.mode_changed = true;
10327                                 new_crtc_state->update_pipe = false;
10328                         }
10329                 }
10330
10331                 if (new_crtc_state->bigjoiner) {
10332                         struct intel_crtc_state *linked_crtc_state =
10333                                 intel_atomic_get_new_crtc_state(state, new_crtc_state->bigjoiner_linked_crtc);
10334
10335                         if (intel_crtc_needs_modeset(linked_crtc_state)) {
10336                                 new_crtc_state->uapi.mode_changed = true;
10337                                 new_crtc_state->update_pipe = false;
10338                         }
10339                 }
10340         }
10341
10342         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10343                                             new_crtc_state, i) {
10344                 if (intel_crtc_needs_modeset(new_crtc_state)) {
10345                         any_ms = true;
10346                         continue;
10347                 }
10348
10349                 if (!new_crtc_state->update_pipe)
10350                         continue;
10351
10352                 intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
10353         }
10354
10355         if (any_ms && !check_digital_port_conflicts(state)) {
10356                 drm_dbg_kms(&dev_priv->drm,
10357                             "rejecting conflicting digital port configuration\n");
10358                 ret = -EINVAL;
10359                 goto fail;
10360         }
10361
10362         ret = drm_dp_mst_atomic_check(&state->base);
10363         if (ret)
10364                 goto fail;
10365
10366         ret = intel_atomic_check_planes(state);
10367         if (ret)
10368                 goto fail;
10369
10370         intel_fbc_choose_crtc(dev_priv, state);
10371         ret = calc_watermark_data(state);
10372         if (ret)
10373                 goto fail;
10374
10375         ret = intel_bw_atomic_check(state);
10376         if (ret)
10377                 goto fail;
10378
10379         ret = intel_atomic_check_cdclk(state, &any_ms);
10380         if (ret)
10381                 goto fail;
10382
10383         if (any_ms) {
10384                 ret = intel_modeset_checks(state);
10385                 if (ret)
10386                         goto fail;
10387
10388                 ret = intel_modeset_calc_cdclk(state);
10389                 if (ret)
10390                         return ret;
10391
10392                 intel_modeset_clear_plls(state);
10393         }
10394
10395         ret = intel_atomic_check_crtcs(state);
10396         if (ret)
10397                 goto fail;
10398
10399         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10400                                             new_crtc_state, i) {
10401                 if (new_crtc_state->uapi.async_flip) {
10402                         ret = intel_atomic_check_async(state);
10403                         if (ret)
10404                                 goto fail;
10405                 }
10406
10407                 if (!intel_crtc_needs_modeset(new_crtc_state) &&
10408                     !new_crtc_state->update_pipe)
10409                         continue;
10410
10411                 intel_dump_pipe_config(new_crtc_state, state,
10412                                        intel_crtc_needs_modeset(new_crtc_state) ?
10413                                        "[modeset]" : "[fastset]");
10414         }
10415
10416         return 0;
10417
10418  fail:
10419         if (ret == -EDEADLK)
10420                 return ret;
10421
10422         /*
10423          * FIXME would probably be nice to know which crtc specifically
10424          * caused the failure, in cases where we can pinpoint it.
10425          */
10426         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10427                                             new_crtc_state, i)
10428                 intel_dump_pipe_config(new_crtc_state, state, "[failed]");
10429
10430         return ret;
10431 }
10432
10433 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
10434 {
10435         struct intel_crtc_state *crtc_state;
10436         struct intel_crtc *crtc;
10437         int i, ret;
10438
10439         ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
10440         if (ret < 0)
10441                 return ret;
10442
10443         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
10444                 bool mode_changed = intel_crtc_needs_modeset(crtc_state);
10445
10446                 if (mode_changed || crtc_state->update_pipe ||
10447                     crtc_state->uapi.color_mgmt_changed) {
10448                         intel_dsb_prepare(crtc_state);
10449                 }
10450         }
10451
10452         return 0;
10453 }
10454
10455 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
10456                                   struct intel_crtc_state *crtc_state)
10457 {
10458         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10459
10460         if (!IS_GEN(dev_priv, 2) || crtc_state->active_planes)
10461                 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
10462
10463         if (crtc_state->has_pch_encoder) {
10464                 enum pipe pch_transcoder =
10465                         intel_crtc_pch_transcoder(crtc);
10466
10467                 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
10468         }
10469 }
10470
10471 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
10472                                const struct intel_crtc_state *new_crtc_state)
10473 {
10474         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
10475         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10476
10477         /*
10478          * Update pipe size and adjust fitter if needed: the reason for this is
10479          * that in compute_mode_changes we check the native mode (not the pfit
10480          * mode) to see if we can flip rather than do a full mode set. In the
10481          * fastboot case, we'll flip, but if we don't update the pipesrc and
10482          * pfit state, we'll end up with a big fb scanned out into the wrong
10483          * sized surface.
10484          */
10485         intel_set_pipe_src_size(new_crtc_state);
10486
10487         /* on skylake this is done by detaching scalers */
10488         if (INTEL_GEN(dev_priv) >= 9) {
10489                 skl_detach_scalers(new_crtc_state);
10490
10491                 if (new_crtc_state->pch_pfit.enabled)
10492                         skl_pfit_enable(new_crtc_state);
10493         } else if (HAS_PCH_SPLIT(dev_priv)) {
10494                 if (new_crtc_state->pch_pfit.enabled)
10495                         ilk_pfit_enable(new_crtc_state);
10496                 else if (old_crtc_state->pch_pfit.enabled)
10497                         ilk_pfit_disable(old_crtc_state);
10498         }
10499
10500         /*
10501          * The register is supposedly single buffered so perhaps
10502          * not 100% correct to do this here. But SKL+ calculate
10503          * this based on the adjust pixel rate so pfit changes do
10504          * affect it and so it must be updated for fastsets.
10505          * HSW/BDW only really need this here for fastboot, after
10506          * that the value should not change without a full modeset.
10507          */
10508         if (INTEL_GEN(dev_priv) >= 9 ||
10509             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
10510                 hsw_set_linetime_wm(new_crtc_state);
10511
10512         if (INTEL_GEN(dev_priv) >= 11)
10513                 icl_set_pipe_chicken(crtc);
10514 }
10515
10516 static void commit_pipe_config(struct intel_atomic_state *state,
10517                                struct intel_crtc *crtc)
10518 {
10519         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10520         const struct intel_crtc_state *old_crtc_state =
10521                 intel_atomic_get_old_crtc_state(state, crtc);
10522         const struct intel_crtc_state *new_crtc_state =
10523                 intel_atomic_get_new_crtc_state(state, crtc);
10524         bool modeset = intel_crtc_needs_modeset(new_crtc_state);
10525
10526         /*
10527          * During modesets pipe configuration was programmed as the
10528          * CRTC was enabled.
10529          */
10530         if (!modeset) {
10531                 if (new_crtc_state->uapi.color_mgmt_changed ||
10532                     new_crtc_state->update_pipe)
10533                         intel_color_commit(new_crtc_state);
10534
10535                 if (INTEL_GEN(dev_priv) >= 9)
10536                         skl_detach_scalers(new_crtc_state);
10537
10538                 if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
10539                         bdw_set_pipemisc(new_crtc_state);
10540
10541                 if (new_crtc_state->update_pipe)
10542                         intel_pipe_fastset(old_crtc_state, new_crtc_state);
10543
10544                 intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
10545         }
10546
10547         if (dev_priv->display.atomic_update_watermarks)
10548                 dev_priv->display.atomic_update_watermarks(state, crtc);
10549 }
10550
10551 static void intel_enable_crtc(struct intel_atomic_state *state,
10552                               struct intel_crtc *crtc)
10553 {
10554         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10555         const struct intel_crtc_state *new_crtc_state =
10556                 intel_atomic_get_new_crtc_state(state, crtc);
10557
10558         if (!intel_crtc_needs_modeset(new_crtc_state))
10559                 return;
10560
10561         intel_crtc_update_active_timings(new_crtc_state);
10562
10563         dev_priv->display.crtc_enable(state, crtc);
10564
10565         if (new_crtc_state->bigjoiner_slave)
10566                 return;
10567
10568         /* vblanks work again, re-enable pipe CRC. */
10569         intel_crtc_enable_pipe_crc(crtc);
10570 }
10571
10572 static void intel_update_crtc(struct intel_atomic_state *state,
10573                               struct intel_crtc *crtc)
10574 {
10575         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10576         const struct intel_crtc_state *old_crtc_state =
10577                 intel_atomic_get_old_crtc_state(state, crtc);
10578         struct intel_crtc_state *new_crtc_state =
10579                 intel_atomic_get_new_crtc_state(state, crtc);
10580         bool modeset = intel_crtc_needs_modeset(new_crtc_state);
10581
10582         if (!modeset) {
10583                 if (new_crtc_state->preload_luts &&
10584                     (new_crtc_state->uapi.color_mgmt_changed ||
10585                      new_crtc_state->update_pipe))
10586                         intel_color_load_luts(new_crtc_state);
10587
10588                 intel_pre_plane_update(state, crtc);
10589
10590                 if (new_crtc_state->update_pipe)
10591                         intel_encoders_update_pipe(state, crtc);
10592         }
10593
10594         if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
10595                 intel_fbc_disable(crtc);
10596         else
10597                 intel_fbc_enable(state, crtc);
10598
10599         /* Perform vblank evasion around commit operation */
10600         intel_pipe_update_start(new_crtc_state);
10601
10602         commit_pipe_config(state, crtc);
10603
10604         if (INTEL_GEN(dev_priv) >= 9)
10605                 skl_update_planes_on_crtc(state, crtc);
10606         else
10607                 i9xx_update_planes_on_crtc(state, crtc);
10608
10609         intel_pipe_update_end(new_crtc_state);
10610
10611         /*
10612          * We usually enable FIFO underrun interrupts as part of the
10613          * CRTC enable sequence during modesets.  But when we inherit a
10614          * valid pipe configuration from the BIOS we need to take care
10615          * of enabling them on the CRTC's first fastset.
10616          */
10617         if (new_crtc_state->update_pipe && !modeset &&
10618             old_crtc_state->inherited)
10619                 intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
10620 }
10621
10622 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
10623                                           struct intel_crtc_state *old_crtc_state,
10624                                           struct intel_crtc_state *new_crtc_state,
10625                                           struct intel_crtc *crtc)
10626 {
10627         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10628
10629         drm_WARN_ON(&dev_priv->drm, old_crtc_state->bigjoiner_slave);
10630
10631         intel_crtc_disable_planes(state, crtc);
10632
10633         /*
10634          * We still need special handling for disabling bigjoiner master
10635          * and slaves since for slave we do not have encoder or plls
10636          * so we dont need to disable those.
10637          */
10638         if (old_crtc_state->bigjoiner) {
10639                 intel_crtc_disable_planes(state,
10640                                           old_crtc_state->bigjoiner_linked_crtc);
10641                 old_crtc_state->bigjoiner_linked_crtc->active = false;
10642         }
10643
10644         /*
10645          * We need to disable pipe CRC before disabling the pipe,
10646          * or we race against vblank off.
10647          */
10648         intel_crtc_disable_pipe_crc(crtc);
10649
10650         dev_priv->display.crtc_disable(state, crtc);
10651         crtc->active = false;
10652         intel_fbc_disable(crtc);
10653         intel_disable_shared_dpll(old_crtc_state);
10654
10655         /* FIXME unify this for all platforms */
10656         if (!new_crtc_state->hw.active &&
10657             !HAS_GMCH(dev_priv) &&
10658             dev_priv->display.initial_watermarks)
10659                 dev_priv->display.initial_watermarks(state, crtc);
10660 }
10661
10662 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
10663 {
10664         struct intel_crtc_state *new_crtc_state, *old_crtc_state;
10665         struct intel_crtc *crtc;
10666         u32 handled = 0;
10667         int i;
10668
10669         /* Only disable port sync and MST slaves */
10670         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10671                                             new_crtc_state, i) {
10672                 if (!intel_crtc_needs_modeset(new_crtc_state) || old_crtc_state->bigjoiner)
10673                         continue;
10674
10675                 if (!old_crtc_state->hw.active)
10676                         continue;
10677
10678                 /* In case of Transcoder port Sync master slave CRTCs can be
10679                  * assigned in any order and we need to make sure that
10680                  * slave CRTCs are disabled first and then master CRTC since
10681                  * Slave vblanks are masked till Master Vblanks.
10682                  */
10683                 if (!is_trans_port_sync_slave(old_crtc_state) &&
10684                     !intel_dp_mst_is_slave_trans(old_crtc_state))
10685                         continue;
10686
10687                 intel_pre_plane_update(state, crtc);
10688                 intel_old_crtc_state_disables(state, old_crtc_state,
10689                                               new_crtc_state, crtc);
10690                 handled |= BIT(crtc->pipe);
10691         }
10692
10693         /* Disable everything else left on */
10694         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10695                                             new_crtc_state, i) {
10696                 if (!intel_crtc_needs_modeset(new_crtc_state) ||
10697                     (handled & BIT(crtc->pipe)) ||
10698                     old_crtc_state->bigjoiner_slave)
10699                         continue;
10700
10701                 intel_pre_plane_update(state, crtc);
10702                 if (old_crtc_state->bigjoiner) {
10703                         struct intel_crtc *slave =
10704                                 old_crtc_state->bigjoiner_linked_crtc;
10705
10706                         intel_pre_plane_update(state, slave);
10707                 }
10708
10709                 if (old_crtc_state->hw.active)
10710                         intel_old_crtc_state_disables(state, old_crtc_state,
10711                                                       new_crtc_state, crtc);
10712         }
10713 }
10714
10715 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
10716 {
10717         struct intel_crtc_state *new_crtc_state;
10718         struct intel_crtc *crtc;
10719         int i;
10720
10721         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10722                 if (!new_crtc_state->hw.active)
10723                         continue;
10724
10725                 intel_enable_crtc(state, crtc);
10726                 intel_update_crtc(state, crtc);
10727         }
10728 }
10729
10730 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
10731 {
10732         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10733         struct intel_crtc *crtc;
10734         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10735         struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
10736         u8 update_pipes = 0, modeset_pipes = 0;
10737         int i;
10738
10739         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10740                 enum pipe pipe = crtc->pipe;
10741
10742                 if (!new_crtc_state->hw.active)
10743                         continue;
10744
10745                 /* ignore allocations for crtc's that have been turned off. */
10746                 if (!intel_crtc_needs_modeset(new_crtc_state)) {
10747                         entries[pipe] = old_crtc_state->wm.skl.ddb;
10748                         update_pipes |= BIT(pipe);
10749                 } else {
10750                         modeset_pipes |= BIT(pipe);
10751                 }
10752         }
10753
10754         /*
10755          * Whenever the number of active pipes changes, we need to make sure we
10756          * update the pipes in the right order so that their ddb allocations
10757          * never overlap with each other between CRTC updates. Otherwise we'll
10758          * cause pipe underruns and other bad stuff.
10759          *
10760          * So first lets enable all pipes that do not need a fullmodeset as
10761          * those don't have any external dependency.
10762          */
10763         while (update_pipes) {
10764                 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10765                                                     new_crtc_state, i) {
10766                         enum pipe pipe = crtc->pipe;
10767
10768                         if ((update_pipes & BIT(pipe)) == 0)
10769                                 continue;
10770
10771                         if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
10772                                                         entries, I915_MAX_PIPES, pipe))
10773                                 continue;
10774
10775                         entries[pipe] = new_crtc_state->wm.skl.ddb;
10776                         update_pipes &= ~BIT(pipe);
10777
10778                         intel_update_crtc(state, crtc);
10779
10780                         /*
10781                          * If this is an already active pipe, it's DDB changed,
10782                          * and this isn't the last pipe that needs updating
10783                          * then we need to wait for a vblank to pass for the
10784                          * new ddb allocation to take effect.
10785                          */
10786                         if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
10787                                                  &old_crtc_state->wm.skl.ddb) &&
10788                             (update_pipes | modeset_pipes))
10789                                 intel_wait_for_vblank(dev_priv, pipe);
10790                 }
10791         }
10792
10793         update_pipes = modeset_pipes;
10794
10795         /*
10796          * Enable all pipes that needs a modeset and do not depends on other
10797          * pipes
10798          */
10799         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10800                 enum pipe pipe = crtc->pipe;
10801
10802                 if ((modeset_pipes & BIT(pipe)) == 0)
10803                         continue;
10804
10805                 if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
10806                     is_trans_port_sync_master(new_crtc_state) ||
10807                     (new_crtc_state->bigjoiner && !new_crtc_state->bigjoiner_slave))
10808                         continue;
10809
10810                 modeset_pipes &= ~BIT(pipe);
10811
10812                 intel_enable_crtc(state, crtc);
10813         }
10814
10815         /*
10816          * Then we enable all remaining pipes that depend on other
10817          * pipes: MST slaves and port sync masters, big joiner master
10818          */
10819         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10820                 enum pipe pipe = crtc->pipe;
10821
10822                 if ((modeset_pipes & BIT(pipe)) == 0)
10823                         continue;
10824
10825                 modeset_pipes &= ~BIT(pipe);
10826
10827                 intel_enable_crtc(state, crtc);
10828         }
10829
10830         /*
10831          * Finally we do the plane updates/etc. for all pipes that got enabled.
10832          */
10833         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10834                 enum pipe pipe = crtc->pipe;
10835
10836                 if ((update_pipes & BIT(pipe)) == 0)
10837                         continue;
10838
10839                 drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
10840                                                                         entries, I915_MAX_PIPES, pipe));
10841
10842                 entries[pipe] = new_crtc_state->wm.skl.ddb;
10843                 update_pipes &= ~BIT(pipe);
10844
10845                 intel_update_crtc(state, crtc);
10846         }
10847
10848         drm_WARN_ON(&dev_priv->drm, modeset_pipes);
10849         drm_WARN_ON(&dev_priv->drm, update_pipes);
10850 }
10851
10852 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
10853 {
10854         struct intel_atomic_state *state, *next;
10855         struct llist_node *freed;
10856
10857         freed = llist_del_all(&dev_priv->atomic_helper.free_list);
10858         llist_for_each_entry_safe(state, next, freed, freed)
10859                 drm_atomic_state_put(&state->base);
10860 }
10861
10862 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
10863 {
10864         struct drm_i915_private *dev_priv =
10865                 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
10866
10867         intel_atomic_helper_free_state(dev_priv);
10868 }
10869
10870 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
10871 {
10872         struct wait_queue_entry wait_fence, wait_reset;
10873         struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
10874
10875         init_wait_entry(&wait_fence, 0);
10876         init_wait_entry(&wait_reset, 0);
10877         for (;;) {
10878                 prepare_to_wait(&intel_state->commit_ready.wait,
10879                                 &wait_fence, TASK_UNINTERRUPTIBLE);
10880                 prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
10881                                               I915_RESET_MODESET),
10882                                 &wait_reset, TASK_UNINTERRUPTIBLE);
10883
10884
10885                 if (i915_sw_fence_done(&intel_state->commit_ready) ||
10886                     test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
10887                         break;
10888
10889                 schedule();
10890         }
10891         finish_wait(&intel_state->commit_ready.wait, &wait_fence);
10892         finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
10893                                   I915_RESET_MODESET),
10894                     &wait_reset);
10895 }
10896
10897 static void intel_cleanup_dsbs(struct intel_atomic_state *state)
10898 {
10899         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10900         struct intel_crtc *crtc;
10901         int i;
10902
10903         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10904                                             new_crtc_state, i)
10905                 intel_dsb_cleanup(old_crtc_state);
10906 }
10907
10908 static void intel_atomic_cleanup_work(struct work_struct *work)
10909 {
10910         struct intel_atomic_state *state =
10911                 container_of(work, struct intel_atomic_state, base.commit_work);
10912         struct drm_i915_private *i915 = to_i915(state->base.dev);
10913
10914         intel_cleanup_dsbs(state);
10915         drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
10916         drm_atomic_helper_commit_cleanup_done(&state->base);
10917         drm_atomic_state_put(&state->base);
10918
10919         intel_atomic_helper_free_state(i915);
10920 }
10921
10922 static void intel_atomic_prepare_plane_clear_colors(struct intel_atomic_state *state)
10923 {
10924         struct drm_i915_private *i915 = to_i915(state->base.dev);
10925         struct intel_plane *plane;
10926         struct intel_plane_state *plane_state;
10927         int i;
10928
10929         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
10930                 struct drm_framebuffer *fb = plane_state->hw.fb;
10931                 int ret;
10932
10933                 if (!fb ||
10934                     fb->modifier != I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC)
10935                         continue;
10936
10937                 /*
10938                  * The layout of the fast clear color value expected by HW
10939                  * (the DRM ABI requiring this value to be located in fb at offset 0 of plane#2):
10940                  * - 4 x 4 bytes per-channel value
10941                  *   (in surface type specific float/int format provided by the fb user)
10942                  * - 8 bytes native color value used by the display
10943                  *   (converted/written by GPU during a fast clear operation using the
10944                  *    above per-channel values)
10945                  *
10946                  * The commit's FB prepare hook already ensured that FB obj is pinned and the
10947                  * caller made sure that the object is synced wrt. the related color clear value
10948                  * GPU write on it.
10949                  */
10950                 ret = i915_gem_object_read_from_page(intel_fb_obj(fb),
10951                                                      fb->offsets[2] + 16,
10952                                                      &plane_state->ccval,
10953                                                      sizeof(plane_state->ccval));
10954                 /* The above could only fail if the FB obj has an unexpected backing store type. */
10955                 drm_WARN_ON(&i915->drm, ret);
10956         }
10957 }
10958
10959 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
10960 {
10961         struct drm_device *dev = state->base.dev;
10962         struct drm_i915_private *dev_priv = to_i915(dev);
10963         struct intel_crtc_state *new_crtc_state, *old_crtc_state;
10964         struct intel_crtc *crtc;
10965         u64 put_domains[I915_MAX_PIPES] = {};
10966         intel_wakeref_t wakeref = 0;
10967         int i;
10968
10969         intel_atomic_commit_fence_wait(state);
10970
10971         drm_atomic_helper_wait_for_dependencies(&state->base);
10972
10973         if (state->modeset)
10974                 wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
10975
10976         intel_atomic_prepare_plane_clear_colors(state);
10977
10978         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10979                                             new_crtc_state, i) {
10980                 if (intel_crtc_needs_modeset(new_crtc_state) ||
10981                     new_crtc_state->update_pipe) {
10982
10983                         put_domains[crtc->pipe] =
10984                                 modeset_get_crtc_power_domains(new_crtc_state);
10985                 }
10986         }
10987
10988         intel_commit_modeset_disables(state);
10989
10990         /* FIXME: Eventually get rid of our crtc->config pointer */
10991         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
10992                 crtc->config = new_crtc_state;
10993
10994         if (state->modeset) {
10995                 drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
10996
10997                 intel_set_cdclk_pre_plane_update(state);
10998
10999                 intel_modeset_verify_disabled(dev_priv, state);
11000         }
11001
11002         intel_sagv_pre_plane_update(state);
11003
11004         /* Complete the events for pipes that have now been disabled */
11005         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
11006                 bool modeset = intel_crtc_needs_modeset(new_crtc_state);
11007
11008                 /* Complete events for now disable pipes here. */
11009                 if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
11010                         spin_lock_irq(&dev->event_lock);
11011                         drm_crtc_send_vblank_event(&crtc->base,
11012                                                    new_crtc_state->uapi.event);
11013                         spin_unlock_irq(&dev->event_lock);
11014
11015                         new_crtc_state->uapi.event = NULL;
11016                 }
11017         }
11018
11019         if (state->modeset)
11020                 intel_encoders_update_prepare(state);
11021
11022         intel_dbuf_pre_plane_update(state);
11023
11024         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
11025                 if (new_crtc_state->uapi.async_flip)
11026                         intel_crtc_enable_flip_done(state, crtc);
11027         }
11028
11029         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
11030         dev_priv->display.commit_modeset_enables(state);
11031
11032         if (state->modeset) {
11033                 intel_encoders_update_complete(state);
11034
11035                 intel_set_cdclk_post_plane_update(state);
11036         }
11037
11038         /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
11039          * already, but still need the state for the delayed optimization. To
11040          * fix this:
11041          * - wrap the optimization/post_plane_update stuff into a per-crtc work.
11042          * - schedule that vblank worker _before_ calling hw_done
11043          * - at the start of commit_tail, cancel it _synchrously
11044          * - switch over to the vblank wait helper in the core after that since
11045          *   we don't need out special handling any more.
11046          */
11047         drm_atomic_helper_wait_for_flip_done(dev, &state->base);
11048
11049         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
11050                 if (new_crtc_state->uapi.async_flip)
11051                         intel_crtc_disable_flip_done(state, crtc);
11052
11053                 if (new_crtc_state->hw.active &&
11054                     !intel_crtc_needs_modeset(new_crtc_state) &&
11055                     !new_crtc_state->preload_luts &&
11056                     (new_crtc_state->uapi.color_mgmt_changed ||
11057                      new_crtc_state->update_pipe))
11058                         intel_color_load_luts(new_crtc_state);
11059         }
11060
11061         /*
11062          * Now that the vblank has passed, we can go ahead and program the
11063          * optimal watermarks on platforms that need two-step watermark
11064          * programming.
11065          *
11066          * TODO: Move this (and other cleanup) to an async worker eventually.
11067          */
11068         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
11069                                             new_crtc_state, i) {
11070                 /*
11071                  * Gen2 reports pipe underruns whenever all planes are disabled.
11072                  * So re-enable underrun reporting after some planes get enabled.
11073                  *
11074                  * We do this before .optimize_watermarks() so that we have a
11075                  * chance of catching underruns with the intermediate watermarks
11076                  * vs. the new plane configuration.
11077                  */
11078                 if (IS_GEN(dev_priv, 2) && planes_enabling(old_crtc_state, new_crtc_state))
11079                         intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
11080
11081                 if (dev_priv->display.optimize_watermarks)
11082                         dev_priv->display.optimize_watermarks(state, crtc);
11083         }
11084
11085         intel_dbuf_post_plane_update(state);
11086
11087         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11088                 intel_post_plane_update(state, crtc);
11089
11090                 modeset_put_crtc_power_domains(crtc, put_domains[crtc->pipe]);
11091
11092                 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
11093
11094                 /*
11095                  * DSB cleanup is done in cleanup_work aligning with framebuffer
11096                  * cleanup. So copy and reset the dsb structure to sync with
11097                  * commit_done and later do dsb cleanup in cleanup_work.
11098                  */
11099                 old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
11100         }
11101
11102         /* Underruns don't always raise interrupts, so check manually */
11103         intel_check_cpu_fifo_underruns(dev_priv);
11104         intel_check_pch_fifo_underruns(dev_priv);
11105
11106         if (state->modeset)
11107                 intel_verify_planes(state);
11108
11109         intel_sagv_post_plane_update(state);
11110
11111         drm_atomic_helper_commit_hw_done(&state->base);
11112
11113         if (state->modeset) {
11114                 /* As one of the primary mmio accessors, KMS has a high
11115                  * likelihood of triggering bugs in unclaimed access. After we
11116                  * finish modesetting, see if an error has been flagged, and if
11117                  * so enable debugging for the next modeset - and hope we catch
11118                  * the culprit.
11119                  */
11120                 intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
11121                 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
11122         }
11123         intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
11124
11125         /*
11126          * Defer the cleanup of the old state to a separate worker to not
11127          * impede the current task (userspace for blocking modesets) that
11128          * are executed inline. For out-of-line asynchronous modesets/flips,
11129          * deferring to a new worker seems overkill, but we would place a
11130          * schedule point (cond_resched()) here anyway to keep latencies
11131          * down.
11132          */
11133         INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
11134         queue_work(system_highpri_wq, &state->base.commit_work);
11135 }
11136
11137 static void intel_atomic_commit_work(struct work_struct *work)
11138 {
11139         struct intel_atomic_state *state =
11140                 container_of(work, struct intel_atomic_state, base.commit_work);
11141
11142         intel_atomic_commit_tail(state);
11143 }
11144
11145 static int __i915_sw_fence_call
11146 intel_atomic_commit_ready(struct i915_sw_fence *fence,
11147                           enum i915_sw_fence_notify notify)
11148 {
11149         struct intel_atomic_state *state =
11150                 container_of(fence, struct intel_atomic_state, commit_ready);
11151
11152         switch (notify) {
11153         case FENCE_COMPLETE:
11154                 /* we do blocking waits in the worker, nothing to do here */
11155                 break;
11156         case FENCE_FREE:
11157                 {
11158                         struct intel_atomic_helper *helper =
11159                                 &to_i915(state->base.dev)->atomic_helper;
11160
11161                         if (llist_add(&state->freed, &helper->free_list))
11162                                 schedule_work(&helper->free_work);
11163                         break;
11164                 }
11165         }
11166
11167         return NOTIFY_DONE;
11168 }
11169
11170 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
11171 {
11172         struct intel_plane_state *old_plane_state, *new_plane_state;
11173         struct intel_plane *plane;
11174         int i;
11175
11176         for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
11177                                              new_plane_state, i)
11178                 intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
11179                                         to_intel_frontbuffer(new_plane_state->hw.fb),
11180                                         plane->frontbuffer_bit);
11181 }
11182
11183 static int intel_atomic_commit(struct drm_device *dev,
11184                                struct drm_atomic_state *_state,
11185                                bool nonblock)
11186 {
11187         struct intel_atomic_state *state = to_intel_atomic_state(_state);
11188         struct drm_i915_private *dev_priv = to_i915(dev);
11189         int ret = 0;
11190
11191         state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
11192
11193         drm_atomic_state_get(&state->base);
11194         i915_sw_fence_init(&state->commit_ready,
11195                            intel_atomic_commit_ready);
11196
11197         /*
11198          * The intel_legacy_cursor_update() fast path takes care
11199          * of avoiding the vblank waits for simple cursor
11200          * movement and flips. For cursor on/off and size changes,
11201          * we want to perform the vblank waits so that watermark
11202          * updates happen during the correct frames. Gen9+ have
11203          * double buffered watermarks and so shouldn't need this.
11204          *
11205          * Unset state->legacy_cursor_update before the call to
11206          * drm_atomic_helper_setup_commit() because otherwise
11207          * drm_atomic_helper_wait_for_flip_done() is a noop and
11208          * we get FIFO underruns because we didn't wait
11209          * for vblank.
11210          *
11211          * FIXME doing watermarks and fb cleanup from a vblank worker
11212          * (assuming we had any) would solve these problems.
11213          */
11214         if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
11215                 struct intel_crtc_state *new_crtc_state;
11216                 struct intel_crtc *crtc;
11217                 int i;
11218
11219                 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
11220                         if (new_crtc_state->wm.need_postvbl_update ||
11221                             new_crtc_state->update_wm_post)
11222                                 state->base.legacy_cursor_update = false;
11223         }
11224
11225         ret = intel_atomic_prepare_commit(state);
11226         if (ret) {
11227                 drm_dbg_atomic(&dev_priv->drm,
11228                                "Preparing state failed with %i\n", ret);
11229                 i915_sw_fence_commit(&state->commit_ready);
11230                 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
11231                 return ret;
11232         }
11233
11234         ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
11235         if (!ret)
11236                 ret = drm_atomic_helper_swap_state(&state->base, true);
11237         if (!ret)
11238                 intel_atomic_swap_global_state(state);
11239
11240         if (ret) {
11241                 struct intel_crtc_state *new_crtc_state;
11242                 struct intel_crtc *crtc;
11243                 int i;
11244
11245                 i915_sw_fence_commit(&state->commit_ready);
11246
11247                 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
11248                         intel_dsb_cleanup(new_crtc_state);
11249
11250                 drm_atomic_helper_cleanup_planes(dev, &state->base);
11251                 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
11252                 return ret;
11253         }
11254         intel_shared_dpll_swap_state(state);
11255         intel_atomic_track_fbs(state);
11256
11257         drm_atomic_state_get(&state->base);
11258         INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
11259
11260         i915_sw_fence_commit(&state->commit_ready);
11261         if (nonblock && state->modeset) {
11262                 queue_work(dev_priv->modeset_wq, &state->base.commit_work);
11263         } else if (nonblock) {
11264                 queue_work(dev_priv->flip_wq, &state->base.commit_work);
11265         } else {
11266                 if (state->modeset)
11267                         flush_workqueue(dev_priv->modeset_wq);
11268                 intel_atomic_commit_tail(state);
11269         }
11270
11271         return 0;
11272 }
11273
11274 struct wait_rps_boost {
11275         struct wait_queue_entry wait;
11276
11277         struct drm_crtc *crtc;
11278         struct i915_request *request;
11279 };
11280
11281 static int do_rps_boost(struct wait_queue_entry *_wait,
11282                         unsigned mode, int sync, void *key)
11283 {
11284         struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
11285         struct i915_request *rq = wait->request;
11286
11287         /*
11288          * If we missed the vblank, but the request is already running it
11289          * is reasonable to assume that it will complete before the next
11290          * vblank without our intervention, so leave RPS alone.
11291          */
11292         if (!i915_request_started(rq))
11293                 intel_rps_boost(rq);
11294         i915_request_put(rq);
11295
11296         drm_crtc_vblank_put(wait->crtc);
11297
11298         list_del(&wait->wait.entry);
11299         kfree(wait);
11300         return 1;
11301 }
11302
11303 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
11304                                        struct dma_fence *fence)
11305 {
11306         struct wait_rps_boost *wait;
11307
11308         if (!dma_fence_is_i915(fence))
11309                 return;
11310
11311         if (INTEL_GEN(to_i915(crtc->dev)) < 6)
11312                 return;
11313
11314         if (drm_crtc_vblank_get(crtc))
11315                 return;
11316
11317         wait = kmalloc(sizeof(*wait), GFP_KERNEL);
11318         if (!wait) {
11319                 drm_crtc_vblank_put(crtc);
11320                 return;
11321         }
11322
11323         wait->request = to_request(dma_fence_get(fence));
11324         wait->crtc = crtc;
11325
11326         wait->wait.func = do_rps_boost;
11327         wait->wait.flags = 0;
11328
11329         add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
11330 }
11331
11332 int intel_plane_pin_fb(struct intel_plane_state *plane_state)
11333 {
11334         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
11335         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11336         struct drm_framebuffer *fb = plane_state->hw.fb;
11337         struct i915_vma *vma;
11338
11339         if (plane->id == PLANE_CURSOR &&
11340             INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
11341                 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11342                 const int align = intel_cursor_alignment(dev_priv);
11343                 int err;
11344
11345                 err = i915_gem_object_attach_phys(obj, align);
11346                 if (err)
11347                         return err;
11348         }
11349
11350         vma = intel_pin_and_fence_fb_obj(fb,
11351                                          &plane_state->view,
11352                                          intel_plane_uses_fence(plane_state),
11353                                          &plane_state->flags);
11354         if (IS_ERR(vma))
11355                 return PTR_ERR(vma);
11356
11357         plane_state->vma = vma;
11358
11359         return 0;
11360 }
11361
11362 void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
11363 {
11364         struct i915_vma *vma;
11365
11366         vma = fetch_and_zero(&old_plane_state->vma);
11367         if (vma)
11368                 intel_unpin_fb_vma(vma, old_plane_state->flags);
11369 }
11370
11371 /**
11372  * intel_prepare_plane_fb - Prepare fb for usage on plane
11373  * @_plane: drm plane to prepare for
11374  * @_new_plane_state: the plane state being prepared
11375  *
11376  * Prepares a framebuffer for usage on a display plane.  Generally this
11377  * involves pinning the underlying object and updating the frontbuffer tracking
11378  * bits.  Some older platforms need special physical address handling for
11379  * cursor planes.
11380  *
11381  * Returns 0 on success, negative error code on failure.
11382  */
11383 int
11384 intel_prepare_plane_fb(struct drm_plane *_plane,
11385                        struct drm_plane_state *_new_plane_state)
11386 {
11387         struct i915_sched_attr attr = {
11388                 .priority = I915_USER_PRIORITY(I915_PRIORITY_DISPLAY),
11389         };
11390         struct intel_plane *plane = to_intel_plane(_plane);
11391         struct intel_plane_state *new_plane_state =
11392                 to_intel_plane_state(_new_plane_state);
11393         struct intel_atomic_state *state =
11394                 to_intel_atomic_state(new_plane_state->uapi.state);
11395         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11396         const struct intel_plane_state *old_plane_state =
11397                 intel_atomic_get_old_plane_state(state, plane);
11398         struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
11399         struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
11400         int ret;
11401
11402         if (old_obj) {
11403                 const struct intel_crtc_state *crtc_state =
11404                         intel_atomic_get_new_crtc_state(state,
11405                                                         to_intel_crtc(old_plane_state->hw.crtc));
11406
11407                 /* Big Hammer, we also need to ensure that any pending
11408                  * MI_WAIT_FOR_EVENT inside a user batch buffer on the
11409                  * current scanout is retired before unpinning the old
11410                  * framebuffer. Note that we rely on userspace rendering
11411                  * into the buffer attached to the pipe they are waiting
11412                  * on. If not, userspace generates a GPU hang with IPEHR
11413                  * point to the MI_WAIT_FOR_EVENT.
11414                  *
11415                  * This should only fail upon a hung GPU, in which case we
11416                  * can safely continue.
11417                  */
11418                 if (intel_crtc_needs_modeset(crtc_state)) {
11419                         ret = i915_sw_fence_await_reservation(&state->commit_ready,
11420                                                               old_obj->base.resv, NULL,
11421                                                               false, 0,
11422                                                               GFP_KERNEL);
11423                         if (ret < 0)
11424                                 return ret;
11425                 }
11426         }
11427
11428         if (new_plane_state->uapi.fence) { /* explicit fencing */
11429                 i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
11430                                              &attr);
11431                 ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
11432                                                     new_plane_state->uapi.fence,
11433                                                     i915_fence_timeout(dev_priv),
11434                                                     GFP_KERNEL);
11435                 if (ret < 0)
11436                         return ret;
11437         }
11438
11439         if (!obj)
11440                 return 0;
11441
11442         ret = i915_gem_object_pin_pages(obj);
11443         if (ret)
11444                 return ret;
11445
11446         ret = intel_plane_pin_fb(new_plane_state);
11447
11448         i915_gem_object_unpin_pages(obj);
11449         if (ret)
11450                 return ret;
11451
11452         i915_gem_object_wait_priority(obj, 0, &attr);
11453         i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
11454
11455         if (!new_plane_state->uapi.fence) { /* implicit fencing */
11456                 struct dma_fence *fence;
11457
11458                 ret = i915_sw_fence_await_reservation(&state->commit_ready,
11459                                                       obj->base.resv, NULL,
11460                                                       false,
11461                                                       i915_fence_timeout(dev_priv),
11462                                                       GFP_KERNEL);
11463                 if (ret < 0)
11464                         goto unpin_fb;
11465
11466                 fence = dma_resv_get_excl_rcu(obj->base.resv);
11467                 if (fence) {
11468                         add_rps_boost_after_vblank(new_plane_state->hw.crtc,
11469                                                    fence);
11470                         dma_fence_put(fence);
11471                 }
11472         } else {
11473                 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
11474                                            new_plane_state->uapi.fence);
11475         }
11476
11477         /*
11478          * We declare pageflips to be interactive and so merit a small bias
11479          * towards upclocking to deliver the frame on time. By only changing
11480          * the RPS thresholds to sample more regularly and aim for higher
11481          * clocks we can hopefully deliver low power workloads (like kodi)
11482          * that are not quite steady state without resorting to forcing
11483          * maximum clocks following a vblank miss (see do_rps_boost()).
11484          */
11485         if (!state->rps_interactive) {
11486                 intel_rps_mark_interactive(&dev_priv->gt.rps, true);
11487                 state->rps_interactive = true;
11488         }
11489
11490         return 0;
11491
11492 unpin_fb:
11493         intel_plane_unpin_fb(new_plane_state);
11494
11495         return ret;
11496 }
11497
11498 /**
11499  * intel_cleanup_plane_fb - Cleans up an fb after plane use
11500  * @plane: drm plane to clean up for
11501  * @_old_plane_state: the state from the previous modeset
11502  *
11503  * Cleans up a framebuffer that has just been removed from a plane.
11504  */
11505 void
11506 intel_cleanup_plane_fb(struct drm_plane *plane,
11507                        struct drm_plane_state *_old_plane_state)
11508 {
11509         struct intel_plane_state *old_plane_state =
11510                 to_intel_plane_state(_old_plane_state);
11511         struct intel_atomic_state *state =
11512                 to_intel_atomic_state(old_plane_state->uapi.state);
11513         struct drm_i915_private *dev_priv = to_i915(plane->dev);
11514         struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
11515
11516         if (!obj)
11517                 return;
11518
11519         if (state->rps_interactive) {
11520                 intel_rps_mark_interactive(&dev_priv->gt.rps, false);
11521                 state->rps_interactive = false;
11522         }
11523
11524         /* Should only be called after a successful intel_prepare_plane_fb()! */
11525         intel_plane_unpin_fb(old_plane_state);
11526 }
11527
11528 /**
11529  * intel_plane_destroy - destroy a plane
11530  * @plane: plane to destroy
11531  *
11532  * Common destruction function for all types of planes (primary, cursor,
11533  * sprite).
11534  */
11535 void intel_plane_destroy(struct drm_plane *plane)
11536 {
11537         drm_plane_cleanup(plane);
11538         kfree(to_intel_plane(plane));
11539 }
11540
11541 static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
11542 {
11543         struct intel_plane *plane;
11544
11545         for_each_intel_plane(&dev_priv->drm, plane) {
11546                 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
11547                                                                   plane->pipe);
11548
11549                 plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
11550         }
11551 }
11552
11553
11554 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
11555                                       struct drm_file *file)
11556 {
11557         struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
11558         struct drm_crtc *drmmode_crtc;
11559         struct intel_crtc *crtc;
11560
11561         drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
11562         if (!drmmode_crtc)
11563                 return -ENOENT;
11564
11565         crtc = to_intel_crtc(drmmode_crtc);
11566         pipe_from_crtc_id->pipe = crtc->pipe;
11567
11568         return 0;
11569 }
11570
11571 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
11572 {
11573         struct drm_device *dev = encoder->base.dev;
11574         struct intel_encoder *source_encoder;
11575         u32 possible_clones = 0;
11576
11577         for_each_intel_encoder(dev, source_encoder) {
11578                 if (encoders_cloneable(encoder, source_encoder))
11579                         possible_clones |= drm_encoder_mask(&source_encoder->base);
11580         }
11581
11582         return possible_clones;
11583 }
11584
11585 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
11586 {
11587         struct drm_device *dev = encoder->base.dev;
11588         struct intel_crtc *crtc;
11589         u32 possible_crtcs = 0;
11590
11591         for_each_intel_crtc(dev, crtc) {
11592                 if (encoder->pipe_mask & BIT(crtc->pipe))
11593                         possible_crtcs |= drm_crtc_mask(&crtc->base);
11594         }
11595
11596         return possible_crtcs;
11597 }
11598
11599 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
11600 {
11601         if (!IS_MOBILE(dev_priv))
11602                 return false;
11603
11604         if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
11605                 return false;
11606
11607         if (IS_GEN(dev_priv, 5) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
11608                 return false;
11609
11610         return true;
11611 }
11612
11613 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
11614 {
11615         if (INTEL_GEN(dev_priv) >= 9)
11616                 return false;
11617
11618         if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
11619                 return false;
11620
11621         if (HAS_PCH_LPT_H(dev_priv) &&
11622             intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
11623                 return false;
11624
11625         /* DDI E can't be used if DDI A requires 4 lanes */
11626         if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
11627                 return false;
11628
11629         if (!dev_priv->vbt.int_crt_support)
11630                 return false;
11631
11632         return true;
11633 }
11634
11635 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
11636 {
11637         struct intel_encoder *encoder;
11638         bool dpd_is_edp = false;
11639
11640         intel_pps_unlock_regs_wa(dev_priv);
11641
11642         if (!HAS_DISPLAY(dev_priv))
11643                 return;
11644
11645         if (IS_ALDERLAKE_S(dev_priv)) {
11646                 intel_ddi_init(dev_priv, PORT_A);
11647                 intel_ddi_init(dev_priv, PORT_TC1);
11648                 intel_ddi_init(dev_priv, PORT_TC2);
11649                 intel_ddi_init(dev_priv, PORT_TC3);
11650                 intel_ddi_init(dev_priv, PORT_TC4);
11651         } else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv)) {
11652                 intel_ddi_init(dev_priv, PORT_A);
11653                 intel_ddi_init(dev_priv, PORT_B);
11654                 intel_ddi_init(dev_priv, PORT_TC1);
11655                 intel_ddi_init(dev_priv, PORT_TC2);
11656         } else if (INTEL_GEN(dev_priv) >= 12) {
11657                 intel_ddi_init(dev_priv, PORT_A);
11658                 intel_ddi_init(dev_priv, PORT_B);
11659                 intel_ddi_init(dev_priv, PORT_TC1);
11660                 intel_ddi_init(dev_priv, PORT_TC2);
11661                 intel_ddi_init(dev_priv, PORT_TC3);
11662                 intel_ddi_init(dev_priv, PORT_TC4);
11663                 intel_ddi_init(dev_priv, PORT_TC5);
11664                 intel_ddi_init(dev_priv, PORT_TC6);
11665                 icl_dsi_init(dev_priv);
11666         } else if (IS_JSL_EHL(dev_priv)) {
11667                 intel_ddi_init(dev_priv, PORT_A);
11668                 intel_ddi_init(dev_priv, PORT_B);
11669                 intel_ddi_init(dev_priv, PORT_C);
11670                 intel_ddi_init(dev_priv, PORT_D);
11671                 icl_dsi_init(dev_priv);
11672         } else if (IS_GEN(dev_priv, 11)) {
11673                 intel_ddi_init(dev_priv, PORT_A);
11674                 intel_ddi_init(dev_priv, PORT_B);
11675                 intel_ddi_init(dev_priv, PORT_C);
11676                 intel_ddi_init(dev_priv, PORT_D);
11677                 intel_ddi_init(dev_priv, PORT_E);
11678                 /*
11679                  * On some ICL SKUs port F is not present. No strap bits for
11680                  * this, so rely on VBT.
11681                  * Work around broken VBTs on SKUs known to have no port F.
11682                  */
11683                 if (IS_ICL_WITH_PORT_F(dev_priv) &&
11684                     intel_bios_is_port_present(dev_priv, PORT_F))
11685                         intel_ddi_init(dev_priv, PORT_F);
11686
11687                 icl_dsi_init(dev_priv);
11688         } else if (IS_GEN9_LP(dev_priv)) {
11689                 /*
11690                  * FIXME: Broxton doesn't support port detection via the
11691                  * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
11692                  * detect the ports.
11693                  */
11694                 intel_ddi_init(dev_priv, PORT_A);
11695                 intel_ddi_init(dev_priv, PORT_B);
11696                 intel_ddi_init(dev_priv, PORT_C);
11697
11698                 vlv_dsi_init(dev_priv);
11699         } else if (HAS_DDI(dev_priv)) {
11700                 int found;
11701
11702                 if (intel_ddi_crt_present(dev_priv))
11703                         intel_crt_init(dev_priv);
11704
11705                 /*
11706                  * Haswell uses DDI functions to detect digital outputs.
11707                  * On SKL pre-D0 the strap isn't connected. Later SKUs may or
11708                  * may not have it - it was supposed to be fixed by the same
11709                  * time we stopped using straps. Assume it's there.
11710                  */
11711                 found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
11712                 /* WaIgnoreDDIAStrap: skl */
11713                 if (found || IS_GEN9_BC(dev_priv))
11714                         intel_ddi_init(dev_priv, PORT_A);
11715
11716                 /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
11717                  * register */
11718                 if (HAS_PCH_TGP(dev_priv)) {
11719                         /* W/A due to lack of STRAP config on TGP PCH*/
11720                         found = (SFUSE_STRAP_DDIB_DETECTED |
11721                                  SFUSE_STRAP_DDIC_DETECTED |
11722                                  SFUSE_STRAP_DDID_DETECTED);
11723                 } else {
11724                         found = intel_de_read(dev_priv, SFUSE_STRAP);
11725                 }
11726
11727                 if (found & SFUSE_STRAP_DDIB_DETECTED)
11728                         intel_ddi_init(dev_priv, PORT_B);
11729                 if (found & SFUSE_STRAP_DDIC_DETECTED)
11730                         intel_ddi_init(dev_priv, PORT_C);
11731                 if (found & SFUSE_STRAP_DDID_DETECTED)
11732                         intel_ddi_init(dev_priv, PORT_D);
11733                 if (found & SFUSE_STRAP_DDIF_DETECTED)
11734                         intel_ddi_init(dev_priv, PORT_F);
11735                 /*
11736                  * On SKL we don't have a way to detect DDI-E so we rely on VBT.
11737                  */
11738                 if (IS_GEN9_BC(dev_priv) &&
11739                     intel_bios_is_port_present(dev_priv, PORT_E))
11740                         intel_ddi_init(dev_priv, PORT_E);
11741
11742         } else if (HAS_PCH_SPLIT(dev_priv)) {
11743                 int found;
11744
11745                 /*
11746                  * intel_edp_init_connector() depends on this completing first,
11747                  * to prevent the registration of both eDP and LVDS and the
11748                  * incorrect sharing of the PPS.
11749                  */
11750                 intel_lvds_init(dev_priv);
11751                 intel_crt_init(dev_priv);
11752
11753                 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
11754
11755                 if (ilk_has_edp_a(dev_priv))
11756                         intel_dp_init(dev_priv, DP_A, PORT_A);
11757
11758                 if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
11759                         /* PCH SDVOB multiplex with HDMIB */
11760                         found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
11761                         if (!found)
11762                                 intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
11763                         if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
11764                                 intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
11765                 }
11766
11767                 if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
11768                         intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
11769
11770                 if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
11771                         intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
11772
11773                 if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
11774                         intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
11775
11776                 if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
11777                         intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
11778         } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
11779                 bool has_edp, has_port;
11780
11781                 if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
11782                         intel_crt_init(dev_priv);
11783
11784                 /*
11785                  * The DP_DETECTED bit is the latched state of the DDC
11786                  * SDA pin at boot. However since eDP doesn't require DDC
11787                  * (no way to plug in a DP->HDMI dongle) the DDC pins for
11788                  * eDP ports may have been muxed to an alternate function.
11789                  * Thus we can't rely on the DP_DETECTED bit alone to detect
11790                  * eDP ports. Consult the VBT as well as DP_DETECTED to
11791                  * detect eDP ports.
11792                  *
11793                  * Sadly the straps seem to be missing sometimes even for HDMI
11794                  * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
11795                  * and VBT for the presence of the port. Additionally we can't
11796                  * trust the port type the VBT declares as we've seen at least
11797                  * HDMI ports that the VBT claim are DP or eDP.
11798                  */
11799                 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
11800                 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
11801                 if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
11802                         has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
11803                 if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
11804                         intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
11805
11806                 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
11807                 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
11808                 if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
11809                         has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
11810                 if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
11811                         intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
11812
11813                 if (IS_CHERRYVIEW(dev_priv)) {
11814                         /*
11815                          * eDP not supported on port D,
11816                          * so no need to worry about it
11817                          */
11818                         has_port = intel_bios_is_port_present(dev_priv, PORT_D);
11819                         if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
11820                                 intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
11821                         if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
11822                                 intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
11823                 }
11824
11825                 vlv_dsi_init(dev_priv);
11826         } else if (IS_PINEVIEW(dev_priv)) {
11827                 intel_lvds_init(dev_priv);
11828                 intel_crt_init(dev_priv);
11829         } else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
11830                 bool found = false;
11831
11832                 if (IS_MOBILE(dev_priv))
11833                         intel_lvds_init(dev_priv);
11834
11835                 intel_crt_init(dev_priv);
11836
11837                 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
11838                         drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
11839                         found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
11840                         if (!found && IS_G4X(dev_priv)) {
11841                                 drm_dbg_kms(&dev_priv->drm,
11842                                             "probing HDMI on SDVOB\n");
11843                                 intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
11844                         }
11845
11846                         if (!found && IS_G4X(dev_priv))
11847                                 intel_dp_init(dev_priv, DP_B, PORT_B);
11848                 }
11849
11850                 /* Before G4X SDVOC doesn't have its own detect register */
11851
11852                 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
11853                         drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
11854                         found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
11855                 }
11856
11857                 if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
11858
11859                         if (IS_G4X(dev_priv)) {
11860                                 drm_dbg_kms(&dev_priv->drm,
11861                                             "probing HDMI on SDVOC\n");
11862                                 intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
11863                         }
11864                         if (IS_G4X(dev_priv))
11865                                 intel_dp_init(dev_priv, DP_C, PORT_C);
11866                 }
11867
11868                 if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
11869                         intel_dp_init(dev_priv, DP_D, PORT_D);
11870
11871                 if (SUPPORTS_TV(dev_priv))
11872                         intel_tv_init(dev_priv);
11873         } else if (IS_GEN(dev_priv, 2)) {
11874                 if (IS_I85X(dev_priv))
11875                         intel_lvds_init(dev_priv);
11876
11877                 intel_crt_init(dev_priv);
11878                 intel_dvo_init(dev_priv);
11879         }
11880
11881         for_each_intel_encoder(&dev_priv->drm, encoder) {
11882                 encoder->base.possible_crtcs =
11883                         intel_encoder_possible_crtcs(encoder);
11884                 encoder->base.possible_clones =
11885                         intel_encoder_possible_clones(encoder);
11886         }
11887
11888         intel_init_pch_refclk(dev_priv);
11889
11890         drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
11891 }
11892
11893 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11894 {
11895         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11896
11897         drm_framebuffer_cleanup(fb);
11898         intel_frontbuffer_put(intel_fb->frontbuffer);
11899
11900         kfree(intel_fb);
11901 }
11902
11903 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
11904                                                 struct drm_file *file,
11905                                                 unsigned int *handle)
11906 {
11907         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11908         struct drm_i915_private *i915 = to_i915(obj->base.dev);
11909
11910         if (obj->userptr.mm) {
11911                 drm_dbg(&i915->drm,
11912                         "attempting to use a userptr for a framebuffer, denied\n");
11913                 return -EINVAL;
11914         }
11915
11916         return drm_gem_handle_create(file, &obj->base, handle);
11917 }
11918
11919 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
11920                                         struct drm_file *file,
11921                                         unsigned flags, unsigned color,
11922                                         struct drm_clip_rect *clips,
11923                                         unsigned num_clips)
11924 {
11925         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11926
11927         i915_gem_object_flush_if_display(obj);
11928         intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
11929
11930         return 0;
11931 }
11932
11933 static const struct drm_framebuffer_funcs intel_fb_funcs = {
11934         .destroy = intel_user_framebuffer_destroy,
11935         .create_handle = intel_user_framebuffer_create_handle,
11936         .dirty = intel_user_framebuffer_dirty,
11937 };
11938
11939 static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
11940                                   struct drm_i915_gem_object *obj,
11941                                   struct drm_mode_fb_cmd2 *mode_cmd)
11942 {
11943         struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
11944         struct drm_framebuffer *fb = &intel_fb->base;
11945         u32 max_stride;
11946         unsigned int tiling, stride;
11947         int ret = -EINVAL;
11948         int i;
11949
11950         intel_fb->frontbuffer = intel_frontbuffer_get(obj);
11951         if (!intel_fb->frontbuffer)
11952                 return -ENOMEM;
11953
11954         i915_gem_object_lock(obj, NULL);
11955         tiling = i915_gem_object_get_tiling(obj);
11956         stride = i915_gem_object_get_stride(obj);
11957         i915_gem_object_unlock(obj);
11958
11959         if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
11960                 /*
11961                  * If there's a fence, enforce that
11962                  * the fb modifier and tiling mode match.
11963                  */
11964                 if (tiling != I915_TILING_NONE &&
11965                     tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
11966                         drm_dbg_kms(&dev_priv->drm,
11967                                     "tiling_mode doesn't match fb modifier\n");
11968                         goto err;
11969                 }
11970         } else {
11971                 if (tiling == I915_TILING_X) {
11972                         mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
11973                 } else if (tiling == I915_TILING_Y) {
11974                         drm_dbg_kms(&dev_priv->drm,
11975                                     "No Y tiling for legacy addfb\n");
11976                         goto err;
11977                 }
11978         }
11979
11980         if (!drm_any_plane_has_format(&dev_priv->drm,
11981                                       mode_cmd->pixel_format,
11982                                       mode_cmd->modifier[0])) {
11983                 struct drm_format_name_buf format_name;
11984
11985                 drm_dbg_kms(&dev_priv->drm,
11986                             "unsupported pixel format %s / modifier 0x%llx\n",
11987                             drm_get_format_name(mode_cmd->pixel_format,
11988                                                 &format_name),
11989                             mode_cmd->modifier[0]);
11990                 goto err;
11991         }
11992
11993         /*
11994          * gen2/3 display engine uses the fence if present,
11995          * so the tiling mode must match the fb modifier exactly.
11996          */
11997         if (INTEL_GEN(dev_priv) < 4 &&
11998             tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
11999                 drm_dbg_kms(&dev_priv->drm,
12000                             "tiling_mode must match fb modifier exactly on gen2/3\n");
12001                 goto err;
12002         }
12003
12004         max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
12005                                          mode_cmd->modifier[0]);
12006         if (mode_cmd->pitches[0] > max_stride) {
12007                 drm_dbg_kms(&dev_priv->drm,
12008                             "%s pitch (%u) must be at most %d\n",
12009                             mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
12010                             "tiled" : "linear",
12011                             mode_cmd->pitches[0], max_stride);
12012                 goto err;
12013         }
12014
12015         /*
12016          * If there's a fence, enforce that
12017          * the fb pitch and fence stride match.
12018          */
12019         if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
12020                 drm_dbg_kms(&dev_priv->drm,
12021                             "pitch (%d) must match tiling stride (%d)\n",
12022                             mode_cmd->pitches[0], stride);
12023                 goto err;
12024         }
12025
12026         /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12027         if (mode_cmd->offsets[0] != 0) {
12028                 drm_dbg_kms(&dev_priv->drm,
12029                             "plane 0 offset (0x%08x) must be 0\n",
12030                             mode_cmd->offsets[0]);
12031                 goto err;
12032         }
12033
12034         drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
12035
12036         for (i = 0; i < fb->format->num_planes; i++) {
12037                 u32 stride_alignment;
12038
12039                 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
12040                         drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
12041                                     i);
12042                         goto err;
12043                 }
12044
12045                 stride_alignment = intel_fb_stride_alignment(fb, i);
12046                 if (fb->pitches[i] & (stride_alignment - 1)) {
12047                         drm_dbg_kms(&dev_priv->drm,
12048                                     "plane %d pitch (%d) must be at least %u byte aligned\n",
12049                                     i, fb->pitches[i], stride_alignment);
12050                         goto err;
12051                 }
12052
12053                 if (is_gen12_ccs_plane(fb, i) && !is_gen12_ccs_cc_plane(fb, i)) {
12054                         int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
12055
12056                         if (fb->pitches[i] != ccs_aux_stride) {
12057                                 drm_dbg_kms(&dev_priv->drm,
12058                                             "ccs aux plane %d pitch (%d) must be %d\n",
12059                                             i,
12060                                             fb->pitches[i], ccs_aux_stride);
12061                                 goto err;
12062                         }
12063                 }
12064
12065                 fb->obj[i] = &obj->base;
12066         }
12067
12068         ret = intel_fill_fb_info(dev_priv, fb);
12069         if (ret)
12070                 goto err;
12071
12072         ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
12073         if (ret) {
12074                 drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
12075                 goto err;
12076         }
12077
12078         return 0;
12079
12080 err:
12081         intel_frontbuffer_put(intel_fb->frontbuffer);
12082         return ret;
12083 }
12084
12085 static struct drm_framebuffer *
12086 intel_user_framebuffer_create(struct drm_device *dev,
12087                               struct drm_file *filp,
12088                               const struct drm_mode_fb_cmd2 *user_mode_cmd)
12089 {
12090         struct drm_framebuffer *fb;
12091         struct drm_i915_gem_object *obj;
12092         struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
12093
12094         obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
12095         if (!obj)
12096                 return ERR_PTR(-ENOENT);
12097
12098         fb = intel_framebuffer_create(obj, &mode_cmd);
12099         i915_gem_object_put(obj);
12100
12101         return fb;
12102 }
12103
12104 static enum drm_mode_status
12105 intel_mode_valid(struct drm_device *dev,
12106                  const struct drm_display_mode *mode)
12107 {
12108         struct drm_i915_private *dev_priv = to_i915(dev);
12109         int hdisplay_max, htotal_max;
12110         int vdisplay_max, vtotal_max;
12111
12112         /*
12113          * Can't reject DBLSCAN here because Xorg ddxen can add piles
12114          * of DBLSCAN modes to the output's mode list when they detect
12115          * the scaling mode property on the connector. And they don't
12116          * ask the kernel to validate those modes in any way until
12117          * modeset time at which point the client gets a protocol error.
12118          * So in order to not upset those clients we silently ignore the
12119          * DBLSCAN flag on such connectors. For other connectors we will
12120          * reject modes with the DBLSCAN flag in encoder->compute_config().
12121          * And we always reject DBLSCAN modes in connector->mode_valid()
12122          * as we never want such modes on the connector's mode list.
12123          */
12124
12125         if (mode->vscan > 1)
12126                 return MODE_NO_VSCAN;
12127
12128         if (mode->flags & DRM_MODE_FLAG_HSKEW)
12129                 return MODE_H_ILLEGAL;
12130
12131         if (mode->flags & (DRM_MODE_FLAG_CSYNC |
12132                            DRM_MODE_FLAG_NCSYNC |
12133                            DRM_MODE_FLAG_PCSYNC))
12134                 return MODE_HSYNC;
12135
12136         if (mode->flags & (DRM_MODE_FLAG_BCAST |
12137                            DRM_MODE_FLAG_PIXMUX |
12138                            DRM_MODE_FLAG_CLKDIV2))
12139                 return MODE_BAD;
12140
12141         /* Transcoder timing limits */
12142         if (INTEL_GEN(dev_priv) >= 11) {
12143                 hdisplay_max = 16384;
12144                 vdisplay_max = 8192;
12145                 htotal_max = 16384;
12146                 vtotal_max = 8192;
12147         } else if (INTEL_GEN(dev_priv) >= 9 ||
12148                    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12149                 hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
12150                 vdisplay_max = 4096;
12151                 htotal_max = 8192;
12152                 vtotal_max = 8192;
12153         } else if (INTEL_GEN(dev_priv) >= 3) {
12154                 hdisplay_max = 4096;
12155                 vdisplay_max = 4096;
12156                 htotal_max = 8192;
12157                 vtotal_max = 8192;
12158         } else {
12159                 hdisplay_max = 2048;
12160                 vdisplay_max = 2048;
12161                 htotal_max = 4096;
12162                 vtotal_max = 4096;
12163         }
12164
12165         if (mode->hdisplay > hdisplay_max ||
12166             mode->hsync_start > htotal_max ||
12167             mode->hsync_end > htotal_max ||
12168             mode->htotal > htotal_max)
12169                 return MODE_H_ILLEGAL;
12170
12171         if (mode->vdisplay > vdisplay_max ||
12172             mode->vsync_start > vtotal_max ||
12173             mode->vsync_end > vtotal_max ||
12174             mode->vtotal > vtotal_max)
12175                 return MODE_V_ILLEGAL;
12176
12177         if (INTEL_GEN(dev_priv) >= 5) {
12178                 if (mode->hdisplay < 64 ||
12179                     mode->htotal - mode->hdisplay < 32)
12180                         return MODE_H_ILLEGAL;
12181
12182                 if (mode->vtotal - mode->vdisplay < 5)
12183                         return MODE_V_ILLEGAL;
12184         } else {
12185                 if (mode->htotal - mode->hdisplay < 32)
12186                         return MODE_H_ILLEGAL;
12187
12188                 if (mode->vtotal - mode->vdisplay < 3)
12189                         return MODE_V_ILLEGAL;
12190         }
12191
12192         return MODE_OK;
12193 }
12194
12195 enum drm_mode_status
12196 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
12197                                 const struct drm_display_mode *mode,
12198                                 bool bigjoiner)
12199 {
12200         int plane_width_max, plane_height_max;
12201
12202         /*
12203          * intel_mode_valid() should be
12204          * sufficient on older platforms.
12205          */
12206         if (INTEL_GEN(dev_priv) < 9)
12207                 return MODE_OK;
12208
12209         /*
12210          * Most people will probably want a fullscreen
12211          * plane so let's not advertize modes that are
12212          * too big for that.
12213          */
12214         if (INTEL_GEN(dev_priv) >= 11) {
12215                 plane_width_max = 5120 << bigjoiner;
12216                 plane_height_max = 4320;
12217         } else {
12218                 plane_width_max = 5120;
12219                 plane_height_max = 4096;
12220         }
12221
12222         if (mode->hdisplay > plane_width_max)
12223                 return MODE_H_ILLEGAL;
12224
12225         if (mode->vdisplay > plane_height_max)
12226                 return MODE_V_ILLEGAL;
12227
12228         return MODE_OK;
12229 }
12230
12231 static const struct drm_mode_config_funcs intel_mode_funcs = {
12232         .fb_create = intel_user_framebuffer_create,
12233         .get_format_info = intel_get_format_info,
12234         .output_poll_changed = intel_fbdev_output_poll_changed,
12235         .mode_valid = intel_mode_valid,
12236         .atomic_check = intel_atomic_check,
12237         .atomic_commit = intel_atomic_commit,
12238         .atomic_state_alloc = intel_atomic_state_alloc,
12239         .atomic_state_clear = intel_atomic_state_clear,
12240         .atomic_state_free = intel_atomic_state_free,
12241 };
12242
12243 /**
12244  * intel_init_display_hooks - initialize the display modesetting hooks
12245  * @dev_priv: device private
12246  */
12247 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
12248 {
12249         intel_init_cdclk_hooks(dev_priv);
12250         intel_init_audio_hooks(dev_priv);
12251
12252         intel_dpll_init_clock_hook(dev_priv);
12253
12254         if (INTEL_GEN(dev_priv) >= 9) {
12255                 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
12256                 dev_priv->display.crtc_enable = hsw_crtc_enable;
12257                 dev_priv->display.crtc_disable = hsw_crtc_disable;
12258         } else if (HAS_DDI(dev_priv)) {
12259                 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
12260                 dev_priv->display.crtc_enable = hsw_crtc_enable;
12261                 dev_priv->display.crtc_disable = hsw_crtc_disable;
12262         } else if (HAS_PCH_SPLIT(dev_priv)) {
12263                 dev_priv->display.get_pipe_config = ilk_get_pipe_config;
12264                 dev_priv->display.crtc_enable = ilk_crtc_enable;
12265                 dev_priv->display.crtc_disable = ilk_crtc_disable;
12266         } else if (IS_CHERRYVIEW(dev_priv) ||
12267                    IS_VALLEYVIEW(dev_priv)) {
12268                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
12269                 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12270                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12271         } else {
12272                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
12273                 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12274                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12275         }
12276
12277         intel_fdi_init_hook(dev_priv);
12278
12279         if (INTEL_GEN(dev_priv) >= 9) {
12280                 dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
12281                 dev_priv->display.get_initial_plane_config = skl_get_initial_plane_config;
12282         } else {
12283                 dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
12284                 dev_priv->display.get_initial_plane_config = i9xx_get_initial_plane_config;
12285         }
12286
12287 }
12288
12289 void intel_modeset_init_hw(struct drm_i915_private *i915)
12290 {
12291         struct intel_cdclk_state *cdclk_state =
12292                 to_intel_cdclk_state(i915->cdclk.obj.state);
12293
12294         intel_update_cdclk(i915);
12295         intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
12296         cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
12297 }
12298
12299 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
12300 {
12301         struct drm_plane *plane;
12302         struct intel_crtc *crtc;
12303
12304         for_each_intel_crtc(state->dev, crtc) {
12305                 struct intel_crtc_state *crtc_state;
12306
12307                 crtc_state = intel_atomic_get_crtc_state(state, crtc);
12308                 if (IS_ERR(crtc_state))
12309                         return PTR_ERR(crtc_state);
12310
12311                 if (crtc_state->hw.active) {
12312                         /*
12313                          * Preserve the inherited flag to avoid
12314                          * taking the full modeset path.
12315                          */
12316                         crtc_state->inherited = true;
12317                 }
12318         }
12319
12320         drm_for_each_plane(plane, state->dev) {
12321                 struct drm_plane_state *plane_state;
12322
12323                 plane_state = drm_atomic_get_plane_state(state, plane);
12324                 if (IS_ERR(plane_state))
12325                         return PTR_ERR(plane_state);
12326         }
12327
12328         return 0;
12329 }
12330
12331 /*
12332  * Calculate what we think the watermarks should be for the state we've read
12333  * out of the hardware and then immediately program those watermarks so that
12334  * we ensure the hardware settings match our internal state.
12335  *
12336  * We can calculate what we think WM's should be by creating a duplicate of the
12337  * current state (which was constructed during hardware readout) and running it
12338  * through the atomic check code to calculate new watermark values in the
12339  * state object.
12340  */
12341 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
12342 {
12343         struct drm_atomic_state *state;
12344         struct intel_atomic_state *intel_state;
12345         struct intel_crtc *crtc;
12346         struct intel_crtc_state *crtc_state;
12347         struct drm_modeset_acquire_ctx ctx;
12348         int ret;
12349         int i;
12350
12351         /* Only supported on platforms that use atomic watermark design */
12352         if (!dev_priv->display.optimize_watermarks)
12353                 return;
12354
12355         state = drm_atomic_state_alloc(&dev_priv->drm);
12356         if (drm_WARN_ON(&dev_priv->drm, !state))
12357                 return;
12358
12359         intel_state = to_intel_atomic_state(state);
12360
12361         drm_modeset_acquire_init(&ctx, 0);
12362
12363 retry:
12364         state->acquire_ctx = &ctx;
12365
12366         /*
12367          * Hardware readout is the only time we don't want to calculate
12368          * intermediate watermarks (since we don't trust the current
12369          * watermarks).
12370          */
12371         if (!HAS_GMCH(dev_priv))
12372                 intel_state->skip_intermediate_wm = true;
12373
12374         ret = sanitize_watermarks_add_affected(state);
12375         if (ret)
12376                 goto fail;
12377
12378         ret = intel_atomic_check(&dev_priv->drm, state);
12379         if (ret)
12380                 goto fail;
12381
12382         /* Write calculated watermark values back */
12383         for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
12384                 crtc_state->wm.need_postvbl_update = true;
12385                 dev_priv->display.optimize_watermarks(intel_state, crtc);
12386
12387                 to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
12388         }
12389
12390 fail:
12391         if (ret == -EDEADLK) {
12392                 drm_atomic_state_clear(state);
12393                 drm_modeset_backoff(&ctx);
12394                 goto retry;
12395         }
12396
12397         /*
12398          * If we fail here, it means that the hardware appears to be
12399          * programmed in a way that shouldn't be possible, given our
12400          * understanding of watermark requirements.  This might mean a
12401          * mistake in the hardware readout code or a mistake in the
12402          * watermark calculations for a given platform.  Raise a WARN
12403          * so that this is noticeable.
12404          *
12405          * If this actually happens, we'll have to just leave the
12406          * BIOS-programmed watermarks untouched and hope for the best.
12407          */
12408         drm_WARN(&dev_priv->drm, ret,
12409                  "Could not determine valid watermarks for inherited state\n");
12410
12411         drm_atomic_state_put(state);
12412
12413         drm_modeset_drop_locks(&ctx);
12414         drm_modeset_acquire_fini(&ctx);
12415 }
12416
12417 static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
12418 {
12419         if (IS_GEN(dev_priv, 5)) {
12420                 u32 fdi_pll_clk =
12421                         intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
12422
12423                 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
12424         } else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
12425                 dev_priv->fdi_pll_freq = 270000;
12426         } else {
12427                 return;
12428         }
12429
12430         drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
12431 }
12432
12433 static int intel_initial_commit(struct drm_device *dev)
12434 {
12435         struct drm_atomic_state *state = NULL;
12436         struct drm_modeset_acquire_ctx ctx;
12437         struct intel_crtc *crtc;
12438         int ret = 0;
12439
12440         state = drm_atomic_state_alloc(dev);
12441         if (!state)
12442                 return -ENOMEM;
12443
12444         drm_modeset_acquire_init(&ctx, 0);
12445
12446 retry:
12447         state->acquire_ctx = &ctx;
12448
12449         for_each_intel_crtc(dev, crtc) {
12450                 struct intel_crtc_state *crtc_state =
12451                         intel_atomic_get_crtc_state(state, crtc);
12452
12453                 if (IS_ERR(crtc_state)) {
12454                         ret = PTR_ERR(crtc_state);
12455                         goto out;
12456                 }
12457
12458                 if (crtc_state->hw.active) {
12459                         struct intel_encoder *encoder;
12460
12461                         /*
12462                          * We've not yet detected sink capabilities
12463                          * (audio,infoframes,etc.) and thus we don't want to
12464                          * force a full state recomputation yet. We want that to
12465                          * happen only for the first real commit from userspace.
12466                          * So preserve the inherited flag for the time being.
12467                          */
12468                         crtc_state->inherited = true;
12469
12470                         ret = drm_atomic_add_affected_planes(state, &crtc->base);
12471                         if (ret)
12472                                 goto out;
12473
12474                         /*
12475                          * FIXME hack to force a LUT update to avoid the
12476                          * plane update forcing the pipe gamma on without
12477                          * having a proper LUT loaded. Remove once we
12478                          * have readout for pipe gamma enable.
12479                          */
12480                         crtc_state->uapi.color_mgmt_changed = true;
12481
12482                         for_each_intel_encoder_mask(dev, encoder,
12483                                                     crtc_state->uapi.encoder_mask) {
12484                                 if (encoder->initial_fastset_check &&
12485                                     !encoder->initial_fastset_check(encoder, crtc_state)) {
12486                                         ret = drm_atomic_add_affected_connectors(state,
12487                                                                                  &crtc->base);
12488                                         if (ret)
12489                                                 goto out;
12490                                 }
12491                         }
12492                 }
12493         }
12494
12495         ret = drm_atomic_commit(state);
12496
12497 out:
12498         if (ret == -EDEADLK) {
12499                 drm_atomic_state_clear(state);
12500                 drm_modeset_backoff(&ctx);
12501                 goto retry;
12502         }
12503
12504         drm_atomic_state_put(state);
12505
12506         drm_modeset_drop_locks(&ctx);
12507         drm_modeset_acquire_fini(&ctx);
12508
12509         return ret;
12510 }
12511
12512 static void intel_mode_config_init(struct drm_i915_private *i915)
12513 {
12514         struct drm_mode_config *mode_config = &i915->drm.mode_config;
12515
12516         drm_mode_config_init(&i915->drm);
12517         INIT_LIST_HEAD(&i915->global_obj_list);
12518
12519         mode_config->min_width = 0;
12520         mode_config->min_height = 0;
12521
12522         mode_config->preferred_depth = 24;
12523         mode_config->prefer_shadow = 1;
12524
12525         mode_config->allow_fb_modifiers = true;
12526
12527         mode_config->funcs = &intel_mode_funcs;
12528
12529         mode_config->async_page_flip = has_async_flips(i915);
12530
12531         /*
12532          * Maximum framebuffer dimensions, chosen to match
12533          * the maximum render engine surface size on gen4+.
12534          */
12535         if (INTEL_GEN(i915) >= 7) {
12536                 mode_config->max_width = 16384;
12537                 mode_config->max_height = 16384;
12538         } else if (INTEL_GEN(i915) >= 4) {
12539                 mode_config->max_width = 8192;
12540                 mode_config->max_height = 8192;
12541         } else if (IS_GEN(i915, 3)) {
12542                 mode_config->max_width = 4096;
12543                 mode_config->max_height = 4096;
12544         } else {
12545                 mode_config->max_width = 2048;
12546                 mode_config->max_height = 2048;
12547         }
12548
12549         if (IS_I845G(i915) || IS_I865G(i915)) {
12550                 mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
12551                 mode_config->cursor_height = 1023;
12552         } else if (IS_I830(i915) || IS_I85X(i915) ||
12553                    IS_I915G(i915) || IS_I915GM(i915)) {
12554                 mode_config->cursor_width = 64;
12555                 mode_config->cursor_height = 64;
12556         } else {
12557                 mode_config->cursor_width = 256;
12558                 mode_config->cursor_height = 256;
12559         }
12560 }
12561
12562 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
12563 {
12564         intel_atomic_global_obj_cleanup(i915);
12565         drm_mode_config_cleanup(&i915->drm);
12566 }
12567
12568 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
12569 {
12570         if (plane_config->fb) {
12571                 struct drm_framebuffer *fb = &plane_config->fb->base;
12572
12573                 /* We may only have the stub and not a full framebuffer */
12574                 if (drm_framebuffer_read_refcount(fb))
12575                         drm_framebuffer_put(fb);
12576                 else
12577                         kfree(fb);
12578         }
12579
12580         if (plane_config->vma)
12581                 i915_vma_put(plane_config->vma);
12582 }
12583
12584 /* part #1: call before irq install */
12585 int intel_modeset_init_noirq(struct drm_i915_private *i915)
12586 {
12587         int ret;
12588
12589         if (i915_inject_probe_failure(i915))
12590                 return -ENODEV;
12591
12592         if (HAS_DISPLAY(i915)) {
12593                 ret = drm_vblank_init(&i915->drm,
12594                                       INTEL_NUM_PIPES(i915));
12595                 if (ret)
12596                         return ret;
12597         }
12598
12599         intel_bios_init(i915);
12600
12601         ret = intel_vga_register(i915);
12602         if (ret)
12603                 goto cleanup_bios;
12604
12605         /* FIXME: completely on the wrong abstraction layer */
12606         intel_power_domains_init_hw(i915, false);
12607
12608         intel_csr_ucode_init(i915);
12609
12610         i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
12611         i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
12612                                         WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
12613
12614         i915->framestart_delay = 1; /* 1-4 */
12615
12616         intel_mode_config_init(i915);
12617
12618         ret = intel_cdclk_init(i915);
12619         if (ret)
12620                 goto cleanup_vga_client_pw_domain_csr;
12621
12622         ret = intel_dbuf_init(i915);
12623         if (ret)
12624                 goto cleanup_vga_client_pw_domain_csr;
12625
12626         ret = intel_bw_init(i915);
12627         if (ret)
12628                 goto cleanup_vga_client_pw_domain_csr;
12629
12630         init_llist_head(&i915->atomic_helper.free_list);
12631         INIT_WORK(&i915->atomic_helper.free_work,
12632                   intel_atomic_helper_free_state_worker);
12633
12634         intel_init_quirks(i915);
12635
12636         intel_fbc_init(i915);
12637
12638         return 0;
12639
12640 cleanup_vga_client_pw_domain_csr:
12641         intel_csr_ucode_fini(i915);
12642         intel_power_domains_driver_remove(i915);
12643         intel_vga_unregister(i915);
12644 cleanup_bios:
12645         intel_bios_driver_remove(i915);
12646
12647         return ret;
12648 }
12649
12650 /* part #2: call after irq install, but before gem init */
12651 int intel_modeset_init_nogem(struct drm_i915_private *i915)
12652 {
12653         struct drm_device *dev = &i915->drm;
12654         enum pipe pipe;
12655         struct intel_crtc *crtc;
12656         int ret;
12657
12658         intel_init_pm(i915);
12659
12660         intel_panel_sanitize_ssc(i915);
12661
12662         intel_pps_setup(i915);
12663
12664         intel_gmbus_setup(i915);
12665
12666         drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
12667                     INTEL_NUM_PIPES(i915),
12668                     INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
12669
12670         if (HAS_DISPLAY(i915)) {
12671                 for_each_pipe(i915, pipe) {
12672                         ret = intel_crtc_init(i915, pipe);
12673                         if (ret) {
12674                                 intel_mode_config_cleanup(i915);
12675                                 return ret;
12676                         }
12677                 }
12678         }
12679
12680         intel_plane_possible_crtcs_init(i915);
12681         intel_shared_dpll_init(dev);
12682         intel_update_fdi_pll_freq(i915);
12683
12684         intel_update_czclk(i915);
12685         intel_modeset_init_hw(i915);
12686         intel_dpll_update_ref_clks(i915);
12687
12688         intel_hdcp_component_init(i915);
12689
12690         if (i915->max_cdclk_freq == 0)
12691                 intel_update_max_cdclk(i915);
12692
12693         /*
12694          * If the platform has HTI, we need to find out whether it has reserved
12695          * any display resources before we create our display outputs.
12696          */
12697         if (INTEL_INFO(i915)->display.has_hti)
12698                 i915->hti_state = intel_de_read(i915, HDPORT_STATE);
12699
12700         /* Just disable it once at startup */
12701         intel_vga_disable(i915);
12702         intel_setup_outputs(i915);
12703
12704         drm_modeset_lock_all(dev);
12705         intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
12706         drm_modeset_unlock_all(dev);
12707
12708         for_each_intel_crtc(dev, crtc) {
12709                 struct intel_initial_plane_config plane_config = {};
12710
12711                 if (!to_intel_crtc_state(crtc->base.state)->uapi.active)
12712                         continue;
12713
12714                 /*
12715                  * Note that reserving the BIOS fb up front prevents us
12716                  * from stuffing other stolen allocations like the ring
12717                  * on top.  This prevents some ugliness at boot time, and
12718                  * can even allow for smooth boot transitions if the BIOS
12719                  * fb is large enough for the active pipe configuration.
12720                  */
12721                 i915->display.get_initial_plane_config(crtc, &plane_config);
12722
12723                 /*
12724                  * If the fb is shared between multiple heads, we'll
12725                  * just get the first one.
12726                  */
12727                 intel_find_initial_plane_obj(crtc, &plane_config);
12728
12729                 plane_config_fini(&plane_config);
12730         }
12731
12732         /*
12733          * Make sure hardware watermarks really match the state we read out.
12734          * Note that we need to do this after reconstructing the BIOS fb's
12735          * since the watermark calculation done here will use pstate->fb.
12736          */
12737         if (!HAS_GMCH(i915))
12738                 sanitize_watermarks(i915);
12739
12740         return 0;
12741 }
12742
12743 /* part #3: call after gem init */
12744 int intel_modeset_init(struct drm_i915_private *i915)
12745 {
12746         int ret;
12747
12748         if (!HAS_DISPLAY(i915))
12749                 return 0;
12750
12751         /*
12752          * Force all active planes to recompute their states. So that on
12753          * mode_setcrtc after probe, all the intel_plane_state variables
12754          * are already calculated and there is no assert_plane warnings
12755          * during bootup.
12756          */
12757         ret = intel_initial_commit(&i915->drm);
12758         if (ret)
12759                 drm_dbg_kms(&i915->drm, "Initial modeset failed, %d\n", ret);
12760
12761         intel_overlay_setup(i915);
12762
12763         ret = intel_fbdev_init(&i915->drm);
12764         if (ret)
12765                 return ret;
12766
12767         /* Only enable hotplug handling once the fbdev is fully set up. */
12768         intel_hpd_init(i915);
12769         intel_hpd_poll_disable(i915);
12770
12771         intel_init_ipc(i915);
12772
12773         return 0;
12774 }
12775
12776 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
12777 {
12778         struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12779         /* 640x480@60Hz, ~25175 kHz */
12780         struct dpll clock = {
12781                 .m1 = 18,
12782                 .m2 = 7,
12783                 .p1 = 13,
12784                 .p2 = 4,
12785                 .n = 2,
12786         };
12787         u32 dpll, fp;
12788         int i;
12789
12790         drm_WARN_ON(&dev_priv->drm,
12791                     i9xx_calc_dpll_params(48000, &clock) != 25154);
12792
12793         drm_dbg_kms(&dev_priv->drm,
12794                     "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
12795                     pipe_name(pipe), clock.vco, clock.dot);
12796
12797         fp = i9xx_dpll_compute_fp(&clock);
12798         dpll = DPLL_DVO_2X_MODE |
12799                 DPLL_VGA_MODE_DIS |
12800                 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
12801                 PLL_P2_DIVIDE_BY_4 |
12802                 PLL_REF_INPUT_DREFCLK |
12803                 DPLL_VCO_ENABLE;
12804
12805         intel_de_write(dev_priv, FP0(pipe), fp);
12806         intel_de_write(dev_priv, FP1(pipe), fp);
12807
12808         intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
12809         intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
12810         intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
12811         intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
12812         intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
12813         intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
12814         intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
12815
12816         /*
12817          * Apparently we need to have VGA mode enabled prior to changing
12818          * the P1/P2 dividers. Otherwise the DPLL will keep using the old
12819          * dividers, even though the register value does change.
12820          */
12821         intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
12822         intel_de_write(dev_priv, DPLL(pipe), dpll);
12823
12824         /* Wait for the clocks to stabilize. */
12825         intel_de_posting_read(dev_priv, DPLL(pipe));
12826         udelay(150);
12827
12828         /* The pixel multiplier can only be updated once the
12829          * DPLL is enabled and the clocks are stable.
12830          *
12831          * So write it again.
12832          */
12833         intel_de_write(dev_priv, DPLL(pipe), dpll);
12834
12835         /* We do this three times for luck */
12836         for (i = 0; i < 3 ; i++) {
12837                 intel_de_write(dev_priv, DPLL(pipe), dpll);
12838                 intel_de_posting_read(dev_priv, DPLL(pipe));
12839                 udelay(150); /* wait for warmup */
12840         }
12841
12842         intel_de_write(dev_priv, PIPECONF(pipe),
12843                        PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
12844         intel_de_posting_read(dev_priv, PIPECONF(pipe));
12845
12846         intel_wait_for_pipe_scanline_moving(crtc);
12847 }
12848
12849 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
12850 {
12851         struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12852
12853         drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
12854                     pipe_name(pipe));
12855
12856         drm_WARN_ON(&dev_priv->drm,
12857                     intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
12858                     DISPLAY_PLANE_ENABLE);
12859         drm_WARN_ON(&dev_priv->drm,
12860                     intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
12861                     DISPLAY_PLANE_ENABLE);
12862         drm_WARN_ON(&dev_priv->drm,
12863                     intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
12864                     DISPLAY_PLANE_ENABLE);
12865         drm_WARN_ON(&dev_priv->drm,
12866                     intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
12867         drm_WARN_ON(&dev_priv->drm,
12868                     intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
12869
12870         intel_de_write(dev_priv, PIPECONF(pipe), 0);
12871         intel_de_posting_read(dev_priv, PIPECONF(pipe));
12872
12873         intel_wait_for_pipe_scanline_stopped(crtc);
12874
12875         intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
12876         intel_de_posting_read(dev_priv, DPLL(pipe));
12877 }
12878
12879 static void
12880 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
12881 {
12882         struct intel_crtc *crtc;
12883
12884         if (INTEL_GEN(dev_priv) >= 4)
12885                 return;
12886
12887         for_each_intel_crtc(&dev_priv->drm, crtc) {
12888                 struct intel_plane *plane =
12889                         to_intel_plane(crtc->base.primary);
12890                 struct intel_crtc *plane_crtc;
12891                 enum pipe pipe;
12892
12893                 if (!plane->get_hw_state(plane, &pipe))
12894                         continue;
12895
12896                 if (pipe == crtc->pipe)
12897                         continue;
12898
12899                 drm_dbg_kms(&dev_priv->drm,
12900                             "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
12901                             plane->base.base.id, plane->base.name);
12902
12903                 plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12904                 intel_plane_disable_noatomic(plane_crtc, plane);
12905         }
12906 }
12907
12908 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
12909 {
12910         struct drm_device *dev = crtc->base.dev;
12911         struct intel_encoder *encoder;
12912
12913         for_each_encoder_on_crtc(dev, &crtc->base, encoder)
12914                 return true;
12915
12916         return false;
12917 }
12918
12919 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
12920 {
12921         struct drm_device *dev = encoder->base.dev;
12922         struct intel_connector *connector;
12923
12924         for_each_connector_on_encoder(dev, &encoder->base, connector)
12925                 return connector;
12926
12927         return NULL;
12928 }
12929
12930 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
12931                               enum pipe pch_transcoder)
12932 {
12933         return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
12934                 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
12935 }
12936
12937 static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
12938 {
12939         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12940         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12941         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
12942
12943         if (INTEL_GEN(dev_priv) >= 9 ||
12944             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12945                 i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
12946                 u32 val;
12947
12948                 if (transcoder_is_dsi(cpu_transcoder))
12949                         return;
12950
12951                 val = intel_de_read(dev_priv, reg);
12952                 val &= ~HSW_FRAME_START_DELAY_MASK;
12953                 val |= HSW_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12954                 intel_de_write(dev_priv, reg, val);
12955         } else {
12956                 i915_reg_t reg = PIPECONF(cpu_transcoder);
12957                 u32 val;
12958
12959                 val = intel_de_read(dev_priv, reg);
12960                 val &= ~PIPECONF_FRAME_START_DELAY_MASK;
12961                 val |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12962                 intel_de_write(dev_priv, reg, val);
12963         }
12964
12965         if (!crtc_state->has_pch_encoder)
12966                 return;
12967
12968         if (HAS_PCH_IBX(dev_priv)) {
12969                 i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
12970                 u32 val;
12971
12972                 val = intel_de_read(dev_priv, reg);
12973                 val &= ~TRANS_FRAME_START_DELAY_MASK;
12974                 val |= TRANS_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12975                 intel_de_write(dev_priv, reg, val);
12976         } else {
12977                 enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
12978                 i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
12979                 u32 val;
12980
12981                 val = intel_de_read(dev_priv, reg);
12982                 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
12983                 val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12984                 intel_de_write(dev_priv, reg, val);
12985         }
12986 }
12987
12988 static void intel_sanitize_crtc(struct intel_crtc *crtc,
12989                                 struct drm_modeset_acquire_ctx *ctx)
12990 {
12991         struct drm_device *dev = crtc->base.dev;
12992         struct drm_i915_private *dev_priv = to_i915(dev);
12993         struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
12994
12995         if (crtc_state->hw.active) {
12996                 struct intel_plane *plane;
12997
12998                 /* Clear any frame start delays used for debugging left by the BIOS */
12999                 intel_sanitize_frame_start_delay(crtc_state);
13000
13001                 /* Disable everything but the primary plane */
13002                 for_each_intel_plane_on_crtc(dev, crtc, plane) {
13003                         const struct intel_plane_state *plane_state =
13004                                 to_intel_plane_state(plane->base.state);
13005
13006                         if (plane_state->uapi.visible &&
13007                             plane->base.type != DRM_PLANE_TYPE_PRIMARY)
13008                                 intel_plane_disable_noatomic(crtc, plane);
13009                 }
13010
13011                 /*
13012                  * Disable any background color set by the BIOS, but enable the
13013                  * gamma and CSC to match how we program our planes.
13014                  */
13015                 if (INTEL_GEN(dev_priv) >= 9)
13016                         intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
13017                                        SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
13018         }
13019
13020         /* Adjust the state of the output pipe according to whether we
13021          * have active connectors/encoders. */
13022         if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc) &&
13023             !crtc_state->bigjoiner_slave)
13024                 intel_crtc_disable_noatomic(crtc, ctx);
13025
13026         if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
13027                 /*
13028                  * We start out with underrun reporting disabled to avoid races.
13029                  * For correct bookkeeping mark this on active crtcs.
13030                  *
13031                  * Also on gmch platforms we dont have any hardware bits to
13032                  * disable the underrun reporting. Which means we need to start
13033                  * out with underrun reporting disabled also on inactive pipes,
13034                  * since otherwise we'll complain about the garbage we read when
13035                  * e.g. coming up after runtime pm.
13036                  *
13037                  * No protection against concurrent access is required - at
13038                  * worst a fifo underrun happens which also sets this to false.
13039                  */
13040                 crtc->cpu_fifo_underrun_disabled = true;
13041                 /*
13042                  * We track the PCH trancoder underrun reporting state
13043                  * within the crtc. With crtc for pipe A housing the underrun
13044                  * reporting state for PCH transcoder A, crtc for pipe B housing
13045                  * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
13046                  * and marking underrun reporting as disabled for the non-existing
13047                  * PCH transcoders B and C would prevent enabling the south
13048                  * error interrupt (see cpt_can_enable_serr_int()).
13049                  */
13050                 if (has_pch_trancoder(dev_priv, crtc->pipe))
13051                         crtc->pch_fifo_underrun_disabled = true;
13052         }
13053 }
13054
13055 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
13056 {
13057         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
13058
13059         /*
13060          * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
13061          * the hardware when a high res displays plugged in. DPLL P
13062          * divider is zero, and the pipe timings are bonkers. We'll
13063          * try to disable everything in that case.
13064          *
13065          * FIXME would be nice to be able to sanitize this state
13066          * without several WARNs, but for now let's take the easy
13067          * road.
13068          */
13069         return IS_GEN(dev_priv, 6) &&
13070                 crtc_state->hw.active &&
13071                 crtc_state->shared_dpll &&
13072                 crtc_state->port_clock == 0;
13073 }
13074
13075 static void intel_sanitize_encoder(struct intel_encoder *encoder)
13076 {
13077         struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
13078         struct intel_connector *connector;
13079         struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
13080         struct intel_crtc_state *crtc_state = crtc ?
13081                 to_intel_crtc_state(crtc->base.state) : NULL;
13082
13083         /* We need to check both for a crtc link (meaning that the
13084          * encoder is active and trying to read from a pipe) and the
13085          * pipe itself being active. */
13086         bool has_active_crtc = crtc_state &&
13087                 crtc_state->hw.active;
13088
13089         if (crtc_state && has_bogus_dpll_config(crtc_state)) {
13090                 drm_dbg_kms(&dev_priv->drm,
13091                             "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
13092                             pipe_name(crtc->pipe));
13093                 has_active_crtc = false;
13094         }
13095
13096         connector = intel_encoder_find_connector(encoder);
13097         if (connector && !has_active_crtc) {
13098                 drm_dbg_kms(&dev_priv->drm,
13099                             "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13100                             encoder->base.base.id,
13101                             encoder->base.name);
13102
13103                 /* Connector is active, but has no active pipe. This is
13104                  * fallout from our resume register restoring. Disable
13105                  * the encoder manually again. */
13106                 if (crtc_state) {
13107                         struct drm_encoder *best_encoder;
13108
13109                         drm_dbg_kms(&dev_priv->drm,
13110                                     "[ENCODER:%d:%s] manually disabled\n",
13111                                     encoder->base.base.id,
13112                                     encoder->base.name);
13113
13114                         /* avoid oopsing in case the hooks consult best_encoder */
13115                         best_encoder = connector->base.state->best_encoder;
13116                         connector->base.state->best_encoder = &encoder->base;
13117
13118                         /* FIXME NULL atomic state passed! */
13119                         if (encoder->disable)
13120                                 encoder->disable(NULL, encoder, crtc_state,
13121                                                  connector->base.state);
13122                         if (encoder->post_disable)
13123                                 encoder->post_disable(NULL, encoder, crtc_state,
13124                                                       connector->base.state);
13125
13126                         connector->base.state->best_encoder = best_encoder;
13127                 }
13128                 encoder->base.crtc = NULL;
13129
13130                 /* Inconsistent output/port/pipe state happens presumably due to
13131                  * a bug in one of the get_hw_state functions. Or someplace else
13132                  * in our code, like the register restore mess on resume. Clamp
13133                  * things to off as a safer default. */
13134
13135                 connector->base.dpms = DRM_MODE_DPMS_OFF;
13136                 connector->base.encoder = NULL;
13137         }
13138
13139         /* notify opregion of the sanitized encoder state */
13140         intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
13141
13142         if (HAS_DDI(dev_priv))
13143                 intel_ddi_sanitize_encoder_pll_mapping(encoder);
13144 }
13145
13146 /* FIXME read out full plane state for all planes */
13147 static void readout_plane_state(struct drm_i915_private *dev_priv)
13148 {
13149         struct intel_plane *plane;
13150         struct intel_crtc *crtc;
13151
13152         for_each_intel_plane(&dev_priv->drm, plane) {
13153                 struct intel_plane_state *plane_state =
13154                         to_intel_plane_state(plane->base.state);
13155                 struct intel_crtc_state *crtc_state;
13156                 enum pipe pipe = PIPE_A;
13157                 bool visible;
13158
13159                 visible = plane->get_hw_state(plane, &pipe);
13160
13161                 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
13162                 crtc_state = to_intel_crtc_state(crtc->base.state);
13163
13164                 intel_set_plane_visible(crtc_state, plane_state, visible);
13165
13166                 drm_dbg_kms(&dev_priv->drm,
13167                             "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
13168                             plane->base.base.id, plane->base.name,
13169                             enableddisabled(visible), pipe_name(pipe));
13170         }
13171
13172         for_each_intel_crtc(&dev_priv->drm, crtc) {
13173                 struct intel_crtc_state *crtc_state =
13174                         to_intel_crtc_state(crtc->base.state);
13175
13176                 fixup_plane_bitmasks(crtc_state);
13177         }
13178 }
13179
13180 static void intel_modeset_readout_hw_state(struct drm_device *dev)
13181 {
13182         struct drm_i915_private *dev_priv = to_i915(dev);
13183         struct intel_cdclk_state *cdclk_state =
13184                 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
13185         struct intel_dbuf_state *dbuf_state =
13186                 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
13187         enum pipe pipe;
13188         struct intel_crtc *crtc;
13189         struct intel_encoder *encoder;
13190         struct intel_connector *connector;
13191         struct drm_connector_list_iter conn_iter;
13192         u8 active_pipes = 0;
13193
13194         for_each_intel_crtc(dev, crtc) {
13195                 struct intel_crtc_state *crtc_state =
13196                         to_intel_crtc_state(crtc->base.state);
13197
13198                 __drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
13199                 intel_crtc_free_hw_state(crtc_state);
13200                 intel_crtc_state_reset(crtc_state, crtc);
13201
13202                 intel_crtc_get_pipe_config(crtc_state);
13203
13204                 crtc_state->hw.enable = crtc_state->hw.active;
13205
13206                 crtc->base.enabled = crtc_state->hw.enable;
13207                 crtc->active = crtc_state->hw.active;
13208
13209                 if (crtc_state->hw.active)
13210                         active_pipes |= BIT(crtc->pipe);
13211
13212                 drm_dbg_kms(&dev_priv->drm,
13213                             "[CRTC:%d:%s] hw state readout: %s\n",
13214                             crtc->base.base.id, crtc->base.name,
13215                             enableddisabled(crtc_state->hw.active));
13216         }
13217
13218         dev_priv->active_pipes = cdclk_state->active_pipes =
13219                 dbuf_state->active_pipes = active_pipes;
13220
13221         readout_plane_state(dev_priv);
13222
13223         for_each_intel_encoder(dev, encoder) {
13224                 pipe = 0;
13225
13226                 if (encoder->get_hw_state(encoder, &pipe)) {
13227                         struct intel_crtc_state *crtc_state;
13228
13229                         crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
13230                         crtc_state = to_intel_crtc_state(crtc->base.state);
13231
13232                         encoder->base.crtc = &crtc->base;
13233                         intel_encoder_get_config(encoder, crtc_state);
13234                         if (encoder->sync_state)
13235                                 encoder->sync_state(encoder, crtc_state);
13236
13237                         /* read out to slave crtc as well for bigjoiner */
13238                         if (crtc_state->bigjoiner) {
13239                                 /* encoder should read be linked to bigjoiner master */
13240                                 WARN_ON(crtc_state->bigjoiner_slave);
13241
13242                                 crtc = crtc_state->bigjoiner_linked_crtc;
13243                                 crtc_state = to_intel_crtc_state(crtc->base.state);
13244                                 intel_encoder_get_config(encoder, crtc_state);
13245                         }
13246                 } else {
13247                         encoder->base.crtc = NULL;
13248                 }
13249
13250                 drm_dbg_kms(&dev_priv->drm,
13251                             "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
13252                             encoder->base.base.id, encoder->base.name,
13253                             enableddisabled(encoder->base.crtc),
13254                             pipe_name(pipe));
13255         }
13256
13257         intel_dpll_readout_hw_state(dev_priv);
13258
13259         drm_connector_list_iter_begin(dev, &conn_iter);
13260         for_each_intel_connector_iter(connector, &conn_iter) {
13261                 if (connector->get_hw_state(connector)) {
13262                         struct intel_crtc_state *crtc_state;
13263                         struct intel_crtc *crtc;
13264
13265                         connector->base.dpms = DRM_MODE_DPMS_ON;
13266
13267                         encoder = intel_attached_encoder(connector);
13268                         connector->base.encoder = &encoder->base;
13269
13270                         crtc = to_intel_crtc(encoder->base.crtc);
13271                         crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
13272
13273                         if (crtc_state && crtc_state->hw.active) {
13274                                 /*
13275                                  * This has to be done during hardware readout
13276                                  * because anything calling .crtc_disable may
13277                                  * rely on the connector_mask being accurate.
13278                                  */
13279                                 crtc_state->uapi.connector_mask |=
13280                                         drm_connector_mask(&connector->base);
13281                                 crtc_state->uapi.encoder_mask |=
13282                                         drm_encoder_mask(&encoder->base);
13283                         }
13284                 } else {
13285                         connector->base.dpms = DRM_MODE_DPMS_OFF;
13286                         connector->base.encoder = NULL;
13287                 }
13288                 drm_dbg_kms(&dev_priv->drm,
13289                             "[CONNECTOR:%d:%s] hw state readout: %s\n",
13290                             connector->base.base.id, connector->base.name,
13291                             enableddisabled(connector->base.encoder));
13292         }
13293         drm_connector_list_iter_end(&conn_iter);
13294
13295         for_each_intel_crtc(dev, crtc) {
13296                 struct intel_bw_state *bw_state =
13297                         to_intel_bw_state(dev_priv->bw_obj.state);
13298                 struct intel_crtc_state *crtc_state =
13299                         to_intel_crtc_state(crtc->base.state);
13300                 struct intel_plane *plane;
13301                 int min_cdclk = 0;
13302
13303                 if (crtc_state->bigjoiner_slave)
13304                         continue;
13305
13306                 if (crtc_state->hw.active) {
13307                         /*
13308                          * The initial mode needs to be set in order to keep
13309                          * the atomic core happy. It wants a valid mode if the
13310                          * crtc's enabled, so we do the above call.
13311                          *
13312                          * But we don't set all the derived state fully, hence
13313                          * set a flag to indicate that a full recalculation is
13314                          * needed on the next commit.
13315                          */
13316                         crtc_state->inherited = true;
13317
13318                         intel_crtc_update_active_timings(crtc_state);
13319
13320                         intel_crtc_copy_hw_to_uapi_state(crtc_state);
13321                 }
13322
13323                 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
13324                         const struct intel_plane_state *plane_state =
13325                                 to_intel_plane_state(plane->base.state);
13326
13327                         /*
13328                          * FIXME don't have the fb yet, so can't
13329                          * use intel_plane_data_rate() :(
13330                          */
13331                         if (plane_state->uapi.visible)
13332                                 crtc_state->data_rate[plane->id] =
13333                                         4 * crtc_state->pixel_rate;
13334                         /*
13335                          * FIXME don't have the fb yet, so can't
13336                          * use plane->min_cdclk() :(
13337                          */
13338                         if (plane_state->uapi.visible && plane->min_cdclk) {
13339                                 if (crtc_state->double_wide ||
13340                                     INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
13341                                         crtc_state->min_cdclk[plane->id] =
13342                                                 DIV_ROUND_UP(crtc_state->pixel_rate, 2);
13343                                 else
13344                                         crtc_state->min_cdclk[plane->id] =
13345                                                 crtc_state->pixel_rate;
13346                         }
13347                         drm_dbg_kms(&dev_priv->drm,
13348                                     "[PLANE:%d:%s] min_cdclk %d kHz\n",
13349                                     plane->base.base.id, plane->base.name,
13350                                     crtc_state->min_cdclk[plane->id]);
13351                 }
13352
13353                 if (crtc_state->hw.active) {
13354                         min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
13355                         if (drm_WARN_ON(dev, min_cdclk < 0))
13356                                 min_cdclk = 0;
13357                 }
13358
13359                 cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
13360                 cdclk_state->min_voltage_level[crtc->pipe] =
13361                         crtc_state->min_voltage_level;
13362
13363                 intel_bw_crtc_update(bw_state, crtc_state);
13364
13365                 intel_pipe_config_sanity_check(dev_priv, crtc_state);
13366
13367                 /* discard our incomplete slave state, copy it from master */
13368                 if (crtc_state->bigjoiner && crtc_state->hw.active) {
13369                         struct intel_crtc *slave = crtc_state->bigjoiner_linked_crtc;
13370                         struct intel_crtc_state *slave_crtc_state =
13371                                 to_intel_crtc_state(slave->base.state);
13372
13373                         copy_bigjoiner_crtc_state(slave_crtc_state, crtc_state);
13374                         slave->base.mode = crtc->base.mode;
13375
13376                         cdclk_state->min_cdclk[slave->pipe] = min_cdclk;
13377                         cdclk_state->min_voltage_level[slave->pipe] =
13378                                 crtc_state->min_voltage_level;
13379
13380                         for_each_intel_plane_on_crtc(&dev_priv->drm, slave, plane) {
13381                                 const struct intel_plane_state *plane_state =
13382                                         to_intel_plane_state(plane->base.state);
13383
13384                                 /*
13385                                  * FIXME don't have the fb yet, so can't
13386                                  * use intel_plane_data_rate() :(
13387                                  */
13388                                 if (plane_state->uapi.visible)
13389                                         crtc_state->data_rate[plane->id] =
13390                                                 4 * crtc_state->pixel_rate;
13391                                 else
13392                                         crtc_state->data_rate[plane->id] = 0;
13393                         }
13394
13395                         intel_bw_crtc_update(bw_state, slave_crtc_state);
13396                         drm_calc_timestamping_constants(&slave->base,
13397                                                         &slave_crtc_state->hw.adjusted_mode);
13398                 }
13399         }
13400 }
13401
13402 static void
13403 get_encoder_power_domains(struct drm_i915_private *dev_priv)
13404 {
13405         struct intel_encoder *encoder;
13406
13407         for_each_intel_encoder(&dev_priv->drm, encoder) {
13408                 struct intel_crtc_state *crtc_state;
13409
13410                 if (!encoder->get_power_domains)
13411                         continue;
13412
13413                 /*
13414                  * MST-primary and inactive encoders don't have a crtc state
13415                  * and neither of these require any power domain references.
13416                  */
13417                 if (!encoder->base.crtc)
13418                         continue;
13419
13420                 crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
13421                 encoder->get_power_domains(encoder, crtc_state);
13422         }
13423 }
13424
13425 static void intel_early_display_was(struct drm_i915_private *dev_priv)
13426 {
13427         /*
13428          * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
13429          * Also known as Wa_14010480278.
13430          */
13431         if (IS_GEN_RANGE(dev_priv, 10, 12) || IS_GEMINILAKE(dev_priv))
13432                 intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
13433                                intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
13434
13435         if (IS_HASWELL(dev_priv)) {
13436                 /*
13437                  * WaRsPkgCStateDisplayPMReq:hsw
13438                  * System hang if this isn't done before disabling all planes!
13439                  */
13440                 intel_de_write(dev_priv, CHICKEN_PAR1_1,
13441                                intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
13442         }
13443
13444         if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv)) {
13445                 /* Display WA #1142:kbl,cfl,cml */
13446                 intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
13447                              KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
13448                 intel_de_rmw(dev_priv, CHICKEN_MISC_2,
13449                              KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
13450                              KBL_ARB_FILL_SPARE_14);
13451         }
13452 }
13453
13454 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
13455                                        enum port port, i915_reg_t hdmi_reg)
13456 {
13457         u32 val = intel_de_read(dev_priv, hdmi_reg);
13458
13459         if (val & SDVO_ENABLE ||
13460             (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
13461                 return;
13462
13463         drm_dbg_kms(&dev_priv->drm,
13464                     "Sanitizing transcoder select for HDMI %c\n",
13465                     port_name(port));
13466
13467         val &= ~SDVO_PIPE_SEL_MASK;
13468         val |= SDVO_PIPE_SEL(PIPE_A);
13469
13470         intel_de_write(dev_priv, hdmi_reg, val);
13471 }
13472
13473 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
13474                                      enum port port, i915_reg_t dp_reg)
13475 {
13476         u32 val = intel_de_read(dev_priv, dp_reg);
13477
13478         if (val & DP_PORT_EN ||
13479             (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
13480                 return;
13481
13482         drm_dbg_kms(&dev_priv->drm,
13483                     "Sanitizing transcoder select for DP %c\n",
13484                     port_name(port));
13485
13486         val &= ~DP_PIPE_SEL_MASK;
13487         val |= DP_PIPE_SEL(PIPE_A);
13488
13489         intel_de_write(dev_priv, dp_reg, val);
13490 }
13491
13492 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
13493 {
13494         /*
13495          * The BIOS may select transcoder B on some of the PCH
13496          * ports even it doesn't enable the port. This would trip
13497          * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
13498          * Sanitize the transcoder select bits to prevent that. We
13499          * assume that the BIOS never actually enabled the port,
13500          * because if it did we'd actually have to toggle the port
13501          * on and back off to make the transcoder A select stick
13502          * (see. intel_dp_link_down(), intel_disable_hdmi(),
13503          * intel_disable_sdvo()).
13504          */
13505         ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
13506         ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
13507         ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
13508
13509         /* PCH SDVOB multiplex with HDMIB */
13510         ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
13511         ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
13512         ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
13513 }
13514
13515 /* Scan out the current hw modeset state,
13516  * and sanitizes it to the current state
13517  */
13518 static void
13519 intel_modeset_setup_hw_state(struct drm_device *dev,
13520                              struct drm_modeset_acquire_ctx *ctx)
13521 {
13522         struct drm_i915_private *dev_priv = to_i915(dev);
13523         struct intel_encoder *encoder;
13524         struct intel_crtc *crtc;
13525         intel_wakeref_t wakeref;
13526
13527         wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
13528
13529         intel_early_display_was(dev_priv);
13530         intel_modeset_readout_hw_state(dev);
13531
13532         /* HW state is read out, now we need to sanitize this mess. */
13533
13534         /* Sanitize the TypeC port mode upfront, encoders depend on this */
13535         for_each_intel_encoder(dev, encoder) {
13536                 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
13537
13538                 /* We need to sanitize only the MST primary port. */
13539                 if (encoder->type != INTEL_OUTPUT_DP_MST &&
13540                     intel_phy_is_tc(dev_priv, phy))
13541                         intel_tc_port_sanitize(enc_to_dig_port(encoder));
13542         }
13543
13544         get_encoder_power_domains(dev_priv);
13545
13546         if (HAS_PCH_IBX(dev_priv))
13547                 ibx_sanitize_pch_ports(dev_priv);
13548
13549         /*
13550          * intel_sanitize_plane_mapping() may need to do vblank
13551          * waits, so we need vblank interrupts restored beforehand.
13552          */
13553         for_each_intel_crtc(&dev_priv->drm, crtc) {
13554                 struct intel_crtc_state *crtc_state =
13555                         to_intel_crtc_state(crtc->base.state);
13556
13557                 drm_crtc_vblank_reset(&crtc->base);
13558
13559                 if (crtc_state->hw.active)
13560                         intel_crtc_vblank_on(crtc_state);
13561         }
13562
13563         intel_sanitize_plane_mapping(dev_priv);
13564
13565         for_each_intel_encoder(dev, encoder)
13566                 intel_sanitize_encoder(encoder);
13567
13568         for_each_intel_crtc(&dev_priv->drm, crtc) {
13569                 struct intel_crtc_state *crtc_state =
13570                         to_intel_crtc_state(crtc->base.state);
13571
13572                 intel_sanitize_crtc(crtc, ctx);
13573                 intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
13574         }
13575
13576         intel_modeset_update_connector_atomic_state(dev);
13577
13578         intel_dpll_sanitize_state(dev_priv);
13579
13580         if (IS_G4X(dev_priv)) {
13581                 g4x_wm_get_hw_state(dev_priv);
13582                 g4x_wm_sanitize(dev_priv);
13583         } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
13584                 vlv_wm_get_hw_state(dev_priv);
13585                 vlv_wm_sanitize(dev_priv);
13586         } else if (INTEL_GEN(dev_priv) >= 9) {
13587                 skl_wm_get_hw_state(dev_priv);
13588         } else if (HAS_PCH_SPLIT(dev_priv)) {
13589                 ilk_wm_get_hw_state(dev_priv);
13590         }
13591
13592         for_each_intel_crtc(dev, crtc) {
13593                 struct intel_crtc_state *crtc_state =
13594                         to_intel_crtc_state(crtc->base.state);
13595                 u64 put_domains;
13596
13597                 put_domains = modeset_get_crtc_power_domains(crtc_state);
13598                 if (drm_WARN_ON(dev, put_domains))
13599                         modeset_put_crtc_power_domains(crtc, put_domains);
13600         }
13601
13602         intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
13603 }
13604
13605 void intel_display_resume(struct drm_device *dev)
13606 {
13607         struct drm_i915_private *dev_priv = to_i915(dev);
13608         struct drm_atomic_state *state = dev_priv->modeset_restore_state;
13609         struct drm_modeset_acquire_ctx ctx;
13610         int ret;
13611
13612         dev_priv->modeset_restore_state = NULL;
13613         if (state)
13614                 state->acquire_ctx = &ctx;
13615
13616         drm_modeset_acquire_init(&ctx, 0);
13617
13618         while (1) {
13619                 ret = drm_modeset_lock_all_ctx(dev, &ctx);
13620                 if (ret != -EDEADLK)
13621                         break;
13622
13623                 drm_modeset_backoff(&ctx);
13624         }
13625
13626         if (!ret)
13627                 ret = __intel_display_resume(dev, state, &ctx);
13628
13629         intel_enable_ipc(dev_priv);
13630         drm_modeset_drop_locks(&ctx);
13631         drm_modeset_acquire_fini(&ctx);
13632
13633         if (ret)
13634                 drm_err(&dev_priv->drm,
13635                         "Restoring old state failed with %i\n", ret);
13636         if (state)
13637                 drm_atomic_state_put(state);
13638 }
13639
13640 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
13641 {
13642         struct intel_connector *connector;
13643         struct drm_connector_list_iter conn_iter;
13644
13645         /* Kill all the work that may have been queued by hpd. */
13646         drm_connector_list_iter_begin(&i915->drm, &conn_iter);
13647         for_each_intel_connector_iter(connector, &conn_iter) {
13648                 if (connector->modeset_retry_work.func)
13649                         cancel_work_sync(&connector->modeset_retry_work);
13650                 if (connector->hdcp.shim) {
13651                         cancel_delayed_work_sync(&connector->hdcp.check_work);
13652                         cancel_work_sync(&connector->hdcp.prop_work);
13653                 }
13654         }
13655         drm_connector_list_iter_end(&conn_iter);
13656 }
13657
13658 /* part #1: call before irq uninstall */
13659 void intel_modeset_driver_remove(struct drm_i915_private *i915)
13660 {
13661         flush_workqueue(i915->flip_wq);
13662         flush_workqueue(i915->modeset_wq);
13663
13664         flush_work(&i915->atomic_helper.free_work);
13665         drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
13666 }
13667
13668 /* part #2: call after irq uninstall */
13669 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
13670 {
13671         /*
13672          * Due to the hpd irq storm handling the hotplug work can re-arm the
13673          * poll handlers. Hence disable polling after hpd handling is shut down.
13674          */
13675         intel_hpd_poll_fini(i915);
13676
13677         /*
13678          * MST topology needs to be suspended so we don't have any calls to
13679          * fbdev after it's finalized. MST will be destroyed later as part of
13680          * drm_mode_config_cleanup()
13681          */
13682         intel_dp_mst_suspend(i915);
13683
13684         /* poll work can call into fbdev, hence clean that up afterwards */
13685         intel_fbdev_fini(i915);
13686
13687         intel_unregister_dsm_handler();
13688
13689         intel_fbc_global_disable(i915);
13690
13691         /* flush any delayed tasks or pending work */
13692         flush_scheduled_work();
13693
13694         intel_hdcp_component_fini(i915);
13695
13696         intel_mode_config_cleanup(i915);
13697
13698         intel_overlay_cleanup(i915);
13699
13700         intel_gmbus_teardown(i915);
13701
13702         destroy_workqueue(i915->flip_wq);
13703         destroy_workqueue(i915->modeset_wq);
13704
13705         intel_fbc_cleanup_cfb(i915);
13706 }
13707
13708 /* part #3: call after gem init */
13709 void intel_modeset_driver_remove_nogem(struct drm_i915_private *i915)
13710 {
13711         intel_csr_ucode_fini(i915);
13712
13713         intel_power_domains_driver_remove(i915);
13714
13715         intel_vga_unregister(i915);
13716
13717         intel_bios_driver_remove(i915);
13718 }
13719
13720 void intel_display_driver_register(struct drm_i915_private *i915)
13721 {
13722         if (!HAS_DISPLAY(i915))
13723                 return;
13724
13725         intel_display_debugfs_register(i915);
13726
13727         /* Must be done after probing outputs */
13728         intel_opregion_register(i915);
13729         acpi_video_register();
13730
13731         intel_audio_init(i915);
13732
13733         /*
13734          * Some ports require correctly set-up hpd registers for
13735          * detection to work properly (leading to ghost connected
13736          * connector status), e.g. VGA on gm45.  Hence we can only set
13737          * up the initial fbdev config after hpd irqs are fully
13738          * enabled. We do it last so that the async config cannot run
13739          * before the connectors are registered.
13740          */
13741         intel_fbdev_initial_config_async(&i915->drm);
13742
13743         /*
13744          * We need to coordinate the hotplugs with the asynchronous
13745          * fbdev configuration, for which we use the
13746          * fbdev->async_cookie.
13747          */
13748         drm_kms_helper_poll_init(&i915->drm);
13749 }
13750
13751 void intel_display_driver_unregister(struct drm_i915_private *i915)
13752 {
13753         if (!HAS_DISPLAY(i915))
13754                 return;
13755
13756         intel_fbdev_unregister(i915);
13757         intel_audio_deinit(i915);
13758
13759         /*
13760          * After flushing the fbdev (incl. a late async config which
13761          * will have delayed queuing of a hotplug event), then flush
13762          * the hotplug events.
13763          */
13764         drm_kms_helper_poll_fini(&i915->drm);
13765         drm_atomic_helper_shutdown(&i915->drm);
13766
13767         acpi_video_unregister();
13768         intel_opregion_unregister(i915);
13769 }
13770
13771 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
13772
13773 struct intel_display_error_state {
13774
13775         u32 power_well_driver;
13776
13777         struct intel_cursor_error_state {
13778                 u32 control;
13779                 u32 position;
13780                 u32 base;
13781                 u32 size;
13782         } cursor[I915_MAX_PIPES];
13783
13784         struct intel_pipe_error_state {
13785                 bool power_domain_on;
13786                 u32 source;
13787                 u32 stat;
13788         } pipe[I915_MAX_PIPES];
13789
13790         struct intel_plane_error_state {
13791                 u32 control;
13792                 u32 stride;
13793                 u32 size;
13794                 u32 pos;
13795                 u32 addr;
13796                 u32 surface;
13797                 u32 tile_offset;
13798         } plane[I915_MAX_PIPES];
13799
13800         struct intel_transcoder_error_state {
13801                 bool available;
13802                 bool power_domain_on;
13803                 enum transcoder cpu_transcoder;
13804
13805                 u32 conf;
13806
13807                 u32 htotal;
13808                 u32 hblank;
13809                 u32 hsync;
13810                 u32 vtotal;
13811                 u32 vblank;
13812                 u32 vsync;
13813         } transcoder[5];
13814 };
13815
13816 struct intel_display_error_state *
13817 intel_display_capture_error_state(struct drm_i915_private *dev_priv)
13818 {
13819         struct intel_display_error_state *error;
13820         int transcoders[] = {
13821                 TRANSCODER_A,
13822                 TRANSCODER_B,
13823                 TRANSCODER_C,
13824                 TRANSCODER_D,
13825                 TRANSCODER_EDP,
13826         };
13827         int i;
13828
13829         BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
13830
13831         if (!HAS_DISPLAY(dev_priv))
13832                 return NULL;
13833
13834         error = kzalloc(sizeof(*error), GFP_ATOMIC);
13835         if (error == NULL)
13836                 return NULL;
13837
13838         if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
13839                 error->power_well_driver = intel_de_read(dev_priv,
13840                                                          HSW_PWR_WELL_CTL2);
13841
13842         for_each_pipe(dev_priv, i) {
13843                 error->pipe[i].power_domain_on =
13844                         __intel_display_power_is_enabled(dev_priv,
13845                                                          POWER_DOMAIN_PIPE(i));
13846                 if (!error->pipe[i].power_domain_on)
13847                         continue;
13848
13849                 error->cursor[i].control = intel_de_read(dev_priv, CURCNTR(i));
13850                 error->cursor[i].position = intel_de_read(dev_priv, CURPOS(i));
13851                 error->cursor[i].base = intel_de_read(dev_priv, CURBASE(i));
13852
13853                 error->plane[i].control = intel_de_read(dev_priv, DSPCNTR(i));
13854                 error->plane[i].stride = intel_de_read(dev_priv, DSPSTRIDE(i));
13855                 if (INTEL_GEN(dev_priv) <= 3) {
13856                         error->plane[i].size = intel_de_read(dev_priv,
13857                                                              DSPSIZE(i));
13858                         error->plane[i].pos = intel_de_read(dev_priv,
13859                                                             DSPPOS(i));
13860                 }
13861                 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
13862                         error->plane[i].addr = intel_de_read(dev_priv,
13863                                                              DSPADDR(i));
13864                 if (INTEL_GEN(dev_priv) >= 4) {
13865                         error->plane[i].surface = intel_de_read(dev_priv,
13866                                                                 DSPSURF(i));
13867                         error->plane[i].tile_offset = intel_de_read(dev_priv,
13868                                                                     DSPTILEOFF(i));
13869                 }
13870
13871                 error->pipe[i].source = intel_de_read(dev_priv, PIPESRC(i));
13872
13873                 if (HAS_GMCH(dev_priv))
13874                         error->pipe[i].stat = intel_de_read(dev_priv,
13875                                                             PIPESTAT(i));
13876         }
13877
13878         for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
13879                 enum transcoder cpu_transcoder = transcoders[i];
13880
13881                 if (!HAS_TRANSCODER(dev_priv, cpu_transcoder))
13882                         continue;
13883
13884                 error->transcoder[i].available = true;
13885                 error->transcoder[i].power_domain_on =
13886                         __intel_display_power_is_enabled(dev_priv,
13887                                 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
13888                 if (!error->transcoder[i].power_domain_on)
13889                         continue;
13890
13891                 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13892
13893                 error->transcoder[i].conf = intel_de_read(dev_priv,
13894                                                           PIPECONF(cpu_transcoder));
13895                 error->transcoder[i].htotal = intel_de_read(dev_priv,
13896                                                             HTOTAL(cpu_transcoder));
13897                 error->transcoder[i].hblank = intel_de_read(dev_priv,
13898                                                             HBLANK(cpu_transcoder));
13899                 error->transcoder[i].hsync = intel_de_read(dev_priv,
13900                                                            HSYNC(cpu_transcoder));
13901                 error->transcoder[i].vtotal = intel_de_read(dev_priv,
13902                                                             VTOTAL(cpu_transcoder));
13903                 error->transcoder[i].vblank = intel_de_read(dev_priv,
13904                                                             VBLANK(cpu_transcoder));
13905                 error->transcoder[i].vsync = intel_de_read(dev_priv,
13906                                                            VSYNC(cpu_transcoder));
13907         }
13908
13909         return error;
13910 }
13911
13912 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13913
13914 void
13915 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
13916                                 struct intel_display_error_state *error)
13917 {
13918         struct drm_i915_private *dev_priv = m->i915;
13919         int i;
13920
13921         if (!error)
13922                 return;
13923
13924         err_printf(m, "Num Pipes: %d\n", INTEL_NUM_PIPES(dev_priv));
13925         if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
13926                 err_printf(m, "PWR_WELL_CTL2: %08x\n",
13927                            error->power_well_driver);
13928         for_each_pipe(dev_priv, i) {
13929                 err_printf(m, "Pipe [%d]:\n", i);
13930                 err_printf(m, "  Power: %s\n",
13931                            onoff(error->pipe[i].power_domain_on));
13932                 err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
13933                 err_printf(m, "  STAT: %08x\n", error->pipe[i].stat);
13934
13935                 err_printf(m, "Plane [%d]:\n", i);
13936                 err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
13937                 err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
13938                 if (INTEL_GEN(dev_priv) <= 3) {
13939                         err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
13940                         err_printf(m, "  POS: %08x\n", error->plane[i].pos);
13941                 }
13942                 if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
13943                         err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
13944                 if (INTEL_GEN(dev_priv) >= 4) {
13945                         err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
13946                         err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
13947                 }
13948
13949                 err_printf(m, "Cursor [%d]:\n", i);
13950                 err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
13951                 err_printf(m, "  POS: %08x\n", error->cursor[i].position);
13952                 err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
13953         }
13954
13955         for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
13956                 if (!error->transcoder[i].available)
13957                         continue;
13958
13959                 err_printf(m, "CPU transcoder: %s\n",
13960                            transcoder_name(error->transcoder[i].cpu_transcoder));
13961                 err_printf(m, "  Power: %s\n",
13962                            onoff(error->transcoder[i].power_domain_on));
13963                 err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
13964                 err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
13965                 err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
13966                 err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
13967                 err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
13968                 err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
13969                 err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
13970         }
13971 }
13972
13973 #endif