aboutsummaryrefslogtreecommitdiff
path: root/hw/cache-p9.c
blob: fb5ce308719c1cb2eb49bf1e1722f57c6a4f7b44 (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
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
/*
 * Copyright 2019 IBM Corp.
 */

#include <skiboot.h>
#include <chip.h>
#include <xscom.h>
#include <timebase.h>
#include <xscom-p9-regs.h>
#include <cache-p9.h>

/* Registers and bits used to clear the L2 and L3 cache */
#define L2_PRD_PURGE_CMD_REG			0x1080e
#define   L2_PRD_PURGE_CMD_TRIGGER		PPC_BIT(0)
#define   L2_PRD_PURGE_CMD_TYPE_MASK		PPC_BITMASK(1, 4)
#define     L2CAC_FLUSH				0x0
#define   L2_PRD_PURGE_CMD_REG_BUSY		PPC_BIT(9)
#define L3_PRD_PURGE_REG			0x1180e
#define   L3_PRD_PURGE_REQ			PPC_BIT(0)
#define   L3_PRD_PURGE_TTYPE_MASK		PPC_BITMASK(1, 4)
#define     L3_FULL_PURGE			0x0

#define L2_L3_PRD_PURGE_TIMEOUT_MS		20

static int start_l2_purge(uint32_t chip_id, uint32_t core_id)
{
	uint64_t addr = XSCOM_ADDR_P9_EX(core_id, L2_PRD_PURGE_CMD_REG);
	int rc;

	rc = xscom_write_mask(chip_id, addr, L2CAC_FLUSH,
			      L2_PRD_PURGE_CMD_TYPE_MASK);
	if (!rc)
		rc = xscom_write_mask(chip_id, addr, L2_PRD_PURGE_CMD_TRIGGER,
			      L2_PRD_PURGE_CMD_TRIGGER);
	if (rc)
		prlog(PR_ERR, "PURGE L2 on core 0x%x: XSCOM write_mask "
		      "failed %i\n", core_id, rc);
	return rc;
}

static int wait_l2_purge(uint32_t chip_id, uint32_t core_id)
{
	uint64_t val;
	uint64_t addr = XSCOM_ADDR_P9_EX(core_id, L2_PRD_PURGE_CMD_REG);
	unsigned long now = mftb();
	unsigned long end = now + msecs_to_tb(L2_L3_PRD_PURGE_TIMEOUT_MS);
	int rc;

	while (1) {
		rc = xscom_read(chip_id, addr, &val);
		if (rc) {
			prlog(PR_ERR, "PURGE L2 on core 0x%x: XSCOM read "
			      "failed %i\n", core_id, rc);
			break;
		}
		if (!(val & L2_PRD_PURGE_CMD_REG_BUSY))
			break;
		now = mftb();
		if (tb_compare(now, end) == TB_AAFTERB) {
			prlog(PR_ERR, "PURGE L2 on core 0x%x timed out %i\n",
			      core_id, rc);
			return OPAL_BUSY;
		}
	}

	/* We have to clear the trigger bit ourselves */
	val &= ~L2_PRD_PURGE_CMD_TRIGGER;
	rc = xscom_write(chip_id, addr, val);
	if (rc)
		prlog(PR_ERR, "PURGE L2 on core 0x%x: XSCOM write failed %i\n",
		      core_id, rc);
	return rc;
}

static int start_l3_purge(uint32_t chip_id, uint32_t core_id)
{
	uint64_t addr = XSCOM_ADDR_P9_EX(core_id, L3_PRD_PURGE_REG);
	int rc;

	rc = xscom_write_mask(chip_id, addr, L3_FULL_PURGE,
			      L3_PRD_PURGE_TTYPE_MASK);
	if (!rc)
		rc = xscom_write_mask(chip_id, addr, L3_PRD_PURGE_REQ,
			      L3_PRD_PURGE_REQ);
	if (rc)
		prlog(PR_ERR, "PURGE L3 on core 0x%x: XSCOM write_mask "
		      "failed %i\n", core_id, rc);
	return rc;
}

static int wait_l3_purge(uint32_t chip_id, uint32_t core_id)
{
	uint64_t val;
	uint64_t addr = XSCOM_ADDR_P9_EX(core_id, L3_PRD_PURGE_REG);
	unsigned long now = mftb();
	unsigned long end = now + msecs_to_tb(L2_L3_PRD_PURGE_TIMEOUT_MS);
	int rc;

	/* Trigger bit is automatically set to zero when flushing is done */
	while (1) {
		rc = xscom_read(chip_id, addr, &val);
		if (rc) {
			prlog(PR_ERR, "PURGE L3 on core 0x%x: XSCOM read "
			      "failed %i\n", core_id, rc);
			break;
		}
		if (!(val & L3_PRD_PURGE_REQ))
			break;
		now = mftb();
		if (tb_compare(now, end) == TB_AAFTERB) {
			prlog(PR_ERR, "PURGE L3 on core 0x%x timed out %i\n",
			      core_id, rc);
			return OPAL_BUSY;
		}
	}
	return rc;
}

int64_t purge_l2_l3_caches(void)
{
	struct cpu_thread *t;
	uint64_t core_id, prev_core_id = (uint64_t)-1;
	int rc;
	unsigned long now = mftb();

	for_each_ungarded_cpu(t) {
		/* Only need to do it once per core chiplet */
		core_id = pir_to_core_id(t->pir);
		if (prev_core_id == core_id)
			continue;
		prev_core_id = core_id;
		rc = start_l2_purge(t->chip_id, core_id);
		if (rc)
			goto trace_exit;
		rc = start_l3_purge(t->chip_id, core_id);
		if (rc)
			goto trace_exit;
	}

	prev_core_id = (uint64_t)-1;
	for_each_ungarded_cpu(t) {
		/* Only need to do it once per core chiplet */
		core_id = pir_to_core_id(t->pir);
		if (prev_core_id == core_id)
			continue;
		prev_core_id = core_id;

		rc = wait_l2_purge(t->chip_id, core_id);
		if (rc)
			goto trace_exit;
		rc = wait_l3_purge(t->chip_id, core_id);
		if (rc)
			goto trace_exit;
	}

trace_exit:
	prlog(PR_TRACE, "L2/L3 purging took %ldus\n",
			tb_to_usecs(mftb() - now));

	return rc;
}