[452] | 1 | /* -*- c++ -*- |
---|
| 2 | * |
---|
| 3 | * SOCLIB_LGPL_HEADER_BEGIN |
---|
| 4 | * |
---|
| 5 | * This file is part of SoCLib, GNU LGPLv2.1. |
---|
| 6 | * |
---|
| 7 | * SoCLib is free software; you can redistribute it and/or modify it |
---|
| 8 | * under the terms of the GNU Lesser General Public License as published |
---|
| 9 | * by the Free Software Foundation; version 2.1 of the License. |
---|
| 10 | * |
---|
| 11 | * SoCLib is distributed in the hope that it will be useful, but |
---|
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with SoCLib; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
| 19 | * 02110-1301 USA |
---|
| 20 | * |
---|
| 21 | * SOCLIB_LGPL_HEADER_END |
---|
| 22 | * |
---|
| 23 | * Copyright (c) UPMC, Lip6 |
---|
| 24 | * Alain Greiner <alain.greiner@lip6.fr> July 2008 |
---|
| 25 | * |
---|
| 26 | * Maintainers: alain yang |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /********************************************************************** |
---|
| 30 | * File : generic_tlb.h |
---|
| 31 | * Date : 07/01/2012 |
---|
| 32 | * Authors : Alain Greiner |
---|
| 33 | ********************************************************************** |
---|
| 34 | * This object is a generic TLB (Translation Lookaside Buffer) |
---|
| 35 | * It is implemented as a set-associative cache. |
---|
| 36 | * It supports two sizes of page : small page of 4K bytes, |
---|
| 37 | * and big pages of 2M bytes. |
---|
| 38 | * The replacement algorithm is pseudo-LRU. |
---|
| 39 | * The virtual adress is 32 bits. |
---|
| 40 | * |
---|
| 41 | * Each TLB entry has the following format: |
---|
| 42 | * - bool V valid |
---|
| 43 | * - bool L locally accessed |
---|
| 44 | * - bool R remotely accessed |
---|
| 45 | * - bool C cachable |
---|
[492] | 46 | * - bool NCC Non Coherente |
---|
[452] | 47 | * - bool W writable |
---|
| 48 | * - bool X executable |
---|
| 49 | * - bool U unprotected: access in user mode allowed |
---|
| 50 | * - bool G global: entry not invalidated by a TLB flush |
---|
| 51 | * - bool D dirty: page has been modified |
---|
| 52 | * - bool B big page (0 => 4K page / 1 => 2M page) |
---|
| 53 | * - bool Z recently used: pseudo-LRU replacement in TLB |
---|
| 54 | * - uint32_t vpn virtual page number (20 bits / 11 bits) |
---|
| 55 | * - uint32_t ppn physical page number (28 bits / 19 bits) |
---|
| 56 | * - paddr_t nline cache line index of the corresponding PTE |
---|
| 57 | * |
---|
| 58 | * This TLB supports a bypass mechanism in order to avoid access to |
---|
| 59 | * PT1 in case of tlb miss for a small page : It keep the last valid |
---|
| 60 | * (vpn -> ptba) translation in four specific registers. |
---|
| 61 | * |
---|
| 62 | * Implementation note: |
---|
| 63 | * Each field of a tlb entry is implemented as an (nways*nsets) array. |
---|
| 64 | * |
---|
| 65 | ********************************************************************** |
---|
| 66 | * This object has 3 constructor parameters: |
---|
| 67 | * - uint32_t nways : number of ways per associative set. |
---|
| 68 | * - uint32_t nsets : number of associative sets. |
---|
| 69 | * - uint32_t paddr_nbits : number of bits in physical address |
---|
| 70 | * Both nways & nsets must be power of 2 no larger than 64. |
---|
| 71 | * paddr_nbits cannot be smaller than 32 or larger than 42. |
---|
| 72 | **********************************************************************/ |
---|
| 73 | |
---|
| 74 | #ifndef SOCLIB_CABA_GENERIC_TLB_H |
---|
| 75 | #define SOCLIB_CABA_GENERIC_TLB_H |
---|
| 76 | |
---|
| 77 | #include <inttypes.h> |
---|
| 78 | #include <systemc> |
---|
| 79 | #include <assert.h> |
---|
| 80 | #include "static_assert.h" |
---|
| 81 | #include "arithmetics.h" |
---|
| 82 | #include <iostream> |
---|
| 83 | #include <iomanip> |
---|
| 84 | |
---|
| 85 | namespace soclib { |
---|
| 86 | namespace caba { |
---|
| 87 | |
---|
| 88 | using namespace sc_core; |
---|
| 89 | |
---|
| 90 | // structure containing the 11 flags of a tlb entry |
---|
| 91 | typedef struct pte_info_s { |
---|
| 92 | bool v; // valid |
---|
| 93 | bool l; // locally accessed |
---|
| 94 | bool r; // remotely accessed |
---|
| 95 | bool c; // cacheable |
---|
[492] | 96 | bool ncc; // Non Coherent (for ODCCP) |
---|
[452] | 97 | bool w; // writable |
---|
| 98 | bool x; // executable |
---|
| 99 | bool u; // unprotected |
---|
| 100 | bool g; // global |
---|
| 101 | bool d; // dirty |
---|
| 102 | bool b; // big page |
---|
| 103 | bool z; // recently used |
---|
| 104 | }pte_info_t; |
---|
| 105 | |
---|
| 106 | enum { |
---|
| 107 | PTD_ID2_MASK = 0x001FF000, |
---|
| 108 | PAGE_K_MASK = 0x00000FFF, |
---|
| 109 | PAGE_M_MASK = 0x001FFFFF, |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | enum { |
---|
| 113 | PAGE_M_NBITS = 21, |
---|
| 114 | PAGE_K_NBITS = 12, |
---|
| 115 | INDEX1_NBITS = 11, |
---|
| 116 | }; |
---|
| 117 | |
---|
| 118 | // masks for flags bits in PTE |
---|
| 119 | enum { |
---|
| 120 | PTE_V_MASK = 0x80000000, // valid bit in PTE |
---|
| 121 | PTE_T_MASK = 0x40000000, // type bit in PTE |
---|
| 122 | PTE_L_MASK = 0x20000000, // local bit in PTE |
---|
| 123 | PTE_R_MASK = 0x10000000, // remote bit in PTE |
---|
| 124 | PTE_C_MASK = 0x08000000, // cacheable bit in PTE |
---|
| 125 | PTE_W_MASK = 0x04000000, // writeable bit in PTE |
---|
| 126 | PTE_X_MASK = 0x02000000, // executable bit in PTE |
---|
| 127 | PTE_U_MASK = 0x01000000, // unprotected bit in PTE |
---|
| 128 | PTE_G_MASK = 0x00800000, // global bit in PTE |
---|
| 129 | PTE_D_MASK = 0x00400000, // dirty bit in PTE |
---|
[492] | 130 | PTE_NCC_MASK = 0x00200000, // Non Coherent bit in PTE (for ODCCP) |
---|
[452] | 131 | }; |
---|
| 132 | |
---|
| 133 | // shifts for flags bits in PTE |
---|
| 134 | enum { |
---|
| 135 | PTE_V_SHIFT = 31, |
---|
| 136 | PTE_T_SHIFT = 30, |
---|
| 137 | PTE_L_SHIFT = 29, |
---|
| 138 | PTE_R_SHIFT = 28, |
---|
| 139 | PTE_C_SHIFT = 27, |
---|
| 140 | PTE_W_SHIFT = 26, |
---|
| 141 | PTE_X_SHIFT = 25, |
---|
| 142 | PTE_U_SHIFT = 24, |
---|
| 143 | PTE_G_SHIFT = 23, |
---|
| 144 | PTE_D_SHIFT = 22, |
---|
[492] | 145 | PTE_NCC_SHIFT = 21, |
---|
[452] | 146 | }; |
---|
| 147 | |
---|
| 148 | using soclib::common::uint32_log2; |
---|
| 149 | |
---|
| 150 | ////////////////////////// |
---|
| 151 | template<typename paddr_t> |
---|
| 152 | class GenericTlb |
---|
| 153 | { |
---|
| 154 | protected: |
---|
| 155 | |
---|
| 156 | // structure constants |
---|
| 157 | const std::string m_name; |
---|
| 158 | const size_t m_procid; |
---|
| 159 | const size_t m_nways; |
---|
| 160 | const size_t m_nsets; |
---|
| 161 | const size_t m_paddr_nbits; |
---|
| 162 | const size_t m_sets_shift; |
---|
| 163 | const size_t m_sets_mask; |
---|
| 164 | |
---|
| 165 | // TLB content: arrays[m_nsets*m_nways] |
---|
| 166 | paddr_t *m_nline; |
---|
| 167 | uint32_t *m_ppn; |
---|
| 168 | uint32_t *m_vpn; |
---|
| 169 | bool *m_valid; |
---|
| 170 | bool *m_local; |
---|
| 171 | bool *m_remote; |
---|
| 172 | bool *m_cacheable; |
---|
| 173 | bool *m_writable; |
---|
| 174 | bool *m_executable; |
---|
| 175 | bool *m_unprotected; |
---|
| 176 | bool *m_global; |
---|
| 177 | bool *m_dirty; |
---|
| 178 | bool *m_big; |
---|
| 179 | bool *m_recent; |
---|
[492] | 180 | bool *m_non_coherent; // for ODCCP |
---|
[452] | 181 | |
---|
| 182 | // bypass registers |
---|
| 183 | bool m_bypass_valid; // valid bypass registered |
---|
| 184 | uint32_t m_bypass_id1; // IX1 field in the VPN |
---|
| 185 | uint32_t m_bypass_ptba; // PTBA value |
---|
| 186 | paddr_t m_bypass_nline; // cache line index for the corresponding PTE |
---|
| 187 | |
---|
| 188 | public: |
---|
| 189 | |
---|
| 190 | ///////////////////////////////////////// |
---|
| 191 | paddr_t get_nline(size_t way, size_t set) |
---|
| 192 | { |
---|
| 193 | return m_nline[m_nsets*way+set]; |
---|
| 194 | } |
---|
| 195 | //////////////////////////////////////// |
---|
| 196 | uint32_t get_vpn(size_t way, size_t set) |
---|
| 197 | { |
---|
| 198 | return m_vpn[m_nsets*way+set]; |
---|
| 199 | } |
---|
| 200 | //////////////////////////////////////// |
---|
| 201 | uint32_t get_ppn(size_t way, size_t set) |
---|
| 202 | { |
---|
| 203 | return m_ppn[m_nsets*way+set]; |
---|
| 204 | } |
---|
| 205 | ////////////////////////////////////// |
---|
| 206 | bool get_valid(size_t way, size_t set) |
---|
| 207 | { |
---|
| 208 | return m_valid[m_nsets*way+set]; |
---|
| 209 | } |
---|
| 210 | /////////////////////////////////////// |
---|
| 211 | bool get_global(size_t way, size_t set) |
---|
| 212 | { |
---|
| 213 | return m_global[m_nsets*way+set]; |
---|
| 214 | } |
---|
| 215 | //////////////////////////////////// |
---|
| 216 | bool get_big(size_t way, size_t set) |
---|
| 217 | { |
---|
| 218 | return m_big[(way*m_nsets)+set]; |
---|
| 219 | } |
---|
| 220 | ////////////////////////////////////// |
---|
| 221 | bool get_local(size_t way, size_t set) |
---|
| 222 | { |
---|
| 223 | return m_local[(way*m_nsets)+set]; |
---|
| 224 | } |
---|
| 225 | /////////////////////////////////////// |
---|
| 226 | bool get_remote(size_t way, size_t set) |
---|
| 227 | { |
---|
| 228 | return m_remote[(way*m_nsets)+set]; |
---|
| 229 | } |
---|
| 230 | ////////////////////////////////////////// |
---|
| 231 | bool get_cacheable(size_t way, size_t set) |
---|
| 232 | { |
---|
| 233 | return m_cacheable[(way*m_nsets)+set]; |
---|
| 234 | } |
---|
| 235 | ///////////////////////////////////////// |
---|
| 236 | bool get_writable(size_t way, size_t set) |
---|
| 237 | { |
---|
| 238 | return m_writable[(way*m_nsets)+set]; |
---|
| 239 | } |
---|
| 240 | /////////////////////////////////////////// |
---|
| 241 | bool get_executable(size_t way, size_t set) |
---|
| 242 | { |
---|
| 243 | return m_executable[(way*m_nsets)+set]; |
---|
| 244 | } |
---|
| 245 | //////////////////////////////////////////// |
---|
| 246 | bool get_unprotected(size_t way, size_t set) |
---|
| 247 | { |
---|
| 248 | return m_unprotected[(way*m_nsets)+set]; |
---|
| 249 | } |
---|
| 250 | ////////////////////////////////////// |
---|
| 251 | bool get_dirty(size_t way, size_t set) |
---|
| 252 | { |
---|
| 253 | return m_dirty[(way*m_nsets)+set]; |
---|
| 254 | } |
---|
| 255 | ////////////////////////////////////// |
---|
[492] | 256 | bool get_non_coherent(size_t way, size_t set) |
---|
[452] | 257 | { |
---|
[492] | 258 | return m_non_coherent[(way*m_nsets)+set]; |
---|
[452] | 259 | } |
---|
| 260 | ////////////////////////////////////// |
---|
| 261 | bool get_recent(size_t way, size_t set) |
---|
| 262 | { |
---|
| 263 | return m_recent[m_nsets*way+set]; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | ////////////////////////////////////////////////////////////// |
---|
| 267 | // constructor checks parameters, allocates the memory |
---|
| 268 | // and computes m_page_mask, m_sets_mask and m_sets_shift |
---|
| 269 | ////////////////////////////////////////////////////////////// |
---|
| 270 | GenericTlb(const std::string &name, |
---|
| 271 | size_t procid, |
---|
| 272 | size_t nways, |
---|
| 273 | size_t nsets, |
---|
| 274 | size_t paddr_nbits) |
---|
| 275 | : m_name(name), |
---|
| 276 | m_procid(procid), |
---|
| 277 | m_nways(nways), |
---|
| 278 | m_nsets(nsets), |
---|
| 279 | m_paddr_nbits(paddr_nbits), |
---|
| 280 | m_sets_shift(uint32_log2(nsets)), |
---|
| 281 | m_sets_mask((1<<(int)uint32_log2(nsets))-1) |
---|
| 282 | { |
---|
| 283 | assert(IS_POW_OF_2(nsets)); |
---|
| 284 | assert(IS_POW_OF_2(nways)); |
---|
| 285 | assert(nsets <= 64); |
---|
| 286 | assert(nways <= 64); |
---|
| 287 | |
---|
| 288 | if((m_paddr_nbits < 32) || (m_paddr_nbits > 42)) |
---|
| 289 | { |
---|
| 290 | printf("Error in the genericTlb component\n"); |
---|
| 291 | printf("The physical address parameter must be in the range [32,42]\n"); |
---|
| 292 | exit(1); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | m_nline = new paddr_t[nways * nsets]; |
---|
| 296 | m_ppn = new uint32_t[nways * nsets]; |
---|
| 297 | m_vpn = new uint32_t[nways * nsets]; |
---|
| 298 | m_valid = new bool[nways * nsets]; |
---|
| 299 | m_local = new bool[nways * nsets]; |
---|
| 300 | m_remote = new bool[nways * nsets]; |
---|
| 301 | m_cacheable = new bool[nways * nsets]; |
---|
| 302 | m_writable = new bool[nways * nsets]; |
---|
| 303 | m_executable = new bool[nways * nsets]; |
---|
| 304 | m_unprotected = new bool[nways * nsets]; |
---|
| 305 | m_global = new bool[nways * nsets]; |
---|
| 306 | m_dirty = new bool[nways * nsets]; |
---|
| 307 | m_big = new bool[nways * nsets]; |
---|
| 308 | m_recent = new bool[nways * nsets]; |
---|
[492] | 309 | m_non_coherent = new bool[nways * nsets]; |
---|
[452] | 310 | |
---|
| 311 | } // end constructor |
---|
| 312 | |
---|
| 313 | ///////////// |
---|
| 314 | ~GenericTlb() |
---|
| 315 | { |
---|
| 316 | delete [] m_nline; |
---|
| 317 | delete [] m_ppn; |
---|
| 318 | delete [] m_vpn; |
---|
| 319 | delete [] m_valid; |
---|
| 320 | delete [] m_local; |
---|
| 321 | delete [] m_remote; |
---|
| 322 | delete [] m_cacheable; |
---|
| 323 | delete [] m_writable; |
---|
| 324 | delete [] m_executable; |
---|
| 325 | delete [] m_unprotected; |
---|
| 326 | delete [] m_global; |
---|
| 327 | delete [] m_dirty; |
---|
| 328 | delete [] m_big; |
---|
| 329 | delete [] m_recent; |
---|
[492] | 330 | delete [] m_non_coherent; |
---|
[452] | 331 | } |
---|
| 332 | |
---|
| 333 | ///////////////////////////////////////////////////////////// |
---|
| 334 | // This method resets all the TLB entries |
---|
| 335 | // as well as the bypass |
---|
| 336 | ///////////////////////////////////////////////////////////// |
---|
| 337 | void reset() |
---|
| 338 | { |
---|
| 339 | for (size_t way = 0 ; way < m_nways ; way++) |
---|
| 340 | { |
---|
| 341 | for (size_t set = 0 ; set < m_nsets ; set++) |
---|
| 342 | { |
---|
| 343 | m_valid[m_nsets*way+set] = false; |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | m_bypass_valid = false; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 350 | // This method takes a virtual adress as input argument. It returns false in case of miss. |
---|
| 351 | // In case of HIT, the physical address, the pte informations, way and set are returned. |
---|
| 352 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 353 | bool translate( uint32_t vaddress, // virtual address |
---|
| 354 | paddr_t *paddress, // return physical address |
---|
| 355 | pte_info_t *pte_info, // return flags |
---|
| 356 | paddr_t *nline, // return nline |
---|
| 357 | size_t *tw, // return tlb way |
---|
| 358 | size_t *ts ) // return tlb set |
---|
| 359 | { |
---|
| 360 | size_t m_set = (vaddress >> PAGE_M_NBITS) & m_sets_mask; |
---|
| 361 | size_t k_set = (vaddress >> PAGE_K_NBITS) & m_sets_mask; |
---|
| 362 | |
---|
| 363 | for( size_t way = 0; way < m_nways; way++ ) |
---|
| 364 | { |
---|
| 365 | // TLB hit test for 2M page size |
---|
| 366 | if( get_valid(way,m_set) and get_big(way,m_set) and |
---|
| 367 | (get_vpn(way,m_set) == (vaddress >> (PAGE_M_NBITS + m_sets_shift))) ) |
---|
| 368 | { |
---|
[492] | 369 | pte_info->v = get_valid(way,m_set); |
---|
| 370 | pte_info->l = get_local(way,m_set); |
---|
| 371 | pte_info->r = get_remote(way,m_set); |
---|
| 372 | pte_info->c = get_cacheable(way,m_set); |
---|
| 373 | pte_info->w = get_writable(way,m_set); |
---|
| 374 | pte_info->x = get_executable(way,m_set); |
---|
| 375 | pte_info->u = get_unprotected(way,m_set); |
---|
| 376 | pte_info->g = get_global(way,m_set); |
---|
| 377 | pte_info->d = get_dirty(way,m_set); |
---|
| 378 | pte_info->b = get_big(way,m_set); |
---|
| 379 | pte_info->z = get_recent(way,m_set); |
---|
| 380 | pte_info->ncc = get_non_coherent(way,m_set); // coherent/not coherent bit for ODCCP |
---|
[452] | 381 | |
---|
| 382 | *nline = get_nline(way,m_set); |
---|
| 383 | *tw = way; |
---|
| 384 | *ts = m_set; |
---|
| 385 | |
---|
| 386 | *paddress = (paddr_t)((paddr_t)get_ppn(way,m_set) << PAGE_M_NBITS) | |
---|
| 387 | (paddr_t)(vaddress & PAGE_M_MASK); |
---|
| 388 | |
---|
| 389 | set_recent(way, m_set); |
---|
| 390 | return true; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | // TLB hit test for 4K page size |
---|
| 394 | if( get_valid(way,k_set) and not get_big(way,k_set) and |
---|
| 395 | (get_vpn(way,k_set) == (vaddress >> (PAGE_K_NBITS + m_sets_shift))) ) |
---|
| 396 | { |
---|
[492] | 397 | pte_info->v = get_valid(way,k_set); |
---|
| 398 | pte_info->l = get_local(way,k_set); |
---|
| 399 | pte_info->r = get_remote(way,k_set); |
---|
| 400 | pte_info->c = get_cacheable(way,k_set); |
---|
| 401 | pte_info->w = get_writable(way,k_set); |
---|
| 402 | pte_info->x = get_executable(way,k_set); |
---|
| 403 | pte_info->u = get_unprotected(way,k_set); |
---|
| 404 | pte_info->g = get_global(way,k_set); |
---|
| 405 | pte_info->d = get_dirty(way,k_set); |
---|
| 406 | pte_info->b = get_big(way,k_set); |
---|
| 407 | pte_info->z = get_recent(way,k_set); |
---|
| 408 | pte_info->ncc = get_non_coherent(way,k_set);// coherent/not coherent bit for ODCCP |
---|
[452] | 409 | |
---|
| 410 | *nline = get_nline(way,k_set); |
---|
| 411 | *tw = way; |
---|
| 412 | *ts = k_set; |
---|
| 413 | |
---|
| 414 | *paddress = (paddr_t)((paddr_t)get_ppn(way,k_set) << PAGE_K_NBITS) | |
---|
| 415 | (paddr_t)(vaddress & PAGE_K_MASK); |
---|
| 416 | |
---|
| 417 | set_recent(way, k_set); |
---|
| 418 | return true; |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | return false; |
---|
| 422 | } // end translate() |
---|
| 423 | |
---|
| 424 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 425 | // This method takes a virtual adress as input argument. It returns false in case of miss. |
---|
| 426 | // In case of HIT, the physical address is returned. |
---|
| 427 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 428 | bool translate(uint32_t vaddress, paddr_t *paddress) |
---|
| 429 | { |
---|
| 430 | size_t m_set = (vaddress >> PAGE_M_NBITS) & m_sets_mask; |
---|
| 431 | size_t k_set = (vaddress >> PAGE_K_NBITS) & m_sets_mask; |
---|
| 432 | |
---|
| 433 | for( size_t way = 0; way < m_nways; way++ ) |
---|
| 434 | { |
---|
| 435 | // TLB hit test for 2M page size |
---|
| 436 | if( get_valid(way,m_set) and get_big(way,m_set) and |
---|
| 437 | (get_vpn(way,m_set) == (vaddress >> (PAGE_M_NBITS + m_sets_shift))) ) |
---|
| 438 | { |
---|
| 439 | *paddress = (paddr_t)((paddr_t)get_ppn(way,m_set) << PAGE_M_NBITS) | |
---|
| 440 | (paddr_t)(vaddress & PAGE_M_MASK); |
---|
| 441 | |
---|
| 442 | set_recent(way, m_set); |
---|
| 443 | return true; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | // TLB hit test for 4K page size |
---|
| 447 | if( get_valid(way,k_set) and not get_big(way,k_set) and |
---|
| 448 | (get_vpn(way,k_set) == (vaddress >> (PAGE_K_NBITS + m_sets_shift))) ) |
---|
| 449 | { |
---|
| 450 | *paddress = ((paddr_t)get_ppn(way,k_set) << PAGE_K_NBITS) | |
---|
| 451 | (paddr_t)(vaddress & PAGE_K_MASK); |
---|
| 452 | |
---|
| 453 | set_recent(way, k_set); |
---|
| 454 | return true; |
---|
| 455 | } |
---|
| 456 | } |
---|
| 457 | return false; |
---|
| 458 | } // end translate() |
---|
| 459 | |
---|
| 460 | ///////////////////////////////////////////////////////////// |
---|
| 461 | // This method resets all valid bits in one cycle, |
---|
| 462 | // for non global tlb entries. |
---|
| 463 | ///////////////////////////////////////////////////////////// |
---|
| 464 | void flush() |
---|
| 465 | { |
---|
| 466 | m_bypass_valid = false; |
---|
| 467 | |
---|
| 468 | for( size_t way = 0; way < m_nways; way++ ) |
---|
| 469 | { |
---|
| 470 | for(size_t set = 0; set < m_nsets; set++) |
---|
| 471 | { |
---|
| 472 | if( not get_global(way,set) ) |
---|
| 473 | { |
---|
| 474 | m_valid[way*m_nsets+set] = false; |
---|
| 475 | } |
---|
| 476 | } |
---|
| 477 | } |
---|
| 478 | } // end flush |
---|
| 479 | |
---|
| 480 | ///////////////////////////////////////////////////////////// |
---|
| 481 | // This method returns the values of the various fields |
---|
| 482 | // of a tlb entry identified by the way and set arguments. |
---|
| 483 | ///////////////////////////////////////////////////////////// |
---|
| 484 | void get_entry(size_t way, |
---|
| 485 | size_t set, |
---|
| 486 | pte_info_t* flags, |
---|
| 487 | uint32_t* vpn, |
---|
| 488 | uint32_t* ppn, |
---|
| 489 | paddr_t* nline) |
---|
| 490 | { |
---|
[492] | 491 | flags->v = m_valid[way*m_nsets+set]; |
---|
| 492 | flags->l = m_local[way*m_nsets+set]; |
---|
| 493 | flags->r = m_remote[way*m_nsets+set]; |
---|
| 494 | flags->c = m_cacheable[way*m_nsets+set]; |
---|
| 495 | flags->w = m_writable[way*m_nsets+set]; |
---|
| 496 | flags->x = m_executable[way*m_nsets+set]; |
---|
| 497 | flags->u = m_unprotected[way*m_nsets+set]; |
---|
| 498 | flags->g = m_global[way*m_nsets+set]; |
---|
| 499 | flags->d = m_dirty[way*m_nsets+set]; |
---|
| 500 | flags->b = m_big[way*m_nsets+set]; |
---|
| 501 | flags->z = m_recent[way*m_nsets+set]; |
---|
| 502 | flags->ncc = m_non_coherent[way*m_nsets+set]; // flag coherent/not coherent for ODCCP |
---|
[452] | 503 | |
---|
| 504 | *ppn = m_ppn[way*m_nsets+set]; |
---|
| 505 | *vpn = m_vpn[way*m_nsets+set]; |
---|
| 506 | *nline = m_nline[way*m_nsets+set]; |
---|
| 507 | } // end get_entry() |
---|
| 508 | |
---|
| 509 | ///////////////////////////////////////////////////////////////////////// |
---|
| 510 | // This method implement the pseudo LRU policy to select a tlb slot: |
---|
| 511 | // It returns the least recently used way in the associative set |
---|
| 512 | // corresponding to the requested virtual address, and page type. |
---|
| 513 | // It returns the selected slot way and set. |
---|
| 514 | ///////////////////////////////////////////////////////////////////// |
---|
| 515 | void select(uint32_t vaddr, |
---|
| 516 | bool pte1, |
---|
| 517 | size_t* selway, |
---|
| 518 | size_t* selset) |
---|
| 519 | { |
---|
| 520 | size_t set; |
---|
| 521 | |
---|
| 522 | if ( pte1 ) set = (vaddr >> PAGE_M_NBITS) & m_sets_mask; |
---|
| 523 | else set = (vaddr >> PAGE_K_NBITS) & m_sets_mask; |
---|
| 524 | |
---|
| 525 | // search an invalid way |
---|
| 526 | for(size_t way = 0; way < m_nways; way++) |
---|
| 527 | { |
---|
| 528 | if( not get_valid(way,set) ) |
---|
| 529 | { |
---|
| 530 | *selway = way; |
---|
| 531 | *selset = set; |
---|
| 532 | return; |
---|
| 533 | } |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | // search an old but non global way |
---|
| 537 | for( size_t way = 0; way < m_nways; way++ ) |
---|
| 538 | { |
---|
| 539 | if( not get_global(way,set) and not get_recent(way,set) ) |
---|
| 540 | { |
---|
| 541 | *selway = way; |
---|
| 542 | *selset = set; |
---|
| 543 | return; |
---|
| 544 | } |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | // finally take the first old way |
---|
| 548 | for( size_t way = 0; way < m_nways; way++ ) |
---|
| 549 | { |
---|
| 550 | if( not get_recent(way,set) ) |
---|
| 551 | { |
---|
| 552 | *selway = way; |
---|
| 553 | *selset = set; |
---|
| 554 | return; |
---|
| 555 | } |
---|
| 556 | } |
---|
| 557 | |
---|
[544] | 558 | assert(false && "all TLB ways can't be new at the same time"); |
---|
[452] | 559 | } // end select() |
---|
| 560 | |
---|
| 561 | //////////////////////////////////////////////////////////////////////// |
---|
| 562 | // This method writes a new entry in the TLB, |
---|
| 563 | // in the slot defined by the way & set arguments. |
---|
| 564 | // The big argument defines the page type (true for 2M page). |
---|
| 565 | // PTE1 is 32 bits / PTE2 is 64 bits |
---|
| 566 | // For both types of page, the pte_flags argument contains the flags. |
---|
| 567 | // For 4K pages, the PPN value is contained in the pte_ppn argument. |
---|
| 568 | // For 2M pages, the PPN value is contained in the pte_flags argument. |
---|
| 569 | //////////////////////////////////////////////////////////////////////// |
---|
| 570 | void write(bool big, |
---|
| 571 | uint32_t pte_flags, |
---|
| 572 | uint32_t pte_ppn, |
---|
| 573 | uint32_t vaddr, |
---|
| 574 | size_t way, |
---|
| 575 | size_t set, |
---|
| 576 | paddr_t nline) |
---|
| 577 | { |
---|
| 578 | if ( big ) // 2M page |
---|
| 579 | { |
---|
| 580 | assert ( (set == ((vaddr >> PAGE_M_NBITS) & m_sets_mask)) and |
---|
| 581 | "error in tlb write for a 2M page"); |
---|
| 582 | m_vpn[way*m_nsets+set] = vaddr >> (PAGE_M_NBITS + m_sets_shift); |
---|
| 583 | m_ppn[way*m_nsets+set] = pte_flags & ((1<<(m_paddr_nbits - PAGE_M_NBITS))-1); |
---|
| 584 | m_big[way*m_nsets+set] = true; |
---|
| 585 | } |
---|
| 586 | else // 4K page |
---|
| 587 | { |
---|
| 588 | assert ( (set == ((vaddr >> PAGE_K_NBITS) & m_sets_mask)) and |
---|
| 589 | "error in tlb write for a 4K page"); |
---|
| 590 | m_vpn[way*m_nsets+set] = vaddr >> (PAGE_K_NBITS + m_sets_shift); |
---|
| 591 | m_ppn[way*m_nsets+set] = pte_ppn & ((1<<(m_paddr_nbits - PAGE_K_NBITS))-1); |
---|
| 592 | m_big[way*m_nsets+set] = false; |
---|
| 593 | } |
---|
[492] | 594 | m_nline[way*m_nsets+set] = nline; |
---|
| 595 | m_valid[way*m_nsets+set] = true; |
---|
[544] | 596 | //m_recent[way*m_nsets+set] = true; |
---|
[492] | 597 | m_local[way*m_nsets+set] = (((pte_flags & PTE_L_MASK) >> PTE_L_SHIFT) == 1) ? true : false; |
---|
| 598 | m_remote[way*m_nsets+set] = (((pte_flags & PTE_R_MASK) >> PTE_R_SHIFT) == 1) ? true : false; |
---|
| 599 | m_cacheable[way*m_nsets+set] = (((pte_flags & PTE_C_MASK) >> PTE_C_SHIFT) == 1) ? true : false; |
---|
| 600 | m_writable[way*m_nsets+set] = (((pte_flags & PTE_W_MASK) >> PTE_W_SHIFT) == 1) ? true : false; |
---|
| 601 | m_executable[way*m_nsets+set] = (((pte_flags & PTE_X_MASK) >> PTE_X_SHIFT) == 1) ? true : false; |
---|
| 602 | m_unprotected[way*m_nsets+set] = (((pte_flags & PTE_U_MASK) >> PTE_U_SHIFT) == 1) ? true : false; |
---|
| 603 | m_global[way*m_nsets+set] = (((pte_flags & PTE_G_MASK) >> PTE_G_SHIFT) == 1) ? true : false; |
---|
| 604 | m_dirty[way*m_nsets+set] = (((pte_flags & PTE_D_MASK) >> PTE_D_SHIFT) == 1) ? true : false; |
---|
| 605 | m_non_coherent[way*m_nsets+set] = (((pte_flags & PTE_NCC_MASK) >> PTE_NCC_SHIFT) == 1) ? true : false; // for ODCCP |
---|
[544] | 606 | set_recent(way, set); |
---|
[452] | 607 | } // end write() |
---|
| 608 | |
---|
| 609 | ////////////////////////////////////////////////////////////// |
---|
| 610 | // This method invalidates a TLB entry |
---|
| 611 | // identified by the virtual address. |
---|
| 612 | ////////////////////////////////////////////////////////////// |
---|
| 613 | bool inval(uint32_t vaddr) |
---|
| 614 | { |
---|
| 615 | size_t m_set = (vaddr >> PAGE_M_NBITS) & m_sets_mask; |
---|
| 616 | size_t k_set = (vaddr >> PAGE_K_NBITS) & m_sets_mask; |
---|
| 617 | |
---|
| 618 | for( size_t way = 0; way < m_nways; way++ ) |
---|
| 619 | { |
---|
| 620 | // TLB hit test for 2M page size |
---|
| 621 | if( get_valid(way,m_set) and get_big(way,m_set) and |
---|
| 622 | ( get_vpn(way,m_set) == (vaddr >> (PAGE_M_NBITS + m_sets_shift))) ) |
---|
| 623 | { |
---|
| 624 | m_valid[way*m_nsets+m_set] = false; |
---|
| 625 | return true; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | // TLB hit test for 4K page size |
---|
| 629 | if( get_valid(way,k_set) and not get_big(way,k_set) and |
---|
| 630 | ( get_vpn(way,k_set) == (vaddr >> (PAGE_K_NBITS + m_sets_shift))) ) |
---|
| 631 | { |
---|
| 632 | m_valid[way*m_nsets+k_set] = false; |
---|
| 633 | return true; |
---|
| 634 | } |
---|
| 635 | } |
---|
| 636 | return false; |
---|
| 637 | } // end inval() |
---|
| 638 | |
---|
| 639 | ////////////////////////////////////////////////////////////// |
---|
| 640 | // This method conditionnally invalidates a TLB entry |
---|
| 641 | // identified by the way and set arguments, if it matches |
---|
| 642 | // the nline argument. |
---|
| 643 | // The bypass is also inalidated if it matches the nline. |
---|
| 644 | ////////////////////////////////////////////////////////////// |
---|
| 645 | bool inval(paddr_t nline, |
---|
| 646 | size_t way, |
---|
| 647 | size_t set) |
---|
| 648 | { |
---|
| 649 | if ( m_bypass_nline == nline ) m_bypass_valid = false; |
---|
| 650 | |
---|
| 651 | if ( m_nline[way*m_nsets+set] == nline ) |
---|
| 652 | { |
---|
| 653 | m_valid[way*m_nsets+set] = false; |
---|
| 654 | return true; |
---|
| 655 | } |
---|
| 656 | return false; |
---|
| 657 | } // end inval() |
---|
| 658 | |
---|
| 659 | /////////////////////////////////////////////////// |
---|
| 660 | // set local bit |
---|
| 661 | ////////////////////////////////////////////////// |
---|
| 662 | void set_local( size_t way, |
---|
| 663 | size_t set ) |
---|
| 664 | { |
---|
| 665 | m_local[way*m_nsets+set] = true; |
---|
| 666 | } |
---|
| 667 | |
---|
| 668 | /////////////////////////////////////////////////// |
---|
| 669 | // set remote bit |
---|
| 670 | ////////////////////////////////////////////////// |
---|
| 671 | void set_remote( size_t way, |
---|
| 672 | size_t set ) |
---|
| 673 | { |
---|
| 674 | m_remote[way*m_nsets+set] = true; |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | /////////////////////////////////////////////////// |
---|
| 678 | // set dirty bit |
---|
| 679 | ////////////////////////////////////////////////// |
---|
| 680 | void set_dirty( size_t way, |
---|
| 681 | size_t set ) |
---|
| 682 | { |
---|
| 683 | m_dirty[way*m_nsets+set] = true; |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | /////////////////////////////////////////////////// |
---|
| 687 | // recent bit management for LRU policy |
---|
| 688 | // if all recent bits but the (way,set) are true, |
---|
| 689 | // all recent bits must be reset |
---|
| 690 | // else only the target bit is set |
---|
| 691 | ////////////////////////////////////////////////// |
---|
| 692 | void set_recent( size_t way, |
---|
| 693 | size_t set ) |
---|
| 694 | { |
---|
| 695 | bool reset = true; |
---|
| 696 | |
---|
| 697 | for ( size_t i = 0 ; i < m_nways ; i++ ) |
---|
| 698 | { |
---|
| 699 | if ( (i != way) and not get_recent(i, set)) reset = false; |
---|
| 700 | } |
---|
| 701 | if ( reset ) // all recent bits must be reset |
---|
| 702 | { |
---|
| 703 | for (size_t i = 0 ; i < m_nways ; i++) |
---|
| 704 | { |
---|
| 705 | m_recent[i*m_nsets+set] = false; |
---|
| 706 | } |
---|
| 707 | } |
---|
| 708 | else // only recent(way,set) is set |
---|
| 709 | { |
---|
| 710 | m_recent[way*m_nsets+set] = true; |
---|
| 711 | } |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | //////////////////////////////////////////////////////////////////////// |
---|
| 715 | // This get_bypass() function implementis the first level page table |
---|
| 716 | // bypass in case of PTE2 miss. |
---|
| 717 | // It returns the registered ptba if it is valid, and if the ID1 field |
---|
| 718 | // of the virtual address matches the regisrered ID1 value. |
---|
| 719 | ///////////////////////////////////////////////////////////////////////// |
---|
| 720 | bool get_bypass(uint32_t vaddr, |
---|
| 721 | uint32_t* ptba) |
---|
| 722 | { |
---|
| 723 | if ( m_bypass_valid and ((vaddr >> PAGE_M_NBITS) == m_bypass_id1) ) |
---|
| 724 | { |
---|
| 725 | *ptba = m_bypass_ptba; |
---|
| 726 | return true; |
---|
| 727 | } |
---|
| 728 | return false; |
---|
| 729 | } |
---|
| 730 | ////////////////////////////////////////////////////////////////////// |
---|
| 731 | // The set_bypass() method registers the last valid PTD1 |
---|
| 732 | // (ID1 -> PTBA) translation in the m_bypass_valid, m_bypass_id1, |
---|
| 733 | // m_bypass_ptba & m_bypass_nline registers. |
---|
| 734 | ////////////////////////////////////////////////////////////////////// |
---|
| 735 | void set_bypass(uint32_t vaddr, |
---|
| 736 | uint32_t ptba, |
---|
| 737 | paddr_t nline) |
---|
| 738 | { |
---|
| 739 | m_bypass_valid = true; |
---|
| 740 | m_bypass_ptba = ptba; |
---|
| 741 | m_bypass_id1 = vaddr >> PAGE_M_NBITS; |
---|
| 742 | m_bypass_nline = nline; |
---|
| 743 | } |
---|
| 744 | /////////////////////////////////////////////////////////////////////// |
---|
| 745 | // The reset_bypass() method conditionnally resets the bypass |
---|
| 746 | // when the nline argument matches the registered nline. |
---|
| 747 | /////////////////////////////////////////////////////////////////////// |
---|
| 748 | void reset_bypass() |
---|
| 749 | { |
---|
| 750 | m_bypass_valid = false; |
---|
| 751 | } |
---|
| 752 | /////////////////////////////////////////////////////////////////////// |
---|
| 753 | // The printTrace() method displays the TLB content |
---|
| 754 | /////////////////////////////////////////////////////////////////////// |
---|
| 755 | void printTrace() |
---|
| 756 | { |
---|
[492] | 757 | std::cout << " set way V L R C W X U G D B Z NCC" |
---|
[452] | 758 | << " TAG PPN NLINE" << std::endl; |
---|
| 759 | |
---|
| 760 | for ( size_t set=0 ; set < m_nsets ; set++ ) |
---|
| 761 | { |
---|
| 762 | for ( size_t way=0 ; way < m_nways ; way++ ) |
---|
| 763 | { |
---|
| 764 | if ( m_valid[m_nsets*way+set] ) |
---|
| 765 | std::cout << std::dec << std::noshowbase |
---|
| 766 | << " [" << set << "] [" |
---|
| 767 | << way << "] [" |
---|
| 768 | << m_valid[m_nsets*way+set] << "][" |
---|
| 769 | << m_local[m_nsets*way+set] << "][" |
---|
| 770 | << m_remote[m_nsets*way+set] << "][" |
---|
| 771 | << m_cacheable[m_nsets*way+set] << "][" |
---|
| 772 | << m_writable[m_nsets*way+set] << "][" |
---|
| 773 | << m_executable[m_nsets*way+set] << "][" |
---|
| 774 | << m_unprotected[m_nsets*way+set] << "][" |
---|
| 775 | << m_global[m_nsets*way+set] << "][" |
---|
| 776 | << m_dirty[m_nsets*way+set] << "][" |
---|
| 777 | << m_big[m_nsets*way+set] << "][" |
---|
| 778 | << m_recent[m_nsets*way+set] << "][" |
---|
[492] | 779 | << m_non_coherent[m_nsets*way+set] << "][" |
---|
[452] | 780 | << std::hex << std::showbase |
---|
| 781 | << std::setw(7) << m_vpn[m_nsets*way+set] << "][" |
---|
| 782 | << std::setw(9) << m_ppn[m_nsets*way+set] << "][" |
---|
| 783 | << std::setw(11) << m_nline[m_nsets*way+set] << "]" |
---|
| 784 | << std::endl; |
---|
| 785 | } |
---|
| 786 | } |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | }; // end GenericTlb |
---|
| 790 | |
---|
| 791 | }} |
---|
| 792 | |
---|
| 793 | #endif /* SOCLIB_CABA_GENERIC_TLB_H */ |
---|
| 794 | |
---|
| 795 | // Local Variables: |
---|
| 796 | // tab-width: 4 |
---|
| 797 | // c-basic-offset: 4 |
---|
| 798 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 799 | // indent-tabs-mode: nil |
---|
| 800 | // End: |
---|
| 801 | |
---|
| 802 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 803 | |
---|
| 804 | |
---|
| 805 | |
---|