Path: blob/master/arch/powerpc/platforms/pseries/nvram.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* c 2001 PPC 64 Team, IBM Corp3*4* /dev/nvram driver for PPC645*/678#include <linux/types.h>9#include <linux/errno.h>10#include <linux/init.h>11#include <linux/spinlock.h>12#include <linux/slab.h>13#include <linux/ctype.h>14#include <linux/uaccess.h>15#include <linux/of.h>16#include <asm/nvram.h>17#include <asm/rtas.h>18#include <asm/machdep.h>1920/* Max bytes to read/write in one go */21#define NVRW_CNT 0x202223static unsigned int nvram_size;24static int nvram_fetch, nvram_store;25static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */26static DEFINE_SPINLOCK(nvram_lock);2728/* See clobbering_unread_rtas_event() */29#define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */30static time64_t last_unread_rtas_event; /* timestamp */3132#ifdef CONFIG_PSTORE33time64_t last_rtas_event;34#endif3536static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)37{38unsigned int i;39unsigned long len;40int done;41unsigned long flags;42char *p = buf;434445if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)46return -ENODEV;4748if (*index >= nvram_size)49return 0;5051i = *index;52if (i + count > nvram_size)53count = nvram_size - i;5455spin_lock_irqsave(&nvram_lock, flags);5657for (; count != 0; count -= len) {58len = count;59if (len > NVRW_CNT)60len = NVRW_CNT;6162if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),63len) != 0) || len != done) {64spin_unlock_irqrestore(&nvram_lock, flags);65return -EIO;66}6768memcpy(p, nvram_buf, len);6970p += len;71i += len;72}7374spin_unlock_irqrestore(&nvram_lock, flags);7576*index = i;77return p - buf;78}7980static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)81{82unsigned int i;83unsigned long len;84int done;85unsigned long flags;86const char *p = buf;8788if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)89return -ENODEV;9091if (*index >= nvram_size)92return 0;9394i = *index;95if (i + count > nvram_size)96count = nvram_size - i;9798spin_lock_irqsave(&nvram_lock, flags);99100for (; count != 0; count -= len) {101len = count;102if (len > NVRW_CNT)103len = NVRW_CNT;104105memcpy(nvram_buf, p, len);106107if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),108len) != 0) || len != done) {109spin_unlock_irqrestore(&nvram_lock, flags);110return -EIO;111}112113p += len;114i += len;115}116spin_unlock_irqrestore(&nvram_lock, flags);117118*index = i;119return p - buf;120}121122static ssize_t pSeries_nvram_get_size(void)123{124return nvram_size ? nvram_size : -ENODEV;125}126127/* nvram_write_error_log128*129* We need to buffer the error logs into nvram to ensure that we have130* the failure information to decode.131*/132int nvram_write_error_log(char * buff, int length,133unsigned int err_type, unsigned int error_log_cnt)134{135int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,136err_type, error_log_cnt);137if (!rc) {138last_unread_rtas_event = ktime_get_real_seconds();139#ifdef CONFIG_PSTORE140last_rtas_event = ktime_get_real_seconds();141#endif142}143144return rc;145}146147/* nvram_read_error_log148*149* Reads nvram for error log for at most 'length'150*/151int nvram_read_error_log(char *buff, int length,152unsigned int *err_type, unsigned int *error_log_cnt)153{154return nvram_read_partition(&rtas_log_partition, buff, length,155err_type, error_log_cnt);156}157158/* This doesn't actually zero anything, but it sets the event_logged159* word to tell that this event is safely in syslog.160*/161int nvram_clear_error_log(void)162{163loff_t tmp_index;164int clear_word = ERR_FLAG_ALREADY_LOGGED;165int rc;166167if (rtas_log_partition.index == -1)168return -1;169170tmp_index = rtas_log_partition.index;171172rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);173if (rc <= 0) {174printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);175return rc;176}177last_unread_rtas_event = 0;178179return 0;180}181182/*183* Are we using the ibm,rtas-log for oops/panic reports? And if so,184* would logging this oops/panic overwrite an RTAS event that rtas_errd185* hasn't had a chance to read and process? Return 1 if so, else 0.186*187* We assume that if rtas_errd hasn't read the RTAS event in188* NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.189*/190int clobbering_unread_rtas_event(void)191{192return (oops_log_partition.index == rtas_log_partition.index193&& last_unread_rtas_event194&& ktime_get_real_seconds() - last_unread_rtas_event <=195NVRAM_RTAS_READ_TIMEOUT);196}197198static int __init pseries_nvram_init_log_partitions(void)199{200int rc;201202/* Scan nvram for partitions */203nvram_scan_partitions();204205rc = nvram_init_os_partition(&rtas_log_partition);206nvram_init_oops_partition(rc == 0);207return 0;208}209machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);210211int __init pSeries_nvram_init(void)212{213struct device_node *nvram;214const __be32 *nbytes_p;215unsigned int proplen;216217nvram = of_find_node_by_type(NULL, "nvram");218if (nvram == NULL)219return -ENODEV;220221nbytes_p = of_get_property(nvram, "#bytes", &proplen);222if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {223of_node_put(nvram);224return -EIO;225}226227nvram_size = be32_to_cpup(nbytes_p);228229nvram_fetch = rtas_function_token(RTAS_FN_NVRAM_FETCH);230nvram_store = rtas_function_token(RTAS_FN_NVRAM_STORE);231printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);232of_node_put(nvram);233234ppc_md.nvram_read = pSeries_nvram_read;235ppc_md.nvram_write = pSeries_nvram_write;236ppc_md.nvram_size = pSeries_nvram_get_size;237238return 0;239}240241242243