2023-09-29 01:03:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @file mm_space.c
|
2023-12-02 10:16:14 +08:00
|
|
|
|
* @author ATShining (1358745329@qq.com)
|
2024-01-01 16:57:55 +08:00
|
|
|
|
* @brief
|
2023-09-29 01:03:19 +08:00
|
|
|
|
* @version 0.1
|
|
|
|
|
|
* @date 2023-09-29
|
2024-01-01 16:57:55 +08:00
|
|
|
|
*
|
2023-09-29 01:03:19 +08:00
|
|
|
|
* @copyright Copyright (c) 2023
|
2024-01-01 16:57:55 +08:00
|
|
|
|
*
|
2023-09-29 01:03:19 +08:00
|
|
|
|
*/
|
2023-08-26 23:12:31 +08:00
|
|
|
|
#include "types.h"
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
#include "mm_space.h"
|
|
|
|
|
|
#include "mpu.h"
|
2023-08-27 16:52:53 +08:00
|
|
|
|
#include "assert.h"
|
|
|
|
|
|
|
2023-08-27 21:25:09 +08:00
|
|
|
|
void mm_space_init(mm_space_t *mm_space, int is_knl)
|
2023-08-26 23:12:31 +08:00
|
|
|
|
{
|
2024-01-05 23:08:42 +08:00
|
|
|
|
// region_info_t *regi_info;
|
2023-08-27 16:52:53 +08:00
|
|
|
|
|
2023-12-05 00:01:26 +08:00
|
|
|
|
for (int i = 0; i < CONFIG_REGION_NUM; i++)
|
2023-08-26 23:12:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
mm_space->pt_regions[i].region_inx = -1;
|
|
|
|
|
|
}
|
2023-08-27 21:25:09 +08:00
|
|
|
|
if (!is_knl)
|
|
|
|
|
|
{
|
2024-01-05 23:08:42 +08:00
|
|
|
|
// regi_info = mm_space_alloc_pt_region(mm_space);
|
|
|
|
|
|
// assert(regi_info);
|
2023-11-25 17:44:54 +08:00
|
|
|
|
// mm_pages_init(&mm_space->mm_pages, regi_info);
|
2023-08-27 21:25:09 +08:00
|
|
|
|
}
|
2023-08-26 23:12:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
region_info_t *mm_space_alloc_pt_region(mm_space_t *m_space)
|
|
|
|
|
|
{
|
2024-01-01 16:57:55 +08:00
|
|
|
|
for (int i = 0; i < CONFIG_REGION_NUM; i++)
|
2023-08-26 23:12:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
/*TODO:原子操作*/
|
|
|
|
|
|
if (m_space->pt_regions[i].region_inx < 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_space->pt_regions[i].region_inx = (int16_t)i;
|
|
|
|
|
|
return &m_space->pt_regions[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
void mm_space_free_pt_region(mm_space_t *m_space, region_info_t *ri)
|
|
|
|
|
|
{
|
|
|
|
|
|
ri->region_inx = -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool_t mm_space_add(mm_space_t *m_space,
|
|
|
|
|
|
umword_t addr,
|
|
|
|
|
|
umword_t size,
|
2023-08-27 16:52:53 +08:00
|
|
|
|
uint8_t attrs)
|
2023-08-26 23:12:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
region_info_t *ri = mm_space_alloc_pt_region(m_space);
|
|
|
|
|
|
|
|
|
|
|
|
if (!ri)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
2024-01-01 16:57:55 +08:00
|
|
|
|
if (!is_power_of_2(size) || (addr & (!(size - 1))) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
//!< 申请的大小必须是2的整数倍,而且地址也必须是2的整数倍
|
|
|
|
|
|
mm_space_free_pt_region(m_space, ri);
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
2023-08-26 23:12:31 +08:00
|
|
|
|
mpu_calc_regs(ri, addr, ffs(size), attrs, 0);
|
2024-01-01 16:57:55 +08:00
|
|
|
|
ri->start_addr = addr;
|
|
|
|
|
|
ri->size = size;
|
2023-08-26 23:12:31 +08:00
|
|
|
|
return TRUE;
|
|
|
|
|
|
}
|
2024-01-01 16:57:55 +08:00
|
|
|
|
void mm_space_del(mm_space_t *m_space, umword_t addr)
|
2023-08-26 23:12:31 +08:00
|
|
|
|
{
|
2024-01-01 16:57:55 +08:00
|
|
|
|
for (int i = 0; i < CONFIG_REGION_NUM; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_space->pt_regions[i].region_inx >= 0 &&
|
|
|
|
|
|
m_space->pt_regions[i].start_addr == addr)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_space->pt_regions[i].region_inx = -1;
|
|
|
|
|
|
m_space->pt_regions[i].start_addr = 0;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-08-26 23:12:31 +08:00
|
|
|
|
}
|