drm/i915: Update DRIVER_DATE to 20150410
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
... / ...
CommitLineData
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
27#include <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>
35#include <drm/drmP.h>
36#include "intel_drv.h"
37#include <drm/i915_drm.h>
38#include "i915_drv.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>
47
48/* Primary plane formats supported by all gen */
49#define COMMON_PRIMARY_FORMATS \
50 DRM_FORMAT_C8, \
51 DRM_FORMAT_RGB565, \
52 DRM_FORMAT_XRGB8888, \
53 DRM_FORMAT_ARGB8888
54
55/* Primary plane formats for gen <= 3 */
56static const uint32_t intel_primary_formats_gen2[] = {
57 COMMON_PRIMARY_FORMATS,
58 DRM_FORMAT_XRGB1555,
59 DRM_FORMAT_ARGB1555,
60};
61
62/* Primary plane formats for gen >= 4 */
63static const uint32_t intel_primary_formats_gen4[] = {
64 COMMON_PRIMARY_FORMATS, \
65 DRM_FORMAT_XBGR8888,
66 DRM_FORMAT_ABGR8888,
67 DRM_FORMAT_XRGB2101010,
68 DRM_FORMAT_ARGB2101010,
69 DRM_FORMAT_XBGR2101010,
70 DRM_FORMAT_ABGR2101010,
71};
72
73/* Cursor formats */
74static const uint32_t intel_cursor_formats[] = {
75 DRM_FORMAT_ARGB8888,
76};
77
78static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79
80static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
81 struct intel_crtc_state *pipe_config);
82static void ironlake_pch_clock_get(struct intel_crtc *crtc,
83 struct intel_crtc_state *pipe_config);
84
85static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
86 int x, int y, struct drm_framebuffer *old_fb,
87 struct drm_atomic_state *state);
88static int intel_framebuffer_init(struct drm_device *dev,
89 struct intel_framebuffer *ifb,
90 struct drm_mode_fb_cmd2 *mode_cmd,
91 struct drm_i915_gem_object *obj);
92static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
93static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
94static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
95 struct intel_link_m_n *m_n,
96 struct intel_link_m_n *m2_n2);
97static void ironlake_set_pipeconf(struct drm_crtc *crtc);
98static void haswell_set_pipeconf(struct drm_crtc *crtc);
99static void intel_set_pipe_csc(struct drm_crtc *crtc);
100static void vlv_prepare_pll(struct intel_crtc *crtc,
101 const struct intel_crtc_state *pipe_config);
102static void chv_prepare_pll(struct intel_crtc *crtc,
103 const struct intel_crtc_state *pipe_config);
104static void intel_begin_crtc_commit(struct drm_crtc *crtc);
105static void intel_finish_crtc_commit(struct drm_crtc *crtc);
106
107static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
108{
109 if (!connector->mst_port)
110 return connector->encoder;
111 else
112 return &connector->mst_port->mst_encoders[pipe]->base;
113}
114
115typedef struct {
116 int min, max;
117} intel_range_t;
118
119typedef struct {
120 int dot_limit;
121 int p2_slow, p2_fast;
122} intel_p2_t;
123
124typedef struct intel_limit intel_limit_t;
125struct intel_limit {
126 intel_range_t dot, vco, n, m, m1, m2, p, p1;
127 intel_p2_t p2;
128};
129
130int
131intel_pch_rawclk(struct drm_device *dev)
132{
133 struct drm_i915_private *dev_priv = dev->dev_private;
134
135 WARN_ON(!HAS_PCH_SPLIT(dev));
136
137 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
138}
139
140static inline u32 /* units of 100MHz */
141intel_fdi_link_freq(struct drm_device *dev)
142{
143 if (IS_GEN5(dev)) {
144 struct drm_i915_private *dev_priv = dev->dev_private;
145 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
146 } else
147 return 27;
148}
149
150static const intel_limit_t intel_limits_i8xx_dac = {
151 .dot = { .min = 25000, .max = 350000 },
152 .vco = { .min = 908000, .max = 1512000 },
153 .n = { .min = 2, .max = 16 },
154 .m = { .min = 96, .max = 140 },
155 .m1 = { .min = 18, .max = 26 },
156 .m2 = { .min = 6, .max = 16 },
157 .p = { .min = 4, .max = 128 },
158 .p1 = { .min = 2, .max = 33 },
159 .p2 = { .dot_limit = 165000,
160 .p2_slow = 4, .p2_fast = 2 },
161};
162
163static const intel_limit_t intel_limits_i8xx_dvo = {
164 .dot = { .min = 25000, .max = 350000 },
165 .vco = { .min = 908000, .max = 1512000 },
166 .n = { .min = 2, .max = 16 },
167 .m = { .min = 96, .max = 140 },
168 .m1 = { .min = 18, .max = 26 },
169 .m2 = { .min = 6, .max = 16 },
170 .p = { .min = 4, .max = 128 },
171 .p1 = { .min = 2, .max = 33 },
172 .p2 = { .dot_limit = 165000,
173 .p2_slow = 4, .p2_fast = 4 },
174};
175
176static const intel_limit_t intel_limits_i8xx_lvds = {
177 .dot = { .min = 25000, .max = 350000 },
178 .vco = { .min = 908000, .max = 1512000 },
179 .n = { .min = 2, .max = 16 },
180 .m = { .min = 96, .max = 140 },
181 .m1 = { .min = 18, .max = 26 },
182 .m2 = { .min = 6, .max = 16 },
183 .p = { .min = 4, .max = 128 },
184 .p1 = { .min = 1, .max = 6 },
185 .p2 = { .dot_limit = 165000,
186 .p2_slow = 14, .p2_fast = 7 },
187};
188
189static const intel_limit_t intel_limits_i9xx_sdvo = {
190 .dot = { .min = 20000, .max = 400000 },
191 .vco = { .min = 1400000, .max = 2800000 },
192 .n = { .min = 1, .max = 6 },
193 .m = { .min = 70, .max = 120 },
194 .m1 = { .min = 8, .max = 18 },
195 .m2 = { .min = 3, .max = 7 },
196 .p = { .min = 5, .max = 80 },
197 .p1 = { .min = 1, .max = 8 },
198 .p2 = { .dot_limit = 200000,
199 .p2_slow = 10, .p2_fast = 5 },
200};
201
202static const intel_limit_t intel_limits_i9xx_lvds = {
203 .dot = { .min = 20000, .max = 400000 },
204 .vco = { .min = 1400000, .max = 2800000 },
205 .n = { .min = 1, .max = 6 },
206 .m = { .min = 70, .max = 120 },
207 .m1 = { .min = 8, .max = 18 },
208 .m2 = { .min = 3, .max = 7 },
209 .p = { .min = 7, .max = 98 },
210 .p1 = { .min = 1, .max = 8 },
211 .p2 = { .dot_limit = 112000,
212 .p2_slow = 14, .p2_fast = 7 },
213};
214
215
216static const intel_limit_t intel_limits_g4x_sdvo = {
217 .dot = { .min = 25000, .max = 270000 },
218 .vco = { .min = 1750000, .max = 3500000},
219 .n = { .min = 1, .max = 4 },
220 .m = { .min = 104, .max = 138 },
221 .m1 = { .min = 17, .max = 23 },
222 .m2 = { .min = 5, .max = 11 },
223 .p = { .min = 10, .max = 30 },
224 .p1 = { .min = 1, .max = 3},
225 .p2 = { .dot_limit = 270000,
226 .p2_slow = 10,
227 .p2_fast = 10
228 },
229};
230
231static const intel_limit_t intel_limits_g4x_hdmi = {
232 .dot = { .min = 22000, .max = 400000 },
233 .vco = { .min = 1750000, .max = 3500000},
234 .n = { .min = 1, .max = 4 },
235 .m = { .min = 104, .max = 138 },
236 .m1 = { .min = 16, .max = 23 },
237 .m2 = { .min = 5, .max = 11 },
238 .p = { .min = 5, .max = 80 },
239 .p1 = { .min = 1, .max = 8},
240 .p2 = { .dot_limit = 165000,
241 .p2_slow = 10, .p2_fast = 5 },
242};
243
244static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
245 .dot = { .min = 20000, .max = 115000 },
246 .vco = { .min = 1750000, .max = 3500000 },
247 .n = { .min = 1, .max = 3 },
248 .m = { .min = 104, .max = 138 },
249 .m1 = { .min = 17, .max = 23 },
250 .m2 = { .min = 5, .max = 11 },
251 .p = { .min = 28, .max = 112 },
252 .p1 = { .min = 2, .max = 8 },
253 .p2 = { .dot_limit = 0,
254 .p2_slow = 14, .p2_fast = 14
255 },
256};
257
258static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
259 .dot = { .min = 80000, .max = 224000 },
260 .vco = { .min = 1750000, .max = 3500000 },
261 .n = { .min = 1, .max = 3 },
262 .m = { .min = 104, .max = 138 },
263 .m1 = { .min = 17, .max = 23 },
264 .m2 = { .min = 5, .max = 11 },
265 .p = { .min = 14, .max = 42 },
266 .p1 = { .min = 2, .max = 6 },
267 .p2 = { .dot_limit = 0,
268 .p2_slow = 7, .p2_fast = 7
269 },
270};
271
272static const intel_limit_t intel_limits_pineview_sdvo = {
273 .dot = { .min = 20000, .max = 400000},
274 .vco = { .min = 1700000, .max = 3500000 },
275 /* Pineview's Ncounter is a ring counter */
276 .n = { .min = 3, .max = 6 },
277 .m = { .min = 2, .max = 256 },
278 /* Pineview only has one combined m divider, which we treat as m2. */
279 .m1 = { .min = 0, .max = 0 },
280 .m2 = { .min = 0, .max = 254 },
281 .p = { .min = 5, .max = 80 },
282 .p1 = { .min = 1, .max = 8 },
283 .p2 = { .dot_limit = 200000,
284 .p2_slow = 10, .p2_fast = 5 },
285};
286
287static const intel_limit_t intel_limits_pineview_lvds = {
288 .dot = { .min = 20000, .max = 400000 },
289 .vco = { .min = 1700000, .max = 3500000 },
290 .n = { .min = 3, .max = 6 },
291 .m = { .min = 2, .max = 256 },
292 .m1 = { .min = 0, .max = 0 },
293 .m2 = { .min = 0, .max = 254 },
294 .p = { .min = 7, .max = 112 },
295 .p1 = { .min = 1, .max = 8 },
296 .p2 = { .dot_limit = 112000,
297 .p2_slow = 14, .p2_fast = 14 },
298};
299
300/* Ironlake / Sandybridge
301 *
302 * We calculate clock using (register_value + 2) for N/M1/M2, so here
303 * the range value for them is (actual_value - 2).
304 */
305static const intel_limit_t intel_limits_ironlake_dac = {
306 .dot = { .min = 25000, .max = 350000 },
307 .vco = { .min = 1760000, .max = 3510000 },
308 .n = { .min = 1, .max = 5 },
309 .m = { .min = 79, .max = 127 },
310 .m1 = { .min = 12, .max = 22 },
311 .m2 = { .min = 5, .max = 9 },
312 .p = { .min = 5, .max = 80 },
313 .p1 = { .min = 1, .max = 8 },
314 .p2 = { .dot_limit = 225000,
315 .p2_slow = 10, .p2_fast = 5 },
316};
317
318static const intel_limit_t intel_limits_ironlake_single_lvds = {
319 .dot = { .min = 25000, .max = 350000 },
320 .vco = { .min = 1760000, .max = 3510000 },
321 .n = { .min = 1, .max = 3 },
322 .m = { .min = 79, .max = 118 },
323 .m1 = { .min = 12, .max = 22 },
324 .m2 = { .min = 5, .max = 9 },
325 .p = { .min = 28, .max = 112 },
326 .p1 = { .min = 2, .max = 8 },
327 .p2 = { .dot_limit = 225000,
328 .p2_slow = 14, .p2_fast = 14 },
329};
330
331static const intel_limit_t intel_limits_ironlake_dual_lvds = {
332 .dot = { .min = 25000, .max = 350000 },
333 .vco = { .min = 1760000, .max = 3510000 },
334 .n = { .min = 1, .max = 3 },
335 .m = { .min = 79, .max = 127 },
336 .m1 = { .min = 12, .max = 22 },
337 .m2 = { .min = 5, .max = 9 },
338 .p = { .min = 14, .max = 56 },
339 .p1 = { .min = 2, .max = 8 },
340 .p2 = { .dot_limit = 225000,
341 .p2_slow = 7, .p2_fast = 7 },
342};
343
344/* LVDS 100mhz refclk limits. */
345static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
346 .dot = { .min = 25000, .max = 350000 },
347 .vco = { .min = 1760000, .max = 3510000 },
348 .n = { .min = 1, .max = 2 },
349 .m = { .min = 79, .max = 126 },
350 .m1 = { .min = 12, .max = 22 },
351 .m2 = { .min = 5, .max = 9 },
352 .p = { .min = 28, .max = 112 },
353 .p1 = { .min = 2, .max = 8 },
354 .p2 = { .dot_limit = 225000,
355 .p2_slow = 14, .p2_fast = 14 },
356};
357
358static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
359 .dot = { .min = 25000, .max = 350000 },
360 .vco = { .min = 1760000, .max = 3510000 },
361 .n = { .min = 1, .max = 3 },
362 .m = { .min = 79, .max = 126 },
363 .m1 = { .min = 12, .max = 22 },
364 .m2 = { .min = 5, .max = 9 },
365 .p = { .min = 14, .max = 42 },
366 .p1 = { .min = 2, .max = 6 },
367 .p2 = { .dot_limit = 225000,
368 .p2_slow = 7, .p2_fast = 7 },
369};
370
371static const intel_limit_t intel_limits_vlv = {
372 /*
373 * These are the data rate limits (measured in fast clocks)
374 * since those are the strictest limits we have. The fast
375 * clock and actual rate limits are more relaxed, so checking
376 * them would make no difference.
377 */
378 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
379 .vco = { .min = 4000000, .max = 6000000 },
380 .n = { .min = 1, .max = 7 },
381 .m1 = { .min = 2, .max = 3 },
382 .m2 = { .min = 11, .max = 156 },
383 .p1 = { .min = 2, .max = 3 },
384 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
385};
386
387static const intel_limit_t intel_limits_chv = {
388 /*
389 * These are the data rate limits (measured in fast clocks)
390 * since those are the strictest limits we have. The fast
391 * clock and actual rate limits are more relaxed, so checking
392 * them would make no difference.
393 */
394 .dot = { .min = 25000 * 5, .max = 540000 * 5},
395 .vco = { .min = 4800000, .max = 6480000 },
396 .n = { .min = 1, .max = 1 },
397 .m1 = { .min = 2, .max = 2 },
398 .m2 = { .min = 24 << 22, .max = 175 << 22 },
399 .p1 = { .min = 2, .max = 4 },
400 .p2 = { .p2_slow = 1, .p2_fast = 14 },
401};
402
403static void vlv_clock(int refclk, intel_clock_t *clock)
404{
405 clock->m = clock->m1 * clock->m2;
406 clock->p = clock->p1 * clock->p2;
407 if (WARN_ON(clock->n == 0 || clock->p == 0))
408 return;
409 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
410 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
411}
412
413/**
414 * Returns whether any output on the specified pipe is of the specified type
415 */
416bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
417{
418 struct drm_device *dev = crtc->base.dev;
419 struct intel_encoder *encoder;
420
421 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
422 if (encoder->type == type)
423 return true;
424
425 return false;
426}
427
428/**
429 * Returns whether any output on the specified pipe will have the specified
430 * type after a staged modeset is complete, i.e., the same as
431 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
432 * encoder->crtc.
433 */
434static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
435 int type)
436{
437 struct drm_atomic_state *state = crtc_state->base.state;
438 struct drm_connector_state *connector_state;
439 struct intel_encoder *encoder;
440 int i, num_connectors = 0;
441
442 for (i = 0; i < state->num_connector; i++) {
443 if (!state->connectors[i])
444 continue;
445
446 connector_state = state->connector_states[i];
447 if (connector_state->crtc != crtc_state->base.crtc)
448 continue;
449
450 num_connectors++;
451
452 encoder = to_intel_encoder(connector_state->best_encoder);
453 if (encoder->type == type)
454 return true;
455 }
456
457 WARN_ON(num_connectors == 0);
458
459 return false;
460}
461
462static const intel_limit_t *
463intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
464{
465 struct drm_device *dev = crtc_state->base.crtc->dev;
466 const intel_limit_t *limit;
467
468 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
469 if (intel_is_dual_link_lvds(dev)) {
470 if (refclk == 100000)
471 limit = &intel_limits_ironlake_dual_lvds_100m;
472 else
473 limit = &intel_limits_ironlake_dual_lvds;
474 } else {
475 if (refclk == 100000)
476 limit = &intel_limits_ironlake_single_lvds_100m;
477 else
478 limit = &intel_limits_ironlake_single_lvds;
479 }
480 } else
481 limit = &intel_limits_ironlake_dac;
482
483 return limit;
484}
485
486static const intel_limit_t *
487intel_g4x_limit(struct intel_crtc_state *crtc_state)
488{
489 struct drm_device *dev = crtc_state->base.crtc->dev;
490 const intel_limit_t *limit;
491
492 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
493 if (intel_is_dual_link_lvds(dev))
494 limit = &intel_limits_g4x_dual_channel_lvds;
495 else
496 limit = &intel_limits_g4x_single_channel_lvds;
497 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
498 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
499 limit = &intel_limits_g4x_hdmi;
500 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
501 limit = &intel_limits_g4x_sdvo;
502 } else /* The option is for other outputs */
503 limit = &intel_limits_i9xx_sdvo;
504
505 return limit;
506}
507
508static const intel_limit_t *
509intel_limit(struct intel_crtc_state *crtc_state, int refclk)
510{
511 struct drm_device *dev = crtc_state->base.crtc->dev;
512 const intel_limit_t *limit;
513
514 if (HAS_PCH_SPLIT(dev))
515 limit = intel_ironlake_limit(crtc_state, refclk);
516 else if (IS_G4X(dev)) {
517 limit = intel_g4x_limit(crtc_state);
518 } else if (IS_PINEVIEW(dev)) {
519 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
520 limit = &intel_limits_pineview_lvds;
521 else
522 limit = &intel_limits_pineview_sdvo;
523 } else if (IS_CHERRYVIEW(dev)) {
524 limit = &intel_limits_chv;
525 } else if (IS_VALLEYVIEW(dev)) {
526 limit = &intel_limits_vlv;
527 } else if (!IS_GEN2(dev)) {
528 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
529 limit = &intel_limits_i9xx_lvds;
530 else
531 limit = &intel_limits_i9xx_sdvo;
532 } else {
533 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
534 limit = &intel_limits_i8xx_lvds;
535 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
536 limit = &intel_limits_i8xx_dvo;
537 else
538 limit = &intel_limits_i8xx_dac;
539 }
540 return limit;
541}
542
543/* m1 is reserved as 0 in Pineview, n is a ring counter */
544static void pineview_clock(int refclk, intel_clock_t *clock)
545{
546 clock->m = clock->m2 + 2;
547 clock->p = clock->p1 * clock->p2;
548 if (WARN_ON(clock->n == 0 || clock->p == 0))
549 return;
550 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
551 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
552}
553
554static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
555{
556 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
557}
558
559static void i9xx_clock(int refclk, intel_clock_t *clock)
560{
561 clock->m = i9xx_dpll_compute_m(clock);
562 clock->p = clock->p1 * clock->p2;
563 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
564 return;
565 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
566 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
567}
568
569static void chv_clock(int refclk, intel_clock_t *clock)
570{
571 clock->m = clock->m1 * clock->m2;
572 clock->p = clock->p1 * clock->p2;
573 if (WARN_ON(clock->n == 0 || clock->p == 0))
574 return;
575 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
576 clock->n << 22);
577 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
578}
579
580#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
581/**
582 * Returns whether the given set of divisors are valid for a given refclk with
583 * the given connectors.
584 */
585
586static bool intel_PLL_is_valid(struct drm_device *dev,
587 const intel_limit_t *limit,
588 const intel_clock_t *clock)
589{
590 if (clock->n < limit->n.min || limit->n.max < clock->n)
591 INTELPllInvalid("n out of range\n");
592 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
593 INTELPllInvalid("p1 out of range\n");
594 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
595 INTELPllInvalid("m2 out of range\n");
596 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
597 INTELPllInvalid("m1 out of range\n");
598
599 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
600 if (clock->m1 <= clock->m2)
601 INTELPllInvalid("m1 <= m2\n");
602
603 if (!IS_VALLEYVIEW(dev)) {
604 if (clock->p < limit->p.min || limit->p.max < clock->p)
605 INTELPllInvalid("p out of range\n");
606 if (clock->m < limit->m.min || limit->m.max < clock->m)
607 INTELPllInvalid("m out of range\n");
608 }
609
610 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
611 INTELPllInvalid("vco out of range\n");
612 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
613 * connector, etc., rather than just a single range.
614 */
615 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
616 INTELPllInvalid("dot out of range\n");
617
618 return true;
619}
620
621static bool
622i9xx_find_best_dpll(const intel_limit_t *limit,
623 struct intel_crtc_state *crtc_state,
624 int target, int refclk, intel_clock_t *match_clock,
625 intel_clock_t *best_clock)
626{
627 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
628 struct drm_device *dev = crtc->base.dev;
629 intel_clock_t clock;
630 int err = target;
631
632 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
633 /*
634 * For LVDS just rely on its current settings for dual-channel.
635 * We haven't figured out how to reliably set up different
636 * single/dual channel state, if we even can.
637 */
638 if (intel_is_dual_link_lvds(dev))
639 clock.p2 = limit->p2.p2_fast;
640 else
641 clock.p2 = limit->p2.p2_slow;
642 } else {
643 if (target < limit->p2.dot_limit)
644 clock.p2 = limit->p2.p2_slow;
645 else
646 clock.p2 = limit->p2.p2_fast;
647 }
648
649 memset(best_clock, 0, sizeof(*best_clock));
650
651 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
652 clock.m1++) {
653 for (clock.m2 = limit->m2.min;
654 clock.m2 <= limit->m2.max; clock.m2++) {
655 if (clock.m2 >= clock.m1)
656 break;
657 for (clock.n = limit->n.min;
658 clock.n <= limit->n.max; clock.n++) {
659 for (clock.p1 = limit->p1.min;
660 clock.p1 <= limit->p1.max; clock.p1++) {
661 int this_err;
662
663 i9xx_clock(refclk, &clock);
664 if (!intel_PLL_is_valid(dev, limit,
665 &clock))
666 continue;
667 if (match_clock &&
668 clock.p != match_clock->p)
669 continue;
670
671 this_err = abs(clock.dot - target);
672 if (this_err < err) {
673 *best_clock = clock;
674 err = this_err;
675 }
676 }
677 }
678 }
679 }
680
681 return (err != target);
682}
683
684static bool
685pnv_find_best_dpll(const intel_limit_t *limit,
686 struct intel_crtc_state *crtc_state,
687 int target, int refclk, intel_clock_t *match_clock,
688 intel_clock_t *best_clock)
689{
690 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
691 struct drm_device *dev = crtc->base.dev;
692 intel_clock_t clock;
693 int err = target;
694
695 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
696 /*
697 * For LVDS just rely on its current settings for dual-channel.
698 * We haven't figured out how to reliably set up different
699 * single/dual channel state, if we even can.
700 */
701 if (intel_is_dual_link_lvds(dev))
702 clock.p2 = limit->p2.p2_fast;
703 else
704 clock.p2 = limit->p2.p2_slow;
705 } else {
706 if (target < limit->p2.dot_limit)
707 clock.p2 = limit->p2.p2_slow;
708 else
709 clock.p2 = limit->p2.p2_fast;
710 }
711
712 memset(best_clock, 0, sizeof(*best_clock));
713
714 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
715 clock.m1++) {
716 for (clock.m2 = limit->m2.min;
717 clock.m2 <= limit->m2.max; clock.m2++) {
718 for (clock.n = limit->n.min;
719 clock.n <= limit->n.max; clock.n++) {
720 for (clock.p1 = limit->p1.min;
721 clock.p1 <= limit->p1.max; clock.p1++) {
722 int this_err;
723
724 pineview_clock(refclk, &clock);
725 if (!intel_PLL_is_valid(dev, limit,
726 &clock))
727 continue;
728 if (match_clock &&
729 clock.p != match_clock->p)
730 continue;
731
732 this_err = abs(clock.dot - target);
733 if (this_err < err) {
734 *best_clock = clock;
735 err = this_err;
736 }
737 }
738 }
739 }
740 }
741
742 return (err != target);
743}
744
745static bool
746g4x_find_best_dpll(const intel_limit_t *limit,
747 struct intel_crtc_state *crtc_state,
748 int target, int refclk, intel_clock_t *match_clock,
749 intel_clock_t *best_clock)
750{
751 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
752 struct drm_device *dev = crtc->base.dev;
753 intel_clock_t clock;
754 int max_n;
755 bool found;
756 /* approximately equals target * 0.00585 */
757 int err_most = (target >> 8) + (target >> 9);
758 found = false;
759
760 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
761 if (intel_is_dual_link_lvds(dev))
762 clock.p2 = limit->p2.p2_fast;
763 else
764 clock.p2 = limit->p2.p2_slow;
765 } else {
766 if (target < limit->p2.dot_limit)
767 clock.p2 = limit->p2.p2_slow;
768 else
769 clock.p2 = limit->p2.p2_fast;
770 }
771
772 memset(best_clock, 0, sizeof(*best_clock));
773 max_n = limit->n.max;
774 /* based on hardware requirement, prefer smaller n to precision */
775 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
776 /* based on hardware requirement, prefere larger m1,m2 */
777 for (clock.m1 = limit->m1.max;
778 clock.m1 >= limit->m1.min; clock.m1--) {
779 for (clock.m2 = limit->m2.max;
780 clock.m2 >= limit->m2.min; clock.m2--) {
781 for (clock.p1 = limit->p1.max;
782 clock.p1 >= limit->p1.min; clock.p1--) {
783 int this_err;
784
785 i9xx_clock(refclk, &clock);
786 if (!intel_PLL_is_valid(dev, limit,
787 &clock))
788 continue;
789
790 this_err = abs(clock.dot - target);
791 if (this_err < err_most) {
792 *best_clock = clock;
793 err_most = this_err;
794 max_n = clock.n;
795 found = true;
796 }
797 }
798 }
799 }
800 }
801 return found;
802}
803
804/*
805 * Check if the calculated PLL configuration is more optimal compared to the
806 * best configuration and error found so far. Return the calculated error.
807 */
808static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
809 const intel_clock_t *calculated_clock,
810 const intel_clock_t *best_clock,
811 unsigned int best_error_ppm,
812 unsigned int *error_ppm)
813{
814 /*
815 * For CHV ignore the error and consider only the P value.
816 * Prefer a bigger P value based on HW requirements.
817 */
818 if (IS_CHERRYVIEW(dev)) {
819 *error_ppm = 0;
820
821 return calculated_clock->p > best_clock->p;
822 }
823
824 if (WARN_ON_ONCE(!target_freq))
825 return false;
826
827 *error_ppm = div_u64(1000000ULL *
828 abs(target_freq - calculated_clock->dot),
829 target_freq);
830 /*
831 * Prefer a better P value over a better (smaller) error if the error
832 * is small. Ensure this preference for future configurations too by
833 * setting the error to 0.
834 */
835 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
836 *error_ppm = 0;
837
838 return true;
839 }
840
841 return *error_ppm + 10 < best_error_ppm;
842}
843
844static bool
845vlv_find_best_dpll(const intel_limit_t *limit,
846 struct intel_crtc_state *crtc_state,
847 int target, int refclk, intel_clock_t *match_clock,
848 intel_clock_t *best_clock)
849{
850 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
851 struct drm_device *dev = crtc->base.dev;
852 intel_clock_t clock;
853 unsigned int bestppm = 1000000;
854 /* min update 19.2 MHz */
855 int max_n = min(limit->n.max, refclk / 19200);
856 bool found = false;
857
858 target *= 5; /* fast clock */
859
860 memset(best_clock, 0, sizeof(*best_clock));
861
862 /* based on hardware requirement, prefer smaller n to precision */
863 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
864 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
865 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
866 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
867 clock.p = clock.p1 * clock.p2;
868 /* based on hardware requirement, prefer bigger m1,m2 values */
869 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
870 unsigned int ppm;
871
872 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
873 refclk * clock.m1);
874
875 vlv_clock(refclk, &clock);
876
877 if (!intel_PLL_is_valid(dev, limit,
878 &clock))
879 continue;
880
881 if (!vlv_PLL_is_optimal(dev, target,
882 &clock,
883 best_clock,
884 bestppm, &ppm))
885 continue;
886
887 *best_clock = clock;
888 bestppm = ppm;
889 found = true;
890 }
891 }
892 }
893 }
894
895 return found;
896}
897
898static bool
899chv_find_best_dpll(const intel_limit_t *limit,
900 struct intel_crtc_state *crtc_state,
901 int target, int refclk, intel_clock_t *match_clock,
902 intel_clock_t *best_clock)
903{
904 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
905 struct drm_device *dev = crtc->base.dev;
906 unsigned int best_error_ppm;
907 intel_clock_t clock;
908 uint64_t m2;
909 int found = false;
910
911 memset(best_clock, 0, sizeof(*best_clock));
912 best_error_ppm = 1000000;
913
914 /*
915 * Based on hardware doc, the n always set to 1, and m1 always
916 * set to 2. If requires to support 200Mhz refclk, we need to
917 * revisit this because n may not 1 anymore.
918 */
919 clock.n = 1, clock.m1 = 2;
920 target *= 5; /* fast clock */
921
922 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
923 for (clock.p2 = limit->p2.p2_fast;
924 clock.p2 >= limit->p2.p2_slow;
925 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
926 unsigned int error_ppm;
927
928 clock.p = clock.p1 * clock.p2;
929
930 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
931 clock.n) << 22, refclk * clock.m1);
932
933 if (m2 > INT_MAX/clock.m1)
934 continue;
935
936 clock.m2 = m2;
937
938 chv_clock(refclk, &clock);
939
940 if (!intel_PLL_is_valid(dev, limit, &clock))
941 continue;
942
943 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
944 best_error_ppm, &error_ppm))
945 continue;
946
947 *best_clock = clock;
948 best_error_ppm = error_ppm;
949 found = true;
950 }
951 }
952
953 return found;
954}
955
956bool intel_crtc_active(struct drm_crtc *crtc)
957{
958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
959
960 /* Be paranoid as we can arrive here with only partial
961 * state retrieved from the hardware during setup.
962 *
963 * We can ditch the adjusted_mode.crtc_clock check as soon
964 * as Haswell has gained clock readout/fastboot support.
965 *
966 * We can ditch the crtc->primary->fb check as soon as we can
967 * properly reconstruct framebuffers.
968 *
969 * FIXME: The intel_crtc->active here should be switched to
970 * crtc->state->active once we have proper CRTC states wired up
971 * for atomic.
972 */
973 return intel_crtc->active && crtc->primary->state->fb &&
974 intel_crtc->config->base.adjusted_mode.crtc_clock;
975}
976
977enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
978 enum pipe pipe)
979{
980 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
982
983 return intel_crtc->config->cpu_transcoder;
984}
985
986static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
987{
988 struct drm_i915_private *dev_priv = dev->dev_private;
989 u32 reg = PIPEDSL(pipe);
990 u32 line1, line2;
991 u32 line_mask;
992
993 if (IS_GEN2(dev))
994 line_mask = DSL_LINEMASK_GEN2;
995 else
996 line_mask = DSL_LINEMASK_GEN3;
997
998 line1 = I915_READ(reg) & line_mask;
999 mdelay(5);
1000 line2 = I915_READ(reg) & line_mask;
1001
1002 return line1 == line2;
1003}
1004
1005/*
1006 * intel_wait_for_pipe_off - wait for pipe to turn off
1007 * @crtc: crtc whose pipe to wait for
1008 *
1009 * After disabling a pipe, we can't wait for vblank in the usual way,
1010 * spinning on the vblank interrupt status bit, since we won't actually
1011 * see an interrupt when the pipe is disabled.
1012 *
1013 * On Gen4 and above:
1014 * wait for the pipe register state bit to turn off
1015 *
1016 * Otherwise:
1017 * wait for the display line value to settle (it usually
1018 * ends up stopping at the start of the next frame).
1019 *
1020 */
1021static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
1022{
1023 struct drm_device *dev = crtc->base.dev;
1024 struct drm_i915_private *dev_priv = dev->dev_private;
1025 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
1026 enum pipe pipe = crtc->pipe;
1027
1028 if (INTEL_INFO(dev)->gen >= 4) {
1029 int reg = PIPECONF(cpu_transcoder);
1030
1031 /* Wait for the Pipe State to go off */
1032 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1033 100))
1034 WARN(1, "pipe_off wait timed out\n");
1035 } else {
1036 /* Wait for the display line to settle */
1037 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
1038 WARN(1, "pipe_off wait timed out\n");
1039 }
1040}
1041
1042/*
1043 * ibx_digital_port_connected - is the specified port connected?
1044 * @dev_priv: i915 private structure
1045 * @port: the port to test
1046 *
1047 * Returns true if @port is connected, false otherwise.
1048 */
1049bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1050 struct intel_digital_port *port)
1051{
1052 u32 bit;
1053
1054 if (HAS_PCH_IBX(dev_priv->dev)) {
1055 switch (port->port) {
1056 case PORT_B:
1057 bit = SDE_PORTB_HOTPLUG;
1058 break;
1059 case PORT_C:
1060 bit = SDE_PORTC_HOTPLUG;
1061 break;
1062 case PORT_D:
1063 bit = SDE_PORTD_HOTPLUG;
1064 break;
1065 default:
1066 return true;
1067 }
1068 } else {
1069 switch (port->port) {
1070 case PORT_B:
1071 bit = SDE_PORTB_HOTPLUG_CPT;
1072 break;
1073 case PORT_C:
1074 bit = SDE_PORTC_HOTPLUG_CPT;
1075 break;
1076 case PORT_D:
1077 bit = SDE_PORTD_HOTPLUG_CPT;
1078 break;
1079 default:
1080 return true;
1081 }
1082 }
1083
1084 return I915_READ(SDEISR) & bit;
1085}
1086
1087static const char *state_string(bool enabled)
1088{
1089 return enabled ? "on" : "off";
1090}
1091
1092/* Only for pre-ILK configs */
1093void assert_pll(struct drm_i915_private *dev_priv,
1094 enum pipe pipe, bool state)
1095{
1096 int reg;
1097 u32 val;
1098 bool cur_state;
1099
1100 reg = DPLL(pipe);
1101 val = I915_READ(reg);
1102 cur_state = !!(val & DPLL_VCO_ENABLE);
1103 I915_STATE_WARN(cur_state != state,
1104 "PLL state assertion failure (expected %s, current %s)\n",
1105 state_string(state), state_string(cur_state));
1106}
1107
1108/* XXX: the dsi pll is shared between MIPI DSI ports */
1109static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1110{
1111 u32 val;
1112 bool cur_state;
1113
1114 mutex_lock(&dev_priv->dpio_lock);
1115 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1116 mutex_unlock(&dev_priv->dpio_lock);
1117
1118 cur_state = val & DSI_PLL_VCO_EN;
1119 I915_STATE_WARN(cur_state != state,
1120 "DSI PLL state assertion failure (expected %s, current %s)\n",
1121 state_string(state), state_string(cur_state));
1122}
1123#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1124#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1125
1126struct intel_shared_dpll *
1127intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1128{
1129 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1130
1131 if (crtc->config->shared_dpll < 0)
1132 return NULL;
1133
1134 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
1135}
1136
1137/* For ILK+ */
1138void assert_shared_dpll(struct drm_i915_private *dev_priv,
1139 struct intel_shared_dpll *pll,
1140 bool state)
1141{
1142 bool cur_state;
1143 struct intel_dpll_hw_state hw_state;
1144
1145 if (WARN (!pll,
1146 "asserting DPLL %s with no DPLL\n", state_string(state)))
1147 return;
1148
1149 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
1150 I915_STATE_WARN(cur_state != state,
1151 "%s assertion failure (expected %s, current %s)\n",
1152 pll->name, state_string(state), state_string(cur_state));
1153}
1154
1155static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1156 enum pipe pipe, bool state)
1157{
1158 int reg;
1159 u32 val;
1160 bool cur_state;
1161 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1162 pipe);
1163
1164 if (HAS_DDI(dev_priv->dev)) {
1165 /* DDI does not have a specific FDI_TX register */
1166 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
1167 val = I915_READ(reg);
1168 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
1169 } else {
1170 reg = FDI_TX_CTL(pipe);
1171 val = I915_READ(reg);
1172 cur_state = !!(val & FDI_TX_ENABLE);
1173 }
1174 I915_STATE_WARN(cur_state != state,
1175 "FDI TX state assertion failure (expected %s, current %s)\n",
1176 state_string(state), state_string(cur_state));
1177}
1178#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1179#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1180
1181static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1182 enum pipe pipe, bool state)
1183{
1184 int reg;
1185 u32 val;
1186 bool cur_state;
1187
1188 reg = FDI_RX_CTL(pipe);
1189 val = I915_READ(reg);
1190 cur_state = !!(val & FDI_RX_ENABLE);
1191 I915_STATE_WARN(cur_state != state,
1192 "FDI RX state assertion failure (expected %s, current %s)\n",
1193 state_string(state), state_string(cur_state));
1194}
1195#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1196#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1197
1198static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1199 enum pipe pipe)
1200{
1201 int reg;
1202 u32 val;
1203
1204 /* ILK FDI PLL is always enabled */
1205 if (INTEL_INFO(dev_priv->dev)->gen == 5)
1206 return;
1207
1208 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
1209 if (HAS_DDI(dev_priv->dev))
1210 return;
1211
1212 reg = FDI_TX_CTL(pipe);
1213 val = I915_READ(reg);
1214 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1215}
1216
1217void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1218 enum pipe pipe, bool state)
1219{
1220 int reg;
1221 u32 val;
1222 bool cur_state;
1223
1224 reg = FDI_RX_CTL(pipe);
1225 val = I915_READ(reg);
1226 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1227 I915_STATE_WARN(cur_state != state,
1228 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1229 state_string(state), state_string(cur_state));
1230}
1231
1232void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1233 enum pipe pipe)
1234{
1235 struct drm_device *dev = dev_priv->dev;
1236 int pp_reg;
1237 u32 val;
1238 enum pipe panel_pipe = PIPE_A;
1239 bool locked = true;
1240
1241 if (WARN_ON(HAS_DDI(dev)))
1242 return;
1243
1244 if (HAS_PCH_SPLIT(dev)) {
1245 u32 port_sel;
1246
1247 pp_reg = PCH_PP_CONTROL;
1248 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1249
1250 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1251 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1252 panel_pipe = PIPE_B;
1253 /* XXX: else fix for eDP */
1254 } else if (IS_VALLEYVIEW(dev)) {
1255 /* presumably write lock depends on pipe, not port select */
1256 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1257 panel_pipe = pipe;
1258 } else {
1259 pp_reg = PP_CONTROL;
1260 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1261 panel_pipe = PIPE_B;
1262 }
1263
1264 val = I915_READ(pp_reg);
1265 if (!(val & PANEL_POWER_ON) ||
1266 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
1267 locked = false;
1268
1269 I915_STATE_WARN(panel_pipe == pipe && locked,
1270 "panel assertion failure, pipe %c regs locked\n",
1271 pipe_name(pipe));
1272}
1273
1274static void assert_cursor(struct drm_i915_private *dev_priv,
1275 enum pipe pipe, bool state)
1276{
1277 struct drm_device *dev = dev_priv->dev;
1278 bool cur_state;
1279
1280 if (IS_845G(dev) || IS_I865G(dev))
1281 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
1282 else
1283 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
1284
1285 I915_STATE_WARN(cur_state != state,
1286 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1287 pipe_name(pipe), state_string(state), state_string(cur_state));
1288}
1289#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1290#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1291
1292void assert_pipe(struct drm_i915_private *dev_priv,
1293 enum pipe pipe, bool state)
1294{
1295 int reg;
1296 u32 val;
1297 bool cur_state;
1298 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1299 pipe);
1300
1301 /* if we need the pipe quirk it must be always on */
1302 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1303 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
1304 state = true;
1305
1306 if (!intel_display_power_is_enabled(dev_priv,
1307 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
1308 cur_state = false;
1309 } else {
1310 reg = PIPECONF(cpu_transcoder);
1311 val = I915_READ(reg);
1312 cur_state = !!(val & PIPECONF_ENABLE);
1313 }
1314
1315 I915_STATE_WARN(cur_state != state,
1316 "pipe %c assertion failure (expected %s, current %s)\n",
1317 pipe_name(pipe), state_string(state), state_string(cur_state));
1318}
1319
1320static void assert_plane(struct drm_i915_private *dev_priv,
1321 enum plane plane, bool state)
1322{
1323 int reg;
1324 u32 val;
1325 bool cur_state;
1326
1327 reg = DSPCNTR(plane);
1328 val = I915_READ(reg);
1329 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1330 I915_STATE_WARN(cur_state != state,
1331 "plane %c assertion failure (expected %s, current %s)\n",
1332 plane_name(plane), state_string(state), state_string(cur_state));
1333}
1334
1335#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1336#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1337
1338static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1339 enum pipe pipe)
1340{
1341 struct drm_device *dev = dev_priv->dev;
1342 int reg, i;
1343 u32 val;
1344 int cur_pipe;
1345
1346 /* Primary planes are fixed to pipes on gen4+ */
1347 if (INTEL_INFO(dev)->gen >= 4) {
1348 reg = DSPCNTR(pipe);
1349 val = I915_READ(reg);
1350 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
1351 "plane %c assertion failure, should be disabled but not\n",
1352 plane_name(pipe));
1353 return;
1354 }
1355
1356 /* Need to check both planes against the pipe */
1357 for_each_pipe(dev_priv, i) {
1358 reg = DSPCNTR(i);
1359 val = I915_READ(reg);
1360 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1361 DISPPLANE_SEL_PIPE_SHIFT;
1362 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
1363 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1364 plane_name(i), pipe_name(pipe));
1365 }
1366}
1367
1368static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe)
1370{
1371 struct drm_device *dev = dev_priv->dev;
1372 int reg, sprite;
1373 u32 val;
1374
1375 if (INTEL_INFO(dev)->gen >= 9) {
1376 for_each_sprite(dev_priv, pipe, sprite) {
1377 val = I915_READ(PLANE_CTL(pipe, sprite));
1378 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
1379 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1380 sprite, pipe_name(pipe));
1381 }
1382 } else if (IS_VALLEYVIEW(dev)) {
1383 for_each_sprite(dev_priv, pipe, sprite) {
1384 reg = SPCNTR(pipe, sprite);
1385 val = I915_READ(reg);
1386 I915_STATE_WARN(val & SP_ENABLE,
1387 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1388 sprite_name(pipe, sprite), pipe_name(pipe));
1389 }
1390 } else if (INTEL_INFO(dev)->gen >= 7) {
1391 reg = SPRCTL(pipe);
1392 val = I915_READ(reg);
1393 I915_STATE_WARN(val & SPRITE_ENABLE,
1394 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1395 plane_name(pipe), pipe_name(pipe));
1396 } else if (INTEL_INFO(dev)->gen >= 5) {
1397 reg = DVSCNTR(pipe);
1398 val = I915_READ(reg);
1399 I915_STATE_WARN(val & DVS_ENABLE,
1400 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1401 plane_name(pipe), pipe_name(pipe));
1402 }
1403}
1404
1405static void assert_vblank_disabled(struct drm_crtc *crtc)
1406{
1407 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1408 drm_crtc_vblank_put(crtc);
1409}
1410
1411static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1412{
1413 u32 val;
1414 bool enabled;
1415
1416 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
1417
1418 val = I915_READ(PCH_DREF_CONTROL);
1419 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1420 DREF_SUPERSPREAD_SOURCE_MASK));
1421 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1422}
1423
1424static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1425 enum pipe pipe)
1426{
1427 int reg;
1428 u32 val;
1429 bool enabled;
1430
1431 reg = PCH_TRANSCONF(pipe);
1432 val = I915_READ(reg);
1433 enabled = !!(val & TRANS_ENABLE);
1434 I915_STATE_WARN(enabled,
1435 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1436 pipe_name(pipe));
1437}
1438
1439static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1440 enum pipe pipe, u32 port_sel, u32 val)
1441{
1442 if ((val & DP_PORT_EN) == 0)
1443 return false;
1444
1445 if (HAS_PCH_CPT(dev_priv->dev)) {
1446 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1447 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1448 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1449 return false;
1450 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1451 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1452 return false;
1453 } else {
1454 if ((val & DP_PIPE_MASK) != (pipe << 30))
1455 return false;
1456 }
1457 return true;
1458}
1459
1460static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1461 enum pipe pipe, u32 val)
1462{
1463 if ((val & SDVO_ENABLE) == 0)
1464 return false;
1465
1466 if (HAS_PCH_CPT(dev_priv->dev)) {
1467 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1468 return false;
1469 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1470 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1471 return false;
1472 } else {
1473 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1474 return false;
1475 }
1476 return true;
1477}
1478
1479static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1480 enum pipe pipe, u32 val)
1481{
1482 if ((val & LVDS_PORT_EN) == 0)
1483 return false;
1484
1485 if (HAS_PCH_CPT(dev_priv->dev)) {
1486 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1487 return false;
1488 } else {
1489 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1490 return false;
1491 }
1492 return true;
1493}
1494
1495static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1496 enum pipe pipe, u32 val)
1497{
1498 if ((val & ADPA_DAC_ENABLE) == 0)
1499 return false;
1500 if (HAS_PCH_CPT(dev_priv->dev)) {
1501 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1502 return false;
1503 } else {
1504 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1505 return false;
1506 }
1507 return true;
1508}
1509
1510static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1511 enum pipe pipe, int reg, u32 port_sel)
1512{
1513 u32 val = I915_READ(reg);
1514 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
1515 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1516 reg, pipe_name(pipe));
1517
1518 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1519 && (val & DP_PIPEB_SELECT),
1520 "IBX PCH dp port still using transcoder B\n");
1521}
1522
1523static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1524 enum pipe pipe, int reg)
1525{
1526 u32 val = I915_READ(reg);
1527 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
1528 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1529 reg, pipe_name(pipe));
1530
1531 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
1532 && (val & SDVO_PIPE_B_SELECT),
1533 "IBX PCH hdmi port still using transcoder B\n");
1534}
1535
1536static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1537 enum pipe pipe)
1538{
1539 int reg;
1540 u32 val;
1541
1542 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1543 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1544 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1545
1546 reg = PCH_ADPA;
1547 val = I915_READ(reg);
1548 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
1549 "PCH VGA enabled on transcoder %c, should be disabled\n",
1550 pipe_name(pipe));
1551
1552 reg = PCH_LVDS;
1553 val = I915_READ(reg);
1554 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
1555 "PCH LVDS enabled on transcoder %c, should be disabled\n",
1556 pipe_name(pipe));
1557
1558 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1559 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1560 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
1561}
1562
1563static void intel_init_dpio(struct drm_device *dev)
1564{
1565 struct drm_i915_private *dev_priv = dev->dev_private;
1566
1567 if (!IS_VALLEYVIEW(dev))
1568 return;
1569
1570 /*
1571 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1572 * CHV x1 PHY (DP/HDMI D)
1573 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1574 */
1575 if (IS_CHERRYVIEW(dev)) {
1576 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1577 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1578 } else {
1579 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1580 }
1581}
1582
1583static void vlv_enable_pll(struct intel_crtc *crtc,
1584 const struct intel_crtc_state *pipe_config)
1585{
1586 struct drm_device *dev = crtc->base.dev;
1587 struct drm_i915_private *dev_priv = dev->dev_private;
1588 int reg = DPLL(crtc->pipe);
1589 u32 dpll = pipe_config->dpll_hw_state.dpll;
1590
1591 assert_pipe_disabled(dev_priv, crtc->pipe);
1592
1593 /* No really, not for ILK+ */
1594 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1595
1596 /* PLL is protected by panel, make sure we can write it */
1597 if (IS_MOBILE(dev_priv->dev))
1598 assert_panel_unlocked(dev_priv, crtc->pipe);
1599
1600 I915_WRITE(reg, dpll);
1601 POSTING_READ(reg);
1602 udelay(150);
1603
1604 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1605 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1606
1607 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
1608 POSTING_READ(DPLL_MD(crtc->pipe));
1609
1610 /* We do this three times for luck */
1611 I915_WRITE(reg, dpll);
1612 POSTING_READ(reg);
1613 udelay(150); /* wait for warmup */
1614 I915_WRITE(reg, dpll);
1615 POSTING_READ(reg);
1616 udelay(150); /* wait for warmup */
1617 I915_WRITE(reg, dpll);
1618 POSTING_READ(reg);
1619 udelay(150); /* wait for warmup */
1620}
1621
1622static void chv_enable_pll(struct intel_crtc *crtc,
1623 const struct intel_crtc_state *pipe_config)
1624{
1625 struct drm_device *dev = crtc->base.dev;
1626 struct drm_i915_private *dev_priv = dev->dev_private;
1627 int pipe = crtc->pipe;
1628 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1629 u32 tmp;
1630
1631 assert_pipe_disabled(dev_priv, crtc->pipe);
1632
1633 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1634
1635 mutex_lock(&dev_priv->dpio_lock);
1636
1637 /* Enable back the 10bit clock to display controller */
1638 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1639 tmp |= DPIO_DCLKP_EN;
1640 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1641
1642 /*
1643 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1644 */
1645 udelay(1);
1646
1647 /* Enable PLL */
1648 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
1649
1650 /* Check PLL is locked */
1651 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1652 DRM_ERROR("PLL %d failed to lock\n", pipe);
1653
1654 /* not sure when this should be written */
1655 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
1656 POSTING_READ(DPLL_MD(pipe));
1657
1658 mutex_unlock(&dev_priv->dpio_lock);
1659}
1660
1661static int intel_num_dvo_pipes(struct drm_device *dev)
1662{
1663 struct intel_crtc *crtc;
1664 int count = 0;
1665
1666 for_each_intel_crtc(dev, crtc)
1667 count += crtc->active &&
1668 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1669
1670 return count;
1671}
1672
1673static void i9xx_enable_pll(struct intel_crtc *crtc)
1674{
1675 struct drm_device *dev = crtc->base.dev;
1676 struct drm_i915_private *dev_priv = dev->dev_private;
1677 int reg = DPLL(crtc->pipe);
1678 u32 dpll = crtc->config->dpll_hw_state.dpll;
1679
1680 assert_pipe_disabled(dev_priv, crtc->pipe);
1681
1682 /* No really, not for ILK+ */
1683 BUG_ON(INTEL_INFO(dev)->gen >= 5);
1684
1685 /* PLL is protected by panel, make sure we can write it */
1686 if (IS_MOBILE(dev) && !IS_I830(dev))
1687 assert_panel_unlocked(dev_priv, crtc->pipe);
1688
1689 /* Enable DVO 2x clock on both PLLs if necessary */
1690 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1691 /*
1692 * It appears to be important that we don't enable this
1693 * for the current pipe before otherwise configuring the
1694 * PLL. No idea how this should be handled if multiple
1695 * DVO outputs are enabled simultaneosly.
1696 */
1697 dpll |= DPLL_DVO_2X_MODE;
1698 I915_WRITE(DPLL(!crtc->pipe),
1699 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1700 }
1701
1702 /* Wait for the clocks to stabilize. */
1703 POSTING_READ(reg);
1704 udelay(150);
1705
1706 if (INTEL_INFO(dev)->gen >= 4) {
1707 I915_WRITE(DPLL_MD(crtc->pipe),
1708 crtc->config->dpll_hw_state.dpll_md);
1709 } else {
1710 /* The pixel multiplier can only be updated once the
1711 * DPLL is enabled and the clocks are stable.
1712 *
1713 * So write it again.
1714 */
1715 I915_WRITE(reg, dpll);
1716 }
1717
1718 /* We do this three times for luck */
1719 I915_WRITE(reg, dpll);
1720 POSTING_READ(reg);
1721 udelay(150); /* wait for warmup */
1722 I915_WRITE(reg, dpll);
1723 POSTING_READ(reg);
1724 udelay(150); /* wait for warmup */
1725 I915_WRITE(reg, dpll);
1726 POSTING_READ(reg);
1727 udelay(150); /* wait for warmup */
1728}
1729
1730/**
1731 * i9xx_disable_pll - disable a PLL
1732 * @dev_priv: i915 private structure
1733 * @pipe: pipe PLL to disable
1734 *
1735 * Disable the PLL for @pipe, making sure the pipe is off first.
1736 *
1737 * Note! This is for pre-ILK only.
1738 */
1739static void i9xx_disable_pll(struct intel_crtc *crtc)
1740{
1741 struct drm_device *dev = crtc->base.dev;
1742 struct drm_i915_private *dev_priv = dev->dev_private;
1743 enum pipe pipe = crtc->pipe;
1744
1745 /* Disable DVO 2x clock on both PLLs if necessary */
1746 if (IS_I830(dev) &&
1747 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
1748 intel_num_dvo_pipes(dev) == 1) {
1749 I915_WRITE(DPLL(PIPE_B),
1750 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1751 I915_WRITE(DPLL(PIPE_A),
1752 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1753 }
1754
1755 /* Don't disable pipe or pipe PLLs if needed */
1756 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1757 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
1758 return;
1759
1760 /* Make sure the pipe isn't still relying on us */
1761 assert_pipe_disabled(dev_priv, pipe);
1762
1763 I915_WRITE(DPLL(pipe), 0);
1764 POSTING_READ(DPLL(pipe));
1765}
1766
1767static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1768{
1769 u32 val = 0;
1770
1771 /* Make sure the pipe isn't still relying on us */
1772 assert_pipe_disabled(dev_priv, pipe);
1773
1774 /*
1775 * Leave integrated clock source and reference clock enabled for pipe B.
1776 * The latter is needed for VGA hotplug / manual detection.
1777 */
1778 if (pipe == PIPE_B)
1779 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
1780 I915_WRITE(DPLL(pipe), val);
1781 POSTING_READ(DPLL(pipe));
1782
1783}
1784
1785static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1786{
1787 enum dpio_channel port = vlv_pipe_to_channel(pipe);
1788 u32 val;
1789
1790 /* Make sure the pipe isn't still relying on us */
1791 assert_pipe_disabled(dev_priv, pipe);
1792
1793 /* Set PLL en = 0 */
1794 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
1795 if (pipe != PIPE_A)
1796 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1797 I915_WRITE(DPLL(pipe), val);
1798 POSTING_READ(DPLL(pipe));
1799
1800 mutex_lock(&dev_priv->dpio_lock);
1801
1802 /* Disable 10bit clock to display controller */
1803 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1804 val &= ~DPIO_DCLKP_EN;
1805 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1806
1807 /* disable left/right clock distribution */
1808 if (pipe != PIPE_B) {
1809 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1810 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1811 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1812 } else {
1813 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1814 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1815 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1816 }
1817
1818 mutex_unlock(&dev_priv->dpio_lock);
1819}
1820
1821void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1822 struct intel_digital_port *dport)
1823{
1824 u32 port_mask;
1825 int dpll_reg;
1826
1827 switch (dport->port) {
1828 case PORT_B:
1829 port_mask = DPLL_PORTB_READY_MASK;
1830 dpll_reg = DPLL(0);
1831 break;
1832 case PORT_C:
1833 port_mask = DPLL_PORTC_READY_MASK;
1834 dpll_reg = DPLL(0);
1835 break;
1836 case PORT_D:
1837 port_mask = DPLL_PORTD_READY_MASK;
1838 dpll_reg = DPIO_PHY_STATUS;
1839 break;
1840 default:
1841 BUG();
1842 }
1843
1844 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
1845 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
1846 port_name(dport->port), I915_READ(dpll_reg));
1847}
1848
1849static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1850{
1851 struct drm_device *dev = crtc->base.dev;
1852 struct drm_i915_private *dev_priv = dev->dev_private;
1853 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1854
1855 if (WARN_ON(pll == NULL))
1856 return;
1857
1858 WARN_ON(!pll->config.crtc_mask);
1859 if (pll->active == 0) {
1860 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1861 WARN_ON(pll->on);
1862 assert_shared_dpll_disabled(dev_priv, pll);
1863
1864 pll->mode_set(dev_priv, pll);
1865 }
1866}
1867
1868/**
1869 * intel_enable_shared_dpll - enable PCH PLL
1870 * @dev_priv: i915 private structure
1871 * @pipe: pipe PLL to enable
1872 *
1873 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1874 * drives the transcoder clock.
1875 */
1876static void intel_enable_shared_dpll(struct intel_crtc *crtc)
1877{
1878 struct drm_device *dev = crtc->base.dev;
1879 struct drm_i915_private *dev_priv = dev->dev_private;
1880 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1881
1882 if (WARN_ON(pll == NULL))
1883 return;
1884
1885 if (WARN_ON(pll->config.crtc_mask == 0))
1886 return;
1887
1888 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
1889 pll->name, pll->active, pll->on,
1890 crtc->base.base.id);
1891
1892 if (pll->active++) {
1893 WARN_ON(!pll->on);
1894 assert_shared_dpll_enabled(dev_priv, pll);
1895 return;
1896 }
1897 WARN_ON(pll->on);
1898
1899 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1900
1901 DRM_DEBUG_KMS("enabling %s\n", pll->name);
1902 pll->enable(dev_priv, pll);
1903 pll->on = true;
1904}
1905
1906static void intel_disable_shared_dpll(struct intel_crtc *crtc)
1907{
1908 struct drm_device *dev = crtc->base.dev;
1909 struct drm_i915_private *dev_priv = dev->dev_private;
1910 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1911
1912 /* PCH only available on ILK+ */
1913 BUG_ON(INTEL_INFO(dev)->gen < 5);
1914 if (WARN_ON(pll == NULL))
1915 return;
1916
1917 if (WARN_ON(pll->config.crtc_mask == 0))
1918 return;
1919
1920 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1921 pll->name, pll->active, pll->on,
1922 crtc->base.base.id);
1923
1924 if (WARN_ON(pll->active == 0)) {
1925 assert_shared_dpll_disabled(dev_priv, pll);
1926 return;
1927 }
1928
1929 assert_shared_dpll_enabled(dev_priv, pll);
1930 WARN_ON(!pll->on);
1931 if (--pll->active)
1932 return;
1933
1934 DRM_DEBUG_KMS("disabling %s\n", pll->name);
1935 pll->disable(dev_priv, pll);
1936 pll->on = false;
1937
1938 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
1939}
1940
1941static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1942 enum pipe pipe)
1943{
1944 struct drm_device *dev = dev_priv->dev;
1945 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1947 uint32_t reg, val, pipeconf_val;
1948
1949 /* PCH only available on ILK+ */
1950 BUG_ON(!HAS_PCH_SPLIT(dev));
1951
1952 /* Make sure PCH DPLL is enabled */
1953 assert_shared_dpll_enabled(dev_priv,
1954 intel_crtc_to_shared_dpll(intel_crtc));
1955
1956 /* FDI must be feeding us bits for PCH ports */
1957 assert_fdi_tx_enabled(dev_priv, pipe);
1958 assert_fdi_rx_enabled(dev_priv, pipe);
1959
1960 if (HAS_PCH_CPT(dev)) {
1961 /* Workaround: Set the timing override bit before enabling the
1962 * pch transcoder. */
1963 reg = TRANS_CHICKEN2(pipe);
1964 val = I915_READ(reg);
1965 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1966 I915_WRITE(reg, val);
1967 }
1968
1969 reg = PCH_TRANSCONF(pipe);
1970 val = I915_READ(reg);
1971 pipeconf_val = I915_READ(PIPECONF(pipe));
1972
1973 if (HAS_PCH_IBX(dev_priv->dev)) {
1974 /*
1975 * make the BPC in transcoder be consistent with
1976 * that in pipeconf reg.
1977 */
1978 val &= ~PIPECONF_BPC_MASK;
1979 val |= pipeconf_val & PIPECONF_BPC_MASK;
1980 }
1981
1982 val &= ~TRANS_INTERLACE_MASK;
1983 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1984 if (HAS_PCH_IBX(dev_priv->dev) &&
1985 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
1986 val |= TRANS_LEGACY_INTERLACED_ILK;
1987 else
1988 val |= TRANS_INTERLACED;
1989 else
1990 val |= TRANS_PROGRESSIVE;
1991
1992 I915_WRITE(reg, val | TRANS_ENABLE);
1993 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1994 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
1995}
1996
1997static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1998 enum transcoder cpu_transcoder)
1999{
2000 u32 val, pipeconf_val;
2001
2002 /* PCH only available on ILK+ */
2003 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
2004
2005 /* FDI must be feeding us bits for PCH ports */
2006 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
2007 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
2008
2009 /* Workaround: set timing override bit. */
2010 val = I915_READ(_TRANSA_CHICKEN2);
2011 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2012 I915_WRITE(_TRANSA_CHICKEN2, val);
2013
2014 val = TRANS_ENABLE;
2015 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
2016
2017 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2018 PIPECONF_INTERLACED_ILK)
2019 val |= TRANS_INTERLACED;
2020 else
2021 val |= TRANS_PROGRESSIVE;
2022
2023 I915_WRITE(LPT_TRANSCONF, val);
2024 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
2025 DRM_ERROR("Failed to enable PCH transcoder\n");
2026}
2027
2028static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2029 enum pipe pipe)
2030{
2031 struct drm_device *dev = dev_priv->dev;
2032 uint32_t reg, val;
2033
2034 /* FDI relies on the transcoder */
2035 assert_fdi_tx_disabled(dev_priv, pipe);
2036 assert_fdi_rx_disabled(dev_priv, pipe);
2037
2038 /* Ports must be off as well */
2039 assert_pch_ports_disabled(dev_priv, pipe);
2040
2041 reg = PCH_TRANSCONF(pipe);
2042 val = I915_READ(reg);
2043 val &= ~TRANS_ENABLE;
2044 I915_WRITE(reg, val);
2045 /* wait for PCH transcoder off, transcoder state */
2046 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
2047 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
2048
2049 if (!HAS_PCH_IBX(dev)) {
2050 /* Workaround: Clear the timing override chicken bit again. */
2051 reg = TRANS_CHICKEN2(pipe);
2052 val = I915_READ(reg);
2053 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2054 I915_WRITE(reg, val);
2055 }
2056}
2057
2058static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
2059{
2060 u32 val;
2061
2062 val = I915_READ(LPT_TRANSCONF);
2063 val &= ~TRANS_ENABLE;
2064 I915_WRITE(LPT_TRANSCONF, val);
2065 /* wait for PCH transcoder off, transcoder state */
2066 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
2067 DRM_ERROR("Failed to disable PCH transcoder\n");
2068
2069 /* Workaround: clear timing override bit. */
2070 val = I915_READ(_TRANSA_CHICKEN2);
2071 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2072 I915_WRITE(_TRANSA_CHICKEN2, val);
2073}
2074
2075/**
2076 * intel_enable_pipe - enable a pipe, asserting requirements
2077 * @crtc: crtc responsible for the pipe
2078 *
2079 * Enable @crtc's pipe, making sure that various hardware specific requirements
2080 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
2081 */
2082static void intel_enable_pipe(struct intel_crtc *crtc)
2083{
2084 struct drm_device *dev = crtc->base.dev;
2085 struct drm_i915_private *dev_priv = dev->dev_private;
2086 enum pipe pipe = crtc->pipe;
2087 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2088 pipe);
2089 enum pipe pch_transcoder;
2090 int reg;
2091 u32 val;
2092
2093 assert_planes_disabled(dev_priv, pipe);
2094 assert_cursor_disabled(dev_priv, pipe);
2095 assert_sprites_disabled(dev_priv, pipe);
2096
2097 if (HAS_PCH_LPT(dev_priv->dev))
2098 pch_transcoder = TRANSCODER_A;
2099 else
2100 pch_transcoder = pipe;
2101
2102 /*
2103 * A pipe without a PLL won't actually be able to drive bits from
2104 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2105 * need the check.
2106 */
2107 if (!HAS_PCH_SPLIT(dev_priv->dev))
2108 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
2109 assert_dsi_pll_enabled(dev_priv);
2110 else
2111 assert_pll_enabled(dev_priv, pipe);
2112 else {
2113 if (crtc->config->has_pch_encoder) {
2114 /* if driving the PCH, we need FDI enabled */
2115 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
2116 assert_fdi_tx_pll_enabled(dev_priv,
2117 (enum pipe) cpu_transcoder);
2118 }
2119 /* FIXME: assert CPU port conditions for SNB+ */
2120 }
2121
2122 reg = PIPECONF(cpu_transcoder);
2123 val = I915_READ(reg);
2124 if (val & PIPECONF_ENABLE) {
2125 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2126 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
2127 return;
2128 }
2129
2130 I915_WRITE(reg, val | PIPECONF_ENABLE);
2131 POSTING_READ(reg);
2132}
2133
2134/**
2135 * intel_disable_pipe - disable a pipe, asserting requirements
2136 * @crtc: crtc whose pipes is to be disabled
2137 *
2138 * Disable the pipe of @crtc, making sure that various hardware
2139 * specific requirements are met, if applicable, e.g. plane
2140 * disabled, panel fitter off, etc.
2141 *
2142 * Will wait until the pipe has shut down before returning.
2143 */
2144static void intel_disable_pipe(struct intel_crtc *crtc)
2145{
2146 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2147 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
2148 enum pipe pipe = crtc->pipe;
2149 int reg;
2150 u32 val;
2151
2152 /*
2153 * Make sure planes won't keep trying to pump pixels to us,
2154 * or we might hang the display.
2155 */
2156 assert_planes_disabled(dev_priv, pipe);
2157 assert_cursor_disabled(dev_priv, pipe);
2158 assert_sprites_disabled(dev_priv, pipe);
2159
2160 reg = PIPECONF(cpu_transcoder);
2161 val = I915_READ(reg);
2162 if ((val & PIPECONF_ENABLE) == 0)
2163 return;
2164
2165 /*
2166 * Double wide has implications for planes
2167 * so best keep it disabled when not needed.
2168 */
2169 if (crtc->config->double_wide)
2170 val &= ~PIPECONF_DOUBLE_WIDE;
2171
2172 /* Don't disable pipe or pipe PLLs if needed */
2173 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2174 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
2175 val &= ~PIPECONF_ENABLE;
2176
2177 I915_WRITE(reg, val);
2178 if ((val & PIPECONF_ENABLE) == 0)
2179 intel_wait_for_pipe_off(crtc);
2180}
2181
2182/*
2183 * Plane regs are double buffered, going from enabled->disabled needs a
2184 * trigger in order to latch. The display address reg provides this.
2185 */
2186void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2187 enum plane plane)
2188{
2189 struct drm_device *dev = dev_priv->dev;
2190 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
2191
2192 I915_WRITE(reg, I915_READ(reg));
2193 POSTING_READ(reg);
2194}
2195
2196/**
2197 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
2198 * @plane: plane to be enabled
2199 * @crtc: crtc for the plane
2200 *
2201 * Enable @plane on @crtc, making sure that the pipe is running first.
2202 */
2203static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2204 struct drm_crtc *crtc)
2205{
2206 struct drm_device *dev = plane->dev;
2207 struct drm_i915_private *dev_priv = dev->dev_private;
2208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2209
2210 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2211 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
2212
2213 if (intel_crtc->primary_enabled)
2214 return;
2215
2216 intel_crtc->primary_enabled = true;
2217
2218 dev_priv->display.update_primary_plane(crtc, plane->fb,
2219 crtc->x, crtc->y);
2220
2221 /*
2222 * BDW signals flip done immediately if the plane
2223 * is disabled, even if the plane enable is already
2224 * armed to occur at the next vblank :(
2225 */
2226 if (IS_BROADWELL(dev))
2227 intel_wait_for_vblank(dev, intel_crtc->pipe);
2228}
2229
2230/**
2231 * intel_disable_primary_hw_plane - disable the primary hardware plane
2232 * @plane: plane to be disabled
2233 * @crtc: crtc for the plane
2234 *
2235 * Disable @plane on @crtc, making sure that the pipe is running first.
2236 */
2237static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2238 struct drm_crtc *crtc)
2239{
2240 struct drm_device *dev = plane->dev;
2241 struct drm_i915_private *dev_priv = dev->dev_private;
2242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2243
2244 if (WARN_ON(!intel_crtc->active))
2245 return;
2246
2247 if (!intel_crtc->primary_enabled)
2248 return;
2249
2250 intel_crtc->primary_enabled = false;
2251
2252 dev_priv->display.update_primary_plane(crtc, plane->fb,
2253 crtc->x, crtc->y);
2254}
2255
2256static bool need_vtd_wa(struct drm_device *dev)
2257{
2258#ifdef CONFIG_INTEL_IOMMU
2259 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2260 return true;
2261#endif
2262 return false;
2263}
2264
2265unsigned int
2266intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2267 uint64_t fb_format_modifier)
2268{
2269 unsigned int tile_height;
2270 uint32_t pixel_bytes;
2271
2272 switch (fb_format_modifier) {
2273 case DRM_FORMAT_MOD_NONE:
2274 tile_height = 1;
2275 break;
2276 case I915_FORMAT_MOD_X_TILED:
2277 tile_height = IS_GEN2(dev) ? 16 : 8;
2278 break;
2279 case I915_FORMAT_MOD_Y_TILED:
2280 tile_height = 32;
2281 break;
2282 case I915_FORMAT_MOD_Yf_TILED:
2283 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2284 switch (pixel_bytes) {
2285 default:
2286 case 1:
2287 tile_height = 64;
2288 break;
2289 case 2:
2290 case 4:
2291 tile_height = 32;
2292 break;
2293 case 8:
2294 tile_height = 16;
2295 break;
2296 case 16:
2297 WARN_ONCE(1,
2298 "128-bit pixels are not supported for display!");
2299 tile_height = 16;
2300 break;
2301 }
2302 break;
2303 default:
2304 MISSING_CASE(fb_format_modifier);
2305 tile_height = 1;
2306 break;
2307 }
2308
2309 return tile_height;
2310}
2311
2312unsigned int
2313intel_fb_align_height(struct drm_device *dev, unsigned int height,
2314 uint32_t pixel_format, uint64_t fb_format_modifier)
2315{
2316 return ALIGN(height, intel_tile_height(dev, pixel_format,
2317 fb_format_modifier));
2318}
2319
2320static int
2321intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2322 const struct drm_plane_state *plane_state)
2323{
2324 struct intel_rotation_info *info = &view->rotation_info;
2325
2326 *view = i915_ggtt_view_normal;
2327
2328 if (!plane_state)
2329 return 0;
2330
2331 if (!intel_rotation_90_or_270(plane_state->rotation))
2332 return 0;
2333
2334 *view = i915_ggtt_view_rotated;
2335
2336 info->height = fb->height;
2337 info->pixel_format = fb->pixel_format;
2338 info->pitch = fb->pitches[0];
2339 info->fb_modifier = fb->modifier[0];
2340
2341 if (!(info->fb_modifier == I915_FORMAT_MOD_Y_TILED ||
2342 info->fb_modifier == I915_FORMAT_MOD_Yf_TILED)) {
2343 DRM_DEBUG_KMS(
2344 "Y or Yf tiling is needed for 90/270 rotation!\n");
2345 return -EINVAL;
2346 }
2347
2348 return 0;
2349}
2350
2351int
2352intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2353 struct drm_framebuffer *fb,
2354 const struct drm_plane_state *plane_state,
2355 struct intel_engine_cs *pipelined)
2356{
2357 struct drm_device *dev = fb->dev;
2358 struct drm_i915_private *dev_priv = dev->dev_private;
2359 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2360 struct i915_ggtt_view view;
2361 u32 alignment;
2362 int ret;
2363
2364 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2365
2366 switch (fb->modifier[0]) {
2367 case DRM_FORMAT_MOD_NONE:
2368 if (INTEL_INFO(dev)->gen >= 9)
2369 alignment = 256 * 1024;
2370 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2371 alignment = 128 * 1024;
2372 else if (INTEL_INFO(dev)->gen >= 4)
2373 alignment = 4 * 1024;
2374 else
2375 alignment = 64 * 1024;
2376 break;
2377 case I915_FORMAT_MOD_X_TILED:
2378 if (INTEL_INFO(dev)->gen >= 9)
2379 alignment = 256 * 1024;
2380 else {
2381 /* pin() will align the object as required by fence */
2382 alignment = 0;
2383 }
2384 break;
2385 case I915_FORMAT_MOD_Y_TILED:
2386 case I915_FORMAT_MOD_Yf_TILED:
2387 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2388 "Y tiling bo slipped through, driver bug!\n"))
2389 return -EINVAL;
2390 alignment = 1 * 1024 * 1024;
2391 break;
2392 default:
2393 MISSING_CASE(fb->modifier[0]);
2394 return -EINVAL;
2395 }
2396
2397 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2398 if (ret)
2399 return ret;
2400
2401 /* Note that the w/a also requires 64 PTE of padding following the
2402 * bo. We currently fill all unused PTE with the shadow page and so
2403 * we should always have valid PTE following the scanout preventing
2404 * the VT-d warning.
2405 */
2406 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2407 alignment = 256 * 1024;
2408
2409 /*
2410 * Global gtt pte registers are special registers which actually forward
2411 * writes to a chunk of system memory. Which means that there is no risk
2412 * that the register values disappear as soon as we call
2413 * intel_runtime_pm_put(), so it is correct to wrap only the
2414 * pin/unpin/fence and not more.
2415 */
2416 intel_runtime_pm_get(dev_priv);
2417
2418 dev_priv->mm.interruptible = false;
2419 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
2420 &view);
2421 if (ret)
2422 goto err_interruptible;
2423
2424 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2425 * fence, whereas 965+ only requires a fence if using
2426 * framebuffer compression. For simplicity, we always install
2427 * a fence as the cost is not that onerous.
2428 */
2429 ret = i915_gem_object_get_fence(obj);
2430 if (ret)
2431 goto err_unpin;
2432
2433 i915_gem_object_pin_fence(obj);
2434
2435 dev_priv->mm.interruptible = true;
2436 intel_runtime_pm_put(dev_priv);
2437 return 0;
2438
2439err_unpin:
2440 i915_gem_object_unpin_from_display_plane(obj, &view);
2441err_interruptible:
2442 dev_priv->mm.interruptible = true;
2443 intel_runtime_pm_put(dev_priv);
2444 return ret;
2445}
2446
2447static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2448 const struct drm_plane_state *plane_state)
2449{
2450 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2451 struct i915_ggtt_view view;
2452 int ret;
2453
2454 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2455
2456 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2457 WARN_ONCE(ret, "Couldn't get view from plane state!");
2458
2459 i915_gem_object_unpin_fence(obj);
2460 i915_gem_object_unpin_from_display_plane(obj, &view);
2461}
2462
2463/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2464 * is assumed to be a power-of-two. */
2465unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2466 unsigned int tiling_mode,
2467 unsigned int cpp,
2468 unsigned int pitch)
2469{
2470 if (tiling_mode != I915_TILING_NONE) {
2471 unsigned int tile_rows, tiles;
2472
2473 tile_rows = *y / 8;
2474 *y %= 8;
2475
2476 tiles = *x / (512/cpp);
2477 *x %= 512/cpp;
2478
2479 return tile_rows * pitch * 8 + tiles * 4096;
2480 } else {
2481 unsigned int offset;
2482
2483 offset = *y * pitch + *x * cpp;
2484 *y = 0;
2485 *x = (offset & 4095) / cpp;
2486 return offset & -4096;
2487 }
2488}
2489
2490static int i9xx_format_to_fourcc(int format)
2491{
2492 switch (format) {
2493 case DISPPLANE_8BPP:
2494 return DRM_FORMAT_C8;
2495 case DISPPLANE_BGRX555:
2496 return DRM_FORMAT_XRGB1555;
2497 case DISPPLANE_BGRX565:
2498 return DRM_FORMAT_RGB565;
2499 default:
2500 case DISPPLANE_BGRX888:
2501 return DRM_FORMAT_XRGB8888;
2502 case DISPPLANE_RGBX888:
2503 return DRM_FORMAT_XBGR8888;
2504 case DISPPLANE_BGRX101010:
2505 return DRM_FORMAT_XRGB2101010;
2506 case DISPPLANE_RGBX101010:
2507 return DRM_FORMAT_XBGR2101010;
2508 }
2509}
2510
2511static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2512{
2513 switch (format) {
2514 case PLANE_CTL_FORMAT_RGB_565:
2515 return DRM_FORMAT_RGB565;
2516 default:
2517 case PLANE_CTL_FORMAT_XRGB_8888:
2518 if (rgb_order) {
2519 if (alpha)
2520 return DRM_FORMAT_ABGR8888;
2521 else
2522 return DRM_FORMAT_XBGR8888;
2523 } else {
2524 if (alpha)
2525 return DRM_FORMAT_ARGB8888;
2526 else
2527 return DRM_FORMAT_XRGB8888;
2528 }
2529 case PLANE_CTL_FORMAT_XRGB_2101010:
2530 if (rgb_order)
2531 return DRM_FORMAT_XBGR2101010;
2532 else
2533 return DRM_FORMAT_XRGB2101010;
2534 }
2535}
2536
2537static bool
2538intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2539 struct intel_initial_plane_config *plane_config)
2540{
2541 struct drm_device *dev = crtc->base.dev;
2542 struct drm_i915_gem_object *obj = NULL;
2543 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2544 struct drm_framebuffer *fb = &plane_config->fb->base;
2545 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2546 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2547 PAGE_SIZE);
2548
2549 size_aligned -= base_aligned;
2550
2551 if (plane_config->size == 0)
2552 return false;
2553
2554 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2555 base_aligned,
2556 base_aligned,
2557 size_aligned);
2558 if (!obj)
2559 return false;
2560
2561 obj->tiling_mode = plane_config->tiling;
2562 if (obj->tiling_mode == I915_TILING_X)
2563 obj->stride = fb->pitches[0];
2564
2565 mode_cmd.pixel_format = fb->pixel_format;
2566 mode_cmd.width = fb->width;
2567 mode_cmd.height = fb->height;
2568 mode_cmd.pitches[0] = fb->pitches[0];
2569 mode_cmd.modifier[0] = fb->modifier[0];
2570 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
2571
2572 mutex_lock(&dev->struct_mutex);
2573 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
2574 &mode_cmd, obj)) {
2575 DRM_DEBUG_KMS("intel fb init failed\n");
2576 goto out_unref_obj;
2577 }
2578 mutex_unlock(&dev->struct_mutex);
2579
2580 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
2581 return true;
2582
2583out_unref_obj:
2584 drm_gem_object_unreference(&obj->base);
2585 mutex_unlock(&dev->struct_mutex);
2586 return false;
2587}
2588
2589/* Update plane->state->fb to match plane->fb after driver-internal updates */
2590static void
2591update_state_fb(struct drm_plane *plane)
2592{
2593 if (plane->fb == plane->state->fb)
2594 return;
2595
2596 if (plane->state->fb)
2597 drm_framebuffer_unreference(plane->state->fb);
2598 plane->state->fb = plane->fb;
2599 if (plane->state->fb)
2600 drm_framebuffer_reference(plane->state->fb);
2601}
2602
2603static void
2604intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2605 struct intel_initial_plane_config *plane_config)
2606{
2607 struct drm_device *dev = intel_crtc->base.dev;
2608 struct drm_i915_private *dev_priv = dev->dev_private;
2609 struct drm_crtc *c;
2610 struct intel_crtc *i;
2611 struct drm_i915_gem_object *obj;
2612 struct drm_plane *primary = intel_crtc->base.primary;
2613 struct drm_framebuffer *fb;
2614
2615 if (!plane_config->fb)
2616 return;
2617
2618 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
2619 fb = &plane_config->fb->base;
2620 goto valid_fb;
2621 }
2622
2623 kfree(plane_config->fb);
2624
2625 /*
2626 * Failed to alloc the obj, check to see if we should share
2627 * an fb with another CRTC instead
2628 */
2629 for_each_crtc(dev, c) {
2630 i = to_intel_crtc(c);
2631
2632 if (c == &intel_crtc->base)
2633 continue;
2634
2635 if (!i->active)
2636 continue;
2637
2638 fb = c->primary->fb;
2639 if (!fb)
2640 continue;
2641
2642 obj = intel_fb_obj(fb);
2643 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
2644 drm_framebuffer_reference(fb);
2645 goto valid_fb;
2646 }
2647 }
2648
2649 return;
2650
2651valid_fb:
2652 obj = intel_fb_obj(fb);
2653 if (obj->tiling_mode != I915_TILING_NONE)
2654 dev_priv->preserve_bios_swizzle = true;
2655
2656 primary->fb = fb;
2657 primary->state->crtc = &intel_crtc->base;
2658 primary->crtc = &intel_crtc->base;
2659 update_state_fb(primary);
2660 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
2661}
2662
2663static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2664 struct drm_framebuffer *fb,
2665 int x, int y)
2666{
2667 struct drm_device *dev = crtc->dev;
2668 struct drm_i915_private *dev_priv = dev->dev_private;
2669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2670 struct drm_i915_gem_object *obj;
2671 int plane = intel_crtc->plane;
2672 unsigned long linear_offset;
2673 u32 dspcntr;
2674 u32 reg = DSPCNTR(plane);
2675 int pixel_size;
2676
2677 if (!intel_crtc->primary_enabled) {
2678 I915_WRITE(reg, 0);
2679 if (INTEL_INFO(dev)->gen >= 4)
2680 I915_WRITE(DSPSURF(plane), 0);
2681 else
2682 I915_WRITE(DSPADDR(plane), 0);
2683 POSTING_READ(reg);
2684 return;
2685 }
2686
2687 obj = intel_fb_obj(fb);
2688 if (WARN_ON(obj == NULL))
2689 return;
2690
2691 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2692
2693 dspcntr = DISPPLANE_GAMMA_ENABLE;
2694
2695 dspcntr |= DISPLAY_PLANE_ENABLE;
2696
2697 if (INTEL_INFO(dev)->gen < 4) {
2698 if (intel_crtc->pipe == PIPE_B)
2699 dspcntr |= DISPPLANE_SEL_PIPE_B;
2700
2701 /* pipesrc and dspsize control the size that is scaled from,
2702 * which should always be the user's requested size.
2703 */
2704 I915_WRITE(DSPSIZE(plane),
2705 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2706 (intel_crtc->config->pipe_src_w - 1));
2707 I915_WRITE(DSPPOS(plane), 0);
2708 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2709 I915_WRITE(PRIMSIZE(plane),
2710 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2711 (intel_crtc->config->pipe_src_w - 1));
2712 I915_WRITE(PRIMPOS(plane), 0);
2713 I915_WRITE(PRIMCNSTALPHA(plane), 0);
2714 }
2715
2716 switch (fb->pixel_format) {
2717 case DRM_FORMAT_C8:
2718 dspcntr |= DISPPLANE_8BPP;
2719 break;
2720 case DRM_FORMAT_XRGB1555:
2721 case DRM_FORMAT_ARGB1555:
2722 dspcntr |= DISPPLANE_BGRX555;
2723 break;
2724 case DRM_FORMAT_RGB565:
2725 dspcntr |= DISPPLANE_BGRX565;
2726 break;
2727 case DRM_FORMAT_XRGB8888:
2728 case DRM_FORMAT_ARGB8888:
2729 dspcntr |= DISPPLANE_BGRX888;
2730 break;
2731 case DRM_FORMAT_XBGR8888:
2732 case DRM_FORMAT_ABGR8888:
2733 dspcntr |= DISPPLANE_RGBX888;
2734 break;
2735 case DRM_FORMAT_XRGB2101010:
2736 case DRM_FORMAT_ARGB2101010:
2737 dspcntr |= DISPPLANE_BGRX101010;
2738 break;
2739 case DRM_FORMAT_XBGR2101010:
2740 case DRM_FORMAT_ABGR2101010:
2741 dspcntr |= DISPPLANE_RGBX101010;
2742 break;
2743 default:
2744 BUG();
2745 }
2746
2747 if (INTEL_INFO(dev)->gen >= 4 &&
2748 obj->tiling_mode != I915_TILING_NONE)
2749 dspcntr |= DISPPLANE_TILED;
2750
2751 if (IS_G4X(dev))
2752 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2753
2754 linear_offset = y * fb->pitches[0] + x * pixel_size;
2755
2756 if (INTEL_INFO(dev)->gen >= 4) {
2757 intel_crtc->dspaddr_offset =
2758 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2759 pixel_size,
2760 fb->pitches[0]);
2761 linear_offset -= intel_crtc->dspaddr_offset;
2762 } else {
2763 intel_crtc->dspaddr_offset = linear_offset;
2764 }
2765
2766 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
2767 dspcntr |= DISPPLANE_ROTATE_180;
2768
2769 x += (intel_crtc->config->pipe_src_w - 1);
2770 y += (intel_crtc->config->pipe_src_h - 1);
2771
2772 /* Finding the last pixel of the last line of the display
2773 data and adding to linear_offset*/
2774 linear_offset +=
2775 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2776 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
2777 }
2778
2779 I915_WRITE(reg, dspcntr);
2780
2781 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2782 if (INTEL_INFO(dev)->gen >= 4) {
2783 I915_WRITE(DSPSURF(plane),
2784 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2785 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2786 I915_WRITE(DSPLINOFF(plane), linear_offset);
2787 } else
2788 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
2789 POSTING_READ(reg);
2790}
2791
2792static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2793 struct drm_framebuffer *fb,
2794 int x, int y)
2795{
2796 struct drm_device *dev = crtc->dev;
2797 struct drm_i915_private *dev_priv = dev->dev_private;
2798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2799 struct drm_i915_gem_object *obj;
2800 int plane = intel_crtc->plane;
2801 unsigned long linear_offset;
2802 u32 dspcntr;
2803 u32 reg = DSPCNTR(plane);
2804 int pixel_size;
2805
2806 if (!intel_crtc->primary_enabled) {
2807 I915_WRITE(reg, 0);
2808 I915_WRITE(DSPSURF(plane), 0);
2809 POSTING_READ(reg);
2810 return;
2811 }
2812
2813 obj = intel_fb_obj(fb);
2814 if (WARN_ON(obj == NULL))
2815 return;
2816
2817 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2818
2819 dspcntr = DISPPLANE_GAMMA_ENABLE;
2820
2821 dspcntr |= DISPLAY_PLANE_ENABLE;
2822
2823 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2824 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2825
2826 switch (fb->pixel_format) {
2827 case DRM_FORMAT_C8:
2828 dspcntr |= DISPPLANE_8BPP;
2829 break;
2830 case DRM_FORMAT_RGB565:
2831 dspcntr |= DISPPLANE_BGRX565;
2832 break;
2833 case DRM_FORMAT_XRGB8888:
2834 case DRM_FORMAT_ARGB8888:
2835 dspcntr |= DISPPLANE_BGRX888;
2836 break;
2837 case DRM_FORMAT_XBGR8888:
2838 case DRM_FORMAT_ABGR8888:
2839 dspcntr |= DISPPLANE_RGBX888;
2840 break;
2841 case DRM_FORMAT_XRGB2101010:
2842 case DRM_FORMAT_ARGB2101010:
2843 dspcntr |= DISPPLANE_BGRX101010;
2844 break;
2845 case DRM_FORMAT_XBGR2101010:
2846 case DRM_FORMAT_ABGR2101010:
2847 dspcntr |= DISPPLANE_RGBX101010;
2848 break;
2849 default:
2850 BUG();
2851 }
2852
2853 if (obj->tiling_mode != I915_TILING_NONE)
2854 dspcntr |= DISPPLANE_TILED;
2855
2856 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
2857 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2858
2859 linear_offset = y * fb->pitches[0] + x * pixel_size;
2860 intel_crtc->dspaddr_offset =
2861 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2862 pixel_size,
2863 fb->pitches[0]);
2864 linear_offset -= intel_crtc->dspaddr_offset;
2865 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
2866 dspcntr |= DISPPLANE_ROTATE_180;
2867
2868 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2869 x += (intel_crtc->config->pipe_src_w - 1);
2870 y += (intel_crtc->config->pipe_src_h - 1);
2871
2872 /* Finding the last pixel of the last line of the display
2873 data and adding to linear_offset*/
2874 linear_offset +=
2875 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2876 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
2877 }
2878 }
2879
2880 I915_WRITE(reg, dspcntr);
2881
2882 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
2883 I915_WRITE(DSPSURF(plane),
2884 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
2885 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2886 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2887 } else {
2888 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2889 I915_WRITE(DSPLINOFF(plane), linear_offset);
2890 }
2891 POSTING_READ(reg);
2892}
2893
2894u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2895 uint32_t pixel_format)
2896{
2897 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2898
2899 /*
2900 * The stride is either expressed as a multiple of 64 bytes
2901 * chunks for linear buffers or in number of tiles for tiled
2902 * buffers.
2903 */
2904 switch (fb_modifier) {
2905 case DRM_FORMAT_MOD_NONE:
2906 return 64;
2907 case I915_FORMAT_MOD_X_TILED:
2908 if (INTEL_INFO(dev)->gen == 2)
2909 return 128;
2910 return 512;
2911 case I915_FORMAT_MOD_Y_TILED:
2912 /* No need to check for old gens and Y tiling since this is
2913 * about the display engine and those will be blocked before
2914 * we get here.
2915 */
2916 return 128;
2917 case I915_FORMAT_MOD_Yf_TILED:
2918 if (bits_per_pixel == 8)
2919 return 64;
2920 else
2921 return 128;
2922 default:
2923 MISSING_CASE(fb_modifier);
2924 return 64;
2925 }
2926}
2927
2928unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2929 struct drm_i915_gem_object *obj)
2930{
2931 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
2932
2933 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
2934 view = &i915_ggtt_view_rotated;
2935
2936 return i915_gem_obj_ggtt_offset_view(obj, view);
2937}
2938
2939static void skylake_update_primary_plane(struct drm_crtc *crtc,
2940 struct drm_framebuffer *fb,
2941 int x, int y)
2942{
2943 struct drm_device *dev = crtc->dev;
2944 struct drm_i915_private *dev_priv = dev->dev_private;
2945 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2946 struct drm_i915_gem_object *obj;
2947 int pipe = intel_crtc->pipe;
2948 u32 plane_ctl, stride_div;
2949 unsigned long surf_addr;
2950
2951 if (!intel_crtc->primary_enabled) {
2952 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2953 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2954 POSTING_READ(PLANE_CTL(pipe, 0));
2955 return;
2956 }
2957
2958 plane_ctl = PLANE_CTL_ENABLE |
2959 PLANE_CTL_PIPE_GAMMA_ENABLE |
2960 PLANE_CTL_PIPE_CSC_ENABLE;
2961
2962 switch (fb->pixel_format) {
2963 case DRM_FORMAT_RGB565:
2964 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2965 break;
2966 case DRM_FORMAT_XRGB8888:
2967 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2968 break;
2969 case DRM_FORMAT_ARGB8888:
2970 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2971 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2972 break;
2973 case DRM_FORMAT_XBGR8888:
2974 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2975 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2976 break;
2977 case DRM_FORMAT_ABGR8888:
2978 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2979 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2980 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2981 break;
2982 case DRM_FORMAT_XRGB2101010:
2983 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2984 break;
2985 case DRM_FORMAT_XBGR2101010:
2986 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2987 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2988 break;
2989 default:
2990 BUG();
2991 }
2992
2993 switch (fb->modifier[0]) {
2994 case DRM_FORMAT_MOD_NONE:
2995 break;
2996 case I915_FORMAT_MOD_X_TILED:
2997 plane_ctl |= PLANE_CTL_TILED_X;
2998 break;
2999 case I915_FORMAT_MOD_Y_TILED:
3000 plane_ctl |= PLANE_CTL_TILED_Y;
3001 break;
3002 case I915_FORMAT_MOD_Yf_TILED:
3003 plane_ctl |= PLANE_CTL_TILED_YF;
3004 break;
3005 default:
3006 MISSING_CASE(fb->modifier[0]);
3007 }
3008
3009 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3010 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
3011 plane_ctl |= PLANE_CTL_ROTATE_180;
3012
3013 obj = intel_fb_obj(fb);
3014 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3015 fb->pixel_format);
3016 surf_addr = intel_plane_obj_offset(to_intel_plane(crtc->primary), obj);
3017
3018 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3019 I915_WRITE(PLANE_POS(pipe, 0), 0);
3020 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
3021 I915_WRITE(PLANE_SIZE(pipe, 0),
3022 (intel_crtc->config->pipe_src_h - 1) << 16 |
3023 (intel_crtc->config->pipe_src_w - 1));
3024 I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
3025 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
3026
3027 POSTING_READ(PLANE_SURF(pipe, 0));
3028}
3029
3030/* Assume fb object is pinned & idle & fenced and just update base pointers */
3031static int
3032intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3033 int x, int y, enum mode_set_atomic state)
3034{
3035 struct drm_device *dev = crtc->dev;
3036 struct drm_i915_private *dev_priv = dev->dev_private;
3037
3038 if (dev_priv->display.disable_fbc)
3039 dev_priv->display.disable_fbc(dev);
3040
3041 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3042
3043 return 0;
3044}
3045
3046static void intel_complete_page_flips(struct drm_device *dev)
3047{
3048 struct drm_crtc *crtc;
3049
3050 for_each_crtc(dev, crtc) {
3051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3052 enum plane plane = intel_crtc->plane;
3053
3054 intel_prepare_page_flip(dev, plane);
3055 intel_finish_page_flip_plane(dev, plane);
3056 }
3057}
3058
3059static void intel_update_primary_planes(struct drm_device *dev)
3060{
3061 struct drm_i915_private *dev_priv = dev->dev_private;
3062 struct drm_crtc *crtc;
3063
3064 for_each_crtc(dev, crtc) {
3065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3066
3067 drm_modeset_lock(&crtc->mutex, NULL);
3068 /*
3069 * FIXME: Once we have proper support for primary planes (and
3070 * disabling them without disabling the entire crtc) allow again
3071 * a NULL crtc->primary->fb.
3072 */
3073 if (intel_crtc->active && crtc->primary->fb)
3074 dev_priv->display.update_primary_plane(crtc,
3075 crtc->primary->fb,
3076 crtc->x,
3077 crtc->y);
3078 drm_modeset_unlock(&crtc->mutex);
3079 }
3080}
3081
3082void intel_prepare_reset(struct drm_device *dev)
3083{
3084 struct drm_i915_private *dev_priv = to_i915(dev);
3085 struct intel_crtc *crtc;
3086
3087 /* no reset support for gen2 */
3088 if (IS_GEN2(dev))
3089 return;
3090
3091 /* reset doesn't touch the display */
3092 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3093 return;
3094
3095 drm_modeset_lock_all(dev);
3096
3097 /*
3098 * Disabling the crtcs gracefully seems nicer. Also the
3099 * g33 docs say we should at least disable all the planes.
3100 */
3101 for_each_intel_crtc(dev, crtc) {
3102 if (crtc->active)
3103 dev_priv->display.crtc_disable(&crtc->base);
3104 }
3105}
3106
3107void intel_finish_reset(struct drm_device *dev)
3108{
3109 struct drm_i915_private *dev_priv = to_i915(dev);
3110
3111 /*
3112 * Flips in the rings will be nuked by the reset,
3113 * so complete all pending flips so that user space
3114 * will get its events and not get stuck.
3115 */
3116 intel_complete_page_flips(dev);
3117
3118 /* no reset support for gen2 */
3119 if (IS_GEN2(dev))
3120 return;
3121
3122 /* reset doesn't touch the display */
3123 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3124 /*
3125 * Flips in the rings have been nuked by the reset,
3126 * so update the base address of all primary
3127 * planes to the the last fb to make sure we're
3128 * showing the correct fb after a reset.
3129 */
3130 intel_update_primary_planes(dev);
3131 return;
3132 }
3133
3134 /*
3135 * The display has been reset as well,
3136 * so need a full re-initialization.
3137 */
3138 intel_runtime_pm_disable_interrupts(dev_priv);
3139 intel_runtime_pm_enable_interrupts(dev_priv);
3140
3141 intel_modeset_init_hw(dev);
3142
3143 spin_lock_irq(&dev_priv->irq_lock);
3144 if (dev_priv->display.hpd_irq_setup)
3145 dev_priv->display.hpd_irq_setup(dev);
3146 spin_unlock_irq(&dev_priv->irq_lock);
3147
3148 intel_modeset_setup_hw_state(dev, true);
3149
3150 intel_hpd_init(dev_priv);
3151
3152 drm_modeset_unlock_all(dev);
3153}
3154
3155static int
3156intel_finish_fb(struct drm_framebuffer *old_fb)
3157{
3158 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
3159 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3160 bool was_interruptible = dev_priv->mm.interruptible;
3161 int ret;
3162
3163 /* Big Hammer, we also need to ensure that any pending
3164 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3165 * current scanout is retired before unpinning the old
3166 * framebuffer.
3167 *
3168 * This should only fail upon a hung GPU, in which case we
3169 * can safely continue.
3170 */
3171 dev_priv->mm.interruptible = false;
3172 ret = i915_gem_object_finish_gpu(obj);
3173 dev_priv->mm.interruptible = was_interruptible;
3174
3175 return ret;
3176}
3177
3178static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3179{
3180 struct drm_device *dev = crtc->dev;
3181 struct drm_i915_private *dev_priv = dev->dev_private;
3182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3183 bool pending;
3184
3185 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3186 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3187 return false;
3188
3189 spin_lock_irq(&dev->event_lock);
3190 pending = to_intel_crtc(crtc)->unpin_work != NULL;
3191 spin_unlock_irq(&dev->event_lock);
3192
3193 return pending;
3194}
3195
3196static void intel_update_pipe_size(struct intel_crtc *crtc)
3197{
3198 struct drm_device *dev = crtc->base.dev;
3199 struct drm_i915_private *dev_priv = dev->dev_private;
3200 const struct drm_display_mode *adjusted_mode;
3201
3202 if (!i915.fastboot)
3203 return;
3204
3205 /*
3206 * Update pipe size and adjust fitter if needed: the reason for this is
3207 * that in compute_mode_changes we check the native mode (not the pfit
3208 * mode) to see if we can flip rather than do a full mode set. In the
3209 * fastboot case, we'll flip, but if we don't update the pipesrc and
3210 * pfit state, we'll end up with a big fb scanned out into the wrong
3211 * sized surface.
3212 *
3213 * To fix this properly, we need to hoist the checks up into
3214 * compute_mode_changes (or above), check the actual pfit state and
3215 * whether the platform allows pfit disable with pipe active, and only
3216 * then update the pipesrc and pfit state, even on the flip path.
3217 */
3218
3219 adjusted_mode = &crtc->config->base.adjusted_mode;
3220
3221 I915_WRITE(PIPESRC(crtc->pipe),
3222 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3223 (adjusted_mode->crtc_vdisplay - 1));
3224 if (!crtc->config->pch_pfit.enabled &&
3225 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3226 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
3227 I915_WRITE(PF_CTL(crtc->pipe), 0);
3228 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3229 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3230 }
3231 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3232 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
3233}
3234
3235static void intel_fdi_normal_train(struct drm_crtc *crtc)
3236{
3237 struct drm_device *dev = crtc->dev;
3238 struct drm_i915_private *dev_priv = dev->dev_private;
3239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3240 int pipe = intel_crtc->pipe;
3241 u32 reg, temp;
3242
3243 /* enable normal train */
3244 reg = FDI_TX_CTL(pipe);
3245 temp = I915_READ(reg);
3246 if (IS_IVYBRIDGE(dev)) {
3247 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3248 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
3249 } else {
3250 temp &= ~FDI_LINK_TRAIN_NONE;
3251 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
3252 }
3253 I915_WRITE(reg, temp);
3254
3255 reg = FDI_RX_CTL(pipe);
3256 temp = I915_READ(reg);
3257 if (HAS_PCH_CPT(dev)) {
3258 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3259 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3260 } else {
3261 temp &= ~FDI_LINK_TRAIN_NONE;
3262 temp |= FDI_LINK_TRAIN_NONE;
3263 }
3264 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3265
3266 /* wait one idle pattern time */
3267 POSTING_READ(reg);
3268 udelay(1000);
3269
3270 /* IVB wants error correction enabled */
3271 if (IS_IVYBRIDGE(dev))
3272 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3273 FDI_FE_ERRC_ENABLE);
3274}
3275
3276/* The FDI link training functions for ILK/Ibexpeak. */
3277static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3278{
3279 struct drm_device *dev = crtc->dev;
3280 struct drm_i915_private *dev_priv = dev->dev_private;
3281 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3282 int pipe = intel_crtc->pipe;
3283 u32 reg, temp, tries;
3284
3285 /* FDI needs bits from pipe first */
3286 assert_pipe_enabled(dev_priv, pipe);
3287
3288 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3289 for train result */
3290 reg = FDI_RX_IMR(pipe);
3291 temp = I915_READ(reg);
3292 temp &= ~FDI_RX_SYMBOL_LOCK;
3293 temp &= ~FDI_RX_BIT_LOCK;
3294 I915_WRITE(reg, temp);
3295 I915_READ(reg);
3296 udelay(150);
3297
3298 /* enable CPU FDI TX and PCH FDI RX */
3299 reg = FDI_TX_CTL(pipe);
3300 temp = I915_READ(reg);
3301 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3302 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3303 temp &= ~FDI_LINK_TRAIN_NONE;
3304 temp |= FDI_LINK_TRAIN_PATTERN_1;
3305 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3306
3307 reg = FDI_RX_CTL(pipe);
3308 temp = I915_READ(reg);
3309 temp &= ~FDI_LINK_TRAIN_NONE;
3310 temp |= FDI_LINK_TRAIN_PATTERN_1;
3311 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3312
3313 POSTING_READ(reg);
3314 udelay(150);
3315
3316 /* Ironlake workaround, enable clock pointer after FDI enable*/
3317 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3318 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3319 FDI_RX_PHASE_SYNC_POINTER_EN);
3320
3321 reg = FDI_RX_IIR(pipe);
3322 for (tries = 0; tries < 5; tries++) {
3323 temp = I915_READ(reg);
3324 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3325
3326 if ((temp & FDI_RX_BIT_LOCK)) {
3327 DRM_DEBUG_KMS("FDI train 1 done.\n");
3328 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3329 break;
3330 }
3331 }
3332 if (tries == 5)
3333 DRM_ERROR("FDI train 1 fail!\n");
3334
3335 /* Train 2 */
3336 reg = FDI_TX_CTL(pipe);
3337 temp = I915_READ(reg);
3338 temp &= ~FDI_LINK_TRAIN_NONE;
3339 temp |= FDI_LINK_TRAIN_PATTERN_2;
3340 I915_WRITE(reg, temp);
3341
3342 reg = FDI_RX_CTL(pipe);
3343 temp = I915_READ(reg);
3344 temp &= ~FDI_LINK_TRAIN_NONE;
3345 temp |= FDI_LINK_TRAIN_PATTERN_2;
3346 I915_WRITE(reg, temp);
3347
3348 POSTING_READ(reg);
3349 udelay(150);
3350
3351 reg = FDI_RX_IIR(pipe);
3352 for (tries = 0; tries < 5; tries++) {
3353 temp = I915_READ(reg);
3354 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3355
3356 if (temp & FDI_RX_SYMBOL_LOCK) {
3357 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3358 DRM_DEBUG_KMS("FDI train 2 done.\n");
3359 break;
3360 }
3361 }
3362 if (tries == 5)
3363 DRM_ERROR("FDI train 2 fail!\n");
3364
3365 DRM_DEBUG_KMS("FDI train done\n");
3366
3367}
3368
3369static const int snb_b_fdi_train_param[] = {
3370 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3371 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3372 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3373 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3374};
3375
3376/* The FDI link training functions for SNB/Cougarpoint. */
3377static void gen6_fdi_link_train(struct drm_crtc *crtc)
3378{
3379 struct drm_device *dev = crtc->dev;
3380 struct drm_i915_private *dev_priv = dev->dev_private;
3381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3382 int pipe = intel_crtc->pipe;
3383 u32 reg, temp, i, retry;
3384
3385 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3386 for train result */
3387 reg = FDI_RX_IMR(pipe);
3388 temp = I915_READ(reg);
3389 temp &= ~FDI_RX_SYMBOL_LOCK;
3390 temp &= ~FDI_RX_BIT_LOCK;
3391 I915_WRITE(reg, temp);
3392
3393 POSTING_READ(reg);
3394 udelay(150);
3395
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 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3404 /* SNB-B */
3405 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3406 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3407
3408 I915_WRITE(FDI_RX_MISC(pipe),
3409 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3410
3411 reg = FDI_RX_CTL(pipe);
3412 temp = I915_READ(reg);
3413 if (HAS_PCH_CPT(dev)) {
3414 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3415 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3416 } else {
3417 temp &= ~FDI_LINK_TRAIN_NONE;
3418 temp |= FDI_LINK_TRAIN_PATTERN_1;
3419 }
3420 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3421
3422 POSTING_READ(reg);
3423 udelay(150);
3424
3425 for (i = 0; i < 4; i++) {
3426 reg = FDI_TX_CTL(pipe);
3427 temp = I915_READ(reg);
3428 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3429 temp |= snb_b_fdi_train_param[i];
3430 I915_WRITE(reg, temp);
3431
3432 POSTING_READ(reg);
3433 udelay(500);
3434
3435 for (retry = 0; retry < 5; retry++) {
3436 reg = FDI_RX_IIR(pipe);
3437 temp = I915_READ(reg);
3438 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3439 if (temp & FDI_RX_BIT_LOCK) {
3440 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3441 DRM_DEBUG_KMS("FDI train 1 done.\n");
3442 break;
3443 }
3444 udelay(50);
3445 }
3446 if (retry < 5)
3447 break;
3448 }
3449 if (i == 4)
3450 DRM_ERROR("FDI train 1 fail!\n");
3451
3452 /* Train 2 */
3453 reg = FDI_TX_CTL(pipe);
3454 temp = I915_READ(reg);
3455 temp &= ~FDI_LINK_TRAIN_NONE;
3456 temp |= FDI_LINK_TRAIN_PATTERN_2;
3457 if (IS_GEN6(dev)) {
3458 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3459 /* SNB-B */
3460 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3461 }
3462 I915_WRITE(reg, temp);
3463
3464 reg = FDI_RX_CTL(pipe);
3465 temp = I915_READ(reg);
3466 if (HAS_PCH_CPT(dev)) {
3467 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3468 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3469 } else {
3470 temp &= ~FDI_LINK_TRAIN_NONE;
3471 temp |= FDI_LINK_TRAIN_PATTERN_2;
3472 }
3473 I915_WRITE(reg, temp);
3474
3475 POSTING_READ(reg);
3476 udelay(150);
3477
3478 for (i = 0; i < 4; i++) {
3479 reg = FDI_TX_CTL(pipe);
3480 temp = I915_READ(reg);
3481 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3482 temp |= snb_b_fdi_train_param[i];
3483 I915_WRITE(reg, temp);
3484
3485 POSTING_READ(reg);
3486 udelay(500);
3487
3488 for (retry = 0; retry < 5; retry++) {
3489 reg = FDI_RX_IIR(pipe);
3490 temp = I915_READ(reg);
3491 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3492 if (temp & FDI_RX_SYMBOL_LOCK) {
3493 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3494 DRM_DEBUG_KMS("FDI train 2 done.\n");
3495 break;
3496 }
3497 udelay(50);
3498 }
3499 if (retry < 5)
3500 break;
3501 }
3502 if (i == 4)
3503 DRM_ERROR("FDI train 2 fail!\n");
3504
3505 DRM_DEBUG_KMS("FDI train done.\n");
3506}
3507
3508/* Manual link training for Ivy Bridge A0 parts */
3509static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3510{
3511 struct drm_device *dev = crtc->dev;
3512 struct drm_i915_private *dev_priv = dev->dev_private;
3513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3514 int pipe = intel_crtc->pipe;
3515 u32 reg, temp, i, j;
3516
3517 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3518 for train result */
3519 reg = FDI_RX_IMR(pipe);
3520 temp = I915_READ(reg);
3521 temp &= ~FDI_RX_SYMBOL_LOCK;
3522 temp &= ~FDI_RX_BIT_LOCK;
3523 I915_WRITE(reg, temp);
3524
3525 POSTING_READ(reg);
3526 udelay(150);
3527
3528 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3529 I915_READ(FDI_RX_IIR(pipe)));
3530
3531 /* Try each vswing and preemphasis setting twice before moving on */
3532 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3533 /* disable first in case we need to retry */
3534 reg = FDI_TX_CTL(pipe);
3535 temp = I915_READ(reg);
3536 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3537 temp &= ~FDI_TX_ENABLE;
3538 I915_WRITE(reg, temp);
3539
3540 reg = FDI_RX_CTL(pipe);
3541 temp = I915_READ(reg);
3542 temp &= ~FDI_LINK_TRAIN_AUTO;
3543 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3544 temp &= ~FDI_RX_ENABLE;
3545 I915_WRITE(reg, temp);
3546
3547 /* enable CPU FDI TX and PCH FDI RX */
3548 reg = FDI_TX_CTL(pipe);
3549 temp = I915_READ(reg);
3550 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3551 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3552 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
3553 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3554 temp |= snb_b_fdi_train_param[j/2];
3555 temp |= FDI_COMPOSITE_SYNC;
3556 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3557
3558 I915_WRITE(FDI_RX_MISC(pipe),
3559 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3560
3561 reg = FDI_RX_CTL(pipe);
3562 temp = I915_READ(reg);
3563 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3564 temp |= FDI_COMPOSITE_SYNC;
3565 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3566
3567 POSTING_READ(reg);
3568 udelay(1); /* should be 0.5us */
3569
3570 for (i = 0; i < 4; i++) {
3571 reg = FDI_RX_IIR(pipe);
3572 temp = I915_READ(reg);
3573 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3574
3575 if (temp & FDI_RX_BIT_LOCK ||
3576 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3577 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3578 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3579 i);
3580 break;
3581 }
3582 udelay(1); /* should be 0.5us */
3583 }
3584 if (i == 4) {
3585 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3586 continue;
3587 }
3588
3589 /* Train 2 */
3590 reg = FDI_TX_CTL(pipe);
3591 temp = I915_READ(reg);
3592 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3593 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3594 I915_WRITE(reg, temp);
3595
3596 reg = FDI_RX_CTL(pipe);
3597 temp = I915_READ(reg);
3598 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3599 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3600 I915_WRITE(reg, temp);
3601
3602 POSTING_READ(reg);
3603 udelay(2); /* should be 1.5us */
3604
3605 for (i = 0; i < 4; i++) {
3606 reg = FDI_RX_IIR(pipe);
3607 temp = I915_READ(reg);
3608 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3609
3610 if (temp & FDI_RX_SYMBOL_LOCK ||
3611 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3612 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3613 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3614 i);
3615 goto train_done;
3616 }
3617 udelay(2); /* should be 1.5us */
3618 }
3619 if (i == 4)
3620 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
3621 }
3622
3623train_done:
3624 DRM_DEBUG_KMS("FDI train done.\n");
3625}
3626
3627static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
3628{
3629 struct drm_device *dev = intel_crtc->base.dev;
3630 struct drm_i915_private *dev_priv = dev->dev_private;
3631 int pipe = intel_crtc->pipe;
3632 u32 reg, temp;
3633
3634
3635 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
3636 reg = FDI_RX_CTL(pipe);
3637 temp = I915_READ(reg);
3638 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3639 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
3640 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3641 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3642
3643 POSTING_READ(reg);
3644 udelay(200);
3645
3646 /* Switch from Rawclk to PCDclk */
3647 temp = I915_READ(reg);
3648 I915_WRITE(reg, temp | FDI_PCDCLK);
3649
3650 POSTING_READ(reg);
3651 udelay(200);
3652
3653 /* Enable CPU FDI TX PLL, always on for Ironlake */
3654 reg = FDI_TX_CTL(pipe);
3655 temp = I915_READ(reg);
3656 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3657 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
3658
3659 POSTING_READ(reg);
3660 udelay(100);
3661 }
3662}
3663
3664static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3665{
3666 struct drm_device *dev = intel_crtc->base.dev;
3667 struct drm_i915_private *dev_priv = dev->dev_private;
3668 int pipe = intel_crtc->pipe;
3669 u32 reg, temp;
3670
3671 /* Switch from PCDclk to Rawclk */
3672 reg = FDI_RX_CTL(pipe);
3673 temp = I915_READ(reg);
3674 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3675
3676 /* Disable CPU FDI TX PLL */
3677 reg = FDI_TX_CTL(pipe);
3678 temp = I915_READ(reg);
3679 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3680
3681 POSTING_READ(reg);
3682 udelay(100);
3683
3684 reg = FDI_RX_CTL(pipe);
3685 temp = I915_READ(reg);
3686 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3687
3688 /* Wait for the clocks to turn off. */
3689 POSTING_READ(reg);
3690 udelay(100);
3691}
3692
3693static void ironlake_fdi_disable(struct drm_crtc *crtc)
3694{
3695 struct drm_device *dev = crtc->dev;
3696 struct drm_i915_private *dev_priv = dev->dev_private;
3697 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3698 int pipe = intel_crtc->pipe;
3699 u32 reg, temp;
3700
3701 /* disable CPU FDI tx and PCH FDI rx */
3702 reg = FDI_TX_CTL(pipe);
3703 temp = I915_READ(reg);
3704 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3705 POSTING_READ(reg);
3706
3707 reg = FDI_RX_CTL(pipe);
3708 temp = I915_READ(reg);
3709 temp &= ~(0x7 << 16);
3710 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3711 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3712
3713 POSTING_READ(reg);
3714 udelay(100);
3715
3716 /* Ironlake workaround, disable clock pointer after downing FDI */
3717 if (HAS_PCH_IBX(dev))
3718 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3719
3720 /* still set train pattern 1 */
3721 reg = FDI_TX_CTL(pipe);
3722 temp = I915_READ(reg);
3723 temp &= ~FDI_LINK_TRAIN_NONE;
3724 temp |= FDI_LINK_TRAIN_PATTERN_1;
3725 I915_WRITE(reg, temp);
3726
3727 reg = FDI_RX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 if (HAS_PCH_CPT(dev)) {
3730 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3731 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3732 } else {
3733 temp &= ~FDI_LINK_TRAIN_NONE;
3734 temp |= FDI_LINK_TRAIN_PATTERN_1;
3735 }
3736 /* BPC in FDI rx is consistent with that in PIPECONF */
3737 temp &= ~(0x07 << 16);
3738 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
3739 I915_WRITE(reg, temp);
3740
3741 POSTING_READ(reg);
3742 udelay(100);
3743}
3744
3745bool intel_has_pending_fb_unpin(struct drm_device *dev)
3746{
3747 struct intel_crtc *crtc;
3748
3749 /* Note that we don't need to be called with mode_config.lock here
3750 * as our list of CRTC objects is static for the lifetime of the
3751 * device and so cannot disappear as we iterate. Similarly, we can
3752 * happily treat the predicates as racy, atomic checks as userspace
3753 * cannot claim and pin a new fb without at least acquring the
3754 * struct_mutex and so serialising with us.
3755 */
3756 for_each_intel_crtc(dev, crtc) {
3757 if (atomic_read(&crtc->unpin_work_count) == 0)
3758 continue;
3759
3760 if (crtc->unpin_work)
3761 intel_wait_for_vblank(dev, crtc->pipe);
3762
3763 return true;
3764 }
3765
3766 return false;
3767}
3768
3769static void page_flip_completed(struct intel_crtc *intel_crtc)
3770{
3771 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3772 struct intel_unpin_work *work = intel_crtc->unpin_work;
3773
3774 /* ensure that the unpin work is consistent wrt ->pending. */
3775 smp_rmb();
3776 intel_crtc->unpin_work = NULL;
3777
3778 if (work->event)
3779 drm_send_vblank_event(intel_crtc->base.dev,
3780 intel_crtc->pipe,
3781 work->event);
3782
3783 drm_crtc_vblank_put(&intel_crtc->base);
3784
3785 wake_up_all(&dev_priv->pending_flip_queue);
3786 queue_work(dev_priv->wq, &work->work);
3787
3788 trace_i915_flip_complete(intel_crtc->plane,
3789 work->pending_flip_obj);
3790}
3791
3792void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3793{
3794 struct drm_device *dev = crtc->dev;
3795 struct drm_i915_private *dev_priv = dev->dev_private;
3796
3797 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3798 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3799 !intel_crtc_has_pending_flip(crtc),
3800 60*HZ) == 0)) {
3801 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3802
3803 spin_lock_irq(&dev->event_lock);
3804 if (intel_crtc->unpin_work) {
3805 WARN_ONCE(1, "Removing stuck page flip\n");
3806 page_flip_completed(intel_crtc);
3807 }
3808 spin_unlock_irq(&dev->event_lock);
3809 }
3810
3811 if (crtc->primary->fb) {
3812 mutex_lock(&dev->struct_mutex);
3813 intel_finish_fb(crtc->primary->fb);
3814 mutex_unlock(&dev->struct_mutex);
3815 }
3816}
3817
3818/* Program iCLKIP clock to the desired frequency */
3819static void lpt_program_iclkip(struct drm_crtc *crtc)
3820{
3821 struct drm_device *dev = crtc->dev;
3822 struct drm_i915_private *dev_priv = dev->dev_private;
3823 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
3824 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3825 u32 temp;
3826
3827 mutex_lock(&dev_priv->dpio_lock);
3828
3829 /* It is necessary to ungate the pixclk gate prior to programming
3830 * the divisors, and gate it back when it is done.
3831 */
3832 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3833
3834 /* Disable SSCCTL */
3835 intel_sbi_write(dev_priv, SBI_SSCCTL6,
3836 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3837 SBI_SSCCTL_DISABLE,
3838 SBI_ICLK);
3839
3840 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
3841 if (clock == 20000) {
3842 auxdiv = 1;
3843 divsel = 0x41;
3844 phaseinc = 0x20;
3845 } else {
3846 /* The iCLK virtual clock root frequency is in MHz,
3847 * but the adjusted_mode->crtc_clock in in KHz. To get the
3848 * divisors, it is necessary to divide one by another, so we
3849 * convert the virtual clock precision to KHz here for higher
3850 * precision.
3851 */
3852 u32 iclk_virtual_root_freq = 172800 * 1000;
3853 u32 iclk_pi_range = 64;
3854 u32 desired_divisor, msb_divisor_value, pi_value;
3855
3856 desired_divisor = (iclk_virtual_root_freq / clock);
3857 msb_divisor_value = desired_divisor / iclk_pi_range;
3858 pi_value = desired_divisor % iclk_pi_range;
3859
3860 auxdiv = 0;
3861 divsel = msb_divisor_value - 2;
3862 phaseinc = pi_value;
3863 }
3864
3865 /* This should not happen with any sane values */
3866 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3867 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3868 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3869 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3870
3871 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
3872 clock,
3873 auxdiv,
3874 divsel,
3875 phasedir,
3876 phaseinc);
3877
3878 /* Program SSCDIVINTPHASE6 */
3879 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
3880 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3881 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3882 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3883 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3884 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3885 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
3886 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
3887
3888 /* Program SSCAUXDIV */
3889 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
3890 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3891 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
3892 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
3893
3894 /* Enable modulator and associated divider */
3895 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
3896 temp &= ~SBI_SSCCTL_DISABLE;
3897 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
3898
3899 /* Wait for initialization time */
3900 udelay(24);
3901
3902 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
3903
3904 mutex_unlock(&dev_priv->dpio_lock);
3905}
3906
3907static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3908 enum pipe pch_transcoder)
3909{
3910 struct drm_device *dev = crtc->base.dev;
3911 struct drm_i915_private *dev_priv = dev->dev_private;
3912 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
3913
3914 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3915 I915_READ(HTOTAL(cpu_transcoder)));
3916 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3917 I915_READ(HBLANK(cpu_transcoder)));
3918 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3919 I915_READ(HSYNC(cpu_transcoder)));
3920
3921 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3922 I915_READ(VTOTAL(cpu_transcoder)));
3923 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3924 I915_READ(VBLANK(cpu_transcoder)));
3925 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3926 I915_READ(VSYNC(cpu_transcoder)));
3927 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3928 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3929}
3930
3931static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
3932{
3933 struct drm_i915_private *dev_priv = dev->dev_private;
3934 uint32_t temp;
3935
3936 temp = I915_READ(SOUTH_CHICKEN1);
3937 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
3938 return;
3939
3940 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3941 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3942
3943 temp &= ~FDI_BC_BIFURCATION_SELECT;
3944 if (enable)
3945 temp |= FDI_BC_BIFURCATION_SELECT;
3946
3947 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
3948 I915_WRITE(SOUTH_CHICKEN1, temp);
3949 POSTING_READ(SOUTH_CHICKEN1);
3950}
3951
3952static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3953{
3954 struct drm_device *dev = intel_crtc->base.dev;
3955
3956 switch (intel_crtc->pipe) {
3957 case PIPE_A:
3958 break;
3959 case PIPE_B:
3960 if (intel_crtc->config->fdi_lanes > 2)
3961 cpt_set_fdi_bc_bifurcation(dev, false);
3962 else
3963 cpt_set_fdi_bc_bifurcation(dev, true);
3964
3965 break;
3966 case PIPE_C:
3967 cpt_set_fdi_bc_bifurcation(dev, true);
3968
3969 break;
3970 default:
3971 BUG();
3972 }
3973}
3974
3975/*
3976 * Enable PCH resources required for PCH ports:
3977 * - PCH PLLs
3978 * - FDI training & RX/TX
3979 * - update transcoder timings
3980 * - DP transcoding bits
3981 * - transcoder
3982 */
3983static void ironlake_pch_enable(struct drm_crtc *crtc)
3984{
3985 struct drm_device *dev = crtc->dev;
3986 struct drm_i915_private *dev_priv = dev->dev_private;
3987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3988 int pipe = intel_crtc->pipe;
3989 u32 reg, temp;
3990
3991 assert_pch_transcoder_disabled(dev_priv, pipe);
3992
3993 if (IS_IVYBRIDGE(dev))
3994 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3995
3996 /* Write the TU size bits before fdi link training, so that error
3997 * detection works. */
3998 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3999 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4000
4001 /* For PCH output, training FDI link */
4002 dev_priv->display.fdi_link_train(crtc);
4003
4004 /* We need to program the right clock selection before writing the pixel
4005 * mutliplier into the DPLL. */
4006 if (HAS_PCH_CPT(dev)) {
4007 u32 sel;
4008
4009 temp = I915_READ(PCH_DPLL_SEL);
4010 temp |= TRANS_DPLL_ENABLE(pipe);
4011 sel = TRANS_DPLLB_SEL(pipe);
4012 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
4013 temp |= sel;
4014 else
4015 temp &= ~sel;
4016 I915_WRITE(PCH_DPLL_SEL, temp);
4017 }
4018
4019 /* XXX: pch pll's can be enabled any time before we enable the PCH
4020 * transcoder, and we actually should do this to not upset any PCH
4021 * transcoder that already use the clock when we share it.
4022 *
4023 * Note that enable_shared_dpll tries to do the right thing, but
4024 * get_shared_dpll unconditionally resets the pll - we need that to have
4025 * the right LVDS enable sequence. */
4026 intel_enable_shared_dpll(intel_crtc);
4027
4028 /* set transcoder timing, panel must allow it */
4029 assert_panel_unlocked(dev_priv, pipe);
4030 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
4031
4032 intel_fdi_normal_train(crtc);
4033
4034 /* For PCH DP, enable TRANS_DP_CTL */
4035 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
4036 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
4037 reg = TRANS_DP_CTL(pipe);
4038 temp = I915_READ(reg);
4039 temp &= ~(TRANS_DP_PORT_SEL_MASK |
4040 TRANS_DP_SYNC_MASK |
4041 TRANS_DP_BPC_MASK);
4042 temp |= (TRANS_DP_OUTPUT_ENABLE |
4043 TRANS_DP_ENH_FRAMING);
4044 temp |= bpc << 9; /* same format but at 11:9 */
4045
4046 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
4047 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
4048 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
4049 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
4050
4051 switch (intel_trans_dp_port_sel(crtc)) {
4052 case PCH_DP_B:
4053 temp |= TRANS_DP_PORT_SEL_B;
4054 break;
4055 case PCH_DP_C:
4056 temp |= TRANS_DP_PORT_SEL_C;
4057 break;
4058 case PCH_DP_D:
4059 temp |= TRANS_DP_PORT_SEL_D;
4060 break;
4061 default:
4062 BUG();
4063 }
4064
4065 I915_WRITE(reg, temp);
4066 }
4067
4068 ironlake_enable_pch_transcoder(dev_priv, pipe);
4069}
4070
4071static void lpt_pch_enable(struct drm_crtc *crtc)
4072{
4073 struct drm_device *dev = crtc->dev;
4074 struct drm_i915_private *dev_priv = dev->dev_private;
4075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4076 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
4077
4078 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
4079
4080 lpt_program_iclkip(crtc);
4081
4082 /* Set transcoder timing. */
4083 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
4084
4085 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
4086}
4087
4088void intel_put_shared_dpll(struct intel_crtc *crtc)
4089{
4090 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4091
4092 if (pll == NULL)
4093 return;
4094
4095 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
4096 WARN(1, "bad %s crtc mask\n", pll->name);
4097 return;
4098 }
4099
4100 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4101 if (pll->config.crtc_mask == 0) {
4102 WARN_ON(pll->on);
4103 WARN_ON(pll->active);
4104 }
4105
4106 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
4107}
4108
4109struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4110 struct intel_crtc_state *crtc_state)
4111{
4112 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
4113 struct intel_shared_dpll *pll;
4114 enum intel_dpll_id i;
4115
4116 if (HAS_PCH_IBX(dev_priv->dev)) {
4117 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
4118 i = (enum intel_dpll_id) crtc->pipe;
4119 pll = &dev_priv->shared_dplls[i];
4120
4121 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4122 crtc->base.base.id, pll->name);
4123
4124 WARN_ON(pll->new_config->crtc_mask);
4125
4126 goto found;
4127 }
4128
4129 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4130 pll = &dev_priv->shared_dplls[i];
4131
4132 /* Only want to check enabled timings first */
4133 if (pll->new_config->crtc_mask == 0)
4134 continue;
4135
4136 if (memcmp(&crtc_state->dpll_hw_state,
4137 &pll->new_config->hw_state,
4138 sizeof(pll->new_config->hw_state)) == 0) {
4139 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
4140 crtc->base.base.id, pll->name,
4141 pll->new_config->crtc_mask,
4142 pll->active);
4143 goto found;
4144 }
4145 }
4146
4147 /* Ok no matching timings, maybe there's a free one? */
4148 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4149 pll = &dev_priv->shared_dplls[i];
4150 if (pll->new_config->crtc_mask == 0) {
4151 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4152 crtc->base.base.id, pll->name);
4153 goto found;
4154 }
4155 }
4156
4157 return NULL;
4158
4159found:
4160 if (pll->new_config->crtc_mask == 0)
4161 pll->new_config->hw_state = crtc_state->dpll_hw_state;
4162
4163 crtc_state->shared_dpll = i;
4164 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4165 pipe_name(crtc->pipe));
4166
4167 pll->new_config->crtc_mask |= 1 << crtc->pipe;
4168
4169 return pll;
4170}
4171
4172/**
4173 * intel_shared_dpll_start_config - start a new PLL staged config
4174 * @dev_priv: DRM device
4175 * @clear_pipes: mask of pipes that will have their PLLs freed
4176 *
4177 * Starts a new PLL staged config, copying the current config but
4178 * releasing the references of pipes specified in clear_pipes.
4179 */
4180static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4181 unsigned clear_pipes)
4182{
4183 struct intel_shared_dpll *pll;
4184 enum intel_dpll_id i;
4185
4186 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4187 pll = &dev_priv->shared_dplls[i];
4188
4189 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4190 GFP_KERNEL);
4191 if (!pll->new_config)
4192 goto cleanup;
4193
4194 pll->new_config->crtc_mask &= ~clear_pipes;
4195 }
4196
4197 return 0;
4198
4199cleanup:
4200 while (--i >= 0) {
4201 pll = &dev_priv->shared_dplls[i];
4202 kfree(pll->new_config);
4203 pll->new_config = NULL;
4204 }
4205
4206 return -ENOMEM;
4207}
4208
4209static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4210{
4211 struct intel_shared_dpll *pll;
4212 enum intel_dpll_id i;
4213
4214 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4215 pll = &dev_priv->shared_dplls[i];
4216
4217 WARN_ON(pll->new_config == &pll->config);
4218
4219 pll->config = *pll->new_config;
4220 kfree(pll->new_config);
4221 pll->new_config = NULL;
4222 }
4223}
4224
4225static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4226{
4227 struct intel_shared_dpll *pll;
4228 enum intel_dpll_id i;
4229
4230 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4231 pll = &dev_priv->shared_dplls[i];
4232
4233 WARN_ON(pll->new_config == &pll->config);
4234
4235 kfree(pll->new_config);
4236 pll->new_config = NULL;
4237 }
4238}
4239
4240static void cpt_verify_modeset(struct drm_device *dev, int pipe)
4241{
4242 struct drm_i915_private *dev_priv = dev->dev_private;
4243 int dslreg = PIPEDSL(pipe);
4244 u32 temp;
4245
4246 temp = I915_READ(dslreg);
4247 udelay(500);
4248 if (wait_for(I915_READ(dslreg) != temp, 5)) {
4249 if (wait_for(I915_READ(dslreg) != temp, 5))
4250 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
4251 }
4252}
4253
4254static void skylake_pfit_enable(struct intel_crtc *crtc)
4255{
4256 struct drm_device *dev = crtc->base.dev;
4257 struct drm_i915_private *dev_priv = dev->dev_private;
4258 int pipe = crtc->pipe;
4259
4260 if (crtc->config->pch_pfit.enabled) {
4261 I915_WRITE(PS_CTL(pipe), PS_ENABLE);
4262 I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4263 I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
4264 }
4265}
4266
4267static void ironlake_pfit_enable(struct intel_crtc *crtc)
4268{
4269 struct drm_device *dev = crtc->base.dev;
4270 struct drm_i915_private *dev_priv = dev->dev_private;
4271 int pipe = crtc->pipe;
4272
4273 if (crtc->config->pch_pfit.enabled) {
4274 /* Force use of hard-coded filter coefficients
4275 * as some pre-programmed values are broken,
4276 * e.g. x201.
4277 */
4278 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4279 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4280 PF_PIPE_SEL_IVB(pipe));
4281 else
4282 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
4283 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4284 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
4285 }
4286}
4287
4288static void intel_enable_sprite_planes(struct drm_crtc *crtc)
4289{
4290 struct drm_device *dev = crtc->dev;
4291 enum pipe pipe = to_intel_crtc(crtc)->pipe;
4292 struct drm_plane *plane;
4293 struct intel_plane *intel_plane;
4294
4295 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4296 intel_plane = to_intel_plane(plane);
4297 if (intel_plane->pipe == pipe)
4298 intel_plane_restore(&intel_plane->base);
4299 }
4300}
4301
4302/*
4303 * Disable a plane internally without actually modifying the plane's state.
4304 * This will allow us to easily restore the plane later by just reprogramming
4305 * its state.
4306 */
4307static void disable_plane_internal(struct drm_plane *plane)
4308{
4309 struct intel_plane *intel_plane = to_intel_plane(plane);
4310 struct drm_plane_state *state =
4311 plane->funcs->atomic_duplicate_state(plane);
4312 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4313
4314 intel_state->visible = false;
4315 intel_plane->commit_plane(plane, intel_state);
4316
4317 intel_plane_destroy_state(plane, state);
4318}
4319
4320static void intel_disable_sprite_planes(struct drm_crtc *crtc)
4321{
4322 struct drm_device *dev = crtc->dev;
4323 enum pipe pipe = to_intel_crtc(crtc)->pipe;
4324 struct drm_plane *plane;
4325 struct intel_plane *intel_plane;
4326
4327 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4328 intel_plane = to_intel_plane(plane);
4329 if (plane->fb && intel_plane->pipe == pipe)
4330 disable_plane_internal(plane);
4331 }
4332}
4333
4334void hsw_enable_ips(struct intel_crtc *crtc)
4335{
4336 struct drm_device *dev = crtc->base.dev;
4337 struct drm_i915_private *dev_priv = dev->dev_private;
4338
4339 if (!crtc->config->ips_enabled)
4340 return;
4341
4342 /* We can only enable IPS after we enable a plane and wait for a vblank */
4343 intel_wait_for_vblank(dev, crtc->pipe);
4344
4345 assert_plane_enabled(dev_priv, crtc->plane);
4346 if (IS_BROADWELL(dev)) {
4347 mutex_lock(&dev_priv->rps.hw_lock);
4348 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4349 mutex_unlock(&dev_priv->rps.hw_lock);
4350 /* Quoting Art Runyan: "its not safe to expect any particular
4351 * value in IPS_CTL bit 31 after enabling IPS through the
4352 * mailbox." Moreover, the mailbox may return a bogus state,
4353 * so we need to just enable it and continue on.
4354 */
4355 } else {
4356 I915_WRITE(IPS_CTL, IPS_ENABLE);
4357 /* The bit only becomes 1 in the next vblank, so this wait here
4358 * is essentially intel_wait_for_vblank. If we don't have this
4359 * and don't wait for vblanks until the end of crtc_enable, then
4360 * the HW state readout code will complain that the expected
4361 * IPS_CTL value is not the one we read. */
4362 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4363 DRM_ERROR("Timed out waiting for IPS enable\n");
4364 }
4365}
4366
4367void hsw_disable_ips(struct intel_crtc *crtc)
4368{
4369 struct drm_device *dev = crtc->base.dev;
4370 struct drm_i915_private *dev_priv = dev->dev_private;
4371
4372 if (!crtc->config->ips_enabled)
4373 return;
4374
4375 assert_plane_enabled(dev_priv, crtc->plane);
4376 if (IS_BROADWELL(dev)) {
4377 mutex_lock(&dev_priv->rps.hw_lock);
4378 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4379 mutex_unlock(&dev_priv->rps.hw_lock);
4380 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4381 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4382 DRM_ERROR("Timed out waiting for IPS disable\n");
4383 } else {
4384 I915_WRITE(IPS_CTL, 0);
4385 POSTING_READ(IPS_CTL);
4386 }
4387
4388 /* We need to wait for a vblank before we can disable the plane. */
4389 intel_wait_for_vblank(dev, crtc->pipe);
4390}
4391
4392/** Loads the palette/gamma unit for the CRTC with the prepared values */
4393static void intel_crtc_load_lut(struct drm_crtc *crtc)
4394{
4395 struct drm_device *dev = crtc->dev;
4396 struct drm_i915_private *dev_priv = dev->dev_private;
4397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4398 enum pipe pipe = intel_crtc->pipe;
4399 int palreg = PALETTE(pipe);
4400 int i;
4401 bool reenable_ips = false;
4402
4403 /* The clocks have to be on to load the palette. */
4404 if (!crtc->state->enable || !intel_crtc->active)
4405 return;
4406
4407 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
4408 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
4409 assert_dsi_pll_enabled(dev_priv);
4410 else
4411 assert_pll_enabled(dev_priv, pipe);
4412 }
4413
4414 /* use legacy palette for Ironlake */
4415 if (!HAS_GMCH_DISPLAY(dev))
4416 palreg = LGC_PALETTE(pipe);
4417
4418 /* Workaround : Do not read or write the pipe palette/gamma data while
4419 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4420 */
4421 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
4422 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4423 GAMMA_MODE_MODE_SPLIT)) {
4424 hsw_disable_ips(intel_crtc);
4425 reenable_ips = true;
4426 }
4427
4428 for (i = 0; i < 256; i++) {
4429 I915_WRITE(palreg + 4 * i,
4430 (intel_crtc->lut_r[i] << 16) |
4431 (intel_crtc->lut_g[i] << 8) |
4432 intel_crtc->lut_b[i]);
4433 }
4434
4435 if (reenable_ips)
4436 hsw_enable_ips(intel_crtc);
4437}
4438
4439static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4440{
4441 if (!enable && intel_crtc->overlay) {
4442 struct drm_device *dev = intel_crtc->base.dev;
4443 struct drm_i915_private *dev_priv = dev->dev_private;
4444
4445 mutex_lock(&dev->struct_mutex);
4446 dev_priv->mm.interruptible = false;
4447 (void) intel_overlay_switch_off(intel_crtc->overlay);
4448 dev_priv->mm.interruptible = true;
4449 mutex_unlock(&dev->struct_mutex);
4450 }
4451
4452 /* Let userspace switch the overlay on again. In most cases userspace
4453 * has to recompute where to put it anyway.
4454 */
4455}
4456
4457static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4458{
4459 struct drm_device *dev = crtc->dev;
4460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4461 int pipe = intel_crtc->pipe;
4462
4463 intel_enable_primary_hw_plane(crtc->primary, crtc);
4464 intel_enable_sprite_planes(crtc);
4465 intel_crtc_update_cursor(crtc, true);
4466 intel_crtc_dpms_overlay(intel_crtc, true);
4467
4468 hsw_enable_ips(intel_crtc);
4469
4470 mutex_lock(&dev->struct_mutex);
4471 intel_fbc_update(dev);
4472 mutex_unlock(&dev->struct_mutex);
4473
4474 /*
4475 * FIXME: Once we grow proper nuclear flip support out of this we need
4476 * to compute the mask of flip planes precisely. For the time being
4477 * consider this a flip from a NULL plane.
4478 */
4479 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4480}
4481
4482static void intel_crtc_disable_planes(struct drm_crtc *crtc)
4483{
4484 struct drm_device *dev = crtc->dev;
4485 struct drm_i915_private *dev_priv = dev->dev_private;
4486 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4487 int pipe = intel_crtc->pipe;
4488
4489 intel_crtc_wait_for_pending_flips(crtc);
4490
4491 if (dev_priv->fbc.crtc == intel_crtc)
4492 intel_fbc_disable(dev);
4493
4494 hsw_disable_ips(intel_crtc);
4495
4496 intel_crtc_dpms_overlay(intel_crtc, false);
4497 intel_crtc_update_cursor(crtc, false);
4498 intel_disable_sprite_planes(crtc);
4499 intel_disable_primary_hw_plane(crtc->primary, crtc);
4500
4501 /*
4502 * FIXME: Once we grow proper nuclear flip support out of this we need
4503 * to compute the mask of flip planes precisely. For the time being
4504 * consider this a flip to a NULL plane.
4505 */
4506 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
4507}
4508
4509static void ironlake_crtc_enable(struct drm_crtc *crtc)
4510{
4511 struct drm_device *dev = crtc->dev;
4512 struct drm_i915_private *dev_priv = dev->dev_private;
4513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4514 struct intel_encoder *encoder;
4515 int pipe = intel_crtc->pipe;
4516
4517 WARN_ON(!crtc->state->enable);
4518
4519 if (intel_crtc->active)
4520 return;
4521
4522 if (intel_crtc->config->has_pch_encoder)
4523 intel_prepare_shared_dpll(intel_crtc);
4524
4525 if (intel_crtc->config->has_dp_encoder)
4526 intel_dp_set_m_n(intel_crtc, M1_N1);
4527
4528 intel_set_pipe_timings(intel_crtc);
4529
4530 if (intel_crtc->config->has_pch_encoder) {
4531 intel_cpu_transcoder_set_m_n(intel_crtc,
4532 &intel_crtc->config->fdi_m_n, NULL);
4533 }
4534
4535 ironlake_set_pipeconf(crtc);
4536
4537 intel_crtc->active = true;
4538
4539 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4540 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
4541
4542 for_each_encoder_on_crtc(dev, crtc, encoder)
4543 if (encoder->pre_enable)
4544 encoder->pre_enable(encoder);
4545
4546 if (intel_crtc->config->has_pch_encoder) {
4547 /* Note: FDI PLL enabling _must_ be done before we enable the
4548 * cpu pipes, hence this is separate from all the other fdi/pch
4549 * enabling. */
4550 ironlake_fdi_pll_enable(intel_crtc);
4551 } else {
4552 assert_fdi_tx_disabled(dev_priv, pipe);
4553 assert_fdi_rx_disabled(dev_priv, pipe);
4554 }
4555
4556 ironlake_pfit_enable(intel_crtc);
4557
4558 /*
4559 * On ILK+ LUT must be loaded before the pipe is running but with
4560 * clocks enabled
4561 */
4562 intel_crtc_load_lut(crtc);
4563
4564 intel_update_watermarks(crtc);
4565 intel_enable_pipe(intel_crtc);
4566
4567 if (intel_crtc->config->has_pch_encoder)
4568 ironlake_pch_enable(crtc);
4569
4570 assert_vblank_disabled(crtc);
4571 drm_crtc_vblank_on(crtc);
4572
4573 for_each_encoder_on_crtc(dev, crtc, encoder)
4574 encoder->enable(encoder);
4575
4576 if (HAS_PCH_CPT(dev))
4577 cpt_verify_modeset(dev, intel_crtc->pipe);
4578
4579 intel_crtc_enable_planes(crtc);
4580}
4581
4582/* IPS only exists on ULT machines and is tied to pipe A. */
4583static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4584{
4585 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
4586}
4587
4588/*
4589 * This implements the workaround described in the "notes" section of the mode
4590 * set sequence documentation. When going from no pipes or single pipe to
4591 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4592 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4593 */
4594static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4595{
4596 struct drm_device *dev = crtc->base.dev;
4597 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4598
4599 /* We want to get the other_active_crtc only if there's only 1 other
4600 * active crtc. */
4601 for_each_intel_crtc(dev, crtc_it) {
4602 if (!crtc_it->active || crtc_it == crtc)
4603 continue;
4604
4605 if (other_active_crtc)
4606 return;
4607
4608 other_active_crtc = crtc_it;
4609 }
4610 if (!other_active_crtc)
4611 return;
4612
4613 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4614 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4615}
4616
4617static void haswell_crtc_enable(struct drm_crtc *crtc)
4618{
4619 struct drm_device *dev = crtc->dev;
4620 struct drm_i915_private *dev_priv = dev->dev_private;
4621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4622 struct intel_encoder *encoder;
4623 int pipe = intel_crtc->pipe;
4624
4625 WARN_ON(!crtc->state->enable);
4626
4627 if (intel_crtc->active)
4628 return;
4629
4630 if (intel_crtc_to_shared_dpll(intel_crtc))
4631 intel_enable_shared_dpll(intel_crtc);
4632
4633 if (intel_crtc->config->has_dp_encoder)
4634 intel_dp_set_m_n(intel_crtc, M1_N1);
4635
4636 intel_set_pipe_timings(intel_crtc);
4637
4638 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4639 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4640 intel_crtc->config->pixel_multiplier - 1);
4641 }
4642
4643 if (intel_crtc->config->has_pch_encoder) {
4644 intel_cpu_transcoder_set_m_n(intel_crtc,
4645 &intel_crtc->config->fdi_m_n, NULL);
4646 }
4647
4648 haswell_set_pipeconf(crtc);
4649
4650 intel_set_pipe_csc(crtc);
4651
4652 intel_crtc->active = true;
4653
4654 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4655 for_each_encoder_on_crtc(dev, crtc, encoder)
4656 if (encoder->pre_enable)
4657 encoder->pre_enable(encoder);
4658
4659 if (intel_crtc->config->has_pch_encoder) {
4660 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4661 true);
4662 dev_priv->display.fdi_link_train(crtc);
4663 }
4664
4665 intel_ddi_enable_pipe_clock(intel_crtc);
4666
4667 if (IS_SKYLAKE(dev))
4668 skylake_pfit_enable(intel_crtc);
4669 else
4670 ironlake_pfit_enable(intel_crtc);
4671
4672 /*
4673 * On ILK+ LUT must be loaded before the pipe is running but with
4674 * clocks enabled
4675 */
4676 intel_crtc_load_lut(crtc);
4677
4678 intel_ddi_set_pipe_settings(crtc);
4679 intel_ddi_enable_transcoder_func(crtc);
4680
4681 intel_update_watermarks(crtc);
4682 intel_enable_pipe(intel_crtc);
4683
4684 if (intel_crtc->config->has_pch_encoder)
4685 lpt_pch_enable(crtc);
4686
4687 if (intel_crtc->config->dp_encoder_is_mst)
4688 intel_ddi_set_vc_payload_alloc(crtc, true);
4689
4690 assert_vblank_disabled(crtc);
4691 drm_crtc_vblank_on(crtc);
4692
4693 for_each_encoder_on_crtc(dev, crtc, encoder) {
4694 encoder->enable(encoder);
4695 intel_opregion_notify_encoder(encoder, true);
4696 }
4697
4698 /* If we change the relative order between pipe/planes enabling, we need
4699 * to change the workaround. */
4700 haswell_mode_set_planes_workaround(intel_crtc);
4701 intel_crtc_enable_planes(crtc);
4702}
4703
4704static void skylake_pfit_disable(struct intel_crtc *crtc)
4705{
4706 struct drm_device *dev = crtc->base.dev;
4707 struct drm_i915_private *dev_priv = dev->dev_private;
4708 int pipe = crtc->pipe;
4709
4710 /* To avoid upsetting the power well on haswell only disable the pfit if
4711 * it's in use. The hw state code will make sure we get this right. */
4712 if (crtc->config->pch_pfit.enabled) {
4713 I915_WRITE(PS_CTL(pipe), 0);
4714 I915_WRITE(PS_WIN_POS(pipe), 0);
4715 I915_WRITE(PS_WIN_SZ(pipe), 0);
4716 }
4717}
4718
4719static void ironlake_pfit_disable(struct intel_crtc *crtc)
4720{
4721 struct drm_device *dev = crtc->base.dev;
4722 struct drm_i915_private *dev_priv = dev->dev_private;
4723 int pipe = crtc->pipe;
4724
4725 /* To avoid upsetting the power well on haswell only disable the pfit if
4726 * it's in use. The hw state code will make sure we get this right. */
4727 if (crtc->config->pch_pfit.enabled) {
4728 I915_WRITE(PF_CTL(pipe), 0);
4729 I915_WRITE(PF_WIN_POS(pipe), 0);
4730 I915_WRITE(PF_WIN_SZ(pipe), 0);
4731 }
4732}
4733
4734static void ironlake_crtc_disable(struct drm_crtc *crtc)
4735{
4736 struct drm_device *dev = crtc->dev;
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4739 struct intel_encoder *encoder;
4740 int pipe = intel_crtc->pipe;
4741 u32 reg, temp;
4742
4743 if (!intel_crtc->active)
4744 return;
4745
4746 intel_crtc_disable_planes(crtc);
4747
4748 for_each_encoder_on_crtc(dev, crtc, encoder)
4749 encoder->disable(encoder);
4750
4751 drm_crtc_vblank_off(crtc);
4752 assert_vblank_disabled(crtc);
4753
4754 if (intel_crtc->config->has_pch_encoder)
4755 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4756
4757 intel_disable_pipe(intel_crtc);
4758
4759 ironlake_pfit_disable(intel_crtc);
4760
4761 for_each_encoder_on_crtc(dev, crtc, encoder)
4762 if (encoder->post_disable)
4763 encoder->post_disable(encoder);
4764
4765 if (intel_crtc->config->has_pch_encoder) {
4766 ironlake_fdi_disable(crtc);
4767
4768 ironlake_disable_pch_transcoder(dev_priv, pipe);
4769
4770 if (HAS_PCH_CPT(dev)) {
4771 /* disable TRANS_DP_CTL */
4772 reg = TRANS_DP_CTL(pipe);
4773 temp = I915_READ(reg);
4774 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4775 TRANS_DP_PORT_SEL_MASK);
4776 temp |= TRANS_DP_PORT_SEL_NONE;
4777 I915_WRITE(reg, temp);
4778
4779 /* disable DPLL_SEL */
4780 temp = I915_READ(PCH_DPLL_SEL);
4781 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
4782 I915_WRITE(PCH_DPLL_SEL, temp);
4783 }
4784
4785 /* disable PCH DPLL */
4786 intel_disable_shared_dpll(intel_crtc);
4787
4788 ironlake_fdi_pll_disable(intel_crtc);
4789 }
4790
4791 intel_crtc->active = false;
4792 intel_update_watermarks(crtc);
4793
4794 mutex_lock(&dev->struct_mutex);
4795 intel_fbc_update(dev);
4796 mutex_unlock(&dev->struct_mutex);
4797}
4798
4799static void haswell_crtc_disable(struct drm_crtc *crtc)
4800{
4801 struct drm_device *dev = crtc->dev;
4802 struct drm_i915_private *dev_priv = dev->dev_private;
4803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4804 struct intel_encoder *encoder;
4805 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
4806
4807 if (!intel_crtc->active)
4808 return;
4809
4810 intel_crtc_disable_planes(crtc);
4811
4812 for_each_encoder_on_crtc(dev, crtc, encoder) {
4813 intel_opregion_notify_encoder(encoder, false);
4814 encoder->disable(encoder);
4815 }
4816
4817 drm_crtc_vblank_off(crtc);
4818 assert_vblank_disabled(crtc);
4819
4820 if (intel_crtc->config->has_pch_encoder)
4821 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4822 false);
4823 intel_disable_pipe(intel_crtc);
4824
4825 if (intel_crtc->config->dp_encoder_is_mst)
4826 intel_ddi_set_vc_payload_alloc(crtc, false);
4827
4828 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4829
4830 if (IS_SKYLAKE(dev))
4831 skylake_pfit_disable(intel_crtc);
4832 else
4833 ironlake_pfit_disable(intel_crtc);
4834
4835 intel_ddi_disable_pipe_clock(intel_crtc);
4836
4837 if (intel_crtc->config->has_pch_encoder) {
4838 lpt_disable_pch_transcoder(dev_priv);
4839 intel_ddi_fdi_disable(crtc);
4840 }
4841
4842 for_each_encoder_on_crtc(dev, crtc, encoder)
4843 if (encoder->post_disable)
4844 encoder->post_disable(encoder);
4845
4846 intel_crtc->active = false;
4847 intel_update_watermarks(crtc);
4848
4849 mutex_lock(&dev->struct_mutex);
4850 intel_fbc_update(dev);
4851 mutex_unlock(&dev->struct_mutex);
4852
4853 if (intel_crtc_to_shared_dpll(intel_crtc))
4854 intel_disable_shared_dpll(intel_crtc);
4855}
4856
4857static void ironlake_crtc_off(struct drm_crtc *crtc)
4858{
4859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4860 intel_put_shared_dpll(intel_crtc);
4861}
4862
4863
4864static void i9xx_pfit_enable(struct intel_crtc *crtc)
4865{
4866 struct drm_device *dev = crtc->base.dev;
4867 struct drm_i915_private *dev_priv = dev->dev_private;
4868 struct intel_crtc_state *pipe_config = crtc->config;
4869
4870 if (!pipe_config->gmch_pfit.control)
4871 return;
4872
4873 /*
4874 * The panel fitter should only be adjusted whilst the pipe is disabled,
4875 * according to register description and PRM.
4876 */
4877 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4878 assert_pipe_disabled(dev_priv, crtc->pipe);
4879
4880 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4881 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
4882
4883 /* Border color in case we don't scale up to the full screen. Black by
4884 * default, change to something else for debugging. */
4885 I915_WRITE(BCLRPAT(crtc->pipe), 0);
4886}
4887
4888static enum intel_display_power_domain port_to_power_domain(enum port port)
4889{
4890 switch (port) {
4891 case PORT_A:
4892 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4893 case PORT_B:
4894 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4895 case PORT_C:
4896 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4897 case PORT_D:
4898 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4899 default:
4900 WARN_ON_ONCE(1);
4901 return POWER_DOMAIN_PORT_OTHER;
4902 }
4903}
4904
4905#define for_each_power_domain(domain, mask) \
4906 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4907 if ((1 << (domain)) & (mask))
4908
4909enum intel_display_power_domain
4910intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4911{
4912 struct drm_device *dev = intel_encoder->base.dev;
4913 struct intel_digital_port *intel_dig_port;
4914
4915 switch (intel_encoder->type) {
4916 case INTEL_OUTPUT_UNKNOWN:
4917 /* Only DDI platforms should ever use this output type */
4918 WARN_ON_ONCE(!HAS_DDI(dev));
4919 case INTEL_OUTPUT_DISPLAYPORT:
4920 case INTEL_OUTPUT_HDMI:
4921 case INTEL_OUTPUT_EDP:
4922 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4923 return port_to_power_domain(intel_dig_port->port);
4924 case INTEL_OUTPUT_DP_MST:
4925 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4926 return port_to_power_domain(intel_dig_port->port);
4927 case INTEL_OUTPUT_ANALOG:
4928 return POWER_DOMAIN_PORT_CRT;
4929 case INTEL_OUTPUT_DSI:
4930 return POWER_DOMAIN_PORT_DSI;
4931 default:
4932 return POWER_DOMAIN_PORT_OTHER;
4933 }
4934}
4935
4936static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4937{
4938 struct drm_device *dev = crtc->dev;
4939 struct intel_encoder *intel_encoder;
4940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4941 enum pipe pipe = intel_crtc->pipe;
4942 unsigned long mask;
4943 enum transcoder transcoder;
4944
4945 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4946
4947 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4948 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4949 if (intel_crtc->config->pch_pfit.enabled ||
4950 intel_crtc->config->pch_pfit.force_thru)
4951 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4952
4953 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4954 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4955
4956 return mask;
4957}
4958
4959static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
4960{
4961 struct drm_device *dev = state->dev;
4962 struct drm_i915_private *dev_priv = dev->dev_private;
4963 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4964 struct intel_crtc *crtc;
4965
4966 /*
4967 * First get all needed power domains, then put all unneeded, to avoid
4968 * any unnecessary toggling of the power wells.
4969 */
4970 for_each_intel_crtc(dev, crtc) {
4971 enum intel_display_power_domain domain;
4972
4973 if (!crtc->base.state->enable)
4974 continue;
4975
4976 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
4977
4978 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4979 intel_display_power_get(dev_priv, domain);
4980 }
4981
4982 if (dev_priv->display.modeset_global_resources)
4983 dev_priv->display.modeset_global_resources(state);
4984
4985 for_each_intel_crtc(dev, crtc) {
4986 enum intel_display_power_domain domain;
4987
4988 for_each_power_domain(domain, crtc->enabled_power_domains)
4989 intel_display_power_put(dev_priv, domain);
4990
4991 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4992 }
4993
4994 intel_display_set_init_power(dev_priv, false);
4995}
4996
4997/* returns HPLL frequency in kHz */
4998static int valleyview_get_vco(struct drm_i915_private *dev_priv)
4999{
5000 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
5001
5002 /* Obtain SKU information */
5003 mutex_lock(&dev_priv->dpio_lock);
5004 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5005 CCK_FUSE_HPLL_FREQ_MASK;
5006 mutex_unlock(&dev_priv->dpio_lock);
5007
5008 return vco_freq[hpll_freq] * 1000;
5009}
5010
5011static void vlv_update_cdclk(struct drm_device *dev)
5012{
5013 struct drm_i915_private *dev_priv = dev->dev_private;
5014
5015 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5016 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5017 dev_priv->vlv_cdclk_freq);
5018
5019 /*
5020 * Program the gmbus_freq based on the cdclk frequency.
5021 * BSpec erroneously claims we should aim for 4MHz, but
5022 * in fact 1MHz is the correct frequency.
5023 */
5024 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
5025}
5026
5027/* Adjust CDclk dividers to allow high res or save power if possible */
5028static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5029{
5030 struct drm_i915_private *dev_priv = dev->dev_private;
5031 u32 val, cmd;
5032
5033 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
5034
5035 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
5036 cmd = 2;
5037 else if (cdclk == 266667)
5038 cmd = 1;
5039 else
5040 cmd = 0;
5041
5042 mutex_lock(&dev_priv->rps.hw_lock);
5043 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5044 val &= ~DSPFREQGUAR_MASK;
5045 val |= (cmd << DSPFREQGUAR_SHIFT);
5046 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5047 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5048 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5049 50)) {
5050 DRM_ERROR("timed out waiting for CDclk change\n");
5051 }
5052 mutex_unlock(&dev_priv->rps.hw_lock);
5053
5054 if (cdclk == 400000) {
5055 u32 divider;
5056
5057 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5058
5059 mutex_lock(&dev_priv->dpio_lock);
5060 /* adjust cdclk divider */
5061 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5062 val &= ~DISPLAY_FREQUENCY_VALUES;
5063 val |= divider;
5064 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
5065
5066 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5067 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5068 50))
5069 DRM_ERROR("timed out waiting for CDclk change\n");
5070 mutex_unlock(&dev_priv->dpio_lock);
5071 }
5072
5073 mutex_lock(&dev_priv->dpio_lock);
5074 /* adjust self-refresh exit latency value */
5075 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5076 val &= ~0x7f;
5077
5078 /*
5079 * For high bandwidth configs, we set a higher latency in the bunit
5080 * so that the core display fetch happens in time to avoid underruns.
5081 */
5082 if (cdclk == 400000)
5083 val |= 4500 / 250; /* 4.5 usec */
5084 else
5085 val |= 3000 / 250; /* 3.0 usec */
5086 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5087 mutex_unlock(&dev_priv->dpio_lock);
5088
5089 vlv_update_cdclk(dev);
5090}
5091
5092static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5093{
5094 struct drm_i915_private *dev_priv = dev->dev_private;
5095 u32 val, cmd;
5096
5097 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
5098
5099 switch (cdclk) {
5100 case 333333:
5101 case 320000:
5102 case 266667:
5103 case 200000:
5104 break;
5105 default:
5106 MISSING_CASE(cdclk);
5107 return;
5108 }
5109
5110 /*
5111 * Specs are full of misinformation, but testing on actual
5112 * hardware has shown that we just need to write the desired
5113 * CCK divider into the Punit register.
5114 */
5115 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5116
5117 mutex_lock(&dev_priv->rps.hw_lock);
5118 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5119 val &= ~DSPFREQGUAR_MASK_CHV;
5120 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5121 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5122 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5123 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5124 50)) {
5125 DRM_ERROR("timed out waiting for CDclk change\n");
5126 }
5127 mutex_unlock(&dev_priv->rps.hw_lock);
5128
5129 vlv_update_cdclk(dev);
5130}
5131
5132static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5133 int max_pixclk)
5134{
5135 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
5136 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
5137
5138 /*
5139 * Really only a few cases to deal with, as only 4 CDclks are supported:
5140 * 200MHz
5141 * 267MHz
5142 * 320/333MHz (depends on HPLL freq)
5143 * 400MHz (VLV only)
5144 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5145 * of the lower bin and adjust if needed.
5146 *
5147 * We seem to get an unstable or solid color picture at 200MHz.
5148 * Not sure what's wrong. For now use 200MHz only when all pipes
5149 * are off.
5150 */
5151 if (!IS_CHERRYVIEW(dev_priv) &&
5152 max_pixclk > freq_320*limit/100)
5153 return 400000;
5154 else if (max_pixclk > 266667*limit/100)
5155 return freq_320;
5156 else if (max_pixclk > 0)
5157 return 266667;
5158 else
5159 return 200000;
5160}
5161
5162/* compute the max pixel clock for new configuration */
5163static int intel_mode_max_pixclk(struct drm_atomic_state *state)
5164{
5165 struct drm_device *dev = state->dev;
5166 struct intel_crtc *intel_crtc;
5167 struct intel_crtc_state *crtc_state;
5168 int max_pixclk = 0;
5169
5170 for_each_intel_crtc(dev, intel_crtc) {
5171 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5172 if (IS_ERR(crtc_state))
5173 return PTR_ERR(crtc_state);
5174
5175 if (!crtc_state->base.enable)
5176 continue;
5177
5178 max_pixclk = max(max_pixclk,
5179 crtc_state->base.adjusted_mode.crtc_clock);
5180 }
5181
5182 return max_pixclk;
5183}
5184
5185static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
5186 unsigned *prepare_pipes)
5187{
5188 struct drm_i915_private *dev_priv = to_i915(state->dev);
5189 struct intel_crtc *intel_crtc;
5190 int max_pixclk = intel_mode_max_pixclk(state);
5191
5192 if (max_pixclk < 0)
5193 return max_pixclk;
5194
5195 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
5196 dev_priv->vlv_cdclk_freq)
5197 return 0;
5198
5199 /* disable/enable all currently active pipes while we change cdclk */
5200 for_each_intel_crtc(state->dev, intel_crtc)
5201 if (intel_crtc->base.state->enable)
5202 *prepare_pipes |= (1 << intel_crtc->pipe);
5203
5204 return 0;
5205}
5206
5207static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5208{
5209 unsigned int credits, default_credits;
5210
5211 if (IS_CHERRYVIEW(dev_priv))
5212 default_credits = PFI_CREDIT(12);
5213 else
5214 default_credits = PFI_CREDIT(8);
5215
5216 if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
5217 /* CHV suggested value is 31 or 63 */
5218 if (IS_CHERRYVIEW(dev_priv))
5219 credits = PFI_CREDIT_31;
5220 else
5221 credits = PFI_CREDIT(15);
5222 } else {
5223 credits = default_credits;
5224 }
5225
5226 /*
5227 * WA - write default credits before re-programming
5228 * FIXME: should we also set the resend bit here?
5229 */
5230 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5231 default_credits);
5232
5233 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5234 credits | PFI_CREDIT_RESEND);
5235
5236 /*
5237 * FIXME is this guaranteed to clear
5238 * immediately or should we poll for it?
5239 */
5240 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5241}
5242
5243static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
5244{
5245 struct drm_device *dev = state->dev;
5246 struct drm_i915_private *dev_priv = dev->dev_private;
5247 int max_pixclk = intel_mode_max_pixclk(state);
5248 int req_cdclk;
5249
5250 /* The only reason this can fail is if we fail to add the crtc_state
5251 * to the atomic state. But that can't happen since the call to
5252 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5253 * can't have failed otherwise the mode set would be aborted) added all
5254 * the states already. */
5255 if (WARN_ON(max_pixclk < 0))
5256 return;
5257
5258 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5259
5260 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
5261 /*
5262 * FIXME: We can end up here with all power domains off, yet
5263 * with a CDCLK frequency other than the minimum. To account
5264 * for this take the PIPE-A power domain, which covers the HW
5265 * blocks needed for the following programming. This can be
5266 * removed once it's guaranteed that we get here either with
5267 * the minimum CDCLK set, or the required power domains
5268 * enabled.
5269 */
5270 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5271
5272 if (IS_CHERRYVIEW(dev))
5273 cherryview_set_cdclk(dev, req_cdclk);
5274 else
5275 valleyview_set_cdclk(dev, req_cdclk);
5276
5277 vlv_program_pfi_credits(dev_priv);
5278
5279 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
5280 }
5281}
5282
5283static void valleyview_crtc_enable(struct drm_crtc *crtc)
5284{
5285 struct drm_device *dev = crtc->dev;
5286 struct drm_i915_private *dev_priv = to_i915(dev);
5287 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5288 struct intel_encoder *encoder;
5289 int pipe = intel_crtc->pipe;
5290 bool is_dsi;
5291
5292 WARN_ON(!crtc->state->enable);
5293
5294 if (intel_crtc->active)
5295 return;
5296
5297 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
5298
5299 if (!is_dsi) {
5300 if (IS_CHERRYVIEW(dev))
5301 chv_prepare_pll(intel_crtc, intel_crtc->config);
5302 else
5303 vlv_prepare_pll(intel_crtc, intel_crtc->config);
5304 }
5305
5306 if (intel_crtc->config->has_dp_encoder)
5307 intel_dp_set_m_n(intel_crtc, M1_N1);
5308
5309 intel_set_pipe_timings(intel_crtc);
5310
5311 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5312 struct drm_i915_private *dev_priv = dev->dev_private;
5313
5314 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5315 I915_WRITE(CHV_CANVAS(pipe), 0);
5316 }
5317
5318 i9xx_set_pipeconf(intel_crtc);
5319
5320 intel_crtc->active = true;
5321
5322 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5323
5324 for_each_encoder_on_crtc(dev, crtc, encoder)
5325 if (encoder->pre_pll_enable)
5326 encoder->pre_pll_enable(encoder);
5327
5328 if (!is_dsi) {
5329 if (IS_CHERRYVIEW(dev))
5330 chv_enable_pll(intel_crtc, intel_crtc->config);
5331 else
5332 vlv_enable_pll(intel_crtc, intel_crtc->config);
5333 }
5334
5335 for_each_encoder_on_crtc(dev, crtc, encoder)
5336 if (encoder->pre_enable)
5337 encoder->pre_enable(encoder);
5338
5339 i9xx_pfit_enable(intel_crtc);
5340
5341 intel_crtc_load_lut(crtc);
5342
5343 intel_update_watermarks(crtc);
5344 intel_enable_pipe(intel_crtc);
5345
5346 assert_vblank_disabled(crtc);
5347 drm_crtc_vblank_on(crtc);
5348
5349 for_each_encoder_on_crtc(dev, crtc, encoder)
5350 encoder->enable(encoder);
5351
5352 intel_crtc_enable_planes(crtc);
5353
5354 /* Underruns don't raise interrupts, so check manually. */
5355 i9xx_check_fifo_underruns(dev_priv);
5356}
5357
5358static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5359{
5360 struct drm_device *dev = crtc->base.dev;
5361 struct drm_i915_private *dev_priv = dev->dev_private;
5362
5363 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5364 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
5365}
5366
5367static void i9xx_crtc_enable(struct drm_crtc *crtc)
5368{
5369 struct drm_device *dev = crtc->dev;
5370 struct drm_i915_private *dev_priv = to_i915(dev);
5371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5372 struct intel_encoder *encoder;
5373 int pipe = intel_crtc->pipe;
5374
5375 WARN_ON(!crtc->state->enable);
5376
5377 if (intel_crtc->active)
5378 return;
5379
5380 i9xx_set_pll_dividers(intel_crtc);
5381
5382 if (intel_crtc->config->has_dp_encoder)
5383 intel_dp_set_m_n(intel_crtc, M1_N1);
5384
5385 intel_set_pipe_timings(intel_crtc);
5386
5387 i9xx_set_pipeconf(intel_crtc);
5388
5389 intel_crtc->active = true;
5390
5391 if (!IS_GEN2(dev))
5392 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5393
5394 for_each_encoder_on_crtc(dev, crtc, encoder)
5395 if (encoder->pre_enable)
5396 encoder->pre_enable(encoder);
5397
5398 i9xx_enable_pll(intel_crtc);
5399
5400 i9xx_pfit_enable(intel_crtc);
5401
5402 intel_crtc_load_lut(crtc);
5403
5404 intel_update_watermarks(crtc);
5405 intel_enable_pipe(intel_crtc);
5406
5407 assert_vblank_disabled(crtc);
5408 drm_crtc_vblank_on(crtc);
5409
5410 for_each_encoder_on_crtc(dev, crtc, encoder)
5411 encoder->enable(encoder);
5412
5413 intel_crtc_enable_planes(crtc);
5414
5415 /*
5416 * Gen2 reports pipe underruns whenever all planes are disabled.
5417 * So don't enable underrun reporting before at least some planes
5418 * are enabled.
5419 * FIXME: Need to fix the logic to work when we turn off all planes
5420 * but leave the pipe running.
5421 */
5422 if (IS_GEN2(dev))
5423 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
5424
5425 /* Underruns don't raise interrupts, so check manually. */
5426 i9xx_check_fifo_underruns(dev_priv);
5427}
5428
5429static void i9xx_pfit_disable(struct intel_crtc *crtc)
5430{
5431 struct drm_device *dev = crtc->base.dev;
5432 struct drm_i915_private *dev_priv = dev->dev_private;
5433
5434 if (!crtc->config->gmch_pfit.control)
5435 return;
5436
5437 assert_pipe_disabled(dev_priv, crtc->pipe);
5438
5439 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5440 I915_READ(PFIT_CONTROL));
5441 I915_WRITE(PFIT_CONTROL, 0);
5442}
5443
5444static void i9xx_crtc_disable(struct drm_crtc *crtc)
5445{
5446 struct drm_device *dev = crtc->dev;
5447 struct drm_i915_private *dev_priv = dev->dev_private;
5448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5449 struct intel_encoder *encoder;
5450 int pipe = intel_crtc->pipe;
5451
5452 if (!intel_crtc->active)
5453 return;
5454
5455 /*
5456 * Gen2 reports pipe underruns whenever all planes are disabled.
5457 * So diasble underrun reporting before all the planes get disabled.
5458 * FIXME: Need to fix the logic to work when we turn off all planes
5459 * but leave the pipe running.
5460 */
5461 if (IS_GEN2(dev))
5462 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5463
5464 /*
5465 * Vblank time updates from the shadow to live plane control register
5466 * are blocked if the memory self-refresh mode is active at that
5467 * moment. So to make sure the plane gets truly disabled, disable
5468 * first the self-refresh mode. The self-refresh enable bit in turn
5469 * will be checked/applied by the HW only at the next frame start
5470 * event which is after the vblank start event, so we need to have a
5471 * wait-for-vblank between disabling the plane and the pipe.
5472 */
5473 intel_set_memory_cxsr(dev_priv, false);
5474 intel_crtc_disable_planes(crtc);
5475
5476 /*
5477 * On gen2 planes are double buffered but the pipe isn't, so we must
5478 * wait for planes to fully turn off before disabling the pipe.
5479 * We also need to wait on all gmch platforms because of the
5480 * self-refresh mode constraint explained above.
5481 */
5482 intel_wait_for_vblank(dev, pipe);
5483
5484 for_each_encoder_on_crtc(dev, crtc, encoder)
5485 encoder->disable(encoder);
5486
5487 drm_crtc_vblank_off(crtc);
5488 assert_vblank_disabled(crtc);
5489
5490 intel_disable_pipe(intel_crtc);
5491
5492 i9xx_pfit_disable(intel_crtc);
5493
5494 for_each_encoder_on_crtc(dev, crtc, encoder)
5495 if (encoder->post_disable)
5496 encoder->post_disable(encoder);
5497
5498 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
5499 if (IS_CHERRYVIEW(dev))
5500 chv_disable_pll(dev_priv, pipe);
5501 else if (IS_VALLEYVIEW(dev))
5502 vlv_disable_pll(dev_priv, pipe);
5503 else
5504 i9xx_disable_pll(intel_crtc);
5505 }
5506
5507 if (!IS_GEN2(dev))
5508 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
5509
5510 intel_crtc->active = false;
5511 intel_update_watermarks(crtc);
5512
5513 mutex_lock(&dev->struct_mutex);
5514 intel_fbc_update(dev);
5515 mutex_unlock(&dev->struct_mutex);
5516}
5517
5518static void i9xx_crtc_off(struct drm_crtc *crtc)
5519{
5520}
5521
5522/* Master function to enable/disable CRTC and corresponding power wells */
5523void intel_crtc_control(struct drm_crtc *crtc, bool enable)
5524{
5525 struct drm_device *dev = crtc->dev;
5526 struct drm_i915_private *dev_priv = dev->dev_private;
5527 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5528 enum intel_display_power_domain domain;
5529 unsigned long domains;
5530
5531 if (enable) {
5532 if (!intel_crtc->active) {
5533 domains = get_crtc_power_domains(crtc);
5534 for_each_power_domain(domain, domains)
5535 intel_display_power_get(dev_priv, domain);
5536 intel_crtc->enabled_power_domains = domains;
5537
5538 dev_priv->display.crtc_enable(crtc);
5539 }
5540 } else {
5541 if (intel_crtc->active) {
5542 dev_priv->display.crtc_disable(crtc);
5543
5544 domains = intel_crtc->enabled_power_domains;
5545 for_each_power_domain(domain, domains)
5546 intel_display_power_put(dev_priv, domain);
5547 intel_crtc->enabled_power_domains = 0;
5548 }
5549 }
5550}
5551
5552/**
5553 * Sets the power management mode of the pipe and plane.
5554 */
5555void intel_crtc_update_dpms(struct drm_crtc *crtc)
5556{
5557 struct drm_device *dev = crtc->dev;
5558 struct intel_encoder *intel_encoder;
5559 bool enable = false;
5560
5561 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5562 enable |= intel_encoder->connectors_active;
5563
5564 intel_crtc_control(crtc, enable);
5565}
5566
5567static void intel_crtc_disable(struct drm_crtc *crtc)
5568{
5569 struct drm_device *dev = crtc->dev;
5570 struct drm_connector *connector;
5571 struct drm_i915_private *dev_priv = dev->dev_private;
5572
5573 /* crtc should still be enabled when we disable it. */
5574 WARN_ON(!crtc->state->enable);
5575
5576 dev_priv->display.crtc_disable(crtc);
5577 dev_priv->display.off(crtc);
5578
5579 crtc->primary->funcs->disable_plane(crtc->primary);
5580
5581 /* Update computed state. */
5582 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5583 if (!connector->encoder || !connector->encoder->crtc)
5584 continue;
5585
5586 if (connector->encoder->crtc != crtc)
5587 continue;
5588
5589 connector->dpms = DRM_MODE_DPMS_OFF;
5590 to_intel_encoder(connector->encoder)->connectors_active = false;
5591 }
5592}
5593
5594void intel_encoder_destroy(struct drm_encoder *encoder)
5595{
5596 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
5597
5598 drm_encoder_cleanup(encoder);
5599 kfree(intel_encoder);
5600}
5601
5602/* Simple dpms helper for encoders with just one connector, no cloning and only
5603 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5604 * state of the entire output pipe. */
5605static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
5606{
5607 if (mode == DRM_MODE_DPMS_ON) {
5608 encoder->connectors_active = true;
5609
5610 intel_crtc_update_dpms(encoder->base.crtc);
5611 } else {
5612 encoder->connectors_active = false;
5613
5614 intel_crtc_update_dpms(encoder->base.crtc);
5615 }
5616}
5617
5618/* Cross check the actual hw state with our own modeset state tracking (and it's
5619 * internal consistency). */
5620static void intel_connector_check_state(struct intel_connector *connector)
5621{
5622 if (connector->get_hw_state(connector)) {
5623 struct intel_encoder *encoder = connector->encoder;
5624 struct drm_crtc *crtc;
5625 bool encoder_enabled;
5626 enum pipe pipe;
5627
5628 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5629 connector->base.base.id,
5630 connector->base.name);
5631
5632 /* there is no real hw state for MST connectors */
5633 if (connector->mst_port)
5634 return;
5635
5636 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5637 "wrong connector dpms state\n");
5638 I915_STATE_WARN(connector->base.encoder != &encoder->base,
5639 "active connector not linked to encoder\n");
5640
5641 if (encoder) {
5642 I915_STATE_WARN(!encoder->connectors_active,
5643 "encoder->connectors_active not set\n");
5644
5645 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5646 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
5647 if (I915_STATE_WARN_ON(!encoder->base.crtc))
5648 return;
5649
5650 crtc = encoder->base.crtc;
5651
5652 I915_STATE_WARN(!crtc->state->enable,
5653 "crtc not enabled\n");
5654 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5655 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
5656 "encoder active on the wrong pipe\n");
5657 }
5658 }
5659}
5660
5661/* Even simpler default implementation, if there's really no special case to
5662 * consider. */
5663void intel_connector_dpms(struct drm_connector *connector, int mode)
5664{
5665 /* All the simple cases only support two dpms states. */
5666 if (mode != DRM_MODE_DPMS_ON)
5667 mode = DRM_MODE_DPMS_OFF;
5668
5669 if (mode == connector->dpms)
5670 return;
5671
5672 connector->dpms = mode;
5673
5674 /* Only need to change hw state when actually enabled */
5675 if (connector->encoder)
5676 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
5677
5678 intel_modeset_check_state(connector->dev);
5679}
5680
5681/* Simple connector->get_hw_state implementation for encoders that support only
5682 * one connector and no cloning and hence the encoder state determines the state
5683 * of the connector. */
5684bool intel_connector_get_hw_state(struct intel_connector *connector)
5685{
5686 enum pipe pipe = 0;
5687 struct intel_encoder *encoder = connector->encoder;
5688
5689 return encoder->get_hw_state(encoder, &pipe);
5690}
5691
5692static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
5693{
5694 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
5695 return crtc_state->fdi_lanes;
5696
5697 return 0;
5698}
5699
5700static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5701 struct intel_crtc_state *pipe_config)
5702{
5703 struct drm_atomic_state *state = pipe_config->base.state;
5704 struct intel_crtc *other_crtc;
5705 struct intel_crtc_state *other_crtc_state;
5706
5707 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5708 pipe_name(pipe), pipe_config->fdi_lanes);
5709 if (pipe_config->fdi_lanes > 4) {
5710 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5711 pipe_name(pipe), pipe_config->fdi_lanes);
5712 return -EINVAL;
5713 }
5714
5715 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
5716 if (pipe_config->fdi_lanes > 2) {
5717 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5718 pipe_config->fdi_lanes);
5719 return -EINVAL;
5720 } else {
5721 return 0;
5722 }
5723 }
5724
5725 if (INTEL_INFO(dev)->num_pipes == 2)
5726 return 0;
5727
5728 /* Ivybridge 3 pipe is really complicated */
5729 switch (pipe) {
5730 case PIPE_A:
5731 return 0;
5732 case PIPE_B:
5733 if (pipe_config->fdi_lanes <= 2)
5734 return 0;
5735
5736 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
5737 other_crtc_state =
5738 intel_atomic_get_crtc_state(state, other_crtc);
5739 if (IS_ERR(other_crtc_state))
5740 return PTR_ERR(other_crtc_state);
5741
5742 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
5743 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5744 pipe_name(pipe), pipe_config->fdi_lanes);
5745 return -EINVAL;
5746 }
5747 return 0;
5748 case PIPE_C:
5749 if (pipe_config->fdi_lanes > 2) {
5750 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
5751 pipe_name(pipe), pipe_config->fdi_lanes);
5752 return -EINVAL;
5753 }
5754
5755 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
5756 other_crtc_state =
5757 intel_atomic_get_crtc_state(state, other_crtc);
5758 if (IS_ERR(other_crtc_state))
5759 return PTR_ERR(other_crtc_state);
5760
5761 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
5762 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5763 return -EINVAL;
5764 }
5765 return 0;
5766 default:
5767 BUG();
5768 }
5769}
5770
5771#define RETRY 1
5772static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5773 struct intel_crtc_state *pipe_config)
5774{
5775 struct drm_device *dev = intel_crtc->base.dev;
5776 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
5777 int lane, link_bw, fdi_dotclock, ret;
5778 bool needs_recompute = false;
5779
5780retry:
5781 /* FDI is a binary signal running at ~2.7GHz, encoding
5782 * each output octet as 10 bits. The actual frequency
5783 * is stored as a divider into a 100MHz clock, and the
5784 * mode pixel clock is stored in units of 1KHz.
5785 * Hence the bw of each lane in terms of the mode signal
5786 * is:
5787 */
5788 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5789
5790 fdi_dotclock = adjusted_mode->crtc_clock;
5791
5792 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
5793 pipe_config->pipe_bpp);
5794
5795 pipe_config->fdi_lanes = lane;
5796
5797 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
5798 link_bw, &pipe_config->fdi_m_n);
5799
5800 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5801 intel_crtc->pipe, pipe_config);
5802 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
5803 pipe_config->pipe_bpp -= 2*3;
5804 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5805 pipe_config->pipe_bpp);
5806 needs_recompute = true;
5807 pipe_config->bw_constrained = true;
5808
5809 goto retry;
5810 }
5811
5812 if (needs_recompute)
5813 return RETRY;
5814
5815 return ret;
5816}
5817
5818static void hsw_compute_ips_config(struct intel_crtc *crtc,
5819 struct intel_crtc_state *pipe_config)
5820{
5821 pipe_config->ips_enabled = i915.enable_ips &&
5822 hsw_crtc_supports_ips(crtc) &&
5823 pipe_config->pipe_bpp <= 24;
5824}
5825
5826static int intel_crtc_compute_config(struct intel_crtc *crtc,
5827 struct intel_crtc_state *pipe_config)
5828{
5829 struct drm_device *dev = crtc->base.dev;
5830 struct drm_i915_private *dev_priv = dev->dev_private;
5831 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
5832
5833 /* FIXME should check pixel clock limits on all platforms */
5834 if (INTEL_INFO(dev)->gen < 4) {
5835 int clock_limit =
5836 dev_priv->display.get_display_clock_speed(dev);
5837
5838 /*
5839 * Enable pixel doubling when the dot clock
5840 * is > 90% of the (display) core speed.
5841 *
5842 * GDG double wide on either pipe,
5843 * otherwise pipe A only.
5844 */
5845 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
5846 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
5847 clock_limit *= 2;
5848 pipe_config->double_wide = true;
5849 }
5850
5851 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
5852 return -EINVAL;
5853 }
5854
5855 /*
5856 * Pipe horizontal size must be even in:
5857 * - DVO ganged mode
5858 * - LVDS dual channel mode
5859 * - Double wide pipe
5860 */
5861 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
5862 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5863 pipe_config->pipe_src_w &= ~1;
5864
5865 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5866 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
5867 */
5868 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5869 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
5870 return -EINVAL;
5871
5872 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5873 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
5874 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5875 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5876 * for lvds. */
5877 pipe_config->pipe_bpp = 8*3;
5878 }
5879
5880 if (HAS_IPS(dev))
5881 hsw_compute_ips_config(crtc, pipe_config);
5882
5883 if (pipe_config->has_pch_encoder)
5884 return ironlake_fdi_compute_config(crtc, pipe_config);
5885
5886 return 0;
5887}
5888
5889static int skylake_get_display_clock_speed(struct drm_device *dev)
5890{
5891 struct drm_i915_private *dev_priv = to_i915(dev);
5892 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
5893 uint32_t cdctl = I915_READ(CDCLK_CTL);
5894 uint32_t linkrate;
5895
5896 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
5897 WARN(1, "LCPLL1 not enabled\n");
5898 return 24000; /* 24MHz is the cd freq with NSSC ref */
5899 }
5900
5901 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
5902 return 540000;
5903
5904 linkrate = (I915_READ(DPLL_CTRL1) &
5905 DPLL_CRTL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
5906
5907 if (linkrate == DPLL_CRTL1_LINK_RATE_2160 ||
5908 linkrate == DPLL_CRTL1_LINK_RATE_1080) {
5909 /* vco 8640 */
5910 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
5911 case CDCLK_FREQ_450_432:
5912 return 432000;
5913 case CDCLK_FREQ_337_308:
5914 return 308570;
5915 case CDCLK_FREQ_675_617:
5916 return 617140;
5917 default:
5918 WARN(1, "Unknown cd freq selection\n");
5919 }
5920 } else {
5921 /* vco 8100 */
5922 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
5923 case CDCLK_FREQ_450_432:
5924 return 450000;
5925 case CDCLK_FREQ_337_308:
5926 return 337500;
5927 case CDCLK_FREQ_675_617:
5928 return 675000;
5929 default:
5930 WARN(1, "Unknown cd freq selection\n");
5931 }
5932 }
5933
5934 /* error case, do as if DPLL0 isn't enabled */
5935 return 24000;
5936}
5937
5938static int broadwell_get_display_clock_speed(struct drm_device *dev)
5939{
5940 struct drm_i915_private *dev_priv = dev->dev_private;
5941 uint32_t lcpll = I915_READ(LCPLL_CTL);
5942 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
5943
5944 if (lcpll & LCPLL_CD_SOURCE_FCLK)
5945 return 800000;
5946 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5947 return 450000;
5948 else if (freq == LCPLL_CLK_FREQ_450)
5949 return 450000;
5950 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
5951 return 540000;
5952 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
5953 return 337500;
5954 else
5955 return 675000;
5956}
5957
5958static int haswell_get_display_clock_speed(struct drm_device *dev)
5959{
5960 struct drm_i915_private *dev_priv = dev->dev_private;
5961 uint32_t lcpll = I915_READ(LCPLL_CTL);
5962 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
5963
5964 if (lcpll & LCPLL_CD_SOURCE_FCLK)
5965 return 800000;
5966 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5967 return 450000;
5968 else if (freq == LCPLL_CLK_FREQ_450)
5969 return 450000;
5970 else if (IS_HSW_ULT(dev))
5971 return 337500;
5972 else
5973 return 540000;
5974}
5975
5976static int valleyview_get_display_clock_speed(struct drm_device *dev)
5977{
5978 struct drm_i915_private *dev_priv = dev->dev_private;
5979 u32 val;
5980 int divider;
5981
5982 if (dev_priv->hpll_freq == 0)
5983 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
5984
5985 mutex_lock(&dev_priv->dpio_lock);
5986 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5987 mutex_unlock(&dev_priv->dpio_lock);
5988
5989 divider = val & DISPLAY_FREQUENCY_VALUES;
5990
5991 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5992 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5993 "cdclk change in progress\n");
5994
5995 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
5996}
5997
5998static int ilk_get_display_clock_speed(struct drm_device *dev)
5999{
6000 return 450000;
6001}
6002
6003static int i945_get_display_clock_speed(struct drm_device *dev)
6004{
6005 return 400000;
6006}
6007
6008static int i915_get_display_clock_speed(struct drm_device *dev)
6009{
6010 return 333333;
6011}
6012
6013static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6014{
6015 return 200000;
6016}
6017
6018static int pnv_get_display_clock_speed(struct drm_device *dev)
6019{
6020 u16 gcfgc = 0;
6021
6022 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6023
6024 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6025 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
6026 return 266667;
6027 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
6028 return 333333;
6029 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
6030 return 444444;
6031 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6032 return 200000;
6033 default:
6034 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6035 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
6036 return 133333;
6037 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
6038 return 166667;
6039 }
6040}
6041
6042static int i915gm_get_display_clock_speed(struct drm_device *dev)
6043{
6044 u16 gcfgc = 0;
6045
6046 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6047
6048 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
6049 return 133333;
6050 else {
6051 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6052 case GC_DISPLAY_CLOCK_333_MHZ:
6053 return 333333;
6054 default:
6055 case GC_DISPLAY_CLOCK_190_200_MHZ:
6056 return 190000;
6057 }
6058 }
6059}
6060
6061static int i865_get_display_clock_speed(struct drm_device *dev)
6062{
6063 return 266667;
6064}
6065
6066static int i855_get_display_clock_speed(struct drm_device *dev)
6067{
6068 u16 hpllcc = 0;
6069 /* Assume that the hardware is in the high speed state. This
6070 * should be the default.
6071 */
6072 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6073 case GC_CLOCK_133_200:
6074 case GC_CLOCK_100_200:
6075 return 200000;
6076 case GC_CLOCK_166_250:
6077 return 250000;
6078 case GC_CLOCK_100_133:
6079 return 133333;
6080 }
6081
6082 /* Shouldn't happen */
6083 return 0;
6084}
6085
6086static int i830_get_display_clock_speed(struct drm_device *dev)
6087{
6088 return 133333;
6089}
6090
6091static void
6092intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
6093{
6094 while (*num > DATA_LINK_M_N_MASK ||
6095 *den > DATA_LINK_M_N_MASK) {
6096 *num >>= 1;
6097 *den >>= 1;
6098 }
6099}
6100
6101static void compute_m_n(unsigned int m, unsigned int n,
6102 uint32_t *ret_m, uint32_t *ret_n)
6103{
6104 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6105 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6106 intel_reduce_m_n_ratio(ret_m, ret_n);
6107}
6108
6109void
6110intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6111 int pixel_clock, int link_clock,
6112 struct intel_link_m_n *m_n)
6113{
6114 m_n->tu = 64;
6115
6116 compute_m_n(bits_per_pixel * pixel_clock,
6117 link_clock * nlanes * 8,
6118 &m_n->gmch_m, &m_n->gmch_n);
6119
6120 compute_m_n(pixel_clock, link_clock,
6121 &m_n->link_m, &m_n->link_n);
6122}
6123
6124static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6125{
6126 if (i915.panel_use_ssc >= 0)
6127 return i915.panel_use_ssc != 0;
6128 return dev_priv->vbt.lvds_use_ssc
6129 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
6130}
6131
6132static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6133 int num_connectors)
6134{
6135 struct drm_device *dev = crtc_state->base.crtc->dev;
6136 struct drm_i915_private *dev_priv = dev->dev_private;
6137 int refclk;
6138
6139 WARN_ON(!crtc_state->base.state);
6140
6141 if (IS_VALLEYVIEW(dev)) {
6142 refclk = 100000;
6143 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
6144 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
6145 refclk = dev_priv->vbt.lvds_ssc_freq;
6146 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
6147 } else if (!IS_GEN2(dev)) {
6148 refclk = 96000;
6149 } else {
6150 refclk = 48000;
6151 }
6152
6153 return refclk;
6154}
6155
6156static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
6157{
6158 return (1 << dpll->n) << 16 | dpll->m2;
6159}
6160
6161static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6162{
6163 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
6164}
6165
6166static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
6167 struct intel_crtc_state *crtc_state,
6168 intel_clock_t *reduced_clock)
6169{
6170 struct drm_device *dev = crtc->base.dev;
6171 u32 fp, fp2 = 0;
6172
6173 if (IS_PINEVIEW(dev)) {
6174 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
6175 if (reduced_clock)
6176 fp2 = pnv_dpll_compute_fp(reduced_clock);
6177 } else {
6178 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
6179 if (reduced_clock)
6180 fp2 = i9xx_dpll_compute_fp(reduced_clock);
6181 }
6182
6183 crtc_state->dpll_hw_state.fp0 = fp;
6184
6185 crtc->lowfreq_avail = false;
6186 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
6187 reduced_clock) {
6188 crtc_state->dpll_hw_state.fp1 = fp2;
6189 crtc->lowfreq_avail = true;
6190 } else {
6191 crtc_state->dpll_hw_state.fp1 = fp;
6192 }
6193}
6194
6195static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6196 pipe)
6197{
6198 u32 reg_val;
6199
6200 /*
6201 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6202 * and set it to a reasonable value instead.
6203 */
6204 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
6205 reg_val &= 0xffffff00;
6206 reg_val |= 0x00000030;
6207 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
6208
6209 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
6210 reg_val &= 0x8cffffff;
6211 reg_val = 0x8c000000;
6212 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
6213
6214 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
6215 reg_val &= 0xffffff00;
6216 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
6217
6218 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
6219 reg_val &= 0x00ffffff;
6220 reg_val |= 0xb0000000;
6221 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
6222}
6223
6224static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6225 struct intel_link_m_n *m_n)
6226{
6227 struct drm_device *dev = crtc->base.dev;
6228 struct drm_i915_private *dev_priv = dev->dev_private;
6229 int pipe = crtc->pipe;
6230
6231 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6232 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6233 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6234 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
6235}
6236
6237static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
6238 struct intel_link_m_n *m_n,
6239 struct intel_link_m_n *m2_n2)
6240{
6241 struct drm_device *dev = crtc->base.dev;
6242 struct drm_i915_private *dev_priv = dev->dev_private;
6243 int pipe = crtc->pipe;
6244 enum transcoder transcoder = crtc->config->cpu_transcoder;
6245
6246 if (INTEL_INFO(dev)->gen >= 5) {
6247 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6248 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6249 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6250 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
6251 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6252 * for gen < 8) and if DRRS is supported (to make sure the
6253 * registers are not unnecessarily accessed).
6254 */
6255 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6256 crtc->config->has_drrs) {
6257 I915_WRITE(PIPE_DATA_M2(transcoder),
6258 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6259 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6260 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6261 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6262 }
6263 } else {
6264 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6265 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6266 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6267 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
6268 }
6269}
6270
6271void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
6272{
6273 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6274
6275 if (m_n == M1_N1) {
6276 dp_m_n = &crtc->config->dp_m_n;
6277 dp_m2_n2 = &crtc->config->dp_m2_n2;
6278 } else if (m_n == M2_N2) {
6279
6280 /*
6281 * M2_N2 registers are not supported. Hence m2_n2 divider value
6282 * needs to be programmed into M1_N1.
6283 */
6284 dp_m_n = &crtc->config->dp_m2_n2;
6285 } else {
6286 DRM_ERROR("Unsupported divider value\n");
6287 return;
6288 }
6289
6290 if (crtc->config->has_pch_encoder)
6291 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
6292 else
6293 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
6294}
6295
6296static void vlv_update_pll(struct intel_crtc *crtc,
6297 struct intel_crtc_state *pipe_config)
6298{
6299 u32 dpll, dpll_md;
6300
6301 /*
6302 * Enable DPIO clock input. We should never disable the reference
6303 * clock for pipe B, since VGA hotplug / manual detection depends
6304 * on it.
6305 */
6306 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6307 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6308 /* We should never disable this, set it here for state tracking */
6309 if (crtc->pipe == PIPE_B)
6310 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6311 dpll |= DPLL_VCO_ENABLE;
6312 pipe_config->dpll_hw_state.dpll = dpll;
6313
6314 dpll_md = (pipe_config->pixel_multiplier - 1)
6315 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6316 pipe_config->dpll_hw_state.dpll_md = dpll_md;
6317}
6318
6319static void vlv_prepare_pll(struct intel_crtc *crtc,
6320 const struct intel_crtc_state *pipe_config)
6321{
6322 struct drm_device *dev = crtc->base.dev;
6323 struct drm_i915_private *dev_priv = dev->dev_private;
6324 int pipe = crtc->pipe;
6325 u32 mdiv;
6326 u32 bestn, bestm1, bestm2, bestp1, bestp2;
6327 u32 coreclk, reg_val;
6328
6329 mutex_lock(&dev_priv->dpio_lock);
6330
6331 bestn = pipe_config->dpll.n;
6332 bestm1 = pipe_config->dpll.m1;
6333 bestm2 = pipe_config->dpll.m2;
6334 bestp1 = pipe_config->dpll.p1;
6335 bestp2 = pipe_config->dpll.p2;
6336
6337 /* See eDP HDMI DPIO driver vbios notes doc */
6338
6339 /* PLL B needs special handling */
6340 if (pipe == PIPE_B)
6341 vlv_pllb_recal_opamp(dev_priv, pipe);
6342
6343 /* Set up Tx target for periodic Rcomp update */
6344 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
6345
6346 /* Disable target IRef on PLL */
6347 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
6348 reg_val &= 0x00ffffff;
6349 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
6350
6351 /* Disable fast lock */
6352 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
6353
6354 /* Set idtafcrecal before PLL is enabled */
6355 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6356 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6357 mdiv |= ((bestn << DPIO_N_SHIFT));
6358 mdiv |= (1 << DPIO_K_SHIFT);
6359
6360 /*
6361 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6362 * but we don't support that).
6363 * Note: don't use the DAC post divider as it seems unstable.
6364 */
6365 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
6366 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
6367
6368 mdiv |= DPIO_ENABLE_CALIBRATION;
6369 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
6370
6371 /* Set HBR and RBR LPF coefficients */
6372 if (pipe_config->port_clock == 162000 ||
6373 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6374 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
6375 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
6376 0x009f0003);
6377 else
6378 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
6379 0x00d0000f);
6380
6381 if (pipe_config->has_dp_encoder) {
6382 /* Use SSC source */
6383 if (pipe == PIPE_A)
6384 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
6385 0x0df40000);
6386 else
6387 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
6388 0x0df70000);
6389 } else { /* HDMI or VGA */
6390 /* Use bend source */
6391 if (pipe == PIPE_A)
6392 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
6393 0x0df70000);
6394 else
6395 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
6396 0x0df40000);
6397 }
6398
6399 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
6400 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
6401 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6402 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
6403 coreclk |= 0x01000000;
6404 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
6405
6406 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
6407 mutex_unlock(&dev_priv->dpio_lock);
6408}
6409
6410static void chv_update_pll(struct intel_crtc *crtc,
6411 struct intel_crtc_state *pipe_config)
6412{
6413 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
6414 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6415 DPLL_VCO_ENABLE;
6416 if (crtc->pipe != PIPE_A)
6417 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6418
6419 pipe_config->dpll_hw_state.dpll_md =
6420 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6421}
6422
6423static void chv_prepare_pll(struct intel_crtc *crtc,
6424 const struct intel_crtc_state *pipe_config)
6425{
6426 struct drm_device *dev = crtc->base.dev;
6427 struct drm_i915_private *dev_priv = dev->dev_private;
6428 int pipe = crtc->pipe;
6429 int dpll_reg = DPLL(crtc->pipe);
6430 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6431 u32 loopfilter, tribuf_calcntr;
6432 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
6433 u32 dpio_val;
6434 int vco;
6435
6436 bestn = pipe_config->dpll.n;
6437 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6438 bestm1 = pipe_config->dpll.m1;
6439 bestm2 = pipe_config->dpll.m2 >> 22;
6440 bestp1 = pipe_config->dpll.p1;
6441 bestp2 = pipe_config->dpll.p2;
6442 vco = pipe_config->dpll.vco;
6443 dpio_val = 0;
6444 loopfilter = 0;
6445
6446 /*
6447 * Enable Refclk and SSC
6448 */
6449 I915_WRITE(dpll_reg,
6450 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
6451
6452 mutex_lock(&dev_priv->dpio_lock);
6453
6454 /* p1 and p2 divider */
6455 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6456 5 << DPIO_CHV_S1_DIV_SHIFT |
6457 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6458 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6459 1 << DPIO_CHV_K_DIV_SHIFT);
6460
6461 /* Feedback post-divider - m2 */
6462 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6463
6464 /* Feedback refclk divider - n and m1 */
6465 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6466 DPIO_CHV_M1_DIV_BY_2 |
6467 1 << DPIO_CHV_N_DIV_SHIFT);
6468
6469 /* M2 fraction division */
6470 if (bestm2_frac)
6471 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
6472
6473 /* M2 fraction division enable */
6474 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6475 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6476 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6477 if (bestm2_frac)
6478 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6479 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
6480
6481 /* Program digital lock detect threshold */
6482 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6483 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6484 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6485 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6486 if (!bestm2_frac)
6487 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6488 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6489
6490 /* Loop filter */
6491 if (vco == 5400000) {
6492 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6493 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6494 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6495 tribuf_calcntr = 0x9;
6496 } else if (vco <= 6200000) {
6497 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6498 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6499 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6500 tribuf_calcntr = 0x9;
6501 } else if (vco <= 6480000) {
6502 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6503 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6504 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6505 tribuf_calcntr = 0x8;
6506 } else {
6507 /* Not supported. Apply the same limits as in the max case */
6508 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6509 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6510 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6511 tribuf_calcntr = 0;
6512 }
6513 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6514
6515 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
6516 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6517 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6518 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6519
6520 /* AFC Recal */
6521 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6522 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6523 DPIO_AFC_RECAL);
6524
6525 mutex_unlock(&dev_priv->dpio_lock);
6526}
6527
6528/**
6529 * vlv_force_pll_on - forcibly enable just the PLL
6530 * @dev_priv: i915 private structure
6531 * @pipe: pipe PLL to enable
6532 * @dpll: PLL configuration
6533 *
6534 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6535 * in cases where we need the PLL enabled even when @pipe is not going to
6536 * be enabled.
6537 */
6538void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
6539 const struct dpll *dpll)
6540{
6541 struct intel_crtc *crtc =
6542 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
6543 struct intel_crtc_state pipe_config = {
6544 .base.crtc = &crtc->base,
6545 .pixel_multiplier = 1,
6546 .dpll = *dpll,
6547 };
6548
6549 if (IS_CHERRYVIEW(dev)) {
6550 chv_update_pll(crtc, &pipe_config);
6551 chv_prepare_pll(crtc, &pipe_config);
6552 chv_enable_pll(crtc, &pipe_config);
6553 } else {
6554 vlv_update_pll(crtc, &pipe_config);
6555 vlv_prepare_pll(crtc, &pipe_config);
6556 vlv_enable_pll(crtc, &pipe_config);
6557 }
6558}
6559
6560/**
6561 * vlv_force_pll_off - forcibly disable just the PLL
6562 * @dev_priv: i915 private structure
6563 * @pipe: pipe PLL to disable
6564 *
6565 * Disable the PLL for @pipe. To be used in cases where we need
6566 * the PLL enabled even when @pipe is not going to be enabled.
6567 */
6568void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6569{
6570 if (IS_CHERRYVIEW(dev))
6571 chv_disable_pll(to_i915(dev), pipe);
6572 else
6573 vlv_disable_pll(to_i915(dev), pipe);
6574}
6575
6576static void i9xx_update_pll(struct intel_crtc *crtc,
6577 struct intel_crtc_state *crtc_state,
6578 intel_clock_t *reduced_clock,
6579 int num_connectors)
6580{
6581 struct drm_device *dev = crtc->base.dev;
6582 struct drm_i915_private *dev_priv = dev->dev_private;
6583 u32 dpll;
6584 bool is_sdvo;
6585 struct dpll *clock = &crtc_state->dpll;
6586
6587 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
6588
6589 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
6590 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
6591
6592 dpll = DPLL_VGA_MODE_DIS;
6593
6594 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
6595 dpll |= DPLLB_MODE_LVDS;
6596 else
6597 dpll |= DPLLB_MODE_DAC_SERIAL;
6598
6599 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6600 dpll |= (crtc_state->pixel_multiplier - 1)
6601 << SDVO_MULTIPLIER_SHIFT_HIRES;
6602 }
6603
6604 if (is_sdvo)
6605 dpll |= DPLL_SDVO_HIGH_SPEED;
6606
6607 if (crtc_state->has_dp_encoder)
6608 dpll |= DPLL_SDVO_HIGH_SPEED;
6609
6610 /* compute bitmask from p1 value */
6611 if (IS_PINEVIEW(dev))
6612 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6613 else {
6614 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6615 if (IS_G4X(dev) && reduced_clock)
6616 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6617 }
6618 switch (clock->p2) {
6619 case 5:
6620 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6621 break;
6622 case 7:
6623 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6624 break;
6625 case 10:
6626 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6627 break;
6628 case 14:
6629 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6630 break;
6631 }
6632 if (INTEL_INFO(dev)->gen >= 4)
6633 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6634
6635 if (crtc_state->sdvo_tv_clock)
6636 dpll |= PLL_REF_INPUT_TVCLKINBC;
6637 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
6638 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6639 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6640 else
6641 dpll |= PLL_REF_INPUT_DREFCLK;
6642
6643 dpll |= DPLL_VCO_ENABLE;
6644 crtc_state->dpll_hw_state.dpll = dpll;
6645
6646 if (INTEL_INFO(dev)->gen >= 4) {
6647 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
6648 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
6649 crtc_state->dpll_hw_state.dpll_md = dpll_md;
6650 }
6651}
6652
6653static void i8xx_update_pll(struct intel_crtc *crtc,
6654 struct intel_crtc_state *crtc_state,
6655 intel_clock_t *reduced_clock,
6656 int num_connectors)
6657{
6658 struct drm_device *dev = crtc->base.dev;
6659 struct drm_i915_private *dev_priv = dev->dev_private;
6660 u32 dpll;
6661 struct dpll *clock = &crtc_state->dpll;
6662
6663 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
6664
6665 dpll = DPLL_VGA_MODE_DIS;
6666
6667 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
6668 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6669 } else {
6670 if (clock->p1 == 2)
6671 dpll |= PLL_P1_DIVIDE_BY_TWO;
6672 else
6673 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6674 if (clock->p2 == 4)
6675 dpll |= PLL_P2_DIVIDE_BY_4;
6676 }
6677
6678 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
6679 dpll |= DPLL_DVO_2X_MODE;
6680
6681 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
6682 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6683 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6684 else
6685 dpll |= PLL_REF_INPUT_DREFCLK;
6686
6687 dpll |= DPLL_VCO_ENABLE;
6688 crtc_state->dpll_hw_state.dpll = dpll;
6689}
6690
6691static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
6692{
6693 struct drm_device *dev = intel_crtc->base.dev;
6694 struct drm_i915_private *dev_priv = dev->dev_private;
6695 enum pipe pipe = intel_crtc->pipe;
6696 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
6697 struct drm_display_mode *adjusted_mode =
6698 &intel_crtc->config->base.adjusted_mode;
6699 uint32_t crtc_vtotal, crtc_vblank_end;
6700 int vsyncshift = 0;
6701
6702 /* We need to be careful not to changed the adjusted mode, for otherwise
6703 * the hw state checker will get angry at the mismatch. */
6704 crtc_vtotal = adjusted_mode->crtc_vtotal;
6705 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
6706
6707 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
6708 /* the chip adds 2 halflines automatically */
6709 crtc_vtotal -= 1;
6710 crtc_vblank_end -= 1;
6711
6712 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
6713 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6714 else
6715 vsyncshift = adjusted_mode->crtc_hsync_start -
6716 adjusted_mode->crtc_htotal / 2;
6717 if (vsyncshift < 0)
6718 vsyncshift += adjusted_mode->crtc_htotal;
6719 }
6720
6721 if (INTEL_INFO(dev)->gen > 3)
6722 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
6723
6724 I915_WRITE(HTOTAL(cpu_transcoder),
6725 (adjusted_mode->crtc_hdisplay - 1) |
6726 ((adjusted_mode->crtc_htotal - 1) << 16));
6727 I915_WRITE(HBLANK(cpu_transcoder),
6728 (adjusted_mode->crtc_hblank_start - 1) |
6729 ((adjusted_mode->crtc_hblank_end - 1) << 16));
6730 I915_WRITE(HSYNC(cpu_transcoder),
6731 (adjusted_mode->crtc_hsync_start - 1) |
6732 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6733
6734 I915_WRITE(VTOTAL(cpu_transcoder),
6735 (adjusted_mode->crtc_vdisplay - 1) |
6736 ((crtc_vtotal - 1) << 16));
6737 I915_WRITE(VBLANK(cpu_transcoder),
6738 (adjusted_mode->crtc_vblank_start - 1) |
6739 ((crtc_vblank_end - 1) << 16));
6740 I915_WRITE(VSYNC(cpu_transcoder),
6741 (adjusted_mode->crtc_vsync_start - 1) |
6742 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6743
6744 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6745 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6746 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6747 * bits. */
6748 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6749 (pipe == PIPE_B || pipe == PIPE_C))
6750 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6751
6752 /* pipesrc controls the size that is scaled from, which should
6753 * always be the user's requested size.
6754 */
6755 I915_WRITE(PIPESRC(pipe),
6756 ((intel_crtc->config->pipe_src_w - 1) << 16) |
6757 (intel_crtc->config->pipe_src_h - 1));
6758}
6759
6760static void intel_get_pipe_timings(struct intel_crtc *crtc,
6761 struct intel_crtc_state *pipe_config)
6762{
6763 struct drm_device *dev = crtc->base.dev;
6764 struct drm_i915_private *dev_priv = dev->dev_private;
6765 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6766 uint32_t tmp;
6767
6768 tmp = I915_READ(HTOTAL(cpu_transcoder));
6769 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6770 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6771 tmp = I915_READ(HBLANK(cpu_transcoder));
6772 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6773 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6774 tmp = I915_READ(HSYNC(cpu_transcoder));
6775 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6776 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6777
6778 tmp = I915_READ(VTOTAL(cpu_transcoder));
6779 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6780 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6781 tmp = I915_READ(VBLANK(cpu_transcoder));
6782 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6783 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6784 tmp = I915_READ(VSYNC(cpu_transcoder));
6785 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6786 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6787
6788 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6789 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6790 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
6791 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
6792 }
6793
6794 tmp = I915_READ(PIPESRC(crtc->pipe));
6795 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6796 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6797
6798 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
6799 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
6800}
6801
6802void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6803 struct intel_crtc_state *pipe_config)
6804{
6805 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
6806 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
6807 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
6808 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
6809
6810 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
6811 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
6812 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
6813 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
6814
6815 mode->flags = pipe_config->base.adjusted_mode.flags;
6816
6817 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
6818 mode->flags |= pipe_config->base.adjusted_mode.flags;
6819}
6820
6821static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6822{
6823 struct drm_device *dev = intel_crtc->base.dev;
6824 struct drm_i915_private *dev_priv = dev->dev_private;
6825 uint32_t pipeconf;
6826
6827 pipeconf = 0;
6828
6829 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6830 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6831 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
6832
6833 if (intel_crtc->config->double_wide)
6834 pipeconf |= PIPECONF_DOUBLE_WIDE;
6835
6836 /* only g4x and later have fancy bpc/dither controls */
6837 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6838 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6839 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
6840 pipeconf |= PIPECONF_DITHER_EN |
6841 PIPECONF_DITHER_TYPE_SP;
6842
6843 switch (intel_crtc->config->pipe_bpp) {
6844 case 18:
6845 pipeconf |= PIPECONF_6BPC;
6846 break;
6847 case 24:
6848 pipeconf |= PIPECONF_8BPC;
6849 break;
6850 case 30:
6851 pipeconf |= PIPECONF_10BPC;
6852 break;
6853 default:
6854 /* Case prevented by intel_choose_pipe_bpp_dither. */
6855 BUG();
6856 }
6857 }
6858
6859 if (HAS_PIPE_CXSR(dev)) {
6860 if (intel_crtc->lowfreq_avail) {
6861 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6862 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6863 } else {
6864 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
6865 }
6866 }
6867
6868 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6869 if (INTEL_INFO(dev)->gen < 4 ||
6870 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
6871 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6872 else
6873 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6874 } else
6875 pipeconf |= PIPECONF_PROGRESSIVE;
6876
6877 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
6878 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
6879
6880 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6881 POSTING_READ(PIPECONF(intel_crtc->pipe));
6882}
6883
6884static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
6885 struct intel_crtc_state *crtc_state)
6886{
6887 struct drm_device *dev = crtc->base.dev;
6888 struct drm_i915_private *dev_priv = dev->dev_private;
6889 int refclk, num_connectors = 0;
6890 intel_clock_t clock, reduced_clock;
6891 bool ok, has_reduced_clock = false;
6892 bool is_lvds = false, is_dsi = false;
6893 struct intel_encoder *encoder;
6894 const intel_limit_t *limit;
6895 struct drm_atomic_state *state = crtc_state->base.state;
6896 struct drm_connector_state *connector_state;
6897 int i;
6898
6899 for (i = 0; i < state->num_connector; i++) {
6900 if (!state->connectors[i])
6901 continue;
6902
6903 connector_state = state->connector_states[i];
6904 if (connector_state->crtc != &crtc->base)
6905 continue;
6906
6907 encoder = to_intel_encoder(connector_state->best_encoder);
6908
6909 switch (encoder->type) {
6910 case INTEL_OUTPUT_LVDS:
6911 is_lvds = true;
6912 break;
6913 case INTEL_OUTPUT_DSI:
6914 is_dsi = true;
6915 break;
6916 default:
6917 break;
6918 }
6919
6920 num_connectors++;
6921 }
6922
6923 if (is_dsi)
6924 return 0;
6925
6926 if (!crtc_state->clock_set) {
6927 refclk = i9xx_get_refclk(crtc_state, num_connectors);
6928
6929 /*
6930 * Returns a set of divisors for the desired target clock with
6931 * the given refclk, or FALSE. The returned values represent
6932 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6933 * 2) / p1 / p2.
6934 */
6935 limit = intel_limit(crtc_state, refclk);
6936 ok = dev_priv->display.find_dpll(limit, crtc_state,
6937 crtc_state->port_clock,
6938 refclk, NULL, &clock);
6939 if (!ok) {
6940 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6941 return -EINVAL;
6942 }
6943
6944 if (is_lvds && dev_priv->lvds_downclock_avail) {
6945 /*
6946 * Ensure we match the reduced clock's P to the target
6947 * clock. If the clocks don't match, we can't switch
6948 * the display clock by using the FP0/FP1. In such case
6949 * we will disable the LVDS downclock feature.
6950 */
6951 has_reduced_clock =
6952 dev_priv->display.find_dpll(limit, crtc_state,
6953 dev_priv->lvds_downclock,
6954 refclk, &clock,
6955 &reduced_clock);
6956 }
6957 /* Compat-code for transition, will disappear. */
6958 crtc_state->dpll.n = clock.n;
6959 crtc_state->dpll.m1 = clock.m1;
6960 crtc_state->dpll.m2 = clock.m2;
6961 crtc_state->dpll.p1 = clock.p1;
6962 crtc_state->dpll.p2 = clock.p2;
6963 }
6964
6965 if (IS_GEN2(dev)) {
6966 i8xx_update_pll(crtc, crtc_state,
6967 has_reduced_clock ? &reduced_clock : NULL,
6968 num_connectors);
6969 } else if (IS_CHERRYVIEW(dev)) {
6970 chv_update_pll(crtc, crtc_state);
6971 } else if (IS_VALLEYVIEW(dev)) {
6972 vlv_update_pll(crtc, crtc_state);
6973 } else {
6974 i9xx_update_pll(crtc, crtc_state,
6975 has_reduced_clock ? &reduced_clock : NULL,
6976 num_connectors);
6977 }
6978
6979 return 0;
6980}
6981
6982static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6983 struct intel_crtc_state *pipe_config)
6984{
6985 struct drm_device *dev = crtc->base.dev;
6986 struct drm_i915_private *dev_priv = dev->dev_private;
6987 uint32_t tmp;
6988
6989 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6990 return;
6991
6992 tmp = I915_READ(PFIT_CONTROL);
6993 if (!(tmp & PFIT_ENABLE))
6994 return;
6995
6996 /* Check whether the pfit is attached to our pipe. */
6997 if (INTEL_INFO(dev)->gen < 4) {
6998 if (crtc->pipe != PIPE_B)
6999 return;
7000 } else {
7001 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7002 return;
7003 }
7004
7005 pipe_config->gmch_pfit.control = tmp;
7006 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7007 if (INTEL_INFO(dev)->gen < 5)
7008 pipe_config->gmch_pfit.lvds_border_bits =
7009 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7010}
7011
7012static void vlv_crtc_clock_get(struct intel_crtc *crtc,
7013 struct intel_crtc_state *pipe_config)
7014{
7015 struct drm_device *dev = crtc->base.dev;
7016 struct drm_i915_private *dev_priv = dev->dev_private;
7017 int pipe = pipe_config->cpu_transcoder;
7018 intel_clock_t clock;
7019 u32 mdiv;
7020 int refclk = 100000;
7021
7022 /* In case of MIPI DPLL will not even be used */
7023 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7024 return;
7025
7026 mutex_lock(&dev_priv->dpio_lock);
7027 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
7028 mutex_unlock(&dev_priv->dpio_lock);
7029
7030 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7031 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7032 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7033 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7034 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7035
7036 vlv_clock(refclk, &clock);
7037
7038 /* clock.dot is the fast clock */
7039 pipe_config->port_clock = clock.dot / 5;
7040}
7041
7042static void
7043i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7044 struct intel_initial_plane_config *plane_config)
7045{
7046 struct drm_device *dev = crtc->base.dev;
7047 struct drm_i915_private *dev_priv = dev->dev_private;
7048 u32 val, base, offset;
7049 int pipe = crtc->pipe, plane = crtc->plane;
7050 int fourcc, pixel_format;
7051 unsigned int aligned_height;
7052 struct drm_framebuffer *fb;
7053 struct intel_framebuffer *intel_fb;
7054
7055 val = I915_READ(DSPCNTR(plane));
7056 if (!(val & DISPLAY_PLANE_ENABLE))
7057 return;
7058
7059 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7060 if (!intel_fb) {
7061 DRM_DEBUG_KMS("failed to alloc fb\n");
7062 return;
7063 }
7064
7065 fb = &intel_fb->base;
7066
7067 if (INTEL_INFO(dev)->gen >= 4) {
7068 if (val & DISPPLANE_TILED) {
7069 plane_config->tiling = I915_TILING_X;
7070 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7071 }
7072 }
7073
7074 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7075 fourcc = i9xx_format_to_fourcc(pixel_format);
7076 fb->pixel_format = fourcc;
7077 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
7078
7079 if (INTEL_INFO(dev)->gen >= 4) {
7080 if (plane_config->tiling)
7081 offset = I915_READ(DSPTILEOFF(plane));
7082 else
7083 offset = I915_READ(DSPLINOFF(plane));
7084 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7085 } else {
7086 base = I915_READ(DSPADDR(plane));
7087 }
7088 plane_config->base = base;
7089
7090 val = I915_READ(PIPESRC(pipe));
7091 fb->width = ((val >> 16) & 0xfff) + 1;
7092 fb->height = ((val >> 0) & 0xfff) + 1;
7093
7094 val = I915_READ(DSPSTRIDE(pipe));
7095 fb->pitches[0] = val & 0xffffffc0;
7096
7097 aligned_height = intel_fb_align_height(dev, fb->height,
7098 fb->pixel_format,
7099 fb->modifier[0]);
7100
7101 plane_config->size = fb->pitches[0] * aligned_height;
7102
7103 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7104 pipe_name(pipe), plane, fb->width, fb->height,
7105 fb->bits_per_pixel, base, fb->pitches[0],
7106 plane_config->size);
7107
7108 plane_config->fb = intel_fb;
7109}
7110
7111static void chv_crtc_clock_get(struct intel_crtc *crtc,
7112 struct intel_crtc_state *pipe_config)
7113{
7114 struct drm_device *dev = crtc->base.dev;
7115 struct drm_i915_private *dev_priv = dev->dev_private;
7116 int pipe = pipe_config->cpu_transcoder;
7117 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7118 intel_clock_t clock;
7119 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7120 int refclk = 100000;
7121
7122 mutex_lock(&dev_priv->dpio_lock);
7123 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7124 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7125 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7126 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7127 mutex_unlock(&dev_priv->dpio_lock);
7128
7129 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7130 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7131 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7132 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7133 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7134
7135 chv_clock(refclk, &clock);
7136
7137 /* clock.dot is the fast clock */
7138 pipe_config->port_clock = clock.dot / 5;
7139}
7140
7141static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
7142 struct intel_crtc_state *pipe_config)
7143{
7144 struct drm_device *dev = crtc->base.dev;
7145 struct drm_i915_private *dev_priv = dev->dev_private;
7146 uint32_t tmp;
7147
7148 if (!intel_display_power_is_enabled(dev_priv,
7149 POWER_DOMAIN_PIPE(crtc->pipe)))
7150 return false;
7151
7152 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
7153 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7154
7155 tmp = I915_READ(PIPECONF(crtc->pipe));
7156 if (!(tmp & PIPECONF_ENABLE))
7157 return false;
7158
7159 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7160 switch (tmp & PIPECONF_BPC_MASK) {
7161 case PIPECONF_6BPC:
7162 pipe_config->pipe_bpp = 18;
7163 break;
7164 case PIPECONF_8BPC:
7165 pipe_config->pipe_bpp = 24;
7166 break;
7167 case PIPECONF_10BPC:
7168 pipe_config->pipe_bpp = 30;
7169 break;
7170 default:
7171 break;
7172 }
7173 }
7174
7175 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7176 pipe_config->limited_color_range = true;
7177
7178 if (INTEL_INFO(dev)->gen < 4)
7179 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7180
7181 intel_get_pipe_timings(crtc, pipe_config);
7182
7183 i9xx_get_pfit_config(crtc, pipe_config);
7184
7185 if (INTEL_INFO(dev)->gen >= 4) {
7186 tmp = I915_READ(DPLL_MD(crtc->pipe));
7187 pipe_config->pixel_multiplier =
7188 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7189 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
7190 pipe_config->dpll_hw_state.dpll_md = tmp;
7191 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7192 tmp = I915_READ(DPLL(crtc->pipe));
7193 pipe_config->pixel_multiplier =
7194 ((tmp & SDVO_MULTIPLIER_MASK)
7195 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7196 } else {
7197 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7198 * port and will be fixed up in the encoder->get_config
7199 * function. */
7200 pipe_config->pixel_multiplier = 1;
7201 }
7202 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7203 if (!IS_VALLEYVIEW(dev)) {
7204 /*
7205 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7206 * on 830. Filter it out here so that we don't
7207 * report errors due to that.
7208 */
7209 if (IS_I830(dev))
7210 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7211
7212 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7213 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
7214 } else {
7215 /* Mask out read-only status bits. */
7216 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7217 DPLL_PORTC_READY_MASK |
7218 DPLL_PORTB_READY_MASK);
7219 }
7220
7221 if (IS_CHERRYVIEW(dev))
7222 chv_crtc_clock_get(crtc, pipe_config);
7223 else if (IS_VALLEYVIEW(dev))
7224 vlv_crtc_clock_get(crtc, pipe_config);
7225 else
7226 i9xx_crtc_clock_get(crtc, pipe_config);
7227
7228 return true;
7229}
7230
7231static void ironlake_init_pch_refclk(struct drm_device *dev)
7232{
7233 struct drm_i915_private *dev_priv = dev->dev_private;
7234 struct intel_encoder *encoder;
7235 u32 val, final;
7236 bool has_lvds = false;
7237 bool has_cpu_edp = false;
7238 bool has_panel = false;
7239 bool has_ck505 = false;
7240 bool can_ssc = false;
7241
7242 /* We need to take the global config into account */
7243 for_each_intel_encoder(dev, encoder) {
7244 switch (encoder->type) {
7245 case INTEL_OUTPUT_LVDS:
7246 has_panel = true;
7247 has_lvds = true;
7248 break;
7249 case INTEL_OUTPUT_EDP:
7250 has_panel = true;
7251 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
7252 has_cpu_edp = true;
7253 break;
7254 default:
7255 break;
7256 }
7257 }
7258
7259 if (HAS_PCH_IBX(dev)) {
7260 has_ck505 = dev_priv->vbt.display_clock_mode;
7261 can_ssc = has_ck505;
7262 } else {
7263 has_ck505 = false;
7264 can_ssc = true;
7265 }
7266
7267 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7268 has_panel, has_lvds, has_ck505);
7269
7270 /* Ironlake: try to setup display ref clock before DPLL
7271 * enabling. This is only under driver's control after
7272 * PCH B stepping, previous chipset stepping should be
7273 * ignoring this setting.
7274 */
7275 val = I915_READ(PCH_DREF_CONTROL);
7276
7277 /* As we must carefully and slowly disable/enable each source in turn,
7278 * compute the final state we want first and check if we need to
7279 * make any changes at all.
7280 */
7281 final = val;
7282 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7283 if (has_ck505)
7284 final |= DREF_NONSPREAD_CK505_ENABLE;
7285 else
7286 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7287
7288 final &= ~DREF_SSC_SOURCE_MASK;
7289 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7290 final &= ~DREF_SSC1_ENABLE;
7291
7292 if (has_panel) {
7293 final |= DREF_SSC_SOURCE_ENABLE;
7294
7295 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7296 final |= DREF_SSC1_ENABLE;
7297
7298 if (has_cpu_edp) {
7299 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7300 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7301 else
7302 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7303 } else
7304 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7305 } else {
7306 final |= DREF_SSC_SOURCE_DISABLE;
7307 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7308 }
7309
7310 if (final == val)
7311 return;
7312
7313 /* Always enable nonspread source */
7314 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7315
7316 if (has_ck505)
7317 val |= DREF_NONSPREAD_CK505_ENABLE;
7318 else
7319 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7320
7321 if (has_panel) {
7322 val &= ~DREF_SSC_SOURCE_MASK;
7323 val |= DREF_SSC_SOURCE_ENABLE;
7324
7325 /* SSC must be turned on before enabling the CPU output */
7326 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
7327 DRM_DEBUG_KMS("Using SSC on panel\n");
7328 val |= DREF_SSC1_ENABLE;
7329 } else
7330 val &= ~DREF_SSC1_ENABLE;
7331
7332 /* Get SSC going before enabling the outputs */
7333 I915_WRITE(PCH_DREF_CONTROL, val);
7334 POSTING_READ(PCH_DREF_CONTROL);
7335 udelay(200);
7336
7337 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7338
7339 /* Enable CPU source on CPU attached eDP */
7340 if (has_cpu_edp) {
7341 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
7342 DRM_DEBUG_KMS("Using SSC on eDP\n");
7343 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7344 } else
7345 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7346 } else
7347 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7348
7349 I915_WRITE(PCH_DREF_CONTROL, val);
7350 POSTING_READ(PCH_DREF_CONTROL);
7351 udelay(200);
7352 } else {
7353 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7354
7355 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7356
7357 /* Turn off CPU output */
7358 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7359
7360 I915_WRITE(PCH_DREF_CONTROL, val);
7361 POSTING_READ(PCH_DREF_CONTROL);
7362 udelay(200);
7363
7364 /* Turn off the SSC source */
7365 val &= ~DREF_SSC_SOURCE_MASK;
7366 val |= DREF_SSC_SOURCE_DISABLE;
7367
7368 /* Turn off SSC1 */
7369 val &= ~DREF_SSC1_ENABLE;
7370
7371 I915_WRITE(PCH_DREF_CONTROL, val);
7372 POSTING_READ(PCH_DREF_CONTROL);
7373 udelay(200);
7374 }
7375
7376 BUG_ON(val != final);
7377}
7378
7379static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
7380{
7381 uint32_t tmp;
7382
7383 tmp = I915_READ(SOUTH_CHICKEN2);
7384 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7385 I915_WRITE(SOUTH_CHICKEN2, tmp);
7386
7387 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7388 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7389 DRM_ERROR("FDI mPHY reset assert timeout\n");
7390
7391 tmp = I915_READ(SOUTH_CHICKEN2);
7392 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7393 I915_WRITE(SOUTH_CHICKEN2, tmp);
7394
7395 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7396 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7397 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
7398}
7399
7400/* WaMPhyProgramming:hsw */
7401static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7402{
7403 uint32_t tmp;
7404
7405 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7406 tmp &= ~(0xFF << 24);
7407 tmp |= (0x12 << 24);
7408 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7409
7410 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7411 tmp |= (1 << 11);
7412 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7413
7414 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7415 tmp |= (1 << 11);
7416 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7417
7418 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7419 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7420 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7421
7422 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7423 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7424 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7425
7426 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7427 tmp &= ~(7 << 13);
7428 tmp |= (5 << 13);
7429 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
7430
7431 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7432 tmp &= ~(7 << 13);
7433 tmp |= (5 << 13);
7434 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
7435
7436 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7437 tmp &= ~0xFF;
7438 tmp |= 0x1C;
7439 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7440
7441 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7442 tmp &= ~0xFF;
7443 tmp |= 0x1C;
7444 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7445
7446 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7447 tmp &= ~(0xFF << 16);
7448 tmp |= (0x1C << 16);
7449 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7450
7451 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7452 tmp &= ~(0xFF << 16);
7453 tmp |= (0x1C << 16);
7454 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7455
7456 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7457 tmp |= (1 << 27);
7458 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
7459
7460 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7461 tmp |= (1 << 27);
7462 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
7463
7464 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7465 tmp &= ~(0xF << 28);
7466 tmp |= (4 << 28);
7467 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
7468
7469 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7470 tmp &= ~(0xF << 28);
7471 tmp |= (4 << 28);
7472 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
7473}
7474
7475/* Implements 3 different sequences from BSpec chapter "Display iCLK
7476 * Programming" based on the parameters passed:
7477 * - Sequence to enable CLKOUT_DP
7478 * - Sequence to enable CLKOUT_DP without spread
7479 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7480 */
7481static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7482 bool with_fdi)
7483{
7484 struct drm_i915_private *dev_priv = dev->dev_private;
7485 uint32_t reg, tmp;
7486
7487 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7488 with_spread = true;
7489 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7490 with_fdi, "LP PCH doesn't have FDI\n"))
7491 with_fdi = false;
7492
7493 mutex_lock(&dev_priv->dpio_lock);
7494
7495 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7496 tmp &= ~SBI_SSCCTL_DISABLE;
7497 tmp |= SBI_SSCCTL_PATHALT;
7498 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7499
7500 udelay(24);
7501
7502 if (with_spread) {
7503 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7504 tmp &= ~SBI_SSCCTL_PATHALT;
7505 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7506
7507 if (with_fdi) {
7508 lpt_reset_fdi_mphy(dev_priv);
7509 lpt_program_fdi_mphy(dev_priv);
7510 }
7511 }
7512
7513 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7514 SBI_GEN0 : SBI_DBUFF0;
7515 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7516 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7517 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7518
7519 mutex_unlock(&dev_priv->dpio_lock);
7520}
7521
7522/* Sequence to disable CLKOUT_DP */
7523static void lpt_disable_clkout_dp(struct drm_device *dev)
7524{
7525 struct drm_i915_private *dev_priv = dev->dev_private;
7526 uint32_t reg, tmp;
7527
7528 mutex_lock(&dev_priv->dpio_lock);
7529
7530 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7531 SBI_GEN0 : SBI_DBUFF0;
7532 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7533 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7534 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7535
7536 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7537 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7538 if (!(tmp & SBI_SSCCTL_PATHALT)) {
7539 tmp |= SBI_SSCCTL_PATHALT;
7540 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7541 udelay(32);
7542 }
7543 tmp |= SBI_SSCCTL_DISABLE;
7544 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7545 }
7546
7547 mutex_unlock(&dev_priv->dpio_lock);
7548}
7549
7550static void lpt_init_pch_refclk(struct drm_device *dev)
7551{
7552 struct intel_encoder *encoder;
7553 bool has_vga = false;
7554
7555 for_each_intel_encoder(dev, encoder) {
7556 switch (encoder->type) {
7557 case INTEL_OUTPUT_ANALOG:
7558 has_vga = true;
7559 break;
7560 default:
7561 break;
7562 }
7563 }
7564
7565 if (has_vga)
7566 lpt_enable_clkout_dp(dev, true, true);
7567 else
7568 lpt_disable_clkout_dp(dev);
7569}
7570
7571/*
7572 * Initialize reference clocks when the driver loads
7573 */
7574void intel_init_pch_refclk(struct drm_device *dev)
7575{
7576 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7577 ironlake_init_pch_refclk(dev);
7578 else if (HAS_PCH_LPT(dev))
7579 lpt_init_pch_refclk(dev);
7580}
7581
7582static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
7583{
7584 struct drm_device *dev = crtc_state->base.crtc->dev;
7585 struct drm_i915_private *dev_priv = dev->dev_private;
7586 struct drm_atomic_state *state = crtc_state->base.state;
7587 struct drm_connector_state *connector_state;
7588 struct intel_encoder *encoder;
7589 int num_connectors = 0, i;
7590 bool is_lvds = false;
7591
7592 for (i = 0; i < state->num_connector; i++) {
7593 if (!state->connectors[i])
7594 continue;
7595
7596 connector_state = state->connector_states[i];
7597 if (connector_state->crtc != crtc_state->base.crtc)
7598 continue;
7599
7600 encoder = to_intel_encoder(connector_state->best_encoder);
7601
7602 switch (encoder->type) {
7603 case INTEL_OUTPUT_LVDS:
7604 is_lvds = true;
7605 break;
7606 default:
7607 break;
7608 }
7609 num_connectors++;
7610 }
7611
7612 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
7613 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
7614 dev_priv->vbt.lvds_ssc_freq);
7615 return dev_priv->vbt.lvds_ssc_freq;
7616 }
7617
7618 return 120000;
7619}
7620
7621static void ironlake_set_pipeconf(struct drm_crtc *crtc)
7622{
7623 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7625 int pipe = intel_crtc->pipe;
7626 uint32_t val;
7627
7628 val = 0;
7629
7630 switch (intel_crtc->config->pipe_bpp) {
7631 case 18:
7632 val |= PIPECONF_6BPC;
7633 break;
7634 case 24:
7635 val |= PIPECONF_8BPC;
7636 break;
7637 case 30:
7638 val |= PIPECONF_10BPC;
7639 break;
7640 case 36:
7641 val |= PIPECONF_12BPC;
7642 break;
7643 default:
7644 /* Case prevented by intel_choose_pipe_bpp_dither. */
7645 BUG();
7646 }
7647
7648 if (intel_crtc->config->dither)
7649 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7650
7651 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
7652 val |= PIPECONF_INTERLACED_ILK;
7653 else
7654 val |= PIPECONF_PROGRESSIVE;
7655
7656 if (intel_crtc->config->limited_color_range)
7657 val |= PIPECONF_COLOR_RANGE_SELECT;
7658
7659 I915_WRITE(PIPECONF(pipe), val);
7660 POSTING_READ(PIPECONF(pipe));
7661}
7662
7663/*
7664 * Set up the pipe CSC unit.
7665 *
7666 * Currently only full range RGB to limited range RGB conversion
7667 * is supported, but eventually this should handle various
7668 * RGB<->YCbCr scenarios as well.
7669 */
7670static void intel_set_pipe_csc(struct drm_crtc *crtc)
7671{
7672 struct drm_device *dev = crtc->dev;
7673 struct drm_i915_private *dev_priv = dev->dev_private;
7674 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7675 int pipe = intel_crtc->pipe;
7676 uint16_t coeff = 0x7800; /* 1.0 */
7677
7678 /*
7679 * TODO: Check what kind of values actually come out of the pipe
7680 * with these coeff/postoff values and adjust to get the best
7681 * accuracy. Perhaps we even need to take the bpc value into
7682 * consideration.
7683 */
7684
7685 if (intel_crtc->config->limited_color_range)
7686 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7687
7688 /*
7689 * GY/GU and RY/RU should be the other way around according
7690 * to BSpec, but reality doesn't agree. Just set them up in
7691 * a way that results in the correct picture.
7692 */
7693 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7694 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7695
7696 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7697 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7698
7699 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7700 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7701
7702 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7703 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7704 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7705
7706 if (INTEL_INFO(dev)->gen > 6) {
7707 uint16_t postoff = 0;
7708
7709 if (intel_crtc->config->limited_color_range)
7710 postoff = (16 * (1 << 12) / 255) & 0x1fff;
7711
7712 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7713 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7714 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7715
7716 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7717 } else {
7718 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7719
7720 if (intel_crtc->config->limited_color_range)
7721 mode |= CSC_BLACK_SCREEN_OFFSET;
7722
7723 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7724 }
7725}
7726
7727static void haswell_set_pipeconf(struct drm_crtc *crtc)
7728{
7729 struct drm_device *dev = crtc->dev;
7730 struct drm_i915_private *dev_priv = dev->dev_private;
7731 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7732 enum pipe pipe = intel_crtc->pipe;
7733 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
7734 uint32_t val;
7735
7736 val = 0;
7737
7738 if (IS_HASWELL(dev) && intel_crtc->config->dither)
7739 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7740
7741 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
7742 val |= PIPECONF_INTERLACED_ILK;
7743 else
7744 val |= PIPECONF_PROGRESSIVE;
7745
7746 I915_WRITE(PIPECONF(cpu_transcoder), val);
7747 POSTING_READ(PIPECONF(cpu_transcoder));
7748
7749 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7750 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
7751
7752 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
7753 val = 0;
7754
7755 switch (intel_crtc->config->pipe_bpp) {
7756 case 18:
7757 val |= PIPEMISC_DITHER_6_BPC;
7758 break;
7759 case 24:
7760 val |= PIPEMISC_DITHER_8_BPC;
7761 break;
7762 case 30:
7763 val |= PIPEMISC_DITHER_10_BPC;
7764 break;
7765 case 36:
7766 val |= PIPEMISC_DITHER_12_BPC;
7767 break;
7768 default:
7769 /* Case prevented by pipe_config_set_bpp. */
7770 BUG();
7771 }
7772
7773 if (intel_crtc->config->dither)
7774 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7775
7776 I915_WRITE(PIPEMISC(pipe), val);
7777 }
7778}
7779
7780static bool ironlake_compute_clocks(struct drm_crtc *crtc,
7781 struct intel_crtc_state *crtc_state,
7782 intel_clock_t *clock,
7783 bool *has_reduced_clock,
7784 intel_clock_t *reduced_clock)
7785{
7786 struct drm_device *dev = crtc->dev;
7787 struct drm_i915_private *dev_priv = dev->dev_private;
7788 int refclk;
7789 const intel_limit_t *limit;
7790 bool ret, is_lvds = false;
7791
7792 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
7793
7794 refclk = ironlake_get_refclk(crtc_state);
7795
7796 /*
7797 * Returns a set of divisors for the desired target clock with the given
7798 * refclk, or FALSE. The returned values represent the clock equation:
7799 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7800 */
7801 limit = intel_limit(crtc_state, refclk);
7802 ret = dev_priv->display.find_dpll(limit, crtc_state,
7803 crtc_state->port_clock,
7804 refclk, NULL, clock);
7805 if (!ret)
7806 return false;
7807
7808 if (is_lvds && dev_priv->lvds_downclock_avail) {
7809 /*
7810 * Ensure we match the reduced clock's P to the target clock.
7811 * If the clocks don't match, we can't switch the display clock
7812 * by using the FP0/FP1. In such case we will disable the LVDS
7813 * downclock feature.
7814 */
7815 *has_reduced_clock =
7816 dev_priv->display.find_dpll(limit, crtc_state,
7817 dev_priv->lvds_downclock,
7818 refclk, clock,
7819 reduced_clock);
7820 }
7821
7822 return true;
7823}
7824
7825int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7826{
7827 /*
7828 * Account for spread spectrum to avoid
7829 * oversubscribing the link. Max center spread
7830 * is 2.5%; use 5% for safety's sake.
7831 */
7832 u32 bps = target_clock * bpp * 21 / 20;
7833 return DIV_ROUND_UP(bps, link_bw * 8);
7834}
7835
7836static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
7837{
7838 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
7839}
7840
7841static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7842 struct intel_crtc_state *crtc_state,
7843 u32 *fp,
7844 intel_clock_t *reduced_clock, u32 *fp2)
7845{
7846 struct drm_crtc *crtc = &intel_crtc->base;
7847 struct drm_device *dev = crtc->dev;
7848 struct drm_i915_private *dev_priv = dev->dev_private;
7849 struct drm_atomic_state *state = crtc_state->base.state;
7850 struct drm_connector_state *connector_state;
7851 struct intel_encoder *encoder;
7852 uint32_t dpll;
7853 int factor, num_connectors = 0, i;
7854 bool is_lvds = false, is_sdvo = false;
7855
7856 for (i = 0; i < state->num_connector; i++) {
7857 if (!state->connectors[i])
7858 continue;
7859
7860 connector_state = state->connector_states[i];
7861 if (connector_state->crtc != crtc_state->base.crtc)
7862 continue;
7863
7864 encoder = to_intel_encoder(connector_state->best_encoder);
7865
7866 switch (encoder->type) {
7867 case INTEL_OUTPUT_LVDS:
7868 is_lvds = true;
7869 break;
7870 case INTEL_OUTPUT_SDVO:
7871 case INTEL_OUTPUT_HDMI:
7872 is_sdvo = true;
7873 break;
7874 default:
7875 break;
7876 }
7877
7878 num_connectors++;
7879 }
7880
7881 /* Enable autotuning of the PLL clock (if permissible) */
7882 factor = 21;
7883 if (is_lvds) {
7884 if ((intel_panel_use_ssc(dev_priv) &&
7885 dev_priv->vbt.lvds_ssc_freq == 100000) ||
7886 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
7887 factor = 25;
7888 } else if (crtc_state->sdvo_tv_clock)
7889 factor = 20;
7890
7891 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7892 *fp |= FP_CB_TUNE;
7893
7894 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7895 *fp2 |= FP_CB_TUNE;
7896
7897 dpll = 0;
7898
7899 if (is_lvds)
7900 dpll |= DPLLB_MODE_LVDS;
7901 else
7902 dpll |= DPLLB_MODE_DAC_SERIAL;
7903
7904 dpll |= (crtc_state->pixel_multiplier - 1)
7905 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
7906
7907 if (is_sdvo)
7908 dpll |= DPLL_SDVO_HIGH_SPEED;
7909 if (crtc_state->has_dp_encoder)
7910 dpll |= DPLL_SDVO_HIGH_SPEED;
7911
7912 /* compute bitmask from p1 value */
7913 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7914 /* also FPA1 */
7915 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7916
7917 switch (crtc_state->dpll.p2) {
7918 case 5:
7919 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7920 break;
7921 case 7:
7922 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7923 break;
7924 case 10:
7925 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7926 break;
7927 case 14:
7928 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7929 break;
7930 }
7931
7932 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7933 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7934 else
7935 dpll |= PLL_REF_INPUT_DREFCLK;
7936
7937 return dpll | DPLL_VCO_ENABLE;
7938}
7939
7940static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
7941 struct intel_crtc_state *crtc_state)
7942{
7943 struct drm_device *dev = crtc->base.dev;
7944 intel_clock_t clock, reduced_clock;
7945 u32 dpll = 0, fp = 0, fp2 = 0;
7946 bool ok, has_reduced_clock = false;
7947 bool is_lvds = false;
7948 struct intel_shared_dpll *pll;
7949
7950 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
7951
7952 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7953 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7954
7955 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
7956 &has_reduced_clock, &reduced_clock);
7957 if (!ok && !crtc_state->clock_set) {
7958 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7959 return -EINVAL;
7960 }
7961 /* Compat-code for transition, will disappear. */
7962 if (!crtc_state->clock_set) {
7963 crtc_state->dpll.n = clock.n;
7964 crtc_state->dpll.m1 = clock.m1;
7965 crtc_state->dpll.m2 = clock.m2;
7966 crtc_state->dpll.p1 = clock.p1;
7967 crtc_state->dpll.p2 = clock.p2;
7968 }
7969
7970 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
7971 if (crtc_state->has_pch_encoder) {
7972 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
7973 if (has_reduced_clock)
7974 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
7975
7976 dpll = ironlake_compute_dpll(crtc, crtc_state,
7977 &fp, &reduced_clock,
7978 has_reduced_clock ? &fp2 : NULL);
7979
7980 crtc_state->dpll_hw_state.dpll = dpll;
7981 crtc_state->dpll_hw_state.fp0 = fp;
7982 if (has_reduced_clock)
7983 crtc_state->dpll_hw_state.fp1 = fp2;
7984 else
7985 crtc_state->dpll_hw_state.fp1 = fp;
7986
7987 pll = intel_get_shared_dpll(crtc, crtc_state);
7988 if (pll == NULL) {
7989 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
7990 pipe_name(crtc->pipe));
7991 return -EINVAL;
7992 }
7993 }
7994
7995 if (is_lvds && has_reduced_clock)
7996 crtc->lowfreq_avail = true;
7997 else
7998 crtc->lowfreq_avail = false;
7999
8000 return 0;
8001}
8002
8003static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8004 struct intel_link_m_n *m_n)
8005{
8006 struct drm_device *dev = crtc->base.dev;
8007 struct drm_i915_private *dev_priv = dev->dev_private;
8008 enum pipe pipe = crtc->pipe;
8009
8010 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8011 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8012 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8013 & ~TU_SIZE_MASK;
8014 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8015 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8016 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8017}
8018
8019static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8020 enum transcoder transcoder,
8021 struct intel_link_m_n *m_n,
8022 struct intel_link_m_n *m2_n2)
8023{
8024 struct drm_device *dev = crtc->base.dev;
8025 struct drm_i915_private *dev_priv = dev->dev_private;
8026 enum pipe pipe = crtc->pipe;
8027
8028 if (INTEL_INFO(dev)->gen >= 5) {
8029 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8030 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8031 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8032 & ~TU_SIZE_MASK;
8033 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8034 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8035 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8036 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8037 * gen < 8) and if DRRS is supported (to make sure the
8038 * registers are not unnecessarily read).
8039 */
8040 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
8041 crtc->config->has_drrs) {
8042 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8043 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8044 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8045 & ~TU_SIZE_MASK;
8046 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8047 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8048 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8049 }
8050 } else {
8051 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8052 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8053 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8054 & ~TU_SIZE_MASK;
8055 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8056 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8057 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8058 }
8059}
8060
8061void intel_dp_get_m_n(struct intel_crtc *crtc,
8062 struct intel_crtc_state *pipe_config)
8063{
8064 if (pipe_config->has_pch_encoder)
8065 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8066 else
8067 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
8068 &pipe_config->dp_m_n,
8069 &pipe_config->dp_m2_n2);
8070}
8071
8072static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
8073 struct intel_crtc_state *pipe_config)
8074{
8075 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
8076 &pipe_config->fdi_m_n, NULL);
8077}
8078
8079static void skylake_get_pfit_config(struct intel_crtc *crtc,
8080 struct intel_crtc_state *pipe_config)
8081{
8082 struct drm_device *dev = crtc->base.dev;
8083 struct drm_i915_private *dev_priv = dev->dev_private;
8084 uint32_t tmp;
8085
8086 tmp = I915_READ(PS_CTL(crtc->pipe));
8087
8088 if (tmp & PS_ENABLE) {
8089 pipe_config->pch_pfit.enabled = true;
8090 pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
8091 pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
8092 }
8093}
8094
8095static void
8096skylake_get_initial_plane_config(struct intel_crtc *crtc,
8097 struct intel_initial_plane_config *plane_config)
8098{
8099 struct drm_device *dev = crtc->base.dev;
8100 struct drm_i915_private *dev_priv = dev->dev_private;
8101 u32 val, base, offset, stride_mult, tiling;
8102 int pipe = crtc->pipe;
8103 int fourcc, pixel_format;
8104 unsigned int aligned_height;
8105 struct drm_framebuffer *fb;
8106 struct intel_framebuffer *intel_fb;
8107
8108 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8109 if (!intel_fb) {
8110 DRM_DEBUG_KMS("failed to alloc fb\n");
8111 return;
8112 }
8113
8114 fb = &intel_fb->base;
8115
8116 val = I915_READ(PLANE_CTL(pipe, 0));
8117 if (!(val & PLANE_CTL_ENABLE))
8118 goto error;
8119
8120 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8121 fourcc = skl_format_to_fourcc(pixel_format,
8122 val & PLANE_CTL_ORDER_RGBX,
8123 val & PLANE_CTL_ALPHA_MASK);
8124 fb->pixel_format = fourcc;
8125 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8126
8127 tiling = val & PLANE_CTL_TILED_MASK;
8128 switch (tiling) {
8129 case PLANE_CTL_TILED_LINEAR:
8130 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8131 break;
8132 case PLANE_CTL_TILED_X:
8133 plane_config->tiling = I915_TILING_X;
8134 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8135 break;
8136 case PLANE_CTL_TILED_Y:
8137 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8138 break;
8139 case PLANE_CTL_TILED_YF:
8140 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8141 break;
8142 default:
8143 MISSING_CASE(tiling);
8144 goto error;
8145 }
8146
8147 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8148 plane_config->base = base;
8149
8150 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8151
8152 val = I915_READ(PLANE_SIZE(pipe, 0));
8153 fb->height = ((val >> 16) & 0xfff) + 1;
8154 fb->width = ((val >> 0) & 0x1fff) + 1;
8155
8156 val = I915_READ(PLANE_STRIDE(pipe, 0));
8157 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8158 fb->pixel_format);
8159 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8160
8161 aligned_height = intel_fb_align_height(dev, fb->height,
8162 fb->pixel_format,
8163 fb->modifier[0]);
8164
8165 plane_config->size = fb->pitches[0] * aligned_height;
8166
8167 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8168 pipe_name(pipe), fb->width, fb->height,
8169 fb->bits_per_pixel, base, fb->pitches[0],
8170 plane_config->size);
8171
8172 plane_config->fb = intel_fb;
8173 return;
8174
8175error:
8176 kfree(fb);
8177}
8178
8179static void ironlake_get_pfit_config(struct intel_crtc *crtc,
8180 struct intel_crtc_state *pipe_config)
8181{
8182 struct drm_device *dev = crtc->base.dev;
8183 struct drm_i915_private *dev_priv = dev->dev_private;
8184 uint32_t tmp;
8185
8186 tmp = I915_READ(PF_CTL(crtc->pipe));
8187
8188 if (tmp & PF_ENABLE) {
8189 pipe_config->pch_pfit.enabled = true;
8190 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8191 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
8192
8193 /* We currently do not free assignements of panel fitters on
8194 * ivb/hsw (since we don't use the higher upscaling modes which
8195 * differentiates them) so just WARN about this case for now. */
8196 if (IS_GEN7(dev)) {
8197 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8198 PF_PIPE_SEL_IVB(crtc->pipe));
8199 }
8200 }
8201}
8202
8203static void
8204ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8205 struct intel_initial_plane_config *plane_config)
8206{
8207 struct drm_device *dev = crtc->base.dev;
8208 struct drm_i915_private *dev_priv = dev->dev_private;
8209 u32 val, base, offset;
8210 int pipe = crtc->pipe;
8211 int fourcc, pixel_format;
8212 unsigned int aligned_height;
8213 struct drm_framebuffer *fb;
8214 struct intel_framebuffer *intel_fb;
8215
8216 val = I915_READ(DSPCNTR(pipe));
8217 if (!(val & DISPLAY_PLANE_ENABLE))
8218 return;
8219
8220 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8221 if (!intel_fb) {
8222 DRM_DEBUG_KMS("failed to alloc fb\n");
8223 return;
8224 }
8225
8226 fb = &intel_fb->base;
8227
8228 if (INTEL_INFO(dev)->gen >= 4) {
8229 if (val & DISPPLANE_TILED) {
8230 plane_config->tiling = I915_TILING_X;
8231 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8232 }
8233 }
8234
8235 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
8236 fourcc = i9xx_format_to_fourcc(pixel_format);
8237 fb->pixel_format = fourcc;
8238 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8239
8240 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
8241 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
8242 offset = I915_READ(DSPOFFSET(pipe));
8243 } else {
8244 if (plane_config->tiling)
8245 offset = I915_READ(DSPTILEOFF(pipe));
8246 else
8247 offset = I915_READ(DSPLINOFF(pipe));
8248 }
8249 plane_config->base = base;
8250
8251 val = I915_READ(PIPESRC(pipe));
8252 fb->width = ((val >> 16) & 0xfff) + 1;
8253 fb->height = ((val >> 0) & 0xfff) + 1;
8254
8255 val = I915_READ(DSPSTRIDE(pipe));
8256 fb->pitches[0] = val & 0xffffffc0;
8257
8258 aligned_height = intel_fb_align_height(dev, fb->height,
8259 fb->pixel_format,
8260 fb->modifier[0]);
8261
8262 plane_config->size = fb->pitches[0] * aligned_height;
8263
8264 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8265 pipe_name(pipe), fb->width, fb->height,
8266 fb->bits_per_pixel, base, fb->pitches[0],
8267 plane_config->size);
8268
8269 plane_config->fb = intel_fb;
8270}
8271
8272static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
8273 struct intel_crtc_state *pipe_config)
8274{
8275 struct drm_device *dev = crtc->base.dev;
8276 struct drm_i915_private *dev_priv = dev->dev_private;
8277 uint32_t tmp;
8278
8279 if (!intel_display_power_is_enabled(dev_priv,
8280 POWER_DOMAIN_PIPE(crtc->pipe)))
8281 return false;
8282
8283 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8284 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8285
8286 tmp = I915_READ(PIPECONF(crtc->pipe));
8287 if (!(tmp & PIPECONF_ENABLE))
8288 return false;
8289
8290 switch (tmp & PIPECONF_BPC_MASK) {
8291 case PIPECONF_6BPC:
8292 pipe_config->pipe_bpp = 18;
8293 break;
8294 case PIPECONF_8BPC:
8295 pipe_config->pipe_bpp = 24;
8296 break;
8297 case PIPECONF_10BPC:
8298 pipe_config->pipe_bpp = 30;
8299 break;
8300 case PIPECONF_12BPC:
8301 pipe_config->pipe_bpp = 36;
8302 break;
8303 default:
8304 break;
8305 }
8306
8307 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8308 pipe_config->limited_color_range = true;
8309
8310 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
8311 struct intel_shared_dpll *pll;
8312
8313 pipe_config->has_pch_encoder = true;
8314
8315 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8316 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8317 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8318
8319 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8320
8321 if (HAS_PCH_IBX(dev_priv->dev)) {
8322 pipe_config->shared_dpll =
8323 (enum intel_dpll_id) crtc->pipe;
8324 } else {
8325 tmp = I915_READ(PCH_DPLL_SEL);
8326 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8327 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8328 else
8329 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8330 }
8331
8332 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8333
8334 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8335 &pipe_config->dpll_hw_state));
8336
8337 tmp = pipe_config->dpll_hw_state.dpll;
8338 pipe_config->pixel_multiplier =
8339 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8340 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
8341
8342 ironlake_pch_clock_get(crtc, pipe_config);
8343 } else {
8344 pipe_config->pixel_multiplier = 1;
8345 }
8346
8347 intel_get_pipe_timings(crtc, pipe_config);
8348
8349 ironlake_get_pfit_config(crtc, pipe_config);
8350
8351 return true;
8352}
8353
8354static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8355{
8356 struct drm_device *dev = dev_priv->dev;
8357 struct intel_crtc *crtc;
8358
8359 for_each_intel_crtc(dev, crtc)
8360 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
8361 pipe_name(crtc->pipe));
8362
8363 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8364 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8365 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8366 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8367 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8368 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
8369 "CPU PWM1 enabled\n");
8370 if (IS_HASWELL(dev))
8371 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
8372 "CPU PWM2 enabled\n");
8373 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
8374 "PCH PWM1 enabled\n");
8375 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
8376 "Utility pin enabled\n");
8377 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
8378
8379 /*
8380 * In theory we can still leave IRQs enabled, as long as only the HPD
8381 * interrupts remain enabled. We used to check for that, but since it's
8382 * gen-specific and since we only disable LCPLL after we fully disable
8383 * the interrupts, the check below should be enough.
8384 */
8385 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
8386}
8387
8388static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8389{
8390 struct drm_device *dev = dev_priv->dev;
8391
8392 if (IS_HASWELL(dev))
8393 return I915_READ(D_COMP_HSW);
8394 else
8395 return I915_READ(D_COMP_BDW);
8396}
8397
8398static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8399{
8400 struct drm_device *dev = dev_priv->dev;
8401
8402 if (IS_HASWELL(dev)) {
8403 mutex_lock(&dev_priv->rps.hw_lock);
8404 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8405 val))
8406 DRM_ERROR("Failed to write to D_COMP\n");
8407 mutex_unlock(&dev_priv->rps.hw_lock);
8408 } else {
8409 I915_WRITE(D_COMP_BDW, val);
8410 POSTING_READ(D_COMP_BDW);
8411 }
8412}
8413
8414/*
8415 * This function implements pieces of two sequences from BSpec:
8416 * - Sequence for display software to disable LCPLL
8417 * - Sequence for display software to allow package C8+
8418 * The steps implemented here are just the steps that actually touch the LCPLL
8419 * register. Callers should take care of disabling all the display engine
8420 * functions, doing the mode unset, fixing interrupts, etc.
8421 */
8422static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8423 bool switch_to_fclk, bool allow_power_down)
8424{
8425 uint32_t val;
8426
8427 assert_can_disable_lcpll(dev_priv);
8428
8429 val = I915_READ(LCPLL_CTL);
8430
8431 if (switch_to_fclk) {
8432 val |= LCPLL_CD_SOURCE_FCLK;
8433 I915_WRITE(LCPLL_CTL, val);
8434
8435 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8436 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8437 DRM_ERROR("Switching to FCLK failed\n");
8438
8439 val = I915_READ(LCPLL_CTL);
8440 }
8441
8442 val |= LCPLL_PLL_DISABLE;
8443 I915_WRITE(LCPLL_CTL, val);
8444 POSTING_READ(LCPLL_CTL);
8445
8446 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8447 DRM_ERROR("LCPLL still locked\n");
8448
8449 val = hsw_read_dcomp(dev_priv);
8450 val |= D_COMP_COMP_DISABLE;
8451 hsw_write_dcomp(dev_priv, val);
8452 ndelay(100);
8453
8454 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8455 1))
8456 DRM_ERROR("D_COMP RCOMP still in progress\n");
8457
8458 if (allow_power_down) {
8459 val = I915_READ(LCPLL_CTL);
8460 val |= LCPLL_POWER_DOWN_ALLOW;
8461 I915_WRITE(LCPLL_CTL, val);
8462 POSTING_READ(LCPLL_CTL);
8463 }
8464}
8465
8466/*
8467 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8468 * source.
8469 */
8470static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
8471{
8472 uint32_t val;
8473
8474 val = I915_READ(LCPLL_CTL);
8475
8476 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8477 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8478 return;
8479
8480 /*
8481 * Make sure we're not on PC8 state before disabling PC8, otherwise
8482 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
8483 */
8484 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
8485
8486 if (val & LCPLL_POWER_DOWN_ALLOW) {
8487 val &= ~LCPLL_POWER_DOWN_ALLOW;
8488 I915_WRITE(LCPLL_CTL, val);
8489 POSTING_READ(LCPLL_CTL);
8490 }
8491
8492 val = hsw_read_dcomp(dev_priv);
8493 val |= D_COMP_COMP_FORCE;
8494 val &= ~D_COMP_COMP_DISABLE;
8495 hsw_write_dcomp(dev_priv, val);
8496
8497 val = I915_READ(LCPLL_CTL);
8498 val &= ~LCPLL_PLL_DISABLE;
8499 I915_WRITE(LCPLL_CTL, val);
8500
8501 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8502 DRM_ERROR("LCPLL not locked yet\n");
8503
8504 if (val & LCPLL_CD_SOURCE_FCLK) {
8505 val = I915_READ(LCPLL_CTL);
8506 val &= ~LCPLL_CD_SOURCE_FCLK;
8507 I915_WRITE(LCPLL_CTL, val);
8508
8509 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8510 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8511 DRM_ERROR("Switching back to LCPLL failed\n");
8512 }
8513
8514 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
8515}
8516
8517/*
8518 * Package states C8 and deeper are really deep PC states that can only be
8519 * reached when all the devices on the system allow it, so even if the graphics
8520 * device allows PC8+, it doesn't mean the system will actually get to these
8521 * states. Our driver only allows PC8+ when going into runtime PM.
8522 *
8523 * The requirements for PC8+ are that all the outputs are disabled, the power
8524 * well is disabled and most interrupts are disabled, and these are also
8525 * requirements for runtime PM. When these conditions are met, we manually do
8526 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
8527 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
8528 * hang the machine.
8529 *
8530 * When we really reach PC8 or deeper states (not just when we allow it) we lose
8531 * the state of some registers, so when we come back from PC8+ we need to
8532 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
8533 * need to take care of the registers kept by RC6. Notice that this happens even
8534 * if we don't put the device in PCI D3 state (which is what currently happens
8535 * because of the runtime PM support).
8536 *
8537 * For more, read "Display Sequences for Package C8" on the hardware
8538 * documentation.
8539 */
8540void hsw_enable_pc8(struct drm_i915_private *dev_priv)
8541{
8542 struct drm_device *dev = dev_priv->dev;
8543 uint32_t val;
8544
8545 DRM_DEBUG_KMS("Enabling package C8+\n");
8546
8547 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8548 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8549 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
8550 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8551 }
8552
8553 lpt_disable_clkout_dp(dev);
8554 hsw_disable_lcpll(dev_priv, true, true);
8555}
8556
8557void hsw_disable_pc8(struct drm_i915_private *dev_priv)
8558{
8559 struct drm_device *dev = dev_priv->dev;
8560 uint32_t val;
8561
8562 DRM_DEBUG_KMS("Disabling package C8+\n");
8563
8564 hsw_restore_lcpll(dev_priv);
8565 lpt_init_pch_refclk(dev);
8566
8567 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
8568 val = I915_READ(SOUTH_DSPCLK_GATE_D);
8569 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
8570 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
8571 }
8572
8573 intel_prepare_ddi(dev);
8574}
8575
8576static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
8577 struct intel_crtc_state *crtc_state)
8578{
8579 if (!intel_ddi_pll_select(crtc, crtc_state))
8580 return -EINVAL;
8581
8582 crtc->lowfreq_avail = false;
8583
8584 return 0;
8585}
8586
8587static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
8588 enum port port,
8589 struct intel_crtc_state *pipe_config)
8590{
8591 u32 temp, dpll_ctl1;
8592
8593 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
8594 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
8595
8596 switch (pipe_config->ddi_pll_sel) {
8597 case SKL_DPLL0:
8598 /*
8599 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
8600 * of the shared DPLL framework and thus needs to be read out
8601 * separately
8602 */
8603 dpll_ctl1 = I915_READ(DPLL_CTRL1);
8604 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
8605 break;
8606 case SKL_DPLL1:
8607 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
8608 break;
8609 case SKL_DPLL2:
8610 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
8611 break;
8612 case SKL_DPLL3:
8613 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
8614 break;
8615 }
8616}
8617
8618static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
8619 enum port port,
8620 struct intel_crtc_state *pipe_config)
8621{
8622 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
8623
8624 switch (pipe_config->ddi_pll_sel) {
8625 case PORT_CLK_SEL_WRPLL1:
8626 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
8627 break;
8628 case PORT_CLK_SEL_WRPLL2:
8629 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
8630 break;
8631 }
8632}
8633
8634static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
8635 struct intel_crtc_state *pipe_config)
8636{
8637 struct drm_device *dev = crtc->base.dev;
8638 struct drm_i915_private *dev_priv = dev->dev_private;
8639 struct intel_shared_dpll *pll;
8640 enum port port;
8641 uint32_t tmp;
8642
8643 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
8644
8645 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
8646
8647 if (IS_SKYLAKE(dev))
8648 skylake_get_ddi_pll(dev_priv, port, pipe_config);
8649 else
8650 haswell_get_ddi_pll(dev_priv, port, pipe_config);
8651
8652 if (pipe_config->shared_dpll >= 0) {
8653 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8654
8655 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8656 &pipe_config->dpll_hw_state));
8657 }
8658
8659 /*
8660 * Haswell has only FDI/PCH transcoder A. It is which is connected to
8661 * DDI E. So just check whether this pipe is wired to DDI E and whether
8662 * the PCH transcoder is on.
8663 */
8664 if (INTEL_INFO(dev)->gen < 9 &&
8665 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
8666 pipe_config->has_pch_encoder = true;
8667
8668 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
8669 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8670 FDI_DP_PORT_WIDTH_SHIFT) + 1;
8671
8672 ironlake_get_fdi_m_n_config(crtc, pipe_config);
8673 }
8674}
8675
8676static bool haswell_get_pipe_config(struct intel_crtc *crtc,
8677 struct intel_crtc_state *pipe_config)
8678{
8679 struct drm_device *dev = crtc->base.dev;
8680 struct drm_i915_private *dev_priv = dev->dev_private;
8681 enum intel_display_power_domain pfit_domain;
8682 uint32_t tmp;
8683
8684 if (!intel_display_power_is_enabled(dev_priv,
8685 POWER_DOMAIN_PIPE(crtc->pipe)))
8686 return false;
8687
8688 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
8689 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
8690
8691 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
8692 if (tmp & TRANS_DDI_FUNC_ENABLE) {
8693 enum pipe trans_edp_pipe;
8694 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
8695 default:
8696 WARN(1, "unknown pipe linked to edp transcoder\n");
8697 case TRANS_DDI_EDP_INPUT_A_ONOFF:
8698 case TRANS_DDI_EDP_INPUT_A_ON:
8699 trans_edp_pipe = PIPE_A;
8700 break;
8701 case TRANS_DDI_EDP_INPUT_B_ONOFF:
8702 trans_edp_pipe = PIPE_B;
8703 break;
8704 case TRANS_DDI_EDP_INPUT_C_ONOFF:
8705 trans_edp_pipe = PIPE_C;
8706 break;
8707 }
8708
8709 if (trans_edp_pipe == crtc->pipe)
8710 pipe_config->cpu_transcoder = TRANSCODER_EDP;
8711 }
8712
8713 if (!intel_display_power_is_enabled(dev_priv,
8714 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
8715 return false;
8716
8717 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
8718 if (!(tmp & PIPECONF_ENABLE))
8719 return false;
8720
8721 haswell_get_ddi_port_state(crtc, pipe_config);
8722
8723 intel_get_pipe_timings(crtc, pipe_config);
8724
8725 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
8726 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
8727 if (IS_SKYLAKE(dev))
8728 skylake_get_pfit_config(crtc, pipe_config);
8729 else
8730 ironlake_get_pfit_config(crtc, pipe_config);
8731 }
8732
8733 if (IS_HASWELL(dev))
8734 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8735 (I915_READ(IPS_CTL) & IPS_ENABLE);
8736
8737 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8738 pipe_config->pixel_multiplier =
8739 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8740 } else {
8741 pipe_config->pixel_multiplier = 1;
8742 }
8743
8744 return true;
8745}
8746
8747static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8748{
8749 struct drm_device *dev = crtc->dev;
8750 struct drm_i915_private *dev_priv = dev->dev_private;
8751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8752 uint32_t cntl = 0, size = 0;
8753
8754 if (base) {
8755 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
8756 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
8757 unsigned int stride = roundup_pow_of_two(width) * 4;
8758
8759 switch (stride) {
8760 default:
8761 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8762 width, stride);
8763 stride = 256;
8764 /* fallthrough */
8765 case 256:
8766 case 512:
8767 case 1024:
8768 case 2048:
8769 break;
8770 }
8771
8772 cntl |= CURSOR_ENABLE |
8773 CURSOR_GAMMA_ENABLE |
8774 CURSOR_FORMAT_ARGB |
8775 CURSOR_STRIDE(stride);
8776
8777 size = (height << 12) | width;
8778 }
8779
8780 if (intel_crtc->cursor_cntl != 0 &&
8781 (intel_crtc->cursor_base != base ||
8782 intel_crtc->cursor_size != size ||
8783 intel_crtc->cursor_cntl != cntl)) {
8784 /* On these chipsets we can only modify the base/size/stride
8785 * whilst the cursor is disabled.
8786 */
8787 I915_WRITE(_CURACNTR, 0);
8788 POSTING_READ(_CURACNTR);
8789 intel_crtc->cursor_cntl = 0;
8790 }
8791
8792 if (intel_crtc->cursor_base != base) {
8793 I915_WRITE(_CURABASE, base);
8794 intel_crtc->cursor_base = base;
8795 }
8796
8797 if (intel_crtc->cursor_size != size) {
8798 I915_WRITE(CURSIZE, size);
8799 intel_crtc->cursor_size = size;
8800 }
8801
8802 if (intel_crtc->cursor_cntl != cntl) {
8803 I915_WRITE(_CURACNTR, cntl);
8804 POSTING_READ(_CURACNTR);
8805 intel_crtc->cursor_cntl = cntl;
8806 }
8807}
8808
8809static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8810{
8811 struct drm_device *dev = crtc->dev;
8812 struct drm_i915_private *dev_priv = dev->dev_private;
8813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8814 int pipe = intel_crtc->pipe;
8815 uint32_t cntl;
8816
8817 cntl = 0;
8818 if (base) {
8819 cntl = MCURSOR_GAMMA_ENABLE;
8820 switch (intel_crtc->base.cursor->state->crtc_w) {
8821 case 64:
8822 cntl |= CURSOR_MODE_64_ARGB_AX;
8823 break;
8824 case 128:
8825 cntl |= CURSOR_MODE_128_ARGB_AX;
8826 break;
8827 case 256:
8828 cntl |= CURSOR_MODE_256_ARGB_AX;
8829 break;
8830 default:
8831 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
8832 return;
8833 }
8834 cntl |= pipe << 28; /* Connect to correct pipe */
8835
8836 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8837 cntl |= CURSOR_PIPE_CSC_ENABLE;
8838 }
8839
8840 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
8841 cntl |= CURSOR_ROTATE_180;
8842
8843 if (intel_crtc->cursor_cntl != cntl) {
8844 I915_WRITE(CURCNTR(pipe), cntl);
8845 POSTING_READ(CURCNTR(pipe));
8846 intel_crtc->cursor_cntl = cntl;
8847 }
8848
8849 /* and commit changes on next vblank */
8850 I915_WRITE(CURBASE(pipe), base);
8851 POSTING_READ(CURBASE(pipe));
8852
8853 intel_crtc->cursor_base = base;
8854}
8855
8856/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
8857static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8858 bool on)
8859{
8860 struct drm_device *dev = crtc->dev;
8861 struct drm_i915_private *dev_priv = dev->dev_private;
8862 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8863 int pipe = intel_crtc->pipe;
8864 int x = crtc->cursor_x;
8865 int y = crtc->cursor_y;
8866 u32 base = 0, pos = 0;
8867
8868 if (on)
8869 base = intel_crtc->cursor_addr;
8870
8871 if (x >= intel_crtc->config->pipe_src_w)
8872 base = 0;
8873
8874 if (y >= intel_crtc->config->pipe_src_h)
8875 base = 0;
8876
8877 if (x < 0) {
8878 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
8879 base = 0;
8880
8881 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8882 x = -x;
8883 }
8884 pos |= x << CURSOR_X_SHIFT;
8885
8886 if (y < 0) {
8887 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
8888 base = 0;
8889
8890 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8891 y = -y;
8892 }
8893 pos |= y << CURSOR_Y_SHIFT;
8894
8895 if (base == 0 && intel_crtc->cursor_base == 0)
8896 return;
8897
8898 I915_WRITE(CURPOS(pipe), pos);
8899
8900 /* ILK+ do this automagically */
8901 if (HAS_GMCH_DISPLAY(dev) &&
8902 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
8903 base += (intel_crtc->base.cursor->state->crtc_h *
8904 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
8905 }
8906
8907 if (IS_845G(dev) || IS_I865G(dev))
8908 i845_update_cursor(crtc, base);
8909 else
8910 i9xx_update_cursor(crtc, base);
8911}
8912
8913static bool cursor_size_ok(struct drm_device *dev,
8914 uint32_t width, uint32_t height)
8915{
8916 if (width == 0 || height == 0)
8917 return false;
8918
8919 /*
8920 * 845g/865g are special in that they are only limited by
8921 * the width of their cursors, the height is arbitrary up to
8922 * the precision of the register. Everything else requires
8923 * square cursors, limited to a few power-of-two sizes.
8924 */
8925 if (IS_845G(dev) || IS_I865G(dev)) {
8926 if ((width & 63) != 0)
8927 return false;
8928
8929 if (width > (IS_845G(dev) ? 64 : 512))
8930 return false;
8931
8932 if (height > 1023)
8933 return false;
8934 } else {
8935 switch (width | height) {
8936 case 256:
8937 case 128:
8938 if (IS_GEN2(dev))
8939 return false;
8940 case 64:
8941 break;
8942 default:
8943 return false;
8944 }
8945 }
8946
8947 return true;
8948}
8949
8950static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
8951 u16 *blue, uint32_t start, uint32_t size)
8952{
8953 int end = (start + size > 256) ? 256 : start + size, i;
8954 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8955
8956 for (i = start; i < end; i++) {
8957 intel_crtc->lut_r[i] = red[i] >> 8;
8958 intel_crtc->lut_g[i] = green[i] >> 8;
8959 intel_crtc->lut_b[i] = blue[i] >> 8;
8960 }
8961
8962 intel_crtc_load_lut(crtc);
8963}
8964
8965/* VESA 640x480x72Hz mode to set on the pipe */
8966static struct drm_display_mode load_detect_mode = {
8967 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8968 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8969};
8970
8971struct drm_framebuffer *
8972__intel_framebuffer_create(struct drm_device *dev,
8973 struct drm_mode_fb_cmd2 *mode_cmd,
8974 struct drm_i915_gem_object *obj)
8975{
8976 struct intel_framebuffer *intel_fb;
8977 int ret;
8978
8979 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8980 if (!intel_fb) {
8981 drm_gem_object_unreference(&obj->base);
8982 return ERR_PTR(-ENOMEM);
8983 }
8984
8985 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
8986 if (ret)
8987 goto err;
8988
8989 return &intel_fb->base;
8990err:
8991 drm_gem_object_unreference(&obj->base);
8992 kfree(intel_fb);
8993
8994 return ERR_PTR(ret);
8995}
8996
8997static struct drm_framebuffer *
8998intel_framebuffer_create(struct drm_device *dev,
8999 struct drm_mode_fb_cmd2 *mode_cmd,
9000 struct drm_i915_gem_object *obj)
9001{
9002 struct drm_framebuffer *fb;
9003 int ret;
9004
9005 ret = i915_mutex_lock_interruptible(dev);
9006 if (ret)
9007 return ERR_PTR(ret);
9008 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9009 mutex_unlock(&dev->struct_mutex);
9010
9011 return fb;
9012}
9013
9014static u32
9015intel_framebuffer_pitch_for_width(int width, int bpp)
9016{
9017 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9018 return ALIGN(pitch, 64);
9019}
9020
9021static u32
9022intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9023{
9024 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
9025 return PAGE_ALIGN(pitch * mode->vdisplay);
9026}
9027
9028static struct drm_framebuffer *
9029intel_framebuffer_create_for_mode(struct drm_device *dev,
9030 struct drm_display_mode *mode,
9031 int depth, int bpp)
9032{
9033 struct drm_i915_gem_object *obj;
9034 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
9035
9036 obj = i915_gem_alloc_object(dev,
9037 intel_framebuffer_size_for_mode(mode, bpp));
9038 if (obj == NULL)
9039 return ERR_PTR(-ENOMEM);
9040
9041 mode_cmd.width = mode->hdisplay;
9042 mode_cmd.height = mode->vdisplay;
9043 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9044 bpp);
9045 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
9046
9047 return intel_framebuffer_create(dev, &mode_cmd, obj);
9048}
9049
9050static struct drm_framebuffer *
9051mode_fits_in_fbdev(struct drm_device *dev,
9052 struct drm_display_mode *mode)
9053{
9054#ifdef CONFIG_DRM_I915_FBDEV
9055 struct drm_i915_private *dev_priv = dev->dev_private;
9056 struct drm_i915_gem_object *obj;
9057 struct drm_framebuffer *fb;
9058
9059 if (!dev_priv->fbdev)
9060 return NULL;
9061
9062 if (!dev_priv->fbdev->fb)
9063 return NULL;
9064
9065 obj = dev_priv->fbdev->fb->obj;
9066 BUG_ON(!obj);
9067
9068 fb = &dev_priv->fbdev->fb->base;
9069 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9070 fb->bits_per_pixel))
9071 return NULL;
9072
9073 if (obj->base.size < mode->vdisplay * fb->pitches[0])
9074 return NULL;
9075
9076 return fb;
9077#else
9078 return NULL;
9079#endif
9080}
9081
9082bool intel_get_load_detect_pipe(struct drm_connector *connector,
9083 struct drm_display_mode *mode,
9084 struct intel_load_detect_pipe *old,
9085 struct drm_modeset_acquire_ctx *ctx)
9086{
9087 struct intel_crtc *intel_crtc;
9088 struct intel_encoder *intel_encoder =
9089 intel_attached_encoder(connector);
9090 struct drm_crtc *possible_crtc;
9091 struct drm_encoder *encoder = &intel_encoder->base;
9092 struct drm_crtc *crtc = NULL;
9093 struct drm_device *dev = encoder->dev;
9094 struct drm_framebuffer *fb;
9095 struct drm_mode_config *config = &dev->mode_config;
9096 struct drm_atomic_state *state = NULL;
9097 struct drm_connector_state *connector_state;
9098 int ret, i = -1;
9099
9100 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
9101 connector->base.id, connector->name,
9102 encoder->base.id, encoder->name);
9103
9104retry:
9105 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9106 if (ret)
9107 goto fail_unlock;
9108
9109 /*
9110 * Algorithm gets a little messy:
9111 *
9112 * - if the connector already has an assigned crtc, use it (but make
9113 * sure it's on first)
9114 *
9115 * - try to find the first unused crtc that can drive this connector,
9116 * and use that if we find one
9117 */
9118
9119 /* See if we already have a CRTC for this connector */
9120 if (encoder->crtc) {
9121 crtc = encoder->crtc;
9122
9123 ret = drm_modeset_lock(&crtc->mutex, ctx);
9124 if (ret)
9125 goto fail_unlock;
9126 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9127 if (ret)
9128 goto fail_unlock;
9129
9130 old->dpms_mode = connector->dpms;
9131 old->load_detect_temp = false;
9132
9133 /* Make sure the crtc and connector are running */
9134 if (connector->dpms != DRM_MODE_DPMS_ON)
9135 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
9136
9137 return true;
9138 }
9139
9140 /* Find an unused one (if possible) */
9141 for_each_crtc(dev, possible_crtc) {
9142 i++;
9143 if (!(encoder->possible_crtcs & (1 << i)))
9144 continue;
9145 if (possible_crtc->state->enable)
9146 continue;
9147 /* This can occur when applying the pipe A quirk on resume. */
9148 if (to_intel_crtc(possible_crtc)->new_enabled)
9149 continue;
9150
9151 crtc = possible_crtc;
9152 break;
9153 }
9154
9155 /*
9156 * If we didn't find an unused CRTC, don't use any.
9157 */
9158 if (!crtc) {
9159 DRM_DEBUG_KMS("no pipe available for load-detect\n");
9160 goto fail_unlock;
9161 }
9162
9163 ret = drm_modeset_lock(&crtc->mutex, ctx);
9164 if (ret)
9165 goto fail_unlock;
9166 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9167 if (ret)
9168 goto fail_unlock;
9169 intel_encoder->new_crtc = to_intel_crtc(crtc);
9170 to_intel_connector(connector)->new_encoder = intel_encoder;
9171
9172 intel_crtc = to_intel_crtc(crtc);
9173 intel_crtc->new_enabled = true;
9174 old->dpms_mode = connector->dpms;
9175 old->load_detect_temp = true;
9176 old->release_fb = NULL;
9177
9178 state = drm_atomic_state_alloc(dev);
9179 if (!state)
9180 return false;
9181
9182 state->acquire_ctx = ctx;
9183
9184 connector_state = drm_atomic_get_connector_state(state, connector);
9185 if (IS_ERR(connector_state)) {
9186 ret = PTR_ERR(connector_state);
9187 goto fail;
9188 }
9189
9190 connector_state->crtc = crtc;
9191 connector_state->best_encoder = &intel_encoder->base;
9192
9193 if (!mode)
9194 mode = &load_detect_mode;
9195
9196 /* We need a framebuffer large enough to accommodate all accesses
9197 * that the plane may generate whilst we perform load detection.
9198 * We can not rely on the fbcon either being present (we get called
9199 * during its initialisation to detect all boot displays, or it may
9200 * not even exist) or that it is large enough to satisfy the
9201 * requested mode.
9202 */
9203 fb = mode_fits_in_fbdev(dev, mode);
9204 if (fb == NULL) {
9205 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
9206 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9207 old->release_fb = fb;
9208 } else
9209 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
9210 if (IS_ERR(fb)) {
9211 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
9212 goto fail;
9213 }
9214
9215 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
9216 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
9217 if (old->release_fb)
9218 old->release_fb->funcs->destroy(old->release_fb);
9219 goto fail;
9220 }
9221 crtc->primary->crtc = crtc;
9222
9223 /* let the connector get through one full cycle before testing */
9224 intel_wait_for_vblank(dev, intel_crtc->pipe);
9225 return true;
9226
9227 fail:
9228 intel_crtc->new_enabled = crtc->state->enable;
9229fail_unlock:
9230 if (state) {
9231 drm_atomic_state_free(state);
9232 state = NULL;
9233 }
9234
9235 if (ret == -EDEADLK) {
9236 drm_modeset_backoff(ctx);
9237 goto retry;
9238 }
9239
9240 return false;
9241}
9242
9243void intel_release_load_detect_pipe(struct drm_connector *connector,
9244 struct intel_load_detect_pipe *old,
9245 struct drm_modeset_acquire_ctx *ctx)
9246{
9247 struct drm_device *dev = connector->dev;
9248 struct intel_encoder *intel_encoder =
9249 intel_attached_encoder(connector);
9250 struct drm_encoder *encoder = &intel_encoder->base;
9251 struct drm_crtc *crtc = encoder->crtc;
9252 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9253 struct drm_atomic_state *state;
9254 struct drm_connector_state *connector_state;
9255
9256 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
9257 connector->base.id, connector->name,
9258 encoder->base.id, encoder->name);
9259
9260 if (old->load_detect_temp) {
9261 state = drm_atomic_state_alloc(dev);
9262 if (!state)
9263 goto fail;
9264
9265 state->acquire_ctx = ctx;
9266
9267 connector_state = drm_atomic_get_connector_state(state, connector);
9268 if (IS_ERR(connector_state))
9269 goto fail;
9270
9271 to_intel_connector(connector)->new_encoder = NULL;
9272 intel_encoder->new_crtc = NULL;
9273 intel_crtc->new_enabled = false;
9274
9275 connector_state->best_encoder = NULL;
9276 connector_state->crtc = NULL;
9277
9278 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9279
9280 drm_atomic_state_free(state);
9281
9282 if (old->release_fb) {
9283 drm_framebuffer_unregister_private(old->release_fb);
9284 drm_framebuffer_unreference(old->release_fb);
9285 }
9286
9287 return;
9288 }
9289
9290 /* Switch crtc and encoder back off if necessary */
9291 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9292 connector->funcs->dpms(connector, old->dpms_mode);
9293
9294 return;
9295fail:
9296 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9297 drm_atomic_state_free(state);
9298}
9299
9300static int i9xx_pll_refclk(struct drm_device *dev,
9301 const struct intel_crtc_state *pipe_config)
9302{
9303 struct drm_i915_private *dev_priv = dev->dev_private;
9304 u32 dpll = pipe_config->dpll_hw_state.dpll;
9305
9306 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
9307 return dev_priv->vbt.lvds_ssc_freq;
9308 else if (HAS_PCH_SPLIT(dev))
9309 return 120000;
9310 else if (!IS_GEN2(dev))
9311 return 96000;
9312 else
9313 return 48000;
9314}
9315
9316/* Returns the clock of the currently programmed mode of the given pipe. */
9317static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
9318 struct intel_crtc_state *pipe_config)
9319{
9320 struct drm_device *dev = crtc->base.dev;
9321 struct drm_i915_private *dev_priv = dev->dev_private;
9322 int pipe = pipe_config->cpu_transcoder;
9323 u32 dpll = pipe_config->dpll_hw_state.dpll;
9324 u32 fp;
9325 intel_clock_t clock;
9326 int refclk = i9xx_pll_refclk(dev, pipe_config);
9327
9328 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
9329 fp = pipe_config->dpll_hw_state.fp0;
9330 else
9331 fp = pipe_config->dpll_hw_state.fp1;
9332
9333 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
9334 if (IS_PINEVIEW(dev)) {
9335 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9336 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
9337 } else {
9338 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9339 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9340 }
9341
9342 if (!IS_GEN2(dev)) {
9343 if (IS_PINEVIEW(dev))
9344 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9345 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
9346 else
9347 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
9348 DPLL_FPA01_P1_POST_DIV_SHIFT);
9349
9350 switch (dpll & DPLL_MODE_MASK) {
9351 case DPLLB_MODE_DAC_SERIAL:
9352 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9353 5 : 10;
9354 break;
9355 case DPLLB_MODE_LVDS:
9356 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9357 7 : 14;
9358 break;
9359 default:
9360 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
9361 "mode\n", (int)(dpll & DPLL_MODE_MASK));
9362 return;
9363 }
9364
9365 if (IS_PINEVIEW(dev))
9366 pineview_clock(refclk, &clock);
9367 else
9368 i9xx_clock(refclk, &clock);
9369 } else {
9370 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
9371 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
9372
9373 if (is_lvds) {
9374 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9375 DPLL_FPA01_P1_POST_DIV_SHIFT);
9376
9377 if (lvds & LVDS_CLKB_POWER_UP)
9378 clock.p2 = 7;
9379 else
9380 clock.p2 = 14;
9381 } else {
9382 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9383 clock.p1 = 2;
9384 else {
9385 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9386 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9387 }
9388 if (dpll & PLL_P2_DIVIDE_BY_4)
9389 clock.p2 = 4;
9390 else
9391 clock.p2 = 2;
9392 }
9393
9394 i9xx_clock(refclk, &clock);
9395 }
9396
9397 /*
9398 * This value includes pixel_multiplier. We will use
9399 * port_clock to compute adjusted_mode.crtc_clock in the
9400 * encoder's get_config() function.
9401 */
9402 pipe_config->port_clock = clock.dot;
9403}
9404
9405int intel_dotclock_calculate(int link_freq,
9406 const struct intel_link_m_n *m_n)
9407{
9408 /*
9409 * The calculation for the data clock is:
9410 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
9411 * But we want to avoid losing precison if possible, so:
9412 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
9413 *
9414 * and the link clock is simpler:
9415 * link_clock = (m * link_clock) / n
9416 */
9417
9418 if (!m_n->link_n)
9419 return 0;
9420
9421 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9422}
9423
9424static void ironlake_pch_clock_get(struct intel_crtc *crtc,
9425 struct intel_crtc_state *pipe_config)
9426{
9427 struct drm_device *dev = crtc->base.dev;
9428
9429 /* read out port_clock from the DPLL */
9430 i9xx_crtc_clock_get(crtc, pipe_config);
9431
9432 /*
9433 * This value does not include pixel_multiplier.
9434 * We will check that port_clock and adjusted_mode.crtc_clock
9435 * agree once we know their relationship in the encoder's
9436 * get_config() function.
9437 */
9438 pipe_config->base.adjusted_mode.crtc_clock =
9439 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9440 &pipe_config->fdi_m_n);
9441}
9442
9443/** Returns the currently programmed mode of the given pipe. */
9444struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9445 struct drm_crtc *crtc)
9446{
9447 struct drm_i915_private *dev_priv = dev->dev_private;
9448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9449 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
9450 struct drm_display_mode *mode;
9451 struct intel_crtc_state pipe_config;
9452 int htot = I915_READ(HTOTAL(cpu_transcoder));
9453 int hsync = I915_READ(HSYNC(cpu_transcoder));
9454 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9455 int vsync = I915_READ(VSYNC(cpu_transcoder));
9456 enum pipe pipe = intel_crtc->pipe;
9457
9458 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9459 if (!mode)
9460 return NULL;
9461
9462 /*
9463 * Construct a pipe_config sufficient for getting the clock info
9464 * back out of crtc_clock_get.
9465 *
9466 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9467 * to use a real value here instead.
9468 */
9469 pipe_config.cpu_transcoder = (enum transcoder) pipe;
9470 pipe_config.pixel_multiplier = 1;
9471 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9472 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9473 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
9474 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9475
9476 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
9477 mode->hdisplay = (htot & 0xffff) + 1;
9478 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9479 mode->hsync_start = (hsync & 0xffff) + 1;
9480 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9481 mode->vdisplay = (vtot & 0xffff) + 1;
9482 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9483 mode->vsync_start = (vsync & 0xffff) + 1;
9484 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9485
9486 drm_mode_set_name(mode);
9487
9488 return mode;
9489}
9490
9491static void intel_decrease_pllclock(struct drm_crtc *crtc)
9492{
9493 struct drm_device *dev = crtc->dev;
9494 struct drm_i915_private *dev_priv = dev->dev_private;
9495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9496
9497 if (!HAS_GMCH_DISPLAY(dev))
9498 return;
9499
9500 if (!dev_priv->lvds_downclock_avail)
9501 return;
9502
9503 /*
9504 * Since this is called by a timer, we should never get here in
9505 * the manual case.
9506 */
9507 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
9508 int pipe = intel_crtc->pipe;
9509 int dpll_reg = DPLL(pipe);
9510 int dpll;
9511
9512 DRM_DEBUG_DRIVER("downclocking LVDS\n");
9513
9514 assert_panel_unlocked(dev_priv, pipe);
9515
9516 dpll = I915_READ(dpll_reg);
9517 dpll |= DISPLAY_RATE_SELECT_FPA1;
9518 I915_WRITE(dpll_reg, dpll);
9519 intel_wait_for_vblank(dev, pipe);
9520 dpll = I915_READ(dpll_reg);
9521 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
9522 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
9523 }
9524
9525}
9526
9527void intel_mark_busy(struct drm_device *dev)
9528{
9529 struct drm_i915_private *dev_priv = dev->dev_private;
9530
9531 if (dev_priv->mm.busy)
9532 return;
9533
9534 intel_runtime_pm_get(dev_priv);
9535 i915_update_gfx_val(dev_priv);
9536 if (INTEL_INFO(dev)->gen >= 6)
9537 gen6_rps_busy(dev_priv);
9538 dev_priv->mm.busy = true;
9539}
9540
9541void intel_mark_idle(struct drm_device *dev)
9542{
9543 struct drm_i915_private *dev_priv = dev->dev_private;
9544 struct drm_crtc *crtc;
9545
9546 if (!dev_priv->mm.busy)
9547 return;
9548
9549 dev_priv->mm.busy = false;
9550
9551 for_each_crtc(dev, crtc) {
9552 if (!crtc->primary->fb)
9553 continue;
9554
9555 intel_decrease_pllclock(crtc);
9556 }
9557
9558 if (INTEL_INFO(dev)->gen >= 6)
9559 gen6_rps_idle(dev->dev_private);
9560
9561 intel_runtime_pm_put(dev_priv);
9562}
9563
9564static void intel_crtc_set_state(struct intel_crtc *crtc,
9565 struct intel_crtc_state *crtc_state)
9566{
9567 kfree(crtc->config);
9568 crtc->config = crtc_state;
9569 crtc->base.state = &crtc_state->base;
9570}
9571
9572static void intel_crtc_destroy(struct drm_crtc *crtc)
9573{
9574 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9575 struct drm_device *dev = crtc->dev;
9576 struct intel_unpin_work *work;
9577
9578 spin_lock_irq(&dev->event_lock);
9579 work = intel_crtc->unpin_work;
9580 intel_crtc->unpin_work = NULL;
9581 spin_unlock_irq(&dev->event_lock);
9582
9583 if (work) {
9584 cancel_work_sync(&work->work);
9585 kfree(work);
9586 }
9587
9588 intel_crtc_set_state(intel_crtc, NULL);
9589 drm_crtc_cleanup(crtc);
9590
9591 kfree(intel_crtc);
9592}
9593
9594static void intel_unpin_work_fn(struct work_struct *__work)
9595{
9596 struct intel_unpin_work *work =
9597 container_of(__work, struct intel_unpin_work, work);
9598 struct drm_device *dev = work->crtc->dev;
9599 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
9600
9601 mutex_lock(&dev->struct_mutex);
9602 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
9603 drm_gem_object_unreference(&work->pending_flip_obj->base);
9604
9605 intel_fbc_update(dev);
9606
9607 if (work->flip_queued_req)
9608 i915_gem_request_assign(&work->flip_queued_req, NULL);
9609 mutex_unlock(&dev->struct_mutex);
9610
9611 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
9612 drm_framebuffer_unreference(work->old_fb);
9613
9614 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
9615 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
9616
9617 kfree(work);
9618}
9619
9620static void do_intel_finish_page_flip(struct drm_device *dev,
9621 struct drm_crtc *crtc)
9622{
9623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9624 struct intel_unpin_work *work;
9625 unsigned long flags;
9626
9627 /* Ignore early vblank irqs */
9628 if (intel_crtc == NULL)
9629 return;
9630
9631 /*
9632 * This is called both by irq handlers and the reset code (to complete
9633 * lost pageflips) so needs the full irqsave spinlocks.
9634 */
9635 spin_lock_irqsave(&dev->event_lock, flags);
9636 work = intel_crtc->unpin_work;
9637
9638 /* Ensure we don't miss a work->pending update ... */
9639 smp_rmb();
9640
9641 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
9642 spin_unlock_irqrestore(&dev->event_lock, flags);
9643 return;
9644 }
9645
9646 page_flip_completed(intel_crtc);
9647
9648 spin_unlock_irqrestore(&dev->event_lock, flags);
9649}
9650
9651void intel_finish_page_flip(struct drm_device *dev, int pipe)
9652{
9653 struct drm_i915_private *dev_priv = dev->dev_private;
9654 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9655
9656 do_intel_finish_page_flip(dev, crtc);
9657}
9658
9659void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
9660{
9661 struct drm_i915_private *dev_priv = dev->dev_private;
9662 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
9663
9664 do_intel_finish_page_flip(dev, crtc);
9665}
9666
9667/* Is 'a' after or equal to 'b'? */
9668static bool g4x_flip_count_after_eq(u32 a, u32 b)
9669{
9670 return !((a - b) & 0x80000000);
9671}
9672
9673static bool page_flip_finished(struct intel_crtc *crtc)
9674{
9675 struct drm_device *dev = crtc->base.dev;
9676 struct drm_i915_private *dev_priv = dev->dev_private;
9677
9678 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
9679 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
9680 return true;
9681
9682 /*
9683 * The relevant registers doen't exist on pre-ctg.
9684 * As the flip done interrupt doesn't trigger for mmio
9685 * flips on gmch platforms, a flip count check isn't
9686 * really needed there. But since ctg has the registers,
9687 * include it in the check anyway.
9688 */
9689 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9690 return true;
9691
9692 /*
9693 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9694 * used the same base address. In that case the mmio flip might
9695 * have completed, but the CS hasn't even executed the flip yet.
9696 *
9697 * A flip count check isn't enough as the CS might have updated
9698 * the base address just after start of vblank, but before we
9699 * managed to process the interrupt. This means we'd complete the
9700 * CS flip too soon.
9701 *
9702 * Combining both checks should get us a good enough result. It may
9703 * still happen that the CS flip has been executed, but has not
9704 * yet actually completed. But in case the base address is the same
9705 * anyway, we don't really care.
9706 */
9707 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9708 crtc->unpin_work->gtt_offset &&
9709 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9710 crtc->unpin_work->flip_count);
9711}
9712
9713void intel_prepare_page_flip(struct drm_device *dev, int plane)
9714{
9715 struct drm_i915_private *dev_priv = dev->dev_private;
9716 struct intel_crtc *intel_crtc =
9717 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9718 unsigned long flags;
9719
9720
9721 /*
9722 * This is called both by irq handlers and the reset code (to complete
9723 * lost pageflips) so needs the full irqsave spinlocks.
9724 *
9725 * NB: An MMIO update of the plane base pointer will also
9726 * generate a page-flip completion irq, i.e. every modeset
9727 * is also accompanied by a spurious intel_prepare_page_flip().
9728 */
9729 spin_lock_irqsave(&dev->event_lock, flags);
9730 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
9731 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
9732 spin_unlock_irqrestore(&dev->event_lock, flags);
9733}
9734
9735static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
9736{
9737 /* Ensure that the work item is consistent when activating it ... */
9738 smp_wmb();
9739 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9740 /* and that it is marked active as soon as the irq could fire. */
9741 smp_wmb();
9742}
9743
9744static int intel_gen2_queue_flip(struct drm_device *dev,
9745 struct drm_crtc *crtc,
9746 struct drm_framebuffer *fb,
9747 struct drm_i915_gem_object *obj,
9748 struct intel_engine_cs *ring,
9749 uint32_t flags)
9750{
9751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9752 u32 flip_mask;
9753 int ret;
9754
9755 ret = intel_ring_begin(ring, 6);
9756 if (ret)
9757 return ret;
9758
9759 /* Can't queue multiple flips, so wait for the previous
9760 * one to finish before executing the next.
9761 */
9762 if (intel_crtc->plane)
9763 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9764 else
9765 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
9766 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9767 intel_ring_emit(ring, MI_NOOP);
9768 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9769 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9770 intel_ring_emit(ring, fb->pitches[0]);
9771 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9772 intel_ring_emit(ring, 0); /* aux display base address, unused */
9773
9774 intel_mark_page_flip_active(intel_crtc);
9775 __intel_ring_advance(ring);
9776 return 0;
9777}
9778
9779static int intel_gen3_queue_flip(struct drm_device *dev,
9780 struct drm_crtc *crtc,
9781 struct drm_framebuffer *fb,
9782 struct drm_i915_gem_object *obj,
9783 struct intel_engine_cs *ring,
9784 uint32_t flags)
9785{
9786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9787 u32 flip_mask;
9788 int ret;
9789
9790 ret = intel_ring_begin(ring, 6);
9791 if (ret)
9792 return ret;
9793
9794 if (intel_crtc->plane)
9795 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9796 else
9797 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
9798 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9799 intel_ring_emit(ring, MI_NOOP);
9800 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9801 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9802 intel_ring_emit(ring, fb->pitches[0]);
9803 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9804 intel_ring_emit(ring, MI_NOOP);
9805
9806 intel_mark_page_flip_active(intel_crtc);
9807 __intel_ring_advance(ring);
9808 return 0;
9809}
9810
9811static int intel_gen4_queue_flip(struct drm_device *dev,
9812 struct drm_crtc *crtc,
9813 struct drm_framebuffer *fb,
9814 struct drm_i915_gem_object *obj,
9815 struct intel_engine_cs *ring,
9816 uint32_t flags)
9817{
9818 struct drm_i915_private *dev_priv = dev->dev_private;
9819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9820 uint32_t pf, pipesrc;
9821 int ret;
9822
9823 ret = intel_ring_begin(ring, 4);
9824 if (ret)
9825 return ret;
9826
9827 /* i965+ uses the linear or tiled offsets from the
9828 * Display Registers (which do not change across a page-flip)
9829 * so we need only reprogram the base address.
9830 */
9831 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9832 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9833 intel_ring_emit(ring, fb->pitches[0]);
9834 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
9835 obj->tiling_mode);
9836
9837 /* XXX Enabling the panel-fitter across page-flip is so far
9838 * untested on non-native modes, so ignore it for now.
9839 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9840 */
9841 pf = 0;
9842 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
9843 intel_ring_emit(ring, pf | pipesrc);
9844
9845 intel_mark_page_flip_active(intel_crtc);
9846 __intel_ring_advance(ring);
9847 return 0;
9848}
9849
9850static int intel_gen6_queue_flip(struct drm_device *dev,
9851 struct drm_crtc *crtc,
9852 struct drm_framebuffer *fb,
9853 struct drm_i915_gem_object *obj,
9854 struct intel_engine_cs *ring,
9855 uint32_t flags)
9856{
9857 struct drm_i915_private *dev_priv = dev->dev_private;
9858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9859 uint32_t pf, pipesrc;
9860 int ret;
9861
9862 ret = intel_ring_begin(ring, 4);
9863 if (ret)
9864 return ret;
9865
9866 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9867 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9868 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
9869 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9870
9871 /* Contrary to the suggestions in the documentation,
9872 * "Enable Panel Fitter" does not seem to be required when page
9873 * flipping with a non-native mode, and worse causes a normal
9874 * modeset to fail.
9875 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9876 */
9877 pf = 0;
9878 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
9879 intel_ring_emit(ring, pf | pipesrc);
9880
9881 intel_mark_page_flip_active(intel_crtc);
9882 __intel_ring_advance(ring);
9883 return 0;
9884}
9885
9886static int intel_gen7_queue_flip(struct drm_device *dev,
9887 struct drm_crtc *crtc,
9888 struct drm_framebuffer *fb,
9889 struct drm_i915_gem_object *obj,
9890 struct intel_engine_cs *ring,
9891 uint32_t flags)
9892{
9893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9894 uint32_t plane_bit = 0;
9895 int len, ret;
9896
9897 switch (intel_crtc->plane) {
9898 case PLANE_A:
9899 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9900 break;
9901 case PLANE_B:
9902 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9903 break;
9904 case PLANE_C:
9905 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9906 break;
9907 default:
9908 WARN_ONCE(1, "unknown plane in flip command\n");
9909 return -ENODEV;
9910 }
9911
9912 len = 4;
9913 if (ring->id == RCS) {
9914 len += 6;
9915 /*
9916 * On Gen 8, SRM is now taking an extra dword to accommodate
9917 * 48bits addresses, and we need a NOOP for the batch size to
9918 * stay even.
9919 */
9920 if (IS_GEN8(dev))
9921 len += 2;
9922 }
9923
9924 /*
9925 * BSpec MI_DISPLAY_FLIP for IVB:
9926 * "The full packet must be contained within the same cache line."
9927 *
9928 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9929 * cacheline, if we ever start emitting more commands before
9930 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9931 * then do the cacheline alignment, and finally emit the
9932 * MI_DISPLAY_FLIP.
9933 */
9934 ret = intel_ring_cacheline_align(ring);
9935 if (ret)
9936 return ret;
9937
9938 ret = intel_ring_begin(ring, len);
9939 if (ret)
9940 return ret;
9941
9942 /* Unmask the flip-done completion message. Note that the bspec says that
9943 * we should do this for both the BCS and RCS, and that we must not unmask
9944 * more than one flip event at any time (or ensure that one flip message
9945 * can be sent by waiting for flip-done prior to queueing new flips).
9946 * Experimentation says that BCS works despite DERRMR masking all
9947 * flip-done completion events and that unmasking all planes at once
9948 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9949 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9950 */
9951 if (ring->id == RCS) {
9952 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9953 intel_ring_emit(ring, DERRMR);
9954 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9955 DERRMR_PIPEB_PRI_FLIP_DONE |
9956 DERRMR_PIPEC_PRI_FLIP_DONE));
9957 if (IS_GEN8(dev))
9958 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9959 MI_SRM_LRM_GLOBAL_GTT);
9960 else
9961 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9962 MI_SRM_LRM_GLOBAL_GTT);
9963 intel_ring_emit(ring, DERRMR);
9964 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
9965 if (IS_GEN8(dev)) {
9966 intel_ring_emit(ring, 0);
9967 intel_ring_emit(ring, MI_NOOP);
9968 }
9969 }
9970
9971 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
9972 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
9973 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
9974 intel_ring_emit(ring, (MI_NOOP));
9975
9976 intel_mark_page_flip_active(intel_crtc);
9977 __intel_ring_advance(ring);
9978 return 0;
9979}
9980
9981static bool use_mmio_flip(struct intel_engine_cs *ring,
9982 struct drm_i915_gem_object *obj)
9983{
9984 /*
9985 * This is not being used for older platforms, because
9986 * non-availability of flip done interrupt forces us to use
9987 * CS flips. Older platforms derive flip done using some clever
9988 * tricks involving the flip_pending status bits and vblank irqs.
9989 * So using MMIO flips there would disrupt this mechanism.
9990 */
9991
9992 if (ring == NULL)
9993 return true;
9994
9995 if (INTEL_INFO(ring->dev)->gen < 5)
9996 return false;
9997
9998 if (i915.use_mmio_flip < 0)
9999 return false;
10000 else if (i915.use_mmio_flip > 0)
10001 return true;
10002 else if (i915.enable_execlists)
10003 return true;
10004 else
10005 return ring != i915_gem_request_get_ring(obj->last_read_req);
10006}
10007
10008static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10009{
10010 struct drm_device *dev = intel_crtc->base.dev;
10011 struct drm_i915_private *dev_priv = dev->dev_private;
10012 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10013 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
10014 struct drm_i915_gem_object *obj = intel_fb->obj;
10015 const enum pipe pipe = intel_crtc->pipe;
10016 u32 ctl, stride;
10017
10018 ctl = I915_READ(PLANE_CTL(pipe, 0));
10019 ctl &= ~PLANE_CTL_TILED_MASK;
10020 if (obj->tiling_mode == I915_TILING_X)
10021 ctl |= PLANE_CTL_TILED_X;
10022
10023 /*
10024 * The stride is either expressed as a multiple of 64 bytes chunks for
10025 * linear buffers or in number of tiles for tiled buffers.
10026 */
10027 stride = fb->pitches[0] >> 6;
10028 if (obj->tiling_mode == I915_TILING_X)
10029 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
10030
10031 /*
10032 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10033 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10034 */
10035 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10036 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10037
10038 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10039 POSTING_READ(PLANE_SURF(pipe, 0));
10040}
10041
10042static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
10043{
10044 struct drm_device *dev = intel_crtc->base.dev;
10045 struct drm_i915_private *dev_priv = dev->dev_private;
10046 struct intel_framebuffer *intel_fb =
10047 to_intel_framebuffer(intel_crtc->base.primary->fb);
10048 struct drm_i915_gem_object *obj = intel_fb->obj;
10049 u32 dspcntr;
10050 u32 reg;
10051
10052 reg = DSPCNTR(intel_crtc->plane);
10053 dspcntr = I915_READ(reg);
10054
10055 if (obj->tiling_mode != I915_TILING_NONE)
10056 dspcntr |= DISPPLANE_TILED;
10057 else
10058 dspcntr &= ~DISPPLANE_TILED;
10059
10060 I915_WRITE(reg, dspcntr);
10061
10062 I915_WRITE(DSPSURF(intel_crtc->plane),
10063 intel_crtc->unpin_work->gtt_offset);
10064 POSTING_READ(DSPSURF(intel_crtc->plane));
10065
10066}
10067
10068/*
10069 * XXX: This is the temporary way to update the plane registers until we get
10070 * around to using the usual plane update functions for MMIO flips
10071 */
10072static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10073{
10074 struct drm_device *dev = intel_crtc->base.dev;
10075 bool atomic_update;
10076 u32 start_vbl_count;
10077
10078 intel_mark_page_flip_active(intel_crtc);
10079
10080 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10081
10082 if (INTEL_INFO(dev)->gen >= 9)
10083 skl_do_mmio_flip(intel_crtc);
10084 else
10085 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10086 ilk_do_mmio_flip(intel_crtc);
10087
10088 if (atomic_update)
10089 intel_pipe_update_end(intel_crtc, start_vbl_count);
10090}
10091
10092static void intel_mmio_flip_work_func(struct work_struct *work)
10093{
10094 struct intel_crtc *crtc =
10095 container_of(work, struct intel_crtc, mmio_flip.work);
10096 struct intel_mmio_flip *mmio_flip;
10097
10098 mmio_flip = &crtc->mmio_flip;
10099 if (mmio_flip->req)
10100 WARN_ON(__i915_wait_request(mmio_flip->req,
10101 crtc->reset_counter,
10102 false, NULL, NULL) != 0);
10103
10104 intel_do_mmio_flip(crtc);
10105 if (mmio_flip->req) {
10106 mutex_lock(&crtc->base.dev->struct_mutex);
10107 i915_gem_request_assign(&mmio_flip->req, NULL);
10108 mutex_unlock(&crtc->base.dev->struct_mutex);
10109 }
10110}
10111
10112static int intel_queue_mmio_flip(struct drm_device *dev,
10113 struct drm_crtc *crtc,
10114 struct drm_framebuffer *fb,
10115 struct drm_i915_gem_object *obj,
10116 struct intel_engine_cs *ring,
10117 uint32_t flags)
10118{
10119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10120
10121 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10122 obj->last_write_req);
10123
10124 schedule_work(&intel_crtc->mmio_flip.work);
10125
10126 return 0;
10127}
10128
10129static int intel_default_queue_flip(struct drm_device *dev,
10130 struct drm_crtc *crtc,
10131 struct drm_framebuffer *fb,
10132 struct drm_i915_gem_object *obj,
10133 struct intel_engine_cs *ring,
10134 uint32_t flags)
10135{
10136 return -ENODEV;
10137}
10138
10139static bool __intel_pageflip_stall_check(struct drm_device *dev,
10140 struct drm_crtc *crtc)
10141{
10142 struct drm_i915_private *dev_priv = dev->dev_private;
10143 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10144 struct intel_unpin_work *work = intel_crtc->unpin_work;
10145 u32 addr;
10146
10147 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10148 return true;
10149
10150 if (!work->enable_stall_check)
10151 return false;
10152
10153 if (work->flip_ready_vblank == 0) {
10154 if (work->flip_queued_req &&
10155 !i915_gem_request_completed(work->flip_queued_req, true))
10156 return false;
10157
10158 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
10159 }
10160
10161 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
10162 return false;
10163
10164 /* Potential stall - if we see that the flip has happened,
10165 * assume a missed interrupt. */
10166 if (INTEL_INFO(dev)->gen >= 4)
10167 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10168 else
10169 addr = I915_READ(DSPADDR(intel_crtc->plane));
10170
10171 /* There is a potential issue here with a false positive after a flip
10172 * to the same address. We could address this by checking for a
10173 * non-incrementing frame counter.
10174 */
10175 return addr == work->gtt_offset;
10176}
10177
10178void intel_check_page_flip(struct drm_device *dev, int pipe)
10179{
10180 struct drm_i915_private *dev_priv = dev->dev_private;
10181 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10183 struct intel_unpin_work *work;
10184
10185 WARN_ON(!in_interrupt());
10186
10187 if (crtc == NULL)
10188 return;
10189
10190 spin_lock(&dev->event_lock);
10191 work = intel_crtc->unpin_work;
10192 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
10193 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
10194 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
10195 page_flip_completed(intel_crtc);
10196 work = NULL;
10197 }
10198 if (work != NULL &&
10199 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10200 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
10201 spin_unlock(&dev->event_lock);
10202}
10203
10204static int intel_crtc_page_flip(struct drm_crtc *crtc,
10205 struct drm_framebuffer *fb,
10206 struct drm_pending_vblank_event *event,
10207 uint32_t page_flip_flags)
10208{
10209 struct drm_device *dev = crtc->dev;
10210 struct drm_i915_private *dev_priv = dev->dev_private;
10211 struct drm_framebuffer *old_fb = crtc->primary->fb;
10212 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
10213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10214 struct drm_plane *primary = crtc->primary;
10215 enum pipe pipe = intel_crtc->pipe;
10216 struct intel_unpin_work *work;
10217 struct intel_engine_cs *ring;
10218 bool mmio_flip;
10219 int ret;
10220
10221 /*
10222 * drm_mode_page_flip_ioctl() should already catch this, but double
10223 * check to be safe. In the future we may enable pageflipping from
10224 * a disabled primary plane.
10225 */
10226 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10227 return -EBUSY;
10228
10229 /* Can't change pixel format via MI display flips. */
10230 if (fb->pixel_format != crtc->primary->fb->pixel_format)
10231 return -EINVAL;
10232
10233 /*
10234 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10235 * Note that pitch changes could also affect these register.
10236 */
10237 if (INTEL_INFO(dev)->gen > 3 &&
10238 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10239 fb->pitches[0] != crtc->primary->fb->pitches[0]))
10240 return -EINVAL;
10241
10242 if (i915_terminally_wedged(&dev_priv->gpu_error))
10243 goto out_hang;
10244
10245 work = kzalloc(sizeof(*work), GFP_KERNEL);
10246 if (work == NULL)
10247 return -ENOMEM;
10248
10249 work->event = event;
10250 work->crtc = crtc;
10251 work->old_fb = old_fb;
10252 INIT_WORK(&work->work, intel_unpin_work_fn);
10253
10254 ret = drm_crtc_vblank_get(crtc);
10255 if (ret)
10256 goto free_work;
10257
10258 /* We borrow the event spin lock for protecting unpin_work */
10259 spin_lock_irq(&dev->event_lock);
10260 if (intel_crtc->unpin_work) {
10261 /* Before declaring the flip queue wedged, check if
10262 * the hardware completed the operation behind our backs.
10263 */
10264 if (__intel_pageflip_stall_check(dev, crtc)) {
10265 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10266 page_flip_completed(intel_crtc);
10267 } else {
10268 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
10269 spin_unlock_irq(&dev->event_lock);
10270
10271 drm_crtc_vblank_put(crtc);
10272 kfree(work);
10273 return -EBUSY;
10274 }
10275 }
10276 intel_crtc->unpin_work = work;
10277 spin_unlock_irq(&dev->event_lock);
10278
10279 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10280 flush_workqueue(dev_priv->wq);
10281
10282 /* Reference the objects for the scheduled work. */
10283 drm_framebuffer_reference(work->old_fb);
10284 drm_gem_object_reference(&obj->base);
10285
10286 crtc->primary->fb = fb;
10287 update_state_fb(crtc->primary);
10288
10289 work->pending_flip_obj = obj;
10290
10291 ret = i915_mutex_lock_interruptible(dev);
10292 if (ret)
10293 goto cleanup;
10294
10295 atomic_inc(&intel_crtc->unpin_work_count);
10296 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
10297
10298 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
10299 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
10300
10301 if (IS_VALLEYVIEW(dev)) {
10302 ring = &dev_priv->ring[BCS];
10303 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
10304 /* vlv: DISPLAY_FLIP fails to change tiling */
10305 ring = NULL;
10306 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
10307 ring = &dev_priv->ring[BCS];
10308 } else if (INTEL_INFO(dev)->gen >= 7) {
10309 ring = i915_gem_request_get_ring(obj->last_read_req);
10310 if (ring == NULL || ring->id != RCS)
10311 ring = &dev_priv->ring[BCS];
10312 } else {
10313 ring = &dev_priv->ring[RCS];
10314 }
10315
10316 mmio_flip = use_mmio_flip(ring, obj);
10317
10318 /* When using CS flips, we want to emit semaphores between rings.
10319 * However, when using mmio flips we will create a task to do the
10320 * synchronisation, so all we want here is to pin the framebuffer
10321 * into the display plane and skip any waits.
10322 */
10323 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
10324 crtc->primary->state,
10325 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
10326 if (ret)
10327 goto cleanup_pending;
10328
10329 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10330 + intel_crtc->dspaddr_offset;
10331
10332 if (mmio_flip) {
10333 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10334 page_flip_flags);
10335 if (ret)
10336 goto cleanup_unpin;
10337
10338 i915_gem_request_assign(&work->flip_queued_req,
10339 obj->last_write_req);
10340 } else {
10341 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
10342 page_flip_flags);
10343 if (ret)
10344 goto cleanup_unpin;
10345
10346 i915_gem_request_assign(&work->flip_queued_req,
10347 intel_ring_get_request(ring));
10348 }
10349
10350 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
10351 work->enable_stall_check = true;
10352
10353 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
10354 INTEL_FRONTBUFFER_PRIMARY(pipe));
10355
10356 intel_fbc_disable(dev);
10357 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
10358 mutex_unlock(&dev->struct_mutex);
10359
10360 trace_i915_flip_request(intel_crtc->plane, obj);
10361
10362 return 0;
10363
10364cleanup_unpin:
10365 intel_unpin_fb_obj(fb, crtc->primary->state);
10366cleanup_pending:
10367 atomic_dec(&intel_crtc->unpin_work_count);
10368 mutex_unlock(&dev->struct_mutex);
10369cleanup:
10370 crtc->primary->fb = old_fb;
10371 update_state_fb(crtc->primary);
10372
10373 drm_gem_object_unreference_unlocked(&obj->base);
10374 drm_framebuffer_unreference(work->old_fb);
10375
10376 spin_lock_irq(&dev->event_lock);
10377 intel_crtc->unpin_work = NULL;
10378 spin_unlock_irq(&dev->event_lock);
10379
10380 drm_crtc_vblank_put(crtc);
10381free_work:
10382 kfree(work);
10383
10384 if (ret == -EIO) {
10385out_hang:
10386 ret = intel_plane_restore(primary);
10387 if (ret == 0 && event) {
10388 spin_lock_irq(&dev->event_lock);
10389 drm_send_vblank_event(dev, pipe, event);
10390 spin_unlock_irq(&dev->event_lock);
10391 }
10392 }
10393 return ret;
10394}
10395
10396static struct drm_crtc_helper_funcs intel_helper_funcs = {
10397 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10398 .load_lut = intel_crtc_load_lut,
10399 .atomic_begin = intel_begin_crtc_commit,
10400 .atomic_flush = intel_finish_crtc_commit,
10401};
10402
10403/**
10404 * intel_modeset_update_staged_output_state
10405 *
10406 * Updates the staged output configuration state, e.g. after we've read out the
10407 * current hw state.
10408 */
10409static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10410{
10411 struct intel_crtc *crtc;
10412 struct intel_encoder *encoder;
10413 struct intel_connector *connector;
10414
10415 for_each_intel_connector(dev, connector) {
10416 connector->new_encoder =
10417 to_intel_encoder(connector->base.encoder);
10418 }
10419
10420 for_each_intel_encoder(dev, encoder) {
10421 encoder->new_crtc =
10422 to_intel_crtc(encoder->base.crtc);
10423 }
10424
10425 for_each_intel_crtc(dev, crtc) {
10426 crtc->new_enabled = crtc->base.state->enable;
10427 }
10428}
10429
10430/* Transitional helper to copy current connector/encoder state to
10431 * connector->state. This is needed so that code that is partially
10432 * converted to atomic does the right thing.
10433 */
10434static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10435{
10436 struct intel_connector *connector;
10437
10438 for_each_intel_connector(dev, connector) {
10439 if (connector->base.encoder) {
10440 connector->base.state->best_encoder =
10441 connector->base.encoder;
10442 connector->base.state->crtc =
10443 connector->base.encoder->crtc;
10444 } else {
10445 connector->base.state->best_encoder = NULL;
10446 connector->base.state->crtc = NULL;
10447 }
10448 }
10449}
10450
10451/**
10452 * intel_modeset_commit_output_state
10453 *
10454 * This function copies the stage display pipe configuration to the real one.
10455 */
10456static void intel_modeset_commit_output_state(struct drm_device *dev)
10457{
10458 struct intel_crtc *crtc;
10459 struct intel_encoder *encoder;
10460 struct intel_connector *connector;
10461
10462 for_each_intel_connector(dev, connector) {
10463 connector->base.encoder = &connector->new_encoder->base;
10464 }
10465
10466 for_each_intel_encoder(dev, encoder) {
10467 encoder->base.crtc = &encoder->new_crtc->base;
10468 }
10469
10470 for_each_intel_crtc(dev, crtc) {
10471 crtc->base.state->enable = crtc->new_enabled;
10472 crtc->base.enabled = crtc->new_enabled;
10473 }
10474
10475 intel_modeset_update_connector_atomic_state(dev);
10476}
10477
10478static void
10479connected_sink_compute_bpp(struct intel_connector *connector,
10480 struct intel_crtc_state *pipe_config)
10481{
10482 int bpp = pipe_config->pipe_bpp;
10483
10484 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10485 connector->base.base.id,
10486 connector->base.name);
10487
10488 /* Don't use an invalid EDID bpc value */
10489 if (connector->base.display_info.bpc &&
10490 connector->base.display_info.bpc * 3 < bpp) {
10491 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10492 bpp, connector->base.display_info.bpc*3);
10493 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10494 }
10495
10496 /* Clamp bpp to 8 on screens without EDID 1.4 */
10497 if (connector->base.display_info.bpc == 0 && bpp > 24) {
10498 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
10499 bpp);
10500 pipe_config->pipe_bpp = 24;
10501 }
10502}
10503
10504static int
10505compute_baseline_pipe_bpp(struct intel_crtc *crtc,
10506 struct drm_framebuffer *fb,
10507 struct intel_crtc_state *pipe_config)
10508{
10509 struct drm_device *dev = crtc->base.dev;
10510 struct drm_atomic_state *state;
10511 struct intel_connector *connector;
10512 int bpp, i;
10513
10514 switch (fb->pixel_format) {
10515 case DRM_FORMAT_C8:
10516 bpp = 8*3; /* since we go through a colormap */
10517 break;
10518 case DRM_FORMAT_XRGB1555:
10519 case DRM_FORMAT_ARGB1555:
10520 /* checked in intel_framebuffer_init already */
10521 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
10522 return -EINVAL;
10523 case DRM_FORMAT_RGB565:
10524 bpp = 6*3; /* min is 18bpp */
10525 break;
10526 case DRM_FORMAT_XBGR8888:
10527 case DRM_FORMAT_ABGR8888:
10528 /* checked in intel_framebuffer_init already */
10529 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10530 return -EINVAL;
10531 case DRM_FORMAT_XRGB8888:
10532 case DRM_FORMAT_ARGB8888:
10533 bpp = 8*3;
10534 break;
10535 case DRM_FORMAT_XRGB2101010:
10536 case DRM_FORMAT_ARGB2101010:
10537 case DRM_FORMAT_XBGR2101010:
10538 case DRM_FORMAT_ABGR2101010:
10539 /* checked in intel_framebuffer_init already */
10540 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
10541 return -EINVAL;
10542 bpp = 10*3;
10543 break;
10544 /* TODO: gen4+ supports 16 bpc floating point, too. */
10545 default:
10546 DRM_DEBUG_KMS("unsupported depth\n");
10547 return -EINVAL;
10548 }
10549
10550 pipe_config->pipe_bpp = bpp;
10551
10552 state = pipe_config->base.state;
10553
10554 /* Clamp display bpp to EDID value */
10555 for (i = 0; i < state->num_connector; i++) {
10556 if (!state->connectors[i])
10557 continue;
10558
10559 connector = to_intel_connector(state->connectors[i]);
10560 if (state->connector_states[i]->crtc != &crtc->base)
10561 continue;
10562
10563 connected_sink_compute_bpp(connector, pipe_config);
10564 }
10565
10566 return bpp;
10567}
10568
10569static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
10570{
10571 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
10572 "type: 0x%x flags: 0x%x\n",
10573 mode->crtc_clock,
10574 mode->crtc_hdisplay, mode->crtc_hsync_start,
10575 mode->crtc_hsync_end, mode->crtc_htotal,
10576 mode->crtc_vdisplay, mode->crtc_vsync_start,
10577 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
10578}
10579
10580static void intel_dump_pipe_config(struct intel_crtc *crtc,
10581 struct intel_crtc_state *pipe_config,
10582 const char *context)
10583{
10584 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
10585 context, pipe_name(crtc->pipe));
10586
10587 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
10588 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
10589 pipe_config->pipe_bpp, pipe_config->dither);
10590 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10591 pipe_config->has_pch_encoder,
10592 pipe_config->fdi_lanes,
10593 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
10594 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
10595 pipe_config->fdi_m_n.tu);
10596 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
10597 pipe_config->has_dp_encoder,
10598 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
10599 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
10600 pipe_config->dp_m_n.tu);
10601
10602 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
10603 pipe_config->has_dp_encoder,
10604 pipe_config->dp_m2_n2.gmch_m,
10605 pipe_config->dp_m2_n2.gmch_n,
10606 pipe_config->dp_m2_n2.link_m,
10607 pipe_config->dp_m2_n2.link_n,
10608 pipe_config->dp_m2_n2.tu);
10609
10610 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
10611 pipe_config->has_audio,
10612 pipe_config->has_infoframe);
10613
10614 DRM_DEBUG_KMS("requested mode:\n");
10615 drm_mode_debug_printmodeline(&pipe_config->base.mode);
10616 DRM_DEBUG_KMS("adjusted mode:\n");
10617 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
10618 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
10619 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
10620 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
10621 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
10622 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
10623 pipe_config->gmch_pfit.control,
10624 pipe_config->gmch_pfit.pgm_ratios,
10625 pipe_config->gmch_pfit.lvds_border_bits);
10626 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
10627 pipe_config->pch_pfit.pos,
10628 pipe_config->pch_pfit.size,
10629 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
10630 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
10631 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
10632}
10633
10634static bool encoders_cloneable(const struct intel_encoder *a,
10635 const struct intel_encoder *b)
10636{
10637 /* masks could be asymmetric, so check both ways */
10638 return a == b || (a->cloneable & (1 << b->type) &&
10639 b->cloneable & (1 << a->type));
10640}
10641
10642static bool check_single_encoder_cloning(struct drm_atomic_state *state,
10643 struct intel_crtc *crtc,
10644 struct intel_encoder *encoder)
10645{
10646 struct intel_encoder *source_encoder;
10647 struct drm_connector_state *connector_state;
10648 int i;
10649
10650 for (i = 0; i < state->num_connector; i++) {
10651 if (!state->connectors[i])
10652 continue;
10653
10654 connector_state = state->connector_states[i];
10655 if (connector_state->crtc != &crtc->base)
10656 continue;
10657
10658 source_encoder =
10659 to_intel_encoder(connector_state->best_encoder);
10660 if (!encoders_cloneable(encoder, source_encoder))
10661 return false;
10662 }
10663
10664 return true;
10665}
10666
10667static bool check_encoder_cloning(struct drm_atomic_state *state,
10668 struct intel_crtc *crtc)
10669{
10670 struct intel_encoder *encoder;
10671 struct drm_connector_state *connector_state;
10672 int i;
10673
10674 for (i = 0; i < state->num_connector; i++) {
10675 if (!state->connectors[i])
10676 continue;
10677
10678 connector_state = state->connector_states[i];
10679 if (connector_state->crtc != &crtc->base)
10680 continue;
10681
10682 encoder = to_intel_encoder(connector_state->best_encoder);
10683 if (!check_single_encoder_cloning(state, crtc, encoder))
10684 return false;
10685 }
10686
10687 return true;
10688}
10689
10690static bool check_digital_port_conflicts(struct drm_atomic_state *state)
10691{
10692 struct drm_device *dev = state->dev;
10693 struct intel_encoder *encoder;
10694 struct drm_connector_state *connector_state;
10695 unsigned int used_ports = 0;
10696 int i;
10697
10698 /*
10699 * Walk the connector list instead of the encoder
10700 * list to detect the problem on ddi platforms
10701 * where there's just one encoder per digital port.
10702 */
10703 for (i = 0; i < state->num_connector; i++) {
10704 if (!state->connectors[i])
10705 continue;
10706
10707 connector_state = state->connector_states[i];
10708 if (!connector_state->best_encoder)
10709 continue;
10710
10711 encoder = to_intel_encoder(connector_state->best_encoder);
10712
10713 WARN_ON(!connector_state->crtc);
10714
10715 switch (encoder->type) {
10716 unsigned int port_mask;
10717 case INTEL_OUTPUT_UNKNOWN:
10718 if (WARN_ON(!HAS_DDI(dev)))
10719 break;
10720 case INTEL_OUTPUT_DISPLAYPORT:
10721 case INTEL_OUTPUT_HDMI:
10722 case INTEL_OUTPUT_EDP:
10723 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
10724
10725 /* the same port mustn't appear more than once */
10726 if (used_ports & port_mask)
10727 return false;
10728
10729 used_ports |= port_mask;
10730 default:
10731 break;
10732 }
10733 }
10734
10735 return true;
10736}
10737
10738static void
10739clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
10740{
10741 struct drm_crtc_state tmp_state;
10742
10743 /* Clear only the intel specific part of the crtc state */
10744 tmp_state = crtc_state->base;
10745 memset(crtc_state, 0, sizeof *crtc_state);
10746 crtc_state->base = tmp_state;
10747}
10748
10749static struct intel_crtc_state *
10750intel_modeset_pipe_config(struct drm_crtc *crtc,
10751 struct drm_framebuffer *fb,
10752 struct drm_display_mode *mode,
10753 struct drm_atomic_state *state)
10754{
10755 struct intel_encoder *encoder;
10756 struct intel_connector *connector;
10757 struct drm_connector_state *connector_state;
10758 struct intel_crtc_state *pipe_config;
10759 int plane_bpp, ret = -EINVAL;
10760 int i;
10761 bool retry = true;
10762
10763 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
10764 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
10765 return ERR_PTR(-EINVAL);
10766 }
10767
10768 if (!check_digital_port_conflicts(state)) {
10769 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
10770 return ERR_PTR(-EINVAL);
10771 }
10772
10773 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
10774 if (IS_ERR(pipe_config))
10775 return pipe_config;
10776
10777 clear_intel_crtc_state(pipe_config);
10778
10779 pipe_config->base.crtc = crtc;
10780 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
10781 drm_mode_copy(&pipe_config->base.mode, mode);
10782
10783 pipe_config->cpu_transcoder =
10784 (enum transcoder) to_intel_crtc(crtc)->pipe;
10785 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
10786
10787 /*
10788 * Sanitize sync polarity flags based on requested ones. If neither
10789 * positive or negative polarity is requested, treat this as meaning
10790 * negative polarity.
10791 */
10792 if (!(pipe_config->base.adjusted_mode.flags &
10793 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
10794 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
10795
10796 if (!(pipe_config->base.adjusted_mode.flags &
10797 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
10798 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
10799
10800 /* Compute a starting value for pipe_config->pipe_bpp taking the source
10801 * plane pixel format and any sink constraints into account. Returns the
10802 * source plane bpp so that dithering can be selected on mismatches
10803 * after encoders and crtc also have had their say. */
10804 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
10805 fb, pipe_config);
10806 if (plane_bpp < 0)
10807 goto fail;
10808
10809 /*
10810 * Determine the real pipe dimensions. Note that stereo modes can
10811 * increase the actual pipe size due to the frame doubling and
10812 * insertion of additional space for blanks between the frame. This
10813 * is stored in the crtc timings. We use the requested mode to do this
10814 * computation to clearly distinguish it from the adjusted mode, which
10815 * can be changed by the connectors in the below retry loop.
10816 */
10817 drm_crtc_get_hv_timing(&pipe_config->base.mode,
10818 &pipe_config->pipe_src_w,
10819 &pipe_config->pipe_src_h);
10820
10821encoder_retry:
10822 /* Ensure the port clock defaults are reset when retrying. */
10823 pipe_config->port_clock = 0;
10824 pipe_config->pixel_multiplier = 1;
10825
10826 /* Fill in default crtc timings, allow encoders to overwrite them. */
10827 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
10828 CRTC_STEREO_DOUBLE);
10829
10830 /* Pass our mode to the connectors and the CRTC to give them a chance to
10831 * adjust it according to limitations or connector properties, and also
10832 * a chance to reject the mode entirely.
10833 */
10834 for (i = 0; i < state->num_connector; i++) {
10835 connector = to_intel_connector(state->connectors[i]);
10836 if (!connector)
10837 continue;
10838
10839 connector_state = state->connector_states[i];
10840 if (connector_state->crtc != crtc)
10841 continue;
10842
10843 encoder = to_intel_encoder(connector_state->best_encoder);
10844
10845 if (!(encoder->compute_config(encoder, pipe_config))) {
10846 DRM_DEBUG_KMS("Encoder config failure\n");
10847 goto fail;
10848 }
10849 }
10850
10851 /* Set default port clock if not overwritten by the encoder. Needs to be
10852 * done afterwards in case the encoder adjusts the mode. */
10853 if (!pipe_config->port_clock)
10854 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
10855 * pipe_config->pixel_multiplier;
10856
10857 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
10858 if (ret < 0) {
10859 DRM_DEBUG_KMS("CRTC fixup failed\n");
10860 goto fail;
10861 }
10862
10863 if (ret == RETRY) {
10864 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10865 ret = -EINVAL;
10866 goto fail;
10867 }
10868
10869 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10870 retry = false;
10871 goto encoder_retry;
10872 }
10873
10874 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10875 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10876 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10877
10878 return pipe_config;
10879fail:
10880 return ERR_PTR(ret);
10881}
10882
10883/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10884 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10885static void
10886intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10887 unsigned *prepare_pipes, unsigned *disable_pipes)
10888{
10889 struct intel_crtc *intel_crtc;
10890 struct drm_device *dev = crtc->dev;
10891 struct intel_encoder *encoder;
10892 struct intel_connector *connector;
10893 struct drm_crtc *tmp_crtc;
10894
10895 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10896
10897 /* Check which crtcs have changed outputs connected to them, these need
10898 * to be part of the prepare_pipes mask. We don't (yet) support global
10899 * modeset across multiple crtcs, so modeset_pipes will only have one
10900 * bit set at most. */
10901 for_each_intel_connector(dev, connector) {
10902 if (connector->base.encoder == &connector->new_encoder->base)
10903 continue;
10904
10905 if (connector->base.encoder) {
10906 tmp_crtc = connector->base.encoder->crtc;
10907
10908 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10909 }
10910
10911 if (connector->new_encoder)
10912 *prepare_pipes |=
10913 1 << connector->new_encoder->new_crtc->pipe;
10914 }
10915
10916 for_each_intel_encoder(dev, encoder) {
10917 if (encoder->base.crtc == &encoder->new_crtc->base)
10918 continue;
10919
10920 if (encoder->base.crtc) {
10921 tmp_crtc = encoder->base.crtc;
10922
10923 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10924 }
10925
10926 if (encoder->new_crtc)
10927 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10928 }
10929
10930 /* Check for pipes that will be enabled/disabled ... */
10931 for_each_intel_crtc(dev, intel_crtc) {
10932 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
10933 continue;
10934
10935 if (!intel_crtc->new_enabled)
10936 *disable_pipes |= 1 << intel_crtc->pipe;
10937 else
10938 *prepare_pipes |= 1 << intel_crtc->pipe;
10939 }
10940
10941
10942 /* set_mode is also used to update properties on life display pipes. */
10943 intel_crtc = to_intel_crtc(crtc);
10944 if (intel_crtc->new_enabled)
10945 *prepare_pipes |= 1 << intel_crtc->pipe;
10946
10947 /*
10948 * For simplicity do a full modeset on any pipe where the output routing
10949 * changed. We could be more clever, but that would require us to be
10950 * more careful with calling the relevant encoder->mode_set functions.
10951 */
10952 if (*prepare_pipes)
10953 *modeset_pipes = *prepare_pipes;
10954
10955 /* ... and mask these out. */
10956 *modeset_pipes &= ~(*disable_pipes);
10957 *prepare_pipes &= ~(*disable_pipes);
10958
10959 /*
10960 * HACK: We don't (yet) fully support global modesets. intel_set_config
10961 * obies this rule, but the modeset restore mode of
10962 * intel_modeset_setup_hw_state does not.
10963 */
10964 *modeset_pipes &= 1 << intel_crtc->pipe;
10965 *prepare_pipes &= 1 << intel_crtc->pipe;
10966
10967 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10968 *modeset_pipes, *prepare_pipes, *disable_pipes);
10969}
10970
10971static bool intel_crtc_in_use(struct drm_crtc *crtc)
10972{
10973 struct drm_encoder *encoder;
10974 struct drm_device *dev = crtc->dev;
10975
10976 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10977 if (encoder->crtc == crtc)
10978 return true;
10979
10980 return false;
10981}
10982
10983static void
10984intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10985{
10986 struct drm_i915_private *dev_priv = dev->dev_private;
10987 struct intel_encoder *intel_encoder;
10988 struct intel_crtc *intel_crtc;
10989 struct drm_connector *connector;
10990
10991 intel_shared_dpll_commit(dev_priv);
10992
10993 for_each_intel_encoder(dev, intel_encoder) {
10994 if (!intel_encoder->base.crtc)
10995 continue;
10996
10997 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10998
10999 if (prepare_pipes & (1 << intel_crtc->pipe))
11000 intel_encoder->connectors_active = false;
11001 }
11002
11003 intel_modeset_commit_output_state(dev);
11004
11005 /* Double check state. */
11006 for_each_intel_crtc(dev, intel_crtc) {
11007 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
11008 }
11009
11010 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11011 if (!connector->encoder || !connector->encoder->crtc)
11012 continue;
11013
11014 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11015
11016 if (prepare_pipes & (1 << intel_crtc->pipe)) {
11017 struct drm_property *dpms_property =
11018 dev->mode_config.dpms_property;
11019
11020 connector->dpms = DRM_MODE_DPMS_ON;
11021 drm_object_property_set_value(&connector->base,
11022 dpms_property,
11023 DRM_MODE_DPMS_ON);
11024
11025 intel_encoder = to_intel_encoder(connector->encoder);
11026 intel_encoder->connectors_active = true;
11027 }
11028 }
11029
11030}
11031
11032static bool intel_fuzzy_clock_check(int clock1, int clock2)
11033{
11034 int diff;
11035
11036 if (clock1 == clock2)
11037 return true;
11038
11039 if (!clock1 || !clock2)
11040 return false;
11041
11042 diff = abs(clock1 - clock2);
11043
11044 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11045 return true;
11046
11047 return false;
11048}
11049
11050#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11051 list_for_each_entry((intel_crtc), \
11052 &(dev)->mode_config.crtc_list, \
11053 base.head) \
11054 if (mask & (1 <<(intel_crtc)->pipe))
11055
11056static bool
11057intel_pipe_config_compare(struct drm_device *dev,
11058 struct intel_crtc_state *current_config,
11059 struct intel_crtc_state *pipe_config)
11060{
11061#define PIPE_CONF_CHECK_X(name) \
11062 if (current_config->name != pipe_config->name) { \
11063 DRM_ERROR("mismatch in " #name " " \
11064 "(expected 0x%08x, found 0x%08x)\n", \
11065 current_config->name, \
11066 pipe_config->name); \
11067 return false; \
11068 }
11069
11070#define PIPE_CONF_CHECK_I(name) \
11071 if (current_config->name != pipe_config->name) { \
11072 DRM_ERROR("mismatch in " #name " " \
11073 "(expected %i, found %i)\n", \
11074 current_config->name, \
11075 pipe_config->name); \
11076 return false; \
11077 }
11078
11079/* This is required for BDW+ where there is only one set of registers for
11080 * switching between high and low RR.
11081 * This macro can be used whenever a comparison has to be made between one
11082 * hw state and multiple sw state variables.
11083 */
11084#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11085 if ((current_config->name != pipe_config->name) && \
11086 (current_config->alt_name != pipe_config->name)) { \
11087 DRM_ERROR("mismatch in " #name " " \
11088 "(expected %i or %i, found %i)\n", \
11089 current_config->name, \
11090 current_config->alt_name, \
11091 pipe_config->name); \
11092 return false; \
11093 }
11094
11095#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11096 if ((current_config->name ^ pipe_config->name) & (mask)) { \
11097 DRM_ERROR("mismatch in " #name "(" #mask ") " \
11098 "(expected %i, found %i)\n", \
11099 current_config->name & (mask), \
11100 pipe_config->name & (mask)); \
11101 return false; \
11102 }
11103
11104#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11105 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11106 DRM_ERROR("mismatch in " #name " " \
11107 "(expected %i, found %i)\n", \
11108 current_config->name, \
11109 pipe_config->name); \
11110 return false; \
11111 }
11112
11113#define PIPE_CONF_QUIRK(quirk) \
11114 ((current_config->quirks | pipe_config->quirks) & (quirk))
11115
11116 PIPE_CONF_CHECK_I(cpu_transcoder);
11117
11118 PIPE_CONF_CHECK_I(has_pch_encoder);
11119 PIPE_CONF_CHECK_I(fdi_lanes);
11120 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11121 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11122 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11123 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11124 PIPE_CONF_CHECK_I(fdi_m_n.tu);
11125
11126 PIPE_CONF_CHECK_I(has_dp_encoder);
11127
11128 if (INTEL_INFO(dev)->gen < 8) {
11129 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11130 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11131 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11132 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11133 PIPE_CONF_CHECK_I(dp_m_n.tu);
11134
11135 if (current_config->has_drrs) {
11136 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11137 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11138 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11139 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11140 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11141 }
11142 } else {
11143 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11144 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11145 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11146 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11147 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11148 }
11149
11150 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11151 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11152 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11153 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11154 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11155 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
11156
11157 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11158 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11159 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11160 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11161 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11162 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
11163
11164 PIPE_CONF_CHECK_I(pixel_multiplier);
11165 PIPE_CONF_CHECK_I(has_hdmi_sink);
11166 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11167 IS_VALLEYVIEW(dev))
11168 PIPE_CONF_CHECK_I(limited_color_range);
11169 PIPE_CONF_CHECK_I(has_infoframe);
11170
11171 PIPE_CONF_CHECK_I(has_audio);
11172
11173 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
11174 DRM_MODE_FLAG_INTERLACE);
11175
11176 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
11177 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
11178 DRM_MODE_FLAG_PHSYNC);
11179 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
11180 DRM_MODE_FLAG_NHSYNC);
11181 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
11182 DRM_MODE_FLAG_PVSYNC);
11183 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
11184 DRM_MODE_FLAG_NVSYNC);
11185 }
11186
11187 PIPE_CONF_CHECK_I(pipe_src_w);
11188 PIPE_CONF_CHECK_I(pipe_src_h);
11189
11190 /*
11191 * FIXME: BIOS likes to set up a cloned config with lvds+external
11192 * screen. Since we don't yet re-compute the pipe config when moving
11193 * just the lvds port away to another pipe the sw tracking won't match.
11194 *
11195 * Proper atomic modesets with recomputed global state will fix this.
11196 * Until then just don't check gmch state for inherited modes.
11197 */
11198 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11199 PIPE_CONF_CHECK_I(gmch_pfit.control);
11200 /* pfit ratios are autocomputed by the hw on gen4+ */
11201 if (INTEL_INFO(dev)->gen < 4)
11202 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11203 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11204 }
11205
11206 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11207 if (current_config->pch_pfit.enabled) {
11208 PIPE_CONF_CHECK_I(pch_pfit.pos);
11209 PIPE_CONF_CHECK_I(pch_pfit.size);
11210 }
11211
11212 /* BDW+ don't expose a synchronous way to read the state */
11213 if (IS_HASWELL(dev))
11214 PIPE_CONF_CHECK_I(ips_enabled);
11215
11216 PIPE_CONF_CHECK_I(double_wide);
11217
11218 PIPE_CONF_CHECK_X(ddi_pll_sel);
11219
11220 PIPE_CONF_CHECK_I(shared_dpll);
11221 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
11222 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
11223 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11224 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
11225 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
11226 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11227 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11228 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
11229
11230 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11231 PIPE_CONF_CHECK_I(pipe_bpp);
11232
11233 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
11234 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
11235
11236#undef PIPE_CONF_CHECK_X
11237#undef PIPE_CONF_CHECK_I
11238#undef PIPE_CONF_CHECK_I_ALT
11239#undef PIPE_CONF_CHECK_FLAGS
11240#undef PIPE_CONF_CHECK_CLOCK_FUZZY
11241#undef PIPE_CONF_QUIRK
11242
11243 return true;
11244}
11245
11246static void check_wm_state(struct drm_device *dev)
11247{
11248 struct drm_i915_private *dev_priv = dev->dev_private;
11249 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11250 struct intel_crtc *intel_crtc;
11251 int plane;
11252
11253 if (INTEL_INFO(dev)->gen < 9)
11254 return;
11255
11256 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11257 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11258
11259 for_each_intel_crtc(dev, intel_crtc) {
11260 struct skl_ddb_entry *hw_entry, *sw_entry;
11261 const enum pipe pipe = intel_crtc->pipe;
11262
11263 if (!intel_crtc->active)
11264 continue;
11265
11266 /* planes */
11267 for_each_plane(dev_priv, pipe, plane) {
11268 hw_entry = &hw_ddb.plane[pipe][plane];
11269 sw_entry = &sw_ddb->plane[pipe][plane];
11270
11271 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11272 continue;
11273
11274 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11275 "(expected (%u,%u), found (%u,%u))\n",
11276 pipe_name(pipe), plane + 1,
11277 sw_entry->start, sw_entry->end,
11278 hw_entry->start, hw_entry->end);
11279 }
11280
11281 /* cursor */
11282 hw_entry = &hw_ddb.cursor[pipe];
11283 sw_entry = &sw_ddb->cursor[pipe];
11284
11285 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11286 continue;
11287
11288 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11289 "(expected (%u,%u), found (%u,%u))\n",
11290 pipe_name(pipe),
11291 sw_entry->start, sw_entry->end,
11292 hw_entry->start, hw_entry->end);
11293 }
11294}
11295
11296static void
11297check_connector_state(struct drm_device *dev)
11298{
11299 struct intel_connector *connector;
11300
11301 for_each_intel_connector(dev, connector) {
11302 /* This also checks the encoder/connector hw state with the
11303 * ->get_hw_state callbacks. */
11304 intel_connector_check_state(connector);
11305
11306 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
11307 "connector's staged encoder doesn't match current encoder\n");
11308 }
11309}
11310
11311static void
11312check_encoder_state(struct drm_device *dev)
11313{
11314 struct intel_encoder *encoder;
11315 struct intel_connector *connector;
11316
11317 for_each_intel_encoder(dev, encoder) {
11318 bool enabled = false;
11319 bool active = false;
11320 enum pipe pipe, tracked_pipe;
11321
11322 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11323 encoder->base.base.id,
11324 encoder->base.name);
11325
11326 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
11327 "encoder's stage crtc doesn't match current crtc\n");
11328 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
11329 "encoder's active_connectors set, but no crtc\n");
11330
11331 for_each_intel_connector(dev, connector) {
11332 if (connector->base.encoder != &encoder->base)
11333 continue;
11334 enabled = true;
11335 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11336 active = true;
11337 }
11338 /*
11339 * for MST connectors if we unplug the connector is gone
11340 * away but the encoder is still connected to a crtc
11341 * until a modeset happens in response to the hotplug.
11342 */
11343 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11344 continue;
11345
11346 I915_STATE_WARN(!!encoder->base.crtc != enabled,
11347 "encoder's enabled state mismatch "
11348 "(expected %i, found %i)\n",
11349 !!encoder->base.crtc, enabled);
11350 I915_STATE_WARN(active && !encoder->base.crtc,
11351 "active encoder with no crtc\n");
11352
11353 I915_STATE_WARN(encoder->connectors_active != active,
11354 "encoder's computed active state doesn't match tracked active state "
11355 "(expected %i, found %i)\n", active, encoder->connectors_active);
11356
11357 active = encoder->get_hw_state(encoder, &pipe);
11358 I915_STATE_WARN(active != encoder->connectors_active,
11359 "encoder's hw state doesn't match sw tracking "
11360 "(expected %i, found %i)\n",
11361 encoder->connectors_active, active);
11362
11363 if (!encoder->base.crtc)
11364 continue;
11365
11366 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
11367 I915_STATE_WARN(active && pipe != tracked_pipe,
11368 "active encoder's pipe doesn't match"
11369 "(expected %i, found %i)\n",
11370 tracked_pipe, pipe);
11371
11372 }
11373}
11374
11375static void
11376check_crtc_state(struct drm_device *dev)
11377{
11378 struct drm_i915_private *dev_priv = dev->dev_private;
11379 struct intel_crtc *crtc;
11380 struct intel_encoder *encoder;
11381 struct intel_crtc_state pipe_config;
11382
11383 for_each_intel_crtc(dev, crtc) {
11384 bool enabled = false;
11385 bool active = false;
11386
11387 memset(&pipe_config, 0, sizeof(pipe_config));
11388
11389 DRM_DEBUG_KMS("[CRTC:%d]\n",
11390 crtc->base.base.id);
11391
11392 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
11393 "active crtc, but not enabled in sw tracking\n");
11394
11395 for_each_intel_encoder(dev, encoder) {
11396 if (encoder->base.crtc != &crtc->base)
11397 continue;
11398 enabled = true;
11399 if (encoder->connectors_active)
11400 active = true;
11401 }
11402
11403 I915_STATE_WARN(active != crtc->active,
11404 "crtc's computed active state doesn't match tracked active state "
11405 "(expected %i, found %i)\n", active, crtc->active);
11406 I915_STATE_WARN(enabled != crtc->base.state->enable,
11407 "crtc's computed enabled state doesn't match tracked enabled state "
11408 "(expected %i, found %i)\n", enabled,
11409 crtc->base.state->enable);
11410
11411 active = dev_priv->display.get_pipe_config(crtc,
11412 &pipe_config);
11413
11414 /* hw state is inconsistent with the pipe quirk */
11415 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11416 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
11417 active = crtc->active;
11418
11419 for_each_intel_encoder(dev, encoder) {
11420 enum pipe pipe;
11421 if (encoder->base.crtc != &crtc->base)
11422 continue;
11423 if (encoder->get_hw_state(encoder, &pipe))
11424 encoder->get_config(encoder, &pipe_config);
11425 }
11426
11427 I915_STATE_WARN(crtc->active != active,
11428 "crtc active state doesn't match with hw state "
11429 "(expected %i, found %i)\n", crtc->active, active);
11430
11431 if (active &&
11432 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
11433 I915_STATE_WARN(1, "pipe state doesn't match!\n");
11434 intel_dump_pipe_config(crtc, &pipe_config,
11435 "[hw state]");
11436 intel_dump_pipe_config(crtc, crtc->config,
11437 "[sw state]");
11438 }
11439 }
11440}
11441
11442static void
11443check_shared_dpll_state(struct drm_device *dev)
11444{
11445 struct drm_i915_private *dev_priv = dev->dev_private;
11446 struct intel_crtc *crtc;
11447 struct intel_dpll_hw_state dpll_hw_state;
11448 int i;
11449
11450 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11451 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11452 int enabled_crtcs = 0, active_crtcs = 0;
11453 bool active;
11454
11455 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11456
11457 DRM_DEBUG_KMS("%s\n", pll->name);
11458
11459 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
11460
11461 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
11462 "more active pll users than references: %i vs %i\n",
11463 pll->active, hweight32(pll->config.crtc_mask));
11464 I915_STATE_WARN(pll->active && !pll->on,
11465 "pll in active use but not on in sw tracking\n");
11466 I915_STATE_WARN(pll->on && !pll->active,
11467 "pll in on but not on in use in sw tracking\n");
11468 I915_STATE_WARN(pll->on != active,
11469 "pll on state mismatch (expected %i, found %i)\n",
11470 pll->on, active);
11471
11472 for_each_intel_crtc(dev, crtc) {
11473 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
11474 enabled_crtcs++;
11475 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11476 active_crtcs++;
11477 }
11478 I915_STATE_WARN(pll->active != active_crtcs,
11479 "pll active crtcs mismatch (expected %i, found %i)\n",
11480 pll->active, active_crtcs);
11481 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
11482 "pll enabled crtcs mismatch (expected %i, found %i)\n",
11483 hweight32(pll->config.crtc_mask), enabled_crtcs);
11484
11485 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
11486 sizeof(dpll_hw_state)),
11487 "pll hw state mismatch\n");
11488 }
11489}
11490
11491void
11492intel_modeset_check_state(struct drm_device *dev)
11493{
11494 check_wm_state(dev);
11495 check_connector_state(dev);
11496 check_encoder_state(dev);
11497 check_crtc_state(dev);
11498 check_shared_dpll_state(dev);
11499}
11500
11501void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
11502 int dotclock)
11503{
11504 /*
11505 * FDI already provided one idea for the dotclock.
11506 * Yell if the encoder disagrees.
11507 */
11508 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
11509 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
11510 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
11511}
11512
11513static void update_scanline_offset(struct intel_crtc *crtc)
11514{
11515 struct drm_device *dev = crtc->base.dev;
11516
11517 /*
11518 * The scanline counter increments at the leading edge of hsync.
11519 *
11520 * On most platforms it starts counting from vtotal-1 on the
11521 * first active line. That means the scanline counter value is
11522 * always one less than what we would expect. Ie. just after
11523 * start of vblank, which also occurs at start of hsync (on the
11524 * last active line), the scanline counter will read vblank_start-1.
11525 *
11526 * On gen2 the scanline counter starts counting from 1 instead
11527 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
11528 * to keep the value positive), instead of adding one.
11529 *
11530 * On HSW+ the behaviour of the scanline counter depends on the output
11531 * type. For DP ports it behaves like most other platforms, but on HDMI
11532 * there's an extra 1 line difference. So we need to add two instead of
11533 * one to the value.
11534 */
11535 if (IS_GEN2(dev)) {
11536 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
11537 int vtotal;
11538
11539 vtotal = mode->crtc_vtotal;
11540 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
11541 vtotal /= 2;
11542
11543 crtc->scanline_offset = vtotal - 1;
11544 } else if (HAS_DDI(dev) &&
11545 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
11546 crtc->scanline_offset = 2;
11547 } else
11548 crtc->scanline_offset = 1;
11549}
11550
11551static struct intel_crtc_state *
11552intel_modeset_compute_config(struct drm_crtc *crtc,
11553 struct drm_display_mode *mode,
11554 struct drm_framebuffer *fb,
11555 struct drm_atomic_state *state,
11556 unsigned *modeset_pipes,
11557 unsigned *prepare_pipes,
11558 unsigned *disable_pipes)
11559{
11560 struct drm_device *dev = crtc->dev;
11561 struct intel_crtc_state *pipe_config = NULL;
11562 struct intel_crtc *intel_crtc;
11563 int ret = 0;
11564
11565 ret = drm_atomic_add_affected_connectors(state, crtc);
11566 if (ret)
11567 return ERR_PTR(ret);
11568
11569 intel_modeset_affected_pipes(crtc, modeset_pipes,
11570 prepare_pipes, disable_pipes);
11571
11572 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
11573 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
11574 if (IS_ERR(pipe_config))
11575 return pipe_config;
11576
11577 pipe_config->base.enable = false;
11578 }
11579
11580 /*
11581 * Note this needs changes when we start tracking multiple modes
11582 * and crtcs. At that point we'll need to compute the whole config
11583 * (i.e. one pipe_config for each crtc) rather than just the one
11584 * for this crtc.
11585 */
11586 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
11587 /* FIXME: For now we still expect modeset_pipes has at most
11588 * one bit set. */
11589 if (WARN_ON(&intel_crtc->base != crtc))
11590 continue;
11591
11592 pipe_config = intel_modeset_pipe_config(crtc, fb, mode, state);
11593 if (IS_ERR(pipe_config))
11594 return pipe_config;
11595
11596 pipe_config->base.enable = true;
11597
11598 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
11599 "[modeset]");
11600 }
11601
11602 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
11603}
11604
11605static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
11606 unsigned modeset_pipes,
11607 unsigned disable_pipes)
11608{
11609 struct drm_device *dev = state->dev;
11610 struct drm_i915_private *dev_priv = to_i915(dev);
11611 unsigned clear_pipes = modeset_pipes | disable_pipes;
11612 struct intel_crtc *intel_crtc;
11613 int ret = 0;
11614
11615 if (!dev_priv->display.crtc_compute_clock)
11616 return 0;
11617
11618 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
11619 if (ret)
11620 goto done;
11621
11622 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11623 struct intel_crtc_state *crtc_state =
11624 intel_atomic_get_crtc_state(state, intel_crtc);
11625
11626 /* Modeset pipes should have a new state by now */
11627 if (WARN_ON(IS_ERR(crtc_state)))
11628 continue;
11629
11630 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11631 crtc_state);
11632 if (ret) {
11633 intel_shared_dpll_abort_config(dev_priv);
11634 goto done;
11635 }
11636 }
11637
11638done:
11639 return ret;
11640}
11641
11642static int __intel_set_mode(struct drm_crtc *crtc,
11643 struct drm_display_mode *mode,
11644 int x, int y, struct drm_framebuffer *fb,
11645 struct intel_crtc_state *pipe_config,
11646 unsigned modeset_pipes,
11647 unsigned prepare_pipes,
11648 unsigned disable_pipes)
11649{
11650 struct drm_device *dev = crtc->dev;
11651 struct drm_i915_private *dev_priv = dev->dev_private;
11652 struct drm_display_mode *saved_mode;
11653 struct drm_atomic_state *state = pipe_config->base.state;
11654 struct intel_crtc_state *crtc_state_copy = NULL;
11655 struct intel_crtc *intel_crtc;
11656 int ret = 0;
11657
11658 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
11659 if (!saved_mode)
11660 return -ENOMEM;
11661
11662 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
11663 if (!crtc_state_copy) {
11664 ret = -ENOMEM;
11665 goto done;
11666 }
11667
11668 *saved_mode = crtc->mode;
11669
11670 /*
11671 * See if the config requires any additional preparation, e.g.
11672 * to adjust global state with pipes off. We need to do this
11673 * here so we can get the modeset_pipe updated config for the new
11674 * mode set on this crtc. For other crtcs we need to use the
11675 * adjusted_mode bits in the crtc directly.
11676 */
11677 if (IS_VALLEYVIEW(dev)) {
11678 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
11679 if (ret)
11680 goto done;
11681
11682 /* may have added more to prepare_pipes than we should */
11683 prepare_pipes &= ~disable_pipes;
11684 }
11685
11686 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
11687 if (ret)
11688 goto done;
11689
11690 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
11691 intel_crtc_disable(&intel_crtc->base);
11692
11693 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11694 if (intel_crtc->base.state->enable)
11695 dev_priv->display.crtc_disable(&intel_crtc->base);
11696 }
11697
11698 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
11699 * to set it here already despite that we pass it down the callchain.
11700 *
11701 * Note we'll need to fix this up when we start tracking multiple
11702 * pipes; here we assume a single modeset_pipe and only track the
11703 * single crtc and mode.
11704 */
11705 if (modeset_pipes) {
11706 crtc->mode = *mode;
11707 /* mode_set/enable/disable functions rely on a correct pipe
11708 * config. */
11709 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
11710
11711 /*
11712 * Calculate and store various constants which
11713 * are later needed by vblank and swap-completion
11714 * timestamping. They are derived from true hwmode.
11715 */
11716 drm_calc_timestamping_constants(crtc,
11717 &pipe_config->base.adjusted_mode);
11718 }
11719
11720 /* Only after disabling all output pipelines that will be changed can we
11721 * update the the output configuration. */
11722 intel_modeset_update_state(dev, prepare_pipes);
11723
11724 modeset_update_crtc_power_domains(state);
11725
11726 /* Set up the DPLL and any encoders state that needs to adjust or depend
11727 * on the DPLL.
11728 */
11729 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
11730 struct drm_plane *primary = intel_crtc->base.primary;
11731 int vdisplay, hdisplay;
11732
11733 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
11734 ret = primary->funcs->update_plane(primary, &intel_crtc->base,
11735 fb, 0, 0,
11736 hdisplay, vdisplay,
11737 x << 16, y << 16,
11738 hdisplay << 16, vdisplay << 16);
11739 }
11740
11741 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
11742 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
11743 update_scanline_offset(intel_crtc);
11744
11745 dev_priv->display.crtc_enable(&intel_crtc->base);
11746 }
11747
11748 /* FIXME: add subpixel order */
11749done:
11750 if (ret && crtc->state->enable)
11751 crtc->mode = *saved_mode;
11752
11753 if (ret == 0 && pipe_config) {
11754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11755
11756 /* The pipe_config will be freed with the atomic state, so
11757 * make a copy. */
11758 memcpy(crtc_state_copy, intel_crtc->config,
11759 sizeof *crtc_state_copy);
11760 intel_crtc->config = crtc_state_copy;
11761 intel_crtc->base.state = &crtc_state_copy->base;
11762 } else {
11763 kfree(crtc_state_copy);
11764 }
11765
11766 kfree(saved_mode);
11767 return ret;
11768}
11769
11770static int intel_set_mode_pipes(struct drm_crtc *crtc,
11771 struct drm_display_mode *mode,
11772 int x, int y, struct drm_framebuffer *fb,
11773 struct intel_crtc_state *pipe_config,
11774 unsigned modeset_pipes,
11775 unsigned prepare_pipes,
11776 unsigned disable_pipes)
11777{
11778 int ret;
11779
11780 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
11781 prepare_pipes, disable_pipes);
11782
11783 if (ret == 0)
11784 intel_modeset_check_state(crtc->dev);
11785
11786 return ret;
11787}
11788
11789static int intel_set_mode(struct drm_crtc *crtc,
11790 struct drm_display_mode *mode,
11791 int x, int y, struct drm_framebuffer *fb,
11792 struct drm_atomic_state *state)
11793{
11794 struct intel_crtc_state *pipe_config;
11795 unsigned modeset_pipes, prepare_pipes, disable_pipes;
11796 int ret = 0;
11797
11798 pipe_config = intel_modeset_compute_config(crtc, mode, fb, state,
11799 &modeset_pipes,
11800 &prepare_pipes,
11801 &disable_pipes);
11802
11803 if (IS_ERR(pipe_config)) {
11804 ret = PTR_ERR(pipe_config);
11805 goto out;
11806 }
11807
11808 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
11809 modeset_pipes, prepare_pipes,
11810 disable_pipes);
11811 if (ret)
11812 goto out;
11813
11814out:
11815 return ret;
11816}
11817
11818void intel_crtc_restore_mode(struct drm_crtc *crtc)
11819{
11820 struct drm_device *dev = crtc->dev;
11821 struct drm_atomic_state *state;
11822 struct intel_encoder *encoder;
11823 struct intel_connector *connector;
11824 struct drm_connector_state *connector_state;
11825
11826 state = drm_atomic_state_alloc(dev);
11827 if (!state) {
11828 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
11829 crtc->base.id);
11830 return;
11831 }
11832
11833 state->acquire_ctx = dev->mode_config.acquire_ctx;
11834
11835 /* The force restore path in the HW readout code relies on the staged
11836 * config still keeping the user requested config while the actual
11837 * state has been overwritten by the configuration read from HW. We
11838 * need to copy the staged config to the atomic state, otherwise the
11839 * mode set will just reapply the state the HW is already in. */
11840 for_each_intel_encoder(dev, encoder) {
11841 if (&encoder->new_crtc->base != crtc)
11842 continue;
11843
11844 for_each_intel_connector(dev, connector) {
11845 if (connector->new_encoder != encoder)
11846 continue;
11847
11848 connector_state = drm_atomic_get_connector_state(state, &connector->base);
11849 if (IS_ERR(connector_state)) {
11850 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
11851 connector->base.base.id,
11852 connector->base.name,
11853 PTR_ERR(connector_state));
11854 continue;
11855 }
11856
11857 connector_state->crtc = crtc;
11858 connector_state->best_encoder = &encoder->base;
11859 }
11860 }
11861
11862 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
11863 state);
11864
11865 drm_atomic_state_free(state);
11866}
11867
11868#undef for_each_intel_crtc_masked
11869
11870static void intel_set_config_free(struct intel_set_config *config)
11871{
11872 if (!config)
11873 return;
11874
11875 kfree(config->save_connector_encoders);
11876 kfree(config->save_encoder_crtcs);
11877 kfree(config->save_crtc_enabled);
11878 kfree(config);
11879}
11880
11881static int intel_set_config_save_state(struct drm_device *dev,
11882 struct intel_set_config *config)
11883{
11884 struct drm_crtc *crtc;
11885 struct drm_encoder *encoder;
11886 struct drm_connector *connector;
11887 int count;
11888
11889 config->save_crtc_enabled =
11890 kcalloc(dev->mode_config.num_crtc,
11891 sizeof(bool), GFP_KERNEL);
11892 if (!config->save_crtc_enabled)
11893 return -ENOMEM;
11894
11895 config->save_encoder_crtcs =
11896 kcalloc(dev->mode_config.num_encoder,
11897 sizeof(struct drm_crtc *), GFP_KERNEL);
11898 if (!config->save_encoder_crtcs)
11899 return -ENOMEM;
11900
11901 config->save_connector_encoders =
11902 kcalloc(dev->mode_config.num_connector,
11903 sizeof(struct drm_encoder *), GFP_KERNEL);
11904 if (!config->save_connector_encoders)
11905 return -ENOMEM;
11906
11907 /* Copy data. Note that driver private data is not affected.
11908 * Should anything bad happen only the expected state is
11909 * restored, not the drivers personal bookkeeping.
11910 */
11911 count = 0;
11912 for_each_crtc(dev, crtc) {
11913 config->save_crtc_enabled[count++] = crtc->state->enable;
11914 }
11915
11916 count = 0;
11917 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
11918 config->save_encoder_crtcs[count++] = encoder->crtc;
11919 }
11920
11921 count = 0;
11922 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11923 config->save_connector_encoders[count++] = connector->encoder;
11924 }
11925
11926 return 0;
11927}
11928
11929static void intel_set_config_restore_state(struct drm_device *dev,
11930 struct intel_set_config *config)
11931{
11932 struct intel_crtc *crtc;
11933 struct intel_encoder *encoder;
11934 struct intel_connector *connector;
11935 int count;
11936
11937 count = 0;
11938 for_each_intel_crtc(dev, crtc) {
11939 crtc->new_enabled = config->save_crtc_enabled[count++];
11940 }
11941
11942 count = 0;
11943 for_each_intel_encoder(dev, encoder) {
11944 encoder->new_crtc =
11945 to_intel_crtc(config->save_encoder_crtcs[count++]);
11946 }
11947
11948 count = 0;
11949 for_each_intel_connector(dev, connector) {
11950 connector->new_encoder =
11951 to_intel_encoder(config->save_connector_encoders[count++]);
11952 }
11953}
11954
11955static bool
11956is_crtc_connector_off(struct drm_mode_set *set)
11957{
11958 int i;
11959
11960 if (set->num_connectors == 0)
11961 return false;
11962
11963 if (WARN_ON(set->connectors == NULL))
11964 return false;
11965
11966 for (i = 0; i < set->num_connectors; i++)
11967 if (set->connectors[i]->encoder &&
11968 set->connectors[i]->encoder->crtc == set->crtc &&
11969 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
11970 return true;
11971
11972 return false;
11973}
11974
11975static void
11976intel_set_config_compute_mode_changes(struct drm_mode_set *set,
11977 struct intel_set_config *config)
11978{
11979
11980 /* We should be able to check here if the fb has the same properties
11981 * and then just flip_or_move it */
11982 if (is_crtc_connector_off(set)) {
11983 config->mode_changed = true;
11984 } else if (set->crtc->primary->fb != set->fb) {
11985 /*
11986 * If we have no fb, we can only flip as long as the crtc is
11987 * active, otherwise we need a full mode set. The crtc may
11988 * be active if we've only disabled the primary plane, or
11989 * in fastboot situations.
11990 */
11991 if (set->crtc->primary->fb == NULL) {
11992 struct intel_crtc *intel_crtc =
11993 to_intel_crtc(set->crtc);
11994
11995 if (intel_crtc->active) {
11996 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
11997 config->fb_changed = true;
11998 } else {
11999 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12000 config->mode_changed = true;
12001 }
12002 } else if (set->fb == NULL) {
12003 config->mode_changed = true;
12004 } else if (set->fb->pixel_format !=
12005 set->crtc->primary->fb->pixel_format) {
12006 config->mode_changed = true;
12007 } else {
12008 config->fb_changed = true;
12009 }
12010 }
12011
12012 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
12013 config->fb_changed = true;
12014
12015 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12016 DRM_DEBUG_KMS("modes are different, full mode set\n");
12017 drm_mode_debug_printmodeline(&set->crtc->mode);
12018 drm_mode_debug_printmodeline(set->mode);
12019 config->mode_changed = true;
12020 }
12021
12022 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12023 set->crtc->base.id, config->mode_changed, config->fb_changed);
12024}
12025
12026static int
12027intel_modeset_stage_output_state(struct drm_device *dev,
12028 struct drm_mode_set *set,
12029 struct intel_set_config *config,
12030 struct drm_atomic_state *state)
12031{
12032 struct intel_connector *connector;
12033 struct drm_connector_state *connector_state;
12034 struct intel_encoder *encoder;
12035 struct intel_crtc *crtc;
12036 int ro;
12037
12038 /* The upper layers ensure that we either disable a crtc or have a list
12039 * of connectors. For paranoia, double-check this. */
12040 WARN_ON(!set->fb && (set->num_connectors != 0));
12041 WARN_ON(set->fb && (set->num_connectors == 0));
12042
12043 for_each_intel_connector(dev, connector) {
12044 /* Otherwise traverse passed in connector list and get encoders
12045 * for them. */
12046 for (ro = 0; ro < set->num_connectors; ro++) {
12047 if (set->connectors[ro] == &connector->base) {
12048 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
12049 break;
12050 }
12051 }
12052
12053 /* If we disable the crtc, disable all its connectors. Also, if
12054 * the connector is on the changing crtc but not on the new
12055 * connector list, disable it. */
12056 if ((!set->fb || ro == set->num_connectors) &&
12057 connector->base.encoder &&
12058 connector->base.encoder->crtc == set->crtc) {
12059 connector->new_encoder = NULL;
12060
12061 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12062 connector->base.base.id,
12063 connector->base.name);
12064 }
12065
12066
12067 if (&connector->new_encoder->base != connector->base.encoder) {
12068 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12069 connector->base.base.id,
12070 connector->base.name);
12071 config->mode_changed = true;
12072 }
12073 }
12074 /* connector->new_encoder is now updated for all connectors. */
12075
12076 /* Update crtc of enabled connectors. */
12077 for_each_intel_connector(dev, connector) {
12078 struct drm_crtc *new_crtc;
12079
12080 if (!connector->new_encoder)
12081 continue;
12082
12083 new_crtc = connector->new_encoder->base.crtc;
12084
12085 for (ro = 0; ro < set->num_connectors; ro++) {
12086 if (set->connectors[ro] == &connector->base)
12087 new_crtc = set->crtc;
12088 }
12089
12090 /* Make sure the new CRTC will work with the encoder */
12091 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12092 new_crtc)) {
12093 return -EINVAL;
12094 }
12095 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
12096
12097 connector_state =
12098 drm_atomic_get_connector_state(state, &connector->base);
12099 if (IS_ERR(connector_state))
12100 return PTR_ERR(connector_state);
12101
12102 connector_state->crtc = new_crtc;
12103 connector_state->best_encoder = &connector->new_encoder->base;
12104
12105 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12106 connector->base.base.id,
12107 connector->base.name,
12108 new_crtc->base.id);
12109 }
12110
12111 /* Check for any encoders that needs to be disabled. */
12112 for_each_intel_encoder(dev, encoder) {
12113 int num_connectors = 0;
12114 for_each_intel_connector(dev, connector) {
12115 if (connector->new_encoder == encoder) {
12116 WARN_ON(!connector->new_encoder->new_crtc);
12117 num_connectors++;
12118 }
12119 }
12120
12121 if (num_connectors == 0)
12122 encoder->new_crtc = NULL;
12123 else if (num_connectors > 1)
12124 return -EINVAL;
12125
12126 /* Only now check for crtc changes so we don't miss encoders
12127 * that will be disabled. */
12128 if (&encoder->new_crtc->base != encoder->base.crtc) {
12129 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12130 encoder->base.base.id,
12131 encoder->base.name);
12132 config->mode_changed = true;
12133 }
12134 }
12135 /* Now we've also updated encoder->new_crtc for all encoders. */
12136 for_each_intel_connector(dev, connector) {
12137 connector_state =
12138 drm_atomic_get_connector_state(state, &connector->base);
12139 if (IS_ERR(connector_state))
12140 return PTR_ERR(connector_state);
12141
12142 if (connector->new_encoder) {
12143 if (connector->new_encoder != connector->encoder)
12144 connector->encoder = connector->new_encoder;
12145 } else {
12146 connector_state->crtc = NULL;
12147 connector_state->best_encoder = NULL;
12148 }
12149 }
12150 for_each_intel_crtc(dev, crtc) {
12151 crtc->new_enabled = false;
12152
12153 for_each_intel_encoder(dev, encoder) {
12154 if (encoder->new_crtc == crtc) {
12155 crtc->new_enabled = true;
12156 break;
12157 }
12158 }
12159
12160 if (crtc->new_enabled != crtc->base.state->enable) {
12161 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12162 crtc->base.base.id,
12163 crtc->new_enabled ? "en" : "dis");
12164 config->mode_changed = true;
12165 }
12166 }
12167
12168 return 0;
12169}
12170
12171static void disable_crtc_nofb(struct intel_crtc *crtc)
12172{
12173 struct drm_device *dev = crtc->base.dev;
12174 struct intel_encoder *encoder;
12175 struct intel_connector *connector;
12176
12177 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12178 pipe_name(crtc->pipe));
12179
12180 for_each_intel_connector(dev, connector) {
12181 if (connector->new_encoder &&
12182 connector->new_encoder->new_crtc == crtc)
12183 connector->new_encoder = NULL;
12184 }
12185
12186 for_each_intel_encoder(dev, encoder) {
12187 if (encoder->new_crtc == crtc)
12188 encoder->new_crtc = NULL;
12189 }
12190
12191 crtc->new_enabled = false;
12192}
12193
12194static int intel_crtc_set_config(struct drm_mode_set *set)
12195{
12196 struct drm_device *dev;
12197 struct drm_mode_set save_set;
12198 struct drm_atomic_state *state = NULL;
12199 struct intel_set_config *config;
12200 struct intel_crtc_state *pipe_config;
12201 unsigned modeset_pipes, prepare_pipes, disable_pipes;
12202 int ret;
12203
12204 BUG_ON(!set);
12205 BUG_ON(!set->crtc);
12206 BUG_ON(!set->crtc->helper_private);
12207
12208 /* Enforce sane interface api - has been abused by the fb helper. */
12209 BUG_ON(!set->mode && set->fb);
12210 BUG_ON(set->fb && set->num_connectors == 0);
12211
12212 if (set->fb) {
12213 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12214 set->crtc->base.id, set->fb->base.id,
12215 (int)set->num_connectors, set->x, set->y);
12216 } else {
12217 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
12218 }
12219
12220 dev = set->crtc->dev;
12221
12222 ret = -ENOMEM;
12223 config = kzalloc(sizeof(*config), GFP_KERNEL);
12224 if (!config)
12225 goto out_config;
12226
12227 ret = intel_set_config_save_state(dev, config);
12228 if (ret)
12229 goto out_config;
12230
12231 save_set.crtc = set->crtc;
12232 save_set.mode = &set->crtc->mode;
12233 save_set.x = set->crtc->x;
12234 save_set.y = set->crtc->y;
12235 save_set.fb = set->crtc->primary->fb;
12236
12237 /* Compute whether we need a full modeset, only an fb base update or no
12238 * change at all. In the future we might also check whether only the
12239 * mode changed, e.g. for LVDS where we only change the panel fitter in
12240 * such cases. */
12241 intel_set_config_compute_mode_changes(set, config);
12242
12243 state = drm_atomic_state_alloc(dev);
12244 if (!state) {
12245 ret = -ENOMEM;
12246 goto out_config;
12247 }
12248
12249 state->acquire_ctx = dev->mode_config.acquire_ctx;
12250
12251 ret = intel_modeset_stage_output_state(dev, set, config, state);
12252 if (ret)
12253 goto fail;
12254
12255 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
12256 set->fb, state,
12257 &modeset_pipes,
12258 &prepare_pipes,
12259 &disable_pipes);
12260 if (IS_ERR(pipe_config)) {
12261 ret = PTR_ERR(pipe_config);
12262 goto fail;
12263 } else if (pipe_config) {
12264 if (pipe_config->has_audio !=
12265 to_intel_crtc(set->crtc)->config->has_audio)
12266 config->mode_changed = true;
12267
12268 /*
12269 * Note we have an issue here with infoframes: current code
12270 * only updates them on the full mode set path per hw
12271 * requirements. So here we should be checking for any
12272 * required changes and forcing a mode set.
12273 */
12274 }
12275
12276 intel_update_pipe_size(to_intel_crtc(set->crtc));
12277
12278 if (config->mode_changed) {
12279 ret = intel_set_mode_pipes(set->crtc, set->mode,
12280 set->x, set->y, set->fb, pipe_config,
12281 modeset_pipes, prepare_pipes,
12282 disable_pipes);
12283 } else if (config->fb_changed) {
12284 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
12285 struct drm_plane *primary = set->crtc->primary;
12286 int vdisplay, hdisplay;
12287
12288 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
12289 ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
12290 0, 0, hdisplay, vdisplay,
12291 set->x << 16, set->y << 16,
12292 hdisplay << 16, vdisplay << 16);
12293
12294 /*
12295 * We need to make sure the primary plane is re-enabled if it
12296 * has previously been turned off.
12297 */
12298 if (!intel_crtc->primary_enabled && ret == 0) {
12299 WARN_ON(!intel_crtc->active);
12300 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
12301 }
12302
12303 /*
12304 * In the fastboot case this may be our only check of the
12305 * state after boot. It would be better to only do it on
12306 * the first update, but we don't have a nice way of doing that
12307 * (and really, set_config isn't used much for high freq page
12308 * flipping, so increasing its cost here shouldn't be a big
12309 * deal).
12310 */
12311 if (i915.fastboot && ret == 0)
12312 intel_modeset_check_state(set->crtc->dev);
12313 }
12314
12315 if (ret) {
12316 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12317 set->crtc->base.id, ret);
12318fail:
12319 intel_set_config_restore_state(dev, config);
12320
12321 drm_atomic_state_clear(state);
12322
12323 /*
12324 * HACK: if the pipe was on, but we didn't have a framebuffer,
12325 * force the pipe off to avoid oopsing in the modeset code
12326 * due to fb==NULL. This should only happen during boot since
12327 * we don't yet reconstruct the FB from the hardware state.
12328 */
12329 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12330 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12331
12332 /* Try to restore the config */
12333 if (config->mode_changed &&
12334 intel_set_mode(save_set.crtc, save_set.mode,
12335 save_set.x, save_set.y, save_set.fb,
12336 state))
12337 DRM_ERROR("failed to restore config after modeset failure\n");
12338 }
12339
12340out_config:
12341 if (state)
12342 drm_atomic_state_free(state);
12343
12344 intel_set_config_free(config);
12345 return ret;
12346}
12347
12348static const struct drm_crtc_funcs intel_crtc_funcs = {
12349 .gamma_set = intel_crtc_gamma_set,
12350 .set_config = intel_crtc_set_config,
12351 .destroy = intel_crtc_destroy,
12352 .page_flip = intel_crtc_page_flip,
12353 .atomic_duplicate_state = intel_crtc_duplicate_state,
12354 .atomic_destroy_state = intel_crtc_destroy_state,
12355};
12356
12357static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12358 struct intel_shared_dpll *pll,
12359 struct intel_dpll_hw_state *hw_state)
12360{
12361 uint32_t val;
12362
12363 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
12364 return false;
12365
12366 val = I915_READ(PCH_DPLL(pll->id));
12367 hw_state->dpll = val;
12368 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12369 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
12370
12371 return val & DPLL_VCO_ENABLE;
12372}
12373
12374static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12375 struct intel_shared_dpll *pll)
12376{
12377 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12378 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
12379}
12380
12381static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12382 struct intel_shared_dpll *pll)
12383{
12384 /* PCH refclock must be enabled first */
12385 ibx_assert_pch_refclk_enabled(dev_priv);
12386
12387 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
12388
12389 /* Wait for the clocks to stabilize. */
12390 POSTING_READ(PCH_DPLL(pll->id));
12391 udelay(150);
12392
12393 /* The pixel multiplier can only be updated once the
12394 * DPLL is enabled and the clocks are stable.
12395 *
12396 * So write it again.
12397 */
12398 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
12399 POSTING_READ(PCH_DPLL(pll->id));
12400 udelay(200);
12401}
12402
12403static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12404 struct intel_shared_dpll *pll)
12405{
12406 struct drm_device *dev = dev_priv->dev;
12407 struct intel_crtc *crtc;
12408
12409 /* Make sure no transcoder isn't still depending on us. */
12410 for_each_intel_crtc(dev, crtc) {
12411 if (intel_crtc_to_shared_dpll(crtc) == pll)
12412 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12413 }
12414
12415 I915_WRITE(PCH_DPLL(pll->id), 0);
12416 POSTING_READ(PCH_DPLL(pll->id));
12417 udelay(200);
12418}
12419
12420static char *ibx_pch_dpll_names[] = {
12421 "PCH DPLL A",
12422 "PCH DPLL B",
12423};
12424
12425static void ibx_pch_dpll_init(struct drm_device *dev)
12426{
12427 struct drm_i915_private *dev_priv = dev->dev_private;
12428 int i;
12429
12430 dev_priv->num_shared_dpll = 2;
12431
12432 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12433 dev_priv->shared_dplls[i].id = i;
12434 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
12435 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
12436 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12437 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
12438 dev_priv->shared_dplls[i].get_hw_state =
12439 ibx_pch_dpll_get_hw_state;
12440 }
12441}
12442
12443static void intel_shared_dpll_init(struct drm_device *dev)
12444{
12445 struct drm_i915_private *dev_priv = dev->dev_private;
12446
12447 if (HAS_DDI(dev))
12448 intel_ddi_pll_init(dev);
12449 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
12450 ibx_pch_dpll_init(dev);
12451 else
12452 dev_priv->num_shared_dpll = 0;
12453
12454 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
12455}
12456
12457/**
12458 * intel_wm_need_update - Check whether watermarks need updating
12459 * @plane: drm plane
12460 * @state: new plane state
12461 *
12462 * Check current plane state versus the new one to determine whether
12463 * watermarks need to be recalculated.
12464 *
12465 * Returns true or false.
12466 */
12467bool intel_wm_need_update(struct drm_plane *plane,
12468 struct drm_plane_state *state)
12469{
12470 /* Update watermarks on tiling changes. */
12471 if (!plane->state->fb || !state->fb ||
12472 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12473 plane->state->rotation != state->rotation)
12474 return true;
12475
12476 return false;
12477}
12478
12479/**
12480 * intel_prepare_plane_fb - Prepare fb for usage on plane
12481 * @plane: drm plane to prepare for
12482 * @fb: framebuffer to prepare for presentation
12483 *
12484 * Prepares a framebuffer for usage on a display plane. Generally this
12485 * involves pinning the underlying object and updating the frontbuffer tracking
12486 * bits. Some older platforms need special physical address handling for
12487 * cursor planes.
12488 *
12489 * Returns 0 on success, negative error code on failure.
12490 */
12491int
12492intel_prepare_plane_fb(struct drm_plane *plane,
12493 struct drm_framebuffer *fb,
12494 const struct drm_plane_state *new_state)
12495{
12496 struct drm_device *dev = plane->dev;
12497 struct intel_plane *intel_plane = to_intel_plane(plane);
12498 enum pipe pipe = intel_plane->pipe;
12499 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12500 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12501 unsigned frontbuffer_bits = 0;
12502 int ret = 0;
12503
12504 if (!obj)
12505 return 0;
12506
12507 switch (plane->type) {
12508 case DRM_PLANE_TYPE_PRIMARY:
12509 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12510 break;
12511 case DRM_PLANE_TYPE_CURSOR:
12512 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12513 break;
12514 case DRM_PLANE_TYPE_OVERLAY:
12515 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12516 break;
12517 }
12518
12519 mutex_lock(&dev->struct_mutex);
12520
12521 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12522 INTEL_INFO(dev)->cursor_needs_physical) {
12523 int align = IS_I830(dev) ? 16 * 1024 : 256;
12524 ret = i915_gem_object_attach_phys(obj, align);
12525 if (ret)
12526 DRM_DEBUG_KMS("failed to attach phys object\n");
12527 } else {
12528 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
12529 }
12530
12531 if (ret == 0)
12532 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
12533
12534 mutex_unlock(&dev->struct_mutex);
12535
12536 return ret;
12537}
12538
12539/**
12540 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12541 * @plane: drm plane to clean up for
12542 * @fb: old framebuffer that was on plane
12543 *
12544 * Cleans up a framebuffer that has just been removed from a plane.
12545 */
12546void
12547intel_cleanup_plane_fb(struct drm_plane *plane,
12548 struct drm_framebuffer *fb,
12549 const struct drm_plane_state *old_state)
12550{
12551 struct drm_device *dev = plane->dev;
12552 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12553
12554 if (WARN_ON(!obj))
12555 return;
12556
12557 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12558 !INTEL_INFO(dev)->cursor_needs_physical) {
12559 mutex_lock(&dev->struct_mutex);
12560 intel_unpin_fb_obj(fb, old_state);
12561 mutex_unlock(&dev->struct_mutex);
12562 }
12563}
12564
12565static int
12566intel_check_primary_plane(struct drm_plane *plane,
12567 struct intel_plane_state *state)
12568{
12569 struct drm_device *dev = plane->dev;
12570 struct drm_i915_private *dev_priv = dev->dev_private;
12571 struct drm_crtc *crtc = state->base.crtc;
12572 struct intel_crtc *intel_crtc;
12573 struct drm_framebuffer *fb = state->base.fb;
12574 struct drm_rect *dest = &state->dst;
12575 struct drm_rect *src = &state->src;
12576 const struct drm_rect *clip = &state->clip;
12577 int ret;
12578
12579 crtc = crtc ? crtc : plane->crtc;
12580 intel_crtc = to_intel_crtc(crtc);
12581
12582 ret = drm_plane_helper_check_update(plane, crtc, fb,
12583 src, dest, clip,
12584 DRM_PLANE_HELPER_NO_SCALING,
12585 DRM_PLANE_HELPER_NO_SCALING,
12586 false, true, &state->visible);
12587 if (ret)
12588 return ret;
12589
12590 if (intel_crtc->active) {
12591 intel_crtc->atomic.wait_for_flips = true;
12592
12593 /*
12594 * FBC does not work on some platforms for rotated
12595 * planes, so disable it when rotation is not 0 and
12596 * update it when rotation is set back to 0.
12597 *
12598 * FIXME: This is redundant with the fbc update done in
12599 * the primary plane enable function except that that
12600 * one is done too late. We eventually need to unify
12601 * this.
12602 */
12603 if (intel_crtc->primary_enabled &&
12604 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
12605 dev_priv->fbc.crtc == intel_crtc &&
12606 state->base.rotation != BIT(DRM_ROTATE_0)) {
12607 intel_crtc->atomic.disable_fbc = true;
12608 }
12609
12610 if (state->visible) {
12611 /*
12612 * BDW signals flip done immediately if the plane
12613 * is disabled, even if the plane enable is already
12614 * armed to occur at the next vblank :(
12615 */
12616 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
12617 intel_crtc->atomic.wait_vblank = true;
12618 }
12619
12620 intel_crtc->atomic.fb_bits |=
12621 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
12622
12623 intel_crtc->atomic.update_fbc = true;
12624
12625 if (intel_wm_need_update(plane, &state->base))
12626 intel_crtc->atomic.update_wm = true;
12627 }
12628
12629 return 0;
12630}
12631
12632static void
12633intel_commit_primary_plane(struct drm_plane *plane,
12634 struct intel_plane_state *state)
12635{
12636 struct drm_crtc *crtc = state->base.crtc;
12637 struct drm_framebuffer *fb = state->base.fb;
12638 struct drm_device *dev = plane->dev;
12639 struct drm_i915_private *dev_priv = dev->dev_private;
12640 struct intel_crtc *intel_crtc;
12641 struct drm_rect *src = &state->src;
12642
12643 crtc = crtc ? crtc : plane->crtc;
12644 intel_crtc = to_intel_crtc(crtc);
12645
12646 plane->fb = fb;
12647 crtc->x = src->x1 >> 16;
12648 crtc->y = src->y1 >> 16;
12649
12650 if (intel_crtc->active) {
12651 if (state->visible) {
12652 /* FIXME: kill this fastboot hack */
12653 intel_update_pipe_size(intel_crtc);
12654
12655 intel_crtc->primary_enabled = true;
12656
12657 dev_priv->display.update_primary_plane(crtc, plane->fb,
12658 crtc->x, crtc->y);
12659 } else {
12660 /*
12661 * If clipping results in a non-visible primary plane,
12662 * we'll disable the primary plane. Note that this is
12663 * a bit different than what happens if userspace
12664 * explicitly disables the plane by passing fb=0
12665 * because plane->fb still gets set and pinned.
12666 */
12667 intel_disable_primary_hw_plane(plane, crtc);
12668 }
12669 }
12670}
12671
12672static void intel_begin_crtc_commit(struct drm_crtc *crtc)
12673{
12674 struct drm_device *dev = crtc->dev;
12675 struct drm_i915_private *dev_priv = dev->dev_private;
12676 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12677 struct intel_plane *intel_plane;
12678 struct drm_plane *p;
12679 unsigned fb_bits = 0;
12680
12681 /* Track fb's for any planes being disabled */
12682 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
12683 intel_plane = to_intel_plane(p);
12684
12685 if (intel_crtc->atomic.disabled_planes &
12686 (1 << drm_plane_index(p))) {
12687 switch (p->type) {
12688 case DRM_PLANE_TYPE_PRIMARY:
12689 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
12690 break;
12691 case DRM_PLANE_TYPE_CURSOR:
12692 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
12693 break;
12694 case DRM_PLANE_TYPE_OVERLAY:
12695 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
12696 break;
12697 }
12698
12699 mutex_lock(&dev->struct_mutex);
12700 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
12701 mutex_unlock(&dev->struct_mutex);
12702 }
12703 }
12704
12705 if (intel_crtc->atomic.wait_for_flips)
12706 intel_crtc_wait_for_pending_flips(crtc);
12707
12708 if (intel_crtc->atomic.disable_fbc)
12709 intel_fbc_disable(dev);
12710
12711 if (intel_crtc->atomic.pre_disable_primary)
12712 intel_pre_disable_primary(crtc);
12713
12714 if (intel_crtc->atomic.update_wm)
12715 intel_update_watermarks(crtc);
12716
12717 intel_runtime_pm_get(dev_priv);
12718
12719 /* Perform vblank evasion around commit operation */
12720 if (intel_crtc->active)
12721 intel_crtc->atomic.evade =
12722 intel_pipe_update_start(intel_crtc,
12723 &intel_crtc->atomic.start_vbl_count);
12724}
12725
12726static void intel_finish_crtc_commit(struct drm_crtc *crtc)
12727{
12728 struct drm_device *dev = crtc->dev;
12729 struct drm_i915_private *dev_priv = dev->dev_private;
12730 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12731 struct drm_plane *p;
12732
12733 if (intel_crtc->atomic.evade)
12734 intel_pipe_update_end(intel_crtc,
12735 intel_crtc->atomic.start_vbl_count);
12736
12737 intel_runtime_pm_put(dev_priv);
12738
12739 if (intel_crtc->atomic.wait_vblank)
12740 intel_wait_for_vblank(dev, intel_crtc->pipe);
12741
12742 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
12743
12744 if (intel_crtc->atomic.update_fbc) {
12745 mutex_lock(&dev->struct_mutex);
12746 intel_fbc_update(dev);
12747 mutex_unlock(&dev->struct_mutex);
12748 }
12749
12750 if (intel_crtc->atomic.post_enable_primary)
12751 intel_post_enable_primary(crtc);
12752
12753 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
12754 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
12755 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
12756 false, false);
12757
12758 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
12759}
12760
12761/**
12762 * intel_plane_destroy - destroy a plane
12763 * @plane: plane to destroy
12764 *
12765 * Common destruction function for all types of planes (primary, cursor,
12766 * sprite).
12767 */
12768void intel_plane_destroy(struct drm_plane *plane)
12769{
12770 struct intel_plane *intel_plane = to_intel_plane(plane);
12771 drm_plane_cleanup(plane);
12772 kfree(intel_plane);
12773}
12774
12775const struct drm_plane_funcs intel_plane_funcs = {
12776 .update_plane = drm_plane_helper_update,
12777 .disable_plane = drm_plane_helper_disable,
12778 .destroy = intel_plane_destroy,
12779 .set_property = drm_atomic_helper_plane_set_property,
12780 .atomic_get_property = intel_plane_atomic_get_property,
12781 .atomic_set_property = intel_plane_atomic_set_property,
12782 .atomic_duplicate_state = intel_plane_duplicate_state,
12783 .atomic_destroy_state = intel_plane_destroy_state,
12784
12785};
12786
12787static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
12788 int pipe)
12789{
12790 struct intel_plane *primary;
12791 struct intel_plane_state *state;
12792 const uint32_t *intel_primary_formats;
12793 int num_formats;
12794
12795 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
12796 if (primary == NULL)
12797 return NULL;
12798
12799 state = intel_create_plane_state(&primary->base);
12800 if (!state) {
12801 kfree(primary);
12802 return NULL;
12803 }
12804 primary->base.state = &state->base;
12805
12806 primary->can_scale = false;
12807 primary->max_downscale = 1;
12808 primary->pipe = pipe;
12809 primary->plane = pipe;
12810 primary->check_plane = intel_check_primary_plane;
12811 primary->commit_plane = intel_commit_primary_plane;
12812 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
12813 primary->plane = !pipe;
12814
12815 if (INTEL_INFO(dev)->gen <= 3) {
12816 intel_primary_formats = intel_primary_formats_gen2;
12817 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
12818 } else {
12819 intel_primary_formats = intel_primary_formats_gen4;
12820 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
12821 }
12822
12823 drm_universal_plane_init(dev, &primary->base, 0,
12824 &intel_plane_funcs,
12825 intel_primary_formats, num_formats,
12826 DRM_PLANE_TYPE_PRIMARY);
12827
12828 if (INTEL_INFO(dev)->gen >= 4) {
12829 if (!dev->mode_config.rotation_property)
12830 dev->mode_config.rotation_property =
12831 drm_mode_create_rotation_property(dev,
12832 BIT(DRM_ROTATE_0) |
12833 BIT(DRM_ROTATE_180));
12834 if (dev->mode_config.rotation_property)
12835 drm_object_attach_property(&primary->base.base,
12836 dev->mode_config.rotation_property,
12837 state->base.rotation);
12838 }
12839
12840 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
12841
12842 return &primary->base;
12843}
12844
12845static int
12846intel_check_cursor_plane(struct drm_plane *plane,
12847 struct intel_plane_state *state)
12848{
12849 struct drm_crtc *crtc = state->base.crtc;
12850 struct drm_device *dev = plane->dev;
12851 struct drm_framebuffer *fb = state->base.fb;
12852 struct drm_rect *dest = &state->dst;
12853 struct drm_rect *src = &state->src;
12854 const struct drm_rect *clip = &state->clip;
12855 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12856 struct intel_crtc *intel_crtc;
12857 unsigned stride;
12858 int ret;
12859
12860 crtc = crtc ? crtc : plane->crtc;
12861 intel_crtc = to_intel_crtc(crtc);
12862
12863 ret = drm_plane_helper_check_update(plane, crtc, fb,
12864 src, dest, clip,
12865 DRM_PLANE_HELPER_NO_SCALING,
12866 DRM_PLANE_HELPER_NO_SCALING,
12867 true, true, &state->visible);
12868 if (ret)
12869 return ret;
12870
12871
12872 /* if we want to turn off the cursor ignore width and height */
12873 if (!obj)
12874 goto finish;
12875
12876 /* Check for which cursor types we support */
12877 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
12878 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
12879 state->base.crtc_w, state->base.crtc_h);
12880 return -EINVAL;
12881 }
12882
12883 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
12884 if (obj->base.size < stride * state->base.crtc_h) {
12885 DRM_DEBUG_KMS("buffer is too small\n");
12886 return -ENOMEM;
12887 }
12888
12889 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
12890 DRM_DEBUG_KMS("cursor cannot be tiled\n");
12891 ret = -EINVAL;
12892 }
12893
12894finish:
12895 if (intel_crtc->active) {
12896 if (plane->state->crtc_w != state->base.crtc_w)
12897 intel_crtc->atomic.update_wm = true;
12898
12899 intel_crtc->atomic.fb_bits |=
12900 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
12901 }
12902
12903 return ret;
12904}
12905
12906static void
12907intel_commit_cursor_plane(struct drm_plane *plane,
12908 struct intel_plane_state *state)
12909{
12910 struct drm_crtc *crtc = state->base.crtc;
12911 struct drm_device *dev = plane->dev;
12912 struct intel_crtc *intel_crtc;
12913 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
12914 uint32_t addr;
12915
12916 crtc = crtc ? crtc : plane->crtc;
12917 intel_crtc = to_intel_crtc(crtc);
12918
12919 plane->fb = state->base.fb;
12920 crtc->cursor_x = state->base.crtc_x;
12921 crtc->cursor_y = state->base.crtc_y;
12922
12923 if (intel_crtc->cursor_bo == obj)
12924 goto update;
12925
12926 if (!obj)
12927 addr = 0;
12928 else if (!INTEL_INFO(dev)->cursor_needs_physical)
12929 addr = i915_gem_obj_ggtt_offset(obj);
12930 else
12931 addr = obj->phys_handle->busaddr;
12932
12933 intel_crtc->cursor_addr = addr;
12934 intel_crtc->cursor_bo = obj;
12935update:
12936
12937 if (intel_crtc->active)
12938 intel_crtc_update_cursor(crtc, state->visible);
12939}
12940
12941static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
12942 int pipe)
12943{
12944 struct intel_plane *cursor;
12945 struct intel_plane_state *state;
12946
12947 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
12948 if (cursor == NULL)
12949 return NULL;
12950
12951 state = intel_create_plane_state(&cursor->base);
12952 if (!state) {
12953 kfree(cursor);
12954 return NULL;
12955 }
12956 cursor->base.state = &state->base;
12957
12958 cursor->can_scale = false;
12959 cursor->max_downscale = 1;
12960 cursor->pipe = pipe;
12961 cursor->plane = pipe;
12962 cursor->check_plane = intel_check_cursor_plane;
12963 cursor->commit_plane = intel_commit_cursor_plane;
12964
12965 drm_universal_plane_init(dev, &cursor->base, 0,
12966 &intel_plane_funcs,
12967 intel_cursor_formats,
12968 ARRAY_SIZE(intel_cursor_formats),
12969 DRM_PLANE_TYPE_CURSOR);
12970
12971 if (INTEL_INFO(dev)->gen >= 4) {
12972 if (!dev->mode_config.rotation_property)
12973 dev->mode_config.rotation_property =
12974 drm_mode_create_rotation_property(dev,
12975 BIT(DRM_ROTATE_0) |
12976 BIT(DRM_ROTATE_180));
12977 if (dev->mode_config.rotation_property)
12978 drm_object_attach_property(&cursor->base.base,
12979 dev->mode_config.rotation_property,
12980 state->base.rotation);
12981 }
12982
12983 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
12984
12985 return &cursor->base;
12986}
12987
12988static void intel_crtc_init(struct drm_device *dev, int pipe)
12989{
12990 struct drm_i915_private *dev_priv = dev->dev_private;
12991 struct intel_crtc *intel_crtc;
12992 struct intel_crtc_state *crtc_state = NULL;
12993 struct drm_plane *primary = NULL;
12994 struct drm_plane *cursor = NULL;
12995 int i, ret;
12996
12997 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
12998 if (intel_crtc == NULL)
12999 return;
13000
13001 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13002 if (!crtc_state)
13003 goto fail;
13004 intel_crtc_set_state(intel_crtc, crtc_state);
13005 crtc_state->base.crtc = &intel_crtc->base;
13006
13007 primary = intel_primary_plane_create(dev, pipe);
13008 if (!primary)
13009 goto fail;
13010
13011 cursor = intel_cursor_plane_create(dev, pipe);
13012 if (!cursor)
13013 goto fail;
13014
13015 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
13016 cursor, &intel_crtc_funcs);
13017 if (ret)
13018 goto fail;
13019
13020 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
13021 for (i = 0; i < 256; i++) {
13022 intel_crtc->lut_r[i] = i;
13023 intel_crtc->lut_g[i] = i;
13024 intel_crtc->lut_b[i] = i;
13025 }
13026
13027 /*
13028 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
13029 * is hooked to pipe B. Hence we want plane A feeding pipe B.
13030 */
13031 intel_crtc->pipe = pipe;
13032 intel_crtc->plane = pipe;
13033 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
13034 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
13035 intel_crtc->plane = !pipe;
13036 }
13037
13038 intel_crtc->cursor_base = ~0;
13039 intel_crtc->cursor_cntl = ~0;
13040 intel_crtc->cursor_size = ~0;
13041
13042 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13043 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13044 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13045 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13046
13047 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13048
13049 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
13050
13051 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
13052 return;
13053
13054fail:
13055 if (primary)
13056 drm_plane_cleanup(primary);
13057 if (cursor)
13058 drm_plane_cleanup(cursor);
13059 kfree(crtc_state);
13060 kfree(intel_crtc);
13061}
13062
13063enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13064{
13065 struct drm_encoder *encoder = connector->base.encoder;
13066 struct drm_device *dev = connector->base.dev;
13067
13068 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
13069
13070 if (!encoder || WARN_ON(!encoder->crtc))
13071 return INVALID_PIPE;
13072
13073 return to_intel_crtc(encoder->crtc)->pipe;
13074}
13075
13076int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
13077 struct drm_file *file)
13078{
13079 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
13080 struct drm_crtc *drmmode_crtc;
13081 struct intel_crtc *crtc;
13082
13083 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
13084
13085 if (!drmmode_crtc) {
13086 DRM_ERROR("no such CRTC id\n");
13087 return -ENOENT;
13088 }
13089
13090 crtc = to_intel_crtc(drmmode_crtc);
13091 pipe_from_crtc_id->pipe = crtc->pipe;
13092
13093 return 0;
13094}
13095
13096static int intel_encoder_clones(struct intel_encoder *encoder)
13097{
13098 struct drm_device *dev = encoder->base.dev;
13099 struct intel_encoder *source_encoder;
13100 int index_mask = 0;
13101 int entry = 0;
13102
13103 for_each_intel_encoder(dev, source_encoder) {
13104 if (encoders_cloneable(encoder, source_encoder))
13105 index_mask |= (1 << entry);
13106
13107 entry++;
13108 }
13109
13110 return index_mask;
13111}
13112
13113static bool has_edp_a(struct drm_device *dev)
13114{
13115 struct drm_i915_private *dev_priv = dev->dev_private;
13116
13117 if (!IS_MOBILE(dev))
13118 return false;
13119
13120 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13121 return false;
13122
13123 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
13124 return false;
13125
13126 return true;
13127}
13128
13129static bool intel_crt_present(struct drm_device *dev)
13130{
13131 struct drm_i915_private *dev_priv = dev->dev_private;
13132
13133 if (INTEL_INFO(dev)->gen >= 9)
13134 return false;
13135
13136 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
13137 return false;
13138
13139 if (IS_CHERRYVIEW(dev))
13140 return false;
13141
13142 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13143 return false;
13144
13145 return true;
13146}
13147
13148static void intel_setup_outputs(struct drm_device *dev)
13149{
13150 struct drm_i915_private *dev_priv = dev->dev_private;
13151 struct intel_encoder *encoder;
13152 struct drm_connector *connector;
13153 bool dpd_is_edp = false;
13154
13155 intel_lvds_init(dev);
13156
13157 if (intel_crt_present(dev))
13158 intel_crt_init(dev);
13159
13160 if (HAS_DDI(dev)) {
13161 int found;
13162
13163 /*
13164 * Haswell uses DDI functions to detect digital outputs.
13165 * On SKL pre-D0 the strap isn't connected, so we assume
13166 * it's there.
13167 */
13168 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
13169 /* WaIgnoreDDIAStrap: skl */
13170 if (found ||
13171 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
13172 intel_ddi_init(dev, PORT_A);
13173
13174 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13175 * register */
13176 found = I915_READ(SFUSE_STRAP);
13177
13178 if (found & SFUSE_STRAP_DDIB_DETECTED)
13179 intel_ddi_init(dev, PORT_B);
13180 if (found & SFUSE_STRAP_DDIC_DETECTED)
13181 intel_ddi_init(dev, PORT_C);
13182 if (found & SFUSE_STRAP_DDID_DETECTED)
13183 intel_ddi_init(dev, PORT_D);
13184 } else if (HAS_PCH_SPLIT(dev)) {
13185 int found;
13186 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
13187
13188 if (has_edp_a(dev))
13189 intel_dp_init(dev, DP_A, PORT_A);
13190
13191 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
13192 /* PCH SDVOB multiplex with HDMIB */
13193 found = intel_sdvo_init(dev, PCH_SDVOB, true);
13194 if (!found)
13195 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
13196 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
13197 intel_dp_init(dev, PCH_DP_B, PORT_B);
13198 }
13199
13200 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
13201 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
13202
13203 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
13204 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
13205
13206 if (I915_READ(PCH_DP_C) & DP_DETECTED)
13207 intel_dp_init(dev, PCH_DP_C, PORT_C);
13208
13209 if (I915_READ(PCH_DP_D) & DP_DETECTED)
13210 intel_dp_init(dev, PCH_DP_D, PORT_D);
13211 } else if (IS_VALLEYVIEW(dev)) {
13212 /*
13213 * The DP_DETECTED bit is the latched state of the DDC
13214 * SDA pin at boot. However since eDP doesn't require DDC
13215 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13216 * eDP ports may have been muxed to an alternate function.
13217 * Thus we can't rely on the DP_DETECTED bit alone to detect
13218 * eDP ports. Consult the VBT as well as DP_DETECTED to
13219 * detect eDP ports.
13220 */
13221 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13222 !intel_dp_is_edp(dev, PORT_B))
13223 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13224 PORT_B);
13225 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13226 intel_dp_is_edp(dev, PORT_B))
13227 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
13228
13229 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13230 !intel_dp_is_edp(dev, PORT_C))
13231 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13232 PORT_C);
13233 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13234 intel_dp_is_edp(dev, PORT_C))
13235 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
13236
13237 if (IS_CHERRYVIEW(dev)) {
13238 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
13239 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13240 PORT_D);
13241 /* eDP not supported on port D, so don't check VBT */
13242 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13243 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
13244 }
13245
13246 intel_dsi_init(dev);
13247 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
13248 bool found = false;
13249
13250 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
13251 DRM_DEBUG_KMS("probing SDVOB\n");
13252 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
13253 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13254 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
13255 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
13256 }
13257
13258 if (!found && SUPPORTS_INTEGRATED_DP(dev))
13259 intel_dp_init(dev, DP_B, PORT_B);
13260 }
13261
13262 /* Before G4X SDVOC doesn't have its own detect register */
13263
13264 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
13265 DRM_DEBUG_KMS("probing SDVOC\n");
13266 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
13267 }
13268
13269 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
13270
13271 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13272 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
13273 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
13274 }
13275 if (SUPPORTS_INTEGRATED_DP(dev))
13276 intel_dp_init(dev, DP_C, PORT_C);
13277 }
13278
13279 if (SUPPORTS_INTEGRATED_DP(dev) &&
13280 (I915_READ(DP_D) & DP_DETECTED))
13281 intel_dp_init(dev, DP_D, PORT_D);
13282 } else if (IS_GEN2(dev))
13283 intel_dvo_init(dev);
13284
13285 if (SUPPORTS_TV(dev))
13286 intel_tv_init(dev);
13287
13288 /*
13289 * FIXME: We don't have full atomic support yet, but we want to be
13290 * able to enable/test plane updates via the atomic interface in the
13291 * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
13292 * will take some atomic codepaths to lookup properties during
13293 * drmModeGetConnector() that unconditionally dereference
13294 * connector->state.
13295 *
13296 * We create a dummy connector state here for each connector to ensure
13297 * the DRM core doesn't try to dereference a NULL connector->state.
13298 * The actual connector properties will never be updated or contain
13299 * useful information, but since we're doing this specifically for
13300 * testing/debug of the plane operations (and only when a specific
13301 * kernel module option is given), that shouldn't really matter.
13302 *
13303 * We are also relying on these states to convert the legacy mode set
13304 * to use a drm_atomic_state struct. The states are kept consistent
13305 * with actual state, so that it is safe to rely on that instead of
13306 * the staged config.
13307 *
13308 * Once atomic support for crtc's + connectors lands, this loop should
13309 * be removed since we'll be setting up real connector state, which
13310 * will contain Intel-specific properties.
13311 */
13312 list_for_each_entry(connector,
13313 &dev->mode_config.connector_list,
13314 head) {
13315 if (!WARN_ON(connector->state)) {
13316 connector->state = kzalloc(sizeof(*connector->state),
13317 GFP_KERNEL);
13318 }
13319 }
13320
13321 intel_psr_init(dev);
13322
13323 for_each_intel_encoder(dev, encoder) {
13324 encoder->base.possible_crtcs = encoder->crtc_mask;
13325 encoder->base.possible_clones =
13326 intel_encoder_clones(encoder);
13327 }
13328
13329 intel_init_pch_refclk(dev);
13330
13331 drm_helper_move_panel_connectors_to_head(dev);
13332}
13333
13334static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13335{
13336 struct drm_device *dev = fb->dev;
13337 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
13338
13339 drm_framebuffer_cleanup(fb);
13340 mutex_lock(&dev->struct_mutex);
13341 WARN_ON(!intel_fb->obj->framebuffer_references--);
13342 drm_gem_object_unreference(&intel_fb->obj->base);
13343 mutex_unlock(&dev->struct_mutex);
13344 kfree(intel_fb);
13345}
13346
13347static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
13348 struct drm_file *file,
13349 unsigned int *handle)
13350{
13351 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
13352 struct drm_i915_gem_object *obj = intel_fb->obj;
13353
13354 return drm_gem_handle_create(file, &obj->base, handle);
13355}
13356
13357static const struct drm_framebuffer_funcs intel_fb_funcs = {
13358 .destroy = intel_user_framebuffer_destroy,
13359 .create_handle = intel_user_framebuffer_create_handle,
13360};
13361
13362static
13363u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13364 uint32_t pixel_format)
13365{
13366 u32 gen = INTEL_INFO(dev)->gen;
13367
13368 if (gen >= 9) {
13369 /* "The stride in bytes must not exceed the of the size of 8K
13370 * pixels and 32K bytes."
13371 */
13372 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13373 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13374 return 32*1024;
13375 } else if (gen >= 4) {
13376 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13377 return 16*1024;
13378 else
13379 return 32*1024;
13380 } else if (gen >= 3) {
13381 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13382 return 8*1024;
13383 else
13384 return 16*1024;
13385 } else {
13386 /* XXX DSPC is limited to 4k tiled */
13387 return 8*1024;
13388 }
13389}
13390
13391static int intel_framebuffer_init(struct drm_device *dev,
13392 struct intel_framebuffer *intel_fb,
13393 struct drm_mode_fb_cmd2 *mode_cmd,
13394 struct drm_i915_gem_object *obj)
13395{
13396 unsigned int aligned_height;
13397 int ret;
13398 u32 pitch_limit, stride_alignment;
13399
13400 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13401
13402 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13403 /* Enforce that fb modifier and tiling mode match, but only for
13404 * X-tiled. This is needed for FBC. */
13405 if (!!(obj->tiling_mode == I915_TILING_X) !=
13406 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13407 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13408 return -EINVAL;
13409 }
13410 } else {
13411 if (obj->tiling_mode == I915_TILING_X)
13412 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13413 else if (obj->tiling_mode == I915_TILING_Y) {
13414 DRM_DEBUG("No Y tiling for legacy addfb\n");
13415 return -EINVAL;
13416 }
13417 }
13418
13419 /* Passed in modifier sanity checking. */
13420 switch (mode_cmd->modifier[0]) {
13421 case I915_FORMAT_MOD_Y_TILED:
13422 case I915_FORMAT_MOD_Yf_TILED:
13423 if (INTEL_INFO(dev)->gen < 9) {
13424 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13425 mode_cmd->modifier[0]);
13426 return -EINVAL;
13427 }
13428 case DRM_FORMAT_MOD_NONE:
13429 case I915_FORMAT_MOD_X_TILED:
13430 break;
13431 default:
13432 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13433 mode_cmd->modifier[0]);
13434 return -EINVAL;
13435 }
13436
13437 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13438 mode_cmd->pixel_format);
13439 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13440 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13441 mode_cmd->pitches[0], stride_alignment);
13442 return -EINVAL;
13443 }
13444
13445 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13446 mode_cmd->pixel_format);
13447 if (mode_cmd->pitches[0] > pitch_limit) {
13448 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13449 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
13450 "tiled" : "linear",
13451 mode_cmd->pitches[0], pitch_limit);
13452 return -EINVAL;
13453 }
13454
13455 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
13456 mode_cmd->pitches[0] != obj->stride) {
13457 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13458 mode_cmd->pitches[0], obj->stride);
13459 return -EINVAL;
13460 }
13461
13462 /* Reject formats not supported by any plane early. */
13463 switch (mode_cmd->pixel_format) {
13464 case DRM_FORMAT_C8:
13465 case DRM_FORMAT_RGB565:
13466 case DRM_FORMAT_XRGB8888:
13467 case DRM_FORMAT_ARGB8888:
13468 break;
13469 case DRM_FORMAT_XRGB1555:
13470 case DRM_FORMAT_ARGB1555:
13471 if (INTEL_INFO(dev)->gen > 3) {
13472 DRM_DEBUG("unsupported pixel format: %s\n",
13473 drm_get_format_name(mode_cmd->pixel_format));
13474 return -EINVAL;
13475 }
13476 break;
13477 case DRM_FORMAT_XBGR8888:
13478 case DRM_FORMAT_ABGR8888:
13479 case DRM_FORMAT_XRGB2101010:
13480 case DRM_FORMAT_ARGB2101010:
13481 case DRM_FORMAT_XBGR2101010:
13482 case DRM_FORMAT_ABGR2101010:
13483 if (INTEL_INFO(dev)->gen < 4) {
13484 DRM_DEBUG("unsupported pixel format: %s\n",
13485 drm_get_format_name(mode_cmd->pixel_format));
13486 return -EINVAL;
13487 }
13488 break;
13489 case DRM_FORMAT_YUYV:
13490 case DRM_FORMAT_UYVY:
13491 case DRM_FORMAT_YVYU:
13492 case DRM_FORMAT_VYUY:
13493 if (INTEL_INFO(dev)->gen < 5) {
13494 DRM_DEBUG("unsupported pixel format: %s\n",
13495 drm_get_format_name(mode_cmd->pixel_format));
13496 return -EINVAL;
13497 }
13498 break;
13499 default:
13500 DRM_DEBUG("unsupported pixel format: %s\n",
13501 drm_get_format_name(mode_cmd->pixel_format));
13502 return -EINVAL;
13503 }
13504
13505 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
13506 if (mode_cmd->offsets[0] != 0)
13507 return -EINVAL;
13508
13509 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
13510 mode_cmd->pixel_format,
13511 mode_cmd->modifier[0]);
13512 /* FIXME drm helper for size checks (especially planar formats)? */
13513 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
13514 return -EINVAL;
13515
13516 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
13517 intel_fb->obj = obj;
13518 intel_fb->obj->framebuffer_references++;
13519
13520 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
13521 if (ret) {
13522 DRM_ERROR("framebuffer init failed %d\n", ret);
13523 return ret;
13524 }
13525
13526 return 0;
13527}
13528
13529static struct drm_framebuffer *
13530intel_user_framebuffer_create(struct drm_device *dev,
13531 struct drm_file *filp,
13532 struct drm_mode_fb_cmd2 *mode_cmd)
13533{
13534 struct drm_i915_gem_object *obj;
13535
13536 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
13537 mode_cmd->handles[0]));
13538 if (&obj->base == NULL)
13539 return ERR_PTR(-ENOENT);
13540
13541 return intel_framebuffer_create(dev, mode_cmd, obj);
13542}
13543
13544#ifndef CONFIG_DRM_I915_FBDEV
13545static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
13546{
13547}
13548#endif
13549
13550static const struct drm_mode_config_funcs intel_mode_funcs = {
13551 .fb_create = intel_user_framebuffer_create,
13552 .output_poll_changed = intel_fbdev_output_poll_changed,
13553 .atomic_check = intel_atomic_check,
13554 .atomic_commit = intel_atomic_commit,
13555};
13556
13557/* Set up chip specific display functions */
13558static void intel_init_display(struct drm_device *dev)
13559{
13560 struct drm_i915_private *dev_priv = dev->dev_private;
13561
13562 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
13563 dev_priv->display.find_dpll = g4x_find_best_dpll;
13564 else if (IS_CHERRYVIEW(dev))
13565 dev_priv->display.find_dpll = chv_find_best_dpll;
13566 else if (IS_VALLEYVIEW(dev))
13567 dev_priv->display.find_dpll = vlv_find_best_dpll;
13568 else if (IS_PINEVIEW(dev))
13569 dev_priv->display.find_dpll = pnv_find_best_dpll;
13570 else
13571 dev_priv->display.find_dpll = i9xx_find_best_dpll;
13572
13573 if (INTEL_INFO(dev)->gen >= 9) {
13574 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
13575 dev_priv->display.get_initial_plane_config =
13576 skylake_get_initial_plane_config;
13577 dev_priv->display.crtc_compute_clock =
13578 haswell_crtc_compute_clock;
13579 dev_priv->display.crtc_enable = haswell_crtc_enable;
13580 dev_priv->display.crtc_disable = haswell_crtc_disable;
13581 dev_priv->display.off = ironlake_crtc_off;
13582 dev_priv->display.update_primary_plane =
13583 skylake_update_primary_plane;
13584 } else if (HAS_DDI(dev)) {
13585 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
13586 dev_priv->display.get_initial_plane_config =
13587 ironlake_get_initial_plane_config;
13588 dev_priv->display.crtc_compute_clock =
13589 haswell_crtc_compute_clock;
13590 dev_priv->display.crtc_enable = haswell_crtc_enable;
13591 dev_priv->display.crtc_disable = haswell_crtc_disable;
13592 dev_priv->display.off = ironlake_crtc_off;
13593 dev_priv->display.update_primary_plane =
13594 ironlake_update_primary_plane;
13595 } else if (HAS_PCH_SPLIT(dev)) {
13596 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
13597 dev_priv->display.get_initial_plane_config =
13598 ironlake_get_initial_plane_config;
13599 dev_priv->display.crtc_compute_clock =
13600 ironlake_crtc_compute_clock;
13601 dev_priv->display.crtc_enable = ironlake_crtc_enable;
13602 dev_priv->display.crtc_disable = ironlake_crtc_disable;
13603 dev_priv->display.off = ironlake_crtc_off;
13604 dev_priv->display.update_primary_plane =
13605 ironlake_update_primary_plane;
13606 } else if (IS_VALLEYVIEW(dev)) {
13607 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
13608 dev_priv->display.get_initial_plane_config =
13609 i9xx_get_initial_plane_config;
13610 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
13611 dev_priv->display.crtc_enable = valleyview_crtc_enable;
13612 dev_priv->display.crtc_disable = i9xx_crtc_disable;
13613 dev_priv->display.off = i9xx_crtc_off;
13614 dev_priv->display.update_primary_plane =
13615 i9xx_update_primary_plane;
13616 } else {
13617 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
13618 dev_priv->display.get_initial_plane_config =
13619 i9xx_get_initial_plane_config;
13620 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
13621 dev_priv->display.crtc_enable = i9xx_crtc_enable;
13622 dev_priv->display.crtc_disable = i9xx_crtc_disable;
13623 dev_priv->display.off = i9xx_crtc_off;
13624 dev_priv->display.update_primary_plane =
13625 i9xx_update_primary_plane;
13626 }
13627
13628 /* Returns the core display clock speed */
13629 if (IS_SKYLAKE(dev))
13630 dev_priv->display.get_display_clock_speed =
13631 skylake_get_display_clock_speed;
13632 else if (IS_BROADWELL(dev))
13633 dev_priv->display.get_display_clock_speed =
13634 broadwell_get_display_clock_speed;
13635 else if (IS_HASWELL(dev))
13636 dev_priv->display.get_display_clock_speed =
13637 haswell_get_display_clock_speed;
13638 else if (IS_VALLEYVIEW(dev))
13639 dev_priv->display.get_display_clock_speed =
13640 valleyview_get_display_clock_speed;
13641 else if (IS_GEN5(dev))
13642 dev_priv->display.get_display_clock_speed =
13643 ilk_get_display_clock_speed;
13644 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
13645 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
13646 dev_priv->display.get_display_clock_speed =
13647 i945_get_display_clock_speed;
13648 else if (IS_I915G(dev))
13649 dev_priv->display.get_display_clock_speed =
13650 i915_get_display_clock_speed;
13651 else if (IS_I945GM(dev) || IS_845G(dev))
13652 dev_priv->display.get_display_clock_speed =
13653 i9xx_misc_get_display_clock_speed;
13654 else if (IS_PINEVIEW(dev))
13655 dev_priv->display.get_display_clock_speed =
13656 pnv_get_display_clock_speed;
13657 else if (IS_I915GM(dev))
13658 dev_priv->display.get_display_clock_speed =
13659 i915gm_get_display_clock_speed;
13660 else if (IS_I865G(dev))
13661 dev_priv->display.get_display_clock_speed =
13662 i865_get_display_clock_speed;
13663 else if (IS_I85X(dev))
13664 dev_priv->display.get_display_clock_speed =
13665 i855_get_display_clock_speed;
13666 else /* 852, 830 */
13667 dev_priv->display.get_display_clock_speed =
13668 i830_get_display_clock_speed;
13669
13670 if (IS_GEN5(dev)) {
13671 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
13672 } else if (IS_GEN6(dev)) {
13673 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
13674 } else if (IS_IVYBRIDGE(dev)) {
13675 /* FIXME: detect B0+ stepping and use auto training */
13676 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
13677 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
13678 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
13679 } else if (IS_VALLEYVIEW(dev)) {
13680 dev_priv->display.modeset_global_resources =
13681 valleyview_modeset_global_resources;
13682 }
13683
13684 switch (INTEL_INFO(dev)->gen) {
13685 case 2:
13686 dev_priv->display.queue_flip = intel_gen2_queue_flip;
13687 break;
13688
13689 case 3:
13690 dev_priv->display.queue_flip = intel_gen3_queue_flip;
13691 break;
13692
13693 case 4:
13694 case 5:
13695 dev_priv->display.queue_flip = intel_gen4_queue_flip;
13696 break;
13697
13698 case 6:
13699 dev_priv->display.queue_flip = intel_gen6_queue_flip;
13700 break;
13701 case 7:
13702 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
13703 dev_priv->display.queue_flip = intel_gen7_queue_flip;
13704 break;
13705 case 9:
13706 /* Drop through - unsupported since execlist only. */
13707 default:
13708 /* Default just returns -ENODEV to indicate unsupported */
13709 dev_priv->display.queue_flip = intel_default_queue_flip;
13710 }
13711
13712 intel_panel_init_backlight_funcs(dev);
13713
13714 mutex_init(&dev_priv->pps_mutex);
13715}
13716
13717/*
13718 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
13719 * resume, or other times. This quirk makes sure that's the case for
13720 * affected systems.
13721 */
13722static void quirk_pipea_force(struct drm_device *dev)
13723{
13724 struct drm_i915_private *dev_priv = dev->dev_private;
13725
13726 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
13727 DRM_INFO("applying pipe a force quirk\n");
13728}
13729
13730static void quirk_pipeb_force(struct drm_device *dev)
13731{
13732 struct drm_i915_private *dev_priv = dev->dev_private;
13733
13734 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
13735 DRM_INFO("applying pipe b force quirk\n");
13736}
13737
13738/*
13739 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
13740 */
13741static void quirk_ssc_force_disable(struct drm_device *dev)
13742{
13743 struct drm_i915_private *dev_priv = dev->dev_private;
13744 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
13745 DRM_INFO("applying lvds SSC disable quirk\n");
13746}
13747
13748/*
13749 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
13750 * brightness value
13751 */
13752static void quirk_invert_brightness(struct drm_device *dev)
13753{
13754 struct drm_i915_private *dev_priv = dev->dev_private;
13755 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
13756 DRM_INFO("applying inverted panel brightness quirk\n");
13757}
13758
13759/* Some VBT's incorrectly indicate no backlight is present */
13760static void quirk_backlight_present(struct drm_device *dev)
13761{
13762 struct drm_i915_private *dev_priv = dev->dev_private;
13763 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
13764 DRM_INFO("applying backlight present quirk\n");
13765}
13766
13767struct intel_quirk {
13768 int device;
13769 int subsystem_vendor;
13770 int subsystem_device;
13771 void (*hook)(struct drm_device *dev);
13772};
13773
13774/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
13775struct intel_dmi_quirk {
13776 void (*hook)(struct drm_device *dev);
13777 const struct dmi_system_id (*dmi_id_list)[];
13778};
13779
13780static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
13781{
13782 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
13783 return 1;
13784}
13785
13786static const struct intel_dmi_quirk intel_dmi_quirks[] = {
13787 {
13788 .dmi_id_list = &(const struct dmi_system_id[]) {
13789 {
13790 .callback = intel_dmi_reverse_brightness,
13791 .ident = "NCR Corporation",
13792 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
13793 DMI_MATCH(DMI_PRODUCT_NAME, ""),
13794 },
13795 },
13796 { } /* terminating entry */
13797 },
13798 .hook = quirk_invert_brightness,
13799 },
13800};
13801
13802static struct intel_quirk intel_quirks[] = {
13803 /* HP Mini needs pipe A force quirk (LP: #322104) */
13804 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
13805
13806 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
13807 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
13808
13809 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
13810 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
13811
13812 /* 830 needs to leave pipe A & dpll A up */
13813 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
13814
13815 /* 830 needs to leave pipe B & dpll B up */
13816 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
13817
13818 /* Lenovo U160 cannot use SSC on LVDS */
13819 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
13820
13821 /* Sony Vaio Y cannot use SSC on LVDS */
13822 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
13823
13824 /* Acer Aspire 5734Z must invert backlight brightness */
13825 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
13826
13827 /* Acer/eMachines G725 */
13828 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
13829
13830 /* Acer/eMachines e725 */
13831 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
13832
13833 /* Acer/Packard Bell NCL20 */
13834 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
13835
13836 /* Acer Aspire 4736Z */
13837 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
13838
13839 /* Acer Aspire 5336 */
13840 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
13841
13842 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
13843 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
13844
13845 /* Acer C720 Chromebook (Core i3 4005U) */
13846 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
13847
13848 /* Apple Macbook 2,1 (Core 2 T7400) */
13849 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
13850
13851 /* Toshiba CB35 Chromebook (Celeron 2955U) */
13852 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
13853
13854 /* HP Chromebook 14 (Celeron 2955U) */
13855 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
13856
13857 /* Dell Chromebook 11 */
13858 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
13859};
13860
13861static void intel_init_quirks(struct drm_device *dev)
13862{
13863 struct pci_dev *d = dev->pdev;
13864 int i;
13865
13866 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
13867 struct intel_quirk *q = &intel_quirks[i];
13868
13869 if (d->device == q->device &&
13870 (d->subsystem_vendor == q->subsystem_vendor ||
13871 q->subsystem_vendor == PCI_ANY_ID) &&
13872 (d->subsystem_device == q->subsystem_device ||
13873 q->subsystem_device == PCI_ANY_ID))
13874 q->hook(dev);
13875 }
13876 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
13877 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
13878 intel_dmi_quirks[i].hook(dev);
13879 }
13880}
13881
13882/* Disable the VGA plane that we never use */
13883static void i915_disable_vga(struct drm_device *dev)
13884{
13885 struct drm_i915_private *dev_priv = dev->dev_private;
13886 u8 sr1;
13887 u32 vga_reg = i915_vgacntrl_reg(dev);
13888
13889 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
13890 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
13891 outb(SR01, VGA_SR_INDEX);
13892 sr1 = inb(VGA_SR_DATA);
13893 outb(sr1 | 1<<5, VGA_SR_DATA);
13894 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
13895 udelay(300);
13896
13897 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
13898 POSTING_READ(vga_reg);
13899}
13900
13901void intel_modeset_init_hw(struct drm_device *dev)
13902{
13903 intel_prepare_ddi(dev);
13904
13905 if (IS_VALLEYVIEW(dev))
13906 vlv_update_cdclk(dev);
13907
13908 intel_init_clock_gating(dev);
13909
13910 intel_enable_gt_powersave(dev);
13911}
13912
13913void intel_modeset_init(struct drm_device *dev)
13914{
13915 struct drm_i915_private *dev_priv = dev->dev_private;
13916 int sprite, ret;
13917 enum pipe pipe;
13918 struct intel_crtc *crtc;
13919
13920 drm_mode_config_init(dev);
13921
13922 dev->mode_config.min_width = 0;
13923 dev->mode_config.min_height = 0;
13924
13925 dev->mode_config.preferred_depth = 24;
13926 dev->mode_config.prefer_shadow = 1;
13927
13928 dev->mode_config.allow_fb_modifiers = true;
13929
13930 dev->mode_config.funcs = &intel_mode_funcs;
13931
13932 intel_init_quirks(dev);
13933
13934 intel_init_pm(dev);
13935
13936 if (INTEL_INFO(dev)->num_pipes == 0)
13937 return;
13938
13939 intel_init_display(dev);
13940 intel_init_audio(dev);
13941
13942 if (IS_GEN2(dev)) {
13943 dev->mode_config.max_width = 2048;
13944 dev->mode_config.max_height = 2048;
13945 } else if (IS_GEN3(dev)) {
13946 dev->mode_config.max_width = 4096;
13947 dev->mode_config.max_height = 4096;
13948 } else {
13949 dev->mode_config.max_width = 8192;
13950 dev->mode_config.max_height = 8192;
13951 }
13952
13953 if (IS_845G(dev) || IS_I865G(dev)) {
13954 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
13955 dev->mode_config.cursor_height = 1023;
13956 } else if (IS_GEN2(dev)) {
13957 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
13958 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
13959 } else {
13960 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
13961 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
13962 }
13963
13964 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
13965
13966 DRM_DEBUG_KMS("%d display pipe%s available.\n",
13967 INTEL_INFO(dev)->num_pipes,
13968 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
13969
13970 for_each_pipe(dev_priv, pipe) {
13971 intel_crtc_init(dev, pipe);
13972 for_each_sprite(dev_priv, pipe, sprite) {
13973 ret = intel_plane_init(dev, pipe, sprite);
13974 if (ret)
13975 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
13976 pipe_name(pipe), sprite_name(pipe, sprite), ret);
13977 }
13978 }
13979
13980 intel_init_dpio(dev);
13981
13982 intel_shared_dpll_init(dev);
13983
13984 /* Just disable it once at startup */
13985 i915_disable_vga(dev);
13986 intel_setup_outputs(dev);
13987
13988 /* Just in case the BIOS is doing something questionable. */
13989 intel_fbc_disable(dev);
13990
13991 drm_modeset_lock_all(dev);
13992 intel_modeset_setup_hw_state(dev, false);
13993 drm_modeset_unlock_all(dev);
13994
13995 for_each_intel_crtc(dev, crtc) {
13996 if (!crtc->active)
13997 continue;
13998
13999 /*
14000 * Note that reserving the BIOS fb up front prevents us
14001 * from stuffing other stolen allocations like the ring
14002 * on top. This prevents some ugliness at boot time, and
14003 * can even allow for smooth boot transitions if the BIOS
14004 * fb is large enough for the active pipe configuration.
14005 */
14006 if (dev_priv->display.get_initial_plane_config) {
14007 dev_priv->display.get_initial_plane_config(crtc,
14008 &crtc->plane_config);
14009 /*
14010 * If the fb is shared between multiple heads, we'll
14011 * just get the first one.
14012 */
14013 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
14014 }
14015 }
14016}
14017
14018static void intel_enable_pipe_a(struct drm_device *dev)
14019{
14020 struct intel_connector *connector;
14021 struct drm_connector *crt = NULL;
14022 struct intel_load_detect_pipe load_detect_temp;
14023 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
14024
14025 /* We can't just switch on the pipe A, we need to set things up with a
14026 * proper mode and output configuration. As a gross hack, enable pipe A
14027 * by enabling the load detect pipe once. */
14028 for_each_intel_connector(dev, connector) {
14029 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14030 crt = &connector->base;
14031 break;
14032 }
14033 }
14034
14035 if (!crt)
14036 return;
14037
14038 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
14039 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
14040}
14041
14042static bool
14043intel_check_plane_mapping(struct intel_crtc *crtc)
14044{
14045 struct drm_device *dev = crtc->base.dev;
14046 struct drm_i915_private *dev_priv = dev->dev_private;
14047 u32 reg, val;
14048
14049 if (INTEL_INFO(dev)->num_pipes == 1)
14050 return true;
14051
14052 reg = DSPCNTR(!crtc->plane);
14053 val = I915_READ(reg);
14054
14055 if ((val & DISPLAY_PLANE_ENABLE) &&
14056 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14057 return false;
14058
14059 return true;
14060}
14061
14062static void intel_sanitize_crtc(struct intel_crtc *crtc)
14063{
14064 struct drm_device *dev = crtc->base.dev;
14065 struct drm_i915_private *dev_priv = dev->dev_private;
14066 u32 reg;
14067
14068 /* Clear any frame start delays used for debugging left by the BIOS */
14069 reg = PIPECONF(crtc->config->cpu_transcoder);
14070 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14071
14072 /* restore vblank interrupts to correct state */
14073 drm_crtc_vblank_reset(&crtc->base);
14074 if (crtc->active) {
14075 update_scanline_offset(crtc);
14076 drm_crtc_vblank_on(&crtc->base);
14077 }
14078
14079 /* We need to sanitize the plane -> pipe mapping first because this will
14080 * disable the crtc (and hence change the state) if it is wrong. Note
14081 * that gen4+ has a fixed plane -> pipe mapping. */
14082 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
14083 struct intel_connector *connector;
14084 bool plane;
14085
14086 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14087 crtc->base.base.id);
14088
14089 /* Pipe has the wrong plane attached and the plane is active.
14090 * Temporarily change the plane mapping and disable everything
14091 * ... */
14092 plane = crtc->plane;
14093 crtc->plane = !plane;
14094 crtc->primary_enabled = true;
14095 dev_priv->display.crtc_disable(&crtc->base);
14096 crtc->plane = plane;
14097
14098 /* ... and break all links. */
14099 for_each_intel_connector(dev, connector) {
14100 if (connector->encoder->base.crtc != &crtc->base)
14101 continue;
14102
14103 connector->base.dpms = DRM_MODE_DPMS_OFF;
14104 connector->base.encoder = NULL;
14105 }
14106 /* multiple connectors may have the same encoder:
14107 * handle them and break crtc link separately */
14108 for_each_intel_connector(dev, connector)
14109 if (connector->encoder->base.crtc == &crtc->base) {
14110 connector->encoder->base.crtc = NULL;
14111 connector->encoder->connectors_active = false;
14112 }
14113
14114 WARN_ON(crtc->active);
14115 crtc->base.state->enable = false;
14116 crtc->base.enabled = false;
14117 }
14118
14119 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14120 crtc->pipe == PIPE_A && !crtc->active) {
14121 /* BIOS forgot to enable pipe A, this mostly happens after
14122 * resume. Force-enable the pipe to fix this, the update_dpms
14123 * call below we restore the pipe to the right state, but leave
14124 * the required bits on. */
14125 intel_enable_pipe_a(dev);
14126 }
14127
14128 /* Adjust the state of the output pipe according to whether we
14129 * have active connectors/encoders. */
14130 intel_crtc_update_dpms(&crtc->base);
14131
14132 if (crtc->active != crtc->base.state->enable) {
14133 struct intel_encoder *encoder;
14134
14135 /* This can happen either due to bugs in the get_hw_state
14136 * functions or because the pipe is force-enabled due to the
14137 * pipe A quirk. */
14138 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14139 crtc->base.base.id,
14140 crtc->base.state->enable ? "enabled" : "disabled",
14141 crtc->active ? "enabled" : "disabled");
14142
14143 crtc->base.state->enable = crtc->active;
14144 crtc->base.enabled = crtc->active;
14145
14146 /* Because we only establish the connector -> encoder ->
14147 * crtc links if something is active, this means the
14148 * crtc is now deactivated. Break the links. connector
14149 * -> encoder links are only establish when things are
14150 * actually up, hence no need to break them. */
14151 WARN_ON(crtc->active);
14152
14153 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14154 WARN_ON(encoder->connectors_active);
14155 encoder->base.crtc = NULL;
14156 }
14157 }
14158
14159 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
14160 /*
14161 * We start out with underrun reporting disabled to avoid races.
14162 * For correct bookkeeping mark this on active crtcs.
14163 *
14164 * Also on gmch platforms we dont have any hardware bits to
14165 * disable the underrun reporting. Which means we need to start
14166 * out with underrun reporting disabled also on inactive pipes,
14167 * since otherwise we'll complain about the garbage we read when
14168 * e.g. coming up after runtime pm.
14169 *
14170 * No protection against concurrent access is required - at
14171 * worst a fifo underrun happens which also sets this to false.
14172 */
14173 crtc->cpu_fifo_underrun_disabled = true;
14174 crtc->pch_fifo_underrun_disabled = true;
14175 }
14176}
14177
14178static void intel_sanitize_encoder(struct intel_encoder *encoder)
14179{
14180 struct intel_connector *connector;
14181 struct drm_device *dev = encoder->base.dev;
14182
14183 /* We need to check both for a crtc link (meaning that the
14184 * encoder is active and trying to read from a pipe) and the
14185 * pipe itself being active. */
14186 bool has_active_crtc = encoder->base.crtc &&
14187 to_intel_crtc(encoder->base.crtc)->active;
14188
14189 if (encoder->connectors_active && !has_active_crtc) {
14190 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14191 encoder->base.base.id,
14192 encoder->base.name);
14193
14194 /* Connector is active, but has no active pipe. This is
14195 * fallout from our resume register restoring. Disable
14196 * the encoder manually again. */
14197 if (encoder->base.crtc) {
14198 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14199 encoder->base.base.id,
14200 encoder->base.name);
14201 encoder->disable(encoder);
14202 if (encoder->post_disable)
14203 encoder->post_disable(encoder);
14204 }
14205 encoder->base.crtc = NULL;
14206 encoder->connectors_active = false;
14207
14208 /* Inconsistent output/port/pipe state happens presumably due to
14209 * a bug in one of the get_hw_state functions. Or someplace else
14210 * in our code, like the register restore mess on resume. Clamp
14211 * things to off as a safer default. */
14212 for_each_intel_connector(dev, connector) {
14213 if (connector->encoder != encoder)
14214 continue;
14215 connector->base.dpms = DRM_MODE_DPMS_OFF;
14216 connector->base.encoder = NULL;
14217 }
14218 }
14219 /* Enabled encoders without active connectors will be fixed in
14220 * the crtc fixup. */
14221}
14222
14223void i915_redisable_vga_power_on(struct drm_device *dev)
14224{
14225 struct drm_i915_private *dev_priv = dev->dev_private;
14226 u32 vga_reg = i915_vgacntrl_reg(dev);
14227
14228 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14229 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14230 i915_disable_vga(dev);
14231 }
14232}
14233
14234void i915_redisable_vga(struct drm_device *dev)
14235{
14236 struct drm_i915_private *dev_priv = dev->dev_private;
14237
14238 /* This function can be called both from intel_modeset_setup_hw_state or
14239 * at a very early point in our resume sequence, where the power well
14240 * structures are not yet restored. Since this function is at a very
14241 * paranoid "someone might have enabled VGA while we were not looking"
14242 * level, just check if the power well is enabled instead of trying to
14243 * follow the "don't touch the power well if we don't need it" policy
14244 * the rest of the driver uses. */
14245 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
14246 return;
14247
14248 i915_redisable_vga_power_on(dev);
14249}
14250
14251static bool primary_get_hw_state(struct intel_crtc *crtc)
14252{
14253 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14254
14255 if (!crtc->active)
14256 return false;
14257
14258 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14259}
14260
14261static void intel_modeset_readout_hw_state(struct drm_device *dev)
14262{
14263 struct drm_i915_private *dev_priv = dev->dev_private;
14264 enum pipe pipe;
14265 struct intel_crtc *crtc;
14266 struct intel_encoder *encoder;
14267 struct intel_connector *connector;
14268 int i;
14269
14270 for_each_intel_crtc(dev, crtc) {
14271 memset(crtc->config, 0, sizeof(*crtc->config));
14272
14273 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
14274
14275 crtc->active = dev_priv->display.get_pipe_config(crtc,
14276 crtc->config);
14277
14278 crtc->base.state->enable = crtc->active;
14279 crtc->base.enabled = crtc->active;
14280 crtc->primary_enabled = primary_get_hw_state(crtc);
14281
14282 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14283 crtc->base.base.id,
14284 crtc->active ? "enabled" : "disabled");
14285 }
14286
14287 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14288 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14289
14290 pll->on = pll->get_hw_state(dev_priv, pll,
14291 &pll->config.hw_state);
14292 pll->active = 0;
14293 pll->config.crtc_mask = 0;
14294 for_each_intel_crtc(dev, crtc) {
14295 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
14296 pll->active++;
14297 pll->config.crtc_mask |= 1 << crtc->pipe;
14298 }
14299 }
14300
14301 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
14302 pll->name, pll->config.crtc_mask, pll->on);
14303
14304 if (pll->config.crtc_mask)
14305 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
14306 }
14307
14308 for_each_intel_encoder(dev, encoder) {
14309 pipe = 0;
14310
14311 if (encoder->get_hw_state(encoder, &pipe)) {
14312 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14313 encoder->base.crtc = &crtc->base;
14314 encoder->get_config(encoder, crtc->config);
14315 } else {
14316 encoder->base.crtc = NULL;
14317 }
14318
14319 encoder->connectors_active = false;
14320 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
14321 encoder->base.base.id,
14322 encoder->base.name,
14323 encoder->base.crtc ? "enabled" : "disabled",
14324 pipe_name(pipe));
14325 }
14326
14327 for_each_intel_connector(dev, connector) {
14328 if (connector->get_hw_state(connector)) {
14329 connector->base.dpms = DRM_MODE_DPMS_ON;
14330 connector->encoder->connectors_active = true;
14331 connector->base.encoder = &connector->encoder->base;
14332 } else {
14333 connector->base.dpms = DRM_MODE_DPMS_OFF;
14334 connector->base.encoder = NULL;
14335 }
14336 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14337 connector->base.base.id,
14338 connector->base.name,
14339 connector->base.encoder ? "enabled" : "disabled");
14340 }
14341}
14342
14343/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14344 * and i915 state tracking structures. */
14345void intel_modeset_setup_hw_state(struct drm_device *dev,
14346 bool force_restore)
14347{
14348 struct drm_i915_private *dev_priv = dev->dev_private;
14349 enum pipe pipe;
14350 struct intel_crtc *crtc;
14351 struct intel_encoder *encoder;
14352 int i;
14353
14354 intel_modeset_readout_hw_state(dev);
14355
14356 /*
14357 * Now that we have the config, copy it to each CRTC struct
14358 * Note that this could go away if we move to using crtc_config
14359 * checking everywhere.
14360 */
14361 for_each_intel_crtc(dev, crtc) {
14362 if (crtc->active && i915.fastboot) {
14363 intel_mode_from_pipe_config(&crtc->base.mode,
14364 crtc->config);
14365 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14366 crtc->base.base.id);
14367 drm_mode_debug_printmodeline(&crtc->base.mode);
14368 }
14369 }
14370
14371 /* HW state is read out, now we need to sanitize this mess. */
14372 for_each_intel_encoder(dev, encoder) {
14373 intel_sanitize_encoder(encoder);
14374 }
14375
14376 for_each_pipe(dev_priv, pipe) {
14377 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14378 intel_sanitize_crtc(crtc);
14379 intel_dump_pipe_config(crtc, crtc->config,
14380 "[setup_hw_state]");
14381 }
14382
14383 intel_modeset_update_connector_atomic_state(dev);
14384
14385 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14386 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14387
14388 if (!pll->on || pll->active)
14389 continue;
14390
14391 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14392
14393 pll->disable(dev_priv, pll);
14394 pll->on = false;
14395 }
14396
14397 if (IS_GEN9(dev))
14398 skl_wm_get_hw_state(dev);
14399 else if (HAS_PCH_SPLIT(dev))
14400 ilk_wm_get_hw_state(dev);
14401
14402 if (force_restore) {
14403 i915_redisable_vga(dev);
14404
14405 /*
14406 * We need to use raw interfaces for restoring state to avoid
14407 * checking (bogus) intermediate states.
14408 */
14409 for_each_pipe(dev_priv, pipe) {
14410 struct drm_crtc *crtc =
14411 dev_priv->pipe_to_crtc_mapping[pipe];
14412
14413 intel_crtc_restore_mode(crtc);
14414 }
14415 } else {
14416 intel_modeset_update_staged_output_state(dev);
14417 }
14418
14419 intel_modeset_check_state(dev);
14420}
14421
14422void intel_modeset_gem_init(struct drm_device *dev)
14423{
14424 struct drm_i915_private *dev_priv = dev->dev_private;
14425 struct drm_crtc *c;
14426 struct drm_i915_gem_object *obj;
14427
14428 mutex_lock(&dev->struct_mutex);
14429 intel_init_gt_powersave(dev);
14430 mutex_unlock(&dev->struct_mutex);
14431
14432 /*
14433 * There may be no VBT; and if the BIOS enabled SSC we can
14434 * just keep using it to avoid unnecessary flicker. Whereas if the
14435 * BIOS isn't using it, don't assume it will work even if the VBT
14436 * indicates as much.
14437 */
14438 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14439 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14440 DREF_SSC1_ENABLE);
14441
14442 intel_modeset_init_hw(dev);
14443
14444 intel_setup_overlay(dev);
14445
14446 /*
14447 * Make sure any fbs we allocated at startup are properly
14448 * pinned & fenced. When we do the allocation it's too early
14449 * for this.
14450 */
14451 mutex_lock(&dev->struct_mutex);
14452 for_each_crtc(dev, c) {
14453 obj = intel_fb_obj(c->primary->fb);
14454 if (obj == NULL)
14455 continue;
14456
14457 if (intel_pin_and_fence_fb_obj(c->primary,
14458 c->primary->fb,
14459 c->primary->state,
14460 NULL)) {
14461 DRM_ERROR("failed to pin boot fb on pipe %d\n",
14462 to_intel_crtc(c)->pipe);
14463 drm_framebuffer_unreference(c->primary->fb);
14464 c->primary->fb = NULL;
14465 update_state_fb(c->primary);
14466 }
14467 }
14468 mutex_unlock(&dev->struct_mutex);
14469
14470 intel_backlight_register(dev);
14471}
14472
14473void intel_connector_unregister(struct intel_connector *intel_connector)
14474{
14475 struct drm_connector *connector = &intel_connector->base;
14476
14477 intel_panel_destroy_backlight(connector);
14478 drm_connector_unregister(connector);
14479}
14480
14481void intel_modeset_cleanup(struct drm_device *dev)
14482{
14483 struct drm_i915_private *dev_priv = dev->dev_private;
14484 struct drm_connector *connector;
14485
14486 intel_disable_gt_powersave(dev);
14487
14488 intel_backlight_unregister(dev);
14489
14490 /*
14491 * Interrupts and polling as the first thing to avoid creating havoc.
14492 * Too much stuff here (turning of connectors, ...) would
14493 * experience fancy races otherwise.
14494 */
14495 intel_irq_uninstall(dev_priv);
14496
14497 /*
14498 * Due to the hpd irq storm handling the hotplug work can re-arm the
14499 * poll handlers. Hence disable polling after hpd handling is shut down.
14500 */
14501 drm_kms_helper_poll_fini(dev);
14502
14503 mutex_lock(&dev->struct_mutex);
14504
14505 intel_unregister_dsm_handler();
14506
14507 intel_fbc_disable(dev);
14508
14509 mutex_unlock(&dev->struct_mutex);
14510
14511 /* flush any delayed tasks or pending work */
14512 flush_scheduled_work();
14513
14514 /* destroy the backlight and sysfs files before encoders/connectors */
14515 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
14516 struct intel_connector *intel_connector;
14517
14518 intel_connector = to_intel_connector(connector);
14519 intel_connector->unregister(intel_connector);
14520 }
14521
14522 drm_mode_config_cleanup(dev);
14523
14524 intel_cleanup_overlay(dev);
14525
14526 mutex_lock(&dev->struct_mutex);
14527 intel_cleanup_gt_powersave(dev);
14528 mutex_unlock(&dev->struct_mutex);
14529}
14530
14531/*
14532 * Return which encoder is currently attached for connector.
14533 */
14534struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
14535{
14536 return &intel_attached_encoder(connector)->base;
14537}
14538
14539void intel_connector_attach_encoder(struct intel_connector *connector,
14540 struct intel_encoder *encoder)
14541{
14542 connector->encoder = encoder;
14543 drm_mode_connector_attach_encoder(&connector->base,
14544 &encoder->base);
14545}
14546
14547/*
14548 * set vga decode state - true == enable VGA decode
14549 */
14550int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
14551{
14552 struct drm_i915_private *dev_priv = dev->dev_private;
14553 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
14554 u16 gmch_ctrl;
14555
14556 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
14557 DRM_ERROR("failed to read control word\n");
14558 return -EIO;
14559 }
14560
14561 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
14562 return 0;
14563
14564 if (state)
14565 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
14566 else
14567 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
14568
14569 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
14570 DRM_ERROR("failed to write control word\n");
14571 return -EIO;
14572 }
14573
14574 return 0;
14575}
14576
14577struct intel_display_error_state {
14578
14579 u32 power_well_driver;
14580
14581 int num_transcoders;
14582
14583 struct intel_cursor_error_state {
14584 u32 control;
14585 u32 position;
14586 u32 base;
14587 u32 size;
14588 } cursor[I915_MAX_PIPES];
14589
14590 struct intel_pipe_error_state {
14591 bool power_domain_on;
14592 u32 source;
14593 u32 stat;
14594 } pipe[I915_MAX_PIPES];
14595
14596 struct intel_plane_error_state {
14597 u32 control;
14598 u32 stride;
14599 u32 size;
14600 u32 pos;
14601 u32 addr;
14602 u32 surface;
14603 u32 tile_offset;
14604 } plane[I915_MAX_PIPES];
14605
14606 struct intel_transcoder_error_state {
14607 bool power_domain_on;
14608 enum transcoder cpu_transcoder;
14609
14610 u32 conf;
14611
14612 u32 htotal;
14613 u32 hblank;
14614 u32 hsync;
14615 u32 vtotal;
14616 u32 vblank;
14617 u32 vsync;
14618 } transcoder[4];
14619};
14620
14621struct intel_display_error_state *
14622intel_display_capture_error_state(struct drm_device *dev)
14623{
14624 struct drm_i915_private *dev_priv = dev->dev_private;
14625 struct intel_display_error_state *error;
14626 int transcoders[] = {
14627 TRANSCODER_A,
14628 TRANSCODER_B,
14629 TRANSCODER_C,
14630 TRANSCODER_EDP,
14631 };
14632 int i;
14633
14634 if (INTEL_INFO(dev)->num_pipes == 0)
14635 return NULL;
14636
14637 error = kzalloc(sizeof(*error), GFP_ATOMIC);
14638 if (error == NULL)
14639 return NULL;
14640
14641 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
14642 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
14643
14644 for_each_pipe(dev_priv, i) {
14645 error->pipe[i].power_domain_on =
14646 __intel_display_power_is_enabled(dev_priv,
14647 POWER_DOMAIN_PIPE(i));
14648 if (!error->pipe[i].power_domain_on)
14649 continue;
14650
14651 error->cursor[i].control = I915_READ(CURCNTR(i));
14652 error->cursor[i].position = I915_READ(CURPOS(i));
14653 error->cursor[i].base = I915_READ(CURBASE(i));
14654
14655 error->plane[i].control = I915_READ(DSPCNTR(i));
14656 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
14657 if (INTEL_INFO(dev)->gen <= 3) {
14658 error->plane[i].size = I915_READ(DSPSIZE(i));
14659 error->plane[i].pos = I915_READ(DSPPOS(i));
14660 }
14661 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
14662 error->plane[i].addr = I915_READ(DSPADDR(i));
14663 if (INTEL_INFO(dev)->gen >= 4) {
14664 error->plane[i].surface = I915_READ(DSPSURF(i));
14665 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
14666 }
14667
14668 error->pipe[i].source = I915_READ(PIPESRC(i));
14669
14670 if (HAS_GMCH_DISPLAY(dev))
14671 error->pipe[i].stat = I915_READ(PIPESTAT(i));
14672 }
14673
14674 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
14675 if (HAS_DDI(dev_priv->dev))
14676 error->num_transcoders++; /* Account for eDP. */
14677
14678 for (i = 0; i < error->num_transcoders; i++) {
14679 enum transcoder cpu_transcoder = transcoders[i];
14680
14681 error->transcoder[i].power_domain_on =
14682 __intel_display_power_is_enabled(dev_priv,
14683 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
14684 if (!error->transcoder[i].power_domain_on)
14685 continue;
14686
14687 error->transcoder[i].cpu_transcoder = cpu_transcoder;
14688
14689 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
14690 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
14691 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
14692 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
14693 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
14694 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
14695 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
14696 }
14697
14698 return error;
14699}
14700
14701#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
14702
14703void
14704intel_display_print_error_state(struct drm_i915_error_state_buf *m,
14705 struct drm_device *dev,
14706 struct intel_display_error_state *error)
14707{
14708 struct drm_i915_private *dev_priv = dev->dev_private;
14709 int i;
14710
14711 if (!error)
14712 return;
14713
14714 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
14715 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
14716 err_printf(m, "PWR_WELL_CTL2: %08x\n",
14717 error->power_well_driver);
14718 for_each_pipe(dev_priv, i) {
14719 err_printf(m, "Pipe [%d]:\n", i);
14720 err_printf(m, " Power: %s\n",
14721 error->pipe[i].power_domain_on ? "on" : "off");
14722 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
14723 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
14724
14725 err_printf(m, "Plane [%d]:\n", i);
14726 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
14727 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
14728 if (INTEL_INFO(dev)->gen <= 3) {
14729 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
14730 err_printf(m, " POS: %08x\n", error->plane[i].pos);
14731 }
14732 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
14733 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
14734 if (INTEL_INFO(dev)->gen >= 4) {
14735 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
14736 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
14737 }
14738
14739 err_printf(m, "Cursor [%d]:\n", i);
14740 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
14741 err_printf(m, " POS: %08x\n", error->cursor[i].position);
14742 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
14743 }
14744
14745 for (i = 0; i < error->num_transcoders; i++) {
14746 err_printf(m, "CPU transcoder: %c\n",
14747 transcoder_name(error->transcoder[i].cpu_transcoder));
14748 err_printf(m, " Power: %s\n",
14749 error->transcoder[i].power_domain_on ? "on" : "off");
14750 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
14751 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
14752 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
14753 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
14754 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
14755 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
14756 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
14757 }
14758}
14759
14760void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
14761{
14762 struct intel_crtc *crtc;
14763
14764 for_each_intel_crtc(dev, crtc) {
14765 struct intel_unpin_work *work;
14766
14767 spin_lock_irq(&dev->event_lock);
14768
14769 work = crtc->unpin_work;
14770
14771 if (work && work->event &&
14772 work->event->base.file_priv == file) {
14773 kfree(work->event);
14774 work->event = NULL;
14775 }
14776
14777 spin_unlock_irq(&dev->event_lock);
14778 }
14779}
This page took 0.076873 seconds and 5 git commands to generate.