mpu_acts.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief actions mpu
  9. */
  10. #define LOG_LEVEL 3
  11. #include <logging/log.h>
  12. LOG_MODULE_REGISTER(mpu_acts);
  13. #include <kernel.h>
  14. #include <init.h>
  15. #include <device.h>
  16. #include <linker/linker-defs.h>
  17. #include <irq.h>
  18. #include <drivers/mpu_acts.h>
  19. #include <soc.h>
  20. #include <string.h>
  21. #include <board_cfg.h>
  22. static void _mpu_protect_disable(void)
  23. {
  24. sys_write32(0, MPUIE);
  25. }
  26. static void _mpu_clear_all_pending(void)
  27. {
  28. sys_write32(0xffffffff, MPUIP);
  29. }
  30. static s8_t mpu_enable_count[4];
  31. void mpu_set_address(u32_t start, u32_t end, u32_t flag, u32_t index)
  32. {
  33. mpu_base_register_t *base_register = (mpu_base_register_t *)MPUCTL0;
  34. if(index >= 4){
  35. LOG_ERR("index =%d >=4\n", index);
  36. return;
  37. }
  38. base_register += index;
  39. base_register->MPUBASE = start;
  40. base_register->MPUEND = end;
  41. base_register->MPUCTL = ((flag) << 1);
  42. sys_write32((sys_read32(MPUIE) & ~(0x1f << 8*index)) | (flag << 8*index), MPUIE);
  43. }
  44. static void _mpu_protect_init(void)
  45. {
  46. #ifdef CONFIG_MPU_MONITOR_CACHECODE_WRITE
  47. /* protect rom section */
  48. mpu_set_address((unsigned int)_image_rom_start, (unsigned int)_image_rom_end - 1,
  49. (MPU_CPU_WRITE), CONFIG_MPU_MONITOR_CACHECODE_WRITE_INDEX);
  50. mpu_enable_region(CONFIG_MPU_MONITOR_CACHECODE_WRITE_INDEX);
  51. #endif
  52. #ifdef CONFIG_MPU_MONITOR_FLASH_AREA_WRITE
  53. /* protect flash cache all address section */
  54. mpu_set_address(CONFIG_MPU_MONITOR_FLASH_AREA_BASE, CONFIG_MPU_MONITOR_FLASH_AREA_END - 1,
  55. (MPU_CPU_WRITE), CONFIG_MPU_MONITOR_CACHECODE_WRITE_INDEX);
  56. mpu_enable_region(CONFIG_MPU_MONITOR_CACHECODE_WRITE_INDEX);
  57. #endif
  58. #ifdef CONFIG_MPU_MONITOR_RAMFUNC_WRITE
  59. /* protect ram function section*/
  60. mpu_set_address((unsigned int)__ramfunc_start, (unsigned int)__ramfunc_end - 1,
  61. (MPU_CPU_WRITE | MPU_DMA_WRITE), CONFIG_MPU_MONITOR_RAMFUNC_WRITE_INDEX);
  62. mpu_enable_region(CONFIG_MPU_MONITOR_RAMFUNC_WRITE_INDEX);
  63. #endif
  64. #ifdef CONFIG_ACTIONS_ARM_MPU
  65. extern void act_mpu_set(uint32_t chan, uint32_t mem_base, uint32_t size, uint32_t attr);
  66. act_mpu_set(6, 0x0, 0x10000, 2); //exe, ro
  67. #else
  68. #ifdef CONFIG_MPU_MONITOR_ROMFUNC_WRITE
  69. /* protect rom addr section */
  70. mpu_set_address(0x0, 0x10000 - 1,
  71. (MPU_CPU_WRITE), CONFIG_MPU_MONITOR_ROMFUNC_WRITE_INDEX);
  72. mpu_enable_region(CONFIG_MPU_MONITOR_ROMFUNC_WRITE_INDEX);
  73. #endif
  74. #endif
  75. _mpu_clear_all_pending();
  76. }
  77. void dma_dump_info(void);
  78. static int mpu_analyse(void)
  79. {
  80. unsigned int mpux_pending,pending, addr;
  81. int i; //, dma, len;
  82. mpu_base_register_t *base_register;
  83. //struct dma_regs *dma_base_regs;
  84. pending = sys_read32(MPUIP);
  85. if(pending == 0)
  86. return 0;
  87. LOG_ERR("mpu pending:0x%x, IE=0x%x\n", pending, sys_read32(MPUIE));
  88. _mpu_protect_disable();
  89. base_register = (mpu_base_register_t *)MPUCTL0;
  90. for (i = 0; i < CONFIG_MPU_ACTS_MAX_INDEX; i++) {
  91. mpux_pending = (sys_read32(MPUIP) >> (i*8)) & 0x3f;
  92. if (!mpux_pending)
  93. continue;
  94. addr = base_register[i].MPUERRADDR;
  95. if (mpux_pending & MPU_CPU_WRITE) {
  96. LOG_ERR("Warning:%d invalid cpu wirte addr=0x%x!\n", i, addr);
  97. }else if (mpux_pending & MPU_DMA_WRITE) {
  98. LOG_ERR("Warning:%d invalid dma wirte addr=0x%x!\n", i, addr);
  99. #if defined(CONFIG_DMA_DBG_DUMP)
  100. dma_dump_info();
  101. #endif
  102. } else /*if(mpux_pending & MPU_DMA_WRITE)*/ {
  103. LOG_ERR("Warning:invalid access %d pd=0x%x addr=0x%x!\n", i, mpux_pending, addr);
  104. }
  105. }
  106. k_panic();
  107. return 0;
  108. }
  109. void mpu_protect_clear_pending(int mpu_no)
  110. {
  111. if (mpu_no < 4) {
  112. sys_write32(0x1f << (8 * mpu_no), MPUIP);
  113. }
  114. }
  115. void mpu_enable_region(unsigned int index)
  116. {
  117. mpu_base_register_t *base_register = (mpu_base_register_t *)MPUCTL0;
  118. u32_t flags;
  119. flags = irq_lock();
  120. if (mpu_enable_count[index] == 0) {
  121. base_register += index;
  122. base_register->MPUCTL |= (0x01);
  123. }
  124. mpu_enable_count[index]++;
  125. irq_unlock(flags);
  126. }
  127. void mpu_disable_region(unsigned int index)
  128. {
  129. mpu_base_register_t *base_register = (mpu_base_register_t *)MPUCTL0;
  130. u32_t flags;
  131. flags = irq_lock();
  132. mpu_enable_count[index]--;
  133. if (mpu_enable_count[index] == 0) {
  134. base_register += index;
  135. base_register->MPUCTL &= (~(0x01));
  136. }
  137. irq_unlock(flags);
  138. }
  139. int mpu_region_is_enable(unsigned int index)
  140. {
  141. mpu_base_register_t *base_register = (mpu_base_register_t *)MPUCTL0;
  142. base_register += index;
  143. return ((base_register->MPUCTL & 0x01) == 1);
  144. }
  145. #ifdef CONFIG_MPU_MONITOR_USER_DATA
  146. int mpu_user_data_monitor(unsigned int start_addr, unsigned int end_addr, int mpu_user_no)
  147. {
  148. /* protect text section*/
  149. mpu_set_address((unsigned int)start_addr, (unsigned int)end_addr - 1,
  150. (MPU_CPU_WRITE | MPU_DMA_WRITE), CONFIG_MPU_MONITOR_USER_DATA_INDEX + mpu_user_no);
  151. mpu_enable_region(CONFIG_MPU_MONITOR_USER_DATA_INDEX + mpu_user_no);
  152. return 0;
  153. }
  154. int mpu_user_data_monitor_stop(int mpu_user_no)
  155. {
  156. mpu_disable_region(CONFIG_MPU_MONITOR_USER_DATA_INDEX + mpu_user_no);
  157. return 0;
  158. }
  159. #endif
  160. void mpu_handler(void *arg)
  161. {
  162. mpu_analyse();
  163. _mpu_clear_all_pending();
  164. }
  165. static int mpu_init(const struct device *arg)
  166. {
  167. ARG_UNUSED(arg);
  168. _mpu_protect_init();
  169. _mpu_clear_all_pending();
  170. LOG_INF("mpu init\n");
  171. #ifdef CONFIG_MPU_IRQ_DRIVEN
  172. IRQ_CONNECT(IRQ_ID_MPU, CONFIG_MPU_IRQ_PRI, mpu_handler, 0, 0);
  173. irq_enable(IRQ_ID_MPU);
  174. #endif
  175. #ifdef CONFIG_MPU_EXCEPTION_DRIVEN
  176. sys_write32(sys_read32(MEMORYCTL) | MEMORYCTL_BUSERROR_BIT, MEMORYCTL);
  177. #endif
  178. LOG_INF("mpu init end\n");
  179. return 0;
  180. }
  181. //SYS_INIT(mpu_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
  182. SYS_INIT(mpu_init, APPLICATION, 20);