^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2016 IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "ops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "stdio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "../include/asm/opal-api.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /* Global OPAL struct used by opal-call.S */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct opal {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) u64 base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) u64 entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) } opal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static u32 opal_con_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* see opal-wrappers.S */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int64_t opal_console_write(int64_t term_number, u64 *length, const u8 *buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int64_t opal_console_read(int64_t term_number, uint64_t *length, u8 *buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int64_t opal_console_write_buffer_space(uint64_t term_number, uint64_t *length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int64_t opal_console_flush(uint64_t term_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int64_t opal_poll_events(uint64_t *outstanding_event_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void opal_kentry(unsigned long fdt_addr, void *vmlinux_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int opal_con_open(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * When OPAL loads the boot kernel it stashes the OPAL base and entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * address in r8 and r9 so the kernel can use the OPAL console
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * before unflattening the devicetree. While executing the wrapper will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * probably trash r8 and r9 so this kentry hook restores them before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * entering the decompressed kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) platform_ops.kentry = opal_kentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static void opal_con_putc(unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int64_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) uint64_t olen, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) rc = opal_console_write_buffer_space(opal_con_id, &olen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) len = be64_to_cpu(olen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) opal_poll_events(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) } while (len < 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) olen = cpu_to_be64(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) opal_console_write(opal_con_id, &olen, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static void opal_con_close(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) opal_console_flush(opal_con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static void opal_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) void *opal_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) opal_node = finddevice("/ibm,opal");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!opal_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (getprop(opal_node, "opal-base-address", &opal.base, sizeof(u64)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) opal.base = be64_to_cpu(opal.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (getprop(opal_node, "opal-entry-address", &opal.entry, sizeof(u64)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) opal.entry = be64_to_cpu(opal.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int opal_console_init(void *devp, struct serial_console_data *scdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) opal_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (devp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int n = getprop(devp, "reg", &opal_con_id, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (n != sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) opal_con_id = be32_to_cpu(opal_con_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) opal_con_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) scdp->open = opal_con_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) scdp->putc = opal_con_putc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) scdp->close = opal_con_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }