Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【增加】增加smp_ipi_call #9427

Merged
merged 15 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions components/smp/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from building import *

cwd = GetCurrentDir()
src = []
if GetDepend("RT_USING_SMP"):
src += Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('mprotect', src, depend = [''], CPPPATH = CPPPATH)
zhuzhuzhus marked this conversation as resolved.
Show resolved Hide resolved

Return('group')
145 changes: 145 additions & 0 deletions components/smp/smp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright (c) 2006-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024/9/12 zhujiale the first version
*/

#include "smp.h"

#define DBG_TAG "SMP"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

struct smp_call global_work[RT_CPUS_NR];
rt_atomic_t wait;
rt_err_t smp_call_handler(struct smp_event *event)
zhuzhuzhus marked this conversation as resolved.
Show resolved Hide resolved
{
switch(event->event_id)
{
case SMP_CALL_EVENT_FUNC:
event->func(event->data);
rt_atomic_add(&wait,1);
break;
default:
LOG_E("error event id\n");
return -RT_ERROR;
}
return RT_EOK;
}
void rt_smp_call_ipi_handler(int vector, void *param)
{
int err;
int cur_cpu = rt_hw_cpu_id();
rt_spin_lock(&global_work[cur_cpu].lock);
if(global_work[cur_cpu].event.event_id)
zhuzhuzhus marked this conversation as resolved.
Show resolved Hide resolved
{
err = smp_call_handler(&global_work[cur_cpu].event);
if(err)
{
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);

}

/**
* @brief call function on specified CPU ,
*
* @param cpu_mask cpu mask for call
* @param func the function pointer
* @param data the data pointer
* @param flag call flag if you set SMP_CALL_WAIT_ALL
* then it will wait all cpu call finish and return
* else it will call function on specified CPU and return immediately
* @param cond the condition function pointer,if you set it then it will call function only when cond return true
*/
void rt_smp_call_func_cond(int cpu_mask, smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond)
{
RT_DEBUG_NOT_IN_INTERRUPT;
struct smp_event event;
rt_bool_t need_call = RT_TRUE,need_wait = RT_FALSE;
int cur_cpu = rt_hw_cpu_id();
int cpuid = 1 << cur_cpu;
int tmp_id = 0,cpu_nr = 0;
int tmp_mask;

if(flag == SMP_CALL_WAIT_ALL)
{
need_wait = RT_TRUE;
rt_atomic_store(&wait,0);
}

if(cpuid & cpu_mask)
{
func(data);
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(cond && !cond(tmp_id,data))
continue;
cpu_nr++;
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;
}
rt_hw_ipi_send(RT_FUNC_IPI, cpu_mask);
}

if(need_wait)
{
while(rt_atomic_load(&wait) != cpu_nr);
}
}

void rt_call_each_cpu(smp_call_func_back func, void *data,rt_uint8_t flag)
{
rt_smp_call_func_cond(RT_ALL_CPU,func,data,flag,RT_NULL);
}

void rt_call_each_cpu_cond(smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func)
{
rt_smp_call_func_cond(RT_ALL_CPU,func,data,flag,cond_func);
}
void rt_call_any_cpu(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag)
{
rt_smp_call_func_cond(cpu_mask,func,data,flag,RT_NULL);
}

void rt_call_any_cpu_cond(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func)
{
rt_smp_call_func_cond(cpu_mask,func,data,flag,cond_func);
}

void smp_init(void)
{
for(int i = 0; i < RT_CPUS_NR; i++)
{
rt_memset(&global_work[i],0,sizeof(struct smp_call));
rt_spin_lock_init(&global_work[i].lock);
}
}
34 changes: 34 additions & 0 deletions components/smp/smp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef __SMP_IPI_H__
#define __SMP_IPI_H__
#include <rtthread.h>
typedef void (*smp_call_func_back)(void *data);
typedef rt_bool_t (*smp_cond)(int cpu, void *info);

#define SMP_CALL_EVENT_FUNC 0x1

#define SMP_CALL_WAIT_ALL (1 << 0)
#define SMP_CALL_NO_WAIT (1 << 1)

#define RT_ALL_CPU ((1 << RT_CPUS_NR) - 1)
struct smp_event
{
int cpu_mask;
int event_id;
void *data;
smp_call_func_back func;
};
struct smp_call
{
struct rt_spinlock lock;
struct smp_event event;
};


void rt_smp_call_ipi_handler(int vector, void *param);
void rt_call_each_cpu(smp_call_func_back func, void *data,rt_uint8_t flag);
void rt_call_each_cpu_cond(smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func);
void rt_call_any_cpu(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag);
void rt_call_any_cpu_cond(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func);
void smp_init(void);
zhuzhuzhus marked this conversation as resolved.
Show resolved Hide resolved

#endif
1 change: 1 addition & 0 deletions examples/utest/testcases/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rsource "drivers/ipc/Kconfig"
rsource "posix/Kconfig"
rsource "mm/Kconfig"
rsource "tmpfs/Kconfig"
rsource "smp/Kconfig"
endif

endmenu
6 changes: 6 additions & 0 deletions examples/utest/testcases/smp/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
menu "SMP Testcase"

config UTEST_SMP_CALL_FUNC
bool "Call random cpu to run func"
default n
endmenu
13 changes: 13 additions & 0 deletions examples/utest/testcases/smp/SConscript
Original file line number Diff line number Diff line change
@@ -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')
75 changes: 75 additions & 0 deletions examples/utest/testcases/smp/smp.c
zhuzhuzhus marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <rtdevice.h>
#include "utest.h"
#include "utest_assert.h"
#include "smp.h"
int pass_count = 0;
int pass = 1000;
struct rt_spinlock lock;
zhuzhuzhus marked this conversation as resolved.
Show resolved Hide resolved

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 test1()
zhuzhuzhus marked this conversation as resolved.
Show resolved Hide resolved
{
int cpu_mask = 0xf;
for (int i = 0; i < 1000; i++)
{
cpu_mask = rand() % 0xf;
if (cpu_mask == 0)
pass--;
rt_call_any_cpu(cpu_mask,test_call, &cpu_mask, SMP_CALL_WAIT_ALL);
if (i % 20 == 0)
rt_kprintf("#");
}
rt_kprintf("\n");
uassert_true(pass_count == pass);
}

void test_call2(void *data)
{
rt_spin_lock(&lock);
int a = 100000;
while (a--);
int *i = (int *)data;
(*i)++;
rt_spin_unlock(&lock);
}
void test2(void)
{
int data = 0;
rt_call_each_cpu(test_call2, &data, SMP_CALL_WAIT_ALL);
uassert_true(data == RT_CPUS_NR);
rt_thread_mdelay(10);
data = 0;
rt_call_each_cpu(test_call2, &data, SMP_CALL_NO_WAIT);
uassert_true(data != RT_CPUS_NR);
}

static rt_err_t utest_tc_init(void)
{
pass_count = 0;
pass = 1000;
rt_spin_lock_init(&lock);
return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
return RT_EOK;
}
static void testcase(void)
{
UTEST_UNIT_RUN(test1);
UTEST_UNIT_RUN(test2);
}

UTEST_TC_EXPORT(testcase, "testcase.smp.smp", utest_tc_init, utest_tc_cleanup, 10);
6 changes: 6 additions & 0 deletions include/rtdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,12 @@ typedef struct rt_cpu_usage_stats *rt_cpu_usage_stats_t;
#define RT_STOP_IPI 1
#endif /* RT_STOP_IPI */

#ifndef RT_FUNC_IPI
#define RT_FUNC_IPI 2
#endif

#define RT_MAX_IPI 3

#define _SCHEDULER_CONTEXT(fileds) fileds

/**
Expand Down
5 changes: 4 additions & 1 deletion libcpu/aarch64/common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define DBG_TAG "cpu.aa64"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include <smp.h>
#include <cpu.h>
#include <mmu.h>
#include <cpuport.h>
Expand Down Expand Up @@ -302,11 +302,14 @@ void rt_hw_common_setup(void)
rt_thread_idle_sethook(rt_hw_idle_wfi);

#ifdef RT_USING_SMP
smp_init();
/* Install the IPI handle */
rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
rt_hw_ipi_handler_install(RT_STOP_IPI, rt_scheduler_ipi_handler);
rt_hw_ipi_handler_install(RT_FUNC_IPI, rt_smp_call_ipi_handler);
rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
rt_hw_interrupt_umask(RT_STOP_IPI);
rt_hw_interrupt_umask(RT_FUNC_IPI);
#endif
}

Expand Down
Loading