xref: /openbmc/linux/drivers/ras/cec.c (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2011d8261SBorislav Petkov #include <linux/mm.h>
3011d8261SBorislav Petkov #include <linux/gfp.h>
4011d8261SBorislav Petkov #include <linux/kernel.h>
5011d8261SBorislav Petkov 
6011d8261SBorislav Petkov #include <asm/mce.h>
7011d8261SBorislav Petkov 
8011d8261SBorislav Petkov #include "debugfs.h"
9011d8261SBorislav Petkov 
10011d8261SBorislav Petkov /*
11011d8261SBorislav Petkov  * RAS Correctable Errors Collector
12011d8261SBorislav Petkov  *
13011d8261SBorislav Petkov  * This is a simple gadget which collects correctable errors and counts their
14011d8261SBorislav Petkov  * occurrence per physical page address.
15011d8261SBorislav Petkov  *
16011d8261SBorislav Petkov  * We've opted for possibly the simplest data structure to collect those - an
17011d8261SBorislav Petkov  * array of the size of a memory page. It stores 512 u64's with the following
18011d8261SBorislav Petkov  * structure:
19011d8261SBorislav Petkov  *
20011d8261SBorislav Petkov  * [63 ... PFN ... 12 | 11 ... generation ... 10 | 9 ... count ... 0]
21011d8261SBorislav Petkov  *
22011d8261SBorislav Petkov  * The generation in the two highest order bits is two bits which are set to 11b
23011d8261SBorislav Petkov  * on every insertion. During the course of each entry's existence, the
24011d8261SBorislav Petkov  * generation field gets decremented during spring cleaning to 10b, then 01b and
25011d8261SBorislav Petkov  * then 00b.
26011d8261SBorislav Petkov  *
27011d8261SBorislav Petkov  * This way we're employing the natural numeric ordering to make sure that newly
28011d8261SBorislav Petkov  * inserted/touched elements have higher 12-bit counts (which we've manufactured)
29011d8261SBorislav Petkov  * and thus iterating over the array initially won't kick out those elements
30011d8261SBorislav Petkov  * which were inserted last.
31011d8261SBorislav Petkov  *
32011d8261SBorislav Petkov  * Spring cleaning is what we do when we reach a certain number CLEAN_ELEMS of
33011d8261SBorislav Petkov  * elements entered into the array, during which, we're decaying all elements.
34011d8261SBorislav Petkov  * If, after decay, an element gets inserted again, its generation is set to 11b
35011d8261SBorislav Petkov  * to make sure it has higher numerical count than other, older elements and
36011d8261SBorislav Petkov  * thus emulate an an LRU-like behavior when deleting elements to free up space
37011d8261SBorislav Petkov  * in the page.
38011d8261SBorislav Petkov  *
39011d8261SBorislav Petkov  * When an element reaches it's max count of count_threshold, we try to poison
40011d8261SBorislav Petkov  * it by assuming that errors triggered count_threshold times in a single page
41011d8261SBorislav Petkov  * are excessive and that page shouldn't be used anymore. count_threshold is
42011d8261SBorislav Petkov  * initialized to COUNT_MASK which is the maximum.
43011d8261SBorislav Petkov  *
44011d8261SBorislav Petkov  * That error event entry causes cec_add_elem() to return !0 value and thus
45011d8261SBorislav Petkov  * signal to its callers to log the error.
46011d8261SBorislav Petkov  *
47011d8261SBorislav Petkov  * To the question why we've chosen a page and moving elements around with
48011d8261SBorislav Petkov  * memmove(), it is because it is a very simple structure to handle and max data
49011d8261SBorislav Petkov  * movement is 4K which on highly optimized modern CPUs is almost unnoticeable.
50011d8261SBorislav Petkov  * We wanted to avoid the pointer traversal of more complex structures like a
51011d8261SBorislav Petkov  * linked list or some sort of a balancing search tree.
52011d8261SBorislav Petkov  *
53011d8261SBorislav Petkov  * Deleting an element takes O(n) but since it is only a single page, it should
54011d8261SBorislav Petkov  * be fast enough and it shouldn't happen all too often depending on error
55011d8261SBorislav Petkov  * patterns.
56011d8261SBorislav Petkov  */
57011d8261SBorislav Petkov 
58011d8261SBorislav Petkov #undef pr_fmt
59011d8261SBorislav Petkov #define pr_fmt(fmt) "RAS: " fmt
60011d8261SBorislav Petkov 
61011d8261SBorislav Petkov /*
62011d8261SBorislav Petkov  * We use DECAY_BITS bits of PAGE_SHIFT bits for counting decay, i.e., how long
63011d8261SBorislav Petkov  * elements have stayed in the array without having been accessed again.
64011d8261SBorislav Petkov  */
65011d8261SBorislav Petkov #define DECAY_BITS		2
66011d8261SBorislav Petkov #define DECAY_MASK		((1ULL << DECAY_BITS) - 1)
67011d8261SBorislav Petkov #define MAX_ELEMS		(PAGE_SIZE / sizeof(u64))
68011d8261SBorislav Petkov 
69011d8261SBorislav Petkov /*
70011d8261SBorislav Petkov  * Threshold amount of inserted elements after which we start spring
71011d8261SBorislav Petkov  * cleaning.
72011d8261SBorislav Petkov  */
73011d8261SBorislav Petkov #define CLEAN_ELEMS		(MAX_ELEMS >> DECAY_BITS)
74011d8261SBorislav Petkov 
75011d8261SBorislav Petkov /* Bits which count the number of errors happened in this 4K page. */
76011d8261SBorislav Petkov #define COUNT_BITS		(PAGE_SHIFT - DECAY_BITS)
77011d8261SBorislav Petkov #define COUNT_MASK		((1ULL << COUNT_BITS) - 1)
78011d8261SBorislav Petkov #define FULL_COUNT_MASK		(PAGE_SIZE - 1)
79011d8261SBorislav Petkov 
80011d8261SBorislav Petkov /*
81011d8261SBorislav Petkov  * u64: [ 63 ... 12 | DECAY_BITS | COUNT_BITS ]
82011d8261SBorislav Petkov  */
83011d8261SBorislav Petkov 
84011d8261SBorislav Petkov #define PFN(e)			((e) >> PAGE_SHIFT)
85011d8261SBorislav Petkov #define DECAY(e)		(((e) >> COUNT_BITS) & DECAY_MASK)
86011d8261SBorislav Petkov #define COUNT(e)		((unsigned int)(e) & COUNT_MASK)
87011d8261SBorislav Petkov #define FULL_COUNT(e)		((e) & (PAGE_SIZE - 1))
88011d8261SBorislav Petkov 
89011d8261SBorislav Petkov static struct ce_array {
90011d8261SBorislav Petkov 	u64 *array;			/* container page */
91011d8261SBorislav Petkov 	unsigned int n;			/* number of elements in the array */
92011d8261SBorislav Petkov 
93011d8261SBorislav Petkov 	unsigned int decay_count;	/*
94011d8261SBorislav Petkov 					 * number of element insertions/increments
95011d8261SBorislav Petkov 					 * since the last spring cleaning.
96011d8261SBorislav Petkov 					 */
97011d8261SBorislav Petkov 
98011d8261SBorislav Petkov 	u64 pfns_poisoned;		/*
99011d8261SBorislav Petkov 					 * number of PFNs which got poisoned.
100011d8261SBorislav Petkov 					 */
101011d8261SBorislav Petkov 
102011d8261SBorislav Petkov 	u64 ces_entered;		/*
103011d8261SBorislav Petkov 					 * The number of correctable errors
104011d8261SBorislav Petkov 					 * entered into the collector.
105011d8261SBorislav Petkov 					 */
106011d8261SBorislav Petkov 
107011d8261SBorislav Petkov 	u64 decays_done;		/*
108011d8261SBorislav Petkov 					 * Times we did spring cleaning.
109011d8261SBorislav Petkov 					 */
110011d8261SBorislav Petkov 
111011d8261SBorislav Petkov 	union {
112011d8261SBorislav Petkov 		struct {
113011d8261SBorislav Petkov 			__u32	disabled : 1,	/* cmdline disabled */
114011d8261SBorislav Petkov 			__resv   : 31;
115011d8261SBorislav Petkov 		};
116011d8261SBorislav Petkov 		__u32 flags;
117011d8261SBorislav Petkov 	};
118011d8261SBorislav Petkov } ce_arr;
119011d8261SBorislav Petkov 
120011d8261SBorislav Petkov static DEFINE_MUTEX(ce_mutex);
121011d8261SBorislav Petkov static u64 dfs_pfn;
122011d8261SBorislav Petkov 
123011d8261SBorislav Petkov /* Amount of errors after which we offline */
124011d8261SBorislav Petkov static unsigned int count_threshold = COUNT_MASK;
125011d8261SBorislav Petkov 
126011d8261SBorislav Petkov /*
127011d8261SBorislav Petkov  * The timer "decays" element count each timer_interval which is 24hrs by
128011d8261SBorislav Petkov  * default.
129011d8261SBorislav Petkov  */
130011d8261SBorislav Petkov 
131011d8261SBorislav Petkov #define CEC_TIMER_DEFAULT_INTERVAL	24 * 60 * 60	/* 24 hrs */
132011d8261SBorislav Petkov #define CEC_TIMER_MIN_INTERVAL		 1 * 60 * 60	/* 1h */
133011d8261SBorislav Petkov #define CEC_TIMER_MAX_INTERVAL	   30 *	24 * 60 * 60	/* one month */
134011d8261SBorislav Petkov static struct timer_list cec_timer;
135011d8261SBorislav Petkov static u64 timer_interval = CEC_TIMER_DEFAULT_INTERVAL;
136011d8261SBorislav Petkov 
137011d8261SBorislav Petkov /*
138011d8261SBorislav Petkov  * Decrement decay value. We're using DECAY_BITS bits to denote decay of an
139011d8261SBorislav Petkov  * element in the array. On insertion and any access, it gets reset to max.
140011d8261SBorislav Petkov  */
141011d8261SBorislav Petkov static void do_spring_cleaning(struct ce_array *ca)
142011d8261SBorislav Petkov {
143011d8261SBorislav Petkov 	int i;
144011d8261SBorislav Petkov 
145011d8261SBorislav Petkov 	for (i = 0; i < ca->n; i++) {
146011d8261SBorislav Petkov 		u8 decay = DECAY(ca->array[i]);
147011d8261SBorislav Petkov 
148011d8261SBorislav Petkov 		if (!decay)
149011d8261SBorislav Petkov 			continue;
150011d8261SBorislav Petkov 
151011d8261SBorislav Petkov 		decay--;
152011d8261SBorislav Petkov 
153011d8261SBorislav Petkov 		ca->array[i] &= ~(DECAY_MASK << COUNT_BITS);
154011d8261SBorislav Petkov 		ca->array[i] |= (decay << COUNT_BITS);
155011d8261SBorislav Petkov 	}
156011d8261SBorislav Petkov 	ca->decay_count = 0;
157011d8261SBorislav Petkov 	ca->decays_done++;
158011d8261SBorislav Petkov }
159011d8261SBorislav Petkov 
160011d8261SBorislav Petkov /*
161011d8261SBorislav Petkov  * @interval in seconds
162011d8261SBorislav Petkov  */
163011d8261SBorislav Petkov static void cec_mod_timer(struct timer_list *t, unsigned long interval)
164011d8261SBorislav Petkov {
165011d8261SBorislav Petkov 	unsigned long iv;
166011d8261SBorislav Petkov 
167011d8261SBorislav Petkov 	iv = interval * HZ + jiffies;
168011d8261SBorislav Petkov 
169011d8261SBorislav Petkov 	mod_timer(t, round_jiffies(iv));
170011d8261SBorislav Petkov }
171011d8261SBorislav Petkov 
172011d8261SBorislav Petkov static void cec_timer_fn(unsigned long data)
173011d8261SBorislav Petkov {
174011d8261SBorislav Petkov 	struct ce_array *ca = (struct ce_array *)data;
175011d8261SBorislav Petkov 
176011d8261SBorislav Petkov 	do_spring_cleaning(ca);
177011d8261SBorislav Petkov 
178011d8261SBorislav Petkov 	cec_mod_timer(&cec_timer, timer_interval);
179011d8261SBorislav Petkov }
180011d8261SBorislav Petkov 
181011d8261SBorislav Petkov /*
182011d8261SBorislav Petkov  * @to: index of the smallest element which is >= then @pfn.
183011d8261SBorislav Petkov  *
184011d8261SBorislav Petkov  * Return the index of the pfn if found, otherwise negative value.
185011d8261SBorislav Petkov  */
186011d8261SBorislav Petkov static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
187011d8261SBorislav Petkov {
188011d8261SBorislav Petkov 	u64 this_pfn;
189011d8261SBorislav Petkov 	int min = 0, max = ca->n;
190011d8261SBorislav Petkov 
191011d8261SBorislav Petkov 	while (min < max) {
192011d8261SBorislav Petkov 		int tmp = (max + min) >> 1;
193011d8261SBorislav Petkov 
194011d8261SBorislav Petkov 		this_pfn = PFN(ca->array[tmp]);
195011d8261SBorislav Petkov 
196011d8261SBorislav Petkov 		if (this_pfn < pfn)
197011d8261SBorislav Petkov 			min = tmp + 1;
198011d8261SBorislav Petkov 		else if (this_pfn > pfn)
199011d8261SBorislav Petkov 			max = tmp;
200011d8261SBorislav Petkov 		else {
201011d8261SBorislav Petkov 			min = tmp;
202011d8261SBorislav Petkov 			break;
203011d8261SBorislav Petkov 		}
204011d8261SBorislav Petkov 	}
205011d8261SBorislav Petkov 
206011d8261SBorislav Petkov 	if (to)
207011d8261SBorislav Petkov 		*to = min;
208011d8261SBorislav Petkov 
209011d8261SBorislav Petkov 	this_pfn = PFN(ca->array[min]);
210011d8261SBorislav Petkov 
211011d8261SBorislav Petkov 	if (this_pfn == pfn)
212011d8261SBorislav Petkov 		return min;
213011d8261SBorislav Petkov 
214011d8261SBorislav Petkov 	return -ENOKEY;
215011d8261SBorislav Petkov }
216011d8261SBorislav Petkov 
217011d8261SBorislav Petkov static int find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
218011d8261SBorislav Petkov {
219011d8261SBorislav Petkov 	WARN_ON(!to);
220011d8261SBorislav Petkov 
221011d8261SBorislav Petkov 	if (!ca->n) {
222011d8261SBorislav Petkov 		*to = 0;
223011d8261SBorislav Petkov 		return -ENOKEY;
224011d8261SBorislav Petkov 	}
225011d8261SBorislav Petkov 	return __find_elem(ca, pfn, to);
226011d8261SBorislav Petkov }
227011d8261SBorislav Petkov 
228011d8261SBorislav Petkov static void del_elem(struct ce_array *ca, int idx)
229011d8261SBorislav Petkov {
230011d8261SBorislav Petkov 	/* Save us a function call when deleting the last element. */
231011d8261SBorislav Petkov 	if (ca->n - (idx + 1))
232011d8261SBorislav Petkov 		memmove((void *)&ca->array[idx],
233011d8261SBorislav Petkov 			(void *)&ca->array[idx + 1],
234011d8261SBorislav Petkov 			(ca->n - (idx + 1)) * sizeof(u64));
235011d8261SBorislav Petkov 
236011d8261SBorislav Petkov 	ca->n--;
237011d8261SBorislav Petkov }
238011d8261SBorislav Petkov 
239011d8261SBorislav Petkov static u64 del_lru_elem_unlocked(struct ce_array *ca)
240011d8261SBorislav Petkov {
241011d8261SBorislav Petkov 	unsigned int min = FULL_COUNT_MASK;
242011d8261SBorislav Petkov 	int i, min_idx = 0;
243011d8261SBorislav Petkov 
244011d8261SBorislav Petkov 	for (i = 0; i < ca->n; i++) {
245011d8261SBorislav Petkov 		unsigned int this = FULL_COUNT(ca->array[i]);
246011d8261SBorislav Petkov 
247011d8261SBorislav Petkov 		if (min > this) {
248011d8261SBorislav Petkov 			min = this;
249011d8261SBorislav Petkov 			min_idx = i;
250011d8261SBorislav Petkov 		}
251011d8261SBorislav Petkov 	}
252011d8261SBorislav Petkov 
253011d8261SBorislav Petkov 	del_elem(ca, min_idx);
254011d8261SBorislav Petkov 
255011d8261SBorislav Petkov 	return PFN(ca->array[min_idx]);
256011d8261SBorislav Petkov }
257011d8261SBorislav Petkov 
258011d8261SBorislav Petkov /*
259011d8261SBorislav Petkov  * We return the 0th pfn in the error case under the assumption that it cannot
260011d8261SBorislav Petkov  * be poisoned and excessive CEs in there are a serious deal anyway.
261011d8261SBorislav Petkov  */
262011d8261SBorislav Petkov static u64 __maybe_unused del_lru_elem(void)
263011d8261SBorislav Petkov {
264011d8261SBorislav Petkov 	struct ce_array *ca = &ce_arr;
265011d8261SBorislav Petkov 	u64 pfn;
266011d8261SBorislav Petkov 
267011d8261SBorislav Petkov 	if (!ca->n)
268011d8261SBorislav Petkov 		return 0;
269011d8261SBorislav Petkov 
270011d8261SBorislav Petkov 	mutex_lock(&ce_mutex);
271011d8261SBorislav Petkov 	pfn = del_lru_elem_unlocked(ca);
272011d8261SBorislav Petkov 	mutex_unlock(&ce_mutex);
273011d8261SBorislav Petkov 
274011d8261SBorislav Petkov 	return pfn;
275011d8261SBorislav Petkov }
276011d8261SBorislav Petkov 
277011d8261SBorislav Petkov 
278011d8261SBorislav Petkov int cec_add_elem(u64 pfn)
279011d8261SBorislav Petkov {
280011d8261SBorislav Petkov 	struct ce_array *ca = &ce_arr;
281011d8261SBorislav Petkov 	unsigned int to;
282011d8261SBorislav Petkov 	int count, ret = 0;
283011d8261SBorislav Petkov 
284011d8261SBorislav Petkov 	/*
285011d8261SBorislav Petkov 	 * We can be called very early on the identify_cpu() path where we are
286011d8261SBorislav Petkov 	 * not initialized yet. We ignore the error for simplicity.
287011d8261SBorislav Petkov 	 */
288011d8261SBorislav Petkov 	if (!ce_arr.array || ce_arr.disabled)
289011d8261SBorislav Petkov 		return -ENODEV;
290011d8261SBorislav Petkov 
291011d8261SBorislav Petkov 	ca->ces_entered++;
292011d8261SBorislav Petkov 
293011d8261SBorislav Petkov 	mutex_lock(&ce_mutex);
294011d8261SBorislav Petkov 
295011d8261SBorislav Petkov 	if (ca->n == MAX_ELEMS)
296011d8261SBorislav Petkov 		WARN_ON(!del_lru_elem_unlocked(ca));
297011d8261SBorislav Petkov 
298011d8261SBorislav Petkov 	ret = find_elem(ca, pfn, &to);
299011d8261SBorislav Petkov 	if (ret < 0) {
300011d8261SBorislav Petkov 		/*
301011d8261SBorislav Petkov 		 * Shift range [to-end] to make room for one more element.
302011d8261SBorislav Petkov 		 */
303011d8261SBorislav Petkov 		memmove((void *)&ca->array[to + 1],
304011d8261SBorislav Petkov 			(void *)&ca->array[to],
305011d8261SBorislav Petkov 			(ca->n - to) * sizeof(u64));
306011d8261SBorislav Petkov 
307011d8261SBorislav Petkov 		ca->array[to] = (pfn << PAGE_SHIFT) |
308011d8261SBorislav Petkov 				(DECAY_MASK << COUNT_BITS) | 1;
309011d8261SBorislav Petkov 
310011d8261SBorislav Petkov 		ca->n++;
311011d8261SBorislav Petkov 
312011d8261SBorislav Petkov 		ret = 0;
313011d8261SBorislav Petkov 
314011d8261SBorislav Petkov 		goto decay;
315011d8261SBorislav Petkov 	}
316011d8261SBorislav Petkov 
317011d8261SBorislav Petkov 	count = COUNT(ca->array[to]);
318011d8261SBorislav Petkov 
319011d8261SBorislav Petkov 	if (count < count_threshold) {
320011d8261SBorislav Petkov 		ca->array[to] |= (DECAY_MASK << COUNT_BITS);
321011d8261SBorislav Petkov 		ca->array[to]++;
322011d8261SBorislav Petkov 
323011d8261SBorislav Petkov 		ret = 0;
324011d8261SBorislav Petkov 	} else {
325011d8261SBorislav Petkov 		u64 pfn = ca->array[to] >> PAGE_SHIFT;
326011d8261SBorislav Petkov 
327011d8261SBorislav Petkov 		if (!pfn_valid(pfn)) {
328011d8261SBorislav Petkov 			pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
329011d8261SBorislav Petkov 		} else {
330011d8261SBorislav Petkov 			/* We have reached max count for this page, soft-offline it. */
331011d8261SBorislav Petkov 			pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
332011d8261SBorislav Petkov 			memory_failure_queue(pfn, 0, MF_SOFT_OFFLINE);
333011d8261SBorislav Petkov 			ca->pfns_poisoned++;
334011d8261SBorislav Petkov 		}
335011d8261SBorislav Petkov 
336011d8261SBorislav Petkov 		del_elem(ca, to);
337011d8261SBorislav Petkov 
338011d8261SBorislav Petkov 		/*
339011d8261SBorislav Petkov 		 * Return a >0 value to denote that we've reached the offlining
340011d8261SBorislav Petkov 		 * threshold.
341011d8261SBorislav Petkov 		 */
342011d8261SBorislav Petkov 		ret = 1;
343011d8261SBorislav Petkov 
344011d8261SBorislav Petkov 		goto unlock;
345011d8261SBorislav Petkov 	}
346011d8261SBorislav Petkov 
347011d8261SBorislav Petkov decay:
348011d8261SBorislav Petkov 	ca->decay_count++;
349011d8261SBorislav Petkov 
350011d8261SBorislav Petkov 	if (ca->decay_count >= CLEAN_ELEMS)
351011d8261SBorislav Petkov 		do_spring_cleaning(ca);
352011d8261SBorislav Petkov 
353011d8261SBorislav Petkov unlock:
354011d8261SBorislav Petkov 	mutex_unlock(&ce_mutex);
355011d8261SBorislav Petkov 
356011d8261SBorislav Petkov 	return ret;
357011d8261SBorislav Petkov }
358011d8261SBorislav Petkov 
359011d8261SBorislav Petkov static int u64_get(void *data, u64 *val)
360011d8261SBorislav Petkov {
361011d8261SBorislav Petkov 	*val = *(u64 *)data;
362011d8261SBorislav Petkov 
363011d8261SBorislav Petkov 	return 0;
364011d8261SBorislav Petkov }
365011d8261SBorislav Petkov 
366011d8261SBorislav Petkov static int pfn_set(void *data, u64 val)
367011d8261SBorislav Petkov {
368011d8261SBorislav Petkov 	*(u64 *)data = val;
369011d8261SBorislav Petkov 
370011d8261SBorislav Petkov 	return cec_add_elem(val);
371011d8261SBorislav Petkov }
372011d8261SBorislav Petkov 
373011d8261SBorislav Petkov DEFINE_DEBUGFS_ATTRIBUTE(pfn_ops, u64_get, pfn_set, "0x%llx\n");
374011d8261SBorislav Petkov 
375011d8261SBorislav Petkov static int decay_interval_set(void *data, u64 val)
376011d8261SBorislav Petkov {
377011d8261SBorislav Petkov 	*(u64 *)data = val;
378011d8261SBorislav Petkov 
379011d8261SBorislav Petkov 	if (val < CEC_TIMER_MIN_INTERVAL)
380011d8261SBorislav Petkov 		return -EINVAL;
381011d8261SBorislav Petkov 
382011d8261SBorislav Petkov 	if (val > CEC_TIMER_MAX_INTERVAL)
383011d8261SBorislav Petkov 		return -EINVAL;
384011d8261SBorislav Petkov 
385011d8261SBorislav Petkov 	timer_interval = val;
386011d8261SBorislav Petkov 
387011d8261SBorislav Petkov 	cec_mod_timer(&cec_timer, timer_interval);
388011d8261SBorislav Petkov 	return 0;
389011d8261SBorislav Petkov }
390011d8261SBorislav Petkov DEFINE_DEBUGFS_ATTRIBUTE(decay_interval_ops, u64_get, decay_interval_set, "%lld\n");
391011d8261SBorislav Petkov 
392011d8261SBorislav Petkov static int count_threshold_set(void *data, u64 val)
393011d8261SBorislav Petkov {
394011d8261SBorislav Petkov 	*(u64 *)data = val;
395011d8261SBorislav Petkov 
396011d8261SBorislav Petkov 	if (val > COUNT_MASK)
397011d8261SBorislav Petkov 		val = COUNT_MASK;
398011d8261SBorislav Petkov 
399011d8261SBorislav Petkov 	count_threshold = val;
400011d8261SBorislav Petkov 
401011d8261SBorislav Petkov 	return 0;
402011d8261SBorislav Petkov }
403011d8261SBorislav Petkov DEFINE_DEBUGFS_ATTRIBUTE(count_threshold_ops, u64_get, count_threshold_set, "%lld\n");
404011d8261SBorislav Petkov 
405011d8261SBorislav Petkov static int array_dump(struct seq_file *m, void *v)
406011d8261SBorislav Petkov {
407011d8261SBorislav Petkov 	struct ce_array *ca = &ce_arr;
408011d8261SBorislav Petkov 	u64 prev = 0;
409011d8261SBorislav Petkov 	int i;
410011d8261SBorislav Petkov 
411011d8261SBorislav Petkov 	mutex_lock(&ce_mutex);
412011d8261SBorislav Petkov 
413011d8261SBorislav Petkov 	seq_printf(m, "{ n: %d\n", ca->n);
414011d8261SBorislav Petkov 	for (i = 0; i < ca->n; i++) {
415011d8261SBorislav Petkov 		u64 this = PFN(ca->array[i]);
416011d8261SBorislav Petkov 
417011d8261SBorislav Petkov 		seq_printf(m, " %03d: [%016llx|%03llx]\n", i, this, FULL_COUNT(ca->array[i]));
418011d8261SBorislav Petkov 
419011d8261SBorislav Petkov 		WARN_ON(prev > this);
420011d8261SBorislav Petkov 
421011d8261SBorislav Petkov 		prev = this;
422011d8261SBorislav Petkov 	}
423011d8261SBorislav Petkov 
424011d8261SBorislav Petkov 	seq_printf(m, "}\n");
425011d8261SBorislav Petkov 
426011d8261SBorislav Petkov 	seq_printf(m, "Stats:\nCEs: %llu\nofflined pages: %llu\n",
427011d8261SBorislav Petkov 		   ca->ces_entered, ca->pfns_poisoned);
428011d8261SBorislav Petkov 
429011d8261SBorislav Petkov 	seq_printf(m, "Flags: 0x%x\n", ca->flags);
430011d8261SBorislav Petkov 
431011d8261SBorislav Petkov 	seq_printf(m, "Timer interval: %lld seconds\n", timer_interval);
432011d8261SBorislav Petkov 	seq_printf(m, "Decays: %lld\n", ca->decays_done);
433011d8261SBorislav Petkov 
434011d8261SBorislav Petkov 	seq_printf(m, "Action threshold: %d\n", count_threshold);
435011d8261SBorislav Petkov 
436011d8261SBorislav Petkov 	mutex_unlock(&ce_mutex);
437011d8261SBorislav Petkov 
438011d8261SBorislav Petkov 	return 0;
439011d8261SBorislav Petkov }
440011d8261SBorislav Petkov 
441011d8261SBorislav Petkov static int array_open(struct inode *inode, struct file *filp)
442011d8261SBorislav Petkov {
443011d8261SBorislav Petkov 	return single_open(filp, array_dump, NULL);
444011d8261SBorislav Petkov }
445011d8261SBorislav Petkov 
446011d8261SBorislav Petkov static const struct file_operations array_ops = {
447011d8261SBorislav Petkov 	.owner	 = THIS_MODULE,
448011d8261SBorislav Petkov 	.open	 = array_open,
449011d8261SBorislav Petkov 	.read	 = seq_read,
450011d8261SBorislav Petkov 	.llseek	 = seq_lseek,
451011d8261SBorislav Petkov 	.release = single_release,
452011d8261SBorislav Petkov };
453011d8261SBorislav Petkov 
454011d8261SBorislav Petkov static int __init create_debugfs_nodes(void)
455011d8261SBorislav Petkov {
456011d8261SBorislav Petkov 	struct dentry *d, *pfn, *decay, *count, *array;
457011d8261SBorislav Petkov 
458011d8261SBorislav Petkov 	d = debugfs_create_dir("cec", ras_debugfs_dir);
459011d8261SBorislav Petkov 	if (!d) {
460011d8261SBorislav Petkov 		pr_warn("Error creating cec debugfs node!\n");
461011d8261SBorislav Petkov 		return -1;
462011d8261SBorislav Petkov 	}
463011d8261SBorislav Petkov 
464011d8261SBorislav Petkov 	pfn = debugfs_create_file("pfn", S_IRUSR | S_IWUSR, d, &dfs_pfn, &pfn_ops);
465011d8261SBorislav Petkov 	if (!pfn) {
466011d8261SBorislav Petkov 		pr_warn("Error creating pfn debugfs node!\n");
467011d8261SBorislav Petkov 		goto err;
468011d8261SBorislav Petkov 	}
469011d8261SBorislav Petkov 
470011d8261SBorislav Petkov 	array = debugfs_create_file("array", S_IRUSR, d, NULL, &array_ops);
471011d8261SBorislav Petkov 	if (!array) {
472011d8261SBorislav Petkov 		pr_warn("Error creating array debugfs node!\n");
473011d8261SBorislav Petkov 		goto err;
474011d8261SBorislav Petkov 	}
475011d8261SBorislav Petkov 
476011d8261SBorislav Petkov 	decay = debugfs_create_file("decay_interval", S_IRUSR | S_IWUSR, d,
477011d8261SBorislav Petkov 				    &timer_interval, &decay_interval_ops);
478011d8261SBorislav Petkov 	if (!decay) {
479011d8261SBorislav Petkov 		pr_warn("Error creating decay_interval debugfs node!\n");
480011d8261SBorislav Petkov 		goto err;
481011d8261SBorislav Petkov 	}
482011d8261SBorislav Petkov 
483011d8261SBorislav Petkov 	count = debugfs_create_file("count_threshold", S_IRUSR | S_IWUSR, d,
484011d8261SBorislav Petkov 				    &count_threshold, &count_threshold_ops);
48532288dafSChristophe JAILLET 	if (!count) {
486011d8261SBorislav Petkov 		pr_warn("Error creating count_threshold debugfs node!\n");
487011d8261SBorislav Petkov 		goto err;
488011d8261SBorislav Petkov 	}
489011d8261SBorislav Petkov 
490011d8261SBorislav Petkov 
491011d8261SBorislav Petkov 	return 0;
492011d8261SBorislav Petkov 
493011d8261SBorislav Petkov err:
494011d8261SBorislav Petkov 	debugfs_remove_recursive(d);
495011d8261SBorislav Petkov 
496011d8261SBorislav Petkov 	return 1;
497011d8261SBorislav Petkov }
498011d8261SBorislav Petkov 
499011d8261SBorislav Petkov void __init cec_init(void)
500011d8261SBorislav Petkov {
501011d8261SBorislav Petkov 	if (ce_arr.disabled)
502011d8261SBorislav Petkov 		return;
503011d8261SBorislav Petkov 
504011d8261SBorislav Petkov 	ce_arr.array = (void *)get_zeroed_page(GFP_KERNEL);
505011d8261SBorislav Petkov 	if (!ce_arr.array) {
506011d8261SBorislav Petkov 		pr_err("Error allocating CE array page!\n");
507011d8261SBorislav Petkov 		return;
508011d8261SBorislav Petkov 	}
509011d8261SBorislav Petkov 
510011d8261SBorislav Petkov 	if (create_debugfs_nodes())
511011d8261SBorislav Petkov 		return;
512011d8261SBorislav Petkov 
513011d8261SBorislav Petkov 	setup_timer(&cec_timer, cec_timer_fn, (unsigned long)&ce_arr);
514011d8261SBorislav Petkov 	cec_mod_timer(&cec_timer, CEC_TIMER_DEFAULT_INTERVAL);
515011d8261SBorislav Petkov 
516011d8261SBorislav Petkov 	pr_info("Correctable Errors collector initialized.\n");
517011d8261SBorislav Petkov }
518011d8261SBorislav Petkov 
519011d8261SBorislav Petkov int __init parse_cec_param(char *str)
520011d8261SBorislav Petkov {
521011d8261SBorislav Petkov 	if (!str)
522011d8261SBorislav Petkov 		return 0;
523011d8261SBorislav Petkov 
524011d8261SBorislav Petkov 	if (*str == '=')
525011d8261SBorislav Petkov 		str++;
526011d8261SBorislav Petkov 
52769a33000SNicolas Iooss 	if (!strcmp(str, "cec_disable"))
528011d8261SBorislav Petkov 		ce_arr.disabled = 1;
529011d8261SBorislav Petkov 	else
530011d8261SBorislav Petkov 		return 0;
531011d8261SBorislav Petkov 
532011d8261SBorislav Petkov 	return 1;
533011d8261SBorislav Petkov }
534