]>
Commit | Line | Data |
---|---|---|
89b831ef | 1 | /* |
95268664 | 2 | * (c) 2005, 2006 Advanced Micro Devices, Inc. |
89b831ef JS |
3 | * Your use of this code is subject to the terms and conditions of the |
4 | * GNU general public license version 2. See "COPYING" or | |
5 | * http://www.gnu.org/licenses/gpl.html | |
6 | * | |
7 | * Written by Jacob Shin - AMD, Inc. | |
8 | * | |
9 | * Support : jacob.shin@amd.com | |
10 | * | |
95268664 JS |
11 | * April 2006 |
12 | * - added support for AMD Family 0x10 processors | |
89b831ef | 13 | * |
95268664 | 14 | * All MC4_MISCi registers are shared between multi-cores |
89b831ef | 15 | */ |
89b831ef | 16 | #include <linux/interrupt.h> |
89b831ef | 17 | #include <linux/notifier.h> |
1cb2a8e1 | 18 | #include <linux/kobject.h> |
34fa1967 | 19 | #include <linux/percpu.h> |
89b831ef | 20 | #include <linux/sysdev.h> |
1cb2a8e1 IM |
21 | #include <linux/errno.h> |
22 | #include <linux/sched.h> | |
89b831ef | 23 | #include <linux/sysfs.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
1cb2a8e1 IM |
25 | #include <linux/init.h> |
26 | #include <linux/cpu.h> | |
27 | #include <linux/smp.h> | |
28 | ||
89b831ef | 29 | #include <asm/apic.h> |
1cb2a8e1 | 30 | #include <asm/idle.h> |
89b831ef JS |
31 | #include <asm/mce.h> |
32 | #include <asm/msr.h> | |
89b831ef | 33 | |
2903ee85 JS |
34 | #define PFX "mce_threshold: " |
35 | #define VERSION "version 1.1.1" | |
36 | #define NR_BANKS 6 | |
37 | #define NR_BLOCKS 9 | |
38 | #define THRESHOLD_MAX 0xFFF | |
39 | #define INT_TYPE_APIC 0x00020000 | |
40 | #define MASK_VALID_HI 0x80000000 | |
24ce0e96 JB |
41 | #define MASK_CNTP_HI 0x40000000 |
42 | #define MASK_LOCKED_HI 0x20000000 | |
2903ee85 JS |
43 | #define MASK_LVTOFF_HI 0x00F00000 |
44 | #define MASK_COUNT_EN_HI 0x00080000 | |
45 | #define MASK_INT_TYPE_HI 0x00060000 | |
46 | #define MASK_OVERFLOW_HI 0x00010000 | |
89b831ef | 47 | #define MASK_ERR_COUNT_HI 0x00000FFF |
95268664 JS |
48 | #define MASK_BLKPTR_LO 0xFF000000 |
49 | #define MCG_XBLK_ADDR 0xC0000400 | |
89b831ef | 50 | |
95268664 | 51 | struct threshold_block { |
1cb2a8e1 IM |
52 | unsigned int block; |
53 | unsigned int bank; | |
54 | unsigned int cpu; | |
55 | u32 address; | |
56 | u16 interrupt_enable; | |
57 | u16 threshold_limit; | |
58 | struct kobject kobj; | |
59 | struct list_head miscj; | |
89b831ef JS |
60 | }; |
61 | ||
95268664 JS |
62 | /* defaults used early on boot */ |
63 | static struct threshold_block threshold_defaults = { | |
1cb2a8e1 IM |
64 | .interrupt_enable = 0, |
65 | .threshold_limit = THRESHOLD_MAX, | |
89b831ef JS |
66 | }; |
67 | ||
95268664 | 68 | struct threshold_bank { |
1cb2a8e1 IM |
69 | struct kobject *kobj; |
70 | struct threshold_block *blocks; | |
71 | cpumask_var_t cpus; | |
95268664 | 72 | }; |
204fba4a | 73 | static DEFINE_PER_CPU(struct threshold_bank * [NR_BANKS], threshold_banks); |
95268664 | 74 | |
89b831ef JS |
75 | #ifdef CONFIG_SMP |
76 | static unsigned char shared_bank[NR_BANKS] = { | |
77 | 0, 0, 0, 0, 1 | |
78 | }; | |
79 | #endif | |
80 | ||
81 | static DEFINE_PER_CPU(unsigned char, bank_map); /* see which banks are on */ | |
82 | ||
b2762686 AK |
83 | static void amd_threshold_interrupt(void); |
84 | ||
89b831ef JS |
85 | /* |
86 | * CPU Initialization | |
87 | */ | |
88 | ||
4cd4601d | 89 | struct thresh_restart { |
1cb2a8e1 IM |
90 | struct threshold_block *b; |
91 | int reset; | |
92 | u16 old_limit; | |
4cd4601d MT |
93 | }; |
94 | ||
89b831ef | 95 | /* must be called with correct cpu affinity */ |
a6b6a14e AM |
96 | /* Called via smp_call_function_single() */ |
97 | static void threshold_restart_bank(void *_tr) | |
89b831ef | 98 | { |
4cd4601d | 99 | struct thresh_restart *tr = _tr; |
89b831ef JS |
100 | u32 mci_misc_hi, mci_misc_lo; |
101 | ||
4cd4601d | 102 | rdmsr(tr->b->address, mci_misc_lo, mci_misc_hi); |
89b831ef | 103 | |
4cd4601d MT |
104 | if (tr->b->threshold_limit < (mci_misc_hi & THRESHOLD_MAX)) |
105 | tr->reset = 1; /* limit cannot be lower than err count */ | |
89b831ef | 106 | |
4cd4601d | 107 | if (tr->reset) { /* reset err count and overflow bit */ |
89b831ef JS |
108 | mci_misc_hi = |
109 | (mci_misc_hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) | | |
4cd4601d MT |
110 | (THRESHOLD_MAX - tr->b->threshold_limit); |
111 | } else if (tr->old_limit) { /* change limit w/o reset */ | |
89b831ef | 112 | int new_count = (mci_misc_hi & THRESHOLD_MAX) + |
4cd4601d | 113 | (tr->old_limit - tr->b->threshold_limit); |
1cb2a8e1 | 114 | |
89b831ef JS |
115 | mci_misc_hi = (mci_misc_hi & ~MASK_ERR_COUNT_HI) | |
116 | (new_count & THRESHOLD_MAX); | |
117 | } | |
118 | ||
4cd4601d | 119 | tr->b->interrupt_enable ? |
89b831ef JS |
120 | (mci_misc_hi = (mci_misc_hi & ~MASK_INT_TYPE_HI) | INT_TYPE_APIC) : |
121 | (mci_misc_hi &= ~MASK_INT_TYPE_HI); | |
122 | ||
123 | mci_misc_hi |= MASK_COUNT_EN_HI; | |
4cd4601d | 124 | wrmsr(tr->b->address, mci_misc_lo, mci_misc_hi); |
89b831ef JS |
125 | } |
126 | ||
95268664 | 127 | /* cpu init entry point, called from mce.c with preempt off */ |
cc3ca220 | 128 | void mce_amd_feature_init(struct cpuinfo_x86 *c) |
89b831ef | 129 | { |
89b831ef | 130 | unsigned int cpu = smp_processor_id(); |
95268664 | 131 | u32 low = 0, high = 0, address = 0; |
1cb2a8e1 | 132 | unsigned int bank, block; |
4cd4601d | 133 | struct thresh_restart tr; |
1cb2a8e1 | 134 | u8 lvt_off; |
89b831ef JS |
135 | |
136 | for (bank = 0; bank < NR_BANKS; ++bank) { | |
95268664 JS |
137 | for (block = 0; block < NR_BLOCKS; ++block) { |
138 | if (block == 0) | |
139 | address = MSR_IA32_MC0_MISC + bank * 4; | |
24ce0e96 JB |
140 | else if (block == 1) { |
141 | address = (low & MASK_BLKPTR_LO) >> 21; | |
142 | if (!address) | |
143 | break; | |
144 | address += MCG_XBLK_ADDR; | |
1cb2a8e1 | 145 | } else |
95268664 JS |
146 | ++address; |
147 | ||
148 | if (rdmsr_safe(address, &low, &high)) | |
24ce0e96 | 149 | break; |
95268664 JS |
150 | |
151 | if (!(high & MASK_VALID_HI)) { | |
152 | if (block) | |
153 | continue; | |
154 | else | |
155 | break; | |
156 | } | |
157 | ||
24ce0e96 JB |
158 | if (!(high & MASK_CNTP_HI) || |
159 | (high & MASK_LOCKED_HI)) | |
95268664 JS |
160 | continue; |
161 | ||
162 | if (!block) | |
163 | per_cpu(bank_map, cpu) |= (1 << bank); | |
89b831ef | 164 | #ifdef CONFIG_SMP |
95268664 JS |
165 | if (shared_bank[bank] && c->cpu_core_id) |
166 | break; | |
89b831ef | 167 | #endif |
7b83dae7 RR |
168 | lvt_off = setup_APIC_eilvt_mce(THRESHOLD_APIC_VECTOR, |
169 | APIC_EILVT_MSG_FIX, 0); | |
170 | ||
95268664 | 171 | high &= ~MASK_LVTOFF_HI; |
7b83dae7 | 172 | high |= lvt_off << 20; |
95268664 JS |
173 | wrmsr(address, low, high); |
174 | ||
95268664 | 175 | threshold_defaults.address = address; |
4cd4601d MT |
176 | tr.b = &threshold_defaults; |
177 | tr.reset = 0; | |
178 | tr.old_limit = 0; | |
179 | threshold_restart_bank(&tr); | |
b2762686 AK |
180 | |
181 | mce_threshold_vector = amd_threshold_interrupt; | |
95268664 | 182 | } |
89b831ef JS |
183 | } |
184 | } | |
185 | ||
186 | /* | |
187 | * APIC Interrupt Handler | |
188 | */ | |
189 | ||
190 | /* | |
191 | * threshold interrupt handler will service THRESHOLD_APIC_VECTOR. | |
192 | * the interrupt goes off when error_count reaches threshold_limit. | |
193 | * the handler will simply log mcelog w/ software defined bank number. | |
194 | */ | |
b2762686 | 195 | static void amd_threshold_interrupt(void) |
89b831ef | 196 | { |
1cb2a8e1 | 197 | u32 low = 0, high = 0, address = 0; |
95268664 | 198 | unsigned int bank, block; |
89b831ef JS |
199 | struct mce m; |
200 | ||
b5f2fa4e | 201 | mce_setup(&m); |
89b831ef JS |
202 | |
203 | /* assume first bank caused it */ | |
204 | for (bank = 0; bank < NR_BANKS; ++bank) { | |
24ce0e96 JB |
205 | if (!(per_cpu(bank_map, m.cpu) & (1 << bank))) |
206 | continue; | |
95268664 | 207 | for (block = 0; block < NR_BLOCKS; ++block) { |
1cb2a8e1 | 208 | if (block == 0) { |
95268664 | 209 | address = MSR_IA32_MC0_MISC + bank * 4; |
1cb2a8e1 | 210 | } else if (block == 1) { |
24ce0e96 JB |
211 | address = (low & MASK_BLKPTR_LO) >> 21; |
212 | if (!address) | |
213 | break; | |
214 | address += MCG_XBLK_ADDR; | |
1cb2a8e1 | 215 | } else { |
95268664 | 216 | ++address; |
1cb2a8e1 | 217 | } |
95268664 JS |
218 | |
219 | if (rdmsr_safe(address, &low, &high)) | |
24ce0e96 | 220 | break; |
95268664 JS |
221 | |
222 | if (!(high & MASK_VALID_HI)) { | |
223 | if (block) | |
224 | continue; | |
225 | else | |
226 | break; | |
227 | } | |
228 | ||
24ce0e96 JB |
229 | if (!(high & MASK_CNTP_HI) || |
230 | (high & MASK_LOCKED_HI)) | |
95268664 JS |
231 | continue; |
232 | ||
1cb2a8e1 IM |
233 | /* |
234 | * Log the machine check that caused the threshold | |
235 | * event. | |
236 | */ | |
ee031c31 AK |
237 | machine_check_poll(MCP_TIMESTAMP, |
238 | &__get_cpu_var(mce_poll_banks)); | |
a98f0dd3 | 239 | |
95268664 JS |
240 | if (high & MASK_OVERFLOW_HI) { |
241 | rdmsrl(address, m.misc); | |
242 | rdmsrl(MSR_IA32_MC0_STATUS + bank * 4, | |
243 | m.status); | |
244 | m.bank = K8_MCE_THRESHOLD_BASE | |
245 | + bank * NR_BLOCKS | |
246 | + block; | |
247 | mce_log(&m); | |
b2762686 | 248 | return; |
95268664 | 249 | } |
89b831ef JS |
250 | } |
251 | } | |
89b831ef JS |
252 | } |
253 | ||
254 | /* | |
255 | * Sysfs Interface | |
256 | */ | |
257 | ||
89b831ef | 258 | struct threshold_attr { |
2903ee85 | 259 | struct attribute attr; |
1cb2a8e1 IM |
260 | ssize_t (*show) (struct threshold_block *, char *); |
261 | ssize_t (*store) (struct threshold_block *, const char *, size_t count); | |
89b831ef JS |
262 | }; |
263 | ||
1cb2a8e1 IM |
264 | #define SHOW_FIELDS(name) \ |
265 | static ssize_t show_ ## name(struct threshold_block *b, char *buf) \ | |
266 | { \ | |
267 | return sprintf(buf, "%lx\n", (unsigned long) b->name); \ | |
2903ee85 | 268 | } |
89b831ef JS |
269 | SHOW_FIELDS(interrupt_enable) |
270 | SHOW_FIELDS(threshold_limit) | |
271 | ||
1cb2a8e1 | 272 | static ssize_t |
9319cec8 | 273 | store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size) |
89b831ef | 274 | { |
4cd4601d | 275 | struct thresh_restart tr; |
1cb2a8e1 | 276 | unsigned long new; |
1cb2a8e1 | 277 | |
9319cec8 | 278 | if (strict_strtoul(buf, 0, &new) < 0) |
89b831ef | 279 | return -EINVAL; |
1cb2a8e1 | 280 | |
89b831ef JS |
281 | b->interrupt_enable = !!new; |
282 | ||
1cb2a8e1 IM |
283 | tr.b = b; |
284 | tr.reset = 0; | |
285 | tr.old_limit = 0; | |
286 | ||
a6b6a14e | 287 | smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1); |
89b831ef | 288 | |
9319cec8 | 289 | return size; |
89b831ef JS |
290 | } |
291 | ||
1cb2a8e1 | 292 | static ssize_t |
9319cec8 | 293 | store_threshold_limit(struct threshold_block *b, const char *buf, size_t size) |
89b831ef | 294 | { |
4cd4601d | 295 | struct thresh_restart tr; |
1cb2a8e1 | 296 | unsigned long new; |
1cb2a8e1 | 297 | |
9319cec8 | 298 | if (strict_strtoul(buf, 0, &new) < 0) |
89b831ef | 299 | return -EINVAL; |
1cb2a8e1 | 300 | |
89b831ef JS |
301 | if (new > THRESHOLD_MAX) |
302 | new = THRESHOLD_MAX; | |
303 | if (new < 1) | |
304 | new = 1; | |
1cb2a8e1 | 305 | |
4cd4601d | 306 | tr.old_limit = b->threshold_limit; |
89b831ef | 307 | b->threshold_limit = new; |
4cd4601d MT |
308 | tr.b = b; |
309 | tr.reset = 0; | |
89b831ef | 310 | |
a6b6a14e | 311 | smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1); |
89b831ef | 312 | |
9319cec8 | 313 | return size; |
89b831ef JS |
314 | } |
315 | ||
a6b6a14e | 316 | struct threshold_block_cross_cpu { |
1cb2a8e1 IM |
317 | struct threshold_block *tb; |
318 | long retval; | |
a6b6a14e AM |
319 | }; |
320 | ||
321 | static void local_error_count_handler(void *_tbcc) | |
89b831ef | 322 | { |
a6b6a14e AM |
323 | struct threshold_block_cross_cpu *tbcc = _tbcc; |
324 | struct threshold_block *b = tbcc->tb; | |
4cd4601d MT |
325 | u32 low, high; |
326 | ||
95268664 | 327 | rdmsr(b->address, low, high); |
a6b6a14e | 328 | tbcc->retval = (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit); |
4cd4601d MT |
329 | } |
330 | ||
331 | static ssize_t show_error_count(struct threshold_block *b, char *buf) | |
332 | { | |
a6b6a14e AM |
333 | struct threshold_block_cross_cpu tbcc = { .tb = b, }; |
334 | ||
335 | smp_call_function_single(b->cpu, local_error_count_handler, &tbcc, 1); | |
336 | return sprintf(buf, "%lx\n", tbcc.retval); | |
89b831ef JS |
337 | } |
338 | ||
95268664 | 339 | static ssize_t store_error_count(struct threshold_block *b, |
89b831ef JS |
340 | const char *buf, size_t count) |
341 | { | |
4cd4601d MT |
342 | struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 }; |
343 | ||
a6b6a14e | 344 | smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1); |
89b831ef JS |
345 | return 1; |
346 | } | |
347 | ||
34fa1967 HS |
348 | #define RW_ATTR(val) \ |
349 | static struct threshold_attr val = { \ | |
350 | .attr = {.name = __stringify(val), .mode = 0644 }, \ | |
351 | .show = show_## val, \ | |
352 | .store = store_## val, \ | |
89b831ef JS |
353 | }; |
354 | ||
2903ee85 JS |
355 | RW_ATTR(interrupt_enable); |
356 | RW_ATTR(threshold_limit); | |
357 | RW_ATTR(error_count); | |
89b831ef JS |
358 | |
359 | static struct attribute *default_attrs[] = { | |
360 | &interrupt_enable.attr, | |
361 | &threshold_limit.attr, | |
362 | &error_count.attr, | |
363 | NULL | |
364 | }; | |
365 | ||
1cb2a8e1 IM |
366 | #define to_block(k) container_of(k, struct threshold_block, kobj) |
367 | #define to_attr(a) container_of(a, struct threshold_attr, attr) | |
89b831ef JS |
368 | |
369 | static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) | |
370 | { | |
95268664 | 371 | struct threshold_block *b = to_block(kobj); |
89b831ef JS |
372 | struct threshold_attr *a = to_attr(attr); |
373 | ssize_t ret; | |
1cb2a8e1 | 374 | |
89b831ef | 375 | ret = a->show ? a->show(b, buf) : -EIO; |
1cb2a8e1 | 376 | |
89b831ef JS |
377 | return ret; |
378 | } | |
379 | ||
380 | static ssize_t store(struct kobject *kobj, struct attribute *attr, | |
381 | const char *buf, size_t count) | |
382 | { | |
95268664 | 383 | struct threshold_block *b = to_block(kobj); |
89b831ef JS |
384 | struct threshold_attr *a = to_attr(attr); |
385 | ssize_t ret; | |
1cb2a8e1 | 386 | |
89b831ef | 387 | ret = a->store ? a->store(b, buf, count) : -EIO; |
1cb2a8e1 | 388 | |
89b831ef JS |
389 | return ret; |
390 | } | |
391 | ||
52cf25d0 | 392 | static const struct sysfs_ops threshold_ops = { |
1cb2a8e1 IM |
393 | .show = show, |
394 | .store = store, | |
89b831ef JS |
395 | }; |
396 | ||
397 | static struct kobj_type threshold_ktype = { | |
1cb2a8e1 IM |
398 | .sysfs_ops = &threshold_ops, |
399 | .default_attrs = default_attrs, | |
89b831ef JS |
400 | }; |
401 | ||
95268664 JS |
402 | static __cpuinit int allocate_threshold_blocks(unsigned int cpu, |
403 | unsigned int bank, | |
404 | unsigned int block, | |
405 | u32 address) | |
406 | { | |
95268664 | 407 | struct threshold_block *b = NULL; |
1cb2a8e1 IM |
408 | u32 low, high; |
409 | int err; | |
95268664 JS |
410 | |
411 | if ((bank >= NR_BANKS) || (block >= NR_BLOCKS)) | |
412 | return 0; | |
413 | ||
a6b6a14e | 414 | if (rdmsr_safe_on_cpu(cpu, address, &low, &high)) |
24ce0e96 | 415 | return 0; |
95268664 JS |
416 | |
417 | if (!(high & MASK_VALID_HI)) { | |
418 | if (block) | |
419 | goto recurse; | |
420 | else | |
421 | return 0; | |
422 | } | |
423 | ||
24ce0e96 JB |
424 | if (!(high & MASK_CNTP_HI) || |
425 | (high & MASK_LOCKED_HI)) | |
95268664 JS |
426 | goto recurse; |
427 | ||
428 | b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL); | |
429 | if (!b) | |
430 | return -ENOMEM; | |
95268664 | 431 | |
1cb2a8e1 IM |
432 | b->block = block; |
433 | b->bank = bank; | |
434 | b->cpu = cpu; | |
435 | b->address = address; | |
436 | b->interrupt_enable = 0; | |
437 | b->threshold_limit = THRESHOLD_MAX; | |
95268664 JS |
438 | |
439 | INIT_LIST_HEAD(&b->miscj); | |
440 | ||
1cb2a8e1 | 441 | if (per_cpu(threshold_banks, cpu)[bank]->blocks) { |
95268664 JS |
442 | list_add(&b->miscj, |
443 | &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj); | |
1cb2a8e1 | 444 | } else { |
95268664 | 445 | per_cpu(threshold_banks, cpu)[bank]->blocks = b; |
1cb2a8e1 | 446 | } |
95268664 | 447 | |
542eb75a GKH |
448 | err = kobject_init_and_add(&b->kobj, &threshold_ktype, |
449 | per_cpu(threshold_banks, cpu)[bank]->kobj, | |
450 | "misc%i", block); | |
95268664 JS |
451 | if (err) |
452 | goto out_free; | |
453 | recurse: | |
454 | if (!block) { | |
455 | address = (low & MASK_BLKPTR_LO) >> 21; | |
456 | if (!address) | |
457 | return 0; | |
458 | address += MCG_XBLK_ADDR; | |
1cb2a8e1 | 459 | } else { |
95268664 | 460 | ++address; |
1cb2a8e1 | 461 | } |
95268664 JS |
462 | |
463 | err = allocate_threshold_blocks(cpu, bank, ++block, address); | |
464 | if (err) | |
465 | goto out_free; | |
466 | ||
213eca7f GKH |
467 | if (b) |
468 | kobject_uevent(&b->kobj, KOBJ_ADD); | |
542eb75a | 469 | |
95268664 JS |
470 | return err; |
471 | ||
472 | out_free: | |
473 | if (b) { | |
38a382ae | 474 | kobject_put(&b->kobj); |
95268664 JS |
475 | kfree(b); |
476 | } | |
477 | return err; | |
478 | } | |
479 | ||
a6b6a14e AM |
480 | static __cpuinit long |
481 | local_allocate_threshold_blocks(int cpu, unsigned int bank) | |
4cd4601d | 482 | { |
a6b6a14e AM |
483 | return allocate_threshold_blocks(cpu, bank, 0, |
484 | MSR_IA32_MC0_MISC + bank * 4); | |
4cd4601d MT |
485 | } |
486 | ||
89b831ef | 487 | /* symlinks sibling shared banks to first core. first core owns dir/files. */ |
95268664 | 488 | static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank) |
89b831ef | 489 | { |
95268664 | 490 | int i, err = 0; |
68209407 | 491 | struct threshold_bank *b = NULL; |
95268664 | 492 | char name[32]; |
a017421d | 493 | #ifdef CONFIG_SMP |
cb9805ab | 494 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
a017421d | 495 | #endif |
95268664 JS |
496 | |
497 | sprintf(name, "threshold_bank%i", bank); | |
89b831ef JS |
498 | |
499 | #ifdef CONFIG_SMP | |
92cb7612 | 500 | if (cpu_data(cpu).cpu_core_id && shared_bank[bank]) { /* symlink */ |
cb9805ab | 501 | i = cpumask_first(c->llc_shared_map); |
95268664 JS |
502 | |
503 | /* first core not up yet */ | |
92cb7612 | 504 | if (cpu_data(i).cpu_core_id) |
95268664 JS |
505 | goto out; |
506 | ||
507 | /* already linked */ | |
508 | if (per_cpu(threshold_banks, cpu)[bank]) | |
509 | goto out; | |
510 | ||
511 | b = per_cpu(threshold_banks, i)[bank]; | |
89b831ef | 512 | |
89b831ef JS |
513 | if (!b) |
514 | goto out; | |
95268664 | 515 | |
cb491fca | 516 | err = sysfs_create_link(&per_cpu(mce_dev, cpu).kobj, |
a521cf20 | 517 | b->kobj, name); |
89b831ef JS |
518 | if (err) |
519 | goto out; | |
95268664 | 520 | |
cb9805ab | 521 | cpumask_copy(b->cpus, c->llc_shared_map); |
89b831ef | 522 | per_cpu(threshold_banks, cpu)[bank] = b; |
1cb2a8e1 | 523 | |
89b831ef JS |
524 | goto out; |
525 | } | |
526 | #endif | |
527 | ||
95268664 | 528 | b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL); |
89b831ef JS |
529 | if (!b) { |
530 | err = -ENOMEM; | |
531 | goto out; | |
532 | } | |
a1c33bbe MT |
533 | if (!alloc_cpumask_var(&b->cpus, GFP_KERNEL)) { |
534 | kfree(b); | |
535 | err = -ENOMEM; | |
536 | goto out; | |
537 | } | |
89b831ef | 538 | |
cb491fca | 539 | b->kobj = kobject_create_and_add(name, &per_cpu(mce_dev, cpu).kobj); |
a521cf20 GKH |
540 | if (!b->kobj) |
541 | goto out_free; | |
542 | ||
95268664 | 543 | #ifndef CONFIG_SMP |
a1c33bbe | 544 | cpumask_setall(b->cpus); |
95268664 | 545 | #else |
cb9805ab | 546 | cpumask_copy(b->cpus, c->llc_shared_map); |
95268664 | 547 | #endif |
95268664 | 548 | |
89b831ef | 549 | per_cpu(threshold_banks, cpu)[bank] = b; |
95268664 | 550 | |
a6b6a14e | 551 | err = local_allocate_threshold_blocks(cpu, bank); |
95268664 JS |
552 | if (err) |
553 | goto out_free; | |
554 | ||
a1c33bbe | 555 | for_each_cpu(i, b->cpus) { |
95268664 JS |
556 | if (i == cpu) |
557 | continue; | |
558 | ||
cb491fca | 559 | err = sysfs_create_link(&per_cpu(mce_dev, i).kobj, |
a521cf20 | 560 | b->kobj, name); |
95268664 JS |
561 | if (err) |
562 | goto out; | |
563 | ||
564 | per_cpu(threshold_banks, i)[bank] = b; | |
565 | } | |
566 | ||
567 | goto out; | |
568 | ||
569 | out_free: | |
570 | per_cpu(threshold_banks, cpu)[bank] = NULL; | |
a1c33bbe | 571 | free_cpumask_var(b->cpus); |
95268664 | 572 | kfree(b); |
2903ee85 | 573 | out: |
89b831ef JS |
574 | return err; |
575 | } | |
576 | ||
577 | /* create dir/files for all valid threshold banks */ | |
578 | static __cpuinit int threshold_create_device(unsigned int cpu) | |
579 | { | |
2903ee85 | 580 | unsigned int bank; |
89b831ef JS |
581 | int err = 0; |
582 | ||
89b831ef | 583 | for (bank = 0; bank < NR_BANKS; ++bank) { |
5a96f4a5 | 584 | if (!(per_cpu(bank_map, cpu) & (1 << bank))) |
89b831ef JS |
585 | continue; |
586 | err = threshold_create_bank(cpu, bank); | |
587 | if (err) | |
588 | goto out; | |
589 | } | |
2903ee85 | 590 | out: |
89b831ef JS |
591 | return err; |
592 | } | |
593 | ||
89b831ef JS |
594 | /* |
595 | * let's be hotplug friendly. | |
596 | * in case of multiple core processors, the first core always takes ownership | |
597 | * of shared sysfs dir/files, and rest of the cores will be symlinked to it. | |
598 | */ | |
599 | ||
be6b5a35 | 600 | static void deallocate_threshold_block(unsigned int cpu, |
95268664 JS |
601 | unsigned int bank) |
602 | { | |
603 | struct threshold_block *pos = NULL; | |
604 | struct threshold_block *tmp = NULL; | |
605 | struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank]; | |
606 | ||
607 | if (!head) | |
608 | return; | |
609 | ||
610 | list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) { | |
38a382ae | 611 | kobject_put(&pos->kobj); |
95268664 JS |
612 | list_del(&pos->miscj); |
613 | kfree(pos); | |
614 | } | |
615 | ||
616 | kfree(per_cpu(threshold_banks, cpu)[bank]->blocks); | |
617 | per_cpu(threshold_banks, cpu)[bank]->blocks = NULL; | |
618 | } | |
619 | ||
be6b5a35 | 620 | static void threshold_remove_bank(unsigned int cpu, int bank) |
89b831ef JS |
621 | { |
622 | struct threshold_bank *b; | |
95268664 | 623 | char name[32]; |
1cb2a8e1 | 624 | int i = 0; |
89b831ef JS |
625 | |
626 | b = per_cpu(threshold_banks, cpu)[bank]; | |
627 | if (!b) | |
628 | return; | |
95268664 JS |
629 | if (!b->blocks) |
630 | goto free_out; | |
631 | ||
632 | sprintf(name, "threshold_bank%i", bank); | |
633 | ||
02316067 | 634 | #ifdef CONFIG_SMP |
95268664 JS |
635 | /* sibling symlink */ |
636 | if (shared_bank[bank] && b->blocks->cpu != cpu) { | |
cb491fca | 637 | sysfs_remove_link(&per_cpu(mce_dev, cpu).kobj, name); |
0d2caebd | 638 | per_cpu(threshold_banks, cpu)[bank] = NULL; |
1cb2a8e1 | 639 | |
95268664 | 640 | return; |
89b831ef | 641 | } |
02316067 | 642 | #endif |
95268664 JS |
643 | |
644 | /* remove all sibling symlinks before unregistering */ | |
a1c33bbe | 645 | for_each_cpu(i, b->cpus) { |
95268664 JS |
646 | if (i == cpu) |
647 | continue; | |
648 | ||
cb491fca | 649 | sysfs_remove_link(&per_cpu(mce_dev, i).kobj, name); |
95268664 JS |
650 | per_cpu(threshold_banks, i)[bank] = NULL; |
651 | } | |
652 | ||
653 | deallocate_threshold_block(cpu, bank); | |
654 | ||
655 | free_out: | |
8735728e | 656 | kobject_del(b->kobj); |
38a382ae | 657 | kobject_put(b->kobj); |
a1c33bbe | 658 | free_cpumask_var(b->cpus); |
95268664 JS |
659 | kfree(b); |
660 | per_cpu(threshold_banks, cpu)[bank] = NULL; | |
89b831ef JS |
661 | } |
662 | ||
be6b5a35 | 663 | static void threshold_remove_device(unsigned int cpu) |
89b831ef | 664 | { |
2903ee85 | 665 | unsigned int bank; |
89b831ef JS |
666 | |
667 | for (bank = 0; bank < NR_BANKS; ++bank) { | |
5a96f4a5 | 668 | if (!(per_cpu(bank_map, cpu) & (1 << bank))) |
89b831ef JS |
669 | continue; |
670 | threshold_remove_bank(cpu, bank); | |
671 | } | |
89b831ef JS |
672 | } |
673 | ||
89b831ef | 674 | /* get notified when a cpu comes on/off */ |
1cb2a8e1 IM |
675 | static void __cpuinit |
676 | amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu) | |
89b831ef | 677 | { |
89b831ef JS |
678 | switch (action) { |
679 | case CPU_ONLINE: | |
8bb78442 | 680 | case CPU_ONLINE_FROZEN: |
89b831ef | 681 | threshold_create_device(cpu); |
89b831ef JS |
682 | break; |
683 | case CPU_DEAD: | |
8bb78442 | 684 | case CPU_DEAD_FROZEN: |
89b831ef JS |
685 | threshold_remove_device(cpu); |
686 | break; | |
687 | default: | |
688 | break; | |
689 | } | |
89b831ef JS |
690 | } |
691 | ||
89b831ef JS |
692 | static __init int threshold_init_device(void) |
693 | { | |
2903ee85 | 694 | unsigned lcpu = 0; |
89b831ef | 695 | |
89b831ef JS |
696 | /* to hit CPUs online before the notifier is up */ |
697 | for_each_online_cpu(lcpu) { | |
fff2e89f | 698 | int err = threshold_create_device(lcpu); |
1cb2a8e1 | 699 | |
89b831ef | 700 | if (err) |
fff2e89f | 701 | return err; |
89b831ef | 702 | } |
8735728e | 703 | threshold_cpu_callback = amd_64_threshold_cpu_callback; |
1cb2a8e1 | 704 | |
fff2e89f | 705 | return 0; |
89b831ef | 706 | } |
89b831ef | 707 | device_initcall(threshold_init_device); |