-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drivers: stm32: SPI: Check that SPI buffers are in a nocache region
DMA only works with non-cached memory regions in H7. Check them and return an error if they don't match this condition. Signed-off-by: Daniel Gaston Ochoa <[email protected]>
- Loading branch information
1 parent
e1e4fcc
commit 929f446
Showing
7 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,19 @@ | ||
/* | ||
* Copyright (c) 2023 Graphcore Ltd. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "spi.h" | ||
|
||
bool spi_buf_set_in_nocache(const struct spi_buf_set *bufs) | ||
{ | ||
for (size_t i = 0; i < bufs->count; i++) { | ||
const struct spi_buf *buf = &bufs->buffers[i]; | ||
|
||
if (!dt_buf_in_nocache((uintptr_t)buf->buf, buf->len)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
This file contains 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,27 @@ | ||
/* | ||
* Copyright (c) 2023 Graphcore Ltd. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef ZEPHYR_DRIVERS_SPI_SPI_H_ | ||
#define ZEPHYR_DRIVERS_SPI_SPI_H_ | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
|
||
#include <zephyr/devicetree.h> | ||
#include <zephyr/drivers/spi.h> | ||
|
||
/** | ||
* @brief Checks that all buffers in `bufs` are in nocache memory region. This | ||
* should be used in platforms where DMA cannot access cache memory, and so any | ||
* buffer used for DMA operations must be in a nocache region. | ||
* | ||
* @param bufs Buffers to check | ||
* | ||
* @return True if all buffers in bufs are in a nocache region, false otherwise. | ||
*/ | ||
bool spi_buf_set_in_nocache(const struct spi_buf_set *bufs); | ||
|
||
#endif /* ZEPHYR_DRIVERS_SPI_SPI_H_ */ |
This file contains 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 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,104 @@ | ||
/** | ||
* @file | ||
* @brief Memory Devicetree macro public API header file. | ||
*/ | ||
|
||
/* | ||
* Copyright (c) 2023 Graphcore Ltd. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef ZEPHYR_INCLUDE_DEVICETREE_MEM_H_ | ||
#define ZEPHYR_INCLUDE_DEVICETREE_MEM_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stdint.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
|
||
#include <zephyr/devicetree/memory-attr.h> | ||
|
||
/** | ||
* @defgroup devicetree-memory Devicetree memory API | ||
* @ingroup devicetree | ||
* @{ | ||
*/ | ||
|
||
/** | ||
* @brief Container for memory-region information specified in devicetree | ||
* | ||
* This type contains pointers to the begin and end of the memory region and | ||
* a string that specifies such region's properties. | ||
* | ||
*/ | ||
struct dt_mem_region { | ||
const uintptr_t start; | ||
const uintptr_t end; | ||
bool isnocache; | ||
}; | ||
|
||
#define _DT_IS_NOCACHE_MEM_REGION(node_id) \ | ||
COND_CODE_1(DT_ENUM_HAS_VALUE(node_id, zephyr_memory_attr, RAM_NOCACHE), \ | ||
(true), \ | ||
(false)) | ||
|
||
/** | ||
* @brief Can be used to initialize a @see{struct dt_mem_region}. | ||
* | ||
* @param node_id Node ID of the memory region declared in device tree from | ||
* which the information will be obtained. | ||
* | ||
* @example | ||
* | ||
* struct dt_mem_region mem_r = { | ||
* DT_GET_MEM_REGION(DT_NODELABEL(<node_label>)) | ||
* }; | ||
* | ||
*/ | ||
#define DT_GET_MEM_REGION(node_id) \ | ||
{ \ | ||
.start = (const uintptr_t)DT_REG_ADDR(node_id), \ | ||
.end = \ | ||
(const uintptr_t)(DT_REG_ADDR(node_id) + DT_REG_SIZE(node_id)) - 1, \ | ||
.isnocache = _DT_IS_NOCACHE_MEM_REGION(node_id) \ | ||
}, | ||
|
||
/** | ||
* @brief Return true if buf is in a nocache memory region | ||
* among mem_regions. | ||
* | ||
* @param buf Buffer to be checked | ||
* @param len_bytes Length of the buffer above | ||
* | ||
*/ | ||
static inline bool dt_buf_in_nocache(uintptr_t buf, size_t len_bytes) | ||
{ | ||
const struct dt_mem_region mem_regions[] = { | ||
DT_MEMORY_ATTR_FOREACH_NODE(DT_GET_MEM_REGION) | ||
}; | ||
|
||
for (size_t i = 0; i < ARRAY_SIZE(mem_regions); i++) { | ||
const struct dt_mem_region *mem_reg = &mem_regions[i]; | ||
|
||
const bool buf_within_bounds = | ||
(buf >= mem_reg->start) && ((buf + len_bytes - 1) <= mem_reg->end); | ||
if (buf_within_bounds && mem_reg->isnocache) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @} | ||
*/ | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* ZEPHYR_INCLUDE_DEVICETREE_MEM_H_ */ |
This file contains 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 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