162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * A udbg backend which logs messages and reads input from in memory
462306a36Sopenharmony_ci * buffers.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * The console output can be read from memcons_output which is a
762306a36Sopenharmony_ci * circular buffer whose next write position is stored in memcons.output_pos.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Input may be passed by writing into the memcons_input buffer when it is
1062306a36Sopenharmony_ci * empty. The input buffer is empty when both input_pos == input_start and
1162306a36Sopenharmony_ci * *input_start == '\0'.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Copyright (C) 2003-2005 Anton Blanchard and Milton Miller, IBM Corp
1462306a36Sopenharmony_ci * Copyright (C) 2013 Alistair Popple, IBM Corp
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <linux/kernel.h>
1862306a36Sopenharmony_ci#include <asm/barrier.h>
1962306a36Sopenharmony_ci#include <asm/page.h>
2062306a36Sopenharmony_ci#include <asm/processor.h>
2162306a36Sopenharmony_ci#include <asm/udbg.h>
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cistruct memcons {
2462306a36Sopenharmony_ci	char *output_start;
2562306a36Sopenharmony_ci	char *output_pos;
2662306a36Sopenharmony_ci	char *output_end;
2762306a36Sopenharmony_ci	char *input_start;
2862306a36Sopenharmony_ci	char *input_pos;
2962306a36Sopenharmony_ci	char *input_end;
3062306a36Sopenharmony_ci};
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistatic char memcons_output[CONFIG_PPC_MEMCONS_OUTPUT_SIZE];
3362306a36Sopenharmony_cistatic char memcons_input[CONFIG_PPC_MEMCONS_INPUT_SIZE];
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_cistruct memcons memcons = {
3662306a36Sopenharmony_ci	.output_start = memcons_output,
3762306a36Sopenharmony_ci	.output_pos = memcons_output,
3862306a36Sopenharmony_ci	.output_end = &memcons_output[CONFIG_PPC_MEMCONS_OUTPUT_SIZE],
3962306a36Sopenharmony_ci	.input_start = memcons_input,
4062306a36Sopenharmony_ci	.input_pos = memcons_input,
4162306a36Sopenharmony_ci	.input_end = &memcons_input[CONFIG_PPC_MEMCONS_INPUT_SIZE],
4262306a36Sopenharmony_ci};
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_civoid memcons_putc(char c)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	char *new_output_pos;
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci	*memcons.output_pos = c;
4962306a36Sopenharmony_ci	wmb();
5062306a36Sopenharmony_ci	new_output_pos = memcons.output_pos + 1;
5162306a36Sopenharmony_ci	if (new_output_pos >= memcons.output_end)
5262306a36Sopenharmony_ci		new_output_pos = memcons.output_start;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	memcons.output_pos = new_output_pos;
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciint memcons_getc_poll(void)
5862306a36Sopenharmony_ci{
5962306a36Sopenharmony_ci	char c;
6062306a36Sopenharmony_ci	char *new_input_pos;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	if (*memcons.input_pos) {
6362306a36Sopenharmony_ci		c = *memcons.input_pos;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci		new_input_pos = memcons.input_pos + 1;
6662306a36Sopenharmony_ci		if (new_input_pos >= memcons.input_end)
6762306a36Sopenharmony_ci			new_input_pos = memcons.input_start;
6862306a36Sopenharmony_ci		else if (*new_input_pos == '\0')
6962306a36Sopenharmony_ci			new_input_pos = memcons.input_start;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci		*memcons.input_pos = '\0';
7262306a36Sopenharmony_ci		wmb();
7362306a36Sopenharmony_ci		memcons.input_pos = new_input_pos;
7462306a36Sopenharmony_ci		return c;
7562306a36Sopenharmony_ci	}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	return -1;
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ciint memcons_getc(void)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	int c;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	while (1) {
8562306a36Sopenharmony_ci		c = memcons_getc_poll();
8662306a36Sopenharmony_ci		if (c == -1)
8762306a36Sopenharmony_ci			cpu_relax();
8862306a36Sopenharmony_ci		else
8962306a36Sopenharmony_ci			break;
9062306a36Sopenharmony_ci	}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	return c;
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_civoid __init udbg_init_memcons(void)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	udbg_putc = memcons_putc;
9862306a36Sopenharmony_ci	udbg_getc = memcons_getc;
9962306a36Sopenharmony_ci	udbg_getc_poll = memcons_getc_poll;
10062306a36Sopenharmony_ci}
101