What is the purpose of invalidating D-cache when D-cache is off?

Viewed 661

In the Linux kernel source arch/arm64/kernel/head.S the boot requirements state it is necessary for the bootloader to enter with the D-cache off:

/*
 * Kernel startup entry point.
 * ---------------------------
 *
 * The requirements are:
 *   MMU = off, D-cache = off, I-cache = on or off,
 *   x0 = physical address to the FDT blob.
 ...
 */

Then, in the preserve_boot_args() function, there is a call to invalidate an area of the D-cache after loading arguments into the boot_args array

SYM_CODE_START_LOCAL(preserve_boot_args)
    mov x21, x0             // x21=FDT

    adr_l   x0, boot_args           // record the contents of
    stp x21, x1, [x0]           // x0 .. x3 at kernel entry
    stp x2, x3, [x0, #16]

    dmb sy              // needed before dc ivac with
                        // MMU off

    mov x1, #0x20           // 4 x 8 bytes
    b   __inval_dcache_area     // tail call
SYM_CODE_END(preserve_boot_args)

Why does the D-cache line need to be invalidated if the D-cache is off?

1 Answers

Why does the D-cache line need to be invalidated if the D-cache is off?

These registers stp x21, x1, [x0] \ stp x2, x3, [x0, #16] are written into memory starting from boot_args address. Caches are off, so data would be written right into memory.

D-Cache is not affected by such writes. So D-Cache contains either data which it held just before it was turned off or some junk if chip was powered up.

By invalidating boot_args + 0x20 area, you guarantee that written data would be read later on from memory if D-cache is enabled.

Consider scenario:

  • written area is not invalidated
  • later on caches are enabled
  • D-Caches has line(s) that contains data from [boot_args .. boot_args + 0x20] memory area
  • [boot_args .. boot_args + 0x20] memory area is read back into registers

So in such case there would be cache hit and 'junk' from cache would go into registers, instead of 'right' data fetched from memory.

Related