Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  c 2001 PPC 64 Team, IBM Corp
0004  *
0005  * /dev/nvram driver for PPC64
0006  */
0007 
0008 
0009 #include <linux/types.h>
0010 #include <linux/errno.h>
0011 #include <linux/init.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/slab.h>
0014 #include <linux/ctype.h>
0015 #include <linux/uaccess.h>
0016 #include <linux/of.h>
0017 #include <asm/nvram.h>
0018 #include <asm/rtas.h>
0019 #include <asm/machdep.h>
0020 
0021 /* Max bytes to read/write in one go */
0022 #define NVRW_CNT 0x20
0023 
0024 static unsigned int nvram_size;
0025 static int nvram_fetch, nvram_store;
0026 static char nvram_buf[NVRW_CNT];    /* assume this is in the first 4GB */
0027 static DEFINE_SPINLOCK(nvram_lock);
0028 
0029 /* See clobbering_unread_rtas_event() */
0030 #define NVRAM_RTAS_READ_TIMEOUT 5       /* seconds */
0031 static time64_t last_unread_rtas_event;     /* timestamp */
0032 
0033 #ifdef CONFIG_PSTORE
0034 time64_t last_rtas_event;
0035 #endif
0036 
0037 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
0038 {
0039     unsigned int i;
0040     unsigned long len;
0041     int done;
0042     unsigned long flags;
0043     char *p = buf;
0044 
0045 
0046     if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
0047         return -ENODEV;
0048 
0049     if (*index >= nvram_size)
0050         return 0;
0051 
0052     i = *index;
0053     if (i + count > nvram_size)
0054         count = nvram_size - i;
0055 
0056     spin_lock_irqsave(&nvram_lock, flags);
0057 
0058     for (; count != 0; count -= len) {
0059         len = count;
0060         if (len > NVRW_CNT)
0061             len = NVRW_CNT;
0062         
0063         if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
0064                    len) != 0) || len != done) {
0065             spin_unlock_irqrestore(&nvram_lock, flags);
0066             return -EIO;
0067         }
0068         
0069         memcpy(p, nvram_buf, len);
0070 
0071         p += len;
0072         i += len;
0073     }
0074 
0075     spin_unlock_irqrestore(&nvram_lock, flags);
0076     
0077     *index = i;
0078     return p - buf;
0079 }
0080 
0081 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
0082 {
0083     unsigned int i;
0084     unsigned long len;
0085     int done;
0086     unsigned long flags;
0087     const char *p = buf;
0088 
0089     if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
0090         return -ENODEV;
0091 
0092     if (*index >= nvram_size)
0093         return 0;
0094 
0095     i = *index;
0096     if (i + count > nvram_size)
0097         count = nvram_size - i;
0098 
0099     spin_lock_irqsave(&nvram_lock, flags);
0100 
0101     for (; count != 0; count -= len) {
0102         len = count;
0103         if (len > NVRW_CNT)
0104             len = NVRW_CNT;
0105 
0106         memcpy(nvram_buf, p, len);
0107 
0108         if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
0109                    len) != 0) || len != done) {
0110             spin_unlock_irqrestore(&nvram_lock, flags);
0111             return -EIO;
0112         }
0113         
0114         p += len;
0115         i += len;
0116     }
0117     spin_unlock_irqrestore(&nvram_lock, flags);
0118     
0119     *index = i;
0120     return p - buf;
0121 }
0122 
0123 static ssize_t pSeries_nvram_get_size(void)
0124 {
0125     return nvram_size ? nvram_size : -ENODEV;
0126 }
0127 
0128 /* nvram_write_error_log
0129  *
0130  * We need to buffer the error logs into nvram to ensure that we have
0131  * the failure information to decode.
0132  */
0133 int nvram_write_error_log(char * buff, int length,
0134                           unsigned int err_type, unsigned int error_log_cnt)
0135 {
0136     int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
0137                         err_type, error_log_cnt);
0138     if (!rc) {
0139         last_unread_rtas_event = ktime_get_real_seconds();
0140 #ifdef CONFIG_PSTORE
0141         last_rtas_event = ktime_get_real_seconds();
0142 #endif
0143     }
0144 
0145     return rc;
0146 }
0147 
0148 /* nvram_read_error_log
0149  *
0150  * Reads nvram for error log for at most 'length'
0151  */
0152 int nvram_read_error_log(char *buff, int length,
0153             unsigned int *err_type, unsigned int *error_log_cnt)
0154 {
0155     return nvram_read_partition(&rtas_log_partition, buff, length,
0156                         err_type, error_log_cnt);
0157 }
0158 
0159 /* This doesn't actually zero anything, but it sets the event_logged
0160  * word to tell that this event is safely in syslog.
0161  */
0162 int nvram_clear_error_log(void)
0163 {
0164     loff_t tmp_index;
0165     int clear_word = ERR_FLAG_ALREADY_LOGGED;
0166     int rc;
0167 
0168     if (rtas_log_partition.index == -1)
0169         return -1;
0170 
0171     tmp_index = rtas_log_partition.index;
0172     
0173     rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
0174     if (rc <= 0) {
0175         printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
0176         return rc;
0177     }
0178     last_unread_rtas_event = 0;
0179 
0180     return 0;
0181 }
0182 
0183 /*
0184  * Are we using the ibm,rtas-log for oops/panic reports?  And if so,
0185  * would logging this oops/panic overwrite an RTAS event that rtas_errd
0186  * hasn't had a chance to read and process?  Return 1 if so, else 0.
0187  *
0188  * We assume that if rtas_errd hasn't read the RTAS event in
0189  * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
0190  */
0191 int clobbering_unread_rtas_event(void)
0192 {
0193     return (oops_log_partition.index == rtas_log_partition.index
0194         && last_unread_rtas_event
0195         && ktime_get_real_seconds() - last_unread_rtas_event <=
0196                         NVRAM_RTAS_READ_TIMEOUT);
0197 }
0198 
0199 static int __init pseries_nvram_init_log_partitions(void)
0200 {
0201     int rc;
0202 
0203     /* Scan nvram for partitions */
0204     nvram_scan_partitions();
0205 
0206     rc = nvram_init_os_partition(&rtas_log_partition);
0207     nvram_init_oops_partition(rc == 0);
0208     return 0;
0209 }
0210 machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
0211 
0212 int __init pSeries_nvram_init(void)
0213 {
0214     struct device_node *nvram;
0215     const __be32 *nbytes_p;
0216     unsigned int proplen;
0217 
0218     nvram = of_find_node_by_type(NULL, "nvram");
0219     if (nvram == NULL)
0220         return -ENODEV;
0221 
0222     nbytes_p = of_get_property(nvram, "#bytes", &proplen);
0223     if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
0224         of_node_put(nvram);
0225         return -EIO;
0226     }
0227 
0228     nvram_size = be32_to_cpup(nbytes_p);
0229 
0230     nvram_fetch = rtas_token("nvram-fetch");
0231     nvram_store = rtas_token("nvram-store");
0232     printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
0233     of_node_put(nvram);
0234 
0235     ppc_md.nvram_read   = pSeries_nvram_read;
0236     ppc_md.nvram_write  = pSeries_nvram_write;
0237     ppc_md.nvram_size   = pSeries_nvram_get_size;
0238 
0239     return 0;
0240 }
0241