blob: 491688676df4799ba80da619a46fd81ec91970dd [file] [log] [blame]
Ben Skeggs1262a202011-07-18 15:15:34 +10001/*
2 * Copyright 2011 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24
25#include "drmP.h"
26#include "nouveau_drv.h"
27#include "nouveau_bios.h"
28#include "nouveau_pm.h"
29#include "nouveau_hw.h"
30
31#define min2(a,b) ((a) < (b) ? (a) : (b))
32
33static u32
34read_pll_1(struct drm_device *dev, u32 reg)
35{
36 u32 ctrl = nv_rd32(dev, reg + 0x00);
37 int P = (ctrl & 0x00070000) >> 16;
38 int N = (ctrl & 0x0000ff00) >> 8;
39 int M = (ctrl & 0x000000ff) >> 0;
40 u32 ref = 27000, clk = 0;
41
42 if (ctrl & 0x80000000)
43 clk = ref * N / M;
44
45 return clk >> P;
46}
47
48static u32
49read_pll_2(struct drm_device *dev, u32 reg)
50{
51 u32 ctrl = nv_rd32(dev, reg + 0x00);
52 u32 coef = nv_rd32(dev, reg + 0x04);
53 int N2 = (coef & 0xff000000) >> 24;
54 int M2 = (coef & 0x00ff0000) >> 16;
55 int N1 = (coef & 0x0000ff00) >> 8;
56 int M1 = (coef & 0x000000ff) >> 0;
57 int P = (ctrl & 0x00070000) >> 16;
58 u32 ref = 27000, clk = 0;
59
60 if (ctrl & 0x80000000)
61 clk = ref * N1 / M1;
62
63 if (!(ctrl & 0x00000100)) {
64 if (ctrl & 0x40000000)
65 clk = clk * N2 / M2;
66 }
67
68 return clk >> P;
69}
70
71static u32
72read_clk(struct drm_device *dev, u32 src)
73{
74 switch (src) {
75 case 3:
76 return read_pll_2(dev, 0x004000);
77 case 2:
78 return read_pll_1(dev, 0x004008);
79 default:
80 break;
81 }
82
83 return 0;
84}
85
86int
87nv40_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
88{
89 u32 ctrl = nv_rd32(dev, 0x00c040);
90
91 perflvl->core = read_clk(dev, (ctrl & 0x00000003) >> 0);
92 perflvl->shader = read_clk(dev, (ctrl & 0x00000030) >> 4);
93 perflvl->memory = read_pll_2(dev, 0x4020);
94 return 0;
95}
96
97struct nv40_pm_state {
98 u32 ctrl;
99 u32 npll_ctrl;
100 u32 npll_coef;
101 u32 spll;
102 u32 mpll_ctrl;
103 u32 mpll_coef;
104};
105
106static int
107nv40_calc_pll(struct drm_device *dev, u32 reg, struct pll_lims *pll,
108 u32 clk, int *N1, int *M1, int *N2, int *M2, int *log2P)
109{
110 struct nouveau_pll_vals coef;
111 int ret;
112
113 ret = get_pll_limits(dev, reg, pll);
114 if (ret)
115 return ret;
116
117 if (clk < pll->vco1.maxfreq)
118 pll->vco2.maxfreq = 0;
119
120 ret = nouveau_calc_pll_mnp(dev, pll, clk, &coef);
121 if (ret == 0)
122 return -ERANGE;
123
124 *N1 = coef.N1;
125 *M1 = coef.M1;
126 if (N2 && M2) {
127 if (pll->vco2.maxfreq) {
128 *N2 = coef.N2;
129 *M2 = coef.M2;
130 } else {
131 *N2 = 1;
132 *M2 = 1;
133 }
134 }
135 *log2P = coef.log2P;
136 return 0;
137}
138
139void *
140nv40_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
141{
142 struct nv40_pm_state *info;
143 struct pll_lims pll;
144 int N1, N2, M1, M2, log2P;
145 int ret;
146
147 info = kmalloc(sizeof(*info), GFP_KERNEL);
148 if (!info)
149 return ERR_PTR(-ENOMEM);
150
151 /* core/geometric clock */
152 ret = nv40_calc_pll(dev, 0x004000, &pll, perflvl->core,
153 &N1, &M1, &N2, &M2, &log2P);
154 if (ret < 0)
155 goto out;
156
157 if (N2 == M2) {
158 info->npll_ctrl = 0x80000100 | (log2P << 16);
159 info->npll_coef = (N1 << 8) | M1;
160 } else {
161 info->npll_ctrl = 0xc0000000 | (log2P << 16);
162 info->npll_coef = (N2 << 24) | (M2 << 16) | (N1 << 8) | M1;
163 }
164
165 /* use the second PLL for shader/rop clock, if it differs from core */
166 if (perflvl->shader && perflvl->shader != perflvl->core) {
167 ret = nv40_calc_pll(dev, 0x004008, &pll, perflvl->shader,
168 &N1, &M1, NULL, NULL, &log2P);
169 if (ret < 0)
170 goto out;
171
172 info->spll = 0xc0000000 | (log2P << 16) | (N1 << 8) | M1;
173 info->ctrl = 0x00000223;
174 } else {
175 info->spll = 0x00000000;
176 info->ctrl = 0x00000333;
177 }
178
179 /* memory clock */
180 ret = nv40_calc_pll(dev, 0x004020, &pll, perflvl->memory,
181 &N1, &M1, &N2, &M2, &log2P);
182 if (ret < 0)
183 goto out;
184
185 info->mpll_ctrl = 0x80000000 | (log2P << 16);
186 info->mpll_ctrl |= min2(pll.log2p_bias + log2P, pll.max_log2p) << 20;
187 if (N2 == M2) {
188 info->mpll_ctrl |= 0x00000100;
189 info->mpll_coef = (N1 << 8) | M1;
190 } else {
191 info->mpll_ctrl |= 0x40000000;
192 info->mpll_coef = (N2 << 24) | (M2 << 16) | (N1 << 8) | M1;
193 }
194
195out:
196 if (ret < 0) {
197 kfree(info);
198 info = ERR_PTR(ret);
199 }
200 return info;
201}
202
203static bool
204nv40_pm_gr_idle(void *data)
205{
206 struct drm_device *dev = data;
207
208 if ((nv_rd32(dev, 0x400760) & 0x000000f0) >> 4 !=
209 (nv_rd32(dev, 0x400760) & 0x0000000f))
210 return false;
211
212 if (nv_rd32(dev, 0x400700))
213 return false;
214
215 return true;
216}
217
218void
219nv40_pm_clocks_set(struct drm_device *dev, void *pre_state)
220{
221 struct drm_nouveau_private *dev_priv = dev->dev_private;
222 struct nv40_pm_state *info = pre_state;
223 unsigned long flags;
224 u32 crtc_mask = 0;
225 u8 sr1[2];
226 int i;
227
228 /* determine which CRTCs are active, fetch VGA_SR1 for each */
229 for (i = 0; i < 2; i++) {
230 u32 vbl = nv_rd32(dev, 0x600808 + (i * 0x2000));
231 u32 cnt = 0;
232 do {
233 if (vbl != nv_rd32(dev, 0x600808 + (i * 0x2000))) {
234 nv_wr08(dev, 0x0c03c4 + (i * 0x2000), 0x01);
235 sr1[i] = nv_rd08(dev, 0x0c03c5 + (i * 0x2000));
236 if (!(sr1[i] & 0x20))
237 crtc_mask |= (1 << i);
238 break;
239 }
240 udelay(1);
241 } while (cnt++ < 32);
242 }
243
244 /* halt and idle engines */
245 spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
246 nv_mask(dev, 0x002500, 0x00000001, 0x00000000);
247 if (!nv_wait(dev, 0x002500, 0x00000010, 0x00000000))
248 goto resume;
249 nv_mask(dev, 0x003220, 0x00000001, 0x00000000);
250 if (!nv_wait(dev, 0x003220, 0x00000010, 0x00000000))
251 goto resume;
252 nv_mask(dev, 0x003200, 0x00000001, 0x00000000);
253 nv04_fifo_cache_pull(dev, false);
254
255 if (!nv_wait_cb(dev, nv40_pm_gr_idle, dev))
256 goto resume;
257
258 /* set engine clocks */
259 nv_mask(dev, 0x00c040, 0x00000333, 0x00000000);
260 nv_wr32(dev, 0x004004, info->npll_coef);
261 nv_mask(dev, 0x004000, 0xc0070100, info->npll_ctrl);
262 nv_mask(dev, 0x004008, 0xc007ffff, info->spll);
263 mdelay(5);
264 nv_mask(dev, 0x00c040, 0x00000333, info->ctrl);
265
266 /* wait for vblank start on active crtcs, disable memory access */
267 for (i = 0; i < 2; i++) {
268 if (!(crtc_mask & (1 << i)))
269 continue;
270 nv_wait(dev, 0x600808 + (i * 0x2000), 0x00010000, 0x00000000);
271 nv_wait(dev, 0x600808 + (i * 0x2000), 0x00010000, 0x00010000);
272 nv_wr08(dev, 0x0c03c4 + (i * 0x2000), 0x01);
273 nv_wr08(dev, 0x0c03c5 + (i * 0x2000), sr1[i] | 0x20);
274 }
275
276 /* prepare ram for reclocking */
277 nv_wr32(dev, 0x1002d4, 0x00000001); /* precharge */
278 nv_wr32(dev, 0x1002d0, 0x00000001); /* refresh */
279 nv_wr32(dev, 0x1002d0, 0x00000001); /* refresh */
280 nv_mask(dev, 0x100210, 0x80000000, 0x00000000); /* no auto refresh */
281 nv_wr32(dev, 0x1002dc, 0x00000001); /* enable self-refresh */
282
283 /* change the PLL of each memory partition */
284 nv_mask(dev, 0x00c040, 0x0000c000, 0x00000000);
285 switch (dev_priv->chipset) {
286 case 0x40:
287 case 0x45:
288 case 0x41:
289 case 0x42:
290 case 0x47:
291 nv_mask(dev, 0x004044, 0xc0771100, info->mpll_ctrl);
292 nv_mask(dev, 0x00402c, 0xc0771100, info->mpll_ctrl);
293 nv_wr32(dev, 0x004048, info->mpll_coef);
294 nv_wr32(dev, 0x004030, info->mpll_coef);
295 case 0x43:
296 case 0x49:
297 case 0x4b:
298 nv_mask(dev, 0x004038, 0xc0771100, info->mpll_ctrl);
299 nv_wr32(dev, 0x00403c, info->mpll_coef);
300 default:
301 nv_mask(dev, 0x004020, 0xc0771100, info->mpll_ctrl);
302 nv_wr32(dev, 0x004024, info->mpll_coef);
303 break;
304 }
305 udelay(100);
306 nv_mask(dev, 0x00c040, 0x0000c000, 0x0000c000);
307
308 /* re-enable normal operation of memory controller */
309 nv_wr32(dev, 0x1002dc, 0x00000000);
310 nv_mask(dev, 0x100210, 0x80000000, 0x80000000);
311 udelay(100);
312
313 /* make sure we're in vblank (hopefully the same one as before), and
314 * then re-enable crtc memory access
315 */
316 for (i = 0; i < 2; i++) {
317 if (!(crtc_mask & (1 << i)))
318 continue;
319 nv_wait(dev, 0x600808 + (i * 0x2000), 0x00010000, 0x00010000);
320 nv_wr08(dev, 0x0c03c4 + (i * 0x2000), 0x01);
321 nv_wr08(dev, 0x0c03c5 + (i * 0x2000), sr1[i]);
322 }
323
324 /* resume engines */
325resume:
326 nv_wr32(dev, 0x003250, 0x00000001);
327 nv_mask(dev, 0x003220, 0x00000001, 0x00000001);
328 nv_wr32(dev, 0x003200, 0x00000001);
329 nv_wr32(dev, 0x002500, 0x00000001);
330 spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
331
332 kfree(info);
333}