From nobody Sat Nov 30 00:39:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=chromium.org ARC-Seal: i=1; a=rsa-sha256; t=1620640358; cv=none; d=zohomail.com; s=zohoarc; b=hWiII4B4j/NiKFH++wuD+myXWKUDeF1LXMRDjwj7kbQO1nA7g81SWah/VPKZzQ8EA6a5Ahu3d7xYt4YDo2neyaOQeo1DytOV+qXn1rxqBUEPSwtOPD9gGCwYkrEiIzNRI/JLdVTQI9oZ4fQDHpbQ6CbkNYZZuoqnEnP74BREI8Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620640358; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3YaoLVJ/BlS2jGrFuaf+HAqZru6rV2ab/VHFDnxpz5s=; b=gnB1ZKpdAR/dXHZ7Eh3K0GwzUpoQDVtwLdavyqK39SmKP70Vc0Ai/n2Jdrrz1BizEXYxELYSK5rAKe3BaJfS9XqeIbU/Se4Aswi1CbyccMcWNW1IAVEA3m/SWk9UEuJHG++Hh1R57y2IEP2xiTZzhCjy3DVMCu+/B6b3MQLqCXk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1620640358405689.3706590818757; Mon, 10 May 2021 02:52:38 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.125089.235546 (Exim 4.92) (envelope-from ) id 1lg2aP-0004dv-SE; Mon, 10 May 2021 09:52:25 +0000 Received: by outflank-mailman (output) from mailman id 125089.235546; Mon, 10 May 2021 09:52:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lg2aP-0004dg-Ou; Mon, 10 May 2021 09:52:25 +0000 Received: by outflank-mailman (input) for mailman id 125089; Mon, 10 May 2021 09:52:25 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lg2aP-0004bT-3d for xen-devel@lists.xenproject.org; Mon, 10 May 2021 09:52:25 +0000 Received: from mail-pl1-x62b.google.com (unknown [2607:f8b0:4864:20::62b]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f1a4f09b-a12b-4939-8f69-9d6128ba80a4; Mon, 10 May 2021 09:52:24 +0000 (UTC) Received: by mail-pl1-x62b.google.com with SMTP id n16so8860989plf.7 for ; Mon, 10 May 2021 02:52:24 -0700 (PDT) Received: from localhost ([2401:fa00:95:205:a524:abe8:94e3:5601]) by smtp.gmail.com with UTF8SMTPSA id f6sm10227287pfe.74.2021.05.10.02.52.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 May 2021 02:52:23 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: f1a4f09b-a12b-4939-8f69-9d6128ba80a4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3YaoLVJ/BlS2jGrFuaf+HAqZru6rV2ab/VHFDnxpz5s=; b=I10YUhrVFYEnBb151sjznBCAywSiwwuXX1ra65r+6Dp7kLXlkJTjZj64MC39DrflfN L6B6coAmKhXT3uzYOIMy3hHP2AqrBakoEoGo6H/hyImCZ+Vs9dEj0AdH5ph0QtkKY+Bg SjBGhbtFT0tOcMKvv+2FyMSaO8PoMl7ClMI8s= 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=3YaoLVJ/BlS2jGrFuaf+HAqZru6rV2ab/VHFDnxpz5s=; b=FoO2R8++42yVAfcdx2QNNhUzxNPEjSup10L064MRyPl794OWeI+uGWf3f4vikhPeiE tloW8gqVHLO2Y5UvsVOsrBbXQMLbvMo3s1U6uu9PXKYyGFQzZ+AepEamAaicfi9VaVwQ L3uKjCqAYeuyqZVvXBsJjjFtLlD65IhelcjJXR+vscIK0zIRdOXnSqgQPatX2au5wBaj tPwcJufFPZMSYnb+21ZnZx4RYhsyTiTLjbm5Eb89bnnvIqxHFxG/jD12mbh1i4EFKdUh L/hn7Rmj3Ct6jiVqWE3lNP4wdjGtnLe58QjdhDVVjFnM/kPDDBhl8lrl+54TJFd9EXqz Tzfg== X-Gm-Message-State: AOAM530kah28iH2rdaYfQu9WijEt894fMyGQrfh47tz8YLssLGKhDjYC 4xtKlcEyepzflozfAfouGhMFRQ== X-Google-Smtp-Source: ABdhPJyvBg/0ILrSmUeqEzW7Fk7bklgK3teEOUZ3TKSkxe3vq+G4eTcWybVNarpL8KZA9dCjKF/+aQ== X-Received: by 2002:a17:90a:246:: with SMTP id t6mr27381833pje.228.1620640343682; Mon, 10 May 2021 02:52:23 -0700 (PDT) From: Claire Chang To: Rob Herring , mpe@ellerman.id.au, Joerg Roedel , Will Deacon , Frank Rowand , Konrad Rzeszutek Wilk , boris.ostrovsky@oracle.com, jgross@suse.com, Christoph Hellwig , Marek Szyprowski Cc: benh@kernel.crashing.org, paulus@samba.org, "list@263.net:IOMMU DRIVERS" , sstabellini@kernel.org, Robin Murphy , grant.likely@arm.com, xypron.glpk@gmx.de, Thierry Reding , mingo@kernel.org, bauerman@linux.ibm.com, peterz@infradead.org, Greg KH , Saravana Kannan , "Rafael J . Wysocki" , heikki.krogerus@linux.intel.com, Andy Shevchenko , Randy Dunlap , Dan Williams , Bartosz Golaszewski , linux-devicetree , lkml , linuxppc-dev@lists.ozlabs.org, xen-devel@lists.xenproject.org, Nicolas Boichat , Jim Quinlan , tfiga@chromium.org, bskeggs@redhat.com, bhelgaas@google.com, chris@chris-wilson.co.uk, tientzu@chromium.org, daniel@ffwll.ch, airlied@linux.ie, dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, jani.nikula@linux.intel.com, jxgao@google.com, joonas.lahtinen@linux.intel.com, linux-pci@vger.kernel.org, maarten.lankhorst@linux.intel.com, matthew.auld@intel.com, nouveau@lists.freedesktop.org, rodrigo.vivi@intel.com, thomas.hellstrom@linux.intel.com Subject: [PATCH v6 12/15] swiotlb: Add restricted DMA alloc/free support. Date: Mon, 10 May 2021 17:50:23 +0800 Message-Id: <20210510095026.3477496-13-tientzu@chromium.org> X-Mailer: git-send-email 2.31.1.607.g51e8a6a459-goog In-Reply-To: <20210510095026.3477496-1-tientzu@chromium.org> References: <20210510095026.3477496-1-tientzu@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @chromium.org) Content-Type: text/plain; charset="utf-8" Add the functions, swiotlb_{alloc,free} to support the memory allocation from restricted DMA pool. Signed-off-by: Claire Chang --- include/linux/swiotlb.h | 4 ++++ kernel/dma/swiotlb.c | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index 0c5a18d9cf89..e8cf49bd90c5 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -134,6 +134,10 @@ unsigned int swiotlb_max_segment(void); size_t swiotlb_max_mapping_size(struct device *dev); bool is_swiotlb_active(struct device *dev); void __init swiotlb_adjust_size(unsigned long size); +#ifdef CONFIG_DMA_RESTRICTED_POOL +struct page *swiotlb_alloc(struct device *dev, size_t size); +bool swiotlb_free(struct device *dev, struct page *page, size_t size); +#endif /* CONFIG_DMA_RESTRICTED_POOL */ #else #define swiotlb_force SWIOTLB_NO_FORCE static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index fa11787e4b95..d99d5f902259 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -457,8 +457,9 @@ static int find_slots(struct device *dev, phys_addr_t o= rig_addr, =20 index =3D wrap =3D wrap_index(mem, ALIGN(mem->index, stride)); do { - if ((slot_addr(tbl_dma_addr, index) & iotlb_align_mask) !=3D - (orig_addr & iotlb_align_mask)) { + if (orig_addr && + (slot_addr(tbl_dma_addr, index) & iotlb_align_mask) !=3D + (orig_addr & iotlb_align_mask)) { index =3D wrap_index(mem, index + 1); continue; } @@ -701,6 +702,36 @@ late_initcall(swiotlb_create_default_debugfs); #endif =20 #ifdef CONFIG_DMA_RESTRICTED_POOL +struct page *swiotlb_alloc(struct device *dev, size_t size) +{ + struct io_tlb_mem *mem =3D dev->dma_io_tlb_mem; + phys_addr_t tlb_addr; + int index; + + if (!mem) + return NULL; + + index =3D find_slots(dev, 0, size); + if (index =3D=3D -1) + return NULL; + + tlb_addr =3D slot_addr(mem->start, index); + + return pfn_to_page(PFN_DOWN(tlb_addr)); +} + +bool swiotlb_free(struct device *dev, struct page *page, size_t size) +{ + phys_addr_t tlb_addr =3D page_to_phys(page); + + if (!is_swiotlb_buffer(dev, tlb_addr)) + return false; + + release_slots(dev, tlb_addr); + + return true; +} + static int rmem_swiotlb_device_init(struct reserved_mem *rmem, struct device *dev) { --=20 2.31.1.607.g51e8a6a459-goog