pdt.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Page Deallocation Table (PDT) support
  4. *
  5. * The Page Deallocation Table (PDT) is maintained by firmware and holds a
  6. * list of memory addresses in which memory errors were detected.
  7. * The list contains both single-bit (correctable) and double-bit
  8. * (uncorrectable) errors.
  9. *
  10. * Copyright 2017 by Helge Deller <deller@gmx.de>
  11. *
  12. * possible future enhancements:
  13. * - add userspace interface via procfs or sysfs to clear PDT
  14. */
  15. #include <linux/memblock.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/kthread.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/initrd.h>
  20. #include <linux/pgtable.h>
  21. #include <linux/mm.h>
  22. #include <asm/pdc.h>
  23. #include <asm/pdcpat.h>
  24. #include <asm/sections.h>
  25. #include <asm/pgtable.h>
  26. enum pdt_access_type {
  27. PDT_NONE,
  28. PDT_PDC,
  29. PDT_PAT_NEW,
  30. PDT_PAT_CELL
  31. };
  32. static enum pdt_access_type pdt_type;
  33. /* PDT poll interval: 1 minute if errors, 5 minutes if everything OK. */
  34. #define PDT_POLL_INTERVAL_DEFAULT (5*60*HZ)
  35. #define PDT_POLL_INTERVAL_SHORT (1*60*HZ)
  36. static unsigned long pdt_poll_interval = PDT_POLL_INTERVAL_DEFAULT;
  37. /* global PDT status information */
  38. static struct pdc_mem_retinfo pdt_status;
  39. #define MAX_PDT_TABLE_SIZE PAGE_SIZE
  40. #define MAX_PDT_ENTRIES (MAX_PDT_TABLE_SIZE / sizeof(unsigned long))
  41. static unsigned long pdt_entry[MAX_PDT_ENTRIES] __page_aligned_bss;
  42. /*
  43. * Constants for the pdt_entry format:
  44. * A pdt_entry holds the physical address in bits 0-57, bits 58-61 are
  45. * reserved, bit 62 is the perm bit and bit 63 is the error_type bit.
  46. * The perm bit indicates whether the error have been verified as a permanent
  47. * error (value of 1) or has not been verified, and may be transient (value
  48. * of 0). The error_type bit indicates whether the error is a single bit error
  49. * (value of 1) or a multiple bit error.
  50. * On non-PAT machines phys_addr is encoded in bits 0-59 and error_type in bit
  51. * 63. Those machines don't provide the perm bit.
  52. */
  53. #define PDT_ADDR_PHYS_MASK (pdt_type != PDT_PDC ? ~0x3f : ~0x0f)
  54. #define PDT_ADDR_PERM_ERR (pdt_type != PDT_PDC ? 2UL : 0UL)
  55. #define PDT_ADDR_SINGLE_ERR 1UL
  56. #ifdef CONFIG_PROC_FS
  57. /* report PDT entries via /proc/meminfo */
  58. void arch_report_meminfo(struct seq_file *m)
  59. {
  60. if (pdt_type == PDT_NONE)
  61. return;
  62. seq_printf(m, "PDT_max_entries: %7lu\n",
  63. pdt_status.pdt_size);
  64. seq_printf(m, "PDT_cur_entries: %7lu\n",
  65. pdt_status.pdt_entries);
  66. }
  67. #endif
  68. static int get_info_pat_new(void)
  69. {
  70. struct pdc_pat_mem_retinfo pat_rinfo;
  71. int ret;
  72. /* newer PAT machines like C8000 report info for all cells */
  73. if (is_pdc_pat())
  74. ret = pdc_pat_mem_pdt_info(&pat_rinfo);
  75. else
  76. return PDC_BAD_PROC;
  77. pdt_status.pdt_size = pat_rinfo.max_pdt_entries;
  78. pdt_status.pdt_entries = pat_rinfo.current_pdt_entries;
  79. pdt_status.pdt_status = 0;
  80. pdt_status.first_dbe_loc = pat_rinfo.first_dbe_loc;
  81. pdt_status.good_mem = pat_rinfo.good_mem;
  82. return ret;
  83. }
  84. static int get_info_pat_cell(void)
  85. {
  86. struct pdc_pat_mem_cell_pdt_retinfo cell_rinfo;
  87. int ret;
  88. /* older PAT machines like rp5470 report cell info only */
  89. if (is_pdc_pat())
  90. ret = pdc_pat_mem_pdt_cell_info(&cell_rinfo, parisc_cell_num);
  91. else
  92. return PDC_BAD_PROC;
  93. pdt_status.pdt_size = cell_rinfo.max_pdt_entries;
  94. pdt_status.pdt_entries = cell_rinfo.current_pdt_entries;
  95. pdt_status.pdt_status = 0;
  96. pdt_status.first_dbe_loc = cell_rinfo.first_dbe_loc;
  97. pdt_status.good_mem = cell_rinfo.good_mem;
  98. return ret;
  99. }
  100. static void report_mem_err(unsigned long pde)
  101. {
  102. struct pdc_pat_mem_phys_mem_location loc;
  103. unsigned long addr;
  104. char dimm_txt[32];
  105. addr = pde & PDT_ADDR_PHYS_MASK;
  106. /* show DIMM slot description on PAT machines */
  107. if (is_pdc_pat()) {
  108. pdc_pat_mem_get_dimm_phys_location(&loc, addr);
  109. sprintf(dimm_txt, "DIMM slot %02x, ", loc.dimm_slot);
  110. } else
  111. dimm_txt[0] = 0;
  112. pr_warn("PDT: BAD MEMORY at 0x%08lx, %s%s%s-bit error.\n",
  113. addr, dimm_txt,
  114. pde & PDT_ADDR_PERM_ERR ? "permanent ":"",
  115. pde & PDT_ADDR_SINGLE_ERR ? "single":"multi");
  116. }
  117. /*
  118. * pdc_pdt_init()
  119. *
  120. * Initialize kernel PDT structures, read initial PDT table from firmware,
  121. * report all current PDT entries and mark bad memory with memblock_reserve()
  122. * to avoid that the kernel will use broken memory areas.
  123. *
  124. */
  125. void __init pdc_pdt_init(void)
  126. {
  127. int ret, i;
  128. unsigned long entries;
  129. struct pdc_mem_read_pdt pdt_read_ret;
  130. pdt_type = PDT_PAT_NEW;
  131. ret = get_info_pat_new();
  132. if (ret != PDC_OK) {
  133. pdt_type = PDT_PAT_CELL;
  134. ret = get_info_pat_cell();
  135. }
  136. if (ret != PDC_OK) {
  137. pdt_type = PDT_PDC;
  138. /* non-PAT machines provide the standard PDC call */
  139. ret = pdc_mem_pdt_info(&pdt_status);
  140. }
  141. if (ret != PDC_OK) {
  142. pdt_type = PDT_NONE;
  143. pr_info("PDT: Firmware does not provide any page deallocation"
  144. " information.\n");
  145. return;
  146. }
  147. entries = pdt_status.pdt_entries;
  148. if (WARN_ON(entries > MAX_PDT_ENTRIES))
  149. entries = pdt_status.pdt_entries = MAX_PDT_ENTRIES;
  150. pr_info("PDT: type %s, size %lu, entries %lu, status %lu, dbe_loc 0x%lx,"
  151. " good_mem %lu MB\n",
  152. pdt_type == PDT_PDC ? __stringify(PDT_PDC) :
  153. pdt_type == PDT_PAT_CELL ? __stringify(PDT_PAT_CELL)
  154. : __stringify(PDT_PAT_NEW),
  155. pdt_status.pdt_size, pdt_status.pdt_entries,
  156. pdt_status.pdt_status, pdt_status.first_dbe_loc,
  157. pdt_status.good_mem / 1024 / 1024);
  158. if (entries == 0) {
  159. pr_info("PDT: Firmware reports all memory OK.\n");
  160. return;
  161. }
  162. if (pdt_status.first_dbe_loc &&
  163. pdt_status.first_dbe_loc <= __pa((unsigned long)&_end))
  164. pr_crit("CRITICAL: Bad memory inside kernel image memory area!\n");
  165. pr_warn("PDT: Firmware reports %lu entries of faulty memory:\n",
  166. entries);
  167. if (pdt_type == PDT_PDC)
  168. ret = pdc_mem_pdt_read_entries(&pdt_read_ret, pdt_entry);
  169. else {
  170. #ifdef CONFIG_64BIT
  171. struct pdc_pat_mem_read_pd_retinfo pat_pret;
  172. if (pdt_type == PDT_PAT_CELL)
  173. ret = pdc_pat_mem_read_cell_pdt(&pat_pret, pdt_entry,
  174. MAX_PDT_ENTRIES);
  175. else
  176. ret = pdc_pat_mem_read_pd_pdt(&pat_pret, pdt_entry,
  177. MAX_PDT_TABLE_SIZE, 0);
  178. #else
  179. ret = PDC_BAD_PROC;
  180. #endif
  181. }
  182. if (ret != PDC_OK) {
  183. pdt_type = PDT_NONE;
  184. pr_warn("PDT: Get PDT entries failed with %d\n", ret);
  185. return;
  186. }
  187. for (i = 0; i < pdt_status.pdt_entries; i++) {
  188. unsigned long addr;
  189. report_mem_err(pdt_entry[i]);
  190. addr = pdt_entry[i] & PDT_ADDR_PHYS_MASK;
  191. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) &&
  192. addr >= initrd_start && addr < initrd_end)
  193. pr_crit("CRITICAL: initrd possibly broken "
  194. "due to bad memory!\n");
  195. /* mark memory page bad */
  196. memblock_reserve(pdt_entry[i] & PAGE_MASK, PAGE_SIZE);
  197. num_poisoned_pages_inc(addr >> PAGE_SHIFT);
  198. }
  199. }
  200. /*
  201. * This is the PDT kernel thread main loop.
  202. */
  203. static int pdt_mainloop(void *unused)
  204. {
  205. struct pdc_mem_read_pdt pdt_read_ret;
  206. struct pdc_pat_mem_read_pd_retinfo pat_pret __maybe_unused;
  207. unsigned long old_num_entries;
  208. unsigned long *bad_mem_ptr;
  209. int num, ret;
  210. for (;;) {
  211. set_current_state(TASK_INTERRUPTIBLE);
  212. old_num_entries = pdt_status.pdt_entries;
  213. schedule_timeout(pdt_poll_interval);
  214. if (kthread_should_stop())
  215. break;
  216. /* Do we have new PDT entries? */
  217. switch (pdt_type) {
  218. case PDT_PAT_NEW:
  219. ret = get_info_pat_new();
  220. break;
  221. case PDT_PAT_CELL:
  222. ret = get_info_pat_cell();
  223. break;
  224. default:
  225. ret = pdc_mem_pdt_info(&pdt_status);
  226. break;
  227. }
  228. if (ret != PDC_OK) {
  229. pr_warn("PDT: unexpected failure %d\n", ret);
  230. return -EINVAL;
  231. }
  232. /* if no new PDT entries, just wait again */
  233. num = pdt_status.pdt_entries - old_num_entries;
  234. if (num <= 0)
  235. continue;
  236. /* decrease poll interval in case we found memory errors */
  237. if (pdt_status.pdt_entries &&
  238. pdt_poll_interval == PDT_POLL_INTERVAL_DEFAULT)
  239. pdt_poll_interval = PDT_POLL_INTERVAL_SHORT;
  240. /* limit entries to get */
  241. if (num > MAX_PDT_ENTRIES) {
  242. num = MAX_PDT_ENTRIES;
  243. pdt_status.pdt_entries = old_num_entries + num;
  244. }
  245. /* get new entries */
  246. switch (pdt_type) {
  247. #ifdef CONFIG_64BIT
  248. case PDT_PAT_CELL:
  249. if (pdt_status.pdt_entries > MAX_PDT_ENTRIES) {
  250. pr_crit("PDT: too many entries.\n");
  251. return -ENOMEM;
  252. }
  253. ret = pdc_pat_mem_read_cell_pdt(&pat_pret, pdt_entry,
  254. MAX_PDT_ENTRIES);
  255. bad_mem_ptr = &pdt_entry[old_num_entries];
  256. break;
  257. case PDT_PAT_NEW:
  258. ret = pdc_pat_mem_read_pd_pdt(&pat_pret,
  259. pdt_entry,
  260. num * sizeof(unsigned long),
  261. old_num_entries * sizeof(unsigned long));
  262. bad_mem_ptr = &pdt_entry[0];
  263. break;
  264. #endif
  265. default:
  266. ret = pdc_mem_pdt_read_entries(&pdt_read_ret,
  267. pdt_entry);
  268. bad_mem_ptr = &pdt_entry[old_num_entries];
  269. break;
  270. }
  271. /* report and mark memory broken */
  272. while (num--) {
  273. unsigned long pde = *bad_mem_ptr++;
  274. report_mem_err(pde);
  275. #ifdef CONFIG_MEMORY_FAILURE
  276. if ((pde & PDT_ADDR_PERM_ERR) ||
  277. ((pde & PDT_ADDR_SINGLE_ERR) == 0))
  278. memory_failure(pde >> PAGE_SHIFT, 0);
  279. else
  280. soft_offline_page(pde >> PAGE_SHIFT, 0);
  281. #else
  282. pr_crit("PDT: memory error at 0x%lx ignored.\n"
  283. "Rebuild kernel with CONFIG_MEMORY_FAILURE=y "
  284. "for real handling.\n",
  285. pde & PDT_ADDR_PHYS_MASK);
  286. #endif
  287. }
  288. }
  289. return 0;
  290. }
  291. static int __init pdt_initcall(void)
  292. {
  293. struct task_struct *kpdtd_task;
  294. if (pdt_type == PDT_NONE)
  295. return -ENODEV;
  296. kpdtd_task = kthread_run(pdt_mainloop, NULL, "kpdtd");
  297. return PTR_ERR_OR_ZERO(kpdtd_task);
  298. }
  299. late_initcall(pdt_initcall);