From patchwork Thu Oct 14 23:18:37 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bjorn Helgaas X-Patchwork-Id: 254881 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id o9ENJoL0028412 for ; Thu, 14 Oct 2010 23:19:51 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755438Ab0JNXSn (ORCPT ); Thu, 14 Oct 2010 19:18:43 -0400 Received: from g5t0009.atlanta.hp.com ([15.192.0.46]:34064 "EHLO g5t0009.atlanta.hp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755441Ab0JNXSl (ORCPT ); Thu, 14 Oct 2010 19:18:41 -0400 Received: from g5t0029.atlanta.hp.com (g5t0029.atlanta.hp.com [16.228.8.141]) by g5t0009.atlanta.hp.com (Postfix) with ESMTP id F1E79301E4; Thu, 14 Oct 2010 23:18:39 +0000 (UTC) Received: from ldl (ldl.usa.hp.com [16.125.112.222]) by g5t0029.atlanta.hp.com (Postfix) with ESMTP id AB3F020138; Thu, 14 Oct 2010 23:18:37 +0000 (UTC) Received: from localhost (ldl.fc.hp.com [127.0.0.1]) by ldl (Postfix) with ESMTP id 76CE9CF0013; Thu, 14 Oct 2010 17:18:37 -0600 (MDT) Received: from ldl ([127.0.0.1]) by localhost (ldl.fc.hp.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ccppouLCsgjx; Thu, 14 Oct 2010 17:18:37 -0600 (MDT) Received: from eh.fc.hp.com (bob.lnx.usa.hp.com [16.125.112.218]) by ldl (Postfix) with ESMTP id 513551A7E025; Thu, 14 Oct 2010 17:18:37 -0600 (MDT) Received: from bob.kio (localhost [127.0.0.1]) by eh.fc.hp.com (Postfix) with ESMTP id 4341926132; Thu, 14 Oct 2010 17:18:37 -0600 (MDT) Subject: [PATCH v4 3/6] PCI: allocate bus resources from the top down To: Jesse Barnes From: Bjorn Helgaas Cc: Bob Picco , Brian Bloniarz , Charles Butterfield , Denys Vlasenko , linux-pci@vger.kernel.org, "Horst H. von Brand" , Ingo Molnar , linux-kernel@vger.kernel.org, Stefan Becker , "H. Peter Anvin" , Yinghai Lu , Leann Ogasawara , Linus Torvalds , Thomas Gleixner Date: Thu, 14 Oct 2010 17:18:37 -0600 Message-ID: <20101014231837.536.62189.stgit@bob.kio> In-Reply-To: <20101014231804.536.62138.stgit@bob.kio> References: <20101014231804.536.62138.stgit@bob.kio> User-Agent: StGit/0.15 MIME-Version: 1.0 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Thu, 14 Oct 2010 23:19:52 +0000 (UTC) diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c index 7f0af0e..172bf26 100644 --- a/drivers/pci/bus.c +++ b/drivers/pci/bus.c @@ -64,6 +64,49 @@ void pci_bus_remove_resources(struct pci_bus *bus) } } +/* + * Find the highest-address bus resource below the cursor "res". If the + * cursor is NULL, return the highest resource. + */ +static struct resource *pci_bus_find_resource_prev(struct pci_bus *bus, + unsigned int type, + struct resource *res) +{ + struct resource *r, *prev = NULL; + int i; + + pci_bus_for_each_resource(bus, r, i) { + if (!r) + continue; + + if ((r->flags & IORESOURCE_TYPE_BITS) != type) + continue; + + /* If this resource is at or past the cursor, skip it */ + if (res) { + if (r == res) + continue; + if (r->end > res->end) + continue; + if (r->end == res->end && r->start > res->start) + continue; + } + + if (!prev) + prev = r; + + /* + * A small resource is higher than a large one that ends at + * the same address. + */ + if (r->end > prev->end || + (r->end == prev->end && r->start > prev->start)) + prev = r; + } + + return prev; +} + /** * pci_bus_alloc_resource - allocate a resource from a parent bus * @bus: PCI bus @@ -89,9 +132,10 @@ pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res, resource_size_t), void *alignf_data) { - int i, ret = -ENOMEM; + int ret = -ENOMEM; struct resource *r; resource_size_t max = -1; + unsigned int type = res->flags & IORESOURCE_TYPE_BITS; type_mask |= IORESOURCE_IO | IORESOURCE_MEM; @@ -99,10 +143,9 @@ pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res, if (!(res->flags & IORESOURCE_MEM_64)) max = PCIBIOS_MAX_MEM_32; - pci_bus_for_each_resource(bus, r, i) { - if (!r) - continue; - + /* Look for space at highest addresses first */ + r = pci_bus_find_resource_prev(bus, type, NULL); + for ( ; r; r = pci_bus_find_resource_prev(bus, type, r)) { /* type_mask must match */ if ((res->flags ^ r->flags) & type_mask) continue;