test_cache_disabled.c 3.6 KB

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