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 <asm/uaccess.h>
41 #include <asm/hvconsole.h>
47 #define TIMEOUT ((HZ + 99) / 100)
50 * Wait this long per iteration while trying to push buffered data to the
51 * hypervisor before allowing the tty to complete a close operation.
53 #define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
56 * The Linux TTY code does not support dynamic addition of tty derived devices
57 * so we need to know how many tty devices we might need when space is allocated
58 * for the tty device. Since this driver supports hotplug of vty adapters we
59 * need to make sure we have enough allocated.
61 #define HVC_ALLOC_TTY_ADAPTERS 8
63 static struct tty_driver *hvc_driver;
64 #ifdef CONFIG_MAGIC_SYSRQ
65 static int sysrq_pressed;
71 #define __ALIGNED__ __attribute__((__aligned__(8)))
76 struct tty_struct *tty;
79 char outbuf[N_OUTBUF] __ALIGNED__;
84 struct list_head next;
85 struct kobject kobj; /* ref count & hvc_struct lifetime */
89 /* dynamic list of hvc_struct instances */
90 static struct list_head hvc_structs = LIST_HEAD_INIT(hvc_structs);
93 * Protect the list of hvc_struct instances from inserts and removals during
96 static spinlock_t hvc_structs_lock = SPIN_LOCK_UNLOCKED;
99 * Initial console vtermnos for console API usage prior to full console
100 * initialization. Any vty adapter outside this range will not have usable
101 * console interfaces but can still be used as a tty device. This has to be
102 * static because kmalloc will not work during early console init.
104 static uint32_t vtermnos[MAX_NR_HVC_CONSOLES];
106 /* Used for accounting purposes */
107 static int num_vterms = 0;
109 static struct task_struct *hvc_task;
112 * This value is used to associate a tty->index value to a hvc_struct based
113 * upon order of exposure via hvc_probe().
115 static int hvc_count = -1;
117 /* Picks up late kicks after list walk but before schedule() */
118 static int hvc_kicked;
120 /* Wake the sleeping khvcd */
121 static void hvc_kick(void)
124 wake_up_process(hvc_task);
128 * NOTE: This API isn't used if the console adapter doesn't support interrupts.
129 * In this case the console is poll driven.
131 static irqreturn_t hvc_handle_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
137 static void hvc_unthrottle(struct tty_struct *tty)
143 * Do not call this function with either the hvc_strucst_lock or the hvc_struct
144 * lock held. If successful, this function increments the kobject reference
145 * count against the target hvc_struct so it should be released when finished.
147 struct hvc_struct *hvc_get_by_index(int index)
149 struct hvc_struct *hp;
152 spin_lock(&hvc_structs_lock);
154 list_for_each_entry(hp, &hvc_structs, next) {
155 spin_lock_irqsave(&hp->lock, flags);
156 if (hp->index == index) {
157 kobject_get(&hp->kobj);
158 spin_unlock_irqrestore(&hp->lock, flags);
159 spin_unlock(&hvc_structs_lock);
162 spin_unlock_irqrestore(&hp->lock, flags);
166 spin_unlock(&hvc_structs_lock);
171 * The TTY interface won't be used until after the vio layer has exposed the vty
172 * adapter to the kernel.
174 static int hvc_open(struct tty_struct *tty, struct file * filp)
176 struct hvc_struct *hp;
180 struct kobject *kobjp;
182 /* Auto increments kobject reference if found. */
183 if (!(hp = hvc_get_by_index(tty->index))) {
184 printk(KERN_WARNING "hvc_console: tty open failed, no vty associated with tty.\n");
188 spin_lock_irqsave(&hp->lock, flags);
189 /* Check and then increment for fast path open. */
190 if (hp->count++ > 0) {
191 spin_unlock_irqrestore(&hp->lock, flags);
194 } /* else count == 0 */
196 tty->driver_data = hp;
198 /* Save for request_irq outside of spin_lock. */
201 hp->irq_requested = 1;
205 spin_unlock_irqrestore(&hp->lock, flags);
206 /* check error, fallback to non-irq */
208 rc = request_irq(irq, hvc_handle_interrupt, SA_INTERRUPT, "hvc_console", hp);
211 * If the request_irq() fails and we return an error. The tty layer
212 * will call hvc_close() after a failed open but we don't want to clean
213 * up there so we'll clean up here and clear out the previously set
214 * tty fields and return the kobject reference.
217 spin_lock_irqsave(&hp->lock, flags);
219 hp->irq_requested = 0;
220 spin_unlock_irqrestore(&hp->lock, flags);
221 tty->driver_data = NULL;
224 /* Force wakeup of the polling thread */
230 static void hvc_close(struct tty_struct *tty, struct file * filp)
232 struct hvc_struct *hp;
233 struct kobject *kobjp;
237 if (tty_hung_up_p(filp))
241 * No driver_data means that this close was issued after a failed
242 * hvcs_open by the tty layer's release_dev() function and we can just
243 * exit cleanly because the kobject reference wasn't made.
245 if (!tty->driver_data)
248 hp = tty->driver_data;
249 spin_lock_irqsave(&hp->lock, flags);
252 if (--hp->count == 0) {
253 if (hp->irq_requested)
255 hp->irq_requested = 0;
257 /* We are done with the tty pointer now. */
259 spin_unlock_irqrestore(&hp->lock, flags);
262 * Chain calls chars_in_buffer() and returns immediately if
263 * there is no buffered data otherwise sleeps on a wait queue
264 * waking periodically to check chars_in_buffer().
266 tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
269 * Since the line disc doesn't block writes during tty close
270 * operations we'll set driver_data to NULL and then make sure
271 * to check tty->driver_data for NULL in hvc_write().
273 tty->driver_data = NULL;
280 printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
281 hp->vtermno, hp->count);
282 spin_unlock_irqrestore(&hp->lock, flags);
288 static void hvc_hangup(struct tty_struct *tty)
290 struct hvc_struct *hp = tty->driver_data;
294 struct kobject *kobjp;
296 spin_lock_irqsave(&hp->lock, flags);
298 temp_open_count = hp->count;
302 if (hp->irq_requested)
303 /* Saved for use outside of spin_lock. */
305 hp->irq_requested = 0;
306 spin_unlock_irqrestore(&hp->lock, flags);
309 while(temp_open_count) {
316 * Push buffered characters whether they were just recently buffered or waiting
317 * on a blocked hypervisor. Call this function with hp->lock held.
319 static void hvc_push(struct hvc_struct *hp)
323 n = hvc_put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
327 /* throw away output on error; this happens when
328 there is no session connected to the vterm. */
332 if (hp->n_outbuf > 0)
333 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
338 static inline int __hvc_write_user(struct hvc_struct *hp,
339 const unsigned char *buf, int count)
342 int tbsize, rsize, written = 0;
345 tbsize = min(count, (int)PAGE_SIZE);
346 if (!(tbuf = kmalloc(tbsize, GFP_KERNEL)))
349 while ((rsize = count - written) > 0) {
355 rsize -= copy_from_user(p, buf, rsize);
363 spin_lock_irqsave(&hp->lock, flags);
365 /* Push pending writes: make some room in buffer */
366 if (hp->n_outbuf > 0)
369 for (wsize = N_OUTBUF - hp->n_outbuf; rsize && wsize;
370 wsize = N_OUTBUF - hp->n_outbuf) {
373 memcpy(hp->outbuf + hp->n_outbuf, p, wsize);
374 hp->n_outbuf += wsize;
380 spin_unlock_irqrestore(&hp->lock, flags);
394 static inline int __hvc_write_kernel(struct hvc_struct *hp,
395 const unsigned char *buf, int count)
398 int rsize, written = 0;
400 spin_lock_irqsave(&hp->lock, flags);
402 /* Push pending writes */
403 if (hp->n_outbuf > 0)
406 while (count > 0 && (rsize = N_OUTBUF - hp->n_outbuf) > 0) {
409 memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
412 hp->n_outbuf += rsize;
416 spin_unlock_irqrestore(&hp->lock, flags);
420 static int hvc_write(struct tty_struct *tty, int from_user,
421 const unsigned char *buf, int count)
423 struct hvc_struct *hp = tty->driver_data;
426 /* This write was probably executed during a tty close. */
431 written = __hvc_write_user(hp, buf, count);
433 written = __hvc_write_kernel(hp, buf, count);
436 * Racy, but harmless, kick thread if there is still pending data.
437 * There really is nothing wrong with kicking the thread, even if there
438 * is no buffered data.
447 * This is actually a contract between the driver and the tty layer outlining
448 * how much write room the driver can guarentee will be sent OR BUFFERED. This
449 * driver MUST honor the return value.
451 static int hvc_write_room(struct tty_struct *tty)
453 struct hvc_struct *hp = tty->driver_data;
458 return N_OUTBUF - hp->n_outbuf;
461 static int hvc_chars_in_buffer(struct tty_struct *tty)
463 struct hvc_struct *hp = tty->driver_data;
470 #define HVC_POLL_READ 0x00000001
471 #define HVC_POLL_WRITE 0x00000002
472 #define HVC_POLL_QUICK 0x00000004
474 static int hvc_poll(struct hvc_struct *hp)
476 struct tty_struct *tty;
477 int i, n, poll_mask = 0;
478 char buf[N_INBUF] __ALIGNED__;
482 spin_lock_irqsave(&hp->lock, flags);
484 /* Push pending writes */
485 if (hp->n_outbuf > 0)
487 /* Reschedule us if still some write pending */
488 if (hp->n_outbuf > 0)
489 poll_mask |= HVC_POLL_WRITE;
491 /* No tty attached, just skip */
496 /* Now check if we can get data (are we throttled ?) */
497 if (test_bit(TTY_THROTTLED, &tty->flags))
500 /* If we aren't interrupt driven and aren't throttled, we always
501 * request a reschedule
503 if (hp->irq == NO_IRQ)
504 poll_mask |= HVC_POLL_READ;
506 /* Read data if any */
509 if (count > (TTY_FLIPBUF_SIZE - tty->flip.count))
510 count = TTY_FLIPBUF_SIZE - tty->flip.count;
512 /* If flip is full, just reschedule a later read */
514 poll_mask |= HVC_POLL_READ;
518 n = hvc_get_chars(hp->vtermno, buf, count);
520 /* Hangup the tty when disconnected from host */
522 spin_unlock_irqrestore(&hp->lock, flags);
524 spin_lock_irqsave(&hp->lock, flags);
528 for (i = 0; i < n; ++i) {
529 #ifdef CONFIG_MAGIC_SYSRQ
530 /* Handle the SysRq Hack */
531 if (buf[i] == '\x0f') { /* ^O -- should support a sequence */
534 } else if (sysrq_pressed) {
535 handle_sysrq(buf[i], NULL, tty);
539 #endif /* CONFIG_MAGIC_SYSRQ */
540 tty_insert_flip_char(tty, buf[i], 0);
544 tty_schedule_flip(tty);
547 * Account for the total amount read in one loop, and if above
548 * 64 bytes, we do a quick schedule loop to let the tty grok the
549 * data and eventually throttle us.
552 if (read_total >= 64) {
553 poll_mask |= HVC_POLL_QUICK;
558 /* Wakeup write queue if necessary */
564 spin_unlock_irqrestore(&hp->lock, flags);
569 #if defined(CONFIG_XMON) && defined(CONFIG_SMP)
570 extern cpumask_t cpus_in_xmon;
572 static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
576 * This kthread is either polling or interrupt driven. This is determined by
577 * calling hvc_poll() who determines whether a console adapter support
580 int khvcd(void *unused)
583 struct hvc_struct *hp;
585 __set_current_state(TASK_RUNNING);
590 if (cpus_empty(cpus_in_xmon)) {
591 spin_lock(&hvc_structs_lock);
592 list_for_each_entry(hp, &hvc_structs, next) {
593 /*hp = list_entry(node, struct hvc_struct, * next); */
594 poll_mask |= hvc_poll(hp);
596 spin_unlock(&hvc_structs_lock);
598 poll_mask |= HVC_POLL_READ;
601 if (poll_mask & HVC_POLL_QUICK) {
605 set_current_state(TASK_INTERRUPTIBLE);
610 schedule_timeout(TIMEOUT);
612 __set_current_state(TASK_RUNNING);
613 } while (!kthread_should_stop());
618 static struct tty_operations hvc_ops = {
622 .hangup = hvc_hangup,
623 .unthrottle = hvc_unthrottle,
624 .write_room = hvc_write_room,
625 .chars_in_buffer = hvc_chars_in_buffer,
628 char hvc_driver_name[] = "hvc_console";
630 static struct vio_device_id hvc_driver_table[] __devinitdata= {
631 {"serial", "hvterm1"},
634 MODULE_DEVICE_TABLE(vio, hvc_driver_table);
636 /* callback when the kboject ref count reaches zero. */
637 static void destroy_hvc_struct(struct kobject *kobj)
639 struct hvc_struct *hp = container_of(kobj, struct hvc_struct, kobj);
642 spin_lock(&hvc_structs_lock);
644 spin_lock_irqsave(&hp->lock, flags);
645 list_del(&(hp->next));
646 spin_unlock_irqrestore(&hp->lock, flags);
648 spin_unlock(&hvc_structs_lock);
653 static struct kobj_type hvc_kobj_type = {
654 .release = destroy_hvc_struct,
657 static int __devinit hvc_probe(
659 const struct vio_device_id *id)
661 struct hvc_struct *hp;
663 /* probed with invalid parameters. */
667 hp = kmalloc(sizeof(*hp), GFP_KERNEL);
671 memset(hp, 0x00, sizeof(*hp));
672 hp->vtermno = dev->unit_address;
674 hp->vdev->dev.driver_data = hp;
677 kobject_init(&hp->kobj);
678 hp->kobj.ktype = &hvc_kobj_type;
680 hp->lock = SPIN_LOCK_UNLOCKED;
681 spin_lock(&hvc_structs_lock);
682 hp->index = ++hvc_count;
683 list_add_tail(&(hp->next), &hvc_structs);
684 spin_unlock(&hvc_structs_lock);
689 static int __devexit hvc_remove(struct vio_dev *dev)
691 struct hvc_struct *hp = dev->dev.driver_data;
693 struct kobject *kobjp;
694 struct tty_struct *tty;
696 spin_lock_irqsave(&hp->lock, flags);
700 if (hp->index < MAX_NR_HVC_CONSOLES)
701 vtermnos[hp->index] = -1;
703 /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
705 spin_unlock_irqrestore(&hp->lock, flags);
708 * We 'put' the instance that was grabbed when the kobject instance
709 * was intialized using kobject_init(). Let the last holder of this
710 * kobject cause it to be removed, which will probably be the tty_hangup
716 * This function call will auto chain call hvc_hangup. The tty should
717 * always be valid at this time unless a simultaneous tty close already
718 * cleaned up the hvc_struct.
725 static struct vio_driver hvc_vio_driver = {
726 .name = hvc_driver_name,
727 .id_table = hvc_driver_table,
729 .remove = hvc_remove,
732 /* Driver initialization. Follow console initialization. This is where the TTY
733 * interfaces start to become available. */
734 int __init hvc_init(void)
738 /* We need more than num_vterms adapters due to hotplug additions. */
739 hvc_driver = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
740 /* hvc_driver = alloc_tty_driver(num_vterms); */
744 hvc_driver->owner = THIS_MODULE;
745 hvc_driver->devfs_name = "hvc/";
746 hvc_driver->driver_name = "hvc";
747 hvc_driver->name = "hvc";
748 hvc_driver->major = HVC_MAJOR;
749 hvc_driver->minor_start = HVC_MINOR;
750 hvc_driver->type = TTY_DRIVER_TYPE_SYSTEM;
751 hvc_driver->init_termios = tty_std_termios;
752 hvc_driver->flags = TTY_DRIVER_REAL_RAW;
753 tty_set_operations(hvc_driver, &hvc_ops);
755 if (tty_register_driver(hvc_driver))
756 panic("Couldn't register hvc console driver\n");
758 /* Always start the kthread because there can be hotplug vty adapters
760 hvc_task = kthread_run(khvcd, NULL, "khvcd");
761 if (IS_ERR(hvc_task)) {
762 panic("Couldn't create kthread for console.\n");
763 put_tty_driver(hvc_driver);
767 /* Register as a vio device to receive callbacks */
768 rc = vio_register_driver(&hvc_vio_driver);
773 /* This isn't particularily necessary due to this being a console driver but it
774 * is nice to be thorough */
775 static void __exit hvc_exit(void)
777 kthread_stop(hvc_task);
779 vio_unregister_driver(&hvc_vio_driver);
780 tty_unregister_driver(hvc_driver);
781 /* return tty_struct instances allocated in hvc_init(). */
782 put_tty_driver(hvc_driver);
786 * Console APIs, NOT TTY. These APIs are available immediately when
787 * hvc_console_setup() finds adapters.
791 * hvc_instantiate() is an early console discovery method which locates consoles
792 * prior to the vio subsystem discovering them. Hotplugged vty adapters do NOT
793 * get an hvc_instantiate() callback since the appear after early console init.
795 int hvc_instantiate(uint32_t vtermno, int index)
797 if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
800 if (vtermnos[index] != -1)
803 vtermnos[index] = vtermno;
807 void hvc_console_print(struct console *co, const char *b, unsigned count)
809 char c[16] __ALIGNED__;
810 unsigned i = 0, n = 0;
813 /* Console access attempt outside of acceptable console range. */
814 if (co->index >= MAX_NR_HVC_CONSOLES)
817 /* This console adapter was removed so it is not useable. */
818 if (vtermnos[co->index] < 0)
821 while (count > 0 || i > 0) {
822 if (count > 0 && i < sizeof(c)) {
823 if (b[n] == '\n' && !donecr) {
832 r = hvc_put_chars(vtermnos[co->index], c, i);
834 /* throw away chars on error */
845 static struct tty_driver *hvc_console_device(struct console *c, int *index)
851 static int __init hvc_console_setup(struct console *co, char *options)
856 struct console hvc_con_driver = {
858 .write = hvc_console_print,
859 .device = hvc_console_device,
860 .setup = hvc_console_setup,
861 .flags = CON_PRINTBUFFER,
865 /* Early console initialization. Preceeds driver initialization. */
866 static int __init hvc_console_init(void)
870 for (i=0; i<MAX_NR_HVC_CONSOLES; i++)
872 num_vterms = hvc_find_vtys();
873 register_console(&hvc_con_driver);
876 console_initcall(hvc_console_init);
878 module_init(hvc_init);
879 module_exit(hvc_exit);