Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions drivers/spi/spi-apple-qspi-policy-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <kunit/test.h>
#include <linux/string.h>

#include "spi-apple-qspi-policy.h"

Expand Down Expand Up @@ -151,6 +152,8 @@ static void qspi_policy_status(struct kunit *test)
/* Block protect only where the board allows it. */
KUNIT_EXPECT_FALSE(test, apple_qspi_status_allows_write(0x04, 0, 0, 0));
KUNIT_EXPECT_TRUE(test, apple_qspi_status_allows_write(0x04, 0x02, 0, 0x1c));
/* The J813 W25Q64: QE and the SR3 drive strength, no block protection. */
KUNIT_EXPECT_TRUE(test, apple_qspi_status_allows_write(0x00, 0x02, 0x60, 0));
/* CMP, SUS and WPS are refused everywhere. */
KUNIT_EXPECT_FALSE(test, apple_qspi_status_allows_write(0, 0x40, 0, 0x1c));
KUNIT_EXPECT_FALSE(test, apple_qspi_status_allows_write(0, 0x80, 0, 0x1c));
Expand Down Expand Up @@ -208,6 +211,181 @@ static void qspi_policy_part_status(struct kunit *test)
KUNIT_EXPECT_FALSE(test, apple_qspi_part_status_allows_write(0xc2, 0x20, 0, 0, 0x1c));
}

static void qspi_policy_window(struct kunit *test)
{
/* The J813 and J700 partitions. */
KUNIT_EXPECT_TRUE(test, apple_qspi_window_valid(1, 2, 0, 0x700000, 0x100000));
KUNIT_EXPECT_TRUE(test, apple_qspi_window_valid(1, 2, 0, 0x24000, 0x1d0000));
/* Two nvram labels, or none (a disabled one is not counted). */
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(2, 2, 0, 0x700000, 0x100000));
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(0, 2, 0, 0x700000, 0x100000));
/* Marked read-only. */
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 1, 0x700000, 0x100000));
/* Three reg cells, or none. */
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 3, 0, 0x700000, 0x100000));
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 0, 0, 0x700000, 0x100000));
/* A misaligned start, a part of a bank, an empty partition. */
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0x700800, 0x100000));
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0x700000, 0xff000));
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0x700000, 0));
/* Past three-byte addressing. */
KUNIT_EXPECT_TRUE(test, apple_qspi_window_valid(1, 2, 0, 0xf00000, 0x100000));
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0xf10000, 0x100000));
KUNIT_EXPECT_FALSE(test, apple_qspi_window_valid(1, 2, 0, 0xfffff000, 0x10000));
}

static void qspi_policy_bank_state(struct kunit *test)
{
unsigned char h[24];

memset(h, 0xff, sizeof(h));
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), 0);
memset(h, 0, sizeof(h));
memcpy(h, "3VVN", 4);
h[5] = 0x40; /* store size 0x4000 */
h[14] = 1;
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), 1);
h[14] = 2; /* another version */
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1);
h[14] = 1;
h[5] = 0;
h[6] = 1; /* 0x10000: a full bank */
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), 1);
h[4] = 1; /* past the bank */
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1);
h[4] = 23;
h[6] = 0; /* smaller than the header */
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1);
h[4] = 24;
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), 1);
h[0] = '2';
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1);
/* Firmware, not NVRAM, and a half-erased bank. */
memset(h, 0, sizeof(h));
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1);
memset(h, 0xff, sizeof(h));
h[23] = 0xfe;
KUNIT_EXPECT_EQ(test, apple_qspi_nvram_bank_state(h), -1);
}

static void qspi_policy_parts(struct kunit *test)
{
static const unsigned char known[][3] = {
{ 0xef, 0x65, 0x17 }, { 0xc2, 0x2b, 0x27 },
{ 0xef, 0x65, 0x15 }, { 0xc8, 0x83, 0x15 },
};
static const unsigned int sizes[] = { 0x800000, 0x800000, 0x200000, 0x200000 };
static const unsigned char bp_ok[] = { 0, 0, 0x1c, 0x1c };
static const unsigned char unknown[][3] = {
{ 0xef, 0x65, 0x16 }, { 0xef, 0x40, 0x17 }, { 0xc2, 0x2b, 0x28 },
{ 0xc8, 0x60, 0x15 }, { 0, 0, 0 }, { 0xff, 0xff, 0xff },
};
const struct apple_qspi_part *part;
unsigned int i;

for (i = 0; i < ARRAY_SIZE(known); i++) {
part = apple_qspi_find_part(known[i]);
KUNIT_ASSERT_NOT_NULL(test, part);
KUNIT_EXPECT_EQ(test, part->size, sizes[i]);
KUNIT_EXPECT_EQ(test, part->bp_ok, bp_ok[i]);
}
for (i = 0; i < ARRAY_SIZE(unknown); i++)
KUNIT_EXPECT_NULL(test, apple_qspi_find_part(unknown[i]));
/* The size bounds the window: the J700's fits 2 MiB, the J813's does not. */
part = apple_qspi_find_part(known[2]);
KUNIT_EXPECT_LE(test, 0x1f4000ULL, (unsigned long long)part->size);
KUNIT_EXPECT_GT(test, 0x800000ULL, (unsigned long long)part->size);
}

static void qspi_policy_protected(struct kunit *test)
{
unsigned long long s, e;

KUNIT_EXPECT_EQ(test, apple_qspi_protected_range(0xef, 0, 0x200000, &s, &e), 0);
/* BP0 on the J700's parts: the top 64 KiB, inside the window's last bank. */
KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x04, 0x200000, &s, &e), 1);
KUNIT_EXPECT_EQ(test, s, 0x1f0000ULL);
KUNIT_EXPECT_EQ(test, e, 0x200000ULL);
KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xc8, 0x04, 0x200000, &s, &e), 1);
KUNIT_EXPECT_EQ(test, s, 0x1f0000ULL);
/* BP0+BP2: the top half; BP1+BP2: all of it. */
KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x14, 0x200000, &s, &e), 1);
KUNIT_EXPECT_EQ(test, s, 0x100000ULL);
KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x18, 0x200000, &s, &e), 1);
KUNIT_EXPECT_EQ(test, s, 0ULL);
KUNIT_EXPECT_EQ(test, e, 0x200000ULL);
/* Winbond TB: the bottom 64 KiB, below the J700's window. */
KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x24, 0x200000, &s, &e), 1);
KUNIT_EXPECT_EQ(test, s, 0ULL);
KUNIT_EXPECT_EQ(test, e, 0x10000ULL);
/* Winbond SEC: 4 KiB to 32 KiB. */
KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x44, 0x200000, &s, &e), 1);
KUNIT_EXPECT_EQ(test, s, 0x1ff000ULL);
KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xef, 0x54, 0x200000, &s, &e), 1);
KUNIT_EXPECT_EQ(test, s, 0x1f8000ULL);
/* GigaDevice SR1 bit 5 is BP3, not TB (the status policy refuses it). */
KUNIT_ASSERT_EQ(test, apple_qspi_protected_range(0xc8, 0x24, 0x200000, &s, &e), 1);
KUNIT_EXPECT_EQ(test, s, 0x1f0000ULL);
}

static void qspi_policy_unprotected_banks(struct kunit *test)
{
unsigned long long s, e;
int ret;

/* Nothing protected inside the window: all of it. */
ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0, 0x10000, &s, &e);
KUNIT_EXPECT_EQ(test, ret, 1);
KUNIT_EXPECT_EQ(test, s, 0x24000ULL);
KUNIT_EXPECT_EQ(test, e, 0x1f4000ULL);
ret = apple_qspi_unprotected_banks(0x700000, 0x800000, 0, 0x700000, &s, &e);
KUNIT_EXPECT_EQ(test, ret, 1);
KUNIT_EXPECT_EQ(test, s, 0x700000ULL);
/* BP0 on the J700: its last bank [0x1e4000, 0x1f4000) is a quarter covered, and dropped. */
ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0x1f0000, 0x200000, &s, &e);
KUNIT_EXPECT_EQ(test, ret, 1);
KUNIT_EXPECT_EQ(test, s, 0x24000ULL);
KUNIT_EXPECT_EQ(test, e, 0x1e4000ULL);
/* TB with BP0+BP1: the bottom 256 KiB takes the first two banks. */
ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0, 0x40000, &s, &e);
KUNIT_EXPECT_EQ(test, ret, 1);
KUNIT_EXPECT_EQ(test, s, 0x44000ULL);
KUNIT_EXPECT_EQ(test, e, 0x1f4000ULL);
/* A range ending on a bank boundary takes exactly those banks. */
ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0, 0x44000, &s, &e);
KUNIT_EXPECT_EQ(test, ret, 1);
KUNIT_EXPECT_EQ(test, s, 0x44000ULL);
ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0x1e4000, 0x200000, &s, &e);
KUNIT_EXPECT_EQ(test, ret, 1);
KUNIT_EXPECT_EQ(test, e, 0x1e4000ULL);
/* The top half, or all of the part: nothing left. */
ret = apple_qspi_unprotected_banks(0x700000, 0x800000, 0x400000, 0x800000, &s, &e);
KUNIT_EXPECT_EQ(test, ret, 0);
ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0, 0x200000, &s, &e);
KUNIT_EXPECT_EQ(test, ret, 0);
/* A range inside the window. */
ret = apple_qspi_unprotected_banks(0x24000, 0x1f4000, 0x100000, 0x110000, &s, &e);
KUNIT_EXPECT_EQ(test, ret, 0);
}

static void qspi_policy_sfdp(struct kunit *test)
{
unsigned char h[16] = { 'S', 'F', 'D', 'P', 6, 1, 1, 0xff,
0x00, 6, 1, 16, 0x30, 0, 0, 0xff };

KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_basic_table(h), 0x30L);
h[3] = 'Q';
KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_basic_table(h), -1L);
h[3] = 'P';
h[8] = 0x84; /* not the basic table */
KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_basic_table(h), -1L);
/* 64 Mbit and 16 Mbit as bit counts minus one, and as a power of two. */
KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_size(0x03ffffff), 0x800000ULL);
KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_size(0x00ffffff), 0x200000ULL);
KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_size(0x80000000 | 26), 0x800000ULL);
KUNIT_EXPECT_EQ(test, apple_qspi_sfdp_size(0x80000000 | 1), 0ULL);
}

static struct kunit_case apple_qspi_policy_cases[] = {
KUNIT_CASE(qspi_policy_write_enable),
KUNIT_CASE(qspi_policy_program),
Expand All @@ -220,6 +398,12 @@ static struct kunit_case apple_qspi_policy_cases[] = {
KUNIT_CASE(qspi_policy_mx_status),
KUNIT_CASE(qspi_policy_gd_status),
KUNIT_CASE(qspi_policy_part_status),
KUNIT_CASE(qspi_policy_window),
KUNIT_CASE(qspi_policy_bank_state),
KUNIT_CASE(qspi_policy_parts),
KUNIT_CASE(qspi_policy_protected),
KUNIT_CASE(qspi_policy_unprotected_banks),
KUNIT_CASE(qspi_policy_sfdp),
{}
};

Expand Down
175 changes: 173 additions & 2 deletions drivers/spi/spi-apple-qspi-policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#define APPLE_QSPI_ERASE_SIZE 4096U

/*
* [start, end) is the board's nvram partition. Subtraction avoids overflow;
* addr is not truncated to wire width first.
* [start, end) is the write window, the device tree's nvram partition.
* Subtraction avoids overflow; addr is not truncated to wire width first.
*/
static inline int apple_qspi_range_allowed(unsigned long long addr,
unsigned int len, unsigned long long start,
Expand Down Expand Up @@ -113,4 +113,175 @@ static inline int apple_qspi_part_status_allows_write(unsigned int mfr,
return apple_qspi_status_allows_write(sr1, sr2, sr3, bp_ok);
}
}

/* The size of one NVRAM v3 bank ("3VVN" store). */
#define APPLE_QSPI_NVRAM_BANK 0x10000U

/*
* Whether an nvram partition can be the write window: the only one with that
* label, one address and one size cell, not marked read-only, a whole number
* of NVRAM banks from a 4 KiB boundary, and within three-byte addressing.
*/
static inline int apple_qspi_window_valid(unsigned int found,
unsigned int reg_cells, int read_only,
unsigned long long start,
unsigned long long size)
{
return found == 1 && reg_cells == 2 && !read_only && size &&
!(start % APPLE_QSPI_ERASE_SIZE) &&
!(size % APPLE_QSPI_NVRAM_BANK) && start + size <= 1ULL << 24;
}

/*
* The first 24 bytes of an NVRAM bank: 1 for a v3 store header ("3VVN",
* version 1, a store size from the header itself up to the bank), 0 for a
* bank that may be erased (the caller checks the rest of it), -1 for
* anything else. The v3 header has no checksum of its own; each variable in
* the store carries a CRC32.
*/
static inline int apple_qspi_nvram_bank_state(const unsigned char h[24])
{
unsigned int size = h[4] | h[5] << 8 | h[6] << 16 | (unsigned int)h[7] << 24;
int i;

for (i = 0; i < 24 && h[i] == 0xff; i++)
;
if (i == 24)
return 0;
if (h[0] == '3' && h[1] == 'V' && h[2] == 'V' && h[3] == 'N' &&
h[14] == 1 && size >= 24 && size <= APPLE_QSPI_NVRAM_BANK)
return 1;
return -1;
}

/*
* The boot NOR parts whose nvram partition may be written: the JEDEC ID, the
* size, which bounds the window (a three-byte address past it wraps around)
* and must match the part's SFDP density, and the SR1 block-protect bits the
* part's firmware leaves set. Those stay set; apple_qspi_protected_range()
* says what they protect.
*/
struct apple_qspi_part {
unsigned char jedec[3];
unsigned int size;
unsigned char bp_ok;
};

#define APPLE_QSPI_JEDEC_W25Q64 { 0xef, 0x65, 0x17 }
#define APPLE_QSPI_JEDEC_W25Q16 { 0xef, 0x65, 0x15 }
#define APPLE_QSPI_JEDEC_MX_2B27 { 0xc2, 0x2b, 0x27 }
#define APPLE_QSPI_JEDEC_GD25UF16E { 0xc8, 0x83, 0x15 }

static const struct apple_qspi_part apple_qspi_parts[] = {
/* J613, J713, J813; also on the bus of some J616s */
{ APPLE_QSPI_JEDEC_W25Q64, 0x800000, 0 },
/* J616s, the ID macOS reports */
{ APPLE_QSPI_JEDEC_MX_2B27, 0x800000, 0 },
/* J700: iBoot leaves BP0 set, over the top 64 KiB */
{ APPLE_QSPI_JEDEC_W25Q16, 0x200000, 0x1c },
/* Some J700s; BP0 covers the same range */
{ APPLE_QSPI_JEDEC_GD25UF16E, 0x200000, 0x1c },
};

static inline const struct apple_qspi_part *
apple_qspi_find_part(const unsigned char id[3])
{
unsigned int i;

for (i = 0; i < ARRAY_SIZE(apple_qspi_parts); i++)
if (id[0] == apple_qspi_parts[i].jedec[0] &&
id[1] == apple_qspi_parts[i].jedec[1] &&
id[2] == apple_qspi_parts[i].jedec[2])
return &apple_qspi_parts[i];
return NULL;
}

/*
* What SR1 protects on a part that tolerates BP bits (the 2 MiB parts, whose
* status policy leaves BP0-BP2 and, on Winbond, TB and SEC as found; CMP, and
* BP3/BP4 on GigaDevice, are refused before this). BP0-BP2 of 1 to 5 protect
* 1/32 to 1/2 of the part, or with SEC 4 to 32 KiB; 6 and 7 protect all of it.
* TB puts the range at the bottom instead of the top. Sets [*start, *end) and
* returns 1, or returns 0 when nothing is protected.
*/
static inline int apple_qspi_protected_range(unsigned int mfr, unsigned int sr1,
unsigned long long size,
unsigned long long *start,
unsigned long long *end)
{
unsigned int bp = (sr1 >> 2) & 7, shift;
int bottom = 0, sec = 0;
unsigned long long len;

if (!bp)
return 0;
if (mfr != APPLE_QSPI_MFR_GIGADEVICE) {
bottom = !!(sr1 & 0x20);
sec = !!(sr1 & 0x40);
}
if (bp >= 6) {
len = size;
} else if (sec) {
shift = bp - 1 < 3 ? bp - 1 : 3;
len = 0x1000ULL << shift;
} else {
len = (size >> 5) << (bp - 1);
}
*start = bottom ? 0 : size - len;
*end = *start + len;
return 1;
}

/*
* The banks of the window [start, end) that a protected range [ps, pe) does
* not reach, as [*wstart, *wend): a bank the range only partly covers is
* dropped whole, so an erase of it never stops halfway. The decoded range
* always reaches one end of the part, so it takes a run of banks from one
* end of the window. Returns 0 when no bank is left, or for a range inside
* the window with banks on both sides of it, which no decode produces.
*/
static inline int apple_qspi_unprotected_banks(unsigned long long start,
unsigned long long end,
unsigned long long ps,
unsigned long long pe,
unsigned long long *wstart,
unsigned long long *wend)
{
unsigned long long bank = APPLE_QSPI_NVRAM_BANK;

*wstart = start;
*wend = end;
if (pe <= start || ps >= end)
return 1;
if (ps <= start)
*wstart = start + (pe - start + bank - 1) / bank * bank;
else if (pe >= end)
*wend = start + (ps - start) / bank * bank;
else
return 0;
return *wstart < *wend;
}

/*
* The JEDEC basic flash parameter table from the first 16 bytes of the SFDP
* space: "SFDP", then the first parameter header, which must be the basic
* table (ID 0x00, MSB 0xff). Returns the table's address, or -1.
*/
static inline long apple_qspi_sfdp_basic_table(const unsigned char h[16])
{
if (h[0] != 'S' || h[1] != 'F' || h[2] != 'D' || h[3] != 'P' ||
h[8] != 0x00 || h[15] != 0xff || h[11] < 2)
return -1;
return h[12] | h[13] << 8 | h[14] << 16;
}

/* The part size in bytes from the basic table's second DWORD, or 0. */
static inline unsigned long long apple_qspi_sfdp_size(unsigned int dword2)
{
unsigned int n = dword2 & 0x7fffffff;

if (dword2 & 0x80000000)
return n >= 3 && n < 64 ? 1ULL << (n - 3) : 0;
return ((unsigned long long)dword2 + 1) / 8;
}
#endif
Loading
Loading