Hardware Abstraction Layer (HAL)
DCACHE Management

Overview

For devices that contain a data cache (DCACHE), the HAL supports cache management in select drivers that can potentially be affected by it. The impacted drivers are:

  • DMA
  • I2S
  • IPC
  • PDM_PCM
  • SDIO
  • TDM
  • UART

The implementation of the cache management is based on the following cache policy and configuration:

  • Write-back with read and write allocate: WB-RWA
  • Cache line size of 32 bytes
  • All memory in application is assumed to be cacheable

Operation

When working with DCACHE, cache coherency issues can be encountered when multiple bus masters access the same region of memory. There are two main scenarios to consider in the case where one of these is the CPU with DCACHE enabled, and the other is a peripheral such as a DMA. Suppose that both access the same memory region located in SRAM. Cache coherency issues can occur depending on the ordering in which the bus masters access that memory and which operations are performed on it.

  1. CPU writes to SRAM, DMA reads the content: When the CPU writes to SRAM, only the cache is updated and not the actual memory. When the DMA reads, it reads the old value in the actual memory.
  2. DMA writes to SRAM, CPU reads the content: When the DMA writes to SRAM, it is updated in the actual memory. When the CPU reads from that memory, it reads the old value in the cache.

To avoid running into these issues, the HAL (and the application) must perform the following operations.

  1. CPU writes to SRAM, DMA reads the content: Clean the cache (SCB_CleanDCache_by_Addr) after CPU write.
  2. DMA writes to SRAM, CPU reads the content: Invalidate the cache (SCB_InvalidateDCache_by_Addr) before CPU read.

In a scenario where multiple bus masters have DCACHE enabled, such as in multi-core (CPU) devices, the application must ensure that memory access is co-ordinated between the cores, such as by using an IPC (Inter-processor commnication) channel.

Alignment and Padding

Cache clean and invalidate operations are performed at a granularity of the cache line size (i.e. 32 bytes). Therefore, all affected memory regions must also be aligned to 32 bytes and suitably padded to be a multiple of 32 bytes. Without proper alignment and padding, adjacent data will become corrupted during these operations.

The HAL is responsible for managing the memory internally within the drivers. The affected memory regions are aligned to the cache line and padded. However, the drivers cannot manage all the memory, such as data buffers passed in by the application. It is the responsibility of the application to properly align and pad the data. Refer to the individual driver API documentation on where these operations should be applied.

References