From patchwork Tue Dec 3 16:19:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 11271541 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0840C138D for ; Tue, 3 Dec 2019 16:19:23 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E552C2080A for ; Tue, 3 Dec 2019 16:19:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E552C2080A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=dri-devel-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C2EE36E83F; Tue, 3 Dec 2019 16:19:21 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by gabe.freedesktop.org (Postfix) with ESMTPS id B76B16E83F for ; Tue, 3 Dec 2019 16:19:20 +0000 (UTC) Received: by mail-wr1-x442.google.com with SMTP id j42so4344722wrj.12 for ; Tue, 03 Dec 2019 08:19:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ak02DWSi16xtOh2ECFAWgXweAP5WExvaHVwWZOvyNnw=; b=dbHZz7SgehlCcyxyW3CzS+N9we4F3QbrIcLBfyIb0yrXVp7DYyDu9xK9+cF3LKPI6v 2BhrAHQIAFpyU9MnVj9mFSKqP0qvEQpqB/+O9RpvY5dqHr3T/ZQwRJKbGCH7X+jGeYJ6 uYIOR2n+lQeua8wjrkKsRvn5o2XZRmUS29pxG9mSheJ+c0jW/otDx+DeE7IoVys0N5gv JtBKXr05+c1/Mro+dO8l4y/XF5p6fkmwqfT9ey36yXLj8MFC/rzqDKHTZP+JAYDEJdYm 4t01aQjBIfUkQhlxATG7XurzeqLSOwJbIWHeRNZsTzhNKpKy8dfxKXVnbvHYv/JYufWV tHvg== X-Gm-Message-State: APjAAAXyjWbqvt/0LjIY196Nu6e4WqZ6iMweN2v+rlDNoDRbV5GNVJDT DvwnMIqEYtSUTo/bGk4pyUw= X-Google-Smtp-Source: APXvYqxX2J/L0HA0d2WUetE0tWp+6QJNxQ50KMEFOyZXtSl+Lp5vT6IUI4TcSqgDC50CFxE2aJTYvw== X-Received: by 2002:a05:6000:12c9:: with SMTP id l9mr6434660wrx.304.1575389959171; Tue, 03 Dec 2019 08:19:19 -0800 (PST) Received: from localhost (pD9E518ED.dip0.t-ipconnect.de. [217.229.24.237]) by smtp.gmail.com with ESMTPSA id h2sm4386670wrt.45.2019.12.03.08.19.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Dec 2019 08:19:18 -0800 (PST) From: Thierry Reding To: Thierry Reding Subject: [PATCH v2 2/9] drm/tegra: gem: Properly pin imported buffers Date: Tue, 3 Dec 2019 17:19:07 +0100 Message-Id: <20191203161914.1312555-2-thierry.reding@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191203161914.1312555-1-thierry.reding@gmail.com> References: <20191203161914.1312555-1-thierry.reding@gmail.com> MIME-Version: 1.0 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ak02DWSi16xtOh2ECFAWgXweAP5WExvaHVwWZOvyNnw=; b=RL3vCTki5+cR7UoU49Qhs4YMV6dygSXVkmjcJrGmlyiUJg7I40PArNtHDdaed3Idsp jqhgLSdkixx8XnwBSZnJBed9WhVY8qumrBkEf771dmfwvNKAlqBseA1KyW/3io7flDmX ylaS17NtXPPiCC45yXaVsWKHcYhofEuygZQ5BH1a+SN+ekT+ttVeQ9V159fDpN+Zw7Lx sHcJ/QPG4K/Lb6Tnkpm6EVre2n0cf96UTeXShV5FhWjBPZCahz5RfM9RgNjJXaIfJpWr 4t09araZKxkwBgt8EB6H4p1ADwfkSsSbtC6yvhDkM2IRriGGs5LBDDNkObPp2it7sCb1 Apyw== X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-tegra@vger.kernel.org, dri-devel@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Thierry Reding Buffers that are imported from a DMA-BUF don't have pages allocated with them. At the same time an SG table for them can't be derived using the DMA API helpers because the necessary information doesn't exist. However there's already an SG table that was created during import, so this can simply be duplicated. Signed-off-by: Thierry Reding --- Note that for proper DMA-BUF usage, the display controllers should each attach to the DMA-BUF for imported buffers. That's a fairly large change and will be sent out as a follow-up, targetting v5.6. This patch is a fix for a recent regression in v5.5-rc1. drivers/gpu/drm/tegra/gem.c | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/drivers/gpu/drm/tegra/gem.c b/drivers/gpu/drm/tegra/gem.c index 746dae32c484..6dfad56eee2b 100644 --- a/drivers/gpu/drm/tegra/gem.c +++ b/drivers/gpu/drm/tegra/gem.c @@ -27,6 +27,29 @@ static void tegra_bo_put(struct host1x_bo *bo) drm_gem_object_put_unlocked(&obj->gem); } +/* XXX move this into lib/scatterlist.c? */ +static int sg_alloc_table_from_sg(struct sg_table *sgt, struct scatterlist *sg, + unsigned int nents, gfp_t gfp_mask) +{ + struct scatterlist *dst; + unsigned int i; + int err; + + err = sg_alloc_table(sgt, nents, gfp_mask); + if (err < 0) + return err; + + dst = sgt->sgl; + + for (i = 0; i < nents; i++) { + sg_set_page(dst, sg_page(sg), sg->length, 0); + dst = sg_next(dst); + sg = sg_next(sg); + } + + return 0; +} + static struct sg_table *tegra_bo_pin(struct device *dev, struct host1x_bo *bo, dma_addr_t *phys) { @@ -52,11 +75,31 @@ static struct sg_table *tegra_bo_pin(struct device *dev, struct host1x_bo *bo, return ERR_PTR(-ENOMEM); if (obj->pages) { + /* + * If the buffer object was allocated from the explicit IOMMU + * API code paths, construct an SG table from the pages. + */ err = sg_alloc_table_from_pages(sgt, obj->pages, obj->num_pages, 0, obj->gem.size, GFP_KERNEL); if (err < 0) goto free; + } else if (obj->sgt) { + /* + * If the buffer object already has an SG table but no pages + * were allocated for it, it means the buffer was imported and + * the SG table needs to be copied to avoid overwriting any + * other potential users of the original SG table. + */ + err = sg_alloc_table_from_sg(sgt, obj->sgt->sgl, obj->sgt->nents, + GFP_KERNEL); + if (err < 0) + goto free; } else { + /* + * If the buffer object had no pages allocated and if it was + * not imported, it had to be allocated with the DMA API, so + * the DMA API helper can be used. + */ err = dma_get_sgtable(dev, sgt, obj->vaddr, obj->iova, obj->gem.size); if (err < 0)