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