cpu.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /* Functions to set page attributes for Region Protection option in the CPU.
  33. * See Xtensa ISA Reference manual for explanation of arguments (section 4.6.3.2).
  34. */
  35. static inline void cpu_write_dtlb(uint32_t vpn, unsigned attr)
  36. {
  37. asm volatile ("wdtlb %1, %0; dsync\n" :: "r" (vpn), "r" (attr));
  38. }
  39. static inline void cpu_write_itlb(unsigned vpn, unsigned attr)
  40. {
  41. asm volatile ("witlb %1, %0; isync\n" :: "r" (vpn), "r" (attr));
  42. }
  43. /**
  44. * @brief Configure memory region protection
  45. *
  46. * Make page 0 access raise an exception.
  47. * Also protect some other unused pages so we can catch weirdness.
  48. * Useful attribute values:
  49. * 0 — cached, RW
  50. * 2 — bypass cache, RWX (default value after CPU reset)
  51. * 15 — no access, raise exception
  52. */
  53. static inline void cpu_configure_region_protection()
  54. {
  55. const uint32_t pages_to_protect[] = {0x00000000, 0x80000000, 0xa0000000, 0xc0000000, 0xe0000000};
  56. for (int i = 0; i < sizeof(pages_to_protect)/sizeof(pages_to_protect[0]); ++i) {
  57. cpu_write_dtlb(pages_to_protect[i], 0xf);
  58. cpu_write_itlb(pages_to_protect[i], 0xf);
  59. }
  60. cpu_write_dtlb(0x20000000, 0);
  61. cpu_write_itlb(0x20000000, 0);
  62. }
  63. /**
  64. * @brief Set CPU frequency to the value defined in menuconfig
  65. *
  66. * Called from cpu_start.c, not intended to be called from other places.
  67. * This is a temporary function which will be replaced once dynamic
  68. * CPU frequency changing is implemented.
  69. */
  70. void esp_set_cpu_freq(void);
  71. /**
  72. * @brief Stall CPU using RTC controller
  73. * @param cpu_id ID of the CPU to stall (0 = PRO, 1 = APP)
  74. */
  75. void esp_cpu_stall(int cpu_id);
  76. /**
  77. * @brief Un-stall CPU using RTC controller
  78. * @param cpu_id ID of the CPU to un-stall (0 = PRO, 1 = APP)
  79. */
  80. void esp_cpu_unstall(int cpu_id);
  81. /**
  82. * @brief Returns true if a JTAG debugger is attached to CPU
  83. * OCD (on chip debug) port.
  84. *
  85. * @note If "Make exception and panic handlers JTAG/OCD aware"
  86. * is disabled, this function always returns false.
  87. */
  88. bool esp_cpu_in_ocd_debug_mode();
  89. #endif