[FIX] Ksyms: Fix warnings
[kernel/swap-modules.git] / buffer / kernel_operations.h
1 /*
2  *  SWAP Buffer Module
3  *  modules/buffer/kernel_operations.h
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013  Alexander Aksenov <a.aksenov@samsung.com>: SWAP Buffer implement
22  *
23  */
24
25 /* Kernel functions wrap */
26
27 #ifndef __KERNEL_OPERATIONS_H__
28 #define __KERNEL_OPERATIONS_H__
29
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/semaphore.h>
33 #include <linux/spinlock.h>
34 #include <linux/string.h>
35 #include <linux/kernel.h>
36 #include <linux/gfp.h>
37 #include <linux/mm.h>
38
39 #include "data_types.h"
40
41
42 /* MESSAGES */
43 #define print_debug(msg, args...) \
44         printk(KERN_DEBUG "SWAP_BUFFER DEBUG : " msg, ##args)
45 #define print_msg(msg, args...)   \
46         printk(KERN_INFO "SWAP_BUFFER : " msg, ##args)
47 #define print_warn(msg, args...)  \
48         printk(KERN_WARNING "SWAP_BUFFER WARNING : " msg, ##args)
49 #define print_err(msg, args...)   \
50         printk(KERN_ERR "SWAP_BUFFER ERROR : " msg, ##args)
51 #define print_crit(msg, args...)  \
52         printk(KERN_CRIT "SWAP_BUFFER CRITICAL : " msg, ##args)
53
54
55
56
57 /* LOCKS */
58
59 /* Spinlocks initialization */
60 static inline void sync_init(struct sync_t *buffer_sync)
61 {
62         spin_lock_init(&buffer_sync->spinlock);
63 }
64
65 /* Lock spinlock */
66 static inline void sync_lock(struct sync_t *buffer_sync)
67 {
68         spin_lock_irqsave(&buffer_sync->spinlock, buffer_sync->flags);
69 }
70
71 /* Unlock spinlock */
72 static inline void sync_unlock(struct sync_t *buffer_sync)
73 {
74         spin_unlock_irqrestore(&buffer_sync->spinlock, buffer_sync->flags);
75 }
76
77
78 /* SWAP SUBBUFER */
79
80
81 /* We alloc memory for swap_subbuffer structures with common kmalloc */
82 #define memory_allocation(memory_size)  kmalloc(memory_size, GFP_KERNEL)
83 #define memory_free(ptr)                kfree(ptr)
84
85 /* For subbuffers themselves, we allocate memory with alloc_pages, so, we have
86  * to evaluate required pages order */
87 #define buffer_allocation(memory_size)                        \
88         alloc_pages(GFP_KERNEL, (pages_order_in_subbuffer >= 0) ? \
89                 pages_order_in_subbuffer :                            \
90                 get_order_for_alloc_pages(memory_size))
91
92 #define buffer_free(ptr, subbuf_size)                         \
93         __free_pages(ptr, (pages_order_in_subbuffer >= 0) ?       \
94                  pages_order_in_subbuffer :                           \
95                  get_order_for_alloc_pages(subbuf_size))
96
97 #define buffer_address(buffer_ptr)  page_address(buffer_ptr)
98 #define set_pages_order_in_subbuffer(memory_size) \
99         pages_order_in_subbuffer = get_order_for_alloc_pages(memory_size)
100
101 /* Functions for pages allocation */
102 static inline unsigned int nearest_power_of_two(unsigned int number)
103 {
104         unsigned int result = 0;
105         unsigned int two_to_the_power = 1;
106
107         /* If aligned_size == PAGE_SIZE we need only one page, so return 0 */
108         if (number == 1)
109                 return result;
110
111         while (two_to_the_power < number) {
112                 two_to_the_power <<= 1;
113                 result++;
114         }
115
116         return result;
117 }
118
119 static inline unsigned int get_order_for_alloc_pages(size_t memory_size)
120 {
121         /* First evaluate remainder of the division memory_size by PAGE_SIZE.
122          * If memory_size is divisible by PAGE_SIZE, then remainder equals 0. */
123         size_t remainder = (memory_size % PAGE_SIZE) ?
124                        (memory_size % PAGE_SIZE) : PAGE_SIZE;
125
126         /* Align memory_size to the PAGE_SIZE. aligned_size >= memory_size */
127         size_t aligned_size = memory_size + (PAGE_SIZE - remainder);
128
129         return nearest_power_of_two(aligned_size / PAGE_SIZE);
130 }
131
132 #endif /* __KERNEL_OPERATIONS_H__ */