Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2157598lqz; Tue, 2 Apr 2024 08:47:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUGQy4kMHXO6Zy+f/oLp+BGZVdu5BM8b1qWluXS1GuusancMeW88/g5+fPEd2VFoULpp8l02kyU9crYNcnZ6K7al/cVft87JGN9ByLJMw== X-Google-Smtp-Source: AGHT+IGrw7MUjsRDMaLe9MSj/tnKKlYIBDAmxHNuyqgTzD9aOmjbfLhps9kwNb+RaK6Vndd8OIYA X-Received: by 2002:ac2:560b:0:b0:513:e63c:cfe7 with SMTP id v11-20020ac2560b000000b00513e63ccfe7mr8132266lfd.66.1712072830682; Tue, 02 Apr 2024 08:47:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712072830; cv=pass; d=google.com; s=arc-20160816; b=yyRkQxvSJ6y03/Mn6FVsXnVA8n7kUiz0Y+lykuQDQUWX9iojolYAu/VxlwdY1jC0L6 oLEosSMFSPjA4hY/5I/WRWLaQX/Ubx2VoPQrej2aBUhQP05yTZAnp6IYMBveaVp9YjnB ZHm85HakWt86cd+kGJBJ9/yovEU1gYM8+T6fG6GtyzNeXE5SyA8oUcs+Ajb+wm0BK420 KABWoDCyjRNm2MDzSc7ypb0Cc0uGjniG5e9xjUwhfU0bB/+75BAKOJVj/2mzchFkPjTK iIcbNbkj8GRnxwYDX3TVtvuqFhqcWeKFBasb0UoSUhRLeC3qrEBrIqgsKPZcGRp/ARRO liDw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=EVyp69so+oPcwcZ1LJDWArZ1MMHG1Do5yViOFYPepx4=; fh=J+88iIK2ywclVpJX68Ru02CZqMpQg2L5UDvKm6d3Rj8=; b=JPyo4DBHBrEdJ/DDyAMflKor54nxmllnLRTROvdQ3vNODC4mXd053Vt5O1ToSJ/qMK e6meflpnyMv+PjJlFmcNN2baRogbQ5eC5/3KQkA8MYQYF0+/4TD17JcNNiC+AQjeheE2 SNqLvYQDbh8E88sI+aGqG7JPjW7k6hJKMi/xSbaqSU4C2T1LBb+I8PlYCEh/UL1RnvfE 4qNcvqdPy9ar03N8oyJe8ivQaJgKWj2CX/qGwnPpjoCg4d8i19vCGO2bXxEwATnpdu16 hmKUTJu6RHoZVZLnJATl8lx8PDs1/i6uhWHbrEOh6vWsX1mRHygFxvZxh3RWR9LnnGI9 N28A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=f8c2190C; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-128273-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128273-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id t3-20020a170906178300b00a4761adde1bsi5677508eje.607.2024.04.02.08.47.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 08:47:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-128273-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=f8c2190C; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-128273-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128273-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 3C1771F25EC2 for ; Tue, 2 Apr 2024 15:47:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9B90513442E; Tue, 2 Apr 2024 15:43:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="f8c2190C" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83AF7133994; Tue, 2 Apr 2024 15:43:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712072631; cv=none; b=ajP3oxDpafUxYu0C/Wxuo4/AGQs+/NuOZoRLteD6XoUEmiV4fkYptf1vsvMIZCk2aEFrIZHLnPZeMYE21vY2XwDhBMKyjoyggsZ5HUM83CD+JmFlALGXgjEAr/HNYuskn4RjXS6AsSsdtI3JKwy67biiRiXlL2M9jZ+DNRqyusc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712072631; c=relaxed/simple; bh=RarfvLq+0S7Qq4OzdY0QupY4nd0iXB2pPjTkmq4uGLU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=suRedW8nQrPzlCHqMweZFN6MBdynbPOTB0ahH8tnD40OUeJyWS2TsEsMQjtk3Tt3H9O4Lio1l8rEzCw+YWNsQXFx2z9go6L+EFLElseSt2jFLf61JdZ8wq4gf3fNF/HHJPFx61p7vhmpXTMTFZjpP4Mrcj2xs87QhfxdGTuL0OY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f8c2190C; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A12F5C433C7; Tue, 2 Apr 2024 15:43:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712072631; bh=RarfvLq+0S7Qq4OzdY0QupY4nd0iXB2pPjTkmq4uGLU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=f8c2190Ct4RIqG7jfTaUcyNMOayYCZONs1OhBRf4HbgikqzzKoNuI60Pb1z4hUJA1 jnSi/Fe9r1tNKVdqkz370NtkPMfSkETpUv2VSeafh0wFjt8coy37FJsoLSAcEhDORd f2dB75H+qf6G0GVKZKnoSVmfJgucMk5AT74ly0lxuyen9BEXQsvVA+1Zp3RfgR6DPF B3rJvuOT1ae04dq98UUwlonx/Ti3yktYt8tupRZv0s3CQbkFqLIlnMX5vQNFOfW9hY yzS8eZDSmT0lWG9wE5vQeEA6Tfc0sJZTVTZ96NybxtyN361WF4aNQit+xgcJ6eqbB9 wL5C0UV+//gRw== Date: Tue, 2 Apr 2024 17:43:44 +0200 From: Niklas Cassel To: Frank Li Cc: rdunlap@infradead.org, hch@infradead.org, corbet@lwn.net, dmaengine@vger.kernel.org, imx@lists.linux.dev, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, lizhijian@fujitsu.com, mst@redhat.com Subject: Re: [PATCH v2 1/1] docs: dma: correct dma_set_mask() sample code Message-ID: References: <20240401174159.642998-1-Frank.Li@nxp.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240401174159.642998-1-Frank.Li@nxp.com> On Mon, Apr 01, 2024 at 01:41:59PM -0400, Frank Li wrote: > There are bunch of codes in driver like > > if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) > dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)) > > Actually it is wrong because if dma_set_mask_and_coherent(64) fails, > dma_set_mask_and_coherent(32) will fail for the same reason. > > And dma_set_mask_and_coherent(64) never returns failure. > > According to the definition of dma_set_mask(), it indicates the width of > address that device DMA can access. If it can access 64-bit address, it > must access 32-bit address inherently. So only need set biggest address > width. > > See below code fragment: > > dma_set_mask(mask) > { > mask = (dma_addr_t)mask; > > if (!dev->dma_mask || !dma_supported(dev, mask)) > return -EIO; > > arch_dma_set_mask(dev, mask); > *dev->dma_mask = mask; > return 0; > } > > dma_supported() will call dma_direct_supported or iommux's dma_supported > call back function. > > int dma_direct_supported(struct device *dev, u64 mask) > { > u64 min_mask = (max_pfn - 1) << PAGE_SHIFT; > > /* > * Because 32-bit DMA masks are so common we expect every architecture > * to be able to satisfy them - either by not supporting more physical > * memory, or by providing a ZONE_DMA32. If neither is the case, the > * architecture needs to use an IOMMU instead of the direct mapping. > */ > if (mask >= DMA_BIT_MASK(32)) > return 1; > > ... > } > > The iommux's dma_supported() actually means iommu requires devices's > minimized dma capability. > > An example: > > static int sba_dma_supported( struct device *dev, u64 mask)() > { > ... > * check if mask is >= than the current max IO Virt Address > * The max IO Virt address will *always* < 30 bits. > */ > return((int)(mask >= (ioc->ibase - 1 + > (ioc->pdir_size / sizeof(u64) * IOVP_SIZE) ))); > ... > } > > 1 means supported. 0 means unsupported. > > Correct document to make it more clear and provide correct sample code. > > Signed-off-by: Frank Li > --- > > Notes: > Change from v1 to v2: > - fixed typo, review by Randy Dunlap > > Documentation/core-api/dma-api-howto.rst | 24 ++++++++++++++++++++++-- > 1 file changed, 22 insertions(+), 2 deletions(-) > > diff --git a/Documentation/core-api/dma-api-howto.rst b/Documentation/core-api/dma-api-howto.rst > index e8a55f9d61dbc..5f6a7d86b6bc2 100644 > --- a/Documentation/core-api/dma-api-howto.rst > +++ b/Documentation/core-api/dma-api-howto.rst > @@ -203,13 +203,33 @@ setting the DMA mask fails. In this manner, if a user of your driver reports > that performance is bad or that the device is not even detected, you can ask > them for the kernel messages to find out exactly why. > > -The standard 64-bit addressing device would do something like this:: > +The 24-bit addressing device would do something like this:: > > - if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) { > + if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(24))) { > dev_warn(dev, "mydev: No suitable DMA available\n"); > goto ignore_this_device; > } > > +The standard 64-bit addressing device would do something like this:: > + > + dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) > + > +dma_set_mask_and_coherent() never return fail when DMA_BIT_MASK(64). Typical > +error code like:: > + > + /* Wrong code */ > + if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) > + dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)) > + > +dma_set_mask_and_coherent() will never return failure when bigger then 32. Nit: s/then/than/ > +So typical code like:: > + > + /* Recommended code */ > + if (support_64bit) > + dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); > + else > + dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); > + > If the device only supports 32-bit addressing for descriptors in the > coherent allocations, but supports full 64-bits for streaming mappings > it would look like this:: > -- > 2.34.1 >