cpu.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2010-2016 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. #ifndef _SOC_CPU_H
  14. #define _SOC_CPU_H
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <stddef.h>
  18. #include "xtensa/corebits.h"
  19. /* C macros for xtensa special register read/write/exchange */
  20. #define RSR(reg, curval) asm volatile ("rsr %0, " #reg : "=r" (curval));
  21. #define WSR(reg, newval) asm volatile ("wsr %0, " #reg : : "r" (newval));
  22. #define XSR(reg, swapval) asm volatile ("xsr %0, " #reg : "+r" (swapval));
  23. /** @brief Read current stack pointer address
  24. *
  25. */
  26. static inline void *get_sp()
  27. {
  28. void *sp;
  29. asm volatile ("mov %0, sp;" : "=r" (sp));
  30. return sp;
  31. }
  32. /* Return true if the CPU is in an interrupt context
  33. (PS.UM == 0)
  34. */
  35. static inline bool cpu_in_interrupt_context(void)
  36. {
  37. uint32_t ps;
  38. RSR(PS, ps);
  39. return (ps & PS_UM) == 0;
  40. }
  41. /* Functions to set page attributes for Region Protection option in the CPU.
  42. * See Xtensa ISA Reference manual for explanation of arguments (section 4.6.3.2).
  43. */
  44. static inline void cpu_write_dtlb(uint32_t vpn, unsigned attr)
  45. {
  46. asm volatile ("wdtlb %1, %0; dsync\n" :: "r" (vpn), "r" (attr));
  47. }
  48. static inline void cpu_write_itlb(unsigned vpn, unsigned attr)
  49. {
  50. asm volatile ("witlb %1, %0; isync\n" :: "r" (vpn), "r" (attr));
  51. }
  52. /**
  53. * @brief Configure memory region protection
  54. *
  55. * Make page 0 access raise an exception.
  56. * Also protect some other unused pages so we can catch weirdness.
  57. * Useful attribute values:
  58. * 0 — cached, RW
  59. * 2 — bypass cache, RWX (default value after CPU reset)
  60. * 15 — no access, raise exception
  61. */
  62. static inline void cpu_configure_region_protection()
  63. {
  64. const uint32_t pages_to_protect[] = {0x00000000, 0x80000000, 0xa0000000, 0xc0000000, 0xe0000000};
  65. for (int i = 0; i < sizeof(pages_to_protect)/sizeof(pages_to_protect[0]); ++i) {
  66. cpu_write_dtlb(pages_to_protect[i], 0xf);
  67. cpu_write_itlb(pages_to_protect[i], 0xf);
  68. }
  69. cpu_write_dtlb(0x20000000, 0);
  70. cpu_write_itlb(0x20000000, 0);
  71. }
  72. /**
  73. * @brief Set CPU frequency to the value defined in menuconfig
  74. *
  75. * Called from cpu_start.c, not intended to be called from other places.
  76. * This is a temporary function which will be replaced once dynamic
  77. * CPU frequency changing is implemented.
  78. */
  79. void esp_set_cpu_freq(void);
  80. /**
  81. * @brief Stall CPU using RTC controller
  82. * @param cpu_id ID of the CPU to stall (0 = PRO, 1 = APP)
  83. */
  84. void esp_cpu_stall(int cpu_id);
  85. /**
  86. * @brief Un-stall CPU using RTC controller
  87. * @param cpu_id ID of the CPU to un-stall (0 = PRO, 1 = APP)
  88. */
  89. void esp_cpu_unstall(int cpu_id);
  90. #endif