2 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
3 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
4 * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
5 * Copyright (C) 2004 IBM Corporation
7 * Additional Author(s):
8 * Ryan S. Arnold <rsa@us.ibm.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/console.h>
26 #include <linux/cpumask.h>
27 #include <linux/init.h>
28 #include <linux/kbd_kern.h>
29 #include <linux/kernel.h>
30 #include <linux/kobject.h>
31 #include <linux/kthread.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
34 #include <linux/major.h>
35 #include <linux/sysrq.h>
36 #include <linux/tty.h>
37 #include <linux/tty_flip.h>
38 #include <linux/sched.h>
39 #include <linux/spinlock.h>
40 #include <linux/delay.h>
41 #include <asm/uaccess.h>
42 #include <asm/hvconsole.h>
51 * Wait this long per iteration while trying to push buffered data to the
52 * hypervisor before allowing the tty to complete a close operation.
54 #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
57 * The Linux TTY code does not support dynamic addition of tty derived devices
58 * so we need to know how many tty devices we might need when space is allocated
59 * for the tty device. Since this driver supports hotplug of vty adapters we
60 * need to make sure we have enough allocated.
62 #define HVC_ALLOC_TTY_ADAPTERS 8
64 static struct tty_driver *hvc_driver;
65 #ifdef CONFIG_MAGIC_SYSRQ
66 static int sysrq_pressed;
72 #define __ALIGNED__ __attribute__((__aligned__(8)))
77 struct tty_struct *tty;
80 char outbuf[N_OUTBUF] __ALIGNED__;
85 struct list_head next;
86 struct kobject kobj; /* ref count & hvc_struct lifetime */
90 /* dynamic list of hvc_struct instances */
91 static struct list_head hvc_structs = LIST_HEAD_INIT(hvc_structs);
94 * Protect the list of hvc_struct instances from inserts and removals during
97 static DEFINE_SPINLOCK(hvc_structs_lock);
100 * Initial console vtermnos for console API usage prior to full console
101 * initialization. Any vty adapter outside this range will not have usable
102 * console interfaces but can still be used as a tty device. This has to be
103 * static because kmalloc will not work during early console init.
105 static uint32_t vtermnos[MAX_NR_HVC_CONSOLES];
107 /* Used for accounting purposes */
108 static int num_vterms = 0;
110 static struct task_struct *hvc_task;
113 * This value is used to associate a tty->index value to a hvc_struct based
114 * upon order of exposure via hvc_probe().
116 static int hvc_count = -1;
118 /* Picks up late kicks after list walk but before schedule() */
119 static int hvc_kicked;
121 /* Wake the sleeping khvcd */
122 static void hvc_kick(void)
125 wake_up_process(hvc_task);
129 * NOTE: This API isn't used if the console adapter doesn't support interrupts.
130 * In this case the console is poll driven.
132 static irqreturn_t hvc_handle_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
138 static void hvc_unthrottle(struct tty_struct *tty)
144 * Do not call this function with either the hvc_strucst_lock or the hvc_struct
145 * lock held. If successful, this function increments the kobject reference
146 * count against the target hvc_struct so it should be released when finished.
148 struct hvc_struct *hvc_get_by_index(int index)
150 struct hvc_struct *hp;
153 spin_lock(&hvc_structs_lock);
155 list_for_each_entry(hp, &hvc_structs, next) {
156 spin_lock_irqsave(&hp->lock, flags);
157 if (hp->index == index) {
158 kobject_get(&hp->kobj);
159 spin_unlock_irqrestore(&hp->lock, flags);
160 spin_unlock(&hvc_structs_lock);
163 spin_unlock_irqrestore(&hp->lock, flags);
167 spin_unlock(&hvc_structs_lock);
172 * The TTY interface won't be used until after the vio layer has exposed the vty
173 * adapter to the kernel.
175 static int hvc_open(struct tty_struct *tty, struct file * filp)
177 struct hvc_struct *hp;
181 struct kobject *kobjp;
183 /* Auto increments kobject reference if found. */
184 if (!(hp = hvc_get_by_index(tty->index))) {
185 printk(KERN_WARNING "hvc_console: tty open failed, no vty associated with tty.\n");
189 spin_lock_irqsave(&hp->lock, flags);
190 /* Check and then increment for fast path open. */
191 if (hp->count++ > 0) {
192 spin_unlock_irqrestore(&hp->lock, flags);
195 } /* else count == 0 */
197 tty->driver_data = hp;
199 /* Save for request_irq outside of spin_lock. */
202 hp->irq_requested = 1;
206 spin_unlock_irqrestore(&hp->lock, flags);
207 /* check error, fallback to non-irq */
209 rc = request_irq(irq, hvc_handle_interrupt, SA_INTERRUPT, "hvc_console", hp);
212 * If the request_irq() fails and we return an error. The tty layer
213 * will call hvc_close() after a failed open but we don't want to clean
214 * up there so we'll clean up here and clear out the previously set
215 * tty fields and return the kobject reference.
218 spin_lock_irqsave(&hp->lock, flags);
220 hp->irq_requested = 0;
221 spin_unlock_irqrestore(&hp->lock, flags);
222 tty->driver_data = NULL;
224 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
226 /* Force wakeup of the polling thread */
232 static void hvc_close(struct tty_struct *tty, struct file * filp)
234 struct hvc_struct *hp;
235 struct kobject *kobjp;
239 if (tty_hung_up_p(filp))
243 * No driver_data means that this close was issued after a failed
244 * hvc_open by the tty layer's release_dev() function and we can just
245 * exit cleanly because the kobject reference wasn't made.
247 if (!tty->driver_data)
250 hp = tty->driver_data;
251 spin_lock_irqsave(&hp->lock, flags);
254 if (--hp->count == 0) {
255 if (hp->irq_requested)
257 hp->irq_requested = 0;
259 /* We are done with the tty pointer now. */
261 spin_unlock_irqrestore(&hp->lock, flags);
264 * Chain calls chars_in_buffer() and returns immediately if
265 * there is no buffered data otherwise sleeps on a wait queue
266 * waking periodically to check chars_in_buffer().
268 tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
275 printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
276 hp->vtermno, hp->count);
277 spin_unlock_irqrestore(&hp->lock, flags);
283 static void hvc_hangup(struct tty_struct *tty)
285 struct hvc_struct *hp = tty->driver_data;
289 struct kobject *kobjp;
294 spin_lock_irqsave(&hp->lock, flags);
297 * The N_TTY line discipline has problems such that in a close vs
298 * open->hangup case this can be called after the final close so prevent
299 * that from happening for now.
301 if (hp->count <= 0) {
302 spin_unlock_irqrestore(&hp->lock, flags);
307 temp_open_count = hp->count;
311 if (hp->irq_requested)
312 /* Saved for use outside of spin_lock. */
314 hp->irq_requested = 0;
315 spin_unlock_irqrestore(&hp->lock, flags);
318 while(temp_open_count) {
325 * Push buffered characters whether they were just recently buffered or waiting
326 * on a blocked hypervisor. Call this function with hp->lock held.
328 static void hvc_push(struct hvc_struct *hp)
332 n = hvc_put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
336 /* throw away output on error; this happens when
337 there is no session connected to the vterm. */
341 if (hp->n_outbuf > 0)
342 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
347 static inline int __hvc_write_kernel(struct hvc_struct *hp,
348 const unsigned char *buf, int count)
351 int rsize, written = 0;
353 spin_lock_irqsave(&hp->lock, flags);
355 /* Push pending writes */
356 if (hp->n_outbuf > 0)
359 while (count > 0 && (rsize = N_OUTBUF - hp->n_outbuf) > 0) {
362 memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
365 hp->n_outbuf += rsize;
369 spin_unlock_irqrestore(&hp->lock, flags);
373 static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
375 struct hvc_struct *hp = tty->driver_data;
378 /* This write was probably executed during a tty close. */
385 written = __hvc_write_kernel(hp, buf, count);
388 * Racy, but harmless, kick thread if there is still pending data.
389 * There really is nothing wrong with kicking the thread, even if there
390 * is no buffered data.
399 * This is actually a contract between the driver and the tty layer outlining
400 * how much write room the driver can guarentee will be sent OR BUFFERED. This
401 * driver MUST honor the return value.
403 static int hvc_write_room(struct tty_struct *tty)
405 struct hvc_struct *hp = tty->driver_data;
410 return N_OUTBUF - hp->n_outbuf;
413 static int hvc_chars_in_buffer(struct tty_struct *tty)
415 struct hvc_struct *hp = tty->driver_data;
422 #define HVC_POLL_READ 0x00000001
423 #define HVC_POLL_WRITE 0x00000002
424 #define HVC_POLL_QUICK 0x00000004
426 static int hvc_poll(struct hvc_struct *hp)
428 struct tty_struct *tty;
429 int i, n, poll_mask = 0;
430 char buf[N_INBUF] __ALIGNED__;
434 spin_lock_irqsave(&hp->lock, flags);
436 /* Push pending writes */
437 if (hp->n_outbuf > 0)
439 /* Reschedule us if still some write pending */
440 if (hp->n_outbuf > 0)
441 poll_mask |= HVC_POLL_WRITE;
443 /* No tty attached, just skip */
448 /* Now check if we can get data (are we throttled ?) */
449 if (test_bit(TTY_THROTTLED, &tty->flags))
452 /* If we aren't interrupt driven and aren't throttled, we always
453 * request a reschedule
455 if (hp->irq == NO_IRQ)
456 poll_mask |= HVC_POLL_READ;
458 /* Read data if any */
461 if (count > (TTY_FLIPBUF_SIZE - tty->flip.count))
462 count = TTY_FLIPBUF_SIZE - tty->flip.count;
464 /* If flip is full, just reschedule a later read */
466 poll_mask |= HVC_POLL_READ;
470 n = hvc_get_chars(hp->vtermno, buf, count);
472 /* Hangup the tty when disconnected from host */
474 spin_unlock_irqrestore(&hp->lock, flags);
476 spin_lock_irqsave(&hp->lock, flags);
480 for (i = 0; i < n; ++i) {
481 #ifdef CONFIG_MAGIC_SYSRQ
482 /* Handle the SysRq Hack */
483 if (buf[i] == '\x0f') { /* ^O -- should support a sequence */
486 } else if (sysrq_pressed) {
487 handle_sysrq(buf[i], NULL, tty);
491 #endif /* CONFIG_MAGIC_SYSRQ */
492 tty_insert_flip_char(tty, buf[i], 0);
496 tty_schedule_flip(tty);
499 * Account for the total amount read in one loop, and if above
500 * 64 bytes, we do a quick schedule loop to let the tty grok the
501 * data and eventually throttle us.
504 if (read_total >= 64) {
505 poll_mask |= HVC_POLL_QUICK;
510 /* Wakeup write queue if necessary */
516 spin_unlock_irqrestore(&hp->lock, flags);
521 #if defined(CONFIG_XMON) && defined(CONFIG_SMP)
522 extern cpumask_t cpus_in_xmon;
524 static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
528 * This kthread is either polling or interrupt driven. This is determined by
529 * calling hvc_poll() who determines whether a console adapter support
532 int khvcd(void *unused)
535 struct hvc_struct *hp;
537 __set_current_state(TASK_RUNNING);
542 if (cpus_empty(cpus_in_xmon)) {
543 spin_lock(&hvc_structs_lock);
544 list_for_each_entry(hp, &hvc_structs, next) {
545 /*hp = list_entry(node, struct hvc_struct, * next); */
546 poll_mask |= hvc_poll(hp);
548 spin_unlock(&hvc_structs_lock);
550 poll_mask |= HVC_POLL_READ;
553 if (poll_mask & HVC_POLL_QUICK) {
557 set_current_state(TASK_INTERRUPTIBLE);
562 msleep_interruptible(TIMEOUT);
564 __set_current_state(TASK_RUNNING);
565 } while (!kthread_should_stop());
570 static struct tty_operations hvc_ops = {
574 .hangup = hvc_hangup,
575 .unthrottle = hvc_unthrottle,
576 .write_room = hvc_write_room,
577 .chars_in_buffer = hvc_chars_in_buffer,
580 char hvc_driver_name[] = "hvc_console";
582 static struct vio_device_id hvc_driver_table[] __devinitdata= {
583 {"serial", "hvterm1"},
586 MODULE_DEVICE_TABLE(vio, hvc_driver_table);
588 /* callback when the kboject ref count reaches zero. */
589 static void destroy_hvc_struct(struct kobject *kobj)
591 struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
594 spin_lock(&hvc_structs_lock);
596 spin_lock_irqsave(&hp->lock, flags);
597 list_del(&(hp->next));
598 spin_unlock_irqrestore(&hp->lock, flags);
600 spin_unlock(&hvc_structs_lock);
605 static struct kobj_type hvc_kobj_type = {
606 .release = destroy_hvc_struct,
609 static int __devinit hvc_probe(
611 const struct vio_device_id *id)
613 struct hvc_struct *hp;
615 /* probed with invalid parameters. */
619 hp = kmalloc(sizeof(*hp), GFP_KERNEL);
623 memset(hp, 0x00, sizeof(*hp));
624 hp->vtermno = dev->unit_address;
626 hp->vdev->dev.driver_data = hp;
629 kobject_init(&hp->kobj);
630 hp->kobj.ktype = &hvc_kobj_type;
632 spin_lock_init(&hp->lock);
633 spin_lock(&hvc_structs_lock);
634 hp->index = ++hvc_count;
635 list_add_tail(&(hp->next), &hvc_structs);
636 spin_unlock(&hvc_structs_lock);
641 static int __devexit hvc_remove(struct vio_dev *dev)
643 struct hvc_struct *hp = dev->dev.driver_data;
645 struct kobject *kobjp;
646 struct tty_struct *tty;
648 spin_lock_irqsave(&hp->lock, flags);
652 if (hp->index < MAX_NR_HVC_CONSOLES)
653 vtermnos[hp->index] = -1;
655 /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
657 spin_unlock_irqrestore(&hp->lock, flags);
660 * We 'put' the instance that was grabbed when the kobject instance
661 * was intialized using kobject_init(). Let the last holder of this
662 * kobject cause it to be removed, which will probably be the tty_hangup
668 * This function call will auto chain call hvc_hangup. The tty should
669 * always be valid at this time unless a simultaneous tty close already
670 * cleaned up the hvc_struct.
677 static struct vio_driver hvc_vio_driver = {
678 .name = hvc_driver_name,
679 .id_table = hvc_driver_table,
681 .remove = hvc_remove,
684 /* Driver initialization. Follow console initialization. This is where the TTY
685 * interfaces start to become available. */
686 int __init hvc_init(void)
690 /* We need more than num_vterms adapters due to hotplug additions. */
691 hvc_driver = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
692 /* hvc_driver = alloc_tty_driver(num_vterms); */
696 hvc_driver->owner = THIS_MODULE;
697 hvc_driver->devfs_name = "hvc/";
698 hvc_driver->driver_name = "hvc";
699 hvc_driver->name = "hvc";
700 hvc_driver->major = HVC_MAJOR;
701 hvc_driver->minor_start = HVC_MINOR;
702 hvc_driver->type = TTY_DRIVER_TYPE_SYSTEM;
703 hvc_driver->init_termios = tty_std_termios;
704 hvc_driver->flags = TTY_DRIVER_REAL_RAW;
705 tty_set_operations(hvc_driver, &hvc_ops);
707 if (tty_register_driver(hvc_driver))
708 panic("Couldn't register hvc console driver\n");
710 /* Always start the kthread because there can be hotplug vty adapters
712 hvc_task = kthread_run(khvcd, NULL, "khvcd");
713 if (IS_ERR(hvc_task)) {
714 panic("Couldn't create kthread for console.\n");
715 put_tty_driver(hvc_driver);
719 /* Register as a vio device to receive callbacks */
720 rc = vio_register_driver(&hvc_vio_driver);
725 /* This isn't particularily necessary due to this being a console driver but it
726 * is nice to be thorough */
727 static void __exit hvc_exit(void)
729 kthread_stop(hvc_task);
731 vio_unregister_driver(&hvc_vio_driver);
732 tty_unregister_driver(hvc_driver);
733 /* return tty_struct instances allocated in hvc_init(). */
734 put_tty_driver(hvc_driver);
738 * Console APIs, NOT TTY. These APIs are available immediately when
739 * hvc_console_setup() finds adapters.
743 * hvc_instantiate() is an early console discovery method which locates consoles
744 * prior to the vio subsystem discovering them. Hotplugged vty adapters do NOT
745 * get an hvc_instantiate() callback since the appear after early console init.
747 int hvc_instantiate(uint32_t vtermno, int index)
749 if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
752 if (vtermnos[index] != -1)
755 vtermnos[index] = vtermno;
759 void hvc_console_print(struct console *co, const char *b, unsigned count)
761 char c[16] __ALIGNED__;
762 unsigned i = 0, n = 0;
765 /* Console access attempt outside of acceptable console range. */
766 if (co->index >= MAX_NR_HVC_CONSOLES)
769 /* This console adapter was removed so it is not useable. */
770 if (vtermnos[co->index] < 0)
773 while (count > 0 || i > 0) {
774 if (count > 0 && i < sizeof(c)) {
775 if (b[n] == '\n' && !donecr) {
784 r = hvc_put_chars(vtermnos[co->index], c, i);
786 /* throw away chars on error */
797 static struct tty_driver *hvc_console_device(struct console *c, int *index)
803 static int __init hvc_console_setup(struct console *co, char *options)
808 struct console hvc_con_driver = {
810 .write = hvc_console_print,
811 .device = hvc_console_device,
812 .setup = hvc_console_setup,
813 .flags = CON_PRINTBUFFER,
817 /* Early console initialization. Preceeds driver initialization. */
818 static int __init hvc_console_init(void)
822 for (i=0; i<MAX_NR_HVC_CONSOLES; i++)
824 num_vterms = hvc_find_vtys();
825 register_console(&hvc_con_driver);
828 console_initcall(hvc_console_init);
830 module_init(hvc_init);
831 module_exit(hvc_exit);