This commit is contained in:
zhujiale 2024-08-29 16:21:19 +08:00 committed by Rbb666
parent 79445b6773
commit d1865d0d93
6 changed files with 122 additions and 64 deletions

View File

@ -1,5 +1,10 @@
#include "smp.h"
struct smp_call **global_work;
#define DBG_TAG "SMP"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
struct smp_call global_work[RT_CPUS_NR];
rt_err_t smp_call_handler(struct smp_event *event)
{
@ -18,64 +23,57 @@ rt_err_t smp_call_handler(struct smp_event * event)
void rt_smp_call_ipi_handler(int vector, void *param)
{
int err;
struct smp_call *work,*tmp;
int cur_cpu = rt_hw_cpu_id();
rt_spin_lock(&global_work[cur_cpu]->lock);
rt_list_for_each_entry_safe(work,tmp,&global_work[cur_cpu]->node,node)
rt_spin_lock(&global_work[cur_cpu].lock);
if(global_work[cur_cpu].event.event_id)
{
if(work->event)
{
err = smp_call_handler(work->event);
err = smp_call_handler(&global_work[cur_cpu].event);
if(err)
break;
rt_list_remove(&work->node);
rt_free(work);
{
LOG_E("Have no event\n");
rt_memset(&global_work[cur_cpu].event,0,sizeof(struct smp_event));
rt_spin_unlock(&global_work[cur_cpu].lock);
}
rt_memset(&global_work[cur_cpu].event,0,sizeof(struct smp_event));
}
rt_spin_unlock(&global_work[cur_cpu]->lock);
rt_spin_unlock(&global_work[cur_cpu].lock);
}
void rt_smp_call_func_cond(int cpu_mask, smp_func func, void *data)
{
rt_bool_t run_cur_cpu = RT_TRUE;
RT_DEBUG_NOT_IN_INTERRUPT;
struct smp_call work;
struct smp_event event;
rt_bool_t need_call = RT_TRUE;
int cur_cpu = rt_hw_cpu_id();
int cpuid = 1 << cur_cpu;
int tmp_id = 0;
int tmp_mask = cpu_mask;
int tmp_mask;
if(cpuid & ~cpu_mask)
run_cur_cpu = RT_FALSE;
if(run_cur_cpu)
if(cpuid & cpu_mask)
{
func(data);
if(!(cpu_mask & cpuid))
need_call = RT_FALSE;
else
cpu_mask = cpu_mask & (~cpuid);
}
if(!cpu_mask)
need_call = RT_FALSE;
tmp_mask = cpu_mask;
if(need_call)
{
while(tmp_mask)
{
if((tmp_mask & 1) && tmp_id < RT_CPUS_NR)
if((tmp_mask & 1) && (tmp_id < RT_CPUS_NR))
{
struct smp_event *event = rt_calloc(1, sizeof(struct smp_event));
event->event_id = SMP_CALL_EVENT_FUNC;
event->func = func;
event->data = data;
event->cpu_mask = cpu_mask;
struct smp_call *work = rt_calloc(1, sizeof(struct smp_call));
if(work == RT_NULL)
break;
work->event = event;
rt_spin_lock(&global_work[tmp_id]->lock);
rt_list_insert_before(&global_work[tmp_id]->node, &work->node);
rt_spin_unlock(&global_work[tmp_id]->lock);
event.event_id = SMP_CALL_EVENT_FUNC;
event.func = func;
event.data = data;
event.cpu_mask = cpu_mask;
rt_spin_lock(&global_work[tmp_id].lock);
global_work[tmp_id].event = event;
rt_spin_unlock(&global_work[tmp_id].lock);
}
tmp_id++;
tmp_mask = tmp_mask >> 1;
@ -83,28 +81,14 @@ void rt_smp_call_func_cond(int cpu_mask, smp_func func, void *data)
rt_hw_ipi_send(RT_IPI_FUNC, cpu_mask);
}
}
struct rt_spinlock lock_1;
void smp_init(void)
{
struct smp_call **work_list = (struct smp_call **)rt_malloc(sizeof(struct smp_call *));
rt_spin_lock_init(&lock_1);
for(int i = 0; i < RT_CPUS_NR; i++)
{
work_list[i] = rt_calloc(1, sizeof(struct smp_call));
if(work_list[i] == RT_NULL)
break;
rt_list_init(&work_list[i]->node);
rt_spin_lock_init(&work_list[i]->lock);
rt_memset(&global_work[i],0,sizeof(struct smp_call));
rt_spin_lock_init(&global_work[i].lock);
}
global_work = work_list;
}
void test_call(void *data)
{
rt_kprintf("call cpu id = %d \n",rt_hw_cpu_id());
}
void test()
{
int cpu_mask = 0xf;
rt_smp_call_func_cond(cpu_mask,test_call, RT_NULL);
}

View File

@ -5,14 +5,6 @@ typedef void (*smp_func)(void *data);
#define SMP_CALL_EVENT_FUNC 0x1
struct smp_call
{
struct rt_spinlock lock;
struct rt_list_node node;
struct smp_event *event;
};
struct smp_event
{
int cpu_mask;
@ -21,8 +13,15 @@ struct smp_event
smp_func func;
};
struct smp_call
{
struct rt_spinlock lock;
struct rt_list_node node;
struct smp_event event;
};
void test();
void rt_smp_call_func_cond(int cpu_mask,smp_func func, void *data);
void rt_smp_call_ipi_handler(int vector, void *param);
void smp_init(void);

View File

@ -15,6 +15,7 @@ rsource "drivers/ipc/Kconfig"
rsource "posix/Kconfig"
rsource "mm/Kconfig"
rsource "tmpfs/Kconfig"
rsource "smp/Kconfig"
endif
endmenu

View File

@ -0,0 +1,6 @@
menu "SMP Testcase"
config UTEST_SMP_CALL_FUNC
bool "Call random cpu to run func"
default n
endmenu

View File

@ -0,0 +1,13 @@
Import('rtconfig')
from building import *
cwd = GetCurrentDir()
src = []
CPPPATH = [cwd]
if GetDepend(['RT_USING_SMP','UTEST_SMP_CALL_FUNC']):
src += ['smp.c']
group = DefineGroup('utestcases', src, depend = ['RT_USING_UTESTCASES'], CPPPATH = CPPPATH)
Return('group')

View File

@ -0,0 +1,55 @@
#include <rtdevice.h>
#include "utest.h"
#include "utest_assert.h"
#include "smp.h"
int pass_count = 0;
int pass = 1000;
struct rt_spinlock lock;
void test_call(void *data)
{
rt_spin_lock(&lock);
int *i = (int *)data;
int id = rt_hw_cpu_id();
*i &= ~(1 << id);
if(*i == 0)
pass_count++;
rt_spin_unlock(&lock);
}
void test()
{
int cpu_mask = 0xf;
for(int i =0 ;i < 1000 ;i++)
{
cpu_mask = rand()% 0xf;
if (cpu_mask == 0)
pass--;
rt_smp_call_func_cond(cpu_mask,test_call, &cpu_mask);
if(i % 20 == 0)
rt_kprintf("#");
rt_thread_mdelay(1);
}
rt_kprintf("\n");
}
static rt_err_t utest_tc_init(void)
{
rt_spin_lock_init(&lock);
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
uassert_true(pass_count == pass);
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(test);
}
UTEST_TC_EXPORT(testcase, "testcase.smp.smp", utest_tc_init, utest_tc_cleanup, 10);