[PATCH] ppc64: remove duplication in pci_alloc_*

Anton Blanchard anton at samba.org
Sun Nov 14 17:28:01 EST 2004


We duplicated the code in pci_alloc_pci_controller twice and had an
ifdef for iseries as well, just to select between kmalloc and bootmem
memory. Change this so we instead pass the allocation into a common
function - pci_setup_pci_controller.

Also use a spinlock around the host_list and global_phb_number code
since we now can modify it at runtime via hotplug.

Signed-off-by: Anton Blanchard <anton at samba.org>

diff -puN arch/ppc64/kernel/pSeries_pci.c~pci3 arch/ppc64/kernel/pSeries_pci.c
--- gr_base/arch/ppc64/kernel/pSeries_pci.c~pci3	2004-11-12 04:50:36.956624582 -0600
+++ gr_base-anton/arch/ppc64/kernel/pSeries_pci.c	2004-11-12 04:50:36.994618384 -0600
@@ -266,48 +266,61 @@ static int phb_set_bus_ranges(struct dev
 	return 0;
 }
 
-static struct pci_controller *alloc_phb(struct device_node *dev,
-				 unsigned int addr_size_words)
+static int __devinit setup_phb(struct device_node *dev,
+			       struct pci_controller *phb,
+			       unsigned int addr_size_words)
 {
-	struct pci_controller *phb;
 	struct reg_property64 reg_struct;
-	struct property *of_prop;
-	int rc;
 
-	rc = get_phb_reg_prop(dev, addr_size_words, &reg_struct);
-	if (rc)
-		return NULL;
+	if (get_phb_reg_prop(dev, addr_size_words, &reg_struct))
+		return 1;
 
-	phb = pci_alloc_pci_controller();
-	if (phb == NULL)
-		return NULL;
+	pci_setup_pci_controller(phb);
 
 	if (is_python(dev))
 		python_countermeasures(reg_struct.address);
 
-	rc = phb_set_bus_ranges(dev, phb);
-	if (rc)
-		return NULL;
+	if (phb_set_bus_ranges(dev, phb))
+		return 1;
 
-	of_prop = (struct property *)alloc_bootmem(sizeof(struct property) +
-			sizeof(phb->global_number));        
+	phb->arch_data = dev;
+	phb->ops = &rtas_pci_ops;
+	phb->buid = get_phb_buid(dev);
 
-	if (!of_prop) {
-		kfree(phb);
-		return NULL;
-	}
+	return 0;
+}
 
+static void __devinit add_linux_pci_domain(struct device_node *dev,
+					   struct pci_controller *phb,
+					   struct property *of_prop)
+{
 	memset(of_prop, 0, sizeof(struct property));
 	of_prop->name = "linux,pci-domain";
 	of_prop->length = sizeof(phb->global_number);
 	of_prop->value = (unsigned char *)&of_prop[1];
 	memcpy(of_prop->value, &phb->global_number, sizeof(phb->global_number));
 	prom_add_property(dev, of_prop);
+}
 
-	phb->arch_data   = dev;
-	phb->ops = &rtas_pci_ops;
+static struct pci_controller * __init alloc_phb(struct device_node *dev,
+						unsigned int addr_size_words)
+{
+	struct pci_controller *phb;
+	struct property *of_prop;
 
-	phb->buid = get_phb_buid(dev);
+	phb = (struct pci_controller *)alloc_bootmem(sizeof(struct pci_controller));
+	if (phb == NULL)
+		return NULL;
+
+	of_prop = (struct property *)alloc_bootmem(sizeof(struct property) +
+			sizeof(phb->global_number));        
+	if (!of_prop)
+		return NULL;
+
+	if (setup_phb(dev, phb, addr_size_words))
+		return NULL;
+
+	add_linux_pci_domain(dev, phb, of_prop);
 
 	return phb;
 }
@@ -315,30 +328,18 @@ static struct pci_controller *alloc_phb(
 static struct pci_controller * __devinit alloc_phb_dynamic(struct device_node *dev, unsigned int addr_size_words)
 {
 	struct pci_controller *phb;
-	struct reg_property64 reg_struct;
-	int rc;
-
-	rc = get_phb_reg_prop(dev, addr_size_words, &reg_struct);
-	if (rc)
-		return NULL;
 
-	phb = pci_alloc_phb_dynamic();
+	phb = (struct pci_controller *)kmalloc(sizeof(struct pci_controller),
+					       GFP_KERNEL);
 	if (phb == NULL)
 		return NULL;
 
-	if (is_python(dev))
-		python_countermeasures(reg_struct.address);
-
-	rc = phb_set_bus_ranges(dev, phb);
-	if (rc)
+	if (setup_phb(dev, phb, addr_size_words))
 		return NULL;
 
-	/* TODO: linux,pci-domain? */
-
-	phb->arch_data   = dev;
-	phb->ops = &rtas_pci_ops;
+	phb->is_dynamic = 1;
 
-	phb->buid = get_phb_buid(dev);
+	/* TODO: linux,pci-domain? */
 
  	return phb;
 }
diff -puN arch/ppc64/kernel/pci.c~pci3 arch/ppc64/kernel/pci.c
--- gr_base/arch/ppc64/kernel/pci.c~pci3	2004-11-12 04:50:36.962623603 -0600
+++ gr_base-anton/arch/ppc64/kernel/pci.c	2004-11-12 04:50:36.996618058 -0600
@@ -157,54 +157,19 @@ void pcibios_align_resource(void *data, 
 	res->start = start;
 }
 
-/*
- * Allocate pci_controller(phb) initialized common variables.
- */
-struct pci_controller * __init pci_alloc_pci_controller()
-{
-	struct pci_controller *hose;
-
-#ifdef CONFIG_PPC_ISERIES
-	hose = (struct pci_controller *)kmalloc(sizeof(struct pci_controller),
-						GFP_KERNEL);
-#else
-	hose = (struct pci_controller *)alloc_bootmem(sizeof(struct pci_controller));
-#endif
-	if (hose == NULL) {
-		printk(KERN_ERR "PCI: Allocate pci_controller failed.\n");
-		return NULL;
-	}
-	memset(hose, 0, sizeof(struct pci_controller));
-
-	hose->is_dynamic = 0;
-	hose->global_number = global_phb_number++;
-
-	list_add_tail(&hose->list_node, &hose_list);
-
-	return hose;
-}
+static spinlock_t hose_spinlock = SPIN_LOCK_UNLOCKED;
 
 /*
- * Dymnamically allocate pci_controller(phb), initialize common variables.
+ * pci_controller(phb) initialized common variables.
  */
-struct pci_controller * pci_alloc_phb_dynamic()
+void __devinit pci_setup_pci_controller(struct pci_controller *hose)
 {
-	struct pci_controller *hose;
-
-	hose = (struct pci_controller *)kmalloc(sizeof(struct pci_controller),
-						GFP_KERNEL);
-	if(hose == NULL) {
-		printk(KERN_ERR "PCI: Allocate pci_controller failed.\n");
-		return NULL;
-	}
 	memset(hose, 0, sizeof(struct pci_controller));
 
-	hose->is_dynamic = 1;
+	spin_lock(&hose_spinlock);
 	hose->global_number = global_phb_number++;
-
 	list_add_tail(&hose->list_node, &hose_list);
-
-	return hose;
+	spin_unlock(&hose_spinlock);
 }
 
 static void __init pcibios_claim_one_bus(struct pci_bus *b)
diff -puN arch/ppc64/kernel/pci.h~pci3 arch/ppc64/kernel/pci.h
--- gr_base/arch/ppc64/kernel/pci.h~pci3	2004-11-12 04:50:36.967622788 -0600
+++ gr_base-anton/arch/ppc64/kernel/pci.h	2004-11-12 04:50:36.997617894 -0600
@@ -14,8 +14,7 @@
 
 extern unsigned long isa_io_base;
 
-extern struct pci_controller* pci_alloc_pci_controller(void);
-extern struct pci_controller* pci_alloc_phb_dynamic(void);
+extern void pci_setup_pci_controller(struct pci_controller *hose);
 extern void pci_setup_phb_io(struct pci_controller *hose, int primary);
 
 extern struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node);
diff -puN arch/ppc64/kernel/pmac_pci.c~pci3 arch/ppc64/kernel/pmac_pci.c
--- gr_base/arch/ppc64/kernel/pmac_pci.c~pci3	2004-11-12 04:50:36.972621972 -0600
+++ gr_base-anton/arch/ppc64/kernel/pmac_pci.c	2004-11-12 04:50:36.999617568 -0600
@@ -614,9 +614,11 @@ static int __init add_bridge(struct devi
        			       dev->full_name);
        	}
 
-       	hose = pci_alloc_pci_controller();
-       	if (!hose)
-       		return -ENOMEM;
+	hose = (struct pci_controller *)alloc_bootmem(sizeof(struct pci_controller));
+	if (hose == NULL)
+		return -ENOMEM;
+       	pci_setup_pci_controller(hose);
+
        	hose->arch_data = dev;
        	hose->first_busno = bus_range ? bus_range[0] : 0;
        	hose->last_busno = bus_range ? bus_range[1] : 0xff;
diff -puN arch/ppc64/kernel/maple_pci.c~pci3 arch/ppc64/kernel/maple_pci.c
--- gr_base/arch/ppc64/kernel/maple_pci.c~pci3	2004-11-12 04:50:36.977621157 -0600
+++ gr_base-anton/arch/ppc64/kernel/maple_pci.c	2004-11-12 04:50:37.001617242 -0600
@@ -325,9 +325,11 @@ static int __init add_bridge(struct devi
        			       dev->full_name);
        	}
 
-       	hose = pci_alloc_pci_controller();
-       	if (!hose)
-       		return -ENOMEM;
+	hose = (struct pci_controller *)alloc_bootmem(sizeof(struct pci_controller));
+	if (hose == NULL)
+		return -ENOMEM;
+       	pci_setup_pci_controller(hose);
+
        	hose->arch_data = dev;
        	hose->first_busno = bus_range ? bus_range[0] : 0;
        	hose->last_busno = bus_range ? bus_range[1] : 0xff;
diff -puN arch/ppc64/kernel/iSeries_pci.c~pci3 arch/ppc64/kernel/iSeries_pci.c
--- gr_base/arch/ppc64/kernel/iSeries_pci.c~pci3	2004-11-12 04:50:36.982620341 -0600
+++ gr_base-anton/arch/ppc64/kernel/iSeries_pci.c	2004-11-12 04:50:37.003616916 -0600
@@ -256,9 +256,12 @@ unsigned long __init find_and_init_phbs(
 		int ret = HvCallXm_testBus(bus);
 		if (ret == 0) {
 			printk("bus %d appears to exist\n", bus);
-			phb = pci_alloc_pci_controller();
+
+			phb = (struct pci_controller *)kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
 			if (phb == NULL)
-				return -1;
+				return -ENOMEM;
+       			pci_setup_pci_controller(phb);
+
 			phb->pci_mem_offset = phb->local_number = bus;
 			phb->first_busno = bus;
 			phb->last_busno = bus;
_



More information about the Linuxppc64-dev mailing list