MMC SPI Cache Friendlyness: use spiPolledExchange

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
mck1117
Posts: 28
Joined: Wed Nov 11, 2020 6:41 am
Has thanked: 1 time
Been thanked: 5 times

MMC SPI Cache Friendlyness: use spiPolledExchange

Postby mck1117 » Mon Feb 15, 2021 10:07 am

There is a limitation in the current MMC SPI driver that makes it very hard to use on an STM32F7/H7 (or any other MCU with a data cache). Nearly all calls to the MMC driver use small buffers (card commands, responses, etc) located on the stack. Calls to spiReceive/spiSend then perform DMA operations to/from these buffers, which means that the calling stack must be in a non-cached region. This is doable with a user thread performing operations, as you can put the thread somewhere with caching disabled (but isn't great for performance). However, other threads, like the one in the community USB mass storage driver don't have that luxury.

The other cost of doing this is performance - tiny DMA transfers are hugely expensive as they require simply running more code, two context switches, and an interrupt. Consider mmcSequentialWrite: it sends 2 bytes, then the 512 byte block, then ignores 2 bytes, and receives a single byte. Then the call to wait() will perform single byte reads until it gets back FF, for a total of 8 extra context switches (minimum!). On a fast card, that extra overhead is a considerable source of latency.

Is it reasonable to add a halconf.h option like MMC_USE_POLLED_EXCHANGE that replaces everything but the actual 512 byte data block read/write calls with spiPolledExchange instead of spiSend/spiReceive? The actual read/write buffers are unaffected as they're a separate buffer passed in which can be independently marked to not cache.

I went ahead and implemented it, and it appears to work well. As an added bonus, it provides a performance boost even on an STM32F407. While testing, USB MSD sequential reads were sped up from ~480KB/s to ~550KB/s just with this change (full speed USB, so it's slow either way).

Here is the diff to implement this change: https://github.com/rusefi/ChibiOS/pull/11 (this is for rusEFI's ChibiOS fork, but I'm happy to have this change pulled in to mainline ChibiOS too)

PS: is there a "how to contribute to ChibiOS" page somewhere? The only one I could find is from 2012 and is stale.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: MMC SPI Cache Friendlyness: use spiPolledExchange

Postby Giovanni » Mon Feb 15, 2021 10:39 am

Hi,

The driver is very old and there were no cache issues at that time.

You need just to post in "small change request" your proposal.

Giovanni

mck1117
Posts: 28
Joined: Wed Nov 11, 2020 6:41 am
Has thanked: 1 time
Been thanked: 5 times

Re: MMC SPI Cache Friendlyness: use spiPolledExchange

Postby mck1117 » Mon Feb 15, 2021 9:21 pm

Can we move this thread over there...?


Return to “Small Change Requests”

Who is online

Users browsing this forum: No registered users and 2 guests