Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | 78d88e8 | 2016-10-29 15:16:34 -0200 | [diff] [blame] | 2 | * Defines, structures, APIs for edac_mc module |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 3 | * |
| 4 | * (C) 2007 Linux Networx (http://lnxi.com) |
| 5 | * This file may be distributed under the terms of the |
| 6 | * GNU General Public License. |
| 7 | * |
| 8 | * Written by Thayne Harbaugh |
| 9 | * Based on work by Dan Hollis <goemon at anime dot net> and others. |
| 10 | * http://www.anime.net/~goemon/linux-ecc/ |
| 11 | * |
| 12 | * NMI handling support added by |
| 13 | * Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com> |
| 14 | * |
| 15 | * Refactored for multi-source files: |
| 16 | * Doug Thompson <norsk5@xmission.com> |
| 17 | * |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 18 | * Please look at Documentation/driver-api/edac.rst for more info about |
| 19 | * EDAC core structs and functions. |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Mauro Carvalho Chehab | 78d88e8 | 2016-10-29 15:16:34 -0200 | [diff] [blame] | 22 | #ifndef _EDAC_MC_H_ |
| 23 | #define _EDAC_MC_H_ |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/smp.h> |
| 30 | #include <linux/pci.h> |
| 31 | #include <linux/time.h> |
| 32 | #include <linux/nmi.h> |
| 33 | #include <linux/rcupdate.h> |
| 34 | #include <linux/completion.h> |
| 35 | #include <linux/kobject.h> |
| 36 | #include <linux/platform_device.h> |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 37 | #include <linux/workqueue.h> |
Mauro Carvalho Chehab | ddeb354 | 2011-03-04 15:11:29 -0300 | [diff] [blame] | 38 | #include <linux/edac.h> |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 39 | |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 40 | #if PAGE_SHIFT < 20 |
Andrei Konovalov | 76f04f2 | 2010-12-07 07:48:00 -0500 | [diff] [blame] | 41 | #define PAGES_TO_MiB(pages) ((pages) >> (20 - PAGE_SHIFT)) |
| 42 | #define MiB_TO_PAGES(mb) ((mb) << (20 - PAGE_SHIFT)) |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 43 | #else /* PAGE_SHIFT > 20 */ |
Andrei Konovalov | 76f04f2 | 2010-12-07 07:48:00 -0500 | [diff] [blame] | 44 | #define PAGES_TO_MiB(pages) ((pages) << (PAGE_SHIFT - 20)) |
Mauro Carvalho Chehab | e914460 | 2010-08-10 20:26:35 -0300 | [diff] [blame] | 45 | #define MiB_TO_PAGES(mb) ((mb) >> (PAGE_SHIFT - 20)) |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | #define edac_printk(level, prefix, fmt, arg...) \ |
| 49 | printk(level "EDAC " prefix ": " fmt, ##arg) |
| 50 | |
| 51 | #define edac_mc_printk(mci, level, fmt, arg...) \ |
| 52 | printk(level "EDAC MC%d: " fmt, mci->mc_idx, ##arg) |
| 53 | |
| 54 | #define edac_mc_chipset_printk(mci, level, prefix, fmt, arg...) \ |
| 55 | printk(level "EDAC " prefix " MC%d: " fmt, mci->mc_idx, ##arg) |
| 56 | |
Douglas Thompson | e27e3da | 2007-07-19 01:49:36 -0700 | [diff] [blame] | 57 | #define edac_device_printk(ctl, level, fmt, arg...) \ |
| 58 | printk(level "EDAC DEVICE%d: " fmt, ctl->dev_idx, ##arg) |
| 59 | |
Dave Jiang | 91b9904 | 2007-07-19 01:49:52 -0700 | [diff] [blame] | 60 | #define edac_pci_printk(ctl, level, fmt, arg...) \ |
| 61 | printk(level "EDAC PCI%d: " fmt, ctl->pci_idx, ##arg) |
| 62 | |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 63 | /* prefixes for edac_printk() and edac_mc_printk() */ |
| 64 | #define EDAC_MC "MC" |
| 65 | #define EDAC_PCI "PCI" |
| 66 | #define EDAC_DEBUG "DEBUG" |
| 67 | |
Borislav Petkov | f4ce6ec | 2014-08-13 23:27:55 +0200 | [diff] [blame] | 68 | extern const char * const edac_mem_types[]; |
Borislav Petkov | 24f9a7f | 2010-10-07 18:29:15 +0200 | [diff] [blame] | 69 | |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 70 | #ifdef CONFIG_EDAC_DEBUG |
| 71 | extern int edac_debug_level; |
| 72 | |
Joe Perches | 956b9ba | 2012-04-29 17:08:39 -0300 | [diff] [blame] | 73 | #define edac_dbg(level, fmt, ...) \ |
Joe Perches | 7e88185 | 2012-04-28 16:41:46 -0300 | [diff] [blame] | 74 | do { \ |
| 75 | if (level <= edac_debug_level) \ |
| 76 | edac_printk(KERN_DEBUG, EDAC_DEBUG, \ |
| 77 | "%s: " fmt, __func__, ##__VA_ARGS__); \ |
| 78 | } while (0) |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 79 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 80 | #else /* !CONFIG_EDAC_DEBUG */ |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 81 | |
Joe Perches | 956b9ba | 2012-04-29 17:08:39 -0300 | [diff] [blame] | 82 | #define edac_dbg(level, fmt, ...) \ |
Joe Perches | 7e88185 | 2012-04-28 16:41:46 -0300 | [diff] [blame] | 83 | do { \ |
| 84 | if (0) \ |
| 85 | edac_printk(KERN_DEBUG, EDAC_DEBUG, \ |
| 86 | "%s: " fmt, __func__, ##__VA_ARGS__); \ |
| 87 | } while (0) |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 88 | |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 89 | #endif /* !CONFIG_EDAC_DEBUG */ |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 90 | |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 91 | #define PCI_VEND_DEV(vend, dev) PCI_VENDOR_ID_ ## vend, \ |
| 92 | PCI_DEVICE_ID_ ## vend ## _ ## dev |
| 93 | |
Stephen Rothwell | 17aa7e0 | 2008-05-05 13:54:19 +1000 | [diff] [blame] | 94 | #define edac_dev_name(dev) (dev)->dev_name |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 95 | |
Borislav Petkov | 7ac8bf9 | 2015-09-22 11:56:04 +0200 | [diff] [blame] | 96 | #define to_mci(k) container_of(k, struct mem_ctl_info, dev) |
| 97 | |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 98 | /** |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 99 | * edac_mc_alloc() - Allocate and partially fill a struct &mem_ctl_info. |
| 100 | * |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 101 | * @mc_num: Memory controller number |
| 102 | * @n_layers: Number of MC hierarchy layers |
| 103 | * @layers: Describes each layer as seen by the Memory Controller |
| 104 | * @sz_pvt: size of private storage needed |
| 105 | * |
| 106 | * |
| 107 | * Everything is kmalloc'ed as one big chunk - more efficient. |
| 108 | * Only can be used if all structures have the same lifetime - otherwise |
| 109 | * you have to allocate and initialize your own structures. |
| 110 | * |
| 111 | * Use edac_mc_free() to free mc structures allocated by this function. |
| 112 | * |
| 113 | * .. note:: |
| 114 | * |
| 115 | * drivers handle multi-rank memories in different ways: in some |
| 116 | * drivers, one multi-rank memory stick is mapped as one entry, while, in |
| 117 | * others, a single multi-rank memory stick would be mapped into several |
| 118 | * entries. Currently, this function will allocate multiple struct dimm_info |
| 119 | * on such scenarios, as grouping the multiple ranks require drivers change. |
| 120 | * |
| 121 | * Returns: |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 122 | * On success, return a pointer to struct mem_ctl_info pointer; |
| 123 | * %NULL otherwise |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 124 | */ |
Mauro Carvalho Chehab | ca0907b | 2012-05-02 14:37:00 -0300 | [diff] [blame] | 125 | struct mem_ctl_info *edac_mc_alloc(unsigned mc_num, |
Mauro Carvalho Chehab | 4275be6 | 2012-04-18 15:20:50 -0300 | [diff] [blame] | 126 | unsigned n_layers, |
| 127 | struct edac_mc_layer *layers, |
| 128 | unsigned sz_pvt); |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 129 | |
| 130 | /** |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 131 | * edac_mc_add_mc_with_groups() - Insert the @mci structure into the mci |
| 132 | * global list and create sysfs entries associated with @mci structure. |
| 133 | * |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 134 | * @mci: pointer to the mci structure to be added to the list |
| 135 | * @groups: optional attribute groups for the driver-specific sysfs entries |
| 136 | * |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 137 | * Returns: |
| 138 | * 0 on Success, or an error code on failure |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 139 | */ |
Takashi Iwai | 4e8d230 | 2015-02-04 11:48:52 +0100 | [diff] [blame] | 140 | extern int edac_mc_add_mc_with_groups(struct mem_ctl_info *mci, |
| 141 | const struct attribute_group **groups); |
| 142 | #define edac_mc_add_mc(mci) edac_mc_add_mc_with_groups(mci, NULL) |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 143 | |
| 144 | /** |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 145 | * edac_mc_free() - Frees a previously allocated @mci structure |
| 146 | * |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 147 | * @mci: pointer to a struct mem_ctl_info structure |
| 148 | */ |
Doug Thompson | b8f6f97 | 2007-07-19 01:50:26 -0700 | [diff] [blame] | 149 | extern void edac_mc_free(struct mem_ctl_info *mci); |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 150 | |
| 151 | /** |
Yazen Ghannam | d7fc9d7 | 2017-01-27 11:24:21 -0600 | [diff] [blame] | 152 | * edac_has_mcs() - Check if any MCs have been allocated. |
| 153 | * |
| 154 | * Returns: |
| 155 | * True if MC instances have been registered successfully. |
| 156 | * False otherwise. |
| 157 | */ |
| 158 | extern bool edac_has_mcs(void); |
| 159 | |
| 160 | /** |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 161 | * edac_mc_find() - Search for a mem_ctl_info structure whose index is @idx. |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 162 | * |
| 163 | * @idx: index to be seek |
| 164 | * |
| 165 | * If found, return a pointer to the structure. |
| 166 | * Else return NULL. |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 167 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 168 | extern struct mem_ctl_info *edac_mc_find(int idx); |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 169 | |
| 170 | /** |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 171 | * find_mci_by_dev() - Scan list of controllers looking for the one that |
| 172 | * manages the @dev device. |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 173 | * |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 174 | * @dev: pointer to a struct device related with the MCI |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 175 | * |
| 176 | * Returns: on success, returns a pointer to struct &mem_ctl_info; |
| 177 | * %NULL otherwise. |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 178 | */ |
Mauro Carvalho Chehab | 939747bd | 2010-08-10 11:22:01 -0300 | [diff] [blame] | 179 | extern struct mem_ctl_info *find_mci_by_dev(struct device *dev); |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 180 | |
| 181 | /** |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 182 | * edac_mc_del_mc() - Remove sysfs entries for mci structure associated with |
| 183 | * @dev and remove mci structure from global list. |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 184 | * |
| 185 | * @dev: Pointer to struct &device representing mci structure to remove. |
| 186 | * |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 187 | * Returns: pointer to removed mci structure, or %NULL if device not found. |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 188 | */ |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 189 | extern struct mem_ctl_info *edac_mc_del_mc(struct device *dev); |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * edac_mc_find_csrow_by_page() - Ancillary routine to identify what csrow |
| 193 | * contains a memory page. |
| 194 | * |
| 195 | * @mci: pointer to a struct mem_ctl_info structure |
| 196 | * @page: memory page to find |
| 197 | * |
| 198 | * Returns: on success, returns the csrow. -1 if not found. |
| 199 | */ |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 200 | extern int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci, |
Douglas Thompson | 079708b | 2007-07-19 01:49:58 -0700 | [diff] [blame] | 201 | unsigned long page); |
Mauro Carvalho Chehab | e7e2483 | 2012-10-31 13:46:11 -0300 | [diff] [blame] | 202 | |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 203 | /** |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 204 | * edac_raw_mc_handle_error() - Reports a memory event to userspace without |
| 205 | * doing anything to discover the error location. |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 206 | * |
| 207 | * @type: severity of the error (CE/UE/Fatal) |
| 208 | * @mci: a struct mem_ctl_info pointer |
| 209 | * @e: error description |
| 210 | * |
| 211 | * This raw function is used internally by edac_mc_handle_error(). It should |
| 212 | * only be called directly when the hardware error come directly from BIOS, |
| 213 | * like in the case of APEI GHES driver. |
| 214 | */ |
Mauro Carvalho Chehab | e7e2483 | 2012-10-31 13:46:11 -0300 | [diff] [blame] | 215 | void edac_raw_mc_handle_error(const enum hw_event_mc_err_type type, |
| 216 | struct mem_ctl_info *mci, |
| 217 | struct edac_raw_error_desc *e); |
| 218 | |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 219 | /** |
Mauro Carvalho Chehab | 66c222a | 2016-10-29 10:35:23 -0200 | [diff] [blame] | 220 | * edac_mc_handle_error() - Reports a memory event to userspace. |
Mauro Carvalho Chehab | e01aa14 | 2016-10-26 15:47:55 -0200 | [diff] [blame] | 221 | * |
| 222 | * @type: severity of the error (CE/UE/Fatal) |
| 223 | * @mci: a struct mem_ctl_info pointer |
| 224 | * @error_count: Number of errors of the same type |
| 225 | * @page_frame_number: mem page where the error occurred |
| 226 | * @offset_in_page: offset of the error inside the page |
| 227 | * @syndrome: ECC syndrome |
| 228 | * @top_layer: Memory layer[0] position |
| 229 | * @mid_layer: Memory layer[1] position |
| 230 | * @low_layer: Memory layer[2] position |
| 231 | * @msg: Message meaningful to the end users that |
| 232 | * explains the event |
| 233 | * @other_detail: Technical details about the event that |
| 234 | * may help hardware manufacturers and |
| 235 | * EDAC developers to analyse the event |
| 236 | */ |
Mauro Carvalho Chehab | 4275be6 | 2012-04-18 15:20:50 -0300 | [diff] [blame] | 237 | void edac_mc_handle_error(const enum hw_event_mc_err_type type, |
| 238 | struct mem_ctl_info *mci, |
Mauro Carvalho Chehab | 9eb07a7 | 2012-06-04 13:27:43 -0300 | [diff] [blame] | 239 | const u16 error_count, |
Mauro Carvalho Chehab | 4275be6 | 2012-04-18 15:20:50 -0300 | [diff] [blame] | 240 | const unsigned long page_frame_number, |
| 241 | const unsigned long offset_in_page, |
| 242 | const unsigned long syndrome, |
Mauro Carvalho Chehab | 53f2d02 | 2012-02-23 08:10:34 -0300 | [diff] [blame] | 243 | const int top_layer, |
| 244 | const int mid_layer, |
| 245 | const int low_layer, |
Mauro Carvalho Chehab | 4275be6 | 2012-04-18 15:20:50 -0300 | [diff] [blame] | 246 | const char *msg, |
Mauro Carvalho Chehab | 03f7eae | 2012-06-04 11:29:25 -0300 | [diff] [blame] | 247 | const char *other_detail); |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 248 | |
| 249 | /* |
Dave Jiang | 91b9904 | 2007-07-19 01:49:52 -0700 | [diff] [blame] | 250 | * edac misc APIs |
| 251 | */ |
Douglas Thompson | 494d0d5 | 2007-07-19 01:50:21 -0700 | [diff] [blame] | 252 | extern char *edac_op_state_to_string(int op_state); |
Douglas Thompson | 7c9281d | 2007-07-19 01:49:33 -0700 | [diff] [blame] | 253 | |
Mauro Carvalho Chehab | 78d88e8 | 2016-10-29 15:16:34 -0200 | [diff] [blame] | 254 | #endif /* _EDAC_MC_H_ */ |