pcache.c
84.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/*****************************************************************
* pcache.c
*
* Primary cache implemented as up to 4-way associative
* caches, compile time choice of associativity, size, & line size.
*
* $Author: blythe $
* $Date: 2002/06/26 20:46:25 $
*******************************************************************/
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include "syslimits.h"
#include "sim_error.h"
#include "../memref.h"
#include "pcache.h"
#include "scache.h"
#include "simrecord.h"
#include "memsys.h"
#include "simutil.h"
#include "false_sharing.h"
#include "hw_events.h"
#include "registry.h"
#include "tcl_init.h"
#include "limits.h"
#include "arch_specifics.h"
#include "cpu_stats.h"
#if (defined(SIM_MIPS64) || defined(SIM_MIPS32))
#include "trace.h"
#else
#define TraceDataRef(a,b,c)
#endif
#ifdef MIPSY_MXS
# include "ms.h"
#endif
/* We haven't been using these, so I'll make them compile time
parameters for now. It will also speed up the caches. */
#define DCACHE_HIT_ALWAYS 0
#define DCACHE_MISS_ALWAYS 0
#define ICACHE_HIT_ALWAYS 0
#define ICACHE_MISS_ALWAYS 0
#define CURRENT_PC(_cpu) (CPUVec.CurrentPC(_cpu))
/*
* Possible return status from the miss handling table.
* MHTSUCCESS - Entry allocated everything looks good.
* MHTMERGE - This request merged with another. Returned other.
* MHTFULL - Entry not allocated because table was full.
* MHTCONFLICT - Entry not allocated because it conflicted with one already
* allocated.
*/
typedef enum { MHTSUCCESS = 0, MHTFULL, MHTMERGE, MHTCONFLICT} MHTStatus;
Cache *CACHE;
bool skipCaches;
/* Variables needed for the lookup macros */
static int iTagShift;
static int iIndexMask;
static int dTagShift;
static int dIndexMask;
#ifndef SIM_ALPHA
static int iAddrDivisor;
static int dAddrDivisor;
#else
int iAddrDivisor;
int dAddrDivisor;
#endif
static bool useWriteBuffer;
static bool noUpgrades = FALSE; /* Send no upgrades to mem system */
/* Local Cache Functions */
static void InitPCaches(void);
static void ICachePUT(int, MHT *, int );
static void DCacheFlush(int, int writeback, int retain, PA, int);
static void DCachePUT(int, MHT *, int);
static Result ICacheMiss(int cpuNum, VA, PA, int *);
static Result DCacheReadMiss(int cpuNum, VA, PA, void *data, RefSize size,
RefFlavor flavor, int *indPtr);
static Result DCacheWriteMiss(int cpuNum, VA, PA, RefSize size, RefFlavor flavor,
struct DCacheSet* set, int way, bool upgrade,
int* mhtind);
static void FreeMHT(int cpuNum, int entryNum);
static MHTStatus AllocMHT(int cpuNum, SCacheCmd cmd, VA vAddr, PA pAddr,
int size, int lru, int *mhtind);
static void RetryNAKedMHTEntry(int cpuNum,EventCallbackHdr *event, void *arg);
static bool AddToWriteBuffer(int cpuNum,PA pAddr, uint64 data, RefSize size,
int mhtind);
static bool AddrIsInWriteBuffer(int cpuNum, PA pAddr, RefSize size);
static void RetireWriteBuffer(int cpuNum, MHT *mht, byte *data);
/****************************************************************
*
* Cope with different associativities.
*
* The following code handles up to 4-way for now, but in the future
* the interface is good enough to handle higher associativities.
* Just extend the if tree and pass a pointer to the first word
* of the set
*/
#define UPDATE_BIG_LRU(lruword,set) \
{ \
unsigned char* _lru = (unsigned char*) &(lruword); \
unsigned char _t1, _t2; \
\
if (_lru[0] != (set)) { \
_t1 = _lru[1]; \
_lru[1] = _lru[0]; \
_lru[0] = (set); \
if (_t1 != (set)) { \
_t2 = _lru[2]; \
_lru[2] = _t1; \
if (_t2 != (set)) _lru[3] = _t2; \
} \
} \
}
#define INIT_BIG_LRU(lruword) \
{ \
char* _lru = (char*) &(lruword); \
_lru[0] = 0; _lru[1] = 1; _lru[2] = 2; _lru[3] = 3; \
}
#define GET_BIG_LRU(lruword, assoc) (((char*) &(lruword))[(assoc)-1])
#define SET_BIG_LRU4(lruword, set) \
{ \
unsigned char* _lru = (unsigned char*) &(lruword); \
unsigned char _t1,_t2; \
\
if (_lru[3] != (set)) { \
_t1 = _lru[2]; \
_lru[2] = _lru[3]; \
_lru[3] = (set); \
if (_t1 != (set)) { \
_t2 = _lru[1]; \
_lru[1] = _t1; \
if (_t2 != (set)) _lru[0] = _t2; \
} \
} \
}
#if ICACHE_ASSOC == 1
#define ICACHE_TOUCH(lruword, set) /* nop */
#define ICACHE_INIT_LRU(lruword) lruword = 0;
#define ICACHE_LRU(lruword) 0
#define ICACHE_MAKE_LRU(lruword, set) /* nop */
#endif
#if ICACHE_ASSOC == 2
#define ICACHE_TOUCH(lruword, set) lruword = set;
#define ICACHE_INIT_LRU(lruword) lruword = 0;
#define ICACHE_LRU(lruword) (!lruword)
#define ICACHE_MAKE_LRU(lruword, set) lruword = !(set);
#endif
#if ICACHE_ASSOC == 4
#define ICACHE_TOUCH(lruword, set) UPDATE_BIG_LRU(lruword, set)
#define ICACHE_INIT_LRU(lruword) INIT_BIG_LRU(lruword)
#define ICACHE_LRU(lruword) GET_BIG_LRU(lruword, ICACHE_ASSOC)
#define ICACHE_MAKE_LRU(lruword,set) SET_BIG_LRU4(lruword, set)
#endif
#if ICACHE_ASSOC == 8
#define ICACHE_TOUCH(lruword, set) UPDATE_BIG_LRU(lruword, set)
#define ICACHE_INIT_LRU(lruword) INIT_BIG_LRU(lruword)
#define ICACHE_LRU(lruword) GET_BIG_LRU(lruword, ICACHE_ASSOC)
#define ICACHE_MAKE_LRU(lruword,set) SET_BIG_LRU8(lruword, set)
#endif
#if DCACHE_ASSOC == 1
#define DCACHE_TOUCH(lruword, set) /* nop */
#define DCACHE_INIT_LRU(lruword) lruword = 0;
#define DCACHE_LRU(lruword) 0
#define DCACHE_MAKE_LRU(lruword, set) /* nop */
#endif
#if DCACHE_ASSOC == 2
#define DCACHE_TOUCH(lruword, set) lruword = set;
#define DCACHE_INIT_LRU(lruword) lruword = 0;
#define DCACHE_LRU(lruword) !lruword
#define DCACHE_MAKE_LRU(lruword, set) lruword = !(set);
#endif
#if DCACHE_ASSOC == 4
#define DCACHE_TOUCH(lruword, set) UPDATE_BIG_LRU(lruword, set)
#define DCACHE_INIT_LRU(lruword) INIT_BIG_LRU(lruword)
#define DCACHE_LRU(lruword) GET_BIG_LRU(lruword, DCACHE_ASSOC)
#define DCACHE_MAKE_LRU(lruword, set) SET_BIG_LRU4(lruword, set)
#endif
#if DCACHE_ASSOC == 8
#define DCACHE_TOUCH(lruword, set) UPDATE_BIG_LRU(lruword, set)
#define DCACHE_INIT_LRU(lruword) INIT_BIG_LRU(lruword)
#define DCACHE_LRU(lruword) GET_BIG_LRU(lruword, DCACHE_ASSOC)
#define DCACHE_MAKE_LRU(lruword, set) SET_BIG_LRU8(lruword, set)
#endif
/**** just for laughs, here's another UPDATE_BIG_LRU routine, with a few
* more instructions but no branches or if tests after the initial
* check. Probably runs faster on a dynamically-scheduled processor like T5.
* Keep x axis of array as set number instead of lru position.
*
* {
* unsigned char* lru = (unsigned char*) lruword;
* register unsigned int l = lru[h];
* if (l != 0) {
* lru[0] += (lru[0] < l);
* lru[1] += (lru[1] < l);
* lru[2] += (lru[2] < l);
* lru[3] += (lru[3] < l);
* lru[h] = 0;
* }
* }
*/
/*****************************************************************
* EVENT SUPPORT
*****************************************************************/
#define DATA_READ_EVENT() \
TraceDataRef(&PE[cpuNum], vAddr, pAddr); \
STATS_INC(cpuNum, dReads, 1); \
if (++(dcache->memSample) >= MS_SAMPLE_MEMOP_INTERVAL) { \
dcache->memSample = 0; \
MEM_SAMPLE_EVENT(CPUVec.CycleCount(cpuNum), cpuNum, CURRENT_PC(cpuNum), \
vAddr, pAddr, 1); \
}
#define DATA_WRITE_EVENT() \
TraceDataRef(&PE[cpuNum], vAddr, pAddr); \
STATS_INC(cpuNum, dWrites, 1); \
if (++CACHE[cacheNum].DCache.memSample >= MS_SAMPLE_MEMOP_INTERVAL) { \
CACHE[cacheNum].DCache.memSample = 0; \
MEM_SAMPLE_EVENT(CPUVec.CycleCount(cpuNum), cpuNum, CURRENT_PC(cpuNum), \
vAddr, pAddr, 0); \
}
/****************************************************************
* MemRefInit
*
*****************************************************************/
void
MemRefInit(void)
{
if (!strcmp(CACHE_MODEL, "None")) {
CACHE = (Cache *)calloc(TOTAL_CPUS, sizeof(Cache));
skipCaches = TRUE;
} else {
ASSERT(!strcmp(CACHE_MODEL, "2Level"));
InitPCaches();
InitSCaches();
skipCaches = FALSE;
}
MemsysInit();
}
/*****************************************************************
* MemRefResetStats
*
*****************************************************************/
void
MemRefResetStats(int cpuNum)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
int scacheNum = GET_SCACHE_NUM(cpuNum);
bzero((char*)&(CACHE[cacheNum].stats), sizeof(CACHE[cacheNum].stats));
bzero((char*)&(SCACHE[scacheNum].stats), sizeof(SCACHE[scacheNum].stats));
}
/*****************************************************************
* MemRefPrintPeriodicStats
*
* This is called every STAT_INTERVAL.
*****************************************************************/
static struct CacheStats {
unsigned int ICacheCnt, ICacheMisses;
unsigned int DCacheCnt, DCacheMisses;
unsigned int SCacheCnt, SCacheMisses;
} oldstats[SIM_MAXCPUS];
static int printcnt;
void
MemRefPeriodicStats(int cpuNum)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
int scacheNum = GET_SCACHE_NUM(cpuNum);
uint newICacheCnt, newICacheMisses;
uint newDCacheCnt, newDCacheMisses;
uint newSCacheCnt, newSCacheMisses;
uint totalICacheCnt, totalICacheMisses;
uint totalDCacheCnt, totalDCacheMisses;
uint totalSCacheCnt, totalSCacheMisses;
struct CacheStats *s;
Cache *C = &CACHE[cacheNum];
SCache *SC = &SCACHE[scacheNum];
totalICacheCnt = totalICacheMisses = 0;
totalDCacheCnt = totalDCacheMisses = 0;
totalSCacheCnt = totalSCacheMisses = 0;
s = &(oldstats[cacheNum]);
newICacheCnt = STATS_VALUE(cpuNum, iReads);
totalICacheCnt += newICacheCnt;
totalICacheMisses += newICacheMisses =
(uint)C->stats.ICache.ReadMisses;
totalDCacheCnt += newDCacheCnt = (uint)
(STATS_VALUE(cpuNum, dReads) + STATS_VALUE(cpuNum, dWrites));
totalDCacheMisses += newDCacheMisses = (uint)
(C->stats.DCache.ReadMisses +
C->stats.DCache.WriteMisses +
C->stats.DCache.UpgradeMisses);
totalSCacheCnt += newSCacheCnt = (uint)
(SC->stats.Igets +
SC->stats.Dgets +
SC->stats.DgetXs +
SC->stats.Dupgrades);
totalSCacheMisses += newSCacheMisses = (uint)
(SC->stats.IgetMisses +
SC->stats.DgetMisses +
SC->stats.DgetXMisses +
SC->stats.DupgradeMisses);
#ifdef DELETE
CPUPrint("C%d I %5.4f%% D %5.4f%% S %3.2f%% %3.2f%%\n",
cacheNum,
((newICacheMisses - s->ICacheMisses == 0) ? 0 :
((100.0 * (newICacheMisses - (uint)s->ICacheMisses)) /
(newICacheCnt - (uint)s->ICacheCnt))),
((newDCacheMisses - (uint)s->DCacheMisses == 0) ? 0 :
((100.0 * (newDCacheMisses - (uint)s->DCacheMisses)) /
(newDCacheCnt - (uint)s->DCacheCnt))),
((newSCacheMisses - (uint)s->SCacheMisses == 0) ? 0 :
((100.0 * (newSCacheMisses - (uint)s->SCacheMisses)) /
(newSCacheCnt - (uint)s->SCacheCnt))));
#endif
s->ICacheMisses = newICacheMisses;
s->DCacheCnt = newDCacheCnt;
s->DCacheMisses = newDCacheMisses;
s->SCacheMisses = newSCacheMisses;
s->SCacheCnt = newSCacheCnt;
printcnt++;
CPUPrint("C%d Total I Misses: %lld Total I Refs: %lld Rate: %5.3f%%\n",
cacheNum,
(uint64)totalICacheMisses,
(uint64)totalICacheCnt,
totalICacheCnt ? (100.0 * totalICacheMisses)/totalICacheCnt : 0.0);
CPUPrint("C%d Total D Misses: %lld Total D Refs: %lld Rate: %5.3f%%\n",
cacheNum,
(uint64)totalDCacheMisses,
(uint64)totalDCacheCnt,
totalDCacheCnt ? (100.0 * totalDCacheMisses)/totalDCacheCnt : 0.0);
CPUPrint("C%d Total S Misses: %lld Total S Refs: %lld Rate: %5.3f%%\n",
cacheNum,
(uint64)totalSCacheMisses,
(uint64)totalSCacheCnt,
totalSCacheCnt ? (100.0 * totalSCacheMisses)/totalSCacheCnt : 0.0);
#if (MHT_SIZE >= 4) && (SMHT_SIZE >= 4)
{
/*
* These stats are currently MHT occupancy histograms over all
* cpu's. They can be changed to per cpu if desireable.
*/
int i, c;
SimCounter occ[MHT_SIZE+1], socc[SMHT_SIZE+1];
for (i = 0; i < MHT_SIZE+1; i++) {
occ[i] = 0;
for (c = 0; c < TOTAL_CPUS; c++) {
occ[i] += CACHE[c].stats.mhtOccupancyHist[i];
}
}
for (i = 0; i < SMHT_SIZE+1; i++) {
socc[i] = 0;
for (c = 0; c < TOTAL_CPUS; c++) {
socc[i] += SCACHE[c].stats.smhtOccupancyHist[i];
}
}
CPUPrint("C%d MHT Occupancy: %lld %lld %lld %lld %lld\n",
cacheNum,
(uint64) occ[0], (uint64) occ[1],
(uint64) occ[2], (uint64) occ[3], (uint64) occ[4]);
CPUPrint("C%d SMHT Occupancy: %lld %lld %lld %lld %lld\n",
cacheNum,
(uint64) socc[0], (uint64) socc[1],
(uint64) socc[2], (uint64) socc[3], (uint64) socc[4]);
}
#endif
/* You could print periodic memsys stats here if they existed */
}
/*****************************************************************
* MemRefDumpStats
*****************************************************************/
void
MemRefDumpStats(int cpuNum)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
int scacheNum = GET_SCACHE_NUM(cpuNum);
int j;
Cache *C = &(CACHE[cacheNum]);
SCache *SC = &(SCACHE[scacheNum]);
#ifdef SOLO
CPUPrint("C%d libc Wait Time: %lld\n", cpuNum, STATS_VALUE(cpuNum, libcWaitTime));
#endif
CPUPrint("C%d I Misses: %lld I Refs: %lld\n",
cacheNum,
(uint64)C->stats.ICache.ReadMisses,
(uint64)STATS_VALUE(cpuNum, iReads));
CPUPrint("C%d I Inval %lld LinesInval %lld\n",
cacheNum,
(uint64)C->stats.ICache.Inval,
(uint64)C->stats.ICache.LinesInval);
CPUPrint("C%d D Read Misses: %lld Reads: %lld\n",
cacheNum,
(uint64)C->stats.DCache.ReadMisses,
(uint64)STATS_VALUE(cpuNum, dReads));
CPUPrint("C%d D Write Misses: %lld Writes: %lld\n",
cacheNum,
(uint64)C->stats.DCache.WriteMisses,
(uint64)STATS_VALUE(cpuNum, dWrites));
CPUPrint("C%d D Upgrades %lld Writebacks %lld \n",
cacheNum,
(uint64)C->stats.DCache.UpgradeMisses,
(uint64)C->stats.DCache.Writebacks);
CPUPrint("C%d D Inval %lld LinesInval %lld Dwngrd %lld LinesWback %lld\n",
cacheNum,
(uint64)C->stats.DCache.Inval,
(uint64)C->stats.DCache.LinesInval,
(uint64)C->stats.DCache.Downgrade,
(uint64)C->stats.DCache.LinesWriteback);
CPUPrint("C%d IGet Retries %lld\n",
cacheNum,
(uint64)C->stats.ICache.RetriedIGets);
CPUPrint("C%d DGet Retries %lld\n",
cacheNum,
(uint64)C->stats.DCache.RetriedDGets);
CPUPrint("C%d DGetX Retries %lld\n",
cacheNum,
(uint64)C->stats.DCache.RetriedDGetXs);
for (j = 0; j < MHT_SIZE+1; j++) {
CPUPrint("C%d MHT%d %lld\n", cacheNum, j,
(uint64)C->stats.mhtOccupancyHist[j]);
}
CPUPrint("C%d S IMisses %lld IRefs: %lld\n",
cacheNum,
(uint64)SC->stats.IgetMisses,
(uint64)SC->stats.Igets);
CPUPrint("C%d S DMisses: GetMisses %lld GetRefs %lld\n",
cacheNum,
(uint64)SC->stats.DgetMisses,
(uint64)SC->stats.Dgets);
CPUPrint("C%d S DMisses: GetXMisses %lld GetXRefs %lld\n",
cacheNum,
(uint64)SC->stats.DgetXMisses,
(uint64)SC->stats.DgetXs);
CPUPrint("C%d S NAKs: %lld Merges: %lld\n",
cacheNum,
(uint64)SC->stats.NAKs,
(uint64)SC->stats.MergeMisses);
CPUPrint("C%d S UpgMisses: %lld UpgRefs: %lld (%.2f%%) Wbacks %lld Replace %lld\n",
cacheNum,
(uint64)SC->stats.DupgradeMisses,
(uint64)SC->stats.Dupgrades,
(SC->stats.Dupgrades>0 ?
(100.0 * ((double)SC->stats.DupgradeMisses /
(double)SC->stats.Dupgrades)): (double)-1.0),
(uint64)SC->stats.Writebacks,
(uint64)SC->stats.Replacements);
CPUPrint("C%d S Inval %lld LinesInval %lld Dwngrd %lld LinesWback %lld\n",
cacheNum,
(uint64)SC->stats.Inval,
(uint64)SC->stats.LinesInval,
(uint64)SC->stats.Downgrade,
(uint64)SC->stats.LinesWriteback);
for (j = 0; j < SMHT_SIZE+1; j++) {
CPUPrint("C%d SMHT%d %lld\n", cacheNum, j,
(uint64)SC->stats.smhtOccupancyHist[j]);
}
CPUPrint("C%d S IGetMHTRetries %lld DGetMHTRetries %lld DGetXMHTRetries %lld DUGetXMHTRetries %lld\n",
cacheNum,
(uint64)SC->stats.IGetMHTRetries,
(uint64)SC->stats.DGetMHTRetries,
(uint64)SC->stats.DGetXMHTRetries,
(uint64)SC->stats.DUGetXMHTRetries);
CPUPrint("C%d S Prefetches %lld\n",
cacheNum,
(uint64)SC->stats.Prefetches);
#ifdef HWBCOPY
if (SimConfigGetBool("Hwbcopy.Streaming")) {
for (j = 1; j <= SimConfigGetInt("Hwbcopy.StreamDepth"); j++) {
CPUPrint("C%d S StreamGets[%d] %lld\n",
cacheNum, j,
(uint64)SC->stats.StreamGets[j]);
CPUPrint("C%d S StreamGetXs[%d] %lld\n",
cacheNum, j,
(uint64)SC->stats.StreamGetXs[j]);
}
}
#endif
if (cpuNum == (TOTAL_CPUS-1)) {
memsysVec.MemsysDumpStats();
}
}
/*****************************************************************
* InitPCaches
* Initialize the primary caches.
*****************************************************************/
void
InitPCaches(void)
{
int i,j,k;
int log2ICACHE_ASSOC = GetLog2(ICACHE_ASSOC);
int log2DCACHE_ASSOC = GetLog2(DCACHE_ASSOC);
if ( ICACHE_LINE_SIZE > SCACHE_LINE_SIZE ) {
CPUError("ICache.LineSize must be smaller than SCacheLineSize\n");
}
if ( DCACHE_LINE_SIZE > SCACHE_LINE_SIZE ) {
CPUError("DCache.LineSize must be smaller than SCacheLineSize\n");
}
if ( machines.DCacheAssoc != DCACHE_ASSOC ) {
CPUError("DCache.Assoc !=%i (not implemented) \n",DCACHE_ASSOC);
}
if ( machines.ICacheAssoc != ICACHE_ASSOC ) {
CPUError("ICache.Assoc !=%i (not implemented) \n",ICACHE_ASSOC);
}
#ifdef DATA_HANDLING
if ( DCACHE_HIT_ALWAYS || ICACHE_HIT_ALWAYS ) {
CPUError("ICache.AHit and DCache.AMiss are not supported with data handling!\n");
}
#endif
if ( DCACHE_MISS_ALWAYS || ICACHE_MISS_ALWAYS ) {
if ( SCACHE_HIT_TIME != 0 ) {
CPUError("ICacheAMiss || DCacheAMiss set. SCacheHitTime must be set to 0!\n");
}
}
if ( ICACHE_HIT_ALWAYS && ICACHE_MISS_ALWAYS) {
CPUError("Make up your mind: ICache.HitAlways OR ICache.MissAlways\n");
}
if ( DCACHE_HIT_ALWAYS && DCACHE_MISS_ALWAYS) {
CPUError("Make up your mind: DCache.HitAlways OR DCache.MissAlways\n");
}
/* Set up variables for the cache macros... it's better to compute
these once here than every time we do a lookup. */
iTagShift = log2ICACHE_SIZE - log2ICACHE_ASSOC;
iAddrDivisor = ICACHE_SIZE/ICACHE_ASSOC;
iIndexMask = ICACHE_INDEX - 1;
dTagShift = log2DCACHE_SIZE - log2DCACHE_ASSOC;
dAddrDivisor = DCACHE_SIZE/DCACHE_ASSOC;
dIndexMask = DCACHE_INDEX - 1;
CACHE = (Cache *)calloc(TOTAL_CPUS, sizeof(Cache));
CPUPrint("ICACHE: Assoc = %d Size = %#x Line = %d\n",
ICACHE_ASSOC, ICACHE_SIZE, ICACHE_LINE_SIZE);
CPUPrint("DCACHE: Assoc = %d Size = %#x Line = %d\n",
DCACHE_ASSOC, DCACHE_SIZE, DCACHE_LINE_SIZE);
CPUPrint("SIMOS running with a write buffer size %d!\n",
WRITE_BUFFER_SIZE);
for (j=0; j<TOTAL_CPUS; j++) {
/* Allocate and initialize the ICaches */
CACHE[j].ICache.set =
(struct ICacheSet *)calloc(ICACHE_INDEX,sizeof(struct ICacheSet));
CACHE[j].DCache.set =
(struct DCacheSet *)calloc(DCACHE_INDEX,sizeof(struct DCacheSet));
for (i=0; i<ICACHE_INDEX; i++) {
for (k=0; k<ICACHE_ASSOC; k++) {
CACHE[j].ICache.set[i].tags[k] = INVALID_TAG;
}
ICACHE_INIT_LRU(CACHE[j].ICache.set[i].LRU);
}
/* Initialize the DCaches */
for (i=0; i<DCACHE_INDEX; i++) {
for (k=0; k<DCACHE_ASSOC; k++) {
CACHE[j].DCache.set[i].tags[k] = INVALID_TAG;
}
DCACHE_INIT_LRU(CACHE[j].DCache.set[i].LRU);
}
/* Initialize the Miss Handling Tables */
for (i=0; i<MHT_SIZE; i++) {
CACHE[j].MHT[i].inuse = FALSE;
CACHE[j].MHT[i].writeBuffer =
MemAlign(sizeof(uint64), sizeof(*CACHE[j].MHT[i].writeBuffer));
for (k = 0; k < 128; k++) {
CACHE[j].MHT[i].writeBuffer->mask[k] = 0;
}
}
CACHE[j].MHTnumInuse = 0;
CACHE[j].activeWriteBuffers = 0;
}
useWriteBuffer = (WRITE_BUFFER_SIZE > 0);
if ((TOTAL_CPUS == 1) && !UPGRADES_ON_UP) {
/* *** Upgrades are not needed on a UP. But some protocols (such as
* flash) require them so they are configurable.
*/
noUpgrades = TRUE;
}
}
/*****************************************************************
* MemRefPrefetch
*
*****************************************************************/
Result
MemRefPrefetch(int cpuNum, VA vAddr, PA pAddr, int hint)
{
PFResult pfRet = PF_SUCCESS;
switch (hint) {
case 0: case 4: case 6:
if (!STATS_VALUE(cpuNum, prefMHTStallStart)) {
STATS_INC(cpuNum, prefStats.prefs, 1);
} else {
STATS_ADD_INTERVAL(cpuNum, prefStats.prefMHTStallTime, prefMHTStallStart);
}
pfRet = SCachePrefetch(cpuNum, vAddr, pAddr, MEMSYS_GET);
{
switch (pfRet) {
case PF_SUCCESS:
/* prefetch was immediately available from the memory system */
STATS_INC(cpuNum, prefStats.prefL1Hits, 1);
break;
case PF_RESIDENT:
STATS_INC(cpuNum, prefStats.prefL2Hits, 1);
break;
case PF_MERGE:
STATS_INC(cpuNum, prefStats.prefMerges, 1);
break;
case PF_STALL:
STATS_INC(cpuNum, prefStats.prefStalls, 1);
break;
case PF_FAILURE:
STATS_INC(cpuNum, prefStats.prefMHTStall, 1);
STATS_SET(cpuNum, prefMHTStallStart, CPUVec.CycleCount(cpuNum));
return FAILURE;
case PF_UPGRADE:
STATS_INC(cpuNum, prefStats.prefUpgrades, 1);
break;
default:
CPUWarning("Bad PREF return value\n");
}
}
break;
case 1: case 5: case 7:
if (!STATS_VALUE(cpuNum, prefMHTStallStart)) {
STATS_INC(cpuNum, prefStats.prefXs, 1);
} else {
STATS_ADD_INTERVAL(cpuNum, prefStats.prefXMHTStallTime, prefMHTStallStart);
}
pfRet = SCachePrefetch(cpuNum, vAddr, pAddr, MEMSYS_GETX);
{
switch (pfRet) {
case PF_SUCCESS:
/* prefetch was immediately available from
the memory system */
STATS_INC(cpuNum, prefStats.prefXL1Hits, 1);
break;
case PF_RESIDENT:
STATS_INC(cpuNum, prefStats.prefXL2Hits, 1);
break;
case PF_MERGE:
STATS_INC(cpuNum, prefStats.prefXMerges, 1);
break;
case PF_STALL:
/* normal case: prefetch out in memsys */
STATS_INC(cpuNum, prefStats.prefXStalls, 1);
break;
case PF_FAILURE:
STATS_INC(cpuNum, prefStats.prefXMHTStall, 1);
STATS_SET(cpuNum, prefMHTStallStart,CPUVec.CycleCount(cpuNum));
return FAILURE;
case PF_UPGRADE:
STATS_INC(cpuNum, prefStats.prefXUpgrades, 1);
break;
default:
CPUWarning("Bad PREF return value\n");
}
}
break;
default:
CPUWarning("Unknown hint in PREF at %#x\n", vAddr);
break;
}
return SUCCESS;
}
/*****************************************************************
* Implementation of supported cache ops
*****************************************************************/
Result
MemRefIndexHitWBInval(int cpuNum, VA vAddr, PA pAddr)
{
int ret;
int wasDirty;
PA realPA;
byte data[128];
Result res = SUCCESS;
ret = CacheExtractIndex(cpuNum, pAddr, SCACHE_LINE_SIZE,
&wasDirty, &realPA, data);
if (ret) {
/* Issue bogus command (get won't occur because of the -1),
but with an associated writeback */
if (wasDirty) {
res = memsysVec.MemsysCmd(cpuNum, MEMSYS_GET, 0LL, -1,
realPA, TRUE, data);
} else {
res = memsysVec.MemsysCmd(cpuNum, MEMSYS_GET, 0LL, -1,
realPA, FALSE, data);
}
}
return res;
}
Result
MemRefHitWBInval(int cpuNum, VA vAddr, PA pAddr)
{
int ret;
int wasDirty;
byte data[128];
Result res = SUCCESS;
ret = CacheExtract(cpuNum, pAddr, SCACHE_LINE_SIZE, &wasDirty, data);
if (ret) { /* If line found */
/* Issue bogus command (get won't occur because of the -1),
but with an associated writeback */
if (wasDirty) {
res = memsysVec.MemsysCmd(cpuNum, MEMSYS_GET, 0LL, -1,
pAddr, TRUE, data);
} else {
res = memsysVec.MemsysCmd(cpuNum, MEMSYS_GET, 0LL, -1,
pAddr, FALSE, data);
}
}
return res;
}
/*****************************************************************
* MemRefReadInst
*
* Main entry into instruction cache from CPU
* Return values:
* SUCCESS - Request hit in the cache, instruction returned.
* STALL - Request missed in the cache, sent to the 2nd-level or memsys.
* FAILURE - Request missed but couldn't be issued to 2nd-level or
* memory system. Caller must retry.
* BUSERROR - Request suffered a bus error.
*
* Simulate a two-way set assoc physically index ICache.
* XXX - Should make this virtually index, physically tagged.
*****************************************************************/
Result
MemRefReadInst(int cpuNum, VA vAddr, PA pAddr, Inst *inst)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
int ind = ICACHE_INDEXOF(pAddr);
PA tag = ICACHE_TAG(pAddr);
struct ICacheSet* set = & CACHE[cacheNum].ICache.set[ind];
Result ret;
int lineindex, setindex;
if ( ICACHE_HIT_ALWAYS ) {
setindex = 0;
set->data[setindex] = (byte*)CPUVec.PAToHostAddr(cpuNum,
pAddr&~(PA)(ICACHE_LINE_SIZE-1),
vAddr);
lineindex = pAddr & (ICACHE_LINE_SIZE-1);
*inst = *(Inst *) (set->data[setindex]+ lineindex);
QUICK_ICACHE_SET(cpuNum, pAddr, set, setindex);
return SUCCESS;
}
if ( !ICACHE_MISS_ALWAYS ) {
if (set->tags[0] == tag) {
ICACHE_TOUCH(set->LRU, 0);
setindex=0;
MS_INSTRUCTION_SET(cpuNum,0);
QUICK_ICACHE_SET(cpuNum, pAddr, set, setindex);
goto hit;
}
#if ICACHE_ASSOC > 1
if (set->tags[1] == tag) {
ICACHE_TOUCH(set->LRU, 1);
setindex=1;
MS_INSTRUCTION_SET(cpuNum,1);
QUICK_ICACHE_SET(cpuNum, pAddr, set, setindex);
goto hit;
}
#endif
#if ICACHE_ASSOC > 2
if (set->tags[2] == tag) {
ICACHE_TOUCH(set->LRU, 2);
setindex=2;
MS_INSTRUCTION_SET(cpuNum,2);
QUICK_ICACHE_SET(cpuNum, pAddr, set, setindex);
goto hit;
}
#endif
#if ICACHE_ASSOC > 3
if (set->tags[3] == tag) {
ICACHE_TOUCH(set->LRU, 3);
setindex=3;
MS_INSTRUCTION_SET(cpuNum,3);
QUICK_ICACHE_SET(cpuNum, pAddr, set, setindex);
goto hit;
}
#endif
}
ret = ICacheMiss(cpuNum, vAddr, pAddr, &setindex);
if (ret != SUCCESS) {
return ret;
} else {
goto hit;
}
hit:
MS_INSTRUCTION(cpuNum, pAddr);
lineindex = pAddr & (ICACHE_LINE_SIZE-1);
*inst = *(Inst *) (set->data[setindex] + lineindex);
return SUCCESS;
}
/*****************************************************************
* ICacheMiss
*
* XXX - Should make this virtually index, physically tagged.
*****************************************************************/
Result
ICacheMiss(int cpuNum, VA vAddr, PA pAddr, int *setindex)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
int mhtind;
int ind = ICACHE_INDEXOF(pAddr);
int lru;
MHTStatus mhtret;
SCResult sret;
if (VERBOSE_DEBUG) {
CPUPrint("%d: ICacheMiss: VA: %8.8lx PA: %8.8lx\n",cpuNum, vAddr, pAddr);
}
#ifdef MIPSY_MXS
if (!MxsApproveImiss(PE[cpuNum].st, pAddr)) {
/* This means that the current Imiss could cause a previous load or
* store to be aborted due to a coherency exception. We stall the
* miss until the condition clears. This is a performance optimization
* that becomes necessary if we speculate through SC instructions.
*/
return FAILURE;
}
#endif /* MIPSY_MXS */
/*
* Check the second level cache for the line. Before we
* record the miss in the ICache MHT entry make sure
* this isn't a re-issued ICache miss from a CONFLICT case.
*/
lru = ICACHE_LRU(CACHE[cacheNum].ICache.set[ind].LRU);
mhtret = AllocMHT(cpuNum, SC_IGET, vAddr,
pAddr,
ICACHE_LINE_SIZE, lru, &mhtind);
switch (mhtret) {
case MHTFULL:
return FAILURE;
case MHTCONFLICT:
return FAILURE;
case MHTMERGE:
/*
* This can occur if someone does a data acces to a line
* and then jumps to it.
*/
return STALL;
default:
ASSERT(mhtret == MHTSUCCESS);
break;
}
if (CACHE[cacheNum].ICache.set[ind].tags[lru] != INVALID_TAG) {
uint type = E_L1 | E_I | E_READ | E_FLUSH_CLEAN;
PA replPA = ITAG_TO_PA(CACHE[cacheNum].ICache.set[ind].tags[lru], ind);
L1_LINE_TRANS_EVENT(cpuNum, replPA, type, lru,
IS_KUSEG(CURRENT_PC(cpuNum)));
}
CACHE[cacheNum].ICache.set[ind].tags[lru] = INVALID_TAG;
/*
* Forward the request on to the secondary cache.
*/
sret = SCacheFetch(cpuNum, vAddr, pAddr, SC_IGET, mhtind);
if (sret == SCRETRY) {
/*
* We must of conflicted with a data miss. Deallocate
* MHT entry and stall waiting for the SMHT to clear up.
* XXX - Should me reissue this ourselves?
*/
FreeMHT(cpuNum, mhtind);
return FAILURE;
} else {
if (sret == SCBUSERROR) {
FreeMHT(cpuNum, mhtind);
return BUSERROR;
}
}
/* Due to the MHT and the fact that the scache will call ICachePUT
and will fill in the pcache, this routine will only be called on
once on the miss to the icache line. */
CACHE[cacheNum].stats.ICache.ReadMisses++;
if (sret == SCSTALL) {
return STALL;
}
/* SCacheFetch may return SCSUCCESS if zero latency misses
* are configured. Record this as a miss but don't stall.
*/
(*setindex) = lru;
ASSERT(sret == SCSUCCESS);
return SUCCESS;
}
/*****************************************************************
* ICacheFlush
*****************************************************************/
void
ICacheFlush(int cpuNum, PA paddr, int size)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
PA a;
int ind;
int set;
QUICK_ICACHE_CLEAR(cpuNum);
CACHE[cacheNum].stats.ICache.Inval++;
for (a = paddr; a < paddr + size; a += ICACHE_LINE_SIZE) {
ind = ICACHE_INDEXOF(a);
if (CACHE[cacheNum].ICache.set[ind].tags[0] == ICACHE_TAG(a)) {
set = 0;
goto foundit;
}
#if ICACHE_ASSOC > 1
if (CACHE[cacheNum].ICache.set[ind].tags[1] == ICACHE_TAG(a)) {
set = 1;
goto foundit;
}
#endif
#if ICACHE_ASSOC > 2
if (CACHE[cacheNum].ICache.set[ind].tags[2] == ICACHE_TAG(a)) {
set = 2;
goto foundit;
}
#endif
#if ICACHE_ASSOC > 3
if (CACHE[cacheNum].ICache.set[ind].tags[3] == ICACHE_TAG(a)) {
set = 3;
goto foundit;
}
#endif
continue; /* not found, go around loop */
foundit:
CACHE[cacheNum].stats.ICache.LinesInval++;
CACHE[cacheNum].ICache.set[ind].tags[set] = INVALID_TAG;
ICACHE_MAKE_LRU(CACHE[cacheNum].ICache.set[ind].LRU, set);
L1_LINE_TRANS_EVENT(cpuNum, a,
(E_L1 | E_I | E_READ | E_EXTERNAL | E_FLUSH_CLEAN), 0,
IS_KUSEG(CURRENT_PC(cpuNum)));
}
}
/*****************************************************************
* AllocMHT
*
* Allocate an entry in the miss handling table for the
* specified first level miss. This routine is responsible for
* checking for conflict and merge miss as well.
*****************************************************************/
static MHTStatus
AllocMHT(int cpuNum, SCacheCmd cmd, VA vAddr, PA pAddr, int size,
int lru, int *mhtind)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
int entry;
int ind,cacheLineSize;
if (cmd == SC_IGET) {
ind = ICACHE_INDEXOF(pAddr);
cacheLineSize = ICACHE_LINE_SIZE;
} else {
ind = DCACHE_INDEXOF(pAddr);
cacheLineSize = DCACHE_LINE_SIZE;
}
ASSERT( size == cacheLineSize );
if (CACHE[cacheNum].MHTnumInuse == 0) {
entry = 0; /* Special case for speed: allocate when MHT is empty */
} else {
int numChecked = CACHE[cacheNum].MHTnumInuse;
int i;
entry = -1;
for (i = 0; (i < MHT_SIZE) && numChecked; i++) {
if (!CACHE[cacheNum].MHT[i].inuse) {
entry = i;
} else {
numChecked--;
if (CACHE[cacheNum].MHT[i].ind == ind) {
if ( (cmd==SC_IGET && CACHE[cacheNum].MHT[i].cmd==SC_IGET) ||
(cmd!=SC_IGET && CACHE[cacheNum].MHT[i].cmd!=SC_IGET) ) {
*mhtind = i;
if (!SameCacheLine(CACHE[cacheNum].MHT[i].pAddr, pAddr,
cacheLineSize)) {
/* Currently we only support one miss per cache index */
return MHTCONFLICT;
}
/* Except we call them merges if two misses happen to the same
* line */
if (CACHE[cacheNum].MHT[i].cmd != cmd) {
/* Call it a conflict if we need a GETX and only a
* GET is outstanding.
*/
return MHTCONFLICT;
}
return MHTMERGE;
}
}
}
}
if (entry == -1) {
if (i == MHT_SIZE) {
/* MHT is full */
return MHTFULL;
} else {
entry = i;
}
}
}
CACHE[cacheNum].MHTnumInuse++;
CACHE[cacheNum].MHT[entry].inuse = TRUE;
CACHE[cacheNum].MHT[entry].startTime = CPUVec.CycleCount(cpuNum);
CACHE[cacheNum].MHT[entry].cmd = cmd;
CACHE[cacheNum].MHT[entry].vAddr = vAddr;
CACHE[cacheNum].MHT[entry].pAddr = pAddr;
CACHE[cacheNum].MHT[entry].PC = CURRENT_PC(cpuNum);
CACHE[cacheNum].MHT[entry].ind = ind;
CACHE[cacheNum].MHT[entry].lru = lru;
CACHE[cacheNum].MHT[entry].writeBuffer->active = FALSE;
*mhtind = entry;
#ifdef MIPSY_MXS
CACHE[cacheNum].MHT[entry].cpu_action = GetMxsAction (PE[cpuNum].st);
#endif
return MHTSUCCESS;
}
/*****************************************************************
* FreeMHT - Delete a MHT entry
*
*****************************************************************/
static void
FreeMHT(int cpuNum, int entryNum)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
ASSERT(CACHE[cacheNum].MHT[entryNum].inuse);
CACHE[cacheNum].MHT[entryNum].inuse = 0;
CACHE[cacheNum].MHTnumInuse--;
ASSERT(CACHE[cacheNum].MHTnumInuse >= 0);
ASSERT(CACHE[cacheNum].MHT[entryNum].writeBuffer->active == FALSE);
}
/*****************************************************************
* ICachePUT
* Since this is the ICache, no real writing.
*****************************************************************/
static void
ICachePUT(int cpuNum, MHT *mht, int mode)
{
int result;
int cacheNum = GET_CACHE_NUM(cpuNum);
PA paddr = mht->pAddr;
int ind = ICACHE_INDEXOF(paddr);
PA tag = ICACHE_TAG(paddr);
int lru = mht->lru;
char *data;
int way =0;
ASSERT(mht->cmd & SC_IGET);
ASSERT(CACHE[cacheNum].ICache.set[ind].tags[lru] == INVALID_TAG);
/* NOTE: This call is no longer optional! We use it to find
the scache line that will be used to satisfy the data part of
the miss */
result = IsInSCache(cpuNum, paddr, MEMSYS_SHARED, &data, &way);
if (!result) {
CPUError("ICachePUT didn't find line in SCache (with IsInSCache())");
}
CACHE[cacheNum].ICache.set[ind].tags[lru] = tag;
#ifdef DATA_HANDLING
CACHE[cacheNum].ICache.set[ind].data[lru] =
data+((paddr&(SCACHE_LINE_SIZE-1)&~(PA)(ICACHE_LINE_SIZE-1)));
/* Point L1 to data location in L2 */
#else
CACHE[cacheNum].ICache.set[ind].data[lru] =
(byte*)CPUVec.PAToHostAddr(cpuNum, (paddr&~(PA)(ICACHE_LINE_SIZE-1)), 0);
#endif
}
/*****************************************************************
* MemRefReadData
* Return values:
* SUCCESS - Request hit in the cache, data returned.
* STALL - Request missed in the cache, sent to the 2nd-level or memsys.
* FAILURE - Request missed but couldn't be issued to 2nd-level or
* memory system. Caller must retry.
* BUSERROR - Request suffered a bus error.
*
* Main entry into 1st level data cache from CPU.
*****************************************************************/
Result
MemRefReadData(int cpuNum, VA vAddr, PA pAddr, void *data,
RefSize size, RefFlavor flavor)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
PA tag = DCACHE_TAG(pAddr);
int ind = DCACHE_INDEXOF(pAddr);
struct DCache *dcache = &CACHE[cacheNum].DCache;
struct DCacheSet *set = &(dcache->set[ind]);
Result ret;
int lineindex;
int setindex;
if ( DCACHE_HIT_ALWAYS ) {
setindex = 0;
set->data[setindex] =
(byte*)CPUVec.PAToHostAddr(cpuNum, pAddr&~(PA)(DCACHE_LINE_SIZE-1), vAddr);
goto hit;
}
if ( !DCACHE_MISS_ALWAYS ) {
if (((~EXCLUSIVE_TAG) & (set->tags[0])) == tag) {
DCACHE_TOUCH(set->LRU, 0);
setindex=0;
goto hit;
}
#if DCACHE_ASSOC > 1
if (((~EXCLUSIVE_TAG) & (set->tags[1])) == tag) {
DCACHE_TOUCH(set->LRU, 1);
setindex=1;
goto hit;
}
#endif
#if DCACHE_ASSOC > 2
if (((~EXCLUSIVE_TAG) & (set->tags[2])) == tag) {
DCACHE_TOUCH(set->LRU, 2);
setindex=2;
goto hit;
}
#endif
#if DCACHE_ASSOC > 3
if (((~EXCLUSIVE_TAG) & (set->tags[3])) == tag) {
DCACHE_TOUCH(set->LRU, 3);
setindex=3;
goto hit;
}
#endif
}
/* Cache misses go here */
if (VERBOSE_DEBUG) {
CPUPrint("%d: Read D$ MISS VA: 0x%8.8lx PA: 0x%8.8lx\n",
cpuNum, vAddr, pAddr);
}
ret = DCacheReadMiss(cpuNum, vAddr, pAddr, data, size, flavor, &setindex);
if (ret != SUCCESS) {
return ret;
} else {
ASSERT(setindex >=0 && setindex < DCACHE_ASSOC);
goto hit;
}
hit:
if ( !DCACHE_HIT_ALWAYS ) {
if (useWriteBuffer && AddrIsInWriteBuffer(cpuNum, pAddr, size)) {
/*
* We currently don't implement any kind of load bypassing so
* we stall when a load hits a buffered write word.
*/
return STALL;
}
}
DATA_READ_EVENT();
FALSE_SHARING_ACCESS(cpuNum,set->fSharing[setindex],pAddr);
lineindex = pAddr & (DCACHE_LINE_SIZE-1);
switch (size) {
case WORD_SZ:
*(int *) data = *(int *) (set->data[setindex] + lineindex);
if (VERBOSE_DEBUG) {
CPUPrint("%d: Read D$ hit VA: 0x%8.8lx PA: 0x%8.8lx WORD data: 0x%8.8lx lineindex=%d\n",
cpuNum, vAddr, pAddr, *(int *) (set->data[setindex] + lineindex), lineindex);
}
break;
case BYTE_SZ:
*(byte *) data = *(byte *) (set->data[setindex] + lineindex);
if (VERBOSE_DEBUG) {
CPUPrint("%d: Read D$ hit VA: 0x%8.8lx PA: 0x%8.8lx BYTE data: 0x%2.2x lineindex=%d\n",
cpuNum, vAddr, pAddr, *(char *) (set->data[setindex] + lineindex), lineindex);
}
break;
case HALF_SZ:
*(short *) data = *(short *) (set->data[setindex] + lineindex);
if (VERBOSE_DEBUG) {
CPUPrint("%d: Read D$ hit VA: 0x%8.8lx PA: 0x%8.8lx HALF data: 0x%8.8lx lineindex=%d\n",
cpuNum, vAddr, pAddr, *(short *) (set->data[setindex] + lineindex), lineindex);
}
break;
case DOUBLE_SZ:
*(uint64 *) data = *(uint64 *) (set->data[setindex] + lineindex);
if (VERBOSE_DEBUG) {
#ifdef __alpha
CPUPrint("%d: Read D$ hit VA: 0x%8.8lx PA: 0x%8.8lx DOUBLE data: 0x%16.16lx lineindex=%d\n",
cpuNum, vAddr, pAddr, *(uint64 *) (set->data[setindex] + lineindex), lineindex);
#else
CPUPrint("%d: Read D$ hit VA: 0x%8.8lx PA: 0x%8.8lx DOUBLE data: 0x%16.16llx\n",
cpuNum, vAddr, pAddr, *(uint64 *) (set->data[setindex] + lineindex));
#endif
}
break;
default:
CPUError("Bad size %d passed to ReadDCache\n", size);
break;
}
return SUCCESS;
}
/*****************************************************************
* DCacheReadMiss
*
*****************************************************************/
static Result
DCacheReadMiss(int cpuNum, VA vAddr, PA pAddr, void *data,
RefSize size, RefFlavor flavor, int *indPtr)
{
PA writebackAddr;
int cacheNum = GET_CACHE_NUM(cpuNum);
int ind = (pAddr/DCACHE_LINE_SIZE) % DCACHE_INDEX;
int mhtind;
int lru;
SCacheCmd cmd;
MHTStatus mhtret;
SCResult sret;
if (noUpgrades) {
cmd = (flavor == LL_FLAVOR ? SC_DLLGETX : SC_DGETX);
} else {
cmd = (flavor == LL_FLAVOR ? SC_DLLGET : SC_DGET);
}
lru = DCACHE_LRU(CACHE[cacheNum].DCache.set[ind].LRU);
mhtret = AllocMHT(cpuNum, cmd, vAddr,
pAddr, DCACHE_LINE_SIZE, lru, &mhtind);
switch (mhtret) {
case MHTFULL:
return FAILURE;
case MHTCONFLICT:
return FAILURE;
case MHTMERGE:
(*indPtr) = mhtind;
return STALL;
default:
ASSERT(mhtret == MHTSUCCESS);
break;
}
if (VERBOSE_DEBUG) {
CPUPrint("%d: DCacheReadMiss: VA: %8.8lx PA: %8.8lx\n",cpuNum, vAddr,
pAddr);
}
if (CACHE[cacheNum].DCache.set[ind].tags[lru] & EXCLUSIVE_TAG) {
CACHE[cacheNum].stats.DCache.Writebacks++;
#ifdef DATA_HANDLING
{
int way;
char *scacheData;
/* Generate writeback address */
writebackAddr = DTAG_TO_PA(CACHE[cacheNum].DCache.set[ind].tags[lru],
ind);
if (!(IsInSCache(cacheNum, writebackAddr, MEMSYS_EXCLUSIVE,
&scacheData, &way))) {
CPUError("DCacheReadMiss didn't find excl. line in SCache for WB");
}
}
#endif
{
uint type = E_L1 | E_D | E_READ | E_WRITEBACK;
PA replPA = DTAG_TO_PA(CACHE[cacheNum].DCache.set[ind].tags[lru], ind);
L1_LINE_TRANS_EVENT(cpuNum, replPA, type, lru,
IS_KUSEG(CURRENT_PC(cpuNum)));
}
} else {
if (!(CACHE[cacheNum].DCache.set[ind].tags[lru] & INVALID_TAG)) {
/* Generate "replacement" address */
writebackAddr = DTAG_TO_PA(CACHE[cacheNum].DCache.set[ind].tags[lru],
ind);
if (VERBOSE_DEBUG) {
CPUPrint("%d: dcache discarding clean line %8.8lx\n",
cpuNum,writebackAddr);
}
}
if (!(CACHE[cacheNum].DCache.set[ind].tags[lru] & INVALID_TAG)) {
uint type = E_L1 | E_D | E_READ | E_FLUSH_CLEAN;
PA replPA = DTAG_TO_PA(CACHE[cacheNum].DCache.set[ind].tags[lru], ind);
L1_LINE_TRANS_EVENT(cpuNum, replPA, type, lru,
IS_KUSEG(CURRENT_PC(cpuNum)));
}
}
CACHE[cacheNum].DCache.set[ind].tags[lru] = INVALID_TAG;
sret = SCacheFetch(cpuNum, vAddr, pAddr, cmd, mhtind);
if (sret == SCRETRY) {
/*
* We must of conflicted with a data miss. Clean MHT and
* stall.
* XXX should we reissue this ourselves?
*/
FreeMHT(cpuNum, mhtind);
return FAILURE;
} else if (sret == SCBUSERROR) {
FreeMHT(cpuNum, mhtind);
return BUSERROR;
}
CACHE[cacheNum].stats.DCache.ReadMisses++;
if (sret == SCSTALL) {
(*indPtr) = mhtind;
return STALL;
}
/*
* SCacheFetch may return SUCCESS if zero latency misses
* are configured. Record this as a miss but don't stall.
*/
(*indPtr) = lru;
return SUCCESS;
}
/*****************************************************************
* DCachePUT
*****************************************************************/
static void
DCachePUT(int cpuNum, MHT *mht, int mode)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
PA pAddr = mht->pAddr;
int ind = DCACHE_INDEXOF(pAddr);
int lru = mht->lru;
int way = 0;
PA tag = DCACHE_TAG(pAddr);
char *data;
/* NOTE: This call is no longer optional! We use it to find
the SCache line that will be used to satisfy the data part of
the miss */
if (!(IsInSCache(cpuNum, pAddr, mode, &data, &way))) {
CPUError("DCachePUT didn't find line in SCache (with IsInSCache())");
}
if (mode & MEMSYS_EXCLUSIVE)
tag |= EXCLUSIVE_TAG;
if (mht->cmd == SC_DSCUGETX
&& (CACHE[cacheNum].DCache.set[ind].tags[lru] & INVALID_TAG)) {
CPUVec.ClearLockFlag(cpuNum); /* SC fails due to a race */
}
/* We don't want to copy data if it is already in the DCache
* exclusive.
*/
if (!((CACHE[cacheNum].DCache.set[ind].tags[lru] & EXCLUSIVE_TAG) &&
((CACHE[cacheNum].DCache.set[ind].tags[lru] & ~EXCLUSIVE_TAG) ==
(tag & ~EXCLUSIVE_TAG)))) {
CACHE[cacheNum].DCache.set[ind].tags[lru] = tag;
#ifdef DATA_HANDLING
CACHE[cacheNum].DCache.set[ind].data[lru] =
data+((pAddr&(SCACHE_LINE_SIZE-1)&~(PA)(DCACHE_LINE_SIZE-1)));
/* Point L1 to data location in L2 */
#else
CACHE[cacheNum].DCache.set[ind].data[lru] =
(byte*)CPUVec.PAToHostAddr(cpuNum, pAddr&~(PA)(DCACHE_LINE_SIZE-1), mht->vAddr);
#endif
CACHE[cacheNum].DCache.set[ind].fSharing[lru] =
SCacheLineToFalseSharing(cacheNum,pAddr,way);
}
if (mht->writeBuffer->active) {
ASSERT(CACHE[cacheNum].DCache.set[ind].tags[lru] & EXCLUSIVE_TAG);
RetireWriteBuffer(cpuNum, mht, CACHE[cacheNum].DCache.set[ind].data[lru]);
}
DCACHE_TOUCH(CACHE[cacheNum].DCache.set[ind].LRU, lru);
}
/***************************************************************
* DCacheFlush
*
* This routine now takes two parameters: writeback and discard.
* Writeback: Should I write the line back to the scache if
* it is dirty?
* Retain: After this call, should the line be retained in my
* cache? (If the line was exclusive and writeback
* is set, the line is downgraded to Shared).
*
* NOTE: writeback==0 && retain==1 is effectively a NOP. We'll
* support that, but I don't see why it should ever be used.
****************************************************************/
/* FIXTHIS: These statistics are mimimally correct, but not much more */
static void
DCacheFlush(int cpuNum, int writeback, int retain, PA paddr, int size)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
PA a;
int ind, set;
ASSERT (writeback || !retain);
/* One of these two should be set */
/* ASSERT for now */
ASSERT((paddr & (DCACHE_LINE_SIZE-1)) == 0);
if (writeback && retain) {
CACHE[cacheNum].stats.DCache.Downgrade++;
} else if (!writeback && !retain) {
CACHE[cacheNum].stats.DCache.Inval++;
}
for (a = paddr; a < paddr + size; a += DCACHE_LINE_SIZE) {
ind = DCACHE_INDEXOF(a);
if ((CACHE[cacheNum].DCache.set[ind].tags[0] & (~EXCLUSIVE_TAG))
== DCACHE_TAG(a)) {
set = 0;
goto foundit;
}
#if DCACHE_ASSOC > 1
if ((CACHE[cacheNum].DCache.set[ind].tags[1] & (~EXCLUSIVE_TAG)) == DCACHE_TAG(a)) {
set = 1;
goto foundit;
}
#endif
#if DCACHE_ASSOC > 2
if ((CACHE[cacheNum].DCache.set[ind].tags[2] & (~EXCLUSIVE_TAG)) == DCACHE_TAG(a)) {
set = 2;
goto foundit;
}
#endif
#if DCACHE_ASSOC > 3
if ((CACHE[cacheNum].DCache.set[ind].tags[3] & (~EXCLUSIVE_TAG)) == DCACHE_TAG(a)) {
set = 3;
goto foundit;
}
#endif
continue; /* not present, go around the loop */
foundit:
if ((CACHE[cacheNum].DCache.set[ind].tags[set] & EXCLUSIVE_TAG) != 0) {
/* Line is exclusive */
if (writeback) {
CACHE[cacheNum].stats.DCache.LinesWriteback++;
CACHE[cacheNum].DCache.set[ind].tags[set] = DCACHE_TAG(a);
#ifdef DATA_HANDLING
{
char *data;
int way;
/* NOTE: This call is not optional for
DATA_HANDLING..we use it to find the scache line
that will be used to receive the writeback */
if (!(IsInSCache(cpuNum, a, 0, &data, &way))) {
CPUError("DCacheFlush didn't find line in SCache");
}
/* Write the line back to the scache */
}
#endif
L1_LINE_TRANS_EVENT(cpuNum, a,
(E_L1 | E_D | E_EXTERNAL | E_DOWNGRADE), 0,
IS_KUSEG(CURRENT_PC(cpuNum)));
}
}
if (!writeback && !retain) {
uint type;
CACHE[cacheNum].stats.DCache.LinesInval++;
if (CACHE[cacheNum].DCache.set[ind].tags[set] & EXCLUSIVE_TAG) {
type = E_L1 | E_D | E_EXTERNAL | E_WRITEBACK;
} else {
type = E_L1 | E_D | E_EXTERNAL | E_FLUSH_CLEAN;
}
L1_LINE_TRANS_EVENT(cpuNum, a, type, 0,
IS_KUSEG(CURRENT_PC(cpuNum)));
}
if (retain) {
CACHE[cacheNum].DCache.set[ind].tags[set] &= ~EXCLUSIVE_TAG;
/* Take away exclusive ownership */
} else {
CACHE[cacheNum].DCache.set[ind].tags[set] = INVALID_TAG;
}
DCACHE_MAKE_LRU(CACHE[cacheNum].DCache.set[ind].LRU, set);
}
}
/****************************************************************
* MemRefWriteData
* Return values:
* SUCCESS - Request hit in the cache, data written.
* STALL - Request missed in the cache, sent to the 2nd-level or memsys.
* FAILURE - Request missed but couldn't be issued to 2nd-level or
* memory system. Caller must retry.
* BUSERROR - Request suffered a bus error.
* SCFAILURE - Was an SC and it failed.
*
* Main entry into 1st level data cache writing from the CPU
*****************************************************************/
Result
MemRefWriteData(int cpuNum, VA vAddr, PA pAddr, uint64 data,
RefSize size, RefFlavor flavor)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
PA tag = DCACHE_TAG_EX(pAddr);
struct DCacheSet* set;
bool upgrade;
int way;
int mhtind = -1;
int lineindex;
int ind = DCACHE_INDEXOF(pAddr);
set = & CACHE[cacheNum].DCache.set[ind];
/* See if weve been stalled on the MHT, update stats if so */
if (useWriteBuffer) {
STATS_ADD_INTERVAL(cpuNum, writeBufferStats.writeMHTStallTime,
writeMHTStallStart);
}
if ( DCACHE_HIT_ALWAYS ) {
way = 0;
set->data[way] =
(byte*)CPUVec.PAToHostAddr(cpuNum, pAddr&~(PA)(DCACHE_LINE_SIZE-1), vAddr);
goto foundit;
}
if (!DCACHE_MISS_ALWAYS ) {
if (set->tags[0] == tag) {
way = 0;
goto foundit;
}
#if DCACHE_ASSOC > 1
if (set->tags[1] == tag) {
way = 1;
goto foundit;
}
#endif
#if DCACHE_ASSOC > 2
if (set->tags[2] == tag) {
way = 2;
goto foundit;
}
#endif
#if DCACHE_ASSOC > 3
if (set->tags[3] == tag) {
way = 3;
goto foundit;
}
#endif
}
if (set->tags[0] == DCACHE_TAG(pAddr)) {
way = 0;
upgrade = TRUE;
goto miss_or_upgrade;
}
#if DCACHE_ASSOC > 1
if (set->tags[1] == DCACHE_TAG(pAddr)) {
way = 1;
upgrade = TRUE;
goto miss_or_upgrade;
}
#endif
#if DCACHE_ASSOC > 2
if (set->tags[2] == DCACHE_TAG(pAddr)) {
way = 2;
upgrade = TRUE;
goto miss_or_upgrade;
}
#endif
#if DCACHE_ASSOC > 3
if (set->tags[3] == DCACHE_TAG(pAddr)) {
way = 3;
upgrade = TRUE;
goto miss_or_upgrade;
}
#endif
/****** a true miss (not an upgrade) ********/
upgrade = FALSE;
way = DCACHE_LRU(set->LRU);
if (set->tags[0] == DCACHE_TAG(pAddr)) {
way = 0;
upgrade = TRUE;
goto miss_or_upgrade;
}
#if DCACHE_ASSOC > 1
if (set->tags[1] == DCACHE_TAG(pAddr)) {
way = 1;
upgrade = TRUE;
goto miss_or_upgrade;
}
#endif
#if DCACHE_ASSOC > 2
if (set->tags[2] == DCACHE_TAG(pAddr)) {
way = 2;
upgrade = TRUE;
goto miss_or_upgrade;
}
#endif
miss_or_upgrade:
{
Result ret;
#ifdef DEBUG_BUFFER
CPUPrint("%d: Write D$ MISS VA: 0x%8.8lx PA: 0x%8.8lx\n",
cpuNum, vAddr, pAddr);
#endif
ret = DCacheWriteMiss(cpuNum, vAddr, pAddr, size, flavor, set, way,
upgrade, &mhtind);
if (ret == FAILURE) {
if (flavor == SC_FLAVOR) {
/* SAH: Satisfies problem with write buffer where an SC
was being merged with a regular store in the MHT. */
return SCFAILURE;
} else {
STATS_INC(cpuNum, writeBufferStats.writeMHTStall, 1);
STATS_SET(cpuNum, writeMHTStallStart, CPUVec.CycleCount(cpuNum));
return STALL;
}
}
if (ret == SCFAILURE || ret == BUSERROR) {
return ret;
}
if (ret == STALL) {
/* Check to see if a write buffer can avoid stalling otherwise
* stall. SC don't every get bufferred. */
if (useWriteBuffer && !(flavor & SC_FLAVOR)) {
if(AddToWriteBuffer(cpuNum, pAddr, data, size, mhtind))
return SUCCESS;
else {
STATS_INC(cpuNum, writeBufferStats.writeMHTStall, 1);
STATS_SET(cpuNum, writeMHTStallStart, CPUVec.CycleCount(cpuNum));
return STALL;
}
} else {
return STALL;
}
}
ASSERT(ret == SUCCESS);
}
/* after satisfying the miss (either if it merged with another write in the
* write buffer, or if running on a magic memory system), rejoin
* the hit case and finish processing the access.
*/
foundit:
DCACHE_TOUCH(set->LRU, way);
DATA_WRITE_EVENT();
lineindex = vAddr & (DCACHE_LINE_SIZE-1);
FALSE_SHARING_ACCESS(cpuNum,set->fSharing[way],pAddr);
FALSE_SHARING_MODIFY(cpuNum,pAddr);
switch (size) {
case WORD_SZ:
if (VERBOSE_DEBUG) {
CPUPrint("%d: WRITE D$ hit VA: 0x%8.8lx PA: 0x%8.8lx writing WORD: 0x%8.8lx \n",
cpuNum, vAddr, pAddr, (uint) data);
}
*(uint *) (set->data[way] + lineindex) = (uint)data;
break;
case BYTE_SZ:
if (VERBOSE_DEBUG) {
CPUPrint("%d: WRITE D$ hit VA: 0x%8.8lx PA: 0x%8.8lx writing BYTE: 0x%2.2x \n",
cpuNum, vAddr, pAddr, (char) data);
}
*(char *) (set->data[way] + lineindex) = (char)data;
break;
case HALF_SZ:
if (VERBOSE_DEBUG) {
CPUPrint("%d: WRITE D$ hit VA: 0x%8.8lx PA: 0x%8.8lx writing HALF: 0x%8.8lx \n",
cpuNum, vAddr, pAddr, (short) data);
}
*(short *) (set->data[way] + lineindex) = (short)data;
break;
case DOUBLE_SZ:
if (VERBOSE_DEBUG) {
#ifdef __alpha
CPUPrint("%d: WRITE D$ hit VA: 0x%8.8lx PA: 0x%8.8lx write DBLE: 0x%16.16lx\n",
cpuNum, vAddr, pAddr, (uint64) data);
#else
CPUPrint("%d: WRITE D$ hit VA: 0x%8.8lx PA: 0x%8.8lx write DBLE: 0x%16.16llx\n",
cpuNum, vAddr, pAddr, (uint64) data);
#endif
}
*(uint64 *) (set->data[way] + lineindex) = (uint64)data;
break;
default:
CPUError("Bad size request to ReadDCache");
break;
}
return SUCCESS;
}
/*********************************************************************
* DCacheWriteMiss
*********************************************************************/
static Result
DCacheWriteMiss(int cpuNum, VA vAddr, PA pAddr, RefSize size, RefFlavor flavor,
struct DCacheSet* set, int way, bool upgrade, int* mergemhtp)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
SCResult sret;
MHTStatus mhtret;
int mhtind;
SCacheCmd cmd = SC_NO_CMD;
if (flavor != SC_FLAVOR) {
if (upgrade) cmd = SC_DUGETX;
else cmd = SC_DGETX;
} else {
/* Handling of the SC instruction */
if (upgrade) {
cmd = SC_DSCUGETX;
} else {
/* actual write miss on an SC */
if ((DCACHE_MISS_ALWAYS || SCACHE_ASSOC == 1)
&& CPUVec.GetLockFlag(cpuNum)) {
/* Direct-maped L2-cache.
* We do not rely on the presence in the cache, but rather
* on the LLbit. Issue a GETX in this case.
* Same thing if we bypass the L1-cache.
*/
PA instrLine, dataLine;
cmd = SC_DGETX;
STATS_INC(cpuNum, syncStats.llscConflictSuccess, 1);
instrLine = (CURRENT_PC(cpuNum) & ~(SCACHE_LINE_SIZE-1))
& (SCACHE_SIZE-1);
dataLine = (pAddr & ~(SCACHE_LINE_SIZE-1))
& (SCACHE_SIZE-1);
if ((!DCACHE_MISS_ALWAYS) && (instrLine != dataLine)
&& (IS_KSEG0(instrLine))) {
CPUError("Direct-map LL/SC conflict, cpu=%i PC=0x%x pAddr=0x%x\n",
cpuNum, CURRENT_PC(cpuNum), pAddr);
}
} else {
/* SC instructions fail if the line is not in the cache. */
return SCFAILURE;
}
}
}
if (cmd == SC_NO_CMD) CPUError("cmd unset");
/* --- pAddr gets cache-line aligned in call to AllocMHT --- */
mhtret = AllocMHT(cpuNum, cmd, vAddr, pAddr,
DCACHE_LINE_SIZE, way, &mhtind);
*mergemhtp = mhtind;
if (mhtret == MHTFULL || mhtret == MHTCONFLICT) return FAILURE;
if (mhtret == MHTMERGE) {
return STALL;
}
ASSERT(mhtret == MHTSUCCESS);
if (VERBOSE_DEBUG) {
CPUPrint("%d: DCacheWriteMiss: VA: %8.8lx PA: %8.8lx\n",cpuNum,
vAddr, pAddr);
}
/* Write back the current dcache line to make space for the fill!! */
if (set->tags[way] & EXCLUSIVE_TAG) {
CACHE[cacheNum].stats.DCache.Writebacks++;
#ifdef DATA_HANDLING
{
char *scacheData;
int cacheindex;
PA writebackAddr;
int myWay=0;
cacheindex = CACHE[cacheNum].MHT[mhtind].ind;
writebackAddr = DTAG_TO_PA(set->tags[way],cacheindex);
if (VERBOSE_DEBUG) {
CPUPrint("%d: Spilling %8.8lx to make way\n",cpuNum,writebackAddr);
}
if (!(IsInSCache(cpuNum, writebackAddr, MEMSYS_EXCLUSIVE,
&scacheData, &myWay))) {
CPUError("DCacheWriteMiss didn't find exc. line in SCache for WB");
}
}
#endif
}
if (!upgrade) {
/* replacing the previous line in this way */
#ifdef MIPSY_MXS
if (!(set->tags[way] & INVALID_TAG)) {
PA paddr = DTAG_TO_PA(set->tags[way],
(set - CACHE[cacheNum].DCache.set));
DoMxsIntervention(PE[cpuNum].st, paddr, DCACHE_LINE_SIZE, 0);
}
#endif
#ifdef DATA_HANDLING
if (!(set->tags[way] & INVALID_TAG)) {
PA flushAddr = DTAG_TO_PA(set->tags[way],
(set - CACHE[cacheNum].DCache.set));
if (VERBOSE_DEBUG) {
CPUPrint("%d: dcache discarding line %8.8lx\n",
cpuNum,flushAddr);
}
}
#endif
set->tags[way] = INVALID_TAG;
}
sret = SCacheFetch(cpuNum, vAddr, pAddr, cmd, mhtind);
if (sret == SCRETRY) {
/*
* We must of conflicted with another miss. Clean MHT and stall.
* XXX Is this right?
*/
FreeMHT(cpuNum, mhtind);
return FAILURE;
} else if (sret == SCBUSERROR) {
FreeMHT(cpuNum, mhtind);
return BUSERROR;
}
if (!upgrade) {
CACHE[cacheNum].stats.DCache.WriteMisses++;
} else {
CACHE[cacheNum].stats.DCache.UpgradeMisses++;
}
if (sret == SCSUCCESS) {
/*
*SCacheFetch may return SUCCESS if zero latency misses are configured.
*/
return SUCCESS;
}
ASSERT(sret == SCSTALL);
if (flavor == SC_FLAVOR) {
/* Store conditionals are always blocking. This should be just
* return STALL except somebody added the following... why?
*/
*mergemhtp = mhtind;
if (CACHE[cacheNum].MHT[mhtind].inuse) return STALL;
else return SCFAILURE;
}
*mergemhtp = mhtind;
return STALL;
}
void
MemRefRemoveReq(int cpuNum, PA pAddr, int size)
{
MHTRemoveReq(cpuNum, pAddr, SCACHE_LINE_SIZE);
}
#ifdef MIPSY_MXS
/*
* MxsClassifyMiss - Return the memstat classification for the specified
* miss.
*/
int
MxsClassifyMiss(int cpuNum, VA vaddr, PA pAddr, bool isICache)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
int entry;
int paddrMask = ~((isICache ? ICACHE_LINE_SIZE : DCACHE_LINE_SIZE)-1);
int stallType;
for (entry = 0; entry < MHT_SIZE; entry++) {
if (!CACHE[cacheNum].MHT[entry].inuse)
continue;
if ((CACHE[cacheNum].MHT[entry].pAddr & paddrMask) != (pAddr & paddrMask))
continue;
stallType = isICache ? E_I : E_D;
if (CACHE[cacheNum].MHT[entry].smhtEntry == SECOND_LEVEL_HIT_ENTRY) {
stallType |= E_L1;
} else {
if ((CACHE[cacheNum].MHT[entry].cmd == SC_DUGETX) ||
(CACHE[cacheNum].MHT[entry].cmd == SC_DSCUGETX)) {
stallType |= E_L2 | E_UPGRADE;
} else {
stallType |= E_L2;
}
}
return stallType;
}
CPUError("MxsClassifyMiss failed!\n");
return 0;
}
#endif /* MIPSY_MXS */
/*****************************************************************
* Routines for dealing with the miss handling table MHT that
* supports lockup free caches.
*****************************************************************/
/*****************************************************************
* SyncCaches
*
*****************************************************************/
extern Result
MemRefSync(int cpuNum)
{
/*
* First make sure the MHT in the caches are empty then send a sync
* to the memory system.
*/
if (CACHE[cpuNum].MHTnumInuse != 0) {
return STALL;
}
if (memsysVec.MemsysCmd != NULL) {
return memsysVec.MemsysCmd(cpuNum, MEMSYS_SYNC, 0LL, 0, 0LL, 0, 0);
} else {
return SUCCESS;
}
}
/*****************************************************************
* MHTRemoveReq
* Blindly remove any entries corresponding to this address and size
* that are in the MHT.
*****************************************************************/
#define SMALLEST_LINE_SIZE ((ICACHE_LINE_SIZE < DCACHE_LINE_SIZE) \
? ICACHE_LINE_SIZE : DCACHE_LINE_SIZE)
void
MHTRemoveReq(int cpuNum, PA pAddr, int size)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
PA startPaddr, endPaddr;
MHT *mht;
int i;
startPaddr = (pAddr/SMALLEST_LINE_SIZE)*SMALLEST_LINE_SIZE;
endPaddr = (pAddr + size)+SMALLEST_LINE_SIZE-1;
for (i = 0; i < MHT_SIZE; i++) {
if (!CACHE[cacheNum].MHT[i].inuse) continue;
mht = &CACHE[cacheNum].MHT[i];
if ((mht->pAddr >= startPaddr) && (mht->pAddr < endPaddr)) {
mht->smhtEntry = SECOND_LEVEL_INVALID_ENTRY;
/* jmc - in the cases I've tested this mht is never in
* the event queue. Mendel says he thinks that should
* only happen if the event is naked, and then only
* for one cycle (it's the retry-next-cycle mechanism).
* Remove for now.
*/
/* EventCallbackRemove((EventCallbackHdr *) mht); */
FreeMHT(cpuNum, mht - CACHE[cacheNum].MHT);
CPUVec.Unstall(cpuNum);
}
}
}
/*****************************************************************
* MHTReqDoneCallback - callback for bus arb / utilization
*
*****************************************************************/
void
MHTReqDoneCallback(int cpuNum,EventCallbackHdr *hdr, void *arg)
{
MHT *mht = (MHT *)hdr;
MHTReqDone(cpuNum, mht, mht->mode, mht->status);
}
/*****************************************************************
* MHTReqDone
*
*****************************************************************/
void
MHTReqDone(int cpuNum, MHT *mht, int mode, int status)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
if (status == MEMSYS_STATUS_SUCCESS) {
if (mht->cmd & SC_IGET) {
L1_IMISS_EVENT(CPUVec.CycleCount(cpuNum), cpuNum, mht->vAddr, mht->pAddr,
(CPUVec.CycleCount(cpuNum) - mht->startTime),
(E_L1 | E_I | E_READ), mht->lru);
ICachePUT(cpuNum, mht, mode);
} else {
uint type;
switch (mht->cmd & (SC_DGET|SC_DGETX|SC_DUGETX|SC_DSCUGETX|SC_DLLGET|SC_DLLGETX)) {
case SC_DGET:
case SC_DLLGET:
type = E_L1 | E_D | E_READ;
break;
case SC_DGETX:
case SC_DLLGETX:
type = E_L1 | E_D | E_WRITE;
break;
case SC_DUGETX:
type = E_L1 | E_D | E_WRITE | E_UPGRADE;
break;
case SC_DSCUGETX:
type = E_L1 | E_D | E_WRITE | E_SC_UPGRADE;
break;
default:
type = 0;
ASSERT(0);
}
L1_DMISS_EVENT(CPUVec.CycleCount(cpuNum), cpuNum, mht->PC, mht->vAddr,
mht->pAddr, CPUVec.CycleCount(cpuNum) - mht->startTime, type);
DCachePUT(cpuNum, mht, mode);
#ifdef DEBUG_BUFFER
CPUPrint("%d: Calling DCachePUT from MHTReqDone\n", cpuNum);
#endif
}
FreeMHT(cpuNum, mht - CACHE[cacheNum].MHT);
#ifdef MIPSY_MXS
DoMxsAction (PE[cpuNum].st, (void *) (mht - CACHE[cacheNum].MHT),
mht->cpu_action);
#endif
} else if (status == MEMSYS_STATUS_NAK) {
/*
* For NAKs - don't fill the cache line. For
* most commands we need to retry the operation. The
* exception is SC upgrades which should not be retried
* since they will fail anyway.
*/
if (mht->cmd == SC_DSCUGETX) {
/* count the stalled time so far */
SC_NAK_EVENT(CPUVec.CycleCount(cpuNum), cpuNum, mht->PC, mht->vAddr,
mht->pAddr, CPUVec.CycleCount(cpuNum) - mht->startTime,
E_L1 | E_D | E_WRITE | E_UPGRADE | E_SC_NAK);
/* Invalidate the line from the first level cache (per the T5) */
DCacheFlush(cpuNum, 1, 0, mht->pAddr & ~(DCACHE_LINE_SIZE-1),
DCACHE_LINE_SIZE);
FreeMHT(cpuNum, mht - CACHE[cacheNum].MHT);
CPUVec.Unstall(cpuNum);
#ifdef MIPSY_MXS
DoMxsAction (PE[cpuNum].st, (void *) (mht - CACHE[cacheNum].MHT),
mht->cpu_action);
#endif
return;
}
/* HOHA (Horrible Hack)
* For transactions which were aborted due to a Cache Error, we
* don't want to retry.
* To be fixed.
*/
/* RPB: disable this when using MXS or write buffers --
the assumptions made by the HOHA no longer hold.
Also to be fixed. */
#ifndef MIPSY_MXS
if (WRITE_BUFFER_SIZE == 0) {
if (CURRENT_PC(cpuNum) != mht->PC) {
FreeMHT(cpuNum, mht - CACHE[cacheNum].MHT);
CPUVec.Unstall(cpuNum);
return;
}
}
#endif
EventDoCallback(cpuNum,RetryNAKedMHTEntry, (EventCallbackHdr *) mht,
(void *)NULL, NAK_RETRY_TIME);
} else {
CPUError("Can't handle status in MHTReqDone\n");
}
/* --- WARNING: This was put in because SYNCs mean that a request ---
* --- arrival does not necessarily wake up the processor --- */
CPUVec.Unstall(cpuNum);
}
/*****************************************************************
* RetryNAKedMHTEntry
* Event callback for a NAKed first level miss. Retry the operation
* in hopes it will succeed this time.
*****************************************************************/
static void
RetryNAKedMHTEntry(int cpuNum,EventCallbackHdr *event, void *arg)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
MHT *mht = (MHT *) event;
int mhtind = mht - CACHE[cacheNum].MHT;
SCResult sret;
if (mht->cmd == SC_DUGETX) {
mht->cmd = SC_DGETX;
}
switch (mht->cmd) {
case SC_IGET:
CACHE[cacheNum].stats.ICache.RetriedIGets++;
break;
case SC_DGET:
case SC_DLLGET:
CACHE[cacheNum].stats.DCache.RetriedDGets++;
break;
case SC_DGETX:
case SC_DLLGETX:
CACHE[cacheNum].stats.DCache.RetriedDGetXs++;
break;
case SC_DUGETX:
/* --- I had to add this because it happenned (MAH), not sure ---
* --- what stat I need to inc here or if I do need to inc one ---
*/
break;
default:
CPUError("RetryNAKedMHTEntry %d: invalid mht->cmd %d\n", cpuNum,
mht->cmd);
}
sret = SCacheFetch(cpuNum, mht->vAddr, mht->pAddr,
mht->cmd, mhtind);
if ((sret == SCSUCCESS) || (sret == SCSTALL)) {
return;
}
ASSERT(sret == SCRETRY);
/*
* Try again a little later.
*/
EventDoCallback(cpuNum,RetryNAKedMHTEntry, (EventCallbackHdr *) mht,
(void *)NULL , 1);
}
void
MemRefDebugWriteData(int cpuNum, VA vAddr, PA pAddr, char *data, int size)
{
if (size == 0)
return;
#ifndef SOLO
#ifdef SIM_ALPHA
ASSERT(0);
#else
if (IS_KSEG1(vAddr)) {
uint flag;
void *dat;
#if 0
if (!RegistryIsInRange(vAddr, &dat, &flag) ||
!(flag & REG_DATA)) {
CPUWarning("CPU %d MemRefDebugWrite bad uncached addr at %#x\n",
cpuNum, vAddr);
} else {
bcopy(data, (char *)dat, size);
}
#else
if (!RegistryIsInRange(vAddr, &dat, &flag)) {
CPUWarning("CPU %d MemRefDebugWrite bad uncached addr at %#x\n",
cpuNum, vAddr);
} else if (!(flag & REG_DATA)) {
/*XXXblythe should check result*/
(*CPUVec.UncachedPIO)(cpuNum, vAddr, 0, size, data);
} else {
bcopy(data, (char *)dat, size);
}
#endif
return;
}
if (data != (char *) PHYS_TO_MEMADDR(M_FROM_CPU(cpuNum),pAddr)) {
bcopy(data, (char *) PHYS_TO_MEMADDR(M_FROM_CPU(cpuNum), pAddr), size);
}
#endif /* SIM_ALPHA */
#endif /* SOLO */
#ifdef SOLO
bcopy(data, (char *) vAddr, size);
#endif
#ifdef DATA_HANDLING
{
PA p, pEnd;
char *cdata;
int i;
int way=0;
int copySize = 0;
/** Need to check write buffer */
/* Yeah, need to do that indeed... */
pEnd = pAddr+size;
for (p = pAddr; p < pEnd; p += copySize) {
int offset = ((uint)p & (SCACHE_LINE_SIZE-1));
int lsize = SCACHE_LINE_SIZE - offset;
int firstCPU = 1;
copySize = (lsize > size) ? size : lsize;
for (i = 0; i < TOTAL_CPUS; i++) {
if (IsInSCache(i, p, MEMSYS_SHARED, &cdata, &way)) {
bcopy(data+(p-pAddr), cdata+offset, copySize);
if (firstCPU) {
size -= copySize;
firstCPU = 0;
}
}
}
/* not found in any of the caches... */
}
}
#endif
}
void
MemRefDebugReadData(int cpuNum, VA vAddr, PA pAddr, char *data, int size)
{
uint flag;
void *dat;
if (size == 0)
return;
#ifndef SOLO
if (IS_KSEG1(vAddr)) {
#if 0
if (!RegistryIsInRange(vAddr, &dat, &flag) ||
!(flag & REG_DATA)) {
CPUWarning("CPU %d MemRefRead bad uncached addr at %#x\n",
cpuNum, vAddr);
} else {
bcopy((char *)dat, data, size);
}
#else
if (!RegistryIsInRange(vAddr, &dat, &flag)) {
CPUWarning("CPU %d MemRefRead bad uncached addr at %#x\n",
cpuNum, vAddr);
} else if (!(flag & REG_DATA)) {
/*XXXblythe should check result*/
(*CPUVec.UncachedPIO)(cpuNum, vAddr, 1, size, data);
} else {
bcopy((char *)dat, data, size);
}
#endif
return;
}
bcopy((char *) PHYS_TO_MEMADDR(M_FROM_CPU(cpuNum), pAddr), data, size);
#endif
#ifdef SOLO
bcopy((char *) vAddr, data, size);
#endif
#ifdef DATA_HANDLING
{
int i;
char *cdata;
int copySize = 0;
int way = 0;
PA p, pEnd = pAddr+size;
for (p = pAddr; p < pEnd; p += copySize) {
int offset = ((uint)p & (SCACHE_LINE_SIZE-1));
int lsize = SCACHE_LINE_SIZE - offset;
copySize = (lsize > size) ? size : lsize;
if (p + copySize > pEnd) {
copySize = pEnd - p;
}
for (i = 0; i < TOTAL_CPUS; i++) {
if (IsInSCache(i, p, MEMSYS_SHARED, &cdata, &way)) {
bcopy(cdata+offset, data+(p-pAddr), copySize);
size -= copySize;
break;
}
}
/* not found in any of the caches...
* CAVEAT: the data might be in transit deep in the guts of Magic.
* For now, we're going to be very light-headed and just take
* whatever is in memory, hoping for the best.
*/
}
}
#endif
}
/*****************************************************************
* Uncached operations
*****************************************************************/
Result
MemRefWriteUncached(int cpuNum, VA vAddr, PA pAddr, void *data,
RefSize refSize, bool accelerated)
{
int size;
switch(refSize) {
case BYTE_SZ:
size = 1;
break;
case HALF_SZ:
size = 2;
break;
case WORD_SZ:
size = 4;
break;
case DOUBLE_SZ:
size = 8;
break;
default:
size=8;
CPUError("MemRefWriteUncached: invalid refSize %d\n", refSize);
}
/* I send the information into the main request path, but I overload */
/* the use of the wbdata field as the pointer to my data to write */
if (!accelerated) {
return memsysVec.MemsysCmd(cpuNum, MEMSYS_UNCWRITE, pAddr, 0,
MEMSYS_NOADDR, size, (void *) data);
} else {
return memsysVec.MemsysCmd(cpuNum, MEMSYS_UNCWRITE_ACCELERATED, pAddr, 0,
MEMSYS_NOADDR, size, (void *) data);
}
}
Result
MemRefReadUncached(int cpuNum, VA vAddr, PA pAddr, void *data, RefSize refSize)
{
int size;
if (CACHE[cpuNum].MHTnumInuse != 0) {
return STALL;
}
switch(refSize) {
case BYTE_SZ:
size = 1;
break;
case HALF_SZ:
size = 2;
break;
case WORD_SZ:
size = 4;
break;
case DOUBLE_SZ:
size = 8;
break;
}
/* NOTE (DT)
* The MHT index passed here is 0. The memsystem better make sure
* that the MHT is empty when the uncached read is issued. The MHT
* has to be empty, since all previous transactions MUST have
* completed for this to issu.
*/
return memsysVec.MemsysCmd(cpuNum, MEMSYS_UNCREAD, pAddr, 0,
MEMSYS_NOADDR, size, (void *) data);
}
Result
MemRefDMAWrite(int cpuNum, PA pAddr, int transId, int length, byte *data)
{
Result ret;
ret = memsysVec.MemsysCmd(cpuNum, MEMSYS_GETX|MEMSYS_DMAFLAVOR, pAddr,
transId, MEMSYS_NOADDR, length, data);
return ret;
}
Result
MemRefDMARead(int cpuNum, PA pAddr, int transId, int length, byte *data)
{
Result ret;
ret = memsysVec.MemsysCmd(cpuNum, MEMSYS_GET|MEMSYS_DMAFLAVOR, pAddr,
transId, MEMSYS_NOADDR, length, data);
return ret;
}
void
MemRefExit(void)
{
/*
* Drain memory system of any dirty data (e.g. writebacks)
*/
memsysVec.MemsysDrain();
#ifndef SOLO
memsysVec.MemsysDone();
#endif
}
/*****************************************************************
* PrimaryFlush - Flush a range of paddrs from the
* primary caches. This is used when a line is replaced or downgraded
* from the secondary scache.
*****************************************************************/
void
PrimaryFlush(int cpuNum, int writeback, int retain, PA paddr, int size)
{
if (!retain &&ICACHE_INCLUSION) {
/* Only invalidates (not writebacks) concern the ICache. */
ICacheFlush(cpuNum, paddr, size);
}
DCacheFlush(cpuNum, writeback, retain, paddr, size);
#ifdef MIPSY_MXS
DoMxsIntervention(PE[cpuNum].st, paddr, size, retain);
#endif
}
/*****************************************************************
* AddToWriteBuffer
* Add a write request to an MHT entyr's write buffer
*****************************************************************/
static bool
AddToWriteBuffer(int cpuNum, PA pAddr, uint64 data, RefSize size, int mhtind)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
if (!CACHE[cacheNum].MHT[mhtind].writeBuffer->active &&
(CACHE[cacheNum].activeWriteBuffers >= WRITE_BUFFER_SIZE)) {
return FALSE;
} else {
int offset = pAddr & (DCACHE_LINE_SIZE - 1);
char *wbdata = CACHE[cacheNum].MHT[mhtind].writeBuffer->data + offset;
char *wbmask = CACHE[cacheNum].MHT[mhtind].writeBuffer->mask + offset;
switch (size) {
case WORD_SZ:
*(int *) wbdata = (int) data;
*(int *) wbmask = (int) -1;
if (VERBOSE_DEBUG) {
CPUPrint("%d: Add to WB PA: %8.8lx WORD: %8.8lx\n",
cpuNum, pAddr, data);
}
break;
case BYTE_SZ:
*(char *) wbdata = (char) data;
*(char *) wbmask = (char) -1;
if (VERBOSE_DEBUG) {
CPUPrint("%d: Add to WB PA: %8.8lx BYTE: %2.2lx\n",
cpuNum, pAddr, data);
}
break;
case HALF_SZ:
*(short *) wbdata = (short) data;
*(short *) wbmask = (short) -1;
if (VERBOSE_DEBUG) {
CPUPrint("%d: Add to WB PA: %8.8lx HALF: %4.4lx\n",
cpuNum, pAddr, data);
}
break;
case DOUBLE_SZ:
*(uint64 *) wbdata = (uint64) data;
*(uint64 *) wbmask = (uint64) -1LL;
if (VERBOSE_DEBUG) {
#ifdef __alpha
CPUPrint("%d: Add to WB PA: %8.8lx DOUBLE: %16.16lx\n",
cpuNum, pAddr, data);
#else
CPUPrint("%d: Add to WB PA: %8.8lx DOUBLE: %16.16llx\n",
cpuNum, pAddr, data);
#endif
}
break;
default:
CPUError("Bad size (%d) in AddToWriteBuffer\n", size);
}
if (!CACHE[cacheNum].MHT[mhtind].writeBuffer->active) {
CACHE[cacheNum].MHT[mhtind].writeBuffer->active = TRUE;
CACHE[cacheNum].activeWriteBuffers++;
}
return TRUE;
}
}
/*****************************************************************
* AddrIsInWriteBuffer
*****************************************************************/
static bool
AddrIsInWriteBuffer(int cpuNum, PA pAddr, RefSize size)
{
int cacheNum = GET_CACHE_NUM(cpuNum);
int offset = pAddr & (DCACHE_LINE_SIZE - 1);
bool match;
PA testPA = pAddr - offset;
MHT *mht;
if (CACHE[cacheNum].activeWriteBuffers == 0) return FALSE;
for (mht = CACHE[cacheNum].MHT;
mht < (CACHE[cacheNum].MHT + MHT_SIZE);
mht++) {
if (mht->inuse && mht->writeBuffer->active &&
(testPA == (mht->pAddr & ~(DCACHE_LINE_SIZE - 1)))) {
char *wbmask = mht->writeBuffer->mask + offset;
switch (size) {
case WORD_SZ:
match = ((*(int *) wbmask) != 0);
if (VERBOSE_DEBUG) {
CPUPrint("%d: AddrIsInWriteBuffer PA: %8.8lx size: WORD %s\n",
cpuNum, pAddr, match ? "TRUE" : "FALSE");
}
return match;
case BYTE_SZ:
match = ((*(char *) wbmask) != 0);
if (VERBOSE_DEBUG) {
CPUPrint("%d: AddrIsInWriteBuffer PA: %8.8lx size: BYTE %s\n",
cpuNum, pAddr, match ? "TRUE" : "FALSE");
}
return match;
case HALF_SZ:
match = ((*(short *) wbmask) != 0);
if (VERBOSE_DEBUG) {
CPUPrint("%d: AddrIsInWriteBuffer PA: %8.8lx size: HALF %s\n",
cpuNum, pAddr, match ? "TRUE" : "FALSE");
}
return match;
case DOUBLE_SZ:
match = ((*(uint64 *) wbmask) != 0LL);
if (VERBOSE_DEBUG) {
CPUPrint("%d: AddrIsInWriteBuffer PA: %8.8lx size: DOUBLE %s\n",
cpuNum, pAddr, match ? "TRUE" : "FALSE");
}
return match;
default:
CPUError("Bad size (%d) in AddrIsInWriteBuffer\n", size);
}
}
}
if (VERBOSE_DEBUG) {
CPUPrint("%d: AddrIsInWriteBuffer PA: %8.8lx FALSE\n", cpuNum, pAddr);
}
return FALSE;
}
/*****************************************************************
* RetiredWriteBuffer
*****************************************************************/
static void
RetireWriteBuffer(int cpuNum, MHT *mht, byte *data)
{
register int i;
char *wbdata = mht->writeBuffer->data;
char *wbmask = mht->writeBuffer->mask;
#ifdef DEBUG_DATA_VERBOSE
char pbuf[384];
char *pbufptr = pbuf;
if (VERBOSE_DEBUG) {
CPUPrint("%d: Retire WB: Addr = %8.8lx, Data =\n", cpuNum,
mht->pAddr & ~(DCACHE_LINE_SIZE - 1));
}
#endif
for (i = 0; i < DCACHE_LINE_SIZE; i++) {
if (*(wbmask+i) != 0) {
*(data+i) = *(wbdata+i);
*(wbmask+i) = 0;
#ifdef DEBUG_DATA_VERBOSE
sprintf(pbufptr, "%02x", *(data+i));
} else {
sprintf(pbufptr, "XX");
}
pbufptr += 2;
if (((i+1) % 32) == 0) sprintf(pbufptr++, "\n");
else if (((i+1) % 4) == 0) sprintf(pbufptr++, " ");
#else
}
#endif
}
#ifdef DEBUG_DATA_VERBOSE
if (VERBOSE_DEBUG) CPUPrint("%s\n", pbuf);
#endif
mht->writeBuffer->active = FALSE;
CACHE[cpuNum].activeWriteBuffers--;
}
#ifdef CC_CHECKER
extern void MustBeInDirectory(int cpu, uint;
int
CacheChecker(PA paddr, int inCpu, int isExclusive)
{
int cpu;
int ind;
if (paddr != (PA) -1) {
PA tag = isExclusive ? SCACHE_TAG(paddr):SCACHE_TAG_EX(paddr);
ind = (paddr/SCACHE_LINE_SIZE) % SCACHE_INDEX;
#ifdef DIR_CHECKER
MustBeInDirectory(inCpu, paddr);
#endif
for (cpu = 0; cpu < TOTAL_CPUS; cpu++) {
if (cpu == inCpu)
continue;
if (isExclusive) {
if ((((~EXCLUSIVE_TAG)&CACHE[cacheNum].SCache.tags[ind][0]) == tag) ||
(((~EXCLUSIVE_TAG)&CACHE[cacheNum].SCache.tags[ind][1]) == tag)) {
CPUError("Cache check failed at paddr 0x%x\n", paddr);
}
} else {
if ((CACHE[cacheNum].SCache.tags[ind][0] == tag) ||
(CACHE[cacheNum].SCache.tags[ind][1] == tag)) {
CPUError("Cache check failed at paddr 0x%x\n", paddr);
}
}
}
} else {
for (cpu = 0; cpu < TOTAL_CPUS; cpu++) {
for (ind = 0; ind < SCACHE_INDEX; ind++) {
if (CACHE[cacheNum].SCache.tags[ind][0] != INVALID_TAG) {
CacheChecker(SCACHE_TAG2PADDR(CACHE[cacheNum].SCache.tags[ind][0], ind), cpu,
(CACHE[cacheNum].SCache.tags[ind][0]&EXCLUSIVE_TAG)!=0);
}
if (CACHE[cacheNum].SCache.tags[ind][1] != INVALID_TAG) {
CacheChecker(SCACHE_TAG2PADDR(CACHE[cacheNum].SCache.tags[ind][1], ind), cpu,
(CACHE[cacheNum].SCache.tags[ind][0]&EXCLUSIVE_TAG)!=0);
}
}
}
}
return 0;
}
#endif /* CC_CHECKER */