Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp364933pxb; Wed, 18 Nov 2020 06:35:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJyYbjyPYe7eDEMSym8ghgYoR8rUdx43XgtejOME2LXMoRKY9YuRJ5lPXp22lCSv2FMjyTbp X-Received: by 2002:a17:906:17d1:: with SMTP id u17mr23268211eje.229.1605710099817; Wed, 18 Nov 2020 06:34:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605710099; cv=none; d=google.com; s=arc-20160816; b=y+TmXLtZO3yNZbEe5uDpxfMr6edI9K4Ou+UHqHyvX3OOWtJw5JEQTYDRXoRqtYgpk7 0MeJiFk6hxr8WT+FSHeyEnHyzLQIDpGfFzT8N4xVQAuqblwN9mU+Vbf06yOm3LrsFC01 XXDy8Tx0DPyXVz7Xqs1WIvXHUe556HyxkhwD3gPRG/daLpDJh7fB4/0TElk4WVOeOy2W xAKIUelOVNTCL8QbjfcUXw1ziUtIIjywl+BwSzDY8p5DyZNCl1UTKs6g2fSMndJFWV1S iTonqdGO4MrRnQ9EftkkklcMrkWN8F6RuJma10jEIuTfLfIh9h4Af3/2cwod+9DeVmwK kQwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=77gMImcbngsyFF3Kh1bUZt4EqmcmVNO0vErBUFdwx2w=; b=DvxcgEb92VTHxbl7BikY6FxACEn1W4e7vUfo+hjMgHVgFNxOE7AzBPqPwTYuFIY9Nt eQ5r0UAqoo4EmT6IRMm5nYc9yqCLZ3hI7+9RgW5UmTysLhXjmngnVkB6f+glmwJweOWB Gm4Qdj4mVZG3wOnpf3F8kFyCqLfJKAyPLZDo78fQrZXx+CYHsHModiMpZyhJ7O9t7MS2 RRVwYR1/8KV9NgLtXPpG2hcZ2hFju2kFVjWRvqRDbRKnpd86/+FeO2GTkDRk+IAPHQRg yC9DAC+eaq3kxR+G9/naaxZMp+PQRFWdr7qbaSPHcTMdyH0FwKMeQzFu5yW2Vxmim5pp o4SQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=v2fvZNY+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t25si3016807edi.255.2020.11.18.06.34.33; Wed, 18 Nov 2020 06:34:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=v2fvZNY+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726860AbgKRObt (ORCPT + 99 others); Wed, 18 Nov 2020 09:31:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726314AbgKRObs (ORCPT ); Wed, 18 Nov 2020 09:31:48 -0500 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79025C0613D4 for ; Wed, 18 Nov 2020 06:31:48 -0800 (PST) Received: by mail-wm1-x341.google.com with SMTP id h21so2918342wmb.2 for ; Wed, 18 Nov 2020 06:31:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=77gMImcbngsyFF3Kh1bUZt4EqmcmVNO0vErBUFdwx2w=; b=v2fvZNY+grT4giPGpG9AfdPemDFWlwFtdYpSOwbiaa6C9E4b1baIyzdWzuiGTlP7eS x7msBmtS2wkxDsy31kncXI7VGq0s/mnzfn74IKGpc1i9+QNjhLrd7j+dFe092Cn0dDjV KDBBZOuI5jZ5RjWsPPQtrwxEdGd6luqcgoD6zYO/sgtdKiCG8KBvsAVG/Iv4p8wQs65p mC5FtsmnqYnaDPfAvok/8WjV3T/akvz3RfZ2fgPXDWOAAS9WeK3XU9DWqe06ehUMfZRc /cUuRFAjKY3RD2J+A4EDKh5NRVcvHAClSrOyTCdwStFA3FuqGktmyzt85/OESDbT0Ssf k3Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=77gMImcbngsyFF3Kh1bUZt4EqmcmVNO0vErBUFdwx2w=; b=uDZb+GFjjzP1h7mqunKXlKOdlOCD7U4zkuiJWjkbzoXGMm3twsEdl62jMVlHnROpIA 0ef4vrQwF3itYjYDzpdYkQAruGkziW9kiAzVPNNNHLloy9MAavf3d37eWZF2tNhe8om3 e7sCTS8UG6PIunGSdgHf684Jwbr+8RSATKVMj0Pr64jopwarWetlGULRCt2V4YKg9+Yp z6UeATZHww/ixp319+5b18TBsVqTtldJR5kC8fBaS0qjrZr+q3nDBfMSw8PBqWgy54tr jGbduk0n/5Ico7c47SSCedGmLQa9wwega922mBJUg1RWi+kscSIFiOb8o+bIxXnwINt6 VatQ== X-Gm-Message-State: AOAM532NwFd0IRQqn/Meyk2LWOiuyfiZGTIH4DgXyTY4p9ahfmGkWyBv ie0nenXCgIx5yqOZcG8wL3qUtQ== X-Received: by 2002:a7b:cbc8:: with SMTP id n8mr334272wmi.124.1605709906953; Wed, 18 Nov 2020 06:31:46 -0800 (PST) Received: from google.com ([2a00:79e0:d:210:f693:9fff:fef4:a7ef]) by smtp.gmail.com with ESMTPSA id u5sm28800161wro.56.2020.11.18.06.31.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Nov 2020 06:31:45 -0800 (PST) Date: Wed, 18 Nov 2020 14:31:42 +0000 From: Quentin Perret To: Rob Herring Cc: Catalin Marinas , Will Deacon , Marc Zyngier , James Morse , Julien Thierry , Suzuki K Poulose , Frank Rowand , "moderated list:ARM64 PORT (AARCH64 ARCHITECTURE)" , open list , "open list:KERNEL VIRTUAL MACHINE FOR ARM64 (KVM/arm64)" , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE" , Android Kernel Team , android-kvm@google.com Subject: Re: [RFC PATCH 15/27] of/fdt: Introduce early_init_dt_add_memory_hyp() Message-ID: <20201118143142.GA2220979@google.com> References: <20201117181607.1761516-1-qperret@google.com> <20201117181607.1761516-16-qperret@google.com> <20201118092547.GA2031536@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201118092547.GA2031536@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wednesday 18 Nov 2020 at 09:25:47 (+0000), Quentin Perret wrote: > I'll have a go at the memblock stuff to see if I find a way to make it > work from that angle. OK, no luck with the memblock API, but I figured that I can actually postpone the KVM memory reservation to a later point, after unflatten_device_tree(), which lets me iterate over the memory nodes directly rather than having the fdt driver do it for me. The below seems to boot alright (though I'm not too familiar with of_address_to_resource() so I may not be using right) and keeps the whole thing in arch/arm64. Thoughts? Thanks, Quentin ---8<--- diff --git a/arch/arm64/kvm/hyp/reserved_mem.c b/arch/arm64/kvm/hyp/reserved_mem.c index 7da8e2915c1c..cab5ad587a3a 100644 --- a/arch/arm64/kvm/hyp/reserved_mem.c +++ b/arch/arm64/kvm/hyp/reserved_mem.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -16,7 +17,7 @@ phys_addr_t hyp_mem_base; phys_addr_t hyp_mem_size; -void __init early_init_dt_add_memory_hyp(u64 base, u64 size) +static int __init add_hyp_memblock_region(struct resource *rsrc) { struct hyp_memblock_region *reg; @@ -24,12 +25,14 @@ void __init early_init_dt_add_memory_hyp(u64 base, u64 size) kvm_nvhe_sym(hyp_memblock_nr) = -1; if (kvm_nvhe_sym(hyp_memblock_nr) < 0) - return; + return -ENOMEM; reg = kvm_nvhe_sym(hyp_memory); - reg[kvm_nvhe_sym(hyp_memblock_nr)].start = base; - reg[kvm_nvhe_sym(hyp_memblock_nr)].end = base + size; + reg[kvm_nvhe_sym(hyp_memblock_nr)].start = rsrc->start; + reg[kvm_nvhe_sym(hyp_memblock_nr)].end = rsrc->end; kvm_nvhe_sym(hyp_memblock_nr)++; + + return 0; } static int cmp_hyp_memblock(const void *p1, const void *p2) @@ -52,7 +55,10 @@ void kvm_sort_memblock_regions(void) extern bool enable_protected_kvm; void __init reserve_kvm_hyp(void) { + struct device_node *np; + struct resource rsrc; u64 nr_pages, prev; + int i; if (!enable_protected_kvm) return; @@ -60,8 +66,14 @@ void __init reserve_kvm_hyp(void) if (!is_hyp_mode_available() || is_kernel_in_hyp_mode()) return; - if (kvm_nvhe_sym(hyp_memblock_nr) <= 0) - return; + for_each_node_by_type(np, "memory") { + for (i = 0; !of_address_to_resource(np, i, &rsrc); i++) { + if (!add_hyp_memblock_region(&rsrc)) + continue; + kvm_err("Failed to add hyp memblock\n"); + return; + } + } hyp_mem_size += num_possible_cpus() << PAGE_SHIFT; hyp_mem_size += hyp_s1_pgtable_size(); diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index f81da019b677..114f788a4da4 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -391,7 +391,6 @@ void __init arm64_memblock_init(void) reserve_elfcorehdr(); - reserve_kvm_hyp(); high_memory = __va(memblock_end_of_DRAM() - 1) + 1; @@ -423,6 +422,8 @@ void __init bootmem_init(void) dma_pernuma_cma_reserve(); + reserve_kvm_hyp(); + /* * sparse_init() tries to allocate memory from memblock, so must be * done after the fixed reservations diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index af2b5a09c5b4..4602e467ca8b 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -1099,10 +1099,6 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0) #endif -void __init __weak early_init_dt_add_memory_hyp(u64 base, u64 size) -{ -} - void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) { const u64 phys_offset = MIN_MEMBLOCK_ADDR; @@ -1143,7 +1139,6 @@ void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) base = phys_offset; } memblock_add(base, size); - early_init_dt_add_memory_hyp(base, size); } int __init __weak early_init_dt_mark_hotplug_memory_arch(u64 base, u64 size)