summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/amd/ras/core/eeprom_fw.c
blob: 02369d54bdffdbb43899baca9d8fd3fee02d0887 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// SPDX-License-Identifier: MIT
/*
 * Copyright 2026 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "ras.h"
#include "ras_mp1.h"
#include "eeprom_fw.h"
#define MAX_EEPROM_ERR_RECORD_NUM 1024

static int fw_eeprom_reset_ras_table(struct ras_core_context *ras_core)
{
	struct fw_eeprom_control *ctl = ras_core->eeprom_mgr.ras_eeprom;
	u32 erase_res = 0;
	int res;

	mutex_lock(&ctl->record_lock);
	res = ras_mp1_reset_ras_table(ras_core, &erase_res);
	if (res || erase_res) {
		RAS_DEV_WARN(ras_core->dev,
			"RAS EEPROM reset failed, res:%d result:%d\n", res, erase_res);
		if (!res)
			res = -EIO;
		goto out;
	}

	ctl->record_count = 0;
	memset(ctl->records, 0,
		sizeof(*ctl->records) * MAX_EEPROM_ERR_RECORD_NUM);

	ctl->ras_table_format_version = 0;
	ctl->eeprom_status = 0;
	ctl->rma_status = 0;
	ctl->bad_channel_bitmap = 0;

out:
	mutex_unlock(&ctl->record_lock);
	return res;
}

static int fw_eeprom_sync_data(struct ras_core_context *ras_core,
		struct fw_eeprom_control *ctl)
{
	struct eeprom_err_record err_rec = {0};
	u32 fw_err_rec_num;
	u32 idx;
	int ret = 0;

	mutex_lock(&ctl->record_lock);
	ret = ras_mp1_get_record_count(ras_core, &fw_err_rec_num);
	if (ret)
		goto out;

	if (!fw_err_rec_num || fw_err_rec_num == ctl->record_count) {
		goto out;
	} else if (fw_err_rec_num < ctl->record_count) {
		RAS_DEV_ERR(ras_core->dev, "EEPROM ECC error count mismatch!\n");
		ret = -EFAULT;
		goto out;
	} else if (fw_err_rec_num > MAX_EEPROM_ERR_RECORD_NUM) {
		RAS_DEV_ERR(ras_core->dev,
			"Invalid EEPROM error count:0x%x\n", fw_err_rec_num);
		ret = -EOVERFLOW;
		goto out;
	}

	for (idx = ctl->record_count;
			idx < fw_err_rec_num; idx++, ctl->record_count = idx) {
		ret = ras_mp1_get_record(ras_core, idx, &err_rec);
		if (ret)
			goto out;

		memcpy(&ctl->records[idx], &err_rec, sizeof(*ctl->records));
	}

out:
	mutex_unlock(&ctl->record_lock);
	return ret;
}

static int fw_eeprom_get_record_count(struct ras_core_context *ras_core)
{
	struct fw_eeprom_control *ctl;

	if (!ras_core->eeprom_mgr.ras_eeprom)
		return -EINVAL;

	ctl = ras_core->eeprom_mgr.ras_eeprom;

	if (!ras_core_gpu_in_reset(ras_core))
		fw_eeprom_sync_data(ras_core, ctl);

	return ctl->record_count;
}

static int fw_eeprom_get_record(struct ras_core_context *ras_core,
		u32 idx, struct eeprom_umc_record *rec)
{
	struct fw_eeprom_control *ctl;
	struct ras_bank_ecc bank = {0};

	if (!ras_core->eeprom_mgr.ras_eeprom)
		return -EINVAL;

	ctl = ras_core->eeprom_mgr.ras_eeprom;

	if (!rec || idx >= ctl->record_count)
		return -EINVAL;

	bank.timestamp = ctl->records[idx].timestamp;
	bank.status = 0;
	bank.ipid = ctl->records[idx].ipid;
	bank.addr = ctl->records[idx].mca_addr;
	bank.nps = ras_core_get_curr_nps_mode(ras_core);

	return ras_umc_bank_to_umc_record(ras_core, &bank, rec);
}

static int fw_eeprom_sw_init(struct ras_core_context *ras_core,
			struct ras_eeprom_param *param)
{
	struct ras_eeprom_mgr *mgr = &ras_core->eeprom_mgr;
	struct fw_eeprom_control *ctl;
	int ret = 0;

	if (!param)
		return -EINVAL;

	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
	if (!ctl)
		return -ENOMEM;

	ctl->eeprom_ip_version = param->eeprom_ip_version;
	ctl->records = kzalloc(sizeof(*ctl->records) * MAX_EEPROM_ERR_RECORD_NUM, GFP_KERNEL);
	if (!ctl->records) {
		ret = -ENOMEM;
		goto out;
	}

	ctl->max_record_count = MAX_EEPROM_ERR_RECORD_NUM;
	mutex_init(&ctl->record_lock);

	mgr->ras_eeprom = ctl;

	return 0;

out:
	kfree(ctl);
	return ret;
}

static int fw_eeprom_sw_fini(struct ras_core_context *ras_core)
{
	struct fw_eeprom_control *ctl;

	if (!ras_core->eeprom_mgr.ras_eeprom)
		return -EINVAL;

	ctl = ras_core->eeprom_mgr.ras_eeprom;

	kfree(ctl->records);
	mutex_destroy(&ctl->record_lock);

	kfree(ctl);
	ras_core->eeprom_mgr.ras_eeprom = NULL;

	return 0;
}

static int fw_eeprom_hw_init(struct ras_core_context *ras_core,
			struct ras_eeprom_param *param)
{
	struct fw_eeprom_control *ctl;

	if (!ras_core->eeprom_mgr.ras_eeprom)
		return -EINVAL;

	ctl = ras_core->eeprom_mgr.ras_eeprom;

	ras_mp1_get_table_version(ras_core, &ctl->ras_table_format_version);

	return 0;
}

static int fw_eeprom_hw_fini(struct ras_core_context *ras_core)
{
	return 0;
}

static int fw_eeprom_get_records(struct ras_core_context *ras_core, u32 start,
		struct eeprom_umc_record *record, u32 num)
{
	struct fw_eeprom_control *ctl = ras_core->eeprom_mgr.ras_eeprom;
	int i, ret = 0;

	mutex_lock(&ctl->record_lock);
	for (i = 0; i < num; i++) {
		ret = fw_eeprom_get_record(ras_core, start + i, &record[i]);
		if (ret)
			break;
	}
	mutex_unlock(&ctl->record_lock);

	return ret;
}

static int fw_eeprom_get_eeprom_info(struct ras_core_context *ras_core,
		struct ras_eeprom_info *eeprom_info, bool fast_mode)
{
	struct fw_eeprom_control *ctl = ras_core->eeprom_mgr.ras_eeprom;

	if (!eeprom_info)
		return -EINVAL;

	eeprom_info->record_count = ctl->record_count;
	eeprom_info->max_record_count = ctl->max_record_count;
	eeprom_info->bad_channel_bitmap = ctl->bad_channel_bitmap;

	if (!fast_mode) {
		if (!ctl->ras_table_format_version)
			ras_mp1_get_table_version(ras_core,
				&ctl->ras_table_format_version);

		eeprom_info->rma_status = ras_mp1_rma_detected(ras_core);
		ctl->rma_status = eeprom_info->rma_status;
	} else {
		eeprom_info->rma_status = ctl->rma_status;
	}

	eeprom_info->record_format_version = ctl->ras_table_format_version;

	if (eeprom_info->rma_status)
		eeprom_info->eeprom_status = RAS_EEPROM_LOCKED;
	else
		eeprom_info->eeprom_status = RAS_EEPROM_OK;

	return 0;
}

struct ras_eeprom_ops ras_fw_eeprom_ops = {
	.sw_init = fw_eeprom_sw_init,
	.sw_fini = fw_eeprom_sw_fini,
	.hw_init = fw_eeprom_hw_init,
	.hw_fini = fw_eeprom_hw_fini,
	.reset_table = fw_eeprom_reset_ras_table,
	.get_records = fw_eeprom_get_records,
	.get_record_count = fw_eeprom_get_record_count,
	.get_eeprom_info = fw_eeprom_get_eeprom_info,
};