metag_da.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. * dashtty.c - tty driver for Dash channels interface.
  3. *
  4. * Copyright (C) 2007,2008,2012 Imagination Technologies
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive
  8. * for more details.
  9. *
  10. */
  11. #include <linux/atomic.h>
  12. #include <linux/completion.h>
  13. #include <linux/console.h>
  14. #include <linux/delay.h>
  15. #include <linux/export.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/kthread.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/mutex.h>
  21. #include <linux/sched.h>
  22. #include <linux/serial.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/string.h>
  26. #include <linux/timer.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/da.h>
  32. /* Channel error codes */
  33. #define CONAOK 0
  34. #define CONERR 1
  35. #define CONBAD 2
  36. #define CONPRM 3
  37. #define CONADR 4
  38. #define CONCNT 5
  39. #define CONCBF 6
  40. #define CONCBE 7
  41. #define CONBSY 8
  42. /* Default channel for the console */
  43. #define CONSOLE_CHANNEL 1
  44. #define NUM_TTY_CHANNELS 6
  45. /* Auto allocate */
  46. #define DA_TTY_MAJOR 0
  47. /* A speedy poll rate helps the userland debug process connection response.
  48. * But, if you set it too high then no other userland processes get much
  49. * of a look in.
  50. */
  51. #define DA_TTY_POLL (HZ / 50)
  52. /*
  53. * A short put delay improves latency but has a high throughput overhead
  54. */
  55. #define DA_TTY_PUT_DELAY (HZ / 100)
  56. static atomic_t num_channels_need_poll = ATOMIC_INIT(0);
  57. static struct timer_list poll_timer;
  58. static struct tty_driver *channel_driver;
  59. static struct timer_list put_timer;
  60. static struct task_struct *dashtty_thread;
  61. /*
  62. * The console_poll parameter determines whether the console channel should be
  63. * polled for input.
  64. * By default the console channel isn't polled at all, in order to avoid the
  65. * overhead, but that means it isn't possible to have a login on /dev/console.
  66. */
  67. static bool console_poll;
  68. module_param(console_poll, bool, S_IRUGO);
  69. #define RX_BUF_SIZE 1024
  70. enum {
  71. INCHR = 1,
  72. OUTCHR,
  73. RDBUF,
  74. WRBUF,
  75. RDSTAT
  76. };
  77. /**
  78. * struct dashtty_port - Wrapper struct for dashtty tty_port.
  79. * @port: TTY port data
  80. * @rx_lock: Lock for rx_buf.
  81. * This protects between the poll timer and user context.
  82. * It's also held during read SWITCH operations.
  83. * @rx_buf: Read buffer
  84. * @xmit_lock: Lock for xmit_*, and port.xmit_buf.
  85. * This protects between user context and kernel thread.
  86. * It's also held during write SWITCH operations.
  87. * @xmit_cnt: Size of xmit buffer contents
  88. * @xmit_head: Head of xmit buffer where data is written
  89. * @xmit_tail: Tail of xmit buffer where data is read
  90. * @xmit_empty: Completion for xmit buffer being empty
  91. */
  92. struct dashtty_port {
  93. struct tty_port port;
  94. spinlock_t rx_lock;
  95. void *rx_buf;
  96. struct mutex xmit_lock;
  97. unsigned int xmit_cnt;
  98. unsigned int xmit_head;
  99. unsigned int xmit_tail;
  100. struct completion xmit_empty;
  101. };
  102. static struct dashtty_port dashtty_ports[NUM_TTY_CHANNELS];
  103. static atomic_t dashtty_xmit_cnt = ATOMIC_INIT(0);
  104. static wait_queue_head_t dashtty_waitqueue;
  105. /*
  106. * Low-level DA channel access routines
  107. */
  108. static int chancall(int in_bios_function, int in_channel,
  109. int in_arg2, void *in_arg3,
  110. void *in_arg4)
  111. {
  112. register int bios_function asm("D1Ar1") = in_bios_function;
  113. register int channel asm("D0Ar2") = in_channel;
  114. register int arg2 asm("D1Ar3") = in_arg2;
  115. register void *arg3 asm("D0Ar4") = in_arg3;
  116. register void *arg4 asm("D1Ar5") = in_arg4;
  117. register int bios_call asm("D0Ar6") = 3;
  118. register int result asm("D0Re0");
  119. asm volatile (
  120. "MSETL [A0StP++], %6,%4,%2\n\t"
  121. "ADD A0StP, A0StP, #8\n\t"
  122. "SWITCH #0x0C30208\n\t"
  123. "GETD %0, [A0StP+#-8]\n\t"
  124. "SUB A0StP, A0StP, #(4*6)+8\n\t"
  125. : "=d" (result) /* outs */
  126. : "d" (bios_function),
  127. "d" (channel),
  128. "d" (arg2),
  129. "d" (arg3),
  130. "d" (arg4),
  131. "d" (bios_call) /* ins */
  132. : "memory");
  133. return result;
  134. }
  135. /*
  136. * Attempts to fetch count bytes from channel and returns actual count.
  137. */
  138. static int fetch_data(unsigned int channel)
  139. {
  140. struct dashtty_port *dport = &dashtty_ports[channel];
  141. int received = 0;
  142. spin_lock_bh(&dport->rx_lock);
  143. /* check the port isn't being shut down */
  144. if (!dport->rx_buf)
  145. goto unlock;
  146. if (chancall(RDBUF, channel, RX_BUF_SIZE,
  147. (void *)dport->rx_buf, &received) == CONAOK) {
  148. if (received) {
  149. int space;
  150. unsigned char *cbuf;
  151. space = tty_prepare_flip_string(&dport->port, &cbuf,
  152. received);
  153. if (space <= 0)
  154. goto unlock;
  155. memcpy(cbuf, dport->rx_buf, space);
  156. tty_flip_buffer_push(&dport->port);
  157. }
  158. }
  159. unlock:
  160. spin_unlock_bh(&dport->rx_lock);
  161. return received;
  162. }
  163. /**
  164. * find_channel_to_poll() - Returns number of the next channel to poll.
  165. * Returns: The number of the next channel to poll, or -1 if none need
  166. * polling.
  167. */
  168. static int find_channel_to_poll(void)
  169. {
  170. static int last_polled_channel;
  171. int last = last_polled_channel;
  172. int chan;
  173. struct dashtty_port *dport;
  174. for (chan = last + 1; ; ++chan) {
  175. if (chan >= NUM_TTY_CHANNELS)
  176. chan = 0;
  177. dport = &dashtty_ports[chan];
  178. if (dport->rx_buf) {
  179. last_polled_channel = chan;
  180. return chan;
  181. }
  182. if (chan == last)
  183. break;
  184. }
  185. return -1;
  186. }
  187. /**
  188. * put_channel_data() - Write out a block of channel data.
  189. * @chan: DA channel number.
  190. *
  191. * Write a single block of data out to the debug adapter. If the circular buffer
  192. * is wrapped then only the first block is written.
  193. *
  194. * Returns: 1 if the remote buffer was too full to accept data.
  195. * 0 otherwise.
  196. */
  197. static int put_channel_data(unsigned int chan)
  198. {
  199. struct dashtty_port *dport;
  200. struct tty_struct *tty;
  201. int number_written;
  202. unsigned int count = 0;
  203. dport = &dashtty_ports[chan];
  204. mutex_lock(&dport->xmit_lock);
  205. if (dport->xmit_cnt) {
  206. count = min((unsigned int)(SERIAL_XMIT_SIZE - dport->xmit_tail),
  207. dport->xmit_cnt);
  208. chancall(WRBUF, chan, count,
  209. dport->port.xmit_buf + dport->xmit_tail,
  210. &number_written);
  211. dport->xmit_cnt -= number_written;
  212. if (!dport->xmit_cnt) {
  213. /* reset pointers to avoid wraps */
  214. dport->xmit_head = 0;
  215. dport->xmit_tail = 0;
  216. complete(&dport->xmit_empty);
  217. } else {
  218. dport->xmit_tail += number_written;
  219. if (dport->xmit_tail >= SERIAL_XMIT_SIZE)
  220. dport->xmit_tail -= SERIAL_XMIT_SIZE;
  221. }
  222. atomic_sub(number_written, &dashtty_xmit_cnt);
  223. }
  224. mutex_unlock(&dport->xmit_lock);
  225. /* if we've made more data available, wake up tty */
  226. if (count && number_written) {
  227. tty = tty_port_tty_get(&dport->port);
  228. if (tty) {
  229. tty_wakeup(tty);
  230. tty_kref_put(tty);
  231. }
  232. }
  233. /* did the write fail? */
  234. return count && !number_written;
  235. }
  236. /**
  237. * put_data() - Kernel thread to write out blocks of channel data to DA.
  238. * @arg: Unused.
  239. *
  240. * This kernel thread runs while @dashtty_xmit_cnt != 0, and loops over the
  241. * channels to write out any buffered data. If any of the channels stall due to
  242. * the remote buffer being full, a hold off happens to allow the debugger to
  243. * drain the buffer.
  244. */
  245. static int put_data(void *arg)
  246. {
  247. unsigned int chan, stall;
  248. __set_current_state(TASK_RUNNING);
  249. while (!kthread_should_stop()) {
  250. /*
  251. * For each channel see if there's anything to transmit in the
  252. * port's xmit_buf.
  253. */
  254. stall = 0;
  255. for (chan = 0; chan < NUM_TTY_CHANNELS; ++chan)
  256. stall += put_channel_data(chan);
  257. /*
  258. * If some of the buffers are full, hold off for a short while
  259. * to allow them to empty.
  260. */
  261. if (stall)
  262. msleep(25);
  263. wait_event_interruptible(dashtty_waitqueue,
  264. atomic_read(&dashtty_xmit_cnt));
  265. }
  266. return 0;
  267. }
  268. /*
  269. * This gets called every DA_TTY_POLL and polls the channels for data
  270. */
  271. static void dashtty_timer(unsigned long ignored)
  272. {
  273. int channel;
  274. /* If there are no ports open do nothing and don't poll again. */
  275. if (!atomic_read(&num_channels_need_poll))
  276. return;
  277. channel = find_channel_to_poll();
  278. /* Did we find a channel to poll? */
  279. if (channel >= 0)
  280. fetch_data(channel);
  281. mod_timer(&poll_timer, jiffies + DA_TTY_POLL);
  282. }
  283. static void add_poll_timer(struct timer_list *poll_timer)
  284. {
  285. setup_pinned_timer(poll_timer, dashtty_timer, 0);
  286. poll_timer->expires = jiffies + DA_TTY_POLL;
  287. /*
  288. * Always attach the timer to the boot CPU. The DA channels are per-CPU
  289. * so all polling should be from a single CPU.
  290. */
  291. add_timer_on(poll_timer, 0);
  292. }
  293. static int dashtty_port_activate(struct tty_port *port, struct tty_struct *tty)
  294. {
  295. struct dashtty_port *dport = container_of(port, struct dashtty_port,
  296. port);
  297. void *rx_buf;
  298. /* Allocate the buffer we use for writing data */
  299. if (tty_port_alloc_xmit_buf(port) < 0)
  300. goto err;
  301. /* Allocate the buffer we use for reading data */
  302. rx_buf = kzalloc(RX_BUF_SIZE, GFP_KERNEL);
  303. if (!rx_buf)
  304. goto err_free_xmit;
  305. spin_lock_bh(&dport->rx_lock);
  306. dport->rx_buf = rx_buf;
  307. spin_unlock_bh(&dport->rx_lock);
  308. /*
  309. * Don't add the poll timer if we're opening a console. This
  310. * avoids the overhead of polling the Dash but means it is not
  311. * possible to have a login on /dev/console.
  312. *
  313. */
  314. if (console_poll || dport != &dashtty_ports[CONSOLE_CHANNEL])
  315. if (atomic_inc_return(&num_channels_need_poll) == 1)
  316. add_poll_timer(&poll_timer);
  317. return 0;
  318. err_free_xmit:
  319. tty_port_free_xmit_buf(port);
  320. err:
  321. return -ENOMEM;
  322. }
  323. static void dashtty_port_shutdown(struct tty_port *port)
  324. {
  325. struct dashtty_port *dport = container_of(port, struct dashtty_port,
  326. port);
  327. void *rx_buf;
  328. unsigned int count;
  329. /* stop reading */
  330. if (console_poll || dport != &dashtty_ports[CONSOLE_CHANNEL])
  331. if (atomic_dec_and_test(&num_channels_need_poll))
  332. del_timer_sync(&poll_timer);
  333. mutex_lock(&dport->xmit_lock);
  334. count = dport->xmit_cnt;
  335. mutex_unlock(&dport->xmit_lock);
  336. if (count) {
  337. /*
  338. * There's still data to write out, so wake and wait for the
  339. * writer thread to drain the buffer.
  340. */
  341. del_timer(&put_timer);
  342. wake_up_interruptible(&dashtty_waitqueue);
  343. wait_for_completion(&dport->xmit_empty);
  344. }
  345. /* Null the read buffer (timer could still be running!) */
  346. spin_lock_bh(&dport->rx_lock);
  347. rx_buf = dport->rx_buf;
  348. dport->rx_buf = NULL;
  349. spin_unlock_bh(&dport->rx_lock);
  350. /* Free the read buffer */
  351. kfree(rx_buf);
  352. /* Free the write buffer */
  353. tty_port_free_xmit_buf(port);
  354. }
  355. static const struct tty_port_operations dashtty_port_ops = {
  356. .activate = dashtty_port_activate,
  357. .shutdown = dashtty_port_shutdown,
  358. };
  359. static int dashtty_install(struct tty_driver *driver, struct tty_struct *tty)
  360. {
  361. return tty_port_install(&dashtty_ports[tty->index].port, driver, tty);
  362. }
  363. static int dashtty_open(struct tty_struct *tty, struct file *filp)
  364. {
  365. return tty_port_open(tty->port, tty, filp);
  366. }
  367. static void dashtty_close(struct tty_struct *tty, struct file *filp)
  368. {
  369. return tty_port_close(tty->port, tty, filp);
  370. }
  371. static void dashtty_hangup(struct tty_struct *tty)
  372. {
  373. int channel;
  374. struct dashtty_port *dport;
  375. channel = tty->index;
  376. dport = &dashtty_ports[channel];
  377. /* drop any data in the xmit buffer */
  378. mutex_lock(&dport->xmit_lock);
  379. if (dport->xmit_cnt) {
  380. atomic_sub(dport->xmit_cnt, &dashtty_xmit_cnt);
  381. dport->xmit_cnt = 0;
  382. dport->xmit_head = 0;
  383. dport->xmit_tail = 0;
  384. complete(&dport->xmit_empty);
  385. }
  386. mutex_unlock(&dport->xmit_lock);
  387. tty_port_hangup(tty->port);
  388. }
  389. /**
  390. * dashtty_put_timer() - Delayed wake up of kernel thread.
  391. * @ignored: unused
  392. *
  393. * This timer function wakes up the kernel thread if any data exists in the
  394. * buffers. It is used to delay the expensive writeout until the writer has
  395. * stopped writing.
  396. */
  397. static void dashtty_put_timer(unsigned long ignored)
  398. {
  399. if (atomic_read(&dashtty_xmit_cnt))
  400. wake_up_interruptible(&dashtty_waitqueue);
  401. }
  402. static int dashtty_write(struct tty_struct *tty, const unsigned char *buf,
  403. int total)
  404. {
  405. int channel, count, block;
  406. struct dashtty_port *dport;
  407. /* Determine the channel */
  408. channel = tty->index;
  409. dport = &dashtty_ports[channel];
  410. /*
  411. * Write to output buffer.
  412. *
  413. * The reason that we asynchronously write the buffer is because if we
  414. * were to write the buffer synchronously then because DA channels are
  415. * per-CPU the buffer would be written to the channel of whatever CPU
  416. * we're running on.
  417. *
  418. * What we actually want to happen is have all input and output done on
  419. * one CPU.
  420. */
  421. mutex_lock(&dport->xmit_lock);
  422. /* work out how many bytes we can write to the xmit buffer */
  423. total = min(total, (int)(SERIAL_XMIT_SIZE - dport->xmit_cnt));
  424. atomic_add(total, &dashtty_xmit_cnt);
  425. dport->xmit_cnt += total;
  426. /* write the actual bytes (may need splitting if it wraps) */
  427. for (count = total; count; count -= block) {
  428. block = min(count, (int)(SERIAL_XMIT_SIZE - dport->xmit_head));
  429. memcpy(dport->port.xmit_buf + dport->xmit_head, buf, block);
  430. dport->xmit_head += block;
  431. if (dport->xmit_head >= SERIAL_XMIT_SIZE)
  432. dport->xmit_head -= SERIAL_XMIT_SIZE;
  433. buf += block;
  434. }
  435. count = dport->xmit_cnt;
  436. /* xmit buffer no longer empty? */
  437. if (count)
  438. reinit_completion(&dport->xmit_empty);
  439. mutex_unlock(&dport->xmit_lock);
  440. if (total) {
  441. /*
  442. * If the buffer is full, wake up the kthread, otherwise allow
  443. * some more time for the buffer to fill up a bit before waking
  444. * it.
  445. */
  446. if (count == SERIAL_XMIT_SIZE) {
  447. del_timer(&put_timer);
  448. wake_up_interruptible(&dashtty_waitqueue);
  449. } else {
  450. mod_timer(&put_timer, jiffies + DA_TTY_PUT_DELAY);
  451. }
  452. }
  453. return total;
  454. }
  455. static int dashtty_write_room(struct tty_struct *tty)
  456. {
  457. struct dashtty_port *dport;
  458. int channel;
  459. int room;
  460. channel = tty->index;
  461. dport = &dashtty_ports[channel];
  462. /* report the space in the xmit buffer */
  463. mutex_lock(&dport->xmit_lock);
  464. room = SERIAL_XMIT_SIZE - dport->xmit_cnt;
  465. mutex_unlock(&dport->xmit_lock);
  466. return room;
  467. }
  468. static int dashtty_chars_in_buffer(struct tty_struct *tty)
  469. {
  470. struct dashtty_port *dport;
  471. int channel;
  472. int chars;
  473. channel = tty->index;
  474. dport = &dashtty_ports[channel];
  475. /* report the number of bytes in the xmit buffer */
  476. mutex_lock(&dport->xmit_lock);
  477. chars = dport->xmit_cnt;
  478. mutex_unlock(&dport->xmit_lock);
  479. return chars;
  480. }
  481. static const struct tty_operations dashtty_ops = {
  482. .install = dashtty_install,
  483. .open = dashtty_open,
  484. .close = dashtty_close,
  485. .hangup = dashtty_hangup,
  486. .write = dashtty_write,
  487. .write_room = dashtty_write_room,
  488. .chars_in_buffer = dashtty_chars_in_buffer,
  489. };
  490. static int __init dashtty_init(void)
  491. {
  492. int ret;
  493. int nport;
  494. struct dashtty_port *dport;
  495. if (!metag_da_enabled())
  496. return -ENODEV;
  497. channel_driver = tty_alloc_driver(NUM_TTY_CHANNELS,
  498. TTY_DRIVER_REAL_RAW);
  499. if (IS_ERR(channel_driver))
  500. return PTR_ERR(channel_driver);
  501. channel_driver->driver_name = "metag_da";
  502. channel_driver->name = "ttyDA";
  503. channel_driver->major = DA_TTY_MAJOR;
  504. channel_driver->minor_start = 0;
  505. channel_driver->type = TTY_DRIVER_TYPE_SERIAL;
  506. channel_driver->subtype = SERIAL_TYPE_NORMAL;
  507. channel_driver->init_termios = tty_std_termios;
  508. channel_driver->init_termios.c_cflag |= CLOCAL;
  509. tty_set_operations(channel_driver, &dashtty_ops);
  510. for (nport = 0; nport < NUM_TTY_CHANNELS; nport++) {
  511. dport = &dashtty_ports[nport];
  512. tty_port_init(&dport->port);
  513. dport->port.ops = &dashtty_port_ops;
  514. spin_lock_init(&dport->rx_lock);
  515. mutex_init(&dport->xmit_lock);
  516. /* the xmit buffer starts empty, i.e. completely written */
  517. init_completion(&dport->xmit_empty);
  518. complete(&dport->xmit_empty);
  519. }
  520. setup_timer(&put_timer, dashtty_put_timer, 0);
  521. init_waitqueue_head(&dashtty_waitqueue);
  522. dashtty_thread = kthread_create(put_data, NULL, "ttyDA");
  523. if (IS_ERR(dashtty_thread)) {
  524. pr_err("Couldn't create dashtty thread\n");
  525. ret = PTR_ERR(dashtty_thread);
  526. goto err_destroy_ports;
  527. }
  528. /*
  529. * Bind the writer thread to the boot CPU so it can't migrate.
  530. * DA channels are per-CPU and we want all channel I/O to be on a single
  531. * predictable CPU.
  532. */
  533. kthread_bind(dashtty_thread, 0);
  534. wake_up_process(dashtty_thread);
  535. ret = tty_register_driver(channel_driver);
  536. if (ret < 0) {
  537. pr_err("Couldn't install dashtty driver: err %d\n",
  538. ret);
  539. goto err_stop_kthread;
  540. }
  541. return 0;
  542. err_stop_kthread:
  543. kthread_stop(dashtty_thread);
  544. err_destroy_ports:
  545. for (nport = 0; nport < NUM_TTY_CHANNELS; nport++) {
  546. dport = &dashtty_ports[nport];
  547. tty_port_destroy(&dport->port);
  548. }
  549. put_tty_driver(channel_driver);
  550. return ret;
  551. }
  552. device_initcall(dashtty_init);
  553. #ifdef CONFIG_DA_CONSOLE
  554. static void dash_console_write(struct console *co, const char *s,
  555. unsigned int count)
  556. {
  557. int actually_written;
  558. chancall(WRBUF, CONSOLE_CHANNEL, count, (void *)s, &actually_written);
  559. }
  560. static struct tty_driver *dash_console_device(struct console *c, int *index)
  561. {
  562. *index = c->index;
  563. return channel_driver;
  564. }
  565. struct console dash_console = {
  566. .name = "ttyDA",
  567. .write = dash_console_write,
  568. .device = dash_console_device,
  569. .flags = CON_PRINTBUFFER,
  570. .index = 1,
  571. };
  572. #endif