Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2299500ybl; Thu, 19 Dec 2019 11:12:40 -0800 (PST) X-Google-Smtp-Source: APXvYqzYOPuRl36dslsO4MjQJJp9vBVa2Uquj92sFQYRD6Xs/Y7cjDiQiGk1O2OIwuYaULqyiyYS X-Received: by 2002:a05:6808:b1c:: with SMTP id s28mr2980815oij.2.1576782760763; Thu, 19 Dec 2019 11:12:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576782760; cv=none; d=google.com; s=arc-20160816; b=psHbO6DdUsiYJwPY6hS/lxuvAVaVqamaOFTF1c/HCxGv0xO2t+J9993gWxr4f9pJXx 2X3Lxpq365XFcs5ccWZZ/DamGNCDnDwIoORNrjvciLUOP3surQyKyD161EsF4SqmywQt xmdmqKUnFHUrjQNZLIcDJLCHi5+Qyc5zGmLXq35woBNAzsFvMdLP9CCNXgeiYux/RKG5 Ufnu61UN64ZjsGQM96qLPhFsrIHQAE29M+f2wS4C70S7chttjsOSrg3BMF8NRdiSFKxn Yri2KgUNjfua/ZJ8CFLOdq46oMEbBBmdNIl1TFrI8htHPkJXsh+9NZaF1bS7IeMl7DsK eHXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Tf8rCKl1j1BZuvr3QofBrsX4WUda2xXaNe9DF3vOi7Q=; b=LcyQTJXZdwX4WJWNw/ZY/lXBVh9XCejFK3ytQmg5FSQ9fevEGGgqfO5JR5owIRiFv8 ET21ld9d2dH6lKtNozOYdcdgFYVilveOcK5+DoXgZ51doo9SEvtH76tPXyuLT/4J452p 44m4vyJEwLezdphYaOMeHo2bIyVF+aZ4ylcQmB5Bd3k3E/lVAB0Bvor8BwNpK7Nqjgqq ppiOIrlrmbs8NoTSsnxB7A6bxFHMfdbqPbNkWocnZ9CUV1luwCDHIJGshSue0Lj/Qlgx KMvxah7q2SX12E28+Mky/Xn+wIa5rNngxgp/TcUij3f0DI3RBOltRYRpfiLiQqIILxVH 3UxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FOcEAoG1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w64si3408566oif.51.2019.12.19.11.12.27; Thu, 19 Dec 2019 11:12:40 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=FOcEAoG1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728148AbfLSSjI (ORCPT + 99 others); Thu, 19 Dec 2019 13:39:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:57062 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728089AbfLSSjA (ORCPT ); Thu, 19 Dec 2019 13:39:00 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C22A8222C2; Thu, 19 Dec 2019 18:38:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576780739; bh=EvYG8J8rrt1Rby5sE8y2ueLkHNm3ED/KbOuIQmmIz5k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FOcEAoG1lXOGTit39/IRDd99eHjZBuSHScDYS/1xIRrzvZD0DXT1zFR/BHwKhA3ey JBikXlRvRG1PjG0erthzW7rjXrvX5OrZqrNVD/X74sl0L2vtg7Pc9Y+cOMQQlWnahw v2IJXnZSzjRWVK5tqx7Oy7CTjdwGdQ/9TXW0Uoi0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Russell King , Boris Brezillon , Miquel Raynal , Russell King Subject: [PATCH 4.4 097/162] mtd: spear_smi: Fix Write Burst mode Date: Thu, 19 Dec 2019 19:33:25 +0100 Message-Id: <20191219183213.689364239@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191219183150.477687052@linuxfoundation.org> References: <20191219183150.477687052@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Miquel Raynal commit 69c7f4618c16b4678f8a4949b6bb5ace259c0033 upstream. Any write with either dd or flashcp to a device driven by the spear_smi.c driver will pass through the spear_smi_cpy_toio() function. This function will get called for chunks of up to 256 bytes. If the amount of data is smaller, we may have a problem if the data length is not 4-byte aligned. In this situation, the kernel panics during the memcpy: # dd if=/dev/urandom bs=1001 count=1 of=/dev/mtd6 spear_smi_cpy_toio [620] dest c9070000, src c7be8800, len 256 spear_smi_cpy_toio [620] dest c9070100, src c7be8900, len 256 spear_smi_cpy_toio [620] dest c9070200, src c7be8a00, len 256 spear_smi_cpy_toio [620] dest c9070300, src c7be8b00, len 233 Unhandled fault: external abort on non-linefetch (0x808) at 0xc90703e8 [...] PC is at memcpy+0xcc/0x330 The above error occurs because the implementation of memcpy_toio() tries to optimize the number of I/O by writing 4 bytes at a time as much as possible, until there are less than 4 bytes left and then switches to word or byte writes. Unfortunately, the specification states about the Write Burst mode: "the next AHB Write request should point to the next incremented address and should have the same size (byte, half-word or word)" This means ARM architecture implementation of memcpy_toio() cannot reliably be used blindly here. Workaround this situation by update the write path to stick to byte access when the burst length is not multiple of 4. Fixes: f18dbbb1bfe0 ("mtd: ST SPEAr: Add SMI driver for serial NOR flash") Cc: Russell King Cc: Boris Brezillon Cc: stable@vger.kernel.org Signed-off-by: Miquel Raynal Reviewed-by: Russell King Signed-off-by: Greg Kroah-Hartman --- drivers/mtd/devices/spear_smi.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) --- a/drivers/mtd/devices/spear_smi.c +++ b/drivers/mtd/devices/spear_smi.c @@ -595,6 +595,26 @@ static int spear_mtd_read(struct mtd_inf return 0; } +/* + * The purpose of this function is to ensure a memcpy_toio() with byte writes + * only. Its structure is inspired from the ARM implementation of _memcpy_toio() + * which also does single byte writes but cannot be used here as this is just an + * implementation detail and not part of the API. Not mentioning the comment + * stating that _memcpy_toio() should be optimized. + */ +static void spear_smi_memcpy_toio_b(volatile void __iomem *dest, + const void *src, size_t len) +{ + const unsigned char *from = src; + + while (len) { + len--; + writeb(*from, dest); + from++; + dest++; + } +} + static inline int spear_smi_cpy_toio(struct spear_smi *dev, u32 bank, void __iomem *dest, const void *src, size_t len) { @@ -617,7 +637,23 @@ static inline int spear_smi_cpy_toio(str ctrlreg1 = readl(dev->io_base + SMI_CR1); writel((ctrlreg1 | WB_MODE) & ~SW_MODE, dev->io_base + SMI_CR1); - memcpy_toio(dest, src, len); + /* + * In Write Burst mode (WB_MODE), the specs states that writes must be: + * - incremental + * - of the same size + * The ARM implementation of memcpy_toio() will optimize the number of + * I/O by using as much 4-byte writes as possible, surrounded by + * 2-byte/1-byte access if: + * - the destination is not 4-byte aligned + * - the length is not a multiple of 4-byte. + * Avoid this alternance of write access size by using our own 'byte + * access' helper if at least one of the two conditions above is true. + */ + if (IS_ALIGNED(len, sizeof(u32)) && + IS_ALIGNED((uintptr_t)dest, sizeof(u32))) + memcpy_toio(dest, src, len); + else + spear_smi_memcpy_toio_b(dest, src, len); writel(ctrlreg1, dev->io_base + SMI_CR1);