drm/nouveau/pmu: prevent falcon from acking interrupts routed to the host
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / powerplay / hwmgr / ppevvmath.h
1 /*
2  * Copyright 2015 Advanced Micro Devices, 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  */
23 #include <asm/div64.h>
24
25 #define SHIFT_AMOUNT 16 /* We multiply all original integers with 2^SHIFT_AMOUNT to get the fInt representation */
26
27 #define PRECISION 5 /* Change this value to change the number of decimal places in the final output - 5 is a good default */
28
29 #define SHIFTED_2 (2 << SHIFT_AMOUNT)
30 #define MAX (1 << (SHIFT_AMOUNT - 1)) - 1 /* 32767 - Might change in the future */
31
32 /* -------------------------------------------------------------------------------
33  * NEW TYPE - fINT
34  * -------------------------------------------------------------------------------
35  * A variable of type fInt can be accessed in 3 ways using the dot (.) operator
36  * fInt A;
37  * A.full => The full number as it is. Generally not easy to read
38  * A.partial.real => Only the integer portion
39  * A.partial.decimal => Only the fractional portion
40  */
41 typedef union _fInt {
42     int full;
43     struct _partial {
44         unsigned int decimal: SHIFT_AMOUNT; /*Needs to always be unsigned*/
45         int real: 32 - SHIFT_AMOUNT;
46     } partial;
47 } fInt;
48
49 /* -------------------------------------------------------------------------------
50  * Function Declarations
51  *  -------------------------------------------------------------------------------
52  */
53 fInt ConvertToFraction(int);                       /* Use this to convert an INT to a FINT */
54 fInt Convert_ULONG_ToFraction(uint32_t);              /* Use this to convert an uint32_t to a FINT */
55 fInt GetScaledFraction(int, int);                  /* Use this to convert an INT to a FINT after scaling it by a factor */
56 int ConvertBackToInteger(fInt);                    /* Convert a FINT back to an INT that is scaled by 1000 (i.e. last 3 digits are the decimal digits) */
57
58 fInt fNegate(fInt);                                /* Returns -1 * input fInt value */
59 fInt fAdd (fInt, fInt);                            /* Returns the sum of two fInt numbers */
60 fInt fSubtract (fInt A, fInt B);                   /* Returns A-B - Sometimes easier than Adding negative numbers */
61 fInt fMultiply (fInt, fInt);                       /* Returns the product of two fInt numbers */
62 fInt fDivide (fInt A, fInt B);                     /* Returns A/B */
63 fInt fGetSquare(fInt);                             /* Returns the square of a fInt number */
64 fInt fSqrt(fInt);                                  /* Returns the Square Root of a fInt number */
65
66 int uAbs(int);                                     /* Returns the Absolute value of the Int */
67 fInt fAbs(fInt);                                   /* Returns the Absolute value of the fInt */
68 int uPow(int base, int exponent);                  /* Returns base^exponent an INT */
69
70 void SolveQuadracticEqn(fInt, fInt, fInt, fInt[]); /* Returns the 2 roots via the array */
71 bool Equal(fInt, fInt);                         /* Returns true if two fInts are equal to each other */
72 bool GreaterThan(fInt A, fInt B);               /* Returns true if A > B */
73
74 fInt fExponential(fInt exponent);                  /* Can be used to calculate e^exponent */
75 fInt fNaturalLog(fInt value);                      /* Can be used to calculate ln(value) */
76
77 /* Fuse decoding functions
78  * -------------------------------------------------------------------------------------
79  */
80 fInt fDecodeLinearFuse(uint32_t fuse_value, fInt f_min, fInt f_range, uint32_t bitlength);
81 fInt fDecodeLogisticFuse(uint32_t fuse_value, fInt f_average, fInt f_range, uint32_t bitlength);
82 fInt fDecodeLeakageID (uint32_t leakageID_fuse, fInt ln_max_div_min, fInt f_min, uint32_t bitlength);
83
84 /* Internal Support Functions - Use these ONLY for testing or adding to internal functions
85  * -------------------------------------------------------------------------------------
86  * Some of the following functions take two INTs as their input - This is unsafe for a variety of reasons.
87  */
88 fInt Add (int, int);                               /* Add two INTs and return Sum as FINT */
89 fInt Multiply (int, int);                          /* Multiply two INTs and return Product as FINT */
90 fInt Divide (int, int);                            /* You get the idea... */
91 fInt fNegate(fInt);
92
93 int uGetScaledDecimal (fInt);                      /* Internal function */
94 int GetReal (fInt A);                              /* Internal function */
95
96 /* Future Additions and Incomplete Functions
97  * -------------------------------------------------------------------------------------
98  */
99 int GetRoundedValue(fInt);                         /* Incomplete function - Useful only when Precision is lacking */
100                                                    /* Let us say we have 2.126 but can only handle 2 decimal points. We could */
101                                                    /* either chop of 6 and keep 2.12 or use this function to get 2.13, which is more accurate */
102
103 /* -------------------------------------------------------------------------------------
104  * TROUBLESHOOTING INFORMATION
105  * -------------------------------------------------------------------------------------
106  * 1) ConvertToFraction - InputOutOfRangeException: Only accepts numbers smaller than MAX (default: 32767)
107  * 2) fAdd - OutputOutOfRangeException: Output bigger than MAX (default: 32767)
108  * 3) fMultiply - OutputOutOfRangeException:
109  * 4) fGetSquare - OutputOutOfRangeException:
110  * 5) fDivide - DivideByZeroException
111  * 6) fSqrt - NegativeSquareRootException: Input cannot be a negative number
112  */
113
114 /* -------------------------------------------------------------------------------------
115  * START OF CODE
116  * -------------------------------------------------------------------------------------
117  */
118 fInt fExponential(fInt exponent)        /*Can be used to calculate e^exponent*/
119 {
120     uint32_t i;
121     bool bNegated = false;
122
123     fInt fPositiveOne = ConvertToFraction(1);
124     fInt fZERO = ConvertToFraction(0);
125
126     fInt lower_bound = Divide(78, 10000);
127     fInt solution = fPositiveOne; /*Starting off with baseline of 1 */
128     fInt error_term;
129
130     uint32_t k_array[11] = {55452, 27726, 13863, 6931, 4055, 2231, 1178, 606, 308, 155, 78};
131     uint32_t expk_array[11] = {2560000, 160000, 40000, 20000, 15000, 12500, 11250, 10625, 10313, 10156, 10078};
132
133     if (GreaterThan(fZERO, exponent)) {
134         exponent = fNegate(exponent);
135         bNegated = true;
136     }
137
138     while (GreaterThan(exponent, lower_bound)) {
139         for (i = 0; i < 11; i++) {
140             if (GreaterThan(exponent, GetScaledFraction(k_array[i], 10000))) {
141                 exponent = fSubtract(exponent, GetScaledFraction(k_array[i], 10000));
142                 solution = fMultiply(solution, GetScaledFraction(expk_array[i], 10000));
143             }
144         }
145     }
146
147     error_term = fAdd(fPositiveOne, exponent);
148
149     solution = fMultiply(solution, error_term);
150
151     if (bNegated)
152         solution = fDivide(fPositiveOne, solution);
153
154     return solution;
155 }
156
157 fInt fNaturalLog(fInt value)
158 {
159     uint32_t i;
160     fInt upper_bound = Divide(8, 1000);
161     fInt fNegativeOne = ConvertToFraction(-1);
162     fInt solution = ConvertToFraction(0); /*Starting off with baseline of 0 */
163     fInt error_term;
164
165     uint32_t k_array[10] = {160000, 40000, 20000, 15000, 12500, 11250, 10625, 10313, 10156, 10078};
166     uint32_t logk_array[10] = {27726, 13863, 6931, 4055, 2231, 1178, 606, 308, 155, 78};
167
168     while (GreaterThan(fAdd(value, fNegativeOne), upper_bound)) {
169         for (i = 0; i < 10; i++) {
170             if (GreaterThan(value, GetScaledFraction(k_array[i], 10000))) {
171                 value = fDivide(value, GetScaledFraction(k_array[i], 10000));
172                 solution = fAdd(solution, GetScaledFraction(logk_array[i], 10000));
173             }
174         }
175     }
176
177     error_term = fAdd(fNegativeOne, value);
178
179     return (fAdd(solution, error_term));
180 }
181
182 fInt fDecodeLinearFuse(uint32_t fuse_value, fInt f_min, fInt f_range, uint32_t bitlength)
183 {
184     fInt f_fuse_value = Convert_ULONG_ToFraction(fuse_value);
185     fInt f_bit_max_value = Convert_ULONG_ToFraction((uPow(2, bitlength)) - 1);
186
187     fInt f_decoded_value;
188
189     f_decoded_value = fDivide(f_fuse_value, f_bit_max_value);
190     f_decoded_value = fMultiply(f_decoded_value, f_range);
191     f_decoded_value = fAdd(f_decoded_value, f_min);
192
193     return f_decoded_value;
194 }
195
196
197 fInt fDecodeLogisticFuse(uint32_t fuse_value, fInt f_average, fInt f_range, uint32_t bitlength)
198 {
199     fInt f_fuse_value = Convert_ULONG_ToFraction(fuse_value);
200     fInt f_bit_max_value = Convert_ULONG_ToFraction((uPow(2, bitlength)) - 1);
201
202     fInt f_CONSTANT_NEG13 = ConvertToFraction(-13);
203     fInt f_CONSTANT1 = ConvertToFraction(1);
204
205     fInt f_decoded_value;
206
207     f_decoded_value = fSubtract(fDivide(f_bit_max_value, f_fuse_value), f_CONSTANT1);
208     f_decoded_value = fNaturalLog(f_decoded_value);
209     f_decoded_value = fMultiply(f_decoded_value, fDivide(f_range, f_CONSTANT_NEG13));
210     f_decoded_value = fAdd(f_decoded_value, f_average);
211
212     return f_decoded_value;
213 }
214
215 fInt fDecodeLeakageID (uint32_t leakageID_fuse, fInt ln_max_div_min, fInt f_min, uint32_t bitlength)
216 {
217     fInt fLeakage;
218     fInt f_bit_max_value = Convert_ULONG_ToFraction((uPow(2, bitlength)) - 1);
219
220     fLeakage = fMultiply(ln_max_div_min, Convert_ULONG_ToFraction(leakageID_fuse));
221     fLeakage = fDivide(fLeakage, f_bit_max_value);
222     fLeakage = fExponential(fLeakage);
223     fLeakage = fMultiply(fLeakage, f_min);
224
225     return fLeakage;
226 }
227
228 fInt ConvertToFraction(int X) /*Add all range checking here. Is it possible to make fInt a private declaration? */
229 {
230     fInt temp;
231
232     if (X <= MAX)
233         temp.full = (X << SHIFT_AMOUNT);
234     else
235         temp.full = 0;
236
237     return temp;
238 }
239
240 fInt fNegate(fInt X)
241 {
242     fInt CONSTANT_NEGONE = ConvertToFraction(-1);
243     return (fMultiply(X, CONSTANT_NEGONE));
244 }
245
246 fInt Convert_ULONG_ToFraction(uint32_t X)
247 {
248     fInt temp;
249
250     if (X <= MAX)
251         temp.full = (X << SHIFT_AMOUNT);
252     else
253         temp.full = 0;
254
255     return temp;
256 }
257
258 fInt GetScaledFraction(int X, int factor)
259 {
260     int times_shifted, factor_shifted;
261     bool bNEGATED;
262     fInt fValue;
263
264     times_shifted = 0;
265     factor_shifted = 0;
266     bNEGATED = false;
267
268     if (X < 0) {
269         X = -1*X;
270         bNEGATED = true;
271     }
272
273     if (factor < 0) {
274         factor = -1*factor;
275
276         bNEGATED = !bNEGATED; /*If bNEGATED = true due to X < 0, this will cover the case of negative cancelling negative */
277     }
278
279     if ((X > MAX) || factor > MAX) {
280         if ((X/factor) <= MAX) {
281             while (X > MAX) {
282                 X = X >> 1;
283                 times_shifted++;
284             }
285
286             while (factor > MAX) {
287                 factor = factor >> 1;
288                 factor_shifted++;
289             }
290         } else {
291             fValue.full = 0;
292             return fValue;
293         }
294     }
295
296     if (factor == 1)
297         return (ConvertToFraction(X));
298
299     fValue = fDivide(ConvertToFraction(X * uPow(-1, bNEGATED)), ConvertToFraction(factor));
300
301     fValue.full = fValue.full << times_shifted;
302     fValue.full = fValue.full >> factor_shifted;
303
304     return fValue;
305 }
306
307 /* Addition using two fInts */
308 fInt fAdd (fInt X, fInt Y)
309 {
310     fInt Sum;
311
312     Sum.full = X.full + Y.full;
313
314     return Sum;
315 }
316
317 /* Addition using two fInts */
318 fInt fSubtract (fInt X, fInt Y)
319 {
320     fInt Difference;
321
322     Difference.full = X.full - Y.full;
323
324     return Difference;
325 }
326
327 bool Equal(fInt A, fInt B)
328 {
329     if (A.full == B.full)
330         return true;
331     else
332         return false;
333 }
334
335 bool GreaterThan(fInt A, fInt B)
336 {
337     if (A.full > B.full)
338         return true;
339     else
340         return false;
341 }
342
343 fInt fMultiply (fInt X, fInt Y) /* Uses 64-bit integers (int64_t) */
344 {
345     fInt Product;
346     int64_t tempProduct;
347     bool X_LessThanOne, Y_LessThanOne;
348
349     X_LessThanOne = (X.partial.real == 0 && X.partial.decimal != 0 && X.full >= 0);
350     Y_LessThanOne = (Y.partial.real == 0 && Y.partial.decimal != 0 && Y.full >= 0);
351
352     /*The following is for a very specific common case: Non-zero number with ONLY fractional portion*/
353     /* TEMPORARILY DISABLED - CAN BE USED TO IMPROVE PRECISION
354
355     if (X_LessThanOne && Y_LessThanOne) {
356         Product.full = X.full * Y.full;
357         return Product
358     }*/
359
360     tempProduct = ((int64_t)X.full) * ((int64_t)Y.full); /*Q(16,16)*Q(16,16) = Q(32, 32) - Might become a negative number! */
361     tempProduct = tempProduct >> 16; /*Remove lagging 16 bits - Will lose some precision from decimal; */
362     Product.full = (int)tempProduct; /*The int64_t will lose the leading 16 bits that were part of the integer portion */
363
364     return Product;
365 }
366
367 fInt fDivide (fInt X, fInt Y)
368 {
369     fInt fZERO, fQuotient;
370     int64_t longlongX, longlongY;
371
372     fZERO = ConvertToFraction(0);
373
374     if (Equal(Y, fZERO))
375         return fZERO;
376
377     longlongX = (int64_t)X.full;
378     longlongY = (int64_t)Y.full;
379
380     longlongX = longlongX << 16; /*Q(16,16) -> Q(32,32) */
381
382     do_div(longlongX, longlongY); /*Q(32,32) divided by Q(16,16) = Q(16,16) Back to original format */
383
384     fQuotient.full = (int)longlongX;
385     return fQuotient;
386 }
387
388 int ConvertBackToInteger (fInt A) /*THIS is the function that will be used to check with the Golden settings table*/
389 {
390     fInt fullNumber, scaledDecimal, scaledReal;
391
392     scaledReal.full = GetReal(A) * uPow(10, PRECISION-1); /* DOUBLE CHECK THISSSS!!! */
393
394     scaledDecimal.full = uGetScaledDecimal(A);
395
396     fullNumber = fAdd(scaledDecimal,scaledReal);
397
398     return fullNumber.full;
399 }
400
401 fInt fGetSquare(fInt A)
402 {
403     return fMultiply(A,A);
404 }
405
406 /* x_new = x_old - (x_old^2 - C) / (2 * x_old) */
407 fInt fSqrt(fInt num)
408 {
409     fInt F_divide_Fprime, Fprime;
410     fInt test;
411     fInt twoShifted;
412     int seed, counter, error;
413     fInt x_new, x_old, C, y;
414
415     fInt fZERO = ConvertToFraction(0);
416     /* (0 > num) is the same as (num < 0), i.e., num is negative */
417     if (GreaterThan(fZERO, num) || Equal(fZERO, num))
418         return fZERO;
419
420     C = num;
421
422     if (num.partial.real > 3000)
423         seed = 60;
424     else if (num.partial.real > 1000)
425         seed = 30;
426     else if (num.partial.real > 100)
427         seed = 10;
428     else
429         seed = 2;
430
431     counter = 0;
432
433     if (Equal(num, fZERO)) /*Square Root of Zero is zero */
434         return fZERO;
435
436     twoShifted = ConvertToFraction(2);
437     x_new = ConvertToFraction(seed);
438
439     do {
440         counter++;
441
442         x_old.full = x_new.full;
443
444         test = fGetSquare(x_old); /*1.75*1.75 is reverting back to 1 when shifted down */
445         y = fSubtract(test, C); /*y = f(x) = x^2 - C; */
446
447         Fprime = fMultiply(twoShifted, x_old);
448         F_divide_Fprime = fDivide(y, Fprime);
449
450         x_new = fSubtract(x_old, F_divide_Fprime);
451
452         error = ConvertBackToInteger(x_new) - ConvertBackToInteger(x_old);
453
454         if (counter > 20) /*20 is already way too many iterations. If we dont have an answer by then, we never will*/
455             return x_new;
456
457     } while (uAbs(error) > 0);
458
459     return (x_new);
460 }
461
462 void SolveQuadracticEqn(fInt A, fInt B, fInt C, fInt Roots[])
463 {
464     fInt* pRoots = &Roots[0];
465     fInt temp, root_first, root_second;
466     fInt f_CONSTANT10, f_CONSTANT100;
467
468     f_CONSTANT100 = ConvertToFraction(100);
469     f_CONSTANT10 = ConvertToFraction(10);
470
471     while(GreaterThan(A, f_CONSTANT100) || GreaterThan(B, f_CONSTANT100) || GreaterThan(C, f_CONSTANT100)) {
472         A = fDivide(A, f_CONSTANT10);
473         B = fDivide(B, f_CONSTANT10);
474         C = fDivide(C, f_CONSTANT10);
475     }
476
477     temp = fMultiply(ConvertToFraction(4), A); /* root = 4*A */
478     temp = fMultiply(temp, C); /* root = 4*A*C */
479     temp = fSubtract(fGetSquare(B), temp); /* root = b^2 - 4AC */
480     temp = fSqrt(temp); /*root = Sqrt (b^2 - 4AC); */
481
482     root_first = fSubtract(fNegate(B), temp); /* b - Sqrt(b^2 - 4AC) */
483     root_second = fAdd(fNegate(B), temp); /* b + Sqrt(b^2 - 4AC) */
484
485     root_first = fDivide(root_first, ConvertToFraction(2)); /* [b +- Sqrt(b^2 - 4AC)]/[2] */
486     root_first = fDivide(root_first, A); /*[b +- Sqrt(b^2 - 4AC)]/[2*A] */
487
488     root_second = fDivide(root_second, ConvertToFraction(2)); /* [b +- Sqrt(b^2 - 4AC)]/[2] */
489     root_second = fDivide(root_second, A); /*[b +- Sqrt(b^2 - 4AC)]/[2*A] */
490
491     *(pRoots + 0) = root_first;
492     *(pRoots + 1) = root_second;
493 }
494
495 /* -----------------------------------------------------------------------------
496  * SUPPORT FUNCTIONS
497  * -----------------------------------------------------------------------------
498  */
499
500 /* Addition using two normal ints - Temporary - Use only for testing purposes?. */
501 fInt Add (int X, int Y)
502 {
503     fInt A, B, Sum;
504
505     A.full = (X << SHIFT_AMOUNT);
506     B.full = (Y << SHIFT_AMOUNT);
507
508     Sum.full = A.full + B.full;
509
510     return Sum;
511 }
512
513 /* Conversion Functions */
514 int GetReal (fInt A)
515 {
516     return (A.full >> SHIFT_AMOUNT);
517 }
518
519 /* Temporarily Disabled */
520 int GetRoundedValue(fInt A) /*For now, round the 3rd decimal place */
521 {
522     /* ROUNDING TEMPORARLY DISABLED
523     int temp = A.full;
524
525     int decimal_cutoff, decimal_mask = 0x000001FF;
526
527     decimal_cutoff = temp & decimal_mask;
528
529
530     if (decimal_cutoff > 0x147) {
531         temp += 673;
532     }*/
533
534     return ConvertBackToInteger(A)/10000; /*Temporary - in case this was used somewhere else */
535 }
536
537 fInt Multiply (int X, int Y)
538 {
539     fInt A, B, Product;
540
541     A.full = X << SHIFT_AMOUNT;
542     B.full = Y << SHIFT_AMOUNT;
543
544     Product = fMultiply(A, B);
545
546     return Product;
547 }
548 fInt Divide (int X, int Y)
549 {
550     fInt A, B, Quotient;
551
552     A.full = X << SHIFT_AMOUNT;
553     B.full = Y << SHIFT_AMOUNT;
554
555     Quotient = fDivide(A, B);
556
557     return Quotient;
558 }
559
560 int uGetScaledDecimal (fInt A) /*Converts the fractional portion to whole integers - Costly function */
561 {
562         int dec[PRECISION];
563         int i, scaledDecimal = 0, tmp = A.partial.decimal;
564
565         for (i = 0; i < PRECISION; i++) {
566         dec[i] = tmp / (1 << SHIFT_AMOUNT);
567
568         tmp = tmp - ((1 << SHIFT_AMOUNT)*dec[i]);
569
570         tmp *= 10;
571
572         scaledDecimal = scaledDecimal + dec[i]*uPow(10, PRECISION - 1 -i);
573     }
574
575     return scaledDecimal;
576 }
577
578 int uPow(int base, int power)
579 {
580         if (power == 0)
581                 return 1;
582         else
583                 return (base)*uPow(base, power - 1);
584 }
585
586 fInt fAbs(fInt A)
587 {
588         if (A.partial.real < 0)
589                 return (fMultiply(A, ConvertToFraction(-1)));
590         else
591                 return A;
592 }
593
594 int uAbs(int X)
595 {
596         if (X < 0)
597                 return (X * -1);
598         else
599                 return X;
600 }
601
602 fInt fRoundUpByStepSize(fInt A, fInt fStepSize, bool error_term)
603 {
604     fInt solution;
605
606     solution = fDivide(A, fStepSize);
607     solution.partial.decimal = 0; /*All fractional digits changes to 0 */
608
609     if (error_term)
610         solution.partial.real += 1; /*Error term of 1 added */
611
612     solution = fMultiply(solution, fStepSize);
613     solution = fAdd(solution, fStepSize);
614
615     return solution;
616 }
617