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.
10 * Copyright 2017 by Helge Deller <deller@gmx.de>
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/initrd.h>
20 #include <linux/pgtable.h>
21 #include <linux/swap.h>
22 #include <linux/swapops.h>
25 #include <asm/pdcpat.h>
26 #include <asm/sections.h>
28 enum pdt_access_type
{
35 static enum pdt_access_type pdt_type
;
37 /* PDT poll interval: 1 minute if errors, 5 minutes if everything OK. */
38 #define PDT_POLL_INTERVAL_DEFAULT (5*60*HZ)
39 #define PDT_POLL_INTERVAL_SHORT (1*60*HZ)
40 static unsigned long pdt_poll_interval
= PDT_POLL_INTERVAL_DEFAULT
;
42 /* global PDT status information */
43 static struct pdc_mem_retinfo pdt_status
;
45 #define MAX_PDT_TABLE_SIZE PAGE_SIZE
46 #define MAX_PDT_ENTRIES (MAX_PDT_TABLE_SIZE / sizeof(unsigned long))
47 static unsigned long pdt_entry
[MAX_PDT_ENTRIES
] __page_aligned_bss
;
50 * Constants for the pdt_entry format:
51 * A pdt_entry holds the physical address in bits 0-57, bits 58-61 are
52 * reserved, bit 62 is the perm bit and bit 63 is the error_type bit.
53 * The perm bit indicates whether the error have been verified as a permanent
54 * error (value of 1) or has not been verified, and may be transient (value
55 * of 0). The error_type bit indicates whether the error is a single bit error
56 * (value of 1) or a multiple bit error.
57 * On non-PAT machines phys_addr is encoded in bits 0-59 and error_type in bit
58 * 63. Those machines don't provide the perm bit.
61 #define PDT_ADDR_PHYS_MASK (pdt_type != PDT_PDC ? ~0x3f : ~0x0f)
62 #define PDT_ADDR_PERM_ERR (pdt_type != PDT_PDC ? 2UL : 0UL)
63 #define PDT_ADDR_SINGLE_ERR 1UL
65 /* report PDT entries via /proc/meminfo */
66 void arch_report_meminfo(struct seq_file
*m
)
68 if (pdt_type
== PDT_NONE
)
71 seq_printf(m
, "PDT_max_entries: %7lu\n",
73 seq_printf(m
, "PDT_cur_entries: %7lu\n",
74 pdt_status
.pdt_entries
);
77 static int get_info_pat_new(void)
79 struct pdc_pat_mem_retinfo pat_rinfo
;
82 /* newer PAT machines like C8000 report info for all cells */
84 ret
= pdc_pat_mem_pdt_info(&pat_rinfo
);
88 pdt_status
.pdt_size
= pat_rinfo
.max_pdt_entries
;
89 pdt_status
.pdt_entries
= pat_rinfo
.current_pdt_entries
;
90 pdt_status
.pdt_status
= 0;
91 pdt_status
.first_dbe_loc
= pat_rinfo
.first_dbe_loc
;
92 pdt_status
.good_mem
= pat_rinfo
.good_mem
;
97 static int get_info_pat_cell(void)
99 struct pdc_pat_mem_cell_pdt_retinfo cell_rinfo
;
102 /* older PAT machines like rp5470 report cell info only */
104 ret
= pdc_pat_mem_pdt_cell_info(&cell_rinfo
, parisc_cell_num
);
108 pdt_status
.pdt_size
= cell_rinfo
.max_pdt_entries
;
109 pdt_status
.pdt_entries
= cell_rinfo
.current_pdt_entries
;
110 pdt_status
.pdt_status
= 0;
111 pdt_status
.first_dbe_loc
= cell_rinfo
.first_dbe_loc
;
112 pdt_status
.good_mem
= cell_rinfo
.good_mem
;
117 static void report_mem_err(unsigned long pde
)
119 struct pdc_pat_mem_phys_mem_location loc
;
123 addr
= pde
& PDT_ADDR_PHYS_MASK
;
125 /* show DIMM slot description on PAT machines */
127 pdc_pat_mem_get_dimm_phys_location(&loc
, addr
);
128 sprintf(dimm_txt
, "DIMM slot %02x, ", loc
.dimm_slot
);
132 pr_warn("PDT: BAD MEMORY at 0x%08lx, %s%s%s-bit error.\n",
134 pde
& PDT_ADDR_PERM_ERR
? "permanent ":"",
135 pde
& PDT_ADDR_SINGLE_ERR
? "single":"multi");
142 * Initialize kernel PDT structures, read initial PDT table from firmware,
143 * report all current PDT entries and mark bad memory with memblock_reserve()
144 * to avoid that the kernel will use broken memory areas.
147 void __init
pdc_pdt_init(void)
150 unsigned long entries
;
151 struct pdc_mem_read_pdt pdt_read_ret
;
153 pdt_type
= PDT_PAT_NEW
;
154 ret
= get_info_pat_new();
157 pdt_type
= PDT_PAT_CELL
;
158 ret
= get_info_pat_cell();
163 /* non-PAT machines provide the standard PDC call */
164 ret
= pdc_mem_pdt_info(&pdt_status
);
169 pr_info("PDT: Firmware does not provide any page deallocation"
174 entries
= pdt_status
.pdt_entries
;
175 if (WARN_ON(entries
> MAX_PDT_ENTRIES
))
176 entries
= pdt_status
.pdt_entries
= MAX_PDT_ENTRIES
;
178 pr_info("PDT: type %s, size %lu, entries %lu, status %lu, dbe_loc 0x%lx,"
179 " good_mem %lu MB\n",
180 pdt_type
== PDT_PDC
? __stringify(PDT_PDC
) :
181 pdt_type
== PDT_PAT_CELL
? __stringify(PDT_PAT_CELL
)
182 : __stringify(PDT_PAT_NEW
),
183 pdt_status
.pdt_size
, pdt_status
.pdt_entries
,
184 pdt_status
.pdt_status
, pdt_status
.first_dbe_loc
,
185 pdt_status
.good_mem
/ 1024 / 1024);
188 pr_info("PDT: Firmware reports all memory OK.\n");
192 if (pdt_status
.first_dbe_loc
&&
193 pdt_status
.first_dbe_loc
<= __pa((unsigned long)&_end
))
194 pr_crit("CRITICAL: Bad memory inside kernel image memory area!\n");
196 pr_warn("PDT: Firmware reports %lu entries of faulty memory:\n",
199 if (pdt_type
== PDT_PDC
)
200 ret
= pdc_mem_pdt_read_entries(&pdt_read_ret
, pdt_entry
);
203 struct pdc_pat_mem_read_pd_retinfo pat_pret
;
205 if (pdt_type
== PDT_PAT_CELL
)
206 ret
= pdc_pat_mem_read_cell_pdt(&pat_pret
, pdt_entry
,
209 ret
= pdc_pat_mem_read_pd_pdt(&pat_pret
, pdt_entry
,
210 MAX_PDT_TABLE_SIZE
, 0);
218 pr_warn("PDT: Get PDT entries failed with %d\n", ret
);
222 for (i
= 0; i
< pdt_status
.pdt_entries
; i
++) {
225 report_mem_err(pdt_entry
[i
]);
227 addr
= pdt_entry
[i
] & PDT_ADDR_PHYS_MASK
;
228 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD
) &&
229 addr
>= initrd_start
&& addr
< initrd_end
)
230 pr_crit("CRITICAL: initrd possibly broken "
231 "due to bad memory!\n");
233 /* mark memory page bad */
234 memblock_reserve(pdt_entry
[i
] & PAGE_MASK
, PAGE_SIZE
);
235 num_poisoned_pages_inc();
241 * This is the PDT kernel thread main loop.
244 static int pdt_mainloop(void *unused
)
246 struct pdc_mem_read_pdt pdt_read_ret
;
247 struct pdc_pat_mem_read_pd_retinfo pat_pret __maybe_unused
;
248 unsigned long old_num_entries
;
249 unsigned long *bad_mem_ptr
;
253 set_current_state(TASK_INTERRUPTIBLE
);
255 old_num_entries
= pdt_status
.pdt_entries
;
257 schedule_timeout(pdt_poll_interval
);
258 if (kthread_should_stop())
261 /* Do we have new PDT entries? */
264 ret
= get_info_pat_new();
267 ret
= get_info_pat_cell();
270 ret
= pdc_mem_pdt_info(&pdt_status
);
275 pr_warn("PDT: unexpected failure %d\n", ret
);
279 /* if no new PDT entries, just wait again */
280 num
= pdt_status
.pdt_entries
- old_num_entries
;
284 /* decrease poll interval in case we found memory errors */
285 if (pdt_status
.pdt_entries
&&
286 pdt_poll_interval
== PDT_POLL_INTERVAL_DEFAULT
)
287 pdt_poll_interval
= PDT_POLL_INTERVAL_SHORT
;
289 /* limit entries to get */
290 if (num
> MAX_PDT_ENTRIES
) {
291 num
= MAX_PDT_ENTRIES
;
292 pdt_status
.pdt_entries
= old_num_entries
+ num
;
295 /* get new entries */
299 if (pdt_status
.pdt_entries
> MAX_PDT_ENTRIES
) {
300 pr_crit("PDT: too many entries.\n");
303 ret
= pdc_pat_mem_read_cell_pdt(&pat_pret
, pdt_entry
,
305 bad_mem_ptr
= &pdt_entry
[old_num_entries
];
308 ret
= pdc_pat_mem_read_pd_pdt(&pat_pret
,
310 num
* sizeof(unsigned long),
311 old_num_entries
* sizeof(unsigned long));
312 bad_mem_ptr
= &pdt_entry
[0];
316 ret
= pdc_mem_pdt_read_entries(&pdt_read_ret
,
318 bad_mem_ptr
= &pdt_entry
[old_num_entries
];
322 /* report and mark memory broken */
324 unsigned long pde
= *bad_mem_ptr
++;
328 #ifdef CONFIG_MEMORY_FAILURE
329 if ((pde
& PDT_ADDR_PERM_ERR
) ||
330 ((pde
& PDT_ADDR_SINGLE_ERR
) == 0))
331 memory_failure(pde
>> PAGE_SHIFT
, 0);
333 soft_offline_page(pde
>> PAGE_SHIFT
, 0);
335 pr_crit("PDT: memory error at 0x%lx ignored.\n"
336 "Rebuild kernel with CONFIG_MEMORY_FAILURE=y "
337 "for real handling.\n",
338 pde
& PDT_ADDR_PHYS_MASK
);
348 static int __init
pdt_initcall(void)
350 struct task_struct
*kpdtd_task
;
352 if (pdt_type
== PDT_NONE
)
355 kpdtd_task
= kthread_create(pdt_mainloop
, NULL
, "kpdtd");
356 if (IS_ERR(kpdtd_task
))
357 return PTR_ERR(kpdtd_task
);
359 wake_up_process(kpdtd_task
);
364 late_initcall(pdt_initcall
);