1 // SPDX-License-Identifier: GPL-2.0
3 * Page Deallocation Table (PDT) support
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.
12 * possible future enhancements:
13 * - add userspace interface via procfs or sysfs to clear PDT
16 #include <linux/memblock.h>
17 #include <linux/seq_file.h>
18 #include <linux/kthread.h>
19 #include <linux/proc_fs.h>
20 #include <linux/initrd.h>
21 #include <linux/pgtable.h>
25 #include <asm/pdcpat.h>
26 #include <asm/sections.h>
27 #include <asm/pgtable.h>
29 enum pdt_access_type {
36 static enum pdt_access_type pdt_type;
38 /* PDT poll interval: 1 minute if errors, 5 minutes if everything OK. */
39 #define PDT_POLL_INTERVAL_DEFAULT (5*60*HZ)
40 #define PDT_POLL_INTERVAL_SHORT (1*60*HZ)
41 static unsigned long pdt_poll_interval = PDT_POLL_INTERVAL_DEFAULT;
43 /* global PDT status information */
44 static struct pdc_mem_retinfo pdt_status;
46 #define MAX_PDT_TABLE_SIZE PAGE_SIZE
47 #define MAX_PDT_ENTRIES (MAX_PDT_TABLE_SIZE / sizeof(unsigned long))
48 static unsigned long pdt_entry[MAX_PDT_ENTRIES] __page_aligned_bss;
51 * Constants for the pdt_entry format:
52 * A pdt_entry holds the physical address in bits 0-57, bits 58-61 are
53 * reserved, bit 62 is the perm bit and bit 63 is the error_type bit.
54 * The perm bit indicates whether the error have been verified as a permanent
55 * error (value of 1) or has not been verified, and may be transient (value
56 * of 0). The error_type bit indicates whether the error is a single bit error
57 * (value of 1) or a multiple bit error.
58 * On non-PAT machines phys_addr is encoded in bits 0-59 and error_type in bit
59 * 63. Those machines don't provide the perm bit.
62 #define PDT_ADDR_PHYS_MASK (pdt_type != PDT_PDC ? ~0x3f : ~0x0f)
63 #define PDT_ADDR_PERM_ERR (pdt_type != PDT_PDC ? 2UL : 0UL)
64 #define PDT_ADDR_SINGLE_ERR 1UL
66 /* report PDT entries via /proc/meminfo */
67 void arch_report_meminfo(struct seq_file *m)
69 if (pdt_type == PDT_NONE)
72 seq_printf(m, "PDT_max_entries: %7lu\n",
74 seq_printf(m, "PDT_cur_entries: %7lu\n",
75 pdt_status.pdt_entries);
78 static int get_info_pat_new(void)
80 struct pdc_pat_mem_retinfo pat_rinfo;
83 /* newer PAT machines like C8000 report info for all cells */
85 ret = pdc_pat_mem_pdt_info(&pat_rinfo);
89 pdt_status.pdt_size = pat_rinfo.max_pdt_entries;
90 pdt_status.pdt_entries = pat_rinfo.current_pdt_entries;
91 pdt_status.pdt_status = 0;
92 pdt_status.first_dbe_loc = pat_rinfo.first_dbe_loc;
93 pdt_status.good_mem = pat_rinfo.good_mem;
98 static int get_info_pat_cell(void)
100 struct pdc_pat_mem_cell_pdt_retinfo cell_rinfo;
103 /* older PAT machines like rp5470 report cell info only */
105 ret = pdc_pat_mem_pdt_cell_info(&cell_rinfo, parisc_cell_num);
109 pdt_status.pdt_size = cell_rinfo.max_pdt_entries;
110 pdt_status.pdt_entries = cell_rinfo.current_pdt_entries;
111 pdt_status.pdt_status = 0;
112 pdt_status.first_dbe_loc = cell_rinfo.first_dbe_loc;
113 pdt_status.good_mem = cell_rinfo.good_mem;
118 static void report_mem_err(unsigned long pde)
120 struct pdc_pat_mem_phys_mem_location loc;
124 addr = pde & PDT_ADDR_PHYS_MASK;
126 /* show DIMM slot description on PAT machines */
128 pdc_pat_mem_get_dimm_phys_location(&loc, addr);
129 sprintf(dimm_txt, "DIMM slot %02x, ", loc.dimm_slot);
133 pr_warn("PDT: BAD MEMORY at 0x%08lx, %s%s%s-bit error.\n",
135 pde & PDT_ADDR_PERM_ERR ? "permanent ":"",
136 pde & PDT_ADDR_SINGLE_ERR ? "single":"multi");
143 * Initialize kernel PDT structures, read initial PDT table from firmware,
144 * report all current PDT entries and mark bad memory with memblock_reserve()
145 * to avoid that the kernel will use broken memory areas.
148 void __init pdc_pdt_init(void)
151 unsigned long entries;
152 struct pdc_mem_read_pdt pdt_read_ret;
154 pdt_type = PDT_PAT_NEW;
155 ret = get_info_pat_new();
158 pdt_type = PDT_PAT_CELL;
159 ret = get_info_pat_cell();
164 /* non-PAT machines provide the standard PDC call */
165 ret = pdc_mem_pdt_info(&pdt_status);
170 pr_info("PDT: Firmware does not provide any page deallocation"
175 entries = pdt_status.pdt_entries;
176 if (WARN_ON(entries > MAX_PDT_ENTRIES))
177 entries = pdt_status.pdt_entries = MAX_PDT_ENTRIES;
179 pr_info("PDT: type %s, size %lu, entries %lu, status %lu, dbe_loc 0x%lx,"
180 " good_mem %lu MB\n",
181 pdt_type == PDT_PDC ? __stringify(PDT_PDC) :
182 pdt_type == PDT_PAT_CELL ? __stringify(PDT_PAT_CELL)
183 : __stringify(PDT_PAT_NEW),
184 pdt_status.pdt_size, pdt_status.pdt_entries,
185 pdt_status.pdt_status, pdt_status.first_dbe_loc,
186 pdt_status.good_mem / 1024 / 1024);
189 pr_info("PDT: Firmware reports all memory OK.\n");
193 if (pdt_status.first_dbe_loc &&
194 pdt_status.first_dbe_loc <= __pa((unsigned long)&_end))
195 pr_crit("CRITICAL: Bad memory inside kernel image memory area!\n");
197 pr_warn("PDT: Firmware reports %lu entries of faulty memory:\n",
200 if (pdt_type == PDT_PDC)
201 ret = pdc_mem_pdt_read_entries(&pdt_read_ret, pdt_entry);
204 struct pdc_pat_mem_read_pd_retinfo pat_pret;
206 if (pdt_type == PDT_PAT_CELL)
207 ret = pdc_pat_mem_read_cell_pdt(&pat_pret, pdt_entry,
210 ret = pdc_pat_mem_read_pd_pdt(&pat_pret, pdt_entry,
211 MAX_PDT_TABLE_SIZE, 0);
219 pr_warn("PDT: Get PDT entries failed with %d\n", ret);
223 for (i = 0; i < pdt_status.pdt_entries; i++) {
226 report_mem_err(pdt_entry[i]);
228 addr = pdt_entry[i] & PDT_ADDR_PHYS_MASK;
229 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) &&
230 addr >= initrd_start && addr < initrd_end)
231 pr_crit("CRITICAL: initrd possibly broken "
232 "due to bad memory!\n");
234 /* mark memory page bad */
235 memblock_reserve(pdt_entry[i] & PAGE_MASK, PAGE_SIZE);
236 num_poisoned_pages_inc(addr >> PAGE_SHIFT);
242 * This is the PDT kernel thread main loop.
245 static int pdt_mainloop(void *unused)
247 struct pdc_mem_read_pdt pdt_read_ret;
248 struct pdc_pat_mem_read_pd_retinfo pat_pret __maybe_unused;
249 unsigned long old_num_entries;
250 unsigned long *bad_mem_ptr;
254 set_current_state(TASK_INTERRUPTIBLE);
256 old_num_entries = pdt_status.pdt_entries;
258 schedule_timeout(pdt_poll_interval);
259 if (kthread_should_stop())
262 /* Do we have new PDT entries? */
265 ret = get_info_pat_new();
268 ret = get_info_pat_cell();
271 ret = pdc_mem_pdt_info(&pdt_status);
276 pr_warn("PDT: unexpected failure %d\n", ret);
280 /* if no new PDT entries, just wait again */
281 num = pdt_status.pdt_entries - old_num_entries;
285 /* decrease poll interval in case we found memory errors */
286 if (pdt_status.pdt_entries &&
287 pdt_poll_interval == PDT_POLL_INTERVAL_DEFAULT)
288 pdt_poll_interval = PDT_POLL_INTERVAL_SHORT;
290 /* limit entries to get */
291 if (num > MAX_PDT_ENTRIES) {
292 num = MAX_PDT_ENTRIES;
293 pdt_status.pdt_entries = old_num_entries + num;
296 /* get new entries */
300 if (pdt_status.pdt_entries > MAX_PDT_ENTRIES) {
301 pr_crit("PDT: too many entries.\n");
304 ret = pdc_pat_mem_read_cell_pdt(&pat_pret, pdt_entry,
306 bad_mem_ptr = &pdt_entry[old_num_entries];
309 ret = pdc_pat_mem_read_pd_pdt(&pat_pret,
311 num * sizeof(unsigned long),
312 old_num_entries * sizeof(unsigned long));
313 bad_mem_ptr = &pdt_entry[0];
317 ret = pdc_mem_pdt_read_entries(&pdt_read_ret,
319 bad_mem_ptr = &pdt_entry[old_num_entries];
323 /* report and mark memory broken */
325 unsigned long pde = *bad_mem_ptr++;
329 #ifdef CONFIG_MEMORY_FAILURE
330 if ((pde & PDT_ADDR_PERM_ERR) ||
331 ((pde & PDT_ADDR_SINGLE_ERR) == 0))
332 memory_failure(pde >> PAGE_SHIFT, 0);
334 soft_offline_page(pde >> PAGE_SHIFT, 0);
336 pr_crit("PDT: memory error at 0x%lx ignored.\n"
337 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y "
338 "for real handling.\n",
339 pde & PDT_ADDR_PHYS_MASK);
349 static int __init pdt_initcall(void)
351 struct task_struct *kpdtd_task;
353 if (pdt_type == PDT_NONE)
356 kpdtd_task = kthread_run(pdt_mainloop, NULL, "kpdtd");
358 return PTR_ERR_OR_ZERO(kpdtd_task);
361 late_initcall(pdt_initcall);