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
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Platform Power Management driver for SUNW,Sun-Blade-1000
*/
#include <sys/modctl.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/ppmvar.h>
#include <sys/ppmio.h>
#include <sys/xcalppm_reg.h>
#include <sys/xcalppm_var.h>
#include <sys/stat.h>
#include <sys/epm.h>
#include <sys/archsystm.h>
#include <sys/cpuvar.h>
#include <sys/cheetahregs.h>
#include <sys/us3_module.h>
/*
* Locking Considerations
*
* To look at and/or modify xcppm_domain fields or elements of its list of
* xcppm_dev structures the domain_lock for the affected domain must be held.
*
* When the autopm framework needs to change the power of a component of a
* device, it needs to hold the associated power lock (see discussion at
* top of uts/common/os/sunpm.c).
*
* If the framework needs to lock a dev/cmpt for a device which this ppm
* has claimed, xcppm_ctlops will be called with PMR_PPM_LOCK_POWER. Ppm
* needs to be involved because, due to platform constraints, changing the
* power of one device may require that other devices be changed in the same
* operation.
*
* In some domains (e.g., cpus) the power lock must be acquired for all the
* affected devices to avoid possible corruption of the power states. The
* joint change must be an atomic operation. Ppm handles this by acquiring
* the domain lock, then walking the list of affected devices and acquiring
* the power lock for each of them. To unlock, the list is traversed and
* each of the power locks is freed, followed by freeing the domain lock.
*
* For other domains ppm will only be changing the power of a single device
* that is known to the framework. In these cases, the locking is done by
* acquiring the domain lock and directly calling the framework routine for
* getting a single power lock.
*/
static int xcppm_attach(dev_info_t *, ddi_attach_cmd_t);
static int xcppm_detach(dev_info_t *, ddi_detach_cmd_t);
static int xcppm_ctlops(dev_info_t *, dev_info_t *,
ddi_ctl_enum_t, void *, void *);
static void xcppm_dev_init(ppm_dev_t *);
static void xcppm_dev_fini(ppm_dev_t *);
static void xcppm_iocset(uint8_t);
static uint8_t xcppm_iocget(void);
/*
* Note: 1394 and pciupa were originally required to be LOCK_ALL domains.
* However, the underlying nexus drivers aren't able to do power mgmt
* (because of hw implementation issues). The locking protocol for these
* domains is changed to LOCK_ONE to simplify other code. The domain
* code itself will be removed in the future.
*/
static ppm_domain_t xcppm_1394 = { "domain_1394", PPMD_LOCK_ONE };
static ppm_domain_t xcppm_cpu = { "domain_cpu", PPMD_LOCK_ALL };
static ppm_domain_t xcppm_fet = { "domain_powerfet", PPMD_LOCK_ONE };
static ppm_domain_t xcppm_upa = { "domain_pciupa", PPMD_LOCK_ONE };
ppm_domain_t *ppm_domains[] = {
&xcppm_1394,
&xcppm_cpu,
&xcppm_fet,
&xcppm_upa,
NULL
};
struct ppm_funcs ppmf = {
xcppm_dev_init, /* dev_init */
xcppm_dev_fini, /* dev_fini */
xcppm_iocset, /* iocset */
xcppm_iocget, /* iocget */
};
/*
* The order of entries must be from slowest to fastest and in
* one-to-one correspondence with the cpu_level array.
*/
static const uint16_t bbc_estar_control_masks[] = {
BBC_ESTAR_SLOW, BBC_ESTAR_MEDIUM, BBC_ESTAR_FAST
};
int bbc_delay = 10; /* microsec */
/*
* Configuration data structures
*/
static struct cb_ops xcppm_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 */
};
static struct bus_ops xcppm_bus_ops = {
BUSO_REV,
0,
0,
0,
0,
0,
ddi_no_dma_map,
ddi_no_dma_allochdl,
ddi_no_dma_freehdl,
ddi_no_dma_bindhdl,
ddi_no_dma_unbindhdl,
ddi_no_dma_flush,
ddi_no_dma_win,
ddi_no_dma_mctl,
xcppm_ctlops,
0,
0, /* (*bus_get_eventcookie)(); */
0, /* (*bus_add_eventcall)(); */
0, /* (*bus_remove_eventcall)(); */
0 /* (*bus_post_event)(); */
};
static struct dev_ops xcppm_ops = {
DEVO_REV, /* devo_rev */
0, /* refcnt */
ppm_getinfo, /* info */
nulldev, /* identify */
nulldev, /* probe */
xcppm_attach, /* attach */
xcppm_detach, /* detach */
nodev, /* reset */
&xcppm_cb_ops, /* driver operations */
&xcppm_bus_ops, /* bus operations */
NULL, /* power */
ddi_quiesce_not_supported, /* devo_quiesce */
};
extern struct mod_ops mod_driverops;
static struct modldrv modldrv = {
&mod_driverops, /* type of module - pseudo */
"platform pm driver",
&xcppm_ops
};
static struct modlinkage modlinkage = {
MODREV_1,
&modldrv,
NULL
};
int
_init(void)
{
return (ppm_init(&modlinkage, sizeof (xcppm_unit_t), "xc"));
}
int
_fini(void)
{
return (EBUSY);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
xcppm_map_all_regs(dev_info_t *dip)
{
ddi_device_acc_attr_t attr_be, attr_le;
int rv0, rv1, rv2, rv3;
xcppm_unit_t *unitp;
caddr_t base_addr;
uint8_t data8;
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
attr_be.devacc_attr_version = DDI_DEVICE_ATTR_V0;
attr_be.devacc_attr_endian_flags = DDI_STRUCTURE_BE_ACC;
attr_be.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
attr_le.devacc_attr_version = DDI_DEVICE_ATTR_V0;
attr_le.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
attr_le.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
rv0 = ddi_regs_map_setup(dip, 0, &base_addr, 0, 0, &attr_be,
&unitp->hndls.bbc_estar_ctrl);
unitp->regs.bbc_estar_ctrl = (uint16_t *)(base_addr +
BBC_ESTAR_CTRL_OFFSET);
unitp->regs.bbc_assert_change = (uint32_t *)(base_addr +
BBC_ASSERT_CHANGE_OFFSET);
unitp->regs.bbc_pll_settle = (uint32_t *)(base_addr +
BBC_PLL_SETTLE_OFFSET);
rv1 = ddi_regs_map_setup(dip, 1,
(caddr_t *)&unitp->regs.rio_mode_auxio,
0, 0, &attr_le, &unitp->hndls.rio_mode_auxio);
rv2 = ddi_regs_map_setup(dip, 2, &base_addr,
0, 0, &attr_le, &unitp->hndls.gpio_bank_select);
unitp->regs.gpio_bank_sel_index = (uint8_t *)(base_addr +
GPIO_BANK_SEL_INDEX_OFFSET);
unitp->regs.gpio_bank_sel_data = (uint8_t *)(base_addr +
GPIO_BANK_SEL_DATA_OFFSET);
rv3 = ddi_regs_map_setup(dip, 3, &base_addr, 0, 0, &attr_le,
&unitp->hndls.gpio_data_ports);
unitp->regs.gpio_port1_data = (uint8_t *)(base_addr +
GPIO_PORT1_DATA_OFFSET);
unitp->regs.gpio_port2_data = (uint8_t *)(base_addr +
GPIO_PORT2_DATA_OFFSET);
if (rv0 != DDI_SUCCESS || rv1 != DDI_SUCCESS ||
rv2 != DDI_SUCCESS || rv3 != DDI_SUCCESS) {
if (rv0 == DDI_SUCCESS)
ddi_regs_map_free(&unitp->hndls.bbc_estar_ctrl);
if (rv1 == DDI_SUCCESS)
ddi_regs_map_free(&unitp->hndls.rio_mode_auxio);
if (rv2 == DDI_SUCCESS)
ddi_regs_map_free(&unitp->hndls.gpio_bank_select);
if (rv3 == DDI_SUCCESS)
ddi_regs_map_free(&unitp->hndls.gpio_data_ports);
return (DDI_FAILURE);
}
/*
* Ppm uses GPIO bits in Bank 0. Make sure Bank 0 is selected.
*/
data8 = SIO_CONFIG2_INDEX;
XCPPM_SETGET8(unitp->hndls.gpio_bank_select,
unitp->regs.gpio_bank_sel_index, data8);
data8 = XCPPM_GET8(unitp->hndls.gpio_bank_select,
unitp->regs.gpio_bank_sel_data);
data8 &= 0x7f; /* Set Bit7 to zero */
XCPPM_SETGET8(unitp->hndls.gpio_bank_select,
unitp->regs.gpio_bank_sel_data, data8);
return (DDI_SUCCESS);
}
static int
xcppm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
#ifdef DEBUG
char *str = "xcppm_attach";
#endif
xcppm_unit_t *unitp;
ppm_domain_t **dompp;
int retval;
DPRINTF(D_ATTACH, ("%s: attach cmd %d\n", str, cmd));
retval = DDI_SUCCESS;
switch (cmd) {
case DDI_ATTACH:
if (ppm_inst != -1) {
DPRINTF(D_ERROR,
("%s: instance already attached\n", str));
return (DDI_FAILURE);
}
ppm_inst = ddi_get_instance(dip);
/*
* Allocate and initialize soft state structure
*/
if (ddi_soft_state_zalloc(ppm_statep, ppm_inst) != 0)
return (DDI_FAILURE);
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
mutex_init(&unitp->unit_lock, NULL, MUTEX_DRIVER, NULL);
mutex_init(&unitp->creator_lock, NULL, MUTEX_DRIVER, NULL);
if (ddi_create_minor_node(dip, "ppm", S_IFCHR,
ppm_inst, "ddi_ppm", 0) == DDI_FAILURE) {
ddi_soft_state_free(ppm_statep, ppm_inst);
DPRINTF(D_ERROR,
("%s: Can't create minor for 0x%p\n", str,
(void *)dip));
return (DDI_FAILURE);
}
ddi_report_dev(dip);
unitp->dip = dip;
if (retval = ppm_create_db(dip))
return (retval);
/*
* Map all of the registers under the ppm node.
*/
if (xcppm_map_all_regs(dip) != DDI_SUCCESS)
return (DDI_FAILURE);
if ((retval =
pm_register_ppm(ppm_claim_dev, dip)) != DDI_SUCCESS) {
DPRINTF(D_ERROR,
("%s: can't register ppm handler\n", str));
return (retval);
}
for (dompp = ppm_domains; *dompp; dompp++)
mutex_init(&(*dompp)->lock, NULL, MUTEX_DRIVER, NULL);
break;
case DDI_RESUME:
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
mutex_enter(&unitp->unit_lock);
unitp->state &= ~XCPPM_ST_SUSPENDED;
mutex_exit(&unitp->unit_lock);
break;
default:
cmn_err(CE_CONT, "xcppm_attach: unknown "
"attach command %d, dip 0x%p\n", cmd, (void *)dip);
retval = DDI_FAILURE;
}
return (retval);
}
/*
* set the front panel LED:
* PPM_LEDON turns it on, PPM_LEDOFF turns it off.
* for GPIO register: 0x0 means led-on, 0x2 means led-off.
*/
static void
xcppm_set_led(int action)
{
xcppm_unit_t *unitp;
uint8_t reg;
ASSERT(action == PPM_LEDON || action == PPM_LEDOFF);
DPRINTF(D_LED, ("xcppm_set_led: Turn LED %s\n",
(action == PPM_LEDON) ? "on" : "off"));
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
reg = XCPPM_GET8(unitp->hndls.gpio_data_ports,
unitp->regs.gpio_port1_data);
if (action == PPM_LEDON)
reg &= ~LED;
else
reg |= LED;
XCPPM_SETGET8(unitp->hndls.gpio_data_ports,
unitp->regs.gpio_port1_data, reg);
}
static void
xcppm_blink_led(void *action)
{
xcppm_unit_t *unitp;
int new_action;
clock_t intvl;
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
mutex_enter(&unitp->unit_lock);
if (unitp->led_tid == 0) {
mutex_exit(&unitp->unit_lock);
return;
}
if ((int)(uintptr_t)action == PPM_LEDON) {
new_action = PPM_LEDOFF;
intvl = PPM_LEDOFF_INTERVAL;
} else {
ASSERT((int)(uintptr_t)action == PPM_LEDOFF);
new_action = PPM_LEDON;
intvl = PPM_LEDON_INTERVAL;
}
xcppm_set_led(new_action);
unitp->led_tid = timeout(xcppm_blink_led, (void *)(uintptr_t)new_action,
intvl);
mutex_exit(&unitp->unit_lock);
}
static void
xcppm_freeze_led(void *action)
{
xcppm_unit_t *unitp;
timeout_id_t tid;
DPRINTF(D_LOWEST, ("xcppm_freeze_led: action %d\n",
(int)(uintptr_t)action));
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
mutex_enter(&unitp->unit_lock);
tid = unitp->led_tid;
unitp->led_tid = 0;
mutex_exit(&unitp->unit_lock);
(void) untimeout(tid);
mutex_enter(&unitp->unit_lock);
xcppm_set_led((int)(uintptr_t)action);
mutex_exit(&unitp->unit_lock);
}
/* ARGSUSED */
static int
xcppm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
xcppm_unit_t *unitp;
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
DPRINTF(D_DETACH, ("xcppm_detach: cmd %d\n", cmd));
switch (cmd) {
case DDI_DETACH:
return (DDI_FAILURE);
case DDI_SUSPEND:
mutex_enter(&unitp->unit_lock);
unitp->state |= XCPPM_ST_SUSPENDED;
mutex_exit(&unitp->unit_lock);
/*
* Suspend requires that timeout callouts to be canceled.
* Turning off the LED blinking will cancel the timeout.
*/
xcppm_freeze_led((void *)PPM_LEDON);
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*
* Device we claimed has detached. We must get rid of
* our state which was used to track this device.
*/
static void
xcppm_detach_ctlop(dev_info_t *dip, power_req_t *reqp)
{
ppm_dev_t *ppmd;
ppmd = PPM_GET_PRIVATE(dip);
if (ppmd == NULL || reqp->req.ppm_config_req.result != DDI_SUCCESS)
return;
ppm_rem_dev(dip);
}
/*
* The system is being resumed from a cpr suspend operation and this
* device's attach entry will be called shortly. The driver will set
* the device's power to a conventional starting value, and we need to
* stay in sync and set our private copy to the same value.
*/
/* ARGSUSED */
static void
xcppm_resume_ctlop(dev_info_t *dip, power_req_t *reqp)
{
ppm_domain_t *domp;
ppm_dev_t *ppmd;
int powered;
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 == &xcppm_fet) {
for (ppmd = domp->devlist; ppmd; ppmd = ppmd->next) {
if (ppmd->dip == dip && ppmd->level)
powered++;
}
/*
* If this device was powered off when the system was
* suspended, this resume acts like a power-on transition,
* so we adjust the count.
*/
if (powered == 0)
domp->pwr_cnt++;
}
for (ppmd = domp->devlist; ppmd; ppmd = ppmd->next) {
if (ppmd->dip == dip)
ppmd->level = ppmd->rplvl = PM_LEVEL_UNKNOWN;
}
mutex_exit(&domp->lock);
}
/*
* 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
xcppm_set_level(ppm_dev_t *ppmd, int cmpt, int level, boolean_t change)
{
#ifdef DEBUG
char *str = "xcppm_set_level";
#endif
int ret;
ret = DDI_SUCCESS;
if (change)
ret = pm_power(ppmd->dip, cmpt, level);
DPRINTF(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
xcppm_change_power_level(ppm_dev_t *ppmd, int cmpt, int level)
{
return (xcppm_set_level(ppmd, cmpt, level, B_TRUE));
}
static int
xcppm_record_level_change(ppm_dev_t *ppmd, int cmpt, int level)
{
return (xcppm_set_level(ppmd, cmpt, level, B_FALSE));
}
static uint8_t
xcppm_gpio_port2(int action, uint8_t pos)
{
#ifdef DEBUG
char *str = "xcppm_gpio_port2";
#endif
xcppm_unit_t *unitp;
uint8_t data8, buf8;
uint8_t ret;
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
mutex_enter(&unitp->gpio_lock);
data8 = buf8 = XCPPM_GET8(unitp->hndls.gpio_data_ports,
unitp->regs.gpio_port2_data);
switch (action) {
case XCPPM_GETBIT:
ret = data8 & pos;
DPRINTF(D_GPIO, ("%s: READ: GPIO Bank2 value 0x%x\n",
str, buf8));
break;
case XCPPM_SETBIT:
case XCPPM_CLRBIT:
if (action == XCPPM_SETBIT)
data8 |= pos;
else
data8 &= ~pos;
XCPPM_SETGET8(unitp->hndls.gpio_data_ports,
unitp->regs.gpio_port2_data, data8);
ret = data8 & pos;
DPRINTF(D_GPIO, ("%s: %s: GPIO Bank2 "
"bit 0x%x changed from 0x%x to 0x%x\n",
str, (action == XCPPM_SETBIT) ? "UP" : "DOWN",
pos, buf8, data8));
break;
default:
cmn_err(CE_PANIC, "xcalppm: unrecognized register "
"IO command %d\n", action);
break;
}
mutex_exit(&unitp->gpio_lock);
return (ret);
}
/*
* 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
*/
static void
xcppm_revert_cpu_power(ppm_dev_t *ecpup, int level)
{
ppm_dev_t *cpup;
for (cpup = xcppm_cpu.devlist; cpup != ecpup; cpup = cpup->next) {
DPRINTF(D_CPU, ("xrcp: \"%s\", revert to level %d\n",
cpup->path, level));
(void) xcppm_change_power_level(cpup, 0, level);
}
}
/*
* Switch the DC/DC converter. Clearing the GPIO bit in SuperI/O puts
* the converter in low power mode and setting the bit puts it back in
* normal mode.
*/
static void
xcppm_switch_dcdc_converter(int action)
{
int tries = XCPPM_VCL_TRIES;
uint_t spl;
uint64_t stick_begin, stick_end;
uint64_t tick_begin, tick_end;
uint64_t cur_speed_ratio, full_speed_ratio;
static int xcppm_dcdc_lpm;
switch (action) {
case XCPPM_SETBIT:
if (xcppm_dcdc_lpm) {
DPRINTF(D_CPU, ("xcppm_switch_dcdc_converter: "
"switch to normal power mode.\n"));
(void) xcppm_gpio_port2(action, HIGHPWR);
xcppm_dcdc_lpm = 0;
}
break;
case XCPPM_CLRBIT:
/*
* In some fast CPU configurations, DC/DC converter was
* put in low power mode before CPUs made the transition
* to 1/32 of clock speed. In those cases, system was
* shut down by hardware for protection. To resolve that
* problem, we make sure CPUs have made the clock transition
* before the DC/DC converter has been put to low power mode.
*/
ASSERT(xcppm_dcdc_lpm == 0);
kpreempt_disable();
full_speed_ratio = cpunodes[CPU->cpu_id].clock_freq /
sys_tick_freq;
while (tries) {
spl = ddi_enter_critical();
tick_begin = gettick_counter();
stick_timestamp((int64_t *)&stick_begin);
ddi_exit_critical(spl);
drv_usecwait(XCPPM_VCL_DELAY);
spl = ddi_enter_critical();
tick_end = gettick_counter();
stick_timestamp((int64_t *)&stick_end);
ddi_exit_critical(spl);
cur_speed_ratio = (tick_end - tick_begin) /
(stick_end - stick_begin);
/*
* tick/stick at current speed should at most be
* equal to full-speed tick/stick, adjusted with
* full/lowest clock speed ratio. If not, speed
* transition has not happened yet.
*/
if (cur_speed_ratio <= ((full_speed_ratio /
XCPPM_VCL_DIVISOR) + 1)) {
DPRINTF(D_CPU, ("xcppm_switch_dcdc_converter: "
"switch to low power mode.\n"));
(void) xcppm_gpio_port2(action, HIGHPWR);
xcppm_dcdc_lpm = 1;
break;
}
DPRINTF(D_CPU, ("xcppm_switch_dcdc_converter: CPU "
"has not made transition to lowest speed yet "
"(%d)\n", tries));
tries--;
}
kpreempt_enable();
break;
}
}
static void
xcppm_rio_mode(xcppm_unit_t *unitp, int mode)
{
uint32_t data32, buf32;
mutex_enter(&unitp->gpio_lock);
data32 = buf32 = XCPPM_GET32(unitp->hndls.rio_mode_auxio,
unitp->regs.rio_mode_auxio);
if (mode == XCPPM_SETBIT)
data32 |= RIO_BBC_ESTAR_MODE;
else
data32 &= ~RIO_BBC_ESTAR_MODE;
XCPPM_SETGET32(unitp->hndls.rio_mode_auxio,
unitp->regs.rio_mode_auxio, data32);
mutex_exit(&unitp->gpio_lock);
DPRINTF(D_CPU, ("xcppm_rio_mode: %s: change from 0x%x to 0x%x\n",
(mode == XCPPM_SETBIT) ? "DOWN" : "UP", buf32, data32));
}
/*
* change the power level of all cpus to the arg value;
* the caller needs to ensure that a legal transition is requested.
*/
static int
xcppm_change_cpu_power(int newlevel)
{
#ifdef DEBUG
char *str = "xcppm_ccp";
#endif
int index, level, oldlevel;
int lowest, highest;
int undo_flag, ret;
int speedup, incr;
uint32_t data32;
uint16_t data16;
xcppm_unit_t *unitp;
ppm_dev_t *cpup;
dev_info_t *dip;
char *chstr;
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
ASSERT(unitp);
cpup = xcppm_cpu.devlist;
lowest = cpup->lowest;
highest = cpup->highest;
/*
* not all cpus may have transitioned to a known level by this time
*/
oldlevel = (cpup->level == PM_LEVEL_UNKNOWN) ? highest : cpup->level;
dip = cpup->dip;
ASSERT(dip);
DPRINTF(D_CPU, ("%s: old %d, new %d, highest %d, lowest %d\n",
str, oldlevel, newlevel, highest, lowest));
if (newlevel > oldlevel) {
chstr = "UP";
speedup = 1;
incr = 1;
} else if (newlevel < oldlevel) {
chstr = "DOWN";
speedup = 0;
incr = -1;
} else
return (DDI_SUCCESS);
undo_flag = 0;
if (speedup) {
/*
* If coming up from lowest power level, set the E*
* mode bit in GPIO to make power supply efficient
* at normal power.
*/
if (oldlevel == cpup->lowest) {
xcppm_switch_dcdc_converter(XCPPM_SETBIT);
undo_flag = 1;
}
} else {
/*
* set BBC Estar mode bit in RIO AUXIO register
*/
if (oldlevel == highest) {
xcppm_rio_mode(unitp, XCPPM_SETBIT);
undo_flag = 1;
}
}
/*
* this loop will execute 1x or 2x depending on
* number of times we need to change clock rates
*/
for (level = oldlevel+incr; level != newlevel+incr; level += incr) {
for (cpup = xcppm_cpu.devlist; cpup; cpup = cpup->next) {
if (cpup->level == level)
continue;
ret = xcppm_change_power_level(cpup, 0, level);
DPRINTF(D_CPU, ("%s: \"%s\", %s to level %d, ret %d\n",
str, cpup->path, chstr, cpup->level, ret));
if (ret == DDI_SUCCESS)
continue;
/*
* if the driver was unable to lower cpu speed,
* the cpu probably got busy; set the previous
* cpus back to the original level
*/
if (speedup == 0)
xcppm_revert_cpu_power(cpup, level + 1);
if (undo_flag) {
if (speedup)
xcppm_switch_dcdc_converter(
XCPPM_CLRBIT);
else
xcppm_rio_mode(unitp, XCPPM_CLRBIT);
}
return (ret);
}
index = level - 1;
spm_change_schizo_speed(index);
DPRINTF(D_CPU, ("%s: safari config reg changed\n", str));
/*
* set the delay times for changing to this rate
*/
data32 = XCPPM_BBC_DELAY(index);
XCPPM_SETGET32(unitp->hndls.bbc_estar_ctrl,
(caddr_t)unitp->regs.bbc_assert_change, data32);
DPRINTF(D_CPU, ("%s: %s: Wrote E* Assert Change Time "
"(t1) = 0x%x\n", str, chstr, data32));
data32 = XCPPM_BBC_DELAY(index);
XCPPM_SETGET32(unitp->hndls.bbc_estar_ctrl,
(caddr_t)unitp->regs.bbc_pll_settle, data32);
DPRINTF(D_CPU, ("%s: %s: Wrote E* PLL Settle Time "
"(t4) = 0x%x\n", str, chstr, data32));
data16 = bbc_estar_control_masks[index];
XCPPM_SETGET16(unitp->hndls.bbc_estar_ctrl,
(caddr_t)unitp->regs.bbc_estar_ctrl, data16);
DPRINTF(D_CPU, ("%s: %s: Wrote BCC E* Control = 0x%x\n",
str, chstr, data16));
}
/*
* clear CPU Estar Mode bit in the gpio register
*/
if (speedup) {
if (newlevel == highest)
xcppm_rio_mode(unitp, XCPPM_CLRBIT);
} else {
if (newlevel == lowest)
xcppm_switch_dcdc_converter(XCPPM_CLRBIT);
}
return (DDI_SUCCESS);
}
/*
* Process a request to change the power level of a cpu. If all cpus
* don't want to be at the same power yet, or if we are currently
* refusing slowdown requests due to thermal stress, just cache the
* request. Otherwise, make the change for all cpus.
*/
/* ARGSUSED */
static int
xcppm_manage_cpus(dev_info_t *dip, power_req_t *reqp, int *result)
{
#ifdef DEBUG
char *str = "xcppm_manage_cpus";
#endif
int old, new, ret, kmflag;
ppm_dev_t *ppmd;
pm_ppm_devlist_t *devlist = NULL, *p;
int do_rescan = 0;
dev_info_t *rescan_dip;
*result = DDI_SUCCESS;
switch (reqp->request_type) {
case PMR_PPM_SET_POWER:
break;
case PMR_PPM_POWER_CHANGE_NOTIFY:
/* cpu driver can`t change cpu power level by itself */
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;
/*
* At power on, the cpus are at full speed. There is no hardware
* transition needed for going from unknown to full. However, the
* state of the pm framework and cpu driver needs to be adjusted.
*/
if (ppmd->level == PM_LEVEL_UNKNOWN && new == ppmd->highest) {
*result = ret = xcppm_change_power_level(ppmd, 0, new);
if (ret != DDI_SUCCESS) {
DPRINTF(D_CPU, ("%s: Failed to change "
"power level to %d\n", str, new));
}
return (ret);
}
if (new == ppmd->level) {
DPRINTF(D_CPU, ("%s: already at power level %d\n", str, new));
return (DDI_SUCCESS);
}
ppmd->rplvl = new;
/*
* A request from lower to higher level transition is granted and
* made effective on both cpus. For more than two cpu platform model,
* the following code needs to be modified to remember the rest of
* the unsoliciting cpus to be rescan'ed.
* A request from higher to lower must be agreed by all cpus.
*/
for (ppmd = xcppm_cpu.devlist; ppmd; ppmd = ppmd->next) {
if (ppmd->rplvl == new)
continue;
if (new < old) {
DPRINTF(D_SOME, ("%s: not all cpus want to go down to "
"level %d yet\n", str, new));
return (DDI_SUCCESS);
}
/*
* If a single cpu requests power up, honor the request
* by powering up both cpus.
*/
if (new > old) {
DPRINTF(D_SOME, ("%s: powering up device(%s@%s, %p) "
"because of request from dip(%s@%s, %p), "
"need pm_rescan\n", str, PM_NAME(ppmd->dip),
PM_ADDR(ppmd->dip), (void *)ppmd->dip,
PM_NAME(dip), PM_ADDR(dip), (void *)dip))
do_rescan++;
rescan_dip = ppmd->dip;
break;
}
}
ret = xcppm_change_cpu_power(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 (ppmd = xcppm_cpu.devlist; ppmd; ppmd = ppmd->next) {
if (ppmd->dip == dip)
continue;
if ((p = kmem_zalloc(sizeof (pm_ppm_devlist_t),
kmflag)) == NULL) {
break;
}
p->ppd_who = ppmd->dip;
p->ppd_cmpt = ppmd->cmpt;
p->ppd_old_level = old;
p->ppd_new_level = new;
p->ppd_next = devlist;
devlist = p;
}
reqp->req.ppm_set_power_req.cookie = (void *) devlist;
if (do_rescan > 0)
pm_rescan(rescan_dip);
}
return (ret);
}
/*
* If powering off and all devices in this domain will now be off,
* shut off common power. If powering up and no devices up yet,
* turn on common power. Always make the requested power level
* change for the target device.
*/
static int
xcppm_manage_fet(dev_info_t *dip, power_req_t *reqp, int *result)
{
#ifdef DEBUG
char *str = "xcppm_manage_fet";
#endif
int (*pwr_func)(ppm_dev_t *, int, int);
int new, old, cmpt, incr = 0;
ppm_dev_t *ppmd;
ppmd = PPM_GET_PRIVATE(dip);
DPRINTF(D_FET, ("%s: \"%s\", req %s\n", str,
ppmd->path, ppm_get_ctlstr(reqp->request_type, ~0)));
*result = DDI_SUCCESS; /* change later for failures */
switch (reqp->request_type) {
case PMR_PPM_SET_POWER:
pwr_func = xcppm_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 = xcppm_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:
return (*result = DDI_FAILURE);
}
/* This is common code for SET_POWER and POWER_CHANGE_NOTIFY cases */
DPRINTF(D_FET, ("%s: \"%s\", old %d, new %d\n",
str, ppmd->path, old, new));
ASSERT(old == ppmd->level);
if (new == ppmd->level)
return (DDI_SUCCESS);
PPM_LOCK_DOMAIN(ppmd->domp);
/*
* Devices in this domain are known to have 0 (off) as their
* lowest power level. We use this fact to simplify the logic.
*/
if (new > 0) {
if (ppmd->domp->pwr_cnt == 0)
(void) xcppm_gpio_port2(XCPPM_SETBIT, DRVON);
if (old == 0) {
ppmd->domp->pwr_cnt++;
incr = 1;
DPRINTF(D_FET, ("%s: UP cnt = %d\n",
str, ppmd->domp->pwr_cnt));
}
}
PPM_UNLOCK_DOMAIN(ppmd->domp);
ASSERT(ppmd->domp->pwr_cnt > 0);
if ((*result = (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS) {
DPRINTF(D_FET, ("%s: \"%s\" power change failed \n",
str, ppmd->path));
}
PPM_LOCK_DOMAIN(ppmd->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(ppmd->domp->pwr_cnt > 0);
ppmd->domp->pwr_cnt--;
DPRINTF(D_FET, ("%s: DN cnt = %d\n", str, ppmd->domp->pwr_cnt));
if (ppmd->domp->pwr_cnt == 0)
(void) xcppm_gpio_port2(XCPPM_CLRBIT, DRVON);
}
PPM_UNLOCK_DOMAIN(ppmd->domp);
ASSERT(ppmd->domp->pwr_cnt >= 0);
return (*result == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
}
/*
* Since UPA64S relies on PCI B staying at nominal 33MHz in order to
* have its interrupt pulse function properly, we ensure
* - Lowering PCI B only if UPA64S is at low power, otherwise defer
* the action until UPA64S goes down; hence right after UPA64S goes
* down, perform the deferred action for PCI B;
* - Always raise PCI B power prior to raising UPA64S power.
*
* Both UPA64S and PCI B devices are considered each other's dependency
* device whenever actual power transition is handled (PMR_PPM_SET_POWER).
*/
static int
xcppm_manage_pciupa(dev_info_t *dip, power_req_t *reqp, int *result)
{
#ifdef DEBUG
char *str = "xcppm_manage_pciupa";
#endif
int (*pwr_func)(ppm_dev_t *, int, int);
uint_t flags = 0, co_flags = 0;
ppm_dev_t *ppmd, *codev;
int new, cmpt, retval;
ppmd = PPM_GET_PRIVATE(dip);
DPRINTF(D_PCIUPA, ("%s: \"%s\", req %s\n", str,
ppmd->path, ppm_get_ctlstr(reqp->request_type, ~0)));
*result = DDI_SUCCESS;
switch (reqp->request_type) {
case PMR_PPM_SET_POWER:
pwr_func = xcppm_change_power_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 = xcppm_record_level_change;
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);
}
/* Common code for SET_POWER and POWER_CHANGE_NOTIFY cases */
ASSERT(ppmd); /* since it should be locked already */
if (new == ppmd->level)
return (DDI_SUCCESS);
DPRINTF(D_PCIUPA, ("%s: \"%s\", levels: current %d, new %d\n",
str, ppmd->path, ppmd->level, new));
/*
* find power-wise co-related device
*/
flags = ppmd->flags;
#ifdef DEBUG
if (flags & ~(XCPPMF_PCIB|XCPPMF_UPA))
DPRINTF(D_ERROR, ("%s: invalid ppmd->flags value 0x%x\n", str,
ppmd->flags));
#endif
if (flags == XCPPMF_UPA)
co_flags = XCPPMF_PCIB;
else if (flags == XCPPMF_PCIB)
co_flags = XCPPMF_UPA;
for (codev = ppmd->domp->devlist; codev; codev = codev->next)
if ((codev->cmpt == 0) && (codev->flags == co_flags))
break;
if (new > ppmd->level) {
/*
* Raise power level -
* pre-raising: upa ensure pci is powered up.
*/
if ((flags == XCPPMF_UPA) && codev &&
(codev->level != codev->highest)) {
if ((retval = xcppm_change_power_level(codev,
0, codev->highest)) != DDI_SUCCESS &&
codev->level != codev->highest) {
*result = retval;
return (DDI_FAILURE);
}
}
if ((retval = (*pwr_func)(ppmd, 0, new)) != DDI_SUCCESS) {
*result = retval;
return (DDI_FAILURE);
}
} else if (new < ppmd->level) {
/*
* Lower power level
*
* once upa is attached, pci checks upa level:
* if upa is at high level, defer the request and return.
* otherwise, set power level then check and lower pci level.
*/
if ((flags == XCPPMF_PCIB) && codev &&
(codev->level != codev->lowest)) {
ppmd->rplvl = new;
return (DDI_SUCCESS);
}
if ((retval = (*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS &&
ppmd->level != new) {
*result = retval;
return (DDI_FAILURE);
}
if (flags == XCPPMF_UPA) {
if (codev && (codev->rplvl != PM_LEVEL_UNKNOWN) &&
(codev->rplvl < codev->level)) {
DPRINTF(D_PCIUPA, ("%s: codev \"%s\" "
"rplvl %d level %d\n", str, codev->path,
codev->rplvl, codev->level));
if ((retval = xcppm_change_power_level(
codev, 0, codev->rplvl)) != DDI_SUCCESS) {
*result = retval;
return (DDI_FAILURE);
}
}
}
}
return (DDI_SUCCESS);
}
/*
* When all of the children of the 1394 nexus are idle, a call will be
* made to the nexus driver's own power entry point to lower power. Ppm
* intercepts this and kills 1394 cable power (since the driver doesn't
* have access to the required register). Similar logic applies when
* coming up from the state where all the children were off.
*/
static int
xcppm_manage_1394(dev_info_t *dip, power_req_t *reqp, int *result)
{
#ifdef DEBUG
char *str = "xcppm_manage_1394";
#endif
int (*pwr_func)(ppm_dev_t *, int, int);
int new, old, cmpt;
ppm_dev_t *ppmd;
ppmd = PPM_GET_PRIVATE(dip);
DPRINTF(D_1394, ("%s: \"%s\", req %s\n", str,
ppmd->path, ppm_get_ctlstr(reqp->request_type, ~0)));
switch (reqp->request_type) {
case PMR_PPM_SET_POWER:
pwr_func = xcppm_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 = xcppm_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:
return (*result = DDI_FAILURE);
}
/* Common code for SET_POWER and POWER_CHANGE_NOTIFY cases */
DPRINTF(D_1394, ("%s: dev %s@%s, old %d new %d\n", str,
ddi_binding_name(dip), ddi_get_name_addr(dip), old, new));
ASSERT(ppmd); /* since it must already be locked */
ASSERT(old == ppmd->level);
if (new == ppmd->level)
return (*result = DDI_SUCCESS);
/* the reduce power case */
if (cmpt == 0 && new < ppmd->level) {
if ((*result =
(*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS) {
return (DDI_FAILURE);
}
if (new == ppmd->lowest)
(void) xcppm_gpio_port2(XCPPM_CLRBIT, CPEN);
ppmd->level = new;
return (DDI_SUCCESS);
}
/* the increase power case */
if (cmpt == 0 && new > ppmd->level) {
if (ppmd->level == ppmd->lowest) {
(void) xcppm_gpio_port2(XCPPM_SETBIT, CPEN);
delay(1);
}
/*
* Even if pwr_func fails we need to check current level again
* because it could have been changed by an intervening
* POWER_CHANGE_NOTIFY operation.
*/
if ((*result =
(*pwr_func)(ppmd, cmpt, new)) != DDI_SUCCESS &&
ppmd->level == ppmd->lowest) {
(void) xcppm_gpio_port2(XCPPM_CLRBIT, CPEN);
} else {
ppmd->level = new;
}
return (*result == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
}
/*
* We get here if component was non-zero. This is not what we
* expect. Let the device deal with it and just pass back the
* result.
*/
*result = xcppm_change_power_level(ppmd, cmpt, new);
return (*result == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
}
/*
* lock, unlock, or trylock for one power mutex
*/
static void
xcppm_lock_one(ppm_dev_t *ppmd, power_req_t *reqp, int *iresp)
{
switch (reqp->request_type) {
case PMR_PPM_LOCK_POWER:
pm_lock_power_single(ppmd->dip,
reqp->req.ppm_lock_power_req.circp);
break;
case PMR_PPM_UNLOCK_POWER:
pm_unlock_power_single(ppmd->dip,
reqp->req.ppm_unlock_power_req.circ);
break;
case PMR_PPM_TRY_LOCK_POWER:
*iresp = pm_try_locking_power_single(ppmd->dip,
reqp->req.ppm_lock_power_req.circp);
break;
}
}
/*
* lock, unlock, or trylock all devices within a domain.
*/
static void
xcppm_lock_all(ppm_domain_t *domp, power_req_t *reqp, int *iresp)
{
/*
* To simplify the implementation we let all the devices
* in the domain be represented by a single device (dip).
* We use the first device in the domain's devlist. This
* is safe because we return with the domain lock held
* which prevents the list from changing.
*/
if (reqp->request_type == PMR_PPM_LOCK_POWER) {
if (!MUTEX_HELD(&domp->lock))
mutex_enter(&domp->lock);
domp->refcnt++;
ASSERT(domp->devlist != NULL);
pm_lock_power_single(domp->devlist->dip,
reqp->req.ppm_lock_power_req.circp);
/* domain lock remains held */
return;
} else if (reqp->request_type == PMR_PPM_UNLOCK_POWER) {
ASSERT(MUTEX_HELD(&domp->lock));
ASSERT(domp->devlist != NULL);
pm_unlock_power_single(domp->devlist->dip,
reqp->req.ppm_unlock_power_req.circ);
if (--domp->refcnt == 0)
mutex_exit(&domp->lock);
return;
}
ASSERT(reqp->request_type == PMR_PPM_TRY_LOCK_POWER);
if (!MUTEX_HELD(&domp->lock))
if (!mutex_tryenter(&domp->lock)) {
*iresp = 0;
return;
}
*iresp = pm_try_locking_power_single(domp->devlist->dip,
reqp->req.ppm_lock_power_req.circp);
if (*iresp)
domp->refcnt++;
else
mutex_exit(&domp->lock);
}
/*
* The pm framework calls us here to manage power for a device.
* We maintain state which tells us whether we need to turn off/on
* system board power components based on the status of all the devices
* sharing a component.
*
*/
/* ARGSUSED */
static int
xcppm_ctlops(dev_info_t *dip, dev_info_t *rdip,
ddi_ctl_enum_t ctlop, void *arg, void *result)
{
power_req_t *reqp = arg;
xcppm_unit_t *unitp;
ppm_domain_t *domp;
ppm_dev_t *ppmd;
#ifdef DEBUG
char path[MAXPATHLEN], *ctlstr, *str = "xcppm_ctlops";
uint_t mask = ppm_debug & (D_CTLOPS1 | D_CTLOPS2);
if (mask && (ctlstr = ppm_get_ctlstr(reqp->request_type, mask))) {
prom_printf("%s: \"%s\", %s\n", str,
ddi_pathname(rdip, path), ctlstr);
}
#endif
if (ctlop != DDI_CTLOPS_POWER)
return (DDI_FAILURE);
switch (reqp->request_type) {
case PMR_PPM_UNMANAGE:
case PMR_PPM_PRE_PROBE:
case PMR_PPM_POST_PROBE:
case PMR_PPM_PRE_ATTACH:
case PMR_PPM_PRE_DETACH:
return (DDI_SUCCESS);
/*
* There is no hardware configuration required to be done on this
* platform prior to installing drivers.
*/
case PMR_PPM_INIT_CHILD:
case PMR_PPM_UNINIT_CHILD:
return (DDI_SUCCESS);
case PMR_PPM_ALL_LOWEST:
DPRINTF(D_LOWEST, ("%s: all devices at lowest power = %d\n",
str, reqp->req.ppm_all_lowest_req.mode));
if (reqp->req.ppm_all_lowest_req.mode == PM_ALL_LOWEST) {
unitp = ddi_get_soft_state(ppm_statep, ppm_inst);
mutex_enter(&unitp->unit_lock);
if (unitp->state & XCPPM_ST_SUSPENDED) {
mutex_exit(&unitp->unit_lock);
return (DDI_SUCCESS);
}
xcppm_set_led(PPM_LEDON);
unitp->led_tid = timeout(xcppm_blink_led,
(void *)PPM_LEDON, PPM_LEDON_INTERVAL);
mutex_exit(&unitp->unit_lock);
DPRINTF(D_LOWEST, ("%s: LED blink started\n", str));
} else {
xcppm_freeze_led((void *)PPM_LEDON);
DPRINTF(D_LOWEST, ("%s: LED freeze ON\n", str));
}
return (DDI_SUCCESS);
case PMR_PPM_POST_ATTACH:
/*
* After a successful attach, if we haven't already created
* our private data structure for this device, ppm_get_dev()
* will force it to be created.
*/
ppmd = PPM_GET_PRIVATE(rdip);
if (reqp->req.ppm_config_req.result != DDI_SUCCESS) {
if (ppmd)
ppm_rem_dev(rdip);
} else if (!ppmd) {
domp = ppm_lookup_dev(rdip);
ASSERT(domp);
(void) ppm_get_dev(rdip, domp);
}
return (DDI_SUCCESS);
case PMR_PPM_POST_DETACH:
xcppm_detach_ctlop(rdip, reqp);
*(int *)result = DDI_SUCCESS;
return (DDI_SUCCESS);
case PMR_PPM_PRE_RESUME:
xcppm_resume_ctlop(rdip, reqp);
return (DDI_SUCCESS);
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);
}
ASSERT(domp->dflags == PPMD_LOCK_ALL ||
domp->dflags == PPMD_LOCK_ONE);
DPRINTF(D_LOCKS, ("xcppm_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)
xcppm_lock_all(domp, reqp, result);
else
xcppm_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);
default:
ppmd = PPM_GET_PRIVATE(rdip);
if (ppmd == NULL) {
domp = ppm_lookup_dev(rdip);
ASSERT(domp);
ppmd = ppm_get_dev(rdip, domp);
}
#ifdef DEBUG
if ((reqp->request_type == PMR_PPM_SET_POWER) &&
(ppm_debug & D_SETPWR)) {
prom_printf("%s: \"%s\", PMR_PPM_SET_POWER\n",
str, ppmd->path);
}
#endif
if (ppmd->domp == &xcppm_cpu)
return (xcppm_manage_cpus(rdip, reqp, result));
else if (ppmd->domp == &xcppm_fet)
return (xcppm_manage_fet(rdip, reqp, result));
else if (ppmd->domp == &xcppm_upa)
return (xcppm_manage_pciupa(rdip, reqp, result));
else {
ASSERT(ppmd->domp == &xcppm_1394);
return (xcppm_manage_1394(rdip, reqp, result));
}
}
}
/*
* Initialize our private version of real power level
* as well as lowest and highest levels the device supports;
* see ppmf and ppm_add_dev
*/
static void
xcppm_dev_init(ppm_dev_t *ppmd)
{
struct pm_component *dcomps;
struct pm_comp *pm_comp;
dev_info_t *dip;
int maxi;
ASSERT(MUTEX_HELD(&ppmd->domp->lock));
ppmd->level = PM_LEVEL_UNKNOWN;
ppmd->rplvl = PM_LEVEL_UNKNOWN;
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];
}
/*
* add any domain-specific initialization here
*/
if (ppmd->domp == &xcppm_fet) {
/*
* when a new device is added to domain_powefet
* it is counted here as being powered up.
*/
ppmd->domp->pwr_cnt++;
DPRINTF(D_FET, ("xcppm_dev_init: UP cnt = %d\n",
ppmd->domp->pwr_cnt));
} else if (ppmd->domp == &xcppm_upa) {
/*
* There may be a better way to determine the device type
* instead of comparing to hard coded string names.
*/
if (strstr(ppmd->path, "pci@8,700000"))
ppmd->flags = XCPPMF_PCIB;
else if (strstr(ppmd->path, "upa@8,480000"))
ppmd->flags = XCPPMF_UPA;
}
}
/*
* see ppmf and ppm_rem_dev
*/
static void
xcppm_dev_fini(ppm_dev_t *ppmd)
{
ASSERT(MUTEX_HELD(&ppmd->domp->lock));
if (ppmd->domp == &xcppm_fet) {
if (ppmd->level != ppmd->lowest) {
ppmd->domp->pwr_cnt--;
DPRINTF(D_FET, ("xcppm_dev_fini: DN cnt = %d\n",
ppmd->domp->pwr_cnt));
};
}
}
/*
* see ppmf and ppm_ioctl, PPMIOCSET
*/
static void
xcppm_iocset(uint8_t value)
{
int action;
if (value == PPM_IDEV_POWER_ON)
action = XCPPM_SETBIT;
else if (value == PPM_IDEV_POWER_OFF)
action = XCPPM_CLRBIT;
(void) xcppm_gpio_port2(action, DRVON);
}
/*
* see ppmf and ppm_ioctl, PPMIOCGET
*/
static uint8_t
xcppm_iocget(void)
{
uint8_t bit;
bit = xcppm_gpio_port2(XCPPM_GETBIT, DRVON);
return ((bit == DRVON) ? PPM_IDEV_POWER_ON : PPM_IDEV_POWER_OFF);
}
|