Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp430684imu; Wed, 12 Dec 2018 20:41:58 -0800 (PST) X-Google-Smtp-Source: AFSGD/V1Y37AIfTEzIc/yS1d7timVS9FZaqTpLacSFFbC+6EmnFVGm7OKbBW2k7J9VKYovepgOxQ X-Received: by 2002:a63:f141:: with SMTP id o1mr20850216pgk.134.1544676118331; Wed, 12 Dec 2018 20:41:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544676118; cv=none; d=google.com; s=arc-20160816; b=L8M+cv2u2bYj4W3JOPEq+kA1QhblS7r4Rr77m55JroT94UsG3Y4g7EWEwJdugtL9Ci Om9jgw9D3VO3Vpl5Ok6/HIP+Ja8++zsxYoizNd+ftUGTXteR5FVLSHBad8tzmMSHS32s uzXMY+J5O+vr3hdG5+7spsLhOnlbEYscYeGnRX2pUliMZvR81MxQn+kdP/zmKiy2CHnD sJibzsZFA3TibS1FJ/cspGdaIkVS11YfnVVrlDPvriDnqnBGe0xQ0qaxczM4mcrsNvst uS5bAZf9YoCuYprNbsa/n7n0lbYwNy+5LCkN78/CF88y/wTQboSb94X9BcTz/+W/OQDN LU9g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ehkYjFGHMtoQMvbYCqVM4sRAHPSVni3PXf8dYKXINJM=; b=NwmgT38XN6LeUovfJUgqJM8cbRYJjZWhv8LUxsmF145ne/gHVTXcRbBdn262jJs5/e t3MRDTN2pFrOBSvqe+Z4HRNTLAYqA9xu3bLQPDKhnHVkUd/msiDrr9OgTL4BpAkvkI4a 6AmlxHNIIJx8H1wspWnE/6D+9o9muDyJ+wfmWxZHxzNqcnyZf8M3MZEiueoT5uH7b5lA VqHeTW+6FeweO14YvIXBWdjAEJqrKpEr9ENGPUoc6psrQQiS4hQzR7/IvUXjJI0kxrCB WYUGgStD9JePodNveYiVDhWHnOYNzev7GWMz7jqiwYmdBK6L4BmKv+aED8GbVDbWy+Cr ZyPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=NR6I0hmk; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m33si712616pgl.379.2018.12.12.20.41.44; Wed, 12 Dec 2018 20:41:58 -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=NR6I0hmk; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729006AbeLMEcO (ORCPT + 99 others); Wed, 12 Dec 2018 23:32:14 -0500 Received: from mail.kernel.org ([198.145.29.99]:45134 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727044AbeLMEcM (ORCPT ); Wed, 12 Dec 2018 23:32:12 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 49D5D20672; Thu, 13 Dec 2018 04:32:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544675531; bh=iKpdsWNrXAPsASuUodZcuGVbdk04VYHiKmO/afWLtpI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NR6I0hmkDyGEghHFfoAvs9LlXX+IAr2tuAJjEALSCqIa7k1+CxYZjCyp9RCh8pqOW FxHmi5bU0zBJeUEUIfU4cRsqjpNyviZEazuhpBHolxyVj9vpxbi7KSo1hE7wLC7qBq 4ALMMZ0Y4lpVI18/20MgfESWP12I3dEO1W7PnHT4= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Jose Abreu , Alexey Brodkin , Joao Pinto , David Laight , Vineet Gupta , Sasha Levin , linux-snps-arc@lists.infradead.org Subject: [PATCH AUTOSEL 4.9 06/34] ARC: io.h: Implement reads{x}()/writes{x}() Date: Wed, 12 Dec 2018 23:31:32 -0500 Message-Id: <20181213043200.76295-6-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181213043200.76295-1-sashal@kernel.org> References: <20181213043200.76295-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jose Abreu [ Upstream commit 10d443431dc2bb733cf7add99b453e3fb9047a2e ] Some ARC CPU's do not support unaligned loads/stores. Currently, generic implementation of reads{b/w/l}()/writes{b/w/l}() is being used with ARC. This can lead to misfunction of some drivers as generic functions do a plain dereference of a pointer that can be unaligned. Let's use {get/put}_unaligned() helpers instead of plain dereference of pointer in order to fix. The helpers allow to get and store data from an unaligned address whilst preserving the CPU internal alignment. According to [1], the use of these helpers are costly in terms of performance so we added an initial check for a buffer already aligned so that the usage of the helpers can be avoided, when possible. [1] Documentation/unaligned-memory-access.txt Cc: Alexey Brodkin Cc: Joao Pinto Cc: David Laight Tested-by: Vitor Soares Signed-off-by: Jose Abreu Signed-off-by: Vineet Gupta Signed-off-by: Sasha Levin --- arch/arc/include/asm/io.h | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/arch/arc/include/asm/io.h b/arch/arc/include/asm/io.h index c22b181e8206..2f39d9b3886e 100644 --- a/arch/arc/include/asm/io.h +++ b/arch/arc/include/asm/io.h @@ -12,6 +12,7 @@ #include #include #include +#include #ifdef CONFIG_ISA_ARCV2 #include @@ -94,6 +95,42 @@ static inline u32 __raw_readl(const volatile void __iomem *addr) return w; } +/* + * {read,write}s{b,w,l}() repeatedly access the same IO address in + * native endianness in 8-, 16-, 32-bit chunks {into,from} memory, + * @count times + */ +#define __raw_readsx(t,f) \ +static inline void __raw_reads##f(const volatile void __iomem *addr, \ + void *ptr, unsigned int count) \ +{ \ + bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \ + u##t *buf = ptr; \ + \ + if (!count) \ + return; \ + \ + /* Some ARC CPU's don't support unaligned accesses */ \ + if (is_aligned) { \ + do { \ + u##t x = __raw_read##f(addr); \ + *buf++ = x; \ + } while (--count); \ + } else { \ + do { \ + u##t x = __raw_read##f(addr); \ + put_unaligned(x, buf++); \ + } while (--count); \ + } \ +} + +#define __raw_readsb __raw_readsb +__raw_readsx(8, b) +#define __raw_readsw __raw_readsw +__raw_readsx(16, w) +#define __raw_readsl __raw_readsl +__raw_readsx(32, l) + #define __raw_writeb __raw_writeb static inline void __raw_writeb(u8 b, volatile void __iomem *addr) { @@ -126,6 +163,35 @@ static inline void __raw_writel(u32 w, volatile void __iomem *addr) } +#define __raw_writesx(t,f) \ +static inline void __raw_writes##f(volatile void __iomem *addr, \ + const void *ptr, unsigned int count) \ +{ \ + bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \ + const u##t *buf = ptr; \ + \ + if (!count) \ + return; \ + \ + /* Some ARC CPU's don't support unaligned accesses */ \ + if (is_aligned) { \ + do { \ + __raw_write##f(*buf++, addr); \ + } while (--count); \ + } else { \ + do { \ + __raw_write##f(get_unaligned(buf++), addr); \ + } while (--count); \ + } \ +} + +#define __raw_writesb __raw_writesb +__raw_writesx(8, b) +#define __raw_writesw __raw_writesw +__raw_writesx(16, w) +#define __raw_writesl __raw_writesl +__raw_writesx(32, l) + /* * MMIO can also get buffered/optimized in micro-arch, so barriers needed * Based on ARM model for the typical use case @@ -141,10 +207,16 @@ static inline void __raw_writel(u32 w, volatile void __iomem *addr) #define readb(c) ({ u8 __v = readb_relaxed(c); __iormb(); __v; }) #define readw(c) ({ u16 __v = readw_relaxed(c); __iormb(); __v; }) #define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(); __v; }) +#define readsb(p,d,l) ({ __raw_readsb(p,d,l); __iormb(); }) +#define readsw(p,d,l) ({ __raw_readsw(p,d,l); __iormb(); }) +#define readsl(p,d,l) ({ __raw_readsl(p,d,l); __iormb(); }) #define writeb(v,c) ({ __iowmb(); writeb_relaxed(v,c); }) #define writew(v,c) ({ __iowmb(); writew_relaxed(v,c); }) #define writel(v,c) ({ __iowmb(); writel_relaxed(v,c); }) +#define writesb(p,d,l) ({ __iowmb(); __raw_writesb(p,d,l); }) +#define writesw(p,d,l) ({ __iowmb(); __raw_writesw(p,d,l); }) +#define writesl(p,d,l) ({ __iowmb(); __raw_writesl(p,d,l); }) /* * Relaxed API for drivers which can handle barrier ordering themselves -- 2.19.1