Skip to content

Use memory fence when disabling cache to avoid -O2 problems #7398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2022

Conversation

dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Dec 30, 2022

Fixes #7279.

Disabling the cache on SAMx5x caused problems when compiling with -O2, apparently due to inlining the microcontroller cache control routines.. Access to the external flash chip did not work.

Changes made:

  • Add memory fences when disabling cache samd-peripherals#43 adds a memory fence before and after disabling the cache.
  • external_flash.c tries a little harder to make sure the flash chip is ready, by retrying fetching the JEDEC ID if necessary. Without this, if the chip was not actually ready, the ID would not be read, and flash_device ended up being NULL.
  • A check was added to make sure flash_device was not NULL when asking for the device size. Previously, this could have returned a garbage value. I saw this cause incorrect data to be returned for MSC queries.
  • A memory fence was also added for nRF, which potentially has similar problems, though they don't seem to be as obvious.

Right now we don't actually compile any SAMx5x builds with -O2, so this change doesn't fix any presentlybroken builds. The nRF builds are -O2 -fno-inline-functions, which may not provoke the optimization problems seen in SAMx5x. I did not remove -fno-inline-functions in nRF, because it makes the -O2 builds smaller. (A while ago, -fno-inline-functions was also used with -O2 in SAMx5x builds, before they got too big, and started needing -Os.)

Cortex-M0 doesn't have a cache, so this is not a problem at all for M0 builds. I looked at cache routines in broadcom, and they include fencing, though not as much. (Maybe worth exploring later.)

@dhalbert dhalbert requested review from tannewt and jepler December 30, 2022 01:01
Copy link

@jepler jepler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good. didn't test.

@dhalbert dhalbert merged commit e8ee4b2 into adafruit:main Dec 30, 2022
@dhalbert dhalbert deleted the cache-memory-fence branch December 30, 2022 05:09
@tannewt
Copy link
Member

tannewt commented Jan 3, 2023

Thanks for this! I've seen something very similar on the nrf bangle.js 2. Will need to update and test it when I'm back to my proper desk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

-O2 optimization is broken on samd5x/e5x
3 participants