1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for Xilinx TMR Inject IP.
4 *
5 * Copyright (C) 2022 Advanced Micro Devices, Inc.
6 *
7 * Description:
8 * This driver is developed for TMR Inject IP,The Triple Modular Redundancy(TMR)
9 * Inject provides fault injection.
10 */
11
12#include <asm/xilinx_mb_manager.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/platform_device.h>
16#include <linux/fault-inject.h>
17
18/* TMR Inject Register offsets */
19#define XTMR_INJECT_CR_OFFSET 0x0
20#define XTMR_INJECT_AIR_OFFSET 0x4
21#define XTMR_INJECT_IIR_OFFSET 0xC
22#define XTMR_INJECT_EAIR_OFFSET 0x10
23#define XTMR_INJECT_ERR_OFFSET 0x204
24
25/* Register Bitmasks/shifts */
26#define XTMR_INJECT_CR_CPUID_SHIFT 8
27#define XTMR_INJECT_CR_IE_SHIFT 10
28#define XTMR_INJECT_IIR_ADDR_MASK GENMASK(31, 16)
29
30#define XTMR_INJECT_MAGIC_MAX_VAL 255
31
32/**
33 * struct xtmr_inject_dev - Driver data for TMR Inject
34 * @regs: device physical base address
35 * @magic: Magic hardware configuration value
36 */
37struct xtmr_inject_dev {
38 void __iomem *regs;
39 u32 magic;
40};
41
42static DECLARE_FAULT_ATTR(inject_fault);
43static char *inject_request;
44module_param(inject_request, charp, 0);
45MODULE_PARM_DESC(inject_request, "default fault injection attributes");
46static struct dentry *dbgfs_root;
47
48/* IO accessors */
49static inline void xtmr_inject_write(struct xtmr_inject_dev *xtmr_inject,
50 u32 addr, u32 value)
51{
52 iowrite32(value, xtmr_inject->regs + addr);
53}
54
55static inline u32 xtmr_inject_read(struct xtmr_inject_dev *xtmr_inject,
56 u32 addr)
57{
58 return ioread32(xtmr_inject->regs + addr);
59}
60
61static int xtmr_inject_set(void *data, u64 val)
62{
63 if (val != 1)
64 return -EINVAL;
65
66 xmb_inject_err();
67 return 0;
68}
69DEFINE_DEBUGFS_ATTRIBUTE(xtmr_inject_fops, NULL, xtmr_inject_set, "%llu\n");
70
71static void xtmr_init_debugfs(struct xtmr_inject_dev *xtmr_inject)
72{
73 struct dentry *dir;
74
75 dbgfs_root = debugfs_create_dir(name: "xtmr_inject", NULL);
76 dir = fault_create_debugfs_attr(name: "inject_fault", parent: dbgfs_root,
77 attr: &inject_fault);
78 debugfs_create_file(name: "inject_fault", mode: 0200, parent: dir, NULL,
79 fops: &xtmr_inject_fops);
80}
81
82static void xtmr_inject_init(struct xtmr_inject_dev *xtmr_inject)
83{
84 u32 cr_val;
85
86 if (inject_request)
87 setup_fault_attr(attr: &inject_fault, str: inject_request);
88 /* Allow fault injection */
89 cr_val = xtmr_inject->magic |
90 (1 << XTMR_INJECT_CR_IE_SHIFT) |
91 (1 << XTMR_INJECT_CR_CPUID_SHIFT);
92 xtmr_inject_write(xtmr_inject, XTMR_INJECT_CR_OFFSET,
93 value: cr_val);
94 /* Initialize the address inject and instruction inject registers */
95 xtmr_inject_write(xtmr_inject, XTMR_INJECT_AIR_OFFSET,
96 value: XMB_INJECT_ERR_OFFSET);
97 xtmr_inject_write(xtmr_inject, XTMR_INJECT_IIR_OFFSET,
98 value: XMB_INJECT_ERR_OFFSET & XTMR_INJECT_IIR_ADDR_MASK);
99}
100
101/**
102 * xtmr_inject_probe - Driver probe function
103 * @pdev: Pointer to the platform_device structure
104 *
105 * This is the driver probe routine. It does all the memory
106 * allocation for the device.
107 *
108 * Return: 0 on success and failure value on error
109 */
110static int xtmr_inject_probe(struct platform_device *pdev)
111{
112 struct xtmr_inject_dev *xtmr_inject;
113 int err;
114
115 xtmr_inject = devm_kzalloc(dev: &pdev->dev, size: sizeof(*xtmr_inject),
116 GFP_KERNEL);
117 if (!xtmr_inject)
118 return -ENOMEM;
119
120 xtmr_inject->regs = devm_platform_ioremap_resource(pdev, index: 0);
121 if (IS_ERR(ptr: xtmr_inject->regs))
122 return PTR_ERR(ptr: xtmr_inject->regs);
123
124 err = of_property_read_u32(np: pdev->dev.of_node, propname: "xlnx,magic",
125 out_value: &xtmr_inject->magic);
126 if (err < 0) {
127 dev_err(&pdev->dev, "unable to read xlnx,magic property");
128 return err;
129 }
130
131 if (xtmr_inject->magic > XTMR_INJECT_MAGIC_MAX_VAL) {
132 dev_err(&pdev->dev, "invalid xlnx,magic property value");
133 return -EINVAL;
134 }
135
136 /* Initialize TMR Inject */
137 xtmr_inject_init(xtmr_inject);
138
139 xtmr_init_debugfs(xtmr_inject);
140
141 platform_set_drvdata(pdev, data: xtmr_inject);
142
143 return 0;
144}
145
146static void xtmr_inject_remove(struct platform_device *pdev)
147{
148 debugfs_remove_recursive(dentry: dbgfs_root);
149 dbgfs_root = NULL;
150}
151
152static const struct of_device_id xtmr_inject_of_match[] = {
153 {
154 .compatible = "xlnx,tmr-inject-1.0",
155 },
156 { /* end of table */ }
157};
158MODULE_DEVICE_TABLE(of, xtmr_inject_of_match);
159
160static struct platform_driver xtmr_inject_driver = {
161 .driver = {
162 .name = "xilinx-tmr_inject",
163 .of_match_table = xtmr_inject_of_match,
164 },
165 .probe = xtmr_inject_probe,
166 .remove_new = xtmr_inject_remove,
167};
168module_platform_driver(xtmr_inject_driver);
169MODULE_AUTHOR("Advanced Micro Devices, Inc");
170MODULE_DESCRIPTION("Xilinx TMR Inject Driver");
171MODULE_LICENSE("GPL");
172

source code of linux/drivers/misc/xilinx_tmr_inject.c