Added page-coloring support to SPARC port
[akaros.git] / kern / include / colored_page_alloc.h
1 /*
2  * Copyright (c) 2009 The Regents of the University  of California.  
3  * See the COPYRIGHT files at the top of this source tree for full 
4  * license information.
5  */
6 /**
7  * @author Kevin Klues <klueska@cs.berkeley.edu>
8  */
9  
10 #ifndef ROS_KERN_COLORED_PAGE_ALLOC_H
11 #define ROS_KERN_COLORED_PAGE_ALLOC_H
12
13 #include <colored_caches.h>
14 #include <arch/colored_page_alloc.h>
15 #include <stdio.h>
16         
17 #define DECLARE_CACHE_COLORED_PAGE_LINK(_cache)                               \
18         page_list_entry_t _cache##_cache_colored_page_link;
19         
20 #define DECLARE_CACHE_COLORED_PAGE_FREE_LIST(_cache)                          \
21         page_list_t *COUNT(npages) _cache##_cache_colored_page_list = NULL;
22         
23 #define DECLARE_EXTERN_CACHE_COLORED_PAGE_FREE_LIST(_cache)                   \
24         extern page_list_t *COUNT(npages) _cache##_cache_colored_page_list;
25         
26 #define DECLARE_CACHE_COLORED_PAGE_ALLOC_FUNCTION(_cache)                     \
27 error_t _cache##_page_alloc(page_t** page, size_t color)                      \
28 {                                                                             \
29         /*      TODO: Put a lock around this */                                       \
30         if(available_caches._cache && !LIST_EMPTY(&(_cache##_cache_colored_page_list)[(color)])) {           \
31                 *(page) = LIST_FIRST(&(_cache##_cache_colored_page_list)[(color)]);   \
32                 LIST_REMOVE(*page, global_link);                                      \
33                 REMOVE_CACHE_COLORING_PAGE_FROM_FREE_LISTS(page);                     \
34                 page_clear(*page);                                                    \
35                 return ESUCCESS;                                                      \
36         }                                                                         \
37         return -ENOMEM;                                                           \
38 }
39
40 #define INIT_CACHE_COLORED_PAGE_FREE_LIST(_cache)                             \
41 {                                                                             \
42         if(available_caches._cache == TRUE) {                                     \
43             uint8_t num_colors = get_cache_num_page_colors(&(_cache));            \
44             size_t list_size = num_colors*sizeof(page_list_t);                    \
45             _cache##_cache_colored_page_list                                      \
46                = (page_list_t*) boot_alloc(list_size, PGSIZE);                    \
47                 for(int i=0; i<num_colors; i++) {                                     \
48                         LIST_INIT(&(_cache##_cache_colored_page_list[i]));                \
49                 }                                                                     \
50         }                                                                         \
51 }
52
53 #define REMOVE_CACHE_COLORING_PAGE_FROM_FREE_LIST(_page, _cache)              \
54         if(available_caches._cache == TRUE)                                       \
55                 LIST_REMOVE(*(_page), _cache##_cache_colored_page_link);
56
57
58 #define INSERT_CACHE_COLORING_PAGE_ONTO_FREE_LIST(_page, _cache)              \
59         if(available_caches._cache == TRUE) {                                     \
60                 LIST_INSERT_HEAD(                                                     \
61                    &(_cache##_cache_colored_page_list                                 \
62                          [get_page_color(page2ppn((_page)), &(_cache))]),             \
63                    (_page),                                                           \
64                    _cache##_cache_colored_page_link                                   \
65                 );                                                                    \
66         }
67
68 #endif // ROS_KERN_COLORED_PAGE_ALLOC_H