pdt.c 9.0 KB

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