xref: /kernel/linux/linux-5.10/drivers/edac/edac_mc.h (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Defines, structures, APIs for edac_mc module
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * (C) 2007 Linux Networx (http://lnxi.com)
58c2ecf20Sopenharmony_ci * This file may be distributed under the terms of the
68c2ecf20Sopenharmony_ci * GNU General Public License.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Written by Thayne Harbaugh
98c2ecf20Sopenharmony_ci * Based on work by Dan Hollis <goemon at anime dot net> and others.
108c2ecf20Sopenharmony_ci *	http://www.anime.net/~goemon/linux-ecc/
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * NMI handling support added by
138c2ecf20Sopenharmony_ci *     Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Refactored for multi-source files:
168c2ecf20Sopenharmony_ci *	Doug Thompson <norsk5@xmission.com>
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Please look at Documentation/driver-api/edac.rst for more info about
198c2ecf20Sopenharmony_ci * EDAC core structs and functions.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#ifndef _EDAC_MC_H_
238c2ecf20Sopenharmony_ci#define _EDAC_MC_H_
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/kernel.h>
268c2ecf20Sopenharmony_ci#include <linux/types.h>
278c2ecf20Sopenharmony_ci#include <linux/module.h>
288c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
298c2ecf20Sopenharmony_ci#include <linux/smp.h>
308c2ecf20Sopenharmony_ci#include <linux/pci.h>
318c2ecf20Sopenharmony_ci#include <linux/time.h>
328c2ecf20Sopenharmony_ci#include <linux/nmi.h>
338c2ecf20Sopenharmony_ci#include <linux/rcupdate.h>
348c2ecf20Sopenharmony_ci#include <linux/completion.h>
358c2ecf20Sopenharmony_ci#include <linux/kobject.h>
368c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
378c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
388c2ecf20Sopenharmony_ci#include <linux/edac.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#if PAGE_SHIFT < 20
418c2ecf20Sopenharmony_ci#define PAGES_TO_MiB(pages)	((pages) >> (20 - PAGE_SHIFT))
428c2ecf20Sopenharmony_ci#define MiB_TO_PAGES(mb)	((mb) << (20 - PAGE_SHIFT))
438c2ecf20Sopenharmony_ci#else				/* PAGE_SHIFT > 20 */
448c2ecf20Sopenharmony_ci#define PAGES_TO_MiB(pages)	((pages) << (PAGE_SHIFT - 20))
458c2ecf20Sopenharmony_ci#define MiB_TO_PAGES(mb)	((mb) >> (PAGE_SHIFT - 20))
468c2ecf20Sopenharmony_ci#endif
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define edac_printk(level, prefix, fmt, arg...) \
498c2ecf20Sopenharmony_ci	printk(level "EDAC " prefix ": " fmt, ##arg)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define edac_mc_printk(mci, level, fmt, arg...) \
528c2ecf20Sopenharmony_ci	printk(level "EDAC MC%d: " fmt, mci->mc_idx, ##arg)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define edac_mc_chipset_printk(mci, level, prefix, fmt, arg...) \
558c2ecf20Sopenharmony_ci	printk(level "EDAC " prefix " MC%d: " fmt, mci->mc_idx, ##arg)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define edac_device_printk(ctl, level, fmt, arg...) \
588c2ecf20Sopenharmony_ci	printk(level "EDAC DEVICE%d: " fmt, ctl->dev_idx, ##arg)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define edac_pci_printk(ctl, level, fmt, arg...) \
618c2ecf20Sopenharmony_ci	printk(level "EDAC PCI%d: " fmt, ctl->pci_idx, ##arg)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/* prefixes for edac_printk() and edac_mc_printk() */
648c2ecf20Sopenharmony_ci#define EDAC_MC "MC"
658c2ecf20Sopenharmony_ci#define EDAC_PCI "PCI"
668c2ecf20Sopenharmony_ci#define EDAC_DEBUG "DEBUG"
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciextern const char * const edac_mem_types[];
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#ifdef CONFIG_EDAC_DEBUG
718c2ecf20Sopenharmony_ciextern int edac_debug_level;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define edac_dbg(level, fmt, ...)					\
748c2ecf20Sopenharmony_cido {									\
758c2ecf20Sopenharmony_ci	if (level <= edac_debug_level)					\
768c2ecf20Sopenharmony_ci		edac_printk(KERN_DEBUG, EDAC_DEBUG,			\
778c2ecf20Sopenharmony_ci			    "%s: " fmt, __func__, ##__VA_ARGS__);	\
788c2ecf20Sopenharmony_ci} while (0)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#else				/* !CONFIG_EDAC_DEBUG */
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define edac_dbg(level, fmt, ...)					\
838c2ecf20Sopenharmony_cido {									\
848c2ecf20Sopenharmony_ci	if (0)								\
858c2ecf20Sopenharmony_ci		edac_printk(KERN_DEBUG, EDAC_DEBUG,			\
868c2ecf20Sopenharmony_ci			    "%s: " fmt, __func__, ##__VA_ARGS__);	\
878c2ecf20Sopenharmony_ci} while (0)
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#endif				/* !CONFIG_EDAC_DEBUG */
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define PCI_VEND_DEV(vend, dev) PCI_VENDOR_ID_ ## vend, \
928c2ecf20Sopenharmony_ci	PCI_DEVICE_ID_ ## vend ## _ ## dev
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define edac_dev_name(dev) (dev)->dev_name
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define to_mci(k) container_of(k, struct mem_ctl_info, dev)
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/**
998c2ecf20Sopenharmony_ci * edac_mc_alloc() - Allocate and partially fill a struct &mem_ctl_info.
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * @mc_num:		Memory controller number
1028c2ecf20Sopenharmony_ci * @n_layers:		Number of MC hierarchy layers
1038c2ecf20Sopenharmony_ci * @layers:		Describes each layer as seen by the Memory Controller
1048c2ecf20Sopenharmony_ci * @sz_pvt:		size of private storage needed
1058c2ecf20Sopenharmony_ci *
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci * Everything is kmalloc'ed as one big chunk - more efficient.
1088c2ecf20Sopenharmony_ci * Only can be used if all structures have the same lifetime - otherwise
1098c2ecf20Sopenharmony_ci * you have to allocate and initialize your own structures.
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci * Use edac_mc_free() to free mc structures allocated by this function.
1128c2ecf20Sopenharmony_ci *
1138c2ecf20Sopenharmony_ci * .. note::
1148c2ecf20Sopenharmony_ci *
1158c2ecf20Sopenharmony_ci *   drivers handle multi-rank memories in different ways: in some
1168c2ecf20Sopenharmony_ci *   drivers, one multi-rank memory stick is mapped as one entry, while, in
1178c2ecf20Sopenharmony_ci *   others, a single multi-rank memory stick would be mapped into several
1188c2ecf20Sopenharmony_ci *   entries. Currently, this function will allocate multiple struct dimm_info
1198c2ecf20Sopenharmony_ci *   on such scenarios, as grouping the multiple ranks require drivers change.
1208c2ecf20Sopenharmony_ci *
1218c2ecf20Sopenharmony_ci * Returns:
1228c2ecf20Sopenharmony_ci *	On success, return a pointer to struct mem_ctl_info pointer;
1238c2ecf20Sopenharmony_ci *	%NULL otherwise
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_cistruct mem_ctl_info *edac_mc_alloc(unsigned int mc_num,
1268c2ecf20Sopenharmony_ci				   unsigned int n_layers,
1278c2ecf20Sopenharmony_ci				   struct edac_mc_layer *layers,
1288c2ecf20Sopenharmony_ci				   unsigned int sz_pvt);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/**
1318c2ecf20Sopenharmony_ci * edac_get_owner - Return the owner's mod_name of EDAC MC
1328c2ecf20Sopenharmony_ci *
1338c2ecf20Sopenharmony_ci * Returns:
1348c2ecf20Sopenharmony_ci *	Pointer to mod_name string when EDAC MC is owned. NULL otherwise.
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_ciextern const char *edac_get_owner(void);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/*
1398c2ecf20Sopenharmony_ci * edac_mc_add_mc_with_groups() - Insert the @mci structure into the mci
1408c2ecf20Sopenharmony_ci *	global list and create sysfs entries associated with @mci structure.
1418c2ecf20Sopenharmony_ci *
1428c2ecf20Sopenharmony_ci * @mci: pointer to the mci structure to be added to the list
1438c2ecf20Sopenharmony_ci * @groups: optional attribute groups for the driver-specific sysfs entries
1448c2ecf20Sopenharmony_ci *
1458c2ecf20Sopenharmony_ci * Returns:
1468c2ecf20Sopenharmony_ci *	0 on Success, or an error code on failure
1478c2ecf20Sopenharmony_ci */
1488c2ecf20Sopenharmony_ciextern int edac_mc_add_mc_with_groups(struct mem_ctl_info *mci,
1498c2ecf20Sopenharmony_ci				      const struct attribute_group **groups);
1508c2ecf20Sopenharmony_ci#define edac_mc_add_mc(mci)	edac_mc_add_mc_with_groups(mci, NULL)
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/**
1538c2ecf20Sopenharmony_ci * edac_mc_free() -  Frees a previously allocated @mci structure
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * @mci: pointer to a struct mem_ctl_info structure
1568c2ecf20Sopenharmony_ci */
1578c2ecf20Sopenharmony_ciextern void edac_mc_free(struct mem_ctl_info *mci);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/**
1608c2ecf20Sopenharmony_ci * edac_has_mcs() - Check if any MCs have been allocated.
1618c2ecf20Sopenharmony_ci *
1628c2ecf20Sopenharmony_ci * Returns:
1638c2ecf20Sopenharmony_ci *	True if MC instances have been registered successfully.
1648c2ecf20Sopenharmony_ci *	False otherwise.
1658c2ecf20Sopenharmony_ci */
1668c2ecf20Sopenharmony_ciextern bool edac_has_mcs(void);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/**
1698c2ecf20Sopenharmony_ci * edac_mc_find() - Search for a mem_ctl_info structure whose index is @idx.
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci * @idx: index to be seek
1728c2ecf20Sopenharmony_ci *
1738c2ecf20Sopenharmony_ci * If found, return a pointer to the structure.
1748c2ecf20Sopenharmony_ci * Else return NULL.
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_ciextern struct mem_ctl_info *edac_mc_find(int idx);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/**
1798c2ecf20Sopenharmony_ci * find_mci_by_dev() - Scan list of controllers looking for the one that
1808c2ecf20Sopenharmony_ci *	manages the @dev device.
1818c2ecf20Sopenharmony_ci *
1828c2ecf20Sopenharmony_ci * @dev: pointer to a struct device related with the MCI
1838c2ecf20Sopenharmony_ci *
1848c2ecf20Sopenharmony_ci * Returns: on success, returns a pointer to struct &mem_ctl_info;
1858c2ecf20Sopenharmony_ci * %NULL otherwise.
1868c2ecf20Sopenharmony_ci */
1878c2ecf20Sopenharmony_ciextern struct mem_ctl_info *find_mci_by_dev(struct device *dev);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/**
1908c2ecf20Sopenharmony_ci * edac_mc_del_mc() - Remove sysfs entries for mci structure associated with
1918c2ecf20Sopenharmony_ci *	@dev and remove mci structure from global list.
1928c2ecf20Sopenharmony_ci *
1938c2ecf20Sopenharmony_ci * @dev: Pointer to struct &device representing mci structure to remove.
1948c2ecf20Sopenharmony_ci *
1958c2ecf20Sopenharmony_ci * Returns: pointer to removed mci structure, or %NULL if device not found.
1968c2ecf20Sopenharmony_ci */
1978c2ecf20Sopenharmony_ciextern struct mem_ctl_info *edac_mc_del_mc(struct device *dev);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/**
2008c2ecf20Sopenharmony_ci * edac_mc_find_csrow_by_page() - Ancillary routine to identify what csrow
2018c2ecf20Sopenharmony_ci *	contains a memory page.
2028c2ecf20Sopenharmony_ci *
2038c2ecf20Sopenharmony_ci * @mci: pointer to a struct mem_ctl_info structure
2048c2ecf20Sopenharmony_ci * @page: memory page to find
2058c2ecf20Sopenharmony_ci *
2068c2ecf20Sopenharmony_ci * Returns: on success, returns the csrow. -1 if not found.
2078c2ecf20Sopenharmony_ci */
2088c2ecf20Sopenharmony_ciextern int edac_mc_find_csrow_by_page(struct mem_ctl_info *mci,
2098c2ecf20Sopenharmony_ci				      unsigned long page);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/**
2128c2ecf20Sopenharmony_ci * edac_raw_mc_handle_error() - Reports a memory event to userspace without
2138c2ecf20Sopenharmony_ci *	doing anything to discover the error location.
2148c2ecf20Sopenharmony_ci *
2158c2ecf20Sopenharmony_ci * @e:			error description
2168c2ecf20Sopenharmony_ci *
2178c2ecf20Sopenharmony_ci * This raw function is used internally by edac_mc_handle_error(). It should
2188c2ecf20Sopenharmony_ci * only be called directly when the hardware error come directly from BIOS,
2198c2ecf20Sopenharmony_ci * like in the case of APEI GHES driver.
2208c2ecf20Sopenharmony_ci */
2218c2ecf20Sopenharmony_civoid edac_raw_mc_handle_error(struct edac_raw_error_desc *e);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci/**
2248c2ecf20Sopenharmony_ci * edac_mc_handle_error() - Reports a memory event to userspace.
2258c2ecf20Sopenharmony_ci *
2268c2ecf20Sopenharmony_ci * @type:		severity of the error (CE/UE/Fatal)
2278c2ecf20Sopenharmony_ci * @mci:		a struct mem_ctl_info pointer
2288c2ecf20Sopenharmony_ci * @error_count:	Number of errors of the same type
2298c2ecf20Sopenharmony_ci * @page_frame_number:	mem page where the error occurred
2308c2ecf20Sopenharmony_ci * @offset_in_page:	offset of the error inside the page
2318c2ecf20Sopenharmony_ci * @syndrome:		ECC syndrome
2328c2ecf20Sopenharmony_ci * @top_layer:		Memory layer[0] position
2338c2ecf20Sopenharmony_ci * @mid_layer:		Memory layer[1] position
2348c2ecf20Sopenharmony_ci * @low_layer:		Memory layer[2] position
2358c2ecf20Sopenharmony_ci * @msg:		Message meaningful to the end users that
2368c2ecf20Sopenharmony_ci *			explains the event
2378c2ecf20Sopenharmony_ci * @other_detail:	Technical details about the event that
2388c2ecf20Sopenharmony_ci *			may help hardware manufacturers and
2398c2ecf20Sopenharmony_ci *			EDAC developers to analyse the event
2408c2ecf20Sopenharmony_ci */
2418c2ecf20Sopenharmony_civoid edac_mc_handle_error(const enum hw_event_mc_err_type type,
2428c2ecf20Sopenharmony_ci			  struct mem_ctl_info *mci,
2438c2ecf20Sopenharmony_ci			  const u16 error_count,
2448c2ecf20Sopenharmony_ci			  const unsigned long page_frame_number,
2458c2ecf20Sopenharmony_ci			  const unsigned long offset_in_page,
2468c2ecf20Sopenharmony_ci			  const unsigned long syndrome,
2478c2ecf20Sopenharmony_ci			  const int top_layer,
2488c2ecf20Sopenharmony_ci			  const int mid_layer,
2498c2ecf20Sopenharmony_ci			  const int low_layer,
2508c2ecf20Sopenharmony_ci			  const char *msg,
2518c2ecf20Sopenharmony_ci			  const char *other_detail);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci/*
2548c2ecf20Sopenharmony_ci * edac misc APIs
2558c2ecf20Sopenharmony_ci */
2568c2ecf20Sopenharmony_ciextern char *edac_op_state_to_string(int op_state);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci#endif				/* _EDAC_MC_H_ */
259