Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4432555pxy; Tue, 27 Apr 2021 05:07:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy5CKF83AO3ot1amDQy/QwC66QmvEjHzL4LTFo0ULFQLvZDWdmOJe8OGllHelZ6HQ1uKRdF X-Received: by 2002:a17:90a:94ca:: with SMTP id j10mr26989912pjw.126.1619525274198; Tue, 27 Apr 2021 05:07:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619525274; cv=none; d=google.com; s=arc-20160816; b=0pG+376CTjH2EamhGsp9A96Hor/QW1V2JLohHOOnnsAfr9p3JA0GS/rv6KCgsMbTf+ avt++MXWiM3L9S8DVwYSbxaFRBLSK1NY82p5Cczgm+OKBVN+vSj5uBODveNobG44bQKy ZFNxhyByVXhekqGTNUFcF2CamcWIiq3EPMFoaDJUn3JftjyIliSQxj1RWuX+4GrGqEe+ 1ljoVkqRhhxDSzWLPNxVdaLR+zDSjI4iCR9FvcGzQtsthfxv27SpLNw46wOLc4cQkPGB MMMQkTzSYoMDat2pZGtBbRgzpQdam7whAvleLU2/ekcK6yL9+IHyR5nxUYGNvMOIvpgr aRNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=RJxVncGS6U+aW2CwWEyoK+WV9nu3WxeKlSv9zlz4jhY=; b=m6febwsXX4ouuYZwm8hzAtebNsSNEiC2fK6p9ZHlpGdbibOYy3zahdUHFXEkBoXw9Z H4BqPh3DT9POuVtcICR9isT3EoioMKS4AwK+L26vNmobUPylXqXofZs3E4a77VYEbzl8 lhfHs4tHV+t7hhUqa62svwb79SzcbQ0d+2BU54md1JTZ3Uq/s1g0Z3WW9KSiELC/vmoy CSQD1MUpVdxQM8OTMfIxKvLwxTfO0MA0Ntk5bySkh5zo7jsl5I/luh4CPKuQQ1sIw7Mr Joo9JH/VrkpFNsQ87w0bd7Fzov8NSKklEXBZlS8pYTUmXQxcMDuIKd1FfSA/UdZb9Qkr wFPg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r34si1244416pgb.320.2021.04.27.05.07.40; Tue, 27 Apr 2021 05:07:54 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235570AbhD0MHu (ORCPT + 99 others); Tue, 27 Apr 2021 08:07:50 -0400 Received: from mail-lf1-f51.google.com ([209.85.167.51]:36697 "EHLO mail-lf1-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235426AbhD0MHt (ORCPT ); Tue, 27 Apr 2021 08:07:49 -0400 Received: by mail-lf1-f51.google.com with SMTP id n138so93456955lfa.3; Tue, 27 Apr 2021 05:07:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=RJxVncGS6U+aW2CwWEyoK+WV9nu3WxeKlSv9zlz4jhY=; b=NA8LH8xroKv34BoyPdLLJ3W8TrlBScMeazqbOr8CtMDlk/KJgYc6T60ntvA9gOYvHj smMRzykGYhUdZSElciOlA+OVmGQW5b6MfZt/F+5/E9fEwOzSH89plQeZ2fRKjWl3qX9E RBNzmIMsTaoAY17OAmZLTIreGq2b9z8dUqf+WSvjdWd8rV2B7XpuPLaESC7iZsXdBRSi u+mfFx4SQNyL0YHO9B/4/emOkMVq1BMjvfcNtb4mw9z5YDlO/40JlSA7FJeqHhAL9hE0 kAp4yd5put2tjCij87pOfBZrY8LzlOPnH4NQtxVUoHJc+TWtMGeBoaTKrh7qMUQv/7bO 1dMw== X-Gm-Message-State: AOAM533S9N2VpBNoGqNQk70vPb6ir+yuX3aZgRjxSxJ/S52A10OnPAku XfF5kBMhKHNUlMfRGDhoNlL35tVnjbk+eDa4pR0= X-Received: by 2002:a05:6512:1116:: with SMTP id l22mr17431918lfg.655.1619525223175; Tue, 27 Apr 2021 05:07:03 -0700 (PDT) Received: from arei.office.basealt.ru ([2a0c:88c0:1:813:96c6:91ff:fe6b:6650]) by smtp.gmail.com with ESMTPSA id h12sm376351ljh.21.2021.04.27.05.07.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Apr 2021 05:07:02 -0700 (PDT) From: Nikita Ermakov To: linux-efi@vger.kernel.org Cc: Ard Biesheuvel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Nikita Ermakov Subject: [PATCH] RISC-V: Relocate the kernel relative to a DRAM base. Date: Tue, 27 Apr 2021 15:06:07 +0300 Message-Id: <20210427120607.2646166-1-arei@altlinux.org> X-Mailer: git-send-email 2.29.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Try to get the base of the DRAM from a DTB to use it as a lowest address in physical memory to relocate the kernel. If it is not possible to obtain the base from a /memory node of the DTB let's make an assumption that the DRAM base at the beginning of the memory. Signed-off-by: Nikita Ermakov --- drivers/firmware/efi/libstub/riscv-stub.c | 39 ++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/efi/libstub/riscv-stub.c b/drivers/firmware/efi/libstub/riscv-stub.c index 380e4e251399..1b5944276e1a 100644 --- a/drivers/firmware/efi/libstub/riscv-stub.c +++ b/drivers/firmware/efi/libstub/riscv-stub.c @@ -46,6 +46,39 @@ static u32 get_boot_hartid_from_fdt(void) return fdt32_to_cpu(*prop); } +static unsigned long get_dram_base_from_fdt(void) +{ + const void *fdt; + int node, len; + const fdt32_t *addr_cells; + const void *prop; + + fdt = get_efi_config_table(DEVICE_TREE_GUID); + if (!fdt) + return ULONG_MAX; + + node = fdt_path_offset(fdt, "/"); + if (node < 0) + return ULONG_MAX; + + addr_cells = fdt_getprop((void *)fdt, node, "#address-cells", &len); + if (!addr_cells) + return ULONG_MAX; + + node = fdt_path_offset(fdt, "/memory"); + if (node < 0) + return ULONG_MAX; + + prop = fdt_getprop((void *)fdt, node, "reg", &len); + if (!prop) + return ULONG_MAX; + + if (fdt32_to_cpu(*addr_cells) > 1) + return fdt64_to_cpu(*((fdt64_t *)prop)); + else + return fdt32_to_cpu(*((fdt32_t *)prop)); +} + efi_status_t check_platform_features(void) { hartid = get_boot_hartid_from_fdt(); @@ -97,7 +130,11 @@ efi_status_t handle_kernel_image(unsigned long *image_addr, * lowest possible memory region as long as the address and size meets * the alignment constraints. */ - preferred_addr = MIN_KIMG_ALIGN; + preferred_addr = get_dram_base_from_fdt(); + if (preferred_addr == ULONG_MAX) + preferred_addr = MIN_KIMG_ALIGN; + else + preferred_addr += MIN_KIMG_ALIGN; status = efi_relocate_kernel(image_addr, kernel_size, *image_size, preferred_addr, MIN_KIMG_ALIGN, 0x0); -- 2.29.3