• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 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 DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eugeni Dodonov <eugeni.dodonov@intel.com>
25  *
26  */
27 
28 #include <linux/cpufreq.h>
29 #include "i915_drv.h"
30 #include "intel_drv.h"
31 #include "../../../platform/x86/intel_ips.h"
32 #include <linux/module.h>
33 #include <linux/vgaarb.h>
34 #include <drm/i915_powerwell.h>
35 #include <linux/pm_runtime.h>
36 
37 /**
38  * RC6 is a special power stage which allows the GPU to enter an very
39  * low-voltage mode when idle, using down to 0V while at this stage.  This
40  * stage is entered automatically when the GPU is idle when RC6 support is
41  * enabled, and as soon as new workload arises GPU wakes up automatically as well.
42  *
43  * There are different RC6 modes available in Intel GPU, which differentiate
44  * among each other with the latency required to enter and leave RC6 and
45  * voltage consumed by the GPU in different states.
46  *
47  * The combination of the following flags define which states GPU is allowed
48  * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
49  * RC6pp is deepest RC6. Their support by hardware varies according to the
50  * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
51  * which brings the most power savings; deeper states save more power, but
52  * require higher latency to switch to and wake up.
53  */
54 #define INTEL_RC6_ENABLE			(1<<0)
55 #define INTEL_RC6p_ENABLE			(1<<1)
56 #define INTEL_RC6pp_ENABLE			(1<<2)
57 
58 /* FBC, or Frame Buffer Compression, is a technique employed to compress the
59  * framebuffer contents in-memory, aiming at reducing the required bandwidth
60  * during in-memory transfers and, therefore, reduce the power packet.
61  *
62  * The benefits of FBC are mostly visible with solid backgrounds and
63  * variation-less patterns.
64  *
65  * FBC-related functionality can be enabled by the means of the
66  * i915.i915_enable_fbc parameter
67  */
68 
i8xx_disable_fbc(struct drm_device * dev)69 static void i8xx_disable_fbc(struct drm_device *dev)
70 {
71 	struct drm_i915_private *dev_priv = dev->dev_private;
72 	u32 fbc_ctl;
73 
74 	/* Disable compression */
75 	fbc_ctl = I915_READ(FBC_CONTROL);
76 	if ((fbc_ctl & FBC_CTL_EN) == 0)
77 		return;
78 
79 	fbc_ctl &= ~FBC_CTL_EN;
80 	I915_WRITE(FBC_CONTROL, fbc_ctl);
81 
82 	/* Wait for compressing bit to clear */
83 	if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
84 		DRM_DEBUG_KMS("FBC idle timed out\n");
85 		return;
86 	}
87 
88 	DRM_DEBUG_KMS("disabled FBC\n");
89 }
90 
i8xx_enable_fbc(struct drm_crtc * crtc)91 static void i8xx_enable_fbc(struct drm_crtc *crtc)
92 {
93 	struct drm_device *dev = crtc->dev;
94 	struct drm_i915_private *dev_priv = dev->dev_private;
95 	struct drm_framebuffer *fb = crtc->primary->fb;
96 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
97 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
98 	int cfb_pitch;
99 	int i;
100 	u32 fbc_ctl;
101 
102 	cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
103 	if (fb->pitches[0] < cfb_pitch)
104 		cfb_pitch = fb->pitches[0];
105 
106 	/* FBC_CTL wants 32B or 64B units */
107 	if (IS_GEN2(dev))
108 		cfb_pitch = (cfb_pitch / 32) - 1;
109 	else
110 		cfb_pitch = (cfb_pitch / 64) - 1;
111 
112 	/* Clear old tags */
113 	for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
114 		I915_WRITE(FBC_TAG + (i * 4), 0);
115 
116 	if (IS_GEN4(dev)) {
117 		u32 fbc_ctl2;
118 
119 		/* Set it up... */
120 		fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
121 		fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
122 		I915_WRITE(FBC_CONTROL2, fbc_ctl2);
123 		I915_WRITE(FBC_FENCE_OFF, crtc->y);
124 	}
125 
126 	/* enable it... */
127 	fbc_ctl = I915_READ(FBC_CONTROL);
128 	fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
129 	fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
130 	if (IS_I945GM(dev))
131 		fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
132 	fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
133 	fbc_ctl |= obj->fence_reg;
134 	I915_WRITE(FBC_CONTROL, fbc_ctl);
135 
136 	DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
137 		      cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
138 }
139 
i8xx_fbc_enabled(struct drm_device * dev)140 static bool i8xx_fbc_enabled(struct drm_device *dev)
141 {
142 	struct drm_i915_private *dev_priv = dev->dev_private;
143 
144 	return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
145 }
146 
g4x_enable_fbc(struct drm_crtc * crtc)147 static void g4x_enable_fbc(struct drm_crtc *crtc)
148 {
149 	struct drm_device *dev = crtc->dev;
150 	struct drm_i915_private *dev_priv = dev->dev_private;
151 	struct drm_framebuffer *fb = crtc->primary->fb;
152 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
153 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
154 	u32 dpfc_ctl;
155 
156 	dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
157 	if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
158 		dpfc_ctl |= DPFC_CTL_LIMIT_2X;
159 	else
160 		dpfc_ctl |= DPFC_CTL_LIMIT_1X;
161 	dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
162 
163 	I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
164 
165 	/* enable it... */
166 	I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
167 
168 	DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
169 }
170 
g4x_disable_fbc(struct drm_device * dev)171 static void g4x_disable_fbc(struct drm_device *dev)
172 {
173 	struct drm_i915_private *dev_priv = dev->dev_private;
174 	u32 dpfc_ctl;
175 
176 	/* Disable compression */
177 	dpfc_ctl = I915_READ(DPFC_CONTROL);
178 	if (dpfc_ctl & DPFC_CTL_EN) {
179 		dpfc_ctl &= ~DPFC_CTL_EN;
180 		I915_WRITE(DPFC_CONTROL, dpfc_ctl);
181 
182 		DRM_DEBUG_KMS("disabled FBC\n");
183 	}
184 }
185 
g4x_fbc_enabled(struct drm_device * dev)186 static bool g4x_fbc_enabled(struct drm_device *dev)
187 {
188 	struct drm_i915_private *dev_priv = dev->dev_private;
189 
190 	return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
191 }
192 
sandybridge_blit_fbc_update(struct drm_device * dev)193 static void sandybridge_blit_fbc_update(struct drm_device *dev)
194 {
195 	struct drm_i915_private *dev_priv = dev->dev_private;
196 	u32 blt_ecoskpd;
197 
198 	/* Make sure blitter notifies FBC of writes */
199 
200 	/* Blitter is part of Media powerwell on VLV. No impact of
201 	 * his param in other platforms for now */
202 	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
203 
204 	blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
205 	blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
206 		GEN6_BLITTER_LOCK_SHIFT;
207 	I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
208 	blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
209 	I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
210 	blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
211 			 GEN6_BLITTER_LOCK_SHIFT);
212 	I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
213 	POSTING_READ(GEN6_BLITTER_ECOSKPD);
214 
215 	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
216 }
217 
ironlake_enable_fbc(struct drm_crtc * crtc)218 static void ironlake_enable_fbc(struct drm_crtc *crtc)
219 {
220 	struct drm_device *dev = crtc->dev;
221 	struct drm_i915_private *dev_priv = dev->dev_private;
222 	struct drm_framebuffer *fb = crtc->primary->fb;
223 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
224 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
225 	u32 dpfc_ctl;
226 
227 	dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
228 	if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
229 		dev_priv->fbc.threshold++;
230 
231 	switch (dev_priv->fbc.threshold) {
232 	case 4:
233 	case 3:
234 		dpfc_ctl |= DPFC_CTL_LIMIT_4X;
235 		break;
236 	case 2:
237 		dpfc_ctl |= DPFC_CTL_LIMIT_2X;
238 		break;
239 	case 1:
240 		dpfc_ctl |= DPFC_CTL_LIMIT_1X;
241 		break;
242 	}
243 	dpfc_ctl |= DPFC_CTL_FENCE_EN;
244 	if (IS_GEN5(dev))
245 		dpfc_ctl |= obj->fence_reg;
246 
247 	I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
248 	I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
249 	/* enable it... */
250 	I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
251 
252 	if (IS_GEN6(dev)) {
253 		I915_WRITE(SNB_DPFC_CTL_SA,
254 			   SNB_CPU_FENCE_ENABLE | obj->fence_reg);
255 		I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
256 		sandybridge_blit_fbc_update(dev);
257 	}
258 
259 	DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
260 }
261 
ironlake_disable_fbc(struct drm_device * dev)262 static void ironlake_disable_fbc(struct drm_device *dev)
263 {
264 	struct drm_i915_private *dev_priv = dev->dev_private;
265 	u32 dpfc_ctl;
266 
267 	/* Disable compression */
268 	dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
269 	if (dpfc_ctl & DPFC_CTL_EN) {
270 		dpfc_ctl &= ~DPFC_CTL_EN;
271 		I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
272 
273 		DRM_DEBUG_KMS("disabled FBC\n");
274 	}
275 }
276 
ironlake_fbc_enabled(struct drm_device * dev)277 static bool ironlake_fbc_enabled(struct drm_device *dev)
278 {
279 	struct drm_i915_private *dev_priv = dev->dev_private;
280 
281 	return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
282 }
283 
gen7_enable_fbc(struct drm_crtc * crtc)284 static void gen7_enable_fbc(struct drm_crtc *crtc)
285 {
286 	struct drm_device *dev = crtc->dev;
287 	struct drm_i915_private *dev_priv = dev->dev_private;
288 	struct drm_framebuffer *fb = crtc->primary->fb;
289 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
290 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
291 	u32 dpfc_ctl;
292 
293 	dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
294 	if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
295 		dev_priv->fbc.threshold++;
296 
297 	switch (dev_priv->fbc.threshold) {
298 	case 4:
299 	case 3:
300 		dpfc_ctl |= DPFC_CTL_LIMIT_4X;
301 		break;
302 	case 2:
303 		dpfc_ctl |= DPFC_CTL_LIMIT_2X;
304 		break;
305 	case 1:
306 		dpfc_ctl |= DPFC_CTL_LIMIT_1X;
307 		break;
308 	}
309 
310 	dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
311 
312 	if (dev_priv->fbc.false_color)
313 		dpfc_ctl |= FBC_CTL_FALSE_COLOR;
314 
315 	I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
316 
317 	if (IS_IVYBRIDGE(dev)) {
318 		/* WaFbcAsynchFlipDisableFbcQueue:ivb */
319 		I915_WRITE(ILK_DISPLAY_CHICKEN1,
320 			   I915_READ(ILK_DISPLAY_CHICKEN1) |
321 			   ILK_FBCQ_DIS);
322 	} else {
323 		/* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
324 		I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
325 			   I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
326 			   HSW_FBCQ_DIS);
327 	}
328 
329 	I915_WRITE(SNB_DPFC_CTL_SA,
330 		   SNB_CPU_FENCE_ENABLE | obj->fence_reg);
331 	I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
332 
333 	sandybridge_blit_fbc_update(dev);
334 
335 	DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
336 }
337 
intel_fbc_enabled(struct drm_device * dev)338 bool intel_fbc_enabled(struct drm_device *dev)
339 {
340 	struct drm_i915_private *dev_priv = dev->dev_private;
341 
342 	if (!dev_priv->display.fbc_enabled)
343 		return false;
344 
345 	return dev_priv->display.fbc_enabled(dev);
346 }
347 
gen8_fbc_sw_flush(struct drm_device * dev,u32 value)348 void gen8_fbc_sw_flush(struct drm_device *dev, u32 value)
349 {
350 	struct drm_i915_private *dev_priv = dev->dev_private;
351 
352 	if (!IS_GEN8(dev))
353 		return;
354 
355 	I915_WRITE(MSG_FBC_REND_STATE, value);
356 }
357 
intel_fbc_work_fn(struct work_struct * __work)358 static void intel_fbc_work_fn(struct work_struct *__work)
359 {
360 	struct intel_fbc_work *work =
361 		container_of(to_delayed_work(__work),
362 			     struct intel_fbc_work, work);
363 	struct drm_device *dev = work->crtc->dev;
364 	struct drm_i915_private *dev_priv = dev->dev_private;
365 
366 	mutex_lock(&dev->struct_mutex);
367 	if (work == dev_priv->fbc.fbc_work) {
368 		/* Double check that we haven't switched fb without cancelling
369 		 * the prior work.
370 		 */
371 		if (work->crtc->primary->fb == work->fb) {
372 			dev_priv->display.enable_fbc(work->crtc);
373 
374 			dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
375 			dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
376 			dev_priv->fbc.y = work->crtc->y;
377 		}
378 
379 		dev_priv->fbc.fbc_work = NULL;
380 	}
381 	mutex_unlock(&dev->struct_mutex);
382 
383 	kfree(work);
384 }
385 
intel_cancel_fbc_work(struct drm_i915_private * dev_priv)386 static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
387 {
388 	if (dev_priv->fbc.fbc_work == NULL)
389 		return;
390 
391 	DRM_DEBUG_KMS("cancelling pending FBC enable\n");
392 
393 	/* Synchronisation is provided by struct_mutex and checking of
394 	 * dev_priv->fbc.fbc_work, so we can perform the cancellation
395 	 * entirely asynchronously.
396 	 */
397 	if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
398 		/* tasklet was killed before being run, clean up */
399 		kfree(dev_priv->fbc.fbc_work);
400 
401 	/* Mark the work as no longer wanted so that if it does
402 	 * wake-up (because the work was already running and waiting
403 	 * for our mutex), it will discover that is no longer
404 	 * necessary to run.
405 	 */
406 	dev_priv->fbc.fbc_work = NULL;
407 }
408 
intel_enable_fbc(struct drm_crtc * crtc)409 static void intel_enable_fbc(struct drm_crtc *crtc)
410 {
411 	struct intel_fbc_work *work;
412 	struct drm_device *dev = crtc->dev;
413 	struct drm_i915_private *dev_priv = dev->dev_private;
414 
415 	if (!dev_priv->display.enable_fbc)
416 		return;
417 
418 	intel_cancel_fbc_work(dev_priv);
419 
420 	work = kzalloc(sizeof(*work), GFP_KERNEL);
421 	if (work == NULL) {
422 		DRM_ERROR("Failed to allocate FBC work structure\n");
423 		dev_priv->display.enable_fbc(crtc);
424 		return;
425 	}
426 
427 	work->crtc = crtc;
428 	work->fb = crtc->primary->fb;
429 	INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
430 
431 	dev_priv->fbc.fbc_work = work;
432 
433 	/* Delay the actual enabling to let pageflipping cease and the
434 	 * display to settle before starting the compression. Note that
435 	 * this delay also serves a second purpose: it allows for a
436 	 * vblank to pass after disabling the FBC before we attempt
437 	 * to modify the control registers.
438 	 *
439 	 * A more complicated solution would involve tracking vblanks
440 	 * following the termination of the page-flipping sequence
441 	 * and indeed performing the enable as a co-routine and not
442 	 * waiting synchronously upon the vblank.
443 	 *
444 	 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
445 	 */
446 	schedule_delayed_work(&work->work, msecs_to_jiffies(50));
447 }
448 
intel_disable_fbc(struct drm_device * dev)449 void intel_disable_fbc(struct drm_device *dev)
450 {
451 	struct drm_i915_private *dev_priv = dev->dev_private;
452 
453 	intel_cancel_fbc_work(dev_priv);
454 
455 	if (!dev_priv->display.disable_fbc)
456 		return;
457 
458 	dev_priv->display.disable_fbc(dev);
459 	dev_priv->fbc.plane = -1;
460 }
461 
set_no_fbc_reason(struct drm_i915_private * dev_priv,enum no_fbc_reason reason)462 static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
463 			      enum no_fbc_reason reason)
464 {
465 	if (dev_priv->fbc.no_fbc_reason == reason)
466 		return false;
467 
468 	dev_priv->fbc.no_fbc_reason = reason;
469 	return true;
470 }
471 
472 /**
473  * intel_update_fbc - enable/disable FBC as needed
474  * @dev: the drm_device
475  *
476  * Set up the framebuffer compression hardware at mode set time.  We
477  * enable it if possible:
478  *   - plane A only (on pre-965)
479  *   - no pixel mulitply/line duplication
480  *   - no alpha buffer discard
481  *   - no dual wide
482  *   - framebuffer <= max_hdisplay in width, max_vdisplay in height
483  *
484  * We can't assume that any compression will take place (worst case),
485  * so the compressed buffer has to be the same size as the uncompressed
486  * one.  It also must reside (along with the line length buffer) in
487  * stolen memory.
488  *
489  * We need to enable/disable FBC on a global basis.
490  */
intel_update_fbc(struct drm_device * dev)491 void intel_update_fbc(struct drm_device *dev)
492 {
493 	struct drm_i915_private *dev_priv = dev->dev_private;
494 	struct drm_crtc *crtc = NULL, *tmp_crtc;
495 	struct intel_crtc *intel_crtc;
496 	struct drm_framebuffer *fb;
497 	struct drm_i915_gem_object *obj;
498 	const struct drm_display_mode *adjusted_mode;
499 	unsigned int max_width, max_height;
500 
501 	if (!HAS_FBC(dev)) {
502 		set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
503 		return;
504 	}
505 
506 	if (!i915.powersave) {
507 		if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
508 			DRM_DEBUG_KMS("fbc disabled per module param\n");
509 		return;
510 	}
511 
512 	/*
513 	 * If FBC is already on, we just have to verify that we can
514 	 * keep it that way...
515 	 * Need to disable if:
516 	 *   - more than one pipe is active
517 	 *   - changing FBC params (stride, fence, mode)
518 	 *   - new fb is too large to fit in compressed buffer
519 	 *   - going to an unsupported config (interlace, pixel multiply, etc.)
520 	 */
521 	for_each_crtc(dev, tmp_crtc) {
522 		if (intel_crtc_active(tmp_crtc) &&
523 		    to_intel_crtc(tmp_crtc)->primary_enabled) {
524 			if (crtc) {
525 				if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
526 					DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
527 				goto out_disable;
528 			}
529 			crtc = tmp_crtc;
530 		}
531 	}
532 
533 	if (!crtc || crtc->primary->fb == NULL) {
534 		if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
535 			DRM_DEBUG_KMS("no output, disabling\n");
536 		goto out_disable;
537 	}
538 
539 	intel_crtc = to_intel_crtc(crtc);
540 	fb = crtc->primary->fb;
541 	obj = intel_fb_obj(fb);
542 	adjusted_mode = &intel_crtc->config.adjusted_mode;
543 
544 	if (i915.enable_fbc < 0) {
545 		if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
546 			DRM_DEBUG_KMS("disabled per chip default\n");
547 		goto out_disable;
548 	}
549 	if (!i915.enable_fbc) {
550 		if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
551 			DRM_DEBUG_KMS("fbc disabled per module param\n");
552 		goto out_disable;
553 	}
554 	if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
555 	    (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
556 		if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
557 			DRM_DEBUG_KMS("mode incompatible with compression, "
558 				      "disabling\n");
559 		goto out_disable;
560 	}
561 
562 	if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
563 		max_width = 4096;
564 		max_height = 4096;
565 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
566 		max_width = 4096;
567 		max_height = 2048;
568 	} else {
569 		max_width = 2048;
570 		max_height = 1536;
571 	}
572 	if (intel_crtc->config.pipe_src_w > max_width ||
573 	    intel_crtc->config.pipe_src_h > max_height) {
574 		if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
575 			DRM_DEBUG_KMS("mode too large for compression, disabling\n");
576 		goto out_disable;
577 	}
578 	if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
579 	    intel_crtc->plane != PLANE_A) {
580 		if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
581 			DRM_DEBUG_KMS("plane not A, disabling compression\n");
582 		goto out_disable;
583 	}
584 
585 	/* The use of a CPU fence is mandatory in order to detect writes
586 	 * by the CPU to the scanout and trigger updates to the FBC.
587 	 */
588 	if (obj->tiling_mode != I915_TILING_X ||
589 	    obj->fence_reg == I915_FENCE_REG_NONE) {
590 		if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
591 			DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
592 		goto out_disable;
593 	}
594 	if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
595 	    to_intel_plane(crtc->primary)->rotation != BIT(DRM_ROTATE_0)) {
596 		if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
597 			DRM_DEBUG_KMS("Rotation unsupported, disabling\n");
598 		goto out_disable;
599 	}
600 
601 	/* If the kernel debugger is active, always disable compression */
602 	if (in_dbg_master())
603 		goto out_disable;
604 
605 	if (i915_gem_stolen_setup_compression(dev, obj->base.size,
606 					      drm_format_plane_cpp(fb->pixel_format, 0))) {
607 		if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
608 			DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
609 		goto out_disable;
610 	}
611 
612 	/* If the scanout has not changed, don't modify the FBC settings.
613 	 * Note that we make the fundamental assumption that the fb->obj
614 	 * cannot be unpinned (and have its GTT offset and fence revoked)
615 	 * without first being decoupled from the scanout and FBC disabled.
616 	 */
617 	if (dev_priv->fbc.plane == intel_crtc->plane &&
618 	    dev_priv->fbc.fb_id == fb->base.id &&
619 	    dev_priv->fbc.y == crtc->y)
620 		return;
621 
622 	if (intel_fbc_enabled(dev)) {
623 		/* We update FBC along two paths, after changing fb/crtc
624 		 * configuration (modeswitching) and after page-flipping
625 		 * finishes. For the latter, we know that not only did
626 		 * we disable the FBC at the start of the page-flip
627 		 * sequence, but also more than one vblank has passed.
628 		 *
629 		 * For the former case of modeswitching, it is possible
630 		 * to switch between two FBC valid configurations
631 		 * instantaneously so we do need to disable the FBC
632 		 * before we can modify its control registers. We also
633 		 * have to wait for the next vblank for that to take
634 		 * effect. However, since we delay enabling FBC we can
635 		 * assume that a vblank has passed since disabling and
636 		 * that we can safely alter the registers in the deferred
637 		 * callback.
638 		 *
639 		 * In the scenario that we go from a valid to invalid
640 		 * and then back to valid FBC configuration we have
641 		 * no strict enforcement that a vblank occurred since
642 		 * disabling the FBC. However, along all current pipe
643 		 * disabling paths we do need to wait for a vblank at
644 		 * some point. And we wait before enabling FBC anyway.
645 		 */
646 		DRM_DEBUG_KMS("disabling active FBC for update\n");
647 		intel_disable_fbc(dev);
648 	}
649 
650 	intel_enable_fbc(crtc);
651 	dev_priv->fbc.no_fbc_reason = FBC_OK;
652 	return;
653 
654 out_disable:
655 	/* Multiple disables should be harmless */
656 	if (intel_fbc_enabled(dev)) {
657 		DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
658 		intel_disable_fbc(dev);
659 	}
660 	i915_gem_stolen_cleanup_compression(dev);
661 }
662 
i915_pineview_get_mem_freq(struct drm_device * dev)663 static void i915_pineview_get_mem_freq(struct drm_device *dev)
664 {
665 	struct drm_i915_private *dev_priv = dev->dev_private;
666 	u32 tmp;
667 
668 	tmp = I915_READ(CLKCFG);
669 
670 	switch (tmp & CLKCFG_FSB_MASK) {
671 	case CLKCFG_FSB_533:
672 		dev_priv->fsb_freq = 533; /* 133*4 */
673 		break;
674 	case CLKCFG_FSB_800:
675 		dev_priv->fsb_freq = 800; /* 200*4 */
676 		break;
677 	case CLKCFG_FSB_667:
678 		dev_priv->fsb_freq =  667; /* 167*4 */
679 		break;
680 	case CLKCFG_FSB_400:
681 		dev_priv->fsb_freq = 400; /* 100*4 */
682 		break;
683 	}
684 
685 	switch (tmp & CLKCFG_MEM_MASK) {
686 	case CLKCFG_MEM_533:
687 		dev_priv->mem_freq = 533;
688 		break;
689 	case CLKCFG_MEM_667:
690 		dev_priv->mem_freq = 667;
691 		break;
692 	case CLKCFG_MEM_800:
693 		dev_priv->mem_freq = 800;
694 		break;
695 	}
696 
697 	/* detect pineview DDR3 setting */
698 	tmp = I915_READ(CSHRDDR3CTL);
699 	dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
700 }
701 
i915_ironlake_get_mem_freq(struct drm_device * dev)702 static void i915_ironlake_get_mem_freq(struct drm_device *dev)
703 {
704 	struct drm_i915_private *dev_priv = dev->dev_private;
705 	u16 ddrpll, csipll;
706 
707 	ddrpll = I915_READ16(DDRMPLL1);
708 	csipll = I915_READ16(CSIPLL0);
709 
710 	switch (ddrpll & 0xff) {
711 	case 0xc:
712 		dev_priv->mem_freq = 800;
713 		break;
714 	case 0x10:
715 		dev_priv->mem_freq = 1066;
716 		break;
717 	case 0x14:
718 		dev_priv->mem_freq = 1333;
719 		break;
720 	case 0x18:
721 		dev_priv->mem_freq = 1600;
722 		break;
723 	default:
724 		DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
725 				 ddrpll & 0xff);
726 		dev_priv->mem_freq = 0;
727 		break;
728 	}
729 
730 	dev_priv->ips.r_t = dev_priv->mem_freq;
731 
732 	switch (csipll & 0x3ff) {
733 	case 0x00c:
734 		dev_priv->fsb_freq = 3200;
735 		break;
736 	case 0x00e:
737 		dev_priv->fsb_freq = 3733;
738 		break;
739 	case 0x010:
740 		dev_priv->fsb_freq = 4266;
741 		break;
742 	case 0x012:
743 		dev_priv->fsb_freq = 4800;
744 		break;
745 	case 0x014:
746 		dev_priv->fsb_freq = 5333;
747 		break;
748 	case 0x016:
749 		dev_priv->fsb_freq = 5866;
750 		break;
751 	case 0x018:
752 		dev_priv->fsb_freq = 6400;
753 		break;
754 	default:
755 		DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
756 				 csipll & 0x3ff);
757 		dev_priv->fsb_freq = 0;
758 		break;
759 	}
760 
761 	if (dev_priv->fsb_freq == 3200) {
762 		dev_priv->ips.c_m = 0;
763 	} else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
764 		dev_priv->ips.c_m = 1;
765 	} else {
766 		dev_priv->ips.c_m = 2;
767 	}
768 }
769 
770 static const struct cxsr_latency cxsr_latency_table[] = {
771 	{1, 0, 800, 400, 3382, 33382, 3983, 33983},    /* DDR2-400 SC */
772 	{1, 0, 800, 667, 3354, 33354, 3807, 33807},    /* DDR2-667 SC */
773 	{1, 0, 800, 800, 3347, 33347, 3763, 33763},    /* DDR2-800 SC */
774 	{1, 1, 800, 667, 6420, 36420, 6873, 36873},    /* DDR3-667 SC */
775 	{1, 1, 800, 800, 5902, 35902, 6318, 36318},    /* DDR3-800 SC */
776 
777 	{1, 0, 667, 400, 3400, 33400, 4021, 34021},    /* DDR2-400 SC */
778 	{1, 0, 667, 667, 3372, 33372, 3845, 33845},    /* DDR2-667 SC */
779 	{1, 0, 667, 800, 3386, 33386, 3822, 33822},    /* DDR2-800 SC */
780 	{1, 1, 667, 667, 6438, 36438, 6911, 36911},    /* DDR3-667 SC */
781 	{1, 1, 667, 800, 5941, 35941, 6377, 36377},    /* DDR3-800 SC */
782 
783 	{1, 0, 400, 400, 3472, 33472, 4173, 34173},    /* DDR2-400 SC */
784 	{1, 0, 400, 667, 3443, 33443, 3996, 33996},    /* DDR2-667 SC */
785 	{1, 0, 400, 800, 3430, 33430, 3946, 33946},    /* DDR2-800 SC */
786 	{1, 1, 400, 667, 6509, 36509, 7062, 37062},    /* DDR3-667 SC */
787 	{1, 1, 400, 800, 5985, 35985, 6501, 36501},    /* DDR3-800 SC */
788 
789 	{0, 0, 800, 400, 3438, 33438, 4065, 34065},    /* DDR2-400 SC */
790 	{0, 0, 800, 667, 3410, 33410, 3889, 33889},    /* DDR2-667 SC */
791 	{0, 0, 800, 800, 3403, 33403, 3845, 33845},    /* DDR2-800 SC */
792 	{0, 1, 800, 667, 6476, 36476, 6955, 36955},    /* DDR3-667 SC */
793 	{0, 1, 800, 800, 5958, 35958, 6400, 36400},    /* DDR3-800 SC */
794 
795 	{0, 0, 667, 400, 3456, 33456, 4103, 34106},    /* DDR2-400 SC */
796 	{0, 0, 667, 667, 3428, 33428, 3927, 33927},    /* DDR2-667 SC */
797 	{0, 0, 667, 800, 3443, 33443, 3905, 33905},    /* DDR2-800 SC */
798 	{0, 1, 667, 667, 6494, 36494, 6993, 36993},    /* DDR3-667 SC */
799 	{0, 1, 667, 800, 5998, 35998, 6460, 36460},    /* DDR3-800 SC */
800 
801 	{0, 0, 400, 400, 3528, 33528, 4255, 34255},    /* DDR2-400 SC */
802 	{0, 0, 400, 667, 3500, 33500, 4079, 34079},    /* DDR2-667 SC */
803 	{0, 0, 400, 800, 3487, 33487, 4029, 34029},    /* DDR2-800 SC */
804 	{0, 1, 400, 667, 6566, 36566, 7145, 37145},    /* DDR3-667 SC */
805 	{0, 1, 400, 800, 6042, 36042, 6584, 36584},    /* DDR3-800 SC */
806 };
807 
intel_get_cxsr_latency(int is_desktop,int is_ddr3,int fsb,int mem)808 static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
809 							 int is_ddr3,
810 							 int fsb,
811 							 int mem)
812 {
813 	const struct cxsr_latency *latency;
814 	int i;
815 
816 	if (fsb == 0 || mem == 0)
817 		return NULL;
818 
819 	for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
820 		latency = &cxsr_latency_table[i];
821 		if (is_desktop == latency->is_desktop &&
822 		    is_ddr3 == latency->is_ddr3 &&
823 		    fsb == latency->fsb_freq && mem == latency->mem_freq)
824 			return latency;
825 	}
826 
827 	DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
828 
829 	return NULL;
830 }
831 
intel_set_memory_cxsr(struct drm_i915_private * dev_priv,bool enable)832 void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
833 {
834 	struct drm_device *dev = dev_priv->dev;
835 	u32 val;
836 
837 	if (IS_VALLEYVIEW(dev)) {
838 		I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
839 	} else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
840 		I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
841 	} else if (IS_PINEVIEW(dev)) {
842 		val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
843 		val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
844 		I915_WRITE(DSPFW3, val);
845 	} else if (IS_I945G(dev) || IS_I945GM(dev)) {
846 		val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
847 			       _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
848 		I915_WRITE(FW_BLC_SELF, val);
849 	} else if (IS_I915GM(dev)) {
850 		val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
851 			       _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
852 		I915_WRITE(INSTPM, val);
853 	} else {
854 		return;
855 	}
856 
857 	DRM_DEBUG_KMS("memory self-refresh is %s\n",
858 		      enable ? "enabled" : "disabled");
859 }
860 
861 /*
862  * Latency for FIFO fetches is dependent on several factors:
863  *   - memory configuration (speed, channels)
864  *   - chipset
865  *   - current MCH state
866  * It can be fairly high in some situations, so here we assume a fairly
867  * pessimal value.  It's a tradeoff between extra memory fetches (if we
868  * set this value too high, the FIFO will fetch frequently to stay full)
869  * and power consumption (set it too low to save power and we might see
870  * FIFO underruns and display "flicker").
871  *
872  * A value of 5us seems to be a good balance; safe for very low end
873  * platforms but not overly aggressive on lower latency configs.
874  */
875 static const int pessimal_latency_ns = 5000;
876 
i9xx_get_fifo_size(struct drm_device * dev,int plane)877 static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
878 {
879 	struct drm_i915_private *dev_priv = dev->dev_private;
880 	uint32_t dsparb = I915_READ(DSPARB);
881 	int size;
882 
883 	size = dsparb & 0x7f;
884 	if (plane)
885 		size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
886 
887 	DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
888 		      plane ? "B" : "A", size);
889 
890 	return size;
891 }
892 
i830_get_fifo_size(struct drm_device * dev,int plane)893 static int i830_get_fifo_size(struct drm_device *dev, int plane)
894 {
895 	struct drm_i915_private *dev_priv = dev->dev_private;
896 	uint32_t dsparb = I915_READ(DSPARB);
897 	int size;
898 
899 	size = dsparb & 0x1ff;
900 	if (plane)
901 		size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
902 	size >>= 1; /* Convert to cachelines */
903 
904 	DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
905 		      plane ? "B" : "A", size);
906 
907 	return size;
908 }
909 
i845_get_fifo_size(struct drm_device * dev,int plane)910 static int i845_get_fifo_size(struct drm_device *dev, int plane)
911 {
912 	struct drm_i915_private *dev_priv = dev->dev_private;
913 	uint32_t dsparb = I915_READ(DSPARB);
914 	int size;
915 
916 	size = dsparb & 0x7f;
917 	size >>= 2; /* Convert to cachelines */
918 
919 	DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
920 		      plane ? "B" : "A",
921 		      size);
922 
923 	return size;
924 }
925 
926 /* Pineview has different values for various configs */
927 static const struct intel_watermark_params pineview_display_wm = {
928 	.fifo_size = PINEVIEW_DISPLAY_FIFO,
929 	.max_wm = PINEVIEW_MAX_WM,
930 	.default_wm = PINEVIEW_DFT_WM,
931 	.guard_size = PINEVIEW_GUARD_WM,
932 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
933 };
934 static const struct intel_watermark_params pineview_display_hplloff_wm = {
935 	.fifo_size = PINEVIEW_DISPLAY_FIFO,
936 	.max_wm = PINEVIEW_MAX_WM,
937 	.default_wm = PINEVIEW_DFT_HPLLOFF_WM,
938 	.guard_size = PINEVIEW_GUARD_WM,
939 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
940 };
941 static const struct intel_watermark_params pineview_cursor_wm = {
942 	.fifo_size = PINEVIEW_CURSOR_FIFO,
943 	.max_wm = PINEVIEW_CURSOR_MAX_WM,
944 	.default_wm = PINEVIEW_CURSOR_DFT_WM,
945 	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
946 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
947 };
948 static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
949 	.fifo_size = PINEVIEW_CURSOR_FIFO,
950 	.max_wm = PINEVIEW_CURSOR_MAX_WM,
951 	.default_wm = PINEVIEW_CURSOR_DFT_WM,
952 	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
953 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
954 };
955 static const struct intel_watermark_params g4x_wm_info = {
956 	.fifo_size = G4X_FIFO_SIZE,
957 	.max_wm = G4X_MAX_WM,
958 	.default_wm = G4X_MAX_WM,
959 	.guard_size = 2,
960 	.cacheline_size = G4X_FIFO_LINE_SIZE,
961 };
962 static const struct intel_watermark_params g4x_cursor_wm_info = {
963 	.fifo_size = I965_CURSOR_FIFO,
964 	.max_wm = I965_CURSOR_MAX_WM,
965 	.default_wm = I965_CURSOR_DFT_WM,
966 	.guard_size = 2,
967 	.cacheline_size = G4X_FIFO_LINE_SIZE,
968 };
969 static const struct intel_watermark_params valleyview_wm_info = {
970 	.fifo_size = VALLEYVIEW_FIFO_SIZE,
971 	.max_wm = VALLEYVIEW_MAX_WM,
972 	.default_wm = VALLEYVIEW_MAX_WM,
973 	.guard_size = 2,
974 	.cacheline_size = G4X_FIFO_LINE_SIZE,
975 };
976 static const struct intel_watermark_params valleyview_cursor_wm_info = {
977 	.fifo_size = I965_CURSOR_FIFO,
978 	.max_wm = VALLEYVIEW_CURSOR_MAX_WM,
979 	.default_wm = I965_CURSOR_DFT_WM,
980 	.guard_size = 2,
981 	.cacheline_size = G4X_FIFO_LINE_SIZE,
982 };
983 static const struct intel_watermark_params i965_cursor_wm_info = {
984 	.fifo_size = I965_CURSOR_FIFO,
985 	.max_wm = I965_CURSOR_MAX_WM,
986 	.default_wm = I965_CURSOR_DFT_WM,
987 	.guard_size = 2,
988 	.cacheline_size = I915_FIFO_LINE_SIZE,
989 };
990 static const struct intel_watermark_params i945_wm_info = {
991 	.fifo_size = I945_FIFO_SIZE,
992 	.max_wm = I915_MAX_WM,
993 	.default_wm = 1,
994 	.guard_size = 2,
995 	.cacheline_size = I915_FIFO_LINE_SIZE,
996 };
997 static const struct intel_watermark_params i915_wm_info = {
998 	.fifo_size = I915_FIFO_SIZE,
999 	.max_wm = I915_MAX_WM,
1000 	.default_wm = 1,
1001 	.guard_size = 2,
1002 	.cacheline_size = I915_FIFO_LINE_SIZE,
1003 };
1004 static const struct intel_watermark_params i830_a_wm_info = {
1005 	.fifo_size = I855GM_FIFO_SIZE,
1006 	.max_wm = I915_MAX_WM,
1007 	.default_wm = 1,
1008 	.guard_size = 2,
1009 	.cacheline_size = I830_FIFO_LINE_SIZE,
1010 };
1011 static const struct intel_watermark_params i830_bc_wm_info = {
1012 	.fifo_size = I855GM_FIFO_SIZE,
1013 	.max_wm = I915_MAX_WM/2,
1014 	.default_wm = 1,
1015 	.guard_size = 2,
1016 	.cacheline_size = I830_FIFO_LINE_SIZE,
1017 };
1018 static const struct intel_watermark_params i845_wm_info = {
1019 	.fifo_size = I830_FIFO_SIZE,
1020 	.max_wm = I915_MAX_WM,
1021 	.default_wm = 1,
1022 	.guard_size = 2,
1023 	.cacheline_size = I830_FIFO_LINE_SIZE,
1024 };
1025 
1026 /**
1027  * intel_calculate_wm - calculate watermark level
1028  * @clock_in_khz: pixel clock
1029  * @wm: chip FIFO params
1030  * @pixel_size: display pixel size
1031  * @latency_ns: memory latency for the platform
1032  *
1033  * Calculate the watermark level (the level at which the display plane will
1034  * start fetching from memory again).  Each chip has a different display
1035  * FIFO size and allocation, so the caller needs to figure that out and pass
1036  * in the correct intel_watermark_params structure.
1037  *
1038  * As the pixel clock runs, the FIFO will be drained at a rate that depends
1039  * on the pixel size.  When it reaches the watermark level, it'll start
1040  * fetching FIFO line sized based chunks from memory until the FIFO fills
1041  * past the watermark point.  If the FIFO drains completely, a FIFO underrun
1042  * will occur, and a display engine hang could result.
1043  */
intel_calculate_wm(unsigned long clock_in_khz,const struct intel_watermark_params * wm,int fifo_size,int pixel_size,unsigned long latency_ns)1044 static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1045 					const struct intel_watermark_params *wm,
1046 					int fifo_size,
1047 					int pixel_size,
1048 					unsigned long latency_ns)
1049 {
1050 	long entries_required, wm_size;
1051 
1052 	/*
1053 	 * Note: we need to make sure we don't overflow for various clock &
1054 	 * latency values.
1055 	 * clocks go from a few thousand to several hundred thousand.
1056 	 * latency is usually a few thousand
1057 	 */
1058 	entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1059 		1000;
1060 	entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1061 
1062 	DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1063 
1064 	wm_size = fifo_size - (entries_required + wm->guard_size);
1065 
1066 	DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1067 
1068 	/* Don't promote wm_size to unsigned... */
1069 	if (wm_size > (long)wm->max_wm)
1070 		wm_size = wm->max_wm;
1071 	if (wm_size <= 0)
1072 		wm_size = wm->default_wm;
1073 
1074 	/*
1075 	 * Bspec seems to indicate that the value shouldn't be lower than
1076 	 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
1077 	 * Lets go for 8 which is the burst size since certain platforms
1078 	 * already use a hardcoded 8 (which is what the spec says should be
1079 	 * done).
1080 	 */
1081 	if (wm_size <= 8)
1082 		wm_size = 8;
1083 
1084 	return wm_size;
1085 }
1086 
single_enabled_crtc(struct drm_device * dev)1087 static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1088 {
1089 	struct drm_crtc *crtc, *enabled = NULL;
1090 
1091 	for_each_crtc(dev, crtc) {
1092 		if (intel_crtc_active(crtc)) {
1093 			if (enabled)
1094 				return NULL;
1095 			enabled = crtc;
1096 		}
1097 	}
1098 
1099 	return enabled;
1100 }
1101 
pineview_update_wm(struct drm_crtc * unused_crtc)1102 static void pineview_update_wm(struct drm_crtc *unused_crtc)
1103 {
1104 	struct drm_device *dev = unused_crtc->dev;
1105 	struct drm_i915_private *dev_priv = dev->dev_private;
1106 	struct drm_crtc *crtc;
1107 	const struct cxsr_latency *latency;
1108 	u32 reg;
1109 	unsigned long wm;
1110 
1111 	latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1112 					 dev_priv->fsb_freq, dev_priv->mem_freq);
1113 	if (!latency) {
1114 		DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
1115 		intel_set_memory_cxsr(dev_priv, false);
1116 		return;
1117 	}
1118 
1119 	crtc = single_enabled_crtc(dev);
1120 	if (crtc) {
1121 		const struct drm_display_mode *adjusted_mode;
1122 		int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
1123 		int clock;
1124 
1125 		adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1126 		clock = adjusted_mode->crtc_clock;
1127 
1128 		/* Display SR */
1129 		wm = intel_calculate_wm(clock, &pineview_display_wm,
1130 					pineview_display_wm.fifo_size,
1131 					pixel_size, latency->display_sr);
1132 		reg = I915_READ(DSPFW1);
1133 		reg &= ~DSPFW_SR_MASK;
1134 		reg |= wm << DSPFW_SR_SHIFT;
1135 		I915_WRITE(DSPFW1, reg);
1136 		DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1137 
1138 		/* cursor SR */
1139 		wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1140 					pineview_display_wm.fifo_size,
1141 					pixel_size, latency->cursor_sr);
1142 		reg = I915_READ(DSPFW3);
1143 		reg &= ~DSPFW_CURSOR_SR_MASK;
1144 		reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
1145 		I915_WRITE(DSPFW3, reg);
1146 
1147 		/* Display HPLL off SR */
1148 		wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1149 					pineview_display_hplloff_wm.fifo_size,
1150 					pixel_size, latency->display_hpll_disable);
1151 		reg = I915_READ(DSPFW3);
1152 		reg &= ~DSPFW_HPLL_SR_MASK;
1153 		reg |= wm & DSPFW_HPLL_SR_MASK;
1154 		I915_WRITE(DSPFW3, reg);
1155 
1156 		/* cursor HPLL off SR */
1157 		wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1158 					pineview_display_hplloff_wm.fifo_size,
1159 					pixel_size, latency->cursor_hpll_disable);
1160 		reg = I915_READ(DSPFW3);
1161 		reg &= ~DSPFW_HPLL_CURSOR_MASK;
1162 		reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
1163 		I915_WRITE(DSPFW3, reg);
1164 		DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1165 
1166 		intel_set_memory_cxsr(dev_priv, true);
1167 	} else {
1168 		intel_set_memory_cxsr(dev_priv, false);
1169 	}
1170 }
1171 
g4x_compute_wm0(struct drm_device * dev,int plane,const struct intel_watermark_params * display,int display_latency_ns,const struct intel_watermark_params * cursor,int cursor_latency_ns,int * plane_wm,int * cursor_wm)1172 static bool g4x_compute_wm0(struct drm_device *dev,
1173 			    int plane,
1174 			    const struct intel_watermark_params *display,
1175 			    int display_latency_ns,
1176 			    const struct intel_watermark_params *cursor,
1177 			    int cursor_latency_ns,
1178 			    int *plane_wm,
1179 			    int *cursor_wm)
1180 {
1181 	struct drm_crtc *crtc;
1182 	const struct drm_display_mode *adjusted_mode;
1183 	int htotal, hdisplay, clock, pixel_size;
1184 	int line_time_us, line_count;
1185 	int entries, tlb_miss;
1186 
1187 	crtc = intel_get_crtc_for_plane(dev, plane);
1188 	if (!intel_crtc_active(crtc)) {
1189 		*cursor_wm = cursor->guard_size;
1190 		*plane_wm = display->guard_size;
1191 		return false;
1192 	}
1193 
1194 	adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1195 	clock = adjusted_mode->crtc_clock;
1196 	htotal = adjusted_mode->crtc_htotal;
1197 	hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
1198 	pixel_size = crtc->primary->fb->bits_per_pixel / 8;
1199 
1200 	/* Use the small buffer method to calculate plane watermark */
1201 	entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1202 	tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1203 	if (tlb_miss > 0)
1204 		entries += tlb_miss;
1205 	entries = DIV_ROUND_UP(entries, display->cacheline_size);
1206 	*plane_wm = entries + display->guard_size;
1207 	if (*plane_wm > (int)display->max_wm)
1208 		*plane_wm = display->max_wm;
1209 
1210 	/* Use the large buffer method to calculate cursor watermark */
1211 	line_time_us = max(htotal * 1000 / clock, 1);
1212 	line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1213 	entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
1214 	tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1215 	if (tlb_miss > 0)
1216 		entries += tlb_miss;
1217 	entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1218 	*cursor_wm = entries + cursor->guard_size;
1219 	if (*cursor_wm > (int)cursor->max_wm)
1220 		*cursor_wm = (int)cursor->max_wm;
1221 
1222 	return true;
1223 }
1224 
1225 /*
1226  * Check the wm result.
1227  *
1228  * If any calculated watermark values is larger than the maximum value that
1229  * can be programmed into the associated watermark register, that watermark
1230  * must be disabled.
1231  */
g4x_check_srwm(struct drm_device * dev,int display_wm,int cursor_wm,const struct intel_watermark_params * display,const struct intel_watermark_params * cursor)1232 static bool g4x_check_srwm(struct drm_device *dev,
1233 			   int display_wm, int cursor_wm,
1234 			   const struct intel_watermark_params *display,
1235 			   const struct intel_watermark_params *cursor)
1236 {
1237 	DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1238 		      display_wm, cursor_wm);
1239 
1240 	if (display_wm > display->max_wm) {
1241 		DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1242 			      display_wm, display->max_wm);
1243 		return false;
1244 	}
1245 
1246 	if (cursor_wm > cursor->max_wm) {
1247 		DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1248 			      cursor_wm, cursor->max_wm);
1249 		return false;
1250 	}
1251 
1252 	if (!(display_wm || cursor_wm)) {
1253 		DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1254 		return false;
1255 	}
1256 
1257 	return true;
1258 }
1259 
g4x_compute_srwm(struct drm_device * dev,int plane,int latency_ns,const struct intel_watermark_params * display,const struct intel_watermark_params * cursor,int * display_wm,int * cursor_wm)1260 static bool g4x_compute_srwm(struct drm_device *dev,
1261 			     int plane,
1262 			     int latency_ns,
1263 			     const struct intel_watermark_params *display,
1264 			     const struct intel_watermark_params *cursor,
1265 			     int *display_wm, int *cursor_wm)
1266 {
1267 	struct drm_crtc *crtc;
1268 	const struct drm_display_mode *adjusted_mode;
1269 	int hdisplay, htotal, pixel_size, clock;
1270 	unsigned long line_time_us;
1271 	int line_count, line_size;
1272 	int small, large;
1273 	int entries;
1274 
1275 	if (!latency_ns) {
1276 		*display_wm = *cursor_wm = 0;
1277 		return false;
1278 	}
1279 
1280 	crtc = intel_get_crtc_for_plane(dev, plane);
1281 	adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1282 	clock = adjusted_mode->crtc_clock;
1283 	htotal = adjusted_mode->crtc_htotal;
1284 	hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
1285 	pixel_size = crtc->primary->fb->bits_per_pixel / 8;
1286 
1287 	line_time_us = max(htotal * 1000 / clock, 1);
1288 	line_count = (latency_ns / line_time_us + 1000) / 1000;
1289 	line_size = hdisplay * pixel_size;
1290 
1291 	/* Use the minimum of the small and large buffer method for primary */
1292 	small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1293 	large = line_count * line_size;
1294 
1295 	entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1296 	*display_wm = entries + display->guard_size;
1297 
1298 	/* calculate the self-refresh watermark for display cursor */
1299 	entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
1300 	entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1301 	*cursor_wm = entries + cursor->guard_size;
1302 
1303 	return g4x_check_srwm(dev,
1304 			      *display_wm, *cursor_wm,
1305 			      display, cursor);
1306 }
1307 
vlv_compute_drain_latency(struct drm_crtc * crtc,int pixel_size,int * prec_mult,int * drain_latency)1308 static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
1309 				      int pixel_size,
1310 				      int *prec_mult,
1311 				      int *drain_latency)
1312 {
1313 	int entries;
1314 	int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
1315 
1316 	if (WARN(clock == 0, "Pixel clock is zero!\n"))
1317 		return false;
1318 
1319 	if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
1320 		return false;
1321 
1322 	entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
1323 	*prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
1324 				       DRAIN_LATENCY_PRECISION_32;
1325 	*drain_latency = (64 * (*prec_mult) * 4) / entries;
1326 
1327 	if (*drain_latency > DRAIN_LATENCY_MASK)
1328 		*drain_latency = DRAIN_LATENCY_MASK;
1329 
1330 	return true;
1331 }
1332 
1333 /*
1334  * Update drain latency registers of memory arbiter
1335  *
1336  * Valleyview SoC has a new memory arbiter and needs drain latency registers
1337  * to be programmed. Each plane has a drain latency multiplier and a drain
1338  * latency value.
1339  */
1340 
vlv_update_drain_latency(struct drm_crtc * crtc)1341 static void vlv_update_drain_latency(struct drm_crtc *crtc)
1342 {
1343 	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1344 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1345 	int pixel_size;
1346 	int drain_latency;
1347 	enum pipe pipe = intel_crtc->pipe;
1348 	int plane_prec, prec_mult, plane_dl;
1349 
1350 	plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_64 |
1351 		   DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_64 |
1352 		   (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
1353 
1354 	if (!intel_crtc_active(crtc)) {
1355 		I915_WRITE(VLV_DDL(pipe), plane_dl);
1356 		return;
1357 	}
1358 
1359 	/* Primary plane Drain Latency */
1360 	pixel_size = crtc->primary->fb->bits_per_pixel / 8;	/* BPP */
1361 	if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1362 		plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1363 					   DDL_PLANE_PRECISION_64 :
1364 					   DDL_PLANE_PRECISION_32;
1365 		plane_dl |= plane_prec | drain_latency;
1366 	}
1367 
1368 	/* Cursor Drain Latency
1369 	 * BPP is always 4 for cursor
1370 	 */
1371 	pixel_size = 4;
1372 
1373 	/* Program cursor DL only if it is enabled */
1374 	if (intel_crtc->cursor_base &&
1375 	    vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1376 		plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1377 					   DDL_CURSOR_PRECISION_64 :
1378 					   DDL_CURSOR_PRECISION_32;
1379 		plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
1380 	}
1381 
1382 	I915_WRITE(VLV_DDL(pipe), plane_dl);
1383 }
1384 
1385 #define single_plane_enabled(mask) is_power_of_2(mask)
1386 
valleyview_update_wm(struct drm_crtc * crtc)1387 static void valleyview_update_wm(struct drm_crtc *crtc)
1388 {
1389 	struct drm_device *dev = crtc->dev;
1390 	static const int sr_latency_ns = 12000;
1391 	struct drm_i915_private *dev_priv = dev->dev_private;
1392 	int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1393 	int plane_sr, cursor_sr;
1394 	int ignore_plane_sr, ignore_cursor_sr;
1395 	unsigned int enabled = 0;
1396 	bool cxsr_enabled;
1397 
1398 	vlv_update_drain_latency(crtc);
1399 
1400 	if (g4x_compute_wm0(dev, PIPE_A,
1401 			    &valleyview_wm_info, pessimal_latency_ns,
1402 			    &valleyview_cursor_wm_info, pessimal_latency_ns,
1403 			    &planea_wm, &cursora_wm))
1404 		enabled |= 1 << PIPE_A;
1405 
1406 	if (g4x_compute_wm0(dev, PIPE_B,
1407 			    &valleyview_wm_info, pessimal_latency_ns,
1408 			    &valleyview_cursor_wm_info, pessimal_latency_ns,
1409 			    &planeb_wm, &cursorb_wm))
1410 		enabled |= 1 << PIPE_B;
1411 
1412 	if (single_plane_enabled(enabled) &&
1413 	    g4x_compute_srwm(dev, ffs(enabled) - 1,
1414 			     sr_latency_ns,
1415 			     &valleyview_wm_info,
1416 			     &valleyview_cursor_wm_info,
1417 			     &plane_sr, &ignore_cursor_sr) &&
1418 	    g4x_compute_srwm(dev, ffs(enabled) - 1,
1419 			     2*sr_latency_ns,
1420 			     &valleyview_wm_info,
1421 			     &valleyview_cursor_wm_info,
1422 			     &ignore_plane_sr, &cursor_sr)) {
1423 		cxsr_enabled = true;
1424 	} else {
1425 		cxsr_enabled = false;
1426 		intel_set_memory_cxsr(dev_priv, false);
1427 		plane_sr = cursor_sr = 0;
1428 	}
1429 
1430 	DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1431 		      "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1432 		      planea_wm, cursora_wm,
1433 		      planeb_wm, cursorb_wm,
1434 		      plane_sr, cursor_sr);
1435 
1436 	I915_WRITE(DSPFW1,
1437 		   (plane_sr << DSPFW_SR_SHIFT) |
1438 		   (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1439 		   (planeb_wm << DSPFW_PLANEB_SHIFT) |
1440 		   (planea_wm << DSPFW_PLANEA_SHIFT));
1441 	I915_WRITE(DSPFW2,
1442 		   (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1443 		   (cursora_wm << DSPFW_CURSORA_SHIFT));
1444 	I915_WRITE(DSPFW3,
1445 		   (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1446 		   (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1447 
1448 	if (cxsr_enabled)
1449 		intel_set_memory_cxsr(dev_priv, true);
1450 }
1451 
cherryview_update_wm(struct drm_crtc * crtc)1452 static void cherryview_update_wm(struct drm_crtc *crtc)
1453 {
1454 	struct drm_device *dev = crtc->dev;
1455 	static const int sr_latency_ns = 12000;
1456 	struct drm_i915_private *dev_priv = dev->dev_private;
1457 	int planea_wm, planeb_wm, planec_wm;
1458 	int cursora_wm, cursorb_wm, cursorc_wm;
1459 	int plane_sr, cursor_sr;
1460 	int ignore_plane_sr, ignore_cursor_sr;
1461 	unsigned int enabled = 0;
1462 	bool cxsr_enabled;
1463 
1464 	vlv_update_drain_latency(crtc);
1465 
1466 	if (g4x_compute_wm0(dev, PIPE_A,
1467 			    &valleyview_wm_info, pessimal_latency_ns,
1468 			    &valleyview_cursor_wm_info, pessimal_latency_ns,
1469 			    &planea_wm, &cursora_wm))
1470 		enabled |= 1 << PIPE_A;
1471 
1472 	if (g4x_compute_wm0(dev, PIPE_B,
1473 			    &valleyview_wm_info, pessimal_latency_ns,
1474 			    &valleyview_cursor_wm_info, pessimal_latency_ns,
1475 			    &planeb_wm, &cursorb_wm))
1476 		enabled |= 1 << PIPE_B;
1477 
1478 	if (g4x_compute_wm0(dev, PIPE_C,
1479 			    &valleyview_wm_info, pessimal_latency_ns,
1480 			    &valleyview_cursor_wm_info, pessimal_latency_ns,
1481 			    &planec_wm, &cursorc_wm))
1482 		enabled |= 1 << PIPE_C;
1483 
1484 	if (single_plane_enabled(enabled) &&
1485 	    g4x_compute_srwm(dev, ffs(enabled) - 1,
1486 			     sr_latency_ns,
1487 			     &valleyview_wm_info,
1488 			     &valleyview_cursor_wm_info,
1489 			     &plane_sr, &ignore_cursor_sr) &&
1490 	    g4x_compute_srwm(dev, ffs(enabled) - 1,
1491 			     2*sr_latency_ns,
1492 			     &valleyview_wm_info,
1493 			     &valleyview_cursor_wm_info,
1494 			     &ignore_plane_sr, &cursor_sr)) {
1495 		cxsr_enabled = true;
1496 	} else {
1497 		cxsr_enabled = false;
1498 		intel_set_memory_cxsr(dev_priv, false);
1499 		plane_sr = cursor_sr = 0;
1500 	}
1501 
1502 	DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1503 		      "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
1504 		      "SR: plane=%d, cursor=%d\n",
1505 		      planea_wm, cursora_wm,
1506 		      planeb_wm, cursorb_wm,
1507 		      planec_wm, cursorc_wm,
1508 		      plane_sr, cursor_sr);
1509 
1510 	I915_WRITE(DSPFW1,
1511 		   (plane_sr << DSPFW_SR_SHIFT) |
1512 		   (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1513 		   (planeb_wm << DSPFW_PLANEB_SHIFT) |
1514 		   (planea_wm << DSPFW_PLANEA_SHIFT));
1515 	I915_WRITE(DSPFW2,
1516 		   (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1517 		   (cursora_wm << DSPFW_CURSORA_SHIFT));
1518 	I915_WRITE(DSPFW3,
1519 		   (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1520 		   (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1521 	I915_WRITE(DSPFW9_CHV,
1522 		   (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
1523 					      DSPFW_CURSORC_MASK)) |
1524 		   (planec_wm << DSPFW_PLANEC_SHIFT) |
1525 		   (cursorc_wm << DSPFW_CURSORC_SHIFT));
1526 
1527 	if (cxsr_enabled)
1528 		intel_set_memory_cxsr(dev_priv, true);
1529 }
1530 
valleyview_update_sprite_wm(struct drm_plane * plane,struct drm_crtc * crtc,uint32_t sprite_width,uint32_t sprite_height,int pixel_size,bool enabled,bool scaled)1531 static void valleyview_update_sprite_wm(struct drm_plane *plane,
1532 					struct drm_crtc *crtc,
1533 					uint32_t sprite_width,
1534 					uint32_t sprite_height,
1535 					int pixel_size,
1536 					bool enabled, bool scaled)
1537 {
1538 	struct drm_device *dev = crtc->dev;
1539 	struct drm_i915_private *dev_priv = dev->dev_private;
1540 	int pipe = to_intel_plane(plane)->pipe;
1541 	int sprite = to_intel_plane(plane)->plane;
1542 	int drain_latency;
1543 	int plane_prec;
1544 	int sprite_dl;
1545 	int prec_mult;
1546 
1547 	sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_64(sprite) |
1548 		    (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
1549 
1550 	if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
1551 						 &drain_latency)) {
1552 		plane_prec = (prec_mult == DRAIN_LATENCY_PRECISION_64) ?
1553 					   DDL_SPRITE_PRECISION_64(sprite) :
1554 					   DDL_SPRITE_PRECISION_32(sprite);
1555 		sprite_dl |= plane_prec |
1556 			     (drain_latency << DDL_SPRITE_SHIFT(sprite));
1557 	}
1558 
1559 	I915_WRITE(VLV_DDL(pipe), sprite_dl);
1560 }
1561 
g4x_update_wm(struct drm_crtc * crtc)1562 static void g4x_update_wm(struct drm_crtc *crtc)
1563 {
1564 	struct drm_device *dev = crtc->dev;
1565 	static const int sr_latency_ns = 12000;
1566 	struct drm_i915_private *dev_priv = dev->dev_private;
1567 	int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1568 	int plane_sr, cursor_sr;
1569 	unsigned int enabled = 0;
1570 	bool cxsr_enabled;
1571 
1572 	if (g4x_compute_wm0(dev, PIPE_A,
1573 			    &g4x_wm_info, pessimal_latency_ns,
1574 			    &g4x_cursor_wm_info, pessimal_latency_ns,
1575 			    &planea_wm, &cursora_wm))
1576 		enabled |= 1 << PIPE_A;
1577 
1578 	if (g4x_compute_wm0(dev, PIPE_B,
1579 			    &g4x_wm_info, pessimal_latency_ns,
1580 			    &g4x_cursor_wm_info, pessimal_latency_ns,
1581 			    &planeb_wm, &cursorb_wm))
1582 		enabled |= 1 << PIPE_B;
1583 
1584 	if (single_plane_enabled(enabled) &&
1585 	    g4x_compute_srwm(dev, ffs(enabled) - 1,
1586 			     sr_latency_ns,
1587 			     &g4x_wm_info,
1588 			     &g4x_cursor_wm_info,
1589 			     &plane_sr, &cursor_sr)) {
1590 		cxsr_enabled = true;
1591 	} else {
1592 		cxsr_enabled = false;
1593 		intel_set_memory_cxsr(dev_priv, false);
1594 		plane_sr = cursor_sr = 0;
1595 	}
1596 
1597 	DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1598 		      "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1599 		      planea_wm, cursora_wm,
1600 		      planeb_wm, cursorb_wm,
1601 		      plane_sr, cursor_sr);
1602 
1603 	I915_WRITE(DSPFW1,
1604 		   (plane_sr << DSPFW_SR_SHIFT) |
1605 		   (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1606 		   (planeb_wm << DSPFW_PLANEB_SHIFT) |
1607 		   (planea_wm << DSPFW_PLANEA_SHIFT));
1608 	I915_WRITE(DSPFW2,
1609 		   (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1610 		   (cursora_wm << DSPFW_CURSORA_SHIFT));
1611 	/* HPLL off in SR has some issues on G4x... disable it */
1612 	I915_WRITE(DSPFW3,
1613 		   (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
1614 		   (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1615 
1616 	if (cxsr_enabled)
1617 		intel_set_memory_cxsr(dev_priv, true);
1618 }
1619 
i965_update_wm(struct drm_crtc * unused_crtc)1620 static void i965_update_wm(struct drm_crtc *unused_crtc)
1621 {
1622 	struct drm_device *dev = unused_crtc->dev;
1623 	struct drm_i915_private *dev_priv = dev->dev_private;
1624 	struct drm_crtc *crtc;
1625 	int srwm = 1;
1626 	int cursor_sr = 16;
1627 	bool cxsr_enabled;
1628 
1629 	/* Calc sr entries for one plane configs */
1630 	crtc = single_enabled_crtc(dev);
1631 	if (crtc) {
1632 		/* self-refresh has much higher latency */
1633 		static const int sr_latency_ns = 12000;
1634 		const struct drm_display_mode *adjusted_mode =
1635 			&to_intel_crtc(crtc)->config.adjusted_mode;
1636 		int clock = adjusted_mode->crtc_clock;
1637 		int htotal = adjusted_mode->crtc_htotal;
1638 		int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
1639 		int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
1640 		unsigned long line_time_us;
1641 		int entries;
1642 
1643 		line_time_us = max(htotal * 1000 / clock, 1);
1644 
1645 		/* Use ns/us then divide to preserve precision */
1646 		entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1647 			pixel_size * hdisplay;
1648 		entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1649 		srwm = I965_FIFO_SIZE - entries;
1650 		if (srwm < 0)
1651 			srwm = 1;
1652 		srwm &= 0x1ff;
1653 		DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1654 			      entries, srwm);
1655 
1656 		entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1657 			pixel_size * to_intel_crtc(crtc)->cursor_width;
1658 		entries = DIV_ROUND_UP(entries,
1659 					  i965_cursor_wm_info.cacheline_size);
1660 		cursor_sr = i965_cursor_wm_info.fifo_size -
1661 			(entries + i965_cursor_wm_info.guard_size);
1662 
1663 		if (cursor_sr > i965_cursor_wm_info.max_wm)
1664 			cursor_sr = i965_cursor_wm_info.max_wm;
1665 
1666 		DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1667 			      "cursor %d\n", srwm, cursor_sr);
1668 
1669 		cxsr_enabled = true;
1670 	} else {
1671 		cxsr_enabled = false;
1672 		/* Turn off self refresh if both pipes are enabled */
1673 		intel_set_memory_cxsr(dev_priv, false);
1674 	}
1675 
1676 	DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1677 		      srwm);
1678 
1679 	/* 965 has limitations... */
1680 	I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1681 		   (8 << DSPFW_CURSORB_SHIFT) |
1682 		   (8 << DSPFW_PLANEB_SHIFT) |
1683 		   (8 << DSPFW_PLANEA_SHIFT));
1684 	I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1685 		   (8 << DSPFW_PLANEC_SHIFT_OLD));
1686 	/* update cursor SR watermark */
1687 	I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1688 
1689 	if (cxsr_enabled)
1690 		intel_set_memory_cxsr(dev_priv, true);
1691 }
1692 
i9xx_update_wm(struct drm_crtc * unused_crtc)1693 static void i9xx_update_wm(struct drm_crtc *unused_crtc)
1694 {
1695 	struct drm_device *dev = unused_crtc->dev;
1696 	struct drm_i915_private *dev_priv = dev->dev_private;
1697 	const struct intel_watermark_params *wm_info;
1698 	uint32_t fwater_lo;
1699 	uint32_t fwater_hi;
1700 	int cwm, srwm = 1;
1701 	int fifo_size;
1702 	int planea_wm, planeb_wm;
1703 	struct drm_crtc *crtc, *enabled = NULL;
1704 
1705 	if (IS_I945GM(dev))
1706 		wm_info = &i945_wm_info;
1707 	else if (!IS_GEN2(dev))
1708 		wm_info = &i915_wm_info;
1709 	else
1710 		wm_info = &i830_a_wm_info;
1711 
1712 	fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1713 	crtc = intel_get_crtc_for_plane(dev, 0);
1714 	if (intel_crtc_active(crtc)) {
1715 		const struct drm_display_mode *adjusted_mode;
1716 		int cpp = crtc->primary->fb->bits_per_pixel / 8;
1717 		if (IS_GEN2(dev))
1718 			cpp = 4;
1719 
1720 		adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1721 		planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1722 					       wm_info, fifo_size, cpp,
1723 					       pessimal_latency_ns);
1724 		enabled = crtc;
1725 	} else {
1726 		planea_wm = fifo_size - wm_info->guard_size;
1727 		if (planea_wm > (long)wm_info->max_wm)
1728 			planea_wm = wm_info->max_wm;
1729 	}
1730 
1731 	if (IS_GEN2(dev))
1732 		wm_info = &i830_bc_wm_info;
1733 
1734 	fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1735 	crtc = intel_get_crtc_for_plane(dev, 1);
1736 	if (intel_crtc_active(crtc)) {
1737 		const struct drm_display_mode *adjusted_mode;
1738 		int cpp = crtc->primary->fb->bits_per_pixel / 8;
1739 		if (IS_GEN2(dev))
1740 			cpp = 4;
1741 
1742 		adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1743 		planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1744 					       wm_info, fifo_size, cpp,
1745 					       pessimal_latency_ns);
1746 		if (enabled == NULL)
1747 			enabled = crtc;
1748 		else
1749 			enabled = NULL;
1750 	} else {
1751 		planeb_wm = fifo_size - wm_info->guard_size;
1752 		if (planeb_wm > (long)wm_info->max_wm)
1753 			planeb_wm = wm_info->max_wm;
1754 	}
1755 
1756 	DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1757 
1758 	if (IS_I915GM(dev) && enabled) {
1759 		struct drm_i915_gem_object *obj;
1760 
1761 		obj = intel_fb_obj(enabled->primary->fb);
1762 
1763 		/* self-refresh seems busted with untiled */
1764 		if (obj->tiling_mode == I915_TILING_NONE)
1765 			enabled = NULL;
1766 	}
1767 
1768 	/*
1769 	 * Overlay gets an aggressive default since video jitter is bad.
1770 	 */
1771 	cwm = 2;
1772 
1773 	/* Play safe and disable self-refresh before adjusting watermarks. */
1774 	intel_set_memory_cxsr(dev_priv, false);
1775 
1776 	/* Calc sr entries for one plane configs */
1777 	if (HAS_FW_BLC(dev) && enabled) {
1778 		/* self-refresh has much higher latency */
1779 		static const int sr_latency_ns = 6000;
1780 		const struct drm_display_mode *adjusted_mode =
1781 			&to_intel_crtc(enabled)->config.adjusted_mode;
1782 		int clock = adjusted_mode->crtc_clock;
1783 		int htotal = adjusted_mode->crtc_htotal;
1784 		int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
1785 		int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
1786 		unsigned long line_time_us;
1787 		int entries;
1788 
1789 		line_time_us = max(htotal * 1000 / clock, 1);
1790 
1791 		/* Use ns/us then divide to preserve precision */
1792 		entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1793 			pixel_size * hdisplay;
1794 		entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1795 		DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1796 		srwm = wm_info->fifo_size - entries;
1797 		if (srwm < 0)
1798 			srwm = 1;
1799 
1800 		if (IS_I945G(dev) || IS_I945GM(dev))
1801 			I915_WRITE(FW_BLC_SELF,
1802 				   FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1803 		else if (IS_I915GM(dev))
1804 			I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1805 	}
1806 
1807 	DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1808 		      planea_wm, planeb_wm, cwm, srwm);
1809 
1810 	fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1811 	fwater_hi = (cwm & 0x1f);
1812 
1813 	/* Set request length to 8 cachelines per fetch */
1814 	fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1815 	fwater_hi = fwater_hi | (1 << 8);
1816 
1817 	I915_WRITE(FW_BLC, fwater_lo);
1818 	I915_WRITE(FW_BLC2, fwater_hi);
1819 
1820 	if (enabled)
1821 		intel_set_memory_cxsr(dev_priv, true);
1822 }
1823 
i845_update_wm(struct drm_crtc * unused_crtc)1824 static void i845_update_wm(struct drm_crtc *unused_crtc)
1825 {
1826 	struct drm_device *dev = unused_crtc->dev;
1827 	struct drm_i915_private *dev_priv = dev->dev_private;
1828 	struct drm_crtc *crtc;
1829 	const struct drm_display_mode *adjusted_mode;
1830 	uint32_t fwater_lo;
1831 	int planea_wm;
1832 
1833 	crtc = single_enabled_crtc(dev);
1834 	if (crtc == NULL)
1835 		return;
1836 
1837 	adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1838 	planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1839 				       &i845_wm_info,
1840 				       dev_priv->display.get_fifo_size(dev, 0),
1841 				       4, pessimal_latency_ns);
1842 	fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1843 	fwater_lo |= (3<<8) | planea_wm;
1844 
1845 	DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1846 
1847 	I915_WRITE(FW_BLC, fwater_lo);
1848 }
1849 
ilk_pipe_pixel_rate(struct drm_device * dev,struct drm_crtc * crtc)1850 static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1851 				    struct drm_crtc *crtc)
1852 {
1853 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1854 	uint32_t pixel_rate;
1855 
1856 	pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
1857 
1858 	/* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1859 	 * adjust the pixel_rate here. */
1860 
1861 	if (intel_crtc->config.pch_pfit.enabled) {
1862 		uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
1863 		uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
1864 
1865 		pipe_w = intel_crtc->config.pipe_src_w;
1866 		pipe_h = intel_crtc->config.pipe_src_h;
1867 		pfit_w = (pfit_size >> 16) & 0xFFFF;
1868 		pfit_h = pfit_size & 0xFFFF;
1869 		if (pipe_w < pfit_w)
1870 			pipe_w = pfit_w;
1871 		if (pipe_h < pfit_h)
1872 			pipe_h = pfit_h;
1873 
1874 		pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1875 				     pfit_w * pfit_h);
1876 	}
1877 
1878 	return pixel_rate;
1879 }
1880 
1881 /* latency must be in 0.1us units. */
ilk_wm_method1(uint32_t pixel_rate,uint8_t bytes_per_pixel,uint32_t latency)1882 static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
1883 			       uint32_t latency)
1884 {
1885 	uint64_t ret;
1886 
1887 	if (WARN(latency == 0, "Latency value missing\n"))
1888 		return UINT_MAX;
1889 
1890 	ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1891 	ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1892 
1893 	return ret;
1894 }
1895 
1896 /* latency must be in 0.1us units. */
ilk_wm_method2(uint32_t pixel_rate,uint32_t pipe_htotal,uint32_t horiz_pixels,uint8_t bytes_per_pixel,uint32_t latency)1897 static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
1898 			       uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1899 			       uint32_t latency)
1900 {
1901 	uint32_t ret;
1902 
1903 	if (WARN(latency == 0, "Latency value missing\n"))
1904 		return UINT_MAX;
1905 
1906 	ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1907 	ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1908 	ret = DIV_ROUND_UP(ret, 64) + 2;
1909 	return ret;
1910 }
1911 
ilk_wm_fbc(uint32_t pri_val,uint32_t horiz_pixels,uint8_t bytes_per_pixel)1912 static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
1913 			   uint8_t bytes_per_pixel)
1914 {
1915 	return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1916 }
1917 
1918 struct ilk_pipe_wm_parameters {
1919 	bool active;
1920 	uint32_t pipe_htotal;
1921 	uint32_t pixel_rate;
1922 	struct intel_plane_wm_parameters pri;
1923 	struct intel_plane_wm_parameters spr;
1924 	struct intel_plane_wm_parameters cur;
1925 };
1926 
1927 struct ilk_wm_maximums {
1928 	uint16_t pri;
1929 	uint16_t spr;
1930 	uint16_t cur;
1931 	uint16_t fbc;
1932 };
1933 
1934 /* used in computing the new watermarks state */
1935 struct intel_wm_config {
1936 	unsigned int num_pipes_active;
1937 	bool sprites_enabled;
1938 	bool sprites_scaled;
1939 };
1940 
1941 /*
1942  * For both WM_PIPE and WM_LP.
1943  * mem_value must be in 0.1us units.
1944  */
ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters * params,uint32_t mem_value,bool is_lp)1945 static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
1946 				   uint32_t mem_value,
1947 				   bool is_lp)
1948 {
1949 	uint32_t method1, method2;
1950 
1951 	if (!params->active || !params->pri.enabled)
1952 		return 0;
1953 
1954 	method1 = ilk_wm_method1(params->pixel_rate,
1955 				 params->pri.bytes_per_pixel,
1956 				 mem_value);
1957 
1958 	if (!is_lp)
1959 		return method1;
1960 
1961 	method2 = ilk_wm_method2(params->pixel_rate,
1962 				 params->pipe_htotal,
1963 				 params->pri.horiz_pixels,
1964 				 params->pri.bytes_per_pixel,
1965 				 mem_value);
1966 
1967 	return min(method1, method2);
1968 }
1969 
1970 /*
1971  * For both WM_PIPE and WM_LP.
1972  * mem_value must be in 0.1us units.
1973  */
ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters * params,uint32_t mem_value)1974 static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
1975 				   uint32_t mem_value)
1976 {
1977 	uint32_t method1, method2;
1978 
1979 	if (!params->active || !params->spr.enabled)
1980 		return 0;
1981 
1982 	method1 = ilk_wm_method1(params->pixel_rate,
1983 				 params->spr.bytes_per_pixel,
1984 				 mem_value);
1985 	method2 = ilk_wm_method2(params->pixel_rate,
1986 				 params->pipe_htotal,
1987 				 params->spr.horiz_pixels,
1988 				 params->spr.bytes_per_pixel,
1989 				 mem_value);
1990 	return min(method1, method2);
1991 }
1992 
1993 /*
1994  * For both WM_PIPE and WM_LP.
1995  * mem_value must be in 0.1us units.
1996  */
ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters * params,uint32_t mem_value)1997 static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
1998 				   uint32_t mem_value)
1999 {
2000 	if (!params->active || !params->cur.enabled)
2001 		return 0;
2002 
2003 	return ilk_wm_method2(params->pixel_rate,
2004 			      params->pipe_htotal,
2005 			      params->cur.horiz_pixels,
2006 			      params->cur.bytes_per_pixel,
2007 			      mem_value);
2008 }
2009 
2010 /* Only for WM_LP. */
ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters * params,uint32_t pri_val)2011 static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
2012 				   uint32_t pri_val)
2013 {
2014 	if (!params->active || !params->pri.enabled)
2015 		return 0;
2016 
2017 	return ilk_wm_fbc(pri_val,
2018 			  params->pri.horiz_pixels,
2019 			  params->pri.bytes_per_pixel);
2020 }
2021 
ilk_display_fifo_size(const struct drm_device * dev)2022 static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
2023 {
2024 	if (INTEL_INFO(dev)->gen >= 8)
2025 		return 3072;
2026 	else if (INTEL_INFO(dev)->gen >= 7)
2027 		return 768;
2028 	else
2029 		return 512;
2030 }
2031 
ilk_plane_wm_reg_max(const struct drm_device * dev,int level,bool is_sprite)2032 static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
2033 					 int level, bool is_sprite)
2034 {
2035 	if (INTEL_INFO(dev)->gen >= 8)
2036 		/* BDW primary/sprite plane watermarks */
2037 		return level == 0 ? 255 : 2047;
2038 	else if (INTEL_INFO(dev)->gen >= 7)
2039 		/* IVB/HSW primary/sprite plane watermarks */
2040 		return level == 0 ? 127 : 1023;
2041 	else if (!is_sprite)
2042 		/* ILK/SNB primary plane watermarks */
2043 		return level == 0 ? 127 : 511;
2044 	else
2045 		/* ILK/SNB sprite plane watermarks */
2046 		return level == 0 ? 63 : 255;
2047 }
2048 
ilk_cursor_wm_reg_max(const struct drm_device * dev,int level)2049 static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
2050 					  int level)
2051 {
2052 	if (INTEL_INFO(dev)->gen >= 7)
2053 		return level == 0 ? 63 : 255;
2054 	else
2055 		return level == 0 ? 31 : 63;
2056 }
2057 
ilk_fbc_wm_reg_max(const struct drm_device * dev)2058 static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
2059 {
2060 	if (INTEL_INFO(dev)->gen >= 8)
2061 		return 31;
2062 	else
2063 		return 15;
2064 }
2065 
2066 /* Calculate the maximum primary/sprite plane watermark */
ilk_plane_wm_max(const struct drm_device * dev,int level,const struct intel_wm_config * config,enum intel_ddb_partitioning ddb_partitioning,bool is_sprite)2067 static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2068 				     int level,
2069 				     const struct intel_wm_config *config,
2070 				     enum intel_ddb_partitioning ddb_partitioning,
2071 				     bool is_sprite)
2072 {
2073 	unsigned int fifo_size = ilk_display_fifo_size(dev);
2074 
2075 	/* if sprites aren't enabled, sprites get nothing */
2076 	if (is_sprite && !config->sprites_enabled)
2077 		return 0;
2078 
2079 	/* HSW allows LP1+ watermarks even with multiple pipes */
2080 	if (level == 0 || config->num_pipes_active > 1) {
2081 		fifo_size /= INTEL_INFO(dev)->num_pipes;
2082 
2083 		/*
2084 		 * For some reason the non self refresh
2085 		 * FIFO size is only half of the self
2086 		 * refresh FIFO size on ILK/SNB.
2087 		 */
2088 		if (INTEL_INFO(dev)->gen <= 6)
2089 			fifo_size /= 2;
2090 	}
2091 
2092 	if (config->sprites_enabled) {
2093 		/* level 0 is always calculated with 1:1 split */
2094 		if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2095 			if (is_sprite)
2096 				fifo_size *= 5;
2097 			fifo_size /= 6;
2098 		} else {
2099 			fifo_size /= 2;
2100 		}
2101 	}
2102 
2103 	/* clamp to max that the registers can hold */
2104 	return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
2105 }
2106 
2107 /* Calculate the maximum cursor plane watermark */
ilk_cursor_wm_max(const struct drm_device * dev,int level,const struct intel_wm_config * config)2108 static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
2109 				      int level,
2110 				      const struct intel_wm_config *config)
2111 {
2112 	/* HSW LP1+ watermarks w/ multiple pipes */
2113 	if (level > 0 && config->num_pipes_active > 1)
2114 		return 64;
2115 
2116 	/* otherwise just report max that registers can hold */
2117 	return ilk_cursor_wm_reg_max(dev, level);
2118 }
2119 
ilk_compute_wm_maximums(const struct drm_device * dev,int level,const struct intel_wm_config * config,enum intel_ddb_partitioning ddb_partitioning,struct ilk_wm_maximums * max)2120 static void ilk_compute_wm_maximums(const struct drm_device *dev,
2121 				    int level,
2122 				    const struct intel_wm_config *config,
2123 				    enum intel_ddb_partitioning ddb_partitioning,
2124 				    struct ilk_wm_maximums *max)
2125 {
2126 	max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2127 	max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2128 	max->cur = ilk_cursor_wm_max(dev, level, config);
2129 	max->fbc = ilk_fbc_wm_reg_max(dev);
2130 }
2131 
ilk_compute_wm_reg_maximums(struct drm_device * dev,int level,struct ilk_wm_maximums * max)2132 static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
2133 					int level,
2134 					struct ilk_wm_maximums *max)
2135 {
2136 	max->pri = ilk_plane_wm_reg_max(dev, level, false);
2137 	max->spr = ilk_plane_wm_reg_max(dev, level, true);
2138 	max->cur = ilk_cursor_wm_reg_max(dev, level);
2139 	max->fbc = ilk_fbc_wm_reg_max(dev);
2140 }
2141 
ilk_validate_wm_level(int level,const struct ilk_wm_maximums * max,struct intel_wm_level * result)2142 static bool ilk_validate_wm_level(int level,
2143 				  const struct ilk_wm_maximums *max,
2144 				  struct intel_wm_level *result)
2145 {
2146 	bool ret;
2147 
2148 	/* already determined to be invalid? */
2149 	if (!result->enable)
2150 		return false;
2151 
2152 	result->enable = result->pri_val <= max->pri &&
2153 			 result->spr_val <= max->spr &&
2154 			 result->cur_val <= max->cur;
2155 
2156 	ret = result->enable;
2157 
2158 	/*
2159 	 * HACK until we can pre-compute everything,
2160 	 * and thus fail gracefully if LP0 watermarks
2161 	 * are exceeded...
2162 	 */
2163 	if (level == 0 && !result->enable) {
2164 		if (result->pri_val > max->pri)
2165 			DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2166 				      level, result->pri_val, max->pri);
2167 		if (result->spr_val > max->spr)
2168 			DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2169 				      level, result->spr_val, max->spr);
2170 		if (result->cur_val > max->cur)
2171 			DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2172 				      level, result->cur_val, max->cur);
2173 
2174 		result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2175 		result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2176 		result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2177 		result->enable = true;
2178 	}
2179 
2180 	return ret;
2181 }
2182 
ilk_compute_wm_level(const struct drm_i915_private * dev_priv,int level,const struct ilk_pipe_wm_parameters * p,struct intel_wm_level * result)2183 static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
2184 				 int level,
2185 				 const struct ilk_pipe_wm_parameters *p,
2186 				 struct intel_wm_level *result)
2187 {
2188 	uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2189 	uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2190 	uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2191 
2192 	/* WM1+ latency values stored in 0.5us units */
2193 	if (level > 0) {
2194 		pri_latency *= 5;
2195 		spr_latency *= 5;
2196 		cur_latency *= 5;
2197 	}
2198 
2199 	result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2200 	result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2201 	result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2202 	result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2203 	result->enable = true;
2204 }
2205 
2206 static uint32_t
hsw_compute_linetime_wm(struct drm_device * dev,struct drm_crtc * crtc)2207 hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
2208 {
2209 	struct drm_i915_private *dev_priv = dev->dev_private;
2210 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2211 	struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
2212 	u32 linetime, ips_linetime;
2213 
2214 	if (!intel_crtc_active(crtc))
2215 		return 0;
2216 
2217 	/* The WM are computed with base on how long it takes to fill a single
2218 	 * row at the given clock rate, multiplied by 8.
2219 	 * */
2220 	linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2221 				     mode->crtc_clock);
2222 	ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2223 					 intel_ddi_get_cdclk_freq(dev_priv));
2224 
2225 	return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2226 	       PIPE_WM_LINETIME_TIME(linetime);
2227 }
2228 
intel_read_wm_latency(struct drm_device * dev,uint16_t wm[5])2229 static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[5])
2230 {
2231 	struct drm_i915_private *dev_priv = dev->dev_private;
2232 
2233 	if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2234 		uint64_t sskpd = I915_READ64(MCH_SSKPD);
2235 
2236 		wm[0] = (sskpd >> 56) & 0xFF;
2237 		if (wm[0] == 0)
2238 			wm[0] = sskpd & 0xF;
2239 		wm[1] = (sskpd >> 4) & 0xFF;
2240 		wm[2] = (sskpd >> 12) & 0xFF;
2241 		wm[3] = (sskpd >> 20) & 0x1FF;
2242 		wm[4] = (sskpd >> 32) & 0x1FF;
2243 	} else if (INTEL_INFO(dev)->gen >= 6) {
2244 		uint32_t sskpd = I915_READ(MCH_SSKPD);
2245 
2246 		wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2247 		wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2248 		wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2249 		wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
2250 	} else if (INTEL_INFO(dev)->gen >= 5) {
2251 		uint32_t mltr = I915_READ(MLTR_ILK);
2252 
2253 		/* ILK primary LP0 latency is 700 ns */
2254 		wm[0] = 7;
2255 		wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2256 		wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
2257 	}
2258 }
2259 
intel_fixup_spr_wm_latency(struct drm_device * dev,uint16_t wm[5])2260 static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2261 {
2262 	/* ILK sprite LP0 latency is 1300 ns */
2263 	if (INTEL_INFO(dev)->gen == 5)
2264 		wm[0] = 13;
2265 }
2266 
intel_fixup_cur_wm_latency(struct drm_device * dev,uint16_t wm[5])2267 static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2268 {
2269 	/* ILK cursor LP0 latency is 1300 ns */
2270 	if (INTEL_INFO(dev)->gen == 5)
2271 		wm[0] = 13;
2272 
2273 	/* WaDoubleCursorLP3Latency:ivb */
2274 	if (IS_IVYBRIDGE(dev))
2275 		wm[3] *= 2;
2276 }
2277 
ilk_wm_max_level(const struct drm_device * dev)2278 int ilk_wm_max_level(const struct drm_device *dev)
2279 {
2280 	/* how many WM levels are we expecting */
2281 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2282 		return 4;
2283 	else if (INTEL_INFO(dev)->gen >= 6)
2284 		return 3;
2285 	else
2286 		return 2;
2287 }
2288 
intel_print_wm_latency(struct drm_device * dev,const char * name,const uint16_t wm[5])2289 static void intel_print_wm_latency(struct drm_device *dev,
2290 				   const char *name,
2291 				   const uint16_t wm[5])
2292 {
2293 	int level, max_level = ilk_wm_max_level(dev);
2294 
2295 	for (level = 0; level <= max_level; level++) {
2296 		unsigned int latency = wm[level];
2297 
2298 		if (latency == 0) {
2299 			DRM_ERROR("%s WM%d latency not provided\n",
2300 				  name, level);
2301 			continue;
2302 		}
2303 
2304 		/* WM1+ latency values in 0.5us units */
2305 		if (level > 0)
2306 			latency *= 5;
2307 
2308 		DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2309 			      name, level, wm[level],
2310 			      latency / 10, latency % 10);
2311 	}
2312 }
2313 
ilk_increase_wm_latency(struct drm_i915_private * dev_priv,uint16_t wm[5],uint16_t min)2314 static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2315 				    uint16_t wm[5], uint16_t min)
2316 {
2317 	int level, max_level = ilk_wm_max_level(dev_priv->dev);
2318 
2319 	if (wm[0] >= min)
2320 		return false;
2321 
2322 	wm[0] = max(wm[0], min);
2323 	for (level = 1; level <= max_level; level++)
2324 		wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2325 
2326 	return true;
2327 }
2328 
snb_wm_latency_quirk(struct drm_device * dev)2329 static void snb_wm_latency_quirk(struct drm_device *dev)
2330 {
2331 	struct drm_i915_private *dev_priv = dev->dev_private;
2332 	bool changed;
2333 
2334 	/*
2335 	 * The BIOS provided WM memory latency values are often
2336 	 * inadequate for high resolution displays. Adjust them.
2337 	 */
2338 	changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2339 		ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2340 		ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2341 
2342 	if (!changed)
2343 		return;
2344 
2345 	DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2346 	intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2347 	intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2348 	intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2349 }
2350 
ilk_setup_wm_latency(struct drm_device * dev)2351 static void ilk_setup_wm_latency(struct drm_device *dev)
2352 {
2353 	struct drm_i915_private *dev_priv = dev->dev_private;
2354 
2355 	intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2356 
2357 	memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2358 	       sizeof(dev_priv->wm.pri_latency));
2359 	memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2360 	       sizeof(dev_priv->wm.pri_latency));
2361 
2362 	intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2363 	intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
2364 
2365 	intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2366 	intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2367 	intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2368 
2369 	if (IS_GEN6(dev))
2370 		snb_wm_latency_quirk(dev);
2371 }
2372 
ilk_compute_wm_parameters(struct drm_crtc * crtc,struct ilk_pipe_wm_parameters * p)2373 static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
2374 				      struct ilk_pipe_wm_parameters *p)
2375 {
2376 	struct drm_device *dev = crtc->dev;
2377 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2378 	enum pipe pipe = intel_crtc->pipe;
2379 	struct drm_plane *plane;
2380 
2381 	if (!intel_crtc_active(crtc))
2382 		return;
2383 
2384 	p->active = true;
2385 	p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
2386 	p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
2387 	p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
2388 	p->cur.bytes_per_pixel = 4;
2389 	p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
2390 	p->cur.horiz_pixels = intel_crtc->cursor_width;
2391 	/* TODO: for now, assume primary and cursor planes are always enabled. */
2392 	p->pri.enabled = true;
2393 	p->cur.enabled = true;
2394 
2395 	drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
2396 		struct intel_plane *intel_plane = to_intel_plane(plane);
2397 
2398 		if (intel_plane->pipe == pipe) {
2399 			p->spr = intel_plane->wm;
2400 			break;
2401 		}
2402 	}
2403 }
2404 
ilk_compute_wm_config(struct drm_device * dev,struct intel_wm_config * config)2405 static void ilk_compute_wm_config(struct drm_device *dev,
2406 				  struct intel_wm_config *config)
2407 {
2408 	struct intel_crtc *intel_crtc;
2409 
2410 	/* Compute the currently _active_ config */
2411 	for_each_intel_crtc(dev, intel_crtc) {
2412 		const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2413 
2414 		if (!wm->pipe_enabled)
2415 			continue;
2416 
2417 		config->sprites_enabled |= wm->sprites_enabled;
2418 		config->sprites_scaled |= wm->sprites_scaled;
2419 		config->num_pipes_active++;
2420 	}
2421 }
2422 
2423 /* Compute new watermarks for the pipe */
intel_compute_pipe_wm(struct drm_crtc * crtc,const struct ilk_pipe_wm_parameters * params,struct intel_pipe_wm * pipe_wm)2424 static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
2425 				  const struct ilk_pipe_wm_parameters *params,
2426 				  struct intel_pipe_wm *pipe_wm)
2427 {
2428 	struct drm_device *dev = crtc->dev;
2429 	const struct drm_i915_private *dev_priv = dev->dev_private;
2430 	int level, max_level = ilk_wm_max_level(dev);
2431 	/* LP0 watermark maximums depend on this pipe alone */
2432 	struct intel_wm_config config = {
2433 		.num_pipes_active = 1,
2434 		.sprites_enabled = params->spr.enabled,
2435 		.sprites_scaled = params->spr.scaled,
2436 	};
2437 	struct ilk_wm_maximums max;
2438 
2439 	pipe_wm->pipe_enabled = params->active;
2440 	pipe_wm->sprites_enabled = params->spr.enabled;
2441 	pipe_wm->sprites_scaled = params->spr.scaled;
2442 
2443 	/* ILK/SNB: LP2+ watermarks only w/o sprites */
2444 	if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2445 		max_level = 1;
2446 
2447 	/* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2448 	if (params->spr.scaled)
2449 		max_level = 0;
2450 
2451 	ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
2452 
2453 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2454 		pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
2455 
2456 	/* LP0 watermarks always use 1/2 DDB partitioning */
2457 	ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2458 
2459 	/* At least LP0 must be valid */
2460 	if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2461 		return false;
2462 
2463 	ilk_compute_wm_reg_maximums(dev, 1, &max);
2464 
2465 	for (level = 1; level <= max_level; level++) {
2466 		struct intel_wm_level wm = {};
2467 
2468 		ilk_compute_wm_level(dev_priv, level, params, &wm);
2469 
2470 		/*
2471 		 * Disable any watermark level that exceeds the
2472 		 * register maximums since such watermarks are
2473 		 * always invalid.
2474 		 */
2475 		if (!ilk_validate_wm_level(level, &max, &wm))
2476 			break;
2477 
2478 		pipe_wm->wm[level] = wm;
2479 	}
2480 
2481 	return true;
2482 }
2483 
2484 /*
2485  * Merge the watermarks from all active pipes for a specific level.
2486  */
ilk_merge_wm_level(struct drm_device * dev,int level,struct intel_wm_level * ret_wm)2487 static void ilk_merge_wm_level(struct drm_device *dev,
2488 			       int level,
2489 			       struct intel_wm_level *ret_wm)
2490 {
2491 	const struct intel_crtc *intel_crtc;
2492 
2493 	ret_wm->enable = true;
2494 
2495 	for_each_intel_crtc(dev, intel_crtc) {
2496 		const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2497 		const struct intel_wm_level *wm = &active->wm[level];
2498 
2499 		if (!active->pipe_enabled)
2500 			continue;
2501 
2502 		/*
2503 		 * The watermark values may have been used in the past,
2504 		 * so we must maintain them in the registers for some
2505 		 * time even if the level is now disabled.
2506 		 */
2507 		if (!wm->enable)
2508 			ret_wm->enable = false;
2509 
2510 		ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2511 		ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2512 		ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2513 		ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2514 	}
2515 }
2516 
2517 /*
2518  * Merge all low power watermarks for all active pipes.
2519  */
ilk_wm_merge(struct drm_device * dev,const struct intel_wm_config * config,const struct ilk_wm_maximums * max,struct intel_pipe_wm * merged)2520 static void ilk_wm_merge(struct drm_device *dev,
2521 			 const struct intel_wm_config *config,
2522 			 const struct ilk_wm_maximums *max,
2523 			 struct intel_pipe_wm *merged)
2524 {
2525 	int level, max_level = ilk_wm_max_level(dev);
2526 	int last_enabled_level = max_level;
2527 
2528 	/* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2529 	if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2530 	    config->num_pipes_active > 1)
2531 		return;
2532 
2533 	/* ILK: FBC WM must be disabled always */
2534 	merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
2535 
2536 	/* merge each WM1+ level */
2537 	for (level = 1; level <= max_level; level++) {
2538 		struct intel_wm_level *wm = &merged->wm[level];
2539 
2540 		ilk_merge_wm_level(dev, level, wm);
2541 
2542 		if (level > last_enabled_level)
2543 			wm->enable = false;
2544 		else if (!ilk_validate_wm_level(level, max, wm))
2545 			/* make sure all following levels get disabled */
2546 			last_enabled_level = level - 1;
2547 
2548 		/*
2549 		 * The spec says it is preferred to disable
2550 		 * FBC WMs instead of disabling a WM level.
2551 		 */
2552 		if (wm->fbc_val > max->fbc) {
2553 			if (wm->enable)
2554 				merged->fbc_wm_enabled = false;
2555 			wm->fbc_val = 0;
2556 		}
2557 	}
2558 
2559 	/* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2560 	/*
2561 	 * FIXME this is racy. FBC might get enabled later.
2562 	 * What we should check here is whether FBC can be
2563 	 * enabled sometime later.
2564 	 */
2565 	if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2566 		for (level = 2; level <= max_level; level++) {
2567 			struct intel_wm_level *wm = &merged->wm[level];
2568 
2569 			wm->enable = false;
2570 		}
2571 	}
2572 }
2573 
ilk_wm_lp_to_level(int wm_lp,const struct intel_pipe_wm * pipe_wm)2574 static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2575 {
2576 	/* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2577 	return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2578 }
2579 
2580 /* The value we need to program into the WM_LPx latency field */
ilk_wm_lp_latency(struct drm_device * dev,int level)2581 static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2582 {
2583 	struct drm_i915_private *dev_priv = dev->dev_private;
2584 
2585 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2586 		return 2 * level;
2587 	else
2588 		return dev_priv->wm.pri_latency[level];
2589 }
2590 
ilk_compute_wm_results(struct drm_device * dev,const struct intel_pipe_wm * merged,enum intel_ddb_partitioning partitioning,struct ilk_wm_values * results)2591 static void ilk_compute_wm_results(struct drm_device *dev,
2592 				   const struct intel_pipe_wm *merged,
2593 				   enum intel_ddb_partitioning partitioning,
2594 				   struct ilk_wm_values *results)
2595 {
2596 	struct intel_crtc *intel_crtc;
2597 	int level, wm_lp;
2598 
2599 	results->enable_fbc_wm = merged->fbc_wm_enabled;
2600 	results->partitioning = partitioning;
2601 
2602 	/* LP1+ register values */
2603 	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2604 		const struct intel_wm_level *r;
2605 
2606 		level = ilk_wm_lp_to_level(wm_lp, merged);
2607 
2608 		r = &merged->wm[level];
2609 
2610 		/*
2611 		 * Maintain the watermark values even if the level is
2612 		 * disabled. Doing otherwise could cause underruns.
2613 		 */
2614 		results->wm_lp[wm_lp - 1] =
2615 			(ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
2616 			(r->pri_val << WM1_LP_SR_SHIFT) |
2617 			r->cur_val;
2618 
2619 		if (r->enable)
2620 			results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2621 
2622 		if (INTEL_INFO(dev)->gen >= 8)
2623 			results->wm_lp[wm_lp - 1] |=
2624 				r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2625 		else
2626 			results->wm_lp[wm_lp - 1] |=
2627 				r->fbc_val << WM1_LP_FBC_SHIFT;
2628 
2629 		/*
2630 		 * Always set WM1S_LP_EN when spr_val != 0, even if the
2631 		 * level is disabled. Doing otherwise could cause underruns.
2632 		 */
2633 		if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2634 			WARN_ON(wm_lp != 1);
2635 			results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2636 		} else
2637 			results->wm_lp_spr[wm_lp - 1] = r->spr_val;
2638 	}
2639 
2640 	/* LP0 register values */
2641 	for_each_intel_crtc(dev, intel_crtc) {
2642 		enum pipe pipe = intel_crtc->pipe;
2643 		const struct intel_wm_level *r =
2644 			&intel_crtc->wm.active.wm[0];
2645 
2646 		if (WARN_ON(!r->enable))
2647 			continue;
2648 
2649 		results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2650 
2651 		results->wm_pipe[pipe] =
2652 			(r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2653 			(r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2654 			r->cur_val;
2655 	}
2656 }
2657 
2658 /* Find the result with the highest level enabled. Check for enable_fbc_wm in
2659  * case both are at the same level. Prefer r1 in case they're the same. */
ilk_find_best_result(struct drm_device * dev,struct intel_pipe_wm * r1,struct intel_pipe_wm * r2)2660 static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
2661 						  struct intel_pipe_wm *r1,
2662 						  struct intel_pipe_wm *r2)
2663 {
2664 	int level, max_level = ilk_wm_max_level(dev);
2665 	int level1 = 0, level2 = 0;
2666 
2667 	for (level = 1; level <= max_level; level++) {
2668 		if (r1->wm[level].enable)
2669 			level1 = level;
2670 		if (r2->wm[level].enable)
2671 			level2 = level;
2672 	}
2673 
2674 	if (level1 == level2) {
2675 		if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
2676 			return r2;
2677 		else
2678 			return r1;
2679 	} else if (level1 > level2) {
2680 		return r1;
2681 	} else {
2682 		return r2;
2683 	}
2684 }
2685 
2686 /* dirty bits used to track which watermarks need changes */
2687 #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2688 #define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2689 #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2690 #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2691 #define WM_DIRTY_FBC (1 << 24)
2692 #define WM_DIRTY_DDB (1 << 25)
2693 
ilk_compute_wm_dirty(struct drm_i915_private * dev_priv,const struct ilk_wm_values * old,const struct ilk_wm_values * new)2694 static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
2695 					 const struct ilk_wm_values *old,
2696 					 const struct ilk_wm_values *new)
2697 {
2698 	unsigned int dirty = 0;
2699 	enum pipe pipe;
2700 	int wm_lp;
2701 
2702 	for_each_pipe(dev_priv, pipe) {
2703 		if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2704 			dirty |= WM_DIRTY_LINETIME(pipe);
2705 			/* Must disable LP1+ watermarks too */
2706 			dirty |= WM_DIRTY_LP_ALL;
2707 		}
2708 
2709 		if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2710 			dirty |= WM_DIRTY_PIPE(pipe);
2711 			/* Must disable LP1+ watermarks too */
2712 			dirty |= WM_DIRTY_LP_ALL;
2713 		}
2714 	}
2715 
2716 	if (old->enable_fbc_wm != new->enable_fbc_wm) {
2717 		dirty |= WM_DIRTY_FBC;
2718 		/* Must disable LP1+ watermarks too */
2719 		dirty |= WM_DIRTY_LP_ALL;
2720 	}
2721 
2722 	if (old->partitioning != new->partitioning) {
2723 		dirty |= WM_DIRTY_DDB;
2724 		/* Must disable LP1+ watermarks too */
2725 		dirty |= WM_DIRTY_LP_ALL;
2726 	}
2727 
2728 	/* LP1+ watermarks already deemed dirty, no need to continue */
2729 	if (dirty & WM_DIRTY_LP_ALL)
2730 		return dirty;
2731 
2732 	/* Find the lowest numbered LP1+ watermark in need of an update... */
2733 	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2734 		if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2735 		    old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2736 			break;
2737 	}
2738 
2739 	/* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2740 	for (; wm_lp <= 3; wm_lp++)
2741 		dirty |= WM_DIRTY_LP(wm_lp);
2742 
2743 	return dirty;
2744 }
2745 
_ilk_disable_lp_wm(struct drm_i915_private * dev_priv,unsigned int dirty)2746 static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2747 			       unsigned int dirty)
2748 {
2749 	struct ilk_wm_values *previous = &dev_priv->wm.hw;
2750 	bool changed = false;
2751 
2752 	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2753 		previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2754 		I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2755 		changed = true;
2756 	}
2757 	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2758 		previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2759 		I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2760 		changed = true;
2761 	}
2762 	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2763 		previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2764 		I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2765 		changed = true;
2766 	}
2767 
2768 	/*
2769 	 * Don't touch WM1S_LP_EN here.
2770 	 * Doing so could cause underruns.
2771 	 */
2772 
2773 	return changed;
2774 }
2775 
2776 /*
2777  * The spec says we shouldn't write when we don't need, because every write
2778  * causes WMs to be re-evaluated, expending some power.
2779  */
ilk_write_wm_values(struct drm_i915_private * dev_priv,struct ilk_wm_values * results)2780 static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2781 				struct ilk_wm_values *results)
2782 {
2783 	struct drm_device *dev = dev_priv->dev;
2784 	struct ilk_wm_values *previous = &dev_priv->wm.hw;
2785 	unsigned int dirty;
2786 	uint32_t val;
2787 
2788 	dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
2789 	if (!dirty)
2790 		return;
2791 
2792 	_ilk_disable_lp_wm(dev_priv, dirty);
2793 
2794 	if (dirty & WM_DIRTY_PIPE(PIPE_A))
2795 		I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
2796 	if (dirty & WM_DIRTY_PIPE(PIPE_B))
2797 		I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
2798 	if (dirty & WM_DIRTY_PIPE(PIPE_C))
2799 		I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2800 
2801 	if (dirty & WM_DIRTY_LINETIME(PIPE_A))
2802 		I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
2803 	if (dirty & WM_DIRTY_LINETIME(PIPE_B))
2804 		I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
2805 	if (dirty & WM_DIRTY_LINETIME(PIPE_C))
2806 		I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2807 
2808 	if (dirty & WM_DIRTY_DDB) {
2809 		if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2810 			val = I915_READ(WM_MISC);
2811 			if (results->partitioning == INTEL_DDB_PART_1_2)
2812 				val &= ~WM_MISC_DATA_PARTITION_5_6;
2813 			else
2814 				val |= WM_MISC_DATA_PARTITION_5_6;
2815 			I915_WRITE(WM_MISC, val);
2816 		} else {
2817 			val = I915_READ(DISP_ARB_CTL2);
2818 			if (results->partitioning == INTEL_DDB_PART_1_2)
2819 				val &= ~DISP_DATA_PARTITION_5_6;
2820 			else
2821 				val |= DISP_DATA_PARTITION_5_6;
2822 			I915_WRITE(DISP_ARB_CTL2, val);
2823 		}
2824 	}
2825 
2826 	if (dirty & WM_DIRTY_FBC) {
2827 		val = I915_READ(DISP_ARB_CTL);
2828 		if (results->enable_fbc_wm)
2829 			val &= ~DISP_FBC_WM_DIS;
2830 		else
2831 			val |= DISP_FBC_WM_DIS;
2832 		I915_WRITE(DISP_ARB_CTL, val);
2833 	}
2834 
2835 	if (dirty & WM_DIRTY_LP(1) &&
2836 	    previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2837 		I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2838 
2839 	if (INTEL_INFO(dev)->gen >= 7) {
2840 		if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2841 			I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2842 		if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2843 			I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2844 	}
2845 
2846 	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
2847 		I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
2848 	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
2849 		I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
2850 	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
2851 		I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
2852 
2853 	dev_priv->wm.hw = *results;
2854 }
2855 
ilk_disable_lp_wm(struct drm_device * dev)2856 static bool ilk_disable_lp_wm(struct drm_device *dev)
2857 {
2858 	struct drm_i915_private *dev_priv = dev->dev_private;
2859 
2860 	return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2861 }
2862 
ilk_update_wm(struct drm_crtc * crtc)2863 static void ilk_update_wm(struct drm_crtc *crtc)
2864 {
2865 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2866 	struct drm_device *dev = crtc->dev;
2867 	struct drm_i915_private *dev_priv = dev->dev_private;
2868 	struct ilk_wm_maximums max;
2869 	struct ilk_pipe_wm_parameters params = {};
2870 	struct ilk_wm_values results = {};
2871 	enum intel_ddb_partitioning partitioning;
2872 	struct intel_pipe_wm pipe_wm = {};
2873 	struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
2874 	struct intel_wm_config config = {};
2875 
2876 	ilk_compute_wm_parameters(crtc, &params);
2877 
2878 	intel_compute_pipe_wm(crtc, &params, &pipe_wm);
2879 
2880 	if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
2881 		return;
2882 
2883 	intel_crtc->wm.active = pipe_wm;
2884 
2885 	ilk_compute_wm_config(dev, &config);
2886 
2887 	ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
2888 	ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
2889 
2890 	/* 5/6 split only in single pipe config on IVB+ */
2891 	if (INTEL_INFO(dev)->gen >= 7 &&
2892 	    config.num_pipes_active == 1 && config.sprites_enabled) {
2893 		ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
2894 		ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
2895 
2896 		best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
2897 	} else {
2898 		best_lp_wm = &lp_wm_1_2;
2899 	}
2900 
2901 	partitioning = (best_lp_wm == &lp_wm_1_2) ?
2902 		       INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
2903 
2904 	ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
2905 
2906 	ilk_write_wm_values(dev_priv, &results);
2907 }
2908 
2909 static void
ilk_update_sprite_wm(struct drm_plane * plane,struct drm_crtc * crtc,uint32_t sprite_width,uint32_t sprite_height,int pixel_size,bool enabled,bool scaled)2910 ilk_update_sprite_wm(struct drm_plane *plane,
2911 		     struct drm_crtc *crtc,
2912 		     uint32_t sprite_width, uint32_t sprite_height,
2913 		     int pixel_size, bool enabled, bool scaled)
2914 {
2915 	struct drm_device *dev = plane->dev;
2916 	struct intel_plane *intel_plane = to_intel_plane(plane);
2917 
2918 	intel_plane->wm.enabled = enabled;
2919 	intel_plane->wm.scaled = scaled;
2920 	intel_plane->wm.horiz_pixels = sprite_width;
2921 	intel_plane->wm.vert_pixels = sprite_width;
2922 	intel_plane->wm.bytes_per_pixel = pixel_size;
2923 
2924 	/*
2925 	 * IVB workaround: must disable low power watermarks for at least
2926 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
2927 	 * when scaling is disabled.
2928 	 *
2929 	 * WaCxSRDisabledForSpriteScaling:ivb
2930 	 */
2931 	if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
2932 		intel_wait_for_vblank(dev, intel_plane->pipe);
2933 
2934 	ilk_update_wm(crtc);
2935 }
2936 
ilk_pipe_wm_get_hw_state(struct drm_crtc * crtc)2937 static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
2938 {
2939 	struct drm_device *dev = crtc->dev;
2940 	struct drm_i915_private *dev_priv = dev->dev_private;
2941 	struct ilk_wm_values *hw = &dev_priv->wm.hw;
2942 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2943 	struct intel_pipe_wm *active = &intel_crtc->wm.active;
2944 	enum pipe pipe = intel_crtc->pipe;
2945 	static const unsigned int wm0_pipe_reg[] = {
2946 		[PIPE_A] = WM0_PIPEA_ILK,
2947 		[PIPE_B] = WM0_PIPEB_ILK,
2948 		[PIPE_C] = WM0_PIPEC_IVB,
2949 	};
2950 
2951 	hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
2952 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2953 		hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
2954 
2955 	memset(active, 0, sizeof(*active));
2956 
2957 	active->pipe_enabled = intel_crtc_active(crtc);
2958 
2959 	if (active->pipe_enabled) {
2960 		u32 tmp = hw->wm_pipe[pipe];
2961 
2962 		/*
2963 		 * For active pipes LP0 watermark is marked as
2964 		 * enabled, and LP1+ watermaks as disabled since
2965 		 * we can't really reverse compute them in case
2966 		 * multiple pipes are active.
2967 		 */
2968 		active->wm[0].enable = true;
2969 		active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
2970 		active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
2971 		active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
2972 		active->linetime = hw->wm_linetime[pipe];
2973 	} else {
2974 		int level, max_level = ilk_wm_max_level(dev);
2975 
2976 		/*
2977 		 * For inactive pipes, all watermark levels
2978 		 * should be marked as enabled but zeroed,
2979 		 * which is what we'd compute them to.
2980 		 */
2981 		for (level = 0; level <= max_level; level++)
2982 			active->wm[level].enable = true;
2983 	}
2984 }
2985 
ilk_wm_get_hw_state(struct drm_device * dev)2986 void ilk_wm_get_hw_state(struct drm_device *dev)
2987 {
2988 	struct drm_i915_private *dev_priv = dev->dev_private;
2989 	struct ilk_wm_values *hw = &dev_priv->wm.hw;
2990 	struct drm_crtc *crtc;
2991 
2992 	for_each_crtc(dev, crtc)
2993 		ilk_pipe_wm_get_hw_state(crtc);
2994 
2995 	hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
2996 	hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
2997 	hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
2998 
2999 	hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
3000 	if (INTEL_INFO(dev)->gen >= 7) {
3001 		hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3002 		hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3003 	}
3004 
3005 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3006 		hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3007 			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3008 	else if (IS_IVYBRIDGE(dev))
3009 		hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3010 			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3011 
3012 	hw->enable_fbc_wm =
3013 		!(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3014 }
3015 
3016 /**
3017  * intel_update_watermarks - update FIFO watermark values based on current modes
3018  *
3019  * Calculate watermark values for the various WM regs based on current mode
3020  * and plane configuration.
3021  *
3022  * There are several cases to deal with here:
3023  *   - normal (i.e. non-self-refresh)
3024  *   - self-refresh (SR) mode
3025  *   - lines are large relative to FIFO size (buffer can hold up to 2)
3026  *   - lines are small relative to FIFO size (buffer can hold more than 2
3027  *     lines), so need to account for TLB latency
3028  *
3029  *   The normal calculation is:
3030  *     watermark = dotclock * bytes per pixel * latency
3031  *   where latency is platform & configuration dependent (we assume pessimal
3032  *   values here).
3033  *
3034  *   The SR calculation is:
3035  *     watermark = (trunc(latency/line time)+1) * surface width *
3036  *       bytes per pixel
3037  *   where
3038  *     line time = htotal / dotclock
3039  *     surface width = hdisplay for normal plane and 64 for cursor
3040  *   and latency is assumed to be high, as above.
3041  *
3042  * The final value programmed to the register should always be rounded up,
3043  * and include an extra 2 entries to account for clock crossings.
3044  *
3045  * We don't use the sprite, so we can ignore that.  And on Crestline we have
3046  * to set the non-SR watermarks to 8.
3047  */
intel_update_watermarks(struct drm_crtc * crtc)3048 void intel_update_watermarks(struct drm_crtc *crtc)
3049 {
3050 	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
3051 
3052 	if (dev_priv->display.update_wm)
3053 		dev_priv->display.update_wm(crtc);
3054 }
3055 
intel_update_sprite_watermarks(struct drm_plane * plane,struct drm_crtc * crtc,uint32_t sprite_width,uint32_t sprite_height,int pixel_size,bool enabled,bool scaled)3056 void intel_update_sprite_watermarks(struct drm_plane *plane,
3057 				    struct drm_crtc *crtc,
3058 				    uint32_t sprite_width,
3059 				    uint32_t sprite_height,
3060 				    int pixel_size,
3061 				    bool enabled, bool scaled)
3062 {
3063 	struct drm_i915_private *dev_priv = plane->dev->dev_private;
3064 
3065 	if (dev_priv->display.update_sprite_wm)
3066 		dev_priv->display.update_sprite_wm(plane, crtc,
3067 						   sprite_width, sprite_height,
3068 						   pixel_size, enabled, scaled);
3069 }
3070 
3071 static struct drm_i915_gem_object *
intel_alloc_context_page(struct drm_device * dev)3072 intel_alloc_context_page(struct drm_device *dev)
3073 {
3074 	struct drm_i915_gem_object *ctx;
3075 	int ret;
3076 
3077 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3078 
3079 	ctx = i915_gem_alloc_object(dev, 4096);
3080 	if (!ctx) {
3081 		DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3082 		return NULL;
3083 	}
3084 
3085 	ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
3086 	if (ret) {
3087 		DRM_ERROR("failed to pin power context: %d\n", ret);
3088 		goto err_unref;
3089 	}
3090 
3091 	ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3092 	if (ret) {
3093 		DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3094 		goto err_unpin;
3095 	}
3096 
3097 	return ctx;
3098 
3099 err_unpin:
3100 	i915_gem_object_ggtt_unpin(ctx);
3101 err_unref:
3102 	drm_gem_object_unreference(&ctx->base);
3103 	return NULL;
3104 }
3105 
3106 /**
3107  * Lock protecting IPS related data structures
3108  */
3109 DEFINE_SPINLOCK(mchdev_lock);
3110 
3111 /* Global for IPS driver to get at the current i915 device. Protected by
3112  * mchdev_lock. */
3113 static struct drm_i915_private *i915_mch_dev;
3114 
ironlake_set_drps(struct drm_device * dev,u8 val)3115 bool ironlake_set_drps(struct drm_device *dev, u8 val)
3116 {
3117 	struct drm_i915_private *dev_priv = dev->dev_private;
3118 	u16 rgvswctl;
3119 
3120 	assert_spin_locked(&mchdev_lock);
3121 
3122 	rgvswctl = I915_READ16(MEMSWCTL);
3123 	if (rgvswctl & MEMCTL_CMD_STS) {
3124 		DRM_DEBUG("gpu busy, RCS change rejected\n");
3125 		return false; /* still busy with another command */
3126 	}
3127 
3128 	rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3129 		(val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3130 	I915_WRITE16(MEMSWCTL, rgvswctl);
3131 	POSTING_READ16(MEMSWCTL);
3132 
3133 	rgvswctl |= MEMCTL_CMD_STS;
3134 	I915_WRITE16(MEMSWCTL, rgvswctl);
3135 
3136 	return true;
3137 }
3138 
ironlake_enable_drps(struct drm_device * dev)3139 static void ironlake_enable_drps(struct drm_device *dev)
3140 {
3141 	struct drm_i915_private *dev_priv = dev->dev_private;
3142 	u32 rgvmodectl = I915_READ(MEMMODECTL);
3143 	u8 fmax, fmin, fstart, vstart;
3144 
3145 	spin_lock_irq(&mchdev_lock);
3146 
3147 	/* Enable temp reporting */
3148 	I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3149 	I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3150 
3151 	/* 100ms RC evaluation intervals */
3152 	I915_WRITE(RCUPEI, 100000);
3153 	I915_WRITE(RCDNEI, 100000);
3154 
3155 	/* Set max/min thresholds to 90ms and 80ms respectively */
3156 	I915_WRITE(RCBMAXAVG, 90000);
3157 	I915_WRITE(RCBMINAVG, 80000);
3158 
3159 	I915_WRITE(MEMIHYST, 1);
3160 
3161 	/* Set up min, max, and cur for interrupt handling */
3162 	fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3163 	fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3164 	fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3165 		MEMMODE_FSTART_SHIFT;
3166 
3167 	vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3168 		PXVFREQ_PX_SHIFT;
3169 
3170 	dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3171 	dev_priv->ips.fstart = fstart;
3172 
3173 	dev_priv->ips.max_delay = fstart;
3174 	dev_priv->ips.min_delay = fmin;
3175 	dev_priv->ips.cur_delay = fstart;
3176 
3177 	DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3178 			 fmax, fmin, fstart);
3179 
3180 	I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3181 
3182 	/*
3183 	 * Interrupts will be enabled in ironlake_irq_postinstall
3184 	 */
3185 
3186 	I915_WRITE(VIDSTART, vstart);
3187 	POSTING_READ(VIDSTART);
3188 
3189 	rgvmodectl |= MEMMODE_SWMODE_EN;
3190 	I915_WRITE(MEMMODECTL, rgvmodectl);
3191 
3192 	if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
3193 		DRM_ERROR("stuck trying to change perf mode\n");
3194 	mdelay(1);
3195 
3196 	ironlake_set_drps(dev, fstart);
3197 
3198 	dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
3199 		I915_READ(0x112e0);
3200 	dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3201 	dev_priv->ips.last_count2 = I915_READ(0x112f4);
3202 	dev_priv->ips.last_time2 = ktime_get_raw_ns();
3203 
3204 	spin_unlock_irq(&mchdev_lock);
3205 }
3206 
ironlake_disable_drps(struct drm_device * dev)3207 static void ironlake_disable_drps(struct drm_device *dev)
3208 {
3209 	struct drm_i915_private *dev_priv = dev->dev_private;
3210 	u16 rgvswctl;
3211 
3212 	spin_lock_irq(&mchdev_lock);
3213 
3214 	rgvswctl = I915_READ16(MEMSWCTL);
3215 
3216 	/* Ack interrupts, disable EFC interrupt */
3217 	I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3218 	I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3219 	I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3220 	I915_WRITE(DEIIR, DE_PCU_EVENT);
3221 	I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3222 
3223 	/* Go back to the starting frequency */
3224 	ironlake_set_drps(dev, dev_priv->ips.fstart);
3225 	mdelay(1);
3226 	rgvswctl |= MEMCTL_CMD_STS;
3227 	I915_WRITE(MEMSWCTL, rgvswctl);
3228 	mdelay(1);
3229 
3230 	spin_unlock_irq(&mchdev_lock);
3231 }
3232 
3233 /* There's a funny hw issue where the hw returns all 0 when reading from
3234  * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3235  * ourselves, instead of doing a rmw cycle (which might result in us clearing
3236  * all limits and the gpu stuck at whatever frequency it is at atm).
3237  */
gen6_rps_limits(struct drm_i915_private * dev_priv,u8 val)3238 static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
3239 {
3240 	u32 limits;
3241 
3242 	/* Only set the down limit when we've reached the lowest level to avoid
3243 	 * getting more interrupts, otherwise leave this clear. This prevents a
3244 	 * race in the hw when coming out of rc6: There's a tiny window where
3245 	 * the hw runs at the minimal clock before selecting the desired
3246 	 * frequency, if the down threshold expires in that window we will not
3247 	 * receive a down interrupt. */
3248 	limits = dev_priv->rps.max_freq_softlimit << 24;
3249 	if (val <= dev_priv->rps.min_freq_softlimit)
3250 		limits |= dev_priv->rps.min_freq_softlimit << 16;
3251 
3252 	return limits;
3253 }
3254 
gen6_set_rps_thresholds(struct drm_i915_private * dev_priv,u8 val)3255 static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3256 {
3257 	int new_power;
3258 
3259 	new_power = dev_priv->rps.power;
3260 	switch (dev_priv->rps.power) {
3261 	case LOW_POWER:
3262 		if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
3263 			new_power = BETWEEN;
3264 		break;
3265 
3266 	case BETWEEN:
3267 		if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
3268 			new_power = LOW_POWER;
3269 		else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
3270 			new_power = HIGH_POWER;
3271 		break;
3272 
3273 	case HIGH_POWER:
3274 		if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
3275 			new_power = BETWEEN;
3276 		break;
3277 	}
3278 	/* Max/min bins are special */
3279 	if (val == dev_priv->rps.min_freq_softlimit)
3280 		new_power = LOW_POWER;
3281 	if (val == dev_priv->rps.max_freq_softlimit)
3282 		new_power = HIGH_POWER;
3283 	if (new_power == dev_priv->rps.power)
3284 		return;
3285 
3286 	/* Note the units here are not exactly 1us, but 1280ns. */
3287 	switch (new_power) {
3288 	case LOW_POWER:
3289 		/* Upclock if more than 95% busy over 16ms */
3290 		I915_WRITE(GEN6_RP_UP_EI, 12500);
3291 		I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3292 
3293 		/* Downclock if less than 85% busy over 32ms */
3294 		I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3295 		I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3296 
3297 		I915_WRITE(GEN6_RP_CONTROL,
3298 			   GEN6_RP_MEDIA_TURBO |
3299 			   GEN6_RP_MEDIA_HW_NORMAL_MODE |
3300 			   GEN6_RP_MEDIA_IS_GFX |
3301 			   GEN6_RP_ENABLE |
3302 			   GEN6_RP_UP_BUSY_AVG |
3303 			   GEN6_RP_DOWN_IDLE_AVG);
3304 		break;
3305 
3306 	case BETWEEN:
3307 		/* Upclock if more than 90% busy over 13ms */
3308 		I915_WRITE(GEN6_RP_UP_EI, 10250);
3309 		I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3310 
3311 		/* Downclock if less than 75% busy over 32ms */
3312 		I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3313 		I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3314 
3315 		I915_WRITE(GEN6_RP_CONTROL,
3316 			   GEN6_RP_MEDIA_TURBO |
3317 			   GEN6_RP_MEDIA_HW_NORMAL_MODE |
3318 			   GEN6_RP_MEDIA_IS_GFX |
3319 			   GEN6_RP_ENABLE |
3320 			   GEN6_RP_UP_BUSY_AVG |
3321 			   GEN6_RP_DOWN_IDLE_AVG);
3322 		break;
3323 
3324 	case HIGH_POWER:
3325 		/* Upclock if more than 85% busy over 10ms */
3326 		I915_WRITE(GEN6_RP_UP_EI, 8000);
3327 		I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3328 
3329 		/* Downclock if less than 60% busy over 32ms */
3330 		I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3331 		I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3332 
3333 		I915_WRITE(GEN6_RP_CONTROL,
3334 			   GEN6_RP_MEDIA_TURBO |
3335 			   GEN6_RP_MEDIA_HW_NORMAL_MODE |
3336 			   GEN6_RP_MEDIA_IS_GFX |
3337 			   GEN6_RP_ENABLE |
3338 			   GEN6_RP_UP_BUSY_AVG |
3339 			   GEN6_RP_DOWN_IDLE_AVG);
3340 		break;
3341 	}
3342 
3343 	dev_priv->rps.power = new_power;
3344 	dev_priv->rps.last_adj = 0;
3345 }
3346 
gen6_rps_pm_mask(struct drm_i915_private * dev_priv,u8 val)3347 static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3348 {
3349 	u32 mask = 0;
3350 
3351 	if (val > dev_priv->rps.min_freq_softlimit)
3352 		mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3353 	if (val < dev_priv->rps.max_freq_softlimit)
3354 		mask |= GEN6_PM_RP_UP_THRESHOLD;
3355 
3356 	mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3357 	mask &= dev_priv->pm_rps_events;
3358 
3359 	/* IVB and SNB hard hangs on looping batchbuffer
3360 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
3361 	 */
3362 	if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
3363 		mask |= GEN6_PM_RP_UP_EI_EXPIRED;
3364 
3365 	if (IS_GEN8(dev_priv->dev))
3366 		mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
3367 
3368 	return ~mask;
3369 }
3370 
3371 /* gen6_set_rps is called to update the frequency request, but should also be
3372  * called when the range (min_delay and max_delay) is modified so that we can
3373  * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
gen6_set_rps(struct drm_device * dev,u8 val)3374 void gen6_set_rps(struct drm_device *dev, u8 val)
3375 {
3376 	struct drm_i915_private *dev_priv = dev->dev_private;
3377 
3378 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3379 	WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3380 	WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3381 
3382 	/* min/max delay may still have been modified so be sure to
3383 	 * write the limits value.
3384 	 */
3385 	if (val != dev_priv->rps.cur_freq) {
3386 		gen6_set_rps_thresholds(dev_priv, val);
3387 
3388 		if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3389 			I915_WRITE(GEN6_RPNSWREQ,
3390 				   HSW_FREQUENCY(val));
3391 		else
3392 			I915_WRITE(GEN6_RPNSWREQ,
3393 				   GEN6_FREQUENCY(val) |
3394 				   GEN6_OFFSET(0) |
3395 				   GEN6_AGGRESSIVE_TURBO);
3396 	}
3397 
3398 	/* Make sure we continue to get interrupts
3399 	 * until we hit the minimum or maximum frequencies.
3400 	 */
3401 	I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
3402 	I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3403 
3404 	POSTING_READ(GEN6_RPNSWREQ);
3405 
3406 	dev_priv->rps.cur_freq = val;
3407 	trace_intel_gpu_freq_change(val * 50);
3408 }
3409 
3410 /* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3411  *
3412  * * If Gfx is Idle, then
3413  * 1. Mask Turbo interrupts
3414  * 2. Bring up Gfx clock
3415  * 3. Change the freq to Rpn and wait till P-Unit updates freq
3416  * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3417  * 5. Unmask Turbo interrupts
3418 */
vlv_set_rps_idle(struct drm_i915_private * dev_priv)3419 static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3420 {
3421 	struct drm_device *dev = dev_priv->dev;
3422 
3423 	/* Latest VLV doesn't need to force the gfx clock */
3424 	if (dev->pdev->revision >= 0xd) {
3425 		valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3426 		return;
3427 	}
3428 
3429 	/*
3430 	 * When we are idle.  Drop to min voltage state.
3431 	 */
3432 
3433 	if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
3434 		return;
3435 
3436 	/* Mask turbo interrupt so that they will not come in between */
3437 	I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
3438 
3439 	vlv_force_gfx_clock(dev_priv, true);
3440 
3441 	dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
3442 
3443 	vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
3444 					dev_priv->rps.min_freq_softlimit);
3445 
3446 	if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
3447 				& GENFREQSTATUS) == 0, 5))
3448 		DRM_ERROR("timed out waiting for Punit\n");
3449 
3450 	vlv_force_gfx_clock(dev_priv, false);
3451 
3452 	I915_WRITE(GEN6_PMINTRMSK,
3453 		   gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
3454 }
3455 
gen6_rps_idle(struct drm_i915_private * dev_priv)3456 void gen6_rps_idle(struct drm_i915_private *dev_priv)
3457 {
3458 	struct drm_device *dev = dev_priv->dev;
3459 
3460 	mutex_lock(&dev_priv->rps.hw_lock);
3461 	if (dev_priv->rps.enabled) {
3462 		if (IS_CHERRYVIEW(dev))
3463 			valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3464 		else if (IS_VALLEYVIEW(dev))
3465 			vlv_set_rps_idle(dev_priv);
3466 		else
3467 			gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3468 		dev_priv->rps.last_adj = 0;
3469 	}
3470 	mutex_unlock(&dev_priv->rps.hw_lock);
3471 }
3472 
gen6_rps_boost(struct drm_i915_private * dev_priv)3473 void gen6_rps_boost(struct drm_i915_private *dev_priv)
3474 {
3475 	struct drm_device *dev = dev_priv->dev;
3476 
3477 	mutex_lock(&dev_priv->rps.hw_lock);
3478 	if (dev_priv->rps.enabled) {
3479 		if (IS_VALLEYVIEW(dev))
3480 			valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
3481 		else
3482 			gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
3483 		dev_priv->rps.last_adj = 0;
3484 	}
3485 	mutex_unlock(&dev_priv->rps.hw_lock);
3486 }
3487 
valleyview_set_rps(struct drm_device * dev,u8 val)3488 void valleyview_set_rps(struct drm_device *dev, u8 val)
3489 {
3490 	struct drm_i915_private *dev_priv = dev->dev_private;
3491 
3492 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3493 	WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3494 	WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3495 
3496 	if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3497 		      "Odd GPU freq value\n"))
3498 		val &= ~1;
3499 
3500 	if (val != dev_priv->rps.cur_freq) {
3501 		DRM_DEBUG_DRIVER("GPU freq request from %d MHz (%u) to %d MHz (%u)\n",
3502 				 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
3503 				 dev_priv->rps.cur_freq,
3504 				 vlv_gpu_freq(dev_priv, val), val);
3505 
3506 		vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3507 	}
3508 
3509 	I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3510 
3511 	dev_priv->rps.cur_freq = val;
3512 	trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
3513 }
3514 
gen8_disable_rps_interrupts(struct drm_device * dev)3515 static void gen8_disable_rps_interrupts(struct drm_device *dev)
3516 {
3517 	struct drm_i915_private *dev_priv = dev->dev_private;
3518 
3519 	I915_WRITE(GEN6_PMINTRMSK, ~GEN8_PMINTR_REDIRECT_TO_NON_DISP);
3520 	I915_WRITE(GEN8_GT_IER(2), I915_READ(GEN8_GT_IER(2)) &
3521 				   ~dev_priv->pm_rps_events);
3522 	/* Complete PM interrupt masking here doesn't race with the rps work
3523 	 * item again unmasking PM interrupts because that is using a different
3524 	 * register (GEN8_GT_IMR(2)) to mask PM interrupts. The only risk is in
3525 	 * leaving stale bits in GEN8_GT_IIR(2) and GEN8_GT_IMR(2) which
3526 	 * gen8_enable_rps will clean up. */
3527 
3528 	spin_lock_irq(&dev_priv->irq_lock);
3529 	dev_priv->rps.pm_iir = 0;
3530 	spin_unlock_irq(&dev_priv->irq_lock);
3531 
3532 	I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3533 }
3534 
gen6_disable_rps_interrupts(struct drm_device * dev)3535 static void gen6_disable_rps_interrupts(struct drm_device *dev)
3536 {
3537 	struct drm_i915_private *dev_priv = dev->dev_private;
3538 
3539 	I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
3540 	I915_WRITE(GEN6_PMIER, I915_READ(GEN6_PMIER) &
3541 				~dev_priv->pm_rps_events);
3542 	/* Complete PM interrupt masking here doesn't race with the rps work
3543 	 * item again unmasking PM interrupts because that is using a different
3544 	 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
3545 	 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
3546 
3547 	spin_lock_irq(&dev_priv->irq_lock);
3548 	dev_priv->rps.pm_iir = 0;
3549 	spin_unlock_irq(&dev_priv->irq_lock);
3550 
3551 	I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
3552 }
3553 
gen6_disable_rps(struct drm_device * dev)3554 static void gen6_disable_rps(struct drm_device *dev)
3555 {
3556 	struct drm_i915_private *dev_priv = dev->dev_private;
3557 
3558 	I915_WRITE(GEN6_RC_CONTROL, 0);
3559 	I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
3560 
3561 	if (IS_BROADWELL(dev))
3562 		gen8_disable_rps_interrupts(dev);
3563 	else
3564 		gen6_disable_rps_interrupts(dev);
3565 }
3566 
cherryview_disable_rps(struct drm_device * dev)3567 static void cherryview_disable_rps(struct drm_device *dev)
3568 {
3569 	struct drm_i915_private *dev_priv = dev->dev_private;
3570 
3571 	I915_WRITE(GEN6_RC_CONTROL, 0);
3572 
3573 	gen8_disable_rps_interrupts(dev);
3574 }
3575 
valleyview_disable_rps(struct drm_device * dev)3576 static void valleyview_disable_rps(struct drm_device *dev)
3577 {
3578 	struct drm_i915_private *dev_priv = dev->dev_private;
3579 
3580 	/* we're doing forcewake before Disabling RC6,
3581 	 * This what the BIOS expects when going into suspend */
3582 	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
3583 
3584 	I915_WRITE(GEN6_RC_CONTROL, 0);
3585 
3586 	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
3587 
3588 	gen6_disable_rps_interrupts(dev);
3589 }
3590 
intel_print_rc6_info(struct drm_device * dev,u32 mode)3591 static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3592 {
3593 	if (IS_VALLEYVIEW(dev)) {
3594 		if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3595 			mode = GEN6_RC_CTL_RC6_ENABLE;
3596 		else
3597 			mode = 0;
3598 	}
3599 	DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s, RC6p %s, RC6pp %s\n",
3600 		      (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3601 		      (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3602 		      (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
3603 }
3604 
sanitize_rc6_option(const struct drm_device * dev,int enable_rc6)3605 static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
3606 {
3607 	/* No RC6 before Ironlake */
3608 	if (INTEL_INFO(dev)->gen < 5)
3609 		return 0;
3610 
3611 	/* RC6 is only on Ironlake mobile not on desktop */
3612 	if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3613 		return 0;
3614 
3615 	/* Respect the kernel parameter if it is set */
3616 	if (enable_rc6 >= 0) {
3617 		int mask;
3618 
3619 		if (INTEL_INFO(dev)->gen == 6 || IS_IVYBRIDGE(dev))
3620 			mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3621 			       INTEL_RC6pp_ENABLE;
3622 		else
3623 			mask = INTEL_RC6_ENABLE;
3624 
3625 		if ((enable_rc6 & mask) != enable_rc6)
3626 			DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
3627 				      enable_rc6 & mask, enable_rc6, mask);
3628 
3629 		return enable_rc6 & mask;
3630 	}
3631 
3632 	/* Disable RC6 on Ironlake */
3633 	if (INTEL_INFO(dev)->gen == 5)
3634 		return 0;
3635 
3636 	if (IS_IVYBRIDGE(dev))
3637 		return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
3638 
3639 	return INTEL_RC6_ENABLE;
3640 }
3641 
intel_enable_rc6(const struct drm_device * dev)3642 int intel_enable_rc6(const struct drm_device *dev)
3643 {
3644 	return i915.enable_rc6;
3645 }
3646 
gen8_enable_rps_interrupts(struct drm_device * dev)3647 static void gen8_enable_rps_interrupts(struct drm_device *dev)
3648 {
3649 	struct drm_i915_private *dev_priv = dev->dev_private;
3650 
3651 	spin_lock_irq(&dev_priv->irq_lock);
3652 	WARN_ON(dev_priv->rps.pm_iir);
3653 	gen8_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
3654 	I915_WRITE(GEN8_GT_IIR(2), dev_priv->pm_rps_events);
3655 	spin_unlock_irq(&dev_priv->irq_lock);
3656 }
3657 
gen6_enable_rps_interrupts(struct drm_device * dev)3658 static void gen6_enable_rps_interrupts(struct drm_device *dev)
3659 {
3660 	struct drm_i915_private *dev_priv = dev->dev_private;
3661 
3662 	spin_lock_irq(&dev_priv->irq_lock);
3663 	WARN_ON(dev_priv->rps.pm_iir);
3664 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
3665 	I915_WRITE(GEN6_PMIIR, dev_priv->pm_rps_events);
3666 	spin_unlock_irq(&dev_priv->irq_lock);
3667 }
3668 
parse_rp_state_cap(struct drm_i915_private * dev_priv,u32 rp_state_cap)3669 static void parse_rp_state_cap(struct drm_i915_private *dev_priv, u32 rp_state_cap)
3670 {
3671 	/* All of these values are in units of 50MHz */
3672 	dev_priv->rps.cur_freq		= 0;
3673 	/* static values from HW: RP0 < RPe < RP1 < RPn (min_freq) */
3674 	dev_priv->rps.rp1_freq		= (rp_state_cap >>  8) & 0xff;
3675 	dev_priv->rps.rp0_freq		= (rp_state_cap >>  0) & 0xff;
3676 	dev_priv->rps.min_freq		= (rp_state_cap >> 16) & 0xff;
3677 	/* XXX: only BYT has a special efficient freq */
3678 	dev_priv->rps.efficient_freq	= dev_priv->rps.rp1_freq;
3679 	/* hw_max = RP0 until we check for overclocking */
3680 	dev_priv->rps.max_freq		= dev_priv->rps.rp0_freq;
3681 
3682 	/* Preserve min/max settings in case of re-init */
3683 	if (dev_priv->rps.max_freq_softlimit == 0)
3684 		dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
3685 
3686 	if (dev_priv->rps.min_freq_softlimit == 0)
3687 		dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
3688 }
3689 
gen8_enable_rps(struct drm_device * dev)3690 static void gen8_enable_rps(struct drm_device *dev)
3691 {
3692 	struct drm_i915_private *dev_priv = dev->dev_private;
3693 	struct intel_engine_cs *ring;
3694 	uint32_t rc6_mask = 0, rp_state_cap;
3695 	int unused;
3696 
3697 	/* 1a: Software RC state - RC0 */
3698 	I915_WRITE(GEN6_RC_STATE, 0);
3699 
3700 	/* 1c & 1d: Get forcewake during program sequence. Although the driver
3701 	 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
3702 	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
3703 
3704 	/* 2a: Disable RC states. */
3705 	I915_WRITE(GEN6_RC_CONTROL, 0);
3706 
3707 	rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3708 	parse_rp_state_cap(dev_priv, rp_state_cap);
3709 
3710 	/* 2b: Program RC6 thresholds.*/
3711 	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
3712 	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
3713 	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
3714 	for_each_ring(ring, dev_priv, unused)
3715 		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3716 	I915_WRITE(GEN6_RC_SLEEP, 0);
3717 	if (IS_BROADWELL(dev))
3718 		I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
3719 	else
3720 		I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
3721 
3722 	/* 3: Enable RC6 */
3723 	if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
3724 		rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
3725 	intel_print_rc6_info(dev, rc6_mask);
3726 	if (IS_BROADWELL(dev))
3727 		I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3728 				GEN7_RC_CTL_TO_MODE |
3729 				rc6_mask);
3730 	else
3731 		I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
3732 				GEN6_RC_CTL_EI_MODE(1) |
3733 				rc6_mask);
3734 
3735 	/* 4 Program defaults and thresholds for RPS*/
3736 	I915_WRITE(GEN6_RPNSWREQ,
3737 		   HSW_FREQUENCY(dev_priv->rps.rp1_freq));
3738 	I915_WRITE(GEN6_RC_VIDEO_FREQ,
3739 		   HSW_FREQUENCY(dev_priv->rps.rp1_freq));
3740 	/* NB: Docs say 1s, and 1000000 - which aren't equivalent */
3741 	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
3742 
3743 	/* Docs recommend 900MHz, and 300 MHz respectively */
3744 	I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
3745 		   dev_priv->rps.max_freq_softlimit << 24 |
3746 		   dev_priv->rps.min_freq_softlimit << 16);
3747 
3748 	I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
3749 	I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
3750 	I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
3751 	I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
3752 
3753 	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3754 
3755 	/* 5: Enable RPS */
3756 	I915_WRITE(GEN6_RP_CONTROL,
3757 		   GEN6_RP_MEDIA_TURBO |
3758 		   GEN6_RP_MEDIA_HW_NORMAL_MODE |
3759 		   GEN6_RP_MEDIA_IS_GFX |
3760 		   GEN6_RP_ENABLE |
3761 		   GEN6_RP_UP_BUSY_AVG |
3762 		   GEN6_RP_DOWN_IDLE_AVG);
3763 
3764 	/* 6: Ring frequency + overclocking (our driver does this later */
3765 
3766 	gen6_set_rps(dev, (I915_READ(GEN6_GT_PERF_STATUS) & 0xff00) >> 8);
3767 
3768 	gen8_enable_rps_interrupts(dev);
3769 
3770 	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
3771 }
3772 
gen6_enable_rps(struct drm_device * dev)3773 static void gen6_enable_rps(struct drm_device *dev)
3774 {
3775 	struct drm_i915_private *dev_priv = dev->dev_private;
3776 	struct intel_engine_cs *ring;
3777 	u32 rp_state_cap;
3778 	u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
3779 	u32 gtfifodbg;
3780 	int rc6_mode;
3781 	int i, ret;
3782 
3783 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3784 
3785 	/* Here begins a magic sequence of register writes to enable
3786 	 * auto-downclocking.
3787 	 *
3788 	 * Perhaps there might be some value in exposing these to
3789 	 * userspace...
3790 	 */
3791 	I915_WRITE(GEN6_RC_STATE, 0);
3792 
3793 	/* Clear the DBG now so we don't confuse earlier errors */
3794 	if ((gtfifodbg = I915_READ(GTFIFODBG))) {
3795 		DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
3796 		I915_WRITE(GTFIFODBG, gtfifodbg);
3797 	}
3798 
3799 	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
3800 
3801 	rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
3802 
3803 	parse_rp_state_cap(dev_priv, rp_state_cap);
3804 
3805 	/* disable the counters and set deterministic thresholds */
3806 	I915_WRITE(GEN6_RC_CONTROL, 0);
3807 
3808 	I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
3809 	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
3810 	I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
3811 	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
3812 	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
3813 
3814 	for_each_ring(ring, dev_priv, i)
3815 		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
3816 
3817 	I915_WRITE(GEN6_RC_SLEEP, 0);
3818 	I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
3819 	if (IS_IVYBRIDGE(dev))
3820 		I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
3821 	else
3822 		I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
3823 	I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
3824 	I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
3825 
3826 	/* Check if we are enabling RC6 */
3827 	rc6_mode = intel_enable_rc6(dev_priv->dev);
3828 	if (rc6_mode & INTEL_RC6_ENABLE)
3829 		rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
3830 
3831 	/* We don't use those on Haswell */
3832 	if (!IS_HASWELL(dev)) {
3833 		if (rc6_mode & INTEL_RC6p_ENABLE)
3834 			rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
3835 
3836 		if (rc6_mode & INTEL_RC6pp_ENABLE)
3837 			rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
3838 	}
3839 
3840 	intel_print_rc6_info(dev, rc6_mask);
3841 
3842 	I915_WRITE(GEN6_RC_CONTROL,
3843 		   rc6_mask |
3844 		   GEN6_RC_CTL_EI_MODE(1) |
3845 		   GEN6_RC_CTL_HW_ENABLE);
3846 
3847 	/* Power down if completely idle for over 50ms */
3848 	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
3849 	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
3850 
3851 	ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
3852 	if (ret)
3853 		DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
3854 
3855 	ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
3856 	if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
3857 		DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
3858 				 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
3859 				 (pcu_mbox & 0xff) * 50);
3860 		dev_priv->rps.max_freq = pcu_mbox & 0xff;
3861 	}
3862 
3863 	dev_priv->rps.power = HIGH_POWER; /* force a reset */
3864 	gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3865 
3866 	gen6_enable_rps_interrupts(dev);
3867 
3868 	rc6vids = 0;
3869 	ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
3870 	if (IS_GEN6(dev) && ret) {
3871 		DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
3872 	} else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
3873 		DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
3874 			  GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
3875 		rc6vids &= 0xffff00;
3876 		rc6vids |= GEN6_ENCODE_RC6_VID(450);
3877 		ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
3878 		if (ret)
3879 			DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
3880 	}
3881 
3882 	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
3883 }
3884 
__gen6_update_ring_freq(struct drm_device * dev)3885 static void __gen6_update_ring_freq(struct drm_device *dev)
3886 {
3887 	struct drm_i915_private *dev_priv = dev->dev_private;
3888 	int min_freq = 15;
3889 	unsigned int gpu_freq;
3890 	unsigned int max_ia_freq, min_ring_freq;
3891 	int scaling_factor = 180;
3892 	struct cpufreq_policy *policy;
3893 
3894 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3895 
3896 	policy = cpufreq_cpu_get(0);
3897 	if (policy) {
3898 		max_ia_freq = policy->cpuinfo.max_freq;
3899 		cpufreq_cpu_put(policy);
3900 	} else {
3901 		/*
3902 		 * Default to measured freq if none found, PCU will ensure we
3903 		 * don't go over
3904 		 */
3905 		max_ia_freq = tsc_khz;
3906 	}
3907 
3908 	/* Convert from kHz to MHz */
3909 	max_ia_freq /= 1000;
3910 
3911 	min_ring_freq = I915_READ(DCLK) & 0xf;
3912 	/* convert DDR frequency from units of 266.6MHz to bandwidth */
3913 	min_ring_freq = mult_frac(min_ring_freq, 8, 3);
3914 
3915 	/*
3916 	 * For each potential GPU frequency, load a ring frequency we'd like
3917 	 * to use for memory access.  We do this by specifying the IA frequency
3918 	 * the PCU should use as a reference to determine the ring frequency.
3919 	 */
3920 	for (gpu_freq = dev_priv->rps.max_freq_softlimit; gpu_freq >= dev_priv->rps.min_freq_softlimit;
3921 	     gpu_freq--) {
3922 		int diff = dev_priv->rps.max_freq_softlimit - gpu_freq;
3923 		unsigned int ia_freq = 0, ring_freq = 0;
3924 
3925 		if (INTEL_INFO(dev)->gen >= 8) {
3926 			/* max(2 * GT, DDR). NB: GT is 50MHz units */
3927 			ring_freq = max(min_ring_freq, gpu_freq);
3928 		} else if (IS_HASWELL(dev)) {
3929 			ring_freq = mult_frac(gpu_freq, 5, 4);
3930 			ring_freq = max(min_ring_freq, ring_freq);
3931 			/* leave ia_freq as the default, chosen by cpufreq */
3932 		} else {
3933 			/* On older processors, there is no separate ring
3934 			 * clock domain, so in order to boost the bandwidth
3935 			 * of the ring, we need to upclock the CPU (ia_freq).
3936 			 *
3937 			 * For GPU frequencies less than 750MHz,
3938 			 * just use the lowest ring freq.
3939 			 */
3940 			if (gpu_freq < min_freq)
3941 				ia_freq = 800;
3942 			else
3943 				ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
3944 			ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
3945 		}
3946 
3947 		sandybridge_pcode_write(dev_priv,
3948 					GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
3949 					ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
3950 					ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
3951 					gpu_freq);
3952 	}
3953 }
3954 
gen6_update_ring_freq(struct drm_device * dev)3955 void gen6_update_ring_freq(struct drm_device *dev)
3956 {
3957 	struct drm_i915_private *dev_priv = dev->dev_private;
3958 
3959 	if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
3960 		return;
3961 
3962 	mutex_lock(&dev_priv->rps.hw_lock);
3963 	__gen6_update_ring_freq(dev);
3964 	mutex_unlock(&dev_priv->rps.hw_lock);
3965 }
3966 
cherryview_rps_max_freq(struct drm_i915_private * dev_priv)3967 static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
3968 {
3969 	u32 val, rp0;
3970 
3971 	val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
3972 	rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
3973 
3974 	return rp0;
3975 }
3976 
cherryview_rps_rpe_freq(struct drm_i915_private * dev_priv)3977 static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
3978 {
3979 	u32 val, rpe;
3980 
3981 	val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
3982 	rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
3983 
3984 	return rpe;
3985 }
3986 
cherryview_rps_guar_freq(struct drm_i915_private * dev_priv)3987 static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
3988 {
3989 	u32 val, rp1;
3990 
3991 	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
3992 	rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
3993 
3994 	return rp1;
3995 }
3996 
cherryview_rps_min_freq(struct drm_i915_private * dev_priv)3997 static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
3998 {
3999 	u32 val, rpn;
4000 
4001 	val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4002 	rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
4003 	return rpn;
4004 }
4005 
valleyview_rps_guar_freq(struct drm_i915_private * dev_priv)4006 static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4007 {
4008 	u32 val, rp1;
4009 
4010 	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4011 
4012 	rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4013 
4014 	return rp1;
4015 }
4016 
valleyview_rps_max_freq(struct drm_i915_private * dev_priv)4017 static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
4018 {
4019 	u32 val, rp0;
4020 
4021 	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4022 
4023 	rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4024 	/* Clamp to max */
4025 	rp0 = min_t(u32, rp0, 0xea);
4026 
4027 	return rp0;
4028 }
4029 
valleyview_rps_rpe_freq(struct drm_i915_private * dev_priv)4030 static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4031 {
4032 	u32 val, rpe;
4033 
4034 	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
4035 	rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
4036 	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
4037 	rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4038 
4039 	return rpe;
4040 }
4041 
valleyview_rps_min_freq(struct drm_i915_private * dev_priv)4042 static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
4043 {
4044 	return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
4045 }
4046 
4047 /* Check that the pctx buffer wasn't move under us. */
valleyview_check_pctx(struct drm_i915_private * dev_priv)4048 static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4049 {
4050 	unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4051 
4052 	WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4053 			     dev_priv->vlv_pctx->stolen->start);
4054 }
4055 
4056 
4057 /* Check that the pcbr address is not empty. */
cherryview_check_pctx(struct drm_i915_private * dev_priv)4058 static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4059 {
4060 	unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4061 
4062 	WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4063 }
4064 
cherryview_setup_pctx(struct drm_device * dev)4065 static void cherryview_setup_pctx(struct drm_device *dev)
4066 {
4067 	struct drm_i915_private *dev_priv = dev->dev_private;
4068 	unsigned long pctx_paddr, paddr;
4069 	struct i915_gtt *gtt = &dev_priv->gtt;
4070 	u32 pcbr;
4071 	int pctx_size = 32*1024;
4072 
4073 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4074 
4075 	pcbr = I915_READ(VLV_PCBR);
4076 	if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
4077 		paddr = (dev_priv->mm.stolen_base +
4078 			 (gtt->stolen_size - pctx_size));
4079 
4080 		pctx_paddr = (paddr & (~4095));
4081 		I915_WRITE(VLV_PCBR, pctx_paddr);
4082 	}
4083 }
4084 
valleyview_setup_pctx(struct drm_device * dev)4085 static void valleyview_setup_pctx(struct drm_device *dev)
4086 {
4087 	struct drm_i915_private *dev_priv = dev->dev_private;
4088 	struct drm_i915_gem_object *pctx;
4089 	unsigned long pctx_paddr;
4090 	u32 pcbr;
4091 	int pctx_size = 24*1024;
4092 
4093 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4094 
4095 	pcbr = I915_READ(VLV_PCBR);
4096 	if (pcbr) {
4097 		/* BIOS set it up already, grab the pre-alloc'd space */
4098 		int pcbr_offset;
4099 
4100 		pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4101 		pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4102 								      pcbr_offset,
4103 								      I915_GTT_OFFSET_NONE,
4104 								      pctx_size);
4105 		goto out;
4106 	}
4107 
4108 	/*
4109 	 * From the Gunit register HAS:
4110 	 * The Gfx driver is expected to program this register and ensure
4111 	 * proper allocation within Gfx stolen memory.  For example, this
4112 	 * register should be programmed such than the PCBR range does not
4113 	 * overlap with other ranges, such as the frame buffer, protected
4114 	 * memory, or any other relevant ranges.
4115 	 */
4116 	pctx = i915_gem_object_create_stolen(dev, pctx_size);
4117 	if (!pctx) {
4118 		DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4119 		return;
4120 	}
4121 
4122 	pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4123 	I915_WRITE(VLV_PCBR, pctx_paddr);
4124 
4125 out:
4126 	dev_priv->vlv_pctx = pctx;
4127 }
4128 
valleyview_cleanup_pctx(struct drm_device * dev)4129 static void valleyview_cleanup_pctx(struct drm_device *dev)
4130 {
4131 	struct drm_i915_private *dev_priv = dev->dev_private;
4132 
4133 	if (WARN_ON(!dev_priv->vlv_pctx))
4134 		return;
4135 
4136 	drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4137 	dev_priv->vlv_pctx = NULL;
4138 }
4139 
valleyview_init_gt_powersave(struct drm_device * dev)4140 static void valleyview_init_gt_powersave(struct drm_device *dev)
4141 {
4142 	struct drm_i915_private *dev_priv = dev->dev_private;
4143 	u32 val;
4144 
4145 	valleyview_setup_pctx(dev);
4146 
4147 	mutex_lock(&dev_priv->rps.hw_lock);
4148 
4149 	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4150 	switch ((val >> 6) & 3) {
4151 	case 0:
4152 	case 1:
4153 		dev_priv->mem_freq = 800;
4154 		break;
4155 	case 2:
4156 		dev_priv->mem_freq = 1066;
4157 		break;
4158 	case 3:
4159 		dev_priv->mem_freq = 1333;
4160 		break;
4161 	}
4162 	DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
4163 
4164 	dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4165 	dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4166 	DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4167 			 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4168 			 dev_priv->rps.max_freq);
4169 
4170 	dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4171 	DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4172 			 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4173 			 dev_priv->rps.efficient_freq);
4174 
4175 	dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4176 	DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
4177 			 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4178 			 dev_priv->rps.rp1_freq);
4179 
4180 	dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4181 	DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4182 			 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4183 			 dev_priv->rps.min_freq);
4184 
4185 	/* Preserve min/max settings in case of re-init */
4186 	if (dev_priv->rps.max_freq_softlimit == 0)
4187 		dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4188 
4189 	if (dev_priv->rps.min_freq_softlimit == 0)
4190 		dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4191 
4192 	mutex_unlock(&dev_priv->rps.hw_lock);
4193 }
4194 
cherryview_init_gt_powersave(struct drm_device * dev)4195 static void cherryview_init_gt_powersave(struct drm_device *dev)
4196 {
4197 	struct drm_i915_private *dev_priv = dev->dev_private;
4198 	u32 val;
4199 
4200 	cherryview_setup_pctx(dev);
4201 
4202 	mutex_lock(&dev_priv->rps.hw_lock);
4203 
4204 	val = vlv_punit_read(dev_priv, CCK_FUSE_REG);
4205 	switch ((val >> 2) & 0x7) {
4206 	case 0:
4207 	case 1:
4208 		dev_priv->rps.cz_freq = 200;
4209 		dev_priv->mem_freq = 1600;
4210 		break;
4211 	case 2:
4212 		dev_priv->rps.cz_freq = 267;
4213 		dev_priv->mem_freq = 1600;
4214 		break;
4215 	case 3:
4216 		dev_priv->rps.cz_freq = 333;
4217 		dev_priv->mem_freq = 2000;
4218 		break;
4219 	case 4:
4220 		dev_priv->rps.cz_freq = 320;
4221 		dev_priv->mem_freq = 1600;
4222 		break;
4223 	case 5:
4224 		dev_priv->rps.cz_freq = 400;
4225 		dev_priv->mem_freq = 1600;
4226 		break;
4227 	}
4228 	DRM_DEBUG_DRIVER("DDR speed: %d MHz", dev_priv->mem_freq);
4229 
4230 	dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4231 	dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4232 	DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
4233 			 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4234 			 dev_priv->rps.max_freq);
4235 
4236 	dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4237 	DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
4238 			 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4239 			 dev_priv->rps.efficient_freq);
4240 
4241 	dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4242 	DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
4243 			 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
4244 			 dev_priv->rps.rp1_freq);
4245 
4246 	dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4247 	DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
4248 			 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4249 			 dev_priv->rps.min_freq);
4250 
4251 	WARN_ONCE((dev_priv->rps.max_freq |
4252 		   dev_priv->rps.efficient_freq |
4253 		   dev_priv->rps.rp1_freq |
4254 		   dev_priv->rps.min_freq) & 1,
4255 		  "Odd GPU freq values\n");
4256 
4257 	/* Preserve min/max settings in case of re-init */
4258 	if (dev_priv->rps.max_freq_softlimit == 0)
4259 		dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4260 
4261 	if (dev_priv->rps.min_freq_softlimit == 0)
4262 		dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4263 
4264 	mutex_unlock(&dev_priv->rps.hw_lock);
4265 }
4266 
valleyview_cleanup_gt_powersave(struct drm_device * dev)4267 static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4268 {
4269 	valleyview_cleanup_pctx(dev);
4270 }
4271 
cherryview_enable_rps(struct drm_device * dev)4272 static void cherryview_enable_rps(struct drm_device *dev)
4273 {
4274 	struct drm_i915_private *dev_priv = dev->dev_private;
4275 	struct intel_engine_cs *ring;
4276 	u32 gtfifodbg, val, rc6_mode = 0, pcbr;
4277 	int i;
4278 
4279 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4280 
4281 	gtfifodbg = I915_READ(GTFIFODBG);
4282 	if (gtfifodbg) {
4283 		DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4284 				 gtfifodbg);
4285 		I915_WRITE(GTFIFODBG, gtfifodbg);
4286 	}
4287 
4288 	cherryview_check_pctx(dev_priv);
4289 
4290 	/* 1a & 1b: Get forcewake during program sequence. Although the driver
4291 	 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4292 	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4293 
4294 	/* 2a: Program RC6 thresholds.*/
4295 	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4296 	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4297 	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4298 
4299 	for_each_ring(ring, dev_priv, i)
4300 		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4301 	I915_WRITE(GEN6_RC_SLEEP, 0);
4302 
4303 	I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4304 
4305 	/* allows RC6 residency counter to work */
4306 	I915_WRITE(VLV_COUNTER_CONTROL,
4307 		   _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4308 				      VLV_MEDIA_RC6_COUNT_EN |
4309 				      VLV_RENDER_RC6_COUNT_EN));
4310 
4311 	/* For now we assume BIOS is allocating and populating the PCBR  */
4312 	pcbr = I915_READ(VLV_PCBR);
4313 
4314 	DRM_DEBUG_DRIVER("PCBR offset : 0x%x\n", pcbr);
4315 
4316 	/* 3: Enable RC6 */
4317 	if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4318 						(pcbr >> VLV_PCBR_ADDR_SHIFT))
4319 		rc6_mode = GEN6_RC_CTL_EI_MODE(1);
4320 
4321 	I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4322 
4323 	/* 4 Program defaults and thresholds for RPS*/
4324 	I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4325 	I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4326 	I915_WRITE(GEN6_RP_UP_EI, 66000);
4327 	I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4328 
4329 	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4330 
4331 	/* WaDisablePwrmtrEvent:chv (pre-production hw) */
4332 	I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
4333 	I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
4334 
4335 	/* 5: Enable RPS */
4336 	I915_WRITE(GEN6_RP_CONTROL,
4337 		   GEN6_RP_MEDIA_HW_NORMAL_MODE |
4338 		   GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
4339 		   GEN6_RP_ENABLE |
4340 		   GEN6_RP_UP_BUSY_AVG |
4341 		   GEN6_RP_DOWN_IDLE_AVG);
4342 
4343 	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4344 
4345 	DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4346 	DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4347 
4348 	dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4349 	DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4350 			 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4351 			 dev_priv->rps.cur_freq);
4352 
4353 	DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4354 			 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4355 			 dev_priv->rps.efficient_freq);
4356 
4357 	valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4358 
4359 	gen8_enable_rps_interrupts(dev);
4360 
4361 	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4362 }
4363 
valleyview_enable_rps(struct drm_device * dev)4364 static void valleyview_enable_rps(struct drm_device *dev)
4365 {
4366 	struct drm_i915_private *dev_priv = dev->dev_private;
4367 	struct intel_engine_cs *ring;
4368 	u32 gtfifodbg, val, rc6_mode = 0;
4369 	int i;
4370 
4371 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4372 
4373 	valleyview_check_pctx(dev_priv);
4374 
4375 	if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4376 		DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4377 				 gtfifodbg);
4378 		I915_WRITE(GTFIFODBG, gtfifodbg);
4379 	}
4380 
4381 	/* If VLV, Forcewake all wells, else re-direct to regular path */
4382 	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4383 
4384 	I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4385 	I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4386 	I915_WRITE(GEN6_RP_UP_EI, 66000);
4387 	I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4388 
4389 	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4390 	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4391 
4392 	I915_WRITE(GEN6_RP_CONTROL,
4393 		   GEN6_RP_MEDIA_TURBO |
4394 		   GEN6_RP_MEDIA_HW_NORMAL_MODE |
4395 		   GEN6_RP_MEDIA_IS_GFX |
4396 		   GEN6_RP_ENABLE |
4397 		   GEN6_RP_UP_BUSY_AVG |
4398 		   GEN6_RP_DOWN_IDLE_CONT);
4399 
4400 	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4401 	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4402 	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4403 
4404 	for_each_ring(ring, dev_priv, i)
4405 		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4406 
4407 	I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
4408 
4409 	/* allows RC6 residency counter to work */
4410 	I915_WRITE(VLV_COUNTER_CONTROL,
4411 		   _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4412 				      VLV_RENDER_RC0_COUNT_EN |
4413 				      VLV_MEDIA_RC6_COUNT_EN |
4414 				      VLV_RENDER_RC6_COUNT_EN));
4415 
4416 	if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4417 		rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
4418 
4419 	intel_print_rc6_info(dev, rc6_mode);
4420 
4421 	I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4422 
4423 	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4424 
4425 	DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & 0x10 ? "yes" : "no");
4426 	DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4427 
4428 	dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4429 	DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
4430 			 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
4431 			 dev_priv->rps.cur_freq);
4432 
4433 	DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
4434 			 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4435 			 dev_priv->rps.efficient_freq);
4436 
4437 	valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4438 
4439 	gen6_enable_rps_interrupts(dev);
4440 
4441 	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4442 }
4443 
ironlake_teardown_rc6(struct drm_device * dev)4444 void ironlake_teardown_rc6(struct drm_device *dev)
4445 {
4446 	struct drm_i915_private *dev_priv = dev->dev_private;
4447 
4448 	if (dev_priv->ips.renderctx) {
4449 		i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
4450 		drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4451 		dev_priv->ips.renderctx = NULL;
4452 	}
4453 
4454 	if (dev_priv->ips.pwrctx) {
4455 		i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
4456 		drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4457 		dev_priv->ips.pwrctx = NULL;
4458 	}
4459 }
4460 
ironlake_disable_rc6(struct drm_device * dev)4461 static void ironlake_disable_rc6(struct drm_device *dev)
4462 {
4463 	struct drm_i915_private *dev_priv = dev->dev_private;
4464 
4465 	if (I915_READ(PWRCTXA)) {
4466 		/* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4467 		I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4468 		wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4469 			 50);
4470 
4471 		I915_WRITE(PWRCTXA, 0);
4472 		POSTING_READ(PWRCTXA);
4473 
4474 		I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4475 		POSTING_READ(RSTDBYCTL);
4476 	}
4477 }
4478 
ironlake_setup_rc6(struct drm_device * dev)4479 static int ironlake_setup_rc6(struct drm_device *dev)
4480 {
4481 	struct drm_i915_private *dev_priv = dev->dev_private;
4482 
4483 	if (dev_priv->ips.renderctx == NULL)
4484 		dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4485 	if (!dev_priv->ips.renderctx)
4486 		return -ENOMEM;
4487 
4488 	if (dev_priv->ips.pwrctx == NULL)
4489 		dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4490 	if (!dev_priv->ips.pwrctx) {
4491 		ironlake_teardown_rc6(dev);
4492 		return -ENOMEM;
4493 	}
4494 
4495 	return 0;
4496 }
4497 
ironlake_enable_rc6(struct drm_device * dev)4498 static void ironlake_enable_rc6(struct drm_device *dev)
4499 {
4500 	struct drm_i915_private *dev_priv = dev->dev_private;
4501 	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
4502 	bool was_interruptible;
4503 	int ret;
4504 
4505 	/* rc6 disabled by default due to repeated reports of hanging during
4506 	 * boot and resume.
4507 	 */
4508 	if (!intel_enable_rc6(dev))
4509 		return;
4510 
4511 	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4512 
4513 	ret = ironlake_setup_rc6(dev);
4514 	if (ret)
4515 		return;
4516 
4517 	was_interruptible = dev_priv->mm.interruptible;
4518 	dev_priv->mm.interruptible = false;
4519 
4520 	/*
4521 	 * GPU can automatically power down the render unit if given a page
4522 	 * to save state.
4523 	 */
4524 	ret = intel_ring_begin(ring, 6);
4525 	if (ret) {
4526 		ironlake_teardown_rc6(dev);
4527 		dev_priv->mm.interruptible = was_interruptible;
4528 		return;
4529 	}
4530 
4531 	intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4532 	intel_ring_emit(ring, MI_SET_CONTEXT);
4533 	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
4534 			MI_MM_SPACE_GTT |
4535 			MI_SAVE_EXT_STATE_EN |
4536 			MI_RESTORE_EXT_STATE_EN |
4537 			MI_RESTORE_INHIBIT);
4538 	intel_ring_emit(ring, MI_SUSPEND_FLUSH);
4539 	intel_ring_emit(ring, MI_NOOP);
4540 	intel_ring_emit(ring, MI_FLUSH);
4541 	intel_ring_advance(ring);
4542 
4543 	/*
4544 	 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
4545 	 * does an implicit flush, combined with MI_FLUSH above, it should be
4546 	 * safe to assume that renderctx is valid
4547 	 */
4548 	ret = intel_ring_idle(ring);
4549 	dev_priv->mm.interruptible = was_interruptible;
4550 	if (ret) {
4551 		DRM_ERROR("failed to enable ironlake power savings\n");
4552 		ironlake_teardown_rc6(dev);
4553 		return;
4554 	}
4555 
4556 	I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
4557 	I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4558 
4559 	intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
4560 }
4561 
intel_pxfreq(u32 vidfreq)4562 static unsigned long intel_pxfreq(u32 vidfreq)
4563 {
4564 	unsigned long freq;
4565 	int div = (vidfreq & 0x3f0000) >> 16;
4566 	int post = (vidfreq & 0x3000) >> 12;
4567 	int pre = (vidfreq & 0x7);
4568 
4569 	if (!pre)
4570 		return 0;
4571 
4572 	freq = ((div * 133333) / ((1<<post) * pre));
4573 
4574 	return freq;
4575 }
4576 
4577 static const struct cparams {
4578 	u16 i;
4579 	u16 t;
4580 	u16 m;
4581 	u16 c;
4582 } cparams[] = {
4583 	{ 1, 1333, 301, 28664 },
4584 	{ 1, 1066, 294, 24460 },
4585 	{ 1, 800, 294, 25192 },
4586 	{ 0, 1333, 276, 27605 },
4587 	{ 0, 1066, 276, 27605 },
4588 	{ 0, 800, 231, 23784 },
4589 };
4590 
__i915_chipset_val(struct drm_i915_private * dev_priv)4591 static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
4592 {
4593 	u64 total_count, diff, ret;
4594 	u32 count1, count2, count3, m = 0, c = 0;
4595 	unsigned long now = jiffies_to_msecs(jiffies), diff1;
4596 	int i;
4597 
4598 	assert_spin_locked(&mchdev_lock);
4599 
4600 	diff1 = now - dev_priv->ips.last_time1;
4601 
4602 	/* Prevent division-by-zero if we are asking too fast.
4603 	 * Also, we don't get interesting results if we are polling
4604 	 * faster than once in 10ms, so just return the saved value
4605 	 * in such cases.
4606 	 */
4607 	if (diff1 <= 10)
4608 		return dev_priv->ips.chipset_power;
4609 
4610 	count1 = I915_READ(DMIEC);
4611 	count2 = I915_READ(DDREC);
4612 	count3 = I915_READ(CSIEC);
4613 
4614 	total_count = count1 + count2 + count3;
4615 
4616 	/* FIXME: handle per-counter overflow */
4617 	if (total_count < dev_priv->ips.last_count1) {
4618 		diff = ~0UL - dev_priv->ips.last_count1;
4619 		diff += total_count;
4620 	} else {
4621 		diff = total_count - dev_priv->ips.last_count1;
4622 	}
4623 
4624 	for (i = 0; i < ARRAY_SIZE(cparams); i++) {
4625 		if (cparams[i].i == dev_priv->ips.c_m &&
4626 		    cparams[i].t == dev_priv->ips.r_t) {
4627 			m = cparams[i].m;
4628 			c = cparams[i].c;
4629 			break;
4630 		}
4631 	}
4632 
4633 	diff = div_u64(diff, diff1);
4634 	ret = ((m * diff) + c);
4635 	ret = div_u64(ret, 10);
4636 
4637 	dev_priv->ips.last_count1 = total_count;
4638 	dev_priv->ips.last_time1 = now;
4639 
4640 	dev_priv->ips.chipset_power = ret;
4641 
4642 	return ret;
4643 }
4644 
i915_chipset_val(struct drm_i915_private * dev_priv)4645 unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
4646 {
4647 	struct drm_device *dev = dev_priv->dev;
4648 	unsigned long val;
4649 
4650 	if (INTEL_INFO(dev)->gen != 5)
4651 		return 0;
4652 
4653 	spin_lock_irq(&mchdev_lock);
4654 
4655 	val = __i915_chipset_val(dev_priv);
4656 
4657 	spin_unlock_irq(&mchdev_lock);
4658 
4659 	return val;
4660 }
4661 
i915_mch_val(struct drm_i915_private * dev_priv)4662 unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
4663 {
4664 	unsigned long m, x, b;
4665 	u32 tsfs;
4666 
4667 	tsfs = I915_READ(TSFS);
4668 
4669 	m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
4670 	x = I915_READ8(TR1);
4671 
4672 	b = tsfs & TSFS_INTR_MASK;
4673 
4674 	return ((m * x) / 127) - b;
4675 }
4676 
pvid_to_extvid(struct drm_i915_private * dev_priv,u8 pxvid)4677 static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
4678 {
4679 	struct drm_device *dev = dev_priv->dev;
4680 	static const struct v_table {
4681 		u16 vd; /* in .1 mil */
4682 		u16 vm; /* in .1 mil */
4683 	} v_table[] = {
4684 		{ 0, 0, },
4685 		{ 375, 0, },
4686 		{ 500, 0, },
4687 		{ 625, 0, },
4688 		{ 750, 0, },
4689 		{ 875, 0, },
4690 		{ 1000, 0, },
4691 		{ 1125, 0, },
4692 		{ 4125, 3000, },
4693 		{ 4125, 3000, },
4694 		{ 4125, 3000, },
4695 		{ 4125, 3000, },
4696 		{ 4125, 3000, },
4697 		{ 4125, 3000, },
4698 		{ 4125, 3000, },
4699 		{ 4125, 3000, },
4700 		{ 4125, 3000, },
4701 		{ 4125, 3000, },
4702 		{ 4125, 3000, },
4703 		{ 4125, 3000, },
4704 		{ 4125, 3000, },
4705 		{ 4125, 3000, },
4706 		{ 4125, 3000, },
4707 		{ 4125, 3000, },
4708 		{ 4125, 3000, },
4709 		{ 4125, 3000, },
4710 		{ 4125, 3000, },
4711 		{ 4125, 3000, },
4712 		{ 4125, 3000, },
4713 		{ 4125, 3000, },
4714 		{ 4125, 3000, },
4715 		{ 4125, 3000, },
4716 		{ 4250, 3125, },
4717 		{ 4375, 3250, },
4718 		{ 4500, 3375, },
4719 		{ 4625, 3500, },
4720 		{ 4750, 3625, },
4721 		{ 4875, 3750, },
4722 		{ 5000, 3875, },
4723 		{ 5125, 4000, },
4724 		{ 5250, 4125, },
4725 		{ 5375, 4250, },
4726 		{ 5500, 4375, },
4727 		{ 5625, 4500, },
4728 		{ 5750, 4625, },
4729 		{ 5875, 4750, },
4730 		{ 6000, 4875, },
4731 		{ 6125, 5000, },
4732 		{ 6250, 5125, },
4733 		{ 6375, 5250, },
4734 		{ 6500, 5375, },
4735 		{ 6625, 5500, },
4736 		{ 6750, 5625, },
4737 		{ 6875, 5750, },
4738 		{ 7000, 5875, },
4739 		{ 7125, 6000, },
4740 		{ 7250, 6125, },
4741 		{ 7375, 6250, },
4742 		{ 7500, 6375, },
4743 		{ 7625, 6500, },
4744 		{ 7750, 6625, },
4745 		{ 7875, 6750, },
4746 		{ 8000, 6875, },
4747 		{ 8125, 7000, },
4748 		{ 8250, 7125, },
4749 		{ 8375, 7250, },
4750 		{ 8500, 7375, },
4751 		{ 8625, 7500, },
4752 		{ 8750, 7625, },
4753 		{ 8875, 7750, },
4754 		{ 9000, 7875, },
4755 		{ 9125, 8000, },
4756 		{ 9250, 8125, },
4757 		{ 9375, 8250, },
4758 		{ 9500, 8375, },
4759 		{ 9625, 8500, },
4760 		{ 9750, 8625, },
4761 		{ 9875, 8750, },
4762 		{ 10000, 8875, },
4763 		{ 10125, 9000, },
4764 		{ 10250, 9125, },
4765 		{ 10375, 9250, },
4766 		{ 10500, 9375, },
4767 		{ 10625, 9500, },
4768 		{ 10750, 9625, },
4769 		{ 10875, 9750, },
4770 		{ 11000, 9875, },
4771 		{ 11125, 10000, },
4772 		{ 11250, 10125, },
4773 		{ 11375, 10250, },
4774 		{ 11500, 10375, },
4775 		{ 11625, 10500, },
4776 		{ 11750, 10625, },
4777 		{ 11875, 10750, },
4778 		{ 12000, 10875, },
4779 		{ 12125, 11000, },
4780 		{ 12250, 11125, },
4781 		{ 12375, 11250, },
4782 		{ 12500, 11375, },
4783 		{ 12625, 11500, },
4784 		{ 12750, 11625, },
4785 		{ 12875, 11750, },
4786 		{ 13000, 11875, },
4787 		{ 13125, 12000, },
4788 		{ 13250, 12125, },
4789 		{ 13375, 12250, },
4790 		{ 13500, 12375, },
4791 		{ 13625, 12500, },
4792 		{ 13750, 12625, },
4793 		{ 13875, 12750, },
4794 		{ 14000, 12875, },
4795 		{ 14125, 13000, },
4796 		{ 14250, 13125, },
4797 		{ 14375, 13250, },
4798 		{ 14500, 13375, },
4799 		{ 14625, 13500, },
4800 		{ 14750, 13625, },
4801 		{ 14875, 13750, },
4802 		{ 15000, 13875, },
4803 		{ 15125, 14000, },
4804 		{ 15250, 14125, },
4805 		{ 15375, 14250, },
4806 		{ 15500, 14375, },
4807 		{ 15625, 14500, },
4808 		{ 15750, 14625, },
4809 		{ 15875, 14750, },
4810 		{ 16000, 14875, },
4811 		{ 16125, 15000, },
4812 	};
4813 	if (INTEL_INFO(dev)->is_mobile)
4814 		return v_table[pxvid].vm;
4815 	else
4816 		return v_table[pxvid].vd;
4817 }
4818 
__i915_update_gfx_val(struct drm_i915_private * dev_priv)4819 static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
4820 {
4821 	u64 now, diff, diffms;
4822 	u32 count;
4823 
4824 	assert_spin_locked(&mchdev_lock);
4825 
4826 	now = ktime_get_raw_ns();
4827 	diffms = now - dev_priv->ips.last_time2;
4828 	do_div(diffms, NSEC_PER_MSEC);
4829 
4830 	/* Don't divide by 0 */
4831 	if (!diffms)
4832 		return;
4833 
4834 	count = I915_READ(GFXEC);
4835 
4836 	if (count < dev_priv->ips.last_count2) {
4837 		diff = ~0UL - dev_priv->ips.last_count2;
4838 		diff += count;
4839 	} else {
4840 		diff = count - dev_priv->ips.last_count2;
4841 	}
4842 
4843 	dev_priv->ips.last_count2 = count;
4844 	dev_priv->ips.last_time2 = now;
4845 
4846 	/* More magic constants... */
4847 	diff = diff * 1181;
4848 	diff = div_u64(diff, diffms * 10);
4849 	dev_priv->ips.gfx_power = diff;
4850 }
4851 
i915_update_gfx_val(struct drm_i915_private * dev_priv)4852 void i915_update_gfx_val(struct drm_i915_private *dev_priv)
4853 {
4854 	struct drm_device *dev = dev_priv->dev;
4855 
4856 	if (INTEL_INFO(dev)->gen != 5)
4857 		return;
4858 
4859 	spin_lock_irq(&mchdev_lock);
4860 
4861 	__i915_update_gfx_val(dev_priv);
4862 
4863 	spin_unlock_irq(&mchdev_lock);
4864 }
4865 
__i915_gfx_val(struct drm_i915_private * dev_priv)4866 static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
4867 {
4868 	unsigned long t, corr, state1, corr2, state2;
4869 	u32 pxvid, ext_v;
4870 
4871 	assert_spin_locked(&mchdev_lock);
4872 
4873 	pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
4874 	pxvid = (pxvid >> 24) & 0x7f;
4875 	ext_v = pvid_to_extvid(dev_priv, pxvid);
4876 
4877 	state1 = ext_v;
4878 
4879 	t = i915_mch_val(dev_priv);
4880 
4881 	/* Revel in the empirically derived constants */
4882 
4883 	/* Correction factor in 1/100000 units */
4884 	if (t > 80)
4885 		corr = ((t * 2349) + 135940);
4886 	else if (t >= 50)
4887 		corr = ((t * 964) + 29317);
4888 	else /* < 50 */
4889 		corr = ((t * 301) + 1004);
4890 
4891 	corr = corr * ((150142 * state1) / 10000 - 78642);
4892 	corr /= 100000;
4893 	corr2 = (corr * dev_priv->ips.corr);
4894 
4895 	state2 = (corr2 * state1) / 10000;
4896 	state2 /= 100; /* convert to mW */
4897 
4898 	__i915_update_gfx_val(dev_priv);
4899 
4900 	return dev_priv->ips.gfx_power + state2;
4901 }
4902 
i915_gfx_val(struct drm_i915_private * dev_priv)4903 unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
4904 {
4905 	struct drm_device *dev = dev_priv->dev;
4906 	unsigned long val;
4907 
4908 	if (INTEL_INFO(dev)->gen != 5)
4909 		return 0;
4910 
4911 	spin_lock_irq(&mchdev_lock);
4912 
4913 	val = __i915_gfx_val(dev_priv);
4914 
4915 	spin_unlock_irq(&mchdev_lock);
4916 
4917 	return val;
4918 }
4919 
4920 /**
4921  * i915_read_mch_val - return value for IPS use
4922  *
4923  * Calculate and return a value for the IPS driver to use when deciding whether
4924  * we have thermal and power headroom to increase CPU or GPU power budget.
4925  */
i915_read_mch_val(void)4926 unsigned long i915_read_mch_val(void)
4927 {
4928 	struct drm_i915_private *dev_priv;
4929 	unsigned long chipset_val, graphics_val, ret = 0;
4930 
4931 	spin_lock_irq(&mchdev_lock);
4932 	if (!i915_mch_dev)
4933 		goto out_unlock;
4934 	dev_priv = i915_mch_dev;
4935 
4936 	chipset_val = __i915_chipset_val(dev_priv);
4937 	graphics_val = __i915_gfx_val(dev_priv);
4938 
4939 	ret = chipset_val + graphics_val;
4940 
4941 out_unlock:
4942 	spin_unlock_irq(&mchdev_lock);
4943 
4944 	return ret;
4945 }
4946 EXPORT_SYMBOL_GPL(i915_read_mch_val);
4947 
4948 /**
4949  * i915_gpu_raise - raise GPU frequency limit
4950  *
4951  * Raise the limit; IPS indicates we have thermal headroom.
4952  */
i915_gpu_raise(void)4953 bool i915_gpu_raise(void)
4954 {
4955 	struct drm_i915_private *dev_priv;
4956 	bool ret = true;
4957 
4958 	spin_lock_irq(&mchdev_lock);
4959 	if (!i915_mch_dev) {
4960 		ret = false;
4961 		goto out_unlock;
4962 	}
4963 	dev_priv = i915_mch_dev;
4964 
4965 	if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
4966 		dev_priv->ips.max_delay--;
4967 
4968 out_unlock:
4969 	spin_unlock_irq(&mchdev_lock);
4970 
4971 	return ret;
4972 }
4973 EXPORT_SYMBOL_GPL(i915_gpu_raise);
4974 
4975 /**
4976  * i915_gpu_lower - lower GPU frequency limit
4977  *
4978  * IPS indicates we're close to a thermal limit, so throttle back the GPU
4979  * frequency maximum.
4980  */
i915_gpu_lower(void)4981 bool i915_gpu_lower(void)
4982 {
4983 	struct drm_i915_private *dev_priv;
4984 	bool ret = true;
4985 
4986 	spin_lock_irq(&mchdev_lock);
4987 	if (!i915_mch_dev) {
4988 		ret = false;
4989 		goto out_unlock;
4990 	}
4991 	dev_priv = i915_mch_dev;
4992 
4993 	if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
4994 		dev_priv->ips.max_delay++;
4995 
4996 out_unlock:
4997 	spin_unlock_irq(&mchdev_lock);
4998 
4999 	return ret;
5000 }
5001 EXPORT_SYMBOL_GPL(i915_gpu_lower);
5002 
5003 /**
5004  * i915_gpu_busy - indicate GPU business to IPS
5005  *
5006  * Tell the IPS driver whether or not the GPU is busy.
5007  */
i915_gpu_busy(void)5008 bool i915_gpu_busy(void)
5009 {
5010 	struct drm_i915_private *dev_priv;
5011 	struct intel_engine_cs *ring;
5012 	bool ret = false;
5013 	int i;
5014 
5015 	spin_lock_irq(&mchdev_lock);
5016 	if (!i915_mch_dev)
5017 		goto out_unlock;
5018 	dev_priv = i915_mch_dev;
5019 
5020 	for_each_ring(ring, dev_priv, i)
5021 		ret |= !list_empty(&ring->request_list);
5022 
5023 out_unlock:
5024 	spin_unlock_irq(&mchdev_lock);
5025 
5026 	return ret;
5027 }
5028 EXPORT_SYMBOL_GPL(i915_gpu_busy);
5029 
5030 /**
5031  * i915_gpu_turbo_disable - disable graphics turbo
5032  *
5033  * Disable graphics turbo by resetting the max frequency and setting the
5034  * current frequency to the default.
5035  */
i915_gpu_turbo_disable(void)5036 bool i915_gpu_turbo_disable(void)
5037 {
5038 	struct drm_i915_private *dev_priv;
5039 	bool ret = true;
5040 
5041 	spin_lock_irq(&mchdev_lock);
5042 	if (!i915_mch_dev) {
5043 		ret = false;
5044 		goto out_unlock;
5045 	}
5046 	dev_priv = i915_mch_dev;
5047 
5048 	dev_priv->ips.max_delay = dev_priv->ips.fstart;
5049 
5050 	if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
5051 		ret = false;
5052 
5053 out_unlock:
5054 	spin_unlock_irq(&mchdev_lock);
5055 
5056 	return ret;
5057 }
5058 EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5059 
5060 /**
5061  * Tells the intel_ips driver that the i915 driver is now loaded, if
5062  * IPS got loaded first.
5063  *
5064  * This awkward dance is so that neither module has to depend on the
5065  * other in order for IPS to do the appropriate communication of
5066  * GPU turbo limits to i915.
5067  */
5068 static void
ips_ping_for_i915_load(void)5069 ips_ping_for_i915_load(void)
5070 {
5071 	void (*link)(void);
5072 
5073 	link = symbol_get(ips_link_to_i915_driver);
5074 	if (link) {
5075 		link();
5076 		symbol_put(ips_link_to_i915_driver);
5077 	}
5078 }
5079 
intel_gpu_ips_init(struct drm_i915_private * dev_priv)5080 void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5081 {
5082 	/* We only register the i915 ips part with intel-ips once everything is
5083 	 * set up, to avoid intel-ips sneaking in and reading bogus values. */
5084 	spin_lock_irq(&mchdev_lock);
5085 	i915_mch_dev = dev_priv;
5086 	spin_unlock_irq(&mchdev_lock);
5087 
5088 	ips_ping_for_i915_load();
5089 }
5090 
intel_gpu_ips_teardown(void)5091 void intel_gpu_ips_teardown(void)
5092 {
5093 	spin_lock_irq(&mchdev_lock);
5094 	i915_mch_dev = NULL;
5095 	spin_unlock_irq(&mchdev_lock);
5096 }
5097 
intel_init_emon(struct drm_device * dev)5098 static void intel_init_emon(struct drm_device *dev)
5099 {
5100 	struct drm_i915_private *dev_priv = dev->dev_private;
5101 	u32 lcfuse;
5102 	u8 pxw[16];
5103 	int i;
5104 
5105 	/* Disable to program */
5106 	I915_WRITE(ECR, 0);
5107 	POSTING_READ(ECR);
5108 
5109 	/* Program energy weights for various events */
5110 	I915_WRITE(SDEW, 0x15040d00);
5111 	I915_WRITE(CSIEW0, 0x007f0000);
5112 	I915_WRITE(CSIEW1, 0x1e220004);
5113 	I915_WRITE(CSIEW2, 0x04000004);
5114 
5115 	for (i = 0; i < 5; i++)
5116 		I915_WRITE(PEW + (i * 4), 0);
5117 	for (i = 0; i < 3; i++)
5118 		I915_WRITE(DEW + (i * 4), 0);
5119 
5120 	/* Program P-state weights to account for frequency power adjustment */
5121 	for (i = 0; i < 16; i++) {
5122 		u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5123 		unsigned long freq = intel_pxfreq(pxvidfreq);
5124 		unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5125 			PXVFREQ_PX_SHIFT;
5126 		unsigned long val;
5127 
5128 		val = vid * vid;
5129 		val *= (freq / 1000);
5130 		val *= 255;
5131 		val /= (127*127*900);
5132 		if (val > 0xff)
5133 			DRM_ERROR("bad pxval: %ld\n", val);
5134 		pxw[i] = val;
5135 	}
5136 	/* Render standby states get 0 weight */
5137 	pxw[14] = 0;
5138 	pxw[15] = 0;
5139 
5140 	for (i = 0; i < 4; i++) {
5141 		u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5142 			(pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5143 		I915_WRITE(PXW + (i * 4), val);
5144 	}
5145 
5146 	/* Adjust magic regs to magic values (more experimental results) */
5147 	I915_WRITE(OGW0, 0);
5148 	I915_WRITE(OGW1, 0);
5149 	I915_WRITE(EG0, 0x00007f00);
5150 	I915_WRITE(EG1, 0x0000000e);
5151 	I915_WRITE(EG2, 0x000e0000);
5152 	I915_WRITE(EG3, 0x68000300);
5153 	I915_WRITE(EG4, 0x42000000);
5154 	I915_WRITE(EG5, 0x00140031);
5155 	I915_WRITE(EG6, 0);
5156 	I915_WRITE(EG7, 0);
5157 
5158 	for (i = 0; i < 8; i++)
5159 		I915_WRITE(PXWL + (i * 4), 0);
5160 
5161 	/* Enable PMON + select events */
5162 	I915_WRITE(ECR, 0x80000019);
5163 
5164 	lcfuse = I915_READ(LCFUSE02);
5165 
5166 	dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
5167 }
5168 
intel_init_gt_powersave(struct drm_device * dev)5169 void intel_init_gt_powersave(struct drm_device *dev)
5170 {
5171 	i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5172 
5173 	if (IS_CHERRYVIEW(dev))
5174 		cherryview_init_gt_powersave(dev);
5175 	else if (IS_VALLEYVIEW(dev))
5176 		valleyview_init_gt_powersave(dev);
5177 }
5178 
intel_cleanup_gt_powersave(struct drm_device * dev)5179 void intel_cleanup_gt_powersave(struct drm_device *dev)
5180 {
5181 	if (IS_CHERRYVIEW(dev))
5182 		return;
5183 	else if (IS_VALLEYVIEW(dev))
5184 		valleyview_cleanup_gt_powersave(dev);
5185 }
5186 
5187 /**
5188  * intel_suspend_gt_powersave - suspend PM work and helper threads
5189  * @dev: drm device
5190  *
5191  * We don't want to disable RC6 or other features here, we just want
5192  * to make sure any work we've queued has finished and won't bother
5193  * us while we're suspended.
5194  */
intel_suspend_gt_powersave(struct drm_device * dev)5195 void intel_suspend_gt_powersave(struct drm_device *dev)
5196 {
5197 	struct drm_i915_private *dev_priv = dev->dev_private;
5198 
5199 	/* Interrupts should be disabled already to avoid re-arming. */
5200 	WARN_ON(intel_irqs_enabled(dev_priv));
5201 
5202 	flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5203 
5204 	cancel_work_sync(&dev_priv->rps.work);
5205 
5206 	/* Force GPU to min freq during suspend */
5207 	gen6_rps_idle(dev_priv);
5208 }
5209 
intel_disable_gt_powersave(struct drm_device * dev)5210 void intel_disable_gt_powersave(struct drm_device *dev)
5211 {
5212 	struct drm_i915_private *dev_priv = dev->dev_private;
5213 
5214 	/* Interrupts should be disabled already to avoid re-arming. */
5215 	WARN_ON(intel_irqs_enabled(dev_priv));
5216 
5217 	if (IS_IRONLAKE_M(dev)) {
5218 		ironlake_disable_drps(dev);
5219 		ironlake_disable_rc6(dev);
5220 	} else if (INTEL_INFO(dev)->gen >= 6) {
5221 		intel_suspend_gt_powersave(dev);
5222 
5223 		mutex_lock(&dev_priv->rps.hw_lock);
5224 		if (IS_CHERRYVIEW(dev))
5225 			cherryview_disable_rps(dev);
5226 		else if (IS_VALLEYVIEW(dev))
5227 			valleyview_disable_rps(dev);
5228 		else
5229 			gen6_disable_rps(dev);
5230 		dev_priv->rps.enabled = false;
5231 		mutex_unlock(&dev_priv->rps.hw_lock);
5232 	}
5233 }
5234 
intel_gen6_powersave_work(struct work_struct * work)5235 static void intel_gen6_powersave_work(struct work_struct *work)
5236 {
5237 	struct drm_i915_private *dev_priv =
5238 		container_of(work, struct drm_i915_private,
5239 			     rps.delayed_resume_work.work);
5240 	struct drm_device *dev = dev_priv->dev;
5241 
5242 	mutex_lock(&dev_priv->rps.hw_lock);
5243 
5244 	if (IS_CHERRYVIEW(dev)) {
5245 		cherryview_enable_rps(dev);
5246 	} else if (IS_VALLEYVIEW(dev)) {
5247 		valleyview_enable_rps(dev);
5248 	} else if (IS_BROADWELL(dev)) {
5249 		gen8_enable_rps(dev);
5250 		__gen6_update_ring_freq(dev);
5251 	} else {
5252 		gen6_enable_rps(dev);
5253 		__gen6_update_ring_freq(dev);
5254 	}
5255 	dev_priv->rps.enabled = true;
5256 	mutex_unlock(&dev_priv->rps.hw_lock);
5257 
5258 	intel_runtime_pm_put(dev_priv);
5259 }
5260 
intel_enable_gt_powersave(struct drm_device * dev)5261 void intel_enable_gt_powersave(struct drm_device *dev)
5262 {
5263 	struct drm_i915_private *dev_priv = dev->dev_private;
5264 
5265 	if (IS_IRONLAKE_M(dev)) {
5266 		mutex_lock(&dev->struct_mutex);
5267 		ironlake_enable_drps(dev);
5268 		ironlake_enable_rc6(dev);
5269 		intel_init_emon(dev);
5270 		mutex_unlock(&dev->struct_mutex);
5271 	} else if (INTEL_INFO(dev)->gen >= 6) {
5272 		/*
5273 		 * PCU communication is slow and this doesn't need to be
5274 		 * done at any specific time, so do this out of our fast path
5275 		 * to make resume and init faster.
5276 		 *
5277 		 * We depend on the HW RC6 power context save/restore
5278 		 * mechanism when entering D3 through runtime PM suspend. So
5279 		 * disable RPM until RPS/RC6 is properly setup. We can only
5280 		 * get here via the driver load/system resume/runtime resume
5281 		 * paths, so the _noresume version is enough (and in case of
5282 		 * runtime resume it's necessary).
5283 		 */
5284 		if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5285 					   round_jiffies_up_relative(HZ)))
5286 			intel_runtime_pm_get_noresume(dev_priv);
5287 	}
5288 }
5289 
intel_reset_gt_powersave(struct drm_device * dev)5290 void intel_reset_gt_powersave(struct drm_device *dev)
5291 {
5292 	struct drm_i915_private *dev_priv = dev->dev_private;
5293 
5294 	dev_priv->rps.enabled = false;
5295 	intel_enable_gt_powersave(dev);
5296 }
5297 
ibx_init_clock_gating(struct drm_device * dev)5298 static void ibx_init_clock_gating(struct drm_device *dev)
5299 {
5300 	struct drm_i915_private *dev_priv = dev->dev_private;
5301 
5302 	/*
5303 	 * On Ibex Peak and Cougar Point, we need to disable clock
5304 	 * gating for the panel power sequencer or it will fail to
5305 	 * start up when no ports are active.
5306 	 */
5307 	I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5308 }
5309 
g4x_disable_trickle_feed(struct drm_device * dev)5310 static void g4x_disable_trickle_feed(struct drm_device *dev)
5311 {
5312 	struct drm_i915_private *dev_priv = dev->dev_private;
5313 	int pipe;
5314 
5315 	for_each_pipe(dev_priv, pipe) {
5316 		I915_WRITE(DSPCNTR(pipe),
5317 			   I915_READ(DSPCNTR(pipe)) |
5318 			   DISPPLANE_TRICKLE_FEED_DISABLE);
5319 		intel_flush_primary_plane(dev_priv, pipe);
5320 	}
5321 }
5322 
ilk_init_lp_watermarks(struct drm_device * dev)5323 static void ilk_init_lp_watermarks(struct drm_device *dev)
5324 {
5325 	struct drm_i915_private *dev_priv = dev->dev_private;
5326 
5327 	I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5328 	I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5329 	I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5330 
5331 	/*
5332 	 * Don't touch WM1S_LP_EN here.
5333 	 * Doing so could cause underruns.
5334 	 */
5335 }
5336 
ironlake_init_clock_gating(struct drm_device * dev)5337 static void ironlake_init_clock_gating(struct drm_device *dev)
5338 {
5339 	struct drm_i915_private *dev_priv = dev->dev_private;
5340 	uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
5341 
5342 	/*
5343 	 * Required for FBC
5344 	 * WaFbcDisableDpfcClockGating:ilk
5345 	 */
5346 	dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5347 		   ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5348 		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
5349 
5350 	I915_WRITE(PCH_3DCGDIS0,
5351 		   MARIUNIT_CLOCK_GATE_DISABLE |
5352 		   SVSMUNIT_CLOCK_GATE_DISABLE);
5353 	I915_WRITE(PCH_3DCGDIS1,
5354 		   VFMUNIT_CLOCK_GATE_DISABLE);
5355 
5356 	/*
5357 	 * According to the spec the following bits should be set in
5358 	 * order to enable memory self-refresh
5359 	 * The bit 22/21 of 0x42004
5360 	 * The bit 5 of 0x42020
5361 	 * The bit 15 of 0x45000
5362 	 */
5363 	I915_WRITE(ILK_DISPLAY_CHICKEN2,
5364 		   (I915_READ(ILK_DISPLAY_CHICKEN2) |
5365 		    ILK_DPARB_GATE | ILK_VSDPFD_FULL));
5366 	dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
5367 	I915_WRITE(DISP_ARB_CTL,
5368 		   (I915_READ(DISP_ARB_CTL) |
5369 		    DISP_FBC_WM_DIS));
5370 
5371 	ilk_init_lp_watermarks(dev);
5372 
5373 	/*
5374 	 * Based on the document from hardware guys the following bits
5375 	 * should be set unconditionally in order to enable FBC.
5376 	 * The bit 22 of 0x42000
5377 	 * The bit 22 of 0x42004
5378 	 * The bit 7,8,9 of 0x42020.
5379 	 */
5380 	if (IS_IRONLAKE_M(dev)) {
5381 		/* WaFbcAsynchFlipDisableFbcQueue:ilk */
5382 		I915_WRITE(ILK_DISPLAY_CHICKEN1,
5383 			   I915_READ(ILK_DISPLAY_CHICKEN1) |
5384 			   ILK_FBCQ_DIS);
5385 		I915_WRITE(ILK_DISPLAY_CHICKEN2,
5386 			   I915_READ(ILK_DISPLAY_CHICKEN2) |
5387 			   ILK_DPARB_GATE);
5388 	}
5389 
5390 	I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5391 
5392 	I915_WRITE(ILK_DISPLAY_CHICKEN2,
5393 		   I915_READ(ILK_DISPLAY_CHICKEN2) |
5394 		   ILK_ELPIN_409_SELECT);
5395 	I915_WRITE(_3D_CHICKEN2,
5396 		   _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5397 		   _3D_CHICKEN2_WM_READ_PIPELINED);
5398 
5399 	/* WaDisableRenderCachePipelinedFlush:ilk */
5400 	I915_WRITE(CACHE_MODE_0,
5401 		   _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
5402 
5403 	/* WaDisable_RenderCache_OperationalFlush:ilk */
5404 	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5405 
5406 	g4x_disable_trickle_feed(dev);
5407 
5408 	ibx_init_clock_gating(dev);
5409 }
5410 
cpt_init_clock_gating(struct drm_device * dev)5411 static void cpt_init_clock_gating(struct drm_device *dev)
5412 {
5413 	struct drm_i915_private *dev_priv = dev->dev_private;
5414 	int pipe;
5415 	uint32_t val;
5416 
5417 	/*
5418 	 * On Ibex Peak and Cougar Point, we need to disable clock
5419 	 * gating for the panel power sequencer or it will fail to
5420 	 * start up when no ports are active.
5421 	 */
5422 	I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5423 		   PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5424 		   PCH_CPUNIT_CLOCK_GATE_DISABLE);
5425 	I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5426 		   DPLS_EDP_PPS_FIX_DIS);
5427 	/* The below fixes the weird display corruption, a few pixels shifted
5428 	 * downward, on (only) LVDS of some HP laptops with IVY.
5429 	 */
5430 	for_each_pipe(dev_priv, pipe) {
5431 		val = I915_READ(TRANS_CHICKEN2(pipe));
5432 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5433 		val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
5434 		if (dev_priv->vbt.fdi_rx_polarity_inverted)
5435 			val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
5436 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5437 		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5438 		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
5439 		I915_WRITE(TRANS_CHICKEN2(pipe), val);
5440 	}
5441 	/* WADP0ClockGatingDisable */
5442 	for_each_pipe(dev_priv, pipe) {
5443 		I915_WRITE(TRANS_CHICKEN1(pipe),
5444 			   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5445 	}
5446 }
5447 
gen6_check_mch_setup(struct drm_device * dev)5448 static void gen6_check_mch_setup(struct drm_device *dev)
5449 {
5450 	struct drm_i915_private *dev_priv = dev->dev_private;
5451 	uint32_t tmp;
5452 
5453 	tmp = I915_READ(MCH_SSKPD);
5454 	if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5455 		DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5456 			      tmp);
5457 }
5458 
gen6_init_clock_gating(struct drm_device * dev)5459 static void gen6_init_clock_gating(struct drm_device *dev)
5460 {
5461 	struct drm_i915_private *dev_priv = dev->dev_private;
5462 	uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
5463 
5464 	I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5465 
5466 	I915_WRITE(ILK_DISPLAY_CHICKEN2,
5467 		   I915_READ(ILK_DISPLAY_CHICKEN2) |
5468 		   ILK_ELPIN_409_SELECT);
5469 
5470 	/* WaDisableHiZPlanesWhenMSAAEnabled:snb */
5471 	I915_WRITE(_3D_CHICKEN,
5472 		   _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5473 
5474 	/* WaDisable_RenderCache_OperationalFlush:snb */
5475 	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5476 
5477 	/*
5478 	 * BSpec recoomends 8x4 when MSAA is used,
5479 	 * however in practice 16x4 seems fastest.
5480 	 *
5481 	 * Note that PS/WM thread counts depend on the WIZ hashing
5482 	 * disable bit, which we don't touch here, but it's good
5483 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
5484 	 */
5485 	I915_WRITE(GEN6_GT_MODE,
5486 		   GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5487 
5488 	ilk_init_lp_watermarks(dev);
5489 
5490 	I915_WRITE(CACHE_MODE_0,
5491 		   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
5492 
5493 	I915_WRITE(GEN6_UCGCTL1,
5494 		   I915_READ(GEN6_UCGCTL1) |
5495 		   GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5496 		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5497 
5498 	/* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5499 	 * gating disable must be set.  Failure to set it results in
5500 	 * flickering pixels due to Z write ordering failures after
5501 	 * some amount of runtime in the Mesa "fire" demo, and Unigine
5502 	 * Sanctuary and Tropics, and apparently anything else with
5503 	 * alpha test or pixel discard.
5504 	 *
5505 	 * According to the spec, bit 11 (RCCUNIT) must also be set,
5506 	 * but we didn't debug actual testcases to find it out.
5507 	 *
5508 	 * WaDisableRCCUnitClockGating:snb
5509 	 * WaDisableRCPBUnitClockGating:snb
5510 	 */
5511 	I915_WRITE(GEN6_UCGCTL2,
5512 		   GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5513 		   GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5514 
5515 	/* WaStripsFansDisableFastClipPerformanceFix:snb */
5516 	I915_WRITE(_3D_CHICKEN3,
5517 		   _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
5518 
5519 	/*
5520 	 * Bspec says:
5521 	 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5522 	 * 3DSTATE_SF number of SF output attributes is more than 16."
5523 	 */
5524 	I915_WRITE(_3D_CHICKEN3,
5525 		   _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5526 
5527 	/*
5528 	 * According to the spec the following bits should be
5529 	 * set in order to enable memory self-refresh and fbc:
5530 	 * The bit21 and bit22 of 0x42000
5531 	 * The bit21 and bit22 of 0x42004
5532 	 * The bit5 and bit7 of 0x42020
5533 	 * The bit14 of 0x70180
5534 	 * The bit14 of 0x71180
5535 	 *
5536 	 * WaFbcAsynchFlipDisableFbcQueue:snb
5537 	 */
5538 	I915_WRITE(ILK_DISPLAY_CHICKEN1,
5539 		   I915_READ(ILK_DISPLAY_CHICKEN1) |
5540 		   ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5541 	I915_WRITE(ILK_DISPLAY_CHICKEN2,
5542 		   I915_READ(ILK_DISPLAY_CHICKEN2) |
5543 		   ILK_DPARB_GATE | ILK_VSDPFD_FULL);
5544 	I915_WRITE(ILK_DSPCLK_GATE_D,
5545 		   I915_READ(ILK_DSPCLK_GATE_D) |
5546 		   ILK_DPARBUNIT_CLOCK_GATE_ENABLE  |
5547 		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
5548 
5549 	g4x_disable_trickle_feed(dev);
5550 
5551 	cpt_init_clock_gating(dev);
5552 
5553 	gen6_check_mch_setup(dev);
5554 }
5555 
gen7_setup_fixed_func_scheduler(struct drm_i915_private * dev_priv)5556 static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5557 {
5558 	uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5559 
5560 	/*
5561 	 * WaVSThreadDispatchOverride:ivb,vlv
5562 	 *
5563 	 * This actually overrides the dispatch
5564 	 * mode for all thread types.
5565 	 */
5566 	reg &= ~GEN7_FF_SCHED_MASK;
5567 	reg |= GEN7_FF_TS_SCHED_HW;
5568 	reg |= GEN7_FF_VS_SCHED_HW;
5569 	reg |= GEN7_FF_DS_SCHED_HW;
5570 
5571 	I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5572 }
5573 
lpt_init_clock_gating(struct drm_device * dev)5574 static void lpt_init_clock_gating(struct drm_device *dev)
5575 {
5576 	struct drm_i915_private *dev_priv = dev->dev_private;
5577 
5578 	/*
5579 	 * TODO: this bit should only be enabled when really needed, then
5580 	 * disabled when not needed anymore in order to save power.
5581 	 */
5582 	if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5583 		I915_WRITE(SOUTH_DSPCLK_GATE_D,
5584 			   I915_READ(SOUTH_DSPCLK_GATE_D) |
5585 			   PCH_LP_PARTITION_LEVEL_DISABLE);
5586 
5587 	/* WADPOClockGatingDisable:hsw */
5588 	I915_WRITE(_TRANSA_CHICKEN1,
5589 		   I915_READ(_TRANSA_CHICKEN1) |
5590 		   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5591 }
5592 
lpt_suspend_hw(struct drm_device * dev)5593 static void lpt_suspend_hw(struct drm_device *dev)
5594 {
5595 	struct drm_i915_private *dev_priv = dev->dev_private;
5596 
5597 	if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5598 		uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5599 
5600 		val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5601 		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5602 	}
5603 }
5604 
broadwell_init_clock_gating(struct drm_device * dev)5605 static void broadwell_init_clock_gating(struct drm_device *dev)
5606 {
5607 	struct drm_i915_private *dev_priv = dev->dev_private;
5608 	enum pipe pipe;
5609 
5610 	I915_WRITE(WM3_LP_ILK, 0);
5611 	I915_WRITE(WM2_LP_ILK, 0);
5612 	I915_WRITE(WM1_LP_ILK, 0);
5613 
5614 	/* FIXME(BDW): Check all the w/a, some might only apply to
5615 	 * pre-production hw. */
5616 
5617 
5618 	I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_BWGTLB_DISABLE));
5619 
5620 	I915_WRITE(_3D_CHICKEN3,
5621 		   _MASKED_BIT_ENABLE(_3D_CHICKEN_SDE_LIMIT_FIFO_POLY_DEPTH(2)));
5622 
5623 
5624 	/* WaSwitchSolVfFArbitrationPriority:bdw */
5625 	I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5626 
5627 	/* WaPsrDPAMaskVBlankInSRD:bdw */
5628 	I915_WRITE(CHICKEN_PAR1_1,
5629 		   I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5630 
5631 	/* WaPsrDPRSUnmaskVBlankInSRD:bdw */
5632 	for_each_pipe(dev_priv, pipe) {
5633 		I915_WRITE(CHICKEN_PIPESL_1(pipe),
5634 			   I915_READ(CHICKEN_PIPESL_1(pipe)) |
5635 			   BDW_DPRS_MASK_VBLANK_SRD);
5636 	}
5637 
5638 	/* WaVSRefCountFullforceMissDisable:bdw */
5639 	/* WaDSRefCountFullforceMissDisable:bdw */
5640 	I915_WRITE(GEN7_FF_THREAD_MODE,
5641 		   I915_READ(GEN7_FF_THREAD_MODE) &
5642 		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
5643 
5644 	I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5645 		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
5646 
5647 	/* WaDisableSDEUnitClockGating:bdw */
5648 	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5649 		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
5650 
5651 	lpt_init_clock_gating(dev);
5652 }
5653 
haswell_init_clock_gating(struct drm_device * dev)5654 static void haswell_init_clock_gating(struct drm_device *dev)
5655 {
5656 	struct drm_i915_private *dev_priv = dev->dev_private;
5657 
5658 	ilk_init_lp_watermarks(dev);
5659 
5660 	/* L3 caching of data atomics doesn't work -- disable it. */
5661 	I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
5662 	I915_WRITE(HSW_ROW_CHICKEN3,
5663 		   _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
5664 
5665 	/* This is required by WaCatErrorRejectionIssue:hsw */
5666 	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5667 			I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5668 			GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5669 
5670 	/* WaVSRefCountFullforceMissDisable:hsw */
5671 	I915_WRITE(GEN7_FF_THREAD_MODE,
5672 		   I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
5673 
5674 	/* WaDisable_RenderCache_OperationalFlush:hsw */
5675 	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5676 
5677 	/* enable HiZ Raw Stall Optimization */
5678 	I915_WRITE(CACHE_MODE_0_GEN7,
5679 		   _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5680 
5681 	/* WaDisable4x2SubspanOptimization:hsw */
5682 	I915_WRITE(CACHE_MODE_1,
5683 		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
5684 
5685 	/*
5686 	 * BSpec recommends 8x4 when MSAA is used,
5687 	 * however in practice 16x4 seems fastest.
5688 	 *
5689 	 * Note that PS/WM thread counts depend on the WIZ hashing
5690 	 * disable bit, which we don't touch here, but it's good
5691 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
5692 	 */
5693 	I915_WRITE(GEN7_GT_MODE,
5694 		   GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5695 
5696 	/* WaSampleCChickenBitEnable:hsw */
5697 	I915_WRITE(HALF_SLICE_CHICKEN3,
5698 		   _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
5699 
5700 	/* WaSwitchSolVfFArbitrationPriority:hsw */
5701 	I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5702 
5703 	/* WaRsPkgCStateDisplayPMReq:hsw */
5704 	I915_WRITE(CHICKEN_PAR1_1,
5705 		   I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
5706 
5707 	lpt_init_clock_gating(dev);
5708 }
5709 
ivybridge_init_clock_gating(struct drm_device * dev)5710 static void ivybridge_init_clock_gating(struct drm_device *dev)
5711 {
5712 	struct drm_i915_private *dev_priv = dev->dev_private;
5713 	uint32_t snpcr;
5714 
5715 	ilk_init_lp_watermarks(dev);
5716 
5717 	I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
5718 
5719 	/* WaDisableEarlyCull:ivb */
5720 	I915_WRITE(_3D_CHICKEN3,
5721 		   _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5722 
5723 	/* WaDisableBackToBackFlipFix:ivb */
5724 	I915_WRITE(IVB_CHICKEN3,
5725 		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5726 		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
5727 
5728 	/* WaDisablePSDDualDispatchEnable:ivb */
5729 	if (IS_IVB_GT1(dev))
5730 		I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5731 			   _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
5732 
5733 	/* WaDisable_RenderCache_OperationalFlush:ivb */
5734 	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5735 
5736 	/* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
5737 	I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
5738 		   GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
5739 
5740 	/* WaApplyL3ControlAndL3ChickenMode:ivb */
5741 	I915_WRITE(GEN7_L3CNTLREG1,
5742 			GEN7_WA_FOR_GEN7_L3_CONTROL);
5743 	I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
5744 		   GEN7_WA_L3_CHICKEN_MODE);
5745 	if (IS_IVB_GT1(dev))
5746 		I915_WRITE(GEN7_ROW_CHICKEN2,
5747 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5748 	else {
5749 		/* must write both registers */
5750 		I915_WRITE(GEN7_ROW_CHICKEN2,
5751 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5752 		I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
5753 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5754 	}
5755 
5756 	/* WaForceL3Serialization:ivb */
5757 	I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5758 		   ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5759 
5760 	/*
5761 	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
5762 	 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
5763 	 */
5764 	I915_WRITE(GEN6_UCGCTL2,
5765 		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
5766 
5767 	/* This is required by WaCatErrorRejectionIssue:ivb */
5768 	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5769 			I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5770 			GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5771 
5772 	g4x_disable_trickle_feed(dev);
5773 
5774 	gen7_setup_fixed_func_scheduler(dev_priv);
5775 
5776 	if (0) { /* causes HiZ corruption on ivb:gt1 */
5777 		/* enable HiZ Raw Stall Optimization */
5778 		I915_WRITE(CACHE_MODE_0_GEN7,
5779 			   _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5780 	}
5781 
5782 	/* WaDisable4x2SubspanOptimization:ivb */
5783 	I915_WRITE(CACHE_MODE_1,
5784 		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
5785 
5786 	/*
5787 	 * BSpec recommends 8x4 when MSAA is used,
5788 	 * however in practice 16x4 seems fastest.
5789 	 *
5790 	 * Note that PS/WM thread counts depend on the WIZ hashing
5791 	 * disable bit, which we don't touch here, but it's good
5792 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
5793 	 */
5794 	I915_WRITE(GEN7_GT_MODE,
5795 		   GEN6_WIZ_HASHING_MASK | GEN6_WIZ_HASHING_16x4);
5796 
5797 	snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
5798 	snpcr &= ~GEN6_MBC_SNPCR_MASK;
5799 	snpcr |= GEN6_MBC_SNPCR_MED;
5800 	I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
5801 
5802 	if (!HAS_PCH_NOP(dev))
5803 		cpt_init_clock_gating(dev);
5804 
5805 	gen6_check_mch_setup(dev);
5806 }
5807 
valleyview_init_clock_gating(struct drm_device * dev)5808 static void valleyview_init_clock_gating(struct drm_device *dev)
5809 {
5810 	struct drm_i915_private *dev_priv = dev->dev_private;
5811 
5812 	I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
5813 
5814 	/* WaDisableEarlyCull:vlv */
5815 	I915_WRITE(_3D_CHICKEN3,
5816 		   _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
5817 
5818 	/* WaDisableBackToBackFlipFix:vlv */
5819 	I915_WRITE(IVB_CHICKEN3,
5820 		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
5821 		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
5822 
5823 	/* WaPsdDispatchEnable:vlv */
5824 	/* WaDisablePSDDualDispatchEnable:vlv */
5825 	I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
5826 		   _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
5827 				      GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
5828 
5829 	/* WaDisable_RenderCache_OperationalFlush:vlv */
5830 	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5831 
5832 	/* WaForceL3Serialization:vlv */
5833 	I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
5834 		   ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
5835 
5836 	/* WaDisableDopClockGating:vlv */
5837 	I915_WRITE(GEN7_ROW_CHICKEN2,
5838 		   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
5839 
5840 	/* This is required by WaCatErrorRejectionIssue:vlv */
5841 	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5842 		   I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5843 		   GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5844 
5845 	gen7_setup_fixed_func_scheduler(dev_priv);
5846 
5847 	/*
5848 	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
5849 	 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
5850 	 */
5851 	I915_WRITE(GEN6_UCGCTL2,
5852 		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
5853 
5854 	/* WaDisableL3Bank2xClockGate:vlv
5855 	 * Disabling L3 clock gating- MMIO 940c[25] = 1
5856 	 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
5857 	I915_WRITE(GEN7_UCGCTL4,
5858 		   I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
5859 
5860 	I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
5861 
5862 	/*
5863 	 * BSpec says this must be set, even though
5864 	 * WaDisable4x2SubspanOptimization isn't listed for VLV.
5865 	 */
5866 	I915_WRITE(CACHE_MODE_1,
5867 		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
5868 
5869 	/*
5870 	 * WaIncreaseL3CreditsForVLVB0:vlv
5871 	 * This is the hardware default actually.
5872 	 */
5873 	I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
5874 
5875 	/*
5876 	 * WaDisableVLVClockGating_VBIIssue:vlv
5877 	 * Disable clock gating on th GCFG unit to prevent a delay
5878 	 * in the reporting of vblank events.
5879 	 */
5880 	I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
5881 }
5882 
cherryview_init_clock_gating(struct drm_device * dev)5883 static void cherryview_init_clock_gating(struct drm_device *dev)
5884 {
5885 	struct drm_i915_private *dev_priv = dev->dev_private;
5886 
5887 	I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
5888 
5889 	I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
5890 
5891 	/* WaVSRefCountFullforceMissDisable:chv */
5892 	/* WaDSRefCountFullforceMissDisable:chv */
5893 	I915_WRITE(GEN7_FF_THREAD_MODE,
5894 		   I915_READ(GEN7_FF_THREAD_MODE) &
5895 		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
5896 
5897 	/* WaDisableSemaphoreAndSyncFlipWait:chv */
5898 	I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5899 		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
5900 
5901 	/* WaDisableCSUnitClockGating:chv */
5902 	I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5903 		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5904 
5905 	/* WaDisableSDEUnitClockGating:chv */
5906 	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5907 		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
5908 
5909 	/* WaDisableGunitClockGating:chv (pre-production hw) */
5910 	I915_WRITE(VLV_GUNIT_CLOCK_GATE, I915_READ(VLV_GUNIT_CLOCK_GATE) |
5911 		   GINT_DIS);
5912 
5913 	/* WaDisableFfDopClockGating:chv (pre-production hw) */
5914 	I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5915 		   _MASKED_BIT_ENABLE(GEN8_FF_DOP_CLOCK_GATE_DISABLE));
5916 
5917 	/* WaDisableDopClockGating:chv (pre-production hw) */
5918 	I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
5919 		   GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
5920 }
5921 
g4x_init_clock_gating(struct drm_device * dev)5922 static void g4x_init_clock_gating(struct drm_device *dev)
5923 {
5924 	struct drm_i915_private *dev_priv = dev->dev_private;
5925 	uint32_t dspclk_gate;
5926 
5927 	I915_WRITE(RENCLK_GATE_D1, 0);
5928 	I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
5929 		   GS_UNIT_CLOCK_GATE_DISABLE |
5930 		   CL_UNIT_CLOCK_GATE_DISABLE);
5931 	I915_WRITE(RAMCLK_GATE_D, 0);
5932 	dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
5933 		OVRUNIT_CLOCK_GATE_DISABLE |
5934 		OVCUNIT_CLOCK_GATE_DISABLE;
5935 	if (IS_GM45(dev))
5936 		dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
5937 	I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
5938 
5939 	/* WaDisableRenderCachePipelinedFlush */
5940 	I915_WRITE(CACHE_MODE_0,
5941 		   _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
5942 
5943 	/* WaDisable_RenderCache_OperationalFlush:g4x */
5944 	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5945 
5946 	g4x_disable_trickle_feed(dev);
5947 }
5948 
crestline_init_clock_gating(struct drm_device * dev)5949 static void crestline_init_clock_gating(struct drm_device *dev)
5950 {
5951 	struct drm_i915_private *dev_priv = dev->dev_private;
5952 
5953 	I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
5954 	I915_WRITE(RENCLK_GATE_D2, 0);
5955 	I915_WRITE(DSPCLK_GATE_D, 0);
5956 	I915_WRITE(RAMCLK_GATE_D, 0);
5957 	I915_WRITE16(DEUC, 0);
5958 	I915_WRITE(MI_ARB_STATE,
5959 		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
5960 
5961 	/* WaDisable_RenderCache_OperationalFlush:gen4 */
5962 	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5963 }
5964 
broadwater_init_clock_gating(struct drm_device * dev)5965 static void broadwater_init_clock_gating(struct drm_device *dev)
5966 {
5967 	struct drm_i915_private *dev_priv = dev->dev_private;
5968 
5969 	I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
5970 		   I965_RCC_CLOCK_GATE_DISABLE |
5971 		   I965_RCPB_CLOCK_GATE_DISABLE |
5972 		   I965_ISC_CLOCK_GATE_DISABLE |
5973 		   I965_FBC_CLOCK_GATE_DISABLE);
5974 	I915_WRITE(RENCLK_GATE_D2, 0);
5975 	I915_WRITE(MI_ARB_STATE,
5976 		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
5977 
5978 	/* WaDisable_RenderCache_OperationalFlush:gen4 */
5979 	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5980 }
5981 
gen3_init_clock_gating(struct drm_device * dev)5982 static void gen3_init_clock_gating(struct drm_device *dev)
5983 {
5984 	struct drm_i915_private *dev_priv = dev->dev_private;
5985 	u32 dstate = I915_READ(D_STATE);
5986 
5987 	dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
5988 		DSTATE_DOT_CLOCK_GATING;
5989 	I915_WRITE(D_STATE, dstate);
5990 
5991 	if (IS_PINEVIEW(dev))
5992 		I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
5993 
5994 	/* IIR "flip pending" means done if this bit is set */
5995 	I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
5996 
5997 	/* interrupts should cause a wake up from C3 */
5998 	I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
5999 
6000 	/* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6001 	I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
6002 
6003 	I915_WRITE(MI_ARB_STATE,
6004 		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6005 }
6006 
i85x_init_clock_gating(struct drm_device * dev)6007 static void i85x_init_clock_gating(struct drm_device *dev)
6008 {
6009 	struct drm_i915_private *dev_priv = dev->dev_private;
6010 
6011 	I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
6012 
6013 	/* interrupts should cause a wake up from C3 */
6014 	I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6015 		   _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
6016 
6017 	I915_WRITE(MEM_MODE,
6018 		   _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
6019 }
6020 
i830_init_clock_gating(struct drm_device * dev)6021 static void i830_init_clock_gating(struct drm_device *dev)
6022 {
6023 	struct drm_i915_private *dev_priv = dev->dev_private;
6024 
6025 	I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
6026 
6027 	I915_WRITE(MEM_MODE,
6028 		   _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6029 		   _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
6030 }
6031 
intel_init_clock_gating(struct drm_device * dev)6032 void intel_init_clock_gating(struct drm_device *dev)
6033 {
6034 	struct drm_i915_private *dev_priv = dev->dev_private;
6035 
6036 	dev_priv->display.init_clock_gating(dev);
6037 }
6038 
intel_suspend_hw(struct drm_device * dev)6039 void intel_suspend_hw(struct drm_device *dev)
6040 {
6041 	if (HAS_PCH_LPT(dev))
6042 		lpt_suspend_hw(dev);
6043 }
6044 
6045 #define for_each_power_well(i, power_well, domain_mask, power_domains)	\
6046 	for (i = 0;							\
6047 	     i < (power_domains)->power_well_count &&			\
6048 		 ((power_well) = &(power_domains)->power_wells[i]);	\
6049 	     i++)							\
6050 		if ((power_well)->domains & (domain_mask))
6051 
6052 #define for_each_power_well_rev(i, power_well, domain_mask, power_domains) \
6053 	for (i = (power_domains)->power_well_count - 1;			 \
6054 	     i >= 0 && ((power_well) = &(power_domains)->power_wells[i]);\
6055 	     i--)							 \
6056 		if ((power_well)->domains & (domain_mask))
6057 
6058 /**
6059  * We should only use the power well if we explicitly asked the hardware to
6060  * enable it, so check if it's enabled and also check if we've requested it to
6061  * be enabled.
6062  */
hsw_power_well_enabled(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6063 static bool hsw_power_well_enabled(struct drm_i915_private *dev_priv,
6064 				   struct i915_power_well *power_well)
6065 {
6066 	return I915_READ(HSW_PWR_WELL_DRIVER) ==
6067 		     (HSW_PWR_WELL_ENABLE_REQUEST | HSW_PWR_WELL_STATE_ENABLED);
6068 }
6069 
intel_display_power_enabled_unlocked(struct drm_i915_private * dev_priv,enum intel_display_power_domain domain)6070 bool intel_display_power_enabled_unlocked(struct drm_i915_private *dev_priv,
6071 					  enum intel_display_power_domain domain)
6072 {
6073 	struct i915_power_domains *power_domains;
6074 	struct i915_power_well *power_well;
6075 	bool is_enabled;
6076 	int i;
6077 
6078 	if (dev_priv->pm.suspended)
6079 		return false;
6080 
6081 	power_domains = &dev_priv->power_domains;
6082 
6083 	is_enabled = true;
6084 
6085 	for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6086 		if (power_well->always_on)
6087 			continue;
6088 
6089 		if (!power_well->hw_enabled) {
6090 			is_enabled = false;
6091 			break;
6092 		}
6093 	}
6094 
6095 	return is_enabled;
6096 }
6097 
intel_display_power_enabled(struct drm_i915_private * dev_priv,enum intel_display_power_domain domain)6098 bool intel_display_power_enabled(struct drm_i915_private *dev_priv,
6099 				 enum intel_display_power_domain domain)
6100 {
6101 	struct i915_power_domains *power_domains;
6102 	bool ret;
6103 
6104 	power_domains = &dev_priv->power_domains;
6105 
6106 	mutex_lock(&power_domains->lock);
6107 	ret = intel_display_power_enabled_unlocked(dev_priv, domain);
6108 	mutex_unlock(&power_domains->lock);
6109 
6110 	return ret;
6111 }
6112 
6113 /*
6114  * Starting with Haswell, we have a "Power Down Well" that can be turned off
6115  * when not needed anymore. We have 4 registers that can request the power well
6116  * to be enabled, and it will only be disabled if none of the registers is
6117  * requesting it to be enabled.
6118  */
hsw_power_well_post_enable(struct drm_i915_private * dev_priv)6119 static void hsw_power_well_post_enable(struct drm_i915_private *dev_priv)
6120 {
6121 	struct drm_device *dev = dev_priv->dev;
6122 
6123 	/*
6124 	 * After we re-enable the power well, if we touch VGA register 0x3d5
6125 	 * we'll get unclaimed register interrupts. This stops after we write
6126 	 * anything to the VGA MSR register. The vgacon module uses this
6127 	 * register all the time, so if we unbind our driver and, as a
6128 	 * consequence, bind vgacon, we'll get stuck in an infinite loop at
6129 	 * console_unlock(). So make here we touch the VGA MSR register, making
6130 	 * sure vgacon can keep working normally without triggering interrupts
6131 	 * and error messages.
6132 	 */
6133 	vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6134 	outb(inb(VGA_MSR_READ), VGA_MSR_WRITE);
6135 	vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6136 
6137 	if (IS_BROADWELL(dev))
6138 		gen8_irq_power_well_post_enable(dev_priv);
6139 }
6140 
hsw_set_power_well(struct drm_i915_private * dev_priv,struct i915_power_well * power_well,bool enable)6141 static void hsw_set_power_well(struct drm_i915_private *dev_priv,
6142 			       struct i915_power_well *power_well, bool enable)
6143 {
6144 	bool is_enabled, enable_requested;
6145 	uint32_t tmp;
6146 
6147 	tmp = I915_READ(HSW_PWR_WELL_DRIVER);
6148 	is_enabled = tmp & HSW_PWR_WELL_STATE_ENABLED;
6149 	enable_requested = tmp & HSW_PWR_WELL_ENABLE_REQUEST;
6150 
6151 	if (enable) {
6152 		if (!enable_requested)
6153 			I915_WRITE(HSW_PWR_WELL_DRIVER,
6154 				   HSW_PWR_WELL_ENABLE_REQUEST);
6155 
6156 		if (!is_enabled) {
6157 			DRM_DEBUG_KMS("Enabling power well\n");
6158 			if (wait_for((I915_READ(HSW_PWR_WELL_DRIVER) &
6159 				      HSW_PWR_WELL_STATE_ENABLED), 20))
6160 				DRM_ERROR("Timeout enabling power well\n");
6161 		}
6162 
6163 		hsw_power_well_post_enable(dev_priv);
6164 	} else {
6165 		if (enable_requested) {
6166 			I915_WRITE(HSW_PWR_WELL_DRIVER, 0);
6167 			POSTING_READ(HSW_PWR_WELL_DRIVER);
6168 			DRM_DEBUG_KMS("Requesting to disable the power well\n");
6169 		}
6170 	}
6171 }
6172 
hsw_power_well_sync_hw(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6173 static void hsw_power_well_sync_hw(struct drm_i915_private *dev_priv,
6174 				   struct i915_power_well *power_well)
6175 {
6176 	hsw_set_power_well(dev_priv, power_well, power_well->count > 0);
6177 
6178 	/*
6179 	 * We're taking over the BIOS, so clear any requests made by it since
6180 	 * the driver is in charge now.
6181 	 */
6182 	if (I915_READ(HSW_PWR_WELL_BIOS) & HSW_PWR_WELL_ENABLE_REQUEST)
6183 		I915_WRITE(HSW_PWR_WELL_BIOS, 0);
6184 }
6185 
hsw_power_well_enable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6186 static void hsw_power_well_enable(struct drm_i915_private *dev_priv,
6187 				  struct i915_power_well *power_well)
6188 {
6189 	hsw_set_power_well(dev_priv, power_well, true);
6190 }
6191 
hsw_power_well_disable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6192 static void hsw_power_well_disable(struct drm_i915_private *dev_priv,
6193 				   struct i915_power_well *power_well)
6194 {
6195 	hsw_set_power_well(dev_priv, power_well, false);
6196 }
6197 
i9xx_always_on_power_well_noop(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6198 static void i9xx_always_on_power_well_noop(struct drm_i915_private *dev_priv,
6199 					   struct i915_power_well *power_well)
6200 {
6201 }
6202 
i9xx_always_on_power_well_enabled(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6203 static bool i9xx_always_on_power_well_enabled(struct drm_i915_private *dev_priv,
6204 					     struct i915_power_well *power_well)
6205 {
6206 	return true;
6207 }
6208 
vlv_set_power_well(struct drm_i915_private * dev_priv,struct i915_power_well * power_well,bool enable)6209 static void vlv_set_power_well(struct drm_i915_private *dev_priv,
6210 			       struct i915_power_well *power_well, bool enable)
6211 {
6212 	enum punit_power_well power_well_id = power_well->data;
6213 	u32 mask;
6214 	u32 state;
6215 	u32 ctrl;
6216 
6217 	mask = PUNIT_PWRGT_MASK(power_well_id);
6218 	state = enable ? PUNIT_PWRGT_PWR_ON(power_well_id) :
6219 			 PUNIT_PWRGT_PWR_GATE(power_well_id);
6220 
6221 	mutex_lock(&dev_priv->rps.hw_lock);
6222 
6223 #define COND \
6224 	((vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask) == state)
6225 
6226 	if (COND)
6227 		goto out;
6228 
6229 	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL);
6230 	ctrl &= ~mask;
6231 	ctrl |= state;
6232 	vlv_punit_write(dev_priv, PUNIT_REG_PWRGT_CTRL, ctrl);
6233 
6234 	if (wait_for(COND, 100))
6235 		DRM_ERROR("timout setting power well state %08x (%08x)\n",
6236 			  state,
6237 			  vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL));
6238 
6239 #undef COND
6240 
6241 out:
6242 	mutex_unlock(&dev_priv->rps.hw_lock);
6243 }
6244 
vlv_power_well_sync_hw(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6245 static void vlv_power_well_sync_hw(struct drm_i915_private *dev_priv,
6246 				   struct i915_power_well *power_well)
6247 {
6248 	vlv_set_power_well(dev_priv, power_well, power_well->count > 0);
6249 }
6250 
vlv_power_well_enable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6251 static void vlv_power_well_enable(struct drm_i915_private *dev_priv,
6252 				  struct i915_power_well *power_well)
6253 {
6254 	vlv_set_power_well(dev_priv, power_well, true);
6255 }
6256 
vlv_power_well_disable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6257 static void vlv_power_well_disable(struct drm_i915_private *dev_priv,
6258 				   struct i915_power_well *power_well)
6259 {
6260 	vlv_set_power_well(dev_priv, power_well, false);
6261 }
6262 
vlv_power_well_enabled(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6263 static bool vlv_power_well_enabled(struct drm_i915_private *dev_priv,
6264 				   struct i915_power_well *power_well)
6265 {
6266 	int power_well_id = power_well->data;
6267 	bool enabled = false;
6268 	u32 mask;
6269 	u32 state;
6270 	u32 ctrl;
6271 
6272 	mask = PUNIT_PWRGT_MASK(power_well_id);
6273 	ctrl = PUNIT_PWRGT_PWR_ON(power_well_id);
6274 
6275 	mutex_lock(&dev_priv->rps.hw_lock);
6276 
6277 	state = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_STATUS) & mask;
6278 	/*
6279 	 * We only ever set the power-on and power-gate states, anything
6280 	 * else is unexpected.
6281 	 */
6282 	WARN_ON(state != PUNIT_PWRGT_PWR_ON(power_well_id) &&
6283 		state != PUNIT_PWRGT_PWR_GATE(power_well_id));
6284 	if (state == ctrl)
6285 		enabled = true;
6286 
6287 	/*
6288 	 * A transient state at this point would mean some unexpected party
6289 	 * is poking at the power controls too.
6290 	 */
6291 	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_PWRGT_CTRL) & mask;
6292 	WARN_ON(ctrl != state);
6293 
6294 	mutex_unlock(&dev_priv->rps.hw_lock);
6295 
6296 	return enabled;
6297 }
6298 
vlv_display_power_well_enable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6299 static void vlv_display_power_well_enable(struct drm_i915_private *dev_priv,
6300 					  struct i915_power_well *power_well)
6301 {
6302 	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6303 
6304 	vlv_set_power_well(dev_priv, power_well, true);
6305 
6306 	spin_lock_irq(&dev_priv->irq_lock);
6307 	valleyview_enable_display_irqs(dev_priv);
6308 	spin_unlock_irq(&dev_priv->irq_lock);
6309 
6310 	/*
6311 	 * During driver initialization/resume we can avoid restoring the
6312 	 * part of the HW/SW state that will be inited anyway explicitly.
6313 	 */
6314 	if (dev_priv->power_domains.initializing)
6315 		return;
6316 
6317 	intel_hpd_init(dev_priv->dev);
6318 
6319 	i915_redisable_vga_power_on(dev_priv->dev);
6320 }
6321 
vlv_display_power_well_disable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6322 static void vlv_display_power_well_disable(struct drm_i915_private *dev_priv,
6323 					   struct i915_power_well *power_well)
6324 {
6325 	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DISP2D);
6326 
6327 	spin_lock_irq(&dev_priv->irq_lock);
6328 	valleyview_disable_display_irqs(dev_priv);
6329 	spin_unlock_irq(&dev_priv->irq_lock);
6330 
6331 	vlv_set_power_well(dev_priv, power_well, false);
6332 
6333 	vlv_power_sequencer_reset(dev_priv);
6334 }
6335 
vlv_dpio_cmn_power_well_enable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6336 static void vlv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6337 					   struct i915_power_well *power_well)
6338 {
6339 	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6340 
6341 	/*
6342 	 * Enable the CRI clock source so we can get at the
6343 	 * display and the reference clock for VGA
6344 	 * hotplug / manual detection.
6345 	 */
6346 	I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6347 		   DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6348 	udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6349 
6350 	vlv_set_power_well(dev_priv, power_well, true);
6351 
6352 	/*
6353 	 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
6354 	 *  6.	De-assert cmn_reset/side_reset. Same as VLV X0.
6355 	 *   a.	GUnit 0x2110 bit[0] set to 1 (def 0)
6356 	 *   b.	The other bits such as sfr settings / modesel may all
6357 	 *	be set to 0.
6358 	 *
6359 	 * This should only be done on init and resume from S3 with
6360 	 * both PLLs disabled, or we risk losing DPIO and PLL
6361 	 * synchronization.
6362 	 */
6363 	I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
6364 }
6365 
vlv_dpio_cmn_power_well_disable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6366 static void vlv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6367 					    struct i915_power_well *power_well)
6368 {
6369 	enum pipe pipe;
6370 
6371 	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC);
6372 
6373 	for_each_pipe(dev_priv, pipe)
6374 		assert_pll_disabled(dev_priv, pipe);
6375 
6376 	/* Assert common reset */
6377 	I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) & ~DPIO_CMNRST);
6378 
6379 	vlv_set_power_well(dev_priv, power_well, false);
6380 }
6381 
chv_dpio_cmn_power_well_enable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6382 static void chv_dpio_cmn_power_well_enable(struct drm_i915_private *dev_priv,
6383 					   struct i915_power_well *power_well)
6384 {
6385 	enum dpio_phy phy;
6386 
6387 	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
6388 		     power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
6389 
6390 	/*
6391 	 * Enable the CRI clock source so we can get at the
6392 	 * display and the reference clock for VGA
6393 	 * hotplug / manual detection.
6394 	 */
6395 	if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
6396 		phy = DPIO_PHY0;
6397 		I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6398 			   DPLL_REFA_CLK_ENABLE_VLV);
6399 		I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
6400 			   DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6401 	} else {
6402 		phy = DPIO_PHY1;
6403 		I915_WRITE(DPLL(PIPE_C), I915_READ(DPLL(PIPE_C)) |
6404 			   DPLL_REFA_CLK_ENABLE_VLV | DPLL_INTEGRATED_CRI_CLK_VLV);
6405 	}
6406 	udelay(1); /* >10ns for cmnreset, >0ns for sidereset */
6407 	vlv_set_power_well(dev_priv, power_well, true);
6408 
6409 	/* Poll for phypwrgood signal */
6410 	if (wait_for(I915_READ(DISPLAY_PHY_STATUS) & PHY_POWERGOOD(phy), 1))
6411 		DRM_ERROR("Display PHY %d is not power up\n", phy);
6412 
6413 	I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) |
6414 		   PHY_COM_LANE_RESET_DEASSERT(phy));
6415 }
6416 
chv_dpio_cmn_power_well_disable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6417 static void chv_dpio_cmn_power_well_disable(struct drm_i915_private *dev_priv,
6418 					    struct i915_power_well *power_well)
6419 {
6420 	enum dpio_phy phy;
6421 
6422 	WARN_ON_ONCE(power_well->data != PUNIT_POWER_WELL_DPIO_CMN_BC &&
6423 		     power_well->data != PUNIT_POWER_WELL_DPIO_CMN_D);
6424 
6425 	if (power_well->data == PUNIT_POWER_WELL_DPIO_CMN_BC) {
6426 		phy = DPIO_PHY0;
6427 		assert_pll_disabled(dev_priv, PIPE_A);
6428 		assert_pll_disabled(dev_priv, PIPE_B);
6429 	} else {
6430 		phy = DPIO_PHY1;
6431 		assert_pll_disabled(dev_priv, PIPE_C);
6432 	}
6433 
6434 	I915_WRITE(DISPLAY_PHY_CONTROL, I915_READ(DISPLAY_PHY_CONTROL) &
6435 		   ~PHY_COM_LANE_RESET_DEASSERT(phy));
6436 
6437 	vlv_set_power_well(dev_priv, power_well, false);
6438 }
6439 
chv_pipe_power_well_enabled(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6440 static bool chv_pipe_power_well_enabled(struct drm_i915_private *dev_priv,
6441 					struct i915_power_well *power_well)
6442 {
6443 	enum pipe pipe = power_well->data;
6444 	bool enabled;
6445 	u32 state, ctrl;
6446 
6447 	mutex_lock(&dev_priv->rps.hw_lock);
6448 
6449 	state = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe);
6450 	/*
6451 	 * We only ever set the power-on and power-gate states, anything
6452 	 * else is unexpected.
6453 	 */
6454 	WARN_ON(state != DP_SSS_PWR_ON(pipe) && state != DP_SSS_PWR_GATE(pipe));
6455 	enabled = state == DP_SSS_PWR_ON(pipe);
6456 
6457 	/*
6458 	 * A transient state at this point would mean some unexpected party
6459 	 * is poking at the power controls too.
6460 	 */
6461 	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSC_MASK(pipe);
6462 	WARN_ON(ctrl << 16 != state);
6463 
6464 	mutex_unlock(&dev_priv->rps.hw_lock);
6465 
6466 	return enabled;
6467 }
6468 
chv_set_pipe_power_well(struct drm_i915_private * dev_priv,struct i915_power_well * power_well,bool enable)6469 static void chv_set_pipe_power_well(struct drm_i915_private *dev_priv,
6470 				    struct i915_power_well *power_well,
6471 				    bool enable)
6472 {
6473 	enum pipe pipe = power_well->data;
6474 	u32 state;
6475 	u32 ctrl;
6476 
6477 	state = enable ? DP_SSS_PWR_ON(pipe) : DP_SSS_PWR_GATE(pipe);
6478 
6479 	mutex_lock(&dev_priv->rps.hw_lock);
6480 
6481 #define COND \
6482 	((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) & DP_SSS_MASK(pipe)) == state)
6483 
6484 	if (COND)
6485 		goto out;
6486 
6487 	ctrl = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
6488 	ctrl &= ~DP_SSC_MASK(pipe);
6489 	ctrl |= enable ? DP_SSC_PWR_ON(pipe) : DP_SSC_PWR_GATE(pipe);
6490 	vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, ctrl);
6491 
6492 	if (wait_for(COND, 100))
6493 		DRM_ERROR("timout setting power well state %08x (%08x)\n",
6494 			  state,
6495 			  vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ));
6496 
6497 #undef COND
6498 
6499 out:
6500 	mutex_unlock(&dev_priv->rps.hw_lock);
6501 }
6502 
chv_pipe_power_well_sync_hw(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6503 static void chv_pipe_power_well_sync_hw(struct drm_i915_private *dev_priv,
6504 					struct i915_power_well *power_well)
6505 {
6506 	chv_set_pipe_power_well(dev_priv, power_well, power_well->count > 0);
6507 }
6508 
chv_pipe_power_well_enable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6509 static void chv_pipe_power_well_enable(struct drm_i915_private *dev_priv,
6510 				       struct i915_power_well *power_well)
6511 {
6512 	WARN_ON_ONCE(power_well->data != PIPE_A &&
6513 		     power_well->data != PIPE_B &&
6514 		     power_well->data != PIPE_C);
6515 
6516 	chv_set_pipe_power_well(dev_priv, power_well, true);
6517 }
6518 
chv_pipe_power_well_disable(struct drm_i915_private * dev_priv,struct i915_power_well * power_well)6519 static void chv_pipe_power_well_disable(struct drm_i915_private *dev_priv,
6520 					struct i915_power_well *power_well)
6521 {
6522 	WARN_ON_ONCE(power_well->data != PIPE_A &&
6523 		     power_well->data != PIPE_B &&
6524 		     power_well->data != PIPE_C);
6525 
6526 	chv_set_pipe_power_well(dev_priv, power_well, false);
6527 }
6528 
intel_display_power_get(struct drm_i915_private * dev_priv,enum intel_display_power_domain domain)6529 void intel_display_power_get(struct drm_i915_private *dev_priv,
6530 			     enum intel_display_power_domain domain)
6531 {
6532 	struct i915_power_domains *power_domains;
6533 	struct i915_power_well *power_well;
6534 	int i;
6535 
6536 	intel_runtime_pm_get(dev_priv);
6537 
6538 	power_domains = &dev_priv->power_domains;
6539 
6540 	mutex_lock(&power_domains->lock);
6541 
6542 	for_each_power_well(i, power_well, BIT(domain), power_domains) {
6543 		if (!power_well->count++) {
6544 			DRM_DEBUG_KMS("enabling %s\n", power_well->name);
6545 			power_well->ops->enable(dev_priv, power_well);
6546 			power_well->hw_enabled = true;
6547 		}
6548 	}
6549 
6550 	power_domains->domain_use_count[domain]++;
6551 
6552 	mutex_unlock(&power_domains->lock);
6553 }
6554 
intel_display_power_put(struct drm_i915_private * dev_priv,enum intel_display_power_domain domain)6555 void intel_display_power_put(struct drm_i915_private *dev_priv,
6556 			     enum intel_display_power_domain domain)
6557 {
6558 	struct i915_power_domains *power_domains;
6559 	struct i915_power_well *power_well;
6560 	int i;
6561 
6562 	power_domains = &dev_priv->power_domains;
6563 
6564 	mutex_lock(&power_domains->lock);
6565 
6566 	WARN_ON(!power_domains->domain_use_count[domain]);
6567 	power_domains->domain_use_count[domain]--;
6568 
6569 	for_each_power_well_rev(i, power_well, BIT(domain), power_domains) {
6570 		WARN_ON(!power_well->count);
6571 
6572 		if (!--power_well->count && i915.disable_power_well) {
6573 			DRM_DEBUG_KMS("disabling %s\n", power_well->name);
6574 			power_well->hw_enabled = false;
6575 			power_well->ops->disable(dev_priv, power_well);
6576 		}
6577 	}
6578 
6579 	mutex_unlock(&power_domains->lock);
6580 
6581 	intel_runtime_pm_put(dev_priv);
6582 }
6583 
6584 static struct i915_power_domains *hsw_pwr;
6585 
6586 /* Display audio driver power well request */
i915_request_power_well(void)6587 int i915_request_power_well(void)
6588 {
6589 	struct drm_i915_private *dev_priv;
6590 
6591 	if (!hsw_pwr)
6592 		return -ENODEV;
6593 
6594 	dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6595 				power_domains);
6596 	intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
6597 	return 0;
6598 }
6599 EXPORT_SYMBOL_GPL(i915_request_power_well);
6600 
6601 /* Display audio driver power well release */
i915_release_power_well(void)6602 int i915_release_power_well(void)
6603 {
6604 	struct drm_i915_private *dev_priv;
6605 
6606 	if (!hsw_pwr)
6607 		return -ENODEV;
6608 
6609 	dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6610 				power_domains);
6611 	intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
6612 	return 0;
6613 }
6614 EXPORT_SYMBOL_GPL(i915_release_power_well);
6615 
6616 /*
6617  * Private interface for the audio driver to get CDCLK in kHz.
6618  *
6619  * Caller must request power well using i915_request_power_well() prior to
6620  * making the call.
6621  */
i915_get_cdclk_freq(void)6622 int i915_get_cdclk_freq(void)
6623 {
6624 	struct drm_i915_private *dev_priv;
6625 
6626 	if (!hsw_pwr)
6627 		return -ENODEV;
6628 
6629 	dev_priv = container_of(hsw_pwr, struct drm_i915_private,
6630 				power_domains);
6631 
6632 	return intel_ddi_get_cdclk_freq(dev_priv);
6633 }
6634 EXPORT_SYMBOL_GPL(i915_get_cdclk_freq);
6635 
6636 
6637 #define POWER_DOMAIN_MASK (BIT(POWER_DOMAIN_NUM) - 1)
6638 
6639 #define HSW_ALWAYS_ON_POWER_DOMAINS (			\
6640 	BIT(POWER_DOMAIN_PIPE_A) |			\
6641 	BIT(POWER_DOMAIN_TRANSCODER_EDP) |		\
6642 	BIT(POWER_DOMAIN_PORT_DDI_A_2_LANES) |		\
6643 	BIT(POWER_DOMAIN_PORT_DDI_A_4_LANES) |		\
6644 	BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |		\
6645 	BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |		\
6646 	BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |		\
6647 	BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |		\
6648 	BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) |		\
6649 	BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |		\
6650 	BIT(POWER_DOMAIN_PORT_CRT) |			\
6651 	BIT(POWER_DOMAIN_PLLS) |			\
6652 	BIT(POWER_DOMAIN_INIT))
6653 #define HSW_DISPLAY_POWER_DOMAINS (				\
6654 	(POWER_DOMAIN_MASK & ~HSW_ALWAYS_ON_POWER_DOMAINS) |	\
6655 	BIT(POWER_DOMAIN_INIT))
6656 
6657 #define BDW_ALWAYS_ON_POWER_DOMAINS (			\
6658 	HSW_ALWAYS_ON_POWER_DOMAINS |			\
6659 	BIT(POWER_DOMAIN_PIPE_A_PANEL_FITTER))
6660 #define BDW_DISPLAY_POWER_DOMAINS (				\
6661 	(POWER_DOMAIN_MASK & ~BDW_ALWAYS_ON_POWER_DOMAINS) |	\
6662 	BIT(POWER_DOMAIN_INIT))
6663 
6664 #define VLV_ALWAYS_ON_POWER_DOMAINS	BIT(POWER_DOMAIN_INIT)
6665 #define VLV_DISPLAY_POWER_DOMAINS	POWER_DOMAIN_MASK
6666 
6667 #define VLV_DPIO_CMN_BC_POWER_DOMAINS (		\
6668 	BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |	\
6669 	BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |	\
6670 	BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |	\
6671 	BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |	\
6672 	BIT(POWER_DOMAIN_PORT_CRT) |		\
6673 	BIT(POWER_DOMAIN_INIT))
6674 
6675 #define VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS (	\
6676 	BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |	\
6677 	BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |	\
6678 	BIT(POWER_DOMAIN_INIT))
6679 
6680 #define VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS (	\
6681 	BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |	\
6682 	BIT(POWER_DOMAIN_INIT))
6683 
6684 #define VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS (	\
6685 	BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |	\
6686 	BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |	\
6687 	BIT(POWER_DOMAIN_INIT))
6688 
6689 #define VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS (	\
6690 	BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |	\
6691 	BIT(POWER_DOMAIN_INIT))
6692 
6693 #define CHV_PIPE_A_POWER_DOMAINS (	\
6694 	BIT(POWER_DOMAIN_PIPE_A) |	\
6695 	BIT(POWER_DOMAIN_INIT))
6696 
6697 #define CHV_PIPE_B_POWER_DOMAINS (	\
6698 	BIT(POWER_DOMAIN_PIPE_B) |	\
6699 	BIT(POWER_DOMAIN_INIT))
6700 
6701 #define CHV_PIPE_C_POWER_DOMAINS (	\
6702 	BIT(POWER_DOMAIN_PIPE_C) |	\
6703 	BIT(POWER_DOMAIN_INIT))
6704 
6705 #define CHV_DPIO_CMN_BC_POWER_DOMAINS (		\
6706 	BIT(POWER_DOMAIN_PORT_DDI_B_2_LANES) |	\
6707 	BIT(POWER_DOMAIN_PORT_DDI_B_4_LANES) |	\
6708 	BIT(POWER_DOMAIN_PORT_DDI_C_2_LANES) |	\
6709 	BIT(POWER_DOMAIN_PORT_DDI_C_4_LANES) |	\
6710 	BIT(POWER_DOMAIN_INIT))
6711 
6712 #define CHV_DPIO_CMN_D_POWER_DOMAINS (		\
6713 	BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) |	\
6714 	BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |	\
6715 	BIT(POWER_DOMAIN_INIT))
6716 
6717 #define CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS (	\
6718 	BIT(POWER_DOMAIN_PORT_DDI_D_2_LANES) |	\
6719 	BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |	\
6720 	BIT(POWER_DOMAIN_INIT))
6721 
6722 #define CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS (	\
6723 	BIT(POWER_DOMAIN_PORT_DDI_D_4_LANES) |	\
6724 	BIT(POWER_DOMAIN_INIT))
6725 
6726 static const struct i915_power_well_ops i9xx_always_on_power_well_ops = {
6727 	.sync_hw = i9xx_always_on_power_well_noop,
6728 	.enable = i9xx_always_on_power_well_noop,
6729 	.disable = i9xx_always_on_power_well_noop,
6730 	.is_enabled = i9xx_always_on_power_well_enabled,
6731 };
6732 
6733 static const struct i915_power_well_ops chv_pipe_power_well_ops = {
6734 	.sync_hw = chv_pipe_power_well_sync_hw,
6735 	.enable = chv_pipe_power_well_enable,
6736 	.disable = chv_pipe_power_well_disable,
6737 	.is_enabled = chv_pipe_power_well_enabled,
6738 };
6739 
6740 static const struct i915_power_well_ops chv_dpio_cmn_power_well_ops = {
6741 	.sync_hw = vlv_power_well_sync_hw,
6742 	.enable = chv_dpio_cmn_power_well_enable,
6743 	.disable = chv_dpio_cmn_power_well_disable,
6744 	.is_enabled = vlv_power_well_enabled,
6745 };
6746 
6747 static struct i915_power_well i9xx_always_on_power_well[] = {
6748 	{
6749 		.name = "always-on",
6750 		.always_on = 1,
6751 		.domains = POWER_DOMAIN_MASK,
6752 		.ops = &i9xx_always_on_power_well_ops,
6753 	},
6754 };
6755 
6756 static const struct i915_power_well_ops hsw_power_well_ops = {
6757 	.sync_hw = hsw_power_well_sync_hw,
6758 	.enable = hsw_power_well_enable,
6759 	.disable = hsw_power_well_disable,
6760 	.is_enabled = hsw_power_well_enabled,
6761 };
6762 
6763 static struct i915_power_well hsw_power_wells[] = {
6764 	{
6765 		.name = "always-on",
6766 		.always_on = 1,
6767 		.domains = HSW_ALWAYS_ON_POWER_DOMAINS,
6768 		.ops = &i9xx_always_on_power_well_ops,
6769 	},
6770 	{
6771 		.name = "display",
6772 		.domains = HSW_DISPLAY_POWER_DOMAINS,
6773 		.ops = &hsw_power_well_ops,
6774 	},
6775 };
6776 
6777 static struct i915_power_well bdw_power_wells[] = {
6778 	{
6779 		.name = "always-on",
6780 		.always_on = 1,
6781 		.domains = BDW_ALWAYS_ON_POWER_DOMAINS,
6782 		.ops = &i9xx_always_on_power_well_ops,
6783 	},
6784 	{
6785 		.name = "display",
6786 		.domains = BDW_DISPLAY_POWER_DOMAINS,
6787 		.ops = &hsw_power_well_ops,
6788 	},
6789 };
6790 
6791 static const struct i915_power_well_ops vlv_display_power_well_ops = {
6792 	.sync_hw = vlv_power_well_sync_hw,
6793 	.enable = vlv_display_power_well_enable,
6794 	.disable = vlv_display_power_well_disable,
6795 	.is_enabled = vlv_power_well_enabled,
6796 };
6797 
6798 static const struct i915_power_well_ops vlv_dpio_cmn_power_well_ops = {
6799 	.sync_hw = vlv_power_well_sync_hw,
6800 	.enable = vlv_dpio_cmn_power_well_enable,
6801 	.disable = vlv_dpio_cmn_power_well_disable,
6802 	.is_enabled = vlv_power_well_enabled,
6803 };
6804 
6805 static const struct i915_power_well_ops vlv_dpio_power_well_ops = {
6806 	.sync_hw = vlv_power_well_sync_hw,
6807 	.enable = vlv_power_well_enable,
6808 	.disable = vlv_power_well_disable,
6809 	.is_enabled = vlv_power_well_enabled,
6810 };
6811 
6812 static struct i915_power_well vlv_power_wells[] = {
6813 	{
6814 		.name = "always-on",
6815 		.always_on = 1,
6816 		.domains = VLV_ALWAYS_ON_POWER_DOMAINS,
6817 		.ops = &i9xx_always_on_power_well_ops,
6818 	},
6819 	{
6820 		.name = "display",
6821 		.domains = VLV_DISPLAY_POWER_DOMAINS,
6822 		.data = PUNIT_POWER_WELL_DISP2D,
6823 		.ops = &vlv_display_power_well_ops,
6824 	},
6825 	{
6826 		.name = "dpio-tx-b-01",
6827 		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6828 			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6829 			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6830 			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6831 		.ops = &vlv_dpio_power_well_ops,
6832 		.data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
6833 	},
6834 	{
6835 		.name = "dpio-tx-b-23",
6836 		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6837 			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6838 			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6839 			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6840 		.ops = &vlv_dpio_power_well_ops,
6841 		.data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
6842 	},
6843 	{
6844 		.name = "dpio-tx-c-01",
6845 		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6846 			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6847 			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6848 			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6849 		.ops = &vlv_dpio_power_well_ops,
6850 		.data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
6851 	},
6852 	{
6853 		.name = "dpio-tx-c-23",
6854 		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6855 			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS |
6856 			   VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6857 			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6858 		.ops = &vlv_dpio_power_well_ops,
6859 		.data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
6860 	},
6861 	{
6862 		.name = "dpio-common",
6863 		.domains = VLV_DPIO_CMN_BC_POWER_DOMAINS,
6864 		.data = PUNIT_POWER_WELL_DPIO_CMN_BC,
6865 		.ops = &vlv_dpio_cmn_power_well_ops,
6866 	},
6867 };
6868 
6869 static struct i915_power_well chv_power_wells[] = {
6870 	{
6871 		.name = "always-on",
6872 		.always_on = 1,
6873 		.domains = VLV_ALWAYS_ON_POWER_DOMAINS,
6874 		.ops = &i9xx_always_on_power_well_ops,
6875 	},
6876 #if 0
6877 	{
6878 		.name = "display",
6879 		.domains = VLV_DISPLAY_POWER_DOMAINS,
6880 		.data = PUNIT_POWER_WELL_DISP2D,
6881 		.ops = &vlv_display_power_well_ops,
6882 	},
6883 	{
6884 		.name = "pipe-a",
6885 		.domains = CHV_PIPE_A_POWER_DOMAINS,
6886 		.data = PIPE_A,
6887 		.ops = &chv_pipe_power_well_ops,
6888 	},
6889 	{
6890 		.name = "pipe-b",
6891 		.domains = CHV_PIPE_B_POWER_DOMAINS,
6892 		.data = PIPE_B,
6893 		.ops = &chv_pipe_power_well_ops,
6894 	},
6895 	{
6896 		.name = "pipe-c",
6897 		.domains = CHV_PIPE_C_POWER_DOMAINS,
6898 		.data = PIPE_C,
6899 		.ops = &chv_pipe_power_well_ops,
6900 	},
6901 #endif
6902 	{
6903 		.name = "dpio-common-bc",
6904 		/*
6905 		 * XXX: cmnreset for one PHY seems to disturb the other.
6906 		 * As a workaround keep both powered on at the same
6907 		 * time for now.
6908 		 */
6909 		.domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
6910 		.data = PUNIT_POWER_WELL_DPIO_CMN_BC,
6911 		.ops = &chv_dpio_cmn_power_well_ops,
6912 	},
6913 	{
6914 		.name = "dpio-common-d",
6915 		/*
6916 		 * XXX: cmnreset for one PHY seems to disturb the other.
6917 		 * As a workaround keep both powered on at the same
6918 		 * time for now.
6919 		 */
6920 		.domains = CHV_DPIO_CMN_BC_POWER_DOMAINS | CHV_DPIO_CMN_D_POWER_DOMAINS,
6921 		.data = PUNIT_POWER_WELL_DPIO_CMN_D,
6922 		.ops = &chv_dpio_cmn_power_well_ops,
6923 	},
6924 #if 0
6925 	{
6926 		.name = "dpio-tx-b-01",
6927 		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6928 			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
6929 		.ops = &vlv_dpio_power_well_ops,
6930 		.data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_01,
6931 	},
6932 	{
6933 		.name = "dpio-tx-b-23",
6934 		.domains = VLV_DPIO_TX_B_LANES_01_POWER_DOMAINS |
6935 			   VLV_DPIO_TX_B_LANES_23_POWER_DOMAINS,
6936 		.ops = &vlv_dpio_power_well_ops,
6937 		.data = PUNIT_POWER_WELL_DPIO_TX_B_LANES_23,
6938 	},
6939 	{
6940 		.name = "dpio-tx-c-01",
6941 		.domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6942 			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6943 		.ops = &vlv_dpio_power_well_ops,
6944 		.data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_01,
6945 	},
6946 	{
6947 		.name = "dpio-tx-c-23",
6948 		.domains = VLV_DPIO_TX_C_LANES_01_POWER_DOMAINS |
6949 			   VLV_DPIO_TX_C_LANES_23_POWER_DOMAINS,
6950 		.ops = &vlv_dpio_power_well_ops,
6951 		.data = PUNIT_POWER_WELL_DPIO_TX_C_LANES_23,
6952 	},
6953 	{
6954 		.name = "dpio-tx-d-01",
6955 		.domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
6956 			   CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
6957 		.ops = &vlv_dpio_power_well_ops,
6958 		.data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_01,
6959 	},
6960 	{
6961 		.name = "dpio-tx-d-23",
6962 		.domains = CHV_DPIO_TX_D_LANES_01_POWER_DOMAINS |
6963 			   CHV_DPIO_TX_D_LANES_23_POWER_DOMAINS,
6964 		.ops = &vlv_dpio_power_well_ops,
6965 		.data = PUNIT_POWER_WELL_DPIO_TX_D_LANES_23,
6966 	},
6967 #endif
6968 };
6969 
lookup_power_well(struct drm_i915_private * dev_priv,enum punit_power_well power_well_id)6970 static struct i915_power_well *lookup_power_well(struct drm_i915_private *dev_priv,
6971 						 enum punit_power_well power_well_id)
6972 {
6973 	struct i915_power_domains *power_domains = &dev_priv->power_domains;
6974 	struct i915_power_well *power_well;
6975 	int i;
6976 
6977 	for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
6978 		if (power_well->data == power_well_id)
6979 			return power_well;
6980 	}
6981 
6982 	return NULL;
6983 }
6984 
6985 #define set_power_wells(power_domains, __power_wells) ({		\
6986 	(power_domains)->power_wells = (__power_wells);			\
6987 	(power_domains)->power_well_count = ARRAY_SIZE(__power_wells);	\
6988 })
6989 
intel_power_domains_init(struct drm_i915_private * dev_priv)6990 int intel_power_domains_init(struct drm_i915_private *dev_priv)
6991 {
6992 	struct i915_power_domains *power_domains = &dev_priv->power_domains;
6993 
6994 	mutex_init(&power_domains->lock);
6995 
6996 	/*
6997 	 * The enabling order will be from lower to higher indexed wells,
6998 	 * the disabling order is reversed.
6999 	 */
7000 	if (IS_HASWELL(dev_priv->dev)) {
7001 		set_power_wells(power_domains, hsw_power_wells);
7002 		hsw_pwr = power_domains;
7003 	} else if (IS_BROADWELL(dev_priv->dev)) {
7004 		set_power_wells(power_domains, bdw_power_wells);
7005 		hsw_pwr = power_domains;
7006 	} else if (IS_CHERRYVIEW(dev_priv->dev)) {
7007 		set_power_wells(power_domains, chv_power_wells);
7008 	} else if (IS_VALLEYVIEW(dev_priv->dev)) {
7009 		set_power_wells(power_domains, vlv_power_wells);
7010 	} else {
7011 		set_power_wells(power_domains, i9xx_always_on_power_well);
7012 	}
7013 
7014 	return 0;
7015 }
7016 
intel_power_domains_remove(struct drm_i915_private * dev_priv)7017 void intel_power_domains_remove(struct drm_i915_private *dev_priv)
7018 {
7019 	hsw_pwr = NULL;
7020 }
7021 
intel_power_domains_resume(struct drm_i915_private * dev_priv)7022 static void intel_power_domains_resume(struct drm_i915_private *dev_priv)
7023 {
7024 	struct i915_power_domains *power_domains = &dev_priv->power_domains;
7025 	struct i915_power_well *power_well;
7026 	int i;
7027 
7028 	mutex_lock(&power_domains->lock);
7029 	for_each_power_well(i, power_well, POWER_DOMAIN_MASK, power_domains) {
7030 		power_well->ops->sync_hw(dev_priv, power_well);
7031 		power_well->hw_enabled = power_well->ops->is_enabled(dev_priv,
7032 								     power_well);
7033 	}
7034 	mutex_unlock(&power_domains->lock);
7035 }
7036 
vlv_cmnlane_wa(struct drm_i915_private * dev_priv)7037 static void vlv_cmnlane_wa(struct drm_i915_private *dev_priv)
7038 {
7039 	struct i915_power_well *cmn =
7040 		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC);
7041 	struct i915_power_well *disp2d =
7042 		lookup_power_well(dev_priv, PUNIT_POWER_WELL_DISP2D);
7043 
7044 	/* nothing to do if common lane is already off */
7045 	if (!cmn->ops->is_enabled(dev_priv, cmn))
7046 		return;
7047 
7048 	/* If the display might be already active skip this */
7049 	if (disp2d->ops->is_enabled(dev_priv, disp2d) &&
7050 	    I915_READ(DPIO_CTL) & DPIO_CMNRST)
7051 		return;
7052 
7053 	DRM_DEBUG_KMS("toggling display PHY side reset\n");
7054 
7055 	/* cmnlane needs DPLL registers */
7056 	disp2d->ops->enable(dev_priv, disp2d);
7057 
7058 	/*
7059 	 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
7060 	 * Need to assert and de-assert PHY SB reset by gating the
7061 	 * common lane power, then un-gating it.
7062 	 * Simply ungating isn't enough to reset the PHY enough to get
7063 	 * ports and lanes running.
7064 	 */
7065 	cmn->ops->disable(dev_priv, cmn);
7066 }
7067 
intel_power_domains_init_hw(struct drm_i915_private * dev_priv)7068 void intel_power_domains_init_hw(struct drm_i915_private *dev_priv)
7069 {
7070 	struct drm_device *dev = dev_priv->dev;
7071 	struct i915_power_domains *power_domains = &dev_priv->power_domains;
7072 
7073 	power_domains->initializing = true;
7074 
7075 	if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
7076 		mutex_lock(&power_domains->lock);
7077 		vlv_cmnlane_wa(dev_priv);
7078 		mutex_unlock(&power_domains->lock);
7079 	}
7080 
7081 	/* For now, we need the power well to be always enabled. */
7082 	intel_display_set_init_power(dev_priv, true);
7083 	intel_power_domains_resume(dev_priv);
7084 	power_domains->initializing = false;
7085 }
7086 
intel_aux_display_runtime_get(struct drm_i915_private * dev_priv)7087 void intel_aux_display_runtime_get(struct drm_i915_private *dev_priv)
7088 {
7089 	intel_runtime_pm_get(dev_priv);
7090 }
7091 
intel_aux_display_runtime_put(struct drm_i915_private * dev_priv)7092 void intel_aux_display_runtime_put(struct drm_i915_private *dev_priv)
7093 {
7094 	intel_runtime_pm_put(dev_priv);
7095 }
7096 
intel_runtime_pm_get(struct drm_i915_private * dev_priv)7097 void intel_runtime_pm_get(struct drm_i915_private *dev_priv)
7098 {
7099 	struct drm_device *dev = dev_priv->dev;
7100 	struct device *device = &dev->pdev->dev;
7101 
7102 	if (!HAS_RUNTIME_PM(dev))
7103 		return;
7104 
7105 	pm_runtime_get_sync(device);
7106 	WARN(dev_priv->pm.suspended, "Device still suspended.\n");
7107 }
7108 
intel_runtime_pm_get_noresume(struct drm_i915_private * dev_priv)7109 void intel_runtime_pm_get_noresume(struct drm_i915_private *dev_priv)
7110 {
7111 	struct drm_device *dev = dev_priv->dev;
7112 	struct device *device = &dev->pdev->dev;
7113 
7114 	if (!HAS_RUNTIME_PM(dev))
7115 		return;
7116 
7117 	WARN(dev_priv->pm.suspended, "Getting nosync-ref while suspended.\n");
7118 	pm_runtime_get_noresume(device);
7119 }
7120 
intel_runtime_pm_put(struct drm_i915_private * dev_priv)7121 void intel_runtime_pm_put(struct drm_i915_private *dev_priv)
7122 {
7123 	struct drm_device *dev = dev_priv->dev;
7124 	struct device *device = &dev->pdev->dev;
7125 
7126 	if (!HAS_RUNTIME_PM(dev))
7127 		return;
7128 
7129 	pm_runtime_mark_last_busy(device);
7130 	pm_runtime_put_autosuspend(device);
7131 }
7132 
intel_init_runtime_pm(struct drm_i915_private * dev_priv)7133 void intel_init_runtime_pm(struct drm_i915_private *dev_priv)
7134 {
7135 	struct drm_device *dev = dev_priv->dev;
7136 	struct device *device = &dev->pdev->dev;
7137 
7138 	if (!HAS_RUNTIME_PM(dev))
7139 		return;
7140 
7141 	pm_runtime_set_active(device);
7142 
7143 	/*
7144 	 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
7145 	 * requirement.
7146 	 */
7147 	if (!intel_enable_rc6(dev)) {
7148 		DRM_INFO("RC6 disabled, disabling runtime PM support\n");
7149 		return;
7150 	}
7151 
7152 	pm_runtime_set_autosuspend_delay(device, 10000); /* 10s */
7153 	pm_runtime_mark_last_busy(device);
7154 	pm_runtime_use_autosuspend(device);
7155 
7156 	pm_runtime_put_autosuspend(device);
7157 }
7158 
intel_fini_runtime_pm(struct drm_i915_private * dev_priv)7159 void intel_fini_runtime_pm(struct drm_i915_private *dev_priv)
7160 {
7161 	struct drm_device *dev = dev_priv->dev;
7162 	struct device *device = &dev->pdev->dev;
7163 
7164 	if (!HAS_RUNTIME_PM(dev))
7165 		return;
7166 
7167 	if (!intel_enable_rc6(dev))
7168 		return;
7169 
7170 	/* Make sure we're not suspended first. */
7171 	pm_runtime_get_sync(device);
7172 	pm_runtime_disable(device);
7173 }
7174 
7175 /* Set up chip specific power management-related functions */
intel_init_pm(struct drm_device * dev)7176 void intel_init_pm(struct drm_device *dev)
7177 {
7178 	struct drm_i915_private *dev_priv = dev->dev_private;
7179 
7180 	if (HAS_FBC(dev)) {
7181 		if (INTEL_INFO(dev)->gen >= 7) {
7182 			dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
7183 			dev_priv->display.enable_fbc = gen7_enable_fbc;
7184 			dev_priv->display.disable_fbc = ironlake_disable_fbc;
7185 		} else if (INTEL_INFO(dev)->gen >= 5) {
7186 			dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
7187 			dev_priv->display.enable_fbc = ironlake_enable_fbc;
7188 			dev_priv->display.disable_fbc = ironlake_disable_fbc;
7189 		} else if (IS_GM45(dev)) {
7190 			dev_priv->display.fbc_enabled = g4x_fbc_enabled;
7191 			dev_priv->display.enable_fbc = g4x_enable_fbc;
7192 			dev_priv->display.disable_fbc = g4x_disable_fbc;
7193 		} else {
7194 			dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
7195 			dev_priv->display.enable_fbc = i8xx_enable_fbc;
7196 			dev_priv->display.disable_fbc = i8xx_disable_fbc;
7197 
7198 			/* This value was pulled out of someone's hat */
7199 			I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
7200 		}
7201 	}
7202 
7203 	/* For cxsr */
7204 	if (IS_PINEVIEW(dev))
7205 		i915_pineview_get_mem_freq(dev);
7206 	else if (IS_GEN5(dev))
7207 		i915_ironlake_get_mem_freq(dev);
7208 
7209 	/* For FIFO watermark updates */
7210 	if (HAS_PCH_SPLIT(dev)) {
7211 		ilk_setup_wm_latency(dev);
7212 
7213 		if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7214 		     dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7215 		    (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7216 		     dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7217 			dev_priv->display.update_wm = ilk_update_wm;
7218 			dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
7219 		} else {
7220 			DRM_DEBUG_KMS("Failed to read display plane latency. "
7221 				      "Disable CxSR\n");
7222 		}
7223 
7224 		if (IS_GEN5(dev))
7225 			dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7226 		else if (IS_GEN6(dev))
7227 			dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7228 		else if (IS_IVYBRIDGE(dev))
7229 			dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7230 		else if (IS_HASWELL(dev))
7231 			dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7232 		else if (INTEL_INFO(dev)->gen == 8)
7233 			dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7234 	} else if (IS_CHERRYVIEW(dev)) {
7235 		dev_priv->display.update_wm = cherryview_update_wm;
7236 		dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
7237 		dev_priv->display.init_clock_gating =
7238 			cherryview_init_clock_gating;
7239 	} else if (IS_VALLEYVIEW(dev)) {
7240 		dev_priv->display.update_wm = valleyview_update_wm;
7241 		dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
7242 		dev_priv->display.init_clock_gating =
7243 			valleyview_init_clock_gating;
7244 	} else if (IS_PINEVIEW(dev)) {
7245 		if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7246 					    dev_priv->is_ddr3,
7247 					    dev_priv->fsb_freq,
7248 					    dev_priv->mem_freq)) {
7249 			DRM_INFO("failed to find known CxSR latency "
7250 				 "(found ddr%s fsb freq %d, mem freq %d), "
7251 				 "disabling CxSR\n",
7252 				 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7253 				 dev_priv->fsb_freq, dev_priv->mem_freq);
7254 			/* Disable CxSR and never update its watermark again */
7255 			intel_set_memory_cxsr(dev_priv, false);
7256 			dev_priv->display.update_wm = NULL;
7257 		} else
7258 			dev_priv->display.update_wm = pineview_update_wm;
7259 		dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7260 	} else if (IS_G4X(dev)) {
7261 		dev_priv->display.update_wm = g4x_update_wm;
7262 		dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7263 	} else if (IS_GEN4(dev)) {
7264 		dev_priv->display.update_wm = i965_update_wm;
7265 		if (IS_CRESTLINE(dev))
7266 			dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7267 		else if (IS_BROADWATER(dev))
7268 			dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7269 	} else if (IS_GEN3(dev)) {
7270 		dev_priv->display.update_wm = i9xx_update_wm;
7271 		dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7272 		dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7273 	} else if (IS_GEN2(dev)) {
7274 		if (INTEL_INFO(dev)->num_pipes == 1) {
7275 			dev_priv->display.update_wm = i845_update_wm;
7276 			dev_priv->display.get_fifo_size = i845_get_fifo_size;
7277 		} else {
7278 			dev_priv->display.update_wm = i9xx_update_wm;
7279 			dev_priv->display.get_fifo_size = i830_get_fifo_size;
7280 		}
7281 
7282 		if (IS_I85X(dev) || IS_I865G(dev))
7283 			dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7284 		else
7285 			dev_priv->display.init_clock_gating = i830_init_clock_gating;
7286 	} else {
7287 		DRM_ERROR("unexpected fall-through in intel_init_pm\n");
7288 	}
7289 }
7290 
sandybridge_pcode_read(struct drm_i915_private * dev_priv,u8 mbox,u32 * val)7291 int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u8 mbox, u32 *val)
7292 {
7293 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7294 
7295 	if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7296 		DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7297 		return -EAGAIN;
7298 	}
7299 
7300 	I915_WRITE(GEN6_PCODE_DATA, *val);
7301 	I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7302 
7303 	if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7304 		     500)) {
7305 		DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7306 		return -ETIMEDOUT;
7307 	}
7308 
7309 	*val = I915_READ(GEN6_PCODE_DATA);
7310 	I915_WRITE(GEN6_PCODE_DATA, 0);
7311 
7312 	return 0;
7313 }
7314 
sandybridge_pcode_write(struct drm_i915_private * dev_priv,u8 mbox,u32 val)7315 int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u8 mbox, u32 val)
7316 {
7317 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7318 
7319 	if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7320 		DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7321 		return -EAGAIN;
7322 	}
7323 
7324 	I915_WRITE(GEN6_PCODE_DATA, val);
7325 	I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7326 
7327 	if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7328 		     500)) {
7329 		DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7330 		return -ETIMEDOUT;
7331 	}
7332 
7333 	I915_WRITE(GEN6_PCODE_DATA, 0);
7334 
7335 	return 0;
7336 }
7337 
byt_gpu_freq(struct drm_i915_private * dev_priv,int val)7338 static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7339 {
7340 	int div;
7341 
7342 	/* 4 x czclk */
7343 	switch (dev_priv->mem_freq) {
7344 	case 800:
7345 		div = 10;
7346 		break;
7347 	case 1066:
7348 		div = 12;
7349 		break;
7350 	case 1333:
7351 		div = 16;
7352 		break;
7353 	default:
7354 		return -1;
7355 	}
7356 
7357 	return DIV_ROUND_CLOSEST(dev_priv->mem_freq * (val + 6 - 0xbd), 4 * div);
7358 }
7359 
byt_freq_opcode(struct drm_i915_private * dev_priv,int val)7360 static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
7361 {
7362 	int mul;
7363 
7364 	/* 4 x czclk */
7365 	switch (dev_priv->mem_freq) {
7366 	case 800:
7367 		mul = 10;
7368 		break;
7369 	case 1066:
7370 		mul = 12;
7371 		break;
7372 	case 1333:
7373 		mul = 16;
7374 		break;
7375 	default:
7376 		return -1;
7377 	}
7378 
7379 	return DIV_ROUND_CLOSEST(4 * mul * val, dev_priv->mem_freq) + 0xbd - 6;
7380 }
7381 
chv_gpu_freq(struct drm_i915_private * dev_priv,int val)7382 static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7383 {
7384 	int div, freq;
7385 
7386 	switch (dev_priv->rps.cz_freq) {
7387 	case 200:
7388 		div = 5;
7389 		break;
7390 	case 267:
7391 		div = 6;
7392 		break;
7393 	case 320:
7394 	case 333:
7395 	case 400:
7396 		div = 8;
7397 		break;
7398 	default:
7399 		return -1;
7400 	}
7401 
7402 	freq = (DIV_ROUND_CLOSEST((dev_priv->rps.cz_freq * val), 2 * div) / 2);
7403 
7404 	return freq;
7405 }
7406 
chv_freq_opcode(struct drm_i915_private * dev_priv,int val)7407 static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7408 {
7409 	int mul, opcode;
7410 
7411 	switch (dev_priv->rps.cz_freq) {
7412 	case 200:
7413 		mul = 5;
7414 		break;
7415 	case 267:
7416 		mul = 6;
7417 		break;
7418 	case 320:
7419 	case 333:
7420 	case 400:
7421 		mul = 8;
7422 		break;
7423 	default:
7424 		return -1;
7425 	}
7426 
7427 	/* CHV needs even values */
7428 	opcode = (DIV_ROUND_CLOSEST((val * 2 * mul), dev_priv->rps.cz_freq) * 2);
7429 
7430 	return opcode;
7431 }
7432 
vlv_gpu_freq(struct drm_i915_private * dev_priv,int val)7433 int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7434 {
7435 	int ret = -1;
7436 
7437 	if (IS_CHERRYVIEW(dev_priv->dev))
7438 		ret = chv_gpu_freq(dev_priv, val);
7439 	else if (IS_VALLEYVIEW(dev_priv->dev))
7440 		ret = byt_gpu_freq(dev_priv, val);
7441 
7442 	return ret;
7443 }
7444 
vlv_freq_opcode(struct drm_i915_private * dev_priv,int val)7445 int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7446 {
7447 	int ret = -1;
7448 
7449 	if (IS_CHERRYVIEW(dev_priv->dev))
7450 		ret = chv_freq_opcode(dev_priv, val);
7451 	else if (IS_VALLEYVIEW(dev_priv->dev))
7452 		ret = byt_freq_opcode(dev_priv, val);
7453 
7454 	return ret;
7455 }
7456 
intel_pm_setup(struct drm_device * dev)7457 void intel_pm_setup(struct drm_device *dev)
7458 {
7459 	struct drm_i915_private *dev_priv = dev->dev_private;
7460 
7461 	mutex_init(&dev_priv->rps.hw_lock);
7462 
7463 	INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7464 			  intel_gen6_powersave_work);
7465 
7466 	dev_priv->pm.suspended = false;
7467 	dev_priv->pm._irqs_disabled = false;
7468 }
7469