Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/evbarm/fdt Deduplicate address-range querying/mappi...



details:   https://anonhg.NetBSD.org/src/rev/49cc998f0e0f
branches:  trunk
changeset: 972057:49cc998f0e0f
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Thu May 14 19:24:35 2020 +0000

description:
Deduplicate address-range querying/mapping logic.

diffstat:

 sys/arch/evbarm/fdt/fdt_machdep.c |  213 ++++++++++++++-----------------------
 1 files changed, 80 insertions(+), 133 deletions(-)

diffs (278 lines):

diff -r 3fb71c7563af -r 49cc998f0e0f sys/arch/evbarm/fdt/fdt_machdep.c
--- a/sys/arch/evbarm/fdt/fdt_machdep.c Thu May 14 19:21:53 2020 +0000
+++ b/sys/arch/evbarm/fdt/fdt_machdep.c Thu May 14 19:24:35 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fdt_machdep.c,v 1.69 2020/05/14 19:21:06 riastradh Exp $ */
+/* $NetBSD: fdt_machdep.c,v 1.70 2020/05/14 19:24:35 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2015-2017 Jared McNeill <jmcneill%invisible.ca@localhost>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.69 2020/05/14 19:21:06 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.70 2020/05/14 19:24:35 riastradh Exp $");
 
 #include "opt_machdep.h"
 #include "opt_bootconfig.h"
@@ -334,83 +334,20 @@
 }
 
 static void
-fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
-{
-       *pstart = *pend = 0;
-
-#ifdef MEMORY_DISK_DYNAMIC
-       const int chosen = OF_finddevice("/chosen");
-       if (chosen < 0)
-               return;
-
-       int len;
-       const void *start_data = fdtbus_get_prop(chosen,
-           "linux,initrd-start", &len);
-       const void *end_data = fdtbus_get_prop(chosen,
-           "linux,initrd-end", NULL);
-       if (start_data == NULL || end_data == NULL)
-               return;
-
-       switch (len) {
-       case 4:
-               *pstart = be32dec(start_data);
-               *pend = be32dec(end_data);
-               break;
-       case 8:
-               *pstart = be64dec(start_data);
-               *pend = be64dec(end_data);
-               break;
-       default:
-               printf("Unsupported len %d for /chosen/initrd-start\n", len);
-               return;
-       }
-#endif
-}
-
-static void
-fdt_setup_initrd(void)
-{
-#ifdef MEMORY_DISK_DYNAMIC
-       const uint64_t initrd_size = initrd_end - initrd_start;
-       paddr_t startpa = trunc_page(initrd_start);
-       paddr_t endpa = round_page(initrd_end);
-       paddr_t pa;
-       vaddr_t va;
-       void *md_start;
-
-       if (initrd_size == 0)
-               return;
-
-       va = uvm_km_alloc(kernel_map, initrd_size, 0,
-           UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
-       if (va == 0) {
-               printf("Failed to allocate VA for initrd\n");
-               return;
-       }
-
-       md_start = (void *)va;
-
-       for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
-               pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
-       pmap_update(pmap_kernel());
-
-       md_root_setconf(md_start, initrd_size);
-#endif
-}
-
-static void
-fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend)
+fdt_probe_range(const char *startname, const char *endname,
+    uint64_t *pstart, uint64_t *pend)
 {
        int chosen, len;
        const void *start_data, *end_data;
 
        *pstart = *pend = 0;
+
        chosen = OF_finddevice("/chosen");
        if (chosen < 0)
                return;
 
-       start_data = fdtbus_get_prop(chosen, "netbsd,rndseed-start", &len);
-       end_data = fdtbus_get_prop(chosen, "netbsd,rndseed-end", NULL);
+       start_data = fdtbus_get_prop(chosen, startname, &len);
+       end_data = fdtbus_get_prop(chosen, endname, NULL);
        if (start_data == NULL || end_data == NULL)
                return;
 
@@ -424,68 +361,92 @@
                *pend = be64dec(end_data);
                break;
        default:
-               printf("Unsupported len %d for /chosen/rndseed-start\n", len);
+               printf("Unsupported len %d for /chosen `%s'\n",
+                   len, startname);
                return;
        }
 }
 
+static void *
+fdt_map_range(uint64_t start, uint64_t end, uint64_t *psize,
+    const char *purpose)
+{
+       const paddr_t startpa = trunc_page(start);
+       const paddr_t endpa = round_page(end);
+       paddr_t pa;
+       vaddr_t va;
+       void *ptr;
+
+       *psize = end - start;
+       if (*psize == 0)
+               return NULL;
+
+       va = uvm_km_alloc(kernel_map, *psize, 0, UVM_KMF_VAONLY|UVM_KMF_NOWAIT);
+       if (va == 0) {
+               printf("Failed to allocate VA for %s\n", purpose);
+               return NULL;
+       }
+       ptr = (void *)(va + (start & (PAGE_SIZE-1)));
+
+       for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
+               pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
+       pmap_update(pmap_kernel());
+
+       return ptr;
+}
+
+static void
+fdt_probe_initrd(uint64_t *pstart, uint64_t *pend)
+{
+       *pstart = *pend = 0;
+
+#ifdef MEMORY_DISK_DYNAMIC
+       fdt_probe_range("linux,initrd-start", "linux,initrd-end", pstart, pend);
+#endif
+}
+
+static void
+fdt_setup_initrd(void)
+{
+#ifdef MEMORY_DISK_DYNAMIC
+       void *md_start;
+       uint64_t initrd_size;
+
+       md_start = fdt_map_range(initrd_start, initrd_end, &initrd_size,
+           "initrd");
+       if (md_start == NULL)
+               return;
+       md_root_setconf(md_start, initrd_size);
+#endif
+}
+
+static void
+fdt_probe_rndseed(uint64_t *pstart, uint64_t *pend)
+{
+
+       fdt_probe_range("netbsd,rndseed-start", "netbsd,rndseed-end",
+           pstart, pend);
+}
+
 static void
 fdt_setup_rndseed(void)
 {
-       const uint64_t rndseed_size = rndseed_end - rndseed_start;
-       const paddr_t startpa = trunc_page(rndseed_start);
-       const paddr_t endpa = round_page(rndseed_end);
-       paddr_t pa;
-       vaddr_t va;
+       uint64_t rndseed_size;
        void *rndseed;
 
-       if (rndseed_size == 0)
-               return;
-
-       va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
-           UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
-       if (va == 0) {
-               printf("Failed to allocate VA for rndseed\n");
+       rndseed = fdt_map_range(rndseed_start, rndseed_end, &rndseed_size,
+           "rndseed");
+       if (rndseed == NULL)
                return;
-       }
-       rndseed = (void *)va;
-
-       for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
-               pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
-       pmap_update(pmap_kernel());
-
        rnd_seed(rndseed, rndseed_size);
 }
 
 static void
 fdt_probe_efirng(uint64_t *pstart, uint64_t *pend)
 {
-       int chosen, len;
-       const void *start_data, *end_data;
 
-       *pstart = *pend = 0;
-       chosen = OF_finddevice("/chosen");
-       if (chosen < 0)
-               return;
-
-       start_data = fdtbus_get_prop(chosen, "netbsd,efirng-start", &len);
-       end_data = fdtbus_get_prop(chosen, "netbsd,efirng-end", NULL);
-       if (start_data == NULL || end_data == NULL)
-               return;
-
-       switch (len) {
-       case 4:
-               *pstart = be32dec(start_data);
-               *pend = be32dec(end_data);
-               break;
-       case 8:
-               *pstart = be64dec(start_data);
-               *pend = be64dec(end_data);
-               break;
-       default:
-               printf("Unsupported len %d for /chosen/efirng-start\n", len);
-               return;
-       }
+       fdt_probe_range("netbsd,efirng-start", "netbsd,efirng-end",
+           pstart, pend);
 }
 
 static struct krndsource efirng_source;
@@ -493,28 +454,14 @@
 static void
 fdt_setup_efirng(void)
 {
-       const uint64_t efirng_size = efirng_end - efirng_start;
-       const paddr_t startpa = trunc_page(efirng_start);
-       const paddr_t endpa = round_page(efirng_end);
-       paddr_t pa;
-       vaddr_t va;
+       uint64_t efirng_size;
        void *efirng;
 
-       if (efirng_size == 0)
+       efirng = fdt_map_range(efirng_start, efirng_end, &efirng_size,
+           "efirng");
+       if (efirng == NULL)
                return;
 
-       va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
-           UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
-       if (va == 0) {
-               printf("Failed to allocate VA for efirng\n");
-               return;
-       }
-       efirng = (void *)va;
-
-       for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
-               pmap_kenter_pa(va, pa, VM_PROT_READ|VM_PROT_WRITE, 0);
-       pmap_update(pmap_kernel());
-
        rnd_attach_source(&efirng_source, "efirng", RND_TYPE_RNG,
            RND_FLAG_DEFAULT);
        rnd_add_data(&efirng_source, efirng, efirng_size, 0);



Home | Main Index | Thread Index | Old Index