2 * Copyright © 2006-2007 Intel Corporation
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:
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
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.
24 * Eric Anholt <eric@anholt.net>
27 #include <linux/dmi.h>
28 #include <linux/module.h>
29 #include <linux/input.h>
30 #include <linux/i2c.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/vgaarb.h>
34 #include <drm/drm_edid.h>
36 #include "intel_drv.h"
37 #include <drm/i915_drm.h>
39 #include "i915_trace.h"
40 #include <drm/drm_atomic.h>
41 #include <drm/drm_atomic_helper.h>
42 #include <drm/drm_dp_helper.h>
43 #include <drm/drm_crtc_helper.h>
44 #include <drm/drm_plane_helper.h>
45 #include <drm/drm_rect.h>
46 #include <linux/dma_remapping.h>
48 /* Primary plane formats for gen <= 3 */
49 static const uint32_t i8xx_primary_formats[] = {
56 /* Primary plane formats for gen >= 4 */
57 static const uint32_t i965_primary_formats[] = {
62 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
66 static const uint32_t skl_primary_formats[] = {
73 DRM_FORMAT_XRGB2101010,
74 DRM_FORMAT_XBGR2101010,
78 static const uint32_t intel_cursor_formats[] = {
82 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
84 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
85 struct intel_crtc_state *pipe_config);
86 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
87 struct intel_crtc_state *pipe_config);
89 static int intel_set_mode(struct drm_crtc *crtc,
90 struct drm_atomic_state *state);
91 static int intel_framebuffer_init(struct drm_device *dev,
92 struct intel_framebuffer *ifb,
93 struct drm_mode_fb_cmd2 *mode_cmd,
94 struct drm_i915_gem_object *obj);
95 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
96 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
97 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
98 struct intel_link_m_n *m_n,
99 struct intel_link_m_n *m2_n2);
100 static void ironlake_set_pipeconf(struct drm_crtc *crtc);
101 static void haswell_set_pipeconf(struct drm_crtc *crtc);
102 static void intel_set_pipe_csc(struct drm_crtc *crtc);
103 static void vlv_prepare_pll(struct intel_crtc *crtc,
104 const struct intel_crtc_state *pipe_config);
105 static void chv_prepare_pll(struct intel_crtc *crtc,
106 const struct intel_crtc_state *pipe_config);
107 static void intel_begin_crtc_commit(struct drm_crtc *crtc);
108 static void intel_finish_crtc_commit(struct drm_crtc *crtc);
109 static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
110 struct intel_crtc_state *crtc_state);
111 static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
113 static void intel_crtc_enable_planes(struct drm_crtc *crtc);
114 static void intel_crtc_disable_planes(struct drm_crtc *crtc);
116 static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
118 if (!connector->mst_port)
119 return connector->encoder;
121 return &connector->mst_port->mst_encoders[pipe]->base;
130 int p2_slow, p2_fast;
133 typedef struct intel_limit intel_limit_t;
135 intel_range_t dot, vco, n, m, m1, m2, p, p1;
140 intel_pch_rawclk(struct drm_device *dev)
142 struct drm_i915_private *dev_priv = dev->dev_private;
144 WARN_ON(!HAS_PCH_SPLIT(dev));
146 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
149 static inline u32 /* units of 100MHz */
150 intel_fdi_link_freq(struct drm_device *dev)
153 struct drm_i915_private *dev_priv = dev->dev_private;
154 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
159 static const intel_limit_t intel_limits_i8xx_dac = {
160 .dot = { .min = 25000, .max = 350000 },
161 .vco = { .min = 908000, .max = 1512000 },
162 .n = { .min = 2, .max = 16 },
163 .m = { .min = 96, .max = 140 },
164 .m1 = { .min = 18, .max = 26 },
165 .m2 = { .min = 6, .max = 16 },
166 .p = { .min = 4, .max = 128 },
167 .p1 = { .min = 2, .max = 33 },
168 .p2 = { .dot_limit = 165000,
169 .p2_slow = 4, .p2_fast = 2 },
172 static const intel_limit_t intel_limits_i8xx_dvo = {
173 .dot = { .min = 25000, .max = 350000 },
174 .vco = { .min = 908000, .max = 1512000 },
175 .n = { .min = 2, .max = 16 },
176 .m = { .min = 96, .max = 140 },
177 .m1 = { .min = 18, .max = 26 },
178 .m2 = { .min = 6, .max = 16 },
179 .p = { .min = 4, .max = 128 },
180 .p1 = { .min = 2, .max = 33 },
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 4, .p2_fast = 4 },
185 static const intel_limit_t intel_limits_i8xx_lvds = {
186 .dot = { .min = 25000, .max = 350000 },
187 .vco = { .min = 908000, .max = 1512000 },
188 .n = { .min = 2, .max = 16 },
189 .m = { .min = 96, .max = 140 },
190 .m1 = { .min = 18, .max = 26 },
191 .m2 = { .min = 6, .max = 16 },
192 .p = { .min = 4, .max = 128 },
193 .p1 = { .min = 1, .max = 6 },
194 .p2 = { .dot_limit = 165000,
195 .p2_slow = 14, .p2_fast = 7 },
198 static const intel_limit_t intel_limits_i9xx_sdvo = {
199 .dot = { .min = 20000, .max = 400000 },
200 .vco = { .min = 1400000, .max = 2800000 },
201 .n = { .min = 1, .max = 6 },
202 .m = { .min = 70, .max = 120 },
203 .m1 = { .min = 8, .max = 18 },
204 .m2 = { .min = 3, .max = 7 },
205 .p = { .min = 5, .max = 80 },
206 .p1 = { .min = 1, .max = 8 },
207 .p2 = { .dot_limit = 200000,
208 .p2_slow = 10, .p2_fast = 5 },
211 static const intel_limit_t intel_limits_i9xx_lvds = {
212 .dot = { .min = 20000, .max = 400000 },
213 .vco = { .min = 1400000, .max = 2800000 },
214 .n = { .min = 1, .max = 6 },
215 .m = { .min = 70, .max = 120 },
216 .m1 = { .min = 8, .max = 18 },
217 .m2 = { .min = 3, .max = 7 },
218 .p = { .min = 7, .max = 98 },
219 .p1 = { .min = 1, .max = 8 },
220 .p2 = { .dot_limit = 112000,
221 .p2_slow = 14, .p2_fast = 7 },
225 static const intel_limit_t intel_limits_g4x_sdvo = {
226 .dot = { .min = 25000, .max = 270000 },
227 .vco = { .min = 1750000, .max = 3500000},
228 .n = { .min = 1, .max = 4 },
229 .m = { .min = 104, .max = 138 },
230 .m1 = { .min = 17, .max = 23 },
231 .m2 = { .min = 5, .max = 11 },
232 .p = { .min = 10, .max = 30 },
233 .p1 = { .min = 1, .max = 3},
234 .p2 = { .dot_limit = 270000,
240 static const intel_limit_t intel_limits_g4x_hdmi = {
241 .dot = { .min = 22000, .max = 400000 },
242 .vco = { .min = 1750000, .max = 3500000},
243 .n = { .min = 1, .max = 4 },
244 .m = { .min = 104, .max = 138 },
245 .m1 = { .min = 16, .max = 23 },
246 .m2 = { .min = 5, .max = 11 },
247 .p = { .min = 5, .max = 80 },
248 .p1 = { .min = 1, .max = 8},
249 .p2 = { .dot_limit = 165000,
250 .p2_slow = 10, .p2_fast = 5 },
253 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
254 .dot = { .min = 20000, .max = 115000 },
255 .vco = { .min = 1750000, .max = 3500000 },
256 .n = { .min = 1, .max = 3 },
257 .m = { .min = 104, .max = 138 },
258 .m1 = { .min = 17, .max = 23 },
259 .m2 = { .min = 5, .max = 11 },
260 .p = { .min = 28, .max = 112 },
261 .p1 = { .min = 2, .max = 8 },
262 .p2 = { .dot_limit = 0,
263 .p2_slow = 14, .p2_fast = 14
267 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
268 .dot = { .min = 80000, .max = 224000 },
269 .vco = { .min = 1750000, .max = 3500000 },
270 .n = { .min = 1, .max = 3 },
271 .m = { .min = 104, .max = 138 },
272 .m1 = { .min = 17, .max = 23 },
273 .m2 = { .min = 5, .max = 11 },
274 .p = { .min = 14, .max = 42 },
275 .p1 = { .min = 2, .max = 6 },
276 .p2 = { .dot_limit = 0,
277 .p2_slow = 7, .p2_fast = 7
281 static const intel_limit_t intel_limits_pineview_sdvo = {
282 .dot = { .min = 20000, .max = 400000},
283 .vco = { .min = 1700000, .max = 3500000 },
284 /* Pineview's Ncounter is a ring counter */
285 .n = { .min = 3, .max = 6 },
286 .m = { .min = 2, .max = 256 },
287 /* Pineview only has one combined m divider, which we treat as m2. */
288 .m1 = { .min = 0, .max = 0 },
289 .m2 = { .min = 0, .max = 254 },
290 .p = { .min = 5, .max = 80 },
291 .p1 = { .min = 1, .max = 8 },
292 .p2 = { .dot_limit = 200000,
293 .p2_slow = 10, .p2_fast = 5 },
296 static const intel_limit_t intel_limits_pineview_lvds = {
297 .dot = { .min = 20000, .max = 400000 },
298 .vco = { .min = 1700000, .max = 3500000 },
299 .n = { .min = 3, .max = 6 },
300 .m = { .min = 2, .max = 256 },
301 .m1 = { .min = 0, .max = 0 },
302 .m2 = { .min = 0, .max = 254 },
303 .p = { .min = 7, .max = 112 },
304 .p1 = { .min = 1, .max = 8 },
305 .p2 = { .dot_limit = 112000,
306 .p2_slow = 14, .p2_fast = 14 },
309 /* Ironlake / Sandybridge
311 * We calculate clock using (register_value + 2) for N/M1/M2, so here
312 * the range value for them is (actual_value - 2).
314 static const intel_limit_t intel_limits_ironlake_dac = {
315 .dot = { .min = 25000, .max = 350000 },
316 .vco = { .min = 1760000, .max = 3510000 },
317 .n = { .min = 1, .max = 5 },
318 .m = { .min = 79, .max = 127 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 5, .max = 80 },
322 .p1 = { .min = 1, .max = 8 },
323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 10, .p2_fast = 5 },
327 static const intel_limit_t intel_limits_ironlake_single_lvds = {
328 .dot = { .min = 25000, .max = 350000 },
329 .vco = { .min = 1760000, .max = 3510000 },
330 .n = { .min = 1, .max = 3 },
331 .m = { .min = 79, .max = 118 },
332 .m1 = { .min = 12, .max = 22 },
333 .m2 = { .min = 5, .max = 9 },
334 .p = { .min = 28, .max = 112 },
335 .p1 = { .min = 2, .max = 8 },
336 .p2 = { .dot_limit = 225000,
337 .p2_slow = 14, .p2_fast = 14 },
340 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
341 .dot = { .min = 25000, .max = 350000 },
342 .vco = { .min = 1760000, .max = 3510000 },
343 .n = { .min = 1, .max = 3 },
344 .m = { .min = 79, .max = 127 },
345 .m1 = { .min = 12, .max = 22 },
346 .m2 = { .min = 5, .max = 9 },
347 .p = { .min = 14, .max = 56 },
348 .p1 = { .min = 2, .max = 8 },
349 .p2 = { .dot_limit = 225000,
350 .p2_slow = 7, .p2_fast = 7 },
353 /* LVDS 100mhz refclk limits. */
354 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
355 .dot = { .min = 25000, .max = 350000 },
356 .vco = { .min = 1760000, .max = 3510000 },
357 .n = { .min = 1, .max = 2 },
358 .m = { .min = 79, .max = 126 },
359 .m1 = { .min = 12, .max = 22 },
360 .m2 = { .min = 5, .max = 9 },
361 .p = { .min = 28, .max = 112 },
362 .p1 = { .min = 2, .max = 8 },
363 .p2 = { .dot_limit = 225000,
364 .p2_slow = 14, .p2_fast = 14 },
367 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
368 .dot = { .min = 25000, .max = 350000 },
369 .vco = { .min = 1760000, .max = 3510000 },
370 .n = { .min = 1, .max = 3 },
371 .m = { .min = 79, .max = 126 },
372 .m1 = { .min = 12, .max = 22 },
373 .m2 = { .min = 5, .max = 9 },
374 .p = { .min = 14, .max = 42 },
375 .p1 = { .min = 2, .max = 6 },
376 .p2 = { .dot_limit = 225000,
377 .p2_slow = 7, .p2_fast = 7 },
380 static const intel_limit_t intel_limits_vlv = {
382 * These are the data rate limits (measured in fast clocks)
383 * since those are the strictest limits we have. The fast
384 * clock and actual rate limits are more relaxed, so checking
385 * them would make no difference.
387 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
388 .vco = { .min = 4000000, .max = 6000000 },
389 .n = { .min = 1, .max = 7 },
390 .m1 = { .min = 2, .max = 3 },
391 .m2 = { .min = 11, .max = 156 },
392 .p1 = { .min = 2, .max = 3 },
393 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
396 static const intel_limit_t intel_limits_chv = {
398 * These are the data rate limits (measured in fast clocks)
399 * since those are the strictest limits we have. The fast
400 * clock and actual rate limits are more relaxed, so checking
401 * them would make no difference.
403 .dot = { .min = 25000 * 5, .max = 540000 * 5},
404 .vco = { .min = 4800000, .max = 6480000 },
405 .n = { .min = 1, .max = 1 },
406 .m1 = { .min = 2, .max = 2 },
407 .m2 = { .min = 24 << 22, .max = 175 << 22 },
408 .p1 = { .min = 2, .max = 4 },
409 .p2 = { .p2_slow = 1, .p2_fast = 14 },
412 static const intel_limit_t intel_limits_bxt = {
413 /* FIXME: find real dot limits */
414 .dot = { .min = 0, .max = INT_MAX },
415 .vco = { .min = 4800000, .max = 6480000 },
416 .n = { .min = 1, .max = 1 },
417 .m1 = { .min = 2, .max = 2 },
418 /* FIXME: find real m2 limits */
419 .m2 = { .min = 2 << 22, .max = 255 << 22 },
420 .p1 = { .min = 2, .max = 4 },
421 .p2 = { .p2_slow = 1, .p2_fast = 20 },
424 static void vlv_clock(int refclk, intel_clock_t *clock)
426 clock->m = clock->m1 * clock->m2;
427 clock->p = clock->p1 * clock->p2;
428 if (WARN_ON(clock->n == 0 || clock->p == 0))
430 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
431 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
435 needs_modeset(struct drm_crtc_state *state)
437 return state->mode_changed || state->active_changed;
441 * Returns whether any output on the specified pipe is of the specified type
443 bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
445 struct drm_device *dev = crtc->base.dev;
446 struct intel_encoder *encoder;
448 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
449 if (encoder->type == type)
456 * Returns whether any output on the specified pipe will have the specified
457 * type after a staged modeset is complete, i.e., the same as
458 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
461 static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
464 struct drm_atomic_state *state = crtc_state->base.state;
465 struct drm_connector *connector;
466 struct drm_connector_state *connector_state;
467 struct intel_encoder *encoder;
468 int i, num_connectors = 0;
470 for_each_connector_in_state(state, connector, connector_state, i) {
471 if (connector_state->crtc != crtc_state->base.crtc)
476 encoder = to_intel_encoder(connector_state->best_encoder);
477 if (encoder->type == type)
481 WARN_ON(num_connectors == 0);
486 static const intel_limit_t *
487 intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
489 struct drm_device *dev = crtc_state->base.crtc->dev;
490 const intel_limit_t *limit;
492 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
493 if (intel_is_dual_link_lvds(dev)) {
494 if (refclk == 100000)
495 limit = &intel_limits_ironlake_dual_lvds_100m;
497 limit = &intel_limits_ironlake_dual_lvds;
499 if (refclk == 100000)
500 limit = &intel_limits_ironlake_single_lvds_100m;
502 limit = &intel_limits_ironlake_single_lvds;
505 limit = &intel_limits_ironlake_dac;
510 static const intel_limit_t *
511 intel_g4x_limit(struct intel_crtc_state *crtc_state)
513 struct drm_device *dev = crtc_state->base.crtc->dev;
514 const intel_limit_t *limit;
516 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
517 if (intel_is_dual_link_lvds(dev))
518 limit = &intel_limits_g4x_dual_channel_lvds;
520 limit = &intel_limits_g4x_single_channel_lvds;
521 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
522 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
523 limit = &intel_limits_g4x_hdmi;
524 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
525 limit = &intel_limits_g4x_sdvo;
526 } else /* The option is for other outputs */
527 limit = &intel_limits_i9xx_sdvo;
532 static const intel_limit_t *
533 intel_limit(struct intel_crtc_state *crtc_state, int refclk)
535 struct drm_device *dev = crtc_state->base.crtc->dev;
536 const intel_limit_t *limit;
539 limit = &intel_limits_bxt;
540 else if (HAS_PCH_SPLIT(dev))
541 limit = intel_ironlake_limit(crtc_state, refclk);
542 else if (IS_G4X(dev)) {
543 limit = intel_g4x_limit(crtc_state);
544 } else if (IS_PINEVIEW(dev)) {
545 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
546 limit = &intel_limits_pineview_lvds;
548 limit = &intel_limits_pineview_sdvo;
549 } else if (IS_CHERRYVIEW(dev)) {
550 limit = &intel_limits_chv;
551 } else if (IS_VALLEYVIEW(dev)) {
552 limit = &intel_limits_vlv;
553 } else if (!IS_GEN2(dev)) {
554 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
555 limit = &intel_limits_i9xx_lvds;
557 limit = &intel_limits_i9xx_sdvo;
559 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
560 limit = &intel_limits_i8xx_lvds;
561 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
562 limit = &intel_limits_i8xx_dvo;
564 limit = &intel_limits_i8xx_dac;
569 /* m1 is reserved as 0 in Pineview, n is a ring counter */
570 static void pineview_clock(int refclk, intel_clock_t *clock)
572 clock->m = clock->m2 + 2;
573 clock->p = clock->p1 * clock->p2;
574 if (WARN_ON(clock->n == 0 || clock->p == 0))
576 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
577 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
580 static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
582 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
585 static void i9xx_clock(int refclk, intel_clock_t *clock)
587 clock->m = i9xx_dpll_compute_m(clock);
588 clock->p = clock->p1 * clock->p2;
589 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
591 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
595 static void chv_clock(int refclk, intel_clock_t *clock)
597 clock->m = clock->m1 * clock->m2;
598 clock->p = clock->p1 * clock->p2;
599 if (WARN_ON(clock->n == 0 || clock->p == 0))
601 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
603 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
606 #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
608 * Returns whether the given set of divisors are valid for a given refclk with
609 * the given connectors.
612 static bool intel_PLL_is_valid(struct drm_device *dev,
613 const intel_limit_t *limit,
614 const intel_clock_t *clock)
616 if (clock->n < limit->n.min || limit->n.max < clock->n)
617 INTELPllInvalid("n out of range\n");
618 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
619 INTELPllInvalid("p1 out of range\n");
620 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
621 INTELPllInvalid("m2 out of range\n");
622 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
623 INTELPllInvalid("m1 out of range\n");
625 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
626 if (clock->m1 <= clock->m2)
627 INTELPllInvalid("m1 <= m2\n");
629 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
630 if (clock->p < limit->p.min || limit->p.max < clock->p)
631 INTELPllInvalid("p out of range\n");
632 if (clock->m < limit->m.min || limit->m.max < clock->m)
633 INTELPllInvalid("m out of range\n");
636 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
637 INTELPllInvalid("vco out of range\n");
638 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
639 * connector, etc., rather than just a single range.
641 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
642 INTELPllInvalid("dot out of range\n");
648 i9xx_find_best_dpll(const intel_limit_t *limit,
649 struct intel_crtc_state *crtc_state,
650 int target, int refclk, intel_clock_t *match_clock,
651 intel_clock_t *best_clock)
653 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
654 struct drm_device *dev = crtc->base.dev;
658 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
660 * For LVDS just rely on its current settings for dual-channel.
661 * We haven't figured out how to reliably set up different
662 * single/dual channel state, if we even can.
664 if (intel_is_dual_link_lvds(dev))
665 clock.p2 = limit->p2.p2_fast;
667 clock.p2 = limit->p2.p2_slow;
669 if (target < limit->p2.dot_limit)
670 clock.p2 = limit->p2.p2_slow;
672 clock.p2 = limit->p2.p2_fast;
675 memset(best_clock, 0, sizeof(*best_clock));
677 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
679 for (clock.m2 = limit->m2.min;
680 clock.m2 <= limit->m2.max; clock.m2++) {
681 if (clock.m2 >= clock.m1)
683 for (clock.n = limit->n.min;
684 clock.n <= limit->n.max; clock.n++) {
685 for (clock.p1 = limit->p1.min;
686 clock.p1 <= limit->p1.max; clock.p1++) {
689 i9xx_clock(refclk, &clock);
690 if (!intel_PLL_is_valid(dev, limit,
694 clock.p != match_clock->p)
697 this_err = abs(clock.dot - target);
698 if (this_err < err) {
707 return (err != target);
711 pnv_find_best_dpll(const intel_limit_t *limit,
712 struct intel_crtc_state *crtc_state,
713 int target, int refclk, intel_clock_t *match_clock,
714 intel_clock_t *best_clock)
716 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
717 struct drm_device *dev = crtc->base.dev;
721 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
723 * For LVDS just rely on its current settings for dual-channel.
724 * We haven't figured out how to reliably set up different
725 * single/dual channel state, if we even can.
727 if (intel_is_dual_link_lvds(dev))
728 clock.p2 = limit->p2.p2_fast;
730 clock.p2 = limit->p2.p2_slow;
732 if (target < limit->p2.dot_limit)
733 clock.p2 = limit->p2.p2_slow;
735 clock.p2 = limit->p2.p2_fast;
738 memset(best_clock, 0, sizeof(*best_clock));
740 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
742 for (clock.m2 = limit->m2.min;
743 clock.m2 <= limit->m2.max; clock.m2++) {
744 for (clock.n = limit->n.min;
745 clock.n <= limit->n.max; clock.n++) {
746 for (clock.p1 = limit->p1.min;
747 clock.p1 <= limit->p1.max; clock.p1++) {
750 pineview_clock(refclk, &clock);
751 if (!intel_PLL_is_valid(dev, limit,
755 clock.p != match_clock->p)
758 this_err = abs(clock.dot - target);
759 if (this_err < err) {
768 return (err != target);
772 g4x_find_best_dpll(const intel_limit_t *limit,
773 struct intel_crtc_state *crtc_state,
774 int target, int refclk, intel_clock_t *match_clock,
775 intel_clock_t *best_clock)
777 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
778 struct drm_device *dev = crtc->base.dev;
782 /* approximately equals target * 0.00585 */
783 int err_most = (target >> 8) + (target >> 9);
786 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
787 if (intel_is_dual_link_lvds(dev))
788 clock.p2 = limit->p2.p2_fast;
790 clock.p2 = limit->p2.p2_slow;
792 if (target < limit->p2.dot_limit)
793 clock.p2 = limit->p2.p2_slow;
795 clock.p2 = limit->p2.p2_fast;
798 memset(best_clock, 0, sizeof(*best_clock));
799 max_n = limit->n.max;
800 /* based on hardware requirement, prefer smaller n to precision */
801 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
802 /* based on hardware requirement, prefere larger m1,m2 */
803 for (clock.m1 = limit->m1.max;
804 clock.m1 >= limit->m1.min; clock.m1--) {
805 for (clock.m2 = limit->m2.max;
806 clock.m2 >= limit->m2.min; clock.m2--) {
807 for (clock.p1 = limit->p1.max;
808 clock.p1 >= limit->p1.min; clock.p1--) {
811 i9xx_clock(refclk, &clock);
812 if (!intel_PLL_is_valid(dev, limit,
816 this_err = abs(clock.dot - target);
817 if (this_err < err_most) {
831 * Check if the calculated PLL configuration is more optimal compared to the
832 * best configuration and error found so far. Return the calculated error.
834 static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
835 const intel_clock_t *calculated_clock,
836 const intel_clock_t *best_clock,
837 unsigned int best_error_ppm,
838 unsigned int *error_ppm)
841 * For CHV ignore the error and consider only the P value.
842 * Prefer a bigger P value based on HW requirements.
844 if (IS_CHERRYVIEW(dev)) {
847 return calculated_clock->p > best_clock->p;
850 if (WARN_ON_ONCE(!target_freq))
853 *error_ppm = div_u64(1000000ULL *
854 abs(target_freq - calculated_clock->dot),
857 * Prefer a better P value over a better (smaller) error if the error
858 * is small. Ensure this preference for future configurations too by
859 * setting the error to 0.
861 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
867 return *error_ppm + 10 < best_error_ppm;
871 vlv_find_best_dpll(const intel_limit_t *limit,
872 struct intel_crtc_state *crtc_state,
873 int target, int refclk, intel_clock_t *match_clock,
874 intel_clock_t *best_clock)
876 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
877 struct drm_device *dev = crtc->base.dev;
879 unsigned int bestppm = 1000000;
880 /* min update 19.2 MHz */
881 int max_n = min(limit->n.max, refclk / 19200);
884 target *= 5; /* fast clock */
886 memset(best_clock, 0, sizeof(*best_clock));
888 /* based on hardware requirement, prefer smaller n to precision */
889 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
890 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
891 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
892 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
893 clock.p = clock.p1 * clock.p2;
894 /* based on hardware requirement, prefer bigger m1,m2 values */
895 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
898 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
901 vlv_clock(refclk, &clock);
903 if (!intel_PLL_is_valid(dev, limit,
907 if (!vlv_PLL_is_optimal(dev, target,
925 chv_find_best_dpll(const intel_limit_t *limit,
926 struct intel_crtc_state *crtc_state,
927 int target, int refclk, intel_clock_t *match_clock,
928 intel_clock_t *best_clock)
930 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
931 struct drm_device *dev = crtc->base.dev;
932 unsigned int best_error_ppm;
937 memset(best_clock, 0, sizeof(*best_clock));
938 best_error_ppm = 1000000;
941 * Based on hardware doc, the n always set to 1, and m1 always
942 * set to 2. If requires to support 200Mhz refclk, we need to
943 * revisit this because n may not 1 anymore.
945 clock.n = 1, clock.m1 = 2;
946 target *= 5; /* fast clock */
948 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
949 for (clock.p2 = limit->p2.p2_fast;
950 clock.p2 >= limit->p2.p2_slow;
951 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
952 unsigned int error_ppm;
954 clock.p = clock.p1 * clock.p2;
956 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
957 clock.n) << 22, refclk * clock.m1);
959 if (m2 > INT_MAX/clock.m1)
964 chv_clock(refclk, &clock);
966 if (!intel_PLL_is_valid(dev, limit, &clock))
969 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
970 best_error_ppm, &error_ppm))
974 best_error_ppm = error_ppm;
982 bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
983 intel_clock_t *best_clock)
985 int refclk = i9xx_get_refclk(crtc_state, 0);
987 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
988 target_clock, refclk, NULL, best_clock);
991 bool intel_crtc_active(struct drm_crtc *crtc)
993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
995 /* Be paranoid as we can arrive here with only partial
996 * state retrieved from the hardware during setup.
998 * We can ditch the adjusted_mode.crtc_clock check as soon
999 * as Haswell has gained clock readout/fastboot support.
1001 * We can ditch the crtc->primary->fb check as soon as we can
1002 * properly reconstruct framebuffers.
1004 * FIXME: The intel_crtc->active here should be switched to
1005 * crtc->state->active once we have proper CRTC states wired up
1008 return intel_crtc->active && crtc->primary->state->fb &&
1009 intel_crtc->config->base.adjusted_mode.crtc_clock;
1012 enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1015 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1018 return intel_crtc->config->cpu_transcoder;
1021 static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1023 struct drm_i915_private *dev_priv = dev->dev_private;
1024 u32 reg = PIPEDSL(pipe);
1029 line_mask = DSL_LINEMASK_GEN2;
1031 line_mask = DSL_LINEMASK_GEN3;
1033 line1 = I915_READ(reg) & line_mask;
1035 line2 = I915_READ(reg) & line_mask;
1037 return line1 == line2;
1041 * intel_wait_for_pipe_off - wait for pipe to turn off
1042 * @crtc: crtc whose pipe to wait for
1044 * After disabling a pipe, we can't wait for vblank in the usual way,
1045 * spinning on the vblank interrupt status bit, since we won't actually
1046 * see an interrupt when the pipe is disabled.
1048 * On Gen4 and above:
1049 * wait for the pipe register state bit to turn off
1052 * wait for the display line value to settle (it usually
1053 * ends up stopping at the start of the next frame).
1056 static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
1058 struct drm_device *dev = crtc->base.dev;
1059 struct drm_i915_private *dev_priv = dev->dev_private;
1060 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1061 enum pipe pipe = crtc->pipe;
1063 if (INTEL_INFO(dev)->gen >= 4) {
1064 int reg = PIPECONF(cpu_transcoder);
1066 /* Wait for the Pipe State to go off */
1067 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1069 WARN(1, "pipe_off wait timed out\n");
1071 /* Wait for the display line to settle */
1072 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
1073 WARN(1, "pipe_off wait timed out\n");
1078 * ibx_digital_port_connected - is the specified port connected?
1079 * @dev_priv: i915 private structure
1080 * @port: the port to test
1082 * Returns true if @port is connected, false otherwise.
1084 bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1085 struct intel_digital_port *port)
1089 if (HAS_PCH_IBX(dev_priv->dev)) {
1090 switch (port->port) {
1092 bit = SDE_PORTB_HOTPLUG;
1095 bit = SDE_PORTC_HOTPLUG;
1098 bit = SDE_PORTD_HOTPLUG;
1104 switch (port->port) {
1106 bit = SDE_PORTB_HOTPLUG_CPT;
1109 bit = SDE_PORTC_HOTPLUG_CPT;
1112 bit = SDE_PORTD_HOTPLUG_CPT;
1119 return I915_READ(SDEISR) & bit;
1122 static const char *state_string(bool enabled)
1124 return enabled ? "on" : "off";
1127 /* Only for pre-ILK configs */
1128 void assert_pll(struct drm_i915_private *dev_priv,
1129 enum pipe pipe, bool state)
1136 val = I915_READ(reg);
1137 cur_state = !!(val & DPLL_VCO_ENABLE);
1138 I915_STATE_WARN(cur_state != state,
1139 "PLL state assertion failure (expected %s, current %s)\n",
1140 state_string(state), state_string(cur_state));
1143 /* XXX: the dsi pll is shared between MIPI DSI ports */
1144 static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1149 mutex_lock(&dev_priv->sb_lock);
1150 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1151 mutex_unlock(&dev_priv->sb_lock);
1153 cur_state = val & DSI_PLL_VCO_EN;
1154 I915_STATE_WARN(cur_state != state,
1155 "DSI PLL state assertion failure (expected %s, current %s)\n",
1156 state_string(state), state_string(cur_state));
1158 #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1159 #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1161 struct intel_shared_dpll *
1162 intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1164 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1166 if (crtc->config->shared_dpll < 0)
1169 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
1173 void assert_shared_dpll(struct drm_i915_private *dev_priv,
1174 struct intel_shared_dpll *pll,
1178 struct intel_dpll_hw_state hw_state;
1181 "asserting DPLL %s with no DPLL\n", state_string(state)))
1184 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
1185 I915_STATE_WARN(cur_state != state,
1186 "%s assertion failure (expected %s, current %s)\n",
1187 pll->name, state_string(state), state_string(cur_state));
1190 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1191 enum pipe pipe, bool state)
1196 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1199 if (HAS_DDI(dev_priv->dev)) {
1200 /* DDI does not have a specific FDI_TX register */
1201 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
1202 val = I915_READ(reg);
1203 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1205 reg = FDI_TX_CTL(pipe);
1206 val = I915_READ(reg);
1207 cur_state = !!(val & FDI_TX_ENABLE);
1209 I915_STATE_WARN(cur_state != state,
1210 "FDI TX state assertion failure (expected %s, current %s)\n",
1211 state_string(state), state_string(cur_state));
1213 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1214 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1216 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1217 enum pipe pipe, bool state)
1223 reg = FDI_RX_CTL(pipe);
1224 val = I915_READ(reg);
1225 cur_state = !!(val & FDI_RX_ENABLE);
1226 I915_STATE_WARN(cur_state != state,
1227 "FDI RX state assertion failure (expected %s, current %s)\n",
1228 state_string(state), state_string(cur_state));
1230 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1231 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1233 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1239 /* ILK FDI PLL is always enabled */
1240 if (INTEL_INFO(dev_priv->dev)->gen == 5)
1243 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
1244 if (HAS_DDI(dev_priv->dev))
1247 reg = FDI_TX_CTL(pipe);
1248 val = I915_READ(reg);
1249 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1252 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1253 enum pipe pipe, bool state)
1259 reg = FDI_RX_CTL(pipe);
1260 val = I915_READ(reg);
1261 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1262 I915_STATE_WARN(cur_state != state,
1263 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1264 state_string(state), state_string(cur_state));
1267 void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1270 struct drm_device *dev = dev_priv->dev;
1273 enum pipe panel_pipe = PIPE_A;
1276 if (WARN_ON(HAS_DDI(dev)))
1279 if (HAS_PCH_SPLIT(dev)) {
1282 pp_reg = PCH_PP_CONTROL;
1283 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1285 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1286 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1287 panel_pipe = PIPE_B;
1288 /* XXX: else fix for eDP */
1289 } else if (IS_VALLEYVIEW(dev)) {
1290 /* presumably write lock depends on pipe, not port select */
1291 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1294 pp_reg = PP_CONTROL;
1295 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1296 panel_pipe = PIPE_B;
1299 val = I915_READ(pp_reg);
1300 if (!(val & PANEL_POWER_ON) ||
1301 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1304 I915_STATE_WARN(panel_pipe == pipe && locked,
1305 "panel assertion failure, pipe %c regs locked\n",
1309 static void assert_cursor(struct drm_i915_private *dev_priv,
1310 enum pipe pipe, bool state)
1312 struct drm_device *dev = dev_priv->dev;
1315 if (IS_845G(dev) || IS_I865G(dev))
1316 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1318 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1320 I915_STATE_WARN(cur_state != state,
1321 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1322 pipe_name(pipe), state_string(state), state_string(cur_state));
1324 #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1325 #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1327 void assert_pipe(struct drm_i915_private *dev_priv,
1328 enum pipe pipe, bool state)
1333 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1336 /* if we need the pipe quirk it must be always on */
1337 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1338 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
1341 if (!intel_display_power_is_enabled(dev_priv,
1342 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
1345 reg = PIPECONF(cpu_transcoder);
1346 val = I915_READ(reg);
1347 cur_state = !!(val & PIPECONF_ENABLE);
1350 I915_STATE_WARN(cur_state != state,
1351 "pipe %c assertion failure (expected %s, current %s)\n",
1352 pipe_name(pipe), state_string(state), state_string(cur_state));
1355 static void assert_plane(struct drm_i915_private *dev_priv,
1356 enum plane plane, bool state)
1362 reg = DSPCNTR(plane);
1363 val = I915_READ(reg);
1364 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1365 I915_STATE_WARN(cur_state != state,
1366 "plane %c assertion failure (expected %s, current %s)\n",
1367 plane_name(plane), state_string(state), state_string(cur_state));
1370 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
1371 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
1373 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1376 struct drm_device *dev = dev_priv->dev;
1381 /* Primary planes are fixed to pipes on gen4+ */
1382 if (INTEL_INFO(dev)->gen >= 4) {
1383 reg = DSPCNTR(pipe);
1384 val = I915_READ(reg);
1385 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
1386 "plane %c assertion failure, should be disabled but not\n",
1391 /* Need to check both planes against the pipe */
1392 for_each_pipe(dev_priv, i) {
1394 val = I915_READ(reg);
1395 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1396 DISPPLANE_SEL_PIPE_SHIFT;
1397 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1398 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1399 plane_name(i), pipe_name(pipe));
1403 static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1406 struct drm_device *dev = dev_priv->dev;
1410 if (INTEL_INFO(dev)->gen >= 9) {
1411 for_each_sprite(dev_priv, pipe, sprite) {
1412 val = I915_READ(PLANE_CTL(pipe, sprite));
1413 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
1414 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1415 sprite, pipe_name(pipe));
1417 } else if (IS_VALLEYVIEW(dev)) {
1418 for_each_sprite(dev_priv, pipe, sprite) {
1419 reg = SPCNTR(pipe, sprite);
1420 val = I915_READ(reg);
1421 I915_STATE_WARN(val & SP_ENABLE,
1422 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1423 sprite_name(pipe, sprite), pipe_name(pipe));
1425 } else if (INTEL_INFO(dev)->gen >= 7) {
1427 val = I915_READ(reg);
1428 I915_STATE_WARN(val & SPRITE_ENABLE,
1429 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1430 plane_name(pipe), pipe_name(pipe));
1431 } else if (INTEL_INFO(dev)->gen >= 5) {
1432 reg = DVSCNTR(pipe);
1433 val = I915_READ(reg);
1434 I915_STATE_WARN(val & DVS_ENABLE,
1435 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1436 plane_name(pipe), pipe_name(pipe));
1440 static void assert_vblank_disabled(struct drm_crtc *crtc)
1442 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1443 drm_crtc_vblank_put(crtc);
1446 static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1451 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
1453 val = I915_READ(PCH_DREF_CONTROL);
1454 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1455 DREF_SUPERSPREAD_SOURCE_MASK));
1456 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1459 static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1466 reg = PCH_TRANSCONF(pipe);
1467 val = I915_READ(reg);
1468 enabled = !!(val & TRANS_ENABLE);
1469 I915_STATE_WARN(enabled,
1470 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1474 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1475 enum pipe pipe, u32 port_sel, u32 val)
1477 if ((val & DP_PORT_EN) == 0)
1480 if (HAS_PCH_CPT(dev_priv->dev)) {
1481 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1482 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1483 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1485 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1486 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1489 if ((val & DP_PIPE_MASK) != (pipe << 30))
1495 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1496 enum pipe pipe, u32 val)
1498 if ((val & SDVO_ENABLE) == 0)
1501 if (HAS_PCH_CPT(dev_priv->dev)) {
1502 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1504 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1505 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1508 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1514 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1515 enum pipe pipe, u32 val)
1517 if ((val & LVDS_PORT_EN) == 0)
1520 if (HAS_PCH_CPT(dev_priv->dev)) {
1521 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1524 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1530 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1531 enum pipe pipe, u32 val)
1533 if ((val & ADPA_DAC_ENABLE) == 0)
1535 if (HAS_PCH_CPT(dev_priv->dev)) {
1536 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1539 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1545 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1546 enum pipe pipe, int reg, u32 port_sel)
1548 u32 val = I915_READ(reg);
1549 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1550 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1551 reg, pipe_name(pipe));
1553 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1554 && (val & DP_PIPEB_SELECT),
1555 "IBX PCH dp port still using transcoder B\n");
1558 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1559 enum pipe pipe, int reg)
1561 u32 val = I915_READ(reg);
1562 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1563 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1564 reg, pipe_name(pipe));
1566 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1567 && (val & SDVO_PIPE_B_SELECT),
1568 "IBX PCH hdmi port still using transcoder B\n");
1571 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1577 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1578 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1579 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1582 val = I915_READ(reg);
1583 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1584 "PCH VGA enabled on transcoder %c, should be disabled\n",
1588 val = I915_READ(reg);
1589 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1590 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1593 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1594 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1595 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1598 static void intel_init_dpio(struct drm_device *dev)
1600 struct drm_i915_private *dev_priv = dev->dev_private;
1602 if (!IS_VALLEYVIEW(dev))
1606 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1607 * CHV x1 PHY (DP/HDMI D)
1608 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1610 if (IS_CHERRYVIEW(dev)) {
1611 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1612 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1614 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1618 static void vlv_enable_pll(struct intel_crtc *crtc,
1619 const struct intel_crtc_state *pipe_config)
1621 struct drm_device *dev = crtc->base.dev;
1622 struct drm_i915_private *dev_priv = dev->dev_private;
1623 int reg = DPLL(crtc->pipe);
1624 u32 dpll = pipe_config->dpll_hw_state.dpll;
1626 assert_pipe_disabled(dev_priv, crtc->pipe);
1628 /* No really, not for ILK+ */
1629 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1631 /* PLL is protected by panel, make sure we can write it */
1632 if (IS_MOBILE(dev_priv->dev))
1633 assert_panel_unlocked(dev_priv, crtc->pipe);
1635 I915_WRITE(reg, dpll);
1639 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1640 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1642 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
1643 POSTING_READ(DPLL_MD(crtc->pipe));
1645 /* We do this three times for luck */
1646 I915_WRITE(reg, dpll);
1648 udelay(150); /* wait for warmup */
1649 I915_WRITE(reg, dpll);
1651 udelay(150); /* wait for warmup */
1652 I915_WRITE(reg, dpll);
1654 udelay(150); /* wait for warmup */
1657 static void chv_enable_pll(struct intel_crtc *crtc,
1658 const struct intel_crtc_state *pipe_config)
1660 struct drm_device *dev = crtc->base.dev;
1661 struct drm_i915_private *dev_priv = dev->dev_private;
1662 int pipe = crtc->pipe;
1663 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1666 assert_pipe_disabled(dev_priv, crtc->pipe);
1668 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1670 mutex_lock(&dev_priv->sb_lock);
1672 /* Enable back the 10bit clock to display controller */
1673 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1674 tmp |= DPIO_DCLKP_EN;
1675 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1677 mutex_unlock(&dev_priv->sb_lock);
1680 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1685 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1687 /* Check PLL is locked */
1688 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1689 DRM_ERROR("PLL %d failed to lock\n", pipe);
1691 /* not sure when this should be written */
1692 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1693 POSTING_READ(DPLL_MD(pipe));
1696 static int intel_num_dvo_pipes(struct drm_device *dev)
1698 struct intel_crtc *crtc;
1701 for_each_intel_crtc(dev, crtc)
1702 count += crtc->active &&
1703 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1708 static void i9xx_enable_pll(struct intel_crtc *crtc)
1710 struct drm_device *dev = crtc->base.dev;
1711 struct drm_i915_private *dev_priv = dev->dev_private;
1712 int reg = DPLL(crtc->pipe);
1713 u32 dpll = crtc->config->dpll_hw_state.dpll;
1715 assert_pipe_disabled(dev_priv, crtc->pipe);
1717 /* No really, not for ILK+ */
1718 BUG_ON(INTEL_INFO(dev)->gen >= 5);
1720 /* PLL is protected by panel, make sure we can write it */
1721 if (IS_MOBILE(dev) && !IS_I830(dev))
1722 assert_panel_unlocked(dev_priv, crtc->pipe);
1724 /* Enable DVO 2x clock on both PLLs if necessary */
1725 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1727 * It appears to be important that we don't enable this
1728 * for the current pipe before otherwise configuring the
1729 * PLL. No idea how this should be handled if multiple
1730 * DVO outputs are enabled simultaneosly.
1732 dpll |= DPLL_DVO_2X_MODE;
1733 I915_WRITE(DPLL(!crtc->pipe),
1734 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1737 /* Wait for the clocks to stabilize. */
1741 if (INTEL_INFO(dev)->gen >= 4) {
1742 I915_WRITE(DPLL_MD(crtc->pipe),
1743 crtc->config->dpll_hw_state.dpll_md);
1745 /* The pixel multiplier can only be updated once the
1746 * DPLL is enabled and the clocks are stable.
1748 * So write it again.
1750 I915_WRITE(reg, dpll);
1753 /* We do this three times for luck */
1754 I915_WRITE(reg, dpll);
1756 udelay(150); /* wait for warmup */
1757 I915_WRITE(reg, dpll);
1759 udelay(150); /* wait for warmup */
1760 I915_WRITE(reg, dpll);
1762 udelay(150); /* wait for warmup */
1766 * i9xx_disable_pll - disable a PLL
1767 * @dev_priv: i915 private structure
1768 * @pipe: pipe PLL to disable
1770 * Disable the PLL for @pipe, making sure the pipe is off first.
1772 * Note! This is for pre-ILK only.
1774 static void i9xx_disable_pll(struct intel_crtc *crtc)
1776 struct drm_device *dev = crtc->base.dev;
1777 struct drm_i915_private *dev_priv = dev->dev_private;
1778 enum pipe pipe = crtc->pipe;
1780 /* Disable DVO 2x clock on both PLLs if necessary */
1782 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
1783 intel_num_dvo_pipes(dev) == 1) {
1784 I915_WRITE(DPLL(PIPE_B),
1785 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1786 I915_WRITE(DPLL(PIPE_A),
1787 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1790 /* Don't disable pipe or pipe PLLs if needed */
1791 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1792 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
1795 /* Make sure the pipe isn't still relying on us */
1796 assert_pipe_disabled(dev_priv, pipe);
1798 I915_WRITE(DPLL(pipe), 0);
1799 POSTING_READ(DPLL(pipe));
1802 static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1806 /* Make sure the pipe isn't still relying on us */
1807 assert_pipe_disabled(dev_priv, pipe);
1810 * Leave integrated clock source and reference clock enabled for pipe B.
1811 * The latter is needed for VGA hotplug / manual detection.
1814 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
1815 I915_WRITE(DPLL(pipe), val);
1816 POSTING_READ(DPLL(pipe));
1820 static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1822 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1825 /* Make sure the pipe isn't still relying on us */
1826 assert_pipe_disabled(dev_priv, pipe);
1828 /* Set PLL en = 0 */
1829 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
1831 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1832 I915_WRITE(DPLL(pipe), val);
1833 POSTING_READ(DPLL(pipe));
1835 mutex_lock(&dev_priv->sb_lock);
1837 /* Disable 10bit clock to display controller */
1838 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1839 val &= ~DPIO_DCLKP_EN;
1840 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1842 /* disable left/right clock distribution */
1843 if (pipe != PIPE_B) {
1844 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1845 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1846 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1848 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1849 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1850 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1853 mutex_unlock(&dev_priv->sb_lock);
1856 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1857 struct intel_digital_port *dport,
1858 unsigned int expected_mask)
1863 switch (dport->port) {
1865 port_mask = DPLL_PORTB_READY_MASK;
1869 port_mask = DPLL_PORTC_READY_MASK;
1871 expected_mask <<= 4;
1874 port_mask = DPLL_PORTD_READY_MASK;
1875 dpll_reg = DPIO_PHY_STATUS;
1881 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1882 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1883 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
1886 static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1888 struct drm_device *dev = crtc->base.dev;
1889 struct drm_i915_private *dev_priv = dev->dev_private;
1890 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1892 if (WARN_ON(pll == NULL))
1895 WARN_ON(!pll->config.crtc_mask);
1896 if (pll->active == 0) {
1897 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1899 assert_shared_dpll_disabled(dev_priv, pll);
1901 pll->mode_set(dev_priv, pll);
1906 * intel_enable_shared_dpll - enable PCH PLL
1907 * @dev_priv: i915 private structure
1908 * @pipe: pipe PLL to enable
1910 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1911 * drives the transcoder clock.
1913 static void intel_enable_shared_dpll(struct intel_crtc *crtc)
1915 struct drm_device *dev = crtc->base.dev;
1916 struct drm_i915_private *dev_priv = dev->dev_private;
1917 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1919 if (WARN_ON(pll == NULL))
1922 if (WARN_ON(pll->config.crtc_mask == 0))
1925 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
1926 pll->name, pll->active, pll->on,
1927 crtc->base.base.id);
1929 if (pll->active++) {
1931 assert_shared_dpll_enabled(dev_priv, pll);
1936 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1938 DRM_DEBUG_KMS("enabling %s\n", pll->name);
1939 pll->enable(dev_priv, pll);
1943 static void intel_disable_shared_dpll(struct intel_crtc *crtc)
1945 struct drm_device *dev = crtc->base.dev;
1946 struct drm_i915_private *dev_priv = dev->dev_private;
1947 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1949 /* PCH only available on ILK+ */
1950 BUG_ON(INTEL_INFO(dev)->gen < 5);
1951 if (WARN_ON(pll == NULL))
1954 if (WARN_ON(pll->config.crtc_mask == 0))
1957 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1958 pll->name, pll->active, pll->on,
1959 crtc->base.base.id);
1961 if (WARN_ON(pll->active == 0)) {
1962 assert_shared_dpll_disabled(dev_priv, pll);
1966 assert_shared_dpll_enabled(dev_priv, pll);
1971 DRM_DEBUG_KMS("disabling %s\n", pll->name);
1972 pll->disable(dev_priv, pll);
1975 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
1978 static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1981 struct drm_device *dev = dev_priv->dev;
1982 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1984 uint32_t reg, val, pipeconf_val;
1986 /* PCH only available on ILK+ */
1987 BUG_ON(!HAS_PCH_SPLIT(dev));
1989 /* Make sure PCH DPLL is enabled */
1990 assert_shared_dpll_enabled(dev_priv,
1991 intel_crtc_to_shared_dpll(intel_crtc));
1993 /* FDI must be feeding us bits for PCH ports */
1994 assert_fdi_tx_enabled(dev_priv, pipe);
1995 assert_fdi_rx_enabled(dev_priv, pipe);
1997 if (HAS_PCH_CPT(dev)) {
1998 /* Workaround: Set the timing override bit before enabling the
1999 * pch transcoder. */
2000 reg = TRANS_CHICKEN2(pipe);
2001 val = I915_READ(reg);
2002 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2003 I915_WRITE(reg, val);
2006 reg = PCH_TRANSCONF(pipe);
2007 val = I915_READ(reg);
2008 pipeconf_val = I915_READ(PIPECONF(pipe));
2010 if (HAS_PCH_IBX(dev_priv->dev)) {
2012 * make the BPC in transcoder be consistent with
2013 * that in pipeconf reg.
2015 val &= ~PIPECONF_BPC_MASK;
2016 val |= pipeconf_val & PIPECONF_BPC_MASK;
2019 val &= ~TRANS_INTERLACE_MASK;
2020 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
2021 if (HAS_PCH_IBX(dev_priv->dev) &&
2022 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
2023 val |= TRANS_LEGACY_INTERLACED_ILK;
2025 val |= TRANS_INTERLACED;
2027 val |= TRANS_PROGRESSIVE;
2029 I915_WRITE(reg, val | TRANS_ENABLE);
2030 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
2031 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
2034 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
2035 enum transcoder cpu_transcoder)
2037 u32 val, pipeconf_val;
2039 /* PCH only available on ILK+ */
2040 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
2042 /* FDI must be feeding us bits for PCH ports */
2043 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
2044 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
2046 /* Workaround: set timing override bit. */
2047 val = I915_READ(_TRANSA_CHICKEN2);
2048 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2049 I915_WRITE(_TRANSA_CHICKEN2, val);
2052 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
2054 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2055 PIPECONF_INTERLACED_ILK)
2056 val |= TRANS_INTERLACED;
2058 val |= TRANS_PROGRESSIVE;
2060 I915_WRITE(LPT_TRANSCONF, val);
2061 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
2062 DRM_ERROR("Failed to enable PCH transcoder\n");
2065 static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2068 struct drm_device *dev = dev_priv->dev;
2071 /* FDI relies on the transcoder */
2072 assert_fdi_tx_disabled(dev_priv, pipe);
2073 assert_fdi_rx_disabled(dev_priv, pipe);
2075 /* Ports must be off as well */
2076 assert_pch_ports_disabled(dev_priv, pipe);
2078 reg = PCH_TRANSCONF(pipe);
2079 val = I915_READ(reg);
2080 val &= ~TRANS_ENABLE;
2081 I915_WRITE(reg, val);
2082 /* wait for PCH transcoder off, transcoder state */
2083 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
2084 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
2086 if (!HAS_PCH_IBX(dev)) {
2087 /* Workaround: Clear the timing override chicken bit again. */
2088 reg = TRANS_CHICKEN2(pipe);
2089 val = I915_READ(reg);
2090 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2091 I915_WRITE(reg, val);
2095 static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
2099 val = I915_READ(LPT_TRANSCONF);
2100 val &= ~TRANS_ENABLE;
2101 I915_WRITE(LPT_TRANSCONF, val);
2102 /* wait for PCH transcoder off, transcoder state */
2103 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
2104 DRM_ERROR("Failed to disable PCH transcoder\n");
2106 /* Workaround: clear timing override bit. */
2107 val = I915_READ(_TRANSA_CHICKEN2);
2108 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2109 I915_WRITE(_TRANSA_CHICKEN2, val);
2113 * intel_enable_pipe - enable a pipe, asserting requirements
2114 * @crtc: crtc responsible for the pipe
2116 * Enable @crtc's pipe, making sure that various hardware specific requirements
2117 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
2119 static void intel_enable_pipe(struct intel_crtc *crtc)
2121 struct drm_device *dev = crtc->base.dev;
2122 struct drm_i915_private *dev_priv = dev->dev_private;
2123 enum pipe pipe = crtc->pipe;
2124 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2126 enum pipe pch_transcoder;
2130 assert_planes_disabled(dev_priv, pipe);
2131 assert_cursor_disabled(dev_priv, pipe);
2132 assert_sprites_disabled(dev_priv, pipe);
2134 if (HAS_PCH_LPT(dev_priv->dev))
2135 pch_transcoder = TRANSCODER_A;
2137 pch_transcoder = pipe;
2140 * A pipe without a PLL won't actually be able to drive bits from
2141 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2144 if (HAS_GMCH_DISPLAY(dev_priv->dev))
2145 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
2146 assert_dsi_pll_enabled(dev_priv);
2148 assert_pll_enabled(dev_priv, pipe);
2150 if (crtc->config->has_pch_encoder) {
2151 /* if driving the PCH, we need FDI enabled */
2152 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
2153 assert_fdi_tx_pll_enabled(dev_priv,
2154 (enum pipe) cpu_transcoder);
2156 /* FIXME: assert CPU port conditions for SNB+ */
2159 reg = PIPECONF(cpu_transcoder);
2160 val = I915_READ(reg);
2161 if (val & PIPECONF_ENABLE) {
2162 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2163 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
2167 I915_WRITE(reg, val | PIPECONF_ENABLE);
2172 * intel_disable_pipe - disable a pipe, asserting requirements
2173 * @crtc: crtc whose pipes is to be disabled
2175 * Disable the pipe of @crtc, making sure that various hardware
2176 * specific requirements are met, if applicable, e.g. plane
2177 * disabled, panel fitter off, etc.
2179 * Will wait until the pipe has shut down before returning.
2181 static void intel_disable_pipe(struct intel_crtc *crtc)
2183 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2184 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
2185 enum pipe pipe = crtc->pipe;
2190 * Make sure planes won't keep trying to pump pixels to us,
2191 * or we might hang the display.
2193 assert_planes_disabled(dev_priv, pipe);
2194 assert_cursor_disabled(dev_priv, pipe);
2195 assert_sprites_disabled(dev_priv, pipe);
2197 reg = PIPECONF(cpu_transcoder);
2198 val = I915_READ(reg);
2199 if ((val & PIPECONF_ENABLE) == 0)
2203 * Double wide has implications for planes
2204 * so best keep it disabled when not needed.
2206 if (crtc->config->double_wide)
2207 val &= ~PIPECONF_DOUBLE_WIDE;
2209 /* Don't disable pipe or pipe PLLs if needed */
2210 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2211 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
2212 val &= ~PIPECONF_ENABLE;
2214 I915_WRITE(reg, val);
2215 if ((val & PIPECONF_ENABLE) == 0)
2216 intel_wait_for_pipe_off(crtc);
2220 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
2221 * @plane: plane to be enabled
2222 * @crtc: crtc for the plane
2224 * Enable @plane on @crtc, making sure that the pipe is running first.
2226 static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2227 struct drm_crtc *crtc)
2229 struct drm_device *dev = plane->dev;
2230 struct drm_i915_private *dev_priv = dev->dev_private;
2231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2233 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2234 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
2235 to_intel_plane_state(plane->state)->visible = true;
2237 dev_priv->display.update_primary_plane(crtc, plane->fb,
2241 static bool need_vtd_wa(struct drm_device *dev)
2243 #ifdef CONFIG_INTEL_IOMMU
2244 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2251 intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2252 uint64_t fb_format_modifier)
2254 unsigned int tile_height;
2255 uint32_t pixel_bytes;
2257 switch (fb_format_modifier) {
2258 case DRM_FORMAT_MOD_NONE:
2261 case I915_FORMAT_MOD_X_TILED:
2262 tile_height = IS_GEN2(dev) ? 16 : 8;
2264 case I915_FORMAT_MOD_Y_TILED:
2267 case I915_FORMAT_MOD_Yf_TILED:
2268 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2269 switch (pixel_bytes) {
2283 "128-bit pixels are not supported for display!");
2289 MISSING_CASE(fb_format_modifier);
2298 intel_fb_align_height(struct drm_device *dev, unsigned int height,
2299 uint32_t pixel_format, uint64_t fb_format_modifier)
2301 return ALIGN(height, intel_tile_height(dev, pixel_format,
2302 fb_format_modifier));
2306 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2307 const struct drm_plane_state *plane_state)
2309 struct intel_rotation_info *info = &view->rotation_info;
2311 *view = i915_ggtt_view_normal;
2316 if (!intel_rotation_90_or_270(plane_state->rotation))
2319 *view = i915_ggtt_view_rotated;
2321 info->height = fb->height;
2322 info->pixel_format = fb->pixel_format;
2323 info->pitch = fb->pitches[0];
2324 info->fb_modifier = fb->modifier[0];
2330 intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2331 struct drm_framebuffer *fb,
2332 const struct drm_plane_state *plane_state,
2333 struct intel_engine_cs *pipelined)
2335 struct drm_device *dev = fb->dev;
2336 struct drm_i915_private *dev_priv = dev->dev_private;
2337 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2338 struct i915_ggtt_view view;
2342 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2344 switch (fb->modifier[0]) {
2345 case DRM_FORMAT_MOD_NONE:
2346 if (INTEL_INFO(dev)->gen >= 9)
2347 alignment = 256 * 1024;
2348 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2349 alignment = 128 * 1024;
2350 else if (INTEL_INFO(dev)->gen >= 4)
2351 alignment = 4 * 1024;
2353 alignment = 64 * 1024;
2355 case I915_FORMAT_MOD_X_TILED:
2356 if (INTEL_INFO(dev)->gen >= 9)
2357 alignment = 256 * 1024;
2359 /* pin() will align the object as required by fence */
2363 case I915_FORMAT_MOD_Y_TILED:
2364 case I915_FORMAT_MOD_Yf_TILED:
2365 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2366 "Y tiling bo slipped through, driver bug!\n"))
2368 alignment = 1 * 1024 * 1024;
2371 MISSING_CASE(fb->modifier[0]);
2375 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2379 /* Note that the w/a also requires 64 PTE of padding following the
2380 * bo. We currently fill all unused PTE with the shadow page and so
2381 * we should always have valid PTE following the scanout preventing
2384 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2385 alignment = 256 * 1024;
2388 * Global gtt pte registers are special registers which actually forward
2389 * writes to a chunk of system memory. Which means that there is no risk
2390 * that the register values disappear as soon as we call
2391 * intel_runtime_pm_put(), so it is correct to wrap only the
2392 * pin/unpin/fence and not more.
2394 intel_runtime_pm_get(dev_priv);
2396 dev_priv->mm.interruptible = false;
2397 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
2400 goto err_interruptible;
2402 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2403 * fence, whereas 965+ only requires a fence if using
2404 * framebuffer compression. For simplicity, we always install
2405 * a fence as the cost is not that onerous.
2407 ret = i915_gem_object_get_fence(obj);
2411 i915_gem_object_pin_fence(obj);
2413 dev_priv->mm.interruptible = true;
2414 intel_runtime_pm_put(dev_priv);
2418 i915_gem_object_unpin_from_display_plane(obj, &view);
2420 dev_priv->mm.interruptible = true;
2421 intel_runtime_pm_put(dev_priv);
2425 static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2426 const struct drm_plane_state *plane_state)
2428 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2429 struct i915_ggtt_view view;
2432 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2434 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2435 WARN_ONCE(ret, "Couldn't get view from plane state!");
2437 i915_gem_object_unpin_fence(obj);
2438 i915_gem_object_unpin_from_display_plane(obj, &view);
2441 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2442 * is assumed to be a power-of-two. */
2443 unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2444 unsigned int tiling_mode,
2448 if (tiling_mode != I915_TILING_NONE) {
2449 unsigned int tile_rows, tiles;
2454 tiles = *x / (512/cpp);
2457 return tile_rows * pitch * 8 + tiles * 4096;
2459 unsigned int offset;
2461 offset = *y * pitch + *x * cpp;
2463 *x = (offset & 4095) / cpp;
2464 return offset & -4096;
2468 static int i9xx_format_to_fourcc(int format)
2471 case DISPPLANE_8BPP:
2472 return DRM_FORMAT_C8;
2473 case DISPPLANE_BGRX555:
2474 return DRM_FORMAT_XRGB1555;
2475 case DISPPLANE_BGRX565:
2476 return DRM_FORMAT_RGB565;
2478 case DISPPLANE_BGRX888:
2479 return DRM_FORMAT_XRGB8888;
2480 case DISPPLANE_RGBX888:
2481 return DRM_FORMAT_XBGR8888;
2482 case DISPPLANE_BGRX101010:
2483 return DRM_FORMAT_XRGB2101010;
2484 case DISPPLANE_RGBX101010:
2485 return DRM_FORMAT_XBGR2101010;
2489 static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2492 case PLANE_CTL_FORMAT_RGB_565:
2493 return DRM_FORMAT_RGB565;
2495 case PLANE_CTL_FORMAT_XRGB_8888:
2498 return DRM_FORMAT_ABGR8888;
2500 return DRM_FORMAT_XBGR8888;
2503 return DRM_FORMAT_ARGB8888;
2505 return DRM_FORMAT_XRGB8888;
2507 case PLANE_CTL_FORMAT_XRGB_2101010:
2509 return DRM_FORMAT_XBGR2101010;
2511 return DRM_FORMAT_XRGB2101010;
2516 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2517 struct intel_initial_plane_config *plane_config)
2519 struct drm_device *dev = crtc->base.dev;
2520 struct drm_i915_gem_object *obj = NULL;
2521 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2522 struct drm_framebuffer *fb = &plane_config->fb->base;
2523 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2524 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2527 size_aligned -= base_aligned;
2529 if (plane_config->size == 0)
2532 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2539 obj->tiling_mode = plane_config->tiling;
2540 if (obj->tiling_mode == I915_TILING_X)
2541 obj->stride = fb->pitches[0];
2543 mode_cmd.pixel_format = fb->pixel_format;
2544 mode_cmd.width = fb->width;
2545 mode_cmd.height = fb->height;
2546 mode_cmd.pitches[0] = fb->pitches[0];
2547 mode_cmd.modifier[0] = fb->modifier[0];
2548 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
2550 mutex_lock(&dev->struct_mutex);
2551 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
2553 DRM_DEBUG_KMS("intel fb init failed\n");
2556 mutex_unlock(&dev->struct_mutex);
2558 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
2562 drm_gem_object_unreference(&obj->base);
2563 mutex_unlock(&dev->struct_mutex);
2567 /* Update plane->state->fb to match plane->fb after driver-internal updates */
2569 update_state_fb(struct drm_plane *plane)
2571 if (plane->fb == plane->state->fb)
2574 if (plane->state->fb)
2575 drm_framebuffer_unreference(plane->state->fb);
2576 plane->state->fb = plane->fb;
2577 if (plane->state->fb)
2578 drm_framebuffer_reference(plane->state->fb);
2582 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2583 struct intel_initial_plane_config *plane_config)
2585 struct drm_device *dev = intel_crtc->base.dev;
2586 struct drm_i915_private *dev_priv = dev->dev_private;
2588 struct intel_crtc *i;
2589 struct drm_i915_gem_object *obj;
2590 struct drm_plane *primary = intel_crtc->base.primary;
2591 struct drm_framebuffer *fb;
2593 if (!plane_config->fb)
2596 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
2597 fb = &plane_config->fb->base;
2601 kfree(plane_config->fb);
2604 * Failed to alloc the obj, check to see if we should share
2605 * an fb with another CRTC instead
2607 for_each_crtc(dev, c) {
2608 i = to_intel_crtc(c);
2610 if (c == &intel_crtc->base)
2616 fb = c->primary->fb;
2620 obj = intel_fb_obj(fb);
2621 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
2622 drm_framebuffer_reference(fb);
2630 obj = intel_fb_obj(fb);
2631 if (obj->tiling_mode != I915_TILING_NONE)
2632 dev_priv->preserve_bios_swizzle = true;
2635 primary->crtc = primary->state->crtc = &intel_crtc->base;
2636 update_state_fb(primary);
2637 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
2638 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
2641 static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2642 struct drm_framebuffer *fb,
2645 struct drm_device *dev = crtc->dev;
2646 struct drm_i915_private *dev_priv = dev->dev_private;
2647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2648 struct drm_plane *primary = crtc->primary;
2649 bool visible = to_intel_plane_state(primary->state)->visible;
2650 struct drm_i915_gem_object *obj;
2651 int plane = intel_crtc->plane;
2652 unsigned long linear_offset;
2654 u32 reg = DSPCNTR(plane);
2657 if (!visible || !fb) {
2659 if (INTEL_INFO(dev)->gen >= 4)
2660 I915_WRITE(DSPSURF(plane), 0);
2662 I915_WRITE(DSPADDR(plane), 0);
2667 obj = intel_fb_obj(fb);
2668 if (WARN_ON(obj == NULL))
2671 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2673 dspcntr = DISPPLANE_GAMMA_ENABLE;
2675 dspcntr |= DISPLAY_PLANE_ENABLE;
2677 if (INTEL_INFO(dev)->gen < 4) {
2678 if (intel_crtc->pipe == PIPE_B)
2679 dspcntr |= DISPPLANE_SEL_PIPE_B;
2681 /* pipesrc and dspsize control the size that is scaled from,
2682 * which should always be the user's requested size.
2684 I915_WRITE(DSPSIZE(plane),
2685 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2686 (intel_crtc->config->pipe_src_w - 1));
2687 I915_WRITE(DSPPOS(plane), 0);
2688 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2689 I915_WRITE(PRIMSIZE(plane),
2690 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2691 (intel_crtc->config->pipe_src_w - 1));
2692 I915_WRITE(PRIMPOS(plane), 0);
2693 I915_WRITE(PRIMCNSTALPHA(plane), 0);
2696 switch (fb->pixel_format) {
2698 dspcntr |= DISPPLANE_8BPP;
2700 case DRM_FORMAT_XRGB1555:
2701 dspcntr |= DISPPLANE_BGRX555;
2703 case DRM_FORMAT_RGB565:
2704 dspcntr |= DISPPLANE_BGRX565;
2706 case DRM_FORMAT_XRGB8888:
2707 dspcntr |= DISPPLANE_BGRX888;
2709 case DRM_FORMAT_XBGR8888:
2710 dspcntr |= DISPPLANE_RGBX888;
2712 case DRM_FORMAT_XRGB2101010:
2713 dspcntr |= DISPPLANE_BGRX101010;
2715 case DRM_FORMAT_XBGR2101010:
2716 dspcntr |= DISPPLANE_RGBX101010;
2722 if (INTEL_INFO(dev)->gen >= 4 &&
2723 obj->tiling_mode != I915_TILING_NONE)
2724 dspcntr |= DISPPLANE_TILED;
2727 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2729 linear_offset = y * fb->pitches[0] + x * pixel_size;
2731 if (INTEL_INFO(dev)->gen >= 4) {
2732 intel_crtc->dspaddr_offset =
2733 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2736 linear_offset -= intel_crtc->dspaddr_offset;
2738 intel_crtc->dspaddr_offset = linear_offset;
2741 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
2742 dspcntr |= DISPPLANE_ROTATE_180;
2744 x += (intel_crtc->config->pipe_src_w - 1);
2745 y += (intel_crtc->config->pipe_src_h - 1);
2747 /* Finding the last pixel of the last line of the display
2748 data and adding to linear_offset*/
2750 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2751 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
2754 I915_WRITE(reg, dspcntr);
2756 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2757 if (INTEL_INFO(dev)->gen >= 4) {
2758 I915_WRITE(DSPSURF(plane),
2759 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2760 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2761 I915_WRITE(DSPLINOFF(plane), linear_offset);
2763 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
2767 static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2768 struct drm_framebuffer *fb,
2771 struct drm_device *dev = crtc->dev;
2772 struct drm_i915_private *dev_priv = dev->dev_private;
2773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2774 struct drm_plane *primary = crtc->primary;
2775 bool visible = to_intel_plane_state(primary->state)->visible;
2776 struct drm_i915_gem_object *obj;
2777 int plane = intel_crtc->plane;
2778 unsigned long linear_offset;
2780 u32 reg = DSPCNTR(plane);
2783 if (!visible || !fb) {
2785 I915_WRITE(DSPSURF(plane), 0);
2790 obj = intel_fb_obj(fb);
2791 if (WARN_ON(obj == NULL))
2794 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2796 dspcntr = DISPPLANE_GAMMA_ENABLE;
2798 dspcntr |= DISPLAY_PLANE_ENABLE;
2800 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2801 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2803 switch (fb->pixel_format) {
2805 dspcntr |= DISPPLANE_8BPP;
2807 case DRM_FORMAT_RGB565:
2808 dspcntr |= DISPPLANE_BGRX565;
2810 case DRM_FORMAT_XRGB8888:
2811 dspcntr |= DISPPLANE_BGRX888;
2813 case DRM_FORMAT_XBGR8888:
2814 dspcntr |= DISPPLANE_RGBX888;
2816 case DRM_FORMAT_XRGB2101010:
2817 dspcntr |= DISPPLANE_BGRX101010;
2819 case DRM_FORMAT_XBGR2101010:
2820 dspcntr |= DISPPLANE_RGBX101010;
2826 if (obj->tiling_mode != I915_TILING_NONE)
2827 dspcntr |= DISPPLANE_TILED;
2829 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
2830 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2832 linear_offset = y * fb->pitches[0] + x * pixel_size;
2833 intel_crtc->dspaddr_offset =
2834 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2837 linear_offset -= intel_crtc->dspaddr_offset;
2838 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
2839 dspcntr |= DISPPLANE_ROTATE_180;
2841 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2842 x += (intel_crtc->config->pipe_src_w - 1);
2843 y += (intel_crtc->config->pipe_src_h - 1);
2845 /* Finding the last pixel of the last line of the display
2846 data and adding to linear_offset*/
2848 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2849 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
2853 I915_WRITE(reg, dspcntr);
2855 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2856 I915_WRITE(DSPSURF(plane),
2857 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2858 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2859 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2861 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2862 I915_WRITE(DSPLINOFF(plane), linear_offset);
2867 u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2868 uint32_t pixel_format)
2870 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2873 * The stride is either expressed as a multiple of 64 bytes
2874 * chunks for linear buffers or in number of tiles for tiled
2877 switch (fb_modifier) {
2878 case DRM_FORMAT_MOD_NONE:
2880 case I915_FORMAT_MOD_X_TILED:
2881 if (INTEL_INFO(dev)->gen == 2)
2884 case I915_FORMAT_MOD_Y_TILED:
2885 /* No need to check for old gens and Y tiling since this is
2886 * about the display engine and those will be blocked before
2890 case I915_FORMAT_MOD_Yf_TILED:
2891 if (bits_per_pixel == 8)
2896 MISSING_CASE(fb_modifier);
2901 unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2902 struct drm_i915_gem_object *obj)
2904 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
2906 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
2907 view = &i915_ggtt_view_rotated;
2909 return i915_gem_obj_ggtt_offset_view(obj, view);
2913 * This function detaches (aka. unbinds) unused scalers in hardware
2915 void skl_detach_scalers(struct intel_crtc *intel_crtc)
2917 struct drm_device *dev;
2918 struct drm_i915_private *dev_priv;
2919 struct intel_crtc_scaler_state *scaler_state;
2922 if (!intel_crtc || !intel_crtc->config)
2925 dev = intel_crtc->base.dev;
2926 dev_priv = dev->dev_private;
2927 scaler_state = &intel_crtc->config->scaler_state;
2929 /* loop through and disable scalers that aren't in use */
2930 for (i = 0; i < intel_crtc->num_scalers; i++) {
2931 if (!scaler_state->scalers[i].in_use) {
2932 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2933 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2934 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2935 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2936 intel_crtc->base.base.id, intel_crtc->pipe, i);
2941 u32 skl_plane_ctl_format(uint32_t pixel_format)
2943 switch (pixel_format) {
2945 return PLANE_CTL_FORMAT_INDEXED;
2946 case DRM_FORMAT_RGB565:
2947 return PLANE_CTL_FORMAT_RGB_565;
2948 case DRM_FORMAT_XBGR8888:
2949 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
2950 case DRM_FORMAT_XRGB8888:
2951 return PLANE_CTL_FORMAT_XRGB_8888;
2953 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2954 * to be already pre-multiplied. We need to add a knob (or a different
2955 * DRM_FORMAT) for user-space to configure that.
2957 case DRM_FORMAT_ABGR8888:
2958 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
2959 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2960 case DRM_FORMAT_ARGB8888:
2961 return PLANE_CTL_FORMAT_XRGB_8888 |
2962 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2963 case DRM_FORMAT_XRGB2101010:
2964 return PLANE_CTL_FORMAT_XRGB_2101010;
2965 case DRM_FORMAT_XBGR2101010:
2966 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
2967 case DRM_FORMAT_YUYV:
2968 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
2969 case DRM_FORMAT_YVYU:
2970 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
2971 case DRM_FORMAT_UYVY:
2972 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
2973 case DRM_FORMAT_VYUY:
2974 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
2976 MISSING_CASE(pixel_format);
2982 u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2984 switch (fb_modifier) {
2985 case DRM_FORMAT_MOD_NONE:
2987 case I915_FORMAT_MOD_X_TILED:
2988 return PLANE_CTL_TILED_X;
2989 case I915_FORMAT_MOD_Y_TILED:
2990 return PLANE_CTL_TILED_Y;
2991 case I915_FORMAT_MOD_Yf_TILED:
2992 return PLANE_CTL_TILED_YF;
2994 MISSING_CASE(fb_modifier);
3000 u32 skl_plane_ctl_rotation(unsigned int rotation)
3003 case BIT(DRM_ROTATE_0):
3006 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3007 * while i915 HW rotation is clockwise, thats why this swapping.
3009 case BIT(DRM_ROTATE_90):
3010 return PLANE_CTL_ROTATE_270;
3011 case BIT(DRM_ROTATE_180):
3012 return PLANE_CTL_ROTATE_180;
3013 case BIT(DRM_ROTATE_270):
3014 return PLANE_CTL_ROTATE_90;
3016 MISSING_CASE(rotation);
3022 static void skylake_update_primary_plane(struct drm_crtc *crtc,
3023 struct drm_framebuffer *fb,
3026 struct drm_device *dev = crtc->dev;
3027 struct drm_i915_private *dev_priv = dev->dev_private;
3028 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3029 struct drm_plane *plane = crtc->primary;
3030 bool visible = to_intel_plane_state(plane->state)->visible;
3031 struct drm_i915_gem_object *obj;
3032 int pipe = intel_crtc->pipe;
3033 u32 plane_ctl, stride_div, stride;
3034 u32 tile_height, plane_offset, plane_size;
3035 unsigned int rotation;
3036 int x_offset, y_offset;
3037 unsigned long surf_addr;
3038 struct intel_crtc_state *crtc_state = intel_crtc->config;
3039 struct intel_plane_state *plane_state;
3040 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3041 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3044 plane_state = to_intel_plane_state(plane->state);
3046 if (!visible || !fb) {
3047 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3048 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3049 POSTING_READ(PLANE_CTL(pipe, 0));
3053 plane_ctl = PLANE_CTL_ENABLE |
3054 PLANE_CTL_PIPE_GAMMA_ENABLE |
3055 PLANE_CTL_PIPE_CSC_ENABLE;
3057 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3058 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3059 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3061 rotation = plane->state->rotation;
3062 plane_ctl |= skl_plane_ctl_rotation(rotation);
3064 obj = intel_fb_obj(fb);
3065 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3067 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3070 * FIXME: intel_plane_state->src, dst aren't set when transitional
3071 * update_plane helpers are called from legacy paths.
3072 * Once full atomic crtc is available, below check can be avoided.
3074 if (drm_rect_width(&plane_state->src)) {
3075 scaler_id = plane_state->scaler_id;
3076 src_x = plane_state->src.x1 >> 16;
3077 src_y = plane_state->src.y1 >> 16;
3078 src_w = drm_rect_width(&plane_state->src) >> 16;
3079 src_h = drm_rect_height(&plane_state->src) >> 16;
3080 dst_x = plane_state->dst.x1;
3081 dst_y = plane_state->dst.y1;
3082 dst_w = drm_rect_width(&plane_state->dst);
3083 dst_h = drm_rect_height(&plane_state->dst);
3085 WARN_ON(x != src_x || y != src_y);
3087 src_w = intel_crtc->config->pipe_src_w;
3088 src_h = intel_crtc->config->pipe_src_h;
3091 if (intel_rotation_90_or_270(rotation)) {
3092 /* stride = Surface height in tiles */
3093 tile_height = intel_tile_height(dev, fb->pixel_format,
3095 stride = DIV_ROUND_UP(fb->height, tile_height);
3096 x_offset = stride * tile_height - y - src_h;
3098 plane_size = (src_w - 1) << 16 | (src_h - 1);
3100 stride = fb->pitches[0] / stride_div;
3103 plane_size = (src_h - 1) << 16 | (src_w - 1);
3105 plane_offset = y_offset << 16 | x_offset;
3107 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3108 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3109 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3110 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
3112 if (scaler_id >= 0) {
3113 uint32_t ps_ctrl = 0;
3115 WARN_ON(!dst_w || !dst_h);
3116 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3117 crtc_state->scaler_state.scalers[scaler_id].mode;
3118 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3119 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3120 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3121 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3122 I915_WRITE(PLANE_POS(pipe, 0), 0);
3124 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3127 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
3129 POSTING_READ(PLANE_SURF(pipe, 0));
3132 /* Assume fb object is pinned & idle & fenced and just update base pointers */
3134 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3135 int x, int y, enum mode_set_atomic state)
3137 struct drm_device *dev = crtc->dev;
3138 struct drm_i915_private *dev_priv = dev->dev_private;
3140 if (dev_priv->display.disable_fbc)
3141 dev_priv->display.disable_fbc(dev);
3143 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3148 static void intel_complete_page_flips(struct drm_device *dev)
3150 struct drm_crtc *crtc;
3152 for_each_crtc(dev, crtc) {
3153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3154 enum plane plane = intel_crtc->plane;
3156 intel_prepare_page_flip(dev, plane);
3157 intel_finish_page_flip_plane(dev, plane);
3161 static void intel_update_primary_planes(struct drm_device *dev)
3163 struct drm_i915_private *dev_priv = dev->dev_private;
3164 struct drm_crtc *crtc;
3166 for_each_crtc(dev, crtc) {
3167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3169 drm_modeset_lock(&crtc->mutex, NULL);
3171 * FIXME: Once we have proper support for primary planes (and
3172 * disabling them without disabling the entire crtc) allow again
3173 * a NULL crtc->primary->fb.
3175 if (intel_crtc->active && crtc->primary->fb)
3176 dev_priv->display.update_primary_plane(crtc,
3180 drm_modeset_unlock(&crtc->mutex);
3184 void intel_prepare_reset(struct drm_device *dev)
3186 /* no reset support for gen2 */
3190 /* reset doesn't touch the display */
3191 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3194 drm_modeset_lock_all(dev);
3196 * Disabling the crtcs gracefully seems nicer. Also the
3197 * g33 docs say we should at least disable all the planes.
3199 intel_display_suspend(dev);
3202 void intel_finish_reset(struct drm_device *dev)
3204 struct drm_i915_private *dev_priv = to_i915(dev);
3207 * Flips in the rings will be nuked by the reset,
3208 * so complete all pending flips so that user space
3209 * will get its events and not get stuck.
3211 intel_complete_page_flips(dev);
3213 /* no reset support for gen2 */
3217 /* reset doesn't touch the display */
3218 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3220 * Flips in the rings have been nuked by the reset,
3221 * so update the base address of all primary
3222 * planes to the the last fb to make sure we're
3223 * showing the correct fb after a reset.
3225 intel_update_primary_planes(dev);
3230 * The display has been reset as well,
3231 * so need a full re-initialization.
3233 intel_runtime_pm_disable_interrupts(dev_priv);
3234 intel_runtime_pm_enable_interrupts(dev_priv);
3236 intel_modeset_init_hw(dev);
3238 spin_lock_irq(&dev_priv->irq_lock);
3239 if (dev_priv->display.hpd_irq_setup)
3240 dev_priv->display.hpd_irq_setup(dev);
3241 spin_unlock_irq(&dev_priv->irq_lock);
3243 intel_modeset_setup_hw_state(dev, true);
3245 intel_hpd_init(dev_priv);
3247 drm_modeset_unlock_all(dev);
3251 intel_finish_fb(struct drm_framebuffer *old_fb)
3253 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
3254 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
3255 bool was_interruptible = dev_priv->mm.interruptible;
3258 /* Big Hammer, we also need to ensure that any pending
3259 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3260 * current scanout is retired before unpinning the old
3261 * framebuffer. Note that we rely on userspace rendering
3262 * into the buffer attached to the pipe they are waiting
3263 * on. If not, userspace generates a GPU hang with IPEHR
3264 * point to the MI_WAIT_FOR_EVENT.
3266 * This should only fail upon a hung GPU, in which case we
3267 * can safely continue.
3269 dev_priv->mm.interruptible = false;
3270 ret = i915_gem_object_wait_rendering(obj, true);
3271 dev_priv->mm.interruptible = was_interruptible;
3276 static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3278 struct drm_device *dev = crtc->dev;
3279 struct drm_i915_private *dev_priv = dev->dev_private;
3280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3283 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3284 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3287 spin_lock_irq(&dev->event_lock);
3288 pending = to_intel_crtc(crtc)->unpin_work != NULL;
3289 spin_unlock_irq(&dev->event_lock);
3294 static void intel_update_pipe_size(struct intel_crtc *crtc)
3296 struct drm_device *dev = crtc->base.dev;
3297 struct drm_i915_private *dev_priv = dev->dev_private;
3298 const struct drm_display_mode *adjusted_mode;
3304 * Update pipe size and adjust fitter if needed: the reason for this is
3305 * that in compute_mode_changes we check the native mode (not the pfit
3306 * mode) to see if we can flip rather than do a full mode set. In the
3307 * fastboot case, we'll flip, but if we don't update the pipesrc and
3308 * pfit state, we'll end up with a big fb scanned out into the wrong
3311 * To fix this properly, we need to hoist the checks up into
3312 * compute_mode_changes (or above), check the actual pfit state and
3313 * whether the platform allows pfit disable with pipe active, and only
3314 * then update the pipesrc and pfit state, even on the flip path.
3317 adjusted_mode = &crtc->config->base.adjusted_mode;
3319 I915_WRITE(PIPESRC(crtc->pipe),
3320 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3321 (adjusted_mode->crtc_vdisplay - 1));
3322 if (!crtc->config->pch_pfit.enabled &&
3323 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3324 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3325 I915_WRITE(PF_CTL(crtc->pipe), 0);
3326 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3327 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3329 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3330 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
3333 static void intel_fdi_normal_train(struct drm_crtc *crtc)
3335 struct drm_device *dev = crtc->dev;
3336 struct drm_i915_private *dev_priv = dev->dev_private;
3337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3338 int pipe = intel_crtc->pipe;
3341 /* enable normal train */
3342 reg = FDI_TX_CTL(pipe);
3343 temp = I915_READ(reg);
3344 if (IS_IVYBRIDGE(dev)) {
3345 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3346 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
3348 temp &= ~FDI_LINK_TRAIN_NONE;
3349 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
3351 I915_WRITE(reg, temp);
3353 reg = FDI_RX_CTL(pipe);
3354 temp = I915_READ(reg);
3355 if (HAS_PCH_CPT(dev)) {
3356 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3357 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3359 temp &= ~FDI_LINK_TRAIN_NONE;
3360 temp |= FDI_LINK_TRAIN_NONE;
3362 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3364 /* wait one idle pattern time */
3368 /* IVB wants error correction enabled */
3369 if (IS_IVYBRIDGE(dev))
3370 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3371 FDI_FE_ERRC_ENABLE);
3374 /* The FDI link training functions for ILK/Ibexpeak. */
3375 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3377 struct drm_device *dev = crtc->dev;
3378 struct drm_i915_private *dev_priv = dev->dev_private;
3379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3380 int pipe = intel_crtc->pipe;
3381 u32 reg, temp, tries;
3383 /* FDI needs bits from pipe first */
3384 assert_pipe_enabled(dev_priv, pipe);
3386 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3388 reg = FDI_RX_IMR(pipe);
3389 temp = I915_READ(reg);
3390 temp &= ~FDI_RX_SYMBOL_LOCK;
3391 temp &= ~FDI_RX_BIT_LOCK;
3392 I915_WRITE(reg, temp);
3396 /* enable CPU FDI TX and PCH FDI RX */
3397 reg = FDI_TX_CTL(pipe);
3398 temp = I915_READ(reg);
3399 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3400 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3401 temp &= ~FDI_LINK_TRAIN_NONE;
3402 temp |= FDI_LINK_TRAIN_PATTERN_1;
3403 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3405 reg = FDI_RX_CTL(pipe);
3406 temp = I915_READ(reg);
3407 temp &= ~FDI_LINK_TRAIN_NONE;
3408 temp |= FDI_LINK_TRAIN_PATTERN_1;
3409 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3414 /* Ironlake workaround, enable clock pointer after FDI enable*/
3415 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3416 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3417 FDI_RX_PHASE_SYNC_POINTER_EN);
3419 reg = FDI_RX_IIR(pipe);
3420 for (tries = 0; tries < 5; tries++) {
3421 temp = I915_READ(reg);
3422 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3424 if ((temp & FDI_RX_BIT_LOCK)) {
3425 DRM_DEBUG_KMS("FDI train 1 done.\n");
3426 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3431 DRM_ERROR("FDI train 1 fail!\n");
3434 reg = FDI_TX_CTL(pipe);
3435 temp = I915_READ(reg);
3436 temp &= ~FDI_LINK_TRAIN_NONE;
3437 temp |= FDI_LINK_TRAIN_PATTERN_2;
3438 I915_WRITE(reg, temp);
3440 reg = FDI_RX_CTL(pipe);
3441 temp = I915_READ(reg);
3442 temp &= ~FDI_LINK_TRAIN_NONE;
3443 temp |= FDI_LINK_TRAIN_PATTERN_2;
3444 I915_WRITE(reg, temp);
3449 reg = FDI_RX_IIR(pipe);
3450 for (tries = 0; tries < 5; tries++) {
3451 temp = I915_READ(reg);
3452 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3454 if (temp & FDI_RX_SYMBOL_LOCK) {
3455 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3456 DRM_DEBUG_KMS("FDI train 2 done.\n");
3461 DRM_ERROR("FDI train 2 fail!\n");
3463 DRM_DEBUG_KMS("FDI train done\n");
3467 static const int snb_b_fdi_train_param[] = {
3468 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3469 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3470 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3471 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3474 /* The FDI link training functions for SNB/Cougarpoint. */
3475 static void gen6_fdi_link_train(struct drm_crtc *crtc)
3477 struct drm_device *dev = crtc->dev;
3478 struct drm_i915_private *dev_priv = dev->dev_private;
3479 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3480 int pipe = intel_crtc->pipe;
3481 u32 reg, temp, i, retry;
3483 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3485 reg = FDI_RX_IMR(pipe);
3486 temp = I915_READ(reg);
3487 temp &= ~FDI_RX_SYMBOL_LOCK;
3488 temp &= ~FDI_RX_BIT_LOCK;
3489 I915_WRITE(reg, temp);
3494 /* enable CPU FDI TX and PCH FDI RX */
3495 reg = FDI_TX_CTL(pipe);
3496 temp = I915_READ(reg);
3497 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3498 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3499 temp &= ~FDI_LINK_TRAIN_NONE;
3500 temp |= FDI_LINK_TRAIN_PATTERN_1;
3501 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3503 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3504 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3506 I915_WRITE(FDI_RX_MISC(pipe),
3507 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3509 reg = FDI_RX_CTL(pipe);
3510 temp = I915_READ(reg);
3511 if (HAS_PCH_CPT(dev)) {
3512 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3513 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3515 temp &= ~FDI_LINK_TRAIN_NONE;
3516 temp |= FDI_LINK_TRAIN_PATTERN_1;
3518 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3523 for (i = 0; i < 4; i++) {
3524 reg = FDI_TX_CTL(pipe);
3525 temp = I915_READ(reg);
3526 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3527 temp |= snb_b_fdi_train_param[i];
3528 I915_WRITE(reg, temp);
3533 for (retry = 0; retry < 5; retry++) {
3534 reg = FDI_RX_IIR(pipe);
3535 temp = I915_READ(reg);
3536 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3537 if (temp & FDI_RX_BIT_LOCK) {
3538 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3539 DRM_DEBUG_KMS("FDI train 1 done.\n");
3548 DRM_ERROR("FDI train 1 fail!\n");
3551 reg = FDI_TX_CTL(pipe);
3552 temp = I915_READ(reg);
3553 temp &= ~FDI_LINK_TRAIN_NONE;
3554 temp |= FDI_LINK_TRAIN_PATTERN_2;
3556 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3558 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3560 I915_WRITE(reg, temp);
3562 reg = FDI_RX_CTL(pipe);
3563 temp = I915_READ(reg);
3564 if (HAS_PCH_CPT(dev)) {
3565 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3566 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3568 temp &= ~FDI_LINK_TRAIN_NONE;
3569 temp |= FDI_LINK_TRAIN_PATTERN_2;
3571 I915_WRITE(reg, temp);
3576 for (i = 0; i < 4; i++) {
3577 reg = FDI_TX_CTL(pipe);
3578 temp = I915_READ(reg);
3579 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3580 temp |= snb_b_fdi_train_param[i];
3581 I915_WRITE(reg, temp);
3586 for (retry = 0; retry < 5; retry++) {
3587 reg = FDI_RX_IIR(pipe);
3588 temp = I915_READ(reg);
3589 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3590 if (temp & FDI_RX_SYMBOL_LOCK) {
3591 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3592 DRM_DEBUG_KMS("FDI train 2 done.\n");
3601 DRM_ERROR("FDI train 2 fail!\n");
3603 DRM_DEBUG_KMS("FDI train done.\n");
3606 /* Manual link training for Ivy Bridge A0 parts */
3607 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3609 struct drm_device *dev = crtc->dev;
3610 struct drm_i915_private *dev_priv = dev->dev_private;
3611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3612 int pipe = intel_crtc->pipe;
3613 u32 reg, temp, i, j;
3615 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3617 reg = FDI_RX_IMR(pipe);
3618 temp = I915_READ(reg);
3619 temp &= ~FDI_RX_SYMBOL_LOCK;
3620 temp &= ~FDI_RX_BIT_LOCK;
3621 I915_WRITE(reg, temp);
3626 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3627 I915_READ(FDI_RX_IIR(pipe)));
3629 /* Try each vswing and preemphasis setting twice before moving on */
3630 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3631 /* disable first in case we need to retry */
3632 reg = FDI_TX_CTL(pipe);
3633 temp = I915_READ(reg);
3634 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3635 temp &= ~FDI_TX_ENABLE;
3636 I915_WRITE(reg, temp);
3638 reg = FDI_RX_CTL(pipe);
3639 temp = I915_READ(reg);
3640 temp &= ~FDI_LINK_TRAIN_AUTO;
3641 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3642 temp &= ~FDI_RX_ENABLE;
3643 I915_WRITE(reg, temp);
3645 /* enable CPU FDI TX and PCH FDI RX */
3646 reg = FDI_TX_CTL(pipe);
3647 temp = I915_READ(reg);
3648 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3649 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3650 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
3651 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3652 temp |= snb_b_fdi_train_param[j/2];
3653 temp |= FDI_COMPOSITE_SYNC;
3654 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3656 I915_WRITE(FDI_RX_MISC(pipe),
3657 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3659 reg = FDI_RX_CTL(pipe);
3660 temp = I915_READ(reg);
3661 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3662 temp |= FDI_COMPOSITE_SYNC;
3663 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3666 udelay(1); /* should be 0.5us */
3668 for (i = 0; i < 4; i++) {
3669 reg = FDI_RX_IIR(pipe);
3670 temp = I915_READ(reg);
3671 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3673 if (temp & FDI_RX_BIT_LOCK ||
3674 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3675 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3676 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3680 udelay(1); /* should be 0.5us */
3683 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3688 reg = FDI_TX_CTL(pipe);
3689 temp = I915_READ(reg);
3690 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3691 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3692 I915_WRITE(reg, temp);
3694 reg = FDI_RX_CTL(pipe);
3695 temp = I915_READ(reg);
3696 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3697 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3698 I915_WRITE(reg, temp);
3701 udelay(2); /* should be 1.5us */
3703 for (i = 0; i < 4; i++) {
3704 reg = FDI_RX_IIR(pipe);
3705 temp = I915_READ(reg);
3706 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3708 if (temp & FDI_RX_SYMBOL_LOCK ||
3709 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3710 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3711 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3715 udelay(2); /* should be 1.5us */
3718 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
3722 DRM_DEBUG_KMS("FDI train done.\n");
3725 static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
3727 struct drm_device *dev = intel_crtc->base.dev;
3728 struct drm_i915_private *dev_priv = dev->dev_private;
3729 int pipe = intel_crtc->pipe;
3733 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
3734 reg = FDI_RX_CTL(pipe);
3735 temp = I915_READ(reg);
3736 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3737 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3738 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3739 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3744 /* Switch from Rawclk to PCDclk */
3745 temp = I915_READ(reg);
3746 I915_WRITE(reg, temp | FDI_PCDCLK);
3751 /* Enable CPU FDI TX PLL, always on for Ironlake */
3752 reg = FDI_TX_CTL(pipe);
3753 temp = I915_READ(reg);
3754 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3755 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
3762 static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3764 struct drm_device *dev = intel_crtc->base.dev;
3765 struct drm_i915_private *dev_priv = dev->dev_private;
3766 int pipe = intel_crtc->pipe;
3769 /* Switch from PCDclk to Rawclk */
3770 reg = FDI_RX_CTL(pipe);
3771 temp = I915_READ(reg);
3772 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3774 /* Disable CPU FDI TX PLL */
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
3777 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3782 reg = FDI_RX_CTL(pipe);
3783 temp = I915_READ(reg);
3784 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3786 /* Wait for the clocks to turn off. */
3791 static void ironlake_fdi_disable(struct drm_crtc *crtc)
3793 struct drm_device *dev = crtc->dev;
3794 struct drm_i915_private *dev_priv = dev->dev_private;
3795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3796 int pipe = intel_crtc->pipe;
3799 /* disable CPU FDI tx and PCH FDI rx */
3800 reg = FDI_TX_CTL(pipe);
3801 temp = I915_READ(reg);
3802 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3805 reg = FDI_RX_CTL(pipe);
3806 temp = I915_READ(reg);
3807 temp &= ~(0x7 << 16);
3808 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3809 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3814 /* Ironlake workaround, disable clock pointer after downing FDI */
3815 if (HAS_PCH_IBX(dev))
3816 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3818 /* still set train pattern 1 */
3819 reg = FDI_TX_CTL(pipe);
3820 temp = I915_READ(reg);
3821 temp &= ~FDI_LINK_TRAIN_NONE;
3822 temp |= FDI_LINK_TRAIN_PATTERN_1;
3823 I915_WRITE(reg, temp);
3825 reg = FDI_RX_CTL(pipe);
3826 temp = I915_READ(reg);
3827 if (HAS_PCH_CPT(dev)) {
3828 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3829 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3831 temp &= ~FDI_LINK_TRAIN_NONE;
3832 temp |= FDI_LINK_TRAIN_PATTERN_1;
3834 /* BPC in FDI rx is consistent with that in PIPECONF */
3835 temp &= ~(0x07 << 16);
3836 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3837 I915_WRITE(reg, temp);
3843 bool intel_has_pending_fb_unpin(struct drm_device *dev)
3845 struct intel_crtc *crtc;
3847 /* Note that we don't need to be called with mode_config.lock here
3848 * as our list of CRTC objects is static for the lifetime of the
3849 * device and so cannot disappear as we iterate. Similarly, we can
3850 * happily treat the predicates as racy, atomic checks as userspace
3851 * cannot claim and pin a new fb without at least acquring the
3852 * struct_mutex and so serialising with us.
3854 for_each_intel_crtc(dev, crtc) {
3855 if (atomic_read(&crtc->unpin_work_count) == 0)
3858 if (crtc->unpin_work)
3859 intel_wait_for_vblank(dev, crtc->pipe);
3867 static void page_flip_completed(struct intel_crtc *intel_crtc)
3869 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3870 struct intel_unpin_work *work = intel_crtc->unpin_work;
3872 /* ensure that the unpin work is consistent wrt ->pending. */
3874 intel_crtc->unpin_work = NULL;
3877 drm_send_vblank_event(intel_crtc->base.dev,
3881 drm_crtc_vblank_put(&intel_crtc->base);
3883 wake_up_all(&dev_priv->pending_flip_queue);
3884 queue_work(dev_priv->wq, &work->work);
3886 trace_i915_flip_complete(intel_crtc->plane,
3887 work->pending_flip_obj);
3890 void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3892 struct drm_device *dev = crtc->dev;
3893 struct drm_i915_private *dev_priv = dev->dev_private;
3895 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3896 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3897 !intel_crtc_has_pending_flip(crtc),
3899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3901 spin_lock_irq(&dev->event_lock);
3902 if (intel_crtc->unpin_work) {
3903 WARN_ONCE(1, "Removing stuck page flip\n");
3904 page_flip_completed(intel_crtc);
3906 spin_unlock_irq(&dev->event_lock);
3909 if (crtc->primary->fb) {
3910 mutex_lock(&dev->struct_mutex);
3911 intel_finish_fb(crtc->primary->fb);
3912 mutex_unlock(&dev->struct_mutex);
3916 /* Program iCLKIP clock to the desired frequency */
3917 static void lpt_program_iclkip(struct drm_crtc *crtc)
3919 struct drm_device *dev = crtc->dev;
3920 struct drm_i915_private *dev_priv = dev->dev_private;
3921 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
3922 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3925 mutex_lock(&dev_priv->sb_lock);
3927 /* It is necessary to ungate the pixclk gate prior to programming
3928 * the divisors, and gate it back when it is done.
3930 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3932 /* Disable SSCCTL */
3933 intel_sbi_write(dev_priv, SBI_SSCCTL6,
3934 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3938 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
3939 if (clock == 20000) {
3944 /* The iCLK virtual clock root frequency is in MHz,
3945 * but the adjusted_mode->crtc_clock in in KHz. To get the
3946 * divisors, it is necessary to divide one by another, so we
3947 * convert the virtual clock precision to KHz here for higher
3950 u32 iclk_virtual_root_freq = 172800 * 1000;
3951 u32 iclk_pi_range = 64;
3952 u32 desired_divisor, msb_divisor_value, pi_value;
3954 desired_divisor = (iclk_virtual_root_freq / clock);
3955 msb_divisor_value = desired_divisor / iclk_pi_range;
3956 pi_value = desired_divisor % iclk_pi_range;
3959 divsel = msb_divisor_value - 2;
3960 phaseinc = pi_value;
3963 /* This should not happen with any sane values */
3964 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3965 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3966 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3967 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3969 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
3976 /* Program SSCDIVINTPHASE6 */
3977 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3978 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3979 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3980 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3981 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3982 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3983 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
3984 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
3986 /* Program SSCAUXDIV */
3987 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3988 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3989 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
3990 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
3992 /* Enable modulator and associated divider */
3993 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3994 temp &= ~SBI_SSCCTL_DISABLE;
3995 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3997 /* Wait for initialization time */
4000 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
4002 mutex_unlock(&dev_priv->sb_lock);
4005 static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4006 enum pipe pch_transcoder)
4008 struct drm_device *dev = crtc->base.dev;
4009 struct drm_i915_private *dev_priv = dev->dev_private;
4010 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
4012 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4013 I915_READ(HTOTAL(cpu_transcoder)));
4014 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4015 I915_READ(HBLANK(cpu_transcoder)));
4016 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4017 I915_READ(HSYNC(cpu_transcoder)));
4019 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4020 I915_READ(VTOTAL(cpu_transcoder)));
4021 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4022 I915_READ(VBLANK(cpu_transcoder)));
4023 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4024 I915_READ(VSYNC(cpu_transcoder)));
4025 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4026 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4029 static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
4031 struct drm_i915_private *dev_priv = dev->dev_private;
4034 temp = I915_READ(SOUTH_CHICKEN1);
4035 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
4038 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4039 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4041 temp &= ~FDI_BC_BIFURCATION_SELECT;
4043 temp |= FDI_BC_BIFURCATION_SELECT;
4045 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
4046 I915_WRITE(SOUTH_CHICKEN1, temp);
4047 POSTING_READ(SOUTH_CHICKEN1);
4050 static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4052 struct drm_device *dev = intel_crtc->base.dev;
4054 switch (intel_crtc->pipe) {
4058 if (intel_crtc->config->fdi_lanes > 2)
4059 cpt_set_fdi_bc_bifurcation(dev, false);
4061 cpt_set_fdi_bc_bifurcation(dev, true);
4065 cpt_set_fdi_bc_bifurcation(dev, true);
4074 * Enable PCH resources required for PCH ports:
4076 * - FDI training & RX/TX
4077 * - update transcoder timings
4078 * - DP transcoding bits
4081 static void ironlake_pch_enable(struct drm_crtc *crtc)
4083 struct drm_device *dev = crtc->dev;
4084 struct drm_i915_private *dev_priv = dev->dev_private;
4085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4086 int pipe = intel_crtc->pipe;
4089 assert_pch_transcoder_disabled(dev_priv, pipe);
4091 if (IS_IVYBRIDGE(dev))
4092 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4094 /* Write the TU size bits before fdi link training, so that error
4095 * detection works. */
4096 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4097 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4099 /* For PCH output, training FDI link */
4100 dev_priv->display.fdi_link_train(crtc);
4102 /* We need to program the right clock selection before writing the pixel
4103 * mutliplier into the DPLL. */
4104 if (HAS_PCH_CPT(dev)) {
4107 temp = I915_READ(PCH_DPLL_SEL);
4108 temp |= TRANS_DPLL_ENABLE(pipe);
4109 sel = TRANS_DPLLB_SEL(pipe);
4110 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
4114 I915_WRITE(PCH_DPLL_SEL, temp);
4117 /* XXX: pch pll's can be enabled any time before we enable the PCH
4118 * transcoder, and we actually should do this to not upset any PCH
4119 * transcoder that already use the clock when we share it.
4121 * Note that enable_shared_dpll tries to do the right thing, but
4122 * get_shared_dpll unconditionally resets the pll - we need that to have
4123 * the right LVDS enable sequence. */
4124 intel_enable_shared_dpll(intel_crtc);
4126 /* set transcoder timing, panel must allow it */
4127 assert_panel_unlocked(dev_priv, pipe);
4128 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
4130 intel_fdi_normal_train(crtc);
4132 /* For PCH DP, enable TRANS_DP_CTL */
4133 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
4134 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
4135 reg = TRANS_DP_CTL(pipe);
4136 temp = I915_READ(reg);
4137 temp &= ~(TRANS_DP_PORT_SEL_MASK |
4138 TRANS_DP_SYNC_MASK |
4140 temp |= TRANS_DP_OUTPUT_ENABLE;
4141 temp |= bpc << 9; /* same format but at 11:9 */
4143 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
4144 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
4145 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
4146 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
4148 switch (intel_trans_dp_port_sel(crtc)) {
4150 temp |= TRANS_DP_PORT_SEL_B;
4153 temp |= TRANS_DP_PORT_SEL_C;
4156 temp |= TRANS_DP_PORT_SEL_D;
4162 I915_WRITE(reg, temp);
4165 ironlake_enable_pch_transcoder(dev_priv, pipe);
4168 static void lpt_pch_enable(struct drm_crtc *crtc)
4170 struct drm_device *dev = crtc->dev;
4171 struct drm_i915_private *dev_priv = dev->dev_private;
4172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4173 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
4175 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
4177 lpt_program_iclkip(crtc);
4179 /* Set transcoder timing. */
4180 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
4182 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
4185 struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4186 struct intel_crtc_state *crtc_state)
4188 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
4189 struct intel_shared_dpll *pll;
4190 enum intel_dpll_id i;
4192 if (HAS_PCH_IBX(dev_priv->dev)) {
4193 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
4194 i = (enum intel_dpll_id) crtc->pipe;
4195 pll = &dev_priv->shared_dplls[i];
4197 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4198 crtc->base.base.id, pll->name);
4200 WARN_ON(pll->new_config->crtc_mask);
4205 if (IS_BROXTON(dev_priv->dev)) {
4206 /* PLL is attached to port in bxt */
4207 struct intel_encoder *encoder;
4208 struct intel_digital_port *intel_dig_port;
4210 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4211 if (WARN_ON(!encoder))
4214 intel_dig_port = enc_to_dig_port(&encoder->base);
4215 /* 1:1 mapping between ports and PLLs */
4216 i = (enum intel_dpll_id)intel_dig_port->port;
4217 pll = &dev_priv->shared_dplls[i];
4218 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4219 crtc->base.base.id, pll->name);
4220 WARN_ON(pll->new_config->crtc_mask);
4225 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4226 pll = &dev_priv->shared_dplls[i];
4228 /* Only want to check enabled timings first */
4229 if (pll->new_config->crtc_mask == 0)
4232 if (memcmp(&crtc_state->dpll_hw_state,
4233 &pll->new_config->hw_state,
4234 sizeof(pll->new_config->hw_state)) == 0) {
4235 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
4236 crtc->base.base.id, pll->name,
4237 pll->new_config->crtc_mask,
4243 /* Ok no matching timings, maybe there's a free one? */
4244 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4245 pll = &dev_priv->shared_dplls[i];
4246 if (pll->new_config->crtc_mask == 0) {
4247 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4248 crtc->base.base.id, pll->name);
4256 if (pll->new_config->crtc_mask == 0)
4257 pll->new_config->hw_state = crtc_state->dpll_hw_state;
4259 crtc_state->shared_dpll = i;
4260 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4261 pipe_name(crtc->pipe));
4263 pll->new_config->crtc_mask |= 1 << crtc->pipe;
4269 * intel_shared_dpll_start_config - start a new PLL staged config
4270 * @dev_priv: DRM device
4271 * @clear_pipes: mask of pipes that will have their PLLs freed
4273 * Starts a new PLL staged config, copying the current config but
4274 * releasing the references of pipes specified in clear_pipes.
4276 static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4277 unsigned clear_pipes)
4279 struct intel_shared_dpll *pll;
4280 enum intel_dpll_id i;
4282 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4283 pll = &dev_priv->shared_dplls[i];
4285 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4287 if (!pll->new_config)
4290 pll->new_config->crtc_mask &= ~clear_pipes;
4297 pll = &dev_priv->shared_dplls[i];
4298 kfree(pll->new_config);
4299 pll->new_config = NULL;
4305 static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4307 struct intel_shared_dpll *pll;
4308 enum intel_dpll_id i;
4310 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4311 pll = &dev_priv->shared_dplls[i];
4313 WARN_ON(pll->new_config == &pll->config);
4315 pll->config = *pll->new_config;
4316 kfree(pll->new_config);
4317 pll->new_config = NULL;
4321 static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4323 struct intel_shared_dpll *pll;
4324 enum intel_dpll_id i;
4326 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4327 pll = &dev_priv->shared_dplls[i];
4329 WARN_ON(pll->new_config == &pll->config);
4331 kfree(pll->new_config);
4332 pll->new_config = NULL;
4336 static void cpt_verify_modeset(struct drm_device *dev, int pipe)
4338 struct drm_i915_private *dev_priv = dev->dev_private;
4339 int dslreg = PIPEDSL(pipe);
4342 temp = I915_READ(dslreg);
4344 if (wait_for(I915_READ(dslreg) != temp, 5)) {
4345 if (wait_for(I915_READ(dslreg) != temp, 5))
4346 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
4351 * skl_update_scaler_users - Stages update to crtc's scaler state
4353 * @crtc_state: crtc_state
4354 * @plane: plane (NULL indicates crtc is requesting update)
4355 * @plane_state: plane's state
4356 * @force_detach: request unconditional detachment of scaler
4358 * This function updates scaler state for requested plane or crtc.
4359 * To request scaler usage update for a plane, caller shall pass plane pointer.
4360 * To request scaler usage update for crtc, caller shall pass plane pointer
4364 * 0 - scaler_usage updated successfully
4365 * error - requested scaling cannot be supported or other error condition
4368 skl_update_scaler_users(
4369 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4370 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4375 int src_w, src_h, dst_w, dst_h;
4377 struct drm_framebuffer *fb;
4378 struct intel_crtc_scaler_state *scaler_state;
4379 unsigned int rotation;
4381 if (!intel_crtc || !crtc_state)
4384 scaler_state = &crtc_state->scaler_state;
4386 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4387 fb = intel_plane ? plane_state->base.fb : NULL;
4390 src_w = drm_rect_width(&plane_state->src) >> 16;
4391 src_h = drm_rect_height(&plane_state->src) >> 16;
4392 dst_w = drm_rect_width(&plane_state->dst);
4393 dst_h = drm_rect_height(&plane_state->dst);
4394 scaler_id = &plane_state->scaler_id;
4395 rotation = plane_state->base.rotation;
4397 struct drm_display_mode *adjusted_mode =
4398 &crtc_state->base.adjusted_mode;
4399 src_w = crtc_state->pipe_src_w;
4400 src_h = crtc_state->pipe_src_h;
4401 dst_w = adjusted_mode->hdisplay;
4402 dst_h = adjusted_mode->vdisplay;
4403 scaler_id = &scaler_state->scaler_id;
4404 rotation = DRM_ROTATE_0;
4407 need_scaling = intel_rotation_90_or_270(rotation) ?
4408 (src_h != dst_w || src_w != dst_h):
4409 (src_w != dst_w || src_h != dst_h);
4412 * if plane is being disabled or scaler is no more required or force detach
4413 * - free scaler binded to this plane/crtc
4414 * - in order to do this, update crtc->scaler_usage
4416 * Here scaler state in crtc_state is set free so that
4417 * scaler can be assigned to other user. Actual register
4418 * update to free the scaler is done in plane/panel-fit programming.
4419 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4421 if (force_detach || !need_scaling || (intel_plane &&
4422 (!fb || !plane_state->visible))) {
4423 if (*scaler_id >= 0) {
4424 scaler_state->scaler_users &= ~(1 << idx);
4425 scaler_state->scalers[*scaler_id].in_use = 0;
4427 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4428 "crtc_state = %p scaler_users = 0x%x\n",
4429 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4430 intel_plane ? intel_plane->base.base.id :
4431 intel_crtc->base.base.id, crtc_state,
4432 scaler_state->scaler_users);
4439 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4440 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4442 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4443 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4444 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4445 "size is out of scaler range\n",
4446 intel_plane ? "PLANE" : "CRTC",
4447 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4448 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4452 /* check colorkey */
4453 if (WARN_ON(intel_plane &&
4454 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4455 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4456 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
4460 /* Check src format */
4462 switch (fb->pixel_format) {
4463 case DRM_FORMAT_RGB565:
4464 case DRM_FORMAT_XBGR8888:
4465 case DRM_FORMAT_XRGB8888:
4466 case DRM_FORMAT_ABGR8888:
4467 case DRM_FORMAT_ARGB8888:
4468 case DRM_FORMAT_XRGB2101010:
4469 case DRM_FORMAT_XBGR2101010:
4470 case DRM_FORMAT_YUYV:
4471 case DRM_FORMAT_YVYU:
4472 case DRM_FORMAT_UYVY:
4473 case DRM_FORMAT_VYUY:
4476 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4477 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4482 /* mark this plane as a scaler user in crtc_state */
4483 scaler_state->scaler_users |= (1 << idx);
4484 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4485 "crtc_state = %p scaler_users = 0x%x\n",
4486 intel_plane ? "PLANE" : "CRTC",
4487 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4488 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4492 static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
4494 struct drm_device *dev = crtc->base.dev;
4495 struct drm_i915_private *dev_priv = dev->dev_private;
4496 int pipe = crtc->pipe;
4497 struct intel_crtc_scaler_state *scaler_state =
4498 &crtc->config->scaler_state;
4500 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4502 /* To update pfit, first update scaler state */
4503 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4504 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4505 skl_detach_scalers(crtc);
4509 if (crtc->config->pch_pfit.enabled) {
4512 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4513 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4517 id = scaler_state->scaler_id;
4518 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4519 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4520 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4521 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4523 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
4527 static void ironlake_pfit_enable(struct intel_crtc *crtc)
4529 struct drm_device *dev = crtc->base.dev;
4530 struct drm_i915_private *dev_priv = dev->dev_private;
4531 int pipe = crtc->pipe;
4533 if (crtc->config->pch_pfit.enabled) {
4534 /* Force use of hard-coded filter coefficients
4535 * as some pre-programmed values are broken,
4538 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4539 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4540 PF_PIPE_SEL_IVB(pipe));
4542 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
4543 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4544 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
4548 static void intel_enable_sprite_planes(struct drm_crtc *crtc)
4550 struct drm_device *dev = crtc->dev;
4551 enum pipe pipe = to_intel_crtc(crtc)->pipe;
4552 struct drm_plane *plane;
4553 struct intel_plane *intel_plane;
4555 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4556 intel_plane = to_intel_plane(plane);
4557 if (intel_plane->pipe == pipe)
4558 intel_plane_restore(&intel_plane->base);
4562 void hsw_enable_ips(struct intel_crtc *crtc)
4564 struct drm_device *dev = crtc->base.dev;
4565 struct drm_i915_private *dev_priv = dev->dev_private;
4567 if (!crtc->config->ips_enabled)
4570 /* We can only enable IPS after we enable a plane and wait for a vblank */
4571 intel_wait_for_vblank(dev, crtc->pipe);
4573 assert_plane_enabled(dev_priv, crtc->plane);
4574 if (IS_BROADWELL(dev)) {
4575 mutex_lock(&dev_priv->rps.hw_lock);
4576 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4577 mutex_unlock(&dev_priv->rps.hw_lock);
4578 /* Quoting Art Runyan: "its not safe to expect any particular
4579 * value in IPS_CTL bit 31 after enabling IPS through the
4580 * mailbox." Moreover, the mailbox may return a bogus state,
4581 * so we need to just enable it and continue on.
4584 I915_WRITE(IPS_CTL, IPS_ENABLE);
4585 /* The bit only becomes 1 in the next vblank, so this wait here
4586 * is essentially intel_wait_for_vblank. If we don't have this
4587 * and don't wait for vblanks until the end of crtc_enable, then
4588 * the HW state readout code will complain that the expected
4589 * IPS_CTL value is not the one we read. */
4590 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4591 DRM_ERROR("Timed out waiting for IPS enable\n");
4595 void hsw_disable_ips(struct intel_crtc *crtc)
4597 struct drm_device *dev = crtc->base.dev;
4598 struct drm_i915_private *dev_priv = dev->dev_private;
4600 if (!crtc->config->ips_enabled)
4603 assert_plane_enabled(dev_priv, crtc->plane);
4604 if (IS_BROADWELL(dev)) {
4605 mutex_lock(&dev_priv->rps.hw_lock);
4606 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4607 mutex_unlock(&dev_priv->rps.hw_lock);
4608 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4609 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4610 DRM_ERROR("Timed out waiting for IPS disable\n");
4612 I915_WRITE(IPS_CTL, 0);
4613 POSTING_READ(IPS_CTL);
4616 /* We need to wait for a vblank before we can disable the plane. */
4617 intel_wait_for_vblank(dev, crtc->pipe);
4620 /** Loads the palette/gamma unit for the CRTC with the prepared values */
4621 static void intel_crtc_load_lut(struct drm_crtc *crtc)
4623 struct drm_device *dev = crtc->dev;
4624 struct drm_i915_private *dev_priv = dev->dev_private;
4625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4626 enum pipe pipe = intel_crtc->pipe;
4627 int palreg = PALETTE(pipe);
4629 bool reenable_ips = false;
4631 /* The clocks have to be on to load the palette. */
4632 if (!crtc->state->active)
4635 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
4636 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
4637 assert_dsi_pll_enabled(dev_priv);
4639 assert_pll_enabled(dev_priv, pipe);
4642 /* use legacy palette for Ironlake */
4643 if (!HAS_GMCH_DISPLAY(dev))
4644 palreg = LGC_PALETTE(pipe);
4646 /* Workaround : Do not read or write the pipe palette/gamma data while
4647 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4649 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
4650 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4651 GAMMA_MODE_MODE_SPLIT)) {
4652 hsw_disable_ips(intel_crtc);
4653 reenable_ips = true;
4656 for (i = 0; i < 256; i++) {
4657 I915_WRITE(palreg + 4 * i,
4658 (intel_crtc->lut_r[i] << 16) |
4659 (intel_crtc->lut_g[i] << 8) |
4660 intel_crtc->lut_b[i]);
4664 hsw_enable_ips(intel_crtc);
4667 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
4669 if (intel_crtc->overlay) {
4670 struct drm_device *dev = intel_crtc->base.dev;
4671 struct drm_i915_private *dev_priv = dev->dev_private;
4673 mutex_lock(&dev->struct_mutex);
4674 dev_priv->mm.interruptible = false;
4675 (void) intel_overlay_switch_off(intel_crtc->overlay);
4676 dev_priv->mm.interruptible = true;
4677 mutex_unlock(&dev->struct_mutex);
4680 /* Let userspace switch the overlay on again. In most cases userspace
4681 * has to recompute where to put it anyway.
4686 * intel_post_enable_primary - Perform operations after enabling primary plane
4687 * @crtc: the CRTC whose primary plane was just enabled
4689 * Performs potentially sleeping operations that must be done after the primary
4690 * plane is enabled, such as updating FBC and IPS. Note that this may be
4691 * called due to an explicit primary plane update, or due to an implicit
4692 * re-enable that is caused when a sprite plane is updated to no longer
4693 * completely hide the primary plane.
4696 intel_post_enable_primary(struct drm_crtc *crtc)
4698 struct drm_device *dev = crtc->dev;
4699 struct drm_i915_private *dev_priv = dev->dev_private;
4700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4701 int pipe = intel_crtc->pipe;
4704 * BDW signals flip done immediately if the plane
4705 * is disabled, even if the plane enable is already
4706 * armed to occur at the next vblank :(
4708 if (IS_BROADWELL(dev))
4709 intel_wait_for_vblank(dev, pipe);
4712 * FIXME IPS should be fine as long as one plane is
4713 * enabled, but in practice it seems to have problems
4714 * when going from primary only to sprite only and vice
4717 hsw_enable_ips(intel_crtc);
4719 mutex_lock(&dev->struct_mutex);
4720 intel_fbc_update(dev);
4721 mutex_unlock(&dev->struct_mutex);
4724 * Gen2 reports pipe underruns whenever all planes are disabled.
4725 * So don't enable underrun reporting before at least some planes
4727 * FIXME: Need to fix the logic to work when we turn off all planes
4728 * but leave the pipe running.
4731 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4733 /* Underruns don't raise interrupts, so check manually. */
4734 if (HAS_GMCH_DISPLAY(dev))
4735 i9xx_check_fifo_underruns(dev_priv);
4739 * intel_pre_disable_primary - Perform operations before disabling primary plane
4740 * @crtc: the CRTC whose primary plane is to be disabled
4742 * Performs potentially sleeping operations that must be done before the
4743 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4744 * be called due to an explicit primary plane update, or due to an implicit
4745 * disable that is caused when a sprite plane completely hides the primary
4749 intel_pre_disable_primary(struct drm_crtc *crtc)
4751 struct drm_device *dev = crtc->dev;
4752 struct drm_i915_private *dev_priv = dev->dev_private;
4753 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4754 int pipe = intel_crtc->pipe;
4757 * Gen2 reports pipe underruns whenever all planes are disabled.
4758 * So diasble underrun reporting before all the planes get disabled.
4759 * FIXME: Need to fix the logic to work when we turn off all planes
4760 * but leave the pipe running.
4763 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4766 * Vblank time updates from the shadow to live plane control register
4767 * are blocked if the memory self-refresh mode is active at that
4768 * moment. So to make sure the plane gets truly disabled, disable
4769 * first the self-refresh mode. The self-refresh enable bit in turn
4770 * will be checked/applied by the HW only at the next frame start
4771 * event which is after the vblank start event, so we need to have a
4772 * wait-for-vblank between disabling the plane and the pipe.
4774 if (HAS_GMCH_DISPLAY(dev))
4775 intel_set_memory_cxsr(dev_priv, false);
4777 mutex_lock(&dev->struct_mutex);
4778 if (dev_priv->fbc.crtc == intel_crtc)
4779 intel_fbc_disable(dev);
4780 mutex_unlock(&dev->struct_mutex);
4783 * FIXME IPS should be fine as long as one plane is
4784 * enabled, but in practice it seems to have problems
4785 * when going from primary only to sprite only and vice
4788 hsw_disable_ips(intel_crtc);
4791 static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4793 struct drm_device *dev = crtc->dev;
4794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4795 int pipe = intel_crtc->pipe;
4797 intel_enable_primary_hw_plane(crtc->primary, crtc);
4798 intel_enable_sprite_planes(crtc);
4799 intel_crtc_update_cursor(crtc, true);
4801 intel_post_enable_primary(crtc);
4804 * FIXME: Once we grow proper nuclear flip support out of this we need
4805 * to compute the mask of flip planes precisely. For the time being
4806 * consider this a flip to a NULL plane.
4808 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4811 static void intel_crtc_disable_planes(struct drm_crtc *crtc)
4813 struct drm_device *dev = crtc->dev;
4814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4815 struct intel_plane *intel_plane;
4816 int pipe = intel_crtc->pipe;
4818 intel_crtc_wait_for_pending_flips(crtc);
4820 intel_pre_disable_primary(crtc);
4822 intel_crtc_dpms_overlay_disable(intel_crtc);
4823 for_each_intel_plane(dev, intel_plane) {
4824 if (intel_plane->pipe == pipe) {
4825 struct drm_crtc *from = intel_plane->base.crtc;
4827 intel_plane->disable_plane(&intel_plane->base,
4828 from ?: crtc, true);
4833 * FIXME: Once we grow proper nuclear flip support out of this we need
4834 * to compute the mask of flip planes precisely. For the time being
4835 * consider this a flip to a NULL plane.
4837 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4840 static void ironlake_crtc_enable(struct drm_crtc *crtc)
4842 struct drm_device *dev = crtc->dev;
4843 struct drm_i915_private *dev_priv = dev->dev_private;
4844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4845 struct intel_encoder *encoder;
4846 int pipe = intel_crtc->pipe;
4848 if (WARN_ON(intel_crtc->active))
4851 if (intel_crtc->config->has_pch_encoder)
4852 intel_prepare_shared_dpll(intel_crtc);
4854 if (intel_crtc->config->has_dp_encoder)
4855 intel_dp_set_m_n(intel_crtc, M1_N1);
4857 intel_set_pipe_timings(intel_crtc);
4859 if (intel_crtc->config->has_pch_encoder) {
4860 intel_cpu_transcoder_set_m_n(intel_crtc,
4861 &intel_crtc->config->fdi_m_n, NULL);
4864 ironlake_set_pipeconf(crtc);
4866 intel_crtc->active = true;
4868 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4869 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
4871 for_each_encoder_on_crtc(dev, crtc, encoder)
4872 if (encoder->pre_enable)
4873 encoder->pre_enable(encoder);
4875 if (intel_crtc->config->has_pch_encoder) {
4876 /* Note: FDI PLL enabling _must_ be done before we enable the
4877 * cpu pipes, hence this is separate from all the other fdi/pch
4879 ironlake_fdi_pll_enable(intel_crtc);
4881 assert_fdi_tx_disabled(dev_priv, pipe);
4882 assert_fdi_rx_disabled(dev_priv, pipe);
4885 ironlake_pfit_enable(intel_crtc);
4888 * On ILK+ LUT must be loaded before the pipe is running but with
4891 intel_crtc_load_lut(crtc);
4893 intel_update_watermarks(crtc);
4894 intel_enable_pipe(intel_crtc);
4896 if (intel_crtc->config->has_pch_encoder)
4897 ironlake_pch_enable(crtc);
4899 assert_vblank_disabled(crtc);
4900 drm_crtc_vblank_on(crtc);
4902 for_each_encoder_on_crtc(dev, crtc, encoder)
4903 encoder->enable(encoder);
4905 if (HAS_PCH_CPT(dev))
4906 cpt_verify_modeset(dev, intel_crtc->pipe);
4909 /* IPS only exists on ULT machines and is tied to pipe A. */
4910 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4912 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
4916 * This implements the workaround described in the "notes" section of the mode
4917 * set sequence documentation. When going from no pipes or single pipe to
4918 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4919 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4921 static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4923 struct drm_device *dev = crtc->base.dev;
4924 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4926 /* We want to get the other_active_crtc only if there's only 1 other
4928 for_each_intel_crtc(dev, crtc_it) {
4929 if (!crtc_it->active || crtc_it == crtc)
4932 if (other_active_crtc)
4935 other_active_crtc = crtc_it;
4937 if (!other_active_crtc)
4940 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4941 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4944 static void haswell_crtc_enable(struct drm_crtc *crtc)
4946 struct drm_device *dev = crtc->dev;
4947 struct drm_i915_private *dev_priv = dev->dev_private;
4948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4949 struct intel_encoder *encoder;
4950 int pipe = intel_crtc->pipe;
4952 if (WARN_ON(intel_crtc->active))
4955 if (intel_crtc_to_shared_dpll(intel_crtc))
4956 intel_enable_shared_dpll(intel_crtc);
4958 if (intel_crtc->config->has_dp_encoder)
4959 intel_dp_set_m_n(intel_crtc, M1_N1);
4961 intel_set_pipe_timings(intel_crtc);
4963 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4964 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4965 intel_crtc->config->pixel_multiplier - 1);
4968 if (intel_crtc->config->has_pch_encoder) {
4969 intel_cpu_transcoder_set_m_n(intel_crtc,
4970 &intel_crtc->config->fdi_m_n, NULL);
4973 haswell_set_pipeconf(crtc);
4975 intel_set_pipe_csc(crtc);
4977 intel_crtc->active = true;
4979 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4980 for_each_encoder_on_crtc(dev, crtc, encoder)
4981 if (encoder->pre_enable)
4982 encoder->pre_enable(encoder);
4984 if (intel_crtc->config->has_pch_encoder) {
4985 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4987 dev_priv->display.fdi_link_train(crtc);
4990 intel_ddi_enable_pipe_clock(intel_crtc);
4992 if (INTEL_INFO(dev)->gen == 9)
4993 skylake_pfit_update(intel_crtc, 1);
4994 else if (INTEL_INFO(dev)->gen < 9)
4995 ironlake_pfit_enable(intel_crtc);
4997 MISSING_CASE(INTEL_INFO(dev)->gen);
5000 * On ILK+ LUT must be loaded before the pipe is running but with
5003 intel_crtc_load_lut(crtc);
5005 intel_ddi_set_pipe_settings(crtc);
5006 intel_ddi_enable_transcoder_func(crtc);
5008 intel_update_watermarks(crtc);
5009 intel_enable_pipe(intel_crtc);
5011 if (intel_crtc->config->has_pch_encoder)
5012 lpt_pch_enable(crtc);
5014 if (intel_crtc->config->dp_encoder_is_mst)
5015 intel_ddi_set_vc_payload_alloc(crtc, true);
5017 assert_vblank_disabled(crtc);
5018 drm_crtc_vblank_on(crtc);
5020 for_each_encoder_on_crtc(dev, crtc, encoder) {
5021 encoder->enable(encoder);
5022 intel_opregion_notify_encoder(encoder, true);
5025 /* If we change the relative order between pipe/planes enabling, we need
5026 * to change the workaround. */
5027 haswell_mode_set_planes_workaround(intel_crtc);
5030 static void ironlake_pfit_disable(struct intel_crtc *crtc)
5032 struct drm_device *dev = crtc->base.dev;
5033 struct drm_i915_private *dev_priv = dev->dev_private;
5034 int pipe = crtc->pipe;
5036 /* To avoid upsetting the power well on haswell only disable the pfit if
5037 * it's in use. The hw state code will make sure we get this right. */
5038 if (crtc->config->pch_pfit.enabled) {
5039 I915_WRITE(PF_CTL(pipe), 0);
5040 I915_WRITE(PF_WIN_POS(pipe), 0);
5041 I915_WRITE(PF_WIN_SZ(pipe), 0);
5045 static void ironlake_crtc_disable(struct drm_crtc *crtc)
5047 struct drm_device *dev = crtc->dev;
5048 struct drm_i915_private *dev_priv = dev->dev_private;
5049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5050 struct intel_encoder *encoder;
5051 int pipe = intel_crtc->pipe;
5054 if (WARN_ON(!intel_crtc->active))
5057 for_each_encoder_on_crtc(dev, crtc, encoder)
5058 encoder->disable(encoder);
5060 drm_crtc_vblank_off(crtc);
5061 assert_vblank_disabled(crtc);
5063 if (intel_crtc->config->has_pch_encoder)
5064 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
5066 intel_disable_pipe(intel_crtc);
5068 ironlake_pfit_disable(intel_crtc);
5070 if (intel_crtc->config->has_pch_encoder)
5071 ironlake_fdi_disable(crtc);
5073 for_each_encoder_on_crtc(dev, crtc, encoder)
5074 if (encoder->post_disable)
5075 encoder->post_disable(encoder);
5077 if (intel_crtc->config->has_pch_encoder) {
5078 ironlake_disable_pch_transcoder(dev_priv, pipe);
5080 if (HAS_PCH_CPT(dev)) {
5081 /* disable TRANS_DP_CTL */
5082 reg = TRANS_DP_CTL(pipe);
5083 temp = I915_READ(reg);
5084 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5085 TRANS_DP_PORT_SEL_MASK);
5086 temp |= TRANS_DP_PORT_SEL_NONE;
5087 I915_WRITE(reg, temp);
5089 /* disable DPLL_SEL */
5090 temp = I915_READ(PCH_DPLL_SEL);
5091 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
5092 I915_WRITE(PCH_DPLL_SEL, temp);
5095 /* disable PCH DPLL */
5096 intel_disable_shared_dpll(intel_crtc);
5098 ironlake_fdi_pll_disable(intel_crtc);
5101 intel_crtc->active = false;
5102 intel_update_watermarks(crtc);
5104 mutex_lock(&dev->struct_mutex);
5105 intel_fbc_update(dev);
5106 mutex_unlock(&dev->struct_mutex);
5109 static void haswell_crtc_disable(struct drm_crtc *crtc)
5111 struct drm_device *dev = crtc->dev;
5112 struct drm_i915_private *dev_priv = dev->dev_private;
5113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5114 struct intel_encoder *encoder;
5115 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
5117 if (WARN_ON(!intel_crtc->active))
5120 for_each_encoder_on_crtc(dev, crtc, encoder) {
5121 intel_opregion_notify_encoder(encoder, false);
5122 encoder->disable(encoder);
5125 drm_crtc_vblank_off(crtc);
5126 assert_vblank_disabled(crtc);
5128 if (intel_crtc->config->has_pch_encoder)
5129 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5131 intel_disable_pipe(intel_crtc);
5133 if (intel_crtc->config->dp_encoder_is_mst)
5134 intel_ddi_set_vc_payload_alloc(crtc, false);
5136 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
5138 if (INTEL_INFO(dev)->gen == 9)
5139 skylake_pfit_update(intel_crtc, 0);
5140 else if (INTEL_INFO(dev)->gen < 9)
5141 ironlake_pfit_disable(intel_crtc);
5143 MISSING_CASE(INTEL_INFO(dev)->gen);
5145 intel_ddi_disable_pipe_clock(intel_crtc);
5147 if (intel_crtc->config->has_pch_encoder) {
5148 lpt_disable_pch_transcoder(dev_priv);
5149 intel_ddi_fdi_disable(crtc);
5152 for_each_encoder_on_crtc(dev, crtc, encoder)
5153 if (encoder->post_disable)
5154 encoder->post_disable(encoder);
5156 intel_crtc->active = false;
5157 intel_update_watermarks(crtc);
5159 mutex_lock(&dev->struct_mutex);
5160 intel_fbc_update(dev);
5161 mutex_unlock(&dev->struct_mutex);
5163 if (intel_crtc_to_shared_dpll(intel_crtc))
5164 intel_disable_shared_dpll(intel_crtc);
5167 static void i9xx_pfit_enable(struct intel_crtc *crtc)
5169 struct drm_device *dev = crtc->base.dev;
5170 struct drm_i915_private *dev_priv = dev->dev_private;
5171 struct intel_crtc_state *pipe_config = crtc->config;
5173 if (!pipe_config->gmch_pfit.control)
5177 * The panel fitter should only be adjusted whilst the pipe is disabled,
5178 * according to register description and PRM.
5180 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5181 assert_pipe_disabled(dev_priv, crtc->pipe);
5183 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5184 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5186 /* Border color in case we don't scale up to the full screen. Black by
5187 * default, change to something else for debugging. */
5188 I915_WRITE(BCLRPAT(crtc->pipe), 0);
5191 static enum intel_display_power_domain port_to_power_domain(enum port port)
5195 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5197 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5199 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5201 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5204 return POWER_DOMAIN_PORT_OTHER;
5208 #define for_each_power_domain(domain, mask) \
5209 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5210 if ((1 << (domain)) & (mask))
5212 enum intel_display_power_domain
5213 intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5215 struct drm_device *dev = intel_encoder->base.dev;
5216 struct intel_digital_port *intel_dig_port;
5218 switch (intel_encoder->type) {
5219 case INTEL_OUTPUT_UNKNOWN:
5220 /* Only DDI platforms should ever use this output type */
5221 WARN_ON_ONCE(!HAS_DDI(dev));
5222 case INTEL_OUTPUT_DISPLAYPORT:
5223 case INTEL_OUTPUT_HDMI:
5224 case INTEL_OUTPUT_EDP:
5225 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
5226 return port_to_power_domain(intel_dig_port->port);
5227 case INTEL_OUTPUT_DP_MST:
5228 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5229 return port_to_power_domain(intel_dig_port->port);
5230 case INTEL_OUTPUT_ANALOG:
5231 return POWER_DOMAIN_PORT_CRT;
5232 case INTEL_OUTPUT_DSI:
5233 return POWER_DOMAIN_PORT_DSI;
5235 return POWER_DOMAIN_PORT_OTHER;
5239 static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5241 struct drm_device *dev = crtc->dev;
5242 struct intel_encoder *intel_encoder;
5243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5244 enum pipe pipe = intel_crtc->pipe;
5246 enum transcoder transcoder;
5248 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5250 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5251 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
5252 if (intel_crtc->config->pch_pfit.enabled ||
5253 intel_crtc->config->pch_pfit.force_thru)
5254 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5256 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5257 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5262 static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
5264 struct drm_device *dev = state->dev;
5265 struct drm_i915_private *dev_priv = dev->dev_private;
5266 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5267 struct intel_crtc *crtc;
5270 * First get all needed power domains, then put all unneeded, to avoid
5271 * any unnecessary toggling of the power wells.
5273 for_each_intel_crtc(dev, crtc) {
5274 enum intel_display_power_domain domain;
5276 if (!crtc->base.state->enable)
5279 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
5281 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5282 intel_display_power_get(dev_priv, domain);
5285 if (dev_priv->display.modeset_global_resources)
5286 dev_priv->display.modeset_global_resources(state);
5288 for_each_intel_crtc(dev, crtc) {
5289 enum intel_display_power_domain domain;
5291 for_each_power_domain(domain, crtc->enabled_power_domains)
5292 intel_display_power_put(dev_priv, domain);
5294 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5297 intel_display_set_init_power(dev_priv, false);
5300 static void intel_update_max_cdclk(struct drm_device *dev)
5302 struct drm_i915_private *dev_priv = dev->dev_private;
5304 if (IS_SKYLAKE(dev)) {
5305 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5307 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5308 dev_priv->max_cdclk_freq = 675000;
5309 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5310 dev_priv->max_cdclk_freq = 540000;
5311 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5312 dev_priv->max_cdclk_freq = 450000;
5314 dev_priv->max_cdclk_freq = 337500;
5315 } else if (IS_BROADWELL(dev)) {
5317 * FIXME with extra cooling we can allow
5318 * 540 MHz for ULX and 675 Mhz for ULT.
5319 * How can we know if extra cooling is
5320 * available? PCI ID, VTB, something else?
5322 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5323 dev_priv->max_cdclk_freq = 450000;
5324 else if (IS_BDW_ULX(dev))
5325 dev_priv->max_cdclk_freq = 450000;
5326 else if (IS_BDW_ULT(dev))
5327 dev_priv->max_cdclk_freq = 540000;
5329 dev_priv->max_cdclk_freq = 675000;
5330 } else if (IS_VALLEYVIEW(dev)) {
5331 dev_priv->max_cdclk_freq = 400000;
5333 /* otherwise assume cdclk is fixed */
5334 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5337 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5338 dev_priv->max_cdclk_freq);
5341 static void intel_update_cdclk(struct drm_device *dev)
5343 struct drm_i915_private *dev_priv = dev->dev_private;
5345 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5346 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5347 dev_priv->cdclk_freq);
5350 * Program the gmbus_freq based on the cdclk frequency.
5351 * BSpec erroneously claims we should aim for 4MHz, but
5352 * in fact 1MHz is the correct frequency.
5354 if (IS_VALLEYVIEW(dev)) {
5356 * Program the gmbus_freq based on the cdclk frequency.
5357 * BSpec erroneously claims we should aim for 4MHz, but
5358 * in fact 1MHz is the correct frequency.
5360 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5363 if (dev_priv->max_cdclk_freq == 0)
5364 intel_update_max_cdclk(dev);
5367 static void broxton_set_cdclk(struct drm_device *dev, int frequency)
5369 struct drm_i915_private *dev_priv = dev->dev_private;
5372 uint32_t current_freq;
5375 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5376 switch (frequency) {
5378 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5379 ratio = BXT_DE_PLL_RATIO(60);
5382 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5383 ratio = BXT_DE_PLL_RATIO(60);
5386 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5387 ratio = BXT_DE_PLL_RATIO(60);
5390 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5391 ratio = BXT_DE_PLL_RATIO(60);
5394 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5395 ratio = BXT_DE_PLL_RATIO(65);
5399 * Bypass frequency with DE PLL disabled. Init ratio, divider
5400 * to suppress GCC warning.
5406 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5411 mutex_lock(&dev_priv->rps.hw_lock);
5412 /* Inform power controller of upcoming frequency change */
5413 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5415 mutex_unlock(&dev_priv->rps.hw_lock);
5418 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5423 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5424 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5425 current_freq = current_freq * 500 + 1000;
5428 * DE PLL has to be disabled when
5429 * - setting to 19.2MHz (bypass, PLL isn't used)
5430 * - before setting to 624MHz (PLL needs toggling)
5431 * - before setting to any frequency from 624MHz (PLL needs toggling)
5433 if (frequency == 19200 || frequency == 624000 ||
5434 current_freq == 624000) {
5435 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5437 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5439 DRM_ERROR("timout waiting for DE PLL unlock\n");
5442 if (frequency != 19200) {
5445 val = I915_READ(BXT_DE_PLL_CTL);
5446 val &= ~BXT_DE_PLL_RATIO_MASK;
5448 I915_WRITE(BXT_DE_PLL_CTL, val);
5450 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5452 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5453 DRM_ERROR("timeout waiting for DE PLL lock\n");
5455 val = I915_READ(CDCLK_CTL);
5456 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5459 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5462 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5463 if (frequency >= 500000)
5464 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5466 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5467 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5468 val |= (frequency - 1000) / 500;
5469 I915_WRITE(CDCLK_CTL, val);
5472 mutex_lock(&dev_priv->rps.hw_lock);
5473 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5474 DIV_ROUND_UP(frequency, 25000));
5475 mutex_unlock(&dev_priv->rps.hw_lock);
5478 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5483 intel_update_cdclk(dev);
5486 void broxton_init_cdclk(struct drm_device *dev)
5488 struct drm_i915_private *dev_priv = dev->dev_private;
5492 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5493 * or else the reset will hang because there is no PCH to respond.
5494 * Move the handshake programming to initialization sequence.
5495 * Previously was left up to BIOS.
5497 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5498 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5499 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5501 /* Enable PG1 for cdclk */
5502 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5504 /* check if cd clock is enabled */
5505 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5506 DRM_DEBUG_KMS("Display already initialized\n");
5512 * - The initial CDCLK needs to be read from VBT.
5513 * Need to make this change after VBT has changes for BXT.
5514 * - check if setting the max (or any) cdclk freq is really necessary
5515 * here, it belongs to modeset time
5517 broxton_set_cdclk(dev, 624000);
5519 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5520 POSTING_READ(DBUF_CTL);
5524 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5525 DRM_ERROR("DBuf power enable timeout!\n");
5528 void broxton_uninit_cdclk(struct drm_device *dev)
5530 struct drm_i915_private *dev_priv = dev->dev_private;
5532 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5533 POSTING_READ(DBUF_CTL);
5537 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5538 DRM_ERROR("DBuf power disable timeout!\n");
5540 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5541 broxton_set_cdclk(dev, 19200);
5543 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5546 static const struct skl_cdclk_entry {
5549 } skl_cdclk_frequencies[] = {
5550 { .freq = 308570, .vco = 8640 },
5551 { .freq = 337500, .vco = 8100 },
5552 { .freq = 432000, .vco = 8640 },
5553 { .freq = 450000, .vco = 8100 },
5554 { .freq = 540000, .vco = 8100 },
5555 { .freq = 617140, .vco = 8640 },
5556 { .freq = 675000, .vco = 8100 },
5559 static unsigned int skl_cdclk_decimal(unsigned int freq)
5561 return (freq - 1000) / 500;
5564 static unsigned int skl_cdclk_get_vco(unsigned int freq)
5568 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5569 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5571 if (e->freq == freq)
5579 skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5581 unsigned int min_freq;
5584 /* select the minimum CDCLK before enabling DPLL 0 */
5585 val = I915_READ(CDCLK_CTL);
5586 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5587 val |= CDCLK_FREQ_337_308;
5589 if (required_vco == 8640)
5594 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5596 I915_WRITE(CDCLK_CTL, val);
5597 POSTING_READ(CDCLK_CTL);
5600 * We always enable DPLL0 with the lowest link rate possible, but still
5601 * taking into account the VCO required to operate the eDP panel at the
5602 * desired frequency. The usual DP link rates operate with a VCO of
5603 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5604 * The modeset code is responsible for the selection of the exact link
5605 * rate later on, with the constraint of choosing a frequency that
5606 * works with required_vco.
5608 val = I915_READ(DPLL_CTRL1);
5610 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5611 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5612 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5613 if (required_vco == 8640)
5614 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5617 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5620 I915_WRITE(DPLL_CTRL1, val);
5621 POSTING_READ(DPLL_CTRL1);
5623 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5625 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5626 DRM_ERROR("DPLL0 not locked\n");
5629 static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5634 /* inform PCU we want to change CDCLK */
5635 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5636 mutex_lock(&dev_priv->rps.hw_lock);
5637 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5638 mutex_unlock(&dev_priv->rps.hw_lock);
5640 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5643 static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5647 for (i = 0; i < 15; i++) {
5648 if (skl_cdclk_pcu_ready(dev_priv))
5656 static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5658 struct drm_device *dev = dev_priv->dev;
5659 u32 freq_select, pcu_ack;
5661 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5663 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5664 DRM_ERROR("failed to inform PCU about cdclk change\n");
5672 freq_select = CDCLK_FREQ_450_432;
5676 freq_select = CDCLK_FREQ_540;
5682 freq_select = CDCLK_FREQ_337_308;
5687 freq_select = CDCLK_FREQ_675_617;
5692 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5693 POSTING_READ(CDCLK_CTL);
5695 /* inform PCU of the change */
5696 mutex_lock(&dev_priv->rps.hw_lock);
5697 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5698 mutex_unlock(&dev_priv->rps.hw_lock);
5700 intel_update_cdclk(dev);
5703 void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5705 /* disable DBUF power */
5706 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5707 POSTING_READ(DBUF_CTL);
5711 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5712 DRM_ERROR("DBuf power disable timeout\n");
5715 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5716 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5717 DRM_ERROR("Couldn't disable DPLL0\n");
5719 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5722 void skl_init_cdclk(struct drm_i915_private *dev_priv)
5725 unsigned int required_vco;
5727 /* enable PCH reset handshake */
5728 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5729 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5731 /* enable PG1 and Misc I/O */
5732 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5734 /* DPLL0 already enabed !? */
5735 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5736 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5741 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5742 skl_dpll0_enable(dev_priv, required_vco);
5744 /* set CDCLK to the frequency the BIOS chose */
5745 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5747 /* enable DBUF power */
5748 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5749 POSTING_READ(DBUF_CTL);
5753 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5754 DRM_ERROR("DBuf power enable timeout\n");
5757 /* returns HPLL frequency in kHz */
5758 static int valleyview_get_vco(struct drm_i915_private *dev_priv)
5760 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
5762 /* Obtain SKU information */
5763 mutex_lock(&dev_priv->sb_lock);
5764 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5765 CCK_FUSE_HPLL_FREQ_MASK;
5766 mutex_unlock(&dev_priv->sb_lock);
5768 return vco_freq[hpll_freq] * 1000;
5771 /* Adjust CDclk dividers to allow high res or save power if possible */
5772 static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5774 struct drm_i915_private *dev_priv = dev->dev_private;
5777 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5778 != dev_priv->cdclk_freq);
5780 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
5782 else if (cdclk == 266667)
5787 mutex_lock(&dev_priv->rps.hw_lock);
5788 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5789 val &= ~DSPFREQGUAR_MASK;
5790 val |= (cmd << DSPFREQGUAR_SHIFT);
5791 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5792 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5793 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5795 DRM_ERROR("timed out waiting for CDclk change\n");
5797 mutex_unlock(&dev_priv->rps.hw_lock);
5799 mutex_lock(&dev_priv->sb_lock);
5801 if (cdclk == 400000) {
5804 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5806 /* adjust cdclk divider */
5807 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5808 val &= ~DISPLAY_FREQUENCY_VALUES;
5810 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
5812 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5813 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5815 DRM_ERROR("timed out waiting for CDclk change\n");
5818 /* adjust self-refresh exit latency value */
5819 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5823 * For high bandwidth configs, we set a higher latency in the bunit
5824 * so that the core display fetch happens in time to avoid underruns.
5826 if (cdclk == 400000)
5827 val |= 4500 / 250; /* 4.5 usec */
5829 val |= 3000 / 250; /* 3.0 usec */
5830 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5832 mutex_unlock(&dev_priv->sb_lock);
5834 intel_update_cdclk(dev);
5837 static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5839 struct drm_i915_private *dev_priv = dev->dev_private;
5842 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5843 != dev_priv->cdclk_freq);
5852 MISSING_CASE(cdclk);
5857 * Specs are full of misinformation, but testing on actual
5858 * hardware has shown that we just need to write the desired
5859 * CCK divider into the Punit register.
5861 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5863 mutex_lock(&dev_priv->rps.hw_lock);
5864 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5865 val &= ~DSPFREQGUAR_MASK_CHV;
5866 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5867 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5868 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5869 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5871 DRM_ERROR("timed out waiting for CDclk change\n");
5873 mutex_unlock(&dev_priv->rps.hw_lock);
5875 intel_update_cdclk(dev);
5878 static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5881 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
5882 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
5885 * Really only a few cases to deal with, as only 4 CDclks are supported:
5888 * 320/333MHz (depends on HPLL freq)
5890 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5891 * of the lower bin and adjust if needed.
5893 * We seem to get an unstable or solid color picture at 200MHz.
5894 * Not sure what's wrong. For now use 200MHz only when all pipes
5897 if (!IS_CHERRYVIEW(dev_priv) &&
5898 max_pixclk > freq_320*limit/100)
5900 else if (max_pixclk > 266667*limit/100)
5902 else if (max_pixclk > 0)
5908 static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5913 * - remove the guardband, it's not needed on BXT
5914 * - set 19.2MHz bypass frequency if there are no active pipes
5916 if (max_pixclk > 576000*9/10)
5918 else if (max_pixclk > 384000*9/10)
5920 else if (max_pixclk > 288000*9/10)
5922 else if (max_pixclk > 144000*9/10)
5928 /* Compute the max pixel clock for new configuration. Uses atomic state if
5929 * that's non-NULL, look at current state otherwise. */
5930 static int intel_mode_max_pixclk(struct drm_device *dev,
5931 struct drm_atomic_state *state)
5933 struct intel_crtc *intel_crtc;
5934 struct intel_crtc_state *crtc_state;
5937 for_each_intel_crtc(dev, intel_crtc) {
5940 intel_atomic_get_crtc_state(state, intel_crtc);
5942 crtc_state = intel_crtc->config;
5943 if (IS_ERR(crtc_state))
5944 return PTR_ERR(crtc_state);
5946 if (!crtc_state->base.enable)
5949 max_pixclk = max(max_pixclk,
5950 crtc_state->base.adjusted_mode.crtc_clock);
5956 static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
5958 struct drm_i915_private *dev_priv = to_i915(state->dev);
5959 struct drm_crtc *crtc;
5960 struct drm_crtc_state *crtc_state;
5961 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
5967 if (IS_VALLEYVIEW(dev_priv))
5968 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5970 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5972 if (cdclk == dev_priv->cdclk_freq)
5975 /* add all active pipes to the state */
5976 for_each_crtc(state->dev, crtc) {
5977 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5978 if (IS_ERR(crtc_state))
5979 return PTR_ERR(crtc_state);
5981 if (!crtc_state->active || needs_modeset(crtc_state))
5984 crtc_state->mode_changed = true;
5986 ret = drm_atomic_add_affected_connectors(state, crtc);
5990 ret = drm_atomic_add_affected_planes(state, crtc);
5998 static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6000 unsigned int credits, default_credits;
6002 if (IS_CHERRYVIEW(dev_priv))
6003 default_credits = PFI_CREDIT(12);
6005 default_credits = PFI_CREDIT(8);
6007 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
6008 /* CHV suggested value is 31 or 63 */
6009 if (IS_CHERRYVIEW(dev_priv))
6010 credits = PFI_CREDIT_31;
6012 credits = PFI_CREDIT(15);
6014 credits = default_credits;
6018 * WA - write default credits before re-programming
6019 * FIXME: should we also set the resend bit here?
6021 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6024 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6025 credits | PFI_CREDIT_RESEND);
6028 * FIXME is this guaranteed to clear
6029 * immediately or should we poll for it?
6031 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6034 static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
6036 struct drm_device *dev = old_state->dev;
6037 struct drm_i915_private *dev_priv = dev->dev_private;
6038 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
6041 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
6043 if (WARN_ON(max_pixclk < 0))
6046 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
6048 if (req_cdclk != dev_priv->cdclk_freq) {
6050 * FIXME: We can end up here with all power domains off, yet
6051 * with a CDCLK frequency other than the minimum. To account
6052 * for this take the PIPE-A power domain, which covers the HW
6053 * blocks needed for the following programming. This can be
6054 * removed once it's guaranteed that we get here either with
6055 * the minimum CDCLK set, or the required power domains
6058 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6060 if (IS_CHERRYVIEW(dev))
6061 cherryview_set_cdclk(dev, req_cdclk);
6063 valleyview_set_cdclk(dev, req_cdclk);
6065 vlv_program_pfi_credits(dev_priv);
6067 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
6071 static void valleyview_crtc_enable(struct drm_crtc *crtc)
6073 struct drm_device *dev = crtc->dev;
6074 struct drm_i915_private *dev_priv = to_i915(dev);
6075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6076 struct intel_encoder *encoder;
6077 int pipe = intel_crtc->pipe;
6080 if (WARN_ON(intel_crtc->active))
6083 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
6086 if (IS_CHERRYVIEW(dev))
6087 chv_prepare_pll(intel_crtc, intel_crtc->config);
6089 vlv_prepare_pll(intel_crtc, intel_crtc->config);
6092 if (intel_crtc->config->has_dp_encoder)
6093 intel_dp_set_m_n(intel_crtc, M1_N1);
6095 intel_set_pipe_timings(intel_crtc);
6097 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6098 struct drm_i915_private *dev_priv = dev->dev_private;
6100 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6101 I915_WRITE(CHV_CANVAS(pipe), 0);
6104 i9xx_set_pipeconf(intel_crtc);
6106 intel_crtc->active = true;
6108 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6110 for_each_encoder_on_crtc(dev, crtc, encoder)
6111 if (encoder->pre_pll_enable)
6112 encoder->pre_pll_enable(encoder);
6115 if (IS_CHERRYVIEW(dev))
6116 chv_enable_pll(intel_crtc, intel_crtc->config);
6118 vlv_enable_pll(intel_crtc, intel_crtc->config);
6121 for_each_encoder_on_crtc(dev, crtc, encoder)
6122 if (encoder->pre_enable)
6123 encoder->pre_enable(encoder);
6125 i9xx_pfit_enable(intel_crtc);
6127 intel_crtc_load_lut(crtc);
6129 intel_update_watermarks(crtc);
6130 intel_enable_pipe(intel_crtc);
6132 assert_vblank_disabled(crtc);
6133 drm_crtc_vblank_on(crtc);
6135 for_each_encoder_on_crtc(dev, crtc, encoder)
6136 encoder->enable(encoder);
6139 static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6141 struct drm_device *dev = crtc->base.dev;
6142 struct drm_i915_private *dev_priv = dev->dev_private;
6144 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6145 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
6148 static void i9xx_crtc_enable(struct drm_crtc *crtc)
6150 struct drm_device *dev = crtc->dev;
6151 struct drm_i915_private *dev_priv = to_i915(dev);
6152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6153 struct intel_encoder *encoder;
6154 int pipe = intel_crtc->pipe;
6156 if (WARN_ON(intel_crtc->active))
6159 i9xx_set_pll_dividers(intel_crtc);
6161 if (intel_crtc->config->has_dp_encoder)
6162 intel_dp_set_m_n(intel_crtc, M1_N1);
6164 intel_set_pipe_timings(intel_crtc);
6166 i9xx_set_pipeconf(intel_crtc);
6168 intel_crtc->active = true;
6171 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
6173 for_each_encoder_on_crtc(dev, crtc, encoder)
6174 if (encoder->pre_enable)
6175 encoder->pre_enable(encoder);
6177 i9xx_enable_pll(intel_crtc);
6179 i9xx_pfit_enable(intel_crtc);
6181 intel_crtc_load_lut(crtc);
6183 intel_update_watermarks(crtc);
6184 intel_enable_pipe(intel_crtc);
6186 assert_vblank_disabled(crtc);
6187 drm_crtc_vblank_on(crtc);
6189 for_each_encoder_on_crtc(dev, crtc, encoder)
6190 encoder->enable(encoder);
6193 static void i9xx_pfit_disable(struct intel_crtc *crtc)
6195 struct drm_device *dev = crtc->base.dev;
6196 struct drm_i915_private *dev_priv = dev->dev_private;
6198 if (!crtc->config->gmch_pfit.control)
6201 assert_pipe_disabled(dev_priv, crtc->pipe);
6203 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6204 I915_READ(PFIT_CONTROL));
6205 I915_WRITE(PFIT_CONTROL, 0);
6208 static void i9xx_crtc_disable(struct drm_crtc *crtc)
6210 struct drm_device *dev = crtc->dev;
6211 struct drm_i915_private *dev_priv = dev->dev_private;
6212 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6213 struct intel_encoder *encoder;
6214 int pipe = intel_crtc->pipe;
6216 if (WARN_ON(!intel_crtc->active))
6220 * On gen2 planes are double buffered but the pipe isn't, so we must
6221 * wait for planes to fully turn off before disabling the pipe.
6222 * We also need to wait on all gmch platforms because of the
6223 * self-refresh mode constraint explained above.
6225 intel_wait_for_vblank(dev, pipe);
6227 for_each_encoder_on_crtc(dev, crtc, encoder)
6228 encoder->disable(encoder);
6230 drm_crtc_vblank_off(crtc);
6231 assert_vblank_disabled(crtc);
6233 intel_disable_pipe(intel_crtc);
6235 i9xx_pfit_disable(intel_crtc);
6237 for_each_encoder_on_crtc(dev, crtc, encoder)
6238 if (encoder->post_disable)
6239 encoder->post_disable(encoder);
6241 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
6242 if (IS_CHERRYVIEW(dev))
6243 chv_disable_pll(dev_priv, pipe);
6244 else if (IS_VALLEYVIEW(dev))
6245 vlv_disable_pll(dev_priv, pipe);
6247 i9xx_disable_pll(intel_crtc);
6251 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
6253 intel_crtc->active = false;
6254 intel_update_watermarks(crtc);
6256 mutex_lock(&dev->struct_mutex);
6257 intel_fbc_update(dev);
6258 mutex_unlock(&dev->struct_mutex);
6262 * turn all crtc's off, but do not adjust state
6263 * This has to be paired with a call to intel_modeset_setup_hw_state.
6265 void intel_display_suspend(struct drm_device *dev)
6267 struct drm_i915_private *dev_priv = to_i915(dev);
6268 struct drm_crtc *crtc;
6270 for_each_crtc(dev, crtc) {
6271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6272 enum intel_display_power_domain domain;
6273 unsigned long domains;
6275 if (!intel_crtc->active)
6278 intel_crtc_disable_planes(crtc);
6279 dev_priv->display.crtc_disable(crtc);
6281 domains = intel_crtc->enabled_power_domains;
6282 for_each_power_domain(domain, domains)
6283 intel_display_power_put(dev_priv, domain);
6284 intel_crtc->enabled_power_domains = 0;
6288 /* Master function to enable/disable CRTC and corresponding power wells */
6289 void intel_crtc_control(struct drm_crtc *crtc, bool enable)
6291 struct drm_device *dev = crtc->dev;
6292 struct drm_i915_private *dev_priv = dev->dev_private;
6293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6294 enum intel_display_power_domain domain;
6295 unsigned long domains;
6297 if (enable == intel_crtc->active)
6300 if (enable && !crtc->state->enable)
6303 crtc->state->active = enable;
6305 domains = get_crtc_power_domains(crtc);
6306 for_each_power_domain(domain, domains)
6307 intel_display_power_get(dev_priv, domain);
6308 intel_crtc->enabled_power_domains = domains;
6310 dev_priv->display.crtc_enable(crtc);
6311 intel_crtc_enable_planes(crtc);
6313 intel_crtc_disable_planes(crtc);
6314 dev_priv->display.crtc_disable(crtc);
6316 domains = intel_crtc->enabled_power_domains;
6317 for_each_power_domain(domain, domains)
6318 intel_display_power_put(dev_priv, domain);
6319 intel_crtc->enabled_power_domains = 0;
6324 * Sets the power management mode of the pipe and plane.
6326 void intel_crtc_update_dpms(struct drm_crtc *crtc)
6328 struct drm_device *dev = crtc->dev;
6329 struct intel_encoder *intel_encoder;
6330 bool enable = false;
6332 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6333 enable |= intel_encoder->connectors_active;
6335 intel_crtc_control(crtc, enable);
6338 void intel_encoder_destroy(struct drm_encoder *encoder)
6340 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
6342 drm_encoder_cleanup(encoder);
6343 kfree(intel_encoder);
6346 /* Simple dpms helper for encoders with just one connector, no cloning and only
6347 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6348 * state of the entire output pipe. */
6349 static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
6351 if (mode == DRM_MODE_DPMS_ON) {
6352 encoder->connectors_active = true;
6354 intel_crtc_update_dpms(encoder->base.crtc);
6356 encoder->connectors_active = false;
6358 intel_crtc_update_dpms(encoder->base.crtc);
6362 /* Cross check the actual hw state with our own modeset state tracking (and it's
6363 * internal consistency). */
6364 static void intel_connector_check_state(struct intel_connector *connector)
6366 if (connector->get_hw_state(connector)) {
6367 struct intel_encoder *encoder = connector->encoder;
6368 struct drm_crtc *crtc;
6369 bool encoder_enabled;
6372 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6373 connector->base.base.id,
6374 connector->base.name);
6376 /* there is no real hw state for MST connectors */
6377 if (connector->mst_port)
6380 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
6381 "wrong connector dpms state\n");
6382 I915_STATE_WARN(connector->base.encoder != &encoder->base,
6383 "active connector not linked to encoder\n");
6386 I915_STATE_WARN(!encoder->connectors_active,
6387 "encoder->connectors_active not set\n");
6389 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
6390 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6391 if (I915_STATE_WARN_ON(!encoder->base.crtc))
6394 crtc = encoder->base.crtc;
6396 I915_STATE_WARN(!crtc->state->enable,
6397 "crtc not enabled\n");
6398 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6399 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
6400 "encoder active on the wrong pipe\n");
6405 int intel_connector_init(struct intel_connector *connector)
6407 struct drm_connector_state *connector_state;
6409 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6410 if (!connector_state)
6413 connector->base.state = connector_state;
6417 struct intel_connector *intel_connector_alloc(void)
6419 struct intel_connector *connector;
6421 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6425 if (intel_connector_init(connector) < 0) {
6433 /* Even simpler default implementation, if there's really no special case to
6435 void intel_connector_dpms(struct drm_connector *connector, int mode)
6437 /* All the simple cases only support two dpms states. */
6438 if (mode != DRM_MODE_DPMS_ON)
6439 mode = DRM_MODE_DPMS_OFF;
6441 if (mode == connector->dpms)
6444 connector->dpms = mode;
6446 /* Only need to change hw state when actually enabled */
6447 if (connector->encoder)
6448 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
6450 intel_modeset_check_state(connector->dev);
6453 /* Simple connector->get_hw_state implementation for encoders that support only
6454 * one connector and no cloning and hence the encoder state determines the state
6455 * of the connector. */
6456 bool intel_connector_get_hw_state(struct intel_connector *connector)
6459 struct intel_encoder *encoder = connector->encoder;
6461 return encoder->get_hw_state(encoder, &pipe);
6464 static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
6466 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6467 return crtc_state->fdi_lanes;
6472 static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
6473 struct intel_crtc_state *pipe_config)
6475 struct drm_atomic_state *state = pipe_config->base.state;
6476 struct intel_crtc *other_crtc;
6477 struct intel_crtc_state *other_crtc_state;
6479 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6480 pipe_name(pipe), pipe_config->fdi_lanes);
6481 if (pipe_config->fdi_lanes > 4) {
6482 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6483 pipe_name(pipe), pipe_config->fdi_lanes);
6487 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
6488 if (pipe_config->fdi_lanes > 2) {
6489 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6490 pipe_config->fdi_lanes);
6497 if (INTEL_INFO(dev)->num_pipes == 2)
6500 /* Ivybridge 3 pipe is really complicated */
6505 if (pipe_config->fdi_lanes <= 2)
6508 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6510 intel_atomic_get_crtc_state(state, other_crtc);
6511 if (IS_ERR(other_crtc_state))
6512 return PTR_ERR(other_crtc_state);
6514 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
6515 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6516 pipe_name(pipe), pipe_config->fdi_lanes);
6521 if (pipe_config->fdi_lanes > 2) {
6522 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6523 pipe_name(pipe), pipe_config->fdi_lanes);
6527 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6529 intel_atomic_get_crtc_state(state, other_crtc);
6530 if (IS_ERR(other_crtc_state))
6531 return PTR_ERR(other_crtc_state);
6533 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
6534 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6544 static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
6545 struct intel_crtc_state *pipe_config)
6547 struct drm_device *dev = intel_crtc->base.dev;
6548 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6549 int lane, link_bw, fdi_dotclock, ret;
6550 bool needs_recompute = false;
6553 /* FDI is a binary signal running at ~2.7GHz, encoding
6554 * each output octet as 10 bits. The actual frequency
6555 * is stored as a divider into a 100MHz clock, and the
6556 * mode pixel clock is stored in units of 1KHz.
6557 * Hence the bw of each lane in terms of the mode signal
6560 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6562 fdi_dotclock = adjusted_mode->crtc_clock;
6564 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
6565 pipe_config->pipe_bpp);
6567 pipe_config->fdi_lanes = lane;
6569 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
6570 link_bw, &pipe_config->fdi_m_n);
6572 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6573 intel_crtc->pipe, pipe_config);
6574 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
6575 pipe_config->pipe_bpp -= 2*3;
6576 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6577 pipe_config->pipe_bpp);
6578 needs_recompute = true;
6579 pipe_config->bw_constrained = true;
6584 if (needs_recompute)
6590 static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6591 struct intel_crtc_state *pipe_config)
6593 if (pipe_config->pipe_bpp > 24)
6596 /* HSW can handle pixel rate up to cdclk? */
6597 if (IS_HASWELL(dev_priv->dev))
6601 * We compare against max which means we must take
6602 * the increased cdclk requirement into account when
6603 * calculating the new cdclk.
6605 * Should measure whether using a lower cdclk w/o IPS
6607 return ilk_pipe_pixel_rate(pipe_config) <=
6608 dev_priv->max_cdclk_freq * 95 / 100;
6611 static void hsw_compute_ips_config(struct intel_crtc *crtc,
6612 struct intel_crtc_state *pipe_config)
6614 struct drm_device *dev = crtc->base.dev;
6615 struct drm_i915_private *dev_priv = dev->dev_private;
6617 pipe_config->ips_enabled = i915.enable_ips &&
6618 hsw_crtc_supports_ips(crtc) &&
6619 pipe_config_supports_ips(dev_priv, pipe_config);
6622 static int intel_crtc_compute_config(struct intel_crtc *crtc,
6623 struct intel_crtc_state *pipe_config)
6625 struct drm_device *dev = crtc->base.dev;
6626 struct drm_i915_private *dev_priv = dev->dev_private;
6627 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6630 /* FIXME should check pixel clock limits on all platforms */
6631 if (INTEL_INFO(dev)->gen < 4) {
6632 int clock_limit = dev_priv->max_cdclk_freq;
6635 * Enable pixel doubling when the dot clock
6636 * is > 90% of the (display) core speed.
6638 * GDG double wide on either pipe,
6639 * otherwise pipe A only.
6641 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
6642 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
6644 pipe_config->double_wide = true;
6647 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
6652 * Pipe horizontal size must be even in:
6654 * - LVDS dual channel mode
6655 * - Double wide pipe
6657 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
6658 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6659 pipe_config->pipe_src_w &= ~1;
6661 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6662 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
6664 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6665 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
6669 hsw_compute_ips_config(crtc, pipe_config);
6671 if (pipe_config->has_pch_encoder)
6672 return ironlake_fdi_compute_config(crtc, pipe_config);
6674 /* FIXME: remove below call once atomic mode set is place and all crtc
6675 * related checks called from atomic_crtc_check function */
6677 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6678 crtc, pipe_config->base.state);
6679 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6684 static int skylake_get_display_clock_speed(struct drm_device *dev)
6686 struct drm_i915_private *dev_priv = to_i915(dev);
6687 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6688 uint32_t cdctl = I915_READ(CDCLK_CTL);
6691 if (!(lcpll1 & LCPLL_PLL_ENABLE))
6692 return 24000; /* 24MHz is the cd freq with NSSC ref */
6694 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6697 linkrate = (I915_READ(DPLL_CTRL1) &
6698 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
6700 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6701 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
6703 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6704 case CDCLK_FREQ_450_432:
6706 case CDCLK_FREQ_337_308:
6708 case CDCLK_FREQ_675_617:
6711 WARN(1, "Unknown cd freq selection\n");
6715 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6716 case CDCLK_FREQ_450_432:
6718 case CDCLK_FREQ_337_308:
6720 case CDCLK_FREQ_675_617:
6723 WARN(1, "Unknown cd freq selection\n");
6727 /* error case, do as if DPLL0 isn't enabled */
6731 static int broadwell_get_display_clock_speed(struct drm_device *dev)
6733 struct drm_i915_private *dev_priv = dev->dev_private;
6734 uint32_t lcpll = I915_READ(LCPLL_CTL);
6735 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6737 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6739 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6741 else if (freq == LCPLL_CLK_FREQ_450)
6743 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6745 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6751 static int haswell_get_display_clock_speed(struct drm_device *dev)
6753 struct drm_i915_private *dev_priv = dev->dev_private;
6754 uint32_t lcpll = I915_READ(LCPLL_CTL);
6755 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6757 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6759 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6761 else if (freq == LCPLL_CLK_FREQ_450)
6763 else if (IS_HSW_ULT(dev))
6769 static int valleyview_get_display_clock_speed(struct drm_device *dev)
6771 struct drm_i915_private *dev_priv = dev->dev_private;
6775 if (dev_priv->hpll_freq == 0)
6776 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6778 mutex_lock(&dev_priv->sb_lock);
6779 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6780 mutex_unlock(&dev_priv->sb_lock);
6782 divider = val & DISPLAY_FREQUENCY_VALUES;
6784 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6785 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6786 "cdclk change in progress\n");
6788 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
6791 static int ilk_get_display_clock_speed(struct drm_device *dev)
6796 static int i945_get_display_clock_speed(struct drm_device *dev)
6801 static int i915_get_display_clock_speed(struct drm_device *dev)
6806 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6811 static int pnv_get_display_clock_speed(struct drm_device *dev)
6815 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6817 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6818 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
6820 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
6822 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
6824 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6827 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6828 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
6830 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
6835 static int i915gm_get_display_clock_speed(struct drm_device *dev)
6839 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6841 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
6844 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6845 case GC_DISPLAY_CLOCK_333_MHZ:
6848 case GC_DISPLAY_CLOCK_190_200_MHZ:
6854 static int i865_get_display_clock_speed(struct drm_device *dev)
6859 static int i85x_get_display_clock_speed(struct drm_device *dev)
6864 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6865 * encoding is different :(
6866 * FIXME is this the right way to detect 852GM/852GMV?
6868 if (dev->pdev->revision == 0x1)
6871 pci_bus_read_config_word(dev->pdev->bus,
6872 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6874 /* Assume that the hardware is in the high speed state. This
6875 * should be the default.
6877 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6878 case GC_CLOCK_133_200:
6879 case GC_CLOCK_133_200_2:
6880 case GC_CLOCK_100_200:
6882 case GC_CLOCK_166_250:
6884 case GC_CLOCK_100_133:
6886 case GC_CLOCK_133_266:
6887 case GC_CLOCK_133_266_2:
6888 case GC_CLOCK_166_266:
6892 /* Shouldn't happen */
6896 static int i830_get_display_clock_speed(struct drm_device *dev)
6901 static unsigned int intel_hpll_vco(struct drm_device *dev)
6903 struct drm_i915_private *dev_priv = dev->dev_private;
6904 static const unsigned int blb_vco[8] = {
6911 static const unsigned int pnv_vco[8] = {
6918 static const unsigned int cl_vco[8] = {
6927 static const unsigned int elk_vco[8] = {
6933 static const unsigned int ctg_vco[8] = {
6941 const unsigned int *vco_table;
6945 /* FIXME other chipsets? */
6947 vco_table = ctg_vco;
6948 else if (IS_G4X(dev))
6949 vco_table = elk_vco;
6950 else if (IS_CRESTLINE(dev))
6952 else if (IS_PINEVIEW(dev))
6953 vco_table = pnv_vco;
6954 else if (IS_G33(dev))
6955 vco_table = blb_vco;
6959 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6961 vco = vco_table[tmp & 0x7];
6963 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6965 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6970 static int gm45_get_display_clock_speed(struct drm_device *dev)
6972 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6975 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6977 cdclk_sel = (tmp >> 12) & 0x1;
6983 return cdclk_sel ? 333333 : 222222;
6985 return cdclk_sel ? 320000 : 228571;
6987 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6992 static int i965gm_get_display_clock_speed(struct drm_device *dev)
6994 static const uint8_t div_3200[] = { 16, 10, 8 };
6995 static const uint8_t div_4000[] = { 20, 12, 10 };
6996 static const uint8_t div_5333[] = { 24, 16, 14 };
6997 const uint8_t *div_table;
6998 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7001 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7003 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7005 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7010 div_table = div_3200;
7013 div_table = div_4000;
7016 div_table = div_5333;
7022 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7025 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7029 static int g33_get_display_clock_speed(struct drm_device *dev)
7031 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7032 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7033 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7034 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7035 const uint8_t *div_table;
7036 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7039 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7041 cdclk_sel = (tmp >> 4) & 0x7;
7043 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7048 div_table = div_3200;
7051 div_table = div_4000;
7054 div_table = div_4800;
7057 div_table = div_5333;
7063 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7066 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7071 intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
7073 while (*num > DATA_LINK_M_N_MASK ||
7074 *den > DATA_LINK_M_N_MASK) {
7080 static void compute_m_n(unsigned int m, unsigned int n,
7081 uint32_t *ret_m, uint32_t *ret_n)
7083 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7084 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7085 intel_reduce_m_n_ratio(ret_m, ret_n);
7089 intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7090 int pixel_clock, int link_clock,
7091 struct intel_link_m_n *m_n)
7095 compute_m_n(bits_per_pixel * pixel_clock,
7096 link_clock * nlanes * 8,
7097 &m_n->gmch_m, &m_n->gmch_n);
7099 compute_m_n(pixel_clock, link_clock,
7100 &m_n->link_m, &m_n->link_n);
7103 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7105 if (i915.panel_use_ssc >= 0)
7106 return i915.panel_use_ssc != 0;
7107 return dev_priv->vbt.lvds_use_ssc
7108 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
7111 static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7114 struct drm_device *dev = crtc_state->base.crtc->dev;
7115 struct drm_i915_private *dev_priv = dev->dev_private;
7118 WARN_ON(!crtc_state->base.state);
7120 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
7122 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7123 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
7124 refclk = dev_priv->vbt.lvds_ssc_freq;
7125 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
7126 } else if (!IS_GEN2(dev)) {
7135 static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
7137 return (1 << dpll->n) << 16 | dpll->m2;
7140 static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7142 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
7145 static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
7146 struct intel_crtc_state *crtc_state,
7147 intel_clock_t *reduced_clock)
7149 struct drm_device *dev = crtc->base.dev;
7152 if (IS_PINEVIEW(dev)) {
7153 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
7155 fp2 = pnv_dpll_compute_fp(reduced_clock);
7157 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
7159 fp2 = i9xx_dpll_compute_fp(reduced_clock);
7162 crtc_state->dpll_hw_state.fp0 = fp;
7164 crtc->lowfreq_avail = false;
7165 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7167 crtc_state->dpll_hw_state.fp1 = fp2;
7168 crtc->lowfreq_avail = true;
7170 crtc_state->dpll_hw_state.fp1 = fp;
7174 static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7180 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7181 * and set it to a reasonable value instead.
7183 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
7184 reg_val &= 0xffffff00;
7185 reg_val |= 0x00000030;
7186 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
7188 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
7189 reg_val &= 0x8cffffff;
7190 reg_val = 0x8c000000;
7191 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
7193 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
7194 reg_val &= 0xffffff00;
7195 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
7197 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
7198 reg_val &= 0x00ffffff;
7199 reg_val |= 0xb0000000;
7200 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
7203 static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7204 struct intel_link_m_n *m_n)
7206 struct drm_device *dev = crtc->base.dev;
7207 struct drm_i915_private *dev_priv = dev->dev_private;
7208 int pipe = crtc->pipe;
7210 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7211 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7212 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7213 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
7216 static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
7217 struct intel_link_m_n *m_n,
7218 struct intel_link_m_n *m2_n2)
7220 struct drm_device *dev = crtc->base.dev;
7221 struct drm_i915_private *dev_priv = dev->dev_private;
7222 int pipe = crtc->pipe;
7223 enum transcoder transcoder = crtc->config->cpu_transcoder;
7225 if (INTEL_INFO(dev)->gen >= 5) {
7226 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7227 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7228 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7229 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
7230 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7231 * for gen < 8) and if DRRS is supported (to make sure the
7232 * registers are not unnecessarily accessed).
7234 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
7235 crtc->config->has_drrs) {
7236 I915_WRITE(PIPE_DATA_M2(transcoder),
7237 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7238 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7239 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7240 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7243 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7244 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7245 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7246 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
7250 void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
7252 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7255 dp_m_n = &crtc->config->dp_m_n;
7256 dp_m2_n2 = &crtc->config->dp_m2_n2;
7257 } else if (m_n == M2_N2) {
7260 * M2_N2 registers are not supported. Hence m2_n2 divider value
7261 * needs to be programmed into M1_N1.
7263 dp_m_n = &crtc->config->dp_m2_n2;
7265 DRM_ERROR("Unsupported divider value\n");
7269 if (crtc->config->has_pch_encoder)
7270 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
7272 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
7275 static void vlv_update_pll(struct intel_crtc *crtc,
7276 struct intel_crtc_state *pipe_config)
7281 * Enable DPIO clock input. We should never disable the reference
7282 * clock for pipe B, since VGA hotplug / manual detection depends
7285 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7286 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7287 /* We should never disable this, set it here for state tracking */
7288 if (crtc->pipe == PIPE_B)
7289 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7290 dpll |= DPLL_VCO_ENABLE;
7291 pipe_config->dpll_hw_state.dpll = dpll;
7293 dpll_md = (pipe_config->pixel_multiplier - 1)
7294 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7295 pipe_config->dpll_hw_state.dpll_md = dpll_md;
7298 static void vlv_prepare_pll(struct intel_crtc *crtc,
7299 const struct intel_crtc_state *pipe_config)
7301 struct drm_device *dev = crtc->base.dev;
7302 struct drm_i915_private *dev_priv = dev->dev_private;
7303 int pipe = crtc->pipe;
7305 u32 bestn, bestm1, bestm2, bestp1, bestp2;
7306 u32 coreclk, reg_val;
7308 mutex_lock(&dev_priv->sb_lock);
7310 bestn = pipe_config->dpll.n;
7311 bestm1 = pipe_config->dpll.m1;
7312 bestm2 = pipe_config->dpll.m2;
7313 bestp1 = pipe_config->dpll.p1;
7314 bestp2 = pipe_config->dpll.p2;
7316 /* See eDP HDMI DPIO driver vbios notes doc */
7318 /* PLL B needs special handling */
7320 vlv_pllb_recal_opamp(dev_priv, pipe);
7322 /* Set up Tx target for periodic Rcomp update */
7323 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
7325 /* Disable target IRef on PLL */
7326 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
7327 reg_val &= 0x00ffffff;
7328 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
7330 /* Disable fast lock */
7331 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
7333 /* Set idtafcrecal before PLL is enabled */
7334 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7335 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7336 mdiv |= ((bestn << DPIO_N_SHIFT));
7337 mdiv |= (1 << DPIO_K_SHIFT);
7340 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7341 * but we don't support that).
7342 * Note: don't use the DAC post divider as it seems unstable.
7344 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
7345 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
7347 mdiv |= DPIO_ENABLE_CALIBRATION;
7348 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
7350 /* Set HBR and RBR LPF coefficients */
7351 if (pipe_config->port_clock == 162000 ||
7352 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7353 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
7354 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
7357 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
7360 if (pipe_config->has_dp_encoder) {
7361 /* Use SSC source */
7363 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7366 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7368 } else { /* HDMI or VGA */
7369 /* Use bend source */
7371 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7374 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
7378 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
7379 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
7380 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7381 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
7382 coreclk |= 0x01000000;
7383 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
7385 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
7386 mutex_unlock(&dev_priv->sb_lock);
7389 static void chv_update_pll(struct intel_crtc *crtc,
7390 struct intel_crtc_state *pipe_config)
7392 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
7393 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7395 if (crtc->pipe != PIPE_A)
7396 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7398 pipe_config->dpll_hw_state.dpll_md =
7399 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7402 static void chv_prepare_pll(struct intel_crtc *crtc,
7403 const struct intel_crtc_state *pipe_config)
7405 struct drm_device *dev = crtc->base.dev;
7406 struct drm_i915_private *dev_priv = dev->dev_private;
7407 int pipe = crtc->pipe;
7408 int dpll_reg = DPLL(crtc->pipe);
7409 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7410 u32 loopfilter, tribuf_calcntr;
7411 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
7415 bestn = pipe_config->dpll.n;
7416 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7417 bestm1 = pipe_config->dpll.m1;
7418 bestm2 = pipe_config->dpll.m2 >> 22;
7419 bestp1 = pipe_config->dpll.p1;
7420 bestp2 = pipe_config->dpll.p2;
7421 vco = pipe_config->dpll.vco;
7426 * Enable Refclk and SSC
7428 I915_WRITE(dpll_reg,
7429 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
7431 mutex_lock(&dev_priv->sb_lock);
7433 /* p1 and p2 divider */
7434 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7435 5 << DPIO_CHV_S1_DIV_SHIFT |
7436 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7437 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7438 1 << DPIO_CHV_K_DIV_SHIFT);
7440 /* Feedback post-divider - m2 */
7441 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7443 /* Feedback refclk divider - n and m1 */
7444 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7445 DPIO_CHV_M1_DIV_BY_2 |
7446 1 << DPIO_CHV_N_DIV_SHIFT);
7448 /* M2 fraction division */
7450 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
7452 /* M2 fraction division enable */
7453 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7454 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7455 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7457 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7458 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
7460 /* Program digital lock detect threshold */
7461 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7462 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7463 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7464 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7466 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7467 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7470 if (vco == 5400000) {
7471 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7472 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7473 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7474 tribuf_calcntr = 0x9;
7475 } else if (vco <= 6200000) {
7476 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7477 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7478 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7479 tribuf_calcntr = 0x9;
7480 } else if (vco <= 6480000) {
7481 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7482 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7483 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7484 tribuf_calcntr = 0x8;
7486 /* Not supported. Apply the same limits as in the max case */
7487 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7488 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7489 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7492 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7494 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
7495 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7496 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7497 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7500 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7501 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7504 mutex_unlock(&dev_priv->sb_lock);
7508 * vlv_force_pll_on - forcibly enable just the PLL
7509 * @dev_priv: i915 private structure
7510 * @pipe: pipe PLL to enable
7511 * @dpll: PLL configuration
7513 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7514 * in cases where we need the PLL enabled even when @pipe is not going to
7517 void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7518 const struct dpll *dpll)
7520 struct intel_crtc *crtc =
7521 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
7522 struct intel_crtc_state pipe_config = {
7523 .base.crtc = &crtc->base,
7524 .pixel_multiplier = 1,
7528 if (IS_CHERRYVIEW(dev)) {
7529 chv_update_pll(crtc, &pipe_config);
7530 chv_prepare_pll(crtc, &pipe_config);
7531 chv_enable_pll(crtc, &pipe_config);
7533 vlv_update_pll(crtc, &pipe_config);
7534 vlv_prepare_pll(crtc, &pipe_config);
7535 vlv_enable_pll(crtc, &pipe_config);
7540 * vlv_force_pll_off - forcibly disable just the PLL
7541 * @dev_priv: i915 private structure
7542 * @pipe: pipe PLL to disable
7544 * Disable the PLL for @pipe. To be used in cases where we need
7545 * the PLL enabled even when @pipe is not going to be enabled.
7547 void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7549 if (IS_CHERRYVIEW(dev))
7550 chv_disable_pll(to_i915(dev), pipe);
7552 vlv_disable_pll(to_i915(dev), pipe);
7555 static void i9xx_update_pll(struct intel_crtc *crtc,
7556 struct intel_crtc_state *crtc_state,
7557 intel_clock_t *reduced_clock,
7560 struct drm_device *dev = crtc->base.dev;
7561 struct drm_i915_private *dev_priv = dev->dev_private;
7564 struct dpll *clock = &crtc_state->dpll;
7566 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
7568 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7569 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
7571 dpll = DPLL_VGA_MODE_DIS;
7573 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
7574 dpll |= DPLLB_MODE_LVDS;
7576 dpll |= DPLLB_MODE_DAC_SERIAL;
7578 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7579 dpll |= (crtc_state->pixel_multiplier - 1)
7580 << SDVO_MULTIPLIER_SHIFT_HIRES;
7584 dpll |= DPLL_SDVO_HIGH_SPEED;
7586 if (crtc_state->has_dp_encoder)
7587 dpll |= DPLL_SDVO_HIGH_SPEED;
7589 /* compute bitmask from p1 value */
7590 if (IS_PINEVIEW(dev))
7591 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7593 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7594 if (IS_G4X(dev) && reduced_clock)
7595 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7597 switch (clock->p2) {
7599 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7602 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7605 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7608 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7611 if (INTEL_INFO(dev)->gen >= 4)
7612 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7614 if (crtc_state->sdvo_tv_clock)
7615 dpll |= PLL_REF_INPUT_TVCLKINBC;
7616 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7617 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7618 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7620 dpll |= PLL_REF_INPUT_DREFCLK;
7622 dpll |= DPLL_VCO_ENABLE;
7623 crtc_state->dpll_hw_state.dpll = dpll;
7625 if (INTEL_INFO(dev)->gen >= 4) {
7626 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
7627 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
7628 crtc_state->dpll_hw_state.dpll_md = dpll_md;
7632 static void i8xx_update_pll(struct intel_crtc *crtc,
7633 struct intel_crtc_state *crtc_state,
7634 intel_clock_t *reduced_clock,
7637 struct drm_device *dev = crtc->base.dev;
7638 struct drm_i915_private *dev_priv = dev->dev_private;
7640 struct dpll *clock = &crtc_state->dpll;
7642 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
7644 dpll = DPLL_VGA_MODE_DIS;
7646 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
7647 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7650 dpll |= PLL_P1_DIVIDE_BY_TWO;
7652 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7654 dpll |= PLL_P2_DIVIDE_BY_4;
7657 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
7658 dpll |= DPLL_DVO_2X_MODE;
7660 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
7661 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7662 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7664 dpll |= PLL_REF_INPUT_DREFCLK;
7666 dpll |= DPLL_VCO_ENABLE;
7667 crtc_state->dpll_hw_state.dpll = dpll;
7670 static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
7672 struct drm_device *dev = intel_crtc->base.dev;
7673 struct drm_i915_private *dev_priv = dev->dev_private;
7674 enum pipe pipe = intel_crtc->pipe;
7675 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7676 struct drm_display_mode *adjusted_mode =
7677 &intel_crtc->config->base.adjusted_mode;
7678 uint32_t crtc_vtotal, crtc_vblank_end;
7681 /* We need to be careful not to changed the adjusted mode, for otherwise
7682 * the hw state checker will get angry at the mismatch. */
7683 crtc_vtotal = adjusted_mode->crtc_vtotal;
7684 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
7686 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
7687 /* the chip adds 2 halflines automatically */
7689 crtc_vblank_end -= 1;
7691 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7692 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7694 vsyncshift = adjusted_mode->crtc_hsync_start -
7695 adjusted_mode->crtc_htotal / 2;
7697 vsyncshift += adjusted_mode->crtc_htotal;
7700 if (INTEL_INFO(dev)->gen > 3)
7701 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
7703 I915_WRITE(HTOTAL(cpu_transcoder),
7704 (adjusted_mode->crtc_hdisplay - 1) |
7705 ((adjusted_mode->crtc_htotal - 1) << 16));
7706 I915_WRITE(HBLANK(cpu_transcoder),
7707 (adjusted_mode->crtc_hblank_start - 1) |
7708 ((adjusted_mode->crtc_hblank_end - 1) << 16));
7709 I915_WRITE(HSYNC(cpu_transcoder),
7710 (adjusted_mode->crtc_hsync_start - 1) |
7711 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7713 I915_WRITE(VTOTAL(cpu_transcoder),
7714 (adjusted_mode->crtc_vdisplay - 1) |
7715 ((crtc_vtotal - 1) << 16));
7716 I915_WRITE(VBLANK(cpu_transcoder),
7717 (adjusted_mode->crtc_vblank_start - 1) |
7718 ((crtc_vblank_end - 1) << 16));
7719 I915_WRITE(VSYNC(cpu_transcoder),
7720 (adjusted_mode->crtc_vsync_start - 1) |
7721 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7723 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7724 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7725 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7727 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7728 (pipe == PIPE_B || pipe == PIPE_C))
7729 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7731 /* pipesrc controls the size that is scaled from, which should
7732 * always be the user's requested size.
7734 I915_WRITE(PIPESRC(pipe),
7735 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7736 (intel_crtc->config->pipe_src_h - 1));
7739 static void intel_get_pipe_timings(struct intel_crtc *crtc,
7740 struct intel_crtc_state *pipe_config)
7742 struct drm_device *dev = crtc->base.dev;
7743 struct drm_i915_private *dev_priv = dev->dev_private;
7744 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7747 tmp = I915_READ(HTOTAL(cpu_transcoder));
7748 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7749 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
7750 tmp = I915_READ(HBLANK(cpu_transcoder));
7751 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7752 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
7753 tmp = I915_READ(HSYNC(cpu_transcoder));
7754 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7755 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
7757 tmp = I915_READ(VTOTAL(cpu_transcoder));
7758 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7759 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
7760 tmp = I915_READ(VBLANK(cpu_transcoder));
7761 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7762 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
7763 tmp = I915_READ(VSYNC(cpu_transcoder));
7764 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7765 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
7767 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
7768 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7769 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7770 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
7773 tmp = I915_READ(PIPESRC(crtc->pipe));
7774 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7775 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7777 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7778 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
7781 void intel_mode_from_pipe_config(struct drm_display_mode *mode,
7782 struct intel_crtc_state *pipe_config)
7784 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7785 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7786 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7787 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
7789 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7790 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7791 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7792 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
7794 mode->flags = pipe_config->base.adjusted_mode.flags;
7796 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7797 mode->flags |= pipe_config->base.adjusted_mode.flags;
7800 static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7802 struct drm_device *dev = intel_crtc->base.dev;
7803 struct drm_i915_private *dev_priv = dev->dev_private;
7808 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7809 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7810 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
7812 if (intel_crtc->config->double_wide)
7813 pipeconf |= PIPECONF_DOUBLE_WIDE;
7815 /* only g4x and later have fancy bpc/dither controls */
7816 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7817 /* Bspec claims that we can't use dithering for 30bpp pipes. */
7818 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
7819 pipeconf |= PIPECONF_DITHER_EN |
7820 PIPECONF_DITHER_TYPE_SP;
7822 switch (intel_crtc->config->pipe_bpp) {
7824 pipeconf |= PIPECONF_6BPC;
7827 pipeconf |= PIPECONF_8BPC;
7830 pipeconf |= PIPECONF_10BPC;
7833 /* Case prevented by intel_choose_pipe_bpp_dither. */
7838 if (HAS_PIPE_CXSR(dev)) {
7839 if (intel_crtc->lowfreq_avail) {
7840 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7841 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7843 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
7847 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
7848 if (INTEL_INFO(dev)->gen < 4 ||
7849 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7850 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7852 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7854 pipeconf |= PIPECONF_PROGRESSIVE;
7856 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
7857 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
7859 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7860 POSTING_READ(PIPECONF(intel_crtc->pipe));
7863 static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7864 struct intel_crtc_state *crtc_state)
7866 struct drm_device *dev = crtc->base.dev;
7867 struct drm_i915_private *dev_priv = dev->dev_private;
7868 int refclk, num_connectors = 0;
7869 intel_clock_t clock, reduced_clock;
7870 bool ok, has_reduced_clock = false;
7871 bool is_lvds = false, is_dsi = false;
7872 struct intel_encoder *encoder;
7873 const intel_limit_t *limit;
7874 struct drm_atomic_state *state = crtc_state->base.state;
7875 struct drm_connector *connector;
7876 struct drm_connector_state *connector_state;
7879 memset(&crtc_state->dpll_hw_state, 0,
7880 sizeof(crtc_state->dpll_hw_state));
7882 for_each_connector_in_state(state, connector, connector_state, i) {
7883 if (connector_state->crtc != &crtc->base)
7886 encoder = to_intel_encoder(connector_state->best_encoder);
7888 switch (encoder->type) {
7889 case INTEL_OUTPUT_LVDS:
7892 case INTEL_OUTPUT_DSI:
7905 if (!crtc_state->clock_set) {
7906 refclk = i9xx_get_refclk(crtc_state, num_connectors);
7909 * Returns a set of divisors for the desired target clock with
7910 * the given refclk, or FALSE. The returned values represent
7911 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7914 limit = intel_limit(crtc_state, refclk);
7915 ok = dev_priv->display.find_dpll(limit, crtc_state,
7916 crtc_state->port_clock,
7917 refclk, NULL, &clock);
7919 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7923 if (is_lvds && dev_priv->lvds_downclock_avail) {
7925 * Ensure we match the reduced clock's P to the target
7926 * clock. If the clocks don't match, we can't switch
7927 * the display clock by using the FP0/FP1. In such case
7928 * we will disable the LVDS downclock feature.
7931 dev_priv->display.find_dpll(limit, crtc_state,
7932 dev_priv->lvds_downclock,
7936 /* Compat-code for transition, will disappear. */
7937 crtc_state->dpll.n = clock.n;
7938 crtc_state->dpll.m1 = clock.m1;
7939 crtc_state->dpll.m2 = clock.m2;
7940 crtc_state->dpll.p1 = clock.p1;
7941 crtc_state->dpll.p2 = clock.p2;
7945 i8xx_update_pll(crtc, crtc_state,
7946 has_reduced_clock ? &reduced_clock : NULL,
7948 } else if (IS_CHERRYVIEW(dev)) {
7949 chv_update_pll(crtc, crtc_state);
7950 } else if (IS_VALLEYVIEW(dev)) {
7951 vlv_update_pll(crtc, crtc_state);
7953 i9xx_update_pll(crtc, crtc_state,
7954 has_reduced_clock ? &reduced_clock : NULL,
7961 static void i9xx_get_pfit_config(struct intel_crtc *crtc,
7962 struct intel_crtc_state *pipe_config)
7964 struct drm_device *dev = crtc->base.dev;
7965 struct drm_i915_private *dev_priv = dev->dev_private;
7968 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7971 tmp = I915_READ(PFIT_CONTROL);
7972 if (!(tmp & PFIT_ENABLE))
7975 /* Check whether the pfit is attached to our pipe. */
7976 if (INTEL_INFO(dev)->gen < 4) {
7977 if (crtc->pipe != PIPE_B)
7980 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7984 pipe_config->gmch_pfit.control = tmp;
7985 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7986 if (INTEL_INFO(dev)->gen < 5)
7987 pipe_config->gmch_pfit.lvds_border_bits =
7988 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7991 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
7992 struct intel_crtc_state *pipe_config)
7994 struct drm_device *dev = crtc->base.dev;
7995 struct drm_i915_private *dev_priv = dev->dev_private;
7996 int pipe = pipe_config->cpu_transcoder;
7997 intel_clock_t clock;
7999 int refclk = 100000;
8001 /* In case of MIPI DPLL will not even be used */
8002 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8005 mutex_lock(&dev_priv->sb_lock);
8006 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
8007 mutex_unlock(&dev_priv->sb_lock);
8009 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8010 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8011 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8012 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8013 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8015 vlv_clock(refclk, &clock);
8017 /* clock.dot is the fast clock */
8018 pipe_config->port_clock = clock.dot / 5;
8022 i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8023 struct intel_initial_plane_config *plane_config)
8025 struct drm_device *dev = crtc->base.dev;
8026 struct drm_i915_private *dev_priv = dev->dev_private;
8027 u32 val, base, offset;
8028 int pipe = crtc->pipe, plane = crtc->plane;
8029 int fourcc, pixel_format;
8030 unsigned int aligned_height;
8031 struct drm_framebuffer *fb;
8032 struct intel_framebuffer *intel_fb;
8034 val = I915_READ(DSPCNTR(plane));
8035 if (!(val & DISPLAY_PLANE_ENABLE))
8038 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8040 DRM_DEBUG_KMS("failed to alloc fb\n");
8044 fb = &intel_fb->base;
8046 if (INTEL_INFO(dev)->gen >= 4) {
8047 if (val & DISPPLANE_TILED) {
8048 plane_config->tiling = I915_TILING_X;
8049 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8053 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
8054 fourcc = i9xx_format_to_fourcc(pixel_format);
8055 fb->pixel_format = fourcc;
8056 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8058 if (INTEL_INFO(dev)->gen >= 4) {
8059 if (plane_config->tiling)
8060 offset = I915_READ(DSPTILEOFF(plane));
8062 offset = I915_READ(DSPLINOFF(plane));
8063 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8065 base = I915_READ(DSPADDR(plane));
8067 plane_config->base = base;
8069 val = I915_READ(PIPESRC(pipe));
8070 fb->width = ((val >> 16) & 0xfff) + 1;
8071 fb->height = ((val >> 0) & 0xfff) + 1;
8073 val = I915_READ(DSPSTRIDE(pipe));
8074 fb->pitches[0] = val & 0xffffffc0;
8076 aligned_height = intel_fb_align_height(dev, fb->height,
8080 plane_config->size = fb->pitches[0] * aligned_height;
8082 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8083 pipe_name(pipe), plane, fb->width, fb->height,
8084 fb->bits_per_pixel, base, fb->pitches[0],
8085 plane_config->size);
8087 plane_config->fb = intel_fb;
8090 static void chv_crtc_clock_get(struct intel_crtc *crtc,
8091 struct intel_crtc_state *pipe_config)
8093 struct drm_device *dev = crtc->base.dev;
8094 struct drm_i915_private *dev_priv = dev->dev_private;
8095 int pipe = pipe_config->cpu_transcoder;
8096 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8097 intel_clock_t clock;
8098 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8099 int refclk = 100000;
8101 mutex_lock(&dev_priv->sb_lock);
8102 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8103 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8104 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8105 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
8106 mutex_unlock(&dev_priv->sb_lock);
8108 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8109 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8110 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8111 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8112 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8114 chv_clock(refclk, &clock);
8116 /* clock.dot is the fast clock */
8117 pipe_config->port_clock = clock.dot / 5;
8120 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
8121 struct intel_crtc_state *pipe_config)
8123 struct drm_device *dev = crtc->base.dev;
8124 struct drm_i915_private *dev_priv = dev->dev_private;
8127 if (!intel_display_power_is_enabled(dev_priv,
8128 POWER_DOMAIN_PIPE(crtc->pipe)))
8131 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8132 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8134 tmp = I915_READ(PIPECONF(crtc->pipe));
8135 if (!(tmp & PIPECONF_ENABLE))
8138 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8139 switch (tmp & PIPECONF_BPC_MASK) {
8141 pipe_config->pipe_bpp = 18;
8144 pipe_config->pipe_bpp = 24;
8146 case PIPECONF_10BPC:
8147 pipe_config->pipe_bpp = 30;
8154 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8155 pipe_config->limited_color_range = true;
8157 if (INTEL_INFO(dev)->gen < 4)
8158 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8160 intel_get_pipe_timings(crtc, pipe_config);
8162 i9xx_get_pfit_config(crtc, pipe_config);
8164 if (INTEL_INFO(dev)->gen >= 4) {
8165 tmp = I915_READ(DPLL_MD(crtc->pipe));
8166 pipe_config->pixel_multiplier =
8167 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8168 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8169 pipe_config->dpll_hw_state.dpll_md = tmp;
8170 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8171 tmp = I915_READ(DPLL(crtc->pipe));
8172 pipe_config->pixel_multiplier =
8173 ((tmp & SDVO_MULTIPLIER_MASK)
8174 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8176 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8177 * port and will be fixed up in the encoder->get_config
8179 pipe_config->pixel_multiplier = 1;
8181 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8182 if (!IS_VALLEYVIEW(dev)) {
8184 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8185 * on 830. Filter it out here so that we don't
8186 * report errors due to that.
8189 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8191 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8192 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
8194 /* Mask out read-only status bits. */
8195 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8196 DPLL_PORTC_READY_MASK |
8197 DPLL_PORTB_READY_MASK);
8200 if (IS_CHERRYVIEW(dev))
8201 chv_crtc_clock_get(crtc, pipe_config);
8202 else if (IS_VALLEYVIEW(dev))
8203 vlv_crtc_clock_get(crtc, pipe_config);
8205 i9xx_crtc_clock_get(crtc, pipe_config);
8210 static void ironlake_init_pch_refclk(struct drm_device *dev)
8212 struct drm_i915_private *dev_priv = dev->dev_private;
8213 struct intel_encoder *encoder;
8215 bool has_lvds = false;
8216 bool has_cpu_edp = false;
8217 bool has_panel = false;
8218 bool has_ck505 = false;
8219 bool can_ssc = false;
8221 /* We need to take the global config into account */
8222 for_each_intel_encoder(dev, encoder) {
8223 switch (encoder->type) {
8224 case INTEL_OUTPUT_LVDS:
8228 case INTEL_OUTPUT_EDP:
8230 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
8238 if (HAS_PCH_IBX(dev)) {
8239 has_ck505 = dev_priv->vbt.display_clock_mode;
8240 can_ssc = has_ck505;
8246 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8247 has_panel, has_lvds, has_ck505);
8249 /* Ironlake: try to setup display ref clock before DPLL
8250 * enabling. This is only under driver's control after
8251 * PCH B stepping, previous chipset stepping should be
8252 * ignoring this setting.
8254 val = I915_READ(PCH_DREF_CONTROL);
8256 /* As we must carefully and slowly disable/enable each source in turn,
8257 * compute the final state we want first and check if we need to
8258 * make any changes at all.
8261 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8263 final |= DREF_NONSPREAD_CK505_ENABLE;
8265 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8267 final &= ~DREF_SSC_SOURCE_MASK;
8268 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8269 final &= ~DREF_SSC1_ENABLE;
8272 final |= DREF_SSC_SOURCE_ENABLE;
8274 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8275 final |= DREF_SSC1_ENABLE;
8278 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8279 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8281 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8283 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8285 final |= DREF_SSC_SOURCE_DISABLE;
8286 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8292 /* Always enable nonspread source */
8293 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8296 val |= DREF_NONSPREAD_CK505_ENABLE;
8298 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8301 val &= ~DREF_SSC_SOURCE_MASK;
8302 val |= DREF_SSC_SOURCE_ENABLE;
8304 /* SSC must be turned on before enabling the CPU output */
8305 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
8306 DRM_DEBUG_KMS("Using SSC on panel\n");
8307 val |= DREF_SSC1_ENABLE;
8309 val &= ~DREF_SSC1_ENABLE;
8311 /* Get SSC going before enabling the outputs */
8312 I915_WRITE(PCH_DREF_CONTROL, val);
8313 POSTING_READ(PCH_DREF_CONTROL);
8316 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8318 /* Enable CPU source on CPU attached eDP */
8320 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
8321 DRM_DEBUG_KMS("Using SSC on eDP\n");
8322 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8324 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8326 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8328 I915_WRITE(PCH_DREF_CONTROL, val);
8329 POSTING_READ(PCH_DREF_CONTROL);
8332 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8334 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8336 /* Turn off CPU output */
8337 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8339 I915_WRITE(PCH_DREF_CONTROL, val);
8340 POSTING_READ(PCH_DREF_CONTROL);
8343 /* Turn off the SSC source */
8344 val &= ~DREF_SSC_SOURCE_MASK;
8345 val |= DREF_SSC_SOURCE_DISABLE;
8348 val &= ~DREF_SSC1_ENABLE;
8350 I915_WRITE(PCH_DREF_CONTROL, val);
8351 POSTING_READ(PCH_DREF_CONTROL);
8355 BUG_ON(val != final);
8358 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
8362 tmp = I915_READ(SOUTH_CHICKEN2);
8363 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8364 I915_WRITE(SOUTH_CHICKEN2, tmp);
8366 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8367 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8368 DRM_ERROR("FDI mPHY reset assert timeout\n");
8370 tmp = I915_READ(SOUTH_CHICKEN2);
8371 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8372 I915_WRITE(SOUTH_CHICKEN2, tmp);
8374 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8375 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8376 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
8379 /* WaMPhyProgramming:hsw */
8380 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8384 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8385 tmp &= ~(0xFF << 24);
8386 tmp |= (0x12 << 24);
8387 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8389 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8391 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8393 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8395 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8397 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8398 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8399 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8401 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8402 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8403 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8405 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8408 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
8410 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8413 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
8415 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8418 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8420 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8423 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8425 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8426 tmp &= ~(0xFF << 16);
8427 tmp |= (0x1C << 16);
8428 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8430 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8431 tmp &= ~(0xFF << 16);
8432 tmp |= (0x1C << 16);
8433 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8435 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8437 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
8439 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8441 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
8443 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8444 tmp &= ~(0xF << 28);
8446 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
8448 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8449 tmp &= ~(0xF << 28);
8451 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
8454 /* Implements 3 different sequences from BSpec chapter "Display iCLK
8455 * Programming" based on the parameters passed:
8456 * - Sequence to enable CLKOUT_DP
8457 * - Sequence to enable CLKOUT_DP without spread
8458 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8460 static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8463 struct drm_i915_private *dev_priv = dev->dev_private;
8466 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8468 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8469 with_fdi, "LP PCH doesn't have FDI\n"))
8472 mutex_lock(&dev_priv->sb_lock);
8474 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8475 tmp &= ~SBI_SSCCTL_DISABLE;
8476 tmp |= SBI_SSCCTL_PATHALT;
8477 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8482 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8483 tmp &= ~SBI_SSCCTL_PATHALT;
8484 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8487 lpt_reset_fdi_mphy(dev_priv);
8488 lpt_program_fdi_mphy(dev_priv);
8492 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8493 SBI_GEN0 : SBI_DBUFF0;
8494 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8495 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8496 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8498 mutex_unlock(&dev_priv->sb_lock);
8501 /* Sequence to disable CLKOUT_DP */
8502 static void lpt_disable_clkout_dp(struct drm_device *dev)
8504 struct drm_i915_private *dev_priv = dev->dev_private;
8507 mutex_lock(&dev_priv->sb_lock);
8509 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8510 SBI_GEN0 : SBI_DBUFF0;
8511 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8512 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8513 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8515 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8516 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8517 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8518 tmp |= SBI_SSCCTL_PATHALT;
8519 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8522 tmp |= SBI_SSCCTL_DISABLE;
8523 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8526 mutex_unlock(&dev_priv->sb_lock);
8529 static void lpt_init_pch_refclk(struct drm_device *dev)
8531 struct intel_encoder *encoder;
8532 bool has_vga = false;
8534 for_each_intel_encoder(dev, encoder) {
8535 switch (encoder->type) {
8536 case INTEL_OUTPUT_ANALOG:
8545 lpt_enable_clkout_dp(dev, true, true);
8547 lpt_disable_clkout_dp(dev);
8551 * Initialize reference clocks when the driver loads
8553 void intel_init_pch_refclk(struct drm_device *dev)
8555 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8556 ironlake_init_pch_refclk(dev);
8557 else if (HAS_PCH_LPT(dev))
8558 lpt_init_pch_refclk(dev);
8561 static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
8563 struct drm_device *dev = crtc_state->base.crtc->dev;
8564 struct drm_i915_private *dev_priv = dev->dev_private;
8565 struct drm_atomic_state *state = crtc_state->base.state;
8566 struct drm_connector *connector;
8567 struct drm_connector_state *connector_state;
8568 struct intel_encoder *encoder;
8569 int num_connectors = 0, i;
8570 bool is_lvds = false;
8572 for_each_connector_in_state(state, connector, connector_state, i) {
8573 if (connector_state->crtc != crtc_state->base.crtc)
8576 encoder = to_intel_encoder(connector_state->best_encoder);
8578 switch (encoder->type) {
8579 case INTEL_OUTPUT_LVDS:
8588 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
8589 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
8590 dev_priv->vbt.lvds_ssc_freq);
8591 return dev_priv->vbt.lvds_ssc_freq;
8597 static void ironlake_set_pipeconf(struct drm_crtc *crtc)
8599 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8600 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8601 int pipe = intel_crtc->pipe;
8606 switch (intel_crtc->config->pipe_bpp) {
8608 val |= PIPECONF_6BPC;
8611 val |= PIPECONF_8BPC;
8614 val |= PIPECONF_10BPC;
8617 val |= PIPECONF_12BPC;
8620 /* Case prevented by intel_choose_pipe_bpp_dither. */
8624 if (intel_crtc->config->dither)
8625 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8627 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
8628 val |= PIPECONF_INTERLACED_ILK;
8630 val |= PIPECONF_PROGRESSIVE;
8632 if (intel_crtc->config->limited_color_range)
8633 val |= PIPECONF_COLOR_RANGE_SELECT;
8635 I915_WRITE(PIPECONF(pipe), val);
8636 POSTING_READ(PIPECONF(pipe));
8640 * Set up the pipe CSC unit.
8642 * Currently only full range RGB to limited range RGB conversion
8643 * is supported, but eventually this should handle various
8644 * RGB<->YCbCr scenarios as well.
8646 static void intel_set_pipe_csc(struct drm_crtc *crtc)
8648 struct drm_device *dev = crtc->dev;
8649 struct drm_i915_private *dev_priv = dev->dev_private;
8650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8651 int pipe = intel_crtc->pipe;
8652 uint16_t coeff = 0x7800; /* 1.0 */
8655 * TODO: Check what kind of values actually come out of the pipe
8656 * with these coeff/postoff values and adjust to get the best
8657 * accuracy. Perhaps we even need to take the bpc value into
8661 if (intel_crtc->config->limited_color_range)
8662 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8665 * GY/GU and RY/RU should be the other way around according
8666 * to BSpec, but reality doesn't agree. Just set them up in
8667 * a way that results in the correct picture.
8669 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8670 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8672 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8673 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8675 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8676 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8678 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8679 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8680 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8682 if (INTEL_INFO(dev)->gen > 6) {
8683 uint16_t postoff = 0;
8685 if (intel_crtc->config->limited_color_range)
8686 postoff = (16 * (1 << 12) / 255) & 0x1fff;
8688 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8689 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8690 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8692 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8694 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8696 if (intel_crtc->config->limited_color_range)
8697 mode |= CSC_BLACK_SCREEN_OFFSET;
8699 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8703 static void haswell_set_pipeconf(struct drm_crtc *crtc)
8705 struct drm_device *dev = crtc->dev;
8706 struct drm_i915_private *dev_priv = dev->dev_private;
8707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8708 enum pipe pipe = intel_crtc->pipe;
8709 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8714 if (IS_HASWELL(dev) && intel_crtc->config->dither)
8715 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8717 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
8718 val |= PIPECONF_INTERLACED_ILK;
8720 val |= PIPECONF_PROGRESSIVE;
8722 I915_WRITE(PIPECONF(cpu_transcoder), val);
8723 POSTING_READ(PIPECONF(cpu_transcoder));
8725 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8726 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
8728 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
8731 switch (intel_crtc->config->pipe_bpp) {
8733 val |= PIPEMISC_DITHER_6_BPC;
8736 val |= PIPEMISC_DITHER_8_BPC;
8739 val |= PIPEMISC_DITHER_10_BPC;
8742 val |= PIPEMISC_DITHER_12_BPC;
8745 /* Case prevented by pipe_config_set_bpp. */
8749 if (intel_crtc->config->dither)
8750 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8752 I915_WRITE(PIPEMISC(pipe), val);
8756 static bool ironlake_compute_clocks(struct drm_crtc *crtc,
8757 struct intel_crtc_state *crtc_state,
8758 intel_clock_t *clock,
8759 bool *has_reduced_clock,
8760 intel_clock_t *reduced_clock)
8762 struct drm_device *dev = crtc->dev;
8763 struct drm_i915_private *dev_priv = dev->dev_private;
8765 const intel_limit_t *limit;
8766 bool ret, is_lvds = false;
8768 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
8770 refclk = ironlake_get_refclk(crtc_state);
8773 * Returns a set of divisors for the desired target clock with the given
8774 * refclk, or FALSE. The returned values represent the clock equation:
8775 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8777 limit = intel_limit(crtc_state, refclk);
8778 ret = dev_priv->display.find_dpll(limit, crtc_state,
8779 crtc_state->port_clock,
8780 refclk, NULL, clock);
8784 if (is_lvds && dev_priv->lvds_downclock_avail) {
8786 * Ensure we match the reduced clock's P to the target clock.
8787 * If the clocks don't match, we can't switch the display clock
8788 * by using the FP0/FP1. In such case we will disable the LVDS
8789 * downclock feature.
8791 *has_reduced_clock =
8792 dev_priv->display.find_dpll(limit, crtc_state,
8793 dev_priv->lvds_downclock,
8801 int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8804 * Account for spread spectrum to avoid
8805 * oversubscribing the link. Max center spread
8806 * is 2.5%; use 5% for safety's sake.
8808 u32 bps = target_clock * bpp * 21 / 20;
8809 return DIV_ROUND_UP(bps, link_bw * 8);
8812 static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
8814 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
8817 static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
8818 struct intel_crtc_state *crtc_state,
8820 intel_clock_t *reduced_clock, u32 *fp2)
8822 struct drm_crtc *crtc = &intel_crtc->base;
8823 struct drm_device *dev = crtc->dev;
8824 struct drm_i915_private *dev_priv = dev->dev_private;
8825 struct drm_atomic_state *state = crtc_state->base.state;
8826 struct drm_connector *connector;
8827 struct drm_connector_state *connector_state;
8828 struct intel_encoder *encoder;
8830 int factor, num_connectors = 0, i;
8831 bool is_lvds = false, is_sdvo = false;
8833 for_each_connector_in_state(state, connector, connector_state, i) {
8834 if (connector_state->crtc != crtc_state->base.crtc)
8837 encoder = to_intel_encoder(connector_state->best_encoder);
8839 switch (encoder->type) {
8840 case INTEL_OUTPUT_LVDS:
8843 case INTEL_OUTPUT_SDVO:
8844 case INTEL_OUTPUT_HDMI:
8854 /* Enable autotuning of the PLL clock (if permissible) */
8857 if ((intel_panel_use_ssc(dev_priv) &&
8858 dev_priv->vbt.lvds_ssc_freq == 100000) ||
8859 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8861 } else if (crtc_state->sdvo_tv_clock)
8864 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
8867 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8873 dpll |= DPLLB_MODE_LVDS;
8875 dpll |= DPLLB_MODE_DAC_SERIAL;
8877 dpll |= (crtc_state->pixel_multiplier - 1)
8878 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
8881 dpll |= DPLL_SDVO_HIGH_SPEED;
8882 if (crtc_state->has_dp_encoder)
8883 dpll |= DPLL_SDVO_HIGH_SPEED;
8885 /* compute bitmask from p1 value */
8886 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
8888 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
8890 switch (crtc_state->dpll.p2) {
8892 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8895 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8898 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8901 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8905 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
8906 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
8908 dpll |= PLL_REF_INPUT_DREFCLK;
8910 return dpll | DPLL_VCO_ENABLE;
8913 static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8914 struct intel_crtc_state *crtc_state)
8916 struct drm_device *dev = crtc->base.dev;
8917 intel_clock_t clock, reduced_clock;
8918 u32 dpll = 0, fp = 0, fp2 = 0;
8919 bool ok, has_reduced_clock = false;
8920 bool is_lvds = false;
8921 struct intel_shared_dpll *pll;
8923 memset(&crtc_state->dpll_hw_state, 0,
8924 sizeof(crtc_state->dpll_hw_state));
8926 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
8928 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8929 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8931 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
8932 &has_reduced_clock, &reduced_clock);
8933 if (!ok && !crtc_state->clock_set) {
8934 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8937 /* Compat-code for transition, will disappear. */
8938 if (!crtc_state->clock_set) {
8939 crtc_state->dpll.n = clock.n;
8940 crtc_state->dpll.m1 = clock.m1;
8941 crtc_state->dpll.m2 = clock.m2;
8942 crtc_state->dpll.p1 = clock.p1;
8943 crtc_state->dpll.p2 = clock.p2;
8946 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8947 if (crtc_state->has_pch_encoder) {
8948 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
8949 if (has_reduced_clock)
8950 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
8952 dpll = ironlake_compute_dpll(crtc, crtc_state,
8953 &fp, &reduced_clock,
8954 has_reduced_clock ? &fp2 : NULL);
8956 crtc_state->dpll_hw_state.dpll = dpll;
8957 crtc_state->dpll_hw_state.fp0 = fp;
8958 if (has_reduced_clock)
8959 crtc_state->dpll_hw_state.fp1 = fp2;
8961 crtc_state->dpll_hw_state.fp1 = fp;
8963 pll = intel_get_shared_dpll(crtc, crtc_state);
8965 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
8966 pipe_name(crtc->pipe));
8971 if (is_lvds && has_reduced_clock)
8972 crtc->lowfreq_avail = true;
8974 crtc->lowfreq_avail = false;
8979 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8980 struct intel_link_m_n *m_n)
8982 struct drm_device *dev = crtc->base.dev;
8983 struct drm_i915_private *dev_priv = dev->dev_private;
8984 enum pipe pipe = crtc->pipe;
8986 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8987 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8988 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8990 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8991 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8992 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8995 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8996 enum transcoder transcoder,
8997 struct intel_link_m_n *m_n,
8998 struct intel_link_m_n *m2_n2)
9000 struct drm_device *dev = crtc->base.dev;
9001 struct drm_i915_private *dev_priv = dev->dev_private;
9002 enum pipe pipe = crtc->pipe;
9004 if (INTEL_INFO(dev)->gen >= 5) {
9005 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9006 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9007 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9009 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9010 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9011 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9012 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9013 * gen < 8) and if DRRS is supported (to make sure the
9014 * registers are not unnecessarily read).
9016 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
9017 crtc->config->has_drrs) {
9018 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9019 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9020 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9022 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9023 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9024 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9027 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9028 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9029 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9031 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9032 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9033 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9037 void intel_dp_get_m_n(struct intel_crtc *crtc,
9038 struct intel_crtc_state *pipe_config)
9040 if (pipe_config->has_pch_encoder)
9041 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9043 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
9044 &pipe_config->dp_m_n,
9045 &pipe_config->dp_m2_n2);
9048 static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
9049 struct intel_crtc_state *pipe_config)
9051 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
9052 &pipe_config->fdi_m_n, NULL);
9055 static void skylake_get_pfit_config(struct intel_crtc *crtc,
9056 struct intel_crtc_state *pipe_config)
9058 struct drm_device *dev = crtc->base.dev;
9059 struct drm_i915_private *dev_priv = dev->dev_private;
9060 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9061 uint32_t ps_ctrl = 0;
9065 /* find scaler attached to this pipe */
9066 for (i = 0; i < crtc->num_scalers; i++) {
9067 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9068 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9070 pipe_config->pch_pfit.enabled = true;
9071 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9072 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9077 scaler_state->scaler_id = id;
9079 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9081 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
9086 skylake_get_initial_plane_config(struct intel_crtc *crtc,
9087 struct intel_initial_plane_config *plane_config)
9089 struct drm_device *dev = crtc->base.dev;
9090 struct drm_i915_private *dev_priv = dev->dev_private;
9091 u32 val, base, offset, stride_mult, tiling;
9092 int pipe = crtc->pipe;
9093 int fourcc, pixel_format;
9094 unsigned int aligned_height;
9095 struct drm_framebuffer *fb;
9096 struct intel_framebuffer *intel_fb;
9098 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9100 DRM_DEBUG_KMS("failed to alloc fb\n");
9104 fb = &intel_fb->base;
9106 val = I915_READ(PLANE_CTL(pipe, 0));
9107 if (!(val & PLANE_CTL_ENABLE))
9110 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9111 fourcc = skl_format_to_fourcc(pixel_format,
9112 val & PLANE_CTL_ORDER_RGBX,
9113 val & PLANE_CTL_ALPHA_MASK);
9114 fb->pixel_format = fourcc;
9115 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9117 tiling = val & PLANE_CTL_TILED_MASK;
9119 case PLANE_CTL_TILED_LINEAR:
9120 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9122 case PLANE_CTL_TILED_X:
9123 plane_config->tiling = I915_TILING_X;
9124 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9126 case PLANE_CTL_TILED_Y:
9127 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9129 case PLANE_CTL_TILED_YF:
9130 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9133 MISSING_CASE(tiling);
9137 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9138 plane_config->base = base;
9140 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9142 val = I915_READ(PLANE_SIZE(pipe, 0));
9143 fb->height = ((val >> 16) & 0xfff) + 1;
9144 fb->width = ((val >> 0) & 0x1fff) + 1;
9146 val = I915_READ(PLANE_STRIDE(pipe, 0));
9147 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9149 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9151 aligned_height = intel_fb_align_height(dev, fb->height,
9155 plane_config->size = fb->pitches[0] * aligned_height;
9157 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9158 pipe_name(pipe), fb->width, fb->height,
9159 fb->bits_per_pixel, base, fb->pitches[0],
9160 plane_config->size);
9162 plane_config->fb = intel_fb;
9169 static void ironlake_get_pfit_config(struct intel_crtc *crtc,
9170 struct intel_crtc_state *pipe_config)
9172 struct drm_device *dev = crtc->base.dev;
9173 struct drm_i915_private *dev_priv = dev->dev_private;
9176 tmp = I915_READ(PF_CTL(crtc->pipe));
9178 if (tmp & PF_ENABLE) {
9179 pipe_config->pch_pfit.enabled = true;
9180 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9181 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
9183 /* We currently do not free assignements of panel fitters on
9184 * ivb/hsw (since we don't use the higher upscaling modes which
9185 * differentiates them) so just WARN about this case for now. */
9187 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9188 PF_PIPE_SEL_IVB(crtc->pipe));
9194 ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9195 struct intel_initial_plane_config *plane_config)
9197 struct drm_device *dev = crtc->base.dev;
9198 struct drm_i915_private *dev_priv = dev->dev_private;
9199 u32 val, base, offset;
9200 int pipe = crtc->pipe;
9201 int fourcc, pixel_format;
9202 unsigned int aligned_height;
9203 struct drm_framebuffer *fb;
9204 struct intel_framebuffer *intel_fb;
9206 val = I915_READ(DSPCNTR(pipe));
9207 if (!(val & DISPLAY_PLANE_ENABLE))
9210 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9212 DRM_DEBUG_KMS("failed to alloc fb\n");
9216 fb = &intel_fb->base;
9218 if (INTEL_INFO(dev)->gen >= 4) {
9219 if (val & DISPPLANE_TILED) {
9220 plane_config->tiling = I915_TILING_X;
9221 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9225 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
9226 fourcc = i9xx_format_to_fourcc(pixel_format);
9227 fb->pixel_format = fourcc;
9228 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9230 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
9231 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
9232 offset = I915_READ(DSPOFFSET(pipe));
9234 if (plane_config->tiling)
9235 offset = I915_READ(DSPTILEOFF(pipe));
9237 offset = I915_READ(DSPLINOFF(pipe));
9239 plane_config->base = base;
9241 val = I915_READ(PIPESRC(pipe));
9242 fb->width = ((val >> 16) & 0xfff) + 1;
9243 fb->height = ((val >> 0) & 0xfff) + 1;
9245 val = I915_READ(DSPSTRIDE(pipe));
9246 fb->pitches[0] = val & 0xffffffc0;
9248 aligned_height = intel_fb_align_height(dev, fb->height,
9252 plane_config->size = fb->pitches[0] * aligned_height;
9254 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9255 pipe_name(pipe), fb->width, fb->height,
9256 fb->bits_per_pixel, base, fb->pitches[0],
9257 plane_config->size);
9259 plane_config->fb = intel_fb;
9262 static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
9263 struct intel_crtc_state *pipe_config)
9265 struct drm_device *dev = crtc->base.dev;
9266 struct drm_i915_private *dev_priv = dev->dev_private;
9269 if (!intel_display_power_is_enabled(dev_priv,
9270 POWER_DOMAIN_PIPE(crtc->pipe)))
9273 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9274 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9276 tmp = I915_READ(PIPECONF(crtc->pipe));
9277 if (!(tmp & PIPECONF_ENABLE))
9280 switch (tmp & PIPECONF_BPC_MASK) {
9282 pipe_config->pipe_bpp = 18;
9285 pipe_config->pipe_bpp = 24;
9287 case PIPECONF_10BPC:
9288 pipe_config->pipe_bpp = 30;
9290 case PIPECONF_12BPC:
9291 pipe_config->pipe_bpp = 36;
9297 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9298 pipe_config->limited_color_range = true;
9300 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
9301 struct intel_shared_dpll *pll;
9303 pipe_config->has_pch_encoder = true;
9305 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9306 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9307 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9309 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9311 if (HAS_PCH_IBX(dev_priv->dev)) {
9312 pipe_config->shared_dpll =
9313 (enum intel_dpll_id) crtc->pipe;
9315 tmp = I915_READ(PCH_DPLL_SEL);
9316 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9317 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9319 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9322 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9324 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9325 &pipe_config->dpll_hw_state));
9327 tmp = pipe_config->dpll_hw_state.dpll;
9328 pipe_config->pixel_multiplier =
9329 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9330 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
9332 ironlake_pch_clock_get(crtc, pipe_config);
9334 pipe_config->pixel_multiplier = 1;
9337 intel_get_pipe_timings(crtc, pipe_config);
9339 ironlake_get_pfit_config(crtc, pipe_config);
9344 static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9346 struct drm_device *dev = dev_priv->dev;
9347 struct intel_crtc *crtc;
9349 for_each_intel_crtc(dev, crtc)
9350 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
9351 pipe_name(crtc->pipe));
9353 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9354 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9355 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9356 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9357 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9358 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
9359 "CPU PWM1 enabled\n");
9360 if (IS_HASWELL(dev))
9361 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
9362 "CPU PWM2 enabled\n");
9363 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
9364 "PCH PWM1 enabled\n");
9365 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
9366 "Utility pin enabled\n");
9367 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
9370 * In theory we can still leave IRQs enabled, as long as only the HPD
9371 * interrupts remain enabled. We used to check for that, but since it's
9372 * gen-specific and since we only disable LCPLL after we fully disable
9373 * the interrupts, the check below should be enough.
9375 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
9378 static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9380 struct drm_device *dev = dev_priv->dev;
9382 if (IS_HASWELL(dev))
9383 return I915_READ(D_COMP_HSW);
9385 return I915_READ(D_COMP_BDW);
9388 static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9390 struct drm_device *dev = dev_priv->dev;
9392 if (IS_HASWELL(dev)) {
9393 mutex_lock(&dev_priv->rps.hw_lock);
9394 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9396 DRM_ERROR("Failed to write to D_COMP\n");
9397 mutex_unlock(&dev_priv->rps.hw_lock);
9399 I915_WRITE(D_COMP_BDW, val);
9400 POSTING_READ(D_COMP_BDW);
9405 * This function implements pieces of two sequences from BSpec:
9406 * - Sequence for display software to disable LCPLL
9407 * - Sequence for display software to allow package C8+
9408 * The steps implemented here are just the steps that actually touch the LCPLL
9409 * register. Callers should take care of disabling all the display engine
9410 * functions, doing the mode unset, fixing interrupts, etc.
9412 static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9413 bool switch_to_fclk, bool allow_power_down)
9417 assert_can_disable_lcpll(dev_priv);
9419 val = I915_READ(LCPLL_CTL);
9421 if (switch_to_fclk) {
9422 val |= LCPLL_CD_SOURCE_FCLK;
9423 I915_WRITE(LCPLL_CTL, val);
9425 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9426 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9427 DRM_ERROR("Switching to FCLK failed\n");
9429 val = I915_READ(LCPLL_CTL);
9432 val |= LCPLL_PLL_DISABLE;
9433 I915_WRITE(LCPLL_CTL, val);
9434 POSTING_READ(LCPLL_CTL);
9436 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9437 DRM_ERROR("LCPLL still locked\n");
9439 val = hsw_read_dcomp(dev_priv);
9440 val |= D_COMP_COMP_DISABLE;
9441 hsw_write_dcomp(dev_priv, val);
9444 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9446 DRM_ERROR("D_COMP RCOMP still in progress\n");
9448 if (allow_power_down) {
9449 val = I915_READ(LCPLL_CTL);
9450 val |= LCPLL_POWER_DOWN_ALLOW;
9451 I915_WRITE(LCPLL_CTL, val);
9452 POSTING_READ(LCPLL_CTL);
9457 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9460 static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
9464 val = I915_READ(LCPLL_CTL);
9466 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9467 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9471 * Make sure we're not on PC8 state before disabling PC8, otherwise
9472 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
9474 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
9476 if (val & LCPLL_POWER_DOWN_ALLOW) {
9477 val &= ~LCPLL_POWER_DOWN_ALLOW;
9478 I915_WRITE(LCPLL_CTL, val);
9479 POSTING_READ(LCPLL_CTL);
9482 val = hsw_read_dcomp(dev_priv);
9483 val |= D_COMP_COMP_FORCE;
9484 val &= ~D_COMP_COMP_DISABLE;
9485 hsw_write_dcomp(dev_priv, val);
9487 val = I915_READ(LCPLL_CTL);
9488 val &= ~LCPLL_PLL_DISABLE;
9489 I915_WRITE(LCPLL_CTL, val);
9491 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9492 DRM_ERROR("LCPLL not locked yet\n");
9494 if (val & LCPLL_CD_SOURCE_FCLK) {
9495 val = I915_READ(LCPLL_CTL);
9496 val &= ~LCPLL_CD_SOURCE_FCLK;
9497 I915_WRITE(LCPLL_CTL, val);
9499 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9500 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9501 DRM_ERROR("Switching back to LCPLL failed\n");
9504 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
9505 intel_update_cdclk(dev_priv->dev);
9509 * Package states C8 and deeper are really deep PC states that can only be
9510 * reached when all the devices on the system allow it, so even if the graphics
9511 * device allows PC8+, it doesn't mean the system will actually get to these
9512 * states. Our driver only allows PC8+ when going into runtime PM.
9514 * The requirements for PC8+ are that all the outputs are disabled, the power
9515 * well is disabled and most interrupts are disabled, and these are also
9516 * requirements for runtime PM. When these conditions are met, we manually do
9517 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9518 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9521 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9522 * the state of some registers, so when we come back from PC8+ we need to
9523 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9524 * need to take care of the registers kept by RC6. Notice that this happens even
9525 * if we don't put the device in PCI D3 state (which is what currently happens
9526 * because of the runtime PM support).
9528 * For more, read "Display Sequences for Package C8" on the hardware
9531 void hsw_enable_pc8(struct drm_i915_private *dev_priv)
9533 struct drm_device *dev = dev_priv->dev;
9536 DRM_DEBUG_KMS("Enabling package C8+\n");
9538 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9539 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9540 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9541 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9544 lpt_disable_clkout_dp(dev);
9545 hsw_disable_lcpll(dev_priv, true, true);
9548 void hsw_disable_pc8(struct drm_i915_private *dev_priv)
9550 struct drm_device *dev = dev_priv->dev;
9553 DRM_DEBUG_KMS("Disabling package C8+\n");
9555 hsw_restore_lcpll(dev_priv);
9556 lpt_init_pch_refclk(dev);
9558 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9559 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9560 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9561 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9564 intel_prepare_ddi(dev);
9567 static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
9569 struct drm_device *dev = old_state->dev;
9570 struct drm_i915_private *dev_priv = dev->dev_private;
9571 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
9574 /* see the comment in valleyview_modeset_global_resources */
9575 if (WARN_ON(max_pixclk < 0))
9578 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9580 if (req_cdclk != dev_priv->cdclk_freq)
9581 broxton_set_cdclk(dev, req_cdclk);
9584 /* compute the max rate for new configuration */
9585 static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9587 struct drm_device *dev = dev_priv->dev;
9588 struct intel_crtc *intel_crtc;
9589 struct drm_crtc *crtc;
9590 int max_pixel_rate = 0;
9593 for_each_crtc(dev, crtc) {
9594 if (!crtc->state->enable)
9597 intel_crtc = to_intel_crtc(crtc);
9598 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9600 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9601 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9602 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9604 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9607 return max_pixel_rate;
9610 static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9612 struct drm_i915_private *dev_priv = dev->dev_private;
9616 if (WARN((I915_READ(LCPLL_CTL) &
9617 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9618 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9619 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9620 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9621 "trying to change cdclk frequency with cdclk not enabled\n"))
9624 mutex_lock(&dev_priv->rps.hw_lock);
9625 ret = sandybridge_pcode_write(dev_priv,
9626 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9627 mutex_unlock(&dev_priv->rps.hw_lock);
9629 DRM_ERROR("failed to inform pcode about cdclk change\n");
9633 val = I915_READ(LCPLL_CTL);
9634 val |= LCPLL_CD_SOURCE_FCLK;
9635 I915_WRITE(LCPLL_CTL, val);
9637 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9638 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9639 DRM_ERROR("Switching to FCLK failed\n");
9641 val = I915_READ(LCPLL_CTL);
9642 val &= ~LCPLL_CLK_FREQ_MASK;
9646 val |= LCPLL_CLK_FREQ_450;
9650 val |= LCPLL_CLK_FREQ_54O_BDW;
9654 val |= LCPLL_CLK_FREQ_337_5_BDW;
9658 val |= LCPLL_CLK_FREQ_675_BDW;
9662 WARN(1, "invalid cdclk frequency\n");
9666 I915_WRITE(LCPLL_CTL, val);
9668 val = I915_READ(LCPLL_CTL);
9669 val &= ~LCPLL_CD_SOURCE_FCLK;
9670 I915_WRITE(LCPLL_CTL, val);
9672 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9673 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9674 DRM_ERROR("Switching back to LCPLL failed\n");
9676 mutex_lock(&dev_priv->rps.hw_lock);
9677 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9678 mutex_unlock(&dev_priv->rps.hw_lock);
9680 intel_update_cdclk(dev);
9682 WARN(cdclk != dev_priv->cdclk_freq,
9683 "cdclk requested %d kHz but got %d kHz\n",
9684 cdclk, dev_priv->cdclk_freq);
9687 static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9693 * FIXME should also account for plane ratio
9694 * once 64bpp pixel formats are supported.
9696 if (max_pixel_rate > 540000)
9698 else if (max_pixel_rate > 450000)
9700 else if (max_pixel_rate > 337500)
9706 * FIXME move the cdclk caclulation to
9707 * compute_config() so we can fail gracegully.
9709 if (cdclk > dev_priv->max_cdclk_freq) {
9710 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9711 cdclk, dev_priv->max_cdclk_freq);
9712 cdclk = dev_priv->max_cdclk_freq;
9718 static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9720 struct drm_i915_private *dev_priv = to_i915(state->dev);
9721 struct drm_crtc *crtc;
9722 struct drm_crtc_state *crtc_state;
9723 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9726 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9728 if (cdclk == dev_priv->cdclk_freq)
9731 /* add all active pipes to the state */
9732 for_each_crtc(state->dev, crtc) {
9733 if (!crtc->state->enable)
9736 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9737 if (IS_ERR(crtc_state))
9738 return PTR_ERR(crtc_state);
9741 /* disable/enable all currently active pipes while we change cdclk */
9742 for_each_crtc_in_state(state, crtc, crtc_state, i)
9743 if (crtc_state->enable)
9744 crtc_state->mode_changed = true;
9749 static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9751 struct drm_device *dev = state->dev;
9752 struct drm_i915_private *dev_priv = dev->dev_private;
9753 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9754 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9756 if (req_cdclk != dev_priv->cdclk_freq)
9757 broadwell_set_cdclk(dev, req_cdclk);
9760 static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9761 struct intel_crtc_state *crtc_state)
9763 if (!intel_ddi_pll_select(crtc, crtc_state))
9766 crtc->lowfreq_avail = false;
9771 static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9773 struct intel_crtc_state *pipe_config)
9777 pipe_config->ddi_pll_sel = SKL_DPLL0;
9778 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9781 pipe_config->ddi_pll_sel = SKL_DPLL1;
9782 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9785 pipe_config->ddi_pll_sel = SKL_DPLL2;
9786 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9789 DRM_ERROR("Incorrect port type\n");
9793 static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9795 struct intel_crtc_state *pipe_config)
9797 u32 temp, dpll_ctl1;
9799 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9800 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9802 switch (pipe_config->ddi_pll_sel) {
9805 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9806 * of the shared DPLL framework and thus needs to be read out
9809 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9810 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9813 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9816 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9819 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9824 static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9826 struct intel_crtc_state *pipe_config)
9828 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9830 switch (pipe_config->ddi_pll_sel) {
9831 case PORT_CLK_SEL_WRPLL1:
9832 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9834 case PORT_CLK_SEL_WRPLL2:
9835 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9840 static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
9841 struct intel_crtc_state *pipe_config)
9843 struct drm_device *dev = crtc->base.dev;
9844 struct drm_i915_private *dev_priv = dev->dev_private;
9845 struct intel_shared_dpll *pll;
9849 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9851 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9853 if (IS_SKYLAKE(dev))
9854 skylake_get_ddi_pll(dev_priv, port, pipe_config);
9855 else if (IS_BROXTON(dev))
9856 bxt_get_ddi_pll(dev_priv, port, pipe_config);
9858 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9860 if (pipe_config->shared_dpll >= 0) {
9861 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9863 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9864 &pipe_config->dpll_hw_state));
9868 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9869 * DDI E. So just check whether this pipe is wired to DDI E and whether
9870 * the PCH transcoder is on.
9872 if (INTEL_INFO(dev)->gen < 9 &&
9873 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
9874 pipe_config->has_pch_encoder = true;
9876 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9877 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9878 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9880 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9884 static bool haswell_get_pipe_config(struct intel_crtc *crtc,
9885 struct intel_crtc_state *pipe_config)
9887 struct drm_device *dev = crtc->base.dev;
9888 struct drm_i915_private *dev_priv = dev->dev_private;
9889 enum intel_display_power_domain pfit_domain;
9892 if (!intel_display_power_is_enabled(dev_priv,
9893 POWER_DOMAIN_PIPE(crtc->pipe)))
9896 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
9897 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9899 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9900 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9901 enum pipe trans_edp_pipe;
9902 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9904 WARN(1, "unknown pipe linked to edp transcoder\n");
9905 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9906 case TRANS_DDI_EDP_INPUT_A_ON:
9907 trans_edp_pipe = PIPE_A;
9909 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9910 trans_edp_pipe = PIPE_B;
9912 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9913 trans_edp_pipe = PIPE_C;
9917 if (trans_edp_pipe == crtc->pipe)
9918 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9921 if (!intel_display_power_is_enabled(dev_priv,
9922 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
9925 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
9926 if (!(tmp & PIPECONF_ENABLE))
9929 haswell_get_ddi_port_state(crtc, pipe_config);
9931 intel_get_pipe_timings(crtc, pipe_config);
9933 if (INTEL_INFO(dev)->gen >= 9) {
9934 skl_init_scalers(dev, crtc, pipe_config);
9937 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
9939 if (INTEL_INFO(dev)->gen >= 9) {
9940 pipe_config->scaler_state.scaler_id = -1;
9941 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9944 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
9945 if (INTEL_INFO(dev)->gen == 9)
9946 skylake_get_pfit_config(crtc, pipe_config);
9947 else if (INTEL_INFO(dev)->gen < 9)
9948 ironlake_get_pfit_config(crtc, pipe_config);
9950 MISSING_CASE(INTEL_INFO(dev)->gen);
9953 if (IS_HASWELL(dev))
9954 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9955 (I915_READ(IPS_CTL) & IPS_ENABLE);
9957 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9958 pipe_config->pixel_multiplier =
9959 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9961 pipe_config->pixel_multiplier = 1;
9967 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9969 struct drm_device *dev = crtc->dev;
9970 struct drm_i915_private *dev_priv = dev->dev_private;
9971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9972 uint32_t cntl = 0, size = 0;
9975 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9976 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
9977 unsigned int stride = roundup_pow_of_two(width) * 4;
9981 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9992 cntl |= CURSOR_ENABLE |
9993 CURSOR_GAMMA_ENABLE |
9994 CURSOR_FORMAT_ARGB |
9995 CURSOR_STRIDE(stride);
9997 size = (height << 12) | width;
10000 if (intel_crtc->cursor_cntl != 0 &&
10001 (intel_crtc->cursor_base != base ||
10002 intel_crtc->cursor_size != size ||
10003 intel_crtc->cursor_cntl != cntl)) {
10004 /* On these chipsets we can only modify the base/size/stride
10005 * whilst the cursor is disabled.
10007 I915_WRITE(_CURACNTR, 0);
10008 POSTING_READ(_CURACNTR);
10009 intel_crtc->cursor_cntl = 0;
10012 if (intel_crtc->cursor_base != base) {
10013 I915_WRITE(_CURABASE, base);
10014 intel_crtc->cursor_base = base;
10017 if (intel_crtc->cursor_size != size) {
10018 I915_WRITE(CURSIZE, size);
10019 intel_crtc->cursor_size = size;
10022 if (intel_crtc->cursor_cntl != cntl) {
10023 I915_WRITE(_CURACNTR, cntl);
10024 POSTING_READ(_CURACNTR);
10025 intel_crtc->cursor_cntl = cntl;
10029 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
10031 struct drm_device *dev = crtc->dev;
10032 struct drm_i915_private *dev_priv = dev->dev_private;
10033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10034 int pipe = intel_crtc->pipe;
10039 cntl = MCURSOR_GAMMA_ENABLE;
10040 switch (intel_crtc->base.cursor->state->crtc_w) {
10042 cntl |= CURSOR_MODE_64_ARGB_AX;
10045 cntl |= CURSOR_MODE_128_ARGB_AX;
10048 cntl |= CURSOR_MODE_256_ARGB_AX;
10051 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
10054 cntl |= pipe << 28; /* Connect to correct pipe */
10056 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10057 cntl |= CURSOR_PIPE_CSC_ENABLE;
10060 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
10061 cntl |= CURSOR_ROTATE_180;
10063 if (intel_crtc->cursor_cntl != cntl) {
10064 I915_WRITE(CURCNTR(pipe), cntl);
10065 POSTING_READ(CURCNTR(pipe));
10066 intel_crtc->cursor_cntl = cntl;
10069 /* and commit changes on next vblank */
10070 I915_WRITE(CURBASE(pipe), base);
10071 POSTING_READ(CURBASE(pipe));
10073 intel_crtc->cursor_base = base;
10076 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
10077 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10080 struct drm_device *dev = crtc->dev;
10081 struct drm_i915_private *dev_priv = dev->dev_private;
10082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10083 int pipe = intel_crtc->pipe;
10084 int x = crtc->cursor_x;
10085 int y = crtc->cursor_y;
10086 u32 base = 0, pos = 0;
10089 base = intel_crtc->cursor_addr;
10091 if (x >= intel_crtc->config->pipe_src_w)
10094 if (y >= intel_crtc->config->pipe_src_h)
10098 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
10101 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10104 pos |= x << CURSOR_X_SHIFT;
10107 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
10110 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10113 pos |= y << CURSOR_Y_SHIFT;
10115 if (base == 0 && intel_crtc->cursor_base == 0)
10118 I915_WRITE(CURPOS(pipe), pos);
10120 /* ILK+ do this automagically */
10121 if (HAS_GMCH_DISPLAY(dev) &&
10122 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
10123 base += (intel_crtc->base.cursor->state->crtc_h *
10124 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
10127 if (IS_845G(dev) || IS_I865G(dev))
10128 i845_update_cursor(crtc, base);
10130 i9xx_update_cursor(crtc, base);
10133 static bool cursor_size_ok(struct drm_device *dev,
10134 uint32_t width, uint32_t height)
10136 if (width == 0 || height == 0)
10140 * 845g/865g are special in that they are only limited by
10141 * the width of their cursors, the height is arbitrary up to
10142 * the precision of the register. Everything else requires
10143 * square cursors, limited to a few power-of-two sizes.
10145 if (IS_845G(dev) || IS_I865G(dev)) {
10146 if ((width & 63) != 0)
10149 if (width > (IS_845G(dev) ? 64 : 512))
10155 switch (width | height) {
10170 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
10171 u16 *blue, uint32_t start, uint32_t size)
10173 int end = (start + size > 256) ? 256 : start + size, i;
10174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10176 for (i = start; i < end; i++) {
10177 intel_crtc->lut_r[i] = red[i] >> 8;
10178 intel_crtc->lut_g[i] = green[i] >> 8;
10179 intel_crtc->lut_b[i] = blue[i] >> 8;
10182 intel_crtc_load_lut(crtc);
10185 /* VESA 640x480x72Hz mode to set on the pipe */
10186 static struct drm_display_mode load_detect_mode = {
10187 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10188 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10191 struct drm_framebuffer *
10192 __intel_framebuffer_create(struct drm_device *dev,
10193 struct drm_mode_fb_cmd2 *mode_cmd,
10194 struct drm_i915_gem_object *obj)
10196 struct intel_framebuffer *intel_fb;
10199 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10201 drm_gem_object_unreference(&obj->base);
10202 return ERR_PTR(-ENOMEM);
10205 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
10209 return &intel_fb->base;
10211 drm_gem_object_unreference(&obj->base);
10214 return ERR_PTR(ret);
10217 static struct drm_framebuffer *
10218 intel_framebuffer_create(struct drm_device *dev,
10219 struct drm_mode_fb_cmd2 *mode_cmd,
10220 struct drm_i915_gem_object *obj)
10222 struct drm_framebuffer *fb;
10225 ret = i915_mutex_lock_interruptible(dev);
10227 return ERR_PTR(ret);
10228 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10229 mutex_unlock(&dev->struct_mutex);
10235 intel_framebuffer_pitch_for_width(int width, int bpp)
10237 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10238 return ALIGN(pitch, 64);
10242 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10244 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
10245 return PAGE_ALIGN(pitch * mode->vdisplay);
10248 static struct drm_framebuffer *
10249 intel_framebuffer_create_for_mode(struct drm_device *dev,
10250 struct drm_display_mode *mode,
10251 int depth, int bpp)
10253 struct drm_i915_gem_object *obj;
10254 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
10256 obj = i915_gem_alloc_object(dev,
10257 intel_framebuffer_size_for_mode(mode, bpp));
10259 return ERR_PTR(-ENOMEM);
10261 mode_cmd.width = mode->hdisplay;
10262 mode_cmd.height = mode->vdisplay;
10263 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10265 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
10267 return intel_framebuffer_create(dev, &mode_cmd, obj);
10270 static struct drm_framebuffer *
10271 mode_fits_in_fbdev(struct drm_device *dev,
10272 struct drm_display_mode *mode)
10274 #ifdef CONFIG_DRM_I915_FBDEV
10275 struct drm_i915_private *dev_priv = dev->dev_private;
10276 struct drm_i915_gem_object *obj;
10277 struct drm_framebuffer *fb;
10279 if (!dev_priv->fbdev)
10282 if (!dev_priv->fbdev->fb)
10285 obj = dev_priv->fbdev->fb->obj;
10288 fb = &dev_priv->fbdev->fb->base;
10289 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10290 fb->bits_per_pixel))
10293 if (obj->base.size < mode->vdisplay * fb->pitches[0])
10302 static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10303 struct drm_crtc *crtc,
10304 struct drm_display_mode *mode,
10305 struct drm_framebuffer *fb,
10308 struct drm_plane_state *plane_state;
10309 int hdisplay, vdisplay;
10312 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10313 if (IS_ERR(plane_state))
10314 return PTR_ERR(plane_state);
10317 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10319 hdisplay = vdisplay = 0;
10321 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10324 drm_atomic_set_fb_for_plane(plane_state, fb);
10325 plane_state->crtc_x = 0;
10326 plane_state->crtc_y = 0;
10327 plane_state->crtc_w = hdisplay;
10328 plane_state->crtc_h = vdisplay;
10329 plane_state->src_x = x << 16;
10330 plane_state->src_y = y << 16;
10331 plane_state->src_w = hdisplay << 16;
10332 plane_state->src_h = vdisplay << 16;
10337 bool intel_get_load_detect_pipe(struct drm_connector *connector,
10338 struct drm_display_mode *mode,
10339 struct intel_load_detect_pipe *old,
10340 struct drm_modeset_acquire_ctx *ctx)
10342 struct intel_crtc *intel_crtc;
10343 struct intel_encoder *intel_encoder =
10344 intel_attached_encoder(connector);
10345 struct drm_crtc *possible_crtc;
10346 struct drm_encoder *encoder = &intel_encoder->base;
10347 struct drm_crtc *crtc = NULL;
10348 struct drm_device *dev = encoder->dev;
10349 struct drm_framebuffer *fb;
10350 struct drm_mode_config *config = &dev->mode_config;
10351 struct drm_atomic_state *state = NULL;
10352 struct drm_connector_state *connector_state;
10353 struct intel_crtc_state *crtc_state;
10356 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
10357 connector->base.id, connector->name,
10358 encoder->base.id, encoder->name);
10361 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10366 * Algorithm gets a little messy:
10368 * - if the connector already has an assigned crtc, use it (but make
10369 * sure it's on first)
10371 * - try to find the first unused crtc that can drive this connector,
10372 * and use that if we find one
10375 /* See if we already have a CRTC for this connector */
10376 if (encoder->crtc) {
10377 crtc = encoder->crtc;
10379 ret = drm_modeset_lock(&crtc->mutex, ctx);
10382 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10386 old->dpms_mode = connector->dpms;
10387 old->load_detect_temp = false;
10389 /* Make sure the crtc and connector are running */
10390 if (connector->dpms != DRM_MODE_DPMS_ON)
10391 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
10396 /* Find an unused one (if possible) */
10397 for_each_crtc(dev, possible_crtc) {
10399 if (!(encoder->possible_crtcs & (1 << i)))
10401 if (possible_crtc->state->enable)
10403 /* This can occur when applying the pipe A quirk on resume. */
10404 if (to_intel_crtc(possible_crtc)->new_enabled)
10407 crtc = possible_crtc;
10412 * If we didn't find an unused CRTC, don't use any.
10415 DRM_DEBUG_KMS("no pipe available for load-detect\n");
10419 ret = drm_modeset_lock(&crtc->mutex, ctx);
10422 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10425 intel_encoder->new_crtc = to_intel_crtc(crtc);
10426 to_intel_connector(connector)->new_encoder = intel_encoder;
10428 intel_crtc = to_intel_crtc(crtc);
10429 intel_crtc->new_enabled = true;
10430 old->dpms_mode = connector->dpms;
10431 old->load_detect_temp = true;
10432 old->release_fb = NULL;
10434 state = drm_atomic_state_alloc(dev);
10438 state->acquire_ctx = ctx;
10440 connector_state = drm_atomic_get_connector_state(state, connector);
10441 if (IS_ERR(connector_state)) {
10442 ret = PTR_ERR(connector_state);
10446 connector_state->crtc = crtc;
10447 connector_state->best_encoder = &intel_encoder->base;
10449 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10450 if (IS_ERR(crtc_state)) {
10451 ret = PTR_ERR(crtc_state);
10455 crtc_state->base.active = crtc_state->base.enable = true;
10458 mode = &load_detect_mode;
10460 /* We need a framebuffer large enough to accommodate all accesses
10461 * that the plane may generate whilst we perform load detection.
10462 * We can not rely on the fbcon either being present (we get called
10463 * during its initialisation to detect all boot displays, or it may
10464 * not even exist) or that it is large enough to satisfy the
10467 fb = mode_fits_in_fbdev(dev, mode);
10469 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
10470 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10471 old->release_fb = fb;
10473 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
10475 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
10479 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10483 drm_mode_copy(&crtc_state->base.mode, mode);
10485 if (intel_set_mode(crtc, state)) {
10486 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
10487 if (old->release_fb)
10488 old->release_fb->funcs->destroy(old->release_fb);
10491 crtc->primary->crtc = crtc;
10493 /* let the connector get through one full cycle before testing */
10494 intel_wait_for_vblank(dev, intel_crtc->pipe);
10498 intel_crtc->new_enabled = crtc->state->enable;
10500 drm_atomic_state_free(state);
10503 if (ret == -EDEADLK) {
10504 drm_modeset_backoff(ctx);
10511 void intel_release_load_detect_pipe(struct drm_connector *connector,
10512 struct intel_load_detect_pipe *old,
10513 struct drm_modeset_acquire_ctx *ctx)
10515 struct drm_device *dev = connector->dev;
10516 struct intel_encoder *intel_encoder =
10517 intel_attached_encoder(connector);
10518 struct drm_encoder *encoder = &intel_encoder->base;
10519 struct drm_crtc *crtc = encoder->crtc;
10520 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10521 struct drm_atomic_state *state;
10522 struct drm_connector_state *connector_state;
10523 struct intel_crtc_state *crtc_state;
10526 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
10527 connector->base.id, connector->name,
10528 encoder->base.id, encoder->name);
10530 if (old->load_detect_temp) {
10531 state = drm_atomic_state_alloc(dev);
10535 state->acquire_ctx = ctx;
10537 connector_state = drm_atomic_get_connector_state(state, connector);
10538 if (IS_ERR(connector_state))
10541 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10542 if (IS_ERR(crtc_state))
10545 to_intel_connector(connector)->new_encoder = NULL;
10546 intel_encoder->new_crtc = NULL;
10547 intel_crtc->new_enabled = false;
10549 connector_state->best_encoder = NULL;
10550 connector_state->crtc = NULL;
10552 crtc_state->base.enable = crtc_state->base.active = false;
10554 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10559 ret = intel_set_mode(crtc, state);
10563 if (old->release_fb) {
10564 drm_framebuffer_unregister_private(old->release_fb);
10565 drm_framebuffer_unreference(old->release_fb);
10571 /* Switch crtc and encoder back off if necessary */
10572 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10573 connector->funcs->dpms(connector, old->dpms_mode);
10577 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10578 drm_atomic_state_free(state);
10581 static int i9xx_pll_refclk(struct drm_device *dev,
10582 const struct intel_crtc_state *pipe_config)
10584 struct drm_i915_private *dev_priv = dev->dev_private;
10585 u32 dpll = pipe_config->dpll_hw_state.dpll;
10587 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
10588 return dev_priv->vbt.lvds_ssc_freq;
10589 else if (HAS_PCH_SPLIT(dev))
10591 else if (!IS_GEN2(dev))
10597 /* Returns the clock of the currently programmed mode of the given pipe. */
10598 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
10599 struct intel_crtc_state *pipe_config)
10601 struct drm_device *dev = crtc->base.dev;
10602 struct drm_i915_private *dev_priv = dev->dev_private;
10603 int pipe = pipe_config->cpu_transcoder;
10604 u32 dpll = pipe_config->dpll_hw_state.dpll;
10606 intel_clock_t clock;
10607 int refclk = i9xx_pll_refclk(dev, pipe_config);
10609 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
10610 fp = pipe_config->dpll_hw_state.fp0;
10612 fp = pipe_config->dpll_hw_state.fp1;
10614 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
10615 if (IS_PINEVIEW(dev)) {
10616 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10617 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
10619 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10620 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10623 if (!IS_GEN2(dev)) {
10624 if (IS_PINEVIEW(dev))
10625 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10626 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
10628 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
10629 DPLL_FPA01_P1_POST_DIV_SHIFT);
10631 switch (dpll & DPLL_MODE_MASK) {
10632 case DPLLB_MODE_DAC_SERIAL:
10633 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10636 case DPLLB_MODE_LVDS:
10637 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10641 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
10642 "mode\n", (int)(dpll & DPLL_MODE_MASK));
10646 if (IS_PINEVIEW(dev))
10647 pineview_clock(refclk, &clock);
10649 i9xx_clock(refclk, &clock);
10651 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
10652 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
10655 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10656 DPLL_FPA01_P1_POST_DIV_SHIFT);
10658 if (lvds & LVDS_CLKB_POWER_UP)
10663 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10666 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10667 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10669 if (dpll & PLL_P2_DIVIDE_BY_4)
10675 i9xx_clock(refclk, &clock);
10679 * This value includes pixel_multiplier. We will use
10680 * port_clock to compute adjusted_mode.crtc_clock in the
10681 * encoder's get_config() function.
10683 pipe_config->port_clock = clock.dot;
10686 int intel_dotclock_calculate(int link_freq,
10687 const struct intel_link_m_n *m_n)
10690 * The calculation for the data clock is:
10691 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
10692 * But we want to avoid losing precison if possible, so:
10693 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
10695 * and the link clock is simpler:
10696 * link_clock = (m * link_clock) / n
10702 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10705 static void ironlake_pch_clock_get(struct intel_crtc *crtc,
10706 struct intel_crtc_state *pipe_config)
10708 struct drm_device *dev = crtc->base.dev;
10710 /* read out port_clock from the DPLL */
10711 i9xx_crtc_clock_get(crtc, pipe_config);
10714 * This value does not include pixel_multiplier.
10715 * We will check that port_clock and adjusted_mode.crtc_clock
10716 * agree once we know their relationship in the encoder's
10717 * get_config() function.
10719 pipe_config->base.adjusted_mode.crtc_clock =
10720 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10721 &pipe_config->fdi_m_n);
10724 /** Returns the currently programmed mode of the given pipe. */
10725 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10726 struct drm_crtc *crtc)
10728 struct drm_i915_private *dev_priv = dev->dev_private;
10729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10730 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
10731 struct drm_display_mode *mode;
10732 struct intel_crtc_state pipe_config;
10733 int htot = I915_READ(HTOTAL(cpu_transcoder));
10734 int hsync = I915_READ(HSYNC(cpu_transcoder));
10735 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10736 int vsync = I915_READ(VSYNC(cpu_transcoder));
10737 enum pipe pipe = intel_crtc->pipe;
10739 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10744 * Construct a pipe_config sufficient for getting the clock info
10745 * back out of crtc_clock_get.
10747 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10748 * to use a real value here instead.
10750 pipe_config.cpu_transcoder = (enum transcoder) pipe;
10751 pipe_config.pixel_multiplier = 1;
10752 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10753 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10754 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
10755 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10757 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
10758 mode->hdisplay = (htot & 0xffff) + 1;
10759 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10760 mode->hsync_start = (hsync & 0xffff) + 1;
10761 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10762 mode->vdisplay = (vtot & 0xffff) + 1;
10763 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10764 mode->vsync_start = (vsync & 0xffff) + 1;
10765 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10767 drm_mode_set_name(mode);
10772 static void intel_decrease_pllclock(struct drm_crtc *crtc)
10774 struct drm_device *dev = crtc->dev;
10775 struct drm_i915_private *dev_priv = dev->dev_private;
10776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10778 if (!HAS_GMCH_DISPLAY(dev))
10781 if (!dev_priv->lvds_downclock_avail)
10785 * Since this is called by a timer, we should never get here in
10788 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
10789 int pipe = intel_crtc->pipe;
10790 int dpll_reg = DPLL(pipe);
10793 DRM_DEBUG_DRIVER("downclocking LVDS\n");
10795 assert_panel_unlocked(dev_priv, pipe);
10797 dpll = I915_READ(dpll_reg);
10798 dpll |= DISPLAY_RATE_SELECT_FPA1;
10799 I915_WRITE(dpll_reg, dpll);
10800 intel_wait_for_vblank(dev, pipe);
10801 dpll = I915_READ(dpll_reg);
10802 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
10803 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
10808 void intel_mark_busy(struct drm_device *dev)
10810 struct drm_i915_private *dev_priv = dev->dev_private;
10812 if (dev_priv->mm.busy)
10815 intel_runtime_pm_get(dev_priv);
10816 i915_update_gfx_val(dev_priv);
10817 if (INTEL_INFO(dev)->gen >= 6)
10818 gen6_rps_busy(dev_priv);
10819 dev_priv->mm.busy = true;
10822 void intel_mark_idle(struct drm_device *dev)
10824 struct drm_i915_private *dev_priv = dev->dev_private;
10825 struct drm_crtc *crtc;
10827 if (!dev_priv->mm.busy)
10830 dev_priv->mm.busy = false;
10832 for_each_crtc(dev, crtc) {
10833 if (!crtc->primary->fb)
10836 intel_decrease_pllclock(crtc);
10839 if (INTEL_INFO(dev)->gen >= 6)
10840 gen6_rps_idle(dev->dev_private);
10842 intel_runtime_pm_put(dev_priv);
10845 static void intel_crtc_destroy(struct drm_crtc *crtc)
10847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10848 struct drm_device *dev = crtc->dev;
10849 struct intel_unpin_work *work;
10851 spin_lock_irq(&dev->event_lock);
10852 work = intel_crtc->unpin_work;
10853 intel_crtc->unpin_work = NULL;
10854 spin_unlock_irq(&dev->event_lock);
10857 cancel_work_sync(&work->work);
10861 drm_crtc_cleanup(crtc);
10866 static void intel_unpin_work_fn(struct work_struct *__work)
10868 struct intel_unpin_work *work =
10869 container_of(__work, struct intel_unpin_work, work);
10870 struct drm_device *dev = work->crtc->dev;
10871 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
10873 mutex_lock(&dev->struct_mutex);
10874 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
10875 drm_gem_object_unreference(&work->pending_flip_obj->base);
10877 intel_fbc_update(dev);
10879 if (work->flip_queued_req)
10880 i915_gem_request_assign(&work->flip_queued_req, NULL);
10881 mutex_unlock(&dev->struct_mutex);
10883 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
10884 drm_framebuffer_unreference(work->old_fb);
10886 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10887 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10892 static void do_intel_finish_page_flip(struct drm_device *dev,
10893 struct drm_crtc *crtc)
10895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10896 struct intel_unpin_work *work;
10897 unsigned long flags;
10899 /* Ignore early vblank irqs */
10900 if (intel_crtc == NULL)
10904 * This is called both by irq handlers and the reset code (to complete
10905 * lost pageflips) so needs the full irqsave spinlocks.
10907 spin_lock_irqsave(&dev->event_lock, flags);
10908 work = intel_crtc->unpin_work;
10910 /* Ensure we don't miss a work->pending update ... */
10913 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
10914 spin_unlock_irqrestore(&dev->event_lock, flags);
10918 page_flip_completed(intel_crtc);
10920 spin_unlock_irqrestore(&dev->event_lock, flags);
10923 void intel_finish_page_flip(struct drm_device *dev, int pipe)
10925 struct drm_i915_private *dev_priv = dev->dev_private;
10926 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10928 do_intel_finish_page_flip(dev, crtc);
10931 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10933 struct drm_i915_private *dev_priv = dev->dev_private;
10934 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10936 do_intel_finish_page_flip(dev, crtc);
10939 /* Is 'a' after or equal to 'b'? */
10940 static bool g4x_flip_count_after_eq(u32 a, u32 b)
10942 return !((a - b) & 0x80000000);
10945 static bool page_flip_finished(struct intel_crtc *crtc)
10947 struct drm_device *dev = crtc->base.dev;
10948 struct drm_i915_private *dev_priv = dev->dev_private;
10950 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10951 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10955 * The relevant registers doen't exist on pre-ctg.
10956 * As the flip done interrupt doesn't trigger for mmio
10957 * flips on gmch platforms, a flip count check isn't
10958 * really needed there. But since ctg has the registers,
10959 * include it in the check anyway.
10961 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10965 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10966 * used the same base address. In that case the mmio flip might
10967 * have completed, but the CS hasn't even executed the flip yet.
10969 * A flip count check isn't enough as the CS might have updated
10970 * the base address just after start of vblank, but before we
10971 * managed to process the interrupt. This means we'd complete the
10972 * CS flip too soon.
10974 * Combining both checks should get us a good enough result. It may
10975 * still happen that the CS flip has been executed, but has not
10976 * yet actually completed. But in case the base address is the same
10977 * anyway, we don't really care.
10979 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10980 crtc->unpin_work->gtt_offset &&
10981 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10982 crtc->unpin_work->flip_count);
10985 void intel_prepare_page_flip(struct drm_device *dev, int plane)
10987 struct drm_i915_private *dev_priv = dev->dev_private;
10988 struct intel_crtc *intel_crtc =
10989 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10990 unsigned long flags;
10994 * This is called both by irq handlers and the reset code (to complete
10995 * lost pageflips) so needs the full irqsave spinlocks.
10997 * NB: An MMIO update of the plane base pointer will also
10998 * generate a page-flip completion irq, i.e. every modeset
10999 * is also accompanied by a spurious intel_prepare_page_flip().
11001 spin_lock_irqsave(&dev->event_lock, flags);
11002 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
11003 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
11004 spin_unlock_irqrestore(&dev->event_lock, flags);
11007 static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
11009 /* Ensure that the work item is consistent when activating it ... */
11011 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
11012 /* and that it is marked active as soon as the irq could fire. */
11016 static int intel_gen2_queue_flip(struct drm_device *dev,
11017 struct drm_crtc *crtc,
11018 struct drm_framebuffer *fb,
11019 struct drm_i915_gem_object *obj,
11020 struct intel_engine_cs *ring,
11023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11027 ret = intel_ring_begin(ring, 6);
11031 /* Can't queue multiple flips, so wait for the previous
11032 * one to finish before executing the next.
11034 if (intel_crtc->plane)
11035 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11037 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11038 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11039 intel_ring_emit(ring, MI_NOOP);
11040 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11041 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11042 intel_ring_emit(ring, fb->pitches[0]);
11043 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
11044 intel_ring_emit(ring, 0); /* aux display base address, unused */
11046 intel_mark_page_flip_active(intel_crtc);
11047 __intel_ring_advance(ring);
11051 static int intel_gen3_queue_flip(struct drm_device *dev,
11052 struct drm_crtc *crtc,
11053 struct drm_framebuffer *fb,
11054 struct drm_i915_gem_object *obj,
11055 struct intel_engine_cs *ring,
11058 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11062 ret = intel_ring_begin(ring, 6);
11066 if (intel_crtc->plane)
11067 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11069 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
11070 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11071 intel_ring_emit(ring, MI_NOOP);
11072 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11073 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11074 intel_ring_emit(ring, fb->pitches[0]);
11075 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
11076 intel_ring_emit(ring, MI_NOOP);
11078 intel_mark_page_flip_active(intel_crtc);
11079 __intel_ring_advance(ring);
11083 static int intel_gen4_queue_flip(struct drm_device *dev,
11084 struct drm_crtc *crtc,
11085 struct drm_framebuffer *fb,
11086 struct drm_i915_gem_object *obj,
11087 struct intel_engine_cs *ring,
11090 struct drm_i915_private *dev_priv = dev->dev_private;
11091 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11092 uint32_t pf, pipesrc;
11095 ret = intel_ring_begin(ring, 4);
11099 /* i965+ uses the linear or tiled offsets from the
11100 * Display Registers (which do not change across a page-flip)
11101 * so we need only reprogram the base address.
11103 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11104 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11105 intel_ring_emit(ring, fb->pitches[0]);
11106 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
11109 /* XXX Enabling the panel-fitter across page-flip is so far
11110 * untested on non-native modes, so ignore it for now.
11111 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11114 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11115 intel_ring_emit(ring, pf | pipesrc);
11117 intel_mark_page_flip_active(intel_crtc);
11118 __intel_ring_advance(ring);
11122 static int intel_gen6_queue_flip(struct drm_device *dev,
11123 struct drm_crtc *crtc,
11124 struct drm_framebuffer *fb,
11125 struct drm_i915_gem_object *obj,
11126 struct intel_engine_cs *ring,
11129 struct drm_i915_private *dev_priv = dev->dev_private;
11130 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11131 uint32_t pf, pipesrc;
11134 ret = intel_ring_begin(ring, 4);
11138 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11139 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11140 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
11141 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
11143 /* Contrary to the suggestions in the documentation,
11144 * "Enable Panel Fitter" does not seem to be required when page
11145 * flipping with a non-native mode, and worse causes a normal
11147 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11150 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
11151 intel_ring_emit(ring, pf | pipesrc);
11153 intel_mark_page_flip_active(intel_crtc);
11154 __intel_ring_advance(ring);
11158 static int intel_gen7_queue_flip(struct drm_device *dev,
11159 struct drm_crtc *crtc,
11160 struct drm_framebuffer *fb,
11161 struct drm_i915_gem_object *obj,
11162 struct intel_engine_cs *ring,
11165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11166 uint32_t plane_bit = 0;
11169 switch (intel_crtc->plane) {
11171 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11174 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11177 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11180 WARN_ONCE(1, "unknown plane in flip command\n");
11185 if (ring->id == RCS) {
11188 * On Gen 8, SRM is now taking an extra dword to accommodate
11189 * 48bits addresses, and we need a NOOP for the batch size to
11197 * BSpec MI_DISPLAY_FLIP for IVB:
11198 * "The full packet must be contained within the same cache line."
11200 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11201 * cacheline, if we ever start emitting more commands before
11202 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11203 * then do the cacheline alignment, and finally emit the
11206 ret = intel_ring_cacheline_align(ring);
11210 ret = intel_ring_begin(ring, len);
11214 /* Unmask the flip-done completion message. Note that the bspec says that
11215 * we should do this for both the BCS and RCS, and that we must not unmask
11216 * more than one flip event at any time (or ensure that one flip message
11217 * can be sent by waiting for flip-done prior to queueing new flips).
11218 * Experimentation says that BCS works despite DERRMR masking all
11219 * flip-done completion events and that unmasking all planes at once
11220 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11221 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11223 if (ring->id == RCS) {
11224 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11225 intel_ring_emit(ring, DERRMR);
11226 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11227 DERRMR_PIPEB_PRI_FLIP_DONE |
11228 DERRMR_PIPEC_PRI_FLIP_DONE));
11230 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11231 MI_SRM_LRM_GLOBAL_GTT);
11233 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11234 MI_SRM_LRM_GLOBAL_GTT);
11235 intel_ring_emit(ring, DERRMR);
11236 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
11237 if (IS_GEN8(dev)) {
11238 intel_ring_emit(ring, 0);
11239 intel_ring_emit(ring, MI_NOOP);
11243 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
11244 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
11245 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
11246 intel_ring_emit(ring, (MI_NOOP));
11248 intel_mark_page_flip_active(intel_crtc);
11249 __intel_ring_advance(ring);
11253 static bool use_mmio_flip(struct intel_engine_cs *ring,
11254 struct drm_i915_gem_object *obj)
11257 * This is not being used for older platforms, because
11258 * non-availability of flip done interrupt forces us to use
11259 * CS flips. Older platforms derive flip done using some clever
11260 * tricks involving the flip_pending status bits and vblank irqs.
11261 * So using MMIO flips there would disrupt this mechanism.
11267 if (INTEL_INFO(ring->dev)->gen < 5)
11270 if (i915.use_mmio_flip < 0)
11272 else if (i915.use_mmio_flip > 0)
11274 else if (i915.enable_execlists)
11277 return ring != i915_gem_request_get_ring(obj->last_write_req);
11280 static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11282 struct drm_device *dev = intel_crtc->base.dev;
11283 struct drm_i915_private *dev_priv = dev->dev_private;
11284 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
11285 const enum pipe pipe = intel_crtc->pipe;
11288 ctl = I915_READ(PLANE_CTL(pipe, 0));
11289 ctl &= ~PLANE_CTL_TILED_MASK;
11290 switch (fb->modifier[0]) {
11291 case DRM_FORMAT_MOD_NONE:
11293 case I915_FORMAT_MOD_X_TILED:
11294 ctl |= PLANE_CTL_TILED_X;
11296 case I915_FORMAT_MOD_Y_TILED:
11297 ctl |= PLANE_CTL_TILED_Y;
11299 case I915_FORMAT_MOD_Yf_TILED:
11300 ctl |= PLANE_CTL_TILED_YF;
11303 MISSING_CASE(fb->modifier[0]);
11307 * The stride is either expressed as a multiple of 64 bytes chunks for
11308 * linear buffers or in number of tiles for tiled buffers.
11310 stride = fb->pitches[0] /
11311 intel_fb_stride_alignment(dev, fb->modifier[0],
11315 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11316 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11318 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11319 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11321 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11322 POSTING_READ(PLANE_SURF(pipe, 0));
11325 static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
11327 struct drm_device *dev = intel_crtc->base.dev;
11328 struct drm_i915_private *dev_priv = dev->dev_private;
11329 struct intel_framebuffer *intel_fb =
11330 to_intel_framebuffer(intel_crtc->base.primary->fb);
11331 struct drm_i915_gem_object *obj = intel_fb->obj;
11335 reg = DSPCNTR(intel_crtc->plane);
11336 dspcntr = I915_READ(reg);
11338 if (obj->tiling_mode != I915_TILING_NONE)
11339 dspcntr |= DISPPLANE_TILED;
11341 dspcntr &= ~DISPPLANE_TILED;
11343 I915_WRITE(reg, dspcntr);
11345 I915_WRITE(DSPSURF(intel_crtc->plane),
11346 intel_crtc->unpin_work->gtt_offset);
11347 POSTING_READ(DSPSURF(intel_crtc->plane));
11352 * XXX: This is the temporary way to update the plane registers until we get
11353 * around to using the usual plane update functions for MMIO flips
11355 static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11357 struct drm_device *dev = intel_crtc->base.dev;
11358 bool atomic_update;
11359 u32 start_vbl_count;
11361 intel_mark_page_flip_active(intel_crtc);
11363 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11365 if (INTEL_INFO(dev)->gen >= 9)
11366 skl_do_mmio_flip(intel_crtc);
11368 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11369 ilk_do_mmio_flip(intel_crtc);
11372 intel_pipe_update_end(intel_crtc, start_vbl_count);
11375 static void intel_mmio_flip_work_func(struct work_struct *work)
11377 struct intel_mmio_flip *mmio_flip =
11378 container_of(work, struct intel_mmio_flip, work);
11380 if (mmio_flip->req)
11381 WARN_ON(__i915_wait_request(mmio_flip->req,
11382 mmio_flip->crtc->reset_counter,
11384 &mmio_flip->i915->rps.mmioflips));
11386 intel_do_mmio_flip(mmio_flip->crtc);
11388 i915_gem_request_unreference__unlocked(mmio_flip->req);
11392 static int intel_queue_mmio_flip(struct drm_device *dev,
11393 struct drm_crtc *crtc,
11394 struct drm_framebuffer *fb,
11395 struct drm_i915_gem_object *obj,
11396 struct intel_engine_cs *ring,
11399 struct intel_mmio_flip *mmio_flip;
11401 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11402 if (mmio_flip == NULL)
11405 mmio_flip->i915 = to_i915(dev);
11406 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
11407 mmio_flip->crtc = to_intel_crtc(crtc);
11409 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11410 schedule_work(&mmio_flip->work);
11415 static int intel_default_queue_flip(struct drm_device *dev,
11416 struct drm_crtc *crtc,
11417 struct drm_framebuffer *fb,
11418 struct drm_i915_gem_object *obj,
11419 struct intel_engine_cs *ring,
11425 static bool __intel_pageflip_stall_check(struct drm_device *dev,
11426 struct drm_crtc *crtc)
11428 struct drm_i915_private *dev_priv = dev->dev_private;
11429 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11430 struct intel_unpin_work *work = intel_crtc->unpin_work;
11433 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11436 if (!work->enable_stall_check)
11439 if (work->flip_ready_vblank == 0) {
11440 if (work->flip_queued_req &&
11441 !i915_gem_request_completed(work->flip_queued_req, true))
11444 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
11447 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
11450 /* Potential stall - if we see that the flip has happened,
11451 * assume a missed interrupt. */
11452 if (INTEL_INFO(dev)->gen >= 4)
11453 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11455 addr = I915_READ(DSPADDR(intel_crtc->plane));
11457 /* There is a potential issue here with a false positive after a flip
11458 * to the same address. We could address this by checking for a
11459 * non-incrementing frame counter.
11461 return addr == work->gtt_offset;
11464 void intel_check_page_flip(struct drm_device *dev, int pipe)
11466 struct drm_i915_private *dev_priv = dev->dev_private;
11467 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11468 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11469 struct intel_unpin_work *work;
11471 WARN_ON(!in_interrupt());
11476 spin_lock(&dev->event_lock);
11477 work = intel_crtc->unpin_work;
11478 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
11479 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
11480 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
11481 page_flip_completed(intel_crtc);
11484 if (work != NULL &&
11485 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11486 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
11487 spin_unlock(&dev->event_lock);
11490 static int intel_crtc_page_flip(struct drm_crtc *crtc,
11491 struct drm_framebuffer *fb,
11492 struct drm_pending_vblank_event *event,
11493 uint32_t page_flip_flags)
11495 struct drm_device *dev = crtc->dev;
11496 struct drm_i915_private *dev_priv = dev->dev_private;
11497 struct drm_framebuffer *old_fb = crtc->primary->fb;
11498 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11499 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11500 struct drm_plane *primary = crtc->primary;
11501 enum pipe pipe = intel_crtc->pipe;
11502 struct intel_unpin_work *work;
11503 struct intel_engine_cs *ring;
11508 * drm_mode_page_flip_ioctl() should already catch this, but double
11509 * check to be safe. In the future we may enable pageflipping from
11510 * a disabled primary plane.
11512 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11515 /* Can't change pixel format via MI display flips. */
11516 if (fb->pixel_format != crtc->primary->fb->pixel_format)
11520 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11521 * Note that pitch changes could also affect these register.
11523 if (INTEL_INFO(dev)->gen > 3 &&
11524 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11525 fb->pitches[0] != crtc->primary->fb->pitches[0]))
11528 if (i915_terminally_wedged(&dev_priv->gpu_error))
11531 work = kzalloc(sizeof(*work), GFP_KERNEL);
11535 work->event = event;
11537 work->old_fb = old_fb;
11538 INIT_WORK(&work->work, intel_unpin_work_fn);
11540 ret = drm_crtc_vblank_get(crtc);
11544 /* We borrow the event spin lock for protecting unpin_work */
11545 spin_lock_irq(&dev->event_lock);
11546 if (intel_crtc->unpin_work) {
11547 /* Before declaring the flip queue wedged, check if
11548 * the hardware completed the operation behind our backs.
11550 if (__intel_pageflip_stall_check(dev, crtc)) {
11551 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11552 page_flip_completed(intel_crtc);
11554 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
11555 spin_unlock_irq(&dev->event_lock);
11557 drm_crtc_vblank_put(crtc);
11562 intel_crtc->unpin_work = work;
11563 spin_unlock_irq(&dev->event_lock);
11565 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11566 flush_workqueue(dev_priv->wq);
11568 /* Reference the objects for the scheduled work. */
11569 drm_framebuffer_reference(work->old_fb);
11570 drm_gem_object_reference(&obj->base);
11572 crtc->primary->fb = fb;
11573 update_state_fb(crtc->primary);
11575 work->pending_flip_obj = obj;
11577 ret = i915_mutex_lock_interruptible(dev);
11581 atomic_inc(&intel_crtc->unpin_work_count);
11582 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
11584 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
11585 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
11587 if (IS_VALLEYVIEW(dev)) {
11588 ring = &dev_priv->ring[BCS];
11589 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
11590 /* vlv: DISPLAY_FLIP fails to change tiling */
11592 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
11593 ring = &dev_priv->ring[BCS];
11594 } else if (INTEL_INFO(dev)->gen >= 7) {
11595 ring = i915_gem_request_get_ring(obj->last_write_req);
11596 if (ring == NULL || ring->id != RCS)
11597 ring = &dev_priv->ring[BCS];
11599 ring = &dev_priv->ring[RCS];
11602 mmio_flip = use_mmio_flip(ring, obj);
11604 /* When using CS flips, we want to emit semaphores between rings.
11605 * However, when using mmio flips we will create a task to do the
11606 * synchronisation, so all we want here is to pin the framebuffer
11607 * into the display plane and skip any waits.
11609 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
11610 crtc->primary->state,
11611 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
11613 goto cleanup_pending;
11615 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11616 + intel_crtc->dspaddr_offset;
11619 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11622 goto cleanup_unpin;
11624 i915_gem_request_assign(&work->flip_queued_req,
11625 obj->last_write_req);
11627 if (obj->last_write_req) {
11628 ret = i915_gem_check_olr(obj->last_write_req);
11630 goto cleanup_unpin;
11633 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
11636 goto cleanup_unpin;
11638 i915_gem_request_assign(&work->flip_queued_req,
11639 intel_ring_get_request(ring));
11642 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
11643 work->enable_stall_check = true;
11645 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
11646 INTEL_FRONTBUFFER_PRIMARY(pipe));
11648 intel_fbc_disable(dev);
11649 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
11650 mutex_unlock(&dev->struct_mutex);
11652 trace_i915_flip_request(intel_crtc->plane, obj);
11657 intel_unpin_fb_obj(fb, crtc->primary->state);
11659 atomic_dec(&intel_crtc->unpin_work_count);
11660 mutex_unlock(&dev->struct_mutex);
11662 crtc->primary->fb = old_fb;
11663 update_state_fb(crtc->primary);
11665 drm_gem_object_unreference_unlocked(&obj->base);
11666 drm_framebuffer_unreference(work->old_fb);
11668 spin_lock_irq(&dev->event_lock);
11669 intel_crtc->unpin_work = NULL;
11670 spin_unlock_irq(&dev->event_lock);
11672 drm_crtc_vblank_put(crtc);
11678 ret = intel_plane_restore(primary);
11679 if (ret == 0 && event) {
11680 spin_lock_irq(&dev->event_lock);
11681 drm_send_vblank_event(dev, pipe, event);
11682 spin_unlock_irq(&dev->event_lock);
11688 static const struct drm_crtc_helper_funcs intel_helper_funcs = {
11689 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11690 .load_lut = intel_crtc_load_lut,
11691 .atomic_begin = intel_begin_crtc_commit,
11692 .atomic_flush = intel_finish_crtc_commit,
11696 * intel_modeset_update_staged_output_state
11698 * Updates the staged output configuration state, e.g. after we've read out the
11699 * current hw state.
11701 static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11703 struct intel_crtc *crtc;
11704 struct intel_encoder *encoder;
11705 struct intel_connector *connector;
11707 for_each_intel_connector(dev, connector) {
11708 connector->new_encoder =
11709 to_intel_encoder(connector->base.encoder);
11712 for_each_intel_encoder(dev, encoder) {
11713 encoder->new_crtc =
11714 to_intel_crtc(encoder->base.crtc);
11717 for_each_intel_crtc(dev, crtc) {
11718 crtc->new_enabled = crtc->base.state->enable;
11722 /* Transitional helper to copy current connector/encoder state to
11723 * connector->state. This is needed so that code that is partially
11724 * converted to atomic does the right thing.
11726 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11728 struct intel_connector *connector;
11730 for_each_intel_connector(dev, connector) {
11731 if (connector->base.encoder) {
11732 connector->base.state->best_encoder =
11733 connector->base.encoder;
11734 connector->base.state->crtc =
11735 connector->base.encoder->crtc;
11737 connector->base.state->best_encoder = NULL;
11738 connector->base.state->crtc = NULL;
11744 connected_sink_compute_bpp(struct intel_connector *connector,
11745 struct intel_crtc_state *pipe_config)
11747 int bpp = pipe_config->pipe_bpp;
11749 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11750 connector->base.base.id,
11751 connector->base.name);
11753 /* Don't use an invalid EDID bpc value */
11754 if (connector->base.display_info.bpc &&
11755 connector->base.display_info.bpc * 3 < bpp) {
11756 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11757 bpp, connector->base.display_info.bpc*3);
11758 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11761 /* Clamp bpp to 8 on screens without EDID 1.4 */
11762 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11763 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11765 pipe_config->pipe_bpp = 24;
11770 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
11771 struct intel_crtc_state *pipe_config)
11773 struct drm_device *dev = crtc->base.dev;
11774 struct drm_atomic_state *state;
11775 struct drm_connector *connector;
11776 struct drm_connector_state *connector_state;
11779 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
11781 else if (INTEL_INFO(dev)->gen >= 5)
11787 pipe_config->pipe_bpp = bpp;
11789 state = pipe_config->base.state;
11791 /* Clamp display bpp to EDID value */
11792 for_each_connector_in_state(state, connector, connector_state, i) {
11793 if (connector_state->crtc != &crtc->base)
11796 connected_sink_compute_bpp(to_intel_connector(connector),
11803 static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11805 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11806 "type: 0x%x flags: 0x%x\n",
11808 mode->crtc_hdisplay, mode->crtc_hsync_start,
11809 mode->crtc_hsync_end, mode->crtc_htotal,
11810 mode->crtc_vdisplay, mode->crtc_vsync_start,
11811 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11814 static void intel_dump_pipe_config(struct intel_crtc *crtc,
11815 struct intel_crtc_state *pipe_config,
11816 const char *context)
11818 struct drm_device *dev = crtc->base.dev;
11819 struct drm_plane *plane;
11820 struct intel_plane *intel_plane;
11821 struct intel_plane_state *state;
11822 struct drm_framebuffer *fb;
11824 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11825 context, pipe_config, pipe_name(crtc->pipe));
11827 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11828 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11829 pipe_config->pipe_bpp, pipe_config->dither);
11830 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11831 pipe_config->has_pch_encoder,
11832 pipe_config->fdi_lanes,
11833 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11834 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11835 pipe_config->fdi_m_n.tu);
11836 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11837 pipe_config->has_dp_encoder,
11838 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11839 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11840 pipe_config->dp_m_n.tu);
11842 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11843 pipe_config->has_dp_encoder,
11844 pipe_config->dp_m2_n2.gmch_m,
11845 pipe_config->dp_m2_n2.gmch_n,
11846 pipe_config->dp_m2_n2.link_m,
11847 pipe_config->dp_m2_n2.link_n,
11848 pipe_config->dp_m2_n2.tu);
11850 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11851 pipe_config->has_audio,
11852 pipe_config->has_infoframe);
11854 DRM_DEBUG_KMS("requested mode:\n");
11855 drm_mode_debug_printmodeline(&pipe_config->base.mode);
11856 DRM_DEBUG_KMS("adjusted mode:\n");
11857 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11858 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
11859 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
11860 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11861 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
11862 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11864 pipe_config->scaler_state.scaler_users,
11865 pipe_config->scaler_state.scaler_id);
11866 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11867 pipe_config->gmch_pfit.control,
11868 pipe_config->gmch_pfit.pgm_ratios,
11869 pipe_config->gmch_pfit.lvds_border_bits);
11870 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
11871 pipe_config->pch_pfit.pos,
11872 pipe_config->pch_pfit.size,
11873 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
11874 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
11875 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
11877 if (IS_BROXTON(dev)) {
11878 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11879 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11880 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11881 pipe_config->ddi_pll_sel,
11882 pipe_config->dpll_hw_state.ebb0,
11883 pipe_config->dpll_hw_state.pll0,
11884 pipe_config->dpll_hw_state.pll1,
11885 pipe_config->dpll_hw_state.pll2,
11886 pipe_config->dpll_hw_state.pll3,
11887 pipe_config->dpll_hw_state.pll6,
11888 pipe_config->dpll_hw_state.pll8,
11889 pipe_config->dpll_hw_state.pcsdw12);
11890 } else if (IS_SKYLAKE(dev)) {
11891 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11892 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11893 pipe_config->ddi_pll_sel,
11894 pipe_config->dpll_hw_state.ctrl1,
11895 pipe_config->dpll_hw_state.cfgcr1,
11896 pipe_config->dpll_hw_state.cfgcr2);
11897 } else if (HAS_DDI(dev)) {
11898 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11899 pipe_config->ddi_pll_sel,
11900 pipe_config->dpll_hw_state.wrpll);
11902 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11903 "fp0: 0x%x, fp1: 0x%x\n",
11904 pipe_config->dpll_hw_state.dpll,
11905 pipe_config->dpll_hw_state.dpll_md,
11906 pipe_config->dpll_hw_state.fp0,
11907 pipe_config->dpll_hw_state.fp1);
11910 DRM_DEBUG_KMS("planes on this crtc\n");
11911 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11912 intel_plane = to_intel_plane(plane);
11913 if (intel_plane->pipe != crtc->pipe)
11916 state = to_intel_plane_state(plane->state);
11917 fb = state->base.fb;
11919 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11920 "disabled, scaler_id = %d\n",
11921 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11922 plane->base.id, intel_plane->pipe,
11923 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11924 drm_plane_index(plane), state->scaler_id);
11928 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11929 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11930 plane->base.id, intel_plane->pipe,
11931 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11932 drm_plane_index(plane));
11933 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11934 fb->base.id, fb->width, fb->height, fb->pixel_format);
11935 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11937 state->src.x1 >> 16, state->src.y1 >> 16,
11938 drm_rect_width(&state->src) >> 16,
11939 drm_rect_height(&state->src) >> 16,
11940 state->dst.x1, state->dst.y1,
11941 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11945 static bool encoders_cloneable(const struct intel_encoder *a,
11946 const struct intel_encoder *b)
11948 /* masks could be asymmetric, so check both ways */
11949 return a == b || (a->cloneable & (1 << b->type) &&
11950 b->cloneable & (1 << a->type));
11953 static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11954 struct intel_crtc *crtc,
11955 struct intel_encoder *encoder)
11957 struct intel_encoder *source_encoder;
11958 struct drm_connector *connector;
11959 struct drm_connector_state *connector_state;
11962 for_each_connector_in_state(state, connector, connector_state, i) {
11963 if (connector_state->crtc != &crtc->base)
11967 to_intel_encoder(connector_state->best_encoder);
11968 if (!encoders_cloneable(encoder, source_encoder))
11975 static bool check_encoder_cloning(struct drm_atomic_state *state,
11976 struct intel_crtc *crtc)
11978 struct intel_encoder *encoder;
11979 struct drm_connector *connector;
11980 struct drm_connector_state *connector_state;
11983 for_each_connector_in_state(state, connector, connector_state, i) {
11984 if (connector_state->crtc != &crtc->base)
11987 encoder = to_intel_encoder(connector_state->best_encoder);
11988 if (!check_single_encoder_cloning(state, crtc, encoder))
11995 static bool check_digital_port_conflicts(struct drm_atomic_state *state)
11997 struct drm_device *dev = state->dev;
11998 struct intel_encoder *encoder;
11999 struct drm_connector *connector;
12000 struct drm_connector_state *connector_state;
12001 unsigned int used_ports = 0;
12005 * Walk the connector list instead of the encoder
12006 * list to detect the problem on ddi platforms
12007 * where there's just one encoder per digital port.
12009 for_each_connector_in_state(state, connector, connector_state, i) {
12010 if (!connector_state->best_encoder)
12013 encoder = to_intel_encoder(connector_state->best_encoder);
12015 WARN_ON(!connector_state->crtc);
12017 switch (encoder->type) {
12018 unsigned int port_mask;
12019 case INTEL_OUTPUT_UNKNOWN:
12020 if (WARN_ON(!HAS_DDI(dev)))
12022 case INTEL_OUTPUT_DISPLAYPORT:
12023 case INTEL_OUTPUT_HDMI:
12024 case INTEL_OUTPUT_EDP:
12025 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12027 /* the same port mustn't appear more than once */
12028 if (used_ports & port_mask)
12031 used_ports |= port_mask;
12041 clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12043 struct drm_crtc_state tmp_state;
12044 struct intel_crtc_scaler_state scaler_state;
12045 struct intel_dpll_hw_state dpll_hw_state;
12046 enum intel_dpll_id shared_dpll;
12047 uint32_t ddi_pll_sel;
12049 /* FIXME: before the switch to atomic started, a new pipe_config was
12050 * kzalloc'd. Code that depends on any field being zero should be
12051 * fixed, so that the crtc_state can be safely duplicated. For now,
12052 * only fields that are know to not cause problems are preserved. */
12054 tmp_state = crtc_state->base;
12055 scaler_state = crtc_state->scaler_state;
12056 shared_dpll = crtc_state->shared_dpll;
12057 dpll_hw_state = crtc_state->dpll_hw_state;
12058 ddi_pll_sel = crtc_state->ddi_pll_sel;
12060 memset(crtc_state, 0, sizeof *crtc_state);
12062 crtc_state->base = tmp_state;
12063 crtc_state->scaler_state = scaler_state;
12064 crtc_state->shared_dpll = shared_dpll;
12065 crtc_state->dpll_hw_state = dpll_hw_state;
12066 crtc_state->ddi_pll_sel = ddi_pll_sel;
12070 intel_modeset_pipe_config(struct drm_crtc *crtc,
12071 struct drm_atomic_state *state,
12072 struct intel_crtc_state *pipe_config)
12074 struct intel_encoder *encoder;
12075 struct drm_connector *connector;
12076 struct drm_connector_state *connector_state;
12077 int base_bpp, ret = -EINVAL;
12081 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
12082 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
12086 if (!check_digital_port_conflicts(state)) {
12087 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12092 * XXX: Add all connectors to make the crtc state match the encoders.
12094 if (!needs_modeset(&pipe_config->base)) {
12095 ret = drm_atomic_add_affected_connectors(state, crtc);
12100 clear_intel_crtc_state(pipe_config);
12102 pipe_config->cpu_transcoder =
12103 (enum transcoder) to_intel_crtc(crtc)->pipe;
12106 * Sanitize sync polarity flags based on requested ones. If neither
12107 * positive or negative polarity is requested, treat this as meaning
12108 * negative polarity.
12110 if (!(pipe_config->base.adjusted_mode.flags &
12111 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
12112 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
12114 if (!(pipe_config->base.adjusted_mode.flags &
12115 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
12116 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
12118 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12119 * plane pixel format and any sink constraints into account. Returns the
12120 * source plane bpp so that dithering can be selected on mismatches
12121 * after encoders and crtc also have had their say. */
12122 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12128 * Determine the real pipe dimensions. Note that stereo modes can
12129 * increase the actual pipe size due to the frame doubling and
12130 * insertion of additional space for blanks between the frame. This
12131 * is stored in the crtc timings. We use the requested mode to do this
12132 * computation to clearly distinguish it from the adjusted mode, which
12133 * can be changed by the connectors in the below retry loop.
12135 drm_crtc_get_hv_timing(&pipe_config->base.mode,
12136 &pipe_config->pipe_src_w,
12137 &pipe_config->pipe_src_h);
12140 /* Ensure the port clock defaults are reset when retrying. */
12141 pipe_config->port_clock = 0;
12142 pipe_config->pixel_multiplier = 1;
12144 /* Fill in default crtc timings, allow encoders to overwrite them. */
12145 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12146 CRTC_STEREO_DOUBLE);
12148 /* Pass our mode to the connectors and the CRTC to give them a chance to
12149 * adjust it according to limitations or connector properties, and also
12150 * a chance to reject the mode entirely.
12152 for_each_connector_in_state(state, connector, connector_state, i) {
12153 if (connector_state->crtc != crtc)
12156 encoder = to_intel_encoder(connector_state->best_encoder);
12158 if (!(encoder->compute_config(encoder, pipe_config))) {
12159 DRM_DEBUG_KMS("Encoder config failure\n");
12164 /* Set default port clock if not overwritten by the encoder. Needs to be
12165 * done afterwards in case the encoder adjusts the mode. */
12166 if (!pipe_config->port_clock)
12167 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
12168 * pipe_config->pixel_multiplier;
12170 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
12172 DRM_DEBUG_KMS("CRTC fixup failed\n");
12176 if (ret == RETRY) {
12177 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12182 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12184 goto encoder_retry;
12187 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
12188 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
12189 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
12191 /* Check if we need to force a modeset */
12192 if (pipe_config->has_audio !=
12193 to_intel_crtc_state(crtc->state)->has_audio) {
12194 pipe_config->base.mode_changed = true;
12195 ret = drm_atomic_add_affected_planes(state, crtc);
12199 * Note we have an issue here with infoframes: current code
12200 * only updates them on the full mode set path per hw
12201 * requirements. So here we should be checking for any
12202 * required changes and forcing a mode set.
12208 static bool intel_crtc_in_use(struct drm_crtc *crtc)
12210 struct drm_encoder *encoder;
12211 struct drm_device *dev = crtc->dev;
12213 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12214 if (encoder->crtc == crtc)
12221 intel_modeset_update_state(struct drm_atomic_state *state)
12223 struct drm_device *dev = state->dev;
12224 struct drm_i915_private *dev_priv = dev->dev_private;
12225 struct intel_encoder *intel_encoder;
12226 struct drm_crtc *crtc;
12227 struct drm_crtc_state *crtc_state;
12228 struct drm_connector *connector;
12230 intel_shared_dpll_commit(dev_priv);
12231 drm_atomic_helper_swap_state(state->dev, state);
12233 for_each_intel_encoder(dev, intel_encoder) {
12234 if (!intel_encoder->base.crtc)
12237 crtc = intel_encoder->base.crtc;
12238 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12239 if (!crtc_state || !needs_modeset(crtc->state))
12242 intel_encoder->connectors_active = false;
12245 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
12246 intel_modeset_update_staged_output_state(state->dev);
12248 /* Double check state. */
12249 for_each_crtc(dev, crtc) {
12250 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
12252 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
12255 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12256 if (!connector->encoder || !connector->encoder->crtc)
12259 crtc = connector->encoder->crtc;
12260 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12261 if (!crtc_state || !needs_modeset(crtc->state))
12264 if (crtc->state->active) {
12265 struct drm_property *dpms_property =
12266 dev->mode_config.dpms_property;
12268 connector->dpms = DRM_MODE_DPMS_ON;
12269 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
12271 intel_encoder = to_intel_encoder(connector->encoder);
12272 intel_encoder->connectors_active = true;
12274 connector->dpms = DRM_MODE_DPMS_OFF;
12278 static bool intel_fuzzy_clock_check(int clock1, int clock2)
12282 if (clock1 == clock2)
12285 if (!clock1 || !clock2)
12288 diff = abs(clock1 - clock2);
12290 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12296 #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12297 list_for_each_entry((intel_crtc), \
12298 &(dev)->mode_config.crtc_list, \
12300 if (mask & (1 <<(intel_crtc)->pipe))
12303 intel_pipe_config_compare(struct drm_device *dev,
12304 struct intel_crtc_state *current_config,
12305 struct intel_crtc_state *pipe_config)
12307 #define PIPE_CONF_CHECK_X(name) \
12308 if (current_config->name != pipe_config->name) { \
12309 DRM_ERROR("mismatch in " #name " " \
12310 "(expected 0x%08x, found 0x%08x)\n", \
12311 current_config->name, \
12312 pipe_config->name); \
12316 #define PIPE_CONF_CHECK_I(name) \
12317 if (current_config->name != pipe_config->name) { \
12318 DRM_ERROR("mismatch in " #name " " \
12319 "(expected %i, found %i)\n", \
12320 current_config->name, \
12321 pipe_config->name); \
12325 /* This is required for BDW+ where there is only one set of registers for
12326 * switching between high and low RR.
12327 * This macro can be used whenever a comparison has to be made between one
12328 * hw state and multiple sw state variables.
12330 #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12331 if ((current_config->name != pipe_config->name) && \
12332 (current_config->alt_name != pipe_config->name)) { \
12333 DRM_ERROR("mismatch in " #name " " \
12334 "(expected %i or %i, found %i)\n", \
12335 current_config->name, \
12336 current_config->alt_name, \
12337 pipe_config->name); \
12341 #define PIPE_CONF_CHECK_FLAGS(name, mask) \
12342 if ((current_config->name ^ pipe_config->name) & (mask)) { \
12343 DRM_ERROR("mismatch in " #name "(" #mask ") " \
12344 "(expected %i, found %i)\n", \
12345 current_config->name & (mask), \
12346 pipe_config->name & (mask)); \
12350 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12351 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12352 DRM_ERROR("mismatch in " #name " " \
12353 "(expected %i, found %i)\n", \
12354 current_config->name, \
12355 pipe_config->name); \
12359 #define PIPE_CONF_QUIRK(quirk) \
12360 ((current_config->quirks | pipe_config->quirks) & (quirk))
12362 PIPE_CONF_CHECK_I(cpu_transcoder);
12364 PIPE_CONF_CHECK_I(has_pch_encoder);
12365 PIPE_CONF_CHECK_I(fdi_lanes);
12366 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12367 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12368 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12369 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12370 PIPE_CONF_CHECK_I(fdi_m_n.tu);
12372 PIPE_CONF_CHECK_I(has_dp_encoder);
12374 if (INTEL_INFO(dev)->gen < 8) {
12375 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12376 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12377 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12378 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12379 PIPE_CONF_CHECK_I(dp_m_n.tu);
12381 if (current_config->has_drrs) {
12382 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12383 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12384 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12385 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12386 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12389 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12390 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12391 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12392 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12393 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12396 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12397 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12398 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12399 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12400 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12401 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
12403 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12404 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12405 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12406 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12407 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12408 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
12410 PIPE_CONF_CHECK_I(pixel_multiplier);
12411 PIPE_CONF_CHECK_I(has_hdmi_sink);
12412 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12413 IS_VALLEYVIEW(dev))
12414 PIPE_CONF_CHECK_I(limited_color_range);
12415 PIPE_CONF_CHECK_I(has_infoframe);
12417 PIPE_CONF_CHECK_I(has_audio);
12419 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12420 DRM_MODE_FLAG_INTERLACE);
12422 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
12423 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12424 DRM_MODE_FLAG_PHSYNC);
12425 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12426 DRM_MODE_FLAG_NHSYNC);
12427 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12428 DRM_MODE_FLAG_PVSYNC);
12429 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
12430 DRM_MODE_FLAG_NVSYNC);
12433 PIPE_CONF_CHECK_I(pipe_src_w);
12434 PIPE_CONF_CHECK_I(pipe_src_h);
12437 * FIXME: BIOS likes to set up a cloned config with lvds+external
12438 * screen. Since we don't yet re-compute the pipe config when moving
12439 * just the lvds port away to another pipe the sw tracking won't match.
12441 * Proper atomic modesets with recomputed global state will fix this.
12442 * Until then just don't check gmch state for inherited modes.
12444 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12445 PIPE_CONF_CHECK_I(gmch_pfit.control);
12446 /* pfit ratios are autocomputed by the hw on gen4+ */
12447 if (INTEL_INFO(dev)->gen < 4)
12448 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12449 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12452 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12453 if (current_config->pch_pfit.enabled) {
12454 PIPE_CONF_CHECK_I(pch_pfit.pos);
12455 PIPE_CONF_CHECK_I(pch_pfit.size);
12458 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12460 /* BDW+ don't expose a synchronous way to read the state */
12461 if (IS_HASWELL(dev))
12462 PIPE_CONF_CHECK_I(ips_enabled);
12464 PIPE_CONF_CHECK_I(double_wide);
12466 PIPE_CONF_CHECK_X(ddi_pll_sel);
12468 PIPE_CONF_CHECK_I(shared_dpll);
12469 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
12470 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
12471 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12472 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
12473 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
12474 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12475 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12476 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
12478 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12479 PIPE_CONF_CHECK_I(pipe_bpp);
12481 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
12482 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
12484 #undef PIPE_CONF_CHECK_X
12485 #undef PIPE_CONF_CHECK_I
12486 #undef PIPE_CONF_CHECK_I_ALT
12487 #undef PIPE_CONF_CHECK_FLAGS
12488 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
12489 #undef PIPE_CONF_QUIRK
12494 static void check_wm_state(struct drm_device *dev)
12496 struct drm_i915_private *dev_priv = dev->dev_private;
12497 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12498 struct intel_crtc *intel_crtc;
12501 if (INTEL_INFO(dev)->gen < 9)
12504 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12505 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12507 for_each_intel_crtc(dev, intel_crtc) {
12508 struct skl_ddb_entry *hw_entry, *sw_entry;
12509 const enum pipe pipe = intel_crtc->pipe;
12511 if (!intel_crtc->active)
12515 for_each_plane(dev_priv, pipe, plane) {
12516 hw_entry = &hw_ddb.plane[pipe][plane];
12517 sw_entry = &sw_ddb->plane[pipe][plane];
12519 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12522 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12523 "(expected (%u,%u), found (%u,%u))\n",
12524 pipe_name(pipe), plane + 1,
12525 sw_entry->start, sw_entry->end,
12526 hw_entry->start, hw_entry->end);
12530 hw_entry = &hw_ddb.cursor[pipe];
12531 sw_entry = &sw_ddb->cursor[pipe];
12533 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12536 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12537 "(expected (%u,%u), found (%u,%u))\n",
12539 sw_entry->start, sw_entry->end,
12540 hw_entry->start, hw_entry->end);
12545 check_connector_state(struct drm_device *dev)
12547 struct intel_connector *connector;
12549 for_each_intel_connector(dev, connector) {
12550 /* This also checks the encoder/connector hw state with the
12551 * ->get_hw_state callbacks. */
12552 intel_connector_check_state(connector);
12554 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
12555 "connector's staged encoder doesn't match current encoder\n");
12560 check_encoder_state(struct drm_device *dev)
12562 struct intel_encoder *encoder;
12563 struct intel_connector *connector;
12565 for_each_intel_encoder(dev, encoder) {
12566 bool enabled = false;
12567 bool active = false;
12568 enum pipe pipe, tracked_pipe;
12570 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12571 encoder->base.base.id,
12572 encoder->base.name);
12574 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
12575 "encoder's stage crtc doesn't match current crtc\n");
12576 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
12577 "encoder's active_connectors set, but no crtc\n");
12579 for_each_intel_connector(dev, connector) {
12580 if (connector->base.encoder != &encoder->base)
12583 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12587 * for MST connectors if we unplug the connector is gone
12588 * away but the encoder is still connected to a crtc
12589 * until a modeset happens in response to the hotplug.
12591 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12594 I915_STATE_WARN(!!encoder->base.crtc != enabled,
12595 "encoder's enabled state mismatch "
12596 "(expected %i, found %i)\n",
12597 !!encoder->base.crtc, enabled);
12598 I915_STATE_WARN(active && !encoder->base.crtc,
12599 "active encoder with no crtc\n");
12601 I915_STATE_WARN(encoder->connectors_active != active,
12602 "encoder's computed active state doesn't match tracked active state "
12603 "(expected %i, found %i)\n", active, encoder->connectors_active);
12605 active = encoder->get_hw_state(encoder, &pipe);
12606 I915_STATE_WARN(active != encoder->connectors_active,
12607 "encoder's hw state doesn't match sw tracking "
12608 "(expected %i, found %i)\n",
12609 encoder->connectors_active, active);
12611 if (!encoder->base.crtc)
12614 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
12615 I915_STATE_WARN(active && pipe != tracked_pipe,
12616 "active encoder's pipe doesn't match"
12617 "(expected %i, found %i)\n",
12618 tracked_pipe, pipe);
12624 check_crtc_state(struct drm_device *dev)
12626 struct drm_i915_private *dev_priv = dev->dev_private;
12627 struct intel_crtc *crtc;
12628 struct intel_encoder *encoder;
12629 struct intel_crtc_state pipe_config;
12631 for_each_intel_crtc(dev, crtc) {
12632 bool enabled = false;
12633 bool active = false;
12635 memset(&pipe_config, 0, sizeof(pipe_config));
12637 DRM_DEBUG_KMS("[CRTC:%d]\n",
12638 crtc->base.base.id);
12640 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
12641 "active crtc, but not enabled in sw tracking\n");
12643 for_each_intel_encoder(dev, encoder) {
12644 if (encoder->base.crtc != &crtc->base)
12647 if (encoder->connectors_active)
12651 I915_STATE_WARN(active != crtc->active,
12652 "crtc's computed active state doesn't match tracked active state "
12653 "(expected %i, found %i)\n", active, crtc->active);
12654 I915_STATE_WARN(enabled != crtc->base.state->enable,
12655 "crtc's computed enabled state doesn't match tracked enabled state "
12656 "(expected %i, found %i)\n", enabled,
12657 crtc->base.state->enable);
12659 active = dev_priv->display.get_pipe_config(crtc,
12662 /* hw state is inconsistent with the pipe quirk */
12663 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12664 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12665 active = crtc->active;
12667 for_each_intel_encoder(dev, encoder) {
12669 if (encoder->base.crtc != &crtc->base)
12671 if (encoder->get_hw_state(encoder, &pipe))
12672 encoder->get_config(encoder, &pipe_config);
12675 I915_STATE_WARN(crtc->active != active,
12676 "crtc active state doesn't match with hw state "
12677 "(expected %i, found %i)\n", crtc->active, active);
12679 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12680 "transitional active state does not match atomic hw state "
12681 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12684 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
12685 I915_STATE_WARN(1, "pipe state doesn't match!\n");
12686 intel_dump_pipe_config(crtc, &pipe_config,
12688 intel_dump_pipe_config(crtc, crtc->config,
12695 check_shared_dpll_state(struct drm_device *dev)
12697 struct drm_i915_private *dev_priv = dev->dev_private;
12698 struct intel_crtc *crtc;
12699 struct intel_dpll_hw_state dpll_hw_state;
12702 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12703 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12704 int enabled_crtcs = 0, active_crtcs = 0;
12707 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12709 DRM_DEBUG_KMS("%s\n", pll->name);
12711 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12713 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
12714 "more active pll users than references: %i vs %i\n",
12715 pll->active, hweight32(pll->config.crtc_mask));
12716 I915_STATE_WARN(pll->active && !pll->on,
12717 "pll in active use but not on in sw tracking\n");
12718 I915_STATE_WARN(pll->on && !pll->active,
12719 "pll in on but not on in use in sw tracking\n");
12720 I915_STATE_WARN(pll->on != active,
12721 "pll on state mismatch (expected %i, found %i)\n",
12724 for_each_intel_crtc(dev, crtc) {
12725 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
12727 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12730 I915_STATE_WARN(pll->active != active_crtcs,
12731 "pll active crtcs mismatch (expected %i, found %i)\n",
12732 pll->active, active_crtcs);
12733 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
12734 "pll enabled crtcs mismatch (expected %i, found %i)\n",
12735 hweight32(pll->config.crtc_mask), enabled_crtcs);
12737 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
12738 sizeof(dpll_hw_state)),
12739 "pll hw state mismatch\n");
12744 intel_modeset_check_state(struct drm_device *dev)
12746 check_wm_state(dev);
12747 check_connector_state(dev);
12748 check_encoder_state(dev);
12749 check_crtc_state(dev);
12750 check_shared_dpll_state(dev);
12753 void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
12757 * FDI already provided one idea for the dotclock.
12758 * Yell if the encoder disagrees.
12760 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
12761 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
12762 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
12765 static void update_scanline_offset(struct intel_crtc *crtc)
12767 struct drm_device *dev = crtc->base.dev;
12770 * The scanline counter increments at the leading edge of hsync.
12772 * On most platforms it starts counting from vtotal-1 on the
12773 * first active line. That means the scanline counter value is
12774 * always one less than what we would expect. Ie. just after
12775 * start of vblank, which also occurs at start of hsync (on the
12776 * last active line), the scanline counter will read vblank_start-1.
12778 * On gen2 the scanline counter starts counting from 1 instead
12779 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12780 * to keep the value positive), instead of adding one.
12782 * On HSW+ the behaviour of the scanline counter depends on the output
12783 * type. For DP ports it behaves like most other platforms, but on HDMI
12784 * there's an extra 1 line difference. So we need to add two instead of
12785 * one to the value.
12787 if (IS_GEN2(dev)) {
12788 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
12791 vtotal = mode->crtc_vtotal;
12792 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12795 crtc->scanline_offset = vtotal - 1;
12796 } else if (HAS_DDI(dev) &&
12797 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
12798 crtc->scanline_offset = 2;
12800 crtc->scanline_offset = 1;
12803 static struct intel_crtc_state *
12804 intel_modeset_compute_config(struct drm_crtc *crtc,
12805 struct drm_atomic_state *state)
12807 struct intel_crtc_state *pipe_config;
12810 ret = drm_atomic_helper_check_modeset(state->dev, state);
12812 return ERR_PTR(ret);
12815 * Note this needs changes when we start tracking multiple modes
12816 * and crtcs. At that point we'll need to compute the whole config
12817 * (i.e. one pipe_config for each crtc) rather than just the one
12820 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12821 if (IS_ERR(pipe_config))
12822 return pipe_config;
12824 if (!pipe_config->base.enable &&
12825 WARN_ON(pipe_config->base.active))
12826 pipe_config->base.active = false;
12828 if (!pipe_config->base.enable)
12829 return pipe_config;
12831 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
12833 return ERR_PTR(ret);
12835 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config, "[modeset]");
12837 ret = drm_atomic_helper_check_planes(state->dev, state);
12839 return ERR_PTR(ret);
12841 return pipe_config;
12844 static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
12846 struct drm_device *dev = state->dev;
12847 struct drm_i915_private *dev_priv = to_i915(dev);
12848 unsigned clear_pipes = 0;
12849 struct intel_crtc *intel_crtc;
12850 struct intel_crtc_state *intel_crtc_state;
12851 struct drm_crtc *crtc;
12852 struct drm_crtc_state *crtc_state;
12856 if (!dev_priv->display.crtc_compute_clock)
12859 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12860 intel_crtc = to_intel_crtc(crtc);
12861 intel_crtc_state = to_intel_crtc_state(crtc_state);
12863 if (needs_modeset(crtc_state)) {
12864 clear_pipes |= 1 << intel_crtc->pipe;
12865 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
12869 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12873 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12874 if (!needs_modeset(crtc_state) || !crtc_state->enable)
12877 intel_crtc = to_intel_crtc(crtc);
12878 intel_crtc_state = to_intel_crtc_state(crtc_state);
12880 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
12883 intel_shared_dpll_abort_config(dev_priv);
12892 /* Code that should eventually be part of atomic_check() */
12893 static int __intel_set_mode_checks(struct drm_atomic_state *state)
12895 struct drm_device *dev = state->dev;
12899 * See if the config requires any additional preparation, e.g.
12900 * to adjust global state with pipes off. We need to do this
12901 * here so we can get the modeset_pipe updated config for the new
12902 * mode set on this crtc. For other crtcs we need to use the
12903 * adjusted_mode bits in the crtc directly.
12905 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
12906 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
12907 ret = valleyview_modeset_global_pipes(state);
12909 ret = broadwell_modeset_global_pipes(state);
12915 ret = __intel_set_mode_setup_plls(state);
12922 static int __intel_set_mode(struct drm_atomic_state *state)
12924 struct drm_device *dev = state->dev;
12925 struct drm_i915_private *dev_priv = dev->dev_private;
12926 struct drm_crtc *crtc;
12927 struct drm_crtc_state *crtc_state;
12931 ret = __intel_set_mode_checks(state);
12935 ret = drm_atomic_helper_prepare_planes(dev, state);
12939 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12940 if (!needs_modeset(crtc_state) || !crtc->state->active)
12943 intel_crtc_disable_planes(crtc);
12944 dev_priv->display.crtc_disable(crtc);
12945 if (!crtc_state->enable)
12946 drm_plane_helper_disable(crtc->primary);
12949 /* Only after disabling all output pipelines that will be changed can we
12950 * update the the output configuration. */
12951 intel_modeset_update_state(state);
12953 /* The state has been swaped above, so state actually contains the
12954 * old state now. */
12956 modeset_update_crtc_power_domains(state);
12958 drm_atomic_helper_commit_planes(dev, state);
12960 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
12961 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12962 if (!needs_modeset(crtc->state) || !crtc->state->active)
12965 update_scanline_offset(to_intel_crtc(crtc));
12967 dev_priv->display.crtc_enable(crtc);
12968 intel_crtc_enable_planes(crtc);
12971 /* FIXME: add subpixel order */
12973 drm_atomic_helper_cleanup_planes(dev, state);
12975 drm_atomic_state_free(state);
12980 static int intel_set_mode_with_config(struct drm_crtc *crtc,
12981 struct intel_crtc_state *pipe_config)
12985 ret = __intel_set_mode(pipe_config->base.state);
12988 intel_modeset_check_state(crtc->dev);
12993 static int intel_set_mode(struct drm_crtc *crtc,
12994 struct drm_atomic_state *state)
12996 struct intel_crtc_state *pipe_config;
12999 pipe_config = intel_modeset_compute_config(crtc, state);
13000 if (IS_ERR(pipe_config)) {
13001 ret = PTR_ERR(pipe_config);
13005 ret = intel_set_mode_with_config(crtc, pipe_config);
13013 void intel_crtc_restore_mode(struct drm_crtc *crtc)
13015 struct drm_device *dev = crtc->dev;
13016 struct drm_atomic_state *state;
13017 struct intel_crtc *intel_crtc;
13018 struct intel_encoder *encoder;
13019 struct intel_connector *connector;
13020 struct drm_connector_state *connector_state;
13021 struct intel_crtc_state *crtc_state;
13024 state = drm_atomic_state_alloc(dev);
13026 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13031 state->acquire_ctx = dev->mode_config.acquire_ctx;
13033 /* The force restore path in the HW readout code relies on the staged
13034 * config still keeping the user requested config while the actual
13035 * state has been overwritten by the configuration read from HW. We
13036 * need to copy the staged config to the atomic state, otherwise the
13037 * mode set will just reapply the state the HW is already in. */
13038 for_each_intel_encoder(dev, encoder) {
13039 if (&encoder->new_crtc->base != crtc)
13042 for_each_intel_connector(dev, connector) {
13043 if (connector->new_encoder != encoder)
13046 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13047 if (IS_ERR(connector_state)) {
13048 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13049 connector->base.base.id,
13050 connector->base.name,
13051 PTR_ERR(connector_state));
13055 connector_state->crtc = crtc;
13056 connector_state->best_encoder = &encoder->base;
13060 for_each_intel_crtc(dev, intel_crtc) {
13061 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13064 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13065 if (IS_ERR(crtc_state)) {
13066 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13067 intel_crtc->base.base.id,
13068 PTR_ERR(crtc_state));
13072 crtc_state->base.active = crtc_state->base.enable =
13073 intel_crtc->new_enabled;
13075 if (&intel_crtc->base == crtc)
13076 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
13079 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13080 crtc->primary->fb, crtc->x, crtc->y);
13082 ret = intel_set_mode(crtc, state);
13084 drm_atomic_state_free(state);
13087 #undef for_each_intel_crtc_masked
13089 static bool intel_connector_in_mode_set(struct intel_connector *connector,
13090 struct drm_mode_set *set)
13094 for (ro = 0; ro < set->num_connectors; ro++)
13095 if (set->connectors[ro] == &connector->base)
13102 intel_modeset_stage_output_state(struct drm_device *dev,
13103 struct drm_mode_set *set,
13104 struct drm_atomic_state *state)
13106 struct intel_connector *connector;
13107 struct drm_connector *drm_connector;
13108 struct drm_connector_state *connector_state;
13109 struct drm_crtc *crtc;
13110 struct drm_crtc_state *crtc_state;
13113 /* The upper layers ensure that we either disable a crtc or have a list
13114 * of connectors. For paranoia, double-check this. */
13115 WARN_ON(!set->fb && (set->num_connectors != 0));
13116 WARN_ON(set->fb && (set->num_connectors == 0));
13118 for_each_intel_connector(dev, connector) {
13119 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13121 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13125 drm_atomic_get_connector_state(state, &connector->base);
13126 if (IS_ERR(connector_state))
13127 return PTR_ERR(connector_state);
13130 int pipe = to_intel_crtc(set->crtc)->pipe;
13131 connector_state->best_encoder =
13132 &intel_find_encoder(connector, pipe)->base;
13135 if (connector->base.state->crtc != set->crtc)
13138 /* If we disable the crtc, disable all its connectors. Also, if
13139 * the connector is on the changing crtc but not on the new
13140 * connector list, disable it. */
13141 if (!set->fb || !in_mode_set) {
13142 connector_state->best_encoder = NULL;
13144 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13145 connector->base.base.id,
13146 connector->base.name);
13149 /* connector->new_encoder is now updated for all connectors. */
13151 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13152 connector = to_intel_connector(drm_connector);
13154 if (!connector_state->best_encoder) {
13155 ret = drm_atomic_set_crtc_for_connector(connector_state,
13163 if (intel_connector_in_mode_set(connector, set)) {
13164 struct drm_crtc *crtc = connector->base.state->crtc;
13166 /* If this connector was in a previous crtc, add it
13167 * to the state. We might need to disable it. */
13170 drm_atomic_get_crtc_state(state, crtc);
13171 if (IS_ERR(crtc_state))
13172 return PTR_ERR(crtc_state);
13175 ret = drm_atomic_set_crtc_for_connector(connector_state,
13181 /* Make sure the new CRTC will work with the encoder */
13182 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13183 connector_state->crtc)) {
13187 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13188 connector->base.base.id,
13189 connector->base.name,
13190 connector_state->crtc->base.id);
13192 if (connector_state->best_encoder != &connector->encoder->base)
13193 connector->encoder =
13194 to_intel_encoder(connector_state->best_encoder);
13197 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13198 bool has_connectors;
13200 ret = drm_atomic_add_affected_connectors(state, crtc);
13204 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13205 if (has_connectors != crtc_state->enable)
13206 crtc_state->enable =
13207 crtc_state->active = has_connectors;
13210 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13211 set->fb, set->x, set->y);
13215 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13216 if (IS_ERR(crtc_state))
13217 return PTR_ERR(crtc_state);
13220 drm_mode_copy(&crtc_state->mode, set->mode);
13222 if (set->num_connectors)
13223 crtc_state->active = true;
13228 static bool primary_plane_visible(struct drm_crtc *crtc)
13230 struct intel_plane_state *plane_state =
13231 to_intel_plane_state(crtc->primary->state);
13233 return plane_state->visible;
13236 static int intel_crtc_set_config(struct drm_mode_set *set)
13238 struct drm_device *dev;
13239 struct drm_atomic_state *state = NULL;
13240 struct intel_crtc_state *pipe_config;
13241 bool primary_plane_was_visible;
13245 BUG_ON(!set->crtc);
13246 BUG_ON(!set->crtc->helper_private);
13248 /* Enforce sane interface api - has been abused by the fb helper. */
13249 BUG_ON(!set->mode && set->fb);
13250 BUG_ON(set->fb && set->num_connectors == 0);
13253 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13254 set->crtc->base.id, set->fb->base.id,
13255 (int)set->num_connectors, set->x, set->y);
13257 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
13260 dev = set->crtc->dev;
13262 state = drm_atomic_state_alloc(dev);
13266 state->acquire_ctx = dev->mode_config.acquire_ctx;
13268 ret = intel_modeset_stage_output_state(dev, set, state);
13272 pipe_config = intel_modeset_compute_config(set->crtc, state);
13273 if (IS_ERR(pipe_config)) {
13274 ret = PTR_ERR(pipe_config);
13278 intel_update_pipe_size(to_intel_crtc(set->crtc));
13280 primary_plane_was_visible = primary_plane_visible(set->crtc);
13282 ret = intel_set_mode_with_config(set->crtc, pipe_config);
13285 pipe_config->base.enable &&
13286 pipe_config->base.planes_changed &&
13287 !needs_modeset(&pipe_config->base)) {
13288 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
13291 * We need to make sure the primary plane is re-enabled if it
13292 * has previously been turned off.
13294 if (ret == 0 && !primary_plane_was_visible &&
13295 primary_plane_visible(set->crtc)) {
13296 WARN_ON(!intel_crtc->active);
13297 intel_post_enable_primary(set->crtc);
13301 * In the fastboot case this may be our only check of the
13302 * state after boot. It would be better to only do it on
13303 * the first update, but we don't have a nice way of doing that
13304 * (and really, set_config isn't used much for high freq page
13305 * flipping, so increasing its cost here shouldn't be a big
13308 if (i915.fastboot && ret == 0)
13309 intel_modeset_check_state(set->crtc->dev);
13313 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13314 set->crtc->base.id, ret);
13319 drm_atomic_state_free(state);
13323 static const struct drm_crtc_funcs intel_crtc_funcs = {
13324 .gamma_set = intel_crtc_gamma_set,
13325 .set_config = intel_crtc_set_config,
13326 .destroy = intel_crtc_destroy,
13327 .page_flip = intel_crtc_page_flip,
13328 .atomic_duplicate_state = intel_crtc_duplicate_state,
13329 .atomic_destroy_state = intel_crtc_destroy_state,
13332 static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13333 struct intel_shared_dpll *pll,
13334 struct intel_dpll_hw_state *hw_state)
13338 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
13341 val = I915_READ(PCH_DPLL(pll->id));
13342 hw_state->dpll = val;
13343 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13344 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
13346 return val & DPLL_VCO_ENABLE;
13349 static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13350 struct intel_shared_dpll *pll)
13352 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13353 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
13356 static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13357 struct intel_shared_dpll *pll)
13359 /* PCH refclock must be enabled first */
13360 ibx_assert_pch_refclk_enabled(dev_priv);
13362 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
13364 /* Wait for the clocks to stabilize. */
13365 POSTING_READ(PCH_DPLL(pll->id));
13368 /* The pixel multiplier can only be updated once the
13369 * DPLL is enabled and the clocks are stable.
13371 * So write it again.
13373 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
13374 POSTING_READ(PCH_DPLL(pll->id));
13378 static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13379 struct intel_shared_dpll *pll)
13381 struct drm_device *dev = dev_priv->dev;
13382 struct intel_crtc *crtc;
13384 /* Make sure no transcoder isn't still depending on us. */
13385 for_each_intel_crtc(dev, crtc) {
13386 if (intel_crtc_to_shared_dpll(crtc) == pll)
13387 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13390 I915_WRITE(PCH_DPLL(pll->id), 0);
13391 POSTING_READ(PCH_DPLL(pll->id));
13395 static char *ibx_pch_dpll_names[] = {
13400 static void ibx_pch_dpll_init(struct drm_device *dev)
13402 struct drm_i915_private *dev_priv = dev->dev_private;
13405 dev_priv->num_shared_dpll = 2;
13407 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13408 dev_priv->shared_dplls[i].id = i;
13409 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
13410 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
13411 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13412 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
13413 dev_priv->shared_dplls[i].get_hw_state =
13414 ibx_pch_dpll_get_hw_state;
13418 static void intel_shared_dpll_init(struct drm_device *dev)
13420 struct drm_i915_private *dev_priv = dev->dev_private;
13422 intel_update_cdclk(dev);
13425 intel_ddi_pll_init(dev);
13426 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
13427 ibx_pch_dpll_init(dev);
13429 dev_priv->num_shared_dpll = 0;
13431 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
13435 * intel_wm_need_update - Check whether watermarks need updating
13436 * @plane: drm plane
13437 * @state: new plane state
13439 * Check current plane state versus the new one to determine whether
13440 * watermarks need to be recalculated.
13442 * Returns true or false.
13444 bool intel_wm_need_update(struct drm_plane *plane,
13445 struct drm_plane_state *state)
13447 /* Update watermarks on tiling changes. */
13448 if (!plane->state->fb || !state->fb ||
13449 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13450 plane->state->rotation != state->rotation)
13457 * intel_prepare_plane_fb - Prepare fb for usage on plane
13458 * @plane: drm plane to prepare for
13459 * @fb: framebuffer to prepare for presentation
13461 * Prepares a framebuffer for usage on a display plane. Generally this
13462 * involves pinning the underlying object and updating the frontbuffer tracking
13463 * bits. Some older platforms need special physical address handling for
13466 * Returns 0 on success, negative error code on failure.
13469 intel_prepare_plane_fb(struct drm_plane *plane,
13470 struct drm_framebuffer *fb,
13471 const struct drm_plane_state *new_state)
13473 struct drm_device *dev = plane->dev;
13474 struct intel_plane *intel_plane = to_intel_plane(plane);
13475 enum pipe pipe = intel_plane->pipe;
13476 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13477 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13478 unsigned frontbuffer_bits = 0;
13484 switch (plane->type) {
13485 case DRM_PLANE_TYPE_PRIMARY:
13486 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13488 case DRM_PLANE_TYPE_CURSOR:
13489 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13491 case DRM_PLANE_TYPE_OVERLAY:
13492 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13496 mutex_lock(&dev->struct_mutex);
13498 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13499 INTEL_INFO(dev)->cursor_needs_physical) {
13500 int align = IS_I830(dev) ? 16 * 1024 : 256;
13501 ret = i915_gem_object_attach_phys(obj, align);
13503 DRM_DEBUG_KMS("failed to attach phys object\n");
13505 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
13509 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13511 mutex_unlock(&dev->struct_mutex);
13517 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13518 * @plane: drm plane to clean up for
13519 * @fb: old framebuffer that was on plane
13521 * Cleans up a framebuffer that has just been removed from a plane.
13524 intel_cleanup_plane_fb(struct drm_plane *plane,
13525 struct drm_framebuffer *fb,
13526 const struct drm_plane_state *old_state)
13528 struct drm_device *dev = plane->dev;
13529 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13534 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13535 !INTEL_INFO(dev)->cursor_needs_physical) {
13536 mutex_lock(&dev->struct_mutex);
13537 intel_unpin_fb_obj(fb, old_state);
13538 mutex_unlock(&dev->struct_mutex);
13543 skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13546 struct drm_device *dev;
13547 struct drm_i915_private *dev_priv;
13548 int crtc_clock, cdclk;
13550 if (!intel_crtc || !crtc_state)
13551 return DRM_PLANE_HELPER_NO_SCALING;
13553 dev = intel_crtc->base.dev;
13554 dev_priv = dev->dev_private;
13555 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13556 cdclk = dev_priv->display.get_display_clock_speed(dev);
13558 if (!crtc_clock || !cdclk)
13559 return DRM_PLANE_HELPER_NO_SCALING;
13562 * skl max scale is lower of:
13563 * close to 3 but not 3, -1 is for that purpose
13567 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13573 intel_check_primary_plane(struct drm_plane *plane,
13574 struct intel_plane_state *state)
13576 struct drm_device *dev = plane->dev;
13577 struct drm_i915_private *dev_priv = dev->dev_private;
13578 struct drm_crtc *crtc = state->base.crtc;
13579 struct intel_crtc *intel_crtc;
13580 struct intel_crtc_state *crtc_state;
13581 struct drm_framebuffer *fb = state->base.fb;
13582 struct drm_rect *dest = &state->dst;
13583 struct drm_rect *src = &state->src;
13584 const struct drm_rect *clip = &state->clip;
13585 bool can_position = false;
13586 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13587 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
13590 crtc = crtc ? crtc : plane->crtc;
13591 intel_crtc = to_intel_crtc(crtc);
13592 crtc_state = state->base.state ?
13593 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
13595 if (INTEL_INFO(dev)->gen >= 9) {
13596 /* use scaler when colorkey is not required */
13597 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13599 max_scale = skl_max_scale(intel_crtc, crtc_state);
13601 can_position = true;
13604 ret = drm_plane_helper_check_update(plane, crtc, fb,
13608 can_position, true,
13613 if (intel_crtc->active) {
13614 struct intel_plane_state *old_state =
13615 to_intel_plane_state(plane->state);
13617 intel_crtc->atomic.wait_for_flips = true;
13620 * FBC does not work on some platforms for rotated
13621 * planes, so disable it when rotation is not 0 and
13622 * update it when rotation is set back to 0.
13624 * FIXME: This is redundant with the fbc update done in
13625 * the primary plane enable function except that that
13626 * one is done too late. We eventually need to unify
13629 if (state->visible &&
13630 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
13631 dev_priv->fbc.crtc == intel_crtc &&
13632 state->base.rotation != BIT(DRM_ROTATE_0)) {
13633 intel_crtc->atomic.disable_fbc = true;
13636 if (state->visible && !old_state->visible) {
13638 * BDW signals flip done immediately if the plane
13639 * is disabled, even if the plane enable is already
13640 * armed to occur at the next vblank :(
13642 if (IS_BROADWELL(dev))
13643 intel_crtc->atomic.wait_vblank = true;
13646 intel_crtc->atomic.fb_bits |=
13647 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13649 intel_crtc->atomic.update_fbc = true;
13651 if (intel_wm_need_update(plane, &state->base))
13652 intel_crtc->atomic.update_wm = true;
13655 if (INTEL_INFO(dev)->gen >= 9) {
13656 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13657 to_intel_plane(plane), state, 0);
13666 intel_commit_primary_plane(struct drm_plane *plane,
13667 struct intel_plane_state *state)
13669 struct drm_crtc *crtc = state->base.crtc;
13670 struct drm_framebuffer *fb = state->base.fb;
13671 struct drm_device *dev = plane->dev;
13672 struct drm_i915_private *dev_priv = dev->dev_private;
13673 struct intel_crtc *intel_crtc;
13674 struct drm_rect *src = &state->src;
13676 crtc = crtc ? crtc : plane->crtc;
13677 intel_crtc = to_intel_crtc(crtc);
13680 crtc->x = src->x1 >> 16;
13681 crtc->y = src->y1 >> 16;
13683 if (intel_crtc->active) {
13684 if (state->visible)
13685 /* FIXME: kill this fastboot hack */
13686 intel_update_pipe_size(intel_crtc);
13688 dev_priv->display.update_primary_plane(crtc, plane->fb,
13694 intel_disable_primary_plane(struct drm_plane *plane,
13695 struct drm_crtc *crtc,
13698 struct drm_device *dev = plane->dev;
13699 struct drm_i915_private *dev_priv = dev->dev_private;
13701 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13704 static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13706 struct drm_device *dev = crtc->dev;
13707 struct drm_i915_private *dev_priv = dev->dev_private;
13708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13709 struct intel_plane *intel_plane;
13710 struct drm_plane *p;
13711 unsigned fb_bits = 0;
13713 /* Track fb's for any planes being disabled */
13714 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13715 intel_plane = to_intel_plane(p);
13717 if (intel_crtc->atomic.disabled_planes &
13718 (1 << drm_plane_index(p))) {
13720 case DRM_PLANE_TYPE_PRIMARY:
13721 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13723 case DRM_PLANE_TYPE_CURSOR:
13724 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13726 case DRM_PLANE_TYPE_OVERLAY:
13727 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13731 mutex_lock(&dev->struct_mutex);
13732 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13733 mutex_unlock(&dev->struct_mutex);
13737 if (intel_crtc->atomic.wait_for_flips)
13738 intel_crtc_wait_for_pending_flips(crtc);
13740 if (intel_crtc->atomic.disable_fbc)
13741 intel_fbc_disable(dev);
13743 if (intel_crtc->atomic.pre_disable_primary)
13744 intel_pre_disable_primary(crtc);
13746 if (intel_crtc->atomic.update_wm)
13747 intel_update_watermarks(crtc);
13749 intel_runtime_pm_get(dev_priv);
13751 /* Perform vblank evasion around commit operation */
13752 if (intel_crtc->active)
13753 intel_crtc->atomic.evade =
13754 intel_pipe_update_start(intel_crtc,
13755 &intel_crtc->atomic.start_vbl_count);
13758 static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13760 struct drm_device *dev = crtc->dev;
13761 struct drm_i915_private *dev_priv = dev->dev_private;
13762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13763 struct drm_plane *p;
13765 if (intel_crtc->atomic.evade)
13766 intel_pipe_update_end(intel_crtc,
13767 intel_crtc->atomic.start_vbl_count);
13769 intel_runtime_pm_put(dev_priv);
13771 if (intel_crtc->atomic.wait_vblank)
13772 intel_wait_for_vblank(dev, intel_crtc->pipe);
13774 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13776 if (intel_crtc->atomic.update_fbc) {
13777 mutex_lock(&dev->struct_mutex);
13778 intel_fbc_update(dev);
13779 mutex_unlock(&dev->struct_mutex);
13782 if (intel_crtc->atomic.post_enable_primary)
13783 intel_post_enable_primary(crtc);
13785 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13786 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13787 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13790 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
13794 * intel_plane_destroy - destroy a plane
13795 * @plane: plane to destroy
13797 * Common destruction function for all types of planes (primary, cursor,
13800 void intel_plane_destroy(struct drm_plane *plane)
13802 struct intel_plane *intel_plane = to_intel_plane(plane);
13803 drm_plane_cleanup(plane);
13804 kfree(intel_plane);
13807 const struct drm_plane_funcs intel_plane_funcs = {
13808 .update_plane = drm_atomic_helper_update_plane,
13809 .disable_plane = drm_atomic_helper_disable_plane,
13810 .destroy = intel_plane_destroy,
13811 .set_property = drm_atomic_helper_plane_set_property,
13812 .atomic_get_property = intel_plane_atomic_get_property,
13813 .atomic_set_property = intel_plane_atomic_set_property,
13814 .atomic_duplicate_state = intel_plane_duplicate_state,
13815 .atomic_destroy_state = intel_plane_destroy_state,
13819 static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13822 struct intel_plane *primary;
13823 struct intel_plane_state *state;
13824 const uint32_t *intel_primary_formats;
13827 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13828 if (primary == NULL)
13831 state = intel_create_plane_state(&primary->base);
13836 primary->base.state = &state->base;
13838 primary->can_scale = false;
13839 primary->max_downscale = 1;
13840 if (INTEL_INFO(dev)->gen >= 9) {
13841 primary->can_scale = true;
13842 state->scaler_id = -1;
13844 primary->pipe = pipe;
13845 primary->plane = pipe;
13846 primary->check_plane = intel_check_primary_plane;
13847 primary->commit_plane = intel_commit_primary_plane;
13848 primary->disable_plane = intel_disable_primary_plane;
13849 primary->ckey.flags = I915_SET_COLORKEY_NONE;
13850 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13851 primary->plane = !pipe;
13853 if (INTEL_INFO(dev)->gen >= 9) {
13854 intel_primary_formats = skl_primary_formats;
13855 num_formats = ARRAY_SIZE(skl_primary_formats);
13856 } else if (INTEL_INFO(dev)->gen >= 4) {
13857 intel_primary_formats = i965_primary_formats;
13858 num_formats = ARRAY_SIZE(i965_primary_formats);
13860 intel_primary_formats = i8xx_primary_formats;
13861 num_formats = ARRAY_SIZE(i8xx_primary_formats);
13864 drm_universal_plane_init(dev, &primary->base, 0,
13865 &intel_plane_funcs,
13866 intel_primary_formats, num_formats,
13867 DRM_PLANE_TYPE_PRIMARY);
13869 if (INTEL_INFO(dev)->gen >= 4)
13870 intel_create_rotation_property(dev, primary);
13872 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13874 return &primary->base;
13877 void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13879 if (!dev->mode_config.rotation_property) {
13880 unsigned long flags = BIT(DRM_ROTATE_0) |
13881 BIT(DRM_ROTATE_180);
13883 if (INTEL_INFO(dev)->gen >= 9)
13884 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13886 dev->mode_config.rotation_property =
13887 drm_mode_create_rotation_property(dev, flags);
13889 if (dev->mode_config.rotation_property)
13890 drm_object_attach_property(&plane->base.base,
13891 dev->mode_config.rotation_property,
13892 plane->base.state->rotation);
13896 intel_check_cursor_plane(struct drm_plane *plane,
13897 struct intel_plane_state *state)
13899 struct drm_crtc *crtc = state->base.crtc;
13900 struct drm_device *dev = plane->dev;
13901 struct drm_framebuffer *fb = state->base.fb;
13902 struct drm_rect *dest = &state->dst;
13903 struct drm_rect *src = &state->src;
13904 const struct drm_rect *clip = &state->clip;
13905 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13906 struct intel_crtc *intel_crtc;
13910 crtc = crtc ? crtc : plane->crtc;
13911 intel_crtc = to_intel_crtc(crtc);
13913 ret = drm_plane_helper_check_update(plane, crtc, fb,
13915 DRM_PLANE_HELPER_NO_SCALING,
13916 DRM_PLANE_HELPER_NO_SCALING,
13917 true, true, &state->visible);
13922 /* if we want to turn off the cursor ignore width and height */
13926 /* Check for which cursor types we support */
13927 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13928 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13929 state->base.crtc_w, state->base.crtc_h);
13933 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13934 if (obj->base.size < stride * state->base.crtc_h) {
13935 DRM_DEBUG_KMS("buffer is too small\n");
13939 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
13940 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13945 if (intel_crtc->active) {
13946 if (plane->state->crtc_w != state->base.crtc_w)
13947 intel_crtc->atomic.update_wm = true;
13949 intel_crtc->atomic.fb_bits |=
13950 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13957 intel_disable_cursor_plane(struct drm_plane *plane,
13958 struct drm_crtc *crtc,
13961 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13965 intel_crtc->cursor_bo = NULL;
13966 intel_crtc->cursor_addr = 0;
13969 intel_crtc_update_cursor(crtc, false);
13973 intel_commit_cursor_plane(struct drm_plane *plane,
13974 struct intel_plane_state *state)
13976 struct drm_crtc *crtc = state->base.crtc;
13977 struct drm_device *dev = plane->dev;
13978 struct intel_crtc *intel_crtc;
13979 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
13982 crtc = crtc ? crtc : plane->crtc;
13983 intel_crtc = to_intel_crtc(crtc);
13985 plane->fb = state->base.fb;
13986 crtc->cursor_x = state->base.crtc_x;
13987 crtc->cursor_y = state->base.crtc_y;
13989 if (intel_crtc->cursor_bo == obj)
13994 else if (!INTEL_INFO(dev)->cursor_needs_physical)
13995 addr = i915_gem_obj_ggtt_offset(obj);
13997 addr = obj->phys_handle->busaddr;
13999 intel_crtc->cursor_addr = addr;
14000 intel_crtc->cursor_bo = obj;
14003 if (intel_crtc->active)
14004 intel_crtc_update_cursor(crtc, state->visible);
14007 static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14010 struct intel_plane *cursor;
14011 struct intel_plane_state *state;
14013 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14014 if (cursor == NULL)
14017 state = intel_create_plane_state(&cursor->base);
14022 cursor->base.state = &state->base;
14024 cursor->can_scale = false;
14025 cursor->max_downscale = 1;
14026 cursor->pipe = pipe;
14027 cursor->plane = pipe;
14028 cursor->check_plane = intel_check_cursor_plane;
14029 cursor->commit_plane = intel_commit_cursor_plane;
14030 cursor->disable_plane = intel_disable_cursor_plane;
14032 drm_universal_plane_init(dev, &cursor->base, 0,
14033 &intel_plane_funcs,
14034 intel_cursor_formats,
14035 ARRAY_SIZE(intel_cursor_formats),
14036 DRM_PLANE_TYPE_CURSOR);
14038 if (INTEL_INFO(dev)->gen >= 4) {
14039 if (!dev->mode_config.rotation_property)
14040 dev->mode_config.rotation_property =
14041 drm_mode_create_rotation_property(dev,
14042 BIT(DRM_ROTATE_0) |
14043 BIT(DRM_ROTATE_180));
14044 if (dev->mode_config.rotation_property)
14045 drm_object_attach_property(&cursor->base.base,
14046 dev->mode_config.rotation_property,
14047 state->base.rotation);
14050 if (INTEL_INFO(dev)->gen >=9)
14051 state->scaler_id = -1;
14053 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14055 return &cursor->base;
14058 static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14059 struct intel_crtc_state *crtc_state)
14062 struct intel_scaler *intel_scaler;
14063 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14065 for (i = 0; i < intel_crtc->num_scalers; i++) {
14066 intel_scaler = &scaler_state->scalers[i];
14067 intel_scaler->in_use = 0;
14068 intel_scaler->id = i;
14070 intel_scaler->mode = PS_SCALER_MODE_DYN;
14073 scaler_state->scaler_id = -1;
14076 static void intel_crtc_init(struct drm_device *dev, int pipe)
14078 struct drm_i915_private *dev_priv = dev->dev_private;
14079 struct intel_crtc *intel_crtc;
14080 struct intel_crtc_state *crtc_state = NULL;
14081 struct drm_plane *primary = NULL;
14082 struct drm_plane *cursor = NULL;
14085 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
14086 if (intel_crtc == NULL)
14089 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14092 intel_crtc->config = crtc_state;
14093 intel_crtc->base.state = &crtc_state->base;
14094 crtc_state->base.crtc = &intel_crtc->base;
14096 /* initialize shared scalers */
14097 if (INTEL_INFO(dev)->gen >= 9) {
14098 if (pipe == PIPE_C)
14099 intel_crtc->num_scalers = 1;
14101 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14103 skl_init_scalers(dev, intel_crtc, crtc_state);
14106 primary = intel_primary_plane_create(dev, pipe);
14110 cursor = intel_cursor_plane_create(dev, pipe);
14114 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
14115 cursor, &intel_crtc_funcs);
14119 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
14120 for (i = 0; i < 256; i++) {
14121 intel_crtc->lut_r[i] = i;
14122 intel_crtc->lut_g[i] = i;
14123 intel_crtc->lut_b[i] = i;
14127 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
14128 * is hooked to pipe B. Hence we want plane A feeding pipe B.
14130 intel_crtc->pipe = pipe;
14131 intel_crtc->plane = pipe;
14132 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
14133 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
14134 intel_crtc->plane = !pipe;
14137 intel_crtc->cursor_base = ~0;
14138 intel_crtc->cursor_cntl = ~0;
14139 intel_crtc->cursor_size = ~0;
14141 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14142 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14143 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14144 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14146 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
14148 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
14153 drm_plane_cleanup(primary);
14155 drm_plane_cleanup(cursor);
14160 enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14162 struct drm_encoder *encoder = connector->base.encoder;
14163 struct drm_device *dev = connector->base.dev;
14165 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
14167 if (!encoder || WARN_ON(!encoder->crtc))
14168 return INVALID_PIPE;
14170 return to_intel_crtc(encoder->crtc)->pipe;
14173 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
14174 struct drm_file *file)
14176 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
14177 struct drm_crtc *drmmode_crtc;
14178 struct intel_crtc *crtc;
14180 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
14182 if (!drmmode_crtc) {
14183 DRM_ERROR("no such CRTC id\n");
14187 crtc = to_intel_crtc(drmmode_crtc);
14188 pipe_from_crtc_id->pipe = crtc->pipe;
14193 static int intel_encoder_clones(struct intel_encoder *encoder)
14195 struct drm_device *dev = encoder->base.dev;
14196 struct intel_encoder *source_encoder;
14197 int index_mask = 0;
14200 for_each_intel_encoder(dev, source_encoder) {
14201 if (encoders_cloneable(encoder, source_encoder))
14202 index_mask |= (1 << entry);
14210 static bool has_edp_a(struct drm_device *dev)
14212 struct drm_i915_private *dev_priv = dev->dev_private;
14214 if (!IS_MOBILE(dev))
14217 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14220 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
14226 static bool intel_crt_present(struct drm_device *dev)
14228 struct drm_i915_private *dev_priv = dev->dev_private;
14230 if (INTEL_INFO(dev)->gen >= 9)
14233 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
14236 if (IS_CHERRYVIEW(dev))
14239 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14245 static void intel_setup_outputs(struct drm_device *dev)
14247 struct drm_i915_private *dev_priv = dev->dev_private;
14248 struct intel_encoder *encoder;
14249 bool dpd_is_edp = false;
14251 intel_lvds_init(dev);
14253 if (intel_crt_present(dev))
14254 intel_crt_init(dev);
14256 if (IS_BROXTON(dev)) {
14258 * FIXME: Broxton doesn't support port detection via the
14259 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14260 * detect the ports.
14262 intel_ddi_init(dev, PORT_A);
14263 intel_ddi_init(dev, PORT_B);
14264 intel_ddi_init(dev, PORT_C);
14265 } else if (HAS_DDI(dev)) {
14269 * Haswell uses DDI functions to detect digital outputs.
14270 * On SKL pre-D0 the strap isn't connected, so we assume
14273 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
14274 /* WaIgnoreDDIAStrap: skl */
14276 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
14277 intel_ddi_init(dev, PORT_A);
14279 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14281 found = I915_READ(SFUSE_STRAP);
14283 if (found & SFUSE_STRAP_DDIB_DETECTED)
14284 intel_ddi_init(dev, PORT_B);
14285 if (found & SFUSE_STRAP_DDIC_DETECTED)
14286 intel_ddi_init(dev, PORT_C);
14287 if (found & SFUSE_STRAP_DDID_DETECTED)
14288 intel_ddi_init(dev, PORT_D);
14289 } else if (HAS_PCH_SPLIT(dev)) {
14291 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
14293 if (has_edp_a(dev))
14294 intel_dp_init(dev, DP_A, PORT_A);
14296 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
14297 /* PCH SDVOB multiplex with HDMIB */
14298 found = intel_sdvo_init(dev, PCH_SDVOB, true);
14300 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
14301 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
14302 intel_dp_init(dev, PCH_DP_B, PORT_B);
14305 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
14306 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
14308 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
14309 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
14311 if (I915_READ(PCH_DP_C) & DP_DETECTED)
14312 intel_dp_init(dev, PCH_DP_C, PORT_C);
14314 if (I915_READ(PCH_DP_D) & DP_DETECTED)
14315 intel_dp_init(dev, PCH_DP_D, PORT_D);
14316 } else if (IS_VALLEYVIEW(dev)) {
14318 * The DP_DETECTED bit is the latched state of the DDC
14319 * SDA pin at boot. However since eDP doesn't require DDC
14320 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14321 * eDP ports may have been muxed to an alternate function.
14322 * Thus we can't rely on the DP_DETECTED bit alone to detect
14323 * eDP ports. Consult the VBT as well as DP_DETECTED to
14324 * detect eDP ports.
14326 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14327 !intel_dp_is_edp(dev, PORT_B))
14328 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14330 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14331 intel_dp_is_edp(dev, PORT_B))
14332 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
14334 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14335 !intel_dp_is_edp(dev, PORT_C))
14336 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14338 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14339 intel_dp_is_edp(dev, PORT_C))
14340 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
14342 if (IS_CHERRYVIEW(dev)) {
14343 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
14344 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14346 /* eDP not supported on port D, so don't check VBT */
14347 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14348 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
14351 intel_dsi_init(dev);
14352 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
14353 bool found = false;
14355 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
14356 DRM_DEBUG_KMS("probing SDVOB\n");
14357 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
14358 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14359 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
14360 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
14363 if (!found && SUPPORTS_INTEGRATED_DP(dev))
14364 intel_dp_init(dev, DP_B, PORT_B);
14367 /* Before G4X SDVOC doesn't have its own detect register */
14369 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
14370 DRM_DEBUG_KMS("probing SDVOC\n");
14371 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
14374 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
14376 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14377 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
14378 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
14380 if (SUPPORTS_INTEGRATED_DP(dev))
14381 intel_dp_init(dev, DP_C, PORT_C);
14384 if (SUPPORTS_INTEGRATED_DP(dev) &&
14385 (I915_READ(DP_D) & DP_DETECTED))
14386 intel_dp_init(dev, DP_D, PORT_D);
14387 } else if (IS_GEN2(dev))
14388 intel_dvo_init(dev);
14390 if (SUPPORTS_TV(dev))
14391 intel_tv_init(dev);
14393 intel_psr_init(dev);
14395 for_each_intel_encoder(dev, encoder) {
14396 encoder->base.possible_crtcs = encoder->crtc_mask;
14397 encoder->base.possible_clones =
14398 intel_encoder_clones(encoder);
14401 intel_init_pch_refclk(dev);
14403 drm_helper_move_panel_connectors_to_head(dev);
14406 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14408 struct drm_device *dev = fb->dev;
14409 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14411 drm_framebuffer_cleanup(fb);
14412 mutex_lock(&dev->struct_mutex);
14413 WARN_ON(!intel_fb->obj->framebuffer_references--);
14414 drm_gem_object_unreference(&intel_fb->obj->base);
14415 mutex_unlock(&dev->struct_mutex);
14419 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
14420 struct drm_file *file,
14421 unsigned int *handle)
14423 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14424 struct drm_i915_gem_object *obj = intel_fb->obj;
14426 return drm_gem_handle_create(file, &obj->base, handle);
14429 static const struct drm_framebuffer_funcs intel_fb_funcs = {
14430 .destroy = intel_user_framebuffer_destroy,
14431 .create_handle = intel_user_framebuffer_create_handle,
14435 u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14436 uint32_t pixel_format)
14438 u32 gen = INTEL_INFO(dev)->gen;
14441 /* "The stride in bytes must not exceed the of the size of 8K
14442 * pixels and 32K bytes."
14444 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14445 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14447 } else if (gen >= 4) {
14448 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14452 } else if (gen >= 3) {
14453 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14458 /* XXX DSPC is limited to 4k tiled */
14463 static int intel_framebuffer_init(struct drm_device *dev,
14464 struct intel_framebuffer *intel_fb,
14465 struct drm_mode_fb_cmd2 *mode_cmd,
14466 struct drm_i915_gem_object *obj)
14468 unsigned int aligned_height;
14470 u32 pitch_limit, stride_alignment;
14472 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14474 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14475 /* Enforce that fb modifier and tiling mode match, but only for
14476 * X-tiled. This is needed for FBC. */
14477 if (!!(obj->tiling_mode == I915_TILING_X) !=
14478 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14479 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14483 if (obj->tiling_mode == I915_TILING_X)
14484 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14485 else if (obj->tiling_mode == I915_TILING_Y) {
14486 DRM_DEBUG("No Y tiling for legacy addfb\n");
14491 /* Passed in modifier sanity checking. */
14492 switch (mode_cmd->modifier[0]) {
14493 case I915_FORMAT_MOD_Y_TILED:
14494 case I915_FORMAT_MOD_Yf_TILED:
14495 if (INTEL_INFO(dev)->gen < 9) {
14496 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14497 mode_cmd->modifier[0]);
14500 case DRM_FORMAT_MOD_NONE:
14501 case I915_FORMAT_MOD_X_TILED:
14504 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14505 mode_cmd->modifier[0]);
14509 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14510 mode_cmd->pixel_format);
14511 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14512 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14513 mode_cmd->pitches[0], stride_alignment);
14517 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14518 mode_cmd->pixel_format);
14519 if (mode_cmd->pitches[0] > pitch_limit) {
14520 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14521 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
14522 "tiled" : "linear",
14523 mode_cmd->pitches[0], pitch_limit);
14527 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
14528 mode_cmd->pitches[0] != obj->stride) {
14529 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14530 mode_cmd->pitches[0], obj->stride);
14534 /* Reject formats not supported by any plane early. */
14535 switch (mode_cmd->pixel_format) {
14536 case DRM_FORMAT_C8:
14537 case DRM_FORMAT_RGB565:
14538 case DRM_FORMAT_XRGB8888:
14539 case DRM_FORMAT_ARGB8888:
14541 case DRM_FORMAT_XRGB1555:
14542 if (INTEL_INFO(dev)->gen > 3) {
14543 DRM_DEBUG("unsupported pixel format: %s\n",
14544 drm_get_format_name(mode_cmd->pixel_format));
14548 case DRM_FORMAT_ABGR8888:
14549 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14550 DRM_DEBUG("unsupported pixel format: %s\n",
14551 drm_get_format_name(mode_cmd->pixel_format));
14555 case DRM_FORMAT_XBGR8888:
14556 case DRM_FORMAT_XRGB2101010:
14557 case DRM_FORMAT_XBGR2101010:
14558 if (INTEL_INFO(dev)->gen < 4) {
14559 DRM_DEBUG("unsupported pixel format: %s\n",
14560 drm_get_format_name(mode_cmd->pixel_format));
14564 case DRM_FORMAT_ABGR2101010:
14565 if (!IS_VALLEYVIEW(dev)) {
14566 DRM_DEBUG("unsupported pixel format: %s\n",
14567 drm_get_format_name(mode_cmd->pixel_format));
14571 case DRM_FORMAT_YUYV:
14572 case DRM_FORMAT_UYVY:
14573 case DRM_FORMAT_YVYU:
14574 case DRM_FORMAT_VYUY:
14575 if (INTEL_INFO(dev)->gen < 5) {
14576 DRM_DEBUG("unsupported pixel format: %s\n",
14577 drm_get_format_name(mode_cmd->pixel_format));
14582 DRM_DEBUG("unsupported pixel format: %s\n",
14583 drm_get_format_name(mode_cmd->pixel_format));
14587 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14588 if (mode_cmd->offsets[0] != 0)
14591 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
14592 mode_cmd->pixel_format,
14593 mode_cmd->modifier[0]);
14594 /* FIXME drm helper for size checks (especially planar formats)? */
14595 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14598 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14599 intel_fb->obj = obj;
14600 intel_fb->obj->framebuffer_references++;
14602 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14604 DRM_ERROR("framebuffer init failed %d\n", ret);
14611 static struct drm_framebuffer *
14612 intel_user_framebuffer_create(struct drm_device *dev,
14613 struct drm_file *filp,
14614 struct drm_mode_fb_cmd2 *mode_cmd)
14616 struct drm_i915_gem_object *obj;
14618 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14619 mode_cmd->handles[0]));
14620 if (&obj->base == NULL)
14621 return ERR_PTR(-ENOENT);
14623 return intel_framebuffer_create(dev, mode_cmd, obj);
14626 #ifndef CONFIG_DRM_I915_FBDEV
14627 static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
14632 static const struct drm_mode_config_funcs intel_mode_funcs = {
14633 .fb_create = intel_user_framebuffer_create,
14634 .output_poll_changed = intel_fbdev_output_poll_changed,
14635 .atomic_check = intel_atomic_check,
14636 .atomic_commit = intel_atomic_commit,
14639 /* Set up chip specific display functions */
14640 static void intel_init_display(struct drm_device *dev)
14642 struct drm_i915_private *dev_priv = dev->dev_private;
14644 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14645 dev_priv->display.find_dpll = g4x_find_best_dpll;
14646 else if (IS_CHERRYVIEW(dev))
14647 dev_priv->display.find_dpll = chv_find_best_dpll;
14648 else if (IS_VALLEYVIEW(dev))
14649 dev_priv->display.find_dpll = vlv_find_best_dpll;
14650 else if (IS_PINEVIEW(dev))
14651 dev_priv->display.find_dpll = pnv_find_best_dpll;
14653 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14655 if (INTEL_INFO(dev)->gen >= 9) {
14656 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
14657 dev_priv->display.get_initial_plane_config =
14658 skylake_get_initial_plane_config;
14659 dev_priv->display.crtc_compute_clock =
14660 haswell_crtc_compute_clock;
14661 dev_priv->display.crtc_enable = haswell_crtc_enable;
14662 dev_priv->display.crtc_disable = haswell_crtc_disable;
14663 dev_priv->display.update_primary_plane =
14664 skylake_update_primary_plane;
14665 } else if (HAS_DDI(dev)) {
14666 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
14667 dev_priv->display.get_initial_plane_config =
14668 ironlake_get_initial_plane_config;
14669 dev_priv->display.crtc_compute_clock =
14670 haswell_crtc_compute_clock;
14671 dev_priv->display.crtc_enable = haswell_crtc_enable;
14672 dev_priv->display.crtc_disable = haswell_crtc_disable;
14673 dev_priv->display.update_primary_plane =
14674 ironlake_update_primary_plane;
14675 } else if (HAS_PCH_SPLIT(dev)) {
14676 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
14677 dev_priv->display.get_initial_plane_config =
14678 ironlake_get_initial_plane_config;
14679 dev_priv->display.crtc_compute_clock =
14680 ironlake_crtc_compute_clock;
14681 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14682 dev_priv->display.crtc_disable = ironlake_crtc_disable;
14683 dev_priv->display.update_primary_plane =
14684 ironlake_update_primary_plane;
14685 } else if (IS_VALLEYVIEW(dev)) {
14686 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14687 dev_priv->display.get_initial_plane_config =
14688 i9xx_get_initial_plane_config;
14689 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
14690 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14691 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14692 dev_priv->display.update_primary_plane =
14693 i9xx_update_primary_plane;
14695 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
14696 dev_priv->display.get_initial_plane_config =
14697 i9xx_get_initial_plane_config;
14698 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
14699 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14700 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14701 dev_priv->display.update_primary_plane =
14702 i9xx_update_primary_plane;
14705 /* Returns the core display clock speed */
14706 if (IS_SKYLAKE(dev))
14707 dev_priv->display.get_display_clock_speed =
14708 skylake_get_display_clock_speed;
14709 else if (IS_BROADWELL(dev))
14710 dev_priv->display.get_display_clock_speed =
14711 broadwell_get_display_clock_speed;
14712 else if (IS_HASWELL(dev))
14713 dev_priv->display.get_display_clock_speed =
14714 haswell_get_display_clock_speed;
14715 else if (IS_VALLEYVIEW(dev))
14716 dev_priv->display.get_display_clock_speed =
14717 valleyview_get_display_clock_speed;
14718 else if (IS_GEN5(dev))
14719 dev_priv->display.get_display_clock_speed =
14720 ilk_get_display_clock_speed;
14721 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14722 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
14723 dev_priv->display.get_display_clock_speed =
14724 i945_get_display_clock_speed;
14725 else if (IS_GM45(dev))
14726 dev_priv->display.get_display_clock_speed =
14727 gm45_get_display_clock_speed;
14728 else if (IS_CRESTLINE(dev))
14729 dev_priv->display.get_display_clock_speed =
14730 i965gm_get_display_clock_speed;
14731 else if (IS_PINEVIEW(dev))
14732 dev_priv->display.get_display_clock_speed =
14733 pnv_get_display_clock_speed;
14734 else if (IS_G33(dev) || IS_G4X(dev))
14735 dev_priv->display.get_display_clock_speed =
14736 g33_get_display_clock_speed;
14737 else if (IS_I915G(dev))
14738 dev_priv->display.get_display_clock_speed =
14739 i915_get_display_clock_speed;
14740 else if (IS_I945GM(dev) || IS_845G(dev))
14741 dev_priv->display.get_display_clock_speed =
14742 i9xx_misc_get_display_clock_speed;
14743 else if (IS_PINEVIEW(dev))
14744 dev_priv->display.get_display_clock_speed =
14745 pnv_get_display_clock_speed;
14746 else if (IS_I915GM(dev))
14747 dev_priv->display.get_display_clock_speed =
14748 i915gm_get_display_clock_speed;
14749 else if (IS_I865G(dev))
14750 dev_priv->display.get_display_clock_speed =
14751 i865_get_display_clock_speed;
14752 else if (IS_I85X(dev))
14753 dev_priv->display.get_display_clock_speed =
14754 i85x_get_display_clock_speed;
14756 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
14757 dev_priv->display.get_display_clock_speed =
14758 i830_get_display_clock_speed;
14761 if (IS_GEN5(dev)) {
14762 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
14763 } else if (IS_GEN6(dev)) {
14764 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
14765 } else if (IS_IVYBRIDGE(dev)) {
14766 /* FIXME: detect B0+ stepping and use auto training */
14767 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
14768 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
14769 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
14770 if (IS_BROADWELL(dev))
14771 dev_priv->display.modeset_global_resources =
14772 broadwell_modeset_global_resources;
14773 } else if (IS_VALLEYVIEW(dev)) {
14774 dev_priv->display.modeset_global_resources =
14775 valleyview_modeset_global_resources;
14776 } else if (IS_BROXTON(dev)) {
14777 dev_priv->display.modeset_global_resources =
14778 broxton_modeset_global_resources;
14781 switch (INTEL_INFO(dev)->gen) {
14783 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14787 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14792 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14796 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14799 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
14800 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14803 /* Drop through - unsupported since execlist only. */
14805 /* Default just returns -ENODEV to indicate unsupported */
14806 dev_priv->display.queue_flip = intel_default_queue_flip;
14809 intel_panel_init_backlight_funcs(dev);
14811 mutex_init(&dev_priv->pps_mutex);
14815 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14816 * resume, or other times. This quirk makes sure that's the case for
14817 * affected systems.
14819 static void quirk_pipea_force(struct drm_device *dev)
14821 struct drm_i915_private *dev_priv = dev->dev_private;
14823 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
14824 DRM_INFO("applying pipe a force quirk\n");
14827 static void quirk_pipeb_force(struct drm_device *dev)
14829 struct drm_i915_private *dev_priv = dev->dev_private;
14831 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14832 DRM_INFO("applying pipe b force quirk\n");
14836 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14838 static void quirk_ssc_force_disable(struct drm_device *dev)
14840 struct drm_i915_private *dev_priv = dev->dev_private;
14841 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
14842 DRM_INFO("applying lvds SSC disable quirk\n");
14846 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14849 static void quirk_invert_brightness(struct drm_device *dev)
14851 struct drm_i915_private *dev_priv = dev->dev_private;
14852 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
14853 DRM_INFO("applying inverted panel brightness quirk\n");
14856 /* Some VBT's incorrectly indicate no backlight is present */
14857 static void quirk_backlight_present(struct drm_device *dev)
14859 struct drm_i915_private *dev_priv = dev->dev_private;
14860 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14861 DRM_INFO("applying backlight present quirk\n");
14864 struct intel_quirk {
14866 int subsystem_vendor;
14867 int subsystem_device;
14868 void (*hook)(struct drm_device *dev);
14871 /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14872 struct intel_dmi_quirk {
14873 void (*hook)(struct drm_device *dev);
14874 const struct dmi_system_id (*dmi_id_list)[];
14877 static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14879 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14883 static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14885 .dmi_id_list = &(const struct dmi_system_id[]) {
14887 .callback = intel_dmi_reverse_brightness,
14888 .ident = "NCR Corporation",
14889 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14890 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14893 { } /* terminating entry */
14895 .hook = quirk_invert_brightness,
14899 static struct intel_quirk intel_quirks[] = {
14900 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14901 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14903 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14904 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14906 /* 830 needs to leave pipe A & dpll A up */
14907 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14909 /* 830 needs to leave pipe B & dpll B up */
14910 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14912 /* Lenovo U160 cannot use SSC on LVDS */
14913 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
14915 /* Sony Vaio Y cannot use SSC on LVDS */
14916 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
14918 /* Acer Aspire 5734Z must invert backlight brightness */
14919 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14921 /* Acer/eMachines G725 */
14922 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14924 /* Acer/eMachines e725 */
14925 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14927 /* Acer/Packard Bell NCL20 */
14928 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14930 /* Acer Aspire 4736Z */
14931 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
14933 /* Acer Aspire 5336 */
14934 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
14936 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14937 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
14939 /* Acer C720 Chromebook (Core i3 4005U) */
14940 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14942 /* Apple Macbook 2,1 (Core 2 T7400) */
14943 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14945 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14946 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
14948 /* HP Chromebook 14 (Celeron 2955U) */
14949 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
14951 /* Dell Chromebook 11 */
14952 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
14955 static void intel_init_quirks(struct drm_device *dev)
14957 struct pci_dev *d = dev->pdev;
14960 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14961 struct intel_quirk *q = &intel_quirks[i];
14963 if (d->device == q->device &&
14964 (d->subsystem_vendor == q->subsystem_vendor ||
14965 q->subsystem_vendor == PCI_ANY_ID) &&
14966 (d->subsystem_device == q->subsystem_device ||
14967 q->subsystem_device == PCI_ANY_ID))
14970 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14971 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14972 intel_dmi_quirks[i].hook(dev);
14976 /* Disable the VGA plane that we never use */
14977 static void i915_disable_vga(struct drm_device *dev)
14979 struct drm_i915_private *dev_priv = dev->dev_private;
14981 u32 vga_reg = i915_vgacntrl_reg(dev);
14983 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
14984 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
14985 outb(SR01, VGA_SR_INDEX);
14986 sr1 = inb(VGA_SR_DATA);
14987 outb(sr1 | 1<<5, VGA_SR_DATA);
14988 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14991 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
14992 POSTING_READ(vga_reg);
14995 void intel_modeset_init_hw(struct drm_device *dev)
14997 intel_update_cdclk(dev);
14998 intel_prepare_ddi(dev);
14999 intel_init_clock_gating(dev);
15000 intel_enable_gt_powersave(dev);
15003 void intel_modeset_init(struct drm_device *dev)
15005 struct drm_i915_private *dev_priv = dev->dev_private;
15008 struct intel_crtc *crtc;
15010 drm_mode_config_init(dev);
15012 dev->mode_config.min_width = 0;
15013 dev->mode_config.min_height = 0;
15015 dev->mode_config.preferred_depth = 24;
15016 dev->mode_config.prefer_shadow = 1;
15018 dev->mode_config.allow_fb_modifiers = true;
15020 dev->mode_config.funcs = &intel_mode_funcs;
15022 intel_init_quirks(dev);
15024 intel_init_pm(dev);
15026 if (INTEL_INFO(dev)->num_pipes == 0)
15029 intel_init_display(dev);
15030 intel_init_audio(dev);
15032 if (IS_GEN2(dev)) {
15033 dev->mode_config.max_width = 2048;
15034 dev->mode_config.max_height = 2048;
15035 } else if (IS_GEN3(dev)) {
15036 dev->mode_config.max_width = 4096;
15037 dev->mode_config.max_height = 4096;
15039 dev->mode_config.max_width = 8192;
15040 dev->mode_config.max_height = 8192;
15043 if (IS_845G(dev) || IS_I865G(dev)) {
15044 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15045 dev->mode_config.cursor_height = 1023;
15046 } else if (IS_GEN2(dev)) {
15047 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15048 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15050 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15051 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15054 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
15056 DRM_DEBUG_KMS("%d display pipe%s available.\n",
15057 INTEL_INFO(dev)->num_pipes,
15058 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
15060 for_each_pipe(dev_priv, pipe) {
15061 intel_crtc_init(dev, pipe);
15062 for_each_sprite(dev_priv, pipe, sprite) {
15063 ret = intel_plane_init(dev, pipe, sprite);
15065 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
15066 pipe_name(pipe), sprite_name(pipe, sprite), ret);
15070 intel_init_dpio(dev);
15072 intel_shared_dpll_init(dev);
15074 /* Just disable it once at startup */
15075 i915_disable_vga(dev);
15076 intel_setup_outputs(dev);
15078 /* Just in case the BIOS is doing something questionable. */
15079 intel_fbc_disable(dev);
15081 drm_modeset_lock_all(dev);
15082 intel_modeset_setup_hw_state(dev, false);
15083 drm_modeset_unlock_all(dev);
15085 for_each_intel_crtc(dev, crtc) {
15090 * Note that reserving the BIOS fb up front prevents us
15091 * from stuffing other stolen allocations like the ring
15092 * on top. This prevents some ugliness at boot time, and
15093 * can even allow for smooth boot transitions if the BIOS
15094 * fb is large enough for the active pipe configuration.
15096 if (dev_priv->display.get_initial_plane_config) {
15097 dev_priv->display.get_initial_plane_config(crtc,
15098 &crtc->plane_config);
15100 * If the fb is shared between multiple heads, we'll
15101 * just get the first one.
15103 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
15108 static void intel_enable_pipe_a(struct drm_device *dev)
15110 struct intel_connector *connector;
15111 struct drm_connector *crt = NULL;
15112 struct intel_load_detect_pipe load_detect_temp;
15113 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
15115 /* We can't just switch on the pipe A, we need to set things up with a
15116 * proper mode and output configuration. As a gross hack, enable pipe A
15117 * by enabling the load detect pipe once. */
15118 for_each_intel_connector(dev, connector) {
15119 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15120 crt = &connector->base;
15128 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
15129 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
15133 intel_check_plane_mapping(struct intel_crtc *crtc)
15135 struct drm_device *dev = crtc->base.dev;
15136 struct drm_i915_private *dev_priv = dev->dev_private;
15139 if (INTEL_INFO(dev)->num_pipes == 1)
15142 reg = DSPCNTR(!crtc->plane);
15143 val = I915_READ(reg);
15145 if ((val & DISPLAY_PLANE_ENABLE) &&
15146 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15152 static void intel_sanitize_crtc(struct intel_crtc *crtc)
15154 struct drm_device *dev = crtc->base.dev;
15155 struct drm_i915_private *dev_priv = dev->dev_private;
15158 /* Clear any frame start delays used for debugging left by the BIOS */
15159 reg = PIPECONF(crtc->config->cpu_transcoder);
15160 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15162 /* restore vblank interrupts to correct state */
15163 drm_crtc_vblank_reset(&crtc->base);
15164 if (crtc->active) {
15165 update_scanline_offset(crtc);
15166 drm_crtc_vblank_on(&crtc->base);
15169 /* We need to sanitize the plane -> pipe mapping first because this will
15170 * disable the crtc (and hence change the state) if it is wrong. Note
15171 * that gen4+ has a fixed plane -> pipe mapping. */
15172 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
15173 struct intel_connector *connector;
15176 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15177 crtc->base.base.id);
15179 /* Pipe has the wrong plane attached and the plane is active.
15180 * Temporarily change the plane mapping and disable everything
15182 plane = crtc->plane;
15183 to_intel_plane_state(crtc->base.primary->state)->visible = true;
15184 crtc->plane = !plane;
15185 intel_crtc_control(&crtc->base, false);
15186 crtc->plane = plane;
15188 /* ... and break all links. */
15189 for_each_intel_connector(dev, connector) {
15190 if (connector->encoder->base.crtc != &crtc->base)
15193 connector->base.dpms = DRM_MODE_DPMS_OFF;
15194 connector->base.encoder = NULL;
15196 /* multiple connectors may have the same encoder:
15197 * handle them and break crtc link separately */
15198 for_each_intel_connector(dev, connector)
15199 if (connector->encoder->base.crtc == &crtc->base) {
15200 connector->encoder->base.crtc = NULL;
15201 connector->encoder->connectors_active = false;
15204 WARN_ON(crtc->active);
15205 crtc->base.state->enable = false;
15206 crtc->base.state->active = false;
15207 crtc->base.enabled = false;
15210 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15211 crtc->pipe == PIPE_A && !crtc->active) {
15212 /* BIOS forgot to enable pipe A, this mostly happens after
15213 * resume. Force-enable the pipe to fix this, the update_dpms
15214 * call below we restore the pipe to the right state, but leave
15215 * the required bits on. */
15216 intel_enable_pipe_a(dev);
15219 /* Adjust the state of the output pipe according to whether we
15220 * have active connectors/encoders. */
15221 intel_crtc_update_dpms(&crtc->base);
15223 if (crtc->active != crtc->base.state->active) {
15224 struct intel_encoder *encoder;
15226 /* This can happen either due to bugs in the get_hw_state
15227 * functions or because the pipe is force-enabled due to the
15229 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15230 crtc->base.base.id,
15231 crtc->base.state->enable ? "enabled" : "disabled",
15232 crtc->active ? "enabled" : "disabled");
15234 crtc->base.state->enable = crtc->active;
15235 crtc->base.state->active = crtc->active;
15236 crtc->base.enabled = crtc->active;
15238 /* Because we only establish the connector -> encoder ->
15239 * crtc links if something is active, this means the
15240 * crtc is now deactivated. Break the links. connector
15241 * -> encoder links are only establish when things are
15242 * actually up, hence no need to break them. */
15243 WARN_ON(crtc->active);
15245 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15246 WARN_ON(encoder->connectors_active);
15247 encoder->base.crtc = NULL;
15251 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
15253 * We start out with underrun reporting disabled to avoid races.
15254 * For correct bookkeeping mark this on active crtcs.
15256 * Also on gmch platforms we dont have any hardware bits to
15257 * disable the underrun reporting. Which means we need to start
15258 * out with underrun reporting disabled also on inactive pipes,
15259 * since otherwise we'll complain about the garbage we read when
15260 * e.g. coming up after runtime pm.
15262 * No protection against concurrent access is required - at
15263 * worst a fifo underrun happens which also sets this to false.
15265 crtc->cpu_fifo_underrun_disabled = true;
15266 crtc->pch_fifo_underrun_disabled = true;
15270 static void intel_sanitize_encoder(struct intel_encoder *encoder)
15272 struct intel_connector *connector;
15273 struct drm_device *dev = encoder->base.dev;
15275 /* We need to check both for a crtc link (meaning that the
15276 * encoder is active and trying to read from a pipe) and the
15277 * pipe itself being active. */
15278 bool has_active_crtc = encoder->base.crtc &&
15279 to_intel_crtc(encoder->base.crtc)->active;
15281 if (encoder->connectors_active && !has_active_crtc) {
15282 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15283 encoder->base.base.id,
15284 encoder->base.name);
15286 /* Connector is active, but has no active pipe. This is
15287 * fallout from our resume register restoring. Disable
15288 * the encoder manually again. */
15289 if (encoder->base.crtc) {
15290 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15291 encoder->base.base.id,
15292 encoder->base.name);
15293 encoder->disable(encoder);
15294 if (encoder->post_disable)
15295 encoder->post_disable(encoder);
15297 encoder->base.crtc = NULL;
15298 encoder->connectors_active = false;
15300 /* Inconsistent output/port/pipe state happens presumably due to
15301 * a bug in one of the get_hw_state functions. Or someplace else
15302 * in our code, like the register restore mess on resume. Clamp
15303 * things to off as a safer default. */
15304 for_each_intel_connector(dev, connector) {
15305 if (connector->encoder != encoder)
15307 connector->base.dpms = DRM_MODE_DPMS_OFF;
15308 connector->base.encoder = NULL;
15311 /* Enabled encoders without active connectors will be fixed in
15312 * the crtc fixup. */
15315 void i915_redisable_vga_power_on(struct drm_device *dev)
15317 struct drm_i915_private *dev_priv = dev->dev_private;
15318 u32 vga_reg = i915_vgacntrl_reg(dev);
15320 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15321 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15322 i915_disable_vga(dev);
15326 void i915_redisable_vga(struct drm_device *dev)
15328 struct drm_i915_private *dev_priv = dev->dev_private;
15330 /* This function can be called both from intel_modeset_setup_hw_state or
15331 * at a very early point in our resume sequence, where the power well
15332 * structures are not yet restored. Since this function is at a very
15333 * paranoid "someone might have enabled VGA while we were not looking"
15334 * level, just check if the power well is enabled instead of trying to
15335 * follow the "don't touch the power well if we don't need it" policy
15336 * the rest of the driver uses. */
15337 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
15340 i915_redisable_vga_power_on(dev);
15343 static bool primary_get_hw_state(struct intel_crtc *crtc)
15345 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15350 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15353 static void intel_modeset_readout_hw_state(struct drm_device *dev)
15355 struct drm_i915_private *dev_priv = dev->dev_private;
15357 struct intel_crtc *crtc;
15358 struct intel_encoder *encoder;
15359 struct intel_connector *connector;
15362 for_each_intel_crtc(dev, crtc) {
15363 struct drm_plane *primary = crtc->base.primary;
15364 struct intel_plane_state *plane_state;
15366 memset(crtc->config, 0, sizeof(*crtc->config));
15368 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
15370 crtc->active = dev_priv->display.get_pipe_config(crtc,
15373 crtc->base.state->enable = crtc->active;
15374 crtc->base.state->active = crtc->active;
15375 crtc->base.enabled = crtc->active;
15377 plane_state = to_intel_plane_state(primary->state);
15378 plane_state->visible = primary_get_hw_state(crtc);
15380 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15381 crtc->base.base.id,
15382 crtc->active ? "enabled" : "disabled");
15385 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15386 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15388 pll->on = pll->get_hw_state(dev_priv, pll,
15389 &pll->config.hw_state);
15391 pll->config.crtc_mask = 0;
15392 for_each_intel_crtc(dev, crtc) {
15393 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
15395 pll->config.crtc_mask |= 1 << crtc->pipe;
15399 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
15400 pll->name, pll->config.crtc_mask, pll->on);
15402 if (pll->config.crtc_mask)
15403 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
15406 for_each_intel_encoder(dev, encoder) {
15409 if (encoder->get_hw_state(encoder, &pipe)) {
15410 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15411 encoder->base.crtc = &crtc->base;
15412 encoder->get_config(encoder, crtc->config);
15414 encoder->base.crtc = NULL;
15417 encoder->connectors_active = false;
15418 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
15419 encoder->base.base.id,
15420 encoder->base.name,
15421 encoder->base.crtc ? "enabled" : "disabled",
15425 for_each_intel_connector(dev, connector) {
15426 if (connector->get_hw_state(connector)) {
15427 connector->base.dpms = DRM_MODE_DPMS_ON;
15428 connector->encoder->connectors_active = true;
15429 connector->base.encoder = &connector->encoder->base;
15431 connector->base.dpms = DRM_MODE_DPMS_OFF;
15432 connector->base.encoder = NULL;
15434 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15435 connector->base.base.id,
15436 connector->base.name,
15437 connector->base.encoder ? "enabled" : "disabled");
15441 /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15442 * and i915 state tracking structures. */
15443 void intel_modeset_setup_hw_state(struct drm_device *dev,
15444 bool force_restore)
15446 struct drm_i915_private *dev_priv = dev->dev_private;
15448 struct intel_crtc *crtc;
15449 struct intel_encoder *encoder;
15452 intel_modeset_readout_hw_state(dev);
15455 * Now that we have the config, copy it to each CRTC struct
15456 * Note that this could go away if we move to using crtc_config
15457 * checking everywhere.
15459 for_each_intel_crtc(dev, crtc) {
15460 if (crtc->active && i915.fastboot) {
15461 intel_mode_from_pipe_config(&crtc->base.mode,
15463 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15464 crtc->base.base.id);
15465 drm_mode_debug_printmodeline(&crtc->base.mode);
15469 /* HW state is read out, now we need to sanitize this mess. */
15470 for_each_intel_encoder(dev, encoder) {
15471 intel_sanitize_encoder(encoder);
15474 for_each_pipe(dev_priv, pipe) {
15475 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15476 intel_sanitize_crtc(crtc);
15477 intel_dump_pipe_config(crtc, crtc->config,
15478 "[setup_hw_state]");
15481 intel_modeset_update_connector_atomic_state(dev);
15483 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15484 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15486 if (!pll->on || pll->active)
15489 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15491 pll->disable(dev_priv, pll);
15496 skl_wm_get_hw_state(dev);
15497 else if (HAS_PCH_SPLIT(dev))
15498 ilk_wm_get_hw_state(dev);
15500 if (force_restore) {
15501 i915_redisable_vga(dev);
15504 * We need to use raw interfaces for restoring state to avoid
15505 * checking (bogus) intermediate states.
15507 for_each_pipe(dev_priv, pipe) {
15508 struct drm_crtc *crtc =
15509 dev_priv->pipe_to_crtc_mapping[pipe];
15511 intel_crtc_restore_mode(crtc);
15514 intel_modeset_update_staged_output_state(dev);
15517 intel_modeset_check_state(dev);
15520 void intel_modeset_gem_init(struct drm_device *dev)
15522 struct drm_i915_private *dev_priv = dev->dev_private;
15523 struct drm_crtc *c;
15524 struct drm_i915_gem_object *obj;
15527 mutex_lock(&dev->struct_mutex);
15528 intel_init_gt_powersave(dev);
15529 mutex_unlock(&dev->struct_mutex);
15532 * There may be no VBT; and if the BIOS enabled SSC we can
15533 * just keep using it to avoid unnecessary flicker. Whereas if the
15534 * BIOS isn't using it, don't assume it will work even if the VBT
15535 * indicates as much.
15537 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15538 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15541 intel_modeset_init_hw(dev);
15543 intel_setup_overlay(dev);
15546 * Make sure any fbs we allocated at startup are properly
15547 * pinned & fenced. When we do the allocation it's too early
15550 for_each_crtc(dev, c) {
15551 obj = intel_fb_obj(c->primary->fb);
15555 mutex_lock(&dev->struct_mutex);
15556 ret = intel_pin_and_fence_fb_obj(c->primary,
15560 mutex_unlock(&dev->struct_mutex);
15562 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15563 to_intel_crtc(c)->pipe);
15564 drm_framebuffer_unreference(c->primary->fb);
15565 c->primary->fb = NULL;
15566 c->primary->crtc = c->primary->state->crtc = NULL;
15567 update_state_fb(c->primary);
15568 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
15572 intel_backlight_register(dev);
15575 void intel_connector_unregister(struct intel_connector *intel_connector)
15577 struct drm_connector *connector = &intel_connector->base;
15579 intel_panel_destroy_backlight(connector);
15580 drm_connector_unregister(connector);
15583 void intel_modeset_cleanup(struct drm_device *dev)
15585 struct drm_i915_private *dev_priv = dev->dev_private;
15586 struct drm_connector *connector;
15588 intel_disable_gt_powersave(dev);
15590 intel_backlight_unregister(dev);
15593 * Interrupts and polling as the first thing to avoid creating havoc.
15594 * Too much stuff here (turning of connectors, ...) would
15595 * experience fancy races otherwise.
15597 intel_irq_uninstall(dev_priv);
15600 * Due to the hpd irq storm handling the hotplug work can re-arm the
15601 * poll handlers. Hence disable polling after hpd handling is shut down.
15603 drm_kms_helper_poll_fini(dev);
15605 mutex_lock(&dev->struct_mutex);
15607 intel_unregister_dsm_handler();
15609 intel_fbc_disable(dev);
15611 mutex_unlock(&dev->struct_mutex);
15613 /* flush any delayed tasks or pending work */
15614 flush_scheduled_work();
15616 /* destroy the backlight and sysfs files before encoders/connectors */
15617 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
15618 struct intel_connector *intel_connector;
15620 intel_connector = to_intel_connector(connector);
15621 intel_connector->unregister(intel_connector);
15624 drm_mode_config_cleanup(dev);
15626 intel_cleanup_overlay(dev);
15628 mutex_lock(&dev->struct_mutex);
15629 intel_cleanup_gt_powersave(dev);
15630 mutex_unlock(&dev->struct_mutex);
15634 * Return which encoder is currently attached for connector.
15636 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
15638 return &intel_attached_encoder(connector)->base;
15641 void intel_connector_attach_encoder(struct intel_connector *connector,
15642 struct intel_encoder *encoder)
15644 connector->encoder = encoder;
15645 drm_mode_connector_attach_encoder(&connector->base,
15650 * set vga decode state - true == enable VGA decode
15652 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15654 struct drm_i915_private *dev_priv = dev->dev_private;
15655 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
15658 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15659 DRM_ERROR("failed to read control word\n");
15663 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15667 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15669 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
15671 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15672 DRM_ERROR("failed to write control word\n");
15679 struct intel_display_error_state {
15681 u32 power_well_driver;
15683 int num_transcoders;
15685 struct intel_cursor_error_state {
15690 } cursor[I915_MAX_PIPES];
15692 struct intel_pipe_error_state {
15693 bool power_domain_on;
15696 } pipe[I915_MAX_PIPES];
15698 struct intel_plane_error_state {
15706 } plane[I915_MAX_PIPES];
15708 struct intel_transcoder_error_state {
15709 bool power_domain_on;
15710 enum transcoder cpu_transcoder;
15723 struct intel_display_error_state *
15724 intel_display_capture_error_state(struct drm_device *dev)
15726 struct drm_i915_private *dev_priv = dev->dev_private;
15727 struct intel_display_error_state *error;
15728 int transcoders[] = {
15736 if (INTEL_INFO(dev)->num_pipes == 0)
15739 error = kzalloc(sizeof(*error), GFP_ATOMIC);
15743 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
15744 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15746 for_each_pipe(dev_priv, i) {
15747 error->pipe[i].power_domain_on =
15748 __intel_display_power_is_enabled(dev_priv,
15749 POWER_DOMAIN_PIPE(i));
15750 if (!error->pipe[i].power_domain_on)
15753 error->cursor[i].control = I915_READ(CURCNTR(i));
15754 error->cursor[i].position = I915_READ(CURPOS(i));
15755 error->cursor[i].base = I915_READ(CURBASE(i));
15757 error->plane[i].control = I915_READ(DSPCNTR(i));
15758 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
15759 if (INTEL_INFO(dev)->gen <= 3) {
15760 error->plane[i].size = I915_READ(DSPSIZE(i));
15761 error->plane[i].pos = I915_READ(DSPPOS(i));
15763 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15764 error->plane[i].addr = I915_READ(DSPADDR(i));
15765 if (INTEL_INFO(dev)->gen >= 4) {
15766 error->plane[i].surface = I915_READ(DSPSURF(i));
15767 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15770 error->pipe[i].source = I915_READ(PIPESRC(i));
15772 if (HAS_GMCH_DISPLAY(dev))
15773 error->pipe[i].stat = I915_READ(PIPESTAT(i));
15776 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15777 if (HAS_DDI(dev_priv->dev))
15778 error->num_transcoders++; /* Account for eDP. */
15780 for (i = 0; i < error->num_transcoders; i++) {
15781 enum transcoder cpu_transcoder = transcoders[i];
15783 error->transcoder[i].power_domain_on =
15784 __intel_display_power_is_enabled(dev_priv,
15785 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
15786 if (!error->transcoder[i].power_domain_on)
15789 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15791 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15792 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15793 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15794 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15795 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15796 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15797 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
15803 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15806 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
15807 struct drm_device *dev,
15808 struct intel_display_error_state *error)
15810 struct drm_i915_private *dev_priv = dev->dev_private;
15816 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
15817 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
15818 err_printf(m, "PWR_WELL_CTL2: %08x\n",
15819 error->power_well_driver);
15820 for_each_pipe(dev_priv, i) {
15821 err_printf(m, "Pipe [%d]:\n", i);
15822 err_printf(m, " Power: %s\n",
15823 error->pipe[i].power_domain_on ? "on" : "off");
15824 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
15825 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
15827 err_printf(m, "Plane [%d]:\n", i);
15828 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15829 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
15830 if (INTEL_INFO(dev)->gen <= 3) {
15831 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15832 err_printf(m, " POS: %08x\n", error->plane[i].pos);
15834 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15835 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
15836 if (INTEL_INFO(dev)->gen >= 4) {
15837 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15838 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
15841 err_printf(m, "Cursor [%d]:\n", i);
15842 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15843 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15844 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
15847 for (i = 0; i < error->num_transcoders; i++) {
15848 err_printf(m, "CPU transcoder: %c\n",
15849 transcoder_name(error->transcoder[i].cpu_transcoder));
15850 err_printf(m, " Power: %s\n",
15851 error->transcoder[i].power_domain_on ? "on" : "off");
15852 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15853 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15854 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15855 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15856 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15857 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15858 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15862 void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15864 struct intel_crtc *crtc;
15866 for_each_intel_crtc(dev, crtc) {
15867 struct intel_unpin_work *work;
15869 spin_lock_irq(&dev->event_lock);
15871 work = crtc->unpin_work;
15873 if (work && work->event &&
15874 work->event->base.file_priv == file) {
15875 kfree(work->event);
15876 work->event = NULL;
15879 spin_unlock_irq(&dev->event_lock);