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
|
/*
* 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 (c) 2009, Intel Corporation.
* All rights reserved.
*/
#include <sys/atomic.h>
#include <sys/cpuvar.h>
#include <sys/cpu.h>
#include <sys/cpu_event.h>
#include <sys/cmn_err.h>
#include <sys/ddi.h>
#include <sys/kmem.h>
#include <sys/kstat.h>
#include <sys/pci.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/synch.h>
#include <sys/sysmacros.h>
#include <sys/fipe.h>
#include <vm/hat.h>
/* Current PM policy, configurable through /etc/system and fipe.conf. */
fipe_pm_policy_t fipe_pm_policy = FIPE_PM_POLICY_BALANCE;
int fipe_pm_throttle_level = 1;
/* Enable kstat support. */
#define FIPE_KSTAT_SUPPORT 1
/* Enable performance relative statistics. */
#define FIPE_KSTAT_DETAIL 1
/* Enable builtin IOAT driver if no IOAT driver is available. */
#define FIPE_IOAT_BUILTIN 0
#if defined(FIPE_IOAT_BUILTIN) && (FIPE_IOAT_BUILTIN == 0)
#undef FIPE_IOAT_BUILTIN
#endif
#ifdef FIPE_IOAT_BUILTIN
/* Use IOAT channel 3 to generate memory transactions. */
#define FIPE_IOAT_CHAN_CTRL 0x200
#define FIPE_IOAT_CHAN_STS_LO 0x204
#define FIPE_IOAT_CHAN_STS_HI 0x208
#define FIPE_IOAT_CHAN_ADDR_LO 0x20C
#define FIPE_IOAT_CHAN_ADDR_HI 0x210
#define FIPE_IOAT_CHAN_CMD 0x214
#define FIPE_IOAT_CHAN_ERR 0x228
#else /* FIPE_IOAT_BUILTIN */
#include <sys/dcopy.h>
#endif /* FIPE_IOAT_BUILTIN */
/* Memory controller relative PCI configuration constants. */
#define FIPE_MC_GBLACT 0x60
#define FIPE_MC_THRTLOW 0x64
#define FIPE_MC_THRTCTRL 0x67
#define FIPE_MC_THRTCTRL_HUNT 0x1
/* Hardware recommended values. */
#define FIPE_MC_MEMORY_OFFSET 1024
#define FIPE_MC_MEMORY_SIZE 128
/* Number of IOAT commands posted when entering idle. */
#define FIPE_IOAT_CMD_NUM 2
/* Resource allocation retry interval in microsecond. */
#define FIPE_IOAT_RETRY_INTERVAL (15 * 1000 * 1000)
/* Statistics update interval in nanosecond. */
#define FIPE_STAT_INTERVAL (10 * 1000 * 1000)
/* Configuration profile support. */
#define FIPE_PROFILE_FIELD(field) (fipe_profile_curr->field)
#define FIPE_PROF_IDLE_COUNT FIPE_PROFILE_FIELD(idle_count)
#define FIPE_PROF_BUSY_THRESHOLD FIPE_PROFILE_FIELD(busy_threshold)
#define FIPE_PROF_INTR_THRESHOLD FIPE_PROFILE_FIELD(intr_threshold)
#define FIPE_PROF_INTR_BUSY_THRESHOLD FIPE_PROFILE_FIELD(intr_busy_threshold)
#define FIPE_PROF_INTR_BUSY_THROTTLE FIPE_PROFILE_FIELD(intr_busy_throttle)
/* Priority assigned to FIPE memory power management driver on x86. */
#define CPU_IDLE_CB_PRIO_FIPE (CPU_IDLE_CB_PRIO_LOW_BASE + 0x4000000)
/* Structure to support power management profile. */
#pragma align CPU_CACHE_COHERENCE_SIZE(fipe_profiles)
static struct fipe_profile {
uint32_t idle_count;
uint32_t busy_threshold;
uint32_t intr_threshold;
uint32_t intr_busy_threshold;
uint32_t intr_busy_throttle;
} fipe_profiles[FIPE_PM_POLICY_MAX] = {
{ 0, 0, 0, 0, 0 },
{ 5, 30, 20, 50, 5 },
{ 10, 40, 40, 75, 4 },
{ 15, 50, 60, 100, 2 },
};
/* Structure to store memory controller relative data. */
#pragma align CPU_CACHE_COHERENCE_SIZE(fipe_mc_ctrl)
static struct fipe_mc_ctrl {
ddi_acc_handle_t mc_pci_hdl;
unsigned char mc_thrtctrl;
unsigned char mc_thrtlow;
unsigned char mc_gblact;
dev_info_t *mc_dip;
boolean_t mc_initialized;
} fipe_mc_ctrl;
/* Structure to store IOAT relative information. */
#pragma align CPU_CACHE_COHERENCE_SIZE(fipe_ioat_ctrl)
static struct fipe_ioat_control {
kmutex_t ioat_lock;
boolean_t ioat_ready;
#ifdef FIPE_IOAT_BUILTIN
boolean_t ioat_reg_mapped;
ddi_acc_handle_t ioat_reg_handle;
uint8_t *ioat_reg_addr;
uint64_t ioat_cmd_physaddr;
#else /* FIPE_IOAT_BUILTIN */
dcopy_cmd_t ioat_cmds[FIPE_IOAT_CMD_NUM + 1];
dcopy_handle_t ioat_handle;
#endif /* FIPE_IOAT_BUILTIN */
dev_info_t *ioat_dev_info;
uint64_t ioat_buf_physaddr;
char *ioat_buf_virtaddr;
char *ioat_buf_start;
size_t ioat_buf_size;
timeout_id_t ioat_timerid;
boolean_t ioat_failed;
boolean_t ioat_cancel;
boolean_t ioat_try_alloc;
} fipe_ioat_ctrl;
#pragma align CPU_CACHE_COHERENCE_SIZE(fipe_idle_ctrl)
static struct fipe_idle_ctrl {
boolean_t idle_ready;
cpu_idle_callback_handle_t cb_handle;
cpu_idle_prop_handle_t prop_enter;
cpu_idle_prop_handle_t prop_exit;
cpu_idle_prop_handle_t prop_busy;
cpu_idle_prop_handle_t prop_idle;
cpu_idle_prop_handle_t prop_intr;
/* Put here for cache efficiency, it should be in fipe_global_ctrl. */
hrtime_t tick_interval;
} fipe_idle_ctrl;
/*
* Global control structure.
* Solaris idle thread has no reentrance issue, so it's enough to count CPUs
* in idle state. Otherwise cpuset_t bitmap should be used to track idle CPUs.
*/
#pragma align CPU_CACHE_COHERENCE_SIZE(fipe_gbl_ctrl)
static struct fipe_global_ctrl {
kmutex_t lock;
boolean_t pm_enabled;
volatile boolean_t pm_active;
volatile uint32_t cpu_count;
volatile uint64_t io_waiters;
hrtime_t enter_ts;
hrtime_t time_in_pm;
size_t state_size;
char *state_buf;
#ifdef FIPE_KSTAT_SUPPORT
kstat_t *fipe_kstat;
#endif /* FIPE_KSTAT_SUPPORT */
} fipe_gbl_ctrl;
#define FIPE_CPU_STATE_PAD (128 - \
2 * sizeof (boolean_t) - 4 * sizeof (hrtime_t) - \
2 * sizeof (uint64_t) - 2 * sizeof (uint32_t))
/* Per-CPU status. */
#pragma pack(1)
typedef struct fipe_cpu_state {
boolean_t cond_ready;
boolean_t state_ready;
uint32_t idle_count;
uint32_t throttle_cnt;
hrtime_t throttle_ts;
hrtime_t next_ts;
hrtime_t last_busy;
hrtime_t last_idle;
uint64_t last_intr;
uint64_t last_iowait;
char pad1[FIPE_CPU_STATE_PAD];
} fipe_cpu_state_t;
#pragma pack()
#ifdef FIPE_KSTAT_SUPPORT
#pragma align CPU_CACHE_COHERENCE_SIZE(fipe_kstat)
static struct fipe_kstat_s {
kstat_named_t fipe_enabled;
kstat_named_t fipe_policy;
kstat_named_t fipe_pm_time;
#ifdef FIPE_KSTAT_DETAIL
kstat_named_t ioat_ready;
kstat_named_t pm_tryenter_cnt;
kstat_named_t pm_success_cnt;
kstat_named_t pm_race_cnt;
kstat_named_t cpu_loop_cnt;
kstat_named_t cpu_busy_cnt;
kstat_named_t cpu_idle_cnt;
kstat_named_t cpu_intr_busy_cnt;
kstat_named_t cpu_intr_throttle_cnt;
kstat_named_t bio_busy_cnt;
kstat_named_t ioat_start_fail_cnt;
kstat_named_t ioat_stop_fail_cnt;
#endif /* FIPE_KSTAT_DETAIL */
} fipe_kstat = {
{ "fipe_enabled", KSTAT_DATA_INT32 },
{ "fipe_policy", KSTAT_DATA_INT32 },
{ "fipe_pm_time", KSTAT_DATA_UINT64 },
#ifdef FIPE_KSTAT_DETAIL
{ "ioat_ready", KSTAT_DATA_INT32 },
{ "pm_tryenter_cnt", KSTAT_DATA_UINT64 },
{ "pm_success_cnt", KSTAT_DATA_UINT64 },
{ "pm_race_cnt", KSTAT_DATA_UINT64 },
{ "cpu_loop_cnt", KSTAT_DATA_UINT64 },
{ "cpu_busy_cnt", KSTAT_DATA_UINT64 },
{ "cpu_idle_cnt", KSTAT_DATA_UINT64 },
{ "cpu_intr_busy_cnt", KSTAT_DATA_UINT64 },
{ "cpu_intr_thrt_cnt", KSTAT_DATA_UINT64 },
{ "bio_busy_cnt", KSTAT_DATA_UINT64 },
{ "ioat_start_fail_cnt", KSTAT_DATA_UINT64 },
{ "ioat_stop_fail_cnt", KSTAT_DATA_UINT64 }
#endif /* FIPE_KSTAT_DETAIL */
};
#define FIPE_KSTAT_INC(v) \
atomic_inc_64(&fipe_kstat.v.value.ui64)
#ifdef FIPE_KSTAT_DETAIL
#define FIPE_KSTAT_DETAIL_INC(v) \
atomic_inc_64(&fipe_kstat.v.value.ui64)
#else /* FIPE_KSTAT_DETAIL */
#define FIPE_KSTAT_DETAIL_INC(v)
#endif /* FIPE_KSTAT_DETAIL */
#else /* FIPE_KSTAT_SUPPORT */
#define FIPE_KSTAT_INC(v)
#define FIPE_KSTAT_DETAIL_INC(v)
#endif /* FIPE_KSTAT_SUPPORT */
/* Save current power management profile during suspend/resume. */
static fipe_pm_policy_t fipe_pm_policy_saved = FIPE_PM_POLICY_BALANCE;
static fipe_cpu_state_t *fipe_cpu_states = NULL;
/*
* There is no lock to protect fipe_profile_curr, so fipe_profile_curr
* could change on threads in fipe_idle_enter. This is not an issue,
* as it always points to a valid profile, and though it might make
* an incorrect choice for the new profile, it will still be a valid
* selection, and would do the correct operation for the new profile on
* next cpu_idle_enter cycle. Since the selections would always be
* valid for some profile, the overhead for the lock is not wasted.
*/
static struct fipe_profile *fipe_profile_curr = NULL;
static void fipe_idle_enter(void *arg, cpu_idle_callback_context_t ctx,
cpu_idle_check_wakeup_t check_func, void* check_arg);
static void fipe_idle_exit(void* arg, cpu_idle_callback_context_t ctx,
int flags);
static cpu_idle_callback_t fipe_idle_cb = {
CPU_IDLE_CALLBACK_VER0,
fipe_idle_enter,
fipe_idle_exit,
};
/*
* Configure memory controller into power saving mode:
* 1) OLTT activation limit is set to unlimited
* 2) MC works in S-CLTT mode
*/
static int
fipe_mc_change(int throttle)
{
/* Enable OLTT/disable S-CLTT mode */
pci_config_put8(fipe_mc_ctrl.mc_pci_hdl, FIPE_MC_THRTCTRL,
fipe_mc_ctrl.mc_thrtctrl & ~FIPE_MC_THRTCTRL_HUNT);
/* Set OLTT activation limit to unlimited */
pci_config_put8(fipe_mc_ctrl.mc_pci_hdl, FIPE_MC_GBLACT, 0);
/*
* Set S-CLTT low throttling to desired value. The lower value,
* the more power saving and the less available memory bandwidth.
*/
pci_config_put8(fipe_mc_ctrl.mc_pci_hdl, FIPE_MC_THRTLOW, throttle);
/* Enable S-CLTT/disable OLTT mode */
pci_config_put8(fipe_mc_ctrl.mc_pci_hdl, FIPE_MC_THRTCTRL,
fipe_mc_ctrl.mc_thrtctrl | FIPE_MC_THRTCTRL_HUNT);
return (0);
}
/*
* Restore memory controller's original configuration.
*/
static void
fipe_mc_restore(void)
{
pci_config_put8(fipe_mc_ctrl.mc_pci_hdl, FIPE_MC_THRTCTRL,
fipe_mc_ctrl.mc_thrtctrl & ~FIPE_MC_THRTCTRL_HUNT);
pci_config_put8(fipe_mc_ctrl.mc_pci_hdl, FIPE_MC_GBLACT,
fipe_mc_ctrl.mc_gblact);
pci_config_put8(fipe_mc_ctrl.mc_pci_hdl, FIPE_MC_THRTLOW,
fipe_mc_ctrl.mc_thrtlow);
pci_config_put8(fipe_mc_ctrl.mc_pci_hdl, FIPE_MC_THRTCTRL,
fipe_mc_ctrl.mc_thrtctrl);
}
/*
* Initialize memory controller's data structure and status.
*/
static int
fipe_mc_init(dev_info_t *dip)
{
ddi_acc_handle_t handle;
bzero(&fipe_mc_ctrl, sizeof (fipe_mc_ctrl));
/* Hold one reference count and will be released in fipe_mc_fini. */
ndi_hold_devi(dip);
/* Setup pci configuration handler. */
if (pci_config_setup(dip, &handle) != DDI_SUCCESS) {
cmn_err(CE_WARN,
"!fipe: failed to setup pcicfg handler in mc_init.");
ndi_rele_devi(dip);
return (-1);
}
/* Save original configuration. */
fipe_mc_ctrl.mc_thrtctrl = pci_config_get8(handle, FIPE_MC_THRTCTRL);
fipe_mc_ctrl.mc_thrtlow = pci_config_get8(handle, FIPE_MC_THRTLOW);
fipe_mc_ctrl.mc_gblact = pci_config_get8(handle, FIPE_MC_GBLACT);
fipe_mc_ctrl.mc_dip = dip;
fipe_mc_ctrl.mc_pci_hdl = handle;
fipe_mc_ctrl.mc_initialized = B_TRUE;
return (0);
}
/*
* Restore memory controller's configuration and release resources.
*/
static void
fipe_mc_fini(void)
{
if (fipe_mc_ctrl.mc_initialized) {
fipe_mc_restore();
pci_config_teardown(&fipe_mc_ctrl.mc_pci_hdl);
ndi_rele_devi(fipe_mc_ctrl.mc_dip);
fipe_mc_ctrl.mc_initialized = B_FALSE;
}
bzero(&fipe_mc_ctrl, sizeof (fipe_mc_ctrl));
}
/* Search device with specific pci ids. */
struct fipe_pci_ioat_id {
uint16_t venid;
uint16_t devid;
uint16_t subvenid;
uint16_t subsysid;
char *unitaddr;
};
static struct fipe_pci_ioat_id fipe_pci_ioat_ids[] = {
{ 0x8086, 0x1a38, 0xffff, 0xffff, NULL },
{ 0x8086, 0x360b, 0xffff, 0xffff, NULL },
};
/*ARGSUSED*/
static int
fipe_search_ioat_dev(dev_info_t *dip, void *arg)
{
char *unit;
struct fipe_pci_ioat_id *id;
int i, max, venid, devid, subvenid, subsysid;
/* Query PCI id properties. */
venid = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"vendor-id", 0xffffffff);
if (venid == 0xffffffff) {
return (DDI_WALK_CONTINUE);
}
devid = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"device-id", 0xffffffff);
if (devid == 0xffffffff) {
return (DDI_WALK_CONTINUE);
}
subvenid = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"subsystem-vendor-id", 0xffffffff);
if (subvenid == 0xffffffff) {
return (DDI_WALK_CONTINUE);
}
subsysid = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"subsystem-id", 0xffffffff);
if (subvenid == 0xffffffff) {
return (DDI_WALK_CONTINUE);
}
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"unit-address", &unit) != DDI_PROP_SUCCESS) {
return (DDI_WALK_CONTINUE);
}
max = sizeof (fipe_pci_ioat_ids) / sizeof (fipe_pci_ioat_ids[0]);
for (i = 0; i < max; i++) {
id = &fipe_pci_ioat_ids[i];
if ((id->venid == 0xffffu || id->venid == venid) &&
(id->devid == 0xffffu || id->devid == devid) &&
(id->subvenid == 0xffffu || id->subvenid == subvenid) &&
(id->subsysid == 0xffffu || id->subsysid == subsysid) &&
(id->unitaddr == NULL || strcmp(id->unitaddr, unit) == 0)) {
break;
}
}
ddi_prop_free(unit);
if (i >= max) {
return (DDI_WALK_CONTINUE);
}
/* Found IOAT device, hold one reference count. */
ndi_hold_devi(dip);
fipe_ioat_ctrl.ioat_dev_info = dip;
return (DDI_WALK_TERMINATE);
}
/*
* To enable FBDIMM idle power enhancement mechanism, IOAT will be used to
* generate enough memory traffic to trigger memory controller thermal throttle
* circuitry.
* If dcopy/ioat is available, we will use dcopy interface to communicate
* with IOAT. Otherwise the built-in driver will directly talk to IOAT
* hardware.
*/
#ifdef FIPE_IOAT_BUILTIN
static int
fipe_ioat_trigger(void)
{
uint16_t ctrl;
uint32_t err;
uint8_t *addr = fipe_ioat_ctrl.ioat_reg_addr;
ddi_acc_handle_t handle = fipe_ioat_ctrl.ioat_reg_handle;
/* Check channel in use flag. */
ctrl = ddi_get16(handle, (uint16_t *)(addr + FIPE_IOAT_CHAN_CTRL));
if (ctrl & 0x100) {
/*
* Channel is in use by somebody else. IOAT driver may have
* been loaded, forbid fipe from accessing IOAT hardware
* anymore.
*/
fipe_ioat_ctrl.ioat_ready = B_FALSE;
fipe_ioat_ctrl.ioat_failed = B_TRUE;
FIPE_KSTAT_INC(ioat_start_fail_cnt);
return (-1);
} else {
/* Set channel in use flag. */
ddi_put16(handle,
(uint16_t *)(addr + FIPE_IOAT_CHAN_CTRL), 0x100);
}
/* Write command address. */
ddi_put32(handle,
(uint32_t *)(addr + FIPE_IOAT_CHAN_ADDR_LO),
(uint32_t)fipe_ioat_ctrl.ioat_cmd_physaddr);
ddi_put32(handle, (uint32_t *)(addr + FIPE_IOAT_CHAN_ADDR_HI),
(uint32_t)(fipe_ioat_ctrl.ioat_cmd_physaddr >> 32));
/* Check and clear error flags. */
err = ddi_get32(handle, (uint32_t *)(addr + FIPE_IOAT_CHAN_ERR));
if (err != 0) {
ddi_put32(handle, (uint32_t *)(addr + FIPE_IOAT_CHAN_ERR), err);
}
/* Start channel. */
ddi_put8(handle, (uint8_t *)(addr + FIPE_IOAT_CHAN_CMD), 0x1);
return (0);
}
static void
fipe_ioat_cancel(void)
{
uint32_t status;
uint8_t *addr = fipe_ioat_ctrl.ioat_reg_addr;
ddi_acc_handle_t handle = fipe_ioat_ctrl.ioat_reg_handle;
/*
* Reset channel. Sometimes reset is not reliable,
* so check completion or abort status after reset.
*/
/* LINTED: constant in conditional context */
while (1) {
/* Issue reset channel command. */
ddi_put8(handle, (uint8_t *)(addr + FIPE_IOAT_CHAN_CMD), 0x20);
/* Query command status. */
status = ddi_get32(handle,
(uint32_t *)(addr + FIPE_IOAT_CHAN_STS_LO));
if (status & 0x1) {
/* Reset channel completed. */
break;
} else {
SMT_PAUSE();
}
}
/* Put channel into "not in use" state. */
ddi_put16(handle, (uint16_t *)(addr + FIPE_IOAT_CHAN_CTRL), 0);
}
/*ARGSUSED*/
static void
fipe_ioat_alloc(void *arg)
{
int rc = 0, nregs;
dev_info_t *dip;
ddi_device_acc_attr_t attr;
boolean_t fatal = B_FALSE;
mutex_enter(&fipe_ioat_ctrl.ioat_lock);
/*
* fipe_ioat_alloc() is called in DEVICE ATTACH context when loaded.
* In DEVICE ATTACH context, it can't call ddi_walk_devs(), so just
* schedule a timer and exit.
*/
if (fipe_ioat_ctrl.ioat_try_alloc == B_FALSE) {
fipe_ioat_ctrl.ioat_try_alloc = B_TRUE;
goto out_error;
}
/* Check whether has been initialized or encountered permanent error. */
if (fipe_ioat_ctrl.ioat_ready || fipe_ioat_ctrl.ioat_failed ||
fipe_ioat_ctrl.ioat_cancel) {
fipe_ioat_ctrl.ioat_timerid = 0;
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
return;
}
if (fipe_ioat_ctrl.ioat_dev_info == NULL) {
/* Find dev_info_t for IOAT engine. */
ddi_walk_devs(ddi_root_node(), fipe_search_ioat_dev, NULL);
if (fipe_ioat_ctrl.ioat_dev_info == NULL) {
cmn_err(CE_NOTE,
"!fipe: no IOAT hardware found, disable pm.");
fatal = B_TRUE;
goto out_error;
}
}
/* Map in IOAT control register window. */
ASSERT(fipe_ioat_ctrl.ioat_dev_info != NULL);
ASSERT(fipe_ioat_ctrl.ioat_reg_mapped == B_FALSE);
dip = fipe_ioat_ctrl.ioat_dev_info;
if (ddi_dev_nregs(dip, &nregs) != DDI_SUCCESS || nregs < 2) {
cmn_err(CE_WARN, "!fipe: ioat has not enough register bars.");
fatal = B_TRUE;
goto out_error;
}
attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
rc = ddi_regs_map_setup(dip, 1,
(caddr_t *)&fipe_ioat_ctrl.ioat_reg_addr,
0, 0, &attr, &fipe_ioat_ctrl.ioat_reg_handle);
if (rc != DDI_SUCCESS) {
cmn_err(CE_WARN, "!fipe: failed to map IOAT registeres.");
fatal = B_TRUE;
goto out_error;
}
/* Mark IOAT status. */
fipe_ioat_ctrl.ioat_reg_mapped = B_TRUE;
fipe_ioat_ctrl.ioat_ready = B_TRUE;
fipe_ioat_ctrl.ioat_failed = B_FALSE;
fipe_ioat_ctrl.ioat_timerid = 0;
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
return;
out_error:
fipe_ioat_ctrl.ioat_timerid = 0;
if (!fipe_ioat_ctrl.ioat_ready && !fipe_ioat_ctrl.ioat_cancel) {
if (fatal) {
/* Mark permanent error and give up. */
fipe_ioat_ctrl.ioat_failed = B_TRUE;
/* Release reference count hold by ddi_find_devinfo. */
if (fipe_ioat_ctrl.ioat_dev_info != NULL) {
ndi_rele_devi(fipe_ioat_ctrl.ioat_dev_info);
fipe_ioat_ctrl.ioat_dev_info = NULL;
}
} else {
/*
* Schedule another timer to keep on trying.
* timeout() should always succeed, no need to check
* return.
*/
fipe_ioat_ctrl.ioat_timerid = timeout(fipe_ioat_alloc,
NULL, drv_usectohz(FIPE_IOAT_RETRY_INTERVAL));
}
}
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
}
static void
fipe_ioat_free(void)
{
mutex_enter(&fipe_ioat_ctrl.ioat_lock);
/* Cancel timeout to avoid race condition. */
if (fipe_ioat_ctrl.ioat_timerid != 0) {
fipe_ioat_ctrl.ioat_cancel = B_TRUE;
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
(void) untimeout(fipe_ioat_ctrl.ioat_timerid);
mutex_enter(&fipe_ioat_ctrl.ioat_lock);
fipe_ioat_ctrl.ioat_timerid = 0;
fipe_ioat_ctrl.ioat_cancel = B_FALSE;
}
if (fipe_ioat_ctrl.ioat_reg_mapped) {
ddi_regs_map_free(&fipe_ioat_ctrl.ioat_reg_handle);
fipe_ioat_ctrl.ioat_reg_mapped = B_FALSE;
}
fipe_ioat_ctrl.ioat_ready = B_FALSE;
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
}
#else /* FIPE_IOAT_BUILTIN */
/*
* Trigger IOAT memory copy operation when entering power saving state.
* A group of commands will be posted to IOAT driver and those commands
* will be placed into an IOAT ring buffer.
*/
static int
fipe_ioat_trigger(void)
{
int idx;
dcopy_cmd_t *cmds = fipe_ioat_ctrl.ioat_cmds;
for (idx = FIPE_IOAT_CMD_NUM; idx > 0; idx--) {
if (dcopy_cmd_post(cmds[idx]) == DCOPY_SUCCESS) {
continue;
} else {
/*
* Don't rollback on failure, it doesn't hurt much more
* than some small memory copy operations.
*/
FIPE_KSTAT_DETAIL_INC(ioat_start_fail_cnt);
return (-1);
}
}
return (0);
}
/*
* Cancel the memory copy operations posted by fipe_ioat_trigger.
* It's achieved by posting a new command which will break the ring
* created by fipe_ioat_trigger. If it fails, the best way to recover
* is to just let it go. IOAT will recover when posting next command
* on the same channel.
*/
static void
fipe_ioat_cancel(void)
{
if (dcopy_cmd_post(fipe_ioat_ctrl.ioat_cmds[0]) != DCOPY_SUCCESS) {
FIPE_KSTAT_DETAIL_INC(ioat_stop_fail_cnt);
}
}
/*
* This function will be called from allocate IOAT resources.
* Allocation may fail due to following reasons:
* 1) IOAT driver hasn't been loaded yet. Keep on trying in this case.
* 2) IOAT resources are temporarily unavailable. Keep on trying in this case.
* 3) Other no recoverable reasons. Disable power management function.
*/
/*ARGSUSED*/
static void
fipe_ioat_alloc(void *arg)
{
int idx, flags, rc = 0;
uint64_t physaddr;
boolean_t fatal = B_FALSE;
dcopy_query_t info;
dcopy_handle_t handle;
dcopy_cmd_t cmds[FIPE_IOAT_CMD_NUM + 1];
mutex_enter(&fipe_ioat_ctrl.ioat_lock);
/*
* fipe_ioat_alloc() is called in DEVICE ATTACH context when loaded.
* In DEVICE ATTACH context, it can't call ddi_walk_devs(), so just
* schedule a timer and exit.
*/
if (fipe_ioat_ctrl.ioat_try_alloc == B_FALSE) {
fipe_ioat_ctrl.ioat_try_alloc = B_TRUE;
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
goto out_error;
}
/*
* Check whether device has been initialized or if it encountered
* some permanent error.
*/
if (fipe_ioat_ctrl.ioat_ready || fipe_ioat_ctrl.ioat_failed ||
fipe_ioat_ctrl.ioat_cancel) {
fipe_ioat_ctrl.ioat_timerid = 0;
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
return;
}
if (fipe_ioat_ctrl.ioat_dev_info == NULL) {
/* Find dev_info_t for IOAT engine. */
ddi_walk_devs(ddi_root_node(), fipe_search_ioat_dev, NULL);
if (fipe_ioat_ctrl.ioat_dev_info == NULL) {
cmn_err(CE_NOTE,
"!fipe: no IOAT hardware found, disable pm.");
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
fatal = B_TRUE;
goto out_error;
}
}
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
/* Check, allocate and initialize IOAT resources with lock released. */
dcopy_query(&info);
if (info.dq_version < DCOPY_QUERY_V0) {
/* Permanent error, give up. */
cmn_err(CE_WARN, "!fipe: IOAT driver version mismatch.");
fatal = B_TRUE;
goto out_error;
} else if (info.dq_num_channels == 0) {
/* IOAT driver hasn't been loaded, keep trying. */
goto out_error;
}
/* Allocate IOAT channel. */
rc = dcopy_alloc(DCOPY_NOSLEEP, &handle);
if (rc == DCOPY_NORESOURCES) {
/* Resource temporarily not available, keep trying. */
goto out_error;
} else if (rc != DCOPY_SUCCESS) {
/* Permanent error, give up. */
cmn_err(CE_WARN, "!fipe: failed to allocate IOAT channel.");
fatal = B_TRUE;
goto out_error;
}
/*
* Allocate multiple IOAT commands and organize them into a ring to
* loop forever. Commands number is determined by IOAT descriptor size
* and memory interleave pattern.
* cmd[0] is used break the loop and disable IOAT operation.
* cmd[1, FIPE_IOAT_CMD_NUM] are grouped into a ring and cmd[1] is the
* list head.
*/
bzero(cmds, sizeof (cmds));
physaddr = fipe_ioat_ctrl.ioat_buf_physaddr;
for (idx = FIPE_IOAT_CMD_NUM; idx >= 0; idx--) {
/* Allocate IOAT commands. */
if (idx == 0 || idx == FIPE_IOAT_CMD_NUM) {
flags = DCOPY_NOSLEEP;
} else {
/*
* To link commands into a list, the initial value of
* cmd need to be set to next cmd on list.
*/
flags = DCOPY_NOSLEEP | DCOPY_ALLOC_LINK;
cmds[idx] = cmds[idx + 1];
}
rc = dcopy_cmd_alloc(handle, flags, &cmds[idx]);
if (rc == DCOPY_NORESOURCES) {
goto out_freecmd;
} else if (rc != DCOPY_SUCCESS) {
/* Permanent error, give up. */
cmn_err(CE_WARN,
"!fipe: failed to allocate IOAT command.");
fatal = B_TRUE;
goto out_freecmd;
}
/* Disable src/dst snoop to improve CPU cache efficiency. */
cmds[idx]->dp_flags = DCOPY_CMD_NOSRCSNP | DCOPY_CMD_NODSTSNP;
/* Specially handle commands on the list. */
if (idx != 0) {
/* Disable IOAT status. */
cmds[idx]->dp_flags |= DCOPY_CMD_NOSTAT;
/* Disable waiting for resources. */
cmds[idx]->dp_flags |= DCOPY_CMD_NOWAIT;
if (idx == 1) {
/* The list head, chain command into loop. */
cmds[idx]->dp_flags |= DCOPY_CMD_LOOP;
} else {
/* Queue all other commands except head. */
cmds[idx]->dp_flags |= DCOPY_CMD_QUEUE;
}
}
cmds[idx]->dp_cmd = DCOPY_CMD_COPY;
cmds[idx]->dp.copy.cc_source = physaddr;
cmds[idx]->dp.copy.cc_dest = physaddr + FIPE_MC_MEMORY_OFFSET;
if (idx == 0) {
/*
* Command 0 is used to cancel memory copy by breaking
* the ring created in fipe_ioat_trigger().
* For efficiency, use the smallest memory copy size.
*/
cmds[idx]->dp.copy.cc_size = 1;
} else {
cmds[idx]->dp.copy.cc_size = FIPE_MC_MEMORY_SIZE;
}
}
/* Update IOAT control status if it hasn't been initialized yet. */
mutex_enter(&fipe_ioat_ctrl.ioat_lock);
if (!fipe_ioat_ctrl.ioat_ready && !fipe_ioat_ctrl.ioat_cancel) {
fipe_ioat_ctrl.ioat_handle = handle;
for (idx = 0; idx <= FIPE_IOAT_CMD_NUM; idx++) {
fipe_ioat_ctrl.ioat_cmds[idx] = cmds[idx];
}
fipe_ioat_ctrl.ioat_ready = B_TRUE;
fipe_ioat_ctrl.ioat_failed = B_FALSE;
fipe_ioat_ctrl.ioat_timerid = 0;
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
return;
}
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
/* Initialized by another thread, fall through to free resources. */
out_freecmd:
if (cmds[0] != NULL) {
dcopy_cmd_free(&cmds[0]);
}
/* Only need to free head, dcopy will free all commands on the list. */
for (idx = 1; idx <= FIPE_IOAT_CMD_NUM; idx++) {
if (cmds[idx] != NULL) {
dcopy_cmd_free(&cmds[idx]);
break;
}
}
dcopy_free(&handle);
out_error:
mutex_enter(&fipe_ioat_ctrl.ioat_lock);
fipe_ioat_ctrl.ioat_timerid = 0;
if (!fipe_ioat_ctrl.ioat_ready && !fipe_ioat_ctrl.ioat_cancel) {
if (fatal) {
/* Mark permanent error and give up. */
fipe_ioat_ctrl.ioat_failed = B_TRUE;
/* Release reference count hold by ddi_find_devinfo. */
if (fipe_ioat_ctrl.ioat_dev_info != NULL) {
ndi_rele_devi(fipe_ioat_ctrl.ioat_dev_info);
fipe_ioat_ctrl.ioat_dev_info = NULL;
}
} else {
/*
* Schedule another timer to keep on trying.
* timeout() should always success, no need to check.
*/
fipe_ioat_ctrl.ioat_timerid = timeout(fipe_ioat_alloc,
NULL, drv_usectohz(FIPE_IOAT_RETRY_INTERVAL));
}
}
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
}
/*
* Free resources allocated in fipe_ioat_alloc.
*/
static void
fipe_ioat_free(void)
{
int idx = 0;
dcopy_cmd_t *cmds = fipe_ioat_ctrl.ioat_cmds;
mutex_enter(&fipe_ioat_ctrl.ioat_lock);
/* Cancel timeout to avoid race condition. */
if (fipe_ioat_ctrl.ioat_timerid != 0) {
fipe_ioat_ctrl.ioat_cancel = B_TRUE;
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
(void) untimeout(fipe_ioat_ctrl.ioat_timerid);
mutex_enter(&fipe_ioat_ctrl.ioat_lock);
fipe_ioat_ctrl.ioat_timerid = 0;
fipe_ioat_ctrl.ioat_cancel = B_FALSE;
}
/* Free ioat resources. */
if (fipe_ioat_ctrl.ioat_ready) {
if (cmds[0] != NULL) {
dcopy_cmd_free(&cmds[0]);
}
for (idx = 1; idx <= FIPE_IOAT_CMD_NUM; idx++) {
if (cmds[idx] != NULL) {
dcopy_cmd_free(&cmds[idx]);
break;
}
}
bzero(fipe_ioat_ctrl.ioat_cmds,
sizeof (fipe_ioat_ctrl.ioat_cmds));
dcopy_free(&fipe_ioat_ctrl.ioat_handle);
fipe_ioat_ctrl.ioat_handle = NULL;
fipe_ioat_ctrl.ioat_ready = B_FALSE;
}
/* Release reference count hold by ddi_find_devinfo. */
if (fipe_ioat_ctrl.ioat_dev_info != NULL) {
ndi_rele_devi(fipe_ioat_ctrl.ioat_dev_info);
fipe_ioat_ctrl.ioat_dev_info = NULL;
}
mutex_exit(&fipe_ioat_ctrl.ioat_lock);
}
#endif /* FIPE_IOAT_BUILTIN */
/*
* Initialize IOAT relative resources.
*/
static int
fipe_ioat_init(void)
{
char *buf;
size_t size;
bzero(&fipe_ioat_ctrl, sizeof (fipe_ioat_ctrl));
mutex_init(&fipe_ioat_ctrl.ioat_lock, NULL, MUTEX_DRIVER, NULL);
/*
* Allocate memory for IOAT memory copy operation.
* The allocated memory should be page aligned to achieve better power
* savings.
* Don't use ddi_dma_mem_alloc here to keep thing simple. This also
* makes quiesce easier.
*/
size = PAGESIZE;
buf = kmem_zalloc(size, KM_SLEEP);
if ((intptr_t)buf & PAGEOFFSET) {
kmem_free(buf, PAGESIZE);
size <<= 1;
buf = kmem_zalloc(size, KM_SLEEP);
}
fipe_ioat_ctrl.ioat_buf_size = size;
fipe_ioat_ctrl.ioat_buf_start = buf;
buf = (char *)P2ROUNDUP((intptr_t)buf, PAGESIZE);
fipe_ioat_ctrl.ioat_buf_virtaddr = buf;
fipe_ioat_ctrl.ioat_buf_physaddr = hat_getpfnum(kas.a_hat, buf);
fipe_ioat_ctrl.ioat_buf_physaddr <<= PAGESHIFT;
#ifdef FIPE_IOAT_BUILTIN
{
uint64_t bufpa;
/* IOAT descriptor data structure copied from ioat.h. */
struct fipe_ioat_cmd_desc {
uint32_t dd_size;
uint32_t dd_ctrl;
uint64_t dd_src_paddr;
uint64_t dd_dest_paddr;
uint64_t dd_next_desc;
uint64_t dd_res4;
uint64_t dd_res5;
uint64_t dd_res6;
uint64_t dd_res7;
} *desc;
/*
* Build two IOAT command descriptors and chain them into ring.
* Control flags as below:
* 0x2: disable source snoop
* 0x4: disable destination snoop
* 0x0 << 24: memory copy operation
* The layout for command descriptors and memory buffers are
* organized for power saving effect, please don't change it.
*/
buf = fipe_ioat_ctrl.ioat_buf_virtaddr;
bufpa = fipe_ioat_ctrl.ioat_buf_physaddr;
fipe_ioat_ctrl.ioat_cmd_physaddr = bufpa;
/* First command descriptor. */
desc = (struct fipe_ioat_cmd_desc *)(buf);
desc->dd_size = 128;
desc->dd_ctrl = 0x6;
desc->dd_src_paddr = bufpa + 2048;
desc->dd_dest_paddr = bufpa + 3072;
/* Point to second descriptor. */
desc->dd_next_desc = bufpa + 64;
/* Second command descriptor. */
desc = (struct fipe_ioat_cmd_desc *)(buf + 64);
desc->dd_size = 128;
desc->dd_ctrl = 0x6;
desc->dd_src_paddr = bufpa + 2048;
desc->dd_dest_paddr = bufpa + 3072;
/* Point to first descriptor. */
desc->dd_next_desc = bufpa;
}
#endif /* FIPE_IOAT_BUILTIN */
return (0);
}
static void
fipe_ioat_fini(void)
{
/* Release reference count hold by ddi_find_devinfo. */
if (fipe_ioat_ctrl.ioat_dev_info != NULL) {
ndi_rele_devi(fipe_ioat_ctrl.ioat_dev_info);
fipe_ioat_ctrl.ioat_dev_info = NULL;
}
if (fipe_ioat_ctrl.ioat_buf_start != NULL) {
ASSERT(fipe_ioat_ctrl.ioat_buf_size != 0);
kmem_free(fipe_ioat_ctrl.ioat_buf_start,
fipe_ioat_ctrl.ioat_buf_size);
}
mutex_destroy(&fipe_ioat_ctrl.ioat_lock);
bzero(&fipe_ioat_ctrl, sizeof (fipe_ioat_ctrl));
}
static int
fipe_idle_start(void)
{
int rc;
if (fipe_idle_ctrl.idle_ready) {
return (0);
}
if (cpu_idle_prop_create_handle(CPU_IDLE_PROP_ENTER_TIMESTAMP,
&fipe_idle_ctrl.prop_enter) != 0) {
cmn_err(CE_WARN, "!fipe: failed to get enter_ts property.");
return (-1);
}
if (cpu_idle_prop_create_handle(CPU_IDLE_PROP_EXIT_TIMESTAMP,
&fipe_idle_ctrl.prop_exit) != 0) {
cmn_err(CE_WARN, "!fipe: failed to get exit_ts property.");
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_enter);
return (-1);
}
if (cpu_idle_prop_create_handle(CPU_IDLE_PROP_TOTAL_IDLE_TIME,
&fipe_idle_ctrl.prop_idle) != 0) {
cmn_err(CE_WARN, "!fipe: failed to get idle_time property.");
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_exit);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_enter);
return (-1);
}
if (cpu_idle_prop_create_handle(CPU_IDLE_PROP_TOTAL_BUSY_TIME,
&fipe_idle_ctrl.prop_busy) != 0) {
cmn_err(CE_WARN, "!fipe: failed to get busy_time property.");
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_idle);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_exit);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_enter);
return (-1);
}
if (cpu_idle_prop_create_handle(CPU_IDLE_PROP_INTERRUPT_COUNT,
&fipe_idle_ctrl.prop_intr) != 0) {
cmn_err(CE_WARN, "!fipe: failed to get intr_count property.");
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_busy);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_idle);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_exit);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_enter);
return (-1);
}
/* Register idle state notification callback. */
rc = cpu_idle_register_callback(CPU_IDLE_CB_PRIO_FIPE, &fipe_idle_cb,
NULL, &fipe_idle_ctrl.cb_handle);
if (rc != 0) {
cmn_err(CE_WARN, "!fipe: failed to register cpuidle callback.");
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_intr);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_busy);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_idle);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_exit);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_enter);
return (-1);
}
fipe_idle_ctrl.idle_ready = B_TRUE;
return (0);
}
static int
fipe_idle_stop(void)
{
int rc;
if (fipe_idle_ctrl.idle_ready == B_FALSE) {
return (0);
}
rc = cpu_idle_unregister_callback(fipe_idle_ctrl.cb_handle);
if (rc != 0) {
cmn_err(CE_WARN,
"!fipe: failed to unregister cpuidle callback.");
return (-1);
}
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_intr);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_busy);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_idle);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_exit);
(void) cpu_idle_prop_destroy_handle(fipe_idle_ctrl.prop_enter);
fipe_idle_ctrl.idle_ready = B_FALSE;
return (0);
}
#ifdef FIPE_KSTAT_SUPPORT
static int
fipe_kstat_update(kstat_t *ksp, int rw)
{
struct fipe_kstat_s *sp;
hrtime_t hrt;
if (rw == KSTAT_WRITE) {
return (EACCES);
}
sp = ksp->ks_data;
sp->fipe_enabled.value.i32 = fipe_gbl_ctrl.pm_enabled ? 1 : 0;
sp->fipe_policy.value.i32 = fipe_pm_policy;
hrt = fipe_gbl_ctrl.time_in_pm;
scalehrtime(&hrt);
sp->fipe_pm_time.value.ui64 = (uint64_t)hrt;
#ifdef FIPE_KSTAT_DETAIL
sp->ioat_ready.value.i32 = fipe_ioat_ctrl.ioat_ready ? 1 : 0;
#endif /* FIPE_KSTAT_DETAIL */
return (0);
}
#endif /* FIPE_KSTAT_SUPPORT */
/*
* Initialize memory power management subsystem.
* Note: This function should only be called from ATTACH.
* Note: caller must ensure exclusive access to all fipe_xxx interfaces.
*/
int
fipe_init(dev_info_t *dip)
{
size_t nsize;
hrtime_t hrt;
/* Initialize global control structure. */
bzero(&fipe_gbl_ctrl, sizeof (fipe_gbl_ctrl));
mutex_init(&fipe_gbl_ctrl.lock, NULL, MUTEX_DRIVER, NULL);
/* Query power management policy from device property. */
fipe_pm_policy = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
FIPE_PROP_PM_POLICY, fipe_pm_policy);
if (fipe_pm_policy < 0 || fipe_pm_policy >= FIPE_PM_POLICY_MAX) {
cmn_err(CE_CONT,
"?fipe: invalid power management policy %d.\n",
fipe_pm_policy);
fipe_pm_policy = FIPE_PM_POLICY_BALANCE;
}
fipe_profile_curr = &fipe_profiles[fipe_pm_policy];
/*
* Compute unscaled hrtime value corresponding to FIPE_STAT_INTERVAL.
* (1 << 36) should be big enough here.
*/
hrt = 1ULL << 36;
scalehrtime(&hrt);
fipe_idle_ctrl.tick_interval = FIPE_STAT_INTERVAL * (1ULL << 36) / hrt;
if (fipe_mc_init(dip) != 0) {
cmn_err(CE_WARN, "!fipe: failed to initialize mc state.");
goto out_mc_error;
}
if (fipe_ioat_init() != 0) {
cmn_err(CE_NOTE, "!fipe: failed to initialize ioat state.");
goto out_ioat_error;
}
/* Allocate per-CPU structure. */
nsize = max_ncpus * sizeof (fipe_cpu_state_t);
nsize += CPU_CACHE_COHERENCE_SIZE;
fipe_gbl_ctrl.state_buf = kmem_zalloc(nsize, KM_SLEEP);
fipe_gbl_ctrl.state_size = nsize;
fipe_cpu_states = (fipe_cpu_state_t *)P2ROUNDUP(
(intptr_t)fipe_gbl_ctrl.state_buf, CPU_CACHE_COHERENCE_SIZE);
#ifdef FIPE_KSTAT_SUPPORT
fipe_gbl_ctrl.fipe_kstat = kstat_create("fipe", 0, "fipe-pm", "misc",
KSTAT_TYPE_NAMED, sizeof (fipe_kstat) / sizeof (kstat_named_t),
KSTAT_FLAG_VIRTUAL);
if (fipe_gbl_ctrl.fipe_kstat == NULL) {
cmn_err(CE_CONT, "?fipe: failed to create kstat object.\n");
} else {
fipe_gbl_ctrl.fipe_kstat->ks_lock = &fipe_gbl_ctrl.lock;
fipe_gbl_ctrl.fipe_kstat->ks_data = &fipe_kstat;
fipe_gbl_ctrl.fipe_kstat->ks_update = fipe_kstat_update;
kstat_install(fipe_gbl_ctrl.fipe_kstat);
}
#endif /* FIPE_KSTAT_SUPPORT */
return (0);
out_ioat_error:
fipe_mc_fini();
out_mc_error:
mutex_destroy(&fipe_gbl_ctrl.lock);
bzero(&fipe_gbl_ctrl, sizeof (fipe_gbl_ctrl));
return (-1);
}
/*
* Destroy memory power management subsystem.
* Note: This function should only be called from DETACH.
* Note: caller must ensure exclusive access to all fipe_xxx interfaces.
*/
int
fipe_fini(void)
{
if (fipe_gbl_ctrl.pm_enabled) {
cmn_err(CE_NOTE, "!fipe: call fipe_fini without stopping PM.");
return (EBUSY);
}
ASSERT(!fipe_gbl_ctrl.pm_active);
fipe_ioat_fini();
fipe_mc_fini();
#ifdef FIPE_KSTAT_SUPPORT
if (fipe_gbl_ctrl.fipe_kstat != NULL) {
kstat_delete(fipe_gbl_ctrl.fipe_kstat);
fipe_gbl_ctrl.fipe_kstat = NULL;
}
#endif /* FIPE_KSTAT_SUPPORT */
if (fipe_gbl_ctrl.state_buf != NULL) {
ASSERT(fipe_gbl_ctrl.state_size != 0);
kmem_free(fipe_gbl_ctrl.state_buf, fipe_gbl_ctrl.state_size);
fipe_cpu_states = NULL;
}
fipe_profile_curr = NULL;
mutex_destroy(&fipe_gbl_ctrl.lock);
bzero(&fipe_gbl_ctrl, sizeof (fipe_gbl_ctrl));
return (0);
}
/*
* Start memory power management subsystem.
* Note: caller must ensure exclusive access to all fipe_xxx interfaces.
*/
int
fipe_start(void)
{
if (fipe_gbl_ctrl.pm_enabled == B_TRUE) {
return (0);
}
bzero(fipe_cpu_states, max_ncpus * sizeof (fipe_cpu_states[0]));
fipe_ioat_alloc(NULL);
if (fipe_idle_start() != 0) {
cmn_err(CE_NOTE, "!fipe: failed to start PM subsystem.");
fipe_ioat_free();
return (-1);
}
fipe_gbl_ctrl.pm_enabled = B_TRUE;
return (0);
}
/*
* Stop memory power management subsystem.
* Note: caller must ensure exclusive access to all fipe_xxx interfaces.
*/
int
fipe_stop(void)
{
if (fipe_gbl_ctrl.pm_enabled) {
if (fipe_idle_stop() != 0) {
cmn_err(CE_NOTE,
"!fipe: failed to stop PM subsystem.");
return (-1);
}
fipe_ioat_free();
fipe_gbl_ctrl.pm_enabled = B_FALSE;
}
ASSERT(!fipe_gbl_ctrl.pm_active);
return (0);
}
int
fipe_suspend(void)
{
/* Save current power management policy. */
fipe_pm_policy_saved = fipe_pm_policy;
/* Disable PM by setting profile to FIPE_PM_POLICY_DISABLE. */
fipe_pm_policy = FIPE_PM_POLICY_DISABLE;
fipe_profile_curr = &fipe_profiles[fipe_pm_policy];
return (0);
}
int
fipe_resume(void)
{
/* Restore saved power management policy. */
fipe_pm_policy = fipe_pm_policy_saved;
fipe_profile_curr = &fipe_profiles[fipe_pm_policy];
return (0);
}
fipe_pm_policy_t
fipe_get_pmpolicy(void)
{
return (fipe_pm_policy);
}
int
fipe_set_pmpolicy(fipe_pm_policy_t policy)
{
if (policy < 0 || policy >= FIPE_PM_POLICY_MAX) {
return (EINVAL);
}
fipe_pm_policy = policy;
fipe_profile_curr = &fipe_profiles[fipe_pm_policy];
return (0);
}
/*
* Check condition (fipe_gbl_ctrl.cpu_cnt == ncpus) to make sure that
* there is other CPU trying to wake up system from memory power saving state.
* If a CPU is waking up system, fipe_disable() will set
* fipe_gbl_ctrl.pm_active to false as soon as possible and allow other CPU's
* to continue, and it will take the responsibility to recover system from
* memory power saving state.
*/
static void
fipe_enable(int throttle, cpu_idle_check_wakeup_t check_func, void* check_arg)
{
extern void membar_sync(void);
FIPE_KSTAT_DETAIL_INC(pm_tryenter_cnt);
/*
* Check CPU wakeup events.
*/
if (check_func != NULL) {
(*check_func)(check_arg);
}
/*
* Try to acquire mutex, which also implicitly has the same effect
* of calling membar_sync().
* If mutex_tryenter fails, that means other CPU is waking up.
*/
if (mutex_tryenter(&fipe_gbl_ctrl.lock) == 0) {
FIPE_KSTAT_DETAIL_INC(pm_race_cnt);
/*
* Handle a special race condition for the case that a CPU wakes
* and then enters into idle state within a short period.
* This case can't be reliably detected by cpu_count mechanism.
*/
} else if (fipe_gbl_ctrl.pm_active) {
FIPE_KSTAT_DETAIL_INC(pm_race_cnt);
mutex_exit(&fipe_gbl_ctrl.lock);
} else {
fipe_gbl_ctrl.pm_active = B_TRUE;
membar_sync();
if (fipe_gbl_ctrl.cpu_count != ncpus) {
FIPE_KSTAT_DETAIL_INC(pm_race_cnt);
fipe_gbl_ctrl.pm_active = B_FALSE;
} else if (fipe_ioat_trigger() != 0) {
fipe_gbl_ctrl.pm_active = B_FALSE;
} else if (fipe_gbl_ctrl.cpu_count != ncpus ||
fipe_mc_change(throttle) != 0) {
fipe_gbl_ctrl.pm_active = B_FALSE;
fipe_ioat_cancel();
if (fipe_gbl_ctrl.cpu_count != ncpus) {
FIPE_KSTAT_DETAIL_INC(pm_race_cnt);
}
} else if (fipe_gbl_ctrl.cpu_count != ncpus) {
fipe_gbl_ctrl.pm_active = B_FALSE;
fipe_mc_restore();
fipe_ioat_cancel();
FIPE_KSTAT_DETAIL_INC(pm_race_cnt);
} else {
FIPE_KSTAT_DETAIL_INC(pm_success_cnt);
}
mutex_exit(&fipe_gbl_ctrl.lock);
}
}
static void
fipe_disable(void)
{
/*
* Try to acquire lock, which also implicitly has the same effect
* of calling membar_sync().
*/
while (mutex_tryenter(&fipe_gbl_ctrl.lock) == 0) {
/*
* If power saving is inactive, just return and all dirty
* house-keeping work will be handled in fipe_enable().
*/
if (fipe_gbl_ctrl.pm_active == B_FALSE) {
return;
} else {
(void) SMT_PAUSE();
}
}
/* Disable power saving if it's active. */
if (fipe_gbl_ctrl.pm_active) {
/*
* Set pm_active to FALSE as soon as possible to prevent
* other CPUs from waiting on pm_active flag.
*/
fipe_gbl_ctrl.pm_active = B_FALSE;
membar_producer();
fipe_mc_restore();
fipe_ioat_cancel();
}
mutex_exit(&fipe_gbl_ctrl.lock);
}
/*ARGSUSED*/
static boolean_t
fipe_check_cpu(struct fipe_cpu_state *sp, cpu_idle_callback_context_t ctx,
hrtime_t ts)
{
if (cpu_flagged_offline(CPU->cpu_flags)) {
/* Treat CPU in offline state as ready. */
sp->cond_ready = B_TRUE;
return (B_TRUE);
} else if (sp->next_ts <= ts) {
uint64_t intr;
hrtime_t idle, busy, diff;
cpu_idle_prop_value_t val;
/* Set default value. */
sp->cond_ready = B_TRUE;
sp->idle_count = 0;
/* Calculate idle percent. */
idle = sp->last_idle;
sp->last_idle = cpu_idle_prop_get_hrtime(
fipe_idle_ctrl.prop_idle, ctx);
idle = sp->last_idle - idle;
busy = sp->last_busy;
sp->last_busy = cpu_idle_prop_get_hrtime(
fipe_idle_ctrl.prop_busy, ctx);
busy = sp->last_busy - busy;
/* Check idle condition. */
if (idle > 0 && busy > 0) {
if (busy * (100 - FIPE_PROF_BUSY_THRESHOLD) >
idle * FIPE_PROF_BUSY_THRESHOLD) {
FIPE_KSTAT_DETAIL_INC(cpu_busy_cnt);
sp->cond_ready = B_FALSE;
} else {
FIPE_KSTAT_DETAIL_INC(cpu_idle_cnt);
}
} else {
FIPE_KSTAT_DETAIL_INC(cpu_busy_cnt);
sp->cond_ready = B_FALSE;
}
/* Calculate interrupt count. */
diff = sp->next_ts;
sp->next_ts = ts + fipe_idle_ctrl.tick_interval;
diff = sp->next_ts - diff;
intr = sp->last_intr;
if (cpu_idle_prop_get_value(fipe_idle_ctrl.prop_intr, ctx,
&val) == 0) {
sp->last_intr = val.cipv_uint64;
intr = sp->last_intr - intr;
if (diff != 0) {
intr = intr * fipe_idle_ctrl.tick_interval;
intr /= diff;
} else {
intr = FIPE_PROF_INTR_THRESHOLD;
}
} else {
intr = FIPE_PROF_INTR_THRESHOLD;
}
/*
* System is busy with interrupts, so disable all PM
* status checks for INTR_BUSY_THROTTLE ticks.
* Interrupts are disabled when FIPE callbacks are called,
* so this optimization will help to reduce interrupt
* latency.
*/
if (intr >= FIPE_PROF_INTR_BUSY_THRESHOLD) {
FIPE_KSTAT_DETAIL_INC(cpu_intr_busy_cnt);
sp->throttle_ts = ts + FIPE_PROF_INTR_BUSY_THROTTLE *
fipe_idle_ctrl.tick_interval;
sp->cond_ready = B_FALSE;
} else if (intr >= FIPE_PROF_INTR_THRESHOLD) {
FIPE_KSTAT_DETAIL_INC(cpu_intr_throttle_cnt);
sp->cond_ready = B_FALSE;
}
} else if (++sp->idle_count >= FIPE_PROF_IDLE_COUNT) {
/* Too many idle enter/exit in this tick. */
FIPE_KSTAT_DETAIL_INC(cpu_loop_cnt);
sp->throttle_ts = sp->next_ts + fipe_idle_ctrl.tick_interval;
sp->idle_count = 0;
sp->cond_ready = B_FALSE;
return (B_FALSE);
}
return (sp->cond_ready);
}
/*ARGSUSED*/
static void
fipe_idle_enter(void *arg, cpu_idle_callback_context_t ctx,
cpu_idle_check_wakeup_t check_func, void* check_arg)
{
hrtime_t ts;
uint32_t cnt;
uint64_t iowait;
cpu_t *cp = CPU;
struct fipe_cpu_state *sp;
sp = &fipe_cpu_states[cp->cpu_id];
ts = cpu_idle_prop_get_hrtime(fipe_idle_ctrl.prop_enter, ctx);
if (fipe_pm_policy != FIPE_PM_POLICY_DISABLE &&
fipe_ioat_ctrl.ioat_ready &&
sp->state_ready && sp->throttle_ts <= ts) {
/* Adjust iowait count for local CPU. */
iowait = CPU_STATS(cp, sys.iowait);
if (iowait != sp->last_iowait) {
atomic_add_64(&fipe_gbl_ctrl.io_waiters,
iowait - sp->last_iowait);
sp->last_iowait = iowait;
}
/* Check current CPU status. */
if (fipe_check_cpu(sp, ctx, ts)) {
/* Increase count of CPU ready for power saving. */
do {
cnt = fipe_gbl_ctrl.cpu_count;
ASSERT(cnt < ncpus);
} while (atomic_cas_32(&fipe_gbl_ctrl.cpu_count,
cnt, cnt + 1) != cnt);
/*
* Enable power saving if all CPUs are idle.
*/
if (cnt + 1 == ncpus) {
if (fipe_gbl_ctrl.io_waiters == 0) {
fipe_gbl_ctrl.enter_ts = ts;
fipe_enable(fipe_pm_throttle_level,
check_func, check_arg);
/* There are ongoing block io operations. */
} else {
FIPE_KSTAT_DETAIL_INC(bio_busy_cnt);
}
}
}
} else if (fipe_pm_policy == FIPE_PM_POLICY_DISABLE ||
fipe_ioat_ctrl.ioat_ready == B_FALSE) {
if (sp->cond_ready == B_TRUE) {
sp->cond_ready = B_FALSE;
}
} else if (sp->state_ready == B_FALSE) {
sp->cond_ready = B_FALSE;
sp->state_ready = B_TRUE;
sp->throttle_ts = 0;
sp->next_ts = ts + fipe_idle_ctrl.tick_interval;
sp->last_busy = cpu_idle_prop_get_hrtime(
fipe_idle_ctrl.prop_busy, ctx);
sp->last_idle = cpu_idle_prop_get_hrtime(
fipe_idle_ctrl.prop_idle, ctx);
sp->last_intr = cpu_idle_prop_get_hrtime(
fipe_idle_ctrl.prop_intr, ctx);
sp->idle_count = 0;
}
}
/*ARGSUSED*/
static void
fipe_idle_exit(void* arg, cpu_idle_callback_context_t ctx, int flags)
{
uint32_t cnt;
hrtime_t ts;
struct fipe_cpu_state *sp;
sp = &fipe_cpu_states[CPU->cpu_id];
if (sp->cond_ready) {
do {
cnt = fipe_gbl_ctrl.cpu_count;
ASSERT(cnt > 0);
} while (atomic_cas_32(&fipe_gbl_ctrl.cpu_count,
cnt, cnt - 1) != cnt);
/*
* Try to disable power saving state.
* Only the first CPU waking from idle state will try to
* disable power saving state, all other CPUs will just go
* on and not try to wait for memory to recover from power
* saving state.
* So there are possible periods during which some CPUs are in
* active state but memory is in power saving state.
* This is OK, since it is an uncommon case, and it is
* better for performance to let them continue as their
* blocking latency is smaller than a mutex, and is only
* hit in the uncommon condition.
*/
if (cnt == ncpus) {
fipe_disable();
ts = cpu_idle_prop_get_hrtime(fipe_idle_ctrl.prop_exit,
ctx);
fipe_gbl_ctrl.time_in_pm += ts - fipe_gbl_ctrl.enter_ts;
}
}
}
|