Received: by 10.223.176.46 with SMTP id f43csp562952wra; Wed, 24 Jan 2018 02:30:45 -0800 (PST) X-Google-Smtp-Source: AH8x227H+2KfsEQt6O7piOM4X2yY24YKixGUTZVa1+iARlYD1mWQ5A2GOnBq47d1DRKoLmI7RBLU X-Received: by 2002:a17:902:b687:: with SMTP id c7-v6mr7936802pls.138.1516789845482; Wed, 24 Jan 2018 02:30:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516789845; cv=none; d=google.com; s=arc-20160816; b=wd4AnbT2RmWaTJArkSlvjKT8Boruho+0Q5Sehe9RDWF/2OVtGtJcd1I/6LkSu1NkWC m9DFg+PCnOnmLXySvCpHSLwxICmERFj+Qr6RaNgNZNV7lA4imfudttyzbBCWShlKaKs/ Nq3Yj83NkFdo/FuEe+tdnsWqOIYueDO1LfV1upMkZ6S4W/GFSwKz39KtWAgUOAhXtnHk hahTVhVYQpZ/xnOBon485/E+G66UXu2nIGoYGKfIqNhqgbGgVUUHypByoEy5AGDZCDy+ nH3cgIZqvu8zRzhiUNmb3boqY+g5LkqYT7ITF64nT+KjikRoEm5QAcgz77dImiG4rl7h rrJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :arc-authentication-results; bh=r0V6O65av61GzVpeX3BiJY5MG4//xMjNAkiGRBfZJ4o=; b=Q91kTEyq0fol7hY0tHRUIKtyWEfwVqG5CuGWWwBKVKdlyBD0/AwuUvmO1NEeEN9jPa EQJGHtdI+NR1BJ+8qHX0DZ0aC6mUPQzudxJRkFirUY8648VeC6wctaHTtmMq516zk6w4 TwJHUp9x7UeD2AFU+l+M3RRHCu58w6USrZaJwGLXF9ep4wCc2Va1VmHGVYUfz0PWDH5W Pd9totrXZXLDyInTfp4Xv9rCGHhTbdv3Rq8qkXy2ylaJUw5pUdHY3q/mgPN3/HRcweJR KRd14cev75s0aL4fwnR9AAEy4UVZXAg/CBysc+5jP1PqdO/xnDTCW62A6b0gsGOPcAE9 rnsQ== 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 s14-v6si5936058plp.604.2018.01.24.02.30.31; Wed, 24 Jan 2018 02:30:45 -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 S933052AbeAXKaA convert rfc822-to-8bit (ORCPT + 99 others); Wed, 24 Jan 2018 05:30:00 -0500 Received: from mail-qt0-f193.google.com ([209.85.216.193]:41356 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932859AbeAXK36 (ORCPT ); Wed, 24 Jan 2018 05:29:58 -0500 Received: by mail-qt0-f193.google.com with SMTP id i1so8986878qtj.8 for ; Wed, 24 Jan 2018 02:29:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Vt3fMz4NycUQCCr0rXCchg0dpwoJfyXcGgQOjeIxyqE=; b=agsDXrhAnVLNWpD7ZR5TsASVjjAcH5TYA1AtakJ9AKoBcz503KBasdXHyIfxWxENeR rNRHiNBZQn+5peNqLpSz2sf0UoGGu/2Aaohpykck0nDhuVP6ioS9E6G3I3NwBUeTDw3b +XAVMxtbX4eC51oNbGoJAQMgwCh8OHyH0ptQauTYcXr44OyyxDMB3oqUfVRGB4LEbB5L FjeINnw9GEu2ArY7B83+NPcQMtc1TXYgbeNdJYRjO3bbZUTFGwZc6yN9S6oTZw0ySfKo Fpl3/W98rCRagK1AETS0cJSeiN6Pu0br+6Cxyud7q/l55eadSwkj33ZDXGs4v0iXOF7b 5lxw== X-Gm-Message-State: AKwxytf9frt8/2wSjTq518o1aO4cToQS89teKQ2V3m+qFasfKyTQij2x FvlooAPxcKApPUckjAvJPN99T/GLJffIP7zK7bcNZg== X-Received: by 10.200.34.194 with SMTP id g2mr8461127qta.302.1516789798005; Wed, 24 Jan 2018 02:29:58 -0800 (PST) MIME-Version: 1.0 Received: by 10.12.146.155 with HTTP; Wed, 24 Jan 2018 02:29:57 -0800 (PST) In-Reply-To: <20180124032505.GD8001@xz-mi> References: <20180123164041.30339-1-marcandre.lureau@redhat.com> <20180123164041.30339-3-marcandre.lureau@redhat.com> <20180124032505.GD8001@xz-mi> From: Marc-Andre Lureau Date: Wed, 24 Jan 2018 11:29:57 +0100 Message-ID: Subject: Re: [PATCH v10 2/4] fw_cfg: do DMA read operation To: Peter Xu Cc: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= , Linux Kernel Mailing List , Baoquan He , slp@redhat.com, "Michael S . Tsirkin" , "Somlo, Gabriel" , xiaolong.ye@intel.com, qemu-devel Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi On Wed, Jan 24, 2018 at 4:25 AM, Peter Xu wrote: > 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. That would not build on 32 bit, but we could have a #ifdef CONFIG_64BIT (untested). > > For the DMA transfer part: > > Acked-by: Peter Xu thanks