Author: emaste
Date: Thu Nov 21 19:20:08 2013
New Revision: 258436
URL: http://svnweb.freebsd.org/changeset/base/258436

Log:
  Refactor amd64 startup SMAP parsing
  
  Extracted from the projects/uefi branch, this change is a reasonable
  cleanup and will reduce the diffs to review when bringing in the
  UEFI work.
  
  Reviewed by:  kib@
  Sponsored by: The FreeBSD Foundation

Modified:
  head/sys/amd64/amd64/machdep.c

Modified: head/sys/amd64/amd64/machdep.c
==============================================================================
--- head/sys/amd64/amd64/machdep.c      Thu Nov 21 19:17:39 2013        
(r258435)
+++ head/sys/amd64/amd64/machdep.c      Thu Nov 21 19:20:08 2013        
(r258436)
@@ -1328,20 +1328,14 @@ isa_irq_pending(void)
 u_int basemem;
 
 static int
-add_smap_entry(struct bios_smap *smap, vm_paddr_t *physmap, int *physmap_idxp)
+add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap,
+    int *physmap_idxp)
 {
        int i, insert_idx, physmap_idx;
 
        physmap_idx = *physmap_idxp;
 
-       if (boothowto & RB_VERBOSE)
-               printf("SMAP type=%02x base=%016lx len=%016lx\n",
-                   smap->type, smap->base, smap->length);
-
-       if (smap->type != SMAP_TYPE_MEMORY)
-               return (1);
-
-       if (smap->length == 0)
+       if (length == 0)
                return (0);
 
        /*
@@ -1350,8 +1344,8 @@ add_smap_entry(struct bios_smap *smap, v
         */
        insert_idx = physmap_idx + 2;
        for (i = 0; i <= physmap_idx; i += 2) {
-               if (smap->base < physmap[i + 1]) {
-                       if (smap->base + smap->length <= physmap[i]) {
+               if (base < physmap[i + 1]) {
+                       if (base + length <= physmap[i]) {
                                insert_idx = i;
                                break;
                        }
@@ -1363,15 +1357,14 @@ add_smap_entry(struct bios_smap *smap, v
        }
 
        /* See if we can prepend to the next entry. */
-       if (insert_idx <= physmap_idx &&
-           smap->base + smap->length == physmap[insert_idx]) {
-               physmap[insert_idx] = smap->base;
+       if (insert_idx <= physmap_idx && base + length == physmap[insert_idx]) {
+               physmap[insert_idx] = base;
                return (1);
        }
 
        /* See if we can append to the previous entry. */
-       if (insert_idx > 0 && smap->base == physmap[insert_idx - 1]) {
-               physmap[insert_idx - 1] += smap->length;
+       if (insert_idx > 0 && base == physmap[insert_idx - 1]) {
+               physmap[insert_idx - 1] += length;
                return (1);
        }
 
@@ -1393,11 +1386,42 @@ add_smap_entry(struct bios_smap *smap, v
        }
 
        /* Insert the new entry. */
-       physmap[insert_idx] = smap->base;
-       physmap[insert_idx + 1] = smap->base + smap->length;
+       physmap[insert_idx] = base;
+       physmap[insert_idx + 1] = base + length;
        return (1);
 }
 
+static void
+add_smap_entries(struct bios_smap *smapbase, vm_paddr_t *physmap,
+    int *physmap_idx)
+{
+       struct bios_smap *smap, *smapend;
+       u_int32_t smapsize;
+
+       /*
+        * Memory map from INT 15:E820.
+        *
+        * subr_module.c says:
+        * "Consumer may safely assume that size value precedes data."
+        * ie: an int32_t immediately precedes smap.
+        */
+       smapsize = *((u_int32_t *)smapbase - 1);
+       smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
+
+       for (smap = smapbase; smap < smapend; smap++) {
+               if (boothowto & RB_VERBOSE)
+                       printf("SMAP type=%02x base=%016lx len=%016lx\n",
+                           smap->type, smap->base, smap->length);
+
+               if (smap->type != SMAP_TYPE_MEMORY)
+                       continue;
+
+               if (!add_physmap_entry(smap->base, smap->length, physmap,
+                   physmap_idx))
+                       break;
+       }
+}
+
 /*
  * Populate the (physmap) array with base/bound pairs describing the
  * available physical memory in the system, then test this memory and
@@ -1415,32 +1439,19 @@ getmemsize(caddr_t kmdp, u_int64_t first
        vm_paddr_t pa, physmap[PHYSMAP_SIZE];
        u_long physmem_start, physmem_tunable, memtest;
        pt_entry_t *pte;
-       struct bios_smap *smapbase, *smap, *smapend;
-       u_int32_t smapsize;
+       struct bios_smap *smapbase;
        quad_t dcons_addr, dcons_size;
 
        bzero(physmap, sizeof(physmap));
        basemem = 0;
        physmap_idx = 0;
 
-       /*
-        * get memory map from INT 15:E820, kindly supplied by the loader.
-        *
-        * subr_module.c says:
-        * "Consumer may safely assume that size value precedes data."
-        * ie: an int32_t immediately precedes smap.
-        */
        smapbase = (struct bios_smap *)preload_search_info(kmdp,
            MODINFO_METADATA | MODINFOMD_SMAP);
        if (smapbase == NULL)
                panic("No BIOS smap info from loader!");
 
-       smapsize = *((u_int32_t *)smapbase - 1);
-       smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
-
-       for (smap = smapbase; smap < smapend; smap++)
-               if (!add_smap_entry(smap, physmap, &physmap_idx))
-                       break;
+       add_smap_entries(smapbase, physmap, &physmap_idx);
 
        /*
         * Find the 'base memory' segment for SMP
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to