Path: blob/master/arch/powerpc/platforms/powernv/opal-msglog.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* PowerNV OPAL in-memory console interface3*4* Copyright 2014 IBM Corp.5*/67#include <asm/io.h>8#include <asm/opal.h>9#include <linux/debugfs.h>10#include <linux/of.h>11#include <linux/types.h>12#include <asm/barrier.h>1314#include "powernv.h"1516/* OPAL in-memory console. Defined in OPAL source at core/console.c */17struct memcons {18__be64 magic;19#define MEMCONS_MAGIC 0x6630696567726173L20__be64 obuf_phys;21__be64 ibuf_phys;22__be32 obuf_size;23__be32 ibuf_size;24__be32 out_pos;25#define MEMCONS_OUT_POS_WRAP 0x80000000u26#define MEMCONS_OUT_POS_MASK 0x00ffffffu27__be32 in_prod;28__be32 in_cons;29};3031static struct memcons *opal_memcons = NULL;3233ssize_t memcons_copy(struct memcons *mc, char *to, loff_t pos, size_t count)34{35const char *conbuf;36ssize_t ret;37size_t first_read = 0;38uint32_t out_pos, avail;3940if (!mc)41return -ENODEV;4243out_pos = be32_to_cpu(READ_ONCE(mc->out_pos));4445/* Now we've read out_pos, put a barrier in before reading the new46* data it points to in conbuf. */47smp_rmb();4849conbuf = phys_to_virt(be64_to_cpu(mc->obuf_phys));5051/* When the buffer has wrapped, read from the out_pos marker to the end52* of the buffer, and then read the remaining data as in the un-wrapped53* case. */54if (out_pos & MEMCONS_OUT_POS_WRAP) {5556out_pos &= MEMCONS_OUT_POS_MASK;57avail = be32_to_cpu(mc->obuf_size) - out_pos;5859ret = memory_read_from_buffer(to, count, &pos,60conbuf + out_pos, avail);6162if (ret < 0)63goto out;6465first_read = ret;66to += first_read;67count -= first_read;68pos -= avail;6970if (count <= 0)71goto out;72}7374/* Sanity check. The firmware should not do this to us. */75if (out_pos > be32_to_cpu(mc->obuf_size)) {76pr_err("OPAL: memory console corruption. Aborting read.\n");77return -EINVAL;78}7980ret = memory_read_from_buffer(to, count, &pos, conbuf, out_pos);8182if (ret < 0)83goto out;8485ret += first_read;86out:87return ret;88}8990ssize_t opal_msglog_copy(char *to, loff_t pos, size_t count)91{92return memcons_copy(opal_memcons, to, pos, count);93}9495static ssize_t opal_msglog_read(struct file *file, struct kobject *kobj,96const struct bin_attribute *bin_attr, char *to,97loff_t pos, size_t count)98{99return opal_msglog_copy(to, pos, count);100}101102static struct bin_attribute opal_msglog_attr __ro_after_init = {103.attr = {.name = "msglog", .mode = 0400},104.read = opal_msglog_read105};106107struct memcons *__init memcons_init(struct device_node *node, const char *mc_prop_name)108{109u64 mcaddr;110struct memcons *mc;111112if (of_property_read_u64(node, mc_prop_name, &mcaddr)) {113pr_warn("%s property not found, no message log\n",114mc_prop_name);115goto out_err;116}117118mc = phys_to_virt(mcaddr);119if (!mc) {120pr_warn("memory console address is invalid\n");121goto out_err;122}123124if (be64_to_cpu(mc->magic) != MEMCONS_MAGIC) {125pr_warn("memory console version is invalid\n");126goto out_err;127}128129return mc;130131out_err:132return NULL;133}134135u32 __init memcons_get_size(struct memcons *mc)136{137return be32_to_cpu(mc->ibuf_size) + be32_to_cpu(mc->obuf_size);138}139140void __init opal_msglog_init(void)141{142opal_memcons = memcons_init(opal_node, "ibm,opal-memcons");143if (!opal_memcons) {144pr_warn("OPAL: memcons failed to load from ibm,opal-memcons\n");145return;146}147148opal_msglog_attr.size = memcons_get_size(opal_memcons);149}150151void __init opal_msglog_sysfs_init(void)152{153if (!opal_memcons) {154pr_warn("OPAL: message log initialisation failed, not creating sysfs entry\n");155return;156}157158if (sysfs_create_bin_file(opal_kobj, &opal_msglog_attr) != 0)159pr_warn("OPAL: sysfs file creation failed\n");160}161162163