tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / kernel / swap / driver / driver_to_buffer.c
1 /**
2  * driver/driver_to_buffer.c
3  * @author Alexander Aksenov <a.aksenov@samsung.com>
4  *
5  * @section LICENSE
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * @section COPYRIGHT
22  *
23  * Copyright (C) Samsung Electronics, 2013
24  *
25  * @section DESCRIPTION
26  *
27  * Driver and buffer interaction interface implementation.
28  */
29
30 #include <linux/string.h>
31 #include <linux/slab.h>
32 #include <linux/splice.h>
33 #include <linux/uaccess.h>
34 #include <linux/spinlock.h>
35 #include <linux/mm.h>
36
37 #include <buffer/swap_buffer_module.h>
38 #include <buffer/swap_buffer_errors.h>
39 #include <buffer/buffer_description.h>
40
41 #include "driver_defs.h"
42 #include "swap_driver_errors.h"
43 #include "device_driver_to_driver_to_buffer.h"
44 #include "app_manage.h"
45
46 /* Current busy buffer */
47 static struct swap_subbuffer *busy_buffer;
48
49 /* Buffers count ready to be read */
50 static int buffers_to_read;
51
52 /* Pages count in one subbuffer */
53 static int pages_per_buffer;
54
55 /* Used to sync changes of the buffers_to_read var */
56 static spinlock_t buf_to_read;
57
58
59 static inline void init_buffers_to_read(void)
60 {
61         spin_lock_init(&buf_to_read);
62         buffers_to_read = 0;
63 }
64
65 static inline void inc_buffers_to_read(void)
66 {
67         unsigned long flags;
68
69         spin_lock_irqsave(&buf_to_read, flags);
70         buffers_to_read++;
71         spin_unlock_irqrestore(&buf_to_read, flags);
72 }
73
74 static inline void dec_buffers_to_read(void)
75 {
76         unsigned long flags;
77
78         spin_lock_irqsave(&buf_to_read, flags);
79         buffers_to_read--;
80         spin_unlock_irqrestore(&buf_to_read, flags);
81 }
82
83 static inline void set_buffers_to_read(int count)
84 {
85         unsigned long flags;
86
87         spin_lock_irqsave(&buf_to_read, flags);
88         buffers_to_read = count;
89         spin_unlock_irqrestore(&buf_to_read, flags);
90 }
91
92 static inline int something_to_read(void)
93 {
94         unsigned long flags;
95         int result;
96
97         spin_lock_irqsave(&buf_to_read, flags);
98         result = buffers_to_read;
99         spin_unlock_irqrestore(&buf_to_read, flags);
100
101         return result;
102 }
103
104 /* TODO Get subbuffer for reading */
105 static size_t driver_to_buffer_get(void)
106 {
107         int result;
108
109         /* If there is no readable buffers, return error */
110         result = swap_buffer_get(&busy_buffer);
111         if (result == -E_SB_NO_READABLE_BUFFERS) {
112                 busy_buffer = NULL;
113                 return -E_SD_NO_DATA_TO_READ;
114         } else if (result < 0) {
115                 print_err("swap_buffer_get unhandle error %d\n", result);
116                 return -E_SD_BUFFER_ERROR;
117         }
118
119         return busy_buffer->full_buffer_part;
120 }
121
122 /* TODO Release subbuffer */
123 static int driver_to_buffer_release(void)
124 {
125         int result;
126
127         if (!busy_buffer)
128                 return -E_SD_NO_BUSY_SUBBUFFER;
129
130         result = swap_buffer_release(&busy_buffer);
131         if (result == -E_SB_NO_SUBBUFFER_IN_BUSY) {
132                 return -E_SD_WRONG_SUBBUFFER_PTR;
133         } else if (result < 0) {
134                 print_err("swap_buffer_release unhandle error %d\n", result);
135                 return -E_SD_BUFFER_ERROR;
136         }
137
138         busy_buffer = NULL;
139
140         return E_SD_SUCCESS;
141 }
142
143 static int driver_to_buffer_callback(bool wakeup)
144 {
145         /* Increment buffers_to_read counter */
146         inc_buffers_to_read();
147         if (wakeup)
148                 swap_device_wake_up_process();
149
150         return E_SD_SUCCESS;
151 }
152
153 /**
154  * @brief Copies data from subbuffer to userspace.
155  *
156  * @param[out] buf Pointer to userspace memory area whereto copy data from
157  * subbuffer.
158  * @param count Size of data to be read.
159  * @return Read data size on success, negative error code on error.
160  */
161 ssize_t driver_to_buffer_read(char __user *buf, size_t count)
162 {
163         size_t bytes_to_copy;
164         size_t bytes_to_read = 0;
165         int page_counter = 0;
166
167         /* Reading from swap_device means reading only current busy_buffer.
168          * So, if there is no busy_buffer, we don't get next to read, we just
169          * read nothing. In this case, or if there is nothing to read from
170          * busy_buffer - return -E_SD_NO_DATA_TO_READ. It should be correctly
171          * handled in device_driver */
172         if (!busy_buffer || !busy_buffer->full_buffer_part)
173                 return -E_SD_NO_DATA_TO_READ;
174
175         /* Bytes count that we're going to copy to user buffer is equal to user
176          * buffer size or to subbuffer readable size whichever is less */
177         bytes_to_copy = (count > busy_buffer->full_buffer_part) ?
178                     busy_buffer->full_buffer_part : count;
179
180         /* Copy data from each page to buffer */
181         while (bytes_to_copy > 0) {
182                 /* Get size that should be copied from current page */
183                 size_t read_from_this_page =
184                         (bytes_to_copy > PAGE_SIZE) ? PAGE_SIZE
185                         : bytes_to_copy;
186
187                 /* Copy and add size to copied bytes count */
188
189                 /* TODO Check with more than one page */
190                 bytes_to_read += read_from_this_page -
191                          copy_to_user(
192                                  buf, page_address(busy_buffer->data_buffer) +
193                                  (sizeof(struct page *) *
194                                   page_counter),
195                                  read_from_this_page);
196                 bytes_to_copy -= read_from_this_page;
197                 page_counter++;
198         }
199
200         return bytes_to_read;
201 }
202
203 /**
204  * @brief Flushes SWAP buffer.
205  *
206  * @return 0.
207  */
208 int driver_to_buffer_flush(void)
209 {
210         unsigned int flushed;
211
212         flushed = swap_buffer_flush();
213         set_buffers_to_read(flushed);
214         swap_device_wake_up_process();
215
216         return E_SD_SUCCESS;
217 }
218
219 /**
220  * @brief Fills spd structure.
221  *
222  * @param[out] spd Pointer to the splice_pipe_desc struct that should be filled.
223  * @return 0 on success, negative error code on error.
224  */
225 int driver_to_buffer_fill_spd(struct splice_pipe_desc *spd)
226 {
227         size_t data_to_splice = busy_buffer->full_buffer_part;
228         struct page **pages = spd->pages;
229         struct partial_page *partial = spd->partial;
230
231         while (data_to_splice) {
232                 size_t read_from_current_page = min(data_to_splice,
233                                                     (size_t)PAGE_SIZE);
234
235                 pages[spd->nr_pages] = alloc_page(GFP_KERNEL);
236                 if (!pages[spd->nr_pages]) {
237                         print_err("Cannot alloc page for splice\n");
238                         return -ENOMEM;
239                 }
240
241                 /* FIXME: maybe there is more efficient way */
242                 memcpy(page_address(pages[spd->nr_pages]),
243                page_address(&busy_buffer->data_buffer[spd->nr_pages]),
244                read_from_current_page);
245
246                 /* Always beginning of the page */
247                 partial[spd->nr_pages].offset = 0;
248                 partial[spd->nr_pages].len = read_from_current_page;
249
250                 /* Private is not used */
251                 partial[spd->nr_pages].private = 0;
252
253                 spd->nr_pages++;
254                 data_to_splice -= read_from_current_page;
255
256                 /* TODO: add check for pipe->buffers exceeding */
257                 /* if (spd->nr_pages == pipe->buffers) { */
258                 /*      break; */
259                 /* } */
260         }
261         return 0;
262 }
263
264 /**
265  * @brief Check for subbuffer ready to be read.
266  *
267  * @return 1 if there is subbuffer to be read, 0 - if there isn't.
268  */
269 int driver_to_buffer_buffer_to_read(void)
270 {
271         return busy_buffer ? 1 : 0;
272 }
273
274 /**
275  * @brief Initializes SWAP buffer.
276  *
277  * @param size Size of one subbuffer.
278  * @param count Count of subbuffers.
279  * @return 0 on success, negative error code on error.
280  */
281 int driver_to_buffer_initialize(size_t size, unsigned int count)
282 {
283         int result;
284         struct buffer_init_t buf_init = {
285                 .subbuffer_size = size,
286                 .nr_subbuffers = count,
287                 .subbuffer_full_cb = driver_to_buffer_callback,
288                 .lower_threshold = 20,
289                 .low_mem_cb = app_manage_pause_apps,
290                 .top_threshold = 80,
291                 .enough_mem_cb = app_manage_cont_apps,
292         };
293
294         if (size == 0 && count == 0)
295                 return -E_SD_WRONG_ARGS;
296
297         result = swap_buffer_init(&buf_init);
298         if (result == -E_SB_NO_MEM_QUEUE_BUSY
299                 || result == -E_SB_NO_MEM_BUFFER_STRUCT) {
300                 return -E_SD_NO_MEMORY;
301         }
302
303         /* TODO Race condition: buffer can be used in other thread till */
304         /* we're in this func */
305         /* Initialize driver_to_buffer variables */
306         pages_per_buffer = result;
307         busy_buffer = NULL;
308         init_buffers_to_read();
309
310         return E_SD_SUCCESS;
311 }
312
313 /**
314  * @brief Uninitializes buffer.
315  *
316  * @return 0 on success, negative error code on error.
317  */
318 int driver_to_buffer_uninitialize(void)
319 {
320         int result;
321
322         /* Release occupied buffer */
323         if (busy_buffer) {
324                 result = driver_to_buffer_release();
325                 /* TODO Maybe release anyway */
326                 if (result < 0)
327                         return result;
328                 busy_buffer = NULL;
329         }
330
331         result = swap_buffer_uninit();
332         if (result == -E_SB_UNRELEASED_BUFFERS) {
333                 print_err("Can't uninit buffer! There are busy subbuffers!\n");
334                 result = -E_SD_BUFFER_ERROR;
335         } else if (result < 0) {
336                 print_err("swap_buffer_uninit error %d\n", result);
337                 result = -E_SD_BUFFER_ERROR;
338         } else {
339                 result = E_SD_SUCCESS;
340         }
341
342         /* Reinit driver_to_buffer vars */
343         init_buffers_to_read();
344         pages_per_buffer = 0;
345
346         return result;
347 }
348
349 /**
350  * @brief Get next buffer to read.
351  *
352  * @return 0 on success, negative error code on error, E_SD_NO_DATA_TO_READ if
353  * there is nothing to be read.
354  */
355 int driver_to_buffer_next_buffer_to_read(void)
356 {
357         int result;
358
359         /* If there is busy_buffer first release it */
360         if (busy_buffer) {
361                 result = driver_to_buffer_release();
362                 if (result)
363                         return result;
364         }
365
366         /* If there is no buffers to read, return E_SD_NO_DATA_TO_READ.
367          * SHOULD BE POSITIVE, cause there is no real error. */
368         if (!something_to_read())
369                 return E_SD_NO_DATA_TO_READ;
370
371         /* Get next buffer to read */
372         result = driver_to_buffer_get();
373         if (result < 0) {
374                 print_err("buffer_to_reads > 0, but there are no buffers to read\n");
375                 return result;
376         }
377
378         /* Decrement buffers_to_read counter */
379         dec_buffers_to_read();
380
381         return E_SD_SUCCESS;
382 }