From patchwork Tue Aug 10 12:21:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 494393 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 991C8C4338F for ; Tue, 10 Aug 2021 12:25:25 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (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 3E09361019 for ; Tue, 10 Aug 2021 12:25:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3E09361019 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id B08D1167D; Tue, 10 Aug 2021 14:24:31 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz B08D1167D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1628598321; bh=4g48p8POQn+VxHuIaxiexhbIYzob/N8ivj1Cl1AUP/M=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=racsZz/CziG0AuLpM6wTpHQ6432KZ2ZZ2ZyC4nb4pbg76mQ0DZ5Ip3dtPB6kRUilS P5NAgXUguPcl4UEnK+qtuePkGme/CBwB4iLezOywbRYddYCZNlh5lC6LOKL/DZKYIQ KiFNWRztM3P1n8rNfBclb6LmoRtEAV2lwAthJCIc= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 9F07BF804FD; Tue, 10 Aug 2021 14:22:25 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 4EF79F800FD; Tue, 10 Aug 2021 14:22:21 +0200 (CEST) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id C71F0F802A0 for ; Tue, 10 Aug 2021 14:22:04 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz C71F0F802A0 Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="cTAoK5jc"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="ZBtUlWy+" Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id A746E22052 for ; Tue, 10 Aug 2021 12:22:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1628598123; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v0KcIkEv8SYFxGqlsKcX6RCLO/gjbURTyZnxpeSaOJw=; b=cTAoK5jcKaEAHPF8rO3rv+mnzArCtxh3C+ys3yVSboxfMzfeE479yE50J5pgVK/2xGL4Th r1XENR6wVI6PscOktrEWKGg2xyYYfIYvLe7siIdbQpAMPoXci1kc+BkP0XAhttCdOMC9qR rUBJUTYZekyEwUzJ4xPVCUa/MyJnm1c= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1628598123; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v0KcIkEv8SYFxGqlsKcX6RCLO/gjbURTyZnxpeSaOJw=; b=ZBtUlWy+T+mOkVbT/GFv8PTYcMJS1UHBrI7h4C4wBOO28R9lkVgk9srNXneqDzzBJysZI2 YZKqBf9+Mp6WjaDQ== Received: from alsa1.nue.suse.com (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id 96771A3B88; Tue, 10 Aug 2021 12:22:03 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Subject: [PATCH RFC 1/5] ALSA: memalloc: Count continuous pages in vmalloc buffer handler Date: Tue, 10 Aug 2021 14:21:56 +0200 Message-Id: <20210810122200.971-2-tiwai@suse.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210810122200.971-1-tiwai@suse.de> References: <20210810122200.971-1-tiwai@suse.de> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" This is an enhancement for the SG-style page handling in vmalloc buffer handler to calculate the continuous pages. When snd_sgbuf_get_chunk_size() is called for a vmalloc buffer, currently we return only the size that fits into a single page. However, this API call is rather supposed for obtaining the continuous pages and most of vmalloc or noncontig buffers do have lots of continuous pages indeed. So, in this patch, the callback now calculates the possibly continuous pages up to the given size limit. Note that the end address in the function is calculated from the last byte, hence it's one byte shorter. This is because ofs + size can be above the actual buffer size boundary. Until now, this feature isn't really used, but it'll become useful in a later patch that adds the non-contiguous buffer type that shares the same callback function as vmalloc. Signed-off-by: Takashi Iwai --- sound/core/memalloc.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/sound/core/memalloc.c b/sound/core/memalloc.c index 1cea8cb9668f..c7c943c661e6 100644 --- a/sound/core/memalloc.c +++ b/sound/core/memalloc.c @@ -290,11 +290,13 @@ static int snd_dma_vmalloc_mmap(struct snd_dma_buffer *dmab, return remap_vmalloc_range(area, dmab->area, 0); } +#define get_vmalloc_page_addr(dmab, offset) \ + page_to_phys(vmalloc_to_page((dmab)->area + (offset))) + static dma_addr_t snd_dma_vmalloc_get_addr(struct snd_dma_buffer *dmab, size_t offset) { - return page_to_phys(vmalloc_to_page(dmab->area + offset)) + - offset % PAGE_SIZE; + return get_vmalloc_page_addr(dmab, offset) + offset % PAGE_SIZE; } static struct page *snd_dma_vmalloc_get_page(struct snd_dma_buffer *dmab, @@ -307,11 +309,23 @@ static unsigned int snd_dma_vmalloc_get_chunk_size(struct snd_dma_buffer *dmab, unsigned int ofs, unsigned int size) { - ofs %= PAGE_SIZE; - size += ofs; - if (size > PAGE_SIZE) - size = PAGE_SIZE; - return size - ofs; + unsigned int start, end; + unsigned long addr; + + start = ALIGN_DOWN(ofs, PAGE_SIZE); + end = ofs + size - 1; /* the last byte address */ + /* check page continuity */ + addr = get_vmalloc_page_addr(dmab, start); + for (;;) { + start += PAGE_SIZE; + if (start > end) + break; + addr += PAGE_SIZE; + if (get_vmalloc_page_addr(dmab, start) != addr) + return start - ofs; + } + /* ok, all on continuous pages */ + return size; } static const struct snd_malloc_ops snd_dma_vmalloc_ops = { From patchwork Tue Aug 10 12:21:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 494394 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4847C4338F for ; Tue, 10 Aug 2021 12:24:42 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (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 AD2036108C for ; Tue, 10 Aug 2021 12:24:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org AD2036108C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id C3659850; Tue, 10 Aug 2021 14:23:48 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz C3659850 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1628598278; bh=1d/yWPxQ17Q8Iwxml+sPboUMgRcOJNGMwgQ9HrCcQPU=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=TyLBbYHMWYoz5R2ediwLKbmE8ZaI/hui328q+Hr6TYP706NWxWF5wYt0FweyKqY1F J4T3SK7d81w+W6gP/zINq6MmDDMC0EckWci881F3bUt93MXrHIInBzPbH571bWxL07 GTHPk9Z288qcYNqq8T6UC5lOCs6SKMiockByFsWo= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 3BC65F804EC; Tue, 10 Aug 2021 14:22:24 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 2A4F3F8025D; Tue, 10 Aug 2021 14:22:21 +0200 (CEST) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 070F6F8016B for ; Tue, 10 Aug 2021 14:22:08 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 070F6F8016B Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="dLWbGYV8"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="M9uSO8iR" Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id C41311FE4B for ; Tue, 10 Aug 2021 12:22:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1628598123; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3OlSDU7YB2UMwzi2nG0Xk724oFxja1GqTCKV8x2q2l4=; b=dLWbGYV8zNbp+Y1Z+48x9lSTvqp8z+ffMwX6Q9X5jy/9Z9kvKkbsqHUnc9g15G/NJJpkgj goei1b7ezrrj/oO4q2M4hpiaQbQkkOBBm+wE+rJ/kubnixsB3XRCDm9MUTe9WE+9ozEi0B NvjYn0FdR4J/81rkNymbemI7DNf8meI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1628598123; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3OlSDU7YB2UMwzi2nG0Xk724oFxja1GqTCKV8x2q2l4=; b=M9uSO8iRXORiAr34x0Xk7aXxd1YkvqHUSeDw84XqxpH2No2s3uvA5rKiooGtCKQvnKO/Om zHwRZnFOo5nNySAw== Received: from alsa1.nue.suse.com (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id B2D08A3B88; Tue, 10 Aug 2021 12:22:03 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Subject: [PATCH RFC 3/5] ALSA: memalloc: Assign ops field to snd_dma_buffer Date: Tue, 10 Aug 2021 14:21:58 +0200 Message-Id: <20210810122200.971-4-tiwai@suse.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210810122200.971-1-tiwai@suse.de> References: <20210810122200.971-1-tiwai@suse.de> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" This is for performance optimization. Instead of deducing the ops from the type at each time, store the ops table directly in snd_dma_buffer struct and refer to it. The upcoming sync ops will be in a hot path, hence some optimization is required. Signed-off-by: Takashi Iwai --- include/sound/memalloc.h | 2 ++ sound/core/memalloc.c | 53 ++++++++++++++++------------------------ 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/include/sound/memalloc.h b/include/sound/memalloc.h index b197e3f431c1..f5f0d32f6d61 100644 --- a/include/sound/memalloc.h +++ b/include/sound/memalloc.h @@ -13,6 +13,7 @@ struct device; struct vm_area_struct; +struct snd_malloc_ops; /* * buffer device info @@ -55,6 +56,7 @@ struct snd_dma_buffer { dma_addr_t addr; /* physical address */ size_t bytes; /* buffer size in bytes */ void *private_data; /* private for allocator; don't touch */ + const struct snd_malloc_ops *ops; /* assigned ops */ }; /* diff --git a/sound/core/memalloc.c b/sound/core/memalloc.c index c7c943c661e6..3f42376dfb0b 100644 --- a/sound/core/memalloc.c +++ b/sound/core/memalloc.c @@ -29,15 +29,6 @@ static inline gfp_t snd_mem_get_gfp_flags(const struct snd_dma_buffer *dmab, return (__force gfp_t)(unsigned long)dmab->dev.dev; } -static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size) -{ - const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab); - - if (WARN_ON_ONCE(!ops || !ops->alloc)) - return NULL; - return ops->alloc(dmab, size); -} - /** * snd_dma_alloc_pages - allocate the buffer area according to the given type * @type: the DMA buffer type @@ -54,6 +45,7 @@ static void *__snd_dma_alloc_pages(struct snd_dma_buffer *dmab, size_t size) int snd_dma_alloc_pages(int type, struct device *device, size_t size, struct snd_dma_buffer *dmab) { + const struct snd_malloc_ops *ops; if (WARN_ON(!size)) return -ENXIO; if (WARN_ON(!dmab)) @@ -65,9 +57,15 @@ int snd_dma_alloc_pages(int type, struct device *device, size_t size, dmab->bytes = 0; dmab->addr = 0; dmab->private_data = NULL; - dmab->area = __snd_dma_alloc_pages(dmab, size); - if (!dmab->area) + ops = snd_dma_get_ops(dmab); + if (WARN_ON(!ops || !ops->alloc)) + return -ENXIO; + dmab->ops = ops; + dmab->area = ops->alloc(dmab, size); + if (!dmab->area) { + dmab->ops = NULL; return -ENOMEM; + } dmab->bytes = size; return 0; } @@ -115,10 +113,8 @@ EXPORT_SYMBOL(snd_dma_alloc_pages_fallback); */ void snd_dma_free_pages(struct snd_dma_buffer *dmab) { - const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab); - - if (ops && ops->free) - ops->free(dmab); + if (dmab->ops && dmab->ops->free) + dmab->ops->free(dmab); } EXPORT_SYMBOL(snd_dma_free_pages); @@ -176,10 +172,8 @@ EXPORT_SYMBOL_GPL(snd_devm_alloc_pages); int snd_dma_buffer_mmap(struct snd_dma_buffer *dmab, struct vm_area_struct *area) { - const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab); - - if (ops && ops->mmap) - return ops->mmap(dmab, area); + if (dmab && dmab->ops && dmab->ops->mmap) + return dmab->ops->mmap(dmab, area); else return -ENOENT; } @@ -192,10 +186,8 @@ EXPORT_SYMBOL(snd_dma_buffer_mmap); */ dma_addr_t snd_sgbuf_get_addr(struct snd_dma_buffer *dmab, size_t offset) { - const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab); - - if (ops && ops->get_addr) - return ops->get_addr(dmab, offset); + if (dmab->ops && dmab->ops->get_addr) + return dmab->ops->get_addr(dmab, offset); else return dmab->addr + offset; } @@ -208,10 +200,8 @@ EXPORT_SYMBOL(snd_sgbuf_get_addr); */ struct page *snd_sgbuf_get_page(struct snd_dma_buffer *dmab, size_t offset) { - const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab); - - if (ops && ops->get_page) - return ops->get_page(dmab, offset); + if (dmab->ops && dmab->ops->get_page) + return dmab->ops->get_page(dmab, offset); else return virt_to_page(dmab->area + offset); } @@ -227,10 +217,8 @@ EXPORT_SYMBOL(snd_sgbuf_get_page); unsigned int snd_sgbuf_get_chunk_size(struct snd_dma_buffer *dmab, unsigned int ofs, unsigned int size) { - const struct snd_malloc_ops *ops = snd_dma_get_ops(dmab); - - if (ops && ops->get_chunk_size) - return ops->get_chunk_size(dmab, ofs, size); + if (dmab->ops && dmab->ops->get_chunk_size) + return dmab->ops->get_chunk_size(dmab, ofs, size); else return size; } @@ -362,7 +350,8 @@ static void *snd_dma_iram_alloc(struct snd_dma_buffer *dmab, size_t size) * so if we fail to malloc, try to fetch memory traditionally. */ dmab->dev.type = SNDRV_DMA_TYPE_DEV; - return __snd_dma_alloc_pages(dmab, size); + dmab->ops = snd_dma_get_ops(dmab); + return dmab->ops->alloc(dmab, size); } static void snd_dma_iram_free(struct snd_dma_buffer *dmab) From patchwork Tue Aug 10 12:22:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 494395 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2647C4338F for ; Tue, 10 Aug 2021 12:23:58 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (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 3CBBD60F55 for ; Tue, 10 Aug 2021 12:23:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3CBBD60F55 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id E05551679; Tue, 10 Aug 2021 14:23:04 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz E05551679 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1628598235; bh=3Mp41M4gj7uKiJ/XrrrLT1frlQrTWL5CMHi1zJhkREc=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=EKVaGpk6qJ8RL7dTIkgPTXFoyMaHnSZf3CeIiz5odlXIN/BCrUzUIp2ouMsoKeMs3 Wu3ZL0a6NMKfNl9DygDRvwQloAlw6NMoigdi6Y5gfoGQTXtNud7piST3ToLXbh9Oaw W9XwT1L6YbGoDbPKKXPpmZTebEsLbiCg6MMaQLQ4= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 522B8F802A0; Tue, 10 Aug 2021 14:22:21 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id A8467F804CF; Tue, 10 Aug 2021 14:22:11 +0200 (CEST) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id B72EBF80245 for ; Tue, 10 Aug 2021 14:22:04 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz B72EBF80245 Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="ddmw+yYK"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="ZfkRvy47" Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id E253022058 for ; Tue, 10 Aug 2021 12:22:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1628598123; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KhgvL821dpw0CiVDE44zwkIbMEg5eBWDJz0RG3DuL04=; b=ddmw+yYKfKJb7mjhsvXXikDa6J2Rpe2XCht7uKss6Kxie8A0dKQTohu5iQzjfKqoIYJVXy s+Ti+uoGioPqv98n+TgV47pri9IVmYJpwMN3gUk3IxVlGAHXEqkcVik51Mtf92nSQlsfXi KTrXYplqxDq/EW21USGYP0cF6CkFzMM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1628598123; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KhgvL821dpw0CiVDE44zwkIbMEg5eBWDJz0RG3DuL04=; b=ZfkRvy47Puu1oP96KCpZlZN0uuK9LXDcg+FuV78oc1ZD9pIUjUZ+NUuV2pVxeKYalqqJLy K+VyppXQVztTrEDw== Received: from alsa1.nue.suse.com (alsa1.suse.de [10.160.4.42]) by relay2.suse.de (Postfix) with ESMTP id D2ACCA3B88; Tue, 10 Aug 2021 12:22:03 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Subject: [PATCH RFC 5/5] ALSA: memalloc: Support for non-coherent page allocation Date: Tue, 10 Aug 2021 14:22:00 +0200 Message-Id: <20210810122200.971-6-tiwai@suse.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210810122200.971-1-tiwai@suse.de> References: <20210810122200.971-1-tiwai@suse.de> MIME-Version: 1.0 X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" This patch adds the new non-coherent contiguous page allocation to the standard memalloc helper. Like the previous patch to add the non-contig SG-buffer support, this non-coherent type is also direction and requires the explicit sync, too. Hence the driver using this type of buffer would have to set SNDRV_PCM_INFO_EXPLICIT_SYNC flag to the PCM hardware.info as well. Signed-off-by: Takashi Iwai --- include/sound/memalloc.h | 1 + sound/core/memalloc.c | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/sound/memalloc.h b/include/sound/memalloc.h index ed54f9dfb2b3..3b03e3266e90 100644 --- a/include/sound/memalloc.h +++ b/include/sound/memalloc.h @@ -50,6 +50,7 @@ struct snd_dma_device { #endif #define SNDRV_DMA_TYPE_VMALLOC 7 /* vmalloc'ed buffer */ #define SNDRV_DMA_TYPE_NONCONTIG 8 /* non-coherent SG buffer */ +#define SNDRV_DMA_TYPE_NONCOHERENT 9 /* non-coherent buffer */ /* * info for buffer allocation diff --git a/sound/core/memalloc.c b/sound/core/memalloc.c index 5ffd356e0327..ad092986c7a8 100644 --- a/sound/core/memalloc.c +++ b/sound/core/memalloc.c @@ -539,6 +539,48 @@ static const struct snd_malloc_ops snd_dma_noncontig_ops = { .get_chunk_size = snd_dma_vmalloc_get_chunk_size, }; +/* + * Non-coherent pages allocator + */ +static void *snd_dma_noncoherent_alloc(struct snd_dma_buffer *dmab, size_t size) +{ + return dma_alloc_noncoherent(dmab->dev.dev, size, &dmab->addr, + dmab->dev.dir, DEFAULT_GFP); +} + +static void snd_dma_noncoherent_free(struct snd_dma_buffer *dmab) +{ + dma_free_noncoherent(dmab->dev.dev, dmab->bytes, dmab->area, + dmab->addr, dmab->dev.dir); +} + +static int snd_dma_noncoherent_mmap(struct snd_dma_buffer *dmab, + struct vm_area_struct *area) +{ + area->vm_page_prot = vm_get_page_prot(area->vm_flags); + return dma_mmap_pages(dmab->dev.dev, area, + area->vm_end - area->vm_start, + virt_to_page(dmab->area)); +} + +static void snd_dma_noncoherent_sync(struct snd_dma_buffer *dmab, + enum snd_dma_sync_mode mode) +{ + if (mode == SNDRV_DMA_SYNC_CPU) + dma_sync_single_for_cpu(dmab->dev.dev, dmab->addr, + dmab->bytes, dmab->dev.dir); + else + dma_sync_single_for_device(dmab->dev.dev, dmab->addr, + dmab->bytes, dmab->dev.dir); +} + +static const struct snd_malloc_ops snd_dma_noncoherent_ops = { + .alloc = snd_dma_noncoherent_alloc, + .free = snd_dma_noncoherent_free, + .mmap = snd_dma_noncoherent_mmap, + .sync = snd_dma_noncoherent_sync, +}; + #endif /* CONFIG_HAS_DMA */ /* @@ -551,6 +593,7 @@ static const struct snd_malloc_ops *dma_ops[] = { [SNDRV_DMA_TYPE_DEV] = &snd_dma_dev_ops, [SNDRV_DMA_TYPE_DEV_WC] = &snd_dma_wc_ops, [SNDRV_DMA_TYPE_NONCONTIG] = &snd_dma_noncontig_ops, + [SNDRV_DMA_TYPE_NONCOHERENT] = &snd_dma_noncoherent_ops, #ifdef CONFIG_GENERIC_ALLOCATOR [SNDRV_DMA_TYPE_DEV_IRAM] = &snd_dma_iram_ops, #endif /* CONFIG_GENERIC_ALLOCATOR */