Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932375AbbFIWXM (ORCPT ); Tue, 9 Jun 2015 18:23:12 -0400 Received: from mail-by2on0115.outbound.protection.outlook.com ([207.46.100.115]:22206 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752183AbbFIWXG (ORCPT ); Tue, 9 Jun 2015 18:23:06 -0400 Authentication-Results: spf=fail (sender IP is 192.88.168.50) smtp.mailfrom=freescale.com; freescale.mail.onmicrosoft.com; dkim=none (message not signed) header.d=none; From: "J. German Rivera" To: , , , CC: , , , , , , , , , , "J. German Rivera" Subject: [PATCH v4 6/7] staging: fsl-mc: Add locking to serialize mc_send_command() calls Date: Tue, 9 Jun 2015 16:59:07 -0500 Message-ID: <1433887148-2310-7-git-send-email-German.Rivera@freescale.com> X-Mailer: git-send-email 2.3.3 In-Reply-To: <1433887148-2310-1-git-send-email-German.Rivera@freescale.com> References: <1433887148-2310-1-git-send-email-German.Rivera@freescale.com> X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1;BN1BFFO11FD034;1:caSU8ePZc8HWILqsdO9DS7FvFYVVHAsxTc8b5TBG7pqHjnQGqO+XGOhhrpF+K8yKj3xUhdsq5RL8+Gs7xY4z5NLkkEoR4AFNIUkqYA1JxvzLoVvawtqiPmTjALxPjkOPmkjely8fu+KxA3oc6OW4XUX5RP5P7f4OC0NtaLUN0Q1unUmsLT5dh+EHQkF9oq3yjtIVwrqBp3cv/7gqEHqt9HJTE2NYCe9UdM50loYT+8yiXLbNJJpBEm1kXeuvKqOdwEbtQgBGmI81/AD3k/RGF5SEBggskRidhdf3Ab2aPDdnFXMb8gcIIpqTejmkPR1hsOXG1P8AgwTPAgXBEZ24/w== X-Forefront-Antispam-Report: CIP:192.88.168.50;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10019020)(6009001)(339900001)(189002)(199003)(48376002)(107886002)(5001770100001)(19580405001)(189998001)(5001960100002)(50466002)(50986999)(36756003)(2201001)(85426001)(50226001)(47776003)(76176999)(106466001)(6806004)(19580395003)(105606002)(62966003)(104016003)(87936001)(77156002)(77096005)(46102003)(86362001)(2950100001)(229853001)(92566002)(4001430100001);DIR:OUT;SFP:1102;SCL:1;SRVR:BY2PR03MB458;H:tx30smr01.am.freescale.net;FPR:;SPF:Fail;MLV:sfv;MX:1;A:1;LANG:en; MIME-Version: 1.0 Content-Type: text/plain X-Microsoft-Exchange-Diagnostics: 1;BY2PR03MB458;2:5c6Z6JPLbSHzYXU4+3DcIaR2cOqWmQ36MAz1OkHUjOAafYksd5NJf1pQqSV8Cv07;2:J3lwtm2gdjNrphXJ32encvDQ4T8C3A/ov5qFQCAUlJFtK4c9w1bgxDwkYU1OXqeD1LgscPAR09wTHSjXXjfFHKT7Q0EqQFYFcmGQlmdgc+NzxTKq3pifmniJ9zbLexAkhgMKyCX4X2Me+apGNSULH/v6MZYx7uZuX3YR+tmpJxJYJ15gPoVHMJhHxfspo68QMr8wp4ssyZJY6XjWOZxy5OBRDWKF5Yy3eg5++KtYR4s=;6:HLuj6u5Dp/KWBEZDxVr/yvSmfzso4XeInSZJ1fMbQ/90x15Ppn0w7iORcHzeIknPd+eeb4tElV1dhTsXYlF6cuNkfI4X0D08f99TL9aV04gEpI+pOJSlPWCzQEBzGiw2XJQ6q7vJyPX3One9gHks/DhmswJEYcFhFkMx755hT4nWq53aCSC2DMFiBEiXL+ZXS6Z0UIA6JXQMAXhNkZruoZ02CgcBdrHPUkowCY0NmkYJUSfRHGYaMCToin9BkXFT+Uo4wybfIEjKIABjxrGFtAMUGEp8QDIdUM1zOc49/xx+45pIKDIBf7Mk9rEwOiEp2Ynwa9VsFa0nbLYQrMZRLA==;3:WkzWdKmu5tYRPLNlxPAp62CDnb6V4Y82zILMeQu6Y+56blzvDF3kXQxVkKRyNtcG6JX7dtA/0fDvF51ML7V0iS0cAlUo0xl+2DhUwrkuOO2vP5vGsS+JIwV7LH+bpPSppgCRG44LOLizcvuHQp3Do8eNTaA4KuJTQDqWNmI79FeCw/CG4Xqth04+/Dve/mC8hj7WN3Qd5ZGmw8Dg+NqAaEoe+oRWqf0OuY3WwFW5ncO+8Ka9bmpO/l8JIkDxMhN3ljtFnft67brVfL17P14wpIf2/Bvudw257twIaKaGZhP1A6l+E+fdnGCRA6a8eDQ/ X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BY2PR03MB458; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(5005006)(520003)(3002001);SRVR:BY2PR03MB458;BCL:0;PCL:0;RULEID:;SRVR:BY2PR03MB458; X-Forefront-PRVS: 06022AA85F X-Microsoft-Exchange-Diagnostics: 1;BY2PR03MB458;9:Bq/KMLwkNhaCaRvs+jOl0bbS96a5JrUZERqBuC5u5MhLZjnE0SLd42QnopgVnKVFZqMrEoCufiirkJ7S4BPzYd551g+D5zNBg85pVnRdGSXBFlXgB0/iVGdDq6ZlaNSIAL5ij2fdl6KZ/1clcVZMUTTzHFDC5dgaStgisuRSWed4/2atWee5xo+SRMTU62IwAHcsxZwOxCSBhM+oqFpo4SHF17QW/UuWxxbbTpDXlabIRMcP29NDXflWVr7cxMx5oHykeiTOxckZvorBy0oNd+ZaRsZzp0jd0tgsvQzj+2qJAdqr0vdJLCTxCdwDymB96mNKV1E9Au2KLiZNrzBWN3jRzO+FtWe4+VokqgyCnI2KDrlLkdZnEklbXqt5ugDWK/nru4Irv/exZAg527I36/zAfkGZptGVBnGWQjH3IXm2acWn7xuDv04Sw9fQ3l4AIVaU2Qgc1QZxceNmceiERGQkHqGPlquc26O5npNjU8MVKvG990q8GTv7V4K8yp4e4DyJKzfeab89PhPRc8hkLxjOATbfQm8wz4i1jrsn3MR2QJIgfhWNX3RGc5CP/B4UC+iXo8MmJN1BMAB8/hfI0+3E/c70/UEd2bQZMdmhVZWD00s8cfVfJ4MnP1ReqtMGjbqyLO2PadKc9loPDh1uvhwajOc8Ao1xEVDAfSt1Tain0Kka47UQ3PKpaiAf+mts77GzHJNq3OWmjcrpX9s+M+MpbYWlH3/I4VJW/GNEryfZQAJwscL6HbfoKCvruKH3/Qm5t6WrKUcG8N9E/gCdE6+oBdkksDV457w9svwVCFI8BsJ74BxoHmMyrEn5uPe1u7+eyewzwf/oYmm0f/SjPLf+wZh2QIshQrysAWc5KpVKRNCHOmyahhAPJaG3xXw2PY+wALQVCzMEWguY3iGmng== X-Microsoft-Exchange-Diagnostics: 1;BY2PR03MB458;3:icVMVB81nw2u0tpZE9YbmOOAnJVp83micc3J3GqZf5mcjNEeXArk5KjaPctwQPKoO5VfpRJ+jNivcezY1IxwqJLTDqvQfaQgIAEaUH1QwUxNHh7Kuf9Se9XbGXxEFAT3Zad73CmUGuvMP+mmG4Hl2w==;10:sfz/vhFHEqEsUVH3XmjQZt0SgWBN0r11ULYz4sciNu1hmA2POvWyOBz+qCGp56h8LmNSmNZrgECqHOWj4K+irIX5JPIRQ62OtS41qBnoBVo=;6:hGlWEVI4KKv9N+hYRd5iwY6too9sZmeXcnCGCQINAB5BVRzN8BkReO8EtdqoHY4u X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2015 22:08:50.4023 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d;Ip=[192.88.168.50];Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR03MB458 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5823 Lines: 169 Add a locking mechanism to serialize mc_send_command() calls that use the same fsl_mc_io object (same MC portal). When the fsl_mc_io object is created the owner needs to know in which type of context the fsl_mc_io object is going to be used. A flag passed-in to fsl_create_mc_io() will indicate whether the fsl_mc_io object will be used in atomic or non-atomic context. If the fsl_mc_io object is going to be used in non-atomic context only, mc_send_command() calls with it will be serialized using a mutex. Otherwise, if the fsl_mc_io object is going to be used in atomic context, mc_semd_command() calls with it will be serialized using a spinlock. Signed-off-by: J. German Rivera Reviewed-by: Stuart Yoder --- Changes in v4: - Fixed new checkpatch checks drivers/staging/fsl-mc/bus/mc-sys.c | 40 ++++++++++++++++++++++++++------- drivers/staging/fsl-mc/include/mc-sys.h | 23 +++++++++++++++++-- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/drivers/staging/fsl-mc/bus/mc-sys.c b/drivers/staging/fsl-mc/bus/mc-sys.c index f421411..0da7700 100644 --- a/drivers/staging/fsl-mc/bus/mc-sys.c +++ b/drivers/staging/fsl-mc/bus/mc-sys.c @@ -40,9 +40,9 @@ #include /** - * Timeout in jiffies to wait for the completion of an MC command + * Timeout in milliseconds to wait for the completion of an MC command */ -#define MC_CMD_COMPLETION_TIMEOUT_JIFFIES (HZ / 2) /* 500 ms */ +#define MC_CMD_COMPLETION_TIMEOUT_MS 500 /* * usleep_range() min and max values used to throttle down polling @@ -86,6 +86,11 @@ int __must_check fsl_create_mc_io(struct device *dev, mc_io->portal_phys_addr = mc_portal_phys_addr; mc_io->portal_size = mc_portal_size; mc_io->resource = resource; + if (flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL) + spin_lock_init(&mc_io->spinlock); + else + mutex_init(&mc_io->mutex); + res = devm_request_mem_region(dev, mc_portal_phys_addr, mc_portal_size, @@ -230,14 +235,21 @@ static inline enum mc_cmd_status mc_read_response(struct mc_command __iomem * * @cmd: command to be sent * * Returns '0' on Success; Error code otherwise. - * - * NOTE: This function cannot be invoked from from atomic contexts. */ int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd) { + int error; enum mc_cmd_status status; unsigned long jiffies_until_timeout = - jiffies + MC_CMD_COMPLETION_TIMEOUT_JIFFIES; + jiffies + msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS); + + if (WARN_ON(in_irq())) + return -EINVAL; + + if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL) + spin_lock(&mc_io->spinlock); + else + mutex_lock(&mc_io->mutex); /* * Send command to the MC hardware: @@ -259,6 +271,8 @@ int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd) if (preemptible()) { usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS, MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS); + } else { + udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS); } if (time_after_eq(jiffies, jiffies_until_timeout)) { @@ -269,7 +283,8 @@ int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd) (unsigned int) MC_CMD_HDR_READ_CMDID(cmd->header)); - return -ETIMEDOUT; + error = -ETIMEDOUT; + goto common_exit; } } @@ -281,9 +296,18 @@ int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd) mc_status_to_string(status), (unsigned int)status); - return mc_status_to_error(status); + error = mc_status_to_error(status); + goto common_exit; } - return 0; + error = 0; + +common_exit: + if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL) + spin_unlock(&mc_io->spinlock); + else + mutex_unlock(&mc_io->mutex); + + return error; } EXPORT_SYMBOL(mc_send_command); diff --git a/drivers/staging/fsl-mc/include/mc-sys.h b/drivers/staging/fsl-mc/include/mc-sys.h index cb3b5a2..d2c95831 100644 --- a/drivers/staging/fsl-mc/include/mc-sys.h +++ b/drivers/staging/fsl-mc/include/mc-sys.h @@ -39,6 +39,13 @@ #include #include #include +#include +#include + +/** + * Bit masks for a MC I/O object (struct fsl_mc_io) flags + */ +#define FSL_MC_IO_ATOMIC_CONTEXT_PORTAL 0x0001 struct fsl_mc_resource; struct mc_command; @@ -53,14 +60,26 @@ struct mc_command; * @resource: generic resource associated with the MC portal if * the MC portal came from a resource pool, or NULL if the MC portal * is permanently bound to a device (e.g., a DPRC) + * @mutex: Mutex to serialize mc_send_command() calls that use the same MC + * portal, if the fsl_mc_io object was created with the + * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag off. mc_send_command() calls for this + * fsl_mc_io object must be made only from non-atomic context. + * @spinlock: Spinlock to serialize mc_send_command() calls that use the same MC + * portal, if the fsl_mc_io object was created with the + * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag on. mc_send_command() calls for this + * fsl_mc_io object can be made from atomic or non-atomic context. */ struct fsl_mc_io { struct device *dev; - uint32_t flags; - uint32_t portal_size; + u16 flags; + u16 portal_size; phys_addr_t portal_phys_addr; void __iomem *portal_virt_addr; struct fsl_mc_resource *resource; + union { + struct mutex mutex; /* serializes mc_send_command() calls */ + spinlock_t spinlock; /* serializes mc_send_command() calls */ + }; }; int __must_check fsl_create_mc_io(struct device *dev, -- 2.3.3 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/