Merge tag 'dmaengine-5.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/vkoul...
[linux-2.6-microblaze.git] / drivers / acpi / apei / hest.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * APEI Hardware Error Souce Table support
4  *
5  * HEST describes error sources in detail; communicates operational
6  * parameters (i.e. severity levels, masking bits, and threshold
7  * values) to Linux as necessary. It also allows the BIOS to report
8  * non-standard error sources to Linux (for example, chipset-specific
9  * error registers).
10  *
11  * For more information about HEST, please refer to ACPI Specification
12  * version 4.0, section 17.3.2.
13  *
14  * Copyright 2009 Intel Corp.
15  *   Author: Huang Ying <ying.huang@intel.com>
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/acpi.h>
22 #include <linux/kdebug.h>
23 #include <linux/highmem.h>
24 #include <linux/io.h>
25 #include <linux/platform_device.h>
26 #include <acpi/apei.h>
27 #include <acpi/ghes.h>
28
29 #include "apei-internal.h"
30
31 #define HEST_PFX "HEST: "
32
33 int hest_disable;
34 EXPORT_SYMBOL_GPL(hest_disable);
35
36 /* HEST table parsing */
37
38 static struct acpi_table_hest *__read_mostly hest_tab;
39
40 static const int hest_esrc_len_tab[ACPI_HEST_TYPE_RESERVED] = {
41         [ACPI_HEST_TYPE_IA32_CHECK] = -1,       /* need further calculation */
42         [ACPI_HEST_TYPE_IA32_CORRECTED_CHECK] = -1,
43         [ACPI_HEST_TYPE_IA32_NMI] = sizeof(struct acpi_hest_ia_nmi),
44         [ACPI_HEST_TYPE_AER_ROOT_PORT] = sizeof(struct acpi_hest_aer_root),
45         [ACPI_HEST_TYPE_AER_ENDPOINT] = sizeof(struct acpi_hest_aer),
46         [ACPI_HEST_TYPE_AER_BRIDGE] = sizeof(struct acpi_hest_aer_bridge),
47         [ACPI_HEST_TYPE_GENERIC_ERROR] = sizeof(struct acpi_hest_generic),
48         [ACPI_HEST_TYPE_GENERIC_ERROR_V2] = sizeof(struct acpi_hest_generic_v2),
49         [ACPI_HEST_TYPE_IA32_DEFERRED_CHECK] = -1,
50 };
51
52 static inline bool is_generic_error(struct acpi_hest_header *hest_hdr)
53 {
54         return hest_hdr->type == ACPI_HEST_TYPE_GENERIC_ERROR ||
55                hest_hdr->type == ACPI_HEST_TYPE_GENERIC_ERROR_V2;
56 }
57
58 static int hest_esrc_len(struct acpi_hest_header *hest_hdr)
59 {
60         u16 hest_type = hest_hdr->type;
61         int len;
62
63         if (hest_type >= ACPI_HEST_TYPE_RESERVED)
64                 return 0;
65
66         len = hest_esrc_len_tab[hest_type];
67
68         if (hest_type == ACPI_HEST_TYPE_IA32_CORRECTED_CHECK) {
69                 struct acpi_hest_ia_corrected *cmc;
70                 cmc = (struct acpi_hest_ia_corrected *)hest_hdr;
71                 len = sizeof(*cmc) + cmc->num_hardware_banks *
72                         sizeof(struct acpi_hest_ia_error_bank);
73         } else if (hest_type == ACPI_HEST_TYPE_IA32_CHECK) {
74                 struct acpi_hest_ia_machine_check *mc;
75                 mc = (struct acpi_hest_ia_machine_check *)hest_hdr;
76                 len = sizeof(*mc) + mc->num_hardware_banks *
77                         sizeof(struct acpi_hest_ia_error_bank);
78         } else if (hest_type == ACPI_HEST_TYPE_IA32_DEFERRED_CHECK) {
79                 struct acpi_hest_ia_deferred_check *mc;
80                 mc = (struct acpi_hest_ia_deferred_check *)hest_hdr;
81                 len = sizeof(*mc) + mc->num_hardware_banks *
82                         sizeof(struct acpi_hest_ia_error_bank);
83         }
84         BUG_ON(len == -1);
85
86         return len;
87 };
88
89 int apei_hest_parse(apei_hest_func_t func, void *data)
90 {
91         struct acpi_hest_header *hest_hdr;
92         int i, rc, len;
93
94         if (hest_disable || !hest_tab)
95                 return -EINVAL;
96
97         hest_hdr = (struct acpi_hest_header *)(hest_tab + 1);
98         for (i = 0; i < hest_tab->error_source_count; i++) {
99                 len = hest_esrc_len(hest_hdr);
100                 if (!len) {
101                         pr_warn(FW_WARN HEST_PFX
102                                 "Unknown or unused hardware error source "
103                                 "type: %d for hardware error source: %d.\n",
104                                 hest_hdr->type, hest_hdr->source_id);
105                         return -EINVAL;
106                 }
107                 if ((void *)hest_hdr + len >
108                     (void *)hest_tab + hest_tab->header.length) {
109                         pr_warn(FW_BUG HEST_PFX
110                 "Table contents overflow for hardware error source: %d.\n",
111                                 hest_hdr->source_id);
112                         return -EINVAL;
113                 }
114
115                 rc = func(hest_hdr, data);
116                 if (rc)
117                         return rc;
118
119                 hest_hdr = (void *)hest_hdr + len;
120         }
121
122         return 0;
123 }
124 EXPORT_SYMBOL_GPL(apei_hest_parse);
125
126 /*
127  * Check if firmware advertises firmware first mode. We need FF bit to be set
128  * along with a set of MC banks which work in FF mode.
129  */
130 static int __init hest_parse_cmc(struct acpi_hest_header *hest_hdr, void *data)
131 {
132         if (hest_hdr->type != ACPI_HEST_TYPE_IA32_CORRECTED_CHECK)
133                 return 0;
134
135         if (!acpi_disable_cmcff)
136                 return !arch_apei_enable_cmcff(hest_hdr, data);
137
138         return 0;
139 }
140
141 struct ghes_arr {
142         struct platform_device **ghes_devs;
143         unsigned int count;
144 };
145
146 static int __init hest_parse_ghes_count(struct acpi_hest_header *hest_hdr, void *data)
147 {
148         int *count = data;
149
150         if (is_generic_error(hest_hdr))
151                 (*count)++;
152         return 0;
153 }
154
155 static int __init hest_parse_ghes(struct acpi_hest_header *hest_hdr, void *data)
156 {
157         struct platform_device *ghes_dev;
158         struct ghes_arr *ghes_arr = data;
159         int rc, i;
160
161         if (!is_generic_error(hest_hdr))
162                 return 0;
163
164         if (!((struct acpi_hest_generic *)hest_hdr)->enabled)
165                 return 0;
166         for (i = 0; i < ghes_arr->count; i++) {
167                 struct acpi_hest_header *hdr;
168                 ghes_dev = ghes_arr->ghes_devs[i];
169                 hdr = *(struct acpi_hest_header **)ghes_dev->dev.platform_data;
170                 if (hdr->source_id == hest_hdr->source_id) {
171                         pr_warn(FW_WARN HEST_PFX "Duplicated hardware error source ID: %d.\n",
172                                 hdr->source_id);
173                         return -EIO;
174                 }
175         }
176         ghes_dev = platform_device_alloc("GHES", hest_hdr->source_id);
177         if (!ghes_dev)
178                 return -ENOMEM;
179
180         rc = platform_device_add_data(ghes_dev, &hest_hdr, sizeof(void *));
181         if (rc)
182                 goto err;
183
184         rc = platform_device_add(ghes_dev);
185         if (rc)
186                 goto err;
187         ghes_arr->ghes_devs[ghes_arr->count++] = ghes_dev;
188
189         return 0;
190 err:
191         platform_device_put(ghes_dev);
192         return rc;
193 }
194
195 static int __init hest_ghes_dev_register(unsigned int ghes_count)
196 {
197         int rc, i;
198         struct ghes_arr ghes_arr;
199
200         ghes_arr.count = 0;
201         ghes_arr.ghes_devs = kmalloc_array(ghes_count, sizeof(void *),
202                                            GFP_KERNEL);
203         if (!ghes_arr.ghes_devs)
204                 return -ENOMEM;
205
206         rc = apei_hest_parse(hest_parse_ghes, &ghes_arr);
207         if (rc)
208                 goto err;
209
210         rc = ghes_estatus_pool_init(ghes_count);
211         if (rc)
212                 goto err;
213
214 out:
215         kfree(ghes_arr.ghes_devs);
216         return rc;
217 err:
218         for (i = 0; i < ghes_arr.count; i++)
219                 platform_device_unregister(ghes_arr.ghes_devs[i]);
220         goto out;
221 }
222
223 static int __init setup_hest_disable(char *str)
224 {
225         hest_disable = HEST_DISABLED;
226         return 0;
227 }
228
229 __setup("hest_disable", setup_hest_disable);
230
231 void __init acpi_hest_init(void)
232 {
233         acpi_status status;
234         int rc;
235         unsigned int ghes_count = 0;
236
237         if (hest_disable) {
238                 pr_info(HEST_PFX "Table parsing disabled.\n");
239                 return;
240         }
241
242         status = acpi_get_table(ACPI_SIG_HEST, 0,
243                                 (struct acpi_table_header **)&hest_tab);
244         if (status == AE_NOT_FOUND) {
245                 hest_disable = HEST_NOT_FOUND;
246                 return;
247         } else if (ACPI_FAILURE(status)) {
248                 const char *msg = acpi_format_exception(status);
249                 pr_err(HEST_PFX "Failed to get table, %s\n", msg);
250                 hest_disable = HEST_DISABLED;
251                 return;
252         }
253
254         rc = apei_hest_parse(hest_parse_cmc, NULL);
255         if (rc)
256                 goto err;
257
258         if (!ghes_disable) {
259                 rc = apei_hest_parse(hest_parse_ghes_count, &ghes_count);
260                 if (rc)
261                         goto err;
262
263                 if (ghes_count)
264                         rc = hest_ghes_dev_register(ghes_count);
265                 if (rc)
266                         goto err;
267         }
268
269         pr_info(HEST_PFX "Table parsing has been initialized.\n");
270         return;
271 err:
272         hest_disable = HEST_DISABLED;
273         acpi_put_table((struct acpi_table_header *)hest_tab);
274 }