Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp467108rdb; Mon, 29 Jan 2024 07:44:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IE/LTYIYTc8y0xSwDgPRiXEcDCeazBQPN0LCTUGLDFGLr3T6idy5VkRy4kyVSl/B3PQYnot X-Received: by 2002:a05:6a00:6807:b0:6db:c6b1:aa5c with SMTP id hq7-20020a056a00680700b006dbc6b1aa5cmr2250615pfb.34.1706543068718; Mon, 29 Jan 2024 07:44:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706543068; cv=pass; d=google.com; s=arc-20160816; b=dLfHxz1SOY5mBRyg7mOoCIe9Ef2YEJKpXEy9dP3I/Efq5tJdru3VTWo55O++bEmhNh UJ7mwTyKPEGVuxwDxfopRbYaYBKNZIVhux/BazlcuAtssr1L7ikf69SEywteE6ux3o8v WqbB6IXA6PXeryYBuwzIYMcV9cVpF41ZIkXmqhbVLav4qNbSEbZLWzHC6C6rKSH5/fOz ++J0gJ+/23nEWML49hise+WgPN60Rcot7CNv80gs/bc9nHc8ihGFyAl/5ZFkNTA8xoYL ME4j2iIRc6ovhfZspgqcRrOQw7XRgcoxshW9uSeXjiFBfrY4BCtwFd4Xs+kElHDD89Xm bQfw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=Mn9uQDwfjVPKTRUPKw08+AlIaxBi7ZkVMLhG6OcBECs=; fh=L0mtDxzbexvY93/TXVDPqRiMSUezBOF86XyL0RxKhcM=; b=A5/OBcTzRhxNSqma1JqrWS029AQPh+lYXhsBxWh+pcsurw7dbzs4ZtsUQn00asEJCg L1n7XFxqCca0h/YR4OLSHLryFiC3tDxWdmE0L4L0K5EIQTpRSPxRggIakIgwIH6yaknj YgejJGV7oBLUY3dLy0hbkDe41pMXdHy0iAJHZa2dNjOOLukCsZ4JlLZe081mtEWfMbcT WufDgGHAV9RElQLKEer+jTEpIzr7WUOLEJ7NoQ/6R1pZ7vcxcQ2YVEvhjQOOgtGYZ9Ay E3/5xa2our7AWfiXANMkYHE5cO+ngJhgIgMhCXALuIgIir3RgSxBZNctwratD5BVNA5q EkcQ== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-43022-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43022-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id le10-20020a056a004fca00b006d98a6168c5si5989705pfb.344.2024.01.29.07.44.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 07:44:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-43022-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-43022-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43022-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 08F78288331 for ; Mon, 29 Jan 2024 15:43:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 22E301552F5; Mon, 29 Jan 2024 15:42:43 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 84136155314 for ; Mon, 29 Jan 2024 15:42:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706542962; cv=none; b=aHYcITu5/+5NsiB82Mhq9NnPIpXCoXOOSo/EXEpH5rr4KvyZCqhYaBB9cxaYtAnzJelsmX0WQVc8w9IuR5Zlw3stY+/yXI6PyJNXcJwVq5OiCuNZaPw21qkt5KS1WaSjQAOXRbIguSpM37KgZszQmYpqU25c2HP1/9YRBVbr1Uo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706542962; c=relaxed/simple; bh=xUWRBNQZVqitk4v7i63j+Unyd7Fb9W7l+PKYSUawyxw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=j4m4XqRBro8eOsITz6getshbwwd5bIX67Sg77FNG0v+C+OAPy0AWpFSIuWdsFBiph//bejwy+aoxqqXa3hbeIn4UbUbZ8ynz6PeAO7uJwOt/2L5h4zhK6XWGW67nixsEY8rqRBcQnYdwU+EGy7ACsFhSHkq8mOo27FOL5TNVWEQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C84EEDA7; Mon, 29 Jan 2024 07:43:23 -0800 (PST) Received: from e127643.lan (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 4DF813F738; Mon, 29 Jan 2024 07:42:37 -0800 (PST) From: James Clark To: coresight@lists.linaro.org, suzuki.poulose@arm.com Cc: James Clark , Mike Leach , Alexander Shishkin , Maxime Coquelin , Alexandre Torgue , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com Subject: [PATCH v2 10/12] coresight: Add helper for atomically taking the device Date: Mon, 29 Jan 2024 15:40:41 +0000 Message-Id: <20240129154050.569566-11-james.clark@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240129154050.569566-1-james.clark@arm.com> References: <20240129154050.569566-1-james.clark@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Now that mode is in struct coresight_device, this pattern can be wrapped in a helper. Signed-off-by: James Clark --- drivers/hwtracing/coresight/coresight-etm3x-core.c | 8 +++----- drivers/hwtracing/coresight/coresight-etm4x-core.c | 8 +++----- drivers/hwtracing/coresight/coresight-stm.c | 8 +++----- include/linux/coresight.h | 11 +++++++++++ 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/drivers/hwtracing/coresight/coresight-etm3x-core.c b/drivers/hwtracing/coresight/coresight-etm3x-core.c index a8be115636f0..ce2b3ed90fb9 100644 --- a/drivers/hwtracing/coresight/coresight-etm3x-core.c +++ b/drivers/hwtracing/coresight/coresight-etm3x-core.c @@ -556,14 +556,12 @@ static int etm_enable(struct coresight_device *csdev, struct perf_event *event, enum cs_mode mode) { int ret; - u32 val; struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); - val = local_cmpxchg(&drvdata->csdev->mode, CS_MODE_DISABLED, mode); - - /* Someone is already using the tracer */ - if (val) + if (!coresight_take_mode(csdev, mode)) { + /* Someone is already using the tracer */ return -EBUSY; + } switch (mode) { case CS_MODE_SYSFS: diff --git a/drivers/hwtracing/coresight/coresight-etm4x-core.c b/drivers/hwtracing/coresight/coresight-etm4x-core.c index 3480b563981c..8fca7fc379e6 100644 --- a/drivers/hwtracing/coresight/coresight-etm4x-core.c +++ b/drivers/hwtracing/coresight/coresight-etm4x-core.c @@ -840,13 +840,11 @@ static int etm4_enable(struct coresight_device *csdev, struct perf_event *event, enum cs_mode mode) { int ret; - u32 val; - val = local_cmpxchg(&csdev->mode, CS_MODE_DISABLED, mode); - - /* Someone is already using the tracer */ - if (val) + if (!coresight_take_mode(csdev, mode)) { + /* Someone is already using the tracer */ return -EBUSY; + } switch (mode) { case CS_MODE_SYSFS: diff --git a/drivers/hwtracing/coresight/coresight-stm.c b/drivers/hwtracing/coresight/coresight-stm.c index af9b21224246..80fed4c377f1 100644 --- a/drivers/hwtracing/coresight/coresight-stm.c +++ b/drivers/hwtracing/coresight/coresight-stm.c @@ -193,17 +193,15 @@ static void stm_enable_hw(struct stm_drvdata *drvdata) static int stm_enable(struct coresight_device *csdev, struct perf_event *event, enum cs_mode mode) { - u32 val; struct stm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); if (mode != CS_MODE_SYSFS) return -EINVAL; - val = local_cmpxchg(&csdev->mode, CS_MODE_DISABLED, mode); - - /* Someone is already using the tracer */ - if (val) + if (!coresight_take_mode(csdev, mode)) { + /* Someone is already using the tracer */ return -EBUSY; + } pm_runtime_get_sync(csdev->dev.parent); diff --git a/include/linux/coresight.h b/include/linux/coresight.h index c5be46d7f85c..175d184b3a1b 100644 --- a/include/linux/coresight.h +++ b/include/linux/coresight.h @@ -580,6 +580,17 @@ static inline bool coresight_is_percpu_sink(struct coresight_device *csdev) (csdev->subtype.sink_subtype == CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM); } +/* + * Atomically try to take the device and set a new mode. Returns true on + * success, false if the device is already taken by someone else. + */ +static inline bool coresight_take_mode(struct coresight_device *csdev, + enum cs_mode new_mode) +{ + return local_cmpxchg(&csdev->mode, CS_MODE_DISABLED, new_mode) == + CS_MODE_DISABLED; +} + extern struct coresight_device * coresight_register(struct coresight_desc *desc); extern void coresight_unregister(struct coresight_device *csdev); -- 2.34.1