nvram.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * c 2001 PPC 64 Team, IBM Corp
  4. *
  5. * /dev/nvram driver for PPC64
  6. */
  7. #include <linux/types.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/slab.h>
  12. #include <linux/ctype.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/of.h>
  15. #include <asm/nvram.h>
  16. #include <asm/rtas.h>
  17. #include <asm/machdep.h>
  18. /* Max bytes to read/write in one go */
  19. #define NVRW_CNT 0x20
  20. static unsigned int nvram_size;
  21. static int nvram_fetch, nvram_store;
  22. static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
  23. static DEFINE_SPINLOCK(nvram_lock);
  24. /* See clobbering_unread_rtas_event() */
  25. #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */
  26. static time64_t last_unread_rtas_event; /* timestamp */
  27. #ifdef CONFIG_PSTORE
  28. time64_t last_rtas_event;
  29. #endif
  30. static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
  31. {
  32. unsigned int i;
  33. unsigned long len;
  34. int done;
  35. unsigned long flags;
  36. char *p = buf;
  37. if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
  38. return -ENODEV;
  39. if (*index >= nvram_size)
  40. return 0;
  41. i = *index;
  42. if (i + count > nvram_size)
  43. count = nvram_size - i;
  44. spin_lock_irqsave(&nvram_lock, flags);
  45. for (; count != 0; count -= len) {
  46. len = count;
  47. if (len > NVRW_CNT)
  48. len = NVRW_CNT;
  49. if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
  50. len) != 0) || len != done) {
  51. spin_unlock_irqrestore(&nvram_lock, flags);
  52. return -EIO;
  53. }
  54. memcpy(p, nvram_buf, len);
  55. p += len;
  56. i += len;
  57. }
  58. spin_unlock_irqrestore(&nvram_lock, flags);
  59. *index = i;
  60. return p - buf;
  61. }
  62. static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
  63. {
  64. unsigned int i;
  65. unsigned long len;
  66. int done;
  67. unsigned long flags;
  68. const char *p = buf;
  69. if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
  70. return -ENODEV;
  71. if (*index >= nvram_size)
  72. return 0;
  73. i = *index;
  74. if (i + count > nvram_size)
  75. count = nvram_size - i;
  76. spin_lock_irqsave(&nvram_lock, flags);
  77. for (; count != 0; count -= len) {
  78. len = count;
  79. if (len > NVRW_CNT)
  80. len = NVRW_CNT;
  81. memcpy(nvram_buf, p, len);
  82. if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
  83. len) != 0) || len != done) {
  84. spin_unlock_irqrestore(&nvram_lock, flags);
  85. return -EIO;
  86. }
  87. p += len;
  88. i += len;
  89. }
  90. spin_unlock_irqrestore(&nvram_lock, flags);
  91. *index = i;
  92. return p - buf;
  93. }
  94. static ssize_t pSeries_nvram_get_size(void)
  95. {
  96. return nvram_size ? nvram_size : -ENODEV;
  97. }
  98. /* nvram_write_error_log
  99. *
  100. * We need to buffer the error logs into nvram to ensure that we have
  101. * the failure information to decode.
  102. */
  103. int nvram_write_error_log(char * buff, int length,
  104. unsigned int err_type, unsigned int error_log_cnt)
  105. {
  106. int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
  107. err_type, error_log_cnt);
  108. if (!rc) {
  109. last_unread_rtas_event = ktime_get_real_seconds();
  110. #ifdef CONFIG_PSTORE
  111. last_rtas_event = ktime_get_real_seconds();
  112. #endif
  113. }
  114. return rc;
  115. }
  116. /* nvram_read_error_log
  117. *
  118. * Reads nvram for error log for at most 'length'
  119. */
  120. int nvram_read_error_log(char *buff, int length,
  121. unsigned int *err_type, unsigned int *error_log_cnt)
  122. {
  123. return nvram_read_partition(&rtas_log_partition, buff, length,
  124. err_type, error_log_cnt);
  125. }
  126. /* This doesn't actually zero anything, but it sets the event_logged
  127. * word to tell that this event is safely in syslog.
  128. */
  129. int nvram_clear_error_log(void)
  130. {
  131. loff_t tmp_index;
  132. int clear_word = ERR_FLAG_ALREADY_LOGGED;
  133. int rc;
  134. if (rtas_log_partition.index == -1)
  135. return -1;
  136. tmp_index = rtas_log_partition.index;
  137. rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
  138. if (rc <= 0) {
  139. printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
  140. return rc;
  141. }
  142. last_unread_rtas_event = 0;
  143. return 0;
  144. }
  145. /*
  146. * Are we using the ibm,rtas-log for oops/panic reports? And if so,
  147. * would logging this oops/panic overwrite an RTAS event that rtas_errd
  148. * hasn't had a chance to read and process? Return 1 if so, else 0.
  149. *
  150. * We assume that if rtas_errd hasn't read the RTAS event in
  151. * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
  152. */
  153. int clobbering_unread_rtas_event(void)
  154. {
  155. return (oops_log_partition.index == rtas_log_partition.index
  156. && last_unread_rtas_event
  157. && ktime_get_real_seconds() - last_unread_rtas_event <=
  158. NVRAM_RTAS_READ_TIMEOUT);
  159. }
  160. static int __init pseries_nvram_init_log_partitions(void)
  161. {
  162. int rc;
  163. /* Scan nvram for partitions */
  164. nvram_scan_partitions();
  165. rc = nvram_init_os_partition(&rtas_log_partition);
  166. nvram_init_oops_partition(rc == 0);
  167. return 0;
  168. }
  169. machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
  170. int __init pSeries_nvram_init(void)
  171. {
  172. struct device_node *nvram;
  173. const __be32 *nbytes_p;
  174. unsigned int proplen;
  175. nvram = of_find_node_by_type(NULL, "nvram");
  176. if (nvram == NULL)
  177. return -ENODEV;
  178. nbytes_p = of_get_property(nvram, "#bytes", &proplen);
  179. if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
  180. of_node_put(nvram);
  181. return -EIO;
  182. }
  183. nvram_size = be32_to_cpup(nbytes_p);
  184. nvram_fetch = rtas_token("nvram-fetch");
  185. nvram_store = rtas_token("nvram-store");
  186. printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
  187. of_node_put(nvram);
  188. ppc_md.nvram_read = pSeries_nvram_read;
  189. ppc_md.nvram_write = pSeries_nvram_write;
  190. ppc_md.nvram_size = pSeries_nvram_get_size;
  191. return 0;
  192. }