From patchwork Thu Oct 13 18:12:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Jernej_=C5=A0krabec?= X-Patchwork-Id: 13006275 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2B260C4332F for ; Thu, 13 Oct 2022 18:15:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=LdQSTxWYuBa6gLwyjQTJf7succxt5Kz5uWUOYY+8N4U=; b=r2guIKfve4QWfi QsWotGIql9cvu70eX5yvoBSWJ3xonIaSuMrHi0g1i6DGN8cRmBZU9Q1AfI8plIiOEZAbnQpQNYMJj NJODKxOlOXH8YeyHuoLjSDCkh+mySo75ZsUMALizdeDKETjJbbFV4QSLksW9oKU6fSHtK/tPrAyi4 UtUS6KwEYmsjwPDFtRRB/hdfrek3rUP047JxRnpKpLZz3x1vVl3TEiX8/uNp7Hen5mejJrz5MBvl4 hZ3R+P/sEz0cpbo6bOqbDuWUlYulxW6IKTvis6+DtEHzGtEUH9BDRJnnZPNxEwR3pN0cA5lCfi1u9 xgJse8t+jGD2C/TerfHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oj2iW-00Cddk-Jc; Thu, 13 Oct 2022 18:14:01 +0000 Received: from mail-ej1-x62e.google.com ([2a00:1450:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oj2hT-00CdFY-9D for linux-arm-kernel@lists.infradead.org; Thu, 13 Oct 2022 18:12:58 +0000 Received: by mail-ej1-x62e.google.com with SMTP id 13so5693332ejn.3 for ; Thu, 13 Oct 2022 11:12:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=G/wqfyJKTZ7wwUwifWDTzmNazpZHMfc05QXeSM6TUXI=; b=HaWB05RUzvGfW6+QiMaqb8b0VOKKAaUqAEvziPp0WVrpvdqnF7t9V7XIW/Kdd2CgTV MWJQpXnB4lJPuCjoPHK4oXZ2a5OmmlJ6mMvRxsbnC+3saJfPtSps5BwSki8CFQMHRPjc aDGbMFxIME7O4mmOfGRwjI3kPeu7DB/u64DMUqpxcNLaMBT+EAwQGRC0ySMbeNQ02CKT 0slQHRidRJ/khi3Tf4mcKVVW7Fdct7kH+myaom2zNvNGPDxIlJv7Xnh/kM8OaXTlNcHJ avaPrzAqSGIvo/rdV72eaY6xHH/dvt2g8Slr/UEBbbSwTOQqUJKa7BoJeS5+rc2+i9by vLYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=G/wqfyJKTZ7wwUwifWDTzmNazpZHMfc05QXeSM6TUXI=; b=ReOZThwiGKRytjswZdUzei4rDQCbqjIxzD2jNvGGRAucMU5Zaya6STHKp0cu9ZlFR7 VNtha9XBnkhnVxMILayO/veAAQV0EdN+IN5BVeCzV6Jx6Q1QnlFowVSQommdD9MLGusO 79FsVInWsEl1xBLwwYzTF0s90OlSdsRML8NaTCrkgb5uewmFfnWnexhEe6Lrsbw8UORL LJyroJVkMSXSh1Xh9ZkEXwU6ZSueDh3yaS3cBFDT/fQycFm2XvJNrxP/xThVUPrDJS0P UrtUAZ2ekVBwd8379AA/cUQBRlTIEGzN+gGLHu9XEhRDHqV1PzwU9hZ6Ht34DeLA83+e 22uw== X-Gm-Message-State: ACrzQf3SNvETzrzPvfs3Tjjm0x7E8+NiowCPrhvX2J7BHkE7ty36U51e eBCmtBPEJavTT0o7NsRcypa/dJXsds3qD1yz X-Google-Smtp-Source: AMsMyM4e5Qp7lIwnRmj6eEtIo1d/yAMzeBAPyESkz/ITsT3lYxT+Mt/6bwUGY7gp+tSLkj/QxDDqXw== X-Received: by 2002:a17:907:75ed:b0:78d:97ed:2894 with SMTP id jz13-20020a17090775ed00b0078d97ed2894mr698352ejc.739.1665684770605; Thu, 13 Oct 2022 11:12:50 -0700 (PDT) Received: from kista.localdomain (82-149-19-102.dynamic.telemach.net. [82.149.19.102]) by smtp.gmail.com with ESMTPSA id ku15-20020a170907788f00b0078b551d2fa3sm211109ejc.103.2022.10.13.11.12.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 11:12:50 -0700 (PDT) From: Jernej Skrabec To: maxime@cerno.tech, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, wens@csie.org, samuel@sholland.org Cc: iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Jernej Skrabec Subject: [PATCH 5/5] iommu/sun50i: Invalidate iova at map and unmap Date: Thu, 13 Oct 2022 20:12:20 +0200 Message-Id: <20221013181221.3247429-6-jernej.skrabec@gmail.com> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221013181221.3247429-1-jernej.skrabec@gmail.com> References: <20221013181221.3247429-1-jernej.skrabec@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221013_111255_394562_AD43CD35 X-CRM114-Status: GOOD ( 10.44 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Mapped and unmapped iova addresses needs to be invalidated immediately or otherwise they might or might not work when used by master device. This was discovered when running video decoder conformity test with Cedrus. Some videos were now and then decoded incorrectly and generated page faults. Fixes: 4100b8c229b3 ("iommu: Add Allwinner H6 IOMMU driver") Signed-off-by: Jernej Skrabec --- drivers/iommu/sun50i-iommu.c | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/drivers/iommu/sun50i-iommu.c b/drivers/iommu/sun50i-iommu.c index 7c3b2ac552da..21e47ce6946a 100644 --- a/drivers/iommu/sun50i-iommu.c +++ b/drivers/iommu/sun50i-iommu.c @@ -518,6 +518,53 @@ static u32 *sun50i_dte_get_page_table(struct sun50i_iommu_domain *sun50i_domain, return page_table; } +static void sun50i_iommu_zap_iova(struct sun50i_iommu *iommu, unsigned long iova) +{ + unsigned long flags; + u32 reg; + int ret; + + spin_lock_irqsave(&iommu->iommu_lock, flags); + + iommu_write(iommu, IOMMU_AUTO_GATING_REG, 0); + + iommu_write(iommu, IOMMU_TLB_IVLD_ADDR_REG, iova); + iommu_write(iommu, IOMMU_TLB_IVLD_ADDR_MASK_REG, GENMASK(11, 0)); + iommu_write(iommu, IOMMU_TLB_IVLD_ENABLE_REG, IOMMU_TLB_IVLD_ENABLE_ENABLE); + + ret = readl_poll_timeout_atomic(iommu->base + IOMMU_TLB_IVLD_ENABLE_REG, + reg, !reg, 1, 2000); + if (ret) + dev_warn(iommu->dev, "TLB invalidation timed out!\n"); + + iommu_write(iommu, IOMMU_AUTO_GATING_REG, IOMMU_AUTO_GATING_ENABLE); + + spin_unlock_irqrestore(&iommu->iommu_lock, flags); +} + +static void sun50i_iommu_zap_ptw_cache(struct sun50i_iommu *iommu, unsigned long iova) +{ + unsigned long flags; + u32 reg; + int ret; + + spin_lock_irqsave(&iommu->iommu_lock, flags); + + iommu_write(iommu, IOMMU_AUTO_GATING_REG, 0); + + iommu_write(iommu, IOMMU_PC_IVLD_ADDR_REG, iova); + iommu_write(iommu, IOMMU_PC_IVLD_ENABLE_REG, IOMMU_PC_IVLD_ENABLE_ENABLE); + + ret = readl_poll_timeout_atomic(iommu->base + IOMMU_PC_IVLD_ENABLE_REG, + reg, !reg, 1, 2000); + if (ret) + dev_warn(iommu->dev, "PTW cache invalidation timed out!\n"); + + iommu_write(iommu, IOMMU_AUTO_GATING_REG, IOMMU_AUTO_GATING_ENABLE); + + spin_unlock_irqrestore(&iommu->iommu_lock, flags); +} + static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova, phys_addr_t paddr, size_t size, int prot, gfp_t gfp) { @@ -546,6 +593,8 @@ static int sun50i_iommu_map(struct iommu_domain *domain, unsigned long iova, *pte_addr = sun50i_mk_pte(paddr, prot); sun50i_table_flush(sun50i_domain, pte_addr, 1); + sun50i_iommu_zap_iova(iommu, iova); + sun50i_iommu_zap_ptw_cache(iommu, iova); out: return ret; @@ -571,6 +620,8 @@ static size_t sun50i_iommu_unmap(struct iommu_domain *domain, unsigned long iova memset(pte_addr, 0, sizeof(*pte_addr)); sun50i_table_flush(sun50i_domain, pte_addr, 1); + sun50i_iommu_zap_iova(sun50i_domain->iommu, iova); + sun50i_iommu_zap_ptw_cache(sun50i_domain->iommu, iova); return SZ_4K; }