]> Git Repo - linux.git/blob - drivers/platform/x86/intel_pmt_crashlog.c
Merge tag 'drm-next-20210322' of git://linuxtv.org/pinchartl/media into drm-next
[linux.git] / drivers / platform / x86 / intel_pmt_crashlog.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel Platform Monitoring Technology Crashlog driver
4  *
5  * Copyright (c) 2020, Intel Corporation.
6  * All Rights Reserved.
7  *
8  * Author: "Alexander Duyck" <[email protected]>
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 "intel_pmt_class.h"
19
20 #define DRV_NAME                "pmt_crashlog"
21
22 /* Crashlog discovery header types */
23 #define CRASH_TYPE_OOBMSM       1
24
25 /* Control Flags */
26 #define CRASHLOG_FLAG_DISABLE           BIT(27)
27
28 /*
29  * Bits 28 and 29 control the state of bit 31.
30  *
31  * Bit 28 will clear bit 31, if set, allowing a new crashlog to be captured.
32  * Bit 29 will immediately trigger a crashlog to be generated, setting bit 31.
33  * Bit 30 is read-only and reserved as 0.
34  * Bit 31 is the read-only status with a 1 indicating log is complete.
35  */
36 #define CRASHLOG_FLAG_TRIGGER_CLEAR     BIT(28)
37 #define CRASHLOG_FLAG_TRIGGER_EXECUTE   BIT(29)
38 #define CRASHLOG_FLAG_TRIGGER_COMPLETE  BIT(31)
39 #define CRASHLOG_FLAG_TRIGGER_MASK      GENMASK(31, 28)
40
41 /* Crashlog Discovery Header */
42 #define CONTROL_OFFSET          0x0
43 #define GUID_OFFSET             0x4
44 #define BASE_OFFSET             0x8
45 #define SIZE_OFFSET             0xC
46 #define GET_ACCESS(v)           ((v) & GENMASK(3, 0))
47 #define GET_TYPE(v)             (((v) & GENMASK(7, 4)) >> 4)
48 #define GET_VERSION(v)          (((v) & GENMASK(19, 16)) >> 16)
49 /* size is in bytes */
50 #define GET_SIZE(v)             ((v) * sizeof(u32))
51
52 struct crashlog_entry {
53         /* entry must be first member of struct */
54         struct intel_pmt_entry          entry;
55         struct mutex                    control_mutex;
56 };
57
58 struct pmt_crashlog_priv {
59         int                     num_entries;
60         struct crashlog_entry   entry[];
61 };
62
63 /*
64  * I/O
65  */
66 static bool pmt_crashlog_complete(struct intel_pmt_entry *entry)
67 {
68         u32 control = readl(entry->disc_table + CONTROL_OFFSET);
69
70         /* return current value of the crashlog complete flag */
71         return !!(control & CRASHLOG_FLAG_TRIGGER_COMPLETE);
72 }
73
74 static bool pmt_crashlog_disabled(struct intel_pmt_entry *entry)
75 {
76         u32 control = readl(entry->disc_table + CONTROL_OFFSET);
77
78         /* return current value of the crashlog disabled flag */
79         return !!(control & CRASHLOG_FLAG_DISABLE);
80 }
81
82 static bool pmt_crashlog_supported(struct intel_pmt_entry *entry)
83 {
84         u32 discovery_header = readl(entry->disc_table + CONTROL_OFFSET);
85         u32 crash_type, version;
86
87         crash_type = GET_TYPE(discovery_header);
88         version = GET_VERSION(discovery_header);
89
90         /*
91          * Currently we only recognize OOBMSM version 0 devices.
92          * We can ignore all other crashlog devices in the system.
93          */
94         return crash_type == CRASH_TYPE_OOBMSM && version == 0;
95 }
96
97 static void pmt_crashlog_set_disable(struct intel_pmt_entry *entry,
98                                      bool disable)
99 {
100         u32 control = readl(entry->disc_table + CONTROL_OFFSET);
101
102         /* clear trigger bits so we are only modifying disable flag */
103         control &= ~CRASHLOG_FLAG_TRIGGER_MASK;
104
105         if (disable)
106                 control |= CRASHLOG_FLAG_DISABLE;
107         else
108                 control &= ~CRASHLOG_FLAG_DISABLE;
109
110         writel(control, entry->disc_table + CONTROL_OFFSET);
111 }
112
113 static void pmt_crashlog_set_clear(struct intel_pmt_entry *entry)
114 {
115         u32 control = readl(entry->disc_table + CONTROL_OFFSET);
116
117         control &= ~CRASHLOG_FLAG_TRIGGER_MASK;
118         control |= CRASHLOG_FLAG_TRIGGER_CLEAR;
119
120         writel(control, entry->disc_table + CONTROL_OFFSET);
121 }
122
123 static void pmt_crashlog_set_execute(struct intel_pmt_entry *entry)
124 {
125         u32 control = readl(entry->disc_table + CONTROL_OFFSET);
126
127         control &= ~CRASHLOG_FLAG_TRIGGER_MASK;
128         control |= CRASHLOG_FLAG_TRIGGER_EXECUTE;
129
130         writel(control, entry->disc_table + CONTROL_OFFSET);
131 }
132
133 /*
134  * sysfs
135  */
136 static ssize_t
137 enable_show(struct device *dev, struct device_attribute *attr, char *buf)
138 {
139         struct intel_pmt_entry *entry = dev_get_drvdata(dev);
140         int enabled = !pmt_crashlog_disabled(entry);
141
142         return sprintf(buf, "%d\n", enabled);
143 }
144
145 static ssize_t
146 enable_store(struct device *dev, struct device_attribute *attr,
147             const char *buf, size_t count)
148 {
149         struct crashlog_entry *entry;
150         bool enabled;
151         int result;
152
153         entry = dev_get_drvdata(dev);
154
155         result = kstrtobool(buf, &enabled);
156         if (result)
157                 return result;
158
159         mutex_lock(&entry->control_mutex);
160         pmt_crashlog_set_disable(&entry->entry, !enabled);
161         mutex_unlock(&entry->control_mutex);
162
163         return count;
164 }
165 static DEVICE_ATTR_RW(enable);
166
167 static ssize_t
168 trigger_show(struct device *dev, struct device_attribute *attr, char *buf)
169 {
170         struct intel_pmt_entry *entry;
171         int trigger;
172
173         entry = dev_get_drvdata(dev);
174         trigger = pmt_crashlog_complete(entry);
175
176         return sprintf(buf, "%d\n", trigger);
177 }
178
179 static ssize_t
180 trigger_store(struct device *dev, struct device_attribute *attr,
181             const char *buf, size_t count)
182 {
183         struct crashlog_entry *entry;
184         bool trigger;
185         int result;
186
187         entry = dev_get_drvdata(dev);
188
189         result = kstrtobool(buf, &trigger);
190         if (result)
191                 return result;
192
193         mutex_lock(&entry->control_mutex);
194
195         if (!trigger) {
196                 pmt_crashlog_set_clear(&entry->entry);
197         } else if (pmt_crashlog_complete(&entry->entry)) {
198                 /* we cannot trigger a new crash if one is still pending */
199                 result = -EEXIST;
200                 goto err;
201         } else if (pmt_crashlog_disabled(&entry->entry)) {
202                 /* if device is currently disabled, return busy */
203                 result = -EBUSY;
204                 goto err;
205         } else {
206                 pmt_crashlog_set_execute(&entry->entry);
207         }
208
209         result = count;
210 err:
211         mutex_unlock(&entry->control_mutex);
212         return result;
213 }
214 static DEVICE_ATTR_RW(trigger);
215
216 static struct attribute *pmt_crashlog_attrs[] = {
217         &dev_attr_enable.attr,
218         &dev_attr_trigger.attr,
219         NULL
220 };
221
222 static struct attribute_group pmt_crashlog_group = {
223         .attrs  = pmt_crashlog_attrs,
224 };
225
226 static int pmt_crashlog_header_decode(struct intel_pmt_entry *entry,
227                                       struct intel_pmt_header *header,
228                                       struct device *dev)
229 {
230         void __iomem *disc_table = entry->disc_table;
231         struct crashlog_entry *crashlog;
232
233         if (!pmt_crashlog_supported(entry))
234                 return 1;
235
236         /* initialize control mutex */
237         crashlog = container_of(entry, struct crashlog_entry, entry);
238         mutex_init(&crashlog->control_mutex);
239
240         header->access_type = GET_ACCESS(readl(disc_table));
241         header->guid = readl(disc_table + GUID_OFFSET);
242         header->base_offset = readl(disc_table + BASE_OFFSET);
243
244         /* Size is measured in DWORDS, but accessor returns bytes */
245         header->size = GET_SIZE(readl(disc_table + SIZE_OFFSET));
246
247         return 0;
248 }
249
250 static DEFINE_XARRAY_ALLOC(crashlog_array);
251 static struct intel_pmt_namespace pmt_crashlog_ns = {
252         .name = "crashlog",
253         .xa = &crashlog_array,
254         .attr_grp = &pmt_crashlog_group,
255         .pmt_header_decode = pmt_crashlog_header_decode,
256 };
257
258 /*
259  * initialization
260  */
261 static int pmt_crashlog_remove(struct platform_device *pdev)
262 {
263         struct pmt_crashlog_priv *priv = platform_get_drvdata(pdev);
264         int i;
265
266         for (i = 0; i < priv->num_entries; i++)
267                 intel_pmt_dev_destroy(&priv->entry[i].entry, &pmt_crashlog_ns);
268
269         return 0;
270 }
271
272 static int pmt_crashlog_probe(struct platform_device *pdev)
273 {
274         struct pmt_crashlog_priv *priv;
275         size_t size;
276         int i, ret;
277
278         size = struct_size(priv, entry, pdev->num_resources);
279         priv = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
280         if (!priv)
281                 return -ENOMEM;
282
283         platform_set_drvdata(pdev, priv);
284
285         for (i = 0; i < pdev->num_resources; i++) {
286                 struct intel_pmt_entry *entry = &priv->entry[i].entry;
287
288                 ret = intel_pmt_dev_create(entry, &pmt_crashlog_ns, pdev, i);
289                 if (ret < 0)
290                         goto abort_probe;
291                 if (ret)
292                         continue;
293
294                 priv->num_entries++;
295         }
296
297         return 0;
298 abort_probe:
299         pmt_crashlog_remove(pdev);
300         return ret;
301 }
302
303 static struct platform_driver pmt_crashlog_driver = {
304         .driver = {
305                 .name   = DRV_NAME,
306         },
307         .remove = pmt_crashlog_remove,
308         .probe  = pmt_crashlog_probe,
309 };
310
311 static int __init pmt_crashlog_init(void)
312 {
313         return platform_driver_register(&pmt_crashlog_driver);
314 }
315
316 static void __exit pmt_crashlog_exit(void)
317 {
318         platform_driver_unregister(&pmt_crashlog_driver);
319         xa_destroy(&crashlog_array);
320 }
321
322 module_init(pmt_crashlog_init);
323 module_exit(pmt_crashlog_exit);
324
325 MODULE_AUTHOR("Alexander Duyck <[email protected]>");
326 MODULE_DESCRIPTION("Intel PMT Crashlog driver");
327 MODULE_ALIAS("platform:" DRV_NAME);
328 MODULE_LICENSE("GPL v2");
This page took 0.052491 seconds and 4 git commands to generate.