File indexing completed on 2025-05-11 08:23:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 #ifdef __ALTIVEC__
0049
0050 #include <rtems.h>
0051 #include <libcpu/cpuIdent.h>
0052 #include <rtems/bspIo.h>
0053 #include <rtems/error.h>
0054 #include <rtems/score/cpu.h>
0055 #include <rtems/powerpc/powerpc.h>
0056
0057 #define STATIC static
0058
0059 #define VEC_ALIGNMENT 16
0060
0061 #define NAM "AltiVec Support"
0062 #define ERRID(a,b,c,d) (((a)<<24) | ((b)<<16) | ((c)<<8) | (d))
0063
0064 typedef uint32_t _vu32 __attribute__((vector_size(VEC_ALIGNMENT)));
0065
0066 #ifndef MSR_VE
0067 #define MSR_VE (1<<(31-6))
0068 #endif
0069
0070
0071
0072
0073 uint32_t _CPU_altivec_ctxt_off = 0;
0074 uint32_t _CPU_altivec_psim_cpu = 0;
0075
0076 static inline uint32_t
0077 mfmsr(void)
0078 {
0079 uint32_t v;
0080 _CPU_MSR_GET(v);
0081 return v;
0082 }
0083
0084 static inline void
0085 mtmsr(uint32_t v)
0086 {
0087 _CPU_MSR_SET(v);
0088 }
0089
0090 static inline void
0091 isync(void)
0092 {
0093 asm volatile("isync");
0094 }
0095
0096 static inline void
0097 dssall(void)
0098 {
0099 if ( !_CPU_altivec_psim_cpu)
0100 asm volatile("dssall");
0101 }
0102
0103 static inline uint32_t
0104 set_MSR_VE(void)
0105 {
0106 uint32_t rval;
0107 rval=mfmsr();
0108 if ( ! (MSR_VE & rval ) ) {
0109 mtmsr(rval | MSR_VE);
0110 isync();
0111 }
0112 return rval;
0113 }
0114
0115 static inline void
0116 clr_MSR_VE(void)
0117 {
0118 dssall();
0119 mtmsr(mfmsr() & ~MSR_VE);
0120 isync();
0121 }
0122
0123 static inline void
0124 rst_MSR_VE(uint32_t old)
0125 {
0126 if ( ! ( MSR_VE & old ) ) {
0127 dssall();
0128 mtmsr(old);
0129 isync();
0130 }
0131 }
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141 static void dummy(void) __attribute__((noinline));
0142
0143 static void dummy(void) { __asm__ volatile(""); }
0144
0145 static unsigned probe_r1(void) __attribute__((noinline));
0146 static unsigned probe_r1(void)
0147 {
0148 unsigned r1;
0149
0150
0151
0152
0153
0154
0155 dummy();
0156
0157 asm volatile("mr %0,1":"=r"(r1));
0158 return r1;
0159 }
0160
0161 static unsigned
0162 probe_ppc_stack_alignment(void)
0163 {
0164 unsigned r1;
0165 asm volatile("mr %0,1":"=r"(r1));
0166 return (r1 - probe_r1()) & ~ 0xf;
0167 }
0168
0169 STATIC int check_stack_alignment(void)
0170 {
0171 int rval = 0;
0172 if ( VEC_ALIGNMENT > PPC_STACK_ALIGNMENT ) {
0173 printk(NAM": CPU support has unsufficient stack alignment;\n");
0174 printk("modify 'cpukit/score/cpu/powerpc/rtems/score/powerpc.h'\n");
0175 printk("and choose PPC_ABI_SVR4. I'll enable a workaround for now.\n");
0176 rval |= 1;
0177 }
0178
0179 if ( probe_ppc_stack_alignment() < VEC_ALIGNMENT ) {
0180 printk(NAM": run-time stack alignment unsufficient; make sure you compile with -mabi=altivec\n");
0181 rval |= 2;
0182 }
0183 return rval;
0184 }
0185
0186
0187 static uint32_t probe_vrsave(_vu32 *p_v) __attribute__((noinline));
0188
0189
0190
0191
0192 static uint32_t probe_vrsave(_vu32 *p_v)
0193 {
0194 _vu32 x;
0195 uint32_t vrsave;
0196
0197
0198
0199
0200
0201
0202
0203 asm volatile("vxor %0, 0, 0; mfvrsave %1":"=v"(x),"=r"(vrsave)::"v0");
0204 if ( p_v ) {
0205 *p_v = x;
0206 }
0207 return vrsave;
0208 }
0209
0210 static int vrsave_yes(void) __attribute__((noinline));
0211
0212 static int vrsave_yes(void)
0213 {
0214 uint32_t vrsave_pre;
0215 asm volatile("mfvrsave %0":"=r"(vrsave_pre));
0216 if ( (vrsave_pre & 0x80000000) ) {
0217 printk(NAM": WARNING - unable to determine whether -mvrsave was used; assuming NO\n");
0218 return 0;
0219 }
0220 return probe_vrsave(0) != vrsave_pre;
0221 }
0222
0223 extern void
0224 _CPU_altivec_set_vrsave_initval(uint32_t);
0225
0226
0227 void
0228 _CPU_Initialize_altivec(void)
0229 {
0230 unsigned pvr;
0231
0232
0233
0234
0235
0236 _CPU_altivec_ctxt_off = offsetof(ppc_context, altivec);
0237
0238
0239
0240
0241 _CPU_altivec_ctxt_off += PPC_DEFAULT_CACHE_LINE_SIZE;
0242
0243
0244
0245
0246 _CPU_altivec_ctxt_off += PPC_CACHE_ALIGNMENT - 1;
0247
0248 if ( ! vrsave_yes() ) {
0249
0250
0251
0252
0253 _CPU_altivec_set_vrsave_initval( -1 );
0254 }
0255
0256 if ( check_stack_alignment() & 2 )
0257 rtems_fatal_error_occurred(ERRID('V','E','C','1'));
0258
0259 pvr = get_ppc_cpu_type();
0260
0261 _CPU_altivec_psim_cpu = (PPC_PSIM == pvr);
0262
0263 if ( ! ppc_cpu_has_altivec() ) {
0264 printk(NAM": This CPU seems not to have AltiVec\n");
0265 rtems_panic("Unable to initialize AltiVec Support\n");
0266 }
0267
0268 if ( ! (mfmsr() & MSR_VE) ) {
0269 printk(NAM": Warning: BSP should set MSR_VE early; doing it now...\n");
0270 set_MSR_VE();
0271 }
0272 }
0273 #endif