Received: by 10.223.164.200 with SMTP id h8csp59550wrb; Sun, 5 Nov 2017 03:04:43 -0800 (PST) X-Google-Smtp-Source: ABhQp+RjpxJE7dhfGVJdjT8SML1Zyqxm8rZiNL64Ux1FuDsBHq1dCOLBewY+I0Q7OONmFWcPdHyP X-Received: by 10.98.62.81 with SMTP id l78mr12982090pfa.171.1509879883663; Sun, 05 Nov 2017 03:04:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1509879883; cv=none; d=google.com; s=arc-20160816; b=FG5gmkqkQm0JsUFl7q/L3FmMNKSYaX0kD2uHnuN/ov5tWehb/RpmDTff0Cx6nZLX7a tlw6gsdSagCdmYWDBl/g8FN5sOhvxnbDVgVg9wF6BIVRShDNJ52SSmBFmVytVfMSX4BH qumqwppzXCpLrya8vGwEISAOJHhzhKtFaes/m4Joy1sAmHyIJz5pfZpHN4IeRwmF9uVQ 9hdo+cnHBaFbyUYPvfAOIgPEsRiPgzCIEFsX2jB8ks2u942PDefwhtdXkdhRQJgANvUP yKAvRPXkJpbBS1RKCTmDKaW72hqCDgLbxyBYwdcDencbxfaQod1RFZVTkQcVrImC1WuX bbGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=hJoBWeKmKaNWAWzKY4VicMIHHm2YiR6fAay3JxLNFhI=; b=knw/HVcnWiI7QSNoiqMTqzSMBJtG5uJpYLv5yZRJcQDp8jqsxVRdDp0SC8JqljvEKw /FPsgn4asej0s1aioJfdtgzJyeL13DnQ7ndvEiuzeA74/uN6imdyRLuMneFeyn8oNIif S6z52Mk0QnQNCx6Ukz/qRPvwXSY63lkNL+2cvsbz+09qI3xleTkrNo2FbZdC3QUlf2zG cPBxn8/FN+/3MsVauiJM/9yDD5L4ZQjiyx5X7kkD7rVmXnBDOmpPuxjq85H0ZXidNJv7 x8VifmNVIxogav9m4qxeyIWHFiY9Oo4cX7TqF5O57vxZuqtsM2XGqIY58EGq2ETDKlut cX/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kapsi.fi header.s=20161220 header.b=vU6Yqjfx; 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=fail (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g3si8385084plb.209.2017.11.05.03.04.30; Sun, 05 Nov 2017 03:04:43 -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=@kapsi.fi header.s=20161220 header.b=vU6Yqjfx; 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=fail (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752786AbdKELCs (ORCPT + 96 others); Sun, 5 Nov 2017 06:02:48 -0500 Received: from mail.kapsi.fi ([91.232.154.25]:38025 "EHLO mail.kapsi.fi" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751990AbdKELCE (ORCPT ); Sun, 5 Nov 2017 06:02:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=kapsi.fi; s=20161220; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=hJoBWeKmKaNWAWzKY4VicMIHHm2YiR6fAay3JxLNFhI=; b=vU6YqjfxSxVYpBAuiYtuJTSCPtiDZuAAxDXQOACQKxaJIm87KX5zRxO/T7xq/5Fl6udV6zsHE1eASdY3M6LwpEHVyzmTNunbQU4jnbtCDs05ot53E4alN9wGk2LCOr/0V5Ca8dp0/TJdhFAXO/0jTPV8ANV4mQtgTxyN8gccL4IEn5DiCWPJnS+uDSWpbGL7iiKQE2p6/1A5SyVo71h/qWD+I5dkgeOZB7if2gsqBh9n1lC2yPfP4d1me3oLpBRJEUH11dqsYOrjZqaFztVgr6aWrwZEgDAF449eVW21DmOaMYjg8FJmE486lAVvzbicFWjWqRTedoA6s+qnNo2mRQ==; Received: from dsl-hkibng22-54f983-249.dhcp.inet.fi ([84.249.131.249] helo=localhost.localdomain) by mail.kapsi.fi with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1eBIgk-0006dR-T2; Sun, 05 Nov 2017 13:02:02 +0200 From: Mikko Perttunen To: thierry.reding@gmail.com, jonathanh@nvidia.com Cc: digetx@gmail.com, dri-devel@lists.freedesktop.org, linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org, Mikko Perttunen Subject: [PATCH 10/10] gpu: host1x: Optionally block when acquiring channel Date: Sun, 5 Nov 2017 13:01:18 +0200 Message-Id: <20171105110118.15142-11-mperttunen@nvidia.com> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171105110118.15142-1-mperttunen@nvidia.com> References: <20171105110118.15142-1-mperttunen@nvidia.com> X-SA-Exim-Connect-IP: 84.249.131.249 X-SA-Exim-Mail-From: mperttunen@nvidia.com X-SA-Exim-Scanned: No (on mail.kapsi.fi); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add an option to host1x_channel_request to interruptibly wait for a free channel. This allows IOCTLs that acquire a channel to block the userspace. Signed-off-by: Mikko Perttunen --- drivers/gpu/drm/tegra/drm.c | 9 +++++---- drivers/gpu/drm/tegra/gr2d.c | 6 +++--- drivers/gpu/drm/tegra/gr3d.c | 6 +++--- drivers/gpu/host1x/channel.c | 40 ++++++++++++++++++++++++++++++---------- drivers/gpu/host1x/channel.h | 1 + include/linux/host1x.h | 2 +- 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/drivers/gpu/drm/tegra/drm.c b/drivers/gpu/drm/tegra/drm.c index 658bc8814f38..19f77c1a76c0 100644 --- a/drivers/gpu/drm/tegra/drm.c +++ b/drivers/gpu/drm/tegra/drm.c @@ -389,7 +389,8 @@ static int host1x_waitchk_copy_from_user(struct host1x_waitchk *dest, * Request a free hardware host1x channel for this user context, or if the * context already has one, bump its refcount. * - * Returns 0 on success, or -EBUSY if there were no free hardware channels. + * Returns 0 on success, -EINTR if wait for a free channel was interrupted, + * or other error. */ int tegra_drm_context_get_channel(struct tegra_drm_context *context) { @@ -398,10 +399,10 @@ int tegra_drm_context_get_channel(struct tegra_drm_context *context) mutex_lock(&context->lock); if (context->pending_jobs == 0) { - context->channel = host1x_channel_request(client->dev); - if (!context->channel) { + context->channel = host1x_channel_request(client->dev, true); + if (IS_ERR(context->channel)) { mutex_unlock(&context->lock); - return -EBUSY; + return PTR_ERR(context->channel); } } diff --git a/drivers/gpu/drm/tegra/gr2d.c b/drivers/gpu/drm/tegra/gr2d.c index 3db3bcac48b9..c1853402f69b 100644 --- a/drivers/gpu/drm/tegra/gr2d.c +++ b/drivers/gpu/drm/tegra/gr2d.c @@ -32,9 +32,9 @@ static int gr2d_init(struct host1x_client *client) unsigned long flags = HOST1X_SYNCPT_HAS_BASE; struct gr2d *gr2d = to_gr2d(drm); - gr2d->channel = host1x_channel_request(client->dev); - if (!gr2d->channel) - return -ENOMEM; + gr2d->channel = host1x_channel_request(client->dev, false); + if (IS_ERR(gr2d->channel)) + return PTR_ERR(gr2d->channel); client->syncpts[0] = host1x_syncpt_request(client->dev, flags); if (!client->syncpts[0]) { diff --git a/drivers/gpu/drm/tegra/gr3d.c b/drivers/gpu/drm/tegra/gr3d.c index 279438342c8c..793a91d577cb 100644 --- a/drivers/gpu/drm/tegra/gr3d.c +++ b/drivers/gpu/drm/tegra/gr3d.c @@ -42,9 +42,9 @@ static int gr3d_init(struct host1x_client *client) unsigned long flags = HOST1X_SYNCPT_HAS_BASE; struct gr3d *gr3d = to_gr3d(drm); - gr3d->channel = host1x_channel_request(client->dev); - if (!gr3d->channel) - return -ENOMEM; + gr3d->channel = host1x_channel_request(client->dev, false); + if (IS_ERR(gr3d->channel)) + return PTR_ERR(gr3d->channel); client->syncpts[0] = host1x_syncpt_request(client->dev, flags); if (!client->syncpts[0]) { diff --git a/drivers/gpu/host1x/channel.c b/drivers/gpu/host1x/channel.c index 9d8cad12f9d8..eebcd51261df 100644 --- a/drivers/gpu/host1x/channel.c +++ b/drivers/gpu/host1x/channel.c @@ -43,6 +43,7 @@ int host1x_channel_list_init(struct host1x_channel_list *chlist, bitmap_zero(chlist->allocated_channels, num_channels); mutex_init(&chlist->lock); + sema_init(&chlist->sema, num_channels); return 0; } @@ -99,6 +100,8 @@ static void release_channel(struct kref *kref) host1x_cdma_deinit(&channel->cdma); clear_bit(channel->id, chlist->allocated_channels); + + up(&chlist->sema); } void host1x_channel_put(struct host1x_channel *channel) @@ -107,19 +110,30 @@ void host1x_channel_put(struct host1x_channel *channel) } EXPORT_SYMBOL(host1x_channel_put); -static struct host1x_channel *acquire_unused_channel(struct host1x *host) +static struct host1x_channel *acquire_unused_channel(struct host1x *host, + bool wait) { struct host1x_channel_list *chlist = &host->channel_list; unsigned int max_channels = host->info->nb_channels; unsigned int index; + int err; + + if (wait) { + err = down_interruptible(&chlist->sema); + if (err) + return ERR_PTR(err); + } else { + if (down_trylock(&chlist->sema)) + return ERR_PTR(-EBUSY); + } mutex_lock(&chlist->lock); index = find_first_zero_bit(chlist->allocated_channels, max_channels); - if (index >= max_channels) { + if (WARN(index >= max_channels, "failed to find free channel")) { mutex_unlock(&chlist->lock); dev_err(host->dev, "failed to find free channel\n"); - return NULL; + return ERR_PTR(-EBUSY); } chlist->channels[index].id = index; @@ -134,20 +148,26 @@ static struct host1x_channel *acquire_unused_channel(struct host1x *host) /** * host1x_channel_request() - Allocate a channel * @device: Host1x unit this channel will be used to send commands to + * @wait: Whether to wait for a free channels if all are reserved + * + * Allocates a new host1x channel for @device. If all channels are in use, + * and @wait is true, does an interruptible wait until one is available. * - * Allocates a new host1x channel for @device. May return NULL if CDMA - * initialization fails. + * If a channel was acquired, returns a pointer to it. Otherwise returns + * an error pointer with -EINTR if the wait was interrupted, -EBUSY + * if a channel could not be acquired or another error code if channel + * initialization failed. */ -struct host1x_channel *host1x_channel_request(struct device *dev) +struct host1x_channel *host1x_channel_request(struct device *dev, bool wait) { struct host1x *host = dev_get_drvdata(dev->parent); struct host1x_channel_list *chlist = &host->channel_list; struct host1x_channel *channel; int err; - channel = acquire_unused_channel(host); - if (!channel) - return NULL; + channel = acquire_unused_channel(host, wait); + if (IS_ERR(channel)) + return channel; kref_init(&channel->refcount); mutex_init(&channel->submitlock); @@ -168,6 +188,6 @@ struct host1x_channel *host1x_channel_request(struct device *dev) dev_err(dev, "failed to initialize channel\n"); - return NULL; + return ERR_PTR(err); } EXPORT_SYMBOL(host1x_channel_request); diff --git a/drivers/gpu/host1x/channel.h b/drivers/gpu/host1x/channel.h index e68a8ae9a670..1f5cf8029b62 100644 --- a/drivers/gpu/host1x/channel.h +++ b/drivers/gpu/host1x/channel.h @@ -31,6 +31,7 @@ struct host1x_channel_list { struct host1x_channel *channels; struct mutex lock; + struct semaphore sema; unsigned long *allocated_channels; }; diff --git a/include/linux/host1x.h b/include/linux/host1x.h index f931d28a68ff..2a34905d4408 100644 --- a/include/linux/host1x.h +++ b/include/linux/host1x.h @@ -171,7 +171,7 @@ u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base); struct host1x_channel; struct host1x_job; -struct host1x_channel *host1x_channel_request(struct device *dev); +struct host1x_channel *host1x_channel_request(struct device *dev, bool wait); struct host1x_channel *host1x_channel_get(struct host1x_channel *channel); void host1x_channel_put(struct host1x_channel *channel); int host1x_job_submit(struct host1x_job *job); -- 2.14.2 From 1585446982183783892@xxx Thu Nov 30 00:01:06 +0000 2017 X-GM-THRID: 1584638759287766078 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread