summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/ppm/ppm.c
blob: e52ff63b78ab457ef0554f02dd287f3532fd326d (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Platform Power Management master pseudo driver -
 *    - attaches only  when ppm.conf file is present, indicating a
 *      workstation (since Excalibur era ) that is designed to
 *      be MOU-3 EPA compliant and which uses platform-specific
 *	hardware to do so;
 *    - this pseudo driver uses a set of simple satellite
 *      device drivers responsible for accessing platform
 *      specific devices to modify the registers they own.
 *	ppm drivers tells these	satellite drivers what to do
 *	according to using command values taken from ppm.conf.
 */
#include <sys/conf.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/open.h>
#include <sys/callb.h>
#include <sys/va_list.h>
#include <sys/errno.h>
#include <sys/modctl.h>
#include <sys/sysmacros.h>
#include <sys/ddi_impldefs.h>
#include <sys/promif.h>
#include <sys/epm.h>
#include <sys/sunpm.h>
#include <sys/ppmio.h>
#include <sys/sunldi.h>
#include <sys/ppmvar.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ppm_plat.h>

/*
 * Note: When pm_power() is called (directly or indirectly) to change the
 * power level of a device and the call returns failure, DO NOT assume the
 * level is unchanged.  Doublecheck it against ppmd->level.
 */

/*
 * cb_ops
 */
static int	ppm_open(dev_t *, int, int, cred_t *);
static int	ppm_close(dev_t, int, int, cred_t *);
static int	ppm_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);

static struct cb_ops ppm_cb_ops = {
	ppm_open,		/* open	*/
	ppm_close,		/* close */
	nodev,			/* strategy */
	nodev,			/* print */
	nodev,			/* dump */
	nodev,			/* read */
	nodev,			/* write */
	ppm_ioctl,		/* ioctl */
	nodev,			/* devmap */
	nodev,			/* mmap */
	nodev,			/* segmap */
	nochpoll,		/* poll */
	ddi_prop_op,		/* prop_op */
	NULL,			/* streamtab */
	D_MP | D_NEW,		/* driver compatibility flag */
	CB_REV,			/* cb_ops revision */
	nodev,			/* async read */
	nodev			/* async write */
};

/*
 * bus_ops
 */
static int	ppm_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *,
    void *);

static struct bus_ops ppm_bus_ops = {
	BUSO_REV,		/* busops_rev		*/
	0,			/* bus_map		*/
	0,			/* bus_get_intrspec	*/
	0,			/* bus_add_intrspec	*/
	0,			/* bus_remove_intrspec	*/
	0,			/* bus_map_fault	*/
	ddi_no_dma_map,		/* bus_dma_map		*/
	ddi_no_dma_allochdl,	/* bus_dma_allochdl	*/
	NULL,			/* bus_dma_freehdl	*/
	NULL,			/* bus_dma_bindhdl	*/
	NULL,			/* bus_dma_unbindhdl	*/
	NULL,			/* bus_dma_flush	*/
	NULL,			/* bus_dma_win		*/
	NULL,			/* bus_dma_ctl		*/
	ppm_ctlops,		/* bus_ctl		*/
	0,			/* bus_prop_op		*/
	0,			/* bus_get_eventcookie	*/
	0,			/* bus_add_eventcall	*/
	0,			/* bus_remove_eventcall	*/
	0,			/* bus_post_event	*/
	0			/* bus_intr_ctl		*/
};

/*
 * dev_ops
 */
static int	ppm_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int	ppm_attach(dev_info_t *, ddi_attach_cmd_t);
static int	ppm_detach(dev_info_t *, ddi_detach_cmd_t);

static struct dev_ops ppm_ops = {
	DEVO_REV,		/* devo_rev */
	0,			/* refcnt */
	ppm_getinfo,		/* info */
	nulldev,		/* identify */
	nulldev,		/* probe */
	ppm_attach,		/* attach */
	ppm_detach,		/* detach */
	nodev,			/* reset */
	&ppm_cb_ops,		/* cb_ops */
	&ppm_bus_ops,		/* bus_ops */
	nulldev			/* power */
};

extern struct mod_ops mod_driverops;

static struct modldrv modldrv = {
	&mod_driverops,
	"platform pm driver v%I%",
	&ppm_ops
};

static struct modlinkage modlinkage = {
	MODREV_1,
	&modldrv,
	NULL
};

/*
 * Global data structure and variables
 */
int	ppm_inst = -1;
void	*ppm_statep;
ppm_domain_t *ppm_domain_p;
callb_id_t   *ppm_cprcb_id;
static kmutex_t ppm_cpr_window_lock;	/* guard ppm_cpr_window_flag */
static	boolean_t ppm_cpr_window_flag;	/* set indicating chpt-resume period */

/* LED actions */
#define	PPM_LED_SOLIDON		0
#define	PPM_LED_BLINKING	1

/*
 * Debug
 */
#ifdef	DEBUG
uint_t	ppm_debug = 0;
#endif

/*
 * Local function prototypes and data
 */
static boolean_t	ppm_cpr_callb(void *, int);
static int		ppm_fetset(ppm_domain_t *, uint8_t);
static int		ppm_fetget(ppm_domain_t *, uint8_t *);
static int		ppm_gpioset(ppm_domain_t *, int);
static int		ppm_manage_cpus(dev_info_t *, power_req_t *, int *);
static int		ppm_manage_pci(dev_info_t *, power_req_t *, int *);
static int		ppm_manage_pcie(dev_info_t *, power_req_t *, int *);
static int		ppm_manage_fet(dev_info_t *, power_req_t *, int *);
static void		ppm_manage_led(int);
static void		ppm_set_led(ppm_domain_t *, int);
static void		ppm_blink_led(void *);
static void		ppm_svc_resume_ctlop(dev_info_t *, power_req_t *);
static int		ppm_set_level(ppm_dev_t *, int, int, boolean_t);
static int		ppm_change_power_level(ppm_dev_t *, int, int);
static int		ppm_record_level_change(ppm_dev_t *, int, int);
static int		ppm_switch_clock(ppm_domain_t *, int);
static int		ppm_pcie_pwr(ppm_domain_t *, int);
static int		ppm_power_up_domain(dev_info_t *dip);
static int		ppm_power_down_domain(dev_info_t *dip);

int
_init(void)
{
	if (ddi_soft_state_init(
	    &ppm_statep, sizeof (ppm_unit_t), 1) != DDI_SUCCESS) {
		PPMD(D_INIT, ("ppm: soft state init\n"))
		return (DDI_FAILURE);
	}

	if (mod_install(&modlinkage) != DDI_SUCCESS) {
		ddi_soft_state_fini(&ppm_statep);
		return (DDI_FAILURE);
	}
	return (DDI_SUCCESS);
}


int
_fini(void)
{
	int error;

	if ((error = mod_remove(&modlinkage)) == DDI_SUCCESS)
		ddi_soft_state_fini(&ppm_statep);

	return (error);
}


int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}


/* ARGSUSED */
int
ppm_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
{
	struct ppm_unit *unitp;
	dev_t	dev;
	int	instance;
	int	rval;

	if (ppm_inst == -1)
		return (DDI_FAILURE);

	switch (cmd) {
	case DDI_INFO_DEVT2DEVINFO:
		if (unitp = ddi_get_soft_state(ppm_statep, (dev_t)arg)) {
			*resultp = unitp->dip;
			rval = DDI_SUCCESS;
		} else
			rval = DDI_FAILURE;

		return (rval);

	case DDI_INFO_DEVT2INSTANCE:
		dev = (dev_t)arg;
		instance = getminor(dev);
		*resultp = (void *)(uintptr_t)instance;
		return (DDI_SUCCESS);

	default:
		return (DDI_FAILURE);
	}
}


/*
 * attach(9E)
 */
static int
ppm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	ppm_unit_t *unitp;
	int ret;
#ifdef	DEBUG
	char *str = "ppm_attach";
#endif


	switch (cmd) {
	case DDI_ATTACH:
		PPMD(D_ATTACH, ("%s: attaching ...\n", str))
		break;

	case DDI_RESUME:
		PPMD(D_ATTACH, ("%s: Resuming ...\n", str))
		unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
		mutex_enter(&unitp->lock);
		unitp->states &= ~PPM_STATE_SUSPENDED;
		mutex_exit(&unitp->lock);
		return (DDI_SUCCESS);

	default:
		cmn_err(CE_WARN, "ppm_attach: unknown command %d, dip(0x%p)",
		    cmd, (void *)dip);
		return (DDI_FAILURE);
	}

	if (ppm_inst != -1) {
		PPMD(D_ATTACH, ("%s: Already attached !", str))
		return (DDI_FAILURE);
	}

	ppm_inst = ddi_get_instance(dip);
	if (ddi_soft_state_zalloc(ppm_statep, ppm_inst) != DDI_SUCCESS) {
		PPMD(D_ATTACH, ("%s: soft states alloc error!\n", str))
		return (DDI_FAILURE);
	}
	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);

	ret = ddi_create_minor_node(dip, "ppm", S_IFCHR, ppm_inst,
	    "ddi_ppm", 0);
	if (ret != DDI_SUCCESS) {
		PPMD(D_ATTACH, ("%s: can't create minor node!\n", str))
		goto fail1;
	}

	unitp->dip = dip;
	mutex_init(&unitp->lock, NULL, MUTEX_DRIVER, NULL);

	/*
	 * read ppm.conf, construct ppm_domain data structure and
	 * their sub data structure.
	 */
	if ((ret = ppm_create_db(dip)) != DDI_SUCCESS)
		goto fail2;

	/*
	 * walk down ppm domain control from each domain, initialize
	 * domain control orthogonal function call handle
	 */
	ppm_init_cb(dip);

	if ((ret = pm_register_ppm(ppm_claim_dev, dip)) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "ppm_attach: can't register ppm handler!");
		goto fail2;
	}

	mutex_init(&ppm_cpr_window_lock, NULL, MUTEX_DRIVER, NULL);
	ppm_cpr_window_flag = B_FALSE;
	ppm_cprcb_id = callb_add(ppm_cpr_callb, (void *)NULL,
	    CB_CL_CPR_PM, "ppm_cpr");

#if defined(__x86)
	/*
	 * Register callback so that once CPUs have been added to
	 * the device tree, ppm can rebuild CPU domains using ACPI
	 * data.
	 */
	cpupm_rebuild_cpu_domains = ppm_rebuild_cpu_domains;

	/*
	 * Register callback so that the ppm can initialize the
	 * topspeed for all CPUs in all domains.
	 */
	cpupm_init_topspeed = ppm_init_topspeed;

	/*
	 * Register callback so that whenever max speed throttle requests
	 * are received, ppm can redefine the high power level for
	 * all CPUs in the domain.
	 */
	cpupm_redefine_topspeed = ppm_redefine_topspeed;
#endif

	ddi_report_dev(dip);
	return (DDI_SUCCESS);

fail2:
	ddi_remove_minor_node(dip, "ddi_ppm");
	mutex_destroy(&unitp->lock);
fail1:
	ddi_soft_state_free(ppm_statep, ppm_inst);
	ppm_inst = -1;
	return (DDI_FAILURE);
}


/* ARGSUSED */
static int
ppm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	ppm_unit_t *unitp;
#ifdef	DEBUG
	char *str = "ppm_detach";
#endif

	switch (cmd) {
	case DDI_DETACH:
		PPMD(D_DETACH, ("%s: detach not allowed.\n", str))
		return (DDI_FAILURE);

	case DDI_SUSPEND:
		PPMD(D_DETACH, ("%s: suspending ...\n", str))
		unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
		mutex_enter(&unitp->lock);
		unitp->states |= PPM_STATE_SUSPENDED;
		mutex_exit(&unitp->lock);

		/*
		 * Suspend requires that timeout callouts to be canceled.
		 * Turning off the LED blinking will cancel the timeout.
		 */
		ppm_manage_led(PPM_LED_SOLIDON);
		return (DDI_SUCCESS);

	default:
		cmn_err(CE_WARN, "ppm_detach: unsupported command %d, dip(%p)",
		    cmd, (void *)dip);
		return (DDI_FAILURE);
	}
}


/* ARGSUSED */
int
ppm_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
{
	if (otyp != OTYP_CHR)
		return (EINVAL);
	PPMD(D_OPEN, ("ppm_open: devp 0x%p, flag 0x%x, otyp %d\n",
	    (void *)devp, flag, otyp))
	return (0);
}


/* ARGSUSED */
int
ppm_close(dev_t dev, int flag, int otyp, cred_t *credp)
{
	PPMD(D_CLOSE, ("ppm_close: dev 0x%lx, flag 0x%x, otyp %d\n",
	    dev, flag, otyp))
	return (0);
}


/* ARGSUSED */
int
ppm_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred_p,
    int *rval_p)
{
#ifdef DEBUG
	char *str = "ppm_ioctl";
#endif
	ppm_domain_t *domp = NULL;
	uint8_t level, lvl;
	int ret = 0;

	PPMD(D_IOCTL, ("%s: dev 0x%lx, cmd 0x%x, mode 0x%x\n",
	    str, dev, cmd, mode))

	switch (cmd) {
	case PPMGET_DPWR:
	{
		STRUCT_DECL(ppm_dpwr, dpwr);
		struct ppm_unit *unitp;
		char *domain;

		STRUCT_INIT(dpwr, mode);
		ret = ddi_copyin((caddr_t)arg, STRUCT_BUF(dpwr),
		    STRUCT_SIZE(dpwr), mode);
		if (ret != 0)
			return (EFAULT);

		/* copyin domain name */
		domain = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
		ret = copyinstr(
		    STRUCT_FGETP(dpwr, domain), domain, MAXNAMELEN, NULL);
		if (ret != 0) {
			PPMD(D_IOCTL, ("%s: can't copyin domain, line(%d)\n",
			    str, __LINE__))
			ret = EFAULT;
			goto err_dpwr;
		}

		/* locate domain */
		if ((domp = ppm_lookup_domain(domain)) == NULL) {
			PPMD(D_IOCTL, ("%s: no such domain %s\n", str, domain))
			ret = ENODEV;
			goto err_dpwr;
		}

		switch (domp->model) {
		case PPMD_FET:	/* report power fet ON or OFF */
			if ((ret = ppm_fetget(domp, &lvl)) != 0) {
				ret = EIO;
				goto err_dpwr;
			}
			level = (lvl == PPMD_ON) ?
			    PPMIO_POWER_ON : PPMIO_POWER_OFF;
			break;

		case PPMD_PCI:	/* report pci slot clock ON or OFF */
		case PPMD_PCI_PROP:
		case PPMD_PCIE:
			level = (domp->status == PPMD_ON) ?
			    PPMIO_POWER_ON : PPMIO_POWER_OFF;
			break;

		case PPMD_LED:	/* report LED blinking or solid on */

			unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
			if (unitp->led_tid == 0)
				level = PPMIO_LED_SOLIDON;
			else
				level = PPMIO_LED_BLINKING;
			break;

		case PPMD_CPU:	/* report cpu speed divisor */
			level = domp->devlist->level;
			break;

		default:
			ret = EINVAL;
			goto err_dpwr;
		}

		STRUCT_FSET(dpwr, level, level);
		ret = ddi_copyout(STRUCT_BUF(dpwr), (caddr_t)arg,
		    STRUCT_SIZE(dpwr), mode);
		if (ret != 0) {
			PPMD(D_IOCTL, ("%s: can't copyout, line(%d)\n",
			    str, __LINE__))
			ret = EFAULT;
		}
err_dpwr:
		kmem_free(domain, MAXNAMELEN);

		break;
	}

	case PPMGET_DOMBYDEV:
	{
		STRUCT_DECL(ppm_bydev, bydev);
		char *path = NULL;
		size_t   size, l;

		STRUCT_INIT(bydev, mode);
		ret = ddi_copyin((caddr_t)arg, STRUCT_BUF(bydev),
		    STRUCT_SIZE(bydev), mode);
		if (ret != 0)
			return (EFAULT);

		/* copyin .path */
		path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
		ret = copyinstr(
		    STRUCT_FGETP(bydev, path), path, MAXPATHLEN, NULL);
		if (ret != 0) {
			PPMD(D_IOCTL, ("%s: can't copyin path, line(%d)\n",
			    str, __LINE__))
			kmem_free(path, MAXPATHLEN);
			return (EFAULT);
		}

		/* so far we have up to one domain for a given device */
		size = STRUCT_FGET(bydev, size);
		domp = ppm_get_domain_by_dev(path);
		kmem_free(path, MAXPATHLEN);
		if (domp != NULL) {
			l = strlen(domp->name) + 1;
			if (l > size) {
				PPMD(D_IOCTL, ("%s: buffer too small\n", str))
				return ((size == 0) ? EINVAL : EFAULT);
			}
		} else	/* no domain found to be associated with given device */
			return (ENODEV);

		ret = copyoutstr(
		    domp->name, STRUCT_FGETP(bydev, domlist), l, &l);
		if (ret != 0) {
			PPMD(D_IOCTL, ("%s: can't copyout domlist, line(%d)"
			    " \n", str, __LINE__))
			return (EFAULT);
		}

		break;
	}


	case PPMGET_DEVBYDOM:
	{
		STRUCT_DECL(ppm_bydom, bydom);
		char *domain = NULL;
		char *devlist = NULL;
		ppm_dev_t *ppmd;
		dev_info_t *odip = NULL;
		char *s, *d;
		size_t  size, l;

		STRUCT_INIT(bydom, mode);
		ret = ddi_copyin((caddr_t)arg, STRUCT_BUF(bydom),
		    STRUCT_SIZE(bydom), mode);
		if (ret != 0)
			return (EFAULT);

		/* copyin .domain */
		domain = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
		ret = copyinstr(STRUCT_FGETP(bydom, domain), domain,
		    MAXNAMELEN, NULL);
		if (ret != 0) {
			PPMD(D_IOCTL, ("%s: can't copyin domain, line(%d)\n",
			    str, __LINE__))
			ret = EFAULT;
			goto err_bydom;
		}

		/* locate domain */
		if ((domp = ppm_lookup_domain(domain)) == NULL) {
			ret = ENODEV;
			goto err_bydom;
		}

		l = 0;
		if ((size = STRUCT_FGET(bydom, size)) == 0)
			ret = EINVAL;
		else
			if ((d = devlist = kmem_zalloc(size, KM_SLEEP)) == NULL)
				ret = EFAULT;
		if (ret != 0)
			goto err_bydom;

		for (ppmd = domp->devlist; ppmd;
		    odip = ppmd->dip, ppmd = ppmd->next) {

			if (ppmd->dip == odip)
				continue;
			if (ppmd != domp->devlist)
				*d++ = ' ';

			l += strlen(ppmd->path) + 1;
			if (l > size) {
				PPMD(D_IOCTL, ("%s: buffer overflow\n", str))
				ret = EFAULT;
				goto err_bydom;
			}

			for (s = ppmd->path; *s != 0; )
				*d++ = *s++;
		}
		*d = 0;

		if (*devlist == 0)
			goto err_bydom;

		ret = copyoutstr(
		    devlist, STRUCT_FGETP(bydom, devlist), l, &l);
		if (ret != 0) {
			PPMD(D_IOCTL, ("%s: can't copyout devlist, line(%d)"
			    " \n", str, __LINE__))
			ret = EFAULT;
		}

err_bydom:
		if (devlist)
			kmem_free(devlist, size);
		if (domain)
			kmem_free(domain, MAXNAMELEN);

		break;
	}

#if defined(__x86)
	/*
	 * Note that these two ioctls exist for test purposes only.
	 * Unfortunately, there really isn't any other good way of
	 * unit testing the dynamic redefinition of the top speed as it
	 * usually occurs due to environmental conditions.
	 */
	case PPMGET_NORMAL:
	case PPMSET_NORMAL:
	{
		STRUCT_DECL(ppm_norm, norm);
		char *path = NULL;
		struct pm_component *dcomps;
		struct pm_comp *pm_comp;
		ppm_dev_t *ppmd;
		int i;

		STRUCT_INIT(norm, mode);
		ret = ddi_copyin((caddr_t)arg, STRUCT_BUF(norm),
		    STRUCT_SIZE(norm), mode);
		if (ret != 0)
			return (EFAULT);

		/* copyin .path */
		path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
		ret = copyinstr(
		    STRUCT_FGETP(norm, path), path, MAXPATHLEN, NULL);
		if (ret != 0) {
			PPMD(D_IOCTL, ("%s: can't copyin path, line(%d)\n",
			    str, __LINE__))
			kmem_free(path, MAXPATHLEN);
			return (EFAULT);
		}

		domp = ppm_get_domain_by_dev(path);
		kmem_free(path, MAXPATHLEN);

		if (domp == NULL)
			return (ENODEV);

		ppmd = domp->devlist;
		if (cmd == PPMSET_NORMAL) {
			if (domp->model != PPMD_CPU)
				return (EINVAL);
			level = STRUCT_FGET(norm, norm);
			dcomps = DEVI(ppmd->dip)->devi_pm_components;
			pm_comp = &dcomps[ppmd->cmpt].pmc_comp;
			for (i = pm_comp->pmc_numlevels; i > 0; i--) {
				if (pm_comp->pmc_lvals[i-1] == level)
					break;
			}
			if (i == 0)
				return (EINVAL);

			ppm_set_topspeed(ppmd, pm_comp->pmc_numlevels - i);
		}

		level = pm_get_normal_power(ppmd->dip, 0);

		STRUCT_FSET(norm, norm, level);
		ret = ddi_copyout(STRUCT_BUF(norm), (caddr_t)arg,
		    STRUCT_SIZE(norm), mode);
		if (ret != 0) {
			PPMD(D_IOCTL, ("%s: can't copyout, line(%d)\n",
			    str, __LINE__))
			ret = EFAULT;
		}
		break;
	}
#endif
	default:
		PPMD(D_IOCTL, ("%s: unsupported ioctl command(%d)\n", str, cmd))
		return (EINVAL);
	}

	return (ret);
}


/*
 * interface between pm framework and ppm driver
 */
/* ARGSUSED */
static int
ppm_ctlops(dev_info_t *dip, dev_info_t *rdip,
    ddi_ctl_enum_t ctlop, void *arg, void *result)
{
	power_req_t	*reqp = (power_req_t *)arg;
	ppm_unit_t	*unitp;
	ppm_domain_t	*domp;
	ppm_dev_t	*ppmd;
	char		path[MAXNAMELEN];
	ppm_owned_t	*owned;
	int		mode;
	int		ret = DDI_SUCCESS;
	static int	ppm_manage_sx(s3a_t *, int);
	static int	ppm_search_list(pm_searchargs_t *);
	int 		*res = (int *)result;
	s3a_t s3args;

#ifdef DEBUG
	char	*str = "ppm_ctlops";
	int	mask = ppm_debug & (D_CTLOPS1 | D_CTLOPS2);
	char *ctlstr = ppm_get_ctlstr(reqp->request_type, mask);
	if (mask && ctlstr)
		PPMD(mask, ("%s: %s, %s\n",
		    str, ddi_binding_name(rdip), ctlstr))
#endif

	if (ctlop != DDI_CTLOPS_POWER) {
		return (DDI_FAILURE);
	}

	unitp = (ppm_unit_t *)ddi_get_soft_state(ppm_statep, ppm_inst);

	switch (reqp->request_type) {

	/* attempt to blink led if indeed all at lowest */
	case PMR_PPM_ALL_LOWEST:
		mode = (reqp->req.ppm_all_lowest_req.mode == PM_ALL_LOWEST);
		if (!(unitp->states & PPM_STATE_SUSPENDED) && mode)
			ppm_manage_led(PPM_LED_BLINKING);
		else
			ppm_manage_led(PPM_LED_SOLIDON);
		return (DDI_SUCCESS);

	/* undo the claiming of 'rdip' at attach time */
	case PMR_PPM_POST_DETACH:
		ASSERT(reqp->req.ppm_set_power_req.who == rdip);
		mutex_enter(&unitp->lock);
		if (reqp->req.ppm_config_req.result != DDI_SUCCESS ||
		    (PPM_GET_PRIVATE(rdip) == NULL)) {
			mutex_exit(&unitp->lock);
			return (DDI_FAILURE);
		}
		mutex_exit(&unitp->lock);
		ppm_rem_dev(rdip);
		return (DDI_SUCCESS);

	/* chance to adjust pwr_cnt if resume is about to power up rdip */
	case PMR_PPM_PRE_RESUME:
		ppm_svc_resume_ctlop(rdip, reqp);
		return (DDI_SUCCESS);

	/*
	 * synchronizing, so that only the owner of the power lock is
	 * permitted to change device and component's power level.
	 */
	case PMR_PPM_UNLOCK_POWER:
	case PMR_PPM_TRY_LOCK_POWER:
	case PMR_PPM_LOCK_POWER:
		ppmd = PPM_GET_PRIVATE(rdip);
		if (ppmd)
			domp = ppmd->domp;
		else if (reqp->request_type != PMR_PPM_UNLOCK_POWER) {
			domp = ppm_lookup_dev(rdip);
			ASSERT(domp);
			ppmd = ppm_get_dev(rdip, domp);
		}

		PPMD(D_LOCKS, ("ppm_lock_%s: %s, %s\n",
		    (domp->dflags & PPMD_LOCK_ALL) ? "all" : "one",
		    ppmd->path, ppm_get_ctlstr(reqp->request_type, D_LOCKS)))

		if (domp->dflags & PPMD_LOCK_ALL)
			ppm_lock_all(domp, reqp, result);
		else
			ppm_lock_one(ppmd, reqp, result);
		return (DDI_SUCCESS);

	case PMR_PPM_POWER_LOCK_OWNER:
		ASSERT(reqp->req.ppm_power_lock_owner_req.who == rdip);
		ppmd = PPM_GET_PRIVATE(rdip);
		if (ppmd)
			domp = ppmd->domp;
		else {
			domp = ppm_lookup_dev(rdip);
			ASSERT(domp);
			ppmd = ppm_get_dev(rdip, domp);
		}

		/*
		 * In case of LOCK_ALL, effective owner of the power lock
		 * is the owner of the domain lock. otherwise, it is the owner
		 * of the power lock.
		 */
		if (domp->dflags & PPMD_LOCK_ALL)
			reqp->req.ppm_power_lock_owner_req.owner =
			    mutex_owner(&domp->lock);
		else {
			reqp->req.ppm_power_lock_owner_req.owner =
			    DEVI(rdip)->devi_busy_thread;
		}
		return (DDI_SUCCESS);

	case PMR_PPM_INIT_CHILD:
		ASSERT(reqp->req.ppm_lock_power_req.who == rdip);
		if ((domp = ppm_lookup_dev(rdip)) == NULL)
			return (DDI_SUCCESS);

		/*
		 * We keep track of power-manageable devices starting with
		 * initialization process.  The initializing flag remains
		 * set until it is cleared by ppm_add_dev().  Power management
		 * policy for some domains are affected even during device
		 * initialization.  For example, PCI domains should leave
		 * their clock running meanwhile a device in that domain
		 * is initializing.
		 */
		mutex_enter(&domp->lock);
		owned = ppm_add_owned(rdip, domp);
		ASSERT(owned->initializing == 0);
		owned->initializing = 1;

		if (PPMD_IS_PCI(domp->model) && domp->status == PPMD_OFF) {
			ret = ppm_switch_clock(domp, PPMD_ON);
			if (ret == DDI_SUCCESS)
				domp->dflags |= PPMD_INITCHILD_CLKON;
		}
		mutex_exit(&domp->lock);
		return (ret);

	case PMR_PPM_POST_ATTACH:
		ASSERT(reqp->req.ppm_config_req.who == rdip);
		domp = ppm_lookup_dev(rdip);
		ASSERT(domp);
		ASSERT(domp->status == PPMD_ON);
		if (reqp->req.ppm_config_req.result == DDI_SUCCESS) {
			/*
			 * call ppm_get_dev, which will increment the
			 * domain power count by the right number.
			 * Undo the power count increment, done in PRE_PROBE.
			 */
			if (PM_GET_PM_INFO(rdip))
				ppmd = ppm_get_dev(rdip, domp);
			mutex_enter(&domp->lock);
			ASSERT(domp->pwr_cnt > 0);
			domp->pwr_cnt--;
			mutex_exit(&domp->lock);
			return (DDI_SUCCESS);
		}

		ret = ppm_power_down_domain(rdip);
		/* FALLTHROUGH */
	case PMR_PPM_UNINIT_CHILD:
		ASSERT(reqp->req.ppm_lock_power_req.who == rdip);
		if ((domp = ppm_lookup_dev(rdip)) == NULL)
			return (DDI_SUCCESS);

		(void) ddi_pathname(rdip, path);
		mutex_enter(&domp->lock);
		for (owned = domp->owned; owned; owned = owned->next)
			if (strcmp(owned->path, path) == 0)
				break;

		/*
		 * In case we didn't go through a complete attach and detach,
		 * the initializing flag will still be set, so clear it.
		 */
		if ((owned != NULL) && (owned->initializing))
			owned->initializing = 0;

		if (PPMD_IS_PCI(domp->model) &&
		    domp->status == PPMD_ON && domp->pwr_cnt == 0 &&
		    (domp->dflags & PPMD_INITCHILD_CLKON) &&
		    ppm_none_else_holds_power(domp)) {
			ret = ppm_switch_clock(domp, PPMD_OFF);
			if (ret == DDI_SUCCESS)
				domp->dflags &= ~PPMD_INITCHILD_CLKON;
		}
		mutex_exit(&domp->lock);
		return (ret);

	/* place holders */
	case PMR_PPM_UNMANAGE:
	case PMR_PPM_PRE_DETACH:
		return (DDI_SUCCESS);

	case PMR_PPM_PRE_PROBE:
		ASSERT(reqp->req.ppm_config_req.who == rdip);
		return (ppm_power_up_domain(rdip));

	case PMR_PPM_POST_PROBE:
		ASSERT(reqp->req.ppm_config_req.who == rdip);
		if (reqp->req.ppm_config_req.result == DDI_PROBE_SUCCESS ||
		    reqp->req.ppm_config_req.result == DDI_PROBE_DONTCARE)
			return (DDI_SUCCESS);

		/* Probe failed */
		PPMD(D_CTLOPS1 | D_CTLOPS2, ("%s: probe failed for %s@%s "
		    "rv %d\n", str, PM_NAME(rdip), PM_ADDR(rdip),
		    reqp->req.ppm_config_req.result))
		return (ppm_power_down_domain(rdip));

	case PMR_PPM_PRE_ATTACH:
		ASSERT(reqp->req.ppm_config_req.who == rdip);
		/* Domain has already been powered up in PRE_PROBE */
		domp = ppm_lookup_dev(rdip);
		ASSERT(domp);
		ASSERT(domp->status == PPMD_ON);
		return (DDI_SUCCESS);

	/* ppm intercepts power change process to the claimed devices */
	case PMR_PPM_SET_POWER:
	case PMR_PPM_POWER_CHANGE_NOTIFY:
		if ((ppmd = PPM_GET_PRIVATE(rdip)) == NULL) {
			domp = ppm_lookup_dev(rdip);
			ASSERT(domp);
			ppmd = ppm_get_dev(rdip, domp);
		}
		switch (ppmd->domp->model) {
		case PPMD_CPU:
			return (ppm_manage_cpus(rdip, reqp, result));
		case PPMD_FET:
			return (ppm_manage_fet(rdip, reqp, result));
		case PPMD_PCI:
		case PPMD_PCI_PROP:
			return (ppm_manage_pci(rdip, reqp, result));
		case PPMD_PCIE:
			return (ppm_manage_pcie(rdip, reqp, result));
		default:
			cmn_err(CE_WARN, "ppm_ctlops: domain model %d does"
			    " not support PMR_PPM_SET_POWER ctlop",
			    ppmd->domp->model);
			return (DDI_FAILURE);
		}

	case PMR_PPM_ENTER_SX:
	case PMR_PPM_EXIT_SX:
		s3args.s3a_state = reqp->req.ppm_power_enter_sx_req.sx_state;
		s3args.s3a_test_point =
		    reqp->req.ppm_power_enter_sx_req.test_point;
		s3args.s3a_wakephys = reqp->req.ppm_power_enter_sx_req.wakephys;
		s3args.s3a_psr = reqp->req.ppm_power_enter_sx_req.psr;
		ret = ppm_manage_sx(&s3args,
		    reqp->request_type == PMR_PPM_ENTER_SX);
		if (ret) {
			PPMD(D_CPR, ("ppm_manage_sx returns %d\n", ret))
			return (DDI_FAILURE);
		} else {
			return (DDI_SUCCESS);
		}

	case PMR_PPM_SEARCH_LIST:
		ret = ppm_search_list(reqp->req.ppm_search_list_req.searchlist);
		reqp->req.ppm_search_list_req.result = ret;
		*res = ret;
		if (ret) {
			PPMD(D_CPR, ("ppm_search_list returns %d\n", ret))
			return (DDI_FAILURE);
		} else {
			PPMD(D_CPR, ("ppm_search_list returns %d\n", ret))
			return (DDI_SUCCESS);
		}

	default:
		cmn_err(CE_WARN, "ppm_ctlops: unrecognized ctlops req(%d)",
		    reqp->request_type);
		return (DDI_FAILURE);
	}
}


/*
 * Raise the power level of a subrange of cpus.  Used when cpu driver
 * failed an attempt to lower the power of a cpu (probably because
 * it got busy).  Need to revert the ones we already changed.
 *
 * ecpup = the ppm_dev_t for the cpu which failed to lower power
 * level = power level to reset prior cpus to
 */
int
ppm_revert_cpu_power(ppm_dev_t *ecpup, int level)
{
	ppm_dev_t *cpup;
	int ret = DDI_SUCCESS;

	for (cpup = ecpup->domp->devlist; cpup != ecpup; cpup = cpup->next) {
		PPMD(D_CPU, ("ppm_revert_cpu_power: \"%s\", revert to "
		    "level %d\n", cpup->path, level))

		ret = pm_power(cpup->dip, 0, level);
		if (ret == DDI_SUCCESS) {
			cpup->level = level;
			cpup->rplvl = PM_LEVEL_UNKNOWN;
		}
	}
	return (ret);
}


/*
 * ppm_manage_cpus - Process a request to change the power level of a cpu.
 * If not all cpus want to be at the same level, OR if we are currently
 * refusing slowdown requests due to thermal stress, we cache the request.
 * Otherwise, set all cpus to the new power level.
 */
/* ARGSUSED */
static int
ppm_manage_cpus(dev_info_t *dip, power_req_t *reqp, int *result)
{
#ifdef	DEBUG
	char *str = "ppm_manage_cpus";
#endif
	int old, new, ret, kmflag;
	ppm_dev_t *ppmd, *cpup;
	int change_notify = 0;
	pm_ppm_devlist_t *devlist = NULL, *p;
	int		do_rescan = 0;

	*result = DDI_SUCCESS;

	switch (reqp->request_type) {
	case PMR_PPM_SET_POWER:
		break;

	case PMR_PPM_POWER_CHANGE_NOTIFY:
		change_notify = 1;
		break;

	default:
		return (DDI_FAILURE);
	}

	ppmd = PPM_GET_PRIVATE(dip);
	ASSERT(MUTEX_HELD(&ppmd->domp->lock));
	old = reqp->req.ppm_set_power_req.old_level;
	new = reqp->req.ppm_set_power_req.new_level;

	if (change_notify) {
		ppmd->level = new;
		ppmd->rplvl = PM_LEVEL_UNKNOWN;

		PPMD(D_CPU, ("%s: Notify cpu dip %p power level has changed "
		    "from %d to %d", str, (void *)dip, old, new))
		return (DDI_SUCCESS);
	}

	if (ppm_manage_early_cpus(dip, new, result))
		return (*result);

	if (new == ppmd->level) {
		PPMD(D_CPU, ("%s: already at power level %d\n", str, new))
		return (DDI_SUCCESS);
	}

	/*
	 * A request from lower to higher level transition is granted and
	 * made effective on all cpus. A request from higher to lower must
	 * be agreed upon by all cpus.
	 */
	ppmd->rplvl = new;
	for (cpup = ppmd->domp->devlist; cpup; cpup = cpup->next) {
		if (cpup->rplvl == new)
			continue;

		if (new < old) {
			PPMD(D_SOME, ("%s: not all cpus wants to be at new "
			    "level %d yet.\n", str, new))
			return (DDI_SUCCESS);
		}

		/*
		 * If a single cpu requests power up, honor the request
		 * powering up all cpus.
		 */
		if (new > old) {
			PPMD(D_SOME, ("%s: powering up device(%s@%s, %p) "
			    "because of request from dip(%s@%s, %p), "
			    "need pm_rescan\n", str, PM_NAME(cpup->dip),
			    PM_ADDR(cpup->dip), (void *)cpup->dip,
			    PM_NAME(dip), PM_ADDR(dip), (void *)dip))
			do_rescan++;
		}
	}

	PPMD(D_SETLVL, ("%s: \"%s\" set power level old %d, new %d \n",
	    str, ppmd->path, ppmd->level, new))
	ret = ppm_change_cpu_power(ppmd, new);
	*result = ret;

	if (ret == DDI_SUCCESS) {
		if (reqp->req.ppm_set_power_req.canblock == PM_CANBLOCK_BLOCK)
			kmflag = KM_SLEEP;
		else
			kmflag = KM_NOSLEEP;

		for (cpup = ppmd->domp->devlist; cpup; cpup = cpup->next) {
			if (cpup->dip == dip)
				continue;

			if ((p = kmem_zalloc(sizeof (pm_ppm_devlist_t),
			    kmflag)) == NULL) {
				break;
			}
			p->ppd_who = cpup->dip;
			p->ppd_cmpt = cpup->cmpt;
			p->ppd_old_level = old;
			p->ppd_new_level = new;
			p->ppd_next = devlist;

			PPMD(D_SETLVL, ("%s: devlist entry[\"%s\"] %d -> %d\n",
			    str, cpup->path, old, new))

			devlist = p;
		}
		reqp->req.ppm_set_power_req.cookie = (void *) devlist;

		if (do_rescan > 0) {
			for (cpup = ppmd->domp->devlist; cpup;
			    cpup = cpup->next) {
				if (cpup->dip == dip)
					continue;
				pm_rescan(cpup->dip);
			}
		}
	}

	return (ret);
}


/*
 * ppm_svc_resume_ctlop - this is a small bookkeeping ppm does -
 * increments its FET domain power count, in anticipation of that
 * the indicated device(dip) would be powered up by its driver as
 * a result of cpr resuming.
 */
/* ARGSUSED */
static void
ppm_svc_resume_ctlop(dev_info_t *dip, power_req_t *reqp)
{
	ppm_domain_t *domp;
	ppm_dev_t *ppmd;
	int powered;	/* power up count per dip */

	ppmd = PPM_GET_PRIVATE(dip);
	if (ppmd == NULL)
		return;

	/*
	 * Maintain correct powered count for domain which cares
	 */
	powered = 0;
	domp = ppmd->domp;
	mutex_enter(&domp->lock);
	if ((domp->model == PPMD_FET) || PPMD_IS_PCI(domp->model) ||
	    (domp->model == PPMD_PCIE)) {
		for (ppmd = domp->devlist; ppmd; ppmd = ppmd->next) {
			if (ppmd->dip == dip && ppmd->level)
				powered++;
		}

		/*
		 * All fets and clocks are held on during suspend -
		 * resume window regardless their domain devices' power
		 * level.
		 */
		ASSERT(domp->status == PPMD_ON);

		/*
		 * The difference indicates the number of components
		 * being off prior to suspend operation, that is the
		 * amount needs to be compensated in order to sync up
		 * bookkeeping with reality, for PROM reset would have
		 * brought up all devices.
		 */
		if (powered < PM_NUMCMPTS(dip))
			domp->pwr_cnt += PM_NUMCMPTS(dip) - powered;
	}
	for (ppmd = domp->devlist; ppmd; ppmd = ppmd->next) {
		if (ppmd->dip == dip)
			ppmd->level = ppmd->rplvl = PM_LEVEL_UNKNOWN;
	}
	mutex_exit(&domp->lock);
}

#ifdef	DEBUG
static int ppmbringup = 0;
#endif

int
ppm_bringup_domains()
{
#ifdef DEBUG
	char *str = "ppm_bringup_domains";
#endif
	ppm_domain_t	*domp;
	int	ret = DDI_SUCCESS;

	PPMD(D_CPR, ("%s[%d]: enter\n", str, ++ppmbringup))
	for (domp = ppm_domain_p; domp; domp = domp->next) {
		if ((!PPMD_IS_PCI(domp->model) && (domp->model != PPMD_FET) &&
		    (domp->model != PPMD_PCIE)) || (domp->devlist == NULL))
			continue;

		mutex_enter(&domp->lock);
		if (domp->status == PPMD_ON) {
			mutex_exit(&domp->lock);
			continue;
		}
		switch (domp->model) {
		case PPMD_FET:
			ret = ppm_fetset(domp, PPMD_ON);
			break;
		case PPMD_PCI:
		case PPMD_PCI_PROP:
			ret = ppm_switch_clock(domp, PPMD_ON);
			break;
		case PPMD_PCIE:
			ret = ppm_pcie_pwr(domp, PPMD_ON);
			break;
		default:
			break;
		}
		mutex_exit(&domp->lock);
	}
	PPMD(D_CPR, ("%s[%d]: exit\n", str, ppmbringup))

	return (ret);
}

#ifdef	DEBUG
static int ppmsyncbp = 0;
#endif

int
ppm_sync_bookkeeping()
{
#ifdef DEBUG
	char *str = "ppm_sync_bookkeeping";
#endif
	ppm_domain_t	*domp;
	int	ret = DDI_SUCCESS;

	PPMD(D_CPR, ("%s[%d]: enter\n", str, ++ppmsyncbp))
	for (domp = ppm_domain_p; domp; domp = domp->next) {
		if ((!PPMD_IS_PCI(domp->model) && (domp->model != PPMD_FET) &&
		    (domp->model != PPMD_PCIE)) || (domp->devlist == NULL))
			continue;

		mutex_enter(&domp->lock);
		if ((domp->pwr_cnt != 0) || !ppm_none_else_holds_power(domp)) {
			mutex_exit(&domp->lock);
			continue;
		}

		/*
		 * skip NULL .devlist slot, for some may host pci device
		 * that can not tolerate clock off or not even participate
		 * in PM.
		 */
		if (domp->devlist == NULL)
			continue;

		switch (domp->model) {
		case PPMD_FET:
			ret = ppm_fetset(domp, PPMD_OFF);
			break;
		case PPMD_PCI:
		case PPMD_PCI_PROP:
			ret = ppm_switch_clock(domp, PPMD_OFF);
			break;
		case PPMD_PCIE:
			ret = ppm_pcie_pwr(domp, PPMD_OFF);
			break;
		default:
			break;
		}
		mutex_exit(&domp->lock);
	}
	PPMD(D_CPR, ("%s[%d]: exit\n", str, ppmsyncbp))

	return (ret);
}



/*
 * pre-suspend window;
 *
 * power up every FET and PCI clock that are off;
 *
 * set ppm_cpr_window global flag to indicate
 * that even though all pm_scan requested power transitions
 * will be honored as usual but that until we're out
 * of this window,  no FET or clock will be turned off
 * for domains with pwr_cnt decremented down to 0.
 * Such is to avoid accessing the orthogonal drivers that own
 * the FET and clock registers that may not be resumed yet.
 *
 * at post-resume window, walk through each FET and PCI domains,
 * bring pwr_cnt and domp->status to sense: if pwr-cnt == 0,
 * and noinvol check okays, power down the FET or PCI.  At last,
 * clear the global flag ppm_cpr_window.
 *
 * ASSERT case 1, during cpr window, checks pwr_cnt against power
 *	transitions;
 * ASSERT case 2, out of cpr window, checks four things:
 *	pwr_cnt <> power transition in/out of 0
 *	<> status <> record of noinvol device detached
 *
 */
/* ARGSUSED */
static boolean_t
ppm_cpr_callb(void *arg, int code)
{
	int	ret;

	switch (code) {
	case CB_CODE_CPR_CHKPT:

		/* pre-suspend: start of cpr window */
		mutex_enter(&ppm_cpr_window_lock);
		ASSERT(ppm_cpr_window_flag == B_FALSE);
		ppm_cpr_window_flag = B_TRUE;
		mutex_exit(&ppm_cpr_window_lock);

		ret = ppm_bringup_domains();

		break;

	case CB_CODE_CPR_RESUME:

		/* post-resume: end of cpr window */
		ret = ppm_sync_bookkeeping();

		mutex_enter(&ppm_cpr_window_lock);
		ASSERT(ppm_cpr_window_flag == B_TRUE);
		ppm_cpr_window_flag = B_FALSE;
		mutex_exit(&ppm_cpr_window_lock);

		break;
	}

	return (ret == DDI_SUCCESS);
}


/*
 * Initialize our private version of real power level
 * as well as lowest and highest levels the device supports;
 * relate to ppm_add_dev
 */
void
ppm_dev_init(ppm_dev_t *ppmd)
{
	struct pm_component *dcomps;
	struct pm_comp *pm_comp;
	dev_info_t *dip;
	int maxi, i;

	ASSERT(MUTEX_HELD(&ppmd->domp->lock));
	ppmd->level = PM_LEVEL_UNKNOWN;
	ppmd->rplvl = PM_LEVEL_UNKNOWN;

	/* increment pwr_cnt per component */
	if ((ppmd->domp->model == PPMD_FET) ||
	    PPMD_IS_PCI(ppmd->domp->model) ||
	    (ppmd->domp->model == PPMD_PCIE))
		ppmd->domp->pwr_cnt++;

	dip = ppmd->dip;

	/*
	 * ppm exists to handle power-manageable devices which require
	 * special handling on the current platform.  However, a
	 * driver for such a device may choose not to support power
	 * management on a particular load/attach.  In this case we
	 * we create a structure to represent a single-component device
	 * for which "level" = PM_LEVEL_UNKNOWN and "lowest" = 0
	 * are effectively constant.
	 */
	if (PM_GET_PM_INFO(dip)) {
		dcomps = DEVI(dip)->devi_pm_components;
		pm_comp = &dcomps[ppmd->cmpt].pmc_comp;

		ppmd->lowest = pm_comp->pmc_lvals[0];
		ASSERT(ppmd->lowest >= 0);
		maxi = pm_comp->pmc_numlevels - 1;
		ppmd->highest = pm_comp->pmc_lvals[maxi];

		/*
		 * If 66mhz PCI device on pci 66mhz bus supports D2 state
		 * (config reg PMC bit 10 set), ppm could turn off its bus
		 * clock once it is at D3hot.
		 */
		if (ppmd->domp->dflags & PPMD_PCI66MHZ) {
			for (i = 0; i < maxi; i++)
				if (pm_comp->pmc_lvals[i] == PM_LEVEL_D2) {
					ppmd->flags |= PPMDEV_PCI66_D2;
					break;
				}
		}
	}

	/*
	 * If device is in PCI_PROP domain and has exported the
	 * property listed in ppm.conf, its clock will be turned
	 * off when all pm'able devices in that domain are at D3.
	 */
	if ((ppmd->domp->model == PPMD_PCI_PROP) &&
	    (ppmd->domp->propname != NULL) &&
	    ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
	    ppmd->domp->propname))
		ppmd->flags |= PPMDEV_PCI_PROP_CLKPM;
}


/*
 * relate to ppm_rem_dev
 */
void
ppm_dev_fini(ppm_dev_t *ppmd)
{
	ASSERT(MUTEX_HELD(&ppmd->domp->lock));

	/* decrement pwr_cnt per component */
	if ((ppmd->domp->model == PPMD_FET) ||
	    PPMD_IS_PCI(ppmd->domp->model) ||
	    (ppmd->domp->model == PPMD_PCIE))
		if (ppmd->level != ppmd->lowest)
			ppmd->domp->pwr_cnt--;
}

/*
 * Each power fet controls the power of one or more platform
 * device(s) within their domain.  Hence domain devices' power
 * level change has been monitored, such that once all devices
 * are powered off, the fet is turned off to save more power.
 *
 * To power on any domain device, the domain power fet
 * needs to be turned on first. always one fet per domain.
 */
static int
ppm_manage_fet(dev_info_t *dip, power_req_t *reqp, int *result)
{
#ifdef DEBUG
	char *str = "ppm_manage_fet";
#endif
	int (*pwr_func)(ppm_dev_t *, int, int);
	int		new, old, cmpt;
	ppm_dev_t	*ppmd;
	ppm_domain_t	*domp;
	int		incr = 0;
	int		dummy_ret;


	*result = DDI_SUCCESS;
	switch (reqp->request_type) {
	case PMR_PPM_SET_POWER:
		pwr_func = ppm_change_power_level;
		old = reqp->req.ppm_set_power_req.old_level;
		new = reqp->req.ppm_set_power_req.new_level;
		cmpt = reqp->req.ppm_set_power_req.cmpt;
		break;
	case PMR_PPM_POWER_CHANGE_NOTIFY:
		pwr_func = ppm_record_level_change;
		old = reqp->req.ppm_notify_level_req.old_level;
		new = reqp->req.ppm_notify_level_req.new_level;
		cmpt = reqp->req.ppm_notify_level_req.cmpt;
		break;
	default:
		*result = DDI_FAILURE;
		PPMD(D_FET, ("%s: unknown request type %d for %s@%s\n",
		    str, reqp->request_type, PM_NAME(dip), PM_ADDR(dip)))
		return (DDI_FAILURE);
	}

	for (ppmd = PPM_GET_PRIVATE(dip); ppmd; ppmd = ppmd->next)
		if (cmpt == ppmd->cmpt)
			break;
	if (!ppmd) {
		PPMD(D_FET, ("%s: dip(%p): old(%d)->new(%d): no ppm_dev"
		    " found for cmpt(%d)", str, (void *)dip, old, new, cmpt))
		*result = DDI_FAILURE;
		return (DDI_FAILURE);
	}
	domp = ppmd->domp;
	PPMD(D_FET, ("%s: %s@%s %s old %d, new %d, c%d, level %d, "
	    "status %s\n", str, PM_NAME(dip), PM_ADDR(dip),
	    ppm_get_ctlstr(reqp->request_type, ~0), old, new, cmpt,
	    ppmd->level, (domp->status == PPMD_OFF ? "off" : "on")))


	ASSERT(old == ppmd->level);

	if (new == ppmd->level) {
		PPMD(D_FET, ("nop\n"))
		return (DDI_SUCCESS);
	}

	PPM_LOCK_DOMAIN(domp);

	/*
	 * In general, a device's published lowest power level does not
	 * have to be 0 if power-off is not tolerated. i.e. a device
	 * instance may export its lowest level > 0.  It is reasonable to
	 * assume that level 0 indicates off state, positive level values
	 * indicate power states above off, include full power state.
	 */
	if (new > 0) { /* device powering up or to different positive level */
		if (domp->status == PPMD_OFF) {

			/* can not be in (chpt, resume) window */
			ASSERT(ppm_cpr_window_flag == B_FALSE);

			ASSERT(old == 0 && domp->pwr_cnt == 0);

			PPMD(D_FET, ("About to turn fet on for %s@%s c%d\n",
			    PM_NAME(dip), PM_ADDR(dip), cmpt))

			*result = ppm_fetset(domp, PPMD_ON);
			if (*result != DDI_SUCCESS) {
				PPMD(D_FET, ("\tCan't turn on power FET: "
				    "ret(%d)\n", *result))
				PPM_UNLOCK_DOMAIN(domp);
				return (DDI_FAILURE);
			}
		}

		/*
		 * If powering up, pre-increment the count before
		 * calling pwr_func, because we are going to release
		 * the domain lock and another thread might turn off
		 * domain power otherwise.
		 */
		if (old == 0) {
			domp->pwr_cnt++;
			incr = 1;
		}

		PPMD(D_FET, ("\t%s domain power count: %d\n",
		    domp->name, domp->pwr_cnt))
	}


	PPM_UNLOCK_DOMAIN(domp);

	ASSERT(domp->pwr_cnt > 0);

	if ((*result = (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS) {
		PPMD(D_FET, ("\t%s power change failed: ret(%d)\n",
		    ppmd->path, *result))
	}

	PPM_LOCK_DOMAIN(domp);

	/*
	 * Decr the power count in two cases:
	 *
	 *   1) request was to power device down and was successful
	 *   2) request was to power up (we pre-incremented count), but failed.
	 */
	if ((*result == DDI_SUCCESS && ppmd->level == 0) ||
	    (*result != DDI_SUCCESS && incr)) {
		ASSERT(domp->pwr_cnt > 0);
		domp->pwr_cnt--;
	}

	PPMD(D_FET, ("\t%s domain power count: %d\n",
	    domp->name, domp->pwr_cnt))

	/*
	 * call to pwr_func will update ppm data structures, if it
	 * succeeds. ppm should return whatever is the return value
	 * from call to pwr_func. This way pm and ppm data structures
	 * always in sync. Use dummy_ret from here for any further
	 * return values.
	 */
	if ((domp->pwr_cnt == 0) &&
	    (ppm_cpr_window_flag == B_FALSE) &&
	    ppm_none_else_holds_power(domp)) {

		PPMD(D_FET, ("About to turn FET off for %s@%s c%d\n",
		    PM_NAME(dip), PM_ADDR(dip), cmpt))

		dummy_ret = ppm_fetset(domp, PPMD_OFF);
		if (dummy_ret != DDI_SUCCESS) {
			PPMD(D_FET, ("\tCan't turn off FET: ret(%d)\n",
			    dummy_ret))
		}
	}

	PPM_UNLOCK_DOMAIN(domp);
	ASSERT(domp->pwr_cnt >= 0);
	return (*result);
}


/*
 * the actual code that turn on or off domain power fet and
 * update domain status
 */
static int
ppm_fetset(ppm_domain_t *domp, uint8_t value)
{
	char	*str = "ppm_fetset";
	int	key;
	ppm_dc_t *dc;
	int	ret;
	clock_t	temp;
	clock_t delay = 0;

	key = (value == PPMD_ON) ? PPMDC_FET_ON : PPMDC_FET_OFF;
	for (dc = domp->dc; dc; dc = dc->next)
		if (dc->cmd == key)
			break;
	if (!dc || !dc->lh) {
		PPMD(D_FET, ("%s: %s domain: NULL ppm_dc handle\n",
		    str, domp->name))
		return (DDI_FAILURE);
	}

	if (key == PPMDC_FET_ON) {
		PPM_GET_IO_DELAY(dc, delay);
		if (delay > 0 && domp->last_off_time > 0) {
			/*
			 * provide any delay required before turning on.
			 * some devices e.g. Samsung DVD require minimum
			 * of 1 sec between OFF->ON. no delay is required
			 * for the first time.
			 */
			temp = ddi_get_lbolt();
			temp -= domp->last_off_time;
			temp = drv_hztousec(temp);

			if (temp < delay) {
				/*
				 * busy wait untill we meet the
				 * required delay. Since we maintain
				 * time stamps in terms of clock ticks
				 * we might wait for longer than required
				 */
				PPMD(D_FET, ("%s : waiting %lu micro seconds "
				    "before on\n", domp->name,
				    delay - temp));
				drv_usecwait(delay - temp);
			}
		}
	}
	switch (dc->method) {
#ifdef sun4u
	case PPMDC_I2CKIO: {
		i2c_gpio_t i2c_req;
		i2c_req.reg_mask = dc->m_un.i2c.mask;
		i2c_req.reg_val = dc->m_un.i2c.val;
		ret = ldi_ioctl(dc->lh, dc->m_un.i2c.iowr,
		    (intptr_t)&i2c_req, FWRITE | FKIOCTL, kcred, NULL);
		break;
	}
#endif

	case PPMDC_KIO:
		ret = ldi_ioctl(dc->lh, dc->m_un.kio.iowr,
		    (intptr_t)&(dc->m_un.kio.val), FWRITE | FKIOCTL, kcred,
		    NULL);
		break;

	default:
		PPMD(D_FET, ("\t%s: unsupported domain control method %d\n",
		    str, domp->dc->method))
		return (DDI_FAILURE);
	}

	PPMD(D_FET, ("%s: %s domain(%s) FET from %s to %s\n", str,
	    (ret == 0) ? "turned" : "failed to turn",
	    domp->name,
	    (domp->status == PPMD_ON) ? "ON" : "OFF",
	    (value == PPMD_ON) ? "ON" : "OFF"))

	if (ret == DDI_SUCCESS) {
		domp->status = value;

		if (key == PPMDC_FET_OFF)
			/*
			 * record the time, when it is off. time is recorded
			 * in clock ticks
			 */
			domp->last_off_time = ddi_get_lbolt();

		/* implement any post op delay. */
		if (key == PPMDC_FET_ON) {
			PPM_GET_IO_DELAY(dc, delay);
			PPMD(D_FET, ("%s : waiting %lu micro seconds "
			    "after on\n", domp->name, delay))
			if (delay > 0)
				drv_usecwait(delay);
		}
	}

	return (ret);
}


/*
 * read power fet status
 */
static int
ppm_fetget(ppm_domain_t *domp, uint8_t *lvl)
{
	char	*str = "ppm_fetget";
	ppm_dc_t *dc = domp->dc;
	uint_t	kio_val;
	int	off_val;
	int	ret;

	if (!dc->lh) {
		PPMD(D_FET, ("%s: %s domain NULL ppm_dc layered handle\n",
		    str, domp->name))
		return (DDI_FAILURE);
	}
	if (!dc->next) {
		cmn_err(CE_WARN, "%s: expect both fet on and fet off ops "
		    "defined, found only one in domain(%s)", str, domp->name);
		return (DDI_FAILURE);
	}

	switch (dc->method) {
#ifdef sun4u
	case PPMDC_I2CKIO: {
		i2c_gpio_t i2c_req;
		i2c_req.reg_mask = dc->m_un.i2c.mask;
		ret = ldi_ioctl(dc->lh, dc->m_un.i2c.iord,
		    (intptr_t)&i2c_req, FWRITE | FKIOCTL, kcred, NULL);

		if (ret) {
			PPMD(D_FET, ("%s: PPMDC_I2CKIO failed: ret(%d)\n",
			    str, ret))
			return (ret);
		}

		off_val = (dc->cmd == PPMDC_FET_OFF) ? dc->m_un.i2c.val :
		    dc->next->m_un.i2c.val;
		*lvl = (i2c_req.reg_val == off_val) ? PPMD_OFF : PPMD_ON;

		PPMD(D_FET, ("%s: %s domain FET %s\n", str, domp->name,
		    (i2c_req.reg_val == off_val) ? "OFF" : "ON"))

		break;
	}
#endif

	case PPMDC_KIO:
		ret = ldi_ioctl(dc->lh, dc->m_un.kio.iord,
		    (intptr_t)&kio_val, FWRITE | FKIOCTL, kcred, NULL);
		if (ret) {
			PPMD(D_FET, ("%s: PPMDC_KIO failed: ret(%d)\n",
			    str, ret))
			return (ret);
		}

		off_val = (dc->cmd == PPMDC_FET_OFF) ? dc->m_un.kio.val :
		    dc->next->m_un.kio.val;
		*lvl = (kio_val == off_val) ? PPMD_OFF : PPMD_ON;

		PPMD(D_FET, ("%s: %s domain FET %s\n", str, domp->name,
		    (kio_val == off_val) ? "OFF" : "ON"))

		break;

	default:
		PPMD(D_FET, ("%s: unsupported domain control method %d\n",
		    str, domp->dc->method))
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}


/*
 * the actual code that switches pci clock and update domain status
 */
static int
ppm_switch_clock(ppm_domain_t *domp, int onoff)
{
#ifdef DEBUG
	char *str = "ppm_switch_clock";
#endif
	int	cmd, pio_save;
	ppm_dc_t *dc;
	int ret;
	extern int do_polled_io;
	extern uint_t cfb_inuse;
	ppm_dev_t	*pdev;

	cmd = (onoff == PPMD_ON) ? PPMDC_CLK_ON : PPMDC_CLK_OFF;
	dc = ppm_lookup_dc(domp, cmd);
	if (!dc) {
		PPMD(D_PCI, ("%s: no ppm_dc found for domain (%s)\n",
		    str, domp->name))
		return (DDI_FAILURE);
	}

	switch (dc->method) {
	case PPMDC_KIO:
		/*
		 * If we're powering up cfb on a Stop-A, we only
		 * want to do polled i/o to turn ON the clock
		 */
		pio_save = do_polled_io;
		if ((cfb_inuse) && (cmd == PPMDC_CLK_ON)) {
			for (pdev = domp->devlist; pdev; pdev = pdev->next) {
				if (pm_is_cfb(pdev->dip)) {
					do_polled_io = 1;
					break;
				}
			}
		}

		ret = ldi_ioctl(dc->lh, dc->m_un.kio.iowr,
		    (intptr_t)&(dc->m_un.kio.val), FWRITE | FKIOCTL,
		    kcred, NULL);

		do_polled_io = pio_save;

		if (ret == 0) {
			if (cmd == PPMDC_CLK_ON) {
				domp->status = PPMD_ON;

				/*
				 * PCI PM spec requires 50ms delay
				 */
				drv_usecwait(50000);
			} else
				domp->status = PPMD_OFF;
		}

		PPMD(D_PCI, ("%s: %s pci clock %s for domain (%s)\n", str,
		    (ret == 0) ? "turned" : "failed to turn",
		    (cmd == PPMDC_CLK_OFF) ? "OFF" : "ON",
		    domp->name))

		break;

	default:
		PPMD(D_PCI, ("%s: unsupported domain control method %d\n",
		    str, dc->method))
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}


/*
 * pci slot domain is formed of pci device(s) reside in a pci slot.
 * This function monitors domain device's power level change, such
 * that,
 *   when all domain power count has gone to 0, it attempts to turn off
 *        the pci slot's clock;
 *   if any domain device is powering up, it'll turn on the pci slot's
 *        clock as the first thing.
 */
/* ARGUSED */
static int
ppm_manage_pci(dev_info_t *dip, power_req_t *reqp, int *result)
{
#ifdef DEBUG
	char *str = "ppm_manage_pci";
#endif
	int (*pwr_func)(ppm_dev_t *, int, int);
	int old, new, cmpt;
	ppm_dev_t *ppmd;
	ppm_domain_t *domp;
	int incr = 0;
	int dummy_ret;

	*result = DDI_SUCCESS;
	switch (reqp->request_type) {
	case PMR_PPM_SET_POWER:
		pwr_func = ppm_change_power_level;
		old = reqp->req.ppm_set_power_req.old_level;
		new = reqp->req.ppm_set_power_req.new_level;
		cmpt = reqp->req.ppm_set_power_req.cmpt;
		break;

	case PMR_PPM_POWER_CHANGE_NOTIFY:
		pwr_func = ppm_record_level_change;
		old = reqp->req.ppm_notify_level_req.old_level;
		new = reqp->req.ppm_notify_level_req.new_level;
		cmpt = reqp->req.ppm_notify_level_req.cmpt;
		break;

	default:
		*result = DDI_FAILURE;
		return (DDI_FAILURE);
	}

	for (ppmd = PPM_GET_PRIVATE(dip); ppmd; ppmd = ppmd->next)
		if (cmpt == ppmd->cmpt)
			break;
	if (!ppmd) {
		PPMD(D_PCI, ("%s: dip(%p): old(%d), new(%d): no ppm_dev"
		    " found for cmpt(%d)", str, (void *)dip, old, new, cmpt))
		*result = DDI_FAILURE;
		return (DDI_FAILURE);
	}
	domp = ppmd->domp;
	PPMD(D_PCI, ("%s: %s, dev(%s), c%d, old %d, new %d\n", str,
	    ppm_get_ctlstr(reqp->request_type, ~0),
	    ppmd->path, cmpt, old, new))

	ASSERT(old == ppmd->level);
	if (new == ppmd->level)
		return (DDI_SUCCESS);

	PPM_LOCK_DOMAIN(domp);

	if (new > 0) {		/* device powering up */
		if (domp->status == PPMD_OFF) {

			/* cannot be off during (chpt, resume) window */
			ASSERT(ppm_cpr_window_flag == B_FALSE);

			/* either both OFF or both ON */
			ASSERT(!((old == 0) ^ (domp->pwr_cnt == 0)));

			PPMD(D_PCI, ("About to turn clock on for %s@%s c%d\n",
			    PM_NAME(dip), PM_ADDR(dip), cmpt))

			*result = ppm_switch_clock(domp, PPMD_ON);
			if (*result != DDI_SUCCESS) {
				PPMD(D_PCI, ("\tcan't switch on pci clock: "
				    "ret(%d)\n", *result))
				PPM_UNLOCK_DOMAIN(domp);
				return (DDI_FAILURE);
			}
		}

		if (old == 0) {
			domp->pwr_cnt++;
			incr = 1;
		}

		PPMD(D_PCI, ("\t%s domain power count: %d\n",
		    domp->name, domp->pwr_cnt))
	}

	PPM_UNLOCK_DOMAIN(domp);

	ASSERT(domp->pwr_cnt > 0);

	if ((*result = (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS) {
		PPMD(D_PCI, ("\t%s power change failed: ret(%d)\n",
		    ppmd->path, *result))
	}

	PPM_LOCK_DOMAIN(domp);

	/*
	 * Decr the power count in two cases:
	 *
	 *   1) request was to power device down and was successful
	 *   2) request was to power up (we pre-incremented count), but failed.
	 */
	if ((*result == DDI_SUCCESS && ppmd->level == 0) ||
	    (*result != DDI_SUCCESS && incr)) {
		ASSERT(domp->pwr_cnt > 0);
		domp->pwr_cnt--;
	}

	PPMD(D_PCI, ("\t%s domain power count: %d\n",
	    domp->name, domp->pwr_cnt))

	/*
	 * call to pwr_func will update ppm data structures, if it
	 * succeeds. ppm should return whatever is the return value
	 * from call to pwr_func. This way pm and ppm data structures
	 * always in sync. Use dummy_ret from here for any further
	 * return values.
	 */
	if ((domp->pwr_cnt == 0) &&
	    (ppm_cpr_window_flag == B_FALSE) &&
	    ppm_none_else_holds_power(domp)) {

		PPMD(D_PCI, ("About to turn clock off for %s@%s c%d\n",
		    PM_NAME(dip), PM_ADDR(dip), cmpt))

		dummy_ret = ppm_switch_clock(domp, PPMD_OFF);
		if (dummy_ret != DDI_SUCCESS) {
			PPMD(D_PCI, ("\tCan't switch clock off: "
			    "ret(%d)\n", dummy_ret))
		}
	}

	PPM_UNLOCK_DOMAIN(domp);
	ASSERT(domp->pwr_cnt >= 0);
	return (*result);
}

/*
 * When the driver for the primary PCI-Express child has set the device to
 * lowest power (D3hot), we come here to save even more power by transitioning
 * the slot to D3cold.  Similarly, if the slot is in D3cold and we need to
 * power up the child, we come here first to power up the slot.
 */
/* ARGUSED */
static int
ppm_manage_pcie(dev_info_t *dip, power_req_t *reqp, int *result)
{
#ifdef DEBUG
	char *str = "ppm_manage_pcie";
#endif
	int (*pwr_func)(ppm_dev_t *, int, int);
	int old, new, cmpt;
	ppm_dev_t *ppmd;
	ppm_domain_t *domp;
	int incr = 0;
	int dummy_ret;

	*result = DDI_SUCCESS;
	switch (reqp->request_type) {
	case PMR_PPM_SET_POWER:
		pwr_func = ppm_change_power_level;
		old = reqp->req.ppm_set_power_req.old_level;
		new = reqp->req.ppm_set_power_req.new_level;
		cmpt = reqp->req.ppm_set_power_req.cmpt;
		break;

	case PMR_PPM_POWER_CHANGE_NOTIFY:
		pwr_func = ppm_record_level_change;
		old = reqp->req.ppm_notify_level_req.old_level;
		new = reqp->req.ppm_notify_level_req.new_level;
		cmpt = reqp->req.ppm_notify_level_req.cmpt;
		break;

	default:
		*result = DDI_FAILURE;
		return (DDI_FAILURE);
	}

	for (ppmd = PPM_GET_PRIVATE(dip); ppmd; ppmd = ppmd->next)
		if (cmpt == ppmd->cmpt)
			break;
	if (!ppmd) {
		PPMD(D_PCI, ("%s: dip(%p): old(%d), new(%d): no ppm_dev"
		    " found for cmpt(%d)", str, (void *)dip, old, new, cmpt))
		*result = DDI_FAILURE;
		return (DDI_FAILURE);
	}
	domp = ppmd->domp;
	PPMD(D_PCI, ("%s: %s, dev(%s), c%d, old %d, new %d\n", str,
	    ppm_get_ctlstr(reqp->request_type, ~0),
	    ppmd->path, cmpt, old, new))

	ASSERT(old == ppmd->level);
	if (new == ppmd->level)
		return (DDI_SUCCESS);

	PPM_LOCK_DOMAIN(domp);

	if (new > 0) {		/* device powering up */
		if (domp->status == PPMD_OFF) {

			/* cannot be off during (chpt, resume) window */
			ASSERT(ppm_cpr_window_flag == B_FALSE);

			/* either both OFF or both ON */
			ASSERT(!((old == 0) ^ (domp->pwr_cnt == 0)));

			PPMD(D_PCI, ("About to turn on pcie slot for "
			    "%s@%s c%d\n", PM_NAME(dip), PM_ADDR(dip), cmpt))

			*result = ppm_pcie_pwr(domp, PPMD_ON);
			if (*result != DDI_SUCCESS) {
				PPMD(D_PCI, ("\tcan't switch on pcie slot: "
				    "ret(%d)\n", *result))
				PPM_UNLOCK_DOMAIN(domp);
				return (DDI_FAILURE);
			}
		}

		if (old == 0) {
			domp->pwr_cnt++;
			incr = 1;
		}

		PPMD(D_PCI, ("\t%s domain power count: %d\n",
		    domp->name, domp->pwr_cnt))
	}

	PPM_UNLOCK_DOMAIN(domp);

	ASSERT(domp->pwr_cnt > 0);

	if ((*result = (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS) {
		PPMD(D_PCI, ("\t%s power change failed: ret(%d)\n",
		    ppmd->path, *result))
	}

	PPM_LOCK_DOMAIN(domp);

	/*
	 * Decr the power count in two cases:
	 *
	 *   1) request was to power device down and was successful
	 *   2) request was to power up (we pre-incremented count), but failed.
	 */
	if ((*result == DDI_SUCCESS && ppmd->level == 0) ||
	    (*result != DDI_SUCCESS && incr)) {
		ASSERT(domp->pwr_cnt > 0);
		domp->pwr_cnt--;
	}

	PPMD(D_PCI, ("\t%s domain power count: %d\n",
	    domp->name, domp->pwr_cnt))

	/*
	 * call to pwr_func will update ppm data structures, if it
	 * succeeds. ppm should return whatever is the return value
	 * from call to pwr_func. This way pm and ppm data structures
	 * always in sync. Use dummy_ret from here for any further
	 * return values.
	 */
	if ((domp->pwr_cnt == 0) &&
	    (ppm_cpr_window_flag == B_FALSE) &&
	    ppm_none_else_holds_power(domp)) {

		PPMD(D_PCI, ("About to turn off pcie slot for %s@%s c%d\n",
		    PM_NAME(dip), PM_ADDR(dip), cmpt))

		dummy_ret = ppm_pcie_pwr(domp, PPMD_OFF);
		if (dummy_ret != DDI_SUCCESS) {
			PPMD(D_PCI, ("\tCan't switch pcie slot off: "
			    "ret(%d)\n", dummy_ret))
		}
	}

	PPM_UNLOCK_DOMAIN(domp);
	ASSERT(domp->pwr_cnt >= 0);
	return (*result);

}

/*
 * Set or clear a bit on a GPIO device.  These bits are used for various device-
 * specific purposes.
 */
static int
ppm_gpioset(ppm_domain_t *domp, int key)
{
#ifdef DEBUG
	char	*str = "ppm_gpioset";
#endif
	ppm_dc_t *dc;
	int	ret;
	clock_t delay = 0;

	for (dc = domp->dc; dc; dc = dc->next)
		if (dc->cmd == key)
			break;
	if (!dc || !dc->lh) {
		PPMD(D_GPIO, ("%s: %s domain: NULL ppm_dc handle\n",
		    str, domp->name))
		return (DDI_FAILURE);
	}

	PPM_GET_IO_DELAY(dc, delay);
	if (delay > 0) {
		PPMD(D_GPIO, ("%s : waiting %lu micro seconds "
		    "before change\n", domp->name, delay))
		drv_usecwait(delay);
	}

	switch (dc->method) {
#ifdef sun4u
	case PPMDC_I2CKIO: {
		i2c_gpio_t i2c_req;
		ppm_dev_t *pdev;
		int pio_save;
		extern int do_polled_io;
		extern uint_t cfb_inuse;
		i2c_req.reg_mask = dc->m_un.i2c.mask;
		i2c_req.reg_val = dc->m_un.i2c.val;

		pio_save = do_polled_io;
		if (cfb_inuse) {
			for (pdev = domp->devlist; pdev; pdev = pdev->next) {
				if (pm_is_cfb(pdev->dip)) {
					do_polled_io = 1;
					PPMD(D_GPIO, ("%s: cfb is in use, "
					    "i2c transaction is done in "
					    "poll-mode.\n", str))
					break;
				}
			}
		}
		ret = ldi_ioctl(dc->lh, dc->m_un.i2c.iowr,
		    (intptr_t)&i2c_req, FWRITE | FKIOCTL, kcred, NULL);
		do_polled_io = pio_save;

		PPMD(D_GPIO, ("%s: %s domain(%s) from %s by writing %x "
		    "to gpio\n",
		    str, (ret == 0) ? "turned" : "FAILed to turn",
		    domp->name,
		    (domp->status == PPMD_ON) ? "ON" : "OFF",
		    dc->m_un.i2c.val))

		break;
	}
#endif

	case PPMDC_KIO:
		ret = ldi_ioctl(dc->lh, dc->m_un.kio.iowr,
		    (intptr_t)&(dc->m_un.kio.val), FWRITE | FKIOCTL, kcred,
		    NULL);

		PPMD(D_GPIO, ("%s: %s domain(%s) from %s by writing %x "
		    "to gpio\n",
		    str, (ret == 0) ? "turned" : "FAILed to turn",
		    domp->name,
		    (domp->status == PPMD_ON) ? "ON" : "OFF",
		    dc->m_un.kio.val))

		break;

	default:
		PPMD(D_GPIO, ("\t%s: unsupported domain control method %d\n",
		    str, domp->dc->method))
		return (DDI_FAILURE);
	}

	/* implement any post op delay. */
	PPM_GET_IO_DELAY(dc, delay);
	if (delay > 0) {
		PPMD(D_GPIO, ("%s : waiting %lu micro seconds "
		    "after change\n", domp->name, delay))
		drv_usecwait(delay);
	}

	return (ret);
}

static int
ppm_pcie_pwr(ppm_domain_t *domp, int onoff)
{
#ifdef DEBUG
	char *str = "ppm_pcie_pwr";
#endif
	int ret = DDI_FAILURE;
	ppm_dc_t *dc;
	clock_t delay;

	ASSERT(onoff == PPMD_OFF || onoff == PPMD_ON);

	dc = ppm_lookup_dc(domp,
	    onoff == PPMD_ON ? PPMDC_PRE_PWR_ON : PPMDC_PRE_PWR_OFF);
	if (dc) {

		/*
		 * Invoke layered ioctl for pcie root complex nexus to
		 * transition the link
		 */
		ASSERT(dc->method == PPMDC_KIO);
		delay = dc->m_un.kio.delay;
		if (delay > 0) {
			PPMD(D_GPIO, ("%s : waiting %lu micro seconds "
			    "before change\n", domp->name, delay))
			drv_usecwait(delay);
		}
		ret = ldi_ioctl(dc->lh, dc->m_un.kio.iowr,
		    (intptr_t)&(dc->m_un.kio.val),
		    FWRITE | FKIOCTL, kcred, NULL);
		if (ret == DDI_SUCCESS) {
			delay = dc->m_un.kio.post_delay;
			if (delay > 0) {
				PPMD(D_GPIO, ("%s : waiting %lu micro seconds "
				    "after change\n", domp->name, delay))
				drv_usecwait(delay);
			}
		} else {
			PPMD(D_PCI, ("%s: ldi_ioctl FAILED for domain(%s)\n",
			    str, domp->name))
			return (ret);
		}
	}

	switch (onoff) {
	case PPMD_OFF:
		/* Turn off the clock for this slot. */
		if ((ret = ppm_gpioset(domp, PPMDC_CLK_OFF)) != DDI_SUCCESS) {
			PPMD(D_GPIO,
			    ("%s: failed to turn off domain(%s) clock\n",
			    str, domp->name))
			return (ret);
		}

		/* Turn off the power to this slot */
		if ((ret = ppm_gpioset(domp, PPMDC_PWR_OFF)) != DDI_SUCCESS) {
			PPMD(D_GPIO,
			    ("%s: failed to turn off domain(%s) power\n",
			    str, domp->name))
			return (ret);
		}
		break;
	case PPMD_ON:
		/* Assert RESET for this slot. */
		if ((ret = ppm_gpioset(domp, PPMDC_RESET_ON)) != DDI_SUCCESS) {
			PPMD(D_GPIO,
			    ("%s: failed to assert reset for domain(%s)\n",
			    str, domp->name))
			return (ret);
		}

		/* Turn on the power to this slot */
		if ((ret = ppm_gpioset(domp, PPMDC_PWR_ON)) != DDI_SUCCESS) {
			PPMD(D_GPIO,
			    ("%s: failed to turn on domain(%s) power\n",
			    str, domp->name))
			return (ret);
		}

		/* Turn on the clock for this slot */
		if ((ret = ppm_gpioset(domp, PPMDC_CLK_ON)) != DDI_SUCCESS) {
			PPMD(D_GPIO,
			    ("%s: failed to turn on domain(%s) clock\n",
			    str, domp->name))
			return (ret);
		}

		/* De-assert RESET for this slot. */
		if ((ret = ppm_gpioset(domp, PPMDC_RESET_OFF)) != DDI_SUCCESS) {
			PPMD(D_GPIO,
			    ("%s: failed to de-assert reset for domain(%s)\n",
			    str, domp->name))
			return (ret);
		}

		dc = ppm_lookup_dc(domp, PPMDC_POST_PWR_ON);
		if (dc) {
			/*
			 * Invoke layered ioctl to PCIe root complex nexus
			 * to transition the link.
			 */
			ASSERT(dc->method == PPMDC_KIO);
			delay = dc->m_un.kio.delay;
			if (delay > 0) {
				PPMD(D_GPIO, ("%s: waiting %lu micro seconds "
				    "before change\n", domp->name, delay))
				drv_usecwait(delay);
			}
			ret = ldi_ioctl(dc->lh, dc->m_un.kio.iowr,
			    (intptr_t)&(dc->m_un.kio.val),
			    FWRITE | FKIOCTL, kcred, NULL);

			if (ret != DDI_SUCCESS) {
				PPMD(D_PCI, ("%s: layered ioctl to PCIe"
				    "root complex nexus FAILed\n", str))
				return (ret);
			}

			delay = dc->m_un.kio.post_delay;
			if (delay > 0) {
				PPMD(D_GPIO, ("%s: waiting %lu micro "
				    "seconds after change\n",
				    domp->name, delay))
				drv_usecwait(delay);
			}
		}
		break;
	default:
		ASSERT(0);
	}

	PPMD(D_PCI, ("%s: turned domain(%s) PCIe slot power from %s to %s\n",
	    str, domp->name, (domp->status == PPMD_ON) ? "ON" : "OFF",
	    onoff == PPMD_ON ? "ON" : "OFF"))

	domp->status = onoff;
	return (ret);
}


/*
 * Change the power level for a component of a device.  If the change
 * arg is true, we call the framework to actually change the device's
 * power; otherwise, we just update our own copy of the power level.
 */
static int
ppm_set_level(ppm_dev_t *ppmd, int cmpt, int level, boolean_t change)
{
#ifdef DEBUG
	char *str = "ppm_set_level";
#endif
	int ret;

	ret = DDI_SUCCESS;
	if (change)
		ret = pm_power(ppmd->dip, cmpt, level);

	PPMD(D_SETLVL, ("%s: %s change=%d, old %d, new %d, ret %d\n",
	    str, ppmd->path, change, ppmd->level, level, ret))

	if (ret == DDI_SUCCESS) {
		ppmd->level = level;
		ppmd->rplvl = PM_LEVEL_UNKNOWN;
	}

	return (ret);
}


static int
ppm_change_power_level(ppm_dev_t *ppmd, int cmpt, int level)
{
	return (ppm_set_level(ppmd, cmpt, level, B_TRUE));
}


static int
ppm_record_level_change(ppm_dev_t *ppmd, int cmpt, int level)
{
	return (ppm_set_level(ppmd, cmpt, level, B_FALSE));
}


static void
ppm_manage_led(int action)
{
	ppm_domain_t *domp;
	ppm_unit_t *unitp;
	timeout_id_t	tid;


	PPMD(D_LED, ("ppm_manage_led: action: %s\n",
	    (action == PPM_LED_BLINKING) ? "PPM_LED_BLINKING" :
	    "PPM_LED_SOLIDON"))

	/*
	 * test whether led operation is practically supported,
	 * if not, we waive without pressing for reasons
	 */
	if (!ppm_lookup_dc(NULL, PPMDC_LED_ON))
		return;

	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
	for (domp = ppm_domain_p; (domp && (domp->model != PPMD_LED)); )
		domp = domp->next;

	mutex_enter(&unitp->lock);
	if (action == PPM_LED_BLINKING) {
		ppm_set_led(domp, PPMD_OFF);
		unitp->led_tid = timeout(
		    ppm_blink_led, domp, PPM_LEDOFF_INTERVAL);

	} else {	/* PPM_LED_SOLIDON */
		ASSERT(action == PPM_LED_SOLIDON);
		tid = unitp->led_tid;
		unitp->led_tid = 0;

		mutex_exit(&unitp->lock);
		(void) untimeout(tid);

		mutex_enter(&unitp->lock);
		ppm_set_led(domp, PPMD_ON);
	}
	mutex_exit(&unitp->lock);
}


static void
ppm_set_led(ppm_domain_t *domp, int val)
{
	int ret;

	ret = ppm_gpioset(domp,
	    (val == PPMD_ON) ? PPMDC_LED_ON : PPMDC_LED_OFF);

	PPMD(D_LED, ("ppm_set_led:  %s LED from %s\n",
	    (ret == 0) ? "turned" : "FAILed to turn",
	    (domp->status == PPMD_ON) ? "ON to OFF" : "OFF to ON"))

	if (ret == DDI_SUCCESS)
		domp->status = val;
}


static void
ppm_blink_led(void *arg)
{
	ppm_unit_t *unitp;
	clock_t intvl;
	ppm_domain_t *domp = arg;

	unitp = ddi_get_soft_state(ppm_statep, ppm_inst);

	mutex_enter(&unitp->lock);
	if (unitp->led_tid == 0) {
		mutex_exit(&unitp->lock);
		return;
	}

	if (domp->status == PPMD_ON) {
		ppm_set_led(domp, PPMD_OFF);
		intvl = PPM_LEDOFF_INTERVAL;
	} else {
		ppm_set_led(domp, PPMD_ON);
		intvl = PPM_LEDON_INTERVAL;
	}

	unitp->led_tid = timeout(ppm_blink_led, domp, intvl);
	mutex_exit(&unitp->lock);
}

/*
 * Function to power up a domain, if required. It also increments the
 * domain pwr_cnt to prevent it from going down.
 */
static int
ppm_power_up_domain(dev_info_t *dip)
{
	int		ret = DDI_SUCCESS;
	ppm_domain_t	*domp;
	char		*str = "ppm_power_up_domain";

	domp = ppm_lookup_dev(dip);
	ASSERT(domp);
	mutex_enter(&domp->lock);
	switch (domp->model) {
	case PPMD_FET:
		if (domp->status == PPMD_OFF) {
			if ((ret = ppm_fetset(domp,  PPMD_ON)) ==
			    DDI_SUCCESS) {
				PPMD(D_FET, ("%s: turned on fet for %s@%s\n",
				    str, PM_NAME(dip), PM_ADDR(dip)))
			} else {
				PPMD(D_FET, ("%s: couldn't turn on fet "
				    "for %s@%s\n", str, PM_NAME(dip),
				    PM_ADDR(dip)))
			}
		}
		break;

	case PPMD_PCI:
	case PPMD_PCI_PROP:
		if (domp->status == PPMD_OFF) {
			if ((ret = ppm_switch_clock(domp, PPMD_ON)) ==
			    DDI_SUCCESS) {
				PPMD(D_PCI, ("%s: turned on clock for "
				    "%s@%s\n", str, PM_NAME(dip),
				    PM_ADDR(dip)))
			} else {
				PPMD(D_PCI, ("%s: couldn't turn on clock "
				    "for %s@%s\n", str, PM_NAME(dip),
				    PM_ADDR(dip)))
			}
		}
		break;

	case PPMD_PCIE:
		if (domp->status == PPMD_OFF) {
			if ((ret = ppm_pcie_pwr(domp, PPMD_ON)) ==
			    DDI_SUCCESS) {
				PPMD(D_PCI, ("%s: turned on link for "
				    "%s@%s\n", str, PM_NAME(dip),
				    PM_ADDR(dip)))
			} else {
				PPMD(D_PCI, ("%s: couldn't turn on link "
				    "for %s@%s\n", str, PM_NAME(dip),
				    PM_ADDR(dip)))
			}
		}
		break;

	default:
		break;
	}
	if (ret == DDI_SUCCESS)
		domp->pwr_cnt++;
	mutex_exit(&domp->lock);
	return (ret);
}

/*
 * Decrements the domain pwr_cnt. if conditions to power down the domain
 * are met, powers down the domain,.
 */
static int
ppm_power_down_domain(dev_info_t *dip)
{
	int		ret = DDI_SUCCESS;
	char		*str = "ppm_power_down_domain";
	ppm_domain_t	*domp;

	domp = ppm_lookup_dev(dip);
	ASSERT(domp);
	mutex_enter(&domp->lock);
	ASSERT(domp->pwr_cnt > 0);
	domp->pwr_cnt--;
	switch (domp->model) {
	case PPMD_FET:
		if ((domp->pwr_cnt == 0) &&
		    (ppm_cpr_window_flag == B_FALSE) &&
		    ppm_none_else_holds_power(domp)) {
			if ((ret = ppm_fetset(domp, PPMD_OFF)) ==
			    DDI_SUCCESS) {
				PPMD(D_FET, ("%s: turned off FET for %s@%s \n",
				    str, PM_NAME(dip), PM_ADDR(dip)))
			} else {
				PPMD(D_FET, ("%s: couldn't turn off FET for "
				    " %s@%s\n", str, PM_NAME(dip),
				    PM_ADDR(dip)))
			}
		}
		break;

	case PPMD_PCI:
	case PPMD_PCI_PROP:
		if ((domp->pwr_cnt == 0) &&
		    (ppm_cpr_window_flag == B_FALSE) &&
		    ppm_none_else_holds_power(domp)) {
			if ((ret = ppm_switch_clock(domp, PPMD_OFF)) ==
			    DDI_SUCCESS) {
				PPMD(D_PCI, ("%s: turned off clock for %s@%s\n",
				    str, PM_NAME(dip), PM_ADDR(dip)))
			} else {
				PPMD(D_PCI, ("%s: couldn't turn off clock "
				    "for %s@%s\n", str, PM_NAME(dip),
				    PM_ADDR(dip)))
			}
		}
		break;

	case PPMD_PCIE:
		if ((domp->pwr_cnt == 0) &&
		    (ppm_cpr_window_flag == B_FALSE) &&
		    ppm_none_else_holds_power(domp)) {
			if ((ret = ppm_pcie_pwr(domp, PPMD_OFF)) ==
			    DDI_SUCCESS) {
				PPMD(D_PCI, ("%s: turned off link for %s@%s\n",
				    str, PM_NAME(dip), PM_ADDR(dip)))
			} else {
				PPMD(D_PCI, ("%s: couldn't turn off link "
				    "for %s@%s\n", str, PM_NAME(dip),
				    PM_ADDR(dip)))
			}
		}
		break;

	default:
		break;
	}
	mutex_exit(&domp->lock);
	return (ret);
}

static int
ppm_manage_sx(s3a_t *s3ap, int enter)
{
	ppm_domain_t *domp = ppm_lookup_domain("domain_estar");
	ppm_dc_t *dc;
	int ret = 0;

	if (domp == NULL) {
		PPMD(D_CPR, ("ppm_manage_sx: can't find estar domain\n"))
		return (ENODEV);
	}
	PPMD(D_CPR, ("ppm_manage_sx %x, enter %d\n", s3ap->s3a_state,
	    enter))
	switch (s3ap->s3a_state) {
	case S3:
		if (enter) {
			dc = ppm_lookup_dc(domp, PPMDC_ENTER_S3);
		} else {
			dc = ppm_lookup_dc(domp, PPMDC_EXIT_S3);
		}
		ASSERT(dc && dc->method == PPMDC_KIO);
		PPMD(D_CPR,
		    ("ppm_manage_sx: calling acpi driver (handle %p)"
		    " with %x\n", (void *)dc->lh, dc->m_un.kio.iowr))
		ret = ldi_ioctl(dc->lh, dc->m_un.kio.iowr,
		    (intptr_t)s3ap, FWRITE | FKIOCTL, kcred, NULL);
		break;

	case S4:
		/* S4 is not supported yet */
		return (EINVAL);
	default:
		ASSERT(0);
	}
	return (ret);
}

/*
 * Search enable/disable lists, which are encoded in ppm.conf as an array
 * of char strings.
 */
static int
ppm_search_list(pm_searchargs_t *sl)
{
	int i;
	int flags = DDI_PROP_DONTPASS;
	ppm_unit_t *unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
	char **pp;
	char *starp;
	uint_t nelements;
	char *manuf = sl->pms_manufacturer;
	char *prod = sl->pms_product;

	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, unitp->dip, flags,
	    sl->pms_listname, &pp, &nelements) != DDI_PROP_SUCCESS) {
		PPMD(D_CPR, ("ppm_search_list prop lookup %s failed--EINVAL\n",
		    sl->pms_listname))
		return (EINVAL);
	}
	ASSERT((nelements & 1) == 0);		/* must be even */

	PPMD(D_CPR, ("ppm_search_list looking for %s, %s\n", manuf, prod))

	for (i = 0; i < nelements; i += 2) {
		PPMD(D_CPR, ("checking %s, %s", pp[i], pp[i+1]))
		/* we support only a trailing '*' pattern match */
		if ((starp = strchr(pp[i], '*')) != NULL && *(starp + 1) == 0) {
			/* LINTED - ptrdiff overflow */
			if (strncmp(manuf, pp[i], (starp - pp[i])) != 0) {
				PPMD(D_CPR, (" no match %s with %s\n",
				    manuf, pp[i + 1]))
				continue;
			}
		}
		if ((starp = strchr(pp[i + 1], '*')) != NULL &&
		    *(starp + 1) == 0) {
			if (strncmp(prod,
			    /* LINTED - ptrdiff overflow */
			    pp[i + 1], (starp - pp[i + 1])) != 0) {
				PPMD(D_CPR, (" no match %s with %s\n",
				    prod, pp[i + 1]))
				continue;
			}
		}
		if (strcmp(manuf, pp[i]) == 0 &&
		    (strcmp(prod, pp[i + 1]) == 0)) {
			PPMD(D_CPR, (" match\n"))
			ddi_prop_free(pp);
			return (0);
		}
		PPMD(D_CPR, (" no match %s with %s or %s with %s\n",
		    manuf, pp[i], prod, pp[i + 1]))
	}
	ddi_prop_free(pp);
	return (ENODEV);
}