summaryrefslogtreecommitdiff
path: root/platform/generic/mips/p8700.c
blob: ff13d37bf1fc962223ea99fcc2b183ccc427a6df (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2025 MIPS
 *
 */

#include <sbi/riscv_io.h>
#include <sbi/sbi_domain.h>
#include <sbi/sbi_error.h>
#include <sbi/sbi_hsm.h>
#include <sbi/sbi_timer.h>
#include <sbi_utils/fdt/fdt_helper.h>
#include <mips/p8700.h>
#include <mips/mips-cm.h>

const struct p8700_cm_info *p8700_cm_info;

void mips_p8700_dump_mmio(void)
{
	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
	unsigned hartid = current_hartid();
	unsigned cl = cpu_cluster(hartid);
	unsigned long cm_base = p8700_cm_info->gcr_base[cl];
	u64 gcr_config = readq((void*)cm_base + GCR_GLOBAL_CONFIG);
	int num_mmios = EXTRACT_FIELD(gcr_config, GCR_GC_NUM_MMIOS);
	static const char *ports[16] = {
		[0] = "MEM",
		[1] = "? 1",
		[2] = "? 2",
		[3] = "? 3",
		[4] = "? 4",
		[5] = "? 5",
		[6] = "? 6",
		[7] = "? 7",
		[8]  = "AUX0",
		[9]  = "AUX1",
		[10] = "AUX2",
		[11] = "AUX3",
		[12] = "? 12",
		[13] = "? 13",
		[14] = "? 14",
		[15] = "? 15",
	};
	static const char *ccas[4] = {
		[0] = "ANY",
		[1] = "UC",
		[2] = "UCA",
		[3] = "UC|UCA",
	};
	static const char *ncs[2] = {
		[0] = "  ",
		[1] = "NC",
	};
	static const char *dis_rqs[2] = {
		[0] = "          ",
		[1] = "DIS_RQ_LIM",
	};
	if (!(scratch->options & SBI_SCRATCH_DEBUG_PRINTS))
		return;

	sbi_printf("Cluster %d: %d MMIO regions\n", cl, num_mmios);
	for (int i = 0; i < num_mmios; i++) {
		u64 b = readq((void*)cm_base + GCR_MMIO_BOTTOM(i));
		u64 t = readq((void*)cm_base + GCR_MMIO_TOP(i));
		if (b & GCR_MMIO_BOTTOM_EN) {
			ulong ta = EXTRACT_FIELD(t, GCR_MMIO_ADDR) << 16;
			ulong ba = EXTRACT_FIELD(b, GCR_MMIO_ADDR) << 16;
			int cca = EXTRACT_FIELD(b, GCR_MMIO_BOTTOM_CCA);
			int nc = EXTRACT_FIELD(b, GCR_MMIO_BOTTOM_FORCE_NC);
			int port = EXTRACT_FIELD(b, GCR_MMIO_BOTTOM_PORT);
			int dis_rq_lim = EXTRACT_FIELD(b, GCR_MMIO_BOTTOM_DIS_RQ_LIM);

			sbi_printf("  [%d] : 0x%016lx-0x%016lx %4s %6s %s %s\n", i,
				   ba, ta, ports[port], ccas[cca], ncs[nc], dis_rqs[dis_rq_lim]);
		} else {
			sbi_printf("  [%d] : --disabled--\n", i);
		}
	}
}

void mips_p8700_pmp_set(unsigned int n, unsigned long flags,
			unsigned long prot, unsigned long addr,
			unsigned long log2len)
{
	int pmacfg_csr, pmacfg_shift;
	unsigned long cfgmask;
	unsigned long pmacfg, cca;

	pmacfg_csr = (CSR_MIPSPMACFG0 + (n >> 2)) & ~1;
	pmacfg_shift = (n & 7) << 3;
	cfgmask = ~(0xffUL << pmacfg_shift);

	/* Read pmacfg to change cacheability */
	pmacfg = (csr_read_num(pmacfg_csr) & cfgmask);
	cca = (flags & SBI_DOMAIN_MEMREGION_MMIO) ? CCA_CACHE_DISABLE :
				  CCA_CACHE_ENABLE | PMA_SPECULATION;
	pmacfg |= ((cca << pmacfg_shift) & ~cfgmask);
	csr_write_num(pmacfg_csr, pmacfg);
}

static void mips_p8700_sync_hrtimer(unsigned int cl)
{
	u64 v1, v2, mv, delta;
	volatile u64 *my_timer = (volatile u64 *)(p8700_cm_info->gcr_base[cl] + CPC_OFFSET + CPC_HRTIME);
	volatile u64 *ref_timer = (volatile u64 *)(p8700_cm_info->gcr_base[0] + CPC_OFFSET + CPC_HRTIME);

	v1 = readq_relaxed(my_timer);
	mv = readq_relaxed(ref_timer);
	v2 = readq_relaxed(my_timer);
	delta = mv - ((v1 / 2) + (v2 / 2));
	writeq_relaxed(readq_relaxed(my_timer) + delta, my_timer);
}

void mips_p8700_power_up_other_cluster(u32 hartid)
{
	unsigned int cl = cpu_cluster(hartid);

	/* Power up CM in cluster */
	write_cpc_pwrup_ctl(hartid, 1);

	/* Wait for the CM to start up */
	for (int i = 100; i > 0; i--) {
		u32 stat = read_cpc_cm_stat_conf(hartid);

		stat = EXTRACT_FIELD(stat, CPC_Cx_STAT_CONF_SEQ_STATE);
		if (stat == CPC_Cx_STAT_CONF_SEQ_STATE_U5) {
			if (cl) /* sync high-res timer to cluster 0 */
				mips_p8700_sync_hrtimer(cl);
			return;
		}
		cpu_relax();
	}
	sbi_printf("ERROR: Fail to power up cluster %u\n", cl);
}

extern void mips_warm_boot(void);

struct mips_boot_params {
	u32 hartid;
	u32 target_state;
};

static bool mips_hart_reached_state(void *arg)
{
	struct mips_boot_params *p = arg;
	u32 stat = read_cpc_co_stat_conf(p->hartid);

	stat = EXTRACT_FIELD(stat, CPC_Cx_STAT_CONF_SEQ_STATE);
	return stat == p->target_state;
}

int mips_p8700_hart_start(u32 hartid, ulong saddr)
{
	/* Hart 0 is the boot hart, and we don't use the CPC cmd to start.  */
	if (hartid == 0)
		return SBI_ENOTSUPP;

	/* Change reset base to mips_warm_boot */
	write_gcr_co_reset_base(hartid, (unsigned long)mips_warm_boot);

	if (cpu_hart(hartid) == 0) {
		unsigned int const timeout_ms = 10;
		bool booted;
		struct mips_boot_params p = {
			.hartid = hartid,
			.target_state = CPC_Cx_STAT_CONF_SEQ_STATE_U6,
		};

		/* Ensure its coherency is disabled */
		write_gcr_co_coherence(hartid, 0);

		/* Start cluster cl core co hart 0 */
		write_cpc_co_vp_run(hartid, 1 << cpu_hart(hartid));

		/* Reset cluster cl core co hart 0 */
		write_cpc_co_cmd(hartid, CPC_Cx_CMD_RESET);

		booted = sbi_timer_waitms_until(mips_hart_reached_state, &p, timeout_ms);
		if (!booted) {
			sbi_printf("ERROR: failed to boot hart 0x%x in %d ms\n",
				   hartid, timeout_ms);
			return -SBI_ETIMEDOUT;
		}
	} else {
		write_cpc_co_vp_run(hartid, 1 << cpu_hart(hartid));
	}

	return 0;
}

int mips_p8700_hart_stop()
{
	u32 hartid = current_hartid();

	/* Hart 0 is the boot hart, and we don't use the CPC cmd to stop.  */
	if (hartid == 0)
		return SBI_ENOTSUPP;

	write_cpc_co_vp_stop(hartid, 1 << cpu_hart(hartid));

	return 0;
}

void mips_p8700_cache_info(struct p8700_cache_info *l1d, struct p8700_cache_info *l1i,
			   struct p8700_cache_info *l2)
{
	u32 mipsconfig1 = csr_read(CSR_MIPSCONFIG1);

	if (l1d) {
		u32 da = EXTRACT_FIELD(mipsconfig1, MIPSCONFIG1_DA);
		u32 dl = EXTRACT_FIELD(mipsconfig1, MIPSCONFIG1_DL);
		u32 ds = EXTRACT_FIELD(mipsconfig1, MIPSCONFIG1_DS);

		l1d->line = dl ? 1 << (dl + 1) : 0;
		l1d->assoc_ways = da +1;
		l1d->sets = ds == 7 ? 32 : 1 << (ds + 6);
	}
	if (l1i) {
		u32 ia = EXTRACT_FIELD(mipsconfig1, MIPSCONFIG1_IA);
		u32 il = EXTRACT_FIELD(mipsconfig1, MIPSCONFIG1_IL);
		u32 is = EXTRACT_FIELD(mipsconfig1, MIPSCONFIG1_IS);

		l1i->line = il ? 1 << (il + 1) : 0;
		l1i->assoc_ways = ia +1;
		l1i->sets = is == 7 ? 32 : 1 << (is + 6);
	}
	if (l2) {
		if (mipsconfig1 & MIPSCONFIG1_L2C) {
			void *cm_base = (void *)p8700_cm_info->gcr_base[0];
			u32 l2_config = readl(cm_base + GCR_L2_CONFIG);

			if (l2_config & GCR_L2_REG_EXISTS) {
				u32 l2a = EXTRACT_FIELD(l2_config, GCR_L2_ASSOC);
				u32 l2l = EXTRACT_FIELD(l2_config, GCR_L2_LINE_SIZE);
				u32 l2s = EXTRACT_FIELD(l2_config, GCR_L2_SET_SIZE);

				l2->assoc_ways = l2a + 1;
				l2->line = 1 << (l2l + 1);
				l2->sets = 1 << (l2s + 6);
				return;
			}
		}
		l2->line = 0;
		l2->assoc_ways = 0;
		l2->sets = 0;
	}
}

/**
 * See CPU cluster memory map in the table below
 * To save PMP regions, group areas with M mode access, marked (1) and (2)
 *
 * GCR_BASE offset   |   |   | Block Name   | Description
 * 0x00000 - 0x01FFF | M | ^ | GCR.Global   | Per-cluster CM registers.
 * 0x02000 - 0x05FFF | M | | | GCR.Core     | Per-core CM registers.
 * 0x06000 - 0x07FFF | - |(1)| Reserved.
 * 0x08000 - 0x09FFF | M | | | CPC.Global   | Per-cluster CPC registers.
 * 0x0A000 - 0x0EFFF | M | | | CPC.Core     | Per-core/Per-device CPC registers.
 * 0x0F000 - 0x0FFFF | - | v | Reserved.
 * 0x10000 - 0x1FFFF | S |   | uGCR         | Reserved for user defined CM registers.
 * 0x20000 - 0x3EFFF | - |   | Reserved.
 * 0x3F000 - 0x3F0FF | ? |   | FDC.Global   | FDC.Global registers.
 * 0x3F100 - 0x3FFFF | ? |   | TRF.Global   | TRF.Global registers
 * 0x40000 - 0x4BFFF | M | ^ | APLIC.M      | APLIC Machine registers.
 * 0x4C000 - 0x4CFFF | M |(2)| APLIC.custom | APLIC custom registers.
 * 0x4D000 - 0x4FFFF | - | | | Reserved.
 * 0x50000 - 0x5FFFF | M | v | ACLINT.M     | ACLINT Machine registers.
 * 0x60000 - 0x6BFFF | S |   | APLIC.S      | APLIC Supervisor registers.
 * 0x6C000 - 0x6FFFF | S |   | ACLINT.S     | ACLINT Supervisor registers.
 * 0x70000 - 0x7EFFF | - |   | Reserved.
 * 0x7F000 - 0x7FFFF | S |   | GCR.U        | User Mode GCRs.
 */
int mips_p8700_add_memranges(void)
{
	int rc = SBI_OK;
	for (int i = 0; i < p8700_cm_info->num_cm; i++) {
		unsigned long cm_base = p8700_cm_info->gcr_base[i];

		/* CM and MTIMER */
		rc = sbi_domain_root_add_memrange(cm_base, SIZE_FOR_CPC_MTIME,
						  SIZE_FOR_CPC_MTIME,
						  (SBI_DOMAIN_MEMREGION_MMIO |
						   SBI_DOMAIN_MEMREGION_M_READABLE |
						   SBI_DOMAIN_MEMREGION_M_WRITABLE));
		if (rc)
			return rc;

		/* For the APLIC and ACLINT m-mode region */
		rc = sbi_domain_root_add_memrange(cm_base + AIA_OFFSET, SIZE_FOR_AIA_M_MODE,
						  SIZE_FOR_AIA_M_MODE,
						  (SBI_DOMAIN_MEMREGION_MMIO |
						   SBI_DOMAIN_MEMREGION_M_READABLE |
						   SBI_DOMAIN_MEMREGION_M_WRITABLE));
		if (rc)
			return rc;
	}
	return rc;
}

int mips_p8700_platform_init(const void *fdt, int nodeoff, const struct fdt_match *match)
{
	const struct p8700_cm_info *data = match->data;

	if (!data) {
		sbi_printf("Missing CM info for %s\n", match->compatible);
		return SBI_EINVAL;
	}

	p8700_cm_info = data;
	return SBI_OK;
}