drm/nvc0/pm: more complete parsing of clock domains
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / nouveau / nva3_pm.c
1 /*
2  * Copyright 2010 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
30 static u32 read_clk(struct drm_device *, int, bool);
31 static u32 read_pll(struct drm_device *, int, u32);
32
33 static u32
34 read_vco(struct drm_device *dev, int clk)
35 {
36         u32 sctl = nv_rd32(dev, 0x4120 + (clk * 4));
37         if ((sctl & 0x00000030) != 0x00000030)
38                 return read_pll(dev, 0x41, 0x00e820);
39         return read_pll(dev, 0x42, 0x00e8a0);
40 }
41
42 static u32
43 read_clk(struct drm_device *dev, int clk, bool ignore_en)
44 {
45         u32 sctl, sdiv, sclk;
46
47         /* refclk for the 0xe8xx plls always 27KHz */
48         if (clk >= 0x40)
49                 return 27000;
50
51         sctl = nv_rd32(dev, 0x4120 + (clk * 4));
52         if (!ignore_en && !(sctl & 0x00000100))
53                 return 0;
54
55         switch (sctl & 0x00003000) {
56         case 0x00000000:
57                 return 27000;
58         case 0x00002000:
59                 if (sctl & 0x00000040)
60                         return 108000;
61                 return 100000;
62         case 0x00003000:
63                 sclk = read_vco(dev, clk);
64                 sdiv = ((sctl & 0x003f0000) >> 16) + 2;
65                 return (sclk * 2) / sdiv;
66         default:
67                 return 0;
68         }
69 }
70
71 static u32
72 read_pll(struct drm_device *dev, int clk, u32 pll)
73 {
74         u32 ctrl = nv_rd32(dev, pll + 0);
75         u32 sclk, P = 1, N = 1, M = 1;
76
77         if (!(ctrl & 0x00000008)) {
78                 u32 coef = nv_rd32(dev, pll + 4);
79                 M = (coef & 0x000000ff) >> 0;
80                 N = (coef & 0x0000ff00) >> 8;
81                 P = (coef & 0x003f0000) >> 16;
82
83                 /* not post-divider on these.. */
84                 if ((pll & 0x00ff00) == 0x00e800)
85                         P = 1;
86
87                 sclk = read_clk(dev, 0x00 + clk, false);
88         } else {
89                 sclk = read_clk(dev, 0x10 + clk, false);
90         }
91
92         return sclk * N / (M * P);
93 }
94
95 struct creg {
96         u32 clk;
97         u32 pll;
98 };
99
100 static int
101 calc_clk(struct drm_device *dev, int clk, u32 pll, u32 khz, struct creg *reg)
102 {
103         struct pll_lims limits;
104         u32 oclk, sclk, sdiv;
105         int P, N, M, diff;
106         int ret;
107
108         reg->pll = 0;
109         reg->clk = 0;
110         if (!khz) {
111                 NV_DEBUG(dev, "no clock for 0x%04x/0x%02x\n", pll, clk);
112                 return 0;
113         }
114
115         switch (khz) {
116         case 27000:
117                 reg->clk = 0x00000100;
118                 return khz;
119         case 100000:
120                 reg->clk = 0x00002100;
121                 return khz;
122         case 108000:
123                 reg->clk = 0x00002140;
124                 return khz;
125         default:
126                 sclk = read_vco(dev, clk);
127                 sdiv = min((sclk * 2) / (khz - 2999), (u32)65);
128                 /* if the clock has a PLL attached, and we can get a within
129                  * [-2, 3) MHz of a divider, we'll disable the PLL and use
130                  * the divider instead.
131                  *
132                  * divider can go as low as 2, limited here because NVIDIA
133                  * and the VBIOS on my NVA8 seem to prefer using the PLL
134                  * for 810MHz - is there a good reason?
135                  */
136                 if (sdiv > 4) {
137                         oclk = (sclk * 2) / sdiv;
138                         diff = khz - oclk;
139                         if (!pll || (diff >= -2000 && diff < 3000)) {
140                                 reg->clk = (((sdiv - 2) << 16) | 0x00003100);
141                                 return oclk;
142                         }
143                 }
144
145                 if (!pll) {
146                         NV_ERROR(dev, "bad freq %02x: %d %d\n", clk, khz, sclk);
147                         return -ERANGE;
148                 }
149
150                 break;
151         }
152
153         ret = get_pll_limits(dev, pll, &limits);
154         if (ret)
155                 return ret;
156
157         limits.refclk = read_clk(dev, clk - 0x10, true);
158         if (!limits.refclk)
159                 return -EINVAL;
160
161         ret = nva3_calc_pll(dev, &limits, khz, &N, NULL, &M, &P);
162         if (ret >= 0) {
163                 reg->clk = nv_rd32(dev, 0x4120 + (clk * 4));
164                 reg->pll = (P << 16) | (N << 8) | M;
165         }
166         return ret;
167 }
168
169 static void
170 prog_pll(struct drm_device *dev, int clk, u32 pll, struct creg *reg)
171 {
172         const u32 src0 = 0x004120 + (clk * 4);
173         const u32 src1 = 0x004160 + (clk * 4);
174         const u32 ctrl = pll + 0;
175         const u32 coef = pll + 4;
176         u32 cntl;
177
178         if (!reg->clk && !reg->pll) {
179                 NV_DEBUG(dev, "no clock for %02x\n", clk);
180                 return;
181         }
182
183         cntl = nv_rd32(dev, ctrl) & 0xfffffff2;
184         if (reg->pll) {
185                 nv_mask(dev, src0, 0x00000101, 0x00000101);
186                 nv_wr32(dev, coef, reg->pll);
187                 nv_wr32(dev, ctrl, cntl | 0x00000015);
188                 nv_mask(dev, src1, 0x00000100, 0x00000000);
189                 nv_mask(dev, src1, 0x00000001, 0x00000000);
190         } else {
191                 nv_mask(dev, src1, 0x003f3141, 0x00000101 | reg->clk);
192                 nv_wr32(dev, ctrl, cntl | 0x0000001d);
193                 nv_mask(dev, ctrl, 0x00000001, 0x00000000);
194                 nv_mask(dev, src0, 0x00000100, 0x00000000);
195                 nv_mask(dev, src0, 0x00000001, 0x00000000);
196         }
197 }
198
199 static void
200 prog_clk(struct drm_device *dev, int clk, struct creg *reg)
201 {
202         if (!reg->clk) {
203                 NV_DEBUG(dev, "no clock for %02x\n", clk);
204                 return;
205         }
206
207         nv_mask(dev, 0x004120 + (clk * 4), 0x003f3141, 0x00000101 | reg->clk);
208 }
209
210 int
211 nva3_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
212 {
213         perflvl->core   = read_pll(dev, 0x00, 0x4200);
214         perflvl->shader = read_pll(dev, 0x01, 0x4220);
215         perflvl->memory = read_pll(dev, 0x02, 0x4000);
216         perflvl->unka0  = read_clk(dev, 0x20, false);
217         perflvl->vdec   = read_clk(dev, 0x21, false);
218         perflvl->daemon = read_clk(dev, 0x25, false);
219         perflvl->copy   = perflvl->core;
220         return 0;
221 }
222
223 struct nva3_pm_state {
224         struct creg nclk;
225         struct creg sclk;
226         struct creg mclk;
227         struct creg vdec;
228         struct creg unka0;
229 };
230
231 void *
232 nva3_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
233 {
234         struct nva3_pm_state *info;
235         int ret;
236
237         info = kzalloc(sizeof(*info), GFP_KERNEL);
238         if (!info)
239                 return ERR_PTR(-ENOMEM);
240
241         ret = calc_clk(dev, 0x10, 0x4200, perflvl->core, &info->nclk);
242         if (ret < 0)
243                 goto out;
244
245         ret = calc_clk(dev, 0x11, 0x4220, perflvl->shader, &info->sclk);
246         if (ret < 0)
247                 goto out;
248
249         ret = calc_clk(dev, 0x12, 0x4000, perflvl->memory, &info->mclk);
250         if (ret < 0)
251                 goto out;
252
253         ret = calc_clk(dev, 0x20, 0x0000, perflvl->unka0, &info->unka0);
254         if (ret < 0)
255                 goto out;
256
257         ret = calc_clk(dev, 0x21, 0x0000, perflvl->vdec, &info->vdec);
258         if (ret < 0)
259                 goto out;
260
261 out:
262         if (ret < 0) {
263                 kfree(info);
264                 info = ERR_PTR(ret);
265         }
266         return info;
267 }
268
269 static bool
270 nva3_pm_grcp_idle(void *data)
271 {
272         struct drm_device *dev = data;
273
274         if (!(nv_rd32(dev, 0x400304) & 0x00000001))
275                 return true;
276         if (nv_rd32(dev, 0x400308) == 0x0050001c)
277                 return true;
278         return false;
279 }
280
281 void
282 nva3_pm_clocks_set(struct drm_device *dev, void *pre_state)
283 {
284         struct drm_nouveau_private *dev_priv = dev->dev_private;
285         struct nva3_pm_state *info = pre_state;
286         unsigned long flags;
287
288         /* prevent any new grctx switches from starting */
289         spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
290         nv_wr32(dev, 0x400324, 0x00000000);
291         nv_wr32(dev, 0x400328, 0x0050001c); /* wait flag 0x1c */
292         /* wait for any pending grctx switches to complete */
293         if (!nv_wait_cb(dev, nva3_pm_grcp_idle, dev)) {
294                 NV_ERROR(dev, "pm: ctxprog didn't go idle\n");
295                 goto cleanup;
296         }
297         /* freeze PFIFO */
298         nv_mask(dev, 0x002504, 0x00000001, 0x00000001);
299         if (!nv_wait(dev, 0x002504, 0x00000010, 0x00000010)) {
300                 NV_ERROR(dev, "pm: fifo didn't go idle\n");
301                 goto cleanup;
302         }
303
304         prog_pll(dev, 0x00, 0x004200, &info->nclk);
305         prog_pll(dev, 0x01, 0x004220, &info->sclk);
306         prog_clk(dev, 0x20, &info->unka0);
307         prog_clk(dev, 0x21, &info->vdec);
308
309         nv_wr32(dev, 0x100210, 0);
310         nv_wr32(dev, 0x1002dc, 1);
311         nv_wr32(dev, 0x004018, 0x00001000);
312         prog_pll(dev, 0x02, 0x004000, &info->mclk);
313         if (nv_rd32(dev, 0x4000) & 0x00000008)
314                 nv_wr32(dev, 0x004018, 0x1000d000);
315         else
316                 nv_wr32(dev, 0x004018, 0x10005000);
317         nv_wr32(dev, 0x1002dc, 0);
318         nv_wr32(dev, 0x100210, 0x80000000);
319
320 cleanup:
321         /* unfreeze PFIFO */
322         nv_mask(dev, 0x002504, 0x00000001, 0x00000000);
323         /* restore ctxprog to normal */
324         nv_wr32(dev, 0x400324, 0x00000000);
325         nv_wr32(dev, 0x400328, 0x0070009c); /* set flag 0x1c */
326         /* unblock it if necessary */
327         if (nv_rd32(dev, 0x400308) == 0x0050001c)
328                 nv_mask(dev, 0x400824, 0x10000000, 0x10000000);
329         spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
330         kfree(info);
331 }