Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.15 665 lines 17 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * dashtty.c - tty driver for Dash channels interface. 4 * 5 * Copyright (C) 2007,2008,2012 Imagination Technologies 6 */ 7 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> 28 29#include <asm/da.h> 30 31/* Channel error codes */ 32#define CONAOK 0 33#define CONERR 1 34#define CONBAD 2 35#define CONPRM 3 36#define CONADR 4 37#define CONCNT 5 38#define CONCBF 6 39#define CONCBE 7 40#define CONBSY 8 41 42/* Default channel for the console */ 43#define CONSOLE_CHANNEL 1 44 45#define NUM_TTY_CHANNELS 6 46 47/* Auto allocate */ 48#define DA_TTY_MAJOR 0 49 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 52 * of a look in. 53 */ 54#define DA_TTY_POLL (HZ / 50) 55 56/* 57 * A short put delay improves latency but has a high throughput overhead 58 */ 59#define DA_TTY_PUT_DELAY (HZ / 100) 60 61static atomic_t num_channels_need_poll = ATOMIC_INIT(0); 62 63static struct timer_list poll_timer; 64 65static struct tty_driver *channel_driver; 66 67static struct timer_list put_timer; 68static struct task_struct *dashtty_thread; 69 70/* 71 * The console_poll parameter determines whether the console channel should be 72 * polled for input. 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. 75 */ 76static bool console_poll; 77module_param(console_poll, bool, S_IRUGO); 78 79#define RX_BUF_SIZE 1024 80 81enum { 82 INCHR = 1, 83 OUTCHR, 84 RDBUF, 85 WRBUF, 86 RDSTAT 87}; 88 89/** 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 103 */ 104struct dashtty_port { 105 struct tty_port port; 106 spinlock_t rx_lock; 107 void *rx_buf; 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; 113}; 114 115static struct dashtty_port dashtty_ports[NUM_TTY_CHANNELS]; 116 117static atomic_t dashtty_xmit_cnt = ATOMIC_INIT(0); 118static wait_queue_head_t dashtty_waitqueue; 119 120/* 121 * Low-level DA channel access routines 122 */ 123static int chancall(int in_bios_function, int in_channel, 124 int in_arg2, void *in_arg3, 125 void *in_arg4) 126{ 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"); 134 135 asm volatile ( 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), 143 "d" (channel), 144 "d" (arg2), 145 "d" (arg3), 146 "d" (arg4), 147 "d" (bios_call) /* ins */ 148 : "memory"); 149 150 return result; 151} 152 153/* 154 * Attempts to fetch count bytes from channel and returns actual count. 155 */ 156static int fetch_data(unsigned int channel) 157{ 158 struct dashtty_port *dport = &dashtty_ports[channel]; 159 int received = 0; 160 161 spin_lock_bh(&dport->rx_lock); 162 /* check the port isn't being shut down */ 163 if (!dport->rx_buf) 164 goto unlock; 165 if (chancall(RDBUF, channel, RX_BUF_SIZE, 166 (void *)dport->rx_buf, &received) == CONAOK) { 167 if (received) { 168 int space; 169 unsigned char *cbuf; 170 171 space = tty_prepare_flip_string(&dport->port, &cbuf, 172 received); 173 174 if (space <= 0) 175 goto unlock; 176 177 memcpy(cbuf, dport->rx_buf, space); 178 tty_flip_buffer_push(&dport->port); 179 } 180 } 181unlock: 182 spin_unlock_bh(&dport->rx_lock); 183 184 return received; 185} 186 187/** 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 190 * polling. 191 */ 192static int find_channel_to_poll(void) 193{ 194 static int last_polled_channel; 195 int last = last_polled_channel; 196 int chan; 197 struct dashtty_port *dport; 198 199 for (chan = last + 1; ; ++chan) { 200 if (chan >= NUM_TTY_CHANNELS) 201 chan = 0; 202 203 dport = &dashtty_ports[chan]; 204 if (dport->rx_buf) { 205 last_polled_channel = chan; 206 return chan; 207 } 208 209 if (chan == last) 210 break; 211 } 212 return -1; 213} 214 215/** 216 * put_channel_data() - Write out a block of channel data. 217 * @chan: DA channel number. 218 * 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. 221 * 222 * Returns: 1 if the remote buffer was too full to accept data. 223 * 0 otherwise. 224 */ 225static int put_channel_data(unsigned int chan) 226{ 227 struct dashtty_port *dport; 228 struct tty_struct *tty; 229 int number_written; 230 unsigned int count = 0; 231 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), 236 dport->xmit_cnt); 237 chancall(WRBUF, chan, count, 238 dport->port.xmit_buf + dport->xmit_tail, 239 &number_written); 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); 246 } else { 247 dport->xmit_tail += number_written; 248 if (dport->xmit_tail >= SERIAL_XMIT_SIZE) 249 dport->xmit_tail -= SERIAL_XMIT_SIZE; 250 } 251 atomic_sub(number_written, &dashtty_xmit_cnt); 252 } 253 mutex_unlock(&dport->xmit_lock); 254 255 /* if we've made more data available, wake up tty */ 256 if (count && number_written) { 257 tty = tty_port_tty_get(&dport->port); 258 if (tty) { 259 tty_wakeup(tty); 260 tty_kref_put(tty); 261 } 262 } 263 264 /* did the write fail? */ 265 return count && !number_written; 266} 267 268/** 269 * put_data() - Kernel thread to write out blocks of channel data to DA. 270 * @arg: Unused. 271 * 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 275 * drain the buffer. 276 */ 277static int put_data(void *arg) 278{ 279 unsigned int chan, stall; 280 281 __set_current_state(TASK_RUNNING); 282 while (!kthread_should_stop()) { 283 /* 284 * For each channel see if there's anything to transmit in the 285 * port's xmit_buf. 286 */ 287 stall = 0; 288 for (chan = 0; chan < NUM_TTY_CHANNELS; ++chan) 289 stall += put_channel_data(chan); 290 291 /* 292 * If some of the buffers are full, hold off for a short while 293 * to allow them to empty. 294 */ 295 if (stall) 296 msleep(25); 297 298 wait_event_interruptible(dashtty_waitqueue, 299 atomic_read(&dashtty_xmit_cnt)); 300 } 301 302 return 0; 303} 304 305/* 306 * This gets called every DA_TTY_POLL and polls the channels for data 307 */ 308static void dashtty_timer(struct timer_list *poll_timer) 309{ 310 int channel; 311 312 /* If there are no ports open do nothing and don't poll again. */ 313 if (!atomic_read(&num_channels_need_poll)) 314 return; 315 316 channel = find_channel_to_poll(); 317 318 /* Did we find a channel to poll? */ 319 if (channel >= 0) 320 fetch_data(channel); 321 322 mod_timer(poll_timer, jiffies + DA_TTY_POLL); 323} 324 325static void add_poll_timer(struct timer_list *poll_timer) 326{ 327 timer_setup(poll_timer, dashtty_timer, TIMER_PINNED); 328 poll_timer->expires = jiffies + DA_TTY_POLL; 329 330 /* 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. 333 */ 334 add_timer_on(poll_timer, 0); 335} 336 337static int dashtty_port_activate(struct tty_port *port, struct tty_struct *tty) 338{ 339 struct dashtty_port *dport = container_of(port, struct dashtty_port, 340 port); 341 void *rx_buf; 342 343 /* Allocate the buffer we use for writing data */ 344 if (tty_port_alloc_xmit_buf(port) < 0) 345 goto err; 346 347 /* Allocate the buffer we use for reading data */ 348 rx_buf = kzalloc(RX_BUF_SIZE, GFP_KERNEL); 349 if (!rx_buf) 350 goto err_free_xmit; 351 352 spin_lock_bh(&dport->rx_lock); 353 dport->rx_buf = rx_buf; 354 spin_unlock_bh(&dport->rx_lock); 355 356 /* 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. 360 * 361 */ 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); 365 366 return 0; 367err_free_xmit: 368 tty_port_free_xmit_buf(port); 369err: 370 return -ENOMEM; 371} 372 373static void dashtty_port_shutdown(struct tty_port *port) 374{ 375 struct dashtty_port *dport = container_of(port, struct dashtty_port, 376 port); 377 void *rx_buf; 378 unsigned int count; 379 380 /* stop reading */ 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); 384 385 mutex_lock(&dport->xmit_lock); 386 count = dport->xmit_cnt; 387 mutex_unlock(&dport->xmit_lock); 388 if (count) { 389 /* 390 * There's still data to write out, so wake and wait for the 391 * writer thread to drain the buffer. 392 */ 393 del_timer(&put_timer); 394 wake_up_interruptible(&dashtty_waitqueue); 395 wait_for_completion(&dport->xmit_empty); 396 } 397 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 */ 404 kfree(rx_buf); 405 406 /* Free the write buffer */ 407 tty_port_free_xmit_buf(port); 408} 409 410static const struct tty_port_operations dashtty_port_ops = { 411 .activate = dashtty_port_activate, 412 .shutdown = dashtty_port_shutdown, 413}; 414 415static int dashtty_install(struct tty_driver *driver, struct tty_struct *tty) 416{ 417 return tty_port_install(&dashtty_ports[tty->index].port, driver, tty); 418} 419 420static int dashtty_open(struct tty_struct *tty, struct file *filp) 421{ 422 return tty_port_open(tty->port, tty, filp); 423} 424 425static void dashtty_close(struct tty_struct *tty, struct file *filp) 426{ 427 return tty_port_close(tty->port, tty, filp); 428} 429 430static void dashtty_hangup(struct tty_struct *tty) 431{ 432 int channel; 433 struct dashtty_port *dport; 434 435 channel = tty->index; 436 dport = &dashtty_ports[channel]; 437 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); 442 dport->xmit_cnt = 0; 443 dport->xmit_head = 0; 444 dport->xmit_tail = 0; 445 complete(&dport->xmit_empty); 446 } 447 mutex_unlock(&dport->xmit_lock); 448 449 tty_port_hangup(tty->port); 450} 451 452/** 453 * dashtty_put_timer() - Delayed wake up of kernel thread. 454 * @ignored: unused 455 * 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 458 * stopped writing. 459 */ 460static void dashtty_put_timer(struct timer_list *unused) 461{ 462 if (atomic_read(&dashtty_xmit_cnt)) 463 wake_up_interruptible(&dashtty_waitqueue); 464} 465 466static int dashtty_write(struct tty_struct *tty, const unsigned char *buf, 467 int total) 468{ 469 int channel, count, block; 470 struct dashtty_port *dport; 471 472 /* Determine the channel */ 473 channel = tty->index; 474 dport = &dashtty_ports[channel]; 475 476 /* 477 * Write to output buffer. 478 * 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 482 * we're running on. 483 * 484 * What we actually want to happen is have all input and output done on 485 * one CPU. 486 */ 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; 499 buf += block; 500 } 501 count = dport->xmit_cnt; 502 /* xmit buffer no longer empty? */ 503 if (count) 504 reinit_completion(&dport->xmit_empty); 505 mutex_unlock(&dport->xmit_lock); 506 507 if (total) { 508 /* 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 511 * it. 512 */ 513 if (count == SERIAL_XMIT_SIZE) { 514 del_timer(&put_timer); 515 wake_up_interruptible(&dashtty_waitqueue); 516 } else { 517 mod_timer(&put_timer, jiffies + DA_TTY_PUT_DELAY); 518 } 519 } 520 return total; 521} 522 523static int dashtty_write_room(struct tty_struct *tty) 524{ 525 struct dashtty_port *dport; 526 int channel; 527 int room; 528 529 channel = tty->index; 530 dport = &dashtty_ports[channel]; 531 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); 536 537 return room; 538} 539 540static int dashtty_chars_in_buffer(struct tty_struct *tty) 541{ 542 struct dashtty_port *dport; 543 int channel; 544 int chars; 545 546 channel = tty->index; 547 dport = &dashtty_ports[channel]; 548 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); 553 554 return chars; 555} 556 557static 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, 565}; 566 567static int __init dashtty_init(void) 568{ 569 int ret; 570 int nport; 571 struct dashtty_port *dport; 572 573 if (!metag_da_enabled()) 574 return -ENODEV; 575 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); 580 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; 589 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); 600 } 601 602 timer_setup(&put_timer, dashtty_put_timer, 0); 603 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; 610 } 611 /* 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 614 * predictable CPU. 615 */ 616 kthread_bind(dashtty_thread, 0); 617 wake_up_process(dashtty_thread); 618 619 ret = tty_register_driver(channel_driver); 620 621 if (ret < 0) { 622 pr_err("Couldn't install dashtty driver: err %d\n", 623 ret); 624 goto err_stop_kthread; 625 } 626 627 return 0; 628 629err_stop_kthread: 630 kthread_stop(dashtty_thread); 631err_destroy_ports: 632 for (nport = 0; nport < NUM_TTY_CHANNELS; nport++) { 633 dport = &dashtty_ports[nport]; 634 tty_port_destroy(&dport->port); 635 } 636 put_tty_driver(channel_driver); 637 return ret; 638} 639device_initcall(dashtty_init); 640 641#ifdef CONFIG_DA_CONSOLE 642 643static void dash_console_write(struct console *co, const char *s, 644 unsigned int count) 645{ 646 int actually_written; 647 648 chancall(WRBUF, CONSOLE_CHANNEL, count, (void *)s, &actually_written); 649} 650 651static struct tty_driver *dash_console_device(struct console *c, int *index) 652{ 653 *index = c->index; 654 return channel_driver; 655} 656 657struct console dash_console = { 658 .name = "ttyDA", 659 .write = dash_console_write, 660 .device = dash_console_device, 661 .flags = CON_PRINTBUFFER, 662 .index = 1, 663}; 664 665#endif