99
1010struct pci_root_info {
1111 struct acpi_device * bridge ;
12- char * name ;
12+ char name [ 16 ] ;
1313 unsigned int res_num ;
1414 struct resource * res ;
15- struct list_head * resources ;
1615 int busnum ;
16+ struct pci_sysdata sd ;
1717};
1818
1919static bool pci_use_crs = true;
@@ -287,7 +287,8 @@ static void coalesce_windows(struct pci_root_info *info, unsigned long type)
287287 }
288288}
289289
290- static void add_resources (struct pci_root_info * info )
290+ static void add_resources (struct pci_root_info * info ,
291+ struct list_head * resources )
291292{
292293 int i ;
293294 struct resource * res , * root , * conflict ;
@@ -311,53 +312,74 @@ static void add_resources(struct pci_root_info *info)
311312 "ignoring host bridge window %pR (conflicts with %s %pR)\n" ,
312313 res , conflict -> name , conflict );
313314 else
314- pci_add_resource (info -> resources , res );
315+ pci_add_resource (resources , res );
315316 }
316317}
317318
319+ static void free_pci_root_info_res (struct pci_root_info * info )
320+ {
321+ kfree (info -> res );
322+ info -> res = NULL ;
323+ info -> res_num = 0 ;
324+ }
325+
326+ static void __release_pci_root_info (struct pci_root_info * info )
327+ {
328+ int i ;
329+ struct resource * res ;
330+
331+ for (i = 0 ; i < info -> res_num ; i ++ ) {
332+ res = & info -> res [i ];
333+
334+ if (!res -> parent )
335+ continue ;
336+
337+ if (!(res -> flags & (IORESOURCE_MEM | IORESOURCE_IO )))
338+ continue ;
339+
340+ release_resource (res );
341+ }
342+
343+ free_pci_root_info_res (info );
344+
345+ kfree (info );
346+ }
347+ static void release_pci_root_info (struct pci_host_bridge * bridge )
348+ {
349+ struct pci_root_info * info = bridge -> release_data ;
350+
351+ __release_pci_root_info (info );
352+ }
353+
318354static void
319- get_current_resources (struct acpi_device * device , int busnum ,
320- int domain , struct list_head * resources )
355+ probe_pci_root_info (struct pci_root_info * info , struct acpi_device * device ,
356+ int busnum , int domain )
321357{
322- struct pci_root_info info ;
323358 size_t size ;
324359
325- info .bridge = device ;
326- info .res_num = 0 ;
327- info .resources = resources ;
360+ info -> bridge = device ;
361+ info -> res_num = 0 ;
328362 acpi_walk_resources (device -> handle , METHOD_NAME__CRS , count_resource ,
329- & info );
330- if (!info . res_num )
363+ info );
364+ if (!info -> res_num )
331365 return ;
332366
333- size = sizeof (* info .res ) * info .res_num ;
334- info .res = kmalloc (size , GFP_KERNEL );
335- if (!info .res )
367+ size = sizeof (* info -> res ) * info -> res_num ;
368+ info -> res_num = 0 ;
369+ info -> res = kmalloc (size , GFP_KERNEL );
370+ if (!info -> res )
336371 return ;
337372
338- info .name = kasprintf (GFP_KERNEL , "PCI Bus %04x:%02x" , domain , busnum );
339- if (!info .name )
340- goto name_alloc_fail ;
373+ sprintf (info -> name , "PCI Bus %04x:%02x" , domain , busnum );
341374
342- info .res_num = 0 ;
343375 acpi_walk_resources (device -> handle , METHOD_NAME__CRS , setup_resource ,
344- & info );
345-
346- if (pci_use_crs ) {
347- add_resources (& info );
348-
349- return ;
350- }
351-
352- kfree (info .name );
353-
354- name_alloc_fail :
355- kfree (info .res );
376+ info );
356377}
357378
358379struct pci_bus * __devinit pci_acpi_scan_root (struct acpi_pci_root * root )
359380{
360381 struct acpi_device * device = root -> device ;
382+ struct pci_root_info * info = NULL ;
361383 int domain = root -> segment ;
362384 int busnum = root -> secondary .start ;
363385 LIST_HEAD (resources );
@@ -389,17 +411,14 @@ struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_pci_root *root)
389411 if (node != -1 && !node_online (node ))
390412 node = -1 ;
391413
392- /* Allocate per-root-bus (not per bus) arch-specific data.
393- * TODO: leak; this memory is never freed.
394- * It's arguable whether it's worth the trouble to care.
395- */
396- sd = kzalloc (sizeof (* sd ), GFP_KERNEL );
397- if (!sd ) {
414+ info = kzalloc (sizeof (* info ), GFP_KERNEL );
415+ if (!info ) {
398416 printk (KERN_WARNING "pci_bus %04x:%02x: "
399417 "ignored (out of memory)\n" , domain , busnum );
400418 return NULL ;
401419 }
402420
421+ sd = & info -> sd ;
403422 sd -> domain = domain ;
404423 sd -> node = node ;
405424 /*
@@ -413,22 +432,32 @@ struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_pci_root *root)
413432 * be replaced by sd.
414433 */
415434 memcpy (bus -> sysdata , sd , sizeof (* sd ));
416- kfree (sd );
435+ kfree (info );
417436 } else {
418- get_current_resources ( device , busnum , domain , & resources );
437+ probe_pci_root_info ( info , device , busnum , domain );
419438
420439 /*
421440 * _CRS with no apertures is normal, so only fall back to
422441 * defaults or native bridge info if we're ignoring _CRS.
423442 */
424- if (!pci_use_crs )
443+ if (pci_use_crs )
444+ add_resources (info , & resources );
445+ else {
446+ free_pci_root_info_res (info );
425447 x86_pci_root_bus_resources (busnum , & resources );
448+ }
449+
426450 bus = pci_create_root_bus (NULL , busnum , & pci_root_ops , sd ,
427451 & resources );
428- if (bus )
452+ if (bus ) {
429453 bus -> subordinate = pci_scan_child_bus (bus );
430- else
454+ pci_set_host_bridge_release (
455+ to_pci_host_bridge (bus -> bridge ),
456+ release_pci_root_info , info );
457+ } else {
431458 pci_free_resource_list (& resources );
459+ __release_pci_root_info (info );
460+ }
432461 }
433462
434463 /* After the PCI-E bus has been walked and all devices discovered,
@@ -445,9 +474,6 @@ struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_pci_root *root)
445474 }
446475 }
447476
448- if (!bus )
449- kfree (sd );
450-
451477 if (bus && node != -1 ) {
452478#ifdef CONFIG_ACPI_NUMA
453479 if (pxm >= 0 )
0 commit comments