113f50867SNicholas Piggin/*
213f50867SNicholas Piggin * Power ISA decode for misc instructions
313f50867SNicholas Piggin *
413f50867SNicholas Piggin * Copyright (c) 2024, IBM Corporation.
513f50867SNicholas Piggin *
613f50867SNicholas Piggin * This library is free software; you can redistribute it and/or
713f50867SNicholas Piggin * modify it under the terms of the GNU Lesser General Public
813f50867SNicholas Piggin * License as published by the Free Software Foundation; either
913f50867SNicholas Piggin * version 2.1 of the License, or (at your option) any later version.
1013f50867SNicholas Piggin *
1113f50867SNicholas Piggin * This library is distributed in the hope that it will be useful,
1213f50867SNicholas Piggin * but WITHOUT ANY WARRANTY; without even the implied warranty of
1313f50867SNicholas Piggin * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1413f50867SNicholas Piggin * Lesser General Public License for more details.
1513f50867SNicholas Piggin *
1613f50867SNicholas Piggin * You should have received a copy of the GNU Lesser General Public
1713f50867SNicholas Piggin * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1813f50867SNicholas Piggin */
1913f50867SNicholas Piggin
2013f50867SNicholas Piggin/*
2113f50867SNicholas Piggin * Memory Barrier Instructions
2213f50867SNicholas Piggin */
2313f50867SNicholas Piggin
2413f50867SNicholas Pigginstatic bool trans_SYNC(DisasContext *ctx, arg_X_sync *a)
2513f50867SNicholas Piggin{
2613f50867SNicholas Piggin    TCGBar bar = TCG_MO_ALL;
2713f50867SNicholas Piggin    uint32_t l = a->l;
2813f50867SNicholas Piggin
2913f50867SNicholas Piggin    /*
3013f50867SNicholas Piggin     * BookE uses the msync mnemonic. This means hwsync, except in the
3113f50867SNicholas Piggin     * 440, where it an execution serialisation point that requires all
3213f50867SNicholas Piggin     * previous storage accesses to have been performed to memory (which
3313f50867SNicholas Piggin     * doesn't matter for TCG).
3413f50867SNicholas Piggin     */
3513f50867SNicholas Piggin    if (!(ctx->insns_flags & PPC_MEM_SYNC)) {
3613f50867SNicholas Piggin        if (ctx->insns_flags & PPC_BOOKE) {
37*ab4f174bSNicholas Piggin            tcg_gen_mb(bar | TCG_BAR_SC);
3813f50867SNicholas Piggin            return true;
3913f50867SNicholas Piggin        }
4013f50867SNicholas Piggin
4113f50867SNicholas Piggin        return false;
4213f50867SNicholas Piggin    }
4313f50867SNicholas Piggin
4413f50867SNicholas Piggin    if ((l == 1) && (ctx->insns_flags2 & PPC2_MEM_LWSYNC)) {
4513f50867SNicholas Piggin        bar = TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST;
4613f50867SNicholas Piggin    }
4713f50867SNicholas Piggin
4813f50867SNicholas Piggin    /*
4913f50867SNicholas Piggin     * We may need to check for a pending TLB flush.
5013f50867SNicholas Piggin     *
5113f50867SNicholas Piggin     * We do this on ptesync (l == 2) on ppc64 and any sync on ppc32.
5213f50867SNicholas Piggin     *
5313f50867SNicholas Piggin     * Additionally, this can only happen in kernel mode however so
5413f50867SNicholas Piggin     * check MSR_PR as well.
5513f50867SNicholas Piggin     */
5613f50867SNicholas Piggin    if (((l == 2) || !(ctx->insns_flags & PPC_64B)) && !ctx->pr) {
5713f50867SNicholas Piggin        gen_check_tlb_flush(ctx, true);
5813f50867SNicholas Piggin    }
5913f50867SNicholas Piggin
6013f50867SNicholas Piggin    tcg_gen_mb(bar | TCG_BAR_SC);
6113f50867SNicholas Piggin
6213f50867SNicholas Piggin    return true;
6313f50867SNicholas Piggin}
6413f50867SNicholas Piggin
6513f50867SNicholas Pigginstatic bool trans_EIEIO(DisasContext *ctx, arg_EIEIO *a)
6613f50867SNicholas Piggin{
6713f50867SNicholas Piggin    TCGBar bar = TCG_MO_ALL;
6813f50867SNicholas Piggin
6913f50867SNicholas Piggin    /*
7013f50867SNicholas Piggin     * BookE uses the mbar instruction instead of eieio, which is basically
7113f50867SNicholas Piggin     * full hwsync memory barrier, but is not execution synchronising. For
7213f50867SNicholas Piggin     * the purpose of TCG the distinction is not relevant.
7313f50867SNicholas Piggin     */
7413f50867SNicholas Piggin    if (!(ctx->insns_flags & PPC_MEM_EIEIO)) {
7513f50867SNicholas Piggin        if ((ctx->insns_flags & PPC_BOOKE) ||
7613f50867SNicholas Piggin            (ctx->insns_flags2 & PPC2_BOOKE206)) {
77*ab4f174bSNicholas Piggin            tcg_gen_mb(bar | TCG_BAR_SC);
7813f50867SNicholas Piggin            return true;
7913f50867SNicholas Piggin        }
8013f50867SNicholas Piggin        return false;
8113f50867SNicholas Piggin    }
8213f50867SNicholas Piggin
8313f50867SNicholas Piggin    /*
8413f50867SNicholas Piggin     * eieio has complex semanitcs. It provides memory ordering between
8513f50867SNicholas Piggin     * operations in the set:
8613f50867SNicholas Piggin     * - loads from CI memory.
8713f50867SNicholas Piggin     * - stores to CI memory.
8813f50867SNicholas Piggin     * - stores to WT memory.
8913f50867SNicholas Piggin     *
9013f50867SNicholas Piggin     * It separately also orders memory for operations in the set:
9113f50867SNicholas Piggin     * - stores to cacheble memory.
9213f50867SNicholas Piggin     *
9313f50867SNicholas Piggin     * It also serializes instructions:
9413f50867SNicholas Piggin     * - dcbt and dcbst.
9513f50867SNicholas Piggin     *
9613f50867SNicholas Piggin     * It separately serializes:
9713f50867SNicholas Piggin     * - tlbie and tlbsync.
9813f50867SNicholas Piggin     *
9913f50867SNicholas Piggin     * And separately serializes:
10013f50867SNicholas Piggin     * - slbieg, slbiag, and slbsync.
10113f50867SNicholas Piggin     *
10213f50867SNicholas Piggin     * The end result is that CI memory ordering requires TCG_MO_ALL
10313f50867SNicholas Piggin     * and it is not possible to special-case more relaxed ordering for
10413f50867SNicholas Piggin     * cacheable accesses. TCG_BAR_SC is required to provide this
10513f50867SNicholas Piggin     * serialization.
10613f50867SNicholas Piggin     */
10713f50867SNicholas Piggin
10813f50867SNicholas Piggin    /*
10913f50867SNicholas Piggin     * POWER9 has a eieio instruction variant using bit 6 as a hint to
11013f50867SNicholas Piggin     * tell the CPU it is a store-forwarding barrier.
11113f50867SNicholas Piggin     */
11213f50867SNicholas Piggin    if (ctx->opcode & 0x2000000) {
11313f50867SNicholas Piggin        /*
11413f50867SNicholas Piggin         * ISA says that "Reserved fields in instructions are ignored
11513f50867SNicholas Piggin         * by the processor". So ignore the bit 6 on non-POWER9 CPU but
11613f50867SNicholas Piggin         * as this is not an instruction software should be using,
11713f50867SNicholas Piggin         * complain to the user.
11813f50867SNicholas Piggin         */
11913f50867SNicholas Piggin        if (!(ctx->insns_flags2 & PPC2_ISA300)) {
12013f50867SNicholas Piggin            qemu_log_mask(LOG_GUEST_ERROR, "invalid eieio using bit 6 at @"
12113f50867SNicholas Piggin                          TARGET_FMT_lx "\n", ctx->cia);
12213f50867SNicholas Piggin        } else {
12313f50867SNicholas Piggin            bar = TCG_MO_ST_LD;
12413f50867SNicholas Piggin        }
12513f50867SNicholas Piggin    }
12613f50867SNicholas Piggin
12713f50867SNicholas Piggin    tcg_gen_mb(bar | TCG_BAR_SC);
12813f50867SNicholas Piggin
12913f50867SNicholas Piggin    return true;
13013f50867SNicholas Piggin}
131