Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp595634ybg; Wed, 3 Jun 2020 08:41:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw2veitYmwsTdzfWG6GXLKTAA5LUn2Hy0hozIJSmfkXO4Wmf9oYw+SGo4SRcHuF4yHp6+Dg X-Received: by 2002:a17:906:2656:: with SMTP id i22mr13884765ejc.397.1591198911819; Wed, 03 Jun 2020 08:41:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591198911; cv=none; d=google.com; s=arc-20160816; b=0xbB/C/FcY2e+5SeEl7SmsXWnzmGcWbBUd/Jefx4fAFogN2SP5yoTqnrJU5E12k2PD EKNXhijc3qs75K4E7uL765Y+6QeSu4EprxEby0OFpJWWg9ug0c5hfviXm9jpqbHZ6mV8 xVrzTOgV+VL/QU8PALJvN8GAqK/cpMEhZxHL+5400hoD+pBJB3IUUQc7BptYWrhB/Emo Q0YfQlT59NRoyCzRGfvpCIhL0QhUXJbJVH/tR3GIBD7A6u51IFkY3sEcQCNBnK6vM05w dkiMXPRl2ZTh52qoqZQMlc0TzMabg9VDrp6+D9DV5Ug30EV7SazDzWtkcuT7ujt1t55g 9iVg== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=PlyhjjPl9FSmSoJ6kSHtm/qt9q2Z93VgewzzteDIbTY=; b=uaXdkgGGnEHH/JUuk3dXEmj7qHpYztwqpkNhE9x6FTwmJxejAkIaJsaQWye/81ygAT fmoWjMDVCW7GPX/mLWF1k7p9JasEHHoT4MZLBtdmc/4BR5dz3Q7uoZLzCgXAeP6Mh6VT v24ebaf1cDatKmCViArtgo0vduWIMl8LG1lYGF6gwUfLJJmT3W3wtV14I9/9QyN/bU7s ltSLDzZL+0d78r7pVW16XLv9FBfERVvQjvBaZzbrD7QMh3TwiZQ4YnNvBoJh7vW1f+hW SsP7ECJuqzd3ZFFLy+LvRZ6c4Fpr7Q/7qxOI3UzbMiuh5dpDtjTbzhY20svUnNdw35tO bXPw== 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 dm9si1542998ejc.624.2020.06.03.08.41.28; Wed, 03 Jun 2020 08:41:51 -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 S1726210AbgFCPiW (ORCPT + 99 others); Wed, 3 Jun 2020 11:38:22 -0400 Received: from relay1-d.mail.gandi.net ([217.70.183.193]:61319 "EHLO relay1-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726061AbgFCPiW (ORCPT ); Wed, 3 Jun 2020 11:38:22 -0400 X-Originating-IP: 90.112.45.105 Received: from debian.home (lfbn-gre-1-325-105.w90-112.abo.wanadoo.fr [90.112.45.105]) (Authenticated sender: alex@ghiti.fr) by relay1-d.mail.gandi.net (Postfix) with ESMTPSA id 02D2F240007; Wed, 3 Jun 2020 15:38:18 +0000 (UTC) From: Alexandre Ghiti To: Paul Walmsley , Palmer Dabbelt , Anup Patel , Atish Patra , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Ghiti Subject: [PATCH 2/2] riscv: Use PUD/PGDIR entries for linear mapping when possible Date: Wed, 3 Jun 2020 11:36:08 -0400 Message-Id: <20200603153608.30056-3-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200603153608.30056-1-alex@ghiti.fr> References: <20200603153608.30056-1-alex@ghiti.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Improve best_map_size so that PUD or PGDIR entries are used for linear mapping when possible as it allows better TLB utilization. Signed-off-by: Alexandre Ghiti --- arch/riscv/mm/init.c | 45 +++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 9a5c97e091c1..d275f9f834cf 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -424,13 +424,29 @@ static void __init create_pgd_mapping(pgd_t *pgdp, create_pgd_next_mapping(nextp, va, pa, sz, prot); } -static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) +static bool is_map_size_ok(uintptr_t map_size, phys_addr_t base, + uintptr_t base_virt, phys_addr_t size) { - /* Upgrade to PMD_SIZE mappings whenever possible */ - if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1))) - return PAGE_SIZE; + return !((base & (map_size - 1)) || (base_virt & (map_size - 1)) || + (size < map_size)); +} + +static uintptr_t __init best_map_size(phys_addr_t base, uintptr_t base_virt, + phys_addr_t size) +{ +#ifndef __PAGETABLE_PMD_FOLDED + if (is_map_size_ok(PGDIR_SIZE, base, base_virt, size)) + return PGDIR_SIZE; + + if (pgtable_l4_enabled) + if (is_map_size_ok(PUD_SIZE, base, base_virt, size)) + return PUD_SIZE; +#endif + + if (is_map_size_ok(PMD_SIZE, base, base_virt, size)) + return PMD_SIZE; - return PMD_SIZE; + return PAGE_SIZE; } /* @@ -576,7 +592,7 @@ void create_kernel_page_table(pgd_t *pgdir, uintptr_t map_size) asmlinkage void __init setup_vm(uintptr_t dtb_pa) { uintptr_t va, end_va; - uintptr_t map_size = best_map_size(load_pa, MAX_EARLY_MAPPING_SIZE); + uintptr_t map_size; load_pa = (uintptr_t)(&_start); load_sz = (uintptr_t)(&_end) - load_pa; @@ -587,6 +603,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) kernel_virt_addr = KERNEL_VIRT_ADDR; + map_size = best_map_size(load_pa, PAGE_OFFSET, MAX_EARLY_MAPPING_SIZE); va_pa_offset = PAGE_OFFSET - load_pa; va_kernel_pa_offset = kernel_virt_addr - load_pa; pfn_base = PFN_DOWN(load_pa); @@ -700,6 +717,8 @@ static void __init setup_vm_final(void) /* Map all memory banks */ for_each_memblock(memory, reg) { + uintptr_t remaining_size; + start = reg->base; end = start + reg->size; @@ -707,15 +726,19 @@ static void __init setup_vm_final(void) break; if (memblock_is_nomap(reg)) continue; - if (start <= __pa(PAGE_OFFSET) && - __pa(PAGE_OFFSET) < end) - start = __pa(PAGE_OFFSET); - map_size = best_map_size(start, end - start); - for (pa = start; pa < end; pa += map_size) { + pa = start; + remaining_size = reg->size; + + while (remaining_size) { va = (uintptr_t)__va(pa); + map_size = best_map_size(pa, va, remaining_size); + create_pgd_mapping(swapper_pg_dir, va, pa, map_size, PAGE_KERNEL); + + pa += map_size; + remaining_size -= map_size; } } -- 2.20.1