1
2
3
4
5
6
7
8
9
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/pci.h>
14#include <linux/slab.h>
15#include <linux/uaccess.h>
16#include <linux/overflow.h>
17
18#include "class.h"
19
20#define DRV_NAME "pmt_crashlog"
21
22
23#define CRASH_TYPE_OOBMSM 1
24
25
26#define CRASHLOG_FLAG_DISABLE BIT(28)
27
28
29
30
31
32
33
34
35#define CRASHLOG_FLAG_TRIGGER_CLEAR BIT(29)
36#define CRASHLOG_FLAG_TRIGGER_EXECUTE BIT(30)
37#define CRASHLOG_FLAG_TRIGGER_COMPLETE BIT(31)
38#define CRASHLOG_FLAG_TRIGGER_MASK GENMASK(31, 28)
39
40
41#define CONTROL_OFFSET 0x0
42#define GUID_OFFSET 0x4
43#define BASE_OFFSET 0x8
44#define SIZE_OFFSET 0xC
45#define GET_ACCESS(v) ((v) & GENMASK(3, 0))
46#define GET_TYPE(v) (((v) & GENMASK(7, 4)) >> 4)
47#define GET_VERSION(v) (((v) & GENMASK(19, 16)) >> 16)
48
49#define GET_SIZE(v) ((v) * sizeof(u32))
50
51struct crashlog_entry {
52
53 struct intel_pmt_entry entry;
54 struct mutex control_mutex;
55};
56
57struct pmt_crashlog_priv {
58 int num_entries;
59 struct crashlog_entry entry[];
60};
61
62
63
64
65static bool pmt_crashlog_complete(struct intel_pmt_entry *entry)
66{
67 u32 control = readl(entry->disc_table + CONTROL_OFFSET);
68
69
70 return !!(control & CRASHLOG_FLAG_TRIGGER_COMPLETE);
71}
72
73static bool pmt_crashlog_disabled(struct intel_pmt_entry *entry)
74{
75 u32 control = readl(entry->disc_table + CONTROL_OFFSET);
76
77
78 return !!(control & CRASHLOG_FLAG_DISABLE);
79}
80
81static bool pmt_crashlog_supported(struct intel_pmt_entry *entry)
82{
83 u32 discovery_header = readl(entry->disc_table + CONTROL_OFFSET);
84 u32 crash_type, version;
85
86 crash_type = GET_TYPE(discovery_header);
87 version = GET_VERSION(discovery_header);
88
89
90
91
92
93 return crash_type == CRASH_TYPE_OOBMSM && version == 0;
94}
95
96static void pmt_crashlog_set_disable(struct intel_pmt_entry *entry,
97 bool disable)
98{
99 u32 control = readl(entry->disc_table + CONTROL_OFFSET);
100
101
102 control &= ~CRASHLOG_FLAG_TRIGGER_MASK;
103
104 if (disable)
105 control |= CRASHLOG_FLAG_DISABLE;
106 else
107 control &= ~CRASHLOG_FLAG_DISABLE;
108
109 writel(control, entry->disc_table + CONTROL_OFFSET);
110}
111
112static void pmt_crashlog_set_clear(struct intel_pmt_entry *entry)
113{
114 u32 control = readl(entry->disc_table + CONTROL_OFFSET);
115
116 control &= ~CRASHLOG_FLAG_TRIGGER_MASK;
117 control |= CRASHLOG_FLAG_TRIGGER_CLEAR;
118
119 writel(control, entry->disc_table + CONTROL_OFFSET);
120}
121
122static void pmt_crashlog_set_execute(struct intel_pmt_entry *entry)
123{
124 u32 control = readl(entry->disc_table + CONTROL_OFFSET);
125
126 control &= ~CRASHLOG_FLAG_TRIGGER_MASK;
127 control |= CRASHLOG_FLAG_TRIGGER_EXECUTE;
128
129 writel(control, entry->disc_table + CONTROL_OFFSET);
130}
131
132
133
134
135static ssize_t
136enable_show(struct device *dev, struct device_attribute *attr, char *buf)
137{
138 struct intel_pmt_entry *entry = dev_get_drvdata(dev);
139 int enabled = !pmt_crashlog_disabled(entry);
140
141 return sprintf(buf, "%d\n", enabled);
142}
143
144static ssize_t
145enable_store(struct device *dev, struct device_attribute *attr,
146 const char *buf, size_t count)
147{
148 struct crashlog_entry *entry;
149 bool enabled;
150 int result;
151
152 entry = dev_get_drvdata(dev);
153
154 result = kstrtobool(buf, &enabled);
155 if (result)
156 return result;
157
158 mutex_lock(&entry->control_mutex);
159 pmt_crashlog_set_disable(&entry->entry, !enabled);
160 mutex_unlock(&entry->control_mutex);
161
162 return count;
163}
164static DEVICE_ATTR_RW(enable);
165
166static ssize_t
167trigger_show(struct device *dev, struct device_attribute *attr, char *buf)
168{
169 struct intel_pmt_entry *entry;
170 int trigger;
171
172 entry = dev_get_drvdata(dev);
173 trigger = pmt_crashlog_complete(entry);
174
175 return sprintf(buf, "%d\n", trigger);
176}
177
178static ssize_t
179trigger_store(struct device *dev, struct device_attribute *attr,
180 const char *buf, size_t count)
181{
182 struct crashlog_entry *entry;
183 bool trigger;
184 int result;
185
186 entry = dev_get_drvdata(dev);
187
188 result = kstrtobool(buf, &trigger);
189 if (result)
190 return result;
191
192 mutex_lock(&entry->control_mutex);
193
194 if (!trigger) {
195 pmt_crashlog_set_clear(&entry->entry);
196 } else if (pmt_crashlog_complete(&entry->entry)) {
197
198 result = -EEXIST;
199 goto err;
200 } else if (pmt_crashlog_disabled(&entry->entry)) {
201
202 result = -EBUSY;
203 goto err;
204 } else {
205 pmt_crashlog_set_execute(&entry->entry);
206 }
207
208 result = count;
209err:
210 mutex_unlock(&entry->control_mutex);
211 return result;
212}
213static DEVICE_ATTR_RW(trigger);
214
215static struct attribute *pmt_crashlog_attrs[] = {
216 &dev_attr_enable.attr,
217 &dev_attr_trigger.attr,
218 NULL
219};
220
221static const struct attribute_group pmt_crashlog_group = {
222 .attrs = pmt_crashlog_attrs,
223};
224
225static int pmt_crashlog_header_decode(struct intel_pmt_entry *entry,
226 struct intel_pmt_header *header,
227 struct device *dev)
228{
229 void __iomem *disc_table = entry->disc_table;
230 struct crashlog_entry *crashlog;
231
232 if (!pmt_crashlog_supported(entry))
233 return 1;
234
235
236 crashlog = container_of(entry, struct crashlog_entry, entry);
237 mutex_init(&crashlog->control_mutex);
238
239 header->access_type = GET_ACCESS(readl(disc_table));
240 header->guid = readl(disc_table + GUID_OFFSET);
241 header->base_offset = readl(disc_table + BASE_OFFSET);
242
243
244 header->size = GET_SIZE(readl(disc_table + SIZE_OFFSET));
245
246 return 0;
247}
248
249static DEFINE_XARRAY_ALLOC(crashlog_array);
250static struct intel_pmt_namespace pmt_crashlog_ns = {
251 .name = "crashlog",
252 .xa = &crashlog_array,
253 .attr_grp = &pmt_crashlog_group,
254 .pmt_header_decode = pmt_crashlog_header_decode,
255};
256
257
258
259
260static int pmt_crashlog_remove(struct platform_device *pdev)
261{
262 struct pmt_crashlog_priv *priv = platform_get_drvdata(pdev);
263 int i;
264
265 for (i = 0; i < priv->num_entries; i++)
266 intel_pmt_dev_destroy(&priv->entry[i].entry, &pmt_crashlog_ns);
267
268 return 0;
269}
270
271static int pmt_crashlog_probe(struct platform_device *pdev)
272{
273 struct pmt_crashlog_priv *priv;
274 size_t size;
275 int i, ret;
276
277 size = struct_size(priv, entry, pdev->num_resources);
278 priv = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
279 if (!priv)
280 return -ENOMEM;
281
282 platform_set_drvdata(pdev, priv);
283
284 for (i = 0; i < pdev->num_resources; i++) {
285 struct intel_pmt_entry *entry = &priv->entry[i].entry;
286
287 ret = intel_pmt_dev_create(entry, &pmt_crashlog_ns, pdev, i);
288 if (ret < 0)
289 goto abort_probe;
290 if (ret)
291 continue;
292
293 priv->num_entries++;
294 }
295
296 return 0;
297abort_probe:
298 pmt_crashlog_remove(pdev);
299 return ret;
300}
301
302static struct platform_driver pmt_crashlog_driver = {
303 .driver = {
304 .name = DRV_NAME,
305 },
306 .remove = pmt_crashlog_remove,
307 .probe = pmt_crashlog_probe,
308};
309
310static int __init pmt_crashlog_init(void)
311{
312 return platform_driver_register(&pmt_crashlog_driver);
313}
314
315static void __exit pmt_crashlog_exit(void)
316{
317 platform_driver_unregister(&pmt_crashlog_driver);
318 xa_destroy(&crashlog_array);
319}
320
321module_init(pmt_crashlog_init);
322module_exit(pmt_crashlog_exit);
323
324MODULE_AUTHOR("Alexander Duyck <alexander.h.duyck@linux.intel.com>");
325MODULE_DESCRIPTION("Intel PMT Crashlog driver");
326MODULE_ALIAS("platform:" DRV_NAME);
327MODULE_LICENSE("GPL v2");
328