3 * modules/buffer/buffer_queue.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 queues implementation */
27 /* For all memory allocation/deallocation operations, except buffer memory
28 * allocation/deallocation should be used
29 * memory_allocation(size_t memory_size)
30 * memory_free(void *ptr)
32 * For subbuffer allocation/deallocation operations should be used
33 * buffer_allocation(size_t subbuffer_size)
34 * buffer_free(void *ptr, size_t subbuffer_size)
35 * To get buffer pointer for any usage, EXCEPT ALLOCATION AND DEALLOCATION
36 * use the following define:
37 * buffer_pointer(void *ptr_to_buffer_element_of_swap_buffer_structure)
38 * DO NOT USE SUBBUFFER PTR IN STRUCT SWAP_BUFFER WITHOUT THIS DEFINE!
39 * It will be ok for user space, but fail in kernel space.
41 * See space_dep_types_and_def.h for details */
45 #include "buffer_queue.h"
46 #include "swap_buffer_to_buffer_queue.h"
47 #include "swap_buffer_errors.h"
49 /* Queue structure. Consist of pointers to the first and the last elements of
52 struct swap_subbuffer *start_ptr;
53 struct swap_subbuffer *end_ptr;
54 struct sync_t queue_sync;
58 struct queue write_queue = {
67 struct queue read_queue = {
75 /* Pointers array. Points to busy buffers */
76 static struct swap_subbuffer **queue_busy = NULL;
78 /* Store last busy element */
79 static unsigned int queue_busy_last_element;
81 /* Subbuffers count */
82 static unsigned int queue_subbuffer_count = 0;
84 /* One subbuffer size */
85 static size_t queue_subbuffer_size = 0;
88 static struct sync_t buffer_busy_sync = {
92 /* Memory pages count in one subbuffer */
93 static int pages_order_in_subbuffer = 0;
96 int buffer_queue_allocation(size_t subbuffer_size,
97 unsigned int subbuffers_count)
101 unsigned int allocated_buffers = 0;
102 unsigned int allocated_structs = 0;
103 struct swap_subbuffer *clean_tmp_struct;
106 /* Static varibles initialization */
107 queue_subbuffer_size = subbuffer_size;
108 queue_subbuffer_count = subbuffers_count;
109 queue_busy_last_element = 0;
111 /* Set variable pages_in_subbuffer. It is used for allocation and
112 * deallocation memory pages and its value is returned from
113 * swap_buffer_get() and contains page count in one subbuffer.
114 * All this useful only in kernel space. In userspace it is dummy.*/
115 set_pages_order_in_subbuffer(queue_subbuffer_size);
116 /* Sync primitives initialization */
117 sync_init(&read_queue.queue_sync);
118 sync_init(&write_queue.queue_sync);
119 sync_init(&buffer_busy_sync);
121 /* Memory allocation for queue_busy */
122 queue_busy = memory_allocation(sizeof(&queue_busy) * queue_subbuffer_count);
125 result = -E_SB_NO_MEM_QUEUE_BUSY;
126 goto buffer_allocation_error_ret;
129 /* Memory allocation for swap_subbuffer structures */
131 /* Allocation for first structure. */
132 write_queue.start_ptr = memory_allocation(sizeof(&write_queue.start_ptr));
134 if (!write_queue.start_ptr) {
135 result = -E_SB_NO_MEM_BUFFER_STRUCT;
136 goto buffer_allocation_queue_busy_free;
141 write_queue.end_ptr = write_queue.start_ptr;
143 write_queue.end_ptr->next_in_queue = NULL;
144 write_queue.end_ptr->full_buffer_part = 0;
145 write_queue.end_ptr->data_buffer = buffer_allocation(queue_subbuffer_size);
146 if (!write_queue.end_ptr->data_buffer) {
147 print_err("Cannot allocate memory for buffer 1\n");
148 result = -E_SB_NO_MEM_DATA_BUFFER;
149 goto buffer_allocation_error_free;
153 print_msg(" Buffer allocated = 0x%x\n", (unsigned long)write_queue.end_ptr->data_buffer);
155 sync_init(&write_queue.end_ptr->buffer_sync);
157 /* Buffer initialization */
158 memset(buffer_address(write_queue.end_ptr->data_buffer), 0, queue_subbuffer_size);
160 /* Allocation for other structures. */
161 for (i = 1; i < queue_subbuffer_count; i++) {
162 write_queue.end_ptr->next_in_queue =
163 memory_allocation(sizeof(write_queue.end_ptr->next_in_queue));
164 if (!write_queue.end_ptr->next_in_queue) {
165 result = -E_SB_NO_MEM_BUFFER_STRUCT;
166 goto buffer_allocation_error_free;
170 /* Now next write_queue.end_ptr is next */
171 write_queue.end_ptr = write_queue.end_ptr->next_in_queue;
173 write_queue.end_ptr->next_in_queue = NULL;
174 write_queue.end_ptr->full_buffer_part = 0;
175 write_queue.end_ptr->data_buffer =
176 buffer_allocation(queue_subbuffer_size);
177 if (!write_queue.end_ptr->data_buffer) {
178 result = -E_SB_NO_MEM_DATA_BUFFER;
179 goto buffer_allocation_error_free;
183 print_msg(" Buffer allocated = 0x%x, pages_order = %d\n",
184 (unsigned long)buffer_address(write_queue.end_ptr->data_buffer),
185 pages_order_in_subbuffer);
187 sync_init(&write_queue.end_ptr->buffer_sync);
189 /* Buffer initialization */
190 memset(buffer_address(write_queue.end_ptr->data_buffer), 0,
191 queue_subbuffer_size);
196 /* In case of errors, this code is called */
197 /* Free all previously allocated memory */
198 buffer_allocation_error_free:
199 clean_tmp_struct = write_queue.start_ptr;
201 for (j = 0; j < allocated_structs; j++) {
202 clean_tmp_struct = write_queue.start_ptr;
203 if (allocated_buffers) {
204 buffer_free(clean_tmp_struct->data_buffer, queue_subbuffer_size);
207 if (write_queue.start_ptr != write_queue.end_ptr)
208 write_queue.start_ptr = write_queue.start_ptr->next_in_queue;
209 memory_free(clean_tmp_struct);
211 write_queue.end_ptr = NULL;
212 write_queue.start_ptr = NULL;
214 buffer_allocation_queue_busy_free:
215 memory_free(queue_busy);
218 buffer_allocation_error_ret:
222 void buffer_queue_free(void)
224 struct swap_subbuffer *tmp = NULL;
226 //TODO Lock read list semaphore to prevent getting subbuffer from read list
227 /* Set all write buffers to read list */
228 set_all_to_read_list();
230 /* Free buffers and structures memory that are in read list */
231 while (read_queue.start_ptr) {
232 tmp = read_queue.start_ptr;
233 read_queue.start_ptr = read_queue.start_ptr->next_in_queue;
234 buffer_free(tmp->data_buffer, queue_subbuffer_size);
235 print_msg(" Buffer free = 0x%x\n", (unsigned long)
236 buffer_address(tmp->data_buffer));
241 memory_free(queue_busy);
244 queue_subbuffer_size = 0;
245 queue_subbuffer_count = 0;
246 read_queue.start_ptr = NULL;
247 read_queue.end_ptr = NULL;
248 write_queue.start_ptr = NULL;
249 write_queue.end_ptr = NULL;
252 static unsigned int is_buffer_enough(struct swap_subbuffer *subbuffer,
255 /* XXX Think about checking full_buffer_part for correctness
256 * (<queue_subbuffer_size). It should be true, but if isn't (due to sources
257 * chaning, etc.) this function should be true! */
258 return ((queue_subbuffer_size-subbuffer->full_buffer_part) >= size) ? 1 : 0;
261 /* Get first subbuffer from read list */
262 struct swap_subbuffer *get_from_read_list(void)
264 struct swap_subbuffer *result = NULL;
266 /* Lock read sync primitive */
267 sync_lock(&read_queue.queue_sync);
269 if (read_queue.start_ptr == NULL) {
271 goto get_from_read_list_unlock;
274 result = read_queue.start_ptr;
276 /* If this is the last readable buffer, read_queue.start_ptr next time will
277 * points to NULL and that case is handled in the beginning of function
279 if (read_queue.start_ptr == read_queue.end_ptr) {
280 read_queue.end_ptr = NULL;
282 read_queue.start_ptr = read_queue.start_ptr->next_in_queue;
284 get_from_read_list_unlock:
285 /* Unlock read sync primitive */
286 sync_unlock(&read_queue.queue_sync);
291 /* Add subbuffer to read list */
292 void add_to_read_list(struct swap_subbuffer *subbuffer)
295 /* Lock read sync primitive */
296 sync_lock(&read_queue.queue_sync);
298 if (!read_queue.start_ptr)
299 read_queue.start_ptr = subbuffer;
301 if (read_queue.end_ptr) {
302 read_queue.end_ptr->next_in_queue = subbuffer;
304 read_queue.end_ptr = read_queue.end_ptr->next_in_queue;
306 read_queue.end_ptr = subbuffer;
308 read_queue.end_ptr->next_in_queue = NULL;
310 /* Unlock read sync primitive */
311 sync_unlock(&read_queue.queue_sync);
314 /* Call add to read list and callback function from driver module */
315 int add_to_read_list_with_callback(struct swap_subbuffer *subbuffer)
319 add_to_read_list(subbuffer);
320 // TODO Handle ret value
321 result = swap_buffer_callback(subbuffer);
326 /* Get first writable subbuffer from write list */
327 struct swap_subbuffer *get_from_write_list(size_t size, void **ptr_to_write)
329 struct swap_subbuffer *result = NULL;
331 /* Callbacks are called at the end of the function to prevent deadlocks */
332 struct queue callback_queue = {
339 struct swap_subbuffer *tmp_buffer = NULL;
342 *ptr_to_write = NULL;
344 /* Lock write list sync primitive */
345 sync_lock(&write_queue.queue_sync);
347 while (write_queue.start_ptr) {
348 /* If start points to NULL => list is empty => exit */
349 if (!write_queue.start_ptr) {
351 goto get_from_write_list_unlock;
354 /* We're found subbuffer */
355 if (is_buffer_enough(write_queue.start_ptr, size)) {
357 result = write_queue.start_ptr;
358 *ptr_to_write = (void *)((unsigned long)
359 (buffer_address(result->data_buffer)) +
360 result->full_buffer_part);
362 /* Add data size to full_buffer_part. Very important to do it in
363 * write_queue.queue_sync spinlock */
364 write_queue.start_ptr->full_buffer_part += size;
366 /* Lock rw sync. Should be unlocked in swap_buffer_write() */
367 sync_lock(&result->buffer_sync);
369 /* This subbuffer is not enough => it goes to read list */
371 result = write_queue.start_ptr;
373 /* If we reached end of the list */
374 if (write_queue.start_ptr == write_queue.end_ptr) {
375 write_queue.end_ptr = NULL;
378 /* Move start write pointer */
379 write_queue.start_ptr = write_queue.start_ptr->next_in_queue;
381 /* Add to callback list */
382 if (!callback_queue.start_ptr)
383 callback_queue.start_ptr = result;
385 if (callback_queue.end_ptr)
386 callback_queue.end_ptr->next_in_queue = result;
387 callback_queue.end_ptr = result;
388 callback_queue.end_ptr->next_in_queue = NULL;
393 get_from_write_list_unlock:
394 /* Unlock write list sync primitive */
395 sync_unlock(&write_queue.queue_sync);
397 /* Adding buffers to read list and calling callbacks */
398 for (tmp_buffer = NULL; callback_queue.start_ptr; ) {
399 if (callback_queue.start_ptr == callback_queue.end_ptr)
400 callback_queue.end_ptr = NULL;
402 tmp_buffer = callback_queue.start_ptr;
403 callback_queue.start_ptr = callback_queue.start_ptr->next_in_queue;
405 add_to_read_list_with_callback(tmp_buffer);
411 /* Add subbuffer to write list */
412 void add_to_write_list(struct swap_subbuffer *subbuffer)
414 sync_lock(&write_queue.queue_sync);
417 // TODO Useless memset
418 // memset(buffer_address(subbuffer->data_buffer), 0, queue_subbuffer_size);
419 subbuffer->full_buffer_part = 0;
421 if (!write_queue.start_ptr)
422 write_queue.start_ptr = subbuffer;
424 if (write_queue.end_ptr) {
425 write_queue.end_ptr->next_in_queue = subbuffer;
426 write_queue.end_ptr = write_queue.end_ptr->next_in_queue;
428 write_queue.end_ptr = subbuffer;
430 write_queue.end_ptr->next_in_queue = NULL;
432 sync_unlock(&write_queue.queue_sync);
435 /* Add subbuffer to busy list when it is read from out of the buffer */
436 void add_to_busy_list(struct swap_subbuffer *subbuffer)
438 /* Lock busy sync primitive */
439 sync_lock(&buffer_busy_sync);
441 subbuffer->next_in_queue = NULL;
442 queue_busy[queue_busy_last_element] = subbuffer;
443 queue_busy_last_element += 1;
445 /* Unlock busy sync primitive */
446 sync_unlock(&buffer_busy_sync);
449 /* Remove subbuffer from busy list when it is released */
450 int remove_from_busy_list(struct swap_subbuffer *subbuffer)
452 int result = -E_SB_NO_SUBBUFFER_IN_BUSY; // For sanitization
455 /* Lock busy list sync primitive */
456 sync_lock(&buffer_busy_sync);
458 /* Sanitization and removing */
459 for (i = 0; i < queue_busy_last_element; i++) {
460 if (queue_busy[i] == subbuffer) {
461 /* Last element goes here and length is down 1 */
462 queue_busy[i] = queue_busy[queue_busy_last_element - 1];
463 queue_busy_last_element -= 1;
464 result = E_SB_SUCCESS;
469 /* Unlock busy list sync primitive */
470 sync_unlock(&buffer_busy_sync);
475 /* Get subbuffers count in read list */
476 /* XXX Think about locks */
477 int get_full_buffers_count(void)
480 struct swap_subbuffer *buffer = read_queue.start_ptr;
482 while (buffer && buffer->full_buffer_part) {
484 buffer = buffer->next_in_queue;
490 /* Set all subbuffers in write list to read list */
491 void set_all_to_read_list(void)
493 struct swap_subbuffer *buffer = write_queue.start_ptr;
495 /* Locking write sync primitive */
496 sync_lock(&write_queue.queue_sync);
498 while (write_queue.start_ptr &&
499 write_queue.start_ptr->full_buffer_part) {
500 /* Waiting till semaphore should be posted */
502 // TODO To think: It's not bad as it is, but maybe it would be better locking
503 // semaphore while changing its list? (Not bad now, cause buffer should have
504 // already been stopped).
506 sync_lock(&buffer->buffer_sync);
508 sync_unlock(&buffer->buffer_sync);
510 buffer = write_queue.start_ptr;
512 /* If we reached end of the list */
513 if (write_queue.start_ptr == write_queue.end_ptr) {
514 write_queue.end_ptr = NULL;
516 write_queue.start_ptr = write_queue.start_ptr->next_in_queue;
518 add_to_read_list(buffer);
521 /* Unlocking write primitive */
522 sync_unlock(&write_queue.queue_sync);
525 /* Get subbuffers count in busy list */
526 /* XXX Think abount lock */
527 int get_busy_buffers_count(void)
529 return queue_busy_last_element;
532 /* Get memory pages count in subbuffer */
533 int get_pages_count_in_subbuffer(void)
535 /* Return 1 if pages order 0, or 2 of power pages_order_in_subbuffer otherwise */
536 return (pages_order_in_subbuffer) ? 2 << (pages_order_in_subbuffer - 1) : 1;