Subversion Repositories Kolibri OS

Rev

Rev 5354 | Rev 6660 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5354 Rev 6084
1
/*
1
/*
2
 * Copyright © 2012 Intel Corporation
2
 * Copyright © 2012 Intel Corporation
3
 *
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a
4
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * copy of this software and associated documentation files (the "Software"),
5
 * copy of this software and associated documentation files (the "Software"),
6
 * to deal in the Software without restriction, including without limitation
6
 * to deal in the Software without restriction, including without limitation
7
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
9
 * Software is furnished to do so, subject to the following conditions:
10
 *
10
 *
11
 * The above copyright notice and this permission notice (including the next
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
12
 * paragraph) shall be included in all copies or substantial portions of the
13
 * Software.
13
 * Software.
14
 *
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
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
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
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
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
 * IN THE SOFTWARE.
21
 * IN THE SOFTWARE.
22
 *
22
 *
23
 * Authors:
23
 * Authors:
24
 *    Eugeni Dodonov 
24
 *    Eugeni Dodonov 
25
 *
25
 *
26
 */
26
 */
27
 
27
 
28
//#include 
28
//#include 
29
#include "i915_drv.h"
29
#include "i915_drv.h"
30
#include "intel_drv.h"
30
#include "intel_drv.h"
31
#include 
-
 
32
//#include "../../../platform/x86/intel_ips.h"
31
//#include "../../../platform/x86/intel_ips.h"
33
#include 
32
#include 
34
 
-
 
35
#include 
33
 
36
 
34
 
37
#define FORCEWAKE_ACK_TIMEOUT_MS 2
35
#define FORCEWAKE_ACK_TIMEOUT_MS 2
38
 
36
 
39
void getrawmonotonic(struct timespec *ts);
37
void getrawmonotonic(struct timespec *ts);
40
 
-
 
41
union ktime {
-
 
42
    s64 tv64;
-
 
43
};
-
 
44
 
-
 
45
typedef union ktime ktime_t;        /* Kill this */
-
 
46
 
-
 
47
#define ktime_to_ns(kt)         ((kt).tv64)
-
 
48
 
-
 
49
static inline u64 ktime_get_raw_ns(void)
-
 
50
{
-
 
51
    return 0; //ktime_to_ns(ktime_get_raw());
-
 
52
}
38
 
53
/**
39
/**
54
 * RC6 is a special power stage which allows the GPU to enter an very
40
 * RC6 is a special power stage which allows the GPU to enter an very
55
 * low-voltage mode when idle, using down to 0V while at this stage.  This
41
 * low-voltage mode when idle, using down to 0V while at this stage.  This
56
 * stage is entered automatically when the GPU is idle when RC6 support is
42
 * stage is entered automatically when the GPU is idle when RC6 support is
57
 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
43
 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
58
 *
44
 *
59
 * There are different RC6 modes available in Intel GPU, which differentiate
45
 * There are different RC6 modes available in Intel GPU, which differentiate
60
 * among each other with the latency required to enter and leave RC6 and
46
 * among each other with the latency required to enter and leave RC6 and
61
 * voltage consumed by the GPU in different states.
47
 * voltage consumed by the GPU in different states.
62
 *
48
 *
63
 * The combination of the following flags define which states GPU is allowed
49
 * The combination of the following flags define which states GPU is allowed
64
 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
50
 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
65
 * RC6pp is deepest RC6. Their support by hardware varies according to the
51
 * RC6pp is deepest RC6. Their support by hardware varies according to the
66
 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
52
 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
67
 * which brings the most power savings; deeper states save more power, but
53
 * which brings the most power savings; deeper states save more power, but
68
 * require higher latency to switch to and wake up.
54
 * require higher latency to switch to and wake up.
69
 */
55
 */
70
#define INTEL_RC6_ENABLE			(1<<0)
56
#define INTEL_RC6_ENABLE			(1<<0)
71
#define INTEL_RC6p_ENABLE			(1<<1)
57
#define INTEL_RC6p_ENABLE			(1<<1)
72
#define INTEL_RC6pp_ENABLE			(1<<2)
58
#define INTEL_RC6pp_ENABLE			(1<<2)
73
 
-
 
74
/* FBC, or Frame Buffer Compression, is a technique employed to compress the
-
 
75
 * framebuffer contents in-memory, aiming at reducing the required bandwidth
-
 
76
 * during in-memory transfers and, therefore, reduce the power packet.
-
 
77
 *
-
 
78
 * The benefits of FBC are mostly visible with solid backgrounds and
-
 
79
 * variation-less patterns.
-
 
80
 *
-
 
81
 * FBC-related functionality can be enabled by the means of the
-
 
82
 * i915.i915_enable_fbc parameter
-
 
83
 */
-
 
84
 
59
 
85
static void gen9_init_clock_gating(struct drm_device *dev)
60
static void bxt_init_clock_gating(struct drm_device *dev)
86
{
61
{
87
	struct drm_i915_private *dev_priv = dev->dev_private;
62
	struct drm_i915_private *dev_priv = dev->dev_private;
88
 
-
 
89
	/*
63
 
90
	 * WaDisableSDEUnitClockGating:skl
-
 
91
	 * This seems to be a pre-production w/a.
-
 
92
	 */
64
	/* WaDisableSDEUnitClockGating:bxt */
93
	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
65
	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
94
		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
66
		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
95
 
67
 
96
	/*
68
	/*
97
	 * WaDisableDgMirrorFixInHalfSliceChicken5:skl
69
	 * FIXME:
98
	 * This is a pre-production w/a.
70
	 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
99
	 */
71
	 */
100
	I915_WRITE(GEN9_HALF_SLICE_CHICKEN5,
72
	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
101
		   I915_READ(GEN9_HALF_SLICE_CHICKEN5) &
-
 
102
		   ~GEN9_DG_MIRROR_FIX_ENABLE);
73
		   GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
103
 
-
 
104
	/* Wa4x4STCOptimizationDisable:skl */
-
 
105
	I915_WRITE(CACHE_MODE_1,
-
 
106
		   _MASKED_BIT_ENABLE(GEN8_4x4_STC_OPTIMIZATION_DISABLE));
-
 
107
}
-
 
108
 
-
 
109
static void i8xx_disable_fbc(struct drm_device *dev)
-
 
110
{
-
 
111
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
112
	u32 fbc_ctl;
-
 
113
 
-
 
114
	dev_priv->fbc.enabled = false;
-
 
115
 
-
 
116
	/* Disable compression */
-
 
117
	fbc_ctl = I915_READ(FBC_CONTROL);
-
 
118
	if ((fbc_ctl & FBC_CTL_EN) == 0)
-
 
119
		return;
-
 
120
 
-
 
121
	fbc_ctl &= ~FBC_CTL_EN;
-
 
122
	I915_WRITE(FBC_CONTROL, fbc_ctl);
-
 
123
 
-
 
124
	/* Wait for compressing bit to clear */
-
 
125
	if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
-
 
126
		DRM_DEBUG_KMS("FBC idle timed out\n");
-
 
127
		return;
-
 
128
	}
-
 
129
 
-
 
130
	DRM_DEBUG_KMS("disabled FBC\n");
-
 
131
}
-
 
132
 
-
 
133
static void i8xx_enable_fbc(struct drm_crtc *crtc)
-
 
134
{
-
 
135
	struct drm_device *dev = crtc->dev;
-
 
136
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
137
	struct drm_framebuffer *fb = crtc->primary->fb;
-
 
138
	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
-
 
139
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
 
140
	int cfb_pitch;
-
 
141
	int i;
-
 
142
	u32 fbc_ctl;
-
 
143
 
-
 
144
	dev_priv->fbc.enabled = true;
-
 
145
 
-
 
146
	cfb_pitch = dev_priv->fbc.size / FBC_LL_SIZE;
-
 
147
	if (fb->pitches[0] < cfb_pitch)
-
 
148
		cfb_pitch = fb->pitches[0];
-
 
149
 
-
 
150
	/* FBC_CTL wants 32B or 64B units */
-
 
151
	if (IS_GEN2(dev))
-
 
152
		cfb_pitch = (cfb_pitch / 32) - 1;
-
 
153
	else
-
 
154
	cfb_pitch = (cfb_pitch / 64) - 1;
-
 
155
 
-
 
156
	/* Clear old tags */
-
 
157
	for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
-
 
158
		I915_WRITE(FBC_TAG + (i * 4), 0);
-
 
159
 
-
 
160
	if (IS_GEN4(dev)) {
-
 
161
		u32 fbc_ctl2;
-
 
162
 
-
 
163
	/* Set it up... */
-
 
164
	fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
-
 
165
		fbc_ctl2 |= FBC_CTL_PLANE(intel_crtc->plane);
-
 
166
	I915_WRITE(FBC_CONTROL2, fbc_ctl2);
-
 
167
	I915_WRITE(FBC_FENCE_OFF, crtc->y);
-
 
168
	}
-
 
169
 
-
 
170
	/* enable it... */
-
 
171
	fbc_ctl = I915_READ(FBC_CONTROL);
-
 
172
	fbc_ctl &= 0x3fff << FBC_CTL_INTERVAL_SHIFT;
-
 
173
	fbc_ctl |= FBC_CTL_EN | FBC_CTL_PERIODIC;
-
 
174
	if (IS_I945GM(dev))
-
 
175
		fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
-
 
176
	fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
-
 
177
	fbc_ctl |= obj->fence_reg;
-
 
178
	I915_WRITE(FBC_CONTROL, fbc_ctl);
-
 
179
 
-
 
180
	DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %c\n",
-
 
181
		      cfb_pitch, crtc->y, plane_name(intel_crtc->plane));
-
 
182
}
-
 
183
 
-
 
184
static bool i8xx_fbc_enabled(struct drm_device *dev)
-
 
185
{
-
 
186
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
187
 
-
 
188
	return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
-
 
189
}
-
 
190
 
-
 
191
static void g4x_enable_fbc(struct drm_crtc *crtc)
-
 
192
{
-
 
193
	struct drm_device *dev = crtc->dev;
-
 
194
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
195
	struct drm_framebuffer *fb = crtc->primary->fb;
-
 
196
	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
-
 
197
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
 
198
	u32 dpfc_ctl;
-
 
199
 
-
 
200
	dev_priv->fbc.enabled = true;
-
 
201
 
-
 
202
	dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane) | DPFC_SR_EN;
-
 
203
	if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
-
 
204
		dpfc_ctl |= DPFC_CTL_LIMIT_2X;
-
 
205
	else
-
 
206
		dpfc_ctl |= DPFC_CTL_LIMIT_1X;
-
 
207
	dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
-
 
208
 
-
 
209
	I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
-
 
210
 
-
 
211
	/* enable it... */
-
 
212
	I915_WRITE(DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
-
 
213
 
-
 
214
	DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
-
 
215
}
-
 
216
 
-
 
217
static void g4x_disable_fbc(struct drm_device *dev)
-
 
218
{
-
 
219
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
220
	u32 dpfc_ctl;
-
 
221
 
-
 
222
	dev_priv->fbc.enabled = false;
-
 
223
 
-
 
224
	/* Disable compression */
-
 
225
	dpfc_ctl = I915_READ(DPFC_CONTROL);
-
 
226
	if (dpfc_ctl & DPFC_CTL_EN) {
-
 
227
		dpfc_ctl &= ~DPFC_CTL_EN;
-
 
228
		I915_WRITE(DPFC_CONTROL, dpfc_ctl);
-
 
229
 
-
 
230
		DRM_DEBUG_KMS("disabled FBC\n");
-
 
231
	}
-
 
232
}
-
 
233
 
-
 
234
static bool g4x_fbc_enabled(struct drm_device *dev)
-
 
235
{
-
 
236
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
237
 
-
 
238
	return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
-
 
239
}
-
 
240
 
-
 
241
static void sandybridge_blit_fbc_update(struct drm_device *dev)
-
 
242
{
-
 
243
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
244
	u32 blt_ecoskpd;
-
 
245
 
-
 
246
	/* Make sure blitter notifies FBC of writes */
-
 
247
 
-
 
248
	/* Blitter is part of Media powerwell on VLV. No impact of
-
 
249
	 * his param in other platforms for now */
-
 
250
	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_MEDIA);
-
 
251
 
-
 
252
	blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
-
 
253
	blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
-
 
254
		GEN6_BLITTER_LOCK_SHIFT;
-
 
255
	I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
-
 
256
	blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
-
 
257
	I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
-
 
258
	blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
-
 
259
			 GEN6_BLITTER_LOCK_SHIFT);
-
 
260
	I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
-
 
261
	POSTING_READ(GEN6_BLITTER_ECOSKPD);
-
 
262
 
-
 
263
	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_MEDIA);
-
 
264
}
-
 
265
 
-
 
266
static void ironlake_enable_fbc(struct drm_crtc *crtc)
-
 
267
{
-
 
268
	struct drm_device *dev = crtc->dev;
-
 
269
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
270
	struct drm_framebuffer *fb = crtc->primary->fb;
-
 
271
	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
-
 
272
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
 
273
	u32 dpfc_ctl;
-
 
274
 
-
 
275
	dev_priv->fbc.enabled = true;
-
 
276
 
-
 
277
	dpfc_ctl = DPFC_CTL_PLANE(intel_crtc->plane);
-
 
278
	if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
-
 
279
		dev_priv->fbc.threshold++;
-
 
280
 
-
 
281
	switch (dev_priv->fbc.threshold) {
-
 
282
	case 4:
-
 
283
	case 3:
-
 
284
		dpfc_ctl |= DPFC_CTL_LIMIT_4X;
-
 
285
		break;
-
 
286
	case 2:
-
 
287
		dpfc_ctl |= DPFC_CTL_LIMIT_2X;
-
 
288
		break;
-
 
289
	case 1:
-
 
290
		dpfc_ctl |= DPFC_CTL_LIMIT_1X;
-
 
291
		break;
-
 
292
	}
-
 
293
	dpfc_ctl |= DPFC_CTL_FENCE_EN;
-
 
294
	if (IS_GEN5(dev))
-
 
295
		dpfc_ctl |= obj->fence_reg;
-
 
296
 
-
 
297
	I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
-
 
298
	I915_WRITE(ILK_FBC_RT_BASE, i915_gem_obj_ggtt_offset(obj) | ILK_FBC_RT_VALID);
-
 
299
	/* enable it... */
-
 
300
	I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
-
 
301
 
-
 
302
	if (IS_GEN6(dev)) {
-
 
303
		I915_WRITE(SNB_DPFC_CTL_SA,
-
 
304
			   SNB_CPU_FENCE_ENABLE | obj->fence_reg);
-
 
305
		I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
-
 
306
		sandybridge_blit_fbc_update(dev);
-
 
307
	}
-
 
308
 
-
 
309
	DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
-
 
310
}
-
 
311
 
-
 
312
static void ironlake_disable_fbc(struct drm_device *dev)
-
 
313
{
-
 
314
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
315
	u32 dpfc_ctl;
-
 
316
 
-
 
317
	dev_priv->fbc.enabled = false;
-
 
318
 
-
 
319
	/* Disable compression */
-
 
320
	dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
-
 
321
	if (dpfc_ctl & DPFC_CTL_EN) {
-
 
322
		dpfc_ctl &= ~DPFC_CTL_EN;
-
 
323
		I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
-
 
324
 
-
 
325
		DRM_DEBUG_KMS("disabled FBC\n");
-
 
326
	}
-
 
327
}
-
 
328
 
-
 
329
static bool ironlake_fbc_enabled(struct drm_device *dev)
-
 
330
{
-
 
331
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
332
 
-
 
333
	return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
-
 
334
}
-
 
335
 
-
 
336
static void gen7_enable_fbc(struct drm_crtc *crtc)
-
 
337
{
-
 
338
	struct drm_device *dev = crtc->dev;
-
 
339
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
340
	struct drm_framebuffer *fb = crtc->primary->fb;
-
 
341
	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
-
 
342
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
 
343
	u32 dpfc_ctl;
-
 
344
 
-
 
345
	dev_priv->fbc.enabled = true;
-
 
346
 
-
 
347
	dpfc_ctl = IVB_DPFC_CTL_PLANE(intel_crtc->plane);
-
 
348
	if (drm_format_plane_cpp(fb->pixel_format, 0) == 2)
-
 
349
		dev_priv->fbc.threshold++;
-
 
350
 
-
 
351
	switch (dev_priv->fbc.threshold) {
-
 
352
	case 4:
-
 
353
	case 3:
-
 
354
		dpfc_ctl |= DPFC_CTL_LIMIT_4X;
-
 
355
		break;
-
 
356
	case 2:
-
 
357
		dpfc_ctl |= DPFC_CTL_LIMIT_2X;
-
 
358
		break;
-
 
359
	case 1:
-
 
360
		dpfc_ctl |= DPFC_CTL_LIMIT_1X;
-
 
361
		break;
-
 
362
	}
-
 
363
 
-
 
364
	dpfc_ctl |= IVB_DPFC_CTL_FENCE_EN;
-
 
365
 
-
 
366
	if (dev_priv->fbc.false_color)
-
 
367
		dpfc_ctl |= FBC_CTL_FALSE_COLOR;
-
 
368
 
-
 
369
	I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
-
 
370
 
-
 
371
	if (IS_IVYBRIDGE(dev)) {
-
 
372
		/* WaFbcAsynchFlipDisableFbcQueue:ivb */
-
 
373
		I915_WRITE(ILK_DISPLAY_CHICKEN1,
-
 
374
			   I915_READ(ILK_DISPLAY_CHICKEN1) |
-
 
375
			   ILK_FBCQ_DIS);
-
 
376
	} else {
-
 
377
		/* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
-
 
378
		I915_WRITE(CHICKEN_PIPESL_1(intel_crtc->pipe),
-
 
379
			   I915_READ(CHICKEN_PIPESL_1(intel_crtc->pipe)) |
-
 
380
			   HSW_FBCQ_DIS);
-
 
381
	}
-
 
382
 
-
 
383
	I915_WRITE(SNB_DPFC_CTL_SA,
-
 
384
		   SNB_CPU_FENCE_ENABLE | obj->fence_reg);
-
 
385
	I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
-
 
386
 
-
 
387
	sandybridge_blit_fbc_update(dev);
-
 
388
 
-
 
389
	DRM_DEBUG_KMS("enabled fbc on plane %c\n", plane_name(intel_crtc->plane));
-
 
390
}
-
 
391
 
-
 
392
bool intel_fbc_enabled(struct drm_device *dev)
-
 
393
{
-
 
394
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
395
 
-
 
396
	return dev_priv->fbc.enabled;
-
 
397
}
-
 
398
 
-
 
399
void bdw_fbc_sw_flush(struct drm_device *dev, u32 value)
-
 
400
{
-
 
401
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
402
 
-
 
403
	if (!IS_GEN8(dev))
-
 
404
		return;
-
 
405
 
-
 
406
	if (!intel_fbc_enabled(dev))
-
 
407
		return;
-
 
408
 
-
 
409
	I915_WRITE(MSG_FBC_REND_STATE, value);
-
 
410
}
-
 
411
 
-
 
412
static void intel_fbc_work_fn(struct work_struct *__work)
-
 
413
{
-
 
414
	struct intel_fbc_work *work =
-
 
415
		container_of(to_delayed_work(__work),
-
 
416
			     struct intel_fbc_work, work);
-
 
417
	struct drm_device *dev = work->crtc->dev;
-
 
418
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
419
 
-
 
420
	mutex_lock(&dev->struct_mutex);
-
 
421
	if (work == dev_priv->fbc.fbc_work) {
-
 
422
		/* Double check that we haven't switched fb without cancelling
-
 
423
		 * the prior work.
-
 
424
		 */
-
 
425
		if (work->crtc->primary->fb == work->fb) {
-
 
426
			dev_priv->display.enable_fbc(work->crtc);
-
 
427
 
-
 
428
			dev_priv->fbc.plane = to_intel_crtc(work->crtc)->plane;
-
 
429
			dev_priv->fbc.fb_id = work->crtc->primary->fb->base.id;
-
 
430
			dev_priv->fbc.y = work->crtc->y;
-
 
431
		}
-
 
432
 
-
 
433
		dev_priv->fbc.fbc_work = NULL;
-
 
434
	}
-
 
435
	mutex_unlock(&dev->struct_mutex);
-
 
436
 
-
 
437
	kfree(work);
-
 
438
}
-
 
439
 
-
 
440
static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
-
 
441
{
-
 
442
	if (dev_priv->fbc.fbc_work == NULL)
-
 
443
		return;
-
 
444
 
-
 
445
	DRM_DEBUG_KMS("cancelling pending FBC enable\n");
-
 
446
 
-
 
447
	/* Synchronisation is provided by struct_mutex and checking of
-
 
448
	 * dev_priv->fbc.fbc_work, so we can perform the cancellation
-
 
449
	 * entirely asynchronously.
-
 
450
	 */
-
 
451
	if (cancel_delayed_work(&dev_priv->fbc.fbc_work->work))
-
 
452
		/* tasklet was killed before being run, clean up */
-
 
453
		kfree(dev_priv->fbc.fbc_work);
-
 
454
 
-
 
455
	/* Mark the work as no longer wanted so that if it does
-
 
456
	 * wake-up (because the work was already running and waiting
-
 
457
	 * for our mutex), it will discover that is no longer
-
 
458
	 * necessary to run.
-
 
459
	 */
-
 
460
	dev_priv->fbc.fbc_work = NULL;
-
 
461
}
-
 
462
 
-
 
463
static void intel_enable_fbc(struct drm_crtc *crtc)
-
 
464
{
-
 
465
	struct intel_fbc_work *work;
-
 
466
	struct drm_device *dev = crtc->dev;
-
 
467
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
468
 
-
 
469
	if (!dev_priv->display.enable_fbc)
-
 
470
		return;
-
 
471
 
-
 
472
	intel_cancel_fbc_work(dev_priv);
-
 
473
 
-
 
474
	work = kzalloc(sizeof(*work), GFP_KERNEL);
-
 
475
	if (work == NULL) {
-
 
476
		DRM_ERROR("Failed to allocate FBC work structure\n");
-
 
477
		dev_priv->display.enable_fbc(crtc);
-
 
478
		return;
-
 
479
	}
-
 
480
 
-
 
481
	work->crtc = crtc;
-
 
482
	work->fb = crtc->primary->fb;
-
 
483
	INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
-
 
484
 
-
 
485
	dev_priv->fbc.fbc_work = work;
-
 
486
 
-
 
487
	/* Delay the actual enabling to let pageflipping cease and the
-
 
488
	 * display to settle before starting the compression. Note that
-
 
489
	 * this delay also serves a second purpose: it allows for a
-
 
490
	 * vblank to pass after disabling the FBC before we attempt
-
 
491
	 * to modify the control registers.
-
 
492
	 *
-
 
493
	 * A more complicated solution would involve tracking vblanks
-
 
494
	 * following the termination of the page-flipping sequence
-
 
495
	 * and indeed performing the enable as a co-routine and not
-
 
496
	 * waiting synchronously upon the vblank.
-
 
497
	 *
-
 
498
	 * WaFbcWaitForVBlankBeforeEnable:ilk,snb
-
 
499
	 */
-
 
500
	schedule_delayed_work(&work->work, msecs_to_jiffies(50));
-
 
501
}
-
 
502
 
-
 
503
void intel_disable_fbc(struct drm_device *dev)
-
 
504
{
-
 
505
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
506
 
-
 
507
	intel_cancel_fbc_work(dev_priv);
-
 
508
 
-
 
509
	if (!dev_priv->display.disable_fbc)
-
 
510
		return;
-
 
511
 
-
 
512
	dev_priv->display.disable_fbc(dev);
-
 
513
	dev_priv->fbc.plane = -1;
-
 
514
}
-
 
515
 
-
 
516
static bool set_no_fbc_reason(struct drm_i915_private *dev_priv,
-
 
517
			      enum no_fbc_reason reason)
-
 
518
{
-
 
519
	if (dev_priv->fbc.no_fbc_reason == reason)
-
 
520
		return false;
-
 
521
 
-
 
522
	dev_priv->fbc.no_fbc_reason = reason;
-
 
523
	return true;
-
 
524
}
-
 
525
 
-
 
526
/**
-
 
527
 * intel_update_fbc - enable/disable FBC as needed
-
 
528
 * @dev: the drm_device
-
 
529
 *
-
 
530
 * Set up the framebuffer compression hardware at mode set time.  We
-
 
531
 * enable it if possible:
-
 
532
 *   - plane A only (on pre-965)
-
 
533
 *   - no pixel mulitply/line duplication
-
 
534
 *   - no alpha buffer discard
-
 
535
 *   - no dual wide
-
 
536
 *   - framebuffer <= max_hdisplay in width, max_vdisplay in height
-
 
537
 *
-
 
538
 * We can't assume that any compression will take place (worst case),
-
 
539
 * so the compressed buffer has to be the same size as the uncompressed
-
 
540
 * one.  It also must reside (along with the line length buffer) in
-
 
541
 * stolen memory.
-
 
542
 *
-
 
543
 * We need to enable/disable FBC on a global basis.
-
 
544
 */
-
 
545
void intel_update_fbc(struct drm_device *dev)
-
 
546
{
-
 
547
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
548
	struct drm_crtc *crtc = NULL, *tmp_crtc;
-
 
549
	struct intel_crtc *intel_crtc;
-
 
550
	struct drm_framebuffer *fb;
-
 
551
	struct drm_i915_gem_object *obj;
-
 
552
	const struct drm_display_mode *adjusted_mode;
-
 
553
	unsigned int max_width, max_height;
-
 
554
 
-
 
555
	if (!HAS_FBC(dev)) {
-
 
556
		set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED);
-
 
557
		return;
-
 
558
	}
-
 
559
 
-
 
560
	if (!i915.powersave) {
-
 
561
		if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
-
 
562
			DRM_DEBUG_KMS("fbc disabled per module param\n");
-
 
563
		return;
-
 
564
	}
-
 
565
 
-
 
566
	/*
-
 
567
	 * If FBC is already on, we just have to verify that we can
-
 
568
	 * keep it that way...
-
 
569
	 * Need to disable if:
-
 
570
	 *   - more than one pipe is active
-
 
571
	 *   - changing FBC params (stride, fence, mode)
-
 
572
	 *   - new fb is too large to fit in compressed buffer
-
 
573
	 *   - going to an unsupported config (interlace, pixel multiply, etc.)
-
 
574
	 */
-
 
575
	for_each_crtc(dev, tmp_crtc) {
-
 
576
		if (intel_crtc_active(tmp_crtc) &&
-
 
577
		    to_intel_crtc(tmp_crtc)->primary_enabled) {
-
 
578
			if (crtc) {
-
 
579
				if (set_no_fbc_reason(dev_priv, FBC_MULTIPLE_PIPES))
-
 
580
				DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
-
 
581
				goto out_disable;
-
 
582
			}
-
 
583
			crtc = tmp_crtc;
-
 
584
		}
-
 
585
	}
-
 
586
 
-
 
587
	if (!crtc || crtc->primary->fb == NULL) {
-
 
588
		if (set_no_fbc_reason(dev_priv, FBC_NO_OUTPUT))
-
 
589
		DRM_DEBUG_KMS("no output, disabling\n");
-
 
590
		goto out_disable;
-
 
591
	}
-
 
592
 
-
 
593
	intel_crtc = to_intel_crtc(crtc);
-
 
594
	fb = crtc->primary->fb;
-
 
595
	obj = intel_fb_obj(fb);
-
 
596
	adjusted_mode = &intel_crtc->config.adjusted_mode;
-
 
597
 
-
 
598
	if (i915.enable_fbc < 0) {
-
 
599
		if (set_no_fbc_reason(dev_priv, FBC_CHIP_DEFAULT))
-
 
600
			DRM_DEBUG_KMS("disabled per chip default\n");
-
 
601
		goto out_disable;
-
 
602
	}
-
 
603
	if (!i915.enable_fbc) {
-
 
604
		if (set_no_fbc_reason(dev_priv, FBC_MODULE_PARAM))
-
 
605
		DRM_DEBUG_KMS("fbc disabled per module param\n");
-
 
606
		goto out_disable;
-
 
607
	}
-
 
608
	if ((adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) ||
-
 
609
	    (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
-
 
610
		if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
-
 
611
		DRM_DEBUG_KMS("mode incompatible with compression, "
-
 
612
			      "disabling\n");
-
 
613
		goto out_disable;
-
 
614
	}
-
 
615
 
-
 
616
	if (INTEL_INFO(dev)->gen >= 8 || IS_HASWELL(dev)) {
-
 
617
		max_width = 4096;
-
 
618
		max_height = 4096;
-
 
619
	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
-
 
620
		max_width = 4096;
-
 
621
		max_height = 2048;
-
 
622
	} else {
-
 
623
		max_width = 2048;
-
 
624
		max_height = 1536;
-
 
625
	}
-
 
626
	if (intel_crtc->config.pipe_src_w > max_width ||
-
 
627
	    intel_crtc->config.pipe_src_h > max_height) {
-
 
628
		if (set_no_fbc_reason(dev_priv, FBC_MODE_TOO_LARGE))
-
 
629
		DRM_DEBUG_KMS("mode too large for compression, disabling\n");
-
 
630
		goto out_disable;
-
 
631
	}
-
 
632
	if ((INTEL_INFO(dev)->gen < 4 || HAS_DDI(dev)) &&
-
 
633
	    intel_crtc->plane != PLANE_A) {
-
 
634
		if (set_no_fbc_reason(dev_priv, FBC_BAD_PLANE))
-
 
635
			DRM_DEBUG_KMS("plane not A, disabling compression\n");
-
 
636
		goto out_disable;
-
 
637
	}
-
 
638
 
-
 
639
	/* The use of a CPU fence is mandatory in order to detect writes
-
 
640
	 * by the CPU to the scanout and trigger updates to the FBC.
-
 
641
	 */
-
 
642
	if (obj->tiling_mode != I915_TILING_X ||
-
 
643
	    obj->fence_reg == I915_FENCE_REG_NONE) {
-
 
644
		if (set_no_fbc_reason(dev_priv, FBC_NOT_TILED))
-
 
645
		DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
-
 
646
		goto out_disable;
-
 
647
	}
-
 
648
	if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
-
 
649
	    to_intel_plane(crtc->primary)->rotation != BIT(DRM_ROTATE_0)) {
-
 
650
		if (set_no_fbc_reason(dev_priv, FBC_UNSUPPORTED_MODE))
-
 
651
			DRM_DEBUG_KMS("Rotation unsupported, disabling\n");
-
 
652
		goto out_disable;
-
 
653
	}
-
 
654
 
-
 
655
	/* If the kernel debugger is active, always disable compression */
-
 
656
	if (in_dbg_master())
-
 
657
		goto out_disable;
-
 
658
 
-
 
659
	if (i915_gem_stolen_setup_compression(dev, obj->base.size,
-
 
660
					      drm_format_plane_cpp(fb->pixel_format, 0))) {
-
 
661
		if (set_no_fbc_reason(dev_priv, FBC_STOLEN_TOO_SMALL))
-
 
662
		DRM_DEBUG_KMS("framebuffer too large, disabling compression\n");
-
 
663
		goto out_disable;
-
 
664
	}
-
 
665
 
-
 
666
	/* If the scanout has not changed, don't modify the FBC settings.
-
 
667
	 * Note that we make the fundamental assumption that the fb->obj
-
 
668
	 * cannot be unpinned (and have its GTT offset and fence revoked)
-
 
669
	 * without first being decoupled from the scanout and FBC disabled.
-
 
670
	 */
-
 
671
	if (dev_priv->fbc.plane == intel_crtc->plane &&
-
 
672
	    dev_priv->fbc.fb_id == fb->base.id &&
-
 
673
	    dev_priv->fbc.y == crtc->y)
-
 
674
		return;
-
 
675
 
-
 
676
	if (intel_fbc_enabled(dev)) {
-
 
677
		/* We update FBC along two paths, after changing fb/crtc
-
 
678
		 * configuration (modeswitching) and after page-flipping
-
 
679
		 * finishes. For the latter, we know that not only did
-
 
680
		 * we disable the FBC at the start of the page-flip
-
 
681
		 * sequence, but also more than one vblank has passed.
-
 
682
		 *
-
 
683
		 * For the former case of modeswitching, it is possible
-
 
684
		 * to switch between two FBC valid configurations
-
 
685
		 * instantaneously so we do need to disable the FBC
-
 
686
		 * before we can modify its control registers. We also
-
 
687
		 * have to wait for the next vblank for that to take
-
 
688
		 * effect. However, since we delay enabling FBC we can
-
 
689
		 * assume that a vblank has passed since disabling and
-
 
690
		 * that we can safely alter the registers in the deferred
-
 
691
		 * callback.
-
 
692
		 *
-
 
693
		 * In the scenario that we go from a valid to invalid
-
 
694
		 * and then back to valid FBC configuration we have
-
 
695
		 * no strict enforcement that a vblank occurred since
-
 
696
		 * disabling the FBC. However, along all current pipe
-
 
697
		 * disabling paths we do need to wait for a vblank at
-
 
698
		 * some point. And we wait before enabling FBC anyway.
-
 
699
		 */
-
 
700
		DRM_DEBUG_KMS("disabling active FBC for update\n");
-
 
701
		intel_disable_fbc(dev);
-
 
702
	}
-
 
703
 
-
 
704
	intel_enable_fbc(crtc);
-
 
705
	dev_priv->fbc.no_fbc_reason = FBC_OK;
-
 
706
	return;
-
 
707
 
-
 
708
out_disable:
-
 
709
	/* Multiple disables should be harmless */
-
 
710
	if (intel_fbc_enabled(dev)) {
-
 
711
		DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
-
 
712
		intel_disable_fbc(dev);
-
 
713
	}
-
 
714
	i915_gem_stolen_cleanup_compression(dev);
-
 
715
}
74
}
716
 
75
 
717
static void i915_pineview_get_mem_freq(struct drm_device *dev)
76
static void i915_pineview_get_mem_freq(struct drm_device *dev)
718
{
77
{
719
	struct drm_i915_private *dev_priv = dev->dev_private;
78
	struct drm_i915_private *dev_priv = dev->dev_private;
720
	u32 tmp;
79
	u32 tmp;
721
 
80
 
722
	tmp = I915_READ(CLKCFG);
81
	tmp = I915_READ(CLKCFG);
723
 
82
 
724
	switch (tmp & CLKCFG_FSB_MASK) {
83
	switch (tmp & CLKCFG_FSB_MASK) {
725
	case CLKCFG_FSB_533:
84
	case CLKCFG_FSB_533:
726
		dev_priv->fsb_freq = 533; /* 133*4 */
85
		dev_priv->fsb_freq = 533; /* 133*4 */
727
		break;
86
		break;
728
	case CLKCFG_FSB_800:
87
	case CLKCFG_FSB_800:
729
		dev_priv->fsb_freq = 800; /* 200*4 */
88
		dev_priv->fsb_freq = 800; /* 200*4 */
730
		break;
89
		break;
731
	case CLKCFG_FSB_667:
90
	case CLKCFG_FSB_667:
732
		dev_priv->fsb_freq =  667; /* 167*4 */
91
		dev_priv->fsb_freq =  667; /* 167*4 */
733
		break;
92
		break;
734
	case CLKCFG_FSB_400:
93
	case CLKCFG_FSB_400:
735
		dev_priv->fsb_freq = 400; /* 100*4 */
94
		dev_priv->fsb_freq = 400; /* 100*4 */
736
		break;
95
		break;
737
	}
96
	}
738
 
97
 
739
	switch (tmp & CLKCFG_MEM_MASK) {
98
	switch (tmp & CLKCFG_MEM_MASK) {
740
	case CLKCFG_MEM_533:
99
	case CLKCFG_MEM_533:
741
		dev_priv->mem_freq = 533;
100
		dev_priv->mem_freq = 533;
742
		break;
101
		break;
743
	case CLKCFG_MEM_667:
102
	case CLKCFG_MEM_667:
744
		dev_priv->mem_freq = 667;
103
		dev_priv->mem_freq = 667;
745
		break;
104
		break;
746
	case CLKCFG_MEM_800:
105
	case CLKCFG_MEM_800:
747
		dev_priv->mem_freq = 800;
106
		dev_priv->mem_freq = 800;
748
		break;
107
		break;
749
	}
108
	}
750
 
109
 
751
	/* detect pineview DDR3 setting */
110
	/* detect pineview DDR3 setting */
752
	tmp = I915_READ(CSHRDDR3CTL);
111
	tmp = I915_READ(CSHRDDR3CTL);
753
	dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
112
	dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
754
}
113
}
755
 
114
 
756
static void i915_ironlake_get_mem_freq(struct drm_device *dev)
115
static void i915_ironlake_get_mem_freq(struct drm_device *dev)
757
{
116
{
758
	struct drm_i915_private *dev_priv = dev->dev_private;
117
	struct drm_i915_private *dev_priv = dev->dev_private;
759
	u16 ddrpll, csipll;
118
	u16 ddrpll, csipll;
760
 
119
 
761
	ddrpll = I915_READ16(DDRMPLL1);
120
	ddrpll = I915_READ16(DDRMPLL1);
762
	csipll = I915_READ16(CSIPLL0);
121
	csipll = I915_READ16(CSIPLL0);
763
 
122
 
764
	switch (ddrpll & 0xff) {
123
	switch (ddrpll & 0xff) {
765
	case 0xc:
124
	case 0xc:
766
		dev_priv->mem_freq = 800;
125
		dev_priv->mem_freq = 800;
767
		break;
126
		break;
768
	case 0x10:
127
	case 0x10:
769
		dev_priv->mem_freq = 1066;
128
		dev_priv->mem_freq = 1066;
770
		break;
129
		break;
771
	case 0x14:
130
	case 0x14:
772
		dev_priv->mem_freq = 1333;
131
		dev_priv->mem_freq = 1333;
773
		break;
132
		break;
774
	case 0x18:
133
	case 0x18:
775
		dev_priv->mem_freq = 1600;
134
		dev_priv->mem_freq = 1600;
776
		break;
135
		break;
777
	default:
136
	default:
778
		DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
137
		DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
779
				 ddrpll & 0xff);
138
				 ddrpll & 0xff);
780
		dev_priv->mem_freq = 0;
139
		dev_priv->mem_freq = 0;
781
		break;
140
		break;
782
	}
141
	}
783
 
142
 
784
	dev_priv->ips.r_t = dev_priv->mem_freq;
143
	dev_priv->ips.r_t = dev_priv->mem_freq;
785
 
144
 
786
	switch (csipll & 0x3ff) {
145
	switch (csipll & 0x3ff) {
787
	case 0x00c:
146
	case 0x00c:
788
		dev_priv->fsb_freq = 3200;
147
		dev_priv->fsb_freq = 3200;
789
		break;
148
		break;
790
	case 0x00e:
149
	case 0x00e:
791
		dev_priv->fsb_freq = 3733;
150
		dev_priv->fsb_freq = 3733;
792
		break;
151
		break;
793
	case 0x010:
152
	case 0x010:
794
		dev_priv->fsb_freq = 4266;
153
		dev_priv->fsb_freq = 4266;
795
		break;
154
		break;
796
	case 0x012:
155
	case 0x012:
797
		dev_priv->fsb_freq = 4800;
156
		dev_priv->fsb_freq = 4800;
798
		break;
157
		break;
799
	case 0x014:
158
	case 0x014:
800
		dev_priv->fsb_freq = 5333;
159
		dev_priv->fsb_freq = 5333;
801
		break;
160
		break;
802
	case 0x016:
161
	case 0x016:
803
		dev_priv->fsb_freq = 5866;
162
		dev_priv->fsb_freq = 5866;
804
		break;
163
		break;
805
	case 0x018:
164
	case 0x018:
806
		dev_priv->fsb_freq = 6400;
165
		dev_priv->fsb_freq = 6400;
807
		break;
166
		break;
808
	default:
167
	default:
809
		DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
168
		DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
810
				 csipll & 0x3ff);
169
				 csipll & 0x3ff);
811
		dev_priv->fsb_freq = 0;
170
		dev_priv->fsb_freq = 0;
812
		break;
171
		break;
813
	}
172
	}
814
 
173
 
815
	if (dev_priv->fsb_freq == 3200) {
174
	if (dev_priv->fsb_freq == 3200) {
816
		dev_priv->ips.c_m = 0;
175
		dev_priv->ips.c_m = 0;
817
	} else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
176
	} else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
818
		dev_priv->ips.c_m = 1;
177
		dev_priv->ips.c_m = 1;
819
	} else {
178
	} else {
820
		dev_priv->ips.c_m = 2;
179
		dev_priv->ips.c_m = 2;
821
	}
180
	}
822
}
181
}
823
 
182
 
824
static const struct cxsr_latency cxsr_latency_table[] = {
183
static const struct cxsr_latency cxsr_latency_table[] = {
825
	{1, 0, 800, 400, 3382, 33382, 3983, 33983},    /* DDR2-400 SC */
184
	{1, 0, 800, 400, 3382, 33382, 3983, 33983},    /* DDR2-400 SC */
826
	{1, 0, 800, 667, 3354, 33354, 3807, 33807},    /* DDR2-667 SC */
185
	{1, 0, 800, 667, 3354, 33354, 3807, 33807},    /* DDR2-667 SC */
827
	{1, 0, 800, 800, 3347, 33347, 3763, 33763},    /* DDR2-800 SC */
186
	{1, 0, 800, 800, 3347, 33347, 3763, 33763},    /* DDR2-800 SC */
828
	{1, 1, 800, 667, 6420, 36420, 6873, 36873},    /* DDR3-667 SC */
187
	{1, 1, 800, 667, 6420, 36420, 6873, 36873},    /* DDR3-667 SC */
829
	{1, 1, 800, 800, 5902, 35902, 6318, 36318},    /* DDR3-800 SC */
188
	{1, 1, 800, 800, 5902, 35902, 6318, 36318},    /* DDR3-800 SC */
830
 
189
 
831
	{1, 0, 667, 400, 3400, 33400, 4021, 34021},    /* DDR2-400 SC */
190
	{1, 0, 667, 400, 3400, 33400, 4021, 34021},    /* DDR2-400 SC */
832
	{1, 0, 667, 667, 3372, 33372, 3845, 33845},    /* DDR2-667 SC */
191
	{1, 0, 667, 667, 3372, 33372, 3845, 33845},    /* DDR2-667 SC */
833
	{1, 0, 667, 800, 3386, 33386, 3822, 33822},    /* DDR2-800 SC */
192
	{1, 0, 667, 800, 3386, 33386, 3822, 33822},    /* DDR2-800 SC */
834
	{1, 1, 667, 667, 6438, 36438, 6911, 36911},    /* DDR3-667 SC */
193
	{1, 1, 667, 667, 6438, 36438, 6911, 36911},    /* DDR3-667 SC */
835
	{1, 1, 667, 800, 5941, 35941, 6377, 36377},    /* DDR3-800 SC */
194
	{1, 1, 667, 800, 5941, 35941, 6377, 36377},    /* DDR3-800 SC */
836
 
195
 
837
	{1, 0, 400, 400, 3472, 33472, 4173, 34173},    /* DDR2-400 SC */
196
	{1, 0, 400, 400, 3472, 33472, 4173, 34173},    /* DDR2-400 SC */
838
	{1, 0, 400, 667, 3443, 33443, 3996, 33996},    /* DDR2-667 SC */
197
	{1, 0, 400, 667, 3443, 33443, 3996, 33996},    /* DDR2-667 SC */
839
	{1, 0, 400, 800, 3430, 33430, 3946, 33946},    /* DDR2-800 SC */
198
	{1, 0, 400, 800, 3430, 33430, 3946, 33946},    /* DDR2-800 SC */
840
	{1, 1, 400, 667, 6509, 36509, 7062, 37062},    /* DDR3-667 SC */
199
	{1, 1, 400, 667, 6509, 36509, 7062, 37062},    /* DDR3-667 SC */
841
	{1, 1, 400, 800, 5985, 35985, 6501, 36501},    /* DDR3-800 SC */
200
	{1, 1, 400, 800, 5985, 35985, 6501, 36501},    /* DDR3-800 SC */
842
 
201
 
843
	{0, 0, 800, 400, 3438, 33438, 4065, 34065},    /* DDR2-400 SC */
202
	{0, 0, 800, 400, 3438, 33438, 4065, 34065},    /* DDR2-400 SC */
844
	{0, 0, 800, 667, 3410, 33410, 3889, 33889},    /* DDR2-667 SC */
203
	{0, 0, 800, 667, 3410, 33410, 3889, 33889},    /* DDR2-667 SC */
845
	{0, 0, 800, 800, 3403, 33403, 3845, 33845},    /* DDR2-800 SC */
204
	{0, 0, 800, 800, 3403, 33403, 3845, 33845},    /* DDR2-800 SC */
846
	{0, 1, 800, 667, 6476, 36476, 6955, 36955},    /* DDR3-667 SC */
205
	{0, 1, 800, 667, 6476, 36476, 6955, 36955},    /* DDR3-667 SC */
847
	{0, 1, 800, 800, 5958, 35958, 6400, 36400},    /* DDR3-800 SC */
206
	{0, 1, 800, 800, 5958, 35958, 6400, 36400},    /* DDR3-800 SC */
848
 
207
 
849
	{0, 0, 667, 400, 3456, 33456, 4103, 34106},    /* DDR2-400 SC */
208
	{0, 0, 667, 400, 3456, 33456, 4103, 34106},    /* DDR2-400 SC */
850
	{0, 0, 667, 667, 3428, 33428, 3927, 33927},    /* DDR2-667 SC */
209
	{0, 0, 667, 667, 3428, 33428, 3927, 33927},    /* DDR2-667 SC */
851
	{0, 0, 667, 800, 3443, 33443, 3905, 33905},    /* DDR2-800 SC */
210
	{0, 0, 667, 800, 3443, 33443, 3905, 33905},    /* DDR2-800 SC */
852
	{0, 1, 667, 667, 6494, 36494, 6993, 36993},    /* DDR3-667 SC */
211
	{0, 1, 667, 667, 6494, 36494, 6993, 36993},    /* DDR3-667 SC */
853
	{0, 1, 667, 800, 5998, 35998, 6460, 36460},    /* DDR3-800 SC */
212
	{0, 1, 667, 800, 5998, 35998, 6460, 36460},    /* DDR3-800 SC */
854
 
213
 
855
	{0, 0, 400, 400, 3528, 33528, 4255, 34255},    /* DDR2-400 SC */
214
	{0, 0, 400, 400, 3528, 33528, 4255, 34255},    /* DDR2-400 SC */
856
	{0, 0, 400, 667, 3500, 33500, 4079, 34079},    /* DDR2-667 SC */
215
	{0, 0, 400, 667, 3500, 33500, 4079, 34079},    /* DDR2-667 SC */
857
	{0, 0, 400, 800, 3487, 33487, 4029, 34029},    /* DDR2-800 SC */
216
	{0, 0, 400, 800, 3487, 33487, 4029, 34029},    /* DDR2-800 SC */
858
	{0, 1, 400, 667, 6566, 36566, 7145, 37145},    /* DDR3-667 SC */
217
	{0, 1, 400, 667, 6566, 36566, 7145, 37145},    /* DDR3-667 SC */
859
	{0, 1, 400, 800, 6042, 36042, 6584, 36584},    /* DDR3-800 SC */
218
	{0, 1, 400, 800, 6042, 36042, 6584, 36584},    /* DDR3-800 SC */
860
};
219
};
861
 
220
 
862
static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
221
static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
863
							 int is_ddr3,
222
							 int is_ddr3,
864
							 int fsb,
223
							 int fsb,
865
							 int mem)
224
							 int mem)
866
{
225
{
867
	const struct cxsr_latency *latency;
226
	const struct cxsr_latency *latency;
868
	int i;
227
	int i;
869
 
228
 
870
	if (fsb == 0 || mem == 0)
229
	if (fsb == 0 || mem == 0)
871
		return NULL;
230
		return NULL;
872
 
231
 
873
	for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
232
	for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
874
		latency = &cxsr_latency_table[i];
233
		latency = &cxsr_latency_table[i];
875
		if (is_desktop == latency->is_desktop &&
234
		if (is_desktop == latency->is_desktop &&
876
		    is_ddr3 == latency->is_ddr3 &&
235
		    is_ddr3 == latency->is_ddr3 &&
877
		    fsb == latency->fsb_freq && mem == latency->mem_freq)
236
		    fsb == latency->fsb_freq && mem == latency->mem_freq)
878
			return latency;
237
			return latency;
879
	}
238
	}
880
 
239
 
881
	DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
240
	DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
882
 
241
 
883
	return NULL;
242
	return NULL;
884
}
243
}
-
 
244
 
-
 
245
static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
-
 
246
{
-
 
247
	u32 val;
-
 
248
 
-
 
249
	mutex_lock(&dev_priv->rps.hw_lock);
-
 
250
 
-
 
251
	val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
-
 
252
	if (enable)
-
 
253
		val &= ~FORCE_DDR_HIGH_FREQ;
-
 
254
	else
-
 
255
		val |= FORCE_DDR_HIGH_FREQ;
-
 
256
	val &= ~FORCE_DDR_LOW_FREQ;
-
 
257
	val |= FORCE_DDR_FREQ_REQ_ACK;
-
 
258
	vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
-
 
259
 
-
 
260
	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
-
 
261
		      FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
-
 
262
		DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
-
 
263
 
-
 
264
	mutex_unlock(&dev_priv->rps.hw_lock);
-
 
265
}
-
 
266
 
-
 
267
static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
-
 
268
{
-
 
269
	u32 val;
-
 
270
 
-
 
271
	mutex_lock(&dev_priv->rps.hw_lock);
-
 
272
 
-
 
273
	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
-
 
274
	if (enable)
-
 
275
		val |= DSP_MAXFIFO_PM5_ENABLE;
-
 
276
	else
-
 
277
		val &= ~DSP_MAXFIFO_PM5_ENABLE;
-
 
278
	vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
-
 
279
 
-
 
280
	mutex_unlock(&dev_priv->rps.hw_lock);
-
 
281
}
-
 
282
 
-
 
283
#define FW_WM(value, plane) \
-
 
284
	(((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
885
 
285
 
886
void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
286
void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
887
{
287
{
888
	struct drm_device *dev = dev_priv->dev;
288
	struct drm_device *dev = dev_priv->dev;
889
	u32 val;
289
	u32 val;
890
 
290
 
891
	if (IS_VALLEYVIEW(dev)) {
291
	if (IS_VALLEYVIEW(dev)) {
892
		I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
292
		I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
-
 
293
		POSTING_READ(FW_BLC_SELF_VLV);
-
 
294
		dev_priv->wm.vlv.cxsr = enable;
893
	} else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
295
	} else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
894
		I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
296
		I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
-
 
297
		POSTING_READ(FW_BLC_SELF);
895
	} else if (IS_PINEVIEW(dev)) {
298
	} else if (IS_PINEVIEW(dev)) {
896
		val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
299
		val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
897
		val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
300
		val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
898
		I915_WRITE(DSPFW3, val);
301
		I915_WRITE(DSPFW3, val);
-
 
302
		POSTING_READ(DSPFW3);
899
	} else if (IS_I945G(dev) || IS_I945GM(dev)) {
303
	} else if (IS_I945G(dev) || IS_I945GM(dev)) {
900
		val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
304
		val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
901
			       _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
305
			       _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
902
		I915_WRITE(FW_BLC_SELF, val);
306
		I915_WRITE(FW_BLC_SELF, val);
-
 
307
		POSTING_READ(FW_BLC_SELF);
903
	} else if (IS_I915GM(dev)) {
308
	} else if (IS_I915GM(dev)) {
904
		val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
309
		val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
905
			       _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
310
			       _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
906
		I915_WRITE(INSTPM, val);
311
		I915_WRITE(INSTPM, val);
-
 
312
		POSTING_READ(INSTPM);
907
	} else {
313
	} else {
908
		return;
314
		return;
909
	}
315
	}
910
 
316
 
911
	DRM_DEBUG_KMS("memory self-refresh is %s\n",
317
	DRM_DEBUG_KMS("memory self-refresh is %s\n",
912
		      enable ? "enabled" : "disabled");
318
		      enable ? "enabled" : "disabled");
913
}
319
}
-
 
320
 
914
 
321
 
915
/*
322
/*
916
 * Latency for FIFO fetches is dependent on several factors:
323
 * Latency for FIFO fetches is dependent on several factors:
917
 *   - memory configuration (speed, channels)
324
 *   - memory configuration (speed, channels)
918
 *   - chipset
325
 *   - chipset
919
 *   - current MCH state
326
 *   - current MCH state
920
 * It can be fairly high in some situations, so here we assume a fairly
327
 * It can be fairly high in some situations, so here we assume a fairly
921
 * pessimal value.  It's a tradeoff between extra memory fetches (if we
328
 * pessimal value.  It's a tradeoff between extra memory fetches (if we
922
 * set this value too high, the FIFO will fetch frequently to stay full)
329
 * set this value too high, the FIFO will fetch frequently to stay full)
923
 * and power consumption (set it too low to save power and we might see
330
 * and power consumption (set it too low to save power and we might see
924
 * FIFO underruns and display "flicker").
331
 * FIFO underruns and display "flicker").
925
 *
332
 *
926
 * A value of 5us seems to be a good balance; safe for very low end
333
 * A value of 5us seems to be a good balance; safe for very low end
927
 * platforms but not overly aggressive on lower latency configs.
334
 * platforms but not overly aggressive on lower latency configs.
928
 */
335
 */
929
static const int pessimal_latency_ns = 5000;
336
static const int pessimal_latency_ns = 5000;
-
 
337
 
-
 
338
#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
-
 
339
	((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
-
 
340
 
-
 
341
static int vlv_get_fifo_size(struct drm_device *dev,
-
 
342
			      enum pipe pipe, int plane)
-
 
343
{
-
 
344
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
345
	int sprite0_start, sprite1_start, size;
-
 
346
 
-
 
347
	switch (pipe) {
-
 
348
		uint32_t dsparb, dsparb2, dsparb3;
-
 
349
	case PIPE_A:
-
 
350
		dsparb = I915_READ(DSPARB);
-
 
351
		dsparb2 = I915_READ(DSPARB2);
-
 
352
		sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
-
 
353
		sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
-
 
354
		break;
-
 
355
	case PIPE_B:
-
 
356
		dsparb = I915_READ(DSPARB);
-
 
357
		dsparb2 = I915_READ(DSPARB2);
-
 
358
		sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
-
 
359
		sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
-
 
360
		break;
-
 
361
	case PIPE_C:
-
 
362
		dsparb2 = I915_READ(DSPARB2);
-
 
363
		dsparb3 = I915_READ(DSPARB3);
-
 
364
		sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
-
 
365
		sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
-
 
366
		break;
-
 
367
	default:
-
 
368
		return 0;
-
 
369
	}
-
 
370
 
-
 
371
	switch (plane) {
-
 
372
	case 0:
-
 
373
		size = sprite0_start;
-
 
374
		break;
-
 
375
	case 1:
-
 
376
		size = sprite1_start - sprite0_start;
-
 
377
		break;
-
 
378
	case 2:
-
 
379
		size = 512 - 1 - sprite1_start;
-
 
380
		break;
-
 
381
	default:
-
 
382
		return 0;
-
 
383
	}
-
 
384
 
-
 
385
	DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
-
 
386
		      pipe_name(pipe), plane == 0 ? "primary" : "sprite",
-
 
387
		      plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
-
 
388
		      size);
-
 
389
 
-
 
390
	return size;
-
 
391
}
930
 
392
 
931
static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
393
static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
932
{
394
{
933
	struct drm_i915_private *dev_priv = dev->dev_private;
395
	struct drm_i915_private *dev_priv = dev->dev_private;
934
	uint32_t dsparb = I915_READ(DSPARB);
396
	uint32_t dsparb = I915_READ(DSPARB);
935
	int size;
397
	int size;
936
 
398
 
937
	size = dsparb & 0x7f;
399
	size = dsparb & 0x7f;
938
	if (plane)
400
	if (plane)
939
		size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
401
		size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
940
 
402
 
941
	DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
403
	DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
942
		      plane ? "B" : "A", size);
404
		      plane ? "B" : "A", size);
943
 
405
 
944
	return size;
406
	return size;
945
}
407
}
946
 
408
 
947
static int i830_get_fifo_size(struct drm_device *dev, int plane)
409
static int i830_get_fifo_size(struct drm_device *dev, int plane)
948
{
410
{
949
	struct drm_i915_private *dev_priv = dev->dev_private;
411
	struct drm_i915_private *dev_priv = dev->dev_private;
950
	uint32_t dsparb = I915_READ(DSPARB);
412
	uint32_t dsparb = I915_READ(DSPARB);
951
	int size;
413
	int size;
952
 
414
 
953
	size = dsparb & 0x1ff;
415
	size = dsparb & 0x1ff;
954
	if (plane)
416
	if (plane)
955
		size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
417
		size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
956
	size >>= 1; /* Convert to cachelines */
418
	size >>= 1; /* Convert to cachelines */
957
 
419
 
958
	DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
420
	DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
959
		      plane ? "B" : "A", size);
421
		      plane ? "B" : "A", size);
960
 
422
 
961
	return size;
423
	return size;
962
}
424
}
963
 
425
 
964
static int i845_get_fifo_size(struct drm_device *dev, int plane)
426
static int i845_get_fifo_size(struct drm_device *dev, int plane)
965
{
427
{
966
	struct drm_i915_private *dev_priv = dev->dev_private;
428
	struct drm_i915_private *dev_priv = dev->dev_private;
967
	uint32_t dsparb = I915_READ(DSPARB);
429
	uint32_t dsparb = I915_READ(DSPARB);
968
	int size;
430
	int size;
969
 
431
 
970
	size = dsparb & 0x7f;
432
	size = dsparb & 0x7f;
971
	size >>= 2; /* Convert to cachelines */
433
	size >>= 2; /* Convert to cachelines */
972
 
434
 
973
	DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
435
	DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
974
		      plane ? "B" : "A",
436
		      plane ? "B" : "A",
975
		      size);
437
		      size);
976
 
438
 
977
	return size;
439
	return size;
978
}
440
}
979
 
441
 
980
/* Pineview has different values for various configs */
442
/* Pineview has different values for various configs */
981
static const struct intel_watermark_params pineview_display_wm = {
443
static const struct intel_watermark_params pineview_display_wm = {
982
	.fifo_size = PINEVIEW_DISPLAY_FIFO,
444
	.fifo_size = PINEVIEW_DISPLAY_FIFO,
983
	.max_wm = PINEVIEW_MAX_WM,
445
	.max_wm = PINEVIEW_MAX_WM,
984
	.default_wm = PINEVIEW_DFT_WM,
446
	.default_wm = PINEVIEW_DFT_WM,
985
	.guard_size = PINEVIEW_GUARD_WM,
447
	.guard_size = PINEVIEW_GUARD_WM,
986
	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
448
	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
987
};
449
};
988
static const struct intel_watermark_params pineview_display_hplloff_wm = {
450
static const struct intel_watermark_params pineview_display_hplloff_wm = {
989
	.fifo_size = PINEVIEW_DISPLAY_FIFO,
451
	.fifo_size = PINEVIEW_DISPLAY_FIFO,
990
	.max_wm = PINEVIEW_MAX_WM,
452
	.max_wm = PINEVIEW_MAX_WM,
991
	.default_wm = PINEVIEW_DFT_HPLLOFF_WM,
453
	.default_wm = PINEVIEW_DFT_HPLLOFF_WM,
992
	.guard_size = PINEVIEW_GUARD_WM,
454
	.guard_size = PINEVIEW_GUARD_WM,
993
	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
455
	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
994
};
456
};
995
static const struct intel_watermark_params pineview_cursor_wm = {
457
static const struct intel_watermark_params pineview_cursor_wm = {
996
	.fifo_size = PINEVIEW_CURSOR_FIFO,
458
	.fifo_size = PINEVIEW_CURSOR_FIFO,
997
	.max_wm = PINEVIEW_CURSOR_MAX_WM,
459
	.max_wm = PINEVIEW_CURSOR_MAX_WM,
998
	.default_wm = PINEVIEW_CURSOR_DFT_WM,
460
	.default_wm = PINEVIEW_CURSOR_DFT_WM,
999
	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
461
	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
1000
	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
462
	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
1001
};
463
};
1002
static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
464
static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
1003
	.fifo_size = PINEVIEW_CURSOR_FIFO,
465
	.fifo_size = PINEVIEW_CURSOR_FIFO,
1004
	.max_wm = PINEVIEW_CURSOR_MAX_WM,
466
	.max_wm = PINEVIEW_CURSOR_MAX_WM,
1005
	.default_wm = PINEVIEW_CURSOR_DFT_WM,
467
	.default_wm = PINEVIEW_CURSOR_DFT_WM,
1006
	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
468
	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
1007
	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
469
	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
1008
};
470
};
1009
static const struct intel_watermark_params g4x_wm_info = {
471
static const struct intel_watermark_params g4x_wm_info = {
1010
	.fifo_size = G4X_FIFO_SIZE,
472
	.fifo_size = G4X_FIFO_SIZE,
1011
	.max_wm = G4X_MAX_WM,
473
	.max_wm = G4X_MAX_WM,
1012
	.default_wm = G4X_MAX_WM,
474
	.default_wm = G4X_MAX_WM,
1013
	.guard_size = 2,
475
	.guard_size = 2,
1014
	.cacheline_size = G4X_FIFO_LINE_SIZE,
476
	.cacheline_size = G4X_FIFO_LINE_SIZE,
1015
};
477
};
1016
static const struct intel_watermark_params g4x_cursor_wm_info = {
478
static const struct intel_watermark_params g4x_cursor_wm_info = {
1017
	.fifo_size = I965_CURSOR_FIFO,
479
	.fifo_size = I965_CURSOR_FIFO,
1018
	.max_wm = I965_CURSOR_MAX_WM,
480
	.max_wm = I965_CURSOR_MAX_WM,
1019
	.default_wm = I965_CURSOR_DFT_WM,
481
	.default_wm = I965_CURSOR_DFT_WM,
1020
	.guard_size = 2,
482
	.guard_size = 2,
1021
	.cacheline_size = G4X_FIFO_LINE_SIZE,
483
	.cacheline_size = G4X_FIFO_LINE_SIZE,
1022
};
484
};
1023
static const struct intel_watermark_params valleyview_wm_info = {
485
static const struct intel_watermark_params valleyview_wm_info = {
1024
	.fifo_size = VALLEYVIEW_FIFO_SIZE,
486
	.fifo_size = VALLEYVIEW_FIFO_SIZE,
1025
	.max_wm = VALLEYVIEW_MAX_WM,
487
	.max_wm = VALLEYVIEW_MAX_WM,
1026
	.default_wm = VALLEYVIEW_MAX_WM,
488
	.default_wm = VALLEYVIEW_MAX_WM,
1027
	.guard_size = 2,
489
	.guard_size = 2,
1028
	.cacheline_size = G4X_FIFO_LINE_SIZE,
490
	.cacheline_size = G4X_FIFO_LINE_SIZE,
1029
};
491
};
1030
static const struct intel_watermark_params valleyview_cursor_wm_info = {
492
static const struct intel_watermark_params valleyview_cursor_wm_info = {
1031
	.fifo_size = I965_CURSOR_FIFO,
493
	.fifo_size = I965_CURSOR_FIFO,
1032
	.max_wm = VALLEYVIEW_CURSOR_MAX_WM,
494
	.max_wm = VALLEYVIEW_CURSOR_MAX_WM,
1033
	.default_wm = I965_CURSOR_DFT_WM,
495
	.default_wm = I965_CURSOR_DFT_WM,
1034
	.guard_size = 2,
496
	.guard_size = 2,
1035
	.cacheline_size = G4X_FIFO_LINE_SIZE,
497
	.cacheline_size = G4X_FIFO_LINE_SIZE,
1036
};
498
};
1037
static const struct intel_watermark_params i965_cursor_wm_info = {
499
static const struct intel_watermark_params i965_cursor_wm_info = {
1038
	.fifo_size = I965_CURSOR_FIFO,
500
	.fifo_size = I965_CURSOR_FIFO,
1039
	.max_wm = I965_CURSOR_MAX_WM,
501
	.max_wm = I965_CURSOR_MAX_WM,
1040
	.default_wm = I965_CURSOR_DFT_WM,
502
	.default_wm = I965_CURSOR_DFT_WM,
1041
	.guard_size = 2,
503
	.guard_size = 2,
1042
	.cacheline_size = I915_FIFO_LINE_SIZE,
504
	.cacheline_size = I915_FIFO_LINE_SIZE,
1043
};
505
};
1044
static const struct intel_watermark_params i945_wm_info = {
506
static const struct intel_watermark_params i945_wm_info = {
1045
	.fifo_size = I945_FIFO_SIZE,
507
	.fifo_size = I945_FIFO_SIZE,
1046
	.max_wm = I915_MAX_WM,
508
	.max_wm = I915_MAX_WM,
1047
	.default_wm = 1,
509
	.default_wm = 1,
1048
	.guard_size = 2,
510
	.guard_size = 2,
1049
	.cacheline_size = I915_FIFO_LINE_SIZE,
511
	.cacheline_size = I915_FIFO_LINE_SIZE,
1050
};
512
};
1051
static const struct intel_watermark_params i915_wm_info = {
513
static const struct intel_watermark_params i915_wm_info = {
1052
	.fifo_size = I915_FIFO_SIZE,
514
	.fifo_size = I915_FIFO_SIZE,
1053
	.max_wm = I915_MAX_WM,
515
	.max_wm = I915_MAX_WM,
1054
	.default_wm = 1,
516
	.default_wm = 1,
1055
	.guard_size = 2,
517
	.guard_size = 2,
1056
	.cacheline_size = I915_FIFO_LINE_SIZE,
518
	.cacheline_size = I915_FIFO_LINE_SIZE,
1057
};
519
};
1058
static const struct intel_watermark_params i830_a_wm_info = {
520
static const struct intel_watermark_params i830_a_wm_info = {
1059
	.fifo_size = I855GM_FIFO_SIZE,
521
	.fifo_size = I855GM_FIFO_SIZE,
1060
	.max_wm = I915_MAX_WM,
522
	.max_wm = I915_MAX_WM,
1061
	.default_wm = 1,
523
	.default_wm = 1,
1062
	.guard_size = 2,
524
	.guard_size = 2,
1063
	.cacheline_size = I830_FIFO_LINE_SIZE,
525
	.cacheline_size = I830_FIFO_LINE_SIZE,
1064
};
526
};
1065
static const struct intel_watermark_params i830_bc_wm_info = {
527
static const struct intel_watermark_params i830_bc_wm_info = {
1066
	.fifo_size = I855GM_FIFO_SIZE,
528
	.fifo_size = I855GM_FIFO_SIZE,
1067
	.max_wm = I915_MAX_WM/2,
529
	.max_wm = I915_MAX_WM/2,
1068
	.default_wm = 1,
530
	.default_wm = 1,
1069
	.guard_size = 2,
531
	.guard_size = 2,
1070
	.cacheline_size = I830_FIFO_LINE_SIZE,
532
	.cacheline_size = I830_FIFO_LINE_SIZE,
1071
};
533
};
1072
static const struct intel_watermark_params i845_wm_info = {
534
static const struct intel_watermark_params i845_wm_info = {
1073
	.fifo_size = I830_FIFO_SIZE,
535
	.fifo_size = I830_FIFO_SIZE,
1074
	.max_wm = I915_MAX_WM,
536
	.max_wm = I915_MAX_WM,
1075
	.default_wm = 1,
537
	.default_wm = 1,
1076
	.guard_size = 2,
538
	.guard_size = 2,
1077
	.cacheline_size = I830_FIFO_LINE_SIZE,
539
	.cacheline_size = I830_FIFO_LINE_SIZE,
1078
};
540
};
1079
 
541
 
1080
/**
542
/**
1081
 * intel_calculate_wm - calculate watermark level
543
 * intel_calculate_wm - calculate watermark level
1082
 * @clock_in_khz: pixel clock
544
 * @clock_in_khz: pixel clock
1083
 * @wm: chip FIFO params
545
 * @wm: chip FIFO params
1084
 * @pixel_size: display pixel size
546
 * @pixel_size: display pixel size
1085
 * @latency_ns: memory latency for the platform
547
 * @latency_ns: memory latency for the platform
1086
 *
548
 *
1087
 * Calculate the watermark level (the level at which the display plane will
549
 * Calculate the watermark level (the level at which the display plane will
1088
 * start fetching from memory again).  Each chip has a different display
550
 * start fetching from memory again).  Each chip has a different display
1089
 * FIFO size and allocation, so the caller needs to figure that out and pass
551
 * FIFO size and allocation, so the caller needs to figure that out and pass
1090
 * in the correct intel_watermark_params structure.
552
 * in the correct intel_watermark_params structure.
1091
 *
553
 *
1092
 * As the pixel clock runs, the FIFO will be drained at a rate that depends
554
 * As the pixel clock runs, the FIFO will be drained at a rate that depends
1093
 * on the pixel size.  When it reaches the watermark level, it'll start
555
 * on the pixel size.  When it reaches the watermark level, it'll start
1094
 * fetching FIFO line sized based chunks from memory until the FIFO fills
556
 * fetching FIFO line sized based chunks from memory until the FIFO fills
1095
 * past the watermark point.  If the FIFO drains completely, a FIFO underrun
557
 * past the watermark point.  If the FIFO drains completely, a FIFO underrun
1096
 * will occur, and a display engine hang could result.
558
 * will occur, and a display engine hang could result.
1097
 */
559
 */
1098
static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
560
static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
1099
					const struct intel_watermark_params *wm,
561
					const struct intel_watermark_params *wm,
1100
					int fifo_size,
562
					int fifo_size,
1101
					int pixel_size,
563
					int pixel_size,
1102
					unsigned long latency_ns)
564
					unsigned long latency_ns)
1103
{
565
{
1104
	long entries_required, wm_size;
566
	long entries_required, wm_size;
1105
 
567
 
1106
	/*
568
	/*
1107
	 * Note: we need to make sure we don't overflow for various clock &
569
	 * Note: we need to make sure we don't overflow for various clock &
1108
	 * latency values.
570
	 * latency values.
1109
	 * clocks go from a few thousand to several hundred thousand.
571
	 * clocks go from a few thousand to several hundred thousand.
1110
	 * latency is usually a few thousand
572
	 * latency is usually a few thousand
1111
	 */
573
	 */
1112
	entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
574
	entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
1113
		1000;
575
		1000;
1114
	entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
576
	entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
1115
 
577
 
1116
	DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
578
	DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
1117
 
579
 
1118
	wm_size = fifo_size - (entries_required + wm->guard_size);
580
	wm_size = fifo_size - (entries_required + wm->guard_size);
1119
 
581
 
1120
	DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
582
	DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
1121
 
583
 
1122
	/* Don't promote wm_size to unsigned... */
584
	/* Don't promote wm_size to unsigned... */
1123
	if (wm_size > (long)wm->max_wm)
585
	if (wm_size > (long)wm->max_wm)
1124
		wm_size = wm->max_wm;
586
		wm_size = wm->max_wm;
1125
	if (wm_size <= 0)
587
	if (wm_size <= 0)
1126
		wm_size = wm->default_wm;
588
		wm_size = wm->default_wm;
1127
 
589
 
1128
	/*
590
	/*
1129
	 * Bspec seems to indicate that the value shouldn't be lower than
591
	 * Bspec seems to indicate that the value shouldn't be lower than
1130
	 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
592
	 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
1131
	 * Lets go for 8 which is the burst size since certain platforms
593
	 * Lets go for 8 which is the burst size since certain platforms
1132
	 * already use a hardcoded 8 (which is what the spec says should be
594
	 * already use a hardcoded 8 (which is what the spec says should be
1133
	 * done).
595
	 * done).
1134
	 */
596
	 */
1135
	if (wm_size <= 8)
597
	if (wm_size <= 8)
1136
		wm_size = 8;
598
		wm_size = 8;
1137
 
599
 
1138
	return wm_size;
600
	return wm_size;
1139
}
601
}
1140
 
602
 
1141
static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
603
static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
1142
{
604
{
1143
	struct drm_crtc *crtc, *enabled = NULL;
605
	struct drm_crtc *crtc, *enabled = NULL;
1144
 
606
 
1145
	for_each_crtc(dev, crtc) {
607
	for_each_crtc(dev, crtc) {
1146
		if (intel_crtc_active(crtc)) {
608
		if (intel_crtc_active(crtc)) {
1147
			if (enabled)
609
			if (enabled)
1148
				return NULL;
610
				return NULL;
1149
			enabled = crtc;
611
			enabled = crtc;
1150
		}
612
		}
1151
	}
613
	}
1152
 
614
 
1153
	return enabled;
615
	return enabled;
1154
}
616
}
1155
 
617
 
1156
static void pineview_update_wm(struct drm_crtc *unused_crtc)
618
static void pineview_update_wm(struct drm_crtc *unused_crtc)
1157
{
619
{
1158
	struct drm_device *dev = unused_crtc->dev;
620
	struct drm_device *dev = unused_crtc->dev;
1159
	struct drm_i915_private *dev_priv = dev->dev_private;
621
	struct drm_i915_private *dev_priv = dev->dev_private;
1160
	struct drm_crtc *crtc;
622
	struct drm_crtc *crtc;
1161
	const struct cxsr_latency *latency;
623
	const struct cxsr_latency *latency;
1162
	u32 reg;
624
	u32 reg;
1163
	unsigned long wm;
625
	unsigned long wm;
1164
 
626
 
1165
	latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
627
	latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
1166
					 dev_priv->fsb_freq, dev_priv->mem_freq);
628
					 dev_priv->fsb_freq, dev_priv->mem_freq);
1167
	if (!latency) {
629
	if (!latency) {
1168
		DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
630
		DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
1169
		intel_set_memory_cxsr(dev_priv, false);
631
		intel_set_memory_cxsr(dev_priv, false);
1170
		return;
632
		return;
1171
	}
633
	}
1172
 
634
 
1173
	crtc = single_enabled_crtc(dev);
635
	crtc = single_enabled_crtc(dev);
1174
	if (crtc) {
636
	if (crtc) {
1175
		const struct drm_display_mode *adjusted_mode;
637
		const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1176
		int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
638
		int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
1177
		int clock;
-
 
1178
 
-
 
1179
		adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
-
 
1180
		clock = adjusted_mode->crtc_clock;
639
		int clock = adjusted_mode->crtc_clock;
1181
 
640
 
1182
		/* Display SR */
641
		/* Display SR */
1183
		wm = intel_calculate_wm(clock, &pineview_display_wm,
642
		wm = intel_calculate_wm(clock, &pineview_display_wm,
1184
					pineview_display_wm.fifo_size,
643
					pineview_display_wm.fifo_size,
1185
					pixel_size, latency->display_sr);
644
					pixel_size, latency->display_sr);
1186
		reg = I915_READ(DSPFW1);
645
		reg = I915_READ(DSPFW1);
1187
		reg &= ~DSPFW_SR_MASK;
646
		reg &= ~DSPFW_SR_MASK;
1188
		reg |= wm << DSPFW_SR_SHIFT;
647
		reg |= FW_WM(wm, SR);
1189
		I915_WRITE(DSPFW1, reg);
648
		I915_WRITE(DSPFW1, reg);
1190
		DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
649
		DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
1191
 
650
 
1192
		/* cursor SR */
651
		/* cursor SR */
1193
		wm = intel_calculate_wm(clock, &pineview_cursor_wm,
652
		wm = intel_calculate_wm(clock, &pineview_cursor_wm,
1194
					pineview_display_wm.fifo_size,
653
					pineview_display_wm.fifo_size,
1195
					pixel_size, latency->cursor_sr);
654
					pixel_size, latency->cursor_sr);
1196
		reg = I915_READ(DSPFW3);
655
		reg = I915_READ(DSPFW3);
1197
		reg &= ~DSPFW_CURSOR_SR_MASK;
656
		reg &= ~DSPFW_CURSOR_SR_MASK;
1198
		reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
657
		reg |= FW_WM(wm, CURSOR_SR);
1199
		I915_WRITE(DSPFW3, reg);
658
		I915_WRITE(DSPFW3, reg);
1200
 
659
 
1201
		/* Display HPLL off SR */
660
		/* Display HPLL off SR */
1202
		wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
661
		wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
1203
					pineview_display_hplloff_wm.fifo_size,
662
					pineview_display_hplloff_wm.fifo_size,
1204
					pixel_size, latency->display_hpll_disable);
663
					pixel_size, latency->display_hpll_disable);
1205
		reg = I915_READ(DSPFW3);
664
		reg = I915_READ(DSPFW3);
1206
		reg &= ~DSPFW_HPLL_SR_MASK;
665
		reg &= ~DSPFW_HPLL_SR_MASK;
1207
		reg |= wm & DSPFW_HPLL_SR_MASK;
666
		reg |= FW_WM(wm, HPLL_SR);
1208
		I915_WRITE(DSPFW3, reg);
667
		I915_WRITE(DSPFW3, reg);
1209
 
668
 
1210
		/* cursor HPLL off SR */
669
		/* cursor HPLL off SR */
1211
		wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
670
		wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
1212
					pineview_display_hplloff_wm.fifo_size,
671
					pineview_display_hplloff_wm.fifo_size,
1213
					pixel_size, latency->cursor_hpll_disable);
672
					pixel_size, latency->cursor_hpll_disable);
1214
		reg = I915_READ(DSPFW3);
673
		reg = I915_READ(DSPFW3);
1215
		reg &= ~DSPFW_HPLL_CURSOR_MASK;
674
		reg &= ~DSPFW_HPLL_CURSOR_MASK;
1216
		reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
675
		reg |= FW_WM(wm, HPLL_CURSOR);
1217
		I915_WRITE(DSPFW3, reg);
676
		I915_WRITE(DSPFW3, reg);
1218
		DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
677
		DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
1219
 
678
 
1220
		intel_set_memory_cxsr(dev_priv, true);
679
		intel_set_memory_cxsr(dev_priv, true);
1221
	} else {
680
	} else {
1222
		intel_set_memory_cxsr(dev_priv, false);
681
		intel_set_memory_cxsr(dev_priv, false);
1223
	}
682
	}
1224
}
683
}
1225
 
684
 
1226
static bool g4x_compute_wm0(struct drm_device *dev,
685
static bool g4x_compute_wm0(struct drm_device *dev,
1227
			    int plane,
686
			    int plane,
1228
			    const struct intel_watermark_params *display,
687
			    const struct intel_watermark_params *display,
1229
			    int display_latency_ns,
688
			    int display_latency_ns,
1230
			    const struct intel_watermark_params *cursor,
689
			    const struct intel_watermark_params *cursor,
1231
			    int cursor_latency_ns,
690
			    int cursor_latency_ns,
1232
			    int *plane_wm,
691
			    int *plane_wm,
1233
			    int *cursor_wm)
692
			    int *cursor_wm)
1234
{
693
{
1235
	struct drm_crtc *crtc;
694
	struct drm_crtc *crtc;
1236
	const struct drm_display_mode *adjusted_mode;
695
	const struct drm_display_mode *adjusted_mode;
1237
	int htotal, hdisplay, clock, pixel_size;
696
	int htotal, hdisplay, clock, pixel_size;
1238
	int line_time_us, line_count;
697
	int line_time_us, line_count;
1239
	int entries, tlb_miss;
698
	int entries, tlb_miss;
1240
 
699
 
1241
	crtc = intel_get_crtc_for_plane(dev, plane);
700
	crtc = intel_get_crtc_for_plane(dev, plane);
1242
	if (!intel_crtc_active(crtc)) {
701
	if (!intel_crtc_active(crtc)) {
1243
		*cursor_wm = cursor->guard_size;
702
		*cursor_wm = cursor->guard_size;
1244
		*plane_wm = display->guard_size;
703
		*plane_wm = display->guard_size;
1245
        return false;
704
		return false;
1246
	}
705
	}
1247
 
706
 
1248
	adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
707
	adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1249
	clock = adjusted_mode->crtc_clock;
708
	clock = adjusted_mode->crtc_clock;
1250
	htotal = adjusted_mode->crtc_htotal;
709
	htotal = adjusted_mode->crtc_htotal;
1251
	hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
710
	hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
1252
	pixel_size = crtc->primary->fb->bits_per_pixel / 8;
711
	pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
1253
 
712
 
1254
	/* Use the small buffer method to calculate plane watermark */
713
	/* Use the small buffer method to calculate plane watermark */
1255
	entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
714
	entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
1256
	tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
715
	tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
1257
	if (tlb_miss > 0)
716
	if (tlb_miss > 0)
1258
		entries += tlb_miss;
717
		entries += tlb_miss;
1259
	entries = DIV_ROUND_UP(entries, display->cacheline_size);
718
	entries = DIV_ROUND_UP(entries, display->cacheline_size);
1260
	*plane_wm = entries + display->guard_size;
719
	*plane_wm = entries + display->guard_size;
1261
	if (*plane_wm > (int)display->max_wm)
720
	if (*plane_wm > (int)display->max_wm)
1262
		*plane_wm = display->max_wm;
721
		*plane_wm = display->max_wm;
1263
 
722
 
1264
	/* Use the large buffer method to calculate cursor watermark */
723
	/* Use the large buffer method to calculate cursor watermark */
1265
	line_time_us = max(htotal * 1000 / clock, 1);
724
	line_time_us = max(htotal * 1000 / clock, 1);
1266
	line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
725
	line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
1267
	entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
726
	entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
1268
	tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
727
	tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
1269
	if (tlb_miss > 0)
728
	if (tlb_miss > 0)
1270
		entries += tlb_miss;
729
		entries += tlb_miss;
1271
	entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
730
	entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1272
	*cursor_wm = entries + cursor->guard_size;
731
	*cursor_wm = entries + cursor->guard_size;
1273
	if (*cursor_wm > (int)cursor->max_wm)
732
	if (*cursor_wm > (int)cursor->max_wm)
1274
		*cursor_wm = (int)cursor->max_wm;
733
		*cursor_wm = (int)cursor->max_wm;
1275
 
734
 
1276
	return true;
735
	return true;
1277
}
736
}
1278
 
737
 
1279
/*
738
/*
1280
 * Check the wm result.
739
 * Check the wm result.
1281
 *
740
 *
1282
 * If any calculated watermark values is larger than the maximum value that
741
 * If any calculated watermark values is larger than the maximum value that
1283
 * can be programmed into the associated watermark register, that watermark
742
 * can be programmed into the associated watermark register, that watermark
1284
 * must be disabled.
743
 * must be disabled.
1285
 */
744
 */
1286
static bool g4x_check_srwm(struct drm_device *dev,
745
static bool g4x_check_srwm(struct drm_device *dev,
1287
			   int display_wm, int cursor_wm,
746
			   int display_wm, int cursor_wm,
1288
			   const struct intel_watermark_params *display,
747
			   const struct intel_watermark_params *display,
1289
			   const struct intel_watermark_params *cursor)
748
			   const struct intel_watermark_params *cursor)
1290
{
749
{
1291
	DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
750
	DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
1292
		      display_wm, cursor_wm);
751
		      display_wm, cursor_wm);
1293
 
752
 
1294
	if (display_wm > display->max_wm) {
753
	if (display_wm > display->max_wm) {
1295
		DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
754
		DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
1296
			      display_wm, display->max_wm);
755
			      display_wm, display->max_wm);
1297
		return false;
756
		return false;
1298
	}
757
	}
1299
 
758
 
1300
	if (cursor_wm > cursor->max_wm) {
759
	if (cursor_wm > cursor->max_wm) {
1301
		DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
760
		DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
1302
			      cursor_wm, cursor->max_wm);
761
			      cursor_wm, cursor->max_wm);
1303
		return false;
762
		return false;
1304
	}
763
	}
1305
 
764
 
1306
	if (!(display_wm || cursor_wm)) {
765
	if (!(display_wm || cursor_wm)) {
1307
		DRM_DEBUG_KMS("SR latency is 0, disabling\n");
766
		DRM_DEBUG_KMS("SR latency is 0, disabling\n");
1308
		return false;
767
		return false;
1309
	}
768
	}
1310
 
769
 
1311
	return true;
770
	return true;
1312
}
771
}
1313
 
772
 
1314
static bool g4x_compute_srwm(struct drm_device *dev,
773
static bool g4x_compute_srwm(struct drm_device *dev,
1315
			     int plane,
774
			     int plane,
1316
			     int latency_ns,
775
			     int latency_ns,
1317
			     const struct intel_watermark_params *display,
776
			     const struct intel_watermark_params *display,
1318
			     const struct intel_watermark_params *cursor,
777
			     const struct intel_watermark_params *cursor,
1319
			     int *display_wm, int *cursor_wm)
778
			     int *display_wm, int *cursor_wm)
1320
{
779
{
1321
	struct drm_crtc *crtc;
780
	struct drm_crtc *crtc;
1322
	const struct drm_display_mode *adjusted_mode;
781
	const struct drm_display_mode *adjusted_mode;
1323
	int hdisplay, htotal, pixel_size, clock;
782
	int hdisplay, htotal, pixel_size, clock;
1324
	unsigned long line_time_us;
783
	unsigned long line_time_us;
1325
	int line_count, line_size;
784
	int line_count, line_size;
1326
	int small, large;
785
	int small, large;
1327
	int entries;
786
	int entries;
1328
 
787
 
1329
	if (!latency_ns) {
788
	if (!latency_ns) {
1330
		*display_wm = *cursor_wm = 0;
789
		*display_wm = *cursor_wm = 0;
1331
		return false;
790
		return false;
1332
	}
791
	}
1333
 
792
 
1334
	crtc = intel_get_crtc_for_plane(dev, plane);
793
	crtc = intel_get_crtc_for_plane(dev, plane);
1335
	adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
794
	adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1336
	clock = adjusted_mode->crtc_clock;
795
	clock = adjusted_mode->crtc_clock;
1337
	htotal = adjusted_mode->crtc_htotal;
796
	htotal = adjusted_mode->crtc_htotal;
1338
	hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
797
	hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
1339
	pixel_size = crtc->primary->fb->bits_per_pixel / 8;
798
	pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
1340
 
799
 
1341
	line_time_us = max(htotal * 1000 / clock, 1);
800
	line_time_us = max(htotal * 1000 / clock, 1);
1342
	line_count = (latency_ns / line_time_us + 1000) / 1000;
801
	line_count = (latency_ns / line_time_us + 1000) / 1000;
1343
	line_size = hdisplay * pixel_size;
802
	line_size = hdisplay * pixel_size;
1344
 
803
 
1345
	/* Use the minimum of the small and large buffer method for primary */
804
	/* Use the minimum of the small and large buffer method for primary */
1346
	small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
805
	small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
1347
	large = line_count * line_size;
806
	large = line_count * line_size;
1348
 
807
 
1349
	entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
808
	entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
1350
	*display_wm = entries + display->guard_size;
809
	*display_wm = entries + display->guard_size;
1351
 
810
 
1352
	/* calculate the self-refresh watermark for display cursor */
811
	/* calculate the self-refresh watermark for display cursor */
1353
	entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
812
	entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
1354
	entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
813
	entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
1355
	*cursor_wm = entries + cursor->guard_size;
814
	*cursor_wm = entries + cursor->guard_size;
1356
 
815
 
1357
	return g4x_check_srwm(dev,
816
	return g4x_check_srwm(dev,
1358
			      *display_wm, *cursor_wm,
817
			      *display_wm, *cursor_wm,
1359
			      display, cursor);
818
			      display, cursor);
1360
}
819
}
1361
 
820
 
1362
static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
821
#define FW_WM_VLV(value, plane) \
-
 
822
	(((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1363
				      int pixel_size,
823
 
1364
				      int *prec_mult,
824
static void vlv_write_wm_values(struct intel_crtc *crtc,
1365
				      int *drain_latency)
825
				const struct vlv_wm_values *wm)
1366
{
826
{
1367
	struct drm_device *dev = crtc->dev;
827
	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1368
	int entries;
-
 
-
 
828
	enum pipe pipe = crtc->pipe;
1369
	int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
829
 
-
 
830
	I915_WRITE(VLV_DDL(pipe),
-
 
831
		   (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
1370
 
832
		   (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
-
 
833
		   (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
-
 
834
		   (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
-
 
835
 
-
 
836
	I915_WRITE(DSPFW1,
-
 
837
		   FW_WM(wm->sr.plane, SR) |
-
 
838
		   FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
-
 
839
		   FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
-
 
840
		   FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
1371
	if (WARN(clock == 0, "Pixel clock is zero!\n"))
841
	I915_WRITE(DSPFW2,
1372
		return false;
842
		   FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
-
 
843
		   FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
-
 
844
		   FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
-
 
845
	I915_WRITE(DSPFW3,
-
 
846
		   FW_WM(wm->sr.cursor, CURSOR_SR));
1373
 
847
 
1374
	if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
848
	if (IS_CHERRYVIEW(dev_priv)) {
1375
		return false;
849
		I915_WRITE(DSPFW7_CHV,
-
 
850
			   FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
-
 
851
			   FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
-
 
852
		I915_WRITE(DSPFW8_CHV,
-
 
853
			   FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
-
 
854
			   FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
1376
 
855
		I915_WRITE(DSPFW9_CHV,
-
 
856
			   FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
-
 
857
			   FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
-
 
858
		I915_WRITE(DSPHOWM,
-
 
859
			   FW_WM(wm->sr.plane >> 9, SR_HI) |
-
 
860
			   FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
-
 
861
			   FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
-
 
862
			   FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
-
 
863
			   FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
-
 
864
			   FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
1377
	entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
865
			   FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
-
 
866
			   FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
1378
	if (IS_CHERRYVIEW(dev))
867
			   FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
-
 
868
			   FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
-
 
869
	} else {
1379
		*prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_32 :
870
		I915_WRITE(DSPFW7,
-
 
871
			   FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
-
 
872
			   FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
-
 
873
		I915_WRITE(DSPHOWM,
-
 
874
			   FW_WM(wm->sr.plane >> 9, SR_HI) |
-
 
875
			   FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
-
 
876
			   FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
-
 
877
			   FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
-
 
878
			   FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
-
 
879
			   FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
-
 
880
			   FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
-
 
881
	}
-
 
882
 
-
 
883
	/* zero (unused) WM1 watermarks */
-
 
884
	I915_WRITE(DSPFW4, 0);
-
 
885
	I915_WRITE(DSPFW5, 0);
-
 
886
	I915_WRITE(DSPFW6, 0);
-
 
887
	I915_WRITE(DSPHOWM1, 0);
-
 
888
 
-
 
889
	POSTING_READ(DSPFW1);
-
 
890
}
-
 
891
 
-
 
892
#undef FW_WM_VLV
-
 
893
 
-
 
894
enum vlv_wm_level {
-
 
895
	VLV_WM_LEVEL_PM2,
1380
					       DRAIN_LATENCY_PRECISION_16;
896
	VLV_WM_LEVEL_PM5,
-
 
897
	VLV_WM_LEVEL_DDR_DVFS,
-
 
898
};
-
 
899
 
-
 
900
/* latency must be in 0.1us units. */
-
 
901
static unsigned int vlv_wm_method2(unsigned int pixel_rate,
-
 
902
				   unsigned int pipe_htotal,
-
 
903
				   unsigned int horiz_pixels,
1381
	else
904
				   unsigned int bytes_per_pixel,
-
 
905
				   unsigned int latency)
1382
		*prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
906
{
1383
					       DRAIN_LATENCY_PRECISION_32;
907
	unsigned int ret;
1384
	*drain_latency = (64 * (*prec_mult) * 4) / entries;
908
 
-
 
909
	ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1385
 
910
	ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1386
	if (*drain_latency > DRAIN_LATENCY_MASK)
911
	ret = DIV_ROUND_UP(ret, 64);
-
 
912
 
-
 
913
	return ret;
-
 
914
}
-
 
915
 
-
 
916
static void vlv_setup_wm_latency(struct drm_device *dev)
-
 
917
{
-
 
918
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
919
 
-
 
920
	/* all latencies in usec */
-
 
921
	dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
-
 
922
 
1387
		*drain_latency = DRAIN_LATENCY_MASK;
923
	dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
-
 
924
 
-
 
925
	if (IS_CHERRYVIEW(dev_priv)) {
1388
 
926
		dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
-
 
927
		dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
-
 
928
 
-
 
929
		dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
-
 
930
	}
1389
	return true;
931
}
-
 
932
 
-
 
933
static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
-
 
934
				     struct intel_crtc *crtc,
-
 
935
				     const struct intel_plane_state *state,
-
 
936
				     int level)
1390
}
937
{
-
 
938
	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
-
 
939
	int clock, htotal, pixel_size, width, wm;
-
 
940
 
-
 
941
	if (dev_priv->wm.pri_latency[level] == 0)
-
 
942
		return USHRT_MAX;
-
 
943
 
-
 
944
	if (!state->visible)
-
 
945
		return 0;
-
 
946
 
-
 
947
	pixel_size = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
-
 
948
	clock = crtc->config->base.adjusted_mode.crtc_clock;
-
 
949
	htotal = crtc->config->base.adjusted_mode.crtc_htotal;
-
 
950
	width = crtc->config->pipe_src_w;
-
 
951
	if (WARN_ON(htotal == 0))
-
 
952
		htotal = 1;
1391
 
953
 
-
 
954
	if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
-
 
955
		/*
-
 
956
		 * FIXME the formula gives values that are
-
 
957
		 * too big for the cursor FIFO, and hence we
-
 
958
		 * would never be able to use cursors. For
-
 
959
		 * now just hardcode the watermark.
-
 
960
		 */
-
 
961
		wm = 63;
1392
/*
962
	} else {
1393
 * Update drain latency registers of memory arbiter
963
		wm = vlv_wm_method2(clock, htotal, width, pixel_size,
1394
 *
964
				    dev_priv->wm.pri_latency[level] * 10);
1395
 * Valleyview SoC has a new memory arbiter and needs drain latency registers
965
	}
1396
 * to be programmed. Each plane has a drain latency multiplier and a drain
966
 
1397
 * latency value.
-
 
1398
 */
967
	return min_t(int, wm, USHRT_MAX);
1399
 
968
}
1400
static void vlv_update_drain_latency(struct drm_crtc *crtc)
969
 
1401
{
-
 
1402
	struct drm_device *dev = crtc->dev;
-
 
1403
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
1404
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
 
1405
	int pixel_size;
-
 
1406
	int drain_latency;
-
 
-
 
970
static void vlv_compute_fifo(struct intel_crtc *crtc)
1407
	enum pipe pipe = intel_crtc->pipe;
971
{
1408
	int plane_prec, prec_mult, plane_dl;
972
	struct drm_device *dev = crtc->base.dev;
-
 
973
	struct vlv_wm_state *wm_state = &crtc->wm_state;
-
 
974
	struct intel_plane *plane;
1409
	const int high_precision = IS_CHERRYVIEW(dev) ?
975
	unsigned int total_rate = 0;
-
 
976
	const int fifo_size = 512 - 1;
-
 
977
	int fifo_extra, fifo_left = fifo_size;
-
 
978
 
-
 
979
	for_each_intel_plane_on_crtc(dev, crtc, plane) {
-
 
980
		struct intel_plane_state *state =
1410
		DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
981
			to_intel_plane_state(plane->base.state);
1411
 
-
 
1412
	plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_HIGH |
-
 
1413
		   DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_HIGH |
982
 
1414
		   (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
983
		if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1415
 
-
 
1416
	if (!intel_crtc_active(crtc)) {
-
 
1417
		I915_WRITE(VLV_DDL(pipe), plane_dl);
984
			continue;
1418
		return;
-
 
1419
	}
-
 
1420
 
985
 
1421
	/* Primary plane Drain Latency */
-
 
1422
	pixel_size = crtc->primary->fb->bits_per_pixel / 8;	/* BPP */
-
 
1423
	if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
-
 
1424
		plane_prec = (prec_mult == high_precision) ?
986
		if (state->visible) {
1425
					   DDL_PLANE_PRECISION_HIGH :
-
 
1426
					   DDL_PLANE_PRECISION_LOW;
-
 
1427
		plane_dl |= plane_prec | drain_latency;
-
 
1428
	}
987
			wm_state->num_active_planes++;
1429
 
988
			total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1430
	/* Cursor Drain Latency
989
		}
1431
	 * BPP is always 4 for cursor
-
 
1432
	 */
990
	}
-
 
991
 
1433
	pixel_size = 4;
992
	for_each_intel_plane_on_crtc(dev, crtc, plane) {
-
 
993
		struct intel_plane_state *state =
1434
 
994
			to_intel_plane_state(plane->base.state);
1435
	/* Program cursor DL only if it is enabled */
995
		unsigned int rate;
-
 
996
 
-
 
997
		if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
-
 
998
			plane->wm.fifo_size = 63;
-
 
999
			continue;
-
 
1000
		}
-
 
1001
 
-
 
1002
		if (!state->visible) {
-
 
1003
			plane->wm.fifo_size = 0;
-
 
1004
			continue;
-
 
1005
		}
-
 
1006
 
-
 
1007
		rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
-
 
1008
		plane->wm.fifo_size = fifo_size * rate / total_rate;
-
 
1009
		fifo_left -= plane->wm.fifo_size;
-
 
1010
	}
-
 
1011
 
-
 
1012
	fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
-
 
1013
 
-
 
1014
	/* spread the remainder evenly */
-
 
1015
	for_each_intel_plane_on_crtc(dev, crtc, plane) {
-
 
1016
		int plane_extra;
-
 
1017
 
-
 
1018
		if (fifo_left == 0)
-
 
1019
			break;
-
 
1020
 
-
 
1021
		if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
-
 
1022
			continue;
-
 
1023
 
1436
	if (intel_crtc->cursor_base &&
1024
		/* give it all to the first plane if none are active */
1437
	    vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
1025
		if (plane->wm.fifo_size == 0 &&
1438
		plane_prec = (prec_mult == high_precision) ?
-
 
1439
					   DDL_CURSOR_PRECISION_HIGH :
-
 
1440
					   DDL_CURSOR_PRECISION_LOW;
1026
		    wm_state->num_active_planes)
1441
		plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
-
 
1442
	}
-
 
1443
 
-
 
1444
	I915_WRITE(VLV_DDL(pipe), plane_dl);
-
 
1445
}
1027
			continue;
-
 
1028
 
-
 
1029
		plane_extra = min(fifo_extra, fifo_left);
-
 
1030
		plane->wm.fifo_size += plane_extra;
1446
 
1031
		fifo_left -= plane_extra;
1447
#define single_plane_enabled(mask) is_power_of_2(mask)
-
 
1448
 
1032
	}
1449
static void valleyview_update_wm(struct drm_crtc *crtc)
1033
 
1450
{
-
 
1451
	struct drm_device *dev = crtc->dev;
-
 
1452
	static const int sr_latency_ns = 12000;
1034
	WARN_ON(fifo_left != 0);
-
 
1035
}
-
 
1036
 
-
 
1037
static void vlv_invert_wms(struct intel_crtc *crtc)
1453
	struct drm_i915_private *dev_priv = dev->dev_private;
1038
{
-
 
1039
	struct vlv_wm_state *wm_state = &crtc->wm_state;
-
 
1040
	int level;
-
 
1041
 
1454
	int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1042
	for (level = 0; level < wm_state->num_levels; level++) {
1455
	int plane_sr, cursor_sr;
1043
		struct drm_device *dev = crtc->base.dev;
-
 
1044
		const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
-
 
1045
		struct intel_plane *plane;
1456
	int ignore_plane_sr, ignore_cursor_sr;
1046
 
-
 
1047
		wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
-
 
1048
		wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
-
 
1049
 
-
 
1050
		for_each_intel_plane_on_crtc(dev, crtc, plane) {
-
 
1051
			switch (plane->base.type) {
-
 
1052
				int sprite;
-
 
1053
			case DRM_PLANE_TYPE_CURSOR:
1457
	unsigned int enabled = 0;
1054
				wm_state->wm[level].cursor = plane->wm.fifo_size -
-
 
1055
					wm_state->wm[level].cursor;
1458
	bool cxsr_enabled;
1056
				break;
1459
 
1057
			case DRM_PLANE_TYPE_PRIMARY:
1460
	vlv_update_drain_latency(crtc);
1058
				wm_state->wm[level].primary = plane->wm.fifo_size -
1461
 
1059
					wm_state->wm[level].primary;
-
 
1060
				break;
-
 
1061
			case DRM_PLANE_TYPE_OVERLAY:
1462
	if (g4x_compute_wm0(dev, PIPE_A,
1062
				sprite = plane->plane;
-
 
1063
				wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1463
			    &valleyview_wm_info, pessimal_latency_ns,
1064
					wm_state->wm[level].sprite[sprite];
1464
			    &valleyview_cursor_wm_info, pessimal_latency_ns,
1065
				break;
-
 
1066
			}
1465
			    &planea_wm, &cursora_wm))
1067
		}
-
 
1068
	}
1466
		enabled |= 1 << PIPE_A;
1069
}
-
 
1070
 
1467
 
1071
static void vlv_compute_wm(struct intel_crtc *crtc)
1468
	if (g4x_compute_wm0(dev, PIPE_B,
1072
{
-
 
1073
	struct drm_device *dev = crtc->base.dev;
1469
			    &valleyview_wm_info, pessimal_latency_ns,
1074
	struct vlv_wm_state *wm_state = &crtc->wm_state;
1470
			    &valleyview_cursor_wm_info, pessimal_latency_ns,
1075
	struct intel_plane *plane;
1471
			    &planeb_wm, &cursorb_wm))
1076
	int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1472
		enabled |= 1 << PIPE_B;
1077
	int level;
-
 
1078
 
1473
 
1079
	memset(wm_state, 0, sizeof(*wm_state));
1474
	if (single_plane_enabled(enabled) &&
-
 
1475
	    g4x_compute_srwm(dev, ffs(enabled) - 1,
1080
 
1476
			     sr_latency_ns,
1081
	wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
1477
			     &valleyview_wm_info,
-
 
1478
			     &valleyview_cursor_wm_info,
1082
	wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
1479
			     &plane_sr, &ignore_cursor_sr) &&
-
 
1480
	    g4x_compute_srwm(dev, ffs(enabled) - 1,
-
 
1481
			     2*sr_latency_ns,
-
 
1482
			     &valleyview_wm_info,
-
 
1483
			     &valleyview_cursor_wm_info,
-
 
1484
			     &ignore_plane_sr, &cursor_sr)) {
1083
 
1485
		cxsr_enabled = true;
-
 
1486
	} else {
-
 
1487
		cxsr_enabled = false;
1084
	wm_state->num_active_planes = 0;
1488
		intel_set_memory_cxsr(dev_priv, false);
-
 
1489
		plane_sr = cursor_sr = 0;
-
 
-
 
1085
 
-
 
1086
	vlv_compute_fifo(crtc);
-
 
1087
 
-
 
1088
	if (wm_state->num_active_planes != 1)
-
 
1089
		wm_state->cxsr = false;
-
 
1090
 
-
 
1091
	if (wm_state->cxsr) {
-
 
1092
		for (level = 0; level < wm_state->num_levels; level++) {
-
 
1093
			wm_state->sr[level].plane = sr_fifo_size;
-
 
1094
			wm_state->sr[level].cursor = 63;
-
 
1095
		}
-
 
1096
	}
-
 
1097
 
-
 
1098
	for_each_intel_plane_on_crtc(dev, crtc, plane) {
-
 
1099
		struct intel_plane_state *state =
-
 
1100
			to_intel_plane_state(plane->base.state);
-
 
1101
 
-
 
1102
		if (!state->visible)
-
 
1103
			continue;
-
 
1104
 
-
 
1105
		/* normal watermarks */
-
 
1106
		for (level = 0; level < wm_state->num_levels; level++) {
-
 
1107
			int wm = vlv_compute_wm_level(plane, crtc, state, level);
-
 
1108
			int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
-
 
1109
 
-
 
1110
			/* hack */
-
 
1111
			if (WARN_ON(level == 0 && wm > max_wm))
-
 
1112
				wm = max_wm;
-
 
1113
 
1490
	}
1114
			if (wm > plane->wm.fifo_size)
-
 
1115
				break;
-
 
1116
 
-
 
1117
			switch (plane->base.type) {
-
 
1118
				int sprite;
-
 
1119
			case DRM_PLANE_TYPE_CURSOR:
-
 
1120
				wm_state->wm[level].cursor = wm;
-
 
1121
				break;
-
 
1122
			case DRM_PLANE_TYPE_PRIMARY:
-
 
1123
				wm_state->wm[level].primary = wm;
-
 
1124
				break;
-
 
1125
			case DRM_PLANE_TYPE_OVERLAY:
-
 
1126
				sprite = plane->plane;
-
 
1127
				wm_state->wm[level].sprite[sprite] = wm;
-
 
1128
				break;
1491
 
1129
			}
-
 
1130
		}
-
 
1131
 
-
 
1132
		wm_state->num_levels = level;
-
 
1133
 
-
 
1134
		if (!wm_state->cxsr)
-
 
1135
			continue;
-
 
1136
 
-
 
1137
		/* maxfifo watermarks */
-
 
1138
		switch (plane->base.type) {
-
 
1139
			int sprite, level;
-
 
1140
		case DRM_PLANE_TYPE_CURSOR:
-
 
1141
			for (level = 0; level < wm_state->num_levels; level++)
-
 
1142
				wm_state->sr[level].cursor =
-
 
1143
					wm_state->wm[level].cursor;
-
 
1144
			break;
1492
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1145
		case DRM_PLANE_TYPE_PRIMARY:
-
 
1146
			for (level = 0; level < wm_state->num_levels; level++)
-
 
1147
				wm_state->sr[level].plane =
-
 
1148
					min(wm_state->sr[level].plane,
-
 
1149
					    wm_state->wm[level].primary);
-
 
1150
			break;
-
 
1151
		case DRM_PLANE_TYPE_OVERLAY:
1493
		      "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1152
			sprite = plane->plane;
1494
		      planea_wm, cursora_wm,
1153
			for (level = 0; level < wm_state->num_levels; level++)
1495
		      planeb_wm, cursorb_wm,
1154
				wm_state->sr[level].plane =
1496
		      plane_sr, cursor_sr);
-
 
1497
 
1155
					min(wm_state->sr[level].plane,
1498
	I915_WRITE(DSPFW1,
1156
					    wm_state->wm[level].sprite[sprite]);
1499
		   (plane_sr << DSPFW_SR_SHIFT) |
1157
			break;
1500
		   (cursorb_wm << DSPFW_CURSORB_SHIFT) |
-
 
1501
		   (planeb_wm << DSPFW_PLANEB_SHIFT) |
-
 
1502
		   (planea_wm << DSPFW_PLANEA_SHIFT));
-
 
1503
	I915_WRITE(DSPFW2,
-
 
-
 
1158
		}
-
 
1159
	}
1504
		   (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1160
 
-
 
1161
	/* clear any (partially) filled invalid levels */
-
 
1162
	for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
1505
		   (cursora_wm << DSPFW_CURSORA_SHIFT));
1163
		memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1506
	I915_WRITE(DSPFW3,
1164
		memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1507
		   (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1165
	}
1508
		   (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1166
 
-
 
1167
	vlv_invert_wms(crtc);
1509
 
1168
}
-
 
1169
 
1510
	if (cxsr_enabled)
-
 
1511
		intel_set_memory_cxsr(dev_priv, true);
-
 
1512
}
-
 
1513
 
1170
#define VLV_FIFO(plane, value) \
1514
static void cherryview_update_wm(struct drm_crtc *crtc)
-
 
-
 
1171
	(((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
-
 
1172
 
-
 
1173
static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
-
 
1174
{
-
 
1175
	struct drm_device *dev = crtc->base.dev;
-
 
1176
	struct drm_i915_private *dev_priv = to_i915(dev);
-
 
1177
	struct intel_plane *plane;
-
 
1178
	int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
-
 
1179
 
-
 
1180
	for_each_intel_plane_on_crtc(dev, crtc, plane) {
-
 
1181
		if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1515
{
1182
			WARN_ON(plane->wm.fifo_size != 63);
-
 
1183
			continue;
1516
	struct drm_device *dev = crtc->dev;
1184
		}
-
 
1185
 
-
 
1186
		if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
-
 
1187
			sprite0_start = plane->wm.fifo_size;
-
 
1188
		else if (plane->plane == 0)
1517
	static const int sr_latency_ns = 12000;
1189
			sprite1_start = sprite0_start + plane->wm.fifo_size;
-
 
1190
		else
-
 
1191
			fifo_size = sprite1_start + plane->wm.fifo_size;
-
 
1192
	}
-
 
1193
 
-
 
1194
	WARN_ON(fifo_size != 512 - 1);
-
 
1195
 
-
 
1196
	DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
-
 
1197
		      pipe_name(crtc->pipe), sprite0_start,
-
 
1198
		      sprite1_start, fifo_size);
1518
	struct drm_i915_private *dev_priv = dev->dev_private;
1199
 
-
 
1200
	switch (crtc->pipe) {
-
 
1201
		uint32_t dsparb, dsparb2, dsparb3;
-
 
1202
	case PIPE_A:
-
 
1203
		dsparb = I915_READ(DSPARB);
-
 
1204
		dsparb2 = I915_READ(DSPARB2);
-
 
1205
 
-
 
1206
		dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
-
 
1207
			    VLV_FIFO(SPRITEB, 0xff));
-
 
1208
		dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
-
 
1209
			   VLV_FIFO(SPRITEB, sprite1_start));
-
 
1210
 
1519
	int planea_wm, planeb_wm, planec_wm;
1211
		dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
-
 
1212
			     VLV_FIFO(SPRITEB_HI, 0x1));
-
 
1213
		dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
-
 
1214
			   VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
-
 
1215
 
-
 
1216
		I915_WRITE(DSPARB, dsparb);
-
 
1217
		I915_WRITE(DSPARB2, dsparb2);
-
 
1218
		break;
-
 
1219
	case PIPE_B:
-
 
1220
		dsparb = I915_READ(DSPARB);
-
 
1221
		dsparb2 = I915_READ(DSPARB2);
-
 
1222
 
-
 
1223
		dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1520
	int cursora_wm, cursorb_wm, cursorc_wm;
1224
			    VLV_FIFO(SPRITED, 0xff));
1521
	int plane_sr, cursor_sr;
1225
		dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1522
	int ignore_plane_sr, ignore_cursor_sr;
1226
			   VLV_FIFO(SPRITED, sprite1_start));
1523
	unsigned int enabled = 0;
-
 
1524
	bool cxsr_enabled;
-
 
1525
 
-
 
1526
	vlv_update_drain_latency(crtc);
-
 
1527
 
1227
 
1528
	if (g4x_compute_wm0(dev, PIPE_A,
-
 
1529
			    &valleyview_wm_info, pessimal_latency_ns,
-
 
1530
			    &valleyview_cursor_wm_info, pessimal_latency_ns,
-
 
1531
			    &planea_wm, &cursora_wm))
1228
		dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1532
		enabled |= 1 << PIPE_A;
1229
			     VLV_FIFO(SPRITED_HI, 0xff));
1533
 
-
 
1534
	if (g4x_compute_wm0(dev, PIPE_B,
-
 
1535
			    &valleyview_wm_info, pessimal_latency_ns,
-
 
1536
			    &valleyview_cursor_wm_info, pessimal_latency_ns,
1230
		dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1537
			    &planeb_wm, &cursorb_wm))
-
 
1538
		enabled |= 1 << PIPE_B;
-
 
1539
 
-
 
1540
	if (g4x_compute_wm0(dev, PIPE_C,
-
 
1541
			    &valleyview_wm_info, pessimal_latency_ns,
-
 
1542
			    &valleyview_cursor_wm_info, pessimal_latency_ns,
-
 
1543
			    &planec_wm, &cursorc_wm))
1231
			   VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1544
		enabled |= 1 << PIPE_C;
-
 
1545
 
-
 
1546
	if (single_plane_enabled(enabled) &&
-
 
1547
	    g4x_compute_srwm(dev, ffs(enabled) - 1,
-
 
1548
			     sr_latency_ns,
1232
 
1549
			     &valleyview_wm_info,
-
 
1550
			     &valleyview_cursor_wm_info,
-
 
1551
			     &plane_sr, &ignore_cursor_sr) &&
1233
		I915_WRITE(DSPARB, dsparb);
1552
	    g4x_compute_srwm(dev, ffs(enabled) - 1,
1234
		I915_WRITE(DSPARB2, dsparb2);
1553
			     2*sr_latency_ns,
-
 
1554
			     &valleyview_wm_info,
-
 
1555
			     &valleyview_cursor_wm_info,
1235
		break;
1556
			     &ignore_plane_sr, &cursor_sr)) {
-
 
1557
		cxsr_enabled = true;
1236
	case PIPE_C:
1558
	} else {
-
 
1559
		cxsr_enabled = false;
-
 
-
 
1237
		dsparb3 = I915_READ(DSPARB3);
1560
		intel_set_memory_cxsr(dev_priv, false);
1238
		dsparb2 = I915_READ(DSPARB2);
-
 
1239
 
1561
		plane_sr = cursor_sr = 0;
1240
		dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
-
 
1241
			     VLV_FIFO(SPRITEF, 0xff));
-
 
1242
		dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
-
 
1243
			    VLV_FIFO(SPRITEF, sprite1_start));
-
 
1244
 
-
 
1245
		dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
-
 
1246
			     VLV_FIFO(SPRITEF_HI, 0xff));
-
 
1247
		dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
-
 
1248
			   VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
-
 
1249
 
-
 
1250
		I915_WRITE(DSPARB3, dsparb3);
1562
	}
1251
		I915_WRITE(DSPARB2, dsparb2);
1563
 
1252
		break;
-
 
1253
	default:
-
 
1254
		break;
1564
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1255
	}
-
 
1256
}
-
 
1257
 
1565
		      "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
1258
#undef VLV_FIFO
-
 
1259
 
1566
		      "SR: plane=%d, cursor=%d\n",
1260
static void vlv_merge_wm(struct drm_device *dev,
-
 
1261
			 struct vlv_wm_values *wm)
-
 
1262
{
1567
		      planea_wm, cursora_wm,
1263
	struct intel_crtc *crtc;
-
 
1264
	int num_active_crtcs = 0;
-
 
1265
 
-
 
1266
	wm->level = to_i915(dev)->wm.max_level;
1568
		      planeb_wm, cursorb_wm,
1267
	wm->cxsr = true;
-
 
1268
 
-
 
1269
	for_each_intel_crtc(dev, crtc) {
-
 
1270
		const struct vlv_wm_state *wm_state = &crtc->wm_state;
-
 
1271
 
-
 
1272
		if (!crtc->active)
-
 
1273
			continue;
-
 
1274
 
-
 
1275
		if (!wm_state->cxsr)
-
 
1276
			wm->cxsr = false;
1569
		      planec_wm, cursorc_wm,
1277
 
1570
		      plane_sr, cursor_sr);
1278
		num_active_crtcs++;
1571
 
1279
		wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1572
	I915_WRITE(DSPFW1,
1280
	}
1573
		   (plane_sr << DSPFW_SR_SHIFT) |
1281
 
1574
		   (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1282
	if (num_active_crtcs != 1)
1575
		   (planeb_wm << DSPFW_PLANEB_SHIFT) |
1283
		wm->cxsr = false;
1576
		   (planea_wm << DSPFW_PLANEA_SHIFT));
1284
 
1577
	I915_WRITE(DSPFW2,
1285
	if (num_active_crtcs > 1)
1578
		   (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1286
		wm->level = VLV_WM_LEVEL_PM2;
1579
		   (cursora_wm << DSPFW_CURSORA_SHIFT));
1287
 
1580
	I915_WRITE(DSPFW3,
1288
	for_each_intel_crtc(dev, crtc) {
1581
		   (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
1289
		struct vlv_wm_state *wm_state = &crtc->wm_state;
1582
		   (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1290
		enum pipe pipe = crtc->pipe;
1583
	I915_WRITE(DSPFW9_CHV,
1291
 
1584
		   (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
1292
		if (!crtc->active)
1585
					      DSPFW_CURSORC_MASK)) |
1293
			continue;
1586
		   (planec_wm << DSPFW_PLANEC_SHIFT) |
1294
 
1587
		   (cursorc_wm << DSPFW_CURSORC_SHIFT));
1295
		wm->pipe[pipe] = wm_state->wm[wm->level];
1588
 
1296
		if (wm->cxsr)
1589
	if (cxsr_enabled)
1297
			wm->sr = wm_state->sr[wm->level];
1590
		intel_set_memory_cxsr(dev_priv, true);
1298
 
1591
}
1299
		wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1592
 
1300
		wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1593
static void valleyview_update_sprite_wm(struct drm_plane *plane,
1301
		wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1594
					struct drm_crtc *crtc,
1302
		wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1595
					uint32_t sprite_width,
1303
	}
1596
					uint32_t sprite_height,
1304
}
1597
					int pixel_size,
1305
 
1598
					bool enabled, bool scaled)
1306
static void vlv_update_wm(struct drm_crtc *crtc)
1599
{
1307
{
1600
	struct drm_device *dev = crtc->dev;
1308
	struct drm_device *dev = crtc->dev;
1601
	struct drm_i915_private *dev_priv = dev->dev_private;
1309
	struct drm_i915_private *dev_priv = dev->dev_private;
1602
	int pipe = to_intel_plane(plane)->pipe;
1310
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1603
	int sprite = to_intel_plane(plane)->plane;
1311
	enum pipe pipe = intel_crtc->pipe;
1604
	int drain_latency;
1312
	struct vlv_wm_values wm = {};
1605
	int plane_prec;
-
 
1606
	int sprite_dl;
-
 
1607
	int prec_mult;
-
 
1608
	const int high_precision = IS_CHERRYVIEW(dev) ?
-
 
1609
		DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
-
 
1610
 
1313
 
1611
	sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_HIGH(sprite) |
1314
	vlv_compute_wm(intel_crtc);
1612
		    (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
1315
	vlv_merge_wm(dev, &wm);
1613
 
1316
 
1614
	if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
1317
	if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1615
						 &drain_latency)) {
-
 
1616
		plane_prec = (prec_mult == high_precision) ?
1318
		/* FIXME should be part of crtc atomic commit */
1617
					   DDL_SPRITE_PRECISION_HIGH(sprite) :
-
 
1618
					   DDL_SPRITE_PRECISION_LOW(sprite);
1319
		vlv_pipe_set_fifo_size(intel_crtc);
1619
		sprite_dl |= plane_prec |
1320
		return;
1620
			     (drain_latency << DDL_SPRITE_SHIFT(sprite));
-
 
1621
	}
1321
	}
-
 
1322
 
-
 
1323
	if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
-
 
1324
	    dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
-
 
1325
		chv_set_memory_dvfs(dev_priv, false);
-
 
1326
 
-
 
1327
	if (wm.level < VLV_WM_LEVEL_PM5 &&
1622
 
1328
	    dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
-
 
1329
		chv_set_memory_pm5(dev_priv, false);
-
 
1330
 
-
 
1331
	if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
-
 
1332
		intel_set_memory_cxsr(dev_priv, false);
-
 
1333
 
-
 
1334
	/* FIXME should be part of crtc atomic commit */
-
 
1335
	vlv_pipe_set_fifo_size(intel_crtc);
-
 
1336
 
-
 
1337
	vlv_write_wm_values(intel_crtc, &wm);
-
 
1338
 
-
 
1339
	DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
-
 
1340
		      "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
-
 
1341
		      pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
-
 
1342
		      wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
-
 
1343
		      wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
-
 
1344
 
-
 
1345
	if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
-
 
1346
		intel_set_memory_cxsr(dev_priv, true);
-
 
1347
 
-
 
1348
	if (wm.level >= VLV_WM_LEVEL_PM5 &&
-
 
1349
	    dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
-
 
1350
		chv_set_memory_pm5(dev_priv, true);
-
 
1351
 
-
 
1352
	if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
-
 
1353
	    dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
-
 
1354
		chv_set_memory_dvfs(dev_priv, true);
-
 
1355
 
1623
	I915_WRITE(VLV_DDL(pipe), sprite_dl);
1356
	dev_priv->wm.vlv = wm;
-
 
1357
}
-
 
1358
 
1624
}
1359
#define single_plane_enabled(mask) is_power_of_2(mask)
1625
 
1360
 
1626
static void g4x_update_wm(struct drm_crtc *crtc)
1361
static void g4x_update_wm(struct drm_crtc *crtc)
1627
{
1362
{
1628
	struct drm_device *dev = crtc->dev;
1363
	struct drm_device *dev = crtc->dev;
1629
	static const int sr_latency_ns = 12000;
1364
	static const int sr_latency_ns = 12000;
1630
	struct drm_i915_private *dev_priv = dev->dev_private;
1365
	struct drm_i915_private *dev_priv = dev->dev_private;
1631
	int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1366
	int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1632
	int plane_sr, cursor_sr;
1367
	int plane_sr, cursor_sr;
1633
	unsigned int enabled = 0;
1368
	unsigned int enabled = 0;
1634
	bool cxsr_enabled;
1369
	bool cxsr_enabled;
1635
 
1370
 
1636
	if (g4x_compute_wm0(dev, PIPE_A,
1371
	if (g4x_compute_wm0(dev, PIPE_A,
1637
			    &g4x_wm_info, pessimal_latency_ns,
1372
			    &g4x_wm_info, pessimal_latency_ns,
1638
			    &g4x_cursor_wm_info, pessimal_latency_ns,
1373
			    &g4x_cursor_wm_info, pessimal_latency_ns,
1639
			    &planea_wm, &cursora_wm))
1374
			    &planea_wm, &cursora_wm))
1640
		enabled |= 1 << PIPE_A;
1375
		enabled |= 1 << PIPE_A;
1641
 
1376
 
1642
	if (g4x_compute_wm0(dev, PIPE_B,
1377
	if (g4x_compute_wm0(dev, PIPE_B,
1643
			    &g4x_wm_info, pessimal_latency_ns,
1378
			    &g4x_wm_info, pessimal_latency_ns,
1644
			    &g4x_cursor_wm_info, pessimal_latency_ns,
1379
			    &g4x_cursor_wm_info, pessimal_latency_ns,
1645
			    &planeb_wm, &cursorb_wm))
1380
			    &planeb_wm, &cursorb_wm))
1646
		enabled |= 1 << PIPE_B;
1381
		enabled |= 1 << PIPE_B;
1647
 
1382
 
1648
	if (single_plane_enabled(enabled) &&
1383
	if (single_plane_enabled(enabled) &&
1649
	    g4x_compute_srwm(dev, ffs(enabled) - 1,
1384
	    g4x_compute_srwm(dev, ffs(enabled) - 1,
1650
			     sr_latency_ns,
1385
			     sr_latency_ns,
1651
			     &g4x_wm_info,
1386
			     &g4x_wm_info,
1652
			     &g4x_cursor_wm_info,
1387
			     &g4x_cursor_wm_info,
1653
			     &plane_sr, &cursor_sr)) {
1388
			     &plane_sr, &cursor_sr)) {
1654
		cxsr_enabled = true;
1389
		cxsr_enabled = true;
1655
	} else {
1390
	} else {
1656
		cxsr_enabled = false;
1391
		cxsr_enabled = false;
1657
		intel_set_memory_cxsr(dev_priv, false);
1392
		intel_set_memory_cxsr(dev_priv, false);
1658
		plane_sr = cursor_sr = 0;
1393
		plane_sr = cursor_sr = 0;
1659
	}
1394
	}
1660
 
1395
 
1661
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1396
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1662
		      "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1397
		      "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1663
		      planea_wm, cursora_wm,
1398
		      planea_wm, cursora_wm,
1664
		      planeb_wm, cursorb_wm,
1399
		      planeb_wm, cursorb_wm,
1665
		      plane_sr, cursor_sr);
1400
		      plane_sr, cursor_sr);
1666
 
1401
 
1667
	I915_WRITE(DSPFW1,
1402
	I915_WRITE(DSPFW1,
1668
		   (plane_sr << DSPFW_SR_SHIFT) |
1403
		   FW_WM(plane_sr, SR) |
1669
		   (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1404
		   FW_WM(cursorb_wm, CURSORB) |
1670
		   (planeb_wm << DSPFW_PLANEB_SHIFT) |
1405
		   FW_WM(planeb_wm, PLANEB) |
1671
		   (planea_wm << DSPFW_PLANEA_SHIFT));
1406
		   FW_WM(planea_wm, PLANEA));
1672
	I915_WRITE(DSPFW2,
1407
	I915_WRITE(DSPFW2,
1673
		   (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1408
		   (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1674
		   (cursora_wm << DSPFW_CURSORA_SHIFT));
1409
		   FW_WM(cursora_wm, CURSORA));
1675
	/* HPLL off in SR has some issues on G4x... disable it */
1410
	/* HPLL off in SR has some issues on G4x... disable it */
1676
	I915_WRITE(DSPFW3,
1411
	I915_WRITE(DSPFW3,
1677
		   (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
1412
		   (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
1678
		   (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1413
		   FW_WM(cursor_sr, CURSOR_SR));
1679
 
1414
 
1680
	if (cxsr_enabled)
1415
	if (cxsr_enabled)
1681
		intel_set_memory_cxsr(dev_priv, true);
1416
		intel_set_memory_cxsr(dev_priv, true);
1682
}
1417
}
1683
 
1418
 
1684
static void i965_update_wm(struct drm_crtc *unused_crtc)
1419
static void i965_update_wm(struct drm_crtc *unused_crtc)
1685
{
1420
{
1686
	struct drm_device *dev = unused_crtc->dev;
1421
	struct drm_device *dev = unused_crtc->dev;
1687
	struct drm_i915_private *dev_priv = dev->dev_private;
1422
	struct drm_i915_private *dev_priv = dev->dev_private;
1688
	struct drm_crtc *crtc;
1423
	struct drm_crtc *crtc;
1689
	int srwm = 1;
1424
	int srwm = 1;
1690
	int cursor_sr = 16;
1425
	int cursor_sr = 16;
1691
	bool cxsr_enabled;
1426
	bool cxsr_enabled;
1692
 
1427
 
1693
	/* Calc sr entries for one plane configs */
1428
	/* Calc sr entries for one plane configs */
1694
	crtc = single_enabled_crtc(dev);
1429
	crtc = single_enabled_crtc(dev);
1695
	if (crtc) {
1430
	if (crtc) {
1696
		/* self-refresh has much higher latency */
1431
		/* self-refresh has much higher latency */
1697
		static const int sr_latency_ns = 12000;
1432
		static const int sr_latency_ns = 12000;
1698
		const struct drm_display_mode *adjusted_mode =
1433
		const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1699
			&to_intel_crtc(crtc)->config.adjusted_mode;
-
 
1700
		int clock = adjusted_mode->crtc_clock;
1434
		int clock = adjusted_mode->crtc_clock;
1701
		int htotal = adjusted_mode->crtc_htotal;
1435
		int htotal = adjusted_mode->crtc_htotal;
1702
		int hdisplay = to_intel_crtc(crtc)->config.pipe_src_w;
1436
		int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
1703
		int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
1437
		int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
1704
		unsigned long line_time_us;
1438
		unsigned long line_time_us;
1705
		int entries;
1439
		int entries;
1706
 
1440
 
1707
		line_time_us = max(htotal * 1000 / clock, 1);
1441
		line_time_us = max(htotal * 1000 / clock, 1);
1708
 
1442
 
1709
		/* Use ns/us then divide to preserve precision */
1443
		/* Use ns/us then divide to preserve precision */
1710
		entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1444
		entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1711
			pixel_size * hdisplay;
1445
			pixel_size * hdisplay;
1712
		entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1446
		entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1713
		srwm = I965_FIFO_SIZE - entries;
1447
		srwm = I965_FIFO_SIZE - entries;
1714
		if (srwm < 0)
1448
		if (srwm < 0)
1715
			srwm = 1;
1449
			srwm = 1;
1716
		srwm &= 0x1ff;
1450
		srwm &= 0x1ff;
1717
		DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1451
		DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1718
			      entries, srwm);
1452
			      entries, srwm);
1719
 
1453
 
1720
		entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1454
		entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1721
			pixel_size * to_intel_crtc(crtc)->cursor_width;
1455
			pixel_size * crtc->cursor->state->crtc_w;
1722
		entries = DIV_ROUND_UP(entries,
1456
		entries = DIV_ROUND_UP(entries,
1723
					  i965_cursor_wm_info.cacheline_size);
1457
					  i965_cursor_wm_info.cacheline_size);
1724
		cursor_sr = i965_cursor_wm_info.fifo_size -
1458
		cursor_sr = i965_cursor_wm_info.fifo_size -
1725
			(entries + i965_cursor_wm_info.guard_size);
1459
			(entries + i965_cursor_wm_info.guard_size);
1726
 
1460
 
1727
		if (cursor_sr > i965_cursor_wm_info.max_wm)
1461
		if (cursor_sr > i965_cursor_wm_info.max_wm)
1728
			cursor_sr = i965_cursor_wm_info.max_wm;
1462
			cursor_sr = i965_cursor_wm_info.max_wm;
1729
 
1463
 
1730
		DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1464
		DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1731
			      "cursor %d\n", srwm, cursor_sr);
1465
			      "cursor %d\n", srwm, cursor_sr);
1732
 
1466
 
1733
		cxsr_enabled = true;
1467
		cxsr_enabled = true;
1734
	} else {
1468
	} else {
1735
		cxsr_enabled = false;
1469
		cxsr_enabled = false;
1736
		/* Turn off self refresh if both pipes are enabled */
1470
		/* Turn off self refresh if both pipes are enabled */
1737
		intel_set_memory_cxsr(dev_priv, false);
1471
		intel_set_memory_cxsr(dev_priv, false);
1738
	}
1472
	}
1739
 
1473
 
1740
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1474
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1741
		      srwm);
1475
		      srwm);
1742
 
1476
 
1743
	/* 965 has limitations... */
1477
	/* 965 has limitations... */
1744
	I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
1478
	I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1745
		   (8 << DSPFW_CURSORB_SHIFT) |
1479
		   FW_WM(8, CURSORB) |
1746
		   (8 << DSPFW_PLANEB_SHIFT) |
1480
		   FW_WM(8, PLANEB) |
1747
		   (8 << DSPFW_PLANEA_SHIFT));
1481
		   FW_WM(8, PLANEA));
1748
	I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1482
	I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1749
		   (8 << DSPFW_PLANEC_SHIFT_OLD));
1483
		   FW_WM(8, PLANEC_OLD));
1750
	/* update cursor SR watermark */
1484
	/* update cursor SR watermark */
1751
	I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
1485
	I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
1752
 
1486
 
1753
	if (cxsr_enabled)
1487
	if (cxsr_enabled)
1754
		intel_set_memory_cxsr(dev_priv, true);
1488
		intel_set_memory_cxsr(dev_priv, true);
1755
}
1489
}
-
 
1490
 
-
 
1491
#undef FW_WM
1756
 
1492
 
1757
static void i9xx_update_wm(struct drm_crtc *unused_crtc)
1493
static void i9xx_update_wm(struct drm_crtc *unused_crtc)
1758
{
1494
{
1759
	struct drm_device *dev = unused_crtc->dev;
1495
	struct drm_device *dev = unused_crtc->dev;
1760
	struct drm_i915_private *dev_priv = dev->dev_private;
1496
	struct drm_i915_private *dev_priv = dev->dev_private;
1761
	const struct intel_watermark_params *wm_info;
1497
	const struct intel_watermark_params *wm_info;
1762
	uint32_t fwater_lo;
1498
	uint32_t fwater_lo;
1763
	uint32_t fwater_hi;
1499
	uint32_t fwater_hi;
1764
	int cwm, srwm = 1;
1500
	int cwm, srwm = 1;
1765
	int fifo_size;
1501
	int fifo_size;
1766
	int planea_wm, planeb_wm;
1502
	int planea_wm, planeb_wm;
1767
	struct drm_crtc *crtc, *enabled = NULL;
1503
	struct drm_crtc *crtc, *enabled = NULL;
1768
 
1504
 
1769
	if (IS_I945GM(dev))
1505
	if (IS_I945GM(dev))
1770
		wm_info = &i945_wm_info;
1506
		wm_info = &i945_wm_info;
1771
	else if (!IS_GEN2(dev))
1507
	else if (!IS_GEN2(dev))
1772
		wm_info = &i915_wm_info;
1508
		wm_info = &i915_wm_info;
1773
	else
1509
	else
1774
		wm_info = &i830_a_wm_info;
1510
		wm_info = &i830_a_wm_info;
1775
 
1511
 
1776
	fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1512
	fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1777
	crtc = intel_get_crtc_for_plane(dev, 0);
1513
	crtc = intel_get_crtc_for_plane(dev, 0);
1778
	if (intel_crtc_active(crtc)) {
1514
	if (intel_crtc_active(crtc)) {
1779
		const struct drm_display_mode *adjusted_mode;
1515
		const struct drm_display_mode *adjusted_mode;
1780
		int cpp = crtc->primary->fb->bits_per_pixel / 8;
1516
		int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
1781
		if (IS_GEN2(dev))
1517
		if (IS_GEN2(dev))
1782
			cpp = 4;
1518
			cpp = 4;
1783
 
1519
 
1784
		adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1520
		adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1785
		planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1521
		planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1786
					       wm_info, fifo_size, cpp,
1522
					       wm_info, fifo_size, cpp,
1787
					       pessimal_latency_ns);
1523
					       pessimal_latency_ns);
1788
		enabled = crtc;
1524
		enabled = crtc;
1789
	} else {
1525
	} else {
1790
		planea_wm = fifo_size - wm_info->guard_size;
1526
		planea_wm = fifo_size - wm_info->guard_size;
1791
		if (planea_wm > (long)wm_info->max_wm)
1527
		if (planea_wm > (long)wm_info->max_wm)
1792
			planea_wm = wm_info->max_wm;
1528
			planea_wm = wm_info->max_wm;
1793
	}
1529
	}
1794
 
1530
 
1795
	if (IS_GEN2(dev))
1531
	if (IS_GEN2(dev))
1796
		wm_info = &i830_bc_wm_info;
1532
		wm_info = &i830_bc_wm_info;
1797
 
1533
 
1798
	fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1534
	fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1799
	crtc = intel_get_crtc_for_plane(dev, 1);
1535
	crtc = intel_get_crtc_for_plane(dev, 1);
1800
	if (intel_crtc_active(crtc)) {
1536
	if (intel_crtc_active(crtc)) {
1801
		const struct drm_display_mode *adjusted_mode;
1537
		const struct drm_display_mode *adjusted_mode;
1802
		int cpp = crtc->primary->fb->bits_per_pixel / 8;
1538
		int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
1803
		if (IS_GEN2(dev))
1539
		if (IS_GEN2(dev))
1804
			cpp = 4;
1540
			cpp = 4;
1805
 
1541
 
1806
		adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1542
		adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1807
		planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1543
		planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1808
					       wm_info, fifo_size, cpp,
1544
					       wm_info, fifo_size, cpp,
1809
					       pessimal_latency_ns);
1545
					       pessimal_latency_ns);
1810
		if (enabled == NULL)
1546
		if (enabled == NULL)
1811
			enabled = crtc;
1547
			enabled = crtc;
1812
		else
1548
		else
1813
			enabled = NULL;
1549
			enabled = NULL;
1814
	} else {
1550
	} else {
1815
		planeb_wm = fifo_size - wm_info->guard_size;
1551
		planeb_wm = fifo_size - wm_info->guard_size;
1816
		if (planeb_wm > (long)wm_info->max_wm)
1552
		if (planeb_wm > (long)wm_info->max_wm)
1817
			planeb_wm = wm_info->max_wm;
1553
			planeb_wm = wm_info->max_wm;
1818
	}
1554
	}
1819
 
1555
 
1820
	DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1556
	DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1821
 
1557
 
1822
	if (IS_I915GM(dev) && enabled) {
1558
	if (IS_I915GM(dev) && enabled) {
1823
		struct drm_i915_gem_object *obj;
1559
		struct drm_i915_gem_object *obj;
1824
 
1560
 
1825
		obj = intel_fb_obj(enabled->primary->fb);
1561
		obj = intel_fb_obj(enabled->primary->state->fb);
1826
 
1562
 
1827
		/* self-refresh seems busted with untiled */
1563
		/* self-refresh seems busted with untiled */
1828
		if (obj->tiling_mode == I915_TILING_NONE)
1564
		if (obj->tiling_mode == I915_TILING_NONE)
1829
			enabled = NULL;
1565
			enabled = NULL;
1830
	}
1566
	}
1831
 
1567
 
1832
	/*
1568
	/*
1833
	 * Overlay gets an aggressive default since video jitter is bad.
1569
	 * Overlay gets an aggressive default since video jitter is bad.
1834
	 */
1570
	 */
1835
	cwm = 2;
1571
	cwm = 2;
1836
 
1572
 
1837
	/* Play safe and disable self-refresh before adjusting watermarks. */
1573
	/* Play safe and disable self-refresh before adjusting watermarks. */
1838
	intel_set_memory_cxsr(dev_priv, false);
1574
	intel_set_memory_cxsr(dev_priv, false);
1839
 
1575
 
1840
	/* Calc sr entries for one plane configs */
1576
	/* Calc sr entries for one plane configs */
1841
	if (HAS_FW_BLC(dev) && enabled) {
1577
	if (HAS_FW_BLC(dev) && enabled) {
1842
		/* self-refresh has much higher latency */
1578
		/* self-refresh has much higher latency */
1843
		static const int sr_latency_ns = 6000;
1579
		static const int sr_latency_ns = 6000;
1844
		const struct drm_display_mode *adjusted_mode =
-
 
1845
			&to_intel_crtc(enabled)->config.adjusted_mode;
1580
		const struct drm_display_mode *adjusted_mode = &to_intel_crtc(enabled)->config->base.adjusted_mode;
1846
		int clock = adjusted_mode->crtc_clock;
1581
		int clock = adjusted_mode->crtc_clock;
1847
		int htotal = adjusted_mode->crtc_htotal;
1582
		int htotal = adjusted_mode->crtc_htotal;
1848
		int hdisplay = to_intel_crtc(enabled)->config.pipe_src_w;
1583
		int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
1849
		int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
1584
		int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
1850
		unsigned long line_time_us;
1585
		unsigned long line_time_us;
1851
		int entries;
1586
		int entries;
1852
 
1587
 
1853
		line_time_us = max(htotal * 1000 / clock, 1);
1588
		line_time_us = max(htotal * 1000 / clock, 1);
1854
 
1589
 
1855
		/* Use ns/us then divide to preserve precision */
1590
		/* Use ns/us then divide to preserve precision */
1856
		entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1591
		entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1857
			pixel_size * hdisplay;
1592
			pixel_size * hdisplay;
1858
		entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1593
		entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1859
		DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1594
		DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1860
		srwm = wm_info->fifo_size - entries;
1595
		srwm = wm_info->fifo_size - entries;
1861
		if (srwm < 0)
1596
		if (srwm < 0)
1862
			srwm = 1;
1597
			srwm = 1;
1863
 
1598
 
1864
		if (IS_I945G(dev) || IS_I945GM(dev))
1599
		if (IS_I945G(dev) || IS_I945GM(dev))
1865
			I915_WRITE(FW_BLC_SELF,
1600
			I915_WRITE(FW_BLC_SELF,
1866
				   FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1601
				   FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1867
		else if (IS_I915GM(dev))
1602
		else if (IS_I915GM(dev))
1868
			I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1603
			I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1869
	}
1604
	}
1870
 
1605
 
1871
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1606
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1872
		      planea_wm, planeb_wm, cwm, srwm);
1607
		      planea_wm, planeb_wm, cwm, srwm);
1873
 
1608
 
1874
	fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1609
	fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1875
	fwater_hi = (cwm & 0x1f);
1610
	fwater_hi = (cwm & 0x1f);
1876
 
1611
 
1877
	/* Set request length to 8 cachelines per fetch */
1612
	/* Set request length to 8 cachelines per fetch */
1878
	fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1613
	fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1879
	fwater_hi = fwater_hi | (1 << 8);
1614
	fwater_hi = fwater_hi | (1 << 8);
1880
 
1615
 
1881
	I915_WRITE(FW_BLC, fwater_lo);
1616
	I915_WRITE(FW_BLC, fwater_lo);
1882
	I915_WRITE(FW_BLC2, fwater_hi);
1617
	I915_WRITE(FW_BLC2, fwater_hi);
1883
 
1618
 
1884
	if (enabled)
1619
	if (enabled)
1885
		intel_set_memory_cxsr(dev_priv, true);
1620
		intel_set_memory_cxsr(dev_priv, true);
1886
}
1621
}
1887
 
1622
 
1888
static void i845_update_wm(struct drm_crtc *unused_crtc)
1623
static void i845_update_wm(struct drm_crtc *unused_crtc)
1889
{
1624
{
1890
	struct drm_device *dev = unused_crtc->dev;
1625
	struct drm_device *dev = unused_crtc->dev;
1891
	struct drm_i915_private *dev_priv = dev->dev_private;
1626
	struct drm_i915_private *dev_priv = dev->dev_private;
1892
	struct drm_crtc *crtc;
1627
	struct drm_crtc *crtc;
1893
	const struct drm_display_mode *adjusted_mode;
1628
	const struct drm_display_mode *adjusted_mode;
1894
	uint32_t fwater_lo;
1629
	uint32_t fwater_lo;
1895
	int planea_wm;
1630
	int planea_wm;
1896
 
1631
 
1897
	crtc = single_enabled_crtc(dev);
1632
	crtc = single_enabled_crtc(dev);
1898
	if (crtc == NULL)
1633
	if (crtc == NULL)
1899
		return;
1634
		return;
1900
 
1635
 
1901
	adjusted_mode = &to_intel_crtc(crtc)->config.adjusted_mode;
1636
	adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1902
	planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1637
	planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1903
				       &i845_wm_info,
1638
				       &i845_wm_info,
1904
				       dev_priv->display.get_fifo_size(dev, 0),
1639
				       dev_priv->display.get_fifo_size(dev, 0),
1905
				       4, pessimal_latency_ns);
1640
				       4, pessimal_latency_ns);
1906
	fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1641
	fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1907
	fwater_lo |= (3<<8) | planea_wm;
1642
	fwater_lo |= (3<<8) | planea_wm;
1908
 
1643
 
1909
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1644
	DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1910
 
1645
 
1911
	I915_WRITE(FW_BLC, fwater_lo);
1646
	I915_WRITE(FW_BLC, fwater_lo);
1912
}
1647
}
1913
 
1648
 
1914
static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
-
 
1915
				      struct drm_crtc *crtc)
1649
uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
1916
{
-
 
1917
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1650
{
1918
	uint32_t pixel_rate;
1651
	uint32_t pixel_rate;
1919
 
1652
 
1920
	pixel_rate = intel_crtc->config.adjusted_mode.crtc_clock;
1653
	pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
1921
 
1654
 
1922
	/* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1655
	/* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1923
	 * adjust the pixel_rate here. */
1656
	 * adjust the pixel_rate here. */
1924
 
1657
 
1925
	if (intel_crtc->config.pch_pfit.enabled) {
1658
	if (pipe_config->pch_pfit.enabled) {
1926
		uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
1659
		uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
-
 
1660
		uint32_t pfit_size = pipe_config->pch_pfit.size;
-
 
1661
 
-
 
1662
		pipe_w = pipe_config->pipe_src_w;
1927
		uint32_t pfit_size = intel_crtc->config.pch_pfit.size;
-
 
1928
 
-
 
1929
		pipe_w = intel_crtc->config.pipe_src_w;
1663
		pipe_h = pipe_config->pipe_src_h;
1930
		pipe_h = intel_crtc->config.pipe_src_h;
1664
 
1931
		pfit_w = (pfit_size >> 16) & 0xFFFF;
1665
		pfit_w = (pfit_size >> 16) & 0xFFFF;
1932
		pfit_h = pfit_size & 0xFFFF;
1666
		pfit_h = pfit_size & 0xFFFF;
1933
		if (pipe_w < pfit_w)
1667
		if (pipe_w < pfit_w)
1934
			pipe_w = pfit_w;
1668
			pipe_w = pfit_w;
1935
		if (pipe_h < pfit_h)
1669
		if (pipe_h < pfit_h)
1936
			pipe_h = pfit_h;
1670
			pipe_h = pfit_h;
1937
 
1671
 
1938
		pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1672
		pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1939
				     pfit_w * pfit_h);
1673
				     pfit_w * pfit_h);
1940
	}
1674
	}
1941
 
1675
 
1942
	return pixel_rate;
1676
	return pixel_rate;
1943
}
1677
}
1944
 
1678
 
1945
/* latency must be in 0.1us units. */
1679
/* latency must be in 0.1us units. */
1946
static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
1680
static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
1947
			       uint32_t latency)
1681
			       uint32_t latency)
1948
{
1682
{
1949
	uint64_t ret;
1683
	uint64_t ret;
1950
 
1684
 
1951
	if (WARN(latency == 0, "Latency value missing\n"))
1685
	if (WARN(latency == 0, "Latency value missing\n"))
1952
		return UINT_MAX;
1686
		return UINT_MAX;
1953
 
1687
 
1954
	ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1688
	ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1955
	ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1689
	ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1956
 
1690
 
1957
	return ret;
1691
	return ret;
1958
}
1692
}
1959
 
1693
 
1960
/* latency must be in 0.1us units. */
1694
/* latency must be in 0.1us units. */
1961
static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
1695
static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
1962
			       uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1696
			       uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1963
			       uint32_t latency)
1697
			       uint32_t latency)
1964
{
1698
{
1965
	uint32_t ret;
1699
	uint32_t ret;
1966
 
1700
 
1967
	if (WARN(latency == 0, "Latency value missing\n"))
1701
	if (WARN(latency == 0, "Latency value missing\n"))
1968
		return UINT_MAX;
1702
		return UINT_MAX;
1969
 
1703
 
1970
	ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1704
	ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1971
	ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1705
	ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1972
	ret = DIV_ROUND_UP(ret, 64) + 2;
1706
	ret = DIV_ROUND_UP(ret, 64) + 2;
1973
	return ret;
1707
	return ret;
1974
}
1708
}
1975
 
1709
 
1976
static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
1710
static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
1977
			   uint8_t bytes_per_pixel)
1711
			   uint8_t bytes_per_pixel)
1978
{
1712
{
1979
	return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1713
	return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1980
}
1714
}
1981
 
1715
 
1982
struct skl_pipe_wm_parameters {
1716
struct skl_pipe_wm_parameters {
1983
	bool active;
1717
	bool active;
1984
	uint32_t pipe_htotal;
1718
	uint32_t pipe_htotal;
1985
	uint32_t pixel_rate; /* in KHz */
1719
	uint32_t pixel_rate; /* in KHz */
1986
	struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1720
	struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1987
	struct intel_plane_wm_parameters cursor;
-
 
1988
};
-
 
1989
 
-
 
1990
struct ilk_pipe_wm_parameters {
-
 
1991
	bool active;
-
 
1992
	uint32_t pipe_htotal;
-
 
1993
	uint32_t pixel_rate;
-
 
1994
	struct intel_plane_wm_parameters pri;
-
 
1995
	struct intel_plane_wm_parameters spr;
-
 
1996
	struct intel_plane_wm_parameters cur;
-
 
1997
};
1721
};
1998
 
1722
 
1999
struct ilk_wm_maximums {
1723
struct ilk_wm_maximums {
2000
	uint16_t pri;
1724
	uint16_t pri;
2001
	uint16_t spr;
1725
	uint16_t spr;
2002
	uint16_t cur;
1726
	uint16_t cur;
2003
	uint16_t fbc;
1727
	uint16_t fbc;
2004
};
1728
};
2005
 
1729
 
2006
/* used in computing the new watermarks state */
1730
/* used in computing the new watermarks state */
2007
struct intel_wm_config {
1731
struct intel_wm_config {
2008
	unsigned int num_pipes_active;
1732
	unsigned int num_pipes_active;
2009
	bool sprites_enabled;
1733
	bool sprites_enabled;
2010
	bool sprites_scaled;
1734
	bool sprites_scaled;
2011
};
1735
};
2012
 
1736
 
2013
/*
1737
/*
2014
 * For both WM_PIPE and WM_LP.
1738
 * For both WM_PIPE and WM_LP.
2015
 * mem_value must be in 0.1us units.
1739
 * mem_value must be in 0.1us units.
2016
 */
1740
 */
2017
static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
1741
static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
-
 
1742
				   const struct intel_plane_state *pstate,
2018
				   uint32_t mem_value,
1743
				   uint32_t mem_value,
2019
				   bool is_lp)
1744
				   bool is_lp)
2020
{
1745
{
-
 
1746
	int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
2021
	uint32_t method1, method2;
1747
	uint32_t method1, method2;
2022
 
1748
 
2023
	if (!params->active || !params->pri.enabled)
1749
	if (!cstate->base.active || !pstate->visible)
2024
		return 0;
1750
		return 0;
2025
 
-
 
2026
	method1 = ilk_wm_method1(params->pixel_rate,
-
 
2027
				 params->pri.bytes_per_pixel,
1751
 
2028
				 mem_value);
1752
	method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), bpp, mem_value);
2029
 
1753
 
2030
	if (!is_lp)
1754
	if (!is_lp)
2031
		return method1;
1755
		return method1;
2032
 
1756
 
2033
	method2 = ilk_wm_method2(params->pixel_rate,
1757
	method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
2034
				 params->pipe_htotal,
1758
				 cstate->base.adjusted_mode.crtc_htotal,
2035
				 params->pri.horiz_pixels,
1759
				 drm_rect_width(&pstate->dst),
2036
				 params->pri.bytes_per_pixel,
1760
				 bpp,
2037
				 mem_value);
1761
				 mem_value);
2038
 
1762
 
2039
	return min(method1, method2);
1763
	return min(method1, method2);
2040
}
1764
}
2041
 
1765
 
2042
/*
1766
/*
2043
 * For both WM_PIPE and WM_LP.
1767
 * For both WM_PIPE and WM_LP.
2044
 * mem_value must be in 0.1us units.
1768
 * mem_value must be in 0.1us units.
2045
 */
1769
 */
2046
static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
1770
static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
-
 
1771
				   const struct intel_plane_state *pstate,
2047
				   uint32_t mem_value)
1772
				   uint32_t mem_value)
2048
{
1773
{
-
 
1774
	int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
2049
	uint32_t method1, method2;
1775
	uint32_t method1, method2;
2050
 
1776
 
2051
	if (!params->active || !params->spr.enabled)
1777
	if (!cstate->base.active || !pstate->visible)
2052
		return 0;
1778
		return 0;
2053
 
-
 
2054
	method1 = ilk_wm_method1(params->pixel_rate,
-
 
2055
				 params->spr.bytes_per_pixel,
1779
 
2056
				 mem_value);
1780
	method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), bpp, mem_value);
2057
	method2 = ilk_wm_method2(params->pixel_rate,
1781
	method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
2058
				 params->pipe_htotal,
1782
				 cstate->base.adjusted_mode.crtc_htotal,
2059
				 params->spr.horiz_pixels,
1783
				 drm_rect_width(&pstate->dst),
2060
				 params->spr.bytes_per_pixel,
1784
				 bpp,
2061
				 mem_value);
1785
				 mem_value);
2062
	return min(method1, method2);
1786
	return min(method1, method2);
2063
}
1787
}
2064
 
1788
 
2065
/*
1789
/*
2066
 * For both WM_PIPE and WM_LP.
1790
 * For both WM_PIPE and WM_LP.
2067
 * mem_value must be in 0.1us units.
1791
 * mem_value must be in 0.1us units.
2068
 */
1792
 */
2069
static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
1793
static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
-
 
1794
				   const struct intel_plane_state *pstate,
2070
				   uint32_t mem_value)
1795
				   uint32_t mem_value)
2071
{
1796
{
-
 
1797
	int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
-
 
1798
 
2072
	if (!params->active || !params->cur.enabled)
1799
	if (!cstate->base.active || !pstate->visible)
2073
		return 0;
1800
		return 0;
2074
 
1801
 
2075
	return ilk_wm_method2(params->pixel_rate,
1802
	return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
2076
			      params->pipe_htotal,
1803
			      cstate->base.adjusted_mode.crtc_htotal,
2077
			      params->cur.horiz_pixels,
1804
			      drm_rect_width(&pstate->dst),
2078
			      params->cur.bytes_per_pixel,
1805
			      bpp,
2079
			      mem_value);
1806
			      mem_value);
2080
}
1807
}
2081
 
1808
 
2082
/* Only for WM_LP. */
1809
/* Only for WM_LP. */
2083
static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
1810
static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
-
 
1811
				   const struct intel_plane_state *pstate,
2084
				   uint32_t pri_val)
1812
				   uint32_t pri_val)
2085
{
1813
{
-
 
1814
	int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
-
 
1815
 
2086
	if (!params->active || !params->pri.enabled)
1816
	if (!cstate->base.active || !pstate->visible)
2087
		return 0;
1817
		return 0;
2088
 
1818
 
2089
	return ilk_wm_fbc(pri_val,
-
 
2090
			  params->pri.horiz_pixels,
-
 
2091
			  params->pri.bytes_per_pixel);
1819
	return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->dst), bpp);
2092
}
1820
}
2093
 
1821
 
2094
static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1822
static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
2095
{
1823
{
2096
	if (INTEL_INFO(dev)->gen >= 8)
1824
	if (INTEL_INFO(dev)->gen >= 8)
2097
		return 3072;
1825
		return 3072;
2098
	else if (INTEL_INFO(dev)->gen >= 7)
1826
	else if (INTEL_INFO(dev)->gen >= 7)
2099
		return 768;
1827
		return 768;
2100
	else
1828
	else
2101
		return 512;
1829
		return 512;
2102
}
1830
}
2103
 
1831
 
2104
static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1832
static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
2105
					 int level, bool is_sprite)
1833
					 int level, bool is_sprite)
2106
{
1834
{
2107
	if (INTEL_INFO(dev)->gen >= 8)
1835
	if (INTEL_INFO(dev)->gen >= 8)
2108
		/* BDW primary/sprite plane watermarks */
1836
		/* BDW primary/sprite plane watermarks */
2109
		return level == 0 ? 255 : 2047;
1837
		return level == 0 ? 255 : 2047;
2110
	else if (INTEL_INFO(dev)->gen >= 7)
1838
	else if (INTEL_INFO(dev)->gen >= 7)
2111
		/* IVB/HSW primary/sprite plane watermarks */
1839
		/* IVB/HSW primary/sprite plane watermarks */
2112
		return level == 0 ? 127 : 1023;
1840
		return level == 0 ? 127 : 1023;
2113
	else if (!is_sprite)
1841
	else if (!is_sprite)
2114
		/* ILK/SNB primary plane watermarks */
1842
		/* ILK/SNB primary plane watermarks */
2115
		return level == 0 ? 127 : 511;
1843
		return level == 0 ? 127 : 511;
2116
	else
1844
	else
2117
		/* ILK/SNB sprite plane watermarks */
1845
		/* ILK/SNB sprite plane watermarks */
2118
		return level == 0 ? 63 : 255;
1846
		return level == 0 ? 63 : 255;
2119
}
1847
}
2120
 
1848
 
2121
static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1849
static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
2122
					  int level)
1850
					  int level)
2123
{
1851
{
2124
	if (INTEL_INFO(dev)->gen >= 7)
1852
	if (INTEL_INFO(dev)->gen >= 7)
2125
		return level == 0 ? 63 : 255;
1853
		return level == 0 ? 63 : 255;
2126
	else
1854
	else
2127
		return level == 0 ? 31 : 63;
1855
		return level == 0 ? 31 : 63;
2128
}
1856
}
2129
 
1857
 
2130
static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1858
static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
2131
{
1859
{
2132
	if (INTEL_INFO(dev)->gen >= 8)
1860
	if (INTEL_INFO(dev)->gen >= 8)
2133
		return 31;
1861
		return 31;
2134
	else
1862
	else
2135
		return 15;
1863
		return 15;
2136
}
1864
}
2137
 
1865
 
2138
/* Calculate the maximum primary/sprite plane watermark */
1866
/* Calculate the maximum primary/sprite plane watermark */
2139
static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1867
static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2140
				     int level,
1868
				     int level,
2141
				     const struct intel_wm_config *config,
1869
				     const struct intel_wm_config *config,
2142
				     enum intel_ddb_partitioning ddb_partitioning,
1870
				     enum intel_ddb_partitioning ddb_partitioning,
2143
				     bool is_sprite)
1871
				     bool is_sprite)
2144
{
1872
{
2145
	unsigned int fifo_size = ilk_display_fifo_size(dev);
1873
	unsigned int fifo_size = ilk_display_fifo_size(dev);
2146
 
1874
 
2147
	/* if sprites aren't enabled, sprites get nothing */
1875
	/* if sprites aren't enabled, sprites get nothing */
2148
	if (is_sprite && !config->sprites_enabled)
1876
	if (is_sprite && !config->sprites_enabled)
2149
		return 0;
1877
		return 0;
2150
 
1878
 
2151
	/* HSW allows LP1+ watermarks even with multiple pipes */
1879
	/* HSW allows LP1+ watermarks even with multiple pipes */
2152
	if (level == 0 || config->num_pipes_active > 1) {
1880
	if (level == 0 || config->num_pipes_active > 1) {
2153
		fifo_size /= INTEL_INFO(dev)->num_pipes;
1881
		fifo_size /= INTEL_INFO(dev)->num_pipes;
2154
 
1882
 
2155
		/*
1883
		/*
2156
		 * For some reason the non self refresh
1884
		 * For some reason the non self refresh
2157
		 * FIFO size is only half of the self
1885
		 * FIFO size is only half of the self
2158
		 * refresh FIFO size on ILK/SNB.
1886
		 * refresh FIFO size on ILK/SNB.
2159
		 */
1887
		 */
2160
		if (INTEL_INFO(dev)->gen <= 6)
1888
		if (INTEL_INFO(dev)->gen <= 6)
2161
			fifo_size /= 2;
1889
			fifo_size /= 2;
2162
	}
1890
	}
2163
 
1891
 
2164
	if (config->sprites_enabled) {
1892
	if (config->sprites_enabled) {
2165
		/* level 0 is always calculated with 1:1 split */
1893
		/* level 0 is always calculated with 1:1 split */
2166
		if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1894
		if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2167
			if (is_sprite)
1895
			if (is_sprite)
2168
				fifo_size *= 5;
1896
				fifo_size *= 5;
2169
			fifo_size /= 6;
1897
			fifo_size /= 6;
2170
		} else {
1898
		} else {
2171
			fifo_size /= 2;
1899
			fifo_size /= 2;
2172
		}
1900
		}
2173
	}
1901
	}
2174
 
1902
 
2175
	/* clamp to max that the registers can hold */
1903
	/* clamp to max that the registers can hold */
2176
	return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
1904
	return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
2177
}
1905
}
2178
 
1906
 
2179
/* Calculate the maximum cursor plane watermark */
1907
/* Calculate the maximum cursor plane watermark */
2180
static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
1908
static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
2181
				      int level,
1909
				      int level,
2182
				      const struct intel_wm_config *config)
1910
				      const struct intel_wm_config *config)
2183
{
1911
{
2184
	/* HSW LP1+ watermarks w/ multiple pipes */
1912
	/* HSW LP1+ watermarks w/ multiple pipes */
2185
	if (level > 0 && config->num_pipes_active > 1)
1913
	if (level > 0 && config->num_pipes_active > 1)
2186
		return 64;
1914
		return 64;
2187
 
1915
 
2188
	/* otherwise just report max that registers can hold */
1916
	/* otherwise just report max that registers can hold */
2189
	return ilk_cursor_wm_reg_max(dev, level);
1917
	return ilk_cursor_wm_reg_max(dev, level);
2190
}
1918
}
2191
 
1919
 
2192
static void ilk_compute_wm_maximums(const struct drm_device *dev,
1920
static void ilk_compute_wm_maximums(const struct drm_device *dev,
2193
		       int level,
1921
				    int level,
2194
		       const struct intel_wm_config *config,
1922
				    const struct intel_wm_config *config,
2195
		       enum intel_ddb_partitioning ddb_partitioning,
1923
				    enum intel_ddb_partitioning ddb_partitioning,
2196
				    struct ilk_wm_maximums *max)
1924
				    struct ilk_wm_maximums *max)
2197
{
1925
{
2198
	max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1926
	max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2199
	max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1927
	max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2200
	max->cur = ilk_cursor_wm_max(dev, level, config);
1928
	max->cur = ilk_cursor_wm_max(dev, level, config);
2201
	max->fbc = ilk_fbc_wm_reg_max(dev);
1929
	max->fbc = ilk_fbc_wm_reg_max(dev);
2202
}
1930
}
2203
 
1931
 
2204
static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1932
static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
2205
					int level,
1933
					int level,
2206
					struct ilk_wm_maximums *max)
1934
					struct ilk_wm_maximums *max)
2207
{
1935
{
2208
	max->pri = ilk_plane_wm_reg_max(dev, level, false);
1936
	max->pri = ilk_plane_wm_reg_max(dev, level, false);
2209
	max->spr = ilk_plane_wm_reg_max(dev, level, true);
1937
	max->spr = ilk_plane_wm_reg_max(dev, level, true);
2210
	max->cur = ilk_cursor_wm_reg_max(dev, level);
1938
	max->cur = ilk_cursor_wm_reg_max(dev, level);
2211
	max->fbc = ilk_fbc_wm_reg_max(dev);
1939
	max->fbc = ilk_fbc_wm_reg_max(dev);
2212
}
1940
}
2213
 
1941
 
2214
static bool ilk_validate_wm_level(int level,
1942
static bool ilk_validate_wm_level(int level,
2215
				  const struct ilk_wm_maximums *max,
1943
				  const struct ilk_wm_maximums *max,
2216
			 struct intel_wm_level *result)
1944
				  struct intel_wm_level *result)
2217
{
1945
{
2218
	bool ret;
1946
	bool ret;
2219
 
1947
 
2220
	/* already determined to be invalid? */
1948
	/* already determined to be invalid? */
2221
	if (!result->enable)
1949
	if (!result->enable)
2222
		return false;
1950
		return false;
2223
 
1951
 
2224
	result->enable = result->pri_val <= max->pri &&
1952
	result->enable = result->pri_val <= max->pri &&
2225
			 result->spr_val <= max->spr &&
1953
			 result->spr_val <= max->spr &&
2226
			 result->cur_val <= max->cur;
1954
			 result->cur_val <= max->cur;
2227
 
1955
 
2228
	ret = result->enable;
1956
	ret = result->enable;
2229
 
1957
 
2230
	/*
1958
	/*
2231
	 * HACK until we can pre-compute everything,
1959
	 * HACK until we can pre-compute everything,
2232
	 * and thus fail gracefully if LP0 watermarks
1960
	 * and thus fail gracefully if LP0 watermarks
2233
	 * are exceeded...
1961
	 * are exceeded...
2234
	 */
1962
	 */
2235
	if (level == 0 && !result->enable) {
1963
	if (level == 0 && !result->enable) {
2236
		if (result->pri_val > max->pri)
1964
		if (result->pri_val > max->pri)
2237
			DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1965
			DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2238
				      level, result->pri_val, max->pri);
1966
				      level, result->pri_val, max->pri);
2239
		if (result->spr_val > max->spr)
1967
		if (result->spr_val > max->spr)
2240
			DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1968
			DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2241
				      level, result->spr_val, max->spr);
1969
				      level, result->spr_val, max->spr);
2242
		if (result->cur_val > max->cur)
1970
		if (result->cur_val > max->cur)
2243
			DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1971
			DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2244
				      level, result->cur_val, max->cur);
1972
				      level, result->cur_val, max->cur);
2245
 
1973
 
2246
		result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1974
		result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2247
		result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1975
		result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2248
		result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1976
		result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2249
		result->enable = true;
1977
		result->enable = true;
2250
	}
1978
	}
2251
 
1979
 
2252
	return ret;
1980
	return ret;
2253
}
1981
}
2254
 
1982
 
2255
static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
1983
static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
-
 
1984
				 const struct intel_crtc *intel_crtc,
2256
				 int level,
1985
				 int level,
2257
				 const struct ilk_pipe_wm_parameters *p,
1986
				 struct intel_crtc_state *cstate,
2258
				 struct intel_wm_level *result)
1987
				 struct intel_wm_level *result)
2259
{
1988
{
-
 
1989
	struct intel_plane *intel_plane;
2260
	uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1990
	uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2261
	uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1991
	uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2262
	uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1992
	uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2263
 
1993
 
2264
	/* WM1+ latency values stored in 0.5us units */
1994
	/* WM1+ latency values stored in 0.5us units */
2265
	if (level > 0) {
1995
	if (level > 0) {
2266
		pri_latency *= 5;
1996
		pri_latency *= 5;
2267
		spr_latency *= 5;
1997
		spr_latency *= 5;
2268
		cur_latency *= 5;
1998
		cur_latency *= 5;
2269
	}
1999
	}
-
 
2000
 
-
 
2001
	for_each_intel_plane_on_crtc(dev_priv->dev, intel_crtc, intel_plane) {
-
 
2002
		struct intel_plane_state *pstate =
-
 
2003
			to_intel_plane_state(intel_plane->base.state);
-
 
2004
 
-
 
2005
		switch (intel_plane->base.type) {
2270
 
2006
		case DRM_PLANE_TYPE_PRIMARY:
-
 
2007
			result->pri_val = ilk_compute_pri_wm(cstate, pstate,
-
 
2008
							     pri_latency,
2271
	result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2009
							     level);
-
 
2010
			result->fbc_val = ilk_compute_fbc_wm(cstate, pstate,
-
 
2011
							     result->pri_val);
-
 
2012
			break;
2272
	result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2013
		case DRM_PLANE_TYPE_OVERLAY:
-
 
2014
			result->spr_val = ilk_compute_spr_wm(cstate, pstate,
-
 
2015
							     spr_latency);
-
 
2016
			break;
2273
	result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2017
		case DRM_PLANE_TYPE_CURSOR:
-
 
2018
			result->cur_val = ilk_compute_cur_wm(cstate, pstate,
-
 
2019
							     cur_latency);
-
 
2020
			break;
-
 
2021
		}
-
 
2022
	}
2274
	result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2023
 
2275
	result->enable = true;
2024
	result->enable = true;
2276
}
2025
}
2277
 
2026
 
2278
static uint32_t
2027
static uint32_t
2279
hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
2028
hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
2280
{
2029
{
2281
	struct drm_i915_private *dev_priv = dev->dev_private;
2030
	struct drm_i915_private *dev_priv = dev->dev_private;
2282
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2031
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2283
	struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
2032
	const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
2284
	u32 linetime, ips_linetime;
2033
	u32 linetime, ips_linetime;
2285
 
2034
 
2286
	if (!intel_crtc_active(crtc))
2035
	if (!intel_crtc->active)
2287
		return 0;
2036
		return 0;
2288
 
2037
 
2289
	/* The WM are computed with base on how long it takes to fill a single
2038
	/* The WM are computed with base on how long it takes to fill a single
2290
	 * row at the given clock rate, multiplied by 8.
2039
	 * row at the given clock rate, multiplied by 8.
2291
	 * */
2040
	 * */
2292
	linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2041
	linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2293
				     mode->crtc_clock);
2042
				     adjusted_mode->crtc_clock);
2294
	ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2043
	ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2295
					 intel_ddi_get_cdclk_freq(dev_priv));
2044
					 dev_priv->cdclk_freq);
2296
 
2045
 
2297
	return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2046
	return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2298
	       PIPE_WM_LINETIME_TIME(linetime);
2047
	       PIPE_WM_LINETIME_TIME(linetime);
2299
}
2048
}
2300
 
2049
 
2301
static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
2050
static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
2302
{
2051
{
2303
	struct drm_i915_private *dev_priv = dev->dev_private;
2052
	struct drm_i915_private *dev_priv = dev->dev_private;
2304
 
2053
 
2305
	if (IS_GEN9(dev)) {
2054
	if (IS_GEN9(dev)) {
2306
		uint32_t val;
2055
		uint32_t val;
2307
		int ret, i;
2056
		int ret, i;
2308
		int level, max_level = ilk_wm_max_level(dev);
2057
		int level, max_level = ilk_wm_max_level(dev);
2309
 
2058
 
2310
		/* read the first set of memory latencies[0:3] */
2059
		/* read the first set of memory latencies[0:3] */
2311
		val = 0; /* data0 to be programmed to 0 for first set */
2060
		val = 0; /* data0 to be programmed to 0 for first set */
2312
		mutex_lock(&dev_priv->rps.hw_lock);
2061
		mutex_lock(&dev_priv->rps.hw_lock);
2313
		ret = sandybridge_pcode_read(dev_priv,
2062
		ret = sandybridge_pcode_read(dev_priv,
2314
					     GEN9_PCODE_READ_MEM_LATENCY,
2063
					     GEN9_PCODE_READ_MEM_LATENCY,
2315
					     &val);
2064
					     &val);
2316
		mutex_unlock(&dev_priv->rps.hw_lock);
2065
		mutex_unlock(&dev_priv->rps.hw_lock);
2317
 
2066
 
2318
		if (ret) {
2067
		if (ret) {
2319
			DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2068
			DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2320
			return;
2069
			return;
2321
		}
2070
		}
2322
 
2071
 
2323
		wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2072
		wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2324
		wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2073
		wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2325
				GEN9_MEM_LATENCY_LEVEL_MASK;
2074
				GEN9_MEM_LATENCY_LEVEL_MASK;
2326
		wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2075
		wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2327
				GEN9_MEM_LATENCY_LEVEL_MASK;
2076
				GEN9_MEM_LATENCY_LEVEL_MASK;
2328
		wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2077
		wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2329
				GEN9_MEM_LATENCY_LEVEL_MASK;
2078
				GEN9_MEM_LATENCY_LEVEL_MASK;
2330
 
2079
 
2331
		/* read the second set of memory latencies[4:7] */
2080
		/* read the second set of memory latencies[4:7] */
2332
		val = 1; /* data0 to be programmed to 1 for second set */
2081
		val = 1; /* data0 to be programmed to 1 for second set */
2333
		mutex_lock(&dev_priv->rps.hw_lock);
2082
		mutex_lock(&dev_priv->rps.hw_lock);
2334
		ret = sandybridge_pcode_read(dev_priv,
2083
		ret = sandybridge_pcode_read(dev_priv,
2335
					     GEN9_PCODE_READ_MEM_LATENCY,
2084
					     GEN9_PCODE_READ_MEM_LATENCY,
2336
					     &val);
2085
					     &val);
2337
		mutex_unlock(&dev_priv->rps.hw_lock);
2086
		mutex_unlock(&dev_priv->rps.hw_lock);
2338
		if (ret) {
2087
		if (ret) {
2339
			DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2088
			DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2340
			return;
2089
			return;
2341
		}
2090
		}
2342
 
2091
 
2343
		wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2092
		wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2344
		wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2093
		wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2345
				GEN9_MEM_LATENCY_LEVEL_MASK;
2094
				GEN9_MEM_LATENCY_LEVEL_MASK;
2346
		wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2095
		wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2347
				GEN9_MEM_LATENCY_LEVEL_MASK;
2096
				GEN9_MEM_LATENCY_LEVEL_MASK;
2348
		wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2097
		wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2349
				GEN9_MEM_LATENCY_LEVEL_MASK;
2098
				GEN9_MEM_LATENCY_LEVEL_MASK;
2350
 
2099
 
2351
		/*
2100
		/*
-
 
2101
		 * WaWmMemoryReadLatency:skl
-
 
2102
		 *
2352
		 * punit doesn't take into account the read latency so we need
2103
		 * punit doesn't take into account the read latency so we need
2353
		 * to add 2us to the various latency levels we retrieve from
2104
		 * to add 2us to the various latency levels we retrieve from
2354
		 * the punit.
2105
		 * the punit.
2355
		 *   - W0 is a bit special in that it's the only level that
2106
		 *   - W0 is a bit special in that it's the only level that
2356
		 *   can't be disabled if we want to have display working, so
2107
		 *   can't be disabled if we want to have display working, so
2357
		 *   we always add 2us there.
2108
		 *   we always add 2us there.
2358
		 *   - For levels >=1, punit returns 0us latency when they are
2109
		 *   - For levels >=1, punit returns 0us latency when they are
2359
		 *   disabled, so we respect that and don't add 2us then
2110
		 *   disabled, so we respect that and don't add 2us then
2360
		 *
2111
		 *
2361
		 * Additionally, if a level n (n > 1) has a 0us latency, all
2112
		 * Additionally, if a level n (n > 1) has a 0us latency, all
2362
		 * levels m (m >= n) need to be disabled. We make sure to
2113
		 * levels m (m >= n) need to be disabled. We make sure to
2363
		 * sanitize the values out of the punit to satisfy this
2114
		 * sanitize the values out of the punit to satisfy this
2364
		 * requirement.
2115
		 * requirement.
2365
		 */
2116
		 */
2366
		wm[0] += 2;
2117
		wm[0] += 2;
2367
		for (level = 1; level <= max_level; level++)
2118
		for (level = 1; level <= max_level; level++)
2368
			if (wm[level] != 0)
2119
			if (wm[level] != 0)
2369
				wm[level] += 2;
2120
				wm[level] += 2;
2370
			else {
2121
			else {
2371
				for (i = level + 1; i <= max_level; i++)
2122
				for (i = level + 1; i <= max_level; i++)
2372
					wm[i] = 0;
2123
					wm[i] = 0;
2373
 
2124
 
2374
				break;
2125
				break;
2375
			}
2126
			}
2376
	} else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2127
	} else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2377
		uint64_t sskpd = I915_READ64(MCH_SSKPD);
2128
		uint64_t sskpd = I915_READ64(MCH_SSKPD);
2378
 
2129
 
2379
		wm[0] = (sskpd >> 56) & 0xFF;
2130
		wm[0] = (sskpd >> 56) & 0xFF;
2380
		if (wm[0] == 0)
2131
		if (wm[0] == 0)
2381
			wm[0] = sskpd & 0xF;
2132
			wm[0] = sskpd & 0xF;
2382
		wm[1] = (sskpd >> 4) & 0xFF;
2133
		wm[1] = (sskpd >> 4) & 0xFF;
2383
		wm[2] = (sskpd >> 12) & 0xFF;
2134
		wm[2] = (sskpd >> 12) & 0xFF;
2384
		wm[3] = (sskpd >> 20) & 0x1FF;
2135
		wm[3] = (sskpd >> 20) & 0x1FF;
2385
		wm[4] = (sskpd >> 32) & 0x1FF;
2136
		wm[4] = (sskpd >> 32) & 0x1FF;
2386
	} else if (INTEL_INFO(dev)->gen >= 6) {
2137
	} else if (INTEL_INFO(dev)->gen >= 6) {
2387
		uint32_t sskpd = I915_READ(MCH_SSKPD);
2138
		uint32_t sskpd = I915_READ(MCH_SSKPD);
2388
 
2139
 
2389
		wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2140
		wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2390
		wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2141
		wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2391
		wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2142
		wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2392
		wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
2143
		wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
2393
	} else if (INTEL_INFO(dev)->gen >= 5) {
2144
	} else if (INTEL_INFO(dev)->gen >= 5) {
2394
		uint32_t mltr = I915_READ(MLTR_ILK);
2145
		uint32_t mltr = I915_READ(MLTR_ILK);
2395
 
2146
 
2396
		/* ILK primary LP0 latency is 700 ns */
2147
		/* ILK primary LP0 latency is 700 ns */
2397
		wm[0] = 7;
2148
		wm[0] = 7;
2398
		wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2149
		wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2399
		wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
2150
		wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
2400
	}
2151
	}
2401
}
2152
}
2402
 
2153
 
2403
static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2154
static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2404
{
2155
{
2405
	/* ILK sprite LP0 latency is 1300 ns */
2156
	/* ILK sprite LP0 latency is 1300 ns */
2406
	if (INTEL_INFO(dev)->gen == 5)
2157
	if (INTEL_INFO(dev)->gen == 5)
2407
		wm[0] = 13;
2158
		wm[0] = 13;
2408
}
2159
}
2409
 
2160
 
2410
static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2161
static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2411
{
2162
{
2412
	/* ILK cursor LP0 latency is 1300 ns */
2163
	/* ILK cursor LP0 latency is 1300 ns */
2413
	if (INTEL_INFO(dev)->gen == 5)
2164
	if (INTEL_INFO(dev)->gen == 5)
2414
		wm[0] = 13;
2165
		wm[0] = 13;
2415
 
2166
 
2416
	/* WaDoubleCursorLP3Latency:ivb */
2167
	/* WaDoubleCursorLP3Latency:ivb */
2417
	if (IS_IVYBRIDGE(dev))
2168
	if (IS_IVYBRIDGE(dev))
2418
		wm[3] *= 2;
2169
		wm[3] *= 2;
2419
}
2170
}
2420
 
2171
 
2421
int ilk_wm_max_level(const struct drm_device *dev)
2172
int ilk_wm_max_level(const struct drm_device *dev)
2422
{
2173
{
2423
	/* how many WM levels are we expecting */
2174
	/* how many WM levels are we expecting */
2424
	if (IS_GEN9(dev))
2175
	if (INTEL_INFO(dev)->gen >= 9)
2425
		return 7;
2176
		return 7;
2426
	else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2177
	else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2427
		return 4;
2178
		return 4;
2428
	else if (INTEL_INFO(dev)->gen >= 6)
2179
	else if (INTEL_INFO(dev)->gen >= 6)
2429
		return 3;
2180
		return 3;
2430
	else
2181
	else
2431
		return 2;
2182
		return 2;
2432
}
2183
}
2433
 
2184
 
2434
static void intel_print_wm_latency(struct drm_device *dev,
2185
static void intel_print_wm_latency(struct drm_device *dev,
2435
				   const char *name,
2186
				   const char *name,
2436
				   const uint16_t wm[8])
2187
				   const uint16_t wm[8])
2437
{
2188
{
2438
	int level, max_level = ilk_wm_max_level(dev);
2189
	int level, max_level = ilk_wm_max_level(dev);
2439
 
2190
 
2440
	for (level = 0; level <= max_level; level++) {
2191
	for (level = 0; level <= max_level; level++) {
2441
		unsigned int latency = wm[level];
2192
		unsigned int latency = wm[level];
2442
 
2193
 
2443
		if (latency == 0) {
2194
		if (latency == 0) {
2444
			DRM_ERROR("%s WM%d latency not provided\n",
2195
			DRM_ERROR("%s WM%d latency not provided\n",
2445
				  name, level);
2196
				  name, level);
2446
			continue;
2197
			continue;
2447
		}
2198
		}
2448
 
2199
 
2449
		/*
2200
		/*
2450
		 * - latencies are in us on gen9.
2201
		 * - latencies are in us on gen9.
2451
		 * - before then, WM1+ latency values are in 0.5us units
2202
		 * - before then, WM1+ latency values are in 0.5us units
2452
		 */
2203
		 */
2453
		if (IS_GEN9(dev))
2204
		if (IS_GEN9(dev))
2454
			latency *= 10;
2205
			latency *= 10;
2455
		else if (level > 0)
2206
		else if (level > 0)
2456
			latency *= 5;
2207
			latency *= 5;
2457
 
2208
 
2458
		DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2209
		DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2459
			      name, level, wm[level],
2210
			      name, level, wm[level],
2460
			      latency / 10, latency % 10);
2211
			      latency / 10, latency % 10);
2461
	}
2212
	}
2462
}
2213
}
2463
 
2214
 
2464
static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2215
static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2465
				    uint16_t wm[5], uint16_t min)
2216
				    uint16_t wm[5], uint16_t min)
2466
{
2217
{
2467
	int level, max_level = ilk_wm_max_level(dev_priv->dev);
2218
	int level, max_level = ilk_wm_max_level(dev_priv->dev);
2468
 
2219
 
2469
	if (wm[0] >= min)
2220
	if (wm[0] >= min)
2470
		return false;
2221
		return false;
2471
 
2222
 
2472
	wm[0] = max(wm[0], min);
2223
	wm[0] = max(wm[0], min);
2473
	for (level = 1; level <= max_level; level++)
2224
	for (level = 1; level <= max_level; level++)
2474
		wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2225
		wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2475
 
2226
 
2476
	return true;
2227
	return true;
2477
}
2228
}
2478
 
2229
 
2479
static void snb_wm_latency_quirk(struct drm_device *dev)
2230
static void snb_wm_latency_quirk(struct drm_device *dev)
2480
{
2231
{
2481
	struct drm_i915_private *dev_priv = dev->dev_private;
2232
	struct drm_i915_private *dev_priv = dev->dev_private;
2482
	bool changed;
2233
	bool changed;
2483
 
2234
 
2484
	/*
2235
	/*
2485
	 * The BIOS provided WM memory latency values are often
2236
	 * The BIOS provided WM memory latency values are often
2486
	 * inadequate for high resolution displays. Adjust them.
2237
	 * inadequate for high resolution displays. Adjust them.
2487
	 */
2238
	 */
2488
	changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2239
	changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2489
		ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2240
		ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2490
		ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2241
		ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2491
 
2242
 
2492
	if (!changed)
2243
	if (!changed)
2493
		return;
2244
		return;
2494
 
2245
 
2495
	DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2246
	DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2496
	intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2247
	intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2497
	intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2248
	intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2498
	intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2249
	intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2499
}
2250
}
2500
 
2251
 
2501
static void ilk_setup_wm_latency(struct drm_device *dev)
2252
static void ilk_setup_wm_latency(struct drm_device *dev)
2502
{
2253
{
2503
	struct drm_i915_private *dev_priv = dev->dev_private;
2254
	struct drm_i915_private *dev_priv = dev->dev_private;
2504
 
2255
 
2505
	intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2256
	intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2506
 
2257
 
2507
	memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2258
	memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2508
	       sizeof(dev_priv->wm.pri_latency));
2259
	       sizeof(dev_priv->wm.pri_latency));
2509
	memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2260
	memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2510
	       sizeof(dev_priv->wm.pri_latency));
2261
	       sizeof(dev_priv->wm.pri_latency));
2511
 
2262
 
2512
	intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2263
	intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2513
	intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
2264
	intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
2514
 
2265
 
2515
	intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2266
	intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2516
	intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2267
	intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2517
	intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2268
	intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2518
 
2269
 
2519
	if (IS_GEN6(dev))
2270
	if (IS_GEN6(dev))
2520
		snb_wm_latency_quirk(dev);
2271
		snb_wm_latency_quirk(dev);
2521
}
2272
}
2522
 
2273
 
2523
static void skl_setup_wm_latency(struct drm_device *dev)
2274
static void skl_setup_wm_latency(struct drm_device *dev)
2524
{
2275
{
2525
	struct drm_i915_private *dev_priv = dev->dev_private;
2276
	struct drm_i915_private *dev_priv = dev->dev_private;
2526
 
2277
 
2527
	intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2278
	intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2528
	intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2279
	intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2529
}
2280
}
2530
 
-
 
2531
static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
-
 
2532
				      struct ilk_pipe_wm_parameters *p)
-
 
2533
{
-
 
2534
	struct drm_device *dev = crtc->dev;
-
 
2535
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
 
2536
	enum pipe pipe = intel_crtc->pipe;
-
 
2537
	struct drm_plane *plane;
-
 
2538
 
-
 
2539
	if (!intel_crtc_active(crtc))
-
 
2540
		return;
-
 
2541
 
-
 
2542
	p->active = true;
-
 
2543
		p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
-
 
2544
		p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
-
 
2545
		p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
-
 
2546
		p->cur.bytes_per_pixel = 4;
-
 
2547
		p->pri.horiz_pixels = intel_crtc->config.pipe_src_w;
-
 
2548
		p->cur.horiz_pixels = intel_crtc->cursor_width;
-
 
2549
		/* TODO: for now, assume primary and cursor planes are always enabled. */
-
 
2550
		p->pri.enabled = true;
-
 
2551
		p->cur.enabled = true;
-
 
2552
 
-
 
2553
	drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
-
 
2554
		struct intel_plane *intel_plane = to_intel_plane(plane);
-
 
2555
 
-
 
2556
		if (intel_plane->pipe == pipe) {
-
 
2557
		p->spr = intel_plane->wm;
-
 
2558
			break;
-
 
2559
		}
-
 
2560
	}
-
 
2561
}
-
 
2562
 
2281
 
2563
static void ilk_compute_wm_config(struct drm_device *dev,
2282
static void ilk_compute_wm_config(struct drm_device *dev,
2564
				  struct intel_wm_config *config)
2283
				  struct intel_wm_config *config)
2565
{
2284
{
2566
	struct intel_crtc *intel_crtc;
2285
	struct intel_crtc *intel_crtc;
2567
 
2286
 
2568
	/* Compute the currently _active_ config */
2287
	/* Compute the currently _active_ config */
2569
	for_each_intel_crtc(dev, intel_crtc) {
2288
	for_each_intel_crtc(dev, intel_crtc) {
2570
		const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2289
		const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2571
 
2290
 
2572
		if (!wm->pipe_enabled)
2291
		if (!wm->pipe_enabled)
2573
			continue;
2292
			continue;
2574
 
2293
 
2575
		config->sprites_enabled |= wm->sprites_enabled;
2294
		config->sprites_enabled |= wm->sprites_enabled;
2576
		config->sprites_scaled |= wm->sprites_scaled;
2295
		config->sprites_scaled |= wm->sprites_scaled;
2577
		config->num_pipes_active++;
2296
		config->num_pipes_active++;
2578
	}
2297
	}
2579
}
2298
}
2580
 
2299
 
2581
/* Compute new watermarks for the pipe */
2300
/* Compute new watermarks for the pipe */
2582
static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
2301
static bool intel_compute_pipe_wm(struct intel_crtc_state *cstate,
2583
				  const struct ilk_pipe_wm_parameters *params,
-
 
2584
				  struct intel_pipe_wm *pipe_wm)
2302
				  struct intel_pipe_wm *pipe_wm)
2585
{
2303
{
-
 
2304
	struct drm_crtc *crtc = cstate->base.crtc;
2586
	struct drm_device *dev = crtc->dev;
2305
	struct drm_device *dev = crtc->dev;
2587
	const struct drm_i915_private *dev_priv = dev->dev_private;
2306
	const struct drm_i915_private *dev_priv = dev->dev_private;
-
 
2307
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
 
2308
	struct intel_plane *intel_plane;
-
 
2309
	struct intel_plane_state *sprstate = NULL;
2588
	int level, max_level = ilk_wm_max_level(dev);
2310
	int level, max_level = ilk_wm_max_level(dev);
2589
	/* LP0 watermark maximums depend on this pipe alone */
2311
	/* LP0 watermark maximums depend on this pipe alone */
2590
	struct intel_wm_config config = {
2312
	struct intel_wm_config config = {
2591
		.num_pipes_active = 1,
2313
		.num_pipes_active = 1,
2592
		.sprites_enabled = params->spr.enabled,
-
 
2593
		.sprites_scaled = params->spr.scaled,
-
 
2594
	};
2314
	};
2595
	struct ilk_wm_maximums max;
2315
	struct ilk_wm_maximums max;
-
 
2316
 
-
 
2317
	for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
-
 
2318
		if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY) {
-
 
2319
			sprstate = to_intel_plane_state(intel_plane->base.state);
-
 
2320
			break;
-
 
2321
		}
-
 
2322
	}
-
 
2323
 
-
 
2324
	config.sprites_enabled = sprstate->visible;
-
 
2325
	config.sprites_scaled = sprstate->visible &&
-
 
2326
		(drm_rect_width(&sprstate->dst) != drm_rect_width(&sprstate->src) >> 16 ||
-
 
2327
		drm_rect_height(&sprstate->dst) != drm_rect_height(&sprstate->src) >> 16);
2596
 
2328
 
2597
	pipe_wm->pipe_enabled = params->active;
2329
	pipe_wm->pipe_enabled = cstate->base.active;
2598
	pipe_wm->sprites_enabled = params->spr.enabled;
2330
	pipe_wm->sprites_enabled = sprstate->visible;
2599
	pipe_wm->sprites_scaled = params->spr.scaled;
2331
	pipe_wm->sprites_scaled = config.sprites_scaled;
2600
 
2332
 
2601
	/* ILK/SNB: LP2+ watermarks only w/o sprites */
2333
	/* ILK/SNB: LP2+ watermarks only w/o sprites */
2602
	if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2334
	if (INTEL_INFO(dev)->gen <= 6 && sprstate->visible)
2603
		max_level = 1;
2335
		max_level = 1;
2604
 
2336
 
2605
	/* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2337
	/* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2606
	if (params->spr.scaled)
2338
	if (config.sprites_scaled)
2607
		max_level = 0;
2339
		max_level = 0;
2608
 
2340
 
2609
	ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
2341
	ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate, &pipe_wm->wm[0]);
2610
 
2342
 
2611
	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2343
	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2612
		pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
2344
		pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
2613
 
2345
 
2614
	/* LP0 watermarks always use 1/2 DDB partitioning */
2346
	/* LP0 watermarks always use 1/2 DDB partitioning */
2615
	ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2347
	ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2616
 
2348
 
2617
	/* At least LP0 must be valid */
2349
	/* At least LP0 must be valid */
2618
	if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2350
	if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2619
		return false;
2351
		return false;
2620
 
2352
 
2621
	ilk_compute_wm_reg_maximums(dev, 1, &max);
2353
	ilk_compute_wm_reg_maximums(dev, 1, &max);
2622
 
2354
 
2623
	for (level = 1; level <= max_level; level++) {
2355
	for (level = 1; level <= max_level; level++) {
2624
		struct intel_wm_level wm = {};
2356
		struct intel_wm_level wm = {};
2625
 
2357
 
2626
		ilk_compute_wm_level(dev_priv, level, params, &wm);
2358
		ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate, &wm);
2627
 
2359
 
2628
		/*
2360
		/*
2629
		 * Disable any watermark level that exceeds the
2361
		 * Disable any watermark level that exceeds the
2630
		 * register maximums since such watermarks are
2362
		 * register maximums since such watermarks are
2631
		 * always invalid.
2363
		 * always invalid.
2632
		 */
2364
		 */
2633
		if (!ilk_validate_wm_level(level, &max, &wm))
2365
		if (!ilk_validate_wm_level(level, &max, &wm))
2634
			break;
2366
			break;
2635
 
2367
 
2636
		pipe_wm->wm[level] = wm;
2368
		pipe_wm->wm[level] = wm;
2637
	}
2369
	}
2638
 
2370
 
2639
	return true;
2371
	return true;
2640
}
2372
}
2641
 
2373
 
2642
/*
2374
/*
2643
 * Merge the watermarks from all active pipes for a specific level.
2375
 * Merge the watermarks from all active pipes for a specific level.
2644
 */
2376
 */
2645
static void ilk_merge_wm_level(struct drm_device *dev,
2377
static void ilk_merge_wm_level(struct drm_device *dev,
2646
			       int level,
2378
			       int level,
2647
			       struct intel_wm_level *ret_wm)
2379
			       struct intel_wm_level *ret_wm)
2648
{
2380
{
2649
	const struct intel_crtc *intel_crtc;
2381
	const struct intel_crtc *intel_crtc;
2650
 
2382
 
2651
	ret_wm->enable = true;
2383
	ret_wm->enable = true;
2652
 
2384
 
2653
	for_each_intel_crtc(dev, intel_crtc) {
2385
	for_each_intel_crtc(dev, intel_crtc) {
2654
		const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2386
		const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2655
		const struct intel_wm_level *wm = &active->wm[level];
2387
		const struct intel_wm_level *wm = &active->wm[level];
2656
 
2388
 
2657
		if (!active->pipe_enabled)
2389
		if (!active->pipe_enabled)
2658
			continue;
2390
			continue;
2659
 
2391
 
2660
		/*
2392
		/*
2661
		 * The watermark values may have been used in the past,
2393
		 * The watermark values may have been used in the past,
2662
		 * so we must maintain them in the registers for some
2394
		 * so we must maintain them in the registers for some
2663
		 * time even if the level is now disabled.
2395
		 * time even if the level is now disabled.
2664
		 */
2396
		 */
2665
		if (!wm->enable)
2397
		if (!wm->enable)
2666
			ret_wm->enable = false;
2398
			ret_wm->enable = false;
2667
 
2399
 
2668
		ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2400
		ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2669
		ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2401
		ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2670
		ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2402
		ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2671
		ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2403
		ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2672
	}
2404
	}
2673
}
2405
}
2674
 
2406
 
2675
/*
2407
/*
2676
 * Merge all low power watermarks for all active pipes.
2408
 * Merge all low power watermarks for all active pipes.
2677
 */
2409
 */
2678
static void ilk_wm_merge(struct drm_device *dev,
2410
static void ilk_wm_merge(struct drm_device *dev,
2679
			 const struct intel_wm_config *config,
2411
			 const struct intel_wm_config *config,
2680
			 const struct ilk_wm_maximums *max,
2412
			 const struct ilk_wm_maximums *max,
2681
			 struct intel_pipe_wm *merged)
2413
			 struct intel_pipe_wm *merged)
2682
{
2414
{
-
 
2415
	struct drm_i915_private *dev_priv = dev->dev_private;
2683
	int level, max_level = ilk_wm_max_level(dev);
2416
	int level, max_level = ilk_wm_max_level(dev);
2684
	int last_enabled_level = max_level;
2417
	int last_enabled_level = max_level;
2685
 
2418
 
2686
	/* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2419
	/* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2687
	if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2420
	if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2688
	    config->num_pipes_active > 1)
2421
	    config->num_pipes_active > 1)
2689
		return;
2422
		return;
2690
 
2423
 
2691
	/* ILK: FBC WM must be disabled always */
2424
	/* ILK: FBC WM must be disabled always */
2692
	merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
2425
	merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
2693
 
2426
 
2694
	/* merge each WM1+ level */
2427
	/* merge each WM1+ level */
2695
	for (level = 1; level <= max_level; level++) {
2428
	for (level = 1; level <= max_level; level++) {
2696
		struct intel_wm_level *wm = &merged->wm[level];
2429
		struct intel_wm_level *wm = &merged->wm[level];
2697
 
2430
 
2698
		ilk_merge_wm_level(dev, level, wm);
2431
		ilk_merge_wm_level(dev, level, wm);
2699
 
2432
 
2700
		if (level > last_enabled_level)
2433
		if (level > last_enabled_level)
2701
			wm->enable = false;
2434
			wm->enable = false;
2702
		else if (!ilk_validate_wm_level(level, max, wm))
2435
		else if (!ilk_validate_wm_level(level, max, wm))
2703
			/* make sure all following levels get disabled */
2436
			/* make sure all following levels get disabled */
2704
			last_enabled_level = level - 1;
2437
			last_enabled_level = level - 1;
2705
 
2438
 
2706
		/*
2439
		/*
2707
		 * The spec says it is preferred to disable
2440
		 * The spec says it is preferred to disable
2708
		 * FBC WMs instead of disabling a WM level.
2441
		 * FBC WMs instead of disabling a WM level.
2709
		 */
2442
		 */
2710
		if (wm->fbc_val > max->fbc) {
2443
		if (wm->fbc_val > max->fbc) {
2711
			if (wm->enable)
2444
			if (wm->enable)
2712
			merged->fbc_wm_enabled = false;
2445
				merged->fbc_wm_enabled = false;
2713
			wm->fbc_val = 0;
2446
			wm->fbc_val = 0;
2714
		}
2447
		}
2715
	}
2448
	}
2716
 
2449
 
2717
	/* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2450
	/* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2718
	/*
2451
	/*
2719
	 * FIXME this is racy. FBC might get enabled later.
2452
	 * FIXME this is racy. FBC might get enabled later.
2720
	 * What we should check here is whether FBC can be
2453
	 * What we should check here is whether FBC can be
2721
	 * enabled sometime later.
2454
	 * enabled sometime later.
2722
	 */
2455
	 */
2723
	if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2456
	if (IS_GEN5(dev) && !merged->fbc_wm_enabled &&
-
 
2457
	    intel_fbc_enabled(dev_priv)) {
2724
		for (level = 2; level <= max_level; level++) {
2458
		for (level = 2; level <= max_level; level++) {
2725
			struct intel_wm_level *wm = &merged->wm[level];
2459
			struct intel_wm_level *wm = &merged->wm[level];
2726
 
2460
 
2727
			wm->enable = false;
2461
			wm->enable = false;
2728
		}
2462
		}
2729
	}
2463
	}
2730
}
2464
}
2731
 
2465
 
2732
static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2466
static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2733
{
2467
{
2734
	/* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2468
	/* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2735
	return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2469
	return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2736
}
2470
}
2737
 
2471
 
2738
/* The value we need to program into the WM_LPx latency field */
2472
/* The value we need to program into the WM_LPx latency field */
2739
static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2473
static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2740
{
2474
{
2741
	struct drm_i915_private *dev_priv = dev->dev_private;
2475
	struct drm_i915_private *dev_priv = dev->dev_private;
2742
 
2476
 
2743
	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2477
	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2744
		return 2 * level;
2478
		return 2 * level;
2745
	else
2479
	else
2746
		return dev_priv->wm.pri_latency[level];
2480
		return dev_priv->wm.pri_latency[level];
2747
}
2481
}
2748
 
2482
 
2749
static void ilk_compute_wm_results(struct drm_device *dev,
2483
static void ilk_compute_wm_results(struct drm_device *dev,
2750
				   const struct intel_pipe_wm *merged,
2484
				   const struct intel_pipe_wm *merged,
2751
				   enum intel_ddb_partitioning partitioning,
2485
				   enum intel_ddb_partitioning partitioning,
2752
				   struct ilk_wm_values *results)
2486
				   struct ilk_wm_values *results)
2753
{
2487
{
2754
	struct intel_crtc *intel_crtc;
2488
	struct intel_crtc *intel_crtc;
2755
	int level, wm_lp;
2489
	int level, wm_lp;
2756
 
2490
 
2757
	results->enable_fbc_wm = merged->fbc_wm_enabled;
2491
	results->enable_fbc_wm = merged->fbc_wm_enabled;
2758
	results->partitioning = partitioning;
2492
	results->partitioning = partitioning;
2759
 
2493
 
2760
	/* LP1+ register values */
2494
	/* LP1+ register values */
2761
	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2495
	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2762
		const struct intel_wm_level *r;
2496
		const struct intel_wm_level *r;
2763
 
2497
 
2764
		level = ilk_wm_lp_to_level(wm_lp, merged);
2498
		level = ilk_wm_lp_to_level(wm_lp, merged);
2765
 
2499
 
2766
		r = &merged->wm[level];
2500
		r = &merged->wm[level];
2767
 
2501
 
2768
		/*
2502
		/*
2769
		 * Maintain the watermark values even if the level is
2503
		 * Maintain the watermark values even if the level is
2770
		 * disabled. Doing otherwise could cause underruns.
2504
		 * disabled. Doing otherwise could cause underruns.
2771
		 */
2505
		 */
2772
		results->wm_lp[wm_lp - 1] =
2506
		results->wm_lp[wm_lp - 1] =
2773
			(ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
2507
			(ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
2774
			(r->pri_val << WM1_LP_SR_SHIFT) |
2508
			(r->pri_val << WM1_LP_SR_SHIFT) |
2775
			r->cur_val;
2509
			r->cur_val;
2776
 
2510
 
2777
		if (r->enable)
2511
		if (r->enable)
2778
			results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2512
			results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2779
 
2513
 
2780
		if (INTEL_INFO(dev)->gen >= 8)
2514
		if (INTEL_INFO(dev)->gen >= 8)
2781
			results->wm_lp[wm_lp - 1] |=
2515
			results->wm_lp[wm_lp - 1] |=
2782
				r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2516
				r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2783
		else
2517
		else
2784
			results->wm_lp[wm_lp - 1] |=
2518
			results->wm_lp[wm_lp - 1] |=
2785
				r->fbc_val << WM1_LP_FBC_SHIFT;
2519
				r->fbc_val << WM1_LP_FBC_SHIFT;
2786
 
2520
 
2787
		/*
2521
		/*
2788
		 * Always set WM1S_LP_EN when spr_val != 0, even if the
2522
		 * Always set WM1S_LP_EN when spr_val != 0, even if the
2789
		 * level is disabled. Doing otherwise could cause underruns.
2523
		 * level is disabled. Doing otherwise could cause underruns.
2790
		 */
2524
		 */
2791
		if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2525
		if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2792
			WARN_ON(wm_lp != 1);
2526
			WARN_ON(wm_lp != 1);
2793
			results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2527
			results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2794
		} else
2528
		} else
2795
		results->wm_lp_spr[wm_lp - 1] = r->spr_val;
2529
			results->wm_lp_spr[wm_lp - 1] = r->spr_val;
2796
	}
2530
	}
2797
 
2531
 
2798
	/* LP0 register values */
2532
	/* LP0 register values */
2799
	for_each_intel_crtc(dev, intel_crtc) {
2533
	for_each_intel_crtc(dev, intel_crtc) {
2800
		enum pipe pipe = intel_crtc->pipe;
2534
		enum pipe pipe = intel_crtc->pipe;
2801
		const struct intel_wm_level *r =
2535
		const struct intel_wm_level *r =
2802
			&intel_crtc->wm.active.wm[0];
2536
			&intel_crtc->wm.active.wm[0];
2803
 
2537
 
2804
		if (WARN_ON(!r->enable))
2538
		if (WARN_ON(!r->enable))
2805
			continue;
2539
			continue;
2806
 
2540
 
2807
		results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2541
		results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2808
 
2542
 
2809
		results->wm_pipe[pipe] =
2543
		results->wm_pipe[pipe] =
2810
			(r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2544
			(r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2811
			(r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2545
			(r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2812
			r->cur_val;
2546
			r->cur_val;
2813
	}
2547
	}
2814
}
2548
}
2815
 
2549
 
2816
/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2550
/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2817
 * case both are at the same level. Prefer r1 in case they're the same. */
2551
 * case both are at the same level. Prefer r1 in case they're the same. */
2818
static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
2552
static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
2819
						  struct intel_pipe_wm *r1,
2553
						  struct intel_pipe_wm *r1,
2820
						  struct intel_pipe_wm *r2)
2554
						  struct intel_pipe_wm *r2)
2821
{
2555
{
2822
	int level, max_level = ilk_wm_max_level(dev);
2556
	int level, max_level = ilk_wm_max_level(dev);
2823
	int level1 = 0, level2 = 0;
2557
	int level1 = 0, level2 = 0;
2824
 
2558
 
2825
	for (level = 1; level <= max_level; level++) {
2559
	for (level = 1; level <= max_level; level++) {
2826
		if (r1->wm[level].enable)
2560
		if (r1->wm[level].enable)
2827
			level1 = level;
2561
			level1 = level;
2828
		if (r2->wm[level].enable)
2562
		if (r2->wm[level].enable)
2829
			level2 = level;
2563
			level2 = level;
2830
	}
2564
	}
2831
 
2565
 
2832
	if (level1 == level2) {
2566
	if (level1 == level2) {
2833
		if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
2567
		if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
2834
			return r2;
2568
			return r2;
2835
		else
2569
		else
2836
			return r1;
2570
			return r1;
2837
	} else if (level1 > level2) {
2571
	} else if (level1 > level2) {
2838
		return r1;
2572
		return r1;
2839
	} else {
2573
	} else {
2840
		return r2;
2574
		return r2;
2841
	}
2575
	}
2842
}
2576
}
2843
 
2577
 
2844
/* dirty bits used to track which watermarks need changes */
2578
/* dirty bits used to track which watermarks need changes */
2845
#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2579
#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2846
#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2580
#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2847
#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2581
#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2848
#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2582
#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2849
#define WM_DIRTY_FBC (1 << 24)
2583
#define WM_DIRTY_FBC (1 << 24)
2850
#define WM_DIRTY_DDB (1 << 25)
2584
#define WM_DIRTY_DDB (1 << 25)
2851
 
2585
 
2852
static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
2586
static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
2853
					 const struct ilk_wm_values *old,
2587
					 const struct ilk_wm_values *old,
2854
					 const struct ilk_wm_values *new)
2588
					 const struct ilk_wm_values *new)
2855
{
2589
{
2856
	unsigned int dirty = 0;
2590
	unsigned int dirty = 0;
2857
	enum pipe pipe;
2591
	enum pipe pipe;
2858
	int wm_lp;
2592
	int wm_lp;
2859
 
2593
 
2860
	for_each_pipe(dev_priv, pipe) {
2594
	for_each_pipe(dev_priv, pipe) {
2861
		if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2595
		if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2862
			dirty |= WM_DIRTY_LINETIME(pipe);
2596
			dirty |= WM_DIRTY_LINETIME(pipe);
2863
			/* Must disable LP1+ watermarks too */
2597
			/* Must disable LP1+ watermarks too */
2864
			dirty |= WM_DIRTY_LP_ALL;
2598
			dirty |= WM_DIRTY_LP_ALL;
2865
		}
2599
		}
2866
 
2600
 
2867
		if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2601
		if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2868
			dirty |= WM_DIRTY_PIPE(pipe);
2602
			dirty |= WM_DIRTY_PIPE(pipe);
2869
			/* Must disable LP1+ watermarks too */
2603
			/* Must disable LP1+ watermarks too */
2870
			dirty |= WM_DIRTY_LP_ALL;
2604
			dirty |= WM_DIRTY_LP_ALL;
2871
		}
2605
		}
2872
	}
2606
	}
2873
 
2607
 
2874
	if (old->enable_fbc_wm != new->enable_fbc_wm) {
2608
	if (old->enable_fbc_wm != new->enable_fbc_wm) {
2875
		dirty |= WM_DIRTY_FBC;
2609
		dirty |= WM_DIRTY_FBC;
2876
		/* Must disable LP1+ watermarks too */
2610
		/* Must disable LP1+ watermarks too */
2877
		dirty |= WM_DIRTY_LP_ALL;
2611
		dirty |= WM_DIRTY_LP_ALL;
2878
	}
2612
	}
2879
 
2613
 
2880
	if (old->partitioning != new->partitioning) {
2614
	if (old->partitioning != new->partitioning) {
2881
		dirty |= WM_DIRTY_DDB;
2615
		dirty |= WM_DIRTY_DDB;
2882
		/* Must disable LP1+ watermarks too */
2616
		/* Must disable LP1+ watermarks too */
2883
		dirty |= WM_DIRTY_LP_ALL;
2617
		dirty |= WM_DIRTY_LP_ALL;
2884
	}
2618
	}
2885
 
2619
 
2886
	/* LP1+ watermarks already deemed dirty, no need to continue */
2620
	/* LP1+ watermarks already deemed dirty, no need to continue */
2887
	if (dirty & WM_DIRTY_LP_ALL)
2621
	if (dirty & WM_DIRTY_LP_ALL)
2888
		return dirty;
2622
		return dirty;
2889
 
2623
 
2890
	/* Find the lowest numbered LP1+ watermark in need of an update... */
2624
	/* Find the lowest numbered LP1+ watermark in need of an update... */
2891
	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2625
	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2892
		if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2626
		if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2893
		    old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2627
		    old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2894
			break;
2628
			break;
2895
	}
2629
	}
2896
 
2630
 
2897
	/* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2631
	/* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2898
	for (; wm_lp <= 3; wm_lp++)
2632
	for (; wm_lp <= 3; wm_lp++)
2899
		dirty |= WM_DIRTY_LP(wm_lp);
2633
		dirty |= WM_DIRTY_LP(wm_lp);
2900
 
2634
 
2901
	return dirty;
2635
	return dirty;
2902
}
2636
}
2903
 
2637
 
2904
static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2638
static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2905
			       unsigned int dirty)
2639
			       unsigned int dirty)
2906
{
2640
{
2907
	struct ilk_wm_values *previous = &dev_priv->wm.hw;
2641
	struct ilk_wm_values *previous = &dev_priv->wm.hw;
2908
	bool changed = false;
2642
	bool changed = false;
2909
 
2643
 
2910
	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2644
	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2911
		previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2645
		previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2912
		I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2646
		I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2913
		changed = true;
2647
		changed = true;
2914
	}
2648
	}
2915
	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2649
	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2916
		previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2650
		previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2917
		I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2651
		I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2918
		changed = true;
2652
		changed = true;
2919
	}
2653
	}
2920
	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2654
	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2921
		previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2655
		previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2922
		I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2656
		I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2923
		changed = true;
2657
		changed = true;
2924
	}
2658
	}
2925
 
2659
 
2926
	/*
2660
	/*
2927
	 * Don't touch WM1S_LP_EN here.
2661
	 * Don't touch WM1S_LP_EN here.
2928
	 * Doing so could cause underruns.
2662
	 * Doing so could cause underruns.
2929
	 */
2663
	 */
2930
 
2664
 
2931
	return changed;
2665
	return changed;
2932
}
2666
}
2933
 
2667
 
2934
/*
2668
/*
2935
 * The spec says we shouldn't write when we don't need, because every write
2669
 * The spec says we shouldn't write when we don't need, because every write
2936
 * causes WMs to be re-evaluated, expending some power.
2670
 * causes WMs to be re-evaluated, expending some power.
2937
	 */
2671
 */
2938
static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2672
static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2939
				struct ilk_wm_values *results)
2673
				struct ilk_wm_values *results)
2940
{
2674
{
2941
	struct drm_device *dev = dev_priv->dev;
2675
	struct drm_device *dev = dev_priv->dev;
2942
	struct ilk_wm_values *previous = &dev_priv->wm.hw;
2676
	struct ilk_wm_values *previous = &dev_priv->wm.hw;
2943
	unsigned int dirty;
2677
	unsigned int dirty;
2944
	uint32_t val;
2678
	uint32_t val;
2945
 
2679
 
2946
	dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
2680
	dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
2947
	if (!dirty)
2681
	if (!dirty)
2948
		return;
2682
		return;
2949
 
2683
 
2950
	_ilk_disable_lp_wm(dev_priv, dirty);
2684
	_ilk_disable_lp_wm(dev_priv, dirty);
2951
 
2685
 
2952
	if (dirty & WM_DIRTY_PIPE(PIPE_A))
2686
	if (dirty & WM_DIRTY_PIPE(PIPE_A))
2953
		I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
2687
		I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
2954
	if (dirty & WM_DIRTY_PIPE(PIPE_B))
2688
	if (dirty & WM_DIRTY_PIPE(PIPE_B))
2955
		I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
2689
		I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
2956
	if (dirty & WM_DIRTY_PIPE(PIPE_C))
2690
	if (dirty & WM_DIRTY_PIPE(PIPE_C))
2957
		I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2691
		I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2958
 
2692
 
2959
	if (dirty & WM_DIRTY_LINETIME(PIPE_A))
2693
	if (dirty & WM_DIRTY_LINETIME(PIPE_A))
2960
		I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
2694
		I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
2961
	if (dirty & WM_DIRTY_LINETIME(PIPE_B))
2695
	if (dirty & WM_DIRTY_LINETIME(PIPE_B))
2962
		I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
2696
		I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
2963
	if (dirty & WM_DIRTY_LINETIME(PIPE_C))
2697
	if (dirty & WM_DIRTY_LINETIME(PIPE_C))
2964
		I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2698
		I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2965
 
2699
 
2966
	if (dirty & WM_DIRTY_DDB) {
2700
	if (dirty & WM_DIRTY_DDB) {
2967
		if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2701
		if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2968
		val = I915_READ(WM_MISC);
2702
			val = I915_READ(WM_MISC);
2969
			if (results->partitioning == INTEL_DDB_PART_1_2)
2703
			if (results->partitioning == INTEL_DDB_PART_1_2)
2970
			val &= ~WM_MISC_DATA_PARTITION_5_6;
2704
				val &= ~WM_MISC_DATA_PARTITION_5_6;
2971
		else
2705
			else
2972
			val |= WM_MISC_DATA_PARTITION_5_6;
2706
				val |= WM_MISC_DATA_PARTITION_5_6;
2973
		I915_WRITE(WM_MISC, val);
2707
			I915_WRITE(WM_MISC, val);
2974
		} else {
2708
		} else {
2975
			val = I915_READ(DISP_ARB_CTL2);
2709
			val = I915_READ(DISP_ARB_CTL2);
2976
			if (results->partitioning == INTEL_DDB_PART_1_2)
2710
			if (results->partitioning == INTEL_DDB_PART_1_2)
2977
				val &= ~DISP_DATA_PARTITION_5_6;
2711
				val &= ~DISP_DATA_PARTITION_5_6;
2978
			else
2712
			else
2979
				val |= DISP_DATA_PARTITION_5_6;
2713
				val |= DISP_DATA_PARTITION_5_6;
2980
			I915_WRITE(DISP_ARB_CTL2, val);
2714
			I915_WRITE(DISP_ARB_CTL2, val);
2981
		}
2715
		}
2982
	}
2716
	}
2983
 
2717
 
2984
	if (dirty & WM_DIRTY_FBC) {
2718
	if (dirty & WM_DIRTY_FBC) {
2985
		val = I915_READ(DISP_ARB_CTL);
2719
		val = I915_READ(DISP_ARB_CTL);
2986
		if (results->enable_fbc_wm)
2720
		if (results->enable_fbc_wm)
2987
			val &= ~DISP_FBC_WM_DIS;
2721
			val &= ~DISP_FBC_WM_DIS;
2988
		else
2722
		else
2989
			val |= DISP_FBC_WM_DIS;
2723
			val |= DISP_FBC_WM_DIS;
2990
		I915_WRITE(DISP_ARB_CTL, val);
2724
		I915_WRITE(DISP_ARB_CTL, val);
2991
	}
2725
	}
2992
 
2726
 
2993
	if (dirty & WM_DIRTY_LP(1) &&
2727
	if (dirty & WM_DIRTY_LP(1) &&
2994
	    previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2728
	    previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2995
		I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2729
		I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2996
 
2730
 
2997
	if (INTEL_INFO(dev)->gen >= 7) {
2731
	if (INTEL_INFO(dev)->gen >= 7) {
2998
		if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2732
		if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2999
		I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2733
			I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3000
		if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2734
		if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3001
		I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2735
			I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3002
	}
2736
	}
3003
 
2737
 
3004
	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
2738
	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
3005
		I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
2739
		I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
3006
	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
2740
	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
3007
		I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
2741
		I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
3008
	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
2742
	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
3009
		I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
2743
		I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
3010
 
2744
 
3011
	dev_priv->wm.hw = *results;
2745
	dev_priv->wm.hw = *results;
3012
}
2746
}
3013
 
2747
 
3014
static bool ilk_disable_lp_wm(struct drm_device *dev)
2748
static bool ilk_disable_lp_wm(struct drm_device *dev)
3015
{
2749
{
3016
	struct drm_i915_private *dev_priv = dev->dev_private;
2750
	struct drm_i915_private *dev_priv = dev->dev_private;
3017
 
2751
 
3018
	return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2752
	return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3019
}
2753
}
3020
 
2754
 
3021
/*
2755
/*
3022
 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2756
 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
3023
 * different active planes.
2757
 * different active planes.
3024
 */
2758
 */
3025
 
2759
 
3026
#define SKL_DDB_SIZE		896	/* in blocks */
2760
#define SKL_DDB_SIZE		896	/* in blocks */
-
 
2761
#define BXT_DDB_SIZE		512
3027
 
2762
 
3028
static void
2763
static void
3029
skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2764
skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
3030
				   struct drm_crtc *for_crtc,
2765
				   struct drm_crtc *for_crtc,
3031
				   const struct intel_wm_config *config,
2766
				   const struct intel_wm_config *config,
3032
				   const struct skl_pipe_wm_parameters *params,
2767
				   const struct skl_pipe_wm_parameters *params,
3033
				   struct skl_ddb_entry *alloc /* out */)
2768
				   struct skl_ddb_entry *alloc /* out */)
3034
{
2769
{
3035
	struct drm_crtc *crtc;
2770
	struct drm_crtc *crtc;
3036
	unsigned int pipe_size, ddb_size;
2771
	unsigned int pipe_size, ddb_size;
3037
	int nth_active_pipe;
2772
	int nth_active_pipe;
3038
 
2773
 
3039
	if (!params->active) {
2774
	if (!params->active) {
3040
		alloc->start = 0;
2775
		alloc->start = 0;
3041
		alloc->end = 0;
2776
		alloc->end = 0;
3042
		return;
2777
		return;
3043
	}
2778
	}
-
 
2779
 
-
 
2780
	if (IS_BROXTON(dev))
-
 
2781
		ddb_size = BXT_DDB_SIZE;
3044
 
2782
	else
3045
	ddb_size = SKL_DDB_SIZE;
2783
		ddb_size = SKL_DDB_SIZE;
3046
 
2784
 
3047
	ddb_size -= 4; /* 4 blocks for bypass path allocation */
2785
	ddb_size -= 4; /* 4 blocks for bypass path allocation */
3048
 
2786
 
3049
	nth_active_pipe = 0;
2787
	nth_active_pipe = 0;
3050
	for_each_crtc(dev, crtc) {
2788
	for_each_crtc(dev, crtc) {
3051
		if (!intel_crtc_active(crtc))
2789
		if (!to_intel_crtc(crtc)->active)
3052
			continue;
2790
			continue;
3053
 
2791
 
3054
		if (crtc == for_crtc)
2792
		if (crtc == for_crtc)
3055
			break;
2793
			break;
3056
 
2794
 
3057
		nth_active_pipe++;
2795
		nth_active_pipe++;
3058
	}
2796
	}
3059
 
2797
 
3060
	pipe_size = ddb_size / config->num_pipes_active;
2798
	pipe_size = ddb_size / config->num_pipes_active;
3061
	alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
2799
	alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
3062
	alloc->end = alloc->start + pipe_size;
2800
	alloc->end = alloc->start + pipe_size;
3063
}
2801
}
3064
 
2802
 
3065
static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2803
static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
3066
{
2804
{
3067
	if (config->num_pipes_active == 1)
2805
	if (config->num_pipes_active == 1)
3068
		return 32;
2806
		return 32;
3069
 
2807
 
3070
	return 8;
2808
	return 8;
3071
}
2809
}
3072
 
2810
 
3073
static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2811
static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3074
{
2812
{
3075
	entry->start = reg & 0x3ff;
2813
	entry->start = reg & 0x3ff;
3076
	entry->end = (reg >> 16) & 0x3ff;
2814
	entry->end = (reg >> 16) & 0x3ff;
3077
	if (entry->end)
2815
	if (entry->end)
3078
		entry->end += 1;
2816
		entry->end += 1;
3079
}
2817
}
3080
 
2818
 
3081
void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2819
void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3082
			  struct skl_ddb_allocation *ddb /* out */)
2820
			  struct skl_ddb_allocation *ddb /* out */)
3083
{
2821
{
3084
	struct drm_device *dev = dev_priv->dev;
-
 
3085
	enum pipe pipe;
2822
	enum pipe pipe;
3086
	int plane;
2823
	int plane;
3087
	u32 val;
2824
	u32 val;
-
 
2825
 
-
 
2826
	memset(ddb, 0, sizeof(*ddb));
3088
 
2827
 
-
 
2828
	for_each_pipe(dev_priv, pipe) {
-
 
2829
		if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PIPE(pipe)))
-
 
2830
			continue;
3089
	for_each_pipe(dev_priv, pipe) {
2831
 
3090
		for_each_plane(pipe, plane) {
2832
		for_each_plane(dev_priv, pipe, plane) {
3091
			val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2833
			val = I915_READ(PLANE_BUF_CFG(pipe, plane));
3092
			skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2834
			skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
3093
						   val);
2835
						   val);
3094
		}
2836
		}
3095
 
2837
 
3096
		val = I915_READ(CUR_BUF_CFG(pipe));
2838
		val = I915_READ(CUR_BUF_CFG(pipe));
3097
		skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2839
		skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
-
 
2840
					   val);
3098
	}
2841
	}
3099
}
2842
}
3100
 
2843
 
3101
static unsigned int
2844
static unsigned int
3102
skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2845
skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p, int y)
3103
{
2846
{
-
 
2847
 
-
 
2848
	/* for planar format */
-
 
2849
	if (p->y_bytes_per_pixel) {
-
 
2850
		if (y)  /* y-plane data rate */
-
 
2851
			return p->horiz_pixels * p->vert_pixels * p->y_bytes_per_pixel;
-
 
2852
		else    /* uv-plane data rate */
-
 
2853
			return (p->horiz_pixels/2) * (p->vert_pixels/2) * p->bytes_per_pixel;
-
 
2854
	}
-
 
2855
 
-
 
2856
	/* for packed formats */
3104
	return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2857
	return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
3105
}
2858
}
3106
 
2859
 
3107
/*
2860
/*
3108
 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2861
 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3109
 * a 8192x4096@32bpp framebuffer:
2862
 * a 8192x4096@32bpp framebuffer:
3110
 *   3 * 4096 * 8192  * 4 < 2^32
2863
 *   3 * 4096 * 8192  * 4 < 2^32
3111
 */
2864
 */
3112
static unsigned int
2865
static unsigned int
3113
skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2866
skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
3114
				 const struct skl_pipe_wm_parameters *params)
2867
				 const struct skl_pipe_wm_parameters *params)
3115
{
2868
{
3116
	unsigned int total_data_rate = 0;
2869
	unsigned int total_data_rate = 0;
3117
	int plane;
2870
	int plane;
3118
 
2871
 
3119
	for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2872
	for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
3120
		const struct intel_plane_wm_parameters *p;
2873
		const struct intel_plane_wm_parameters *p;
3121
 
2874
 
3122
		p = ¶ms->plane[plane];
2875
		p = ¶ms->plane[plane];
3123
		if (!p->enabled)
2876
		if (!p->enabled)
3124
			continue;
2877
			continue;
-
 
2878
 
-
 
2879
		total_data_rate += skl_plane_relative_data_rate(p, 0); /* packed/uv */
3125
 
2880
		if (p->y_bytes_per_pixel) {
-
 
2881
			total_data_rate += skl_plane_relative_data_rate(p, 1); /* y-plane */
3126
		total_data_rate += skl_plane_relative_data_rate(p);
2882
		}
3127
	}
2883
	}
3128
 
2884
 
3129
	return total_data_rate;
2885
	return total_data_rate;
3130
}
2886
}
3131
 
2887
 
3132
static void
2888
static void
3133
skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2889
skl_allocate_pipe_ddb(struct drm_crtc *crtc,
3134
		      const struct intel_wm_config *config,
2890
		      const struct intel_wm_config *config,
3135
		      const struct skl_pipe_wm_parameters *params,
2891
		      const struct skl_pipe_wm_parameters *params,
3136
		      struct skl_ddb_allocation *ddb /* out */)
2892
		      struct skl_ddb_allocation *ddb /* out */)
3137
{
2893
{
3138
	struct drm_device *dev = crtc->dev;
2894
	struct drm_device *dev = crtc->dev;
-
 
2895
	struct drm_i915_private *dev_priv = dev->dev_private;
3139
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2896
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3140
	enum pipe pipe = intel_crtc->pipe;
2897
	enum pipe pipe = intel_crtc->pipe;
3141
	struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
2898
	struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
3142
	uint16_t alloc_size, start, cursor_blocks;
2899
	uint16_t alloc_size, start, cursor_blocks;
-
 
2900
	uint16_t minimum[I915_MAX_PLANES];
-
 
2901
	uint16_t y_minimum[I915_MAX_PLANES];
3143
	unsigned int total_data_rate;
2902
	unsigned int total_data_rate;
3144
	int plane;
2903
	int plane;
3145
 
2904
 
3146
	skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2905
	skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
3147
	alloc_size = skl_ddb_entry_size(alloc);
2906
	alloc_size = skl_ddb_entry_size(alloc);
3148
	if (alloc_size == 0) {
2907
	if (alloc_size == 0) {
3149
		memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2908
		memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3150
		memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2909
		memset(&ddb->plane[pipe][PLANE_CURSOR], 0,
-
 
2910
		       sizeof(ddb->plane[pipe][PLANE_CURSOR]));
3151
		return;
2911
		return;
3152
	}
2912
	}
3153
 
2913
 
3154
	cursor_blocks = skl_cursor_allocation(config);
2914
	cursor_blocks = skl_cursor_allocation(config);
3155
	ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2915
	ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - cursor_blocks;
3156
	ddb->cursor[pipe].end = alloc->end;
2916
	ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
3157
 
2917
 
3158
	alloc_size -= cursor_blocks;
2918
	alloc_size -= cursor_blocks;
3159
	alloc->end -= cursor_blocks;
2919
	alloc->end -= cursor_blocks;
-
 
2920
 
-
 
2921
	/* 1. Allocate the mininum required blocks for each active plane */
-
 
2922
	for_each_plane(dev_priv, pipe, plane) {
-
 
2923
		const struct intel_plane_wm_parameters *p;
-
 
2924
 
-
 
2925
		p = ¶ms->plane[plane];
-
 
2926
		if (!p->enabled)
-
 
2927
			continue;
-
 
2928
 
-
 
2929
		minimum[plane] = 8;
-
 
2930
		alloc_size -= minimum[plane];
-
 
2931
		y_minimum[plane] = p->y_bytes_per_pixel ? 8 : 0;
-
 
2932
		alloc_size -= y_minimum[plane];
-
 
2933
	}
3160
 
2934
 
3161
		/*
2935
	/*
3162
	 * Each active plane get a portion of the remaining space, in
2936
	 * 2. Distribute the remaining space in proportion to the amount of
3163
	 * proportion to the amount of data they need to fetch from memory.
2937
	 * data each plane needs to fetch from memory.
3164
	 *
2938
	 *
3165
	 * FIXME: we may not allocate every single block here.
2939
	 * FIXME: we may not allocate every single block here.
3166
		 */
2940
	 */
3167
	total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2941
	total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
3168
 
2942
 
3169
	start = alloc->start;
2943
	start = alloc->start;
3170
	for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2944
	for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
3171
		const struct intel_plane_wm_parameters *p;
2945
		const struct intel_plane_wm_parameters *p;
3172
		unsigned int data_rate;
2946
		unsigned int data_rate, y_data_rate;
3173
		uint16_t plane_blocks;
2947
		uint16_t plane_blocks, y_plane_blocks = 0;
3174
 
2948
 
3175
		p = ¶ms->plane[plane];
2949
		p = ¶ms->plane[plane];
3176
		if (!p->enabled)
2950
		if (!p->enabled)
3177
			continue;
2951
			continue;
3178
 
2952
 
3179
		data_rate = skl_plane_relative_data_rate(p);
2953
		data_rate = skl_plane_relative_data_rate(p, 0);
-
 
2954
 
3180
 
2955
		/*
3181
		/*
2956
		 * allocation for (packed formats) or (uv-plane part of planar format):
3182
		 * promote the expression to 64 bits to avoid overflowing, the
2957
		 * promote the expression to 64 bits to avoid overflowing, the
3183
		 * result is < available as data_rate / total_data_rate < 1
2958
		 * result is < available as data_rate / total_data_rate < 1
3184
		 */
2959
		 */
-
 
2960
		plane_blocks = minimum[plane];
3185
		plane_blocks = div_u64((uint64_t)alloc_size * data_rate,
2961
		plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3186
				       total_data_rate);
2962
					total_data_rate);
3187
 
2963
 
3188
		ddb->plane[pipe][plane].start = start;
2964
		ddb->plane[pipe][plane].start = start;
3189
		ddb->plane[pipe][plane].end = start + plane_blocks;
2965
		ddb->plane[pipe][plane].end = start + plane_blocks;
3190
 
2966
 
3191
		start += plane_blocks;
2967
		start += plane_blocks;
-
 
2968
 
-
 
2969
		/*
-
 
2970
		 * allocation for y_plane part of planar format:
-
 
2971
		 */
-
 
2972
		if (p->y_bytes_per_pixel) {
-
 
2973
			y_data_rate = skl_plane_relative_data_rate(p, 1);
-
 
2974
			y_plane_blocks = y_minimum[plane];
-
 
2975
			y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
-
 
2976
						total_data_rate);
-
 
2977
 
-
 
2978
			ddb->y_plane[pipe][plane].start = start;
-
 
2979
			ddb->y_plane[pipe][plane].end = start + y_plane_blocks;
-
 
2980
 
-
 
2981
			start += y_plane_blocks;
3192
	}
2982
		}
3193
 
2983
 
3194
}
2984
	}
-
 
2985
 
-
 
2986
}
3195
 
2987
 
3196
static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_config *config)
2988
static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
3197
{
2989
{
3198
	/* TODO: Take into account the scalers once we support them */
2990
	/* TODO: Take into account the scalers once we support them */
3199
	return config->adjusted_mode.crtc_clock;
2991
	return config->base.adjusted_mode.crtc_clock;
3200
}
2992
}
3201
 
2993
 
3202
/*
2994
/*
3203
 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2995
 * The max latency should be 257 (max the punit can code is 255 and we add 2us
3204
 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2996
 * for the read latency) and bytes_per_pixel should always be <= 8, so that
3205
 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2997
 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3206
 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2998
 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3207
*/
2999
*/
3208
static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
3000
static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
3209
			       uint32_t latency)
3001
			       uint32_t latency)
3210
{
3002
{
3211
	uint32_t wm_intermediate_val, ret;
3003
	uint32_t wm_intermediate_val, ret;
3212
 
3004
 
3213
	if (latency == 0)
3005
	if (latency == 0)
3214
		return UINT_MAX;
3006
		return UINT_MAX;
3215
 
3007
 
3216
	wm_intermediate_val = latency * pixel_rate * bytes_per_pixel;
3008
	wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
3217
	ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3009
	ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3218
 
3010
 
3219
	return ret;
3011
	return ret;
3220
}
3012
}
3221
 
3013
 
3222
static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
3014
static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
3223
			       uint32_t horiz_pixels, uint8_t bytes_per_pixel,
3015
			       uint32_t horiz_pixels, uint8_t bytes_per_pixel,
3224
			       uint32_t latency)
3016
			       uint64_t tiling, uint32_t latency)
3225
{
3017
{
-
 
3018
	uint32_t ret;
3226
	uint32_t ret, plane_bytes_per_line, wm_intermediate_val;
3019
	uint32_t plane_bytes_per_line, plane_blocks_per_line;
-
 
3020
	uint32_t wm_intermediate_val;
3227
 
3021
 
3228
	if (latency == 0)
3022
	if (latency == 0)
3229
		return UINT_MAX;
3023
		return UINT_MAX;
3230
 
3024
 
3231
	plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
3025
	plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
-
 
3026
 
-
 
3027
	if (tiling == I915_FORMAT_MOD_Y_TILED ||
-
 
3028
	    tiling == I915_FORMAT_MOD_Yf_TILED) {
-
 
3029
		plane_bytes_per_line *= 4;
-
 
3030
		plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
-
 
3031
		plane_blocks_per_line /= 4;
-
 
3032
	} else {
-
 
3033
		plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
-
 
3034
	}
-
 
3035
 
3232
	wm_intermediate_val = latency * pixel_rate;
3036
	wm_intermediate_val = latency * pixel_rate;
3233
	ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
3037
	ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
3234
				plane_bytes_per_line;
3038
				plane_blocks_per_line;
3235
 
3039
 
3236
	return ret;
3040
	return ret;
3237
}
3041
}
3238
 
3042
 
3239
static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3043
static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3240
				       const struct intel_crtc *intel_crtc)
3044
				       const struct intel_crtc *intel_crtc)
3241
{
3045
{
3242
	struct drm_device *dev = intel_crtc->base.dev;
3046
	struct drm_device *dev = intel_crtc->base.dev;
3243
	struct drm_i915_private *dev_priv = dev->dev_private;
3047
	struct drm_i915_private *dev_priv = dev->dev_private;
3244
	const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
3048
	const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
3245
	enum pipe pipe = intel_crtc->pipe;
3049
	enum pipe pipe = intel_crtc->pipe;
3246
 
3050
 
3247
	if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
3051
	if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
3248
		   sizeof(new_ddb->plane[pipe])))
3052
		   sizeof(new_ddb->plane[pipe])))
3249
		return true;
3053
		return true;
3250
 
3054
 
3251
	if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
3055
	if (memcmp(&new_ddb->plane[pipe][PLANE_CURSOR], &cur_ddb->plane[pipe][PLANE_CURSOR],
3252
		    sizeof(new_ddb->cursor[pipe])))
3056
		    sizeof(new_ddb->plane[pipe][PLANE_CURSOR])))
3253
		return true;
3057
		return true;
3254
 
3058
 
3255
	return false;
3059
	return false;
3256
}
3060
}
3257
 
3061
 
3258
static void skl_compute_wm_global_parameters(struct drm_device *dev,
3062
static void skl_compute_wm_global_parameters(struct drm_device *dev,
3259
					     struct intel_wm_config *config)
3063
					     struct intel_wm_config *config)
3260
{
3064
{
3261
	struct drm_crtc *crtc;
3065
	struct drm_crtc *crtc;
3262
	struct drm_plane *plane;
3066
	struct drm_plane *plane;
3263
 
3067
 
3264
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3068
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3265
		config->num_pipes_active += intel_crtc_active(crtc);
3069
		config->num_pipes_active += to_intel_crtc(crtc)->active;
3266
 
3070
 
3267
	/* FIXME: I don't think we need those two global parameters on SKL */
3071
	/* FIXME: I don't think we need those two global parameters on SKL */
3268
	list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3072
	list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3269
		struct intel_plane *intel_plane = to_intel_plane(plane);
3073
		struct intel_plane *intel_plane = to_intel_plane(plane);
3270
 
3074
 
3271
		config->sprites_enabled |= intel_plane->wm.enabled;
3075
		config->sprites_enabled |= intel_plane->wm.enabled;
3272
		config->sprites_scaled |= intel_plane->wm.scaled;
3076
		config->sprites_scaled |= intel_plane->wm.scaled;
3273
	}
3077
	}
3274
}
3078
}
3275
 
3079
 
3276
static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
3080
static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
3277
					   struct skl_pipe_wm_parameters *p)
3081
					   struct skl_pipe_wm_parameters *p)
3278
{
3082
{
3279
	struct drm_device *dev = crtc->dev;
3083
	struct drm_device *dev = crtc->dev;
3280
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3084
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3281
	enum pipe pipe = intel_crtc->pipe;
3085
	enum pipe pipe = intel_crtc->pipe;
3282
	struct drm_plane *plane;
3086
	struct drm_plane *plane;
-
 
3087
	struct drm_framebuffer *fb;
3283
	int i = 1; /* Index for sprite planes start */
3088
	int i = 1; /* Index for sprite planes start */
3284
 
3089
 
3285
	p->active = intel_crtc_active(crtc);
3090
	p->active = intel_crtc->active;
3286
	if (p->active) {
3091
	if (p->active) {
3287
		p->pipe_htotal = intel_crtc->config.adjusted_mode.crtc_htotal;
3092
		p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
3288
		p->pixel_rate = skl_pipe_pixel_rate(&intel_crtc->config);
3093
		p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
3289
 
3094
 
3290
		/*
3095
		fb = crtc->primary->state->fb;
3291
		 * For now, assume primary and cursor planes are always enabled.
3096
		/* For planar: Bpp is for uv plane, y_Bpp is for y plane */
-
 
3097
		if (fb) {
-
 
3098
			p->plane[0].enabled = true;
-
 
3099
			p->plane[0].bytes_per_pixel = fb->pixel_format == DRM_FORMAT_NV12 ?
-
 
3100
				drm_format_plane_cpp(fb->pixel_format, 1) :
-
 
3101
				drm_format_plane_cpp(fb->pixel_format, 0);
-
 
3102
			p->plane[0].y_bytes_per_pixel = fb->pixel_format == DRM_FORMAT_NV12 ?
-
 
3103
				drm_format_plane_cpp(fb->pixel_format, 0) : 0;
-
 
3104
			p->plane[0].tiling = fb->modifier[0];
3292
		 */
3105
		} else {
3293
		p->plane[0].enabled = true;
3106
			p->plane[0].enabled = false;
-
 
3107
			p->plane[0].bytes_per_pixel = 0;
-
 
3108
			p->plane[0].y_bytes_per_pixel = 0;
3294
		p->plane[0].bytes_per_pixel =
3109
			p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
3295
			crtc->primary->fb->bits_per_pixel / 8;
3110
		}
-
 
3111
		p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
3296
		p->plane[0].horiz_pixels = intel_crtc->config.pipe_src_w;
3112
		p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
3297
		p->plane[0].vert_pixels = intel_crtc->config.pipe_src_h;
3113
		p->plane[0].rotation = crtc->primary->state->rotation;
3298
 
3114
 
-
 
3115
		fb = crtc->cursor->state->fb;
-
 
3116
		p->plane[PLANE_CURSOR].y_bytes_per_pixel = 0;
-
 
3117
		if (fb) {
3299
		p->cursor.enabled = true;
3118
			p->plane[PLANE_CURSOR].enabled = true;
-
 
3119
			p->plane[PLANE_CURSOR].bytes_per_pixel = fb->bits_per_pixel / 8;
-
 
3120
			p->plane[PLANE_CURSOR].horiz_pixels = crtc->cursor->state->crtc_w;
-
 
3121
			p->plane[PLANE_CURSOR].vert_pixels = crtc->cursor->state->crtc_h;
-
 
3122
		} else {
3300
		p->cursor.bytes_per_pixel = 4;
3123
			p->plane[PLANE_CURSOR].enabled = false;
-
 
3124
			p->plane[PLANE_CURSOR].bytes_per_pixel = 0;
-
 
3125
			p->plane[PLANE_CURSOR].horiz_pixels = 64;
3301
		p->cursor.horiz_pixels = intel_crtc->cursor_width ?
3126
			p->plane[PLANE_CURSOR].vert_pixels = 64;
3302
					 intel_crtc->cursor_width : 64;
3127
		}
3303
	}
3128
	}
3304
 
3129
 
3305
	list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3130
	list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3306
		struct intel_plane *intel_plane = to_intel_plane(plane);
3131
		struct intel_plane *intel_plane = to_intel_plane(plane);
3307
 
3132
 
-
 
3133
		if (intel_plane->pipe == pipe &&
3308
		if (intel_plane->pipe == pipe)
3134
			plane->type == DRM_PLANE_TYPE_OVERLAY)
3309
			p->plane[i++] = intel_plane->wm;
3135
			p->plane[i++] = intel_plane->wm;
3310
	}
3136
	}
3311
}
3137
}
3312
 
3138
 
-
 
3139
static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3313
static bool skl_compute_plane_wm(struct skl_pipe_wm_parameters *p,
3140
				 struct skl_pipe_wm_parameters *p,
3314
				 struct intel_plane_wm_parameters *p_params,
3141
				 struct intel_plane_wm_parameters *p_params,
3315
				 uint16_t ddb_allocation,
3142
				 uint16_t ddb_allocation,
3316
				 uint32_t mem_value,
3143
				 int level,
3317
				 uint16_t *out_blocks, /* out */
3144
				 uint16_t *out_blocks, /* out */
3318
				 uint8_t *out_lines /* out */)
3145
				 uint8_t *out_lines /* out */)
3319
{
3146
{
-
 
3147
	uint32_t latency = dev_priv->wm.skl_latency[level];
-
 
3148
	uint32_t method1, method2;
3320
	uint32_t method1, method2, plane_bytes_per_line, res_blocks, res_lines;
3149
	uint32_t plane_bytes_per_line, plane_blocks_per_line;
-
 
3150
	uint32_t res_blocks, res_lines;
3321
	uint32_t result_bytes;
3151
	uint32_t selected_result;
-
 
3152
	uint8_t bytes_per_pixel;
3322
 
3153
 
3323
	if (mem_value == 0 || !p->active || !p_params->enabled)
3154
	if (latency == 0 || !p->active || !p_params->enabled)
-
 
3155
		return false;
-
 
3156
 
-
 
3157
	bytes_per_pixel = p_params->y_bytes_per_pixel ?
3324
		return false;
3158
		p_params->y_bytes_per_pixel :
3325
 
3159
		p_params->bytes_per_pixel;
3326
	method1 = skl_wm_method1(p->pixel_rate,
3160
	method1 = skl_wm_method1(p->pixel_rate,
3327
				 p_params->bytes_per_pixel,
3161
				 bytes_per_pixel,
3328
				 mem_value);
3162
				 latency);
3329
	method2 = skl_wm_method2(p->pixel_rate,
3163
	method2 = skl_wm_method2(p->pixel_rate,
3330
				 p->pipe_htotal,
3164
				 p->pipe_htotal,
3331
				 p_params->horiz_pixels,
3165
				 p_params->horiz_pixels,
-
 
3166
				 bytes_per_pixel,
-
 
3167
				 p_params->tiling,
-
 
3168
				 latency);
-
 
3169
 
-
 
3170
	plane_bytes_per_line = p_params->horiz_pixels * bytes_per_pixel;
-
 
3171
	plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
-
 
3172
 
-
 
3173
	if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
-
 
3174
	    p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
-
 
3175
		uint32_t min_scanlines = 4;
-
 
3176
		uint32_t y_tile_minimum;
-
 
3177
		if (intel_rotation_90_or_270(p_params->rotation)) {
3332
				 p_params->bytes_per_pixel,
3178
			switch (p_params->bytes_per_pixel) {
-
 
3179
			case 1:
-
 
3180
				min_scanlines = 16;
3333
				 mem_value);
3181
				break;
-
 
3182
			case 2:
-
 
3183
				min_scanlines = 8;
-
 
3184
				break;
-
 
3185
			case 8:
-
 
3186
				WARN(1, "Unsupported pixel depth for rotation");
-
 
3187
			}
-
 
3188
		}
-
 
3189
		y_tile_minimum = plane_blocks_per_line * min_scanlines;
-
 
3190
		selected_result = max(method2, y_tile_minimum);
-
 
3191
	} else {
-
 
3192
		if ((ddb_allocation / plane_blocks_per_line) >= 1)
-
 
3193
			selected_result = min(method1, method2);
-
 
3194
		else
-
 
3195
			selected_result = method1;
-
 
3196
	}
3334
 
3197
 
3335
	plane_bytes_per_line = p_params->horiz_pixels *
3198
	res_blocks = selected_result + 1;
3336
					p_params->bytes_per_pixel;
3199
	res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
3337
 
3200
 
3338
	/* For now xtile and linear */
3201
	if (level >= 1 && level <= 7) {
-
 
3202
		if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
3339
	if (((ddb_allocation * 512) / plane_bytes_per_line) >= 1)
3203
		    p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
3340
		result_bytes = min(method1, method2);
3204
			res_lines += 4;
3341
	else
3205
		else
3342
		result_bytes = method1;
-
 
3343
 
-
 
3344
	res_blocks = DIV_ROUND_UP(result_bytes, 512) + 1;
3206
			res_blocks++;
3345
	res_lines = DIV_ROUND_UP(result_bytes, plane_bytes_per_line);
3207
	}
3346
 
3208
 
3347
	if (res_blocks > ddb_allocation || res_lines > 31)
3209
	if (res_blocks >= ddb_allocation || res_lines > 31)
3348
		return false;
3210
		return false;
3349
 
3211
 
3350
	*out_blocks = res_blocks;
3212
	*out_blocks = res_blocks;
3351
	*out_lines = res_lines;
3213
	*out_lines = res_lines;
3352
 
3214
 
3353
	return true;
3215
	return true;
3354
}
3216
}
3355
 
3217
 
3356
static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3218
static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3357
				 struct skl_ddb_allocation *ddb,
3219
				 struct skl_ddb_allocation *ddb,
3358
				 struct skl_pipe_wm_parameters *p,
3220
				 struct skl_pipe_wm_parameters *p,
3359
				 enum pipe pipe,
3221
				 enum pipe pipe,
3360
				 int level,
3222
				 int level,
3361
				 int num_planes,
3223
				 int num_planes,
3362
				 struct skl_wm_level *result)
3224
				 struct skl_wm_level *result)
3363
{
3225
{
3364
	uint16_t latency = dev_priv->wm.skl_latency[level];
-
 
3365
	uint16_t ddb_blocks;
3226
	uint16_t ddb_blocks;
3366
	int i;
3227
	int i;
3367
 
3228
 
3368
	for (i = 0; i < num_planes; i++) {
3229
	for (i = 0; i < num_planes; i++) {
3369
		ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3230
		ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3370
 
3231
 
-
 
3232
		result->plane_en[i] = skl_compute_plane_wm(dev_priv,
3371
		result->plane_en[i] = skl_compute_plane_wm(p, &p->plane[i],
3233
						p, &p->plane[i],
3372
						ddb_blocks,
3234
						ddb_blocks,
3373
						latency,
3235
						level,
3374
						&result->plane_res_b[i],
3236
						&result->plane_res_b[i],
3375
						&result->plane_res_l[i]);
3237
						&result->plane_res_l[i]);
3376
	}
3238
	}
3377
 
3239
 
3378
	ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
3240
	ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][PLANE_CURSOR]);
-
 
3241
	result->plane_en[PLANE_CURSOR] = skl_compute_plane_wm(dev_priv, p,
-
 
3242
						 &p->plane[PLANE_CURSOR],
3379
	result->cursor_en = skl_compute_plane_wm(p, &p->cursor, ddb_blocks,
3243
						 ddb_blocks, level,
3380
						 latency, &result->cursor_res_b,
3244
						 &result->plane_res_b[PLANE_CURSOR],
3381
						 &result->cursor_res_l);
3245
						 &result->plane_res_l[PLANE_CURSOR]);
3382
}
3246
}
3383
 
3247
 
3384
static uint32_t
3248
static uint32_t
3385
skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
3249
skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
3386
{
3250
{
3387
	if (!intel_crtc_active(crtc))
3251
	if (!to_intel_crtc(crtc)->active)
3388
		return 0;
3252
		return 0;
3389
 
3253
 
-
 
3254
	if (WARN_ON(p->pixel_rate == 0))
-
 
3255
		return 0;
3390
	return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
3256
 
3391
 
3257
	return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
3392
}
3258
}
3393
 
3259
 
3394
static void skl_compute_transition_wm(struct drm_crtc *crtc,
3260
static void skl_compute_transition_wm(struct drm_crtc *crtc,
3395
				      struct skl_pipe_wm_parameters *params,
3261
				      struct skl_pipe_wm_parameters *params,
3396
				      struct skl_wm_level *trans_wm /* out */)
3262
				      struct skl_wm_level *trans_wm /* out */)
3397
{
3263
{
3398
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3264
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3399
	int i;
3265
	int i;
3400
 
3266
 
3401
	if (!params->active)
3267
	if (!params->active)
3402
		return;
3268
		return;
3403
 
3269
 
3404
	/* Until we know more, just disable transition WMs */
3270
	/* Until we know more, just disable transition WMs */
3405
	for (i = 0; i < intel_num_planes(intel_crtc); i++)
3271
	for (i = 0; i < intel_num_planes(intel_crtc); i++)
3406
		trans_wm->plane_en[i] = false;
3272
		trans_wm->plane_en[i] = false;
3407
	trans_wm->cursor_en = false;
3273
	trans_wm->plane_en[PLANE_CURSOR] = false;
3408
}
3274
}
3409
 
3275
 
3410
static void skl_compute_pipe_wm(struct drm_crtc *crtc,
3276
static void skl_compute_pipe_wm(struct drm_crtc *crtc,
3411
				struct skl_ddb_allocation *ddb,
3277
				struct skl_ddb_allocation *ddb,
3412
				struct skl_pipe_wm_parameters *params,
3278
				struct skl_pipe_wm_parameters *params,
3413
				struct skl_pipe_wm *pipe_wm)
3279
				struct skl_pipe_wm *pipe_wm)
3414
{
3280
{
3415
	struct drm_device *dev = crtc->dev;
3281
	struct drm_device *dev = crtc->dev;
3416
	const struct drm_i915_private *dev_priv = dev->dev_private;
3282
	const struct drm_i915_private *dev_priv = dev->dev_private;
3417
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3283
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3418
	int level, max_level = ilk_wm_max_level(dev);
3284
	int level, max_level = ilk_wm_max_level(dev);
3419
 
3285
 
3420
	for (level = 0; level <= max_level; level++) {
3286
	for (level = 0; level <= max_level; level++) {
3421
		skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
3287
		skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
3422
				     level, intel_num_planes(intel_crtc),
3288
				     level, intel_num_planes(intel_crtc),
3423
				     &pipe_wm->wm[level]);
3289
				     &pipe_wm->wm[level]);
3424
	}
3290
	}
3425
	pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
3291
	pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
3426
 
3292
 
3427
	skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
3293
	skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
3428
}
3294
}
3429
 
3295
 
3430
static void skl_compute_wm_results(struct drm_device *dev,
3296
static void skl_compute_wm_results(struct drm_device *dev,
3431
				   struct skl_pipe_wm_parameters *p,
3297
				   struct skl_pipe_wm_parameters *p,
3432
				   struct skl_pipe_wm *p_wm,
3298
				   struct skl_pipe_wm *p_wm,
3433
				   struct skl_wm_values *r,
3299
				   struct skl_wm_values *r,
3434
				   struct intel_crtc *intel_crtc)
3300
				   struct intel_crtc *intel_crtc)
3435
{
3301
{
3436
	int level, max_level = ilk_wm_max_level(dev);
3302
	int level, max_level = ilk_wm_max_level(dev);
3437
	enum pipe pipe = intel_crtc->pipe;
3303
	enum pipe pipe = intel_crtc->pipe;
3438
	uint32_t temp;
3304
	uint32_t temp;
3439
	int i;
3305
	int i;
3440
 
3306
 
3441
	for (level = 0; level <= max_level; level++) {
3307
	for (level = 0; level <= max_level; level++) {
3442
		for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3308
		for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3443
			temp = 0;
3309
			temp = 0;
3444
 
3310
 
3445
			temp |= p_wm->wm[level].plane_res_l[i] <<
3311
			temp |= p_wm->wm[level].plane_res_l[i] <<
3446
					PLANE_WM_LINES_SHIFT;
3312
					PLANE_WM_LINES_SHIFT;
3447
			temp |= p_wm->wm[level].plane_res_b[i];
3313
			temp |= p_wm->wm[level].plane_res_b[i];
3448
			if (p_wm->wm[level].plane_en[i])
3314
			if (p_wm->wm[level].plane_en[i])
3449
				temp |= PLANE_WM_EN;
3315
				temp |= PLANE_WM_EN;
3450
 
3316
 
3451
			r->plane[pipe][i][level] = temp;
3317
			r->plane[pipe][i][level] = temp;
3452
		}
3318
		}
3453
 
3319
 
3454
		temp = 0;
3320
		temp = 0;
3455
 
3321
 
3456
		temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
3322
		temp |= p_wm->wm[level].plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3457
		temp |= p_wm->wm[level].cursor_res_b;
3323
		temp |= p_wm->wm[level].plane_res_b[PLANE_CURSOR];
3458
 
3324
 
3459
		if (p_wm->wm[level].cursor_en)
3325
		if (p_wm->wm[level].plane_en[PLANE_CURSOR])
3460
			temp |= PLANE_WM_EN;
3326
			temp |= PLANE_WM_EN;
3461
 
3327
 
3462
		r->cursor[pipe][level] = temp;
3328
		r->plane[pipe][PLANE_CURSOR][level] = temp;
3463
 
3329
 
3464
	}
3330
	}
3465
 
3331
 
3466
	/* transition WMs */
3332
	/* transition WMs */
3467
	for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3333
	for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3468
		temp = 0;
3334
		temp = 0;
3469
		temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3335
		temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3470
		temp |= p_wm->trans_wm.plane_res_b[i];
3336
		temp |= p_wm->trans_wm.plane_res_b[i];
3471
		if (p_wm->trans_wm.plane_en[i])
3337
		if (p_wm->trans_wm.plane_en[i])
3472
			temp |= PLANE_WM_EN;
3338
			temp |= PLANE_WM_EN;
3473
 
3339
 
3474
		r->plane_trans[pipe][i] = temp;
3340
		r->plane_trans[pipe][i] = temp;
3475
	}
3341
	}
3476
 
3342
 
3477
	temp = 0;
3343
	temp = 0;
3478
	temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
3344
	temp |= p_wm->trans_wm.plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3479
	temp |= p_wm->trans_wm.cursor_res_b;
3345
	temp |= p_wm->trans_wm.plane_res_b[PLANE_CURSOR];
3480
	if (p_wm->trans_wm.cursor_en)
3346
	if (p_wm->trans_wm.plane_en[PLANE_CURSOR])
3481
		temp |= PLANE_WM_EN;
3347
		temp |= PLANE_WM_EN;
3482
 
3348
 
3483
	r->cursor_trans[pipe] = temp;
3349
	r->plane_trans[pipe][PLANE_CURSOR] = temp;
3484
 
3350
 
3485
	r->wm_linetime[pipe] = p_wm->linetime;
3351
	r->wm_linetime[pipe] = p_wm->linetime;
3486
}
3352
}
3487
 
3353
 
3488
static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3354
static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3489
				const struct skl_ddb_entry *entry)
3355
				const struct skl_ddb_entry *entry)
3490
{
3356
{
3491
	if (entry->end)
3357
	if (entry->end)
3492
		I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3358
		I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3493
	else
3359
	else
3494
		I915_WRITE(reg, 0);
3360
		I915_WRITE(reg, 0);
3495
}
3361
}
3496
 
3362
 
3497
static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3363
static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3498
				const struct skl_wm_values *new)
3364
				const struct skl_wm_values *new)
3499
{
3365
{
3500
	struct drm_device *dev = dev_priv->dev;
3366
	struct drm_device *dev = dev_priv->dev;
3501
	struct intel_crtc *crtc;
3367
	struct intel_crtc *crtc;
3502
 
3368
 
3503
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3369
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3504
		int i, level, max_level = ilk_wm_max_level(dev);
3370
		int i, level, max_level = ilk_wm_max_level(dev);
3505
		enum pipe pipe = crtc->pipe;
3371
		enum pipe pipe = crtc->pipe;
3506
 
3372
 
3507
		if (!new->dirty[pipe])
3373
		if (!new->dirty[pipe])
3508
			continue;
3374
			continue;
3509
 
3375
 
3510
		I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3376
		I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3511
 
3377
 
3512
		for (level = 0; level <= max_level; level++) {
3378
		for (level = 0; level <= max_level; level++) {
3513
			for (i = 0; i < intel_num_planes(crtc); i++)
3379
			for (i = 0; i < intel_num_planes(crtc); i++)
3514
				I915_WRITE(PLANE_WM(pipe, i, level),
3380
				I915_WRITE(PLANE_WM(pipe, i, level),
3515
					   new->plane[pipe][i][level]);
3381
					   new->plane[pipe][i][level]);
3516
			I915_WRITE(CUR_WM(pipe, level),
3382
			I915_WRITE(CUR_WM(pipe, level),
3517
				   new->cursor[pipe][level]);
3383
				   new->plane[pipe][PLANE_CURSOR][level]);
3518
		}
3384
		}
3519
		for (i = 0; i < intel_num_planes(crtc); i++)
3385
		for (i = 0; i < intel_num_planes(crtc); i++)
3520
			I915_WRITE(PLANE_WM_TRANS(pipe, i),
3386
			I915_WRITE(PLANE_WM_TRANS(pipe, i),
3521
				   new->plane_trans[pipe][i]);
3387
				   new->plane_trans[pipe][i]);
3522
		I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3388
		I915_WRITE(CUR_WM_TRANS(pipe),
-
 
3389
			   new->plane_trans[pipe][PLANE_CURSOR]);
3523
 
3390
 
3524
		for (i = 0; i < intel_num_planes(crtc); i++)
3391
		for (i = 0; i < intel_num_planes(crtc); i++) {
3525
			skl_ddb_entry_write(dev_priv,
3392
			skl_ddb_entry_write(dev_priv,
3526
					    PLANE_BUF_CFG(pipe, i),
3393
					    PLANE_BUF_CFG(pipe, i),
3527
					    &new->ddb.plane[pipe][i]);
3394
					    &new->ddb.plane[pipe][i]);
-
 
3395
			skl_ddb_entry_write(dev_priv,
-
 
3396
					    PLANE_NV12_BUF_CFG(pipe, i),
-
 
3397
					    &new->ddb.y_plane[pipe][i]);
-
 
3398
		}
3528
 
3399
 
3529
		skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3400
		skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3530
				    &new->ddb.cursor[pipe]);
3401
				    &new->ddb.plane[pipe][PLANE_CURSOR]);
3531
	}
3402
	}
3532
}
3403
}
3533
 
3404
 
3534
/*
3405
/*
3535
 * When setting up a new DDB allocation arrangement, we need to correctly
3406
 * When setting up a new DDB allocation arrangement, we need to correctly
3536
 * sequence the times at which the new allocations for the pipes are taken into
3407
 * sequence the times at which the new allocations for the pipes are taken into
3537
 * account or we'll have pipes fetching from space previously allocated to
3408
 * account or we'll have pipes fetching from space previously allocated to
3538
 * another pipe.
3409
 * another pipe.
3539
 *
3410
 *
3540
 * Roughly the sequence looks like:
3411
 * Roughly the sequence looks like:
3541
 *  1. re-allocate the pipe(s) with the allocation being reduced and not
3412
 *  1. re-allocate the pipe(s) with the allocation being reduced and not
3542
 *     overlapping with a previous light-up pipe (another way to put it is:
3413
 *     overlapping with a previous light-up pipe (another way to put it is:
3543
 *     pipes with their new allocation strickly included into their old ones).
3414
 *     pipes with their new allocation strickly included into their old ones).
3544
 *  2. re-allocate the other pipes that get their allocation reduced
3415
 *  2. re-allocate the other pipes that get their allocation reduced
3545
 *  3. allocate the pipes having their allocation increased
3416
 *  3. allocate the pipes having their allocation increased
3546
 *
3417
 *
3547
 * Steps 1. and 2. are here to take care of the following case:
3418
 * Steps 1. and 2. are here to take care of the following case:
3548
 * - Initially DDB looks like this:
3419
 * - Initially DDB looks like this:
3549
 *     |   B    |   C    |
3420
 *     |   B    |   C    |
3550
 * - enable pipe A.
3421
 * - enable pipe A.
3551
 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3422
 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3552
 *   allocation
3423
 *   allocation
3553
 *     |  A  |  B  |  C  |
3424
 *     |  A  |  B  |  C  |
3554
 *
3425
 *
3555
 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3426
 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3556
 */
3427
 */
3557
 
3428
 
3558
static void
3429
static void
3559
skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
3430
skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
3560
{
3431
{
3561
	struct drm_device *dev = dev_priv->dev;
-
 
3562
	int plane;
3432
	int plane;
3563
 
3433
 
3564
	DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3434
	DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3565
 
3435
 
3566
	for_each_plane(pipe, plane) {
3436
	for_each_plane(dev_priv, pipe, plane) {
3567
		I915_WRITE(PLANE_SURF(pipe, plane),
3437
		I915_WRITE(PLANE_SURF(pipe, plane),
3568
			   I915_READ(PLANE_SURF(pipe, plane)));
3438
			   I915_READ(PLANE_SURF(pipe, plane)));
3569
	}
3439
	}
3570
	I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3440
	I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3571
}
3441
}
3572
 
3442
 
3573
static bool
3443
static bool
3574
skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3444
skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3575
			    const struct skl_ddb_allocation *new,
3445
			    const struct skl_ddb_allocation *new,
3576
			    enum pipe pipe)
3446
			    enum pipe pipe)
3577
{
3447
{
3578
	uint16_t old_size, new_size;
3448
	uint16_t old_size, new_size;
3579
 
3449
 
3580
	old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3450
	old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3581
	new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3451
	new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3582
 
3452
 
3583
	return old_size != new_size &&
3453
	return old_size != new_size &&
3584
	       new->pipe[pipe].start >= old->pipe[pipe].start &&
3454
	       new->pipe[pipe].start >= old->pipe[pipe].start &&
3585
	       new->pipe[pipe].end <= old->pipe[pipe].end;
3455
	       new->pipe[pipe].end <= old->pipe[pipe].end;
3586
}
3456
}
3587
 
3457
 
3588
static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3458
static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3589
				struct skl_wm_values *new_values)
3459
				struct skl_wm_values *new_values)
3590
{
3460
{
3591
	struct drm_device *dev = dev_priv->dev;
3461
	struct drm_device *dev = dev_priv->dev;
3592
	struct skl_ddb_allocation *cur_ddb, *new_ddb;
3462
	struct skl_ddb_allocation *cur_ddb, *new_ddb;
3593
	bool reallocated[I915_MAX_PIPES] = {false, false, false};
3463
	bool reallocated[I915_MAX_PIPES] = {};
3594
	struct intel_crtc *crtc;
3464
	struct intel_crtc *crtc;
3595
	enum pipe pipe;
3465
	enum pipe pipe;
3596
 
3466
 
3597
	new_ddb = &new_values->ddb;
3467
	new_ddb = &new_values->ddb;
3598
	cur_ddb = &dev_priv->wm.skl_hw.ddb;
3468
	cur_ddb = &dev_priv->wm.skl_hw.ddb;
3599
 
3469
 
3600
	/*
3470
	/*
3601
	 * First pass: flush the pipes with the new allocation contained into
3471
	 * First pass: flush the pipes with the new allocation contained into
3602
	 * the old space.
3472
	 * the old space.
3603
	 *
3473
	 *
3604
	 * We'll wait for the vblank on those pipes to ensure we can safely
3474
	 * We'll wait for the vblank on those pipes to ensure we can safely
3605
	 * re-allocate the freed space without this pipe fetching from it.
3475
	 * re-allocate the freed space without this pipe fetching from it.
3606
	 */
3476
	 */
3607
	for_each_intel_crtc(dev, crtc) {
3477
	for_each_intel_crtc(dev, crtc) {
3608
		if (!crtc->active)
3478
		if (!crtc->active)
3609
			continue;
3479
			continue;
3610
 
3480
 
3611
		pipe = crtc->pipe;
3481
		pipe = crtc->pipe;
3612
 
3482
 
3613
		if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3483
		if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3614
			continue;
3484
			continue;
3615
 
3485
 
3616
		skl_wm_flush_pipe(dev_priv, pipe, 1);
3486
		skl_wm_flush_pipe(dev_priv, pipe, 1);
3617
		intel_wait_for_vblank(dev, pipe);
3487
		intel_wait_for_vblank(dev, pipe);
3618
 
3488
 
3619
		reallocated[pipe] = true;
3489
		reallocated[pipe] = true;
3620
	}
3490
	}
3621
 
3491
 
3622
 
3492
 
3623
	/*
3493
	/*
3624
	 * Second pass: flush the pipes that are having their allocation
3494
	 * Second pass: flush the pipes that are having their allocation
3625
	 * reduced, but overlapping with a previous allocation.
3495
	 * reduced, but overlapping with a previous allocation.
3626
	 *
3496
	 *
3627
	 * Here as well we need to wait for the vblank to make sure the freed
3497
	 * Here as well we need to wait for the vblank to make sure the freed
3628
	 * space is not used anymore.
3498
	 * space is not used anymore.
3629
	 */
3499
	 */
3630
	for_each_intel_crtc(dev, crtc) {
3500
	for_each_intel_crtc(dev, crtc) {
3631
		if (!crtc->active)
3501
		if (!crtc->active)
3632
			continue;
3502
			continue;
3633
 
3503
 
3634
		pipe = crtc->pipe;
3504
		pipe = crtc->pipe;
3635
 
3505
 
3636
		if (reallocated[pipe])
3506
		if (reallocated[pipe])
3637
			continue;
3507
			continue;
3638
 
3508
 
3639
		if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3509
		if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3640
		    skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
3510
		    skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
3641
			skl_wm_flush_pipe(dev_priv, pipe, 2);
3511
			skl_wm_flush_pipe(dev_priv, pipe, 2);
3642
			intel_wait_for_vblank(dev, pipe);
3512
			intel_wait_for_vblank(dev, pipe);
3643
		}
-
 
3644
 
-
 
3645
		reallocated[pipe] = true;
3513
			reallocated[pipe] = true;
3646
	}
3514
		}
-
 
3515
	}
3647
 
3516
 
3648
	/*
3517
	/*
3649
	 * Third pass: flush the pipes that got more space allocated.
3518
	 * Third pass: flush the pipes that got more space allocated.
3650
	 *
3519
	 *
3651
	 * We don't need to actively wait for the update here, next vblank
3520
	 * We don't need to actively wait for the update here, next vblank
3652
	 * will just get more DDB space with the correct WM values.
3521
	 * will just get more DDB space with the correct WM values.
3653
	 */
3522
	 */
3654
	for_each_intel_crtc(dev, crtc) {
3523
	for_each_intel_crtc(dev, crtc) {
3655
		if (!crtc->active)
3524
		if (!crtc->active)
3656
			continue;
3525
			continue;
3657
 
3526
 
3658
		pipe = crtc->pipe;
3527
		pipe = crtc->pipe;
3659
 
3528
 
3660
		/*
3529
		/*
3661
		 * At this point, only the pipes more space than before are
3530
		 * At this point, only the pipes more space than before are
3662
		 * left to re-allocate.
3531
		 * left to re-allocate.
3663
		 */
3532
		 */
3664
		if (reallocated[pipe])
3533
		if (reallocated[pipe])
3665
			continue;
3534
			continue;
3666
 
3535
 
3667
		skl_wm_flush_pipe(dev_priv, pipe, 3);
3536
		skl_wm_flush_pipe(dev_priv, pipe, 3);
3668
	}
3537
	}
3669
}
3538
}
3670
 
3539
 
3671
static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3540
static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3672
			       struct skl_pipe_wm_parameters *params,
3541
			       struct skl_pipe_wm_parameters *params,
3673
			       struct intel_wm_config *config,
3542
			       struct intel_wm_config *config,
3674
			       struct skl_ddb_allocation *ddb, /* out */
3543
			       struct skl_ddb_allocation *ddb, /* out */
3675
			       struct skl_pipe_wm *pipe_wm /* out */)
3544
			       struct skl_pipe_wm *pipe_wm /* out */)
3676
{
3545
{
3677
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3546
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3678
 
3547
 
3679
	skl_compute_wm_pipe_parameters(crtc, params);
3548
	skl_compute_wm_pipe_parameters(crtc, params);
3680
	skl_allocate_pipe_ddb(crtc, config, params, ddb);
3549
	skl_allocate_pipe_ddb(crtc, config, params, ddb);
3681
	skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3550
	skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3682
 
3551
 
3683
	if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3552
	if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3684
		return false;
3553
		return false;
3685
 
3554
 
3686
	intel_crtc->wm.skl_active = *pipe_wm;
3555
	intel_crtc->wm.skl_active = *pipe_wm;
-
 
3556
 
3687
	return true;
3557
	return true;
3688
}
3558
}
3689
 
3559
 
3690
static void skl_update_other_pipe_wm(struct drm_device *dev,
3560
static void skl_update_other_pipe_wm(struct drm_device *dev,
3691
				     struct drm_crtc *crtc,
3561
				     struct drm_crtc *crtc,
3692
				     struct intel_wm_config *config,
3562
				     struct intel_wm_config *config,
3693
				     struct skl_wm_values *r)
3563
				     struct skl_wm_values *r)
3694
{
3564
{
3695
	struct intel_crtc *intel_crtc;
3565
	struct intel_crtc *intel_crtc;
3696
	struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3566
	struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3697
 
3567
 
3698
	/*
3568
	/*
3699
	 * If the WM update hasn't changed the allocation for this_crtc (the
3569
	 * If the WM update hasn't changed the allocation for this_crtc (the
3700
	 * crtc we are currently computing the new WM values for), other
3570
	 * crtc we are currently computing the new WM values for), other
3701
	 * enabled crtcs will keep the same allocation and we don't need to
3571
	 * enabled crtcs will keep the same allocation and we don't need to
3702
	 * recompute anything for them.
3572
	 * recompute anything for them.
3703
	 */
3573
	 */
3704
	if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3574
	if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3705
		return;
3575
		return;
3706
 
3576
 
3707
	/*
3577
	/*
3708
	 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3578
	 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3709
	 * other active pipes need new DDB allocation and WM values.
3579
	 * other active pipes need new DDB allocation and WM values.
3710
	 */
3580
	 */
3711
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3581
	list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3712
				base.head) {
3582
				base.head) {
3713
		struct skl_pipe_wm_parameters params = {};
3583
		struct skl_pipe_wm_parameters params = {};
3714
		struct skl_pipe_wm pipe_wm = {};
3584
		struct skl_pipe_wm pipe_wm = {};
3715
		bool wm_changed;
3585
		bool wm_changed;
3716
 
3586
 
3717
		if (this_crtc->pipe == intel_crtc->pipe)
3587
		if (this_crtc->pipe == intel_crtc->pipe)
3718
			continue;
3588
			continue;
3719
 
3589
 
3720
		if (!intel_crtc->active)
3590
		if (!intel_crtc->active)
3721
			continue;
3591
			continue;
3722
 
3592
 
3723
		wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3593
		wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3724
						¶ms, config,
3594
						¶ms, config,
3725
						&r->ddb, &pipe_wm);
3595
						&r->ddb, &pipe_wm);
3726
 
3596
 
3727
		/*
3597
		/*
3728
		 * If we end up re-computing the other pipe WM values, it's
3598
		 * If we end up re-computing the other pipe WM values, it's
3729
		 * because it was really needed, so we expect the WM values to
3599
		 * because it was really needed, so we expect the WM values to
3730
		 * be different.
3600
		 * be different.
3731
	 */
3601
		 */
3732
		WARN_ON(!wm_changed);
3602
		WARN_ON(!wm_changed);
3733
 
3603
 
3734
		skl_compute_wm_results(dev, ¶ms, &pipe_wm, r, intel_crtc);
3604
		skl_compute_wm_results(dev, ¶ms, &pipe_wm, r, intel_crtc);
3735
		r->dirty[intel_crtc->pipe] = true;
3605
		r->dirty[intel_crtc->pipe] = true;
3736
	}
3606
	}
3737
}
3607
}
-
 
3608
 
-
 
3609
static void skl_clear_wm(struct skl_wm_values *watermarks, enum pipe pipe)
-
 
3610
{
-
 
3611
	watermarks->wm_linetime[pipe] = 0;
-
 
3612
	memset(watermarks->plane[pipe], 0,
-
 
3613
	       sizeof(uint32_t) * 8 * I915_MAX_PLANES);
-
 
3614
	memset(watermarks->plane_trans[pipe],
-
 
3615
	       0, sizeof(uint32_t) * I915_MAX_PLANES);
-
 
3616
	watermarks->plane_trans[pipe][PLANE_CURSOR] = 0;
-
 
3617
 
-
 
3618
	/* Clear ddb entries for pipe */
-
 
3619
	memset(&watermarks->ddb.pipe[pipe], 0, sizeof(struct skl_ddb_entry));
-
 
3620
	memset(&watermarks->ddb.plane[pipe], 0,
-
 
3621
	       sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
-
 
3622
	memset(&watermarks->ddb.y_plane[pipe], 0,
-
 
3623
	       sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
-
 
3624
	memset(&watermarks->ddb.plane[pipe][PLANE_CURSOR], 0,
-
 
3625
	       sizeof(struct skl_ddb_entry));
-
 
3626
 
-
 
3627
}
3738
 
3628
 
3739
static void skl_update_wm(struct drm_crtc *crtc)
3629
static void skl_update_wm(struct drm_crtc *crtc)
3740
{
3630
{
3741
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3631
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3742
	struct drm_device *dev = crtc->dev;
3632
	struct drm_device *dev = crtc->dev;
3743
	struct drm_i915_private *dev_priv = dev->dev_private;
3633
	struct drm_i915_private *dev_priv = dev->dev_private;
3744
	struct skl_pipe_wm_parameters params = {};
3634
	struct skl_pipe_wm_parameters params = {};
3745
	struct skl_wm_values *results = &dev_priv->wm.skl_results;
3635
	struct skl_wm_values *results = &dev_priv->wm.skl_results;
3746
	struct skl_pipe_wm pipe_wm = {};
3636
	struct skl_pipe_wm pipe_wm = {};
3747
	struct intel_wm_config config = {};
3637
	struct intel_wm_config config = {};
-
 
3638
 
-
 
3639
 
3748
 
3640
	/* Clear all dirty flags */
-
 
3641
	memset(results->dirty, 0, sizeof(bool) * I915_MAX_PIPES);
-
 
3642
 
3749
	memset(results, 0, sizeof(*results));
3643
	skl_clear_wm(results, intel_crtc->pipe);
3750
 
3644
 
3751
	skl_compute_wm_global_parameters(dev, &config);
3645
	skl_compute_wm_global_parameters(dev, &config);
3752
 
3646
 
3753
	if (!skl_update_pipe_wm(crtc, ¶ms, &config,
3647
	if (!skl_update_pipe_wm(crtc, ¶ms, &config,
3754
				&results->ddb, &pipe_wm))
3648
				&results->ddb, &pipe_wm))
3755
		return;
3649
		return;
3756
 
3650
 
3757
	skl_compute_wm_results(dev, ¶ms, &pipe_wm, results, intel_crtc);
3651
	skl_compute_wm_results(dev, ¶ms, &pipe_wm, results, intel_crtc);
3758
	results->dirty[intel_crtc->pipe] = true;
3652
	results->dirty[intel_crtc->pipe] = true;
3759
 
3653
 
3760
	skl_update_other_pipe_wm(dev, crtc, &config, results);
3654
	skl_update_other_pipe_wm(dev, crtc, &config, results);
3761
	skl_write_wm_values(dev_priv, results);
3655
	skl_write_wm_values(dev_priv, results);
3762
	skl_flush_wm_values(dev_priv, results);
3656
	skl_flush_wm_values(dev_priv, results);
3763
 
3657
 
3764
	/* store the new configuration */
3658
	/* store the new configuration */
3765
	dev_priv->wm.skl_hw = *results;
3659
	dev_priv->wm.skl_hw = *results;
3766
}
3660
}
3767
 
3661
 
3768
static void
3662
static void
3769
skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3663
skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3770
		     uint32_t sprite_width, uint32_t sprite_height,
3664
		     uint32_t sprite_width, uint32_t sprite_height,
3771
		     int pixel_size, bool enabled, bool scaled)
3665
		     int pixel_size, bool enabled, bool scaled)
3772
{
3666
{
3773
	struct intel_plane *intel_plane = to_intel_plane(plane);
3667
	struct intel_plane *intel_plane = to_intel_plane(plane);
-
 
3668
	struct drm_framebuffer *fb = plane->state->fb;
3774
 
3669
 
3775
	intel_plane->wm.enabled = enabled;
3670
	intel_plane->wm.enabled = enabled;
3776
	intel_plane->wm.scaled = scaled;
3671
	intel_plane->wm.scaled = scaled;
3777
	intel_plane->wm.horiz_pixels = sprite_width;
3672
	intel_plane->wm.horiz_pixels = sprite_width;
3778
	intel_plane->wm.vert_pixels = sprite_height;
3673
	intel_plane->wm.vert_pixels = sprite_height;
-
 
3674
	intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
-
 
3675
 
-
 
3676
	/* For planar: Bpp is for UV plane, y_Bpp is for Y plane */
3779
	intel_plane->wm.bytes_per_pixel = pixel_size;
3677
	intel_plane->wm.bytes_per_pixel =
-
 
3678
		(fb && fb->pixel_format == DRM_FORMAT_NV12) ?
-
 
3679
		drm_format_plane_cpp(plane->state->fb->pixel_format, 1) : pixel_size;
-
 
3680
	intel_plane->wm.y_bytes_per_pixel =
-
 
3681
		(fb && fb->pixel_format == DRM_FORMAT_NV12) ?
-
 
3682
		drm_format_plane_cpp(plane->state->fb->pixel_format, 0) : 0;
-
 
3683
 
-
 
3684
	/*
-
 
3685
	 * Framebuffer can be NULL on plane disable, but it does not
-
 
3686
	 * matter for watermarks if we assume no tiling in that case.
-
 
3687
	 */
-
 
3688
	if (fb)
-
 
3689
		intel_plane->wm.tiling = fb->modifier[0];
-
 
3690
	intel_plane->wm.rotation = plane->state->rotation;
3780
 
3691
 
3781
	skl_update_wm(crtc);
3692
	skl_update_wm(crtc);
3782
}
3693
}
3783
 
3694
 
3784
static void ilk_update_wm(struct drm_crtc *crtc)
3695
static void ilk_update_wm(struct drm_crtc *crtc)
3785
{
3696
{
3786
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3697
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
-
 
3698
	struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3787
	struct drm_device *dev = crtc->dev;
3699
	struct drm_device *dev = crtc->dev;
3788
	struct drm_i915_private *dev_priv = dev->dev_private;
3700
	struct drm_i915_private *dev_priv = dev->dev_private;
3789
	struct ilk_wm_maximums max;
3701
	struct ilk_wm_maximums max;
3790
	struct ilk_pipe_wm_parameters params = {};
-
 
3791
	struct ilk_wm_values results = {};
3702
	struct ilk_wm_values results = {};
3792
	enum intel_ddb_partitioning partitioning;
3703
	enum intel_ddb_partitioning partitioning;
3793
	struct intel_pipe_wm pipe_wm = {};
3704
	struct intel_pipe_wm pipe_wm = {};
3794
	struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
3705
	struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
3795
	struct intel_wm_config config = {};
3706
	struct intel_wm_config config = {};
3796
 
3707
 
3797
	ilk_compute_wm_parameters(crtc, ¶ms);
3708
	WARN_ON(cstate->base.active != intel_crtc->active);
3798
 
3709
 
3799
	intel_compute_pipe_wm(crtc, ¶ms, &pipe_wm);
3710
	intel_compute_pipe_wm(cstate, &pipe_wm);
3800
 
3711
 
3801
	if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3712
	if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3802
		return;
3713
		return;
3803
 
3714
 
3804
	intel_crtc->wm.active = pipe_wm;
3715
	intel_crtc->wm.active = pipe_wm;
3805
 
3716
 
3806
	ilk_compute_wm_config(dev, &config);
3717
	ilk_compute_wm_config(dev, &config);
3807
 
3718
 
3808
	ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
3719
	ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
3809
	ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
3720
	ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
3810
 
3721
 
3811
	/* 5/6 split only in single pipe config on IVB+ */
3722
	/* 5/6 split only in single pipe config on IVB+ */
3812
	if (INTEL_INFO(dev)->gen >= 7 &&
3723
	if (INTEL_INFO(dev)->gen >= 7 &&
3813
	    config.num_pipes_active == 1 && config.sprites_enabled) {
3724
	    config.num_pipes_active == 1 && config.sprites_enabled) {
3814
		ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
3725
		ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
3815
		ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
3726
		ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
3816
 
3727
 
3817
		best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
3728
		best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
3818
	} else {
3729
	} else {
3819
		best_lp_wm = &lp_wm_1_2;
3730
		best_lp_wm = &lp_wm_1_2;
3820
	}
3731
	}
3821
 
3732
 
3822
	partitioning = (best_lp_wm == &lp_wm_1_2) ?
3733
	partitioning = (best_lp_wm == &lp_wm_1_2) ?
3823
		       INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
3734
		       INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
3824
 
3735
 
3825
	ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
3736
	ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
3826
 
3737
 
3827
	ilk_write_wm_values(dev_priv, &results);
3738
	ilk_write_wm_values(dev_priv, &results);
3828
}
3739
}
3829
 
3740
 
3830
static void
3741
static void
3831
ilk_update_sprite_wm(struct drm_plane *plane,
3742
ilk_update_sprite_wm(struct drm_plane *plane,
3832
		     struct drm_crtc *crtc,
3743
		     struct drm_crtc *crtc,
3833
		     uint32_t sprite_width, uint32_t sprite_height,
3744
		     uint32_t sprite_width, uint32_t sprite_height,
3834
		     int pixel_size, bool enabled, bool scaled)
3745
		     int pixel_size, bool enabled, bool scaled)
3835
{
3746
{
3836
	struct drm_device *dev = plane->dev;
3747
	struct drm_device *dev = plane->dev;
3837
	struct intel_plane *intel_plane = to_intel_plane(plane);
3748
	struct intel_plane *intel_plane = to_intel_plane(plane);
3838
 
-
 
3839
	intel_plane->wm.enabled = enabled;
-
 
3840
	intel_plane->wm.scaled = scaled;
-
 
3841
	intel_plane->wm.horiz_pixels = sprite_width;
-
 
3842
	intel_plane->wm.vert_pixels = sprite_width;
-
 
3843
	intel_plane->wm.bytes_per_pixel = pixel_size;
-
 
3844
 
3749
 
3845
	/*
3750
	/*
3846
	 * IVB workaround: must disable low power watermarks for at least
3751
	 * IVB workaround: must disable low power watermarks for at least
3847
	 * one frame before enabling scaling.  LP watermarks can be re-enabled
3752
	 * one frame before enabling scaling.  LP watermarks can be re-enabled
3848
	 * when scaling is disabled.
3753
	 * when scaling is disabled.
3849
	 *
3754
	 *
3850
	 * WaCxSRDisabledForSpriteScaling:ivb
3755
	 * WaCxSRDisabledForSpriteScaling:ivb
3851
	 */
3756
	 */
3852
	if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3757
	if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3853
		intel_wait_for_vblank(dev, intel_plane->pipe);
3758
		intel_wait_for_vblank(dev, intel_plane->pipe);
3854
 
3759
 
3855
	ilk_update_wm(crtc);
3760
	ilk_update_wm(crtc);
3856
}
3761
}
3857
 
3762
 
3858
static void skl_pipe_wm_active_state(uint32_t val,
3763
static void skl_pipe_wm_active_state(uint32_t val,
3859
				     struct skl_pipe_wm *active,
3764
				     struct skl_pipe_wm *active,
3860
				     bool is_transwm,
3765
				     bool is_transwm,
3861
				     bool is_cursor,
3766
				     bool is_cursor,
3862
				     int i,
3767
				     int i,
3863
				     int level)
3768
				     int level)
3864
{
3769
{
3865
	bool is_enabled = (val & PLANE_WM_EN) != 0;
3770
	bool is_enabled = (val & PLANE_WM_EN) != 0;
3866
 
3771
 
3867
	if (!is_transwm) {
3772
	if (!is_transwm) {
3868
		if (!is_cursor) {
3773
		if (!is_cursor) {
3869
			active->wm[level].plane_en[i] = is_enabled;
3774
			active->wm[level].plane_en[i] = is_enabled;
3870
			active->wm[level].plane_res_b[i] =
3775
			active->wm[level].plane_res_b[i] =
3871
					val & PLANE_WM_BLOCKS_MASK;
3776
					val & PLANE_WM_BLOCKS_MASK;
3872
			active->wm[level].plane_res_l[i] =
3777
			active->wm[level].plane_res_l[i] =
3873
					(val >> PLANE_WM_LINES_SHIFT) &
3778
					(val >> PLANE_WM_LINES_SHIFT) &
3874
						PLANE_WM_LINES_MASK;
3779
						PLANE_WM_LINES_MASK;
3875
		} else {
3780
		} else {
3876
			active->wm[level].cursor_en = is_enabled;
3781
			active->wm[level].plane_en[PLANE_CURSOR] = is_enabled;
3877
			active->wm[level].cursor_res_b =
3782
			active->wm[level].plane_res_b[PLANE_CURSOR] =
3878
					val & PLANE_WM_BLOCKS_MASK;
3783
					val & PLANE_WM_BLOCKS_MASK;
3879
			active->wm[level].cursor_res_l =
3784
			active->wm[level].plane_res_l[PLANE_CURSOR] =
3880
					(val >> PLANE_WM_LINES_SHIFT) &
3785
					(val >> PLANE_WM_LINES_SHIFT) &
3881
						PLANE_WM_LINES_MASK;
3786
						PLANE_WM_LINES_MASK;
3882
		}
3787
		}
3883
	} else {
3788
	} else {
3884
		if (!is_cursor) {
3789
		if (!is_cursor) {
3885
			active->trans_wm.plane_en[i] = is_enabled;
3790
			active->trans_wm.plane_en[i] = is_enabled;
3886
			active->trans_wm.plane_res_b[i] =
3791
			active->trans_wm.plane_res_b[i] =
3887
					val & PLANE_WM_BLOCKS_MASK;
3792
					val & PLANE_WM_BLOCKS_MASK;
3888
			active->trans_wm.plane_res_l[i] =
3793
			active->trans_wm.plane_res_l[i] =
3889
					(val >> PLANE_WM_LINES_SHIFT) &
3794
					(val >> PLANE_WM_LINES_SHIFT) &
3890
						PLANE_WM_LINES_MASK;
3795
						PLANE_WM_LINES_MASK;
3891
		} else {
3796
		} else {
3892
			active->trans_wm.cursor_en = is_enabled;
3797
			active->trans_wm.plane_en[PLANE_CURSOR] = is_enabled;
3893
			active->trans_wm.cursor_res_b =
3798
			active->trans_wm.plane_res_b[PLANE_CURSOR] =
3894
					val & PLANE_WM_BLOCKS_MASK;
3799
					val & PLANE_WM_BLOCKS_MASK;
3895
			active->trans_wm.cursor_res_l =
3800
			active->trans_wm.plane_res_l[PLANE_CURSOR] =
3896
					(val >> PLANE_WM_LINES_SHIFT) &
3801
					(val >> PLANE_WM_LINES_SHIFT) &
3897
						PLANE_WM_LINES_MASK;
3802
						PLANE_WM_LINES_MASK;
3898
		}
3803
		}
3899
	}
3804
	}
3900
}
3805
}
3901
 
3806
 
3902
static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3807
static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3903
{
3808
{
3904
	struct drm_device *dev = crtc->dev;
3809
	struct drm_device *dev = crtc->dev;
3905
	struct drm_i915_private *dev_priv = dev->dev_private;
3810
	struct drm_i915_private *dev_priv = dev->dev_private;
3906
	struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3811
	struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3907
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3812
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3908
	struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3813
	struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3909
	enum pipe pipe = intel_crtc->pipe;
3814
	enum pipe pipe = intel_crtc->pipe;
3910
	int level, i, max_level;
3815
	int level, i, max_level;
3911
	uint32_t temp;
3816
	uint32_t temp;
3912
 
3817
 
3913
	max_level = ilk_wm_max_level(dev);
3818
	max_level = ilk_wm_max_level(dev);
3914
 
3819
 
3915
	hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3820
	hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3916
 
3821
 
3917
	for (level = 0; level <= max_level; level++) {
3822
	for (level = 0; level <= max_level; level++) {
3918
		for (i = 0; i < intel_num_planes(intel_crtc); i++)
3823
		for (i = 0; i < intel_num_planes(intel_crtc); i++)
3919
			hw->plane[pipe][i][level] =
3824
			hw->plane[pipe][i][level] =
3920
					I915_READ(PLANE_WM(pipe, i, level));
3825
					I915_READ(PLANE_WM(pipe, i, level));
3921
		hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3826
		hw->plane[pipe][PLANE_CURSOR][level] = I915_READ(CUR_WM(pipe, level));
3922
	}
3827
	}
3923
 
3828
 
3924
	for (i = 0; i < intel_num_planes(intel_crtc); i++)
3829
	for (i = 0; i < intel_num_planes(intel_crtc); i++)
3925
		hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3830
		hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3926
	hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3831
	hw->plane_trans[pipe][PLANE_CURSOR] = I915_READ(CUR_WM_TRANS(pipe));
3927
 
3832
 
3928
	if (!intel_crtc_active(crtc))
3833
	if (!intel_crtc->active)
3929
		return;
3834
		return;
3930
 
3835
 
3931
	hw->dirty[pipe] = true;
3836
	hw->dirty[pipe] = true;
3932
 
3837
 
3933
	active->linetime = hw->wm_linetime[pipe];
3838
	active->linetime = hw->wm_linetime[pipe];
3934
 
3839
 
3935
	for (level = 0; level <= max_level; level++) {
3840
	for (level = 0; level <= max_level; level++) {
3936
		for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3841
		for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3937
			temp = hw->plane[pipe][i][level];
3842
			temp = hw->plane[pipe][i][level];
3938
			skl_pipe_wm_active_state(temp, active, false,
3843
			skl_pipe_wm_active_state(temp, active, false,
3939
						false, i, level);
3844
						false, i, level);
3940
		}
3845
		}
3941
		temp = hw->cursor[pipe][level];
3846
		temp = hw->plane[pipe][PLANE_CURSOR][level];
3942
		skl_pipe_wm_active_state(temp, active, false, true, i, level);
3847
		skl_pipe_wm_active_state(temp, active, false, true, i, level);
3943
	}
3848
	}
3944
 
3849
 
3945
	for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3850
	for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3946
		temp = hw->plane_trans[pipe][i];
3851
		temp = hw->plane_trans[pipe][i];
3947
		skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3852
		skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3948
	}
3853
	}
3949
 
3854
 
3950
	temp = hw->cursor_trans[pipe];
3855
	temp = hw->plane_trans[pipe][PLANE_CURSOR];
3951
	skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3856
	skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3952
}
3857
}
3953
 
3858
 
3954
void skl_wm_get_hw_state(struct drm_device *dev)
3859
void skl_wm_get_hw_state(struct drm_device *dev)
3955
{
3860
{
3956
	struct drm_i915_private *dev_priv = dev->dev_private;
3861
	struct drm_i915_private *dev_priv = dev->dev_private;
3957
	struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
3862
	struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
3958
	struct drm_crtc *crtc;
3863
	struct drm_crtc *crtc;
3959
 
3864
 
3960
	skl_ddb_get_hw_state(dev_priv, ddb);
3865
	skl_ddb_get_hw_state(dev_priv, ddb);
3961
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3866
	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3962
		skl_pipe_wm_get_hw_state(crtc);
3867
		skl_pipe_wm_get_hw_state(crtc);
3963
}
3868
}
3964
 
3869
 
3965
static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3870
static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3966
{
3871
{
3967
	struct drm_device *dev = crtc->dev;
3872
	struct drm_device *dev = crtc->dev;
3968
	struct drm_i915_private *dev_priv = dev->dev_private;
3873
	struct drm_i915_private *dev_priv = dev->dev_private;
3969
	struct ilk_wm_values *hw = &dev_priv->wm.hw;
3874
	struct ilk_wm_values *hw = &dev_priv->wm.hw;
3970
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3875
	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3971
	struct intel_pipe_wm *active = &intel_crtc->wm.active;
3876
	struct intel_pipe_wm *active = &intel_crtc->wm.active;
3972
	enum pipe pipe = intel_crtc->pipe;
3877
	enum pipe pipe = intel_crtc->pipe;
3973
	static const unsigned int wm0_pipe_reg[] = {
3878
	static const unsigned int wm0_pipe_reg[] = {
3974
		[PIPE_A] = WM0_PIPEA_ILK,
3879
		[PIPE_A] = WM0_PIPEA_ILK,
3975
		[PIPE_B] = WM0_PIPEB_ILK,
3880
		[PIPE_B] = WM0_PIPEB_ILK,
3976
		[PIPE_C] = WM0_PIPEC_IVB,
3881
		[PIPE_C] = WM0_PIPEC_IVB,
3977
	};
3882
	};
3978
 
3883
 
3979
	hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
3884
	hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
3980
	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3885
	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3981
		hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3886
		hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3982
 
3887
 
3983
	active->pipe_enabled = intel_crtc_active(crtc);
3888
	active->pipe_enabled = intel_crtc->active;
3984
 
3889
 
3985
	if (active->pipe_enabled) {
3890
	if (active->pipe_enabled) {
3986
		u32 tmp = hw->wm_pipe[pipe];
3891
		u32 tmp = hw->wm_pipe[pipe];
3987
 
3892
 
3988
		/*
3893
		/*
3989
		 * For active pipes LP0 watermark is marked as
3894
		 * For active pipes LP0 watermark is marked as
3990
		 * enabled, and LP1+ watermaks as disabled since
3895
		 * enabled, and LP1+ watermaks as disabled since
3991
		 * we can't really reverse compute them in case
3896
		 * we can't really reverse compute them in case
3992
		 * multiple pipes are active.
3897
		 * multiple pipes are active.
3993
		 */
3898
		 */
3994
		active->wm[0].enable = true;
3899
		active->wm[0].enable = true;
3995
		active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3900
		active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3996
		active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3901
		active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3997
		active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3902
		active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3998
		active->linetime = hw->wm_linetime[pipe];
3903
		active->linetime = hw->wm_linetime[pipe];
3999
	} else {
3904
	} else {
4000
		int level, max_level = ilk_wm_max_level(dev);
3905
		int level, max_level = ilk_wm_max_level(dev);
4001
 
3906
 
4002
		/*
3907
		/*
4003
		 * For inactive pipes, all watermark levels
3908
		 * For inactive pipes, all watermark levels
4004
		 * should be marked as enabled but zeroed,
3909
		 * should be marked as enabled but zeroed,
4005
		 * which is what we'd compute them to.
3910
		 * which is what we'd compute them to.
4006
		 */
3911
		 */
4007
		for (level = 0; level <= max_level; level++)
3912
		for (level = 0; level <= max_level; level++)
4008
			active->wm[level].enable = true;
3913
			active->wm[level].enable = true;
4009
	}
3914
	}
4010
}
3915
}
-
 
3916
 
-
 
3917
#define _FW_WM(value, plane) \
-
 
3918
	(((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
-
 
3919
#define _FW_WM_VLV(value, plane) \
-
 
3920
	(((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
-
 
3921
 
-
 
3922
static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
-
 
3923
			       struct vlv_wm_values *wm)
-
 
3924
{
-
 
3925
	enum pipe pipe;
-
 
3926
	uint32_t tmp;
-
 
3927
 
-
 
3928
	for_each_pipe(dev_priv, pipe) {
-
 
3929
		tmp = I915_READ(VLV_DDL(pipe));
-
 
3930
 
-
 
3931
		wm->ddl[pipe].primary =
-
 
3932
			(tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
-
 
3933
		wm->ddl[pipe].cursor =
-
 
3934
			(tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
-
 
3935
		wm->ddl[pipe].sprite[0] =
-
 
3936
			(tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
-
 
3937
		wm->ddl[pipe].sprite[1] =
-
 
3938
			(tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
-
 
3939
	}
-
 
3940
 
-
 
3941
	tmp = I915_READ(DSPFW1);
-
 
3942
	wm->sr.plane = _FW_WM(tmp, SR);
-
 
3943
	wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
-
 
3944
	wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
-
 
3945
	wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
-
 
3946
 
-
 
3947
	tmp = I915_READ(DSPFW2);
-
 
3948
	wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
-
 
3949
	wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
-
 
3950
	wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
-
 
3951
 
-
 
3952
	tmp = I915_READ(DSPFW3);
-
 
3953
	wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
-
 
3954
 
-
 
3955
	if (IS_CHERRYVIEW(dev_priv)) {
-
 
3956
		tmp = I915_READ(DSPFW7_CHV);
-
 
3957
		wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
-
 
3958
		wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
-
 
3959
 
-
 
3960
		tmp = I915_READ(DSPFW8_CHV);
-
 
3961
		wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
-
 
3962
		wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
-
 
3963
 
-
 
3964
		tmp = I915_READ(DSPFW9_CHV);
-
 
3965
		wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
-
 
3966
		wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
-
 
3967
 
-
 
3968
		tmp = I915_READ(DSPHOWM);
-
 
3969
		wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
-
 
3970
		wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
-
 
3971
		wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
-
 
3972
		wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
-
 
3973
		wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
-
 
3974
		wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
-
 
3975
		wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
-
 
3976
		wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
-
 
3977
		wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
-
 
3978
		wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
-
 
3979
	} else {
-
 
3980
		tmp = I915_READ(DSPFW7);
-
 
3981
		wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
-
 
3982
		wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
-
 
3983
 
-
 
3984
		tmp = I915_READ(DSPHOWM);
-
 
3985
		wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
-
 
3986
		wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
-
 
3987
		wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
-
 
3988
		wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
-
 
3989
		wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
-
 
3990
		wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
-
 
3991
		wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
-
 
3992
	}
-
 
3993
}
-
 
3994
 
-
 
3995
#undef _FW_WM
-
 
3996
#undef _FW_WM_VLV
-
 
3997
 
-
 
3998
void vlv_wm_get_hw_state(struct drm_device *dev)
-
 
3999
{
-
 
4000
	struct drm_i915_private *dev_priv = to_i915(dev);
-
 
4001
	struct vlv_wm_values *wm = &dev_priv->wm.vlv;
-
 
4002
	struct intel_plane *plane;
-
 
4003
	enum pipe pipe;
-
 
4004
	u32 val;
-
 
4005
 
-
 
4006
	vlv_read_wm_values(dev_priv, wm);
-
 
4007
 
-
 
4008
	for_each_intel_plane(dev, plane) {
-
 
4009
		switch (plane->base.type) {
-
 
4010
			int sprite;
-
 
4011
		case DRM_PLANE_TYPE_CURSOR:
-
 
4012
			plane->wm.fifo_size = 63;
-
 
4013
			break;
-
 
4014
		case DRM_PLANE_TYPE_PRIMARY:
-
 
4015
			plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
-
 
4016
			break;
-
 
4017
		case DRM_PLANE_TYPE_OVERLAY:
-
 
4018
			sprite = plane->plane;
-
 
4019
			plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
-
 
4020
			break;
-
 
4021
		}
-
 
4022
	}
-
 
4023
 
-
 
4024
	wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
-
 
4025
	wm->level = VLV_WM_LEVEL_PM2;
-
 
4026
 
-
 
4027
	if (IS_CHERRYVIEW(dev_priv)) {
-
 
4028
		mutex_lock(&dev_priv->rps.hw_lock);
-
 
4029
 
-
 
4030
		val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
-
 
4031
		if (val & DSP_MAXFIFO_PM5_ENABLE)
-
 
4032
			wm->level = VLV_WM_LEVEL_PM5;
-
 
4033
 
-
 
4034
		/*
-
 
4035
		 * If DDR DVFS is disabled in the BIOS, Punit
-
 
4036
		 * will never ack the request. So if that happens
-
 
4037
		 * assume we don't have to enable/disable DDR DVFS
-
 
4038
		 * dynamically. To test that just set the REQ_ACK
-
 
4039
		 * bit to poke the Punit, but don't change the
-
 
4040
		 * HIGH/LOW bits so that we don't actually change
-
 
4041
		 * the current state.
-
 
4042
		 */
-
 
4043
		val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
-
 
4044
		val |= FORCE_DDR_FREQ_REQ_ACK;
-
 
4045
		vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
-
 
4046
 
-
 
4047
		if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
-
 
4048
			      FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
-
 
4049
			DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
-
 
4050
				      "assuming DDR DVFS is disabled\n");
-
 
4051
			dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
-
 
4052
		} else {
-
 
4053
			val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
-
 
4054
			if ((val & FORCE_DDR_HIGH_FREQ) == 0)
-
 
4055
				wm->level = VLV_WM_LEVEL_DDR_DVFS;
-
 
4056
		}
-
 
4057
 
-
 
4058
		mutex_unlock(&dev_priv->rps.hw_lock);
-
 
4059
	}
-
 
4060
 
-
 
4061
	for_each_pipe(dev_priv, pipe)
-
 
4062
		DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
-
 
4063
			      pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
-
 
4064
			      wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
-
 
4065
 
-
 
4066
	DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
-
 
4067
		      wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
-
 
4068
}
4011
 
4069
 
4012
void ilk_wm_get_hw_state(struct drm_device *dev)
4070
void ilk_wm_get_hw_state(struct drm_device *dev)
4013
{
4071
{
4014
	struct drm_i915_private *dev_priv = dev->dev_private;
4072
	struct drm_i915_private *dev_priv = dev->dev_private;
4015
	struct ilk_wm_values *hw = &dev_priv->wm.hw;
4073
	struct ilk_wm_values *hw = &dev_priv->wm.hw;
4016
	struct drm_crtc *crtc;
4074
	struct drm_crtc *crtc;
4017
 
4075
 
4018
	for_each_crtc(dev, crtc)
4076
	for_each_crtc(dev, crtc)
4019
		ilk_pipe_wm_get_hw_state(crtc);
4077
		ilk_pipe_wm_get_hw_state(crtc);
4020
 
4078
 
4021
	hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4079
	hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4022
	hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4080
	hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4023
	hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4081
	hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4024
 
4082
 
4025
	hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
4083
	hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
4026
	if (INTEL_INFO(dev)->gen >= 7) {
4084
	if (INTEL_INFO(dev)->gen >= 7) {
4027
		hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4085
		hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4028
		hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4086
		hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4029
	}
4087
	}
4030
 
4088
 
4031
	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4089
	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4032
		hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4090
		hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4033
			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4091
			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4034
	else if (IS_IVYBRIDGE(dev))
4092
	else if (IS_IVYBRIDGE(dev))
4035
		hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4093
		hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4036
			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4094
			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4037
 
4095
 
4038
	hw->enable_fbc_wm =
4096
	hw->enable_fbc_wm =
4039
		!(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4097
		!(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4040
}
4098
}
4041
 
4099
 
4042
/**
4100
/**
4043
 * intel_update_watermarks - update FIFO watermark values based on current modes
4101
 * intel_update_watermarks - update FIFO watermark values based on current modes
4044
 *
4102
 *
4045
 * Calculate watermark values for the various WM regs based on current mode
4103
 * Calculate watermark values for the various WM regs based on current mode
4046
 * and plane configuration.
4104
 * and plane configuration.
4047
 *
4105
 *
4048
 * There are several cases to deal with here:
4106
 * There are several cases to deal with here:
4049
 *   - normal (i.e. non-self-refresh)
4107
 *   - normal (i.e. non-self-refresh)
4050
 *   - self-refresh (SR) mode
4108
 *   - self-refresh (SR) mode
4051
 *   - lines are large relative to FIFO size (buffer can hold up to 2)
4109
 *   - lines are large relative to FIFO size (buffer can hold up to 2)
4052
 *   - lines are small relative to FIFO size (buffer can hold more than 2
4110
 *   - lines are small relative to FIFO size (buffer can hold more than 2
4053
 *     lines), so need to account for TLB latency
4111
 *     lines), so need to account for TLB latency
4054
 *
4112
 *
4055
 *   The normal calculation is:
4113
 *   The normal calculation is:
4056
 *     watermark = dotclock * bytes per pixel * latency
4114
 *     watermark = dotclock * bytes per pixel * latency
4057
 *   where latency is platform & configuration dependent (we assume pessimal
4115
 *   where latency is platform & configuration dependent (we assume pessimal
4058
 *   values here).
4116
 *   values here).
4059
 *
4117
 *
4060
 *   The SR calculation is:
4118
 *   The SR calculation is:
4061
 *     watermark = (trunc(latency/line time)+1) * surface width *
4119
 *     watermark = (trunc(latency/line time)+1) * surface width *
4062
 *       bytes per pixel
4120
 *       bytes per pixel
4063
 *   where
4121
 *   where
4064
 *     line time = htotal / dotclock
4122
 *     line time = htotal / dotclock
4065
 *     surface width = hdisplay for normal plane and 64 for cursor
4123
 *     surface width = hdisplay for normal plane and 64 for cursor
4066
 *   and latency is assumed to be high, as above.
4124
 *   and latency is assumed to be high, as above.
4067
 *
4125
 *
4068
 * The final value programmed to the register should always be rounded up,
4126
 * The final value programmed to the register should always be rounded up,
4069
 * and include an extra 2 entries to account for clock crossings.
4127
 * and include an extra 2 entries to account for clock crossings.
4070
 *
4128
 *
4071
 * We don't use the sprite, so we can ignore that.  And on Crestline we have
4129
 * We don't use the sprite, so we can ignore that.  And on Crestline we have
4072
 * to set the non-SR watermarks to 8.
4130
 * to set the non-SR watermarks to 8.
4073
 */
4131
 */
4074
void intel_update_watermarks(struct drm_crtc *crtc)
4132
void intel_update_watermarks(struct drm_crtc *crtc)
4075
{
4133
{
4076
	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4134
	struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4077
 
4135
 
4078
	if (dev_priv->display.update_wm)
4136
	if (dev_priv->display.update_wm)
4079
		dev_priv->display.update_wm(crtc);
4137
		dev_priv->display.update_wm(crtc);
4080
}
4138
}
4081
 
4139
 
4082
void intel_update_sprite_watermarks(struct drm_plane *plane,
4140
void intel_update_sprite_watermarks(struct drm_plane *plane,
4083
				    struct drm_crtc *crtc,
4141
				    struct drm_crtc *crtc,
4084
				    uint32_t sprite_width,
4142
				    uint32_t sprite_width,
4085
				    uint32_t sprite_height,
4143
				    uint32_t sprite_height,
4086
				    int pixel_size,
4144
				    int pixel_size,
4087
				    bool enabled, bool scaled)
4145
				    bool enabled, bool scaled)
4088
{
4146
{
4089
	struct drm_i915_private *dev_priv = plane->dev->dev_private;
4147
	struct drm_i915_private *dev_priv = plane->dev->dev_private;
4090
 
4148
 
4091
	if (dev_priv->display.update_sprite_wm)
4149
	if (dev_priv->display.update_sprite_wm)
4092
		dev_priv->display.update_sprite_wm(plane, crtc,
4150
		dev_priv->display.update_sprite_wm(plane, crtc,
4093
						   sprite_width, sprite_height,
4151
						   sprite_width, sprite_height,
4094
						   pixel_size, enabled, scaled);
4152
						   pixel_size, enabled, scaled);
4095
}
4153
}
4096
 
-
 
4097
static struct drm_i915_gem_object *
-
 
4098
intel_alloc_context_page(struct drm_device *dev)
-
 
4099
{
-
 
4100
	struct drm_i915_gem_object *ctx;
-
 
4101
	int ret;
-
 
4102
 
-
 
4103
	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
-
 
4104
 
-
 
4105
	ctx = i915_gem_alloc_object(dev, 4096);
-
 
4106
	if (!ctx) {
-
 
4107
		DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
-
 
4108
		return NULL;
-
 
4109
	}
-
 
4110
 
-
 
4111
	ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
-
 
4112
	if (ret) {
-
 
4113
		DRM_ERROR("failed to pin power context: %d\n", ret);
-
 
4114
		goto err_unref;
-
 
4115
	}
-
 
4116
 
-
 
4117
	ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
-
 
4118
	if (ret) {
-
 
4119
		DRM_ERROR("failed to set-domain on power context: %d\n", ret);
-
 
4120
		goto err_unpin;
-
 
4121
	}
-
 
4122
 
-
 
4123
	return ctx;
-
 
4124
 
-
 
4125
err_unpin:
-
 
4126
	i915_gem_object_ggtt_unpin(ctx);
-
 
4127
err_unref:
-
 
4128
	drm_gem_object_unreference(&ctx->base);
-
 
4129
	return NULL;
-
 
4130
}
-
 
4131
 
4154
 
4132
/**
4155
/**
4133
 * Lock protecting IPS related data structures
4156
 * Lock protecting IPS related data structures
4134
 */
4157
 */
4135
DEFINE_SPINLOCK(mchdev_lock);
4158
DEFINE_SPINLOCK(mchdev_lock);
4136
 
4159
 
4137
/* Global for IPS driver to get at the current i915 device. Protected by
4160
/* Global for IPS driver to get at the current i915 device. Protected by
4138
 * mchdev_lock. */
4161
 * mchdev_lock. */
4139
static struct drm_i915_private *i915_mch_dev;
4162
static struct drm_i915_private *i915_mch_dev;
4140
 
4163
 
4141
bool ironlake_set_drps(struct drm_device *dev, u8 val)
4164
bool ironlake_set_drps(struct drm_device *dev, u8 val)
4142
{
4165
{
4143
	struct drm_i915_private *dev_priv = dev->dev_private;
4166
	struct drm_i915_private *dev_priv = dev->dev_private;
4144
	u16 rgvswctl;
4167
	u16 rgvswctl;
4145
 
4168
 
4146
	assert_spin_locked(&mchdev_lock);
4169
	assert_spin_locked(&mchdev_lock);
4147
 
4170
 
4148
	rgvswctl = I915_READ16(MEMSWCTL);
4171
	rgvswctl = I915_READ16(MEMSWCTL);
4149
	if (rgvswctl & MEMCTL_CMD_STS) {
4172
	if (rgvswctl & MEMCTL_CMD_STS) {
4150
		DRM_DEBUG("gpu busy, RCS change rejected\n");
4173
		DRM_DEBUG("gpu busy, RCS change rejected\n");
4151
		return false; /* still busy with another command */
4174
		return false; /* still busy with another command */
4152
	}
4175
	}
4153
 
4176
 
4154
	rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4177
	rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4155
		(val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4178
		(val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4156
	I915_WRITE16(MEMSWCTL, rgvswctl);
4179
	I915_WRITE16(MEMSWCTL, rgvswctl);
4157
	POSTING_READ16(MEMSWCTL);
4180
	POSTING_READ16(MEMSWCTL);
4158
 
4181
 
4159
	rgvswctl |= MEMCTL_CMD_STS;
4182
	rgvswctl |= MEMCTL_CMD_STS;
4160
	I915_WRITE16(MEMSWCTL, rgvswctl);
4183
	I915_WRITE16(MEMSWCTL, rgvswctl);
4161
 
4184
 
4162
	return true;
4185
	return true;
4163
}
4186
}
4164
 
4187
 
4165
static void ironlake_enable_drps(struct drm_device *dev)
4188
static void ironlake_enable_drps(struct drm_device *dev)
4166
{
4189
{
4167
	struct drm_i915_private *dev_priv = dev->dev_private;
4190
	struct drm_i915_private *dev_priv = dev->dev_private;
4168
	u32 rgvmodectl = I915_READ(MEMMODECTL);
4191
	u32 rgvmodectl = I915_READ(MEMMODECTL);
4169
	u8 fmax, fmin, fstart, vstart;
4192
	u8 fmax, fmin, fstart, vstart;
4170
 
4193
 
4171
	spin_lock_irq(&mchdev_lock);
4194
	spin_lock_irq(&mchdev_lock);
4172
 
4195
 
4173
	/* Enable temp reporting */
4196
	/* Enable temp reporting */
4174
	I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4197
	I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4175
	I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4198
	I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4176
 
4199
 
4177
	/* 100ms RC evaluation intervals */
4200
	/* 100ms RC evaluation intervals */
4178
	I915_WRITE(RCUPEI, 100000);
4201
	I915_WRITE(RCUPEI, 100000);
4179
	I915_WRITE(RCDNEI, 100000);
4202
	I915_WRITE(RCDNEI, 100000);
4180
 
4203
 
4181
	/* Set max/min thresholds to 90ms and 80ms respectively */
4204
	/* Set max/min thresholds to 90ms and 80ms respectively */
4182
	I915_WRITE(RCBMAXAVG, 90000);
4205
	I915_WRITE(RCBMAXAVG, 90000);
4183
	I915_WRITE(RCBMINAVG, 80000);
4206
	I915_WRITE(RCBMINAVG, 80000);
4184
 
4207
 
4185
	I915_WRITE(MEMIHYST, 1);
4208
	I915_WRITE(MEMIHYST, 1);
4186
 
4209
 
4187
	/* Set up min, max, and cur for interrupt handling */
4210
	/* Set up min, max, and cur for interrupt handling */
4188
	fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4211
	fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4189
	fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4212
	fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4190
	fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4213
	fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4191
		MEMMODE_FSTART_SHIFT;
4214
		MEMMODE_FSTART_SHIFT;
4192
 
4215
 
4193
	vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
4216
	vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
4194
		PXVFREQ_PX_SHIFT;
4217
		PXVFREQ_PX_SHIFT;
4195
 
4218
 
4196
	dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4219
	dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4197
	dev_priv->ips.fstart = fstart;
4220
	dev_priv->ips.fstart = fstart;
4198
 
4221
 
4199
	dev_priv->ips.max_delay = fstart;
4222
	dev_priv->ips.max_delay = fstart;
4200
	dev_priv->ips.min_delay = fmin;
4223
	dev_priv->ips.min_delay = fmin;
4201
	dev_priv->ips.cur_delay = fstart;
4224
	dev_priv->ips.cur_delay = fstart;
4202
 
4225
 
4203
	DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4226
	DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4204
			 fmax, fmin, fstart);
4227
			 fmax, fmin, fstart);
4205
 
4228
 
4206
	I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4229
	I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4207
 
4230
 
4208
	/*
4231
	/*
4209
	 * Interrupts will be enabled in ironlake_irq_postinstall
4232
	 * Interrupts will be enabled in ironlake_irq_postinstall
4210
	 */
4233
	 */
4211
 
4234
 
4212
	I915_WRITE(VIDSTART, vstart);
4235
	I915_WRITE(VIDSTART, vstart);
4213
	POSTING_READ(VIDSTART);
4236
	POSTING_READ(VIDSTART);
4214
 
4237
 
4215
	rgvmodectl |= MEMMODE_SWMODE_EN;
4238
	rgvmodectl |= MEMMODE_SWMODE_EN;
4216
	I915_WRITE(MEMMODECTL, rgvmodectl);
4239
	I915_WRITE(MEMMODECTL, rgvmodectl);
4217
 
4240
 
4218
	if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
4241
	if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
4219
		DRM_ERROR("stuck trying to change perf mode\n");
4242
		DRM_ERROR("stuck trying to change perf mode\n");
4220
	mdelay(1);
4243
	mdelay(1);
4221
 
4244
 
4222
	ironlake_set_drps(dev, fstart);
4245
	ironlake_set_drps(dev, fstart);
4223
 
4246
 
4224
	dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
4247
	dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4225
		I915_READ(0x112e0);
4248
		I915_READ(DDREC) + I915_READ(CSIEC);
4226
	dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
4249
	dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
4227
	dev_priv->ips.last_count2 = I915_READ(0x112f4);
4250
	dev_priv->ips.last_count2 = I915_READ(GFXEC);
4228
	dev_priv->ips.last_time2 = ktime_get_raw_ns();
4251
	dev_priv->ips.last_time2 = ktime_get_raw_ns();
4229
 
4252
 
4230
	spin_unlock_irq(&mchdev_lock);
4253
	spin_unlock_irq(&mchdev_lock);
4231
}
4254
}
4232
 
4255
 
4233
static void ironlake_disable_drps(struct drm_device *dev)
4256
static void ironlake_disable_drps(struct drm_device *dev)
4234
{
4257
{
4235
	struct drm_i915_private *dev_priv = dev->dev_private;
4258
	struct drm_i915_private *dev_priv = dev->dev_private;
4236
	u16 rgvswctl;
4259
	u16 rgvswctl;
4237
 
4260
 
4238
	spin_lock_irq(&mchdev_lock);
4261
	spin_lock_irq(&mchdev_lock);
4239
 
4262
 
4240
	rgvswctl = I915_READ16(MEMSWCTL);
4263
	rgvswctl = I915_READ16(MEMSWCTL);
4241
 
4264
 
4242
	/* Ack interrupts, disable EFC interrupt */
4265
	/* Ack interrupts, disable EFC interrupt */
4243
	I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4266
	I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4244
	I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4267
	I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4245
	I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4268
	I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4246
	I915_WRITE(DEIIR, DE_PCU_EVENT);
4269
	I915_WRITE(DEIIR, DE_PCU_EVENT);
4247
	I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4270
	I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4248
 
4271
 
4249
	/* Go back to the starting frequency */
4272
	/* Go back to the starting frequency */
4250
	ironlake_set_drps(dev, dev_priv->ips.fstart);
4273
	ironlake_set_drps(dev, dev_priv->ips.fstart);
4251
	mdelay(1);
4274
	mdelay(1);
4252
	rgvswctl |= MEMCTL_CMD_STS;
4275
	rgvswctl |= MEMCTL_CMD_STS;
4253
	I915_WRITE(MEMSWCTL, rgvswctl);
4276
	I915_WRITE(MEMSWCTL, rgvswctl);
4254
	mdelay(1);
4277
	mdelay(1);
4255
 
4278
 
4256
	spin_unlock_irq(&mchdev_lock);
4279
	spin_unlock_irq(&mchdev_lock);
4257
}
4280
}
4258
 
4281
 
4259
/* There's a funny hw issue where the hw returns all 0 when reading from
4282
/* There's a funny hw issue where the hw returns all 0 when reading from
4260
 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4283
 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4261
 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4284
 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4262
 * all limits and the gpu stuck at whatever frequency it is at atm).
4285
 * all limits and the gpu stuck at whatever frequency it is at atm).
4263
 */
4286
 */
4264
static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
4287
static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
4265
{
4288
{
4266
	u32 limits;
4289
	u32 limits;
4267
 
4290
 
4268
	/* Only set the down limit when we've reached the lowest level to avoid
4291
	/* Only set the down limit when we've reached the lowest level to avoid
4269
	 * getting more interrupts, otherwise leave this clear. This prevents a
4292
	 * getting more interrupts, otherwise leave this clear. This prevents a
4270
	 * race in the hw when coming out of rc6: There's a tiny window where
4293
	 * race in the hw when coming out of rc6: There's a tiny window where
4271
	 * the hw runs at the minimal clock before selecting the desired
4294
	 * the hw runs at the minimal clock before selecting the desired
4272
	 * frequency, if the down threshold expires in that window we will not
4295
	 * frequency, if the down threshold expires in that window we will not
4273
	 * receive a down interrupt. */
4296
	 * receive a down interrupt. */
-
 
4297
	if (IS_GEN9(dev_priv->dev)) {
-
 
4298
		limits = (dev_priv->rps.max_freq_softlimit) << 23;
-
 
4299
		if (val <= dev_priv->rps.min_freq_softlimit)
-
 
4300
			limits |= (dev_priv->rps.min_freq_softlimit) << 14;
-
 
4301
	} else {
4274
	limits = dev_priv->rps.max_freq_softlimit << 24;
4302
		limits = dev_priv->rps.max_freq_softlimit << 24;
4275
	if (val <= dev_priv->rps.min_freq_softlimit)
4303
		if (val <= dev_priv->rps.min_freq_softlimit)
4276
		limits |= dev_priv->rps.min_freq_softlimit << 16;
4304
			limits |= dev_priv->rps.min_freq_softlimit << 16;
-
 
4305
	}
4277
 
4306
 
4278
	return limits;
4307
	return limits;
4279
}
4308
}
4280
 
4309
 
4281
static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4310
static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4282
{
4311
{
4283
	int new_power;
4312
	int new_power;
-
 
4313
	u32 threshold_up = 0, threshold_down = 0; /* in % */
-
 
4314
	u32 ei_up = 0, ei_down = 0;
4284
 
4315
 
4285
	new_power = dev_priv->rps.power;
4316
	new_power = dev_priv->rps.power;
4286
	switch (dev_priv->rps.power) {
4317
	switch (dev_priv->rps.power) {
4287
	case LOW_POWER:
4318
	case LOW_POWER:
4288
		if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
4319
		if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
4289
			new_power = BETWEEN;
4320
			new_power = BETWEEN;
4290
		break;
4321
		break;
4291
 
4322
 
4292
	case BETWEEN:
4323
	case BETWEEN:
4293
		if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
4324
		if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
4294
			new_power = LOW_POWER;
4325
			new_power = LOW_POWER;
4295
		else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
4326
		else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
4296
			new_power = HIGH_POWER;
4327
			new_power = HIGH_POWER;
4297
		break;
4328
		break;
4298
 
4329
 
4299
	case HIGH_POWER:
4330
	case HIGH_POWER:
4300
		if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
4331
		if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
4301
			new_power = BETWEEN;
4332
			new_power = BETWEEN;
4302
		break;
4333
		break;
4303
	}
4334
	}
4304
	/* Max/min bins are special */
4335
	/* Max/min bins are special */
4305
	if (val == dev_priv->rps.min_freq_softlimit)
4336
	if (val <= dev_priv->rps.min_freq_softlimit)
4306
		new_power = LOW_POWER;
4337
		new_power = LOW_POWER;
4307
	if (val == dev_priv->rps.max_freq_softlimit)
4338
	if (val >= dev_priv->rps.max_freq_softlimit)
4308
		new_power = HIGH_POWER;
4339
		new_power = HIGH_POWER;
4309
	if (new_power == dev_priv->rps.power)
4340
	if (new_power == dev_priv->rps.power)
4310
		return;
4341
		return;
4311
 
4342
 
4312
	/* Note the units here are not exactly 1us, but 1280ns. */
4343
	/* Note the units here are not exactly 1us, but 1280ns. */
4313
	switch (new_power) {
4344
	switch (new_power) {
4314
	case LOW_POWER:
4345
	case LOW_POWER:
4315
		/* Upclock if more than 95% busy over 16ms */
4346
		/* Upclock if more than 95% busy over 16ms */
4316
		I915_WRITE(GEN6_RP_UP_EI, 12500);
4347
		ei_up = 16000;
4317
		I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
4348
		threshold_up = 95;
4318
 
4349
 
4319
		/* Downclock if less than 85% busy over 32ms */
-
 
4320
		I915_WRITE(GEN6_RP_DOWN_EI, 25000);
-
 
4321
		I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
-
 
4322
 
-
 
4323
		I915_WRITE(GEN6_RP_CONTROL,
4350
		/* Downclock if less than 85% busy over 32ms */
4324
			   GEN6_RP_MEDIA_TURBO |
-
 
4325
			   GEN6_RP_MEDIA_HW_NORMAL_MODE |
-
 
4326
			   GEN6_RP_MEDIA_IS_GFX |
4351
		ei_down = 32000;
4327
			   GEN6_RP_ENABLE |
-
 
4328
			   GEN6_RP_UP_BUSY_AVG |
-
 
4329
			   GEN6_RP_DOWN_IDLE_AVG);
4352
		threshold_down = 85;
4330
		break;
4353
		break;
4331
 
4354
 
4332
	case BETWEEN:
4355
	case BETWEEN:
4333
		/* Upclock if more than 90% busy over 13ms */
4356
		/* Upclock if more than 90% busy over 13ms */
4334
		I915_WRITE(GEN6_RP_UP_EI, 10250);
4357
		ei_up = 13000;
4335
		I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
4358
		threshold_up = 90;
4336
 
4359
 
4337
		/* Downclock if less than 75% busy over 32ms */
-
 
4338
		I915_WRITE(GEN6_RP_DOWN_EI, 25000);
-
 
4339
		I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
-
 
4340
 
-
 
4341
		I915_WRITE(GEN6_RP_CONTROL,
4360
		/* Downclock if less than 75% busy over 32ms */
4342
			   GEN6_RP_MEDIA_TURBO |
-
 
4343
			   GEN6_RP_MEDIA_HW_NORMAL_MODE |
-
 
4344
			   GEN6_RP_MEDIA_IS_GFX |
4361
		ei_down = 32000;
4345
			   GEN6_RP_ENABLE |
-
 
4346
			   GEN6_RP_UP_BUSY_AVG |
-
 
4347
			   GEN6_RP_DOWN_IDLE_AVG);
4362
		threshold_down = 75;
4348
		break;
4363
		break;
4349
 
4364
 
4350
	case HIGH_POWER:
4365
	case HIGH_POWER:
4351
		/* Upclock if more than 85% busy over 10ms */
4366
		/* Upclock if more than 85% busy over 10ms */
4352
		I915_WRITE(GEN6_RP_UP_EI, 8000);
4367
		ei_up = 10000;
4353
		I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
4368
		threshold_up = 85;
4354
 
4369
 
-
 
4370
		/* Downclock if less than 60% busy over 32ms */
-
 
4371
		ei_down = 32000;
-
 
4372
		threshold_down = 60;
-
 
4373
		break;
-
 
4374
	}
-
 
4375
 
-
 
4376
	I915_WRITE(GEN6_RP_UP_EI,
-
 
4377
		GT_INTERVAL_FROM_US(dev_priv, ei_up));
-
 
4378
	I915_WRITE(GEN6_RP_UP_THRESHOLD,
-
 
4379
		GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4355
		/* Downclock if less than 60% busy over 32ms */
4380
 
-
 
4381
	I915_WRITE(GEN6_RP_DOWN_EI,
4356
		I915_WRITE(GEN6_RP_DOWN_EI, 25000);
4382
		GT_INTERVAL_FROM_US(dev_priv, ei_down));
-
 
4383
	I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4357
		I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
4384
		GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4358
 
4385
 
4359
		I915_WRITE(GEN6_RP_CONTROL,
4386
	 I915_WRITE(GEN6_RP_CONTROL,
4360
			   GEN6_RP_MEDIA_TURBO |
4387
		    GEN6_RP_MEDIA_TURBO |
4361
			   GEN6_RP_MEDIA_HW_NORMAL_MODE |
4388
		    GEN6_RP_MEDIA_HW_NORMAL_MODE |
4362
			   GEN6_RP_MEDIA_IS_GFX |
4389
		    GEN6_RP_MEDIA_IS_GFX |
4363
			   GEN6_RP_ENABLE |
4390
		    GEN6_RP_ENABLE |
4364
			   GEN6_RP_UP_BUSY_AVG |
4391
		    GEN6_RP_UP_BUSY_AVG |
4365
			   GEN6_RP_DOWN_IDLE_AVG);
4392
		    GEN6_RP_DOWN_IDLE_AVG);
4366
		break;
-
 
4367
	}
-
 
4368
 
4393
 
-
 
4394
	dev_priv->rps.power = new_power;
-
 
4395
	dev_priv->rps.up_threshold = threshold_up;
4369
	dev_priv->rps.power = new_power;
4396
	dev_priv->rps.down_threshold = threshold_down;
4370
	dev_priv->rps.last_adj = 0;
4397
	dev_priv->rps.last_adj = 0;
4371
}
4398
}
4372
 
4399
 
4373
static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4400
static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4374
{
4401
{
4375
	u32 mask = 0;
4402
	u32 mask = 0;
4376
 
4403
 
4377
	if (val > dev_priv->rps.min_freq_softlimit)
4404
	if (val > dev_priv->rps.min_freq_softlimit)
4378
		mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
4405
		mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
4379
	if (val < dev_priv->rps.max_freq_softlimit)
4406
	if (val < dev_priv->rps.max_freq_softlimit)
4380
		mask |= GEN6_PM_RP_UP_THRESHOLD;
4407
		mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
4381
 
-
 
4382
	mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
4408
 
4383
	mask &= dev_priv->pm_rps_events;
-
 
4384
 
4409
	mask &= dev_priv->pm_rps_events;
4385
	/* IVB and SNB hard hangs on looping batchbuffer
-
 
4386
	 * if GEN6_PM_UP_EI_EXPIRED is masked.
-
 
4387
	 */
-
 
4388
	if (INTEL_INFO(dev_priv->dev)->gen <= 7 && !IS_HASWELL(dev_priv->dev))
-
 
4389
		mask |= GEN6_PM_RP_UP_EI_EXPIRED;
-
 
4390
 
-
 
4391
	if (IS_GEN8(dev_priv->dev))
-
 
4392
		mask |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
-
 
4393
 
4410
 
4394
	return ~mask;
4411
	return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
4395
}
4412
}
4396
 
4413
 
4397
/* gen6_set_rps is called to update the frequency request, but should also be
4414
/* gen6_set_rps is called to update the frequency request, but should also be
4398
 * called when the range (min_delay and max_delay) is modified so that we can
4415
 * called when the range (min_delay and max_delay) is modified so that we can
4399
 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
4416
 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
4400
void gen6_set_rps(struct drm_device *dev, u8 val)
4417
static void gen6_set_rps(struct drm_device *dev, u8 val)
4401
{
4418
{
4402
	struct drm_i915_private *dev_priv = dev->dev_private;
4419
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
4420
 
-
 
4421
	/* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
-
 
4422
	if (IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0))
-
 
4423
		return;
4403
 
4424
 
4404
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4425
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4405
	WARN_ON(val > dev_priv->rps.max_freq_softlimit);
4426
	WARN_ON(val > dev_priv->rps.max_freq);
4406
	WARN_ON(val < dev_priv->rps.min_freq_softlimit);
4427
	WARN_ON(val < dev_priv->rps.min_freq);
4407
 
4428
 
4408
	/* min/max delay may still have been modified so be sure to
4429
	/* min/max delay may still have been modified so be sure to
4409
	 * write the limits value.
4430
	 * write the limits value.
4410
	 */
4431
	 */
4411
	if (val != dev_priv->rps.cur_freq) {
4432
	if (val != dev_priv->rps.cur_freq) {
4412
		gen6_set_rps_thresholds(dev_priv, val);
4433
		gen6_set_rps_thresholds(dev_priv, val);
-
 
4434
 
-
 
4435
		if (IS_GEN9(dev))
-
 
4436
			I915_WRITE(GEN6_RPNSWREQ,
4413
 
4437
				   GEN9_FREQUENCY(val));
4414
		if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4438
		else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4415
			I915_WRITE(GEN6_RPNSWREQ,
4439
			I915_WRITE(GEN6_RPNSWREQ,
4416
				   HSW_FREQUENCY(val));
4440
				   HSW_FREQUENCY(val));
4417
		else
4441
		else
4418
			I915_WRITE(GEN6_RPNSWREQ,
4442
			I915_WRITE(GEN6_RPNSWREQ,
4419
				   GEN6_FREQUENCY(val) |
4443
				   GEN6_FREQUENCY(val) |
4420
				   GEN6_OFFSET(0) |
4444
				   GEN6_OFFSET(0) |
4421
				   GEN6_AGGRESSIVE_TURBO);
4445
				   GEN6_AGGRESSIVE_TURBO);
4422
	}
4446
	}
4423
 
4447
 
4424
	/* Make sure we continue to get interrupts
4448
	/* Make sure we continue to get interrupts
4425
	 * until we hit the minimum or maximum frequencies.
4449
	 * until we hit the minimum or maximum frequencies.
4426
	 */
4450
	 */
4427
	I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
4451
	I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
4428
	I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4452
	I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4429
 
4453
 
4430
	POSTING_READ(GEN6_RPNSWREQ);
4454
	POSTING_READ(GEN6_RPNSWREQ);
4431
 
4455
 
4432
	dev_priv->rps.cur_freq = val;
4456
	dev_priv->rps.cur_freq = val;
4433
	trace_intel_gpu_freq_change(val * 50);
4457
	trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4434
}
4458
}
4435
 
-
 
4436
/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
-
 
4437
 *
-
 
4438
 * * If Gfx is Idle, then
-
 
4439
 * 1. Mask Turbo interrupts
-
 
4440
 * 2. Bring up Gfx clock
-
 
4441
 * 3. Change the freq to Rpn and wait till P-Unit updates freq
-
 
4442
 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
-
 
4443
 * 5. Unmask Turbo interrupts
-
 
4444
*/
4459
 
4445
static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4460
static void valleyview_set_rps(struct drm_device *dev, u8 val)
4446
{
4461
{
4447
	struct drm_device *dev = dev_priv->dev;
-
 
4448
 
-
 
4449
	/* Latest VLV doesn't need to force the gfx clock */
-
 
4450
	if (dev->pdev->revision >= 0xd) {
-
 
4451
		valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
-
 
4452
		return;
-
 
4453
	}
-
 
4454
 
-
 
4455
	/*
-
 
4456
	 * When we are idle.  Drop to min voltage state.
-
 
-
 
4462
	struct drm_i915_private *dev_priv = dev->dev_private;
4457
	 */
4463
 
4458
 
4464
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4459
	if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
4465
	WARN_ON(val > dev_priv->rps.max_freq);
4460
		return;
4466
	WARN_ON(val < dev_priv->rps.min_freq);
-
 
4467
 
4461
 
4468
	if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4462
	/* Mask turbo interrupt so that they will not come in between */
4469
		      "Odd GPU freq value\n"))
-
 
4470
		val &= ~1;
-
 
4471
 
-
 
4472
	I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
-
 
4473
 
4463
	I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
4474
	if (val != dev_priv->rps.cur_freq) {
4464
 
4475
		vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
-
 
4476
		if (!IS_CHERRYVIEW(dev_priv))
4465
	vlv_force_gfx_clock(dev_priv, true);
4477
			gen6_set_rps_thresholds(dev_priv, val);
-
 
4478
	}
-
 
4479
 
4466
 
4480
	dev_priv->rps.cur_freq = val;
-
 
4481
	trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4467
	dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
4482
}
-
 
4483
 
-
 
4484
/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
-
 
4485
 *
-
 
4486
 * * If Gfx is Idle, then
4468
 
4487
 * 1. Forcewake Media well.
-
 
4488
 * 2. Request idle freq.
-
 
4489
 * 3. Release Forcewake of Media well.
-
 
4490
*/
-
 
4491
static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4469
	vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
4492
{
4470
					dev_priv->rps.min_freq_softlimit);
4493
	u32 val = dev_priv->rps.idle_freq;
4471
 
4494
 
4472
	if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
4495
	if (dev_priv->rps.cur_freq <= val)
4473
				& GENFREQSTATUS) == 0, 100))
4496
		return;
4474
		DRM_ERROR("timed out waiting for Punit\n");
-
 
4475
 
-
 
4476
	vlv_force_gfx_clock(dev_priv, false);
4497
 
4477
 
4498
	/* Wake up the media well, as that takes a lot less
4478
	I915_WRITE(GEN6_PMINTRMSK,
-
 
4479
		   gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4499
	 * power than the Render well. */
4480
}
4500
	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
4481
 
4501
	valleyview_set_rps(dev_priv->dev, val);
4482
void gen6_rps_idle(struct drm_i915_private *dev_priv)
-
 
4483
{
4502
	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
4484
	struct drm_device *dev = dev_priv->dev;
-
 
4485
 
4503
}
4486
	mutex_lock(&dev_priv->rps.hw_lock);
4504
 
4487
	if (dev_priv->rps.enabled) {
4505
void gen6_rps_busy(struct drm_i915_private *dev_priv)
4488
		if (IS_CHERRYVIEW(dev))
4506
{
4489
			valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4507
	mutex_lock(&dev_priv->rps.hw_lock);
4490
		else if (IS_VALLEYVIEW(dev))
4508
	if (dev_priv->rps.enabled) {
4491
			vlv_set_rps_idle(dev_priv);
4509
		if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4492
		else
4510
			gen6_rps_reset_ei(dev_priv);
4493
			gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4511
		I915_WRITE(GEN6_PMINTRMSK,
4494
		dev_priv->rps.last_adj = 0;
4512
			   gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4495
	}
4513
	}
4496
	mutex_unlock(&dev_priv->rps.hw_lock);
4514
	mutex_unlock(&dev_priv->rps.hw_lock);
4497
}
4515
}
4498
 
4516
 
4499
void gen6_rps_boost(struct drm_i915_private *dev_priv)
4517
void gen6_rps_idle(struct drm_i915_private *dev_priv)
4500
{
4518
{
4501
	struct drm_device *dev = dev_priv->dev;
4519
	struct drm_device *dev = dev_priv->dev;
4502
 
4520
 
4503
	mutex_lock(&dev_priv->rps.hw_lock);
4521
	mutex_lock(&dev_priv->rps.hw_lock);
4504
	if (dev_priv->rps.enabled) {
4522
	if (dev_priv->rps.enabled) {
4505
		if (IS_VALLEYVIEW(dev))
4523
		if (IS_VALLEYVIEW(dev))
4506
			valleyview_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
4524
			vlv_set_rps_idle(dev_priv);
4507
		else
4525
		else
4508
			gen6_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
4526
			gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
4509
		dev_priv->rps.last_adj = 0;
4527
		dev_priv->rps.last_adj = 0;
-
 
4528
		I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
4510
	}
4529
	}
4511
	mutex_unlock(&dev_priv->rps.hw_lock);
4530
	mutex_unlock(&dev_priv->rps.hw_lock);
-
 
4531
 
-
 
4532
	spin_lock(&dev_priv->rps.client_lock);
-
 
4533
	while (!list_empty(&dev_priv->rps.clients))
-
 
4534
		list_del_init(dev_priv->rps.clients.next);
-
 
4535
	spin_unlock(&dev_priv->rps.client_lock);
4512
}
4536
}
4513
 
4537
 
-
 
4538
void gen6_rps_boost(struct drm_i915_private *dev_priv,
-
 
4539
		    struct intel_rps_client *rps,
4514
void valleyview_set_rps(struct drm_device *dev, u8 val)
4540
		    unsigned long submitted)
4515
{
4541
{
-
 
4542
	/* This is intentionally racy! We peek at the state here, then
4516
	struct drm_i915_private *dev_priv = dev->dev_private;
4543
	 * validate inside the RPS worker.
4517
 
4544
	 */
4518
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4545
	if (!(dev_priv->mm.busy &&
4519
	WARN_ON(val > dev_priv->rps.max_freq_softlimit);
4546
	      dev_priv->rps.enabled &&
-
 
4547
	      dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4520
	WARN_ON(val < dev_priv->rps.min_freq_softlimit);
4548
		return;
4521
 
4549
 
-
 
4550
	/* Force a RPS boost (and don't count it against the client) if
-
 
4551
	 * the GPU is severely congested.
4522
	if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4552
	 */
4523
		      "Odd GPU freq value\n"))
4553
	if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
-
 
4554
		rps = NULL;
-
 
4555
 
-
 
4556
	spin_lock(&dev_priv->rps.client_lock);
-
 
4557
	if (rps == NULL || list_empty(&rps->link)) {
4524
		val &= ~1;
4558
		spin_lock_irq(&dev_priv->irq_lock);
-
 
4559
		if (dev_priv->rps.interrupts_enabled) {
-
 
4560
			dev_priv->rps.client_boost = true;
-
 
4561
			queue_work(dev_priv->wq, &dev_priv->rps.work);
4525
 
4562
		}
-
 
4563
		spin_unlock_irq(&dev_priv->irq_lock);
-
 
4564
 
-
 
4565
		if (rps != NULL) {
-
 
4566
			list_add(&rps->link, &dev_priv->rps.clients);
-
 
4567
			rps->boosts++;
-
 
4568
		} else
-
 
4569
			dev_priv->rps.boosts++;
-
 
4570
	}
-
 
4571
	spin_unlock(&dev_priv->rps.client_lock);
4526
	if (val != dev_priv->rps.cur_freq)
4572
}
-
 
4573
 
4527
		vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
4574
void intel_set_rps(struct drm_device *dev, u8 val)
4528
 
4575
{
4529
	I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4576
	if (IS_VALLEYVIEW(dev))
4530
 
4577
		valleyview_set_rps(dev, val);
4531
	dev_priv->rps.cur_freq = val;
4578
	else
4532
	trace_intel_gpu_freq_change(vlv_gpu_freq(dev_priv, val));
4579
		gen6_set_rps(dev, val);
4533
}
4580
}
4534
 
4581
 
4535
static void gen9_disable_rps(struct drm_device *dev)
4582
static void gen9_disable_rps(struct drm_device *dev)
4536
{
4583
{
4537
	struct drm_i915_private *dev_priv = dev->dev_private;
4584
	struct drm_i915_private *dev_priv = dev->dev_private;
4538
 
4585
 
4539
	I915_WRITE(GEN6_RC_CONTROL, 0);
4586
	I915_WRITE(GEN6_RC_CONTROL, 0);
-
 
4587
	I915_WRITE(GEN9_PG_ENABLE, 0);
4540
}
4588
}
4541
 
4589
 
4542
static void gen6_disable_rps(struct drm_device *dev)
4590
static void gen6_disable_rps(struct drm_device *dev)
4543
{
4591
{
4544
	struct drm_i915_private *dev_priv = dev->dev_private;
4592
	struct drm_i915_private *dev_priv = dev->dev_private;
4545
 
4593
 
4546
	I915_WRITE(GEN6_RC_CONTROL, 0);
4594
	I915_WRITE(GEN6_RC_CONTROL, 0);
4547
	I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
4595
	I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
4548
}
4596
}
4549
 
4597
 
4550
static void cherryview_disable_rps(struct drm_device *dev)
4598
static void cherryview_disable_rps(struct drm_device *dev)
4551
{
4599
{
4552
	struct drm_i915_private *dev_priv = dev->dev_private;
4600
	struct drm_i915_private *dev_priv = dev->dev_private;
4553
 
4601
 
4554
	I915_WRITE(GEN6_RC_CONTROL, 0);
4602
	I915_WRITE(GEN6_RC_CONTROL, 0);
4555
}
4603
}
4556
 
4604
 
4557
static void valleyview_disable_rps(struct drm_device *dev)
4605
static void valleyview_disable_rps(struct drm_device *dev)
4558
{
4606
{
4559
	struct drm_i915_private *dev_priv = dev->dev_private;
4607
	struct drm_i915_private *dev_priv = dev->dev_private;
4560
 
4608
 
4561
	/* we're doing forcewake before Disabling RC6,
4609
	/* we're doing forcewake before Disabling RC6,
4562
	 * This what the BIOS expects when going into suspend */
4610
	 * This what the BIOS expects when going into suspend */
4563
	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4611
	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4564
 
4612
 
4565
	I915_WRITE(GEN6_RC_CONTROL, 0);
4613
	I915_WRITE(GEN6_RC_CONTROL, 0);
4566
 
4614
 
4567
	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4615
	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4568
}
4616
}
4569
 
4617
 
4570
static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4618
static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4571
{
4619
{
4572
	if (IS_VALLEYVIEW(dev)) {
4620
	if (IS_VALLEYVIEW(dev)) {
4573
		if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4621
		if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4574
			mode = GEN6_RC_CTL_RC6_ENABLE;
4622
			mode = GEN6_RC_CTL_RC6_ENABLE;
4575
		else
4623
		else
4576
			mode = 0;
4624
			mode = 0;
4577
	}
4625
	}
4578
	if (HAS_RC6p(dev))
4626
	if (HAS_RC6p(dev))
4579
		DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4627
		DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4580
			(mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4628
			      (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4581
			(mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4629
			      (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4582
			(mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4630
			      (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4583
 
4631
 
4584
	else
4632
	else
4585
		DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4633
		DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4586
			      (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
4634
			      (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
4587
}
4635
}
4588
 
4636
 
4589
static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
4637
static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
4590
{
4638
{
4591
	/* No RC6 before Ironlake */
4639
	/* No RC6 before Ironlake and code is gone for ilk. */
4592
	if (INTEL_INFO(dev)->gen < 5)
4640
	if (INTEL_INFO(dev)->gen < 6)
4593
		return 0;
-
 
4594
 
-
 
4595
	/* RC6 is only on Ironlake mobile not on desktop */
-
 
4596
	if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
-
 
4597
		return 0;
4641
		return 0;
4598
 
4642
 
4599
	/* Respect the kernel parameter if it is set */
4643
	/* Respect the kernel parameter if it is set */
4600
	if (enable_rc6 >= 0) {
4644
	if (enable_rc6 >= 0) {
4601
		int mask;
4645
		int mask;
4602
 
4646
 
4603
		if (HAS_RC6p(dev))
4647
		if (HAS_RC6p(dev))
4604
			mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4648
			mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4605
			       INTEL_RC6pp_ENABLE;
4649
			       INTEL_RC6pp_ENABLE;
4606
		else
4650
		else
4607
			mask = INTEL_RC6_ENABLE;
4651
			mask = INTEL_RC6_ENABLE;
4608
 
4652
 
4609
		if ((enable_rc6 & mask) != enable_rc6)
4653
		if ((enable_rc6 & mask) != enable_rc6)
4610
			DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4654
			DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4611
				 enable_rc6 & mask, enable_rc6, mask);
4655
				      enable_rc6 & mask, enable_rc6, mask);
4612
 
4656
 
4613
		return enable_rc6 & mask;
4657
		return enable_rc6 & mask;
4614
	}
4658
	}
4615
 
-
 
4616
	/* Disable RC6 on Ironlake */
-
 
4617
	if (INTEL_INFO(dev)->gen == 5)
-
 
4618
		return 0;
-
 
4619
 
4659
 
4620
	if (IS_IVYBRIDGE(dev))
4660
	if (IS_IVYBRIDGE(dev))
4621
		return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
4661
		return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
4622
 
4662
 
4623
		return INTEL_RC6_ENABLE;
4663
	return INTEL_RC6_ENABLE;
4624
}
4664
}
4625
 
4665
 
4626
int intel_enable_rc6(const struct drm_device *dev)
4666
int intel_enable_rc6(const struct drm_device *dev)
4627
{
4667
{
4628
	return i915.enable_rc6;
4668
	return i915.enable_rc6;
4629
}
4669
}
4630
 
4670
 
4631
static void gen6_init_rps_frequencies(struct drm_device *dev)
4671
static void gen6_init_rps_frequencies(struct drm_device *dev)
4632
{
4672
{
4633
	struct drm_i915_private *dev_priv = dev->dev_private;
4673
	struct drm_i915_private *dev_priv = dev->dev_private;
4634
	uint32_t rp_state_cap;
4674
	uint32_t rp_state_cap;
4635
	u32 ddcc_status = 0;
4675
	u32 ddcc_status = 0;
4636
	int ret;
4676
	int ret;
4637
 
-
 
4638
	rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4677
 
4639
	/* All of these values are in units of 50MHz */
4678
	/* All of these values are in units of 50MHz */
4640
	dev_priv->rps.cur_freq		= 0;
4679
	dev_priv->rps.cur_freq		= 0;
4641
	/* static values from HW: RP0 > RP1 > RPn (min_freq) */
4680
	/* static values from HW: RP0 > RP1 > RPn (min_freq) */
-
 
4681
	if (IS_BROXTON(dev)) {
-
 
4682
		rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
-
 
4683
		dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
-
 
4684
		dev_priv->rps.rp1_freq = (rp_state_cap >>  8) & 0xff;
-
 
4685
		dev_priv->rps.min_freq = (rp_state_cap >>  0) & 0xff;
-
 
4686
	} else {
-
 
4687
		rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4642
	dev_priv->rps.rp0_freq		= (rp_state_cap >>  0) & 0xff;
4688
		dev_priv->rps.rp0_freq = (rp_state_cap >>  0) & 0xff;
4643
	dev_priv->rps.rp1_freq		= (rp_state_cap >>  8) & 0xff;
4689
		dev_priv->rps.rp1_freq = (rp_state_cap >>  8) & 0xff;
4644
	dev_priv->rps.min_freq		= (rp_state_cap >> 16) & 0xff;
4690
		dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
-
 
4691
	}
-
 
4692
 
4645
	/* hw_max = RP0 until we check for overclocking */
4693
	/* hw_max = RP0 until we check for overclocking */
4646
	dev_priv->rps.max_freq		= dev_priv->rps.rp0_freq;
4694
	dev_priv->rps.max_freq		= dev_priv->rps.rp0_freq;
4647
 
4695
 
4648
	dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4696
	dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4649
	if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4697
	if (IS_HASWELL(dev) || IS_BROADWELL(dev) || IS_SKYLAKE(dev)) {
4650
		ret = sandybridge_pcode_read(dev_priv,
4698
		ret = sandybridge_pcode_read(dev_priv,
4651
					HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4699
					HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4652
					&ddcc_status);
4700
					&ddcc_status);
4653
		if (0 == ret)
4701
		if (0 == ret)
4654
			dev_priv->rps.efficient_freq =
4702
			dev_priv->rps.efficient_freq =
-
 
4703
				clamp_t(u8,
4655
				(ddcc_status >> 8) & 0xff;
4704
					((ddcc_status >> 8) & 0xff),
-
 
4705
					dev_priv->rps.min_freq,
-
 
4706
					dev_priv->rps.max_freq);
-
 
4707
	}
-
 
4708
 
-
 
4709
	if (IS_SKYLAKE(dev)) {
-
 
4710
		/* Store the frequency values in 16.66 MHZ units, which is
-
 
4711
		   the natural hardware unit for SKL */
-
 
4712
		dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
-
 
4713
		dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
-
 
4714
		dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
-
 
4715
		dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
-
 
4716
		dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
4656
	}
4717
	}
-
 
4718
 
-
 
4719
	dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4657
 
4720
 
4658
	/* Preserve min/max settings in case of re-init */
4721
	/* Preserve min/max settings in case of re-init */
4659
	if (dev_priv->rps.max_freq_softlimit == 0)
4722
	if (dev_priv->rps.max_freq_softlimit == 0)
4660
		dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4723
		dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4661
 
4724
 
4662
	if (dev_priv->rps.min_freq_softlimit == 0) {
4725
	if (dev_priv->rps.min_freq_softlimit == 0) {
4663
		if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4726
		if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4664
			dev_priv->rps.min_freq_softlimit =
4727
			dev_priv->rps.min_freq_softlimit =
4665
				/* max(RPe, 450 MHz) */
4728
				max_t(int, dev_priv->rps.efficient_freq,
4666
				max(dev_priv->rps.efficient_freq, (u8) 9);
4729
				      intel_freq_opcode(dev_priv, 450));
4667
		else
4730
		else
4668
			dev_priv->rps.min_freq_softlimit =
4731
			dev_priv->rps.min_freq_softlimit =
4669
				dev_priv->rps.min_freq;
4732
				dev_priv->rps.min_freq;
4670
	}
4733
	}
4671
}
4734
}
-
 
4735
 
4672
 
4736
/* See the Gen9_GT_PM_Programming_Guide doc for the below */
4673
static void gen9_enable_rps(struct drm_device *dev)
4737
static void gen9_enable_rps(struct drm_device *dev)
4674
{
4738
{
4675
	struct drm_i915_private *dev_priv = dev->dev_private;
4739
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
4740
 
-
 
4741
	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
-
 
4742
 
-
 
4743
	gen6_init_rps_frequencies(dev);
-
 
4744
 
-
 
4745
	/* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
-
 
4746
	if (IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0)) {
-
 
4747
		intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
-
 
4748
		return;
-
 
4749
	}
-
 
4750
 
-
 
4751
	/* Program defaults and thresholds for RPS*/
-
 
4752
	I915_WRITE(GEN6_RC_VIDEO_FREQ,
-
 
4753
		GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
-
 
4754
 
-
 
4755
	/* 1 second timeout*/
-
 
4756
	I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
-
 
4757
		GT_INTERVAL_FROM_US(dev_priv, 1000000));
-
 
4758
 
-
 
4759
	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
-
 
4760
 
-
 
4761
	/* Leaning on the below call to gen6_set_rps to program/setup the
-
 
4762
	 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
-
 
4763
	 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
-
 
4764
	dev_priv->rps.power = HIGH_POWER; /* force a reset */
-
 
4765
	gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
-
 
4766
 
-
 
4767
	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
-
 
4768
}
-
 
4769
 
-
 
4770
static void gen9_enable_rc6(struct drm_device *dev)
-
 
4771
{
-
 
4772
	struct drm_i915_private *dev_priv = dev->dev_private;
4676
	struct intel_engine_cs *ring;
4773
	struct intel_engine_cs *ring;
4677
	uint32_t rc6_mask = 0;
4774
	uint32_t rc6_mask = 0;
4678
	int unused;
4775
	int unused;
4679
 
4776
 
4680
	/* 1a: Software RC state - RC0 */
4777
	/* 1a: Software RC state - RC0 */
4681
	I915_WRITE(GEN6_RC_STATE, 0);
4778
	I915_WRITE(GEN6_RC_STATE, 0);
4682
 
4779
 
4683
	/* 1b: Get forcewake during program sequence. Although the driver
4780
	/* 1b: Get forcewake during program sequence. Although the driver
4684
	 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4781
	 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4685
	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4782
	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4686
 
4783
 
4687
	/* 2a: Disable RC states. */
4784
	/* 2a: Disable RC states. */
4688
	I915_WRITE(GEN6_RC_CONTROL, 0);
4785
	I915_WRITE(GEN6_RC_CONTROL, 0);
4689
 
4786
 
4690
	/* 2b: Program RC6 thresholds.*/
4787
	/* 2b: Program RC6 thresholds.*/
-
 
4788
 
-
 
4789
	/* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
-
 
4790
	if (IS_SKYLAKE(dev))
-
 
4791
		I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
-
 
4792
	else
4691
	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4793
		I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4692
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4794
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4693
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4795
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4694
	for_each_ring(ring, dev_priv, unused)
4796
	for_each_ring(ring, dev_priv, unused)
4695
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4797
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
-
 
4798
 
-
 
4799
	if (HAS_GUC_UCODE(dev))
-
 
4800
		I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
-
 
4801
 
4696
	I915_WRITE(GEN6_RC_SLEEP, 0);
4802
	I915_WRITE(GEN6_RC_SLEEP, 0);
-
 
4803
 
-
 
4804
	/* 2c: Program Coarse Power Gating Policies. */
-
 
4805
	I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4697
	I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4806
	I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4698
 
4807
 
4699
	/* 3a: Enable RC6 */
4808
	/* 3a: Enable RC6 */
4700
	if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4809
	if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4701
		rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4810
		rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4702
	DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4811
	DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4703
			"on" : "off");
4812
			"on" : "off");
-
 
4813
	/* WaRsUseTimeoutMode */
-
 
4814
	if ((IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_D0) ||
-
 
4815
	    (IS_BROXTON(dev) && INTEL_REVID(dev) <= BXT_REVID_A0)) {
-
 
4816
		I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
-
 
4817
		I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
-
 
4818
			   GEN7_RC_CTL_TO_MODE |
-
 
4819
			   rc6_mask);
-
 
4820
	} else {
-
 
4821
		I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4704
	I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4822
		I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4705
				   GEN6_RC_CTL_EI_MODE(1) |
4823
			   GEN6_RC_CTL_EI_MODE(1) |
4706
				   rc6_mask);
4824
			   rc6_mask);
-
 
4825
	}
-
 
4826
 
-
 
4827
	/*
-
 
4828
	 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
-
 
4829
	 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
-
 
4830
	 */
-
 
4831
	if ((IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0)) ||
-
 
4832
	    ((IS_SKL_GT3(dev) || IS_SKL_GT4(dev)) && (INTEL_REVID(dev) <= SKL_REVID_F0)))
-
 
4833
		I915_WRITE(GEN9_PG_ENABLE, 0);
-
 
4834
	else
-
 
4835
		I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
-
 
4836
				(GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
4707
 
4837
 
4708
	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4838
	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4709
 
4839
 
4710
}
4840
}
4711
 
4841
 
4712
static void gen8_enable_rps(struct drm_device *dev)
4842
static void gen8_enable_rps(struct drm_device *dev)
4713
{
4843
{
4714
	struct drm_i915_private *dev_priv = dev->dev_private;
4844
	struct drm_i915_private *dev_priv = dev->dev_private;
4715
	struct intel_engine_cs *ring;
4845
	struct intel_engine_cs *ring;
4716
	uint32_t rc6_mask = 0;
4846
	uint32_t rc6_mask = 0;
4717
	int unused;
4847
	int unused;
4718
 
4848
 
4719
	/* 1a: Software RC state - RC0 */
4849
	/* 1a: Software RC state - RC0 */
4720
	I915_WRITE(GEN6_RC_STATE, 0);
4850
	I915_WRITE(GEN6_RC_STATE, 0);
4721
 
4851
 
4722
	/* 1c & 1d: Get forcewake during program sequence. Although the driver
4852
	/* 1c & 1d: Get forcewake during program sequence. Although the driver
4723
	 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4853
	 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4724
	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4854
	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4725
 
4855
 
4726
	/* 2a: Disable RC states. */
4856
	/* 2a: Disable RC states. */
4727
	I915_WRITE(GEN6_RC_CONTROL, 0);
4857
	I915_WRITE(GEN6_RC_CONTROL, 0);
4728
 
4858
 
4729
	/* Initialize rps frequencies */
4859
	/* Initialize rps frequencies */
4730
	gen6_init_rps_frequencies(dev);
4860
	gen6_init_rps_frequencies(dev);
4731
 
4861
 
4732
	/* 2b: Program RC6 thresholds.*/
4862
	/* 2b: Program RC6 thresholds.*/
4733
	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4863
	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4734
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4864
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4735
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4865
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4736
	for_each_ring(ring, dev_priv, unused)
4866
	for_each_ring(ring, dev_priv, unused)
4737
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4867
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4738
	I915_WRITE(GEN6_RC_SLEEP, 0);
4868
	I915_WRITE(GEN6_RC_SLEEP, 0);
4739
	if (IS_BROADWELL(dev))
4869
	if (IS_BROADWELL(dev))
4740
		I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4870
		I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4741
	else
4871
	else
4742
	I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4872
		I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4743
 
4873
 
4744
	/* 3: Enable RC6 */
4874
	/* 3: Enable RC6 */
4745
	if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4875
	if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4746
		rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4876
		rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4747
	intel_print_rc6_info(dev, rc6_mask);
4877
	intel_print_rc6_info(dev, rc6_mask);
4748
	if (IS_BROADWELL(dev))
4878
	if (IS_BROADWELL(dev))
4749
		I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4879
		I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4750
				GEN7_RC_CTL_TO_MODE |
4880
				GEN7_RC_CTL_TO_MODE |
4751
				rc6_mask);
4881
				rc6_mask);
4752
	else
4882
	else
4753
	I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4883
		I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4754
			GEN6_RC_CTL_EI_MODE(1) |
4884
				GEN6_RC_CTL_EI_MODE(1) |
4755
			rc6_mask);
4885
				rc6_mask);
4756
 
4886
 
4757
	/* 4 Program defaults and thresholds for RPS*/
4887
	/* 4 Program defaults and thresholds for RPS*/
4758
	I915_WRITE(GEN6_RPNSWREQ,
4888
	I915_WRITE(GEN6_RPNSWREQ,
4759
		   HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4889
		   HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4760
	I915_WRITE(GEN6_RC_VIDEO_FREQ,
4890
	I915_WRITE(GEN6_RC_VIDEO_FREQ,
4761
		   HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4891
		   HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4762
	/* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4892
	/* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4763
	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
4893
	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
4764
 
4894
 
4765
	/* Docs recommend 900MHz, and 300 MHz respectively */
4895
	/* Docs recommend 900MHz, and 300 MHz respectively */
4766
	I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4896
	I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4767
		   dev_priv->rps.max_freq_softlimit << 24 |
4897
		   dev_priv->rps.max_freq_softlimit << 24 |
4768
		   dev_priv->rps.min_freq_softlimit << 16);
4898
		   dev_priv->rps.min_freq_softlimit << 16);
4769
 
4899
 
4770
	I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4900
	I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4771
	I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4901
	I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4772
	I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4902
	I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4773
	I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
4903
	I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
4774
 
4904
 
4775
	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4905
	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4776
 
4906
 
4777
	/* 5: Enable RPS */
4907
	/* 5: Enable RPS */
4778
	I915_WRITE(GEN6_RP_CONTROL,
4908
	I915_WRITE(GEN6_RP_CONTROL,
4779
		   GEN6_RP_MEDIA_TURBO |
4909
		   GEN6_RP_MEDIA_TURBO |
4780
		   GEN6_RP_MEDIA_HW_NORMAL_MODE |
4910
		   GEN6_RP_MEDIA_HW_NORMAL_MODE |
4781
		   GEN6_RP_MEDIA_IS_GFX |
4911
		   GEN6_RP_MEDIA_IS_GFX |
4782
		   GEN6_RP_ENABLE |
4912
		   GEN6_RP_ENABLE |
4783
		   GEN6_RP_UP_BUSY_AVG |
4913
		   GEN6_RP_UP_BUSY_AVG |
4784
		   GEN6_RP_DOWN_IDLE_AVG);
4914
		   GEN6_RP_DOWN_IDLE_AVG);
4785
 
4915
 
4786
	/* 6: Ring frequency + overclocking (our driver does this later */
4916
	/* 6: Ring frequency + overclocking (our driver does this later */
4787
 
4917
 
4788
	dev_priv->rps.power = HIGH_POWER; /* force a reset */
4918
	dev_priv->rps.power = HIGH_POWER; /* force a reset */
4789
	gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4919
	gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
4790
 
4920
 
4791
	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
4921
	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4792
}
4922
}
4793
 
4923
 
4794
static void gen6_enable_rps(struct drm_device *dev)
4924
static void gen6_enable_rps(struct drm_device *dev)
4795
{
4925
{
4796
	struct drm_i915_private *dev_priv = dev->dev_private;
4926
	struct drm_i915_private *dev_priv = dev->dev_private;
4797
	struct intel_engine_cs *ring;
4927
	struct intel_engine_cs *ring;
4798
	u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
4928
	u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
4799
	u32 gtfifodbg;
4929
	u32 gtfifodbg;
4800
	int rc6_mode;
4930
	int rc6_mode;
4801
	int i, ret;
4931
	int i, ret;
4802
 
4932
 
4803
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4933
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4804
 
4934
 
4805
	/* Here begins a magic sequence of register writes to enable
4935
	/* Here begins a magic sequence of register writes to enable
4806
	 * auto-downclocking.
4936
	 * auto-downclocking.
4807
	 *
4937
	 *
4808
	 * Perhaps there might be some value in exposing these to
4938
	 * Perhaps there might be some value in exposing these to
4809
	 * userspace...
4939
	 * userspace...
4810
	 */
4940
	 */
4811
	I915_WRITE(GEN6_RC_STATE, 0);
4941
	I915_WRITE(GEN6_RC_STATE, 0);
4812
 
4942
 
4813
	/* Clear the DBG now so we don't confuse earlier errors */
4943
	/* Clear the DBG now so we don't confuse earlier errors */
4814
	if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4944
	if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4815
		DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4945
		DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4816
		I915_WRITE(GTFIFODBG, gtfifodbg);
4946
		I915_WRITE(GTFIFODBG, gtfifodbg);
4817
	}
4947
	}
4818
 
4948
 
4819
	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
4949
	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4820
 
4950
 
4821
	/* Initialize rps frequencies */
4951
	/* Initialize rps frequencies */
4822
	gen6_init_rps_frequencies(dev);
4952
	gen6_init_rps_frequencies(dev);
4823
 
4953
 
4824
	/* disable the counters and set deterministic thresholds */
4954
	/* disable the counters and set deterministic thresholds */
4825
	I915_WRITE(GEN6_RC_CONTROL, 0);
4955
	I915_WRITE(GEN6_RC_CONTROL, 0);
4826
 
4956
 
4827
	I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4957
	I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4828
	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4958
	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4829
	I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4959
	I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4830
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4960
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4831
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4961
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4832
 
4962
 
4833
	for_each_ring(ring, dev_priv, i)
4963
	for_each_ring(ring, dev_priv, i)
4834
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4964
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4835
 
4965
 
4836
	I915_WRITE(GEN6_RC_SLEEP, 0);
4966
	I915_WRITE(GEN6_RC_SLEEP, 0);
4837
	I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
4967
	I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
4838
	if (IS_IVYBRIDGE(dev))
4968
	if (IS_IVYBRIDGE(dev))
4839
		I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4969
		I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4840
	else
4970
	else
4841
	I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
4971
		I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
4842
	I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
4972
	I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
4843
	I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4973
	I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4844
 
4974
 
4845
	/* Check if we are enabling RC6 */
4975
	/* Check if we are enabling RC6 */
4846
	rc6_mode = intel_enable_rc6(dev_priv->dev);
4976
	rc6_mode = intel_enable_rc6(dev_priv->dev);
4847
	if (rc6_mode & INTEL_RC6_ENABLE)
4977
	if (rc6_mode & INTEL_RC6_ENABLE)
4848
		rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4978
		rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4849
 
4979
 
4850
	/* We don't use those on Haswell */
4980
	/* We don't use those on Haswell */
4851
	if (!IS_HASWELL(dev)) {
4981
	if (!IS_HASWELL(dev)) {
4852
		if (rc6_mode & INTEL_RC6p_ENABLE)
4982
		if (rc6_mode & INTEL_RC6p_ENABLE)
4853
			rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
4983
			rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
4854
 
4984
 
4855
		if (rc6_mode & INTEL_RC6pp_ENABLE)
4985
		if (rc6_mode & INTEL_RC6pp_ENABLE)
4856
			rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4986
			rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4857
	}
4987
	}
4858
 
4988
 
4859
	intel_print_rc6_info(dev, rc6_mask);
4989
	intel_print_rc6_info(dev, rc6_mask);
4860
 
4990
 
4861
	I915_WRITE(GEN6_RC_CONTROL,
4991
	I915_WRITE(GEN6_RC_CONTROL,
4862
		   rc6_mask |
4992
		   rc6_mask |
4863
		   GEN6_RC_CTL_EI_MODE(1) |
4993
		   GEN6_RC_CTL_EI_MODE(1) |
4864
		   GEN6_RC_CTL_HW_ENABLE);
4994
		   GEN6_RC_CTL_HW_ENABLE);
4865
 
4995
 
4866
	/* Power down if completely idle for over 50ms */
4996
	/* Power down if completely idle for over 50ms */
4867
	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
4997
	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
4868
	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4998
	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4869
 
4999
 
4870
	ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
5000
	ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
4871
	if (ret)
5001
	if (ret)
4872
		DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
5002
		DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
4873
 
5003
 
4874
		ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
5004
	ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4875
		if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
5005
	if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4876
			DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
5006
		DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
4877
				 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
5007
				 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
4878
					 (pcu_mbox & 0xff) * 50);
5008
				 (pcu_mbox & 0xff) * 50);
4879
		dev_priv->rps.max_freq = pcu_mbox & 0xff;
5009
		dev_priv->rps.max_freq = pcu_mbox & 0xff;
4880
	}
5010
	}
4881
 
5011
 
4882
	dev_priv->rps.power = HIGH_POWER; /* force a reset */
5012
	dev_priv->rps.power = HIGH_POWER; /* force a reset */
4883
	gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
5013
	gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
4884
 
5014
 
4885
	rc6vids = 0;
5015
	rc6vids = 0;
4886
	ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
5016
	ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4887
	if (IS_GEN6(dev) && ret) {
5017
	if (IS_GEN6(dev) && ret) {
4888
		DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
5018
		DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4889
	} else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
5019
	} else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4890
		DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5020
		DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4891
			  GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5021
			  GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4892
		rc6vids &= 0xffff00;
5022
		rc6vids &= 0xffff00;
4893
		rc6vids |= GEN6_ENCODE_RC6_VID(450);
5023
		rc6vids |= GEN6_ENCODE_RC6_VID(450);
4894
		ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5024
		ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4895
		if (ret)
5025
		if (ret)
4896
			DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5026
			DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4897
	}
5027
	}
4898
 
5028
 
4899
	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
5029
	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4900
}
5030
}
4901
 
5031
 
4902
static void __gen6_update_ring_freq(struct drm_device *dev)
5032
static void __gen6_update_ring_freq(struct drm_device *dev)
4903
{
5033
{
4904
	struct drm_i915_private *dev_priv = dev->dev_private;
5034
	struct drm_i915_private *dev_priv = dev->dev_private;
4905
	int min_freq = 15;
5035
	int min_freq = 15;
4906
	unsigned int gpu_freq;
5036
	unsigned int gpu_freq;
4907
	unsigned int max_ia_freq, min_ring_freq;
5037
	unsigned int max_ia_freq, min_ring_freq;
-
 
5038
	unsigned int max_gpu_freq, min_gpu_freq;
4908
	int scaling_factor = 180;
5039
	int scaling_factor = 180;
4909
	struct cpufreq_policy *policy;
5040
	struct cpufreq_policy *policy;
4910
 
5041
 
4911
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5042
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4912
 
5043
 
4913
	max_ia_freq = cpufreq_quick_get_max(0);
5044
	max_ia_freq = cpufreq_quick_get_max(0);
4914
	/*
5045
	/*
4915
		 * Default to measured freq if none found, PCU will ensure we
5046
		 * Default to measured freq if none found, PCU will ensure we
4916
		 * don't go over
5047
		 * don't go over
4917
	 */
5048
	 */
4918
		max_ia_freq = tsc_khz;
5049
		max_ia_freq = tsc_khz;
4919
 
5050
 
4920
	/* Convert from kHz to MHz */
5051
	/* Convert from kHz to MHz */
4921
	max_ia_freq /= 1000;
5052
	max_ia_freq /= 1000;
4922
 
5053
 
4923
	min_ring_freq = I915_READ(DCLK) & 0xf;
5054
	min_ring_freq = I915_READ(DCLK) & 0xf;
4924
	/* convert DDR frequency from units of 266.6MHz to bandwidth */
5055
	/* convert DDR frequency from units of 266.6MHz to bandwidth */
4925
	min_ring_freq = mult_frac(min_ring_freq, 8, 3);
5056
	min_ring_freq = mult_frac(min_ring_freq, 8, 3);
-
 
5057
 
-
 
5058
	if (IS_SKYLAKE(dev)) {
-
 
5059
		/* Convert GT frequency to 50 HZ units */
-
 
5060
		min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
-
 
5061
		max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
-
 
5062
	} else {
-
 
5063
		min_gpu_freq = dev_priv->rps.min_freq;
-
 
5064
		max_gpu_freq = dev_priv->rps.max_freq;
-
 
5065
	}
4926
 
5066
 
4927
	/*
5067
	/*
4928
	 * For each potential GPU frequency, load a ring frequency we'd like
5068
	 * For each potential GPU frequency, load a ring frequency we'd like
4929
	 * to use for memory access.  We do this by specifying the IA frequency
5069
	 * to use for memory access.  We do this by specifying the IA frequency
4930
	 * the PCU should use as a reference to determine the ring frequency.
5070
	 * the PCU should use as a reference to determine the ring frequency.
4931
	 */
5071
	 */
4932
	for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
5072
	for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
4933
	     gpu_freq--) {
-
 
4934
		int diff = dev_priv->rps.max_freq - gpu_freq;
5073
		int diff = max_gpu_freq - gpu_freq;
4935
		unsigned int ia_freq = 0, ring_freq = 0;
5074
		unsigned int ia_freq = 0, ring_freq = 0;
-
 
5075
 
-
 
5076
		if (IS_SKYLAKE(dev)) {
-
 
5077
			/*
-
 
5078
			 * ring_freq = 2 * GT. ring_freq is in 100MHz units
-
 
5079
			 * No floor required for ring frequency on SKL.
-
 
5080
			 */
4936
 
5081
			ring_freq = gpu_freq;
4937
		if (INTEL_INFO(dev)->gen >= 8) {
5082
		} else if (INTEL_INFO(dev)->gen >= 8) {
4938
			/* max(2 * GT, DDR). NB: GT is 50MHz units */
5083
			/* max(2 * GT, DDR). NB: GT is 50MHz units */
4939
			ring_freq = max(min_ring_freq, gpu_freq);
5084
			ring_freq = max(min_ring_freq, gpu_freq);
4940
		} else if (IS_HASWELL(dev)) {
5085
		} else if (IS_HASWELL(dev)) {
4941
			ring_freq = mult_frac(gpu_freq, 5, 4);
5086
			ring_freq = mult_frac(gpu_freq, 5, 4);
4942
			ring_freq = max(min_ring_freq, ring_freq);
5087
			ring_freq = max(min_ring_freq, ring_freq);
4943
			/* leave ia_freq as the default, chosen by cpufreq */
5088
			/* leave ia_freq as the default, chosen by cpufreq */
4944
		} else {
5089
		} else {
4945
			/* On older processors, there is no separate ring
5090
			/* On older processors, there is no separate ring
4946
			 * clock domain, so in order to boost the bandwidth
5091
			 * clock domain, so in order to boost the bandwidth
4947
			 * of the ring, we need to upclock the CPU (ia_freq).
5092
			 * of the ring, we need to upclock the CPU (ia_freq).
4948
			 *
5093
			 *
4949
			 * For GPU frequencies less than 750MHz,
5094
			 * For GPU frequencies less than 750MHz,
4950
			 * just use the lowest ring freq.
5095
			 * just use the lowest ring freq.
4951
		 */
5096
			 */
4952
		if (gpu_freq < min_freq)
5097
			if (gpu_freq < min_freq)
4953
			ia_freq = 800;
5098
				ia_freq = 800;
4954
		else
5099
			else
4955
			ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5100
				ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4956
		ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5101
			ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4957
		}
5102
		}
4958
 
5103
 
4959
		sandybridge_pcode_write(dev_priv,
5104
		sandybridge_pcode_write(dev_priv,
4960
					GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
5105
					GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
4961
					ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5106
					ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4962
					ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5107
					ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4963
					gpu_freq);
5108
					gpu_freq);
4964
	}
5109
	}
4965
}
5110
}
4966
 
5111
 
4967
void gen6_update_ring_freq(struct drm_device *dev)
5112
void gen6_update_ring_freq(struct drm_device *dev)
4968
{
5113
{
4969
	struct drm_i915_private *dev_priv = dev->dev_private;
5114
	struct drm_i915_private *dev_priv = dev->dev_private;
4970
 
5115
 
4971
	if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
5116
	if (!HAS_CORE_RING_FREQ(dev))
4972
		return;
5117
		return;
4973
 
5118
 
4974
	mutex_lock(&dev_priv->rps.hw_lock);
5119
	mutex_lock(&dev_priv->rps.hw_lock);
4975
	__gen6_update_ring_freq(dev);
5120
	__gen6_update_ring_freq(dev);
4976
	mutex_unlock(&dev_priv->rps.hw_lock);
5121
	mutex_unlock(&dev_priv->rps.hw_lock);
4977
}
5122
}
4978
 
5123
 
4979
static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
5124
static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
4980
{
5125
{
-
 
5126
	struct drm_device *dev = dev_priv->dev;
4981
	u32 val, rp0;
5127
	u32 val, rp0;
4982
 
5128
 
-
 
5129
	val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
-
 
5130
 
-
 
5131
	switch (INTEL_INFO(dev)->eu_total) {
-
 
5132
	case 8:
-
 
5133
		/* (2 * 4) config */
-
 
5134
		rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
-
 
5135
		break;
-
 
5136
	case 12:
-
 
5137
		/* (2 * 6) config */
-
 
5138
		rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
-
 
5139
		break;
-
 
5140
	case 16:
-
 
5141
		/* (2 * 8) config */
-
 
5142
	default:
4983
	val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
5143
		/* Setting (2 * 8) Min RP0 for any other combination */
-
 
5144
		rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
-
 
5145
		break;
-
 
5146
	}
-
 
5147
 
4984
	rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
5148
	rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4985
 
5149
 
4986
	return rp0;
5150
	return rp0;
4987
}
5151
}
4988
 
5152
 
4989
static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5153
static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4990
{
5154
{
4991
	u32 val, rpe;
5155
	u32 val, rpe;
4992
 
5156
 
4993
	val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5157
	val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4994
	rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5158
	rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4995
 
5159
 
4996
	return rpe;
5160
	return rpe;
4997
}
5161
}
4998
 
5162
 
4999
static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5163
static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5000
{
5164
{
5001
	u32 val, rp1;
5165
	u32 val, rp1;
5002
 
5166
 
5003
	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5167
	val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5004
	rp1 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) & PUNIT_GPU_STATUS_MAX_FREQ_MASK;
5168
	rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5005
 
5169
 
5006
	return rp1;
5170
	return rp1;
5007
}
5171
}
5008
 
-
 
5009
static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
-
 
5010
{
-
 
5011
	u32 val, rpn;
-
 
5012
 
-
 
5013
	val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
-
 
5014
	rpn = (val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) & PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK;
-
 
5015
	return rpn;
-
 
5016
}
-
 
5017
 
5172
 
5018
static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5173
static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5019
{
5174
{
5020
	u32 val, rp1;
5175
	u32 val, rp1;
5021
 
5176
 
5022
	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5177
	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5023
 
5178
 
5024
	rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5179
	rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5025
 
5180
 
5026
	return rp1;
5181
	return rp1;
5027
}
5182
}
5028
 
5183
 
5029
static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
5184
static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
5030
{
5185
{
5031
	u32 val, rp0;
5186
	u32 val, rp0;
5032
 
5187
 
5033
	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5188
	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5034
 
5189
 
5035
	rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5190
	rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5036
	/* Clamp to max */
5191
	/* Clamp to max */
5037
	rp0 = min_t(u32, rp0, 0xea);
5192
	rp0 = min_t(u32, rp0, 0xea);
5038
 
5193
 
5039
	return rp0;
5194
	return rp0;
5040
}
5195
}
5041
 
5196
 
5042
static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5197
static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5043
{
5198
{
5044
	u32 val, rpe;
5199
	u32 val, rpe;
5045
 
5200
 
5046
	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
5201
	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
5047
	rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
5202
	rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
5048
	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
5203
	val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
5049
	rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5204
	rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5050
 
5205
 
5051
	return rpe;
5206
	return rpe;
5052
}
5207
}
5053
 
5208
 
5054
static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
5209
static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
5055
{
5210
{
5056
	return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5211
	return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5057
}
5212
}
5058
 
5213
 
5059
/* Check that the pctx buffer wasn't move under us. */
5214
/* Check that the pctx buffer wasn't move under us. */
5060
static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5215
static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5061
{
5216
{
5062
	unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5217
	unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5063
 
5218
 
5064
	WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5219
	WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5065
			     dev_priv->vlv_pctx->stolen->start);
5220
			     dev_priv->vlv_pctx->stolen->start);
5066
}
5221
}
5067
 
5222
 
5068
 
5223
 
5069
/* Check that the pcbr address is not empty. */
5224
/* Check that the pcbr address is not empty. */
5070
static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5225
static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5071
{
5226
{
5072
	unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5227
	unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5073
 
5228
 
5074
	WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5229
	WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5075
}
5230
}
5076
 
5231
 
5077
static void cherryview_setup_pctx(struct drm_device *dev)
5232
static void cherryview_setup_pctx(struct drm_device *dev)
5078
{
5233
{
5079
	struct drm_i915_private *dev_priv = dev->dev_private;
5234
	struct drm_i915_private *dev_priv = dev->dev_private;
5080
	unsigned long pctx_paddr, paddr;
5235
	unsigned long pctx_paddr, paddr;
5081
	struct i915_gtt *gtt = &dev_priv->gtt;
5236
	struct i915_gtt *gtt = &dev_priv->gtt;
5082
	u32 pcbr;
5237
	u32 pcbr;
5083
	int pctx_size = 32*1024;
5238
	int pctx_size = 32*1024;
5084
 
5239
 
5085
	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5240
	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5086
 
5241
 
5087
	pcbr = I915_READ(VLV_PCBR);
5242
	pcbr = I915_READ(VLV_PCBR);
5088
	if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
5243
	if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
5089
		DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5244
		DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5090
		paddr = (dev_priv->mm.stolen_base +
5245
		paddr = (dev_priv->mm.stolen_base +
5091
			 (gtt->stolen_size - pctx_size));
5246
			 (gtt->stolen_size - pctx_size));
5092
 
5247
 
5093
		pctx_paddr = (paddr & (~4095));
5248
		pctx_paddr = (paddr & (~4095));
5094
		I915_WRITE(VLV_PCBR, pctx_paddr);
5249
		I915_WRITE(VLV_PCBR, pctx_paddr);
5095
	}
5250
	}
5096
 
5251
 
5097
	DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5252
	DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5098
}
5253
}
5099
 
5254
 
5100
static void valleyview_setup_pctx(struct drm_device *dev)
5255
static void valleyview_setup_pctx(struct drm_device *dev)
5101
{
5256
{
5102
	struct drm_i915_private *dev_priv = dev->dev_private;
5257
	struct drm_i915_private *dev_priv = dev->dev_private;
5103
	struct drm_i915_gem_object *pctx;
5258
	struct drm_i915_gem_object *pctx;
5104
	unsigned long pctx_paddr;
5259
	unsigned long pctx_paddr;
5105
	u32 pcbr;
5260
	u32 pcbr;
5106
	int pctx_size = 24*1024;
5261
	int pctx_size = 24*1024;
5107
 
5262
 
5108
	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5263
	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5109
 
5264
 
5110
	pcbr = I915_READ(VLV_PCBR);
5265
	pcbr = I915_READ(VLV_PCBR);
5111
	if (pcbr) {
5266
	if (pcbr) {
5112
		/* BIOS set it up already, grab the pre-alloc'd space */
5267
		/* BIOS set it up already, grab the pre-alloc'd space */
5113
		int pcbr_offset;
5268
		int pcbr_offset;
5114
 
5269
 
5115
		pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5270
		pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5116
		pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5271
		pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5117
								      pcbr_offset,
5272
								      pcbr_offset,
5118
								      I915_GTT_OFFSET_NONE,
5273
								      I915_GTT_OFFSET_NONE,
5119
								      pctx_size);
5274
								      pctx_size);
5120
		goto out;
5275
		goto out;
5121
	}
5276
	}
5122
 
5277
 
5123
	DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5278
	DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5124
 
5279
 
5125
	/*
5280
	/*
5126
	 * From the Gunit register HAS:
5281
	 * From the Gunit register HAS:
5127
	 * The Gfx driver is expected to program this register and ensure
5282
	 * The Gfx driver is expected to program this register and ensure
5128
	 * proper allocation within Gfx stolen memory.  For example, this
5283
	 * proper allocation within Gfx stolen memory.  For example, this
5129
	 * register should be programmed such than the PCBR range does not
5284
	 * register should be programmed such than the PCBR range does not
5130
	 * overlap with other ranges, such as the frame buffer, protected
5285
	 * overlap with other ranges, such as the frame buffer, protected
5131
	 * memory, or any other relevant ranges.
5286
	 * memory, or any other relevant ranges.
5132
	 */
5287
	 */
5133
	pctx = i915_gem_object_create_stolen(dev, pctx_size);
5288
	pctx = i915_gem_object_create_stolen(dev, pctx_size);
5134
	if (!pctx) {
5289
	if (!pctx) {
5135
		DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
5290
		DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
5136
		return;
5291
		return;
5137
	}
5292
	}
5138
 
5293
 
5139
	pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5294
	pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5140
	I915_WRITE(VLV_PCBR, pctx_paddr);
5295
	I915_WRITE(VLV_PCBR, pctx_paddr);
5141
 
5296
 
5142
out:
5297
out:
5143
	DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5298
	DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5144
	dev_priv->vlv_pctx = pctx;
5299
	dev_priv->vlv_pctx = pctx;
5145
}
5300
}
5146
 
5301
 
5147
static void valleyview_cleanup_pctx(struct drm_device *dev)
5302
static void valleyview_cleanup_pctx(struct drm_device *dev)
5148
{
5303
{
5149
	struct drm_i915_private *dev_priv = dev->dev_private;
5304
	struct drm_i915_private *dev_priv = dev->dev_private;
5150
 
5305
 
5151
	if (WARN_ON(!dev_priv->vlv_pctx))
5306
	if (WARN_ON(!dev_priv->vlv_pctx))
5152
		return;
5307
		return;
5153
 
5308
 
5154
	drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
5309
	drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
5155
	dev_priv->vlv_pctx = NULL;
5310
	dev_priv->vlv_pctx = NULL;
5156
}
5311
}
5157
 
5312
 
5158
static void valleyview_init_gt_powersave(struct drm_device *dev)
5313
static void valleyview_init_gt_powersave(struct drm_device *dev)
5159
{
5314
{
5160
	struct drm_i915_private *dev_priv = dev->dev_private;
5315
	struct drm_i915_private *dev_priv = dev->dev_private;
5161
	u32 val;
5316
	u32 val;
5162
 
5317
 
5163
	valleyview_setup_pctx(dev);
5318
	valleyview_setup_pctx(dev);
5164
 
5319
 
5165
	mutex_lock(&dev_priv->rps.hw_lock);
5320
	mutex_lock(&dev_priv->rps.hw_lock);
5166
 
5321
 
5167
	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5322
	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5168
	switch ((val >> 6) & 3) {
5323
	switch ((val >> 6) & 3) {
5169
	case 0:
5324
	case 0:
5170
	case 1:
5325
	case 1:
5171
		dev_priv->mem_freq = 800;
5326
		dev_priv->mem_freq = 800;
5172
		break;
5327
		break;
5173
	case 2:
5328
	case 2:
5174
		dev_priv->mem_freq = 1066;
5329
		dev_priv->mem_freq = 1066;
5175
		break;
5330
		break;
5176
	case 3:
5331
	case 3:
5177
		dev_priv->mem_freq = 1333;
5332
		dev_priv->mem_freq = 1333;
5178
		break;
5333
		break;
5179
	}
5334
	}
5180
	DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5335
	DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5181
 
5336
 
5182
	dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5337
	dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5183
	dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5338
	dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5184
	DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5339
	DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5185
			 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5340
			 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5186
			 dev_priv->rps.max_freq);
5341
			 dev_priv->rps.max_freq);
5187
 
5342
 
5188
	dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5343
	dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5189
	DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5344
	DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5190
			 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5345
			 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5191
			 dev_priv->rps.efficient_freq);
5346
			 dev_priv->rps.efficient_freq);
5192
 
5347
 
5193
	dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5348
	dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5194
	DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
5349
	DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
5195
			 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5350
			 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5196
			 dev_priv->rps.rp1_freq);
5351
			 dev_priv->rps.rp1_freq);
5197
 
5352
 
5198
	dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5353
	dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5199
	DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5354
	DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5200
			 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5355
			 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5201
			 dev_priv->rps.min_freq);
5356
			 dev_priv->rps.min_freq);
-
 
5357
 
-
 
5358
	dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5202
 
5359
 
5203
	/* Preserve min/max settings in case of re-init */
5360
	/* Preserve min/max settings in case of re-init */
5204
	if (dev_priv->rps.max_freq_softlimit == 0)
5361
	if (dev_priv->rps.max_freq_softlimit == 0)
5205
		dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5362
		dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5206
 
5363
 
5207
	if (dev_priv->rps.min_freq_softlimit == 0)
5364
	if (dev_priv->rps.min_freq_softlimit == 0)
5208
		dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5365
		dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5209
 
5366
 
5210
	mutex_unlock(&dev_priv->rps.hw_lock);
5367
	mutex_unlock(&dev_priv->rps.hw_lock);
5211
}
5368
}
5212
 
5369
 
5213
static void cherryview_init_gt_powersave(struct drm_device *dev)
5370
static void cherryview_init_gt_powersave(struct drm_device *dev)
5214
{
5371
{
5215
	struct drm_i915_private *dev_priv = dev->dev_private;
5372
	struct drm_i915_private *dev_priv = dev->dev_private;
5216
	u32 val;
5373
	u32 val;
5217
 
5374
 
5218
	cherryview_setup_pctx(dev);
5375
	cherryview_setup_pctx(dev);
5219
 
5376
 
5220
	mutex_lock(&dev_priv->rps.hw_lock);
5377
	mutex_lock(&dev_priv->rps.hw_lock);
5221
 
5378
 
5222
	mutex_lock(&dev_priv->dpio_lock);
5379
	mutex_lock(&dev_priv->sb_lock);
5223
	val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
5380
	val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
5224
	mutex_unlock(&dev_priv->dpio_lock);
5381
	mutex_unlock(&dev_priv->sb_lock);
5225
 
-
 
5226
	switch ((val >> 2) & 0x7) {
-
 
5227
	case 0:
-
 
5228
	case 1:
-
 
5229
		dev_priv->rps.cz_freq = 200;
-
 
5230
		dev_priv->mem_freq = 1600;
-
 
5231
		break;
-
 
5232
	case 2:
-
 
5233
		dev_priv->rps.cz_freq = 267;
-
 
5234
		dev_priv->mem_freq = 1600;
5382
 
5235
		break;
-
 
5236
	case 3:
5383
	switch ((val >> 2) & 0x7) {
5237
		dev_priv->rps.cz_freq = 333;
5384
	case 3:
5238
		dev_priv->mem_freq = 2000;
-
 
5239
		break;
-
 
5240
	case 4:
-
 
5241
		dev_priv->rps.cz_freq = 320;
-
 
5242
		dev_priv->mem_freq = 1600;
5385
		dev_priv->mem_freq = 2000;
5243
		break;
-
 
5244
	case 5:
5386
		break;
5245
		dev_priv->rps.cz_freq = 400;
5387
	default:
5246
		dev_priv->mem_freq = 1600;
5388
		dev_priv->mem_freq = 1600;
5247
		break;
5389
		break;
5248
	}
5390
	}
5249
	DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5391
	DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5250
 
5392
 
5251
	dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5393
	dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5252
	dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5394
	dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5253
	DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5395
	DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5254
			 vlv_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5396
			 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5255
			 dev_priv->rps.max_freq);
5397
			 dev_priv->rps.max_freq);
5256
 
5398
 
5257
	dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5399
	dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5258
	DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5400
	DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5259
			 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5401
			 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5260
			 dev_priv->rps.efficient_freq);
5402
			 dev_priv->rps.efficient_freq);
5261
 
5403
 
5262
	dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5404
	dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5263
	DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
5405
	DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
5264
			 vlv_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5406
			 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5265
			 dev_priv->rps.rp1_freq);
5407
			 dev_priv->rps.rp1_freq);
-
 
5408
 
5266
 
5409
	/* PUnit validated range is only [RPe, RP0] */
5267
	dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
5410
	dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
5268
	DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5411
	DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5269
			 vlv_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5412
			 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5270
			 dev_priv->rps.min_freq);
5413
			 dev_priv->rps.min_freq);
5271
 
5414
 
5272
	WARN_ONCE((dev_priv->rps.max_freq |
5415
	WARN_ONCE((dev_priv->rps.max_freq |
5273
		   dev_priv->rps.efficient_freq |
5416
		   dev_priv->rps.efficient_freq |
5274
		   dev_priv->rps.rp1_freq |
5417
		   dev_priv->rps.rp1_freq |
5275
		   dev_priv->rps.min_freq) & 1,
5418
		   dev_priv->rps.min_freq) & 1,
5276
		  "Odd GPU freq values\n");
5419
		  "Odd GPU freq values\n");
-
 
5420
 
-
 
5421
	dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5277
 
5422
 
5278
	/* Preserve min/max settings in case of re-init */
5423
	/* Preserve min/max settings in case of re-init */
5279
	if (dev_priv->rps.max_freq_softlimit == 0)
5424
	if (dev_priv->rps.max_freq_softlimit == 0)
5280
		dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5425
		dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5281
 
5426
 
5282
	if (dev_priv->rps.min_freq_softlimit == 0)
5427
	if (dev_priv->rps.min_freq_softlimit == 0)
5283
		dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5428
		dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5284
 
5429
 
5285
	mutex_unlock(&dev_priv->rps.hw_lock);
5430
	mutex_unlock(&dev_priv->rps.hw_lock);
5286
}
5431
}
5287
 
5432
 
5288
static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5433
static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5289
{
5434
{
5290
	valleyview_cleanup_pctx(dev);
5435
	valleyview_cleanup_pctx(dev);
5291
}
5436
}
5292
 
5437
 
5293
static void cherryview_enable_rps(struct drm_device *dev)
5438
static void cherryview_enable_rps(struct drm_device *dev)
5294
{
5439
{
5295
	struct drm_i915_private *dev_priv = dev->dev_private;
5440
	struct drm_i915_private *dev_priv = dev->dev_private;
5296
	struct intel_engine_cs *ring;
5441
	struct intel_engine_cs *ring;
5297
	u32 gtfifodbg, val, rc6_mode = 0, pcbr;
5442
	u32 gtfifodbg, val, rc6_mode = 0, pcbr;
5298
	int i;
5443
	int i;
5299
 
5444
 
5300
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5445
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5301
 
5446
 
5302
	gtfifodbg = I915_READ(GTFIFODBG);
5447
	gtfifodbg = I915_READ(GTFIFODBG);
5303
	if (gtfifodbg) {
5448
	if (gtfifodbg) {
5304
		DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5449
		DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5305
				 gtfifodbg);
5450
				 gtfifodbg);
5306
		I915_WRITE(GTFIFODBG, gtfifodbg);
5451
		I915_WRITE(GTFIFODBG, gtfifodbg);
5307
	}
5452
	}
5308
 
5453
 
5309
	cherryview_check_pctx(dev_priv);
5454
	cherryview_check_pctx(dev_priv);
5310
 
5455
 
5311
	/* 1a & 1b: Get forcewake during program sequence. Although the driver
5456
	/* 1a & 1b: Get forcewake during program sequence. Although the driver
5312
	 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
5457
	 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
5313
	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
5458
	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
-
 
5459
 
-
 
5460
	/*  Disable RC states. */
-
 
5461
	I915_WRITE(GEN6_RC_CONTROL, 0);
5314
 
5462
 
5315
	/* 2a: Program RC6 thresholds.*/
5463
	/* 2a: Program RC6 thresholds.*/
5316
	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5464
	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5317
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5465
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5318
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5466
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5319
 
5467
 
5320
	for_each_ring(ring, dev_priv, i)
5468
	for_each_ring(ring, dev_priv, i)
5321
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5469
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5322
	I915_WRITE(GEN6_RC_SLEEP, 0);
5470
	I915_WRITE(GEN6_RC_SLEEP, 0);
-
 
5471
 
5323
 
5472
	/* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5324
	I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
5473
	I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
5325
 
5474
 
5326
	/* allows RC6 residency counter to work */
5475
	/* allows RC6 residency counter to work */
5327
	I915_WRITE(VLV_COUNTER_CONTROL,
5476
	I915_WRITE(VLV_COUNTER_CONTROL,
5328
		   _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5477
		   _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5329
				      VLV_MEDIA_RC6_COUNT_EN |
5478
				      VLV_MEDIA_RC6_COUNT_EN |
5330
				      VLV_RENDER_RC6_COUNT_EN));
5479
				      VLV_RENDER_RC6_COUNT_EN));
5331
 
5480
 
5332
	/* For now we assume BIOS is allocating and populating the PCBR  */
5481
	/* For now we assume BIOS is allocating and populating the PCBR  */
5333
	pcbr = I915_READ(VLV_PCBR);
5482
	pcbr = I915_READ(VLV_PCBR);
5334
 
5483
 
5335
	/* 3: Enable RC6 */
5484
	/* 3: Enable RC6 */
5336
	if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5485
	if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5337
						(pcbr >> VLV_PCBR_ADDR_SHIFT))
5486
						(pcbr >> VLV_PCBR_ADDR_SHIFT))
5338
		rc6_mode = GEN6_RC_CTL_EI_MODE(1);
5487
		rc6_mode = GEN7_RC_CTL_TO_MODE;
5339
 
5488
 
5340
	I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5489
	I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5341
 
5490
 
5342
	/* 4 Program defaults and thresholds for RPS*/
5491
	/* 4 Program defaults and thresholds for RPS*/
-
 
5492
	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
5343
	I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5493
	I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5344
	I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5494
	I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5345
	I915_WRITE(GEN6_RP_UP_EI, 66000);
5495
	I915_WRITE(GEN6_RP_UP_EI, 66000);
5346
	I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5496
	I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5347
 
5497
 
5348
	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5498
	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5349
 
-
 
5350
	/* WaDisablePwrmtrEvent:chv (pre-production hw) */
-
 
5351
	I915_WRITE(0xA80C, I915_READ(0xA80C) & 0x00ffffff);
-
 
5352
	I915_WRITE(0xA810, I915_READ(0xA810) & 0xffffff00);
-
 
5353
 
5499
 
5354
	/* 5: Enable RPS */
5500
	/* 5: Enable RPS */
5355
	I915_WRITE(GEN6_RP_CONTROL,
5501
	I915_WRITE(GEN6_RP_CONTROL,
5356
		   GEN6_RP_MEDIA_HW_NORMAL_MODE |
5502
		   GEN6_RP_MEDIA_HW_NORMAL_MODE |
5357
		   GEN6_RP_MEDIA_IS_GFX | /* WaSetMaskForGfxBusyness:chv (pre-production hw ?) */
5503
		   GEN6_RP_MEDIA_IS_GFX |
5358
		   GEN6_RP_ENABLE |
5504
		   GEN6_RP_ENABLE |
5359
		   GEN6_RP_UP_BUSY_AVG |
5505
		   GEN6_RP_UP_BUSY_AVG |
5360
		   GEN6_RP_DOWN_IDLE_AVG);
5506
		   GEN6_RP_DOWN_IDLE_AVG);
-
 
5507
 
-
 
5508
	/* Setting Fixed Bias */
-
 
5509
	val = VLV_OVERRIDE_EN |
-
 
5510
		  VLV_SOC_TDP_EN |
-
 
5511
		  CHV_BIAS_CPU_50_SOC_50;
-
 
5512
	vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5361
 
5513
 
5362
	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5514
	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5363
 
5515
 
5364
	/* RPS code assumes GPLL is used */
5516
	/* RPS code assumes GPLL is used */
5365
	WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5517
	WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5366
 
5518
 
5367
	DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
5519
	DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
5368
	DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5520
	DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5369
 
5521
 
5370
	dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5522
	dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5371
	DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5523
	DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5372
			 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5524
			 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5373
			 dev_priv->rps.cur_freq);
5525
			 dev_priv->rps.cur_freq);
5374
 
5526
 
5375
	DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5527
	DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5376
			 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5528
			 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5377
			 dev_priv->rps.efficient_freq);
5529
			 dev_priv->rps.efficient_freq);
5378
 
5530
 
5379
	valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5531
	valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5380
 
5532
 
5381
	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
5533
	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5382
}
5534
}
5383
 
5535
 
5384
static void valleyview_enable_rps(struct drm_device *dev)
5536
static void valleyview_enable_rps(struct drm_device *dev)
5385
{
5537
{
5386
	struct drm_i915_private *dev_priv = dev->dev_private;
5538
	struct drm_i915_private *dev_priv = dev->dev_private;
5387
	struct intel_engine_cs *ring;
5539
	struct intel_engine_cs *ring;
5388
	u32 gtfifodbg, val, rc6_mode = 0;
5540
	u32 gtfifodbg, val, rc6_mode = 0;
5389
	int i;
5541
	int i;
5390
 
5542
 
5391
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5543
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5392
 
5544
 
5393
	valleyview_check_pctx(dev_priv);
5545
	valleyview_check_pctx(dev_priv);
5394
 
5546
 
5395
	if ((gtfifodbg = I915_READ(GTFIFODBG))) {
5547
	if ((gtfifodbg = I915_READ(GTFIFODBG))) {
5396
		DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5548
		DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5397
				 gtfifodbg);
5549
				 gtfifodbg);
5398
		I915_WRITE(GTFIFODBG, gtfifodbg);
5550
		I915_WRITE(GTFIFODBG, gtfifodbg);
5399
	}
5551
	}
5400
 
5552
 
5401
	/* If VLV, Forcewake all wells, else re-direct to regular path */
5553
	/* If VLV, Forcewake all wells, else re-direct to regular path */
5402
	gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
5554
	intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
-
 
5555
 
-
 
5556
	/*  Disable RC states. */
-
 
5557
	I915_WRITE(GEN6_RC_CONTROL, 0);
-
 
5558
 
5403
 
5559
	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
5404
	I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5560
	I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5405
	I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5561
	I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5406
	I915_WRITE(GEN6_RP_UP_EI, 66000);
5562
	I915_WRITE(GEN6_RP_UP_EI, 66000);
5407
	I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5563
	I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5408
 
5564
 
5409
	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5565
	I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5410
	I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
-
 
5411
 
5566
 
5412
	I915_WRITE(GEN6_RP_CONTROL,
5567
	I915_WRITE(GEN6_RP_CONTROL,
5413
		   GEN6_RP_MEDIA_TURBO |
5568
		   GEN6_RP_MEDIA_TURBO |
5414
		   GEN6_RP_MEDIA_HW_NORMAL_MODE |
5569
		   GEN6_RP_MEDIA_HW_NORMAL_MODE |
5415
		   GEN6_RP_MEDIA_IS_GFX |
5570
		   GEN6_RP_MEDIA_IS_GFX |
5416
		   GEN6_RP_ENABLE |
5571
		   GEN6_RP_ENABLE |
5417
		   GEN6_RP_UP_BUSY_AVG |
5572
		   GEN6_RP_UP_BUSY_AVG |
5418
		   GEN6_RP_DOWN_IDLE_CONT);
5573
		   GEN6_RP_DOWN_IDLE_CONT);
5419
 
5574
 
5420
	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5575
	I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5421
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5576
	I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5422
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5577
	I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5423
 
5578
 
5424
	for_each_ring(ring, dev_priv, i)
5579
	for_each_ring(ring, dev_priv, i)
5425
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5580
		I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5426
 
5581
 
5427
	I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
5582
	I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
5428
 
5583
 
5429
	/* allows RC6 residency counter to work */
5584
	/* allows RC6 residency counter to work */
5430
	I915_WRITE(VLV_COUNTER_CONTROL,
5585
	I915_WRITE(VLV_COUNTER_CONTROL,
5431
		   _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5586
		   _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5432
				      VLV_RENDER_RC0_COUNT_EN |
5587
				      VLV_RENDER_RC0_COUNT_EN |
5433
				      VLV_MEDIA_RC6_COUNT_EN |
5588
				      VLV_MEDIA_RC6_COUNT_EN |
5434
				      VLV_RENDER_RC6_COUNT_EN));
5589
				      VLV_RENDER_RC6_COUNT_EN));
5435
 
5590
 
5436
	if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
5591
	if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
5437
		rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
5592
		rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
5438
 
5593
 
5439
	intel_print_rc6_info(dev, rc6_mode);
5594
	intel_print_rc6_info(dev, rc6_mode);
5440
 
5595
 
5441
	I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5596
	I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
-
 
5597
 
-
 
5598
	/* Setting Fixed Bias */
-
 
5599
	val = VLV_OVERRIDE_EN |
-
 
5600
		  VLV_SOC_TDP_EN |
-
 
5601
		  VLV_BIAS_CPU_125_SOC_875;
-
 
5602
	vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5442
 
5603
 
5443
	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5604
	val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5444
 
5605
 
5445
	/* RPS code assumes GPLL is used */
5606
	/* RPS code assumes GPLL is used */
5446
	WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5607
	WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5447
 
5608
 
5448
	DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
5609
	DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
5449
	DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5610
	DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5450
 
5611
 
5451
	dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5612
	dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5452
	DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5613
	DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5453
			 vlv_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5614
			 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5454
			 dev_priv->rps.cur_freq);
5615
			 dev_priv->rps.cur_freq);
5455
 
5616
 
5456
	DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5617
	DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5457
			 vlv_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5618
			 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5458
			 dev_priv->rps.efficient_freq);
5619
			 dev_priv->rps.efficient_freq);
5459
 
5620
 
5460
	valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5621
	valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5461
 
5622
 
5462
	gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
-
 
5463
}
-
 
5464
 
-
 
5465
void ironlake_teardown_rc6(struct drm_device *dev)
-
 
5466
{
-
 
5467
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
5468
 
-
 
5469
	if (dev_priv->ips.renderctx) {
-
 
5470
		i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
-
 
5471
		drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
-
 
5472
		dev_priv->ips.renderctx = NULL;
-
 
5473
	}
-
 
5474
 
-
 
5475
	if (dev_priv->ips.pwrctx) {
-
 
5476
		i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
-
 
5477
		drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
-
 
5478
		dev_priv->ips.pwrctx = NULL;
-
 
5479
	}
-
 
5480
}
-
 
5481
 
-
 
5482
static void ironlake_disable_rc6(struct drm_device *dev)
-
 
5483
{
-
 
5484
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
5485
 
-
 
5486
	if (I915_READ(PWRCTXA)) {
-
 
5487
		/* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
-
 
5488
		I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
-
 
5489
		wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
-
 
5490
			 50);
-
 
5491
 
-
 
5492
		I915_WRITE(PWRCTXA, 0);
-
 
5493
		POSTING_READ(PWRCTXA);
-
 
5494
 
-
 
5495
		I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
-
 
5496
		POSTING_READ(RSTDBYCTL);
-
 
5497
	}
-
 
5498
}
-
 
5499
 
-
 
5500
static int ironlake_setup_rc6(struct drm_device *dev)
-
 
5501
{
-
 
5502
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
5503
 
-
 
5504
	if (dev_priv->ips.renderctx == NULL)
-
 
5505
		dev_priv->ips.renderctx = intel_alloc_context_page(dev);
-
 
5506
	if (!dev_priv->ips.renderctx)
-
 
5507
		return -ENOMEM;
-
 
5508
 
-
 
5509
	if (dev_priv->ips.pwrctx == NULL)
-
 
5510
		dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
-
 
5511
	if (!dev_priv->ips.pwrctx) {
-
 
5512
		ironlake_teardown_rc6(dev);
-
 
5513
		return -ENOMEM;
-
 
5514
	}
-
 
5515
 
-
 
5516
	return 0;
-
 
5517
}
-
 
5518
 
-
 
5519
static void ironlake_enable_rc6(struct drm_device *dev)
-
 
5520
{
-
 
5521
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
5522
	struct intel_engine_cs *ring = &dev_priv->ring[RCS];
-
 
5523
	bool was_interruptible;
-
 
5524
	int ret;
-
 
5525
 
-
 
5526
	/* rc6 disabled by default due to repeated reports of hanging during
-
 
5527
	 * boot and resume.
-
 
5528
	 */
-
 
5529
	if (!intel_enable_rc6(dev))
-
 
5530
		return;
-
 
5531
 
-
 
5532
	WARN_ON(!mutex_is_locked(&dev->struct_mutex));
-
 
5533
 
-
 
5534
	ret = ironlake_setup_rc6(dev);
-
 
5535
	if (ret)
-
 
5536
		return;
-
 
5537
 
-
 
5538
	was_interruptible = dev_priv->mm.interruptible;
-
 
5539
	dev_priv->mm.interruptible = false;
-
 
5540
 
-
 
5541
	/*
-
 
5542
	 * GPU can automatically power down the render unit if given a page
-
 
5543
	 * to save state.
-
 
5544
	 */
-
 
5545
	ret = intel_ring_begin(ring, 6);
-
 
5546
	if (ret) {
-
 
5547
		ironlake_teardown_rc6(dev);
-
 
5548
		dev_priv->mm.interruptible = was_interruptible;
-
 
5549
		return;
-
 
5550
	}
-
 
5551
 
-
 
5552
	intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
-
 
5553
	intel_ring_emit(ring, MI_SET_CONTEXT);
-
 
5554
	intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
-
 
5555
			MI_MM_SPACE_GTT |
-
 
5556
			MI_SAVE_EXT_STATE_EN |
-
 
5557
			MI_RESTORE_EXT_STATE_EN |
-
 
5558
			MI_RESTORE_INHIBIT);
-
 
5559
	intel_ring_emit(ring, MI_SUSPEND_FLUSH);
-
 
5560
	intel_ring_emit(ring, MI_NOOP);
-
 
5561
	intel_ring_emit(ring, MI_FLUSH);
-
 
5562
	intel_ring_advance(ring);
-
 
5563
 
-
 
5564
	/*
-
 
5565
	 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
-
 
5566
	 * does an implicit flush, combined with MI_FLUSH above, it should be
-
 
5567
	 * safe to assume that renderctx is valid
-
 
5568
	 */
-
 
5569
	ret = intel_ring_idle(ring);
-
 
5570
	dev_priv->mm.interruptible = was_interruptible;
-
 
5571
	if (ret) {
-
 
5572
		DRM_ERROR("failed to enable ironlake power savings\n");
-
 
5573
		ironlake_teardown_rc6(dev);
-
 
5574
		return;
-
 
5575
	}
-
 
5576
 
-
 
5577
	I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
-
 
5578
	I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
-
 
5579
 
-
 
5580
	intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
5623
	intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5581
}
5624
}
5582
 
5625
 
5583
static unsigned long intel_pxfreq(u32 vidfreq)
5626
static unsigned long intel_pxfreq(u32 vidfreq)
5584
{
5627
{
5585
	unsigned long freq;
5628
	unsigned long freq;
5586
	int div = (vidfreq & 0x3f0000) >> 16;
5629
	int div = (vidfreq & 0x3f0000) >> 16;
5587
	int post = (vidfreq & 0x3000) >> 12;
5630
	int post = (vidfreq & 0x3000) >> 12;
5588
	int pre = (vidfreq & 0x7);
5631
	int pre = (vidfreq & 0x7);
5589
 
5632
 
5590
	if (!pre)
5633
	if (!pre)
5591
		return 0;
5634
		return 0;
5592
 
5635
 
5593
	freq = ((div * 133333) / ((1<
5636
	freq = ((div * 133333) / ((1<
5594
 
5637
 
5595
	return freq;
5638
	return freq;
5596
}
5639
}
5597
 
5640
 
5598
static const struct cparams {
5641
static const struct cparams {
5599
	u16 i;
5642
	u16 i;
5600
	u16 t;
5643
	u16 t;
5601
	u16 m;
5644
	u16 m;
5602
	u16 c;
5645
	u16 c;
5603
} cparams[] = {
5646
} cparams[] = {
5604
	{ 1, 1333, 301, 28664 },
5647
	{ 1, 1333, 301, 28664 },
5605
	{ 1, 1066, 294, 24460 },
5648
	{ 1, 1066, 294, 24460 },
5606
	{ 1, 800, 294, 25192 },
5649
	{ 1, 800, 294, 25192 },
5607
	{ 0, 1333, 276, 27605 },
5650
	{ 0, 1333, 276, 27605 },
5608
	{ 0, 1066, 276, 27605 },
5651
	{ 0, 1066, 276, 27605 },
5609
	{ 0, 800, 231, 23784 },
5652
	{ 0, 800, 231, 23784 },
5610
};
5653
};
5611
 
5654
 
5612
static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
5655
static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
5613
{
5656
{
5614
	u64 total_count, diff, ret;
5657
	u64 total_count, diff, ret;
5615
	u32 count1, count2, count3, m = 0, c = 0;
5658
	u32 count1, count2, count3, m = 0, c = 0;
5616
	unsigned long now = jiffies_to_msecs(jiffies), diff1;
5659
	unsigned long now = jiffies_to_msecs(jiffies), diff1;
5617
	int i;
5660
	int i;
5618
 
5661
 
5619
	assert_spin_locked(&mchdev_lock);
5662
	assert_spin_locked(&mchdev_lock);
5620
 
5663
 
5621
	diff1 = now - dev_priv->ips.last_time1;
5664
	diff1 = now - dev_priv->ips.last_time1;
5622
 
5665
 
5623
	/* Prevent division-by-zero if we are asking too fast.
5666
	/* Prevent division-by-zero if we are asking too fast.
5624
	 * Also, we don't get interesting results if we are polling
5667
	 * Also, we don't get interesting results if we are polling
5625
	 * faster than once in 10ms, so just return the saved value
5668
	 * faster than once in 10ms, so just return the saved value
5626
	 * in such cases.
5669
	 * in such cases.
5627
	 */
5670
	 */
5628
	if (diff1 <= 10)
5671
	if (diff1 <= 10)
5629
		return dev_priv->ips.chipset_power;
5672
		return dev_priv->ips.chipset_power;
5630
 
5673
 
5631
	count1 = I915_READ(DMIEC);
5674
	count1 = I915_READ(DMIEC);
5632
	count2 = I915_READ(DDREC);
5675
	count2 = I915_READ(DDREC);
5633
	count3 = I915_READ(CSIEC);
5676
	count3 = I915_READ(CSIEC);
5634
 
5677
 
5635
	total_count = count1 + count2 + count3;
5678
	total_count = count1 + count2 + count3;
5636
 
5679
 
5637
	/* FIXME: handle per-counter overflow */
5680
	/* FIXME: handle per-counter overflow */
5638
	if (total_count < dev_priv->ips.last_count1) {
5681
	if (total_count < dev_priv->ips.last_count1) {
5639
		diff = ~0UL - dev_priv->ips.last_count1;
5682
		diff = ~0UL - dev_priv->ips.last_count1;
5640
		diff += total_count;
5683
		diff += total_count;
5641
	} else {
5684
	} else {
5642
		diff = total_count - dev_priv->ips.last_count1;
5685
		diff = total_count - dev_priv->ips.last_count1;
5643
	}
5686
	}
5644
 
5687
 
5645
	for (i = 0; i < ARRAY_SIZE(cparams); i++) {
5688
	for (i = 0; i < ARRAY_SIZE(cparams); i++) {
5646
		if (cparams[i].i == dev_priv->ips.c_m &&
5689
		if (cparams[i].i == dev_priv->ips.c_m &&
5647
		    cparams[i].t == dev_priv->ips.r_t) {
5690
		    cparams[i].t == dev_priv->ips.r_t) {
5648
			m = cparams[i].m;
5691
			m = cparams[i].m;
5649
			c = cparams[i].c;
5692
			c = cparams[i].c;
5650
			break;
5693
			break;
5651
		}
5694
		}
5652
	}
5695
	}
5653
 
5696
 
5654
	diff = div_u64(diff, diff1);
5697
	diff = div_u64(diff, diff1);
5655
	ret = ((m * diff) + c);
5698
	ret = ((m * diff) + c);
5656
	ret = div_u64(ret, 10);
5699
	ret = div_u64(ret, 10);
5657
 
5700
 
5658
	dev_priv->ips.last_count1 = total_count;
5701
	dev_priv->ips.last_count1 = total_count;
5659
	dev_priv->ips.last_time1 = now;
5702
	dev_priv->ips.last_time1 = now;
5660
 
5703
 
5661
	dev_priv->ips.chipset_power = ret;
5704
	dev_priv->ips.chipset_power = ret;
5662
 
5705
 
5663
	return ret;
5706
	return ret;
5664
}
5707
}
5665
 
5708
 
5666
unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5709
unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5667
{
5710
{
5668
	struct drm_device *dev = dev_priv->dev;
5711
	struct drm_device *dev = dev_priv->dev;
5669
	unsigned long val;
5712
	unsigned long val;
5670
 
5713
 
5671
	if (INTEL_INFO(dev)->gen != 5)
5714
	if (INTEL_INFO(dev)->gen != 5)
5672
		return 0;
5715
		return 0;
5673
 
5716
 
5674
	spin_lock_irq(&mchdev_lock);
5717
	spin_lock_irq(&mchdev_lock);
5675
 
5718
 
5676
	val = __i915_chipset_val(dev_priv);
5719
	val = __i915_chipset_val(dev_priv);
5677
 
5720
 
5678
	spin_unlock_irq(&mchdev_lock);
5721
	spin_unlock_irq(&mchdev_lock);
5679
 
5722
 
5680
	return val;
5723
	return val;
5681
}
5724
}
5682
 
5725
 
5683
unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5726
unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5684
{
5727
{
5685
	unsigned long m, x, b;
5728
	unsigned long m, x, b;
5686
	u32 tsfs;
5729
	u32 tsfs;
5687
 
5730
 
5688
	tsfs = I915_READ(TSFS);
5731
	tsfs = I915_READ(TSFS);
5689
 
5732
 
5690
	m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5733
	m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5691
	x = I915_READ8(TR1);
5734
	x = I915_READ8(TR1);
5692
 
5735
 
5693
	b = tsfs & TSFS_INTR_MASK;
5736
	b = tsfs & TSFS_INTR_MASK;
5694
 
5737
 
5695
	return ((m * x) / 127) - b;
5738
	return ((m * x) / 127) - b;
5696
}
5739
}
-
 
5740
 
-
 
5741
static int _pxvid_to_vd(u8 pxvid)
-
 
5742
{
-
 
5743
	if (pxvid == 0)
-
 
5744
		return 0;
-
 
5745
 
-
 
5746
	if (pxvid >= 8 && pxvid < 31)
-
 
5747
		pxvid = 31;
-
 
5748
 
-
 
5749
	return (pxvid + 2) * 125;
-
 
5750
}
5697
 
5751
 
5698
static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
5752
static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
5699
{
5753
{
5700
	struct drm_device *dev = dev_priv->dev;
5754
	struct drm_device *dev = dev_priv->dev;
5701
	static const struct v_table {
5755
	const int vd = _pxvid_to_vd(pxvid);
5702
		u16 vd; /* in .1 mil */
-
 
5703
		u16 vm; /* in .1 mil */
-
 
5704
	} v_table[] = {
-
 
5705
		{ 0, 0, },
-
 
5706
		{ 375, 0, },
-
 
5707
		{ 500, 0, },
-
 
5708
		{ 625, 0, },
-
 
5709
		{ 750, 0, },
-
 
5710
		{ 875, 0, },
-
 
5711
		{ 1000, 0, },
-
 
5712
		{ 1125, 0, },
-
 
5713
		{ 4125, 3000, },
-
 
5714
		{ 4125, 3000, },
-
 
5715
		{ 4125, 3000, },
-
 
5716
		{ 4125, 3000, },
-
 
5717
		{ 4125, 3000, },
-
 
5718
		{ 4125, 3000, },
-
 
5719
		{ 4125, 3000, },
-
 
5720
		{ 4125, 3000, },
-
 
5721
		{ 4125, 3000, },
-
 
5722
		{ 4125, 3000, },
-
 
5723
		{ 4125, 3000, },
-
 
5724
		{ 4125, 3000, },
-
 
5725
		{ 4125, 3000, },
-
 
5726
		{ 4125, 3000, },
-
 
5727
		{ 4125, 3000, },
-
 
5728
		{ 4125, 3000, },
-
 
5729
		{ 4125, 3000, },
-
 
5730
		{ 4125, 3000, },
-
 
5731
		{ 4125, 3000, },
-
 
5732
		{ 4125, 3000, },
-
 
5733
		{ 4125, 3000, },
-
 
5734
		{ 4125, 3000, },
-
 
5735
		{ 4125, 3000, },
-
 
5736
		{ 4125, 3000, },
-
 
5737
		{ 4250, 3125, },
-
 
5738
		{ 4375, 3250, },
-
 
5739
		{ 4500, 3375, },
-
 
5740
		{ 4625, 3500, },
-
 
5741
		{ 4750, 3625, },
-
 
5742
		{ 4875, 3750, },
-
 
5743
		{ 5000, 3875, },
-
 
5744
		{ 5125, 4000, },
-
 
5745
		{ 5250, 4125, },
-
 
5746
		{ 5375, 4250, },
-
 
5747
		{ 5500, 4375, },
-
 
5748
		{ 5625, 4500, },
-
 
5749
		{ 5750, 4625, },
-
 
5750
		{ 5875, 4750, },
-
 
5751
		{ 6000, 4875, },
-
 
5752
		{ 6125, 5000, },
-
 
5753
		{ 6250, 5125, },
-
 
5754
		{ 6375, 5250, },
-
 
5755
		{ 6500, 5375, },
-
 
5756
		{ 6625, 5500, },
-
 
5757
		{ 6750, 5625, },
-
 
5758
		{ 6875, 5750, },
-
 
5759
		{ 7000, 5875, },
-
 
5760
		{ 7125, 6000, },
-
 
5761
		{ 7250, 6125, },
-
 
5762
		{ 7375, 6250, },
-
 
5763
		{ 7500, 6375, },
-
 
5764
		{ 7625, 6500, },
-
 
5765
		{ 7750, 6625, },
-
 
5766
		{ 7875, 6750, },
-
 
5767
		{ 8000, 6875, },
-
 
5768
		{ 8125, 7000, },
-
 
5769
		{ 8250, 7125, },
-
 
5770
		{ 8375, 7250, },
-
 
5771
		{ 8500, 7375, },
-
 
5772
		{ 8625, 7500, },
-
 
5773
		{ 8750, 7625, },
-
 
5774
		{ 8875, 7750, },
-
 
5775
		{ 9000, 7875, },
-
 
5776
		{ 9125, 8000, },
-
 
5777
		{ 9250, 8125, },
-
 
5778
		{ 9375, 8250, },
-
 
5779
		{ 9500, 8375, },
-
 
5780
		{ 9625, 8500, },
-
 
5781
		{ 9750, 8625, },
-
 
5782
		{ 9875, 8750, },
-
 
5783
		{ 10000, 8875, },
-
 
5784
		{ 10125, 9000, },
-
 
5785
		{ 10250, 9125, },
-
 
5786
		{ 10375, 9250, },
-
 
5787
		{ 10500, 9375, },
-
 
5788
		{ 10625, 9500, },
-
 
5789
		{ 10750, 9625, },
-
 
5790
		{ 10875, 9750, },
-
 
5791
		{ 11000, 9875, },
-
 
5792
		{ 11125, 10000, },
-
 
5793
		{ 11250, 10125, },
-
 
5794
		{ 11375, 10250, },
-
 
5795
		{ 11500, 10375, },
-
 
5796
		{ 11625, 10500, },
-
 
5797
		{ 11750, 10625, },
-
 
5798
		{ 11875, 10750, },
-
 
5799
		{ 12000, 10875, },
-
 
5800
		{ 12125, 11000, },
-
 
5801
		{ 12250, 11125, },
-
 
5802
		{ 12375, 11250, },
-
 
5803
		{ 12500, 11375, },
-
 
5804
		{ 12625, 11500, },
-
 
5805
		{ 12750, 11625, },
-
 
5806
		{ 12875, 11750, },
-
 
5807
		{ 13000, 11875, },
-
 
5808
		{ 13125, 12000, },
-
 
5809
		{ 13250, 12125, },
-
 
5810
		{ 13375, 12250, },
-
 
5811
		{ 13500, 12375, },
-
 
5812
		{ 13625, 12500, },
-
 
5813
		{ 13750, 12625, },
-
 
5814
		{ 13875, 12750, },
-
 
5815
		{ 14000, 12875, },
-
 
5816
		{ 14125, 13000, },
-
 
5817
		{ 14250, 13125, },
-
 
5818
		{ 14375, 13250, },
-
 
5819
		{ 14500, 13375, },
-
 
5820
		{ 14625, 13500, },
-
 
5821
		{ 14750, 13625, },
-
 
5822
		{ 14875, 13750, },
-
 
5823
		{ 15000, 13875, },
-
 
5824
		{ 15125, 14000, },
-
 
5825
		{ 15250, 14125, },
-
 
5826
		{ 15375, 14250, },
-
 
5827
		{ 15500, 14375, },
-
 
5828
		{ 15625, 14500, },
-
 
5829
		{ 15750, 14625, },
-
 
5830
		{ 15875, 14750, },
-
 
5831
		{ 16000, 14875, },
-
 
5832
		{ 16125, 15000, },
5756
	const int vm = vd - 1125;
5833
	};
5757
 
5834
	if (INTEL_INFO(dev)->is_mobile)
5758
	if (INTEL_INFO(dev)->is_mobile)
5835
		return v_table[pxvid].vm;
5759
		return vm > 0 ? vm : 0;
5836
	else
5760
 
5837
		return v_table[pxvid].vd;
5761
	return vd;
5838
}
5762
}
5839
 
5763
 
5840
static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
5764
static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
5841
{
5765
{
5842
	u64 now, diff, diffms;
5766
	u64 now, diff, diffms;
5843
	u32 count;
5767
	u32 count;
5844
 
5768
 
5845
	assert_spin_locked(&mchdev_lock);
5769
	assert_spin_locked(&mchdev_lock);
5846
 
5770
 
5847
	now = ktime_get_raw_ns();
5771
	now = ktime_get_raw_ns();
5848
	diffms = now - dev_priv->ips.last_time2;
5772
	diffms = now - dev_priv->ips.last_time2;
5849
	do_div(diffms, NSEC_PER_MSEC);
5773
	do_div(diffms, NSEC_PER_MSEC);
5850
 
5774
 
5851
	/* Don't divide by 0 */
5775
	/* Don't divide by 0 */
5852
	if (!diffms)
5776
	if (!diffms)
5853
		return;
5777
		return;
5854
 
5778
 
5855
	count = I915_READ(GFXEC);
5779
	count = I915_READ(GFXEC);
5856
 
5780
 
5857
	if (count < dev_priv->ips.last_count2) {
5781
	if (count < dev_priv->ips.last_count2) {
5858
		diff = ~0UL - dev_priv->ips.last_count2;
5782
		diff = ~0UL - dev_priv->ips.last_count2;
5859
		diff += count;
5783
		diff += count;
5860
	} else {
5784
	} else {
5861
		diff = count - dev_priv->ips.last_count2;
5785
		diff = count - dev_priv->ips.last_count2;
5862
	}
5786
	}
5863
 
5787
 
5864
	dev_priv->ips.last_count2 = count;
5788
	dev_priv->ips.last_count2 = count;
5865
	dev_priv->ips.last_time2 = now;
5789
	dev_priv->ips.last_time2 = now;
5866
 
5790
 
5867
	/* More magic constants... */
5791
	/* More magic constants... */
5868
	diff = diff * 1181;
5792
	diff = diff * 1181;
5869
	diff = div_u64(diff, diffms * 10);
5793
	diff = div_u64(diff, diffms * 10);
5870
	dev_priv->ips.gfx_power = diff;
5794
	dev_priv->ips.gfx_power = diff;
5871
}
5795
}
5872
 
5796
 
5873
void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5797
void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5874
{
5798
{
5875
	struct drm_device *dev = dev_priv->dev;
5799
	struct drm_device *dev = dev_priv->dev;
5876
 
5800
 
5877
	if (INTEL_INFO(dev)->gen != 5)
5801
	if (INTEL_INFO(dev)->gen != 5)
5878
		return;
5802
		return;
5879
 
5803
 
5880
	spin_lock_irq(&mchdev_lock);
5804
	spin_lock_irq(&mchdev_lock);
5881
 
5805
 
5882
	__i915_update_gfx_val(dev_priv);
5806
	__i915_update_gfx_val(dev_priv);
5883
 
5807
 
5884
	spin_unlock_irq(&mchdev_lock);
5808
	spin_unlock_irq(&mchdev_lock);
5885
}
5809
}
5886
 
5810
 
5887
static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
5811
static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
5888
{
5812
{
5889
	unsigned long t, corr, state1, corr2, state2;
5813
	unsigned long t, corr, state1, corr2, state2;
5890
	u32 pxvid, ext_v;
5814
	u32 pxvid, ext_v;
5891
 
5815
 
5892
	assert_spin_locked(&mchdev_lock);
5816
	assert_spin_locked(&mchdev_lock);
5893
 
5817
 
5894
	pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
5818
	pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
5895
	pxvid = (pxvid >> 24) & 0x7f;
5819
	pxvid = (pxvid >> 24) & 0x7f;
5896
	ext_v = pvid_to_extvid(dev_priv, pxvid);
5820
	ext_v = pvid_to_extvid(dev_priv, pxvid);
5897
 
5821
 
5898
	state1 = ext_v;
5822
	state1 = ext_v;
5899
 
5823
 
5900
	t = i915_mch_val(dev_priv);
5824
	t = i915_mch_val(dev_priv);
5901
 
5825
 
5902
	/* Revel in the empirically derived constants */
5826
	/* Revel in the empirically derived constants */
5903
 
5827
 
5904
	/* Correction factor in 1/100000 units */
5828
	/* Correction factor in 1/100000 units */
5905
	if (t > 80)
5829
	if (t > 80)
5906
		corr = ((t * 2349) + 135940);
5830
		corr = ((t * 2349) + 135940);
5907
	else if (t >= 50)
5831
	else if (t >= 50)
5908
		corr = ((t * 964) + 29317);
5832
		corr = ((t * 964) + 29317);
5909
	else /* < 50 */
5833
	else /* < 50 */
5910
		corr = ((t * 301) + 1004);
5834
		corr = ((t * 301) + 1004);
5911
 
5835
 
5912
	corr = corr * ((150142 * state1) / 10000 - 78642);
5836
	corr = corr * ((150142 * state1) / 10000 - 78642);
5913
	corr /= 100000;
5837
	corr /= 100000;
5914
	corr2 = (corr * dev_priv->ips.corr);
5838
	corr2 = (corr * dev_priv->ips.corr);
5915
 
5839
 
5916
	state2 = (corr2 * state1) / 10000;
5840
	state2 = (corr2 * state1) / 10000;
5917
	state2 /= 100; /* convert to mW */
5841
	state2 /= 100; /* convert to mW */
5918
 
5842
 
5919
	__i915_update_gfx_val(dev_priv);
5843
	__i915_update_gfx_val(dev_priv);
5920
 
5844
 
5921
	return dev_priv->ips.gfx_power + state2;
5845
	return dev_priv->ips.gfx_power + state2;
5922
}
5846
}
5923
 
5847
 
5924
unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5848
unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5925
{
5849
{
5926
	struct drm_device *dev = dev_priv->dev;
5850
	struct drm_device *dev = dev_priv->dev;
5927
	unsigned long val;
5851
	unsigned long val;
5928
 
5852
 
5929
	if (INTEL_INFO(dev)->gen != 5)
5853
	if (INTEL_INFO(dev)->gen != 5)
5930
		return 0;
5854
		return 0;
5931
 
5855
 
5932
	spin_lock_irq(&mchdev_lock);
5856
	spin_lock_irq(&mchdev_lock);
5933
 
5857
 
5934
	val = __i915_gfx_val(dev_priv);
5858
	val = __i915_gfx_val(dev_priv);
5935
 
5859
 
5936
	spin_unlock_irq(&mchdev_lock);
5860
	spin_unlock_irq(&mchdev_lock);
5937
 
5861
 
5938
	return val;
5862
	return val;
5939
}
5863
}
5940
 
5864
 
5941
/**
5865
/**
5942
 * i915_read_mch_val - return value for IPS use
5866
 * i915_read_mch_val - return value for IPS use
5943
 *
5867
 *
5944
 * Calculate and return a value for the IPS driver to use when deciding whether
5868
 * Calculate and return a value for the IPS driver to use when deciding whether
5945
 * we have thermal and power headroom to increase CPU or GPU power budget.
5869
 * we have thermal and power headroom to increase CPU or GPU power budget.
5946
 */
5870
 */
5947
unsigned long i915_read_mch_val(void)
5871
unsigned long i915_read_mch_val(void)
5948
{
5872
{
5949
	struct drm_i915_private *dev_priv;
5873
	struct drm_i915_private *dev_priv;
5950
	unsigned long chipset_val, graphics_val, ret = 0;
5874
	unsigned long chipset_val, graphics_val, ret = 0;
5951
 
5875
 
5952
	spin_lock_irq(&mchdev_lock);
5876
	spin_lock_irq(&mchdev_lock);
5953
	if (!i915_mch_dev)
5877
	if (!i915_mch_dev)
5954
		goto out_unlock;
5878
		goto out_unlock;
5955
	dev_priv = i915_mch_dev;
5879
	dev_priv = i915_mch_dev;
5956
 
5880
 
5957
	chipset_val = __i915_chipset_val(dev_priv);
5881
	chipset_val = __i915_chipset_val(dev_priv);
5958
	graphics_val = __i915_gfx_val(dev_priv);
5882
	graphics_val = __i915_gfx_val(dev_priv);
5959
 
5883
 
5960
	ret = chipset_val + graphics_val;
5884
	ret = chipset_val + graphics_val;
5961
 
5885
 
5962
out_unlock:
5886
out_unlock:
5963
	spin_unlock_irq(&mchdev_lock);
5887
	spin_unlock_irq(&mchdev_lock);
5964
 
5888
 
5965
	return ret;
5889
	return ret;
5966
}
5890
}
5967
EXPORT_SYMBOL_GPL(i915_read_mch_val);
5891
EXPORT_SYMBOL_GPL(i915_read_mch_val);
5968
 
5892
 
5969
/**
5893
/**
5970
 * i915_gpu_raise - raise GPU frequency limit
5894
 * i915_gpu_raise - raise GPU frequency limit
5971
 *
5895
 *
5972
 * Raise the limit; IPS indicates we have thermal headroom.
5896
 * Raise the limit; IPS indicates we have thermal headroom.
5973
 */
5897
 */
5974
bool i915_gpu_raise(void)
5898
bool i915_gpu_raise(void)
5975
{
5899
{
5976
	struct drm_i915_private *dev_priv;
5900
	struct drm_i915_private *dev_priv;
5977
	bool ret = true;
5901
	bool ret = true;
5978
 
5902
 
5979
	spin_lock_irq(&mchdev_lock);
5903
	spin_lock_irq(&mchdev_lock);
5980
	if (!i915_mch_dev) {
5904
	if (!i915_mch_dev) {
5981
		ret = false;
5905
		ret = false;
5982
		goto out_unlock;
5906
		goto out_unlock;
5983
	}
5907
	}
5984
	dev_priv = i915_mch_dev;
5908
	dev_priv = i915_mch_dev;
5985
 
5909
 
5986
	if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5910
	if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5987
		dev_priv->ips.max_delay--;
5911
		dev_priv->ips.max_delay--;
5988
 
5912
 
5989
out_unlock:
5913
out_unlock:
5990
	spin_unlock_irq(&mchdev_lock);
5914
	spin_unlock_irq(&mchdev_lock);
5991
 
5915
 
5992
	return ret;
5916
	return ret;
5993
}
5917
}
5994
EXPORT_SYMBOL_GPL(i915_gpu_raise);
5918
EXPORT_SYMBOL_GPL(i915_gpu_raise);
5995
 
5919
 
5996
/**
5920
/**
5997
 * i915_gpu_lower - lower GPU frequency limit
5921
 * i915_gpu_lower - lower GPU frequency limit
5998
 *
5922
 *
5999
 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5923
 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6000
 * frequency maximum.
5924
 * frequency maximum.
6001
 */
5925
 */
6002
bool i915_gpu_lower(void)
5926
bool i915_gpu_lower(void)
6003
{
5927
{
6004
	struct drm_i915_private *dev_priv;
5928
	struct drm_i915_private *dev_priv;
6005
	bool ret = true;
5929
	bool ret = true;
6006
 
5930
 
6007
	spin_lock_irq(&mchdev_lock);
5931
	spin_lock_irq(&mchdev_lock);
6008
	if (!i915_mch_dev) {
5932
	if (!i915_mch_dev) {
6009
		ret = false;
5933
		ret = false;
6010
		goto out_unlock;
5934
		goto out_unlock;
6011
	}
5935
	}
6012
	dev_priv = i915_mch_dev;
5936
	dev_priv = i915_mch_dev;
6013
 
5937
 
6014
	if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5938
	if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6015
		dev_priv->ips.max_delay++;
5939
		dev_priv->ips.max_delay++;
6016
 
5940
 
6017
out_unlock:
5941
out_unlock:
6018
	spin_unlock_irq(&mchdev_lock);
5942
	spin_unlock_irq(&mchdev_lock);
6019
 
5943
 
6020
	return ret;
5944
	return ret;
6021
}
5945
}
6022
EXPORT_SYMBOL_GPL(i915_gpu_lower);
5946
EXPORT_SYMBOL_GPL(i915_gpu_lower);
6023
 
5947
 
6024
/**
5948
/**
6025
 * i915_gpu_busy - indicate GPU business to IPS
5949
 * i915_gpu_busy - indicate GPU business to IPS
6026
 *
5950
 *
6027
 * Tell the IPS driver whether or not the GPU is busy.
5951
 * Tell the IPS driver whether or not the GPU is busy.
6028
 */
5952
 */
6029
bool i915_gpu_busy(void)
5953
bool i915_gpu_busy(void)
6030
{
5954
{
6031
	struct drm_i915_private *dev_priv;
5955
	struct drm_i915_private *dev_priv;
6032
	struct intel_engine_cs *ring;
5956
	struct intel_engine_cs *ring;
6033
	bool ret = false;
5957
	bool ret = false;
6034
	int i;
5958
	int i;
6035
 
5959
 
6036
	spin_lock_irq(&mchdev_lock);
5960
	spin_lock_irq(&mchdev_lock);
6037
	if (!i915_mch_dev)
5961
	if (!i915_mch_dev)
6038
		goto out_unlock;
5962
		goto out_unlock;
6039
	dev_priv = i915_mch_dev;
5963
	dev_priv = i915_mch_dev;
6040
 
5964
 
6041
	for_each_ring(ring, dev_priv, i)
5965
	for_each_ring(ring, dev_priv, i)
6042
		ret |= !list_empty(&ring->request_list);
5966
		ret |= !list_empty(&ring->request_list);
6043
 
5967
 
6044
out_unlock:
5968
out_unlock:
6045
	spin_unlock_irq(&mchdev_lock);
5969
	spin_unlock_irq(&mchdev_lock);
6046
 
5970
 
6047
	return ret;
5971
	return ret;
6048
}
5972
}
6049
EXPORT_SYMBOL_GPL(i915_gpu_busy);
5973
EXPORT_SYMBOL_GPL(i915_gpu_busy);
6050
 
5974
 
6051
/**
5975
/**
6052
 * i915_gpu_turbo_disable - disable graphics turbo
5976
 * i915_gpu_turbo_disable - disable graphics turbo
6053
 *
5977
 *
6054
 * Disable graphics turbo by resetting the max frequency and setting the
5978
 * Disable graphics turbo by resetting the max frequency and setting the
6055
 * current frequency to the default.
5979
 * current frequency to the default.
6056
 */
5980
 */
6057
bool i915_gpu_turbo_disable(void)
5981
bool i915_gpu_turbo_disable(void)
6058
{
5982
{
6059
	struct drm_i915_private *dev_priv;
5983
	struct drm_i915_private *dev_priv;
6060
	bool ret = true;
5984
	bool ret = true;
6061
 
5985
 
6062
	spin_lock_irq(&mchdev_lock);
5986
	spin_lock_irq(&mchdev_lock);
6063
	if (!i915_mch_dev) {
5987
	if (!i915_mch_dev) {
6064
		ret = false;
5988
		ret = false;
6065
		goto out_unlock;
5989
		goto out_unlock;
6066
	}
5990
	}
6067
	dev_priv = i915_mch_dev;
5991
	dev_priv = i915_mch_dev;
6068
 
5992
 
6069
	dev_priv->ips.max_delay = dev_priv->ips.fstart;
5993
	dev_priv->ips.max_delay = dev_priv->ips.fstart;
6070
 
5994
 
6071
	if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
5995
	if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
6072
		ret = false;
5996
		ret = false;
6073
 
5997
 
6074
out_unlock:
5998
out_unlock:
6075
	spin_unlock_irq(&mchdev_lock);
5999
	spin_unlock_irq(&mchdev_lock);
6076
 
6000
 
6077
	return ret;
6001
	return ret;
6078
}
6002
}
6079
EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6003
EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6080
 
6004
 
6081
/**
6005
/**
6082
 * Tells the intel_ips driver that the i915 driver is now loaded, if
6006
 * Tells the intel_ips driver that the i915 driver is now loaded, if
6083
 * IPS got loaded first.
6007
 * IPS got loaded first.
6084
 *
6008
 *
6085
 * This awkward dance is so that neither module has to depend on the
6009
 * This awkward dance is so that neither module has to depend on the
6086
 * other in order for IPS to do the appropriate communication of
6010
 * other in order for IPS to do the appropriate communication of
6087
 * GPU turbo limits to i915.
6011
 * GPU turbo limits to i915.
6088
 */
6012
 */
6089
static void
6013
static void
6090
ips_ping_for_i915_load(void)
6014
ips_ping_for_i915_load(void)
6091
{
6015
{
6092
	void (*link)(void);
6016
	void (*link)(void);
6093
 
6017
 
6094
//   link = symbol_get(ips_link_to_i915_driver);
6018
//   link = symbol_get(ips_link_to_i915_driver);
6095
//   if (link) {
6019
//   if (link) {
6096
//       link();
6020
//       link();
6097
//       symbol_put(ips_link_to_i915_driver);
6021
//       symbol_put(ips_link_to_i915_driver);
6098
//   }
6022
//   }
6099
}
6023
}
6100
 
6024
 
6101
void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6025
void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6102
{
6026
{
6103
	/* We only register the i915 ips part with intel-ips once everything is
6027
	/* We only register the i915 ips part with intel-ips once everything is
6104
	 * set up, to avoid intel-ips sneaking in and reading bogus values. */
6028
	 * set up, to avoid intel-ips sneaking in and reading bogus values. */
6105
	spin_lock_irq(&mchdev_lock);
6029
	spin_lock_irq(&mchdev_lock);
6106
	i915_mch_dev = dev_priv;
6030
	i915_mch_dev = dev_priv;
6107
	spin_unlock_irq(&mchdev_lock);
6031
	spin_unlock_irq(&mchdev_lock);
6108
 
6032
 
6109
	ips_ping_for_i915_load();
6033
	ips_ping_for_i915_load();
6110
}
6034
}
6111
 
6035
 
6112
void intel_gpu_ips_teardown(void)
6036
void intel_gpu_ips_teardown(void)
6113
{
6037
{
6114
	spin_lock_irq(&mchdev_lock);
6038
	spin_lock_irq(&mchdev_lock);
6115
	i915_mch_dev = NULL;
6039
	i915_mch_dev = NULL;
6116
	spin_unlock_irq(&mchdev_lock);
6040
	spin_unlock_irq(&mchdev_lock);
6117
}
6041
}
6118
 
6042
 
6119
static void intel_init_emon(struct drm_device *dev)
6043
static void intel_init_emon(struct drm_device *dev)
6120
{
6044
{
6121
	struct drm_i915_private *dev_priv = dev->dev_private;
6045
	struct drm_i915_private *dev_priv = dev->dev_private;
6122
	u32 lcfuse;
6046
	u32 lcfuse;
6123
	u8 pxw[16];
6047
	u8 pxw[16];
6124
	int i;
6048
	int i;
6125
 
6049
 
6126
	/* Disable to program */
6050
	/* Disable to program */
6127
	I915_WRITE(ECR, 0);
6051
	I915_WRITE(ECR, 0);
6128
	POSTING_READ(ECR);
6052
	POSTING_READ(ECR);
6129
 
6053
 
6130
	/* Program energy weights for various events */
6054
	/* Program energy weights for various events */
6131
	I915_WRITE(SDEW, 0x15040d00);
6055
	I915_WRITE(SDEW, 0x15040d00);
6132
	I915_WRITE(CSIEW0, 0x007f0000);
6056
	I915_WRITE(CSIEW0, 0x007f0000);
6133
	I915_WRITE(CSIEW1, 0x1e220004);
6057
	I915_WRITE(CSIEW1, 0x1e220004);
6134
	I915_WRITE(CSIEW2, 0x04000004);
6058
	I915_WRITE(CSIEW2, 0x04000004);
6135
 
6059
 
6136
	for (i = 0; i < 5; i++)
6060
	for (i = 0; i < 5; i++)
6137
		I915_WRITE(PEW + (i * 4), 0);
6061
		I915_WRITE(PEW(i), 0);
6138
	for (i = 0; i < 3; i++)
6062
	for (i = 0; i < 3; i++)
6139
		I915_WRITE(DEW + (i * 4), 0);
6063
		I915_WRITE(DEW(i), 0);
6140
 
6064
 
6141
	/* Program P-state weights to account for frequency power adjustment */
6065
	/* Program P-state weights to account for frequency power adjustment */
6142
	for (i = 0; i < 16; i++) {
6066
	for (i = 0; i < 16; i++) {
6143
		u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
6067
		u32 pxvidfreq = I915_READ(PXVFREQ(i));
6144
		unsigned long freq = intel_pxfreq(pxvidfreq);
6068
		unsigned long freq = intel_pxfreq(pxvidfreq);
6145
		unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6069
		unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6146
			PXVFREQ_PX_SHIFT;
6070
			PXVFREQ_PX_SHIFT;
6147
		unsigned long val;
6071
		unsigned long val;
6148
 
6072
 
6149
		val = vid * vid;
6073
		val = vid * vid;
6150
		val *= (freq / 1000);
6074
		val *= (freq / 1000);
6151
		val *= 255;
6075
		val *= 255;
6152
		val /= (127*127*900);
6076
		val /= (127*127*900);
6153
		if (val > 0xff)
6077
		if (val > 0xff)
6154
			DRM_ERROR("bad pxval: %ld\n", val);
6078
			DRM_ERROR("bad pxval: %ld\n", val);
6155
		pxw[i] = val;
6079
		pxw[i] = val;
6156
	}
6080
	}
6157
	/* Render standby states get 0 weight */
6081
	/* Render standby states get 0 weight */
6158
	pxw[14] = 0;
6082
	pxw[14] = 0;
6159
	pxw[15] = 0;
6083
	pxw[15] = 0;
6160
 
6084
 
6161
	for (i = 0; i < 4; i++) {
6085
	for (i = 0; i < 4; i++) {
6162
		u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6086
		u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6163
			(pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6087
			(pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6164
		I915_WRITE(PXW + (i * 4), val);
6088
		I915_WRITE(PXW(i), val);
6165
	}
6089
	}
6166
 
6090
 
6167
	/* Adjust magic regs to magic values (more experimental results) */
6091
	/* Adjust magic regs to magic values (more experimental results) */
6168
	I915_WRITE(OGW0, 0);
6092
	I915_WRITE(OGW0, 0);
6169
	I915_WRITE(OGW1, 0);
6093
	I915_WRITE(OGW1, 0);
6170
	I915_WRITE(EG0, 0x00007f00);
6094
	I915_WRITE(EG0, 0x00007f00);
6171
	I915_WRITE(EG1, 0x0000000e);
6095
	I915_WRITE(EG1, 0x0000000e);
6172
	I915_WRITE(EG2, 0x000e0000);
6096
	I915_WRITE(EG2, 0x000e0000);
6173
	I915_WRITE(EG3, 0x68000300);
6097
	I915_WRITE(EG3, 0x68000300);
6174
	I915_WRITE(EG4, 0x42000000);
6098
	I915_WRITE(EG4, 0x42000000);
6175
	I915_WRITE(EG5, 0x00140031);
6099
	I915_WRITE(EG5, 0x00140031);
6176
	I915_WRITE(EG6, 0);
6100
	I915_WRITE(EG6, 0);
6177
	I915_WRITE(EG7, 0);
6101
	I915_WRITE(EG7, 0);
6178
 
6102
 
6179
	for (i = 0; i < 8; i++)
6103
	for (i = 0; i < 8; i++)
6180
		I915_WRITE(PXWL + (i * 4), 0);
6104
		I915_WRITE(PXWL(i), 0);
6181
 
6105
 
6182
	/* Enable PMON + select events */
6106
	/* Enable PMON + select events */
6183
	I915_WRITE(ECR, 0x80000019);
6107
	I915_WRITE(ECR, 0x80000019);
6184
 
6108
 
6185
	lcfuse = I915_READ(LCFUSE02);
6109
	lcfuse = I915_READ(LCFUSE02);
6186
 
6110
 
6187
	dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
6111
	dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
6188
}
6112
}
6189
 
6113
 
6190
void intel_init_gt_powersave(struct drm_device *dev)
6114
void intel_init_gt_powersave(struct drm_device *dev)
6191
{
6115
{
6192
	i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
6116
	i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
6193
 
6117
 
6194
	if (IS_CHERRYVIEW(dev))
6118
	if (IS_CHERRYVIEW(dev))
6195
		cherryview_init_gt_powersave(dev);
6119
		cherryview_init_gt_powersave(dev);
6196
	else if (IS_VALLEYVIEW(dev))
6120
	else if (IS_VALLEYVIEW(dev))
6197
		valleyview_init_gt_powersave(dev);
6121
		valleyview_init_gt_powersave(dev);
6198
}
6122
}
6199
 
6123
 
6200
void intel_cleanup_gt_powersave(struct drm_device *dev)
6124
void intel_cleanup_gt_powersave(struct drm_device *dev)
6201
{
6125
{
6202
	if (IS_CHERRYVIEW(dev))
6126
	if (IS_CHERRYVIEW(dev))
6203
		return;
6127
		return;
6204
	else if (IS_VALLEYVIEW(dev))
6128
	else if (IS_VALLEYVIEW(dev))
6205
		valleyview_cleanup_gt_powersave(dev);
6129
		valleyview_cleanup_gt_powersave(dev);
6206
}
6130
}
6207
 
6131
 
6208
static void gen6_suspend_rps(struct drm_device *dev)
6132
static void gen6_suspend_rps(struct drm_device *dev)
6209
{
6133
{
6210
	struct drm_i915_private *dev_priv = dev->dev_private;
6134
	struct drm_i915_private *dev_priv = dev->dev_private;
6211
 
6135
 
6212
//   flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6136
//   flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6213
 
-
 
6214
	/*
-
 
6215
	 * TODO: disable RPS interrupts on GEN9+ too once RPS support
-
 
6216
	 * is added for it.
-
 
6217
	 */
-
 
6218
	if (INTEL_INFO(dev)->gen < 9)
6137
 
6219
		gen6_disable_rps_interrupts(dev);
6138
	gen6_disable_rps_interrupts(dev);
6220
}
6139
}
6221
 
6140
 
6222
/**
6141
/**
6223
 * intel_suspend_gt_powersave - suspend PM work and helper threads
6142
 * intel_suspend_gt_powersave - suspend PM work and helper threads
6224
 * @dev: drm device
6143
 * @dev: drm device
6225
 *
6144
 *
6226
 * We don't want to disable RC6 or other features here, we just want
6145
 * We don't want to disable RC6 or other features here, we just want
6227
 * to make sure any work we've queued has finished and won't bother
6146
 * to make sure any work we've queued has finished and won't bother
6228
 * us while we're suspended.
6147
 * us while we're suspended.
6229
 */
6148
 */
6230
void intel_suspend_gt_powersave(struct drm_device *dev)
6149
void intel_suspend_gt_powersave(struct drm_device *dev)
6231
{
6150
{
6232
	struct drm_i915_private *dev_priv = dev->dev_private;
6151
	struct drm_i915_private *dev_priv = dev->dev_private;
6233
 
6152
 
6234
	if (INTEL_INFO(dev)->gen < 6)
6153
	if (INTEL_INFO(dev)->gen < 6)
6235
		return;
6154
		return;
6236
 
6155
 
6237
	gen6_suspend_rps(dev);
6156
	gen6_suspend_rps(dev);
6238
 
6157
 
6239
	/* Force GPU to min freq during suspend */
6158
	/* Force GPU to min freq during suspend */
6240
	gen6_rps_idle(dev_priv);
6159
	gen6_rps_idle(dev_priv);
6241
}
6160
}
6242
 
6161
 
6243
void intel_disable_gt_powersave(struct drm_device *dev)
6162
void intel_disable_gt_powersave(struct drm_device *dev)
6244
{
6163
{
6245
	struct drm_i915_private *dev_priv = dev->dev_private;
6164
	struct drm_i915_private *dev_priv = dev->dev_private;
6246
 
6165
 
6247
	if (IS_IRONLAKE_M(dev)) {
6166
	if (IS_IRONLAKE_M(dev)) {
6248
		ironlake_disable_drps(dev);
6167
		ironlake_disable_drps(dev);
6249
		ironlake_disable_rc6(dev);
-
 
6250
	} else if (INTEL_INFO(dev)->gen >= 6) {
6168
	} else if (INTEL_INFO(dev)->gen >= 6) {
6251
		intel_suspend_gt_powersave(dev);
6169
		intel_suspend_gt_powersave(dev);
6252
 
6170
 
6253
		mutex_lock(&dev_priv->rps.hw_lock);
6171
		mutex_lock(&dev_priv->rps.hw_lock);
6254
		if (INTEL_INFO(dev)->gen >= 9)
6172
		if (INTEL_INFO(dev)->gen >= 9)
6255
			gen9_disable_rps(dev);
6173
			gen9_disable_rps(dev);
6256
		else if (IS_CHERRYVIEW(dev))
6174
		else if (IS_CHERRYVIEW(dev))
6257
			cherryview_disable_rps(dev);
6175
			cherryview_disable_rps(dev);
6258
		else if (IS_VALLEYVIEW(dev))
6176
		else if (IS_VALLEYVIEW(dev))
6259
			valleyview_disable_rps(dev);
6177
			valleyview_disable_rps(dev);
6260
		else
6178
		else
6261
		gen6_disable_rps(dev);
6179
			gen6_disable_rps(dev);
6262
 
6180
 
6263
		dev_priv->rps.enabled = false;
6181
		dev_priv->rps.enabled = false;
6264
		mutex_unlock(&dev_priv->rps.hw_lock);
6182
		mutex_unlock(&dev_priv->rps.hw_lock);
6265
	}
6183
	}
6266
}
6184
}
6267
 
6185
 
6268
static void intel_gen6_powersave_work(struct work_struct *work)
6186
static void intel_gen6_powersave_work(struct work_struct *work)
6269
{
6187
{
6270
	struct drm_i915_private *dev_priv =
6188
	struct drm_i915_private *dev_priv =
6271
		container_of(work, struct drm_i915_private,
6189
		container_of(work, struct drm_i915_private,
6272
			     rps.delayed_resume_work.work);
6190
			     rps.delayed_resume_work.work);
6273
	struct drm_device *dev = dev_priv->dev;
6191
	struct drm_device *dev = dev_priv->dev;
6274
 
6192
 
6275
	mutex_lock(&dev_priv->rps.hw_lock);
6193
	mutex_lock(&dev_priv->rps.hw_lock);
6276
 
-
 
6277
	/*
-
 
6278
	 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
-
 
6279
	 * added for it.
-
 
6280
	 */
-
 
6281
	if (INTEL_INFO(dev)->gen < 9)
6194
 
6282
		gen6_reset_rps_interrupts(dev);
6195
	gen6_reset_rps_interrupts(dev);
6283
 
6196
 
6284
	if (IS_CHERRYVIEW(dev)) {
6197
	if (IS_CHERRYVIEW(dev)) {
6285
		cherryview_enable_rps(dev);
6198
		cherryview_enable_rps(dev);
6286
	} else if (IS_VALLEYVIEW(dev)) {
6199
	} else if (IS_VALLEYVIEW(dev)) {
6287
		valleyview_enable_rps(dev);
6200
		valleyview_enable_rps(dev);
6288
	} else if (INTEL_INFO(dev)->gen >= 9) {
6201
	} else if (INTEL_INFO(dev)->gen >= 9) {
-
 
6202
		gen9_enable_rc6(dev);
6289
		gen9_enable_rps(dev);
6203
		gen9_enable_rps(dev);
-
 
6204
		if (IS_SKYLAKE(dev))
-
 
6205
			__gen6_update_ring_freq(dev);
6290
	} else if (IS_BROADWELL(dev)) {
6206
	} else if (IS_BROADWELL(dev)) {
6291
		gen8_enable_rps(dev);
6207
		gen8_enable_rps(dev);
6292
		__gen6_update_ring_freq(dev);
6208
		__gen6_update_ring_freq(dev);
6293
	} else {
6209
	} else {
6294
	gen6_enable_rps(dev);
6210
		gen6_enable_rps(dev);
6295
		__gen6_update_ring_freq(dev);
6211
		__gen6_update_ring_freq(dev);
6296
	}
6212
	}
-
 
6213
 
-
 
6214
	WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
-
 
6215
	WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
-
 
6216
 
-
 
6217
	WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
-
 
6218
	WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
-
 
6219
 
6297
	dev_priv->rps.enabled = true;
6220
	dev_priv->rps.enabled = true;
6298
 
-
 
6299
	if (INTEL_INFO(dev)->gen < 9)
6221
 
6300
		gen6_enable_rps_interrupts(dev);
6222
	gen6_enable_rps_interrupts(dev);
6301
 
6223
 
6302
	mutex_unlock(&dev_priv->rps.hw_lock);
6224
	mutex_unlock(&dev_priv->rps.hw_lock);
6303
 
6225
 
6304
	intel_runtime_pm_put(dev_priv);
6226
	intel_runtime_pm_put(dev_priv);
6305
}
6227
}
6306
 
6228
 
6307
void intel_enable_gt_powersave(struct drm_device *dev)
6229
void intel_enable_gt_powersave(struct drm_device *dev)
6308
{
6230
{
6309
	struct drm_i915_private *dev_priv = dev->dev_private;
6231
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
6232
 
-
 
6233
	/* Powersaving is controlled by the host when inside a VM */
-
 
6234
	if (intel_vgpu_active(dev))
-
 
6235
		return;
6310
 
6236
 
6311
	if (IS_IRONLAKE_M(dev)) {
6237
	if (IS_IRONLAKE_M(dev)) {
6312
		mutex_lock(&dev->struct_mutex);
6238
		mutex_lock(&dev->struct_mutex);
6313
		ironlake_enable_drps(dev);
6239
		ironlake_enable_drps(dev);
6314
		ironlake_enable_rc6(dev);
-
 
6315
		intel_init_emon(dev);
6240
		intel_init_emon(dev);
6316
		mutex_unlock(&dev->struct_mutex);
6241
		mutex_unlock(&dev->struct_mutex);
6317
	} else if (INTEL_INFO(dev)->gen >= 6) {
6242
	} else if (INTEL_INFO(dev)->gen >= 6) {
6318
		/*
6243
		/*
6319
		 * PCU communication is slow and this doesn't need to be
6244
		 * PCU communication is slow and this doesn't need to be
6320
		 * done at any specific time, so do this out of our fast path
6245
		 * done at any specific time, so do this out of our fast path
6321
		 * to make resume and init faster.
6246
		 * to make resume and init faster.
6322
		 *
6247
		 *
6323
		 * We depend on the HW RC6 power context save/restore
6248
		 * We depend on the HW RC6 power context save/restore
6324
		 * mechanism when entering D3 through runtime PM suspend. So
6249
		 * mechanism when entering D3 through runtime PM suspend. So
6325
		 * disable RPM until RPS/RC6 is properly setup. We can only
6250
		 * disable RPM until RPS/RC6 is properly setup. We can only
6326
		 * get here via the driver load/system resume/runtime resume
6251
		 * get here via the driver load/system resume/runtime resume
6327
		 * paths, so the _noresume version is enough (and in case of
6252
		 * paths, so the _noresume version is enough (and in case of
6328
		 * runtime resume it's necessary).
6253
		 * runtime resume it's necessary).
6329
		 */
6254
		 */
6330
		if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6255
		if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6331
					   round_jiffies_up_relative(HZ)))
6256
					   round_jiffies_up_relative(HZ)))
6332
			intel_runtime_pm_get_noresume(dev_priv);
6257
			intel_runtime_pm_get_noresume(dev_priv);
6333
	}
6258
	}
6334
}
6259
}
6335
 
6260
 
6336
void intel_reset_gt_powersave(struct drm_device *dev)
6261
void intel_reset_gt_powersave(struct drm_device *dev)
6337
{
6262
{
6338
	struct drm_i915_private *dev_priv = dev->dev_private;
6263
	struct drm_i915_private *dev_priv = dev->dev_private;
6339
 
6264
 
6340
	if (INTEL_INFO(dev)->gen < 6)
6265
	if (INTEL_INFO(dev)->gen < 6)
6341
		return;
6266
		return;
6342
 
6267
 
6343
	gen6_suspend_rps(dev);
6268
	gen6_suspend_rps(dev);
6344
	dev_priv->rps.enabled = false;
6269
	dev_priv->rps.enabled = false;
6345
}
6270
}
6346
 
6271
 
6347
static void ibx_init_clock_gating(struct drm_device *dev)
6272
static void ibx_init_clock_gating(struct drm_device *dev)
6348
{
6273
{
6349
	struct drm_i915_private *dev_priv = dev->dev_private;
6274
	struct drm_i915_private *dev_priv = dev->dev_private;
6350
 
6275
 
6351
	/*
6276
	/*
6352
	 * On Ibex Peak and Cougar Point, we need to disable clock
6277
	 * On Ibex Peak and Cougar Point, we need to disable clock
6353
	 * gating for the panel power sequencer or it will fail to
6278
	 * gating for the panel power sequencer or it will fail to
6354
	 * start up when no ports are active.
6279
	 * start up when no ports are active.
6355
	 */
6280
	 */
6356
	I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6281
	I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6357
}
6282
}
6358
 
6283
 
6359
static void g4x_disable_trickle_feed(struct drm_device *dev)
6284
static void g4x_disable_trickle_feed(struct drm_device *dev)
6360
{
6285
{
6361
	struct drm_i915_private *dev_priv = dev->dev_private;
6286
	struct drm_i915_private *dev_priv = dev->dev_private;
6362
	int pipe;
6287
	enum pipe pipe;
6363
 
6288
 
6364
	for_each_pipe(dev_priv, pipe) {
6289
	for_each_pipe(dev_priv, pipe) {
6365
		I915_WRITE(DSPCNTR(pipe),
6290
		I915_WRITE(DSPCNTR(pipe),
6366
			   I915_READ(DSPCNTR(pipe)) |
6291
			   I915_READ(DSPCNTR(pipe)) |
6367
			   DISPPLANE_TRICKLE_FEED_DISABLE);
6292
			   DISPPLANE_TRICKLE_FEED_DISABLE);
-
 
6293
 
6368
		intel_flush_primary_plane(dev_priv, pipe);
6294
		I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
-
 
6295
		POSTING_READ(DSPSURF(pipe));
6369
	}
6296
	}
6370
}
6297
}
6371
 
6298
 
6372
static void ilk_init_lp_watermarks(struct drm_device *dev)
6299
static void ilk_init_lp_watermarks(struct drm_device *dev)
6373
{
6300
{
6374
	struct drm_i915_private *dev_priv = dev->dev_private;
6301
	struct drm_i915_private *dev_priv = dev->dev_private;
6375
 
6302
 
6376
	I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6303
	I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6377
	I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6304
	I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6378
	I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6305
	I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6379
 
6306
 
6380
	/*
6307
	/*
6381
	 * Don't touch WM1S_LP_EN here.
6308
	 * Don't touch WM1S_LP_EN here.
6382
	 * Doing so could cause underruns.
6309
	 * Doing so could cause underruns.
6383
	 */
6310
	 */
6384
}
6311
}
6385
 
6312
 
6386
static void ironlake_init_clock_gating(struct drm_device *dev)
6313
static void ironlake_init_clock_gating(struct drm_device *dev)
6387
{
6314
{
6388
	struct drm_i915_private *dev_priv = dev->dev_private;
6315
	struct drm_i915_private *dev_priv = dev->dev_private;
6389
	uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6316
	uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6390
 
6317
 
6391
	/*
6318
	/*
6392
	 * Required for FBC
6319
	 * Required for FBC
6393
	 * WaFbcDisableDpfcClockGating:ilk
6320
	 * WaFbcDisableDpfcClockGating:ilk
6394
	 */
6321
	 */
6395
	dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6322
	dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6396
		   ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6323
		   ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6397
		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
6324
		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
6398
 
6325
 
6399
	I915_WRITE(PCH_3DCGDIS0,
6326
	I915_WRITE(PCH_3DCGDIS0,
6400
		   MARIUNIT_CLOCK_GATE_DISABLE |
6327
		   MARIUNIT_CLOCK_GATE_DISABLE |
6401
		   SVSMUNIT_CLOCK_GATE_DISABLE);
6328
		   SVSMUNIT_CLOCK_GATE_DISABLE);
6402
	I915_WRITE(PCH_3DCGDIS1,
6329
	I915_WRITE(PCH_3DCGDIS1,
6403
		   VFMUNIT_CLOCK_GATE_DISABLE);
6330
		   VFMUNIT_CLOCK_GATE_DISABLE);
6404
 
6331
 
6405
	/*
6332
	/*
6406
	 * According to the spec the following bits should be set in
6333
	 * According to the spec the following bits should be set in
6407
	 * order to enable memory self-refresh
6334
	 * order to enable memory self-refresh
6408
	 * The bit 22/21 of 0x42004
6335
	 * The bit 22/21 of 0x42004
6409
	 * The bit 5 of 0x42020
6336
	 * The bit 5 of 0x42020
6410
	 * The bit 15 of 0x45000
6337
	 * The bit 15 of 0x45000
6411
	 */
6338
	 */
6412
	I915_WRITE(ILK_DISPLAY_CHICKEN2,
6339
	I915_WRITE(ILK_DISPLAY_CHICKEN2,
6413
		   (I915_READ(ILK_DISPLAY_CHICKEN2) |
6340
		   (I915_READ(ILK_DISPLAY_CHICKEN2) |
6414
		    ILK_DPARB_GATE | ILK_VSDPFD_FULL));
6341
		    ILK_DPARB_GATE | ILK_VSDPFD_FULL));
6415
	dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
6342
	dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
6416
	I915_WRITE(DISP_ARB_CTL,
6343
	I915_WRITE(DISP_ARB_CTL,
6417
		   (I915_READ(DISP_ARB_CTL) |
6344
		   (I915_READ(DISP_ARB_CTL) |
6418
		    DISP_FBC_WM_DIS));
6345
		    DISP_FBC_WM_DIS));
6419
 
6346
 
6420
	ilk_init_lp_watermarks(dev);
6347
	ilk_init_lp_watermarks(dev);
6421
 
6348
 
6422
	/*
6349
	/*
6423
	 * Based on the document from hardware guys the following bits
6350
	 * Based on the document from hardware guys the following bits
6424
	 * should be set unconditionally in order to enable FBC.
6351
	 * should be set unconditionally in order to enable FBC.
6425
	 * The bit 22 of 0x42000
6352
	 * The bit 22 of 0x42000
6426
	 * The bit 22 of 0x42004
6353
	 * The bit 22 of 0x42004
6427
	 * The bit 7,8,9 of 0x42020.
6354
	 * The bit 7,8,9 of 0x42020.
6428
	 */
6355
	 */
6429
	if (IS_IRONLAKE_M(dev)) {
6356
	if (IS_IRONLAKE_M(dev)) {
6430
		/* WaFbcAsynchFlipDisableFbcQueue:ilk */
6357
		/* WaFbcAsynchFlipDisableFbcQueue:ilk */
6431
		I915_WRITE(ILK_DISPLAY_CHICKEN1,
6358
		I915_WRITE(ILK_DISPLAY_CHICKEN1,
6432
			   I915_READ(ILK_DISPLAY_CHICKEN1) |
6359
			   I915_READ(ILK_DISPLAY_CHICKEN1) |
6433
			   ILK_FBCQ_DIS);
6360
			   ILK_FBCQ_DIS);
6434
		I915_WRITE(ILK_DISPLAY_CHICKEN2,
6361
		I915_WRITE(ILK_DISPLAY_CHICKEN2,
6435
			   I915_READ(ILK_DISPLAY_CHICKEN2) |
6362
			   I915_READ(ILK_DISPLAY_CHICKEN2) |
6436
			   ILK_DPARB_GATE);
6363
			   ILK_DPARB_GATE);
6437
	}
6364
	}
6438
 
6365
 
6439
	I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6366
	I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6440
 
6367
 
6441
	I915_WRITE(ILK_DISPLAY_CHICKEN2,
6368
	I915_WRITE(ILK_DISPLAY_CHICKEN2,
6442
		   I915_READ(ILK_DISPLAY_CHICKEN2) |
6369
		   I915_READ(ILK_DISPLAY_CHICKEN2) |
6443
		   ILK_ELPIN_409_SELECT);
6370
		   ILK_ELPIN_409_SELECT);
6444
	I915_WRITE(_3D_CHICKEN2,
6371
	I915_WRITE(_3D_CHICKEN2,
6445
		   _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6372
		   _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6446
		   _3D_CHICKEN2_WM_READ_PIPELINED);
6373
		   _3D_CHICKEN2_WM_READ_PIPELINED);
6447
 
6374
 
6448
	/* WaDisableRenderCachePipelinedFlush:ilk */
6375
	/* WaDisableRenderCachePipelinedFlush:ilk */
6449
	I915_WRITE(CACHE_MODE_0,
6376
	I915_WRITE(CACHE_MODE_0,
6450
		   _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6377
		   _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6451
 
6378
 
6452
	/* WaDisable_RenderCache_OperationalFlush:ilk */
6379
	/* WaDisable_RenderCache_OperationalFlush:ilk */
6453
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6380
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6454
 
6381
 
6455
	g4x_disable_trickle_feed(dev);
6382
	g4x_disable_trickle_feed(dev);
6456
 
6383
 
6457
	ibx_init_clock_gating(dev);
6384
	ibx_init_clock_gating(dev);
6458
}
6385
}
6459
 
6386
 
6460
static void cpt_init_clock_gating(struct drm_device *dev)
6387
static void cpt_init_clock_gating(struct drm_device *dev)
6461
{
6388
{
6462
	struct drm_i915_private *dev_priv = dev->dev_private;
6389
	struct drm_i915_private *dev_priv = dev->dev_private;
6463
	int pipe;
6390
	int pipe;
6464
	uint32_t val;
6391
	uint32_t val;
6465
 
6392
 
6466
	/*
6393
	/*
6467
	 * On Ibex Peak and Cougar Point, we need to disable clock
6394
	 * On Ibex Peak and Cougar Point, we need to disable clock
6468
	 * gating for the panel power sequencer or it will fail to
6395
	 * gating for the panel power sequencer or it will fail to
6469
	 * start up when no ports are active.
6396
	 * start up when no ports are active.
6470
	 */
6397
	 */
6471
	I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6398
	I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6472
		   PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6399
		   PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6473
		   PCH_CPUNIT_CLOCK_GATE_DISABLE);
6400
		   PCH_CPUNIT_CLOCK_GATE_DISABLE);
6474
	I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6401
	I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6475
		   DPLS_EDP_PPS_FIX_DIS);
6402
		   DPLS_EDP_PPS_FIX_DIS);
6476
	/* The below fixes the weird display corruption, a few pixels shifted
6403
	/* The below fixes the weird display corruption, a few pixels shifted
6477
	 * downward, on (only) LVDS of some HP laptops with IVY.
6404
	 * downward, on (only) LVDS of some HP laptops with IVY.
6478
	 */
6405
	 */
6479
	for_each_pipe(dev_priv, pipe) {
6406
	for_each_pipe(dev_priv, pipe) {
6480
		val = I915_READ(TRANS_CHICKEN2(pipe));
6407
		val = I915_READ(TRANS_CHICKEN2(pipe));
6481
		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6408
		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6482
		val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6409
		val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6483
		if (dev_priv->vbt.fdi_rx_polarity_inverted)
6410
		if (dev_priv->vbt.fdi_rx_polarity_inverted)
6484
			val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6411
			val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6485
		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6412
		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6486
		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6413
		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6487
		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
6414
		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
6488
		I915_WRITE(TRANS_CHICKEN2(pipe), val);
6415
		I915_WRITE(TRANS_CHICKEN2(pipe), val);
6489
	}
6416
	}
6490
	/* WADP0ClockGatingDisable */
6417
	/* WADP0ClockGatingDisable */
6491
	for_each_pipe(dev_priv, pipe) {
6418
	for_each_pipe(dev_priv, pipe) {
6492
		I915_WRITE(TRANS_CHICKEN1(pipe),
6419
		I915_WRITE(TRANS_CHICKEN1(pipe),
6493
			   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6420
			   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6494
	}
6421
	}
6495
}
6422
}
6496
 
6423
 
6497
static void gen6_check_mch_setup(struct drm_device *dev)
6424
static void gen6_check_mch_setup(struct drm_device *dev)
6498
{
6425
{
6499
	struct drm_i915_private *dev_priv = dev->dev_private;
6426
	struct drm_i915_private *dev_priv = dev->dev_private;
6500
	uint32_t tmp;
6427
	uint32_t tmp;
6501
 
6428
 
6502
	tmp = I915_READ(MCH_SSKPD);
6429
	tmp = I915_READ(MCH_SSKPD);
6503
	if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6430
	if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6504
		DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6431
		DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6505
			      tmp);
6432
			      tmp);
6506
}
6433
}
6507
 
6434
 
6508
static void gen6_init_clock_gating(struct drm_device *dev)
6435
static void gen6_init_clock_gating(struct drm_device *dev)
6509
{
6436
{
6510
	struct drm_i915_private *dev_priv = dev->dev_private;
6437
	struct drm_i915_private *dev_priv = dev->dev_private;
6511
	uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6438
	uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6512
 
6439
 
6513
	I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6440
	I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6514
 
6441
 
6515
	I915_WRITE(ILK_DISPLAY_CHICKEN2,
6442
	I915_WRITE(ILK_DISPLAY_CHICKEN2,
6516
		   I915_READ(ILK_DISPLAY_CHICKEN2) |
6443
		   I915_READ(ILK_DISPLAY_CHICKEN2) |
6517
		   ILK_ELPIN_409_SELECT);
6444
		   ILK_ELPIN_409_SELECT);
6518
 
6445
 
6519
	/* WaDisableHiZPlanesWhenMSAAEnabled:snb */
6446
	/* WaDisableHiZPlanesWhenMSAAEnabled:snb */
6520
	I915_WRITE(_3D_CHICKEN,
6447
	I915_WRITE(_3D_CHICKEN,
6521
		   _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6448
		   _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6522
 
6449
 
6523
	/* WaDisable_RenderCache_OperationalFlush:snb */
6450
	/* WaDisable_RenderCache_OperationalFlush:snb */
6524
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6451
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6525
 
6452
 
6526
	/*
6453
	/*
6527
	 * BSpec recoomends 8x4 when MSAA is used,
6454
	 * BSpec recoomends 8x4 when MSAA is used,
6528
	 * however in practice 16x4 seems fastest.
6455
	 * however in practice 16x4 seems fastest.
6529
	 *
6456
	 *
6530
	 * Note that PS/WM thread counts depend on the WIZ hashing
6457
	 * Note that PS/WM thread counts depend on the WIZ hashing
6531
	 * disable bit, which we don't touch here, but it's good
6458
	 * disable bit, which we don't touch here, but it's good
6532
	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6459
	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6533
	 */
6460
	 */
6534
	I915_WRITE(GEN6_GT_MODE,
6461
	I915_WRITE(GEN6_GT_MODE,
6535
		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6462
		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6536
 
6463
 
6537
	ilk_init_lp_watermarks(dev);
6464
	ilk_init_lp_watermarks(dev);
6538
 
6465
 
6539
	I915_WRITE(CACHE_MODE_0,
6466
	I915_WRITE(CACHE_MODE_0,
6540
		   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
6467
		   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
6541
 
6468
 
6542
	I915_WRITE(GEN6_UCGCTL1,
6469
	I915_WRITE(GEN6_UCGCTL1,
6543
		   I915_READ(GEN6_UCGCTL1) |
6470
		   I915_READ(GEN6_UCGCTL1) |
6544
		   GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6471
		   GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6545
		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6472
		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6546
 
6473
 
6547
	/* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6474
	/* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6548
	 * gating disable must be set.  Failure to set it results in
6475
	 * gating disable must be set.  Failure to set it results in
6549
	 * flickering pixels due to Z write ordering failures after
6476
	 * flickering pixels due to Z write ordering failures after
6550
	 * some amount of runtime in the Mesa "fire" demo, and Unigine
6477
	 * some amount of runtime in the Mesa "fire" demo, and Unigine
6551
	 * Sanctuary and Tropics, and apparently anything else with
6478
	 * Sanctuary and Tropics, and apparently anything else with
6552
	 * alpha test or pixel discard.
6479
	 * alpha test or pixel discard.
6553
	 *
6480
	 *
6554
	 * According to the spec, bit 11 (RCCUNIT) must also be set,
6481
	 * According to the spec, bit 11 (RCCUNIT) must also be set,
6555
	 * but we didn't debug actual testcases to find it out.
6482
	 * but we didn't debug actual testcases to find it out.
6556
	 *
6483
	 *
6557
	 * WaDisableRCCUnitClockGating:snb
6484
	 * WaDisableRCCUnitClockGating:snb
6558
	 * WaDisableRCPBUnitClockGating:snb
6485
	 * WaDisableRCPBUnitClockGating:snb
6559
	 */
6486
	 */
6560
	I915_WRITE(GEN6_UCGCTL2,
6487
	I915_WRITE(GEN6_UCGCTL2,
6561
		   GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6488
		   GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6562
		   GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6489
		   GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6563
 
6490
 
6564
	/* WaStripsFansDisableFastClipPerformanceFix:snb */
6491
	/* WaStripsFansDisableFastClipPerformanceFix:snb */
6565
	I915_WRITE(_3D_CHICKEN3,
6492
	I915_WRITE(_3D_CHICKEN3,
6566
		   _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
6493
		   _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
6567
 
6494
 
6568
	/*
6495
	/*
6569
	 * Bspec says:
6496
	 * Bspec says:
6570
	 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6497
	 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6571
	 * 3DSTATE_SF number of SF output attributes is more than 16."
6498
	 * 3DSTATE_SF number of SF output attributes is more than 16."
6572
	 */
6499
	 */
6573
	I915_WRITE(_3D_CHICKEN3,
6500
	I915_WRITE(_3D_CHICKEN3,
6574
		   _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6501
		   _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6575
 
6502
 
6576
	/*
6503
	/*
6577
	 * According to the spec the following bits should be
6504
	 * According to the spec the following bits should be
6578
	 * set in order to enable memory self-refresh and fbc:
6505
	 * set in order to enable memory self-refresh and fbc:
6579
	 * The bit21 and bit22 of 0x42000
6506
	 * The bit21 and bit22 of 0x42000
6580
	 * The bit21 and bit22 of 0x42004
6507
	 * The bit21 and bit22 of 0x42004
6581
	 * The bit5 and bit7 of 0x42020
6508
	 * The bit5 and bit7 of 0x42020
6582
	 * The bit14 of 0x70180
6509
	 * The bit14 of 0x70180
6583
	 * The bit14 of 0x71180
6510
	 * The bit14 of 0x71180
6584
	 *
6511
	 *
6585
	 * WaFbcAsynchFlipDisableFbcQueue:snb
6512
	 * WaFbcAsynchFlipDisableFbcQueue:snb
6586
	 */
6513
	 */
6587
	I915_WRITE(ILK_DISPLAY_CHICKEN1,
6514
	I915_WRITE(ILK_DISPLAY_CHICKEN1,
6588
		   I915_READ(ILK_DISPLAY_CHICKEN1) |
6515
		   I915_READ(ILK_DISPLAY_CHICKEN1) |
6589
		   ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6516
		   ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6590
	I915_WRITE(ILK_DISPLAY_CHICKEN2,
6517
	I915_WRITE(ILK_DISPLAY_CHICKEN2,
6591
		   I915_READ(ILK_DISPLAY_CHICKEN2) |
6518
		   I915_READ(ILK_DISPLAY_CHICKEN2) |
6592
		   ILK_DPARB_GATE | ILK_VSDPFD_FULL);
6519
		   ILK_DPARB_GATE | ILK_VSDPFD_FULL);
6593
	I915_WRITE(ILK_DSPCLK_GATE_D,
6520
	I915_WRITE(ILK_DSPCLK_GATE_D,
6594
		   I915_READ(ILK_DSPCLK_GATE_D) |
6521
		   I915_READ(ILK_DSPCLK_GATE_D) |
6595
		   ILK_DPARBUNIT_CLOCK_GATE_ENABLE  |
6522
		   ILK_DPARBUNIT_CLOCK_GATE_ENABLE  |
6596
		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
6523
		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
6597
 
6524
 
6598
	g4x_disable_trickle_feed(dev);
6525
	g4x_disable_trickle_feed(dev);
6599
 
6526
 
6600
	cpt_init_clock_gating(dev);
6527
	cpt_init_clock_gating(dev);
6601
 
6528
 
6602
	gen6_check_mch_setup(dev);
6529
	gen6_check_mch_setup(dev);
6603
}
6530
}
6604
 
6531
 
6605
static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6532
static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6606
{
6533
{
6607
	uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6534
	uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6608
 
6535
 
6609
	/*
6536
	/*
6610
	 * WaVSThreadDispatchOverride:ivb,vlv
6537
	 * WaVSThreadDispatchOverride:ivb,vlv
6611
	 *
6538
	 *
6612
	 * This actually overrides the dispatch
6539
	 * This actually overrides the dispatch
6613
	 * mode for all thread types.
6540
	 * mode for all thread types.
6614
	 */
6541
	 */
6615
	reg &= ~GEN7_FF_SCHED_MASK;
6542
	reg &= ~GEN7_FF_SCHED_MASK;
6616
	reg |= GEN7_FF_TS_SCHED_HW;
6543
	reg |= GEN7_FF_TS_SCHED_HW;
6617
	reg |= GEN7_FF_VS_SCHED_HW;
6544
	reg |= GEN7_FF_VS_SCHED_HW;
6618
	reg |= GEN7_FF_DS_SCHED_HW;
6545
	reg |= GEN7_FF_DS_SCHED_HW;
6619
 
6546
 
6620
	I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6547
	I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6621
}
6548
}
6622
 
6549
 
6623
static void lpt_init_clock_gating(struct drm_device *dev)
6550
static void lpt_init_clock_gating(struct drm_device *dev)
6624
{
6551
{
6625
	struct drm_i915_private *dev_priv = dev->dev_private;
6552
	struct drm_i915_private *dev_priv = dev->dev_private;
6626
 
6553
 
6627
	/*
6554
	/*
6628
	 * TODO: this bit should only be enabled when really needed, then
6555
	 * TODO: this bit should only be enabled when really needed, then
6629
	 * disabled when not needed anymore in order to save power.
6556
	 * disabled when not needed anymore in order to save power.
6630
	 */
6557
	 */
6631
	if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
6558
	if (HAS_PCH_LPT_LP(dev))
6632
		I915_WRITE(SOUTH_DSPCLK_GATE_D,
6559
		I915_WRITE(SOUTH_DSPCLK_GATE_D,
6633
			   I915_READ(SOUTH_DSPCLK_GATE_D) |
6560
			   I915_READ(SOUTH_DSPCLK_GATE_D) |
6634
			   PCH_LP_PARTITION_LEVEL_DISABLE);
6561
			   PCH_LP_PARTITION_LEVEL_DISABLE);
6635
 
6562
 
6636
	/* WADPOClockGatingDisable:hsw */
6563
	/* WADPOClockGatingDisable:hsw */
6637
	I915_WRITE(_TRANSA_CHICKEN1,
6564
	I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6638
		   I915_READ(_TRANSA_CHICKEN1) |
6565
		   I915_READ(TRANS_CHICKEN1(PIPE_A)) |
6639
		   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6566
		   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6640
}
6567
}
6641
 
6568
 
6642
static void lpt_suspend_hw(struct drm_device *dev)
6569
static void lpt_suspend_hw(struct drm_device *dev)
6643
{
6570
{
6644
	struct drm_i915_private *dev_priv = dev->dev_private;
6571
	struct drm_i915_private *dev_priv = dev->dev_private;
6645
 
6572
 
6646
	if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6573
	if (HAS_PCH_LPT_LP(dev)) {
6647
		uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6574
		uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6648
 
6575
 
6649
		val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6576
		val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6650
		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6577
		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6651
	}
6578
	}
6652
}
6579
}
6653
 
6580
 
6654
static void broadwell_init_clock_gating(struct drm_device *dev)
6581
static void broadwell_init_clock_gating(struct drm_device *dev)
6655
{
6582
{
6656
	struct drm_i915_private *dev_priv = dev->dev_private;
6583
	struct drm_i915_private *dev_priv = dev->dev_private;
6657
	enum pipe pipe;
6584
	enum pipe pipe;
-
 
6585
	uint32_t misccpctl;
6658
 
-
 
6659
	I915_WRITE(WM3_LP_ILK, 0);
-
 
6660
	I915_WRITE(WM2_LP_ILK, 0);
6586
 
6661
	I915_WRITE(WM1_LP_ILK, 0);
6587
	ilk_init_lp_watermarks(dev);
6662
 
6588
 
6663
	/* WaSwitchSolVfFArbitrationPriority:bdw */
6589
	/* WaSwitchSolVfFArbitrationPriority:bdw */
6664
	I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6590
	I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6665
 
6591
 
6666
	/* WaPsrDPAMaskVBlankInSRD:bdw */
6592
	/* WaPsrDPAMaskVBlankInSRD:bdw */
6667
	I915_WRITE(CHICKEN_PAR1_1,
6593
	I915_WRITE(CHICKEN_PAR1_1,
6668
		   I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6594
		   I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6669
 
6595
 
6670
	/* WaPsrDPRSUnmaskVBlankInSRD:bdw */
6596
	/* WaPsrDPRSUnmaskVBlankInSRD:bdw */
6671
	for_each_pipe(dev_priv, pipe) {
6597
	for_each_pipe(dev_priv, pipe) {
6672
		I915_WRITE(CHICKEN_PIPESL_1(pipe),
6598
		I915_WRITE(CHICKEN_PIPESL_1(pipe),
6673
			   I915_READ(CHICKEN_PIPESL_1(pipe)) |
6599
			   I915_READ(CHICKEN_PIPESL_1(pipe)) |
6674
			   BDW_DPRS_MASK_VBLANK_SRD);
6600
			   BDW_DPRS_MASK_VBLANK_SRD);
6675
	}
6601
	}
6676
 
6602
 
6677
	/* WaVSRefCountFullforceMissDisable:bdw */
6603
	/* WaVSRefCountFullforceMissDisable:bdw */
6678
	/* WaDSRefCountFullforceMissDisable:bdw */
6604
	/* WaDSRefCountFullforceMissDisable:bdw */
6679
	I915_WRITE(GEN7_FF_THREAD_MODE,
6605
	I915_WRITE(GEN7_FF_THREAD_MODE,
6680
		   I915_READ(GEN7_FF_THREAD_MODE) &
6606
		   I915_READ(GEN7_FF_THREAD_MODE) &
6681
		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6607
		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6682
 
6608
 
6683
	I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6609
	I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6684
		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6610
		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6685
 
6611
 
6686
	/* WaDisableSDEUnitClockGating:bdw */
6612
	/* WaDisableSDEUnitClockGating:bdw */
6687
	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6613
	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6688
		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6614
		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
-
 
6615
 
-
 
6616
	/*
-
 
6617
	 * WaProgramL3SqcReg1Default:bdw
-
 
6618
	 * WaTempDisableDOPClkGating:bdw
-
 
6619
	 */
-
 
6620
	misccpctl = I915_READ(GEN7_MISCCPCTL);
-
 
6621
	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
-
 
6622
	I915_WRITE(GEN8_L3SQCREG1, BDW_WA_L3SQCREG1_DEFAULT);
-
 
6623
	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
-
 
6624
 
-
 
6625
	/*
-
 
6626
	 * WaGttCachingOffByDefault:bdw
-
 
6627
	 * GTT cache may not work with big pages, so if those
-
 
6628
	 * are ever enabled GTT cache may need to be disabled.
-
 
6629
	 */
-
 
6630
	I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6689
 
6631
 
6690
	lpt_init_clock_gating(dev);
6632
	lpt_init_clock_gating(dev);
6691
}
6633
}
6692
 
6634
 
6693
static void haswell_init_clock_gating(struct drm_device *dev)
6635
static void haswell_init_clock_gating(struct drm_device *dev)
6694
{
6636
{
6695
	struct drm_i915_private *dev_priv = dev->dev_private;
6637
	struct drm_i915_private *dev_priv = dev->dev_private;
6696
 
6638
 
6697
	ilk_init_lp_watermarks(dev);
6639
	ilk_init_lp_watermarks(dev);
6698
 
6640
 
6699
	/* L3 caching of data atomics doesn't work -- disable it. */
6641
	/* L3 caching of data atomics doesn't work -- disable it. */
6700
	I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6642
	I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6701
	I915_WRITE(HSW_ROW_CHICKEN3,
6643
	I915_WRITE(HSW_ROW_CHICKEN3,
6702
		   _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6644
		   _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6703
 
6645
 
6704
	/* This is required by WaCatErrorRejectionIssue:hsw */
6646
	/* This is required by WaCatErrorRejectionIssue:hsw */
6705
	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6647
	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6706
			I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6648
			I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6707
			GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6649
			GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6708
 
6650
 
6709
	/* WaVSRefCountFullforceMissDisable:hsw */
6651
	/* WaVSRefCountFullforceMissDisable:hsw */
6710
	I915_WRITE(GEN7_FF_THREAD_MODE,
6652
	I915_WRITE(GEN7_FF_THREAD_MODE,
6711
		   I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
6653
		   I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
6712
 
6654
 
6713
	/* WaDisable_RenderCache_OperationalFlush:hsw */
6655
	/* WaDisable_RenderCache_OperationalFlush:hsw */
6714
	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6656
	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6715
 
6657
 
6716
	/* enable HiZ Raw Stall Optimization */
6658
	/* enable HiZ Raw Stall Optimization */
6717
	I915_WRITE(CACHE_MODE_0_GEN7,
6659
	I915_WRITE(CACHE_MODE_0_GEN7,
6718
		   _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6660
		   _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6719
 
6661
 
6720
	/* WaDisable4x2SubspanOptimization:hsw */
6662
	/* WaDisable4x2SubspanOptimization:hsw */
6721
	I915_WRITE(CACHE_MODE_1,
6663
	I915_WRITE(CACHE_MODE_1,
6722
		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6664
		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6723
 
6665
 
6724
	/*
6666
	/*
6725
	 * BSpec recommends 8x4 when MSAA is used,
6667
	 * BSpec recommends 8x4 when MSAA is used,
6726
	 * however in practice 16x4 seems fastest.
6668
	 * however in practice 16x4 seems fastest.
6727
	 *
6669
	 *
6728
	 * Note that PS/WM thread counts depend on the WIZ hashing
6670
	 * Note that PS/WM thread counts depend on the WIZ hashing
6729
	 * disable bit, which we don't touch here, but it's good
6671
	 * disable bit, which we don't touch here, but it's good
6730
	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6672
	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6731
	 */
6673
	 */
6732
	I915_WRITE(GEN7_GT_MODE,
6674
	I915_WRITE(GEN7_GT_MODE,
6733
		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6675
		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
-
 
6676
 
-
 
6677
	/* WaSampleCChickenBitEnable:hsw */
-
 
6678
	I915_WRITE(HALF_SLICE_CHICKEN3,
-
 
6679
		   _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6734
 
6680
 
6735
	/* WaSwitchSolVfFArbitrationPriority:hsw */
6681
	/* WaSwitchSolVfFArbitrationPriority:hsw */
6736
	I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6682
	I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6737
 
6683
 
6738
	/* WaRsPkgCStateDisplayPMReq:hsw */
6684
	/* WaRsPkgCStateDisplayPMReq:hsw */
6739
	I915_WRITE(CHICKEN_PAR1_1,
6685
	I915_WRITE(CHICKEN_PAR1_1,
6740
		   I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
6686
		   I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
6741
 
6687
 
6742
	lpt_init_clock_gating(dev);
6688
	lpt_init_clock_gating(dev);
6743
}
6689
}
6744
 
6690
 
6745
static void ivybridge_init_clock_gating(struct drm_device *dev)
6691
static void ivybridge_init_clock_gating(struct drm_device *dev)
6746
{
6692
{
6747
	struct drm_i915_private *dev_priv = dev->dev_private;
6693
	struct drm_i915_private *dev_priv = dev->dev_private;
6748
	uint32_t snpcr;
6694
	uint32_t snpcr;
6749
 
6695
 
6750
	ilk_init_lp_watermarks(dev);
6696
	ilk_init_lp_watermarks(dev);
6751
 
6697
 
6752
	I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
6698
	I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
6753
 
6699
 
6754
	/* WaDisableEarlyCull:ivb */
6700
	/* WaDisableEarlyCull:ivb */
6755
	I915_WRITE(_3D_CHICKEN3,
6701
	I915_WRITE(_3D_CHICKEN3,
6756
		   _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6702
		   _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6757
 
6703
 
6758
	/* WaDisableBackToBackFlipFix:ivb */
6704
	/* WaDisableBackToBackFlipFix:ivb */
6759
	I915_WRITE(IVB_CHICKEN3,
6705
	I915_WRITE(IVB_CHICKEN3,
6760
		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6706
		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6761
		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
6707
		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
6762
 
6708
 
6763
	/* WaDisablePSDDualDispatchEnable:ivb */
6709
	/* WaDisablePSDDualDispatchEnable:ivb */
6764
	if (IS_IVB_GT1(dev))
6710
	if (IS_IVB_GT1(dev))
6765
		I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6711
		I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6766
			   _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6712
			   _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6767
 
6713
 
6768
	/* WaDisable_RenderCache_OperationalFlush:ivb */
6714
	/* WaDisable_RenderCache_OperationalFlush:ivb */
6769
	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6715
	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6770
 
6716
 
6771
	/* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
6717
	/* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
6772
	I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6718
	I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6773
		   GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6719
		   GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6774
 
6720
 
6775
	/* WaApplyL3ControlAndL3ChickenMode:ivb */
6721
	/* WaApplyL3ControlAndL3ChickenMode:ivb */
6776
	I915_WRITE(GEN7_L3CNTLREG1,
6722
	I915_WRITE(GEN7_L3CNTLREG1,
6777
			GEN7_WA_FOR_GEN7_L3_CONTROL);
6723
			GEN7_WA_FOR_GEN7_L3_CONTROL);
6778
	I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
6724
	I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
6779
			GEN7_WA_L3_CHICKEN_MODE);
6725
		   GEN7_WA_L3_CHICKEN_MODE);
6780
	if (IS_IVB_GT1(dev))
6726
	if (IS_IVB_GT1(dev))
6781
		I915_WRITE(GEN7_ROW_CHICKEN2,
6727
		I915_WRITE(GEN7_ROW_CHICKEN2,
6782
			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6728
			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6783
	else {
6729
	else {
6784
		/* must write both registers */
6730
		/* must write both registers */
6785
		I915_WRITE(GEN7_ROW_CHICKEN2,
6731
		I915_WRITE(GEN7_ROW_CHICKEN2,
6786
			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6732
			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6787
		I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6733
		I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6788
			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6734
			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6789
	}
6735
	}
6790
 
6736
 
6791
	/* WaForceL3Serialization:ivb */
6737
	/* WaForceL3Serialization:ivb */
6792
	I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6738
	I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6793
		   ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6739
		   ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6794
 
6740
 
6795
	/*
6741
	/*
6796
	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6742
	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6797
	 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
6743
	 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
6798
	 */
6744
	 */
6799
	I915_WRITE(GEN6_UCGCTL2,
6745
	I915_WRITE(GEN6_UCGCTL2,
6800
		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6746
		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6801
 
6747
 
6802
	/* This is required by WaCatErrorRejectionIssue:ivb */
6748
	/* This is required by WaCatErrorRejectionIssue:ivb */
6803
	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6749
	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6804
			I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6750
			I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6805
			GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6751
			GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6806
 
6752
 
6807
	g4x_disable_trickle_feed(dev);
6753
	g4x_disable_trickle_feed(dev);
6808
 
6754
 
6809
	gen7_setup_fixed_func_scheduler(dev_priv);
6755
	gen7_setup_fixed_func_scheduler(dev_priv);
6810
 
6756
 
6811
	if (0) { /* causes HiZ corruption on ivb:gt1 */
6757
	if (0) { /* causes HiZ corruption on ivb:gt1 */
6812
		/* enable HiZ Raw Stall Optimization */
6758
		/* enable HiZ Raw Stall Optimization */
6813
		I915_WRITE(CACHE_MODE_0_GEN7,
6759
		I915_WRITE(CACHE_MODE_0_GEN7,
6814
			   _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6760
			   _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6815
	}
6761
	}
6816
 
6762
 
6817
	/* WaDisable4x2SubspanOptimization:ivb */
6763
	/* WaDisable4x2SubspanOptimization:ivb */
6818
	I915_WRITE(CACHE_MODE_1,
6764
	I915_WRITE(CACHE_MODE_1,
6819
		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6765
		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6820
 
6766
 
6821
	/*
6767
	/*
6822
	 * BSpec recommends 8x4 when MSAA is used,
6768
	 * BSpec recommends 8x4 when MSAA is used,
6823
	 * however in practice 16x4 seems fastest.
6769
	 * however in practice 16x4 seems fastest.
6824
	 *
6770
	 *
6825
	 * Note that PS/WM thread counts depend on the WIZ hashing
6771
	 * Note that PS/WM thread counts depend on the WIZ hashing
6826
	 * disable bit, which we don't touch here, but it's good
6772
	 * disable bit, which we don't touch here, but it's good
6827
	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6773
	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6828
	 */
6774
	 */
6829
	I915_WRITE(GEN7_GT_MODE,
6775
	I915_WRITE(GEN7_GT_MODE,
6830
		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6776
		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6831
 
6777
 
6832
	snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6778
	snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6833
	snpcr &= ~GEN6_MBC_SNPCR_MASK;
6779
	snpcr &= ~GEN6_MBC_SNPCR_MASK;
6834
	snpcr |= GEN6_MBC_SNPCR_MED;
6780
	snpcr |= GEN6_MBC_SNPCR_MED;
6835
	I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
6781
	I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
6836
 
6782
 
6837
	if (!HAS_PCH_NOP(dev))
6783
	if (!HAS_PCH_NOP(dev))
6838
	cpt_init_clock_gating(dev);
6784
		cpt_init_clock_gating(dev);
6839
 
6785
 
6840
	gen6_check_mch_setup(dev);
6786
	gen6_check_mch_setup(dev);
6841
}
6787
}
-
 
6788
 
-
 
6789
static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
-
 
6790
{
-
 
6791
	I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
-
 
6792
 
-
 
6793
	/*
-
 
6794
	 * Disable trickle feed and enable pnd deadline calculation
-
 
6795
	 */
-
 
6796
	I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
-
 
6797
	I915_WRITE(CBR1_VLV, 0);
-
 
6798
}
6842
 
6799
 
6843
static void valleyview_init_clock_gating(struct drm_device *dev)
6800
static void valleyview_init_clock_gating(struct drm_device *dev)
6844
{
6801
{
6845
	struct drm_i915_private *dev_priv = dev->dev_private;
6802
	struct drm_i915_private *dev_priv = dev->dev_private;
6846
 
6803
 
6847
	I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6804
	vlv_init_display_clock_gating(dev_priv);
6848
 
6805
 
6849
	/* WaDisableEarlyCull:vlv */
6806
	/* WaDisableEarlyCull:vlv */
6850
	I915_WRITE(_3D_CHICKEN3,
6807
	I915_WRITE(_3D_CHICKEN3,
6851
		   _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6808
		   _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6852
 
6809
 
6853
	/* WaDisableBackToBackFlipFix:vlv */
6810
	/* WaDisableBackToBackFlipFix:vlv */
6854
	I915_WRITE(IVB_CHICKEN3,
6811
	I915_WRITE(IVB_CHICKEN3,
6855
		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6812
		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6856
		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
6813
		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
6857
 
6814
 
6858
	/* WaPsdDispatchEnable:vlv */
6815
	/* WaPsdDispatchEnable:vlv */
6859
	/* WaDisablePSDDualDispatchEnable:vlv */
6816
	/* WaDisablePSDDualDispatchEnable:vlv */
6860
	I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6817
	I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6861
		   _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6818
		   _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6862
				      GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6819
				      GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6863
 
6820
 
6864
	/* WaDisable_RenderCache_OperationalFlush:vlv */
6821
	/* WaDisable_RenderCache_OperationalFlush:vlv */
6865
	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6822
	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6866
 
6823
 
6867
	/* WaForceL3Serialization:vlv */
6824
	/* WaForceL3Serialization:vlv */
6868
	I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6825
	I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6869
		   ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6826
		   ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6870
 
6827
 
6871
	/* WaDisableDopClockGating:vlv */
6828
	/* WaDisableDopClockGating:vlv */
6872
	I915_WRITE(GEN7_ROW_CHICKEN2,
6829
	I915_WRITE(GEN7_ROW_CHICKEN2,
6873
		   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6830
		   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6874
 
6831
 
6875
	/* This is required by WaCatErrorRejectionIssue:vlv */
6832
	/* This is required by WaCatErrorRejectionIssue:vlv */
6876
	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6833
	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6877
		   I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6834
		   I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6878
		   GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6835
		   GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6879
 
6836
 
6880
	gen7_setup_fixed_func_scheduler(dev_priv);
6837
	gen7_setup_fixed_func_scheduler(dev_priv);
6881
 
6838
 
6882
	/*
6839
	/*
6883
	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6840
	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6884
	 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
6841
	 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
6885
	 */
6842
	 */
6886
	I915_WRITE(GEN6_UCGCTL2,
6843
	I915_WRITE(GEN6_UCGCTL2,
6887
		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6844
		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6888
 
6845
 
6889
	/* WaDisableL3Bank2xClockGate:vlv
6846
	/* WaDisableL3Bank2xClockGate:vlv
6890
	 * Disabling L3 clock gating- MMIO 940c[25] = 1
6847
	 * Disabling L3 clock gating- MMIO 940c[25] = 1
6891
	 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6848
	 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6892
	I915_WRITE(GEN7_UCGCTL4,
6849
	I915_WRITE(GEN7_UCGCTL4,
6893
		   I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
6850
		   I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
6894
 
-
 
6895
	I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
-
 
6896
 
6851
 
6897
	/*
6852
	/*
6898
	 * BSpec says this must be set, even though
6853
	 * BSpec says this must be set, even though
6899
	 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6854
	 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6900
	 */
6855
	 */
6901
	I915_WRITE(CACHE_MODE_1,
6856
	I915_WRITE(CACHE_MODE_1,
6902
		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6857
		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6903
 
6858
 
6904
	/*
6859
	/*
-
 
6860
	 * BSpec recommends 8x4 when MSAA is used,
-
 
6861
	 * however in practice 16x4 seems fastest.
-
 
6862
	 *
-
 
6863
	 * Note that PS/WM thread counts depend on the WIZ hashing
-
 
6864
	 * disable bit, which we don't touch here, but it's good
-
 
6865
	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
-
 
6866
	 */
-
 
6867
	I915_WRITE(GEN7_GT_MODE,
-
 
6868
		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
-
 
6869
 
-
 
6870
	/*
6905
	 * WaIncreaseL3CreditsForVLVB0:vlv
6871
	 * WaIncreaseL3CreditsForVLVB0:vlv
6906
	 * This is the hardware default actually.
6872
	 * This is the hardware default actually.
6907
	 */
6873
	 */
6908
	I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6874
	I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6909
 
6875
 
6910
	/*
6876
	/*
6911
	 * WaDisableVLVClockGating_VBIIssue:vlv
6877
	 * WaDisableVLVClockGating_VBIIssue:vlv
6912
	 * Disable clock gating on th GCFG unit to prevent a delay
6878
	 * Disable clock gating on th GCFG unit to prevent a delay
6913
	 * in the reporting of vblank events.
6879
	 * in the reporting of vblank events.
6914
	 */
6880
	 */
6915
	I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
6881
	I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
6916
}
6882
}
6917
 
6883
 
6918
static void cherryview_init_clock_gating(struct drm_device *dev)
6884
static void cherryview_init_clock_gating(struct drm_device *dev)
6919
{
6885
{
6920
	struct drm_i915_private *dev_priv = dev->dev_private;
6886
	struct drm_i915_private *dev_priv = dev->dev_private;
6921
 
6887
 
6922
	I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
-
 
6923
 
-
 
6924
	I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6888
	vlv_init_display_clock_gating(dev_priv);
6925
 
6889
 
6926
	/* WaVSRefCountFullforceMissDisable:chv */
6890
	/* WaVSRefCountFullforceMissDisable:chv */
6927
	/* WaDSRefCountFullforceMissDisable:chv */
6891
	/* WaDSRefCountFullforceMissDisable:chv */
6928
	I915_WRITE(GEN7_FF_THREAD_MODE,
6892
	I915_WRITE(GEN7_FF_THREAD_MODE,
6929
		   I915_READ(GEN7_FF_THREAD_MODE) &
6893
		   I915_READ(GEN7_FF_THREAD_MODE) &
6930
		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6894
		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6931
 
6895
 
6932
	/* WaDisableSemaphoreAndSyncFlipWait:chv */
6896
	/* WaDisableSemaphoreAndSyncFlipWait:chv */
6933
	I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6897
	I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6934
		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6898
		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6935
 
6899
 
6936
	/* WaDisableCSUnitClockGating:chv */
6900
	/* WaDisableCSUnitClockGating:chv */
6937
	I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6901
	I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6938
		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6902
		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6939
 
6903
 
6940
	/* WaDisableSDEUnitClockGating:chv */
6904
	/* WaDisableSDEUnitClockGating:chv */
6941
	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6905
	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6942
		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6906
		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
-
 
6907
 
-
 
6908
	/*
-
 
6909
	 * GTT cache may not work with big pages, so if those
-
 
6910
	 * are ever enabled GTT cache may need to be disabled.
-
 
6911
	 */
-
 
6912
	I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6943
}
6913
}
6944
 
6914
 
6945
static void g4x_init_clock_gating(struct drm_device *dev)
6915
static void g4x_init_clock_gating(struct drm_device *dev)
6946
{
6916
{
6947
	struct drm_i915_private *dev_priv = dev->dev_private;
6917
	struct drm_i915_private *dev_priv = dev->dev_private;
6948
	uint32_t dspclk_gate;
6918
	uint32_t dspclk_gate;
6949
 
6919
 
6950
	I915_WRITE(RENCLK_GATE_D1, 0);
6920
	I915_WRITE(RENCLK_GATE_D1, 0);
6951
	I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6921
	I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6952
		   GS_UNIT_CLOCK_GATE_DISABLE |
6922
		   GS_UNIT_CLOCK_GATE_DISABLE |
6953
		   CL_UNIT_CLOCK_GATE_DISABLE);
6923
		   CL_UNIT_CLOCK_GATE_DISABLE);
6954
	I915_WRITE(RAMCLK_GATE_D, 0);
6924
	I915_WRITE(RAMCLK_GATE_D, 0);
6955
	dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6925
	dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6956
		OVRUNIT_CLOCK_GATE_DISABLE |
6926
		OVRUNIT_CLOCK_GATE_DISABLE |
6957
		OVCUNIT_CLOCK_GATE_DISABLE;
6927
		OVCUNIT_CLOCK_GATE_DISABLE;
6958
	if (IS_GM45(dev))
6928
	if (IS_GM45(dev))
6959
		dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6929
		dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6960
	I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
6930
	I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
6961
 
6931
 
6962
	/* WaDisableRenderCachePipelinedFlush */
6932
	/* WaDisableRenderCachePipelinedFlush */
6963
	I915_WRITE(CACHE_MODE_0,
6933
	I915_WRITE(CACHE_MODE_0,
6964
		   _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6934
		   _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6965
 
6935
 
6966
	/* WaDisable_RenderCache_OperationalFlush:g4x */
6936
	/* WaDisable_RenderCache_OperationalFlush:g4x */
6967
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6937
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6968
 
6938
 
6969
	g4x_disable_trickle_feed(dev);
6939
	g4x_disable_trickle_feed(dev);
6970
}
6940
}
6971
 
6941
 
6972
static void crestline_init_clock_gating(struct drm_device *dev)
6942
static void crestline_init_clock_gating(struct drm_device *dev)
6973
{
6943
{
6974
	struct drm_i915_private *dev_priv = dev->dev_private;
6944
	struct drm_i915_private *dev_priv = dev->dev_private;
6975
 
6945
 
6976
	I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6946
	I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6977
	I915_WRITE(RENCLK_GATE_D2, 0);
6947
	I915_WRITE(RENCLK_GATE_D2, 0);
6978
	I915_WRITE(DSPCLK_GATE_D, 0);
6948
	I915_WRITE(DSPCLK_GATE_D, 0);
6979
	I915_WRITE(RAMCLK_GATE_D, 0);
6949
	I915_WRITE(RAMCLK_GATE_D, 0);
6980
	I915_WRITE16(DEUC, 0);
6950
	I915_WRITE16(DEUC, 0);
6981
	I915_WRITE(MI_ARB_STATE,
6951
	I915_WRITE(MI_ARB_STATE,
6982
		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6952
		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6983
 
6953
 
6984
	/* WaDisable_RenderCache_OperationalFlush:gen4 */
6954
	/* WaDisable_RenderCache_OperationalFlush:gen4 */
6985
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6955
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6986
}
6956
}
6987
 
6957
 
6988
static void broadwater_init_clock_gating(struct drm_device *dev)
6958
static void broadwater_init_clock_gating(struct drm_device *dev)
6989
{
6959
{
6990
	struct drm_i915_private *dev_priv = dev->dev_private;
6960
	struct drm_i915_private *dev_priv = dev->dev_private;
6991
 
6961
 
6992
	I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6962
	I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6993
		   I965_RCC_CLOCK_GATE_DISABLE |
6963
		   I965_RCC_CLOCK_GATE_DISABLE |
6994
		   I965_RCPB_CLOCK_GATE_DISABLE |
6964
		   I965_RCPB_CLOCK_GATE_DISABLE |
6995
		   I965_ISC_CLOCK_GATE_DISABLE |
6965
		   I965_ISC_CLOCK_GATE_DISABLE |
6996
		   I965_FBC_CLOCK_GATE_DISABLE);
6966
		   I965_FBC_CLOCK_GATE_DISABLE);
6997
	I915_WRITE(RENCLK_GATE_D2, 0);
6967
	I915_WRITE(RENCLK_GATE_D2, 0);
6998
	I915_WRITE(MI_ARB_STATE,
6968
	I915_WRITE(MI_ARB_STATE,
6999
		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6969
		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7000
 
6970
 
7001
	/* WaDisable_RenderCache_OperationalFlush:gen4 */
6971
	/* WaDisable_RenderCache_OperationalFlush:gen4 */
7002
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6972
	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7003
}
6973
}
7004
 
6974
 
7005
static void gen3_init_clock_gating(struct drm_device *dev)
6975
static void gen3_init_clock_gating(struct drm_device *dev)
7006
{
6976
{
7007
	struct drm_i915_private *dev_priv = dev->dev_private;
6977
	struct drm_i915_private *dev_priv = dev->dev_private;
7008
	u32 dstate = I915_READ(D_STATE);
6978
	u32 dstate = I915_READ(D_STATE);
7009
 
6979
 
7010
	dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6980
	dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7011
		DSTATE_DOT_CLOCK_GATING;
6981
		DSTATE_DOT_CLOCK_GATING;
7012
	I915_WRITE(D_STATE, dstate);
6982
	I915_WRITE(D_STATE, dstate);
7013
 
6983
 
7014
	if (IS_PINEVIEW(dev))
6984
	if (IS_PINEVIEW(dev))
7015
		I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
6985
		I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
7016
 
6986
 
7017
	/* IIR "flip pending" means done if this bit is set */
6987
	/* IIR "flip pending" means done if this bit is set */
7018
	I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
6988
	I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
7019
 
6989
 
7020
	/* interrupts should cause a wake up from C3 */
6990
	/* interrupts should cause a wake up from C3 */
7021
	I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
6991
	I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
7022
 
6992
 
7023
	/* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6993
	/* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7024
	I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
6994
	I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
7025
 
6995
 
7026
	I915_WRITE(MI_ARB_STATE,
6996
	I915_WRITE(MI_ARB_STATE,
7027
		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6997
		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7028
}
6998
}
7029
 
6999
 
7030
static void i85x_init_clock_gating(struct drm_device *dev)
7000
static void i85x_init_clock_gating(struct drm_device *dev)
7031
{
7001
{
7032
	struct drm_i915_private *dev_priv = dev->dev_private;
7002
	struct drm_i915_private *dev_priv = dev->dev_private;
7033
 
7003
 
7034
	I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
7004
	I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
7035
 
7005
 
7036
	/* interrupts should cause a wake up from C3 */
7006
	/* interrupts should cause a wake up from C3 */
7037
	I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7007
	I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7038
		   _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
7008
		   _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
7039
 
7009
 
7040
	I915_WRITE(MEM_MODE,
7010
	I915_WRITE(MEM_MODE,
7041
		   _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
7011
		   _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
7042
}
7012
}
7043
 
7013
 
7044
static void i830_init_clock_gating(struct drm_device *dev)
7014
static void i830_init_clock_gating(struct drm_device *dev)
7045
{
7015
{
7046
	struct drm_i915_private *dev_priv = dev->dev_private;
7016
	struct drm_i915_private *dev_priv = dev->dev_private;
7047
 
7017
 
7048
	I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
7018
	I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
7049
 
7019
 
7050
	I915_WRITE(MEM_MODE,
7020
	I915_WRITE(MEM_MODE,
7051
		   _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7021
		   _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7052
		   _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
7022
		   _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
7053
}
7023
}
7054
 
7024
 
7055
void intel_init_clock_gating(struct drm_device *dev)
7025
void intel_init_clock_gating(struct drm_device *dev)
7056
{
7026
{
7057
	struct drm_i915_private *dev_priv = dev->dev_private;
7027
	struct drm_i915_private *dev_priv = dev->dev_private;
-
 
7028
 
7058
 
7029
	if (dev_priv->display.init_clock_gating)
7059
	dev_priv->display.init_clock_gating(dev);
7030
		dev_priv->display.init_clock_gating(dev);
7060
}
7031
}
7061
 
7032
 
7062
void intel_suspend_hw(struct drm_device *dev)
7033
void intel_suspend_hw(struct drm_device *dev)
7063
{
7034
{
7064
	if (HAS_PCH_LPT(dev))
7035
	if (HAS_PCH_LPT(dev))
7065
		lpt_suspend_hw(dev);
7036
		lpt_suspend_hw(dev);
7066
}
7037
}
7067
 
-
 
7068
static void intel_init_fbc(struct drm_i915_private *dev_priv)
-
 
7069
{
-
 
7070
	if (!HAS_FBC(dev_priv)) {
-
 
7071
		dev_priv->fbc.enabled = false;
-
 
7072
		return;
-
 
7073
	}
-
 
7074
 
-
 
7075
	if (INTEL_INFO(dev_priv)->gen >= 7) {
-
 
7076
			dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
-
 
7077
			dev_priv->display.enable_fbc = gen7_enable_fbc;
-
 
7078
			dev_priv->display.disable_fbc = ironlake_disable_fbc;
-
 
7079
	} else if (INTEL_INFO(dev_priv)->gen >= 5) {
-
 
7080
			dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
-
 
7081
			dev_priv->display.enable_fbc = ironlake_enable_fbc;
-
 
7082
			dev_priv->display.disable_fbc = ironlake_disable_fbc;
-
 
7083
	} else if (IS_GM45(dev_priv)) {
-
 
7084
			dev_priv->display.fbc_enabled = g4x_fbc_enabled;
-
 
7085
			dev_priv->display.enable_fbc = g4x_enable_fbc;
-
 
7086
			dev_priv->display.disable_fbc = g4x_disable_fbc;
-
 
7087
		} else {
-
 
7088
			dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
-
 
7089
			dev_priv->display.enable_fbc = i8xx_enable_fbc;
-
 
7090
			dev_priv->display.disable_fbc = i8xx_disable_fbc;
-
 
7091
 
-
 
7092
			/* This value was pulled out of someone's hat */
-
 
7093
			I915_WRITE(FBC_CONTROL, 500 << FBC_CTL_INTERVAL_SHIFT);
-
 
7094
		}
-
 
7095
 
-
 
7096
	dev_priv->fbc.enabled = dev_priv->display.fbc_enabled(dev_priv->dev);
-
 
7097
}
-
 
7098
 
7038
 
7099
/* Set up chip specific power management-related functions */
7039
/* Set up chip specific power management-related functions */
7100
void intel_init_pm(struct drm_device *dev)
7040
void intel_init_pm(struct drm_device *dev)
7101
{
7041
{
7102
	struct drm_i915_private *dev_priv = dev->dev_private;
7042
	struct drm_i915_private *dev_priv = dev->dev_private;
7103
 
7043
 
7104
	intel_init_fbc(dev_priv);
7044
	intel_fbc_init(dev_priv);
7105
 
7045
 
7106
	/* For cxsr */
7046
	/* For cxsr */
7107
	if (IS_PINEVIEW(dev))
7047
	if (IS_PINEVIEW(dev))
7108
		i915_pineview_get_mem_freq(dev);
7048
		i915_pineview_get_mem_freq(dev);
7109
	else if (IS_GEN5(dev))
7049
	else if (IS_GEN5(dev))
7110
		i915_ironlake_get_mem_freq(dev);
7050
		i915_ironlake_get_mem_freq(dev);
7111
 
7051
 
7112
	/* For FIFO watermark updates */
7052
	/* For FIFO watermark updates */
7113
	if (INTEL_INFO(dev)->gen >= 9) {
7053
	if (INTEL_INFO(dev)->gen >= 9) {
7114
		skl_setup_wm_latency(dev);
7054
		skl_setup_wm_latency(dev);
-
 
7055
 
7115
 
7056
		if (IS_BROXTON(dev))
-
 
7057
			dev_priv->display.init_clock_gating =
7116
		dev_priv->display.init_clock_gating = gen9_init_clock_gating;
7058
				bxt_init_clock_gating;
7117
		dev_priv->display.update_wm = skl_update_wm;
7059
		dev_priv->display.update_wm = skl_update_wm;
7118
		dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
7060
		dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
7119
	} else if (HAS_PCH_SPLIT(dev)) {
7061
	} else if (HAS_PCH_SPLIT(dev)) {
7120
		ilk_setup_wm_latency(dev);
7062
		ilk_setup_wm_latency(dev);
7121
 
7063
 
7122
		if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7064
		if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7123
		     dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7065
		     dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7124
		    (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7066
		    (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7125
		     dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7067
		     dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7126
			dev_priv->display.update_wm = ilk_update_wm;
7068
			dev_priv->display.update_wm = ilk_update_wm;
7127
			dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
7069
			dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
7128
			} else {
7070
		} else {
7129
				DRM_DEBUG_KMS("Failed to read display plane latency. "
7071
			DRM_DEBUG_KMS("Failed to read display plane latency. "
7130
					      "Disable CxSR\n");
7072
				      "Disable CxSR\n");
7131
			}
7073
		}
7132
 
7074
 
7133
		if (IS_GEN5(dev))
7075
		if (IS_GEN5(dev))
7134
			dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7076
			dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7135
		else if (IS_GEN6(dev))
7077
		else if (IS_GEN6(dev))
7136
			dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7078
			dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7137
		else if (IS_IVYBRIDGE(dev))
7079
		else if (IS_IVYBRIDGE(dev))
7138
			dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7080
			dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7139
		else if (IS_HASWELL(dev))
7081
		else if (IS_HASWELL(dev))
7140
			dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7082
			dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7141
		else if (INTEL_INFO(dev)->gen == 8)
7083
		else if (INTEL_INFO(dev)->gen == 8)
7142
			dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7084
			dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7143
	} else if (IS_CHERRYVIEW(dev)) {
7085
	} else if (IS_CHERRYVIEW(dev)) {
7144
		dev_priv->display.update_wm = cherryview_update_wm;
7086
		vlv_setup_wm_latency(dev);
-
 
7087
 
7145
		dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
7088
		dev_priv->display.update_wm = vlv_update_wm;
7146
		dev_priv->display.init_clock_gating =
7089
		dev_priv->display.init_clock_gating =
7147
			cherryview_init_clock_gating;
7090
			cherryview_init_clock_gating;
7148
	} else if (IS_VALLEYVIEW(dev)) {
7091
	} else if (IS_VALLEYVIEW(dev)) {
7149
		dev_priv->display.update_wm = valleyview_update_wm;
7092
		vlv_setup_wm_latency(dev);
-
 
7093
 
7150
		dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
7094
		dev_priv->display.update_wm = vlv_update_wm;
7151
		dev_priv->display.init_clock_gating =
7095
		dev_priv->display.init_clock_gating =
7152
			valleyview_init_clock_gating;
7096
			valleyview_init_clock_gating;
7153
	} else if (IS_PINEVIEW(dev)) {
7097
	} else if (IS_PINEVIEW(dev)) {
7154
		if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7098
		if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7155
					    dev_priv->is_ddr3,
7099
					    dev_priv->is_ddr3,
7156
					    dev_priv->fsb_freq,
7100
					    dev_priv->fsb_freq,
7157
					    dev_priv->mem_freq)) {
7101
					    dev_priv->mem_freq)) {
7158
			DRM_INFO("failed to find known CxSR latency "
7102
			DRM_INFO("failed to find known CxSR latency "
7159
				 "(found ddr%s fsb freq %d, mem freq %d), "
7103
				 "(found ddr%s fsb freq %d, mem freq %d), "
7160
				 "disabling CxSR\n",
7104
				 "disabling CxSR\n",
7161
				 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7105
				 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7162
				 dev_priv->fsb_freq, dev_priv->mem_freq);
7106
				 dev_priv->fsb_freq, dev_priv->mem_freq);
7163
			/* Disable CxSR and never update its watermark again */
7107
			/* Disable CxSR and never update its watermark again */
7164
			intel_set_memory_cxsr(dev_priv, false);
7108
			intel_set_memory_cxsr(dev_priv, false);
7165
			dev_priv->display.update_wm = NULL;
7109
			dev_priv->display.update_wm = NULL;
7166
		} else
7110
		} else
7167
			dev_priv->display.update_wm = pineview_update_wm;
7111
			dev_priv->display.update_wm = pineview_update_wm;
7168
		dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7112
		dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7169
	} else if (IS_G4X(dev)) {
7113
	} else if (IS_G4X(dev)) {
7170
		dev_priv->display.update_wm = g4x_update_wm;
7114
		dev_priv->display.update_wm = g4x_update_wm;
7171
		dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7115
		dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7172
	} else if (IS_GEN4(dev)) {
7116
	} else if (IS_GEN4(dev)) {
7173
		dev_priv->display.update_wm = i965_update_wm;
7117
		dev_priv->display.update_wm = i965_update_wm;
7174
		if (IS_CRESTLINE(dev))
7118
		if (IS_CRESTLINE(dev))
7175
			dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7119
			dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7176
		else if (IS_BROADWATER(dev))
7120
		else if (IS_BROADWATER(dev))
7177
			dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7121
			dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7178
	} else if (IS_GEN3(dev)) {
7122
	} else if (IS_GEN3(dev)) {
7179
		dev_priv->display.update_wm = i9xx_update_wm;
7123
		dev_priv->display.update_wm = i9xx_update_wm;
7180
		dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7124
		dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7181
		dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7125
		dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7182
	} else if (IS_GEN2(dev)) {
7126
	} else if (IS_GEN2(dev)) {
7183
		if (INTEL_INFO(dev)->num_pipes == 1) {
7127
		if (INTEL_INFO(dev)->num_pipes == 1) {
7184
			dev_priv->display.update_wm = i845_update_wm;
7128
			dev_priv->display.update_wm = i845_update_wm;
7185
			dev_priv->display.get_fifo_size = i845_get_fifo_size;
7129
			dev_priv->display.get_fifo_size = i845_get_fifo_size;
7186
		} else {
7130
		} else {
7187
			dev_priv->display.update_wm = i9xx_update_wm;
7131
			dev_priv->display.update_wm = i9xx_update_wm;
7188
		dev_priv->display.get_fifo_size = i830_get_fifo_size;
7132
			dev_priv->display.get_fifo_size = i830_get_fifo_size;
7189
		}
7133
		}
7190
 
7134
 
7191
		if (IS_I85X(dev) || IS_I865G(dev))
7135
		if (IS_I85X(dev) || IS_I865G(dev))
7192
		dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7136
			dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7193
		else
7137
		else
7194
			dev_priv->display.init_clock_gating = i830_init_clock_gating;
7138
			dev_priv->display.init_clock_gating = i830_init_clock_gating;
7195
	} else {
7139
	} else {
7196
		DRM_ERROR("unexpected fall-through in intel_init_pm\n");
7140
		DRM_ERROR("unexpected fall-through in intel_init_pm\n");
7197
	}
7141
	}
7198
}
7142
}
7199
 
7143
 
7200
int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
7144
int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
7201
{
7145
{
7202
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7146
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7203
 
7147
 
7204
	if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7148
	if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7205
		DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7149
		DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7206
		return -EAGAIN;
7150
		return -EAGAIN;
7207
	}
7151
	}
7208
 
7152
 
7209
	I915_WRITE(GEN6_PCODE_DATA, *val);
7153
	I915_WRITE(GEN6_PCODE_DATA, *val);
7210
	I915_WRITE(GEN6_PCODE_DATA1, 0);
7154
	I915_WRITE(GEN6_PCODE_DATA1, 0);
7211
	I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7155
	I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7212
 
7156
 
7213
	if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7157
	if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7214
		     500)) {
7158
		     500)) {
7215
		DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7159
		DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7216
		return -ETIMEDOUT;
7160
		return -ETIMEDOUT;
7217
			}
7161
	}
7218
 
7162
 
7219
	*val = I915_READ(GEN6_PCODE_DATA);
7163
	*val = I915_READ(GEN6_PCODE_DATA);
7220
	I915_WRITE(GEN6_PCODE_DATA, 0);
7164
	I915_WRITE(GEN6_PCODE_DATA, 0);
7221
 
7165
 
7222
	return 0;
7166
	return 0;
7223
}
7167
}
7224
 
7168
 
7225
int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
7169
int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
7226
{
7170
{
7227
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7171
	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7228
 
7172
 
7229
	if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7173
	if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7230
		DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7174
		DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7231
		return -EAGAIN;
7175
		return -EAGAIN;
7232
		}
7176
	}
7233
 
7177
 
7234
	I915_WRITE(GEN6_PCODE_DATA, val);
7178
	I915_WRITE(GEN6_PCODE_DATA, val);
7235
	I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7179
	I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7236
 
7180
 
7237
	if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7181
	if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7238
		     500)) {
7182
		     500)) {
7239
		DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7183
		DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7240
		return -ETIMEDOUT;
7184
		return -ETIMEDOUT;
7241
	}
7185
	}
7242
 
7186
 
7243
	I915_WRITE(GEN6_PCODE_DATA, 0);
7187
	I915_WRITE(GEN6_PCODE_DATA, 0);
7244
 
7188
 
7245
	return 0;
7189
	return 0;
7246
}
7190
}
7247
 
7191
 
7248
static int vlv_gpu_freq_div(unsigned int czclk_freq)
7192
static int vlv_gpu_freq_div(unsigned int czclk_freq)
7249
{
7193
{
7250
	switch (czclk_freq) {
7194
	switch (czclk_freq) {
7251
	case 200:
7195
	case 200:
7252
		return 10;
7196
		return 10;
7253
	case 267:
7197
	case 267:
7254
		return 12;
7198
		return 12;
7255
	case 320:
7199
	case 320:
7256
	case 333:
7200
	case 333:
7257
		return 16;
7201
		return 16;
7258
	case 400:
7202
	case 400:
7259
		return 20;
7203
		return 20;
7260
	default:
7204
	default:
7261
		return -1;
7205
		return -1;
7262
	}
7206
	}
7263
}
7207
}
7264
 
7208
 
7265
static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7209
static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7266
{
7210
{
7267
	int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
7211
	int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7268
 
7212
 
7269
	div = vlv_gpu_freq_div(czclk_freq);
7213
	div = vlv_gpu_freq_div(czclk_freq);
7270
	if (div < 0)
7214
	if (div < 0)
7271
		return div;
7215
		return div;
7272
 
7216
 
7273
	return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
7217
	return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
7274
}
7218
}
7275
 
7219
 
7276
static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
7220
static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
7277
{
7221
{
7278
	int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
7222
	int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7279
 
7223
 
7280
	mul = vlv_gpu_freq_div(czclk_freq);
7224
	mul = vlv_gpu_freq_div(czclk_freq);
7281
	if (mul < 0)
7225
	if (mul < 0)
7282
		return mul;
7226
		return mul;
7283
 
7227
 
7284
	return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
7228
	return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
7285
}
7229
}
7286
 
7230
 
7287
static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7231
static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7288
{
7232
{
7289
	int div, czclk_freq = dev_priv->rps.cz_freq;
7233
	int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7290
 
7234
 
7291
	div = vlv_gpu_freq_div(czclk_freq) / 2;
7235
	div = vlv_gpu_freq_div(czclk_freq) / 2;
7292
	if (div < 0)
7236
	if (div < 0)
7293
		return div;
7237
		return div;
7294
 
7238
 
7295
	return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
7239
	return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
7296
}
7240
}
7297
 
7241
 
7298
static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7242
static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7299
{
7243
{
7300
	int mul, czclk_freq = dev_priv->rps.cz_freq;
7244
	int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7301
 
7245
 
7302
	mul = vlv_gpu_freq_div(czclk_freq) / 2;
7246
	mul = vlv_gpu_freq_div(czclk_freq) / 2;
7303
	if (mul < 0)
7247
	if (mul < 0)
7304
		return mul;
7248
		return mul;
7305
 
7249
 
7306
	/* CHV needs even values */
7250
	/* CHV needs even values */
7307
	return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
7251
	return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
7308
}
7252
}
7309
 
7253
 
7310
int vlv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7254
int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
-
 
7255
{
-
 
7256
	if (IS_GEN9(dev_priv->dev))
7311
{
7257
		return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
-
 
7258
					 GEN9_FREQ_SCALER);
-
 
7259
	else if (IS_CHERRYVIEW(dev_priv->dev))
-
 
7260
		return chv_gpu_freq(dev_priv, val);
-
 
7261
	else if (IS_VALLEYVIEW(dev_priv->dev))
-
 
7262
		return byt_gpu_freq(dev_priv, val);
-
 
7263
	else
-
 
7264
		return val * GT_FREQUENCY_MULTIPLIER;
-
 
7265
}
-
 
7266
 
-
 
7267
int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
-
 
7268
{
-
 
7269
	if (IS_GEN9(dev_priv->dev))
7312
	int ret = -1;
7270
		return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7313
 
7271
					 GT_FREQUENCY_MULTIPLIER);
7314
	if (IS_CHERRYVIEW(dev_priv->dev))
7272
	else if (IS_CHERRYVIEW(dev_priv->dev))
7315
		ret = chv_gpu_freq(dev_priv, val);
7273
		return chv_freq_opcode(dev_priv, val);
-
 
7274
	else if (IS_VALLEYVIEW(dev_priv->dev))
-
 
7275
		return byt_freq_opcode(dev_priv, val);
-
 
7276
	else
-
 
7277
		return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
-
 
7278
}
-
 
7279
 
-
 
7280
struct request_boost {
-
 
7281
	struct work_struct work;
-
 
7282
	struct drm_i915_gem_request *req;
-
 
7283
};
-
 
7284
 
-
 
7285
static void __intel_rps_boost_work(struct work_struct *work)
-
 
7286
{
-
 
7287
	struct request_boost *boost = container_of(work, struct request_boost, work);
-
 
7288
	struct drm_i915_gem_request *req = boost->req;
-
 
7289
 
-
 
7290
	if (!i915_gem_request_completed(req, true))
-
 
7291
		gen6_rps_boost(to_i915(req->ring->dev), NULL,
7316
	else if (IS_VALLEYVIEW(dev_priv->dev))
7292
			       req->emitted_jiffies);
7317
		ret = byt_gpu_freq(dev_priv, val);
7293
 
-
 
7294
	i915_gem_request_unreference__unlocked(req);
7318
 
7295
	kfree(boost);
7319
	return ret;
7296
}
7320
}
7297
 
7321
 
-
 
7322
int vlv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7298
void intel_queue_rps_boost_for_request(struct drm_device *dev,
7323
{
-
 
7324
	int ret = -1;
7299
				       struct drm_i915_gem_request *req)
-
 
7300
{
7325
 
7301
	struct request_boost *boost;
-
 
7302
 
-
 
7303
	if (req == NULL || INTEL_INFO(dev)->gen < 6)
-
 
7304
		return;
-
 
7305
 
-
 
7306
	if (i915_gem_request_completed(req, true))
-
 
7307
		return;
-
 
7308
 
-
 
7309
	boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
-
 
7310
	if (boost == NULL)
-
 
7311
		return;
7326
	if (IS_CHERRYVIEW(dev_priv->dev))
7312
 
7327
		ret = chv_freq_opcode(dev_priv, val);
7313
	i915_gem_request_reference(req);
7328
	else if (IS_VALLEYVIEW(dev_priv->dev))
7314
	boost->req = req;
7329
		ret = byt_freq_opcode(dev_priv, val);
7315
 
7330
 
7316
	INIT_WORK(&boost->work, __intel_rps_boost_work);
7331
	return ret;
7317
	queue_work(to_i915(dev)->wq, &boost->work);
7332
}
7318
}
7333
 
7319
 
7334
void intel_pm_setup(struct drm_device *dev)
7320
void intel_pm_setup(struct drm_device *dev)
7335
{
7321
{
7336
	struct drm_i915_private *dev_priv = dev->dev_private;
7322
	struct drm_i915_private *dev_priv = dev->dev_private;
7337
 
7323
 
7338
	mutex_init(&dev_priv->rps.hw_lock);
7324
	mutex_init(&dev_priv->rps.hw_lock);
-
 
7325
	spin_lock_init(&dev_priv->rps.client_lock);
7339
 
7326
 
7340
	INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7327
	INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7341
			  intel_gen6_powersave_work);
7328
			  intel_gen6_powersave_work);
-
 
7329
	INIT_LIST_HEAD(&dev_priv->rps.clients);
-
 
7330
	INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
-
 
7331
	INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
7342
 
7332
 
7343
	dev_priv->pm.suspended = false;
7333
	dev_priv->pm.suspended = false;
7344
}
7334
}
7345
>
7335
>
7346
>
7336
>
7347
>
7337
>
7348
>
7338
 
7349
>
7339
static>
7350
>
7340
 
7351
>
7341
static>
7352
 
7342
#define>
7353
/*>
7343
#define>
7354
 
7344
#define>
7355
/*>
7345
#define>
7356
#define>
-
 
7357
#define>
-
 
7358
#define>
-
 
7359
#define>
-