1 // SPDX-License-Identifier: GPL-2.0
3 * dashtty.c - tty driver for Dash channels interface.
5 * Copyright (C) 2007,2008,2012 Imagination Technologies
8 #include <linux/atomic.h>
9 #include <linux/completion.h>
10 #include <linux/console.h>
11 #include <linux/delay.h>
12 #include <linux/export.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/kthread.h>
16 #include <linux/moduleparam.h>
17 #include <linux/mutex.h>
18 #include <linux/sched.h>
19 #include <linux/serial.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/string.h>
23 #include <linux/timer.h>
24 #include <linux/tty.h>
25 #include <linux/tty_driver.h>
26 #include <linux/tty_flip.h>
27 #include <linux/uaccess.h>
31 /* Channel error codes */
42 /* Default channel for the console */
43 #define CONSOLE_CHANNEL 1
45 #define NUM_TTY_CHANNELS 6
48 #define DA_TTY_MAJOR 0
50 /* A speedy poll rate helps the userland debug process connection response.
51 * But, if you set it too high then no other userland processes get much
54 #define DA_TTY_POLL (HZ / 50)
57 * A short put delay improves latency but has a high throughput overhead
59 #define DA_TTY_PUT_DELAY (HZ / 100)
61 static atomic_t num_channels_need_poll
= ATOMIC_INIT(0);
63 static struct timer_list poll_timer
;
65 static struct tty_driver
*channel_driver
;
67 static struct timer_list put_timer
;
68 static struct task_struct
*dashtty_thread
;
71 * The console_poll parameter determines whether the console channel should be
73 * By default the console channel isn't polled at all, in order to avoid the
74 * overhead, but that means it isn't possible to have a login on /dev/console.
76 static bool console_poll
;
77 module_param(console_poll
, bool, S_IRUGO
);
79 #define RX_BUF_SIZE 1024
90 * struct dashtty_port - Wrapper struct for dashtty tty_port.
91 * @port: TTY port data
92 * @rx_lock: Lock for rx_buf.
93 * This protects between the poll timer and user context.
94 * It's also held during read SWITCH operations.
95 * @rx_buf: Read buffer
96 * @xmit_lock: Lock for xmit_*, and port.xmit_buf.
97 * This protects between user context and kernel thread.
98 * It's also held during write SWITCH operations.
99 * @xmit_cnt: Size of xmit buffer contents
100 * @xmit_head: Head of xmit buffer where data is written
101 * @xmit_tail: Tail of xmit buffer where data is read
102 * @xmit_empty: Completion for xmit buffer being empty
104 struct dashtty_port
{
105 struct tty_port port
;
108 struct mutex xmit_lock
;
109 unsigned int xmit_cnt
;
110 unsigned int xmit_head
;
111 unsigned int xmit_tail
;
112 struct completion xmit_empty
;
115 static struct dashtty_port dashtty_ports
[NUM_TTY_CHANNELS
];
117 static atomic_t dashtty_xmit_cnt
= ATOMIC_INIT(0);
118 static wait_queue_head_t dashtty_waitqueue
;
121 * Low-level DA channel access routines
123 static int chancall(int in_bios_function
, int in_channel
,
124 int in_arg2
, void *in_arg3
,
127 register int bios_function
asm("D1Ar1") = in_bios_function
;
128 register int channel
asm("D0Ar2") = in_channel
;
129 register int arg2
asm("D1Ar3") = in_arg2
;
130 register void *arg3
asm("D0Ar4") = in_arg3
;
131 register void *arg4
asm("D1Ar5") = in_arg4
;
132 register int bios_call
asm("D0Ar6") = 3;
133 register int result
asm("D0Re0");
136 "MSETL [A0StP++], %6,%4,%2\n\t"
137 "ADD A0StP, A0StP, #8\n\t"
138 "SWITCH #0x0C30208\n\t"
139 "GETD %0, [A0StP+#-8]\n\t"
140 "SUB A0StP, A0StP, #(4*6)+8\n\t"
141 : "=d" (result
) /* outs */
142 : "d" (bios_function
),
147 "d" (bios_call
) /* ins */
154 * Attempts to fetch count bytes from channel and returns actual count.
156 static int fetch_data(unsigned int channel
)
158 struct dashtty_port
*dport
= &dashtty_ports
[channel
];
161 spin_lock_bh(&dport
->rx_lock
);
162 /* check the port isn't being shut down */
165 if (chancall(RDBUF
, channel
, RX_BUF_SIZE
,
166 (void *)dport
->rx_buf
, &received
) == CONAOK
) {
171 space
= tty_prepare_flip_string(&dport
->port
, &cbuf
,
177 memcpy(cbuf
, dport
->rx_buf
, space
);
178 tty_flip_buffer_push(&dport
->port
);
182 spin_unlock_bh(&dport
->rx_lock
);
188 * find_channel_to_poll() - Returns number of the next channel to poll.
189 * Returns: The number of the next channel to poll, or -1 if none need
192 static int find_channel_to_poll(void)
194 static int last_polled_channel
;
195 int last
= last_polled_channel
;
197 struct dashtty_port
*dport
;
199 for (chan
= last
+ 1; ; ++chan
) {
200 if (chan
>= NUM_TTY_CHANNELS
)
203 dport
= &dashtty_ports
[chan
];
205 last_polled_channel
= chan
;
216 * put_channel_data() - Write out a block of channel data.
217 * @chan: DA channel number.
219 * Write a single block of data out to the debug adapter. If the circular buffer
220 * is wrapped then only the first block is written.
222 * Returns: 1 if the remote buffer was too full to accept data.
225 static int put_channel_data(unsigned int chan
)
227 struct dashtty_port
*dport
;
228 struct tty_struct
*tty
;
230 unsigned int count
= 0;
232 dport
= &dashtty_ports
[chan
];
233 mutex_lock(&dport
->xmit_lock
);
234 if (dport
->xmit_cnt
) {
235 count
= min((unsigned int)(SERIAL_XMIT_SIZE
- dport
->xmit_tail
),
237 chancall(WRBUF
, chan
, count
,
238 dport
->port
.xmit_buf
+ dport
->xmit_tail
,
240 dport
->xmit_cnt
-= number_written
;
241 if (!dport
->xmit_cnt
) {
242 /* reset pointers to avoid wraps */
243 dport
->xmit_head
= 0;
244 dport
->xmit_tail
= 0;
245 complete(&dport
->xmit_empty
);
247 dport
->xmit_tail
+= number_written
;
248 if (dport
->xmit_tail
>= SERIAL_XMIT_SIZE
)
249 dport
->xmit_tail
-= SERIAL_XMIT_SIZE
;
251 atomic_sub(number_written
, &dashtty_xmit_cnt
);
253 mutex_unlock(&dport
->xmit_lock
);
255 /* if we've made more data available, wake up tty */
256 if (count
&& number_written
) {
257 tty
= tty_port_tty_get(&dport
->port
);
264 /* did the write fail? */
265 return count
&& !number_written
;
269 * put_data() - Kernel thread to write out blocks of channel data to DA.
272 * This kernel thread runs while @dashtty_xmit_cnt != 0, and loops over the
273 * channels to write out any buffered data. If any of the channels stall due to
274 * the remote buffer being full, a hold off happens to allow the debugger to
277 static int put_data(void *arg
)
279 unsigned int chan
, stall
;
281 __set_current_state(TASK_RUNNING
);
282 while (!kthread_should_stop()) {
284 * For each channel see if there's anything to transmit in the
288 for (chan
= 0; chan
< NUM_TTY_CHANNELS
; ++chan
)
289 stall
+= put_channel_data(chan
);
292 * If some of the buffers are full, hold off for a short while
293 * to allow them to empty.
298 wait_event_interruptible(dashtty_waitqueue
,
299 atomic_read(&dashtty_xmit_cnt
));
306 * This gets called every DA_TTY_POLL and polls the channels for data
308 static void dashtty_timer(struct timer_list
*poll_timer
)
312 /* If there are no ports open do nothing and don't poll again. */
313 if (!atomic_read(&num_channels_need_poll
))
316 channel
= find_channel_to_poll();
318 /* Did we find a channel to poll? */
322 mod_timer(poll_timer
, jiffies
+ DA_TTY_POLL
);
325 static void add_poll_timer(struct timer_list
*poll_timer
)
327 timer_setup(poll_timer
, dashtty_timer
, TIMER_PINNED
);
328 poll_timer
->expires
= jiffies
+ DA_TTY_POLL
;
331 * Always attach the timer to the boot CPU. The DA channels are per-CPU
332 * so all polling should be from a single CPU.
334 add_timer_on(poll_timer
, 0);
337 static int dashtty_port_activate(struct tty_port
*port
, struct tty_struct
*tty
)
339 struct dashtty_port
*dport
= container_of(port
, struct dashtty_port
,
343 /* Allocate the buffer we use for writing data */
344 if (tty_port_alloc_xmit_buf(port
) < 0)
347 /* Allocate the buffer we use for reading data */
348 rx_buf
= kzalloc(RX_BUF_SIZE
, GFP_KERNEL
);
352 spin_lock_bh(&dport
->rx_lock
);
353 dport
->rx_buf
= rx_buf
;
354 spin_unlock_bh(&dport
->rx_lock
);
357 * Don't add the poll timer if we're opening a console. This
358 * avoids the overhead of polling the Dash but means it is not
359 * possible to have a login on /dev/console.
362 if (console_poll
|| dport
!= &dashtty_ports
[CONSOLE_CHANNEL
])
363 if (atomic_inc_return(&num_channels_need_poll
) == 1)
364 add_poll_timer(&poll_timer
);
368 tty_port_free_xmit_buf(port
);
373 static void dashtty_port_shutdown(struct tty_port
*port
)
375 struct dashtty_port
*dport
= container_of(port
, struct dashtty_port
,
381 if (console_poll
|| dport
!= &dashtty_ports
[CONSOLE_CHANNEL
])
382 if (atomic_dec_and_test(&num_channels_need_poll
))
383 del_timer_sync(&poll_timer
);
385 mutex_lock(&dport
->xmit_lock
);
386 count
= dport
->xmit_cnt
;
387 mutex_unlock(&dport
->xmit_lock
);
390 * There's still data to write out, so wake and wait for the
391 * writer thread to drain the buffer.
393 del_timer(&put_timer
);
394 wake_up_interruptible(&dashtty_waitqueue
);
395 wait_for_completion(&dport
->xmit_empty
);
398 /* Null the read buffer (timer could still be running!) */
399 spin_lock_bh(&dport
->rx_lock
);
400 rx_buf
= dport
->rx_buf
;
401 dport
->rx_buf
= NULL
;
402 spin_unlock_bh(&dport
->rx_lock
);
403 /* Free the read buffer */
406 /* Free the write buffer */
407 tty_port_free_xmit_buf(port
);
410 static const struct tty_port_operations dashtty_port_ops
= {
411 .activate
= dashtty_port_activate
,
412 .shutdown
= dashtty_port_shutdown
,
415 static int dashtty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
417 return tty_port_install(&dashtty_ports
[tty
->index
].port
, driver
, tty
);
420 static int dashtty_open(struct tty_struct
*tty
, struct file
*filp
)
422 return tty_port_open(tty
->port
, tty
, filp
);
425 static void dashtty_close(struct tty_struct
*tty
, struct file
*filp
)
427 return tty_port_close(tty
->port
, tty
, filp
);
430 static void dashtty_hangup(struct tty_struct
*tty
)
433 struct dashtty_port
*dport
;
435 channel
= tty
->index
;
436 dport
= &dashtty_ports
[channel
];
438 /* drop any data in the xmit buffer */
439 mutex_lock(&dport
->xmit_lock
);
440 if (dport
->xmit_cnt
) {
441 atomic_sub(dport
->xmit_cnt
, &dashtty_xmit_cnt
);
443 dport
->xmit_head
= 0;
444 dport
->xmit_tail
= 0;
445 complete(&dport
->xmit_empty
);
447 mutex_unlock(&dport
->xmit_lock
);
449 tty_port_hangup(tty
->port
);
453 * dashtty_put_timer() - Delayed wake up of kernel thread.
456 * This timer function wakes up the kernel thread if any data exists in the
457 * buffers. It is used to delay the expensive writeout until the writer has
460 static void dashtty_put_timer(struct timer_list
*unused
)
462 if (atomic_read(&dashtty_xmit_cnt
))
463 wake_up_interruptible(&dashtty_waitqueue
);
466 static int dashtty_write(struct tty_struct
*tty
, const unsigned char *buf
,
469 int channel
, count
, block
;
470 struct dashtty_port
*dport
;
472 /* Determine the channel */
473 channel
= tty
->index
;
474 dport
= &dashtty_ports
[channel
];
477 * Write to output buffer.
479 * The reason that we asynchronously write the buffer is because if we
480 * were to write the buffer synchronously then because DA channels are
481 * per-CPU the buffer would be written to the channel of whatever CPU
484 * What we actually want to happen is have all input and output done on
487 mutex_lock(&dport
->xmit_lock
);
488 /* work out how many bytes we can write to the xmit buffer */
489 total
= min(total
, (int)(SERIAL_XMIT_SIZE
- dport
->xmit_cnt
));
490 atomic_add(total
, &dashtty_xmit_cnt
);
491 dport
->xmit_cnt
+= total
;
492 /* write the actual bytes (may need splitting if it wraps) */
493 for (count
= total
; count
; count
-= block
) {
494 block
= min(count
, (int)(SERIAL_XMIT_SIZE
- dport
->xmit_head
));
495 memcpy(dport
->port
.xmit_buf
+ dport
->xmit_head
, buf
, block
);
496 dport
->xmit_head
+= block
;
497 if (dport
->xmit_head
>= SERIAL_XMIT_SIZE
)
498 dport
->xmit_head
-= SERIAL_XMIT_SIZE
;
501 count
= dport
->xmit_cnt
;
502 /* xmit buffer no longer empty? */
504 reinit_completion(&dport
->xmit_empty
);
505 mutex_unlock(&dport
->xmit_lock
);
509 * If the buffer is full, wake up the kthread, otherwise allow
510 * some more time for the buffer to fill up a bit before waking
513 if (count
== SERIAL_XMIT_SIZE
) {
514 del_timer(&put_timer
);
515 wake_up_interruptible(&dashtty_waitqueue
);
517 mod_timer(&put_timer
, jiffies
+ DA_TTY_PUT_DELAY
);
523 static int dashtty_write_room(struct tty_struct
*tty
)
525 struct dashtty_port
*dport
;
529 channel
= tty
->index
;
530 dport
= &dashtty_ports
[channel
];
532 /* report the space in the xmit buffer */
533 mutex_lock(&dport
->xmit_lock
);
534 room
= SERIAL_XMIT_SIZE
- dport
->xmit_cnt
;
535 mutex_unlock(&dport
->xmit_lock
);
540 static int dashtty_chars_in_buffer(struct tty_struct
*tty
)
542 struct dashtty_port
*dport
;
546 channel
= tty
->index
;
547 dport
= &dashtty_ports
[channel
];
549 /* report the number of bytes in the xmit buffer */
550 mutex_lock(&dport
->xmit_lock
);
551 chars
= dport
->xmit_cnt
;
552 mutex_unlock(&dport
->xmit_lock
);
557 static const struct tty_operations dashtty_ops
= {
558 .install
= dashtty_install
,
559 .open
= dashtty_open
,
560 .close
= dashtty_close
,
561 .hangup
= dashtty_hangup
,
562 .write
= dashtty_write
,
563 .write_room
= dashtty_write_room
,
564 .chars_in_buffer
= dashtty_chars_in_buffer
,
567 static int __init
dashtty_init(void)
571 struct dashtty_port
*dport
;
573 if (!metag_da_enabled())
576 channel_driver
= tty_alloc_driver(NUM_TTY_CHANNELS
,
577 TTY_DRIVER_REAL_RAW
);
578 if (IS_ERR(channel_driver
))
579 return PTR_ERR(channel_driver
);
581 channel_driver
->driver_name
= "metag_da";
582 channel_driver
->name
= "ttyDA";
583 channel_driver
->major
= DA_TTY_MAJOR
;
584 channel_driver
->minor_start
= 0;
585 channel_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
586 channel_driver
->subtype
= SERIAL_TYPE_NORMAL
;
587 channel_driver
->init_termios
= tty_std_termios
;
588 channel_driver
->init_termios
.c_cflag
|= CLOCAL
;
590 tty_set_operations(channel_driver
, &dashtty_ops
);
591 for (nport
= 0; nport
< NUM_TTY_CHANNELS
; nport
++) {
592 dport
= &dashtty_ports
[nport
];
593 tty_port_init(&dport
->port
);
594 dport
->port
.ops
= &dashtty_port_ops
;
595 spin_lock_init(&dport
->rx_lock
);
596 mutex_init(&dport
->xmit_lock
);
597 /* the xmit buffer starts empty, i.e. completely written */
598 init_completion(&dport
->xmit_empty
);
599 complete(&dport
->xmit_empty
);
602 timer_setup(&put_timer
, dashtty_put_timer
, 0);
604 init_waitqueue_head(&dashtty_waitqueue
);
605 dashtty_thread
= kthread_create(put_data
, NULL
, "ttyDA");
606 if (IS_ERR(dashtty_thread
)) {
607 pr_err("Couldn't create dashtty thread\n");
608 ret
= PTR_ERR(dashtty_thread
);
609 goto err_destroy_ports
;
612 * Bind the writer thread to the boot CPU so it can't migrate.
613 * DA channels are per-CPU and we want all channel I/O to be on a single
616 kthread_bind(dashtty_thread
, 0);
617 wake_up_process(dashtty_thread
);
619 ret
= tty_register_driver(channel_driver
);
622 pr_err("Couldn't install dashtty driver: err %d\n",
624 goto err_stop_kthread
;
630 kthread_stop(dashtty_thread
);
632 for (nport
= 0; nport
< NUM_TTY_CHANNELS
; nport
++) {
633 dport
= &dashtty_ports
[nport
];
634 tty_port_destroy(&dport
->port
);
636 put_tty_driver(channel_driver
);
639 device_initcall(dashtty_init
);
641 #ifdef CONFIG_DA_CONSOLE
643 static void dash_console_write(struct console
*co
, const char *s
,
646 int actually_written
;
648 chancall(WRBUF
, CONSOLE_CHANNEL
, count
, (void *)s
, &actually_written
);
651 static struct tty_driver
*dash_console_device(struct console
*c
, int *index
)
654 return channel_driver
;
657 struct console dash_console
= {
659 .write
= dash_console_write
,
660 .device
= dash_console_device
,
661 .flags
= CON_PRINTBUFFER
,