mpu_acts.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_MPU_MONITOR_ROMFUNC_WRITE
  65. /* protect rom addr section */
  66. mpu_set_address(0x0, 0x10000 - 1,
  67. (MPU_CPU_WRITE), CONFIG_MPU_MONITOR_ROMFUNC_WRITE_INDEX);
  68. mpu_enable_region(CONFIG_MPU_MONITOR_ROMFUNC_WRITE_INDEX);
  69. #endif
  70. _mpu_clear_all_pending();
  71. }
  72. void dma_dump_info(void);
  73. static int mpu_analyse(void)
  74. {
  75. unsigned int mpux_pending,pending, addr;
  76. int i; //, dma, len;
  77. mpu_base_register_t *base_register;
  78. //struct dma_regs *dma_base_regs;
  79. pending = sys_read32(MPUIP);
  80. if(pending == 0)
  81. return 0;
  82. LOG_ERR("mpu pending:0x%x, IE=0x%x\n", pending, sys_read32(MPUIE));
  83. _mpu_protect_disable();
  84. base_register = (mpu_base_register_t *)MPUCTL0;
  85. for (i = 0; i < CONFIG_MPU_ACTS_MAX_INDEX; i++) {
  86. mpux_pending = (sys_read32(MPUIP) >> (i*8)) & 0x3f;
  87. if (!mpux_pending)
  88. continue;
  89. addr = base_register[i].MPUERRADDR;
  90. if (mpux_pending & MPU_CPU_WRITE) {
  91. LOG_ERR("Warning:%d invalid cpu wirte addr=0x%x!\n", i, addr);
  92. }else if (mpux_pending & MPU_DMA_WRITE) {
  93. LOG_ERR("Warning:%d invalid dma wirte addr=0x%x!\n", i, addr);
  94. #if defined(CONFIG_DMA_DBG_DUMP)
  95. dma_dump_info();
  96. #endif
  97. } else /*if(mpux_pending & MPU_DMA_WRITE)*/ {
  98. LOG_ERR("Warning:invalid access %d pd=0x%x addr=0x%x!\n", i, mpux_pending, addr);
  99. }
  100. }
  101. k_panic();
  102. return 0;
  103. }
  104. void mpu_protect_clear_pending(int mpu_no)
  105. {
  106. if (mpu_no < 4) {
  107. sys_write32(0x1f << (8 * mpu_no), MPUIP);
  108. }
  109. }
  110. void mpu_enable_region(unsigned int index)
  111. {
  112. mpu_base_register_t *base_register = (mpu_base_register_t *)MPUCTL0;
  113. u32_t flags;
  114. flags = irq_lock();
  115. if (mpu_enable_count[index] == 0) {
  116. base_register += index;
  117. base_register->MPUCTL |= (0x01);
  118. }
  119. mpu_enable_count[index]++;
  120. irq_unlock(flags);
  121. }
  122. void mpu_disable_region(unsigned int index)
  123. {
  124. mpu_base_register_t *base_register = (mpu_base_register_t *)MPUCTL0;
  125. u32_t flags;
  126. flags = irq_lock();
  127. mpu_enable_count[index]--;
  128. if (mpu_enable_count[index] == 0) {
  129. base_register += index;
  130. base_register->MPUCTL &= (~(0x01));
  131. }
  132. irq_unlock(flags);
  133. }
  134. int mpu_region_is_enable(unsigned int index)
  135. {
  136. mpu_base_register_t *base_register = (mpu_base_register_t *)MPUCTL0;
  137. base_register += index;
  138. return ((base_register->MPUCTL & 0x01) == 1);
  139. }
  140. #ifdef CONFIG_MPU_MONITOR_USER_DATA
  141. int mpu_user_data_monitor(unsigned int start_addr, unsigned int end_addr, int mpu_user_no)
  142. {
  143. /* protect text section*/
  144. mpu_set_address((unsigned int)start_addr, (unsigned int)end_addr - 1,
  145. (MPU_CPU_WRITE | MPU_DMA_WRITE), CONFIG_MPU_MONITOR_USER_DATA_INDEX + mpu_user_no);
  146. mpu_enable_region(CONFIG_MPU_MONITOR_USER_DATA_INDEX + mpu_user_no);
  147. return 0;
  148. }
  149. int mpu_user_data_monitor_stop(int mpu_user_no)
  150. {
  151. mpu_disable_region(CONFIG_MPU_MONITOR_USER_DATA_INDEX + mpu_user_no);
  152. return 0;
  153. }
  154. #endif
  155. void mpu_handler(void *arg)
  156. {
  157. mpu_analyse();
  158. _mpu_clear_all_pending();
  159. }
  160. static int mpu_init(const struct device *arg)
  161. {
  162. ARG_UNUSED(arg);
  163. _mpu_protect_init();
  164. _mpu_clear_all_pending();
  165. LOG_INF("mpu init\n");
  166. #ifdef CONFIG_MPU_IRQ_DRIVEN
  167. IRQ_CONNECT(IRQ_ID_MPU, CONFIG_MPU_IRQ_PRI, mpu_handler, 0, 0);
  168. irq_enable(IRQ_ID_MPU);
  169. #endif
  170. #ifdef CONFIG_MPU_EXCEPTION_DRIVEN
  171. sys_write32(sys_read32(MEMORYCTL) | MEMORYCTL_BUSERROR_BIT, MEMORYCTL);
  172. #endif
  173. LOG_INF("mpu init end\n");
  174. return 0;
  175. }
  176. //SYS_INIT(mpu_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
  177. SYS_INIT(mpu_init, APPLICATION, 20);