18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
38c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
48c2ecf20Sopenharmony_ci * for more details.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2012 Cavium, Inc.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 2009 Wind River Systems,
98c2ecf20Sopenharmony_ci *   written by Ralf Baechle <ralf@linux-mips.org>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
158c2ecf20Sopenharmony_ci#include <linux/io.h>
168c2ecf20Sopenharmony_ci#include <linux/edac.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "edac_module.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx.h>
218c2ecf20Sopenharmony_ci#include <asm/mipsregs.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciextern int register_co_cache_error_notifier(struct notifier_block *nb);
248c2ecf20Sopenharmony_ciextern int unregister_co_cache_error_notifier(struct notifier_block *nb);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciextern unsigned long long cache_err_dcache[NR_CPUS];
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct co_cache_error {
298c2ecf20Sopenharmony_ci	struct notifier_block notifier;
308c2ecf20Sopenharmony_ci	struct edac_device_ctl_info *ed;
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/**
348c2ecf20Sopenharmony_ci * EDAC CPU cache error callback
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * @event: non-zero if unrecoverable.
378c2ecf20Sopenharmony_ci */
388c2ecf20Sopenharmony_cistatic int  co_cache_error_event(struct notifier_block *this,
398c2ecf20Sopenharmony_ci	unsigned long event, void *ptr)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct co_cache_error *p = container_of(this, struct co_cache_error,
428c2ecf20Sopenharmony_ci						notifier);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	unsigned int core = cvmx_get_core_num();
458c2ecf20Sopenharmony_ci	unsigned int cpu = smp_processor_id();
468c2ecf20Sopenharmony_ci	u64 icache_err = read_octeon_c0_icacheerr();
478c2ecf20Sopenharmony_ci	u64 dcache_err;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (event) {
508c2ecf20Sopenharmony_ci		dcache_err = cache_err_dcache[core];
518c2ecf20Sopenharmony_ci		cache_err_dcache[core] = 0;
528c2ecf20Sopenharmony_ci	} else {
538c2ecf20Sopenharmony_ci		dcache_err = read_octeon_c0_dcacheerr();
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	if (icache_err & 1) {
578c2ecf20Sopenharmony_ci		edac_device_printk(p->ed, KERN_ERR,
588c2ecf20Sopenharmony_ci				   "CacheErr (Icache):%llx, core %d/cpu %d, cp0_errorepc == %lx\n",
598c2ecf20Sopenharmony_ci				   (unsigned long long)icache_err, core, cpu,
608c2ecf20Sopenharmony_ci				   read_c0_errorepc());
618c2ecf20Sopenharmony_ci		write_octeon_c0_icacheerr(0);
628c2ecf20Sopenharmony_ci		edac_device_handle_ce(p->ed, cpu, 1, "icache");
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci	if (dcache_err & 1) {
658c2ecf20Sopenharmony_ci		edac_device_printk(p->ed, KERN_ERR,
668c2ecf20Sopenharmony_ci				   "CacheErr (Dcache):%llx, core %d/cpu %d, cp0_errorepc == %lx\n",
678c2ecf20Sopenharmony_ci				   (unsigned long long)dcache_err, core, cpu,
688c2ecf20Sopenharmony_ci				   read_c0_errorepc());
698c2ecf20Sopenharmony_ci		if (event)
708c2ecf20Sopenharmony_ci			edac_device_handle_ue(p->ed, cpu, 0, "dcache");
718c2ecf20Sopenharmony_ci		else
728c2ecf20Sopenharmony_ci			edac_device_handle_ce(p->ed, cpu, 0, "dcache");
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		/* Clear the error indication */
758c2ecf20Sopenharmony_ci		if (OCTEON_IS_OCTEON2())
768c2ecf20Sopenharmony_ci			write_octeon_c0_dcacheerr(1);
778c2ecf20Sopenharmony_ci		else
788c2ecf20Sopenharmony_ci			write_octeon_c0_dcacheerr(0);
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return NOTIFY_STOP;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int co_cache_error_probe(struct platform_device *pdev)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct co_cache_error *p = devm_kzalloc(&pdev->dev, sizeof(*p),
878c2ecf20Sopenharmony_ci						GFP_KERNEL);
888c2ecf20Sopenharmony_ci	if (!p)
898c2ecf20Sopenharmony_ci		return -ENOMEM;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	p->notifier.notifier_call = co_cache_error_event;
928c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, p);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	p->ed = edac_device_alloc_ctl_info(0, "cpu", num_possible_cpus(),
958c2ecf20Sopenharmony_ci					   "cache", 2, 0, NULL, 0,
968c2ecf20Sopenharmony_ci					   edac_device_alloc_index());
978c2ecf20Sopenharmony_ci	if (!p->ed)
988c2ecf20Sopenharmony_ci		goto err;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	p->ed->dev = &pdev->dev;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	p->ed->dev_name = dev_name(&pdev->dev);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	p->ed->mod_name = "octeon-cpu";
1058c2ecf20Sopenharmony_ci	p->ed->ctl_name = "cache";
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	if (edac_device_add_device(p->ed)) {
1088c2ecf20Sopenharmony_ci		pr_err("%s: edac_device_add_device() failed\n", __func__);
1098c2ecf20Sopenharmony_ci		goto err1;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	register_co_cache_error_notifier(&p->notifier);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cierr1:
1178c2ecf20Sopenharmony_ci	edac_device_free_ctl_info(p->ed);
1188c2ecf20Sopenharmony_cierr:
1198c2ecf20Sopenharmony_ci	return -ENXIO;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic int co_cache_error_remove(struct platform_device *pdev)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct co_cache_error *p = platform_get_drvdata(pdev);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	unregister_co_cache_error_notifier(&p->notifier);
1278c2ecf20Sopenharmony_ci	edac_device_del_device(&pdev->dev);
1288c2ecf20Sopenharmony_ci	edac_device_free_ctl_info(p->ed);
1298c2ecf20Sopenharmony_ci	return 0;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic struct platform_driver co_cache_error_driver = {
1338c2ecf20Sopenharmony_ci	.probe = co_cache_error_probe,
1348c2ecf20Sopenharmony_ci	.remove = co_cache_error_remove,
1358c2ecf20Sopenharmony_ci	.driver = {
1368c2ecf20Sopenharmony_ci		   .name = "octeon_pc_edac",
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_cimodule_platform_driver(co_cache_error_driver);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1428c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
143