kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / ddi_compute_wrpll.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5
6 #include "intel_io.h"
7 #include "drmtest.h"
8
9 #define LC_FREQ 2700
10 #define LC_FREQ_2K (LC_FREQ * 2000)
11
12 #define P_MIN 2
13 #define P_MAX 64
14 #define P_INC 2
15
16 /* Constraints for PLL good behavior */
17 #define REF_MIN 48
18 #define REF_MAX 400
19 #define VCO_MIN 2400
20 #define VCO_MAX 4800
21
22 #define ABS_DIFF(a, b) ((a > b) ? (a - b) : (b - a))
23
24 struct wrpll_rnp {
25         unsigned p, n2, r2;
26 };
27
28 static unsigned wrpll_get_budget_for_freq(int clock)
29 {
30         unsigned budget;
31
32         switch (clock) {
33         case 25175000:
34         case 25200000:
35         case 27000000:
36         case 27027000:
37         case 37762500:
38         case 37800000:
39         case 40500000:
40         case 40541000:
41         case 54000000:
42         case 54054000:
43         case 59341000:
44         case 59400000:
45         case 72000000:
46         case 74176000:
47         case 74250000:
48         case 81000000:
49         case 81081000:
50         case 89012000:
51         case 89100000:
52         case 108000000:
53         case 108108000:
54         case 111264000:
55         case 111375000:
56         case 148352000:
57         case 148500000:
58         case 162000000:
59         case 162162000:
60         case 222525000:
61         case 222750000:
62         case 296703000:
63         case 297000000:
64                 budget = 0;
65                 break;
66         case 233500000:
67         case 245250000:
68         case 247750000:
69         case 253250000:
70         case 298000000:
71                 budget = 1500;
72                 break;
73         case 169128000:
74         case 169500000:
75         case 179500000:
76         case 202000000:
77                 budget = 2000;
78                 break;
79         case 256250000:
80         case 262500000:
81         case 270000000:
82         case 272500000:
83         case 273750000:
84         case 280750000:
85         case 281250000:
86         case 286000000:
87         case 291750000:
88                 budget = 4000;
89                 break;
90         case 267250000:
91         case 268500000:
92                 budget = 5000;
93                 break;
94         default:
95                 budget = 1000;
96                 break;
97         }
98
99         return budget;
100 }
101
102 static void wrpll_update_rnp(uint64_t freq2k, unsigned budget,
103                              unsigned r2, unsigned n2, unsigned p,
104                              struct wrpll_rnp *best)
105 {
106         uint64_t a, b, c, d, diff, diff_best;
107
108         /* No best (r,n,p) yet */
109         if (best->p == 0) {
110                 best->p = p;
111                 best->n2 = n2;
112                 best->r2 = r2;
113                 return;
114         }
115
116         /*
117          * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
118          * freq2k.
119          *
120          * delta = 1e6 *
121          *         abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
122          *         freq2k;
123          *
124          * and we would like delta <= budget.
125          *
126          * If the discrepancy is above the PPM-based budget, always prefer to
127          * improve upon the previous solution.  However, if you're within the
128          * budget, try to maximize Ref * VCO, that is N / (P * R^2).
129          */
130         a = freq2k * budget * p * r2;
131         b = freq2k * budget * best->p * best->r2;
132         diff = ABS_DIFF((freq2k * p * r2), (LC_FREQ_2K * n2));
133         diff_best = ABS_DIFF((freq2k * best->p * best->r2),
134                              (LC_FREQ_2K * best->n2));
135         c = 1000000 * diff;
136         d = 1000000 * diff_best;
137
138         if (a < c && b < d) {
139                 /* If both are above the budget, pick the closer */
140                 if (best->p * best->r2 * diff < p * r2 * diff_best) {
141                         best->p = p;
142                         best->n2 = n2;
143                         best->r2 = r2;
144                 }
145         } else if (a >= c && b < d) {
146                 /* If A is below the threshold but B is above it?  Update. */
147                 best->p = p;
148                 best->n2 = n2;
149                 best->r2 = r2;
150         } else if (a >= c && b >= d) {
151                 /* Both are below the limit, so pick the higher n2/(r2*r2) */
152                 if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
153                         best->p = p;
154                         best->n2 = n2;
155                         best->r2 = r2;
156                 }
157         }
158         /* Otherwise a < c && b >= d, do nothing */
159 }
160
161 static void
162 wrpll_compute_rnp(int clock /* in Hz */,
163                   unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
164 {
165         uint64_t freq2k;
166         unsigned p, n2, r2;
167         struct wrpll_rnp best = { 0, 0, 0 };
168         unsigned budget;
169
170         freq2k = clock / 100;
171
172         budget = wrpll_get_budget_for_freq(clock);
173
174         /* Special case handling for 540 pixel clock: bypass WR PLL entirely
175          * and directly pass the LC PLL to it. */
176         if (freq2k == 5400000) {
177                 *n2_out = 2;
178                 *p_out = 1;
179                 *r2_out = 2;
180                 return;
181         }
182
183         /*
184          * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
185          * the WR PLL.
186          *
187          * We want R so that REF_MIN <= Ref <= REF_MAX.
188          * Injecting R2 = 2 * R gives:
189          *   REF_MAX * r2 > LC_FREQ * 2 and
190          *   REF_MIN * r2 < LC_FREQ * 2
191          *
192          * Which means the desired boundaries for r2 are:
193          *  LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
194          *
195          */
196         for (r2 = LC_FREQ * 2 / REF_MAX + 1;
197              r2 <= LC_FREQ * 2 / REF_MIN;
198              r2++) {
199
200                 /*
201                  * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
202                  *
203                  * Once again we want VCO_MIN <= VCO <= VCO_MAX.
204                  * Injecting R2 = 2 * R and N2 = 2 * N, we get:
205                  *   VCO_MAX * r2 > n2 * LC_FREQ and
206                  *   VCO_MIN * r2 < n2 * LC_FREQ)
207                  *
208                  * Which means the desired boundaries for n2 are:
209                  * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
210                  */
211                 for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
212                      n2 <= VCO_MAX * r2 / LC_FREQ;
213                      n2++) {
214
215                         for (p = P_MIN; p <= P_MAX; p += P_INC)
216                                 wrpll_update_rnp(freq2k, budget,
217                                                  r2, n2, p, &best);
218                 }
219         }
220
221         *n2_out = best.n2;
222         *p_out = best.p;
223         *r2_out = best.r2;
224 }
225
226 /* WRPLL clock dividers */
227 struct wrpll_tmds_clock {
228         uint32_t clock;
229         uint16_t p;             /* Post divider */
230         uint16_t n2;            /* Feedback divider */
231         uint16_t r2;            /* Reference divider */
232 };
233
234 /* Table of matching values for WRPLL clocks programming for each frequency.
235  * The code assumes this table is sorted. */
236 static const struct wrpll_tmds_clock wrpll_tmds_clock_table[] = {
237         {19750000,      38,     25,     18},
238         {20000000,      48,     32,     18},
239         {21000000,      36,     21,     15},
240         {21912000,      42,     29,     17},
241         {22000000,      36,     22,     15},
242         {23000000,      36,     23,     15},
243         {23500000,      40,     40,     23},
244         {23750000,      26,     16,     14},
245         {24000000,      36,     24,     15},
246         {25000000,      36,     25,     15},
247         {25175000,      26,     40,     33},
248         {25200000,      30,     21,     15},
249         {26000000,      36,     26,     15},
250         {27000000,      30,     21,     14},
251         {27027000,      18,     100,    111},
252         {27500000,      30,     29,     19},
253         {28000000,      34,     30,     17},
254         {28320000,      26,     30,     22},
255         {28322000,      32,     42,     25},
256         {28750000,      24,     23,     18},
257         {29000000,      30,     29,     18},
258         {29750000,      32,     30,     17},
259         {30000000,      30,     25,     15},
260         {30750000,      30,     41,     24},
261         {31000000,      30,     31,     18},
262         {31500000,      30,     28,     16},
263         {32000000,      30,     32,     18},
264         {32500000,      28,     32,     19},
265         {33000000,      24,     22,     15},
266         {34000000,      28,     30,     17},
267         {35000000,      26,     32,     19},
268         {35500000,      24,     30,     19},
269         {36000000,      26,     26,     15},
270         {36750000,      26,     46,     26},
271         {37000000,      24,     23,     14},
272         {37762500,      22,     40,     26},
273         {37800000,      20,     21,     15},
274         {38000000,      24,     27,     16},
275         {38250000,      24,     34,     20},
276         {39000000,      24,     26,     15},
277         {40000000,      24,     32,     18},
278         {40500000,      20,     21,     14},
279         {40541000,      22,     147,    89},
280         {40750000,      18,     19,     14},
281         {41000000,      16,     17,     14},
282         {41500000,      22,     44,     26},
283         {41540000,      22,     44,     26},
284         {42000000,      18,     21,     15},
285         {42500000,      22,     45,     26},
286         {43000000,      20,     43,     27},
287         {43163000,      20,     24,     15},
288         {44000000,      18,     22,     15},
289         {44900000,      20,     108,    65},
290         {45000000,      20,     25,     15},
291         {45250000,      20,     52,     31},
292         {46000000,      18,     23,     15},
293         {46750000,      20,     45,     26},
294         {47000000,      20,     40,     23},
295         {48000000,      18,     24,     15},
296         {49000000,      18,     49,     30},
297         {49500000,      16,     22,     15},
298         {50000000,      18,     25,     15},
299         {50500000,      18,     32,     19},
300         {51000000,      18,     34,     20},
301         {52000000,      18,     26,     15},
302         {52406000,      14,     34,     25},
303         {53000000,      16,     22,     14},
304         {54000000,      16,     24,     15},
305         {54054000,      16,     173,    108},
306         {54500000,      14,     24,     17},
307         {55000000,      12,     22,     18},
308         {56000000,      14,     45,     31},
309         {56250000,      16,     25,     15},
310         {56750000,      14,     25,     17},
311         {57000000,      16,     27,     16},
312         {58000000,      16,     43,     25},
313         {58250000,      16,     38,     22},
314         {58750000,      16,     40,     23},
315         {59000000,      14,     26,     17},
316         {59341000,      14,     40,     26},
317         {59400000,      16,     44,     25},
318         {60000000,      16,     32,     18},
319         {60500000,      12,     39,     29},
320         {61000000,      14,     49,     31},
321         {62000000,      14,     37,     23},
322         {62250000,      14,     42,     26},
323         {63000000,      12,     21,     15},
324         {63500000,      14,     28,     17},
325         {64000000,      12,     27,     19},
326         {65000000,      14,     32,     19},
327         {65250000,      12,     29,     20},
328         {65500000,      12,     32,     22},
329         {66000000,      12,     22,     15},
330         {66667000,      14,     38,     22},
331         {66750000,      10,     21,     17},
332         {67000000,      14,     33,     19},
333         {67750000,      14,     58,     33},
334         {68000000,      14,     30,     17},
335         {68179000,      14,     46,     26},
336         {68250000,      14,     46,     26},
337         {69000000,      12,     23,     15},
338         {70000000,      12,     28,     18},
339         {71000000,      12,     30,     19},
340         {72000000,      12,     24,     15},
341         {73000000,      10,     23,     17},
342         {74000000,      12,     23,     14},
343         {74176000,      8,      100,    91},
344         {74250000,      10,     22,     16},
345         {74481000,      12,     43,     26},
346         {74500000,      10,     29,     21},
347         {75000000,      12,     25,     15},
348         {75250000,      10,     39,     28},
349         {76000000,      12,     27,     16},
350         {77000000,      12,     53,     31},
351         {78000000,      12,     26,     15},
352         {78750000,      12,     28,     16},
353         {79000000,      10,     38,     26},
354         {79500000,      10,     28,     19},
355         {80000000,      12,     32,     18},
356         {81000000,      10,     21,     14},
357         {81081000,      6,      100,    111},
358         {81624000,      8,      29,     24},
359         {82000000,      8,      17,     14},
360         {83000000,      10,     40,     26},
361         {83950000,      10,     28,     18},
362         {84000000,      10,     28,     18},
363         {84750000,      6,      16,     17},
364         {85000000,      6,      17,     18},
365         {85250000,      10,     30,     19},
366         {85750000,      10,     27,     17},
367         {86000000,      10,     43,     27},
368         {87000000,      10,     29,     18},
369         {88000000,      10,     44,     27},
370         {88500000,      10,     41,     25},
371         {89000000,      10,     28,     17},
372         {89012000,      6,      90,     91},
373         {89100000,      10,     33,     20},
374         {90000000,      10,     25,     15},
375         {91000000,      10,     32,     19},
376         {92000000,      10,     46,     27},
377         {93000000,      10,     31,     18},
378         {94000000,      10,     40,     23},
379         {94500000,      10,     28,     16},
380         {95000000,      10,     44,     25},
381         {95654000,      10,     39,     22},
382         {95750000,      10,     39,     22},
383         {96000000,      10,     32,     18},
384         {97000000,      8,      23,     16},
385         {97750000,      8,      42,     29},
386         {98000000,      8,      45,     31},
387         {99000000,      8,      22,     15},
388         {99750000,      8,      34,     23},
389         {100000000,     6,      20,     18},
390         {100500000,     6,      19,     17},
391         {101000000,     6,      37,     33},
392         {101250000,     8,      21,     14},
393         {102000000,     6,      17,     15},
394         {102250000,     6,      25,     22},
395         {103000000,     8,      29,     19},
396         {104000000,     8,      37,     24},
397         {105000000,     8,      28,     18},
398         {106000000,     8,      22,     14},
399         {107000000,     8,      46,     29},
400         {107214000,     8,      27,     17},
401         {108000000,     8,      24,     15},
402         {108108000,     8,      173,    108},
403         {109000000,     6,      23,     19},
404         {110000000,     6,      22,     18},
405         {110013000,     6,      22,     18},
406         {110250000,     8,      49,     30},
407         {110500000,     8,      36,     22},
408         {111000000,     8,      23,     14},
409         {111264000,     8,      150,    91},
410         {111375000,     8,      33,     20},
411         {112000000,     8,      63,     38},
412         {112500000,     8,      25,     15},
413         {113100000,     8,      57,     34},
414         {113309000,     8,      42,     25},
415         {114000000,     8,      27,     16},
416         {115000000,     6,      23,     18},
417         {116000000,     8,      43,     25},
418         {117000000,     8,      26,     15},
419         {117500000,     8,      40,     23},
420         {118000000,     6,      38,     29},
421         {119000000,     8,      30,     17},
422         {119500000,     8,      46,     26},
423         {119651000,     8,      39,     22},
424         {120000000,     8,      32,     18},
425         {121000000,     6,      39,     29},
426         {121250000,     6,      31,     23},
427         {121750000,     6,      23,     17},
428         {122000000,     6,      42,     31},
429         {122614000,     6,      30,     22},
430         {123000000,     6,      41,     30},
431         {123379000,     6,      37,     27},
432         {124000000,     6,      51,     37},
433         {125000000,     6,      25,     18},
434         {125250000,     4,      13,     14},
435         {125750000,     4,      27,     29},
436         {126000000,     6,      21,     15},
437         {127000000,     6,      24,     17},
438         {127250000,     6,      41,     29},
439         {128000000,     6,      27,     19},
440         {129000000,     6,      43,     30},
441         {129859000,     4,      25,     26},
442         {130000000,     6,      26,     18},
443         {130250000,     6,      42,     29},
444         {131000000,     6,      32,     22},
445         {131500000,     6,      38,     26},
446         {131850000,     6,      41,     28},
447         {132000000,     6,      22,     15},
448         {132750000,     6,      28,     19},
449         {133000000,     6,      34,     23},
450         {133330000,     6,      37,     25},
451         {134000000,     6,      61,     41},
452         {135000000,     6,      21,     14},
453         {135250000,     6,      167,    111},
454         {136000000,     6,      62,     41},
455         {137000000,     6,      35,     23},
456         {138000000,     6,      23,     15},
457         {138500000,     6,      40,     26},
458         {138750000,     6,      37,     24},
459         {139000000,     6,      34,     22},
460         {139050000,     6,      34,     22},
461         {139054000,     6,      34,     22},
462         {140000000,     6,      28,     18},
463         {141000000,     6,      36,     23},
464         {141500000,     6,      22,     14},
465         {142000000,     6,      30,     19},
466         {143000000,     6,      27,     17},
467         {143472000,     4,      17,     16},
468         {144000000,     6,      24,     15},
469         {145000000,     6,      29,     18},
470         {146000000,     6,      47,     29},
471         {146250000,     6,      26,     16},
472         {147000000,     6,      49,     30},
473         {147891000,     6,      23,     14},
474         {148000000,     6,      23,     14},
475         {148250000,     6,      28,     17},
476         {148352000,     4,      100,    91},
477         {148500000,     6,      33,     20},
478         {149000000,     6,      48,     29},
479         {150000000,     6,      25,     15},
480         {151000000,     4,      19,     17},
481         {152000000,     6,      27,     16},
482         {152280000,     6,      44,     26},
483         {153000000,     6,      34,     20},
484         {154000000,     6,      53,     31},
485         {155000000,     6,      31,     18},
486         {155250000,     6,      50,     29},
487         {155750000,     6,      45,     26},
488         {156000000,     6,      26,     15},
489         {157000000,     6,      61,     35},
490         {157500000,     6,      28,     16},
491         {158000000,     6,      65,     37},
492         {158250000,     6,      44,     25},
493         {159000000,     6,      53,     30},
494         {159500000,     6,      39,     22},
495         {160000000,     6,      32,     18},
496         {161000000,     4,      31,     26},
497         {162000000,     4,      18,     15},
498         {162162000,     4,      131,    109},
499         {162500000,     4,      53,     44},
500         {163000000,     4,      29,     24},
501         {164000000,     4,      17,     14},
502         {165000000,     4,      22,     18},
503         {166000000,     4,      32,     26},
504         {167000000,     4,      26,     21},
505         {168000000,     4,      46,     37},
506         {169000000,     4,      104,    83},
507         {169128000,     4,      64,     51},
508         {169500000,     4,      39,     31},
509         {170000000,     4,      34,     27},
510         {171000000,     4,      19,     15},
511         {172000000,     4,      51,     40},
512         {172750000,     4,      32,     25},
513         {172800000,     4,      32,     25},
514         {173000000,     4,      41,     32},
515         {174000000,     4,      49,     38},
516         {174787000,     4,      22,     17},
517         {175000000,     4,      35,     27},
518         {176000000,     4,      30,     23},
519         {177000000,     4,      38,     29},
520         {178000000,     4,      29,     22},
521         {178500000,     4,      37,     28},
522         {179000000,     4,      53,     40},
523         {179500000,     4,      73,     55},
524         {180000000,     4,      20,     15},
525         {181000000,     4,      55,     41},
526         {182000000,     4,      31,     23},
527         {183000000,     4,      42,     31},
528         {184000000,     4,      30,     22},
529         {184750000,     4,      26,     19},
530         {185000000,     4,      37,     27},
531         {186000000,     4,      51,     37},
532         {187000000,     4,      36,     26},
533         {188000000,     4,      32,     23},
534         {189000000,     4,      21,     15},
535         {190000000,     4,      38,     27},
536         {190960000,     4,      41,     29},
537         {191000000,     4,      41,     29},
538         {192000000,     4,      27,     19},
539         {192250000,     4,      37,     26},
540         {193000000,     4,      20,     14},
541         {193250000,     4,      53,     37},
542         {194000000,     4,      23,     16},
543         {194208000,     4,      23,     16},
544         {195000000,     4,      26,     18},
545         {196000000,     4,      45,     31},
546         {197000000,     4,      35,     24},
547         {197750000,     4,      41,     28},
548         {198000000,     4,      22,     15},
549         {198500000,     4,      25,     17},
550         {199000000,     4,      28,     19},
551         {200000000,     4,      37,     25},
552         {201000000,     4,      61,     41},
553         {202000000,     4,      112,    75},
554         {202500000,     4,      21,     14},
555         {203000000,     4,      146,    97},
556         {204000000,     4,      62,     41},
557         {204750000,     4,      44,     29},
558         {205000000,     4,      38,     25},
559         {206000000,     4,      29,     19},
560         {207000000,     4,      23,     15},
561         {207500000,     4,      40,     26},
562         {208000000,     4,      37,     24},
563         {208900000,     4,      48,     31},
564         {209000000,     4,      48,     31},
565         {209250000,     4,      31,     20},
566         {210000000,     4,      28,     18},
567         {211000000,     4,      25,     16},
568         {212000000,     4,      22,     14},
569         {213000000,     4,      30,     19},
570         {213750000,     4,      38,     24},
571         {214000000,     4,      46,     29},
572         {214750000,     4,      35,     22},
573         {215000000,     4,      43,     27},
574         {216000000,     4,      24,     15},
575         {217000000,     4,      37,     23},
576         {218000000,     4,      42,     26},
577         {218250000,     4,      42,     26},
578         {218750000,     4,      34,     21},
579         {219000000,     4,      47,     29},
580         {220000000,     4,      44,     27},
581         {220640000,     4,      49,     30},
582         {220750000,     4,      36,     22},
583         {221000000,     4,      36,     22},
584         {222000000,     4,      23,     14},
585         {222525000,     4,      150,    91},
586         {222750000,     4,      33,     20},
587         {227000000,     4,      37,     22},
588         {230250000,     4,      29,     17},
589         {233500000,     4,      38,     22},
590         {235000000,     4,      40,     23},
591         {238000000,     4,      30,     17},
592         {241500000,     2,      17,     19},
593         {245250000,     2,      20,     22},
594         {247750000,     2,      22,     24},
595         {253250000,     2,      15,     16},
596         {256250000,     2,      18,     19},
597         {262500000,     2,      31,     32},
598         {267250000,     2,      66,     67},
599         {268500000,     2,      94,     95},
600         {270000000,     2,      14,     14},
601         {272500000,     2,      77,     76},
602         {273750000,     2,      57,     56},
603         {280750000,     2,      24,     23},
604         {281250000,     2,      23,     22},
605         {286000000,     2,      17,     16},
606         {291750000,     2,      26,     24},
607         {296703000,     2,      100,    91},
608         {297000000,     2,      22,     20},
609         {298000000,     2,      21,     19},
610 };
611
612 int main(void)
613 {
614         int i;
615
616         for (i = 0; i < ARRAY_SIZE(wrpll_tmds_clock_table); i++) {
617                 const struct wrpll_tmds_clock *ref = &wrpll_tmds_clock_table[i];
618                 unsigned r2, n2, p;
619
620                 wrpll_compute_rnp(ref->clock, &r2, &n2, &p);
621                 igt_fail_on_f(ref->r2 != r2 || ref->n2 != n2 || ref->p != p,
622                               "Computed value differs for %li Hz:\n""  Reference: (%u,%u,%u)\n""  Computed:  (%u,%u,%u)\n", (int64_t)ref->clock * 1000, ref->r2, ref->n2, ref->p, r2, n2, p);
623         }
624
625         return 0;
626 }