Skip to content

Commit 497c5f6

Browse files
committed
i.MX RT1050: Reactivate data cache
Since commit 12c6b1b, the i.MX RT1050 has effectively had its data cache disabled, as the SDRAM was marked Shareable; for the Cortex-M7, shareable memory is not cached. This was done to make the Ethernet driver work without any cache maintenance code. This commit adds cache maintenance and memory barriers to the Ethernet driver, and removes the Shareable attribute from the SDRAM, so the data cache is used again. Cache code in the base fsl_enet.c driver has not been activated - the bulk of it is in higher-level Read and Write calls that we're not using, and there is one flawed invalidate in its initialisation. Instead imx_emac.cpp takes full cache responsibility. This commit also marks the SDRAM as read/write-allocate. As the Cortex-M7 has its "Dynamic read allocate mode" to automatically switch back to read-allocate in cases where write allocate is working poorly (eg large memset), this should result in a performance boost with no downside. Activating write-allocate is also an attempt to provoke any flaws in cache maintenance - the Ethernet transmit buffers for example will be more likely to have a little data in the cache that needs cleaning.
1 parent 65942ba commit 497c5f6

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

features/netsocket/emac-drivers/TARGET_NXP_EMAC/TARGET_IMX/imx_emac.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ static void update_read_buffer(uint8_t *buf)
9999
g_handle.rxBdCurrent[0]->buffer = buf;
100100
}
101101

102+
/* Ensures buffer pointer is written before control. */
103+
__DMB();
104+
102105
/* Clears status. */
103106
g_handle.rxBdCurrent[0]->control &= ENET_BUFFDESCRIPTOR_RX_WRAP_MASK;
104107

@@ -112,6 +115,9 @@ static void update_read_buffer(uint8_t *buf)
112115
g_handle.rxBdCurrent[0]++;
113116
}
114117

118+
/* Ensures descriptor is written before kicking hardware. */
119+
__DSB();
120+
115121
/* Actives the receive buffer descriptor. */
116122
ENET->RDAR = ENET_RDAR_RDAR_MASK;
117123
}
@@ -195,6 +201,7 @@ bool Kinetis_EMAC::low_level_init_successful()
195201
return false;
196202

197203
rx_ptr[i] = (uint32_t*)memory_manager->get_ptr(rx_buff[i]);
204+
SCB_InvalidateDCache_by_Addr(rx_ptr[i], ENET_ALIGN(ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT));
198205
}
199206

200207
tx_consume_index = tx_produce_index = 0;
@@ -277,6 +284,7 @@ emac_mem_buf_t *Kinetis_EMAC::low_level_input(int idx)
277284

278285
/* Zero-copy */
279286
p = rx_buff[idx];
287+
SCB_InvalidateDCache_by_Addr(rx_ptr[idx], length);
280288
memory_manager->set_len(p, length);
281289

282290
/* Attempt to queue new buffer */
@@ -295,6 +303,7 @@ emac_mem_buf_t *Kinetis_EMAC::low_level_input(int idx)
295303

296304
rx_buff[idx] = temp_rxbuf;
297305
rx_ptr[idx] = (uint32_t*)memory_manager->get_ptr(rx_buff[idx]);
306+
SCB_InvalidateDCache_by_Addr(rx_ptr[idx], ENET_ALIGN(ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT));
298307

299308
update_read_buffer((uint8_t*)rx_ptr[idx]);
300309
}
@@ -399,6 +408,8 @@ bool Kinetis_EMAC::link_out(emac_mem_buf_t *buf)
399408
buf = copy_buf;
400409
}
401410

411+
SCB_CleanDCache_by_Addr(static_cast<uint32_t *>(memory_manager->get_ptr(buf), memory_manager->get_len(buf));
412+
402413
/* Check if a descriptor is available for the transfer (wait 10ms before dropping the buffer) */
403414
if (xTXDCountSem.wait(10) == 0) {
404415
memory_manager->free(buf);
@@ -415,6 +426,8 @@ bool Kinetis_EMAC::link_out(emac_mem_buf_t *buf)
415426
/* Setup transfers */
416427
g_handle.txBdCurrent[0]->buffer = static_cast<uint8_t *>(memory_manager->get_ptr(buf));
417428
g_handle.txBdCurrent[0]->length = memory_manager->get_len(buf);
429+
/* Ensures buffer and length is written before control. */
430+
__DMB();
418431
g_handle.txBdCurrent[0]->control |= (ENET_BUFFDESCRIPTOR_TX_READY_MASK | ENET_BUFFDESCRIPTOR_TX_LAST_MASK);
419432

420433
/* Increase the buffer descriptor address. */
@@ -424,6 +437,9 @@ bool Kinetis_EMAC::link_out(emac_mem_buf_t *buf)
424437
g_handle.txBdCurrent[0]++;
425438
}
426439

440+
/* Ensures descriptor is written before kicking hardware. */
441+
__DSB();
442+
427443
/* Active the transmit buffer descriptor. */
428444
ENET->TDAR = ENET_TDAR_TDAR_MASK;
429445

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_MIMXRT1050/TARGET_EVK/mbed_overrides.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ void BOARD_ConfigMPU(void)
102102
* this suggestion is referred from chapter 2.2.1 Memory regions,
103103
* types and attributes in Cortex-M7 Devices, Generic User Guide */
104104
#if defined(SDRAM_IS_SHAREABLE)
105-
/* Region 7 setting: Memory with Normal type, not shareable, outer/inner write back */
105+
/* Region 7 setting: Memory with Normal type, shareable, outer/inner write back, write/read allocate */
106106
MPU->RBAR = ARM_MPU_RBAR(7, 0x80000000U);
107-
MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 1, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB);
107+
MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 1, 1, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB);
108108
#else
109-
/* Region 7 setting: Memory with Normal type, not shareable, outer/inner write back */
109+
/* Region 7 setting: Memory with Normal type, not shareable, outer/inner write back, write/read allocate */
110110
MPU->RBAR = ARM_MPU_RBAR(7, 0x80000000U);
111-
MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB);
111+
MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 1, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB);
112112
#endif
113113

114114
/* Region 8 setting, set last 2MB of SDRAM can't be accessed by cache, glocal variables which are not expected to be

targets/targets.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,6 @@
19161916
"XIP_BOOT_HEADER_DCD_ENABLE=1",
19171917
"SKIP_SYSCLK_INIT",
19181918
"FSL_FEATURE_PHYKSZ8081_USE_RMII50M_MODE",
1919-
"SDRAM_IS_SHAREABLE",
19201919
"MBED_MPU_CUSTOM"
19211920
],
19221921
"inherits": ["Target"],

0 commit comments

Comments
 (0)