drm/nvc0/pm: start filling in memory reclocking stubs
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nvc0_pm.c
1 /*
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
30 static u32 read_div(struct drm_device *, int, u32, u32);
31 static u32 read_pll(struct drm_device *, u32);
32
33 static u32
34 read_vco(struct drm_device *dev, u32 dsrc)
35 {
36         u32 ssrc = nv_rd32(dev, dsrc);
37         if (!(ssrc & 0x00000100))
38                 return read_pll(dev, 0x00e800);
39         return read_pll(dev, 0x00e820);
40 }
41
42 static u32
43 read_pll(struct drm_device *dev, u32 pll)
44 {
45         u32 ctrl = nv_rd32(dev, pll + 0);
46         u32 coef = nv_rd32(dev, pll + 4);
47         u32 P = (coef & 0x003f0000) >> 16;
48         u32 N = (coef & 0x0000ff00) >> 8;
49         u32 M = (coef & 0x000000ff) >> 0;
50         u32 sclk, doff;
51
52         if (!(ctrl & 0x00000001))
53                 return 0;
54
55         switch (pll & 0xfff000) {
56         case 0x00e000:
57                 sclk = 27000;
58                 P = 1;
59                 break;
60         case 0x137000:
61                 doff = (pll - 0x137000) / 0x20;
62                 sclk = read_div(dev, doff, 0x137120, 0x137140);
63                 break;
64         case 0x132000:
65                 switch (pll) {
66                 case 0x132000:
67                         sclk = read_pll(dev, 0x132020);
68                         break;
69                 case 0x132020:
70                         sclk = read_div(dev, 0, 0x137320, 0x137330);
71                         break;
72                 default:
73                         return 0;
74                 }
75                 break;
76         default:
77                 return 0;
78         }
79
80         return sclk * N / M / P;
81 }
82
83 static u32
84 read_div(struct drm_device *dev, int doff, u32 dsrc, u32 dctl)
85 {
86         u32 ssrc = nv_rd32(dev, dsrc + (doff * 4));
87         u32 sctl = nv_rd32(dev, dctl + (doff * 4));
88
89         switch (ssrc & 0x00000003) {
90         case 0:
91                 if ((ssrc & 0x00030000) != 0x00030000)
92                         return 27000;
93                 return 108000;
94         case 2:
95                 return 100000;
96         case 3:
97                 if (sctl & 0x80000000) {
98                         u32 sclk = read_vco(dev, dsrc + (doff * 4));
99                         u32 sdiv = (sctl & 0x0000003f) + 2;
100                         return (sclk * 2) / sdiv;
101                 }
102
103                 return read_vco(dev, dsrc + (doff * 4));
104         default:
105                 return 0;
106         }
107 }
108
109 static u32
110 read_mem(struct drm_device *dev)
111 {
112         u32 ssel = nv_rd32(dev, 0x1373f0);
113         if (ssel & 0x00000001)
114                 return read_div(dev, 0, 0x137300, 0x137310);
115         return read_pll(dev, 0x132000);
116 }
117
118 static u32
119 read_clk(struct drm_device *dev, int clk)
120 {
121         u32 sctl = nv_rd32(dev, 0x137250 + (clk * 4));
122         u32 ssel = nv_rd32(dev, 0x137100);
123         u32 sclk, sdiv;
124
125         if (ssel & (1 << clk)) {
126                 if (clk < 7)
127                         sclk = read_pll(dev, 0x137000 + (clk * 0x20));
128                 else
129                         sclk = read_pll(dev, 0x1370e0);
130                 sdiv = ((sctl & 0x00003f00) >> 8) + 2;
131         } else {
132                 sclk = read_div(dev, clk, 0x137160, 0x1371d0);
133                 sdiv = ((sctl & 0x0000003f) >> 0) + 2;
134         }
135
136         if (sctl & 0x80000000)
137                 return (sclk * 2) / sdiv;
138         return sclk;
139 }
140
141 int
142 nvc0_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
143 {
144         perflvl->shader = read_clk(dev, 0x00);
145         perflvl->core   = perflvl->shader / 2;
146         perflvl->memory = read_mem(dev);
147         perflvl->rop    = read_clk(dev, 0x01);
148         perflvl->hub07  = read_clk(dev, 0x02);
149         perflvl->hub06  = read_clk(dev, 0x07);
150         perflvl->hub01  = read_clk(dev, 0x08);
151         perflvl->copy   = read_clk(dev, 0x09);
152         perflvl->daemon = read_clk(dev, 0x0c);
153         perflvl->vdec   = read_clk(dev, 0x0e);
154         return 0;
155 }
156
157 struct nvc0_pm_clock {
158         u32 freq;
159         u32 ssel;
160         u32 mdiv;
161         u32 dsrc;
162         u32 ddiv;
163         u32 coef;
164 };
165
166 struct nvc0_pm_state {
167         struct nouveau_pm_level *perflvl;
168         struct nvc0_pm_clock eng[16];
169 };
170
171 static u32
172 calc_div(struct drm_device *dev, int clk, u32 ref, u32 freq, u32 *ddiv)
173 {
174         u32 div = min((ref * 2) / freq, (u32)65);
175         if (div < 2)
176                 div = 2;
177
178         *ddiv = div - 2;
179         return (ref * 2) / div;
180 }
181
182 static u32
183 calc_src(struct drm_device *dev, int clk, u32 freq, u32 *dsrc, u32 *ddiv)
184 {
185         u32 sclk;
186
187         /* use one of the fixed frequencies if possible */
188         *ddiv = 0x00000000;
189         switch (freq) {
190         case  27000:
191         case 108000:
192                 *dsrc = 0x00000000;
193                 if (freq == 108000)
194                         *dsrc |= 0x00030000;
195                 return freq;
196         case 100000:
197                 *dsrc = 0x00000002;
198                 return freq;
199         default:
200                 *dsrc = 0x00000003;
201                 break;
202         }
203
204         /* otherwise, calculate the closest divider */
205         sclk = read_vco(dev, clk);
206         if (clk < 7)
207                 sclk = calc_div(dev, clk, sclk, freq, ddiv);
208         return sclk;
209 }
210
211 static u32
212 calc_pll(struct drm_device *dev, int clk, u32 freq, u32 *coef)
213 {
214         struct pll_lims limits;
215         int N, M, P, ret;
216
217         ret = get_pll_limits(dev, 0x137000 + (clk * 0x20), &limits);
218         if (ret)
219                 return 0;
220
221         limits.refclk = read_div(dev, clk, 0x137120, 0x137140);
222         if (!limits.refclk)
223                 return 0;
224
225         ret = nva3_calc_pll(dev, &limits, freq, &N, NULL, &M, &P);
226         if (ret <= 0)
227                 return 0;
228
229         *coef = (P << 16) | (N << 8) | M;
230         return ret;
231 }
232
233 /* A (likely rather simplified and incomplete) view of the clock tree
234  *
235  * Key:
236  *
237  * S: source select
238  * D: divider
239  * P: pll
240  * F: switch
241  *
242  * Engine clocks:
243  *
244  * 137250(D) ---- 137100(F0) ---- 137160(S)/1371d0(D) ------------------- ref
245  *                      (F1) ---- 1370X0(P) ---- 137120(S)/137140(D) ---- ref
246  *
247  * Not all registers exist for all clocks.  For example: clocks >= 8 don't
248  * have their own PLL (all tied to clock 7's PLL when in PLL mode), nor do
249  * they have the divider at 1371d0, though the source selection at 137160
250  * still exists.  You must use the divider at 137250 for these instead.
251  *
252  * Memory clock:
253  *
254  * TBD, read_mem() above is likely very wrong...
255  *
256  */
257
258 static int
259 calc_clk(struct drm_device *dev, int clk, struct nvc0_pm_clock *info, u32 freq)
260 {
261         u32 src0, div0, div1D, div1P = 0;
262         u32 clk0, clk1 = 0;
263
264         /* invalid clock domain */
265         if (!freq)
266                 return 0;
267
268         /* first possible path, using only dividers */
269         clk0 = calc_src(dev, clk, freq, &src0, &div0);
270         clk0 = calc_div(dev, clk, clk0, freq, &div1D);
271
272         /* see if we can get any closer using PLLs */
273         if (clk0 != freq && (0x00004387 & (1 << clk))) {
274                 if (clk < 7)
275                         clk1 = calc_pll(dev, clk, freq, &info->coef);
276                 else
277                         clk1 = read_pll(dev, 0x1370e0);
278                 clk1 = calc_div(dev, clk, clk1, freq, &div1P);
279         }
280
281         /* select the method which gets closest to target freq */
282         if (abs((int)freq - clk0) <= abs((int)freq - clk1)) {
283                 info->dsrc = src0;
284                 if (div0) {
285                         info->ddiv |= 0x80000000;
286                         info->ddiv |= div0 << 8;
287                         info->ddiv |= div0;
288                 }
289                 if (div1D) {
290                         info->mdiv |= 0x80000000;
291                         info->mdiv |= div1D;
292                 }
293                 info->ssel = 0;
294                 info->freq = clk0;
295         } else {
296                 if (div1P) {
297                         info->mdiv |= 0x80000000;
298                         info->mdiv |= div1P << 8;
299                 }
300                 info->ssel = (1 << clk);
301                 info->freq = clk1;
302         }
303
304         return 0;
305 }
306
307 void *
308 nvc0_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl)
309 {
310         struct drm_nouveau_private *dev_priv = dev->dev_private;
311         struct nvc0_pm_state *info;
312         int ret;
313
314         info = kzalloc(sizeof(*info), GFP_KERNEL);
315         if (!info)
316                 return ERR_PTR(-ENOMEM);
317
318         /* NFI why this is still in the performance table, the ROPCs appear
319          * to get their clock from clock 2 ("hub07", actually hub05 on this
320          * chip, but, anyway...) as well.  nvatiming confirms hub05 and ROP
321          * are always the same freq with the binary driver even when the
322          * performance table says they should differ.
323          */
324         if (dev_priv->chipset == 0xd9)
325                 perflvl->rop = 0;
326
327         if ((ret = calc_clk(dev, 0x00, &info->eng[0x00], perflvl->shader)) ||
328             (ret = calc_clk(dev, 0x01, &info->eng[0x01], perflvl->rop)) ||
329             (ret = calc_clk(dev, 0x02, &info->eng[0x02], perflvl->hub07)) ||
330             (ret = calc_clk(dev, 0x07, &info->eng[0x07], perflvl->hub06)) ||
331             (ret = calc_clk(dev, 0x08, &info->eng[0x08], perflvl->hub01)) ||
332             (ret = calc_clk(dev, 0x09, &info->eng[0x09], perflvl->copy)) ||
333             (ret = calc_clk(dev, 0x0c, &info->eng[0x0c], perflvl->daemon)) ||
334             (ret = calc_clk(dev, 0x0e, &info->eng[0x0e], perflvl->vdec))) {
335                 kfree(info);
336                 return ERR_PTR(ret);
337         }
338
339         info->perflvl = perflvl;
340         return info;
341 }
342
343 static void
344 prog_clk(struct drm_device *dev, int clk, struct nvc0_pm_clock *info)
345 {
346         /* program dividers at 137160/1371d0 first */
347         if (clk < 7 && !info->ssel) {
348                 nv_mask(dev, 0x1371d0 + (clk * 0x04), 0x80003f3f, info->ddiv);
349                 nv_wr32(dev, 0x137160 + (clk * 0x04), info->dsrc);
350         }
351
352         /* switch clock to non-pll mode */
353         nv_mask(dev, 0x137100, (1 << clk), 0x00000000);
354         nv_wait(dev, 0x137100, (1 << clk), 0x00000000);
355
356         /* reprogram pll */
357         if (clk < 7) {
358                 /* make sure it's disabled first... */
359                 u32 base = 0x137000 + (clk * 0x20);
360                 u32 ctrl = nv_rd32(dev, base + 0x00);
361                 if (ctrl & 0x00000001) {
362                         nv_mask(dev, base + 0x00, 0x00000004, 0x00000000);
363                         nv_mask(dev, base + 0x00, 0x00000001, 0x00000000);
364                 }
365                 /* program it to new values, if necessary */
366                 if (info->ssel) {
367                         nv_wr32(dev, base + 0x04, info->coef);
368                         nv_mask(dev, base + 0x00, 0x00000001, 0x00000001);
369                         nv_wait(dev, base + 0x00, 0x00020000, 0x00020000);
370                         nv_mask(dev, base + 0x00, 0x00020004, 0x00000004);
371                 }
372         }
373
374         /* select pll/non-pll mode, and program final clock divider */
375         nv_mask(dev, 0x137100, (1 << clk), info->ssel);
376         nv_wait(dev, 0x137100, (1 << clk), info->ssel);
377         nv_mask(dev, 0x137250 + (clk * 0x04), 0x00003f3f, info->mdiv);
378 }
379
380 static void
381 mclk_precharge(struct nouveau_mem_exec_func *exec)
382 {
383 }
384
385 static void
386 mclk_refresh(struct nouveau_mem_exec_func *exec)
387 {
388 }
389
390 static void
391 mclk_refresh_auto(struct nouveau_mem_exec_func *exec, bool enable)
392 {
393         nv_wr32(exec->dev, 0x10f210, enable ? 0x80000000 : 0x00000000);
394 }
395
396 static void
397 mclk_refresh_self(struct nouveau_mem_exec_func *exec, bool enable)
398 {
399 }
400
401 static void
402 mclk_wait(struct nouveau_mem_exec_func *exec, u32 nsec)
403 {
404         udelay((nsec + 500) / 1000);
405 }
406
407 static u32
408 mclk_mrg(struct nouveau_mem_exec_func *exec, int mr)
409 {
410         struct drm_device *dev = exec->dev;
411         struct drm_nouveau_private *dev_priv = dev->dev_private;
412         if (dev_priv->vram_type != NV_MEM_TYPE_GDDR5) {
413                 if (mr <= 1)
414                         return nv_rd32(dev, 0x10f300 + ((mr - 0) * 4));
415                 return nv_rd32(dev, 0x10f320 + ((mr - 2) * 4));
416         } else {
417                 if (mr == 0)
418                         return nv_rd32(dev, 0x10f300 + (mr * 4));
419                 else
420                 if (mr <= 7)
421                         return nv_rd32(dev, 0x10f32c + (mr * 4));
422                 return nv_rd32(dev, 0x10f34c);
423         }
424 }
425
426 static void
427 mclk_mrs(struct nouveau_mem_exec_func *exec, int mr, u32 data)
428 {
429         struct drm_device *dev = exec->dev;
430         struct drm_nouveau_private *dev_priv = dev->dev_private;
431         if (dev_priv->vram_type != NV_MEM_TYPE_GDDR5) {
432                 if (mr <= 1) {
433                         nv_wr32(dev, 0x10f300 + ((mr - 0) * 4), data);
434                         if (dev_priv->vram_rank_B)
435                                 nv_wr32(dev, 0x10f308 + ((mr - 0) * 4), data);
436                 } else
437                 if (mr <= 3) {
438                         nv_wr32(dev, 0x10f320 + ((mr - 2) * 4), data);
439                         if (dev_priv->vram_rank_B)
440                                 nv_wr32(dev, 0x10f328 + ((mr - 2) * 4), data);
441                 }
442         } else {
443                 if      (mr ==  0) nv_wr32(dev, 0x10f300 + (mr * 4), data);
444                 else if (mr <=  7) nv_wr32(dev, 0x10f32c + (mr * 4), data);
445                 else if (mr == 15) nv_wr32(dev, 0x10f34c, data);
446         }
447 }
448
449 static void
450 mclk_clock_set(struct nouveau_mem_exec_func *exec)
451 {
452 }
453
454 static void
455 mclk_timing_set(struct nouveau_mem_exec_func *exec)
456 {
457         struct nvc0_pm_state *info = exec->priv;
458         struct nouveau_pm_level *perflvl = info->perflvl;
459         int i;
460
461         for (i = 0; i < 5; i++)
462                 nv_wr32(exec->dev, 0x10f290 + (i * 4), perflvl->timing.reg[i]);
463 }
464
465 static void
466 prog_mem(struct drm_device *dev, struct nvc0_pm_state *info)
467 {
468         struct drm_nouveau_private *dev_priv = dev->dev_private;
469         struct nouveau_mem_exec_func exec = {
470                 .dev = dev,
471                 .precharge = mclk_precharge,
472                 .refresh = mclk_refresh,
473                 .refresh_auto = mclk_refresh_auto,
474                 .refresh_self = mclk_refresh_self,
475                 .wait = mclk_wait,
476                 .mrg = mclk_mrg,
477                 .mrs = mclk_mrs,
478                 .clock_set = mclk_clock_set,
479                 .timing_set = mclk_timing_set,
480                 .priv = info
481         };
482
483         if (dev_priv->chipset < 0xd0)
484                 nv_wr32(dev, 0x611200, 0x00003300);
485
486         nouveau_mem_exec(&exec, info->perflvl);
487
488         if (dev_priv->chipset < 0xd0)
489                 nv_wr32(dev, 0x611200, 0x00003300);
490 }
491 int
492 nvc0_pm_clocks_set(struct drm_device *dev, void *data)
493 {
494         struct nvc0_pm_state *info = data;
495         int i;
496
497         if (0)
498                 prog_mem(dev, info);
499
500         for (i = 0; i < 16; i++) {
501                 if (!info->eng[i].freq)
502                         continue;
503                 prog_clk(dev, i, &info->eng[i]);
504         }
505
506         kfree(info);
507         return 0;
508 }