Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (c) 2016 IBM Corporation.
0004  */
0005 
0006 #include "ops.h"
0007 #include "stdio.h"
0008 #include "io.h"
0009 #include <libfdt.h>
0010 #include "../include/asm/opal-api.h"
0011 
0012 /* Global OPAL struct used by opal-call.S */
0013 struct opal {
0014     u64 base;
0015     u64 entry;
0016 } opal;
0017 
0018 static u32 opal_con_id;
0019 
0020 /* see opal-wrappers.S */
0021 int64_t opal_console_write(int64_t term_number, u64 *length, const u8 *buffer);
0022 int64_t opal_console_read(int64_t term_number, uint64_t *length, u8 *buffer);
0023 int64_t opal_console_write_buffer_space(uint64_t term_number, uint64_t *length);
0024 int64_t opal_console_flush(uint64_t term_number);
0025 int64_t opal_poll_events(uint64_t *outstanding_event_mask);
0026 
0027 void opal_kentry(unsigned long fdt_addr, void *vmlinux_addr);
0028 
0029 static int opal_con_open(void)
0030 {
0031     /*
0032      * When OPAL loads the boot kernel it stashes the OPAL base and entry
0033      * address in r8 and r9 so the kernel can use the OPAL console
0034      * before unflattening the devicetree. While executing the wrapper will
0035      * probably trash r8 and r9 so this kentry hook restores them before
0036      * entering the decompressed kernel.
0037      */
0038     platform_ops.kentry = opal_kentry;
0039     return 0;
0040 }
0041 
0042 static void opal_con_putc(unsigned char c)
0043 {
0044     int64_t rc;
0045     uint64_t olen, len;
0046 
0047     do {
0048         rc = opal_console_write_buffer_space(opal_con_id, &olen);
0049         len = be64_to_cpu(olen);
0050         if (rc)
0051             return;
0052         opal_poll_events(NULL);
0053     } while (len < 1);
0054 
0055 
0056     olen = cpu_to_be64(1);
0057     opal_console_write(opal_con_id, &olen, &c);
0058 }
0059 
0060 static void opal_con_close(void)
0061 {
0062     opal_console_flush(opal_con_id);
0063 }
0064 
0065 static void opal_init(void)
0066 {
0067     void *opal_node;
0068 
0069     opal_node = finddevice("/ibm,opal");
0070     if (!opal_node)
0071         return;
0072     if (getprop(opal_node, "opal-base-address", &opal.base, sizeof(u64)) < 0)
0073         return;
0074     opal.base = be64_to_cpu(opal.base);
0075     if (getprop(opal_node, "opal-entry-address", &opal.entry, sizeof(u64)) < 0)
0076         return;
0077     opal.entry = be64_to_cpu(opal.entry);
0078 }
0079 
0080 int opal_console_init(void *devp, struct serial_console_data *scdp)
0081 {
0082     opal_init();
0083 
0084     if (devp) {
0085         int n = getprop(devp, "reg", &opal_con_id, sizeof(u32));
0086         if (n != sizeof(u32))
0087             return -1;
0088         opal_con_id = be32_to_cpu(opal_con_id);
0089     } else
0090         opal_con_id = 0;
0091 
0092     scdp->open = opal_con_open;
0093     scdp->putc = opal_con_putc;
0094     scdp->close = opal_con_close;
0095 
0096     return 0;
0097 }