3 * modules/buffer/swap_buffer_module.c
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 /* SWAP Buffer interface implementation */
27 #include "swap_buffer_module.h"
28 #include "buffer_queue.h"
29 #include "buffer_description.h"
30 #include "swap_buffer_errors.h"
31 #include "kernel_operations.h"
33 /* Bitwise mask for buffer status */
34 enum _swap_buffer_status_mask {
41 /* Buffer status masks:
43 * 1 - memory allocated
44 * 10 - buffer overflow
47 static unsigned char swap_buffer_status = BUFFER_FREE;
50 typedef int(*subbuffer_callback_type)(void);
52 /* Callback that is called when full subbuffer appears */
53 static subbuffer_callback_type subbuffer_callback = NULL;
55 /* One subbuffer size */
56 static size_t subbuffers_size = 0;
58 /* Subbuffers count */
59 static unsigned int subbuffers_num = 0;
61 static unsigned int enough_writable_bufs = 0;
62 static unsigned int min_writable_bufs = 0;
63 static int (*low_mem_cb)(void) = NULL;
64 static int (*enough_mem_cb)(void) = NULL;
67 static inline int areas_overlap(const void *area1,const void *area2, size_t size)
71 for (i = 0; i < size; i++)
72 if ((area1 + i == area2) || (area2 + i == area1))
78 static inline unsigned int percent_to_count(unsigned char percent,
81 return (percent * cnt) / 100;
84 int swap_buffer_init(struct buffer_init_t *buf_init)
88 swap_buffer_status &= ~BUFFER_WORK;
89 print_debug("status buffer stop = %d\n", swap_buffer_status);
91 if ((buf_init->top_threshold > 100) || (buf_init->lower_threshold > 100) ||
92 (buf_init->top_threshold < buf_init->lower_threshold))
93 return -E_SB_WRONG_THRESHOLD;
95 min_writable_bufs = percent_to_count(buf_init->lower_threshold,
96 buf_init->nr_subbuffers);
98 enough_writable_bufs = percent_to_count(buf_init->top_threshold,
99 buf_init->nr_subbuffers);
101 low_mem_cb = buf_init->low_mem_cb;
102 enough_mem_cb = buf_init->enough_mem_cb;
104 if ((swap_buffer_status & BUFFER_ALLOC) &&
105 (subbuffers_size == buf_init->subbuffer_size) &&
106 (subbuffers_num == buf_init->nr_subbuffers) &&
107 ((subbuffer_callback_type)subbuffer_callback ==
108 buf_init->subbuffer_full_cb)) {
109 result = buffer_queue_reset();
110 goto swap_buffer_init_work;
113 subbuffer_callback = buf_init->subbuffer_full_cb;
114 subbuffers_size = buf_init->subbuffer_size;
115 subbuffers_num = buf_init->nr_subbuffers;
117 result = buffer_queue_allocation(subbuffers_size, subbuffers_num);
121 result = get_pages_count_in_subbuffer();
123 swap_buffer_status |= BUFFER_ALLOC;
124 print_debug("status buffer alloc = %d\n", swap_buffer_status);
126 swap_buffer_init_work:
127 swap_buffer_status |= BUFFER_WORK;
128 print_debug("status buffer work = %d\n", swap_buffer_status);
132 EXPORT_SYMBOL_GPL(swap_buffer_init);
135 int swap_buffer_uninit(void)
137 /* Check whether buffer is allocated */
138 if (!(swap_buffer_status & BUFFER_ALLOC))
139 return -E_SB_NOT_ALLOC;
142 swap_buffer_status &= ~BUFFER_WORK;
143 print_debug("status buffer stop = %d\n", swap_buffer_status);
145 /* Check whether all buffers are released */
146 if (get_busy_buffers_count())
147 return -E_SB_UNRELEASED_BUFFERS;
152 subbuffer_callback = NULL;
155 min_writable_bufs = 0;
156 enough_writable_bufs = 0;
158 enough_mem_cb = NULL;
160 swap_buffer_status &= ~BUFFER_ALLOC;
161 print_debug("status buffer dealloc = %d\n", swap_buffer_status);
165 EXPORT_SYMBOL_GPL(swap_buffer_uninit);
168 ssize_t swap_buffer_write(void *data, size_t size)
170 int result = E_SB_SUCCESS;
171 struct swap_subbuffer *buffer_to_write = NULL;
172 void *ptr_to_write = NULL;
174 /* Size sanitization */
175 if ((size > subbuffers_size) || (size == 0))
176 return -E_SB_WRONG_DATA_SIZE;
178 /* Check buffer status */
179 if (!(swap_buffer_status & BUFFER_WORK))
180 return -E_SB_IS_STOPPED;
182 /* Get next write buffer and occupying semaphore */
183 buffer_to_write = get_from_write_list(size, &ptr_to_write);
184 if (!buffer_to_write)
185 return -E_SB_NO_WRITABLE_BUFFERS;
187 /* Check for overlapping */
188 if (areas_overlap(ptr_to_write, data, size)) {
189 result = -E_SB_OVERLAP;
190 goto buf_write_sem_post;
193 /* Copy data to buffer */
194 /* XXX Think of using memmove instead - useless, anyway overlapping means
195 * that something went wrong. */
196 memcpy(ptr_to_write, data, size);
200 if ((get_writable_buf_cnt() < min_writable_bufs) &&
201 !(swap_buffer_status & BUFFER_PAUSE)) {
202 swap_buffer_status |= BUFFER_PAUSE;
203 if (low_mem_cb != NULL)
207 /* Unlock sync (Locked in get_from_write_list()) */
209 sync_unlock(&buffer_to_write->buffer_sync);
213 EXPORT_SYMBOL_GPL(swap_buffer_write);
216 int swap_buffer_get(struct swap_subbuffer **subbuffer)
219 struct swap_subbuffer *buffer_to_read = NULL;
221 /* Check buffer status */
222 if (!(swap_buffer_status & BUFFER_WORK))
223 return -E_SB_IS_STOPPED;
225 /* Get next read buffer */
226 buffer_to_read = get_from_read_list();
228 return -E_SB_NO_READABLE_BUFFERS;
230 /* Add to busy list */
231 buffer_to_read->next_in_queue = NULL;
232 add_to_busy_list(buffer_to_read);
234 *subbuffer = buffer_to_read;
236 result = get_pages_count_in_subbuffer();
240 EXPORT_SYMBOL_GPL(swap_buffer_get);
243 int swap_buffer_release(struct swap_subbuffer **subbuffer)
247 /* Remove from busy list (includes sanitization) */
248 result = remove_from_busy_list(*subbuffer);
252 /* Add to write list */
253 add_to_write_list(*subbuffer);
255 if ((swap_buffer_status & BUFFER_PAUSE) &&
256 (get_writable_buf_cnt() >= enough_writable_bufs)) {
257 swap_buffer_status &= ~BUFFER_PAUSE;
258 if (enough_mem_cb != NULL)
264 EXPORT_SYMBOL_GPL(swap_buffer_release);
267 unsigned int swap_buffer_flush(void)
271 /* Set all non-empty write buffers to read list */
272 buffer_queue_flush();
274 /* Get count of all full buffers */
275 result = get_readable_buf_cnt();
279 EXPORT_SYMBOL_GPL(swap_buffer_flush);
282 int swap_buffer_callback(void *buffer)
286 if (!subbuffer_callback) {
287 return -E_SB_NO_CALLBACK;
290 result = subbuffer_callback();
292 print_err("Callback error! Error code: %d\n", result);
297 static int __init swap_buffer_module_init(void)
299 printk(KERN_NOTICE "SWAP_BUFFER : Buffer module initialized\n");
303 static void __exit swap_buffer_module_exit(void)
305 if (swap_buffer_status & BUFFER_ALLOC)
306 swap_buffer_uninit();
307 printk(KERN_NOTICE "SWAP_BUFFER : Buffer module unintialized\n");
310 module_init(swap_buffer_module_init);
311 module_exit(swap_buffer_module_exit);
313 MODULE_LICENSE("GPL");
314 MODULE_DESCRIPTION("SWAP buffer module");
315 MODULE_AUTHOR("Aksenov A.S.");