Index: cbv2/src/northbridge/amd/amdk8/northbridge.c
===================================================================
--- cbv2.orig/src/northbridge/amd/amdk8/northbridge.c
+++ cbv2/src/northbridge/amd/amdk8/northbridge.c
@@ -295,13 +295,14 @@ static int reg_useable(unsigned reg,
 	return result;
 }
 
-static struct resource *amdk8_find_iopair(device_t dev, unsigned nodeid, unsigned link)
+static unsigned amdk8_find_reg(device_t dev, unsigned nodeid, unsigned link,
+			       unsigned min, unsigned max)
 {
-	struct resource *resource;
+	unsigned resource;
 	unsigned free_reg, reg;
 	resource = 0;
 	free_reg = 0;
-	for(reg = 0xc0; reg <= 0xd8; reg += 0x8) {
+	for(reg = min; reg <= max; reg += 0x8) {
 		int result;
 		result = reg_useable(reg, dev, nodeid, link);
 		if (result == 1) {
@@ -313,40 +314,23 @@ static struct resource *amdk8_find_iopai
 			free_reg = reg;
 		}
 	}
-	if (reg > 0xd8) {
+	if (reg > max) {
 		reg = free_reg;
 	}
 	if (reg > 0) {
-		resource = new_resource(dev, IOINDEX(0x100 + reg, link));
+		resource = IOINDEX(0x100 + reg, link);
 	}
 	return resource;
 }
 
-static struct resource *amdk8_find_mempair(device_t dev, unsigned nodeid, unsigned link)
+static unsigned amdk8_find_iopair(device_t dev, unsigned nodeid, unsigned link)
 {
-	struct resource *resource;
-	unsigned free_reg, reg;
-	resource = 0;
-	free_reg = 0;
-	for(reg = 0x80; reg <= 0xb8; reg += 0x8) {
-		int result;
-		result = reg_useable(reg, dev, nodeid, link);
-		if (result == 1) {
-			/* I have been allocated this one */
-			break;
-		}
-		else if (result > 1) {
-			/* I have a free register pair */
-			free_reg = reg;
-		}
-	}
-	if (reg > 0xb8) {
-		reg = free_reg;
-	}
-	if (reg > 0) {
-		resource = new_resource(dev, IOINDEX(0x100 + reg, link));
-	}
-	return resource;
+	return amdk8_find_reg(dev, nodeid, link, 0xc0, 0xd8);
+}
+
+static unsigned amdk8_find_mempair(device_t dev, unsigned nodeid, unsigned link)
+{
+	return amdk8_find_reg(dev, nodeid, link, 0x80, 0xb8);
 }
 
 static void amdk8_link_read_bases(device_t dev, unsigned nodeid, unsigned link)
@@ -354,7 +338,7 @@ static void amdk8_link_read_bases(device
 	struct resource *resource;
 
 	/* Initialize the io space constraints on the current bus */
-	resource =  amdk8_find_iopair(dev, nodeid, link);
+	resource = new_resource(dev, IOINDEX(0, link));
 	if (resource) {
 		resource->base  = 0;
 		resource->size  = 0;
@@ -365,7 +349,7 @@ static void amdk8_link_read_bases(device
 	}
 
 	/* Initialize the prefetchable memory constraints on the current bus */
-	resource = amdk8_find_mempair(dev, nodeid, link);
+	resource = new_resource(dev, IOINDEX(2, link));
 	if (resource) {
 		resource->base  = 0;
 		resource->size  = 0;
@@ -379,7 +363,7 @@ static void amdk8_link_read_bases(device
 	}
 
 	/* Initialize the memory constraints on the current bus */
-	resource = amdk8_find_mempair(dev, nodeid, link);
+	resource = new_resource(dev, IOINDEX(1, link));
 	if (resource) {
 		resource->base  = 0;
 		resource->size  = 0;
@@ -390,6 +374,8 @@ static void amdk8_link_read_bases(device
 	}
 }
 
+static void amdk8_create_vga_resource(device_t dev, unsigned nodeid);
+
 static void amdk8_read_resources(device_t dev)
 {
 	unsigned nodeid, link;
@@ -399,6 +385,8 @@ static void amdk8_read_resources(device_
 			amdk8_link_read_bases(dev, nodeid, link);
 		}
 	}
+
+	amdk8_create_vga_resource(dev, nodeid);
 }
 
 static void amdk8_set_resource(device_t dev, struct resource *resource, unsigned nodeid)
@@ -500,8 +488,6 @@ static void amdk8_create_vga_resource(de
 {
 	struct resource *resource;
 	unsigned link;
-	uint32_t base, limit;
-	unsigned reg;
 
 	/* find out which link the VGA card is connected,
 	 * we only deal with the 'first' vga card */
@@ -525,31 +511,16 @@ static void amdk8_create_vga_resource(de
 
 	printk_debug("VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, link);
 
-	/* allocate a temp resrouce for legacy VGA buffer */
-	resource = amdk8_find_mempair(dev, nodeid, link);
+	/* allocate a temp resource for the legacy VGA buffer */
+	resource = new_resource(dev, IOINDEX(4, link));
 	if(!resource){
-		printk_debug("VGA: Can not find free mmio reg for legacy VGA buffer\n");
+		printk_debug("VGA: %s out of resources.\n", dev_path(dev));
 		return;
 	}
 	resource->base = 0xa0000;
 	resource->size = 0x20000;
-
-	/* write the resource to the hardware */
-	reg  = resource->index & 0xfc;
-	base  = f1_read_config32(reg);
-	limit = f1_read_config32(reg + 0x4);
-	base  &= 0x000000f0;
-	base  |= (resource->base >> 8) & 0xffffff00;
-	base  |= 3;
-	limit &= 0x00000048;
-	limit |= (resource_end(resource) >> 8) & 0xffffff00;
-	limit |= (resource->index & 3) << 4;
-	limit |= (nodeid & 7);
-	f1_write_config32(reg + 0x4, limit);
-	f1_write_config32(reg, base);
-
-	/* release the temp resource */
-	resource->flags = 0;
+	resource->flags = IORESOURCE_FIXED | IORESOURCE_MEM |
+			  IORESOURCE_ASSIGNED;
 }
 
 static void amdk8_set_resources(device_t dev)
@@ -560,13 +531,36 @@ static void amdk8_set_resources(device_t
 	/* Find the nodeid */
 	nodeid = amdk8_nodeid(dev);
 
-	amdk8_create_vga_resource(dev, nodeid);
-
 	/* Set each resource we have found */
 	for(i = 0; i < dev->resources; i++) {
-		amdk8_set_resource(dev, &dev->resource[i], nodeid);
+		struct resource *res = &dev->resource[i];
+		struct resource *old = NULL;
+		unsigned index;
+
+		if (res->size == 0) /* No need to allocate registers. */
+			continue;
+
+		if (res->flags & IORESOURCE_IO)
+			index = amdk8_find_iopair(dev, nodeid,
+						  IOINDEX_LINK(res->index));
+		else
+			index = amdk8_find_mempair(dev, nodeid,
+						   IOINDEX_LINK(res->index));
+
+		old = probe_resource(dev, index);
+		if (old) {
+			res->index = old->index;
+			old->index = 0;
+			old->flags = 0;
+		}
+		else
+			res->index = index;
+
+		amdk8_set_resource(dev, res, nodeid);
 	}
 
+	compact_resources(dev);
+
 	for(link = 0; link < dev->links; link++) {
 		struct bus *bus;
 		bus = &dev->link[link];
@@ -636,6 +630,8 @@ static void amdk8_domain_read_resources(
 				struct resource *res;
 				res = new_resource(reg_dev, IOINDEX(0x100 + reg, link));
 				if (res) {
+					res->base = base;
+					res->limit = limit;
 					res->flags = 1;
 				}
 			}
@@ -672,7 +668,8 @@ static void tolm_test(void *gp, struct d
 	struct resource **best_p = gp;
 	struct resource *best;
 	best = *best_p;
-	if (!best || (best->base > new->base)) {
+	/* Skip VGA. */
+	if (!best || (best->base > new->base && new->base > 0xa0000)) {
 		best = new;
 	}
 	*best_p = best;
Index: cbv2/src/devices/device.c
===================================================================
--- cbv2.orig/src/devices/device.c
+++ cbv2/src/devices/device.c
@@ -662,9 +662,9 @@ static void avoid_fixed_resources(struct
 
 #if CONFIG_CONSOLE_VGA == 1
 device_t vga_pri = 0;
-static void allocate_vga_resource(void)
+static void set_vga_bridge_bits(void)
 {
-#warning "FIXME modify allocate_vga_resource so it is less pci centric!"
+#warning "FIXME modify set_vga_bridge so it is less pci centric!"
 #warning "This function knows too much about PCI stuff, it should be just a iterator/visitor."
 
 	/* FIXME: Handle the VGA palette snooping. */
@@ -715,7 +715,7 @@ static void allocate_vga_resource(void)
 
 	if (vga) {
 		/* VGA is first add on card or the only onboard VGA. */
-		printk_debug("Allocating VGA resource %s\n", dev_path(vga));
+		printk_debug("Setting up VGA for %s\n", dev_path(vga));
 		/* All legacy VGA cards have MEM & I/O space registers. */
 		vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
 		vga_pri = vga;
@@ -920,6 +920,10 @@ void dev_configure(void)
 	struct device *child;
 	int i;
 
+#if CONFIG_CONSOLE_VGA == 1
+	set_vga_bridge_bits();
+#endif
+
 	printk_info("Allocating resources...\n");
 
 	root = &dev_root;
@@ -983,12 +987,6 @@ void dev_configure(void)
 		}
 	}
 
-#if CONFIG_CONSOLE_VGA == 1
-	/* Allocate the VGA I/O resource. */
-	allocate_vga_resource();
-	print_resource_tree(root, BIOS_DEBUG, "After VGA.");
-#endif
-
 	/* Store the computed resource allocations into device registers ... */
 	printk_info("Setting resources...\n");
 	for (child = root->link[0].children; child; child = child->sibling) {
