3 * modules/buffer/kernel_operations.h
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.
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.
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.
19 * Copyright (C) Samsung Electronics, 2013
21 * 2013 Alexander Aksenov <a.aksenov@samsung.com>: SWAP Buffer implement
25 /* Kernel functions wrap */
27 #ifndef __KERNEL_OPERATIONS_H__
28 #define __KERNEL_OPERATIONS_H__
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>
39 #include "data_types.h"
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)
59 /* Spinlocks initialization */
60 static inline void sync_init(struct sync_t *buffer_sync)
62 spin_lock_init(&buffer_sync->spinlock);
66 static inline void sync_lock(struct sync_t *buffer_sync)
68 spin_lock_irqsave(&buffer_sync->spinlock, buffer_sync->flags);
72 static inline void sync_unlock(struct sync_t *buffer_sync)
74 spin_unlock_irqrestore(&buffer_sync->spinlock, buffer_sync->flags);
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)
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))
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))
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)
101 /* Functions for pages allocation */
102 static inline unsigned int nearest_power_of_two(unsigned int number)
104 unsigned int result = 0;
105 unsigned int two_to_the_power = 1;
107 /* If aligned_size == PAGE_SIZE we need only one page, so return 0 */
111 while (two_to_the_power < number) {
112 two_to_the_power <<= 1;
119 static inline unsigned int get_order_for_alloc_pages(size_t memory_size)
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;
126 /* Align memory_size to the PAGE_SIZE. aligned_size >= memory_size */
127 size_t aligned_size = memory_size + (PAGE_SIZE - remainder);
129 return nearest_power_of_two(aligned_size / PAGE_SIZE);
132 #endif /* __KERNEL_OPERATIONS_H__ */