diff mbox

[3/5] x86: avoid PNP resources when allocating address space

Message ID 20101208213616.13026.77390.stgit@bob.kio (mailing list archive)
State Accepted, archived
Headers show

Commit Message

Bjorn Helgaas Dec. 8, 2010, 9:36 p.m. UTC
None
diff mbox

Patch

diff --git a/arch/x86/kernel/resource.c b/arch/x86/kernel/resource.c
index 5dd6473..1daee92 100644
--- a/arch/x86/kernel/resource.c
+++ b/arch/x86/kernel/resource.c
@@ -1,6 +1,80 @@ 
 #include <linux/ioport.h>
+#include <linux/pnp.h>
 #include <asm/e820.h>
 
+#ifdef CONFIG_PNP
+static bool resource_conflict(struct resource *res, resource_size_t start,
+			      resource_size_t end)
+{
+	/*
+	 * Return true if and only if "res" conflicts with the [start-end]
+	 * range.
+	 */
+	return res->start <= end && res->end >= start;
+}
+
+static void resource_split(struct resource *res, resource_size_t start,
+			   resource_size_t end, struct resource *low,
+			   struct resource *high)
+{
+	/*
+	 * If "res" conflicts with [start-end], split "res" into the
+	 * part below "start" (low) and the part above "end" (high),
+	 * either (or both) of which may be empty.
+	 *
+	 * If there's no conflict, return the entire "res" as "low".
+	 */
+	*low = *res;
+	low->start = res->start;
+	low->end = res->start - 1;	/* default to empty (size 0) */
+
+	*high = *res;
+	high->end = res->end;
+	high->start = res->end + 1;	/* default to empty (size 0) */
+
+	if (!resource_conflict(res, start, end)) {
+		low->end = res->end;
+		return;
+	}
+
+	if (res->start < start)
+		low->end = start - 1;
+
+	if (res->end > end)
+		high->start = end + 1;
+}
+
+static void pnp_remove_reservations(struct resource *avail)
+{
+	unsigned long type = resource_type(avail);
+	struct pnp_dev *dev;
+	int i;
+	struct resource *res, low, high;
+
+	/*
+	 * Clip the available region to avoid PNP devices.  The PNP
+	 * resources really should be in the resource map to begin with,
+	 * but there are still some issues preventing that.
+	 */
+	pnp_for_each_dev(dev) {
+		i = 0;
+		res = pnp_get_resource(dev, type, i++);
+		while (res) {
+			if (!(res->flags & IORESOURCE_WINDOW)) {
+				resource_split(avail, res->start, res->end,
+					       &low, &high);
+				if (resource_size(&low) > resource_size(&high))
+					*avail = low;
+				else
+					*avail = high;
+			}
+
+			res = pnp_get_resource(dev, type, i++);
+		}
+	}
+}
+#endif
+
 void arch_remove_reservations(struct resource *avail)
 {
 	/*
@@ -11,4 +85,8 @@  void arch_remove_reservations(struct resource *avail)
 		if (avail->start < BIOS_END)
 			avail->start = BIOS_END;
 	}
+
+#ifdef CONFIG_PNP
+	pnp_remove_reservations(avail);
+#endif
 }