numa.c 60.7 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
/*
 * 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. 
 *
 */

/*****************************************************************
 *  File:  numa.c 
 *
 * NUMA memory system. 
 *
 * Parameters are:
 * NUMA_BUS_TIME: fixed delay for bus/interconnect between CPU and DC
 * NUMA_PILOCAL_DC_TIME
 * NUMA_PIREMOTE_DC_TIME
 * NUMA_NILOCAL_DC_TIME
 * NUMA_NIREMOTE_DC_TIME
 * NUMA_NET_TIME: fixed delay for network transit
 * NUMA_MEM_TIME: contention based delay for access to DRAM
 * Minimum local miss time is 2*(NUMA_BUS_TIME) + (NUMA_PILOCAL_DC_TIME) + (NUMA_MEM_TIME)
 * Minimum remote miss time is 2*(NUMA_BUS_TIME)
 *              + NUMA_PIREMOTE_DC_TIME
 *              + NUMA_NILOCAL_DC_TIME
 *              + NUMA_NIREMOTE_DC_TIME
 *              + (NUMA_MEM_TIME) + 2*(NUMA_NET_TIME)
 *
 * Author: $Author: blythe $
 * Date:   $Date: 2002/05/29 01:09:11 $
 *****************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "syslimits.h"
#include "scache.h"
#include "memsys.h"
#include "sim_error.h"
#include "simutil.h"
#include "eventcallback.h"
#include "list.h"
#include "cpu_interface.h"
#include "registry.h"

/* 
 * Flag for memory system init to prevent repeated mallocs when switching
 */
static uint numaDoneInit = 0;  

#define NUMA_MAX_MEMORIES    64

#define PAGE_NUM(x) (x)/PAGE_SIZE
static uint numaStripeSizePages;
static uint numaStripeSizeBytes;
static uint numaStripeSizeLines;
static uint numaStripeChunk;

/*
 * Numa memsystem version of the counting code in flashlite
 * Counts, at the home, cache misses/CPU (countdown from Trigger) and write misses.
 * Things are done to mirror the flashlite counting implementation closely
 * Count misses in DirContDone for Get and GetX
 * Mark writes in DirContDone for Writeback and InvalCollectAcks for sharing writebacks
 * 
 */
/* # of hot pages pending */
#define MIGREP_PEND 10
/* # of hot pages before interrupt */
#define MIGREP_PEND_INTR migRepPendIntr
int migRepPendIntr = 2;
/* value returned when no pages are left */
#define MIGREP_NULLPAGE 0xffffffff

typedef struct {
   unsigned long sampleCounter;         /* sample count frequency */
   unsigned long pendingPages[MIGREP_PEND];     /* hot pages pending */
   unsigned long pendingPagesCount;     /* number of hot pending pages */
   unsigned char *cmissCounter;         /* cache-miss counter, a byte perc CPU per page */
   unsigned char *writeCounter;         /* write counter, a bit per page */
} MigRepNodeInfo;
MigRepNodeInfo migRepInfo[NUMA_MAX_MEMORIES];   /* One per memory */

/* All variables for resetting counters */
unsigned long counterResetInterval;     /* Cycles between invocations of the reset routine */
unsigned long cmissResetIndex = 0;      /* Next cache-miss counter to reset */
unsigned long writeResetIndex = 0;      /* Next write counter to reset */
unsigned long resetWriteCounters;       /* countdown for resetting write counters */
EventCallbackHdr resetCounterCB;        /* periodic call back for resetting counters */

/* Policy parameters. Will be eventually set through tcl */
/* Sample 1:N */
#define SAMPLE_COUNT migRepSampleCount
int migRepSampleCount = 10;
/* Assuming sampling 1:10 */
int migRepTriggerThreshold = 9;
#define TRIGGER_THRESHOLD migRepTriggerThreshold
/* Reset interval in ms */
int migRepResetInterval = 500;
#define RESET_INTERVAL migRepResetInterval
/* enable counting */
int migRepEnableCounting = 0;
#define ENABLE_COUNT migRepEnableCounting
/* static kern size in pages */
int migRepMaxKern = 0;
#define MAX_KERN migRepMaxKern
/* which node to interrupt */
int migRepIntrHot = 0;
#define INTR_HOT migRepIntrHot
int migRepZeroOnWrite = 0;
#define ZERO_ON_WRITE migRepZeroOnWrite
int migRep4BitCounters = 0;
#define FOUR_BIT_COUNTERS migRep4BitCounters

#ifdef SOLO
#define NumaAddrToMemnum(_addr)  SOLO_PA_NODE(SoloDecompressAddr(0,_addr))
#define NumaAddrToMemline(_addr) (SOLO_PA_OFFSET(SoloDecompressAddr(0,_addr))/SCACHE_LINE_SIZE)
#else
#define NumaAddrToMemnum(_addr) ((_addr/numaStripeSizeBytes)%NUM_MEMORIES(0))
#define NumaAddrToMemline(_addr) (((_addr/numaStripeChunk)*numaStripeSizeLines) \
                                  + ((_addr/SCACHE_LINE_SIZE)%numaStripeSizeLines))

#endif

/*
 * Memory number that is local to this CPU
 * This is the same as the CPU, except if clustering (a la DASH)is used
 */
#define NumaLocalMem(c) ((c)*NUM_MEMORIES(0)/NUM_CPUS(0))
/* Test if cpu c is local to memory m */
#define NumaIsLocal(c, m) ((m) == NumaLocalMem(c))

#define NumaMemFirstCPU(m) ((m)*NUM_CPUS(0)/NUM_MEMORIES(0));
   
/* States for memory and invalidation requests. Helps debugging */
enum MemReqState { REQ_LOCAL_WAIT,      /* Waiting for the local bus */
                   REQ_LOCAL_DC_WAIT,   /* Waiting for the local DC */
                   REQ_NET_WAIT,        /* Sending to the remote DC */
                   REQ_REMOTE_DC_WAIT,  /* Waiting for the remote DC */
                   IN_MEM_DC,           /* In the DC (not really a wait) */
                   INVAL_WAIT,          /* Waiting for invalidates to be done */
                   MEM_WAIT,            /* Waiting to write to DRAM */
                   INVAL_LOCAL_WAIT,    /* Invalidate: waiting for local bus */
                   INVAL_NET_WAIT,      /* Invalidate: Sending to remote DC */
                   INVAL_REMOTE_DC_WAIT,        /* Invalidate: waiting for remote DC */ 
                   INVAL_REMOTE_LOCAL_WAIT,     /* Invalidate: waiting for remote bus */
                   INVAL_ACK_NET_WAIT,  /* Invalidate: Sending Ack to home */
                   INVAL_HOME_DC_WAIT,   /* Invalidate: waiting for home DC */
                   WB_MEM_WAIT,         /* Sharing writeback going to memory */
                   REPLY_NET_WAIT,      /* Sending reply to requesting DC */
                   REPLY_LOCAL_WAIT,    /* Sending reply on bus to requesting processor */
                   REPLY_LOCAL_DC_WAIT, /* Reply waiting for requesting DC */
                   ALL_DONE };

/* A memory request structure */
#define MAX_OUTSTANDING (SIM_MAXCPUS*MEMSYS_MAX_OUTSTANDING)
typedef struct MemRequest  {
   EventCallbackHdr hdr;  /* Must be first!! */
   List_Links  link; /* Must be second (after EventCallbackhdr */
#define MREQ_TO_LIST(_m) (&((_m)->link))
#define LIST_TO_MREQ(_l) ((MemRequest *) (((char *)(_l)) - sizeof(EventCallbackHdr)))
   enum MemReqState state;
   uint dc_delay_time;
   /* Fields till here are common with the InvalRequest and should be kept that way */
   int cmd;
   int transId;
   uint reqAddr;
   uint addr;    /* Address scache aligned */
   unsigned int mode;
   PA replacedPaddr;
   int replaceWasDirty;
   int status;
   int result;   
   int memnum;
   int machnum;
   int cpunum;
   int localMemNum;
   struct DirState *dirState;
   uint starttime;
   byte *data;  /* data handling - dma */
   int len;
   int drainNeeded;
}  MemRequest;

static MemRequest MemRequestStorage[MAX_OUTSTANDING];
static List_Links freeMemReqList;  /* List of available mreq structures */

/*
 * The inval structure must resemble the request structure 
 * for the first few fields, as they are mixed in DC and memory queues
 */
#define MAX_INVALS (MAX_OUTSTANDING*5)
typedef struct InvalRequest  {
   EventCallbackHdr hdr;  /* Must be first!! */
   List_Links  link; /* Must be second (after EventCallbackhdr */
#define INVAL_TO_LIST(_m) (&((_m)->link))
#define LIST_TO_INVAL(_l) ((InvalRequest *) (((char *)(_l)) - sizeof(EventCallbackHdr)))
   enum MemReqState state;
   uint dc_delay_time;
   /* Fields till here are common with the MemRequest and should be kept that way */
   int cpu;             /* CPU to be invalidated */
   MemRequest *mreq;    /* The memory request for this invalidate */
} InvalRequest;

static InvalRequest InvalRequestStorage[MAX_INVALS];
static List_Links freeInvalReqList;  /* List of available inval structures */

/*
 * Directory state for a memory line.
 */
typedef struct DirState {
   unsigned char  numsharers;  /* Number of cpus with line cached. */
   unsigned char  pending:1;   /* 1 if invalidates are pending. */
   unsigned char  dirty:1;     /* Someone has it exclusive */
   unsigned char  bitmap[(SIM_MAXCPUS+7)/8];
   unsigned char  acks;        /* inval acks pending for this directory line */
} DirState;

#define STAT_HIST_BUCKETS 100
#define DEFAULT_HIST_BUCKET_SCALE 30

typedef struct StatsHist {
   unsigned int scaledivisor;
   SimCounter sum;
   SimCounter count;
   SimCounter counts[STAT_HIST_BUCKETS];
} StatsHist;

/*
 * Stats collected per memory system.
 */
typedef struct NumaMemStats {
   SimCounter counts[COUNT_TOTAL];
   StatsHist  reqtime;                  /* latency */
} NumaStats;

/* Global Info about misses, local and remote home */
static NumaStats globalStats[2];

/*
 * State associated with a memory in the system.
 */

typedef struct MemState  { 
   List_Links localQueue;   /* Queue for the Directory Controller */
   List_Links memoryQueue;    /* Queue to memory */
   NumaStats stats;   
   DirState dirState[1]; /* really NUMA_MAX_BYTES_PER_MEMORY/SCACHE_LINE_SIZE */
} MemState;

static MemState *memState[NUMA_MAX_MEMORIES];

#ifdef SOLO
#include "solo_page.h"
#define DATA_ADDR(_m, _pa) SoloGetMemoryAddr(SoloDecompressAddr(0,_pa))
#else
#define DATA_ADDR(_m, _pa) PHYS_TO_MEMADDR(_m, _pa)
#endif

static void DoDCDelay(int num, MemRequest *mreq) ;
static void RequestLocal(int cpuNum, EventCallbackHdr *hdr, void *v);
static void RequestLocalDC(int num, EventCallbackHdr *hdr, void *v) ;
static void RequestNet(int cpuNum, EventCallbackHdr *hdr, void *v) ;
static void RequestRemoteDC(int num, EventCallbackHdr *hdr, void *v) ;
static void DirContEnter(int num, MemRequest *mreq) ;
static void MemContDelay(int num, MemRequest *mreq) ;
static void MemContDone(int num, EventCallbackHdr *hdr, void *v) ;
static void SharingWBDelay(int num, InvalRequest *lreq);
static void SharingWBDone(int num, EventCallbackHdr *hdr, void *v); 
static void DoInvalidates(MemRequest *mreq) ;
static void InvalRequestNet(int num, EventCallbackHdr *hdr, void *v) ;
static void InvalRemoteDC(int num, EventCallbackHdr *hdr, void *v) ;
static void InvalAckNet(int num, EventCallbackHdr *hdr, void *v) ;
static void InvalHomeDC(int num, EventCallbackHdr *hdr, void *v) ;
static void InvalCollectAcks(int num, EventCallbackHdr *hdr, void *v) ;
static void InvalRemoteAckDone(int num, EventCallbackHdr *hdr, void *v); 
static void DirContDone(int num, MemRequest *mreq) ;
static void ReplyNet(int num, EventCallbackHdr *hdr, void *v);
static void ReplyDC(int num, EventCallbackHdr *hdr, void *v);
static void SendReplyToCPU(int cpuNum, EventCallbackHdr *hdr, void *v);

static void MigRepNumaInit(void);
static void MigRepDoWriteCounter(unsigned long addr, int memnum);
static void MigRepDoCmissCounter(MemRequest *mreq);
static void migRepHotPage(unsigned long localPageNum, int cpunum, int memnum, int localmemnum);
uint64 MigRepGetHotPage(int memnum);
uint64 MigRepGetInfo(unsigned long addr, int memnum, 
                                 unsigned long countType, unsigned long countAddr );
void MigRepSetInfo(unsigned long addr, int memnum, uint64 val,
                   unsigned long countType, unsigned long countAddr );
static void MigRepPeriodic(int cpuNum, EventCallbackHdr *hdr, void *v);

void NumaInit(void);
Result NumaCmd(int cpuNum, int cmd, PA addr, 
               int transId, PA replacedPaddr, int writeback, byte *data);
void NumaDone(void);
void NumaDumpStats(void);
void NumaStatus(void);
void NumaDrain(void);


/* Increment a histogram structure */
static void 
StatsIncHist(StatsHist *s, uint value)
{
   uint bucket;
   if (s->scaledivisor == 0) {
      bucket = value / DEFAULT_HIST_BUCKET_SCALE;
   } else { 
      bucket = value / s->scaledivisor;
   }
   if (bucket > STAT_HIST_BUCKETS-1) {
      bucket = STAT_HIST_BUCKETS-1;
   }
   s->counts[bucket]++;
   s->sum += value;
   s->count++;
}


/* increment a stat bucket in the memory and local/remote home structures */
static void
StatsInc(int bucket, MemRequest *mreq, int i)
{
   MemState *mState = memState[mreq->memnum];
   int isLocal = NumaIsLocal(mreq->cpunum, mreq->memnum) ? 1 : 0;

   mState->stats.counts[bucket] += i;
   globalStats[isLocal].counts[bucket] += i;
}



#ifndef SOLO

/*****************************************************************
 * remap region support 
 *****************************************************************/

static PA backmapMask;
static PA nodeaddrMask;

static void
NumaUpdateBackmapMask(void)
{
   /* the backmapMask is an optimization that summarizes all the enabled
    * remap masks; it has ones in any bit position which, if set, means
    * this physical address could not be the target of a remap on any
    * CPU.
    *
    * nodeaddrMask masks out the node id bits.
    */

   int i;
   backmapMask = nodeaddrMask;
   for (i=0; i<TOTAL_CPUS; i++) {
      if (remapVec->RemapEnable[i]) {
	 backmapMask &= remapVec->RemapMask[i];
      }
   }
}

static void
NumaInitRemap(void)
{
   int i;

   /* We initialize backmapMask to all ones except for the
    * bits that might be set in the node id field
    */

   nodeaddrMask = ~0;
   for (i=0; i<TOTAL_CPUS; i++) {
      if (!remapVec->NodeAddrInitialized) {
         int m = M_FROM_CPU(i);
#ifdef TORNADO
         remapVec->NodeAddr[i] = (i*NUM_MEMORIES(m)/NUM_CPUS(m)) *
            (MEM_SIZE(m) / NUM_CPUS(m));
         CPUWarning("numa: nodeaddr for %d is %lx\n",
                    i, (unsigned long)(remapVec->NodeAddr[i]));
#elif defined(SIM_ORIGIN)
         remapVec->NodeAddr[i] = 
            MEMADDR_TO_PHYS(m, SIM_MEM_ADDR(m) + 
                            (MCPU_FROM_CPU(i)/2) * 2 * (MEM_SIZE(m) / NUM_CPUS(m)));
#else
         remapVec->NodeAddr[i] = 
            MEMADDR_TO_PHYS(m, SIM_MEM_ADDR(m) + 
                            i * (MEM_SIZE(m) / NUM_CPUS(m)));
#endif

      }
      nodeaddrMask &= ~remapVec->NodeAddr[i];
   }
   remapVec->NodeAddrInitialized = 1;

   /* now zero out the low bits of the backmapmask */
   NumaUpdateBackmapMask();
}

static void
NumaSetRemap(int cpunum, PA mask)
{
   remapVec->RemapMask[cpunum] = mask;
   NumaUpdateBackmapMask();
}

static void
NumaControlRemap(int cpunum, int isEnabled)
{
   remapVec->RemapEnable[cpunum] = isEnabled;
   NumaUpdateBackmapMask();
}

static PA
NumaGetNodeAddress(int cpunum)
{
   return remapVec->NodeAddr[cpunum];
}

static PA
ReverseRemap(PA paddr, int cpunum)
{
   if ((paddr & remapVec->RemapMask[cpunum]) == remapVec->NodeAddr[cpunum]) {
      return paddr - remapVec->NodeAddr[cpunum];
   } else if ((paddr & remapVec->RemapMask[cpunum]) == 0) {
      return paddr + remapVec->NodeAddr[cpunum];
   } else {
      return paddr;
   }
}

#define BACKMAP_PADDR(paddr,cpunum)		                   \
   (((paddr & backmapMask)			                   \
     || !remapVec->RemapEnable[cpunum])                             \
    ? paddr : ReverseRemap(paddr, cpunum))

#endif

#ifdef SOLO
#define BACKMAP_PADDR(paddr,cpunum)  (paddr)
#endif


/*****************************************************************
 * DirIsASharer
 * Check if the cpunum is a sharer
 *****************************************************************/
static int
DirIsASharer(DirState *dirState, int cpunum)
{
   return (dirState->bitmap[cpunum/8] & (1 << (cpunum % 8)));
}

/*****************************************************************
 * DirSetASharer
 *
 * Add a cpu to the list of processors sharing a cache line
 *****************************************************************/
static void
DirSetASharer(DirState *dirState, int cpunum, int exclusive)
{
   if (!DirIsASharer(dirState, cpunum)) { 
      dirState->numsharers++;
      dirState->bitmap[cpunum/8] |= (1 << (cpunum % 8));
   }
   dirState->dirty = exclusive;
   if (exclusive && (dirState->numsharers != 1)) {
      CPUError("Directory protocol screwup\n");
   }
}


/*****************************************************************
 * DirClearASharer
 *
 * Remove a processor from the list of cache line sharers
 *****************************************************************/
static void
DirClearASharer(DirState *dirState, int cpunum)
{
   if (!DirIsASharer(dirState, cpunum))
      return;
   dirState->numsharers--;
   dirState->bitmap[cpunum/8] &= ~(1 << (cpunum % 8));
   if (dirState->numsharers == 0) 
      dirState->dirty = 0;
}



/********  NUMA STATE MACHINE ROUTINES **********/

/*
 * Queue up for the memory controller
 */ 
static void
DoDCDelay(int num, MemRequest *mreq) 
{
   MemState  *mState = memState[num];

   ASSERT((mreq->dc_delay_time == NUMA_PILOCAL_DC_TIME)
          || (mreq->dc_delay_time == NUMA_PIREMOTE_DC_TIME)
          || (mreq->dc_delay_time == NUMA_NILOCAL_DC_TIME)
          || (mreq->dc_delay_time == NUMA_NIREMOTE_DC_TIME));
   
   if(List_IsEmpty(&mState->localQueue)) {
      EventDoCallback(num, mreq->hdr.rout, 
                      (EventCallbackHdr *) mreq, 
                      0, mreq->dc_delay_time);
   }
   List_Insert(MREQ_TO_LIST(mreq), LIST_ATREAR(&mState->localQueue));
}


/* 
 * Called after the fixed local delay for a request
 * Will queue for the local directory controller
 */
static void
RequestLocal(int cpuNum, EventCallbackHdr *hdr, void *v) 
{
   MemRequest *mreq = (MemRequest *)hdr;

   mreq->state = REQ_LOCAL_DC_WAIT;
   mreq->hdr.rout = RequestLocalDC;

   if (mreq->memnum == mreq->localMemNum) {
      mreq->dc_delay_time = NUMA_PILOCAL_DC_TIME;
   } else {
      mreq->dc_delay_time = NUMA_PIREMOTE_DC_TIME;
   }
   
   DoDCDelay(mreq->localMemNum, mreq);
}


/* 
 * Called after the queued local DC delay for a request is done
 * Will check if the request needs to go remote 
 * and send to the appropriate  directory controller
 */
static void
RequestLocalDC(int num, EventCallbackHdr *hdr, void *v) 
{
   MemRequest *mreq = (MemRequest *)hdr;
   MemState  *mState = memState[num];

   /* 
    * Remove request from list and schedule next callback
    */
  List_Remove(MREQ_TO_LIST(mreq));
   if (!List_IsEmpty(&mState->localQueue)) {
      MemRequest *newReq;
      /*
       * Start the next memory request. 
       */
      newReq = LIST_TO_MREQ(List_First(&(mState->localQueue)));
      EventDoCallback(num, newReq->hdr.rout, (EventCallbackHdr *) newReq, 0,
                      newReq->dc_delay_time);
   } 

   /* Send to the directory controller, with net delay if remote */
   if (mreq->memnum == mreq->localMemNum) {
      DirContEnter(mreq->memnum, mreq);
   } else {
      mreq->state = REQ_NET_WAIT;
      EventDoCallback(mreq->cpunum, RequestNet, 
                      (EventCallbackHdr *) mreq, 
                      0, NUMA_NET_TIME);
   }
}




/* 
 * Called after the fixed net delay for a request is done
 * Now queue for the destination (remote) DC
 */
static void
RequestNet(int cpuNum, EventCallbackHdr *hdr, void *v) 
{
   MemRequest *mreq = (MemRequest *)hdr;

   mreq->state = REQ_REMOTE_DC_WAIT;
   mreq->hdr.rout = RequestRemoteDC;
   mreq->dc_delay_time = NUMA_NILOCAL_DC_TIME;
   
   DoDCDelay(mreq->memnum, mreq);

}



/* 
 * Called after the queued remote DC delay for a request
 * Will send to the  directory controller
 */
static void
RequestRemoteDC(int num, EventCallbackHdr *hdr, void *v) 
{
   MemRequest *mreq = (MemRequest *)hdr;
   MemState  *mState = memState[num];

   /* 
    * Remove request from list and schedule next callback
    */
  List_Remove(MREQ_TO_LIST(mreq));
   if (!List_IsEmpty(&mState->localQueue)) {
      MemRequest *newReq;
      /*
       * Start the next memory request. 
       */
      newReq = LIST_TO_MREQ(List_First(&(mState->localQueue)));
      EventDoCallback(num, newReq->hdr.rout, (EventCallbackHdr *) newReq, 0,
                      newReq->dc_delay_time);
   } 

   /* Send to the directory controller */
   DirContEnter(mreq->memnum, mreq);
}


/*
 * Make the decision on where to get the data from or NAK
 * If uncached op, next step memory
 * If Pending set, this request will be NAKed
 * If !pending and clean with not cached, next step memory
 * If !pending and shared and GET, next step memory
 * If !pending and shared and GETX,next step invalidate 
 *    (memory access overlaps, and is not modelled)
 * If !pending and dirty and GET, next step downgrade
 * If !pending and dirty and GETX, next step invalidate
 */
static void
DirContEnter(int num, MemRequest *mreq) 
{
   DirState *dirState = mreq->dirState;
   int nextop;
#define NEXTOP_MEM 1
#define NEXTOP_INV 2
#define NEXTOP_NAK 3
#define NEXTOP_NONE 4

   mreq->state = IN_MEM_DC;

   /* Decide the next operation based on request type and state of
      the directory, and change the result status if necessary */
   switch (mreq->cmd & MEMSYS_CMDMASK) {
   case MEMSYS_GET:
      if (dirState->pending) {
         nextop = NEXTOP_NAK;
      } else if (dirState->dirty) {
         StatsInc(COUNT_REMOTEDIRTY, mreq, 1);
         nextop = NEXTOP_INV;
         mreq->result = MEMSYS_RESULT_CACHE|MEMSYS_RESULT_DOWNGRADE;
         ASSERT(!dirState->pending);
         dirState->pending = 1;
      } else {
         nextop = NEXTOP_MEM;
         ASSERT(!dirState->pending);
         dirState->pending = 1;
      }
      break;

   case MEMSYS_GETX:
      if (dirState->pending) {
         nextop = NEXTOP_NAK;
      } else if (dirState->numsharers > 0){
         nextop = NEXTOP_INV;
         if (dirState->dirty) {
            StatsInc(COUNT_REMOTEDIRTY, mreq, 1);
            mreq->result = MEMSYS_RESULT_INVALIDATE|MEMSYS_RESULT_CACHE;
         } else {
            StatsInc(COUNT_EXCLUSIVEONSHARED, mreq, 1);
            mreq->result = MEMSYS_RESULT_INVALIDATE|MEMSYS_RESULT_MEMORY;
         }
         ASSERT(!dirState->pending);
         dirState->pending = 1;
      } else {
         nextop = NEXTOP_MEM;
         ASSERT(!dirState->pending);
         dirState->pending = 1;
      }
      break;

   case MEMSYS_UPGRADE:
      if (dirState->pending) {
         nextop = NEXTOP_NAK;
      } else if (!DirIsASharer(dirState, mreq->cpunum)) {
         /* 
          * this is required because inval acks have no delay 
          * therfore the directory can remove the sharer from the list
          * after the upgrade is issued. NAK will cause retry as GETX.
          */
         nextop = NEXTOP_NAK;
      } else if (dirState->numsharers > 1){
         StatsInc(COUNT_EXCLUSIVEONSHARED, mreq, 1);
         nextop = NEXTOP_INV;
         mreq->result = MEMSYS_RESULT_INVALIDATE|MEMSYS_RESULT_MEMORY;
         ASSERT(!dirState->pending);
         dirState->pending = 1;
      } else {
         nextop = NEXTOP_NONE;
      }
      break;

   case MEMSYS_UNCWRITE:
   case MEMSYS_UNCWRITE_ACCELERATED:
   case MEMSYS_UNCREAD:
      nextop = NEXTOP_MEM;
      break;

   case MEMSYS_WRITEBACK:
      nextop = NEXTOP_MEM;
      break;
         
   case MEMSYS_REPLACEMENT_HINT:
      nextop = NEXTOP_NONE;
      break;
         
   default:
      CPUError("Unknown memsys command (0x%x) in UmaCmd\n", mreq->cmd);
      nextop = NEXTOP_NONE; /* Quite compiler warning message */
   }

   /* Forward the request based on the next operation */
   switch (nextop) {
   case NEXTOP_NAK:
      mreq->status = MEMSYS_STATUS_NAK;
      StatsInc(COUNT_NAKS, mreq, 1);
      DirContDone(mreq->memnum, mreq);
      break;

   case NEXTOP_MEM:
      StatsInc(COUNT_MEMORYACCESS, mreq, 1);
      mreq->hdr.rout = MemContDone;
      MemContDelay(mreq->memnum, mreq);
      break;

   case NEXTOP_INV:
      DoInvalidates(mreq);
      break;

   case NEXTOP_NONE:
      DirContDone(mreq->memnum, mreq);
      break;

   }

   return;
}


/* Queue up for the memory controller */
static void
MemContDelay(int num, MemRequest *mreq) 
{
   MemState  *mState = memState[num];

   mreq->state = MEM_WAIT;
   if(List_IsEmpty(&mState->memoryQueue)) {
      EventDoCallback(num, mreq->hdr.rout, 
                      (EventCallbackHdr *) mreq, 
                      0, NUMA_MEM_TIME);
   }
   List_Insert(MREQ_TO_LIST(mreq), LIST_ATREAR(&mState->memoryQueue));

}


/* Memory controller delay done */
static void
MemContDone(int num, EventCallbackHdr *hdr, void *v) 
{
   MemRequest *mreq = (MemRequest *)hdr;
   MemState  *mState = memState[num];

   /* 
    * Remove request from list and schedule next callback
    */
   List_Remove(MREQ_TO_LIST(mreq));
   if (!List_IsEmpty(&mState->memoryQueue)) {
      MemRequest *newReq;
      /*
       * Start the next memory request. 
       */
      newReq = LIST_TO_MREQ(List_First(&(mState->memoryQueue)));
      EventDoCallback(num, newReq->hdr.rout, (EventCallbackHdr *) newReq, 0,
                      NUMA_MEM_TIME);
   }

   DirContDone(mreq->memnum, mreq);
   
}



/* Sharing writebacks queue up for the memory controller */
static void
SharingWBDelay(int num, InvalRequest *lreq) 
{
   MemState  *mState = memState[num];

   lreq->state = WB_MEM_WAIT;
   if(List_IsEmpty(&mState->memoryQueue)) {
      EventDoCallback(num, lreq->hdr.rout, 
                      (EventCallbackHdr *) lreq, 
                      0, NUMA_MEM_TIME);
   }
   List_Insert(INVAL_TO_LIST(lreq), LIST_ATREAR(&mState->memoryQueue));

}


/* Memory controller delay done for sharing writebacks */
static void
SharingWBDone(int num, EventCallbackHdr *hdr, void *v) 
{
   InvalRequest *lreq = (InvalRequest *)hdr;
   MemState  *mState = memState[num];

   /* 
    * Remove request from list and schedule next callback
    */
   List_Remove(INVAL_TO_LIST(lreq));
   if (!List_IsEmpty(&mState->memoryQueue)) {
      InvalRequest *newReq;
      /*
       * Start the next memory request. 
       */
      newReq = LIST_TO_INVAL(List_First(&(mState->memoryQueue)));
      EventDoCallback(num, newReq->hdr.rout, (EventCallbackHdr *) newReq, 0,
                      NUMA_MEM_TIME);
   }

   /* Sharing writeback done. Release the structure at this point */
   lreq->state = ALL_DONE;
   List_Insert(INVAL_TO_LIST(lreq), LIST_ATREAR(&freeInvalReqList));
}



/* 
 * Send the required invalidates or downgrades 
 * Local invalidates have only a local delay
 * Remote invalidates have a net + dc + local delay
 */
static void
DoInvalidates(MemRequest *mreq) 
{
   DirState *dirState = mreq->dirState;
   int i;
   List_Links *itemPtr;
   InvalRequest *lreq;

   dirState->acks = 0;
   mreq->state = INVAL_WAIT;
   
   for (i = 0; i < TOTAL_CPUS; i++) {
      if (i == mreq->cpunum && mreq->transId>=0) {
         /* Requesting CPU */
         dirState->acks++;
      } else if (!DirIsASharer(dirState, i)) {
         /* 
          * Not a sharer
          * MemStat must still know of invalidations
          */
         CacheFakeInvalidate(i, BACKMAP_PADDR(mreq->addr, i), SCACHE_LINE_SIZE);
         dirState->acks++;
      } else {
         itemPtr = List_First(&freeInvalReqList);
         ASSERT(itemPtr != &freeInvalReqList);
         List_Remove(itemPtr);
         lreq = LIST_TO_INVAL(itemPtr);
         lreq->cpu = i;
         lreq->mreq = mreq;

         if (NumaLocalMem(i) != mreq->memnum) {
            /* CPU to be invalidated is remote */
            lreq->state = INVAL_NET_WAIT;
            EventDoCallback(mreq->cpunum, InvalRequestNet, (EventCallbackHdr *) lreq, 0,
                            NUMA_NET_TIME);
         } else {
            /* CPU to be invalidated is local */
            lreq->state = INVAL_LOCAL_WAIT;
            EventDoCallback(mreq->cpunum, InvalCollectAcks, (EventCallbackHdr *) lreq, 0,
                            NUMA_BUS_TIME);
         }
      }
   }
   StatsInc(COUNT_INVALORDNGRADESENT, mreq, (TOTAL_CPUS - dirState->acks));

   /* Make sure that there is at least 1 outstanding inval */
   ASSERT(dirState->acks != TOTAL_CPUS);
}
   
 
/* 
 * net delay done for invalidations that go remote
 * next step is the DC delay
 */
static void
InvalRequestNet(int num, EventCallbackHdr *hdr, void *v) 
{
   InvalRequest *lreq = (InvalRequest *)hdr;
   int memnum = NumaLocalMem(lreq->cpu);
   MemState  *mState = memState[memnum];

   lreq->hdr.rout = InvalRemoteDC;
   lreq->state = INVAL_REMOTE_DC_WAIT;
   lreq->dc_delay_time = NUMA_NILOCAL_DC_TIME;
   if(List_IsEmpty(&mState->localQueue)) {
      EventDoCallback(memnum, lreq->hdr.rout, 
                      (EventCallbackHdr *) lreq, 
                      0, lreq->dc_delay_time);
   }
   List_Insert(INVAL_TO_LIST(lreq), LIST_ATREAR(&mState->localQueue));
}
   

/* 
 * DC delay done for remote invals
 * Next do a local delay
 */
static void
InvalRemoteDC(int num, EventCallbackHdr *hdr, void *v) 
{
   InvalRequest *lreq = (InvalRequest *)hdr;
   int memnum = NumaLocalMem(lreq->cpu);
   MemState  *mState = memState[memnum];

   /* 
    * Remove request from list and schedule next callback
    */
   List_Remove(INVAL_TO_LIST(lreq));
   if (!List_IsEmpty(&mState->localQueue)) {
      InvalRequest *newReq;
      /*
       * Start the next memory request. 
       */
      newReq = LIST_TO_INVAL(List_First(&(mState->localQueue)));
      EventDoCallback(num, newReq->hdr.rout, (EventCallbackHdr *) newReq, 0,
                      lreq->dc_delay_time);
   }


   lreq->state = INVAL_REMOTE_LOCAL_WAIT;
   EventDoCallback(num, InvalAckNet, (EventCallbackHdr *) lreq, 0,
                      NUMA_BUS_TIME);
  
}

/*
 * Cache has been invalidated. Now send the ack to home
 */
static void
InvalAckNet(int num, EventCallbackHdr *hdr, void *v) 
{
   InvalRequest *lreq = (InvalRequest *)hdr;

   lreq->state = INVAL_ACK_NET_WAIT;
   EventDoCallback(num, InvalHomeDC, (EventCallbackHdr *) lreq, 0,
                      NUMA_NET_TIME);
}


/*
 * Do a DC delay for the inval acks returning to the home DC
 */
static void
InvalHomeDC(int num, EventCallbackHdr *hdr, void *v) 
{
   InvalRequest *lreq = (InvalRequest *)hdr;
   int memnum = lreq->mreq->memnum;
   MemState  *mState = memState[memnum];

   lreq->hdr.rout = InvalRemoteAckDone;
   lreq->state = INVAL_HOME_DC_WAIT;
   lreq->dc_delay_time = NUMA_NIREMOTE_DC_TIME;
   if(List_IsEmpty(&mState->localQueue)) {
      EventDoCallback(memnum, lreq->hdr.rout, 
                      (EventCallbackHdr *) lreq, 
                      0, lreq->dc_delay_time);
   }
   List_Insert(INVAL_TO_LIST(lreq), LIST_ATREAR(&mState->localQueue));
}



/* 
 * Remote ack received. Schedule next callback in DC queue
 * and pass this one on to InvalCollectAcks
 */
static void
InvalRemoteAckDone(int num, EventCallbackHdr *hdr, void *v) 
{

   InvalRequest *lreq = (InvalRequest *)hdr;
   int memnum = lreq->mreq->memnum;
   MemState  *mState = memState[memnum];

   /* 
    * Remove request from list and schedule next callback
    */
   List_Remove(INVAL_TO_LIST(lreq));
   if (!List_IsEmpty(&mState->localQueue)) {
      InvalRequest *newReq;
      /*
       * Start the next memory request. 
       */
      newReq = LIST_TO_INVAL(List_First(&(mState->localQueue)));
      EventDoCallback(num, newReq->hdr.rout, (EventCallbackHdr *) newReq, 0,
                      newReq->dc_delay_time);
   }

   InvalCollectAcks(memnum, (EventCallbackHdr *)lreq, 0);

}

/*
 * Invalidation acks for a cache line are counted here
 * When they are all received, we move on.
 * The invals could have crossed writebacks along the way.
 * If this happens, the request is NAKed.
 */
static void
InvalCollectAcks(int num, EventCallbackHdr *hdr, void *v) 
{
   InvalRequest *lreq = (InvalRequest *)hdr;
   int invalCpu = lreq->cpu;
   MemRequest *mreq = lreq->mreq;
   DirState *dirState = mreq->dirState;
   byte *data;
   unsigned wbNeeded = dirState->dirty;
   int wasDirty;

   
   data = (byte*) DATA_ADDR(mreq->machnum, mreq->addr);
   /* 
    * The following check should actually be done at the remote DC, 
    * But since we have the pending flag on the directory controller,
    * it should not matter. This does increase the window for the Inval
    * to cross the writeback.
    */

   if (mreq->mode == MEMSYS_EXCLUSIVE) { 
      if (CacheExtract(invalCpu, 
                       BACKMAP_PADDR(mreq->addr, invalCpu), 
                       SCACHE_LINE_SIZE, &wasDirty, data)) {
         /* Clear only if successful */
         DirClearASharer(dirState,invalCpu); 
      } else {
         wbNeeded = 0;
         /* Writeback or replacement hint in flight, NAK this request */
         mreq->status = MEMSYS_STATUS_NAK;
         StatsInc(COUNT_NAKS, mreq, 1);
      }
   } else {
      if (CacheWriteback(invalCpu, 
                         BACKMAP_PADDR(mreq->addr, invalCpu), 
                         SCACHE_LINE_SIZE, data)) {
         /* 
          * This line could have been replaced while we were waiting for the ack
          * Set it to be a sharer only if the return values says so
          */
         DirSetASharer(dirState,invalCpu,0); 
      } else {
          wbNeeded = 0;
          /* Writeback in flight, NAK this request */
          mreq->status = MEMSYS_STATUS_NAK;
          StatsInc(COUNT_NAKS, mreq, 1);
      }
   }

   if (wbNeeded) {
      /* need a sharing writeback to memory. Use the current Inval structure */
      lreq->hdr.rout = SharingWBDone;
      SharingWBDelay(mreq->memnum, lreq);
      if (ENABLE_COUNT)
         MigRepDoWriteCounter(mreq->reqAddr, mreq->memnum);
   } else {
      /* Put this inval structure back */
      lreq->state = ALL_DONE;
      List_Insert(INVAL_TO_LIST(lreq), LIST_ATREAR(&freeInvalReqList));
   }

   if (++(dirState->acks) == TOTAL_CPUS) {
      /* all acks are in, go to the next step */
      mreq->state = IN_MEM_DC;
      if (mreq->status == MEMSYS_STATUS_NAK) {
         /* this one got NAKed during inval, turn off the pending bit */
         mreq->dirState->pending = 0;
      }
      DirContDone(mreq->memnum, mreq);
   }
}


/*
 * All directory processing is over at this point
 * Send reply, local or remote
 */
static void
DirContDone(int num, MemRequest *mreq) 
{
   DirState *dirState = mreq->dirState;

   /*
    * DMA Data handling
    */
   if( mreq->cmd&MEMSYS_DMAFLAVOR ) { 
      byte *memAddr = (byte*)DATA_ADDR(mreq->machnum, mreq->reqAddr);
      if( mreq->cmd&MEMSYS_GETX ) {
         bcopy(mreq->data,memAddr,mreq->len);
      } else { 
         bcopy(memAddr,mreq->data,mreq->len);
      }
   }
   
   /* Update directory state */
   if (mreq->status != MEMSYS_STATUS_NAK) { 
      switch (mreq->cmd & MEMSYS_CMDMASK) {
      case MEMSYS_GET:
         dirState->pending = 0;
         if (!(mreq->cmd&MEMSYS_DMAFLAVOR)) {
            DirSetASharer(dirState, mreq->cpunum, 0);
         }
         if (ENABLE_COUNT)
            MigRepDoCmissCounter(mreq);
         break;

      case MEMSYS_GETX:
      case MEMSYS_UPGRADE:
         dirState->pending = 0;
         if (!(mreq->cmd&MEMSYS_DMAFLAVOR)) {
            DirSetASharer(dirState, mreq->cpunum, 1);
         }
         if (ENABLE_COUNT)
            MigRepDoCmissCounter(mreq);
         break;

      case MEMSYS_UNCWRITE:
      case MEMSYS_UNCWRITE_ACCELERATED:
      case MEMSYS_UNCREAD:
         break;

      case MEMSYS_WRITEBACK:
         /*
          * End of the road
          * update memory if data handling
          */
         if (mreq->data) { 
            byte *memAddr = (byte*)DATA_ADDR(mreq->machnum, mreq->replacedPaddr);
            bcopy(mreq->data, memAddr, SCACHE_LINE_SIZE);
            free(mreq->data);
         }
         DirClearASharer(dirState, mreq->cpunum);
         List_Insert(MREQ_TO_LIST(mreq), LIST_ATREAR(&freeMemReqList));
         mreq->drainNeeded = 0;
#ifdef MIG_REP_WBACK
         if (ENABLE_COUNT)
            MigRepDoWriteCounter(mreq->replacedPaddr, mreq->memnum);
#endif         
         return;

      case MEMSYS_REPLACEMENT_HINT:
         /*
          * End of the road
          */
         DirClearASharer(dirState, mreq->cpunum);
         List_Insert(MREQ_TO_LIST(mreq), LIST_ATREAR(&freeMemReqList));
         mreq->drainNeeded = 0;
         return;

      default:
         CPUError("Unknown memsys command (0x%x) in UmaCmd\n", mreq->cmd);
      }
   }

   /* Send this one on its way */
   if (mreq->memnum == mreq->localMemNum) {
      mreq->state = REPLY_LOCAL_WAIT;
      EventDoCallback(mreq->cpunum, SendReplyToCPU, 
                      (EventCallbackHdr *) mreq, 
                      0, NUMA_BUS_TIME);
   } else {
      mreq->state = REPLY_NET_WAIT;
      EventDoCallback(mreq->cpunum, ReplyNet, 
                      (EventCallbackHdr *) mreq, 
                      0, NUMA_NET_TIME);
   }
}

/*
 * Network delay done for the reply
 * Send it to the requesting DC
 */
static void
ReplyNet(int num, EventCallbackHdr *hdr, void *v)
{
   MemRequest *mreq = (MemRequest *)hdr;

   mreq->hdr.rout = ReplyDC;
   mreq->state = REPLY_LOCAL_DC_WAIT;
   mreq->dc_delay_time = NUMA_NIREMOTE_DC_TIME;

   DoDCDelay(mreq->localMemNum, mreq);
}


/*
 * Requesting DC is done
 * Do a local delay
 */ 
static void
ReplyDC(int num, EventCallbackHdr *hdr, void *v)
{
   MemRequest *mreq = (MemRequest *)hdr;
   MemState  *mState = memState[mreq->localMemNum];

   /* 
    * Remove request from list and schedule next callback
    */
   List_Remove(MREQ_TO_LIST(mreq));
   if (!List_IsEmpty(&mState->localQueue)) {
      MemRequest *newReq;
      /*
       * Start the next memory request. 
       */
      newReq = LIST_TO_MREQ(List_First(&(mState->localQueue)));
      EventDoCallback(num, newReq->hdr.rout, (EventCallbackHdr *) newReq, 0,
                      newReq->dc_delay_time);
   } 

   mreq->state = REPLY_LOCAL_WAIT;
   EventDoCallback(mreq->cpunum, SendReplyToCPU, (EventCallbackHdr *) mreq, 0,
                      NUMA_BUS_TIME);
 
}


/* 
 * All done. update the caches
 */
static void
SendReplyToCPU(int cpuNum, EventCallbackHdr *hdr, void *v)
{
   MemRequest *mreq = (MemRequest *)hdr;
   MemState  *mState = memState[mreq->memnum];
   StatsHist *hist;
   int isLocal = NumaIsLocal(mreq->cpunum, mreq->memnum) ? 1 : 0;
   byte* data;
   uint difftime = CPUVec.CycleCount(mreq->cpunum) - mreq->starttime;

   /* 
    * DMA requests are not necessarily aligned!!!
    */
   if( mreq->cmd & MEMSYS_DMAFLAVOR ) {
      data = (byte*)DATA_ADDR(mreq->machnum, mreq->reqAddr);
   } else { 
      data = (byte*)DATA_ADDR(mreq->machnum, mreq->addr);
   }

   hist = &(mState->stats.reqtime);
   StatsIncHist(hist, difftime);
   hist = &(globalStats[isLocal].reqtime);
   StatsIncHist(hist, difftime);
   if (mreq->memnum != mreq->localMemNum) {
      mreq->result |= MEMSYS_RESULT_REMOTE_HOME;
   }

   if (mreq->mode == MEMSYS_UNCACHED) {
      if ((mreq->cmd & MEMSYS_CMDMASK) == MEMSYS_UNCREAD) {
         void *from = (void*)DATA_ADDR(mreq->machnum, (uint)mreq->reqAddr);
         bcopy(from, mreq->data, mreq->len);
         /* Unstall the processor */
         ASSERT(CPUVec.ReissueUncachedOp);
         CPUVec.ReissueUncachedOp(mreq->cpunum);
      } else {
         void *to = (void*)DATA_ADDR(mreq->machnum, (uint)mreq->reqAddr);
         bcopy(mreq->data, to, mreq->len);
      }         

   } else {
      CacheCmdDone(mreq->cpunum, mreq->transId, mreq->mode, mreq->status, 
                   mreq->result, data);
   }

   mreq->state = ALL_DONE;
   List_Insert(MREQ_TO_LIST(mreq), LIST_ATREAR(&freeMemReqList));

}


/* Update the stats on a new request */
static void
UpdateRequestStats(MemRequest *mreq)
{

   switch (mreq->cmd & MEMSYS_CMDMASK) {
   case MEMSYS_GET:
      if (mreq->cmd & MEMSYS_DMAFLAVOR) {
         StatsInc(COUNT_DMAGETS, mreq, 1);
      } else {
         if (mreq->cmd & MEMSYS_IFFLAVOR) {
            StatsInc(COUNT_IGETS, mreq, 1);
         } else if (mreq->cmd & MEMSYS_LLFLAVOR) {
            StatsInc(COUNT_LLGETS, mreq, 1);
         } else {
            StatsInc(COUNT_DGETS, mreq, 1);
         }

         if (mreq->replacedPaddr != MEMSYS_NOADDR) {
            if (mreq->replaceWasDirty) {
               StatsInc(COUNT_WRITEBACKS, mreq, 1);
            } else {
               StatsInc(COUNT_REPLHINTS, mreq, 1);
            }
         }
      }
      break;

   case MEMSYS_GETX:
      if (mreq->cmd & MEMSYS_DMAFLAVOR) {
         StatsInc(COUNT_DMAGETXS, mreq, 1);
      } else {
         StatsInc(COUNT_GETXS, mreq, 1);

         if (mreq->replacedPaddr != MEMSYS_NOADDR) {
            if (mreq->replaceWasDirty) {
               StatsInc(COUNT_WRITEBACKS, mreq, 1);
            } else {
               StatsInc(COUNT_REPLHINTS, mreq, 1);
            }
         }
      }
      break;

   case MEMSYS_UPGRADE:
      if (mreq->cmd & MEMSYS_SCFLAVOR) {
         StatsInc(COUNT_SCUPGRADES, mreq, 1);
      } else {
         StatsInc(COUNT_UPGRADES, mreq, 1);
      }
      break;

   case MEMSYS_UNCWRITE:
      StatsInc(COUNT_UNCACHEDWRITES, mreq, 1);
      break;

   case MEMSYS_UNCWRITE_ACCELERATED:
      StatsInc(COUNT_UNCACHEDACCWRITES, mreq, 1);
      break;
         
   case MEMSYS_UNCREAD:
      StatsInc(COUNT_UNCACHEDREADS, mreq, 1);
      break;
   
   case MEMSYS_REPLACEMENT_HINT:
      StatsInc(COUNT_REPLHINTS, mreq, 1);
      break;

   case MEMSYS_WRITEBACK:
      StatsInc(COUNT_WRITEBACKS, mreq, 1);
      break;

   default:
      CPUError("Unknown memsys command (0x%x) in UmaCmd\n", mreq->cmd);
   }
}



/*****************************************************************
 * MemsysInit
 *****************************************************************/
void 
NumaInit(void)
{
   int i;
   u_long lines_per_mem, size;

   /* 
    * Initialize the pointers to functions and delay parameters
    */
   memsysVec.type = NUMA;
   memsysVec.NoMemoryDelay = 0;
   memsysVec.MemsysCmd = NumaCmd;
   memsysVec.MemsysDumpStats = NumaDumpStats;
   memsysVec.MemsysDone = NumaDone;
   memsysVec.MemsysStatus = NumaStatus;
#ifndef SOLO
   memsysVec.MemsysDrain = NumaDrain;
   memsysVec.MemsysSetRemap = NumaSetRemap;
   memsysVec.MemsysControlRemap = NumaControlRemap;
   memsysVec.MemsysGetNodeAddress = NumaGetNodeAddress;
#endif
 
   CPUPrint("MEM num igets llgets dmagets dgets dmagetxs getxs scupgrades upgrades writebacks replhints uncachedwrites uncachedaccwrites uncachedreads invalordngradesent naks remotedirty exclusiveonshared memoryaccess misscount totlatency\n");
   /* 
    * Initialize the list of request structures
    */
   List_Init(&freeMemReqList);
   
   for (i = 0; i < MAX_OUTSTANDING; i++) { 
      MemRequest *mreq = MemRequestStorage + i;
      bzero((char *) (mreq), sizeof(MemRequestStorage[0]));
      List_InitElement(MREQ_TO_LIST(mreq));
      List_Insert(MREQ_TO_LIST(mreq), LIST_ATREAR(&freeMemReqList));
   }

   /* 
    * Initialize the list of Invalidate structures
    */
   List_Init(&freeInvalReqList);
   
   for (i = 0; i < MAX_INVALS; i++) { 
      InvalRequest *lreq = InvalRequestStorage + i;
      bzero((char *) (lreq), sizeof(InvalRequestStorage[0]));
      List_InitElement(INVAL_TO_LIST(lreq));
      List_Insert(INVAL_TO_LIST(lreq), LIST_ATREAR(&freeInvalReqList));
   }

   /*
    * Allocate and initialize the directory state for each memory,
    * the stats per memory and the overall stats.
    */
   lines_per_mem = MEM_SIZE(0) / SCACHE_LINE_SIZE;
   size = sizeof(MemState)+ (sizeof(DirState)*lines_per_mem);   

   for (i = 0; i < NUM_MEMORIES(0); i++) { 
      if (!numaDoneInit) {
         memState[i] = (MemState *) ZMALLOC(size, "memState");
      }
      ASSERT(memState[i]);
      List_Init(&memState[i]->memoryQueue);
      List_Init(&memState[i]->localQueue);
      bzero((char *) &memState[i]->dirState, sizeof(DirState)*lines_per_mem);
      bzero((char *) &memState[i]->stats, sizeof(NumaStats));      
   }
   bzero((char *) globalStats, 2*sizeof(NumaStats));
#ifndef SOLO
   NumaInitRemap();

   if (NUMA_STRIPE_SIZE < 0) {
      numaStripeSizePages = MEM_SIZE(0)/PAGE_SIZE/NUM_MEMORIES(0);
   } else {
      numaStripeSizePages = NUMA_STRIPE_SIZE;
   }
   numaStripeSizeBytes = numaStripeSizePages*PAGE_SIZE;
   numaStripeSizeLines = numaStripeSizeBytes/SCACHE_LINE_SIZE;
   numaStripeChunk = numaStripeSizeBytes*NUM_MEMORIES(0);

   ASSERT(NUMA_STRIPE_SIZE < 0);
   if (ENABLE_COUNT)
      MigRepNumaInit();
#endif
   numaDoneInit = 1;
}

/*****************************************************************
 * MemsysCmd
 * 
 * Main interface from secondary cache into here
 * For DMA opeartions, the writeback parameter holds the
 * length of the DMA transfer and is NOT a boolean.
 *****************************************************************/

Result
NumaCmd(int cpuNum, int cmd, PA addr, int transId, 
	PA replacedPaddr, int writeback, byte *data)
{
   MemState  *mState;
   MemRequest *mreq = NULL;
   List_Links *itemPtr;
   int memnum;
   int stall = 1;

      
#ifndef SOLO
   addr = REMAP_PADDR(addr, cpuNum);
   replacedPaddr = REMAP_PADDR(replacedPaddr, cpuNum);
#endif 

   if (cmd == MEMSYS_SYNC) {
      return SUCCESS;
   } 

   if ((cmd == MEMSYS_GET) && (transId == -1)) {
      /* 
       * Ugly hack for Cache instructions. Note the DMAFLAVOR also 
       * uses negative transids too. However it sets MEMSYS_DMAFLAVOR
       * Should be fixed by having MIPSY use the cmds MEMSYS_WRITEBACK/REPLHINT
       * Original comment attached:
       * Handle special case: a GET with transId -1 indicates
       * a writeback or repl hint without the associated GET (due
       * to a CACHE instruction executed in the processor)
       */
      if (writeback) {
         cmd = MEMSYS_WRITEBACK;
      } else {
         cmd = MEMSYS_REPLACEMENT_HINT;
      }
   }
  

   /* A number of cases do not need an mreq allocated */
   switch (cmd & MEMSYS_CMDMASK) {
   case MEMSYS_GET:
   case MEMSYS_GETX:
   case MEMSYS_UPGRADE:
   memnum = NumaAddrToMemnum(addr);
   mState = memState[memnum];
   /*
    * Got a request. Allocate and fill in a memrequest
    */
   itemPtr = List_First(&freeMemReqList);
   ASSERT(itemPtr != &freeMemReqList);
   List_Remove(itemPtr);
   mreq = LIST_TO_MREQ(itemPtr);
   mreq->cmd = cmd;
   mreq->transId = transId;
   mreq->reqAddr = addr;
   mreq->addr =  addr & ~(SCACHE_LINE_SIZE-1);
   mreq->data = data;
   mreq->len = writeback;
   if( cmd & MEMSYS_DMAFLAVOR ) {
      ASSERT( transId < 0 );
      ASSERT( data );
      mreq->replaceWasDirty  = 0;
   } else {
      mreq->replaceWasDirty = writeback;
   }
   mreq->replacedPaddr = replacedPaddr;
   mreq->status = MEMSYS_STATUS_SUCCESS;
   mreq->memnum = memnum;
   mreq->machnum = M_FROM_CPU(cpuNum);
   mreq->cpunum = cpuNum;
   mreq->localMemNum = NumaLocalMem(cpuNum);
   mreq->dirState = &mState->dirState[NumaAddrToMemline(addr)];
   mreq->result = MEMSYS_RESULT_MEMORY|MEMSYS_RESULT_NOTRANSITION;

   UpdateRequestStats(mreq);
      break;
   default:
      break;
   }

   switch (cmd & MEMSYS_CMDMASK) {
   case MEMSYS_GET:
      
      mreq->mode = MEMSYS_SHARED;
      break;

   case MEMSYS_GETX:
      mreq->mode = MEMSYS_EXCLUSIVE;
      break;

   case MEMSYS_UPGRADE:
      mreq->mode = MEMSYS_EXCLUSIVE;
      break;

   case MEMSYS_UNCWRITE:
   case MEMSYS_UNCWRITE_ACCELERATED:
      /* Currently uncached write return with no delay,
       */
      globalStats[1].counts[MEMSYS_UNCWRITE]++;
#ifndef SOLO
      {
         Result ret;
         ASSERT(CPUVec.UncachedPIO);
         ret = CPUVec.UncachedPIO(cpuNum, addr, 0, writeback, data);
         ASSERT(ret == SUCCESS);
         return SUCCESS;
      }
      
#else
      stall = 0;
      break;
#endif
   
   case MEMSYS_UNCREAD:
      /* Data handling is done in SendReplyToCPU for non-KSEG1 */
      globalStats[1].counts[MEMSYS_UNCREAD]++;
#ifndef SOLO
      {
         Result ret;
         ASSERT(CPUVec.UncachedPIO);
         ret = CPUVec.UncachedPIO(cpuNum, addr, 1, writeback, data);
         ASSERT(ret == SUCCESS);
         return SUCCESS;
      }
#else
      break;
#endif  
   case MEMSYS_WRITEBACK:
   case MEMSYS_REPLACEMENT_HINT:
      stall = 0;
      break;

   default:
      CPUError("Unknown memsys command (0x%x) for memsys Numa\n", cmd);
   }

   /*
    * Handle replacement hints and writebacks here.
    */
   if (replacedPaddr != MEMSYS_NOADDR) {
      int rmemnum = NumaAddrToMemnum(replacedPaddr);
      DirState *dir = &memState[rmemnum]->dirState[NumaAddrToMemline(replacedPaddr)];
      MemRequest *wreq;
      /*
       * Allocate and fill in a memrequest for the writeback
       */
      itemPtr = List_First(&freeMemReqList);
      ASSERT(itemPtr != &freeMemReqList);
      List_Remove(itemPtr);
      wreq = LIST_TO_MREQ(itemPtr);

      wreq->data = 0;
      if (writeback) {
         wreq->cmd = MEMSYS_WRITEBACK;
         if (data) {
            wreq->data = malloc(SCACHE_LINE_SIZE);
            bcopy(data, wreq->data, SCACHE_LINE_SIZE);
         }
      } else {
         wreq->cmd = MEMSYS_REPLACEMENT_HINT;
      }

      wreq->transId = transId;
      wreq->reqAddr = 0;
      wreq->addr =  0;
      wreq->len = 0;
      wreq->replaceWasDirty = writeback;
      wreq->replacedPaddr = replacedPaddr;
      wreq->status = MEMSYS_STATUS_SUCCESS;
      wreq->memnum = rmemnum;
      wreq->machnum = M_FROM_CPU(cpuNum);
      wreq->cpunum = cpuNum;
      wreq->localMemNum = NumaLocalMem(cpuNum);
      wreq->dirState = dir;
      wreq->result = MEMSYS_RESULT_MEMORY|MEMSYS_RESULT_NOTRANSITION;
      wreq->drainNeeded = 1;
      UpdateRequestStats(wreq);

      /* 
       * add 1 cycle to this to ensure that it does not get ahead of the original
       * request through the DC queue 
       */
      EventDoCallback(wreq->cpunum, RequestLocal, (EventCallbackHdr *) wreq, 0,
                      NUMA_BUS_TIME + 1);
   }

   if (stall) {
      /*
       * Queue us for the Local delay
       */
      mreq->starttime = CPUVec.CycleCount(mreq->cpunum);
      mreq->hdr.rout = RequestLocal;
      mreq->state = REQ_LOCAL_WAIT;
      EventDoCallback(mreq->cpunum, RequestLocal, (EventCallbackHdr *) mreq, 0,
                      NUMA_BUS_TIME);
      return STALL; 
   } else {
      return SUCCESS;
   }
}

void 
NumaDone(void)
{
   CPUPrint("Exiting the NUMA memory system\n");
}

/* Dump stats about the memory system */
void
NumaDumpStats(void)
{

   int i, j, isLocal;

   for (i = 0; i < NUM_MEMORIES(0); i++) {
      CPUPrint("MEM %d", i);
      for (j=0; j< COUNT_TOTAL; j++) {
         CPUPrint(" %lld", memState[i]->stats.counts[j]);
      }
      CPUPrint(" %lld %lld\n", memState[i]->stats.reqtime.count, memState[i]->stats.reqtime.sum);
   }

   isLocal = 1;
   CPUPrint("MEM LOCAL");
   for (j=0; j< COUNT_TOTAL; j++) {
      CPUPrint(" %lld", globalStats[isLocal].counts[j]);
   }
   CPUPrint(" %lld %lld\n", globalStats[isLocal].reqtime.count, globalStats[isLocal].reqtime.sum);
   
   isLocal = 0;
   CPUPrint("MEM REMOTE");
   for (j=0; j< COUNT_TOTAL; j++) {
      CPUPrint(" %lld", globalStats[isLocal].counts[j]);
   }
   CPUPrint(" %lld %lld\n", globalStats[isLocal].reqtime.count, globalStats[isLocal].reqtime.sum);
}


void
NumaStatus(void) 
{
   NumaDumpStats();
}


/*
 * Currently only writebacks and replacement hints need to
 * be drained. The others will cause processors to be stalled
 * and the clock will be run forward till all processors are unstalled.
 * For writebacks with data handling, the data needs to be written back.
 */
void
NumaDrain(void)
{
   int i;

   for (i = 0; i < MAX_OUTSTANDING; i++) { 
      MemRequest *mreq = MemRequestStorage + i;
      
      if (mreq->drainNeeded) {
         EventCallbackRemove((EventCallbackHdr*)mreq);
      
         if ((mreq->cmd & MEMSYS_CMDMASK) == MEMSYS_WRITEBACK) {
            if (mreq->data) { 
               byte *memAddr = (byte*)DATA_ADDR(mreq->machnum, mreq->replacedPaddr);
               bcopy(mreq->data, memAddr, SCACHE_LINE_SIZE);
               free(mreq->data);
            }
         }
      }
   }
}

/* Cache miss counting code for migration/replication */

/* 
 * Resets a cache line worth of counters every time
 * Mirrors Flashlite implementation and cache-line size (128)
 */
#define CMISS_COUNTERS_PER_CACHELINE (128/NUM_CPUS(0))
#define WRITE_COUNTERS_PER_CACHELINE (128*8)

#define MIGREP_LOCAL_PAGENUM(_a) (((_a)/PAGE_SIZE)%numaStripeSizePages)

/* Init all the structures for counting cache misses */
static void
MigRepNumaInit(void)
{
   int i, j;
   unsigned long size;

   ASSERT(NUMA_STRIPE_SIZE < 0);
   
   for(i=0; i<NUM_MEMORIES(0);i++) {
      migRepInfo[i].sampleCounter = SAMPLE_COUNT;
      for (j=0; j<MIGREP_PEND; j++) {
         migRepInfo[i].pendingPages[j] = MIGREP_NULLPAGE;
      }
      migRepInfo[i].pendingPagesCount = 0;

      if (!numaDoneInit) {
         size = numaStripeSizePages*NUM_MEMORIES(0);
         migRepInfo[i].cmissCounter = (unsigned char *) MALLOC(size, "migRep");
         size = numaStripeSizePages/8;
         migRepInfo[i].writeCounter = (unsigned char *) ZMALLOC(size, "migRep");
      }

      size = numaStripeSizePages/8;
      bzero((char *) migRepInfo[i].writeCounter, size);
      size = numaStripeSizePages*NUM_MEMORIES(0);
      for (j=0; j<size; j++) {
         migRepInfo[i].cmissCounter[j] = TRIGGER_THRESHOLD;
      }
   }

   /* calculate the period for calling the counter reset routine */
   counterResetInterval = (unsigned long)
      NanoSecsToCycles(RESET_INTERVAL*1000*1000)
      / (numaStripeSizePages/CMISS_COUNTERS_PER_CACHELINE);

   /* Init the write counter countdown variable, i.e. 1 in
      resetWriteCounters invocations */

   resetWriteCounters = WRITE_COUNTERS_PER_CACHELINE
      / CMISS_COUNTERS_PER_CACHELINE;
   /* Set up the first callback */
   bzero((char *) (&resetCounterCB), sizeof(EventCallbackHdr));
   EventDoCallback(0, MigRepPeriodic, &resetCounterCB, 0, counterResetInterval);
}


/* mark the page as written */
static void
MigRepDoWriteCounter(unsigned long addr, int memnum)
{
   unsigned long localPageNum = MIGREP_LOCAL_PAGENUM(addr);
   unsigned long cindex = localPageNum/8;
   unsigned long cbit = localPageNum % 8;

   migRepInfo[memnum].writeCounter[cindex] |= (1<<cbit);
}



/* Increment cache miss count for the page on a sampled basis */
static void
MigRepDoCmissCounter(MemRequest *mreq)
{
   int memnum = mreq->memnum;
   int cpunum = mreq->cpunum;
   int localmemnum = mreq->localMemNum;
   
   if (!(--migRepInfo[memnum].sampleCounter)) {
      unsigned long localPageNum = MIGREP_LOCAL_PAGENUM(mreq->reqAddr);
      unsigned long cindex = localPageNum*NUM_CPUS(0) + localmemnum;

      /* If GETX or UPGRADE mark it as a write */
      if ((mreq->cmd == MEMSYS_GETX) || (mreq->cmd == MEMSYS_UPGRADE)) {
         MigRepDoWriteCounter(mreq->reqAddr, memnum);
         if (ZERO_ON_WRITE) {
            int i;
            unsigned long ci;
            ASSERT(NUM_MEMORIES(0) <= 16);
            for(i=0; i<NUM_MEMORIES(0); i++) {
               if (i != localmemnum) {
                  ci = localPageNum*NUM_CPUS(0) + i;
                  migRepInfo[memnum].cmissCounter[ci] = TRIGGER_THRESHOLD;
               }
            }
         }
      }

      /* Only decrement if it has not hit the trigger */
      if(migRepInfo[memnum].cmissCounter[cindex]) {
         if(!(--migRepInfo[memnum].cmissCounter[cindex])) {
            /* This page is officially hot */
            if ((PAGE_NUM(mreq->reqAddr) > MAX_KERN) && (localmemnum != memnum)) {
               /* 
                * Interrupt only if it is not a static kernel page
                * and it is not a local page
                */
               migRepHotPage(localPageNum, cpunum, memnum, localmemnum);
            }
         }
      }

      migRepInfo[memnum].sampleCounter = SAMPLE_COUNT;
   }
}



/* Add hot page to list of pending pages and potentially interrupt the CPU */
static void
migRepHotPage(unsigned long localPageNum, int cpunum, int memnum, int localmemnum)
{
   int i, cputointr;
   unsigned long page;
   int qnode;
   
   if (INTR_HOT) {
      cputointr = cpunum;
      qnode = localmemnum;
      page = localPageNum + numaStripeSizePages*memnum;
   } else {
      cputointr = NumaMemFirstCPU(memnum);
      qnode = memnum;
      page = localPageNum;
   }
    
   for (i=0; i<MIGREP_PEND; i++) {
      if (migRepInfo[qnode].pendingPages[i] == MIGREP_NULLPAGE) {
         migRepInfo[qnode].pendingPages[i] = page;
         if (++migRepInfo[qnode].pendingPagesCount >= MIGREP_PEND_INTR) {
            /* Interrupt the CPU, enough pending pages */
            CPUVec.MigRepStart(cputointr);
         }
         break;
      } else if (migRepInfo[qnode].pendingPages[i] == page) {
         /* page already there as hot. Ignore! */
         break;
      }
   }
   if (i == MIGREP_PEND)
      CPUWarning("NUMA WARNING: pending page count exceeded on %d\n", qnode);
/*   ASSERT(i != MIGREP_PEND); */
}


/*
 * Called from the kernel through simmagic.
 * Returns a pending page or 0 if no pages
 * Resets the interrupt when all pages are gone
 */
uint64
MigRepGetHotPage(int cpunum)
{
   int i, localmemnum;
   unsigned long page;
   
   localmemnum = NumaLocalMem(cpunum);

   page = MIGREP_NULLPAGE;
   for (i=0; i<MIGREP_PEND; i++) {
      if (migRepInfo[localmemnum].pendingPages[i] != MIGREP_NULLPAGE) {
         page = migRepInfo[localmemnum].pendingPages[i];
         migRepInfo[localmemnum].pendingPages[i] = MIGREP_NULLPAGE;
         migRepInfo[localmemnum].pendingPagesCount--;
         break;
      }
   }

   if (!migRepInfo[localmemnum].pendingPagesCount) {
      /* All pages done, reset the interrupt */
      CPUVec.MigRepEnd(cpunum);
   }

   return ((uint64)page);
}


/*
 * Called from the kernel through simmagic.
 * Returns the requested counter value as int64
 * This is because flashlite does it this way as a int64
 * Bit 19 of the address (20 active bits) is used to demux cache and write counters
 * Bottom 3 bits cannot be used.
 */
uint64
MigRepGetInfo(unsigned long addr, int memnum, unsigned long countType, 
              unsigned long countAddr)
{
   int i;
   uint64 counterVal = 0;
   unsigned long bitVal;
   uint64 dummy;
   unsigned long cindex, cbit;
   unsigned char *counter;

   if (!(ENABLE_COUNT)) {
      return ((uint64) 0);
   }

   if (countType) {
      /* Write counter */
      cindex = countAddr/8;
      cbit = countAddr%8;
      bitVal = (migRepInfo[memnum].writeCounter[cindex])&(1<<cbit);
      if (bitVal) {
         cbit = countAddr%64;
         counterVal = ((uint64) 1) << cbit;
      }
   } else {
      /* cache miss counter */
      cindex = countAddr*NUM_MEMORIES(0);
      counter = &(migRepInfo[memnum].cmissCounter[cindex]);
      if (FOUR_BIT_COUNTERS) {
         ASSERT(NUM_MEMORIES(0) <= 16);
         ASSERT(NUM_MACHINES == 1);
         for(i=0; i<NUM_MEMORIES(0); i++) {
            dummy =  counter[i];
            ASSERT(dummy < 16);
            counterVal |= ((dummy & 0xf) << (i*4));
         }
      } else {
         ASSERT(NUM_MEMORIES(0) <= 8);
         ASSERT(NUM_MACHINES == 1);
         for(i=0; i<NUM_MEMORIES(0); i++) {
            dummy =  counter[i];
            ASSERT(dummy < 256);
            counterVal |= ((dummy & 0xff) << (i*8));
         }
      }
   }
   return (counterVal);
}



/*
 * Called from the kernel through simmagic.
 * Sets the requested counter value as int64
 * This is because flashlite does it this way as a int64
 * Bit 19 of the address (20 active bits) is used to demux cache and write counters
 * Bottom 3 bits cannot be used.
 */
void
MigRepSetInfo(unsigned long addr, int memnum, uint64 val, 
              unsigned long countType, unsigned long countAddr)
{
   int i;
   unsigned long cindex, cbit, bitVal;
   unsigned char *counter;

   if (!(ENABLE_COUNT)) {
      return ;
   }

   if (countType) {
      /* Write counter */
      cindex = countAddr/8;
      cbit = countAddr%8;
      bitVal = 1 << cbit;
      if (val) {
         migRepInfo[memnum].writeCounter[cindex] |= bitVal;
      } else {
         migRepInfo[memnum].writeCounter[cindex] &= ~(bitVal);
      }
   } else {
      /* cache miss counter */
      cindex = countAddr*NUM_MEMORIES(0);
      counter = &(migRepInfo[memnum].cmissCounter[cindex]);
      if (FOUR_BIT_COUNTERS) {
         ASSERT(NUM_MEMORIES(0) <= 16);
         ASSERT(NUM_MACHINES == 1);
         for(i=0; i<NUM_MEMORIES(0); i++) {
            counter[i] = (char) ((val >> (i*4)) & 0xf);
            /* counter[i] = (char) ((val >> (i*8)) & 0x0ff); */
         }
      } else {
         ASSERT(NUM_MEMORIES(0) <= 8);
         ASSERT(NUM_MACHINES == 1);
         for(i=0; i<NUM_MEMORIES(0); i++) {
            counter[i] = (char) ((val >> (i*8)) & 0x0ff);
         }
      }
   }

}


/*
 * Called periodically to reset some miss counters
 * Each time through a MAGIC cache line (128 bytes) of cache-miss counters are reset
 * When resetWriteCounters goes to zero 128 bytes of write counters are reset.
 * Uses the callback mechanism
 */
static void
MigRepPeriodic(int cpuNum, EventCallbackHdr *hdr, void *v)
{
   int i, j, k;
   unsigned long nextResetIndex = cmissResetIndex + CMISS_COUNTERS_PER_CACHELINE;
   unsigned long countersToReset = CMISS_COUNTERS_PER_CACHELINE;
   unsigned char *counter;

   if (nextResetIndex >= numaStripeSizePages) {
      countersToReset = CMISS_COUNTERS_PER_CACHELINE - (nextResetIndex - numaStripeSizePages);
      nextResetIndex = 0;
   }

   for (k=0; k<NUM_MEMORIES(0); k++) {
      counter = &(migRepInfo[k].cmissCounter[cmissResetIndex]);
      for (i=0; i<countersToReset; i++) {
         for (j=0; j<TOTAL_CPUS; j++, counter++) {
            *counter = TRIGGER_THRESHOLD;
         }
      }
   }
   cmissResetIndex = nextResetIndex;

   if (!(--resetWriteCounters)) {
      resetWriteCounters = WRITE_COUNTERS_PER_CACHELINE/CMISS_COUNTERS_PER_CACHELINE;

      nextResetIndex = writeResetIndex + WRITE_COUNTERS_PER_CACHELINE;
      countersToReset = WRITE_COUNTERS_PER_CACHELINE;

      if (nextResetIndex >= numaStripeSizePages) {
         countersToReset = WRITE_COUNTERS_PER_CACHELINE - (nextResetIndex - numaStripeSizePages);
         nextResetIndex = 0;
      }

      for (k=0; k<NUM_MEMORIES(0); k++) {
         counter = &(migRepInfo[k].writeCounter[writeResetIndex/8]);
         for (i=0; i<(countersToReset/8); i++, counter++) {
               *counter = 0;
         }
      }
      writeResetIndex = nextResetIndex;
   }

   EventDoCallback(0, MigRepPeriodic, &resetCounterCB, 0, counterResetInterval);

}