Received: by 10.223.176.46 with SMTP id f43csp214189wra; Tue, 23 Jan 2018 19:27:10 -0800 (PST) X-Google-Smtp-Source: AH8x226bpM5tZIymEBttCsgSIl5hbu4Sd0835iOE1+qBsD4hioo30GEewyomh28w6F5SHRZ8Jkbz X-Received: by 10.99.123.74 with SMTP id k10mr9630051pgn.165.1516764429892; Tue, 23 Jan 2018 19:27:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516764429; cv=none; d=google.com; s=arc-20160816; b=QmdSkz/BTlr9pZtJmMFSUeDgalaUGaOrdKM7HgyH0uK0f74iCorpe9O43utZbdXFuQ IpEalHBEx1adNQVCR6dEwNGlYjolINSu8vLyCUsLk48EwhneJISZ0IOVl+MJmsnb/Y+U rP0+g0K1lduWseAGusjSuMpyfBOiqF8arHcmWeBZpZJ6rDctcZ6b21mUE++CKvBwRsFm Y3HfFBYSXIgK0d2ZromJpMiCxJIumPE/+uNpRXYzVP9dmpLroi/+qu3qpseo+kOK7Lu5 YROu3mPgk2DgRvUzDgNZwsSV8VncuBdX38aMzxX4glSHT9yxx2N9QdsqdDIaH2ui/i9z cRPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date :arc-authentication-results; bh=Nh7fA8IefjTJKRsF8bRnGrQGTr4k2zWlpImYoEjmpOA=; b=OnU15kPxKvY/n7RL93UvnADqON9CSP1e4GqTPA/Y2C+WTSlnArsnjXzYNVx8Btigce sYy+SbN+/8jTrqvs9omftdx7OCzBZSXdH3KIvROlea58bzHD0HGhnb3WK3LnCfgrwxRC ziX25YVC9I4bB3JZ71KSD1WbPrWcMkc+slkdiMM6naKyLgaOgm4WOL+Echn+5hJQYBiS oePslWRujsAdhqGiMTOtytTY+yeUbkUV1fmbQzvipbFM8qiD7vgWE2ObuMg6F8fruZi3 NioTW0fzsiafEMZrjxoOArqT7ADrLfBEQHtlip8mQuCbrsv6IoG2GL0TAH3PA9yCHm0A XeIA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g15-v6si5534221pli.392.2018.01.23.19.26.56; Tue, 23 Jan 2018 19:27:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752529AbeAXDZV (ORCPT + 99 others); Tue, 23 Jan 2018 22:25:21 -0500 Received: from mx1.redhat.com ([209.132.183.28]:57616 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752489AbeAXDZU (ORCPT ); Tue, 23 Jan 2018 22:25:20 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3330880F75; Wed, 24 Jan 2018 03:25:20 +0000 (UTC) Received: from xz-mi (ovpn-12-154.pek2.redhat.com [10.72.12.154]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7996F67678; Wed, 24 Jan 2018 03:25:08 +0000 (UTC) Date: Wed, 24 Jan 2018 11:25:05 +0800 From: Peter Xu To: =?utf-8?Q?Marc-Andr=C3=A9?= Lureau Cc: linux-kernel@vger.kernel.org, bhe@redhat.com, slp@redhat.com, mst@redhat.com, somlo@cmu.edu, xiaolong.ye@intel.com, qemu-devel@nongnu.org Subject: Re: [PATCH v10 2/4] fw_cfg: do DMA read operation Message-ID: <20180124032505.GD8001@xz-mi> References: <20180123164041.30339-1-marcandre.lureau@redhat.com> <20180123164041.30339-3-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20180123164041.30339-3-marcandre.lureau@redhat.com> User-Agent: Mutt/1.9.1 (2017-09-22) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 24 Jan 2018 03:25:20 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 23, 2018 at 05:40:39PM +0100, Marc-André Lureau wrote: > Modify fw_cfg_read_blob() to use DMA if the device supports it. > Return errors, because the operation may fail. > > The DMA operation is expected to run synchronously with today qemu, > but the specification states that it may become async, so we run > "control" field check in a loop for eventual changes. > > We may want to switch all the *buf addresses to use only kmalloc'ed > buffers (instead of using stack/image addresses with dma=false). > > Signed-off-by: Marc-André Lureau > --- > drivers/firmware/qemu_fw_cfg.c | 131 ++++++++++++++++++++++++++++++++++------- > 1 file changed, 111 insertions(+), 20 deletions(-) > > diff --git a/drivers/firmware/qemu_fw_cfg.c b/drivers/firmware/qemu_fw_cfg.c > index 740df0df2260..686f0e839858 100644 > --- a/drivers/firmware/qemu_fw_cfg.c > +++ b/drivers/firmware/qemu_fw_cfg.c > @@ -33,6 +33,7 @@ > #include > #include > #include > +#include > > MODULE_AUTHOR("Gabriel L. Somlo "); > MODULE_DESCRIPTION("QEMU fw_cfg sysfs support"); > @@ -43,12 +44,22 @@ MODULE_LICENSE("GPL"); > #define FW_CFG_ID 0x01 > #define FW_CFG_FILE_DIR 0x19 > > +#define FW_CFG_VERSION_DMA 0x02 > +#define FW_CFG_DMA_CTL_ERROR 0x01 > +#define FW_CFG_DMA_CTL_READ 0x02 > +#define FW_CFG_DMA_CTL_SKIP 0x04 > +#define FW_CFG_DMA_CTL_SELECT 0x08 > +#define FW_CFG_DMA_CTL_WRITE 0x10 > + > /* size in bytes of fw_cfg signature */ > #define FW_CFG_SIG_SIZE 4 > > /* fw_cfg "file name" is up to 56 characters (including terminating nul) */ > #define FW_CFG_MAX_FILE_PATH 56 > > +/* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */ > +static u32 fw_cfg_rev; > + > /* fw_cfg file directory entry type */ > struct fw_cfg_file { > u32 size; > @@ -57,6 +68,12 @@ struct fw_cfg_file { > char name[FW_CFG_MAX_FILE_PATH]; > }; > > +struct fw_cfg_dma { > + u32 control; > + u32 length; > + u64 address; > +} __packed; > + > /* fw_cfg device i/o register addresses */ > static bool fw_cfg_is_mmio; > static phys_addr_t fw_cfg_p_base; > @@ -75,12 +92,68 @@ static inline u16 fw_cfg_sel_endianness(u16 key) > return fw_cfg_is_mmio ? cpu_to_be16(key) : cpu_to_le16(key); > } > > +static inline bool fw_cfg_dma_enabled(void) > +{ > + return fw_cfg_rev & FW_CFG_VERSION_DMA && fw_cfg_reg_dma; > +} > + > +/* qemu fw_cfg device is sync today, but spec says it may become async */ > +static void fw_cfg_wait_for_control(struct fw_cfg_dma *d) > +{ > + do { > + u32 ctrl = be32_to_cpu(READ_ONCE(d->control)); > + > + if ((ctrl & ~FW_CFG_DMA_CTL_ERROR) == 0) > + return; > + > + usleep_range(50, 100); > + } while (true); > +} > + > +static ssize_t fw_cfg_dma_transfer(struct device *dev, > + void *address, u32 length, u32 control) > +{ > + phys_addr_t dma; > + struct fw_cfg_dma *d = NULL; > + ssize_t ret = length; > + > + d = kmalloc(sizeof(*d), GFP_KERNEL); > + if (!d) { > + ret = -ENOMEM; > + goto end; > + } > + > + *d = (struct fw_cfg_dma) { > + .address = cpu_to_be64(virt_to_phys(address)), > + .length = cpu_to_be32(length), > + .control = cpu_to_be32(control) > + }; > + > + dma = virt_to_phys(d); > + > + iowrite32be((u64)dma >> 32, fw_cfg_reg_dma); > + iowrite32be(dma, fw_cfg_reg_dma + 4); We can do it with iowrite64be(virt_to_phys(d)) too? In all cases I think it's good enough and no worth for a repost. For the DMA transfer part: Acked-by: Peter Xu Thanks, -- Peter Xu