aboutsummaryrefslogtreecommitdiff
path: root/hw/fsp/fsp-mdst-table.c
blob: 5b299482930b48052b987d73c896f2a856c44706 (plain)
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
/* Copyright 2013-2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/*
 * Sapphire dump design:
 *   - During initialization we setup Memory Dump Source Table (MDST) table
 *     which contains address, size pair.
 *   - We send MDST table update notification to FSP via MBOX command.
 *   - During Sapphire checkstop:
 *     - FSP retrieves HWDUMP.
 *     - FSP retrieves CEC memory based on MDST table.
 *   - Once Sapphire reboot FSP sends new dump avialable notification via HDAT
 */

#include <fsp.h>
#include <psi.h>
#include <opal.h>
#include <lock.h>
#include <skiboot.h>
#include <fsp-elog.h>
#include <fsp-mdst-table.h>

/*
 * Sapphire dump size
 *   This is the maximum memory that FSP can retrieve during checkstop.
 *
 * Note:
 *   Presently we are hardcoding this parameter. Eventually we need
 *   new System parameter so that we can get max size dynamically.
 */
#define MAX_SAPPHIRE_DUMP_SIZE	0x1000000

DEFINE_LOG_ENTRY(OPAL_RC_DUMP_MDST_INIT, OPAL_PLATFORM_ERR_EVT, OPAL_DUMP,
		 OPAL_PLATFORM_FIRMWARE, OPAL_PREDICTIVE_ERR_FAULT_RECTIFY_REBOOT,
		 OPAL_NA, NULL);

DEFINE_LOG_ENTRY(OPAL_RC_DUMP_MDST_UPDATE, OPAL_PLATFORM_ERR_EVT, OPAL_DUMP,
		 OPAL_PLATFORM_FIRMWARE, OPAL_PREDICTIVE_ERR_GENERAL,
		 OPAL_NA, NULL);


static struct dump_mdst_table *mdst_table;

static int cur_mdst_entry;
static int max_mdst_entry;
static int cur_dump_size;
/*
 * Presently both sizes are same.. But if someday FSP gives more space
 * than our TCE mapping then we need this validation..
 *
 * Also once FSP implements MAX_SAPPHIRE_DUMP_SIZE system param, we can
 * move this validation to separate function.
 */
static int max_dump_size = MIN(MAX_SAPPHIRE_DUMP_SIZE, PSI_DMA_HYP_DUMP_SIZE);

/* Protect MDST table entries */
static struct lock mdst_lock = LOCK_UNLOCKED;

/* Not supported on P7 */
static inline bool fsp_mdst_supported(void)
{
	return proc_gen >= proc_gen_p8;
}

static void update_mdst_table_complete(struct fsp_msg *msg)
{
	uint8_t status = (msg->resp->word1 >> 8) & 0xff;

	if (status)
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_UPDATE),
				 "MDST: MDST table update failed: 0x%x\n",
				 status);
	else
		printf("MDST: Table updated.\n");

	fsp_freemsg(msg);
}

/* Send MDST table to FSP */
static int64_t fsp_update_mdst_table(void)
{
	struct fsp_msg *msg;
	int rc = OPAL_SUCCESS;

	if (cur_mdst_entry <= 0) {
		printf("MDST: Table is empty\n");
		return OPAL_INTERNAL_ERROR;
	}

	lock(&mdst_lock);
	msg = fsp_mkmsg(FSP_CMD_HYP_MDST_TABLE, 4, 0,
			PSI_DMA_MDST_TABLE,
			sizeof(*mdst_table) * cur_mdst_entry,
			sizeof(*mdst_table));
	unlock(&mdst_lock);

	if (!msg) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_UPDATE),
				 "MDST: Message allocation failed.!\n");
		rc = OPAL_INTERNAL_ERROR;
	} else if (fsp_queue_msg(msg, update_mdst_table_complete)) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_UPDATE),
				 "MDST: Failed to queue MDST table message.\n");
		fsp_freemsg(msg);
		rc = OPAL_INTERNAL_ERROR;
	}
	return rc;
}

/* Add entry to MDST table */
static int __mdst_table_add_entry(void *addr, uint32_t type, uint32_t size)
{
	int rc = OPAL_INTERNAL_ERROR;

	lock(&mdst_lock);

	if (!mdst_table)
		goto out;

	if (cur_mdst_entry >= max_mdst_entry) {
		printf("MDST: Table is full.\n");
		goto out;
	}

	/* Make sure we don't cross dump size limit */
	if (cur_dump_size + size > max_dump_size) {
		printf("MDST: %d is crossing max dump size (%d) limit.\n",
		       cur_dump_size + size, max_dump_size);
		goto out;
	}

	/* TCE mapping */
	fsp_tce_map(PSI_DMA_HYP_DUMP + cur_dump_size, addr, ALIGN_UP(size, TCE_PSIZE));

	/* Add entry to MDST table */
	mdst_table[cur_mdst_entry].addr = PSI_DMA_HYP_DUMP + cur_dump_size;
	mdst_table[cur_mdst_entry].type = type;
	mdst_table[cur_mdst_entry].size = size;

	/* Update MDST count and dump size */
	cur_mdst_entry++;
	cur_dump_size += ALIGN_UP(size, TCE_PSIZE);

	printf("MDST: Addr = 0x%llx [size : %d bytes] added to MDST table.\n",
	       (uint64_t)addr, size);

	rc = OPAL_SUCCESS;

out:
	unlock(&mdst_lock);
	return rc;
}

static int mdst_table_add_entries(void)
{
	int rc;

	/* Add console buffer */
	rc = __mdst_table_add_entry((void *)INMEM_CON_START,
				    DUMP_SECTION_CONSOLE, INMEM_CON_LEN);
	if (rc)
		return rc;

	/* Add HBRT buffer */
	rc = __mdst_table_add_entry((void *)HBRT_CON_START,
				    DUMP_SECTION_HBRT_LOG, HBRT_CON_LEN);

	return rc;
}

/* TCE mapping */
static inline void mdst_table_tce_map(void)
{
	fsp_tce_map(PSI_DMA_MDST_TABLE, mdst_table, PSI_DMA_MDST_TABLE_SIZE);
}

/* Initialize MDST table */
static int mdst_table_init(void)
{
	max_mdst_entry = PSI_DMA_MDST_TABLE_SIZE / sizeof(*mdst_table);
	printf("MDST: Max entries in MDST table : %d\n", max_mdst_entry);

	mdst_table = memalign(TCE_PSIZE, PSI_DMA_MDST_TABLE_SIZE);
	if (!mdst_table) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_INIT),
			 "MDST: Failed to allocate memory for MDST table.\n");
		return -ENOMEM;
	}

	memset(mdst_table, 0, PSI_DMA_MDST_TABLE_SIZE);
	mdst_table_tce_map();

	return OPAL_SUCCESS;
}

/*
 * Handle FSP R/R event.
 */
static bool fsp_mdst_update_rr(uint32_t cmd_sub_mod,
			       struct fsp_msg *msg __unused)
{
	switch (cmd_sub_mod) {
	case FSP_RESET_START:
		return true;
	case FSP_RELOAD_COMPLETE: /* Send MDST to FSP */
		fsp_update_mdst_table();
		return true;
	}
	return false;
}

static struct fsp_client fsp_mdst_client_rr = {
	.message = fsp_mdst_update_rr,
};

/* Initialize MDST table and send notification to FSP */
void fsp_mdst_table_init(void)
{
	if (!fsp_present())
		return;

	if (!fsp_mdst_supported())
		return;

	/* Initiate MDST */
	if (mdst_table_init() != OPAL_SUCCESS)
		return;

	/*
	 * Ignore return code from mdst_table_add_entries so that
	 * we can atleast capture partial dump.
	 */
	mdst_table_add_entries();
	fsp_update_mdst_table();

	/* Register for Class AA (FSP R/R) */
	fsp_register_client(&fsp_mdst_client_rr, FSP_MCLASS_RR_EVENT);
}