Implement our own DCache cleaning functions

This commit is contained in:
TuxSH 2016-06-13 21:14:53 +02:00
parent 2943dcb2e9
commit 159c9cb475
7 changed files with 84 additions and 20 deletions

18
source/cache.h Normal file
View File

@ -0,0 +1,18 @@
/*
* cache.h
* by TuxSH
*/
#pragma once
#include "types.h"
/***
The following functions flush the data cache, then waits for all memory transfers to be finished.
The data cache MUST be flushed before doing one of the following:
- rebooting
- powering down
- setting the ARM11 entrypoint to execute a function
***/
void flushEntireDCache(void);
void flushDCacheRange(void *startAddress, u32 size);

51
source/cache.s Normal file
View File

@ -0,0 +1,51 @@
@
@ cache.s
@ by TuxSH
@
@ This is part of Luma3DS, see LICENSE.txt for details
@
.text
.arm
.align 4
.global flushEntireDCache
.type flushEntireDCache, %function
flushEntireDCache:
@ Adpated from http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0155a/ch03s03s05.html ,
@ and https://github.com/gemarcano/libctr9_io/blob/master/src/ctr_system_ARM.c#L39 as well
@ Note: ARM's example is actually for a 8KB DCache (which is what the 3DS has)
mov r1, #0 @ segment counter
outer_loop:
mov r0, #0 @ line counter
inner_loop:
orr r2, r1, r0 @ generate segment and line address
mcr p15, 0, r2, c7, c14, 2 @ clean and flush the line
add r0, #0x20 @ increment to next line
cmp r0, #0x400
bne inner_loop
add r1, #0x40000000
cmp r1, #0
bne outer_loop
mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
bx lr
.global flushDCacheRange
.type flushDCacheRange, %function
flushDCacheRange:
add r1, r0, r1 @ end address
bic r0, #0x1f @ align source address to cache line size (32 bytes)
flush_range_loop:
mcr p15, 0, r0, c7, c14, 1 @ clean and flush the line corresponding to the address r0 is holding
add r0, #0x20
cmp r0, r1
bls flush_range_loop
mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
bx lr

View File

@ -8,6 +8,7 @@
#include "fs.h" #include "fs.h"
#include "patches.h" #include "patches.h"
#include "memory.h" #include "memory.h"
#include "cache.h"
#include "emunand.h" #include "emunand.h"
#include "crypto.h" #include "crypto.h"
#include "draw.h" #include "draw.h"
@ -378,7 +379,7 @@ static inline void launchFirm(FirmwareType firmType, u32 isFirmlaunch)
arm11 = (u32 *)0x1FFFFFF8; arm11 = (u32 *)0x1FFFFFF8;
} }
cleanInvalidateDCacheAndDMB(); //Ensure that all memory transfers have completed and that the data cache has been flushed flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
//Set ARM11 kernel entrypoint //Set ARM11 kernel entrypoint
*arm11 = (u32)firm->arm11Entry; *arm11 = (u32)firm->arm11Entry;

View File

@ -61,8 +61,3 @@ u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize)
return NULL; return NULL;
} }
void cleanInvalidateDCacheAndDMB(void)
{
((void (*)())0xFFFF0830)(); //Why write our own code when it's well implemented in the unprotected bootROM?
}

View File

@ -12,12 +12,3 @@ void memcpy(void *dest, const void *src, u32 size);
void memset32(void *dest, u32 filler, u32 size); void memset32(void *dest, u32 filler, u32 size);
int memcmp(const void *buf1, const void *buf2, u32 size); int memcmp(const void *buf1, const void *buf2, u32 size);
u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize); u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize);
/***
Cleans and invalidates the data cache, then waits for all memory transfers to be finished.
This function MUST be called before doing the following:
- rebooting
- powering down
- setting the ARM11 entrypoint to execute a function
***/
void cleanInvalidateDCacheAndDMB(void);

View File

@ -8,6 +8,7 @@
#include "screen.h" #include "screen.h"
#include "config.h" #include "config.h"
#include "memory.h" #include "memory.h"
#include "cache.h"
#include "draw.h" #include "draw.h"
#include "i2c.h" #include "i2c.h"
@ -30,9 +31,11 @@ void __attribute__((naked)) arm11Stub(void)
static inline void invokeArm11Function(void (*func)()) static inline void invokeArm11Function(void (*func)())
{ {
static u32 hasCopiedStub = 0; static u32 hasCopiedStub = 0;
if(!hasCopiedStub++) memcpy((void *)ARM11_STUB_ADDRESS, arm11Stub, 0x40); if(!hasCopiedStub++)
{
cleanInvalidateDCacheAndDMB(); memcpy((void *)ARM11_STUB_ADDRESS, arm11Stub, 0x40);
flushDCacheRange((void *)ARM11_STUB_ADDRESS, 0x40);
}
*arm11Entry = (u32)func; *arm11Entry = (u32)func;
while(*arm11Entry); while(*arm11Entry);
@ -77,6 +80,7 @@ void updateBrightness(u32 brightnessLevel)
WAIT_FOR_ARM9(); WAIT_FOR_ARM9();
} }
flushDCacheRange(&brightnessValue, 4);
invokeArm11Function(ARM11); invokeArm11Function(ARM11);
} }
@ -116,6 +120,7 @@ void clearScreens(void)
WAIT_FOR_ARM9(); WAIT_FOR_ARM9();
} }
flushDCacheRange(fb, sizeof(struct fb));
invokeArm11Function(ARM11); invokeArm11Function(ARM11);
} }
@ -223,6 +228,8 @@ u32 initScreens(void)
if(needToInit) if(needToInit)
{ {
flushDCacheRange(&config, 4);
flushDCacheRange(fb, sizeof(struct fb));
invokeArm11Function(ARM11); invokeArm11Function(ARM11);
//Turn on backlight //Turn on backlight

View File

@ -6,6 +6,7 @@
#include "i2c.h" #include "i2c.h"
#include "buttons.h" #include "buttons.h"
#include "memory.h" #include "memory.h"
#include "cache.h"
u32 waitInput(void) u32 waitInput(void)
{ {
@ -36,7 +37,7 @@ u32 waitInput(void)
void mcuReboot(void) void mcuReboot(void)
{ {
cleanInvalidateDCacheAndDMB(); //Ensure that all memory transfers have completed and that the data cache has been flushed flushEntireDCache(); //Ensure that all memory transfers have completed and that the data cache has been flushed
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2); i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2);
while(1); while(1);