nvram.c 5.6 KB

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