Merge tag 'csky-for-linus-5.14-rc1' of git://github.com/c-sky/csky-linux
[linux-2.6-microblaze.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_fb.h"
58 #include "display/intel_gmbus.h"
59 #include "display/intel_hdmi.h"
60 #include "display/intel_lvds.h"
61 #include "display/intel_sdvo.h"
62 #include "display/intel_tv.h"
63 #include "display/intel_vdsc.h"
64 #include "display/intel_vrr.h"
65
66 #include "gem/i915_gem_lmem.h"
67 #include "gem/i915_gem_object.h"
68
69 #include "gt/intel_rps.h"
70 #include "gt/gen8_ppgtt.h"
71
72 #include "g4x_dp.h"
73 #include "g4x_hdmi.h"
74 #include "i915_drv.h"
75 #include "intel_acpi.h"
76 #include "intel_atomic.h"
77 #include "intel_atomic_plane.h"
78 #include "intel_bw.h"
79 #include "intel_cdclk.h"
80 #include "intel_color.h"
81 #include "intel_crtc.h"
82 #include "intel_de.h"
83 #include "intel_display_types.h"
84 #include "intel_dmc.h"
85 #include "intel_dp_link_training.h"
86 #include "intel_fbc.h"
87 #include "intel_fdi.h"
88 #include "intel_fbdev.h"
89 #include "intel_fifo_underrun.h"
90 #include "intel_frontbuffer.h"
91 #include "intel_hdcp.h"
92 #include "intel_hotplug.h"
93 #include "intel_overlay.h"
94 #include "intel_pipe_crc.h"
95 #include "intel_pm.h"
96 #include "intel_pps.h"
97 #include "intel_psr.h"
98 #include "intel_quirks.h"
99 #include "intel_sideband.h"
100 #include "intel_sprite.h"
101 #include "intel_tc.h"
102 #include "intel_vga.h"
103 #include "i9xx_plane.h"
104 #include "skl_scaler.h"
105 #include "skl_universal_plane.h"
106
107 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
108                                 struct intel_crtc_state *pipe_config);
109 static void ilk_pch_clock_get(struct intel_crtc *crtc,
110                               struct intel_crtc_state *pipe_config);
111
112 static int intel_framebuffer_init(struct intel_framebuffer *ifb,
113                                   struct drm_i915_gem_object *obj,
114                                   struct drm_mode_fb_cmd2 *mode_cmd);
115 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state);
116 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
117 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
118                                          const struct intel_link_m_n *m_n,
119                                          const struct intel_link_m_n *m2_n2);
120 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
121 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
122 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state);
123 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
124 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
125 static void intel_modeset_setup_hw_state(struct drm_device *dev,
126                                          struct drm_modeset_acquire_ctx *ctx);
127
128 struct i915_dpt {
129         struct i915_address_space vm;
130
131         struct drm_i915_gem_object *obj;
132         struct i915_vma *vma;
133         void __iomem *iomem;
134 };
135
136 #define i915_is_dpt(vm) ((vm)->is_dpt)
137
138 static inline struct i915_dpt *
139 i915_vm_to_dpt(struct i915_address_space *vm)
140 {
141         BUILD_BUG_ON(offsetof(struct i915_dpt, vm));
142         GEM_BUG_ON(!i915_is_dpt(vm));
143         return container_of(vm, struct i915_dpt, vm);
144 }
145
146 #define dpt_total_entries(dpt) ((dpt)->vm.total >> PAGE_SHIFT)
147
148 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
149 {
150         writeq(pte, addr);
151 }
152
153 static void dpt_insert_page(struct i915_address_space *vm,
154                             dma_addr_t addr,
155                             u64 offset,
156                             enum i915_cache_level level,
157                             u32 flags)
158 {
159         struct i915_dpt *dpt = i915_vm_to_dpt(vm);
160         gen8_pte_t __iomem *base = dpt->iomem;
161
162         gen8_set_pte(base + offset / I915_GTT_PAGE_SIZE,
163                      vm->pte_encode(addr, level, flags));
164 }
165
166 static void dpt_insert_entries(struct i915_address_space *vm,
167                                struct i915_vma *vma,
168                                enum i915_cache_level level,
169                                u32 flags)
170 {
171         struct i915_dpt *dpt = i915_vm_to_dpt(vm);
172         gen8_pte_t __iomem *base = dpt->iomem;
173         const gen8_pte_t pte_encode = vm->pte_encode(0, level, flags);
174         struct sgt_iter sgt_iter;
175         dma_addr_t addr;
176         int i;
177
178         /*
179          * Note that we ignore PTE_READ_ONLY here. The caller must be careful
180          * not to allow the user to override access to a read only page.
181          */
182
183         i = vma->node.start / I915_GTT_PAGE_SIZE;
184         for_each_sgt_daddr(addr, sgt_iter, vma->pages)
185                 gen8_set_pte(&base[i++], pte_encode | addr);
186 }
187
188 static void dpt_clear_range(struct i915_address_space *vm,
189                             u64 start, u64 length)
190 {
191 }
192
193 static void dpt_bind_vma(struct i915_address_space *vm,
194                          struct i915_vm_pt_stash *stash,
195                          struct i915_vma *vma,
196                          enum i915_cache_level cache_level,
197                          u32 flags)
198 {
199         struct drm_i915_gem_object *obj = vma->obj;
200         u32 pte_flags;
201
202         /* Applicable to VLV (gen8+ do not support RO in the GGTT) */
203         pte_flags = 0;
204         if (vma->vm->has_read_only && i915_gem_object_is_readonly(obj))
205                 pte_flags |= PTE_READ_ONLY;
206         if (i915_gem_object_is_lmem(obj))
207                 pte_flags |= PTE_LM;
208
209         vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
210
211         vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
212
213         /*
214          * Without aliasing PPGTT there's no difference between
215          * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
216          * upgrade to both bound if we bind either to avoid double-binding.
217          */
218         atomic_or(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND, &vma->flags);
219 }
220
221 static void dpt_unbind_vma(struct i915_address_space *vm, struct i915_vma *vma)
222 {
223         vm->clear_range(vm, vma->node.start, vma->size);
224 }
225
226 static void dpt_cleanup(struct i915_address_space *vm)
227 {
228         struct i915_dpt *dpt = i915_vm_to_dpt(vm);
229
230         i915_gem_object_put(dpt->obj);
231 }
232
233 static struct i915_address_space *
234 intel_dpt_create(struct intel_framebuffer *fb)
235 {
236         struct drm_gem_object *obj = &intel_fb_obj(&fb->base)->base;
237         struct drm_i915_private *i915 = to_i915(obj->dev);
238         struct drm_i915_gem_object *dpt_obj;
239         struct i915_address_space *vm;
240         struct i915_dpt *dpt;
241         size_t size;
242         int ret;
243
244         if (intel_fb_needs_pot_stride_remap(fb))
245                 size = intel_remapped_info_size(&fb->remapped_view.gtt.remapped);
246         else
247                 size = DIV_ROUND_UP_ULL(obj->size, I915_GTT_PAGE_SIZE);
248
249         size = round_up(size * sizeof(gen8_pte_t), I915_GTT_PAGE_SIZE);
250
251         if (HAS_LMEM(i915))
252                 dpt_obj = i915_gem_object_create_lmem(i915, size, 0);
253         else
254                 dpt_obj = i915_gem_object_create_stolen(i915, size);
255         if (IS_ERR(dpt_obj))
256                 return ERR_CAST(dpt_obj);
257
258         ret = i915_gem_object_set_cache_level(dpt_obj, I915_CACHE_NONE);
259         if (ret) {
260                 i915_gem_object_put(dpt_obj);
261                 return ERR_PTR(ret);
262         }
263
264         dpt = kzalloc(sizeof(*dpt), GFP_KERNEL);
265         if (!dpt) {
266                 i915_gem_object_put(dpt_obj);
267                 return ERR_PTR(-ENOMEM);
268         }
269
270         vm = &dpt->vm;
271
272         vm->gt = &i915->gt;
273         vm->i915 = i915;
274         vm->dma = i915->drm.dev;
275         vm->total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
276         vm->is_dpt = true;
277
278         i915_address_space_init(vm, VM_CLASS_DPT);
279
280         vm->insert_page = dpt_insert_page;
281         vm->clear_range = dpt_clear_range;
282         vm->insert_entries = dpt_insert_entries;
283         vm->cleanup = dpt_cleanup;
284
285         vm->vma_ops.bind_vma    = dpt_bind_vma;
286         vm->vma_ops.unbind_vma  = dpt_unbind_vma;
287         vm->vma_ops.set_pages   = ggtt_set_pages;
288         vm->vma_ops.clear_pages = clear_pages;
289
290         vm->pte_encode = gen8_ggtt_pte_encode;
291
292         dpt->obj = dpt_obj;
293
294         return &dpt->vm;
295 }
296
297 static void intel_dpt_destroy(struct i915_address_space *vm)
298 {
299         struct i915_dpt *dpt = i915_vm_to_dpt(vm);
300
301         i915_vm_close(&dpt->vm);
302 }
303
304 /* returns HPLL frequency in kHz */
305 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
306 {
307         int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
308
309         /* Obtain SKU information */
310         hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
311                 CCK_FUSE_HPLL_FREQ_MASK;
312
313         return vco_freq[hpll_freq] * 1000;
314 }
315
316 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
317                       const char *name, u32 reg, int ref_freq)
318 {
319         u32 val;
320         int divider;
321
322         val = vlv_cck_read(dev_priv, reg);
323         divider = val & CCK_FREQUENCY_VALUES;
324
325         drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
326                  (divider << CCK_FREQUENCY_STATUS_SHIFT),
327                  "%s change in progress\n", name);
328
329         return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
330 }
331
332 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
333                            const char *name, u32 reg)
334 {
335         int hpll;
336
337         vlv_cck_get(dev_priv);
338
339         if (dev_priv->hpll_freq == 0)
340                 dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
341
342         hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
343
344         vlv_cck_put(dev_priv);
345
346         return hpll;
347 }
348
349 static void intel_update_czclk(struct drm_i915_private *dev_priv)
350 {
351         if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
352                 return;
353
354         dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
355                                                       CCK_CZ_CLOCK_CONTROL);
356
357         drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
358                 dev_priv->czclk_freq);
359 }
360
361 /* WA Display #0827: Gen9:all */
362 static void
363 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
364 {
365         if (enable)
366                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
367                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DUPS1_GATING_DIS | DUPS2_GATING_DIS);
368         else
369                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
370                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
371 }
372
373 /* Wa_2006604312:icl,ehl */
374 static void
375 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
376                        bool enable)
377 {
378         if (enable)
379                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
380                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
381         else
382                 intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
383                                intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
384 }
385
386 static bool
387 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
388 {
389         return crtc_state->master_transcoder != INVALID_TRANSCODER;
390 }
391
392 static bool
393 is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
394 {
395         return crtc_state->sync_mode_slaves_mask != 0;
396 }
397
398 bool
399 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
400 {
401         return is_trans_port_sync_master(crtc_state) ||
402                 is_trans_port_sync_slave(crtc_state);
403 }
404
405 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
406                                     enum pipe pipe)
407 {
408         i915_reg_t reg = PIPEDSL(pipe);
409         u32 line1, line2;
410         u32 line_mask;
411
412         if (DISPLAY_VER(dev_priv) == 2)
413                 line_mask = DSL_LINEMASK_GEN2;
414         else
415                 line_mask = DSL_LINEMASK_GEN3;
416
417         line1 = intel_de_read(dev_priv, reg) & line_mask;
418         msleep(5);
419         line2 = intel_de_read(dev_priv, reg) & line_mask;
420
421         return line1 != line2;
422 }
423
424 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
425 {
426         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
427         enum pipe pipe = crtc->pipe;
428
429         /* Wait for the display line to settle/start moving */
430         if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
431                 drm_err(&dev_priv->drm,
432                         "pipe %c scanline %s wait timed out\n",
433                         pipe_name(pipe), onoff(state));
434 }
435
436 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
437 {
438         wait_for_pipe_scanline_moving(crtc, false);
439 }
440
441 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
442 {
443         wait_for_pipe_scanline_moving(crtc, true);
444 }
445
446 static void
447 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
448 {
449         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
450         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
451
452         if (DISPLAY_VER(dev_priv) >= 4) {
453                 enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
454                 i915_reg_t reg = PIPECONF(cpu_transcoder);
455
456                 /* Wait for the Pipe State to go off */
457                 if (intel_de_wait_for_clear(dev_priv, reg,
458                                             I965_PIPECONF_ACTIVE, 100))
459                         drm_WARN(&dev_priv->drm, 1,
460                                  "pipe_off wait timed out\n");
461         } else {
462                 intel_wait_for_pipe_scanline_stopped(crtc);
463         }
464 }
465
466 /* Only for pre-ILK configs */
467 void assert_pll(struct drm_i915_private *dev_priv,
468                 enum pipe pipe, bool state)
469 {
470         u32 val;
471         bool cur_state;
472
473         val = intel_de_read(dev_priv, DPLL(pipe));
474         cur_state = !!(val & DPLL_VCO_ENABLE);
475         I915_STATE_WARN(cur_state != state,
476              "PLL state assertion failure (expected %s, current %s)\n",
477                         onoff(state), onoff(cur_state));
478 }
479
480 /* XXX: the dsi pll is shared between MIPI DSI ports */
481 void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
482 {
483         u32 val;
484         bool cur_state;
485
486         vlv_cck_get(dev_priv);
487         val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
488         vlv_cck_put(dev_priv);
489
490         cur_state = val & DSI_PLL_VCO_EN;
491         I915_STATE_WARN(cur_state != state,
492              "DSI PLL state assertion failure (expected %s, current %s)\n",
493                         onoff(state), onoff(cur_state));
494 }
495
496 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
497                           enum pipe pipe, bool state)
498 {
499         bool cur_state;
500
501         if (HAS_DDI(dev_priv)) {
502                 /*
503                  * DDI does not have a specific FDI_TX register.
504                  *
505                  * FDI is never fed from EDP transcoder
506                  * so pipe->transcoder cast is fine here.
507                  */
508                 enum transcoder cpu_transcoder = (enum transcoder)pipe;
509                 u32 val = intel_de_read(dev_priv,
510                                         TRANS_DDI_FUNC_CTL(cpu_transcoder));
511                 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
512         } else {
513                 u32 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
514                 cur_state = !!(val & FDI_TX_ENABLE);
515         }
516         I915_STATE_WARN(cur_state != state,
517              "FDI TX state assertion failure (expected %s, current %s)\n",
518                         onoff(state), onoff(cur_state));
519 }
520 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
521 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
522
523 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
524                           enum pipe pipe, bool state)
525 {
526         u32 val;
527         bool cur_state;
528
529         val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
530         cur_state = !!(val & FDI_RX_ENABLE);
531         I915_STATE_WARN(cur_state != state,
532              "FDI RX state assertion failure (expected %s, current %s)\n",
533                         onoff(state), onoff(cur_state));
534 }
535 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
536 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
537
538 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
539                                       enum pipe pipe)
540 {
541         u32 val;
542
543         /* ILK FDI PLL is always enabled */
544         if (IS_IRONLAKE(dev_priv))
545                 return;
546
547         /* On Haswell, DDI ports are responsible for the FDI PLL setup */
548         if (HAS_DDI(dev_priv))
549                 return;
550
551         val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
552         I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
553 }
554
555 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
556                        enum pipe pipe, bool state)
557 {
558         u32 val;
559         bool cur_state;
560
561         val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
562         cur_state = !!(val & FDI_RX_PLL_ENABLE);
563         I915_STATE_WARN(cur_state != state,
564              "FDI RX PLL assertion failure (expected %s, current %s)\n",
565                         onoff(state), onoff(cur_state));
566 }
567
568 void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
569 {
570         i915_reg_t pp_reg;
571         u32 val;
572         enum pipe panel_pipe = INVALID_PIPE;
573         bool locked = true;
574
575         if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
576                 return;
577
578         if (HAS_PCH_SPLIT(dev_priv)) {
579                 u32 port_sel;
580
581                 pp_reg = PP_CONTROL(0);
582                 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
583
584                 switch (port_sel) {
585                 case PANEL_PORT_SELECT_LVDS:
586                         intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
587                         break;
588                 case PANEL_PORT_SELECT_DPA:
589                         g4x_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
590                         break;
591                 case PANEL_PORT_SELECT_DPC:
592                         g4x_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
593                         break;
594                 case PANEL_PORT_SELECT_DPD:
595                         g4x_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
596                         break;
597                 default:
598                         MISSING_CASE(port_sel);
599                         break;
600                 }
601         } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
602                 /* presumably write lock depends on pipe, not port select */
603                 pp_reg = PP_CONTROL(pipe);
604                 panel_pipe = pipe;
605         } else {
606                 u32 port_sel;
607
608                 pp_reg = PP_CONTROL(0);
609                 port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
610
611                 drm_WARN_ON(&dev_priv->drm,
612                             port_sel != PANEL_PORT_SELECT_LVDS);
613                 intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
614         }
615
616         val = intel_de_read(dev_priv, pp_reg);
617         if (!(val & PANEL_POWER_ON) ||
618             ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
619                 locked = false;
620
621         I915_STATE_WARN(panel_pipe == pipe && locked,
622              "panel assertion failure, pipe %c regs locked\n",
623              pipe_name(pipe));
624 }
625
626 void assert_pipe(struct drm_i915_private *dev_priv,
627                  enum transcoder cpu_transcoder, bool state)
628 {
629         bool cur_state;
630         enum intel_display_power_domain power_domain;
631         intel_wakeref_t wakeref;
632
633         /* we keep both pipes enabled on 830 */
634         if (IS_I830(dev_priv))
635                 state = true;
636
637         power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
638         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
639         if (wakeref) {
640                 u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
641                 cur_state = !!(val & PIPECONF_ENABLE);
642
643                 intel_display_power_put(dev_priv, power_domain, wakeref);
644         } else {
645                 cur_state = false;
646         }
647
648         I915_STATE_WARN(cur_state != state,
649                         "transcoder %s assertion failure (expected %s, current %s)\n",
650                         transcoder_name(cpu_transcoder),
651                         onoff(state), onoff(cur_state));
652 }
653
654 static void assert_plane(struct intel_plane *plane, bool state)
655 {
656         enum pipe pipe;
657         bool cur_state;
658
659         cur_state = plane->get_hw_state(plane, &pipe);
660
661         I915_STATE_WARN(cur_state != state,
662                         "%s assertion failure (expected %s, current %s)\n",
663                         plane->base.name, onoff(state), onoff(cur_state));
664 }
665
666 #define assert_plane_enabled(p) assert_plane(p, true)
667 #define assert_plane_disabled(p) assert_plane(p, false)
668
669 static void assert_planes_disabled(struct intel_crtc *crtc)
670 {
671         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
672         struct intel_plane *plane;
673
674         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
675                 assert_plane_disabled(plane);
676 }
677
678 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
679                                     enum pipe pipe)
680 {
681         u32 val;
682         bool enabled;
683
684         val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
685         enabled = !!(val & TRANS_ENABLE);
686         I915_STATE_WARN(enabled,
687              "transcoder assertion failed, should be off on pipe %c but is still active\n",
688              pipe_name(pipe));
689 }
690
691 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
692                                    enum pipe pipe, enum port port,
693                                    i915_reg_t dp_reg)
694 {
695         enum pipe port_pipe;
696         bool state;
697
698         state = g4x_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
699
700         I915_STATE_WARN(state && port_pipe == pipe,
701                         "PCH DP %c enabled on transcoder %c, should be disabled\n",
702                         port_name(port), pipe_name(pipe));
703
704         I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
705                         "IBX PCH DP %c still using transcoder B\n",
706                         port_name(port));
707 }
708
709 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
710                                      enum pipe pipe, enum port port,
711                                      i915_reg_t hdmi_reg)
712 {
713         enum pipe port_pipe;
714         bool state;
715
716         state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
717
718         I915_STATE_WARN(state && port_pipe == pipe,
719                         "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
720                         port_name(port), pipe_name(pipe));
721
722         I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
723                         "IBX PCH HDMI %c still using transcoder B\n",
724                         port_name(port));
725 }
726
727 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
728                                       enum pipe pipe)
729 {
730         enum pipe port_pipe;
731
732         assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
733         assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
734         assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
735
736         I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
737                         port_pipe == pipe,
738                         "PCH VGA enabled on transcoder %c, should be disabled\n",
739                         pipe_name(pipe));
740
741         I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
742                         port_pipe == pipe,
743                         "PCH LVDS enabled on transcoder %c, should be disabled\n",
744                         pipe_name(pipe));
745
746         /* PCH SDVOB multiplex with HDMIB */
747         assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
748         assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
749         assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
750 }
751
752 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
753                          struct intel_digital_port *dig_port,
754                          unsigned int expected_mask)
755 {
756         u32 port_mask;
757         i915_reg_t dpll_reg;
758
759         switch (dig_port->base.port) {
760         case PORT_B:
761                 port_mask = DPLL_PORTB_READY_MASK;
762                 dpll_reg = DPLL(0);
763                 break;
764         case PORT_C:
765                 port_mask = DPLL_PORTC_READY_MASK;
766                 dpll_reg = DPLL(0);
767                 expected_mask <<= 4;
768                 break;
769         case PORT_D:
770                 port_mask = DPLL_PORTD_READY_MASK;
771                 dpll_reg = DPIO_PHY_STATUS;
772                 break;
773         default:
774                 BUG();
775         }
776
777         if (intel_de_wait_for_register(dev_priv, dpll_reg,
778                                        port_mask, expected_mask, 1000))
779                 drm_WARN(&dev_priv->drm, 1,
780                          "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
781                          dig_port->base.base.base.id, dig_port->base.base.name,
782                          intel_de_read(dev_priv, dpll_reg) & port_mask,
783                          expected_mask);
784 }
785
786 static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
787 {
788         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
789         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
790         enum pipe pipe = crtc->pipe;
791         i915_reg_t reg;
792         u32 val, pipeconf_val;
793
794         /* Make sure PCH DPLL is enabled */
795         assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
796
797         /* FDI must be feeding us bits for PCH ports */
798         assert_fdi_tx_enabled(dev_priv, pipe);
799         assert_fdi_rx_enabled(dev_priv, pipe);
800
801         if (HAS_PCH_CPT(dev_priv)) {
802                 reg = TRANS_CHICKEN2(pipe);
803                 val = intel_de_read(dev_priv, reg);
804                 /*
805                  * Workaround: Set the timing override bit
806                  * before enabling the pch transcoder.
807                  */
808                 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
809                 /* Configure frame start delay to match the CPU */
810                 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
811                 val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
812                 intel_de_write(dev_priv, reg, val);
813         }
814
815         reg = PCH_TRANSCONF(pipe);
816         val = intel_de_read(dev_priv, reg);
817         pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
818
819         if (HAS_PCH_IBX(dev_priv)) {
820                 /* Configure frame start delay to match the CPU */
821                 val &= ~TRANS_FRAME_START_DELAY_MASK;
822                 val |= TRANS_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
823
824                 /*
825                  * Make the BPC in transcoder be consistent with
826                  * that in pipeconf reg. For HDMI we must use 8bpc
827                  * here for both 8bpc and 12bpc.
828                  */
829                 val &= ~PIPECONF_BPC_MASK;
830                 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
831                         val |= PIPECONF_8BPC;
832                 else
833                         val |= pipeconf_val & PIPECONF_BPC_MASK;
834         }
835
836         val &= ~TRANS_INTERLACE_MASK;
837         if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
838                 if (HAS_PCH_IBX(dev_priv) &&
839                     intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
840                         val |= TRANS_LEGACY_INTERLACED_ILK;
841                 else
842                         val |= TRANS_INTERLACED;
843         } else {
844                 val |= TRANS_PROGRESSIVE;
845         }
846
847         intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
848         if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
849                 drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
850                         pipe_name(pipe));
851 }
852
853 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
854                                       enum transcoder cpu_transcoder)
855 {
856         u32 val, pipeconf_val;
857
858         /* FDI must be feeding us bits for PCH ports */
859         assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
860         assert_fdi_rx_enabled(dev_priv, PIPE_A);
861
862         val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
863         /* Workaround: set timing override bit. */
864         val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
865         /* Configure frame start delay to match the CPU */
866         val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
867         val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
868         intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
869
870         val = TRANS_ENABLE;
871         pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
872
873         if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
874             PIPECONF_INTERLACED_ILK)
875                 val |= TRANS_INTERLACED;
876         else
877                 val |= TRANS_PROGRESSIVE;
878
879         intel_de_write(dev_priv, LPT_TRANSCONF, val);
880         if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
881                                   TRANS_STATE_ENABLE, 100))
882                 drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
883 }
884
885 static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
886                                        enum pipe pipe)
887 {
888         i915_reg_t reg;
889         u32 val;
890
891         /* FDI relies on the transcoder */
892         assert_fdi_tx_disabled(dev_priv, pipe);
893         assert_fdi_rx_disabled(dev_priv, pipe);
894
895         /* Ports must be off as well */
896         assert_pch_ports_disabled(dev_priv, pipe);
897
898         reg = PCH_TRANSCONF(pipe);
899         val = intel_de_read(dev_priv, reg);
900         val &= ~TRANS_ENABLE;
901         intel_de_write(dev_priv, reg, val);
902         /* wait for PCH transcoder off, transcoder state */
903         if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
904                 drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
905                         pipe_name(pipe));
906
907         if (HAS_PCH_CPT(dev_priv)) {
908                 /* Workaround: Clear the timing override chicken bit again. */
909                 reg = TRANS_CHICKEN2(pipe);
910                 val = intel_de_read(dev_priv, reg);
911                 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
912                 intel_de_write(dev_priv, reg, val);
913         }
914 }
915
916 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
917 {
918         u32 val;
919
920         val = intel_de_read(dev_priv, LPT_TRANSCONF);
921         val &= ~TRANS_ENABLE;
922         intel_de_write(dev_priv, LPT_TRANSCONF, val);
923         /* wait for PCH transcoder off, transcoder state */
924         if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
925                                     TRANS_STATE_ENABLE, 50))
926                 drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
927
928         /* Workaround: clear timing override bit. */
929         val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
930         val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
931         intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
932 }
933
934 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
935 {
936         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
937
938         if (HAS_PCH_LPT(dev_priv))
939                 return PIPE_A;
940         else
941                 return crtc->pipe;
942 }
943
944 void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
945 {
946         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
947         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
948         enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
949         enum pipe pipe = crtc->pipe;
950         i915_reg_t reg;
951         u32 val;
952
953         drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
954
955         assert_planes_disabled(crtc);
956
957         /*
958          * A pipe without a PLL won't actually be able to drive bits from
959          * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
960          * need the check.
961          */
962         if (HAS_GMCH(dev_priv)) {
963                 if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
964                         assert_dsi_pll_enabled(dev_priv);
965                 else
966                         assert_pll_enabled(dev_priv, pipe);
967         } else {
968                 if (new_crtc_state->has_pch_encoder) {
969                         /* if driving the PCH, we need FDI enabled */
970                         assert_fdi_rx_pll_enabled(dev_priv,
971                                                   intel_crtc_pch_transcoder(crtc));
972                         assert_fdi_tx_pll_enabled(dev_priv,
973                                                   (enum pipe) cpu_transcoder);
974                 }
975                 /* FIXME: assert CPU port conditions for SNB+ */
976         }
977
978         /* Wa_22012358565:adlp */
979         if (DISPLAY_VER(dev_priv) == 13)
980                 intel_de_rmw(dev_priv, PIPE_ARB_CTL(pipe),
981                              0, PIPE_ARB_USE_PROG_SLOTS);
982
983         reg = PIPECONF(cpu_transcoder);
984         val = intel_de_read(dev_priv, reg);
985         if (val & PIPECONF_ENABLE) {
986                 /* we keep both pipes enabled on 830 */
987                 drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
988                 return;
989         }
990
991         intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
992         intel_de_posting_read(dev_priv, reg);
993
994         /*
995          * Until the pipe starts PIPEDSL reads will return a stale value,
996          * which causes an apparent vblank timestamp jump when PIPEDSL
997          * resets to its proper value. That also messes up the frame count
998          * when it's derived from the timestamps. So let's wait for the
999          * pipe to start properly before we call drm_crtc_vblank_on()
1000          */
1001         if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
1002                 intel_wait_for_pipe_scanline_moving(crtc);
1003 }
1004
1005 void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
1006 {
1007         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1008         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1009         enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
1010         enum pipe pipe = crtc->pipe;
1011         i915_reg_t reg;
1012         u32 val;
1013
1014         drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
1015
1016         /*
1017          * Make sure planes won't keep trying to pump pixels to us,
1018          * or we might hang the display.
1019          */
1020         assert_planes_disabled(crtc);
1021
1022         reg = PIPECONF(cpu_transcoder);
1023         val = intel_de_read(dev_priv, reg);
1024         if ((val & PIPECONF_ENABLE) == 0)
1025                 return;
1026
1027         /*
1028          * Double wide has implications for planes
1029          * so best keep it disabled when not needed.
1030          */
1031         if (old_crtc_state->double_wide)
1032                 val &= ~PIPECONF_DOUBLE_WIDE;
1033
1034         /* Don't disable pipe or pipe PLLs if needed */
1035         if (!IS_I830(dev_priv))
1036                 val &= ~PIPECONF_ENABLE;
1037
1038         intel_de_write(dev_priv, reg, val);
1039         if ((val & PIPECONF_ENABLE) == 0)
1040                 intel_wait_for_pipe_off(old_crtc_state);
1041 }
1042
1043 bool
1044 intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
1045                                     u64 modifier)
1046 {
1047         return info->is_yuv &&
1048                info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
1049 }
1050
1051 unsigned int
1052 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
1053 {
1054         struct drm_i915_private *dev_priv = to_i915(fb->dev);
1055         unsigned int cpp = fb->format->cpp[color_plane];
1056
1057         switch (fb->modifier) {
1058         case DRM_FORMAT_MOD_LINEAR:
1059                 return intel_tile_size(dev_priv);
1060         case I915_FORMAT_MOD_X_TILED:
1061                 if (DISPLAY_VER(dev_priv) == 2)
1062                         return 128;
1063                 else
1064                         return 512;
1065         case I915_FORMAT_MOD_Y_TILED_CCS:
1066                 if (is_ccs_plane(fb, color_plane))
1067                         return 128;
1068                 fallthrough;
1069         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1070         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1071         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1072                 if (is_ccs_plane(fb, color_plane))
1073                         return 64;
1074                 fallthrough;
1075         case I915_FORMAT_MOD_Y_TILED:
1076                 if (DISPLAY_VER(dev_priv) == 2 || HAS_128_BYTE_Y_TILING(dev_priv))
1077                         return 128;
1078                 else
1079                         return 512;
1080         case I915_FORMAT_MOD_Yf_TILED_CCS:
1081                 if (is_ccs_plane(fb, color_plane))
1082                         return 128;
1083                 fallthrough;
1084         case I915_FORMAT_MOD_Yf_TILED:
1085                 switch (cpp) {
1086                 case 1:
1087                         return 64;
1088                 case 2:
1089                 case 4:
1090                         return 128;
1091                 case 8:
1092                 case 16:
1093                         return 256;
1094                 default:
1095                         MISSING_CASE(cpp);
1096                         return cpp;
1097                 }
1098                 break;
1099         default:
1100                 MISSING_CASE(fb->modifier);
1101                 return cpp;
1102         }
1103 }
1104
1105 unsigned int
1106 intel_fb_align_height(const struct drm_framebuffer *fb,
1107                       int color_plane, unsigned int height)
1108 {
1109         unsigned int tile_height = intel_tile_height(fb, color_plane);
1110
1111         return ALIGN(height, tile_height);
1112 }
1113
1114 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
1115 {
1116         unsigned int size = 0;
1117         int i;
1118
1119         for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
1120                 size += rot_info->plane[i].dst_stride * rot_info->plane[i].width;
1121
1122         return size;
1123 }
1124
1125 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
1126 {
1127         unsigned int size = 0;
1128         int i;
1129
1130         for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
1131                 size += rem_info->plane[i].dst_stride * rem_info->plane[i].height;
1132
1133         return size;
1134 }
1135
1136 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
1137 {
1138         if (DISPLAY_VER(dev_priv) >= 9)
1139                 return 256 * 1024;
1140         else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
1141                  IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
1142                 return 128 * 1024;
1143         else if (DISPLAY_VER(dev_priv) >= 4)
1144                 return 4 * 1024;
1145         else
1146                 return 0;
1147 }
1148
1149 static bool has_async_flips(struct drm_i915_private *i915)
1150 {
1151         return DISPLAY_VER(i915) >= 5;
1152 }
1153
1154 unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
1155                                   int color_plane)
1156 {
1157         struct drm_i915_private *dev_priv = to_i915(fb->dev);
1158
1159         if (intel_fb_uses_dpt(fb))
1160                 return 512 * 4096;
1161
1162         /* AUX_DIST needs only 4K alignment */
1163         if (is_ccs_plane(fb, color_plane))
1164                 return 4096;
1165
1166         if (is_semiplanar_uv_plane(fb, color_plane)) {
1167                 /*
1168                  * TODO: cross-check wrt. the bspec stride in bytes * 64 bytes
1169                  * alignment for linear UV planes on all platforms.
1170                  */
1171                 if (DISPLAY_VER(dev_priv) >= 12) {
1172                         if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
1173                                 return intel_linear_alignment(dev_priv);
1174
1175                         return intel_tile_row_size(fb, color_plane);
1176                 }
1177
1178                 return 4096;
1179         }
1180
1181         drm_WARN_ON(&dev_priv->drm, color_plane != 0);
1182
1183         switch (fb->modifier) {
1184         case DRM_FORMAT_MOD_LINEAR:
1185                 return intel_linear_alignment(dev_priv);
1186         case I915_FORMAT_MOD_X_TILED:
1187                 if (has_async_flips(dev_priv))
1188                         return 256 * 1024;
1189                 return 0;
1190         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1191         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1192         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1193                 return 16 * 1024;
1194         case I915_FORMAT_MOD_Y_TILED_CCS:
1195         case I915_FORMAT_MOD_Yf_TILED_CCS:
1196         case I915_FORMAT_MOD_Y_TILED:
1197         case I915_FORMAT_MOD_Yf_TILED:
1198                 return 1 * 1024 * 1024;
1199         default:
1200                 MISSING_CASE(fb->modifier);
1201                 return 0;
1202         }
1203 }
1204
1205 static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
1206 {
1207         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1208         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1209
1210         return DISPLAY_VER(dev_priv) < 4 ||
1211                 (plane->has_fbc &&
1212                  plane_state->view.gtt.type == I915_GGTT_VIEW_NORMAL);
1213 }
1214
1215 static struct i915_vma *
1216 intel_pin_fb_obj_dpt(struct drm_framebuffer *fb,
1217                      const struct i915_ggtt_view *view,
1218                      bool uses_fence,
1219                      unsigned long *out_flags,
1220                      struct i915_address_space *vm)
1221 {
1222         struct drm_device *dev = fb->dev;
1223         struct drm_i915_private *dev_priv = to_i915(dev);
1224         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1225         struct i915_vma *vma;
1226         u32 alignment;
1227         int ret;
1228
1229         if (WARN_ON(!i915_gem_object_is_framebuffer(obj)))
1230                 return ERR_PTR(-EINVAL);
1231
1232         alignment = 4096 * 512;
1233
1234         atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
1235
1236         ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
1237         if (ret) {
1238                 vma = ERR_PTR(ret);
1239                 goto err;
1240         }
1241
1242         vma = i915_vma_instance(obj, vm, view);
1243         if (IS_ERR(vma))
1244                 goto err;
1245
1246         if (i915_vma_misplaced(vma, 0, alignment, 0)) {
1247                 ret = i915_vma_unbind(vma);
1248                 if (ret) {
1249                         vma = ERR_PTR(ret);
1250                         goto err;
1251                 }
1252         }
1253
1254         ret = i915_vma_pin(vma, 0, alignment, PIN_GLOBAL);
1255         if (ret) {
1256                 vma = ERR_PTR(ret);
1257                 goto err;
1258         }
1259
1260         vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
1261
1262         i915_gem_object_flush_if_display(obj);
1263
1264         i915_vma_get(vma);
1265 err:
1266         atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
1267
1268         return vma;
1269 }
1270
1271 struct i915_vma *
1272 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
1273                            bool phys_cursor,
1274                            const struct i915_ggtt_view *view,
1275                            bool uses_fence,
1276                            unsigned long *out_flags)
1277 {
1278         struct drm_device *dev = fb->dev;
1279         struct drm_i915_private *dev_priv = to_i915(dev);
1280         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1281         intel_wakeref_t wakeref;
1282         struct i915_gem_ww_ctx ww;
1283         struct i915_vma *vma;
1284         unsigned int pinctl;
1285         u32 alignment;
1286         int ret;
1287
1288         if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
1289                 return ERR_PTR(-EINVAL);
1290
1291         if (phys_cursor)
1292                 alignment = intel_cursor_alignment(dev_priv);
1293         else
1294                 alignment = intel_surf_alignment(fb, 0);
1295         if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
1296                 return ERR_PTR(-EINVAL);
1297
1298         /* Note that the w/a also requires 64 PTE of padding following the
1299          * bo. We currently fill all unused PTE with the shadow page and so
1300          * we should always have valid PTE following the scanout preventing
1301          * the VT-d warning.
1302          */
1303         if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
1304                 alignment = 256 * 1024;
1305
1306         /*
1307          * Global gtt pte registers are special registers which actually forward
1308          * writes to a chunk of system memory. Which means that there is no risk
1309          * that the register values disappear as soon as we call
1310          * intel_runtime_pm_put(), so it is correct to wrap only the
1311          * pin/unpin/fence and not more.
1312          */
1313         wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
1314
1315         atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
1316
1317         /*
1318          * Valleyview is definitely limited to scanning out the first
1319          * 512MiB. Lets presume this behaviour was inherited from the
1320          * g4x display engine and that all earlier gen are similarly
1321          * limited. Testing suggests that it is a little more
1322          * complicated than this. For example, Cherryview appears quite
1323          * happy to scanout from anywhere within its global aperture.
1324          */
1325         pinctl = 0;
1326         if (HAS_GMCH(dev_priv))
1327                 pinctl |= PIN_MAPPABLE;
1328
1329         i915_gem_ww_ctx_init(&ww, true);
1330 retry:
1331         ret = i915_gem_object_lock(obj, &ww);
1332         if (!ret && phys_cursor)
1333                 ret = i915_gem_object_attach_phys(obj, alignment);
1334         if (!ret)
1335                 ret = i915_gem_object_pin_pages(obj);
1336         if (ret)
1337                 goto err;
1338
1339         if (!ret) {
1340                 vma = i915_gem_object_pin_to_display_plane(obj, &ww, alignment,
1341                                                            view, pinctl);
1342                 if (IS_ERR(vma)) {
1343                         ret = PTR_ERR(vma);
1344                         goto err_unpin;
1345                 }
1346         }
1347
1348         if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
1349                 /*
1350                  * Install a fence for tiled scan-out. Pre-i965 always needs a
1351                  * fence, whereas 965+ only requires a fence if using
1352                  * framebuffer compression.  For simplicity, we always, when
1353                  * possible, install a fence as the cost is not that onerous.
1354                  *
1355                  * If we fail to fence the tiled scanout, then either the
1356                  * modeset will reject the change (which is highly unlikely as
1357                  * the affected systems, all but one, do not have unmappable
1358                  * space) or we will not be able to enable full powersaving
1359                  * techniques (also likely not to apply due to various limits
1360                  * FBC and the like impose on the size of the buffer, which
1361                  * presumably we violated anyway with this unmappable buffer).
1362                  * Anyway, it is presumably better to stumble onwards with
1363                  * something and try to run the system in a "less than optimal"
1364                  * mode that matches the user configuration.
1365                  */
1366                 ret = i915_vma_pin_fence(vma);
1367                 if (ret != 0 && DISPLAY_VER(dev_priv) < 4) {
1368                         i915_vma_unpin(vma);
1369                         goto err_unpin;
1370                 }
1371                 ret = 0;
1372
1373                 if (vma->fence)
1374                         *out_flags |= PLANE_HAS_FENCE;
1375         }
1376
1377         i915_vma_get(vma);
1378
1379 err_unpin:
1380         i915_gem_object_unpin_pages(obj);
1381 err:
1382         if (ret == -EDEADLK) {
1383                 ret = i915_gem_ww_ctx_backoff(&ww);
1384                 if (!ret)
1385                         goto retry;
1386         }
1387         i915_gem_ww_ctx_fini(&ww);
1388         if (ret)
1389                 vma = ERR_PTR(ret);
1390
1391         atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
1392         intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
1393         return vma;
1394 }
1395
1396 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
1397 {
1398         if (flags & PLANE_HAS_FENCE)
1399                 i915_vma_unpin_fence(vma);
1400         i915_vma_unpin(vma);
1401         i915_vma_put(vma);
1402 }
1403
1404 /*
1405  * Convert the x/y offsets into a linear offset.
1406  * Only valid with 0/180 degree rotation, which is fine since linear
1407  * offset is only used with linear buffers on pre-hsw and tiled buffers
1408  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
1409  */
1410 u32 intel_fb_xy_to_linear(int x, int y,
1411                           const struct intel_plane_state *state,
1412                           int color_plane)
1413 {
1414         const struct drm_framebuffer *fb = state->hw.fb;
1415         unsigned int cpp = fb->format->cpp[color_plane];
1416         unsigned int pitch = state->view.color_plane[color_plane].stride;
1417
1418         return y * pitch + x * cpp;
1419 }
1420
1421 /*
1422  * Add the x/y offsets derived from fb->offsets[] to the user
1423  * specified plane src x/y offsets. The resulting x/y offsets
1424  * specify the start of scanout from the beginning of the gtt mapping.
1425  */
1426 void intel_add_fb_offsets(int *x, int *y,
1427                           const struct intel_plane_state *state,
1428                           int color_plane)
1429
1430 {
1431         *x += state->view.color_plane[color_plane].x;
1432         *y += state->view.color_plane[color_plane].y;
1433 }
1434
1435 static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
1436 {
1437         switch (fb_modifier) {
1438         case I915_FORMAT_MOD_X_TILED:
1439                 return I915_TILING_X;
1440         case I915_FORMAT_MOD_Y_TILED:
1441         case I915_FORMAT_MOD_Y_TILED_CCS:
1442         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1443         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1444         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1445                 return I915_TILING_Y;
1446         default:
1447                 return I915_TILING_NONE;
1448         }
1449 }
1450
1451 /*
1452  * From the Sky Lake PRM:
1453  * "The Color Control Surface (CCS) contains the compression status of
1454  *  the cache-line pairs. The compression state of the cache-line pair
1455  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
1456  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
1457  *  cache-line-pairs. CCS is always Y tiled."
1458  *
1459  * Since cache line pairs refers to horizontally adjacent cache lines,
1460  * each cache line in the CCS corresponds to an area of 32x16 cache
1461  * lines on the main surface. Since each pixel is 4 bytes, this gives
1462  * us a ratio of one byte in the CCS for each 8x16 pixels in the
1463  * main surface.
1464  */
1465 static const struct drm_format_info skl_ccs_formats[] = {
1466         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
1467           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
1468         { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
1469           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
1470         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
1471           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
1472         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
1473           .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
1474 };
1475
1476 /*
1477  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
1478  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
1479  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
1480  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
1481  * the main surface.
1482  */
1483 static const struct drm_format_info gen12_ccs_formats[] = {
1484         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
1485           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1486           .hsub = 1, .vsub = 1, },
1487         { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
1488           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1489           .hsub = 1, .vsub = 1, },
1490         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
1491           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1492           .hsub = 1, .vsub = 1, .has_alpha = true },
1493         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
1494           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1495           .hsub = 1, .vsub = 1, .has_alpha = true },
1496         { .format = DRM_FORMAT_YUYV, .num_planes = 2,
1497           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1498           .hsub = 2, .vsub = 1, .is_yuv = true },
1499         { .format = DRM_FORMAT_YVYU, .num_planes = 2,
1500           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1501           .hsub = 2, .vsub = 1, .is_yuv = true },
1502         { .format = DRM_FORMAT_UYVY, .num_planes = 2,
1503           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1504           .hsub = 2, .vsub = 1, .is_yuv = true },
1505         { .format = DRM_FORMAT_VYUY, .num_planes = 2,
1506           .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1507           .hsub = 2, .vsub = 1, .is_yuv = true },
1508         { .format = DRM_FORMAT_XYUV8888, .num_planes = 2,
1509           .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1510           .hsub = 1, .vsub = 1, .is_yuv = true },
1511         { .format = DRM_FORMAT_NV12, .num_planes = 4,
1512           .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
1513           .hsub = 2, .vsub = 2, .is_yuv = true },
1514         { .format = DRM_FORMAT_P010, .num_planes = 4,
1515           .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
1516           .hsub = 2, .vsub = 2, .is_yuv = true },
1517         { .format = DRM_FORMAT_P012, .num_planes = 4,
1518           .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
1519           .hsub = 2, .vsub = 2, .is_yuv = true },
1520         { .format = DRM_FORMAT_P016, .num_planes = 4,
1521           .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
1522           .hsub = 2, .vsub = 2, .is_yuv = true },
1523 };
1524
1525 /*
1526  * Same as gen12_ccs_formats[] above, but with additional surface used
1527  * to pass Clear Color information in plane 2 with 64 bits of data.
1528  */
1529 static const struct drm_format_info gen12_ccs_cc_formats[] = {
1530         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 3,
1531           .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1532           .hsub = 1, .vsub = 1, },
1533         { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 3,
1534           .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1535           .hsub = 1, .vsub = 1, },
1536         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 3,
1537           .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1538           .hsub = 1, .vsub = 1, .has_alpha = true },
1539         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 3,
1540           .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1541           .hsub = 1, .vsub = 1, .has_alpha = true },
1542 };
1543
1544 static const struct drm_format_info *
1545 lookup_format_info(const struct drm_format_info formats[],
1546                    int num_formats, u32 format)
1547 {
1548         int i;
1549
1550         for (i = 0; i < num_formats; i++) {
1551                 if (formats[i].format == format)
1552                         return &formats[i];
1553         }
1554
1555         return NULL;
1556 }
1557
1558 static const struct drm_format_info *
1559 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
1560 {
1561         switch (cmd->modifier[0]) {
1562         case I915_FORMAT_MOD_Y_TILED_CCS:
1563         case I915_FORMAT_MOD_Yf_TILED_CCS:
1564                 return lookup_format_info(skl_ccs_formats,
1565                                           ARRAY_SIZE(skl_ccs_formats),
1566                                           cmd->pixel_format);
1567         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1568         case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1569                 return lookup_format_info(gen12_ccs_formats,
1570                                           ARRAY_SIZE(gen12_ccs_formats),
1571                                           cmd->pixel_format);
1572         case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1573                 return lookup_format_info(gen12_ccs_cc_formats,
1574                                           ARRAY_SIZE(gen12_ccs_cc_formats),
1575                                           cmd->pixel_format);
1576         default:
1577                 return NULL;
1578         }
1579 }
1580
1581 static int gen12_ccs_aux_stride(struct drm_framebuffer *fb, int ccs_plane)
1582 {
1583         return DIV_ROUND_UP(fb->pitches[skl_ccs_to_main_plane(fb, ccs_plane)],
1584                             512) * 64;
1585 }
1586
1587 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
1588                               u32 pixel_format, u64 modifier)
1589 {
1590         struct intel_crtc *crtc;
1591         struct intel_plane *plane;
1592
1593         if (!HAS_DISPLAY(dev_priv))
1594                 return 0;
1595
1596         /*
1597          * We assume the primary plane for pipe A has
1598          * the highest stride limits of them all,
1599          * if in case pipe A is disabled, use the first pipe from pipe_mask.
1600          */
1601         crtc = intel_get_first_crtc(dev_priv);
1602         if (!crtc)
1603                 return 0;
1604
1605         plane = to_intel_plane(crtc->base.primary);
1606
1607         return plane->max_stride(plane, pixel_format, modifier,
1608                                  DRM_MODE_ROTATE_0);
1609 }
1610
1611 static
1612 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
1613                         u32 pixel_format, u64 modifier)
1614 {
1615         /*
1616          * Arbitrary limit for gen4+ chosen to match the
1617          * render engine max stride.
1618          *
1619          * The new CCS hash mode makes remapping impossible
1620          */
1621         if (DISPLAY_VER(dev_priv) < 4 || is_ccs_modifier(modifier) ||
1622             intel_modifier_uses_dpt(dev_priv, modifier))
1623                 return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
1624         else if (DISPLAY_VER(dev_priv) >= 7)
1625                 return 256 * 1024;
1626         else
1627                 return 128 * 1024;
1628 }
1629
1630 static u32
1631 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
1632 {
1633         struct drm_i915_private *dev_priv = to_i915(fb->dev);
1634         u32 tile_width;
1635
1636         if (is_surface_linear(fb, color_plane)) {
1637                 u32 max_stride = intel_plane_fb_max_stride(dev_priv,
1638                                                            fb->format->format,
1639                                                            fb->modifier);
1640
1641                 /*
1642                  * To make remapping with linear generally feasible
1643                  * we need the stride to be page aligned.
1644                  */
1645                 if (fb->pitches[color_plane] > max_stride &&
1646                     !is_ccs_modifier(fb->modifier))
1647                         return intel_tile_size(dev_priv);
1648                 else
1649                         return 64;
1650         }
1651
1652         tile_width = intel_tile_width_bytes(fb, color_plane);
1653         if (is_ccs_modifier(fb->modifier)) {
1654                 /*
1655                  * Display WA #0531: skl,bxt,kbl,glk
1656                  *
1657                  * Render decompression and plane width > 3840
1658                  * combined with horizontal panning requires the
1659                  * plane stride to be a multiple of 4. We'll just
1660                  * require the entire fb to accommodate that to avoid
1661                  * potential runtime errors at plane configuration time.
1662                  */
1663                 if ((DISPLAY_VER(dev_priv) == 9 || IS_GEMINILAKE(dev_priv)) &&
1664                     color_plane == 0 && fb->width > 3840)
1665                         tile_width *= 4;
1666                 /*
1667                  * The main surface pitch must be padded to a multiple of four
1668                  * tile widths.
1669                  */
1670                 else if (DISPLAY_VER(dev_priv) >= 12)
1671                         tile_width *= 4;
1672         }
1673         return tile_width;
1674 }
1675
1676 static struct i915_vma *
1677 initial_plane_vma(struct drm_i915_private *i915,
1678                   struct intel_initial_plane_config *plane_config)
1679 {
1680         struct drm_i915_gem_object *obj;
1681         struct i915_vma *vma;
1682         u32 base, size;
1683
1684         if (plane_config->size == 0)
1685                 return NULL;
1686
1687         base = round_down(plane_config->base,
1688                           I915_GTT_MIN_ALIGNMENT);
1689         size = round_up(plane_config->base + plane_config->size,
1690                         I915_GTT_MIN_ALIGNMENT);
1691         size -= base;
1692
1693         /*
1694          * If the FB is too big, just don't use it since fbdev is not very
1695          * important and we should probably use that space with FBC or other
1696          * features.
1697          */
1698         if (IS_ENABLED(CONFIG_FRAMEBUFFER_CONSOLE) &&
1699             size * 2 > i915->stolen_usable_size)
1700                 return NULL;
1701
1702         obj = i915_gem_object_create_stolen_for_preallocated(i915, base, size);
1703         if (IS_ERR(obj))
1704                 return NULL;
1705
1706         /*
1707          * Mark it WT ahead of time to avoid changing the
1708          * cache_level during fbdev initialization. The
1709          * unbind there would get stuck waiting for rcu.
1710          */
1711         i915_gem_object_set_cache_coherency(obj, HAS_WT(i915) ?
1712                                             I915_CACHE_WT : I915_CACHE_NONE);
1713
1714         switch (plane_config->tiling) {
1715         case I915_TILING_NONE:
1716                 break;
1717         case I915_TILING_X:
1718         case I915_TILING_Y:
1719                 obj->tiling_and_stride =
1720                         plane_config->fb->base.pitches[0] |
1721                         plane_config->tiling;
1722                 break;
1723         default:
1724                 MISSING_CASE(plane_config->tiling);
1725                 goto err_obj;
1726         }
1727
1728         vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
1729         if (IS_ERR(vma))
1730                 goto err_obj;
1731
1732         if (i915_ggtt_pin(vma, NULL, 0, PIN_MAPPABLE | PIN_OFFSET_FIXED | base))
1733                 goto err_obj;
1734
1735         if (i915_gem_object_is_tiled(obj) &&
1736             !i915_vma_is_map_and_fenceable(vma))
1737                 goto err_obj;
1738
1739         return vma;
1740
1741 err_obj:
1742         i915_gem_object_put(obj);
1743         return NULL;
1744 }
1745
1746 static bool
1747 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
1748                               struct intel_initial_plane_config *plane_config)
1749 {
1750         struct drm_device *dev = crtc->base.dev;
1751         struct drm_i915_private *dev_priv = to_i915(dev);
1752         struct drm_mode_fb_cmd2 mode_cmd = { 0 };
1753         struct drm_framebuffer *fb = &plane_config->fb->base;
1754         struct i915_vma *vma;
1755
1756         switch (fb->modifier) {
1757         case DRM_FORMAT_MOD_LINEAR:
1758         case I915_FORMAT_MOD_X_TILED:
1759         case I915_FORMAT_MOD_Y_TILED:
1760                 break;
1761         default:
1762                 drm_dbg(&dev_priv->drm,
1763                         "Unsupported modifier for initial FB: 0x%llx\n",
1764                         fb->modifier);
1765                 return false;
1766         }
1767
1768         vma = initial_plane_vma(dev_priv, plane_config);
1769         if (!vma)
1770                 return false;
1771
1772         mode_cmd.pixel_format = fb->format->format;
1773         mode_cmd.width = fb->width;
1774         mode_cmd.height = fb->height;
1775         mode_cmd.pitches[0] = fb->pitches[0];
1776         mode_cmd.modifier[0] = fb->modifier;
1777         mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
1778
1779         if (intel_framebuffer_init(to_intel_framebuffer(fb),
1780                                    vma->obj, &mode_cmd)) {
1781                 drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
1782                 goto err_vma;
1783         }
1784
1785         plane_config->vma = vma;
1786         return true;
1787
1788 err_vma:
1789         i915_vma_put(vma);
1790         return false;
1791 }
1792
1793 static void
1794 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
1795                         struct intel_plane_state *plane_state,
1796                         bool visible)
1797 {
1798         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1799
1800         plane_state->uapi.visible = visible;
1801
1802         if (visible)
1803                 crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
1804         else
1805                 crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
1806 }
1807
1808 static void fixup_plane_bitmasks(struct intel_crtc_state *crtc_state)
1809 {
1810         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1811         struct drm_plane *plane;
1812
1813         /*
1814          * Active_planes aliases if multiple "primary" or cursor planes
1815          * have been used on the same (or wrong) pipe. plane_mask uses
1816          * unique ids, hence we can use that to reconstruct active_planes.
1817          */
1818         crtc_state->enabled_planes = 0;
1819         crtc_state->active_planes = 0;
1820
1821         drm_for_each_plane_mask(plane, &dev_priv->drm,
1822                                 crtc_state->uapi.plane_mask) {
1823                 crtc_state->enabled_planes |= BIT(to_intel_plane(plane)->id);
1824                 crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
1825         }
1826 }
1827
1828 static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
1829                                          struct intel_plane *plane)
1830 {
1831         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1832         struct intel_crtc_state *crtc_state =
1833                 to_intel_crtc_state(crtc->base.state);
1834         struct intel_plane_state *plane_state =
1835                 to_intel_plane_state(plane->base.state);
1836
1837         drm_dbg_kms(&dev_priv->drm,
1838                     "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
1839                     plane->base.base.id, plane->base.name,
1840                     crtc->base.base.id, crtc->base.name);
1841
1842         intel_set_plane_visible(crtc_state, plane_state, false);
1843         fixup_plane_bitmasks(crtc_state);
1844         crtc_state->data_rate[plane->id] = 0;
1845         crtc_state->min_cdclk[plane->id] = 0;
1846
1847         if (plane->id == PLANE_PRIMARY)
1848                 hsw_disable_ips(crtc_state);
1849
1850         /*
1851          * Vblank time updates from the shadow to live plane control register
1852          * are blocked if the memory self-refresh mode is active at that
1853          * moment. So to make sure the plane gets truly disabled, disable
1854          * first the self-refresh mode. The self-refresh enable bit in turn
1855          * will be checked/applied by the HW only at the next frame start
1856          * event which is after the vblank start event, so we need to have a
1857          * wait-for-vblank between disabling the plane and the pipe.
1858          */
1859         if (HAS_GMCH(dev_priv) &&
1860             intel_set_memory_cxsr(dev_priv, false))
1861                 intel_wait_for_vblank(dev_priv, crtc->pipe);
1862
1863         /*
1864          * Gen2 reports pipe underruns whenever all planes are disabled.
1865          * So disable underrun reporting before all the planes get disabled.
1866          */
1867         if (DISPLAY_VER(dev_priv) == 2 && !crtc_state->active_planes)
1868                 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
1869
1870         intel_disable_plane(plane, crtc_state);
1871         intel_wait_for_vblank(dev_priv, crtc->pipe);
1872 }
1873
1874 static struct i915_vma *intel_dpt_pin(struct i915_address_space *vm)
1875 {
1876         struct drm_i915_private *i915 = vm->i915;
1877         struct i915_dpt *dpt = i915_vm_to_dpt(vm);
1878         intel_wakeref_t wakeref;
1879         struct i915_vma *vma;
1880         void __iomem *iomem;
1881
1882         wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1883         atomic_inc(&i915->gpu_error.pending_fb_pin);
1884
1885         vma = i915_gem_object_ggtt_pin(dpt->obj, NULL, 0, 4096,
1886                                        HAS_LMEM(i915) ? 0 : PIN_MAPPABLE);
1887         if (IS_ERR(vma))
1888                 goto err;
1889
1890         iomem = i915_vma_pin_iomap(vma);
1891         i915_vma_unpin(vma);
1892         if (IS_ERR(iomem)) {
1893                 vma = iomem;
1894                 goto err;
1895         }
1896
1897         dpt->vma = vma;
1898         dpt->iomem = iomem;
1899
1900         i915_vma_get(vma);
1901
1902 err:
1903         atomic_dec(&i915->gpu_error.pending_fb_pin);
1904         intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1905
1906         return vma;
1907 }
1908
1909 static void intel_dpt_unpin(struct i915_address_space *vm)
1910 {
1911         struct i915_dpt *dpt = i915_vm_to_dpt(vm);
1912
1913         i915_vma_unpin_iomap(dpt->vma);
1914         i915_vma_put(dpt->vma);
1915 }
1916
1917 static void
1918 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
1919                              struct intel_initial_plane_config *plane_config)
1920 {
1921         struct drm_device *dev = intel_crtc->base.dev;
1922         struct drm_i915_private *dev_priv = to_i915(dev);
1923         struct drm_crtc *c;
1924         struct drm_plane *primary = intel_crtc->base.primary;
1925         struct drm_plane_state *plane_state = primary->state;
1926         struct intel_plane *intel_plane = to_intel_plane(primary);
1927         struct intel_plane_state *intel_state =
1928                 to_intel_plane_state(plane_state);
1929         struct intel_crtc_state *crtc_state =
1930                 to_intel_crtc_state(intel_crtc->base.state);
1931         struct drm_framebuffer *fb;
1932         struct i915_vma *vma;
1933
1934         /*
1935          * TODO:
1936          *   Disable planes if get_initial_plane_config() failed.
1937          *   Make sure things work if the surface base is not page aligned.
1938          */
1939         if (!plane_config->fb)
1940                 return;
1941
1942         if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
1943                 fb = &plane_config->fb->base;
1944                 vma = plane_config->vma;
1945                 goto valid_fb;
1946         }
1947
1948         /*
1949          * Failed to alloc the obj, check to see if we should share
1950          * an fb with another CRTC instead
1951          */
1952         for_each_crtc(dev, c) {
1953                 struct intel_plane_state *state;
1954
1955                 if (c == &intel_crtc->base)
1956                         continue;
1957
1958                 if (!to_intel_crtc_state(c->state)->uapi.active)
1959                         continue;
1960
1961                 state = to_intel_plane_state(c->primary->state);
1962                 if (!state->ggtt_vma)
1963                         continue;
1964
1965                 if (intel_plane_ggtt_offset(state) == plane_config->base) {
1966                         fb = state->hw.fb;
1967                         vma = state->ggtt_vma;
1968                         goto valid_fb;
1969                 }
1970         }
1971
1972         /*
1973          * We've failed to reconstruct the BIOS FB.  Current display state
1974          * indicates that the primary plane is visible, but has a NULL FB,
1975          * which will lead to problems later if we don't fix it up.  The
1976          * simplest solution is to just disable the primary plane now and
1977          * pretend the BIOS never had it enabled.
1978          */
1979         intel_plane_disable_noatomic(intel_crtc, intel_plane);
1980         if (crtc_state->bigjoiner) {
1981                 struct intel_crtc *slave =
1982                         crtc_state->bigjoiner_linked_crtc;
1983                 intel_plane_disable_noatomic(slave, to_intel_plane(slave->base.primary));
1984         }
1985
1986         return;
1987
1988 valid_fb:
1989         plane_state->rotation = plane_config->rotation;
1990         intel_fb_fill_view(to_intel_framebuffer(fb), plane_state->rotation,
1991                            &intel_state->view);
1992
1993         __i915_vma_pin(vma);
1994         intel_state->ggtt_vma = i915_vma_get(vma);
1995         if (intel_plane_uses_fence(intel_state) && i915_vma_pin_fence(vma) == 0)
1996                 if (vma->fence)
1997                         intel_state->flags |= PLANE_HAS_FENCE;
1998
1999         plane_state->src_x = 0;
2000         plane_state->src_y = 0;
2001         plane_state->src_w = fb->width << 16;
2002         plane_state->src_h = fb->height << 16;
2003
2004         plane_state->crtc_x = 0;
2005         plane_state->crtc_y = 0;
2006         plane_state->crtc_w = fb->width;
2007         plane_state->crtc_h = fb->height;
2008
2009         if (plane_config->tiling)
2010                 dev_priv->preserve_bios_swizzle = true;
2011
2012         plane_state->fb = fb;
2013         drm_framebuffer_get(fb);
2014
2015         plane_state->crtc = &intel_crtc->base;
2016         intel_plane_copy_uapi_to_hw_state(intel_state, intel_state,
2017                                           intel_crtc);
2018
2019         intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
2020
2021         atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2022                   &to_intel_frontbuffer(fb)->bits);
2023 }
2024
2025 unsigned int
2026 intel_plane_fence_y_offset(const struct intel_plane_state *plane_state)
2027 {
2028         int x = 0, y = 0;
2029
2030         intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
2031                                           plane_state->view.color_plane[0].offset, 0);
2032
2033         return y;
2034 }
2035
2036 static int
2037 __intel_display_resume(struct drm_device *dev,
2038                        struct drm_atomic_state *state,
2039                        struct drm_modeset_acquire_ctx *ctx)
2040 {
2041         struct drm_crtc_state *crtc_state;
2042         struct drm_crtc *crtc;
2043         int i, ret;
2044
2045         intel_modeset_setup_hw_state(dev, ctx);
2046         intel_vga_redisable(to_i915(dev));
2047
2048         if (!state)
2049                 return 0;
2050
2051         /*
2052          * We've duplicated the state, pointers to the old state are invalid.
2053          *
2054          * Don't attempt to use the old state until we commit the duplicated state.
2055          */
2056         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2057                 /*
2058                  * Force recalculation even if we restore
2059                  * current state. With fast modeset this may not result
2060                  * in a modeset when the state is compatible.
2061                  */
2062                 crtc_state->mode_changed = true;
2063         }
2064
2065         /* ignore any reset values/BIOS leftovers in the WM registers */
2066         if (!HAS_GMCH(to_i915(dev)))
2067                 to_intel_atomic_state(state)->skip_intermediate_wm = true;
2068
2069         ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
2070
2071         drm_WARN_ON(dev, ret == -EDEADLK);
2072         return ret;
2073 }
2074
2075 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
2076 {
2077         return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
2078                 intel_has_gpu_reset(&dev_priv->gt));
2079 }
2080
2081 void intel_display_prepare_reset(struct drm_i915_private *dev_priv)
2082 {
2083         struct drm_device *dev = &dev_priv->drm;
2084         struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
2085         struct drm_atomic_state *state;
2086         int ret;
2087
2088         if (!HAS_DISPLAY(dev_priv))
2089                 return;
2090
2091         /* reset doesn't touch the display */
2092         if (!dev_priv->params.force_reset_modeset_test &&
2093             !gpu_reset_clobbers_display(dev_priv))
2094                 return;
2095
2096         /* We have a modeset vs reset deadlock, defensively unbreak it. */
2097         set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
2098         smp_mb__after_atomic();
2099         wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
2100
2101         if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
2102                 drm_dbg_kms(&dev_priv->drm,
2103                             "Modeset potentially stuck, unbreaking through wedging\n");
2104                 intel_gt_set_wedged(&dev_priv->gt);
2105         }
2106
2107         /*
2108          * Need mode_config.mutex so that we don't
2109          * trample ongoing ->detect() and whatnot.
2110          */
2111         mutex_lock(&dev->mode_config.mutex);
2112         drm_modeset_acquire_init(ctx, 0);
2113         while (1) {
2114                 ret = drm_modeset_lock_all_ctx(dev, ctx);
2115                 if (ret != -EDEADLK)
2116                         break;
2117
2118                 drm_modeset_backoff(ctx);
2119         }
2120         /*
2121          * Disabling the crtcs gracefully seems nicer. Also the
2122          * g33 docs say we should at least disable all the planes.
2123          */
2124         state = drm_atomic_helper_duplicate_state(dev, ctx);
2125         if (IS_ERR(state)) {
2126                 ret = PTR_ERR(state);
2127                 drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
2128                         ret);
2129                 return;
2130         }
2131
2132         ret = drm_atomic_helper_disable_all(dev, ctx);
2133         if (ret) {
2134                 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
2135                         ret);
2136                 drm_atomic_state_put(state);
2137                 return;
2138         }
2139
2140         dev_priv->modeset_restore_state = state;
2141         state->acquire_ctx = ctx;
2142 }
2143
2144 void intel_display_finish_reset(struct drm_i915_private *dev_priv)
2145 {
2146         struct drm_device *dev = &dev_priv->drm;
2147         struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
2148         struct drm_atomic_state *state;
2149         int ret;
2150
2151         if (!HAS_DISPLAY(dev_priv))
2152                 return;
2153
2154         /* reset doesn't touch the display */
2155         if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
2156                 return;
2157
2158         state = fetch_and_zero(&dev_priv->modeset_restore_state);
2159         if (!state)
2160                 goto unlock;
2161
2162         /* reset doesn't touch the display */
2163         if (!gpu_reset_clobbers_display(dev_priv)) {
2164                 /* for testing only restore the display */
2165                 ret = __intel_display_resume(dev, state, ctx);
2166                 if (ret)
2167                         drm_err(&dev_priv->drm,
2168                                 "Restoring old state failed with %i\n", ret);
2169         } else {
2170                 /*
2171                  * The display has been reset as well,
2172                  * so need a full re-initialization.
2173                  */
2174                 intel_pps_unlock_regs_wa(dev_priv);
2175                 intel_modeset_init_hw(dev_priv);
2176                 intel_init_clock_gating(dev_priv);
2177                 intel_hpd_init(dev_priv);
2178
2179                 ret = __intel_display_resume(dev, state, ctx);
2180                 if (ret)
2181                         drm_err(&dev_priv->drm,
2182                                 "Restoring old state failed with %i\n", ret);
2183
2184                 intel_hpd_poll_disable(dev_priv);
2185         }
2186
2187         drm_atomic_state_put(state);
2188 unlock:
2189         drm_modeset_drop_locks(ctx);
2190         drm_modeset_acquire_fini(ctx);
2191         mutex_unlock(&dev->mode_config.mutex);
2192
2193         clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
2194 }
2195
2196 static void icl_set_pipe_chicken(struct intel_crtc *crtc)
2197 {
2198         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2199         enum pipe pipe = crtc->pipe;
2200         u32 tmp;
2201
2202         tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
2203
2204         /*
2205          * Display WA #1153: icl
2206          * enable hardware to bypass the alpha math
2207          * and rounding for per-pixel values 00 and 0xff
2208          */
2209         tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
2210         /*
2211          * Display WA # 1605353570: icl
2212          * Set the pixel rounding bit to 1 for allowing
2213          * passthrough of Frame buffer pixels unmodified
2214          * across pipe
2215          */
2216         tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
2217
2218         /*
2219          * "The underrun recovery mechanism should be disabled
2220          *  when the following is enabled for this pipe:
2221          *  WiDi
2222          *  Downscaling (this includes YUV420 fullblend)
2223          *  COG
2224          *  DSC
2225          *  PSR2"
2226          *
2227          * FIXME: enable whenever possible...
2228          */
2229         if (IS_ALDERLAKE_P(dev_priv))
2230                 tmp |= UNDERRUN_RECOVERY_DISABLE;
2231
2232         intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
2233 }
2234
2235 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
2236 {
2237         struct drm_crtc *crtc;
2238         bool cleanup_done;
2239
2240         drm_for_each_crtc(crtc, &dev_priv->drm) {
2241                 struct drm_crtc_commit *commit;
2242                 spin_lock(&crtc->commit_lock);
2243                 commit = list_first_entry_or_null(&crtc->commit_list,
2244                                                   struct drm_crtc_commit, commit_entry);
2245                 cleanup_done = commit ?
2246                         try_wait_for_completion(&commit->cleanup_done) : true;
2247                 spin_unlock(&crtc->commit_lock);
2248
2249                 if (cleanup_done)
2250                         continue;
2251
2252                 drm_crtc_wait_one_vblank(crtc);
2253
2254                 return true;
2255         }
2256
2257         return false;
2258 }
2259
2260 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
2261 {
2262         u32 temp;
2263
2264         intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
2265
2266         mutex_lock(&dev_priv->sb_lock);
2267
2268         temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2269         temp |= SBI_SSCCTL_DISABLE;
2270         intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2271
2272         mutex_unlock(&dev_priv->sb_lock);
2273 }
2274
2275 /* Program iCLKIP clock to the desired frequency */
2276 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
2277 {
2278         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2279         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2280         int clock = crtc_state->hw.adjusted_mode.crtc_clock;
2281         u32 divsel, phaseinc, auxdiv, phasedir = 0;
2282         u32 temp;
2283
2284         lpt_disable_iclkip(dev_priv);
2285
2286         /* The iCLK virtual clock root frequency is in MHz,
2287          * but the adjusted_mode->crtc_clock in in KHz. To get the
2288          * divisors, it is necessary to divide one by another, so we
2289          * convert the virtual clock precision to KHz here for higher
2290          * precision.
2291          */
2292         for (auxdiv = 0; auxdiv < 2; auxdiv++) {
2293                 u32 iclk_virtual_root_freq = 172800 * 1000;
2294                 u32 iclk_pi_range = 64;
2295                 u32 desired_divisor;
2296
2297                 desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
2298                                                     clock << auxdiv);
2299                 divsel = (desired_divisor / iclk_pi_range) - 2;
2300                 phaseinc = desired_divisor % iclk_pi_range;
2301
2302                 /*
2303                  * Near 20MHz is a corner case which is
2304                  * out of range for the 7-bit divisor
2305                  */
2306                 if (divsel <= 0x7f)
2307                         break;
2308         }
2309
2310         /* This should not happen with any sane values */
2311         drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2312                     ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2313         drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
2314                     ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2315
2316         drm_dbg_kms(&dev_priv->drm,
2317                     "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2318                     clock, auxdiv, divsel, phasedir, phaseinc);
2319
2320         mutex_lock(&dev_priv->sb_lock);
2321
2322         /* Program SSCDIVINTPHASE6 */
2323         temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2324         temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2325         temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2326         temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2327         temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2328         temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2329         temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2330         intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
2331
2332         /* Program SSCAUXDIV */
2333         temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2334         temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2335         temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2336         intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
2337
2338         /* Enable modulator and associated divider */
2339         temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2340         temp &= ~SBI_SSCCTL_DISABLE;
2341         intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2342
2343         mutex_unlock(&dev_priv->sb_lock);
2344
2345         /* Wait for initialization time */
2346         udelay(24);
2347
2348         intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2349 }
2350
2351 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
2352 {
2353         u32 divsel, phaseinc, auxdiv;
2354         u32 iclk_virtual_root_freq = 172800 * 1000;
2355         u32 iclk_pi_range = 64;
2356         u32 desired_divisor;
2357         u32 temp;
2358
2359         if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
2360                 return 0;
2361
2362         mutex_lock(&dev_priv->sb_lock);
2363
2364         temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2365         if (temp & SBI_SSCCTL_DISABLE) {
2366                 mutex_unlock(&dev_priv->sb_lock);
2367                 return 0;
2368         }
2369
2370         temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2371         divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
2372                 SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
2373         phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
2374                 SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
2375
2376         temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2377         auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
2378                 SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
2379
2380         mutex_unlock(&dev_priv->sb_lock);
2381
2382         desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
2383
2384         return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
2385                                  desired_divisor << auxdiv);
2386 }
2387
2388 static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
2389                                            enum pipe pch_transcoder)
2390 {
2391         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2392         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2393         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2394
2395         intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
2396                        intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
2397         intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
2398                        intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
2399         intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
2400                        intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
2401
2402         intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
2403                        intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
2404         intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
2405                        intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
2406         intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
2407                        intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
2408         intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2409                        intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
2410 }
2411
2412 static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
2413 {
2414         u32 temp;
2415
2416         temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
2417         if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
2418                 return;
2419
2420         drm_WARN_ON(&dev_priv->drm,
2421                     intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
2422                     FDI_RX_ENABLE);
2423         drm_WARN_ON(&dev_priv->drm,
2424                     intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
2425                     FDI_RX_ENABLE);
2426
2427         temp &= ~FDI_BC_BIFURCATION_SELECT;
2428         if (enable)
2429                 temp |= FDI_BC_BIFURCATION_SELECT;
2430
2431         drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
2432                     enable ? "en" : "dis");
2433         intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
2434         intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
2435 }
2436
2437 static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
2438 {
2439         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2440         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2441
2442         switch (crtc->pipe) {
2443         case PIPE_A:
2444                 break;
2445         case PIPE_B:
2446                 if (crtc_state->fdi_lanes > 2)
2447                         cpt_set_fdi_bc_bifurcation(dev_priv, false);
2448                 else
2449                         cpt_set_fdi_bc_bifurcation(dev_priv, true);
2450
2451                 break;
2452         case PIPE_C:
2453                 cpt_set_fdi_bc_bifurcation(dev_priv, true);
2454
2455                 break;
2456         default:
2457                 BUG();
2458         }
2459 }
2460
2461 /*
2462  * Finds the encoder associated with the given CRTC. This can only be
2463  * used when we know that the CRTC isn't feeding multiple encoders!
2464  */
2465 struct intel_encoder *
2466 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
2467                            const struct intel_crtc_state *crtc_state)
2468 {
2469         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2470         const struct drm_connector_state *connector_state;
2471         const struct drm_connector *connector;
2472         struct intel_encoder *encoder = NULL;
2473         int num_encoders = 0;
2474         int i;
2475
2476         for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
2477                 if (connector_state->crtc != &crtc->base)
2478                         continue;
2479
2480                 encoder = to_intel_encoder(connector_state->best_encoder);
2481                 num_encoders++;
2482         }
2483
2484         drm_WARN(encoder->base.dev, num_encoders != 1,
2485                  "%d encoders for pipe %c\n",
2486                  num_encoders, pipe_name(crtc->pipe));
2487
2488         return encoder;
2489 }
2490
2491 /*
2492  * Enable PCH resources required for PCH ports:
2493  *   - PCH PLLs
2494  *   - FDI training & RX/TX
2495  *   - update transcoder timings
2496  *   - DP transcoding bits
2497  *   - transcoder
2498  */
2499 static void ilk_pch_enable(const struct intel_atomic_state *state,
2500                            const struct intel_crtc_state *crtc_state)
2501 {
2502         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2503         struct drm_device *dev = crtc->base.dev;
2504         struct drm_i915_private *dev_priv = to_i915(dev);
2505         enum pipe pipe = crtc->pipe;
2506         u32 temp;
2507
2508         assert_pch_transcoder_disabled(dev_priv, pipe);
2509
2510         if (IS_IVYBRIDGE(dev_priv))
2511                 ivb_update_fdi_bc_bifurcation(crtc_state);
2512
2513         /* Write the TU size bits before fdi link training, so that error
2514          * detection works. */
2515         intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
2516                        intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2517
2518         /* For PCH output, training FDI link */
2519         dev_priv->display.fdi_link_train(crtc, crtc_state);
2520
2521         /* We need to program the right clock selection before writing the pixel
2522          * mutliplier into the DPLL. */
2523         if (HAS_PCH_CPT(dev_priv)) {
2524                 u32 sel;
2525
2526                 temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
2527                 temp |= TRANS_DPLL_ENABLE(pipe);
2528                 sel = TRANS_DPLLB_SEL(pipe);
2529                 if (crtc_state->shared_dpll ==
2530                     intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
2531                         temp |= sel;
2532                 else
2533                         temp &= ~sel;
2534                 intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
2535         }
2536
2537         /* XXX: pch pll's can be enabled any time before we enable the PCH
2538          * transcoder, and we actually should do this to not upset any PCH
2539          * transcoder that already use the clock when we share it.
2540          *
2541          * Note that enable_shared_dpll tries to do the right thing, but
2542          * get_shared_dpll unconditionally resets the pll - we need that to have
2543          * the right LVDS enable sequence. */
2544         intel_enable_shared_dpll(crtc_state);
2545
2546         /* set transcoder timing, panel must allow it */
2547         assert_panel_unlocked(dev_priv, pipe);
2548         ilk_pch_transcoder_set_timings(crtc_state, pipe);
2549
2550         intel_fdi_normal_train(crtc);
2551
2552         /* For PCH DP, enable TRANS_DP_CTL */
2553         if (HAS_PCH_CPT(dev_priv) &&
2554             intel_crtc_has_dp_encoder(crtc_state)) {
2555                 const struct drm_display_mode *adjusted_mode =
2556                         &crtc_state->hw.adjusted_mode;
2557                 u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
2558                 i915_reg_t reg = TRANS_DP_CTL(pipe);
2559                 enum port port;
2560
2561                 temp = intel_de_read(dev_priv, reg);
2562                 temp &= ~(TRANS_DP_PORT_SEL_MASK |
2563                           TRANS_DP_SYNC_MASK |
2564                           TRANS_DP_BPC_MASK);
2565                 temp |= TRANS_DP_OUTPUT_ENABLE;
2566                 temp |= bpc << 9; /* same format but at 11:9 */
2567
2568                 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
2569                         temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
2570                 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
2571                         temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
2572
2573                 port = intel_get_crtc_new_encoder(state, crtc_state)->port;
2574                 drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
2575                 temp |= TRANS_DP_PORT_SEL(port);
2576
2577                 intel_de_write(dev_priv, reg, temp);
2578         }
2579
2580         ilk_enable_pch_transcoder(crtc_state);
2581 }
2582
2583 void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
2584 {
2585         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2586         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2587         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2588
2589         assert_pch_transcoder_disabled(dev_priv, PIPE_A);
2590
2591         lpt_program_iclkip(crtc_state);
2592
2593         /* Set transcoder timing. */
2594         ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
2595
2596         lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
2597 }
2598
2599 static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
2600                                enum pipe pipe)
2601 {
2602         i915_reg_t dslreg = PIPEDSL(pipe);
2603         u32 temp;
2604
2605         temp = intel_de_read(dev_priv, dslreg);
2606         udelay(500);
2607         if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
2608                 if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
2609                         drm_err(&dev_priv->drm,
2610                                 "mode set failed: pipe %c stuck\n",
2611                                 pipe_name(pipe));
2612         }
2613 }
2614
2615 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
2616 {
2617         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2618         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2619         const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
2620         enum pipe pipe = crtc->pipe;
2621         int width = drm_rect_width(dst);
2622         int height = drm_rect_height(dst);
2623         int x = dst->x1;
2624         int y = dst->y1;
2625
2626         if (!crtc_state->pch_pfit.enabled)
2627                 return;
2628
2629         /* Force use of hard-coded filter coefficients
2630          * as some pre-programmed values are broken,
2631          * e.g. x201.
2632          */
2633         if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
2634                 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
2635                                PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
2636         else
2637                 intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
2638                                PF_FILTER_MED_3x3);
2639         intel_de_write(dev_priv, PF_WIN_POS(pipe), x << 16 | y);
2640         intel_de_write(dev_priv, PF_WIN_SZ(pipe), width << 16 | height);
2641 }
2642
2643 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
2644 {
2645         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2646         struct drm_device *dev = crtc->base.dev;
2647         struct drm_i915_private *dev_priv = to_i915(dev);
2648
2649         if (!crtc_state->ips_enabled)
2650                 return;
2651
2652         /*
2653          * We can only enable IPS after we enable a plane and wait for a vblank
2654          * This function is called from post_plane_update, which is run after
2655          * a vblank wait.
2656          */
2657         drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
2658
2659         if (IS_BROADWELL(dev_priv)) {
2660                 drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
2661                                                          IPS_ENABLE | IPS_PCODE_CONTROL));
2662                 /* Quoting Art Runyan: "its not safe to expect any particular
2663                  * value in IPS_CTL bit 31 after enabling IPS through the
2664                  * mailbox." Moreover, the mailbox may return a bogus state,
2665                  * so we need to just enable it and continue on.
2666                  */
2667         } else {
2668                 intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
2669                 /* The bit only becomes 1 in the next vblank, so this wait here
2670                  * is essentially intel_wait_for_vblank. If we don't have this
2671                  * and don't wait for vblanks until the end of crtc_enable, then
2672                  * the HW state readout code will complain that the expected
2673                  * IPS_CTL value is not the one we read. */
2674                 if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
2675                         drm_err(&dev_priv->drm,
2676                                 "Timed out waiting for IPS enable\n");
2677         }
2678 }
2679
2680 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
2681 {
2682         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2683         struct drm_device *dev = crtc->base.dev;
2684         struct drm_i915_private *dev_priv = to_i915(dev);
2685
2686         if (!crtc_state->ips_enabled)
2687                 return;
2688
2689         if (IS_BROADWELL(dev_priv)) {
2690                 drm_WARN_ON(dev,
2691                             sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
2692                 /*
2693                  * Wait for PCODE to finish disabling IPS. The BSpec specified
2694                  * 42ms timeout value leads to occasional timeouts so use 100ms
2695                  * instead.
2696                  */
2697                 if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
2698                         drm_err(&dev_priv->drm,
2699                                 "Timed out waiting for IPS disable\n");
2700         } else {
2701                 intel_de_write(dev_priv, IPS_CTL, 0);
2702                 intel_de_posting_read(dev_priv, IPS_CTL);
2703         }
2704
2705         /* We need to wait for a vblank before we can disable the plane. */
2706         intel_wait_for_vblank(dev_priv, crtc->pipe);
2707 }
2708
2709 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
2710 {
2711         if (intel_crtc->overlay)
2712                 (void) intel_overlay_switch_off(intel_crtc->overlay);
2713
2714         /* Let userspace switch the overlay on again. In most cases userspace
2715          * has to recompute where to put it anyway.
2716          */
2717 }
2718
2719 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
2720                                        const struct intel_crtc_state *new_crtc_state)
2721 {
2722         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
2723         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2724
2725         if (!old_crtc_state->ips_enabled)
2726                 return false;
2727
2728         if (intel_crtc_needs_modeset(new_crtc_state))
2729                 return true;
2730
2731         /*
2732          * Workaround : Do not read or write the pipe palette/gamma data while
2733          * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
2734          *
2735          * Disable IPS before we program the LUT.
2736          */
2737         if (IS_HASWELL(dev_priv) &&
2738             (new_crtc_state->uapi.color_mgmt_changed ||
2739              new_crtc_state->update_pipe) &&
2740             new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
2741                 return true;
2742
2743         return !new_crtc_state->ips_enabled;
2744 }
2745
2746 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
2747                                        const struct intel_crtc_state *new_crtc_state)
2748 {
2749         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
2750         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2751
2752         if (!new_crtc_state->ips_enabled)
2753                 return false;
2754
2755         if (intel_crtc_needs_modeset(new_crtc_state))
2756                 return true;
2757
2758         /*
2759          * Workaround : Do not read or write the pipe palette/gamma data while
2760          * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
2761          *
2762          * Re-enable IPS after the LUT has been programmed.
2763          */
2764         if (IS_HASWELL(dev_priv) &&
2765             (new_crtc_state->uapi.color_mgmt_changed ||
2766              new_crtc_state->update_pipe) &&
2767             new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
2768                 return true;
2769
2770         /*
2771          * We can't read out IPS on broadwell, assume the worst and
2772          * forcibly enable IPS on the first fastset.
2773          */
2774         if (new_crtc_state->update_pipe && old_crtc_state->inherited)
2775                 return true;
2776
2777         return !old_crtc_state->ips_enabled;
2778 }
2779
2780 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
2781 {
2782         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
2783
2784         if (!crtc_state->nv12_planes)
2785                 return false;
2786
2787         /* WA Display #0827: Gen9:all */
2788         if (DISPLAY_VER(dev_priv) == 9)
2789                 return true;
2790
2791         return false;
2792 }
2793
2794 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
2795 {
2796         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
2797
2798         /* Wa_2006604312:icl,ehl */
2799         if (crtc_state->scaler_state.scaler_users > 0 && DISPLAY_VER(dev_priv) == 11)
2800                 return true;
2801
2802         return false;
2803 }
2804
2805 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
2806                             const struct intel_crtc_state *new_crtc_state)
2807 {
2808         return (!old_crtc_state->active_planes || intel_crtc_needs_modeset(new_crtc_state)) &&
2809                 new_crtc_state->active_planes;
2810 }
2811
2812 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
2813                              const struct intel_crtc_state *new_crtc_state)
2814 {
2815         return old_crtc_state->active_planes &&
2816                 (!new_crtc_state->active_planes || intel_crtc_needs_modeset(new_crtc_state));
2817 }
2818
2819 static void intel_post_plane_update(struct intel_atomic_state *state,
2820                                     struct intel_crtc *crtc)
2821 {
2822         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2823         const struct intel_crtc_state *old_crtc_state =
2824                 intel_atomic_get_old_crtc_state(state, crtc);
2825         const struct intel_crtc_state *new_crtc_state =
2826                 intel_atomic_get_new_crtc_state(state, crtc);
2827         enum pipe pipe = crtc->pipe;
2828
2829         intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
2830
2831         if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
2832                 intel_update_watermarks(crtc);
2833
2834         if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
2835                 hsw_enable_ips(new_crtc_state);
2836
2837         intel_fbc_post_update(state, crtc);
2838
2839         if (needs_nv12_wa(old_crtc_state) &&
2840             !needs_nv12_wa(new_crtc_state))
2841                 skl_wa_827(dev_priv, pipe, false);
2842
2843         if (needs_scalerclk_wa(old_crtc_state) &&
2844             !needs_scalerclk_wa(new_crtc_state))
2845                 icl_wa_scalerclkgating(dev_priv, pipe, false);
2846 }
2847
2848 static void intel_crtc_enable_flip_done(struct intel_atomic_state *state,
2849                                         struct intel_crtc *crtc)
2850 {
2851         const struct intel_crtc_state *crtc_state =
2852                 intel_atomic_get_new_crtc_state(state, crtc);
2853         u8 update_planes = crtc_state->update_planes;
2854         const struct intel_plane_state *plane_state;
2855         struct intel_plane *plane;
2856         int i;
2857
2858         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
2859                 if (plane->enable_flip_done &&
2860                     plane->pipe == crtc->pipe &&
2861                     update_planes & BIT(plane->id))
2862                         plane->enable_flip_done(plane);
2863         }
2864 }
2865
2866 static void intel_crtc_disable_flip_done(struct intel_atomic_state *state,
2867                                          struct intel_crtc *crtc)
2868 {
2869         const struct intel_crtc_state *crtc_state =
2870                 intel_atomic_get_new_crtc_state(state, crtc);
2871         u8 update_planes = crtc_state->update_planes;
2872         const struct intel_plane_state *plane_state;
2873         struct intel_plane *plane;
2874         int i;
2875
2876         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
2877                 if (plane->disable_flip_done &&
2878                     plane->pipe == crtc->pipe &&
2879                     update_planes & BIT(plane->id))
2880                         plane->disable_flip_done(plane);
2881         }
2882 }
2883
2884 static void intel_crtc_async_flip_disable_wa(struct intel_atomic_state *state,
2885                                              struct intel_crtc *crtc)
2886 {
2887         struct drm_i915_private *i915 = to_i915(state->base.dev);
2888         const struct intel_crtc_state *old_crtc_state =
2889                 intel_atomic_get_old_crtc_state(state, crtc);
2890         const struct intel_crtc_state *new_crtc_state =
2891                 intel_atomic_get_new_crtc_state(state, crtc);
2892         u8 update_planes = new_crtc_state->update_planes;
2893         const struct intel_plane_state *old_plane_state;
2894         struct intel_plane *plane;
2895         bool need_vbl_wait = false;
2896         int i;
2897
2898         for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
2899                 if (plane->need_async_flip_disable_wa &&
2900                     plane->pipe == crtc->pipe &&
2901                     update_planes & BIT(plane->id)) {
2902                         /*
2903                          * Apart from the async flip bit we want to
2904                          * preserve the old state for the plane.
2905                          */
2906                         plane->async_flip(plane, old_crtc_state,
2907                                           old_plane_state, false);
2908                         need_vbl_wait = true;
2909                 }
2910         }
2911
2912         if (need_vbl_wait)
2913                 intel_wait_for_vblank(i915, crtc->pipe);
2914 }
2915
2916 static void intel_pre_plane_update(struct intel_atomic_state *state,
2917                                    struct intel_crtc *crtc)
2918 {
2919         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2920         const struct intel_crtc_state *old_crtc_state =
2921                 intel_atomic_get_old_crtc_state(state, crtc);
2922         const struct intel_crtc_state *new_crtc_state =
2923                 intel_atomic_get_new_crtc_state(state, crtc);
2924         enum pipe pipe = crtc->pipe;
2925
2926         if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
2927                 hsw_disable_ips(old_crtc_state);
2928
2929         if (intel_fbc_pre_update(state, crtc))
2930                 intel_wait_for_vblank(dev_priv, pipe);
2931
2932         /* Display WA 827 */
2933         if (!needs_nv12_wa(old_crtc_state) &&
2934             needs_nv12_wa(new_crtc_state))
2935                 skl_wa_827(dev_priv, pipe, true);
2936
2937         /* Wa_2006604312:icl,ehl */
2938         if (!needs_scalerclk_wa(old_crtc_state) &&
2939             needs_scalerclk_wa(new_crtc_state))
2940                 icl_wa_scalerclkgating(dev_priv, pipe, true);
2941
2942         /*
2943          * Vblank time updates from the shadow to live plane control register
2944          * are blocked if the memory self-refresh mode is active at that
2945          * moment. So to make sure the plane gets truly disabled, disable
2946          * first the self-refresh mode. The self-refresh enable bit in turn
2947          * will be checked/applied by the HW only at the next frame start
2948          * event which is after the vblank start event, so we need to have a
2949          * wait-for-vblank between disabling the plane and the pipe.
2950          */
2951         if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
2952             new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
2953                 intel_wait_for_vblank(dev_priv, pipe);
2954
2955         /*
2956          * IVB workaround: must disable low power watermarks for at least
2957          * one frame before enabling scaling.  LP watermarks can be re-enabled
2958          * when scaling is disabled.
2959          *
2960          * WaCxSRDisabledForSpriteScaling:ivb
2961          */
2962         if (old_crtc_state->hw.active &&
2963             new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
2964                 intel_wait_for_vblank(dev_priv, pipe);
2965
2966         /*
2967          * If we're doing a modeset we don't need to do any
2968          * pre-vblank watermark programming here.
2969          */
2970         if (!intel_crtc_needs_modeset(new_crtc_state)) {
2971                 /*
2972                  * For platforms that support atomic watermarks, program the
2973                  * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
2974                  * will be the intermediate values that are safe for both pre- and
2975                  * post- vblank; when vblank happens, the 'active' values will be set
2976                  * to the final 'target' values and we'll do this again to get the
2977                  * optimal watermarks.  For gen9+ platforms, the values we program here
2978                  * will be the final target values which will get automatically latched
2979                  * at vblank time; no further programming will be necessary.
2980                  *
2981                  * If a platform hasn't been transitioned to atomic watermarks yet,
2982                  * we'll continue to update watermarks the old way, if flags tell
2983                  * us to.
2984                  */
2985                 if (dev_priv->display.initial_watermarks)
2986                         dev_priv->display.initial_watermarks(state, crtc);
2987                 else if (new_crtc_state->update_wm_pre)
2988                         intel_update_watermarks(crtc);
2989         }
2990
2991         /*
2992          * Gen2 reports pipe underruns whenever all planes are disabled.
2993          * So disable underrun reporting before all the planes get disabled.
2994          *
2995          * We do this after .initial_watermarks() so that we have a
2996          * chance of catching underruns with the intermediate watermarks
2997          * vs. the old plane configuration.
2998          */
2999         if (DISPLAY_VER(dev_priv) == 2 && planes_disabling(old_crtc_state, new_crtc_state))
3000                 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
3001
3002         /*
3003          * WA for platforms where async address update enable bit
3004          * is double buffered and only latched at start of vblank.
3005          */
3006         if (old_crtc_state->uapi.async_flip && !new_crtc_state->uapi.async_flip)
3007                 intel_crtc_async_flip_disable_wa(state, crtc);
3008 }
3009
3010 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
3011                                       struct intel_crtc *crtc)
3012 {
3013         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3014         const struct intel_crtc_state *new_crtc_state =
3015                 intel_atomic_get_new_crtc_state(state, crtc);
3016         unsigned int update_mask = new_crtc_state->update_planes;
3017         const struct intel_plane_state *old_plane_state;
3018         struct intel_plane *plane;
3019         unsigned fb_bits = 0;
3020         int i;
3021
3022         intel_crtc_dpms_overlay_disable(crtc);
3023
3024         for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
3025                 if (crtc->pipe != plane->pipe ||
3026                     !(update_mask & BIT(plane->id)))
3027                         continue;
3028
3029                 intel_disable_plane(plane, new_crtc_state);
3030
3031                 if (old_plane_state->uapi.visible)
3032                         fb_bits |= plane->frontbuffer_bit;
3033         }
3034
3035         intel_frontbuffer_flip(dev_priv, fb_bits);
3036 }
3037
3038 /*
3039  * intel_connector_primary_encoder - get the primary encoder for a connector
3040  * @connector: connector for which to return the encoder
3041  *
3042  * Returns the primary encoder for a connector. There is a 1:1 mapping from
3043  * all connectors to their encoder, except for DP-MST connectors which have
3044  * both a virtual and a primary encoder. These DP-MST primary encoders can be
3045  * pointed to by as many DP-MST connectors as there are pipes.
3046  */
3047 static struct intel_encoder *
3048 intel_connector_primary_encoder(struct intel_connector *connector)
3049 {
3050         struct intel_encoder *encoder;
3051
3052         if (connector->mst_port)
3053                 return &dp_to_dig_port(connector->mst_port)->base;
3054
3055         encoder = intel_attached_encoder(connector);
3056         drm_WARN_ON(connector->base.dev, !encoder);
3057
3058         return encoder;
3059 }
3060
3061 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
3062 {
3063         struct drm_connector_state *new_conn_state;
3064         struct drm_connector *connector;
3065         int i;
3066
3067         for_each_new_connector_in_state(&state->base, connector, new_conn_state,
3068                                         i) {
3069                 struct intel_connector *intel_connector;
3070                 struct intel_encoder *encoder;
3071                 struct intel_crtc *crtc;
3072
3073                 if (!intel_connector_needs_modeset(state, connector))
3074                         continue;
3075
3076                 intel_connector = to_intel_connector(connector);
3077                 encoder = intel_connector_primary_encoder(intel_connector);
3078                 if (!encoder->update_prepare)
3079                         continue;
3080
3081                 crtc = new_conn_state->crtc ?
3082                         to_intel_crtc(new_conn_state->crtc) : NULL;
3083                 encoder->update_prepare(state, encoder, crtc);
3084         }
3085 }
3086
3087 static void intel_encoders_update_complete(struct intel_atomic_state *state)
3088 {
3089         struct drm_connector_state *new_conn_state;
3090         struct drm_connector *connector;
3091         int i;
3092
3093         for_each_new_connector_in_state(&state->base, connector, new_conn_state,
3094                                         i) {
3095                 struct intel_connector *intel_connector;
3096                 struct intel_encoder *encoder;
3097                 struct intel_crtc *crtc;
3098
3099                 if (!intel_connector_needs_modeset(state, connector))
3100                         continue;
3101
3102                 intel_connector = to_intel_connector(connector);
3103                 encoder = intel_connector_primary_encoder(intel_connector);
3104                 if (!encoder->update_complete)
3105                         continue;
3106
3107                 crtc = new_conn_state->crtc ?
3108                         to_intel_crtc(new_conn_state->crtc) : NULL;
3109                 encoder->update_complete(state, encoder, crtc);
3110         }
3111 }
3112
3113 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
3114                                           struct intel_crtc *crtc)
3115 {
3116         const struct intel_crtc_state *crtc_state =
3117                 intel_atomic_get_new_crtc_state(state, crtc);
3118         const struct drm_connector_state *conn_state;
3119         struct drm_connector *conn;
3120         int i;
3121
3122         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3123                 struct intel_encoder *encoder =
3124                         to_intel_encoder(conn_state->best_encoder);
3125
3126                 if (conn_state->crtc != &crtc->base)
3127                         continue;
3128
3129                 if (encoder->pre_pll_enable)
3130                         encoder->pre_pll_enable(state, encoder,
3131                                                 crtc_state, conn_state);
3132         }
3133 }
3134
3135 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
3136                                       struct intel_crtc *crtc)
3137 {
3138         const struct intel_crtc_state *crtc_state =
3139                 intel_atomic_get_new_crtc_state(state, crtc);
3140         const struct drm_connector_state *conn_state;
3141         struct drm_connector *conn;
3142         int i;
3143
3144         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3145                 struct intel_encoder *encoder =
3146                         to_intel_encoder(conn_state->best_encoder);
3147
3148                 if (conn_state->crtc != &crtc->base)
3149                         continue;
3150
3151                 if (encoder->pre_enable)
3152                         encoder->pre_enable(state, encoder,
3153                                             crtc_state, conn_state);
3154         }
3155 }
3156
3157 static void intel_encoders_enable(struct intel_atomic_state *state,
3158                                   struct intel_crtc *crtc)
3159 {
3160         const struct intel_crtc_state *crtc_state =
3161                 intel_atomic_get_new_crtc_state(state, crtc);
3162         const struct drm_connector_state *conn_state;
3163         struct drm_connector *conn;
3164         int i;
3165
3166         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3167                 struct intel_encoder *encoder =
3168                         to_intel_encoder(conn_state->best_encoder);
3169
3170                 if (conn_state->crtc != &crtc->base)
3171                         continue;
3172
3173                 if (encoder->enable)
3174                         encoder->enable(state, encoder,
3175                                         crtc_state, conn_state);
3176                 intel_opregion_notify_encoder(encoder, true);
3177         }
3178 }
3179
3180 static void intel_encoders_disable(struct intel_atomic_state *state,
3181                                    struct intel_crtc *crtc)
3182 {
3183         const struct intel_crtc_state *old_crtc_state =
3184                 intel_atomic_get_old_crtc_state(state, crtc);
3185         const struct drm_connector_state *old_conn_state;
3186         struct drm_connector *conn;
3187         int i;
3188
3189         for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
3190                 struct intel_encoder *encoder =
3191                         to_intel_encoder(old_conn_state->best_encoder);
3192
3193                 if (old_conn_state->crtc != &crtc->base)
3194                         continue;
3195
3196                 intel_opregion_notify_encoder(encoder, false);
3197                 if (encoder->disable)
3198                         encoder->disable(state, encoder,
3199                                          old_crtc_state, old_conn_state);
3200         }
3201 }
3202
3203 static void intel_encoders_post_disable(struct intel_atomic_state *state,
3204                                         struct intel_crtc *crtc)
3205 {
3206         const struct intel_crtc_state *old_crtc_state =
3207                 intel_atomic_get_old_crtc_state(state, crtc);
3208         const struct drm_connector_state *old_conn_state;
3209         struct drm_connector *conn;
3210         int i;
3211
3212         for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
3213                 struct intel_encoder *encoder =
3214                         to_intel_encoder(old_conn_state->best_encoder);
3215
3216                 if (old_conn_state->crtc != &crtc->base)
3217                         continue;
3218
3219                 if (encoder->post_disable)
3220                         encoder->post_disable(state, encoder,
3221                                               old_crtc_state, old_conn_state);
3222         }
3223 }
3224
3225 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
3226                                             struct intel_crtc *crtc)
3227 {
3228         const struct intel_crtc_state *old_crtc_state =
3229                 intel_atomic_get_old_crtc_state(state, crtc);
3230         const struct drm_connector_state *old_conn_state;
3231         struct drm_connector *conn;
3232         int i;
3233
3234         for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
3235                 struct intel_encoder *encoder =
3236                         to_intel_encoder(old_conn_state->best_encoder);
3237
3238                 if (old_conn_state->crtc != &crtc->base)
3239                         continue;
3240
3241                 if (encoder->post_pll_disable)
3242                         encoder->post_pll_disable(state, encoder,
3243                                                   old_crtc_state, old_conn_state);
3244         }
3245 }
3246
3247 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
3248                                        struct intel_crtc *crtc)
3249 {
3250         const struct intel_crtc_state *crtc_state =
3251                 intel_atomic_get_new_crtc_state(state, crtc);
3252         const struct drm_connector_state *conn_state;
3253         struct drm_connector *conn;
3254         int i;
3255
3256         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3257                 struct intel_encoder *encoder =
3258                         to_intel_encoder(conn_state->best_encoder);
3259
3260                 if (conn_state->crtc != &crtc->base)
3261                         continue;
3262
3263                 if (encoder->update_pipe)
3264                         encoder->update_pipe(state, encoder,
3265                                              crtc_state, conn_state);
3266         }
3267 }
3268
3269 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
3270 {
3271         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3272         struct intel_plane *plane = to_intel_plane(crtc->base.primary);
3273
3274         plane->disable_plane(plane, crtc_state);
3275 }
3276
3277 static void ilk_crtc_enable(struct intel_atomic_state *state,
3278                             struct intel_crtc *crtc)
3279 {
3280         const struct intel_crtc_state *new_crtc_state =
3281                 intel_atomic_get_new_crtc_state(state, crtc);
3282         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3283         enum pipe pipe = crtc->pipe;
3284
3285         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3286                 return;
3287
3288         /*
3289          * Sometimes spurious CPU pipe underruns happen during FDI
3290          * training, at least with VGA+HDMI cloning. Suppress them.
3291          *
3292          * On ILK we get an occasional spurious CPU pipe underruns
3293          * between eDP port A enable and vdd enable. Also PCH port
3294          * enable seems to result in the occasional CPU pipe underrun.
3295          *
3296          * Spurious PCH underruns also occur during PCH enabling.
3297          */
3298         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
3299         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
3300
3301         if (new_crtc_state->has_pch_encoder)
3302                 intel_prepare_shared_dpll(new_crtc_state);
3303
3304         if (intel_crtc_has_dp_encoder(new_crtc_state))
3305                 intel_dp_set_m_n(new_crtc_state, M1_N1);
3306
3307         intel_set_transcoder_timings(new_crtc_state);
3308         intel_set_pipe_src_size(new_crtc_state);
3309
3310         if (new_crtc_state->has_pch_encoder)
3311                 intel_cpu_transcoder_set_m_n(new_crtc_state,
3312                                              &new_crtc_state->fdi_m_n, NULL);
3313
3314         ilk_set_pipeconf(new_crtc_state);
3315
3316         crtc->active = true;
3317
3318         intel_encoders_pre_enable(state, crtc);
3319
3320         if (new_crtc_state->has_pch_encoder) {
3321                 /* Note: FDI PLL enabling _must_ be done before we enable the
3322                  * cpu pipes, hence this is separate from all the other fdi/pch
3323                  * enabling. */
3324                 ilk_fdi_pll_enable(new_crtc_state);
3325         } else {
3326                 assert_fdi_tx_disabled(dev_priv, pipe);
3327                 assert_fdi_rx_disabled(dev_priv, pipe);
3328         }
3329
3330         ilk_pfit_enable(new_crtc_state);
3331
3332         /*
3333          * On ILK+ LUT must be loaded before the pipe is running but with
3334          * clocks enabled
3335          */
3336         intel_color_load_luts(new_crtc_state);
3337         intel_color_commit(new_crtc_state);
3338         /* update DSPCNTR to configure gamma for pipe bottom color */
3339         intel_disable_primary_plane(new_crtc_state);
3340
3341         if (dev_priv->display.initial_watermarks)
3342                 dev_priv->display.initial_watermarks(state, crtc);
3343         intel_enable_pipe(new_crtc_state);
3344
3345         if (new_crtc_state->has_pch_encoder)
3346                 ilk_pch_enable(state, new_crtc_state);
3347
3348         intel_crtc_vblank_on(new_crtc_state);
3349
3350         intel_encoders_enable(state, crtc);
3351
3352         if (HAS_PCH_CPT(dev_priv))
3353                 cpt_verify_modeset(dev_priv, pipe);
3354
3355         /*
3356          * Must wait for vblank to avoid spurious PCH FIFO underruns.
3357          * And a second vblank wait is needed at least on ILK with
3358          * some interlaced HDMI modes. Let's do the double wait always
3359          * in case there are more corner cases we don't know about.
3360          */
3361         if (new_crtc_state->has_pch_encoder) {
3362                 intel_wait_for_vblank(dev_priv, pipe);
3363                 intel_wait_for_vblank(dev_priv, pipe);
3364         }
3365         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3366         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
3367 }
3368
3369 /* IPS only exists on ULT machines and is tied to pipe A. */
3370 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3371 {
3372         return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
3373 }
3374
3375 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
3376                                             enum pipe pipe, bool apply)
3377 {
3378         u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
3379         u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
3380
3381         if (apply)
3382                 val |= mask;
3383         else
3384                 val &= ~mask;
3385
3386         intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
3387 }
3388
3389 static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
3390 {
3391         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3392         enum pipe pipe = crtc->pipe;
3393         u32 val;
3394
3395         val = MBUS_DBOX_A_CREDIT(2);
3396
3397         if (DISPLAY_VER(dev_priv) >= 12) {
3398                 val |= MBUS_DBOX_BW_CREDIT(2);
3399                 val |= MBUS_DBOX_B_CREDIT(12);
3400         } else {
3401                 val |= MBUS_DBOX_BW_CREDIT(1);
3402                 val |= MBUS_DBOX_B_CREDIT(8);
3403         }
3404
3405         intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
3406 }
3407
3408 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
3409 {
3410         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3411         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3412
3413         intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
3414                        HSW_LINETIME(crtc_state->linetime) |
3415                        HSW_IPS_LINETIME(crtc_state->ips_linetime));
3416 }
3417
3418 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
3419 {
3420         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3421         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3422         i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
3423         u32 val;
3424
3425         val = intel_de_read(dev_priv, reg);
3426         val &= ~HSW_FRAME_START_DELAY_MASK;
3427         val |= HSW_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
3428         intel_de_write(dev_priv, reg, val);
3429 }
3430
3431 static void icl_ddi_bigjoiner_pre_enable(struct intel_atomic_state *state,
3432                                          const struct intel_crtc_state *crtc_state)
3433 {
3434         struct intel_crtc *master = to_intel_crtc(crtc_state->uapi.crtc);
3435         struct drm_i915_private *dev_priv = to_i915(master->base.dev);
3436         struct intel_crtc_state *master_crtc_state;
3437         struct drm_connector_state *conn_state;
3438         struct drm_connector *conn;
3439         struct intel_encoder *encoder = NULL;
3440         int i;
3441
3442         if (crtc_state->bigjoiner_slave)
3443                 master = crtc_state->bigjoiner_linked_crtc;
3444
3445         master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
3446
3447         for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3448                 if (conn_state->crtc != &master->base)
3449                         continue;
3450
3451                 encoder = to_intel_encoder(conn_state->best_encoder);
3452                 break;
3453         }
3454
3455         if (!crtc_state->bigjoiner_slave) {
3456                 /* need to enable VDSC, which we skipped in pre-enable */
3457                 intel_dsc_enable(encoder, crtc_state);
3458         } else {
3459                 /*
3460                  * Enable sequence steps 1-7 on bigjoiner master
3461                  */
3462                 intel_encoders_pre_pll_enable(state, master);
3463                 intel_enable_shared_dpll(master_crtc_state);
3464                 intel_encoders_pre_enable(state, master);
3465
3466                 /* and DSC on slave */
3467                 intel_dsc_enable(NULL, crtc_state);
3468         }
3469
3470         if (DISPLAY_VER(dev_priv) >= 13)
3471                 intel_uncompressed_joiner_enable(crtc_state);
3472 }
3473
3474 static void hsw_crtc_enable(struct intel_atomic_state *state,
3475                             struct intel_crtc *crtc)
3476 {
3477         const struct intel_crtc_state *new_crtc_state =
3478                 intel_atomic_get_new_crtc_state(state, crtc);
3479         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3480         enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
3481         enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
3482         bool psl_clkgate_wa;
3483
3484         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3485                 return;
3486
3487         if (!new_crtc_state->bigjoiner) {
3488                 intel_encoders_pre_pll_enable(state, crtc);
3489
3490                 if (new_crtc_state->shared_dpll)
3491                         intel_enable_shared_dpll(new_crtc_state);
3492
3493                 intel_encoders_pre_enable(state, crtc);
3494         } else {
3495                 icl_ddi_bigjoiner_pre_enable(state, new_crtc_state);
3496         }
3497
3498         intel_set_pipe_src_size(new_crtc_state);
3499         if (DISPLAY_VER(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
3500                 bdw_set_pipemisc(new_crtc_state);
3501
3502         if (!new_crtc_state->bigjoiner_slave && !transcoder_is_dsi(cpu_transcoder)) {
3503                 intel_set_transcoder_timings(new_crtc_state);
3504
3505                 if (cpu_transcoder != TRANSCODER_EDP)
3506                         intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
3507                                        new_crtc_state->pixel_multiplier - 1);
3508
3509                 if (new_crtc_state->has_pch_encoder)
3510                         intel_cpu_transcoder_set_m_n(new_crtc_state,
3511                                                      &new_crtc_state->fdi_m_n, NULL);
3512
3513                 hsw_set_frame_start_delay(new_crtc_state);
3514         }
3515
3516         if (!transcoder_is_dsi(cpu_transcoder))
3517                 hsw_set_pipeconf(new_crtc_state);
3518
3519         crtc->active = true;
3520
3521         /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
3522         psl_clkgate_wa = DISPLAY_VER(dev_priv) == 10 &&
3523                 new_crtc_state->pch_pfit.enabled;
3524         if (psl_clkgate_wa)
3525                 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
3526
3527         if (DISPLAY_VER(dev_priv) >= 9)
3528                 skl_pfit_enable(new_crtc_state);
3529         else
3530                 ilk_pfit_enable(new_crtc_state);
3531
3532         /*
3533          * On ILK+ LUT must be loaded before the pipe is running but with
3534          * clocks enabled
3535          */
3536         intel_color_load_luts(new_crtc_state);
3537         intel_color_commit(new_crtc_state);
3538         /* update DSPCNTR to configure gamma/csc for pipe bottom color */
3539         if (DISPLAY_VER(dev_priv) < 9)
3540                 intel_disable_primary_plane(new_crtc_state);
3541
3542         hsw_set_linetime_wm(new_crtc_state);
3543
3544         if (DISPLAY_VER(dev_priv) >= 11)
3545                 icl_set_pipe_chicken(crtc);
3546
3547         if (dev_priv->display.initial_watermarks)
3548                 dev_priv->display.initial_watermarks(state, crtc);
3549
3550         if (DISPLAY_VER(dev_priv) >= 11)
3551                 icl_pipe_mbus_enable(crtc);
3552
3553         if (new_crtc_state->bigjoiner_slave)
3554                 intel_crtc_vblank_on(new_crtc_state);
3555
3556         intel_encoders_enable(state, crtc);
3557
3558         if (psl_clkgate_wa) {
3559                 intel_wait_for_vblank(dev_priv, pipe);
3560                 glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
3561         }
3562
3563         /* If we change the relative order between pipe/planes enabling, we need
3564          * to change the workaround. */
3565         hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
3566         if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
3567                 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
3568                 intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
3569         }
3570 }
3571
3572 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
3573 {
3574         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
3575         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3576         enum pipe pipe = crtc->pipe;
3577
3578         /* To avoid upsetting the power well on haswell only disable the pfit if
3579          * it's in use. The hw state code will make sure we get this right. */
3580         if (!old_crtc_state->pch_pfit.enabled)
3581                 return;
3582
3583         intel_de_write(dev_priv, PF_CTL(pipe), 0);
3584         intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
3585         intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
3586 }
3587
3588 static void ilk_crtc_disable(struct intel_atomic_state *state,
3589                              struct intel_crtc *crtc)
3590 {
3591         const struct intel_crtc_state *old_crtc_state =
3592                 intel_atomic_get_old_crtc_state(state, crtc);
3593         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3594         enum pipe pipe = crtc->pipe;
3595
3596         /*
3597          * Sometimes spurious CPU pipe underruns happen when the
3598          * pipe is already disabled, but FDI RX/TX is still enabled.
3599          * Happens at least with VGA+HDMI cloning. Suppress them.
3600          */
3601         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
3602         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
3603
3604         intel_encoders_disable(state, crtc);
3605
3606         intel_crtc_vblank_off(old_crtc_state);
3607
3608         intel_disable_pipe(old_crtc_state);
3609
3610         ilk_pfit_disable(old_crtc_state);
3611
3612         if (old_crtc_state->has_pch_encoder)
3613                 ilk_fdi_disable(crtc);
3614
3615         intel_encoders_post_disable(state, crtc);
3616
3617         if (old_crtc_state->has_pch_encoder) {
3618                 ilk_disable_pch_transcoder(dev_priv, pipe);
3619
3620                 if (HAS_PCH_CPT(dev_priv)) {
3621                         i915_reg_t reg;
3622                         u32 temp;
3623
3624                         /* disable TRANS_DP_CTL */
3625                         reg = TRANS_DP_CTL(pipe);
3626                         temp = intel_de_read(dev_priv, reg);
3627                         temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3628                                   TRANS_DP_PORT_SEL_MASK);
3629                         temp |= TRANS_DP_PORT_SEL_NONE;
3630                         intel_de_write(dev_priv, reg, temp);
3631
3632                         /* disable DPLL_SEL */
3633                         temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
3634                         temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
3635                         intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
3636                 }
3637
3638                 ilk_fdi_pll_disable(crtc);
3639         }
3640
3641         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3642         intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
3643 }
3644
3645 static void hsw_crtc_disable(struct intel_atomic_state *state,
3646                              struct intel_crtc *crtc)
3647 {
3648         /*
3649          * FIXME collapse everything to one hook.
3650          * Need care with mst->ddi interactions.
3651          */
3652         intel_encoders_disable(state, crtc);
3653         intel_encoders_post_disable(state, crtc);
3654 }
3655
3656 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
3657 {
3658         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3659         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3660
3661         if (!crtc_state->gmch_pfit.control)
3662                 return;
3663
3664         /*
3665          * The panel fitter should only be adjusted whilst the pipe is disabled,
3666          * according to register description and PRM.
3667          */
3668         drm_WARN_ON(&dev_priv->drm,
3669                     intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
3670         assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
3671
3672         intel_de_write(dev_priv, PFIT_PGM_RATIOS,
3673                        crtc_state->gmch_pfit.pgm_ratios);
3674         intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
3675
3676         /* Border color in case we don't scale up to the full screen. Black by
3677          * default, change to something else for debugging. */
3678         intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
3679 }
3680
3681 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
3682 {
3683         if (phy == PHY_NONE)
3684                 return false;
3685         else if (IS_ALDERLAKE_S(dev_priv))
3686                 return phy <= PHY_E;
3687         else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv))
3688                 return phy <= PHY_D;
3689         else if (IS_JSL_EHL(dev_priv))
3690                 return phy <= PHY_C;
3691         else if (DISPLAY_VER(dev_priv) >= 11)
3692                 return phy <= PHY_B;
3693         else
3694                 return false;
3695 }
3696
3697 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
3698 {
3699         if (IS_ALDERLAKE_P(dev_priv))
3700                 return phy >= PHY_F && phy <= PHY_I;
3701         else if (IS_TIGERLAKE(dev_priv))
3702                 return phy >= PHY_D && phy <= PHY_I;
3703         else if (IS_ICELAKE(dev_priv))
3704                 return phy >= PHY_C && phy <= PHY_F;
3705         else
3706                 return false;
3707 }
3708
3709 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
3710 {
3711         if (DISPLAY_VER(i915) >= 13 && port >= PORT_D_XELPD)
3712                 return PHY_D + port - PORT_D_XELPD;
3713         else if (DISPLAY_VER(i915) >= 13 && port >= PORT_TC1)
3714                 return PHY_F + port - PORT_TC1;
3715         else if (IS_ALDERLAKE_S(i915) && port >= PORT_TC1)
3716                 return PHY_B + port - PORT_TC1;
3717         else if ((IS_DG1(i915) || IS_ROCKETLAKE(i915)) && port >= PORT_TC1)
3718                 return PHY_C + port - PORT_TC1;
3719         else if (IS_JSL_EHL(i915) && port == PORT_D)
3720                 return PHY_A;
3721
3722         return PHY_A + port - PORT_A;
3723 }
3724
3725 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
3726 {
3727         if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
3728                 return TC_PORT_NONE;
3729
3730         if (DISPLAY_VER(dev_priv) >= 12)
3731                 return TC_PORT_1 + port - PORT_TC1;
3732         else
3733                 return TC_PORT_1 + port - PORT_C;
3734 }
3735
3736 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
3737 {
3738         switch (port) {
3739         case PORT_A:
3740                 return POWER_DOMAIN_PORT_DDI_A_LANES;
3741         case PORT_B:
3742                 return POWER_DOMAIN_PORT_DDI_B_LANES;
3743         case PORT_C:
3744                 return POWER_DOMAIN_PORT_DDI_C_LANES;
3745         case PORT_D:
3746                 return POWER_DOMAIN_PORT_DDI_D_LANES;
3747         case PORT_E:
3748                 return POWER_DOMAIN_PORT_DDI_E_LANES;
3749         case PORT_F:
3750                 return POWER_DOMAIN_PORT_DDI_F_LANES;
3751         case PORT_G:
3752                 return POWER_DOMAIN_PORT_DDI_G_LANES;
3753         case PORT_H:
3754                 return POWER_DOMAIN_PORT_DDI_H_LANES;
3755         case PORT_I:
3756                 return POWER_DOMAIN_PORT_DDI_I_LANES;
3757         default:
3758                 MISSING_CASE(port);
3759                 return POWER_DOMAIN_PORT_OTHER;
3760         }
3761 }
3762
3763 enum intel_display_power_domain
3764 intel_aux_power_domain(struct intel_digital_port *dig_port)
3765 {
3766         struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
3767         enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
3768
3769         if (intel_phy_is_tc(dev_priv, phy) &&
3770             dig_port->tc_mode == TC_PORT_TBT_ALT) {
3771                 switch (dig_port->aux_ch) {
3772                 case AUX_CH_C:
3773                         return POWER_DOMAIN_AUX_C_TBT;
3774                 case AUX_CH_D:
3775                         return POWER_DOMAIN_AUX_D_TBT;
3776                 case AUX_CH_E:
3777                         return POWER_DOMAIN_AUX_E_TBT;
3778                 case AUX_CH_F:
3779                         return POWER_DOMAIN_AUX_F_TBT;
3780                 case AUX_CH_G:
3781                         return POWER_DOMAIN_AUX_G_TBT;
3782                 case AUX_CH_H:
3783                         return POWER_DOMAIN_AUX_H_TBT;
3784                 case AUX_CH_I:
3785                         return POWER_DOMAIN_AUX_I_TBT;
3786                 default:
3787                         MISSING_CASE(dig_port->aux_ch);
3788                         return POWER_DOMAIN_AUX_C_TBT;
3789                 }
3790         }
3791
3792         return intel_legacy_aux_to_power_domain(dig_port->aux_ch);
3793 }
3794
3795 /*
3796  * Converts aux_ch to power_domain without caring about TBT ports for that use
3797  * intel_aux_power_domain()
3798  */
3799 enum intel_display_power_domain
3800 intel_legacy_aux_to_power_domain(enum aux_ch aux_ch)
3801 {
3802         switch (aux_ch) {
3803         case AUX_CH_A:
3804                 return POWER_DOMAIN_AUX_A;
3805         case AUX_CH_B:
3806                 return POWER_DOMAIN_AUX_B;
3807         case AUX_CH_C:
3808                 return POWER_DOMAIN_AUX_C;
3809         case AUX_CH_D:
3810                 return POWER_DOMAIN_AUX_D;
3811         case AUX_CH_E:
3812                 return POWER_DOMAIN_AUX_E;
3813         case AUX_CH_F:
3814                 return POWER_DOMAIN_AUX_F;
3815         case AUX_CH_G:
3816                 return POWER_DOMAIN_AUX_G;
3817         case AUX_CH_H:
3818                 return POWER_DOMAIN_AUX_H;
3819         case AUX_CH_I:
3820                 return POWER_DOMAIN_AUX_I;
3821         default:
3822                 MISSING_CASE(aux_ch);
3823                 return POWER_DOMAIN_AUX_A;
3824         }
3825 }
3826
3827 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
3828 {
3829         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3830         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3831         struct drm_encoder *encoder;
3832         enum pipe pipe = crtc->pipe;
3833         u64 mask;
3834         enum transcoder transcoder = crtc_state->cpu_transcoder;
3835
3836         if (!crtc_state->hw.active)
3837                 return 0;
3838
3839         mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
3840         mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
3841         if (crtc_state->pch_pfit.enabled ||
3842             crtc_state->pch_pfit.force_thru)
3843                 mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
3844
3845         drm_for_each_encoder_mask(encoder, &dev_priv->drm,
3846                                   crtc_state->uapi.encoder_mask) {
3847                 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3848
3849                 mask |= BIT_ULL(intel_encoder->power_domain);
3850         }
3851
3852         if (HAS_DDI(dev_priv) && crtc_state->has_audio)
3853                 mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
3854
3855         if (crtc_state->shared_dpll)
3856                 mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
3857
3858         if (crtc_state->dsc.compression_enable)
3859                 mask |= BIT_ULL(intel_dsc_power_domain(crtc_state));
3860
3861         return mask;
3862 }
3863
3864 static u64
3865 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
3866 {
3867         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3868         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3869         enum intel_display_power_domain domain;
3870         u64 domains, new_domains, old_domains;
3871
3872         domains = get_crtc_power_domains(crtc_state);
3873
3874         new_domains = domains & ~crtc->enabled_power_domains.mask;
3875         old_domains = crtc->enabled_power_domains.mask & ~domains;
3876
3877         for_each_power_domain(domain, new_domains)
3878                 intel_display_power_get_in_set(dev_priv,
3879                                                &crtc->enabled_power_domains,
3880                                                domain);
3881
3882         return old_domains;
3883 }
3884
3885 static void modeset_put_crtc_power_domains(struct intel_crtc *crtc,
3886                                            u64 domains)
3887 {
3888         intel_display_power_put_mask_in_set(to_i915(crtc->base.dev),
3889                                             &crtc->enabled_power_domains,
3890                                             domains);
3891 }
3892
3893 static void valleyview_crtc_enable(struct intel_atomic_state *state,
3894                                    struct intel_crtc *crtc)
3895 {
3896         const struct intel_crtc_state *new_crtc_state =
3897                 intel_atomic_get_new_crtc_state(state, crtc);
3898         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3899         enum pipe pipe = crtc->pipe;
3900
3901         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3902                 return;
3903
3904         if (intel_crtc_has_dp_encoder(new_crtc_state))
3905                 intel_dp_set_m_n(new_crtc_state, M1_N1);
3906
3907         intel_set_transcoder_timings(new_crtc_state);
3908         intel_set_pipe_src_size(new_crtc_state);
3909
3910         if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
3911                 intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
3912                 intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
3913         }
3914
3915         i9xx_set_pipeconf(new_crtc_state);
3916
3917         crtc->active = true;
3918
3919         intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3920
3921         intel_encoders_pre_pll_enable(state, crtc);
3922
3923         if (IS_CHERRYVIEW(dev_priv)) {
3924                 chv_prepare_pll(crtc, new_crtc_state);
3925                 chv_enable_pll(crtc, new_crtc_state);
3926         } else {
3927                 vlv_prepare_pll(crtc, new_crtc_state);
3928                 vlv_enable_pll(crtc, new_crtc_state);
3929         }
3930
3931         intel_encoders_pre_enable(state, crtc);
3932
3933         i9xx_pfit_enable(new_crtc_state);
3934
3935         intel_color_load_luts(new_crtc_state);
3936         intel_color_commit(new_crtc_state);
3937         /* update DSPCNTR to configure gamma for pipe bottom color */
3938         intel_disable_primary_plane(new_crtc_state);
3939
3940         dev_priv->display.initial_watermarks(state, crtc);
3941         intel_enable_pipe(new_crtc_state);
3942
3943         intel_crtc_vblank_on(new_crtc_state);
3944
3945         intel_encoders_enable(state, crtc);
3946 }
3947
3948 static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
3949 {
3950         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3951         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3952
3953         intel_de_write(dev_priv, FP0(crtc->pipe),
3954                        crtc_state->dpll_hw_state.fp0);
3955         intel_de_write(dev_priv, FP1(crtc->pipe),
3956                        crtc_state->dpll_hw_state.fp1);
3957 }
3958
3959 static void i9xx_crtc_enable(struct intel_atomic_state *state,
3960                              struct intel_crtc *crtc)
3961 {
3962         const struct intel_crtc_state *new_crtc_state =
3963                 intel_atomic_get_new_crtc_state(state, crtc);
3964         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3965         enum pipe pipe = crtc->pipe;
3966
3967         if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3968                 return;
3969
3970         i9xx_set_pll_dividers(new_crtc_state);
3971
3972         if (intel_crtc_has_dp_encoder(new_crtc_state))
3973                 intel_dp_set_m_n(new_crtc_state, M1_N1);
3974
3975         intel_set_transcoder_timings(new_crtc_state);
3976         intel_set_pipe_src_size(new_crtc_state);
3977
3978         i9xx_set_pipeconf(new_crtc_state);
3979
3980         crtc->active = true;
3981
3982         if (DISPLAY_VER(dev_priv) != 2)
3983                 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3984
3985         intel_encoders_pre_enable(state, crtc);
3986
3987         i9xx_enable_pll(crtc, new_crtc_state);
3988
3989         i9xx_pfit_enable(new_crtc_state);
3990
3991         intel_color_load_luts(new_crtc_state);
3992         intel_color_commit(new_crtc_state);
3993         /* update DSPCNTR to configure gamma for pipe bottom color */
3994         intel_disable_primary_plane(new_crtc_state);
3995
3996         if (dev_priv->display.initial_watermarks)
3997                 dev_priv->display.initial_watermarks(state, crtc);
3998         else
3999                 intel_update_watermarks(crtc);
4000         intel_enable_pipe(new_crtc_state);
4001
4002         intel_crtc_vblank_on(new_crtc_state);
4003
4004         intel_encoders_enable(state, crtc);
4005
4006         /* prevents spurious underruns */
4007         if (DISPLAY_VER(dev_priv) == 2)
4008                 intel_wait_for_vblank(dev_priv, pipe);
4009 }
4010
4011 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
4012 {
4013         struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
4014         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4015
4016         if (!old_crtc_state->gmch_pfit.control)
4017                 return;
4018
4019         assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
4020
4021         drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
4022                     intel_de_read(dev_priv, PFIT_CONTROL));
4023         intel_de_write(dev_priv, PFIT_CONTROL, 0);
4024 }
4025
4026 static void i9xx_crtc_disable(struct intel_atomic_state *state,
4027                               struct intel_crtc *crtc)
4028 {
4029         struct intel_crtc_state *old_crtc_state =
4030                 intel_atomic_get_old_crtc_state(state, crtc);
4031         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4032         enum pipe pipe = crtc->pipe;
4033
4034         /*
4035          * On gen2 planes are double buffered but the pipe isn't, so we must
4036          * wait for planes to fully turn off before disabling the pipe.
4037          */
4038         if (DISPLAY_VER(dev_priv) == 2)
4039                 intel_wait_for_vblank(dev_priv, pipe);
4040
4041         intel_encoders_disable(state, crtc);
4042
4043         intel_crtc_vblank_off(old_crtc_state);
4044
4045         intel_disable_pipe(old_crtc_state);
4046
4047         i9xx_pfit_disable(old_crtc_state);
4048
4049         intel_encoders_post_disable(state, crtc);
4050
4051         if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
4052                 if (IS_CHERRYVIEW(dev_priv))
4053                         chv_disable_pll(dev_priv, pipe);
4054                 else if (IS_VALLEYVIEW(dev_priv))
4055                         vlv_disable_pll(dev_priv, pipe);
4056                 else
4057                         i9xx_disable_pll(old_crtc_state);
4058         }
4059
4060         intel_encoders_post_pll_disable(state, crtc);
4061
4062         if (DISPLAY_VER(dev_priv) != 2)
4063                 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4064
4065         if (!dev_priv->display.initial_watermarks)
4066                 intel_update_watermarks(crtc);
4067
4068         /* clock the pipe down to 640x480@60 to potentially save power */
4069         if (IS_I830(dev_priv))
4070                 i830_enable_pipe(dev_priv, pipe);
4071 }
4072
4073 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
4074                                         struct drm_modeset_acquire_ctx *ctx)
4075 {
4076         struct intel_encoder *encoder;
4077         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4078         struct intel_bw_state *bw_state =
4079                 to_intel_bw_state(dev_priv->bw_obj.state);
4080         struct intel_cdclk_state *cdclk_state =
4081                 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
4082         struct intel_dbuf_state *dbuf_state =
4083                 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
4084         struct intel_crtc_state *crtc_state =
4085                 to_intel_crtc_state(crtc->base.state);
4086         struct intel_plane *plane;
4087         struct drm_atomic_state *state;
4088         struct intel_crtc_state *temp_crtc_state;
4089         enum pipe pipe = crtc->pipe;
4090         int ret;
4091
4092         if (!crtc_state->hw.active)
4093                 return;
4094
4095         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
4096                 const struct intel_plane_state *plane_state =
4097                         to_intel_plane_state(plane->base.state);
4098
4099                 if (plane_state->uapi.visible)
4100                         intel_plane_disable_noatomic(crtc, plane);
4101         }
4102
4103         state = drm_atomic_state_alloc(&dev_priv->drm);
4104         if (!state) {
4105                 drm_dbg_kms(&dev_priv->drm,
4106                             "failed to disable [CRTC:%d:%s], out of memory",
4107                             crtc->base.base.id, crtc->base.name);
4108                 return;
4109         }
4110
4111         state->acquire_ctx = ctx;
4112
4113         /* Everything's already locked, -EDEADLK can't happen. */
4114         temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
4115         ret = drm_atomic_add_affected_connectors(state, &crtc->base);
4116
4117         drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
4118
4119         dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
4120
4121         drm_atomic_state_put(state);
4122
4123         drm_dbg_kms(&dev_priv->drm,
4124                     "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
4125                     crtc->base.base.id, crtc->base.name);
4126
4127         crtc->active = false;
4128         crtc->base.enabled = false;
4129
4130         drm_WARN_ON(&dev_priv->drm,
4131                     drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
4132         crtc_state->uapi.active = false;
4133         crtc_state->uapi.connector_mask = 0;
4134         crtc_state->uapi.encoder_mask = 0;
4135         intel_crtc_free_hw_state(crtc_state);
4136         memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
4137
4138         for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
4139                 encoder->base.crtc = NULL;
4140
4141         intel_fbc_disable(crtc);
4142         intel_update_watermarks(crtc);
4143         intel_disable_shared_dpll(crtc_state);
4144
4145         intel_display_power_put_all_in_set(dev_priv, &crtc->enabled_power_domains);
4146
4147         dev_priv->active_pipes &= ~BIT(pipe);
4148         cdclk_state->min_cdclk[pipe] = 0;
4149         cdclk_state->min_voltage_level[pipe] = 0;
4150         cdclk_state->active_pipes &= ~BIT(pipe);
4151
4152         dbuf_state->active_pipes &= ~BIT(pipe);
4153
4154         bw_state->data_rate[pipe] = 0;
4155         bw_state->num_active_planes[pipe] = 0;
4156 }
4157
4158 /*
4159  * turn all crtc's off, but do not adjust state
4160  * This has to be paired with a call to intel_modeset_setup_hw_state.
4161  */
4162 int intel_display_suspend(struct drm_device *dev)
4163 {
4164         struct drm_i915_private *dev_priv = to_i915(dev);
4165         struct drm_atomic_state *state;
4166         int ret;
4167
4168         if (!HAS_DISPLAY(dev_priv))
4169                 return 0;
4170
4171         state = drm_atomic_helper_suspend(dev);
4172         ret = PTR_ERR_OR_ZERO(state);
4173         if (ret)
4174                 drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
4175                         ret);
4176         else
4177                 dev_priv->modeset_restore_state = state;
4178         return ret;
4179 }
4180
4181 void intel_encoder_destroy(struct drm_encoder *encoder)
4182 {
4183         struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
4184
4185         drm_encoder_cleanup(encoder);
4186         kfree(intel_encoder);
4187 }
4188
4189 /* Cross check the actual hw state with our own modeset state tracking (and it's
4190  * internal consistency). */
4191 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
4192                                          struct drm_connector_state *conn_state)
4193 {
4194         struct intel_connector *connector = to_intel_connector(conn_state->connector);
4195         struct drm_i915_private *i915 = to_i915(connector->base.dev);
4196
4197         drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
4198                     connector->base.base.id, connector->base.name);
4199
4200         if (connector->get_hw_state(connector)) {
4201                 struct intel_encoder *encoder = intel_attached_encoder(connector);
4202
4203                 I915_STATE_WARN(!crtc_state,
4204                          "connector enabled without attached crtc\n");
4205
4206                 if (!crtc_state)
4207                         return;
4208
4209                 I915_STATE_WARN(!crtc_state->hw.active,
4210                                 "connector is active, but attached crtc isn't\n");
4211
4212                 if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
4213                         return;
4214
4215                 I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
4216                         "atomic encoder doesn't match attached encoder\n");
4217
4218                 I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
4219                         "attached encoder crtc differs from connector crtc\n");
4220         } else {
4221                 I915_STATE_WARN(crtc_state && crtc_state->hw.active,
4222                                 "attached crtc is active, but connector isn't\n");
4223                 I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
4224                         "best encoder set without crtc!\n");
4225         }
4226 }
4227
4228 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
4229 {
4230         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4231         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4232
4233         /* IPS only exists on ULT machines and is tied to pipe A. */
4234         if (!hsw_crtc_supports_ips(crtc))
4235                 return false;
4236
4237         if (!dev_priv->params.enable_ips)
4238                 return false;
4239
4240         if (crtc_state->pipe_bpp > 24)
4241                 return false;
4242
4243         /*
4244          * We compare against max which means we must take
4245          * the increased cdclk requirement into account when
4246          * calculating the new cdclk.
4247          *
4248          * Should measure whether using a lower cdclk w/o IPS
4249          */
4250         if (IS_BROADWELL(dev_priv) &&
4251             crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
4252                 return false;
4253
4254         return true;
4255 }
4256
4257 static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
4258 {
4259         struct drm_i915_private *dev_priv =
4260                 to_i915(crtc_state->uapi.crtc->dev);
4261         struct intel_atomic_state *state =
4262                 to_intel_atomic_state(crtc_state->uapi.state);
4263
4264         crtc_state->ips_enabled = false;
4265
4266         if (!hsw_crtc_state_ips_capable(crtc_state))
4267                 return 0;
4268
4269         /*
4270          * When IPS gets enabled, the pipe CRC changes. Since IPS gets
4271          * enabled and disabled dynamically based on package C states,
4272          * user space can't make reliable use of the CRCs, so let's just
4273          * completely disable it.
4274          */
4275         if (crtc_state->crc_enabled)
4276                 return 0;
4277
4278         /* IPS should be fine as long as at least one plane is enabled. */
4279         if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
4280                 return 0;
4281
4282         if (IS_BROADWELL(dev_priv)) {
4283                 const struct intel_cdclk_state *cdclk_state;
4284
4285                 cdclk_state = intel_atomic_get_cdclk_state(state);
4286                 if (IS_ERR(cdclk_state))
4287                         return PTR_ERR(cdclk_state);
4288
4289                 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
4290                 if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
4291                         return 0;
4292         }
4293
4294         crtc_state->ips_enabled = true;
4295
4296         return 0;
4297 }
4298
4299 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
4300 {
4301         const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4302
4303         /* GDG double wide on either pipe, otherwise pipe A only */
4304         return DISPLAY_VER(dev_priv) < 4 &&
4305                 (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
4306 }
4307
4308 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
4309 {
4310         u32 pixel_rate = crtc_state->hw.pipe_mode.crtc_clock;
4311         struct drm_rect src;
4312
4313         /*
4314          * We only use IF-ID interlacing. If we ever use
4315          * PF-ID we'll need to adjust the pixel_rate here.
4316          */
4317
4318         if (!crtc_state->pch_pfit.enabled)
4319                 return pixel_rate;
4320
4321         drm_rect_init(&src, 0, 0,
4322                       crtc_state->pipe_src_w << 16,
4323                       crtc_state->pipe_src_h << 16);
4324
4325         return intel_adjusted_rate(&src, &crtc_state->pch_pfit.dst,
4326                                    pixel_rate);
4327 }
4328
4329 static void intel_mode_from_crtc_timings(struct drm_display_mode *mode,
4330                                          const struct drm_display_mode *timings)
4331 {
4332         mode->hdisplay = timings->crtc_hdisplay;
4333         mode->htotal = timings->crtc_htotal;
4334         mode->hsync_start = timings->crtc_hsync_start;
4335         mode->hsync_end = timings->crtc_hsync_end;
4336
4337         mode->vdisplay = timings->crtc_vdisplay;
4338         mode->vtotal = timings->crtc_vtotal;
4339         mode->vsync_start = timings->crtc_vsync_start;
4340         mode->vsync_end = timings->crtc_vsync_end;
4341
4342         mode->flags = timings->flags;
4343         mode->type = DRM_MODE_TYPE_DRIVER;
4344
4345         mode->clock = timings->crtc_clock;
4346
4347         drm_mode_set_name(mode);
4348 }
4349
4350 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
4351 {
4352         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4353
4354         if (HAS_GMCH(dev_priv))
4355                 /* FIXME calculate proper pipe pixel rate for GMCH pfit */
4356                 crtc_state->pixel_rate =
4357                         crtc_state->hw.pipe_mode.crtc_clock;
4358         else
4359                 crtc_state->pixel_rate =
4360                         ilk_pipe_pixel_rate(crtc_state);
4361 }
4362
4363 static void intel_crtc_readout_derived_state(struct intel_crtc_state *crtc_state)
4364 {
4365         struct drm_display_mode *mode = &crtc_state->hw.mode;
4366         struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4367         struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
4368
4369         drm_mode_copy(pipe_mode, adjusted_mode);
4370
4371         if (crtc_state->bigjoiner) {
4372                 /*
4373                  * transcoder is programmed to the full mode,
4374                  * but pipe timings are half of the transcoder mode
4375                  */
4376                 pipe_mode->crtc_hdisplay /= 2;
4377                 pipe_mode->crtc_hblank_start /= 2;
4378                 pipe_mode->crtc_hblank_end /= 2;
4379                 pipe_mode->crtc_hsync_start /= 2;
4380                 pipe_mode->crtc_hsync_end /= 2;
4381                 pipe_mode->crtc_htotal /= 2;
4382                 pipe_mode->crtc_clock /= 2;
4383         }
4384
4385         if (crtc_state->splitter.enable) {
4386                 int n = crtc_state->splitter.link_count;
4387                 int overlap = crtc_state->splitter.pixel_overlap;
4388
4389                 /*
4390                  * eDP MSO uses segment timings from EDID for transcoder
4391                  * timings, but full mode for everything else.
4392                  *
4393                  * h_full = (h_segment - pixel_overlap) * link_count
4394                  */
4395                 pipe_mode->crtc_hdisplay = (pipe_mode->crtc_hdisplay - overlap) * n;
4396                 pipe_mode->crtc_hblank_start = (pipe_mode->crtc_hblank_start - overlap) * n;
4397                 pipe_mode->crtc_hblank_end = (pipe_mode->crtc_hblank_end - overlap) * n;
4398                 pipe_mode->crtc_hsync_start = (pipe_mode->crtc_hsync_start - overlap) * n;
4399                 pipe_mode->crtc_hsync_end = (pipe_mode->crtc_hsync_end - overlap) * n;
4400                 pipe_mode->crtc_htotal = (pipe_mode->crtc_htotal - overlap) * n;
4401                 pipe_mode->crtc_clock *= n;
4402
4403                 intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
4404                 intel_mode_from_crtc_timings(adjusted_mode, pipe_mode);
4405         } else {
4406                 intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
4407                 intel_mode_from_crtc_timings(adjusted_mode, adjusted_mode);
4408         }
4409
4410         intel_crtc_compute_pixel_rate(crtc_state);
4411
4412         drm_mode_copy(mode, adjusted_mode);
4413         mode->hdisplay = crtc_state->pipe_src_w << crtc_state->bigjoiner;
4414         mode->vdisplay = crtc_state->pipe_src_h;
4415 }
4416
4417 static void intel_encoder_get_config(struct intel_encoder *encoder,
4418                                      struct intel_crtc_state *crtc_state)
4419 {
4420         encoder->get_config(encoder, crtc_state);
4421
4422         intel_crtc_readout_derived_state(crtc_state);
4423 }
4424
4425 static int intel_crtc_compute_config(struct intel_crtc *crtc,
4426                                      struct intel_crtc_state *pipe_config)
4427 {
4428         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4429         struct drm_display_mode *pipe_mode = &pipe_config->hw.pipe_mode;
4430         int clock_limit = dev_priv->max_dotclk_freq;
4431
4432         drm_mode_copy(pipe_mode, &pipe_config->hw.adjusted_mode);
4433
4434         /* Adjust pipe_mode for bigjoiner, with half the horizontal mode */
4435         if (pipe_config->bigjoiner) {
4436                 pipe_mode->crtc_clock /= 2;
4437                 pipe_mode->crtc_hdisplay /= 2;
4438                 pipe_mode->crtc_hblank_start /= 2;
4439                 pipe_mode->crtc_hblank_end /= 2;
4440                 pipe_mode->crtc_hsync_start /= 2;
4441                 pipe_mode->crtc_hsync_end /= 2;
4442                 pipe_mode->crtc_htotal /= 2;
4443                 pipe_config->pipe_src_w /= 2;
4444         }
4445
4446         if (pipe_config->splitter.enable) {
4447                 int n = pipe_config->splitter.link_count;
4448                 int overlap = pipe_config->splitter.pixel_overlap;
4449
4450                 pipe_mode->crtc_hdisplay = (pipe_mode->crtc_hdisplay - overlap) * n;
4451                 pipe_mode->crtc_hblank_start = (pipe_mode->crtc_hblank_start - overlap) * n;
4452                 pipe_mode->crtc_hblank_end = (pipe_mode->crtc_hblank_end - overlap) * n;
4453                 pipe_mode->crtc_hsync_start = (pipe_mode->crtc_hsync_start - overlap) * n;
4454                 pipe_mode->crtc_hsync_end = (pipe_mode->crtc_hsync_end - overlap) * n;
4455                 pipe_mode->crtc_htotal = (pipe_mode->crtc_htotal - overlap) * n;
4456                 pipe_mode->crtc_clock *= n;
4457         }
4458
4459         intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
4460
4461         if (DISPLAY_VER(dev_priv) < 4) {
4462                 clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
4463
4464                 /*
4465                  * Enable double wide mode when the dot clock
4466                  * is > 90% of the (display) core speed.
4467                  */
4468                 if (intel_crtc_supports_double_wide(crtc) &&
4469                     pipe_mode->crtc_clock > clock_limit) {
4470                         clock_limit = dev_priv->max_dotclk_freq;
4471                         pipe_config->double_wide = true;
4472                 }
4473         }
4474
4475         if (pipe_mode->crtc_clock > clock_limit) {
4476                 drm_dbg_kms(&dev_priv->drm,
4477                             "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
4478                             pipe_mode->crtc_clock, clock_limit,
4479                             yesno(pipe_config->double_wide));
4480                 return -EINVAL;
4481         }
4482
4483         /*
4484          * Pipe horizontal size must be even in:
4485          * - DVO ganged mode
4486          * - LVDS dual channel mode
4487          * - Double wide pipe
4488          */
4489         if (pipe_config->pipe_src_w & 1) {
4490                 if (pipe_config->double_wide) {
4491                         drm_dbg_kms(&dev_priv->drm,
4492                                     "Odd pipe source width not supported with double wide pipe\n");
4493                         return -EINVAL;
4494                 }
4495
4496                 if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
4497                     intel_is_dual_link_lvds(dev_priv)) {
4498                         drm_dbg_kms(&dev_priv->drm,
4499                                     "Odd pipe source width not supported with dual link LVDS\n");
4500                         return -EINVAL;
4501                 }
4502         }
4503
4504         /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4505          * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
4506          */
4507         if ((DISPLAY_VER(dev_priv) > 4 || IS_G4X(dev_priv)) &&
4508             pipe_mode->crtc_hsync_start == pipe_mode->crtc_hdisplay)
4509                 return -EINVAL;
4510
4511         intel_crtc_compute_pixel_rate(pipe_config);
4512
4513         if (pipe_config->has_pch_encoder)
4514                 return ilk_fdi_compute_config(crtc, pipe_config);
4515
4516         return 0;
4517 }
4518
4519 static void
4520 intel_reduce_m_n_ratio(u32 *num, u32 *den)
4521 {
4522         while (*num > DATA_LINK_M_N_MASK ||
4523                *den > DATA_LINK_M_N_MASK) {
4524                 *num >>= 1;
4525                 *den >>= 1;
4526         }
4527 }
4528
4529 static void compute_m_n(unsigned int m, unsigned int n,
4530                         u32 *ret_m, u32 *ret_n,
4531                         bool constant_n)
4532 {
4533         /*
4534          * Several DP dongles in particular seem to be fussy about
4535          * too large link M/N values. Give N value as 0x8000 that
4536          * should be acceptable by specific devices. 0x8000 is the
4537          * specified fixed N value for asynchronous clock mode,
4538          * which the devices expect also in synchronous clock mode.
4539          */
4540         if (constant_n)
4541                 *ret_n = DP_LINK_CONSTANT_N_VALUE;
4542         else
4543                 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4544
4545         *ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
4546         intel_reduce_m_n_ratio(ret_m, ret_n);
4547 }
4548
4549 void
4550 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
4551                        int pixel_clock, int link_clock,
4552                        struct intel_link_m_n *m_n,
4553                        bool constant_n, bool fec_enable)
4554 {
4555         u32 data_clock = bits_per_pixel * pixel_clock;
4556
4557         if (fec_enable)
4558                 data_clock = intel_dp_mode_to_fec_clock(data_clock);
4559
4560         m_n->tu = 64;
4561         compute_m_n(data_clock,
4562                     link_clock * nlanes * 8,
4563                     &m_n->gmch_m, &m_n->gmch_n,
4564                     constant_n);
4565
4566         compute_m_n(pixel_clock, link_clock,
4567                     &m_n->link_m, &m_n->link_n,
4568                     constant_n);
4569 }
4570
4571 static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
4572 {
4573         /*
4574          * There may be no VBT; and if the BIOS enabled SSC we can
4575          * just keep using it to avoid unnecessary flicker.  Whereas if the
4576          * BIOS isn't using it, don't assume it will work even if the VBT
4577          * indicates as much.
4578          */
4579         if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
4580                 bool bios_lvds_use_ssc = intel_de_read(dev_priv,
4581                                                        PCH_DREF_CONTROL) &
4582                         DREF_SSC1_ENABLE;
4583
4584                 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
4585                         drm_dbg_kms(&dev_priv->drm,
4586                                     "SSC %s by BIOS, overriding VBT which says %s\n",
4587                                     enableddisabled(bios_lvds_use_ssc),
4588                                     enableddisabled(dev_priv->vbt.lvds_use_ssc));
4589                         dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
4590                 }
4591         }
4592 }
4593
4594 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
4595                                          const struct intel_link_m_n *m_n)
4596 {
4597         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4598         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4599         enum pipe pipe = crtc->pipe;
4600
4601         intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
4602                        TU_SIZE(m_n->tu) | m_n->gmch_m);
4603         intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
4604         intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
4605         intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
4606 }
4607
4608 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
4609                                  enum transcoder transcoder)
4610 {
4611         if (IS_HASWELL(dev_priv))
4612                 return transcoder == TRANSCODER_EDP;
4613
4614         /*
4615          * Strictly speaking some registers are available before
4616          * gen7, but we only support DRRS on gen7+
4617          */
4618         return DISPLAY_VER(dev_priv) == 7 || IS_CHERRYVIEW(dev_priv);
4619 }
4620
4621 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
4622                                          const struct intel_link_m_n *m_n,
4623                                          const struct intel_link_m_n *m2_n2)
4624 {
4625         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4626         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4627         enum pipe pipe = crtc->pipe;
4628         enum transcoder transcoder = crtc_state->cpu_transcoder;
4629
4630         if (DISPLAY_VER(dev_priv) >= 5) {
4631                 intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
4632                                TU_SIZE(m_n->tu) | m_n->gmch_m);
4633                 intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
4634                                m_n->gmch_n);
4635                 intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
4636                                m_n->link_m);
4637                 intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
4638                                m_n->link_n);
4639                 /*
4640                  *  M2_N2 registers are set only if DRRS is supported
4641                  * (to make sure the registers are not unnecessarily accessed).
4642                  */
4643                 if (m2_n2 && crtc_state->has_drrs &&
4644                     transcoder_has_m2_n2(dev_priv, transcoder)) {
4645                         intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
4646                                        TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
4647                         intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
4648                                        m2_n2->gmch_n);
4649                         intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
4650                                        m2_n2->link_m);
4651                         intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
4652                                        m2_n2->link_n);
4653                 }
4654         } else {
4655                 intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
4656                                TU_SIZE(m_n->tu) | m_n->gmch_m);
4657                 intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
4658                 intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
4659                 intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
4660         }
4661 }
4662
4663 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
4664 {
4665         const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
4666         struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
4667
4668         if (m_n == M1_N1) {
4669                 dp_m_n = &crtc_state->dp_m_n;
4670                 dp_m2_n2 = &crtc_state->dp_m2_n2;
4671         } else if (m_n == M2_N2) {
4672
4673                 /*
4674                  * M2_N2 registers are not supported. Hence m2_n2 divider value
4675                  * needs to be programmed into M1_N1.
4676                  */
4677                 dp_m_n = &crtc_state->dp_m2_n2;
4678         } else {
4679                 drm_err(&i915->drm, "Unsupported divider value\n");
4680                 return;
4681         }
4682
4683         if (crtc_state->has_pch_encoder)
4684                 intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
4685         else
4686                 intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
4687 }
4688
4689 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
4690 {
4691         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4692         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4693         enum pipe pipe = crtc->pipe;
4694         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
4695         const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
4696         u32 crtc_vtotal, crtc_vblank_end;
4697         int vsyncshift = 0;
4698
4699         /* We need to be careful not to changed the adjusted mode, for otherwise
4700          * the hw state checker will get angry at the mismatch. */
4701         crtc_vtotal = adjusted_mode->crtc_vtotal;
4702         crtc_vblank_end = adjusted_mode->crtc_vblank_end;
4703
4704         if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4705                 /* the chip adds 2 halflines automatically */
4706                 crtc_vtotal -= 1;
4707                 crtc_vblank_end -= 1;
4708
4709                 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
4710                         vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
4711                 else
4712                         vsyncshift = adjusted_mode->crtc_hsync_start -
4713                                 adjusted_mode->crtc_htotal / 2;
4714                 if (vsyncshift < 0)
4715                         vsyncshift += adjusted_mode->crtc_htotal;
4716         }
4717
4718         if (DISPLAY_VER(dev_priv) > 3)
4719                 intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
4720                                vsyncshift);
4721
4722         intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
4723                        (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
4724         intel_de_write(dev_priv, HBLANK(cpu_transcoder),
4725                        (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
4726         intel_de_write(dev_priv, HSYNC(cpu_transcoder),
4727                        (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
4728
4729         intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
4730                        (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
4731         intel_de_write(dev_priv, VBLANK(cpu_transcoder),
4732                        (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
4733         intel_de_write(dev_priv, VSYNC(cpu_transcoder),
4734                        (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
4735
4736         /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4737          * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4738          * documented on the DDI_FUNC_CTL register description, EDP Input Select
4739          * bits. */
4740         if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
4741             (pipe == PIPE_B || pipe == PIPE_C))
4742                 intel_de_write(dev_priv, VTOTAL(pipe),
4743                                intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
4744
4745 }
4746
4747 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
4748 {
4749         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4750         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4751         enum pipe pipe = crtc->pipe;
4752
4753         /* pipesrc controls the size that is scaled from, which should
4754          * always be the user's requested size.
4755          */
4756         intel_de_write(dev_priv, PIPESRC(pipe),
4757                        ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
4758 }
4759
4760 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
4761 {
4762         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4763         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
4764
4765         if (DISPLAY_VER(dev_priv) == 2)
4766                 return false;
4767
4768         if (DISPLAY_VER(dev_priv) >= 9 ||
4769             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
4770                 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
4771         else
4772                 return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
4773 }
4774
4775 static void intel_get_transcoder_timings(struct intel_crtc *crtc,
4776                                          struct intel_crtc_state *pipe_config)
4777 {
4778         struct drm_device *dev = crtc->base.dev;
4779         struct drm_i915_private *dev_priv = to_i915(dev);
4780         enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4781         u32 tmp;
4782
4783         tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
4784         pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
4785         pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
4786
4787         if (!transcoder_is_dsi(cpu_transcoder)) {
4788                 tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
4789                 pipe_config->hw.adjusted_mode.crtc_hblank_start =
4790                                                         (tmp & 0xffff) + 1;
4791                 pipe_config->hw.adjusted_mode.crtc_hblank_end =
4792                                                 ((tmp >> 16) & 0xffff) + 1;
4793         }
4794         tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
4795         pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
4796         pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
4797
4798         tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
4799         pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
4800         pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
4801
4802         if (!transcoder_is_dsi(cpu_transcoder)) {
4803                 tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
4804                 pipe_config->hw.adjusted_mode.crtc_vblank_start =
4805                                                         (tmp & 0xffff) + 1;
4806                 pipe_config->hw.adjusted_mode.crtc_vblank_end =
4807                                                 ((tmp >> 16) & 0xffff) + 1;
4808         }
4809         tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
4810         pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
4811         pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
4812
4813         if (intel_pipe_is_interlaced(pipe_config)) {
4814                 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
4815                 pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
4816                 pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
4817         }
4818 }
4819
4820 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
4821                                     struct intel_crtc_state *pipe_config)
4822 {
4823         struct drm_device *dev = crtc->base.dev;
4824         struct drm_i915_private *dev_priv = to_i915(dev);
4825         u32 tmp;
4826
4827         tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
4828         pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
4829         pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
4830 }
4831
4832 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
4833 {
4834         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4835         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4836         u32 pipeconf;
4837
4838         pipeconf = 0;
4839
4840         /* we keep both pipes enabled on 830 */
4841         if (IS_I830(dev_priv))
4842                 pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
4843
4844         if (crtc_state->double_wide)
4845                 pipeconf |= PIPECONF_DOUBLE_WIDE;
4846
4847         /* only g4x and later have fancy bpc/dither controls */
4848         if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
4849             IS_CHERRYVIEW(dev_priv)) {
4850                 /* Bspec claims that we can't use dithering for 30bpp pipes. */
4851                 if (crtc_state->dither && crtc_state->pipe_bpp != 30)
4852                         pipeconf |= PIPECONF_DITHER_EN |
4853                                     PIPECONF_DITHER_TYPE_SP;
4854
4855                 switch (crtc_state->pipe_bpp) {
4856                 case 18:
4857                         pipeconf |= PIPECONF_6BPC;
4858                         break;
4859                 case 24:
4860                         pipeconf |= PIPECONF_8BPC;
4861                         break;
4862                 case 30:
4863                         pipeconf |= PIPECONF_10BPC;
4864                         break;
4865                 default:
4866                         /* Case prevented by intel_choose_pipe_bpp_dither. */
4867                         BUG();
4868                 }
4869         }
4870
4871         if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
4872                 if (DISPLAY_VER(dev_priv) < 4 ||
4873                     intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
4874                         pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4875                 else
4876                         pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
4877         } else {
4878                 pipeconf |= PIPECONF_PROGRESSIVE;
4879         }
4880
4881         if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
4882              crtc_state->limited_color_range)
4883                 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4884
4885         pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
4886
4887         pipeconf |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
4888
4889         intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
4890         intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
4891 }
4892
4893 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
4894 {
4895         if (IS_I830(dev_priv))
4896                 return false;
4897
4898         return DISPLAY_VER(dev_priv) >= 4 ||
4899                 IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
4900 }
4901
4902 static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
4903 {
4904         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4905         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4906         u32 tmp;
4907
4908         if (!i9xx_has_pfit(dev_priv))
4909                 return;
4910
4911         tmp = intel_de_read(dev_priv, PFIT_CONTROL);
4912         if (!(tmp & PFIT_ENABLE))
4913                 return;
4914
4915         /* Check whether the pfit is attached to our pipe. */
4916         if (DISPLAY_VER(dev_priv) < 4) {
4917                 if (crtc->pipe != PIPE_B)
4918                         return;
4919         } else {
4920                 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4921                         return;
4922         }
4923
4924         crtc_state->gmch_pfit.control = tmp;
4925         crtc_state->gmch_pfit.pgm_ratios =
4926                 intel_de_read(dev_priv, PFIT_PGM_RATIOS);
4927 }
4928
4929 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
4930                                struct intel_crtc_state *pipe_config)
4931 {
4932         struct drm_device *dev = crtc->base.dev;
4933         struct drm_i915_private *dev_priv = to_i915(dev);
4934         enum pipe pipe = crtc->pipe;
4935         struct dpll clock;
4936         u32 mdiv;
4937         int refclk = 100000;
4938
4939         /* In case of DSI, DPLL will not be used */
4940         if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
4941                 return;
4942
4943         vlv_dpio_get(dev_priv);
4944         mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
4945         vlv_dpio_put(dev_priv);
4946
4947         clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
4948         clock.m2 = mdiv & DPIO_M2DIV_MASK;
4949         clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
4950         clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
4951         clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
4952
4953         pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
4954 }
4955
4956 static void chv_crtc_clock_get(struct intel_crtc *crtc,
4957                                struct intel_crtc_state *pipe_config)
4958 {
4959         struct drm_device *dev = crtc->base.dev;
4960         struct drm_i915_private *dev_priv = to_i915(dev);
4961         enum pipe pipe = crtc->pipe;
4962         enum dpio_channel port = vlv_pipe_to_channel(pipe);
4963         struct dpll clock;
4964         u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
4965         int refclk = 100000;
4966
4967         /* In case of DSI, DPLL will not be used */
4968         if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
4969                 return;
4970
4971         vlv_dpio_get(dev_priv);
4972         cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
4973         pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
4974         pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
4975         pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
4976         pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
4977         vlv_dpio_put(dev_priv);
4978
4979         clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
4980         clock.m2 = (pll_dw0 & 0xff) << 22;
4981         if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
4982                 clock.m2 |= pll_dw2 & 0x3fffff;
4983         clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
4984         clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
4985         clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
4986
4987         pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
4988 }
4989
4990 static enum intel_output_format
4991 bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
4992 {
4993         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4994         u32 tmp;
4995
4996         tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
4997
4998         if (tmp & PIPEMISC_YUV420_ENABLE) {
4999                 /* We support 4:2:0 in full blend mode only */
5000                 drm_WARN_ON(&dev_priv->drm,
5001                             (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
5002
5003                 return INTEL_OUTPUT_FORMAT_YCBCR420;
5004         } else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
5005                 return INTEL_OUTPUT_FORMAT_YCBCR444;
5006         } else {
5007                 return INTEL_OUTPUT_FORMAT_RGB;
5008         }
5009 }
5010
5011 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
5012 {
5013         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5014         struct intel_plane *plane = to_intel_plane(crtc->base.primary);
5015         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5016         enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
5017         u32 tmp;
5018
5019         tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
5020
5021         if (tmp & DISPPLANE_GAMMA_ENABLE)
5022                 crtc_state->gamma_enable = true;
5023
5024         if (!HAS_GMCH(dev_priv) &&
5025             tmp & DISPPLANE_PIPE_CSC_ENABLE)
5026                 crtc_state->csc_enable = true;
5027 }
5028
5029 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5030                                  struct intel_crtc_state *pipe_config)
5031 {
5032         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5033         enum intel_display_power_domain power_domain;
5034         intel_wakeref_t wakeref;
5035         u32 tmp;
5036         bool ret;
5037
5038         power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
5039         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
5040         if (!wakeref)
5041                 return false;
5042
5043         pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
5044         pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
5045         pipe_config->shared_dpll = NULL;
5046
5047         ret = false;
5048
5049         tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
5050         if (!(tmp & PIPECONF_ENABLE))
5051                 goto out;
5052
5053         if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
5054             IS_CHERRYVIEW(dev_priv)) {
5055                 switch (tmp & PIPECONF_BPC_MASK) {
5056                 case PIPECONF_6BPC:
5057                         pipe_config->pipe_bpp = 18;
5058                         break;
5059                 case PIPECONF_8BPC:
5060                         pipe_config->pipe_bpp = 24;
5061                         break;
5062                 case PIPECONF_10BPC:
5063                         pipe_config->pipe_bpp = 30;
5064                         break;
5065                 default:
5066                         break;
5067                 }
5068         }
5069
5070         if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
5071             (tmp & PIPECONF_COLOR_RANGE_SELECT))
5072                 pipe_config->limited_color_range = true;
5073
5074         pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
5075                 PIPECONF_GAMMA_MODE_SHIFT;
5076
5077         if (IS_CHERRYVIEW(dev_priv))
5078                 pipe_config->cgm_mode = intel_de_read(dev_priv,
5079                                                       CGM_PIPE_MODE(crtc->pipe));
5080
5081         i9xx_get_pipe_color_config(pipe_config);
5082         intel_color_get_config(pipe_config);
5083
5084         if (DISPLAY_VER(dev_priv) < 4)
5085                 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5086
5087         intel_get_transcoder_timings(crtc, pipe_config);
5088         intel_get_pipe_src_size(crtc, pipe_config);
5089
5090         i9xx_get_pfit_config(pipe_config);
5091
5092         if (DISPLAY_VER(dev_priv) >= 4) {
5093                 /* No way to read it out on pipes B and C */
5094                 if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
5095                         tmp = dev_priv->chv_dpll_md[crtc->pipe];
5096                 else
5097                         tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
5098                 pipe_config->pixel_multiplier =
5099                         ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5100                          >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
5101                 pipe_config->dpll_hw_state.dpll_md = tmp;
5102         } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5103                    IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
5104                 tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
5105                 pipe_config->pixel_multiplier =
5106                         ((tmp & SDVO_MULTIPLIER_MASK)
5107                          >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5108         } else {
5109                 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5110                  * port and will be fixed up in the encoder->get_config
5111                  * function. */
5112                 pipe_config->pixel_multiplier = 1;
5113         }
5114         pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
5115                                                         DPLL(crtc->pipe));
5116         if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
5117                 pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
5118                                                                FP0(crtc->pipe));
5119                 pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
5120                                                                FP1(crtc->pipe));
5121         } else {
5122                 /* Mask out read-only status bits. */
5123                 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5124                                                      DPLL_PORTC_READY_MASK |
5125                                                      DPLL_PORTB_READY_MASK);
5126         }
5127
5128         if (IS_CHERRYVIEW(dev_priv))
5129                 chv_crtc_clock_get(crtc, pipe_config);
5130         else if (IS_VALLEYVIEW(dev_priv))
5131                 vlv_crtc_clock_get(crtc, pipe_config);
5132         else
5133                 i9xx_crtc_clock_get(crtc, pipe_config);
5134
5135         /*
5136          * Normally the dotclock is filled in by the encoder .get_config()
5137          * but in case the pipe is enabled w/o any ports we need a sane
5138          * default.
5139          */
5140         pipe_config->hw.adjusted_mode.crtc_clock =
5141                 pipe_config->port_clock / pipe_config->pixel_multiplier;
5142
5143         ret = true;
5144
5145 out:
5146         intel_display_power_put(dev_priv, power_domain, wakeref);
5147
5148         return ret;
5149 }
5150
5151 static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
5152 {
5153         struct intel_encoder *encoder;
5154         int i;
5155         u32 val, final;
5156         bool has_lvds = false;
5157         bool has_cpu_edp = false;
5158         bool has_panel = false;
5159         bool has_ck505 = false;
5160         bool can_ssc = false;
5161         bool using_ssc_source = false;
5162
5163         /* We need to take the global config into account */
5164         for_each_intel_encoder(&dev_priv->drm, encoder) {
5165                 switch (encoder->type) {
5166                 case INTEL_OUTPUT_LVDS:
5167                         has_panel = true;
5168                         has_lvds = true;
5169                         break;
5170                 case INTEL_OUTPUT_EDP:
5171                         has_panel = true;
5172                         if (encoder->port == PORT_A)
5173                                 has_cpu_edp = true;
5174                         break;
5175                 default:
5176                         break;
5177                 }
5178         }
5179
5180         if (HAS_PCH_IBX(dev_priv)) {
5181                 has_ck505 = dev_priv->vbt.display_clock_mode;
5182                 can_ssc = has_ck505;
5183         } else {
5184                 has_ck505 = false;
5185                 can_ssc = true;
5186         }
5187
5188         /* Check if any DPLLs are using the SSC source */
5189         for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
5190                 u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
5191
5192                 if (!(temp & DPLL_VCO_ENABLE))
5193                         continue;
5194
5195                 if ((temp & PLL_REF_INPUT_MASK) ==
5196                     PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5197                         using_ssc_source = true;
5198                         break;
5199                 }
5200         }
5201
5202         drm_dbg_kms(&dev_priv->drm,
5203                     "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
5204                     has_panel, has_lvds, has_ck505, using_ssc_source);
5205
5206         /* Ironlake: try to setup display ref clock before DPLL
5207          * enabling. This is only under driver's control after
5208          * PCH B stepping, previous chipset stepping should be
5209          * ignoring this setting.
5210          */
5211         val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
5212
5213         /* As we must carefully and slowly disable/enable each source in turn,
5214          * compute the final state we want first and check if we need to
5215          * make any changes at all.
5216          */
5217         final = val;
5218         final &= ~DREF_NONSPREAD_SOURCE_MASK;
5219         if (has_ck505)
5220                 final |= DREF_NONSPREAD_CK505_ENABLE;
5221         else
5222                 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5223
5224         final &= ~DREF_SSC_SOURCE_MASK;
5225         final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5226         final &= ~DREF_SSC1_ENABLE;
5227
5228         if (has_panel) {
5229                 final |= DREF_SSC_SOURCE_ENABLE;
5230
5231                 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5232                         final |= DREF_SSC1_ENABLE;
5233
5234                 if (has_cpu_edp) {
5235                         if (intel_panel_use_ssc(dev_priv) && can_ssc)
5236                                 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5237                         else
5238                                 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5239                 } else
5240                         final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5241         } else if (using_ssc_source) {
5242                 final |= DREF_SSC_SOURCE_ENABLE;
5243                 final |= DREF_SSC1_ENABLE;
5244         }
5245
5246         if (final == val)
5247                 return;
5248
5249         /* Always enable nonspread source */
5250         val &= ~DREF_NONSPREAD_SOURCE_MASK;
5251
5252         if (has_ck505)
5253                 val |= DREF_NONSPREAD_CK505_ENABLE;
5254         else
5255                 val |= DREF_NONSPREAD_SOURCE_ENABLE;
5256
5257         if (has_panel) {
5258                 val &= ~DREF_SSC_SOURCE_MASK;
5259                 val |= DREF_SSC_SOURCE_ENABLE;
5260
5261                 /* SSC must be turned on before enabling the CPU output  */
5262                 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5263                         drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
5264                         val |= DREF_SSC1_ENABLE;
5265                 } else
5266                         val &= ~DREF_SSC1_ENABLE;
5267
5268                 /* Get SSC going before enabling the outputs */
5269                 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5270                 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5271                 udelay(200);
5272
5273                 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5274
5275                 /* Enable CPU source on CPU attached eDP */
5276                 if (has_cpu_edp) {
5277                         if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5278                                 drm_dbg_kms(&dev_priv->drm,
5279                                             "Using SSC on eDP\n");
5280                                 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5281                         } else
5282                                 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5283                 } else
5284                         val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5285
5286                 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5287                 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5288                 udelay(200);
5289         } else {
5290                 drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
5291
5292                 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5293
5294                 /* Turn off CPU output */
5295                 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5296
5297                 intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5298                 intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5299                 udelay(200);
5300
5301                 if (!using_ssc_source) {
5302                         drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
5303
5304                         /* Turn off the SSC source */
5305                         val &= ~DREF_SSC_SOURCE_MASK;
5306                         val |= DREF_SSC_SOURCE_DISABLE;
5307
5308                         /* Turn off SSC1 */
5309                         val &= ~DREF_SSC1_ENABLE;
5310
5311                         intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5312                         intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5313                         udelay(200);
5314                 }
5315         }
5316
5317         BUG_ON(val != final);
5318 }
5319
5320 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
5321 {
5322         u32 tmp;
5323
5324         tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
5325         tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5326         intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
5327
5328         if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
5329                         FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5330                 drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
5331
5332         tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
5333         tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5334         intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
5335
5336         if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
5337                          FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5338                 drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
5339 }
5340
5341 /* WaMPhyProgramming:hsw */
5342 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5343 {
5344         u32 tmp;
5345
5346         tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5347         tmp &= ~(0xFF << 24);
5348         tmp |= (0x12 << 24);
5349         intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5350
5351         tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5352         tmp |= (1 << 11);
5353         intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5354
5355         tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5356         tmp |= (1 << 11);
5357         intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5358
5359         tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5360         tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5361         intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5362
5363         tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5364         tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5365         intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5366
5367         tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5368         tmp &= ~(7 << 13);
5369         tmp |= (5 << 13);
5370         intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5371
5372         tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5373         tmp &= ~(7 << 13);
5374         tmp |= (5 << 13);
5375         intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5376
5377         tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5378         tmp &= ~0xFF;
5379         tmp |= 0x1C;
5380         intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5381
5382         tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5383         tmp &= ~0xFF;
5384         tmp |= 0x1C;
5385         intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5386
5387         tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5388         tmp &= ~(0xFF << 16);
5389         tmp |= (0x1C << 16);
5390         intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5391
5392         tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5393         tmp &= ~(0xFF << 16);
5394         tmp |= (0x1C << 16);
5395         intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5396
5397         tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5398         tmp |= (1 << 27);
5399         intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5400
5401         tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5402         tmp |= (1 << 27);
5403         intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5404
5405         tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5406         tmp &= ~(0xF << 28);
5407         tmp |= (4 << 28);
5408         intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5409
5410         tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5411         tmp &= ~(0xF << 28);
5412         tmp |= (4 << 28);
5413         intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5414 }
5415
5416 /* Implements 3 different sequences from BSpec chapter "Display iCLK
5417  * Programming" based on the parameters passed:
5418  * - Sequence to enable CLKOUT_DP
5419  * - Sequence to enable CLKOUT_DP without spread
5420  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5421  */
5422 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
5423                                  bool with_spread, bool with_fdi)
5424 {
5425         u32 reg, tmp;
5426
5427         if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
5428                      "FDI requires downspread\n"))
5429                 with_spread = true;
5430         if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
5431                      with_fdi, "LP PCH doesn't have FDI\n"))
5432                 with_fdi = false;
5433
5434         mutex_lock(&dev_priv->sb_lock);
5435
5436         tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5437         tmp &= ~SBI_SSCCTL_DISABLE;
5438         tmp |= SBI_SSCCTL_PATHALT;
5439         intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5440
5441         udelay(24);
5442
5443         if (with_spread) {
5444                 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5445                 tmp &= ~SBI_SSCCTL_PATHALT;
5446                 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5447
5448                 if (with_fdi) {
5449                         lpt_reset_fdi_mphy(dev_priv);
5450                         lpt_program_fdi_mphy(dev_priv);
5451                 }
5452         }
5453
5454         reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
5455         tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5456         tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5457         intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5458
5459         mutex_unlock(&dev_priv->sb_lock);
5460 }
5461
5462 /* Sequence to disable CLKOUT_DP */
5463 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
5464 {
5465         u32 reg, tmp;
5466
5467         mutex_lock(&dev_priv->sb_lock);
5468
5469         reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
5470         tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5471         tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5472         intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5473
5474         tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5475         if (!(tmp & SBI_SSCCTL_DISABLE)) {
5476                 if (!(tmp & SBI_SSCCTL_PATHALT)) {
5477                         tmp |= SBI_SSCCTL_PATHALT;
5478                         intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5479                         udelay(32);
5480                 }
5481                 tmp |= SBI_SSCCTL_DISABLE;
5482                 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5483         }
5484
5485         mutex_unlock(&dev_priv->sb_lock);
5486 }
5487
5488 #define BEND_IDX(steps) ((50 + (steps)) / 5)
5489
5490 static const u16 sscdivintphase[] = {
5491         [BEND_IDX( 50)] = 0x3B23,
5492         [BEND_IDX( 45)] = 0x3B23,
5493         [BEND_IDX( 40)] = 0x3C23,
5494         [BEND_IDX( 35)] = 0x3C23,
5495         [BEND_IDX( 30)] = 0x3D23,
5496         [BEND_IDX( 25)] = 0x3D23,
5497         [BEND_IDX( 20)] = 0x3E23,
5498         [BEND_IDX( 15)] = 0x3E23,
5499         [BEND_IDX( 10)] = 0x3F23,
5500         [BEND_IDX(  5)] = 0x3F23,
5501         [BEND_IDX(  0)] = 0x0025,
5502         [BEND_IDX( -5)] = 0x0025,
5503         [BEND_IDX(-10)] = 0x0125,
5504         [BEND_IDX(-15)] = 0x0125,
5505         [BEND_IDX(-20)] = 0x0225,
5506         [BEND_IDX(-25)] = 0x0225,
5507         [BEND_IDX(-30)] = 0x0325,
5508         [BEND_IDX(-35)] = 0x0325,
5509         [BEND_IDX(-40)] = 0x0425,
5510         [BEND_IDX(-45)] = 0x0425,
5511         [BEND_IDX(-50)] = 0x0525,
5512 };
5513
5514 /*
5515  * Bend CLKOUT_DP
5516  * steps -50 to 50 inclusive, in steps of 5
5517  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
5518  * change in clock period = -(steps / 10) * 5.787 ps
5519  */
5520 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
5521 {
5522         u32 tmp;
5523         int idx = BEND_IDX(steps);
5524
5525         if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
5526                 return;
5527
5528         if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
5529                 return;
5530
5531         mutex_lock(&dev_priv->sb_lock);
5532
5533         if (steps % 10 != 0)
5534                 tmp = 0xAAAAAAAB;
5535         else
5536                 tmp = 0x00000000;
5537         intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
5538
5539         tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
5540         tmp &= 0xffff0000;
5541         tmp |= sscdivintphase[idx];
5542         intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
5543
5544         mutex_unlock(&dev_priv->sb_lock);
5545 }
5546
5547 #undef BEND_IDX
5548
5549 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
5550 {
5551         u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
5552         u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
5553
5554         if ((ctl & SPLL_PLL_ENABLE) == 0)
5555                 return false;
5556
5557         if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
5558             (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
5559                 return true;
5560
5561         if (IS_BROADWELL(dev_priv) &&
5562             (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
5563                 return true;
5564
5565         return false;
5566 }
5567
5568 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
5569                                enum intel_dpll_id id)
5570 {
5571         u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
5572         u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
5573
5574         if ((ctl & WRPLL_PLL_ENABLE) == 0)
5575                 return false;
5576
5577         if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
5578                 return true;
5579
5580         if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
5581             (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
5582             (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
5583                 return true;
5584
5585         return false;
5586 }
5587
5588 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
5589 {
5590         struct intel_encoder *encoder;
5591         bool has_fdi = false;
5592
5593         for_each_intel_encoder(&dev_priv->drm, encoder) {
5594                 switch (encoder->type) {
5595                 case INTEL_OUTPUT_ANALOG:
5596                         has_fdi = true;
5597                         break;
5598                 default:
5599                         break;
5600                 }
5601         }
5602
5603         /*
5604          * The BIOS may have decided to use the PCH SSC
5605          * reference so we must not disable it until the
5606          * relevant PLLs have stopped relying on it. We'll
5607          * just leave the PCH SSC reference enabled in case
5608          * any active PLL is using it. It will get disabled
5609          * after runtime suspend if we don't have FDI.
5610          *
5611          * TODO: Move the whole reference clock handling
5612          * to the modeset sequence proper so that we can
5613          * actually enable/disable/reconfigure these things
5614          * safely. To do that we need to introduce a real
5615          * clock hierarchy. That would also allow us to do
5616          * clock bending finally.
5617          */
5618         dev_priv->pch_ssc_use = 0;
5619
5620         if (spll_uses_pch_ssc(dev_priv)) {
5621                 drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
5622                 dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
5623         }
5624
5625         if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
5626                 drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
5627                 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
5628         }
5629
5630         if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
5631                 drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
5632                 dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
5633         }
5634
5635         if (dev_priv->pch_ssc_use)
5636                 return;
5637
5638         if (has_fdi) {
5639                 lpt_bend_clkout_dp(dev_priv, 0);
5640                 lpt_enable_clkout_dp(dev_priv, true, true);
5641         } else {
5642                 lpt_disable_clkout_dp(dev_priv);
5643         }
5644 }
5645
5646 /*
5647  * Initialize reference clocks when the driver loads
5648  */
5649 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
5650 {
5651         if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
5652                 ilk_init_pch_refclk(dev_priv);
5653         else if (HAS_PCH_LPT(dev_priv))
5654                 lpt_init_pch_refclk(dev_priv);
5655 }
5656
5657 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
5658 {
5659         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5660         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5661         enum pipe pipe = crtc->pipe;
5662         u32 val;
5663
5664         val = 0;
5665
5666         switch (crtc_state->pipe_bpp) {
5667         case 18:
5668                 val |= PIPECONF_6BPC;
5669                 break;
5670         case 24:
5671                 val |= PIPECONF_8BPC;
5672                 break;
5673         case 30:
5674                 val |= PIPECONF_10BPC;
5675                 break;
5676         case 36:
5677                 val |= PIPECONF_12BPC;
5678                 break;
5679         default:
5680                 /* Case prevented by intel_choose_pipe_bpp_dither. */
5681                 BUG();
5682         }
5683
5684         if (crtc_state->dither)
5685                 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5686
5687         if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5688                 val |= PIPECONF_INTERLACED_ILK;
5689         else
5690                 val |= PIPECONF_PROGRESSIVE;
5691
5692         /*
5693          * This would end up with an odd purple hue over
5694          * the entire display. Make sure we don't do it.
5695          */
5696         drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
5697                     crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
5698
5699         if (crtc_state->limited_color_range &&
5700             !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
5701                 val |= PIPECONF_COLOR_RANGE_SELECT;
5702
5703         if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
5704                 val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
5705
5706         val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
5707
5708         val |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
5709
5710         intel_de_write(dev_priv, PIPECONF(pipe), val);
5711         intel_de_posting_read(dev_priv, PIPECONF(pipe));
5712 }
5713
5714 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
5715 {
5716         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5717         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5718         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5719         u32 val = 0;
5720
5721         if (IS_HASWELL(dev_priv) && crtc_state->dither)
5722                 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5723
5724         if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5725                 val |= PIPECONF_INTERLACED_ILK;
5726         else
5727                 val |= PIPECONF_PROGRESSIVE;
5728
5729         if (IS_HASWELL(dev_priv) &&
5730             crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
5731                 val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
5732
5733         intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
5734         intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
5735 }
5736
5737 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
5738 {
5739         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5740         const struct intel_crtc_scaler_state *scaler_state =
5741                 &crtc_state->scaler_state;
5742
5743         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5744         u32 val = 0;
5745         int i;
5746
5747         switch (crtc_state->pipe_bpp) {
5748         case 18:
5749                 val |= PIPEMISC_DITHER_6_BPC;
5750                 break;
5751         case 24:
5752                 val |= PIPEMISC_DITHER_8_BPC;
5753                 break;
5754         case 30:
5755                 val |= PIPEMISC_DITHER_10_BPC;
5756                 break;
5757         case 36:
5758                 val |= PIPEMISC_DITHER_12_BPC;
5759                 break;
5760         default:
5761                 MISSING_CASE(crtc_state->pipe_bpp);
5762                 break;
5763         }
5764
5765         if (crtc_state->dither)
5766                 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
5767
5768         if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
5769             crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
5770                 val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
5771
5772         if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
5773                 val |= PIPEMISC_YUV420_ENABLE |
5774                         PIPEMISC_YUV420_MODE_FULL_BLEND;
5775
5776         if (DISPLAY_VER(dev_priv) >= 11 &&
5777             (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
5778                                            BIT(PLANE_CURSOR))) == 0)
5779                 val |= PIPEMISC_HDR_MODE_PRECISION;
5780
5781         if (DISPLAY_VER(dev_priv) >= 12)
5782                 val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
5783
5784         if (IS_ALDERLAKE_P(dev_priv)) {
5785                 bool scaler_in_use = false;
5786
5787                 for (i = 0; i < crtc->num_scalers; i++) {
5788                         if (!scaler_state->scalers[i].in_use)
5789                                 continue;
5790
5791                         scaler_in_use = true;
5792                         break;
5793                 }
5794
5795                 intel_de_rmw(dev_priv, PIPE_MISC2(crtc->pipe),
5796                              PIPE_MISC2_UNDERRUN_BUBBLE_COUNTER_MASK,
5797                              scaler_in_use ? PIPE_MISC2_BUBBLE_COUNTER_SCALER_EN :
5798                              PIPE_MISC2_BUBBLE_COUNTER_SCALER_DIS);
5799         }
5800
5801         intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
5802 }
5803
5804 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
5805 {
5806         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5807         u32 tmp;
5808
5809         tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
5810
5811         switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
5812         case PIPEMISC_DITHER_6_BPC:
5813                 return 18;
5814         case PIPEMISC_DITHER_8_BPC:
5815                 return 24;
5816         case PIPEMISC_DITHER_10_BPC:
5817                 return 30;
5818         case PIPEMISC_DITHER_12_BPC:
5819                 return 36;
5820         default:
5821                 MISSING_CASE(tmp);
5822                 return 0;
5823         }
5824 }
5825
5826 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
5827 {
5828         /*
5829          * Account for spread spectrum to avoid
5830          * oversubscribing the link. Max center spread
5831          * is 2.5%; use 5% for safety's sake.
5832          */
5833         u32 bps = target_clock * bpp * 21 / 20;
5834         return DIV_ROUND_UP(bps, link_bw * 8);
5835 }
5836
5837 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
5838                                          struct intel_link_m_n *m_n)
5839 {
5840         struct drm_device *dev = crtc->base.dev;
5841         struct drm_i915_private *dev_priv = to_i915(dev);
5842         enum pipe pipe = crtc->pipe;
5843
5844         m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
5845         m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
5846         m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
5847                 & ~TU_SIZE_MASK;
5848         m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
5849         m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
5850                     & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5851 }
5852
5853 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
5854                                          enum transcoder transcoder,
5855                                          struct intel_link_m_n *m_n,
5856                                          struct intel_link_m_n *m2_n2)
5857 {
5858         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5859         enum pipe pipe = crtc->pipe;
5860
5861         if (DISPLAY_VER(dev_priv) >= 5) {
5862                 m_n->link_m = intel_de_read(dev_priv,
5863                                             PIPE_LINK_M1(transcoder));
5864                 m_n->link_n = intel_de_read(dev_priv,
5865                                             PIPE_LINK_N1(transcoder));
5866                 m_n->gmch_m = intel_de_read(dev_priv,
5867                                             PIPE_DATA_M1(transcoder))
5868                         & ~TU_SIZE_MASK;
5869                 m_n->gmch_n = intel_de_read(dev_priv,
5870                                             PIPE_DATA_N1(transcoder));
5871                 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
5872                             & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5873
5874                 if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
5875                         m2_n2->link_m = intel_de_read(dev_priv,
5876                                                       PIPE_LINK_M2(transcoder));
5877                         m2_n2->link_n = intel_de_read(dev_priv,
5878                                                              PIPE_LINK_N2(transcoder));
5879                         m2_n2->gmch_m = intel_de_read(dev_priv,
5880                                                              PIPE_DATA_M2(transcoder))
5881                                         & ~TU_SIZE_MASK;
5882                         m2_n2->gmch_n = intel_de_read(dev_priv,
5883                                                              PIPE_DATA_N2(transcoder));
5884                         m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
5885                                         & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5886                 }
5887         } else {
5888                 m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
5889                 m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
5890                 m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
5891                         & ~TU_SIZE_MASK;
5892                 m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
5893                 m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
5894                             & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
5895         }
5896 }
5897
5898 void intel_dp_get_m_n(struct intel_crtc *crtc,
5899                       struct intel_crtc_state *pipe_config)
5900 {
5901         if (pipe_config->has_pch_encoder)
5902                 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
5903         else
5904                 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
5905                                              &pipe_config->dp_m_n,
5906                                              &pipe_config->dp_m2_n2);
5907 }
5908
5909 static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
5910                                    struct intel_crtc_state *pipe_config)
5911 {
5912         intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
5913                                      &pipe_config->fdi_m_n, NULL);
5914 }
5915
5916 static void ilk_get_pfit_pos_size(struct intel_crtc_state *crtc_state,
5917                                   u32 pos, u32 size)
5918 {
5919         drm_rect_init(&crtc_state->pch_pfit.dst,
5920                       pos >> 16, pos & 0xffff,
5921                       size >> 16, size & 0xffff);
5922 }
5923
5924 static void skl_get_pfit_config(struct intel_crtc_state *crtc_state)
5925 {
5926         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5927         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5928         struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
5929         int id = -1;
5930         int i;
5931
5932         /* find scaler attached to this pipe */
5933         for (i = 0; i < crtc->num_scalers; i++) {
5934                 u32 ctl, pos, size;
5935
5936                 ctl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
5937                 if ((ctl & (PS_SCALER_EN | PS_PLANE_SEL_MASK)) != PS_SCALER_EN)
5938                         continue;
5939
5940                 id = i;
5941                 crtc_state->pch_pfit.enabled = true;
5942
5943                 pos = intel_de_read(dev_priv, SKL_PS_WIN_POS(crtc->pipe, i));
5944                 size = intel_de_read(dev_priv, SKL_PS_WIN_SZ(crtc->pipe, i));
5945
5946                 ilk_get_pfit_pos_size(crtc_state, pos, size);
5947
5948                 scaler_state->scalers[i].in_use = true;
5949                 break;
5950         }
5951
5952         scaler_state->scaler_id = id;
5953         if (id >= 0)
5954                 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
5955         else
5956                 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
5957 }
5958
5959 static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
5960 {
5961         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5962         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5963         u32 ctl, pos, size;
5964
5965         ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
5966         if ((ctl & PF_ENABLE) == 0)
5967                 return;
5968
5969         crtc_state->pch_pfit.enabled = true;
5970
5971         pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
5972         size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
5973
5974         ilk_get_pfit_pos_size(crtc_state, pos, size);
5975
5976         /*
5977          * We currently do not free assignements of panel fitters on
5978          * ivb/hsw (since we don't use the higher upscaling modes which
5979          * differentiates them) so just WARN about this case for now.
5980          */
5981         drm_WARN_ON(&dev_priv->drm, DISPLAY_VER(dev_priv) == 7 &&
5982                     (ctl & PF_PIPE_SEL_MASK_IVB) != PF_PIPE_SEL_IVB(crtc->pipe));
5983 }
5984
5985 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
5986                                 struct intel_crtc_state *pipe_config)
5987 {
5988         struct drm_device *dev = crtc->base.dev;
5989         struct drm_i915_private *dev_priv = to_i915(dev);
5990         enum intel_display_power_domain power_domain;
5991         intel_wakeref_t wakeref;
5992         u32 tmp;
5993         bool ret;
5994
5995         power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
5996         wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
5997         if (!wakeref)
5998                 return false;
5999
6000         pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6001         pipe_config->shared_dpll = NULL;
6002
6003         ret = false;
6004         tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
6005         if (!(tmp & PIPECONF_ENABLE))
6006                 goto out;
6007
6008         switch (tmp & PIPECONF_BPC_MASK) {
6009         case PIPECONF_6BPC:
6010                 pipe_config->pipe_bpp = 18;
6011                 break;
6012         case PIPECONF_8BPC:
6013                 pipe_config->pipe_bpp = 24;
6014                 break;
6015         case PIPECONF_10BPC:
6016                 pipe_config->pipe_bpp = 30;
6017                 break;
6018         case PIPECONF_12BPC:
6019                 pipe_config->pipe_bpp = 36;
6020                 break;
6021         default:
6022                 break;
6023         }
6024
6025         if (tmp & PIPECONF_COLOR_RANGE_SELECT)
6026                 pipe_config->limited_color_range = true;
6027
6028         switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
6029         case PIPECONF_OUTPUT_COLORSPACE_YUV601:
6030         case PIPECONF_OUTPUT_COLORSPACE_YUV709:
6031                 pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
6032                 break;
6033         default:
6034                 pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
6035                 break;
6036         }
6037
6038         pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
6039                 PIPECONF_GAMMA_MODE_SHIFT;
6040
6041         pipe_config->csc_mode = intel_de_read(dev_priv,
6042                                               PIPE_CSC_MODE(crtc->pipe));
6043
6044         i9xx_get_pipe_color_config(pipe_config);
6045         intel_color_get_config(pipe_config);
6046
6047         if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
6048                 struct intel_shared_dpll *pll;
6049                 enum intel_dpll_id pll_id;
6050                 bool pll_active;
6051
6052                 pipe_config->has_pch_encoder = true;
6053
6054                 tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
6055                 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6056                                           FDI_DP_PORT_WIDTH_SHIFT) + 1;
6057
6058                 ilk_get_fdi_m_n_config(crtc, pipe_config);
6059
6060                 if (HAS_PCH_IBX(dev_priv)) {
6061                         /*
6062                          * The pipe->pch transcoder and pch transcoder->pll
6063                          * mapping is fixed.
6064                          */
6065                         pll_id = (enum intel_dpll_id) crtc->pipe;
6066                 } else {
6067                         tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
6068                         if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6069                                 pll_id = DPLL_ID_PCH_PLL_B;
6070                         else
6071                                 pll_id= DPLL_ID_PCH_PLL_A;
6072                 }
6073
6074                 pipe_config->shared_dpll =
6075                         intel_get_shared_dpll_by_id(dev_priv, pll_id);
6076                 pll = pipe_config->shared_dpll;
6077
6078                 pll_active = intel_dpll_get_hw_state(dev_priv, pll,
6079                                                      &pipe_config->dpll_hw_state);
6080                 drm_WARN_ON(dev, !pll_active);
6081
6082                 tmp = pipe_config->dpll_hw_state.dpll;
6083                 pipe_config->pixel_multiplier =
6084                         ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6085                          >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
6086
6087                 ilk_pch_clock_get(crtc, pipe_config);
6088         } else {
6089                 pipe_config->pixel_multiplier = 1;
6090         }
6091
6092         intel_get_transcoder_timings(crtc, pipe_config);
6093         intel_get_pipe_src_size(crtc, pipe_config);
6094
6095         ilk_get_pfit_config(pipe_config);
6096
6097         ret = true;
6098
6099 out:
6100         intel_display_power_put(dev_priv, power_domain, wakeref);
6101
6102         return ret;
6103 }
6104
6105 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
6106                                      struct intel_crtc_state *pipe_config,
6107                                      struct intel_display_power_domain_set *power_domain_set)
6108 {
6109         struct drm_device *dev = crtc->base.dev;
6110         struct drm_i915_private *dev_priv = to_i915(dev);
6111         unsigned long panel_transcoder_mask = BIT(TRANSCODER_EDP);
6112         unsigned long enabled_panel_transcoders = 0;
6113         enum transcoder panel_transcoder;
6114         u32 tmp;
6115
6116         if (DISPLAY_VER(dev_priv) >= 11)
6117                 panel_transcoder_mask |=
6118                         BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
6119
6120         /*
6121          * The pipe->transcoder mapping is fixed with the exception of the eDP
6122          * and DSI transcoders handled below.
6123          */
6124         pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6125
6126         /*
6127          * XXX: Do intel_display_power_get_if_enabled before reading this (for
6128          * consistency and less surprising code; it's in always on power).
6129          */
6130         for_each_cpu_transcoder_masked(dev_priv, panel_transcoder,
6131                                        panel_transcoder_mask) {
6132                 bool force_thru = false;
6133                 enum pipe trans_pipe;
6134
6135                 tmp = intel_de_read(dev_priv,
6136                                     TRANS_DDI_FUNC_CTL(panel_transcoder));
6137                 if (!(tmp & TRANS_DDI_FUNC_ENABLE))
6138                         continue;
6139
6140                 /*
6141                  * Log all enabled ones, only use the first one.
6142                  *
6143                  * FIXME: This won't work for two separate DSI displays.
6144                  */
6145                 enabled_panel_transcoders |= BIT(panel_transcoder);
6146                 if (enabled_panel_transcoders != BIT(panel_transcoder))
6147                         continue;
6148
6149                 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6150                 default:
6151                         drm_WARN(dev, 1,
6152                                  "unknown pipe linked to transcoder %s\n",
6153                                  transcoder_name(panel_transcoder));
6154                         fallthrough;
6155                 case TRANS_DDI_EDP_INPUT_A_ONOFF:
6156                         force_thru = true;
6157                         fallthrough;
6158                 case TRANS_DDI_EDP_INPUT_A_ON:
6159                         trans_pipe = PIPE_A;
6160                         break;
6161                 case TRANS_DDI_EDP_INPUT_B_ONOFF:
6162                         trans_pipe = PIPE_B;
6163                         break;
6164                 case TRANS_DDI_EDP_INPUT_C_ONOFF:
6165                         trans_pipe = PIPE_C;
6166                         break;
6167                 case TRANS_DDI_EDP_INPUT_D_ONOFF:
6168                         trans_pipe = PIPE_D;
6169                         break;
6170                 }
6171
6172                 if (trans_pipe == crtc->pipe) {
6173                         pipe_config->cpu_transcoder = panel_transcoder;
6174                         pipe_config->pch_pfit.force_thru = force_thru;
6175                 }
6176         }
6177
6178         /*
6179          * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
6180          */
6181         drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
6182                     enabled_panel_transcoders != BIT(TRANSCODER_EDP));
6183
6184         if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
6185                                                        POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6186                 return false;
6187
6188         tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
6189
6190         return tmp & PIPECONF_ENABLE;
6191 }
6192
6193 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
6194                                          struct intel_crtc_state *pipe_config,
6195                                          struct intel_display_power_domain_set *power_domain_set)
6196 {
6197         struct drm_device *dev = crtc->base.dev;
6198         struct drm_i915_private *dev_priv = to_i915(dev);
6199         enum transcoder cpu_transcoder;
6200         enum port port;
6201         u32 tmp;
6202
6203         for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
6204                 if (port == PORT_A)
6205                         cpu_transcoder = TRANSCODER_DSI_A;
6206                 else
6207                         cpu_transcoder = TRANSCODER_DSI_C;
6208
6209                 if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
6210                                                                POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
6211                         continue;
6212
6213                 /*
6214                  * The PLL needs to be enabled with a valid divider
6215                  * configuration, otherwise accessing DSI registers will hang
6216                  * the machine. See BSpec North Display Engine
6217                  * registers/MIPI[BXT]. We can break out here early, since we
6218                  * need the same DSI PLL to be enabled for both DSI ports.
6219                  */
6220                 if (!bxt_dsi_pll_is_enabled(dev_priv))
6221                         break;
6222
6223                 /* XXX: this works for video mode only */
6224                 tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
6225                 if (!(tmp & DPI_ENABLE))
6226                         continue;
6227
6228                 tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
6229                 if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
6230                         continue;
6231
6232                 pipe_config->cpu_transcoder = cpu_transcoder;
6233                 break;
6234         }
6235
6236         return transcoder_is_dsi(pipe_config->cpu_transcoder);
6237 }
6238
6239 static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
6240                                    struct intel_crtc_state *pipe_config)
6241 {
6242         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6243         enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6244         enum port port;
6245         u32 tmp;
6246
6247         if (transcoder_is_dsi(cpu_transcoder)) {
6248                 port = (cpu_transcoder == TRANSCODER_DSI_A) ?
6249                                                 PORT_A : PORT_B;
6250         } else {
6251                 tmp = intel_de_read(dev_priv,
6252                                     TRANS_DDI_FUNC_CTL(cpu_transcoder));
6253                 if (!(tmp & TRANS_DDI_FUNC_ENABLE))
6254                         return;
6255                 if (DISPLAY_VER(dev_priv) >= 12)
6256                         port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
6257                 else
6258                         port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
6259         }
6260
6261         /*
6262          * Haswell has only FDI/PCH transcoder A. It is which is connected to
6263          * DDI E. So just check whether this pipe is wired to DDI E and whether
6264          * the PCH transcoder is on.
6265          */
6266         if (DISPLAY_VER(dev_priv) < 9 &&
6267             (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
6268                 pipe_config->has_pch_encoder = true;
6269
6270                 tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
6271                 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6272                                           FDI_DP_PORT_WIDTH_SHIFT) + 1;
6273
6274                 ilk_get_fdi_m_n_config(crtc, pipe_config);
6275         }
6276 }
6277
6278 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
6279                                 struct intel_crtc_state *pipe_config)
6280 {
6281         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6282         struct intel_display_power_domain_set power_domain_set = { };
6283         bool active;
6284         u32 tmp;
6285
6286         if (!intel_display_power_get_in_set_if_enabled(dev_priv, &power_domain_set,
6287                                                        POWER_DOMAIN_PIPE(crtc->pipe)))
6288                 return false;
6289
6290         pipe_config->shared_dpll = NULL;
6291
6292         active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_set);
6293
6294         if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
6295             bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_set)) {
6296                 drm_WARN_ON(&dev_priv->drm, active);
6297                 active = true;
6298         }
6299
6300         intel_dsc_get_config(pipe_config);
6301         if (DISPLAY_VER(dev_priv) >= 13 && !pipe_config->dsc.compression_enable)
6302                 intel_uncompressed_joiner_get_config(pipe_config);
6303
6304         if (!active) {
6305                 /* bigjoiner slave doesn't enable transcoder */
6306                 if (!pipe_config->bigjoiner_slave)
6307                         goto out;
6308
6309                 active = true;
6310                 pipe_config->pixel_multiplier = 1;
6311
6312                 /* we cannot read out most state, so don't bother.. */
6313                 pipe_config->quirks |= PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE;
6314         } else if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
6315             DISPLAY_VER(dev_priv) >= 11) {
6316                 hsw_get_ddi_port_state(crtc, pipe_config);
6317                 intel_get_transcoder_timings(crtc, pipe_config);
6318         }
6319
6320         if (HAS_VRR(dev_priv) && !transcoder_is_dsi(pipe_config->cpu_transcoder))
6321                 intel_vrr_get_config(crtc, pipe_config);
6322
6323         intel_get_pipe_src_size(crtc, pipe_config);
6324
6325         if (IS_HASWELL(dev_priv)) {
6326                 u32 tmp = intel_de_read(dev_priv,
6327                                         PIPECONF(pipe_config->cpu_transcoder));
6328
6329                 if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
6330                         pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
6331                 else
6332                         pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
6333         } else {
6334                 pipe_config->output_format =
6335                         bdw_get_pipemisc_output_format(crtc);
6336         }
6337
6338         pipe_config->gamma_mode = intel_de_read(dev_priv,
6339                                                 GAMMA_MODE(crtc->pipe));
6340
6341         pipe_config->csc_mode = intel_de_read(dev_priv,
6342                                               PIPE_CSC_MODE(crtc->pipe));
6343
6344         if (DISPLAY_VER(dev_priv) >= 9) {
6345                 tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
6346
6347                 if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
6348                         pipe_config->gamma_enable = true;
6349
6350                 if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
6351                         pipe_config->csc_enable = true;
6352         } else {
6353                 i9xx_get_pipe_color_config(pipe_config);
6354         }
6355
6356         intel_color_get_config(pipe_config);
6357
6358         tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
6359         pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
6360         if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
6361                 pipe_config->ips_linetime =
6362                         REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
6363
6364         if (intel_display_power_get_in_set_if_enabled(dev_priv, &power_domain_set,
6365                                                       POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe))) {
6366                 if (DISPLAY_VER(dev_priv) >= 9)
6367                         skl_get_pfit_config(pipe_config);
6368                 else
6369                         ilk_get_pfit_config(pipe_config);
6370         }
6371
6372         if (hsw_crtc_supports_ips(crtc)) {
6373                 if (IS_HASWELL(dev_priv))
6374                         pipe_config->ips_enabled = intel_de_read(dev_priv,
6375                                                                  IPS_CTL) & IPS_ENABLE;
6376                 else {
6377                         /*
6378                          * We cannot readout IPS state on broadwell, set to
6379                          * true so we can set it to a defined state on first
6380                          * commit.
6381                          */
6382                         pipe_config->ips_enabled = true;
6383                 }
6384         }
6385
6386         if (pipe_config->bigjoiner_slave) {
6387                 /* Cannot be read out as a slave, set to 0. */
6388                 pipe_config->pixel_multiplier = 0;
6389         } else if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
6390             !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
6391                 pipe_config->pixel_multiplier =
6392                         intel_de_read(dev_priv,
6393                                       PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
6394         } else {
6395                 pipe_config->pixel_multiplier = 1;
6396         }
6397
6398 out:
6399         intel_display_power_put_all_in_set(dev_priv, &power_domain_set);
6400
6401         return active;
6402 }
6403
6404 static bool intel_crtc_get_pipe_config(struct intel_crtc_state *crtc_state)
6405 {
6406         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6407         struct drm_i915_private *i915 = to_i915(crtc->base.dev);
6408
6409         if (!i915->display.get_pipe_config(crtc, crtc_state))
6410                 return false;
6411
6412         crtc_state->hw.active = true;
6413
6414         intel_crtc_readout_derived_state(crtc_state);
6415
6416         return true;
6417 }
6418
6419 /* VESA 640x480x72Hz mode to set on the pipe */
6420 static const struct drm_display_mode load_detect_mode = {
6421         DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6422                  704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6423 };
6424
6425 struct drm_framebuffer *
6426 intel_framebuffer_create(struct drm_i915_gem_object *obj,
6427                          struct drm_mode_fb_cmd2 *mode_cmd)
6428 {
6429         struct intel_framebuffer *intel_fb;
6430         int ret;
6431
6432         intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6433         if (!intel_fb)
6434                 return ERR_PTR(-ENOMEM);
6435
6436         ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
6437         if (ret)
6438                 goto err;
6439
6440         return &intel_fb->base;
6441
6442 err:
6443         kfree(intel_fb);
6444         return ERR_PTR(ret);
6445 }
6446
6447 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
6448                                         struct drm_crtc *crtc)
6449 {
6450         struct drm_plane *plane;
6451         struct drm_plane_state *plane_state;
6452         int ret, i;
6453
6454         ret = drm_atomic_add_affected_planes(state, crtc);
6455         if (ret)
6456                 return ret;
6457
6458         for_each_new_plane_in_state(state, plane, plane_state, i) {
6459                 if (plane_state->crtc != crtc)
6460                         continue;
6461
6462                 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
6463                 if (ret)
6464                         return ret;
6465
6466                 drm_atomic_set_fb_for_plane(plane_state, NULL);
6467         }
6468
6469         return 0;
6470 }
6471
6472 int intel_get_load_detect_pipe(struct drm_connector *connector,
6473                                struct intel_load_detect_pipe *old,
6474                                struct drm_modeset_acquire_ctx *ctx)
6475 {
6476         struct intel_crtc *intel_crtc;
6477         struct intel_encoder *intel_encoder =
6478                 intel_attached_encoder(to_intel_connector(connector));
6479         struct drm_crtc *possible_crtc;
6480         struct drm_encoder *encoder = &intel_encoder->base;
6481         struct drm_crtc *crtc = NULL;
6482         struct drm_device *dev = encoder->dev;
6483         struct drm_i915_private *dev_priv = to_i915(dev);
6484         struct drm_mode_config *config = &dev->mode_config;
6485         struct drm_atomic_state *state = NULL, *restore_state = NULL;
6486         struct drm_connector_state *connector_state;
6487         struct intel_crtc_state *crtc_state;
6488         int ret, i = -1;
6489
6490         drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6491                     connector->base.id, connector->name,
6492                     encoder->base.id, encoder->name);
6493
6494         old->restore_state = NULL;
6495
6496         drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
6497
6498         /*
6499          * Algorithm gets a little messy:
6500          *
6501          *   - if the connector already has an assigned crtc, use it (but make
6502          *     sure it's on first)
6503          *
6504          *   - try to find the first unused crtc that can drive this connector,
6505          *     and use that if we find one
6506          */
6507
6508         /* See if we already have a CRTC for this connector */
6509         if (connector->state->crtc) {
6510                 crtc = connector->state->crtc;
6511
6512                 ret = drm_modeset_lock(&crtc->mutex, ctx);
6513                 if (ret)
6514                         goto fail;
6515
6516                 /* Make sure the crtc and connector are running */
6517                 goto found;
6518         }
6519
6520         /* Find an unused one (if possible) */
6521         for_each_crtc(dev, possible_crtc) {
6522                 i++;
6523                 if (!(encoder->possible_crtcs & (1 << i)))
6524                         continue;
6525
6526                 ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
6527                 if (ret)
6528                         goto fail;
6529
6530                 if (possible_crtc->state->enable) {
6531                         drm_modeset_unlock(&possible_crtc->mutex);
6532                         continue;
6533                 }
6534
6535                 crtc = possible_crtc;
6536                 break;
6537         }
6538
6539         /*
6540          * If we didn't find an unused CRTC, don't use any.
6541          */
6542         if (!crtc) {
6543                 drm_dbg_kms(&dev_priv->drm,
6544                             "no pipe available for load-detect\n");
6545                 ret = -ENODEV;
6546                 goto fail;
6547         }
6548
6549 found:
6550         intel_crtc = to_intel_crtc(crtc);
6551
6552         state = drm_atomic_state_alloc(dev);
6553         restore_state = drm_atomic_state_alloc(dev);
6554         if (!state || !restore_state) {
6555                 ret = -ENOMEM;
6556                 goto fail;
6557         }
6558
6559         state->acquire_ctx = ctx;
6560         restore_state->acquire_ctx = ctx;
6561
6562         connector_state = drm_atomic_get_connector_state(state, connector);
6563         if (IS_ERR(connector_state)) {
6564                 ret = PTR_ERR(connector_state);
6565                 goto fail;
6566         }
6567
6568         ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
6569         if (ret)
6570                 goto fail;
6571
6572         crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
6573         if (IS_ERR(crtc_state)) {
6574                 ret = PTR_ERR(crtc_state);
6575                 goto fail;
6576         }
6577
6578         crtc_state->uapi.active = true;
6579
6580         ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
6581                                            &load_detect_mode);
6582         if (ret)
6583                 goto fail;
6584
6585         ret = intel_modeset_disable_planes(state, crtc);
6586         if (ret)
6587                 goto fail;
6588
6589         ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
6590         if (!ret)
6591                 ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
6592         if (!ret)
6593                 ret = drm_atomic_add_affected_planes(restore_state, crtc);
6594         if (ret) {
6595                 drm_dbg_kms(&dev_priv->drm,
6596                             "Failed to create a copy of old state to restore: %i\n",
6597                             ret);
6598                 goto fail;
6599         }
6600
6601         ret = drm_atomic_commit(state);
6602         if (ret) {
6603                 drm_dbg_kms(&dev_priv->drm,
6604                             "failed to set mode on load-detect pipe\n");
6605                 goto fail;
6606         }
6607
6608         old->restore_state = restore_state;
6609         drm_atomic_state_put(state);
6610
6611         /* let the connector get through one full cycle before testing */
6612         intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
6613         return true;
6614
6615 fail:
6616         if (state) {
6617                 drm_atomic_state_put(state);
6618                 state = NULL;
6619         }
6620         if (restore_state) {
6621                 drm_atomic_state_put(restore_state);
6622                 restore_state = NULL;
6623         }
6624
6625         if (ret == -EDEADLK)
6626                 return ret;
6627
6628         return false;
6629 }
6630
6631 void intel_release_load_detect_pipe(struct drm_connector *connector,
6632                                     struct intel_load_detect_pipe *old,
6633                                     struct drm_modeset_acquire_ctx *ctx)
6634 {
6635         struct intel_encoder *intel_encoder =
6636                 intel_attached_encoder(to_intel_connector(connector));
6637         struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
6638         struct drm_encoder *encoder = &intel_encoder->base;
6639         struct drm_atomic_state *state = old->restore_state;
6640         int ret;
6641
6642         drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6643                     connector->base.id, connector->name,
6644                     encoder->base.id, encoder->name);
6645
6646         if (!state)
6647                 return;
6648
6649         ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
6650         if (ret)
6651                 drm_dbg_kms(&i915->drm,
6652                             "Couldn't release load detect pipe: %i\n", ret);
6653         drm_atomic_state_put(state);
6654 }
6655
6656 static int i9xx_pll_refclk(struct drm_device *dev,
6657                            const struct intel_crtc_state *pipe_config)
6658 {
6659         struct drm_i915_private *dev_priv = to_i915(dev);
6660         u32 dpll = pipe_config->dpll_hw_state.dpll;
6661
6662         if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
6663                 return dev_priv->vbt.lvds_ssc_freq;
6664         else if (HAS_PCH_SPLIT(dev_priv))
6665                 return 120000;
6666         else if (DISPLAY_VER(dev_priv) != 2)
6667                 return 96000;
6668         else
6669                 return 48000;
6670 }
6671
6672 /* Returns the clock of the currently programmed mode of the given pipe. */
6673 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
6674                                 struct intel_crtc_state *pipe_config)
6675 {
6676         struct drm_device *dev = crtc->base.dev;
6677         struct drm_i915_private *dev_priv = to_i915(dev);
6678         enum pipe pipe = crtc->pipe;
6679         u32 dpll = pipe_config->dpll_hw_state.dpll;
6680         u32 fp;
6681         struct dpll clock;
6682         int port_clock;
6683         int refclk = i9xx_pll_refclk(dev, pipe_config);
6684
6685         if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
6686                 fp = pipe_config->dpll_hw_state.fp0;
6687         else
6688                 fp = pipe_config->dpll_hw_state.fp1;
6689
6690         clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
6691         if (IS_PINEVIEW(dev_priv)) {
6692                 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6693                 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
6694         } else {
6695                 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6696                 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6697         }
6698
6699         if (DISPLAY_VER(dev_priv) != 2) {
6700                 if (IS_PINEVIEW(dev_priv))
6701                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6702                                 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
6703                 else
6704                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
6705                                DPLL_FPA01_P1_POST_DIV_SHIFT);
6706
6707                 switch (dpll & DPLL_MODE_MASK) {
6708                 case DPLLB_MODE_DAC_SERIAL:
6709                         clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6710                                 5 : 10;
6711                         break;
6712                 case DPLLB_MODE_LVDS:
6713                         clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6714                                 7 : 14;
6715                         break;
6716                 default:
6717                         drm_dbg_kms(&dev_priv->drm,
6718                                     "Unknown DPLL mode %08x in programmed "
6719                                     "mode\n", (int)(dpll & DPLL_MODE_MASK));
6720                         return;
6721                 }
6722
6723                 if (IS_PINEVIEW(dev_priv))
6724                         port_clock = pnv_calc_dpll_params(refclk, &clock);
6725                 else
6726                         port_clock = i9xx_calc_dpll_params(refclk, &clock);
6727         } else {
6728                 u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
6729                                                                  LVDS);
6730                 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
6731
6732                 if (is_lvds) {
6733                         clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6734                                        DPLL_FPA01_P1_POST_DIV_SHIFT);
6735
6736                         if (lvds & LVDS_CLKB_POWER_UP)
6737                                 clock.p2 = 7;
6738                         else
6739                                 clock.p2 = 14;
6740                 } else {
6741                         if (dpll & PLL_P1_DIVIDE_BY_TWO)
6742                                 clock.p1 = 2;
6743                         else {
6744                                 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6745                                             DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6746                         }
6747                         if (dpll & PLL_P2_DIVIDE_BY_4)
6748                                 clock.p2 = 4;
6749                         else
6750                                 clock.p2 = 2;
6751                 }
6752
6753                 port_clock = i9xx_calc_dpll_params(refclk, &clock);
6754         }
6755
6756         /*
6757          * This value includes pixel_multiplier. We will use
6758          * port_clock to compute adjusted_mode.crtc_clock in the
6759          * encoder's get_config() function.
6760          */
6761         pipe_config->port_clock = port_clock;
6762 }
6763
6764 int intel_dotclock_calculate(int link_freq,
6765                              const struct intel_link_m_n *m_n)
6766 {
6767         /*
6768          * The calculation for the data clock is:
6769          * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
6770          * But we want to avoid losing precison if possible, so:
6771          * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
6772          *
6773          * and the link clock is simpler:
6774          * link_clock = (m * link_clock) / n
6775          */
6776
6777         if (!m_n->link_n)
6778                 return 0;
6779
6780         return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
6781 }
6782
6783 static void ilk_pch_clock_get(struct intel_crtc *crtc,
6784                               struct intel_crtc_state *pipe_config)
6785 {
6786         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6787
6788         /* read out port_clock from the DPLL */
6789         i9xx_crtc_clock_get(crtc, pipe_config);
6790
6791         /*
6792          * In case there is an active pipe without active ports,
6793          * we may need some idea for the dotclock anyway.
6794          * Calculate one based on the FDI configuration.
6795          */
6796         pipe_config->hw.adjusted_mode.crtc_clock =
6797                 intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
6798                                          &pipe_config->fdi_m_n);
6799 }
6800
6801 /* Returns the currently programmed mode of the given encoder. */
6802 struct drm_display_mode *
6803 intel_encoder_current_mode(struct intel_encoder *encoder)
6804 {
6805         struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6806         struct intel_crtc_state *crtc_state;
6807         struct drm_display_mode *mode;
6808         struct intel_crtc *crtc;
6809         enum pipe pipe;
6810
6811         if (!encoder->get_hw_state(encoder, &pipe))
6812                 return NULL;
6813
6814         crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
6815
6816         mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6817         if (!mode)
6818                 return NULL;
6819
6820         crtc_state = intel_crtc_state_alloc(crtc);
6821         if (!crtc_state) {
6822                 kfree(mode);
6823                 return NULL;
6824         }
6825
6826         if (!intel_crtc_get_pipe_config(crtc_state)) {
6827                 kfree(crtc_state);
6828                 kfree(mode);
6829                 return NULL;
6830         }
6831
6832         intel_encoder_get_config(encoder, crtc_state);
6833
6834         intel_mode_from_crtc_timings(mode, &crtc_state->hw.adjusted_mode);
6835
6836         kfree(crtc_state);
6837
6838         return mode;
6839 }
6840
6841 /**
6842  * intel_wm_need_update - Check whether watermarks need updating
6843  * @cur: current plane state
6844  * @new: new plane state
6845  *
6846  * Check current plane state versus the new one to determine whether
6847  * watermarks need to be recalculated.
6848  *
6849  * Returns true or false.
6850  */
6851 static bool intel_wm_need_update(const struct intel_plane_state *cur,
6852                                  struct intel_plane_state *new)
6853 {
6854         /* Update watermarks on tiling or size changes. */
6855         if (new->uapi.visible != cur->uapi.visible)
6856                 return true;
6857
6858         if (!cur->hw.fb || !new->hw.fb)
6859                 return false;
6860
6861         if (cur->hw.fb->modifier != new->hw.fb->modifier ||
6862             cur->hw.rotation != new->hw.rotation ||
6863             drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
6864             drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
6865             drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
6866             drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
6867                 return true;
6868
6869         return false;
6870 }
6871
6872 static bool needs_scaling(const struct intel_plane_state *state)
6873 {
6874         int src_w = drm_rect_width(&state->uapi.src) >> 16;
6875         int src_h = drm_rect_height(&state->uapi.src) >> 16;
6876         int dst_w = drm_rect_width(&state->uapi.dst);
6877         int dst_h = drm_rect_height(&state->uapi.dst);
6878
6879         return (src_w != dst_w || src_h != dst_h);
6880 }
6881
6882 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
6883                                     struct intel_crtc_state *crtc_state,
6884                                     const struct intel_plane_state *old_plane_state,
6885                                     struct intel_plane_state *plane_state)
6886 {
6887         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6888         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
6889         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6890         bool mode_changed = intel_crtc_needs_modeset(crtc_state);
6891         bool was_crtc_enabled = old_crtc_state->hw.active;
6892         bool is_crtc_enabled = crtc_state->hw.active;
6893         bool turn_off, turn_on, visible, was_visible;
6894         int ret;
6895
6896         if (DISPLAY_VER(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
6897                 ret = skl_update_scaler_plane(crtc_state, plane_state);
6898                 if (ret)
6899                         return ret;
6900         }
6901
6902         was_visible = old_plane_state->uapi.visible;
6903         visible = plane_state->uapi.visible;
6904
6905         if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
6906                 was_visible = false;
6907
6908         /*
6909          * Visibility is calculated as if the crtc was on, but
6910          * after scaler setup everything depends on it being off
6911          * when the crtc isn't active.
6912          *
6913          * FIXME this is wrong for watermarks. Watermarks should also
6914          * be computed as if the pipe would be active. Perhaps move
6915          * per-plane wm computation to the .check_plane() hook, and
6916          * only combine the results from all planes in the current place?
6917          */
6918         if (!is_crtc_enabled) {
6919                 intel_plane_set_invisible(crtc_state, plane_state);
6920                 visible = false;
6921         }
6922
6923         if (!was_visible && !visible)
6924                 return 0;
6925
6926         turn_off = was_visible && (!visible || mode_changed);
6927         turn_on = visible && (!was_visible || mode_changed);
6928
6929         drm_dbg_atomic(&dev_priv->drm,
6930                        "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
6931                        crtc->base.base.id, crtc->base.name,
6932                        plane->base.base.id, plane->base.name,
6933                        was_visible, visible,
6934                        turn_off, turn_on, mode_changed);
6935
6936         if (turn_on) {
6937                 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
6938                         crtc_state->update_wm_pre = true;
6939
6940                 /* must disable cxsr around plane enable/disable */
6941                 if (plane->id != PLANE_CURSOR)
6942                         crtc_state->disable_cxsr = true;
6943         } else if (turn_off) {
6944                 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
6945                         crtc_state->update_wm_post = true;
6946
6947                 /* must disable cxsr around plane enable/disable */
6948                 if (plane->id != PLANE_CURSOR)
6949                         crtc_state->disable_cxsr = true;
6950         } else if (intel_wm_need_update(old_plane_state, plane_state)) {
6951                 if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv)) {
6952                         /* FIXME bollocks */
6953                         crtc_state->update_wm_pre = true;
6954                         crtc_state->update_wm_post = true;
6955                 }
6956         }
6957
6958         if (visible || was_visible)
6959                 crtc_state->fb_bits |= plane->frontbuffer_bit;
6960
6961         /*
6962          * ILK/SNB DVSACNTR/Sprite Enable
6963          * IVB SPR_CTL/Sprite Enable
6964          * "When in Self Refresh Big FIFO mode, a write to enable the
6965          *  plane will be internally buffered and delayed while Big FIFO
6966          *  mode is exiting."
6967          *
6968          * Which means that enabling the sprite can take an extra frame
6969          * when we start in big FIFO mode (LP1+). Thus we need to drop
6970          * down to LP0 and wait for vblank in order to make sure the
6971          * sprite gets enabled on the next vblank after the register write.
6972          * Doing otherwise would risk enabling the sprite one frame after
6973          * we've already signalled flip completion. We can resume LP1+
6974          * once the sprite has been enabled.
6975          *
6976          *
6977          * WaCxSRDisabledForSpriteScaling:ivb
6978          * IVB SPR_SCALE/Scaling Enable
6979          * "Low Power watermarks must be disabled for at least one
6980          *  frame before enabling sprite scaling, and kept disabled
6981          *  until sprite scaling is disabled."
6982          *
6983          * ILK/SNB DVSASCALE/Scaling Enable
6984          * "When in Self Refresh Big FIFO mode, scaling enable will be
6985          *  masked off while Big FIFO mode is exiting."
6986          *
6987          * Despite the w/a only being listed for IVB we assume that
6988          * the ILK/SNB note has similar ramifications, hence we apply
6989          * the w/a on all three platforms.
6990          *
6991          * With experimental results seems this is needed also for primary
6992          * plane, not only sprite plane.
6993          */
6994         if (plane->id != PLANE_CURSOR &&
6995             (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) ||
6996              IS_IVYBRIDGE(dev_priv)) &&
6997             (turn_on || (!needs_scaling(old_plane_state) &&
6998                          needs_scaling(plane_state))))
6999                 crtc_state->disable_lp_wm = true;
7000
7001         return 0;
7002 }
7003
7004 static bool encoders_cloneable(const struct intel_encoder *a,
7005                                const struct intel_encoder *b)
7006 {
7007         /* masks could be asymmetric, so check both ways */
7008         return a == b || (a->cloneable & (1 << b->type) &&
7009                           b->cloneable & (1 << a->type));
7010 }
7011
7012 static bool check_single_encoder_cloning(struct intel_atomic_state *state,
7013                                          struct intel_crtc *crtc,
7014                                          struct intel_encoder *encoder)
7015 {
7016         struct intel_encoder *source_encoder;
7017         struct drm_connector *connector;
7018         struct drm_connector_state *connector_state;
7019         int i;
7020
7021         for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
7022                 if (connector_state->crtc != &crtc->base)
7023                         continue;
7024
7025                 source_encoder =
7026                         to_intel_encoder(connector_state->best_encoder);
7027                 if (!encoders_cloneable(encoder, source_encoder))
7028                         return false;
7029         }
7030
7031         return true;
7032 }
7033
7034 static int icl_add_linked_planes(struct intel_atomic_state *state)
7035 {
7036         struct intel_plane *plane, *linked;
7037         struct intel_plane_state *plane_state, *linked_plane_state;
7038         int i;
7039
7040         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7041                 linked = plane_state->planar_linked_plane;
7042
7043                 if (!linked)
7044                         continue;
7045
7046                 linked_plane_state = intel_atomic_get_plane_state(state, linked);
7047                 if (IS_ERR(linked_plane_state))
7048                         return PTR_ERR(linked_plane_state);
7049
7050                 drm_WARN_ON(state->base.dev,
7051                             linked_plane_state->planar_linked_plane != plane);
7052                 drm_WARN_ON(state->base.dev,
7053                             linked_plane_state->planar_slave == plane_state->planar_slave);
7054         }
7055
7056         return 0;
7057 }
7058
7059 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
7060 {
7061         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7062         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7063         struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
7064         struct intel_plane *plane, *linked;
7065         struct intel_plane_state *plane_state;
7066         int i;
7067
7068         if (DISPLAY_VER(dev_priv) < 11)
7069                 return 0;
7070
7071         /*
7072          * Destroy all old plane links and make the slave plane invisible
7073          * in the crtc_state->active_planes mask.
7074          */
7075         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7076                 if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
7077                         continue;
7078
7079                 plane_state->planar_linked_plane = NULL;
7080                 if (plane_state->planar_slave && !plane_state->uapi.visible) {
7081                         crtc_state->enabled_planes &= ~BIT(plane->id);
7082                         crtc_state->active_planes &= ~BIT(plane->id);
7083                         crtc_state->update_planes |= BIT(plane->id);
7084                 }
7085
7086                 plane_state->planar_slave = false;
7087         }
7088
7089         if (!crtc_state->nv12_planes)
7090                 return 0;
7091
7092         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7093                 struct intel_plane_state *linked_state = NULL;
7094
7095                 if (plane->pipe != crtc->pipe ||
7096                     !(crtc_state->nv12_planes & BIT(plane->id)))
7097                         continue;
7098
7099                 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
7100                         if (!icl_is_nv12_y_plane(dev_priv, linked->id))
7101                                 continue;
7102
7103                         if (crtc_state->active_planes & BIT(linked->id))
7104                                 continue;
7105
7106                         linked_state = intel_atomic_get_plane_state(state, linked);
7107                         if (IS_ERR(linked_state))
7108                                 return PTR_ERR(linked_state);
7109
7110                         break;
7111                 }
7112
7113                 if (!linked_state) {
7114                         drm_dbg_kms(&dev_priv->drm,
7115                                     "Need %d free Y planes for planar YUV\n",
7116                                     hweight8(crtc_state->nv12_planes));
7117
7118                         return -EINVAL;
7119                 }
7120
7121                 plane_state->planar_linked_plane = linked;
7122
7123                 linked_state->planar_slave = true;
7124                 linked_state->planar_linked_plane = plane;
7125                 crtc_state->enabled_planes |= BIT(linked->id);
7126                 crtc_state->active_planes |= BIT(linked->id);
7127                 crtc_state->update_planes |= BIT(linked->id);
7128                 drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
7129                             linked->base.name, plane->base.name);
7130
7131                 /* Copy parameters to slave plane */
7132                 linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
7133                 linked_state->color_ctl = plane_state->color_ctl;
7134                 linked_state->view = plane_state->view;
7135
7136                 intel_plane_copy_hw_state(linked_state, plane_state);
7137                 linked_state->uapi.src = plane_state->uapi.src;
7138                 linked_state->uapi.dst = plane_state->uapi.dst;
7139
7140                 if (icl_is_hdr_plane(dev_priv, plane->id)) {
7141                         if (linked->id == PLANE_SPRITE5)
7142                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
7143                         else if (linked->id == PLANE_SPRITE4)
7144                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
7145                         else if (linked->id == PLANE_SPRITE3)
7146                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_5_RKL;
7147                         else if (linked->id == PLANE_SPRITE2)
7148                                 plane_state->cus_ctl |= PLANE_CUS_PLANE_4_RKL;
7149                         else
7150                                 MISSING_CASE(linked->id);
7151                 }
7152         }
7153
7154         return 0;
7155 }
7156
7157 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
7158 {
7159         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
7160         struct intel_atomic_state *state =
7161                 to_intel_atomic_state(new_crtc_state->uapi.state);
7162         const struct intel_crtc_state *old_crtc_state =
7163                 intel_atomic_get_old_crtc_state(state, crtc);
7164
7165         return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
7166 }
7167
7168 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
7169 {
7170         const struct drm_display_mode *pipe_mode =
7171                 &crtc_state->hw.pipe_mode;
7172         int linetime_wm;
7173
7174         if (!crtc_state->hw.enable)
7175                 return 0;
7176
7177         linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
7178                                         pipe_mode->crtc_clock);
7179
7180         return min(linetime_wm, 0x1ff);
7181 }
7182
7183 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
7184                                const struct intel_cdclk_state *cdclk_state)
7185 {
7186         const struct drm_display_mode *pipe_mode =
7187                 &crtc_state->hw.pipe_mode;
7188         int linetime_wm;
7189
7190         if (!crtc_state->hw.enable)
7191                 return 0;
7192
7193         linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
7194                                         cdclk_state->logical.cdclk);
7195
7196         return min(linetime_wm, 0x1ff);
7197 }
7198
7199 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
7200 {
7201         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7202         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7203         const struct drm_display_mode *pipe_mode =
7204                 &crtc_state->hw.pipe_mode;
7205         int linetime_wm;
7206
7207         if (!crtc_state->hw.enable)
7208                 return 0;
7209
7210         linetime_wm = DIV_ROUND_UP(pipe_mode->crtc_htotal * 1000 * 8,
7211                                    crtc_state->pixel_rate);
7212
7213         /* Display WA #1135: BXT:ALL GLK:ALL */
7214         if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
7215             dev_priv->ipc_enabled)
7216                 linetime_wm /= 2;
7217
7218         return min(linetime_wm, 0x1ff);
7219 }
7220
7221 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
7222                                    struct intel_crtc *crtc)
7223 {
7224         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7225         struct intel_crtc_state *crtc_state =
7226                 intel_atomic_get_new_crtc_state(state, crtc);
7227         const struct intel_cdclk_state *cdclk_state;
7228
7229         if (DISPLAY_VER(dev_priv) >= 9)
7230                 crtc_state->linetime = skl_linetime_wm(crtc_state);
7231         else
7232                 crtc_state->linetime = hsw_linetime_wm(crtc_state);
7233
7234         if (!hsw_crtc_supports_ips(crtc))
7235                 return 0;
7236
7237         cdclk_state = intel_atomic_get_cdclk_state(state);
7238         if (IS_ERR(cdclk_state))
7239                 return PTR_ERR(cdclk_state);
7240
7241         crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
7242                                                        cdclk_state);
7243
7244         return 0;
7245 }
7246
7247 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
7248                                    struct intel_crtc *crtc)
7249 {
7250         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7251         struct intel_crtc_state *crtc_state =
7252                 intel_atomic_get_new_crtc_state(state, crtc);
7253         bool mode_changed = intel_crtc_needs_modeset(crtc_state);
7254         int ret;
7255
7256         if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv) &&
7257             mode_changed && !crtc_state->hw.active)
7258                 crtc_state->update_wm_post = true;
7259
7260         if (mode_changed && crtc_state->hw.enable &&
7261             dev_priv->display.crtc_compute_clock &&
7262             !crtc_state->bigjoiner_slave &&
7263             !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
7264                 ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
7265                 if (ret)
7266                         return ret;
7267         }
7268
7269         /*
7270          * May need to update pipe gamma enable bits
7271          * when C8 planes are getting enabled/disabled.
7272          */
7273         if (c8_planes_changed(crtc_state))
7274                 crtc_state->uapi.color_mgmt_changed = true;
7275
7276         if (mode_changed || crtc_state->update_pipe ||
7277             crtc_state->uapi.color_mgmt_changed) {
7278                 ret = intel_color_check(crtc_state);
7279                 if (ret)
7280                         return ret;
7281         }
7282
7283         if (dev_priv->display.compute_pipe_wm) {
7284                 ret = dev_priv->display.compute_pipe_wm(crtc_state);
7285                 if (ret) {
7286                         drm_dbg_kms(&dev_priv->drm,
7287                                     "Target pipe watermarks are invalid\n");
7288                         return ret;
7289                 }
7290         }
7291
7292         if (dev_priv->display.compute_intermediate_wm) {
7293                 if (drm_WARN_ON(&dev_priv->drm,
7294                                 !dev_priv->display.compute_pipe_wm))
7295                         return 0;
7296
7297                 /*
7298                  * Calculate 'intermediate' watermarks that satisfy both the
7299                  * old state and the new state.  We can program these
7300                  * immediately.
7301                  */
7302                 ret = dev_priv->display.compute_intermediate_wm(crtc_state);
7303                 if (ret) {
7304                         drm_dbg_kms(&dev_priv->drm,
7305                                     "No valid intermediate pipe watermarks are possible\n");
7306                         return ret;
7307                 }
7308         }
7309
7310         if (DISPLAY_VER(dev_priv) >= 9) {
7311                 if (mode_changed || crtc_state->update_pipe) {
7312                         ret = skl_update_scaler_crtc(crtc_state);
7313                         if (ret)
7314                                 return ret;
7315                 }
7316
7317                 ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
7318                 if (ret)
7319                         return ret;
7320         }
7321
7322         if (HAS_IPS(dev_priv)) {
7323                 ret = hsw_compute_ips_config(crtc_state);
7324                 if (ret)
7325                         return ret;
7326         }
7327
7328         if (DISPLAY_VER(dev_priv) >= 9 ||
7329             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
7330                 ret = hsw_compute_linetime_wm(state, crtc);
7331                 if (ret)
7332                         return ret;
7333
7334         }
7335
7336         if (!mode_changed) {
7337                 ret = intel_psr2_sel_fetch_update(state, crtc);
7338                 if (ret)
7339                         return ret;
7340         }
7341
7342         return 0;
7343 }
7344
7345 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
7346 {
7347         struct intel_connector *connector;
7348         struct drm_connector_list_iter conn_iter;
7349
7350         drm_connector_list_iter_begin(dev, &conn_iter);
7351         for_each_intel_connector_iter(connector, &conn_iter) {
7352                 struct drm_connector_state *conn_state = connector->base.state;
7353                 struct intel_encoder *encoder =
7354                         to_intel_encoder(connector->base.encoder);
7355
7356                 if (conn_state->crtc)
7357                         drm_connector_put(&connector->base);
7358
7359                 if (encoder) {
7360                         struct intel_crtc *crtc =
7361                                 to_intel_crtc(encoder->base.crtc);
7362                         const struct intel_crtc_state *crtc_state =
7363                                 to_intel_crtc_state(crtc->base.state);
7364
7365                         conn_state->best_encoder = &encoder->base;
7366                         conn_state->crtc = &crtc->base;
7367                         conn_state->max_bpc = (crtc_state->pipe_bpp ?: 24) / 3;
7368
7369                         drm_connector_get(&connector->base);
7370                 } else {
7371                         conn_state->best_encoder = NULL;
7372                         conn_state->crtc = NULL;
7373                 }
7374         }
7375         drm_connector_list_iter_end(&conn_iter);
7376 }
7377
7378 static int
7379 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
7380                       struct intel_crtc_state *pipe_config)
7381 {
7382         struct drm_connector *connector = conn_state->connector;
7383         struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
7384         const struct drm_display_info *info = &connector->display_info;
7385         int bpp;
7386
7387         switch (conn_state->max_bpc) {
7388         case 6 ... 7:
7389                 bpp = 6 * 3;
7390                 break;
7391         case 8 ... 9:
7392                 bpp = 8 * 3;
7393                 break;
7394         case 10 ... 11:
7395                 bpp = 10 * 3;
7396                 break;
7397         case 12 ... 16:
7398                 bpp = 12 * 3;
7399                 break;
7400         default:
7401                 MISSING_CASE(conn_state->max_bpc);
7402                 return -EINVAL;
7403         }
7404
7405         if (bpp < pipe_config->pipe_bpp) {
7406                 drm_dbg_kms(&i915->drm,
7407                             "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
7408                             "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
7409                             connector->base.id, connector->name,
7410                             bpp, 3 * info->bpc,
7411                             3 * conn_state->max_requested_bpc,
7412                             pipe_config->pipe_bpp);
7413
7414                 pipe_config->pipe_bpp = bpp;
7415         }
7416
7417         return 0;
7418 }
7419
7420 static int
7421 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7422                           struct intel_crtc_state *pipe_config)
7423 {
7424         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7425         struct drm_atomic_state *state = pipe_config->uapi.state;
7426         struct drm_connector *connector;
7427         struct drm_connector_state *connector_state;
7428         int bpp, i;
7429
7430         if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7431             IS_CHERRYVIEW(dev_priv)))
7432                 bpp = 10*3;
7433         else if (DISPLAY_VER(dev_priv) >= 5)
7434                 bpp = 12*3;
7435         else
7436                 bpp = 8*3;
7437
7438         pipe_config->pipe_bpp = bpp;
7439
7440         /* Clamp display bpp to connector max bpp */
7441         for_each_new_connector_in_state(state, connector, connector_state, i) {
7442                 int ret;
7443
7444                 if (connector_state->crtc != &crtc->base)
7445                         continue;
7446
7447                 ret = compute_sink_pipe_bpp(connector_state, pipe_config);
7448                 if (ret)
7449                         return ret;
7450         }
7451
7452         return 0;
7453 }
7454
7455 static void intel_dump_crtc_timings(struct drm_i915_private *i915,
7456                                     const struct drm_display_mode *mode)
7457 {
7458         drm_dbg_kms(&i915->drm, "crtc timings: %d %d %d %d %d %d %d %d %d, "
7459                     "type: 0x%x flags: 0x%x\n",
7460                     mode->crtc_clock,
7461                     mode->crtc_hdisplay, mode->crtc_hsync_start,
7462                     mode->crtc_hsync_end, mode->crtc_htotal,
7463                     mode->crtc_vdisplay, mode->crtc_vsync_start,
7464                     mode->crtc_vsync_end, mode->crtc_vtotal,
7465                     mode->type, mode->flags);
7466 }
7467
7468 static void
7469 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
7470                       const char *id, unsigned int lane_count,
7471                       const struct intel_link_m_n *m_n)
7472 {
7473         struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
7474
7475         drm_dbg_kms(&i915->drm,
7476                     "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7477                     id, lane_count,
7478                     m_n->gmch_m, m_n->gmch_n,
7479                     m_n->link_m, m_n->link_n, m_n->tu);
7480 }
7481
7482 static void
7483 intel_dump_infoframe(struct drm_i915_private *dev_priv,
7484                      const union hdmi_infoframe *frame)
7485 {
7486         if (!drm_debug_enabled(DRM_UT_KMS))
7487                 return;
7488
7489         hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
7490 }
7491
7492 static void
7493 intel_dump_dp_vsc_sdp(struct drm_i915_private *dev_priv,
7494                       const struct drm_dp_vsc_sdp *vsc)
7495 {
7496         if (!drm_debug_enabled(DRM_UT_KMS))
7497                 return;
7498
7499         drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, vsc);
7500 }
7501
7502 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
7503
7504 static const char * const output_type_str[] = {
7505         OUTPUT_TYPE(UNUSED),
7506         OUTPUT_TYPE(ANALOG),
7507         OUTPUT_TYPE(DVO),
7508         OUTPUT_TYPE(SDVO),
7509         OUTPUT_TYPE(LVDS),
7510         OUTPUT_TYPE(TVOUT),
7511         OUTPUT_TYPE(HDMI),
7512         OUTPUT_TYPE(DP),
7513         OUTPUT_TYPE(EDP),
7514         OUTPUT_TYPE(DSI),
7515         OUTPUT_TYPE(DDI),
7516         OUTPUT_TYPE(DP_MST),
7517 };
7518
7519 #undef OUTPUT_TYPE
7520
7521 static void snprintf_output_types(char *buf, size_t len,
7522                                   unsigned int output_types)
7523 {
7524         char *str = buf;
7525         int i;
7526
7527         str[0] = '\0';
7528
7529         for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
7530                 int r;
7531
7532                 if ((output_types & BIT(i)) == 0)
7533                         continue;
7534
7535                 r = snprintf(str, len, "%s%s",
7536                              str != buf ? "," : "", output_type_str[i]);
7537                 if (r >= len)
7538                         break;
7539                 str += r;
7540                 len -= r;
7541
7542                 output_types &= ~BIT(i);
7543         }
7544
7545         WARN_ON_ONCE(output_types != 0);
7546 }
7547
7548 static const char * const output_format_str[] = {
7549         [INTEL_OUTPUT_FORMAT_RGB] = "RGB",
7550         [INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
7551         [INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
7552 };
7553
7554 static const char *output_formats(enum intel_output_format format)
7555 {
7556         if (format >= ARRAY_SIZE(output_format_str))
7557                 return "invalid";
7558         return output_format_str[format];
7559 }
7560
7561 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
7562 {
7563         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
7564         struct drm_i915_private *i915 = to_i915(plane->base.dev);
7565         const struct drm_framebuffer *fb = plane_state->hw.fb;
7566
7567         if (!fb) {
7568                 drm_dbg_kms(&i915->drm,
7569                             "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
7570                             plane->base.base.id, plane->base.name,
7571                             yesno(plane_state->uapi.visible));
7572                 return;
7573         }
7574
7575         drm_dbg_kms(&i915->drm,
7576                     "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %p4cc modifier = 0x%llx, visible: %s\n",
7577                     plane->base.base.id, plane->base.name,
7578                     fb->base.id, fb->width, fb->height, &fb->format->format,
7579                     fb->modifier, yesno(plane_state->uapi.visible));
7580         drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
7581                     plane_state->hw.rotation, plane_state->scaler_id);
7582         if (plane_state->uapi.visible)
7583                 drm_dbg_kms(&i915->drm,
7584                             "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
7585                             DRM_RECT_FP_ARG(&plane_state->uapi.src),
7586                             DRM_RECT_ARG(&plane_state->uapi.dst));
7587 }
7588
7589 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
7590                                    struct intel_atomic_state *state,
7591                                    const char *context)
7592 {
7593         struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
7594         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7595         const struct intel_plane_state *plane_state;
7596         struct intel_plane *plane;
7597         char buf[64];
7598         int i;
7599
7600         drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
7601                     crtc->base.base.id, crtc->base.name,
7602                     yesno(pipe_config->hw.enable), context);
7603
7604         if (!pipe_config->hw.enable)
7605                 goto dump_planes;
7606
7607         snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
7608         drm_dbg_kms(&dev_priv->drm,
7609                     "active: %s, output_types: %s (0x%x), output format: %s\n",
7610                     yesno(pipe_config->hw.active),
7611                     buf, pipe_config->output_types,
7612                     output_formats(pipe_config->output_format));
7613
7614         drm_dbg_kms(&dev_priv->drm,
7615                     "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
7616                     transcoder_name(pipe_config->cpu_transcoder),
7617                     pipe_config->pipe_bpp, pipe_config->dither);
7618
7619         drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
7620                     transcoder_name(pipe_config->mst_master_transcoder));
7621
7622         drm_dbg_kms(&dev_priv->drm,
7623                     "port sync: master transcoder: %s, slave transcoder bitmask = 0x%x\n",
7624                     transcoder_name(pipe_config->master_transcoder),
7625                     pipe_config->sync_mode_slaves_mask);
7626
7627         drm_dbg_kms(&dev_priv->drm, "bigjoiner: %s\n",
7628                     pipe_config->bigjoiner_slave ? "slave" :
7629                     pipe_config->bigjoiner ? "master" : "no");
7630
7631         drm_dbg_kms(&dev_priv->drm, "splitter: %s, link count %d, overlap %d\n",
7632                     enableddisabled(pipe_config->splitter.enable),
7633                     pipe_config->splitter.link_count,
7634                     pipe_config->splitter.pixel_overlap);
7635
7636         if (pipe_config->has_pch_encoder)
7637                 intel_dump_m_n_config(pipe_config, "fdi",
7638                                       pipe_config->fdi_lanes,
7639                                       &pipe_config->fdi_m_n);
7640
7641         if (intel_crtc_has_dp_encoder(pipe_config)) {
7642                 intel_dump_m_n_config(pipe_config, "dp m_n",
7643                                 pipe_config->lane_count, &pipe_config->dp_m_n);
7644                 if (pipe_config->has_drrs)
7645                         intel_dump_m_n_config(pipe_config, "dp m2_n2",
7646                                               pipe_config->lane_count,
7647                                               &pipe_config->dp_m2_n2);
7648         }
7649
7650         drm_dbg_kms(&dev_priv->drm,
7651                     "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
7652                     pipe_config->has_audio, pipe_config->has_infoframe,
7653                     pipe_config->infoframes.enable);
7654
7655         if (pipe_config->infoframes.enable &
7656             intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
7657                 drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
7658                             pipe_config->infoframes.gcp);
7659         if (pipe_config->infoframes.enable &
7660             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
7661                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
7662         if (pipe_config->infoframes.enable &
7663             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
7664                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
7665         if (pipe_config->infoframes.enable &
7666             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
7667                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
7668         if (pipe_config->infoframes.enable &
7669             intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM))
7670                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
7671         if (pipe_config->infoframes.enable &
7672             intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
7673                 intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
7674         if (pipe_config->infoframes.enable &
7675             intel_hdmi_infoframe_enable(DP_SDP_VSC))
7676                 intel_dump_dp_vsc_sdp(dev_priv, &pipe_config->infoframes.vsc);
7677
7678         drm_dbg_kms(&dev_priv->drm, "vrr: %s, vmin: %d, vmax: %d, pipeline full: %d, guardband: %d flipline: %d, vmin vblank: %d, vmax vblank: %d\n",
7679                     yesno(pipe_config->vrr.enable),
7680                     pipe_config->vrr.vmin, pipe_config->vrr.vmax,
7681                     pipe_config->vrr.pipeline_full, pipe_config->vrr.guardband,
7682                     pipe_config->vrr.flipline,
7683                     intel_vrr_vmin_vblank_start(pipe_config),
7684                     intel_vrr_vmax_vblank_start(pipe_config));
7685
7686         drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
7687         drm_mode_debug_printmodeline(&pipe_config->hw.mode);
7688         drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
7689         drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
7690         intel_dump_crtc_timings(dev_priv, &pipe_config->hw.adjusted_mode);
7691         drm_dbg_kms(&dev_priv->drm, "pipe mode:\n");
7692         drm_mode_debug_printmodeline(&pipe_config->hw.pipe_mode);
7693         intel_dump_crtc_timings(dev_priv, &pipe_config->hw.pipe_mode);
7694         drm_dbg_kms(&dev_priv->drm,
7695                     "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
7696                     pipe_config->port_clock,
7697                     pipe_config->pipe_src_w, pipe_config->pipe_src_h,
7698                     pipe_config->pixel_rate);
7699
7700         drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
7701                     pipe_config->linetime, pipe_config->ips_linetime);
7702
7703         if (DISPLAY_VER(dev_priv) >= 9)
7704                 drm_dbg_kms(&dev_priv->drm,
7705                             "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
7706                             crtc->num_scalers,
7707                             pipe_config->scaler_state.scaler_users,
7708                             pipe_config->scaler_state.scaler_id);
7709
7710         if (HAS_GMCH(dev_priv))
7711                 drm_dbg_kms(&dev_priv->drm,
7712                             "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
7713                             pipe_config->gmch_pfit.control,
7714                             pipe_config->gmch_pfit.pgm_ratios,
7715                             pipe_config->gmch_pfit.lvds_border_bits);
7716         else
7717                 drm_dbg_kms(&dev_priv->drm,
7718                             "pch pfit: " DRM_RECT_FMT ", %s, force thru: %s\n",
7719                             DRM_RECT_ARG(&pipe_config->pch_pfit.dst),
7720                             enableddisabled(pipe_config->pch_pfit.enabled),
7721                             yesno(pipe_config->pch_pfit.force_thru));
7722
7723         drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
7724                     pipe_config->ips_enabled, pipe_config->double_wide);
7725
7726         intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
7727
7728         if (IS_CHERRYVIEW(dev_priv))
7729                 drm_dbg_kms(&dev_priv->drm,
7730                             "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
7731                             pipe_config->cgm_mode, pipe_config->gamma_mode,
7732                             pipe_config->gamma_enable, pipe_config->csc_enable);
7733         else
7734                 drm_dbg_kms(&dev_priv->drm,
7735                             "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
7736                             pipe_config->csc_mode, pipe_config->gamma_mode,
7737                             pipe_config->gamma_enable, pipe_config->csc_enable);
7738
7739         drm_dbg_kms(&dev_priv->drm, "degamma lut: %d entries, gamma lut: %d entries\n",
7740                     pipe_config->hw.degamma_lut ?
7741                     drm_color_lut_size(pipe_config->hw.degamma_lut) : 0,
7742                     pipe_config->hw.gamma_lut ?
7743                     drm_color_lut_size(pipe_config->hw.gamma_lut) : 0);
7744
7745 dump_planes:
7746         if (!state)
7747                 return;
7748
7749         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7750                 if (plane->pipe == crtc->pipe)
7751                         intel_dump_plane_state(plane_state);
7752         }
7753 }
7754
7755 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
7756 {
7757         struct drm_device *dev = state->base.dev;
7758         struct drm_connector *connector;
7759         struct drm_connector_list_iter conn_iter;
7760         unsigned int used_ports = 0;
7761         unsigned int used_mst_ports = 0;
7762         bool ret = true;
7763
7764         /*
7765          * We're going to peek into connector->state,
7766          * hence connection_mutex must be held.
7767          */
7768         drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
7769
7770         /*
7771          * Walk the connector list instead of the encoder
7772          * list to detect the problem on ddi platforms
7773          * where there's just one encoder per digital port.
7774          */
7775         drm_connector_list_iter_begin(dev, &conn_iter);
7776         drm_for_each_connector_iter(connector, &conn_iter) {
7777                 struct drm_connector_state *connector_state;
7778                 struct intel_encoder *encoder;
7779
7780                 connector_state =
7781                         drm_atomic_get_new_connector_state(&state->base,
7782                                                            connector);
7783                 if (!connector_state)
7784                         connector_state = connector->state;
7785
7786                 if (!connector_state->best_encoder)
7787                         continue;
7788
7789                 encoder = to_intel_encoder(connector_state->best_encoder);
7790
7791                 drm_WARN_ON(dev, !connector_state->crtc);
7792
7793                 switch (encoder->type) {
7794                 case INTEL_OUTPUT_DDI:
7795                         if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
7796                                 break;
7797                         fallthrough;
7798                 case INTEL_OUTPUT_DP:
7799                 case INTEL_OUTPUT_HDMI:
7800                 case INTEL_OUTPUT_EDP:
7801                         /* the same port mustn't appear more than once */
7802                         if (used_ports & BIT(encoder->port))
7803                                 ret = false;
7804
7805                         used_ports |= BIT(encoder->port);
7806                         break;
7807                 case INTEL_OUTPUT_DP_MST:
7808                         used_mst_ports |=
7809                                 1 << encoder->port;
7810                         break;
7811                 default:
7812                         break;
7813                 }
7814         }
7815         drm_connector_list_iter_end(&conn_iter);
7816
7817         /* can't mix MST and SST/HDMI on the same port */
7818         if (used_ports & used_mst_ports)
7819                 return false;
7820
7821         return ret;
7822 }
7823
7824 static void
7825 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_atomic_state *state,
7826                                            struct intel_crtc_state *crtc_state)
7827 {
7828         const struct intel_crtc_state *from_crtc_state = crtc_state;
7829
7830         if (crtc_state->bigjoiner_slave) {
7831                 from_crtc_state = intel_atomic_get_new_crtc_state(state,
7832                                                                   crtc_state->bigjoiner_linked_crtc);
7833
7834                 /* No need to copy state if the master state is unchanged */
7835                 if (!from_crtc_state)
7836                         return;
7837         }
7838
7839         intel_crtc_copy_color_blobs(crtc_state, from_crtc_state);
7840 }
7841
7842 static void
7843 intel_crtc_copy_uapi_to_hw_state(struct intel_atomic_state *state,
7844                                  struct intel_crtc_state *crtc_state)
7845 {
7846         crtc_state->hw.enable = crtc_state->uapi.enable;
7847         crtc_state->hw.active = crtc_state->uapi.active;
7848         crtc_state->hw.mode = crtc_state->uapi.mode;
7849         crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
7850         crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
7851
7852         intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc_state);
7853 }
7854
7855 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
7856 {
7857         if (crtc_state->bigjoiner_slave)
7858                 return;
7859
7860         crtc_state->uapi.enable = crtc_state->hw.enable;
7861         crtc_state->uapi.active = crtc_state->hw.active;
7862         drm_WARN_ON(crtc_state->uapi.crtc->dev,
7863                     drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
7864
7865         crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
7866         crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
7867
7868         /* copy color blobs to uapi */
7869         drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
7870                                   crtc_state->hw.degamma_lut);
7871         drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
7872                                   crtc_state->hw.gamma_lut);
7873         drm_property_replace_blob(&crtc_state->uapi.ctm,
7874                                   crtc_state->hw.ctm);
7875 }
7876
7877 static int
7878 copy_bigjoiner_crtc_state(struct intel_crtc_state *crtc_state,
7879                           const struct intel_crtc_state *from_crtc_state)
7880 {
7881         struct intel_crtc_state *saved_state;
7882         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7883
7884         saved_state = kmemdup(from_crtc_state, sizeof(*saved_state), GFP_KERNEL);
7885         if (!saved_state)
7886                 return -ENOMEM;
7887
7888         saved_state->uapi = crtc_state->uapi;
7889         saved_state->scaler_state = crtc_state->scaler_state;
7890         saved_state->shared_dpll = crtc_state->shared_dpll;
7891         saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
7892         saved_state->crc_enabled = crtc_state->crc_enabled;
7893
7894         intel_crtc_free_hw_state(crtc_state);
7895         memcpy(crtc_state, saved_state, sizeof(*crtc_state));
7896         kfree(saved_state);
7897
7898         /* Re-init hw state */
7899         memset(&crtc_state->hw, 0, sizeof(saved_state->hw));
7900         crtc_state->hw.enable = from_crtc_state->hw.enable;
7901         crtc_state->hw.active = from_crtc_state->hw.active;
7902         crtc_state->hw.pipe_mode = from_crtc_state->hw.pipe_mode;
7903         crtc_state->hw.adjusted_mode = from_crtc_state->hw.adjusted_mode;
7904
7905         /* Some fixups */
7906         crtc_state->uapi.mode_changed = from_crtc_state->uapi.mode_changed;
7907         crtc_state->uapi.connectors_changed = from_crtc_state->uapi.connectors_changed;
7908         crtc_state->uapi.active_changed = from_crtc_state->uapi.active_changed;
7909         crtc_state->nv12_planes = crtc_state->c8_planes = crtc_state->update_planes = 0;
7910         crtc_state->bigjoiner_linked_crtc = to_intel_crtc(from_crtc_state->uapi.crtc);
7911         crtc_state->bigjoiner_slave = true;
7912         crtc_state->cpu_transcoder = (enum transcoder)crtc->pipe;
7913         crtc_state->has_audio = false;
7914
7915         return 0;
7916 }
7917
7918 static int
7919 intel_crtc_prepare_cleared_state(struct intel_atomic_state *state,
7920                                  struct intel_crtc_state *crtc_state)
7921 {
7922         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7923         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7924         struct intel_crtc_state *saved_state;
7925
7926         saved_state = intel_crtc_state_alloc(crtc);
7927         if (!saved_state)
7928                 return -ENOMEM;
7929
7930         /* free the old crtc_state->hw members */
7931         intel_crtc_free_hw_state(crtc_state);
7932
7933         /* FIXME: before the switch to atomic started, a new pipe_config was
7934          * kzalloc'd. Code that depends on any field being zero should be
7935          * fixed, so that the crtc_state can be safely duplicated. For now,
7936          * only fields that are know to not cause problems are preserved. */
7937
7938         saved_state->uapi = crtc_state->uapi;
7939         saved_state->scaler_state = crtc_state->scaler_state;
7940         saved_state->shared_dpll = crtc_state->shared_dpll;
7941         saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
7942         memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
7943                sizeof(saved_state->icl_port_dplls));
7944         saved_state->crc_enabled = crtc_state->crc_enabled;
7945         if (IS_G4X(dev_priv) ||
7946             IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
7947                 saved_state->wm = crtc_state->wm;
7948
7949         memcpy(crtc_state, saved_state, sizeof(*crtc_state));
7950         kfree(saved_state);
7951
7952         intel_crtc_copy_uapi_to_hw_state(state, crtc_state);
7953
7954         return 0;
7955 }
7956
7957 static int
7958 intel_modeset_pipe_config(struct intel_atomic_state *state,
7959                           struct intel_crtc_state *pipe_config)
7960 {
7961         struct drm_crtc *crtc = pipe_config->uapi.crtc;
7962         struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
7963         struct drm_connector *connector;
7964         struct drm_connector_state *connector_state;
7965         int base_bpp, ret, i;
7966         bool retry = true;
7967
7968         pipe_config->cpu_transcoder =
7969                 (enum transcoder) to_intel_crtc(crtc)->pipe;
7970
7971         /*
7972          * Sanitize sync polarity flags based on requested ones. If neither
7973          * positive or negative polarity is requested, treat this as meaning
7974          * negative polarity.
7975          */
7976         if (!(pipe_config->hw.adjusted_mode.flags &
7977               (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
7978                 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
7979
7980         if (!(pipe_config->hw.adjusted_mode.flags &
7981               (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
7982                 pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
7983
7984         ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7985                                         pipe_config);
7986         if (ret)
7987                 return ret;
7988
7989         base_bpp = pipe_config->pipe_bpp;
7990
7991         /*
7992          * Determine the real pipe dimensions. Note that stereo modes can
7993          * increase the actual pipe size due to the frame doubling and
7994          * insertion of additional space for blanks between the frame. This
7995          * is stored in the crtc timings. We use the requested mode to do this
7996          * computation to clearly distinguish it from the adjusted mode, which
7997          * can be changed by the connectors in the below retry loop.
7998          */
7999         drm_mode_get_hv_timing(&pipe_config->hw.mode,
8000                                &pipe_config->pipe_src_w,
8001                                &pipe_config->pipe_src_h);
8002
8003         for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
8004                 struct intel_encoder *encoder =
8005                         to_intel_encoder(connector_state->best_encoder);
8006
8007                 if (connector_state->crtc != crtc)
8008                         continue;
8009
8010                 if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
8011                         drm_dbg_kms(&i915->drm,
8012                                     "rejecting invalid cloning configuration\n");
8013                         return -EINVAL;
8014                 }
8015
8016                 /*
8017                  * Determine output_types before calling the .compute_config()
8018                  * hooks so that the hooks can use this information safely.
8019                  */
8020                 if (encoder->compute_output_type)
8021                         pipe_config->output_types |=
8022                                 BIT(encoder->compute_output_type(encoder, pipe_config,
8023                                                                  connector_state));
8024                 else
8025                         pipe_config->output_types |= BIT(encoder->type);
8026         }
8027
8028 encoder_retry:
8029         /* Ensure the port clock defaults are reset when retrying. */
8030         pipe_config->port_clock = 0;
8031         pipe_config->pixel_multiplier = 1;
8032
8033         /* Fill in default crtc timings, allow encoders to overwrite them. */
8034         drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
8035                               CRTC_STEREO_DOUBLE);
8036
8037         /* Pass our mode to the connectors and the CRTC to give them a chance to
8038          * adjust it according to limitations or connector properties, and also
8039          * a chance to reject the mode entirely.
8040          */
8041         for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
8042                 struct intel_encoder *encoder =
8043                         to_intel_encoder(connector_state->best_encoder);
8044
8045                 if (connector_state->crtc != crtc)
8046                         continue;
8047
8048                 ret = encoder->compute_config(encoder, pipe_config,
8049                                               connector_state);
8050                 if (ret < 0) {
8051                         if (ret != -EDEADLK)
8052                                 drm_dbg_kms(&i915->drm,
8053                                             "Encoder config failure: %d\n",
8054                                             ret);
8055                         return ret;
8056                 }
8057         }
8058
8059         /* Set default port clock if not overwritten by the encoder. Needs to be
8060          * done afterwards in case the encoder adjusts the mode. */
8061         if (!pipe_config->port_clock)
8062                 pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
8063                         * pipe_config->pixel_multiplier;
8064
8065         ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
8066         if (ret == -EDEADLK)
8067                 return ret;
8068         if (ret < 0) {
8069                 drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
8070                 return ret;
8071         }
8072
8073         if (ret == I915_DISPLAY_CONFIG_RETRY) {
8074                 if (drm_WARN(&i915->drm, !retry,
8075                              "loop in pipe configuration computation\n"))
8076                         return -EINVAL;
8077
8078                 drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
8079                 retry = false;
8080                 goto encoder_retry;
8081         }
8082
8083         /* Dithering seems to not pass-through bits correctly when it should, so
8084          * only enable it on 6bpc panels and when its not a compliance
8085          * test requesting 6bpc video pattern.
8086          */
8087         pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
8088                 !pipe_config->dither_force_disable;
8089         drm_dbg_kms(&i915->drm,
8090                     "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
8091                     base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8092
8093         return 0;
8094 }
8095
8096 static int
8097 intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
8098 {
8099         struct intel_atomic_state *state =
8100                 to_intel_atomic_state(crtc_state->uapi.state);
8101         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8102         struct drm_connector_state *conn_state;
8103         struct drm_connector *connector;
8104         int i;
8105
8106         for_each_new_connector_in_state(&state->base, connector,
8107                                         conn_state, i) {
8108                 struct intel_encoder *encoder =
8109                         to_intel_encoder(conn_state->best_encoder);
8110                 int ret;
8111
8112                 if (conn_state->crtc != &crtc->base ||
8113                     !encoder->compute_config_late)
8114                         continue;
8115
8116                 ret = encoder->compute_config_late(encoder, crtc_state,
8117                                                    conn_state);
8118                 if (ret)
8119                         return ret;
8120         }
8121
8122         return 0;
8123 }
8124
8125 bool intel_fuzzy_clock_check(int clock1, int clock2)
8126 {
8127         int diff;
8128
8129         if (clock1 == clock2)
8130                 return true;
8131
8132         if (!clock1 || !clock2)
8133                 return false;
8134
8135         diff = abs(clock1 - clock2);
8136
8137         if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8138                 return true;
8139
8140         return false;
8141 }
8142
8143 static bool
8144 intel_compare_m_n(unsigned int m, unsigned int n,
8145                   unsigned int m2, unsigned int n2,
8146                   bool exact)
8147 {
8148         if (m == m2 && n == n2)
8149                 return true;
8150
8151         if (exact || !m || !n || !m2 || !n2)
8152                 return false;
8153
8154         BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
8155
8156         if (n > n2) {
8157                 while (n > n2) {
8158                         m2 <<= 1;
8159                         n2 <<= 1;
8160                 }
8161         } else if (n < n2) {
8162                 while (n < n2) {
8163                         m <<= 1;
8164                         n <<= 1;
8165                 }
8166         }
8167
8168         if (n != n2)
8169                 return false;
8170
8171         return intel_fuzzy_clock_check(m, m2);
8172 }
8173
8174 static bool
8175 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
8176                        const struct intel_link_m_n *m2_n2,
8177                        bool exact)
8178 {
8179         return m_n->tu == m2_n2->tu &&
8180                 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
8181                                   m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
8182                 intel_compare_m_n(m_n->link_m, m_n->link_n,
8183                                   m2_n2->link_m, m2_n2->link_n, exact);
8184 }
8185
8186 static bool
8187 intel_compare_infoframe(const union hdmi_infoframe *a,
8188                         const union hdmi_infoframe *b)
8189 {
8190         return memcmp(a, b, sizeof(*a)) == 0;
8191 }
8192
8193 static bool
8194 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
8195                          const struct drm_dp_vsc_sdp *b)
8196 {
8197         return memcmp(a, b, sizeof(*a)) == 0;
8198 }
8199
8200 static void
8201 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
8202                                bool fastset, const char *name,
8203                                const union hdmi_infoframe *a,
8204                                const union hdmi_infoframe *b)
8205 {
8206         if (fastset) {
8207                 if (!drm_debug_enabled(DRM_UT_KMS))
8208                         return;
8209
8210                 drm_dbg_kms(&dev_priv->drm,
8211                             "fastset mismatch in %s infoframe\n", name);
8212                 drm_dbg_kms(&dev_priv->drm, "expected:\n");
8213                 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
8214                 drm_dbg_kms(&dev_priv->drm, "found:\n");
8215                 hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
8216         } else {
8217                 drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
8218                 drm_err(&dev_priv->drm, "expected:\n");
8219                 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
8220                 drm_err(&dev_priv->drm, "found:\n");
8221                 hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
8222         }
8223 }
8224
8225 static void
8226 pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
8227                                 bool fastset, const char *name,
8228                                 const struct drm_dp_vsc_sdp *a,
8229                                 const struct drm_dp_vsc_sdp *b)
8230 {
8231         if (fastset) {
8232                 if (!drm_debug_enabled(DRM_UT_KMS))
8233                         return;
8234
8235                 drm_dbg_kms(&dev_priv->drm,
8236                             "fastset mismatch in %s dp sdp\n", name);
8237                 drm_dbg_kms(&dev_priv->drm, "expected:\n");
8238                 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
8239                 drm_dbg_kms(&dev_priv->drm, "found:\n");
8240                 drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
8241         } else {
8242                 drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
8243                 drm_err(&dev_priv->drm, "expected:\n");
8244                 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
8245                 drm_err(&dev_priv->drm, "found:\n");
8246                 drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
8247         }
8248 }
8249
8250 static void __printf(4, 5)
8251 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
8252                      const char *name, const char *format, ...)
8253 {
8254         struct drm_i915_private *i915 = to_i915(crtc->base.dev);
8255         struct va_format vaf;
8256         va_list args;
8257
8258         va_start(args, format);
8259         vaf.fmt = format;
8260         vaf.va = &args;
8261
8262         if (fastset)
8263                 drm_dbg_kms(&i915->drm,
8264                             "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
8265                             crtc->base.base.id, crtc->base.name, name, &vaf);
8266         else
8267                 drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
8268                         crtc->base.base.id, crtc->base.name, name, &vaf);
8269
8270         va_end(args);
8271 }
8272
8273 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
8274 {
8275         if (dev_priv->params.fastboot != -1)
8276                 return dev_priv->params.fastboot;
8277
8278         /* Enable fastboot by default on Skylake and newer */
8279         if (DISPLAY_VER(dev_priv) >= 9)
8280                 return true;
8281
8282         /* Enable fastboot by default on VLV and CHV */
8283         if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
8284                 return true;
8285
8286         /* Disabled by default on all others */
8287         return false;
8288 }
8289
8290 static bool
8291 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
8292                           const struct intel_crtc_state *pipe_config,
8293                           bool fastset)
8294 {
8295         struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
8296         struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
8297         bool ret = true;
8298         u32 bp_gamma = 0;
8299         bool fixup_inherited = fastset &&
8300                 current_config->inherited && !pipe_config->inherited;
8301
8302         if (fixup_inherited && !fastboot_enabled(dev_priv)) {
8303                 drm_dbg_kms(&dev_priv->drm,
8304                             "initial modeset and fastboot not set\n");
8305                 ret = false;
8306         }
8307
8308 #define PIPE_CONF_CHECK_X(name) do { \
8309         if (current_config->name != pipe_config->name) { \
8310                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8311                                      "(expected 0x%08x, found 0x%08x)", \
8312                                      current_config->name, \
8313                                      pipe_config->name); \
8314                 ret = false; \
8315         } \
8316 } while (0)
8317
8318 #define PIPE_CONF_CHECK_X_WITH_MASK(name, mask) do { \
8319         if ((current_config->name & (mask)) != (pipe_config->name & (mask))) { \
8320                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8321                                      "(expected 0x%08x, found 0x%08x)", \
8322                                      current_config->name & (mask), \
8323                                      pipe_config->name & (mask)); \
8324                 ret = false; \
8325         } \
8326 } while (0)
8327
8328 #define PIPE_CONF_CHECK_I(name) do { \
8329         if (current_config->name != pipe_config->name) { \
8330                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8331                                      "(expected %i, found %i)", \
8332                                      current_config->name, \
8333                                      pipe_config->name); \
8334                 ret = false; \
8335         } \
8336 } while (0)
8337
8338 #define PIPE_CONF_CHECK_BOOL(name) do { \
8339         if (current_config->name != pipe_config->name) { \
8340                 pipe_config_mismatch(fastset, crtc,  __stringify(name), \
8341                                      "(expected %s, found %s)", \
8342                                      yesno(current_config->name), \
8343                                      yesno(pipe_config->name)); \
8344                 ret = false; \
8345         } \
8346 } while (0)
8347
8348 /*
8349  * Checks state where we only read out the enabling, but not the entire
8350  * state itself (like full infoframes or ELD for audio). These states
8351  * require a full modeset on bootup to fix up.
8352  */
8353 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
8354         if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
8355                 PIPE_CONF_CHECK_BOOL(name); \
8356         } else { \
8357                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8358                                      "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
8359                                      yesno(current_config->name), \
8360                                      yesno(pipe_config->name)); \
8361                 ret = false; \
8362         } \
8363 } while (0)
8364
8365 #define PIPE_CONF_CHECK_P(name) do { \
8366         if (current_config->name != pipe_config->name) { \
8367                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8368                                      "(expected %p, found %p)", \
8369                                      current_config->name, \
8370                                      pipe_config->name); \
8371                 ret = false; \
8372         } \
8373 } while (0)
8374
8375 #define PIPE_CONF_CHECK_M_N(name) do { \
8376         if (!intel_compare_link_m_n(&current_config->name, \
8377                                     &pipe_config->name,\
8378                                     !fastset)) { \
8379                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8380                                      "(expected tu %i gmch %i/%i link %i/%i, " \
8381                                      "found tu %i, gmch %i/%i link %i/%i)", \
8382                                      current_config->name.tu, \
8383                                      current_config->name.gmch_m, \
8384                                      current_config->name.gmch_n, \
8385                                      current_config->name.link_m, \
8386                                      current_config->name.link_n, \
8387                                      pipe_config->name.tu, \
8388                                      pipe_config->name.gmch_m, \
8389                                      pipe_config->name.gmch_n, \
8390                                      pipe_config->name.link_m, \
8391                                      pipe_config->name.link_n); \
8392                 ret = false; \
8393         } \
8394 } while (0)
8395
8396 /* This is required for BDW+ where there is only one set of registers for
8397  * switching between high and low RR.
8398  * This macro can be used whenever a comparison has to be made between one
8399  * hw state and multiple sw state variables.
8400  */
8401 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
8402         if (!intel_compare_link_m_n(&current_config->name, \
8403                                     &pipe_config->name, !fastset) && \
8404             !intel_compare_link_m_n(&current_config->alt_name, \
8405                                     &pipe_config->name, !fastset)) { \
8406                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8407                                      "(expected tu %i gmch %i/%i link %i/%i, " \
8408                                      "or tu %i gmch %i/%i link %i/%i, " \
8409                                      "found tu %i, gmch %i/%i link %i/%i)", \
8410                                      current_config->name.tu, \
8411                                      current_config->name.gmch_m, \
8412                                      current_config->name.gmch_n, \
8413                                      current_config->name.link_m, \
8414                                      current_config->name.link_n, \
8415                                      current_config->alt_name.tu, \
8416                                      current_config->alt_name.gmch_m, \
8417                                      current_config->alt_name.gmch_n, \
8418                                      current_config->alt_name.link_m, \
8419                                      current_config->alt_name.link_n, \
8420                                      pipe_config->name.tu, \
8421                                      pipe_config->name.gmch_m, \
8422                                      pipe_config->name.gmch_n, \
8423                                      pipe_config->name.link_m, \
8424                                      pipe_config->name.link_n); \
8425                 ret = false; \
8426         } \
8427 } while (0)
8428
8429 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
8430         if ((current_config->name ^ pipe_config->name) & (mask)) { \
8431                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8432                                      "(%x) (expected %i, found %i)", \
8433                                      (mask), \
8434                                      current_config->name & (mask), \
8435                                      pipe_config->name & (mask)); \
8436                 ret = false; \
8437         } \
8438 } while (0)
8439
8440 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
8441         if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
8442                 pipe_config_mismatch(fastset, crtc, __stringify(name), \
8443                                      "(expected %i, found %i)", \
8444                                      current_config->name, \
8445                                      pipe_config->name); \
8446                 ret = false; \
8447         } \
8448 } while (0)
8449
8450 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
8451         if (!intel_compare_infoframe(&current_config->infoframes.name, \
8452                                      &pipe_config->infoframes.name)) { \
8453                 pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
8454                                                &current_config->infoframes.name, \
8455                                                &pipe_config->infoframes.name); \
8456                 ret = false; \
8457         } \
8458 } while (0)
8459
8460 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
8461         if (!current_config->has_psr && !pipe_config->has_psr && \
8462             !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
8463                                       &pipe_config->infoframes.name)) { \
8464                 pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
8465                                                 &current_config->infoframes.name, \
8466                                                 &pipe_config->infoframes.name); \
8467                 ret = false; \
8468         } \
8469 } while (0)
8470
8471 #define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
8472         if (current_config->name1 != pipe_config->name1) { \
8473                 pipe_config_mismatch(fastset, crtc, __stringify(name1), \
8474                                 "(expected %i, found %i, won't compare lut values)", \
8475                                 current_config->name1, \
8476                                 pipe_config->name1); \
8477                 ret = false;\
8478         } else { \
8479                 if (!intel_color_lut_equal(current_config->name2, \
8480                                         pipe_config->name2, pipe_config->name1, \
8481                                         bit_precision)) { \
8482                         pipe_config_mismatch(fastset, crtc, __stringify(name2), \
8483                                         "hw_state doesn't match sw_state"); \
8484                         ret = false; \
8485                 } \
8486         } \
8487 } while (0)
8488
8489 #define PIPE_CONF_QUIRK(quirk) \
8490         ((current_config->quirks | pipe_config->quirks) & (quirk))
8491
8492         PIPE_CONF_CHECK_I(cpu_transcoder);
8493
8494         PIPE_CONF_CHECK_BOOL(has_pch_encoder);
8495         PIPE_CONF_CHECK_I(fdi_lanes);
8496         PIPE_CONF_CHECK_M_N(fdi_m_n);
8497
8498         PIPE_CONF_CHECK_I(lane_count);
8499         PIPE_CONF_CHECK_X(lane_lat_optim_mask);
8500
8501         if (DISPLAY_VER(dev_priv) < 8) {
8502                 PIPE_CONF_CHECK_M_N(dp_m_n);
8503
8504                 if (current_config->has_drrs)
8505                         PIPE_CONF_CHECK_M_N(dp_m2_n2);
8506         } else
8507                 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
8508
8509         PIPE_CONF_CHECK_X(output_types);
8510
8511         /* FIXME do the readout properly and get rid of this quirk */
8512         if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
8513                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hdisplay);
8514                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_htotal);
8515                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_start);
8516                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_end);
8517                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_start);
8518                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_end);
8519
8520                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vdisplay);
8521                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vtotal);
8522                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_start);
8523                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_end);
8524                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_start);
8525                 PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_end);
8526
8527                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
8528                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
8529                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
8530                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
8531                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
8532                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
8533
8534                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
8535                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
8536                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
8537                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
8538                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
8539                 PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
8540
8541                 PIPE_CONF_CHECK_I(pixel_multiplier);
8542
8543                 PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8544                                       DRM_MODE_FLAG_INTERLACE);
8545
8546                 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8547                         PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8548                                               DRM_MODE_FLAG_PHSYNC);
8549                         PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8550                                               DRM_MODE_FLAG_NHSYNC);
8551                         PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8552                                               DRM_MODE_FLAG_PVSYNC);
8553                         PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8554                                               DRM_MODE_FLAG_NVSYNC);
8555                 }
8556         }
8557
8558         PIPE_CONF_CHECK_I(output_format);
8559         PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
8560         if ((DISPLAY_VER(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
8561             IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
8562                 PIPE_CONF_CHECK_BOOL(limited_color_range);
8563
8564         PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
8565         PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
8566         PIPE_CONF_CHECK_BOOL(has_infoframe);
8567         /* FIXME do the readout properly and get rid of this quirk */
8568         if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
8569                 PIPE_CONF_CHECK_BOOL(fec_enable);
8570
8571         PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
8572
8573         PIPE_CONF_CHECK_X(gmch_pfit.control);
8574         /* pfit ratios are autocomputed by the hw on gen4+ */
8575         if (DISPLAY_VER(dev_priv) < 4)
8576                 PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
8577         PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
8578
8579         /*
8580          * Changing the EDP transcoder input mux
8581          * (A_ONOFF vs. A_ON) requires a full modeset.
8582          */
8583         PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
8584
8585         if (!fastset) {
8586                 PIPE_CONF_CHECK_I(pipe_src_w);
8587                 PIPE_CONF_CHECK_I(pipe_src_h);
8588
8589                 PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
8590                 if (current_config->pch_pfit.enabled) {
8591                         PIPE_CONF_CHECK_I(pch_pfit.dst.x1);
8592                         PIPE_CONF_CHECK_I(pch_pfit.dst.y1);
8593                         PIPE_CONF_CHECK_I(pch_pfit.dst.x2);
8594                         PIPE_CONF_CHECK_I(pch_pfit.dst.y2);
8595                 }
8596
8597                 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
8598                 /* FIXME do the readout properly and get rid of this quirk */
8599                 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
8600                         PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
8601
8602                 PIPE_CONF_CHECK_X(gamma_mode);
8603                 if (IS_CHERRYVIEW(dev_priv))
8604                         PIPE_CONF_CHECK_X(cgm_mode);
8605                 else
8606                         PIPE_CONF_CHECK_X(csc_mode);
8607                 PIPE_CONF_CHECK_BOOL(gamma_enable);
8608                 PIPE_CONF_CHECK_BOOL(csc_enable);
8609
8610                 PIPE_CONF_CHECK_I(linetime);
8611                 PIPE_CONF_CHECK_I(ips_linetime);
8612
8613                 bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
8614                 if (bp_gamma)
8615                         PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
8616
8617                 PIPE_CONF_CHECK_BOOL(has_psr);
8618                 PIPE_CONF_CHECK_BOOL(has_psr2);
8619                 PIPE_CONF_CHECK_BOOL(enable_psr2_sel_fetch);
8620                 PIPE_CONF_CHECK_I(dc3co_exitline);
8621         }
8622
8623         PIPE_CONF_CHECK_BOOL(double_wide);
8624
8625         PIPE_CONF_CHECK_P(shared_dpll);
8626
8627         /* FIXME do the readout properly and get rid of this quirk */
8628         if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
8629                 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8630                 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
8631                 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
8632                 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
8633                 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
8634                 PIPE_CONF_CHECK_X(dpll_hw_state.spll);
8635                 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
8636                 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
8637                 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
8638                 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
8639                 PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
8640                 PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
8641                 PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
8642                 PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
8643                 PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
8644                 PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
8645                 PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
8646                 PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
8647                 PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
8648                 PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
8649                 PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
8650                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
8651                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
8652                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
8653                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
8654                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
8655                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
8656                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
8657                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
8658                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
8659                 PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
8660
8661                 PIPE_CONF_CHECK_X(dsi_pll.ctrl);
8662                 PIPE_CONF_CHECK_X(dsi_pll.div);
8663
8664                 if (IS_G4X(dev_priv) || DISPLAY_VER(dev_priv) >= 5)
8665                         PIPE_CONF_CHECK_I(pipe_bpp);
8666
8667                 PIPE_CONF_CHECK_CLOCK_FUZZY(hw.pipe_mode.crtc_clock);
8668                 PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
8669                 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
8670
8671                 PIPE_CONF_CHECK_I(min_voltage_level);
8672         }
8673
8674         if (fastset && (current_config->has_psr || pipe_config->has_psr))
8675                 PIPE_CONF_CHECK_X_WITH_MASK(infoframes.enable,
8676                                             ~intel_hdmi_infoframe_enable(DP_SDP_VSC));
8677         else
8678                 PIPE_CONF_CHECK_X(infoframes.enable);
8679
8680         PIPE_CONF_CHECK_X(infoframes.gcp);
8681         PIPE_CONF_CHECK_INFOFRAME(avi);
8682         PIPE_CONF_CHECK_INFOFRAME(spd);
8683         PIPE_CONF_CHECK_INFOFRAME(hdmi);
8684         PIPE_CONF_CHECK_INFOFRAME(drm);
8685         PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
8686
8687         PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
8688         PIPE_CONF_CHECK_I(master_transcoder);
8689         PIPE_CONF_CHECK_BOOL(bigjoiner);
8690         PIPE_CONF_CHECK_BOOL(bigjoiner_slave);
8691         PIPE_CONF_CHECK_P(bigjoiner_linked_crtc);
8692
8693         PIPE_CONF_CHECK_I(dsc.compression_enable);
8694         PIPE_CONF_CHECK_I(dsc.dsc_split);
8695         PIPE_CONF_CHECK_I(dsc.compressed_bpp);
8696
8697         PIPE_CONF_CHECK_BOOL(splitter.enable);
8698         PIPE_CONF_CHECK_I(splitter.link_count);
8699         PIPE_CONF_CHECK_I(splitter.pixel_overlap);
8700
8701         PIPE_CONF_CHECK_I(mst_master_transcoder);
8702
8703         PIPE_CONF_CHECK_BOOL(vrr.enable);
8704         PIPE_CONF_CHECK_I(vrr.vmin);
8705         PIPE_CONF_CHECK_I(vrr.vmax);
8706         PIPE_CONF_CHECK_I(vrr.flipline);
8707         PIPE_CONF_CHECK_I(vrr.pipeline_full);
8708         PIPE_CONF_CHECK_I(vrr.guardband);
8709
8710 #undef PIPE_CONF_CHECK_X
8711 #undef PIPE_CONF_CHECK_I
8712 #undef PIPE_CONF_CHECK_BOOL
8713 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
8714 #undef PIPE_CONF_CHECK_P
8715 #undef PIPE_CONF_CHECK_FLAGS
8716 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
8717 #undef PIPE_CONF_CHECK_COLOR_LUT
8718 #undef PIPE_CONF_QUIRK
8719
8720         return ret;
8721 }
8722
8723 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
8724                                            const struct intel_crtc_state *pipe_config)
8725 {
8726         if (pipe_config->has_pch_encoder) {
8727                 int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
8728                                                             &pipe_config->fdi_m_n);
8729                 int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
8730
8731                 /*
8732                  * FDI already provided one idea for the dotclock.
8733                  * Yell if the encoder disagrees.
8734                  */
8735                 drm_WARN(&dev_priv->drm,
8736                          !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
8737                          "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
8738                          fdi_dotclock, dotclock);
8739         }
8740 }
8741
8742 static void verify_wm_state(struct intel_crtc *crtc,
8743                             struct intel_crtc_state *new_crtc_state)
8744 {
8745         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8746         struct skl_hw_state {
8747                 struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
8748                 struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
8749                 struct skl_pipe_wm wm;
8750         } *hw;
8751         const struct skl_pipe_wm *sw_wm = &new_crtc_state->wm.skl.optimal;
8752         int level, max_level = ilk_wm_max_level(dev_priv);
8753         struct intel_plane *plane;
8754         u8 hw_enabled_slices;
8755
8756         if (DISPLAY_VER(dev_priv) < 9 || !new_crtc_state->hw.active)
8757                 return;
8758
8759         hw = kzalloc(sizeof(*hw), GFP_KERNEL);
8760         if (!hw)
8761                 return;
8762
8763         skl_pipe_wm_get_hw_state(crtc, &hw->wm);
8764
8765         skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
8766
8767         hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
8768
8769         if (DISPLAY_VER(dev_priv) >= 11 &&
8770             hw_enabled_slices != dev_priv->dbuf.enabled_slices)
8771                 drm_err(&dev_priv->drm,
8772                         "mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
8773                         dev_priv->dbuf.enabled_slices,
8774                         hw_enabled_slices);
8775
8776         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
8777                 const struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
8778                 const struct skl_wm_level *hw_wm_level, *sw_wm_level;
8779
8780                 /* Watermarks */
8781                 for (level = 0; level <= max_level; level++) {
8782                         hw_wm_level = &hw->wm.planes[plane->id].wm[level];
8783                         sw_wm_level = skl_plane_wm_level(sw_wm, plane->id, level);
8784
8785                         if (skl_wm_level_equals(hw_wm_level, sw_wm_level))
8786                                 continue;
8787
8788                         drm_err(&dev_priv->drm,
8789                                 "[PLANE:%d:%s] mismatch in WM%d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
8790                                 plane->base.base.id, plane->base.name, level,
8791                                 sw_wm_level->enable,
8792                                 sw_wm_level->blocks,
8793                                 sw_wm_level->lines,
8794                                 hw_wm_level->enable,
8795                                 hw_wm_level->blocks,
8796                                 hw_wm_level->lines);
8797                 }
8798
8799                 hw_wm_level = &hw->wm.planes[plane->id].trans_wm;
8800                 sw_wm_level = skl_plane_trans_wm(sw_wm, plane->id);
8801
8802                 if (!skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
8803                         drm_err(&dev_priv->drm,
8804                                 "[PLANE:%d:%s] mismatch in trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
8805                                 plane->base.base.id, plane->base.name,
8806                                 sw_wm_level->enable,
8807                                 sw_wm_level->blocks,
8808                                 sw_wm_level->lines,
8809                                 hw_wm_level->enable,
8810                                 hw_wm_level->blocks,
8811                                 hw_wm_level->lines);
8812                 }
8813
8814                 hw_wm_level = &hw->wm.planes[plane->id].sagv.wm0;
8815                 sw_wm_level = &sw_wm->planes[plane->id].sagv.wm0;
8816
8817                 if (HAS_HW_SAGV_WM(dev_priv) &&
8818                     !skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
8819                         drm_err(&dev_priv->drm,
8820                                 "[PLANE:%d:%s] mismatch in SAGV WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
8821                                 plane->base.base.id, plane->base.name,
8822                                 sw_wm_level->enable,
8823                                 sw_wm_level->blocks,
8824                                 sw_wm_level->lines,
8825                                 hw_wm_level->enable,
8826                                 hw_wm_level->blocks,
8827                                 hw_wm_level->lines);
8828                 }
8829
8830                 hw_wm_level = &hw->wm.planes[plane->id].sagv.trans_wm;
8831                 sw_wm_level = &sw_wm->planes[plane->id].sagv.trans_wm;
8832
8833                 if (HAS_HW_SAGV_WM(dev_priv) &&
8834                     !skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
8835                         drm_err(&dev_priv->drm,
8836                                 "[PLANE:%d:%s] mismatch in SAGV trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
8837                                 plane->base.base.id, plane->base.name,
8838                                 sw_wm_level->enable,
8839                                 sw_wm_level->blocks,
8840                                 sw_wm_level->lines,
8841                                 hw_wm_level->enable,
8842                                 hw_wm_level->blocks,
8843                                 hw_wm_level->lines);
8844                 }
8845
8846                 /* DDB */
8847                 hw_ddb_entry = &hw->ddb_y[plane->id];
8848                 sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane->id];
8849
8850                 if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
8851                         drm_err(&dev_priv->drm,
8852                                 "[PLANE:%d:%s] mismatch in DDB (expected (%u,%u), found (%u,%u))\n",
8853                                 plane->base.base.id, plane->base.name,
8854                                 sw_ddb_entry->start, sw_ddb_entry->end,
8855                                 hw_ddb_entry->start, hw_ddb_entry->end);
8856                 }
8857         }
8858
8859         kfree(hw);
8860 }
8861
8862 static void
8863 verify_connector_state(struct intel_atomic_state *state,
8864                        struct intel_crtc *crtc)
8865 {
8866         struct drm_connector *connector;
8867         struct drm_connector_state *new_conn_state;
8868         int i;
8869
8870         for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
8871                 struct drm_encoder *encoder = connector->encoder;
8872                 struct intel_crtc_state *crtc_state = NULL;
8873
8874                 if (new_conn_state->crtc != &crtc->base)
8875                         continue;
8876
8877                 if (crtc)
8878                         crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
8879
8880                 intel_connector_verify_state(crtc_state, new_conn_state);
8881
8882                 I915_STATE_WARN(new_conn_state->best_encoder != encoder,
8883                      "connector's atomic encoder doesn't match legacy encoder\n");
8884         }
8885 }
8886
8887 static void
8888 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
8889 {
8890         struct intel_encoder *encoder;
8891         struct drm_connector *connector;
8892         struct drm_connector_state *old_conn_state, *new_conn_state;
8893         int i;
8894
8895         for_each_intel_encoder(&dev_priv->drm, encoder) {
8896                 bool enabled = false, found = false;
8897                 enum pipe pipe;
8898
8899                 drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
8900                             encoder->base.base.id,
8901                             encoder->base.name);
8902
8903                 for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
8904                                                    new_conn_state, i) {
8905                         if (old_conn_state->best_encoder == &encoder->base)
8906                                 found = true;
8907
8908                         if (new_conn_state->best_encoder != &encoder->base)
8909                                 continue;
8910                         found = enabled = true;
8911
8912                         I915_STATE_WARN(new_conn_state->crtc !=
8913                                         encoder->base.crtc,
8914                              "connector's crtc doesn't match encoder crtc\n");
8915                 }
8916
8917                 if (!found)
8918                         continue;
8919
8920                 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8921                      "encoder's enabled state mismatch "
8922                      "(expected %i, found %i)\n",
8923                      !!encoder->base.crtc, enabled);
8924
8925                 if (!encoder->base.crtc) {
8926                         bool active;
8927
8928                         active = encoder->get_hw_state(encoder, &pipe);
8929                         I915_STATE_WARN(active,
8930                              "encoder detached but still enabled on pipe %c.\n",
8931                              pipe_name(pipe));
8932                 }
8933         }
8934 }
8935
8936 static void
8937 verify_crtc_state(struct intel_crtc *crtc,
8938                   struct intel_crtc_state *old_crtc_state,
8939                   struct intel_crtc_state *new_crtc_state)
8940 {
8941         struct drm_device *dev = crtc->base.dev;
8942         struct drm_i915_private *dev_priv = to_i915(dev);
8943         struct intel_encoder *encoder;
8944         struct intel_crtc_state *pipe_config = old_crtc_state;
8945         struct drm_atomic_state *state = old_crtc_state->uapi.state;
8946         struct intel_crtc *master = crtc;
8947
8948         __drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
8949         intel_crtc_free_hw_state(old_crtc_state);
8950         intel_crtc_state_reset(old_crtc_state, crtc);
8951         old_crtc_state->uapi.state = state;
8952
8953         drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
8954                     crtc->base.name);
8955
8956         pipe_config->hw.enable = new_crtc_state->hw.enable;
8957
8958         intel_crtc_get_pipe_config(pipe_config);
8959
8960         /* we keep both pipes enabled on 830 */
8961         if (IS_I830(dev_priv) && pipe_config->hw.active)
8962                 pipe_config->hw.active = new_crtc_state->hw.active;
8963
8964         I915_STATE_WARN(new_crtc_state->hw.active != pipe_config->hw.active,
8965                         "crtc active state doesn't match with hw state "
8966                         "(expected %i, found %i)\n",
8967                         new_crtc_state->hw.active, pipe_config->hw.active);
8968
8969         I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
8970                         "transitional active state does not match atomic hw state "
8971                         "(expected %i, found %i)\n",
8972                         new_crtc_state->hw.active, crtc->active);
8973
8974         if (new_crtc_state->bigjoiner_slave)
8975                 master = new_crtc_state->bigjoiner_linked_crtc;
8976
8977         for_each_encoder_on_crtc(dev, &master->base, encoder) {
8978                 enum pipe pipe;
8979                 bool active;
8980
8981                 active = encoder->get_hw_state(encoder, &pipe);
8982                 I915_STATE_WARN(active != new_crtc_state->hw.active,
8983                                 "[ENCODER:%i] active %i with crtc active %i\n",
8984                                 encoder->base.base.id, active,
8985                                 new_crtc_state->hw.active);
8986
8987                 I915_STATE_WARN(active && master->pipe != pipe,
8988                                 "Encoder connected to wrong pipe %c\n",
8989                                 pipe_name(pipe));
8990
8991                 if (active)
8992                         intel_encoder_get_config(encoder, pipe_config);
8993         }
8994
8995         if (!new_crtc_state->hw.active)
8996                 return;
8997
8998         intel_pipe_config_sanity_check(dev_priv, pipe_config);
8999
9000         if (!intel_pipe_config_compare(new_crtc_state,
9001                                        pipe_config, false)) {
9002                 I915_STATE_WARN(1, "pipe state doesn't match!\n");
9003                 intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
9004                 intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
9005         }
9006 }
9007
9008 static void
9009 intel_verify_planes(struct intel_atomic_state *state)
9010 {
9011         struct intel_plane *plane;
9012         const struct intel_plane_state *plane_state;
9013         int i;
9014
9015         for_each_new_intel_plane_in_state(state, plane,
9016                                           plane_state, i)
9017                 assert_plane(plane, plane_state->planar_slave ||
9018                              plane_state->uapi.visible);
9019 }
9020
9021 static void
9022 verify_single_dpll_state(struct drm_i915_private *dev_priv,
9023                          struct intel_shared_dpll *pll,
9024                          struct intel_crtc *crtc,
9025                          struct intel_crtc_state *new_crtc_state)
9026 {
9027         struct intel_dpll_hw_state dpll_hw_state;
9028         u8 pipe_mask;
9029         bool active;
9030
9031         memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9032
9033         drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
9034
9035         active = intel_dpll_get_hw_state(dev_priv, pll, &dpll_hw_state);
9036
9037         if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
9038                 I915_STATE_WARN(!pll->on && pll->active_mask,
9039                      "pll in active use but not on in sw tracking\n");
9040                 I915_STATE_WARN(pll->on && !pll->active_mask,
9041                      "pll is on but not used by any active pipe\n");
9042                 I915_STATE_WARN(pll->on != active,
9043                      "pll on state mismatch (expected %i, found %i)\n",
9044                      pll->on, active);
9045         }
9046
9047         if (!crtc) {
9048                 I915_STATE_WARN(pll->active_mask & ~pll->state.pipe_mask,
9049                                 "more active pll users than references: 0x%x vs 0x%x\n",
9050                                 pll->active_mask, pll->state.pipe_mask);
9051
9052                 return;
9053         }
9054
9055         pipe_mask = BIT(crtc->pipe);
9056
9057         if (new_crtc_state->hw.active)
9058                 I915_STATE_WARN(!(pll->active_mask & pipe_mask),
9059                                 "pll active mismatch (expected pipe %c in active mask 0x%x)\n",
9060                                 pipe_name(crtc->pipe), pll->active_mask);
9061         else
9062                 I915_STATE_WARN(pll->active_mask & pipe_mask,
9063                                 "pll active mismatch (didn't expect pipe %c in active mask 0x%x)\n",
9064                                 pipe_name(crtc->pipe), pll->active_mask);
9065
9066         I915_STATE_WARN(!(pll->state.pipe_mask & pipe_mask),
9067                         "pll enabled crtcs mismatch (expected 0x%x in 0x%x)\n",
9068                         pipe_mask, pll->state.pipe_mask);
9069
9070         I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
9071                                           &dpll_hw_state,
9072                                           sizeof(dpll_hw_state)),
9073                         "pll hw state mismatch\n");
9074 }
9075
9076 static void
9077 verify_shared_dpll_state(struct intel_crtc *crtc,
9078                          struct intel_crtc_state *old_crtc_state,
9079                          struct intel_crtc_state *new_crtc_state)
9080 {
9081         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9082
9083         if (new_crtc_state->shared_dpll)
9084                 verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
9085
9086         if (old_crtc_state->shared_dpll &&
9087             old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
9088                 u8 pipe_mask = BIT(crtc->pipe);
9089                 struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
9090
9091                 I915_STATE_WARN(pll->active_mask & pipe_mask,
9092                                 "pll active mismatch (didn't expect pipe %c in active mask (0x%x))\n",
9093                                 pipe_name(crtc->pipe), pll->active_mask);
9094                 I915_STATE_WARN(pll->state.pipe_mask & pipe_mask,
9095                                 "pll enabled crtcs mismatch (found %x in enabled mask (0x%x))\n",
9096                                 pipe_name(crtc->pipe), pll->state.pipe_mask);
9097         }
9098 }
9099
9100 static void
9101 intel_modeset_verify_crtc(struct intel_crtc *crtc,
9102                           struct intel_atomic_state *state,
9103                           struct intel_crtc_state *old_crtc_state,
9104                           struct intel_crtc_state *new_crtc_state)
9105 {
9106         if (!intel_crtc_needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
9107                 return;
9108
9109         verify_wm_state(crtc, new_crtc_state);
9110         verify_connector_state(state, crtc);
9111         verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
9112         verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
9113 }
9114
9115 static void
9116 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
9117 {
9118         int i;
9119
9120         for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
9121                 verify_single_dpll_state(dev_priv,
9122                                          &dev_priv->dpll.shared_dplls[i],
9123                                          NULL, NULL);
9124 }
9125
9126 static void
9127 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
9128                               struct intel_atomic_state *state)
9129 {
9130         verify_encoder_state(dev_priv, state);
9131         verify_connector_state(state, NULL);
9132         verify_disabled_dpll_state(dev_priv);
9133 }
9134
9135 int intel_modeset_all_pipes(struct intel_atomic_state *state)
9136 {
9137         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9138         struct intel_crtc *crtc;
9139
9140         /*
9141          * Add all pipes to the state, and force
9142          * a modeset on all the active ones.
9143          */
9144         for_each_intel_crtc(&dev_priv->drm, crtc) {
9145                 struct intel_crtc_state *crtc_state;
9146                 int ret;
9147
9148                 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
9149                 if (IS_ERR(crtc_state))
9150                         return PTR_ERR(crtc_state);
9151
9152                 if (!crtc_state->hw.active ||
9153                     drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
9154                         continue;
9155
9156                 crtc_state->uapi.mode_changed = true;
9157
9158                 ret = drm_atomic_add_affected_connectors(&state->base,
9159                                                          &crtc->base);
9160                 if (ret)
9161                         return ret;
9162
9163                 ret = intel_atomic_add_affected_planes(state, crtc);
9164                 if (ret)
9165                         return ret;
9166
9167                 crtc_state->update_planes |= crtc_state->active_planes;
9168         }
9169
9170         return 0;
9171 }
9172
9173 static void
9174 intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
9175 {
9176         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9177         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9178         struct drm_display_mode adjusted_mode =
9179                 crtc_state->hw.adjusted_mode;
9180
9181         if (crtc_state->vrr.enable) {
9182                 adjusted_mode.crtc_vtotal = crtc_state->vrr.vmax;
9183                 adjusted_mode.crtc_vblank_end = crtc_state->vrr.vmax;
9184                 adjusted_mode.crtc_vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
9185                 crtc->vmax_vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
9186         }
9187
9188         drm_calc_timestamping_constants(&crtc->base, &adjusted_mode);
9189
9190         crtc->mode_flags = crtc_state->mode_flags;
9191
9192         /*
9193          * The scanline counter increments at the leading edge of hsync.
9194          *
9195          * On most platforms it starts counting from vtotal-1 on the
9196          * first active line. That means the scanline counter value is
9197          * always one less than what we would expect. Ie. just after
9198          * start of vblank, which also occurs at start of hsync (on the
9199          * last active line), the scanline counter will read vblank_start-1.
9200          *
9201          * On gen2 the scanline counter starts counting from 1 instead
9202          * of vtotal-1, so we have to subtract one (or rather add vtotal-1
9203          * to keep the value positive), instead of adding one.
9204          *
9205          * On HSW+ the behaviour of the scanline counter depends on the output
9206          * type. For DP ports it behaves like most other platforms, but on HDMI
9207          * there's an extra 1 line difference. So we need to add two instead of
9208          * one to the value.
9209          *
9210          * On VLV/CHV DSI the scanline counter would appear to increment
9211          * approx. 1/3 of a scanline before start of vblank. Unfortunately
9212          * that means we can't tell whether we're in vblank or not while
9213          * we're on that particular line. We must still set scanline_offset
9214          * to 1 so that the vblank timestamps come out correct when we query
9215          * the scanline counter from within the vblank interrupt handler.
9216          * However if queried just before the start of vblank we'll get an
9217          * answer that's slightly in the future.
9218          */
9219         if (DISPLAY_VER(dev_priv) == 2) {
9220                 int vtotal;
9221
9222                 vtotal = adjusted_mode.crtc_vtotal;
9223                 if (adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
9224                         vtotal /= 2;
9225
9226                 crtc->scanline_offset = vtotal - 1;
9227         } else if (HAS_DDI(dev_priv) &&
9228                    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
9229                 crtc->scanline_offset = 2;
9230         } else {
9231                 crtc->scanline_offset = 1;
9232         }
9233 }
9234
9235 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
9236 {
9237         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9238         struct intel_crtc_state *new_crtc_state;
9239         struct intel_crtc *crtc;
9240         int i;
9241
9242         if (!dev_priv->display.crtc_compute_clock)
9243                 return;
9244
9245         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9246                 if (!intel_crtc_needs_modeset(new_crtc_state))
9247                         continue;
9248
9249                 intel_release_shared_dplls(state, crtc);
9250         }
9251 }
9252
9253 /*
9254  * This implements the workaround described in the "notes" section of the mode
9255  * set sequence documentation. When going from no pipes or single pipe to
9256  * multiple pipes, and planes are enabled after the pipe, we need to wait at
9257  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
9258  */
9259 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
9260 {
9261         struct intel_crtc_state *crtc_state;
9262         struct intel_crtc *crtc;
9263         struct intel_crtc_state *first_crtc_state = NULL;
9264         struct intel_crtc_state *other_crtc_state = NULL;
9265         enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
9266         int i;
9267
9268         /* look at all crtc's that are going to be enabled in during modeset */
9269         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9270                 if (!crtc_state->hw.active ||
9271                     !intel_crtc_needs_modeset(crtc_state))
9272                         continue;
9273
9274                 if (first_crtc_state) {
9275                         other_crtc_state = crtc_state;
9276                         break;
9277                 } else {
9278                         first_crtc_state = crtc_state;
9279                         first_pipe = crtc->pipe;
9280                 }
9281         }
9282
9283         /* No workaround needed? */
9284         if (!first_crtc_state)
9285                 return 0;
9286
9287         /* w/a possibly needed, check how many crtc's are already enabled. */
9288         for_each_intel_crtc(state->base.dev, crtc) {
9289                 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
9290                 if (IS_ERR(crtc_state))
9291                         return PTR_ERR(crtc_state);
9292
9293                 crtc_state->hsw_workaround_pipe = INVALID_PIPE;
9294
9295                 if (!crtc_state->hw.active ||
9296                     intel_crtc_needs_modeset(crtc_state))
9297                         continue;
9298
9299                 /* 2 or more enabled crtcs means no need for w/a */
9300                 if (enabled_pipe != INVALID_PIPE)
9301                         return 0;
9302
9303                 enabled_pipe = crtc->pipe;
9304         }
9305
9306         if (enabled_pipe != INVALID_PIPE)
9307                 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
9308         else if (other_crtc_state)
9309                 other_crtc_state->hsw_workaround_pipe = first_pipe;
9310
9311         return 0;
9312 }
9313
9314 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
9315                            u8 active_pipes)
9316 {
9317         const struct intel_crtc_state *crtc_state;
9318         struct intel_crtc *crtc;
9319         int i;
9320
9321         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9322                 if (crtc_state->hw.active)
9323                         active_pipes |= BIT(crtc->pipe);
9324                 else
9325                         active_pipes &= ~BIT(crtc->pipe);
9326         }
9327
9328         return active_pipes;
9329 }
9330
9331 static int intel_modeset_checks(struct intel_atomic_state *state)
9332 {
9333         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9334
9335         state->modeset = true;
9336
9337         if (IS_HASWELL(dev_priv))
9338                 return hsw_mode_set_planes_workaround(state);
9339
9340         return 0;
9341 }
9342
9343 /*
9344  * Handle calculation of various watermark data at the end of the atomic check
9345  * phase.  The code here should be run after the per-crtc and per-plane 'check'
9346  * handlers to ensure that all derived state has been updated.
9347  */
9348 static int calc_watermark_data(struct intel_atomic_state *state)
9349 {
9350         struct drm_device *dev = state->base.dev;
9351         struct drm_i915_private *dev_priv = to_i915(dev);
9352
9353         /* Is there platform-specific watermark information to calculate? */
9354         if (dev_priv->display.compute_global_watermarks)
9355                 return dev_priv->display.compute_global_watermarks(state);
9356
9357         return 0;
9358 }
9359
9360 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
9361                                      struct intel_crtc_state *new_crtc_state)
9362 {
9363         if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
9364                 return;
9365
9366         new_crtc_state->uapi.mode_changed = false;
9367         new_crtc_state->update_pipe = true;
9368 }
9369
9370 static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
9371                                     struct intel_crtc_state *new_crtc_state)
9372 {
9373         /*
9374          * If we're not doing the full modeset we want to
9375          * keep the current M/N values as they may be
9376          * sufficiently different to the computed values
9377          * to cause problems.
9378          *
9379          * FIXME: should really copy more fuzzy state here
9380          */
9381         new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
9382         new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
9383         new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
9384         new_crtc_state->has_drrs = old_crtc_state->has_drrs;
9385 }
9386
9387 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
9388                                           struct intel_crtc *crtc,
9389                                           u8 plane_ids_mask)
9390 {
9391         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9392         struct intel_plane *plane;
9393
9394         for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
9395                 struct intel_plane_state *plane_state;
9396
9397                 if ((plane_ids_mask & BIT(plane->id)) == 0)
9398                         continue;
9399
9400                 plane_state = intel_atomic_get_plane_state(state, plane);
9401                 if (IS_ERR(plane_state))
9402                         return PTR_ERR(plane_state);
9403         }
9404
9405         return 0;
9406 }
9407
9408 int intel_atomic_add_affected_planes(struct intel_atomic_state *state,
9409                                      struct intel_crtc *crtc)
9410 {
9411         const struct intel_crtc_state *old_crtc_state =
9412                 intel_atomic_get_old_crtc_state(state, crtc);
9413         const struct intel_crtc_state *new_crtc_state =
9414                 intel_atomic_get_new_crtc_state(state, crtc);
9415
9416         return intel_crtc_add_planes_to_state(state, crtc,
9417                                               old_crtc_state->enabled_planes |
9418                                               new_crtc_state->enabled_planes);
9419 }
9420
9421 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
9422 {
9423         /* See {hsw,vlv,ivb}_plane_ratio() */
9424         return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
9425                 IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9426                 IS_IVYBRIDGE(dev_priv);
9427 }
9428
9429 static int intel_crtc_add_bigjoiner_planes(struct intel_atomic_state *state,
9430                                            struct intel_crtc *crtc,
9431                                            struct intel_crtc *other)
9432 {
9433         const struct intel_plane_state *plane_state;
9434         struct intel_plane *plane;
9435         u8 plane_ids = 0;
9436         int i;
9437
9438         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9439                 if (plane->pipe == crtc->pipe)
9440                         plane_ids |= BIT(plane->id);
9441         }
9442
9443         return intel_crtc_add_planes_to_state(state, other, plane_ids);
9444 }
9445
9446 static int intel_bigjoiner_add_affected_planes(struct intel_atomic_state *state)
9447 {
9448         const struct intel_crtc_state *crtc_state;
9449         struct intel_crtc *crtc;
9450         int i;
9451
9452         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9453                 int ret;
9454
9455                 if (!crtc_state->bigjoiner)
9456                         continue;
9457
9458                 ret = intel_crtc_add_bigjoiner_planes(state, crtc,
9459                                                       crtc_state->bigjoiner_linked_crtc);
9460                 if (ret)
9461                         return ret;
9462         }
9463
9464         return 0;
9465 }
9466
9467 static int intel_atomic_check_planes(struct intel_atomic_state *state)
9468 {
9469         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9470         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
9471         struct intel_plane_state *plane_state;
9472         struct intel_plane *plane;
9473         struct intel_crtc *crtc;
9474         int i, ret;
9475
9476         ret = icl_add_linked_planes(state);
9477         if (ret)
9478                 return ret;
9479
9480         ret = intel_bigjoiner_add_affected_planes(state);
9481         if (ret)
9482                 return ret;
9483
9484         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9485                 ret = intel_plane_atomic_check(state, plane);
9486                 if (ret) {
9487                         drm_dbg_atomic(&dev_priv->drm,
9488                                        "[PLANE:%d:%s] atomic driver check failed\n",
9489                                        plane->base.base.id, plane->base.name);
9490                         return ret;
9491                 }
9492         }
9493
9494         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9495                                             new_crtc_state, i) {
9496                 u8 old_active_planes, new_active_planes;
9497
9498                 ret = icl_check_nv12_planes(new_crtc_state);
9499                 if (ret)
9500                         return ret;
9501
9502                 /*
9503                  * On some platforms the number of active planes affects
9504                  * the planes' minimum cdclk calculation. Add such planes
9505                  * to the state before we compute the minimum cdclk.
9506                  */
9507                 if (!active_planes_affects_min_cdclk(dev_priv))
9508                         continue;
9509
9510                 old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
9511                 new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
9512
9513                 if (hweight8(old_active_planes) == hweight8(new_active_planes))
9514                         continue;
9515
9516                 ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
9517                 if (ret)
9518                         return ret;
9519         }
9520
9521         return 0;
9522 }
9523
9524 static int intel_atomic_check_cdclk(struct intel_atomic_state *state,
9525                                     bool *need_cdclk_calc)
9526 {
9527         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9528         const struct intel_cdclk_state *old_cdclk_state;
9529         const struct intel_cdclk_state *new_cdclk_state;
9530         struct intel_plane_state *plane_state;
9531         struct intel_bw_state *new_bw_state;
9532         struct intel_plane *plane;
9533         int min_cdclk = 0;
9534         enum pipe pipe;
9535         int ret;
9536         int i;
9537         /*
9538          * active_planes bitmask has been updated, and potentially
9539          * affected planes are part of the state. We can now
9540          * compute the minimum cdclk for each plane.
9541          */
9542         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9543                 ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
9544                 if (ret)
9545                         return ret;
9546         }
9547
9548         old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
9549         new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
9550
9551         if (new_cdclk_state &&
9552             old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk)
9553                 *need_cdclk_calc = true;
9554
9555         ret = dev_priv->display.bw_calc_min_cdclk(state);
9556         if (ret)
9557                 return ret;
9558
9559         new_bw_state = intel_atomic_get_new_bw_state(state);
9560
9561         if (!new_cdclk_state || !new_bw_state)
9562                 return 0;
9563
9564         for_each_pipe(dev_priv, pipe) {
9565                 min_cdclk = max(new_cdclk_state->min_cdclk[pipe], min_cdclk);
9566
9567                 /*
9568                  * Currently do this change only if we need to increase
9569                  */
9570                 if (new_bw_state->min_cdclk > min_cdclk)
9571                         *need_cdclk_calc = true;
9572         }
9573
9574         return 0;
9575 }
9576
9577 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
9578 {
9579         struct intel_crtc_state *crtc_state;
9580         struct intel_crtc *crtc;
9581         int i;
9582
9583         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9584                 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
9585                 int ret;
9586
9587                 ret = intel_crtc_atomic_check(state, crtc);
9588                 if (ret) {
9589                         drm_dbg_atomic(&i915->drm,
9590                                        "[CRTC:%d:%s] atomic driver check failed\n",
9591                                        crtc->base.base.id, crtc->base.name);
9592                         return ret;
9593                 }
9594         }
9595
9596         return 0;
9597 }
9598
9599 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
9600                                                u8 transcoders)
9601 {
9602         const struct intel_crtc_state *new_crtc_state;
9603         struct intel_crtc *crtc;
9604         int i;
9605
9606         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9607                 if (new_crtc_state->hw.enable &&
9608                     transcoders & BIT(new_crtc_state->cpu_transcoder) &&
9609                     intel_crtc_needs_modeset(new_crtc_state))
9610                         return true;
9611         }
9612
9613         return false;
9614 }
9615
9616 static int intel_atomic_check_bigjoiner(struct intel_atomic_state *state,
9617                                         struct intel_crtc *crtc,
9618                                         struct intel_crtc_state *old_crtc_state,
9619                                         struct intel_crtc_state *new_crtc_state)
9620 {
9621         struct intel_crtc_state *slave_crtc_state, *master_crtc_state;
9622         struct intel_crtc *slave, *master;
9623
9624         /* slave being enabled, is master is still claiming this crtc? */
9625         if (old_crtc_state->bigjoiner_slave) {
9626                 slave = crtc;
9627                 master = old_crtc_state->bigjoiner_linked_crtc;
9628                 master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
9629                 if (!master_crtc_state || !intel_crtc_needs_modeset(master_crtc_state))
9630                         goto claimed;
9631         }
9632
9633         if (!new_crtc_state->bigjoiner)
9634                 return 0;
9635
9636         slave = intel_dsc_get_bigjoiner_secondary(crtc);
9637         if (!slave) {
9638                 DRM_DEBUG_KMS("[CRTC:%d:%s] Big joiner configuration requires "
9639                               "CRTC + 1 to be used, doesn't exist\n",
9640                               crtc->base.base.id, crtc->base.name);
9641                 return -EINVAL;
9642         }
9643
9644         new_crtc_state->bigjoiner_linked_crtc = slave;
9645         slave_crtc_state = intel_atomic_get_crtc_state(&state->base, slave);
9646         master = crtc;
9647         if (IS_ERR(slave_crtc_state))
9648                 return PTR_ERR(slave_crtc_state);
9649
9650         /* master being enabled, slave was already configured? */
9651         if (slave_crtc_state->uapi.enable)
9652                 goto claimed;
9653
9654         DRM_DEBUG_KMS("[CRTC:%d:%s] Used as slave for big joiner\n",
9655                       slave->base.base.id, slave->base.name);
9656
9657         return copy_bigjoiner_crtc_state(slave_crtc_state, new_crtc_state);
9658
9659 claimed:
9660         DRM_DEBUG_KMS("[CRTC:%d:%s] Slave is enabled as normal CRTC, but "
9661                       "[CRTC:%d:%s] claiming this CRTC for bigjoiner.\n",
9662                       slave->base.base.id, slave->base.name,
9663                       master->base.base.id, master->base.name);
9664         return -EINVAL;
9665 }
9666
9667 static void kill_bigjoiner_slave(struct intel_atomic_state *state,
9668                                  struct intel_crtc_state *master_crtc_state)
9669 {
9670         struct intel_crtc_state *slave_crtc_state =
9671                 intel_atomic_get_new_crtc_state(state, master_crtc_state->bigjoiner_linked_crtc);
9672
9673         slave_crtc_state->bigjoiner = master_crtc_state->bigjoiner = false;
9674         slave_crtc_state->bigjoiner_slave = master_crtc_state->bigjoiner_slave = false;
9675         slave_crtc_state->bigjoiner_linked_crtc = master_crtc_state->bigjoiner_linked_crtc = NULL;
9676         intel_crtc_copy_uapi_to_hw_state(state, slave_crtc_state);
9677 }
9678
9679 /**
9680  * DOC: asynchronous flip implementation
9681  *
9682  * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
9683  * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
9684  * Correspondingly, support is currently added for primary plane only.
9685  *
9686  * Async flip can only change the plane surface address, so anything else
9687  * changing is rejected from the intel_atomic_check_async() function.
9688  * Once this check is cleared, flip done interrupt is enabled using
9689  * the intel_crtc_enable_flip_done() function.
9690  *
9691  * As soon as the surface address register is written, flip done interrupt is
9692  * generated and the requested events are sent to the usersapce in the interrupt
9693  * handler itself. The timestamp and sequence sent during the flip done event
9694  * correspond to the last vblank and have no relation to the actual time when
9695  * the flip done event was sent.
9696  */
9697 static int intel_atomic_check_async(struct intel_atomic_state *state)
9698 {
9699         struct drm_i915_private *i915 = to_i915(state->base.dev);
9700         const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
9701         const struct intel_plane_state *new_plane_state, *old_plane_state;
9702         struct intel_crtc *crtc;
9703         struct intel_plane *plane;
9704         int i;
9705
9706         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9707                                             new_crtc_state, i) {
9708                 if (intel_crtc_needs_modeset(new_crtc_state)) {
9709                         drm_dbg_kms(&i915->drm, "Modeset Required. Async flip not supported\n");
9710                         return -EINVAL;
9711                 }
9712
9713                 if (!new_crtc_state->hw.active) {
9714                         drm_dbg_kms(&i915->drm, "CRTC inactive\n");
9715                         return -EINVAL;
9716                 }
9717                 if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
9718                         drm_dbg_kms(&i915->drm,
9719                                     "Active planes cannot be changed during async flip\n");
9720                         return -EINVAL;
9721                 }
9722         }
9723
9724         for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
9725                                              new_plane_state, i) {
9726                 /*
9727                  * TODO: Async flip is only supported through the page flip IOCTL
9728                  * as of now. So support currently added for primary plane only.
9729                  * Support for other planes on platforms on which supports
9730                  * this(vlv/chv and icl+) should be added when async flip is
9731                  * enabled in the atomic IOCTL path.
9732                  */
9733                 if (!plane->async_flip)
9734                         return -EINVAL;
9735
9736                 /*
9737                  * FIXME: This check is kept generic for all platforms.
9738                  * Need to verify this for all gen9 and gen10 platforms to enable
9739                  * this selectively if required.
9740                  */
9741                 switch (new_plane_state->hw.fb->modifier) {
9742                 case I915_FORMAT_MOD_X_TILED:
9743                 case I915_FORMAT_MOD_Y_TILED:
9744                 case I915_FORMAT_MOD_Yf_TILED:
9745                         break;
9746                 default:
9747                         drm_dbg_kms(&i915->drm,
9748                                     "Linear memory/CCS does not support async flips\n");
9749                         return -EINVAL;
9750                 }
9751
9752                 if (old_plane_state->view.color_plane[0].stride !=
9753                     new_plane_state->view.color_plane[0].stride) {
9754                         drm_dbg_kms(&i915->drm, "Stride cannot be changed in async flip\n");
9755                         return -EINVAL;
9756                 }
9757
9758                 if (old_plane_state->hw.fb->modifier !=
9759                     new_plane_state->hw.fb->modifier) {
9760                         drm_dbg_kms(&i915->drm,
9761                                     "Framebuffer modifiers cannot be changed in async flip\n");
9762                         return -EINVAL;
9763                 }
9764
9765                 if (old_plane_state->hw.fb->format !=
9766                     new_plane_state->hw.fb->format) {
9767                         drm_dbg_kms(&i915->drm,
9768                                     "Framebuffer format cannot be changed in async flip\n");
9769                         return -EINVAL;
9770                 }
9771
9772                 if (old_plane_state->hw.rotation !=
9773                     new_plane_state->hw.rotation) {
9774                         drm_dbg_kms(&i915->drm, "Rotation cannot be changed in async flip\n");
9775                         return -EINVAL;
9776                 }
9777
9778                 if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
9779                     !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
9780                         drm_dbg_kms(&i915->drm,
9781                                     "Plane size/co-ordinates cannot be changed in async flip\n");
9782                         return -EINVAL;
9783                 }
9784
9785                 if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
9786                         drm_dbg_kms(&i915->drm, "Alpha value cannot be changed in async flip\n");
9787                         return -EINVAL;
9788                 }
9789
9790                 if (old_plane_state->hw.pixel_blend_mode !=
9791                     new_plane_state->hw.pixel_blend_mode) {
9792                         drm_dbg_kms(&i915->drm,
9793                                     "Pixel blend mode cannot be changed in async flip\n");
9794                         return -EINVAL;
9795                 }
9796
9797                 if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
9798                         drm_dbg_kms(&i915->drm,
9799                                     "Color encoding cannot be changed in async flip\n");
9800                         return -EINVAL;
9801                 }
9802
9803                 if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
9804                         drm_dbg_kms(&i915->drm, "Color range cannot be changed in async flip\n");
9805                         return -EINVAL;
9806                 }
9807         }
9808
9809         return 0;
9810 }
9811
9812 static int intel_bigjoiner_add_affected_crtcs(struct intel_atomic_state *state)
9813 {
9814         struct intel_crtc_state *crtc_state;
9815         struct intel_crtc *crtc;
9816         int i;
9817
9818         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9819                 struct intel_crtc_state *linked_crtc_state;
9820                 struct intel_crtc *linked_crtc;
9821                 int ret;
9822
9823                 if (!crtc_state->bigjoiner)
9824                         continue;
9825
9826                 linked_crtc = crtc_state->bigjoiner_linked_crtc;
9827                 linked_crtc_state = intel_atomic_get_crtc_state(&state->base, linked_crtc);
9828                 if (IS_ERR(linked_crtc_state))
9829                         return PTR_ERR(linked_crtc_state);
9830
9831                 if (!intel_crtc_needs_modeset(crtc_state))
9832                         continue;
9833
9834                 linked_crtc_state->uapi.mode_changed = true;
9835
9836                 ret = drm_atomic_add_affected_connectors(&state->base,
9837                                                          &linked_crtc->base);
9838                 if (ret)
9839                         return ret;
9840
9841                 ret = intel_atomic_add_affected_planes(state, linked_crtc);
9842                 if (ret)
9843                         return ret;
9844         }
9845
9846         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9847                 /* Kill old bigjoiner link, we may re-establish afterwards */
9848                 if (intel_crtc_needs_modeset(crtc_state) &&
9849                     crtc_state->bigjoiner && !crtc_state->bigjoiner_slave)
9850                         kill_bigjoiner_slave(state, crtc_state);
9851         }
9852
9853         return 0;
9854 }
9855
9856 /**
9857  * intel_atomic_check - validate state object
9858  * @dev: drm device
9859  * @_state: state to validate
9860  */
9861 static int intel_atomic_check(struct drm_device *dev,
9862                               struct drm_atomic_state *_state)
9863 {
9864         struct drm_i915_private *dev_priv = to_i915(dev);
9865         struct intel_atomic_state *state = to_intel_atomic_state(_state);
9866         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
9867         struct intel_crtc *crtc;
9868         int ret, i;
9869         bool any_ms = false;
9870
9871         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9872                                             new_crtc_state, i) {
9873                 if (new_crtc_state->inherited != old_crtc_state->inherited)
9874                         new_crtc_state->uapi.mode_changed = true;
9875         }
9876
9877         intel_vrr_check_modeset(state);
9878
9879         ret = drm_atomic_helper_check_modeset(dev, &state->base);
9880         if (ret)
9881                 goto fail;
9882
9883         ret = intel_bigjoiner_add_affected_crtcs(state);
9884         if (ret)
9885                 goto fail;
9886
9887         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9888                                             new_crtc_state, i) {
9889                 if (!intel_crtc_needs_modeset(new_crtc_state)) {
9890                         /* Light copy */
9891                         intel_crtc_copy_uapi_to_hw_state_nomodeset(state, new_crtc_state);
9892
9893                         continue;
9894                 }
9895
9896                 if (!new_crtc_state->uapi.enable) {
9897                         if (!new_crtc_state->bigjoiner_slave) {
9898                                 intel_crtc_copy_uapi_to_hw_state(state, new_crtc_state);
9899                                 any_ms = true;
9900                         }
9901                         continue;
9902                 }
9903
9904                 ret = intel_crtc_prepare_cleared_state(state, new_crtc_state);
9905                 if (ret)
9906                         goto fail;
9907
9908                 ret = intel_modeset_pipe_config(state, new_crtc_state);
9909                 if (ret)
9910                         goto fail;
9911
9912                 ret = intel_atomic_check_bigjoiner(state, crtc, old_crtc_state,
9913                                                    new_crtc_state);
9914                 if (ret)
9915                         goto fail;
9916         }
9917
9918         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9919                                             new_crtc_state, i) {
9920                 if (!intel_crtc_needs_modeset(new_crtc_state))
9921                         continue;
9922
9923                 ret = intel_modeset_pipe_config_late(new_crtc_state);
9924                 if (ret)
9925                         goto fail;
9926
9927                 intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
9928         }
9929
9930         /**
9931          * Check if fastset is allowed by external dependencies like other
9932          * pipes and transcoders.
9933          *
9934          * Right now it only forces a fullmodeset when the MST master
9935          * transcoder did not changed but the pipe of the master transcoder
9936          * needs a fullmodeset so all slaves also needs to do a fullmodeset or
9937          * in case of port synced crtcs, if one of the synced crtcs
9938          * needs a full modeset, all other synced crtcs should be
9939          * forced a full modeset.
9940          */
9941         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9942                 if (!new_crtc_state->hw.enable || intel_crtc_needs_modeset(new_crtc_state))
9943                         continue;
9944
9945                 if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
9946                         enum transcoder master = new_crtc_state->mst_master_transcoder;
9947
9948                         if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
9949                                 new_crtc_state->uapi.mode_changed = true;
9950                                 new_crtc_state->update_pipe = false;
9951                         }
9952                 }
9953
9954                 if (is_trans_port_sync_mode(new_crtc_state)) {
9955                         u8 trans = new_crtc_state->sync_mode_slaves_mask;
9956
9957                         if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
9958                                 trans |= BIT(new_crtc_state->master_transcoder);
9959
9960                         if (intel_cpu_transcoders_need_modeset(state, trans)) {
9961                                 new_crtc_state->uapi.mode_changed = true;
9962                                 new_crtc_state->update_pipe = false;
9963                         }
9964                 }
9965
9966                 if (new_crtc_state->bigjoiner) {
9967                         struct intel_crtc_state *linked_crtc_state =
9968                                 intel_atomic_get_new_crtc_state(state, new_crtc_state->bigjoiner_linked_crtc);
9969
9970                         if (intel_crtc_needs_modeset(linked_crtc_state)) {
9971                                 new_crtc_state->uapi.mode_changed = true;
9972                                 new_crtc_state->update_pipe = false;
9973                         }
9974                 }
9975         }
9976
9977         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9978                                             new_crtc_state, i) {
9979                 if (intel_crtc_needs_modeset(new_crtc_state)) {
9980                         any_ms = true;
9981                         continue;
9982                 }
9983
9984                 if (!new_crtc_state->update_pipe)
9985                         continue;
9986
9987                 intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
9988         }
9989
9990         if (any_ms && !check_digital_port_conflicts(state)) {
9991                 drm_dbg_kms(&dev_priv->drm,
9992                             "rejecting conflicting digital port configuration\n");
9993                 ret = -EINVAL;
9994                 goto fail;
9995         }
9996
9997         ret = drm_dp_mst_atomic_check(&state->base);
9998         if (ret)
9999                 goto fail;
10000
10001         ret = intel_atomic_check_planes(state);
10002         if (ret)
10003                 goto fail;
10004
10005         intel_fbc_choose_crtc(dev_priv, state);
10006         ret = calc_watermark_data(state);
10007         if (ret)
10008                 goto fail;
10009
10010         ret = intel_bw_atomic_check(state);
10011         if (ret)
10012                 goto fail;
10013
10014         ret = intel_atomic_check_cdclk(state, &any_ms);
10015         if (ret)
10016                 goto fail;
10017
10018         if (intel_any_crtc_needs_modeset(state))
10019                 any_ms = true;
10020
10021         if (any_ms) {
10022                 ret = intel_modeset_checks(state);
10023                 if (ret)
10024                         goto fail;
10025
10026                 ret = intel_modeset_calc_cdclk(state);
10027                 if (ret)
10028                         return ret;
10029
10030                 intel_modeset_clear_plls(state);
10031         }
10032
10033         ret = intel_atomic_check_crtcs(state);
10034         if (ret)
10035                 goto fail;
10036
10037         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10038                                             new_crtc_state, i) {
10039                 if (new_crtc_state->uapi.async_flip) {
10040                         ret = intel_atomic_check_async(state);
10041                         if (ret)
10042                                 goto fail;
10043                 }
10044
10045                 if (!intel_crtc_needs_modeset(new_crtc_state) &&
10046                     !new_crtc_state->update_pipe)
10047                         continue;
10048
10049                 intel_dump_pipe_config(new_crtc_state, state,
10050                                        intel_crtc_needs_modeset(new_crtc_state) ?
10051                                        "[modeset]" : "[fastset]");
10052         }
10053
10054         return 0;
10055
10056  fail:
10057         if (ret == -EDEADLK)
10058                 return ret;
10059
10060         /*
10061          * FIXME would probably be nice to know which crtc specifically
10062          * caused the failure, in cases where we can pinpoint it.
10063          */
10064         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10065                                             new_crtc_state, i)
10066                 intel_dump_pipe_config(new_crtc_state, state, "[failed]");
10067
10068         return ret;
10069 }
10070
10071 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
10072 {
10073         struct intel_crtc_state *crtc_state;
10074         struct intel_crtc *crtc;
10075         int i, ret;
10076
10077         ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
10078         if (ret < 0)
10079                 return ret;
10080
10081         for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
10082                 bool mode_changed = intel_crtc_needs_modeset(crtc_state);
10083
10084                 if (mode_changed || crtc_state->update_pipe ||
10085                     crtc_state->uapi.color_mgmt_changed) {
10086                         intel_dsb_prepare(crtc_state);
10087                 }
10088         }
10089
10090         return 0;
10091 }
10092
10093 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
10094                                   struct intel_crtc_state *crtc_state)
10095 {
10096         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10097
10098         if (DISPLAY_VER(dev_priv) != 2 || crtc_state->active_planes)
10099                 intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
10100
10101         if (crtc_state->has_pch_encoder) {
10102                 enum pipe pch_transcoder =
10103                         intel_crtc_pch_transcoder(crtc);
10104
10105                 intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
10106         }
10107 }
10108
10109 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
10110                                const struct intel_crtc_state *new_crtc_state)
10111 {
10112         struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
10113         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10114
10115         /*
10116          * Update pipe size and adjust fitter if needed: the reason for this is
10117          * that in compute_mode_changes we check the native mode (not the pfit
10118          * mode) to see if we can flip rather than do a full mode set. In the
10119          * fastboot case, we'll flip, but if we don't update the pipesrc and
10120          * pfit state, we'll end up with a big fb scanned out into the wrong
10121          * sized surface.
10122          */
10123         intel_set_pipe_src_size(new_crtc_state);
10124
10125         /* on skylake this is done by detaching scalers */
10126         if (DISPLAY_VER(dev_priv) >= 9) {
10127                 if (new_crtc_state->pch_pfit.enabled)
10128                         skl_pfit_enable(new_crtc_state);
10129         } else if (HAS_PCH_SPLIT(dev_priv)) {
10130                 if (new_crtc_state->pch_pfit.enabled)
10131                         ilk_pfit_enable(new_crtc_state);
10132                 else if (old_crtc_state->pch_pfit.enabled)
10133                         ilk_pfit_disable(old_crtc_state);
10134         }
10135
10136         /*
10137          * The register is supposedly single buffered so perhaps
10138          * not 100% correct to do this here. But SKL+ calculate
10139          * this based on the adjust pixel rate so pfit changes do
10140          * affect it and so it must be updated for fastsets.
10141          * HSW/BDW only really need this here for fastboot, after
10142          * that the value should not change without a full modeset.
10143          */
10144         if (DISPLAY_VER(dev_priv) >= 9 ||
10145             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
10146                 hsw_set_linetime_wm(new_crtc_state);
10147
10148         if (DISPLAY_VER(dev_priv) >= 11)
10149                 icl_set_pipe_chicken(crtc);
10150 }
10151
10152 static void commit_pipe_pre_planes(struct intel_atomic_state *state,
10153                                    struct intel_crtc *crtc)
10154 {
10155         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10156         const struct intel_crtc_state *old_crtc_state =
10157                 intel_atomic_get_old_crtc_state(state, crtc);
10158         const struct intel_crtc_state *new_crtc_state =
10159                 intel_atomic_get_new_crtc_state(state, crtc);
10160         bool modeset = intel_crtc_needs_modeset(new_crtc_state);
10161
10162         /*
10163          * During modesets pipe configuration was programmed as the
10164          * CRTC was enabled.
10165          */
10166         if (!modeset) {
10167                 if (new_crtc_state->uapi.color_mgmt_changed ||
10168                     new_crtc_state->update_pipe)
10169                         intel_color_commit(new_crtc_state);
10170
10171                 if (DISPLAY_VER(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
10172                         bdw_set_pipemisc(new_crtc_state);
10173
10174                 if (new_crtc_state->update_pipe)
10175                         intel_pipe_fastset(old_crtc_state, new_crtc_state);
10176
10177                 intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
10178         }
10179
10180         if (dev_priv->display.atomic_update_watermarks)
10181                 dev_priv->display.atomic_update_watermarks(state, crtc);
10182 }
10183
10184 static void commit_pipe_post_planes(struct intel_atomic_state *state,
10185                                     struct intel_crtc *crtc)
10186 {
10187         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10188         const struct intel_crtc_state *new_crtc_state =
10189                 intel_atomic_get_new_crtc_state(state, crtc);
10190
10191         /*
10192          * Disable the scaler(s) after the plane(s) so that we don't
10193          * get a catastrophic underrun even if the two operations
10194          * end up happening in two different frames.
10195          */
10196         if (DISPLAY_VER(dev_priv) >= 9 &&
10197             !intel_crtc_needs_modeset(new_crtc_state))
10198                 skl_detach_scalers(new_crtc_state);
10199 }
10200
10201 static void intel_enable_crtc(struct intel_atomic_state *state,
10202                               struct intel_crtc *crtc)
10203 {
10204         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10205         const struct intel_crtc_state *new_crtc_state =
10206                 intel_atomic_get_new_crtc_state(state, crtc);
10207
10208         if (!intel_crtc_needs_modeset(new_crtc_state))
10209                 return;
10210
10211         intel_crtc_update_active_timings(new_crtc_state);
10212
10213         dev_priv->display.crtc_enable(state, crtc);
10214
10215         if (new_crtc_state->bigjoiner_slave)
10216                 return;
10217
10218         /* vblanks work again, re-enable pipe CRC. */
10219         intel_crtc_enable_pipe_crc(crtc);
10220 }
10221
10222 static void intel_update_crtc(struct intel_atomic_state *state,
10223                               struct intel_crtc *crtc)
10224 {
10225         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10226         const struct intel_crtc_state *old_crtc_state =
10227                 intel_atomic_get_old_crtc_state(state, crtc);
10228         struct intel_crtc_state *new_crtc_state =
10229                 intel_atomic_get_new_crtc_state(state, crtc);
10230         bool modeset = intel_crtc_needs_modeset(new_crtc_state);
10231
10232         if (!modeset) {
10233                 if (new_crtc_state->preload_luts &&
10234                     (new_crtc_state->uapi.color_mgmt_changed ||
10235                      new_crtc_state->update_pipe))
10236                         intel_color_load_luts(new_crtc_state);
10237
10238                 intel_pre_plane_update(state, crtc);
10239
10240                 if (new_crtc_state->update_pipe)
10241                         intel_encoders_update_pipe(state, crtc);
10242         }
10243
10244         if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
10245                 intel_fbc_disable(crtc);
10246         else
10247                 intel_fbc_enable(state, crtc);
10248
10249         /* Perform vblank evasion around commit operation */
10250         intel_pipe_update_start(new_crtc_state);
10251
10252         commit_pipe_pre_planes(state, crtc);
10253
10254         if (DISPLAY_VER(dev_priv) >= 9)
10255                 skl_update_planes_on_crtc(state, crtc);
10256         else
10257                 i9xx_update_planes_on_crtc(state, crtc);
10258
10259         commit_pipe_post_planes(state, crtc);
10260
10261         intel_pipe_update_end(new_crtc_state);
10262
10263         /*
10264          * We usually enable FIFO underrun interrupts as part of the
10265          * CRTC enable sequence during modesets.  But when we inherit a
10266          * valid pipe configuration from the BIOS we need to take care
10267          * of enabling them on the CRTC's first fastset.
10268          */
10269         if (new_crtc_state->update_pipe && !modeset &&
10270             old_crtc_state->inherited)
10271                 intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
10272 }
10273
10274 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
10275                                           struct intel_crtc_state *old_crtc_state,
10276                                           struct intel_crtc_state *new_crtc_state,
10277                                           struct intel_crtc *crtc)
10278 {
10279         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10280
10281         drm_WARN_ON(&dev_priv->drm, old_crtc_state->bigjoiner_slave);
10282
10283         intel_crtc_disable_planes(state, crtc);
10284
10285         /*
10286          * We still need special handling for disabling bigjoiner master
10287          * and slaves since for slave we do not have encoder or plls
10288          * so we dont need to disable those.
10289          */
10290         if (old_crtc_state->bigjoiner) {
10291                 intel_crtc_disable_planes(state,
10292                                           old_crtc_state->bigjoiner_linked_crtc);
10293                 old_crtc_state->bigjoiner_linked_crtc->active = false;
10294         }
10295
10296         /*
10297          * We need to disable pipe CRC before disabling the pipe,
10298          * or we race against vblank off.
10299          */
10300         intel_crtc_disable_pipe_crc(crtc);
10301
10302         dev_priv->display.crtc_disable(state, crtc);
10303         crtc->active = false;
10304         intel_fbc_disable(crtc);
10305         intel_disable_shared_dpll(old_crtc_state);
10306
10307         /* FIXME unify this for all platforms */
10308         if (!new_crtc_state->hw.active &&
10309             !HAS_GMCH(dev_priv) &&
10310             dev_priv->display.initial_watermarks)
10311                 dev_priv->display.initial_watermarks(state, crtc);
10312 }
10313
10314 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
10315 {
10316         struct intel_crtc_state *new_crtc_state, *old_crtc_state;
10317         struct intel_crtc *crtc;
10318         u32 handled = 0;
10319         int i;
10320
10321         /* Only disable port sync and MST slaves */
10322         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10323                                             new_crtc_state, i) {
10324                 if (!intel_crtc_needs_modeset(new_crtc_state) || old_crtc_state->bigjoiner)
10325                         continue;
10326
10327                 if (!old_crtc_state->hw.active)
10328                         continue;
10329
10330                 /* In case of Transcoder port Sync master slave CRTCs can be
10331                  * assigned in any order and we need to make sure that
10332                  * slave CRTCs are disabled first and then master CRTC since
10333                  * Slave vblanks are masked till Master Vblanks.
10334                  */
10335                 if (!is_trans_port_sync_slave(old_crtc_state) &&
10336                     !intel_dp_mst_is_slave_trans(old_crtc_state))
10337                         continue;
10338
10339                 intel_pre_plane_update(state, crtc);
10340                 intel_old_crtc_state_disables(state, old_crtc_state,
10341                                               new_crtc_state, crtc);
10342                 handled |= BIT(crtc->pipe);
10343         }
10344
10345         /* Disable everything else left on */
10346         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10347                                             new_crtc_state, i) {
10348                 if (!intel_crtc_needs_modeset(new_crtc_state) ||
10349                     (handled & BIT(crtc->pipe)) ||
10350                     old_crtc_state->bigjoiner_slave)
10351                         continue;
10352
10353                 intel_pre_plane_update(state, crtc);
10354                 if (old_crtc_state->bigjoiner) {
10355                         struct intel_crtc *slave =
10356                                 old_crtc_state->bigjoiner_linked_crtc;
10357
10358                         intel_pre_plane_update(state, slave);
10359                 }
10360
10361                 if (old_crtc_state->hw.active)
10362                         intel_old_crtc_state_disables(state, old_crtc_state,
10363                                                       new_crtc_state, crtc);
10364         }
10365 }
10366
10367 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
10368 {
10369         struct intel_crtc_state *new_crtc_state;
10370         struct intel_crtc *crtc;
10371         int i;
10372
10373         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10374                 if (!new_crtc_state->hw.active)
10375                         continue;
10376
10377                 intel_enable_crtc(state, crtc);
10378                 intel_update_crtc(state, crtc);
10379         }
10380 }
10381
10382 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
10383 {
10384         struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10385         struct intel_crtc *crtc;
10386         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10387         struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
10388         u8 update_pipes = 0, modeset_pipes = 0;
10389         int i;
10390
10391         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10392                 enum pipe pipe = crtc->pipe;
10393
10394                 if (!new_crtc_state->hw.active)
10395                         continue;
10396
10397                 /* ignore allocations for crtc's that have been turned off. */
10398                 if (!intel_crtc_needs_modeset(new_crtc_state)) {
10399                         entries[pipe] = old_crtc_state->wm.skl.ddb;
10400                         update_pipes |= BIT(pipe);
10401                 } else {
10402                         modeset_pipes |= BIT(pipe);
10403                 }
10404         }
10405
10406         /*
10407          * Whenever the number of active pipes changes, we need to make sure we
10408          * update the pipes in the right order so that their ddb allocations
10409          * never overlap with each other between CRTC updates. Otherwise we'll
10410          * cause pipe underruns and other bad stuff.
10411          *
10412          * So first lets enable all pipes that do not need a fullmodeset as
10413          * those don't have any external dependency.
10414          */
10415         while (update_pipes) {
10416                 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10417                                                     new_crtc_state, i) {
10418                         enum pipe pipe = crtc->pipe;
10419
10420                         if ((update_pipes & BIT(pipe)) == 0)
10421                                 continue;
10422
10423                         if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
10424                                                         entries, I915_MAX_PIPES, pipe))
10425                                 continue;
10426
10427                         entries[pipe] = new_crtc_state->wm.skl.ddb;
10428                         update_pipes &= ~BIT(pipe);
10429
10430                         intel_update_crtc(state, crtc);
10431
10432                         /*
10433                          * If this is an already active pipe, it's DDB changed,
10434                          * and this isn't the last pipe that needs updating
10435                          * then we need to wait for a vblank to pass for the
10436                          * new ddb allocation to take effect.
10437                          */
10438                         if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
10439                                                  &old_crtc_state->wm.skl.ddb) &&
10440                             (update_pipes | modeset_pipes))
10441                                 intel_wait_for_vblank(dev_priv, pipe);
10442                 }
10443         }
10444
10445         update_pipes = modeset_pipes;
10446
10447         /*
10448          * Enable all pipes that needs a modeset and do not depends on other
10449          * pipes
10450          */
10451         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10452                 enum pipe pipe = crtc->pipe;
10453
10454                 if ((modeset_pipes & BIT(pipe)) == 0)
10455                         continue;
10456
10457                 if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
10458                     is_trans_port_sync_master(new_crtc_state) ||
10459                     (new_crtc_state->bigjoiner && !new_crtc_state->bigjoiner_slave))
10460                         continue;
10461
10462                 modeset_pipes &= ~BIT(pipe);
10463
10464                 intel_enable_crtc(state, crtc);
10465         }
10466
10467         /*
10468          * Then we enable all remaining pipes that depend on other
10469          * pipes: MST slaves and port sync masters, big joiner master
10470          */
10471         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10472                 enum pipe pipe = crtc->pipe;
10473
10474                 if ((modeset_pipes & BIT(pipe)) == 0)
10475                         continue;
10476
10477                 modeset_pipes &= ~BIT(pipe);
10478
10479                 intel_enable_crtc(state, crtc);
10480         }
10481
10482         /*
10483          * Finally we do the plane updates/etc. for all pipes that got enabled.
10484          */
10485         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10486                 enum pipe pipe = crtc->pipe;
10487
10488                 if ((update_pipes & BIT(pipe)) == 0)
10489                         continue;
10490
10491                 drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
10492                                                                         entries, I915_MAX_PIPES, pipe));
10493
10494                 entries[pipe] = new_crtc_state->wm.skl.ddb;
10495                 update_pipes &= ~BIT(pipe);
10496
10497                 intel_update_crtc(state, crtc);
10498         }
10499
10500         drm_WARN_ON(&dev_priv->drm, modeset_pipes);
10501         drm_WARN_ON(&dev_priv->drm, update_pipes);
10502 }
10503
10504 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
10505 {
10506         struct intel_atomic_state *state, *next;
10507         struct llist_node *freed;
10508
10509         freed = llist_del_all(&dev_priv->atomic_helper.free_list);
10510         llist_for_each_entry_safe(state, next, freed, freed)
10511                 drm_atomic_state_put(&state->base);
10512 }
10513
10514 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
10515 {
10516         struct drm_i915_private *dev_priv =
10517                 container_of(work, typeof(*dev_priv), atomic_helper.free_work);
10518
10519         intel_atomic_helper_free_state(dev_priv);
10520 }
10521
10522 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
10523 {
10524         struct wait_queue_entry wait_fence, wait_reset;
10525         struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
10526
10527         init_wait_entry(&wait_fence, 0);
10528         init_wait_entry(&wait_reset, 0);
10529         for (;;) {
10530                 prepare_to_wait(&intel_state->commit_ready.wait,
10531                                 &wait_fence, TASK_UNINTERRUPTIBLE);
10532                 prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
10533                                               I915_RESET_MODESET),
10534                                 &wait_reset, TASK_UNINTERRUPTIBLE);
10535
10536
10537                 if (i915_sw_fence_done(&intel_state->commit_ready) ||
10538                     test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
10539                         break;
10540
10541                 schedule();
10542         }
10543         finish_wait(&intel_state->commit_ready.wait, &wait_fence);
10544         finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
10545                                   I915_RESET_MODESET),
10546                     &wait_reset);
10547 }
10548
10549 static void intel_cleanup_dsbs(struct intel_atomic_state *state)
10550 {
10551         struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10552         struct intel_crtc *crtc;
10553         int i;
10554
10555         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10556                                             new_crtc_state, i)
10557                 intel_dsb_cleanup(old_crtc_state);
10558 }
10559
10560 static void intel_atomic_cleanup_work(struct work_struct *work)
10561 {
10562         struct intel_atomic_state *state =
10563                 container_of(work, struct intel_atomic_state, base.commit_work);
10564         struct drm_i915_private *i915 = to_i915(state->base.dev);
10565
10566         intel_cleanup_dsbs(state);
10567         drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
10568         drm_atomic_helper_commit_cleanup_done(&state->base);
10569         drm_atomic_state_put(&state->base);
10570
10571         intel_atomic_helper_free_state(i915);
10572 }
10573
10574 static void intel_atomic_prepare_plane_clear_colors(struct intel_atomic_state *state)
10575 {
10576         struct drm_i915_private *i915 = to_i915(state->base.dev);
10577         struct intel_plane *plane;
10578         struct intel_plane_state *plane_state;
10579         int i;
10580
10581         for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
10582                 struct drm_framebuffer *fb = plane_state->hw.fb;
10583                 int ret;
10584
10585                 if (!fb ||
10586                     fb->modifier != I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC)
10587                         continue;
10588
10589                 /*
10590                  * The layout of the fast clear color value expected by HW
10591                  * (the DRM ABI requiring this value to be located in fb at offset 0 of plane#2):
10592                  * - 4 x 4 bytes per-channel value
10593                  *   (in surface type specific float/int format provided by the fb user)
10594                  * - 8 bytes native color value used by the display
10595                  *   (converted/written by GPU during a fast clear operation using the
10596                  *    above per-channel values)
10597                  *
10598                  * The commit's FB prepare hook already ensured that FB obj is pinned and the
10599                  * caller made sure that the object is synced wrt. the related color clear value
10600                  * GPU write on it.
10601                  */
10602                 ret = i915_gem_object_read_from_page(intel_fb_obj(fb),
10603                                                      fb->offsets[2] + 16,
10604                                                      &plane_state->ccval,
10605                                                      sizeof(plane_state->ccval));
10606                 /* The above could only fail if the FB obj has an unexpected backing store type. */
10607                 drm_WARN_ON(&i915->drm, ret);
10608         }
10609 }
10610
10611 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
10612 {
10613         struct drm_device *dev = state->base.dev;
10614         struct drm_i915_private *dev_priv = to_i915(dev);
10615         struct intel_crtc_state *new_crtc_state, *old_crtc_state;
10616         struct intel_crtc *crtc;
10617         u64 put_domains[I915_MAX_PIPES] = {};
10618         intel_wakeref_t wakeref = 0;
10619         int i;
10620
10621         intel_atomic_commit_fence_wait(state);
10622
10623         drm_atomic_helper_wait_for_dependencies(&state->base);
10624
10625         if (state->modeset)
10626                 wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
10627
10628         intel_atomic_prepare_plane_clear_colors(state);
10629
10630         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10631                                             new_crtc_state, i) {
10632                 if (intel_crtc_needs_modeset(new_crtc_state) ||
10633                     new_crtc_state->update_pipe) {
10634
10635                         put_domains[crtc->pipe] =
10636                                 modeset_get_crtc_power_domains(new_crtc_state);
10637                 }
10638         }
10639
10640         intel_commit_modeset_disables(state);
10641
10642         /* FIXME: Eventually get rid of our crtc->config pointer */
10643         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
10644                 crtc->config = new_crtc_state;
10645
10646         if (state->modeset) {
10647                 drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
10648
10649                 intel_set_cdclk_pre_plane_update(state);
10650
10651                 intel_modeset_verify_disabled(dev_priv, state);
10652         }
10653
10654         intel_sagv_pre_plane_update(state);
10655
10656         /* Complete the events for pipes that have now been disabled */
10657         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10658                 bool modeset = intel_crtc_needs_modeset(new_crtc_state);
10659
10660                 /* Complete events for now disable pipes here. */
10661                 if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
10662                         spin_lock_irq(&dev->event_lock);
10663                         drm_crtc_send_vblank_event(&crtc->base,
10664                                                    new_crtc_state->uapi.event);
10665                         spin_unlock_irq(&dev->event_lock);
10666
10667                         new_crtc_state->uapi.event = NULL;
10668                 }
10669         }
10670
10671         if (state->modeset)
10672                 intel_encoders_update_prepare(state);
10673
10674         intel_dbuf_pre_plane_update(state);
10675
10676         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10677                 if (new_crtc_state->uapi.async_flip)
10678                         intel_crtc_enable_flip_done(state, crtc);
10679         }
10680
10681         /* Now enable the clocks, plane, pipe, and connectors that we set up. */
10682         dev_priv->display.commit_modeset_enables(state);
10683
10684         if (state->modeset) {
10685                 intel_encoders_update_complete(state);
10686
10687                 intel_set_cdclk_post_plane_update(state);
10688         }
10689
10690         /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
10691          * already, but still need the state for the delayed optimization. To
10692          * fix this:
10693          * - wrap the optimization/post_plane_update stuff into a per-crtc work.
10694          * - schedule that vblank worker _before_ calling hw_done
10695          * - at the start of commit_tail, cancel it _synchrously
10696          * - switch over to the vblank wait helper in the core after that since
10697          *   we don't need out special handling any more.
10698          */
10699         drm_atomic_helper_wait_for_flip_done(dev, &state->base);
10700
10701         for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10702                 if (new_crtc_state->uapi.async_flip)
10703                         intel_crtc_disable_flip_done(state, crtc);
10704
10705                 if (new_crtc_state->hw.active &&
10706                     !intel_crtc_needs_modeset(new_crtc_state) &&
10707                     !new_crtc_state->preload_luts &&
10708                     (new_crtc_state->uapi.color_mgmt_changed ||
10709                      new_crtc_state->update_pipe))
10710                         intel_color_load_luts(new_crtc_state);
10711         }
10712
10713         /*
10714          * Now that the vblank has passed, we can go ahead and program the
10715          * optimal watermarks on platforms that need two-step watermark
10716          * programming.
10717          *
10718          * TODO: Move this (and other cleanup) to an async worker eventually.
10719          */
10720         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10721                                             new_crtc_state, i) {
10722                 /*
10723                  * Gen2 reports pipe underruns whenever all planes are disabled.
10724                  * So re-enable underrun reporting after some planes get enabled.
10725                  *
10726                  * We do this before .optimize_watermarks() so that we have a
10727                  * chance of catching underruns with the intermediate watermarks
10728                  * vs. the new plane configuration.
10729                  */
10730                 if (DISPLAY_VER(dev_priv) == 2 && planes_enabling(old_crtc_state, new_crtc_state))
10731                         intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
10732
10733                 if (dev_priv->display.optimize_watermarks)
10734                         dev_priv->display.optimize_watermarks(state, crtc);
10735         }
10736
10737         intel_dbuf_post_plane_update(state);
10738
10739         for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10740                 intel_post_plane_update(state, crtc);
10741
10742                 modeset_put_crtc_power_domains(crtc, put_domains[crtc->pipe]);
10743
10744                 intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
10745
10746                 /*
10747                  * DSB cleanup is done in cleanup_work aligning with framebuffer
10748                  * cleanup. So copy and reset the dsb structure to sync with
10749                  * commit_done and later do dsb cleanup in cleanup_work.
10750                  */
10751                 old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
10752         }
10753
10754         /* Underruns don't always raise interrupts, so check manually */
10755         intel_check_cpu_fifo_underruns(dev_priv);
10756         intel_check_pch_fifo_underruns(dev_priv);
10757
10758         if (state->modeset)
10759                 intel_verify_planes(state);
10760
10761         intel_sagv_post_plane_update(state);
10762
10763         drm_atomic_helper_commit_hw_done(&state->base);
10764
10765         if (state->modeset) {
10766                 /* As one of the primary mmio accessors, KMS has a high
10767                  * likelihood of triggering bugs in unclaimed access. After we
10768                  * finish modesetting, see if an error has been flagged, and if
10769                  * so enable debugging for the next modeset - and hope we catch
10770                  * the culprit.
10771                  */
10772                 intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
10773                 intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
10774         }
10775         intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
10776
10777         /*
10778          * Defer the cleanup of the old state to a separate worker to not
10779          * impede the current task (userspace for blocking modesets) that
10780          * are executed inline. For out-of-line asynchronous modesets/flips,
10781          * deferring to a new worker seems overkill, but we would place a
10782          * schedule point (cond_resched()) here anyway to keep latencies
10783          * down.
10784          */
10785         INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
10786         queue_work(system_highpri_wq, &state->base.commit_work);
10787 }
10788
10789 static void intel_atomic_commit_work(struct work_struct *work)
10790 {
10791         struct intel_atomic_state *state =
10792                 container_of(work, struct intel_atomic_state, base.commit_work);
10793
10794         intel_atomic_commit_tail(state);
10795 }
10796
10797 static int __i915_sw_fence_call
10798 intel_atomic_commit_ready(struct i915_sw_fence *fence,
10799                           enum i915_sw_fence_notify notify)
10800 {
10801         struct intel_atomic_state *state =
10802                 container_of(fence, struct intel_atomic_state, commit_ready);
10803
10804         switch (notify) {
10805         case FENCE_COMPLETE:
10806                 /* we do blocking waits in the worker, nothing to do here */
10807                 break;
10808         case FENCE_FREE:
10809                 {
10810                         struct intel_atomic_helper *helper =
10811                                 &to_i915(state->base.dev)->atomic_helper;
10812
10813                         if (llist_add(&state->freed, &helper->free_list))
10814                                 schedule_work(&helper->free_work);
10815                         break;
10816                 }
10817         }
10818
10819         return NOTIFY_DONE;
10820 }
10821
10822 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
10823 {
10824         struct intel_plane_state *old_plane_state, *new_plane_state;
10825         struct intel_plane *plane;
10826         int i;
10827
10828         for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
10829                                              new_plane_state, i)
10830                 intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
10831                                         to_intel_frontbuffer(new_plane_state->hw.fb),
10832                                         plane->frontbuffer_bit);
10833 }
10834
10835 static int intel_atomic_commit(struct drm_device *dev,
10836                                struct drm_atomic_state *_state,
10837                                bool nonblock)
10838 {
10839         struct intel_atomic_state *state = to_intel_atomic_state(_state);
10840         struct drm_i915_private *dev_priv = to_i915(dev);
10841         int ret = 0;
10842
10843         state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
10844
10845         drm_atomic_state_get(&state->base);
10846         i915_sw_fence_init(&state->commit_ready,
10847                            intel_atomic_commit_ready);
10848
10849         /*
10850          * The intel_legacy_cursor_update() fast path takes care
10851          * of avoiding the vblank waits for simple cursor
10852          * movement and flips. For cursor on/off and size changes,
10853          * we want to perform the vblank waits so that watermark
10854          * updates happen during the correct frames. Gen9+ have
10855          * double buffered watermarks and so shouldn't need this.
10856          *
10857          * Unset state->legacy_cursor_update before the call to
10858          * drm_atomic_helper_setup_commit() because otherwise
10859          * drm_atomic_helper_wait_for_flip_done() is a noop and
10860          * we get FIFO underruns because we didn't wait
10861          * for vblank.
10862          *
10863          * FIXME doing watermarks and fb cleanup from a vblank worker
10864          * (assuming we had any) would solve these problems.
10865          */
10866         if (DISPLAY_VER(dev_priv) < 9 && state->base.legacy_cursor_update) {
10867                 struct intel_crtc_state *new_crtc_state;
10868                 struct intel_crtc *crtc;
10869                 int i;
10870
10871                 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
10872                         if (new_crtc_state->wm.need_postvbl_update ||
10873                             new_crtc_state->update_wm_post)
10874                                 state->base.legacy_cursor_update = false;
10875         }
10876
10877         ret = intel_atomic_prepare_commit(state);
10878         if (ret) {
10879                 drm_dbg_atomic(&dev_priv->drm,
10880                                "Preparing state failed with %i\n", ret);
10881                 i915_sw_fence_commit(&state->commit_ready);
10882                 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
10883                 return ret;
10884         }
10885
10886         ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
10887         if (!ret)
10888                 ret = drm_atomic_helper_swap_state(&state->base, true);
10889         if (!ret)
10890                 intel_atomic_swap_global_state(state);
10891
10892         if (ret) {
10893                 struct intel_crtc_state *new_crtc_state;
10894                 struct intel_crtc *crtc;
10895                 int i;
10896
10897                 i915_sw_fence_commit(&state->commit_ready);
10898
10899                 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
10900                         intel_dsb_cleanup(new_crtc_state);
10901
10902                 drm_atomic_helper_cleanup_planes(dev, &state->base);
10903                 intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
10904                 return ret;
10905         }
10906         intel_shared_dpll_swap_state(state);
10907         intel_atomic_track_fbs(state);
10908
10909         drm_atomic_state_get(&state->base);
10910         INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
10911
10912         i915_sw_fence_commit(&state->commit_ready);
10913         if (nonblock && state->modeset) {
10914                 queue_work(dev_priv->modeset_wq, &state->base.commit_work);
10915         } else if (nonblock) {
10916                 queue_work(dev_priv->flip_wq, &state->base.commit_work);
10917         } else {
10918                 if (state->modeset)
10919                         flush_workqueue(dev_priv->modeset_wq);
10920                 intel_atomic_commit_tail(state);
10921         }
10922
10923         return 0;
10924 }
10925
10926 struct wait_rps_boost {
10927         struct wait_queue_entry wait;
10928
10929         struct drm_crtc *crtc;
10930         struct i915_request *request;
10931 };
10932
10933 static int do_rps_boost(struct wait_queue_entry *_wait,
10934                         unsigned mode, int sync, void *key)
10935 {
10936         struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
10937         struct i915_request *rq = wait->request;
10938
10939         /*
10940          * If we missed the vblank, but the request is already running it
10941          * is reasonable to assume that it will complete before the next
10942          * vblank without our intervention, so leave RPS alone.
10943          */
10944         if (!i915_request_started(rq))
10945                 intel_rps_boost(rq);
10946         i915_request_put(rq);
10947
10948         drm_crtc_vblank_put(wait->crtc);
10949
10950         list_del(&wait->wait.entry);
10951         kfree(wait);
10952         return 1;
10953 }
10954
10955 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
10956                                        struct dma_fence *fence)
10957 {
10958         struct wait_rps_boost *wait;
10959
10960         if (!dma_fence_is_i915(fence))
10961                 return;
10962
10963         if (DISPLAY_VER(to_i915(crtc->dev)) < 6)
10964                 return;
10965
10966         if (drm_crtc_vblank_get(crtc))
10967                 return;
10968
10969         wait = kmalloc(sizeof(*wait), GFP_KERNEL);
10970         if (!wait) {
10971                 drm_crtc_vblank_put(crtc);
10972                 return;
10973         }
10974
10975         wait->request = to_request(dma_fence_get(fence));
10976         wait->crtc = crtc;
10977
10978         wait->wait.func = do_rps_boost;
10979         wait->wait.flags = 0;
10980
10981         add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
10982 }
10983
10984 int intel_plane_pin_fb(struct intel_plane_state *plane_state)
10985 {
10986         struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
10987         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
10988         struct drm_framebuffer *fb = plane_state->hw.fb;
10989         struct i915_vma *vma;
10990         bool phys_cursor =
10991                 plane->id == PLANE_CURSOR &&
10992                 INTEL_INFO(dev_priv)->display.cursor_needs_physical;
10993
10994         if (!intel_fb_uses_dpt(fb)) {
10995                 vma = intel_pin_and_fence_fb_obj(fb, phys_cursor,
10996                                                  &plane_state->view.gtt,
10997                                                  intel_plane_uses_fence(plane_state),
10998                                                  &plane_state->flags);
10999                 if (IS_ERR(vma))
11000                         return PTR_ERR(vma);
11001
11002                 plane_state->ggtt_vma = vma;
11003         } else {
11004                 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11005
11006                 vma = intel_dpt_pin(intel_fb->dpt_vm);
11007                 if (IS_ERR(vma))
11008                         return PTR_ERR(vma);
11009
11010                 plane_state->ggtt_vma = vma;
11011
11012                 vma = intel_pin_fb_obj_dpt(fb, &plane_state->view.gtt, false,
11013                                            &plane_state->flags, intel_fb->dpt_vm);
11014                 if (IS_ERR(vma)) {
11015                         intel_dpt_unpin(intel_fb->dpt_vm);
11016                         plane_state->ggtt_vma = NULL;
11017                         return PTR_ERR(vma);
11018                 }
11019
11020                 plane_state->dpt_vma = vma;
11021
11022                 WARN_ON(plane_state->ggtt_vma == plane_state->dpt_vma);
11023         }
11024
11025         return 0;
11026 }
11027
11028 void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
11029 {
11030         struct drm_framebuffer *fb = old_plane_state->hw.fb;
11031         struct i915_vma *vma;
11032
11033         if (!intel_fb_uses_dpt(fb)) {
11034                 vma = fetch_and_zero(&old_plane_state->ggtt_vma);
11035                 if (vma)
11036                         intel_unpin_fb_vma(vma, old_plane_state->flags);
11037         } else {
11038                 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11039
11040                 vma = fetch_and_zero(&old_plane_state->dpt_vma);
11041                 if (vma)
11042                         intel_unpin_fb_vma(vma, old_plane_state->flags);
11043
11044                 vma = fetch_and_zero(&old_plane_state->ggtt_vma);
11045                 if (vma)
11046                         intel_dpt_unpin(intel_fb->dpt_vm);
11047         }
11048 }
11049
11050 /**
11051  * intel_prepare_plane_fb - Prepare fb for usage on plane
11052  * @_plane: drm plane to prepare for
11053  * @_new_plane_state: the plane state being prepared
11054  *
11055  * Prepares a framebuffer for usage on a display plane.  Generally this
11056  * involves pinning the underlying object and updating the frontbuffer tracking
11057  * bits.  Some older platforms need special physical address handling for
11058  * cursor planes.
11059  *
11060  * Returns 0 on success, negative error code on failure.
11061  */
11062 int
11063 intel_prepare_plane_fb(struct drm_plane *_plane,
11064                        struct drm_plane_state *_new_plane_state)
11065 {
11066         struct i915_sched_attr attr = { .priority = I915_PRIORITY_DISPLAY };
11067         struct intel_plane *plane = to_intel_plane(_plane);
11068         struct intel_plane_state *new_plane_state =
11069                 to_intel_plane_state(_new_plane_state);
11070         struct intel_atomic_state *state =
11071                 to_intel_atomic_state(new_plane_state->uapi.state);
11072         struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11073         const struct intel_plane_state *old_plane_state =
11074                 intel_atomic_get_old_plane_state(state, plane);
11075         struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
11076         struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
11077         int ret;
11078
11079         if (old_obj) {
11080                 const struct intel_crtc_state *crtc_state =
11081                         intel_atomic_get_new_crtc_state(state,
11082                                                         to_intel_crtc(old_plane_state->hw.crtc));
11083
11084                 /* Big Hammer, we also need to ensure that any pending
11085                  * MI_WAIT_FOR_EVENT inside a user batch buffer on the
11086                  * current scanout is retired before unpinning the old
11087                  * framebuffer. Note that we rely on userspace rendering
11088                  * into the buffer attached to the pipe they are waiting
11089                  * on. If not, userspace generates a GPU hang with IPEHR
11090                  * point to the MI_WAIT_FOR_EVENT.
11091                  *
11092                  * This should only fail upon a hung GPU, in which case we
11093                  * can safely continue.
11094                  */
11095                 if (intel_crtc_needs_modeset(crtc_state)) {
11096                         ret = i915_sw_fence_await_reservation(&state->commit_ready,
11097                                                               old_obj->base.resv, NULL,
11098                                                               false, 0,
11099                                                               GFP_KERNEL);
11100                         if (ret < 0)
11101                                 return ret;
11102                 }
11103         }
11104
11105         if (new_plane_state->uapi.fence) { /* explicit fencing */
11106                 i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
11107                                              &attr);
11108                 ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
11109                                                     new_plane_state->uapi.fence,
11110                                                     i915_fence_timeout(dev_priv),
11111                                                     GFP_KERNEL);
11112                 if (ret < 0)
11113                         return ret;
11114         }
11115
11116         if (!obj)
11117                 return 0;
11118
11119
11120         ret = intel_plane_pin_fb(new_plane_state);
11121         if (ret)
11122                 return ret;
11123
11124         i915_gem_object_wait_priority(obj, 0, &attr);
11125         i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
11126
11127         if (!new_plane_state->uapi.fence) { /* implicit fencing */
11128                 struct dma_fence *fence;
11129
11130                 ret = i915_sw_fence_await_reservation(&state->commit_ready,
11131                                                       obj->base.resv, NULL,
11132                                                       false,
11133                                                       i915_fence_timeout(dev_priv),
11134                                                       GFP_KERNEL);
11135                 if (ret < 0)
11136                         goto unpin_fb;
11137
11138                 fence = dma_resv_get_excl_unlocked(obj->base.resv);
11139                 if (fence) {
11140                         add_rps_boost_after_vblank(new_plane_state->hw.crtc,
11141                                                    fence);
11142                         dma_fence_put(fence);
11143                 }
11144         } else {
11145                 add_rps_boost_after_vblank(new_plane_state->hw.crtc,
11146                                            new_plane_state->uapi.fence);
11147         }
11148
11149         /*
11150          * We declare pageflips to be interactive and so merit a small bias
11151          * towards upclocking to deliver the frame on time. By only changing
11152          * the RPS thresholds to sample more regularly and aim for higher
11153          * clocks we can hopefully deliver low power workloads (like kodi)
11154          * that are not quite steady state without resorting to forcing
11155          * maximum clocks following a vblank miss (see do_rps_boost()).
11156          */
11157         if (!state->rps_interactive) {
11158                 intel_rps_mark_interactive(&dev_priv->gt.rps, true);
11159                 state->rps_interactive = true;
11160         }
11161
11162         return 0;
11163
11164 unpin_fb:
11165         intel_plane_unpin_fb(new_plane_state);
11166
11167         return ret;
11168 }
11169
11170 /**
11171  * intel_cleanup_plane_fb - Cleans up an fb after plane use
11172  * @plane: drm plane to clean up for
11173  * @_old_plane_state: the state from the previous modeset
11174  *
11175  * Cleans up a framebuffer that has just been removed from a plane.
11176  */
11177 void
11178 intel_cleanup_plane_fb(struct drm_plane *plane,
11179                        struct drm_plane_state *_old_plane_state)
11180 {
11181         struct intel_plane_state *old_plane_state =
11182                 to_intel_plane_state(_old_plane_state);
11183         struct intel_atomic_state *state =
11184                 to_intel_atomic_state(old_plane_state->uapi.state);
11185         struct drm_i915_private *dev_priv = to_i915(plane->dev);
11186         struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
11187
11188         if (!obj)
11189                 return;
11190
11191         if (state->rps_interactive) {
11192                 intel_rps_mark_interactive(&dev_priv->gt.rps, false);
11193                 state->rps_interactive = false;
11194         }
11195
11196         /* Should only be called after a successful intel_prepare_plane_fb()! */
11197         intel_plane_unpin_fb(old_plane_state);
11198 }
11199
11200 /**
11201  * intel_plane_destroy - destroy a plane
11202  * @plane: plane to destroy
11203  *
11204  * Common destruction function for all types of planes (primary, cursor,
11205  * sprite).
11206  */
11207 void intel_plane_destroy(struct drm_plane *plane)
11208 {
11209         drm_plane_cleanup(plane);
11210         kfree(to_intel_plane(plane));
11211 }
11212
11213 static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
11214 {
11215         struct intel_plane *plane;
11216
11217         for_each_intel_plane(&dev_priv->drm, plane) {
11218                 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
11219                                                                   plane->pipe);
11220
11221                 plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
11222         }
11223 }
11224
11225
11226 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
11227                                       struct drm_file *file)
11228 {
11229         struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
11230         struct drm_crtc *drmmode_crtc;
11231         struct intel_crtc *crtc;
11232
11233         drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
11234         if (!drmmode_crtc)
11235                 return -ENOENT;
11236
11237         crtc = to_intel_crtc(drmmode_crtc);
11238         pipe_from_crtc_id->pipe = crtc->pipe;
11239
11240         return 0;
11241 }
11242
11243 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
11244 {
11245         struct drm_device *dev = encoder->base.dev;
11246         struct intel_encoder *source_encoder;
11247         u32 possible_clones = 0;
11248
11249         for_each_intel_encoder(dev, source_encoder) {
11250                 if (encoders_cloneable(encoder, source_encoder))
11251                         possible_clones |= drm_encoder_mask(&source_encoder->base);
11252         }
11253
11254         return possible_clones;
11255 }
11256
11257 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
11258 {
11259         struct drm_device *dev = encoder->base.dev;
11260         struct intel_crtc *crtc;
11261         u32 possible_crtcs = 0;
11262
11263         for_each_intel_crtc(dev, crtc) {
11264                 if (encoder->pipe_mask & BIT(crtc->pipe))
11265                         possible_crtcs |= drm_crtc_mask(&crtc->base);
11266         }
11267
11268         return possible_crtcs;
11269 }
11270
11271 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
11272 {
11273         if (!IS_MOBILE(dev_priv))
11274                 return false;
11275
11276         if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
11277                 return false;
11278
11279         if (IS_IRONLAKE(dev_priv) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
11280                 return false;
11281
11282         return true;
11283 }
11284
11285 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
11286 {
11287         if (DISPLAY_VER(dev_priv) >= 9)
11288                 return false;
11289
11290         if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
11291                 return false;
11292
11293         if (HAS_PCH_LPT_H(dev_priv) &&
11294             intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
11295                 return false;
11296
11297         /* DDI E can't be used if DDI A requires 4 lanes */
11298         if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
11299                 return false;
11300
11301         if (!dev_priv->vbt.int_crt_support)
11302                 return false;
11303
11304         return true;
11305 }
11306
11307 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
11308 {
11309         struct intel_encoder *encoder;
11310         bool dpd_is_edp = false;
11311
11312         intel_pps_unlock_regs_wa(dev_priv);
11313
11314         if (!HAS_DISPLAY(dev_priv))
11315                 return;
11316
11317         if (IS_ALDERLAKE_P(dev_priv)) {
11318                 intel_ddi_init(dev_priv, PORT_A);
11319                 intel_ddi_init(dev_priv, PORT_B);
11320                 intel_ddi_init(dev_priv, PORT_TC1);
11321                 intel_ddi_init(dev_priv, PORT_TC2);
11322                 intel_ddi_init(dev_priv, PORT_TC3);
11323                 intel_ddi_init(dev_priv, PORT_TC4);
11324         } else if (IS_ALDERLAKE_S(dev_priv)) {
11325                 intel_ddi_init(dev_priv, PORT_A);
11326                 intel_ddi_init(dev_priv, PORT_TC1);
11327                 intel_ddi_init(dev_priv, PORT_TC2);
11328                 intel_ddi_init(dev_priv, PORT_TC3);
11329                 intel_ddi_init(dev_priv, PORT_TC4);
11330         } else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv)) {
11331                 intel_ddi_init(dev_priv, PORT_A);
11332                 intel_ddi_init(dev_priv, PORT_B);
11333                 intel_ddi_init(dev_priv, PORT_TC1);
11334                 intel_ddi_init(dev_priv, PORT_TC2);
11335         } else if (DISPLAY_VER(dev_priv) >= 12) {
11336                 intel_ddi_init(dev_priv, PORT_A);
11337                 intel_ddi_init(dev_priv, PORT_B);
11338                 intel_ddi_init(dev_priv, PORT_TC1);
11339                 intel_ddi_init(dev_priv, PORT_TC2);
11340                 intel_ddi_init(dev_priv, PORT_TC3);
11341                 intel_ddi_init(dev_priv, PORT_TC4);
11342                 intel_ddi_init(dev_priv, PORT_TC5);
11343                 intel_ddi_init(dev_priv, PORT_TC6);
11344                 icl_dsi_init(dev_priv);
11345         } else if (IS_JSL_EHL(dev_priv)) {
11346                 intel_ddi_init(dev_priv, PORT_A);
11347                 intel_ddi_init(dev_priv, PORT_B);
11348                 intel_ddi_init(dev_priv, PORT_C);
11349                 intel_ddi_init(dev_priv, PORT_D);
11350                 icl_dsi_init(dev_priv);
11351         } else if (DISPLAY_VER(dev_priv) == 11) {
11352                 intel_ddi_init(dev_priv, PORT_A);
11353                 intel_ddi_init(dev_priv, PORT_B);
11354                 intel_ddi_init(dev_priv, PORT_C);
11355                 intel_ddi_init(dev_priv, PORT_D);
11356                 intel_ddi_init(dev_priv, PORT_E);
11357                 intel_ddi_init(dev_priv, PORT_F);
11358                 icl_dsi_init(dev_priv);
11359         } else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
11360                 intel_ddi_init(dev_priv, PORT_A);
11361                 intel_ddi_init(dev_priv, PORT_B);
11362                 intel_ddi_init(dev_priv, PORT_C);
11363                 vlv_dsi_init(dev_priv);
11364         } else if (DISPLAY_VER(dev_priv) >= 9) {
11365                 intel_ddi_init(dev_priv, PORT_A);
11366                 intel_ddi_init(dev_priv, PORT_B);
11367                 intel_ddi_init(dev_priv, PORT_C);
11368                 intel_ddi_init(dev_priv, PORT_D);
11369                 intel_ddi_init(dev_priv, PORT_E);
11370                 intel_ddi_init(dev_priv, PORT_F);
11371         } else if (HAS_DDI(dev_priv)) {
11372                 u32 found;
11373
11374                 if (intel_ddi_crt_present(dev_priv))
11375                         intel_crt_init(dev_priv);
11376
11377                 /* Haswell uses DDI functions to detect digital outputs. */
11378                 found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
11379                 if (found)
11380                         intel_ddi_init(dev_priv, PORT_A);
11381
11382                 found = intel_de_read(dev_priv, SFUSE_STRAP);
11383                 if (found & SFUSE_STRAP_DDIB_DETECTED)
11384                         intel_ddi_init(dev_priv, PORT_B);
11385                 if (found & SFUSE_STRAP_DDIC_DETECTED)
11386                         intel_ddi_init(dev_priv, PORT_C);
11387                 if (found & SFUSE_STRAP_DDID_DETECTED)
11388                         intel_ddi_init(dev_priv, PORT_D);
11389                 if (found & SFUSE_STRAP_DDIF_DETECTED)
11390                         intel_ddi_init(dev_priv, PORT_F);
11391         } else if (HAS_PCH_SPLIT(dev_priv)) {
11392                 int found;
11393
11394                 /*
11395                  * intel_edp_init_connector() depends on this completing first,
11396                  * to prevent the registration of both eDP and LVDS and the
11397                  * incorrect sharing of the PPS.
11398                  */
11399                 intel_lvds_init(dev_priv);
11400                 intel_crt_init(dev_priv);
11401
11402                 dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
11403
11404                 if (ilk_has_edp_a(dev_priv))
11405                         g4x_dp_init(dev_priv, DP_A, PORT_A);
11406
11407                 if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
11408                         /* PCH SDVOB multiplex with HDMIB */
11409                         found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
11410                         if (!found)
11411                                 g4x_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
11412                         if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
11413                                 g4x_dp_init(dev_priv, PCH_DP_B, PORT_B);
11414                 }
11415
11416                 if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
11417                         g4x_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
11418
11419                 if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
11420                         g4x_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
11421
11422                 if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
11423                         g4x_dp_init(dev_priv, PCH_DP_C, PORT_C);
11424
11425                 if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
11426                         g4x_dp_init(dev_priv, PCH_DP_D, PORT_D);
11427         } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
11428                 bool has_edp, has_port;
11429
11430                 if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
11431                         intel_crt_init(dev_priv);
11432
11433                 /*
11434                  * The DP_DETECTED bit is the latched state of the DDC
11435                  * SDA pin at boot. However since eDP doesn't require DDC
11436                  * (no way to plug in a DP->HDMI dongle) the DDC pins for
11437                  * eDP ports may have been muxed to an alternate function.
11438                  * Thus we can't rely on the DP_DETECTED bit alone to detect
11439                  * eDP ports. Consult the VBT as well as DP_DETECTED to
11440                  * detect eDP ports.
11441                  *
11442                  * Sadly the straps seem to be missing sometimes even for HDMI
11443                  * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
11444                  * and VBT for the presence of the port. Additionally we can't
11445                  * trust the port type the VBT declares as we've seen at least
11446                  * HDMI ports that the VBT claim are DP or eDP.
11447                  */
11448                 has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
11449                 has_port = intel_bios_is_port_present(dev_priv, PORT_B);
11450                 if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
11451                         has_edp &= g4x_dp_init(dev_priv, VLV_DP_B, PORT_B);
11452                 if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
11453                         g4x_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
11454
11455                 has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
11456                 has_port = intel_bios_is_port_present(dev_priv, PORT_C);
11457                 if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
11458                         has_edp &= g4x_dp_init(dev_priv, VLV_DP_C, PORT_C);
11459                 if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
11460                         g4x_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
11461
11462                 if (IS_CHERRYVIEW(dev_priv)) {
11463                         /*
11464                          * eDP not supported on port D,
11465                          * so no need to worry about it
11466                          */
11467                         has_port = intel_bios_is_port_present(dev_priv, PORT_D);
11468                         if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
11469                                 g4x_dp_init(dev_priv, CHV_DP_D, PORT_D);
11470                         if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
11471                                 g4x_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
11472                 }
11473
11474                 vlv_dsi_init(dev_priv);
11475         } else if (IS_PINEVIEW(dev_priv)) {
11476                 intel_lvds_init(dev_priv);
11477                 intel_crt_init(dev_priv);
11478         } else if (IS_DISPLAY_VER(dev_priv, 3, 4)) {
11479                 bool found = false;
11480
11481                 if (IS_MOBILE(dev_priv))
11482                         intel_lvds_init(dev_priv);
11483
11484                 intel_crt_init(dev_priv);
11485
11486                 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
11487                         drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
11488                         found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
11489                         if (!found && IS_G4X(dev_priv)) {
11490                                 drm_dbg_kms(&dev_priv->drm,
11491                                             "probing HDMI on SDVOB\n");
11492                                 g4x_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
11493                         }
11494
11495                         if (!found && IS_G4X(dev_priv))
11496                                 g4x_dp_init(dev_priv, DP_B, PORT_B);
11497                 }
11498
11499                 /* Before G4X SDVOC doesn't have its own detect register */
11500
11501                 if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
11502                         drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
11503                         found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
11504                 }
11505
11506                 if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
11507
11508                         if (IS_G4X(dev_priv)) {
11509                                 drm_dbg_kms(&dev_priv->drm,
11510                                             "probing HDMI on SDVOC\n");
11511                                 g4x_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
11512                         }
11513                         if (IS_G4X(dev_priv))
11514                                 g4x_dp_init(dev_priv, DP_C, PORT_C);
11515                 }
11516
11517                 if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
11518                         g4x_dp_init(dev_priv, DP_D, PORT_D);
11519
11520                 if (SUPPORTS_TV(dev_priv))
11521                         intel_tv_init(dev_priv);
11522         } else if (DISPLAY_VER(dev_priv) == 2) {
11523                 if (IS_I85X(dev_priv))
11524                         intel_lvds_init(dev_priv);
11525
11526                 intel_crt_init(dev_priv);
11527                 intel_dvo_init(dev_priv);
11528         }
11529
11530         for_each_intel_encoder(&dev_priv->drm, encoder) {
11531                 encoder->base.possible_crtcs =
11532                         intel_encoder_possible_crtcs(encoder);
11533                 encoder->base.possible_clones =
11534                         intel_encoder_possible_clones(encoder);
11535         }
11536
11537         intel_init_pch_refclk(dev_priv);
11538
11539         drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
11540 }
11541
11542 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11543 {
11544         struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11545
11546         drm_framebuffer_cleanup(fb);
11547
11548         if (intel_fb_uses_dpt(fb))
11549                 intel_dpt_destroy(intel_fb->dpt_vm);
11550
11551         intel_frontbuffer_put(intel_fb->frontbuffer);
11552
11553         kfree(intel_fb);
11554 }
11555
11556 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
11557                                                 struct drm_file *file,
11558                                                 unsigned int *handle)
11559 {
11560         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11561         struct drm_i915_private *i915 = to_i915(obj->base.dev);
11562
11563         if (i915_gem_object_is_userptr(obj)) {
11564                 drm_dbg(&i915->drm,
11565                         "attempting to use a userptr for a framebuffer, denied\n");
11566                 return -EINVAL;
11567         }
11568
11569         return drm_gem_handle_create(file, &obj->base, handle);
11570 }
11571
11572 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
11573                                         struct drm_file *file,
11574                                         unsigned flags, unsigned color,
11575                                         struct drm_clip_rect *clips,
11576                                         unsigned num_clips)
11577 {
11578         struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11579
11580         i915_gem_object_flush_if_display(obj);
11581         intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
11582
11583         return 0;
11584 }
11585
11586 static const struct drm_framebuffer_funcs intel_fb_funcs = {
11587         .destroy = intel_user_framebuffer_destroy,
11588         .create_handle = intel_user_framebuffer_create_handle,
11589         .dirty = intel_user_framebuffer_dirty,
11590 };
11591
11592 static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
11593                                   struct drm_i915_gem_object *obj,
11594                                   struct drm_mode_fb_cmd2 *mode_cmd)
11595 {
11596         struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
11597         struct drm_framebuffer *fb = &intel_fb->base;
11598         u32 max_stride;
11599         unsigned int tiling, stride;
11600         int ret = -EINVAL;
11601         int i;
11602
11603         intel_fb->frontbuffer = intel_frontbuffer_get(obj);
11604         if (!intel_fb->frontbuffer)
11605                 return -ENOMEM;
11606
11607         i915_gem_object_lock(obj, NULL);
11608         tiling = i915_gem_object_get_tiling(obj);
11609         stride = i915_gem_object_get_stride(obj);
11610         i915_gem_object_unlock(obj);
11611
11612         if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
11613                 /*
11614                  * If there's a fence, enforce that
11615                  * the fb modifier and tiling mode match.
11616                  */
11617                 if (tiling != I915_TILING_NONE &&
11618                     tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
11619                         drm_dbg_kms(&dev_priv->drm,
11620                                     "tiling_mode doesn't match fb modifier\n");
11621                         goto err;
11622                 }
11623         } else {
11624                 if (tiling == I915_TILING_X) {
11625                         mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
11626                 } else if (tiling == I915_TILING_Y) {
11627                         drm_dbg_kms(&dev_priv->drm,
11628                                     "No Y tiling for legacy addfb\n");
11629                         goto err;
11630                 }
11631         }
11632
11633         if (!drm_any_plane_has_format(&dev_priv->drm,
11634                                       mode_cmd->pixel_format,
11635                                       mode_cmd->modifier[0])) {
11636                 drm_dbg_kms(&dev_priv->drm,
11637                             "unsupported pixel format %p4cc / modifier 0x%llx\n",
11638                             &mode_cmd->pixel_format, mode_cmd->modifier[0]);
11639                 goto err;
11640         }
11641
11642         /*
11643          * gen2/3 display engine uses the fence if present,
11644          * so the tiling mode must match the fb modifier exactly.
11645          */
11646         if (DISPLAY_VER(dev_priv) < 4 &&
11647             tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
11648                 drm_dbg_kms(&dev_priv->drm,
11649                             "tiling_mode must match fb modifier exactly on gen2/3\n");
11650                 goto err;
11651         }
11652
11653         max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
11654                                          mode_cmd->modifier[0]);
11655         if (mode_cmd->pitches[0] > max_stride) {
11656                 drm_dbg_kms(&dev_priv->drm,
11657                             "%s pitch (%u) must be at most %d\n",
11658                             mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
11659                             "tiled" : "linear",
11660                             mode_cmd->pitches[0], max_stride);
11661                 goto err;
11662         }
11663
11664         /*
11665          * If there's a fence, enforce that
11666          * the fb pitch and fence stride match.
11667          */
11668         if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
11669                 drm_dbg_kms(&dev_priv->drm,
11670                             "pitch (%d) must match tiling stride (%d)\n",
11671                             mode_cmd->pitches[0], stride);
11672                 goto err;
11673         }
11674
11675         /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11676         if (mode_cmd->offsets[0] != 0) {
11677                 drm_dbg_kms(&dev_priv->drm,
11678                             "plane 0 offset (0x%08x) must be 0\n",
11679                             mode_cmd->offsets[0]);
11680                 goto err;
11681         }
11682
11683         drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
11684
11685         for (i = 0; i < fb->format->num_planes; i++) {
11686                 u32 stride_alignment;
11687
11688                 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
11689                         drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
11690                                     i);
11691                         goto err;
11692                 }
11693
11694                 stride_alignment = intel_fb_stride_alignment(fb, i);
11695                 if (fb->pitches[i] & (stride_alignment - 1)) {
11696                         drm_dbg_kms(&dev_priv->drm,
11697                                     "plane %d pitch (%d) must be at least %u byte aligned\n",
11698                                     i, fb->pitches[i], stride_alignment);
11699                         goto err;
11700                 }
11701
11702                 if (is_gen12_ccs_plane(fb, i) && !is_gen12_ccs_cc_plane(fb, i)) {
11703                         int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
11704
11705                         if (fb->pitches[i] != ccs_aux_stride) {
11706                                 drm_dbg_kms(&dev_priv->drm,
11707                                             "ccs aux plane %d pitch (%d) must be %d\n",
11708                                             i,
11709                                             fb->pitches[i], ccs_aux_stride);
11710                                 goto err;
11711                         }
11712                 }
11713
11714                 /* TODO: Add POT stride remapping support for CCS formats as well. */
11715                 if (IS_ALDERLAKE_P(dev_priv) &&
11716                     mode_cmd->modifier[i] != DRM_FORMAT_MOD_LINEAR &&
11717                     !intel_fb_needs_pot_stride_remap(intel_fb) &&
11718                     !is_power_of_2(mode_cmd->pitches[i])) {
11719                         drm_dbg_kms(&dev_priv->drm,
11720                                     "plane %d pitch (%d) must be power of two for tiled buffers\n",
11721                                     i, mode_cmd->pitches[i]);
11722                         goto err;
11723                 }
11724
11725                 fb->obj[i] = &obj->base;
11726         }
11727
11728         ret = intel_fill_fb_info(dev_priv, intel_fb);
11729         if (ret)
11730                 goto err;
11731
11732         if (intel_fb_uses_dpt(fb)) {
11733                 struct i915_address_space *vm;
11734
11735                 vm = intel_dpt_create(intel_fb);
11736                 if (IS_ERR(vm)) {
11737                         ret = PTR_ERR(vm);
11738                         goto err;
11739                 }
11740
11741                 intel_fb->dpt_vm = vm;
11742         }
11743
11744         ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
11745         if (ret) {
11746                 drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
11747                 goto err;
11748         }
11749
11750         return 0;
11751
11752 err:
11753         intel_frontbuffer_put(intel_fb->frontbuffer);
11754         return ret;
11755 }
11756
11757 static struct drm_framebuffer *
11758 intel_user_framebuffer_create(struct drm_device *dev,
11759                               struct drm_file *filp,
11760                               const struct drm_mode_fb_cmd2 *user_mode_cmd)
11761 {
11762         struct drm_framebuffer *fb;
11763         struct drm_i915_gem_object *obj;
11764         struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
11765         struct drm_i915_private *i915;
11766
11767         obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
11768         if (!obj)
11769                 return ERR_PTR(-ENOENT);
11770
11771         /* object is backed with LMEM for discrete */
11772         i915 = to_i915(obj->base.dev);
11773         if (HAS_LMEM(i915) && !i915_gem_object_is_lmem(obj)) {
11774                 /* object is "remote", not in local memory */
11775                 i915_gem_object_put(obj);
11776                 return ERR_PTR(-EREMOTE);
11777         }
11778
11779         fb = intel_framebuffer_create(obj, &mode_cmd);
11780         i915_gem_object_put(obj);
11781
11782         return fb;
11783 }
11784
11785 static enum drm_mode_status
11786 intel_mode_valid(struct drm_device *dev,
11787                  const struct drm_display_mode *mode)
11788 {
11789         struct drm_i915_private *dev_priv = to_i915(dev);
11790         int hdisplay_max, htotal_max;
11791         int vdisplay_max, vtotal_max;
11792
11793         /*
11794          * Can't reject DBLSCAN here because Xorg ddxen can add piles
11795          * of DBLSCAN modes to the output's mode list when they detect
11796          * the scaling mode property on the connector. And they don't
11797          * ask the kernel to validate those modes in any way until
11798          * modeset time at which point the client gets a protocol error.
11799          * So in order to not upset those clients we silently ignore the
11800          * DBLSCAN flag on such connectors. For other connectors we will
11801          * reject modes with the DBLSCAN flag in encoder->compute_config().
11802          * And we always reject DBLSCAN modes in connector->mode_valid()
11803          * as we never want such modes on the connector's mode list.
11804          */
11805
11806         if (mode->vscan > 1)
11807                 return MODE_NO_VSCAN;
11808
11809         if (mode->flags & DRM_MODE_FLAG_HSKEW)
11810                 return MODE_H_ILLEGAL;
11811
11812         if (mode->flags & (DRM_MODE_FLAG_CSYNC |
11813                            DRM_MODE_FLAG_NCSYNC |
11814                            DRM_MODE_FLAG_PCSYNC))
11815                 return MODE_HSYNC;
11816
11817         if (mode->flags & (DRM_MODE_FLAG_BCAST |
11818                            DRM_MODE_FLAG_PIXMUX |
11819                            DRM_MODE_FLAG_CLKDIV2))
11820                 return MODE_BAD;
11821
11822         /* Transcoder timing limits */
11823         if (DISPLAY_VER(dev_priv) >= 11) {
11824                 hdisplay_max = 16384;
11825                 vdisplay_max = 8192;
11826                 htotal_max = 16384;
11827                 vtotal_max = 8192;
11828         } else if (DISPLAY_VER(dev_priv) >= 9 ||
11829                    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
11830                 hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
11831                 vdisplay_max = 4096;
11832                 htotal_max = 8192;
11833                 vtotal_max = 8192;
11834         } else if (DISPLAY_VER(dev_priv) >= 3) {
11835                 hdisplay_max = 4096;
11836                 vdisplay_max = 4096;
11837                 htotal_max = 8192;
11838                 vtotal_max = 8192;
11839         } else {
11840                 hdisplay_max = 2048;
11841                 vdisplay_max = 2048;
11842                 htotal_max = 4096;
11843                 vtotal_max = 4096;
11844         }
11845
11846         if (mode->hdisplay > hdisplay_max ||
11847             mode->hsync_start > htotal_max ||
11848             mode->hsync_end > htotal_max ||
11849             mode->htotal > htotal_max)
11850                 return MODE_H_ILLEGAL;
11851
11852         if (mode->vdisplay > vdisplay_max ||
11853             mode->vsync_start > vtotal_max ||
11854             mode->vsync_end > vtotal_max ||
11855             mode->vtotal > vtotal_max)
11856                 return MODE_V_ILLEGAL;
11857
11858         if (DISPLAY_VER(dev_priv) >= 5) {
11859                 if (mode->hdisplay < 64 ||
11860                     mode->htotal - mode->hdisplay < 32)
11861                         return MODE_H_ILLEGAL;
11862
11863                 if (mode->vtotal - mode->vdisplay < 5)
11864                         return MODE_V_ILLEGAL;
11865         } else {
11866                 if (mode->htotal - mode->hdisplay < 32)
11867                         return MODE_H_ILLEGAL;
11868
11869                 if (mode->vtotal - mode->vdisplay < 3)
11870                         return MODE_V_ILLEGAL;
11871         }
11872
11873         return MODE_OK;
11874 }
11875
11876 enum drm_mode_status
11877 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
11878                                 const struct drm_display_mode *mode,
11879                                 bool bigjoiner)
11880 {
11881         int plane_width_max, plane_height_max;
11882
11883         /*
11884          * intel_mode_valid() should be
11885          * sufficient on older platforms.
11886          */
11887         if (DISPLAY_VER(dev_priv) < 9)
11888                 return MODE_OK;
11889
11890         /*
11891          * Most people will probably want a fullscreen
11892          * plane so let's not advertize modes that are
11893          * too big for that.
11894          */
11895         if (DISPLAY_VER(dev_priv) >= 11) {
11896                 plane_width_max = 5120 << bigjoiner;
11897                 plane_height_max = 4320;
11898         } else {
11899                 plane_width_max = 5120;
11900                 plane_height_max = 4096;
11901         }
11902
11903         if (mode->hdisplay > plane_width_max)
11904                 return MODE_H_ILLEGAL;
11905
11906         if (mode->vdisplay > plane_height_max)
11907                 return MODE_V_ILLEGAL;
11908
11909         return MODE_OK;
11910 }
11911
11912 static const struct drm_mode_config_funcs intel_mode_funcs = {
11913         .fb_create = intel_user_framebuffer_create,
11914         .get_format_info = intel_get_format_info,
11915         .output_poll_changed = intel_fbdev_output_poll_changed,
11916         .mode_valid = intel_mode_valid,
11917         .atomic_check = intel_atomic_check,
11918         .atomic_commit = intel_atomic_commit,
11919         .atomic_state_alloc = intel_atomic_state_alloc,
11920         .atomic_state_clear = intel_atomic_state_clear,
11921         .atomic_state_free = intel_atomic_state_free,
11922 };
11923
11924 /**
11925  * intel_init_display_hooks - initialize the display modesetting hooks
11926  * @dev_priv: device private
11927  */
11928 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
11929 {
11930         if (!HAS_DISPLAY(dev_priv))
11931                 return;
11932
11933         intel_init_cdclk_hooks(dev_priv);
11934         intel_init_audio_hooks(dev_priv);
11935
11936         intel_dpll_init_clock_hook(dev_priv);
11937
11938         if (DISPLAY_VER(dev_priv) >= 9) {
11939                 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
11940                 dev_priv->display.crtc_enable = hsw_crtc_enable;
11941                 dev_priv->display.crtc_disable = hsw_crtc_disable;
11942         } else if (HAS_DDI(dev_priv)) {
11943                 dev_priv->display.get_pipe_config = hsw_get_pipe_config;
11944                 dev_priv->display.crtc_enable = hsw_crtc_enable;
11945                 dev_priv->display.crtc_disable = hsw_crtc_disable;
11946         } else if (HAS_PCH_SPLIT(dev_priv)) {
11947                 dev_priv->display.get_pipe_config = ilk_get_pipe_config;
11948                 dev_priv->display.crtc_enable = ilk_crtc_enable;
11949                 dev_priv->display.crtc_disable = ilk_crtc_disable;
11950         } else if (IS_CHERRYVIEW(dev_priv) ||
11951                    IS_VALLEYVIEW(dev_priv)) {
11952                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
11953                 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11954                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11955         } else {
11956                 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
11957                 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11958                 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11959         }
11960
11961         intel_fdi_init_hook(dev_priv);
11962
11963         if (DISPLAY_VER(dev_priv) >= 9) {
11964                 dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
11965                 dev_priv->display.get_initial_plane_config = skl_get_initial_plane_config;
11966         } else {
11967                 dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
11968                 dev_priv->display.get_initial_plane_config = i9xx_get_initial_plane_config;
11969         }
11970
11971 }
11972
11973 void intel_modeset_init_hw(struct drm_i915_private *i915)
11974 {
11975         struct intel_cdclk_state *cdclk_state;
11976
11977         if (!HAS_DISPLAY(i915))
11978                 return;
11979
11980         cdclk_state = to_intel_cdclk_state(i915->cdclk.obj.state);
11981
11982         intel_update_cdclk(i915);
11983         intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
11984         cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
11985 }
11986
11987 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
11988 {
11989         struct drm_plane *plane;
11990         struct intel_crtc *crtc;
11991
11992         for_each_intel_crtc(state->dev, crtc) {
11993                 struct intel_crtc_state *crtc_state;
11994
11995                 crtc_state = intel_atomic_get_crtc_state(state, crtc);
11996                 if (IS_ERR(crtc_state))
11997                         return PTR_ERR(crtc_state);
11998
11999                 if (crtc_state->hw.active) {
12000                         /*
12001                          * Preserve the inherited flag to avoid
12002                          * taking the full modeset path.
12003                          */
12004                         crtc_state->inherited = true;
12005                 }
12006         }
12007
12008         drm_for_each_plane(plane, state->dev) {
12009                 struct drm_plane_state *plane_state;
12010
12011                 plane_state = drm_atomic_get_plane_state(state, plane);
12012                 if (IS_ERR(plane_state))
12013                         return PTR_ERR(plane_state);
12014         }
12015
12016         return 0;
12017 }
12018
12019 /*
12020  * Calculate what we think the watermarks should be for the state we've read
12021  * out of the hardware and then immediately program those watermarks so that
12022  * we ensure the hardware settings match our internal state.
12023  *
12024  * We can calculate what we think WM's should be by creating a duplicate of the
12025  * current state (which was constructed during hardware readout) and running it
12026  * through the atomic check code to calculate new watermark values in the
12027  * state object.
12028  */
12029 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
12030 {
12031         struct drm_atomic_state *state;
12032         struct intel_atomic_state *intel_state;
12033         struct intel_crtc *crtc;
12034         struct intel_crtc_state *crtc_state;
12035         struct drm_modeset_acquire_ctx ctx;
12036         int ret;
12037         int i;
12038
12039         /* Only supported on platforms that use atomic watermark design */
12040         if (!dev_priv->display.optimize_watermarks)
12041                 return;
12042
12043         state = drm_atomic_state_alloc(&dev_priv->drm);
12044         if (drm_WARN_ON(&dev_priv->drm, !state))
12045                 return;
12046
12047         intel_state = to_intel_atomic_state(state);
12048
12049         drm_modeset_acquire_init(&ctx, 0);
12050
12051 retry:
12052         state->acquire_ctx = &ctx;
12053
12054         /*
12055          * Hardware readout is the only time we don't want to calculate
12056          * intermediate watermarks (since we don't trust the current
12057          * watermarks).
12058          */
12059         if (!HAS_GMCH(dev_priv))
12060                 intel_state->skip_intermediate_wm = true;
12061
12062         ret = sanitize_watermarks_add_affected(state);
12063         if (ret)
12064                 goto fail;
12065
12066         ret = intel_atomic_check(&dev_priv->drm, state);
12067         if (ret)
12068                 goto fail;
12069
12070         /* Write calculated watermark values back */
12071         for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
12072                 crtc_state->wm.need_postvbl_update = true;
12073                 dev_priv->display.optimize_watermarks(intel_state, crtc);
12074
12075                 to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
12076         }
12077
12078 fail:
12079         if (ret == -EDEADLK) {
12080                 drm_atomic_state_clear(state);
12081                 drm_modeset_backoff(&ctx);
12082                 goto retry;
12083         }
12084
12085         /*
12086          * If we fail here, it means that the hardware appears to be
12087          * programmed in a way that shouldn't be possible, given our
12088          * understanding of watermark requirements.  This might mean a
12089          * mistake in the hardware readout code or a mistake in the
12090          * watermark calculations for a given platform.  Raise a WARN
12091          * so that this is noticeable.
12092          *
12093          * If this actually happens, we'll have to just leave the
12094          * BIOS-programmed watermarks untouched and hope for the best.
12095          */
12096         drm_WARN(&dev_priv->drm, ret,
12097                  "Could not determine valid watermarks for inherited state\n");
12098
12099         drm_atomic_state_put(state);
12100
12101         drm_modeset_drop_locks(&ctx);
12102         drm_modeset_acquire_fini(&ctx);
12103 }
12104
12105 static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
12106 {
12107         if (IS_IRONLAKE(dev_priv)) {
12108                 u32 fdi_pll_clk =
12109                         intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
12110
12111                 dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
12112         } else if (IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
12113                 dev_priv->fdi_pll_freq = 270000;
12114         } else {
12115                 return;
12116         }
12117
12118         drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
12119 }
12120
12121 static int intel_initial_commit(struct drm_device *dev)
12122 {
12123         struct drm_atomic_state *state = NULL;
12124         struct drm_modeset_acquire_ctx ctx;
12125         struct intel_crtc *crtc;
12126         int ret = 0;
12127
12128         state = drm_atomic_state_alloc(dev);
12129         if (!state)
12130                 return -ENOMEM;
12131
12132         drm_modeset_acquire_init(&ctx, 0);
12133
12134 retry:
12135         state->acquire_ctx = &ctx;
12136
12137         for_each_intel_crtc(dev, crtc) {
12138                 struct intel_crtc_state *crtc_state =
12139                         intel_atomic_get_crtc_state(state, crtc);
12140
12141                 if (IS_ERR(crtc_state)) {
12142                         ret = PTR_ERR(crtc_state);
12143                         goto out;
12144                 }
12145
12146                 if (crtc_state->hw.active) {
12147                         struct intel_encoder *encoder;
12148
12149                         /*
12150                          * We've not yet detected sink capabilities
12151                          * (audio,infoframes,etc.) and thus we don't want to
12152                          * force a full state recomputation yet. We want that to
12153                          * happen only for the first real commit from userspace.
12154                          * So preserve the inherited flag for the time being.
12155                          */
12156                         crtc_state->inherited = true;
12157
12158                         ret = drm_atomic_add_affected_planes(state, &crtc->base);
12159                         if (ret)
12160                                 goto out;
12161
12162                         /*
12163                          * FIXME hack to force a LUT update to avoid the
12164                          * plane update forcing the pipe gamma on without
12165                          * having a proper LUT loaded. Remove once we
12166                          * have readout for pipe gamma enable.
12167                          */
12168                         crtc_state->uapi.color_mgmt_changed = true;
12169
12170                         for_each_intel_encoder_mask(dev, encoder,
12171                                                     crtc_state->uapi.encoder_mask) {
12172                                 if (encoder->initial_fastset_check &&
12173                                     !encoder->initial_fastset_check(encoder, crtc_state)) {
12174                                         ret = drm_atomic_add_affected_connectors(state,
12175                                                                                  &crtc->base);
12176                                         if (ret)
12177                                                 goto out;
12178                                 }
12179                         }
12180                 }
12181         }
12182
12183         ret = drm_atomic_commit(state);
12184
12185 out:
12186         if (ret == -EDEADLK) {
12187                 drm_atomic_state_clear(state);
12188                 drm_modeset_backoff(&ctx);
12189                 goto retry;
12190         }
12191
12192         drm_atomic_state_put(state);
12193
12194         drm_modeset_drop_locks(&ctx);
12195         drm_modeset_acquire_fini(&ctx);
12196
12197         return ret;
12198 }
12199
12200 static void intel_mode_config_init(struct drm_i915_private *i915)
12201 {
12202         struct drm_mode_config *mode_config = &i915->drm.mode_config;
12203
12204         drm_mode_config_init(&i915->drm);
12205         INIT_LIST_HEAD(&i915->global_obj_list);
12206
12207         mode_config->min_width = 0;
12208         mode_config->min_height = 0;
12209
12210         mode_config->preferred_depth = 24;
12211         mode_config->prefer_shadow = 1;
12212
12213         mode_config->funcs = &intel_mode_funcs;
12214
12215         mode_config->async_page_flip = has_async_flips(i915);
12216
12217         /*
12218          * Maximum framebuffer dimensions, chosen to match
12219          * the maximum render engine surface size on gen4+.
12220          */
12221         if (DISPLAY_VER(i915) >= 7) {
12222                 mode_config->max_width = 16384;
12223                 mode_config->max_height = 16384;
12224         } else if (DISPLAY_VER(i915) >= 4) {
12225                 mode_config->max_width = 8192;
12226                 mode_config->max_height = 8192;
12227         } else if (DISPLAY_VER(i915) == 3) {
12228                 mode_config->max_width = 4096;
12229                 mode_config->max_height = 4096;
12230         } else {
12231                 mode_config->max_width = 2048;
12232                 mode_config->max_height = 2048;
12233         }
12234
12235         if (IS_I845G(i915) || IS_I865G(i915)) {
12236                 mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
12237                 mode_config->cursor_height = 1023;
12238         } else if (IS_I830(i915) || IS_I85X(i915) ||
12239                    IS_I915G(i915) || IS_I915GM(i915)) {
12240                 mode_config->cursor_width = 64;
12241                 mode_config->cursor_height = 64;
12242         } else {
12243                 mode_config->cursor_width = 256;
12244                 mode_config->cursor_height = 256;
12245         }
12246 }
12247
12248 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
12249 {
12250         intel_atomic_global_obj_cleanup(i915);
12251         drm_mode_config_cleanup(&i915->drm);
12252 }
12253
12254 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
12255 {
12256         if (plane_config->fb) {
12257                 struct drm_framebuffer *fb = &plane_config->fb->base;
12258
12259                 /* We may only have the stub and not a full framebuffer */
12260                 if (drm_framebuffer_read_refcount(fb))
12261                         drm_framebuffer_put(fb);
12262                 else
12263                         kfree(fb);
12264         }
12265
12266         if (plane_config->vma)
12267                 i915_vma_put(plane_config->vma);
12268 }
12269
12270 /* part #1: call before irq install */
12271 int intel_modeset_init_noirq(struct drm_i915_private *i915)
12272 {
12273         int ret;
12274
12275         if (i915_inject_probe_failure(i915))
12276                 return -ENODEV;
12277
12278         if (HAS_DISPLAY(i915)) {
12279                 ret = drm_vblank_init(&i915->drm,
12280                                       INTEL_NUM_PIPES(i915));
12281                 if (ret)
12282                         return ret;
12283         }
12284
12285         intel_bios_init(i915);
12286
12287         ret = intel_vga_register(i915);
12288         if (ret)
12289                 goto cleanup_bios;
12290
12291         /* FIXME: completely on the wrong abstraction layer */
12292         intel_power_domains_init_hw(i915, false);
12293
12294         if (!HAS_DISPLAY(i915))
12295                 return 0;
12296
12297         intel_dmc_ucode_init(i915);
12298
12299         i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
12300         i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
12301                                         WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
12302
12303         i915->framestart_delay = 1; /* 1-4 */
12304
12305         i915->window2_delay = 0; /* No DSB so no window2 delay */
12306
12307         intel_mode_config_init(i915);
12308
12309         ret = intel_cdclk_init(i915);
12310         if (ret)
12311                 goto cleanup_vga_client_pw_domain_dmc;
12312
12313         ret = intel_dbuf_init(i915);
12314         if (ret)
12315                 goto cleanup_vga_client_pw_domain_dmc;
12316
12317         ret = intel_bw_init(i915);
12318         if (ret)
12319                 goto cleanup_vga_client_pw_domain_dmc;
12320
12321         init_llist_head(&i915->atomic_helper.free_list);
12322         INIT_WORK(&i915->atomic_helper.free_work,
12323                   intel_atomic_helper_free_state_worker);
12324
12325         intel_init_quirks(i915);
12326
12327         intel_fbc_init(i915);
12328
12329         return 0;
12330
12331 cleanup_vga_client_pw_domain_dmc:
12332         intel_dmc_ucode_fini(i915);
12333         intel_power_domains_driver_remove(i915);
12334         intel_vga_unregister(i915);
12335 cleanup_bios:
12336         intel_bios_driver_remove(i915);
12337
12338         return ret;
12339 }
12340
12341 /* part #2: call after irq install, but before gem init */
12342 int intel_modeset_init_nogem(struct drm_i915_private *i915)
12343 {
12344         struct drm_device *dev = &i915->drm;
12345         enum pipe pipe;
12346         struct intel_crtc *crtc;
12347         int ret;
12348
12349         if (!HAS_DISPLAY(i915))
12350                 return 0;
12351
12352         intel_init_pm(i915);
12353
12354         intel_panel_sanitize_ssc(i915);
12355
12356         intel_pps_setup(i915);
12357
12358         intel_gmbus_setup(i915);
12359
12360         drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
12361                     INTEL_NUM_PIPES(i915),
12362                     INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
12363
12364         for_each_pipe(i915, pipe) {
12365                 ret = intel_crtc_init(i915, pipe);
12366                 if (ret) {
12367                         intel_mode_config_cleanup(i915);
12368                         return ret;
12369                 }
12370         }
12371
12372         intel_plane_possible_crtcs_init(i915);
12373         intel_shared_dpll_init(dev);
12374         intel_update_fdi_pll_freq(i915);
12375
12376         intel_update_czclk(i915);
12377         intel_modeset_init_hw(i915);
12378         intel_dpll_update_ref_clks(i915);
12379
12380         intel_hdcp_component_init(i915);
12381
12382         if (i915->max_cdclk_freq == 0)
12383                 intel_update_max_cdclk(i915);
12384
12385         /*
12386          * If the platform has HTI, we need to find out whether it has reserved
12387          * any display resources before we create our display outputs.
12388          */
12389         if (INTEL_INFO(i915)->display.has_hti)
12390                 i915->hti_state = intel_de_read(i915, HDPORT_STATE);
12391
12392         /* Just disable it once at startup */
12393         intel_vga_disable(i915);
12394         intel_setup_outputs(i915);
12395
12396         drm_modeset_lock_all(dev);
12397         intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
12398         drm_modeset_unlock_all(dev);
12399
12400         for_each_intel_crtc(dev, crtc) {
12401                 struct intel_initial_plane_config plane_config = {};
12402
12403                 if (!to_intel_crtc_state(crtc->base.state)->uapi.active)
12404                         continue;
12405
12406                 /*
12407                  * Note that reserving the BIOS fb up front prevents us
12408                  * from stuffing other stolen allocations like the ring
12409                  * on top.  This prevents some ugliness at boot time, and
12410                  * can even allow for smooth boot transitions if the BIOS
12411                  * fb is large enough for the active pipe configuration.
12412                  */
12413                 i915->display.get_initial_plane_config(crtc, &plane_config);
12414
12415                 /*
12416                  * If the fb is shared between multiple heads, we'll
12417                  * just get the first one.
12418                  */
12419                 intel_find_initial_plane_obj(crtc, &plane_config);
12420
12421                 plane_config_fini(&plane_config);
12422         }
12423
12424         /*
12425          * Make sure hardware watermarks really match the state we read out.
12426          * Note that we need to do this after reconstructing the BIOS fb's
12427          * since the watermark calculation done here will use pstate->fb.
12428          */
12429         if (!HAS_GMCH(i915))
12430                 sanitize_watermarks(i915);
12431
12432         return 0;
12433 }
12434
12435 /* part #3: call after gem init */
12436 int intel_modeset_init(struct drm_i915_private *i915)
12437 {
12438         int ret;
12439
12440         if (!HAS_DISPLAY(i915))
12441                 return 0;
12442
12443         /*
12444          * Force all active planes to recompute their states. So that on
12445          * mode_setcrtc after probe, all the intel_plane_state variables
12446          * are already calculated and there is no assert_plane warnings
12447          * during bootup.
12448          */
12449         ret = intel_initial_commit(&i915->drm);
12450         if (ret)
12451                 drm_dbg_kms(&i915->drm, "Initial modeset failed, %d\n", ret);
12452
12453         intel_overlay_setup(i915);
12454
12455         ret = intel_fbdev_init(&i915->drm);
12456         if (ret)
12457                 return ret;
12458
12459         /* Only enable hotplug handling once the fbdev is fully set up. */
12460         intel_hpd_init(i915);
12461         intel_hpd_poll_disable(i915);
12462
12463         intel_init_ipc(i915);
12464
12465         return 0;
12466 }
12467
12468 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
12469 {
12470         struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12471         /* 640x480@60Hz, ~25175 kHz */
12472         struct dpll clock = {
12473                 .m1 = 18,
12474                 .m2 = 7,
12475                 .p1 = 13,
12476                 .p2 = 4,
12477                 .n = 2,
12478         };
12479         u32 dpll, fp;
12480         int i;
12481
12482         drm_WARN_ON(&dev_priv->drm,
12483                     i9xx_calc_dpll_params(48000, &clock) != 25154);
12484
12485         drm_dbg_kms(&dev_priv->drm,
12486                     "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
12487                     pipe_name(pipe), clock.vco, clock.dot);
12488
12489         fp = i9xx_dpll_compute_fp(&clock);
12490         dpll = DPLL_DVO_2X_MODE |
12491                 DPLL_VGA_MODE_DIS |
12492                 ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
12493                 PLL_P2_DIVIDE_BY_4 |
12494                 PLL_REF_INPUT_DREFCLK |
12495                 DPLL_VCO_ENABLE;
12496
12497         intel_de_write(dev_priv, FP0(pipe), fp);
12498         intel_de_write(dev_priv, FP1(pipe), fp);
12499
12500         intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
12501         intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
12502         intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
12503         intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
12504         intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
12505         intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
12506         intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
12507
12508         /*
12509          * Apparently we need to have VGA mode enabled prior to changing
12510          * the P1/P2 dividers. Otherwise the DPLL will keep using the old
12511          * dividers, even though the register value does change.
12512          */
12513         intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
12514         intel_de_write(dev_priv, DPLL(pipe), dpll);
12515
12516         /* Wait for the clocks to stabilize. */
12517         intel_de_posting_read(dev_priv, DPLL(pipe));
12518         udelay(150);
12519
12520         /* The pixel multiplier can only be updated once the
12521          * DPLL is enabled and the clocks are stable.
12522          *
12523          * So write it again.
12524          */
12525         intel_de_write(dev_priv, DPLL(pipe), dpll);
12526
12527         /* We do this three times for luck */
12528         for (i = 0; i < 3 ; i++) {
12529                 intel_de_write(dev_priv, DPLL(pipe), dpll);
12530                 intel_de_posting_read(dev_priv, DPLL(pipe));
12531                 udelay(150); /* wait for warmup */
12532         }
12533
12534         intel_de_write(dev_priv, PIPECONF(pipe),
12535                        PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
12536         intel_de_posting_read(dev_priv, PIPECONF(pipe));
12537
12538         intel_wait_for_pipe_scanline_moving(crtc);
12539 }
12540
12541 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
12542 {
12543         struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12544
12545         drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
12546                     pipe_name(pipe));
12547
12548         drm_WARN_ON(&dev_priv->drm,
12549                     intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
12550                     DISPLAY_PLANE_ENABLE);
12551         drm_WARN_ON(&dev_priv->drm,
12552                     intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
12553                     DISPLAY_PLANE_ENABLE);
12554         drm_WARN_ON(&dev_priv->drm,
12555                     intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
12556                     DISPLAY_PLANE_ENABLE);
12557         drm_WARN_ON(&dev_priv->drm,
12558                     intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
12559         drm_WARN_ON(&dev_priv->drm,
12560                     intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
12561
12562         intel_de_write(dev_priv, PIPECONF(pipe), 0);
12563         intel_de_posting_read(dev_priv, PIPECONF(pipe));
12564
12565         intel_wait_for_pipe_scanline_stopped(crtc);
12566
12567         intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
12568         intel_de_posting_read(dev_priv, DPLL(pipe));
12569 }
12570
12571 static void
12572 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
12573 {
12574         struct intel_crtc *crtc;
12575
12576         if (DISPLAY_VER(dev_priv) >= 4)
12577                 return;
12578
12579         for_each_intel_crtc(&dev_priv->drm, crtc) {
12580                 struct intel_plane *plane =
12581                         to_intel_plane(crtc->base.primary);
12582                 struct intel_crtc *plane_crtc;
12583                 enum pipe pipe;
12584
12585                 if (!plane->get_hw_state(plane, &pipe))
12586                         continue;
12587
12588                 if (pipe == crtc->pipe)
12589                         continue;
12590
12591                 drm_dbg_kms(&dev_priv->drm,
12592                             "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
12593                             plane->base.base.id, plane->base.name);
12594
12595                 plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12596                 intel_plane_disable_noatomic(plane_crtc, plane);
12597         }
12598 }
12599
12600 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
12601 {
12602         struct drm_device *dev = crtc->base.dev;
12603         struct intel_encoder *encoder;
12604
12605         for_each_encoder_on_crtc(dev, &crtc->base, encoder)
12606                 return true;
12607
12608         return false;
12609 }
12610
12611 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
12612 {
12613         struct drm_device *dev = encoder->base.dev;
12614         struct intel_connector *connector;
12615
12616         for_each_connector_on_encoder(dev, &encoder->base, connector)
12617                 return connector;
12618
12619         return NULL;
12620 }
12621
12622 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
12623                               enum pipe pch_transcoder)
12624 {
12625         return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
12626                 (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
12627 }
12628
12629 static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
12630 {
12631         struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12632         struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12633         enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
12634
12635         if (DISPLAY_VER(dev_priv) >= 9 ||
12636             IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12637                 i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
12638                 u32 val;
12639
12640                 if (transcoder_is_dsi(cpu_transcoder))
12641                         return;
12642
12643                 val = intel_de_read(dev_priv, reg);
12644                 val &= ~HSW_FRAME_START_DELAY_MASK;
12645                 val |= HSW_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12646                 intel_de_write(dev_priv, reg, val);
12647         } else {
12648                 i915_reg_t reg = PIPECONF(cpu_transcoder);
12649                 u32 val;
12650
12651                 val = intel_de_read(dev_priv, reg);
12652                 val &= ~PIPECONF_FRAME_START_DELAY_MASK;
12653                 val |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12654                 intel_de_write(dev_priv, reg, val);
12655         }
12656
12657         if (!crtc_state->has_pch_encoder)
12658                 return;
12659
12660         if (HAS_PCH_IBX(dev_priv)) {
12661                 i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
12662                 u32 val;
12663
12664                 val = intel_de_read(dev_priv, reg);
12665                 val &= ~TRANS_FRAME_START_DELAY_MASK;
12666                 val |= TRANS_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12667                 intel_de_write(dev_priv, reg, val);
12668         } else {
12669                 enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
12670                 i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
12671                 u32 val;
12672
12673                 val = intel_de_read(dev_priv, reg);
12674                 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
12675                 val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12676                 intel_de_write(dev_priv, reg, val);
12677         }
12678 }
12679
12680 static void intel_sanitize_crtc(struct intel_crtc *crtc,
12681                                 struct drm_modeset_acquire_ctx *ctx)
12682 {
12683         struct drm_device *dev = crtc->base.dev;
12684         struct drm_i915_private *dev_priv = to_i915(dev);
12685         struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
12686
12687         if (crtc_state->hw.active) {
12688                 struct intel_plane *plane;
12689
12690                 /* Clear any frame start delays used for debugging left by the BIOS */
12691                 intel_sanitize_frame_start_delay(crtc_state);
12692
12693                 /* Disable everything but the primary plane */
12694                 for_each_intel_plane_on_crtc(dev, crtc, plane) {
12695                         const struct intel_plane_state *plane_state =
12696                                 to_intel_plane_state(plane->base.state);
12697
12698                         if (plane_state->uapi.visible &&
12699                             plane->base.type != DRM_PLANE_TYPE_PRIMARY)
12700                                 intel_plane_disable_noatomic(crtc, plane);
12701                 }
12702
12703                 /*
12704                  * Disable any background color set by the BIOS, but enable the
12705                  * gamma and CSC to match how we program our planes.
12706                  */
12707                 if (DISPLAY_VER(dev_priv) >= 9)
12708                         intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
12709                                        SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
12710         }
12711
12712         /* Adjust the state of the output pipe according to whether we
12713          * have active connectors/encoders. */
12714         if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc) &&
12715             !crtc_state->bigjoiner_slave)
12716                 intel_crtc_disable_noatomic(crtc, ctx);
12717
12718         if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
12719                 /*
12720                  * We start out with underrun reporting disabled to avoid races.
12721                  * For correct bookkeeping mark this on active crtcs.
12722                  *
12723                  * Also on gmch platforms we dont have any hardware bits to
12724                  * disable the underrun reporting. Which means we need to start
12725                  * out with underrun reporting disabled also on inactive pipes,
12726                  * since otherwise we'll complain about the garbage we read when
12727                  * e.g. coming up after runtime pm.
12728                  *
12729                  * No protection against concurrent access is required - at
12730                  * worst a fifo underrun happens which also sets this to false.
12731                  */
12732                 crtc->cpu_fifo_underrun_disabled = true;
12733                 /*
12734                  * We track the PCH trancoder underrun reporting state
12735                  * within the crtc. With crtc for pipe A housing the underrun
12736                  * reporting state for PCH transcoder A, crtc for pipe B housing
12737                  * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
12738                  * and marking underrun reporting as disabled for the non-existing
12739                  * PCH transcoders B and C would prevent enabling the south
12740                  * error interrupt (see cpt_can_enable_serr_int()).
12741                  */
12742                 if (has_pch_trancoder(dev_priv, crtc->pipe))
12743                         crtc->pch_fifo_underrun_disabled = true;
12744         }
12745 }
12746
12747 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
12748 {
12749         struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
12750
12751         /*
12752          * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
12753          * the hardware when a high res displays plugged in. DPLL P
12754          * divider is zero, and the pipe timings are bonkers. We'll
12755          * try to disable everything in that case.
12756          *
12757          * FIXME would be nice to be able to sanitize this state
12758          * without several WARNs, but for now let's take the easy
12759          * road.
12760          */
12761         return IS_SANDYBRIDGE(dev_priv) &&
12762                 crtc_state->hw.active &&
12763                 crtc_state->shared_dpll &&
12764                 crtc_state->port_clock == 0;
12765 }
12766
12767 static void intel_sanitize_encoder(struct intel_encoder *encoder)
12768 {
12769         struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
12770         struct intel_connector *connector;
12771         struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
12772         struct intel_crtc_state *crtc_state = crtc ?
12773                 to_intel_crtc_state(crtc->base.state) : NULL;
12774
12775         /* We need to check both for a crtc link (meaning that the
12776          * encoder is active and trying to read from a pipe) and the
12777          * pipe itself being active. */
12778         bool has_active_crtc = crtc_state &&
12779                 crtc_state->hw.active;
12780
12781         if (crtc_state && has_bogus_dpll_config(crtc_state)) {
12782                 drm_dbg_kms(&dev_priv->drm,
12783                             "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
12784                             pipe_name(crtc->pipe));
12785                 has_active_crtc = false;
12786         }
12787
12788         connector = intel_encoder_find_connector(encoder);
12789         if (connector && !has_active_crtc) {
12790                 drm_dbg_kms(&dev_priv->drm,
12791                             "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12792                             encoder->base.base.id,
12793                             encoder->base.name);
12794
12795                 /* Connector is active, but has no active pipe. This is
12796                  * fallout from our resume register restoring. Disable
12797                  * the encoder manually again. */
12798                 if (crtc_state) {
12799                         struct drm_encoder *best_encoder;
12800
12801                         drm_dbg_kms(&dev_priv->drm,
12802                                     "[ENCODER:%d:%s] manually disabled\n",
12803                                     encoder->base.base.id,
12804                                     encoder->base.name);
12805
12806                         /* avoid oopsing in case the hooks consult best_encoder */
12807                         best_encoder = connector->base.state->best_encoder;
12808                         connector->base.state->best_encoder = &encoder->base;
12809
12810                         /* FIXME NULL atomic state passed! */
12811                         if (encoder->disable)
12812                                 encoder->disable(NULL, encoder, crtc_state,
12813                                                  connector->base.state);
12814                         if (encoder->post_disable)
12815                                 encoder->post_disable(NULL, encoder, crtc_state,
12816                                                       connector->base.state);
12817
12818                         connector->base.state->best_encoder = best_encoder;
12819                 }
12820                 encoder->base.crtc = NULL;
12821
12822                 /* Inconsistent output/port/pipe state happens presumably due to
12823                  * a bug in one of the get_hw_state functions. Or someplace else
12824                  * in our code, like the register restore mess on resume. Clamp
12825                  * things to off as a safer default. */
12826
12827                 connector->base.dpms = DRM_MODE_DPMS_OFF;
12828                 connector->base.encoder = NULL;
12829         }
12830
12831         /* notify opregion of the sanitized encoder state */
12832         intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
12833
12834         if (HAS_DDI(dev_priv))
12835                 intel_ddi_sanitize_encoder_pll_mapping(encoder);
12836 }
12837
12838 /* FIXME read out full plane state for all planes */
12839 static void readout_plane_state(struct drm_i915_private *dev_priv)
12840 {
12841         struct intel_plane *plane;
12842         struct intel_crtc *crtc;
12843
12844         for_each_intel_plane(&dev_priv->drm, plane) {
12845                 struct intel_plane_state *plane_state =
12846                         to_intel_plane_state(plane->base.state);
12847                 struct intel_crtc_state *crtc_state;
12848                 enum pipe pipe = PIPE_A;
12849                 bool visible;
12850
12851                 visible = plane->get_hw_state(plane, &pipe);
12852
12853                 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12854                 crtc_state = to_intel_crtc_state(crtc->base.state);
12855
12856                 intel_set_plane_visible(crtc_state, plane_state, visible);
12857
12858                 drm_dbg_kms(&dev_priv->drm,
12859                             "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
12860                             plane->base.base.id, plane->base.name,
12861                             enableddisabled(visible), pipe_name(pipe));
12862         }
12863
12864         for_each_intel_crtc(&dev_priv->drm, crtc) {
12865                 struct intel_crtc_state *crtc_state =
12866                         to_intel_crtc_state(crtc->base.state);
12867
12868                 fixup_plane_bitmasks(crtc_state);
12869         }
12870 }
12871
12872 static void intel_modeset_readout_hw_state(struct drm_device *dev)
12873 {
12874         struct drm_i915_private *dev_priv = to_i915(dev);
12875         struct intel_cdclk_state *cdclk_state =
12876                 to_intel_cdclk_state(dev_priv->cdclk.obj.state);
12877         struct intel_dbuf_state *dbuf_state =
12878                 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
12879         enum pipe pipe;
12880         struct intel_crtc *crtc;
12881         struct intel_encoder *encoder;
12882         struct intel_connector *connector;
12883         struct drm_connector_list_iter conn_iter;
12884         u8 active_pipes = 0;
12885
12886         for_each_intel_crtc(dev, crtc) {
12887                 struct intel_crtc_state *crtc_state =
12888                         to_intel_crtc_state(crtc->base.state);
12889
12890                 __drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
12891                 intel_crtc_free_hw_state(crtc_state);
12892                 intel_crtc_state_reset(crtc_state, crtc);
12893
12894                 intel_crtc_get_pipe_config(crtc_state);
12895
12896                 crtc_state->hw.enable = crtc_state->hw.active;
12897
12898                 crtc->base.enabled = crtc_state->hw.enable;
12899                 crtc->active = crtc_state->hw.active;
12900
12901                 if (crtc_state->hw.active)
12902                         active_pipes |= BIT(crtc->pipe);
12903
12904                 drm_dbg_kms(&dev_priv->drm,
12905                             "[CRTC:%d:%s] hw state readout: %s\n",
12906                             crtc->base.base.id, crtc->base.name,
12907                             enableddisabled(crtc_state->hw.active));
12908         }
12909
12910         dev_priv->active_pipes = cdclk_state->active_pipes =
12911                 dbuf_state->active_pipes = active_pipes;
12912
12913         readout_plane_state(dev_priv);
12914
12915         for_each_intel_encoder(dev, encoder) {
12916                 pipe = 0;
12917
12918                 if (encoder->get_hw_state(encoder, &pipe)) {
12919                         struct intel_crtc_state *crtc_state;
12920
12921                         crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12922                         crtc_state = to_intel_crtc_state(crtc->base.state);
12923
12924                         encoder->base.crtc = &crtc->base;
12925                         intel_encoder_get_config(encoder, crtc_state);
12926                         if (encoder->sync_state)
12927                                 encoder->sync_state(encoder, crtc_state);
12928
12929                         /* read out to slave crtc as well for bigjoiner */
12930                         if (crtc_state->bigjoiner) {
12931                                 /* encoder should read be linked to bigjoiner master */
12932                                 WARN_ON(crtc_state->bigjoiner_slave);
12933
12934                                 crtc = crtc_state->bigjoiner_linked_crtc;
12935                                 crtc_state = to_intel_crtc_state(crtc->base.state);
12936                                 intel_encoder_get_config(encoder, crtc_state);
12937                         }
12938                 } else {
12939                         encoder->base.crtc = NULL;
12940                 }
12941
12942                 drm_dbg_kms(&dev_priv->drm,
12943                             "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
12944                             encoder->base.base.id, encoder->base.name,
12945                             enableddisabled(encoder->base.crtc),
12946                             pipe_name(pipe));
12947         }
12948
12949         intel_dpll_readout_hw_state(dev_priv);
12950
12951         drm_connector_list_iter_begin(dev, &conn_iter);
12952         for_each_intel_connector_iter(connector, &conn_iter) {
12953                 if (connector->get_hw_state(connector)) {
12954                         struct intel_crtc_state *crtc_state;
12955                         struct intel_crtc *crtc;
12956
12957                         connector->base.dpms = DRM_MODE_DPMS_ON;
12958
12959                         encoder = intel_attached_encoder(connector);
12960                         connector->base.encoder = &encoder->base;
12961
12962                         crtc = to_intel_crtc(encoder->base.crtc);
12963                         crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
12964
12965                         if (crtc_state && crtc_state->hw.active) {
12966                                 /*
12967                                  * This has to be done during hardware readout
12968                                  * because anything calling .crtc_disable may
12969                                  * rely on the connector_mask being accurate.
12970                                  */
12971                                 crtc_state->uapi.connector_mask |=
12972                                         drm_connector_mask(&connector->base);
12973                                 crtc_state->uapi.encoder_mask |=
12974                                         drm_encoder_mask(&encoder->base);
12975                         }
12976                 } else {
12977                         connector->base.dpms = DRM_MODE_DPMS_OFF;
12978                         connector->base.encoder = NULL;
12979                 }
12980                 drm_dbg_kms(&dev_priv->drm,
12981                             "[CONNECTOR:%d:%s] hw state readout: %s\n",
12982                             connector->base.base.id, connector->base.name,
12983                             enableddisabled(connector->base.encoder));
12984         }
12985         drm_connector_list_iter_end(&conn_iter);
12986
12987         for_each_intel_crtc(dev, crtc) {
12988                 struct intel_bw_state *bw_state =
12989                         to_intel_bw_state(dev_priv->bw_obj.state);
12990                 struct intel_crtc_state *crtc_state =
12991                         to_intel_crtc_state(crtc->base.state);
12992                 struct intel_plane *plane;
12993                 int min_cdclk = 0;
12994
12995                 if (crtc_state->bigjoiner_slave)
12996                         continue;
12997
12998                 if (crtc_state->hw.active) {
12999                         /*
13000                          * The initial mode needs to be set in order to keep
13001                          * the atomic core happy. It wants a valid mode if the
13002                          * crtc's enabled, so we do the above call.
13003                          *
13004                          * But we don't set all the derived state fully, hence
13005                          * set a flag to indicate that a full recalculation is
13006                          * needed on the next commit.
13007                          */
13008                         crtc_state->inherited = true;
13009
13010                         intel_crtc_update_active_timings(crtc_state);
13011
13012                         intel_crtc_copy_hw_to_uapi_state(crtc_state);
13013                 }
13014
13015                 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
13016                         const struct intel_plane_state *plane_state =
13017                                 to_intel_plane_state(plane->base.state);
13018
13019                         /*
13020                          * FIXME don't have the fb yet, so can't
13021                          * use intel_plane_data_rate() :(
13022                          */
13023                         if (plane_state->uapi.visible)
13024                                 crtc_state->data_rate[plane->id] =
13025                                         4 * crtc_state->pixel_rate;
13026                         /*
13027                          * FIXME don't have the fb yet, so can't
13028                          * use plane->min_cdclk() :(
13029                          */
13030                         if (plane_state->uapi.visible && plane->min_cdclk) {
13031                                 if (crtc_state->double_wide || DISPLAY_VER(dev_priv) >= 10)
13032                                         crtc_state->min_cdclk[plane->id] =
13033                                                 DIV_ROUND_UP(crtc_state->pixel_rate, 2);
13034                                 else
13035                                         crtc_state->min_cdclk[plane->id] =
13036                                                 crtc_state->pixel_rate;
13037                         }
13038                         drm_dbg_kms(&dev_priv->drm,
13039                                     "[PLANE:%d:%s] min_cdclk %d kHz\n",
13040                                     plane->base.base.id, plane->base.name,
13041                                     crtc_state->min_cdclk[plane->id]);
13042                 }
13043
13044                 if (crtc_state->hw.active) {
13045                         min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
13046                         if (drm_WARN_ON(dev, min_cdclk < 0))
13047                                 min_cdclk = 0;
13048                 }
13049
13050                 cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
13051                 cdclk_state->min_voltage_level[crtc->pipe] =
13052                         crtc_state->min_voltage_level;
13053
13054                 intel_bw_crtc_update(bw_state, crtc_state);
13055
13056                 intel_pipe_config_sanity_check(dev_priv, crtc_state);
13057
13058                 /* discard our incomplete slave state, copy it from master */
13059                 if (crtc_state->bigjoiner && crtc_state->hw.active) {
13060                         struct intel_crtc *slave = crtc_state->bigjoiner_linked_crtc;
13061                         struct intel_crtc_state *slave_crtc_state =
13062                                 to_intel_crtc_state(slave->base.state);
13063
13064                         copy_bigjoiner_crtc_state(slave_crtc_state, crtc_state);
13065                         slave->base.mode = crtc->base.mode;
13066
13067                         cdclk_state->min_cdclk[slave->pipe] = min_cdclk;
13068                         cdclk_state->min_voltage_level[slave->pipe] =
13069                                 crtc_state->min_voltage_level;
13070
13071                         for_each_intel_plane_on_crtc(&dev_priv->drm, slave, plane) {
13072                                 const struct intel_plane_state *plane_state =
13073                                         to_intel_plane_state(plane->base.state);
13074
13075                                 /*
13076                                  * FIXME don't have the fb yet, so can't
13077                                  * use intel_plane_data_rate() :(
13078                                  */
13079                                 if (plane_state->uapi.visible)
13080                                         crtc_state->data_rate[plane->id] =
13081                                                 4 * crtc_state->pixel_rate;
13082                                 else
13083                                         crtc_state->data_rate[plane->id] = 0;
13084                         }
13085
13086                         intel_bw_crtc_update(bw_state, slave_crtc_state);
13087                         drm_calc_timestamping_constants(&slave->base,
13088                                                         &slave_crtc_state->hw.adjusted_mode);
13089                 }
13090         }
13091 }
13092
13093 static void
13094 get_encoder_power_domains(struct drm_i915_private *dev_priv)
13095 {
13096         struct intel_encoder *encoder;
13097
13098         for_each_intel_encoder(&dev_priv->drm, encoder) {
13099                 struct intel_crtc_state *crtc_state;
13100
13101                 if (!encoder->get_power_domains)
13102                         continue;
13103
13104                 /*
13105                  * MST-primary and inactive encoders don't have a crtc state
13106                  * and neither of these require any power domain references.
13107                  */
13108                 if (!encoder->base.crtc)
13109                         continue;
13110
13111                 crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
13112                 encoder->get_power_domains(encoder, crtc_state);
13113         }
13114 }
13115
13116 static void intel_early_display_was(struct drm_i915_private *dev_priv)
13117 {
13118         /*
13119          * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
13120          * Also known as Wa_14010480278.
13121          */
13122         if (IS_DISPLAY_VER(dev_priv, 10, 12))
13123                 intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
13124                                intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
13125
13126         if (IS_HASWELL(dev_priv)) {
13127                 /*
13128                  * WaRsPkgCStateDisplayPMReq:hsw
13129                  * System hang if this isn't done before disabling all planes!
13130                  */
13131                 intel_de_write(dev_priv, CHICKEN_PAR1_1,
13132                                intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
13133         }
13134
13135         if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv)) {
13136                 /* Display WA #1142:kbl,cfl,cml */
13137                 intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
13138                              KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
13139                 intel_de_rmw(dev_priv, CHICKEN_MISC_2,
13140                              KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
13141                              KBL_ARB_FILL_SPARE_14);
13142         }
13143 }
13144
13145 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
13146                                        enum port port, i915_reg_t hdmi_reg)
13147 {
13148         u32 val = intel_de_read(dev_priv, hdmi_reg);
13149
13150         if (val & SDVO_ENABLE ||
13151             (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
13152                 return;
13153
13154         drm_dbg_kms(&dev_priv->drm,
13155                     "Sanitizing transcoder select for HDMI %c\n",
13156                     port_name(port));
13157
13158         val &= ~SDVO_PIPE_SEL_MASK;
13159         val |= SDVO_PIPE_SEL(PIPE_A);
13160
13161         intel_de_write(dev_priv, hdmi_reg, val);
13162 }
13163
13164 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
13165                                      enum port port, i915_reg_t dp_reg)
13166 {
13167         u32 val = intel_de_read(dev_priv, dp_reg);
13168
13169         if (val & DP_PORT_EN ||
13170             (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
13171                 return;
13172
13173         drm_dbg_kms(&dev_priv->drm,
13174                     "Sanitizing transcoder select for DP %c\n",
13175                     port_name(port));
13176
13177         val &= ~DP_PIPE_SEL_MASK;
13178         val |= DP_PIPE_SEL(PIPE_A);
13179
13180         intel_de_write(dev_priv, dp_reg, val);
13181 }
13182
13183 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
13184 {
13185         /*
13186          * The BIOS may select transcoder B on some of the PCH
13187          * ports even it doesn't enable the port. This would trip
13188          * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
13189          * Sanitize the transcoder select bits to prevent that. We
13190          * assume that the BIOS never actually enabled the port,
13191          * because if it did we'd actually have to toggle the port
13192          * on and back off to make the transcoder A select stick
13193          * (see. intel_dp_link_down(), intel_disable_hdmi(),
13194          * intel_disable_sdvo()).
13195          */
13196         ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
13197         ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
13198         ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
13199
13200         /* PCH SDVOB multiplex with HDMIB */
13201         ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
13202         ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
13203         ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
13204 }
13205
13206 /* Scan out the current hw modeset state,
13207  * and sanitizes it to the current state
13208  */
13209 static void
13210 intel_modeset_setup_hw_state(struct drm_device *dev,
13211                              struct drm_modeset_acquire_ctx *ctx)
13212 {
13213         struct drm_i915_private *dev_priv = to_i915(dev);
13214         struct intel_encoder *encoder;
13215         struct intel_crtc *crtc;
13216         intel_wakeref_t wakeref;
13217
13218         wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
13219
13220         intel_early_display_was(dev_priv);
13221         intel_modeset_readout_hw_state(dev);
13222
13223         /* HW state is read out, now we need to sanitize this mess. */
13224
13225         /* Sanitize the TypeC port mode upfront, encoders depend on this */
13226         for_each_intel_encoder(dev, encoder) {
13227                 enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
13228
13229                 /* We need to sanitize only the MST primary port. */
13230                 if (encoder->type != INTEL_OUTPUT_DP_MST &&
13231                     intel_phy_is_tc(dev_priv, phy))
13232                         intel_tc_port_sanitize(enc_to_dig_port(encoder));
13233         }
13234
13235         get_encoder_power_domains(dev_priv);
13236
13237         if (HAS_PCH_IBX(dev_priv))
13238                 ibx_sanitize_pch_ports(dev_priv);
13239
13240         /*
13241          * intel_sanitize_plane_mapping() may need to do vblank
13242          * waits, so we need vblank interrupts restored beforehand.
13243          */
13244         for_each_intel_crtc(&dev_priv->drm, crtc) {
13245                 struct intel_crtc_state *crtc_state =
13246                         to_intel_crtc_state(crtc->base.state);
13247
13248                 drm_crtc_vblank_reset(&crtc->base);
13249
13250                 if (crtc_state->hw.active)
13251                         intel_crtc_vblank_on(crtc_state);
13252         }
13253
13254         intel_sanitize_plane_mapping(dev_priv);
13255
13256         for_each_intel_encoder(dev, encoder)
13257                 intel_sanitize_encoder(encoder);
13258
13259         for_each_intel_crtc(&dev_priv->drm, crtc) {
13260                 struct intel_crtc_state *crtc_state =
13261                         to_intel_crtc_state(crtc->base.state);
13262
13263                 intel_sanitize_crtc(crtc, ctx);
13264                 intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
13265         }
13266
13267         intel_modeset_update_connector_atomic_state(dev);
13268
13269         intel_dpll_sanitize_state(dev_priv);
13270
13271         if (IS_G4X(dev_priv)) {
13272                 g4x_wm_get_hw_state(dev_priv);
13273                 g4x_wm_sanitize(dev_priv);
13274         } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
13275                 vlv_wm_get_hw_state(dev_priv);
13276                 vlv_wm_sanitize(dev_priv);
13277         } else if (DISPLAY_VER(dev_priv) >= 9) {
13278                 skl_wm_get_hw_state(dev_priv);
13279         } else if (HAS_PCH_SPLIT(dev_priv)) {
13280                 ilk_wm_get_hw_state(dev_priv);
13281         }
13282
13283         for_each_intel_crtc(dev, crtc) {
13284                 struct intel_crtc_state *crtc_state =
13285                         to_intel_crtc_state(crtc->base.state);
13286                 u64 put_domains;
13287
13288                 put_domains = modeset_get_crtc_power_domains(crtc_state);
13289                 if (drm_WARN_ON(dev, put_domains))
13290                         modeset_put_crtc_power_domains(crtc, put_domains);
13291         }
13292
13293         intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
13294 }
13295
13296 void intel_display_resume(struct drm_device *dev)
13297 {
13298         struct drm_i915_private *dev_priv = to_i915(dev);
13299         struct drm_atomic_state *state = dev_priv->modeset_restore_state;
13300         struct drm_modeset_acquire_ctx ctx;
13301         int ret;
13302
13303         if (!HAS_DISPLAY(dev_priv))
13304                 return;
13305
13306         dev_priv->modeset_restore_state = NULL;
13307         if (state)
13308                 state->acquire_ctx = &ctx;
13309
13310         drm_modeset_acquire_init(&ctx, 0);
13311
13312         while (1) {
13313                 ret = drm_modeset_lock_all_ctx(dev, &ctx);
13314                 if (ret != -EDEADLK)
13315                         break;
13316
13317                 drm_modeset_backoff(&ctx);
13318         }
13319
13320         if (!ret)
13321                 ret = __intel_display_resume(dev, state, &ctx);
13322
13323         intel_enable_ipc(dev_priv);
13324         drm_modeset_drop_locks(&ctx);
13325         drm_modeset_acquire_fini(&ctx);
13326
13327         if (ret)
13328                 drm_err(&dev_priv->drm,
13329                         "Restoring old state failed with %i\n", ret);
13330         if (state)
13331                 drm_atomic_state_put(state);
13332 }
13333
13334 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
13335 {
13336         struct intel_connector *connector;
13337         struct drm_connector_list_iter conn_iter;
13338
13339         /* Kill all the work that may have been queued by hpd. */
13340         drm_connector_list_iter_begin(&i915->drm, &conn_iter);
13341         for_each_intel_connector_iter(connector, &conn_iter) {
13342                 if (connector->modeset_retry_work.func)
13343                         cancel_work_sync(&connector->modeset_retry_work);
13344                 if (connector->hdcp.shim) {
13345                         cancel_delayed_work_sync(&connector->hdcp.check_work);
13346                         cancel_work_sync(&connector->hdcp.prop_work);
13347                 }
13348         }
13349         drm_connector_list_iter_end(&conn_iter);
13350 }
13351
13352 /* part #1: call before irq uninstall */
13353 void intel_modeset_driver_remove(struct drm_i915_private *i915)
13354 {
13355         if (!HAS_DISPLAY(i915))
13356                 return;
13357
13358         flush_workqueue(i915->flip_wq);
13359         flush_workqueue(i915->modeset_wq);
13360
13361         flush_work(&i915->atomic_helper.free_work);
13362         drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
13363 }
13364
13365 /* part #2: call after irq uninstall */
13366 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
13367 {
13368         if (!HAS_DISPLAY(i915))
13369                 return;
13370
13371         /*
13372          * Due to the hpd irq storm handling the hotplug work can re-arm the
13373          * poll handlers. Hence disable polling after hpd handling is shut down.
13374          */
13375         intel_hpd_poll_fini(i915);
13376
13377         /*
13378          * MST topology needs to be suspended so we don't have any calls to
13379          * fbdev after it's finalized. MST will be destroyed later as part of
13380          * drm_mode_config_cleanup()
13381          */
13382         intel_dp_mst_suspend(i915);
13383
13384         /* poll work can call into fbdev, hence clean that up afterwards */
13385         intel_fbdev_fini(i915);
13386
13387         intel_unregister_dsm_handler();
13388
13389         intel_fbc_global_disable(i915);
13390
13391         /* flush any delayed tasks or pending work */
13392         flush_scheduled_work();
13393
13394         intel_hdcp_component_fini(i915);
13395
13396         intel_mode_config_cleanup(i915);
13397
13398         intel_overlay_cleanup(i915);
13399
13400         intel_gmbus_teardown(i915);
13401
13402         destroy_workqueue(i915->flip_wq);
13403         destroy_workqueue(i915->modeset_wq);
13404
13405         intel_fbc_cleanup_cfb(i915);
13406 }
13407
13408 /* part #3: call after gem init */
13409 void intel_modeset_driver_remove_nogem(struct drm_i915_private *i915)
13410 {
13411         intel_dmc_ucode_fini(i915);
13412
13413         intel_power_domains_driver_remove(i915);
13414
13415         intel_vga_unregister(i915);
13416
13417         intel_bios_driver_remove(i915);
13418 }
13419
13420 void intel_display_driver_register(struct drm_i915_private *i915)
13421 {
13422         if (!HAS_DISPLAY(i915))
13423                 return;
13424
13425         intel_display_debugfs_register(i915);
13426
13427         /* Must be done after probing outputs */
13428         intel_opregion_register(i915);
13429         acpi_video_register();
13430
13431         intel_audio_init(i915);
13432
13433         /*
13434          * Some ports require correctly set-up hpd registers for
13435          * detection to work properly (leading to ghost connected
13436          * connector status), e.g. VGA on gm45.  Hence we can only set
13437          * up the initial fbdev config after hpd irqs are fully
13438          * enabled. We do it last so that the async config cannot run
13439          * before the connectors are registered.
13440          */
13441         intel_fbdev_initial_config_async(&i915->drm);
13442
13443         /*
13444          * We need to coordinate the hotplugs with the asynchronous
13445          * fbdev configuration, for which we use the
13446          * fbdev->async_cookie.
13447          */
13448         drm_kms_helper_poll_init(&i915->drm);
13449 }
13450
13451 void intel_display_driver_unregister(struct drm_i915_private *i915)
13452 {
13453         if (!HAS_DISPLAY(i915))
13454                 return;
13455
13456         intel_fbdev_unregister(i915);
13457         intel_audio_deinit(i915);
13458
13459         /*
13460          * After flushing the fbdev (incl. a late async config which
13461          * will have delayed queuing of a hotplug event), then flush
13462          * the hotplug events.
13463          */
13464         drm_kms_helper_poll_fini(&i915->drm);
13465         drm_atomic_helper_shutdown(&i915->drm);
13466
13467         acpi_video_unregister();
13468         intel_opregion_unregister(i915);
13469 }