1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* CPPC (Collaborative Processor Performance Control) methods used by CPUfreq drivers.
*
* (C) Copyright 2014, 2015 Linaro Ltd.
* Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
*
* CPPC describes a few methods for controlling CPU performance using
* information from a per CPU table called CPC. This table is described in
* the ACPI v5.0+ specification. The table consists of a list of
* registers which may be memory mapped or hardware registers and also may
* include some static integer values.
*
* CPU performance is on an abstract continuous scale as against a discretized
* P-state scale which is tied to CPU frequency only. In brief, the basic
* operation involves:
*
* - OS makes a CPU performance request. (Can provide min and max bounds)
*
* - Platform (such as BMC) is free to optimize request within requested bounds
* depending on power/thermal budgets etc.
*
* - Platform conveys its decision back to OS
*
* The communication between OS and platform occurs through another medium
* called (PCC) Platform Communication Channel. This is a generic mailbox like
* mechanism which includes doorbell semantics to indicate register updates.
* See drivers/mailbox/pcc.c for details on PCC.
*
* Finer details about the PCC and CPPC spec are available in the ACPI v5.1 and
* above specifications.
*/
#define pr_fmt(fmt) "ACPI CPPC: " fmt
#include <linux/delay.h>
#include <linux/interval_tree_generic.h>
#include <linux/iopoll.h>
#include <linux/ktime.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/rbtree.h>
#include <linux/rwsem.h>
#include <linux/wait.h>
#include <linux/topology.h>
#include <linux/dmi.h>
#include <linux/units.h>
#include <linux/unaligned.h>
#include <acpi/cppc_acpi.h>
struct cppc_pcc_data {
struct pcc_mbox_chan *pcc_channel;
bool pcc_channel_acquired;
unsigned int deadline_us;
unsigned int pcc_mpar, pcc_mrtt, pcc_nominal;
bool pending_pcc_write_cmd; /* Any pending/batched PCC write cmds? */
bool platform_owns_pcc; /* Ownership of PCC subspace */
unsigned int pcc_write_cnt; /* Running count of PCC write commands */
/*
* Lock to provide controlled access to the PCC channel.
*
* For performance critical usecases(currently cppc_set_perf)
* We need to take read_lock and check if channel belongs to OSPM
* before reading or writing to PCC subspace
* We need to take write_lock before transferring the channel
* ownership to the platform via a Doorbell
* This allows us to batch a number of CPPC requests if they happen
* to originate in about the same time
*
* For non-performance critical usecases(init)
* Take write_lock for all purposes which gives exclusive access
*/
struct rw_semaphore pcc_lock;
/* Serialize byte-oriented accesses to aliased PCC payload fields. */
raw_spinlock_t payload_lock;
/* Wait queue for CPUs whose requests were batched */
wait_queue_head_t pcc_write_wait_q;
ktime_t last_cmd_cmpl_time;
ktime_t last_mpar_reset;
int mpar_count;
int refcount;
};
/* Array to represent the PCC channel per subspace ID */
static struct cppc_pcc_data *pcc_data[MAX_PCC_SUBSPACES];
static DEFINE_MUTEX(pcc_data_lock);
/* The cpu_pcc_subspace_idx contains per CPU subspace ID */
static DEFINE_PER_CPU(int, cpu_pcc_subspace_idx);
/*
* The cpc_desc structure contains the ACPI register details
* as described in the per CPU _CPC tables. The details
* include the type of register (e.g. PCC, System IO, FFH etc.)
* and destination addresses which lets us READ/WRITE CPU performance
* information using the appropriate I/O methods.
*/
static DEFINE_PER_CPU(struct cpc_desc *, cpc_desc_ptr);
/* Protect immutable capability queries against descriptor removal. */
static DEFINE_MUTEX(cpc_desc_lock);
static void cpc_set_desc(unsigned int cpu, struct cpc_desc *desc)
{
guard(mutex)(&cpc_desc_lock);
per_cpu(cpc_desc_ptr, cpu) = desc;
}
struct cpc_sysmem_node {
struct rb_node rb;
u64 subtree_last;
u64 start;
u64 last;
struct cpc_desc *desc;
unsigned int reg_idx;
struct list_head aliases;
struct list_head alias_node;
struct cpc_sysmem_node *alias_of;
bool registered;
};
struct cpc_non_mmio_node {
struct rb_node rb;
u64 subtree_last;
u64 start;
u64 last;
struct cpc_desc *desc;
unsigned int reg_idx;
u8 space_id;
u8 pcc_ss_id;
bool registered;
};
#define CPC_SYSMEM_START(node) ((node)->start)
#define CPC_SYSMEM_LAST(node) ((node)->last)
INTERVAL_TREE_DEFINE(struct cpc_sysmem_node, rb, u64, subtree_last,
CPC_SYSMEM_START, CPC_SYSMEM_LAST, static inline,
cpc_sysmem_itree)
static struct rb_root_cached cpc_sysmem_tree = RB_ROOT_CACHED;
static DEFINE_MUTEX(cpc_sysmem_lock);
#define CPC_NON_MMIO_START(node) ((node)->start)
#define CPC_NON_MMIO_LAST(node) ((node)->last)
INTERVAL_TREE_DEFINE(struct cpc_non_mmio_node, rb, u64, subtree_last,
CPC_NON_MMIO_START, CPC_NON_MMIO_LAST, static inline,
cpc_non_mmio_itree)
static struct rb_root_cached cpc_pcc_trees[MAX_PCC_SUBSPACES];
static struct rb_root_cached cpc_sysio_tree = RB_ROOT_CACHED;
static DEFINE_MUTEX(cpc_non_mmio_lock);
static struct cpc_sysmem_node *cpc_sysmem_first(u64 start, u64 last)
{
return cpc_sysmem_itree_iter_first(&cpc_sysmem_tree, start, last);
}
static struct cpc_sysmem_node *cpc_sysmem_next(struct cpc_sysmem_node *node,
u64 start, u64 last)
{
return cpc_sysmem_itree_iter_next(node, start, last);
}
#define CPC_PCC_HEADER_SIZE 0x8
/* pcc mapped address + header size + offset within PCC subspace */
#define GET_PCC_VADDR(offs, pcc_ss_id) (pcc_data[pcc_ss_id]->pcc_channel->shmem + \
CPC_PCC_HEADER_SIZE + (offs))
/* Check if a CPC register is in PCC */
#define CPC_IN_PCC(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
(cpc)->cpc_entry.reg.space_id == \
ACPI_ADR_SPACE_PLATFORM_COMM)
/* Check if a CPC register is in FFH */
#define CPC_IN_FFH(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
(cpc)->cpc_entry.reg.space_id == \
ACPI_ADR_SPACE_FIXED_HARDWARE)
/* Check if a CPC register is in SystemMemory */
#define CPC_IN_SYSTEM_MEMORY(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
(cpc)->cpc_entry.reg.space_id == \
ACPI_ADR_SPACE_SYSTEM_MEMORY)
/* Check if a CPC register is in SystemIo */
#define CPC_IN_SYSTEM_IO(cpc) ((cpc)->type == ACPI_TYPE_BUFFER && \
(cpc)->cpc_entry.reg.space_id == \
ACPI_ADR_SPACE_SYSTEM_IO)
/* Evaluates to True if reg is a NULL register descriptor */
#define IS_NULL_REG(reg) ((reg)->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY && \
(reg)->address == 0 && \
(reg)->bit_width == 0 && \
(reg)->bit_offset == 0 && \
(reg)->access_width == 0)
/* Evaluates to True if an optional cpc field is supported */
#define CPC_SUPPORTED(cpc) ((cpc)->type == ACPI_TYPE_INTEGER ? \
!!(cpc)->cpc_entry.int_value : \
!IS_NULL_REG(&(cpc)->cpc_entry.reg))
static bool cpc_is_writable(const struct cpc_register_resource *cpc)
{
return cpc->type == ACPI_TYPE_BUFFER &&
!IS_NULL_REG(&cpc->cpc_entry.reg) &&
!cpc->cpc_entry.write_unsupported;
}
static bool cpc_is_readable(const struct cpc_register_resource *cpc)
{
return cpc->type != ACPI_TYPE_BUFFER ||
!cpc->cpc_entry.read_unsupported;
}
static bool cpc_entry_present(const struct cpc_register_resource *cpc)
{
if (cpc->type == ACPI_TYPE_INTEGER)
return true;
return cpc->type == ACPI_TYPE_BUFFER &&
!IS_NULL_REG(&cpc->cpc_entry.reg);
}
/*
* Each bit indicates the optionality of the register in per-cpu
* cpc_regs[] with the corresponding index. 0 means mandatory and 1
* means optional.
*/
#define REG_OPTIONAL (0x7FC7D0)
/*
* Use the index of the register in per-cpu cpc_regs[] to check if
* it's an optional one.
*/
#define IS_OPTIONAL_CPC_REG(reg_idx) (REG_OPTIONAL & (1U << (reg_idx)))
static bool cpc_integer_entry_valid(unsigned int reg_idx, u64 value,
bool *legacy_null)
{
*legacy_null = false;
switch (reg_idx) {
case HIGHEST_PERF:
case NOMINAL_PERF:
case LOW_NON_LINEAR_PERF:
case LOWEST_PERF:
case REFERENCE_PERF:
case LOWEST_FREQ:
case NOMINAL_FREQ:
return value <= U32_MAX;
case CTR_WRAP_TIME:
/* AML Integers and the kernel interface are both 64-bit. */
return true;
case AUTO_SEL_ENABLE:
return value <= 1;
case DESIRED_PERF:
/* Validated against Autonomous Selection after parsing. */
*legacy_null = value == 0;
return *legacy_null;
default:
/* Tolerate legacy Integer 0 placeholders for absent options. */
*legacy_null = value == 0 && IS_OPTIONAL_CPC_REG(reg_idx);
return *legacy_null;
}
}
/*
* Arbitrary Retries in case the remote processor is slow to respond
* to PCC commands. Keeping it high enough to cover emulators where
* the processors run painfully slow.
*/
#define NUM_RETRIES 500ULL
#define CPC_GENERIC_REGISTER_DESCRIPTOR 0x82
#define CPC_GENERIC_REGISTER_LENGTH (sizeof(struct cpc_reg) - 3)
#define define_one_cppc_ro(_name) \
static struct kobj_attribute _name = \
__ATTR(_name, 0444, show_##_name, NULL)
#define to_cpc_desc(a) container_of(a, struct cpc_desc, kobj)
#define show_cppc_data(access_fn, struct_name, member_name) \
static ssize_t show_##member_name(struct kobject *kobj, \
struct kobj_attribute *attr, char *buf) \
{ \
struct cpc_desc *cpc_ptr = to_cpc_desc(kobj); \
struct struct_name st_name = {0}; \
int ret; \
\
ret = access_fn(cpc_ptr->cpu_id, &st_name); \
if (ret) \
return ret; \
\
return sysfs_emit(buf, "%llu\n", \
(u64)st_name.member_name); \
} \
define_one_cppc_ro(member_name)
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, highest_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, reference_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_nonlinear_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, guaranteed_perf);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, lowest_freq);
show_cppc_data(cppc_get_perf_caps, cppc_perf_caps, nominal_freq);
show_cppc_data(cppc_get_perf_ctrs, cppc_perf_fb_ctrs, wraparound_time);
/*
* PCC reuses the access_width field as the subspace id, so only decode access
* size for non-PCC registers. Otherwise, use the bit_width.
*/
#define GET_BIT_WIDTH(reg) (((reg)->access_width && \
(reg)->space_id != ACPI_ADR_SPACE_PLATFORM_COMM) ? \
(8 << ((reg)->access_width - 1)) : (reg)->bit_width)
/* Shift and apply the mask for CPC reads/writes */
#define MASK_VAL_READ(reg, val) (((val) >> (reg)->bit_offset) & \
GENMASK(((reg)->bit_width) - 1, 0))
#define MASK_VAL_WRITE(reg, prev_val, val) \
((((val) & GENMASK(((reg)->bit_width) - 1, 0)) << (reg)->bit_offset) | \
((prev_val) & ~(GENMASK(((reg)->bit_width) - 1, 0) << (reg)->bit_offset))) \
static unsigned int cpc_reg_access_width(const struct cpc_reg *reg)
{
if (reg->access_width > 4)
return 0;
if (reg->access_width)
return 8U << (reg->access_width - 1);
return reg->bit_width;
}
enum cpc_platform_quirk {
CPC_QUIRK_PERF_LIMITED_OWNS_UNIT = BIT(0),
};
static const struct acpi_platform_list cpc_platform_quirk_list[] = {
{
.oem_id = "NVIDIA",
.oem_table_id = "T41",
.table = ACPI_SIG_DSDT,
.pred = all_versions,
.reason = "Performance Limited owns its access unit",
.data = CPC_QUIRK_PERF_LIMITED_OWNS_UNIT,
},
{ }
};
static DEFINE_MUTEX(cpc_platform_quirk_lock);
static bool cpc_platform_quirks_initialized;
static u32 cpc_platform_quirks;
static int cpc_get_platform_quirks(u32 *quirks)
{
int idx, ret = 0;
mutex_lock(&cpc_platform_quirk_lock);
if (!cpc_platform_quirks_initialized) {
idx = acpi_match_platform_list(cpc_platform_quirk_list);
if (idx < 0 && idx != -ENODEV) {
ret = idx;
goto out;
}
if (idx >= 0)
cpc_platform_quirks = cpc_platform_quirk_list[idx].data;
cpc_platform_quirks_initialized = true;
}
*quirks = cpc_platform_quirks;
out:
mutex_unlock(&cpc_platform_quirk_lock);
return ret;
}
static void cpc_apply_platform_quirks(struct cpc_reg *reg,
unsigned int reg_idx, u32 quirks)
{
unsigned int access_width;
if (!(quirks & CPC_QUIRK_PERF_LIMITED_OWNS_UNIT) ||
reg_idx != PERF_LIMITED ||
reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY ||
reg->bit_width != 2 || reg->bit_offset)
return;
access_width = cpc_reg_access_width(reg);
if (access_width != 32)
return;
reg->bit_width = access_width;
pr_info_once("firmware quirk: Performance Limited owns its access unit, using Bit Width %u\n",
access_width);
}
static u64 cpc_sysmem_access_size(const struct cpc_register_resource *reg)
{
unsigned int width = cpc_reg_access_width(®->cpc_entry.reg);
if (width != 8 && width != 16 && width != 32 && width != 64)
return 0;
return width / 8;
}
static u64 cpc_sysmem_field_size(const struct cpc_reg *gas)
{
return DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8);
}
static u64 cpc_sysmem_claim_size(const struct cpc_register_resource *reg)
{
const struct cpc_reg *gas = ®->cpc_entry.reg;
u64 access_size = cpc_sysmem_access_size(reg);
if (!gas->bit_width)
return access_size;
return max(access_size, cpc_sysmem_field_size(gas));
}
static bool cpc_reg_access_aligned(const struct cpc_reg *reg, u64 access_size)
{
/* x86 MMIO and port-I/O accessors support unaligned addresses. */
return IS_ENABLED(CONFIG_X86) || IS_ALIGNED(reg->address, access_size);
}
static bool cpc_sysmem_access_units_overlap(const struct cpc_register_resource *a,
const struct cpc_register_resource *b)
{
const struct cpc_reg *a_gas = &a->cpc_entry.reg;
const struct cpc_reg *b_gas = &b->cpc_entry.reg;
u64 a_size = cpc_sysmem_claim_size(a);
u64 b_size = cpc_sysmem_claim_size(b);
/* Keep the conservative locking path for malformed access widths. */
if (!a_size || !b_size)
return true;
if (a_gas->address < b_gas->address)
return b_gas->address - a_gas->address < a_size;
return a_gas->address - b_gas->address < b_size;
}
static bool cpc_reg_is_writable(unsigned int reg_idx)
{
/* Only controls written by this driver can be competing writers. */
switch (reg_idx) {
case DESIRED_PERF:
case MIN_PERF:
case MAX_PERF:
case PERF_LIMITED:
case ENABLE:
case AUTO_SEL_ENABLE:
case AUTO_ACT_WINDOW:
case ENERGY_PERF:
return true;
default:
return false;
}
}
static bool cpc_reg_is_write_only(const struct cpc_desc *cpc_desc,
unsigned int reg_idx)
{
return cpc_desc->version >= CPPC_V4_REV &&
(reg_idx == DESIRED_PERF || reg_idx == OSPM_NOMINAL_PERF);
}
static void cpc_disable_reg(struct cpc_desc *cpc_desc, unsigned int reg_idx)
{
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx];
reg->type = ACPI_TYPE_INTEGER;
reg->cpc_entry.int_value = 0;
}
static bool cpc_optional_writer_can_be_disabled(unsigned int reg_idx)
{
if (!IS_OPTIONAL_CPC_REG(reg_idx) || !cpc_reg_is_writable(reg_idx) ||
reg_idx == MIN_PERF || reg_idx == MAX_PERF || reg_idx == ENABLE ||
reg_idx == AUTO_SEL_ENABLE)
return false;
return true;
}
static bool cpc_sysmem_reg_needs_rmw(const struct cpc_register_resource *reg)
{
const struct cpc_reg *gas = ®->cpc_entry.reg;
u64 access_size = cpc_sysmem_access_size(reg);
return gas->bit_offset || gas->bit_width != access_size * 8;
}
static int cpc_validate_sysmem_reg(struct cpc_desc *cpc_desc,
const struct cpc_reg *gas,
unsigned int reg_idx)
{
unsigned int access_width = cpc_reg_access_width(gas);
u64 access_size;
if (access_width != 8 && access_width != 16 &&
access_width != 32 && access_width != 64)
goto invalid;
if (!gas->bit_width || gas->bit_width > access_width ||
gas->bit_offset >= access_width ||
gas->bit_width > access_width - gas->bit_offset)
goto invalid;
access_size = access_width / 8;
if (!gas->address || gas->address > U64_MAX - (access_size - 1))
goto invalid;
if (!cpc_reg_access_aligned(gas, access_size))
goto invalid;
if (reg_idx == PERF_LIMITED) {
if (access_width == 64 && !IS_ENABLED(CONFIG_64BIT)) {
pr_warn_once("CPU%d: Performance Limited register cannot be accessed atomically; keeping its range reserved\n",
cpc_desc->cpu_id);
cpc_desc->cpc_regs[reg_idx].cpc_entry.read_unsupported = true;
cpc_desc->cpc_regs[reg_idx].cpc_entry.write_unsupported = true;
return 0;
}
if (gas->bit_offset || gas->bit_width != access_width) {
pr_warn_once("CPU%d: Performance Limited register cannot be cleared safely; keeping it readable\n",
cpc_desc->cpu_id);
cpc_desc->cpc_regs[reg_idx].cpc_entry.write_unsupported = true;
}
}
return 0;
invalid:
access_size = 0;
if (access_width == 8 || access_width == 16 ||
access_width == 32 || access_width == 64)
access_size = access_width / 8;
if (gas->bit_width)
access_size = max(access_size, cpc_sysmem_field_size(gas));
if ((cpc_reg_is_write_only(cpc_desc, reg_idx) ||
reg_idx == PERF_LIMITED) && gas->address && access_size &&
gas->address <= U64_MAX - (access_size - 1)) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx];
if (reg_idx == PERF_LIMITED)
pr_warn_once("CPU%d: _CPC v%d register %u is inaccessible; keeping its range reserved\n",
cpc_desc->cpu_id, cpc_desc->version, reg_idx);
else
pr_warn("CPU%d: _CPC v%d register %u is inaccessible; keeping its range reserved\n",
cpc_desc->cpu_id, cpc_desc->version, reg_idx);
reg->cpc_entry.read_unsupported = true;
reg->cpc_entry.write_unsupported = true;
return 0;
}
pr_debug("CPU:%d invalid SystemMemory GAS for _CPC register %u\n",
cpc_desc->cpu_id, reg_idx);
return -EINVAL;
}
static bool cpc_immutable_autonomous(const struct cpc_desc *cpc_desc)
{
const struct cpc_register_resource *reg;
reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
return osc_sb_cppc2_support_acked && reg->type == ACPI_TYPE_INTEGER &&
reg->cpc_entry.int_value == 1;
}
static bool cpc_retain_pcc_status(struct cpc_desc *cpc_desc,
unsigned int reg_idx);
static int cpc_resolve_unsupported(struct cpc_desc *cpc_desc,
u32 unsupported)
{
unsigned int i;
u32 bounds = BIT(MIN_PERF) | BIT(MAX_PERF);
bool min_unusable, max_unusable;
if (unsupported & bounds) {
min_unusable = (unsupported & BIT(MIN_PERF)) ||
!cpc_is_writable(&cpc_desc->cpc_regs[MIN_PERF]);
max_unusable = (unsupported & BIT(MAX_PERF)) ||
!cpc_is_writable(&cpc_desc->cpc_regs[MAX_PERF]);
if (min_unusable && max_unusable) {
pr_warn("CPU%d: ignoring inaccessible Minimum and Maximum Performance registers\n",
cpc_desc->cpu_id);
cpc_disable_reg(cpc_desc, MIN_PERF);
cpc_disable_reg(cpc_desc, MAX_PERF);
unsupported &= ~bounds;
}
}
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
if (!(unsupported & BIT(i)))
continue;
/* CPPC control does not depend on Performance Limited status. */
if (i == PERF_LIMITED) {
if (CPC_IN_PCC(&cpc_desc->cpc_regs[i]) &&
cpc_retain_pcc_status(cpc_desc, i))
continue;
pr_warn_once("CPU%d: ignoring inaccessible Performance Limited register\n",
cpc_desc->cpu_id);
cpc_disable_reg(cpc_desc, i);
continue;
}
if (i == DESIRED_PERF && cpc_immutable_autonomous(cpc_desc)) {
pr_warn("CPU%d: ignoring inaccessible Desired Performance register in autonomous mode\n",
cpc_desc->cpu_id);
cpc_disable_reg(cpc_desc, i);
continue;
}
/*
* A present Enable or Autonomous Selection control must remain
* usable. Disabling the latter could leave autonomous selection
* enabled while OSPM believes that it has disabled it.
*/
if (i == ENABLE ||
(i == AUTO_SEL_ENABLE && cpc_entry_present(&cpc_desc->cpc_regs[i])) ||
i == MIN_PERF || i == MAX_PERF ||
!IS_OPTIONAL_CPC_REG(i)) {
pr_err("CPU%d: cannot access _CPC register %u\n",
cpc_desc->cpu_id, i);
return -EINVAL;
}
pr_warn("CPU%d: ignoring inaccessible optional _CPC register %u\n",
cpc_desc->cpu_id, i);
cpc_disable_reg(cpc_desc, i);
}
return 0;
}
static int cpc_validate_required_controls(struct cpc_desc *cpc_desc)
{
unsigned int i;
/*
* Performance Limited is required by the specification, but tolerate a
* NULL descriptor used by firmware which cannot report limiting events.
* CPPC control does not depend on this status.
*/
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
if (i != DESIRED_PERF && i != PERF_LIMITED &&
!IS_OPTIONAL_CPC_REG(i) &&
!cpc_entry_present(&cpc_desc->cpc_regs[i])) {
pr_debug("CPU:%d lacks mandatory _CPC register %u\n",
cpc_desc->cpu_id, i);
return -EINVAL;
}
}
/* Desired may be absent only for immutable autonomous operation. */
if (!cpc_is_writable(&cpc_desc->cpc_regs[DESIRED_PERF]) &&
!cpc_immutable_autonomous(cpc_desc)) {
pr_debug("CPU:%d lacks a writable Desired Performance register\n",
cpc_desc->cpu_id);
return -EINVAL;
}
return 0;
}
static int cpc_validate_bound_controls(struct cpc_desc *cpc_desc)
{
bool have_min, have_max;
have_min = cpc_is_writable(&cpc_desc->cpc_regs[MIN_PERF]);
have_max = cpc_is_writable(&cpc_desc->cpc_regs[MAX_PERF]);
if (have_min != have_max) {
pr_err("CPU%d: _CPC must provide both Minimum and Maximum Performance or neither\n",
cpc_desc->cpu_id);
return -EINVAL;
}
return 0;
}
static bool
cpc_retain_pcc_status(struct cpc_desc *cpc_desc, unsigned int reg_idx)
{
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx];
const struct cpc_reg *gas = ®->cpc_entry.reg;
u64 size;
if (reg_idx != PERF_LIMITED || !gas->bit_width)
return false;
size = DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8);
if (!size || gas->address > U64_MAX - (size - 1))
return false;
reg->cpc_entry.read_unsupported = true;
reg->cpc_entry.write_unsupported = true;
pr_warn_once("CPU%d: Performance Limited register cannot be accessed; keeping its PCC range reserved\n",
cpc_desc->cpu_id);
return true;
}
static bool
cpc_retain_sysio_status(struct cpc_desc *cpc_desc, unsigned int reg_idx)
{
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx];
const struct cpc_reg *gas = ®->cpc_entry.reg;
if (reg_idx != PERF_LIMITED || !gas->bit_width)
return false;
/* Retain any in-range portion for overlap validation only. */
if (gas->address > U16_MAX)
return false;
pr_warn_once("CPU%d: Performance Limited register cannot be accessed; keeping its SystemIO range reserved\n",
cpc_desc->cpu_id);
reg->cpc_entry.read_unsupported = true;
reg->cpc_entry.write_unsupported = true;
return true;
}
static u64 cpc_non_mmio_access_size(const struct cpc_register_resource *reg)
{
const struct cpc_reg *gas = ®->cpc_entry.reg;
if (gas->space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
return DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8);
return max((u64)cpc_reg_access_width(gas) / 8,
DIV_ROUND_UP((u64)gas->bit_offset + gas->bit_width, 8));
}
static void cpc_validate_pcc_bounds(struct cpc_desc *cpc_desc,
int pcc_ss_id, struct cppc_pcc_data *data,
u32 *unsupported)
{
u64 shmem_size = data->pcc_channel->shmem_size;
unsigned int i;
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
struct cpc_reg *gas;
u64 access_size;
if ((*unsupported & BIT(i)) || !CPC_SUPPORTED(reg) ||
!CPC_IN_PCC(reg))
continue;
gas = ®->cpc_entry.reg;
if (gas->access_width != pcc_ss_id)
continue;
access_size = cpc_non_mmio_access_size(reg);
if (shmem_size >= CPC_PCC_HEADER_SIZE &&
gas->address <= shmem_size - CPC_PCC_HEADER_SIZE &&
access_size <= shmem_size - CPC_PCC_HEADER_SIZE - gas->address)
continue;
pr_debug("CPU%d: _CPC register %u exceeds the PCC shared region\n",
cpc_desc->cpu_id, i);
*unsupported |= BIT(i);
}
}
static bool cpc_pcc_access_needed(const struct cpc_desc *cpc_desc)
{
unsigned int i;
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
const struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
if (CPC_SUPPORTED(reg) && CPC_IN_PCC(reg) &&
(cpc_is_readable(reg) || cpc_is_writable(reg)))
return true;
}
return false;
}
static bool cpc_non_mmio_overlap_conflicts(u8 space_id, bool a_writable,
bool b_writable, bool a_write_only,
bool b_write_only)
{
/* Only a write-only control can use a separate read-side port alias. */
if (space_id == ACPI_ADR_SPACE_SYSTEM_IO && a_writable != b_writable)
return a_writable ? !a_write_only : !b_write_only;
return a_writable || b_writable;
}
static bool
cpc_sysio_perf_limited_conflicts(unsigned int a_idx, bool a_writable,
unsigned int b_idx, bool b_writable)
{
return (a_idx == PERF_LIMITED && b_writable) ||
(b_idx == PERF_LIMITED && a_writable);
}
static struct rb_root_cached *cpc_non_mmio_tree(u8 space_id, u8 pcc_ss_id)
{
if (space_id == ACPI_ADR_SPACE_PLATFORM_COMM)
return &cpc_pcc_trees[pcc_ss_id];
if (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
return &cpc_sysio_tree;
return NULL;
}
static bool cpc_same_non_mmio_register(const struct cpc_non_mmio_node *a,
const struct cpc_non_mmio_node *b)
{
const struct cpc_reg *a_gas =
&a->desc->cpc_regs[a->reg_idx].cpc_entry.reg;
const struct cpc_reg *b_gas =
&b->desc->cpc_regs[b->reg_idx].cpc_entry.reg;
return a->space_id == b->space_id && a->pcc_ss_id == b->pcc_ss_id &&
a->reg_idx == b->reg_idx && a->start == b->start &&
a->last == b->last && a_gas->bit_offset == b_gas->bit_offset &&
a_gas->bit_width == b_gas->bit_width &&
(a->space_id == ACPI_ADR_SPACE_PLATFORM_COMM ||
cpc_reg_access_width(a_gas) == cpc_reg_access_width(b_gas));
}
static int cpc_validate_non_mmio_pair(const struct cpc_non_mmio_node *a,
const struct cpc_non_mmio_node *b)
{
const struct cpc_register_resource *a_reg;
const struct cpc_register_resource *b_reg;
bool a_writable, b_writable;
const char *name;
a_reg = &a->desc->cpc_regs[a->reg_idx];
b_reg = &b->desc->cpc_regs[b->reg_idx];
a_writable = cpc_reg_is_writable(a->reg_idx) && cpc_is_writable(a_reg);
b_writable = cpc_reg_is_writable(b->reg_idx) && cpc_is_writable(b_reg);
if (!cpc_non_mmio_overlap_conflicts(a->space_id, a_writable,
b_writable,
cpc_reg_is_write_only(a->desc, a->reg_idx),
cpc_reg_is_write_only(b->desc, b->reg_idx)) &&
!(a->space_id == ACPI_ADR_SPACE_SYSTEM_IO &&
cpc_sysio_perf_limited_conflicts(a->reg_idx, a_writable,
b->reg_idx, b_writable)))
return 0;
if (cpc_same_non_mmio_register(a, b))
return 0;
name = a->space_id == ACPI_ADR_SPACE_PLATFORM_COMM ?
"PCC" : "SystemIO";
pr_err("CPU%d: %s _CPC register %u conflicts with CPU%d register %u\n",
a->desc->cpu_id, name, a->reg_idx, b->desc->cpu_id,
b->reg_idx);
return -EINVAL;
}
static void cpc_unregister_non_mmio_desc_locked(struct cpc_desc *cpc_desc)
{
unsigned int i;
if (!cpc_desc->non_mmio_nodes)
return;
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
struct cpc_non_mmio_node *node = &cpc_desc->non_mmio_nodes[i];
struct rb_root_cached *tree;
if (!node->registered)
continue;
tree = cpc_non_mmio_tree(node->space_id, node->pcc_ss_id);
cpc_non_mmio_itree_remove(node, tree);
}
kfree(cpc_desc->non_mmio_nodes);
cpc_desc->non_mmio_nodes = NULL;
}
static int cpc_register_non_mmio_desc(struct cpc_desc *cpc_desc)
{
unsigned int nr_regs = cpc_desc->num_entries - 2;
unsigned int i;
int ret = 0;
bool found = false;
for (i = 0; i < nr_regs; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
u8 space_id;
if (!CPC_SUPPORTED(reg) || reg->type != ACPI_TYPE_BUFFER)
continue;
space_id = reg->cpc_entry.reg.space_id;
if (space_id == ACPI_ADR_SPACE_PLATFORM_COMM ||
space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
found = true;
break;
}
}
if (!found)
return 0;
cpc_desc->non_mmio_nodes = kcalloc(nr_regs,
sizeof(*cpc_desc->non_mmio_nodes),
GFP_KERNEL);
if (!cpc_desc->non_mmio_nodes)
return -ENOMEM;
mutex_lock(&cpc_non_mmio_lock);
for (i = 0; i < nr_regs; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
struct cpc_non_mmio_node *match, *node;
struct rb_root_cached *tree;
u8 space_id;
u64 size;
if (!CPC_SUPPORTED(reg) || reg->type != ACPI_TYPE_BUFFER)
continue;
space_id = reg->cpc_entry.reg.space_id;
if (space_id != ACPI_ADR_SPACE_PLATFORM_COMM &&
space_id != ACPI_ADR_SPACE_SYSTEM_IO)
continue;
node = &cpc_desc->non_mmio_nodes[i];
size = cpc_non_mmio_access_size(reg);
node->start = reg->cpc_entry.reg.address;
node->last = node->start + size - 1;
node->desc = cpc_desc;
node->reg_idx = i;
node->space_id = space_id;
node->pcc_ss_id = space_id == ACPI_ADR_SPACE_PLATFORM_COMM ?
reg->cpc_entry.reg.access_width : 0;
tree = cpc_non_mmio_tree(space_id, node->pcc_ss_id);
match = cpc_non_mmio_itree_iter_first(tree, node->start,
node->last);
while (match) {
ret = cpc_validate_non_mmio_pair(node, match);
if (ret)
goto out_unregister;
match = cpc_non_mmio_itree_iter_next(match, node->start,
node->last);
}
cpc_non_mmio_itree_insert(node, tree);
node->registered = true;
}
mutex_unlock(&cpc_non_mmio_lock);
return 0;
out_unregister:
cpc_unregister_non_mmio_desc_locked(cpc_desc);
mutex_unlock(&cpc_non_mmio_lock);
return ret;
}
static void cpc_unregister_non_mmio_desc(struct cpc_desc *cpc_desc)
{
if (!cpc_desc->non_mmio_nodes)
return;
mutex_lock(&cpc_non_mmio_lock);
cpc_unregister_non_mmio_desc_locked(cpc_desc);
mutex_unlock(&cpc_non_mmio_lock);
}
static void cpc_mark_rmw_lock_users(struct cpc_desc *cpc_desc)
{
int i;
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
if (CPC_SUPPORTED(reg) && CPC_IN_SYSTEM_MEMORY(reg) &&
cpc_is_writable(reg))
reg->cpc_entry.use_rmw_lock =
cpc_sysmem_reg_needs_rmw(reg);
}
}
struct cpc_bit_position {
u64 byte;
u8 bit;
};
static bool cpc_bit_position_before(const struct cpc_bit_position *a,
const struct cpc_bit_position *b)
{
return a->byte < b->byte || (a->byte == b->byte && a->bit < b->bit);
}
static bool cpc_sysmem_fields_overlap(const struct cpc_register_resource *a,
const struct cpc_register_resource *b)
{
const struct cpc_reg *a_gas = &a->cpc_entry.reg;
const struct cpc_reg *b_gas = &b->cpc_entry.reg;
unsigned int a_last_bit = a_gas->bit_offset + a_gas->bit_width - 1;
unsigned int b_last_bit = b_gas->bit_offset + b_gas->bit_width - 1;
struct cpc_bit_position a_start = {
.byte = a_gas->address + a_gas->bit_offset / 8,
.bit = a_gas->bit_offset % 8,
};
struct cpc_bit_position a_end = {
.byte = a_gas->address + a_last_bit / 8,
.bit = a_last_bit % 8,
};
struct cpc_bit_position b_start = {
.byte = b_gas->address + b_gas->bit_offset / 8,
.bit = b_gas->bit_offset % 8,
};
struct cpc_bit_position b_end = {
.byte = b_gas->address + b_last_bit / 8,
.bit = b_last_bit % 8,
};
return !cpc_bit_position_before(&a_end, &b_start) &&
!cpc_bit_position_before(&b_end, &a_start);
}
static bool cpc_sysmem_access_overlaps_field(const struct cpc_register_resource *access,
const struct cpc_register_resource *field)
{
const struct cpc_reg *access_gas = &access->cpc_entry.reg;
const struct cpc_reg *field_gas = &field->cpc_entry.reg;
u64 access_last;
u64 field_start;
u64 field_last;
if (!field_gas->bit_width)
return cpc_sysmem_access_units_overlap(access, field);
access_last = access_gas->address +
cpc_sysmem_access_size(access) - 1;
field_start = field_gas->address + field_gas->bit_offset / 8;
field_last = field_gas->address +
(field_gas->bit_offset + field_gas->bit_width - 1) / 8;
return access_gas->address <= field_last && field_start <= access_last;
}
static bool cpc_same_sysmem_register(unsigned int a_idx,
const struct cpc_register_resource *a,
unsigned int b_idx,
const struct cpc_register_resource *b)
{
const struct cpc_reg *a_gas = &a->cpc_entry.reg;
const struct cpc_reg *b_gas = &b->cpc_entry.reg;
return a_idx == b_idx &&
a_gas->address == b_gas->address &&
a_gas->bit_width == b_gas->bit_width &&
a_gas->bit_offset == b_gas->bit_offset &&
cpc_reg_access_width(a_gas) == cpc_reg_access_width(b_gas);
}
static int cpc_validate_sysmem_pair(const struct cpc_desc *a_desc,
unsigned int a_idx,
const struct cpc_desc *b_desc,
unsigned int b_idx)
{
const struct cpc_register_resource *a = &a_desc->cpc_regs[a_idx];
const struct cpc_register_resource *b = &b_desc->cpc_regs[b_idx];
bool a_write_only, b_write_only;
bool a_writable, b_writable;
bool fields_overlap;
/* The overlap helper includes each descriptor's conservative claim. */
if (!CPC_SUPPORTED(a) || !CPC_IN_SYSTEM_MEMORY(a) ||
!CPC_SUPPORTED(b) || !CPC_IN_SYSTEM_MEMORY(b) ||
!cpc_sysmem_access_units_overlap(a, b))
return 0;
a_write_only = cpc_reg_is_write_only(a_desc, a_idx);
b_write_only = cpc_reg_is_write_only(b_desc, b_idx);
fields_overlap = !a->cpc_entry.reg.bit_width ||
!b->cpc_entry.reg.bit_width ||
cpc_sysmem_fields_overlap(a, b);
/* A readable field must not expose another field's undefined bits. */
if (a_write_only != b_write_only &&
cpc_is_readable(a_write_only ? b : a) &&
fields_overlap)
goto conflict;
a_writable = cpc_reg_is_writable(a_idx) && cpc_is_writable(a);
b_writable = cpc_reg_is_writable(b_idx) && cpc_is_writable(b);
if (!a_writable && !b_writable)
return 0;
if (cpc_same_sysmem_register(a_idx, a, b_idx, b)) {
u64 access_size = cpc_sysmem_access_size(a);
/*
* Exact partial aliases update the same field and retain
* last-writer-wins semantics when the complete access is one native
* transaction. A 64-bit MMIO write may be split on 32-bit kernels,
* and an unaligned x86 access is not guaranteed to be one device
* transaction.
*/
if (!a_writable ||
(IS_ALIGNED(a->cpc_entry.reg.address, access_size) &&
(access_size < sizeof(u64) ||
IS_ENABLED(CONFIG_64BIT))))
return 0;
goto conflict;
}
/*
* The platform may set Performance Limited asynchronously. A write to
* another field in the same access unit could write back stale status
* bits, which an OSPM lock cannot prevent.
*/
if ((a_idx == PERF_LIMITED && b_writable) ||
(b_idx == PERF_LIMITED && a_writable))
goto conflict;
/* A full-width writer must not overwrite another logical field. */
if (fields_overlap &&
((a_writable && b_writable) ||
(a_writable && !cpc_sysmem_reg_needs_rmw(a)) ||
(b_writable && !cpc_sysmem_reg_needs_rmw(b))))
goto conflict;
/* Different descriptors do not share their partial-write locks. */
if (a_desc != b_desc && a_writable && b_writable)
goto conflict;
/*
* RMW of either writer preserves the other field. If that field is
* write-only, its readback is undefined and cannot safely be replayed.
*/
if ((a_write_only && b_writable &&
cpc_sysmem_reg_needs_rmw(b) &&
cpc_sysmem_access_overlaps_field(b, a)) ||
(b_write_only && a_writable &&
cpc_sysmem_reg_needs_rmw(a) &&
cpc_sysmem_access_overlaps_field(a, b)))
goto conflict;
return 0;
conflict:
pr_err("CPU%d: SystemMemory _CPC register %u conflicts with CPU%d register %u\n",
a_desc->cpu_id, a_idx, b_desc->cpu_id, b_idx);
return -EINVAL;
}
static bool cpc_disable_new_sysmem_writer(struct cpc_desc *cpc_desc,
unsigned int reg_idx,
const struct cpc_sysmem_node *node)
{
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[reg_idx];
struct cpc_sysmem_node *match;
unsigned int status_cpu = 0;
bool found = false;
if (!cpc_optional_writer_can_be_disabled(reg_idx) ||
!cpc_is_writable(reg))
return false;
match = cpc_sysmem_first(node->start, node->last);
while (match) {
const struct cpc_register_resource *status;
if (match->reg_idx == PERF_LIMITED) {
status = &match->desc->cpc_regs[PERF_LIMITED];
if (!status->cpc_entry.reg.bit_width ||
cpc_sysmem_fields_overlap(reg, status))
return false;
status_cpu = match->desc->cpu_id;
found = true;
}
match = cpc_sysmem_next(match, node->start, node->last);
}
if (!found)
return false;
pr_warn_once("CPU%d: ignoring optional _CPC register %u sharing CPU%d Performance Limited access unit\n",
cpc_desc->cpu_id, reg_idx, status_cpu);
cpc_disable_reg(cpc_desc, reg_idx);
return true;
}
static void cpc_unregister_sysmem_desc_locked(struct cpc_desc *cpc_desc)
{
unsigned int i;
if (!cpc_desc->sysmem_nodes)
return;
for (i = 0; i < cpc_desc->num_entries - 2; i++) {
struct cpc_sysmem_node *node = &cpc_desc->sysmem_nodes[i];
struct cpc_sysmem_node *alias, *child;
if (node->alias_of) {
list_del(&node->alias_node);
continue;
}
if (!node->registered)
continue;
cpc_sysmem_itree_remove(node, &cpc_sysmem_tree);
node->registered = false;
if (list_empty(&node->aliases))
continue;
/* Keep one representative for aliases owned by live descriptors. */
alias = list_first_entry(&node->aliases,
struct cpc_sysmem_node, alias_node);
list_del_init(&alias->alias_node);
alias->alias_of = NULL;
alias->registered = true;
list_splice_init(&node->aliases, &alias->aliases);
list_for_each_entry(child, &alias->aliases, alias_node)
child->alias_of = alias;
cpc_sysmem_itree_insert(alias, &cpc_sysmem_tree);
}
kfree(cpc_desc->sysmem_nodes);
cpc_desc->sysmem_nodes = NULL;
}
static int cpc_register_sysmem_desc(struct cpc_desc *cpc_desc)
{
unsigned int nr_regs = cpc_desc->num_entries - 2;
unsigned int i;
int ret = 0;
bool found = false;
for (i = 0; i < nr_regs; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
if (CPC_SUPPORTED(reg) && CPC_IN_SYSTEM_MEMORY(reg)) {
found = true;
break;
}
}
if (!found)
return 0;
cpc_desc->sysmem_nodes = kcalloc(nr_regs,
sizeof(*cpc_desc->sysmem_nodes),
GFP_KERNEL);
if (!cpc_desc->sysmem_nodes)
return -ENOMEM;
mutex_lock(&cpc_sysmem_lock);
for (i = 0; i < nr_regs; i++) {
struct cpc_register_resource *reg = &cpc_desc->cpc_regs[i];
struct cpc_sysmem_node *alias = NULL, *match, *node;
u64 size;
if (!CPC_SUPPORTED(reg) || !CPC_IN_SYSTEM_MEMORY(reg))
continue;
node = &cpc_desc->sysmem_nodes[i];
size = cpc_sysmem_claim_size(reg);
node->start = reg->cpc_entry.reg.address;
node->last = node->start + size - 1;
node->desc = cpc_desc;
node->reg_idx = i;
INIT_LIST_HEAD(&node->aliases);
INIT_LIST_HEAD(&node->alias_node);
/* Performance Limited precedes every optional writer we may disable. */
if (cpc_disable_new_sysmem_writer(cpc_desc, i, node))
continue;
match = cpc_sysmem_first(node->start, node->last);
while (match) {
struct cpc_register_resource *match_reg;
match_reg = &match->desc->cpc_regs[match->reg_idx];
ret = cpc_validate_sysmem_pair(cpc_desc, i, match->desc,
match->reg_idx);
if (ret)
goto out_unregister;
if (cpc_desc == match->desc) {
reg->cpc_entry.use_rmw_lock = true;
match_reg->cpc_entry.use_rmw_lock = true;
}
if (cpc_same_sysmem_register(i, reg, match->reg_idx, match_reg))
alias = match;
match = cpc_sysmem_next(match, node->start, node->last);
}
if (alias) {
node->alias_of = alias;
list_add_tail(&node->alias_node, &alias->aliases);
continue;
}
cpc_sysmem_itree_insert(node, &cpc_sysmem_tree);
node->registered = true;
}
mutex_unlock(&cpc_sysmem_lock);
return 0;
out_unregister:
cpc_unregister_sysmem_desc_locked(cpc_desc);
mutex_unlock(&cpc_sysmem_lock);
return ret;
}
static void cpc_unregister_sysmem_desc(struct cpc_desc *cpc_desc)
{
if (!cpc_desc->sysmem_nodes)
return;
mutex_lock(&cpc_sysmem_lock);
cpc_unregister_sysmem_desc_locked(cpc_desc);
mutex_unlock(&cpc_sysmem_lock);
}
static ssize_t show_feedback_ctrs(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
struct cpc_desc *cpc_ptr = to_cpc_desc(kobj);
struct cppc_perf_fb_ctrs fb_ctrs = {0};
int ret;
ret = cppc_get_perf_ctrs(cpc_ptr->cpu_id, &fb_ctrs);
if (ret)
return ret;
return sysfs_emit(buf, "ref:%llu del:%llu\n",
fb_ctrs.reference, fb_ctrs.delivered);
}
define_one_cppc_ro(feedback_ctrs);
static struct attribute *cppc_attrs[] = {
&feedback_ctrs.attr,
&reference_perf.attr,
&wraparound_time.attr,
&highest_perf.attr,
&lowest_perf.attr,
&lowest_nonlinear_perf.attr,
&guaranteed_perf.attr,
&nominal_perf.attr,
&nominal_freq.attr,
&lowest_freq.attr,
NULL
};
ATTRIBUTE_GROUPS(cppc);
static void cppc_free_desc(struct cpc_desc *cpc_ptr)
{
unsigned int i;
cpc_unregister_non_mmio_desc(cpc_ptr);
cpc_unregister_sysmem_desc(cpc_ptr);
for (i = 2; i < cpc_ptr->num_entries; i++) {
void __iomem *addr = cpc_ptr->cpc_regs[i - 2].sys_mem_vaddr;
if (addr)
iounmap(addr);
}
kfree(cpc_ptr);
}
static void cppc_kobj_release(struct kobject *kobj)
{
cppc_free_desc(to_cpc_desc(kobj));
}
static const struct kobj_type cppc_ktype = {
.release = cppc_kobj_release,
.sysfs_ops = &kobj_sysfs_ops,
.default_groups = cppc_groups,
};
static int check_pcc_chan(int pcc_ss_id, bool chk_err_bit)
{
int ret, status;
struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
struct acpi_pcct_shared_memory __iomem *generic_comm_base =
pcc_ss_data->pcc_channel->shmem;
if (!pcc_ss_data->platform_owns_pcc)
return 0;
/*
* Poll PCC status register every 3us(delay_us) for maximum of
* deadline_us(timeout_us) until PCC command complete bit is set(cond)
*/
ret = readw_relaxed_poll_timeout(&generic_comm_base->status, status,
status & PCC_CMD_COMPLETE_MASK, 3,
pcc_ss_data->deadline_us);
if (likely(!ret)) {
/* Order completion status before reading the returned payload. */
rmb();
pcc_ss_data->platform_owns_pcc = false;
if (chk_err_bit && (status & PCC_ERROR_MASK))
ret = -EIO;
}
if (unlikely(ret))
pr_err("PCC check channel failed for ss: %d. ret=%d\n",
pcc_ss_id, ret);
return ret;
}
static void cppc_complete_pcc_write(int pcc_ss_id,
struct cppc_pcc_data *pcc_ss_data, int ret)
{
int i;
if (unlikely(ret)) {
for_each_possible_cpu(i) {
struct cpc_desc *desc = per_cpu(cpc_desc_ptr, i);
if (!desc ||
per_cpu(cpu_pcc_subspace_idx, i) != pcc_ss_id)
continue;
if (desc->write_cmd_id == pcc_ss_data->pcc_write_cnt)
desc->write_cmd_status = ret;
}
}
pcc_ss_data->pcc_write_cnt++;
wake_up_all(&pcc_ss_data->pcc_write_wait_q);
}
/* The caller must hold pcc_lock for write. */
static void cppc_abort_pending_pcc_write(int pcc_ss_id,
struct cppc_pcc_data *pcc_ss_data,
int ret)
{
if (!pcc_ss_data->pending_pcc_write_cmd)
return;
pcc_ss_data->pending_pcc_write_cmd = false;
cppc_complete_pcc_write(pcc_ss_id, pcc_ss_data, ret);
}
/*
* This function transfers the ownership of the PCC to the platform
* So it must be called while holding write_lock(pcc_lock)
*/
static int send_pcc_cmd(int pcc_ss_id, u16 cmd)
{
int ret = -EIO;
struct cppc_pcc_data *pcc_ss_data = pcc_data[pcc_ss_id];
struct acpi_pcct_shared_memory __iomem *generic_comm_base =
pcc_ss_data->pcc_channel->shmem;
unsigned int time_delta;
/*
* For CMD_WRITE we know for a fact the caller should have checked
* the channel before writing to PCC space
*/
if (cmd == CMD_READ) {
/*
* If there are pending cpc_writes, then we stole the channel
* before write completion, so first send a WRITE command to
* platform
*/
if (pcc_ss_data->pending_pcc_write_cmd)
send_pcc_cmd(pcc_ss_id, CMD_WRITE);
ret = check_pcc_chan(pcc_ss_id, false);
if (ret)
goto end;
} else /* CMD_WRITE */
pcc_ss_data->pending_pcc_write_cmd = FALSE;
/*
* Handle the Minimum Request Turnaround Time(MRTT)
* "The minimum amount of time that OSPM must wait after the completion
* of a command before issuing the next command, in microseconds"
*/
if (pcc_ss_data->pcc_mrtt) {
time_delta = ktime_us_delta(ktime_get(),
pcc_ss_data->last_cmd_cmpl_time);
if (pcc_ss_data->pcc_mrtt > time_delta)
udelay(pcc_ss_data->pcc_mrtt - time_delta);
}
/*
* Handle the non-zero Maximum Periodic Access Rate(MPAR)
* "The maximum number of periodic requests that the subspace channel can
* support, reported in commands per minute. 0 indicates no limitation."
*
* This parameter should be ideally zero or large enough so that it can
* handle maximum number of requests that all the cores in the system can
* collectively generate. If it is not, we will follow the spec and just
* not send the request to the platform after hitting the MPAR limit in
* any 60s window
*/
if (pcc_ss_data->pcc_mpar) {
if (pcc_ss_data->mpar_count == 0) {
time_delta = ktime_ms_delta(ktime_get(),
pcc_ss_data->last_mpar_reset);
if ((time_delta < 60 * MSEC_PER_SEC) && pcc_ss_data->last_mpar_reset) {
pr_debug("PCC cmd for subspace %d not sent due to MPAR limit",
pcc_ss_id);
ret = -EIO;
goto end;
}
pcc_ss_data->last_mpar_reset = ktime_get();
pcc_ss_data->mpar_count = pcc_ss_data->pcc_mpar;
}
pcc_ss_data->mpar_count--;
}
/* Write to the shared comm region. */
writew_relaxed(cmd, &generic_comm_base->command);
/* Flip CMD COMPLETE bit */
writew_relaxed(0, &generic_comm_base->status);
pcc_ss_data->platform_owns_pcc = true;
/* Ring doorbell */
ret = mbox_send_message(pcc_ss_data->pcc_channel->mchan, &cmd);
if (ret < 0) {
pr_err("Err sending PCC mbox message. ss: %d cmd:%d, ret:%d\n",
pcc_ss_id, cmd, ret);
goto end;
}
/* wait for completion and check for PCC error bit */
ret = check_pcc_chan(pcc_ss_id, true);
if (pcc_ss_data->pcc_mrtt)
pcc_ss_data->last_cmd_cmpl_time = ktime_get();
if (pcc_ss_data->pcc_channel->mchan->mbox->txdone_irq)
mbox_chan_txdone(pcc_ss_data->pcc_channel->mchan, ret);
else
mbox_client_txdone(pcc_ss_data->pcc_channel->mchan, ret);
end:
if (cmd == CMD_WRITE)
cppc_complete_pcc_write(pcc_ss_id, pcc_ss_data, ret);
return ret;
}
static void cppc_chan_tx_done(struct mbox_client *cl, void *msg, int ret)
{
if (ret < 0)
pr_debug("TX did not complete: CMD sent:%x, ret:%d\n",
*(u16 *)msg, ret);
else
pr_debug("TX completed. CMD sent:%x, ret:%d\n",
*(u16 *)msg, ret);
}
static struct mbox_client cppc_mbox_cl = {
.tx_done = cppc_chan_tx_done,
.knows_txdone = true,
};
static int acpi_get_psd(struct cpc_desc *cpc_ptr, acpi_handle handle)
{
int result = -EFAULT;
acpi_status status = AE_OK;
struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
struct acpi_buffer state = {0, NULL};
union acpi_object *psd = NULL;
struct acpi_psd_package *pdomain;
status = acpi_evaluate_object_typed(handle, "_PSD", NULL,
&buffer, ACPI_TYPE_PACKAGE);
if (status == AE_NOT_FOUND) /* _PSD is optional */
return 0;
if (ACPI_FAILURE(status))
return -ENODEV;
psd = buffer.pointer;
if (!psd || psd->package.count != 1) {
pr_debug("Invalid _PSD data\n");
goto end;
}
pdomain = &(cpc_ptr->domain_info);
state.length = sizeof(struct acpi_psd_package);
state.pointer = pdomain;
status = acpi_extract_package(&(psd->package.elements[0]),
&format, &state);
if (ACPI_FAILURE(status)) {
pr_debug("Invalid _PSD data for CPU:%d\n", cpc_ptr->cpu_id);
goto end;
}
if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
pr_debug("Unknown _PSD:num_entries for CPU:%d\n", cpc_ptr->cpu_id);
goto end;
}
if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
pr_debug("Unknown _PSD:revision for CPU: %d\n", cpc_ptr->cpu_id);
goto end;
}
if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
pr_debug("Invalid _PSD:coord_type for CPU:%d\n", cpc_ptr->cpu_id);
goto end;
}
result = 0;
end:
kfree(buffer.pointer);
return result;
}
bool acpi_cpc_valid(void)
{
struct cpc_desc *cpc_ptr;
int cpu;
if (acpi_disabled)
return false;
for_each_online_cpu(cpu) {
cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
if (!cpc_ptr)
return false;
}
return true;
}
EXPORT_SYMBOL_GPL(acpi_cpc_valid);
bool cppc_allow_fast_switch(const struct cpumask *cpus)
{
struct cpc_register_resource *desired_reg, *min_reg, *max_reg;
struct cpc_desc *cpc_ptr;
int cpu;
for_each_cpu(cpu, cpus) {
cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
if (!cpc_ptr)
return false;
desired_reg = &cpc_ptr->cpc_regs[DESIRED_PERF];
min_reg = &cpc_ptr->cpc_regs[MIN_PERF];
max_reg = &cpc_ptr->cpc_regs[MAX_PERF];
if (!cpc_is_writable(desired_reg) ||
(!CPC_IN_SYSTEM_MEMORY(desired_reg) &&
!CPC_IN_SYSTEM_IO(desired_reg)) ||
(CPC_SUPPORTED(min_reg) &&
!CPC_IN_SYSTEM_MEMORY(min_reg) &&
!CPC_IN_SYSTEM_IO(min_reg)) ||
(CPC_SUPPORTED(max_reg) &&
!CPC_IN_SYSTEM_MEMORY(max_reg) &&
!CPC_IN_SYSTEM_IO(max_reg)))
return false;
}
return true;
}
EXPORT_SYMBOL_GPL(cppc_allow_fast_switch);
/**
* acpi_get_psd_map - Map the CPUs in the freq domain of a given cpu
* @cpu: Find all CPUs that share a domain with cpu.
* @cpu_data: Pointer to CPU specific CPPC data including PSD info.
*
* Return: 0 for success or negative value for err.
*/
int acpi_get_psd_map(unsigned int cpu, struct cppc_cpudata *cpu_data)
{
struct cpc_desc *cpc_ptr, *match_cpc_ptr;
struct acpi_psd_package *match_pdomain;
struct acpi_psd_package *pdomain;
int count_target, i;
/*
* Now that we have _PSD data from all CPUs, let's setup P-state
* domain info.
*/
cpc_ptr = per_cpu(cpc_desc_ptr, cpu);
if (!cpc_ptr)
return -EFAULT;
pdomain = &(cpc_ptr->domain_info);
cpumask_set_cpu(cpu, cpu_data->shared_cpu_map);
if (pdomain->num_processors <= 1)
return 0;
/* Validate the Domain info */
count_target = pdomain->num_processors;
if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ALL;
else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
cpu_data->shared_type = CPUFREQ_SHARED_TYPE_HW;
else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
cpu_data->shared_type = CPUFREQ_SHARED_TYPE_ANY;
for_each_possible_cpu(i) {
if (i == cpu)
continue;
match_cpc_ptr = per_cpu(cpc_desc_ptr, i);
if (!match_cpc_ptr)
continue;
match_pdomain = &(match_cpc_ptr->domain_info);
if (match_pdomain->domain != pdomain->domain)
continue;
/* Here i and cpu are in the same domain */
if (match_pdomain->num_processors != count_target)
goto err_fault;
if (pdomain->coord_type != match_pdomain->coord_type)
goto err_fault;
cpumask_set_cpu(i, cpu_data->shared_cpu_map);
}
return 0;
err_fault:
/* Assume no coordination on any error parsing domain info */
cpumask_clear(cpu_data->shared_cpu_map);
cpumask_set_cpu(cpu, cpu_data->shared_cpu_map);
cpu_data->shared_type = CPUFREQ_SHARED_TYPE_NONE;
return -EFAULT;
}
EXPORT_SYMBOL_GPL(acpi_get_psd_map);
static int register_pcc_channel(int pcc_ss_idx)
{
struct cppc_pcc_data *data;
struct pcc_mbox_chan *pcc_chan;
u64 usecs_lat;
int ret = 0;
if (pcc_ss_idx < 0 || pcc_ss_idx >= MAX_PCC_SUBSPACES)
return -EINVAL;
mutex_lock(&pcc_data_lock);
data = pcc_data[pcc_ss_idx];
if (!data) {
ret = -ENODEV;
goto out_unlock;
}
if (data->pcc_channel_acquired)
goto out_unlock;
pcc_chan = pcc_mbox_request_channel(&cppc_mbox_cl, pcc_ss_idx);
if (IS_ERR(pcc_chan)) {
ret = -ENODEV;
goto out_unlock;
}
data->pcc_channel = pcc_chan;
/*
* cppc_ss->latency is just a Nominal value. In reality
* the remote processor could be much slower to reply.
* So add an arbitrary amount of wait on top of Nominal.
*/
usecs_lat = NUM_RETRIES * pcc_chan->latency;
data->deadline_us = usecs_lat;
data->pcc_mrtt = pcc_chan->min_turnaround_time;
data->pcc_mpar = pcc_chan->max_access_rate;
data->pcc_nominal = pcc_chan->latency;
init_rwsem(&data->pcc_lock);
init_waitqueue_head(&data->pcc_write_wait_q);
/* Reuse this channel when another CPU references the same subspace. */
data->pcc_channel_acquired = true;
out_unlock:
mutex_unlock(&pcc_data_lock);
return ret;
}
/**
* cpc_ffh_supported() - check if FFH reading supported
*
* Check if the architecture has support for functional fixed hardware
* read/write capability.
*
* Return: true for supported, false for not supported
*/
bool __weak cpc_ffh_supported(void)
{
return false;
}
/**
* cpc_supported_by_cpu() - check if CPPC is supported by CPU
*
* Check if the architectural support for CPPC is present even
* if the _OSC hasn't prescribed it
*
* Return: true for supported, false for not supported
*/
bool __weak cpc_supported_by_cpu(void)
{
return false;
}
/**
* pcc_data_alloc() - Allocate the pcc_data memory for pcc subspace
* @pcc_ss_id: PCC Subspace index as in the PCC client ACPI package.
*
* Check and allocate the cppc_pcc_data memory.
* In some processor configurations it is possible that same subspace
* is shared between multiple CPUs. This is seen especially in CPUs
* with hardware multi-threading support.
*
* Return: 0 for success, errno for failure
*/
static int pcc_data_alloc(int pcc_ss_id)
{
struct cppc_pcc_data *data;
int ret = 0;
if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES)
return -EINVAL;
mutex_lock(&pcc_data_lock);
data = pcc_data[pcc_ss_id];
if (!data) {
data = kzalloc_obj(struct cppc_pcc_data);
if (!data) {
ret = -ENOMEM;
goto out_unlock;
}
raw_spin_lock_init(&data->payload_lock);
pcc_data[pcc_ss_id] = data;
}
data->refcount++;
out_unlock:
mutex_unlock(&pcc_data_lock);
return ret;
}
static void pcc_data_put(int pcc_ss_id)
{
struct cppc_pcc_data *data;
if (pcc_ss_id < 0 || pcc_ss_id >= MAX_PCC_SUBSPACES)
return;
mutex_lock(&pcc_data_lock);
data = pcc_data[pcc_ss_id];
if (!data || --data->refcount)
goto out_unlock;
pcc_data[pcc_ss_id] = NULL;
if (data->pcc_channel_acquired)
pcc_mbox_free_channel(data->pcc_channel);
kfree(data);
out_unlock:
mutex_unlock(&pcc_data_lock);
}
/*
* An example CPC table looks like the following.
*
* Name (_CPC, Package() {
* 17, // NumEntries
* 1, // Revision
* ResourceTemplate() {Register(PCC, 32, 0, 0x120, 2)}, // Highest Performance
* ResourceTemplate() {Register(PCC, 32, 0, 0x124, 2)}, // Nominal Performance
* ResourceTemplate() {Register(PCC, 32, 0, 0x128, 2)}, // Lowest Nonlinear Performance
* ResourceTemplate() {Register(PCC, 32, 0, 0x12C, 2)}, // Lowest Performance
* ResourceTemplate() {Register(PCC, 32, 0, 0x130, 2)}, // Guaranteed Performance Register
* ResourceTemplate() {Register(PCC, 32, 0, 0x110, 2)}, // Desired Performance Register
* ResourceTemplate() {Register(SystemMemory, 0, 0, 0, 0)},
* ...
* ...
* ...
* }
* Each Register() encodes how to access that specific register.
* e.g. a sample PCC entry has the following encoding:
*
* Register (
* PCC, // AddressSpaceKeyword
* 8, // RegisterBitWidth
* 8, // RegisterBitOffset
* 0x30, // RegisterAddress
* 9, // AccessSize (subspace ID)
* )
*/
/**
* acpi_cppc_processor_probe - Search for per CPU _CPC objects.
* @pr: Ptr to acpi_processor containing this CPU's logical ID.
*
* Return: 0 for success or negative value for err.
*/
int acpi_cppc_processor_probe(struct acpi_processor *pr)
{
struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
union acpi_object *out_obj, *cpc_obj;
struct cpc_desc *cpc_ptr;
struct cpc_reg *gas_t;
struct device *cpu_dev;
acpi_handle handle = pr->handle;
unsigned int num_ent, i, cpc_rev;
u32 unsupported_regs = 0;
u32 platform_quirks;
int pcc_subspace_id = -1;
bool pcc_data_ref = false;
bool cpc_present = false;
acpi_status status;
int ret = -EINVAL;
int err;
if (per_cpu(cpc_desc_ptr, pr->id))
return 0;
ret = cpc_get_platform_quirks(&platform_quirks);
if (ret) {
pr_err("CPU%d: failed to match CPPC platform quirks: %d\n",
pr->id, ret);
return ret;
}
per_cpu(cpu_pcc_subspace_idx, pr->id) = -1;
if (!osc_sb_cppc2_support_acked) {
pr_debug("CPPC v2 _OSC not acked\n");
if (!cpc_supported_by_cpu()) {
pr_debug("CPPC is not supported by the CPU\n");
return -ENODEV;
}
}
/* Parse the ACPI _CPC table for this CPU. */
status = acpi_evaluate_object_typed(handle, "_CPC", NULL, &output,
ACPI_TYPE_PACKAGE);
if (ACPI_FAILURE(status)) {
ret = -ENODEV;
goto out_buf_free;
}
cpc_present = true;
ret = -EINVAL;
out_obj = (union acpi_object *) output.pointer;
if (out_obj->package.count < 2) {
pr_debug("Unexpected _CPC package count (%u) for CPU:%d\n",
out_obj->package.count, pr->id);
goto out_buf_free;
}
cpc_ptr = kzalloc_obj(struct cpc_desc);
if (!cpc_ptr) {
ret = -ENOMEM;
goto out_buf_free;
}
cpc_ptr->cpu_id = pr->id;
/* First entry is NumEntries. */
cpc_obj = &out_obj->package.elements[0];
if (cpc_obj->type == ACPI_TYPE_INTEGER) {
if (cpc_obj->integer.value < 2 ||
cpc_obj->integer.value > out_obj->package.count) {
pr_debug("Invalid _CPC NumEntries (%llu) for package count (%u) on CPU:%d\n",
cpc_obj->integer.value, out_obj->package.count,
pr->id);
goto out_free;
}
num_ent = cpc_obj->integer.value;
} else {
pr_debug("Unexpected _CPC NumEntries entry type (%d) for CPU:%d\n",
cpc_obj->type, pr->id);
goto out_free;
}
/* Second entry should be revision. */
cpc_obj = &out_obj->package.elements[1];
if (cpc_obj->type == ACPI_TYPE_INTEGER) {
if (cpc_obj->integer.value > U8_MAX) {
pr_debug("Invalid _CPC Revision (%llu) for CPU:%d\n",
cpc_obj->integer.value, pr->id);
ret = -EINVAL;
goto out_free;
}
cpc_rev = cpc_obj->integer.value;
} else {
pr_debug("Unexpected _CPC Revision entry type (%d) for CPU:%d\n",
cpc_obj->type, pr->id);
goto out_free;
}
if (cpc_rev < CPPC_V2_REV) {
pr_debug("Unsupported _CPC Revision (%d) for CPU:%d\n", cpc_rev,
pr->id);
goto out_free;
}
/*
* Disregard _CPC if the number of entries in the return package is not
* as expected, but support future revisions being proper supersets of
* the v4 and only causing more entries to be returned by _CPC.
*/
if ((cpc_rev == CPPC_V2_REV && num_ent != CPPC_V2_NUM_ENT) ||
(cpc_rev == CPPC_V3_REV && num_ent != CPPC_V3_NUM_ENT) ||
(cpc_rev == CPPC_V4_REV && num_ent != CPPC_V4_NUM_ENT) ||
(cpc_rev > CPPC_V4_REV && num_ent <= CPPC_V4_NUM_ENT)) {
pr_debug("Unexpected number of _CPC return package entries (%d) for CPU:%d\n",
num_ent, pr->id);
goto out_free;
}
if (cpc_rev > CPPC_V4_REV) {
num_ent = CPPC_V4_NUM_ENT;
cpc_rev = CPPC_V4_REV;
}
cpc_ptr->num_entries = num_ent;
cpc_ptr->version = cpc_rev;
/* Iterate through remaining entries in _CPC */
for (i = 2; i < num_ent; i++) {
cpc_obj = &out_obj->package.elements[i];
if (cpc_obj->type == ACPI_TYPE_INTEGER) {
bool legacy_null;
if (!cpc_integer_entry_valid(i - 2,
cpc_obj->integer.value,
&legacy_null)) {
pr_debug("Invalid Integer _CPC register %u for CPU:%d\n",
i - 2, pr->id);
ret = -EINVAL;
goto out_free;
}
if (legacy_null)
pr_warn_once(FW_BUG "_CPC register %u uses Integer 0 for an absent Buffer\n",
i - 2);
cpc_ptr->cpc_regs[i - 2].type = ACPI_TYPE_INTEGER;
cpc_ptr->cpc_regs[i - 2].cpc_entry.int_value = cpc_obj->integer.value;
} else if (cpc_obj->type == ACPI_TYPE_BUFFER) {
if (cpc_obj->buffer.length < sizeof(*gas_t)) {
pr_debug("Invalid register descriptor for CPU:%d\n",
pr->id);
ret = -EINVAL;
goto out_free;
}
gas_t = (struct cpc_reg *)
cpc_obj->buffer.pointer;
if (gas_t->descriptor != CPC_GENERIC_REGISTER_DESCRIPTOR ||
gas_t->length != CPC_GENERIC_REGISTER_LENGTH) {
pr_debug("Invalid register resource for CPU:%d\n",
pr->id);
ret = -EINVAL;
goto out_free;
}
cpc_ptr->cpc_regs[i - 2].type = ACPI_TYPE_BUFFER;
memcpy(&cpc_ptr->cpc_regs[i - 2].cpc_entry.reg, gas_t,
sizeof(*gas_t));
gas_t = &cpc_ptr->cpc_regs[i - 2].cpc_entry.reg;
cpc_apply_platform_quirks(gas_t, i - 2,
platform_quirks);
/*
* The PCC Subspace index is encoded inside
* the CPC table entries. The same PCC index
* will be used for all the PCC entries,
* so extract it only once.
*/
if (gas_t->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
/* These registers have no specified 32-bit upper bound. */
bool wide_write = i - 2 == PERF_LIMITED ||
i - 2 == ENABLE ||
i - 2 == AUTO_SEL_ENABLE;
bool write_width_supported = gas_t->bit_width == 8 ||
gas_t->bit_width == 16 ||
gas_t->bit_width == 32 ||
gas_t->bit_width == 64;
bool unsupported;
unsupported = !gas_t->bit_width ||
gas_t->bit_width > 64 ||
gas_t->bit_offset ||
gas_t->bit_width % 8 ||
(cpc_reg_is_writable(i - 2) &&
(!write_width_supported ||
(!wide_write && gas_t->bit_width > 32)));
if (unsupported) {
if (!cpc_retain_pcc_status(cpc_ptr, i - 2))
unsupported_regs |= BIT(i - 2);
continue;
}
if (pcc_subspace_id < 0) {
pcc_subspace_id = gas_t->access_width;
} else if (pcc_subspace_id != gas_t->access_width) {
pr_debug("Mismatched PCC ids in _CPC for CPU:%d\n",
pr->id);
ret = -EINVAL;
goto out_free;
}
if (!pcc_data_ref) {
err = pcc_data_alloc(pcc_subspace_id);
if (err) {
ret = err;
goto out_free;
}
pcc_data_ref = true;
}
} else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
if (!IS_NULL_REG(gas_t)) {
void __iomem *addr;
size_t access_width;
err = cpc_validate_sysmem_reg(cpc_ptr, gas_t,
i - 2);
if (err) {
unsupported_regs |= BIT(i - 2);
continue;
}
if (!cpc_is_readable(&cpc_ptr->cpc_regs[i - 2]) &&
!cpc_is_writable(&cpc_ptr->cpc_regs[i - 2]))
continue;
if (!osc_cpc_flexible_adr_space_confirmed) {
pr_debug("Flexible address space capability not supported\n");
ret = -EOPNOTSUPP;
if (!cpc_supported_by_cpu())
goto out_free;
ret = -EINVAL;
}
access_width = cpc_reg_access_width(gas_t);
access_width /= 8;
addr = ioremap(gas_t->address, access_width);
if (!addr) {
ret = -ENOMEM;
goto out_free;
}
cpc_ptr->cpc_regs[i - 2].sys_mem_vaddr = addr;
}
} else if (gas_t->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
u64 access_size;
const char *reason = "uses unsupported SystemIO geometry";
unsigned int access_width;
bool partial = false;
bool unsupported;
access_width = cpc_reg_access_width(gas_t);
unsupported = !IS_ENABLED(CONFIG_HAS_IOPORT);
if (unsupported)
reason = "requires unavailable SystemIO support";
else
unsupported = access_width != 8 &&
access_width != 16 &&
access_width != 32;
if (!unsupported) {
access_size = access_width / 8;
unsupported = !gas_t->bit_width ||
gas_t->bit_width > access_width ||
gas_t->bit_offset >= access_width ||
gas_t->bit_width > access_width -
gas_t->bit_offset;
partial = gas_t->bit_offset ||
gas_t->bit_width != access_width;
}
if (!unsupported) {
unsupported = (cpc_reg_is_writable(i - 2) &&
i - 2 != PERF_LIMITED &&
cpc_is_writable(&cpc_ptr->cpc_regs[i - 2]) &&
partial) ||
!cpc_reg_access_aligned(gas_t,
access_size) ||
gas_t->address >
U16_MAX - (access_size - 1);
}
if (unsupported) {
if (cpc_retain_sysio_status(cpc_ptr, i - 2))
continue;
pr_debug("CPU%d: _CPC register %u %s\n",
pr->id, i - 2, reason);
unsupported_regs |= BIT(i - 2);
continue;
}
if (i - 2 == PERF_LIMITED && partial) {
pr_warn_once("CPU%d: Performance Limited register cannot be cleared safely; keeping it readable\n",
cpc_ptr->cpu_id);
cpc_ptr->cpc_regs[i - 2].cpc_entry.write_unsupported = true;
}
if (!osc_cpc_flexible_adr_space_confirmed) {
pr_debug("Flexible address space capability not supported\n");
ret = -EOPNOTSUPP;
if (!cpc_supported_by_cpu())
goto out_free;
ret = -EINVAL;
}
} else {
if (gas_t->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE || !cpc_ffh_supported()) {
/* Support only PCC, SystemMemory, SystemIO, and FFH type regs. */
pr_debug("Unsupported register type (%d) in _CPC\n",
gas_t->space_id);
ret = -EOPNOTSUPP;
goto out_free;
}
}
} else if (cpc_obj->type == ACPI_TYPE_PACKAGE && (i - 2) == RESOURCE_PRIORITY) {
/*
* ACPI 6.6, s8.4.6.1.2.7 defines Resource Priority as a
* Package of Resource Priority Register Descriptor sub-packages.
* Parsing the full structure is not yet supported.
* Mark the register as unsupported for now.
*/
pr_debug("CPU:%d Resource Priority not supported\n", pr->id);
cpc_ptr->cpc_regs[i-2].type = ACPI_TYPE_INTEGER;
cpc_ptr->cpc_regs[i-2].cpc_entry.int_value = 0;
} else {
pr_debug("Invalid entry type (%d) in _CPC for CPU:%d\n",
i, pr->id);
goto out_free;
}
}
per_cpu(cpu_pcc_subspace_idx, pr->id) = pcc_data_ref ?
pcc_subspace_id : -1;
ret = cpc_resolve_unsupported(cpc_ptr, unsupported_regs);
if (ret)
goto out_free;
unsupported_regs = 0;
ret = cpc_validate_required_controls(cpc_ptr);
if (ret)
goto out_free;
/*
* Initialize the remaining cpc_regs as unsupported.
* Example: In case FW exposes CPPC v2, the below loop will initialize
* LOWEST_FREQ and NOMINAL_FREQ regs as unsupported
*/
for (i = num_ent - 2; i < MAX_CPC_REG_ENT; i++) {
cpc_ptr->cpc_regs[i].type = ACPI_TYPE_INTEGER;
cpc_ptr->cpc_regs[i].cpc_entry.int_value = 0;
}
cpc_mark_rmw_lock_users(cpc_ptr);
raw_spin_lock_init(&cpc_ptr->rmw_lock);
/* Parse PSD data for this CPU */
ret = acpi_get_psd(cpc_ptr, handle);
if (ret)
goto out_free;
ret = cpc_register_sysmem_desc(cpc_ptr);
if (ret)
goto out_free;
/* Register PCC channel once for all PCC subspace ID. */
if (pcc_data_ref) {
ret = register_pcc_channel(pcc_subspace_id);
if (ret) {
pr_err("Failed to find PCC channel for subspace %d\n",
pcc_subspace_id);
goto out_free;
}
cpc_validate_pcc_bounds(cpc_ptr, pcc_subspace_id,
pcc_data[pcc_subspace_id],
&unsupported_regs);
ret = cpc_resolve_unsupported(cpc_ptr, unsupported_regs);
if (ret)
goto out_free;
/* A range-only status entry needs the channel only for bounds. */
if (!cpc_pcc_access_needed(cpc_ptr)) {
pcc_data_put(pcc_subspace_id);
pcc_data_ref = false;
per_cpu(cpu_pcc_subspace_idx, pr->id) = -1;
}
}
ret = cpc_validate_bound_controls(cpc_ptr);
if (ret)
goto out_free;
ret = cpc_register_non_mmio_desc(cpc_ptr);
if (ret)
goto out_free;
/* Everything looks okay */
pr_debug("Parsed CPC struct for CPU: %d\n", pr->id);
/* Add per logical CPU nodes for reading its feedback counters. */
cpu_dev = get_cpu_device(pr->id);
if (!cpu_dev) {
ret = -EINVAL;
goto out_free;
}
/* Plug PSD data into this CPU's CPC descriptor. */
cpc_set_desc(pr->id, cpc_ptr);
ret = kobject_init_and_add(&cpc_ptr->kobj, &cppc_ktype, &cpu_dev->kobj,
"acpi_cppc");
if (ret) {
cpc_set_desc(pr->id, NULL);
cpc_unregister_non_mmio_desc(cpc_ptr);
cpc_unregister_sysmem_desc(cpc_ptr);
kobject_put(&cpc_ptr->kobj);
goto out_pcc_put;
}
kfree(output.pointer);
return 0;
out_free:
cppc_free_desc(cpc_ptr);
out_pcc_put:
if (pcc_data_ref)
pcc_data_put(pcc_subspace_id);
per_cpu(cpu_pcc_subspace_idx, pr->id) = -1;
out_buf_free:
if (cpc_present)
pr_err("CPU%d: failed to initialize _CPC: %d\n", pr->id, ret);
kfree(output.pointer);
return ret;
}
EXPORT_SYMBOL_GPL(acpi_cppc_processor_probe);
/**
* acpi_cppc_processor_exit - Cleanup CPC structs.
* @pr: Ptr to acpi_processor containing this CPU's logical ID.
*
* Return: Void
*/
void acpi_cppc_processor_exit(struct acpi_processor *pr)
{
struct cpc_desc *cpc_ptr;
int pcc_ss_id;
cpc_ptr = per_cpu(cpc_desc_ptr, pr->id);
if (!cpc_ptr) {
per_cpu(cpu_pcc_subspace_idx, pr->id) = -1;
return;
}
pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, pr->id);
cpc_set_desc(pr->id, NULL);
kobject_del(&cpc_ptr->kobj);
cpc_unregister_non_mmio_desc(cpc_ptr);
cpc_unregister_sysmem_desc(cpc_ptr);
pcc_data_put(pcc_ss_id);
per_cpu(cpu_pcc_subspace_idx, pr->id) = -1;
kobject_put(&cpc_ptr->kobj);
}
EXPORT_SYMBOL_GPL(acpi_cppc_processor_exit);
/**
* cpc_read_ffh() - Read FFH register
* @cpunum: CPU number to read
* @reg: cppc register information
* @val: place holder for return value
*
* Read bit_width bits from a specified address and bit_offset
*
* Return: 0 for success and error code
*/
int __weak cpc_read_ffh(int cpunum, struct cpc_reg *reg, u64 *val)
{
return -ENOTSUPP;
}
/**
* cpc_read_ffh_fb_ctrs() - Read FFH feedback counters together
* @cpunum: Target CPU
* @reg1: first CPPC register information
* @val1: place holder for first return value
* @reg2: second CPPC register information
* @val2: place holder for second return value
*
* Return: 0 on success, error code otherwise
*/
int __weak cpc_read_ffh_fb_ctrs(int cpunum, struct cpc_reg *reg1,
u64 *val1, struct cpc_reg *reg2, u64 *val2)
{
return -EOPNOTSUPP;
}
/**
* cpc_write_ffh() - Write FFH register
* @cpunum: CPU number to write
* @reg: cppc register information
* @val: value to write
*
* Write value of bit_width bits to a specified address and bit_offset
*
* Return: 0 for success and error code
*/
int __weak cpc_write_ffh(int cpunum, struct cpc_reg *reg, u64 val)
{
return -ENOTSUPP;
}
/*
* Since cpc_read and cpc_write are called while holding pcc_lock, it should be
* as fast as possible. We have already mapped the PCC subspace during init, so
* we can directly write to it.
*/
static int cpc_read(int cpu, struct cpc_register_resource *reg_res, u64 *val)
{
void __iomem *vaddr = NULL;
unsigned long flags;
u8 buf[sizeof(*val)];
unsigned int i;
int size;
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
struct cpc_reg *reg = ®_res->cpc_entry.reg;
if (!cpc_is_readable(reg_res))
return -EOPNOTSUPP;
if (reg_res->type == ACPI_TYPE_INTEGER) {
*val = reg_res->cpc_entry.int_value;
return 0;
}
*val = 0;
if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
return cpc_read_ffh(cpu, reg, val);
size = GET_BIT_WIDTH(reg);
if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
u32 val_u32;
acpi_status status;
if (!IS_ENABLED(CONFIG_HAS_IOPORT))
return -EOPNOTSUPP;
status = acpi_os_read_port((acpi_io_address)reg->address,
&val_u32, size);
if (ACPI_FAILURE(status)) {
pr_debug("Error: Failed to read SystemIO port %llx\n",
reg->address);
return -EFAULT;
}
*val = MASK_VAL_READ(reg, val_u32);
return 0;
} else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
if (pcc_ss_id < 0 || !pcc_data[pcc_ss_id])
return -ENODEV;
/*
* For registers in PCC space, the register size is determined
* by the bit width field; the access size is used to indicate
* the PCC subspace id.
*/
vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
size = reg->bit_width / 8;
if (!size || size > sizeof(buf) || reg->bit_width % 8)
return -EFAULT;
raw_spin_lock_irqsave(&pcc_data[pcc_ss_id]->payload_lock, flags);
memcpy_fromio(buf, vaddr, size);
raw_spin_unlock_irqrestore(&pcc_data[pcc_ss_id]->payload_lock,
flags);
*val = 0;
for (i = 0; i < size; i++)
*val |= (u64)buf[i] << (i * 8);
return 0;
} else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
vaddr = reg_res->sys_mem_vaddr;
else
return acpi_os_read_memory((acpi_physical_address)reg->address,
val, size);
switch (size) {
case 8:
*val = readb_relaxed(vaddr);
break;
case 16:
*val = readw_relaxed(vaddr);
break;
case 32:
*val = readl_relaxed(vaddr);
break;
case 64:
*val = readq_relaxed(vaddr);
break;
default:
pr_debug("Error: Cannot read %u bit width from system memory: 0x%llx\n",
size, reg->address);
return -EFAULT;
}
*val = MASK_VAL_READ(reg, *val);
return 0;
}
static int cpc_write(int cpu, struct cpc_register_resource *reg_res, u64 val)
{
int ret_val = 0;
int size;
u64 prev_val;
void __iomem *vaddr = NULL;
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
struct cpc_reg *reg;
struct cpc_desc *cpc_desc;
unsigned long flags;
u8 buf[sizeof(val)];
unsigned int i;
bool locked = false;
if (!cpc_is_writable(reg_res))
return -EOPNOTSUPP;
reg = ®_res->cpc_entry.reg;
if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE)
return cpc_write_ffh(cpu, reg, val);
size = GET_BIT_WIDTH(reg);
if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
acpi_status status;
if (!IS_ENABLED(CONFIG_HAS_IOPORT))
return -EOPNOTSUPP;
status = acpi_os_write_port((acpi_io_address)reg->address,
(u32)val, size);
if (ACPI_FAILURE(status)) {
pr_debug("Error: Failed to write SystemIO port %llx\n",
reg->address);
return -EFAULT;
}
return 0;
} else if (reg->space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
if (pcc_ss_id < 0 || !pcc_data[pcc_ss_id])
return -ENODEV;
/*
* For registers in PCC space, the register size is determined
* by the bit width field; the access size is used to indicate
* the PCC subspace id.
*/
vaddr = GET_PCC_VADDR(reg->address, pcc_ss_id);
size = reg->bit_width / 8;
if (!size || size > sizeof(buf) || reg->bit_width % 8)
return -EFAULT;
for (i = 0; i < size; i++)
buf[i] = val >> (i * 8);
raw_spin_lock_irqsave(&pcc_data[pcc_ss_id]->payload_lock, flags);
memcpy_toio(vaddr, buf, size);
/* Publish every payload byte before another CPU can ring the doorbell. */
wmb();
raw_spin_unlock_irqrestore(&pcc_data[pcc_ss_id]->payload_lock,
flags);
return 0;
} else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
vaddr = reg_res->sys_mem_vaddr;
else
return acpi_os_write_memory((acpi_physical_address)reg->address,
val, size);
/* Partial fields and local overlaps use the descriptor lock. */
locked = reg_res->cpc_entry.use_rmw_lock;
if (locked) {
cpc_desc = per_cpu(cpc_desc_ptr, cpu);
if (!cpc_desc) {
pr_debug("No CPC descriptor for CPU:%d\n", cpu);
return -ENODEV;
}
raw_spin_lock_irqsave(&cpc_desc->rmw_lock, flags);
}
if (reg->bit_offset || reg->bit_width != size) {
/*
* MASK_VAL_WRITE() discards the field's old bits, so undefined
* readback from a write-only field is not propagated.
*/
switch (size) {
case 8:
prev_val = readb_relaxed(vaddr);
break;
case 16:
prev_val = readw_relaxed(vaddr);
break;
case 32:
prev_val = readl_relaxed(vaddr);
break;
case 64:
prev_val = readq_relaxed(vaddr);
break;
default:
if (locked)
raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock,
flags);
return -EFAULT;
}
val = MASK_VAL_WRITE(reg, prev_val, val);
}
switch (size) {
case 8:
writeb_relaxed(val, vaddr);
break;
case 16:
writew_relaxed(val, vaddr);
break;
case 32:
writel_relaxed(val, vaddr);
break;
case 64:
writeq_relaxed(val, vaddr);
break;
default:
pr_debug("Error: Cannot write %u bit width to system memory: 0x%llx\n",
size, reg->address);
ret_val = -EFAULT;
break;
}
if (locked) {
if (!ret_val)
mmiowb_set_pending();
raw_spin_unlock_irqrestore(&cpc_desc->rmw_lock, flags);
}
return ret_val;
}
static int cppc_get_reg_val_in_pcc(int cpu, struct cpc_register_resource *reg, u64 *val)
{
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
struct cppc_pcc_data *pcc_ss_data = NULL;
int ret;
if (pcc_ss_id < 0) {
pr_debug("Invalid pcc_ss_id\n");
return -ENODEV;
}
pcc_ss_data = pcc_data[pcc_ss_id];
down_write(&pcc_ss_data->pcc_lock);
if (send_pcc_cmd(pcc_ss_id, CMD_READ) >= 0)
ret = cpc_read(cpu, reg, val);
else
ret = -EIO;
up_write(&pcc_ss_data->pcc_lock);
return ret;
}
static int cppc_get_reg_val(int cpu, enum cppc_regs reg_idx, u64 *val)
{
struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
struct cpc_register_resource *reg;
if (val == NULL)
return -EINVAL;
if (!cpc_desc) {
pr_debug("No CPC descriptor for CPU:%d\n", cpu);
return -ENODEV;
}
if (cpc_reg_is_write_only(cpc_desc, reg_idx))
return -EOPNOTSUPP;
reg = &cpc_desc->cpc_regs[reg_idx];
/*
* Desired and Performance Limited may be disabled despite not being
* generally optional.
*/
if ((reg->type == ACPI_TYPE_INTEGER &&
(IS_OPTIONAL_CPC_REG(reg_idx) || reg_idx == DESIRED_PERF ||
reg_idx == PERF_LIMITED) &&
!reg->cpc_entry.int_value) || (reg->type != ACPI_TYPE_INTEGER &&
IS_NULL_REG(®->cpc_entry.reg))) {
pr_debug("CPC register is not supported\n");
return -EOPNOTSUPP;
}
if (!cpc_is_readable(reg))
return -EOPNOTSUPP;
if (CPC_IN_PCC(reg))
return cppc_get_reg_val_in_pcc(cpu, reg, val);
return cpc_read(cpu, reg, val);
}
static int cppc_set_reg_val_in_pcc(int cpu, struct cpc_register_resource *reg, u64 val)
{
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
struct cppc_pcc_data *pcc_ss_data;
int ret;
if (pcc_ss_id < 0) {
pr_debug("Invalid pcc_ss_id\n");
return -ENODEV;
}
pcc_ss_data = pcc_data[pcc_ss_id];
if (!pcc_ss_data)
return -ENODEV;
down_write(&pcc_ss_data->pcc_lock);
ret = check_pcc_chan(pcc_ss_id, false);
if (ret)
goto out;
ret = cpc_write(cpu, reg, val);
if (ret)
goto out;
/* after writing CPC, transfer the ownership of PCC to platform */
ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
out:
if (ret)
cppc_abort_pending_pcc_write(pcc_ss_id, pcc_ss_data, ret);
up_write(&pcc_ss_data->pcc_lock);
return ret;
}
static int cppc_set_reg_val(int cpu, enum cppc_regs reg_idx, u64 val)
{
struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
struct cpc_register_resource *reg;
if (!cpc_desc) {
pr_debug("No CPC descriptor for CPU:%d\n", cpu);
return -ENODEV;
}
reg = &cpc_desc->cpc_regs[reg_idx];
/* Integer 1 describes autonomous selection that is always enabled. */
if (reg_idx == AUTO_SEL_ENABLE && reg->type == ACPI_TYPE_INTEGER &&
reg->cpc_entry.int_value == 1)
return val == 1 ? 0 : -EOPNOTSUPP;
/* if a register is writeable, it must be a buffer and not null */
if (!cpc_is_writable(reg)) {
pr_debug("CPC register is not supported\n");
return -EOPNOTSUPP;
}
if (CPC_IN_PCC(reg))
return cppc_set_reg_val_in_pcc(cpu, reg, val);
return cpc_write(cpu, reg, val);
}
/**
* cppc_get_desired_perf - Get the desired performance register value.
* @cpunum: CPU from which to get desired performance.
* @desired_perf: Return address.
*
* Return: 0 for success, -EOPNOTSUPP for _CPC revision 4 or later, and a
* negative errno otherwise.
*/
int cppc_get_desired_perf(int cpunum, u64 *desired_perf)
{
return cppc_get_reg_val(cpunum, DESIRED_PERF, desired_perf);
}
EXPORT_SYMBOL_GPL(cppc_get_desired_perf);
/**
* cppc_get_nominal_perf - Get the nominal performance register value.
* @cpunum: CPU from which to get nominal performance.
* @nominal_perf: Return address.
*
* Return: 0 for success, -EIO otherwise.
*/
int cppc_get_nominal_perf(int cpunum, u64 *nominal_perf)
{
return cppc_get_reg_val(cpunum, NOMINAL_PERF, nominal_perf);
}
/**
* cppc_get_highest_perf - Get the highest performance register value.
* @cpunum: CPU from which to get highest performance.
* @highest_perf: Return address.
*
* Return: 0 for success, -EIO otherwise.
*/
int cppc_get_highest_perf(int cpunum, u64 *highest_perf)
{
return cppc_get_reg_val(cpunum, HIGHEST_PERF, highest_perf);
}
EXPORT_SYMBOL_GPL(cppc_get_highest_perf);
/**
* cppc_get_epp_perf - Get the epp register value.
* @cpunum: CPU from which to get epp preference value.
* @epp_perf: Return address.
*
* Return: 0 for success, -EIO otherwise.
*/
int cppc_get_epp_perf(int cpunum, u64 *epp_perf)
{
return cppc_get_reg_val(cpunum, ENERGY_PERF, epp_perf);
}
EXPORT_SYMBOL_GPL(cppc_get_epp_perf);
/**
* cppc_get_perf_caps - Get a CPU's performance capabilities.
* @cpunum: CPU from which to get capabilities info.
* @perf_caps: ptr to cppc_perf_caps. See cppc_acpi.h
*
* Return: 0 for success with perf_caps populated else -ERRNO.
*/
int cppc_get_perf_caps(int cpunum, struct cppc_perf_caps *perf_caps)
{
struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
struct cpc_register_resource *highest_reg, *lowest_reg,
*lowest_non_linear_reg, *nominal_reg, *reference_reg,
*guaranteed_reg, *low_freq_reg = NULL, *nom_freq_reg = NULL;
u64 high, low, guaranteed = 0, nom, ref, min_nonlinear,
low_f = 0, nom_f = 0;
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
struct cppc_pcc_data *pcc_ss_data = NULL;
int ret = 0, regs_in_pcc = 0;
if (!cpc_desc) {
pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
return -ENODEV;
}
highest_reg = &cpc_desc->cpc_regs[HIGHEST_PERF];
lowest_reg = &cpc_desc->cpc_regs[LOWEST_PERF];
lowest_non_linear_reg = &cpc_desc->cpc_regs[LOW_NON_LINEAR_PERF];
nominal_reg = &cpc_desc->cpc_regs[NOMINAL_PERF];
reference_reg = &cpc_desc->cpc_regs[REFERENCE_PERF];
low_freq_reg = &cpc_desc->cpc_regs[LOWEST_FREQ];
nom_freq_reg = &cpc_desc->cpc_regs[NOMINAL_FREQ];
guaranteed_reg = &cpc_desc->cpc_regs[GUARANTEED_PERF];
/* Are any of the regs PCC ?*/
if (CPC_IN_PCC(highest_reg) || CPC_IN_PCC(lowest_reg) ||
CPC_IN_PCC(lowest_non_linear_reg) || CPC_IN_PCC(nominal_reg) ||
(CPC_SUPPORTED(reference_reg) && CPC_IN_PCC(reference_reg)) ||
CPC_IN_PCC(low_freq_reg) || CPC_IN_PCC(nom_freq_reg) ||
CPC_IN_PCC(guaranteed_reg)) {
if (pcc_ss_id < 0) {
pr_debug("Invalid pcc_ss_id\n");
return -ENODEV;
}
pcc_ss_data = pcc_data[pcc_ss_id];
regs_in_pcc = 1;
down_write(&pcc_ss_data->pcc_lock);
/* Ring doorbell once to update PCC subspace */
if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
ret = -EIO;
goto out_err;
}
}
ret = cpc_read(cpunum, highest_reg, &high);
if (ret)
goto out_err;
perf_caps->highest_perf = high;
ret = cpc_read(cpunum, lowest_reg, &low);
if (ret)
goto out_err;
perf_caps->lowest_perf = low;
ret = cpc_read(cpunum, nominal_reg, &nom);
if (ret)
goto out_err;
perf_caps->nominal_perf = nom;
/*
* If reference perf register is not supported then we should
* use the nominal perf value
*/
if (CPC_SUPPORTED(reference_reg)) {
ret = cpc_read(cpunum, reference_reg, &ref);
if (ret)
goto out_err;
} else {
ref = nom;
}
perf_caps->reference_perf = ref;
if (guaranteed_reg->type != ACPI_TYPE_BUFFER ||
IS_NULL_REG(&guaranteed_reg->cpc_entry.reg)) {
perf_caps->guaranteed_perf = 0;
} else {
ret = cpc_read(cpunum, guaranteed_reg, &guaranteed);
if (ret)
goto out_err;
perf_caps->guaranteed_perf = guaranteed;
}
ret = cpc_read(cpunum, lowest_non_linear_reg, &min_nonlinear);
if (ret)
goto out_err;
perf_caps->lowest_nonlinear_perf = min_nonlinear;
if (!high || !low || !nom || !ref || !min_nonlinear ||
high > U32_MAX || low > U32_MAX || guaranteed > U32_MAX ||
nom > U32_MAX || ref > U32_MAX || min_nonlinear > U32_MAX ||
high < nom || nom < min_nonlinear || min_nonlinear < low ||
(CPC_SUPPORTED(guaranteed_reg) &&
(guaranteed < low || guaranteed > nom))) {
ret = -EFAULT;
goto out_err;
}
/* Read optional lowest and nominal frequencies if present */
if (CPC_SUPPORTED(low_freq_reg)) {
ret = cpc_read(cpunum, low_freq_reg, &low_f);
if (ret)
goto out_err;
}
if (CPC_SUPPORTED(nom_freq_reg)) {
ret = cpc_read(cpunum, nom_freq_reg, &nom_f);
if (ret)
goto out_err;
}
/* Require ordered anchors and a nonzero slope when frequencies differ. */
if (low_f > U32_MAX || nom_f > U32_MAX ||
(low_f && nom_f &&
(nom_f < low_f || nom < low ||
(nom_f != low_f && nom == low)))) {
ret = -EFAULT;
goto out_err;
}
perf_caps->lowest_freq = low_f;
perf_caps->nominal_freq = nom_f;
out_err:
if (regs_in_pcc)
up_write(&pcc_ss_data->pcc_lock);
return ret;
}
EXPORT_SYMBOL_GPL(cppc_get_perf_caps);
/**
* cppc_perf_ctrs_in_pcc_cpu - Check if any perf counters of a CPU are in PCC.
* @cpu: CPU on which to check perf counters.
*
* Return: true if any of the counters are in PCC regions, false otherwise
*/
bool cppc_perf_ctrs_in_pcc_cpu(unsigned int cpu)
{
struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
if (!cpc_desc)
return false;
return CPC_IN_PCC(&cpc_desc->cpc_regs[DELIVERED_CTR]) ||
CPC_IN_PCC(&cpc_desc->cpc_regs[REFERENCE_CTR]) ||
CPC_IN_PCC(&cpc_desc->cpc_regs[CTR_WRAP_TIME]);
}
EXPORT_SYMBOL_GPL(cppc_perf_ctrs_in_pcc_cpu);
static int cppc_read_fb_ctrs(int cpunum,
struct cpc_register_resource *delivered_reg,
struct cpc_register_resource *reference_reg,
u64 *delivered, u64 *reference)
{
int ret;
/*
* For FFH feedback counters, try a paired read first to reduce
* sampling skew between delivered and reference counters. Fall
* back to the existing per-register reads if unsupported.
*/
if (CPC_IN_FFH(delivered_reg) && CPC_IN_FFH(reference_reg)) {
ret = cpc_read_ffh_fb_ctrs(cpunum,
&delivered_reg->cpc_entry.reg, delivered,
&reference_reg->cpc_entry.reg, reference);
if (ret != -EOPNOTSUPP)
return ret;
}
ret = cpc_read(cpunum, delivered_reg, delivered);
if (ret)
return ret;
return cpc_read(cpunum, reference_reg, reference);
}
/**
* cppc_perf_ctrs_in_pcc - Check if any perf counters are in a PCC region.
*
* CPPC has flexibility about how CPU performance counters are accessed.
* One of the choices is PCC regions, which can have a high access latency. This
* routine allows callers of cppc_get_perf_ctrs() to know this ahead of time.
*
* Return: true if any of the counters are in PCC regions, false otherwise
*/
bool cppc_perf_ctrs_in_pcc(void)
{
int cpu;
for_each_online_cpu(cpu) {
if (cppc_perf_ctrs_in_pcc_cpu(cpu))
return true;
}
return false;
}
EXPORT_SYMBOL_GPL(cppc_perf_ctrs_in_pcc);
/**
* cppc_get_perf_ctrs - Read a CPU's performance feedback counters.
* @cpunum: CPU from which to read counters.
* @perf_fb_ctrs: ptr to cppc_perf_fb_ctrs. See cppc_acpi.h
*
* Return: 0 for success with perf_fb_ctrs populated else -ERRNO.
*/
int cppc_get_perf_ctrs(int cpunum, struct cppc_perf_fb_ctrs *perf_fb_ctrs)
{
struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpunum);
struct cpc_register_resource *delivered_reg, *reference_reg,
*ctr_wrap_reg;
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpunum);
struct cppc_pcc_data *pcc_ss_data = NULL;
u64 delivered, reference, ctr_wrap_time;
int ret = 0, regs_in_pcc = 0;
if (!cpc_desc) {
pr_debug("No CPC descriptor for CPU:%d\n", cpunum);
return -ENODEV;
}
delivered_reg = &cpc_desc->cpc_regs[DELIVERED_CTR];
reference_reg = &cpc_desc->cpc_regs[REFERENCE_CTR];
ctr_wrap_reg = &cpc_desc->cpc_regs[CTR_WRAP_TIME];
/* Are any of the regs PCC ?*/
if (CPC_IN_PCC(delivered_reg) || CPC_IN_PCC(reference_reg) ||
CPC_IN_PCC(ctr_wrap_reg)) {
if (pcc_ss_id < 0) {
pr_debug("Invalid pcc_ss_id\n");
return -ENODEV;
}
pcc_ss_data = pcc_data[pcc_ss_id];
down_write(&pcc_ss_data->pcc_lock);
regs_in_pcc = 1;
/* Ring doorbell once to update PCC subspace */
if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
ret = -EIO;
goto out_err;
}
}
ret = cppc_read_fb_ctrs(cpunum, delivered_reg, reference_reg,
&delivered, &reference);
if (ret)
goto out_err;
/*
* Per spec, if ctr_wrap_time optional register is unsupported, then the
* performance counters are assumed to never wrap during the lifetime of
* platform
*/
ctr_wrap_time = (u64)(~((u64)0));
if (CPC_SUPPORTED(ctr_wrap_reg)) {
ret = cpc_read(cpunum, ctr_wrap_reg, &ctr_wrap_time);
if (ret)
goto out_err;
}
if (!delivered || !reference) {
ret = -EFAULT;
goto out_err;
}
perf_fb_ctrs->delivered = delivered;
perf_fb_ctrs->reference = reference;
perf_fb_ctrs->wraparound_time = ctr_wrap_time;
out_err:
if (regs_in_pcc)
up_write(&pcc_ss_data->pcc_lock);
return ret;
}
EXPORT_SYMBOL_GPL(cppc_get_perf_ctrs);
/*
* Set Energy Performance Preference Register value through
* Performance Controls Interface
*/
int cppc_set_epp_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls, bool enable)
{
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
struct cpc_register_resource *epp_set_reg;
struct cpc_register_resource *auto_sel_reg;
struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
struct cppc_pcc_data *pcc_ss_data = NULL;
bool auto_sel_pcc;
bool auto_sel_non_pcc;
bool epp_pcc;
bool epp_non_pcc;
int ret;
if (!cpc_desc) {
pr_debug("No CPC descriptor for CPU:%d\n", cpu);
return -ENODEV;
}
auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
epp_set_reg = &cpc_desc->cpc_regs[ENERGY_PERF];
if (!enable && auto_sel_reg->type == ACPI_TYPE_INTEGER &&
auto_sel_reg->cpc_entry.int_value == 1)
return -EOPNOTSUPP;
auto_sel_pcc = cpc_is_writable(auto_sel_reg) &&
CPC_IN_PCC(auto_sel_reg);
epp_pcc = cpc_is_writable(epp_set_reg) && CPC_IN_PCC(epp_set_reg);
auto_sel_non_pcc = cpc_is_writable(auto_sel_reg) && !auto_sel_pcc;
epp_non_pcc = cpc_is_writable(epp_set_reg) && !epp_pcc;
/* Complete fallible non-PCC writes before staging PCC data. */
if (auto_sel_non_pcc) {
ret = cpc_write(cpu, auto_sel_reg, enable);
if (ret)
return ret;
}
if (epp_non_pcc) {
ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf);
if (ret)
return ret;
}
if (epp_pcc || auto_sel_pcc) {
if (pcc_ss_id < 0) {
pr_debug("Invalid pcc_ss_id for CPU:%d\n", cpu);
return -ENODEV;
}
pcc_ss_data = pcc_data[pcc_ss_id];
if (!pcc_ss_data)
return -ENODEV;
down_write(&pcc_ss_data->pcc_lock);
ret = check_pcc_chan(pcc_ss_id, false);
if (ret)
goto out_unlock;
if (auto_sel_pcc) {
ret = cpc_write(cpu, auto_sel_reg, enable);
if (ret)
goto out_unlock;
}
if (epp_pcc) {
ret = cpc_write(cpu, epp_set_reg, perf_ctrls->energy_perf);
if (ret)
goto out_unlock;
}
/* after writing CPC, transfer the ownership of PCC to platform */
ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
out_unlock:
if (ret)
cppc_abort_pending_pcc_write(pcc_ss_id, pcc_ss_data, ret);
up_write(&pcc_ss_data->pcc_lock);
} else if (epp_non_pcc || auto_sel_non_pcc) {
ret = 0;
} else {
ret = -EOPNOTSUPP;
pr_debug("No writable EPP controls for CPU:%d\n", cpu);
}
return ret;
}
EXPORT_SYMBOL_GPL(cppc_set_epp_perf);
/**
* cppc_set_epp() - Write the EPP register.
* @cpu: CPU on which to write register.
* @epp_val: Value to write to the EPP register.
*/
int cppc_set_epp(int cpu, u64 epp_val)
{
if (epp_val > CPPC_EPP_ENERGY_EFFICIENCY_PREF)
return -EINVAL;
return cppc_set_reg_val(cpu, ENERGY_PERF, epp_val);
}
EXPORT_SYMBOL_GPL(cppc_set_epp);
/**
* cppc_get_auto_act_window() - Read autonomous activity window register.
* @cpu: CPU from which to read register.
* @auto_act_window: Return address.
*
* According to ACPI 6.5, s8.4.6.1.6, the value read from the autonomous
* activity window register consists of two parts: a 7 bits value indicate
* significand and a 3 bits value indicate exponent.
*/
int cppc_get_auto_act_window(int cpu, u64 *auto_act_window)
{
unsigned int exp;
u64 val, sig;
int ret;
if (auto_act_window == NULL)
return -EINVAL;
ret = cppc_get_reg_val(cpu, AUTO_ACT_WINDOW, &val);
if (ret)
return ret;
sig = val & CPPC_AUTO_ACT_WINDOW_MAX_SIG;
exp = (val >> CPPC_AUTO_ACT_WINDOW_SIG_BIT_SIZE) & CPPC_AUTO_ACT_WINDOW_MAX_EXP;
*auto_act_window = sig * int_pow(10, exp);
return 0;
}
EXPORT_SYMBOL_GPL(cppc_get_auto_act_window);
/**
* cppc_set_auto_act_window() - Write autonomous activity window register.
* @cpu: CPU on which to write register.
* @auto_act_window: usec value to write to the autonomous activity window register.
*
* According to ACPI 6.5, s8.4.6.1.6, the value to write to the autonomous
* activity window register consists of two parts: a 7 bits value indicate
* significand and a 3 bits value indicate exponent.
*/
int cppc_set_auto_act_window(int cpu, u64 auto_act_window)
{
/* The max value to store is 1270000000 */
u64 max_val = CPPC_AUTO_ACT_WINDOW_MAX_SIG * int_pow(10, CPPC_AUTO_ACT_WINDOW_MAX_EXP);
int exp = 0;
u64 val;
if (auto_act_window > max_val)
return -EINVAL;
/*
* The max significand is 127, when auto_act_window is larger than
* 129, discard the precision of the last digit and increase the
* exponent by 1.
*/
while (auto_act_window > CPPC_AUTO_ACT_WINDOW_SIG_CARRY_THRESH) {
auto_act_window /= 10;
exp += 1;
}
/* For 128 and 129, cut it to 127. */
if (auto_act_window > CPPC_AUTO_ACT_WINDOW_MAX_SIG)
auto_act_window = CPPC_AUTO_ACT_WINDOW_MAX_SIG;
val = (exp << CPPC_AUTO_ACT_WINDOW_SIG_BIT_SIZE) + auto_act_window;
return cppc_set_reg_val(cpu, AUTO_ACT_WINDOW, val);
}
EXPORT_SYMBOL_GPL(cppc_set_auto_act_window);
/**
* cppc_get_auto_sel() - Read autonomous selection register.
* @cpu: CPU from which to read register.
* @enable: Return address.
*/
int cppc_get_auto_sel(int cpu, bool *enable)
{
u64 auto_sel;
int ret;
if (enable == NULL)
return -EINVAL;
ret = cppc_get_reg_val(cpu, AUTO_SEL_ENABLE, &auto_sel);
if (ret)
return ret;
*enable = (bool)auto_sel;
return 0;
}
EXPORT_SYMBOL_GPL(cppc_get_auto_sel);
/**
* cppc_auto_sel_is_immutable - Check for always-enabled autonomous selection.
* @cpu: CPU whose _CPC descriptor to check.
*
* Context: Process context.
* Return: true for Integer 1, false for a register or an absent descriptor.
*/
bool cppc_auto_sel_is_immutable(int cpu)
{
struct cpc_desc *cpc_desc;
struct cpc_register_resource *reg;
guard(mutex)(&cpc_desc_lock);
cpc_desc = per_cpu(cpc_desc_ptr, cpu);
if (!cpc_desc)
return false;
reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
return reg->type == ACPI_TYPE_INTEGER && reg->cpc_entry.int_value == 1;
}
EXPORT_SYMBOL_GPL(cppc_auto_sel_is_immutable);
/**
* cppc_set_auto_sel - Write autonomous selection register.
* @cpu : CPU to which to write register.
* @enable : the desired value of autonomous selection resiter to be updated.
*/
int cppc_set_auto_sel(int cpu, bool enable)
{
return cppc_set_reg_val(cpu, AUTO_SEL_ENABLE, enable);
}
EXPORT_SYMBOL_GPL(cppc_set_auto_sel);
/**
* cppc_set_enable - Set to enable CPPC on the processor by writing the
* Continuous Performance Control package EnableRegister field.
* @cpu: CPU for which to enable CPPC register.
* @enable: 0 - disable, 1 - enable CPPC feature on the processor.
*
* Return: 0 for success, -ERRNO or -EIO otherwise.
*/
int cppc_set_enable(int cpu, bool enable)
{
return cppc_set_reg_val(cpu, ENABLE, enable);
}
EXPORT_SYMBOL_GPL(cppc_set_enable);
/**
* cppc_get_perf - Get a CPU's performance controls.
* @cpu: CPU for which to get performance controls.
* @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
*
* Desired Performance is not read and is returned as 0.
*
* Return: 0 for success with perf_ctrls, -ERRNO otherwise.
*/
int cppc_get_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
{
struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
struct cpc_register_resource *min_perf_reg, *max_perf_reg,
*energy_perf_reg, *auto_sel_reg;
u64 min = 0, max = 0, energy_perf = 0, auto_sel = 0;
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
struct cppc_pcc_data *pcc_ss_data = NULL;
int ret = 0, regs_in_pcc = 0;
if (!cpc_desc) {
pr_debug("No CPC descriptor for CPU:%d\n", cpu);
return -ENODEV;
}
if (!perf_ctrls) {
pr_debug("Invalid perf_ctrls pointer\n");
return -EINVAL;
}
min_perf_reg = &cpc_desc->cpc_regs[MIN_PERF];
max_perf_reg = &cpc_desc->cpc_regs[MAX_PERF];
energy_perf_reg = &cpc_desc->cpc_regs[ENERGY_PERF];
auto_sel_reg = &cpc_desc->cpc_regs[AUTO_SEL_ENABLE];
perf_ctrls->min_perf_valid = false;
/* Are any of the regs PCC ?*/
if (CPC_IN_PCC(min_perf_reg) || CPC_IN_PCC(max_perf_reg) ||
CPC_IN_PCC(energy_perf_reg) ||
CPC_IN_PCC(auto_sel_reg)) {
if (pcc_ss_id < 0) {
pr_debug("Invalid pcc_ss_id for CPU:%d\n", cpu);
return -ENODEV;
}
pcc_ss_data = pcc_data[pcc_ss_id];
regs_in_pcc = 1;
down_write(&pcc_ss_data->pcc_lock);
/* Ring doorbell once to update PCC subspace */
if (send_pcc_cmd(pcc_ss_id, CMD_READ) < 0) {
ret = -EIO;
goto out_err;
}
}
/* Read optional elements if present */
if (CPC_SUPPORTED(max_perf_reg)) {
ret = cpc_read(cpu, max_perf_reg, &max);
if (ret)
goto out_err;
if (max > U32_MAX) {
ret = -EFAULT;
goto out_err;
}
}
perf_ctrls->max_perf = max;
if (CPC_SUPPORTED(min_perf_reg)) {
ret = cpc_read(cpu, min_perf_reg, &min);
if (ret)
goto out_err;
if (min > U32_MAX) {
ret = -EFAULT;
goto out_err;
}
perf_ctrls->min_perf_valid = true;
}
perf_ctrls->min_perf = min;
perf_ctrls->desired_perf = 0;
if (CPC_SUPPORTED(energy_perf_reg)) {
ret = cpc_read(cpu, energy_perf_reg, &energy_perf);
if (ret)
goto out_err;
}
perf_ctrls->energy_perf = energy_perf;
if (CPC_SUPPORTED(auto_sel_reg)) {
ret = cpc_read(cpu, auto_sel_reg, &auto_sel);
if (ret)
goto out_err;
}
perf_ctrls->auto_sel = (bool)auto_sel;
out_err:
if (regs_in_pcc)
up_write(&pcc_ss_data->pcc_lock);
return ret;
}
EXPORT_SYMBOL_GPL(cppc_get_perf);
/**
* cppc_set_perf - Set a CPU's performance controls.
* @cpu: CPU for which to set performance controls.
* @perf_ctrls: ptr to cppc_perf_ctrls. See cppc_acpi.h
*
* Return: 0 for success, -ERRNO otherwise.
*/
int cppc_set_perf(int cpu, struct cppc_perf_ctrls *perf_ctrls)
{
struct cpc_desc *cpc_desc = per_cpu(cpc_desc_ptr, cpu);
struct cpc_register_resource *desired_reg, *min_perf_reg, *max_perf_reg;
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu);
struct cppc_pcc_data *pcc_ss_data = NULL;
bool desired_update, min_update, max_update;
bool desired_pcc, min_pcc, max_pcc, pcc_update;
bool pcc_layout, direct_layout, mixed_layout;
int ret = 0;
if (!cpc_desc) {
pr_debug("No CPC descriptor for CPU:%d\n", cpu);
return -ENODEV;
}
desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
min_perf_reg = &cpc_desc->cpc_regs[MIN_PERF];
max_perf_reg = &cpc_desc->cpc_regs[MAX_PERF];
desired_update = cpc_is_writable(desired_reg);
min_update = cpc_is_writable(min_perf_reg) &&
(perf_ctrls->min_perf || perf_ctrls->min_perf_valid);
max_update = cpc_is_writable(max_perf_reg) &&
perf_ctrls->max_perf;
desired_pcc = desired_update && CPC_IN_PCC(desired_reg);
min_pcc = min_update && CPC_IN_PCC(min_perf_reg);
max_pcc = max_update && CPC_IN_PCC(max_perf_reg);
pcc_update = desired_pcc || min_pcc || max_pcc;
pcc_layout = (cpc_is_writable(desired_reg) && CPC_IN_PCC(desired_reg)) ||
(cpc_is_writable(min_perf_reg) && CPC_IN_PCC(min_perf_reg)) ||
(cpc_is_writable(max_perf_reg) && CPC_IN_PCC(max_perf_reg));
direct_layout = (cpc_is_writable(desired_reg) &&
!CPC_IN_PCC(desired_reg)) ||
(cpc_is_writable(min_perf_reg) &&
!CPC_IN_PCC(min_perf_reg)) ||
(cpc_is_writable(max_perf_reg) &&
!CPC_IN_PCC(max_perf_reg));
mixed_layout = pcc_layout && direct_layout;
if (mixed_layout || pcc_update) {
if (pcc_ss_id < 0) {
pr_debug("Invalid pcc_ss_id\n");
return -ENODEV;
}
pcc_ss_data = pcc_data[pcc_ss_id];
if (!pcc_ss_data)
return -ENODEV;
}
/*
* A mixed layout cannot batch fallible direct writes safely: another
* CPU's staged PCC values may no longer match if a direct write fails.
* Serialize the complete mixed transaction and drain an older batch
* before changing a direct control.
*/
if (mixed_layout) {
down_write(&pcc_ss_data->pcc_lock);
if (pcc_ss_data->pending_pcc_write_cmd) {
ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
if (ret)
goto out_mixed_unlock;
}
if (pcc_ss_data->platform_owns_pcc) {
ret = check_pcc_chan(pcc_ss_id, false);
if (ret)
goto out_mixed_unlock;
}
if (desired_update && !desired_pcc) {
ret = cpc_write(cpu, desired_reg,
perf_ctrls->desired_perf);
if (ret)
goto out_mixed_unlock;
}
if (min_update && !min_pcc) {
ret = cpc_write(cpu, min_perf_reg,
perf_ctrls->min_perf);
if (ret)
goto out_mixed_unlock;
}
if (max_update && !max_pcc) {
ret = cpc_write(cpu, max_perf_reg,
perf_ctrls->max_perf);
if (ret)
goto out_mixed_unlock;
}
if (desired_pcc) {
ret = cpc_write(cpu, desired_reg,
perf_ctrls->desired_perf);
if (ret)
goto out_mixed_unlock;
}
if (min_pcc) {
ret = cpc_write(cpu, min_perf_reg,
perf_ctrls->min_perf);
if (ret)
goto out_mixed_unlock;
}
if (max_pcc) {
ret = cpc_write(cpu, max_perf_reg,
perf_ctrls->max_perf);
if (ret)
goto out_mixed_unlock;
}
if (pcc_update) {
WRITE_ONCE(pcc_ss_data->pending_pcc_write_cmd, true);
cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt;
cpc_desc->write_cmd_status = 0;
ret = send_pcc_cmd(pcc_ss_id, CMD_WRITE);
}
out_mixed_unlock:
up_write(&pcc_ss_data->pcc_lock);
return ret;
}
/* A request without PCC updates has no payload to coordinate. */
if (!pcc_update) {
if (desired_update) {
ret = cpc_write(cpu, desired_reg,
perf_ctrls->desired_perf);
if (ret)
return ret;
}
if (min_update) {
ret = cpc_write(cpu, min_perf_reg,
perf_ctrls->min_perf);
if (ret)
return ret;
}
if (max_update)
ret = cpc_write(cpu, max_perf_reg,
perf_ctrls->max_perf);
return ret;
}
down_read(&pcc_ss_data->pcc_lock); /* BEGIN Phase-I */
if (pcc_ss_data->platform_owns_pcc) {
ret = check_pcc_chan(pcc_ss_id, false);
if (ret)
goto out_pcc_read_unlock;
}
/*
* This is Phase-I where we want to write to CPC registers
* -> We want all CPUs to be able to execute this phase in parallel
*
* Since read_lock can be acquired by multiple CPUs simultaneously we
* achieve that goal here.
*/
if (desired_pcc) {
ret = cpc_write(cpu, desired_reg, perf_ctrls->desired_perf);
if (ret)
goto out_pcc_read_unlock;
}
if (min_pcc) {
ret = cpc_write(cpu, min_perf_reg, perf_ctrls->min_perf);
if (ret)
goto out_pcc_read_unlock;
}
if (max_pcc) {
ret = cpc_write(cpu, max_perf_reg, perf_ctrls->max_perf);
if (ret)
goto out_pcc_read_unlock;
}
/* Block a PCC read until the staged payload has been submitted. */
WRITE_ONCE(pcc_ss_data->pending_pcc_write_cmd, true);
cpc_desc->write_cmd_id = pcc_ss_data->pcc_write_cnt;
cpc_desc->write_cmd_status = 0;
up_read(&pcc_ss_data->pcc_lock); /* END Phase-I */
/*
* This is Phase-II where we transfer the ownership of PCC to Platform
*
* Short Summary: Basically if we think of a group of cppc_set_perf
* requests that happened in short overlapping interval. The last CPU to
* come out of Phase-I will enter Phase-II and ring the doorbell.
*
* We have the following requirements for Phase-II:
* 1. We want to execute Phase-II only when there are no CPUs
* currently executing in Phase-I
* 2. Once we start Phase-II we want to avoid all other CPUs from
* entering Phase-I.
* 3. We want only one CPU among all those who went through Phase-I
* to run phase-II
*
* If write_trylock fails to get the lock and doesn't transfer the
* PCC ownership to the platform, then one of the following will be TRUE
* 1. There is at-least one CPU in Phase-I which will later execute
* write_trylock, so the CPUs in Phase-I will be responsible for
* executing the Phase-II.
* 2. Some other CPU has beaten this CPU to successfully execute the
* write_trylock and has already acquired the write_lock. We know for a
* fact it (other CPU acquiring the write_lock) couldn't have happened
* before this CPU's Phase-I as we held the read_lock.
* 3. Some other CPU executing pcc CMD_READ has stolen the
* down_write, in which case, send_pcc_cmd will check for pending
* CMD_WRITE commands by checking the pending_pcc_write_cmd.
* So this CPU can be certain that its request will be delivered
* So in all cases, this CPU knows that its request will be delivered
* by another CPU and can return
*
* After getting the down_write we still need to check for
* pending_pcc_write_cmd to take care of the following scenario
* The thread running this code could be scheduled out between
* Phase-I and Phase-II. Before it is scheduled back on, another CPU
* could have delivered the request to Platform by triggering the
* doorbell and transferred the ownership of PCC to platform. So this
* avoids triggering an unnecessary doorbell and more importantly before
* triggering the doorbell it makes sure that the PCC channel ownership
* is still with OSPM.
* pending_pcc_write_cmd can also be cleared by a different CPU, if
* there was a pcc CMD_READ waiting on down_write and it steals the lock
* before the pcc CMD_WRITE is completed. send_pcc_cmd checks for this
* case during a CMD_READ and if there are pending writes it delivers
* the write command before servicing the read command
*/
if (down_write_trylock(&pcc_ss_data->pcc_lock)) {/* BEGIN Phase-II */
/* Update only if there are pending write commands */
if (pcc_ss_data->pending_pcc_write_cmd)
send_pcc_cmd(pcc_ss_id, CMD_WRITE);
up_write(&pcc_ss_data->pcc_lock); /* END Phase-II */
} else {
/* Wait until pcc_write_cnt is updated by send_pcc_cmd */
wait_event(pcc_ss_data->pcc_write_wait_q,
cpc_desc->write_cmd_id != pcc_ss_data->pcc_write_cnt);
}
/* send_pcc_cmd updates the status in case of failure */
return cpc_desc->write_cmd_status;
out_pcc_read_unlock:
up_read(&pcc_ss_data->pcc_lock);
return ret;
}
EXPORT_SYMBOL_GPL(cppc_set_perf);
/**
* cppc_get_perf_limited - Get the Performance Limited register value.
* @cpu: CPU from which to get Performance Limited register.
* @perf_limited: Pointer to store the Performance Limited value.
*
* The returned value contains sticky status bits indicating platform-imposed
* performance limitations.
*
* Return: 0 for success, -EIO on failure, -EOPNOTSUPP if not supported.
*/
int cppc_get_perf_limited(int cpu, u64 *perf_limited)
{
return cppc_get_reg_val(cpu, PERF_LIMITED, perf_limited);
}
EXPORT_SYMBOL_GPL(cppc_get_perf_limited);
/**
* cppc_set_perf_limited() - Clear bits in the Performance Limited register.
* @cpu: CPU on which to write register.
* @bits_to_clear: Zero for no-op or CPPC_PERF_LIMITED_MASK to clear both bits.
*
* The Performance Limited register contains two sticky bits set by platform:
* - Bit 0 (Desired_Excursion): Set when delivered performance is constrained
* below desired performance. Not used when Autonomous Selection is enabled.
* - Bit 1 (Minimum_Excursion): Set when delivered performance is constrained
* below minimum performance.
*
* These bits are sticky and remain set until OSPM explicitly clears them.
* Selective clears are unsupported because they require an interlocked RMW.
*
* Return: 0 for success, -EINVAL for invalid bits, -EIO on register
* access failure, -EOPNOTSUPP if not supported.
*/
int cppc_set_perf_limited(int cpu, u64 bits_to_clear)
{
/* Only bits 0 and 1 are valid */
if (bits_to_clear & ~(u64)CPPC_PERF_LIMITED_MASK)
return -EINVAL;
if (!bits_to_clear)
return 0;
/*
* Writing zero clears both bits without depending on how a platform
* treats written ones. ACPI does not define the effect of writing one,
* so a selective clear cannot be implemented without an interlocked RMW.
*/
if (bits_to_clear != CPPC_PERF_LIMITED_MASK)
return -EOPNOTSUPP;
return cppc_set_reg_val(cpu, PERF_LIMITED, 0);
}
EXPORT_SYMBOL_GPL(cppc_set_perf_limited);
/**
* cppc_get_transition_latency - returns frequency transition latency in ns
* @cpu_num: CPU number for per_cpu().
*
* ACPI CPPC does not explicitly specify how a platform can specify the
* transition latency for performance change requests. The closest we have
* is the timing information from the PCCT tables which provides the info
* on the number and frequency of PCC commands the platform can handle.
*
* If desired_reg is in the SystemMemory or SystemIo ACPI address space,
* then assume there is no latency.
*/
int cppc_get_transition_latency(int cpu_num)
{
/*
* Expected transition latency is based on the PCCT timing values
* Below are definition from ACPI spec:
* pcc_nominal- Expected latency to process a command, in microseconds
* pcc_mpar - The maximum number of periodic requests that the subspace
* channel can support, reported in commands per minute. 0
* indicates no limitation.
* pcc_mrtt - The minimum amount of time that OSPM must wait after the
* completion of a command before issuing the next command,
* in microseconds.
*/
struct cpc_desc *cpc_desc;
struct cpc_register_resource *desired_reg;
int pcc_ss_id = per_cpu(cpu_pcc_subspace_idx, cpu_num);
struct cppc_pcc_data *pcc_ss_data;
int latency_ns = 0;
cpc_desc = per_cpu(cpc_desc_ptr, cpu_num);
if (!cpc_desc)
return -ENODATA;
desired_reg = &cpc_desc->cpc_regs[DESIRED_PERF];
if (!cpc_is_writable(desired_reg))
return -ENODATA;
if (CPC_IN_SYSTEM_MEMORY(desired_reg) || CPC_IN_SYSTEM_IO(desired_reg))
return 0;
if (!CPC_IN_PCC(desired_reg) || pcc_ss_id < 0)
return -ENODATA;
pcc_ss_data = pcc_data[pcc_ss_id];
if (pcc_ss_data->pcc_mpar)
latency_ns = 60 * (1000 * 1000 * 1000 / pcc_ss_data->pcc_mpar);
latency_ns = max_t(int, latency_ns, pcc_ss_data->pcc_nominal * 1000);
latency_ns = max_t(int, latency_ns, pcc_ss_data->pcc_mrtt * 1000);
return latency_ns;
}
EXPORT_SYMBOL_GPL(cppc_get_transition_latency);
/* Minimum struct length needed for the DMI processor entry we want */
#define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
/* Offset in the DMI processor structure for the max frequency */
#define DMI_PROCESSOR_MAX_SPEED 0x14
/* Callback function used to retrieve the max frequency from DMI */
static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
{
const u8 *dmi_data = (const u8 *)dm;
u16 *mhz = (u16 *)private;
if (dm->type == DMI_ENTRY_PROCESSOR &&
dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
u16 val = (u16)get_unaligned((const u16 *)
(dmi_data + DMI_PROCESSOR_MAX_SPEED));
*mhz = umax(val, *mhz);
}
}
/* Look up the max frequency in DMI */
u64 cppc_get_dmi_max_khz(void)
{
u16 mhz = 0;
dmi_walk(cppc_find_dmi_mhz, &mhz);
/*
* Real stupid fallback value, just in case there is no
* actual value set.
*/
mhz = mhz ? mhz : 1;
return KHZ_PER_MHZ * mhz;
}
EXPORT_SYMBOL_GPL(cppc_get_dmi_max_khz);
/*
* If CPPC lowest_freq and nominal_freq registers are exposed then we can
* use them to convert perf to freq and vice versa. The conversion is
* extrapolated as an affine function passing by the 2 points:
* - (Low perf, Low freq)
* - (Nominal perf, Nominal freq)
*/
unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned int perf)
{
s64 retval, offset = 0;
static u64 max_khz;
u64 mul, div;
if (caps->lowest_freq && caps->nominal_freq) {
/* Avoid special case when nominal_freq is equal to lowest_freq */
if (caps->lowest_freq == caps->nominal_freq) {
mul = caps->nominal_freq;
div = caps->nominal_perf;
} else {
mul = caps->nominal_freq - caps->lowest_freq;
div = caps->nominal_perf - caps->lowest_perf;
}
mul *= KHZ_PER_MHZ;
offset = caps->nominal_freq * KHZ_PER_MHZ -
div64_u64(caps->nominal_perf * mul, div);
} else {
if (!max_khz)
max_khz = cppc_get_dmi_max_khz();
mul = max_khz;
div = caps->highest_perf;
}
retval = offset + div64_u64(perf * mul, div);
if (retval >= 0)
return retval;
return 0;
}
EXPORT_SYMBOL_GPL(cppc_perf_to_khz);
unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned int freq)
{
s64 retval, offset = 0;
static u64 max_khz;
u64 mul, div;
if (caps->lowest_freq && caps->nominal_freq) {
/* Avoid special case when nominal_freq is equal to lowest_freq */
if (caps->lowest_freq == caps->nominal_freq) {
mul = caps->nominal_perf;
div = caps->nominal_freq;
} else {
mul = caps->nominal_perf - caps->lowest_perf;
div = caps->nominal_freq - caps->lowest_freq;
}
/*
* We don't need to convert to kHz for computing offset and can
* directly use nominal_freq and lowest_freq as the div64_u64
* will remove the frequency unit.
*/
offset = caps->nominal_perf -
div64_u64(caps->nominal_freq * mul, div);
/* But we need it for computing the perf level. */
div *= KHZ_PER_MHZ;
} else {
if (!max_khz)
max_khz = cppc_get_dmi_max_khz();
mul = caps->highest_perf;
div = max_khz;
}
retval = offset + div64_u64(freq * mul, div);
if (retval >= 0)
return retval;
return 0;
}
EXPORT_SYMBOL_GPL(cppc_khz_to_perf);
|