cpu.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /* Return true if the CPU is in an interrupt context
  24. (PS.UM == 0)
  25. */
  26. static inline bool cpu_in_interrupt_context(void)
  27. {
  28. uint32_t ps;
  29. RSR(PS, ps);
  30. return (ps & PS_UM) == 0;
  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. #endif