Skip to content

Commit

Permalink
tl321x_retention: add config for packet buffer optimization
Browse files Browse the repository at this point in the history
optimize the packet buffer to save iram space

Signed-off-by: Liang Zhong <[email protected]>
  • Loading branch information
liang4804 committed Dec 17, 2024
1 parent 3e36bd0 commit 8b70ef9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
12 changes: 11 additions & 1 deletion config/telink/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ config HEAP_MEM_POOL_SIZE

# need to enlarge to 21000 , to pass TC_RR_1.1
config COMMON_LIBC_MALLOC_ARENA_SIZE
default 21000 if SOC_RISCV_TELINK_TL321X || SOC_SERIES_RISCV_TELINK_B9X_RETENTION
default 12288 if SOC_RISCV_TELINK_TL321X || SOC_SERIES_RISCV_TELINK_B9X_RETENTION
default 12288

config NET_IPV6_MLD
Expand Down Expand Up @@ -329,6 +329,16 @@ config OPENTHREAD_CSMABACKOFF_OPTIMIZATION
default n if SOC_SERIES_RISCV_TELINK_TLX_RETENTION || SOC_SERIES_RISCV_TELINK_B9X_RETENTION
default n

config ZEPHYR_NETBUFFER_OPTIMIZATION
bool "move the net buffer from iram to dram"
default y if SOC_SERIES_RISCV_TELINK_TLX_RETENTION || SOC_SERIES_RISCV_TELINK_B9X_RETENTION
default n

config CHIP_PACKETBUFFER_OPTIMIZATION
bool "move the packet buffer from iram to dram"
default y if SOC_SERIES_RISCV_TELINK_TLX_RETENTION || SOC_SERIES_RISCV_TELINK_B9X_RETENTION
default n

config OPENTHREAD_THREAD_STACK_SIZE
default 2400 if PM || SOC_RISCV_TELINK_TL321X

Expand Down
7 changes: 6 additions & 1 deletion src/platform/telink/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@

extern "C" {
extern __attribute__((noinline)) void telink_bt_blc_mac_init(uint8_t * bt_mac);
#if CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION
extern char packetBufferBLEFlag;
#endif /* CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */
}

#if defined(CONFIG_PM) && !defined(CONFIG_CHIP_ENABLE_PM_DURING_BLE)
Expand Down Expand Up @@ -951,7 +954,9 @@ void BLEManagerImpl::SwitchToIeee802154(void)
// Deinit BLE
bt_disable();
mBLERadioInitialized = false;

#if CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION
packetBufferBLEFlag = 1;
#endif /* CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */
#if defined(CONFIG_PM) && !defined(CONFIG_CHIP_ENABLE_PM_DURING_BLE)
pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
#endif
Expand Down
9 changes: 9 additions & 0 deletions src/platform/telink/SystemPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ struct ChipDeviceEvent;
#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS

#if defined(CONFIG_SOC_RISCV_TELINK_TL321X) || defined(CONFIG_SOC_SERIES_RISCV_TELINK_B9X_RETENTION)
#if CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION
#define CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_HEAP 0
#define CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_POOL 1
#else /* !CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */
#define CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_HEAP 1
#define CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_POOL 0
#endif /* CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */
#define CHIP_SYSTEM_CONFIG_POOL_USE_HEAP 1
#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_CAPACITY_MAX 1280
#endif
Expand All @@ -56,7 +61,11 @@ struct ChipDeviceEvent;

// Reduce packet buffer pool size (default 15) to reduce ram consumption
#if defined CONFIG_PM || defined CONFIG_SOC_RISCV_TELINK_TL321X
#if CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION
#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE 6
#else /* !CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */
#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE 0
#endif /* CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */
#else
#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE 8
#endif
Expand Down
52 changes: 52 additions & 0 deletions src/system/SystemPacketBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
#include <stdlib.h>
#include <string.h>
#include <utility>
#if CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION
extern "C"
{
char packetBufferBLEFlag=0;
char packetBufferThreadFlag=0;
extern unsigned int _RAMCODE_BLE_VMA_START;
extern unsigned int _RAMCODE_BLE_LMA_START;

}
#endif /* CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */


#if CHIP_SYSTEM_CONFIG_USE_LWIP
#include <lwip/mem.h>
Expand All @@ -66,7 +77,12 @@ namespace System {
// Pool allocation for PacketBuffer objects.
//

#if CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION
PacketBuffer::BufferPoolElement * PacketBuffer::sFreeRetentionList;
__attribute__((section(".bss"))) PacketBuffer::BufferPoolElement PacketBuffer::sBufferPool[CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE];
#else /* !CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */
PacketBuffer::BufferPoolElement PacketBuffer::sBufferPool[CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE];
#endif /* CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */

PacketBuffer * PacketBuffer::sFreeList = PacketBuffer::BuildFreeList();

Expand Down Expand Up @@ -104,6 +120,27 @@ PacketBuffer * PacketBuffer::BuildFreeList()
return static_cast<PacketBuffer *>(lHead);
}

#if CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION
PacketBuffer * PacketBuffer::BuildRetentionFreeList()
{
pbuf * lHead = nullptr;

for (int i = 0; i < CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE; i++)
{
pbuf * lCursor = &sFreeRetentionList[i].Header;
lCursor->next = lHead;
lCursor->ref = 0;
lHead = lCursor;
}

#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
Mutex::Init(sBufferPoolMutex);
#endif // !CHIP_SYSTEM_CONFIG_NO_LOCKING

return static_cast<PacketBuffer *>(lHead);
}
#endif /* CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */

#elif CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_HEAP
//
// Heap allocation for PacketBuffer objects.
Expand Down Expand Up @@ -510,6 +547,21 @@ void PacketBuffer::AddRef()

PacketBufferHandle PacketBufferHandle::New(size_t aAvailableSize, uint16_t aReservedSize)
{
#if CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION
if(packetBufferBLEFlag==1)
{
packetBufferBLEFlag = 0;
packetBufferThreadFlag = 2;
PacketBuffer::sFreeRetentionList = (PacketBuffer::BufferPoolElement *)&_RAMCODE_BLE_VMA_START;
PacketBuffer::sFreeList = PacketBuffer::BuildRetentionFreeList();
}
else if(packetBufferThreadFlag==1)
{
packetBufferThreadFlag = 2;
PacketBuffer::sFreeRetentionList = (PacketBuffer::BufferPoolElement *)&_RAMCODE_BLE_VMA_START;
PacketBuffer::sFreeList = PacketBuffer::BuildRetentionFreeList();
}
#endif /* CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */
// Sanity check for kStructureSize to ensure that it matches the PacketBuffer size.
static_assert(PacketBuffer::kStructureSize == sizeof(PacketBuffer), "PacketBuffer size mismatch");
// Setting a static upper bound on kStructureSize to ensure the summation of all the sizes does not overflow.
Expand Down
4 changes: 4 additions & 0 deletions src/system/SystemPacketBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ class DLL_EXPORT PacketBuffer : private pbuf
static BufferPoolElement sBufferPool[CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE];
static PacketBuffer * sFreeList;
static PacketBuffer * BuildFreeList();
#if CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION
static BufferPoolElement * sFreeRetentionList;
static PacketBuffer * BuildRetentionFreeList();
#endif /* CONFIG_CHIP_PACKETBUFFER_OPTIMIZATION */
#endif // CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_POOL || defined(DOXYGEN)

#if CHIP_SYSTEM_PACKETBUFFER_HAS_CHECK
Expand Down

0 comments on commit 8b70ef9

Please sign in to comment.