WIP more pci debuggery

Change-Id: Ic3d88c8d0abb7a55d414c01e17ab56249bb7c85d
diff --git a/kernel/dev/pcie/pcie_bus_driver.cpp b/kernel/dev/pcie/pcie_bus_driver.cpp
index b8f658e..56c7c13 100644
--- a/kernel/dev/pcie/pcie_bus_driver.cpp
+++ b/kernel/dev/pcie/pcie_bus_driver.cpp
@@ -400,6 +400,8 @@
                                                 uint64_t size,
                                                 PciAddrSpace aspace,
                                                 bool add_op) {
+    TRACEF("base %#" PRIx64 " size %#" PRIx64 " add %d\n", base, size, add_op);
+
     if (!IsNotStarted(true)) {
         TRACEF("Cannot add/subtract bus regions once the bus driver has been started!\n");
         return ZX_ERR_BAD_STATE;
@@ -419,9 +421,15 @@
         auto& mmio_hi = mmio_hi_regions_;
 
         if (end <= U32_MAX) {
+            for (auto const& r: mmio_lo.sorted_by_base()) {
+                TRACEF("lo r %#" PRIx64 " %#" PRIx64 "\n", r.base, r.size);
+            }
             return (mmio_lo.*OpPtr)({ .base = base, .size = size }, true);
         } else
         if (base > U32_MAX) {
+            for (auto const& r: mmio_hi.sorted_by_base()) {
+                TRACEF("hi r %#" PRIx64 " %#" PRIx64 "\n", r.base, r.size);
+            }
             return (mmio_hi.*OpPtr)({ .base = base, .size = size }, true);
         } else {
             uint64_t lo_base = base;
@@ -430,6 +438,8 @@
             uint64_t hi_size = size - lo_size;
             zx_status_t res;
 
+            TRACEF("lo %#lx %#lx hi %#lx %#lx\n", lo_base, lo_size, hi_base, hi_size);
+
             res = (mmio_lo.*OpPtr)({ .base = lo_base, .size = lo_size }, true);
             if (res != ZX_OK)
                 return res;
diff --git a/kernel/dev/pcie/pcie_device.cpp b/kernel/dev/pcie/pcie_device.cpp
index a53c40a..84712aa 100644
--- a/kernel/dev/pcie/pcie_device.cpp
+++ b/kernel/dev/pcie/pcie_device.cpp
@@ -557,6 +557,10 @@
 
         zx_status_t res = ZX_ERR_NOT_FOUND;
         if (alloc != nullptr) {
+            TRACEF("preserve: going to allocate %#" PRIx64 " %#" PRIx64 "\n", info.bus_addr, info.size);
+            for (auto const& r: alloc->sorted_by_base()) {
+                TRACEF("before r %#" PRIx64 " %#" PRIx64 "\n", r.base, r.size);
+            }
             res = alloc->GetRegion({ .base = info.bus_addr, .size = info.size }, info.allocation);
         }
 
@@ -599,6 +603,10 @@
         uint64_t align_size  = ((info.size >= PAGE_SIZE) || is_io_space)
                              ? info.size
                              : PAGE_SIZE;
+        TRACEF("dynamic: going to allocate size %#" PRIx64 "\n", align_size);
+        for (auto const& r: alloc->sorted_by_base()) {
+            TRACEF("before r %#" PRIx64 " %#" PRIx64 "\n", r.base, r.size);
+        }
         zx_status_t res = alloc->GetRegion(align_size, align_size, info.allocation);
 
         if (res != ZX_OK) {
diff --git a/system/ulib/region-alloc/include/region-alloc/region-alloc.h b/system/ulib/region-alloc/include/region-alloc/region-alloc.h
index 6fabbdd..82a2fb1 100644
--- a/system/ulib/region-alloc/include/region-alloc/region-alloc.h
+++ b/system/ulib/region-alloc/include/region-alloc/region-alloc.h
@@ -473,6 +473,8 @@
         return avail_regions_by_base_.size();
     }
 
+    const Region::WAVLTreeSortByBase &sorted_by_base() const { return avail_regions_by_base_; }
+
 private:
     zx_status_t AddSubtractSanityCheckLocked(const ralloc_region_t& region);
     void ReleaseRegion(Region* region);