From patchwork Fri Nov 8 05:19:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Stanley X-Patchwork-Id: 178853 Delivered-To: patch@linaro.org Received: by 2002:a92:38d5:0:0:0:0:0 with SMTP id g82csp2158955ilf; Thu, 7 Nov 2019 21:20:30 -0800 (PST) X-Google-Smtp-Source: APXvYqzA41qqVpY3uHk5Pbq1ux4IbyqaQmP3WKQdkK6C96iiIlpniUroiGwLNfZ6bNMtD0L+fUpD X-Received: by 2002:aa7:c145:: with SMTP id r5mr8223689edp.102.1573190430798; Thu, 07 Nov 2019 21:20:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573190430; cv=none; d=google.com; s=arc-20160816; b=KDo+WNzXJBW+AAVXUc7LTrQYieSu+2BmNzUIVTAssOx9n0c9CUvw09oKld2tAlf3RY 5GO999rFDchx2k/HcZ9iz0Gx+U2L61vO9BqiIMqDCPQwShNeiOYwQ6fv6MDrK6/rRBvU gkeBVJjSnheS12wlfYl2VUULWuCpu3m9ZS0A1lQUy0/xvSReaifK3QM8eifpNJ3gpNpq 9mGZq2Fz6tLYX1Mn2GbrCQihy2CxMSLVFJ7DR/ZNHRIS1xA6QDjySpXaq/FwEDN40cfQ tAG9hDf/gt5NzRVKKTHeKTaC+lNQtYGilz8YSKEmgp8nTYfz+lwON+V2pMTi/5CszlpU 6dVA== 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=abrU7G2BP5v8QeCE5OZCvci6QXqITkbp0Z5/JmfzQPA=; b=QHwUga8Gl6pPuAtisNCkltA28URY1qdNl0D6FVn9NysXCwmI7jXEvyqC6P9JfIp36p 35XeEw1N7wFIUwiK+B+fjeTG6KYPXmOA1iVq3KiB1JBQTCfeQ3KpPsNHfV+gfmr7zkuY L0kswvBCwzfLKuur1I4pCOwFoEZT44BfO0nlSFVBnI57sPG2JGpLTqQ4Z23fhAzoX9cB Y6Ra1suY/wFcG6Cf5DFLkNQUWp0GlYlhMrst54s5kXr5hLmMjM3I4j7dvZnvCcg+YZ5D 4BXJw034KZzDK5T6kKut1ZW9LxuTeyhap0nBndnY7KTFuxNLFGTP89Dkp431jmi0g4x2 x1Og== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=XS4EhrtL; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-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 o14si3182142ejx.248.2019.11.07.21.20.30; Thu, 07 Nov 2019 21:20:30 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=XS4EhrtL; spf=pass (google.com: best guess record for domain of devicetree-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726125AbfKHFU0 (ORCPT + 8 others); Fri, 8 Nov 2019 00:20:26 -0500 Received: from mail-pg1-f193.google.com ([209.85.215.193]:42201 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727832AbfKHFU0 (ORCPT ); Fri, 8 Nov 2019 00:20:26 -0500 Received: by mail-pg1-f193.google.com with SMTP id q17so3292057pgt.9; Thu, 07 Nov 2019 21:20:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=abrU7G2BP5v8QeCE5OZCvci6QXqITkbp0Z5/JmfzQPA=; b=XS4EhrtLD7ira7yvelmd2Zy3twZdQh8w75/0Bvi5MIu/L4pOW3y8Uz4PCFCEcn0x2V UJRHR7EHVv4upN1s3mKOU+aXz8ou1jxdpweTYwgv4ZnBbnd7Ii4zfwrgAp55n2AvBJH3 1jhNhhQuZvooBbddpFbLmdVJs6jwPqfRAfvZpnzrHYf0XrHgX5EGpC7yIkL5+Us9WUFx BakG/iV27xw1PKS7f7ib3sHdPeWqSavpn0U224EvrUgbgOKPkXXEHgU5eVlcMk3CC16K aYQeJ7wbJ/sy2mgsQ4LVYZXgUfbtEZQm42iDbOBbjbnNadLsIbqADQtzfg1IwqX9IdC7 iKVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=abrU7G2BP5v8QeCE5OZCvci6QXqITkbp0Z5/JmfzQPA=; b=NYeyBd2IDy07Iavz8tGvYp6Twf0NJYuqy4p1gxO4t7XF7xWxzdMSlm34EyIhJuT6Wv UPcPQztEJc7XHj2k2gs4WYyd49Ddux1JAYXu7+FhBi12wNF5KlEWRwuJfZnPLRecPZAS B3L+y72NEpVtdsld12ZeD4BpUBDtSlMFOeD//Lbp6Ayw7sGpwIGz/yJYqbyztd+8t1Eu JRqfcQn+QxpVrHEWoOR+1xH4FlurUmLRqsASqQWtWaLMqxsT/5DGLtfyG+I7vt+z1qav AQOdzYtSkR1juvfm6dNwHTcpJbKXKP5ImY8gDDcS3fZwkiOE1A6Y7SpWl82/4MUIY3zo IhMg== X-Gm-Message-State: APjAAAWj2QVSr/46hU+4Q/zVWQWUMoZtXz9PBw/80q51m0Da4E2Tixsl YhkgILX+LEc5nkCbHfTaigQ= X-Received: by 2002:a63:495b:: with SMTP id y27mr9468935pgk.438.1573190425229; Thu, 07 Nov 2019 21:20:25 -0800 (PST) Received: from voyager.ibm.com ([36.255.48.244]) by smtp.gmail.com with ESMTPSA id v19sm3798443pjr.14.2019.11.07.21.20.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Nov 2019 21:20:24 -0800 (PST) From: Joel Stanley To: Rob Herring , Greg KH , Jeremy Kerr Cc: Andrew Jeffery , Alistar Popple , Eddie James , Steven Rostedt , Ingo Molnar , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-fsi@lists.ozlabs.org Subject: [PATCH v2 05/11] fsi: core: Fix small accesses and unaligned offsets via sysfs Date: Fri, 8 Nov 2019 15:49:39 +1030 Message-Id: <20191108051945.7109-6-joel@jms.id.au> X-Mailer: git-send-email 2.24.0.rc1 In-Reply-To: <20191108051945.7109-1-joel@jms.id.au> References: <20191108051945.7109-1-joel@jms.id.au> MIME-Version: 1.0 Sender: devicetree-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org From: Andrew Jeffery Subtracting the offset delta from four-byte alignment lead to wrapping of the requested length where `count` is less than `off`. Generalise the length handling to enable and optimise aligned access sizes for all offset and size combinations. The new formula produces the following results for given offset and count values: offset count | length --------------+------- 0 1 | 1 0 2 | 2 0 3 | 2 0 4 | 4 0 5 | 4 1 1 | 1 1 2 | 1 1 3 | 1 1 4 | 1 1 5 | 1 2 1 | 1 2 2 | 2 2 3 | 2 2 4 | 2 2 5 | 2 3 1 | 1 3 2 | 1 3 3 | 1 3 4 | 1 3 5 | 1 We might need something like this for the cfam chardevs as well, for example we don't currently implement any alignment restrictions / handling in the hardware master driver. Signed-off-by: Andrew Jeffery Signed-off-by: Joel Stanley --- drivers/fsi/fsi-core.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) -- 2.24.0.rc1 diff --git a/drivers/fsi/fsi-core.c b/drivers/fsi/fsi-core.c index c773c65a5058..e02ebcb0c9e6 100644 --- a/drivers/fsi/fsi-core.c +++ b/drivers/fsi/fsi-core.c @@ -544,6 +544,31 @@ static int fsi_slave_scan(struct fsi_slave *slave) return 0; } +static unsigned long aligned_access_size(size_t offset, size_t count) +{ + unsigned long offset_unit, count_unit; + + /* Criteria: + * + * 1. Access size must be less than or equal to the maximum access + * width or the highest power-of-two factor of offset + * 2. Access size must be less than or equal to the amount specified by + * count + * + * The access width is optimal if we can calculate 1 to be strictly + * equal while still satisfying 2. + */ + + /* Find 1 by the bottom bit of offset (with a 4 byte access cap) */ + offset_unit = BIT(__builtin_ctzl(offset | 4)); + + /* Find 2 by the top bit of count */ + count_unit = BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count)); + + /* Constrain the maximum access width to the minimum of both criteria */ + return BIT(__builtin_ctzl(offset_unit | count_unit)); +} + static ssize_t fsi_slave_sysfs_raw_read(struct file *file, struct kobject *kobj, struct bin_attribute *attr, char *buf, loff_t off, size_t count) @@ -559,8 +584,7 @@ static ssize_t fsi_slave_sysfs_raw_read(struct file *file, return -EINVAL; for (total_len = 0; total_len < count; total_len += read_len) { - read_len = min_t(size_t, count, 4); - read_len -= off & 0x3; + read_len = aligned_access_size(off, count - total_len); rc = fsi_slave_read(slave, off, buf + total_len, read_len); if (rc) @@ -587,8 +611,7 @@ static ssize_t fsi_slave_sysfs_raw_write(struct file *file, return -EINVAL; for (total_len = 0; total_len < count; total_len += write_len) { - write_len = min_t(size_t, count, 4); - write_len -= off & 0x3; + write_len = aligned_access_size(off, count - total_len); rc = fsi_slave_write(slave, off, buf + total_len, write_len); if (rc)