nvram.c 5.7 KB

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