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

drivers: dma: esp32s3: Add DMA support for esp32s3 #57495

Merged
merged 3 commits into from
May 10, 2023

Conversation

LucasTambor
Copy link
Collaborator

Add GDMA support for esp32s3.
Remove suspend/resume since they are optional and do the same as start/stop.
Fix possible null pointer derreference.

@zephyrbot zephyrbot added area: Devicetree Binding PR modifies or adds a Device Tree binding area: Xtensa Xtensa Architecture platform: ESP32 Espressif ESP32 area: DMA Direct Memory Access labels May 2, 2023
@marekmatej
Copy link

maybe put files in test/drivers/dma/*.[conf,overlay] to separate commit?

@LucasTambor
Copy link
Collaborator Author

@marekmatej done ;)

teburd
teburd previously approved these changes May 5, 2023
static int dma_esp32_config_rx_descriptor(struct dma_esp32_channel *dma_channel,
struct dma_block_config *block)
{
memset(&dma_channel->desc, 0, sizeof(dma_channel->desc));
dma_channel->desc.buffer = (void *)block->dest_address;
dma_channel->buf_original = (uint8_t *)block->dest_address;
k_free(dma_channel->buf_temp);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are freeing buf_temp which is allocated in the dma_esp32_config_tx_descriptor. Why cannot it be allocated for the driver lifetime?

@@ -577,7 +624,7 @@ DMA_ESP32_DEFINE_IRQ_HANDLER(4)
}, \
}; \
\
DEVICE_DT_INST_DEFINE(idx, &dma_esp32_init, NULL, &dma_data_##idx, &dma_config_##idx, \
DEVICE_DT_INST_DEFINE(idx, &dma_esp32_init, NULL, &dma_data_##idx, &dma_config_##idx, \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you add trailing space

sylvioalves
sylvioalves previously approved these changes May 5, 2023
Copy link

@marekmatej marekmatej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tx_descriptor func. needs to free the buf_temp too

sylvioalves
sylvioalves previously approved these changes May 5, 2023
Copy link
Collaborator

@teburd teburd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that the heap is still being used here

static int dma_esp32_config_rx_descriptor(struct dma_esp32_channel *dma_channel,
struct dma_block_config *block)
{
memset(&dma_channel->desc, 0, sizeof(dma_channel->desc));
dma_channel->desc.buffer = (void *)block->dest_address;
dma_channel->buf_original = (uint8_t *)block->dest_address;
k_free(dma_channel->buf_temp);
dma_channel->buf_temp = NULL;
if (!esp_ptr_dma_capable((uint32_t *)block->dest_address)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we trying to allocate on behalf of callers here, if an invalid buffer for DMA is being provided this seems like a bug that should be asserted on.

I'm a little confused by the code here to malloc/free a temporary buffer when that's not the case. Is there a particular scenario esp32 expects this to occur?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are freeing the channel's temporary buffer every time a new descriptor needs to be created. In case the original buffer is in a valid DMA region, the k_free can handle a NULL pointer without issue and no allocation is needed. In case it is in an invalid region, we create a temporary one to handle the transactions, freeing it and reallocating according the size required by the transactions. I'm trying to deal with both scenarios here, since the original buffer location depends on the application or driver using it.

Did I answer your question? I'm not shure haha

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess what I'm suggesting is this is a bug then in the driver or application that should be caught. There's an RFC for managing buffers #57602

At the moment this would be, to my best knowledge, the only DMA driver that attempts to fix the buffer you asked a copy to by allocating from the heap. I would suggest pushing that issue up the stack as this is a surprising place to have that happen.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice! I will follow up this RFC and contribute in whatever way I can. But since it is an ongoing discussion, should we block the merge of this commit? This strategy of dealing with buffer is around for quite a while, this commit only add support for esp32s3 and small fixes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd point out that generally Zephyr is trying to follow misra, and in misra-c "dynamic allocation is not allowed" See our guidelines https://docs.zephyrproject.org/latest/contribute/coding_guidelines/index.html#main-rules

So I think this is something that needs to be revisited but today should be considered a programming error trying to ask DMA to work with a buffer that doesn't conform to DMA requirements.

Add GDMA support for esp32s3.
Remove suspend/resume since they are optional and do
the same as start/stop.
Fix possible null pointer derreference.

Signed-off-by: Lucas Tamborrino <[email protected]>
According to the coding guidelines "dynamic allocation is not allowed".

This commit removes handling invalid DMA capable buffers by allocating
temporary buffer in a valid memory region, considering them as errors.

Signed-off-by: Lucas Tamborrino <[email protected]>
Add esp32s3 overlays for dma tests.

Signed-off-by: Lucas Tamborrino <[email protected]>
@LucasTambor LucasTambor requested a review from teburd May 9, 2023 19:32
Copy link

@marekmatej marekmatej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@carlescufi carlescufi merged commit 9431e61 into zephyrproject-rtos:main May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree Binding PR modifies or adds a Device Tree binding area: DMA Direct Memory Access area: Xtensa Xtensa Architecture platform: ESP32 Espressif ESP32
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants