xref: /linux/drivers/edac/sifive_edac.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
191abaeaaSYash Shah // SPDX-License-Identifier: GPL-2.0
291abaeaaSYash Shah /*
391abaeaaSYash Shah  * SiFive Platform EDAC Driver
491abaeaaSYash Shah  *
5ca120a79SGreentime Hu  * Copyright (C) 2018-2022 SiFive, Inc.
691abaeaaSYash Shah  *
791abaeaaSYash Shah  * This driver is partially based on octeon_edac-pc.c
891abaeaaSYash Shah  *
991abaeaaSYash Shah  */
1091abaeaaSYash Shah #include <linux/edac.h>
1191abaeaaSYash Shah #include <linux/platform_device.h>
1291abaeaaSYash Shah #include "edac_module.h"
13ca120a79SGreentime Hu #include <soc/sifive/sifive_ccache.h>
1491abaeaaSYash Shah 
1591abaeaaSYash Shah #define DRVNAME "sifive_edac"
1691abaeaaSYash Shah 
1791abaeaaSYash Shah struct sifive_edac_priv {
1891abaeaaSYash Shah 	struct notifier_block notifier;
1991abaeaaSYash Shah 	struct edac_device_ctl_info *dci;
2091abaeaaSYash Shah };
2191abaeaaSYash Shah 
22ad2c302bSRandy Dunlap /*
2391abaeaaSYash Shah  * EDAC error callback
2491abaeaaSYash Shah  *
2591abaeaaSYash Shah  * @event: non-zero if unrecoverable.
2691abaeaaSYash Shah  */
2791abaeaaSYash Shah static
ecc_err_event(struct notifier_block * this,unsigned long event,void * ptr)2891abaeaaSYash Shah int ecc_err_event(struct notifier_block *this, unsigned long event, void *ptr)
2991abaeaaSYash Shah {
3091abaeaaSYash Shah 	const char *msg = (char *)ptr;
3191abaeaaSYash Shah 	struct sifive_edac_priv *p;
3291abaeaaSYash Shah 
3391abaeaaSYash Shah 	p = container_of(this, struct sifive_edac_priv, notifier);
3491abaeaaSYash Shah 
35ca120a79SGreentime Hu 	if (event == SIFIVE_CCACHE_ERR_TYPE_UE)
3691abaeaaSYash Shah 		edac_device_handle_ue(p->dci, 0, 0, msg);
37ca120a79SGreentime Hu 	else if (event == SIFIVE_CCACHE_ERR_TYPE_CE)
3891abaeaaSYash Shah 		edac_device_handle_ce(p->dci, 0, 0, msg);
3991abaeaaSYash Shah 
4091abaeaaSYash Shah 	return NOTIFY_OK;
4191abaeaaSYash Shah }
4291abaeaaSYash Shah 
ecc_register(struct platform_device * pdev)4391abaeaaSYash Shah static int ecc_register(struct platform_device *pdev)
4491abaeaaSYash Shah {
4591abaeaaSYash Shah 	struct sifive_edac_priv *p;
4691abaeaaSYash Shah 
4791abaeaaSYash Shah 	p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
4891abaeaaSYash Shah 	if (!p)
4991abaeaaSYash Shah 		return -ENOMEM;
5091abaeaaSYash Shah 
5191abaeaaSYash Shah 	p->notifier.notifier_call = ecc_err_event;
5291abaeaaSYash Shah 	platform_set_drvdata(pdev, p);
5391abaeaaSYash Shah 
5491abaeaaSYash Shah 	p->dci = edac_device_alloc_ctl_info(0, "sifive_ecc", 1, "sifive_ecc",
55*48bc8869SJiri Slaby (SUSE) 					    1, 1, edac_device_alloc_index());
566cd18453SWei Yongjun 	if (!p->dci)
576cd18453SWei Yongjun 		return -ENOMEM;
5891abaeaaSYash Shah 
5991abaeaaSYash Shah 	p->dci->dev = &pdev->dev;
6091abaeaaSYash Shah 	p->dci->mod_name = "Sifive ECC Manager";
6191abaeaaSYash Shah 	p->dci->ctl_name = dev_name(&pdev->dev);
6291abaeaaSYash Shah 	p->dci->dev_name = dev_name(&pdev->dev);
6391abaeaaSYash Shah 
6491abaeaaSYash Shah 	if (edac_device_add_device(p->dci)) {
6591abaeaaSYash Shah 		dev_err(p->dci->dev, "failed to register with EDAC core\n");
6691abaeaaSYash Shah 		goto err;
6791abaeaaSYash Shah 	}
6891abaeaaSYash Shah 
69ca120a79SGreentime Hu 	register_sifive_ccache_error_notifier(&p->notifier);
7091abaeaaSYash Shah 
7191abaeaaSYash Shah 	return 0;
7291abaeaaSYash Shah 
7391abaeaaSYash Shah err:
7491abaeaaSYash Shah 	edac_device_free_ctl_info(p->dci);
7591abaeaaSYash Shah 
7691abaeaaSYash Shah 	return -ENXIO;
7791abaeaaSYash Shah }
7891abaeaaSYash Shah 
ecc_unregister(struct platform_device * pdev)7991abaeaaSYash Shah static int ecc_unregister(struct platform_device *pdev)
8091abaeaaSYash Shah {
8191abaeaaSYash Shah 	struct sifive_edac_priv *p = platform_get_drvdata(pdev);
8291abaeaaSYash Shah 
83ca120a79SGreentime Hu 	unregister_sifive_ccache_error_notifier(&p->notifier);
8491abaeaaSYash Shah 	edac_device_del_device(&pdev->dev);
8591abaeaaSYash Shah 	edac_device_free_ctl_info(p->dci);
8691abaeaaSYash Shah 
8791abaeaaSYash Shah 	return 0;
8891abaeaaSYash Shah }
8991abaeaaSYash Shah 
9091abaeaaSYash Shah static struct platform_device *sifive_pdev;
9191abaeaaSYash Shah 
sifive_edac_init(void)9291abaeaaSYash Shah static int __init sifive_edac_init(void)
9391abaeaaSYash Shah {
9491abaeaaSYash Shah 	int ret;
9591abaeaaSYash Shah 
9691abaeaaSYash Shah 	sifive_pdev = platform_device_register_simple(DRVNAME, 0, NULL, 0);
9791abaeaaSYash Shah 	if (IS_ERR(sifive_pdev))
9891abaeaaSYash Shah 		return PTR_ERR(sifive_pdev);
9991abaeaaSYash Shah 
10091abaeaaSYash Shah 	ret = ecc_register(sifive_pdev);
10191abaeaaSYash Shah 	if (ret)
10291abaeaaSYash Shah 		platform_device_unregister(sifive_pdev);
10391abaeaaSYash Shah 
10491abaeaaSYash Shah 	return ret;
10591abaeaaSYash Shah }
10691abaeaaSYash Shah 
sifive_edac_exit(void)10791abaeaaSYash Shah static void __exit sifive_edac_exit(void)
10891abaeaaSYash Shah {
10991abaeaaSYash Shah 	ecc_unregister(sifive_pdev);
11091abaeaaSYash Shah 	platform_device_unregister(sifive_pdev);
11191abaeaaSYash Shah }
11291abaeaaSYash Shah 
11391abaeaaSYash Shah module_init(sifive_edac_init);
11491abaeaaSYash Shah module_exit(sifive_edac_exit);
11591abaeaaSYash Shah 
11691abaeaaSYash Shah MODULE_AUTHOR("SiFive Inc.");
11791abaeaaSYash Shah MODULE_DESCRIPTION("SiFive platform EDAC driver");
11891abaeaaSYash Shah MODULE_LICENSE("GPL v2");
119