2021-09-14 15:48:57 +00:00
|
|
|
/*
|
|
|
|
* mem-phys.c
|
|
|
|
*
|
|
|
|
* Created on: Aug 27, 2011
|
|
|
|
* Author: Tiberiu
|
|
|
|
*/
|
|
|
|
#include <memory-add.h>
|
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
uint32* BlockMap;
|
|
|
|
uint32 TotalBlocks;
|
2021-09-14 15:48:57 +00:00
|
|
|
uint32 TotalMemory;
|
2021-09-14 15:54:59 +00:00
|
|
|
uint32 UsedBlocks;
|
2021-09-14 15:48:57 +00:00
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
inline void ConvertIndexToBlock (uint32 index, uint32* address, uint32* offset)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
*address = (index >> 5);
|
|
|
|
*offset = index & 0x1f;
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
inline uint32 ConvertBlockToIndex (uint32 address, uint32 offset)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
return (address<<5) | offset;
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
void MemPhSetBlock (uint32 Block, uint8 value)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
uint32 addr, off;
|
2021-09-14 15:54:59 +00:00
|
|
|
ConvertIndexToBlock(Block, &addr, &off);
|
2021-09-14 15:48:57 +00:00
|
|
|
|
|
|
|
if (value) {
|
2021-09-14 15:54:59 +00:00
|
|
|
if ((BlockMap[addr] & (1<<off)) == 0) UsedBlocks++;
|
|
|
|
BlockMap[addr] |= 1<<off;
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2021-09-14 15:54:59 +00:00
|
|
|
if (BlockMap[addr] & (1<<off)) UsedBlocks--;
|
|
|
|
BlockMap[addr] &= ~(1<<off);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
uint32 MemPhGetBlock (uint32 Block)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
uint32 addr, off;
|
2021-09-14 15:54:59 +00:00
|
|
|
ConvertIndexToBlock(Block, &addr, &off);
|
2021-09-14 15:48:57 +00:00
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
return BlockMap[addr] & (1<<off);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
uint32 MemPhAllocateBlock()
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
uint32 addr, pos;
|
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
for (addr = 0; addr < TotalBlocks >> 5; addr++)
|
|
|
|
if (BlockMap[addr] != 0xffffffff)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
2021-09-14 15:54:59 +00:00
|
|
|
for (pos = 0; (BlockMap[addr] & (1<<pos)) != 0; pos++) ;
|
2021-09-14 15:48:57 +00:00
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
uint32 index = ConvertBlockToIndex(addr, pos);
|
|
|
|
MemPhSetBlock(index, 1);
|
|
|
|
|
|
|
|
// Return address
|
|
|
|
return (index<<12);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0xffffffff;
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
void MemPhFreeBlock(uint32 addr)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
2021-09-14 15:54:59 +00:00
|
|
|
uint32 Block = addr >> 12;
|
|
|
|
if (!Block) return;
|
2021-09-14 15:48:57 +00:00
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
MemPhSetBlock(Block, 0);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemPhInitialize(uint32 SystemMemoryKb)
|
|
|
|
{
|
2021-09-14 15:54:59 +00:00
|
|
|
TotalBlocks = SystemMemoryKb >> 2;
|
2021-09-14 15:48:57 +00:00
|
|
|
TotalMemory = SystemMemoryKb;
|
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
BlockMap = (uint32*) kmalloc(sizeof(uint32) * (1 + (TotalBlocks>>5)));
|
|
|
|
memset(BlockMap, 0, sizeof(uint32) * (1 + (TotalBlocks>>5)));
|
2021-09-14 15:51:43 +00:00
|
|
|
Log("Mem", "%#Started physical memory manager ok!, found %ukb\n", ColorLightGreen, SystemMemoryKb);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 15:54:59 +00:00
|
|
|
void MemPhReserveBlocks (uint32 address, uint32 length)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
address >>= 12;
|
|
|
|
length = (length>>12) + ((length & 0xfff) > 0);
|
|
|
|
uint32 end = address + length;
|
|
|
|
|
|
|
|
for (; address < end ; address++)
|
2021-09-14 15:54:59 +00:00
|
|
|
MemPhSetBlock(address, 1);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|