| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup sparc64
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_sparc64_ASM_H_
|
|---|
| 36 | #define KERN_sparc64_ASM_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <arch/arch.h>
|
|---|
| 39 | #include <typedefs.h>
|
|---|
| 40 | #include <align.h>
|
|---|
| 41 | #include <arch/register.h>
|
|---|
| 42 | #include <config.h>
|
|---|
| 43 | #include <arch/stack.h>
|
|---|
| 44 | #include <arch/barrier.h>
|
|---|
| 45 |
|
|---|
| 46 | static inline void pio_write_8(ioport8_t *port, uint8_t v)
|
|---|
| 47 | {
|
|---|
| 48 | *port = v;
|
|---|
| 49 | memory_barrier();
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | static inline void pio_write_16(ioport16_t *port, uint16_t v)
|
|---|
| 53 | {
|
|---|
| 54 | *port = v;
|
|---|
| 55 | memory_barrier();
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | static inline void pio_write_32(ioport32_t *port, uint32_t v)
|
|---|
| 59 | {
|
|---|
| 60 | *port = v;
|
|---|
| 61 | memory_barrier();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | static inline uint8_t pio_read_8(ioport8_t *port)
|
|---|
| 65 | {
|
|---|
| 66 | uint8_t rv;
|
|---|
| 67 |
|
|---|
| 68 | rv = *port;
|
|---|
| 69 | memory_barrier();
|
|---|
| 70 |
|
|---|
| 71 | return rv;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | static inline uint16_t pio_read_16(ioport16_t *port)
|
|---|
| 75 | {
|
|---|
| 76 | uint16_t rv;
|
|---|
| 77 |
|
|---|
| 78 | rv = *port;
|
|---|
| 79 | memory_barrier();
|
|---|
| 80 |
|
|---|
| 81 | return rv;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | static inline uint32_t pio_read_32(ioport32_t *port)
|
|---|
| 85 | {
|
|---|
| 86 | uint32_t rv;
|
|---|
| 87 |
|
|---|
| 88 | rv = *port;
|
|---|
| 89 | memory_barrier();
|
|---|
| 90 |
|
|---|
| 91 | return rv;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /** Read Processor State register.
|
|---|
| 95 | *
|
|---|
| 96 | * @return Value of PSTATE register.
|
|---|
| 97 | */
|
|---|
| 98 | static inline uint64_t pstate_read(void)
|
|---|
| 99 | {
|
|---|
| 100 | uint64_t v;
|
|---|
| 101 |
|
|---|
| 102 | asm volatile ("rdpr %%pstate, %0\n" : "=r" (v));
|
|---|
| 103 |
|
|---|
| 104 | return v;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /** Write Processor State register.
|
|---|
| 108 | *
|
|---|
| 109 | * @param v New value of PSTATE register.
|
|---|
| 110 | */
|
|---|
| 111 | static inline void pstate_write(uint64_t v)
|
|---|
| 112 | {
|
|---|
| 113 | asm volatile ("wrpr %0, %1, %%pstate\n" : : "r" (v), "i" (0));
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** Read TICK_compare Register.
|
|---|
| 117 | *
|
|---|
| 118 | * @return Value of TICK_comapre register.
|
|---|
| 119 | */
|
|---|
| 120 | static inline uint64_t tick_compare_read(void)
|
|---|
| 121 | {
|
|---|
| 122 | uint64_t v;
|
|---|
| 123 |
|
|---|
| 124 | asm volatile ("rd %%tick_cmpr, %0\n" : "=r" (v));
|
|---|
| 125 |
|
|---|
| 126 | return v;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /** Write TICK_compare Register.
|
|---|
| 130 | *
|
|---|
| 131 | * @param v New value of TICK_comapre register.
|
|---|
| 132 | */
|
|---|
| 133 | static inline void tick_compare_write(uint64_t v)
|
|---|
| 134 | {
|
|---|
| 135 | asm volatile ("wr %0, %1, %%tick_cmpr\n" : : "r" (v), "i" (0));
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /** Read STICK_compare Register.
|
|---|
| 139 | *
|
|---|
| 140 | * @return Value of STICK_compare register.
|
|---|
| 141 | */
|
|---|
| 142 | static inline uint64_t stick_compare_read(void)
|
|---|
| 143 | {
|
|---|
| 144 | uint64_t v;
|
|---|
| 145 |
|
|---|
| 146 | asm volatile ("rd %%asr25, %0\n" : "=r" (v));
|
|---|
| 147 |
|
|---|
| 148 | return v;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** Write STICK_compare Register.
|
|---|
| 152 | *
|
|---|
| 153 | * @param v New value of STICK_comapre register.
|
|---|
| 154 | */
|
|---|
| 155 | static inline void stick_compare_write(uint64_t v)
|
|---|
| 156 | {
|
|---|
| 157 | asm volatile ("wr %0, %1, %%asr25\n" : : "r" (v), "i" (0));
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** Read TICK Register.
|
|---|
| 161 | *
|
|---|
| 162 | * @return Value of TICK register.
|
|---|
| 163 | */
|
|---|
| 164 | static inline uint64_t tick_read(void)
|
|---|
| 165 | {
|
|---|
| 166 | uint64_t v;
|
|---|
| 167 |
|
|---|
| 168 | asm volatile ("rdpr %%tick, %0\n" : "=r" (v));
|
|---|
| 169 |
|
|---|
| 170 | return v;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /** Write TICK Register.
|
|---|
| 174 | *
|
|---|
| 175 | * @param v New value of TICK register.
|
|---|
| 176 | */
|
|---|
| 177 | static inline void tick_write(uint64_t v)
|
|---|
| 178 | {
|
|---|
| 179 | asm volatile ("wrpr %0, %1, %%tick\n" : : "r" (v), "i" (0));
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /** Read FPRS Register.
|
|---|
| 183 | *
|
|---|
| 184 | * @return Value of FPRS register.
|
|---|
| 185 | */
|
|---|
| 186 | static inline uint64_t fprs_read(void)
|
|---|
| 187 | {
|
|---|
| 188 | uint64_t v;
|
|---|
| 189 |
|
|---|
| 190 | asm volatile ("rd %%fprs, %0\n" : "=r" (v));
|
|---|
| 191 |
|
|---|
| 192 | return v;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /** Write FPRS Register.
|
|---|
| 196 | *
|
|---|
| 197 | * @param v New value of FPRS register.
|
|---|
| 198 | */
|
|---|
| 199 | static inline void fprs_write(uint64_t v)
|
|---|
| 200 | {
|
|---|
| 201 | asm volatile ("wr %0, %1, %%fprs\n" : : "r" (v), "i" (0));
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /** Read SOFTINT Register.
|
|---|
| 205 | *
|
|---|
| 206 | * @return Value of SOFTINT register.
|
|---|
| 207 | */
|
|---|
| 208 | static inline uint64_t softint_read(void)
|
|---|
| 209 | {
|
|---|
| 210 | uint64_t v;
|
|---|
| 211 |
|
|---|
| 212 | asm volatile ("rd %%softint, %0\n" : "=r" (v));
|
|---|
| 213 |
|
|---|
| 214 | return v;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /** Write SOFTINT Register.
|
|---|
| 218 | *
|
|---|
| 219 | * @param v New value of SOFTINT register.
|
|---|
| 220 | */
|
|---|
| 221 | static inline void softint_write(uint64_t v)
|
|---|
| 222 | {
|
|---|
| 223 | asm volatile ("wr %0, %1, %%softint\n" : : "r" (v), "i" (0));
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /** Write CLEAR_SOFTINT Register.
|
|---|
| 227 | *
|
|---|
| 228 | * Bits set in CLEAR_SOFTINT register will be cleared in SOFTINT register.
|
|---|
| 229 | *
|
|---|
| 230 | * @param v New value of CLEAR_SOFTINT register.
|
|---|
| 231 | */
|
|---|
| 232 | static inline void clear_softint_write(uint64_t v)
|
|---|
| 233 | {
|
|---|
| 234 | asm volatile ("wr %0, %1, %%clear_softint\n" : : "r" (v), "i" (0));
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | /** Write SET_SOFTINT Register.
|
|---|
| 238 | *
|
|---|
| 239 | * Bits set in SET_SOFTINT register will be set in SOFTINT register.
|
|---|
| 240 | *
|
|---|
| 241 | * @param v New value of SET_SOFTINT register.
|
|---|
| 242 | */
|
|---|
| 243 | static inline void set_softint_write(uint64_t v)
|
|---|
| 244 | {
|
|---|
| 245 | asm volatile ("wr %0, %1, %%set_softint\n" : : "r" (v), "i" (0));
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /** Enable interrupts.
|
|---|
| 249 | *
|
|---|
| 250 | * Enable interrupts and return previous
|
|---|
| 251 | * value of IPL.
|
|---|
| 252 | *
|
|---|
| 253 | * @return Old interrupt priority level.
|
|---|
| 254 | */
|
|---|
| 255 | static inline ipl_t interrupts_enable(void) {
|
|---|
| 256 | pstate_reg_t pstate;
|
|---|
| 257 | uint64_t value;
|
|---|
| 258 |
|
|---|
| 259 | value = pstate_read();
|
|---|
| 260 | pstate.value = value;
|
|---|
| 261 | pstate.ie = true;
|
|---|
| 262 | pstate_write(pstate.value);
|
|---|
| 263 |
|
|---|
| 264 | return (ipl_t) value;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /** Disable interrupts.
|
|---|
| 268 | *
|
|---|
| 269 | * Disable interrupts and return previous
|
|---|
| 270 | * value of IPL.
|
|---|
| 271 | *
|
|---|
| 272 | * @return Old interrupt priority level.
|
|---|
| 273 | */
|
|---|
| 274 | static inline ipl_t interrupts_disable(void) {
|
|---|
| 275 | pstate_reg_t pstate;
|
|---|
| 276 | uint64_t value;
|
|---|
| 277 |
|
|---|
| 278 | value = pstate_read();
|
|---|
| 279 | pstate.value = value;
|
|---|
| 280 | pstate.ie = false;
|
|---|
| 281 | pstate_write(pstate.value);
|
|---|
| 282 |
|
|---|
| 283 | return (ipl_t) value;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /** Restore interrupt priority level.
|
|---|
| 287 | *
|
|---|
| 288 | * Restore IPL.
|
|---|
| 289 | *
|
|---|
| 290 | * @param ipl Saved interrupt priority level.
|
|---|
| 291 | */
|
|---|
| 292 | static inline void interrupts_restore(ipl_t ipl) {
|
|---|
| 293 | pstate_reg_t pstate;
|
|---|
| 294 |
|
|---|
| 295 | pstate.value = pstate_read();
|
|---|
| 296 | pstate.ie = ((pstate_reg_t) ipl).ie;
|
|---|
| 297 | pstate_write(pstate.value);
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | /** Return interrupt priority level.
|
|---|
| 301 | *
|
|---|
| 302 | * Return IPL.
|
|---|
| 303 | *
|
|---|
| 304 | * @return Current interrupt priority level.
|
|---|
| 305 | */
|
|---|
| 306 | static inline ipl_t interrupts_read(void) {
|
|---|
| 307 | return (ipl_t) pstate_read();
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | /** Check interrupts state.
|
|---|
| 311 | *
|
|---|
| 312 | * @return True if interrupts are disabled.
|
|---|
| 313 | *
|
|---|
| 314 | */
|
|---|
| 315 | static inline bool interrupts_disabled(void)
|
|---|
| 316 | {
|
|---|
| 317 | pstate_reg_t pstate;
|
|---|
| 318 |
|
|---|
| 319 | pstate.value = pstate_read();
|
|---|
| 320 | return !pstate.ie;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /** Return base address of current stack.
|
|---|
| 324 | *
|
|---|
| 325 | * Return the base address of the current stack.
|
|---|
| 326 | * The stack is assumed to be STACK_SIZE bytes long.
|
|---|
| 327 | * The stack must start on page boundary.
|
|---|
| 328 | */
|
|---|
| 329 | static inline uintptr_t get_stack_base(void)
|
|---|
| 330 | {
|
|---|
| 331 | uintptr_t unbiased_sp;
|
|---|
| 332 |
|
|---|
| 333 | asm volatile ("add %%sp, %1, %0\n" : "=r" (unbiased_sp) : "i" (STACK_BIAS));
|
|---|
| 334 |
|
|---|
| 335 | return ALIGN_DOWN(unbiased_sp, STACK_SIZE);
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | /** Read Version Register.
|
|---|
| 339 | *
|
|---|
| 340 | * @return Value of VER register.
|
|---|
| 341 | */
|
|---|
| 342 | static inline uint64_t ver_read(void)
|
|---|
| 343 | {
|
|---|
| 344 | uint64_t v;
|
|---|
| 345 |
|
|---|
| 346 | asm volatile ("rdpr %%ver, %0\n" : "=r" (v));
|
|---|
| 347 |
|
|---|
| 348 | return v;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | /** Read Trap Program Counter register.
|
|---|
| 352 | *
|
|---|
| 353 | * @return Current value in TPC.
|
|---|
| 354 | */
|
|---|
| 355 | static inline uint64_t tpc_read(void)
|
|---|
| 356 | {
|
|---|
| 357 | uint64_t v;
|
|---|
| 358 |
|
|---|
| 359 | asm volatile ("rdpr %%tpc, %0\n" : "=r" (v));
|
|---|
| 360 |
|
|---|
| 361 | return v;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | /** Read Trap Level register.
|
|---|
| 365 | *
|
|---|
| 366 | * @return Current value in TL.
|
|---|
| 367 | */
|
|---|
| 368 | static inline uint64_t tl_read(void)
|
|---|
| 369 | {
|
|---|
| 370 | uint64_t v;
|
|---|
| 371 |
|
|---|
| 372 | asm volatile ("rdpr %%tl, %0\n" : "=r" (v));
|
|---|
| 373 |
|
|---|
| 374 | return v;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /** Read Trap Base Address register.
|
|---|
| 378 | *
|
|---|
| 379 | * @return Current value in TBA.
|
|---|
| 380 | */
|
|---|
| 381 | static inline uint64_t tba_read(void)
|
|---|
| 382 | {
|
|---|
| 383 | uint64_t v;
|
|---|
| 384 |
|
|---|
| 385 | asm volatile ("rdpr %%tba, %0\n" : "=r" (v));
|
|---|
| 386 |
|
|---|
| 387 | return v;
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | /** Write Trap Base Address register.
|
|---|
| 391 | *
|
|---|
| 392 | * @param v New value of TBA.
|
|---|
| 393 | */
|
|---|
| 394 | static inline void tba_write(uint64_t v)
|
|---|
| 395 | {
|
|---|
| 396 | asm volatile ("wrpr %0, %1, %%tba\n" : : "r" (v), "i" (0));
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | /** Load uint64_t from alternate space.
|
|---|
| 400 | *
|
|---|
| 401 | * @param asi ASI determining the alternate space.
|
|---|
| 402 | * @param va Virtual address within the ASI.
|
|---|
| 403 | *
|
|---|
| 404 | * @return Value read from the virtual address in the specified address space.
|
|---|
| 405 | */
|
|---|
| 406 | static inline uint64_t asi_u64_read(asi_t asi, uintptr_t va)
|
|---|
| 407 | {
|
|---|
| 408 | uint64_t v;
|
|---|
| 409 |
|
|---|
| 410 | asm volatile ("ldxa [%1] %2, %0\n" : "=r" (v) : "r" (va), "i" ((unsigned) asi));
|
|---|
| 411 |
|
|---|
| 412 | return v;
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | /** Store uint64_t to alternate space.
|
|---|
| 416 | *
|
|---|
| 417 | * @param asi ASI determining the alternate space.
|
|---|
| 418 | * @param va Virtual address within the ASI.
|
|---|
| 419 | * @param v Value to be written.
|
|---|
| 420 | */
|
|---|
| 421 | static inline void asi_u64_write(asi_t asi, uintptr_t va, uint64_t v)
|
|---|
| 422 | {
|
|---|
| 423 | asm volatile ("stxa %0, [%1] %2\n" : : "r" (v), "r" (va), "i" ((unsigned) asi) : "memory");
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | /** Flush all valid register windows to memory. */
|
|---|
| 427 | static inline void flushw(void)
|
|---|
| 428 | {
|
|---|
| 429 | asm volatile ("flushw\n");
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | /** Switch to nucleus by setting TL to 1. */
|
|---|
| 433 | static inline void nucleus_enter(void)
|
|---|
| 434 | {
|
|---|
| 435 | asm volatile ("wrpr %g0, 1, %tl\n");
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | /** Switch from nucleus by setting TL to 0. */
|
|---|
| 439 | static inline void nucleus_leave(void)
|
|---|
| 440 | {
|
|---|
| 441 | asm volatile ("wrpr %g0, %g0, %tl\n");
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | extern void cpu_halt(void) __attribute__((noreturn));
|
|---|
| 445 | extern void cpu_sleep(void);
|
|---|
| 446 | extern void asm_delay_loop(const uint32_t usec);
|
|---|
| 447 |
|
|---|
| 448 | extern uint64_t read_from_ag_g6(void);
|
|---|
| 449 | extern uint64_t read_from_ag_g7(void);
|
|---|
| 450 | extern void write_to_ag_g6(uint64_t val);
|
|---|
| 451 | extern void write_to_ag_g7(uint64_t val);
|
|---|
| 452 | extern void write_to_ig_g6(uint64_t val);
|
|---|
| 453 |
|
|---|
| 454 | extern void switch_to_userspace(uint64_t pc, uint64_t sp, uint64_t uarg);
|
|---|
| 455 |
|
|---|
| 456 | #endif
|
|---|
| 457 |
|
|---|
| 458 | /** @}
|
|---|
| 459 | */
|
|---|