Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f56b018e authored by Elliot Berman's avatar Elliot Berman
Browse files

tty: hvc_haven: Use thread worker to send characters



hh_hvc_put_chars may be called with interrupts disabled.
hh_rm_console_write is a blocking call (to wait for the response to come
back). Use a FIFO and workqueue to send characters so that console_write
may sleep.

Change-Id: I7060b3604cb112caf00ecc1d94a5b2d543e7956e
Signed-off-by: default avatarElliot Berman <eberman@codeaurora.org>
parent aff0badb
Loading
Loading
Loading
Loading
+61 −14
Original line number Diff line number Diff line
@@ -12,21 +12,27 @@
#include <linux/notifier.h>
#include <linux/spinlock.h>
#include <linux/printk.h>
#include <linux/workqueue.h>

#include <linux/haven/hh_msgq.h>
#include <linux/haven/hh_common.h>
#include <linux/haven/hh_rm_drv.h>

#include "hvc_console.h"

#define HVC_HH_VTERM_COOKIE	0x474E5948
/* # of payload bytes that can fit in a 1-fragment CONSOLE_WRITE message */
#define HH_HVC_WRITE_MSG_SIZE	((1 * (HH_MSGQ_MAX_MSG_SIZE_BYTES - 8)) - 4)

struct hh_hvc_prv {
	struct hvc_struct *hvc;
	DECLARE_KFIFO(fifo, char, 1024);
	DECLARE_KFIFO(get_fifo, char, 1024);
	DECLARE_KFIFO(put_fifo, char, 1024);
	struct work_struct put_work;
};

static DEFINE_SPINLOCK(fifo_lock);
static struct hh_hvc_prv hh_hvc_data[2];
static struct hh_hvc_prv hh_hvc_data[HH_VM_MAX];

static inline int hh_vm_name_to_vtermno(enum hh_vm_names vmname)
{
@@ -50,11 +56,12 @@ static int hh_hvc_notify_console_chars(struct notifier_block *this,

	ret = hh_rm_get_vm_name(msg->vmid, &vm_name);
	if (ret) {
		pr_warn_ratelimited("don't know VMID %d\n", vm_name);
		pr_warn_ratelimited("don't know VMID %d ret: %d\n", msg->vmid,
				    ret);
		return NOTIFY_OK;
	}

	ret = kfifo_in_spinlocked(&hh_hvc_data[vm_name].fifo,
	ret = kfifo_in_spinlocked(&hh_hvc_data[vm_name].get_fifo,
				  msg->bytes, msg->num_bytes,
				  &fifo_lock);

@@ -69,6 +76,36 @@ static int hh_hvc_notify_console_chars(struct notifier_block *this,
	return NOTIFY_OK;
}

static void hh_hvc_put_work_fn(struct work_struct *ws)
{
	hh_vmid_t vmid;
	char buf[HH_HVC_WRITE_MSG_SIZE];
	int count, ret;
	struct hh_hvc_prv *prv = container_of(ws, struct hh_hvc_prv, put_work);
	enum hh_vm_names vm_name = vtermno_to_hh_vm_name(prv->hvc->vtermno);

	ret = hh_rm_get_vmid(vm_name, &vmid);
	if (ret) {
		pr_warn_once("hh_rm_get_vmid failed for %d: %d\n",
			     vm_name, ret);
		return;
	}

	while (!kfifo_is_empty(&prv->put_fifo)) {
		count = kfifo_out_spinlocked(&prv->put_fifo, buf, sizeof(buf),
					     &fifo_lock);
		if (count <= 0)
			continue;

		ret = hh_rm_console_write(vmid, buf, count);
		if (ret) {
			pr_warn_once("hh_rm_console_write failed for %d: %d\n",
				vm_name, ret);
			break;
		}
	}
}

static int hh_hvc_get_chars(uint32_t vtermno, char *buf, int count)
{
	int vm_name = vtermno_to_hh_vm_name(vtermno);
@@ -76,24 +113,22 @@ static int hh_hvc_get_chars(uint32_t vtermno, char *buf, int count)
	if (vm_name < 0 || vm_name >= HH_VM_MAX)
		return -EINVAL;

	return kfifo_out_spinlocked(&hh_hvc_data[vm_name].fifo,
	return kfifo_out_spinlocked(&hh_hvc_data[vm_name].get_fifo,
				    buf, count, &fifo_lock);
}

static int hh_hvc_put_chars(uint32_t vtermno, const char *buf, int count)
{
	int ret, vm_name = vtermno_to_hh_vm_name(vtermno);
	hh_vmid_t vmid;

	if (vm_name < 0 || vm_name >= HH_VM_MAX)
		return -EINVAL;

	ret = hh_rm_get_vmid(vm_name, &vmid);
	if (ret)
	ret = kfifo_in_spinlocked(&hh_hvc_data[vm_name].put_fifo,
				   buf, count, &fifo_lock);
	if (ret > 0)
		schedule_work(&hh_hvc_data[vm_name].put_work);
	return ret;


	return hh_rm_console_write(vmid, buf, count);
}

static int hh_hvc_flush(uint32_t vtermno, bool wait)
@@ -108,6 +143,11 @@ static int hh_hvc_flush(uint32_t vtermno, bool wait)
	if (ret)
		return ret;

	if (cancel_work_sync(&hh_hvc_data[vm_name].put_work)) {
		/* flush the fifo */
		hh_hvc_put_work_fn(&hh_hvc_data[vm_name].put_work);
	}

	return hh_rm_console_flush(vmid);
}

@@ -131,6 +171,11 @@ static void hh_hvc_notify_del(struct hvc_struct *hp, int vm_name)
	if (vm_name < 0 || vm_name >= HH_VM_MAX)
		return;

	if (cancel_work_sync(&hh_hvc_data[vm_name].put_work)) {
		/* flush the fifo */
		hh_hvc_put_work_fn(&hh_hvc_data[vm_name].put_work);
	}

	ret = hh_rm_get_vmid(vm_name, &vmid);
	if (ret)
		return;
@@ -140,7 +185,7 @@ static void hh_hvc_notify_del(struct hvc_struct *hp, int vm_name)
	if (ret)
		pr_err("Failed close VM%d console - %d\n", vm_name, ret);

	kfifo_reset(&hh_hvc_data[vm_name].fifo);
	kfifo_reset(&hh_hvc_data[vm_name].get_fifo);
}

static struct notifier_block hh_hvc_nb = {
@@ -175,7 +220,9 @@ static int __init hvc_hh_init(void)

	for (i = 0; i < HH_VM_MAX; i++) {
		prv = &hh_hvc_data[i];
		INIT_KFIFO(prv->fifo);
		INIT_KFIFO(prv->get_fifo);
		INIT_KFIFO(prv->put_fifo);
		INIT_WORK(&prv->put_work, hh_hvc_put_work_fn);
		prv->hvc = hvc_alloc(hh_vm_name_to_vtermno(i), i, &hh_hv_ops,
				     256);
		ret = PTR_ERR_OR_ZERO(prv->hvc);