2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-10-20 10:31:12 -03:00
|
|
|
*
|
2018-01-05 02:19:51 -04:00
|
|
|
* Code by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
2022-02-20 23:44:56 -04:00
|
|
|
|
|
|
|
#include <hal.h>
|
2018-01-05 02:19:51 -04:00
|
|
|
#include "shared_dma.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
code to handle sharing of DMA channels between peripherals
|
|
|
|
*/
|
|
|
|
|
2021-02-23 00:07:53 -04:00
|
|
|
#if CH_CFG_USE_MUTEXES == TRUE && !defined(HAL_NO_SHARED_DMA)
|
2018-03-28 23:07:50 -03:00
|
|
|
|
2020-12-30 02:44:19 -04:00
|
|
|
#include <AP_Common/ExpandingString.h>
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
using namespace ChibiOS;
|
2020-11-21 12:00:11 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2018-01-13 00:02:05 -04:00
|
|
|
|
2019-02-09 17:51:13 -04:00
|
|
|
Shared_DMA::dma_lock Shared_DMA::locks[SHARED_DMA_MAX_STREAM_ID+1];
|
2020-11-21 12:00:11 -04:00
|
|
|
volatile Shared_DMA::dma_stats* Shared_DMA::_contention_stats;
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
void Shared_DMA::init(void)
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<SHARED_DMA_MAX_STREAM_ID; i++) {
|
2020-12-05 15:16:27 -04:00
|
|
|
chMtxObjectInit(&locks[i].mutex);
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// constructor
|
|
|
|
Shared_DMA::Shared_DMA(uint8_t _stream_id1,
|
|
|
|
uint8_t _stream_id2,
|
|
|
|
dma_allocate_fn_t _allocate,
|
|
|
|
dma_deallocate_fn_t _deallocate)
|
|
|
|
{
|
|
|
|
stream_id1 = _stream_id1;
|
|
|
|
stream_id2 = _stream_id2;
|
2020-12-05 15:16:27 -04:00
|
|
|
if (stream_id2 < stream_id1) {
|
|
|
|
stream_id1 = _stream_id2;
|
|
|
|
stream_id2 = _stream_id1;
|
|
|
|
}
|
2018-01-05 02:19:51 -04:00
|
|
|
allocate = _allocate;
|
|
|
|
deallocate = _deallocate;
|
|
|
|
}
|
|
|
|
|
2021-08-27 21:58:18 -03:00
|
|
|
/*
|
|
|
|
return true if a stream ID is shared between two peripherals
|
|
|
|
*/
|
|
|
|
bool Shared_DMA::is_shared(uint8_t stream_id)
|
|
|
|
{
|
|
|
|
return (stream_id < SHARED_DMA_MAX_STREAM_ID) && ((1U<<stream_id) & SHARED_DMA_MASK) != 0;
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
//remove any assigned deallocator or allocator
|
|
|
|
void Shared_DMA::unregister()
|
|
|
|
{
|
2019-02-10 03:04:57 -04:00
|
|
|
if (stream_id1 < SHARED_DMA_MAX_STREAM_ID &&
|
|
|
|
locks[stream_id1].obj == this) {
|
2018-03-14 03:06:30 -03:00
|
|
|
locks[stream_id1].deallocate(this);
|
2018-01-05 02:19:51 -04:00
|
|
|
locks[stream_id1].obj = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-10 03:04:57 -04:00
|
|
|
if (stream_id2 < SHARED_DMA_MAX_STREAM_ID &&
|
|
|
|
locks[stream_id2].obj == this) {
|
2018-03-14 03:06:30 -03:00
|
|
|
locks[stream_id2].deallocate(this);
|
2018-01-05 02:19:51 -04:00
|
|
|
locks[stream_id2].obj = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 17:51:13 -04:00
|
|
|
// lock one stream
|
2020-12-05 15:16:27 -04:00
|
|
|
bool Shared_DMA::lock_stream(uint8_t stream_id)
|
2019-02-09 17:51:13 -04:00
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
bool cont = false;
|
2019-02-10 03:04:57 -04:00
|
|
|
if (stream_id < SHARED_DMA_MAX_STREAM_ID) {
|
2020-12-05 15:16:27 -04:00
|
|
|
const thread_t* curr_owner = locks[stream_id].mutex.owner;
|
|
|
|
chMtxLock(&locks[stream_id].mutex);
|
|
|
|
cont = curr_owner != nullptr && curr_owner != locks[stream_id].mutex.owner;
|
2019-02-09 17:51:13 -04:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
return cont;
|
2019-02-09 17:51:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// unlock one stream
|
2020-12-05 15:16:27 -04:00
|
|
|
void Shared_DMA::unlock_stream(uint8_t stream_id, bool success)
|
2019-02-09 17:51:13 -04:00
|
|
|
{
|
2019-02-10 03:04:57 -04:00
|
|
|
if (stream_id < SHARED_DMA_MAX_STREAM_ID) {
|
2020-12-05 15:16:27 -04:00
|
|
|
chMtxUnlock(&locks[stream_id].mutex);
|
|
|
|
if (success && _contention_stats != nullptr) {
|
|
|
|
_contention_stats[stream_id1].transactions++;
|
|
|
|
}
|
2019-02-09 17:51:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock one stream, non-blocking
|
|
|
|
bool Shared_DMA::lock_stream_nonblocking(uint8_t stream_id)
|
|
|
|
{
|
2019-02-10 03:04:57 -04:00
|
|
|
if (stream_id < SHARED_DMA_MAX_STREAM_ID) {
|
2020-12-05 15:16:27 -04:00
|
|
|
return chMtxTryLock(&locks[stream_id].mutex);
|
2019-02-09 17:51:13 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
// lock the DMA channels
|
2018-03-02 06:34:57 -04:00
|
|
|
void Shared_DMA::lock_core(void)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
// see if another driver has DMA allocated. If so, call their
|
|
|
|
// deallocation function
|
2019-02-10 03:04:57 -04:00
|
|
|
if (stream_id1 < SHARED_DMA_MAX_STREAM_ID &&
|
2018-01-05 02:19:51 -04:00
|
|
|
locks[stream_id1].obj && locks[stream_id1].obj != this) {
|
2018-03-14 03:06:30 -03:00
|
|
|
locks[stream_id1].deallocate(locks[stream_id1].obj);
|
2018-01-05 02:19:51 -04:00
|
|
|
locks[stream_id1].obj = nullptr;
|
|
|
|
}
|
2019-02-10 03:04:57 -04:00
|
|
|
if (stream_id2 < SHARED_DMA_MAX_STREAM_ID &&
|
2018-01-05 02:19:51 -04:00
|
|
|
locks[stream_id2].obj && locks[stream_id2].obj != this) {
|
2018-03-14 03:06:30 -03:00
|
|
|
locks[stream_id2].deallocate(locks[stream_id2].obj);
|
2018-01-05 02:19:51 -04:00
|
|
|
locks[stream_id2].obj = nullptr;
|
|
|
|
}
|
2019-02-10 03:04:57 -04:00
|
|
|
if ((stream_id1 < SHARED_DMA_MAX_STREAM_ID && locks[stream_id1].obj == nullptr) ||
|
|
|
|
(stream_id2 < SHARED_DMA_MAX_STREAM_ID && locks[stream_id2].obj == nullptr)) {
|
2018-01-05 02:19:51 -04:00
|
|
|
// allocate the DMA channels and put our deallocation function in place
|
2018-03-14 03:06:30 -03:00
|
|
|
allocate(this);
|
2019-02-10 17:01:12 -04:00
|
|
|
if (stream_id1 < SHARED_DMA_MAX_STREAM_ID) {
|
2018-01-05 02:19:51 -04:00
|
|
|
locks[stream_id1].deallocate = deallocate;
|
|
|
|
locks[stream_id1].obj = this;
|
|
|
|
}
|
2019-02-10 17:01:12 -04:00
|
|
|
if (stream_id2 < SHARED_DMA_MAX_STREAM_ID) {
|
2018-01-05 02:19:51 -04:00
|
|
|
locks[stream_id2].deallocate = deallocate;
|
|
|
|
locks[stream_id2].obj = this;
|
|
|
|
}
|
2019-02-13 03:44:45 -04:00
|
|
|
}
|
|
|
|
#ifdef STM32_DMA_STREAM_ID_ANY
|
|
|
|
else if (stream_id1 == STM32_DMA_STREAM_ID_ANY ||
|
|
|
|
stream_id2 == STM32_DMA_STREAM_ID_ANY) {
|
2019-02-10 03:04:57 -04:00
|
|
|
// call allocator without needing locking
|
|
|
|
allocate(this);
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2019-02-13 03:44:45 -04:00
|
|
|
#endif
|
2020-12-05 15:16:27 -04:00
|
|
|
// update contention stats
|
|
|
|
if (_contention_stats != nullptr) {
|
|
|
|
if (stream_id1 < SHARED_DMA_MAX_STREAM_ID) {
|
|
|
|
if (contention) {
|
|
|
|
_contention_stats[stream_id1].contended_locks++;
|
|
|
|
} else {
|
|
|
|
_contention_stats[stream_id1].uncontended_locks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (stream_id2 < SHARED_DMA_MAX_STREAM_ID) {
|
|
|
|
if (contention) {
|
|
|
|
_contention_stats[stream_id2].contended_locks++;
|
|
|
|
} else {
|
|
|
|
_contention_stats[stream_id2].uncontended_locks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 23:59:10 -04:00
|
|
|
have_lock = true;
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-03-02 06:34:57 -04:00
|
|
|
// lock the DMA channels, blocking method
|
|
|
|
void Shared_DMA::lock(void)
|
|
|
|
{
|
2020-12-05 15:16:27 -04:00
|
|
|
bool c1 = lock_stream(stream_id1);
|
|
|
|
bool c2 = lock_stream(stream_id2);
|
|
|
|
contention = c1 || c2;
|
2018-03-02 06:34:57 -04:00
|
|
|
lock_core();
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock the DMA channels, non-blocking
|
|
|
|
bool Shared_DMA::lock_nonblock(void)
|
|
|
|
{
|
2019-02-10 17:01:12 -04:00
|
|
|
if (!lock_stream_nonblocking(stream_id1)) {
|
|
|
|
chSysDisable();
|
|
|
|
if (locks[stream_id1].obj != nullptr && locks[stream_id1].obj != this) {
|
|
|
|
locks[stream_id1].obj->contention = true;
|
2020-11-21 12:00:11 -04:00
|
|
|
if (_contention_stats != nullptr) {
|
|
|
|
_contention_stats[stream_id1].contended_locks++;
|
|
|
|
}
|
2018-03-02 06:34:57 -04:00
|
|
|
}
|
2019-02-10 17:01:12 -04:00
|
|
|
chSysEnable();
|
|
|
|
contention = true;
|
|
|
|
return false;
|
2018-03-02 06:34:57 -04:00
|
|
|
}
|
2020-11-21 12:00:11 -04:00
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
if (_contention_stats != nullptr && stream_id1 < SHARED_DMA_MAX_STREAM_ID) {
|
2020-11-21 12:00:11 -04:00
|
|
|
_contention_stats[stream_id1].uncontended_locks++;
|
|
|
|
}
|
|
|
|
|
2019-02-10 17:01:12 -04:00
|
|
|
if (!lock_stream_nonblocking(stream_id2)) {
|
2020-12-05 15:16:27 -04:00
|
|
|
unlock_stream(stream_id1, false);
|
2019-02-10 17:01:12 -04:00
|
|
|
chSysDisable();
|
|
|
|
if (locks[stream_id2].obj != nullptr && locks[stream_id2].obj != this) {
|
|
|
|
locks[stream_id2].obj->contention = true;
|
2020-11-21 12:00:11 -04:00
|
|
|
if (_contention_stats != nullptr) {
|
|
|
|
_contention_stats[stream_id2].contended_locks++;
|
|
|
|
}
|
2018-03-02 06:34:57 -04:00
|
|
|
}
|
2019-02-10 17:01:12 -04:00
|
|
|
chSysEnable();
|
|
|
|
contention = true;
|
|
|
|
return false;
|
2018-03-02 06:34:57 -04:00
|
|
|
}
|
|
|
|
lock_core();
|
2020-12-05 15:16:27 -04:00
|
|
|
if (_contention_stats != nullptr && stream_id2 < SHARED_DMA_MAX_STREAM_ID) {
|
2020-11-21 12:00:11 -04:00
|
|
|
_contention_stats[stream_id2].uncontended_locks++;
|
|
|
|
}
|
2018-03-02 06:34:57 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
// unlock the DMA channels
|
2020-12-05 15:16:27 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic error "-Wframe-larger-than=128"
|
|
|
|
void Shared_DMA::unlock(bool success)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-02-05 23:59:10 -04:00
|
|
|
osalDbgAssert(have_lock, "must have lock");
|
|
|
|
have_lock = false;
|
2020-12-05 15:16:27 -04:00
|
|
|
unlock_stream(stream_id2, success);
|
|
|
|
unlock_stream(stream_id1, success);
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2018-01-28 21:43:55 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
lock all channels - used on reboot to ensure no sensor DMA is in
|
|
|
|
progress
|
|
|
|
*/
|
|
|
|
void Shared_DMA::lock_all(void)
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<SHARED_DMA_MAX_STREAM_ID; i++) {
|
2019-02-09 17:51:13 -04:00
|
|
|
lock_stream(i);
|
2018-01-28 21:43:55 -04:00
|
|
|
}
|
|
|
|
}
|
2018-03-28 23:07:50 -03:00
|
|
|
|
2020-11-21 12:00:11 -04:00
|
|
|
// display dma contention statistics as text buffer for @SYS/dma.txt
|
2020-12-30 02:44:19 -04:00
|
|
|
void Shared_DMA::dma_info(ExpandingString &str)
|
2020-11-21 12:00:11 -04:00
|
|
|
{
|
|
|
|
// no buffer allocated, start counting
|
|
|
|
if (_contention_stats == nullptr) {
|
|
|
|
_contention_stats = new dma_stats[SHARED_DMA_MAX_STREAM_ID+1];
|
2021-05-31 21:45:16 -03:00
|
|
|
// return zeros on first fetch
|
2020-11-21 12:00:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// a header to allow for machine parsers to determine format
|
2020-12-30 02:44:19 -04:00
|
|
|
str.printf("DMAV1\n");
|
2020-11-21 12:00:11 -04:00
|
|
|
|
2020-12-05 15:16:27 -04:00
|
|
|
for (uint8_t i = 0; i < SHARED_DMA_MAX_STREAM_ID; i++) {
|
|
|
|
// ignore locks not in use
|
|
|
|
if (_contention_stats[i].contended_locks == 0
|
|
|
|
&& _contention_stats[i].uncontended_locks == 0
|
|
|
|
&& _contention_stats[i].transactions == 0) {
|
2020-11-21 12:00:11 -04:00
|
|
|
continue;
|
|
|
|
}
|
2020-12-05 15:16:27 -04:00
|
|
|
#if STM32_DMA_ADVANCED
|
|
|
|
#define STREAM_MUX 8
|
|
|
|
#define STREAM_OFFSET 0
|
|
|
|
#else
|
|
|
|
#define STREAM_MUX 7
|
|
|
|
#define STREAM_OFFSET 1
|
|
|
|
#endif
|
|
|
|
const char* fmt = "DMA=%1u:%1u TX=%8u ULCK=%8u CLCK=%8u CONT=%4.1f%%\n";
|
2020-11-21 12:00:11 -04:00
|
|
|
float cond_per = 100.0f * float(_contention_stats[i].contended_locks)
|
|
|
|
/ (1 + _contention_stats[i].contended_locks + _contention_stats[i].uncontended_locks);
|
2020-12-05 15:16:27 -04:00
|
|
|
str.printf(fmt, i / STREAM_MUX + 1, i % STREAM_MUX + STREAM_OFFSET, _contention_stats[i].transactions,
|
|
|
|
_contention_stats[i].uncontended_locks, _contention_stats[i].contended_locks, cond_per);
|
|
|
|
|
2020-11-21 12:00:11 -04:00
|
|
|
_contention_stats[i].contended_locks = 0;
|
|
|
|
_contention_stats[i].uncontended_locks = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 22:45:30 -03:00
|
|
|
#endif // CH_CFG_USE_SEMAPHORES
|
2021-02-23 00:07:53 -04:00
|
|
|
|