tty: serial: mxs-auart: Check the return value from clk_prepare_enable()
[linux-2.6-block.git] / drivers / tty / tty_buffer.c
CommitLineData
e0495736
AC
1/*
2 * Tty buffer allocation management
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
10#include <linux/timer.h>
11#include <linux/string.h>
12#include <linux/slab.h>
13#include <linux/sched.h>
14#include <linux/init.h>
15#include <linux/wait.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/module.h>
593fb1ae 19#include <linux/ratelimit.h>
e0495736 20
1cef50e3
PH
21
22#define MIN_TTYB_SIZE 256
23#define TTYB_ALIGN_MASK 255
24
7bfe0b71
PH
25/*
26 * Byte threshold to limit memory consumption for flip buffers.
27 * The actual memory limit is > 2x this amount.
28 */
4d18e6ef 29#define TTYB_DEFAULT_MEM_LIMIT 65536
7bfe0b71 30
9114fe8c
PH
31/*
32 * We default to dicing tty buffer allocations to this many characters
33 * in order to avoid multiple page allocations. We know the size of
34 * tty_buffer itself but it must also be taken into account that the
35 * the buffer is 256 byte aligned. See tty_buffer_find for the allocation
36 * logic this must match
37 */
38
39#define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
40
7bfe0b71 41
a7c8d58c
PH
42/**
43 * tty_buffer_lock_exclusive - gain exclusive access to buffer
44 * tty_buffer_unlock_exclusive - release exclusive access
45 *
46 * @port - tty_port owning the flip buffer
47 *
48 * Guarantees safe use of the line discipline's receive_buf() method by
49 * excluding the buffer work and any pending flush from using the flip
50 * buffer. Data can continue to be added concurrently to the flip buffer
51 * from the driver side.
52 *
53 * On release, the buffer work is restarted if there is data in the
54 * flip buffer
55 */
56
57void tty_buffer_lock_exclusive(struct tty_port *port)
58{
59 struct tty_bufhead *buf = &port->buf;
60
61 atomic_inc(&buf->priority);
62 mutex_lock(&buf->lock);
63}
64
65void tty_buffer_unlock_exclusive(struct tty_port *port)
66{
67 struct tty_bufhead *buf = &port->buf;
68 int restart;
69
70 restart = buf->head->commit != buf->head->read;
71
72 atomic_dec(&buf->priority);
73 mutex_unlock(&buf->lock);
74 if (restart)
75 queue_work(system_unbound_wq, &buf->work);
76}
77
7bfe0b71
PH
78/**
79 * tty_buffer_space_avail - return unused buffer space
80 * @port - tty_port owning the flip buffer
81 *
82 * Returns the # of bytes which can be written by the driver without
83 * reaching the buffer limit.
84 *
85 * Note: this does not guarantee that memory is available to write
86 * the returned # of bytes (use tty_prepare_flip_string_xxx() to
87 * pre-allocate if memory guarantee is required).
88 */
89
90int tty_buffer_space_avail(struct tty_port *port)
91{
5dda4ca5 92 int space = port->buf.mem_limit - atomic_read(&port->buf.mem_used);
7bfe0b71
PH
93 return max(space, 0);
94}
c4a8dab5 95EXPORT_SYMBOL_GPL(tty_buffer_space_avail);
7bfe0b71 96
9dd5139f
PH
97static void tty_buffer_reset(struct tty_buffer *p, size_t size)
98{
99 p->used = 0;
100 p->size = size;
101 p->next = NULL;
102 p->commit = 0;
103 p->read = 0;
104}
105
e0495736
AC
106/**
107 * tty_buffer_free_all - free buffers used by a tty
108 * @tty: tty to free from
109 *
110 * Remove all the buffers pending on a tty whether queued with data
111 * or in the free ring. Must be called when the tty is no longer in use
e0495736
AC
112 */
113
ecbbfd44 114void tty_buffer_free_all(struct tty_port *port)
e0495736 115{
ecbbfd44 116 struct tty_bufhead *buf = &port->buf;
809850b7
PH
117 struct tty_buffer *p, *next;
118 struct llist_node *llist;
5cff39c6 119
2cf7b67e
PH
120 while ((p = buf->head) != NULL) {
121 buf->head = p->next;
7391ee16
PH
122 if (p->size > 0)
123 kfree(p);
e0495736 124 }
809850b7
PH
125 llist = llist_del_all(&buf->free);
126 llist_for_each_entry_safe(p, next, llist, free)
2cf7b67e 127 kfree(p);
809850b7 128
7391ee16
PH
129 tty_buffer_reset(&buf->sentinel, 0);
130 buf->head = &buf->sentinel;
131 buf->tail = &buf->sentinel;
7bfe0b71 132
5dda4ca5 133 atomic_set(&buf->mem_used, 0);
e0495736
AC
134}
135
136/**
137 * tty_buffer_alloc - allocate a tty buffer
138 * @tty: tty device
139 * @size: desired size (characters)
140 *
141 * Allocate a new tty buffer to hold the desired number of characters.
11b9faa4
PH
142 * We round our buffers off in 256 character chunks to get better
143 * allocation behaviour.
e0495736
AC
144 * Return NULL if out of memory or the allocation would exceed the
145 * per device queue
e0495736
AC
146 */
147
ecbbfd44 148static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size)
e0495736 149{
809850b7 150 struct llist_node *free;
e0495736
AC
151 struct tty_buffer *p;
152
11b9faa4
PH
153 /* Round the buffer size out */
154 size = __ALIGN_MASK(size, TTYB_ALIGN_MASK);
155
156 if (size <= MIN_TTYB_SIZE) {
809850b7
PH
157 free = llist_del_first(&port->buf.free);
158 if (free) {
159 p = llist_entry(free, struct tty_buffer, free);
11b9faa4
PH
160 goto found;
161 }
162 }
163
164 /* Should possibly check if this fails for the largest buffer we
165 have queued and recycle that ? */
5dda4ca5 166 if (atomic_read(&port->buf.mem_used) > port->buf.mem_limit)
e0495736
AC
167 return NULL;
168 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
169 if (p == NULL)
170 return NULL;
9dd5139f 171
11b9faa4 172found:
9dd5139f 173 tty_buffer_reset(p, size);
5dda4ca5 174 atomic_add(size, &port->buf.mem_used);
e0495736
AC
175 return p;
176}
177
178/**
179 * tty_buffer_free - free a tty buffer
180 * @tty: tty owning the buffer
181 * @b: the buffer to free
182 *
183 * Free a tty buffer, or add it to the free list according to our
184 * internal strategy
e0495736
AC
185 */
186
ecbbfd44 187static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
e0495736 188{
ecbbfd44 189 struct tty_bufhead *buf = &port->buf;
5cff39c6 190
e0495736 191 /* Dumb strategy for now - should keep some stats */
5dda4ca5 192 WARN_ON(atomic_sub_return(b->size, &buf->mem_used) < 0);
e0495736 193
1cef50e3 194 if (b->size > MIN_TTYB_SIZE)
e0495736 195 kfree(b);
7391ee16 196 else if (b->size > 0)
809850b7 197 llist_add(&b->free, &buf->free);
e0495736
AC
198}
199
e0495736
AC
200/**
201 * tty_buffer_flush - flush full tty buffers
202 * @tty: tty to flush
203 *
753023dc 204 * flush all the buffers containing receive data.
e0495736 205 *
a7c8d58c 206 * Locking: takes buffer lock to ensure single-threaded flip buffer
e9975fde 207 * 'consumer'
e0495736
AC
208 */
209
210void tty_buffer_flush(struct tty_struct *tty)
211{
2fc20661 212 struct tty_port *port = tty->port;
ecbbfd44 213 struct tty_bufhead *buf = &port->buf;
47aa658a 214 struct tty_buffer *next;
e0495736 215
a7c8d58c 216 atomic_inc(&buf->priority);
e9975fde 217
a7c8d58c 218 mutex_lock(&buf->lock);
47aa658a
PH
219 while ((next = buf->head->next) != NULL) {
220 tty_buffer_free(port, buf->head);
221 buf->head = next;
222 }
223 buf->head->read = buf->head->commit;
a7c8d58c
PH
224 atomic_dec(&buf->priority);
225 mutex_unlock(&buf->lock);
e0495736
AC
226}
227
e0495736 228/**
64325a3b 229 * tty_buffer_request_room - grow tty buffer if needed
e0495736
AC
230 * @tty: tty structure
231 * @size: size desired
232 *
233 * Make at least size bytes of linear space available for the tty
234 * buffer. If we fail return the size we managed to find.
e0495736 235 */
64325a3b 236int tty_buffer_request_room(struct tty_port *port, size_t size)
e0495736 237{
ecbbfd44 238 struct tty_bufhead *buf = &port->buf;
e0495736
AC
239 struct tty_buffer *b, *n;
240 int left;
e8437d7e 241
5cff39c6 242 b = buf->tail;
7391ee16 243 left = b->size - b->used;
e0495736
AC
244
245 if (left < size) {
246 /* This is the slow path - looking for new buffers to use */
11b9faa4 247 if ((n = tty_buffer_alloc(port, size)) != NULL) {
5cff39c6 248 buf->tail = n;
e8437d7e
PH
249 b->commit = b->used;
250 smp_mb();
251 b->next = n;
e0495736
AC
252 } else
253 size = left;
254 }
e0495736
AC
255 return size;
256}
257EXPORT_SYMBOL_GPL(tty_buffer_request_room);
258
259/**
2832fc11 260 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
2f693357 261 * @port: tty port
e0495736 262 * @chars: characters
2832fc11 263 * @flag: flag value for each character
e0495736
AC
264 * @size: size
265 *
266 * Queue a series of bytes to the tty buffering. All the characters
ccc5ca8d 267 * passed are marked with the supplied flag. Returns the number added.
e0495736
AC
268 */
269
2f693357 270int tty_insert_flip_string_fixed_flag(struct tty_port *port,
2832fc11 271 const unsigned char *chars, char flag, size_t size)
e0495736
AC
272{
273 int copied = 0;
274 do {
d4bee0a6 275 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
64325a3b
IZ
276 int space = tty_buffer_request_room(port, goal);
277 struct tty_buffer *tb = port->buf.tail;
7391ee16 278 if (unlikely(space == 0))
e0495736 279 break;
1fc359fc
PH
280 memcpy(char_buf_ptr(tb, tb->used), chars, space);
281 memset(flag_buf_ptr(tb, tb->used), flag, space);
e0495736
AC
282 tb->used += space;
283 copied += space;
284 chars += space;
285 /* There is a small chance that we need to split the data over
286 several buffers. If this is the case we must loop */
287 } while (unlikely(size > copied));
288 return copied;
289}
2832fc11 290EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
e0495736
AC
291
292/**
293 * tty_insert_flip_string_flags - Add characters to the tty buffer
2f693357 294 * @port: tty port
e0495736
AC
295 * @chars: characters
296 * @flags: flag bytes
297 * @size: size
298 *
299 * Queue a series of bytes to the tty buffering. For each character
300 * the flags array indicates the status of the character. Returns the
301 * number added.
e0495736
AC
302 */
303
2f693357 304int tty_insert_flip_string_flags(struct tty_port *port,
e0495736
AC
305 const unsigned char *chars, const char *flags, size_t size)
306{
307 int copied = 0;
308 do {
d4bee0a6 309 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
64325a3b
IZ
310 int space = tty_buffer_request_room(port, goal);
311 struct tty_buffer *tb = port->buf.tail;
7391ee16 312 if (unlikely(space == 0))
e0495736 313 break;
1fc359fc
PH
314 memcpy(char_buf_ptr(tb, tb->used), chars, space);
315 memcpy(flag_buf_ptr(tb, tb->used), flags, space);
e0495736
AC
316 tb->used += space;
317 copied += space;
318 chars += space;
319 flags += space;
320 /* There is a small chance that we need to split the data over
321 several buffers. If this is the case we must loop */
322 } while (unlikely(size > copied));
323 return copied;
324}
325EXPORT_SYMBOL(tty_insert_flip_string_flags);
326
327/**
328 * tty_schedule_flip - push characters to ldisc
6732c8bb 329 * @port: tty port to push from
e0495736
AC
330 *
331 * Takes any pending buffers and transfers their ownership to the
332 * ldisc side of the queue. It then schedules those characters for
333 * processing by the line discipline.
cee4ad1e
IS
334 * Note that this function can only be used when the low_latency flag
335 * is unset. Otherwise the workqueue won't be flushed.
e0495736
AC
336 */
337
6732c8bb 338void tty_schedule_flip(struct tty_port *port)
e0495736 339{
6732c8bb 340 struct tty_bufhead *buf = &port->buf;
6732c8bb 341 WARN_ON(port->low_latency);
5cff39c6 342
7391ee16 343 buf->tail->commit = buf->tail->used;
5cff39c6 344 schedule_work(&buf->work);
e0495736
AC
345}
346EXPORT_SYMBOL(tty_schedule_flip);
347
348/**
349 * tty_prepare_flip_string - make room for characters
2f693357 350 * @port: tty port
e0495736
AC
351 * @chars: return pointer for character write area
352 * @size: desired size
353 *
354 * Prepare a block of space in the buffer for data. Returns the length
355 * available and buffer pointer to the space which is now allocated and
356 * accounted for as ready for normal characters. This is used for drivers
357 * that need their own block copy routines into the buffer. There is no
358 * guarantee the buffer is a DMA target!
e0495736
AC
359 */
360
2f693357 361int tty_prepare_flip_string(struct tty_port *port, unsigned char **chars,
ecbbfd44 362 size_t size)
e0495736 363{
64325a3b 364 int space = tty_buffer_request_room(port, size);
e0495736 365 if (likely(space)) {
64325a3b 366 struct tty_buffer *tb = port->buf.tail;
1fc359fc
PH
367 *chars = char_buf_ptr(tb, tb->used);
368 memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space);
e0495736
AC
369 tb->used += space;
370 }
371 return space;
372}
373EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
374
e0495736 375
da261e7f
PH
376static int
377receive_buf(struct tty_struct *tty, struct tty_buffer *head, int count)
378{
379 struct tty_ldisc *disc = tty->ldisc;
1fc359fc
PH
380 unsigned char *p = char_buf_ptr(head, head->read);
381 char *f = flag_buf_ptr(head, head->read);
da261e7f 382
24a89d1c
PH
383 if (disc->ops->receive_buf2)
384 count = disc->ops->receive_buf2(tty, p, f, count);
385 else {
386 count = min_t(int, count, tty->receive_room);
387 if (count)
388 disc->ops->receive_buf(tty, p, f, count);
389 }
da261e7f
PH
390 head->read += count;
391 return count;
392}
e0495736
AC
393
394/**
395 * flush_to_ldisc
396 * @work: tty structure passed from work queue.
397 *
398 * This routine is called out of the software interrupt to flush data
399 * from the buffer chain to the line discipline.
400 *
e9975fde
PH
401 * The receive_buf method is single threaded for each tty instance.
402 *
a7c8d58c 403 * Locking: takes buffer lock to ensure single-threaded flip buffer
e9975fde 404 * 'consumer'
e0495736
AC
405 */
406
407static void flush_to_ldisc(struct work_struct *work)
408{
ecbbfd44
JS
409 struct tty_port *port = container_of(work, struct tty_port, buf.work);
410 struct tty_bufhead *buf = &port->buf;
411 struct tty_struct *tty;
e0495736 412 struct tty_ldisc *disc;
e0495736 413
ecbbfd44 414 tty = port->itty;
34dcfb84 415 if (tty == NULL)
ecbbfd44
JS
416 return;
417
e0495736 418 disc = tty_ldisc_ref(tty);
36697529 419 if (disc == NULL)
e0495736
AC
420 return;
421
a7c8d58c 422 mutex_lock(&buf->lock);
45242006 423
d7a68be4
PH
424 while (1) {
425 struct tty_buffer *head = buf->head;
426 int count;
427
a7c8d58c
PH
428 /* Ldisc or user is trying to gain exclusive access */
429 if (atomic_read(&buf->priority))
d7a68be4
PH
430 break;
431
432 count = head->commit - head->read;
433 if (!count) {
434 if (head->next == NULL)
da261e7f 435 break;
d7a68be4
PH
436 buf->head = head->next;
437 tty_buffer_free(port, head);
438 continue;
e0495736 439 }
d7a68be4
PH
440
441 count = receive_buf(tty, head, count);
442 if (!count)
443 break;
e0495736 444 }
45242006 445
a7c8d58c 446 mutex_unlock(&buf->lock);
e0495736
AC
447
448 tty_ldisc_deref(disc);
449}
450
e043e42b
OH
451/**
452 * tty_flush_to_ldisc
453 * @tty: tty to push
454 *
455 * Push the terminal flip buffers to the line discipline.
456 *
457 * Must not be called from IRQ context.
458 */
459void tty_flush_to_ldisc(struct tty_struct *tty)
460{
d6c53c0e 461 if (!tty->port->low_latency)
ecbbfd44 462 flush_work(&tty->port->buf.work);
e043e42b
OH
463}
464
e0495736
AC
465/**
466 * tty_flip_buffer_push - terminal
2e124b4a 467 * @port: tty port to push
e0495736
AC
468 *
469 * Queue a push of the terminal flip buffers to the line discipline. This
d6c53c0e
JS
470 * function must not be called from IRQ context if port->low_latency is
471 * set.
e0495736
AC
472 *
473 * In the event of the queue being busy for flipping the work will be
474 * held off and retried later.
e0495736
AC
475 */
476
2e124b4a 477void tty_flip_buffer_push(struct tty_port *port)
e0495736 478{
2e124b4a 479 struct tty_bufhead *buf = &port->buf;
5cff39c6 480
7391ee16 481 buf->tail->commit = buf->tail->used;
e0495736 482
2e124b4a 483 if (port->low_latency)
5cff39c6 484 flush_to_ldisc(&buf->work);
e0495736 485 else
5cff39c6 486 schedule_work(&buf->work);
e0495736
AC
487}
488EXPORT_SYMBOL(tty_flip_buffer_push);
489
490/**
491 * tty_buffer_init - prepare a tty buffer structure
492 * @tty: tty to initialise
493 *
494 * Set up the initial state of the buffer management for a tty device.
495 * Must be called before the other tty buffer functions are used.
e0495736
AC
496 */
497
ecbbfd44 498void tty_buffer_init(struct tty_port *port)
e0495736 499{
ecbbfd44 500 struct tty_bufhead *buf = &port->buf;
5cff39c6 501
a7c8d58c 502 mutex_init(&buf->lock);
7391ee16
PH
503 tty_buffer_reset(&buf->sentinel, 0);
504 buf->head = &buf->sentinel;
505 buf->tail = &buf->sentinel;
809850b7 506 init_llist_head(&buf->free);
5dda4ca5 507 atomic_set(&buf->mem_used, 0);
a7c8d58c 508 atomic_set(&buf->priority, 0);
5cff39c6 509 INIT_WORK(&buf->work, flush_to_ldisc);
4d18e6ef 510 buf->mem_limit = TTYB_DEFAULT_MEM_LIMIT;
e0495736 511}
4d18e6ef
PH
512
513/**
514 * tty_buffer_set_limit - change the tty buffer memory limit
515 * @port: tty port to change
516 *
517 * Change the tty buffer memory limit.
518 * Must be called before the other tty buffer functions are used.
519 */
520
521int tty_buffer_set_limit(struct tty_port *port, int limit)
522{
523 if (limit < MIN_TTYB_SIZE)
524 return -EINVAL;
525 port->buf.mem_limit = limit;
526 return 0;
527}
528EXPORT_SYMBOL_GPL(tty_buffer_set_limit);