Major reworking to integrate cache coloring into the kernel.
[akaros.git] / kern / src / colored_caches.c
1 /* Copyright (c) 2009 The Regents of the University  of California. 
2  * See the COPYRIGHT files at the top of this source tree for full 
3  * license information.
4  * 
5  * Kevin Klues <klueska@cs.berkeley.edu>    
6  */
7
8 #include <arch/types.h>
9 #include <arch/mmu.h>
10 #include <colored_caches.h>
11 #include <stdio.h>
12
13 /************** Cache Related Functions  *****************/
14 inline void init_cache_properties(cache_t *c, size_t sz_k, size_t wa, size_t clsz) {
15         c->wa = wa;
16         c->sz_k = sz_k;
17         c->clsz = clsz;
18         
19         //Added as optimization (derived from above);
20         c->num_colors = get_cache_num_page_colors(c);
21 }
22 inline size_t get_page_color(uintptr_t page, cache_t *c) {
23     return (page % c->num_colors);
24 }
25 inline size_t get_offset_in_cache_line(uintptr_t addr, cache_t *c) {
26     return (addr % get_cache_bytes_per_line(c));
27 }
28
29 void print_cache_properties(char *NT lstring, cache_t *c)
30 {
31         printk("%s_WAYS_ASSOCIATIVE: %ld\n", lstring, get_cache_ways_associative(c));
32         printk("%s_LINE_SIZE_BYTES: %ld\n", lstring, get_cache_line_size_bytes(c));
33         printk("%s_SIZE_BYTES: %ld\n", lstring, get_cache_size_bytes(c));
34         printk("%s_SIZE_KILOBYTES: %ld\n", lstring, get_cache_size_kilobytes(c));
35         printk("%s_SIZE_MEGABYTES: %ld\n", lstring, get_cache_size_megabytes(c));
36         printk("%s_OFFSET_BITS: %ld\n", lstring, get_cache_num_offset_bits(c));
37         printk("%s_INDEX_BITS: %ld\n", lstring, get_cache_num_index_bits(c));
38         printk("%s_TAG_BITS: %ld\n", lstring, get_cache_num_tag_bits(c));
39         printk("%s_PAGE_COLOR_BITS: %ld\n", lstring, get_cache_num_page_color_bits(c));
40         printk("%s_BYTES_PER_LINE: %ld\n", lstring, get_cache_bytes_per_line(c));
41         printk("%s_NUM_LINES: %ld\n", lstring, get_cache_num_lines(c));
42         printk("%s_NUM_SETS: %ld\n", lstring, get_cache_num_sets(c));
43         printk("%s_LINES_PER_SET: %ld\n", lstring, get_cache_lines_per_set(c));
44         printk("%s_LINES_PER_PAGE: %ld\n", lstring, get_cache_lines_per_page(c));
45         printk("%s_BYTES_PER_WAY: %ld\n", lstring, get_cache_bytes_per_way(c));
46         printk("%s_LINES_PER_WAY: %ld\n", lstring, get_cache_lines_per_way(c));
47         printk("%s_PAGES_PER_WAY: %ld\n", lstring, get_cache_pages_per_way(c));
48         printk("%s_NUM_PAGE_COLORS: %ld\n", lstring, get_cache_num_page_colors(c));
49 }
50
51 /****************** Cache Properties *********************/
52 inline size_t get_cache_ways_associative(cache_t *c) {
53         return (c->wa);
54 }
55 inline size_t get_cache_line_size_bytes(cache_t *c) {
56         return (c->clsz);
57 }
58 inline size_t get_cache_size_bytes(cache_t *c) {
59         return (c->sz_k * ONE_KILOBYTE);
60 }
61 inline size_t get_cache_size_kilobytes(cache_t *c) {
62         return (c->sz_k);
63 }
64 inline size_t get_cache_size_megabytes(cache_t *c) {
65         return (c->sz_k / ONE_KILOBYTE);
66 }
67 inline size_t get_cache_num_offset_bits(cache_t *c) {
68         return (LOG2(get_cache_line_size_bytes(c)));
69 }
70 inline size_t get_cache_num_index_bits(cache_t *c) {
71         return (LOG2(get_cache_size_bytes(c) 
72                    / get_cache_ways_associative(c)
73                    / get_cache_line_size_bytes(c)));
74 }
75 inline size_t get_cache_num_tag_bits(cache_t *c) {
76         return (NUM_ADDR_BITS - get_cache_num_offset_bits(c) 
77                           - get_cache_num_index_bits(c));
78 }
79 inline size_t get_cache_num_page_color_bits(cache_t *c) {
80         return (get_cache_num_offset_bits(c) 
81                   + get_cache_num_index_bits(c) 
82                   - PGSHIFT); 
83 }
84 inline size_t get_cache_bytes_per_line(cache_t *c) {
85         return (1 << get_cache_num_offset_bits(c));
86 }
87 inline size_t get_cache_num_lines(cache_t *c) {
88         return (get_cache_size_bytes(c)/get_cache_bytes_per_line(c));  
89 }
90 inline size_t get_cache_num_sets(cache_t *c) {
91         return (get_cache_num_lines(c)/get_cache_ways_associative(c));
92 }
93 inline size_t get_cache_lines_per_set(cache_t *c) {
94         return (get_cache_ways_associative(c));
95 }
96 inline size_t get_cache_lines_per_page(cache_t *c) {
97         return (PGSIZE / get_cache_bytes_per_line(c));
98 }
99 inline size_t get_cache_bytes_per_way(cache_t *c) {
100         return (get_cache_size_bytes(c)/get_cache_ways_associative(c));
101
102 inline size_t get_cache_lines_per_way(cache_t *c) {
103         return (get_cache_num_lines(c)/get_cache_ways_associative(c));
104
105 inline size_t get_cache_pages_per_way(cache_t *c) {
106         return (get_cache_lines_per_way(c)/get_cache_lines_per_page(c));
107 }
108 inline size_t get_cache_num_page_colors(cache_t *c) {
109         return get_cache_pages_per_way(c);
110 }