xt_instr_macros.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #define RSR(reg, at) asm volatile ("rsr %0, %1" : "=r" (at) : "i" (reg))
  15. #define WSR(reg, at) asm volatile ("wsr %0, %1" : : "r" (at), "i" (reg))
  16. #define XSR(reg, at) asm volatile ("xsr %0, %1" : "+r" (at) : "i" (reg))
  17. #define RER(reg, at) asm volatile ("rer %0, %1" : "=r" (at) : "r" (reg))
  18. #define WITLB(at, as) asm volatile ("witlb %0, %1; \n isync \n " : : "r" (at), "r" (as))
  19. #define WDTLB(at, as) asm volatile ("wdtlb %0, %1; \n dsync \n " : : "r" (at), "r" (as))
  20. /* The SET_STACK implements a setting a new stack pointer (sp or a1).
  21. * to do this the need reset PS_WOE, reset WINDOWSTART, update SP, and return PS_WOE.
  22. *
  23. * Note: It has 2 implementations one for using in assembler files (*.S) and one for using in C.
  24. *
  25. * C code prototype for SET_STACK:
  26. * uint32_t ps_reg;
  27. * uint32_t w_base;
  28. * RSR(PS, ps_reg);
  29. * ps_reg &= ~(PS_WOE_MASK | PS_OWB_MASK | PS_CALLINC_MASK);
  30. * WSR(PS, ps_reg);
  31. *
  32. * RSR(WINDOWBASE, w_base);
  33. * WSR(WINDOWSTART, (1 << w_base));
  34. *
  35. * asm volatile ( "movi sp, "XTSTR( (SOC_DRAM_LOW + (SOC_DRAM_HIGH - SOC_DRAM_LOW) / 2) )"");
  36. *
  37. * RSR(PS, ps_reg);
  38. * ps_reg |= (PS_WOE_MASK);
  39. * WSR(PS, ps_reg);
  40. */
  41. #ifdef __ASSEMBLER__
  42. .macro SET_STACK new_sp tmp1 tmp2
  43. rsr.ps \tmp1
  44. movi \tmp2, ~(PS_WOE_MASK | PS_OWB_MASK | PS_CALLINC_MASK)
  45. and \tmp1, \tmp1, \tmp2
  46. wsr.ps \tmp1
  47. rsync
  48. rsr.windowbase \tmp1
  49. ssl \tmp1
  50. movi \tmp1, 1
  51. sll \tmp1, \tmp1
  52. wsr.windowstart \tmp1
  53. rsync
  54. mov sp, \new_sp
  55. rsr.ps \tmp1
  56. movi \tmp2, (PS_WOE)
  57. or \tmp1, \tmp1, \tmp2
  58. wsr.ps \tmp1
  59. rsync
  60. .endm
  61. #else
  62. #define SET_STACK(new_sp) \
  63. do { \
  64. uint32_t tmp1 = 0, tmp2 = 0; \
  65. asm volatile ( \
  66. "rsr.ps %1 \n"\
  67. "movi %2, ~" XTSTR( PS_WOE_MASK | PS_OWB_MASK | PS_CALLINC_MASK ) " \n"\
  68. "and %1, %1, %2 \n"\
  69. "wsr.ps %1 \n"\
  70. "rsync \n"\
  71. " \n"\
  72. "rsr.windowbase %1 \n"\
  73. "ssl %1 \n"\
  74. "movi %1, 1 \n"\
  75. "sll %1, %1 \n"\
  76. "wsr.windowstart %1 \n"\
  77. "rsync \n"\
  78. " \n"\
  79. "mov sp, %0 \n"\
  80. "rsr.ps %1 \n"\
  81. " \n"\
  82. "movi %2, " XTSTR( PS_WOE_MASK ) "\n"\
  83. " \n"\
  84. "or %1, %1, %2 \n"\
  85. "wsr.ps %1 \n"\
  86. "rsync \n"\
  87. : "+r"(new_sp), "+r"(tmp1), "+r"(tmp2)); \
  88. } while (0);
  89. #endif // __ASSEMBLER__