Repository navigation
# SD card driver: do not hold interrupts off while the card is busy - #1066
Conversation
SdCard::read() and write() ran each sector's whole SPI exchange (command, response, data and the wait for the card) inside ENTER_SAFE_SECTION. After a write the card stays busy while it programs the block, up to 250 ms by the spec. On a C64U, timing the interrupts-off sections with a free-running counter showed 78 ms at SdCard::write, and over a thousand sections of 2 ms or more since boot. The tick and the RMII, UART and USB interrupts waited for the card. The section only kept other tasks off the SPI port. The SPI master in this tree (fpga/io/spi, used by ultimate_logic_32) clocks each byte on its own and raises SS between bytes, so the exchange needs no timing from the CPU, and no interrupt handler uses the port. The C64U's gateware is not in this tree; its SD port is assumed to work the same way. That still has to be confirmed on a C64U. The card's mutex was created in the constructor but never taken. It now does the job with interrupts on: around each sector in read() and write(), around the CID and CSD reads, and around init(), which the card manager runs on its own task when a card is inserted. sdio.cc is unchanged, and builds without OS are unchanged. The waits for the card (busy after a write, the data token before a read) still poll, now with interrupts on. They do not yield: with the tick running, tasks of the same priority already share the CPU by time slicing, and higher priorities preempt. A yield every few polls was tried and dropped. Against a busy task of the same priority it handed over the rest of every 5 ms tick, and a host model of the driver (0.4 us per SPI byte) put reads and writes at 1 to 4 % of the old rate. Without it they keep 97 to 99 % of the old rate on a fast card and 74 to 91 % on slower ones. Risk: this is the SD block driver. Anything that touches the SD port without taking the mutex can now run in the middle of a transfer. sdio_sense() (card detect, a register read with no SPI exchange) is the only such caller left. A transfer can now be preempted part-way through a sector, which costs some throughput when another task of the same priority is busy (the figures above). The new host test target/pc/linux/sdcard runs sd_card.cc and sdio.cc against a simulated slow card, with the mutex simulated as a non-recursive FreeRTOS mutex. It covers init(), CID/CSD, read and write, a contended mutex and the error paths, and checks that the waits never give up the CPU (taskYIELD, portYIELD, vTaskDelay or vTaskDelayUntil). SD_MODEL=1 prints the throughput model. It is part of make host_tests and builds with gcc and clang. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@JC-000 thanks, this is a clear improvement. The mutex covers the same regions that were interrupts-off, every exit path releases it, and To get it over the line:
Happy to merge after that. |
chrisgleissner
left a comment
There was a problem hiding this comment.
Thanks @JC-000. Requesting changes for the points in my comment above; I will merge once they are addressed.
|
@JC-000 thank you again for the SD card work; your measurements of the long interrupt blackout are what made this worth fixing. I had said I would finish this PR myself. I am handing it back to you, so please treat it as yours again. This is where it stands. Done (pushed to your branch):
Outstanding before this can be merged:
Nothing else is asked of you on this PR. |
|
Hardware run: C64 Ultimate with an SD card, RAM loads (cold), one lock hold, 2026-11-10
Count of interrupts-off sections of 2 ms or more during the writes: 18386 on master, 3 with the PR. In the PR run the only U2+ and U2+L: Not run. We have neither device; they were only compiled by CI. Written with Claude Opus and Claude Sonnet in Claude Code. |
Makefile host_tests: keep both the sdcard and the new socketdma test lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
chrisgleissner
left a comment
There was a problem hiding this comment.
Thanks. The hardware run on the C64 Ultimate covers what I asked for: SdCard::write no longer holds interrupts off (149 ms on master), sections of 2 ms or more drop from 18386 to 3, all checksums match and the rate is not lower. The comment wording is fixed and the U2+ and U2+L status is stated (compiled by CI only). Approving.
|
I'm assigning this to myself. I'm investigating whether it is merge ready by running the standard E2E profile locally on an Ultimate 64 Elite and an Ultimate II+L, with this PR combined with the other approved bug fixes, and comparing against master. I'll report the result here. |
|
Test result for the batch this PR is part of (#1043, #1064, #1065, #1066, #1068, #1070). Firmware: upstream master
|
# Conflicts: # Makefile
Fixes #1055
Problem
Every SD card sector transfer runs with all interrupts masked, including the poll loops that wait for the card to finish programming. While the card is busy the system tick, networking and USB interrupts are all blacked out. Measured on the C64 Ultimate: an interrupts-off stretch of ~119 ms during SD writes. The poll waits are bounded by the SD spec worst case (up to ~100 ms for a data token, ~250 ms for card-busy), so the blackout can in principle be longer still.
Cause, on current master:
SdCard::read/writewrap each sector — command, R1, data, and the card-busy / data-token poll waits — inENTER_SAFE_SECTION(software/io/sd_card/sd_card.cc:257-276and:334-346). The poll waits live insdio.cc. The section only needs to give one task exclusive use of the SPI port; the SPI master raises SS after each byte and needs no CPU timing, no ISR uses SDIO, and no DMA is involved.What changed and why
Replace the interrupt mask with the driver's existing per-instance mutex, taken around each sector, around CID/CSD, and around
init(). Mutual exclusion between tasks is preserved; interrupts stay on while the card programs. Non-OS builds (whereENTER_SAFE_SECTIONwas already empty) are unchanged.Products. builds for the C64 Ultimate and the Ultimate-II cartridges (U2+, U2+L); the Ultimate 64 Elite (Nios) does not compile
sd_card.cc. Tested on the C64 Ultimate only.How I tested it
Host: macOS, with the device-free tests run in an Ubuntu 24.04 container (g++ 13.3). The hardware runs used the test harness from the same machine.
Host tests (device-free, run before and after the change):
make -B -C target/pc/linux/sdcard testsd_card.ccfrom master: 10 fail (init, CID/CSD and write run with interrupts off and without the mutex)Hardware.
RED = master, GREEN = fix, from the runtime interrupts-off profiler.
SdCard::write, ~119 msHost suite
target/pc/linux/sdcard(gcc; the Makefile hard-codesCXX=g++, and it also builds under clang with aCXX=clang++override): 12/12 with the fix; 10 fail on master; 13 mutants killed.Standard profile (
./run-tests --profile standard), red = master, green = a build carrying this landing set of fixes (this change included), report frompython3 tools/e2e_report.py:iec-partition-config: it stops at a key-send step because the upstream test's REST backend has noF2key alias (rest_backend.py:556), a test-side limitation, not an app difference.iec-partition-config, here at a "menu screen unavailable" step that also fails on master, so it is not from these changes.Soaks pass on both devices: mount-cache-leak, listener-soak, network-connection.
Hardware: C64 Ultimate with an SD card
SdCard::read and SdCard::write ran every sector, including the wait for the card to finish programming, inside a critical
section that keeps interrupts off. On a C64 Ultimate with an SD card, a counter build of master showed SdCard::write holding
interrupts off for up to 149 ms (and SdCard::read 9 ms) while a large file was written, with 18386 such sections of 2 ms or
more across the run. With this PR, the same build and the same transfers show no interrupts-off section of 2 ms or more at
any SdCard site. The driver takes its own mutex per sector instead, so the port is still used by one task at a time. Eight
megabyte writes with a second client reading the card at the same time kept matching checksums, and the rate did not drop
(271 vs 250 KB/s alone, 204 vs 176 KB/s with the second client).
The results table is in the comment below.
Untested
CXX=g++; aCXX=clang++override works but is not what the default run uses.Written with Claude Opus 5.5 in Claude Code.