summaryrefslogtreecommitdiffstats
path: root/src/cache_types.h
blob: 4bc112c9e2a14de91d7c164ebc6b9ab50b4c238f (plain) (blame)
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
#pragma once

#include "device_conf.h"

#include <linux/build_bug.h>

#define SET_MASK(SETS) (((((uintptr_t) SETS) * CACHELINE_SIZE) - 1) ^ (CACHELINE_SIZE - 1))

#define REMOVE_PAGE_OFFSET(ptr) ((void *) (((uintptr_t) ptr) & PAGE_MASK))

#define GET_BIT(b, i) (((b & (1 << i)) >> i) & 1)
#define SET_BIT(b, i) (b | (1 << i))

/* Operate cacheline flags
 * Used flags:
 *  32                    2              1       0
 * |  | ... | cache group initialized | last | first |
 */
#define DEFAULT_FLAGS 0
#define SET_FIRST(flags) SET_BIT(flags, 0)
#define SET_LAST(flags) SET_BIT(flags, 1)
#define SET_CACHE_GROUP_INIT(flags) SET_BIT(flags, 2)
#define IS_FIRST(flags) GET_BIT(flags, 0)
#define IS_LAST(flags) GET_BIT(flags, 1)
#define IS_CACHE_GROUP_INIT(flags) GET_BIT(flags, 2)

// Offset of the next and prev field in the cacheline struct
#define CL_NEXT_OFFSET 0
#define CL_PREV_OFFSET 8

typedef enum cache_level cache_level;
typedef enum addressing_type addressing_type;
typedef struct cacheline cacheline;
typedef struct cache_ctx cache_ctx;

enum cache_level {L1, L2};
enum addressing_type {VIRTUAL, PHYSICAL};

struct cache_ctx {
    cache_level cache_level;
    addressing_type addressing;

    uint32_t sets;
    uint32_t associativity;
    uint32_t access_time;
    uint32_t nr_of_cachelines;
    uint32_t set_size;
    uint32_t cache_size;
};

struct cacheline {
    // Doubly linked list inside same set
    // Attention: CL_NEXT_OFFSET and CL_PREV_OFFSET
    // must be kept up to date
    cacheline *next;
    cacheline *prev;

    uint16_t cache_set;
    uint16_t flags;

    // Unused padding to fill cache line
    uint64_t count;
    char padding[32];
};

static_assert(sizeof(struct cacheline) == CACHELINE_SIZE, "Bad cache line struct size");