2 * PMU register read/write functions for TCG IBM POWER chips
4 * Copyright IBM Corp. 2021
7 * Daniel Henrique Barboza <danielhb413@gmail.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
16 * Checks whether the Group A SPR (MMCR0, MMCR2, MMCRA, and the
17 * PMCs) has problem state read access.
19 * Read acccess is granted for all PMCC values but 0b01, where a
20 * Facility Unavailable Interrupt will occur.
22 static bool spr_groupA_read_allowed(DisasContext *ctx)
24 if (!ctx->mmcr0_pmcc0 && ctx->mmcr0_pmcc1) {
25 gen_hvpriv_exception(ctx, POWERPC_EXCP_FU);
33 * Checks whether the Group A SPR (MMCR0, MMCR2, MMCRA, and the
34 * PMCs) has problem state write access.
36 * Write acccess is granted for PMCC values 0b10 and 0b11. Userspace
37 * writing with PMCC 0b00 will generate a Hypervisor Emulation
38 * Assistance Interrupt. Userspace writing with PMCC 0b01 will
39 * generate a Facility Unavailable Interrupt.
41 static bool spr_groupA_write_allowed(DisasContext *ctx)
43 if (ctx->mmcr0_pmcc0) {
47 if (ctx->mmcr0_pmcc1) {
49 gen_hvpriv_exception(ctx, POWERPC_EXCP_FU);
52 gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
59 * Helper function to avoid code repetition between MMCR0 and
60 * MMCR2 problem state write functions.
62 * 'ret' must be tcg_temp_freed() by the caller.
64 static TCGv masked_gprn_for_spr_write(int gprn, int sprn,
67 TCGv ret = tcg_temp_new();
68 TCGv t0 = tcg_temp_new();
70 /* 'ret' starts with all mask bits cleared */
71 gen_load_spr(ret, sprn);
72 tcg_gen_andi_tl(ret, ret, ~(spr_mask));
74 /* Apply the mask into 'gprn' in a temp var */
75 tcg_gen_andi_tl(t0, cpu_gpr[gprn], spr_mask);
77 /* Add the masked gprn bits into 'ret' */
78 tcg_gen_or_tl(ret, ret, t0);
85 void spr_read_MMCR0_ureg(DisasContext *ctx, int gprn, int sprn)
89 if (!spr_groupA_read_allowed(ctx)) {
96 * Filter out all bits but FC, PMAO, and PMAE, according
97 * to ISA v3.1, in 10.4.4 Monitor Mode Control Register 0,
100 gen_load_spr(t0, SPR_POWER_MMCR0);
101 tcg_gen_andi_tl(t0, t0, MMCR0_UREG_MASK);
102 tcg_gen_mov_tl(cpu_gpr[gprn], t0);
107 static void write_MMCR0_common(DisasContext *ctx, TCGv val)
110 * helper_store_mmcr0 will make clock based operations that
111 * will cause 'bad icount read' errors if we do not execute
112 * gen_icount_io_start() beforehand.
114 gen_icount_io_start(ctx);
115 gen_helper_store_mmcr0(cpu_env, val);
118 void spr_write_MMCR0_ureg(DisasContext *ctx, int sprn, int gprn)
122 if (!spr_groupA_write_allowed(ctx)) {
127 * Filter out all bits but FC, PMAO, and PMAE, according
128 * to ISA v3.1, in 10.4.4 Monitor Mode Control Register 0,
131 masked_gprn = masked_gprn_for_spr_write(gprn, SPR_POWER_MMCR0,
133 write_MMCR0_common(ctx, masked_gprn);
135 tcg_temp_free(masked_gprn);
138 void spr_read_MMCR2_ureg(DisasContext *ctx, int gprn, int sprn)
142 if (!spr_groupA_read_allowed(ctx)) {
149 * On read, filter out all bits that are not FCnP0 bits.
150 * When MMCR0[PMCC] is set to 0b10 or 0b11, providing
151 * problem state programs read/write access to MMCR2,
152 * only the FCnP0 bits can be accessed. All other bits are
153 * not changed when mtspr is executed in problem state, and
154 * all other bits return 0s when mfspr is executed in problem
155 * state, according to ISA v3.1, section 10.4.6 Monitor Mode
156 * Control Register 2, p. 1316, third paragraph.
158 gen_load_spr(t0, SPR_POWER_MMCR2);
159 tcg_gen_andi_tl(t0, t0, MMCR2_UREG_MASK);
160 tcg_gen_mov_tl(cpu_gpr[gprn], t0);
165 void spr_write_MMCR2_ureg(DisasContext *ctx, int sprn, int gprn)
169 if (!spr_groupA_write_allowed(ctx)) {
174 * Filter the bits that can be written using MMCR2_UREG_MASK,
175 * similar to what is done in spr_write_MMCR0_ureg().
177 masked_gprn = masked_gprn_for_spr_write(gprn, SPR_POWER_MMCR2,
179 gen_store_spr(SPR_POWER_MMCR2, masked_gprn);
181 tcg_temp_free(masked_gprn);
184 void spr_read_PMC(DisasContext *ctx, int gprn, int sprn)
186 TCGv_i32 t_sprn = tcg_const_i32(sprn);
188 gen_icount_io_start(ctx);
189 gen_helper_read_pmc(cpu_gpr[gprn], cpu_env, t_sprn);
191 tcg_temp_free_i32(t_sprn);
194 void spr_read_PMC14_ureg(DisasContext *ctx, int gprn, int sprn)
196 if (!spr_groupA_read_allowed(ctx)) {
200 spr_read_PMC(ctx, gprn, sprn + 0x10);
203 void spr_read_PMC56_ureg(DisasContext *ctx, int gprn, int sprn)
206 * If PMCC = 0b11, PMC5 and PMC6 aren't included in the Performance
207 * Monitor, and a read attempt results in a Facility Unavailable
210 if (ctx->mmcr0_pmcc0 && ctx->mmcr0_pmcc1) {
211 gen_hvpriv_exception(ctx, POWERPC_EXCP_FU);
215 /* The remaining steps are similar to PMCs 1-4 userspace read */
216 spr_read_PMC14_ureg(ctx, gprn, sprn);
219 void spr_write_PMC(DisasContext *ctx, int sprn, int gprn)
221 TCGv_i32 t_sprn = tcg_const_i32(sprn);
223 gen_icount_io_start(ctx);
224 gen_helper_store_pmc(cpu_env, t_sprn, cpu_gpr[gprn]);
226 tcg_temp_free_i32(t_sprn);
229 void spr_write_PMC14_ureg(DisasContext *ctx, int sprn, int gprn)
231 if (!spr_groupA_write_allowed(ctx)) {
235 spr_write_PMC(ctx, sprn + 0x10, gprn);
238 void spr_write_PMC56_ureg(DisasContext *ctx, int sprn, int gprn)
241 * If PMCC = 0b11, PMC5 and PMC6 aren't included in the Performance
242 * Monitor, and a write attempt results in a Facility Unavailable
245 if (ctx->mmcr0_pmcc0 && ctx->mmcr0_pmcc1) {
246 gen_hvpriv_exception(ctx, POWERPC_EXCP_FU);
250 /* The remaining steps are similar to PMCs 1-4 userspace write */
251 spr_write_PMC14_ureg(ctx, sprn, gprn);
254 void spr_write_MMCR0(DisasContext *ctx, int sprn, int gprn)
256 write_MMCR0_common(ctx, cpu_gpr[gprn]);
259 void spr_write_MMCR1(DisasContext *ctx, int sprn, int gprn)
261 gen_icount_io_start(ctx);
262 gen_helper_store_mmcr1(cpu_env, cpu_gpr[gprn]);
265 void spr_read_MMCR0_ureg(DisasContext *ctx, int gprn, int sprn)
267 spr_read_ureg(ctx, gprn, sprn);
270 void spr_write_MMCR0_ureg(DisasContext *ctx, int sprn, int gprn)
272 spr_noaccess(ctx, gprn, sprn);
275 void spr_read_MMCR2_ureg(DisasContext *ctx, int gprn, int sprn)
277 spr_read_ureg(ctx, gprn, sprn);
280 void spr_write_MMCR2_ureg(DisasContext *ctx, int sprn, int gprn)
282 spr_noaccess(ctx, gprn, sprn);
285 void spr_read_PMC14_ureg(DisasContext *ctx, int gprn, int sprn)
287 spr_read_ureg(ctx, gprn, sprn);
290 void spr_read_PMC56_ureg(DisasContext *ctx, int gprn, int sprn)
292 spr_read_ureg(ctx, gprn, sprn);
295 void spr_write_PMC14_ureg(DisasContext *ctx, int sprn, int gprn)
297 spr_noaccess(ctx, gprn, sprn);
300 void spr_write_PMC56_ureg(DisasContext *ctx, int sprn, int gprn)
302 spr_noaccess(ctx, gprn, sprn);
305 void spr_write_MMCR0(DisasContext *ctx, int sprn, int gprn)
307 spr_write_generic(ctx, sprn, gprn);
310 void spr_write_MMCR1(DisasContext *ctx, int sprn, int gprn)
312 spr_write_generic(ctx, sprn, gprn);
315 void spr_write_PMC(DisasContext *ctx, int sprn, int gprn)
317 spr_write_generic(ctx, sprn, gprn);
319 #endif /* defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY) */