gint/src/cpu/cpu.c

68 lines
1.3 KiB
C
Raw Normal View History

kernel: driver and world system overhaul Changes in the driver and world system: * Rewrite driver logic to include more advanced concepts. The notion of binding a driver to a device is introduced to formalize wait(); power management is now built-in instead of being handled by the drivers (for instance DMA). The new driver model is described in great detail in <gint/drivers.h> * Formalized the concept of "world switch" where the hardware state is saved and later restored. As a tool, the world switch turns out to be very stable, and allows a lot of hardware manipulation that would be edgy at best when running in the OS world. * Added a GINT_DRV_SHARED flag for drivers to specify that their state is shared between worlds and not saved/restored. This has a couple of uses. * Exposed a lot more of the internal driver/world system as their is no particular downside to it. This includes stuff in <gint/drivers.h> and the driver's state structures in <gint/drivers/states.h>. This is useful for debugging and for cracked concepts, but there is no API stability guarantee. * Added a more flexible driver level system that allows any 2-digit level to be used. Feature changes: * Added a CPU driver that provides the VBR change as its state save. Because the whole context switch relied on interrupts being disabled anyway, there is no longer an inversion of control when setting the VBR; this is just part of the CPU driver's configuration. The CPU driver may also support other features such as XYRAM block transfer in the future. * Moved gint_inthandler() to the INTC driver under the name intc_handler(), pairing up again with intc_priority(). * Added a reentrant atomic lock based on the test-and-set primitive. Interrupts are disabled with IMASK=15 for the duration of atomic operations. * Enabled the DMA driver on SH7305-based fx-9860G. The DMA provides little benefit on this platform because the RAM is generally faster and buffers are ultimately small. The DMA is still not available on SH3-based fx-9860G models. * Solved an extremely obnoxious bug in timer_spin_wait() where the timer is not freed, causing the callback to be called when interrupts are re-enabled. This increments a random value on the stack. As a consequence of the change, removed the long delays in the USB driver since they are not actually needed. Minor changes: * Deprecated some of the elements in <gint/hardware.h>. There really is no good way to "enumerate" devices yet. * Deprecated gint_switch() in favor of a new function gint_world_switch() which uses the GINT_CALL abstraction. * Made the fx-9860G VRAM 32-aligned so that it can be used for tests with the DMA. Some features of the driver and world systems have not been implemented yet, but may be in the future: * Some driver flags should be per-world in order to create multiple gint worlds. This would be useful in Yatis' hypervisor. * A GINT_DRV_LAZY flag would be useful for drivers that don't want to be started up automatically during a world switch. This is relevant for drivers that have a slow start/stop sequence. However, this is tricky to do correctly as it requires dynamic start/stop and also tracking which world the current hardware state belongs to.
2021-04-23 18:50:20 +02:00
//---
// gint:cpu - Driver for CPU built-in features
//---
#include <gint/cpu.h>
#include <gint/drivers.h>
#include <gint/drivers/states.h>
#include <gint/hardware.h>
/* VBR address to be used in the next world's configure() */
static uint32_t configure_VBR = 0;
void cpu_configure_VBR(uint32_t VBR)
{
configure_VBR = VBR;
}
static void configure(void)
{
cpu_setVBR(configure_VBR);
configure_VBR = 0;
if(isSH4()) {
/* Set CPUOPM.INTMU. On the fx-CG 50 emulator it is available but
ignored by the emulator, so additional checks still need to be done
in interrupt handlers. */
cpu_setCPUOPM(cpu_getCPUOPM() | 0x00000008);
/* Enable DSP instructions */
cpu_sr_t SR = cpu_getSR();
SR.DSP = 1;
cpu_setSR(SR);
}
}
//---
// Device state and driver metadata
//---
static void hsave(cpu_state_t *s)
{
s->VBR = cpu_getVBR();
if(isSH4()) {
s->CPUOPM = cpu_getCPUOPM();
s->SR = cpu_getSR().lword;
}
}
static void hrestore(cpu_state_t const *s)
{
cpu_setVBR(s->VBR);
if(isSH4()) {
cpu_setCPUOPM(s->CPUOPM);
cpu_setSR((cpu_sr_t)s->SR);
}
}
gint_driver_t drv_cpu = {
.name = "CPU",
.configure = configure,
.hsave = (void *)hsave,
.hrestore = (void *)hrestore,
.state_size = sizeof(cpu_state_t),
};
GINT_DECLARE_DRIVER(00, drv_cpu);