hvc_vio.c 11.4 KB
Newer Older
1 2 3
/*
 * vio driver interface to hvc_console.c
 *
L
Lucas De Marchi 已提交
4
 * This code was moved here to allow the remaining code to be reused as a
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * generic polling mode with semi-reliable transport driver core to the
 * console and tty subsystems.
 *
 *
 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
 * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
 * Copyright (C) 2004 IBM Corporation
 *
 * Additional Author(s):
 *  Ryan S. Arnold <rsa@us.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
30 31 32 33 34
 *
 * TODO:
 *
 *   - handle error in sending hvsi protocol packets
 *   - retry nego on subsequent sends ?
35 36
 */

37 38
#undef DEBUG

39 40
#include <linux/types.h>
#include <linux/init.h>
41 42 43
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/console.h>
44

45 46 47
#include <asm/hvconsole.h>
#include <asm/vio.h>
#include <asm/prom.h>
48 49
#include <asm/hvsi.h>
#include <asm/udbg.h>
50
#include <asm/machdep.h>
51

52 53
#include "hvc_console.h"

54
static const char hvc_driver_name[] = "hvc_console";
55

B
Bill Pemberton 已提交
56
static struct vio_device_id hvc_driver_table[] = {
57
	{"serial", "hvterm1"},
58 59 60
#ifndef HVC_OLD_HVSI
	{"serial", "hvterm-protocol"},
#endif
61
	{ "", "" }
62 63
};

64 65 66 67 68 69
typedef enum hv_protocol {
	HV_PROTOCOL_RAW,
	HV_PROTOCOL_HVSI
} hv_protocol_t;

struct hvterm_priv {
70 71 72
	u32			termno;	/* HV term number */
	hv_protocol_t		proto;	/* Raw data or HVSI packets */
	struct hvsi_priv	hvsi;	/* HVSI specific data */
73 74 75 76
	spinlock_t		buf_lock;
	char			buf[SIZE_VIO_GET_CHARS];
	int			left;
	int			offset;
77 78 79 80 81 82
};
static struct hvterm_priv *hvterm_privs[MAX_NR_HVC_CONSOLES];
/* For early boot console */
static struct hvterm_priv hvterm_priv0;

static int hvterm_raw_get_chars(uint32_t vtermno, char *buf, int count)
83
{
84
	struct hvterm_priv *pv = hvterm_privs[vtermno];
85 86 87
	unsigned long i;
	unsigned long flags;
	int got;
88 89 90

	if (WARN_ON(!pv))
		return 0;
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	spin_lock_irqsave(&pv->buf_lock, flags);

	if (pv->left == 0) {
		pv->offset = 0;
		pv->left = hvc_get_chars(pv->termno, pv->buf, count);

		/*
		 * Work around a HV bug where it gives us a null
		 * after every \r.  -- paulus
		 */
		for (i = 1; i < pv->left; ++i) {
			if (pv->buf[i] == 0 && pv->buf[i-1] == '\r') {
				--pv->left;
				if (i < pv->left) {
					memmove(&pv->buf[i], &pv->buf[i+1],
						pv->left - i);
				}
			}
110 111
		}
	}
112 113 114 115 116 117 118 119

	got = min(count, pv->left);
	memcpy(buf, &pv->buf[pv->offset], got);
	pv->offset += got;
	pv->left -= got;

	spin_unlock_irqrestore(&pv->buf_lock, flags);

120 121 122
	return got;
}

123 124 125 126 127 128 129 130 131 132 133 134 135
static int hvterm_raw_put_chars(uint32_t vtermno, const char *buf, int count)
{
	struct hvterm_priv *pv = hvterm_privs[vtermno];

	if (WARN_ON(!pv))
		return 0;

	return hvc_put_chars(pv->termno, buf, count);
}

static const struct hv_ops hvterm_raw_ops = {
	.get_chars = hvterm_raw_get_chars,
	.put_chars = hvterm_raw_put_chars,
136 137
	.notifier_add = notifier_add_irq,
	.notifier_del = notifier_del_irq,
138
	.notifier_hangup = notifier_hangup_irq,
139 140
};

141 142 143 144 145 146 147
static int hvterm_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
{
	struct hvterm_priv *pv = hvterm_privs[vtermno];

	if (WARN_ON(!pv))
		return 0;

148
	return hvsilib_get_chars(&pv->hvsi, buf, count);
149 150 151 152 153 154 155 156 157
}

static int hvterm_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
{
	struct hvterm_priv *pv = hvterm_privs[vtermno];

	if (WARN_ON(!pv))
		return 0;

158
	return hvsilib_put_chars(&pv->hvsi, buf, count);
159 160 161 162 163 164 165 166 167 168 169 170 171
}

static int hvterm_hvsi_open(struct hvc_struct *hp, int data)
{
	struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
	int rc;

	pr_devel("HVSI@%x: open !\n", pv->termno);

	rc = notifier_add_irq(hp, data);
	if (rc)
		return rc;

172
	return hvsilib_open(&pv->hvsi, hp);
173 174 175 176 177 178
}

static void hvterm_hvsi_close(struct hvc_struct *hp, int data)
{
	struct hvterm_priv *pv = hvterm_privs[hp->vtermno];

179
	pr_devel("HVSI@%x: do close !\n", pv->termno);
180

181
	hvsilib_close(&pv->hvsi, hp);
182 183 184 185 186 187 188 189

	notifier_del_irq(hp, data);
}

void hvterm_hvsi_hangup(struct hvc_struct *hp, int data)
{
	struct hvterm_priv *pv = hvterm_privs[hp->vtermno];

190
	pr_devel("HVSI@%x: do hangup !\n", pv->termno);
191

192
	hvsilib_close(&pv->hvsi, hp);
193 194 195 196 197 198 199 200 201 202

	notifier_hangup_irq(hp, data);
}

static int hvterm_hvsi_tiocmget(struct hvc_struct *hp)
{
	struct hvterm_priv *pv = hvterm_privs[hp->vtermno];

	if (!pv)
		return -EINVAL;
203
	return pv->hvsi.mctrl;
204 205 206 207 208 209 210 211 212 213 214
}

static int hvterm_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
				unsigned int clear)
{
	struct hvterm_priv *pv = hvterm_privs[hp->vtermno];

	pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
		 pv->termno, set, clear);

	if (set & TIOCM_DTR)
215
		hvsilib_write_mctrl(&pv->hvsi, 1);
216
	else if (clear & TIOCM_DTR)
217
		hvsilib_write_mctrl(&pv->hvsi, 0);
218 219 220 221 222 223 224 225 226 227 228 229 230 231

	return 0;
}

static const struct hv_ops hvterm_hvsi_ops = {
	.get_chars = hvterm_hvsi_get_chars,
	.put_chars = hvterm_hvsi_put_chars,
	.notifier_add = hvterm_hvsi_open,
	.notifier_del = hvterm_hvsi_close,
	.notifier_hangup = hvterm_hvsi_hangup,
	.tiocmget = hvterm_hvsi_tiocmget,
	.tiocmset = hvterm_hvsi_tiocmset,
};

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static void udbg_hvc_putc(char c)
{
	int count = -1;

	if (!hvterm_privs[0])
		return;

	if (c == '\n')
		udbg_hvc_putc('\r');

	do {
		switch(hvterm_privs[0]->proto) {
		case HV_PROTOCOL_RAW:
			count = hvterm_raw_put_chars(0, &c, 1);
			break;
		case HV_PROTOCOL_HVSI:
			count = hvterm_hvsi_put_chars(0, &c, 1);
			break;
		}
	} while(count == 0);
}

static int udbg_hvc_getc_poll(void)
{
	int rc = 0;
	char c;

	if (!hvterm_privs[0])
		return -1;

	switch(hvterm_privs[0]->proto) {
	case HV_PROTOCOL_RAW:
		rc = hvterm_raw_get_chars(0, &c, 1);
		break;
	case HV_PROTOCOL_HVSI:
		rc = hvterm_hvsi_get_chars(0, &c, 1);
		break;
	}
	if (!rc)
		return -1;
	return c;
}

static int udbg_hvc_getc(void)
{
	int ch;

	if (!hvterm_privs[0])
		return -1;

	for (;;) {
		ch = udbg_hvc_getc_poll();
		if (ch == -1) {
			/* This shouldn't be needed...but... */
			volatile unsigned long delay;
			for (delay=0; delay < 2000000; delay++)
				;
		} else {
			return ch;
		}
	}
}

B
Bill Pemberton 已提交
295
static int hvc_vio_probe(struct vio_dev *vdev,
296
				   const struct vio_device_id *id)
297
{
298
	const struct hv_ops *ops;
299
	struct hvc_struct *hp;
300 301 302
	struct hvterm_priv *pv;
	hv_protocol_t proto;
	int i, termno = -1;
303 304 305 306 307

	/* probed with invalid parameters. */
	if (!vdev || !id)
		return -EPERM;

308 309 310 311 312 313 314
	if (of_device_is_compatible(vdev->dev.of_node, "hvterm1")) {
		proto = HV_PROTOCOL_RAW;
		ops = &hvterm_raw_ops;
	} else if (of_device_is_compatible(vdev->dev.of_node, "hvterm-protocol")) {
		proto = HV_PROTOCOL_HVSI;
		ops = &hvterm_hvsi_ops;
	} else {
M
Masanari Iida 已提交
315
		pr_err("hvc_vio: Unknown protocol for %s\n", vdev->dev.of_node->full_name);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
		return -ENXIO;
	}

	pr_devel("hvc_vio_probe() device %s, using %s protocol\n",
		 vdev->dev.of_node->full_name,
		 proto == HV_PROTOCOL_RAW ? "raw" : "hvsi");

	/* Is it our boot one ? */
	if (hvterm_privs[0] == &hvterm_priv0 &&
	    vdev->unit_address == hvterm_priv0.termno) {
		pv = hvterm_privs[0];
		termno = 0;
		pr_devel("->boot console, using termno 0\n");
	}
	/* nope, allocate a new one */
	else {
		for (i = 0; i < MAX_NR_HVC_CONSOLES && termno < 0; i++)
			if (!hvterm_privs[i])
				termno = i;
		pr_devel("->non-boot console, using termno %d\n", termno);
		if (termno < 0)
			return -ENODEV;
		pv = kzalloc(sizeof(struct hvterm_priv), GFP_KERNEL);
		if (!pv)
			return -ENOMEM;
		pv->termno = vdev->unit_address;
		pv->proto = proto;
343
		spin_lock_init(&pv->buf_lock);
344
		hvterm_privs[termno] = pv;
345 346
		hvsilib_init(&pv->hvsi, hvc_get_chars, hvc_put_chars,
			     pv->termno, 0);
347 348 349
	}

	hp = hvc_alloc(termno, vdev->irq, ops, MAX_VIO_PUT_CHARS);
350 351 352 353
	if (IS_ERR(hp))
		return PTR_ERR(hp);
	dev_set_drvdata(&vdev->dev, hp);

354 355 356 357 358 359 360
	/* register udbg if it's not there already for console 0 */
	if (hp->index == 0 && !udbg_putc) {
		udbg_putc = udbg_hvc_putc;
		udbg_getc = udbg_hvc_getc;
		udbg_getc_poll = udbg_hvc_getc_poll;
	}

361 362 363 364 365 366
	return 0;
}

static struct vio_driver hvc_vio_driver = {
	.id_table	= hvc_driver_table,
	.probe		= hvc_vio_probe,
367
	.name		= hvc_driver_name,
368 369 370
	.driver = {
		.suppress_bind_attrs	= true,
	},
371 372
};

373
static int __init hvc_vio_init(void)
374 375 376 377 378 379 380 381
{
	int rc;

	/* Register as a vio device to receive callbacks */
	rc = vio_register_driver(&hvc_vio_driver);

	return rc;
}
382
device_initcall(hvc_vio_init); /* after drivers/tty/hvc/hvc_console.c */
383

384 385
void __init hvc_vio_init_early(void)
{
386
	const __be32 *termno;
387 388 389 390
	const char *name;
	const struct hv_ops *ops;

	/* find the boot console from /chosen/stdout */
391
	if (!of_stdout)
392
		return;
393
	name = of_get_property(of_stdout, "name", NULL);
394 395
	if (!name) {
		printk(KERN_WARNING "stdout node missing 'name' property!\n");
396
		return;
397 398 399 400
	}

	/* Check if it's a virtual terminal */
	if (strncmp(name, "vty", 3) != 0)
401 402
		return;
	termno = of_get_property(of_stdout, "reg", NULL);
403
	if (termno == NULL)
404
		return;
405
	hvterm_priv0.termno = of_read_number(termno, 1);
406
	spin_lock_init(&hvterm_priv0.buf_lock);
407 408 409
	hvterm_privs[0] = &hvterm_priv0;

	/* Check the protocol */
410
	if (of_device_is_compatible(of_stdout, "hvterm1")) {
411 412 413
		hvterm_priv0.proto = HV_PROTOCOL_RAW;
		ops = &hvterm_raw_ops;
	}
414
	else if (of_device_is_compatible(of_stdout, "hvterm-protocol")) {
415 416
		hvterm_priv0.proto = HV_PROTOCOL_HVSI;
		ops = &hvterm_hvsi_ops;
417 418
		hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
			     hvterm_priv0.termno, 1);
419
		/* HVSI, perform the handshake now */
420
		hvsilib_establish(&hvterm_priv0.hvsi);
421
	} else
422
		return;
423 424 425 426 427 428 429 430
	udbg_putc = udbg_hvc_putc;
	udbg_getc = udbg_hvc_getc;
	udbg_getc_poll = udbg_hvc_getc_poll;
#ifdef HVC_OLD_HVSI
	/* When using the old HVSI driver don't register the HVC
	 * backend for HVSI, only do udbg
	 */
	if (hvterm_priv0.proto == HV_PROTOCOL_HVSI)
431
		return;
432
#endif
433
	/* Check whether the user has requested a different console. */
434
	if (!strstr(boot_command_line, "console="))
435
		add_preferred_console("hvc", 0, NULL);
436 437 438 439 440 441 442 443 444
	hvc_instantiate(0, 0, ops);
}

/* call this from early_init() for a working debug console on
 * vterm capable LPAR machines
 */
#ifdef CONFIG_PPC_EARLY_DEBUG_LPAR
void __init udbg_init_debug_lpar(void)
{
445 446 447 448 449 450 451 452
	/*
	 * If we're running as a hypervisor then we definitely can't call the
	 * hypervisor to print debug output (we *are* the hypervisor), so don't
	 * register if we detect that MSR_HV=1.
	 */
	if (mfmsr() & MSR_HV)
		return;

453 454 455
	hvterm_privs[0] = &hvterm_priv0;
	hvterm_priv0.termno = 0;
	hvterm_priv0.proto = HV_PROTOCOL_RAW;
456
	spin_lock_init(&hvterm_priv0.buf_lock);
457 458 459 460 461
	udbg_putc = udbg_hvc_putc;
	udbg_getc = udbg_hvc_getc;
	udbg_getc_poll = udbg_hvc_getc_poll;
}
#endif /* CONFIG_PPC_EARLY_DEBUG_LPAR */
462

463 464 465
#ifdef CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI
void __init udbg_init_debug_lpar_hvsi(void)
{
466 467 468 469
	/* See comment above in udbg_init_debug_lpar() */
	if (mfmsr() & MSR_HV)
		return;

470 471 472
	hvterm_privs[0] = &hvterm_priv0;
	hvterm_priv0.termno = CONFIG_PPC_EARLY_DEBUG_HVSI_VTERMNO;
	hvterm_priv0.proto = HV_PROTOCOL_HVSI;
473
	spin_lock_init(&hvterm_priv0.buf_lock);
474 475 476
	udbg_putc = udbg_hvc_putc;
	udbg_getc = udbg_hvc_getc;
	udbg_getc_poll = udbg_hvc_getc_poll;
477 478 479
	hvsilib_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
		     hvterm_priv0.termno, 1);
	hvsilib_establish(&hvterm_priv0.hvsi);
480
}
481
#endif /* CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI */