Commit | Line | Data |
---|---|---|
da9bb1d2 AC |
1 | /* |
2 | * edac_mc kernel module | |
49c0dab7 | 3 | * (C) 2005, 2006 Linux Networx (http://lnxi.com) |
da9bb1d2 AC |
4 | * This file may be distributed under the terms of the |
5 | * GNU General Public License. | |
6 | * | |
7 | * Written by Thayne Harbaugh | |
8 | * Based on work by Dan Hollis <goemon at anime dot net> and others. | |
9 | * http://www.anime.net/~goemon/linux-ecc/ | |
10 | * | |
11 | * Modified by Dave Peterson and Doug Thompson | |
12 | * | |
13 | */ | |
14 | ||
da9bb1d2 AC |
15 | #include <linux/module.h> |
16 | #include <linux/proc_fs.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/types.h> | |
19 | #include <linux/smp.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/sysctl.h> | |
22 | #include <linux/highmem.h> | |
23 | #include <linux/timer.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/jiffies.h> | |
26 | #include <linux/spinlock.h> | |
27 | #include <linux/list.h> | |
da9bb1d2 | 28 | #include <linux/ctype.h> |
c0d12172 | 29 | #include <linux/edac.h> |
53f2d028 | 30 | #include <linux/bitops.h> |
da9bb1d2 AC |
31 | #include <asm/uaccess.h> |
32 | #include <asm/page.h> | |
33 | #include <asm/edac.h> | |
20bcb7a8 | 34 | #include "edac_core.h" |
7c9281d7 | 35 | #include "edac_module.h" |
da9bb1d2 | 36 | |
53f2d028 MCC |
37 | #define CREATE_TRACE_POINTS |
38 | #define TRACE_INCLUDE_PATH ../../include/ras | |
39 | #include <ras/ras_event.h> | |
40 | ||
da9bb1d2 | 41 | /* lock to memory controller's control array */ |
63b7df91 | 42 | static DEFINE_MUTEX(mem_ctls_mutex); |
ff6ac2a6 | 43 | static LIST_HEAD(mc_devices); |
da9bb1d2 | 44 | |
6e84d359 MCC |
45 | unsigned edac_dimm_info_location(struct dimm_info *dimm, char *buf, |
46 | unsigned len) | |
47 | { | |
48 | struct mem_ctl_info *mci = dimm->mci; | |
49 | int i, n, count = 0; | |
50 | char *p = buf; | |
51 | ||
52 | for (i = 0; i < mci->n_layers; i++) { | |
53 | n = snprintf(p, len, "%s %d ", | |
54 | edac_layer_name[mci->layers[i].type], | |
55 | dimm->location[i]); | |
56 | p += n; | |
57 | len -= n; | |
58 | count += n; | |
59 | if (!len) | |
60 | break; | |
61 | } | |
62 | ||
63 | return count; | |
64 | } | |
65 | ||
da9bb1d2 AC |
66 | #ifdef CONFIG_EDAC_DEBUG |
67 | ||
a4b4be3f | 68 | static void edac_mc_dump_channel(struct rank_info *chan) |
da9bb1d2 | 69 | { |
6e84d359 MCC |
70 | edac_dbg(4, " channel->chan_idx = %d\n", chan->chan_idx); |
71 | edac_dbg(4, " channel = %p\n", chan); | |
72 | edac_dbg(4, " channel->csrow = %p\n", chan->csrow); | |
73 | edac_dbg(4, " channel->dimm = %p\n", chan->dimm); | |
4275be63 MCC |
74 | } |
75 | ||
6e84d359 | 76 | static void edac_mc_dump_dimm(struct dimm_info *dimm, int number) |
4275be63 | 77 | { |
6e84d359 MCC |
78 | char location[80]; |
79 | ||
80 | edac_dimm_info_location(dimm, location, sizeof(location)); | |
81 | ||
82 | edac_dbg(4, "%s%i: %smapped as virtual row %d, chan %d\n", | |
83 | dimm->mci->mem_is_per_rank ? "rank" : "dimm", | |
84 | number, location, dimm->csrow, dimm->cschannel); | |
85 | edac_dbg(4, " dimm = %p\n", dimm); | |
86 | edac_dbg(4, " dimm->label = '%s'\n", dimm->label); | |
87 | edac_dbg(4, " dimm->nr_pages = 0x%x\n", dimm->nr_pages); | |
88 | edac_dbg(4, " dimm->grain = %d\n", dimm->grain); | |
89 | edac_dbg(4, " dimm->nr_pages = 0x%x\n", dimm->nr_pages); | |
da9bb1d2 AC |
90 | } |
91 | ||
2da1c119 | 92 | static void edac_mc_dump_csrow(struct csrow_info *csrow) |
da9bb1d2 | 93 | { |
6e84d359 MCC |
94 | edac_dbg(4, "csrow->csrow_idx = %d\n", csrow->csrow_idx); |
95 | edac_dbg(4, " csrow = %p\n", csrow); | |
96 | edac_dbg(4, " csrow->first_page = 0x%lx\n", csrow->first_page); | |
97 | edac_dbg(4, " csrow->last_page = 0x%lx\n", csrow->last_page); | |
98 | edac_dbg(4, " csrow->page_mask = 0x%lx\n", csrow->page_mask); | |
99 | edac_dbg(4, " csrow->nr_channels = %d\n", csrow->nr_channels); | |
100 | edac_dbg(4, " csrow->channels = %p\n", csrow->channels); | |
101 | edac_dbg(4, " csrow->mci = %p\n", csrow->mci); | |
da9bb1d2 AC |
102 | } |
103 | ||
2da1c119 | 104 | static void edac_mc_dump_mci(struct mem_ctl_info *mci) |
da9bb1d2 | 105 | { |
956b9ba1 JP |
106 | edac_dbg(3, "\tmci = %p\n", mci); |
107 | edac_dbg(3, "\tmci->mtype_cap = %lx\n", mci->mtype_cap); | |
108 | edac_dbg(3, "\tmci->edac_ctl_cap = %lx\n", mci->edac_ctl_cap); | |
109 | edac_dbg(3, "\tmci->edac_cap = %lx\n", mci->edac_cap); | |
110 | edac_dbg(4, "\tmci->edac_check = %p\n", mci->edac_check); | |
111 | edac_dbg(3, "\tmci->nr_csrows = %d, csrows = %p\n", | |
112 | mci->nr_csrows, mci->csrows); | |
113 | edac_dbg(3, "\tmci->nr_dimms = %d, dimms = %p\n", | |
114 | mci->tot_dimms, mci->dimms); | |
115 | edac_dbg(3, "\tdev = %p\n", mci->pdev); | |
116 | edac_dbg(3, "\tmod_name:ctl_name = %s:%s\n", | |
117 | mci->mod_name, mci->ctl_name); | |
118 | edac_dbg(3, "\tpvt_info = %p\n\n", mci->pvt_info); | |
da9bb1d2 AC |
119 | } |
120 | ||
24f9a7fe BP |
121 | #endif /* CONFIG_EDAC_DEBUG */ |
122 | ||
239642fe BP |
123 | /* |
124 | * keep those in sync with the enum mem_type | |
125 | */ | |
126 | const char *edac_mem_types[] = { | |
127 | "Empty csrow", | |
128 | "Reserved csrow type", | |
129 | "Unknown csrow type", | |
130 | "Fast page mode RAM", | |
131 | "Extended data out RAM", | |
132 | "Burst Extended data out RAM", | |
133 | "Single data rate SDRAM", | |
134 | "Registered single data rate SDRAM", | |
135 | "Double data rate SDRAM", | |
136 | "Registered Double data rate SDRAM", | |
137 | "Rambus DRAM", | |
138 | "Unbuffered DDR2 RAM", | |
139 | "Fully buffered DDR2", | |
140 | "Registered DDR2 RAM", | |
141 | "Rambus XDR", | |
142 | "Unbuffered DDR3 RAM", | |
143 | "Registered DDR3 RAM", | |
144 | }; | |
145 | EXPORT_SYMBOL_GPL(edac_mem_types); | |
146 | ||
93e4fe64 MCC |
147 | /** |
148 | * edac_align_ptr - Prepares the pointer offsets for a single-shot allocation | |
149 | * @p: pointer to a pointer with the memory offset to be used. At | |
150 | * return, this will be incremented to point to the next offset | |
151 | * @size: Size of the data structure to be reserved | |
152 | * @n_elems: Number of elements that should be reserved | |
da9bb1d2 AC |
153 | * |
154 | * If 'size' is a constant, the compiler will optimize this whole function | |
93e4fe64 MCC |
155 | * down to either a no-op or the addition of a constant to the value of '*p'. |
156 | * | |
157 | * The 'p' pointer is absolutely needed to keep the proper advancing | |
158 | * further in memory to the proper offsets when allocating the struct along | |
159 | * with its embedded structs, as edac_device_alloc_ctl_info() does it | |
160 | * above, for example. | |
161 | * | |
162 | * At return, the pointer 'p' will be incremented to be used on a next call | |
163 | * to this function. | |
da9bb1d2 | 164 | */ |
93e4fe64 | 165 | void *edac_align_ptr(void **p, unsigned size, int n_elems) |
da9bb1d2 AC |
166 | { |
167 | unsigned align, r; | |
93e4fe64 | 168 | void *ptr = *p; |
da9bb1d2 | 169 | |
93e4fe64 MCC |
170 | *p += size * n_elems; |
171 | ||
172 | /* | |
173 | * 'p' can possibly be an unaligned item X such that sizeof(X) is | |
174 | * 'size'. Adjust 'p' so that its alignment is at least as | |
175 | * stringent as what the compiler would provide for X and return | |
176 | * the aligned result. | |
177 | * Here we assume that the alignment of a "long long" is the most | |
da9bb1d2 AC |
178 | * stringent alignment that the compiler will ever provide by default. |
179 | * As far as I know, this is a reasonable assumption. | |
180 | */ | |
181 | if (size > sizeof(long)) | |
182 | align = sizeof(long long); | |
183 | else if (size > sizeof(int)) | |
184 | align = sizeof(long); | |
185 | else if (size > sizeof(short)) | |
186 | align = sizeof(int); | |
187 | else if (size > sizeof(char)) | |
188 | align = sizeof(short); | |
189 | else | |
079708b9 | 190 | return (char *)ptr; |
da9bb1d2 | 191 | |
8447c4d1 | 192 | r = (unsigned long)p % align; |
da9bb1d2 AC |
193 | |
194 | if (r == 0) | |
079708b9 | 195 | return (char *)ptr; |
da9bb1d2 | 196 | |
93e4fe64 MCC |
197 | *p += align - r; |
198 | ||
7391c6dc | 199 | return (void *)(((unsigned long)ptr) + align - r); |
da9bb1d2 AC |
200 | } |
201 | ||
da9bb1d2 | 202 | /** |
4275be63 MCC |
203 | * edac_mc_alloc: Allocate and partially fill a struct mem_ctl_info structure |
204 | * @mc_num: Memory controller number | |
205 | * @n_layers: Number of MC hierarchy layers | |
206 | * layers: Describes each layer as seen by the Memory Controller | |
207 | * @size_pvt: size of private storage needed | |
208 | * | |
da9bb1d2 AC |
209 | * |
210 | * Everything is kmalloc'ed as one big chunk - more efficient. | |
211 | * Only can be used if all structures have the same lifetime - otherwise | |
212 | * you have to allocate and initialize your own structures. | |
213 | * | |
214 | * Use edac_mc_free() to free mc structures allocated by this function. | |
215 | * | |
4275be63 MCC |
216 | * NOTE: drivers handle multi-rank memories in different ways: in some |
217 | * drivers, one multi-rank memory stick is mapped as one entry, while, in | |
218 | * others, a single multi-rank memory stick would be mapped into several | |
219 | * entries. Currently, this function will allocate multiple struct dimm_info | |
220 | * on such scenarios, as grouping the multiple ranks require drivers change. | |
221 | * | |
da9bb1d2 | 222 | * Returns: |
ca0907b9 MCC |
223 | * On failure: NULL |
224 | * On success: struct mem_ctl_info pointer | |
da9bb1d2 | 225 | */ |
ca0907b9 MCC |
226 | struct mem_ctl_info *edac_mc_alloc(unsigned mc_num, |
227 | unsigned n_layers, | |
228 | struct edac_mc_layer *layers, | |
229 | unsigned sz_pvt) | |
da9bb1d2 AC |
230 | { |
231 | struct mem_ctl_info *mci; | |
4275be63 | 232 | struct edac_mc_layer *layer; |
de3910eb MCC |
233 | struct csrow_info *csr; |
234 | struct rank_info *chan; | |
a7d7d2e1 | 235 | struct dimm_info *dimm; |
4275be63 MCC |
236 | u32 *ce_per_layer[EDAC_MAX_LAYERS], *ue_per_layer[EDAC_MAX_LAYERS]; |
237 | unsigned pos[EDAC_MAX_LAYERS]; | |
4275be63 MCC |
238 | unsigned size, tot_dimms = 1, count = 1; |
239 | unsigned tot_csrows = 1, tot_channels = 1, tot_errcount = 0; | |
5926ff50 | 240 | void *pvt, *p, *ptr = NULL; |
de3910eb | 241 | int i, j, row, chn, n, len, off; |
4275be63 MCC |
242 | bool per_rank = false; |
243 | ||
244 | BUG_ON(n_layers > EDAC_MAX_LAYERS || n_layers == 0); | |
245 | /* | |
246 | * Calculate the total amount of dimms and csrows/cschannels while | |
247 | * in the old API emulation mode | |
248 | */ | |
249 | for (i = 0; i < n_layers; i++) { | |
250 | tot_dimms *= layers[i].size; | |
251 | if (layers[i].is_virt_csrow) | |
252 | tot_csrows *= layers[i].size; | |
253 | else | |
254 | tot_channels *= layers[i].size; | |
255 | ||
256 | if (layers[i].type == EDAC_MC_LAYER_CHIP_SELECT) | |
257 | per_rank = true; | |
258 | } | |
da9bb1d2 AC |
259 | |
260 | /* Figure out the offsets of the various items from the start of an mc | |
261 | * structure. We want the alignment of each item to be at least as | |
262 | * stringent as what the compiler would provide if we could simply | |
263 | * hardcode everything into a single struct. | |
264 | */ | |
93e4fe64 | 265 | mci = edac_align_ptr(&ptr, sizeof(*mci), 1); |
4275be63 | 266 | layer = edac_align_ptr(&ptr, sizeof(*layer), n_layers); |
4275be63 MCC |
267 | for (i = 0; i < n_layers; i++) { |
268 | count *= layers[i].size; | |
956b9ba1 | 269 | edac_dbg(4, "errcount layer %d size %d\n", i, count); |
4275be63 MCC |
270 | ce_per_layer[i] = edac_align_ptr(&ptr, sizeof(u32), count); |
271 | ue_per_layer[i] = edac_align_ptr(&ptr, sizeof(u32), count); | |
272 | tot_errcount += 2 * count; | |
273 | } | |
274 | ||
956b9ba1 | 275 | edac_dbg(4, "allocating %d error counters\n", tot_errcount); |
93e4fe64 | 276 | pvt = edac_align_ptr(&ptr, sz_pvt, 1); |
079708b9 | 277 | size = ((unsigned long)pvt) + sz_pvt; |
da9bb1d2 | 278 | |
956b9ba1 JP |
279 | edac_dbg(1, "allocating %u bytes for mci data (%d %s, %d csrows/channels)\n", |
280 | size, | |
281 | tot_dimms, | |
282 | per_rank ? "ranks" : "dimms", | |
283 | tot_csrows * tot_channels); | |
de3910eb | 284 | |
8096cfaf DT |
285 | mci = kzalloc(size, GFP_KERNEL); |
286 | if (mci == NULL) | |
da9bb1d2 AC |
287 | return NULL; |
288 | ||
289 | /* Adjust pointers so they point within the memory we just allocated | |
290 | * rather than an imaginary chunk of memory located at address 0. | |
291 | */ | |
4275be63 | 292 | layer = (struct edac_mc_layer *)(((char *)mci) + ((unsigned long)layer)); |
4275be63 MCC |
293 | for (i = 0; i < n_layers; i++) { |
294 | mci->ce_per_layer[i] = (u32 *)((char *)mci + ((unsigned long)ce_per_layer[i])); | |
295 | mci->ue_per_layer[i] = (u32 *)((char *)mci + ((unsigned long)ue_per_layer[i])); | |
296 | } | |
079708b9 | 297 | pvt = sz_pvt ? (((char *)mci) + ((unsigned long)pvt)) : NULL; |
da9bb1d2 | 298 | |
b8f6f975 | 299 | /* setup index and various internal pointers */ |
4275be63 | 300 | mci->mc_idx = mc_num; |
4275be63 | 301 | mci->tot_dimms = tot_dimms; |
da9bb1d2 | 302 | mci->pvt_info = pvt; |
4275be63 MCC |
303 | mci->n_layers = n_layers; |
304 | mci->layers = layer; | |
305 | memcpy(mci->layers, layers, sizeof(*layer) * n_layers); | |
306 | mci->nr_csrows = tot_csrows; | |
307 | mci->num_cschannel = tot_channels; | |
308 | mci->mem_is_per_rank = per_rank; | |
da9bb1d2 | 309 | |
a7d7d2e1 | 310 | /* |
de3910eb | 311 | * Alocate and fill the csrow/channels structs |
a7d7d2e1 | 312 | */ |
de3910eb MCC |
313 | mci->csrows = kcalloc(sizeof(*mci->csrows), tot_csrows, GFP_KERNEL); |
314 | if (!mci->csrows) | |
315 | goto error; | |
4275be63 | 316 | for (row = 0; row < tot_csrows; row++) { |
de3910eb MCC |
317 | csr = kzalloc(sizeof(**mci->csrows), GFP_KERNEL); |
318 | if (!csr) | |
319 | goto error; | |
320 | mci->csrows[row] = csr; | |
4275be63 MCC |
321 | csr->csrow_idx = row; |
322 | csr->mci = mci; | |
323 | csr->nr_channels = tot_channels; | |
de3910eb MCC |
324 | csr->channels = kcalloc(sizeof(*csr->channels), tot_channels, |
325 | GFP_KERNEL); | |
326 | if (!csr->channels) | |
327 | goto error; | |
4275be63 MCC |
328 | |
329 | for (chn = 0; chn < tot_channels; chn++) { | |
de3910eb MCC |
330 | chan = kzalloc(sizeof(**csr->channels), GFP_KERNEL); |
331 | if (!chan) | |
332 | goto error; | |
333 | csr->channels[chn] = chan; | |
da9bb1d2 | 334 | chan->chan_idx = chn; |
4275be63 MCC |
335 | chan->csrow = csr; |
336 | } | |
337 | } | |
338 | ||
339 | /* | |
de3910eb | 340 | * Allocate and fill the dimm structs |
4275be63 | 341 | */ |
de3910eb MCC |
342 | mci->dimms = kcalloc(sizeof(*mci->dimms), tot_dimms, GFP_KERNEL); |
343 | if (!mci->dimms) | |
344 | goto error; | |
345 | ||
4275be63 MCC |
346 | memset(&pos, 0, sizeof(pos)); |
347 | row = 0; | |
348 | chn = 0; | |
4275be63 | 349 | for (i = 0; i < tot_dimms; i++) { |
de3910eb MCC |
350 | chan = mci->csrows[row]->channels[chn]; |
351 | off = EDAC_DIMM_OFF(layer, n_layers, pos[0], pos[1], pos[2]); | |
352 | if (off < 0 || off >= tot_dimms) { | |
353 | edac_mc_printk(mci, KERN_ERR, "EDAC core bug: EDAC_DIMM_OFF is trying to do an illegal data access\n"); | |
354 | goto error; | |
355 | } | |
4275be63 | 356 | |
de3910eb | 357 | dimm = kzalloc(sizeof(**mci->dimms), GFP_KERNEL); |
08a4a136 DC |
358 | if (!dimm) |
359 | goto error; | |
de3910eb | 360 | mci->dimms[off] = dimm; |
4275be63 | 361 | dimm->mci = mci; |
4275be63 | 362 | |
5926ff50 MCC |
363 | /* |
364 | * Copy DIMM location and initialize it. | |
365 | */ | |
366 | len = sizeof(dimm->label); | |
367 | p = dimm->label; | |
368 | n = snprintf(p, len, "mc#%u", mc_num); | |
369 | p += n; | |
370 | len -= n; | |
371 | for (j = 0; j < n_layers; j++) { | |
372 | n = snprintf(p, len, "%s#%u", | |
373 | edac_layer_name[layers[j].type], | |
374 | pos[j]); | |
375 | p += n; | |
376 | len -= n; | |
4275be63 MCC |
377 | dimm->location[j] = pos[j]; |
378 | ||
5926ff50 MCC |
379 | if (len <= 0) |
380 | break; | |
381 | } | |
382 | ||
4275be63 MCC |
383 | /* Link it to the csrows old API data */ |
384 | chan->dimm = dimm; | |
385 | dimm->csrow = row; | |
386 | dimm->cschannel = chn; | |
387 | ||
388 | /* Increment csrow location */ | |
389 | row++; | |
390 | if (row == tot_csrows) { | |
391 | row = 0; | |
392 | chn++; | |
393 | } | |
a7d7d2e1 | 394 | |
4275be63 MCC |
395 | /* Increment dimm location */ |
396 | for (j = n_layers - 1; j >= 0; j--) { | |
397 | pos[j]++; | |
398 | if (pos[j] < layers[j].size) | |
399 | break; | |
400 | pos[j] = 0; | |
da9bb1d2 AC |
401 | } |
402 | } | |
403 | ||
81d87cb1 | 404 | mci->op_state = OP_ALLOC; |
8096cfaf DT |
405 | |
406 | /* at this point, the root kobj is valid, and in order to | |
407 | * 'free' the object, then the function: | |
408 | * edac_mc_unregister_sysfs_main_kobj() must be called | |
409 | * which will perform kobj unregistration and the actual free | |
410 | * will occur during the kobject callback operation | |
411 | */ | |
53f2d028 | 412 | |
da9bb1d2 | 413 | return mci; |
de3910eb MCC |
414 | |
415 | error: | |
416 | if (mci->dimms) { | |
417 | for (i = 0; i < tot_dimms; i++) | |
418 | kfree(mci->dimms[i]); | |
419 | kfree(mci->dimms); | |
420 | } | |
421 | if (mci->csrows) { | |
422 | for (chn = 0; chn < tot_channels; chn++) { | |
423 | csr = mci->csrows[chn]; | |
424 | if (csr) { | |
425 | for (chn = 0; chn < tot_channels; chn++) | |
426 | kfree(csr->channels[chn]); | |
427 | kfree(csr); | |
428 | } | |
429 | kfree(mci->csrows[i]); | |
430 | } | |
431 | kfree(mci->csrows); | |
432 | } | |
433 | kfree(mci); | |
434 | ||
435 | return NULL; | |
4275be63 | 436 | } |
9110540f | 437 | EXPORT_SYMBOL_GPL(edac_mc_alloc); |
da9bb1d2 | 438 | |
da9bb1d2 | 439 | /** |
8096cfaf DT |
440 | * edac_mc_free |
441 | * 'Free' a previously allocated 'mci' structure | |
da9bb1d2 | 442 | * @mci: pointer to a struct mem_ctl_info structure |
da9bb1d2 AC |
443 | */ |
444 | void edac_mc_free(struct mem_ctl_info *mci) | |
445 | { | |
956b9ba1 | 446 | edac_dbg(1, "\n"); |
bbc560ae | 447 | |
de3910eb | 448 | /* the mci instance is freed here, when the sysfs object is dropped */ |
7a623c03 | 449 | edac_unregister_sysfs(mci); |
da9bb1d2 | 450 | } |
9110540f | 451 | EXPORT_SYMBOL_GPL(edac_mc_free); |
da9bb1d2 | 452 | |
bce19683 | 453 | |
939747bd | 454 | /** |
bce19683 DT |
455 | * find_mci_by_dev |
456 | * | |
457 | * scan list of controllers looking for the one that manages | |
458 | * the 'dev' device | |
939747bd | 459 | * @dev: pointer to a struct device related with the MCI |
bce19683 | 460 | */ |
939747bd | 461 | struct mem_ctl_info *find_mci_by_dev(struct device *dev) |
da9bb1d2 AC |
462 | { |
463 | struct mem_ctl_info *mci; | |
464 | struct list_head *item; | |
465 | ||
956b9ba1 | 466 | edac_dbg(3, "\n"); |
da9bb1d2 AC |
467 | |
468 | list_for_each(item, &mc_devices) { | |
469 | mci = list_entry(item, struct mem_ctl_info, link); | |
470 | ||
fd687502 | 471 | if (mci->pdev == dev) |
da9bb1d2 AC |
472 | return mci; |
473 | } | |
474 | ||
475 | return NULL; | |
476 | } | |
939747bd | 477 | EXPORT_SYMBOL_GPL(find_mci_by_dev); |
da9bb1d2 | 478 | |
81d87cb1 DJ |
479 | /* |
480 | * handler for EDAC to check if NMI type handler has asserted interrupt | |
481 | */ | |
482 | static int edac_mc_assert_error_check_and_clear(void) | |
483 | { | |
66ee2f94 | 484 | int old_state; |
81d87cb1 | 485 | |
079708b9 | 486 | if (edac_op_state == EDAC_OPSTATE_POLL) |
81d87cb1 DJ |
487 | return 1; |
488 | ||
66ee2f94 DJ |
489 | old_state = edac_err_assert; |
490 | edac_err_assert = 0; | |
81d87cb1 | 491 | |
66ee2f94 | 492 | return old_state; |
81d87cb1 DJ |
493 | } |
494 | ||
495 | /* | |
496 | * edac_mc_workq_function | |
497 | * performs the operation scheduled by a workq request | |
498 | */ | |
81d87cb1 DJ |
499 | static void edac_mc_workq_function(struct work_struct *work_req) |
500 | { | |
fbeb4384 | 501 | struct delayed_work *d_work = to_delayed_work(work_req); |
81d87cb1 | 502 | struct mem_ctl_info *mci = to_edac_mem_ctl_work(d_work); |
81d87cb1 DJ |
503 | |
504 | mutex_lock(&mem_ctls_mutex); | |
505 | ||
bf52fa4a DT |
506 | /* if this control struct has movd to offline state, we are done */ |
507 | if (mci->op_state == OP_OFFLINE) { | |
508 | mutex_unlock(&mem_ctls_mutex); | |
509 | return; | |
510 | } | |
511 | ||
81d87cb1 DJ |
512 | /* Only poll controllers that are running polled and have a check */ |
513 | if (edac_mc_assert_error_check_and_clear() && (mci->edac_check != NULL)) | |
514 | mci->edac_check(mci); | |
515 | ||
81d87cb1 DJ |
516 | mutex_unlock(&mem_ctls_mutex); |
517 | ||
518 | /* Reschedule */ | |
4de78c68 | 519 | queue_delayed_work(edac_workqueue, &mci->work, |
052dfb45 | 520 | msecs_to_jiffies(edac_mc_get_poll_msec())); |
81d87cb1 DJ |
521 | } |
522 | ||
523 | /* | |
524 | * edac_mc_workq_setup | |
525 | * initialize a workq item for this mci | |
526 | * passing in the new delay period in msec | |
bf52fa4a DT |
527 | * |
528 | * locking model: | |
529 | * | |
530 | * called with the mem_ctls_mutex held | |
81d87cb1 | 531 | */ |
bf52fa4a | 532 | static void edac_mc_workq_setup(struct mem_ctl_info *mci, unsigned msec) |
81d87cb1 | 533 | { |
956b9ba1 | 534 | edac_dbg(0, "\n"); |
81d87cb1 | 535 | |
bf52fa4a DT |
536 | /* if this instance is not in the POLL state, then simply return */ |
537 | if (mci->op_state != OP_RUNNING_POLL) | |
538 | return; | |
539 | ||
81d87cb1 | 540 | INIT_DELAYED_WORK(&mci->work, edac_mc_workq_function); |
81d87cb1 DJ |
541 | queue_delayed_work(edac_workqueue, &mci->work, msecs_to_jiffies(msec)); |
542 | } | |
543 | ||
544 | /* | |
545 | * edac_mc_workq_teardown | |
546 | * stop the workq processing on this mci | |
bf52fa4a DT |
547 | * |
548 | * locking model: | |
549 | * | |
550 | * called WITHOUT lock held | |
81d87cb1 | 551 | */ |
bf52fa4a | 552 | static void edac_mc_workq_teardown(struct mem_ctl_info *mci) |
81d87cb1 DJ |
553 | { |
554 | int status; | |
555 | ||
00740c58 BP |
556 | if (mci->op_state != OP_RUNNING_POLL) |
557 | return; | |
558 | ||
bce19683 DT |
559 | status = cancel_delayed_work(&mci->work); |
560 | if (status == 0) { | |
956b9ba1 | 561 | edac_dbg(0, "not canceled, flush the queue\n"); |
bf52fa4a | 562 | |
bce19683 DT |
563 | /* workq instance might be running, wait for it */ |
564 | flush_workqueue(edac_workqueue); | |
81d87cb1 DJ |
565 | } |
566 | } | |
567 | ||
568 | /* | |
bce19683 DT |
569 | * edac_mc_reset_delay_period(unsigned long value) |
570 | * | |
571 | * user space has updated our poll period value, need to | |
572 | * reset our workq delays | |
81d87cb1 | 573 | */ |
bce19683 | 574 | void edac_mc_reset_delay_period(int value) |
81d87cb1 | 575 | { |
bce19683 DT |
576 | struct mem_ctl_info *mci; |
577 | struct list_head *item; | |
578 | ||
579 | mutex_lock(&mem_ctls_mutex); | |
580 | ||
581 | /* scan the list and turn off all workq timers, doing so under lock | |
582 | */ | |
583 | list_for_each(item, &mc_devices) { | |
584 | mci = list_entry(item, struct mem_ctl_info, link); | |
585 | ||
586 | if (mci->op_state == OP_RUNNING_POLL) | |
587 | cancel_delayed_work(&mci->work); | |
588 | } | |
589 | ||
590 | mutex_unlock(&mem_ctls_mutex); | |
81d87cb1 | 591 | |
bce19683 DT |
592 | |
593 | /* re-walk the list, and reset the poll delay */ | |
bf52fa4a DT |
594 | mutex_lock(&mem_ctls_mutex); |
595 | ||
bce19683 DT |
596 | list_for_each(item, &mc_devices) { |
597 | mci = list_entry(item, struct mem_ctl_info, link); | |
598 | ||
599 | edac_mc_workq_setup(mci, (unsigned long) value); | |
600 | } | |
81d87cb1 DJ |
601 | |
602 | mutex_unlock(&mem_ctls_mutex); | |
603 | } | |
604 | ||
bce19683 DT |
605 | |
606 | ||
2d7bbb91 DT |
607 | /* Return 0 on success, 1 on failure. |
608 | * Before calling this function, caller must | |
609 | * assign a unique value to mci->mc_idx. | |
bf52fa4a DT |
610 | * |
611 | * locking model: | |
612 | * | |
613 | * called with the mem_ctls_mutex lock held | |
2d7bbb91 | 614 | */ |
079708b9 | 615 | static int add_mc_to_global_list(struct mem_ctl_info *mci) |
da9bb1d2 AC |
616 | { |
617 | struct list_head *item, *insert_before; | |
618 | struct mem_ctl_info *p; | |
da9bb1d2 | 619 | |
2d7bbb91 | 620 | insert_before = &mc_devices; |
da9bb1d2 | 621 | |
fd687502 | 622 | p = find_mci_by_dev(mci->pdev); |
bf52fa4a | 623 | if (unlikely(p != NULL)) |
2d7bbb91 | 624 | goto fail0; |
da9bb1d2 | 625 | |
2d7bbb91 DT |
626 | list_for_each(item, &mc_devices) { |
627 | p = list_entry(item, struct mem_ctl_info, link); | |
da9bb1d2 | 628 | |
2d7bbb91 DT |
629 | if (p->mc_idx >= mci->mc_idx) { |
630 | if (unlikely(p->mc_idx == mci->mc_idx)) | |
631 | goto fail1; | |
da9bb1d2 | 632 | |
2d7bbb91 DT |
633 | insert_before = item; |
634 | break; | |
da9bb1d2 | 635 | } |
da9bb1d2 AC |
636 | } |
637 | ||
638 | list_add_tail_rcu(&mci->link, insert_before); | |
c0d12172 | 639 | atomic_inc(&edac_handlers); |
da9bb1d2 | 640 | return 0; |
2d7bbb91 | 641 | |
052dfb45 | 642 | fail0: |
2d7bbb91 | 643 | edac_printk(KERN_WARNING, EDAC_MC, |
fd687502 | 644 | "%s (%s) %s %s already assigned %d\n", dev_name(p->pdev), |
17aa7e03 | 645 | edac_dev_name(mci), p->mod_name, p->ctl_name, p->mc_idx); |
2d7bbb91 DT |
646 | return 1; |
647 | ||
052dfb45 | 648 | fail1: |
2d7bbb91 | 649 | edac_printk(KERN_WARNING, EDAC_MC, |
052dfb45 DT |
650 | "bug in low-level driver: attempt to assign\n" |
651 | " duplicate mc_idx %d in %s()\n", p->mc_idx, __func__); | |
2d7bbb91 | 652 | return 1; |
da9bb1d2 AC |
653 | } |
654 | ||
e7ecd891 | 655 | static void del_mc_from_global_list(struct mem_ctl_info *mci) |
a1d03fcc | 656 | { |
c0d12172 | 657 | atomic_dec(&edac_handlers); |
a1d03fcc | 658 | list_del_rcu(&mci->link); |
e2e77098 LJ |
659 | |
660 | /* these are for safe removal of devices from global list while | |
661 | * NMI handlers may be traversing list | |
662 | */ | |
663 | synchronize_rcu(); | |
664 | INIT_LIST_HEAD(&mci->link); | |
a1d03fcc DP |
665 | } |
666 | ||
5da0831c DT |
667 | /** |
668 | * edac_mc_find: Search for a mem_ctl_info structure whose index is 'idx'. | |
669 | * | |
670 | * If found, return a pointer to the structure. | |
671 | * Else return NULL. | |
672 | * | |
673 | * Caller must hold mem_ctls_mutex. | |
674 | */ | |
079708b9 | 675 | struct mem_ctl_info *edac_mc_find(int idx) |
5da0831c DT |
676 | { |
677 | struct list_head *item; | |
678 | struct mem_ctl_info *mci; | |
679 | ||
680 | list_for_each(item, &mc_devices) { | |
681 | mci = list_entry(item, struct mem_ctl_info, link); | |
682 | ||
683 | if (mci->mc_idx >= idx) { | |
684 | if (mci->mc_idx == idx) | |
685 | return mci; | |
686 | ||
687 | break; | |
688 | } | |
689 | } | |
690 | ||
691 | return NULL; | |
692 | } | |
693 | EXPORT_SYMBOL(edac_mc_find); | |
694 | ||
da9bb1d2 | 695 | /** |
472678eb DP |
696 | * edac_mc_add_mc: Insert the 'mci' structure into the mci global list and |
697 | * create sysfs entries associated with mci structure | |
da9bb1d2 AC |
698 | * @mci: pointer to the mci structure to be added to the list |
699 | * | |
700 | * Return: | |
701 | * 0 Success | |
702 | * !0 Failure | |
703 | */ | |
704 | ||
705 | /* FIXME - should a warning be printed if no error detection? correction? */ | |
b8f6f975 | 706 | int edac_mc_add_mc(struct mem_ctl_info *mci) |
da9bb1d2 | 707 | { |
956b9ba1 | 708 | edac_dbg(0, "\n"); |
b8f6f975 | 709 | |
da9bb1d2 AC |
710 | #ifdef CONFIG_EDAC_DEBUG |
711 | if (edac_debug_level >= 3) | |
712 | edac_mc_dump_mci(mci); | |
e7ecd891 | 713 | |
da9bb1d2 AC |
714 | if (edac_debug_level >= 4) { |
715 | int i; | |
716 | ||
717 | for (i = 0; i < mci->nr_csrows; i++) { | |
6e84d359 MCC |
718 | struct csrow_info *csrow = mci->csrows[i]; |
719 | u32 nr_pages = 0; | |
da9bb1d2 | 720 | int j; |
e7ecd891 | 721 | |
6e84d359 MCC |
722 | for (j = 0; j < csrow->nr_channels; j++) |
723 | nr_pages += csrow->channels[j]->dimm->nr_pages; | |
724 | if (!nr_pages) | |
725 | continue; | |
726 | edac_mc_dump_csrow(csrow); | |
727 | for (j = 0; j < csrow->nr_channels; j++) | |
728 | if (csrow->channels[j]->dimm->nr_pages) | |
729 | edac_mc_dump_channel(csrow->channels[j]); | |
da9bb1d2 | 730 | } |
4275be63 | 731 | for (i = 0; i < mci->tot_dimms; i++) |
6e84d359 MCC |
732 | if (mci->dimms[i]->nr_pages) |
733 | edac_mc_dump_dimm(mci->dimms[i], i); | |
da9bb1d2 AC |
734 | } |
735 | #endif | |
63b7df91 | 736 | mutex_lock(&mem_ctls_mutex); |
da9bb1d2 AC |
737 | |
738 | if (add_mc_to_global_list(mci)) | |
028a7b6d | 739 | goto fail0; |
da9bb1d2 AC |
740 | |
741 | /* set load time so that error rate can be tracked */ | |
742 | mci->start_time = jiffies; | |
743 | ||
9794f33d | 744 | if (edac_create_sysfs_mci_device(mci)) { |
745 | edac_mc_printk(mci, KERN_WARNING, | |
052dfb45 | 746 | "failed to create sysfs device\n"); |
9794f33d | 747 | goto fail1; |
748 | } | |
da9bb1d2 | 749 | |
81d87cb1 DJ |
750 | /* If there IS a check routine, then we are running POLLED */ |
751 | if (mci->edac_check != NULL) { | |
752 | /* This instance is NOW RUNNING */ | |
753 | mci->op_state = OP_RUNNING_POLL; | |
754 | ||
755 | edac_mc_workq_setup(mci, edac_mc_get_poll_msec()); | |
756 | } else { | |
757 | mci->op_state = OP_RUNNING_INTERRUPT; | |
758 | } | |
759 | ||
da9bb1d2 | 760 | /* Report action taken */ |
bf52fa4a | 761 | edac_mc_printk(mci, KERN_INFO, "Giving out device to '%s' '%s':" |
17aa7e03 | 762 | " DEV %s\n", mci->mod_name, mci->ctl_name, edac_dev_name(mci)); |
da9bb1d2 | 763 | |
63b7df91 | 764 | mutex_unlock(&mem_ctls_mutex); |
028a7b6d | 765 | return 0; |
da9bb1d2 | 766 | |
052dfb45 | 767 | fail1: |
028a7b6d DP |
768 | del_mc_from_global_list(mci); |
769 | ||
052dfb45 | 770 | fail0: |
63b7df91 | 771 | mutex_unlock(&mem_ctls_mutex); |
028a7b6d | 772 | return 1; |
da9bb1d2 | 773 | } |
9110540f | 774 | EXPORT_SYMBOL_GPL(edac_mc_add_mc); |
da9bb1d2 | 775 | |
da9bb1d2 | 776 | /** |
472678eb DP |
777 | * edac_mc_del_mc: Remove sysfs entries for specified mci structure and |
778 | * remove mci structure from global list | |
37f04581 | 779 | * @pdev: Pointer to 'struct device' representing mci structure to remove. |
da9bb1d2 | 780 | * |
18dbc337 | 781 | * Return pointer to removed mci structure, or NULL if device not found. |
da9bb1d2 | 782 | */ |
079708b9 | 783 | struct mem_ctl_info *edac_mc_del_mc(struct device *dev) |
da9bb1d2 | 784 | { |
18dbc337 | 785 | struct mem_ctl_info *mci; |
da9bb1d2 | 786 | |
956b9ba1 | 787 | edac_dbg(0, "\n"); |
bf52fa4a | 788 | |
63b7df91 | 789 | mutex_lock(&mem_ctls_mutex); |
18dbc337 | 790 | |
bf52fa4a DT |
791 | /* find the requested mci struct in the global list */ |
792 | mci = find_mci_by_dev(dev); | |
793 | if (mci == NULL) { | |
63b7df91 | 794 | mutex_unlock(&mem_ctls_mutex); |
18dbc337 DP |
795 | return NULL; |
796 | } | |
797 | ||
da9bb1d2 | 798 | del_mc_from_global_list(mci); |
63b7df91 | 799 | mutex_unlock(&mem_ctls_mutex); |
bf52fa4a | 800 | |
bb31b312 | 801 | /* flush workq processes */ |
bf52fa4a | 802 | edac_mc_workq_teardown(mci); |
bb31b312 BP |
803 | |
804 | /* marking MCI offline */ | |
805 | mci->op_state = OP_OFFLINE; | |
806 | ||
807 | /* remove from sysfs */ | |
bf52fa4a DT |
808 | edac_remove_sysfs_mci_device(mci); |
809 | ||
537fba28 | 810 | edac_printk(KERN_INFO, EDAC_MC, |
052dfb45 | 811 | "Removed device %d for %s %s: DEV %s\n", mci->mc_idx, |
17aa7e03 | 812 | mci->mod_name, mci->ctl_name, edac_dev_name(mci)); |
bf52fa4a | 813 | |
18dbc337 | 814 | return mci; |
da9bb1d2 | 815 | } |
9110540f | 816 | EXPORT_SYMBOL_GPL(edac_mc_del_mc); |
da9bb1d2 | 817 | |
2da1c119 AB |
818 | static void edac_mc_scrub_block(unsigned long page, unsigned long offset, |
819 | u32 size) | |
da9bb1d2 AC |
820 | { |
821 | struct page *pg; | |
822 | void *virt_addr; | |
823 | unsigned long flags = 0; | |
824 | ||
956b9ba1 | 825 | edac_dbg(3, "\n"); |
da9bb1d2 AC |
826 | |
827 | /* ECC error page was not in our memory. Ignore it. */ | |
079708b9 | 828 | if (!pfn_valid(page)) |
da9bb1d2 AC |
829 | return; |
830 | ||
831 | /* Find the actual page structure then map it and fix */ | |
832 | pg = pfn_to_page(page); | |
833 | ||
834 | if (PageHighMem(pg)) | |
835 | local_irq_save(flags); | |
836 | ||
4e5df7ca | 837 | virt_addr = kmap_atomic(pg); |
da9bb1d2 AC |
838 | |
839 | /* Perform architecture specific atomic scrub operation */ | |
840 | atomic_scrub(virt_addr + offset, size); | |
841 | ||
842 | /* Unmap and complete */ | |
4e5df7ca | 843 | kunmap_atomic(virt_addr); |
da9bb1d2 AC |
844 | |
845 | if (PageHighMem(pg)) | |
846 | local_irq_restore(flags); | |
847 | } | |
848 | ||
da9bb1d2 | 849 | /* FIXME - should return -1 */ |
e7ecd891 | 850 | int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci, unsigned long page) |
da9bb1d2 | 851 | { |
de3910eb | 852 | struct csrow_info **csrows = mci->csrows; |
a895bf8b | 853 | int row, i, j, n; |
da9bb1d2 | 854 | |
956b9ba1 | 855 | edac_dbg(1, "MC%d: 0x%lx\n", mci->mc_idx, page); |
da9bb1d2 AC |
856 | row = -1; |
857 | ||
858 | for (i = 0; i < mci->nr_csrows; i++) { | |
de3910eb | 859 | struct csrow_info *csrow = csrows[i]; |
a895bf8b MCC |
860 | n = 0; |
861 | for (j = 0; j < csrow->nr_channels; j++) { | |
de3910eb | 862 | struct dimm_info *dimm = csrow->channels[j]->dimm; |
a895bf8b MCC |
863 | n += dimm->nr_pages; |
864 | } | |
865 | if (n == 0) | |
da9bb1d2 AC |
866 | continue; |
867 | ||
956b9ba1 JP |
868 | edac_dbg(3, "MC%d: first(0x%lx) page(0x%lx) last(0x%lx) mask(0x%lx)\n", |
869 | mci->mc_idx, | |
870 | csrow->first_page, page, csrow->last_page, | |
871 | csrow->page_mask); | |
da9bb1d2 AC |
872 | |
873 | if ((page >= csrow->first_page) && | |
874 | (page <= csrow->last_page) && | |
875 | ((page & csrow->page_mask) == | |
876 | (csrow->first_page & csrow->page_mask))) { | |
877 | row = i; | |
878 | break; | |
879 | } | |
880 | } | |
881 | ||
882 | if (row == -1) | |
537fba28 | 883 | edac_mc_printk(mci, KERN_ERR, |
052dfb45 DT |
884 | "could not look up page error address %lx\n", |
885 | (unsigned long)page); | |
da9bb1d2 AC |
886 | |
887 | return row; | |
888 | } | |
9110540f | 889 | EXPORT_SYMBOL_GPL(edac_mc_find_csrow_by_page); |
da9bb1d2 | 890 | |
4275be63 MCC |
891 | const char *edac_layer_name[] = { |
892 | [EDAC_MC_LAYER_BRANCH] = "branch", | |
893 | [EDAC_MC_LAYER_CHANNEL] = "channel", | |
894 | [EDAC_MC_LAYER_SLOT] = "slot", | |
895 | [EDAC_MC_LAYER_CHIP_SELECT] = "csrow", | |
896 | }; | |
897 | EXPORT_SYMBOL_GPL(edac_layer_name); | |
898 | ||
899 | static void edac_inc_ce_error(struct mem_ctl_info *mci, | |
9eb07a7f MCC |
900 | bool enable_per_layer_report, |
901 | const int pos[EDAC_MAX_LAYERS], | |
902 | const u16 count) | |
da9bb1d2 | 903 | { |
4275be63 | 904 | int i, index = 0; |
da9bb1d2 | 905 | |
9eb07a7f | 906 | mci->ce_mc += count; |
da9bb1d2 | 907 | |
4275be63 | 908 | if (!enable_per_layer_report) { |
9eb07a7f | 909 | mci->ce_noinfo_count += count; |
da9bb1d2 AC |
910 | return; |
911 | } | |
e7ecd891 | 912 | |
4275be63 MCC |
913 | for (i = 0; i < mci->n_layers; i++) { |
914 | if (pos[i] < 0) | |
915 | break; | |
916 | index += pos[i]; | |
9eb07a7f | 917 | mci->ce_per_layer[i][index] += count; |
4275be63 MCC |
918 | |
919 | if (i < mci->n_layers - 1) | |
920 | index *= mci->layers[i + 1].size; | |
921 | } | |
922 | } | |
923 | ||
924 | static void edac_inc_ue_error(struct mem_ctl_info *mci, | |
925 | bool enable_per_layer_report, | |
9eb07a7f MCC |
926 | const int pos[EDAC_MAX_LAYERS], |
927 | const u16 count) | |
4275be63 MCC |
928 | { |
929 | int i, index = 0; | |
930 | ||
9eb07a7f | 931 | mci->ue_mc += count; |
4275be63 MCC |
932 | |
933 | if (!enable_per_layer_report) { | |
9eb07a7f | 934 | mci->ce_noinfo_count += count; |
da9bb1d2 AC |
935 | return; |
936 | } | |
937 | ||
4275be63 MCC |
938 | for (i = 0; i < mci->n_layers; i++) { |
939 | if (pos[i] < 0) | |
940 | break; | |
941 | index += pos[i]; | |
9eb07a7f | 942 | mci->ue_per_layer[i][index] += count; |
a7d7d2e1 | 943 | |
4275be63 MCC |
944 | if (i < mci->n_layers - 1) |
945 | index *= mci->layers[i + 1].size; | |
946 | } | |
947 | } | |
da9bb1d2 | 948 | |
4275be63 | 949 | static void edac_ce_error(struct mem_ctl_info *mci, |
9eb07a7f | 950 | const u16 error_count, |
4275be63 MCC |
951 | const int pos[EDAC_MAX_LAYERS], |
952 | const char *msg, | |
953 | const char *location, | |
954 | const char *label, | |
955 | const char *detail, | |
956 | const char *other_detail, | |
957 | const bool enable_per_layer_report, | |
958 | const unsigned long page_frame_number, | |
959 | const unsigned long offset_in_page, | |
53f2d028 | 960 | long grain) |
4275be63 MCC |
961 | { |
962 | unsigned long remapped_page; | |
963 | ||
964 | if (edac_mc_get_log_ce()) { | |
965 | if (other_detail && *other_detail) | |
966 | edac_mc_printk(mci, KERN_WARNING, | |
9eb07a7f MCC |
967 | "%d CE %s on %s (%s %s - %s)\n", |
968 | error_count, | |
4275be63 MCC |
969 | msg, label, location, |
970 | detail, other_detail); | |
971 | else | |
972 | edac_mc_printk(mci, KERN_WARNING, | |
9eb07a7f MCC |
973 | "%d CE %s on %s (%s %s)\n", |
974 | error_count, | |
4275be63 MCC |
975 | msg, label, location, |
976 | detail); | |
977 | } | |
9eb07a7f | 978 | edac_inc_ce_error(mci, enable_per_layer_report, pos, error_count); |
da9bb1d2 AC |
979 | |
980 | if (mci->scrub_mode & SCRUB_SW_SRC) { | |
981 | /* | |
4275be63 MCC |
982 | * Some memory controllers (called MCs below) can remap |
983 | * memory so that it is still available at a different | |
984 | * address when PCI devices map into memory. | |
985 | * MC's that can't do this, lose the memory where PCI | |
986 | * devices are mapped. This mapping is MC-dependent | |
987 | * and so we call back into the MC driver for it to | |
988 | * map the MC page to a physical (CPU) page which can | |
989 | * then be mapped to a virtual page - which can then | |
990 | * be scrubbed. | |
991 | */ | |
da9bb1d2 | 992 | remapped_page = mci->ctl_page_to_phys ? |
052dfb45 DT |
993 | mci->ctl_page_to_phys(mci, page_frame_number) : |
994 | page_frame_number; | |
da9bb1d2 | 995 | |
4275be63 MCC |
996 | edac_mc_scrub_block(remapped_page, |
997 | offset_in_page, grain); | |
da9bb1d2 AC |
998 | } |
999 | } | |
1000 | ||
4275be63 | 1001 | static void edac_ue_error(struct mem_ctl_info *mci, |
9eb07a7f | 1002 | const u16 error_count, |
4275be63 MCC |
1003 | const int pos[EDAC_MAX_LAYERS], |
1004 | const char *msg, | |
1005 | const char *location, | |
1006 | const char *label, | |
1007 | const char *detail, | |
1008 | const char *other_detail, | |
1009 | const bool enable_per_layer_report) | |
da9bb1d2 | 1010 | { |
4275be63 MCC |
1011 | if (edac_mc_get_log_ue()) { |
1012 | if (other_detail && *other_detail) | |
1013 | edac_mc_printk(mci, KERN_WARNING, | |
9eb07a7f MCC |
1014 | "%d UE %s on %s (%s %s - %s)\n", |
1015 | error_count, | |
4275be63 MCC |
1016 | msg, label, location, detail, |
1017 | other_detail); | |
1018 | else | |
1019 | edac_mc_printk(mci, KERN_WARNING, | |
9eb07a7f MCC |
1020 | "%d UE %s on %s (%s %s)\n", |
1021 | error_count, | |
4275be63 MCC |
1022 | msg, label, location, detail); |
1023 | } | |
e7ecd891 | 1024 | |
4275be63 MCC |
1025 | if (edac_mc_get_panic_on_ue()) { |
1026 | if (other_detail && *other_detail) | |
1027 | panic("UE %s on %s (%s%s - %s)\n", | |
1028 | msg, label, location, detail, other_detail); | |
1029 | else | |
1030 | panic("UE %s on %s (%s%s)\n", | |
1031 | msg, label, location, detail); | |
1032 | } | |
1033 | ||
9eb07a7f | 1034 | edac_inc_ue_error(mci, enable_per_layer_report, pos, error_count); |
da9bb1d2 AC |
1035 | } |
1036 | ||
4275be63 | 1037 | #define OTHER_LABEL " or " |
53f2d028 MCC |
1038 | |
1039 | /** | |
1040 | * edac_mc_handle_error - reports a memory event to userspace | |
1041 | * | |
1042 | * @type: severity of the error (CE/UE/Fatal) | |
1043 | * @mci: a struct mem_ctl_info pointer | |
9eb07a7f | 1044 | * @error_count: Number of errors of the same type |
53f2d028 MCC |
1045 | * @page_frame_number: mem page where the error occurred |
1046 | * @offset_in_page: offset of the error inside the page | |
1047 | * @syndrome: ECC syndrome | |
1048 | * @top_layer: Memory layer[0] position | |
1049 | * @mid_layer: Memory layer[1] position | |
1050 | * @low_layer: Memory layer[2] position | |
1051 | * @msg: Message meaningful to the end users that | |
1052 | * explains the event | |
1053 | * @other_detail: Technical details about the event that | |
1054 | * may help hardware manufacturers and | |
1055 | * EDAC developers to analyse the event | |
53f2d028 | 1056 | */ |
4275be63 MCC |
1057 | void edac_mc_handle_error(const enum hw_event_mc_err_type type, |
1058 | struct mem_ctl_info *mci, | |
9eb07a7f | 1059 | const u16 error_count, |
4275be63 MCC |
1060 | const unsigned long page_frame_number, |
1061 | const unsigned long offset_in_page, | |
1062 | const unsigned long syndrome, | |
53f2d028 MCC |
1063 | const int top_layer, |
1064 | const int mid_layer, | |
1065 | const int low_layer, | |
4275be63 | 1066 | const char *msg, |
03f7eae8 | 1067 | const char *other_detail) |
da9bb1d2 | 1068 | { |
4275be63 MCC |
1069 | /* FIXME: too much for stack: move it to some pre-alocated area */ |
1070 | char detail[80], location[80]; | |
1071 | char label[(EDAC_MC_LABEL_LEN + 1 + sizeof(OTHER_LABEL)) * mci->tot_dimms]; | |
1072 | char *p; | |
1073 | int row = -1, chan = -1; | |
53f2d028 | 1074 | int pos[EDAC_MAX_LAYERS] = { top_layer, mid_layer, low_layer }; |
4275be63 | 1075 | int i; |
53f2d028 | 1076 | long grain; |
4275be63 | 1077 | bool enable_per_layer_report = false; |
53f2d028 | 1078 | u8 grain_bits; |
da9bb1d2 | 1079 | |
956b9ba1 | 1080 | edac_dbg(3, "MC%d\n", mci->mc_idx); |
da9bb1d2 | 1081 | |
4275be63 MCC |
1082 | /* |
1083 | * Check if the event report is consistent and if the memory | |
1084 | * location is known. If it is known, enable_per_layer_report will be | |
1085 | * true, the DIMM(s) label info will be filled and the per-layer | |
1086 | * error counters will be incremented. | |
1087 | */ | |
1088 | for (i = 0; i < mci->n_layers; i++) { | |
1089 | if (pos[i] >= (int)mci->layers[i].size) { | |
1090 | if (type == HW_EVENT_ERR_CORRECTED) | |
1091 | p = "CE"; | |
1092 | else | |
1093 | p = "UE"; | |
1094 | ||
1095 | edac_mc_printk(mci, KERN_ERR, | |
1096 | "INTERNAL ERROR: %s value is out of range (%d >= %d)\n", | |
1097 | edac_layer_name[mci->layers[i].type], | |
1098 | pos[i], mci->layers[i].size); | |
1099 | /* | |
1100 | * Instead of just returning it, let's use what's | |
1101 | * known about the error. The increment routines and | |
1102 | * the DIMM filter logic will do the right thing by | |
1103 | * pointing the likely damaged DIMMs. | |
1104 | */ | |
1105 | pos[i] = -1; | |
1106 | } | |
1107 | if (pos[i] >= 0) | |
1108 | enable_per_layer_report = true; | |
da9bb1d2 AC |
1109 | } |
1110 | ||
4275be63 MCC |
1111 | /* |
1112 | * Get the dimm label/grain that applies to the match criteria. | |
1113 | * As the error algorithm may not be able to point to just one memory | |
1114 | * stick, the logic here will get all possible labels that could | |
1115 | * pottentially be affected by the error. | |
1116 | * On FB-DIMM memory controllers, for uncorrected errors, it is common | |
1117 | * to have only the MC channel and the MC dimm (also called "branch") | |
1118 | * but the channel is not known, as the memory is arranged in pairs, | |
1119 | * where each memory belongs to a separate channel within the same | |
1120 | * branch. | |
1121 | */ | |
1122 | grain = 0; | |
1123 | p = label; | |
1124 | *p = '\0'; | |
1125 | for (i = 0; i < mci->tot_dimms; i++) { | |
de3910eb | 1126 | struct dimm_info *dimm = mci->dimms[i]; |
da9bb1d2 | 1127 | |
53f2d028 | 1128 | if (top_layer >= 0 && top_layer != dimm->location[0]) |
4275be63 | 1129 | continue; |
53f2d028 | 1130 | if (mid_layer >= 0 && mid_layer != dimm->location[1]) |
4275be63 | 1131 | continue; |
53f2d028 | 1132 | if (low_layer >= 0 && low_layer != dimm->location[2]) |
4275be63 | 1133 | continue; |
da9bb1d2 | 1134 | |
4275be63 MCC |
1135 | /* get the max grain, over the error match range */ |
1136 | if (dimm->grain > grain) | |
1137 | grain = dimm->grain; | |
9794f33d | 1138 | |
4275be63 MCC |
1139 | /* |
1140 | * If the error is memory-controller wide, there's no need to | |
1141 | * seek for the affected DIMMs because the whole | |
1142 | * channel/memory controller/... may be affected. | |
1143 | * Also, don't show errors for empty DIMM slots. | |
1144 | */ | |
1145 | if (enable_per_layer_report && dimm->nr_pages) { | |
1146 | if (p != label) { | |
1147 | strcpy(p, OTHER_LABEL); | |
1148 | p += strlen(OTHER_LABEL); | |
1149 | } | |
1150 | strcpy(p, dimm->label); | |
1151 | p += strlen(p); | |
1152 | *p = '\0'; | |
1153 | ||
1154 | /* | |
1155 | * get csrow/channel of the DIMM, in order to allow | |
1156 | * incrementing the compat API counters | |
1157 | */ | |
956b9ba1 JP |
1158 | edac_dbg(4, "%s csrows map: (%d,%d)\n", |
1159 | mci->mem_is_per_rank ? "rank" : "dimm", | |
1160 | dimm->csrow, dimm->cschannel); | |
4275be63 MCC |
1161 | if (row == -1) |
1162 | row = dimm->csrow; | |
1163 | else if (row >= 0 && row != dimm->csrow) | |
1164 | row = -2; | |
1165 | ||
1166 | if (chan == -1) | |
1167 | chan = dimm->cschannel; | |
1168 | else if (chan >= 0 && chan != dimm->cschannel) | |
1169 | chan = -2; | |
1170 | } | |
9794f33d | 1171 | } |
1172 | ||
4275be63 MCC |
1173 | if (!enable_per_layer_report) { |
1174 | strcpy(label, "any memory"); | |
1175 | } else { | |
956b9ba1 | 1176 | edac_dbg(4, "csrow/channel to increment: (%d,%d)\n", row, chan); |
4275be63 MCC |
1177 | if (p == label) |
1178 | strcpy(label, "unknown memory"); | |
1179 | if (type == HW_EVENT_ERR_CORRECTED) { | |
1180 | if (row >= 0) { | |
9eb07a7f | 1181 | mci->csrows[row]->ce_count += error_count; |
4275be63 | 1182 | if (chan >= 0) |
9eb07a7f | 1183 | mci->csrows[row]->channels[chan]->ce_count += error_count; |
4275be63 MCC |
1184 | } |
1185 | } else | |
1186 | if (row >= 0) | |
9eb07a7f | 1187 | mci->csrows[row]->ue_count += error_count; |
9794f33d | 1188 | } |
1189 | ||
4275be63 MCC |
1190 | /* Fill the RAM location data */ |
1191 | p = location; | |
1192 | for (i = 0; i < mci->n_layers; i++) { | |
1193 | if (pos[i] < 0) | |
1194 | continue; | |
9794f33d | 1195 | |
4275be63 MCC |
1196 | p += sprintf(p, "%s:%d ", |
1197 | edac_layer_name[mci->layers[i].type], | |
1198 | pos[i]); | |
9794f33d | 1199 | } |
53f2d028 MCC |
1200 | if (p > location) |
1201 | *(p - 1) = '\0'; | |
1202 | ||
1203 | /* Report the error via the trace interface */ | |
1204 | ||
53f2d028 MCC |
1205 | grain_bits = fls_long(grain) + 1; |
1206 | trace_mc_event(type, msg, label, error_count, | |
1207 | mci->mc_idx, top_layer, mid_layer, low_layer, | |
1208 | PAGES_TO_MiB(page_frame_number) | offset_in_page, | |
1209 | grain_bits, syndrome, other_detail); | |
a7d7d2e1 | 1210 | |
4275be63 MCC |
1211 | /* Memory type dependent details about the error */ |
1212 | if (type == HW_EVENT_ERR_CORRECTED) { | |
1213 | snprintf(detail, sizeof(detail), | |
53f2d028 | 1214 | "page:0x%lx offset:0x%lx grain:%ld syndrome:0x%lx", |
4275be63 MCC |
1215 | page_frame_number, offset_in_page, |
1216 | grain, syndrome); | |
9eb07a7f MCC |
1217 | edac_ce_error(mci, error_count, pos, msg, location, label, |
1218 | detail, other_detail, enable_per_layer_report, | |
4275be63 MCC |
1219 | page_frame_number, offset_in_page, grain); |
1220 | } else { | |
1221 | snprintf(detail, sizeof(detail), | |
53f2d028 | 1222 | "page:0x%lx offset:0x%lx grain:%ld", |
4275be63 | 1223 | page_frame_number, offset_in_page, grain); |
9794f33d | 1224 | |
9eb07a7f MCC |
1225 | edac_ue_error(mci, error_count, pos, msg, location, label, |
1226 | detail, other_detail, enable_per_layer_report); | |
4275be63 | 1227 | } |
9794f33d | 1228 | } |
4275be63 | 1229 | EXPORT_SYMBOL_GPL(edac_mc_handle_error); |