Peios Learn
Documentation

Peios

Peios Peios Kernel Universal Directory Project

Developer Tools

Trail Acta Provium clackatoo CAF GCSL Pekit

Applications

Framewright

Standards and Conventions

Standards and Conventions Peios Specifications
Peios Kernel

MMC and SD

Single-page view · as markdown

MMC/SD/SDIO card support

Peios Kernel / Hardware and drivers / Driver development / MMC and SD

  • SD and MMC Block Device Attributes
  • SD and MMC Device Partitions
  • MMC Asynchronous Request
  • MMC Test Framework
  • MMC tools introduction

MMC Asynchronous Request

Peios Kernel / Hardware and drivers / Driver development / MMC and SD

Rationale #

How significant is the cache maintenance overhead?

It depends. Fast eMMC and multiple cache levels with speculative cache pre-fetch makes the cache overhead relatively significant. If the DMA preparations for the next request are done in parallel with the current transfer, the DMA preparation overhead would not affect the MMC performance.

The intention of non-blocking (asynchronous) MMC requests is to minimize the time between when an MMC request ends and another MMC request begins.

Using mmc_wait_for_req(), the MMC controller is idle while dma_map_sg and dma_unmap_sg are processing. Using non-blocking MMC requests makes it possible to prepare the caches for next job in parallel with an active MMC request.

MMC block driver #

The mmc_blk_issue_rw_rq() in the MMC block driver is made non-blocking.

The increase in throughput is proportional to the time it takes to prepare (major part of preparations are dma_map_sg() and dma_unmap_sg()) a request and how fast the memory is. The faster the MMC/SD is the more significant the prepare request time becomes. Roughly the expected performance gain is 5% for large writes and 10% on large reads on a L2 cache platform. In power save mode, when clocks run on a lower frequency, the DMA preparation may cost even more. As long as these slower preparations are run in parallel with the transfer performance won't be affected.

Details on measurements from IOZone and mmc_test #

https://wiki.linaro.org/WorkingGroups/Kernel/Specs/StoragePerfMMC-async-req

MMC core API extension #

There is one new public function mmc_start_req().

It starts a new MMC command request for a host. The function isn't truly non-blocking. If there is an ongoing async request it waits for completion of that request and starts the new one and returns. It doesn't wait for the new request to complete. If there is no ongoing request it starts the new request and returns immediately.

MMC host extensions #

There are two optional members in the mmc_host_ops -- pre_req() and post_req() -- that the host driver may implement in order to move work to before and after the actual mmc_host_ops.request() function is called.

In the DMA case pre_req() may do dma_map_sg() and prepare the DMA descriptor, and post_req() runs the dma_unmap_sg().

Optimize for the first request #

The first request in a series of requests can't be prepared in parallel with the previous transfer, since there is no previous request.

The argument is_first_req in pre_req() indicates that there is no previous request. The host driver may optimize for this scenario to minimize the performance loss. A way to optimize for this is to split the current request in two chunks, prepare the first chunk and start the request, and finally prepare the second chunk and start the transfer.

Pseudocode to handle is_first_req scenario with minimal prepare overhead:

if (is_first_req && req->size > threshold)
   /* start MMC transfer for the complete transfer size */
   mmc_start_command(MMC_CMD_TRANSFER_FULL_SIZE);

   /*
    * Begin to prepare DMA while cmd is being processed by MMC.
    * The first chunk of the request should take the same time
    * to prepare as the "MMC process command time".
    * If prepare time exceeds MMC cmd time
    * the transfer is delayed, guesstimate max 4k as first chunk size.
    */
    prepare_1st_chunk_for_dma(req);
    /* flush pending desc to the DMAC (dmaengine.h) */
    dma_issue_pending(req->dma_desc);

    prepare_2nd_chunk_for_dma(req);
    /*
     * The second issue_pending should be called before MMC runs out
     * of the first chunk. If the MMC runs out of the first data chunk
     * before this call, the transfer is delayed.
     */
    dma_issue_pending(req->dma_desc);

SD and MMC Block Device Attributes

Peios Kernel / Hardware and drivers / Driver development / MMC and SD

These attributes are defined for the block devices associated with the SD or MMC device.

The following attributes are read/write.

force_ro

Enforce read-only access even if write protect switch is off.

SD and MMC Device Attributes #

All attributes are read-only.

cid

Card Identification Register

csd

Card Specific Data Register

scr

SD Card Configuration Register (SD only)

date

Manufacturing Date (from CID Register)

fwrev

Firmware/Product Revision (from CID Register) (SD and MMCv1 only)

hwrev

Hardware/Product Revision (from CID Register) (SD and MMCv1 only)

manfid

Manufacturer ID (from CID Register)

name

Product Name (from CID Register)

oemid

OEM/Application ID (from CID Register)

prv

Product Revision (from CID Register) (SD and MMCv4 only)

serial

Product Serial Number (from CID Register)

erase_size

Erase group size

preferred_erase_size

Preferred erase size

raw_rpmb_size_mult

RPMB partition size

rel_sectors

Reliable write sector count

ocr

Operation Conditions Register

dsr

Driver Stage Register

cmdq_en

Command Queue enabled:

1 => enabled, 0 => not enabled

Note on Erase Size and Preferred Erase Size:

"erase_size" is the minimum size, in bytes, of an erase operation. For MMC, "erase_size" is the erase group size reported by the card. Note that "erase_size" does not apply to trim or secure trim operations where the minimum size is always one 512 byte sector. For SD, "erase_size" is 512 if the card is block-addressed, 0 otherwise.

SD/MMC cards can erase an arbitrarily large area up to and including the whole card. When erasing a large area it may be desirable to do it in smaller chunks for three reasons:

  1. A single erase command will make all other I/O on the card wait. This is not a problem if the whole card is being erased, but erasing one partition will make I/O for another partition on the same card wait for the duration of the erase - which could be a several minutes.

  2. To be able to inform the user of erase progress.

  3. The erase timeout becomes too large to be very useful. Because the erase timeout contains a margin which is multiplied by the size of the erase area, the value can end up being several minutes for large areas.

"erase_size" is not the most efficient unit to erase (especially for SD where it is just one sector), hence "preferred_erase_size" provides a good chunk size for erasing large areas.

For MMC, "preferred_erase_size" is the high-capacity erase size if a card specifies one, otherwise it is based on the capacity of the card.

For SD, "preferred_erase_size" is the allocation unit size specified by the card.

"preferred_erase_size" is in bytes.

Note on raw_rpmb_size_mult:

"raw_rpmb_size_mult" is a multiple of 128kB block.

RPMB size in byte is calculated by using the following equation:

RPMB partition size = 128kB x raw_rpmb_size_mult

SD and MMC Device Partitions

Peios Kernel / Hardware and drivers / Driver development / MMC and SD

Device partitions are additional logical block devices present on the SD/MMC device.

As of this writing, MMC boot partitions as supported and exposed as /dev/mmcblkXboot0 and /dev/mmcblkXboot1, where X is the index of the parent /dev/mmcblkX.

MMC Boot Partitions #

Read and write access is provided to the two MMC boot partitions. Due to the sensitive nature of the boot partition contents, which often store a bootloader or bootloader configuration tables crucial to booting the platform, write access is disabled by default to reduce the chance of accidental bricking.

To enable write access to /dev/mmcblkXbootY, disable the forced read-only access with:

echo 0 > /sys/block/mmcblkXbootY/force_ro

To re-enable read-only access:

echo 1 > /sys/block/mmcblkXbootY/force_ro

The boot partitions can also be locked read only until the next power on, with:

echo 1 > /sys/block/mmcblkXbootY/ro_lock_until_next_power_on

This is a feature of the card and not of the kernel. If the card does not support boot partition locking, the file will not exist. If the feature has been disabled on the card, the file will be read-only.

The boot partitions can also be locked permanently, but this feature is not accessible through sysfs in order to avoid accidental or malicious bricking.

MMC Test Framework

Peios Kernel / Hardware and drivers / Driver development / MMC and SD

Overview #

The mmc_test framework is designed to test the performance and reliability of host controller drivers and all devices handled by the MMC subsystem. This includes not only MMC devices but also SD cards and other devices supported by the subsystem.

The framework provides a variety of tests to evaluate different aspects of the host controller and device interactions, such as read and write performance, data integrity, and error handling. These tests help ensure that the host controller drivers and devices operate correctly under various conditions.

The mmc_test framework is particularly useful for:

  • Verifying the functionality and performance of MMC and SD host controller drivers.

  • Ensuring compatibility and reliability of MMC and SD devices.

  • Identifying and diagnosing issues in the MMC subsystem.

The results of the tests are logged in the kernel log, providing detailed information about the test outcomes and any encountered issues.

Note: whatever is on your card will be overwritten by these tests.

Initialization #

To use the mmc_test framework, follow these steps:

  1. Enable the MMC Test Framework:

    Ensure that the CONFIG_MMC_TEST kernel configuration option is enabled. This can be done by configuring the kernel:

    make menuconfig
    

    Navigate to:

    • Device Drivers --->

      • <*> MMC/SD/SDIO card support --->

        [*] MMC host test driver

    Alternatively, you can enable it directly in the kernel configuration file:

    echo "CONFIG_MMC_TEST=y" >> .config
    

    Rebuild and install the kernel if necessary.

  2. Load the MMC Test Module:

    If the mmc_test framework is built as a module, you need to load it using modprobe:

    modprobe mmc_test
    

Binding the MMC Card for Testing #

To enable MMC testing, you need to unbind the MMC card from the mmcblk driver and bind it to the mmc_test driver. This allows the mmc_test framework to take control of the MMC card for testing purposes.

  1. Identify the MMC card:

    ls /sys/bus/mmc/devices/
    

    This will list the MMC devices, such as mmc0:0001.

  2. Unbind the MMC card from the mmcblk driver:

    echo 'mmc0:0001' > /sys/bus/mmc/drivers/mmcblk/unbind
    
  3. Bind the MMC card to the mmc_test driver:

    echo 'mmc0:0001' > /sys/bus/mmc/drivers/mmc_test/bind
    

After binding, you should see a line in the kernel log indicating that the card has been claimed for testing:

mmc_test mmc0:0001: Card claimed for testing.

Usage - Debugfs Entries #

Once the mmc_test framework is enabled, you can interact with the following debugfs entries located in /sys/kernel/debug/mmc0/mmc0:0001:

  1. test:

    This file is used to run specific tests. Write the test number to this file to execute a test.

    echo <test_number> > /sys/kernel/debug/mmc0/mmc0:0001/test
    

    The test result is indicated in the kernel log info. You can view the kernel log using the dmesg command or by checking the log file in /var/log/.

    dmesg | grep mmc0
    

    Example:

    To run test number 4 (Basic read with data verification):

    echo 4 > /sys/kernel/debug/mmc0/mmc0:0001/test
    

    Check the kernel log for the result:

    dmesg | grep mmc0
    
  2. testlist:

    This file lists all available tests. You can read this file to see the list of tests and their corresponding numbers.

    cat /sys/kernel/debug/mmc0/mmc0:0001/testlist
    

    The available tests are listed in the table below:

Test

Test Name

Test Description

0

Run all tests

Runs all available tests

1

Basic write

Performs a basic write operation of a single 512-Byte block to the MMC card without data verification.

2

Basic read

Same for read

3

Basic write (with data verification)

Performs a basic write operation of a single 512-Byte block to the MMC card with data verification by reading back the written data and comparing it.

4

Basic read (with data verification)

Same for read

5

Multi-block write

Performs a multi-block write operation of 8 blocks (each 512 bytes) to the MMC card.

6

Multi-block read

Same for read

7

Power of two block writes

Performs write operations with block sizes that are powers of two, starting from 1 byte up to 256 bytes, to the MMC card.

8

Power of two block reads

Same for read

9

Weird sized block writes

Performs write operations with varying block sizes starting from 3 bytes and increasing by 7 bytes each iteration, up to 511 bytes, to the MMC card.

10

Weird sized block reads

same for read

11

Badly aligned write

Performs write operations with buffers starting at different alignments (0 to 7 bytes offset) to test how the MMC card handles unaligned data transfers.

12

Badly aligned read

same for read

13

Badly aligned multi-block write

same for multi-write

14

Badly aligned multi-block read

same for multi-read

15

Proper xfer_size at write (Start failure)

intentionally create a broken transfer by modifying the MMC request in a way that it will not perform as expected, e.g. use MMC_WRITE_BLOCK for a multi-block transfer

16

Proper xfer_size at read (Start failure)

same for read

17

Proper xfer_size at write (Midway failure)

same for 2 blocks

18

Proper xfer_size at read (Midway failure)

same for read

19

Highmem write

use a high memory page

20

Highmem read

same for read

21

Multi-block highmem write

same for multi-write

22

Multi-block highmem read

same for mult-read

23

Best-case read performance

Performs 512K sequential read (non sg)

24

Best-case write performance

same for write

25

Best-case read performance (Into scattered pages)

Same using sg

26

Best-case write performance (From scattered pages)

same for write

27

Single read performance

By transfer size

28

Single write performance

By transfer size

29

Single trim performance

By transfer size

30

Consecutive read performance

By transfer size

31

Consecutive write performance

By transfer size

32

Consecutive trim performance

By transfer size

33

Random read performance

By transfer size

34

Random write performance

By transfer size

35

Large sequential read

Into scattered pages

36

Large sequential write

From scattered pages

37

Write performance

With blocking req 4k to 4MB

38

Write performance

With non-blocking req 4k to 4MB

39

Read performance

With blocking req 4k to 4MB

40

Read performance

With non-blocking req 4k to 4MB

41

Write performance

Blocking req 1 to 512 sg elems

42

Write performance

Non-blocking req 1 to 512 sg elems

43

Read performance

Blocking req 1 to 512 sg elems

44

Read performance

Non-blocking req 1 to 512 sg elems

45

Reset test

46

Commands during read

No Set Block Count (CMD23)

47

Commands during write

No Set Block Count (CMD23)

48

Commands during read

Use Set Block Count (CMD23)

49

Commands during write

Use Set Block Count (CMD23)

50

Commands during non-blocking

Read - use Set Block Count (CMD23)

51

Commands during non-blocking

Write - use Set Block Count (CMD23)

Test Results #

The results of the tests are logged in the kernel log. Each test logs the start, end, and result of the test. The possible results are:

  • OK: The test completed successfully.

  • FAILED: The test failed.

  • UNSUPPORTED (by host): The test is unsupported by the host.

  • UNSUPPORTED (by card): The test is unsupported by the card.

  • ERROR: An error occurred during the test.

Example Kernel Log Output #

When running a test, you will see log entries similar to the following in the kernel log:

[ 1234.567890] mmc0: Starting tests of card mmc0:0001...
[ 1234.567891] mmc0: Test case 4. Basic read (with data verification)...
[ 1234.567892] mmc0: Result: OK
[ 1234.567893] mmc0: Tests completed.

In this example, test case 4 (Basic read with data verification) was executed, and the result was OK.

Contributing #

Contributions to the mmc_test framework are welcome. Please follow the standard Linux kernel contribution guidelines and submit patches to the appropriate maintainers.

Contact #

For more information or to report issues, please contact the MMC subsystem maintainers.

MMC tools introduction

Peios Kernel / Hardware and drivers / Driver development / MMC and SD

There is one MMC test tools called mmc-utils, which is maintained by Ulf Hansson, you can find it at the below public git repository:

https://git.kernel.org/pub/scm/utils/mmc/mmc-utils.git

Functions #

The mmc-utils tools can do the following:

  • Print and parse extcsd data.

  • Determine the eMMC writeprotect status.

  • Set the eMMC writeprotect status.

  • Set the eMMC data sector size to 4KB by disabling emulation.

  • Create general purpose partition.

  • Enable the enhanced user area.

  • Enable write reliability per partition.

  • Print the response to STATUS_SEND (CMD13).

  • Enable the boot partition.

  • Set Boot Bus Conditions.

  • Enable the eMMC BKOPS feature.

  • Permanently enable the eMMC H/W Reset feature.

  • Permanently disable the eMMC H/W Reset feature.

  • Send Sanitize command.

  • Program authentication key for the device.

  • Counter value for the rpmb device will be read to stdout.

  • Read from rpmb device to output.

  • Write to rpmb device from data file.

  • Enable the eMMC cache feature.

  • Disable the eMMC cache feature.

  • Print and parse CID data.

  • Print and parse CSD data.

  • Print and parse SCR data.

Peios Learn — documentation for Peios software.

Built with Trail.