xref: /openbmc/qemu/include/exec/cpu-defs.h (revision b11ec7f2)
1022c62cbSPaolo Bonzini /*
2022c62cbSPaolo Bonzini  * common defines for all CPUs
3022c62cbSPaolo Bonzini  *
4022c62cbSPaolo Bonzini  * Copyright (c) 2003 Fabrice Bellard
5022c62cbSPaolo Bonzini  *
6022c62cbSPaolo Bonzini  * This library is free software; you can redistribute it and/or
7022c62cbSPaolo Bonzini  * modify it under the terms of the GNU Lesser General Public
8022c62cbSPaolo Bonzini  * License as published by the Free Software Foundation; either
9022c62cbSPaolo Bonzini  * version 2 of the License, or (at your option) any later version.
10022c62cbSPaolo Bonzini  *
11022c62cbSPaolo Bonzini  * This library is distributed in the hope that it will be useful,
12022c62cbSPaolo Bonzini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13022c62cbSPaolo Bonzini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14022c62cbSPaolo Bonzini  * Lesser General Public License for more details.
15022c62cbSPaolo Bonzini  *
16022c62cbSPaolo Bonzini  * You should have received a copy of the GNU Lesser General Public
17022c62cbSPaolo Bonzini  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18022c62cbSPaolo Bonzini  */
19022c62cbSPaolo Bonzini #ifndef CPU_DEFS_H
20022c62cbSPaolo Bonzini #define CPU_DEFS_H
21022c62cbSPaolo Bonzini 
22022c62cbSPaolo Bonzini #ifndef NEED_CPU_H
23022c62cbSPaolo Bonzini #error cpu.h included from common code
24022c62cbSPaolo Bonzini #endif
25022c62cbSPaolo Bonzini 
2687776ab7SPaolo Bonzini #include "qemu/host-utils.h"
271de7afc9SPaolo Bonzini #include "qemu/queue.h"
28*b11ec7f2SYang Zhong #ifdef CONFIG_TCG
291de29aefSPaolo Bonzini #include "tcg-target.h"
30*b11ec7f2SYang Zhong #endif
31ce927ed9SAndreas Färber #ifndef CONFIG_USER_ONLY
32022c62cbSPaolo Bonzini #include "exec/hwaddr.h"
33ce927ed9SAndreas Färber #endif
34fadc1cbeSPeter Maydell #include "exec/memattrs.h"
35022c62cbSPaolo Bonzini 
36022c62cbSPaolo Bonzini #ifndef TARGET_LONG_BITS
37022c62cbSPaolo Bonzini #error TARGET_LONG_BITS must be defined before including this header
38022c62cbSPaolo Bonzini #endif
39022c62cbSPaolo Bonzini 
40022c62cbSPaolo Bonzini #define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
41022c62cbSPaolo Bonzini 
42022c62cbSPaolo Bonzini /* target_ulong is the type of a virtual address */
43022c62cbSPaolo Bonzini #if TARGET_LONG_SIZE == 4
446cfd9b52SPaolo Bonzini typedef int32_t target_long;
456cfd9b52SPaolo Bonzini typedef uint32_t target_ulong;
46022c62cbSPaolo Bonzini #define TARGET_FMT_lx "%08x"
47022c62cbSPaolo Bonzini #define TARGET_FMT_ld "%d"
48022c62cbSPaolo Bonzini #define TARGET_FMT_lu "%u"
49022c62cbSPaolo Bonzini #elif TARGET_LONG_SIZE == 8
506cfd9b52SPaolo Bonzini typedef int64_t target_long;
516cfd9b52SPaolo Bonzini typedef uint64_t target_ulong;
52022c62cbSPaolo Bonzini #define TARGET_FMT_lx "%016" PRIx64
53022c62cbSPaolo Bonzini #define TARGET_FMT_ld "%" PRId64
54022c62cbSPaolo Bonzini #define TARGET_FMT_lu "%" PRIu64
55022c62cbSPaolo Bonzini #else
56022c62cbSPaolo Bonzini #error TARGET_LONG_SIZE undefined
57022c62cbSPaolo Bonzini #endif
58022c62cbSPaolo Bonzini 
59*b11ec7f2SYang Zhong #if !defined(CONFIG_USER_ONLY) && defined(CONFIG_TCG)
6088e89a57SXin Tong /* use a fully associative victim tlb of 8 entries */
6188e89a57SXin Tong #define CPU_VTLB_SIZE 8
62022c62cbSPaolo Bonzini 
63022c62cbSPaolo Bonzini #if HOST_LONG_BITS == 32 && TARGET_LONG_BITS == 32
64022c62cbSPaolo Bonzini #define CPU_TLB_ENTRY_BITS 4
65022c62cbSPaolo Bonzini #else
66022c62cbSPaolo Bonzini #define CPU_TLB_ENTRY_BITS 5
67022c62cbSPaolo Bonzini #endif
68022c62cbSPaolo Bonzini 
691de29aefSPaolo Bonzini /* TCG_TARGET_TLB_DISPLACEMENT_BITS is used in CPU_TLB_BITS to ensure that
701de29aefSPaolo Bonzini  * the TLB is not unnecessarily small, but still small enough for the
711de29aefSPaolo Bonzini  * TLB lookup instruction sequence used by the TCG target.
721de29aefSPaolo Bonzini  *
731de29aefSPaolo Bonzini  * TCG will have to generate an operand as large as the distance between
741de29aefSPaolo Bonzini  * env and the tlb_table[NB_MMU_MODES - 1][0].addend.  For simplicity,
751de29aefSPaolo Bonzini  * the TCG targets just round everything up to the next power of two, and
761de29aefSPaolo Bonzini  * count bits.  This works because: 1) the size of each TLB is a largish
771de29aefSPaolo Bonzini  * power of two, 2) and because the limit of the displacement is really close
781de29aefSPaolo Bonzini  * to a power of two, 3) the offset of tlb_table[0][0] inside env is smaller
791de29aefSPaolo Bonzini  * than the size of a TLB.
801de29aefSPaolo Bonzini  *
811de29aefSPaolo Bonzini  * For example, the maximum displacement 0xFFF0 on PPC and MIPS, but TCG
821de29aefSPaolo Bonzini  * just says "the displacement is 16 bits".  TCG_TARGET_TLB_DISPLACEMENT_BITS
831de29aefSPaolo Bonzini  * then ensures that tlb_table at least 0x8000 bytes large ("not unnecessarily
841de29aefSPaolo Bonzini  * small": 2^15).  The operand then will come up smaller than 0xFFF0 without
851de29aefSPaolo Bonzini  * any particular care, because the TLB for a single MMU mode is larger than
861de29aefSPaolo Bonzini  * 0x10000-0xFFF0=16 bytes.  In the end, the maximum value of the operand
871de29aefSPaolo Bonzini  * could be something like 0xC000 (the offset of the last TLB table) plus
881de29aefSPaolo Bonzini  * 0x18 (the offset of the addend field in each TLB entry) plus the offset
891de29aefSPaolo Bonzini  * of tlb_table inside env (which is non-trivial but not huge).
901de29aefSPaolo Bonzini  */
911de29aefSPaolo Bonzini #define CPU_TLB_BITS                                             \
921de29aefSPaolo Bonzini     MIN(8,                                                       \
931de29aefSPaolo Bonzini         TCG_TARGET_TLB_DISPLACEMENT_BITS - CPU_TLB_ENTRY_BITS -  \
941de29aefSPaolo Bonzini         (NB_MMU_MODES <= 1 ? 0 :                                 \
951de29aefSPaolo Bonzini          NB_MMU_MODES <= 2 ? 1 :                                 \
961de29aefSPaolo Bonzini          NB_MMU_MODES <= 4 ? 2 :                                 \
971de29aefSPaolo Bonzini          NB_MMU_MODES <= 8 ? 3 : 4))
981de29aefSPaolo Bonzini 
991de29aefSPaolo Bonzini #define CPU_TLB_SIZE (1 << CPU_TLB_BITS)
1001de29aefSPaolo Bonzini 
101022c62cbSPaolo Bonzini typedef struct CPUTLBEntry {
102022c62cbSPaolo Bonzini     /* bit TARGET_LONG_BITS to TARGET_PAGE_BITS : virtual address
103022c62cbSPaolo Bonzini        bit TARGET_PAGE_BITS-1..4  : Nonzero for accesses that should not
104022c62cbSPaolo Bonzini                                     go directly to ram.
105022c62cbSPaolo Bonzini        bit 3                      : indicates that the entry is invalid
106022c62cbSPaolo Bonzini        bit 2..0                   : zero
107022c62cbSPaolo Bonzini     */
108b4a4b8d0SPeter Crosthwaite     union {
109b4a4b8d0SPeter Crosthwaite         struct {
110022c62cbSPaolo Bonzini             target_ulong addr_read;
111022c62cbSPaolo Bonzini             target_ulong addr_write;
112022c62cbSPaolo Bonzini             target_ulong addr_code;
113022c62cbSPaolo Bonzini             /* Addend to virtual address to get host address.  IO accesses
114022c62cbSPaolo Bonzini                use the corresponding iotlb value.  */
115022c62cbSPaolo Bonzini             uintptr_t addend;
116b4a4b8d0SPeter Crosthwaite         };
117022c62cbSPaolo Bonzini         /* padding to get a power of two size */
118b4a4b8d0SPeter Crosthwaite         uint8_t dummy[1 << CPU_TLB_ENTRY_BITS];
119b4a4b8d0SPeter Crosthwaite     };
120022c62cbSPaolo Bonzini } CPUTLBEntry;
121022c62cbSPaolo Bonzini 
122e85ef538SRichard Henderson QEMU_BUILD_BUG_ON(sizeof(CPUTLBEntry) != (1 << CPU_TLB_ENTRY_BITS));
123022c62cbSPaolo Bonzini 
124e469b22fSPeter Maydell /* The IOTLB is not accessed directly inline by generated TCG code,
125e469b22fSPeter Maydell  * so the CPUIOTLBEntry layout is not as critical as that of the
126e469b22fSPeter Maydell  * CPUTLBEntry. (This is also why we don't want to combine the two
127e469b22fSPeter Maydell  * structs into one.)
128e469b22fSPeter Maydell  */
129e469b22fSPeter Maydell typedef struct CPUIOTLBEntry {
130e469b22fSPeter Maydell     hwaddr addr;
131fadc1cbeSPeter Maydell     MemTxAttrs attrs;
132e469b22fSPeter Maydell } CPUIOTLBEntry;
133e469b22fSPeter Maydell 
134022c62cbSPaolo Bonzini #define CPU_COMMON_TLB \
135022c62cbSPaolo Bonzini     /* The meaning of the MMU modes is defined in the target code. */   \
136022c62cbSPaolo Bonzini     CPUTLBEntry tlb_table[NB_MMU_MODES][CPU_TLB_SIZE];                  \
13788e89a57SXin Tong     CPUTLBEntry tlb_v_table[NB_MMU_MODES][CPU_VTLB_SIZE];               \
138e469b22fSPeter Maydell     CPUIOTLBEntry iotlb[NB_MMU_MODES][CPU_TLB_SIZE];                    \
139e469b22fSPeter Maydell     CPUIOTLBEntry iotlb_v[NB_MMU_MODES][CPU_VTLB_SIZE];                 \
140022c62cbSPaolo Bonzini     target_ulong tlb_flush_addr;                                        \
14188e89a57SXin Tong     target_ulong tlb_flush_mask;                                        \
14288e89a57SXin Tong     target_ulong vtlb_index;                                            \
143022c62cbSPaolo Bonzini 
144022c62cbSPaolo Bonzini #else
145022c62cbSPaolo Bonzini 
146022c62cbSPaolo Bonzini #define CPU_COMMON_TLB
147022c62cbSPaolo Bonzini 
148022c62cbSPaolo Bonzini #endif
149022c62cbSPaolo Bonzini 
150022c62cbSPaolo Bonzini 
151022c62cbSPaolo Bonzini #define CPU_COMMON                                                      \
152022c62cbSPaolo Bonzini     /* soft mmu support */                                              \
153022c62cbSPaolo Bonzini     CPU_COMMON_TLB                                                      \
154022c62cbSPaolo Bonzini 
155022c62cbSPaolo Bonzini #endif
156