test_cache_disabled.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <freertos/FreeRTOS.h>
  10. #include <freertos/task.h>
  11. #include <freertos/semphr.h>
  12. #include <unity.h>
  13. #include <spi_flash_mmap.h>
  14. #include <esp_attr.h>
  15. #include <esp_flash_encrypt.h>
  16. #include "esp_memory_utils.h"
  17. #include "esp_private/cache_utils.h"
  18. static QueueHandle_t result_queue;
  19. static IRAM_ATTR void cache_test_task(void *arg)
  20. {
  21. bool do_disable = (bool)arg;
  22. bool result;
  23. if(do_disable) {
  24. spi_flash_disable_interrupts_caches_and_other_cpu();
  25. }
  26. result = spi_flash_cache_enabled();
  27. if (do_disable) {
  28. spi_flash_enable_interrupts_caches_and_other_cpu();
  29. }
  30. TEST_ASSERT( xQueueSendToBack(result_queue, &result, 0) );
  31. vTaskDelete(NULL);
  32. }
  33. TEST_CASE("spi_flash_cache_enabled() works on both CPUs", "[spi_flash][esp_flash]")
  34. {
  35. result_queue = xQueueCreate(1, sizeof(bool));
  36. for(int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
  37. for(int disable = 0; disable <= 1; disable++) {
  38. bool do_disable = disable;
  39. bool result;
  40. printf("Testing cpu %d disabled %d\n", cpu, do_disable);
  41. xTaskCreatePinnedToCore(cache_test_task, "cache_check_task",
  42. 2048, (void *)do_disable, configMAX_PRIORITIES-1, NULL, cpu);
  43. TEST_ASSERT( xQueueReceive(result_queue, &result, 2) );
  44. TEST_ASSERT_EQUAL(!do_disable, result);
  45. }
  46. }
  47. vQueueDelete(result_queue);
  48. }
  49. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  50. // This needs to sufficiently large array, otherwise it may end up in
  51. // DRAM (e.g. size <= 8 bytes && ARCH == RISCV)
  52. static const uint32_t s_in_rodata[8] = { 0x12345678, 0xfedcba98 };
  53. static void IRAM_ATTR cache_access_test_func(void* arg)
  54. {
  55. /* Assert that the array s_in_rodata is in DROM. If not, this test is
  56. * invalid as disabling the cache wouldn't have any effect. */
  57. TEST_ASSERT(esp_ptr_in_drom(s_in_rodata));
  58. spi_flash_disable_interrupts_caches_and_other_cpu();
  59. volatile uint32_t* src = (volatile uint32_t*) s_in_rodata;
  60. uint32_t v1 = src[0];
  61. uint32_t v2 = src[1];
  62. bool cache_enabled = spi_flash_cache_enabled();
  63. spi_flash_enable_interrupts_caches_and_other_cpu();
  64. printf("%d %x %x\n", cache_enabled, v1, v2);
  65. vTaskDelete(NULL);
  66. }
  67. #if CONFIG_IDF_TARGET_ESP32
  68. #define CACHE_ERROR_REASON "Cache disabled,SW_RESET"
  69. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32H2
  70. #define CACHE_ERROR_REASON "Cache error,RTC_SW_CPU_RST"
  71. #elif CONFIG_IDF_TARGET_ESP32S3
  72. #define CACHE_ERROR_REASON "Cache disabled,RTC_SW_CPU_RST"
  73. #endif
  74. // These tests works properly if they resets the chip with the
  75. // "Cache disabled but cached memory region accessed" reason and the correct CPU is logged.
  76. TEST_CASE("invalid access to cache raises panic (PRO CPU)", "[spi_flash][reset="CACHE_ERROR_REASON"]")
  77. {
  78. xTaskCreatePinnedToCore(&cache_access_test_func, "ia", 2048, NULL, 5, NULL, 0);
  79. vTaskDelay(1000/portTICK_PERIOD_MS);
  80. }
  81. #ifndef CONFIG_FREERTOS_UNICORE
  82. TEST_CASE("invalid access to cache raises panic (APP CPU)", "[spi_flash][reset="CACHE_ERROR_REASON"]")
  83. {
  84. xTaskCreatePinnedToCore(&cache_access_test_func, "ia", 2048, NULL, 5, NULL, 1);
  85. vTaskDelay(1000/portTICK_PERIOD_MS);
  86. }
  87. #endif // !CONFIG_FREERTOS_UNICORE
  88. #endif // !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)