-
Notifications
You must be signed in to change notification settings - Fork 8.1k
dma: New syscalls #97652
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
Draft
teburd
wants to merge
1
commit into
zephyrproject-rtos:main
Choose a base branch
from
teburd:dma_new_syscalls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
dma: New syscalls #97652
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -784,6 +784,125 @@ static inline uint32_t dma_burst_index(uint32_t burst) | |
*/ | ||
#define DMA_COPY_ALIGNMENT(node) DT_PROP(node, dma_copy_alignment) | ||
|
||
|
||
/** | ||
* @brief A handle around a DMA channel provided as a kernel object | ||
* | ||
* In order to provide per channel permissions access on a DMA a kobject | ||
* per dma channel is provided. This could also be used for convienence around | ||
* instantiation of dma channels from device tree using some macros | ||
* that understand a particular nodes DMA channel configuration. This isn't | ||
* always a single channel or commonly named property though. | ||
*/ | ||
struct dma_channel { | ||
const struct device *dma; | ||
uint32_t channel; | ||
}; | ||
|
||
/** | ||
* @brief A handle around a hardware FIFO address provided as a kernel object | ||
* | ||
* In order to provide secure transfers to/from buffers and to/from hardware fifos | ||
* in a syscall, the hardware FIFO needs to be a well understood kernel object with | ||
* access control. Effectively "just" a memory address for the machine, this kobject | ||
* can be checked against for each usermode thread wishing to send/recv data to it. | ||
*/ | ||
struct dma_fifo { | ||
uint32_t hwfifo; | ||
}; | ||
|
||
/** | ||
* @brief Start a DMA channel | ||
* | ||
* @see `dma_start` for details | ||
* | ||
* @param dma_chan A DMA channel to start | ||
* @retval 0 Success | ||
* @retval -errno Error | ||
*/ | ||
__syscall int dma_channel_start(const struct dma_channel *dma_chan); | ||
|
||
static inline int z_impl_dma_channel_start(const struct dma_channel *dma_chan) | ||
{ | ||
return dma_start(dma_chan->dma, dma_chan->channel); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, I don't see any dma handlers in this commit (e.g. z_vrfy_dma_channel_start()). Did you forget this from the second commit? |
||
|
||
/** | ||
* @brief Stop a DMA channel | ||
* | ||
* @see `dma_stop` for details | ||
* | ||
* @param dma_chan A DMA channel to stop | ||
* @retval 0 Success | ||
* @retval -errno Error | ||
*/ | ||
__syscall int dma_channel_stop(const struct dma_channel *dma_chan); | ||
|
||
static inline int z_impl_dma_channel_stop(const struct dma_channel *dma_chan) | ||
{ | ||
return dma_stop(dma_chan->dma, dma_chan->channel); | ||
} | ||
|
||
/** | ||
* @brief Obtain the status of a DMA channel | ||
* | ||
* @param dma_chan A DMA channel to stop | ||
* @retval 0 Success | ||
* @retval -errno Error | ||
*/ | ||
__syscall int dma_channel_get_status(const struct dma_channel *dma_chan, struct dma_status *status); | ||
|
||
static inline int z_impl_dma_channel_get_status(const struct dma_channel *dma_chan, | ||
struct dma_status *status) | ||
{ | ||
return dma_get_status(dma_chan->dma, dma_chan->channel, status); | ||
} | ||
|
||
|
||
/** | ||
* @brief Reload a DMA channel transfer from a hardware FIFO | ||
* | ||
* @see `dma_reload` for details | ||
* | ||
* @param dma_chan A DMA channel to stop | ||
* @param src_fifo A hardware FIFO handle | ||
* @param dst A buffer address | ||
* @param len Length of the transfer | ||
* @retval 0 Success | ||
* @retval -errno Error | ||
*/ | ||
__syscall int dma_channel_reload_from_fifo(const struct dma_channel *dma_chan, | ||
struct dma_fifo *src_fifo, uint32_t dst, size_t len); | ||
|
||
static inline int z_impl_dma_channel_reload_from_fifo(const struct dma_channel *dma_chan, | ||
struct dma_fifo *src_fifo, | ||
uint32_t dst, size_t len) | ||
{ | ||
return dma_reload(dma_chan->dma, dma_chan->channel, src_fifo->hwfifo, dst, len); | ||
} | ||
|
||
/** | ||
* @brief Reload a DMA channel transfer to a hardware FIFO | ||
* | ||
* @see `dma_reload` for details | ||
* | ||
* @param dma_chan A DMA channel to stop | ||
* @param src A buffer address | ||
* @param dst_fifo A hardware FIFO handle | ||
* @param len Length of the transfer | ||
* @retval 0 Success | ||
* @retval -errno Error | ||
*/ | ||
__syscall int dma_channel_reload_to_fifo(const struct dma_channel *dma_chan, uint32_t src, | ||
struct dma_fifo *dst_fifo, size_t len); | ||
|
||
static inline int z_impl_dma_channel_reload_to_fifo(const struct dma_channel *dma_chan, | ||
uint32_t src, struct dma_fifo *dst_fifo, | ||
size_t len) | ||
{ | ||
return dma_reload(dma_chan->dma, dma_chan->channel, src, dst_fifo->hwfifo, len); | ||
} | ||
|
||
/** | ||
* @} | ||
*/ | ||
|
@@ -792,4 +911,6 @@ static inline uint32_t dma_burst_index(uint32_t burst) | |
} | ||
#endif | ||
|
||
#include <zephyr/syscalls/dma.h> | ||
|
||
#endif /* ZEPHYR_INCLUDE_DRIVERS_DMA_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(dma_usermode) | ||
|
||
FILE(GLOB app_sources src/*.c) | ||
target_sources(app PRIVATE ${app_sources}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/ { | ||
Check warning on line 1 in tests/drivers/dma/usermode/app.overlay
|
||
dma: dma { | ||
compatible = "zephyr,dma-emul"; | ||
#dma-cells = <1>; | ||
dma-channels = <8>; | ||
stack-size = <4096>; | ||
status = "okay"; | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CONFIG_ZTEST=y | ||
CONFIG_USERSPACE=y | ||
CONFIG_LOG=y | ||
CONFIG_DMA=y | ||
CONFIG_DMA_EMUL=y | ||
CONFIG_DMA_LOG_LEVEL_INF=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright (c) 2025 Intel Corporation. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* @file | ||
* @brief Verify usermode APIs for DMA | ||
*/ | ||
|
||
#include "zephyr/fatal_types.h" | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/drivers/dma.h> | ||
#include <zephyr/ztest.h> | ||
|
||
ZTEST_BMEM static volatile bool expect_fault; | ||
ZTEST_BMEM static volatile unsigned int expected_reason; | ||
ZTEST_BMEM static volatile bool faulted; | ||
|
||
static void clear_fault(void) | ||
{ | ||
expect_fault = false; | ||
compiler_barrier(); | ||
} | ||
|
||
static void set_fault(unsigned int reason) | ||
{ | ||
faulted = false; | ||
expect_fault = true; | ||
expected_reason = reason; | ||
compiler_barrier(); | ||
} | ||
|
||
void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf) | ||
{ | ||
printk("Caught system error -- reason %d\n", reason); | ||
faulted = true; | ||
|
||
if (expect_fault) { | ||
if (expected_reason == reason) { | ||
printk("System error was expected\n"); | ||
clear_fault(); | ||
} else { | ||
printk("Wrong fault reason, expecting %d\n", | ||
expected_reason); | ||
TC_END_REPORT(TC_FAIL); | ||
k_fatal_halt(reason); | ||
} | ||
} else { | ||
printk("Unexpected fault during test\n"); | ||
TC_END_REPORT(TC_FAIL); | ||
k_fatal_halt(reason); | ||
} | ||
} | ||
|
||
const struct device *dma = DEVICE_DT_GET(DT_NODELABEL(dma)); | ||
|
||
ZTEST_USER(dma_usermode, test_invalid_chan_filter) | ||
{ | ||
zassert_true(k_is_user_context()); | ||
|
||
set_fault(K_ERR_CPU_EXCEPTION); | ||
dma_chan_filter(dma, 0, NULL); | ||
|
||
TC_END_REPORT(TC_FAIL); | ||
} | ||
|
||
|
||
ZTEST_USER(dma_usermode, test_invalid_config) | ||
{ | ||
zassert_true(k_is_user_context()); | ||
|
||
struct dma_config cfg; | ||
|
||
set_fault(K_ERR_CPU_EXCEPTION); | ||
dma_config(dma, 0, &cfg); | ||
|
||
TC_END_REPORT(TC_FAIL); | ||
} | ||
|
||
ZTEST_USER(dma_usermode, test_invalid_start) | ||
{ | ||
zassert_true(k_is_user_context()); | ||
|
||
set_fault(K_ERR_CPU_EXCEPTION); | ||
dma_start(dma, 0); | ||
|
||
TC_END_REPORT(TC_FAIL); | ||
} | ||
|
||
ZTEST_USER(dma_usermode, test_invalid_stop) | ||
{ | ||
zassert_true(k_is_user_context()); | ||
|
||
set_fault(K_ERR_CPU_EXCEPTION); | ||
dma_stop(dma, 0); | ||
|
||
TC_END_REPORT(TC_FAIL); | ||
} | ||
|
||
ZTEST_USER(dma_usermode, test_invalid_suspend) | ||
{ | ||
zassert_true(k_is_user_context()); | ||
|
||
set_fault(K_ERR_CPU_EXCEPTION); | ||
dma_suspend(dma, 0); | ||
|
||
TC_END_REPORT(TC_FAIL); | ||
} | ||
|
||
ZTEST_USER(dma_usermode, test_invalid_resume) | ||
{ | ||
zassert_true(k_is_user_context()); | ||
|
||
set_fault(K_ERR_CPU_EXCEPTION); | ||
dma_resume(dma, 0); | ||
|
||
TC_END_REPORT(TC_FAIL); | ||
} | ||
|
||
ZTEST_USER(dma_usermode, test_invalid_reload) | ||
{ | ||
zassert_true(k_is_user_context()); | ||
|
||
set_fault(K_ERR_CPU_EXCEPTION); | ||
dma_reload(dma, 0, 0, 1, 1); | ||
|
||
TC_END_REPORT(TC_FAIL); | ||
} | ||
|
||
ZTEST_USER(dma_usermode, test_invalid_get_status) | ||
{ | ||
struct dma_status status; | ||
|
||
zassert_true(k_is_user_context()); | ||
|
||
set_fault(K_ERR_CPU_EXCEPTION); | ||
dma_get_status(dma, 0, &status); | ||
|
||
TC_END_REPORT(TC_FAIL); | ||
} | ||
|
||
void *userspace_setup(void) | ||
{ | ||
return NULL; | ||
} | ||
|
||
|
||
ZTEST_SUITE(dma_usermode, NULL, userspace_setup, NULL, NULL, NULL); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
tests: | ||
drivers.dma.usermode: | ||
filter: CONFIG_ARCH_HAS_USERSPACE | ||
tags: | ||
- drivers | ||
- userspace | ||
- dma |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@teburd Ok this looks something I could work with. I have my draft code that implements the syscalls on SOF side. I did this better understand the pros and cons. Code here:
https://github.com/kv2019i/sof/commits/202510-userspace-sof-dma-test/
So I could perhaps try to use this PR as a basis.
You did now add more interfaces (like reload) to user-space? Were you thinking to add the Zephyr side API to also cover calls like the get dma_config() and dma_get_attribute()? dma_config() is a bit problematic as well as the generic interface has lot of configuration options, including passing a callback function. These obviously won't work from user-space. For SOF, we really use a subset of the interface, so I could expose a more limited version of dma_config() (as sof_dma_config()) and verify the allowed fields the vrfy function. Aside those two interfaces, I don't see much missing.