From patchwork Thu Nov 28 15:37:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 11266283 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 AFDD91393 for ; Thu, 28 Nov 2019 15:37:52 +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 9790621781 for ; Thu, 28 Nov 2019 15:37:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9790621781 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 9192889B3B; Thu, 28 Nov 2019 15:37:51 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by gabe.freedesktop.org (Postfix) with ESMTPS id AB48A89A32 for ; Thu, 28 Nov 2019 15:37:48 +0000 (UTC) Received: by mail-wm1-x341.google.com with SMTP id j18so7697122wmk.1 for ; Thu, 28 Nov 2019 07:37:48 -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=dNaoo3mI+xfZVrHcwdtziDtvG7sdJdHcDl5YU1IbCPI=; b=SvMkvqhfJeu/3qvtl2nshZ1xUGzOahFkCOYBJhWMnFEx/UH/g6PIz8g9xENQB9LzwF +7tB5BMAjlAO6tY/01tbw/W3RR1aktwWsCYIWiHiWtAJ+0oeiG0ji/AZG7n0W4Iy8IeB EwFoABqdN3b53dDBE9rkxx/l7AN6c5gAsrpXsQnsoo06hDRXPUpX7sipr5Dw9b5onGa1 6d0EB+flIlVnxcQ/tgm4D5RPdN3mv0zU1KXVYAvFBpV793t5xTKe7MSehWPORdord35X nNazTt7zT1DUbDNtbYtO9WvSTvXxwao3dd5gA33olMq9+k+CxskQd/SvLgg5Q0FiPhxD kVsw== X-Gm-Message-State: APjAAAXTaGr5l0hzNVLgoMGbC/K9YjmIMGGp6zkDMOJIUh97h6rPhS73 VAhJGRQo4Qy7P5pdZ2ImwbY= X-Google-Smtp-Source: APXvYqzoiFXF+tYKLyY+hdee6OAJkYHDs/CjucImZ7iJ1DslaZ2xMNDMpFVD7kmM4nZlKtPsYeO6fw== X-Received: by 2002:a1c:4e1a:: with SMTP id g26mr10460607wmh.138.1574955467207; Thu, 28 Nov 2019 07:37:47 -0800 (PST) Received: from localhost (pD9E518ED.dip0.t-ipconnect.de. [217.229.24.237]) by smtp.gmail.com with ESMTPSA id a3sm1323242wmh.5.2019.11.28.07.37.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2019 07:37:46 -0800 (PST) From: Thierry Reding To: Thierry Reding Subject: [PATCH 2/9] drm/tegra: gem: Properly pin imported buffers Date: Thu, 28 Nov 2019 16:37:34 +0100 Message-Id: <20191128153741.2380419-3-thierry.reding@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191128153741.2380419-1-thierry.reding@gmail.com> References: <20191128153741.2380419-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=dNaoo3mI+xfZVrHcwdtziDtvG7sdJdHcDl5YU1IbCPI=; b=A02Ea3PDZO6vcgy0N6CnvrNeKVCYmC8EdeKZvajIZfa2c6r+VRck/zUsN5UwHpSuRM X7kx5yVWo3NcbuDOVOT9xjkUoae6pXA0AUpUyJPM2K7JEiM08ZmpqnOjIV9LHdWauIs9 vrK0hP2mbyQ+Xaz5I0RGa0L/aYEMWoBqcUDXknB4wJN6J5G3xJLlpT9i9HRTpPF0Bl/o bPqsjM29bzR0hfJoqgF4Rpd2EjWUm06ai/Rb+hocVj8fUpotxuEL2jM5a0zbbMfY9EI1 9Npvr+rAgeuIooH+BXmOWoKNfFzjKJez05QTGBrq33SjFc2GIw9Sqr10j21U1XbjInOP peoQ== 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 --- 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)