Pyrogenesis  trunk
cache.h
Go to the documentation of this file.
1 /* Copyright (c) 2013 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef INCLUDED_X86_X64_CACHE
24 #define INCLUDED_X86_X64_CACHE
25 
26 namespace x86_x64 {
27 
28 struct Cache // POD (may be used before static constructors)
29 {
30  enum Type
31  {
32  // (values match the CPUID.4 definition)
37  // note: further values are "reserved"
38  };
39 
40  static const size_t maxLevels = 4;
41 
42  static const size_t fullyAssociative = 0xFF; // (CPUID.4 definition)
43 
44  /**
45  * 1..maxLevels
46  **/
47  size_t level;
48 
49  /**
50  * never kNull
51  **/
53 
54  /**
55  * if 0, the cache is disabled and all other values are zero
56  **/
57  size_t numEntries;
58 
59  /**
60  * NB: cache entries are lines, TLB entries are pages
61  **/
62  size_t entrySize;
63 
64  /**
65  * = fullyAssociative or the actual ways of associativity
66  **/
67  size_t associativity;
68 
69  /**
70  * how many logical processors share this cache?
71  **/
72  size_t sharedBy;
73 
74  void Initialize(size_t level, Type type)
75  {
76  this->level = level;
77  this->type = type;
78  numEntries = 0;
79  entrySize = 0;
80  associativity = 0;
81  sharedBy = 0;
82 
83  ENSURE(Validate());
84  }
85 
86  bool Validate() const
87  {
88  if(!(1 <= level && level <= maxLevels))
89  return false;
90 
91  if(type == kNull)
92  return false;
93 
94  if(numEntries == 0) // disabled
95  {
96  if(entrySize != 0)
97  return false;
98  if(associativity != 0)
99  return false;
100  if(sharedBy != 0)
101  return false;
102  }
103  else
104  {
105  if(entrySize == 0)
106  return false;
107  if(associativity == 0 || associativity > fullyAssociative)
108  return false;
109  if(sharedBy == 0)
110  return false;
111  }
112 
113  return true;
114  }
115 
116  u64 TotalSize() const
117  {
118  return u64(numEntries)*entrySize;
119  }
120 };
121 
123 {
124  // (AddCache relies upon this order)
125  L1D = 1,
134 };
135 
136 /**
137  * @return 0 if idxCache >= TLB+numTLBs, otherwise a valid pointer to
138  * a Cache whose numEntries is 0 if disabled / not present.
139  **/
140 LIB_API const Cache* Caches(size_t idxCache);
141 
142 } // namespace x86_x64
143 
144 #endif // #ifndef INCLUDED_X86_X64_CACHE
Definition: cache.h:36
Definition: cache.h:131
Definition: cache.h:133
Definition: cache.h:127
Definition: cache.h:130
const x86_x64::Cache * Caches(size_t idxCache)
Definition: cache.cpp:649
Type type
never kNull
Definition: cache.h:52
Definition: cache.h:34
Definition: cache.h:128
Definition: cache.h:132
Type
Definition: cache.h:30
static const size_t fullyAssociative
Definition: cache.h:42
Definition: cache.h:35
uint64_t u64
Definition: types.h:40
u64 TotalSize() const
Definition: cache.h:116
Definition: cache.h:125
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:287
size_t sharedBy
how many logical processors share this cache?
Definition: cache.h:72
static const size_t maxLevels
Definition: cache.h:40
Definition: cache.h:129
Definition: cache.h:33
void Initialize(size_t level, Type type)
Definition: cache.h:74
Definition: cache.h:126
Definition: cache.h:28
Definition: cache.cpp:32
size_t level
1..maxLevels
Definition: cache.h:47
size_t numEntries
if 0, the cache is disabled and all other values are zero
Definition: cache.h:57
IdxCache
Definition: cache.h:122
size_t associativity
= fullyAssociative or the actual ways of associativity
Definition: cache.h:67
bool Validate() const
Definition: cache.h:86
size_t entrySize
NB: cache entries are lines, TLB entries are pages.
Definition: cache.h:62