Received: from eggs.gnu.org ([209.51.188.92]:36547) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGyy1-0003Ec-6n for qemu-devel@nongnu.org; Thu, 18 Apr 2019 00:48:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hGyxz-0000Uu-90 for qemu-devel@nongnu.org; Thu, 18 Apr 2019 00:48:09 -0400 References: <20190417220147.67648-1-stephen.checkoway@oberlin.edu> <20190417220147.67648-2-stephen.checkoway@oberlin.edu> From: Thomas Huth Message-ID: <3ffacb2c-2459-fb45-ca9c-7b59b10a1c76@redhat.com> Date: Thu, 18 Apr 2019 06:47:56 +0200 MIME-Version: 1.0 In-Reply-To: <20190417220147.67648-2-stephen.checkoway@oberlin.edu> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v3 01/10] block/pflash_cfi02: Add test for supported commands List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stephen Checkoway , QEMU Developers , =?UTF-8?Q?Philippe_Mathieu-Daud=c3=a9?= , Kevin Wolf , Max Reitz , "open list:Block layer core" , Markus Armbruster , Laszlo Ersek , Laurent Vivier , Paolo Bonzini On 18/04/2019 00.01, Stephen Checkoway wrote: > Test the AMD command set for parallel flash chips. This test uses an > ARM musicpal board with a pflash drive to test the following list of > currently-supported commands. > - Autoselect > - CFI > - Sector erase > - Chip erase > - Program > - Unlock bypass > - Reset >=20 > Signed-off-by: Stephen Checkoway > --- > tests/Makefile.include | 2 + > tests/pflash-cfi02-test.c | 227 ++++++++++++++++++++++++++++++++++++++ > 2 files changed, 229 insertions(+) > create mode 100644 tests/pflash-cfi02-test.c [...] > new file mode 100644 > index 0000000000..b113fca5af > --- /dev/null > +++ b/tests/pflash-cfi02-test.c > @@ -0,0 +1,227 @@ > +/* > + * QTest testcase for parallel flash with AMD command set > + * > + * Copyright (c) 2018 Stephen Checkoway Do you maybe want to update that to 2019? > + * This work is licensed under the terms of the GNU GPL, version 2 or = later. > + * See the COPYING file in the top-level directory. > + */ > + > +#include "qemu/osdep.h" > +#include We generally don't use err.h in QEMU ... could you please use the standard functions from glib instead? > +#include unistd.h is already provided by osdep.h, so you don't need to include it here again. (and the scripts/clean-includes script will barf at this later, so better fix it right from the start) > +#include "libqtest.h" > + > +/* > + * To test the pflash_cfi02 device, we run QEMU with the musicpal mach= ine with > + * a pflash drive. This enables us to test some flash configurations, = but not > + * all. In particular, we're limited to a 16-bit wide flash device. > + */ > + > +#define MP_FLASH_SIZE_MAX (32 * 1024 * 1024) > +#define BASE_ADDR (0x100000000ULL - MP_FLASH_SIZE_MAX) > + > +#define FLASH_WIDTH 2 > +#define CFI_ADDR (FLASH_WIDTH * 0x55) > +#define UNLOCK0_ADDR (FLASH_WIDTH * 0x5555) > +#define UNLOCK1_ADDR (FLASH_WIDTH * 0x2AAA) > + > +#define CFI_CMD 0x98 > +#define UNLOCK0_CMD 0xAA > +#define UNLOCK1_CMD 0x55 > +#define AUTOSELECT_CMD 0x90 > +#define RESET_CMD 0xF0 > +#define PROGRAM_CMD 0xA0 > +#define SECTOR_ERASE_CMD 0x30 > +#define CHIP_ERASE_CMD 0x10 > +#define UNLOCK_BYPASS_CMD 0x20 > +#define UNLOCK_BYPASS_RESET_CMD 0x00 > + > +static char image_path[] =3D "/tmp/qtest.XXXXXX"; > + > +static inline void flash_write(uint64_t byte_addr, uint16_t data) > +{ > + qtest_writew(global_qtest, BASE_ADDR + byte_addr, data); > +} > + > +static inline uint16_t flash_read(uint64_t byte_addr) > +{ > + return qtest_readw(global_qtest, BASE_ADDR + byte_addr); > +} > + > +static void unlock(void) > +{ > + flash_write(UNLOCK0_ADDR, UNLOCK0_CMD); > + flash_write(UNLOCK1_ADDR, UNLOCK1_CMD); > +} > + > +static void reset(void) > +{ > + flash_write(0, RESET_CMD); > +} > + > +static void sector_erase(uint64_t byte_addr) > +{ > + unlock(); > + flash_write(UNLOCK0_ADDR, 0x80); > + unlock(); > + flash_write(byte_addr, SECTOR_ERASE_CMD); > +} > + > +static void wait_for_completion(uint64_t byte_addr) > +{ > + /* If DQ6 is toggling, step the clock and ensure the toggle stops.= */ > + if ((flash_read(byte_addr) & 0x40) ^ (flash_read(byte_addr) & 0x40= )) { > + /* Wait for erase or program to finish. */ > + clock_step_next(); > + /* Ensure that DQ6 has stopped toggling. */ > + g_assert_cmpint(flash_read(byte_addr), =3D=3D, flash_read(byte= _addr)); > + } > +} > + > +static void bypass_program(uint64_t byte_addr, uint16_t data) > +{ > + flash_write(UNLOCK0_ADDR, PROGRAM_CMD); > + flash_write(byte_addr, data); > + /* > + * Data isn't valid until DQ6 stops toggling. We don't model this = as > + * writes are immediate, but if this changes in the future, we can= wait > + * until the program is complete. > + */ > + wait_for_completion(byte_addr); > +} > + > +static void program(uint64_t byte_addr, uint16_t data) > +{ > + unlock(); > + bypass_program(byte_addr, data); > +} > + > +static void chip_erase(void) > +{ > + unlock(); > + flash_write(UNLOCK0_ADDR, 0x80); > + unlock(); > + flash_write(UNLOCK0_ADDR, SECTOR_ERASE_CMD); > +} > + > +static void test_flash(void) > +{ > + global_qtest =3D qtest_initf("-M musicpal,accel=3Dqtest " > + "-drive if=3Dpflash,file=3D%s,format=3D= raw,copy-on-read", > + image_path); > + /* Check the IDs. */ > + unlock(); > + flash_write(UNLOCK0_ADDR, AUTOSELECT_CMD); > + g_assert_cmpint(flash_read(FLASH_WIDTH * 0x0000), =3D=3D, 0x00BF); > + g_assert_cmpint(flash_read(FLASH_WIDTH * 0x0001), =3D=3D, 0x236D); > + reset(); > + > + /* Check the erase blocks. */ > + flash_write(CFI_ADDR, CFI_CMD); > + g_assert_cmpint(flash_read(FLASH_WIDTH * 0x10), =3D=3D, 'Q'); > + g_assert_cmpint(flash_read(FLASH_WIDTH * 0x11), =3D=3D, 'R'); > + g_assert_cmpint(flash_read(FLASH_WIDTH * 0x12), =3D=3D, 'Y'); > + /* Num erase regions. */ > + g_assert_cmpint(flash_read(FLASH_WIDTH * 0x2C), >=3D, 1); > + uint32_t nb_sectors =3D flash_read(FLASH_WIDTH * 0x2D) + > + (flash_read(FLASH_WIDTH * 0x2E) << 8) + 1; > + uint32_t sector_len =3D (flash_read(FLASH_WIDTH * 0x2F) << 8) + > + (flash_read(FLASH_WIDTH * 0x30) << 16); > + reset(); > + > + /* Erase and program sector. */ > + for (uint32_t i =3D 0; i < nb_sectors; ++i) { > + uint64_t byte_addr =3D i * sector_len; > + sector_erase(byte_addr); > + /* Read toggle. */ > + uint16_t status0 =3D flash_read(byte_addr); > + /* DQ7 is 0 during an erase. */ > + g_assert_cmpint(status0 & 0x80, =3D=3D, 0); > + uint16_t status1 =3D flash_read(byte_addr); > + /* DQ6 toggles during an erase. */ > + g_assert_cmpint(status0 & 0x40, !=3D, status1 & 0x40); > + /* Wait for erase to complete. */ > + clock_step_next(); > + /* Ensure DQ6 has stopped toggling. */ > + g_assert_cmpint(flash_read(byte_addr), =3D=3D, flash_read(byte= _addr)); > + /* Now the data should be valid. */ > + g_assert_cmpint(flash_read(byte_addr), =3D=3D, 0xFFFF); > + > + /* Program a bit pattern. */ > + program(byte_addr, 0x5555); > + g_assert_cmpint(flash_read(byte_addr), =3D=3D, 0x5555); > + program(byte_addr, 0xAA55); > + g_assert_cmpint(flash_read(byte_addr), =3D=3D, 0x0055); > + } > + > + /* Erase the chip. */ > + chip_erase(); > + /* Read toggle. */ > + uint16_t status0 =3D flash_read(0); > + /* DQ7 is 0 during an erase. */ > + g_assert_cmpint(status0 & 0x80, =3D=3D, 0); > + uint16_t status1 =3D flash_read(0); > + /* DQ6 toggles during an erase. */ > + g_assert_cmpint(status0 & 0x40, !=3D, status1 & 0x40); > + /* Wait for erase to complete. */ > + clock_step_next(); > + /* Ensure DQ6 has stopped toggling. */ > + g_assert_cmpint(flash_read(0), =3D=3D, flash_read(0)); > + /* Now the data should be valid. */ > + g_assert_cmpint(flash_read(0), =3D=3D, 0xFFFF); > + > + /* Unlock bypass */ > + unlock(); > + flash_write(UNLOCK0_ADDR, UNLOCK_BYPASS_CMD); > + bypass_program(0, 0x0123); > + bypass_program(2, 0x4567); > + bypass_program(4, 0x89AB); > + /* > + * Test that bypass programming, unlike normal programming can use= any > + * address for the PROGRAM_CMD. > + */ > + flash_write(6, PROGRAM_CMD); > + flash_write(6, 0xCDEF); > + wait_for_completion(6); > + flash_write(0, UNLOCK_BYPASS_RESET_CMD); > + bypass_program(8, 0x55AA); /* Should fail. */ > + g_assert_cmpint(flash_read(0), =3D=3D, 0x0123); > + g_assert_cmpint(flash_read(2), =3D=3D, 0x4567); > + g_assert_cmpint(flash_read(4), =3D=3D, 0x89AB); > + g_assert_cmpint(flash_read(6), =3D=3D, 0xCDEF); > + g_assert_cmpint(flash_read(8), =3D=3D, 0xFFFF); > + > + qtest_quit(global_qtest); > +} > + > +static void cleanup(void *opaque) > +{ > + unlink(image_path); > +} > + > +int main(int argc, char **argv) > +{ > + int fd =3D mkstemp(image_path); > + if (fd =3D=3D -1) { > + err(1, "Failed to create temporary file %s", image_path); g_printerr() ? or maybe simply use a g_assert_cmpint(ret, !=3D, -1) like we do it in a couple of other places already. > + } > + if (ftruncate(fd, 8 * 1024 * 1024) < 0) { > + int error_code =3D errno; > + close(fd); > + unlink(image_path); > + g_printerr("Failed to truncate file %s to 8 MB: %s\n", image_p= ath, > + strerror(error_code)); > + exit(EXIT_FAILURE); > + } > + close(fd); > + > + qtest_add_abrt_handler(cleanup, NULL); > + g_test_init(&argc, &argv, NULL); > + qtest_add_func("pflash-cfi02", test_flash); > + int result =3D g_test_run(); > + cleanup(NULL); > + return result; > +} > + > +/* vim: set sw=3D4 sts=3D4 ts=3D8 et: */ IMHO the vim line should not be necessary (and we hardly do that in any of the other files). Thomas